author | rschmelter |
Mon, 22 Jul 2019 14:40:00 +0200 | |
changeset 55758 | bbe9c361a477 |
parent 50785 | d1b24f2ceca5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
55758
bbe9c361a477
8227868: jinfo and jstack can fail converting UTF8 output to strings
rschmelter
parents:
50785
diff
changeset
|
2 |
* Copyright (c) 2005, 2019, 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; |
|
32 |
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
|
33 |
import sun.tools.common.ProcessArgumentMatcher; |
55758
bbe9c361a477
8227868: jinfo and jstack can fail converting UTF8 output to strings
rschmelter
parents:
50785
diff
changeset
|
34 |
import sun.tools.common.PrintStreamPrinter; |
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; |
50785
d1b24f2ceca5
8200720: Print additional information in thread dump (times, allocated bytes etc.)
ghaug
parents:
48543
diff
changeset
|
51 |
boolean extended = false; |
2 | 52 |
|
53 |
// Parse the options (arguments starting with "-" ) |
|
54 |
int optionCount = 0; |
|
55 |
while (optionCount < args.length) { |
|
56 |
String arg = args[optionCount]; |
|
57 |
if (!arg.startsWith("-")) { |
|
58 |
break; |
|
59 |
} |
|
48543
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
60 |
if (arg.equals("-?") || |
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
61 |
arg.equals("-h") || |
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
62 |
arg.equals("--help") || |
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
63 |
// -help: legacy. |
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
64 |
arg.equals("-help")) { |
21673
c5d341bc60dd
8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents:
14342
diff
changeset
|
65 |
usage(0); |
c5d341bc60dd
8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents:
14342
diff
changeset
|
66 |
} |
c5d341bc60dd
8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents:
14342
diff
changeset
|
67 |
else { |
38360
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
68 |
if (arg.equals("-l")) { |
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
69 |
locks = true; |
2 | 70 |
} else { |
50785
d1b24f2ceca5
8200720: Print additional information in thread dump (times, allocated bytes etc.)
ghaug
parents:
48543
diff
changeset
|
71 |
if (arg.equals("-e")) { |
d1b24f2ceca5
8200720: Print additional information in thread dump (times, allocated bytes etc.)
ghaug
parents:
48543
diff
changeset
|
72 |
extended = true; |
d1b24f2ceca5
8200720: Print additional information in thread dump (times, allocated bytes etc.)
ghaug
parents:
48543
diff
changeset
|
73 |
} else { |
d1b24f2ceca5
8200720: Print additional information in thread dump (times, allocated bytes etc.)
ghaug
parents:
48543
diff
changeset
|
74 |
usage(1); |
d1b24f2ceca5
8200720: Print additional information in thread dump (times, allocated bytes etc.)
ghaug
parents:
48543
diff
changeset
|
75 |
} |
2 | 76 |
} |
77 |
} |
|
78 |
optionCount++; |
|
79 |
} |
|
80 |
||
38360
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
81 |
// Next we check the parameter count. |
2 | 82 |
int paramCount = args.length - optionCount; |
38360
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
83 |
if (paramCount != 1) { |
21673
c5d341bc60dd
8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents:
14342
diff
changeset
|
84 |
usage(1); |
2 | 85 |
} |
86 |
||
38360
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
87 |
// 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
|
88 |
String pidArg = args[optionCount]; |
50785
d1b24f2ceca5
8200720: Print additional information in thread dump (times, allocated bytes etc.)
ghaug
parents:
48543
diff
changeset
|
89 |
String params[]= new String[] { "" }; |
d1b24f2ceca5
8200720: Print additional information in thread dump (times, allocated bytes etc.)
ghaug
parents:
48543
diff
changeset
|
90 |
if (extended) { |
d1b24f2ceca5
8200720: Print additional information in thread dump (times, allocated bytes etc.)
ghaug
parents:
48543
diff
changeset
|
91 |
params[0] += "-e "; |
d1b24f2ceca5
8200720: Print additional information in thread dump (times, allocated bytes etc.)
ghaug
parents:
48543
diff
changeset
|
92 |
} |
38360
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
93 |
if (locks) { |
50785
d1b24f2ceca5
8200720: Print additional information in thread dump (times, allocated bytes etc.)
ghaug
parents:
48543
diff
changeset
|
94 |
params[0] += "-l"; |
2 | 95 |
} |
50785
d1b24f2ceca5
8200720: Print additional information in thread dump (times, allocated bytes etc.)
ghaug
parents:
48543
diff
changeset
|
96 |
|
38956
cf2c4bfba78a
8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents:
38361
diff
changeset
|
97 |
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
|
98 |
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
|
99 |
|
44422
14ab3266fe62
8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents:
38956
diff
changeset
|
100 |
if (pids.isEmpty()) { |
38956
cf2c4bfba78a
8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents:
38361
diff
changeset
|
101 |
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
|
102 |
System.exit(1); |
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 |
|
44422
14ab3266fe62
8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents:
38956
diff
changeset
|
105 |
for (String pid : pids) { |
14ab3266fe62
8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents:
38956
diff
changeset
|
106 |
if (pids.size() > 1) { |
14ab3266fe62
8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents:
38956
diff
changeset
|
107 |
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
|
108 |
} |
44422
14ab3266fe62
8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents:
38956
diff
changeset
|
109 |
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
|
110 |
} |
2 | 111 |
} |
112 |
||
113 |
// Attach to pid and perform a thread dump |
|
114 |
private static void runThreadDump(String pid, String args[]) throws Exception { |
|
115 |
VirtualMachine vm = null; |
|
116 |
try { |
|
117 |
vm = VirtualMachine.attach(pid); |
|
118 |
} catch (Exception x) { |
|
119 |
String msg = x.getMessage(); |
|
120 |
if (msg != null) { |
|
121 |
System.err.println(pid + ": " + msg); |
|
122 |
} else { |
|
123 |
x.printStackTrace(); |
|
124 |
} |
|
125 |
System.exit(1); |
|
126 |
} |
|
127 |
||
128 |
// Cast to HotSpotVirtualMachine as this is implementation specific |
|
129 |
// method. |
|
130 |
InputStream in = ((HotSpotVirtualMachine)vm).remoteDataDump((Object[])args); |
|
131 |
// read to EOF and just print output |
|
55758
bbe9c361a477
8227868: jinfo and jstack can fail converting UTF8 output to strings
rschmelter
parents:
50785
diff
changeset
|
132 |
PrintStreamPrinter.drainUTF8(in, System.out); |
2 | 133 |
vm.detach(); |
134 |
} |
|
135 |
||
38360
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
136 |
private static void checkForUnsupportedOptions(String[] args) { |
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
137 |
// Check arguments for -F, -m, and non-numeric value |
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
138 |
// and warn the user that SA is not supported anymore |
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
139 |
|
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
140 |
int paramCount = 0; |
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
141 |
|
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
142 |
for (String s : args) { |
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
143 |
if (s.equals("-F")) { |
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
144 |
SAOptionError("-F option used"); |
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
145 |
} |
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
146 |
|
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
147 |
if (s.equals("-m")) { |
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
148 |
SAOptionError("-m option used"); |
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 |
|
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
151 |
if (! s.startsWith("-")) { |
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
152 |
paramCount += 1; |
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 |
} |
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
155 |
|
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
156 |
if (paramCount > 1) { |
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
157 |
SAOptionError("More than one non-option argument"); |
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 |
} |
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
160 |
|
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
161 |
private static void SAOptionError(String msg) { |
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
162 |
System.err.println("Error: " + msg); |
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
36511
diff
changeset
|
163 |
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
|
164 |
System.exit(1); |
2 | 165 |
} |
166 |
||
167 |
// print usage message |
|
21673
c5d341bc60dd
8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents:
14342
diff
changeset
|
168 |
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
|
169 |
System.err.println("Usage:"); |
50785
d1b24f2ceca5
8200720: Print additional information in thread dump (times, allocated bytes etc.)
ghaug
parents:
48543
diff
changeset
|
170 |
System.err.println(" jstack [-l][-e] <pid>"); |
21673
c5d341bc60dd
8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents:
14342
diff
changeset
|
171 |
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
|
172 |
System.err.println(""); |
c5d341bc60dd
8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents:
14342
diff
changeset
|
173 |
System.err.println("Options:"); |
c5d341bc60dd
8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents:
14342
diff
changeset
|
174 |
System.err.println(" -l long listing. Prints additional information about locks"); |
50785
d1b24f2ceca5
8200720: Print additional information in thread dump (times, allocated bytes etc.)
ghaug
parents:
48543
diff
changeset
|
175 |
System.err.println(" -e extended listing. Prints additional information about threads"); |
48543
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
176 |
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
|
177 |
System.exit(exit); |
2 | 178 |
} |
179 |
} |