Wed, 28 Nov 2012 17:50:21 -0500
8003635: NPG: AsynchGetCallTrace broken by Method* virtual call
Summary: Make metaspace::contains be lock free and used to see if something is in metaspace, also compare Method* with vtbl pointer.
Reviewed-by: dholmes, sspitsyn, dcubed, jmasa
1 /*
2 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
25 #include "precompiled.hpp"
26 #include "code/debugInfoRec.hpp"
27 #include "code/pcDesc.hpp"
28 #include "gc_interface/collectedHeap.inline.hpp"
29 #include "memory/space.hpp"
30 #include "memory/universe.inline.hpp"
31 #include "oops/oop.inline.hpp"
32 #include "oops/oop.inline2.hpp"
33 #include "prims/forte.hpp"
34 #include "runtime/thread.hpp"
35 #include "runtime/vframe.hpp"
36 #include "runtime/vframeArray.hpp"
38 // These name match the names reported by the forte quality kit
39 enum {
40 ticks_no_Java_frame = 0,
41 ticks_no_class_load = -1,
42 ticks_GC_active = -2,
43 ticks_unknown_not_Java = -3,
44 ticks_not_walkable_not_Java = -4,
45 ticks_unknown_Java = -5,
46 ticks_not_walkable_Java = -6,
47 ticks_unknown_state = -7,
48 ticks_thread_exit = -8,
49 ticks_deopt = -9,
50 ticks_safepoint = -10
51 };
53 //-------------------------------------------------------
55 // Native interfaces for use by Forte tools.
58 #ifndef IA64
60 class vframeStreamForte : public vframeStreamCommon {
61 public:
62 // constructor that starts with sender of frame fr (top_frame)
63 vframeStreamForte(JavaThread *jt, frame fr, bool stop_at_java_call_stub);
64 void forte_next();
65 };
68 static bool is_decipherable_compiled_frame(JavaThread* thread, frame* fr, nmethod* nm);
69 static bool is_decipherable_interpreted_frame(JavaThread* thread,
70 frame* fr,
71 Method** method_p,
72 int* bci_p);
77 vframeStreamForte::vframeStreamForte(JavaThread *jt,
78 frame fr,
79 bool stop_at_java_call_stub) : vframeStreamCommon(jt) {
81 _stop_at_java_call_stub = stop_at_java_call_stub;
82 _frame = fr;
84 // We must always have a valid frame to start filling
86 bool filled_in = fill_from_frame();
88 assert(filled_in, "invariant");
90 }
93 // Solaris SPARC Compiler1 needs an additional check on the grandparent
94 // of the top_frame when the parent of the top_frame is interpreted and
95 // the grandparent is compiled. However, in this method we do not know
96 // the relationship of the current _frame relative to the top_frame so
97 // we implement a more broad sanity check. When the previous callee is
98 // interpreted and the current sender is compiled, we verify that the
99 // current sender is also walkable. If it is not walkable, then we mark
100 // the current vframeStream as at the end.
101 void vframeStreamForte::forte_next() {
102 // handle frames with inlining
103 if (_mode == compiled_mode &&
104 vframeStreamCommon::fill_in_compiled_inlined_sender()) {
105 return;
106 }
108 // handle general case
110 int loop_count = 0;
111 int loop_max = MaxJavaStackTraceDepth * 2;
114 do {
116 loop_count++;
118 // By the time we get here we should never see unsafe but better
119 // safe then segv'd
121 if (loop_count > loop_max || !_frame.safe_for_sender(_thread)) {
122 _mode = at_end_mode;
123 return;
124 }
126 _frame = _frame.sender(&_reg_map);
128 } while (!fill_from_frame());
129 }
131 // Determine if 'fr' is a decipherable compiled frame. We are already
132 // assured that fr is for a java nmethod.
134 static bool is_decipherable_compiled_frame(JavaThread* thread, frame* fr, nmethod* nm) {
135 assert(nm->is_java_method(), "invariant");
137 if (thread->has_last_Java_frame() && thread->last_Java_pc() == fr->pc()) {
138 // We're stopped at a call into the JVM so look for a PcDesc with
139 // the actual pc reported by the frame.
140 PcDesc* pc_desc = nm->pc_desc_at(fr->pc());
142 // Did we find a useful PcDesc?
143 if (pc_desc != NULL &&
144 pc_desc->scope_decode_offset() != DebugInformationRecorder::serialized_null) {
145 return true;
146 }
147 }
149 // We're at some random pc in the nmethod so search for the PcDesc
150 // whose pc is greater than the current PC. It's done this way
151 // because the extra PcDescs that are recorded for improved debug
152 // info record the end of the region covered by the ScopeDesc
153 // instead of the beginning.
154 PcDesc* pc_desc = nm->pc_desc_near(fr->pc() + 1);
156 // Now do we have a useful PcDesc?
157 if (pc_desc == NULL ||
158 pc_desc->scope_decode_offset() == DebugInformationRecorder::serialized_null) {
159 // No debug information available for this pc
160 // vframeStream would explode if we try and walk the frames.
161 return false;
162 }
164 // This PcDesc is useful however we must adjust the frame's pc
165 // so that the vframeStream lookups will use this same pc
166 fr->set_pc(pc_desc->real_pc(nm));
167 return true;
168 }
171 // Determine if 'fr' is a walkable interpreted frame. Returns false
172 // if it is not. *method_p, and *bci_p are not set when false is
173 // returned. *method_p is non-NULL if frame was executing a Java
174 // method. *bci_p is != -1 if a valid BCI in the Java method could
175 // be found.
176 // Note: this method returns true when a valid Java method is found
177 // even if a valid BCI cannot be found.
179 static bool is_decipherable_interpreted_frame(JavaThread* thread,
180 frame* fr,
181 Method** method_p,
182 int* bci_p) {
183 assert(fr->is_interpreted_frame(), "just checking");
185 // top frame is an interpreted frame
186 // check if it is walkable (i.e. valid Method* and valid bci)
188 // Because we may be racing a gc thread the method and/or bci
189 // of a valid interpreter frame may look bad causing us to
190 // fail the is_interpreted_frame_valid test. If the thread
191 // is in any of the following states we are assured that the
192 // frame is in fact valid and we must have hit the race.
194 JavaThreadState state = thread->thread_state();
195 bool known_valid = (state == _thread_in_native ||
196 state == _thread_in_vm ||
197 state == _thread_blocked );
199 if (known_valid || fr->is_interpreted_frame_valid(thread)) {
201 // The frame code should completely validate the frame so that
202 // references to Method* and bci are completely safe to access
203 // If they aren't the frame code should be fixed not this
204 // code. However since gc isn't locked out the values could be
205 // stale. This is a race we can never completely win since we can't
206 // lock out gc so do one last check after retrieving their values
207 // from the frame for additional safety
209 Method* method = fr->interpreter_frame_method();
211 // We've at least found a method.
212 // NOTE: there is something to be said for the approach that
213 // if we don't find a valid bci then the method is not likely
214 // a valid method. Then again we may have caught an interpreter
215 // frame in the middle of construction and the bci field is
216 // not yet valid.
218 *method_p = method;
219 if (!method->is_valid_method()) return false;
221 intptr_t bcx = fr->interpreter_frame_bcx();
223 int bci = method->validate_bci_from_bcx(bcx);
225 // note: bci is set to -1 if not a valid bci
226 *bci_p = bci;
227 return true;
228 }
230 return false;
231 }
234 // Determine if 'fr' can be used to find an initial Java frame.
235 // Return false if it can not find a fully decipherable Java frame
236 // (in other words a frame that isn't safe to use in a vframe stream).
237 // Obviously if it can't even find a Java frame false will also be returned.
238 //
239 // If we find a Java frame decipherable or not then by definition we have
240 // identified a method and that will be returned to the caller via method_p.
241 // If we can determine a bci that is returned also. (Hmm is it possible
242 // to return a method and bci and still return false? )
243 //
244 // The initial Java frame we find (if any) is return via initial_frame_p.
245 //
247 static bool find_initial_Java_frame(JavaThread* thread,
248 frame* fr,
249 frame* initial_frame_p,
250 Method** method_p,
251 int* bci_p) {
253 // It is possible that for a frame containing an nmethod
254 // we can capture the method but no bci. If we get no
255 // bci the frame isn't walkable but the method is usable.
256 // Therefore we init the returned Method* to NULL so the
257 // caller can make the distinction.
259 *method_p = NULL;
261 // On the initial call to this method the frame we get may not be
262 // recognizable to us. This should only happen if we are in a JRT_LEAF
263 // or something called by a JRT_LEAF method.
267 frame candidate = *fr;
269 // If the starting frame we were given has no codeBlob associated with
270 // it see if we can find such a frame because only frames with codeBlobs
271 // are possible Java frames.
273 if (fr->cb() == NULL) {
275 // See if we can find a useful frame
276 int loop_count;
277 int loop_max = MaxJavaStackTraceDepth * 2;
278 RegisterMap map(thread, false);
280 for (loop_count = 0; loop_count < loop_max; loop_count++) {
281 if (!candidate.safe_for_sender(thread)) return false;
282 candidate = candidate.sender(&map);
283 if (candidate.cb() != NULL) break;
284 }
285 if (candidate.cb() == NULL) return false;
286 }
288 // We have a frame known to be in the codeCache
289 // We will hopefully be able to figure out something to do with it.
290 int loop_count;
291 int loop_max = MaxJavaStackTraceDepth * 2;
292 RegisterMap map(thread, false);
294 for (loop_count = 0; loop_count < loop_max; loop_count++) {
296 if (candidate.is_first_frame()) {
297 // If initial frame is frame from StubGenerator and there is no
298 // previous anchor, there are no java frames associated with a method
299 return false;
300 }
302 if (candidate.is_interpreted_frame()) {
303 if (is_decipherable_interpreted_frame(thread, &candidate, method_p, bci_p)) {
304 *initial_frame_p = candidate;
305 return true;
306 }
308 // Hopefully we got some data
309 return false;
310 }
312 if (candidate.cb()->is_nmethod()) {
314 nmethod* nm = (nmethod*) candidate.cb();
315 *method_p = nm->method();
317 // If the frame isn't fully decipherable then the default
318 // value for the bci is a signal that we don't have a bci.
319 // If we have a decipherable frame this bci value will
320 // not be used.
322 *bci_p = -1;
324 *initial_frame_p = candidate;
326 // Native wrapper code is trivial to decode by vframeStream
328 if (nm->is_native_method()) return true;
330 // If it isn't decipherable then we have found a pc that doesn't
331 // have a PCDesc that can get us a bci however we did find
332 // a method
334 if (!is_decipherable_compiled_frame(thread, &candidate, nm)) {
335 return false;
336 }
338 // is_decipherable_compiled_frame may modify candidate's pc
339 *initial_frame_p = candidate;
341 assert(nm->pc_desc_at(candidate.pc()) != NULL, "if it's decipherable then pc must be valid");
343 return true;
344 }
346 // Must be some stub frame that we don't care about
348 if (!candidate.safe_for_sender(thread)) return false;
349 candidate = candidate.sender(&map);
351 // If it isn't in the code cache something is wrong
352 // since once we find a frame in the code cache they
353 // all should be there.
355 if (candidate.cb() == NULL) return false;
357 }
359 return false;
361 }
364 // call frame copied from old .h file and renamed
365 typedef struct {
366 jint lineno; // line number in the source file
367 jmethodID method_id; // method executed in this frame
368 } ASGCT_CallFrame;
370 // call trace copied from old .h file and renamed
371 typedef struct {
372 JNIEnv *env_id; // Env where trace was recorded
373 jint num_frames; // number of frames in this trace
374 ASGCT_CallFrame *frames; // frames
375 } ASGCT_CallTrace;
377 static void forte_fill_call_trace_given_top(JavaThread* thd,
378 ASGCT_CallTrace* trace,
379 int depth,
380 frame top_frame) {
381 NoHandleMark nhm;
383 frame initial_Java_frame;
384 Method* method;
385 int bci;
386 int count;
388 count = 0;
389 assert(trace->frames != NULL, "trace->frames must be non-NULL");
391 bool fully_decipherable = find_initial_Java_frame(thd, &top_frame, &initial_Java_frame, &method, &bci);
393 // The frame might not be walkable but still recovered a method
394 // (e.g. an nmethod with no scope info for the pc)
396 if (method == NULL) return;
398 if (!method->is_valid_method()) {
399 trace->num_frames = ticks_GC_active; // -2
400 return;
401 }
403 // We got a Java frame however it isn't fully decipherable
404 // so it won't necessarily be safe to use it for the
405 // initial frame in the vframe stream.
407 if (!fully_decipherable) {
408 // Take whatever method the top-frame decoder managed to scrape up.
409 // We look further at the top frame only if non-safepoint
410 // debugging information is available.
411 count++;
412 trace->num_frames = count;
413 trace->frames[0].method_id = method->find_jmethod_id_or_null();
414 if (!method->is_native()) {
415 trace->frames[0].lineno = bci;
416 } else {
417 trace->frames[0].lineno = -3;
418 }
420 if (!initial_Java_frame.safe_for_sender(thd)) return;
422 RegisterMap map(thd, false);
423 initial_Java_frame = initial_Java_frame.sender(&map);
424 }
426 vframeStreamForte st(thd, initial_Java_frame, false);
428 for (; !st.at_end() && count < depth; st.forte_next(), count++) {
429 bci = st.bci();
430 method = st.method();
432 if (!method->is_valid_method()) {
433 // we throw away everything we've gathered in this sample since
434 // none of it is safe
435 trace->num_frames = ticks_GC_active; // -2
436 return;
437 }
439 trace->frames[count].method_id = method->find_jmethod_id_or_null();
440 if (!method->is_native()) {
441 trace->frames[count].lineno = bci;
442 } else {
443 trace->frames[count].lineno = -3;
444 }
445 }
446 trace->num_frames = count;
447 return;
448 }
451 // Forte Analyzer AsyncGetCallTrace() entry point. Currently supported
452 // on Linux X86, Solaris SPARC and Solaris X86.
453 //
454 // Async-safe version of GetCallTrace being called from a signal handler
455 // when a LWP gets interrupted by SIGPROF but the stack traces are filled
456 // with different content (see below).
457 //
458 // This function must only be called when JVM/TI
459 // CLASS_LOAD events have been enabled since agent startup. The enabled
460 // event will cause the jmethodIDs to be allocated at class load time.
461 // The jmethodIDs cannot be allocated in a signal handler because locks
462 // cannot be grabbed in a signal handler safely.
463 //
464 // void (*AsyncGetCallTrace)(ASGCT_CallTrace *trace, jint depth, void* ucontext)
465 //
466 // Called by the profiler to obtain the current method call stack trace for
467 // a given thread. The thread is identified by the env_id field in the
468 // ASGCT_CallTrace structure. The profiler agent should allocate a ASGCT_CallTrace
469 // structure with enough memory for the requested stack depth. The VM fills in
470 // the frames buffer and the num_frames field.
471 //
472 // Arguments:
473 //
474 // trace - trace data structure to be filled by the VM.
475 // depth - depth of the call stack trace.
476 // ucontext - ucontext_t of the LWP
477 //
478 // ASGCT_CallTrace:
479 // typedef struct {
480 // JNIEnv *env_id;
481 // jint num_frames;
482 // ASGCT_CallFrame *frames;
483 // } ASGCT_CallTrace;
484 //
485 // Fields:
486 // env_id - ID of thread which executed this trace.
487 // num_frames - number of frames in the trace.
488 // (< 0 indicates the frame is not walkable).
489 // frames - the ASGCT_CallFrames that make up this trace. Callee followed by callers.
490 //
491 // ASGCT_CallFrame:
492 // typedef struct {
493 // jint lineno;
494 // jmethodID method_id;
495 // } ASGCT_CallFrame;
496 //
497 // Fields:
498 // 1) For Java frame (interpreted and compiled),
499 // lineno - bci of the method being executed or -1 if bci is not available
500 // method_id - jmethodID of the method being executed
501 // 2) For native method
502 // lineno - (-3)
503 // method_id - jmethodID of the method being executed
505 extern "C" {
506 JNIEXPORT
507 void AsyncGetCallTrace(ASGCT_CallTrace *trace, jint depth, void* ucontext) {
508 JavaThread* thread;
510 if (trace->env_id == NULL ||
511 (thread = JavaThread::thread_from_jni_environment(trace->env_id)) == NULL ||
512 thread->is_exiting()) {
514 // bad env_id, thread has exited or thread is exiting
515 trace->num_frames = ticks_thread_exit; // -8
516 return;
517 }
519 if (thread->in_deopt_handler()) {
520 // thread is in the deoptimization handler so return no frames
521 trace->num_frames = ticks_deopt; // -9
522 return;
523 }
525 assert(JavaThread::current() == thread,
526 "AsyncGetCallTrace must be called by the current interrupted thread");
528 if (!JvmtiExport::should_post_class_load()) {
529 trace->num_frames = ticks_no_class_load; // -1
530 return;
531 }
533 if (Universe::heap()->is_gc_active()) {
534 trace->num_frames = ticks_GC_active; // -2
535 return;
536 }
538 switch (thread->thread_state()) {
539 case _thread_new:
540 case _thread_uninitialized:
541 case _thread_new_trans:
542 // We found the thread on the threads list above, but it is too
543 // young to be useful so return that there are no Java frames.
544 trace->num_frames = 0;
545 break;
546 case _thread_in_native:
547 case _thread_in_native_trans:
548 case _thread_blocked:
549 case _thread_blocked_trans:
550 case _thread_in_vm:
551 case _thread_in_vm_trans:
552 {
553 frame fr;
555 // param isInJava == false - indicate we aren't in Java code
556 if (!thread->pd_get_top_frame_for_signal_handler(&fr, ucontext, false)) {
557 trace->num_frames = ticks_unknown_not_Java; // -3 unknown frame
558 } else {
559 if (!thread->has_last_Java_frame()) {
560 trace->num_frames = 0; // No Java frames
561 } else {
562 trace->num_frames = ticks_not_walkable_not_Java; // -4 non walkable frame by default
563 forte_fill_call_trace_given_top(thread, trace, depth, fr);
565 // This assert would seem to be valid but it is not.
566 // It would be valid if we weren't possibly racing a gc
567 // thread. A gc thread can make a valid interpreted frame
568 // look invalid. It's a small window but it does happen.
569 // The assert is left here commented out as a reminder.
570 // assert(trace->num_frames != ticks_not_walkable_not_Java, "should always be walkable");
572 }
573 }
574 }
575 break;
576 case _thread_in_Java:
577 case _thread_in_Java_trans:
578 {
579 frame fr;
581 // param isInJava == true - indicate we are in Java code
582 if (!thread->pd_get_top_frame_for_signal_handler(&fr, ucontext, true)) {
583 trace->num_frames = ticks_unknown_Java; // -5 unknown frame
584 } else {
585 trace->num_frames = ticks_not_walkable_Java; // -6, non walkable frame by default
586 forte_fill_call_trace_given_top(thread, trace, depth, fr);
587 }
588 }
589 break;
590 default:
591 // Unknown thread state
592 trace->num_frames = ticks_unknown_state; // -7
593 break;
594 }
595 }
598 #ifndef _WINDOWS
599 // Support for the Forte(TM) Peformance Tools collector.
600 //
601 // The method prototype is derived from libcollector.h. For more
602 // information, please see the libcollect man page.
604 // Method to let libcollector know about a dynamically loaded function.
605 // Because it is weakly bound, the calls become NOP's when the library
606 // isn't present.
607 #ifdef __APPLE__
608 // XXXDARWIN: Link errors occur even when __attribute__((weak_import))
609 // is added
610 #define collector_func_load(x0,x1,x2,x3,x4,x5,x6) (0)
611 #else
612 void collector_func_load(char* name,
613 void* null_argument_1,
614 void* null_argument_2,
615 void *vaddr,
616 int size,
617 int zero_argument,
618 void* null_argument_3);
619 #pragma weak collector_func_load
620 #define collector_func_load(x0,x1,x2,x3,x4,x5,x6) \
621 ( collector_func_load ? collector_func_load(x0,x1,x2,x3,x4,x5,x6),0 : 0 )
622 #endif // __APPLE__
623 #endif // !_WINDOWS
625 } // end extern "C"
626 #endif // !IA64
628 void Forte::register_stub(const char* name, address start, address end) {
629 #if !defined(_WINDOWS) && !defined(IA64)
630 assert(pointer_delta(end, start, sizeof(jbyte)) < INT_MAX,
631 "Code size exceeds maximum range");
633 collector_func_load((char*)name, NULL, NULL, start,
634 pointer_delta(end, start, sizeof(jbyte)), 0, NULL);
635 #endif // !_WINDOWS && !IA64
636 }