author | goetz |
Tue, 16 Jan 2018 08:48:34 +0100 | |
changeset 48543 | 7067fe4e054e |
parent 47216 | 71c04702a3d5 |
child 50785 | d1b24f2ceca5 |
permissions | -rw-r--r-- |
2 | 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 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.tools.jstack; |
|
27 |
||
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 | 30 |
|
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 | 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 | 35 |
|
36 |
/* |
|
37 |
* This class is the main class for the JStack utility. It parses its arguments |
|
38 |
* and decides if the command should be executed by the SA JStack tool or by |
|
39 |
* obtained the thread dump from a target process using the VM attach mechanism |
|
40 |
*/ |
|
41 |
public class JStack { |
|
36511 | 42 |
|
2 | 43 |
public static void main(String[] args) throws Exception { |
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 | 46 |
} |
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 | 50 |
boolean locks = false; |
51 |
||
52 |
// Parse the options (arguments starting with "-" ) |
|
53 |
int optionCount = 0; |
|
54 |
while (optionCount < args.length) { |
|
55 |
String arg = args[optionCount]; |
|
56 |
if (!arg.startsWith("-")) { |
|
57 |
break; |
|
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 | 69 |
} else { |
38360
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
70 |
usage(1); |
2 | 71 |
} |
72 |
} |
|
73 |
optionCount++; |
|
74 |
} |
|
75 |
||
38360
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
76 |
// Next we check the parameter count. |
2 | 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 | 80 |
} |
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 | 87 |
} else { |
38360
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
88 |
params = new String[0]; |
2 | 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 | 104 |
} |
105 |
||
106 |
// Attach to pid and perform a thread dump |
|
107 |
private static void runThreadDump(String pid, String args[]) throws Exception { |
|
108 |
VirtualMachine vm = null; |
|
109 |
try { |
|
110 |
vm = VirtualMachine.attach(pid); |
|
111 |
} catch (Exception x) { |
|
112 |
String msg = x.getMessage(); |
|
113 |
if (msg != null) { |
|
114 |
System.err.println(pid + ": " + msg); |
|
115 |
} else { |
|
116 |
x.printStackTrace(); |
|
117 |
} |
|
118 |
System.exit(1); |
|
119 |
} |
|
120 |
||
121 |
// Cast to HotSpotVirtualMachine as this is implementation specific |
|
122 |
// method. |
|
123 |
InputStream in = ((HotSpotVirtualMachine)vm).remoteDataDump((Object[])args); |
|
124 |
||
125 |
// read to EOF and just print output |
|
126 |
byte b[] = new byte[256]; |
|
127 |
int n; |
|
128 |
do { |
|
129 |
n = in.read(b); |
|
130 |
if (n > 0) { |
|
131 |
String s = new String(b, 0, n, "UTF-8"); |
|
132 |
System.out.print(s); |
|
133 |
} |
|
134 |
} while (n > 0); |
|
135 |
in.close(); |
|
136 |
vm.detach(); |
|
137 |
} |
|
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 | 168 |
} |
169 |
||
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 | 180 |
} |
181 |
} |