src/jdk.jcmd/share/classes/sun/tools/common/ProcessArgumentMatcher.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58506 dfe3d11378cc
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
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
/*
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents: 47216
diff changeset
     2
 * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
38361
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) {
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents: 47216
diff changeset
    81
58265
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 53713
diff changeset
    82
        // Try to get the main class name using (platform specific) ProcessHelper
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 53713
diff changeset
    83
        String mainClass = ProcessHelper.getMainClass(vmd.id());
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents: 47216
diff changeset
    84
58265
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 53713
diff changeset
    85
        // If the main class name could not be retrieved by ProcessHelper, get it with the attach mechanism
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents: 47216
diff changeset
    86
        if (mainClass == null) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents: 47216
diff changeset
    87
            try {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents: 47216
diff changeset
    88
                VmIdentifier vmId = new VmIdentifier(vmd.id());
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents: 47216
diff changeset
    89
                MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(vmId);
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents: 47216
diff changeset
    90
                MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, -1);
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents: 47216
diff changeset
    91
                mainClass = MonitoredVmUtil.mainClass(monitoredVm, true);
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents: 47216
diff changeset
    92
                monitoredHost.detach(monitoredVm);
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents: 47216
diff changeset
    93
            } catch (NullPointerException npe) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents: 47216
diff changeset
    94
                // There is a potential race, where a running java app is being
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents: 47216
diff changeset
    95
                // queried, unfortunately the java app has shutdown after this
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents: 47216
diff changeset
    96
                // method is started but before getMonitoredVM is called.
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents: 47216
diff changeset
    97
                // If this is the case, then the /tmp/hsperfdata_xxx/pid file
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents: 47216
diff changeset
    98
                // will have disappeared and we will get a NullPointerException.
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents: 47216
diff changeset
    99
                // Handle this gracefully....
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents: 47216
diff changeset
   100
                return false;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents: 47216
diff changeset
   101
            } catch (MonitorException | URISyntaxException e) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents: 47216
diff changeset
   102
                return false;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents: 47216
diff changeset
   103
            }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents: 47216
diff changeset
   104
        }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents: 47216
diff changeset
   105
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents: 47216
diff changeset
   106
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   107
        if (excludeClass != null && mainClass.equals(excludeClass)) {
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   108
            return false;
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
        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
   112
            return false;
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
   113
        }
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
   114
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   115
        return true;
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   116
    }
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   117
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   118
    private static Collection<VirtualMachineDescriptor> getSingleVMD(String pid) {
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   119
        Collection<VirtualMachineDescriptor> vids = new ArrayList<>();
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   120
        List<VirtualMachineDescriptor> vmds = VirtualMachine.list();
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   121
        for (VirtualMachineDescriptor vmd : vmds) {
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   122
            if (check(vmd, null, null)) {
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   123
                if (pid.equals(vmd.id())) {
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   124
                    vids.add(vmd);
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   125
                }
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   126
            }
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
   127
        }
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   128
        return vids;
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
   129
    }
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
   130
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   131
    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
   132
        String excludeCls = getExcludeStringFrom(excludeClass);
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   133
        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
   134
        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
   135
        for (VirtualMachineDescriptor vmd : vmds) {
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   136
            if (check(vmd, excludeCls, partialMatch)) {
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   137
                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
   138
            }
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
   139
        }
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   140
        return vids;
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
diff changeset
   141
    }
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   142
58506
dfe3d11378cc 8199136: Dead code in src/jdk.jcmd/share/classes/sun/tools/common/ProcessArgumentMatcher.java
dtitov
parents: 58265
diff changeset
   143
    public Collection<VirtualMachineDescriptor> getVirtualMachineDescriptors() {
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   144
        if (singlePid != null) {
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   145
            return getSingleVMD(singlePid);
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   146
        } else {
58506
dfe3d11378cc 8199136: Dead code in src/jdk.jcmd/share/classes/sun/tools/common/ProcessArgumentMatcher.java
dtitov
parents: 58265
diff changeset
   147
            return getVMDs(null, matchClass);
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   148
        }
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   149
    }
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
   150
44422
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   151
    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
   152
        if (singlePid != null) {
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   153
            // 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
   154
            // 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
   155
            return List.of(singlePid);
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   156
        } else {
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   157
            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
   158
        }
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   159
    }
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
}