comparison src/share/vm/prims/forte.cpp @ 0:a61af66fc99e jdk7-b24

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children 93b6525e3b82
comparison
equal deleted inserted replaced
-1:000000000000 0:a61af66fc99e
1 /*
2 * Copyright 2003-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25 # include "incls/_precompiled.incl"
26 # include "incls/_forte.cpp.incl"
27
28
29 //-------------------------------------------------------
30
31 // Native interfaces for use by Forte tools.
32
33
34 #ifndef IA64
35
36 class vframeStreamForte : public vframeStreamCommon {
37 public:
38 // constructor that starts with sender of frame fr (top_frame)
39 vframeStreamForte(JavaThread *jt, frame fr, bool stop_at_java_call_stub);
40 void forte_next();
41 };
42
43
44 static void forte_is_walkable_compiled_frame(frame* fr, RegisterMap* map,
45 bool* is_compiled_p, bool* is_walkable_p);
46 static bool forte_is_walkable_interpreted_frame(frame* fr,
47 methodOop* method_p, int* bci_p);
48
49
50 // A Forte specific version of frame:safe_for_sender().
51 static bool forte_safe_for_sender(frame* fr, JavaThread *thread) {
52 bool ret_value = false; // be pessimistic
53
54 #ifdef COMPILER2
55 #if defined(IA32) || defined(AMD64)
56 {
57 // This check is the same as the standard safe_for_sender()
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;
226
227 if (!DebugNonSafepoints_IS_CLEARED) {
228 // decode the top frame fully
229 // (usual case, if JVMTI is enabled)
230 _frame = fr;
231 } else {
232 // skip top frame, as it may not be at safepoint
233 // For AsyncGetCallTrace(), we extracted as much info from the top
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 }
336
337
338 // 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
340 // the grandparent is compiled. However, in this method we do not know
341 // the relationship of the current _frame relative to the top_frame so
342 // we implement a more broad sanity check. When the previous callee is
343 // interpreted and the current sender is compiled, we verify that the
344 // current sender is also walkable. If it is not walkable, then we mark
345 // the current vframeStream as at the end.
346 void vframeStreamForte::forte_next() {
347 // handle frames with inlining
348 if (_mode == compiled_mode &&
349 vframeStreamCommon::fill_in_compiled_inlined_sender()) {
350 return;
351 }
352
353 // handle general case
354
355 int loop_count = 0;
356 int loop_max = MaxJavaStackTraceDepth * 2;
357
358
359 do {
360
361 #if defined(COMPILER1) && defined(SPARC)
362 bool prevIsInterpreted = _frame.is_interpreted_frame();
363 #endif // COMPILER1 && SPARC
364
365 _frame = _frame.sender(&_reg_map);
366
367 if (!forte_safe_for_sender(&_frame, _thread)) {
368 _mode = at_end_mode;
369 return;
370 }
371
372 #if defined(COMPILER1) && defined(SPARC)
373 if (prevIsInterpreted) {
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());
401 }
402
403 // Determine if 'fr' is a walkable, compiled frame.
404 // *is_compiled_p is set to true if the frame is compiled and if it
405 // is, then *is_walkable_p is set to true if it is also walkable.
406 static void forte_is_walkable_compiled_frame(frame* fr, RegisterMap* map,
407 bool* is_compiled_p, bool* is_walkable_p) {
408
409 *is_compiled_p = false;
410 *is_walkable_p = false;
411
412 CodeBlob* cb = CodeCache::find_blob(fr->pc());
413 if (cb != NULL &&
414 cb->is_nmethod() &&
415 ((nmethod*)cb)->is_java_method()) {
416 // frame is compiled and executing a Java method
417 *is_compiled_p = true;
418
419 // Increment PC because the PcDesc we want is associated with
420 // the *end* of the instruction, and pc_desc_near searches
421 // forward to the first matching PC after the probe PC.
422 PcDesc* pc_desc = NULL;
423 if (!DebugNonSafepoints_IS_CLEARED) {
424 // usual case: look for any safepoint near the sampled PC
425 address probe_pc = fr->pc() + 1;
426 pc_desc = ((nmethod*) cb)->pc_desc_near(probe_pc);
427 } else {
428 // reduced functionality: only recognize PCs immediately after calls
429 pc_desc = ((nmethod*) cb)->pc_desc_at(fr->pc());
430 }
431 if (pc_desc != NULL && (pc_desc->scope_decode_offset()
432 == DebugInformationRecorder::serialized_null)) {
433 pc_desc = NULL;
434 }
435 if (pc_desc != NULL) {
436 // it has a PcDesc so the frame is also walkable
437 *is_walkable_p = true;
438 if (!DebugNonSafepoints_IS_CLEARED) {
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 }
451
452 // 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
454 // 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
456 // be found.
457 // Note: this method returns true when a valid Java method is found
458 // even if a valid BCI cannot be found.
459
460 static bool forte_is_walkable_interpreted_frame(frame* fr,
461 methodOop* method_p, int* bci_p) {
462 assert(fr->is_interpreted_frame(), "just checking");
463
464 // top frame is an interpreted frame
465 // check if it is walkable (i.e. valid methodOop and valid bci)
466 if (fr->is_interpreted_frame_valid()) {
467 if (fr->fp() != NULL) {
468 // access address in order not to trigger asserts that
469 // are built in interpreter_frame_method function
470 methodOop method = *fr->interpreter_frame_method_addr();
471 if (Universe::heap()->is_valid_method(method)) {
472 intptr_t bcx = fr->interpreter_frame_bcx();
473 int bci = method->validate_bci_from_bcx(bcx);
474 // note: bci is set to -1 if not a valid bci
475 *method_p = method;
476 *bci_p = bci;
477 return true;
478 }
479 }
480 }
481 return false;
482 }
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 }
614
615
616 // call frame copied from old .h file and renamed
617 typedef struct {
618 jint lineno; // line number in the source file
619 jmethodID method_id; // method executed in this frame
620 } ASGCT_CallFrame;
621
622 // call trace copied from old .h file and renamed
623 typedef struct {
624 JNIEnv *env_id; // Env where trace was recorded
625 jint num_frames; // number of frames in this trace
626 ASGCT_CallFrame *frames; // frames
627 } ASGCT_CallTrace;
628
629 static void forte_fill_call_trace_given_top(JavaThread* thd,
630 ASGCT_CallTrace* trace, int depth, frame top_frame) {
631 NoHandleMark nhm;
632
633 frame walkframe;
634 methodOop method;
635 int bci;
636 int count;
637
638 count = 0;
639 assert(trace->frames != NULL, "trace->frames must be non-NULL");
640
641 if (!forte_is_walkable_frame(thd, &top_frame, &walkframe, &method, &bci)) {
642 // return if no walkable frame is found
643 return;
644 }
645
646 CollectedHeap* ch = Universe::heap();
647
648 if (method != NULL) {
649 // The method is not stored GC safe so see if GC became active
650 // after we entered AsyncGetCallTrace() and before we try to
651 // use the methodOop.
652 // Yes, there is still a window after this check and before
653 // we use methodOop below, but we can't lock out GC so that
654 // has to be an acceptable risk.
655 if (!ch->is_valid_method(method)) {
656 trace->num_frames = -2;
657 return;
658 }
659
660 if (DebugNonSafepoints_IS_CLEARED) {
661 // Take whatever method the top-frame decoder managed to scrape up.
662 // We look further at the top frame only if non-safepoint
663 // debugging information is available.
664 count++;
665 trace->num_frames = count;
666 trace->frames[0].method_id = method->find_jmethod_id_or_null();
667 if (!method->is_native()) {
668 trace->frames[0].lineno = bci;
669 } else {
670 trace->frames[0].lineno = -3;
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++) {
684 bci = st.bci();
685 method = st.method();
686
687 // The method is not stored GC safe so see if GC became active
688 // after we entered AsyncGetCallTrace() and before we try to
689 // use the methodOop.
690 // Yes, there is still a window after this check and before
691 // we use methodOop below, but we can't lock out GC so that
692 // has to be an acceptable risk.
693 if (!ch->is_valid_method(method)) {
694 // we throw away everything we've gathered in this sample since
695 // none of it is safe
696 trace->num_frames = -2;
697 return;
698 }
699
700 trace->frames[count].method_id = method->find_jmethod_id_or_null();
701 if (!method->is_native()) {
702 trace->frames[count].lineno = bci;
703 } else {
704 trace->frames[count].lineno = -3;
705 }
706 }
707 trace->num_frames = count;
708 return;
709 }
710
711
712 // Forte Analyzer AsyncGetCallTrace() entry point. Currently supported
713 // on Linux X86, Solaris SPARC and Solaris X86.
714 //
715 // Async-safe version of GetCallTrace being called from a signal handler
716 // when a LWP gets interrupted by SIGPROF but the stack traces are filled
717 // with different content (see below).
718 //
719 // This function must only be called when JVM/TI
720 // CLASS_LOAD events have been enabled since agent startup. The enabled
721 // event will cause the jmethodIDs to be allocated at class load time.
722 // The jmethodIDs cannot be allocated in a signal handler because locks
723 // cannot be grabbed in a signal handler safely.
724 //
725 // void (*AsyncGetCallTrace)(ASGCT_CallTrace *trace, jint depth, void* ucontext)
726 //
727 // Called by the profiler to obtain the current method call stack trace for
728 // a given thread. The thread is identified by the env_id field in the
729 // ASGCT_CallTrace structure. The profiler agent should allocate a ASGCT_CallTrace
730 // structure with enough memory for the requested stack depth. The VM fills in
731 // the frames buffer and the num_frames field.
732 //
733 // Arguments:
734 //
735 // trace - trace data structure to be filled by the VM.
736 // depth - depth of the call stack trace.
737 // ucontext - ucontext_t of the LWP
738 //
739 // ASGCT_CallTrace:
740 // typedef struct {
741 // JNIEnv *env_id;
742 // jint num_frames;
743 // ASGCT_CallFrame *frames;
744 // } ASGCT_CallTrace;
745 //
746 // Fields:
747 // env_id - ID of thread which executed this trace.
748 // num_frames - number of frames in the trace.
749 // (< 0 indicates the frame is not walkable).
750 // frames - the ASGCT_CallFrames that make up this trace. Callee followed by callers.
751 //
752 // ASGCT_CallFrame:
753 // typedef struct {
754 // jint lineno;
755 // jmethodID method_id;
756 // } ASGCT_CallFrame;
757 //
758 // Fields:
759 // 1) For Java frame (interpreted and compiled),
760 // lineno - bci of the method being executed or -1 if bci is not available
761 // method_id - jmethodID of the method being executed
762 // 2) For native method
763 // lineno - (-3)
764 // method_id - jmethodID of the method being executed
765
766 extern "C" {
767 void AsyncGetCallTrace(ASGCT_CallTrace *trace, jint depth, void* ucontext) {
768 if (SafepointSynchronize::is_synchronizing()) {
769 // The safepoint mechanism is trying to synchronize all the threads.
770 // 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
772 // returning to the SIGPROF handler. However, it is still possible
773 // for VMThread to catch us here or in the SIGPROF handler. If we
774 // are suspended while holding a resource and another thread blocks
775 // on that resource in the SIGPROF handler, then we will have a
776 // three-thread deadlock (VMThread, this thread, the other thread).
777 trace->num_frames = -10;
778 return;
779 }
780
781 JavaThread* thread;
782
783 if (trace->env_id == NULL ||
784 (thread = JavaThread::thread_from_jni_environment(trace->env_id)) == NULL ||
785 thread->is_exiting()) {
786
787 // bad env_id, thread has exited or thread is exiting
788 trace->num_frames = -8;
789 return;
790 }
791
792 if (thread->in_deopt_handler()) {
793 // thread is in the deoptimization handler so return no frames
794 trace->num_frames = -9;
795 return;
796 }
797
798 assert(JavaThread::current() == thread,
799 "AsyncGetCallTrace must be called by the current interrupted thread");
800
801 if (!JvmtiExport::should_post_class_load()) {
802 trace->num_frames = -1;
803 return;
804 }
805
806 if (Universe::heap()->is_gc_active()) {
807 trace->num_frames = -2;
808 return;
809 }
810
811 switch (thread->thread_state()) {
812 case _thread_new:
813 case _thread_uninitialized:
814 case _thread_new_trans:
815 // We found the thread on the threads list above, but it is too
816 // young to be useful so return that there are no Java frames.
817 trace->num_frames = 0;
818 break;
819 case _thread_in_native:
820 case _thread_in_native_trans:
821 case _thread_blocked:
822 case _thread_blocked_trans:
823 case _thread_in_vm:
824 case _thread_in_vm_trans:
825 {
826 frame fr;
827
828 // param isInJava == false - indicate we aren't in Java code
829 if (!thread->pd_get_top_frame_for_signal_handler(&fr, ucontext, false)) {
830 if (!thread->has_last_Java_frame()) {
831 trace->num_frames = 0; // no Java frames
832 } else {
833 trace->num_frames = -3; // unknown frame
834 }
835 } else {
836 trace->num_frames = -4; // non walkable frame by default
837 forte_fill_call_trace_given_top(thread, trace, depth, fr);
838 }
839 }
840 break;
841 case _thread_in_Java:
842 case _thread_in_Java_trans:
843 {
844 frame fr;
845
846 // param isInJava == true - indicate we are in Java code
847 if (!thread->pd_get_top_frame_for_signal_handler(&fr, ucontext, true)) {
848 trace->num_frames = -5; // unknown frame
849 } else {
850 trace->num_frames = -6; // non walkable frame by default
851 forte_fill_call_trace_given_top(thread, trace, depth, fr);
852 }
853 }
854 break;
855 default:
856 // Unknown thread state
857 trace->num_frames = -7;
858 break;
859 }
860 }
861
862
863 #ifndef _WINDOWS
864 // Support for the Forte(TM) Peformance Tools collector.
865 //
866 // The method prototype is derived from libcollector.h. For more
867 // information, please see the libcollect man page.
868
869 // Method to let libcollector know about a dynamically loaded function.
870 // Because it is weakly bound, the calls become NOP's when the library
871 // isn't present.
872 void collector_func_load(char* name,
873 void* null_argument_1,
874 void* null_argument_2,
875 void *vaddr,
876 int size,
877 int zero_argument,
878 void* null_argument_3);
879 #pragma weak collector_func_load
880 #define collector_func_load(x0,x1,x2,x3,x4,x5,x6) \
881 ( collector_func_load ? collector_func_load(x0,x1,x2,x3,x4,x5,x6),0 : 0 )
882 #endif // !_WINDOWS
883
884 } // end extern "C"
885 #endif // !IA64
886
887 void Forte::register_stub(const char* name, address start, address end) {
888 #if !defined(_WINDOWS) && !defined(IA64)
889 assert(pointer_delta(end, start, sizeof(jbyte)) < INT_MAX,
890 "Code size exceeds maximum range")
891
892 collector_func_load((char*)name, NULL, NULL, start,
893 pointer_delta(end, start, sizeof(jbyte)), 0, NULL);
894 #endif // !_WINDOWS && !IA64
895 }