Skip to content

Commit d7cc78c

Browse files
fix(environment): Exclude classpath from VM options (#9255)
1 parent 95673fa commit d7cc78c

File tree

2 files changed

+49
-54
lines changed

2 files changed

+49
-54
lines changed

components/environment/src/main/java/datadog/environment/JvmOptions.java

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -40,57 +40,11 @@ private String[] readProcFsCmdLine() {
4040
return null;
4141
}
4242

43-
private List<String> findVmOptions() {
44-
return findVmOptions(PROCFS_CMDLINE);
45-
}
46-
4743
@SuppressForbidden // Class.forName() as backup
48-
// Visible for testing
49-
List<String> findVmOptions(String[] procfsCmdline) {
44+
private List<String> findVmOptions() {
5045
// Try ProcFS on Linux
51-
// Be aware that when running a native image, the command line in /proc/self/cmdline is just the
52-
// executable
53-
if (procfsCmdline != null) {
54-
// Create list of VM options
55-
List<String> vmOptions = new ArrayList<>();
56-
// Start at 1 to skip "java" command itself
57-
int index = 1;
58-
// Look for first self-standing argument that is not prefixed with "-" or end of VM options
59-
// Skip "-jar" and the jar file
60-
// Simultaneously, collect all arguments in the VM options
61-
for (; index < procfsCmdline.length; index++) {
62-
String argument = procfsCmdline[index];
63-
if (argument.startsWith("@")) {
64-
vmOptions.addAll(getArgumentsFromFile(argument));
65-
} else {
66-
if ("-jar".equals(argument)) {
67-
// skip "-jar" and the jar file
68-
index++;
69-
continue;
70-
} else if ("-cp".equals(argument)) {
71-
// slurp '-cp' and the classpath
72-
vmOptions.add(argument);
73-
if (index + 1 < procfsCmdline.length) {
74-
argument = procfsCmdline[++index];
75-
}
76-
} else if (!argument.startsWith("-")) {
77-
// end of VM options
78-
break;
79-
}
80-
vmOptions.add(argument);
81-
}
82-
}
83-
// Insert JDK_JAVA_OPTIONS at the start if present and supported
84-
List<String> jdkJavaOptions = getJdkJavaOptions();
85-
if (!jdkJavaOptions.isEmpty()) {
86-
vmOptions.addAll(0, jdkJavaOptions);
87-
}
88-
// Insert JAVA_TOOL_OPTIONS at the start if present
89-
List<String> javaToolOptions = getJavaToolOptions();
90-
if (!javaToolOptions.isEmpty()) {
91-
vmOptions.addAll(0, javaToolOptions);
92-
}
93-
return vmOptions;
46+
if (PROCFS_CMDLINE != null) {
47+
return findVmOptionsFromProcFs(PROCFS_CMDLINE);
9448
}
9549

9650
// Try Oracle-based
@@ -137,6 +91,48 @@ List<String> findVmOptions(String[] procfsCmdline) {
13791
return emptyList();
13892
}
13993

94+
// Be aware that when running a native image, the command line in /proc/self/cmdline is just the
95+
// executable
96+
// Visible for testing
97+
List<String> findVmOptionsFromProcFs(String[] procfsCmdline) {
98+
// Create list of VM options
99+
List<String> vmOptions = new ArrayList<>();
100+
// Look for first self-standing argument that is not prefixed with "-" or end of VM options
101+
// while simultaneously, collect all arguments in the VM options
102+
// Starts from 1 as 0 is the java command itself (or native-image)
103+
for (int index = 1; index < procfsCmdline.length; index++) {
104+
String argument = procfsCmdline[index];
105+
// Inflate arg files
106+
if (argument.startsWith("@")) {
107+
vmOptions.addAll(getArgumentsFromFile(argument));
108+
}
109+
// Skip classpath argument (not part of VM options)
110+
else if ("-cp".equals(argument)) {
111+
index++;
112+
}
113+
// Check "-jar" or class name argument as the end of the VM options
114+
else if ("-jar".equals(argument) || !argument.startsWith("-")) {
115+
// End of VM options
116+
break;
117+
}
118+
// Otherwise add as VM option
119+
else {
120+
vmOptions.add(argument);
121+
}
122+
}
123+
// Insert JDK_JAVA_OPTIONS at the start if present and supported
124+
List<String> jdkJavaOptions = getJdkJavaOptions();
125+
if (!jdkJavaOptions.isEmpty()) {
126+
vmOptions.addAll(0, jdkJavaOptions);
127+
}
128+
// Insert JAVA_TOOL_OPTIONS at the start if present
129+
List<String> javaToolOptions = getJavaToolOptions();
130+
if (!javaToolOptions.isEmpty()) {
131+
vmOptions.addAll(0, javaToolOptions);
132+
}
133+
return vmOptions;
134+
}
135+
140136
private static List<String> getArgumentsFromFile(String argFile) {
141137
String filename = argFile.substring(1);
142138
Path path = Paths.get(filename);

components/environment/src/test/java/datadog/environment/JvmOptionsTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,12 @@ private static Stream<Arguments> procFsCmdLine() {
135135
arguments(
136136
"Java from class and options",
137137
new String[]{"java", "-Xmx512m", "-Xms256m", "-cp", "app.jar", "Main"},
138-
asList("-Xmx512m", "-Xms256m", "-cp", "app.jar")
138+
asList("-Xmx512m", "-Xms256m")
139139
),
140140
arguments(
141141
"Java from class and options, mixed",
142142
new String[]{"java", "-Xms256m", "-cp", "app.jar", "-Xmx512m", "Main"},
143-
asList("-Xms256m", "-cp", "app.jar", "-Xmx512m")
143+
asList("-Xms256m", "-Xmx512m")
144144
),
145145
arguments(
146146
"Args from file",
@@ -167,10 +167,9 @@ private static Stream<Arguments> procFsCmdLine() {
167167

168168
@ParameterizedTest(name = "[{index}] {0}")
169169
@MethodSource("procFsCmdLine")
170-
void testFindVmOptionsWithProcFsCmdLine(
171-
String useCase, String[] procfsCmdline, List<String> expected) throws Exception {
170+
void testFindVmOptionsFromProcFs(String useCase, String[] procfsCmdline, List<String> expected) {
172171
JvmOptions vmOptions = new JvmOptions();
173-
List<String> found = vmOptions.findVmOptions(procfsCmdline);
172+
List<String> found = vmOptions.findVmOptionsFromProcFs(procfsCmdline);
174173
assertEquals(expected, found);
175174
}
176175

0 commit comments

Comments
 (0)