Skip to content

Commit 42a4122

Browse files
committed
Use Java home of the crashed process to launch crash uploader
1 parent b9533be commit 42a4122

File tree

6 files changed

+76
-6
lines changed

6 files changed

+76
-6
lines changed

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

+3
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

+2-1
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

+3-2
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

+43-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,50 @@
11
@echo off
22
setlocal enabledelayedexpansion
33

4+
REM ========================================================
5+
REM Function: ensureJava
6+
REM Usage: call :ensureJava path\to\hs_err_file.txt
7+
REM If 'java' is not found in PATH, extract JAVA_HOME from the
8+
REM hs_err file and update PATH accordingly.
9+
REM ========================================================
10+
:ensureJava
11+
REM Check if java is available
12+
where java >nul 2>&1
13+
if %ERRORLEVEL%==0 (
14+
REM Java found; nothing to do.
15+
goto :EOF
16+
)
17+
18+
REM Java not found; try to extract JAVA_HOME from the hs_err file passed as parameter.
19+
if "%~1"=="" (
20+
echo Error: No hs_err file provided.
21+
exit /b 1
22+
)
23+
24+
REM Use findstr to locate the line with JAVA_HOME.
25+
for /f "tokens=2 delims==" %%A in ('findstr "JAVA_HOME" "%~1"') do (
26+
set "JAVA_HOME=%%A"
27+
)
28+
29+
REM Check if JAVA_HOME was found
30+
if not defined JAVA_HOME (
31+
echo Error: Java executable not found. Cannot upload error file.
32+
exit /b 1
33+
)
34+
35+
REM Optionally, remove any surrounding quotes or spaces:
36+
set "JAVA_HOME=%JAVA_HOME:"=%"
37+
for /f "tokens=* delims= " %%A in ("%JAVA_HOME%") do set "JAVA_HOME=%%A"
38+
39+
REM Prepend JAVA_HOME\bin to PATH
40+
set "PATH=%JAVA_HOME%\bin;%PATH%"
41+
goto :EOF
42+
43+
444
:: Check if PID is provided
545
if "%1"=="" (
646
echo "Error: No PID provided. Running in legacy mode."
47+
call :ensureJava "!JAVA_ERROR_FILE!"
748
java -jar "!AGENT_JAR!" uploadCrash "!JAVA_ERROR_FILE!"
849
if %ERRORLEVEL% EQU 0 (
950
echo "Uploaded error file \"!JAVA_ERROR_FILE!\""
@@ -38,10 +79,11 @@ for /f "tokens=1,2 delims=: " %%a in (%configFile%.cfg) do (
3879
:: Debug: Print the loaded values (Optional)
3980
echo Agent Jar: %agent%
4081
echo Error Log: %hs_err%
82+
echo JAVA_HOME: %java_home%
4183
echo PID: %PID%
4284

4385
:: Execute the Java command with the loaded values
44-
java -jar "%agent%" uploadCrash "%hs_err%"
86+
%java_home%\bin\java -jar "%agent%" uploadCrash "%hs_err%"
4587
set RC=%ERRORLEVEL%
4688
del "%configFile%" :: Clean up the configuration file
4789
if %RC% EQU 0 (

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

+21-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,27 @@
22

33
set +e # Disable exit on error
44

5+
function ensureJava() {
6+
# Check if Java is available
7+
if [ -z "$(which java)" ]; then
8+
# Extract the JAVA_HOME from the provided hs_err file
9+
JAVA_HOME=$(grep "JAVA_HOME" "$1")
10+
if [ -n "$JAVA_HOME" ]; then
11+
JAVA_HOME=$(cut -f2 -d '=' <<< "$JAVA_HOME")
12+
export JAVA_HOME
13+
export PATH=$JAVA_HOME/bin:$PATH
14+
else
15+
echo "Error: Java executable not found. Can not upload error file."
16+
exit 1
17+
fi
18+
fi
19+
}
20+
521
# Check if PID is provided
622
if [ -z "$1" ]; then
723
echo "Warn: No PID provided. Running in legacy mode."
24+
ensureJava "!JAVA_ERROR_FILE!"
25+
826
java -jar "!AGENT_JAR!" uploadCrash "!JAVA_ERROR_FILE!"
927
if [ $? -eq 0 ]; then
1028
echo "Error file !JAVA_ERROR_FILE! was uploaded successfully"
@@ -35,18 +53,19 @@ while IFS="=" read -r key value; do
3553
done < "$configFile"
3654

3755
# Exiting early if configuration is missing
38-
if [ -z "${config_agent}" ] || [ -z "${config_hs_err}" ]; then
56+
if [ -z "${config_agent}" ] || [ -z "${config_hs_err}" ] || [ -z "${config_java_home}" ]; then
3957
echo "Error: Missing configuration"
4058
exit 1
4159
fi
4260

4361
# Debug: Print the loaded values (Optional)
4462
echo "Agent Jar: ${config_agent}"
4563
echo "Error Log: ${config_hs_err}"
64+
echo "JAVA_HOME: ${config_java_home}"
4665
echo "PID: $PID"
4766

4867
# Execute the Java command with the loaded values
49-
java -jar "${config_agent}" uploadCrash "${config_hs_err}"
68+
${config_java_home}/bin/java -jar "${config_agent}" uploadCrash "${config_hs_err}"
5069
RC=$?
5170
rm -f "${configFile}" # Remove the configuration file
5271

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

+4
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ void testCrashUploaderInitializationSuccess(String target, String pidArg)
6565
assertFalse(lines.isEmpty(), "File " + file + " is expected to be non-empty");
6666
// sanity to check the crash log file was properly replaced in the script
6767
assertTrue(lines.stream().anyMatch(l -> l.contains(hsErrFile)));
68+
// sanity to check the java home was properly captured
69+
assertTrue(lines.stream().anyMatch(l -> l.contains("java_home")));
6870
}
6971

7072
@Test
@@ -96,6 +98,8 @@ void testOomeNotifierInitializationSuccess(String target) throws IOException {
9698
assertFalse(lines.isEmpty(), "File " + file + " is expected to be non-empty");
9799
// sanity to check the placeholder was properly replaced
98100
assertTrue(lines.stream().anyMatch(l -> !l.contains("!TAGS!")));
101+
// sanity to check the java home was properly captured
102+
assertTrue(lines.stream().anyMatch(l -> l.contains("java_home")));
99103
}
100104

101105
@Test

0 commit comments

Comments
 (0)