src/jdk.jcmd/share/classes/sun/tools/common/ProcessArgumentMatcher.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 44545 jdk/src/jdk.jcmd/share/classes/sun/tools/common/ProcessArgumentMatcher.java@83b611b88ac8
child 53707 67537bbafd7f
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
     1
/*
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
     4
 *
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    10
 *
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    15
 * accompanied this code).
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    16
 *
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    20
 *
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    23
 * questions.
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    24
 */
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    25
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    26
package sun.tools.common;
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    27
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    28
import java.net.URISyntaxException;
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    29
import java.util.ArrayList;
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    30
import java.util.Collection;
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    31
import java.util.List;
44422
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
    32
import java.util.stream.Collectors;
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    33
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    34
import com.sun.tools.attach.VirtualMachine;
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    35
import com.sun.tools.attach.VirtualMachineDescriptor;
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    36
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    37
import sun.jvmstat.monitor.MonitorException;
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    38
import sun.jvmstat.monitor.MonitoredHost;
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    39
import sun.jvmstat.monitor.MonitoredVm;
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    40
import sun.jvmstat.monitor.MonitoredVmUtil;
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    41
import sun.jvmstat.monitor.VmIdentifier;
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    42
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    43
/**
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    44
 * Class for finding process matching a process argument,
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    45
 * excluding tool it self and returning a list containing
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    46
 * the process identifiers.
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    47
 */
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    48
public class ProcessArgumentMatcher {
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    49
    private String matchClass;
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    50
    private String singlePid;
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    51
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    52
    public ProcessArgumentMatcher(String pidArg) {
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    53
        if (pidArg == null || pidArg.isEmpty()) {
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    54
            throw new IllegalArgumentException("Pid string is invalid");
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    55
        }
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    56
        if (pidArg.charAt(0) == '-') {
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    57
            throw new IllegalArgumentException("Unrecognized " + pidArg);
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    58
        }
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    59
        try {
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    60
            long pid = Long.parseLong(pidArg);
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    61
            if (pid != 0) {
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    62
                singlePid = String.valueOf(pid);
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    63
            }
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    64
        } catch (NumberFormatException nfe) {
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    65
            matchClass = pidArg;
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    66
        }
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    67
    }
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    68
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    69
    private static String getExcludeStringFrom(Class<?> excludeClass) {
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    70
        if (excludeClass == null) {
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    71
            return "";
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    72
        }
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    73
        Module m = excludeClass.getModule();
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    74
        if (m.isNamed()) {
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    75
            return m.getName() + "/" + excludeClass.getName();
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    76
        }
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    77
        return excludeClass.getName();
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    78
    }
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    79
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    80
    private static boolean check(VirtualMachineDescriptor vmd, String excludeClass, String partialMatch) {
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    81
        String mainClass = null;
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    82
        try {
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    83
            VmIdentifier vmId = new VmIdentifier(vmd.id());
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    84
            MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(vmId);
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    85
            MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, -1);
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    86
            mainClass = MonitoredVmUtil.mainClass(monitoredVm, true);
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    87
            monitoredHost.detach(monitoredVm);
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    88
        } catch (NullPointerException npe) {
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    89
            // There is a potential race, where a running java app is being
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    90
            // queried, unfortunately the java app has shutdown after this
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    91
            // method is started but before getMonitoredVM is called.
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    92
            // If this is the case, then the /tmp/hsperfdata_xxx/pid file
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    93
            // will have disappeared and we will get a NullPointerException.
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    94
            // Handle this gracefully....
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    95
            return false;
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    96
        } catch (MonitorException | URISyntaxException e) {
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    97
            return false;
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    98
        }
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
    99
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   100
        if (excludeClass != null && mainClass.equals(excludeClass)) {
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   101
            return false;
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   102
        }
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   103
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   104
        if (partialMatch != null && mainClass.indexOf(partialMatch) == -1) {
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
   105
            return false;
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
   106
        }
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
   107
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   108
        return true;
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   109
    }
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   110
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   111
    private static Collection<VirtualMachineDescriptor> getSingleVMD(String pid) {
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   112
        Collection<VirtualMachineDescriptor> vids = new ArrayList<>();
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   113
        List<VirtualMachineDescriptor> vmds = VirtualMachine.list();
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   114
        for (VirtualMachineDescriptor vmd : vmds) {
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   115
            if (check(vmd, null, null)) {
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   116
                if (pid.equals(vmd.id())) {
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   117
                    vids.add(vmd);
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   118
                }
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   119
            }
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
   120
        }
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   121
        return vids;
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
   122
    }
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
   123
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   124
    private static Collection<VirtualMachineDescriptor> getVMDs(Class<?> excludeClass, String partialMatch) {
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   125
        String excludeCls = getExcludeStringFrom(excludeClass);
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   126
        Collection<VirtualMachineDescriptor> vids = new ArrayList<>();
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
   127
        List<VirtualMachineDescriptor> vmds = VirtualMachine.list();
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
   128
        for (VirtualMachineDescriptor vmd : vmds) {
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   129
            if (check(vmd, excludeCls, partialMatch)) {
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   130
                vids.add(vmd);
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
   131
            }
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
   132
        }
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   133
        return vids;
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
   134
    }
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   135
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   136
    public Collection<VirtualMachineDescriptor> getVirtualMachineDescriptors(Class<?> excludeClass) {
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   137
        if (singlePid != null) {
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   138
            return getSingleVMD(singlePid);
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   139
        } else {
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   140
            return getVMDs(excludeClass, matchClass);
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   141
        }
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   142
    }
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   143
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   144
    public Collection<VirtualMachineDescriptor> getVirtualMachineDescriptors() {
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   145
        return this.getVirtualMachineDescriptors(null);
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   146
    }
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   147
44422
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   148
    public Collection<String> getVirtualMachinePids(Class<?> excludeClass) {
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   149
        if (singlePid != null) {
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   150
            // There is a bug in AttachProvider, when VM is debuggee-suspended it's not listed by the AttachProvider.
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   151
            // If we are talking about a specific pid, just return it.
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   152
            return List.of(singlePid);
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   153
        } else {
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   154
            return getVMDs(excludeClass, matchClass).stream().map(x -> {return x.id();}).collect(Collectors.toList());
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   155
        }
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   156
    }
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   157
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   158
    public Collection<String> getVirtualMachinePids() {
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   159
        return this.getVirtualMachinePids(null);
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   160
    }
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
   161
}