author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 46636 | hotspot/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/tools/PStack.java@26b8de0359a0 |
child 54955 | 46409371a691 |
permissions | -rw-r--r-- |
1 | 1 |
/* |
46636 | 2 |
* Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved. |
1 | 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 |
* |
|
5547
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
764
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
764
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
764
diff
changeset
|
21 |
* questions. |
1 | 22 |
* |
23 |
*/ |
|
24 |
||
25 |
package sun.jvm.hotspot.tools; |
|
26 |
||
27 |
import java.io.*; |
|
28 |
import java.util.*; |
|
29 |
import sun.jvm.hotspot.code.*; |
|
30 |
import sun.jvm.hotspot.interpreter.*; |
|
31 |
import sun.jvm.hotspot.debugger.*; |
|
32 |
import sun.jvm.hotspot.debugger.cdbg.*; |
|
33 |
import sun.jvm.hotspot.oops.*; |
|
34 |
import sun.jvm.hotspot.runtime.*; |
|
16351 | 35 |
import sun.jvm.hotspot.utilities.PlatformInfo; |
1 | 36 |
|
37 |
public class PStack extends Tool { |
|
13728
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
13088
diff
changeset
|
38 |
// in non-verbose mode, Method*s are not printed in java frames |
1 | 39 |
public PStack(boolean v, boolean concurrentLocks) { |
40 |
this.verbose = v; |
|
41 |
this.concurrentLocks = concurrentLocks; |
|
42 |
} |
|
43 |
||
44 |
public PStack() { |
|
45 |
this(true, true); |
|
46 |
} |
|
47 |
||
18481
fa7f6ad24216
8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents:
16351
diff
changeset
|
48 |
public PStack(JVMDebugger d) { |
fa7f6ad24216
8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents:
16351
diff
changeset
|
49 |
super(d); |
fa7f6ad24216
8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents:
16351
diff
changeset
|
50 |
} |
fa7f6ad24216
8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents:
16351
diff
changeset
|
51 |
|
1 | 52 |
public void run() { |
53 |
run(System.out); |
|
54 |
} |
|
55 |
||
56 |
public void run(PrintStream out) { |
|
57 |
Debugger dbg = getAgent().getDebugger(); |
|
15798 | 58 |
run(out, dbg); |
1 | 59 |
} |
60 |
||
61 |
public void run(PrintStream out, Debugger dbg) { |
|
16351 | 62 |
if (PlatformInfo.getOS().equals("darwin")) { |
63 |
out.println("Not available on Darwin"); |
|
64 |
return; |
|
65 |
} |
|
66 |
||
1 | 67 |
CDebugger cdbg = dbg.getCDebugger(); |
68 |
if (cdbg != null) { |
|
69 |
ConcurrentLocksPrinter concLocksPrinter = null; |
|
15798 | 70 |
// compute and cache java Vframes. |
71 |
initJFrameCache(); |
|
72 |
if (concurrentLocks) { |
|
73 |
concLocksPrinter = new ConcurrentLocksPrinter(); |
|
74 |
} |
|
75 |
// print Java level deadlocks |
|
76 |
try { |
|
77 |
DeadlockDetector.print(out); |
|
78 |
} catch (Exception exp) { |
|
79 |
out.println("can't print deadlock information: " + exp.getMessage()); |
|
1 | 80 |
} |
81 |
||
82 |
List l = cdbg.getThreadList(); |
|
83 |
final boolean cdbgCanDemangle = cdbg.canDemangle(); |
|
84 |
for (Iterator itr = l.iterator() ; itr.hasNext();) { |
|
85 |
ThreadProxy th = (ThreadProxy) itr.next(); |
|
86 |
try { |
|
87 |
CFrame f = cdbg.topFrameForThread(th); |
|
88 |
out.print("----------------- "); |
|
89 |
out.print(th); |
|
90 |
out.println(" -----------------"); |
|
46636 | 91 |
JavaThread jthread = (JavaThread) proxyToThread.get(th); |
92 |
if (jthread != null) { |
|
93 |
jthread.printThreadInfoOn(out); |
|
94 |
} |
|
1 | 95 |
while (f != null) { |
96 |
ClosestSymbol sym = f.closestSymbolToPC(); |
|
97 |
Address pc = f.pc(); |
|
764
67578bc37423
6620329: jstack prints double native methods on Solaris/sparc
swamyv
parents:
1
diff
changeset
|
98 |
out.print(pc + "\t"); |
1 | 99 |
if (sym != null) { |
100 |
String name = sym.getName(); |
|
101 |
if (cdbgCanDemangle) { |
|
102 |
name = cdbg.demangle(name); |
|
103 |
} |
|
764
67578bc37423
6620329: jstack prints double native methods on Solaris/sparc
swamyv
parents:
1
diff
changeset
|
104 |
out.print(name); |
1 | 105 |
long diff = sym.getOffset(); |
106 |
if (diff != 0L) { |
|
107 |
out.print(" + 0x" + Long.toHexString(diff)); |
|
108 |
} |
|
109 |
out.println(); |
|
110 |
} else { |
|
15798 | 111 |
// look for one or more java frames |
112 |
String[] names = null; |
|
113 |
// check interpreter frame |
|
114 |
Interpreter interp = VM.getVM().getInterpreter(); |
|
115 |
if (interp.contains(pc)) { |
|
116 |
names = getJavaNames(th, f.localVariableBase()); |
|
117 |
// print codelet name if we can't determine method |
|
118 |
if (names == null || names.length == 0) { |
|
119 |
out.print("<interpreter> "); |
|
120 |
InterpreterCodelet ic = interp.getCodeletContaining(pc); |
|
121 |
if (ic != null) { |
|
122 |
String desc = ic.getDescription(); |
|
123 |
if (desc != null) out.print(desc); |
|
124 |
} |
|
125 |
out.println(); |
|
126 |
} |
|
127 |
} else { |
|
128 |
// look for known code blobs |
|
129 |
CodeCache c = VM.getVM().getCodeCache(); |
|
130 |
if (c.contains(pc)) { |
|
131 |
CodeBlob cb = c.findBlobUnsafe(pc); |
|
132 |
if (cb.isNMethod()) { |
|
133 |
names = getJavaNames(th, f.localVariableBase()); |
|
134 |
// just print compiled code, if can't determine method |
|
135 |
if (names == null || names.length == 0) { |
|
136 |
out.println("<Unknown compiled code>"); |
|
137 |
} |
|
138 |
} else if (cb.isBufferBlob()) { |
|
139 |
out.println("<StubRoutines>"); |
|
140 |
} else if (cb.isRuntimeStub()) { |
|
141 |
out.println("<RuntimeStub>"); |
|
142 |
} else if (cb.isDeoptimizationStub()) { |
|
143 |
out.println("<DeoptimizationStub>"); |
|
144 |
} else if (cb.isUncommonTrapStub()) { |
|
145 |
out.println("<UncommonTrap>"); |
|
146 |
} else if (cb.isExceptionStub()) { |
|
147 |
out.println("<ExceptionStub>"); |
|
148 |
} else if (cb.isSafepointStub()) { |
|
149 |
out.println("<SafepointStub>"); |
|
150 |
} else { |
|
151 |
out.println("<Unknown code blob>"); |
|
152 |
} |
|
153 |
} else { |
|
154 |
printUnknown(out); |
|
155 |
} |
|
156 |
} |
|
157 |
// print java frames, if any |
|
158 |
if (names != null && names.length != 0) { |
|
159 |
// print java frame(s) |
|
160 |
for (int i = 0; i < names.length; i++) { |
|
161 |
out.println(names[i]); |
|
162 |
} |
|
163 |
} |
|
1 | 164 |
} |
13088 | 165 |
f = f.sender(th); |
1 | 166 |
} |
167 |
} catch (Exception exp) { |
|
168 |
exp.printStackTrace(); |
|
169 |
// continue, may be we can do a better job for other threads |
|
170 |
} |
|
15798 | 171 |
if (concurrentLocks) { |
1 | 172 |
JavaThread jthread = (JavaThread) proxyToThread.get(th); |
173 |
if (jthread != null) { |
|
174 |
concLocksPrinter.print(jthread, out); |
|
175 |
} |
|
176 |
} |
|
177 |
} // for threads |
|
178 |
} else { |
|
179 |
if (getDebugeeType() == DEBUGEE_REMOTE) { |
|
180 |
out.println("remote configuration is not yet implemented"); |
|
181 |
} else { |
|
182 |
out.println("not yet implemented (debugger does not support CDebugger)!"); |
|
183 |
} |
|
184 |
} |
|
185 |
} |
|
186 |
||
187 |
public static void main(String[] args) throws Exception { |
|
188 |
PStack t = new PStack(); |
|
21065
b8281f54308b
8025638: jmap returns 0 instead of 1 when it fails.
farvidsson
parents:
18481
diff
changeset
|
189 |
t.execute(args); |
1 | 190 |
} |
191 |
||
192 |
// -- Internals only below this point |
|
193 |
private Map jframeCache; // Map<ThreadProxy, JavaVFrame[]> |
|
194 |
private Map proxyToThread; // Map<ThreadProxy, JavaThread> |
|
195 |
private PrintStream out; |
|
196 |
private boolean verbose; |
|
197 |
private boolean concurrentLocks; |
|
198 |
||
199 |
private void initJFrameCache() { |
|
200 |
// cache frames for subsequent reference |
|
201 |
jframeCache = new HashMap(); |
|
202 |
proxyToThread = new HashMap(); |
|
203 |
Threads threads = VM.getVM().getThreads(); |
|
204 |
for (JavaThread cur = threads.first(); cur != null; cur = cur.next()) { |
|
205 |
List tmp = new ArrayList(10); |
|
206 |
try { |
|
207 |
for (JavaVFrame vf = cur.getLastJavaVFrameDbg(); vf != null; vf = vf.javaSender()) { |
|
208 |
tmp.add(vf); |
|
209 |
} |
|
210 |
} catch (Exception exp) { |
|
211 |
// may be we may get frames for other threads, continue |
|
212 |
// after printing stack trace. |
|
213 |
exp.printStackTrace(); |
|
214 |
} |
|
215 |
JavaVFrame[] jvframes = new JavaVFrame[tmp.size()]; |
|
216 |
System.arraycopy(tmp.toArray(), 0, jvframes, 0, jvframes.length); |
|
217 |
jframeCache.put(cur.getThreadProxy(), jvframes); |
|
218 |
proxyToThread.put(cur.getThreadProxy(), cur); |
|
219 |
} |
|
220 |
} |
|
221 |
||
764
67578bc37423
6620329: jstack prints double native methods on Solaris/sparc
swamyv
parents:
1
diff
changeset
|
222 |
private void printUnknown(PrintStream out) { |
67578bc37423
6620329: jstack prints double native methods on Solaris/sparc
swamyv
parents:
1
diff
changeset
|
223 |
out.println("\t????????"); |
1 | 224 |
} |
225 |
||
226 |
private String[] getJavaNames(ThreadProxy th, Address fp) { |
|
227 |
if (fp == null) { |
|
228 |
return null; |
|
229 |
} |
|
230 |
JavaVFrame[] jvframes = (JavaVFrame[]) jframeCache.get(th); |
|
231 |
if (jvframes == null) return null; // not a java thread |
|
232 |
List names = new ArrayList(10); |
|
233 |
for (int fCount = 0; fCount < jvframes.length; fCount++) { |
|
234 |
JavaVFrame vf = jvframes[fCount]; |
|
235 |
Frame f = vf.getFrame(); |
|
236 |
if (fp.equals(f.getFP())) { |
|
237 |
StringBuffer sb = new StringBuffer(); |
|
238 |
Method method = vf.getMethod(); |
|
239 |
// a special char to identify java frames in output |
|
240 |
sb.append("* "); |
|
241 |
sb.append(method.externalNameAndSignature()); |
|
242 |
sb.append(" bci:" + vf.getBCI()); |
|
243 |
int lineNumber = method.getLineNumberFromBCI(vf.getBCI()); |
|
244 |
if (lineNumber != -1) { |
|
245 |
sb.append(" line:" + lineNumber); |
|
246 |
} |
|
247 |
||
248 |
if (verbose) { |
|
13728
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
13088
diff
changeset
|
249 |
sb.append(" Method*:" + method.getAddress()); |
1 | 250 |
} |
251 |
||
252 |
if (vf.isCompiledFrame()) { |
|
253 |
sb.append(" (Compiled frame"); |
|
254 |
if (vf.isDeoptimized()) { |
|
255 |
sb.append(" [deoptimized]"); |
|
256 |
} |
|
257 |
} else if (vf.isInterpretedFrame()) { |
|
258 |
sb.append(" (Interpreted frame"); |
|
259 |
} |
|
260 |
if (vf.mayBeImpreciseDbg()) { |
|
261 |
sb.append("; information may be imprecise"); |
|
262 |
} |
|
263 |
sb.append(")"); |
|
264 |
names.add(sb.toString()); |
|
265 |
} |
|
266 |
} |
|
267 |
String[] res = new String[names.size()]; |
|
268 |
System.arraycopy(names.toArray(), 0, res, 0, res.length); |
|
269 |
return res; |
|
270 |
} |
|
271 |
} |