8221730: jcmd process name matching broken
authordtitov
Mon, 08 Apr 2019 17:09:02 +0000
changeset 54460 6733a9176cce
parent 54459 e5713cefcf41
child 54461 ba0d64652b86
8221730: jcmd process name matching broken Reviewed-by: jcbeyler, dholmes, cjplummer
src/jdk.jcmd/linux/classes/sun/tools/ProcessHelper.java
test/hotspot/jtreg/serviceability/dcmd/framework/HelpTest.java
test/hotspot/jtreg/serviceability/dcmd/framework/InvalidCommandTest.java
test/hotspot/jtreg/serviceability/dcmd/framework/TestJavaProcess.java
test/hotspot/jtreg/serviceability/dcmd/framework/TestProcessJarLauncher.java
test/hotspot/jtreg/serviceability/dcmd/framework/TestProcessLauncher.java
test/hotspot/jtreg/serviceability/dcmd/framework/TestProcessModuleLauncher.java
test/hotspot/jtreg/serviceability/dcmd/framework/VMVersionTest.java
test/hotspot/jtreg/serviceability/dcmd/framework/process/TestJavaProcess.java
test/jdk/sun/tools/jcmd/TestProcessHelper.java
--- a/src/jdk.jcmd/linux/classes/sun/tools/ProcessHelper.java	Mon Apr 08 15:01:39 2019 +0100
+++ b/src/jdk.jcmd/linux/classes/sun/tools/ProcessHelper.java	Mon Apr 08 17:09:02 2019 +0000
@@ -31,7 +31,6 @@
 import java.nio.file.Paths;
 import java.util.jar.Attributes;
 import java.util.jar.JarFile;
-import java.util.jar.Manifest;
 import java.util.stream.Stream;
 
 /**
@@ -50,9 +49,12 @@
 
     /**
      * Gets the main class name for the given Java process by parsing the
-     * process command line.
+     * process command line. If the application was started with the <em>-jar</em>
+     * option this method returns the name of the jar file. If the application
+     * was started with <em>-m</em> or <em>--module</em> option, the method returns
+     * the module name and the main class name.
      * @param pid - process ID (pid)
-     * @return main class name or null if the process no longer exists or
+     * @return the main class name or null if the process no longer exists or
      * was started with a native launcher (e.g. jcmd etc)
      */
 
@@ -81,20 +83,16 @@
             }
         }
 
-        // If -jar option is used then read the main class name from the manifest file.
-        // Otherwise, the main class name is either specified in -m or --module options or it
-        // is the first part that is not a Java option (doesn't start with '-' and is not a
-        // classpath or a module path).
+        // To be consistent with the behavior on other platforms, if -jar, -m, or --module
+        // 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).
 
         for (int i = 1; i < parts.length && mainClass == null; i++) {
             if (i < parts.length - 1) {
-                // Check if the module is executed with explicitly specified main class
-                if ((parts[i].equals("-m") || parts[i].equals("--module"))) {
-                    return getMainClassFromModuleArg(parts[i + 1]);
-                }
-                // Check if the main class needs to be read from the manifest.mf in a JAR file
-                if (parts[i].equals("-jar")) {
-                    return getMainClassFromJar(parts[i + 1], pid);
+                if (parts[i].equals("-m") || parts[i].equals("--module") || parts[i].equals("-jar")) {
+                    return parts[i + 1];
                 }
             }
             // If this is a classpath or a module path option then skip the next part
@@ -114,34 +112,6 @@
 
     }
 
-    private String getMainClassFromModuleArg(String moduleArg) {
-        int pos = moduleArg.lastIndexOf("/");
-        return (pos > 0 && pos < moduleArg.length()-1) ? moduleArg.substring(pos + 1) : null;
-    }
-
-    private String getMainClassFromJar(String jar, String pid) {
-        if (!jar.startsWith("/")) {
-            String cwd = getCurrentWorkingDir(pid);
-            if (cwd != null) {
-                jar = cwd + "/" + jar;
-            }
-        }
-        try (JarFile jarFile = new JarFile(jar)) {
-            Manifest mf = jarFile.getManifest();
-            if (mf != null) {
-                Attributes mainAttributes = mf.getMainAttributes();
-                return mainAttributes.getValue("Main-Class");
-            }
-        } catch (IOException e) {
-            return null;
-        }
-        return null;
-    }
-
-    private static String getCurrentWorkingDir(String pid) {
-        return ("/proc/" + pid + "/cwd");
-    }
-
     private static String getCommandLine(String pid) {
         try (Stream<String> lines =
                      Files.lines(Paths.get("/proc/" + pid + "/cmdline"))) {
--- a/test/hotspot/jtreg/serviceability/dcmd/framework/HelpTest.java	Mon Apr 08 15:01:39 2019 +0100
+++ b/test/hotspot/jtreg/serviceability/dcmd/framework/HelpTest.java	Mon Apr 08 17:09:02 2019 +0000
@@ -75,6 +75,6 @@
         run(new JMXExecutor());
     }
 
-    private static class Process extends TestJavaProcess {
+    private static class Process extends process.TestJavaProcess {
     }
 }
--- a/test/hotspot/jtreg/serviceability/dcmd/framework/InvalidCommandTest.java	Mon Apr 08 15:01:39 2019 +0100
+++ b/test/hotspot/jtreg/serviceability/dcmd/framework/InvalidCommandTest.java	Mon Apr 08 17:09:02 2019 +0000
@@ -73,6 +73,6 @@
         run(new JMXExecutor());
     }
 
-    private static class Process extends TestJavaProcess {
+    private static class Process extends process.TestJavaProcess {
     }
 }
--- a/test/hotspot/jtreg/serviceability/dcmd/framework/TestJavaProcess.java	Mon Apr 08 15:01:39 2019 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,61 +0,0 @@
-/*
- * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-import nsk.share.jpda.*;
-import nsk.share.jdi.*;
-
-/**
- * A simple process that connects to a pipe and waits for command "quit" to
- * be received.
- *
- * Usage: java TestJavaProcess -pipe.port <PIPE_PORT_NUMBER>
- */
-
-public class TestJavaProcess {
-
-    static final int PASSED = 0;
-    static final int FAILED = 2;
-
-    public static void main(String argv[]) {
-
-        log("Test Java process started!");
-
-        ArgumentHandler argHandler = new ArgumentHandler(argv);
-        IOPipe pipe = argHandler.createDebugeeIOPipe();
-        pipe.println("ready");
-        log("Waiting for the quit command from the test ...");
-        String cmd = pipe.readln();
-        int exitCode = PASSED;
-        if (cmd.equals("quit")) {
-            log("'quit' received");
-        } else {
-            log("Invalid command received " + cmd);
-            exitCode = FAILED;
-        }
-        System.exit(exitCode);
-    }
-
-    private static void log(String message) {
-        System.out.println(message);
-    }
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/serviceability/dcmd/framework/TestProcessJarLauncher.java	Mon Apr 08 17:09:02 2019 +0000
@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import jdk.test.lib.util.JarUtils;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.jar.Attributes;
+import java.util.jar.Manifest;
+
+/**
+ * Launches a new Java process using -jar Java option.
+ */
+
+public class TestProcessJarLauncher extends TestProcessLauncher {
+
+    private static final String JAR_FILE = "testprocess.jar";
+
+
+    public TestProcessJarLauncher(String className) {
+        super(className);
+    }
+
+    protected String prepareLaunch(String javaExec, String pipePort) {
+        try {
+            File jarFile = prepareJar();
+            return javaExec + " -jar " + jarFile.getAbsolutePath() + " -pipe.port=" + pipePort;
+        } catch (IOException e) {
+            throw new RuntimeException("Failed to prepare a jar file", e);
+        }
+    }
+
+    private File prepareJar() throws IOException {
+        Path jarFile = USER_DIR.resolve(JAR_FILE);
+        Manifest manifest = createManifest();
+        Path testClass = TEST_CLASSES_DIR.resolve(className + ".class");
+        JarUtils.createJarFile(jarFile, manifest, TEST_CLASSES_DIR, Paths.get("."));
+        return jarFile.toFile();
+    }
+
+    private Manifest createManifest() {
+        Manifest manifest = new Manifest();
+        manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
+        manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, className);
+        return manifest;
+    }
+
+    public String getJarFile() {
+        return JAR_FILE;
+    }
+}
--- a/test/hotspot/jtreg/serviceability/dcmd/framework/TestProcessLauncher.java	Mon Apr 08 15:01:39 2019 +0100
+++ b/test/hotspot/jtreg/serviceability/dcmd/framework/TestProcessLauncher.java	Mon Apr 08 17:09:02 2019 +0000
@@ -26,6 +26,9 @@
 import nsk.share.jpda.*;
 import nsk.share.jdi.*;
 
+import java.nio.file.FileSystems;
+import java.nio.file.Path;
+
 /**
  * Launches a new Java process that uses a communication pipe to interact
  * with the test.
@@ -33,7 +36,10 @@
 
 public class TestProcessLauncher {
 
-    private final String className;
+    protected static final Path USER_DIR = FileSystems.getDefault().getPath(System.getProperty("user.dir", "."));
+    protected static final Path TEST_CLASSES_DIR = FileSystems.getDefault().getPath(System.getProperty("test.classes"));
+
+    protected final String className;
     private final ArgumentHandler argHandler;
 
     private IOPipe pipe;
@@ -55,7 +61,7 @@
         Binder binder = new Binder(argHandler, log);
         binder.prepareForPipeConnection(argHandler);
 
-        String cmd = java + " " + className + " -pipe.port=" + argHandler.getPipePort();
+        String cmd = prepareLaunch(java, argHandler.getPipePort());
 
         Debugee debuggee = binder.startLocalDebugee(cmd);
         debuggee.redirectOutput(log);
@@ -75,4 +81,8 @@
         }
     }
 
+    protected String prepareLaunch(String javaExec, String pipePort) {
+        return  javaExec + " " + className + " -pipe.port=" + pipePort;
+    }
+
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/serviceability/dcmd/framework/TestProcessModuleLauncher.java	Mon Apr 08 17:09:02 2019 +0000
@@ -0,0 +1,125 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import jdk.internal.module.ModuleInfoWriter;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.lang.module.ModuleDescriptor;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.jar.JarEntry;
+import java.util.jar.JarOutputStream;
+import java.util.stream.Stream;
+
+/*
+ * Launches a new Java process with a main class packed inside a module.
+ */
+
+public class TestProcessModuleLauncher extends TestProcessLauncher {
+
+    private static final Path TEST_MODULES = USER_DIR.resolve("testmodules");
+    private static final String MODULE_NAME = "module1";
+
+    public TestProcessModuleLauncher(String className) {
+        super(className);
+    }
+
+    protected String prepareLaunch(String javaExec, String pipePort) {
+        try {
+            prepareModule();
+            return javaExec + " --module-path " + TEST_MODULES.toFile().getAbsolutePath() +
+                    " -m " + MODULE_NAME + "/" + className + " -pipe.port=" + pipePort;
+        } catch (IOException e) {
+            throw new RuntimeException("Failed to prepare a jar file", e);
+        }
+    }
+
+    private void prepareModule() throws IOException {
+        TEST_MODULES.toFile().mkdirs();
+        Path moduleJar = TEST_MODULES.resolve("mod1.jar");
+        ModuleDescriptor md = createModuleDescriptor();
+        createModuleJarFile(moduleJar, md, TEST_CLASSES_DIR, Paths.get("."));
+    }
+
+    private ModuleDescriptor createModuleDescriptor() {
+        ModuleDescriptor.Builder builder
+                = ModuleDescriptor.newModule(MODULE_NAME).requires("java.base");
+        return builder.build();
+    }
+
+    private static void createModuleJarFile(Path jarfile, ModuleDescriptor md, Path dir, Path... files)
+            throws IOException {
+
+        Path parent = jarfile.getParent();
+        if (parent != null) {
+            Files.createDirectories(parent);
+        }
+
+        List<Path> entries = findAllRegularFiles(dir, files);
+
+        try (OutputStream out = Files.newOutputStream(jarfile);
+             JarOutputStream jos = new JarOutputStream(out)) {
+            if (md != null) {
+                JarEntry je = new JarEntry("module-info.class");
+                jos.putNextEntry(je);
+                ModuleInfoWriter.write(md, jos);
+                jos.closeEntry();
+            }
+
+            for (Path entry : entries) {
+                String name = toJarEntryName(entry);
+                jos.putNextEntry(new JarEntry(name));
+                Files.copy(dir.resolve(entry), jos);
+                jos.closeEntry();
+            }
+        }
+    }
+
+    private static String toJarEntryName(Path file) {
+        Path normalized = file.normalize();
+        return normalized.subpath(0, normalized.getNameCount())
+                .toString()
+                .replace(File.separatorChar, '/');
+    }
+
+    private static List<Path> findAllRegularFiles(Path dir, Path[] files) throws IOException {
+        List<Path> entries = new ArrayList<>();
+        for (Path file : files) {
+            try (Stream<Path> stream = Files.find(dir.resolve(file), Integer.MAX_VALUE,
+                    (p, attrs) -> attrs.isRegularFile() && !p.getParent().equals(dir.resolve(".")))) {
+                stream.map(dir::relativize)
+                        .forEach(entries::add);
+            }
+        }
+        return entries;
+    }
+
+    public String getModuleName() {
+        return MODULE_NAME;
+    }
+}
--- a/test/hotspot/jtreg/serviceability/dcmd/framework/VMVersionTest.java	Mon Apr 08 15:01:39 2019 +0100
+++ b/test/hotspot/jtreg/serviceability/dcmd/framework/VMVersionTest.java	Mon Apr 08 17:09:02 2019 +0000
@@ -33,17 +33,21 @@
 
 /*
  * @test
+ * @bug 8221730
  * @summary Test of diagnostic command VM.version (tests all DCMD executors)
  * @library /test/lib
  *          /vmTestbase
- * @build   TestJavaProcess
  * @modules java.base/jdk.internal.misc
+ *          java.base/jdk.internal.module
  *          java.compiler
  *          java.management
  *          jdk.internal.jvmstat/sun.jvmstat.monitor
  * @run testng/othervm -XX:+UsePerfData VMVersionTest
  */
 public class VMVersionTest {
+
+    private static final String TEST_PROCESS_CLASS_NAME = process.TestJavaProcess.class.getName();
+
     public void run(CommandExecutor executor) {
         OutputAnalyzer output = executor.execute("VM.version");
         output.shouldMatch(".*(?:HotSpot|OpenJDK).*VM.*");
@@ -56,10 +60,34 @@
 
     @Test
     public void mainClass() {
-        TestProcessLauncher t = new TestProcessLauncher(Process.class.getName());
+        TestProcessLauncher t = new TestProcessLauncher(TEST_PROCESS_CLASS_NAME);
+        try {
+            t.launch();
+            run(new MainClassJcmdExecutor(TEST_PROCESS_CLASS_NAME));
+        } finally {
+            t.quit();
+        }
+    }
+
+    @Test
+    public void mainClassForJar() {
+        TestProcessJarLauncher t = new TestProcessJarLauncher(TEST_PROCESS_CLASS_NAME);
         try {
             t.launch();
-            run(new MainClassJcmdExecutor(Process.class.getName()));
+            String jarFile = t.getJarFile();
+            run(new MainClassJcmdExecutor(jarFile));
+        } finally {
+            t.quit();
+        }
+    }
+
+    @Test
+    public void mainClassForModule() {
+        TestProcessModuleLauncher t = new TestProcessModuleLauncher(TEST_PROCESS_CLASS_NAME);
+        try {
+            t.launch();
+            String moduleName = t.getModuleName();
+            run(new MainClassJcmdExecutor(moduleName));
         } finally {
             t.quit();
         }
@@ -75,5 +103,4 @@
         run(new JMXExecutor());
     }
 
-    private static class Process extends TestJavaProcess{}
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/serviceability/dcmd/framework/process/TestJavaProcess.java	Mon Apr 08 17:09:02 2019 +0000
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package process;
+
+import nsk.share.jdi.ArgumentHandler;
+import nsk.share.jpda.IOPipe;
+
+/**
+ * A simple process that connects to a pipe and waits for command "quit" to
+ * be received.
+ *
+ * Usage: java TestJavaProcess -pipe.port <PIPE_PORT_NUMBER>
+ */
+
+public class TestJavaProcess {
+
+    static final int PASSED = 0;
+    static final int FAILED = 2;
+
+    public static void main(String argv[]) {
+
+        log("Test Java process started!");
+
+        ArgumentHandler argHandler = new ArgumentHandler(argv);
+        IOPipe pipe = argHandler.createDebugeeIOPipe();
+        pipe.println("ready");
+        log("Waiting for the quit command from the test ...");
+        String cmd = pipe.readln();
+        int exitCode = PASSED;
+        if (cmd.equals("quit")) {
+            log("'quit' received");
+        } else {
+            log("Invalid command received " + cmd);
+            exitCode = FAILED;
+        }
+        System.exit(exitCode);
+    }
+
+    private static void log(String message) {
+        System.out.println(message);
+    }
+}
--- a/test/jdk/sun/tools/jcmd/TestProcessHelper.java	Mon Apr 08 15:01:39 2019 +0100
+++ b/test/jdk/sun/tools/jcmd/TestProcessHelper.java	Mon Apr 08 17:09:02 2019 +0000
@@ -108,7 +108,7 @@
                     for (String a : arg) {
                         cmd.add(a);
                     }
-                    testProcessHelper(cmd);
+                    testProcessHelper(cmd, TEST_PROCESS_MAIN_CLASS);
                 }
             }
         }
@@ -130,7 +130,7 @@
                 for (String a : arg) {
                     cmd.add(a);
                 }
-                testProcessHelper(cmd);
+                testProcessHelper(cmd, jarFile.getAbsolutePath());
             }
         }
 
@@ -156,7 +156,7 @@
                         for (String a : arg) {
                             cmd.add(a);
                         }
-                        testProcessHelper(cmd);
+                        testProcessHelper(cmd, MODULE_NAME + "/" + TEST_PROCESS_MAIN_CLASS);
                     }
                 }
             }
@@ -171,7 +171,7 @@
         }
     }
 
-    private void testProcessHelper(List<String> args) throws Exception {
+    private void testProcessHelper(List<String> args, String expectedValue) throws Exception {
         ProcessBuilder pb = new ProcessBuilder(args);
         String cmd = pb.command().stream().collect(Collectors.joining(" "));
         System.out.println("Starting the process:" + cmd);
@@ -179,7 +179,7 @@
         if (!p.isAlive()) {
             throw new RuntimeException("Cannot start the process: " + cmd);
         }
-        checkMainClass(p, TEST_PROCESS_MAIN_CLASS);
+        checkMainClass(p, expectedValue);
     }
 
     private File prepareJar() throws Exception {