/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "precompiled.hpp"
#include "code/debugInfoRec.hpp"
#include "code/pcDesc.hpp"
#include "gc_interface/collectedHeap.inline.hpp"
#include "memory/universe.inline.hpp"
#include "oops/oop.inline.hpp"
#include "oops/oop.inline2.hpp"
#include "runtime/thread.hpp"
#include "runtime/vframe.hpp"
#include "runtime/vframeArray.hpp"
// These name match the names reported by the forte quality kit
enum {
ticks_no_Java_frame = 0,
};
//-------------------------------------------------------
// Native interfaces for use by Forte tools.
#ifndef IA64
public:
// constructor that starts with sender of frame fr (top_frame)
void forte_next();
};
int* bci_p);
// We must always have a valid frame to start filling
}
// Solaris SPARC Compiler1 needs an additional check on the grandparent
// of the top_frame when the parent of the top_frame is interpreted and
// the grandparent is compiled. However, in this method we do not know
// the relationship of the current _frame relative to the top_frame so
// we implement a more broad sanity check. When the previous callee is
// interpreted and the current sender is compiled, we verify that the
// current sender is also walkable. If it is not walkable, then we mark
// the current vframeStream as at the end.
// handle frames with inlining
if (_mode == compiled_mode &&
return;
}
// handle general case
int loop_count = 0;
do {
loop_count++;
// By the time we get here we should never see unsafe but better
// safe then segv'd
_mode = at_end_mode;
return;
}
} while (!fill_from_frame());
}
// Determine if 'fr' is a decipherable compiled frame. We are already
// assured that fr is for a java nmethod.
// We're stopped at a call into the JVM so look for a PcDesc with
// the actual pc reported by the frame.
// Did we find a useful PcDesc?
return true;
}
}
// We're at some random pc in the nmethod so search for the PcDesc
// whose pc is greater than the current PC. It's done this way
// because the extra PcDescs that are recorded for improved debug
// info record the end of the region covered by the ScopeDesc
// instead of the beginning.
// Now do we have a useful PcDesc?
// No debug information available for this pc
// vframeStream would explode if we try and walk the frames.
return false;
}
// This PcDesc is useful however we must adjust the frame's pc
// so that the vframeStream lookups will use this same pc
return true;
}
// Determine if 'fr' is a walkable interpreted frame. Returns false
// if it is not. *method_p, and *bci_p are not set when false is
// returned. *method_p is non-NULL if frame was executing a Java
// method. *bci_p is != -1 if a valid BCI in the Java method could
// be found.
// Note: this method returns true when a valid Java method is found
// even if a valid BCI cannot be found.
int* bci_p) {
// top frame is an interpreted frame
// check if it is walkable (i.e. valid methodOop and valid bci)
// of a valid interpreter frame may look bad causing us to
// fail the is_interpreted_frame_valid test. If the thread
// is in any of the following states we are assured that the
// frame is in fact valid and we must have hit the race.
state == _thread_in_vm ||
state == _thread_blocked );
// The frame code should completely validate the frame so that
// references to methodOop and bci are completely safe to access
// If they aren't the frame code should be fixed not this
// code. However since gc isn't locked out the values could be
// stale. This is a race we can never completely win since we can't
// lock out gc so do one last check after retrieving their values
// from the frame for additional safety
// We've at least found a method.
// NOTE: there is something to be said for the approach that
// if we don't find a valid bci then the method is not likely
// a valid method. Then again we may have caught an interpreter
// frame in the middle of construction and the bci field is
// not yet valid.
// See if gc may have invalidated method since we validated frame
// note: bci is set to -1 if not a valid bci
return true;
}
return false;
}
// Determine if 'fr' can be used to find an initial Java frame.
// Return false if it can not find a fully decipherable Java frame
// (in other words a frame that isn't safe to use in a vframe stream).
// Obviously if it can't even find a Java frame false will also be returned.
//
// If we find a Java frame decipherable or not then by definition we have
// identified a method and that will be returned to the caller via method_p.
// If we can determine a bci that is returned also. (Hmm is it possible
// to return a method and bci and still return false? )
//
// The initial Java frame we find (if any) is return via initial_frame_p.
//
int* bci_p) {
// It is possible that for a frame containing an nmethod
// we can capture the method but no bci. If we get no
// bci the frame isn't walkable but the method is usable.
// Therefore we init the returned methodOop to NULL so the
// caller can make the distinction.
// On the initial call to this method the frame we get may not be
// recognizable to us. This should only happen if we are in a JRT_LEAF
// or something called by a JRT_LEAF method.
// If the starting frame we were given has no codeBlob associated with
// it see if we can find such a frame because only frames with codeBlobs
// are possible Java frames.
// See if we can find a useful frame
int loop_count;
}
}
// We have a frame known to be in the codeCache
// We will hopefully be able to figure out something to do with it.
int loop_count;
if (candidate.is_first_frame()) {
// If initial frame is frame from StubGenerator and there is no
// previous anchor, there are no java frames associated with a method
return false;
}
if (candidate.is_interpreted_frame()) {
return true;
}
// Hopefully we got some data
return false;
}
// If the frame isn't fully decipherable then the default
// value for the bci is a signal that we don't have a bci.
// If we have a decipherable frame this bci value will
// not be used.
*bci_p = -1;
// Native wrapper code is trivial to decode by vframeStream
if (nm->is_native_method()) return true;
// If it isn't decipherable then we have found a pc that doesn't
// have a PCDesc that can get us a bci however we did find
// a method
return false;
}
// is_decipherable_compiled_frame may modify candidate's pc
return true;
}
// Must be some stub frame that we don't care about
// If it isn't in the code cache something is wrong
// since once we find a frame in the code cache they
// all should be there.
}
return false;
}
// call frame copied from old .h file and renamed
typedef struct {
// call trace copied from old .h file and renamed
typedef struct {
int depth,
int bci;
int count;
count = 0;
bool fully_decipherable = find_initial_Java_frame(thd, &top_frame, &initial_Java_frame, &method, &bci);
// The frame might not be walkable but still recovered a method
// (e.g. an nmethod with no scope info for the pc
// The method is not stored GC safe so see if GC became active
// after we entered AsyncGetCallTrace() and before we try to
// use the methodOop.
// Yes, there is still a window after this check and before
// we use methodOop below, but we can't lock out GC so that
// has to be an acceptable risk.
return;
}
// We got a Java frame however it isn't fully decipherable
// so it won't necessarily be safe to use it for the
// initial frame in the vframe stream.
if (!fully_decipherable) {
// Take whatever method the top-frame decoder managed to scrape up.
// We look further at the top frame only if non-safepoint
// debugging information is available.
count++;
} else {
}
}
// The method is not stored GC safe so see if GC became active
// after we entered AsyncGetCallTrace() and before we try to
// use the methodOop.
// Yes, there is still a window after this check and before
// we use methodOop below, but we can't lock out GC so that
// has to be an acceptable risk.
// we throw away everything we've gathered in this sample since
// none of it is safe
return;
}
} else {
}
}
return;
}
// Forte Analyzer AsyncGetCallTrace() entry point. Currently supported
// on Linux X86, Solaris SPARC and Solaris X86.
//
// Async-safe version of GetCallTrace being called from a signal handler
// when a LWP gets interrupted by SIGPROF but the stack traces are filled
// with different content (see below).
//
// CLASS_LOAD events have been enabled since agent startup. The enabled
// event will cause the jmethodIDs to be allocated at class load time.
// The jmethodIDs cannot be allocated in a signal handler because locks
// cannot be grabbed in a signal handler safely.
//
// void (*AsyncGetCallTrace)(ASGCT_CallTrace *trace, jint depth, void* ucontext)
//
// Called by the profiler to obtain the current method call stack trace for
// a given thread. The thread is identified by the env_id field in the
// ASGCT_CallTrace structure. The profiler agent should allocate a ASGCT_CallTrace
// structure with enough memory for the requested stack depth. The VM fills in
// the frames buffer and the num_frames field.
//
// Arguments:
//
// trace - trace data structure to be filled by the VM.
// depth - depth of the call stack trace.
// ucontext - ucontext_t of the LWP
//
// ASGCT_CallTrace:
// typedef struct {
// JNIEnv *env_id;
// jint num_frames;
// ASGCT_CallFrame *frames;
// } ASGCT_CallTrace;
//
// Fields:
// env_id - ID of thread which executed this trace.
// num_frames - number of frames in the trace.
// (< 0 indicates the frame is not walkable).
// frames - the ASGCT_CallFrames that make up this trace. Callee followed by callers.
//
// ASGCT_CallFrame:
// typedef struct {
// jint lineno;
// jmethodID method_id;
// } ASGCT_CallFrame;
//
// Fields:
// 1) For Java frame (interpreted and compiled),
// lineno - bci of the method being executed or -1 if bci is not available
// method_id - jmethodID of the method being executed
// 2) For native method
// lineno - (-3)
// method_id - jmethodID of the method being executed
extern "C" {
thread->is_exiting()) {
// bad env_id, thread has exited or thread is exiting
return;
}
if (thread->in_deopt_handler()) {
// thread is in the deoptimization handler so return no frames
return;
}
"AsyncGetCallTrace must be called by the current interrupted thread");
if (!JvmtiExport::should_post_class_load()) {
return;
}
return;
}
switch (thread->thread_state()) {
case _thread_new:
case _thread_uninitialized:
case _thread_new_trans:
// We found the thread on the threads list above, but it is too
// young to be useful so return that there are no Java frames.
trace->num_frames = 0;
break;
case _thread_in_native:
case _thread_in_native_trans:
case _thread_blocked:
case _thread_blocked_trans:
case _thread_in_vm:
case _thread_in_vm_trans:
{
// param isInJava == false - indicate we aren't in Java code
} else {
if (!thread->has_last_Java_frame()) {
} else {
// This assert would seem to be valid but it is not.
// It would be valid if we weren't possibly racing a gc
// thread. A gc thread can make a valid interpreted frame
// look invalid. It's a small window but it does happen.
// The assert is left here commented out as a reminder.
// assert(trace->num_frames != ticks_not_walkable_not_Java, "should always be walkable");
}
}
}
break;
case _thread_in_Java:
case _thread_in_Java_trans:
{
// param isInJava == true - indicate we are in Java code
} else {
}
}
break;
default:
// Unknown thread state
break;
}
}
#ifndef _WINDOWS
// Support for the Forte(TM) Peformance Tools collector.
//
// The method prototype is derived from libcollector.h. For more
// information, please see the libcollect man page.
// Method to let libcollector know about a dynamically loaded function.
// Because it is weakly bound, the calls become NOP's when the library
// isn't present.
#ifdef __APPLE__
// XXXDARWIN: Link errors occur even when __attribute__((weak_import))
// is added
#else
void collector_func_load(char* name,
void* null_argument_1,
void* null_argument_2,
void *vaddr,
int size,
int zero_argument,
void* null_argument_3);
#endif // __APPLE__
#endif // !_WINDOWS
} // end extern "C"
#endif // !IA64
"Code size exceeds maximum range");
#endif // !_WINDOWS && !IA64
}