jvmtiEnvBase.hpp revision 0
0N/A/*
0N/A * Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A *
0N/A */
0N/A
0N/A#ifndef _JAVA_JVMTIENVBASE_H_
0N/A#define _JAVA_JVMTIENVBASE_H_
0N/A
0N/A//
0N/A// Forward Declarations
0N/A//
0N/A
0N/Aclass JvmtiEnv;
0N/Aclass JvmtiThreadState;
0N/Aclass JvmtiRawMonitor; // for jvmtiEnv.hpp
0N/Aclass JvmtiEventControllerPrivate;
0N/Aclass JvmtiTagMap;
0N/A
0N/A
0N/A
0N/A// One JvmtiEnv object is created per jvmti attachment;
0N/A// done via JNI GetEnv() call. Multiple attachments are
0N/A// allowed in jvmti.
0N/A
0N/Aclass JvmtiEnvBase : public CHeapObj {
0N/A
0N/A private:
0N/A
0N/A static JvmtiEnvBase* _head_environment; // head of environment list
0N/A
0N/A static bool _globally_initialized;
0N/A static jvmtiPhase _phase;
0N/A static volatile int _dying_thread_env_iteration_count;
0N/A
0N/A public:
0N/A
0N/A enum {
0N/A JDK15_JVMTI_VERSION = JVMTI_VERSION_1_0 + 33, /* version: 1.0.33 */
0N/A JDK16_JVMTI_VERSION = JVMTI_VERSION_1_1 + 102 /* version: 1.1.102 */
0N/A };
0N/A
0N/A static jvmtiPhase get_phase() { return _phase; }
0N/A static void set_phase(jvmtiPhase phase) { _phase = phase; }
0N/A static bool is_vm_live() { return _phase == JVMTI_PHASE_LIVE; }
0N/A
0N/A static void entering_dying_thread_env_iteration() { ++_dying_thread_env_iteration_count; }
0N/A static void leaving_dying_thread_env_iteration() { --_dying_thread_env_iteration_count; }
0N/A static bool is_inside_dying_thread_env_iteration(){ return _dying_thread_env_iteration_count > 0; }
0N/A
0N/A private:
0N/A
0N/A enum {
0N/A JVMTI_MAGIC = 0x71EE,
0N/A DISPOSED_MAGIC = 0xDEFC,
0N/A BAD_MAGIC = 0xDEAD
0N/A };
0N/A
0N/A jvmtiEnv _jvmti_external;
0N/A jint _magic;
0N/A JvmtiEnvBase* _next;
0N/A bool _is_retransformable;
0N/A const void *_env_local_storage; // per env agent allocated data.
0N/A jvmtiEventCallbacks _event_callbacks;
0N/A jvmtiExtEventCallbacks _ext_event_callbacks;
0N/A JvmtiTagMap* _tag_map;
0N/A JvmtiEnvEventEnable _env_event_enable;
0N/A jvmtiCapabilities _current_capabilities;
0N/A jvmtiCapabilities _prohibited_capabilities;
0N/A volatile bool _class_file_load_hook_ever_enabled;
0N/A static volatile bool _needs_clean_up;
0N/A char** _native_method_prefixes;
0N/A int _native_method_prefix_count;
0N/A
0N/A protected:
0N/A JvmtiEnvBase();
0N/A ~JvmtiEnvBase();
0N/A void dispose();
0N/A void env_dispose();
0N/A
0N/A void set_env_local_storage(const void* data) { _env_local_storage = data; }
0N/A const void* get_env_local_storage() { return _env_local_storage; }
0N/A
0N/A void record_class_file_load_hook_enabled();
0N/A void record_first_time_class_file_load_hook_enabled();
0N/A
0N/A char** get_native_method_prefixes() { return _native_method_prefixes; }
0N/A int get_native_method_prefix_count() { return _native_method_prefix_count; }
0N/A jvmtiError set_native_method_prefixes(jint prefix_count, char** prefixes);
0N/A
0N/A private:
0N/A friend class JvmtiEventControllerPrivate;
0N/A void initialize();
0N/A void set_event_callbacks(const jvmtiEventCallbacks* callbacks, jint size_of_callbacks);
0N/A static void globally_initialize();
0N/A static void periodic_clean_up();
0N/A
0N/A friend class JvmtiEnvIterator;
0N/A JvmtiEnv* next_environment() { return (JvmtiEnv*)_next; }
0N/A void set_next_environment(JvmtiEnvBase* env) { _next = env; }
0N/A static JvmtiEnv* head_environment() { return (JvmtiEnv*)_head_environment; }
0N/A
0N/A public:
0N/A
0N/A bool is_valid() { return _magic == JVMTI_MAGIC; }
0N/A
0N/A bool is_retransformable() { return _is_retransformable; }
0N/A
0N/A static ByteSize jvmti_external_offset() {
0N/A return byte_offset_of(JvmtiEnvBase, _jvmti_external);
0N/A };
0N/A
0N/A static JvmtiEnv* JvmtiEnv_from_jvmti_env(jvmtiEnv *env) {
0N/A return (JvmtiEnv*)((intptr_t)env - in_bytes(jvmti_external_offset()));
0N/A };
0N/A
0N/A jvmtiCapabilities *get_capabilities() { return &_current_capabilities; }
0N/A
0N/A jvmtiCapabilities *get_prohibited_capabilities() { return &_prohibited_capabilities; }
0N/A
0N/A static char** get_all_native_method_prefixes(int* count_ptr);
0N/A
0N/A // This test will answer true when all environments have been disposed and some have
0N/A // not yet been deallocated. As a result, this test should only be used as an
0N/A // optimization for the no environment case.
0N/A static bool environments_might_exist() {
0N/A return head_environment() != NULL;
0N/A }
0N/A
0N/A static void check_for_periodic_clean_up();
0N/A
0N/A JvmtiEnvEventEnable *env_event_enable() {
0N/A return &_env_event_enable;
0N/A }
0N/A
0N/A jvmtiError allocate(jlong size, unsigned char** mem_ptr) {
0N/A if (size < 0) {
0N/A return JVMTI_ERROR_ILLEGAL_ARGUMENT;
0N/A }
0N/A if (size == 0) {
0N/A *mem_ptr = NULL;
0N/A } else {
0N/A *mem_ptr = (unsigned char *)os::malloc((size_t)size);
0N/A if (*mem_ptr == NULL) {
0N/A return JVMTI_ERROR_OUT_OF_MEMORY;
0N/A }
0N/A }
0N/A return JVMTI_ERROR_NONE;
0N/A }
0N/A
0N/A jvmtiError deallocate(unsigned char* mem) {
0N/A if (mem != NULL) {
0N/A os::free(mem);
0N/A }
0N/A return JVMTI_ERROR_NONE;
0N/A }
0N/A
0N/A
0N/A // Memory functions
0N/A unsigned char* jvmtiMalloc(jlong size); // don't use this - call allocate
0N/A
0N/A // method to create a local handle
0N/A jobject jni_reference(Handle hndl) {
0N/A return JNIHandles::make_local(hndl());
0N/A }
0N/A
0N/A // method to create a local handle.
0N/A // This function allows caller to specify which
0N/A // threads local handle table to use.
0N/A jobject jni_reference(JavaThread *thread, Handle hndl) {
0N/A return JNIHandles::make_local(thread, hndl());
0N/A }
0N/A
0N/A // method to destroy a local handle
0N/A void destroy_jni_reference(jobject jobj) {
0N/A JNIHandles::destroy_local(jobj);
0N/A }
0N/A
0N/A // method to destroy a local handle.
0N/A // This function allows caller to specify which
0N/A // threads local handle table to use although currently it is
0N/A // not used.
0N/A void destroy_jni_reference(JavaThread *thread, jobject jobj) {
0N/A destroy_jni_reference(jobj);
0N/A }
0N/A
0N/A jvmtiEnv* jvmti_external() { return &_jvmti_external; };
0N/A
0N/A// Event Dispatch
0N/A
0N/A bool has_callback(jvmtiEvent event_type) {
0N/A assert(event_type >= JVMTI_MIN_EVENT_TYPE_VAL &&
0N/A event_type <= JVMTI_MAX_EVENT_TYPE_VAL, "checking");
0N/A return ((void**)&_event_callbacks)[event_type-JVMTI_MIN_EVENT_TYPE_VAL] != NULL;
0N/A }
0N/A
0N/A jvmtiEventCallbacks* callbacks() {
0N/A return &_event_callbacks;
0N/A }
0N/A
0N/A jvmtiExtEventCallbacks* ext_callbacks() {
0N/A return &_ext_event_callbacks;
0N/A }
0N/A
0N/A void set_tag_map(JvmtiTagMap* tag_map) {
0N/A _tag_map = tag_map;
0N/A }
0N/A
0N/A JvmtiTagMap* tag_map() {
0N/A return _tag_map;
0N/A }
0N/A
0N/A
0N/A // return true if event is enabled globally or for any thread
0N/A // True only if there is a callback for it.
0N/A bool is_enabled(jvmtiEvent event_type) {
0N/A return _env_event_enable.is_enabled(event_type);
0N/A }
0N/A
0N/A// Random Utilities
0N/A
0N/A protected:
0N/A // helper methods for creating arrays of global JNI Handles from local Handles
0N/A // allocated into environment specific storage
0N/A jobject * new_jobjectArray(int length, Handle *handles);
0N/A jthread * new_jthreadArray(int length, Handle *handles);
0N/A jthreadGroup * new_jthreadGroupArray(int length, Handle *handles);
0N/A
0N/A // convert from JNIHandle to JavaThread *
0N/A JavaThread * get_JavaThread(jthread jni_thread);
0N/A
0N/A // convert to a jni jclass from a non-null klassOop
0N/A jclass get_jni_class_non_null(klassOop k);
0N/A
0N/A void update_klass_field_access_flag(fieldDescriptor *fd);
0N/A
0N/A jint count_locked_objects(JavaThread *java_thread, Handle hobj);
0N/A jvmtiError get_locked_objects_in_frame(JavaThread *calling_thread,
0N/A JavaThread* java_thread,
0N/A javaVFrame *jvf,
0N/A GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitors_list,
0N/A jint depth);
0N/A vframe* vframeFor(JavaThread* java_thread, jint depth);
0N/A
0N/A public:
0N/A // get a field descriptor for the specified class and field
0N/A static bool get_field_descriptor(klassOop k, jfieldID field, fieldDescriptor* fd);
0N/A // test for suspend - most (all?) of these should go away
0N/A static bool is_thread_fully_suspended(JavaThread *thread,
0N/A bool wait_for_suspend,
0N/A uint32_t *bits);
0N/A
0N/A
0N/A // JVMTI API helper functions which are called at safepoint or thread is suspended.
0N/A jvmtiError get_frame_count(JvmtiThreadState *state, jint *count_ptr);
0N/A jvmtiError get_frame_location(JavaThread* java_thread, jint depth,
0N/A jmethodID* method_ptr, jlocation* location_ptr);
0N/A jvmtiError get_object_monitor_usage(JavaThread *calling_thread,
0N/A jobject object, jvmtiMonitorUsage* info_ptr);
0N/A jvmtiError get_stack_trace(JavaThread *java_thread,
0N/A jint stack_depth, jint max_count,
0N/A jvmtiFrameInfo* frame_buffer, jint* count_ptr);
0N/A jvmtiError get_current_contended_monitor(JavaThread *calling_thread,
0N/A JavaThread *java_thread,
0N/A jobject *monitor_ptr);
0N/A jvmtiError get_owned_monitors(JavaThread *calling_thread, JavaThread* java_thread,
0N/A GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list);
0N/A jvmtiError check_top_frame(JavaThread* current_thread, JavaThread* java_thread,
0N/A jvalue value, TosState tos, Handle* ret_ob_h);
0N/A jvmtiError force_early_return(JavaThread* java_thread, jvalue value, TosState tos);
0N/A};
0N/A
0N/A// This class is the only safe means of iterating through environments.
0N/A// Note that this iteratation includes invalid environments pending
0N/A// deallocation -- in fact, some uses depend on this behavior.
0N/A
0N/Aclass JvmtiEnvIterator : public StackObj {
0N/A private:
0N/A bool _entry_was_marked;
0N/A public:
0N/A JvmtiEnvIterator() {
0N/A if (Threads::number_of_threads() == 0) {
0N/A _entry_was_marked = false; // we are single-threaded, no need
0N/A } else {
0N/A Thread::current()->entering_jvmti_env_iteration();
0N/A _entry_was_marked = true;
0N/A }
0N/A }
0N/A ~JvmtiEnvIterator() {
0N/A if (_entry_was_marked) {
0N/A Thread::current()->leaving_jvmti_env_iteration();
0N/A }
0N/A }
0N/A JvmtiEnv* first() { return JvmtiEnvBase::head_environment(); }
0N/A JvmtiEnv* next(JvmtiEnvBase* env) { return env->next_environment(); }
0N/A};
0N/A
0N/A
0N/A// VM operation to get monitor information with stack depth.
0N/Aclass VM_GetOwnedMonitorInfo : public VM_Operation {
0N/Aprivate:
0N/A JvmtiEnv *_env;
0N/A JavaThread* _calling_thread;
0N/A JavaThread *_java_thread;
0N/A jvmtiError _result;
0N/A GrowableArray<jvmtiMonitorStackDepthInfo*> *_owned_monitors_list;
0N/A
0N/Apublic:
0N/A VM_GetOwnedMonitorInfo(JvmtiEnv* env, JavaThread* calling_thread,
0N/A JavaThread* java_thread,
0N/A GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitor_list) {
0N/A _env = env;
0N/A _calling_thread = calling_thread;
0N/A _java_thread = java_thread;
0N/A _owned_monitors_list = owned_monitor_list;
0N/A _result = JVMTI_ERROR_NONE;
0N/A }
0N/A VMOp_Type type() const { return VMOp_GetOwnedMonitorInfo; }
0N/A void doit() {
0N/A ((JvmtiEnvBase *)_env)->get_owned_monitors(_calling_thread, _java_thread,
0N/A _owned_monitors_list);
0N/A }
0N/A jvmtiError result() { return _result; }
0N/A};
0N/A
0N/A
0N/A// VM operation to get object monitor usage.
0N/Aclass VM_GetObjectMonitorUsage : public VM_Operation {
0N/Aprivate:
JvmtiEnv *_env;
jobject _object;
JavaThread* _calling_thread;
jvmtiMonitorUsage* _info_ptr;
jvmtiError _result;
public:
VM_GetObjectMonitorUsage(JvmtiEnv *env, JavaThread* calling_thread, jobject object, jvmtiMonitorUsage* info_ptr) {
_env = env;
_object = object;
_calling_thread = calling_thread;
_info_ptr = info_ptr;
}
VMOp_Type type() const { return VMOp_GetObjectMonitorUsage; }
jvmtiError result() { return _result; }
void doit() {
_result = ((JvmtiEnvBase*) _env)->get_object_monitor_usage(_calling_thread, _object, _info_ptr);
}
};
// VM operation to get current contended monitor.
class VM_GetCurrentContendedMonitor : public VM_Operation {
private:
JvmtiEnv *_env;
JavaThread *_calling_thread;
JavaThread *_java_thread;
jobject *_owned_monitor_ptr;
jvmtiError _result;
public:
VM_GetCurrentContendedMonitor(JvmtiEnv *env, JavaThread *calling_thread, JavaThread *java_thread, jobject *mon_ptr) {
_env = env;
_calling_thread = calling_thread;
_java_thread = java_thread;
_owned_monitor_ptr = mon_ptr;
}
VMOp_Type type() const { return VMOp_GetCurrentContendedMonitor; }
jvmtiError result() { return _result; }
void doit() {
_result = ((JvmtiEnvBase *)_env)->get_current_contended_monitor(_calling_thread,_java_thread,_owned_monitor_ptr);
}
};
// VM operation to get stack trace at safepoint.
class VM_GetStackTrace : public VM_Operation {
private:
JvmtiEnv *_env;
JavaThread *_java_thread;
jint _start_depth;
jint _max_count;
jvmtiFrameInfo *_frame_buffer;
jint *_count_ptr;
jvmtiError _result;
public:
VM_GetStackTrace(JvmtiEnv *env, JavaThread *java_thread,
jint start_depth, jint max_count,
jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
_env = env;
_java_thread = java_thread;
_start_depth = start_depth;
_max_count = max_count;
_frame_buffer = frame_buffer;
_count_ptr = count_ptr;
}
jvmtiError result() { return _result; }
VMOp_Type type() const { return VMOp_GetStackTrace; }
void doit() {
_result = ((JvmtiEnvBase *)_env)->get_stack_trace(_java_thread,
_start_depth, _max_count,
_frame_buffer, _count_ptr);
}
};
// forward declaration
struct StackInfoNode;
// VM operation to get stack trace at safepoint.
class VM_GetMultipleStackTraces : public VM_Operation {
private:
JvmtiEnv *_env;
jint _max_frame_count;
jvmtiStackInfo *_stack_info;
jvmtiError _result;
int _frame_count_total;
struct StackInfoNode *_head;
JvmtiEnvBase *env() { return (JvmtiEnvBase *)_env; }
jint max_frame_count() { return _max_frame_count; }
struct StackInfoNode *head() { return _head; }
void set_head(StackInfoNode *head) { _head = head; }
protected:
void set_result(jvmtiError result) { _result = result; }
void fill_frames(jthread jt, JavaThread *thr, oop thread_oop);
void allocate_and_fill_stacks(jint thread_count);
public:
VM_GetMultipleStackTraces(JvmtiEnv *env, jint max_frame_count) {
_env = env;
_max_frame_count = max_frame_count;
_frame_count_total = 0;
_head = NULL;
_result = JVMTI_ERROR_NONE;
}
VMOp_Type type() const { return VMOp_GetMultipleStackTraces; }
jvmtiStackInfo *stack_info() { return _stack_info; }
jvmtiError result() { return _result; }
};
// VM operation to get stack trace at safepoint.
class VM_GetAllStackTraces : public VM_GetMultipleStackTraces {
private:
JavaThread *_calling_thread;
jint _final_thread_count;
public:
VM_GetAllStackTraces(JvmtiEnv *env, JavaThread *calling_thread,
jint max_frame_count)
: VM_GetMultipleStackTraces(env, max_frame_count) {
_calling_thread = calling_thread;
}
VMOp_Type type() const { return VMOp_GetAllStackTraces; }
void doit();
jint final_thread_count() { return _final_thread_count; }
};
// VM operation to get stack trace at safepoint.
class VM_GetThreadListStackTraces : public VM_GetMultipleStackTraces {
private:
jint _thread_count;
const jthread* _thread_list;
public:
VM_GetThreadListStackTraces(JvmtiEnv *env, jint thread_count, const jthread* thread_list, jint max_frame_count)
: VM_GetMultipleStackTraces(env, max_frame_count) {
_thread_count = thread_count;
_thread_list = thread_list;
}
VMOp_Type type() const { return VMOp_GetThreadListStackTraces; }
void doit();
};
// VM operation to count stack frames at safepoint.
class VM_GetFrameCount : public VM_Operation {
private:
JvmtiEnv *_env;
JvmtiThreadState *_state;
jint *_count_ptr;
jvmtiError _result;
public:
VM_GetFrameCount(JvmtiEnv *env, JvmtiThreadState *state, jint *count_ptr) {
_env = env;
_state = state;
_count_ptr = count_ptr;
}
VMOp_Type type() const { return VMOp_GetFrameCount; }
jvmtiError result() { return _result; }
void doit() {
_result = ((JvmtiEnvBase*)_env)->get_frame_count(_state, _count_ptr);
}
};
// VM operation to frame location at safepoint.
class VM_GetFrameLocation : public VM_Operation {
private:
JvmtiEnv *_env;
JavaThread* _java_thread;
jint _depth;
jmethodID* _method_ptr;
jlocation* _location_ptr;
jvmtiError _result;
public:
VM_GetFrameLocation(JvmtiEnv *env, JavaThread* java_thread, jint depth,
jmethodID* method_ptr, jlocation* location_ptr) {
_env = env;
_java_thread = java_thread;
_depth = depth;
_method_ptr = method_ptr;
_location_ptr = location_ptr;
}
VMOp_Type type() const { return VMOp_GetFrameLocation; }
jvmtiError result() { return _result; }
void doit() {
_result = ((JvmtiEnvBase*)_env)->get_frame_location(_java_thread, _depth,
_method_ptr, _location_ptr);
}
};
// ResourceTracker
//
// ResourceTracker works a little like a ResourceMark. All allocates
// using the resource tracker are recorded. If an allocate using the
// resource tracker fails the destructor will free any resources
// that were allocated using the tracker.
// The motive for this class is to avoid messy error recovery code
// in situations where multiple allocations are done in sequence. If
// the second or subsequent allocation fails it avoids any code to
// release memory allocated in the previous calls.
//
// Usage :-
// ResourceTracker rt(env);
// :
// err = rt.allocate(1024, &ptr);
class ResourceTracker : public StackObj {
private:
JvmtiEnv* _env;
GrowableArray<unsigned char*> *_allocations;
bool _failed;
public:
ResourceTracker(JvmtiEnv* env);
~ResourceTracker();
jvmtiError allocate(jlong size, unsigned char** mem_ptr);
unsigned char* allocate(jlong size);
char* strdup(const char* str);
};
// Jvmti monitor closure to collect off stack monitors.
class JvmtiMonitorClosure: public MonitorClosure {
private:
JavaThread *_java_thread;
JavaThread *_calling_thread;
GrowableArray<jvmtiMonitorStackDepthInfo*> *_owned_monitors_list;
jvmtiError _error;
JvmtiEnvBase *_env;
public:
JvmtiMonitorClosure(JavaThread* thread, JavaThread *calling_thread,
GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors,
JvmtiEnvBase *env) {
_java_thread = thread;
_calling_thread = calling_thread;
_owned_monitors_list = owned_monitors;
_error = JVMTI_ERROR_NONE;
_env = env;
}
void do_monitor(ObjectMonitor* mon);
jvmtiError error() { return _error;}
};
#endif /* _JAVA_JVMTIENVBASE_H_ */