Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.jcmd/share/classes/sun/tools/jstack/JStack.java
41159 views
1
/*
2
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
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
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
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
*
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.
24
*/
25
26
package sun.tools.jstack;
27
28
import java.io.InputStream;
29
import java.util.Collection;
30
31
import com.sun.tools.attach.VirtualMachine;
32
import sun.tools.attach.HotSpotVirtualMachine;
33
import sun.tools.common.ProcessArgumentMatcher;
34
import sun.tools.common.PrintStreamPrinter;
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 {
42
43
public static void main(String[] args) throws Exception {
44
if (args.length == 0) {
45
usage(1); // no arguments
46
}
47
48
checkForUnsupportedOptions(args);
49
50
boolean locks = false;
51
boolean extended = false;
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
}
60
if (arg.equals("-?") ||
61
arg.equals("-h") ||
62
arg.equals("--help") ||
63
// -help: legacy.
64
arg.equals("-help")) {
65
usage(0);
66
}
67
else {
68
if (arg.equals("-l")) {
69
locks = true;
70
} else {
71
if (arg.equals("-e")) {
72
extended = true;
73
} else {
74
usage(1);
75
}
76
}
77
}
78
optionCount++;
79
}
80
81
// Next we check the parameter count.
82
int paramCount = args.length - optionCount;
83
if (paramCount != 1) {
84
usage(1);
85
}
86
87
// pass -l to thread dump operation to get extra lock info
88
String pidArg = args[optionCount];
89
String params[]= new String[] { "" };
90
if (extended) {
91
params[0] += "-e ";
92
}
93
if (locks) {
94
params[0] += "-l";
95
}
96
97
ProcessArgumentMatcher ap = new ProcessArgumentMatcher(pidArg);
98
Collection<String> pids = ap.getVirtualMachinePids(JStack.class);
99
100
if (pids.isEmpty()) {
101
System.err.println("Could not find any processes matching : '" + pidArg + "'");
102
System.exit(1);
103
}
104
105
for (String pid : pids) {
106
if (pids.size() > 1) {
107
System.out.println("Pid:" + pid);
108
}
109
runThreadDump(pid, params);
110
}
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
132
PrintStreamPrinter.drainUTF8(in, System.out);
133
vm.detach();
134
}
135
136
private static void checkForUnsupportedOptions(String[] args) {
137
// Check arguments for -F, -m, and non-numeric value
138
// and warn the user that SA is not supported anymore
139
140
int paramCount = 0;
141
142
for (String s : args) {
143
if (s.equals("-F")) {
144
SAOptionError("-F option used");
145
}
146
147
if (s.equals("-m")) {
148
SAOptionError("-m option used");
149
}
150
151
if (! s.startsWith("-")) {
152
paramCount += 1;
153
}
154
}
155
156
if (paramCount > 1) {
157
SAOptionError("More than one non-option argument");
158
}
159
}
160
161
private static void SAOptionError(String msg) {
162
System.err.println("Error: " + msg);
163
System.err.println("Cannot connect to core dump or remote debug server. Use jhsdb jstack instead");
164
System.exit(1);
165
}
166
167
// print usage message
168
private static void usage(int exit) {
169
System.err.println("Usage:");
170
System.err.println(" jstack [-l][-e] <pid>");
171
System.err.println(" (to connect to running process)");
172
System.err.println("");
173
System.err.println("Options:");
174
System.err.println(" -l long listing. Prints additional information about locks");
175
System.err.println(" -e extended listing. Prints additional information about threads");
176
System.err.println(" -? -h --help -help to print this help message");
177
System.exit(exit);
178
}
179
}
180
181