8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
Reviewed-by: sspitsyn, dholmes
--- a/src/jdk.jcmd/linux/classes/sun/tools/ProcessHelper.java Mon Jun 17 09:11:27 2019 -0700
+++ b/src/jdk.jcmd/linux/classes/sun/tools/ProcessHelper.java Thu Jun 13 11:21:50 2019 -0700
@@ -66,7 +66,7 @@
if (cmdLine.startsWith(CMD_PREFIX)) {
cmdLine = cmdLine.substring(CMD_PREFIX.length());
}
- String[] parts = cmdLine.split(" ");
+ String[] parts = cmdLine.split("\0");
String mainClass = null;
if(parts.length == 0) {
@@ -87,7 +87,7 @@
// options are used then just return the value (the path to the jar file or module
// name with a main class). Otherwise, the main class name is the first part that
// is not a Java option (doesn't start with '-' and is not a classpath or a module
- // path).
+ // whitespace option).
for (int i = 1; i < parts.length && mainClass == null; i++) {
if (i < parts.length - 1) {
@@ -95,10 +95,15 @@
return parts[i + 1];
}
}
- // If this is a classpath or a module path option then skip the next part
- // (the classpath or the module path itself)
+
+ if (parts[i].startsWith("--module=")) {
+ return parts[i].substring("--module=".length());
+ }
+
+ // If this is a classpath or a module whitespace option then skip the next part
+ // (the classpath or the option value itself)
if (parts[i].equals("-cp") || parts[i].equals("-classpath") || parts[i].equals("--class-path") ||
- parts[i].equals("-p") || parts[i].equals("--module-path")) {
+ isModuleWhiteSpaceOption(parts[i])) {
i++;
continue;
}
@@ -106,6 +111,12 @@
if (parts[i].startsWith("-")) {
continue;
}
+
+ // If it is a source-file mode then return null
+ if (parts[i].endsWith(".java")) {
+ return null;
+ }
+
mainClass = parts[i];
}
return mainClass;
@@ -115,11 +126,24 @@
private static String getCommandLine(String pid) {
try (Stream<String> lines =
Files.lines(Paths.get("/proc/" + pid + "/cmdline"))) {
- return lines.map(x -> x.replaceAll("\0", " ")).findFirst().orElse(null);
+ return lines.findFirst().orElse(null);
} catch (IOException | UncheckedIOException e) {
return null;
}
}
+
+ private static boolean isModuleWhiteSpaceOption(String option) {
+ return option.equals("-p") ||
+ option.equals("--module-path") ||
+ option.equals("--upgrade-module-path") ||
+ option.equals("--add-modules") ||
+ option.equals("--limit-modules") ||
+ option.equals("--add-exports") ||
+ option.equals("--add-opens") ||
+ option.equals("--add-reads") ||
+ option.equals("--patch-module");
+ }
+
}
--- a/test/jdk/sun/tools/jcmd/TestProcessHelper.java Mon Jun 17 09:11:27 2019 -0700
+++ b/test/jdk/sun/tools/jcmd/TestProcessHelper.java Thu Jun 13 11:21:50 2019 -0700
@@ -73,13 +73,21 @@
.resolve(TEST_PROCESS_MAIN_CLASS_NAME + ".class");
private static final String[] CP_OPTIONS = {"-cp", "-classpath", "--class-path"};
- private static final String[][] VM_ARGS = {{}, {"-Dtest1=aaa"}, {"-Dtest1=aaa", "-Dtest2=bbb"}};
+ private static final String[][] VM_ARGS = {{}, {"-Dtest1=aaa"}, {"-Dtest1=aaa", "-Dtest2=bbb ccc"}};
private static final String[][] ARGS = {{}, {"param1"}, {"param1", "param2"}};
private static final String[] MP_OPTIONS = {"-p", "--module-path"};
- private static final String[] MODULE_OPTIONS = {"-m", "--module"};
+ private static final String[] MODULE_OPTIONS = {"-m", "--module", "--module="};
private static final String JAR_OPTION = "-jar";
private static final String MODULE_NAME = "module1";
+ private static final String[][] EXTRA_MODULAR_OPTIONS = {null,
+ {"--add-opens", "java.base/java.net=ALL-UNNAMED"},
+ {"--add-exports", "java.base/java.net=ALL-UNNAMED"},
+ {"--add-reads", "java.base/java.net=ALL-UNNAMED"},
+ {"--add-modules", "java.management"},
+ {"--limit-modules", "java.management"},
+ {"--upgrade-module-path", "test"}};
+ private static final String[] PATCH_MODULE_OPTIONS = {"--patch-module", null};
public static void main(String[] args) throws Exception {
new TestProcessHelper().runTests();
@@ -97,18 +105,24 @@
for (String cp : CP_OPTIONS) {
for (String[] vma : VM_ARGS) {
for (String[] arg : ARGS) {
- List<String> cmd = new LinkedList<>();
- cmd.add(JAVA_PATH);
- cmd.add(cp);
- cmd.add(TEST_CLASSES.toAbsolutePath().toString());
- for (String v : vma) {
- cmd.add(v);
+ for (String[] modularOptions : EXTRA_MODULAR_OPTIONS) {
+ List<String> cmd = new LinkedList<>();
+ cmd.add(JAVA_PATH);
+ cmd.add(cp);
+ cmd.add(TEST_CLASSES.toAbsolutePath().toString());
+ for (String v : vma) {
+ cmd.add(v);
+ }
+ if (modularOptions != null) {
+ cmd.add(modularOptions[0]);
+ cmd.add(modularOptions[1]);
+ }
+ cmd.add(TEST_PROCESS_MAIN_CLASS);
+ for (String a : arg) {
+ cmd.add(a);
+ }
+ testProcessHelper(cmd, TEST_PROCESS_MAIN_CLASS);
}
- cmd.add(TEST_PROCESS_MAIN_CLASS);
- for (String a : arg) {
- cmd.add(a);
- }
- testProcessHelper(cmd, TEST_PROCESS_MAIN_CLASS);
}
}
}
@@ -144,19 +158,29 @@
for (String m : MODULE_OPTIONS) {
for (String[] vma : VM_ARGS) {
for (String[] arg : ARGS) {
- List<String> cmd = new LinkedList<>();
- cmd.add(JAVA_PATH);
- cmd.add(mp);
- cmd.add(TEST_MODULES.toAbsolutePath().toString());
- for (String v : vma) {
- cmd.add(v);
+ for(String patchModuleOption : PATCH_MODULE_OPTIONS) {
+ List<String> cmd = new LinkedList<>();
+ cmd.add(JAVA_PATH);
+ cmd.add(mp);
+ cmd.add(TEST_MODULES.toAbsolutePath().toString());
+ if (patchModuleOption != null) {
+ cmd.add(patchModuleOption);
+ cmd.add(MODULE_NAME + "=" + TEST_MODULES.toAbsolutePath().toString());
+ }
+ for (String v : vma) {
+ cmd.add(v);
+ }
+ if (m.endsWith("=")) {
+ cmd.add(m + MODULE_NAME + "/" + TEST_PROCESS_MAIN_CLASS);
+ } else {
+ cmd.add(m);
+ cmd.add(MODULE_NAME + "/" + TEST_PROCESS_MAIN_CLASS);
+ }
+ for (String a : arg) {
+ cmd.add(a);
+ }
+ testProcessHelper(cmd, MODULE_NAME + "/" + TEST_PROCESS_MAIN_CLASS);
}
- cmd.add(m);
- cmd.add(MODULE_NAME + "/" + TEST_PROCESS_MAIN_CLASS);
- for (String a : arg) {
- cmd.add(a);
- }
- testProcessHelper(cmd, MODULE_NAME + "/" + TEST_PROCESS_MAIN_CLASS);
}
}
}