Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/tools/PStack.java
41161 views
1
/*
2
* Copyright (c) 2003, 2021, 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*
23
*/
24
25
package sun.jvm.hotspot.tools;
26
27
import java.io.*;
28
import java.util.*;
29
import sun.jvm.hotspot.*;
30
import sun.jvm.hotspot.code.*;
31
import sun.jvm.hotspot.interpreter.*;
32
import sun.jvm.hotspot.debugger.*;
33
import sun.jvm.hotspot.debugger.cdbg.*;
34
import sun.jvm.hotspot.debugger.remote.*;
35
import sun.jvm.hotspot.oops.*;
36
import sun.jvm.hotspot.runtime.*;
37
import sun.jvm.hotspot.utilities.PlatformInfo;
38
39
public class PStack extends Tool {
40
// in non-verbose mode, Method*s are not printed in java frames
41
public PStack(boolean v, boolean concurrentLocks, HotSpotAgent agent) {
42
super(agent);
43
this.verbose = v;
44
this.concurrentLocks = concurrentLocks;
45
}
46
47
public PStack(boolean v, boolean concurrentLocks) {
48
this(v, concurrentLocks, null);
49
}
50
51
public PStack() {
52
this(true, true, null);
53
}
54
55
public PStack(JVMDebugger d) {
56
super(d);
57
}
58
59
public void run() {
60
run(System.out);
61
}
62
63
public void run(PrintStream out) {
64
Debugger dbg = getAgent().getDebugger();
65
run(out, dbg);
66
}
67
68
public void run(PrintStream out, Debugger dbg) {
69
CDebugger cdbg = dbg.getCDebugger();
70
if (cdbg != null) {
71
ConcurrentLocksPrinter concLocksPrinter = null;
72
// compute and cache java Vframes.
73
initJFrameCache();
74
if (concurrentLocks) {
75
concLocksPrinter = new ConcurrentLocksPrinter();
76
}
77
// print Java level deadlocks
78
try {
79
DeadlockDetector.print(out);
80
} catch (Exception exp) {
81
out.println("can't print deadlock information: " + exp.getMessage());
82
}
83
84
List<ThreadProxy> l = cdbg.getThreadList();
85
if (l.isEmpty() && PlatformInfo.getOS().equals("darwin")) {
86
// If the list is empty, we assume we attached to a process, and on OSX we can only
87
// get the native thread list for core files.
88
out.println("Not available for Mac OS X processes");
89
return;
90
}
91
final boolean cdbgCanDemangle = cdbg.canDemangle();
92
String fillerForAddress = " ".repeat(2 + 2 * (int) VM.getVM().getAddressSize()) + "\t";
93
for (Iterator<ThreadProxy> itr = l.iterator() ; itr.hasNext();) {
94
ThreadProxy th = itr.next();
95
try {
96
CFrame f = cdbg.topFrameForThread(th);
97
out.print("----------------- ");
98
out.print(th);
99
out.println(" -----------------");
100
JavaThread jthread = (JavaThread) proxyToThread.get(th);
101
if (jthread != null) {
102
jthread.printThreadInfoOn(out);
103
}
104
while (f != null) {
105
ClosestSymbol sym = f.closestSymbolToPC();
106
Address pc = f.pc();
107
out.print(pc + "\t");
108
if (sym != null) {
109
String name = sym.getName();
110
if (cdbgCanDemangle) {
111
name = cdbg.demangle(name);
112
}
113
out.print(name);
114
long diff = sym.getOffset();
115
if (diff != 0L) {
116
out.print(" + 0x" + Long.toHexString(diff));
117
}
118
out.println();
119
} else {
120
// look for one or more java frames
121
String[] names = null;
122
// check interpreter frame
123
Interpreter interp = VM.getVM().getInterpreter();
124
if (interp.contains(pc)) {
125
names = getJavaNames(th, f.localVariableBase());
126
// print codelet name if we can't determine method
127
if (names == null || names.length == 0) {
128
out.print("<interpreter> ");
129
InterpreterCodelet ic = interp.getCodeletContaining(pc);
130
if (ic != null) {
131
String desc = ic.getDescription();
132
if (desc != null) out.print(desc);
133
}
134
out.println();
135
}
136
} else {
137
// look for known code blobs
138
CodeCache c = VM.getVM().getCodeCache();
139
if (c.contains(pc)) {
140
CodeBlob cb = c.findBlobUnsafe(pc);
141
if (cb.isNMethod()) {
142
if (cb.isNativeMethod()) {
143
out.print(((CompiledMethod)cb).getMethod().externalNameAndSignature());
144
long diff = pc.minus(cb.codeBegin());
145
if (diff != 0L) {
146
out.print(" + 0x" + Long.toHexString(diff));
147
}
148
out.println(" (Native method)");
149
} else {
150
names = getJavaNames(th, f.localVariableBase());
151
// just print compiled code, if can't determine method
152
if (names == null || names.length == 0) {
153
out.println("<Unknown compiled code>");
154
}
155
}
156
} else if (cb.isBufferBlob()) {
157
out.println("<StubRoutines>");
158
} else if (cb.isRuntimeStub()) {
159
out.println("<RuntimeStub>");
160
} else if (cb.isDeoptimizationStub()) {
161
out.println("<DeoptimizationStub>");
162
} else if (cb.isUncommonTrapStub()) {
163
out.println("<UncommonTrap>");
164
} else if (cb.isExceptionStub()) {
165
out.println("<ExceptionStub>");
166
} else if (cb.isSafepointStub()) {
167
out.println("<SafepointStub>");
168
} else {
169
out.println("<Unknown code blob>");
170
}
171
} else {
172
printUnknown(out);
173
}
174
}
175
// print java frames, if any
176
if (names != null && names.length != 0) {
177
// print java frame(s)
178
for (int i = 0; i < names.length; i++) {
179
if (i > 0) {
180
out.print(fillerForAddress);
181
}
182
out.println(names[i]);
183
}
184
}
185
}
186
f = f.sender(th);
187
}
188
} catch (Exception exp) {
189
exp.printStackTrace();
190
// continue, may be we can do a better job for other threads
191
}
192
if (concurrentLocks) {
193
JavaThread jthread = (JavaThread) proxyToThread.get(th);
194
if (jthread != null) {
195
concLocksPrinter.print(jthread, out);
196
}
197
}
198
} // for threads
199
} else {
200
if (getDebugeeType() == DEBUGEE_REMOTE) {
201
out.print(((RemoteDebuggerClient)dbg).execCommandOnServer("pstack", Map.of("concurrentLocks", concurrentLocks)));
202
} else {
203
out.println("not yet implemented (debugger does not support CDebugger)!");
204
}
205
}
206
}
207
208
public static void main(String[] args) throws Exception {
209
PStack t = new PStack();
210
t.execute(args);
211
}
212
213
// -- Internals only below this point
214
private Map<ThreadProxy, JavaVFrame[]> jframeCache;
215
private Map<ThreadProxy, JavaThread> proxyToThread;
216
private PrintStream out;
217
private boolean verbose;
218
private boolean concurrentLocks;
219
220
private void initJFrameCache() {
221
// cache frames for subsequent reference
222
jframeCache = new HashMap<>();
223
proxyToThread = new HashMap<>();
224
Threads threads = VM.getVM().getThreads();
225
for (int i = 0; i < threads.getNumberOfThreads(); i++) {
226
JavaThread cur = threads.getJavaThreadAt(i);
227
List<JavaVFrame> tmp = new ArrayList<>(10);
228
try {
229
for (JavaVFrame vf = cur.getLastJavaVFrameDbg(); vf != null; vf = vf.javaSender()) {
230
tmp.add(vf);
231
}
232
} catch (Exception exp) {
233
// may be we may get frames for other threads, continue
234
// after printing stack trace.
235
exp.printStackTrace();
236
}
237
JavaVFrame[] jvframes = new JavaVFrame[tmp.size()];
238
System.arraycopy(tmp.toArray(), 0, jvframes, 0, jvframes.length);
239
jframeCache.put(cur.getThreadProxy(), jvframes);
240
proxyToThread.put(cur.getThreadProxy(), cur);
241
}
242
}
243
244
private void printUnknown(PrintStream out) {
245
out.println("\t????????");
246
}
247
248
private String[] getJavaNames(ThreadProxy th, Address fp) {
249
if (fp == null) {
250
return null;
251
}
252
JavaVFrame[] jvframes = (JavaVFrame[]) jframeCache.get(th);
253
if (jvframes == null) return null; // not a java thread
254
List<String> names = new ArrayList<>(10);
255
for (int fCount = 0; fCount < jvframes.length; fCount++) {
256
JavaVFrame vf = jvframes[fCount];
257
Frame f = vf.getFrame();
258
if (fp.equals(f.getFP())) {
259
StringBuilder sb = new StringBuilder();
260
Method method = vf.getMethod();
261
// a special char to identify java frames in output
262
sb.append("* ");
263
sb.append(method.externalNameAndSignature());
264
sb.append(" bci:").append(vf.getBCI());
265
int lineNumber = method.getLineNumberFromBCI(vf.getBCI());
266
if (lineNumber != -1) {
267
sb.append(" line:").append(lineNumber);
268
}
269
270
if (verbose) {
271
sb.append(" Method*:").append(method.getAddress());
272
}
273
274
if (vf.isCompiledFrame()) {
275
sb.append(" (Compiled frame");
276
if (vf.isDeoptimized()) {
277
sb.append(" [deoptimized]");
278
}
279
} else if (vf.isInterpretedFrame()) {
280
sb.append(" (Interpreted frame");
281
}
282
if (vf.mayBeImpreciseDbg()) {
283
sb.append("; information may be imprecise");
284
}
285
sb.append(")");
286
names.add(sb.toString());
287
}
288
}
289
String[] res = new String[names.size()];
290
System.arraycopy(names.toArray(), 0, res, 0, res.length);
291
return res;
292
}
293
294
public void setVerbose(boolean verbose) {
295
this.verbose = verbose;
296
}
297
298
public void setConcurrentLocks(boolean concurrentLocks) {
299
this.concurrentLocks = concurrentLocks;
300
}
301
}
302
303