changeset 54840:67537bbafd7f

8205654: serviceability/dcmd/framework/HelpTest.java timed out Reviewed-by: sspitsyn, dholmes
author dtitov
date Fri, 08 Feb 2019 09:41:45 -0800
parents b5b373bda814
children 0e61020e9d00 58e292dd4be0 3b9b8721b9f9 f05fea82d612 0e2c8f1ee3b5 1505d37d0444 1708dcd7b9a3 d35bbe6a60f4 0aae8f210ec1 9acdc1ff686a c34acb3a3330
files src/jdk.jcmd/linux/classes/sun/tools/ProcessHelper.java src/jdk.jcmd/share/classes/sun/tools/common/ProcessArgumentMatcher.java src/jdk.jcmd/share/classes/sun/tools/common/ProcessHelper.java test/hotspot/jtreg/serviceability/dcmd/framework/HelpTest.java test/hotspot/jtreg/serviceability/dcmd/framework/InvalidCommandTest.java test/hotspot/jtreg/serviceability/dcmd/framework/TEST.properties test/hotspot/jtreg/serviceability/dcmd/framework/TestJavaProcess.java test/hotspot/jtreg/serviceability/dcmd/framework/TestProcessLauncher.java test/hotspot/jtreg/serviceability/dcmd/framework/VMVersionTest.java test/hotspot/jtreg/vmTestbase/nsk/share/jpda/DebugeeBinder.java test/jdk/sun/tools/jcmd/TestProcess.java test/jdk/sun/tools/jcmd/TestProcessHelper.java
diffstat 12 files changed, 721 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/jdk.jcmd/linux/classes/sun/tools/ProcessHelper.java	Fri Feb 08 09:41:45 2019 -0800
@@ -0,0 +1,155 @@
+/*
+ * 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.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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 sun.tools;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.nio.file.Files;
+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;
+
+/**
+ * A helper class that retrieves the main class name for
+ * a running Java process using the proc filesystem (procfs)
+ */
+public class ProcessHelper implements sun.tools.common.ProcessHelper {
+
+
+    private static final String CMD_PREFIX = "cmd:";
+    private static final ProcessHelper INSTANCE = new ProcessHelper();
+
+    public static ProcessHelper getInstance() {
+        return INSTANCE;
+    }
+
+    /**
+     * Gets the main class name for the given Java process by parsing the
+     * process command line.
+     * @param pid - process ID (pid)
+     * @return main class name or null if the process no longer exists or
+     * was started with a native launcher (e.g. jcmd etc)
+     */
+
+    public String getMainClass(String pid) {
+        String cmdLine = getCommandLine(pid);
+        if (cmdLine == null) {
+            return null;
+        }
+        if (cmdLine.startsWith(CMD_PREFIX)) {
+            cmdLine = cmdLine.substring(CMD_PREFIX.length());
+        }
+        String[] parts = cmdLine.split(" ");
+        String mainClass = null;
+
+        if(parts.length == 0) {
+            return null;
+        }
+
+        // Check the executable
+        String[] executablePath = parts[0].split("/");
+        if (executablePath.length > 0) {
+            String binaryName = executablePath[executablePath.length - 1];
+            if (!"java".equals(binaryName)) {
+                // Skip the process if it is not started with java launcher
+                return null;
+            }
+        }
+
+        // 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).
+
+        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 this is a classpath or a module path option then skip the next part
+            // (the classpath or the module path itself)
+            if (parts[i].equals("-cp") || parts[i].equals("-classpath") ||  parts[i].equals("--class-path") ||
+                    parts[i].equals("-p") || parts[i].equals("--module-path")) {
+                i++;
+                continue;
+            }
+            // Skip all other Java options
+            if (parts[i].startsWith("-")) {
+                continue;
+            }
+            mainClass = parts[i];
+        }
+        return mainClass;
+
+    }
+
+    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"))) {
+            return lines.map(x -> x.replaceAll("\0", " ")).findFirst().orElse(null);
+        } catch (IOException | UncheckedIOException e) {
+            return null;
+        }
+    }
+}
+
+
--- a/src/jdk.jcmd/share/classes/sun/tools/common/ProcessArgumentMatcher.java	Thu Feb 07 09:55:57 2019 +0100
+++ b/src/jdk.jcmd/share/classes/sun/tools/common/ProcessArgumentMatcher.java	Fri Feb 08 09:41:45 2019 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 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
@@ -78,25 +78,40 @@
     }
 
     private static boolean check(VirtualMachineDescriptor vmd, String excludeClass, String partialMatch) {
+
         String mainClass = null;
-        try {
-            VmIdentifier vmId = new VmIdentifier(vmd.id());
-            MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(vmId);
-            MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, -1);
-            mainClass = MonitoredVmUtil.mainClass(monitoredVm, true);
-            monitoredHost.detach(monitoredVm);
-        } catch (NullPointerException npe) {
-            // There is a potential race, where a running java app is being
-            // queried, unfortunately the java app has shutdown after this
-            // method is started but before getMonitoredVM is called.
-            // If this is the case, then the /tmp/hsperfdata_xxx/pid file
-            // will have disappeared and we will get a NullPointerException.
-            // Handle this gracefully....
-            return false;
-        } catch (MonitorException | URISyntaxException e) {
-            return false;
+
+        // Get the main class name using platform specific helper
+        ProcessHelper helper = ProcessHelper.platformProcessHelper();
+        if (helper != null) {
+            mainClass = helper.getMainClass(vmd.id());
+            if (mainClass == null) {
+                return false;
+            }
         }
 
+        // If the main class name is still unset then retrieve it with the attach mechanism
+        if (mainClass == null) {
+            try {
+                VmIdentifier vmId = new VmIdentifier(vmd.id());
+                MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(vmId);
+                MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, -1);
+                mainClass = MonitoredVmUtil.mainClass(monitoredVm, true);
+                monitoredHost.detach(monitoredVm);
+            } catch (NullPointerException npe) {
+                // There is a potential race, where a running java app is being
+                // queried, unfortunately the java app has shutdown after this
+                // method is started but before getMonitoredVM is called.
+                // If this is the case, then the /tmp/hsperfdata_xxx/pid file
+                // will have disappeared and we will get a NullPointerException.
+                // Handle this gracefully....
+                return false;
+            } catch (MonitorException | URISyntaxException e) {
+                return false;
+            }
+        }
+
+
         if (excludeClass != null && mainClass.equals(excludeClass)) {
             return false;
         }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/jdk.jcmd/share/classes/sun/tools/common/ProcessHelper.java	Fri Feb 08 09:41:45 2019 -0800
@@ -0,0 +1,64 @@
+/*
+ * 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.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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 sun.tools.common;
+
+import java.lang.reflect.Method;
+
+/**
+ * A helper class to retrieve the main class name for a running
+ * Java process.
+ */
+
+public interface ProcessHelper {
+
+    /**
+     * Returns an instance of the ProcessHelper class.
+     *
+     * @return ProcessHelper object or null if not supported on this platform.
+     */
+    public static ProcessHelper platformProcessHelper() {
+        try {
+            Class<?> c = Class.forName("sun.tools.ProcessHelper");
+            @SuppressWarnings("unchecked")
+            Method m = c.getMethod("getInstance");
+            return (ProcessHelper) m.invoke(null);
+        } catch (ClassNotFoundException e) {
+            return null;
+        } catch (ReflectiveOperationException e) {
+            throw new InternalError(e);
+        }
+    }
+
+
+    /**
+     * Returns the main class name for the given Java process
+     *
+     * @param pid - process ID (pid)
+     * @return main class name or null if the main class could not be retrieved
+     */
+
+    String getMainClass(String pid);
+}
--- a/test/hotspot/jtreg/serviceability/dcmd/framework/HelpTest.java	Thu Feb 07 09:55:57 2019 +0100
+++ b/test/hotspot/jtreg/serviceability/dcmd/framework/HelpTest.java	Fri Feb 08 09:41:45 2019 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 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
@@ -33,6 +33,7 @@
  * @test
  * @summary Test of diagnostic command help (tests all DCMD executors)
  * @library /test/lib
+ *          /vmTestbase
  * @modules java.base/jdk.internal.misc
  *          java.compiler
  *          java.management
@@ -55,7 +56,13 @@
 
     @Test
     public void mainClass() {
-        run(new MainClassJcmdExecutor());
+        TestProcessLauncher t = new TestProcessLauncher(Process.class.getName());
+        try {
+            t.launch();
+            run(new MainClassJcmdExecutor(Process.class.getName()));
+        } finally {
+            t.quit();
+        }
     }
 
     @Test
@@ -68,4 +75,6 @@
         run(new JMXExecutor());
     }
 
+    private static class Process extends TestJavaProcess {
+    }
 }
--- a/test/hotspot/jtreg/serviceability/dcmd/framework/InvalidCommandTest.java	Thu Feb 07 09:55:57 2019 +0100
+++ b/test/hotspot/jtreg/serviceability/dcmd/framework/InvalidCommandTest.java	Fri Feb 08 09:41:45 2019 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 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
@@ -33,6 +33,7 @@
  * @test
  * @summary Test of invalid diagnostic command (tests all DCMD executors)
  * @library /test/lib
+ *          /vmTestbase
  * @modules java.base/jdk.internal.misc
  *          java.compiler
  *          java.management
@@ -53,7 +54,13 @@
 
     @Test
     public void mainClass() {
-        run(new MainClassJcmdExecutor());
+        TestProcessLauncher t = new TestProcessLauncher(Process.class.getName());
+        try {
+            t.launch();
+            run(new MainClassJcmdExecutor(Process.class.getName()));
+        } finally {
+            t.quit();
+        }
     }
 
     @Test
@@ -65,4 +72,7 @@
     public void jmx() {
         run(new JMXExecutor());
     }
+
+    private static class Process extends TestJavaProcess {
+    }
 }
--- a/test/hotspot/jtreg/serviceability/dcmd/framework/TEST.properties	Thu Feb 07 09:55:57 2019 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,2 +0,0 @@
-exclusiveAccess.dirs=.
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/serviceability/dcmd/framework/TestJavaProcess.java	Fri Feb 08 09:41:45 2019 -0800
@@ -0,0 +1,61 @@
+/*
+ * 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/TestProcessLauncher.java	Fri Feb 08 09:41:45 2019 -0800
@@ -0,0 +1,78 @@
+
+/*
+ * 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.*;
+import nsk.share.jpda.*;
+import nsk.share.jdi.*;
+
+/**
+ * Launches a new Java process that uses a communication pipe to interact
+ * with the test.
+ */
+
+public class TestProcessLauncher {
+
+    private final String className;
+    private final ArgumentHandler argHandler;
+
+    private IOPipe pipe;
+
+    public TestProcessLauncher(String className, ArgumentHandler argHandler) {
+        this.className = className;
+        this.argHandler = argHandler;
+    }
+
+    public TestProcessLauncher(String className) {
+        this(className, new ArgumentHandler(new String[0]));
+    }
+
+    public Process launch() {
+
+        String java = argHandler.getLaunchExecPath();
+
+        Log log = new Log(System.out, argHandler);
+        Binder binder = new Binder(argHandler, log);
+        binder.prepareForPipeConnection(argHandler);
+
+        String cmd = java + " " + className + " -pipe.port=" + argHandler.getPipePort();
+
+        Debugee debuggee = binder.startLocalDebugee(cmd);
+        debuggee.redirectOutput(log);
+
+        pipe = new IOPipe(debuggee);
+
+        String line = pipe.readln();
+        if (!"ready".equals(line)) {
+            System.out.println("Wrong reply received:" + line);
+        }
+        return debuggee.getProcess();
+    }
+
+    public void quit() {
+        if (pipe != null) {
+            pipe.println("quit");
+        }
+    }
+
+}
--- a/test/hotspot/jtreg/serviceability/dcmd/framework/VMVersionTest.java	Thu Feb 07 09:55:57 2019 +0100
+++ b/test/hotspot/jtreg/serviceability/dcmd/framework/VMVersionTest.java	Fri Feb 08 09:41:45 2019 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 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
@@ -27,6 +27,7 @@
 import jdk.test.lib.dcmd.MainClassJcmdExecutor;
 import jdk.test.lib.dcmd.FileJcmdExecutor;
 import jdk.test.lib.dcmd.JMXExecutor;
+import nsk.share.jdi.ArgumentHandler;
 
 import org.testng.annotations.Test;
 
@@ -34,6 +35,8 @@
  * @test
  * @summary Test of diagnostic command VM.version (tests all DCMD executors)
  * @library /test/lib
+ *          /vmTestbase
+ * @build   TestJavaProcess
  * @modules java.base/jdk.internal.misc
  *          java.compiler
  *          java.management
@@ -53,7 +56,13 @@
 
     @Test
     public void mainClass() {
-        run(new MainClassJcmdExecutor());
+        TestProcessLauncher t = new TestProcessLauncher(Process.class.getName());
+        try {
+            t.launch();
+            run(new MainClassJcmdExecutor(Process.class.getName()));
+        } finally {
+            t.quit();
+        }
     }
 
     @Test
@@ -65,4 +74,6 @@
     public void jmx() {
         run(new JMXExecutor());
     }
+
+    private static class Process extends TestJavaProcess{}
 }
--- a/test/hotspot/jtreg/vmTestbase/nsk/share/jpda/DebugeeBinder.java	Thu Feb 07 09:55:57 2019 +0100
+++ b/test/hotspot/jtreg/vmTestbase/nsk/share/jpda/DebugeeBinder.java	Fri Feb 08 09:41:45 2019 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 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
@@ -175,7 +175,7 @@
      * Make preperation for IOPipe connection before starting debugee VM process.
      * May change options in the passed <code>argumentHandler</code>.
      */
-    protected void prepareForPipeConnection(DebugeeArgumentHandler argumentHandler) {
+    public void prepareForPipeConnection(DebugeeArgumentHandler argumentHandler) {
         if (argumentHandler.isTransportAddressDynamic()) {
             try {
                 pipeServerSocket = new ServerSocket();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/sun/tools/jcmd/TestProcess.java	Fri Feb 08 09:41:45 2019 -0800
@@ -0,0 +1,34 @@
+/*
+ * 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 test;
+
+public class TestProcess {
+
+    public static void main(String[] args) throws Exception {
+       System.out.print("The process started, pid:" + ProcessHandle.current().pid());
+        while(true) {
+            Thread.sleep(100);
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/sun/tools/jcmd/TestProcessHelper.java	Fri Feb 08 09:41:45 2019 -0800
@@ -0,0 +1,259 @@
+/*
+ * 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 jdk.test.lib.JDKToolFinder;
+import jdk.test.lib.process.ProcessTools;
+import jdk.test.lib.util.JarUtils;
+import sun.tools.common.ProcessHelper;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.lang.module.ModuleDescriptor;
+import java.nio.file.FileSystems;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.jar.Attributes;
+import java.util.jar.JarEntry;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Manifest;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+/*
+ * @test
+ * @bug 8205654
+ * @summary Unit test for sun.tools.ProcessHelper class. The test launches Java processes with different Java options
+ * and checks that sun.tools.ProcessHelper.getMainClass(pid) method returns a correct main class.                                                                                                                               return a .
+ *
+ * @requires os.family == "linux"
+ * @library /test/lib
+ * @modules jdk.jcmd/sun.tools.common
+ *          java.base/jdk.internal.module
+ * @build test.TestProcess
+ * @run main/othervm TestProcessHelper
+ */
+public class TestProcessHelper {
+
+    private ProcessHelper PROCESS_HELPER = ProcessHelper.platformProcessHelper();
+
+    private static final String TEST_PROCESS_MAIN_CLASS_NAME = "TestProcess";
+    private static final String TEST_PROCESS_MAIN_CLASS_PACKAGE = "test";
+    private static final String TEST_PROCESS_MAIN_CLASS = TEST_PROCESS_MAIN_CLASS_PACKAGE + "."
+            + TEST_PROCESS_MAIN_CLASS_NAME;
+    private static final Path TEST_CLASSES = FileSystems.getDefault().getPath(System.getProperty("test.classes"));
+    private static final Path USER_DIR = FileSystems.getDefault().getPath(System.getProperty("user.dir", "."));
+    private static final Path TEST_MODULES = USER_DIR.resolve("testmodules");
+    private static final String JAVA_PATH = JDKToolFinder.getJDKTool("java");
+    private static final Path TEST_CLASS = TEST_CLASSES.resolve(TEST_PROCESS_MAIN_CLASS_PACKAGE)
+            .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[][] 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 JAR_OPTION = "-jar";
+    private static final String MODULE_NAME = "module1";
+
+
+    public static void main(String[] args) throws Exception {
+        new TestProcessHelper().runTests();
+    }
+
+    public void runTests() throws Exception {
+        testClassPath();
+        testJar();
+        testModule();
+    }
+
+    // Test Java processes that are started with -classpath, -cp, or --class-path options
+    // and with different combinations of VM and program args.
+    private void testClassPath() throws Exception {
+        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);
+                    }
+                    cmd.add(TEST_PROCESS_MAIN_CLASS);
+                    for (String a : arg) {
+                        cmd.add(a);
+                    }
+                    testProcessHelper(cmd);
+                }
+            }
+        }
+    }
+
+    // Test Java processes that are started with -jar option
+    // and with different combinations of VM and program args.
+    private void testJar() throws Exception {
+        File jarFile = prepareJar();
+        for (String[] vma : VM_ARGS) {
+            for (String[] arg : ARGS) {
+                List<String> cmd = new LinkedList<>();
+                cmd.add(JAVA_PATH);
+                for (String v : vma) {
+                    cmd.add(v);
+                }
+                cmd.add(JAR_OPTION);
+                cmd.add(jarFile.getAbsolutePath());
+                for (String a : arg) {
+                    cmd.add(a);
+                }
+                testProcessHelper(cmd);
+            }
+        }
+
+    }
+
+    // Test Java processes that are started with -m or --module options
+    // and with different combination of VM and program args.
+    private void testModule() throws Exception {
+        prepareModule();
+        for (String mp : MP_OPTIONS) {
+            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);
+                        }
+                        cmd.add(m);
+                        cmd.add(MODULE_NAME + "/" + TEST_PROCESS_MAIN_CLASS);
+                        for (String a : arg) {
+                            cmd.add(a);
+                        }
+                        testProcessHelper(cmd);
+                    }
+                }
+            }
+        }
+    }
+
+    private void checkMainClass(Process p, String expectedMainClass) {
+        String mainClass = PROCESS_HELPER.getMainClass(Long.toString(p.pid()));
+        p.destroyForcibly();
+        if (!expectedMainClass.equals(mainClass)) {
+            throw new RuntimeException("Main class is wrong: " + mainClass);
+        }
+    }
+
+    private void testProcessHelper(List<String> args) throws Exception {
+        ProcessBuilder pb = new ProcessBuilder(args);
+        String cmd = pb.command().stream().collect(Collectors.joining(" "));
+        System.out.println("Starting the process:" + cmd);
+        Process p = ProcessTools.startProcess("test", pb);
+        if (!p.isAlive()) {
+            throw new RuntimeException("Cannot start the process: " + cmd);
+        }
+        checkMainClass(p, TEST_PROCESS_MAIN_CLASS);
+    }
+
+    private File prepareJar() throws Exception {
+        Path jarFile = USER_DIR.resolve("testprocess.jar");
+        Manifest manifest = createManifest();
+        JarUtils.createJarFile(jarFile, manifest, TEST_CLASSES, TEST_CLASS);
+        return jarFile.toFile();
+    }
+
+    private void prepareModule() throws Exception {
+        TEST_MODULES.toFile().mkdirs();
+        Path moduleJar = TEST_MODULES.resolve("mod1.jar");
+        ModuleDescriptor md = createModuleDescriptor();
+        createModuleJarFile(moduleJar, md, TEST_CLASSES, TEST_CLASS);
+    }
+
+    private Manifest createManifest() {
+        Manifest manifest = new Manifest();
+        manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
+        manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, TEST_PROCESS_MAIN_CLASS);
+        return manifest;
+    }
+
+    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())) {
+                stream.map(dir::relativize)
+                        .forEach(entries::add);
+            }
+        }
+        return entries;
+    }
+
+}