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