forte.cpp revision 2273:1d1603768966
1255376Sdes/*
2348980Sdes * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
3348980Sdes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
491094Sdes *
591094Sdes * This code is free software; you can redistribute it and/or modify it
691094Sdes * under the terms of the GNU General Public License version 2 only, as
791094Sdes * published by the Free Software Foundation.
891094Sdes *
991094Sdes * This code is distributed in the hope that it will be useful, but WITHOUT
10108794Sdes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1191094Sdes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12117610Sdes * version 2 for more details (a copy is included in the LICENSE file that
13174832Sdes * accompanied this code).
1491094Sdes *
1591094Sdes * You should have received a copy of the GNU General Public License version
16236109Sdes * 2 along with this work; if not, write to the Free Software Foundation,
1791100Sdes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1891100Sdes *
19141098Sdes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20228692Sdes * or visit www.oracle.com if you need additional information or have any
21228692Sdes * questions.
22141098Sdes *
23228692Sdes */
24228692Sdes
25228692Sdes#include "precompiled.hpp"
26228692Sdes#include "code/debugInfoRec.hpp"
27228692Sdes#include "code/pcDesc.hpp"
28228692Sdes#include "gc_interface/collectedHeap.inline.hpp"
2991094Sdes#include "memory/space.hpp"
3091094Sdes#include "memory/universe.inline.hpp"
31236109Sdes#include "oops/oop.inline.hpp"
3291100Sdes#include "oops/oop.inline2.hpp"
3391100Sdes#include "prims/forte.hpp"
3491100Sdes#include "runtime/thread.hpp"
3591094Sdes#include "runtime/vframe.hpp"
36228692Sdes#include "runtime/vframeArray.hpp"
37228692Sdes
3891094Sdes// These name match the names reported by the forte quality kit
3991094Sdesenum {
4091094Sdes  ticks_no_Java_frame         =  0,
4191094Sdes  ticks_no_class_load         = -1,
4291094Sdes  ticks_GC_active             = -2,
4391094Sdes  ticks_unknown_not_Java      = -3,
4491094Sdes  ticks_not_walkable_not_Java = -4,
45236109Sdes  ticks_unknown_Java          = -5,
46236109Sdes  ticks_not_walkable_Java     = -6,
47236109Sdes  ticks_unknown_state         = -7,
48117610Sdes  ticks_thread_exit           = -8,
49236109Sdes  ticks_deopt                 = -9,
50147466Sdes  ticks_safepoint             = -10
51117610Sdes};
5291094Sdes
5391094Sdes//-------------------------------------------------------
54255376Sdes
55255376Sdes// Native interfaces for use by Forte tools.
56348980Sdes
57
58#ifndef IA64
59
60class 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};
66
67
68static bool is_decipherable_compiled_frame(JavaThread* thread, frame* fr, nmethod* nm);
69static bool is_decipherable_interpreted_frame(JavaThread* thread,
70                                              frame* fr,
71                                              methodOop* method_p,
72                                              int* bci_p);
73
74
75
76
77vframeStreamForte::vframeStreamForte(JavaThread *jt,
78                                     frame fr,
79                                     bool stop_at_java_call_stub) : vframeStreamCommon(jt) {
80
81  _stop_at_java_call_stub = stop_at_java_call_stub;
82  _frame = fr;
83
84  // We must always have a valid frame to start filling
85
86  bool filled_in = fill_from_frame();
87
88  assert(filled_in, "invariant");
89
90}
91
92
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.
101void vframeStreamForte::forte_next() {
102  // handle frames with inlining
103  if (_mode == compiled_mode &&
104      vframeStreamCommon::fill_in_compiled_inlined_sender()) {
105    return;
106  }
107
108  // handle general case
109
110  int loop_count = 0;
111  int loop_max = MaxJavaStackTraceDepth * 2;
112
113
114  do {
115
116    loop_count++;
117
118    // By the time we get here we should never see unsafe but better
119    // safe then segv'd
120
121    if (loop_count > loop_max || !_frame.safe_for_sender(_thread)) {
122      _mode = at_end_mode;
123      return;
124    }
125
126    _frame = _frame.sender(&_reg_map);
127
128  } while (!fill_from_frame());
129}
130
131// Determine if 'fr' is a decipherable compiled frame. We are already
132// assured that fr is for a java nmethod.
133
134static bool is_decipherable_compiled_frame(JavaThread* thread, frame* fr, nmethod* nm) {
135  assert(nm->is_java_method(), "invariant");
136
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());
141
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  }
148
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);
155
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  }
163
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}
169
170
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.
178
179static bool is_decipherable_interpreted_frame(JavaThread* thread,
180                                              frame* fr,
181                                              methodOop* method_p,
182                                              int* bci_p) {
183  assert(fr->is_interpreted_frame(), "just checking");
184
185  // top frame is an interpreted frame
186  // check if it is walkable (i.e. valid methodOop and valid bci)
187
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.
193
194  JavaThreadState state = thread->thread_state();
195  bool known_valid = (state == _thread_in_native ||
196                      state == _thread_in_vm ||
197                      state == _thread_blocked );
198
199  if (known_valid || fr->is_interpreted_frame_valid(thread)) {
200
201    // The frame code should completely validate the frame so that
202    // references to methodOop 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
208
209    methodOop method = fr->interpreter_frame_method();
210
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.
217
218    *method_p = method;
219
220    // See if gc may have invalidated method since we validated frame
221
222    if (!Universe::heap()->is_valid_method(method)) return false;
223
224    intptr_t bcx = fr->interpreter_frame_bcx();
225
226    int      bci = method->validate_bci_from_bcx(bcx);
227
228    // note: bci is set to -1 if not a valid bci
229    *bci_p = bci;
230    return true;
231  }
232
233  return false;
234}
235
236
237// Determine if 'fr' can be used to find an initial Java frame.
238// Return false if it can not find a fully decipherable Java frame
239// (in other words a frame that isn't safe to use in a vframe stream).
240// Obviously if it can't even find a Java frame false will also be returned.
241//
242// If we find a Java frame decipherable or not then by definition we have
243// identified a method and that will be returned to the caller via method_p.
244// If we can determine a bci that is returned also. (Hmm is it possible
245// to return a method and bci and still return false? )
246//
247// The initial Java frame we find (if any) is return via initial_frame_p.
248//
249
250static bool find_initial_Java_frame(JavaThread* thread,
251                                    frame* fr,
252                                    frame* initial_frame_p,
253                                    methodOop* method_p,
254                                    int* bci_p) {
255
256  // It is possible that for a frame containing an nmethod
257  // we can capture the method but no bci. If we get no
258  // bci the frame isn't walkable but the method is usable.
259  // Therefore we init the returned methodOop to NULL so the
260  // caller can make the distinction.
261
262  *method_p = NULL;
263
264  // On the initial call to this method the frame we get may not be
265  // recognizable to us. This should only happen if we are in a JRT_LEAF
266  // or something called by a JRT_LEAF method.
267
268
269
270  frame candidate = *fr;
271
272  // If the starting frame we were given has no codeBlob associated with
273  // it see if we can find such a frame because only frames with codeBlobs
274  // are possible Java frames.
275
276  if (fr->cb() == NULL) {
277
278    // See if we can find a useful frame
279    int loop_count;
280    int loop_max = MaxJavaStackTraceDepth * 2;
281    RegisterMap map(thread, false);
282
283    for (loop_count = 0; loop_count < loop_max; loop_count++) {
284      if (!candidate.safe_for_sender(thread)) return false;
285      candidate = candidate.sender(&map);
286      if (candidate.cb() != NULL) break;
287    }
288    if (candidate.cb() == NULL) return false;
289  }
290
291  // We have a frame known to be in the codeCache
292  // We will hopefully be able to figure out something to do with it.
293  int loop_count;
294  int loop_max = MaxJavaStackTraceDepth * 2;
295  RegisterMap map(thread, false);
296
297  for (loop_count = 0; loop_count < loop_max; loop_count++) {
298
299    if (candidate.is_first_frame()) {
300      // If initial frame is frame from StubGenerator and there is no
301      // previous anchor, there are no java frames associated with a method
302      return false;
303    }
304
305    if (candidate.is_interpreted_frame()) {
306      if (is_decipherable_interpreted_frame(thread, &candidate, method_p, bci_p)) {
307        *initial_frame_p = candidate;
308        return true;
309      }
310
311      // Hopefully we got some data
312      return false;
313    }
314
315    if (candidate.cb()->is_nmethod()) {
316
317      nmethod* nm = (nmethod*) candidate.cb();
318      *method_p = nm->method();
319
320      // If the frame isn't fully decipherable then the default
321      // value for the bci is a signal that we don't have a bci.
322      // If we have a decipherable frame this bci value will
323      // not be used.
324
325      *bci_p = -1;
326
327      *initial_frame_p = candidate;
328
329      // Native wrapper code is trivial to decode by vframeStream
330
331      if (nm->is_native_method()) return true;
332
333      // If it isn't decipherable then we have found a pc that doesn't
334      // have a PCDesc that can get us a bci however we did find
335      // a method
336
337      if (!is_decipherable_compiled_frame(thread, &candidate, nm)) {
338        return false;
339      }
340
341      // is_decipherable_compiled_frame may modify candidate's pc
342      *initial_frame_p = candidate;
343
344      assert(nm->pc_desc_at(candidate.pc()) != NULL, "if it's decipherable then pc must be valid");
345
346      return true;
347    }
348
349    // Must be some stub frame that we don't care about
350
351    if (!candidate.safe_for_sender(thread)) return false;
352    candidate = candidate.sender(&map);
353
354    // If it isn't in the code cache something is wrong
355    // since once we find a frame in the code cache they
356    // all should be there.
357
358    if (candidate.cb() == NULL) return false;
359
360  }
361
362  return false;
363
364}
365
366
367// call frame copied from old .h file and renamed
368typedef struct {
369    jint lineno;                      // line number in the source file
370    jmethodID method_id;              // method executed in this frame
371} ASGCT_CallFrame;
372
373// call trace copied from old .h file and renamed
374typedef struct {
375    JNIEnv *env_id;                   // Env where trace was recorded
376    jint num_frames;                  // number of frames in this trace
377    ASGCT_CallFrame *frames;          // frames
378} ASGCT_CallTrace;
379
380static void forte_fill_call_trace_given_top(JavaThread* thd,
381                                            ASGCT_CallTrace* trace,
382                                            int depth,
383                                            frame top_frame) {
384  NoHandleMark nhm;
385
386  frame initial_Java_frame;
387  methodOop method;
388  int bci;
389  int count;
390
391  count = 0;
392  assert(trace->frames != NULL, "trace->frames must be non-NULL");
393
394  bool fully_decipherable = find_initial_Java_frame(thd, &top_frame, &initial_Java_frame, &method, &bci);
395
396  // The frame might not be walkable but still recovered a method
397  // (e.g. an nmethod with no scope info for the pc
398
399  if (method == NULL) return;
400
401  CollectedHeap* ch = Universe::heap();
402
403  // The method is not stored GC safe so see if GC became active
404  // after we entered AsyncGetCallTrace() and before we try to
405  // use the methodOop.
406  // Yes, there is still a window after this check and before
407  // we use methodOop below, but we can't lock out GC so that
408  // has to be an acceptable risk.
409  if (!ch->is_valid_method(method)) {
410    trace->num_frames = ticks_GC_active; // -2
411    return;
412  }
413
414  // We got a Java frame however it isn't fully decipherable
415  // so it won't necessarily be safe to use it for the
416  // initial frame in the vframe stream.
417
418  if (!fully_decipherable) {
419    // Take whatever method the top-frame decoder managed to scrape up.
420    // We look further at the top frame only if non-safepoint
421    // debugging information is available.
422    count++;
423    trace->num_frames = count;
424    trace->frames[0].method_id = method->find_jmethod_id_or_null();
425    if (!method->is_native()) {
426      trace->frames[0].lineno = bci;
427    } else {
428      trace->frames[0].lineno = -3;
429    }
430
431    if (!initial_Java_frame.safe_for_sender(thd)) return;
432
433    RegisterMap map(thd, false);
434    initial_Java_frame = initial_Java_frame.sender(&map);
435  }
436
437  vframeStreamForte st(thd, initial_Java_frame, false);
438
439  for (; !st.at_end() && count < depth; st.forte_next(), count++) {
440    bci = st.bci();
441    method = st.method();
442
443    // The method is not stored GC safe so see if GC became active
444    // after we entered AsyncGetCallTrace() and before we try to
445    // use the methodOop.
446    // Yes, there is still a window after this check and before
447    // we use methodOop below, but we can't lock out GC so that
448    // has to be an acceptable risk.
449    if (!ch->is_valid_method(method)) {
450      // we throw away everything we've gathered in this sample since
451      // none of it is safe
452      trace->num_frames = ticks_GC_active; // -2
453      return;
454    }
455
456    trace->frames[count].method_id = method->find_jmethod_id_or_null();
457    if (!method->is_native()) {
458      trace->frames[count].lineno = bci;
459    } else {
460      trace->frames[count].lineno = -3;
461    }
462  }
463  trace->num_frames = count;
464  return;
465}
466
467
468// Forte Analyzer AsyncGetCallTrace() entry point. Currently supported
469// on Linux X86, Solaris SPARC and Solaris X86.
470//
471// Async-safe version of GetCallTrace being called from a signal handler
472// when a LWP gets interrupted by SIGPROF but the stack traces are filled
473// with different content (see below).
474//
475// This function must only be called when JVM/TI
476// CLASS_LOAD events have been enabled since agent startup. The enabled
477// event will cause the jmethodIDs to be allocated at class load time.
478// The jmethodIDs cannot be allocated in a signal handler because locks
479// cannot be grabbed in a signal handler safely.
480//
481// void (*AsyncGetCallTrace)(ASGCT_CallTrace *trace, jint depth, void* ucontext)
482//
483// Called by the profiler to obtain the current method call stack trace for
484// a given thread. The thread is identified by the env_id field in the
485// ASGCT_CallTrace structure. The profiler agent should allocate a ASGCT_CallTrace
486// structure with enough memory for the requested stack depth. The VM fills in
487// the frames buffer and the num_frames field.
488//
489// Arguments:
490//
491//   trace    - trace data structure to be filled by the VM.
492//   depth    - depth of the call stack trace.
493//   ucontext - ucontext_t of the LWP
494//
495// ASGCT_CallTrace:
496//   typedef struct {
497//       JNIEnv *env_id;
498//       jint num_frames;
499//       ASGCT_CallFrame *frames;
500//   } ASGCT_CallTrace;
501//
502// Fields:
503//   env_id     - ID of thread which executed this trace.
504//   num_frames - number of frames in the trace.
505//                (< 0 indicates the frame is not walkable).
506//   frames     - the ASGCT_CallFrames that make up this trace. Callee followed by callers.
507//
508//  ASGCT_CallFrame:
509//    typedef struct {
510//        jint lineno;
511//        jmethodID method_id;
512//    } ASGCT_CallFrame;
513//
514//  Fields:
515//    1) For Java frame (interpreted and compiled),
516//       lineno    - bci of the method being executed or -1 if bci is not available
517//       method_id - jmethodID of the method being executed
518//    2) For native method
519//       lineno    - (-3)
520//       method_id - jmethodID of the method being executed
521
522extern "C" {
523JNIEXPORT
524void AsyncGetCallTrace(ASGCT_CallTrace *trace, jint depth, void* ucontext) {
525
526// This is if'd out because we no longer use thread suspension.
527// However if someone wanted to backport this to a 5.0 jvm then this
528// code would be important.
529#if 0
530  if (SafepointSynchronize::is_synchronizing()) {
531    // The safepoint mechanism is trying to synchronize all the threads.
532    // Since this can involve thread suspension, it is not safe for us
533    // to be here. We can reduce the deadlock risk window by quickly
534    // returning to the SIGPROF handler. However, it is still possible
535    // for VMThread to catch us here or in the SIGPROF handler. If we
536    // are suspended while holding a resource and another thread blocks
537    // on that resource in the SIGPROF handler, then we will have a
538    // three-thread deadlock (VMThread, this thread, the other thread).
539    trace->num_frames = ticks_safepoint; // -10
540    return;
541  }
542#endif
543
544  JavaThread* thread;
545
546  if (trace->env_id == NULL ||
547    (thread = JavaThread::thread_from_jni_environment(trace->env_id)) == NULL ||
548    thread->is_exiting()) {
549
550    // bad env_id, thread has exited or thread is exiting
551    trace->num_frames = ticks_thread_exit; // -8
552    return;
553  }
554
555  if (thread->in_deopt_handler()) {
556    // thread is in the deoptimization handler so return no frames
557    trace->num_frames = ticks_deopt; // -9
558    return;
559  }
560
561  assert(JavaThread::current() == thread,
562         "AsyncGetCallTrace must be called by the current interrupted thread");
563
564  if (!JvmtiExport::should_post_class_load()) {
565    trace->num_frames = ticks_no_class_load; // -1
566    return;
567  }
568
569  if (Universe::heap()->is_gc_active()) {
570    trace->num_frames = ticks_GC_active; // -2
571    return;
572  }
573
574  switch (thread->thread_state()) {
575  case _thread_new:
576  case _thread_uninitialized:
577  case _thread_new_trans:
578    // We found the thread on the threads list above, but it is too
579    // young to be useful so return that there are no Java frames.
580    trace->num_frames = 0;
581    break;
582  case _thread_in_native:
583  case _thread_in_native_trans:
584  case _thread_blocked:
585  case _thread_blocked_trans:
586  case _thread_in_vm:
587  case _thread_in_vm_trans:
588    {
589      frame fr;
590
591      // param isInJava == false - indicate we aren't in Java code
592      if (!thread->pd_get_top_frame_for_signal_handler(&fr, ucontext, false)) {
593        trace->num_frames = ticks_unknown_not_Java;  // -3 unknown frame
594      } else {
595        if (!thread->has_last_Java_frame()) {
596          trace->num_frames = 0; // No Java frames
597        } else {
598          trace->num_frames = ticks_not_walkable_not_Java;    // -4 non walkable frame by default
599          forte_fill_call_trace_given_top(thread, trace, depth, fr);
600
601          // This assert would seem to be valid but it is not.
602          // It would be valid if we weren't possibly racing a gc
603          // thread. A gc thread can make a valid interpreted frame
604          // look invalid. It's a small window but it does happen.
605          // The assert is left here commented out as a reminder.
606          // assert(trace->num_frames != ticks_not_walkable_not_Java, "should always be walkable");
607
608        }
609      }
610    }
611    break;
612  case _thread_in_Java:
613  case _thread_in_Java_trans:
614    {
615      frame fr;
616
617      // param isInJava == true - indicate we are in Java code
618      if (!thread->pd_get_top_frame_for_signal_handler(&fr, ucontext, true)) {
619        trace->num_frames = ticks_unknown_Java;  // -5 unknown frame
620      } else {
621        trace->num_frames = ticks_not_walkable_Java;  // -6, non walkable frame by default
622        forte_fill_call_trace_given_top(thread, trace, depth, fr);
623      }
624    }
625    break;
626  default:
627    // Unknown thread state
628    trace->num_frames = ticks_unknown_state; // -7
629    break;
630  }
631}
632
633
634#ifndef _WINDOWS
635// Support for the Forte(TM) Peformance Tools collector.
636//
637// The method prototype is derived from libcollector.h. For more
638// information, please see the libcollect man page.
639
640// Method to let libcollector know about a dynamically loaded function.
641// Because it is weakly bound, the calls become NOP's when the library
642// isn't present.
643void    collector_func_load(char* name,
644                            void* null_argument_1,
645                            void* null_argument_2,
646                            void *vaddr,
647                            int size,
648                            int zero_argument,
649                            void* null_argument_3);
650#pragma weak collector_func_load
651#define collector_func_load(x0,x1,x2,x3,x4,x5,x6) \
652        ( collector_func_load ? collector_func_load(x0,x1,x2,x3,x4,x5,x6),0 : 0 )
653#endif // !_WINDOWS
654
655} // end extern "C"
656#endif // !IA64
657
658void Forte::register_stub(const char* name, address start, address end) {
659#if !defined(_WINDOWS) && !defined(IA64)
660  assert(pointer_delta(end, start, sizeof(jbyte)) < INT_MAX,
661         "Code size exceeds maximum range");
662
663  collector_func_load((char*)name, NULL, NULL, start,
664    pointer_delta(end, start, sizeof(jbyte)), 0, NULL);
665#endif // !_WINDOWS && !IA64
666}
667