8221730: jcmd process name matching broken
Reviewed-by: jcbeyler, dholmes, cjplummer
This commit is contained in:
parent
3ff0b6ea5e
commit
8dfa6d1acc
@ -31,7 +31,6 @@ 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;
|
||||
|
||||
/**
|
||||
@ -50,9 +49,12 @@ public class ProcessHelper implements sun.tools.common.ProcessHelper {
|
||||
|
||||
/**
|
||||
* 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 @@ public class ProcessHelper implements sun.tools.common.ProcessHelper {
|
||||
}
|
||||
}
|
||||
|
||||
// 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 @@ public class ProcessHelper implements sun.tools.common.ProcessHelper {
|
||||
|
||||
}
|
||||
|
||||
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"))) {
|
||||
|
@ -75,6 +75,6 @@ public class HelpTest {
|
||||
run(new JMXExecutor());
|
||||
}
|
||||
|
||||
private static class Process extends TestJavaProcess {
|
||||
private static class Process extends process.TestJavaProcess {
|
||||
}
|
||||
}
|
||||
|
@ -73,6 +73,6 @@ public class InvalidCommandTest {
|
||||
run(new JMXExecutor());
|
||||
}
|
||||
|
||||
private static class Process extends TestJavaProcess {
|
||||
private static class Process extends process.TestJavaProcess {
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -26,6 +26,9 @@ import nsk.share.*;
|
||||
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 @@ import nsk.share.jdi.*;
|
||||
|
||||
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 @@ public class TestProcessLauncher {
|
||||
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 @@ public class TestProcessLauncher {
|
||||
}
|
||||
}
|
||||
|
||||
protected String prepareLaunch(String javaExec, String pipePort) {
|
||||
return javaExec + " " + className + " -pipe.port=" + pipePort;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -33,17 +33,21 @@ import org.testng.annotations.Test;
|
||||
|
||||
/*
|
||||
* @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 @@ public class VMVersionTest {
|
||||
|
||||
@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(Process.class.getName()));
|
||||
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();
|
||||
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 @@ public class VMVersionTest {
|
||||
run(new JMXExecutor());
|
||||
}
|
||||
|
||||
private static class Process extends TestJavaProcess{}
|
||||
}
|
||||
|
@ -21,8 +21,10 @@
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import nsk.share.jpda.*;
|
||||
import nsk.share.jdi.*;
|
||||
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
|
@ -108,7 +108,7 @@ public class TestProcessHelper {
|
||||
for (String a : arg) {
|
||||
cmd.add(a);
|
||||
}
|
||||
testProcessHelper(cmd);
|
||||
testProcessHelper(cmd, TEST_PROCESS_MAIN_CLASS);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -130,7 +130,7 @@ public class TestProcessHelper {
|
||||
for (String a : arg) {
|
||||
cmd.add(a);
|
||||
}
|
||||
testProcessHelper(cmd);
|
||||
testProcessHelper(cmd, jarFile.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
|
||||
@ -156,7 +156,7 @@ public class TestProcessHelper {
|
||||
for (String a : arg) {
|
||||
cmd.add(a);
|
||||
}
|
||||
testProcessHelper(cmd);
|
||||
testProcessHelper(cmd, MODULE_NAME + "/" + TEST_PROCESS_MAIN_CLASS);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -171,7 +171,7 @@ public class TestProcessHelper {
|
||||
}
|
||||
}
|
||||
|
||||
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 @@ public class TestProcessHelper {
|
||||
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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user