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