author | cjplummer |
Wed, 13 Nov 2019 13:05:16 -0800 | |
changeset 59065 | 5f458adec06b |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
1 | 1 |
/* |
59065
5f458adec06b
8231635: SA Stackwalking code stuck in BasicTypeDataBase.findDynamicTypeForAddress()
cjplummer
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2003, 2019, 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:
1
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1
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:
1
diff
changeset
|
21 |
* questions. |
1 | 22 |
* |
23 |
*/ |
|
24 |
||
25 |
package sun.jvm.hotspot.runtime.amd64; |
|
26 |
||
27 |
import sun.jvm.hotspot.debugger.*; |
|
28 |
import sun.jvm.hotspot.debugger.amd64.*; |
|
29 |
import sun.jvm.hotspot.code.*; |
|
30 |
import sun.jvm.hotspot.interpreter.*; |
|
31 |
import sun.jvm.hotspot.runtime.*; |
|
10517
f92c9ff3a15f
7051798: SA-JDI: NPE in Frame.addressOfStackSlot(Frame.java:244)
never
parents:
5547
diff
changeset
|
32 |
import sun.jvm.hotspot.runtime.x86.*; |
1 | 33 |
|
34 |
/** <P> Should be able to be used on all amd64 platforms we support |
|
35 |
(Linux/amd64) to implement JavaThread's |
|
36 |
"currentFrameGuess()" functionality. Input is an AMD64ThreadContext; |
|
37 |
output is SP, FP, and PC for an AMD64Frame. Instantiation of the |
|
38 |
AMD64Frame is left to the caller, since we may need to subclass |
|
39 |
AMD64Frame to support signal handler frames on Unix platforms. </P> |
|
40 |
||
41 |
<P> Algorithm is to walk up the stack within a given range (say, |
|
42 |
512K at most) looking for a plausible PC and SP for a Java frame, |
|
43 |
also considering those coming in from the context. If we find a PC |
|
44 |
that belongs to the VM (i.e., in generated code like the |
|
45 |
interpreter or CodeCache) then we try to find an associated EBP. |
|
46 |
We repeat this until we either find a complete frame or run out of |
|
47 |
stack to look at. </P> */ |
|
48 |
||
49 |
public class AMD64CurrentFrameGuess { |
|
50 |
private AMD64ThreadContext context; |
|
51 |
private JavaThread thread; |
|
52 |
private Address spFound; |
|
53 |
private Address fpFound; |
|
54 |
private Address pcFound; |
|
55 |
||
56 |
private static final boolean DEBUG = System.getProperty("sun.jvm.hotspot.runtime.amd64.AMD64Frame.DEBUG") |
|
57 |
!= null; |
|
58 |
||
59 |
public AMD64CurrentFrameGuess(AMD64ThreadContext context, |
|
60 |
JavaThread thread) { |
|
61 |
this.context = context; |
|
62 |
this.thread = thread; |
|
63 |
} |
|
64 |
||
65 |
/** Returns false if not able to find a frame within a reasonable range. */ |
|
66 |
public boolean run(long regionInBytesToSearch) { |
|
67 |
Address sp = context.getRegisterAsAddress(AMD64ThreadContext.RSP); |
|
68 |
Address pc = context.getRegisterAsAddress(AMD64ThreadContext.RIP); |
|
69 |
Address fp = context.getRegisterAsAddress(AMD64ThreadContext.RBP); |
|
70 |
if (sp == null) { |
|
71 |
// Bail out if no last java frame either |
|
72 |
if (thread.getLastJavaSP() != null) { |
|
73 |
setValues(thread.getLastJavaSP(), thread.getLastJavaFP(), null); |
|
74 |
return true; |
|
75 |
} |
|
76 |
return false; |
|
77 |
} |
|
78 |
Address end = sp.addOffsetTo(regionInBytesToSearch); |
|
79 |
VM vm = VM.getVM(); |
|
80 |
||
81 |
setValues(null, null, null); // Assume we're not going to find anything |
|
82 |
||
83 |
if (vm.isJavaPCDbg(pc)) { |
|
84 |
if (vm.isClientCompiler()) { |
|
85 |
// If the topmost frame is a Java frame, we are (pretty much) |
|
86 |
// guaranteed to have a viable EBP. We should be more robust |
|
87 |
// than this (we have the potential for losing entire threads' |
|
88 |
// stack traces) but need to see how much work we really have |
|
89 |
// to do here. Searching the stack for an (SP, FP) pair is |
|
90 |
// hard since it's easy to misinterpret inter-frame stack |
|
91 |
// pointers as base-of-frame pointers; we also don't know the |
|
92 |
// sizes of C1 frames (not registered in the nmethod) so can't |
|
93 |
// derive them from ESP. |
|
94 |
||
95 |
setValues(sp, fp, pc); |
|
96 |
return true; |
|
97 |
} else { |
|
98 |
if (vm.getInterpreter().contains(pc)) { |
|
99 |
if (DEBUG) { |
|
100 |
System.out.println("CurrentFrameGuess: choosing interpreter frame: sp = " + |
|
101 |
sp + ", fp = " + fp + ", pc = " + pc); |
|
102 |
} |
|
103 |
setValues(sp, fp, pc); |
|
104 |
return true; |
|
105 |
} |
|
106 |
||
107 |
// For the server compiler, EBP is not guaranteed to be valid |
|
108 |
// for compiled code. In addition, an earlier attempt at a |
|
109 |
// non-searching algorithm (see below) failed because the |
|
110 |
// stack pointer from the thread context was pointing |
|
111 |
// (considerably) beyond the ostensible end of the stack, into |
|
112 |
// garbage; walking from the topmost frame back caused a crash. |
|
113 |
// |
|
114 |
// This algorithm takes the current PC as a given and tries to |
|
115 |
// find the correct corresponding SP by walking up the stack |
|
116 |
// and repeatedly performing stackwalks (very inefficient). |
|
117 |
// |
|
118 |
// FIXME: there is something wrong with stackwalking across |
|
119 |
// adapter frames...this is likely to be the root cause of the |
|
120 |
// failure with the simpler algorithm below. |
|
121 |
||
122 |
for (long offset = 0; |
|
123 |
offset < regionInBytesToSearch; |
|
124 |
offset += vm.getAddressSize()) { |
|
125 |
try { |
|
126 |
Address curSP = sp.addOffsetTo(offset); |
|
10517
f92c9ff3a15f
7051798: SA-JDI: NPE in Frame.addressOfStackSlot(Frame.java:244)
never
parents:
5547
diff
changeset
|
127 |
Frame frame = new X86Frame(curSP, null, pc); |
1 | 128 |
RegisterMap map = thread.newRegisterMap(false); |
129 |
while (frame != null) { |
|
130 |
if (frame.isEntryFrame() && frame.entryFrameIsFirst()) { |
|
131 |
// We were able to traverse all the way to the |
|
132 |
// bottommost Java frame. |
|
133 |
// This sp looks good. Keep it. |
|
134 |
if (DEBUG) { |
|
135 |
System.out.println("CurrentFrameGuess: Choosing sp = " + curSP + ", pc = " + pc); |
|
136 |
} |
|
137 |
setValues(curSP, null, pc); |
|
138 |
return true; |
|
139 |
} |
|
59065
5f458adec06b
8231635: SA Stackwalking code stuck in BasicTypeDataBase.findDynamicTypeForAddress()
cjplummer
parents:
47216
diff
changeset
|
140 |
Frame oldFrame = frame; |
1 | 141 |
frame = frame.sender(map); |
59065
5f458adec06b
8231635: SA Stackwalking code stuck in BasicTypeDataBase.findDynamicTypeForAddress()
cjplummer
parents:
47216
diff
changeset
|
142 |
if (frame.getSP().lessThanOrEqual(oldFrame.getSP())) { |
5f458adec06b
8231635: SA Stackwalking code stuck in BasicTypeDataBase.findDynamicTypeForAddress()
cjplummer
parents:
47216
diff
changeset
|
143 |
// Frame points to itself or to a location in the wrong direction. |
5f458adec06b
8231635: SA Stackwalking code stuck in BasicTypeDataBase.findDynamicTypeForAddress()
cjplummer
parents:
47216
diff
changeset
|
144 |
// Break the loop and move on to next offset. |
5f458adec06b
8231635: SA Stackwalking code stuck in BasicTypeDataBase.findDynamicTypeForAddress()
cjplummer
parents:
47216
diff
changeset
|
145 |
if (DEBUG) { |
5f458adec06b
8231635: SA Stackwalking code stuck in BasicTypeDataBase.findDynamicTypeForAddress()
cjplummer
parents:
47216
diff
changeset
|
146 |
System.out.println("AMD64CurrentFrameGuess.run: frame <= oldFrame: " + frame); |
5f458adec06b
8231635: SA Stackwalking code stuck in BasicTypeDataBase.findDynamicTypeForAddress()
cjplummer
parents:
47216
diff
changeset
|
147 |
} |
5f458adec06b
8231635: SA Stackwalking code stuck in BasicTypeDataBase.findDynamicTypeForAddress()
cjplummer
parents:
47216
diff
changeset
|
148 |
break; |
5f458adec06b
8231635: SA Stackwalking code stuck in BasicTypeDataBase.findDynamicTypeForAddress()
cjplummer
parents:
47216
diff
changeset
|
149 |
} |
1 | 150 |
} |
151 |
} catch (Exception e) { |
|
152 |
if (DEBUG) { |
|
153 |
System.out.println("CurrentFrameGuess: Exception " + e + " at offset " + offset); |
|
154 |
} |
|
155 |
// Bad SP. Try another. |
|
156 |
} |
|
157 |
} |
|
158 |
||
159 |
// Were not able to find a plausible SP to go with this PC. |
|
160 |
// Bail out. |
|
161 |
return false; |
|
162 |
||
163 |
/* |
|
164 |
// Original algorithm which does not work because SP was |
|
165 |
// pointing beyond where it should have: |
|
166 |
||
167 |
// For the server compiler, EBP is not guaranteed to be valid |
|
168 |
// for compiled code. We see whether the PC is in the |
|
169 |
// interpreter and take care of that, otherwise we run code |
|
170 |
// (unfortunately) duplicated from AMD64Frame.senderForCompiledFrame. |
|
171 |
||
172 |
CodeCache cc = vm.getCodeCache(); |
|
173 |
if (cc.contains(pc)) { |
|
174 |
CodeBlob cb = cc.findBlob(pc); |
|
175 |
||
176 |
// See if we can derive a frame pointer from SP and PC |
|
177 |
// NOTE: This is the code duplicated from AMD64Frame |
|
178 |
Address saved_fp = null; |
|
179 |
int llink_offset = cb.getLinkOffset(); |
|
180 |
if (llink_offset >= 0) { |
|
181 |
// Restore base-pointer, since next frame might be an interpreter frame. |
|
182 |
Address fp_addr = sp.addOffsetTo(VM.getVM().getAddressSize() * llink_offset); |
|
183 |
saved_fp = fp_addr.getAddressAt(0); |
|
184 |
} |
|
185 |
||
186 |
setValues(sp, saved_fp, pc); |
|
187 |
return true; |
|
188 |
} |
|
189 |
*/ |
|
190 |
} |
|
191 |
} else { |
|
192 |
// If the current program counter was not known to us as a Java |
|
193 |
// PC, we currently assume that we are in the run-time system |
|
194 |
// and attempt to look to thread-local storage for saved ESP and |
|
195 |
// EBP. Note that if these are null (because we were, in fact, |
|
196 |
// in Java code, i.e., vtable stubs or similar, and the SA |
|
197 |
// didn't have enough insight into the target VM to understand |
|
198 |
// that) then we are going to lose the entire stack trace for |
|
199 |
// the thread, which is sub-optimal. FIXME. |
|
200 |
||
201 |
if (DEBUG) { |
|
202 |
System.out.println("CurrentFrameGuess: choosing last Java frame: sp = " + |
|
203 |
thread.getLastJavaSP() + ", fp = " + thread.getLastJavaFP()); |
|
204 |
} |
|
205 |
if (thread.getLastJavaSP() == null) { |
|
206 |
return false; // No known Java frames on stack |
|
207 |
} |
|
208 |
setValues(thread.getLastJavaSP(), thread.getLastJavaFP(), null); |
|
209 |
return true; |
|
210 |
} |
|
211 |
} |
|
212 |
||
213 |
public Address getSP() { return spFound; } |
|
214 |
public Address getFP() { return fpFound; } |
|
215 |
/** May be null if getting values from thread-local storage; take |
|
216 |
care to call the correct AMD64Frame constructor to recover this if |
|
217 |
necessary */ |
|
218 |
public Address getPC() { return pcFound; } |
|
219 |
||
220 |
private void setValues(Address sp, Address fp, Address pc) { |
|
221 |
spFound = sp; |
|
222 |
fpFound = fp; |
|
223 |
pcFound = pc; |
|
224 |
} |
|
225 |
} |