forte.cpp revision 1472:c18cbe5936b8
1/* 2 * Copyright (c) 2003, 2008, 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 */ 24 25# include "incls/_precompiled.incl" 26# include "incls/_forte.cpp.incl" 27 28// These name match the names reported by the forte quality kit 29enum { 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}; 42 43//------------------------------------------------------- 44 45// Native interfaces for use by Forte tools. 46 47 48#ifndef IA64 49 50class vframeStreamForte : public vframeStreamCommon { 51 public: 52 // constructor that starts with sender of frame fr (top_frame) 53 vframeStreamForte(JavaThread *jt, frame fr, bool stop_at_java_call_stub); 54 void forte_next(); 55}; 56 57 58static bool is_decipherable_compiled_frame(JavaThread* thread, frame* fr, nmethod* nm); 59static bool is_decipherable_interpreted_frame(JavaThread* thread, 60 frame* fr, 61 methodOop* method_p, 62 int* bci_p); 63 64 65 66 67vframeStreamForte::vframeStreamForte(JavaThread *jt, 68 frame fr, 69 bool stop_at_java_call_stub) : vframeStreamCommon(jt) { 70 71 _stop_at_java_call_stub = stop_at_java_call_stub; 72 _frame = fr; 73 74 // We must always have a valid frame to start filling 75 76 bool filled_in = fill_from_frame(); 77 78 assert(filled_in, "invariant"); 79 80} 81 82 83// Solaris SPARC Compiler1 needs an additional check on the grandparent 84// of the top_frame when the parent of the top_frame is interpreted and 85// the grandparent is compiled. However, in this method we do not know 86// the relationship of the current _frame relative to the top_frame so 87// we implement a more broad sanity check. When the previous callee is 88// interpreted and the current sender is compiled, we verify that the 89// current sender is also walkable. If it is not walkable, then we mark 90// the current vframeStream as at the end. 91void vframeStreamForte::forte_next() { 92 // handle frames with inlining 93 if (_mode == compiled_mode && 94 vframeStreamCommon::fill_in_compiled_inlined_sender()) { 95 return; 96 } 97 98 // handle general case 99 100 int loop_count = 0; 101 int loop_max = MaxJavaStackTraceDepth * 2; 102 103 104 do { 105 106 loop_count++; 107 108 // By the time we get here we should never see unsafe but better 109 // safe then segv'd 110 111 if (loop_count > loop_max || !_frame.safe_for_sender(_thread)) { 112 _mode = at_end_mode; 113 return; 114 } 115 116 _frame = _frame.sender(&_reg_map); 117 118 } while (!fill_from_frame()); 119} 120 121// Determine if 'fr' is a decipherable compiled frame. We are already 122// assured that fr is for a java nmethod. 123 124static bool is_decipherable_compiled_frame(JavaThread* thread, frame* fr, nmethod* nm) { 125 assert(nm->is_java_method(), "invariant"); 126 127 if (thread->has_last_Java_frame() && thread->last_Java_pc() == fr->pc()) { 128 // We're stopped at a call into the JVM so look for a PcDesc with 129 // the actual pc reported by the frame. 130 PcDesc* pc_desc = nm->pc_desc_at(fr->pc()); 131 132 // Did we find a useful PcDesc? 133 if (pc_desc != NULL && 134 pc_desc->scope_decode_offset() != DebugInformationRecorder::serialized_null) { 135 return true; 136 } 137 } 138 139 // We're at some random pc in the nmethod so search for the PcDesc 140 // whose pc is greater than the current PC. It's done this way 141 // because the extra PcDescs that are recorded for improved debug 142 // info record the end of the region covered by the ScopeDesc 143 // instead of the beginning. 144 PcDesc* pc_desc = nm->pc_desc_near(fr->pc() + 1); 145 146 // Now do we have a useful PcDesc? 147 if (pc_desc == NULL || 148 pc_desc->scope_decode_offset() == DebugInformationRecorder::serialized_null) { 149 // No debug information available for this pc 150 // vframeStream would explode if we try and walk the frames. 151 return false; 152 } 153 154 // This PcDesc is useful however we must adjust the frame's pc 155 // so that the vframeStream lookups will use this same pc 156 fr->set_pc(pc_desc->real_pc(nm)); 157 return true; 158} 159 160 161// Determine if 'fr' is a walkable interpreted frame. Returns false 162// if it is not. *method_p, and *bci_p are not set when false is 163// returned. *method_p is non-NULL if frame was executing a Java 164// method. *bci_p is != -1 if a valid BCI in the Java method could 165// be found. 166// Note: this method returns true when a valid Java method is found 167// even if a valid BCI cannot be found. 168 169static bool is_decipherable_interpreted_frame(JavaThread* thread, 170 frame* fr, 171 methodOop* method_p, 172 int* bci_p) { 173 assert(fr->is_interpreted_frame(), "just checking"); 174 175 // top frame is an interpreted frame 176 // check if it is walkable (i.e. valid methodOop and valid bci) 177 178 // Because we may be racing a gc thread the method and/or bci 179 // of a valid interpreter frame may look bad causing us to 180 // fail the is_interpreted_frame_valid test. If the thread 181 // is in any of the following states we are assured that the 182 // frame is in fact valid and we must have hit the race. 183 184 JavaThreadState state = thread->thread_state(); 185 bool known_valid = (state == _thread_in_native || 186 state == _thread_in_vm || 187 state == _thread_blocked ); 188 189 if (known_valid || fr->is_interpreted_frame_valid(thread)) { 190 191 // The frame code should completely validate the frame so that 192 // references to methodOop and bci are completely safe to access 193 // If they aren't the frame code should be fixed not this 194 // code. However since gc isn't locked out the values could be 195 // stale. This is a race we can never completely win since we can't 196 // lock out gc so do one last check after retrieving their values 197 // from the frame for additional safety 198 199 methodOop method = fr->interpreter_frame_method(); 200 201 // We've at least found a method. 202 // NOTE: there is something to be said for the approach that 203 // if we don't find a valid bci then the method is not likely 204 // a valid method. Then again we may have caught an interpreter 205 // frame in the middle of construction and the bci field is 206 // not yet valid. 207 208 *method_p = method; 209 210 // See if gc may have invalidated method since we validated frame 211 212 if (!Universe::heap()->is_valid_method(method)) return false; 213 214 intptr_t bcx = fr->interpreter_frame_bcx(); 215 216 int bci = method->validate_bci_from_bcx(bcx); 217 218 // note: bci is set to -1 if not a valid bci 219 *bci_p = bci; 220 return true; 221 } 222 223 return false; 224} 225 226 227// Determine if 'fr' can be used to find an initial Java frame. 228// Return false if it can not find a fully decipherable Java frame 229// (in other words a frame that isn't safe to use in a vframe stream). 230// Obviously if it can't even find a Java frame false will also be returned. 231// 232// If we find a Java frame decipherable or not then by definition we have 233// identified a method and that will be returned to the caller via method_p. 234// If we can determine a bci that is returned also. (Hmm is it possible 235// to return a method and bci and still return false? ) 236// 237// The initial Java frame we find (if any) is return via initial_frame_p. 238// 239 240static bool find_initial_Java_frame(JavaThread* thread, 241 frame* fr, 242 frame* initial_frame_p, 243 methodOop* method_p, 244 int* bci_p) { 245 246 // It is possible that for a frame containing an nmethod 247 // we can capture the method but no bci. If we get no 248 // bci the frame isn't walkable but the method is usable. 249 // Therefore we init the returned methodOop to NULL so the 250 // caller can make the distinction. 251 252 *method_p = NULL; 253 254 // On the initial call to this method the frame we get may not be 255 // recognizable to us. This should only happen if we are in a JRT_LEAF 256 // or something called by a JRT_LEAF method. 257 258 259 260 frame candidate = *fr; 261 262 // If the starting frame we were given has no codeBlob associated with 263 // it see if we can find such a frame because only frames with codeBlobs 264 // are possible Java frames. 265 266 if (fr->cb() == NULL) { 267 268 // See if we can find a useful frame 269 int loop_count; 270 int loop_max = MaxJavaStackTraceDepth * 2; 271 RegisterMap map(thread, false); 272 273 for (loop_count = 0; loop_count < loop_max; loop_count++) { 274 if (!candidate.safe_for_sender(thread)) return false; 275 candidate = candidate.sender(&map); 276 if (candidate.cb() != NULL) break; 277 } 278 if (candidate.cb() == NULL) return false; 279 } 280 281 // We have a frame known to be in the codeCache 282 // We will hopefully be able to figure out something to do with it. 283 int loop_count; 284 int loop_max = MaxJavaStackTraceDepth * 2; 285 RegisterMap map(thread, false); 286 287 for (loop_count = 0; loop_count < loop_max; loop_count++) { 288 289 if (candidate.is_first_frame()) { 290 // If initial frame is frame from StubGenerator and there is no 291 // previous anchor, there are no java frames associated with a method 292 return false; 293 } 294 295 if (candidate.is_interpreted_frame()) { 296 if (is_decipherable_interpreted_frame(thread, &candidate, method_p, bci_p)) { 297 *initial_frame_p = candidate; 298 return true; 299 } 300 301 // Hopefully we got some data 302 return false; 303 } 304 305 if (candidate.cb()->is_nmethod()) { 306 307 nmethod* nm = (nmethod*) candidate.cb(); 308 *method_p = nm->method(); 309 310 // If the frame isn't fully decipherable then the default 311 // value for the bci is a signal that we don't have a bci. 312 // If we have a decipherable frame this bci value will 313 // not be used. 314 315 *bci_p = -1; 316 317 *initial_frame_p = candidate; 318 319 // Native wrapper code is trivial to decode by vframeStream 320 321 if (nm->is_native_method()) return true; 322 323 // If it isn't decipherable then we have found a pc that doesn't 324 // have a PCDesc that can get us a bci however we did find 325 // a method 326 327 if (!is_decipherable_compiled_frame(thread, &candidate, nm)) { 328 return false; 329 } 330 331 // is_decipherable_compiled_frame may modify candidate's pc 332 *initial_frame_p = candidate; 333 334 assert(nm->pc_desc_at(candidate.pc()) != NULL, "if it's decipherable then pc must be valid"); 335 336 return true; 337 } 338 339 // Must be some stub frame that we don't care about 340 341 if (!candidate.safe_for_sender(thread)) return false; 342 candidate = candidate.sender(&map); 343 344 // If it isn't in the code cache something is wrong 345 // since once we find a frame in the code cache they 346 // all should be there. 347 348 if (candidate.cb() == NULL) return false; 349 350 } 351 352 return false; 353 354} 355 356 357// call frame copied from old .h file and renamed 358typedef struct { 359 jint lineno; // line number in the source file 360 jmethodID method_id; // method executed in this frame 361} ASGCT_CallFrame; 362 363// call trace copied from old .h file and renamed 364typedef struct { 365 JNIEnv *env_id; // Env where trace was recorded 366 jint num_frames; // number of frames in this trace 367 ASGCT_CallFrame *frames; // frames 368} ASGCT_CallTrace; 369 370static void forte_fill_call_trace_given_top(JavaThread* thd, 371 ASGCT_CallTrace* trace, 372 int depth, 373 frame top_frame) { 374 NoHandleMark nhm; 375 376 frame initial_Java_frame; 377 methodOop method; 378 int bci; 379 int count; 380 381 count = 0; 382 assert(trace->frames != NULL, "trace->frames must be non-NULL"); 383 384 bool fully_decipherable = find_initial_Java_frame(thd, &top_frame, &initial_Java_frame, &method, &bci); 385 386 // The frame might not be walkable but still recovered a method 387 // (e.g. an nmethod with no scope info for the pc 388 389 if (method == NULL) return; 390 391 CollectedHeap* ch = Universe::heap(); 392 393 // The method is not stored GC safe so see if GC became active 394 // after we entered AsyncGetCallTrace() and before we try to 395 // use the methodOop. 396 // Yes, there is still a window after this check and before 397 // we use methodOop below, but we can't lock out GC so that 398 // has to be an acceptable risk. 399 if (!ch->is_valid_method(method)) { 400 trace->num_frames = ticks_GC_active; // -2 401 return; 402 } 403 404 // We got a Java frame however it isn't fully decipherable 405 // so it won't necessarily be safe to use it for the 406 // initial frame in the vframe stream. 407 408 if (!fully_decipherable) { 409 // Take whatever method the top-frame decoder managed to scrape up. 410 // We look further at the top frame only if non-safepoint 411 // debugging information is available. 412 count++; 413 trace->num_frames = count; 414 trace->frames[0].method_id = method->find_jmethod_id_or_null(); 415 if (!method->is_native()) { 416 trace->frames[0].lineno = bci; 417 } else { 418 trace->frames[0].lineno = -3; 419 } 420 421 if (!initial_Java_frame.safe_for_sender(thd)) return; 422 423 RegisterMap map(thd, false); 424 initial_Java_frame = initial_Java_frame.sender(&map); 425 } 426 427 vframeStreamForte st(thd, initial_Java_frame, false); 428 429 for (; !st.at_end() && count < depth; st.forte_next(), count++) { 430 bci = st.bci(); 431 method = st.method(); 432 433 // The method is not stored GC safe so see if GC became active 434 // after we entered AsyncGetCallTrace() and before we try to 435 // use the methodOop. 436 // Yes, there is still a window after this check and before 437 // we use methodOop below, but we can't lock out GC so that 438 // has to be an acceptable risk. 439 if (!ch->is_valid_method(method)) { 440 // we throw away everything we've gathered in this sample since 441 // none of it is safe 442 trace->num_frames = ticks_GC_active; // -2 443 return; 444 } 445 446 trace->frames[count].method_id = method->find_jmethod_id_or_null(); 447 if (!method->is_native()) { 448 trace->frames[count].lineno = bci; 449 } else { 450 trace->frames[count].lineno = -3; 451 } 452 } 453 trace->num_frames = count; 454 return; 455} 456 457 458// Forte Analyzer AsyncGetCallTrace() entry point. Currently supported 459// on Linux X86, Solaris SPARC and Solaris X86. 460// 461// Async-safe version of GetCallTrace being called from a signal handler 462// when a LWP gets interrupted by SIGPROF but the stack traces are filled 463// with different content (see below). 464// 465// This function must only be called when JVM/TI 466// CLASS_LOAD events have been enabled since agent startup. The enabled 467// event will cause the jmethodIDs to be allocated at class load time. 468// The jmethodIDs cannot be allocated in a signal handler because locks 469// cannot be grabbed in a signal handler safely. 470// 471// void (*AsyncGetCallTrace)(ASGCT_CallTrace *trace, jint depth, void* ucontext) 472// 473// Called by the profiler to obtain the current method call stack trace for 474// a given thread. The thread is identified by the env_id field in the 475// ASGCT_CallTrace structure. The profiler agent should allocate a ASGCT_CallTrace 476// structure with enough memory for the requested stack depth. The VM fills in 477// the frames buffer and the num_frames field. 478// 479// Arguments: 480// 481// trace - trace data structure to be filled by the VM. 482// depth - depth of the call stack trace. 483// ucontext - ucontext_t of the LWP 484// 485// ASGCT_CallTrace: 486// typedef struct { 487// JNIEnv *env_id; 488// jint num_frames; 489// ASGCT_CallFrame *frames; 490// } ASGCT_CallTrace; 491// 492// Fields: 493// env_id - ID of thread which executed this trace. 494// num_frames - number of frames in the trace. 495// (< 0 indicates the frame is not walkable). 496// frames - the ASGCT_CallFrames that make up this trace. Callee followed by callers. 497// 498// ASGCT_CallFrame: 499// typedef struct { 500// jint lineno; 501// jmethodID method_id; 502// } ASGCT_CallFrame; 503// 504// Fields: 505// 1) For Java frame (interpreted and compiled), 506// lineno - bci of the method being executed or -1 if bci is not available 507// method_id - jmethodID of the method being executed 508// 2) For native method 509// lineno - (-3) 510// method_id - jmethodID of the method being executed 511 512extern "C" { 513void AsyncGetCallTrace(ASGCT_CallTrace *trace, jint depth, void* ucontext) { 514 515// This is if'd out because we no longer use thread suspension. 516// However if someone wanted to backport this to a 5.0 jvm then this 517// code would be important. 518#if 0 519 if (SafepointSynchronize::is_synchronizing()) { 520 // The safepoint mechanism is trying to synchronize all the threads. 521 // Since this can involve thread suspension, it is not safe for us 522 // to be here. We can reduce the deadlock risk window by quickly 523 // returning to the SIGPROF handler. However, it is still possible 524 // for VMThread to catch us here or in the SIGPROF handler. If we 525 // are suspended while holding a resource and another thread blocks 526 // on that resource in the SIGPROF handler, then we will have a 527 // three-thread deadlock (VMThread, this thread, the other thread). 528 trace->num_frames = ticks_safepoint; // -10 529 return; 530 } 531#endif 532 533 JavaThread* thread; 534 535 if (trace->env_id == NULL || 536 (thread = JavaThread::thread_from_jni_environment(trace->env_id)) == NULL || 537 thread->is_exiting()) { 538 539 // bad env_id, thread has exited or thread is exiting 540 trace->num_frames = ticks_thread_exit; // -8 541 return; 542 } 543 544 if (thread->in_deopt_handler()) { 545 // thread is in the deoptimization handler so return no frames 546 trace->num_frames = ticks_deopt; // -9 547 return; 548 } 549 550 assert(JavaThread::current() == thread, 551 "AsyncGetCallTrace must be called by the current interrupted thread"); 552 553 if (!JvmtiExport::should_post_class_load()) { 554 trace->num_frames = ticks_no_class_load; // -1 555 return; 556 } 557 558 if (Universe::heap()->is_gc_active()) { 559 trace->num_frames = ticks_GC_active; // -2 560 return; 561 } 562 563 switch (thread->thread_state()) { 564 case _thread_new: 565 case _thread_uninitialized: 566 case _thread_new_trans: 567 // We found the thread on the threads list above, but it is too 568 // young to be useful so return that there are no Java frames. 569 trace->num_frames = 0; 570 break; 571 case _thread_in_native: 572 case _thread_in_native_trans: 573 case _thread_blocked: 574 case _thread_blocked_trans: 575 case _thread_in_vm: 576 case _thread_in_vm_trans: 577 { 578 frame fr; 579 580 // param isInJava == false - indicate we aren't in Java code 581 if (!thread->pd_get_top_frame_for_signal_handler(&fr, ucontext, false)) { 582 trace->num_frames = ticks_unknown_not_Java; // -3 unknown frame 583 } else { 584 if (!thread->has_last_Java_frame()) { 585 trace->num_frames = 0; // No Java frames 586 } else { 587 trace->num_frames = ticks_not_walkable_not_Java; // -4 non walkable frame by default 588 forte_fill_call_trace_given_top(thread, trace, depth, fr); 589 590 // This assert would seem to be valid but it is not. 591 // It would be valid if we weren't possibly racing a gc 592 // thread. A gc thread can make a valid interpreted frame 593 // look invalid. It's a small window but it does happen. 594 // The assert is left here commented out as a reminder. 595 // assert(trace->num_frames != ticks_not_walkable_not_Java, "should always be walkable"); 596 597 } 598 } 599 } 600 break; 601 case _thread_in_Java: 602 case _thread_in_Java_trans: 603 { 604 frame fr; 605 606 // param isInJava == true - indicate we are in Java code 607 if (!thread->pd_get_top_frame_for_signal_handler(&fr, ucontext, true)) { 608 trace->num_frames = ticks_unknown_Java; // -5 unknown frame 609 } else { 610 trace->num_frames = ticks_not_walkable_Java; // -6, non walkable frame by default 611 forte_fill_call_trace_given_top(thread, trace, depth, fr); 612 } 613 } 614 break; 615 default: 616 // Unknown thread state 617 trace->num_frames = ticks_unknown_state; // -7 618 break; 619 } 620} 621 622 623#ifndef _WINDOWS 624// Support for the Forte(TM) Peformance Tools collector. 625// 626// The method prototype is derived from libcollector.h. For more 627// information, please see the libcollect man page. 628 629// Method to let libcollector know about a dynamically loaded function. 630// Because it is weakly bound, the calls become NOP's when the library 631// isn't present. 632void collector_func_load(char* name, 633 void* null_argument_1, 634 void* null_argument_2, 635 void *vaddr, 636 int size, 637 int zero_argument, 638 void* null_argument_3); 639#pragma weak collector_func_load 640#define collector_func_load(x0,x1,x2,x3,x4,x5,x6) \ 641 ( collector_func_load ? collector_func_load(x0,x1,x2,x3,x4,x5,x6),0 : 0 ) 642#endif // !_WINDOWS 643 644} // end extern "C" 645#endif // !IA64 646 647void Forte::register_stub(const char* name, address start, address end) { 648#if !defined(_WINDOWS) && !defined(IA64) 649 assert(pointer_delta(end, start, sizeof(jbyte)) < INT_MAX, 650 "Code size exceeds maximum range"); 651 652 collector_func_load((char*)name, NULL, NULL, start, 653 pointer_delta(end, start, sizeof(jbyte)), 0, NULL); 654#endif // !_WINDOWS && !IA64 655} 656