CoreCollector { Map<String, Object> collect(NodeAgentContext context, Path coredumpPath) { Map<String, Object> data = new HashMap<>(); try { Path binPath = readBinPath(context, coredumpPath); data.put("bin_path", binPath.toString()); if (binPath.getFileName().toString().equals("java")) { data.put("backtrace_all_threads", readJstack(context, coredumpPath, binPath)); } else { data.put("backtrace", readBacktrace(context, coredumpPath, binPath, false)); data.put("backtrace_all_threads", readBacktrace(context, coredumpPath, binPath, true)); } } catch (RuntimeException e) { context.log(logger, Level.WARNING, "Failed to extract backtrace", e); } return data; } CoreCollector(ContainerOperations docker);   }
@Test public void collectsDataTest() { mockExec(new String[]{"file", TEST_CORE_PATH.toString()}, "/tmp/core.1234: ELF 64-bit LSB core file x86-64, version 1 (SYSV), SVR4-style, from " + "'/usr/bin/program'"); mockExec(new String[]{GDB_PATH, "-n", "-ex", "bt", "-batch", "/usr/bin/program", "/tmp/core.1234"}, String.join("\n", GDB_BACKTRACE)); mockExec(new String[]{GDB_PATH, "-n", "-ex", "thread apply all bt", "-batch", "/usr/bin/program", "/tmp/core.1234"}, String.join("\n", GDB_BACKTRACE)); Map<String, Object> expectedData = Map.of( "bin_path", TEST_BIN_PATH.toString(), "backtrace", GDB_BACKTRACE, "backtrace_all_threads", GDB_BACKTRACE); assertEquals(expectedData, coreCollector.collect(context, TEST_CORE_PATH)); }
@Test public void collectsPartialIfBacktraceFailsTest() { mockExec(new String[]{"file", TEST_CORE_PATH.toString()}, "/tmp/core.1234: ELF 64-bit LSB core file x86-64, version 1 (SYSV), SVR4-style, from " + "'/usr/bin/program'"); mockExec(new String[]{GDB_PATH + " -n -ex bt -batch /usr/bin/program /tmp/core.1234"}, "", "Failure"); Map<String, Object> expectedData = Map.of("bin_path", TEST_BIN_PATH.toString()); assertEquals(expectedData, coreCollector.collect(context, TEST_CORE_PATH)); }
@Test public void reportsJstackInsteadOfGdbForJdkCores() { mockExec(new String[]{"file", TEST_CORE_PATH.toString()}, "dump.core.5954: ELF 64-bit LSB core file x86-64, version 1 (SYSV), too many program header sections (33172)"); mockExec(new String[]{"/bin/sh", "-c", GDB_PATH + " -n -batch -core /tmp/core.1234 | grep '^Core was generated by'"}, "Core was generated by `" + JDK_PATH + " -Dconfig.id=default/container.11 -XX:+Pre'."); String jstack = "jstack11"; mockExec(new String[]{"jhsdb", "jstack", "--exe", JDK_PATH, "--core", "/tmp/core.1234"}, jstack); Map<String, Object> expectedData = Map.of( "bin_path", JDK_PATH, "backtrace_all_threads", List.of(jstack)); assertEquals(expectedData, coreCollector.collect(context, TEST_CORE_PATH)); }