diff -r 5c08f2a9daee src/jdk.jcmd/share/classes/sun/tools/common/ProcessArgumentMatcher.java --- a/src/jdk.jcmd/share/classes/sun/tools/common/ProcessArgumentMatcher.java Thu Jan 19 10:55:39 2017 -0500 +++ b/src/jdk.jcmd/share/classes/sun/tools/common/ProcessArgumentMatcher.java Tue Mar 14 18:12:38 2017 -0700 @@ -25,7 +25,6 @@ package sun.tools.common; -import java.lang.reflect.Module; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Collection; @@ -46,10 +45,13 @@ * the process identifiers. */ public class ProcessArgumentMatcher { - private String matchClass; - private String singlePid; + private String excludeCls; + private String matchClass = null; + private String singlePid = null; + private boolean matchAll = false; - public ProcessArgumentMatcher(String pidArg) { + public ProcessArgumentMatcher(String pidArg, Class excludeClass) { + excludeCls = excludeClass.getName(); if (pidArg == null || pidArg.isEmpty()) { throw new IllegalArgumentException("Pid string is invalid"); } @@ -58,7 +60,9 @@ } try { long pid = Long.parseLong(pidArg); - if (pid != 0) { + if (pid == 0) { + matchAll = true; + } else { singlePid = String.valueOf(pid); } } catch (NumberFormatException nfe) { @@ -66,18 +70,7 @@ } } - private static String getExcludeStringFrom(Class excludeClass) { - if (excludeClass == null) { - return ""; - } - Module m = excludeClass.getModule(); - if (m.isNamed()) { - return m.getName() + "/" + excludeClass.getName(); - } - return excludeClass.getName(); - } - - private static boolean check(VirtualMachineDescriptor vmd, String excludeClass, String partialMatch) { + private boolean check(VirtualMachineDescriptor vmd) { String mainClass = null; try { VmIdentifier vmId = new VmIdentifier(vmd.id()); @@ -94,55 +87,42 @@ // Handle this gracefully.... return false; } catch (MonitorException | URISyntaxException e) { + if (e.getMessage() != null) { + System.err.println(e.getMessage()); + } else { + Throwable cause = e.getCause(); + if ((cause != null) && (cause.getMessage() != null)) { + System.err.println(cause.getMessage()); + } else { + e.printStackTrace(); + } + } return false; } - if (excludeClass != null && mainClass.equals(excludeClass)) { + if (mainClass.equals(excludeCls)) { return false; } - if (partialMatch != null && mainClass.indexOf(partialMatch) == -1) { - return false; + if (matchAll || mainClass.indexOf(matchClass) != -1) { + return true; } - return true; + return false; } - private static Collection getSingleVMD(String pid) { - Collection vids = new ArrayList<>(); + public Collection getPids() { + Collection pids = new ArrayList<>(); + if (singlePid != null) { + pids.add(singlePid); + return pids; + } List vmds = VirtualMachine.list(); for (VirtualMachineDescriptor vmd : vmds) { - if (check(vmd, null, null)) { - if (pid.equals(vmd.id())) { - vids.add(vmd); - } + if (check(vmd)) { + pids.add(vmd.id()); } } - return vids; + return pids; } - - private static Collection getVMDs(Class excludeClass, String partialMatch) { - String excludeCls = getExcludeStringFrom(excludeClass); - Collection vids = new ArrayList<>(); - List vmds = VirtualMachine.list(); - for (VirtualMachineDescriptor vmd : vmds) { - if (check(vmd, excludeCls, partialMatch)) { - vids.add(vmd); - } - } - return vids; - } - - public Collection getVirtualMachineDescriptors(Class excludeClass) { - if (singlePid != null) { - return getSingleVMD(singlePid); - } else { - return getVMDs(excludeClass, matchClass); - } - } - - public Collection getVirtualMachineDescriptors() { - return this.getVirtualMachineDescriptors(null); - } - } diff -r 5c08f2a9daee src/jdk.jcmd/share/classes/sun/tools/jcmd/Arguments.java --- a/src/jdk.jcmd/share/classes/sun/tools/jcmd/Arguments.java Thu Jan 19 10:55:39 2017 -0500 +++ b/src/jdk.jcmd/share/classes/sun/tools/jcmd/Arguments.java Tue Mar 14 18:12:38 2017 -0700 @@ -45,8 +45,6 @@ public Arguments(String[] args) { if (args.length == 0 || args[0].equals("-l")) { listProcesses = true; - /* list all processes */ - processString = "0"; return; } diff -r 5c08f2a9daee src/jdk.jcmd/share/classes/sun/tools/jcmd/JCmd.java --- a/src/jdk.jcmd/share/classes/sun/tools/jcmd/JCmd.java Thu Jan 19 10:55:39 2017 -0500 +++ b/src/jdk.jcmd/share/classes/sun/tools/jcmd/JCmd.java Tue Mar 14 18:12:38 2017 -0700 @@ -65,37 +65,36 @@ System.exit(1); } - ProcessArgumentMatcher ap = null; - try { - ap = new ProcessArgumentMatcher(arg.getProcessString()); - } catch (IllegalArgumentException iae) { - System.err.println("Invalid pid '" + arg.getProcessString() + "' specified"); - System.exit(1); - } - if (arg.isListProcesses()) { - for (VirtualMachineDescriptor vmd : ap.getVirtualMachineDescriptors(/* include jcmd in listing */)) { + List vmds = VirtualMachine.list(); + for (VirtualMachineDescriptor vmd : vmds) { System.out.println(vmd.id() + " " + vmd.displayName()); } System.exit(0); } - Collection vids = ap.getVirtualMachineDescriptors(JCmd.class); - - if (vids.isEmpty()) { + Collection pids = Collections.emptyList(); + try { + ProcessArgumentMatcher ap = new ProcessArgumentMatcher(arg.getProcessString(), JCmd.class); + pids = ap.getPids(); + } catch (IllegalArgumentException iae) { + System.err.println("Invalid pid specified"); + System.exit(1); + } + if (pids.isEmpty()) { System.err.println("Could not find any processes matching : '" + arg.getProcessString() + "'"); System.exit(1); } boolean success = true; - for (VirtualMachineDescriptor vid : vids) { - System.out.println(vid.id() + ":"); + for (String pid : pids) { + System.out.println(pid + ":"); if (arg.isListCounters()) { - listCounters(vid.id()); + listCounters(pid); } else { try { - executeCommandForPid(vid.id(), arg.getCommand()); + executeCommandForPid(pid, arg.getCommand()); } catch(AttachOperationFailedException ex) { System.err.println(ex.getMessage()); success = false; diff -r 5c08f2a9daee src/jdk.jcmd/share/classes/sun/tools/jinfo/JInfo.java --- a/src/jdk.jcmd/share/classes/sun/tools/jinfo/JInfo.java Thu Jan 19 10:55:39 2017 -0500 +++ b/src/jdk.jcmd/share/classes/sun/tools/jinfo/JInfo.java Tue Mar 14 18:12:38 2017 -0700 @@ -30,7 +30,6 @@ import java.util.Collection; import com.sun.tools.attach.VirtualMachine; -import com.sun.tools.attach.VirtualMachineDescriptor; import sun.tools.attach.HotSpotVirtualMachine; import sun.tools.common.ProcessArgumentMatcher; @@ -51,7 +50,6 @@ boolean doFlag = false; boolean doFlags = false; boolean doSysprops = false; - int flag = -1; // Parse the options (arguments starting with "-" ) int optionCount = 0; @@ -69,64 +67,65 @@ if (arg.equals("-flag")) { doFlag = true; - // Consume the flag - if (optionCount < args.length) { - flag = optionCount++; - break; - } - usage(1); + continue; } if (arg.equals("-flags")) { doFlags = true; - break; + continue; } if (arg.equals("-sysprops")) { doSysprops = true; - break; + continue; } } + // Next we check the parameter count. -flag allows extra parameters int paramCount = args.length - optionCount; - if (paramCount != 1) { + if ((doFlag && paramCount != 2) || ((!doFlag && paramCount != 1))) { usage(1); } - String parg = args[optionCount]; - - ProcessArgumentMatcher ap = new ProcessArgumentMatcher(parg); - Collection vids = ap.getVirtualMachineDescriptors(JInfo.class); - - if (vids.isEmpty()) { - System.err.println("Could not find any processes matching : '" + parg + "'"); - System.exit(1); + if (!doFlag && !doFlags && !doSysprops) { + // Print flags and sysporps if no options given + ProcessArgumentMatcher ap = new ProcessArgumentMatcher(args[optionCount], JInfo.class); + Collection pids = ap.getPids(); + for (String pid : pids) { + if (pids.size() > 1) { + System.out.println("Pid:" + pid); + } + sysprops(pid); + System.out.println(); + flags(pid); + System.out.println(); + commandLine(pid); + } } - for (VirtualMachineDescriptor vid : vids) { - if (vids.size() > 1) { - System.out.println("Pid:" + vid.id()); + if (doFlag) { + ProcessArgumentMatcher ap = new ProcessArgumentMatcher(args[optionCount+1], JInfo.class); + Collection pids = ap.getPids(); + for (String pid : pids) { + if (pids.size() > 1) { + System.out.println("Pid:" + pid); + } + flag(pid, args[optionCount]); } - if (!doFlag && !doFlags && !doSysprops) { - // Print flags and sysporps if no options given - sysprops(vid.id()); - System.out.println(); - flags(vid.id()); - System.out.println(); - commandLine(vid.id()); - } - if (doFlag) { - if (flag < 0) { - System.err.println("Missing flag"); - usage(1); + } + else if (doFlags || doSysprops) { + ProcessArgumentMatcher ap = new ProcessArgumentMatcher(args[optionCount], JInfo.class); + Collection pids = ap.getPids(); + for (String pid : pids) { + if (pids.size() > 1) { + System.out.println("Pid:" + pid); } - flag(vid.id(), args[flag]); - } - if (doFlags) { - flags(vid.id()); - } - if (doSysprops) { - sysprops(vid.id()); + if (doFlags) { + flags(pid); + } + else if (doSysprops) { + sysprops(pid); + } } } } diff -r 5c08f2a9daee src/jdk.jcmd/share/classes/sun/tools/jmap/JMap.java --- a/src/jdk.jcmd/share/classes/sun/tools/jmap/JMap.java Thu Jan 19 10:55:39 2017 -0500 +++ b/src/jdk.jcmd/share/classes/sun/tools/jmap/JMap.java Tue Mar 14 18:12:38 2017 -0700 @@ -32,7 +32,6 @@ import java.util.Collection; import com.sun.tools.attach.VirtualMachine; -import com.sun.tools.attach.VirtualMachineDescriptor; import com.sun.tools.attach.AttachNotSupportedException; import sun.tools.attach.HotSpotVirtualMachine; import sun.tools.common.ProcessArgumentMatcher; @@ -90,17 +89,10 @@ // Here we handle the built-in options // As more options are added we should create an abstract tool class and // have a table to map the options - ProcessArgumentMatcher ap = new ProcessArgumentMatcher(pidArg); - Collection vids = ap.getVirtualMachineDescriptors(JMap.class); - - if (vids.isEmpty()) { - System.err.println("Could not find any processes matching : '" + pidArg + "'"); - System.exit(1); - } - - for (VirtualMachineDescriptor vid : vids) { - String pid = vid.id(); - if (vids.size() > 1) { + ProcessArgumentMatcher ap = new ProcessArgumentMatcher(pidArg, JMap.class); + Collection pids = ap.getPids(); + for (String pid : pids) { + if (pids.size() > 1) { System.out.println("Pid:" + pid); } if (option.equals("-histo")) { diff -r 5c08f2a9daee src/jdk.jcmd/share/classes/sun/tools/jstack/JStack.java --- a/src/jdk.jcmd/share/classes/sun/tools/jstack/JStack.java Thu Jan 19 10:55:39 2017 -0500 +++ b/src/jdk.jcmd/share/classes/sun/tools/jstack/JStack.java Tue Mar 14 18:12:38 2017 -0700 @@ -29,7 +29,6 @@ import java.util.Collection; import com.sun.tools.attach.VirtualMachine; -import com.sun.tools.attach.VirtualMachineDescriptor; import sun.tools.attach.HotSpotVirtualMachine; import sun.tools.common.ProcessArgumentMatcher; @@ -83,19 +82,13 @@ } else { params = new String[0]; } - ProcessArgumentMatcher ap = new ProcessArgumentMatcher(pidArg); - Collection vids = ap.getVirtualMachineDescriptors(JStack.class); - - if (vids.isEmpty()) { - System.err.println("Could not find any processes matching : '" + pidArg + "'"); - System.exit(1); - } - - for (VirtualMachineDescriptor vid : vids) { - if (vids.size() > 1) { - System.out.println("Pid:" + vid.id()); + ProcessArgumentMatcher ap = new ProcessArgumentMatcher(pidArg, JStack.class); + Collection pids = ap.getPids(); + for (String pid : pids) { + if (pids.size() > 1) { + System.out.println("Pid:" + pid); } - runThreadDump(vid.id(), params); + runThreadDump(pid, params); } }