comparison src/share/vm/prims/forte.cpp @ 107:93b6525e3b82

6603919: Stackwalking crash on x86 -server with Sun Studio's collect -j on Summary: Rewrite frame::safe_for_sender and friends to be safe for collector/analyzer Reviewed-by: dcubed, kvn
author sgoldman
date Tue, 08 Apr 2008 12:23:15 -0400
parents a61af66fc99e
children d1605aabd0a1
comparison
equal deleted inserted replaced
106:c9314fa4f757 107:93b6525e3b82
23 */ 23 */
24 24
25 # include "incls/_precompiled.incl" 25 # include "incls/_precompiled.incl"
26 # include "incls/_forte.cpp.incl" 26 # include "incls/_forte.cpp.incl"
27 27
28 // These name match the names reported by the forte quality kit
29 enum {
30 ticks_no_Java_frame = 0,
31 ticks_no_class_load = -1,
32 ticks_GC_active = -2,
33 ticks_unknown_not_Java = -3,
34 ticks_not_walkable_not_Java = -4,
35 ticks_unknown_Java = -5,
36 ticks_not_walkable_Java = -6,
37 ticks_unknown_state = -7,
38 ticks_thread_exit = -8,
39 ticks_deopt = -9,
40 ticks_safepoint = -10
41 };
28 42
29 //------------------------------------------------------- 43 //-------------------------------------------------------
30 44
31 // Native interfaces for use by Forte tools. 45 // Native interfaces for use by Forte tools.
32 46
39 vframeStreamForte(JavaThread *jt, frame fr, bool stop_at_java_call_stub); 53 vframeStreamForte(JavaThread *jt, frame fr, bool stop_at_java_call_stub);
40 void forte_next(); 54 void forte_next();
41 }; 55 };
42 56
43 57
44 static void forte_is_walkable_compiled_frame(frame* fr, RegisterMap* map, 58 static void is_decipherable_compiled_frame(frame* fr, RegisterMap* map,
45 bool* is_compiled_p, bool* is_walkable_p); 59 bool* is_compiled_p, bool* is_walkable_p);
46 static bool forte_is_walkable_interpreted_frame(frame* fr, 60 static bool is_decipherable_interpreted_frame(JavaThread* thread,
47 methodOop* method_p, int* bci_p); 61 frame* fr,
48 62 methodOop* method_p,
49 63 int* bci_p);
50 // A Forte specific version of frame:safe_for_sender(). 64
51 static bool forte_safe_for_sender(frame* fr, JavaThread *thread) { 65
52 bool ret_value = false; // be pessimistic 66
53 67
54 #ifdef COMPILER2 68 vframeStreamForte::vframeStreamForte(JavaThread *jt,
55 #if defined(IA32) || defined(AMD64) 69 frame fr,
56 { 70 bool stop_at_java_call_stub) : vframeStreamCommon(jt) {
57 // This check is the same as the standard safe_for_sender() 71
58 // on IA32 or AMD64 except that NULL FP values are tolerated
59 // for C2.
60 address sp = (address)fr->sp();
61 address fp = (address)fr->fp();
62 ret_value = sp != NULL && sp <= thread->stack_base() &&
63 sp >= thread->stack_base() - thread->stack_size() &&
64 (fp == NULL || (fp <= thread->stack_base() &&
65 fp >= thread->stack_base() - thread->stack_size()));
66
67 // We used to use standard safe_for_sender() when we are supposed
68 // to be executing Java code. However, that prevents us from
69 // walking some intrinsic stacks so now we have to be more refined.
70 // If we passed the above check and we have a NULL frame pointer
71 // and we are supposed to be executing Java code, then we have a
72 // couple of more checks to make.
73 if (ret_value && fp == NULL && (thread->thread_state() == _thread_in_Java
74 || thread->thread_state() == _thread_in_Java_trans)) {
75
76 if (fr->is_interpreted_frame()) {
77 // interpreted frames don't really have a NULL frame pointer
78 return false;
79 } else if (CodeCache::find_blob(fr->pc()) == NULL) {
80 // the NULL frame pointer should be associated with generated code
81 return false;
82 }
83 }
84 }
85
86 #else // !(IA32 || AMD64)
87 ret_value = fr->safe_for_sender(thread);
88 #endif // IA32 || AMD64
89
90 #else // !COMPILER2
91 ret_value = fr->safe_for_sender(thread);
92 #endif // COMPILER2
93
94 if (!ret_value) {
95 return ret_value; // not safe, nothing more to do
96 }
97
98 address sp1;
99
100 #ifdef SPARC
101 // On Solaris SPARC, when a compiler frame has an interpreted callee
102 // the _interpreter_sp_adjustment field contains the adjustment to
103 // this frame's SP made by that interpreted callee.
104 // For AsyncGetCallTrace(), we need to verify that the resulting SP
105 // is valid for the specified thread's stack.
106 sp1 = (address)fr->sp();
107 address sp2 = (address)fr->unextended_sp();
108
109 // If the second SP is NULL, then the _interpreter_sp_adjustment
110 // field simply adjusts this frame's SP to NULL and the frame is
111 // not safe. This strange value can be set in the frame constructor
112 // when our peek into the interpreted callee's adjusted value for
113 // this frame's SP finds a NULL. This can happen when SIGPROF
114 // catches us while we are creating the interpreter frame.
115 //
116 if (sp2 == NULL ||
117
118 // If the two SPs are different, then _interpreter_sp_adjustment
119 // is non-zero and we need to validate the second SP. We invert
120 // the range check from frame::safe_for_sender() and bail out
121 // if the second SP is not safe.
122 (sp1 != sp2 && !(sp2 <= thread->stack_base()
123 && sp2 >= (thread->stack_base() - thread->stack_size())))) {
124 return false;
125 }
126 #endif // SPARC
127
128 if (fr->is_entry_frame()) {
129 // This frame thinks it is an entry frame; we need to validate
130 // the JavaCallWrapper pointer.
131 // Note: frame::entry_frame_is_first() assumes that the
132 // JavaCallWrapper has a non-NULL _anchor field. We don't
133 // check that here (yet) since we've never seen a failure
134 // due to a NULL _anchor field.
135 // Update: Originally this check was done only for SPARC. However,
136 // this failure has now been seen on C2 C86. I have no reason to
137 // believe that this is not a general issue so I'm enabling the
138 // check for all compilers on all supported platforms.
139 #ifdef COMPILER2
140 #if defined(IA32) || defined(AMD64)
141 if (fr->fp() == NULL) {
142 // C2 X86 allows NULL frame pointers, but if we have one then
143 // we cannot call entry_frame_call_wrapper().
144 return false;
145 }
146 #endif // IA32 || AMD64
147 #endif // COMPILER2
148
149 sp1 = (address)fr->entry_frame_call_wrapper();
150 // We invert the range check from frame::safe_for_sender() and
151 // bail out if the JavaCallWrapper * is not safe.
152 if (!(sp1 <= thread->stack_base()
153 && sp1 >= (thread->stack_base() - thread->stack_size()))) {
154 return false;
155 }
156 }
157
158 return ret_value;
159 }
160
161
162 // Unknown compiled frames have caused assertion failures on Solaris
163 // X86. This code also detects unknown compiled frames on Solaris
164 // SPARC, but no assertion failures have been observed. However, I'm
165 // paranoid so I'm enabling this code whenever we have a compiler.
166 //
167 // Returns true if the specified frame is an unknown compiled frame
168 // and false otherwise.
169 static bool is_unknown_compiled_frame(frame* fr, JavaThread *thread) {
170 bool ret_value = false; // be optimistic
171
172 // This failure mode only occurs when the thread is in state
173 // _thread_in_Java so we are okay for this check for any other
174 // thread state.
175 //
176 // Note: _thread_in_Java does not always mean that the thread
177 // is executing Java code. AsyncGetCallTrace() has caught
178 // threads executing in JRT_LEAF() routines when the state
179 // will also be _thread_in_Java.
180 if (thread->thread_state() != _thread_in_Java) {
181 return ret_value;
182 }
183
184 // This failure mode only occurs with compiled frames so we are
185 // okay for this check for both entry and interpreted frames.
186 if (fr->is_entry_frame() || fr->is_interpreted_frame()) {
187 return ret_value;
188 }
189
190 // This failure mode only occurs when the compiled frame's PC
191 // is in the code cache so we are okay for this check if the
192 // PC is not in the code cache.
193 CodeBlob* cb = CodeCache::find_blob(fr->pc());
194 if (cb == NULL) {
195 return ret_value;
196 }
197
198 // We have compiled code in the code cache so it is time for
199 // the final check: let's see if any frame type is set
200 ret_value = !(
201 // is_entry_frame() is checked above
202 // testers that are a subset of is_entry_frame():
203 // is_first_frame()
204 fr->is_java_frame()
205 // testers that are a subset of is_java_frame():
206 // is_interpreted_frame()
207 // is_compiled_frame()
208 || fr->is_native_frame()
209 || fr->is_runtime_frame()
210 || fr->is_safepoint_blob_frame()
211 );
212
213 // If there is no frame type set, then we have an unknown compiled
214 // frame and sender() should not be called on it.
215
216 return ret_value;
217 }
218
219 #define DebugNonSafepoints_IS_CLEARED \
220 (!FLAG_IS_DEFAULT(DebugNonSafepoints) && !DebugNonSafepoints)
221
222 // if -XX:-DebugNonSafepoints, then top-frame will be skipped
223 vframeStreamForte::vframeStreamForte(JavaThread *jt, frame fr,
224 bool stop_at_java_call_stub) : vframeStreamCommon(jt) {
225 _stop_at_java_call_stub = stop_at_java_call_stub; 72 _stop_at_java_call_stub = stop_at_java_call_stub;
226 73 _frame = fr;
227 if (!DebugNonSafepoints_IS_CLEARED) { 74
228 // decode the top frame fully 75 // We must always have a valid frame to start filling
229 // (usual case, if JVMTI is enabled) 76
230 _frame = fr; 77 bool filled_in = fill_from_frame();
231 } else { 78
232 // skip top frame, as it may not be at safepoint 79 assert(filled_in, "invariant");
233 // For AsyncGetCallTrace(), we extracted as much info from the top 80
234 // frame as we could in forte_is_walkable_frame(). We also verified
235 // forte_safe_for_sender() so this sender() call is safe.
236 _frame = fr.sender(&_reg_map);
237 }
238
239 if (jt->thread_state() == _thread_in_Java && !fr.is_first_frame()) {
240 bool sender_check = false; // assume sender is not safe
241
242 if (forte_safe_for_sender(&_frame, jt)) {
243 // If the initial sender frame is safe, then continue on with other
244 // checks. The unsafe sender frame has been seen on Solaris X86
245 // with both Compiler1 and Compiler2. It has not been seen on
246 // Solaris SPARC, but seems like a good sanity check to have
247 // anyway.
248
249 // SIGPROF caught us in Java code and the current frame is not the
250 // first frame so we should sanity check the sender frame. It is
251 // possible for SIGPROF to catch us in the middle of making a call.
252 // When that happens the current frame is actually a combination of
253 // the real sender and some of the new call's info. We can't find
254 // the real sender with such a current frame and things can get
255 // confused.
256 //
257 // This sanity check has caught problems with the sender frame on
258 // Solaris SPARC. So far Solaris X86 has not had a failure here.
259 sender_check = _frame.is_entry_frame()
260 // testers that are a subset of is_entry_frame():
261 // is_first_frame()
262 || _frame.is_java_frame()
263 // testers that are a subset of is_java_frame():
264 // is_interpreted_frame()
265 // is_compiled_frame()
266 || _frame.is_native_frame()
267 || _frame.is_runtime_frame()
268 || _frame.is_safepoint_blob_frame()
269 ;
270
271 // We need an additional sanity check on an initial interpreted
272 // sender frame. This interpreted frame needs to be both walkable
273 // and have a valid BCI. This is yet another variant of SIGPROF
274 // catching us in the middle of making a call.
275 if (sender_check && _frame.is_interpreted_frame()) {
276 methodOop method = NULL;
277 int bci = -1;
278
279 if (!forte_is_walkable_interpreted_frame(&_frame, &method, &bci)
280 || bci == -1) {
281 sender_check = false;
282 }
283 }
284
285 // We need an additional sanity check on an initial compiled
286 // sender frame. This compiled frame also needs to be walkable.
287 // This is yet another variant of SIGPROF catching us in the
288 // middle of making a call.
289 if (sender_check && !_frame.is_interpreted_frame()) {
290 bool is_compiled, is_walkable;
291
292 forte_is_walkable_compiled_frame(&_frame, &_reg_map,
293 &is_compiled, &is_walkable);
294 if (is_compiled && !is_walkable) {
295 sender_check = false;
296 }
297 }
298 }
299
300 if (!sender_check) {
301 // nothing else to try if we can't recognize the sender
302 _mode = at_end_mode;
303 return;
304 }
305 }
306
307 int loop_count = 0;
308 int loop_max = MaxJavaStackTraceDepth * 2;
309
310 while (!fill_from_frame()) {
311 _frame = _frame.sender(&_reg_map);
312
313 #ifdef COMPILER2
314 #if defined(IA32) || defined(AMD64)
315 // Stress testing on C2 X86 has shown a periodic problem with
316 // the sender() call below. The initial _frame that we have on
317 // entry to the loop has already passed forte_safe_for_sender()
318 // so we only check frames after it.
319 if (!forte_safe_for_sender(&_frame, _thread)) {
320 _mode = at_end_mode;
321 return;
322 }
323 #endif // IA32 || AMD64
324 #endif // COMPILER2
325
326 if (++loop_count >= loop_max) {
327 // We have looped more than twice the number of possible
328 // Java frames. This indicates that we are trying to walk
329 // a stack that is in the middle of being constructed and
330 // it is self referential.
331 _mode = at_end_mode;
332 return;
333 }
334 }
335 } 81 }
336 82
337 83
338 // Solaris SPARC Compiler1 needs an additional check on the grandparent 84 // Solaris SPARC Compiler1 needs an additional check on the grandparent
339 // of the top_frame when the parent of the top_frame is interpreted and 85 // of the top_frame when the parent of the top_frame is interpreted and
356 int loop_max = MaxJavaStackTraceDepth * 2; 102 int loop_max = MaxJavaStackTraceDepth * 2;
357 103
358 104
359 do { 105 do {
360 106
361 #if defined(COMPILER1) && defined(SPARC) 107 loop_count++;
362 bool prevIsInterpreted = _frame.is_interpreted_frame(); 108
363 #endif // COMPILER1 && SPARC 109 // By the time we get here we should never see unsafe but better
364 110 // safe then segv'd
365 _frame = _frame.sender(&_reg_map); 111
366 112 if (loop_count > loop_max || !_frame.safe_for_sender(_thread)) {
367 if (!forte_safe_for_sender(&_frame, _thread)) {
368 _mode = at_end_mode; 113 _mode = at_end_mode;
369 return; 114 return;
370 } 115 }
371 116
372 #if defined(COMPILER1) && defined(SPARC) 117 _frame = _frame.sender(&_reg_map);
373 if (prevIsInterpreted) { 118
374 // previous callee was interpreted and may require a special check
375 if (_frame.is_compiled_frame() && _frame.cb()->is_compiled_by_c1()) {
376 // compiled sender called interpreted callee so need one more check
377 bool is_compiled, is_walkable;
378
379 // sanity check the compiled sender frame
380 forte_is_walkable_compiled_frame(&_frame, &_reg_map,
381 &is_compiled, &is_walkable);
382 assert(is_compiled, "sanity check");
383 if (!is_walkable) {
384 // compiled sender frame is not walkable so bail out
385 _mode = at_end_mode;
386 return;
387 }
388 }
389 }
390 #endif // COMPILER1 && SPARC
391
392 if (++loop_count >= loop_max) {
393 // We have looped more than twice the number of possible
394 // Java frames. This indicates that we are trying to walk
395 // a stack that is in the middle of being constructed and
396 // it is self referential.
397 _mode = at_end_mode;
398 return;
399 }
400 } while (!fill_from_frame()); 119 } while (!fill_from_frame());
401 } 120 }
402 121
403 // Determine if 'fr' is a walkable, compiled frame. 122 // Determine if 'fr' is a decipherable compiled frame. We are already
404 // *is_compiled_p is set to true if the frame is compiled and if it 123 // assured that fr is for a java nmethod.
405 // is, then *is_walkable_p is set to true if it is also walkable. 124
406 static void forte_is_walkable_compiled_frame(frame* fr, RegisterMap* map, 125 static bool is_decipherable_compiled_frame(frame* fr) {
407 bool* is_compiled_p, bool* is_walkable_p) { 126
408 127 assert(fr->cb() != NULL && fr->cb()->is_nmethod(), "invariant");
409 *is_compiled_p = false; 128 nmethod* nm = (nmethod*) fr->cb();
410 *is_walkable_p = false; 129 assert(nm->is_java_method(), "invariant");
411 130
412 CodeBlob* cb = CodeCache::find_blob(fr->pc()); 131 // First try and find an exact PcDesc
413 if (cb != NULL && 132
414 cb->is_nmethod() && 133 PcDesc* pc_desc = nm->pc_desc_at(fr->pc());
415 ((nmethod*)cb)->is_java_method()) { 134
416 // frame is compiled and executing a Java method 135 // Did we find a useful PcDesc?
417 *is_compiled_p = true; 136 if (pc_desc != NULL &&
418 137 pc_desc->scope_decode_offset() == DebugInformationRecorder::serialized_null) {
419 // Increment PC because the PcDesc we want is associated with 138
420 // the *end* of the instruction, and pc_desc_near searches 139 address probe_pc = fr->pc() + 1;
421 // forward to the first matching PC after the probe PC. 140 pc_desc = nm->pc_desc_near(probe_pc);
422 PcDesc* pc_desc = NULL; 141
423 if (!DebugNonSafepoints_IS_CLEARED) { 142 // Now do we have a useful PcDesc?
424 // usual case: look for any safepoint near the sampled PC 143
425 address probe_pc = fr->pc() + 1; 144 if (pc_desc != NULL &&
426 pc_desc = ((nmethod*) cb)->pc_desc_near(probe_pc); 145 pc_desc->scope_decode_offset() == DebugInformationRecorder::serialized_null) {
427 } else { 146 // No debug information available for this pc
428 // reduced functionality: only recognize PCs immediately after calls 147 // vframeStream would explode if we try and walk the frames.
429 pc_desc = ((nmethod*) cb)->pc_desc_at(fr->pc()); 148 return false;
430 } 149 }
431 if (pc_desc != NULL && (pc_desc->scope_decode_offset() 150
432 == DebugInformationRecorder::serialized_null)) { 151 // This PcDesc is useful however we must adjust the frame's pc
433 pc_desc = NULL; 152 // so that the vframeStream lookups will use this same pc
434 } 153
435 if (pc_desc != NULL) { 154 fr->set_pc(pc_desc->real_pc(nm));
436 // it has a PcDesc so the frame is also walkable 155 }
437 *is_walkable_p = true; 156
438 if (!DebugNonSafepoints_IS_CLEARED) { 157 return true;
439 // Normalize the PC to the one associated exactly with
440 // this PcDesc, so that subsequent stack-walking queries
441 // need not be approximate:
442 fr->set_pc(pc_desc->real_pc((nmethod*) cb));
443 }
444 }
445 // Implied else: this compiled frame has no PcDesc, i.e., contains
446 // a frameless stub such as C1 method exit, so it is not walkable.
447 }
448 // Implied else: this isn't a compiled frame so it isn't a
449 // walkable, compiled frame.
450 } 158 }
451 159
452 // Determine if 'fr' is a walkable interpreted frame. Returns false 160 // Determine if 'fr' is a walkable interpreted frame. Returns false
453 // if it is not. *method_p, and *bci_p are not set when false is 161 // if it is not. *method_p, and *bci_p are not set when false is
454 // returned. *method_p is non-NULL if frame was executing a Java 162 // returned. *method_p is non-NULL if frame was executing a Java
455 // method. *bci_p is != -1 if a valid BCI in the Java method could 163 // method. *bci_p is != -1 if a valid BCI in the Java method could
456 // be found. 164 // be found.
457 // Note: this method returns true when a valid Java method is found 165 // Note: this method returns true when a valid Java method is found
458 // even if a valid BCI cannot be found. 166 // even if a valid BCI cannot be found.
459 167
460 static bool forte_is_walkable_interpreted_frame(frame* fr, 168 static bool is_decipherable_interpreted_frame(JavaThread* thread,
461 methodOop* method_p, int* bci_p) { 169 frame* fr,
170 methodOop* method_p,
171 int* bci_p) {
462 assert(fr->is_interpreted_frame(), "just checking"); 172 assert(fr->is_interpreted_frame(), "just checking");
463 173
464 // top frame is an interpreted frame 174 // top frame is an interpreted frame
465 // check if it is walkable (i.e. valid methodOop and valid bci) 175 // check if it is walkable (i.e. valid methodOop and valid bci)
466 if (fr->is_interpreted_frame_valid()) { 176
467 if (fr->fp() != NULL) { 177 // Because we may be racing a gc thread the method and/or bci
468 // access address in order not to trigger asserts that 178 // of a valid interpreter frame may look bad causing us to
469 // are built in interpreter_frame_method function 179 // fail the is_interpreted_frame_valid test. If the thread
470 methodOop method = *fr->interpreter_frame_method_addr(); 180 // is in any of the following states we are assured that the
471 if (Universe::heap()->is_valid_method(method)) { 181 // frame is in fact valid and we must have hit the race.
472 intptr_t bcx = fr->interpreter_frame_bcx(); 182
473 int bci = method->validate_bci_from_bcx(bcx); 183 JavaThreadState state = thread->thread_state();
474 // note: bci is set to -1 if not a valid bci 184 bool known_valid = (state == _thread_in_native ||
475 *method_p = method; 185 state == _thread_in_vm ||
476 *bci_p = bci; 186 state == _thread_blocked );
187
188 if (known_valid || fr->is_interpreted_frame_valid(thread)) {
189
190 // The frame code should completely validate the frame so that
191 // references to methodOop and bci are completely safe to access
192 // If they aren't the frame code should be fixed not this
193 // code. However since gc isn't locked out the values could be
194 // stale. This is a race we can never completely win since we can't
195 // lock out gc so do one last check after retrieving their values
196 // from the frame for additional safety
197
198 methodOop method = fr->interpreter_frame_method();
199
200 // We've at least found a method.
201 // NOTE: there is something to be said for the approach that
202 // if we don't find a valid bci then the method is not likely
203 // a valid method. Then again we may have caught an interpreter
204 // frame in the middle of construction and the bci field is
205 // not yet valid.
206
207 *method_p = method;
208
209 // See if gc may have invalidated method since we validated frame
210
211 if (!Universe::heap()->is_valid_method(method)) return false;
212
213 intptr_t bcx = fr->interpreter_frame_bcx();
214
215 int bci = method->validate_bci_from_bcx(bcx);
216
217 // note: bci is set to -1 if not a valid bci
218 *bci_p = bci;
219 return true;
220 }
221
222 return false;
223 }
224
225
226 // Determine if 'fr' can be used to find an initial Java frame.
227 // Return false if it can not find a fully decipherable Java frame
228 // (in other words a frame that isn't safe to use in a vframe stream).
229 // Obviously if it can't even find a Java frame false will also be returned.
230 //
231 // If we find a Java frame decipherable or not then by definition we have
232 // identified a method and that will be returned to the caller via method_p.
233 // If we can determine a bci that is returned also. (Hmm is it possible
234 // to return a method and bci and still return false? )
235 //
236 // The initial Java frame we find (if any) is return via initial_frame_p.
237 //
238
239 static bool find_initial_Java_frame(JavaThread* thread,
240 frame* fr,
241 frame* initial_frame_p,
242 methodOop* method_p,
243 int* bci_p) {
244
245 // It is possible that for a frame containing an nmethod
246 // we can capture the method but no bci. If we get no
247 // bci the frame isn't walkable but the method is usable.
248 // Therefore we init the returned methodOop to NULL so the
249 // caller can make the distinction.
250
251 *method_p = NULL;
252
253 // On the initial call to this method the frame we get may not be
254 // recognizable to us. This should only happen if we are in a JRT_LEAF
255 // or something called by a JRT_LEAF method.
256
257
258
259 frame candidate = *fr;
260
261 // If the starting frame we were given has no codeBlob associated with
262 // it see if we can find such a frame because only frames with codeBlobs
263 // are possible Java frames.
264
265 if (fr->cb() == NULL) {
266
267 // See if we can find a useful frame
268 int loop_count;
269 int loop_max = MaxJavaStackTraceDepth * 2;
270 RegisterMap map(thread, false);
271
272 for (loop_count = 0; loop_count < loop_max; loop_count++) {
273 if (!candidate.safe_for_sender(thread)) return false;
274 candidate = candidate.sender(&map);
275 if (candidate.cb() != NULL) break;
276 }
277 if (candidate.cb() == NULL) return false;
278 }
279
280 // We have a frame known to be in the codeCache
281 // We will hopefully be able to figure out something to do with it.
282 int loop_count;
283 int loop_max = MaxJavaStackTraceDepth * 2;
284 RegisterMap map(thread, false);
285
286 for (loop_count = 0; loop_count < loop_max; loop_count++) {
287
288 if (candidate.is_first_frame()) {
289 // If initial frame is frame from StubGenerator and there is no
290 // previous anchor, there are no java frames associated with a method
291 return false;
292 }
293
294 if (candidate.is_interpreted_frame()) {
295 if (is_decipherable_interpreted_frame(thread, &candidate, method_p, bci_p)) {
296 *initial_frame_p = candidate;
477 return true; 297 return true;
478 } 298 }
479 } 299
480 } 300 // Hopefully we got some data
301 return false;
302 }
303
304 if (candidate.cb()->is_nmethod()) {
305
306 nmethod* nm = (nmethod*) candidate.cb();
307 *method_p = nm->method();
308
309 // If the frame isn't fully decipherable then the default
310 // value for the bci is a signal that we don't have a bci.
311 // If we have a decipherable frame this bci value will
312 // not be used.
313
314 *bci_p = -1;
315
316 *initial_frame_p = candidate;
317
318 // Native wrapper code is trivial to decode by vframeStream
319
320 if (nm->is_native_method()) return true;
321
322 // If it isn't decipherable then we have found a pc that doesn't
323 // have a PCDesc that can get us a bci however we did find
324 // a method
325
326 if (!is_decipherable_compiled_frame(&candidate)) {
327 return false;
328 }
329
330 // is_decipherable_compiled_frame may modify candidate's pc
331 *initial_frame_p = candidate;
332
333 return true;
334 }
335
336 // Must be some stub frame that we don't care about
337
338 if (!candidate.safe_for_sender(thread)) return false;
339 candidate = candidate.sender(&map);
340
341 // If it isn't in the code cache something is wrong
342 // since once we find a frame in the code cache they
343 // all should be there.
344
345 if (candidate.cb() == NULL) return false;
346
347 }
348
481 return false; 349 return false;
482 } 350
483
484
485 // Determine if 'fr' can be used to find a walkable frame. Returns
486 // false if a walkable frame cannot be found. *walkframe_p, *method_p,
487 // and *bci_p are not set when false is returned. Returns true if a
488 // walkable frame is returned via *walkframe_p. *method_p is non-NULL
489 // if the returned frame was executing a Java method. *bci_p is != -1
490 // if a valid BCI in the Java method could be found.
491 //
492 // *walkframe_p will be used by vframeStreamForte as the initial
493 // frame for walking the stack. Currently the initial frame is
494 // skipped by vframeStreamForte because we inherited the logic from
495 // the vframeStream class. This needs to be revisited in the future.
496 static bool forte_is_walkable_frame(JavaThread* thread, frame* fr,
497 frame* walkframe_p, methodOop* method_p, int* bci_p) {
498
499 if (!forte_safe_for_sender(fr, thread)
500 || is_unknown_compiled_frame(fr, thread)
501 ) {
502 // If the initial frame is not safe, then bail out. So far this
503 // has only been seen on Solaris X86 with Compiler2, but it seems
504 // like a great initial sanity check.
505 return false;
506 }
507
508 if (fr->is_first_frame()) {
509 // If initial frame is frame from StubGenerator and there is no
510 // previous anchor, there are no java frames yet
511 return false;
512 }
513
514 if (fr->is_interpreted_frame()) {
515 if (forte_is_walkable_interpreted_frame(fr, method_p, bci_p)) {
516 *walkframe_p = *fr;
517 return true;
518 }
519 return false;
520 }
521
522 // At this point we have something other than a first frame or an
523 // interpreted frame.
524
525 methodOop method = NULL;
526 frame candidate = *fr;
527
528 // If we loop more than twice the number of possible Java
529 // frames, then this indicates that we are trying to walk
530 // a stack that is in the middle of being constructed and
531 // it is self referential. So far this problem has only
532 // been seen on Solaris X86 Compiler2, but it seems like
533 // a good robustness fix for all platforms.
534
535 int loop_count;
536 int loop_max = MaxJavaStackTraceDepth * 2;
537
538 for (loop_count = 0; loop_count < loop_max; loop_count++) {
539 // determine if the candidate frame is executing a Java method
540 if (CodeCache::contains(candidate.pc())) {
541 // candidate is a compiled frame or stub routine
542 CodeBlob* cb = CodeCache::find_blob(candidate.pc());
543
544 if (cb->is_nmethod()) {
545 method = ((nmethod *)cb)->method();
546 }
547 } // end if CodeCache has our PC
548
549 RegisterMap map(thread, false);
550
551 // we have a Java frame that seems reasonable
552 if (method != NULL && candidate.is_java_frame()
553 && candidate.sp() != NULL && candidate.pc() != NULL) {
554 // we need to sanity check the candidate further
555 bool is_compiled, is_walkable;
556
557 forte_is_walkable_compiled_frame(&candidate, &map, &is_compiled,
558 &is_walkable);
559 if (is_compiled) {
560 // At this point, we know we have a compiled Java frame with
561 // method information that we want to return. We don't check
562 // the is_walkable flag here because that flag pertains to
563 // vframeStreamForte work that is done after we are done here.
564 break;
565 }
566 }
567
568 // At this point, the candidate doesn't work so try the sender.
569
570 // For AsyncGetCallTrace() we cannot assume there is a sender
571 // for the initial frame. The initial forte_safe_for_sender() call
572 // and check for is_first_frame() is done on entry to this method.
573 candidate = candidate.sender(&map);
574 if (!forte_safe_for_sender(&candidate, thread)) {
575
576 #ifdef COMPILER2
577 #if defined(IA32) || defined(AMD64)
578 // C2 on X86 can use the ebp register as a general purpose register
579 // which can cause the candidate to fail theforte_safe_for_sender()
580 // above. We try one more time using a NULL frame pointer (fp).
581
582 candidate = frame(candidate.sp(), NULL, candidate.pc());
583 if (!forte_safe_for_sender(&candidate, thread)) {
584 #endif // IA32 || AMD64
585 #endif // COMPILER2
586
587 return false;
588
589 #ifdef COMPILER2
590 #if defined(IA32) || defined(AMD64)
591 } // end forte_safe_for_sender retry with NULL fp
592 #endif // IA32 || AMD64
593 #endif // COMPILER2
594
595 } // end first forte_safe_for_sender check
596
597 if (candidate.is_first_frame()
598 || is_unknown_compiled_frame(&candidate, thread)) {
599 return false;
600 }
601 } // end for loop_count
602
603 if (method == NULL) {
604 // If we didn't get any method info from the candidate, then
605 // we have nothing to return so bail out.
606 return false;
607 }
608
609 *walkframe_p = candidate;
610 *method_p = method;
611 *bci_p = -1;
612 return true;
613 } 351 }
614 352
615 353
616 // call frame copied from old .h file and renamed 354 // call frame copied from old .h file and renamed
617 typedef struct { 355 typedef struct {
625 jint num_frames; // number of frames in this trace 363 jint num_frames; // number of frames in this trace
626 ASGCT_CallFrame *frames; // frames 364 ASGCT_CallFrame *frames; // frames
627 } ASGCT_CallTrace; 365 } ASGCT_CallTrace;
628 366
629 static void forte_fill_call_trace_given_top(JavaThread* thd, 367 static void forte_fill_call_trace_given_top(JavaThread* thd,
630 ASGCT_CallTrace* trace, int depth, frame top_frame) { 368 ASGCT_CallTrace* trace,
369 int depth,
370 frame top_frame) {
631 NoHandleMark nhm; 371 NoHandleMark nhm;
632 372
633 frame walkframe; 373 frame initial_Java_frame;
634 methodOop method; 374 methodOop method;
635 int bci; 375 int bci;
636 int count; 376 int count;
637 377
638 count = 0; 378 count = 0;
639 assert(trace->frames != NULL, "trace->frames must be non-NULL"); 379 assert(trace->frames != NULL, "trace->frames must be non-NULL");
640 380
641 if (!forte_is_walkable_frame(thd, &top_frame, &walkframe, &method, &bci)) { 381 bool fully_decipherable = find_initial_Java_frame(thd, &top_frame, &initial_Java_frame, &method, &bci);
642 // return if no walkable frame is found 382
383 // The frame might not be walkable but still recovered a method
384 // (e.g. an nmethod with no scope info for the pc
385
386 if (method == NULL) return;
387
388 CollectedHeap* ch = Universe::heap();
389
390 // The method is not stored GC safe so see if GC became active
391 // after we entered AsyncGetCallTrace() and before we try to
392 // use the methodOop.
393 // Yes, there is still a window after this check and before
394 // we use methodOop below, but we can't lock out GC so that
395 // has to be an acceptable risk.
396 if (!ch->is_valid_method(method)) {
397 trace->num_frames = ticks_GC_active; // -2
643 return; 398 return;
644 } 399 }
645 400
646 CollectedHeap* ch = Universe::heap(); 401 // We got a Java frame however it isn't fully decipherable
647 402 // so it won't necessarily be safe to use it for the
648 if (method != NULL) { 403 // initial frame in the vframe stream.
649 // The method is not stored GC safe so see if GC became active 404
650 // after we entered AsyncGetCallTrace() and before we try to 405 if (!fully_decipherable) {
651 // use the methodOop. 406 // Take whatever method the top-frame decoder managed to scrape up.
652 // Yes, there is still a window after this check and before 407 // We look further at the top frame only if non-safepoint
653 // we use methodOop below, but we can't lock out GC so that 408 // debugging information is available.
654 // has to be an acceptable risk. 409 count++;
655 if (!ch->is_valid_method(method)) { 410 trace->num_frames = count;
656 trace->num_frames = -2; 411 trace->frames[0].method_id = method->find_jmethod_id_or_null();
657 return; 412 if (!method->is_native()) {
658 } 413 trace->frames[0].lineno = bci;
659 414 } else {
660 if (DebugNonSafepoints_IS_CLEARED) { 415 trace->frames[0].lineno = -3;
661 // Take whatever method the top-frame decoder managed to scrape up. 416 }
662 // We look further at the top frame only if non-safepoint 417
663 // debugging information is available. 418 if (!initial_Java_frame.safe_for_sender(thd)) return;
664 count++; 419
665 trace->num_frames = count; 420 RegisterMap map(thd, false);
666 trace->frames[0].method_id = method->find_jmethod_id_or_null(); 421 initial_Java_frame = initial_Java_frame.sender(&map);
667 if (!method->is_native()) { 422 }
668 trace->frames[0].lineno = bci; 423
669 } else { 424 vframeStreamForte st(thd, initial_Java_frame, false);
670 trace->frames[0].lineno = -3; 425
671 }
672 }
673 }
674
675 // check has_last_Java_frame() after looking at the top frame
676 // which may be an interpreted Java frame.
677 if (!thd->has_last_Java_frame() && method == NULL) {
678 trace->num_frames = 0;
679 return;
680 }
681
682 vframeStreamForte st(thd, walkframe, false);
683 for (; !st.at_end() && count < depth; st.forte_next(), count++) { 426 for (; !st.at_end() && count < depth; st.forte_next(), count++) {
684 bci = st.bci(); 427 bci = st.bci();
685 method = st.method(); 428 method = st.method();
686 429
687 // The method is not stored GC safe so see if GC became active 430 // The method is not stored GC safe so see if GC became active
691 // we use methodOop below, but we can't lock out GC so that 434 // we use methodOop below, but we can't lock out GC so that
692 // has to be an acceptable risk. 435 // has to be an acceptable risk.
693 if (!ch->is_valid_method(method)) { 436 if (!ch->is_valid_method(method)) {
694 // we throw away everything we've gathered in this sample since 437 // we throw away everything we've gathered in this sample since
695 // none of it is safe 438 // none of it is safe
696 trace->num_frames = -2; 439 trace->num_frames = ticks_GC_active; // -2
697 return; 440 return;
698 } 441 }
699 442
700 trace->frames[count].method_id = method->find_jmethod_id_or_null(); 443 trace->frames[count].method_id = method->find_jmethod_id_or_null();
701 if (!method->is_native()) { 444 if (!method->is_native()) {
763 // lineno - (-3) 506 // lineno - (-3)
764 // method_id - jmethodID of the method being executed 507 // method_id - jmethodID of the method being executed
765 508
766 extern "C" { 509 extern "C" {
767 void AsyncGetCallTrace(ASGCT_CallTrace *trace, jint depth, void* ucontext) { 510 void AsyncGetCallTrace(ASGCT_CallTrace *trace, jint depth, void* ucontext) {
511
512 // This is if'd out because we no longer use thread suspension.
513 // However if someone wanted to backport this to a 5.0 jvm then this
514 // code would be important.
515 #if 0
768 if (SafepointSynchronize::is_synchronizing()) { 516 if (SafepointSynchronize::is_synchronizing()) {
769 // The safepoint mechanism is trying to synchronize all the threads. 517 // The safepoint mechanism is trying to synchronize all the threads.
770 // Since this can involve thread suspension, it is not safe for us 518 // Since this can involve thread suspension, it is not safe for us
771 // to be here. We can reduce the deadlock risk window by quickly 519 // to be here. We can reduce the deadlock risk window by quickly
772 // returning to the SIGPROF handler. However, it is still possible 520 // returning to the SIGPROF handler. However, it is still possible
773 // for VMThread to catch us here or in the SIGPROF handler. If we 521 // for VMThread to catch us here or in the SIGPROF handler. If we
774 // are suspended while holding a resource and another thread blocks 522 // are suspended while holding a resource and another thread blocks
775 // on that resource in the SIGPROF handler, then we will have a 523 // on that resource in the SIGPROF handler, then we will have a
776 // three-thread deadlock (VMThread, this thread, the other thread). 524 // three-thread deadlock (VMThread, this thread, the other thread).
777 trace->num_frames = -10; 525 trace->num_frames = ticks_safepoint; // -10
778 return; 526 return;
779 } 527 }
528 #endif
780 529
781 JavaThread* thread; 530 JavaThread* thread;
782 531
783 if (trace->env_id == NULL || 532 if (trace->env_id == NULL ||
784 (thread = JavaThread::thread_from_jni_environment(trace->env_id)) == NULL || 533 (thread = JavaThread::thread_from_jni_environment(trace->env_id)) == NULL ||
785 thread->is_exiting()) { 534 thread->is_exiting()) {
786 535
787 // bad env_id, thread has exited or thread is exiting 536 // bad env_id, thread has exited or thread is exiting
788 trace->num_frames = -8; 537 trace->num_frames = ticks_thread_exit; // -8
789 return; 538 return;
790 } 539 }
791 540
792 if (thread->in_deopt_handler()) { 541 if (thread->in_deopt_handler()) {
793 // thread is in the deoptimization handler so return no frames 542 // thread is in the deoptimization handler so return no frames
794 trace->num_frames = -9; 543 trace->num_frames = ticks_deopt; // -9
795 return; 544 return;
796 } 545 }
797 546
798 assert(JavaThread::current() == thread, 547 assert(JavaThread::current() == thread,
799 "AsyncGetCallTrace must be called by the current interrupted thread"); 548 "AsyncGetCallTrace must be called by the current interrupted thread");
800 549
801 if (!JvmtiExport::should_post_class_load()) { 550 if (!JvmtiExport::should_post_class_load()) {
802 trace->num_frames = -1; 551 trace->num_frames = ticks_no_class_load; // -1
803 return; 552 return;
804 } 553 }
805 554
806 if (Universe::heap()->is_gc_active()) { 555 if (Universe::heap()->is_gc_active()) {
807 trace->num_frames = -2; 556 trace->num_frames = ticks_GC_active; // -2
808 return; 557 return;
809 } 558 }
810 559
811 switch (thread->thread_state()) { 560 switch (thread->thread_state()) {
812 case _thread_new: 561 case _thread_new:
825 { 574 {
826 frame fr; 575 frame fr;
827 576
828 // param isInJava == false - indicate we aren't in Java code 577 // param isInJava == false - indicate we aren't in Java code
829 if (!thread->pd_get_top_frame_for_signal_handler(&fr, ucontext, false)) { 578 if (!thread->pd_get_top_frame_for_signal_handler(&fr, ucontext, false)) {
579 trace->num_frames = ticks_unknown_not_Java; // -3 unknown frame
580 } else {
830 if (!thread->has_last_Java_frame()) { 581 if (!thread->has_last_Java_frame()) {
831 trace->num_frames = 0; // no Java frames 582 trace->num_frames = 0; // No Java frames
832 } else { 583 } else {
833 trace->num_frames = -3; // unknown frame 584 trace->num_frames = ticks_not_walkable_not_Java; // -4 non walkable frame by default
585 forte_fill_call_trace_given_top(thread, trace, depth, fr);
586
587 // This assert would seem to be valid but it is not.
588 // It would be valid if we weren't possibly racing a gc
589 // thread. A gc thread can make a valid interpreted frame
590 // look invalid. It's a small window but it does happen.
591 // The assert is left here commented out as a reminder.
592 // assert(trace->num_frames != ticks_not_walkable_not_Java, "should always be walkable");
593
834 } 594 }
835 } else {
836 trace->num_frames = -4; // non walkable frame by default
837 forte_fill_call_trace_given_top(thread, trace, depth, fr);
838 } 595 }
839 } 596 }
840 break; 597 break;
841 case _thread_in_Java: 598 case _thread_in_Java:
842 case _thread_in_Java_trans: 599 case _thread_in_Java_trans:
843 { 600 {
844 frame fr; 601 frame fr;
845 602
846 // param isInJava == true - indicate we are in Java code 603 // param isInJava == true - indicate we are in Java code
847 if (!thread->pd_get_top_frame_for_signal_handler(&fr, ucontext, true)) { 604 if (!thread->pd_get_top_frame_for_signal_handler(&fr, ucontext, true)) {
848 trace->num_frames = -5; // unknown frame 605 trace->num_frames = ticks_unknown_Java; // -5 unknown frame
849 } else { 606 } else {
850 trace->num_frames = -6; // non walkable frame by default 607 trace->num_frames = ticks_not_walkable_Java; // -6, non walkable frame by default
851 forte_fill_call_trace_given_top(thread, trace, depth, fr); 608 forte_fill_call_trace_given_top(thread, trace, depth, fr);
852 } 609 }
853 } 610 }
854 break; 611 break;
855 default: 612 default:
856 // Unknown thread state 613 // Unknown thread state
857 trace->num_frames = -7; 614 trace->num_frames = ticks_unknown_state; // -7
858 break; 615 break;
859 } 616 }
860 } 617 }
861 618
862 619