Skip to content

Commit 987006c

Browse files
authored
Use Java home of the crashed process to launch crash uploader (#8348)
1 parent ec92bff commit 987006c

File tree

7 files changed

+26
-8
lines changed

7 files changed

+26
-8
lines changed

dd-java-agent/agent-crashtracking/src/main/java/com/datadog/crashtracking/CrashUploaderScriptInitializer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ private static void writeCrashUploaderScript(
109109

110110
private static String template(String line, String execClass, String crashFile) {
111111
line = Strings.replace(line, "!AGENT_JAR!", execClass);
112+
line = Strings.replace(line, "!JAVA_HOME!", System.getProperty("java.home"));
112113
if (crashFile != null) {
113114
line = Strings.replace(line, "!JAVA_ERROR_FILE!", crashFile);
114115
}

dd-java-agent/agent-crashtracking/src/main/java/com/datadog/crashtracking/ScriptInitializer.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ static void writeConfig(Path scriptPath, String... entries) {
9090
bw.write(entries[i + 1]);
9191
bw.newLine();
9292
}
93+
bw.write("java_home=" + System.getProperty("java.home"));
94+
bw.newLine();
95+
9396
Runtime.getRuntime()
9497
.addShutdownHook(
9598
new Thread(

dd-java-agent/agent-crashtracking/src/main/resources/com/datadog/crashtracking/notify_oome.bat

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ for /f "tokens=1,2 delims=: " %%a in (%configFile%.cfg) do (
3131
:: Debug: Print the loaded values (Optional)
3232
echo Agent Jar: %agent%
3333
echo Tags: %tags%
34+
echo JAVA_HOME: %java_home%
3435
echo PID: %PID%
3536

3637
:: Execute the Java command with the loaded values
37-
java -Ddd.dogstatsd.start-delay=0 -jar "%agent%" sendOomeEvent "%tags%"
38+
"%java_home%\bin\java" -Ddd.dogstatsd.start-delay=0 -jar "%agent%" sendOomeEvent "%tags%"
3839
set RC=%ERRORLEVEL%
3940
del "%configFile%" :: Clean up the configuration file
4041
if %RC% EQU 0 (

dd-java-agent/agent-crashtracking/src/main/resources/com/datadog/crashtracking/notify_oome.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,19 @@ while IFS="=" read -r key value; do
2727
done < "$configFile"
2828

2929
# Exiting early if configuration is missing
30-
if [ -z "${config_agent}" ] || [ -z "${config_tags}" ]; then
30+
if [ -z "${config_agent}" ] || [ -z "${config_tags}" ] || [ -z "${config_java_home}" ]; then
3131
echo "Error: Missing configuration"
3232
exit 1
3333
fi
3434

3535
# Debug: Print the loaded values (Optional)
3636
echo "Agent Jar: ${config_agent}"
3737
echo "Tags: ${config_tags}"
38+
echo "JAVA_HOME: ${config_java_home}"
3839
echo "PID: $PID"
3940

4041
# Execute the Java command with the loaded values
41-
java -Ddd.dogstatsd.start-delay=0 -jar "${config_agent}" sendOomeEvent "${config_tags}"
42+
"${config_java_home}/bin/java" -Ddd.dogstatsd.start-delay=0 -jar "${config_agent}" sendOomeEvent "${config_tags}"
4243
RC=$?
4344
rm -f "${configFile}" # Remove the configuration file
4445

dd-java-agent/agent-crashtracking/src/main/resources/com/datadog/crashtracking/upload_crash.bat

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ setlocal enabledelayedexpansion
44
:: Check if PID is provided
55
if "%1"=="" (
66
echo "Error: No PID provided. Running in legacy mode."
7-
java -jar "!AGENT_JAR!" uploadCrash "!JAVA_ERROR_FILE!"
7+
call :ensureJava "!JAVA_ERROR_FILE!"
8+
"!JAVA_HOME!\bin\java" -jar "!AGENT_JAR!" uploadCrash "!JAVA_ERROR_FILE!"
89
if %ERRORLEVEL% EQU 0 (
910
echo "Uploaded error file \"!JAVA_ERROR_FILE!\""
1011
) else (
@@ -38,10 +39,11 @@ for /f "tokens=1,2 delims=: " %%a in (%configFile%.cfg) do (
3839
:: Debug: Print the loaded values (Optional)
3940
echo Agent Jar: %agent%
4041
echo Error Log: %hs_err%
42+
echo JAVA_HOME: %java_home%
4143
echo PID: %PID%
4244

4345
:: Execute the Java command with the loaded values
44-
java -jar "%agent%" uploadCrash "%hs_err%"
46+
"%java_home%\bin\java" -jar "%agent%" uploadCrash "%hs_err%"
4547
set RC=%ERRORLEVEL%
4648
del "%configFile%" :: Clean up the configuration file
4749
if %RC% EQU 0 (

dd-java-agent/agent-crashtracking/src/main/resources/com/datadog/crashtracking/upload_crash.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ set +e # Disable exit on error
55
# Check if PID is provided
66
if [ -z "$1" ]; then
77
echo "Warn: No PID provided. Running in legacy mode."
8-
java -jar "!AGENT_JAR!" uploadCrash "!JAVA_ERROR_FILE!"
8+
9+
"!JAVA_HOME!/bin/java" -jar "!AGENT_JAR!" uploadCrash "!JAVA_ERROR_FILE!"
910
if [ $? -eq 0 ]; then
1011
echo "Error file !JAVA_ERROR_FILE! was uploaded successfully"
1112
else
@@ -35,18 +36,19 @@ while IFS="=" read -r key value; do
3536
done < "$configFile"
3637

3738
# Exiting early if configuration is missing
38-
if [ -z "${config_agent}" ] || [ -z "${config_hs_err}" ]; then
39+
if [ -z "${config_agent}" ] || [ -z "${config_hs_err}" ] || [ -z "${config_java_home}" ]; then
3940
echo "Error: Missing configuration"
4041
exit 1
4142
fi
4243

4344
# Debug: Print the loaded values (Optional)
4445
echo "Agent Jar: ${config_agent}"
4546
echo "Error Log: ${config_hs_err}"
47+
echo "JAVA_HOME: ${config_java_home}"
4648
echo "PID: $PID"
4749

4850
# Execute the Java command with the loaded values
49-
java -jar "${config_agent}" uploadCrash "${config_hs_err}"
51+
"${config_java_home}/bin/java" -jar "${config_agent}" uploadCrash "${config_hs_err}"
5052
RC=$?
5153
rm -f "${configFile}" # Remove the configuration file
5254

dd-java-agent/agent-crashtracking/src/test/java/com/datadog/crashtracking/ScriptInitializerTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.nio.file.attribute.PosixFilePermissions;
1212
import java.util.Comparator;
1313
import java.util.List;
14+
import java.util.regex.Pattern;
1415
import java.util.stream.Stream;
1516
import org.junit.jupiter.api.AfterEach;
1617
import org.junit.jupiter.api.BeforeEach;
@@ -63,8 +64,13 @@ void testCrashUploaderInitializationSuccess(String target, String pidArg)
6364
assertTrue(Files.exists(file), "File " + file + " should have been created");
6465
List<String> lines = Files.readAllLines(file);
6566
assertFalse(lines.isEmpty(), "File " + file + " is expected to be non-empty");
67+
// sanity check to see if no placeholders are left
68+
Pattern placeholder = Pattern.compile("![A-Z_]+!");
69+
assertFalse(lines.stream().anyMatch(l -> placeholder.matcher(l).find()));
6670
// sanity to check the crash log file was properly replaced in the script
6771
assertTrue(lines.stream().anyMatch(l -> l.contains(hsErrFile)));
72+
// sanity to check the java home was properly captured
73+
assertTrue(lines.stream().anyMatch(l -> l.contains("java_home")));
6874
}
6975

7076
@Test
@@ -96,6 +102,8 @@ void testOomeNotifierInitializationSuccess(String target) throws IOException {
96102
assertFalse(lines.isEmpty(), "File " + file + " is expected to be non-empty");
97103
// sanity to check the placeholder was properly replaced
98104
assertTrue(lines.stream().anyMatch(l -> !l.contains("!TAGS!")));
105+
// sanity to check the java home was properly captured
106+
assertTrue(lines.stream().anyMatch(l -> l.contains("java_home")));
99107
}
100108

101109
@Test

0 commit comments

Comments
 (0)