src/jdk.jcmd/share/classes/sun/tools/jstack/JStack.java
author goetz
Tue, 16 Jan 2018 08:48:34 +0100
changeset 48543 7067fe4e054e
parent 47216 71c04702a3d5
child 50785 d1b24f2ceca5
permissions -rw-r--r--
8189102: All tools should support -?, -h and --help Reviewed-by: kvn, jjg, weijun, alanb, rfield, ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
     2
 * Copyright (c) 2005, 2018, 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.jstack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.InputStream;
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
    29
import java.util.Collection;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.attach.VirtualMachine;
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    32
import com.sun.tools.attach.VirtualMachineDescriptor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.tools.attach.HotSpotVirtualMachine;
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
    34
import sun.tools.common.ProcessArgumentMatcher;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This class is the main class for the JStack utility. It parses its arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * and decides if the command should be executed by the SA JStack tool or by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * obtained the thread dump from a target process using the VM attach mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class JStack {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 25859
diff changeset
    42
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        if (args.length == 0) {
21673
c5d341bc60dd 8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents: 14342
diff changeset
    45
            usage(1); // no arguments
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    48
        checkForUnsupportedOptions(args);
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    49
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        boolean locks = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        // Parse the options (arguments starting with "-" )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        int optionCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        while (optionCount < args.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            String arg = args[optionCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            if (!arg.startsWith("-")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            }
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
    59
            if (arg.equals("-?")     ||
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
    60
                arg.equals("-h")     ||
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
    61
                arg.equals("--help") ||
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
    62
                // -help: legacy.
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
    63
                arg.equals("-help")) {
21673
c5d341bc60dd 8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents: 14342
diff changeset
    64
                usage(0);
c5d341bc60dd 8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents: 14342
diff changeset
    65
            }
c5d341bc60dd 8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents: 14342
diff changeset
    66
            else {
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    67
                if (arg.equals("-l")) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    68
                    locks = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                } else {
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    70
                    usage(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            optionCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    76
        // Next we check the parameter count.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        int paramCount = args.length - optionCount;
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    78
        if (paramCount != 1) {
21673
c5d341bc60dd 8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents: 14342
diff changeset
    79
            usage(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    82
        // pass -l to thread dump operation to get extra lock info
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
    83
        String pidArg = args[optionCount];
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    84
        String params[];
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    85
        if (locks) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    86
            params = new String[] { "-l" };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        } else {
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
    88
            params = new String[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    90
        ProcessArgumentMatcher ap = new ProcessArgumentMatcher(pidArg);
44422
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
    91
        Collection<String> pids = ap.getVirtualMachinePids(JStack.class);
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    92
44422
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
    93
        if (pids.isEmpty()) {
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    94
            System.err.println("Could not find any processes matching : '" + pidArg + "'");
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    95
            System.exit(1);
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    96
        }
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    97
44422
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
    98
        for (String pid : pids) {
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
    99
            if (pids.size() > 1) {
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   100
                System.out.println("Pid:" + pid);
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
   101
            }
44422
14ab3266fe62 8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents: 38956
diff changeset
   102
            runThreadDump(pid, params);
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 38360
diff changeset
   103
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    // Attach to pid and perform a thread dump
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private static void runThreadDump(String pid, String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        VirtualMachine vm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            vm = VirtualMachine.attach(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            String msg = x.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (msg != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                System.err.println(pid + ": " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                x.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        // Cast to HotSpotVirtualMachine as this is implementation specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        // method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        InputStream in = ((HotSpotVirtualMachine)vm).remoteDataDump((Object[])args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // read to EOF and just print output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        byte b[] = new byte[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            n = in.read(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                String s = new String(b, 0, n, "UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                System.out.print(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        } while (n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        vm.detach();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
38360
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   139
    private static void checkForUnsupportedOptions(String[] args) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   140
        // Check arguments for -F, -m, and non-numeric value
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   141
        // and warn the user that SA is not supported anymore
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   142
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   143
        int paramCount = 0;
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   144
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   145
        for (String s : args) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   146
            if (s.equals("-F")) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   147
                SAOptionError("-F option used");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   148
            }
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   149
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   150
            if (s.equals("-m")) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   151
                SAOptionError("-m option used");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   152
            }
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   153
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   154
            if (! s.startsWith("-")) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   155
                paramCount += 1;
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   156
            }
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   157
        }
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   158
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   159
        if (paramCount > 1) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   160
            SAOptionError("More than one non-option argument");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   161
        }
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   162
    }
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   163
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   164
    private static void SAOptionError(String msg) {
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   165
        System.err.println("Error: " + msg);
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   166
        System.err.println("Cannot connect to core dump or remote debug server. Use jhsdb jstack instead");
fb63be22ffa6 8155091: Remove SA related functions from tmtools
dsamersoff
parents: 36511
diff changeset
   167
        System.exit(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    // print usage message
21673
c5d341bc60dd 8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents: 14342
diff changeset
   171
    private static void usage(int exit) {
c5d341bc60dd 8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents: 14342
diff changeset
   172
        System.err.println("Usage:");
c5d341bc60dd 8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents: 14342
diff changeset
   173
        System.err.println("    jstack [-l] <pid>");
c5d341bc60dd 8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents: 14342
diff changeset
   174
        System.err.println("        (to connect to running process)");
c5d341bc60dd 8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents: 14342
diff changeset
   175
        System.err.println("");
c5d341bc60dd 8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents: 14342
diff changeset
   176
        System.err.println("Options:");
c5d341bc60dd 8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents: 14342
diff changeset
   177
        System.err.println("    -l  long listing. Prints additional information about locks");
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
   178
        System.err.println("    -? -h --help -help to print this help message");
21673
c5d341bc60dd 8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents: 14342
diff changeset
   179
        System.exit(exit);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
}