src/jdk.jcmd/share/classes/sun/tools/jps/Jps.java
author ihse
Tue, 13 Feb 2018 14:07:56 +0100
branchihse-remove-mapfiles-branch
changeset 56112 39286667d14e
parent 47216 71c04702a3d5
permissions -rw-r--r--
Remove libjsig mapfile.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
32474
5b41b53a4997 8134420: sun/tools/jps/TestJpsClass fails with java.lang.RuntimeException: The line 'line 2' does not match pattern '^\\d+\\s+.*': expected true, was false
jbachorik
parents: 27957
diff changeset
     2
 * Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.tools.jps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.jvmstat.monitor.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Application to provide a listing of monitorable java processes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author Brian Doherty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class Jps {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static Arguments arguments;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            arguments = new Arguments(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            System.err.println(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            Arguments.printUsage(System.err);
21673
c5d341bc60dd 8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents: 11125
diff changeset
    48
            System.exit(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        if (arguments.isHelp()) {
21673
c5d341bc60dd 8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents: 11125
diff changeset
    52
            Arguments.printUsage(System.err);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            System.exit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            HostIdentifier hostId = arguments.hostId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            MonitoredHost monitoredHost =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                    MonitoredHost.getMonitoredHost(hostId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            // get the set active JVMs on the specified host.
11125
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 8390
diff changeset
    62
            Set<Integer> jvms = monitoredHost.activeVms();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
11125
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 8390
diff changeset
    64
            for (Integer jvm: jvms) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                StringBuilder output = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                Throwable lastError = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
11125
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 8390
diff changeset
    68
                int lvmid = jvm;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                output.append(String.valueOf(lvmid));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                if (arguments.isQuiet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    System.out.println(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                MonitoredVm vm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                String vmidString = "//" + lvmid + "?mode=r";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
8390
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    80
                String errorString = null;
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    81
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                try {
8390
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    83
                    // Note: The VM associated with the current VM id may
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    84
                    // no longer be running so these queries may fail. We
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    85
                    // already added the VM id to the output stream above.
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    86
                    // If one of the queries fails, then we try to add a
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    87
                    // reasonable message to indicate that the requested
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    88
                    // info is not available.
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    89
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    90
                    errorString = " -- process information unavailable";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    VmIdentifier id = new VmIdentifier(vmidString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    vm = monitoredHost.getMonitoredVm(id, 0);
8390
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    93
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    94
                    errorString = " -- main class information unavailable";
27957
24b4e6082f19 8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents: 25859
diff changeset
    95
                    output.append(' ').append(MonitoredVmUtil.mainClass(vm,
8390
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    96
                            arguments.showLongPaths()));
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    97
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    98
                    if (arguments.showMainArgs()) {
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
    99
                        errorString = " -- main args information unavailable";
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   100
                        String mainArgs = MonitoredVmUtil.mainArgs(vm);
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   101
                        if (mainArgs != null && mainArgs.length() > 0) {
27957
24b4e6082f19 8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents: 25859
diff changeset
   102
                            output.append(' ').append(mainArgs);
8390
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   103
                        }
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   104
                    }
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   105
                    if (arguments.showVmArgs()) {
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   106
                        errorString = " -- jvm args information unavailable";
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   107
                        String jvmArgs = MonitoredVmUtil.jvmArgs(vm);
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   108
                        if (jvmArgs != null && jvmArgs.length() > 0) {
32474
5b41b53a4997 8134420: sun/tools/jps/TestJpsClass fails with java.lang.RuntimeException: The line 'line 2' does not match pattern '^\\d+\\s+.*': expected true, was false
jbachorik
parents: 27957
diff changeset
   109
                          output.append(' ')
5b41b53a4997 8134420: sun/tools/jps/TestJpsClass fails with java.lang.RuntimeException: The line 'line 2' does not match pattern '^\\d+\\s+.*': expected true, was false
jbachorik
parents: 27957
diff changeset
   110
                            .append(
5b41b53a4997 8134420: sun/tools/jps/TestJpsClass fails with java.lang.RuntimeException: The line 'line 2' does not match pattern '^\\d+\\s+.*': expected true, was false
jbachorik
parents: 27957
diff changeset
   111
                                // multi-line args are permitted
5b41b53a4997 8134420: sun/tools/jps/TestJpsClass fails with java.lang.RuntimeException: The line 'line 2' does not match pattern '^\\d+\\s+.*': expected true, was false
jbachorik
parents: 27957
diff changeset
   112
                                jvmArgs.replace("\n", "\\n").replace("\r", "\\r")
5b41b53a4997 8134420: sun/tools/jps/TestJpsClass fails with java.lang.RuntimeException: The line 'line 2' does not match pattern '^\\d+\\s+.*': expected true, was false
jbachorik
parents: 27957
diff changeset
   113
                            );
8390
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   114
                        }
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   115
                    }
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   116
                    if (arguments.showVmFlags()) {
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   117
                        errorString = " -- jvm flags information unavailable";
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   118
                        String jvmFlags = MonitoredVmUtil.jvmFlags(vm);
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   119
                        if (jvmFlags != null && jvmFlags.length() > 0) {
27957
24b4e6082f19 8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents: 25859
diff changeset
   120
                            output.append(' ').append(jvmFlags);
8390
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   121
                        }
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   122
                    }
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   123
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   124
                    errorString = " -- detach failed";
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   125
                    monitoredHost.detach(vm);
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   126
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   127
                    System.out.println(output);
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   128
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   129
                    errorString = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                } catch (URISyntaxException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    // unexpected as vmidString is based on a validated hostid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    lastError = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    lastError = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                } finally {
8390
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   137
                    if (errorString != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                         * we ignore most exceptions, as there are race
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                         * conditions where a JVM in 'jvms' may terminate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                         * before we get a chance to list its information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                         * Other errors, such as access and I/O exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                         * should stop us from iterating over the complete set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                         */
8390
f9ed509a2634 6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents: 5506
diff changeset
   145
                        output.append(errorString);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                        if (arguments.isDebug()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                            if ((lastError != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                    && (lastError.getMessage() != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                                output.append("\n\t");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                                output.append(lastError.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        System.out.println(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                        if (arguments.printStackTrace()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                            lastError.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        } catch (MonitorException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            if (e.getMessage() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                System.err.println(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                Throwable cause = e.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                if ((cause != null) && (cause.getMessage() != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    System.err.println(cause.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            }
21673
c5d341bc60dd 8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents: 11125
diff changeset
   172
            System.exit(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
}