duke@435: /* duke@435: * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: # include "incls/_precompiled.incl" duke@435: # include "incls/_jvmtiEnvBase.cpp.incl" duke@435: duke@435: duke@435: /////////////////////////////////////////////////////////////// duke@435: // duke@435: // JvmtiEnvBase duke@435: // duke@435: duke@435: JvmtiEnvBase* JvmtiEnvBase::_head_environment = NULL; duke@435: duke@435: bool JvmtiEnvBase::_globally_initialized = false; duke@435: volatile bool JvmtiEnvBase::_needs_clean_up = false; duke@435: duke@435: jvmtiPhase JvmtiEnvBase::_phase = JVMTI_PHASE_PRIMORDIAL; duke@435: duke@435: volatile int JvmtiEnvBase::_dying_thread_env_iteration_count = 0; duke@435: duke@435: extern jvmtiInterface_1_ jvmti_Interface; duke@435: extern jvmtiInterface_1_ jvmtiTrace_Interface; duke@435: duke@435: duke@435: // perform initializations that must occur before any JVMTI environments duke@435: // are released but which should only be initialized once (no matter duke@435: // how many environments are created). duke@435: void duke@435: JvmtiEnvBase::globally_initialize() { duke@435: assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check"); duke@435: assert(_globally_initialized == false, "bad call"); duke@435: duke@435: JvmtiManageCapabilities::initialize(); duke@435: duke@435: #ifndef JVMTI_KERNEL duke@435: // register extension functions and events duke@435: JvmtiExtensions::register_extensions(); duke@435: #endif // !JVMTI_KERNEL duke@435: duke@435: #ifdef JVMTI_TRACE duke@435: JvmtiTrace::initialize(); duke@435: #endif duke@435: duke@435: _globally_initialized = true; duke@435: } duke@435: duke@435: duke@435: void duke@435: JvmtiEnvBase::initialize() { duke@435: assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check"); duke@435: duke@435: // Add this environment to the end of the environment list (order is important) duke@435: { duke@435: // This block of code must not contain any safepoints, as list deallocation duke@435: // (which occurs at a safepoint) cannot occur simultaneously with this list duke@435: // addition. Note: No_Safepoint_Verifier cannot, currently, be used before duke@435: // threads exist. duke@435: JvmtiEnvIterator it; duke@435: JvmtiEnvBase *previous_env = NULL; duke@435: for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) { duke@435: previous_env = env; duke@435: } duke@435: if (previous_env == NULL) { duke@435: _head_environment = this; duke@435: } else { duke@435: previous_env->set_next_environment(this); duke@435: } duke@435: } duke@435: duke@435: if (_globally_initialized == false) { duke@435: globally_initialize(); duke@435: } duke@435: } duke@435: duke@435: duke@435: JvmtiEnvBase::JvmtiEnvBase() : _env_event_enable() { duke@435: _env_local_storage = NULL; duke@435: _tag_map = NULL; duke@435: _native_method_prefix_count = 0; duke@435: _native_method_prefixes = NULL; duke@435: _next = NULL; duke@435: _class_file_load_hook_ever_enabled = false; duke@435: duke@435: // Moot since ClassFileLoadHook not yet enabled. duke@435: // But "true" will give a more predictable ClassFileLoadHook behavior duke@435: // for environment creation during ClassFileLoadHook. duke@435: _is_retransformable = true; duke@435: duke@435: // all callbacks initially NULL duke@435: memset(&_event_callbacks,0,sizeof(jvmtiEventCallbacks)); duke@435: duke@435: // all capabilities initially off duke@435: memset(&_current_capabilities, 0, sizeof(_current_capabilities)); duke@435: duke@435: // all prohibited capabilities initially off duke@435: memset(&_prohibited_capabilities, 0, sizeof(_prohibited_capabilities)); duke@435: duke@435: _magic = JVMTI_MAGIC; duke@435: duke@435: JvmtiEventController::env_initialize((JvmtiEnv*)this); duke@435: duke@435: #ifdef JVMTI_TRACE duke@435: _jvmti_external.functions = strlen(TraceJVMTI)? &jvmtiTrace_Interface : &jvmti_Interface; duke@435: #else duke@435: _jvmti_external.functions = &jvmti_Interface; duke@435: #endif duke@435: } duke@435: duke@435: duke@435: void duke@435: JvmtiEnvBase::dispose() { duke@435: duke@435: #ifdef JVMTI_TRACE duke@435: JvmtiTrace::shutdown(); duke@435: #endif duke@435: duke@435: // Dispose of event info and let the event controller call us back duke@435: // in a locked state (env_dispose, below) duke@435: JvmtiEventController::env_dispose(this); duke@435: } duke@435: duke@435: void duke@435: JvmtiEnvBase::env_dispose() { duke@435: assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check"); duke@435: duke@435: // We have been entered with all events disabled on this environment. duke@435: // A race to re-enable events (by setting callbacks) is prevented by duke@435: // checking for a valid environment when setting callbacks (while duke@435: // holding the JvmtiThreadState_lock). duke@435: duke@435: // Mark as invalid. duke@435: _magic = DISPOSED_MAGIC; duke@435: duke@435: // Relinquish all capabilities. duke@435: jvmtiCapabilities *caps = get_capabilities(); duke@435: JvmtiManageCapabilities::relinquish_capabilities(caps, caps, caps); duke@435: duke@435: // Same situation as with events (see above) duke@435: set_native_method_prefixes(0, NULL); duke@435: duke@435: #ifndef JVMTI_KERNEL duke@435: JvmtiTagMap* tag_map_to_deallocate = _tag_map; duke@435: set_tag_map(NULL); duke@435: // A tag map can be big, deallocate it now duke@435: if (tag_map_to_deallocate != NULL) { duke@435: delete tag_map_to_deallocate; duke@435: } duke@435: #endif // !JVMTI_KERNEL duke@435: duke@435: _needs_clean_up = true; duke@435: } duke@435: duke@435: duke@435: JvmtiEnvBase::~JvmtiEnvBase() { duke@435: assert(SafepointSynchronize::is_at_safepoint(), "sanity check"); duke@435: duke@435: // There is a small window of time during which the tag map of a duke@435: // disposed environment could have been reallocated. duke@435: // Make sure it is gone. duke@435: #ifndef JVMTI_KERNEL duke@435: JvmtiTagMap* tag_map_to_deallocate = _tag_map; duke@435: set_tag_map(NULL); duke@435: // A tag map can be big, deallocate it now duke@435: if (tag_map_to_deallocate != NULL) { duke@435: delete tag_map_to_deallocate; duke@435: } duke@435: #endif // !JVMTI_KERNEL duke@435: duke@435: _magic = BAD_MAGIC; duke@435: } duke@435: duke@435: duke@435: void duke@435: JvmtiEnvBase::periodic_clean_up() { duke@435: assert(SafepointSynchronize::is_at_safepoint(), "sanity check"); duke@435: duke@435: // JvmtiEnvBase reference is saved in JvmtiEnvThreadState. So duke@435: // clean up JvmtiThreadState before deleting JvmtiEnv pointer. duke@435: JvmtiThreadState::periodic_clean_up(); duke@435: duke@435: // Unlink all invalid environments from the list of environments duke@435: // and deallocate them duke@435: JvmtiEnvIterator it; duke@435: JvmtiEnvBase* previous_env = NULL; duke@435: JvmtiEnvBase* env = it.first(); duke@435: while (env != NULL) { duke@435: if (env->is_valid()) { duke@435: previous_env = env; duke@435: env = it.next(env); duke@435: } else { duke@435: // This one isn't valid, remove it from the list and deallocate it duke@435: JvmtiEnvBase* defunct_env = env; duke@435: env = it.next(env); duke@435: if (previous_env == NULL) { duke@435: _head_environment = env; duke@435: } else { duke@435: previous_env->set_next_environment(env); duke@435: } duke@435: delete defunct_env; duke@435: } duke@435: } duke@435: duke@435: } duke@435: duke@435: duke@435: void duke@435: JvmtiEnvBase::check_for_periodic_clean_up() { duke@435: assert(SafepointSynchronize::is_at_safepoint(), "sanity check"); duke@435: duke@435: class ThreadInsideIterationClosure: public ThreadClosure { duke@435: private: duke@435: bool _inside; duke@435: public: duke@435: ThreadInsideIterationClosure() : _inside(false) {}; duke@435: duke@435: void do_thread(Thread* thread) { duke@435: _inside |= thread->is_inside_jvmti_env_iteration(); duke@435: } duke@435: duke@435: bool is_inside_jvmti_env_iteration() { duke@435: return _inside; duke@435: } duke@435: }; duke@435: duke@435: if (_needs_clean_up) { duke@435: // Check if we are currently iterating environment, duke@435: // deallocation should not occur if we are duke@435: ThreadInsideIterationClosure tiic; duke@435: Threads::threads_do(&tiic); duke@435: if (!tiic.is_inside_jvmti_env_iteration() && duke@435: !is_inside_dying_thread_env_iteration()) { duke@435: _needs_clean_up = false; duke@435: JvmtiEnvBase::periodic_clean_up(); duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: void duke@435: JvmtiEnvBase::record_first_time_class_file_load_hook_enabled() { duke@435: assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), duke@435: "sanity check"); duke@435: duke@435: if (!_class_file_load_hook_ever_enabled) { duke@435: _class_file_load_hook_ever_enabled = true; duke@435: duke@435: if (get_capabilities()->can_retransform_classes) { duke@435: _is_retransformable = true; duke@435: } else { duke@435: _is_retransformable = false; duke@435: duke@435: // cannot add retransform capability after ClassFileLoadHook has been enabled duke@435: get_prohibited_capabilities()->can_retransform_classes = 1; duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: void duke@435: JvmtiEnvBase::record_class_file_load_hook_enabled() { duke@435: if (!_class_file_load_hook_ever_enabled) { duke@435: if (Threads::number_of_threads() == 0) { duke@435: record_first_time_class_file_load_hook_enabled(); duke@435: } else { duke@435: MutexLocker mu(JvmtiThreadState_lock); duke@435: record_first_time_class_file_load_hook_enabled(); duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: jvmtiError duke@435: JvmtiEnvBase::set_native_method_prefixes(jint prefix_count, char** prefixes) { duke@435: assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), duke@435: "sanity check"); duke@435: duke@435: int old_prefix_count = get_native_method_prefix_count(); duke@435: char **old_prefixes = get_native_method_prefixes(); duke@435: duke@435: // allocate and install the new prefixex duke@435: if (prefix_count == 0 || !is_valid()) { duke@435: _native_method_prefix_count = 0; duke@435: _native_method_prefixes = NULL; duke@435: } else { duke@435: // there are prefixes, allocate an array to hold them, and fill it duke@435: char** new_prefixes = (char**)os::malloc((prefix_count) * sizeof(char*)); duke@435: if (new_prefixes == NULL) { duke@435: return JVMTI_ERROR_OUT_OF_MEMORY; duke@435: } duke@435: for (int i = 0; i < prefix_count; i++) { duke@435: char* prefix = prefixes[i]; duke@435: if (prefix == NULL) { duke@435: for (int j = 0; j < (i-1); j++) { duke@435: os::free(new_prefixes[j]); duke@435: } duke@435: os::free(new_prefixes); duke@435: return JVMTI_ERROR_NULL_POINTER; duke@435: } duke@435: prefix = os::strdup(prefixes[i]); duke@435: if (prefix == NULL) { duke@435: for (int j = 0; j < (i-1); j++) { duke@435: os::free(new_prefixes[j]); duke@435: } duke@435: os::free(new_prefixes); duke@435: return JVMTI_ERROR_OUT_OF_MEMORY; duke@435: } duke@435: new_prefixes[i] = prefix; duke@435: } duke@435: _native_method_prefix_count = prefix_count; duke@435: _native_method_prefixes = new_prefixes; duke@435: } duke@435: duke@435: // now that we know the new prefixes have been successfully installed we can duke@435: // safely remove the old ones duke@435: if (old_prefix_count != 0) { duke@435: for (int i = 0; i < old_prefix_count; i++) { duke@435: os::free(old_prefixes[i]); duke@435: } duke@435: os::free(old_prefixes); duke@435: } duke@435: duke@435: return JVMTI_ERROR_NONE; duke@435: } duke@435: duke@435: duke@435: // Collect all the prefixes which have been set in any JVM TI environments duke@435: // by the SetNativeMethodPrefix(es) functions. Be sure to maintain the duke@435: // order of environments and the order of prefixes within each environment. duke@435: // Return in a resource allocated array. duke@435: char** duke@435: JvmtiEnvBase::get_all_native_method_prefixes(int* count_ptr) { duke@435: assert(Threads::number_of_threads() == 0 || duke@435: SafepointSynchronize::is_at_safepoint() || duke@435: JvmtiThreadState_lock->is_locked(), duke@435: "sanity check"); duke@435: duke@435: int total_count = 0; duke@435: GrowableArray* prefix_array =new GrowableArray(5); duke@435: duke@435: JvmtiEnvIterator it; duke@435: for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) { duke@435: int prefix_count = env->get_native_method_prefix_count(); duke@435: char** prefixes = env->get_native_method_prefixes(); duke@435: for (int j = 0; j < prefix_count; j++) { duke@435: // retrieve a prefix and so that it is safe against asynchronous changes duke@435: // copy it into the resource area duke@435: char* prefix = prefixes[j]; duke@435: char* prefix_copy = NEW_RESOURCE_ARRAY(char, strlen(prefix)+1); duke@435: strcpy(prefix_copy, prefix); duke@435: prefix_array->at_put_grow(total_count++, prefix_copy); duke@435: } duke@435: } duke@435: duke@435: char** all_prefixes = NEW_RESOURCE_ARRAY(char*, total_count); duke@435: char** p = all_prefixes; duke@435: for (int i = 0; i < total_count; ++i) { duke@435: *p++ = prefix_array->at(i); duke@435: } duke@435: *count_ptr = total_count; duke@435: return all_prefixes; duke@435: } duke@435: duke@435: void duke@435: JvmtiEnvBase::set_event_callbacks(const jvmtiEventCallbacks* callbacks, duke@435: jint size_of_callbacks) { duke@435: assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check"); duke@435: duke@435: size_t byte_cnt = sizeof(jvmtiEventCallbacks); duke@435: duke@435: // clear in either case to be sure we got any gap between sizes duke@435: memset(&_event_callbacks, 0, byte_cnt); duke@435: duke@435: // Now that JvmtiThreadState_lock is held, prevent a possible race condition where events duke@435: // are re-enabled by a call to set event callbacks where the DisposeEnvironment duke@435: // occurs after the boiler-plate environment check and before the lock is acquired. duke@435: if (callbacks != NULL && is_valid()) { duke@435: if (size_of_callbacks < (jint)byte_cnt) { duke@435: byte_cnt = size_of_callbacks; duke@435: } duke@435: memcpy(&_event_callbacks, callbacks, byte_cnt); duke@435: } duke@435: } duke@435: duke@435: // Called from JVMTI entry points which perform stack walking. If the duke@435: // associated JavaThread is the current thread, then wait_for_suspend duke@435: // is not used. Otherwise, it determines if we should wait for the duke@435: // "other" thread to complete external suspension. (NOTE: in future duke@435: // releases the suspension mechanism should be reimplemented so this duke@435: // is not necessary.) duke@435: // duke@435: bool duke@435: JvmtiEnvBase::is_thread_fully_suspended(JavaThread* thr, bool wait_for_suspend, uint32_t *bits) { duke@435: // "other" threads require special handling duke@435: if (thr != JavaThread::current()) { duke@435: if (wait_for_suspend) { duke@435: // We are allowed to wait for the external suspend to complete duke@435: // so give the other thread a chance to get suspended. duke@435: if (!thr->wait_for_ext_suspend_completion(SuspendRetryCount, duke@435: SuspendRetryDelay, bits)) { duke@435: // didn't make it so let the caller know duke@435: return false; duke@435: } duke@435: } duke@435: // We aren't allowed to wait for the external suspend to complete duke@435: // so if the other thread isn't externally suspended we need to duke@435: // let the caller know. duke@435: else if (!thr->is_ext_suspend_completed_with_lock(bits)) { duke@435: return false; duke@435: } duke@435: } duke@435: duke@435: return true; duke@435: } duke@435: duke@435: duke@435: // In the fullness of time, all users of the method should instead duke@435: // directly use allocate, besides being cleaner and faster, this will duke@435: // mean much better out of memory handling duke@435: unsigned char * duke@435: JvmtiEnvBase::jvmtiMalloc(jlong size) { duke@435: unsigned char* mem; duke@435: jvmtiError result = allocate(size, &mem); duke@435: assert(result == JVMTI_ERROR_NONE, "Allocate failed"); duke@435: return mem; duke@435: } duke@435: duke@435: duke@435: // duke@435: // Threads duke@435: // duke@435: duke@435: jobject * duke@435: JvmtiEnvBase::new_jobjectArray(int length, Handle *handles) { duke@435: if (length == 0) { duke@435: return NULL; duke@435: } duke@435: duke@435: jobject *objArray = (jobject *) jvmtiMalloc(sizeof(jobject) * length); duke@435: NULL_CHECK(objArray, NULL); duke@435: duke@435: for (int i=0; iis_a(SystemDictionary::thread_klass())) { duke@435: return NULL; duke@435: } duke@435: // The following returns NULL if the thread has not yet run or is in duke@435: // process of exiting duke@435: return java_lang_Thread::thread(t); duke@435: } duke@435: duke@435: duke@435: // update the access_flags for the field in the klass duke@435: void duke@435: JvmtiEnvBase::update_klass_field_access_flag(fieldDescriptor *fd) { duke@435: instanceKlass* ik = instanceKlass::cast(fd->field_holder()); duke@435: typeArrayOop fields = ik->fields(); duke@435: fields->ushort_at_put(fd->index(), (jushort)fd->access_flags().as_short()); duke@435: } duke@435: duke@435: duke@435: // return the vframe on the specified thread and depth, NULL if no such frame duke@435: vframe* duke@435: JvmtiEnvBase::vframeFor(JavaThread* java_thread, jint depth) { duke@435: if (!java_thread->has_last_Java_frame()) { duke@435: return NULL; duke@435: } duke@435: RegisterMap reg_map(java_thread); duke@435: vframe *vf = java_thread->last_java_vframe(®_map); duke@435: int d = 0; duke@435: while ((vf != NULL) && (d < depth)) { duke@435: vf = vf->java_sender(); duke@435: d++; duke@435: } duke@435: return vf; duke@435: } duke@435: duke@435: duke@435: // duke@435: // utilities: JNI objects duke@435: // duke@435: duke@435: duke@435: jclass duke@435: JvmtiEnvBase::get_jni_class_non_null(klassOop k) { duke@435: assert(k != NULL, "k != NULL"); duke@435: return (jclass)jni_reference(Klass::cast(k)->java_mirror()); duke@435: } duke@435: duke@435: #ifndef JVMTI_KERNEL duke@435: duke@435: // duke@435: // Field Information duke@435: // duke@435: duke@435: bool duke@435: JvmtiEnvBase::get_field_descriptor(klassOop k, jfieldID field, fieldDescriptor* fd) { duke@435: if (!jfieldIDWorkaround::is_valid_jfieldID(k, field)) { duke@435: return false; duke@435: } duke@435: bool found = false; duke@435: if (jfieldIDWorkaround::is_static_jfieldID(field)) { duke@435: JNIid* id = jfieldIDWorkaround::from_static_jfieldID(field); duke@435: int offset = id->offset(); duke@435: klassOop holder = id->holder(); duke@435: found = instanceKlass::cast(holder)->find_local_field_from_offset(offset, true, fd); duke@435: } else { duke@435: // Non-static field. The fieldID is really the offset of the field within the object. duke@435: int offset = jfieldIDWorkaround::from_instance_jfieldID(k, field); duke@435: found = instanceKlass::cast(k)->find_field_from_offset(offset, false, fd); duke@435: } duke@435: return found; duke@435: } duke@435: duke@435: // duke@435: // Object Monitor Information duke@435: // duke@435: duke@435: // duke@435: // Count the number of objects for a lightweight monitor. The hobj duke@435: // parameter is object that owns the monitor so this routine will duke@435: // count the number of times the same object was locked by frames duke@435: // in java_thread. duke@435: // duke@435: jint duke@435: JvmtiEnvBase::count_locked_objects(JavaThread *java_thread, Handle hobj) { duke@435: jint ret = 0; duke@435: if (!java_thread->has_last_Java_frame()) { duke@435: return ret; // no Java frames so no monitors duke@435: } duke@435: duke@435: ResourceMark rm; duke@435: HandleMark hm; duke@435: RegisterMap reg_map(java_thread); duke@435: duke@435: for(javaVFrame *jvf=java_thread->last_java_vframe(®_map); jvf != NULL; duke@435: jvf = jvf->java_sender()) { duke@435: GrowableArray* mons = jvf->monitors(); duke@435: if (!mons->is_empty()) { duke@435: for (int i = 0; i < mons->length(); i++) { duke@435: MonitorInfo *mi = mons->at(i); duke@435: duke@435: // see if owner of the monitor is our object duke@435: if (mi->owner() != NULL && mi->owner() == hobj()) { duke@435: ret++; duke@435: } duke@435: } duke@435: } duke@435: } duke@435: return ret; duke@435: } duke@435: duke@435: duke@435: duke@435: jvmtiError duke@435: JvmtiEnvBase::get_current_contended_monitor(JavaThread *calling_thread, JavaThread *java_thread, jobject *monitor_ptr) { duke@435: #ifdef ASSERT duke@435: uint32_t debug_bits = 0; duke@435: #endif duke@435: assert((SafepointSynchronize::is_at_safepoint() || duke@435: is_thread_fully_suspended(java_thread, false, &debug_bits)), duke@435: "at safepoint or target thread is suspended"); duke@435: oop obj = NULL; duke@435: ObjectMonitor *mon = java_thread->current_waiting_monitor(); duke@435: if (mon == NULL) { duke@435: // thread is not doing an Object.wait() call duke@435: mon = java_thread->current_pending_monitor(); duke@435: if (mon != NULL) { duke@435: // The thread is trying to enter() or raw_enter() an ObjectMonitor. duke@435: obj = (oop)mon->object(); duke@435: // If obj == NULL, then ObjectMonitor is raw which doesn't count duke@435: // as contended for this API duke@435: } duke@435: // implied else: no contended ObjectMonitor duke@435: } else { duke@435: // thread is doing an Object.wait() call duke@435: obj = (oop)mon->object(); duke@435: assert(obj != NULL, "Object.wait() should have an object"); duke@435: } duke@435: duke@435: if (obj == NULL) { duke@435: *monitor_ptr = NULL; duke@435: } else { duke@435: HandleMark hm; duke@435: Handle hobj(obj); duke@435: *monitor_ptr = jni_reference(calling_thread, hobj); duke@435: } duke@435: return JVMTI_ERROR_NONE; duke@435: } duke@435: duke@435: duke@435: jvmtiError duke@435: JvmtiEnvBase::get_owned_monitors(JavaThread *calling_thread, JavaThread* java_thread, duke@435: GrowableArray *owned_monitors_list) { duke@435: jvmtiError err = JVMTI_ERROR_NONE; duke@435: #ifdef ASSERT duke@435: uint32_t debug_bits = 0; duke@435: #endif duke@435: assert((SafepointSynchronize::is_at_safepoint() || duke@435: is_thread_fully_suspended(java_thread, false, &debug_bits)), duke@435: "at safepoint or target thread is suspended"); duke@435: duke@435: if (java_thread->has_last_Java_frame()) { duke@435: ResourceMark rm; duke@435: HandleMark hm; duke@435: RegisterMap reg_map(java_thread); duke@435: duke@435: int depth = 0; duke@435: for (javaVFrame *jvf = java_thread->last_java_vframe(®_map); jvf != NULL; duke@435: jvf = jvf->java_sender()) { duke@435: if (depth++ < MaxJavaStackTraceDepth) { // check for stack too deep duke@435: // add locked objects for this frame into list duke@435: err = get_locked_objects_in_frame(calling_thread, java_thread, jvf, owned_monitors_list, depth-1); duke@435: if (err != JVMTI_ERROR_NONE) { duke@435: return err; duke@435: } duke@435: } duke@435: } duke@435: } duke@435: duke@435: // Get off stack monitors. (e.g. acquired via jni MonitorEnter). duke@435: JvmtiMonitorClosure jmc(java_thread, calling_thread, owned_monitors_list, this); duke@435: ObjectSynchronizer::monitors_iterate(&jmc); duke@435: err = jmc.error(); duke@435: duke@435: return err; duke@435: } duke@435: duke@435: // Save JNI local handles for any objects that this frame owns. duke@435: jvmtiError duke@435: JvmtiEnvBase::get_locked_objects_in_frame(JavaThread* calling_thread, JavaThread* java_thread, duke@435: javaVFrame *jvf, GrowableArray* owned_monitors_list, int stack_depth) { duke@435: jvmtiError err = JVMTI_ERROR_NONE; duke@435: ResourceMark rm; duke@435: duke@435: GrowableArray* mons = jvf->monitors(); duke@435: if (mons->is_empty()) { duke@435: return err; // this javaVFrame holds no monitors duke@435: } duke@435: duke@435: HandleMark hm; duke@435: oop wait_obj = NULL; duke@435: { duke@435: // save object of current wait() call (if any) for later comparison duke@435: ObjectMonitor *mon = java_thread->current_waiting_monitor(); duke@435: if (mon != NULL) { duke@435: wait_obj = (oop)mon->object(); duke@435: } duke@435: } duke@435: oop pending_obj = NULL; duke@435: { duke@435: // save object of current enter() call (if any) for later comparison duke@435: ObjectMonitor *mon = java_thread->current_pending_monitor(); duke@435: if (mon != NULL) { duke@435: pending_obj = (oop)mon->object(); duke@435: } duke@435: } duke@435: duke@435: for (int i = 0; i < mons->length(); i++) { duke@435: MonitorInfo *mi = mons->at(i); duke@435: duke@435: oop obj = mi->owner(); duke@435: if (obj == NULL) { duke@435: // this monitor doesn't have an owning object so skip it duke@435: continue; duke@435: } duke@435: duke@435: if (wait_obj == obj) { duke@435: // the thread is waiting on this monitor so it isn't really owned duke@435: continue; duke@435: } duke@435: duke@435: if (pending_obj == obj) { duke@435: // the thread is pending on this monitor so it isn't really owned duke@435: continue; duke@435: } duke@435: duke@435: if (owned_monitors_list->length() > 0) { duke@435: // Our list has at least one object on it so we have to check duke@435: // for recursive object locking duke@435: bool found = false; duke@435: for (int j = 0; j < owned_monitors_list->length(); j++) { duke@435: jobject jobj = ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(j))->monitor; duke@435: oop check = JNIHandles::resolve(jobj); duke@435: if (check == obj) { duke@435: found = true; // we found the object duke@435: break; duke@435: } duke@435: } duke@435: duke@435: if (found) { duke@435: // already have this object so don't include it duke@435: continue; duke@435: } duke@435: } duke@435: duke@435: // add the owning object to our list duke@435: jvmtiMonitorStackDepthInfo *jmsdi; duke@435: err = allocate(sizeof(jvmtiMonitorStackDepthInfo), (unsigned char **)&jmsdi); duke@435: if (err != JVMTI_ERROR_NONE) { duke@435: return err; duke@435: } duke@435: Handle hobj(obj); duke@435: jmsdi->monitor = jni_reference(calling_thread, hobj); duke@435: jmsdi->stack_depth = stack_depth; duke@435: owned_monitors_list->append(jmsdi); duke@435: } duke@435: duke@435: return err; duke@435: } duke@435: duke@435: jvmtiError duke@435: JvmtiEnvBase::get_stack_trace(JavaThread *java_thread, duke@435: jint start_depth, jint max_count, duke@435: jvmtiFrameInfo* frame_buffer, jint* count_ptr) { duke@435: #ifdef ASSERT duke@435: uint32_t debug_bits = 0; duke@435: #endif duke@435: assert((SafepointSynchronize::is_at_safepoint() || duke@435: is_thread_fully_suspended(java_thread, false, &debug_bits)), duke@435: "at safepoint or target thread is suspended"); duke@435: int count = 0; duke@435: if (java_thread->has_last_Java_frame()) { duke@435: RegisterMap reg_map(java_thread); duke@435: Thread* current_thread = Thread::current(); duke@435: ResourceMark rm(current_thread); duke@435: javaVFrame *jvf = java_thread->last_java_vframe(®_map); duke@435: HandleMark hm(current_thread); duke@435: if (start_depth != 0) { duke@435: if (start_depth > 0) { duke@435: for (int j = 0; j < start_depth && jvf != NULL; j++) { duke@435: jvf = jvf->java_sender(); duke@435: } duke@435: if (jvf == NULL) { duke@435: // start_depth is deeper than the stack depth duke@435: return JVMTI_ERROR_ILLEGAL_ARGUMENT; duke@435: } duke@435: } else { // start_depth < 0 duke@435: // we are referencing the starting depth based on the oldest duke@435: // part of the stack. duke@435: // optimize to limit the number of times that java_sender() is called duke@435: javaVFrame *jvf_cursor = jvf; duke@435: javaVFrame *jvf_prev = NULL; duke@435: javaVFrame *jvf_prev_prev; duke@435: int j = 0; duke@435: while (jvf_cursor != NULL) { duke@435: jvf_prev_prev = jvf_prev; duke@435: jvf_prev = jvf_cursor; duke@435: for (j = 0; j > start_depth && jvf_cursor != NULL; j--) { duke@435: jvf_cursor = jvf_cursor->java_sender(); duke@435: } duke@435: } duke@435: if (j == start_depth) { duke@435: // previous pointer is exactly where we want to start duke@435: jvf = jvf_prev; duke@435: } else { duke@435: // we need to back up further to get to the right place duke@435: if (jvf_prev_prev == NULL) { duke@435: // the -start_depth is greater than the stack depth duke@435: return JVMTI_ERROR_ILLEGAL_ARGUMENT; duke@435: } duke@435: // j now is the number of frames on the stack starting with duke@435: // jvf_prev, we start from jvf_prev_prev and move older on duke@435: // the stack that many, the result is -start_depth frames duke@435: // remaining. duke@435: jvf = jvf_prev_prev; duke@435: for (; j < 0; j++) { duke@435: jvf = jvf->java_sender(); duke@435: } duke@435: } duke@435: } duke@435: } duke@435: for (; count < max_count && jvf != NULL; count++) { duke@435: frame_buffer[count].method = jvf->method()->jmethod_id(); duke@435: frame_buffer[count].location = (jvf->method()->is_native() ? -1 : jvf->bci()); duke@435: jvf = jvf->java_sender(); duke@435: } duke@435: } else { duke@435: if (start_depth != 0) { duke@435: // no frames and there is a starting depth duke@435: return JVMTI_ERROR_ILLEGAL_ARGUMENT; duke@435: } duke@435: } duke@435: *count_ptr = count; duke@435: return JVMTI_ERROR_NONE; duke@435: } duke@435: duke@435: jvmtiError duke@435: JvmtiEnvBase::get_frame_count(JvmtiThreadState *state, jint *count_ptr) { duke@435: assert((state != NULL), duke@435: "JavaThread should create JvmtiThreadState before calling this method"); duke@435: *count_ptr = state->count_frames(); duke@435: return JVMTI_ERROR_NONE; duke@435: } duke@435: duke@435: jvmtiError duke@435: JvmtiEnvBase::get_frame_location(JavaThread *java_thread, jint depth, duke@435: jmethodID* method_ptr, jlocation* location_ptr) { duke@435: #ifdef ASSERT duke@435: uint32_t debug_bits = 0; duke@435: #endif duke@435: assert((SafepointSynchronize::is_at_safepoint() || duke@435: is_thread_fully_suspended(java_thread, false, &debug_bits)), duke@435: "at safepoint or target thread is suspended"); duke@435: Thread* current_thread = Thread::current(); duke@435: ResourceMark rm(current_thread); duke@435: duke@435: vframe *vf = vframeFor(java_thread, depth); duke@435: if (vf == NULL) { duke@435: return JVMTI_ERROR_NO_MORE_FRAMES; duke@435: } duke@435: duke@435: // vframeFor should return a java frame. If it doesn't duke@435: // it means we've got an internal error and we return the duke@435: // error in product mode. In debug mode we will instead duke@435: // attempt to cast the vframe to a javaVFrame and will duke@435: // cause an assertion/crash to allow further diagnosis. duke@435: #ifdef PRODUCT duke@435: if (!vf->is_java_frame()) { duke@435: return JVMTI_ERROR_INTERNAL; duke@435: } duke@435: #endif duke@435: duke@435: HandleMark hm(current_thread); duke@435: javaVFrame *jvf = javaVFrame::cast(vf); duke@435: methodOop method = jvf->method(); duke@435: if (method->is_native()) { duke@435: *location_ptr = -1; duke@435: } else { duke@435: *location_ptr = jvf->bci(); duke@435: } duke@435: *method_ptr = method->jmethod_id(); duke@435: duke@435: return JVMTI_ERROR_NONE; duke@435: } duke@435: duke@435: duke@435: jvmtiError duke@435: JvmtiEnvBase::get_object_monitor_usage(JavaThread* calling_thread, jobject object, jvmtiMonitorUsage* info_ptr) { duke@435: HandleMark hm; duke@435: Handle hobj; duke@435: duke@435: bool at_safepoint = SafepointSynchronize::is_at_safepoint(); duke@435: duke@435: // Check arguments duke@435: { duke@435: oop mirror = JNIHandles::resolve_external_guard(object); duke@435: NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT); duke@435: NULL_CHECK(info_ptr, JVMTI_ERROR_NULL_POINTER); duke@435: duke@435: hobj = Handle(mirror); duke@435: } duke@435: duke@435: JavaThread *owning_thread = NULL; duke@435: ObjectMonitor *mon = NULL; duke@435: jvmtiMonitorUsage ret = { duke@435: NULL, 0, 0, NULL, 0, NULL duke@435: }; duke@435: duke@435: uint32_t debug_bits = 0; duke@435: // first derive the object's owner and entry_count (if any) duke@435: { duke@435: // Revoke any biases before querying the mark word duke@435: if (SafepointSynchronize::is_at_safepoint()) { duke@435: BiasedLocking::revoke_at_safepoint(hobj); duke@435: } else { duke@435: BiasedLocking::revoke_and_rebias(hobj, false, calling_thread); duke@435: } duke@435: duke@435: address owner = NULL; duke@435: { duke@435: markOop mark = hobj()->mark(); duke@435: duke@435: if (!mark->has_monitor()) { duke@435: // this object has a lightweight monitor duke@435: duke@435: if (mark->has_locker()) { duke@435: owner = (address)mark->locker(); // save the address of the Lock word duke@435: } duke@435: // implied else: no owner duke@435: } else { duke@435: // this object has a heavyweight monitor duke@435: mon = mark->monitor(); duke@435: duke@435: // The owner field of a heavyweight monitor may be NULL for no duke@435: // owner, a JavaThread * or it may still be the address of the duke@435: // Lock word in a JavaThread's stack. A monitor can be inflated duke@435: // by a non-owning JavaThread, but only the owning JavaThread duke@435: // can change the owner field from the Lock word to the duke@435: // JavaThread * and it may not have done that yet. duke@435: owner = (address)mon->owner(); duke@435: } duke@435: } duke@435: duke@435: if (owner != NULL) { duke@435: // This monitor is owned so we have to find the owning JavaThread. duke@435: // Since owning_thread_from_monitor_owner() grabs a lock, GC can duke@435: // move our object at this point. However, our owner value is safe duke@435: // since it is either the Lock word on a stack or a JavaThread *. duke@435: owning_thread = Threads::owning_thread_from_monitor_owner(owner, !at_safepoint); duke@435: assert(owning_thread != NULL, "sanity check"); duke@435: if (owning_thread != NULL) { // robustness duke@435: // The monitor's owner either has to be the current thread, at safepoint duke@435: // or it has to be suspended. Any of these conditions will prevent both duke@435: // contending and waiting threads from modifying the state of duke@435: // the monitor. duke@435: if (!at_safepoint && !JvmtiEnv::is_thread_fully_suspended(owning_thread, true, &debug_bits)) { duke@435: return JVMTI_ERROR_THREAD_NOT_SUSPENDED; duke@435: } duke@435: HandleMark hm; duke@435: Handle th(owning_thread->threadObj()); duke@435: ret.owner = (jthread)jni_reference(calling_thread, th); duke@435: } duke@435: // implied else: no owner duke@435: } duke@435: duke@435: if (owning_thread != NULL) { // monitor is owned duke@435: if ((address)owning_thread == owner) { duke@435: // the owner field is the JavaThread * duke@435: assert(mon != NULL, duke@435: "must have heavyweight monitor with JavaThread * owner"); duke@435: ret.entry_count = mon->recursions() + 1; duke@435: } else { duke@435: // The owner field is the Lock word on the JavaThread's stack duke@435: // so the recursions field is not valid. We have to count the duke@435: // number of recursive monitor entries the hard way. We pass duke@435: // a handle to survive any GCs along the way. duke@435: ResourceMark rm; duke@435: ret.entry_count = count_locked_objects(owning_thread, hobj); duke@435: } duke@435: } duke@435: // implied else: entry_count == 0 duke@435: } duke@435: duke@435: int nWant,nWait; duke@435: if (mon != NULL) { duke@435: // this object has a heavyweight monitor duke@435: nWant = mon->contentions(); // # of threads contending for monitor duke@435: nWait = mon->waiters(); // # of threads in Object.wait() duke@435: ret.waiter_count = nWant + nWait; duke@435: ret.notify_waiter_count = nWait; duke@435: } else { duke@435: // this object has a lightweight monitor duke@435: ret.waiter_count = 0; duke@435: ret.notify_waiter_count = 0; duke@435: } duke@435: duke@435: // Allocate memory for heavyweight and lightweight monitor. duke@435: jvmtiError err; duke@435: err = allocate(ret.waiter_count * sizeof(jthread *), (unsigned char**)&ret.waiters); duke@435: if (err != JVMTI_ERROR_NONE) { duke@435: return err; duke@435: } duke@435: err = allocate(ret.notify_waiter_count * sizeof(jthread *), duke@435: (unsigned char**)&ret.notify_waiters); duke@435: if (err != JVMTI_ERROR_NONE) { duke@435: deallocate((unsigned char*)ret.waiters); duke@435: return err; duke@435: } duke@435: duke@435: // now derive the rest of the fields duke@435: if (mon != NULL) { duke@435: // this object has a heavyweight monitor duke@435: duke@435: // Number of waiters may actually be less than the waiter count. duke@435: // So NULL out memory so that unused memory will be NULL. duke@435: memset(ret.waiters, 0, ret.waiter_count * sizeof(jthread *)); duke@435: memset(ret.notify_waiters, 0, ret.notify_waiter_count * sizeof(jthread *)); duke@435: duke@435: if (ret.waiter_count > 0) { duke@435: // we have contending and/or waiting threads duke@435: HandleMark hm; duke@435: if (nWant > 0) { duke@435: // we have contending threads duke@435: ResourceMark rm; duke@435: // get_pending_threads returns only java thread so we do not need to duke@435: // check for non java threads. duke@435: GrowableArray* wantList = Threads::get_pending_threads( duke@435: nWant, (address)mon, !at_safepoint); duke@435: if (wantList->length() < nWant) { duke@435: // robustness: the pending list has gotten smaller duke@435: nWant = wantList->length(); duke@435: } duke@435: for (int i = 0; i < nWant; i++) { duke@435: JavaThread *pending_thread = wantList->at(i); duke@435: // If the monitor has no owner, then a non-suspended contending duke@435: // thread could potentially change the state of the monitor by duke@435: // entering it. The JVM/TI spec doesn't allow this. duke@435: if (owning_thread == NULL && !at_safepoint & duke@435: !JvmtiEnv::is_thread_fully_suspended(pending_thread, true, &debug_bits)) { duke@435: if (ret.owner != NULL) { duke@435: destroy_jni_reference(calling_thread, ret.owner); duke@435: } duke@435: for (int j = 0; j < i; j++) { duke@435: destroy_jni_reference(calling_thread, ret.waiters[j]); duke@435: } duke@435: deallocate((unsigned char*)ret.waiters); duke@435: deallocate((unsigned char*)ret.notify_waiters); duke@435: return JVMTI_ERROR_THREAD_NOT_SUSPENDED; duke@435: } duke@435: Handle th(pending_thread->threadObj()); duke@435: ret.waiters[i] = (jthread)jni_reference(calling_thread, th); duke@435: } duke@435: } duke@435: if (nWait > 0) { duke@435: // we have threads in Object.wait() duke@435: int offset = nWant; // add after any contending threads duke@435: ObjectWaiter *waiter = mon->first_waiter(); duke@435: for (int i = 0, j = 0; i < nWait; i++) { duke@435: if (waiter == NULL) { duke@435: // robustness: the waiting list has gotten smaller duke@435: nWait = j; duke@435: break; duke@435: } duke@435: Thread *t = mon->thread_of_waiter(waiter); duke@435: if (t != NULL && t->is_Java_thread()) { duke@435: JavaThread *wjava_thread = (JavaThread *)t; duke@435: // If the thread was found on the ObjectWaiter list, then duke@435: // it has not been notified. This thread can't change the duke@435: // state of the monitor so it doesn't need to be suspended. duke@435: Handle th(wjava_thread->threadObj()); duke@435: ret.waiters[offset + j] = (jthread)jni_reference(calling_thread, th); duke@435: ret.notify_waiters[j++] = (jthread)jni_reference(calling_thread, th); duke@435: } duke@435: waiter = mon->next_waiter(waiter); duke@435: } duke@435: } duke@435: } duke@435: duke@435: // Adjust count. nWant and nWait count values may be less than original. duke@435: ret.waiter_count = nWant + nWait; duke@435: ret.notify_waiter_count = nWait; duke@435: } else { duke@435: // this object has a lightweight monitor and we have nothing more duke@435: // to do here because the defaults are just fine. duke@435: } duke@435: duke@435: // we don't update return parameter unless everything worked duke@435: *info_ptr = ret; duke@435: duke@435: return JVMTI_ERROR_NONE; duke@435: } duke@435: duke@435: ResourceTracker::ResourceTracker(JvmtiEnv* env) { duke@435: _env = env; duke@435: _allocations = new (ResourceObj::C_HEAP) GrowableArray(20, true); duke@435: _failed = false; duke@435: } duke@435: ResourceTracker::~ResourceTracker() { duke@435: if (_failed) { duke@435: for (int i=0; i<_allocations->length(); i++) { duke@435: _env->deallocate(_allocations->at(i)); duke@435: } duke@435: } duke@435: delete _allocations; duke@435: } duke@435: duke@435: jvmtiError ResourceTracker::allocate(jlong size, unsigned char** mem_ptr) { duke@435: unsigned char *ptr; duke@435: jvmtiError err = _env->allocate(size, &ptr); duke@435: if (err == JVMTI_ERROR_NONE) { duke@435: _allocations->append(ptr); duke@435: *mem_ptr = ptr; duke@435: } else { duke@435: *mem_ptr = NULL; duke@435: _failed = true; duke@435: } duke@435: return err; duke@435: } duke@435: duke@435: unsigned char* ResourceTracker::allocate(jlong size) { duke@435: unsigned char* ptr; duke@435: allocate(size, &ptr); duke@435: return ptr; duke@435: } duke@435: duke@435: char* ResourceTracker::strdup(const char* str) { duke@435: char *dup_str = (char*)allocate(strlen(str)+1); duke@435: if (dup_str != NULL) { duke@435: strcpy(dup_str, str); duke@435: } duke@435: return dup_str; duke@435: } duke@435: duke@435: struct StackInfoNode { duke@435: struct StackInfoNode *next; duke@435: jvmtiStackInfo info; duke@435: }; duke@435: duke@435: // Create a jvmtiStackInfo inside a linked list node and create a duke@435: // buffer for the frame information, both allocated as resource objects. duke@435: // Fill in both the jvmtiStackInfo and the jvmtiFrameInfo. duke@435: // Note that either or both of thr and thread_oop duke@435: // may be null if the thread is new or has exited. duke@435: void duke@435: VM_GetMultipleStackTraces::fill_frames(jthread jt, JavaThread *thr, oop thread_oop) { duke@435: assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); duke@435: duke@435: jint state = 0; duke@435: struct StackInfoNode *node = NEW_RESOURCE_OBJ(struct StackInfoNode); duke@435: jvmtiStackInfo *infop = &(node->info); duke@435: node->next = head(); duke@435: set_head(node); duke@435: infop->frame_count = 0; duke@435: infop->thread = jt; duke@435: duke@435: if (thread_oop != NULL) { duke@435: // get most state bits duke@435: state = (jint)java_lang_Thread::get_thread_status(thread_oop); duke@435: } duke@435: duke@435: if (thr != NULL) { // add more state bits if there is a JavaThead to query duke@435: // same as is_being_ext_suspended() but without locking duke@435: if (thr->is_ext_suspended() || thr->is_external_suspend()) { duke@435: state |= JVMTI_THREAD_STATE_SUSPENDED; duke@435: } duke@435: JavaThreadState jts = thr->thread_state(); duke@435: if (jts == _thread_in_native) { duke@435: state |= JVMTI_THREAD_STATE_IN_NATIVE; duke@435: } duke@435: OSThread* osThread = thr->osthread(); duke@435: if (osThread != NULL && osThread->interrupted()) { duke@435: state |= JVMTI_THREAD_STATE_INTERRUPTED; duke@435: } duke@435: } duke@435: infop->state = state; duke@435: duke@435: if (thr != NULL || (state & JVMTI_THREAD_STATE_ALIVE) != 0) { duke@435: infop->frame_buffer = NEW_RESOURCE_ARRAY(jvmtiFrameInfo, max_frame_count()); duke@435: env()->get_stack_trace(thr, 0, max_frame_count(), duke@435: infop->frame_buffer, &(infop->frame_count)); duke@435: } else { duke@435: infop->frame_buffer = NULL; duke@435: infop->frame_count = 0; duke@435: } duke@435: _frame_count_total += infop->frame_count; duke@435: } duke@435: duke@435: // Based on the stack information in the linked list, allocate memory duke@435: // block to return and fill it from the info in the linked list. duke@435: void duke@435: VM_GetMultipleStackTraces::allocate_and_fill_stacks(jint thread_count) { duke@435: // do I need to worry about alignment issues? duke@435: jlong alloc_size = thread_count * sizeof(jvmtiStackInfo) duke@435: + _frame_count_total * sizeof(jvmtiFrameInfo); duke@435: env()->allocate(alloc_size, (unsigned char **)&_stack_info); duke@435: duke@435: // pointers to move through the newly allocated space as it is filled in duke@435: jvmtiStackInfo *si = _stack_info + thread_count; // bottom of stack info duke@435: jvmtiFrameInfo *fi = (jvmtiFrameInfo *)si; // is the top of frame info duke@435: duke@435: // copy information in resource area into allocated buffer duke@435: // insert stack info backwards since linked list is backwards duke@435: // insert frame info forwards duke@435: // walk the StackInfoNodes duke@435: for (struct StackInfoNode *sin = head(); sin != NULL; sin = sin->next) { duke@435: jint frame_count = sin->info.frame_count; duke@435: size_t frames_size = frame_count * sizeof(jvmtiFrameInfo); duke@435: --si; duke@435: memcpy(si, &(sin->info), sizeof(jvmtiStackInfo)); duke@435: if (frames_size == 0) { duke@435: si->frame_buffer = NULL; duke@435: } else { duke@435: memcpy(fi, sin->info.frame_buffer, frames_size); duke@435: si->frame_buffer = fi; // point to the new allocated copy of the frames duke@435: fi += frame_count; duke@435: } duke@435: } duke@435: assert(si == _stack_info, "the last copied stack info must be the first record"); duke@435: assert((unsigned char *)fi == ((unsigned char *)_stack_info) + alloc_size, duke@435: "the last copied frame info must be the last record"); duke@435: } duke@435: duke@435: duke@435: void duke@435: VM_GetThreadListStackTraces::doit() { duke@435: assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); duke@435: duke@435: ResourceMark rm; duke@435: for (int i = 0; i < _thread_count; ++i) { duke@435: jthread jt = _thread_list[i]; duke@435: oop thread_oop = JNIHandles::resolve_external_guard(jt); duke@435: if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::thread_klass())) { duke@435: set_result(JVMTI_ERROR_INVALID_THREAD); duke@435: return; duke@435: } duke@435: fill_frames(jt, java_lang_Thread::thread(thread_oop), thread_oop); duke@435: } duke@435: allocate_and_fill_stacks(_thread_count); duke@435: } duke@435: duke@435: void duke@435: VM_GetAllStackTraces::doit() { duke@435: assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); duke@435: duke@435: ResourceMark rm; duke@435: _final_thread_count = 0; duke@435: for (JavaThread *jt = Threads::first(); jt != NULL; jt = jt->next()) { duke@435: oop thread_oop = jt->threadObj(); duke@435: if (thread_oop != NULL && duke@435: !jt->is_exiting() && duke@435: java_lang_Thread::is_alive(thread_oop) && duke@435: !jt->is_hidden_from_external_view()) { duke@435: ++_final_thread_count; duke@435: // Handle block of the calling thread is used to create local refs. duke@435: fill_frames((jthread)JNIHandles::make_local(_calling_thread, thread_oop), duke@435: jt, thread_oop); duke@435: } duke@435: } duke@435: allocate_and_fill_stacks(_final_thread_count); duke@435: } duke@435: duke@435: // Verifies that the top frame is a java frame in an expected state. duke@435: // Deoptimizes frame if needed. duke@435: // Checks that the frame method signature matches the return type (tos). duke@435: // HandleMark must be defined in the caller only. duke@435: // It is to keep a ret_ob_h handle alive after return to the caller. duke@435: jvmtiError duke@435: JvmtiEnvBase::check_top_frame(JavaThread* current_thread, JavaThread* java_thread, duke@435: jvalue value, TosState tos, Handle* ret_ob_h) { duke@435: ResourceMark rm(current_thread); duke@435: duke@435: vframe *vf = vframeFor(java_thread, 0); duke@435: NULL_CHECK(vf, JVMTI_ERROR_NO_MORE_FRAMES); duke@435: duke@435: javaVFrame *jvf = (javaVFrame*) vf; duke@435: if (!vf->is_java_frame() || jvf->method()->is_native()) { duke@435: return JVMTI_ERROR_OPAQUE_FRAME; duke@435: } duke@435: duke@435: // If the frame is a compiled one, need to deoptimize it. duke@435: if (vf->is_compiled_frame()) { duke@435: if (!vf->fr().can_be_deoptimized()) { duke@435: return JVMTI_ERROR_OPAQUE_FRAME; duke@435: } duke@435: VM_DeoptimizeFrame deopt(java_thread, jvf->fr().id()); duke@435: VMThread::execute(&deopt); duke@435: } duke@435: duke@435: // Get information about method return type duke@435: symbolHandle signature(current_thread, jvf->method()->signature()); duke@435: duke@435: ResultTypeFinder rtf(signature); duke@435: TosState fr_tos = as_TosState(rtf.type()); duke@435: if (fr_tos != tos) { duke@435: if (tos != itos || (fr_tos != btos && fr_tos != ctos && fr_tos != stos)) { duke@435: return JVMTI_ERROR_TYPE_MISMATCH; duke@435: } duke@435: } duke@435: duke@435: // Check that the jobject class matches the return type signature. duke@435: jobject jobj = value.l; duke@435: if (tos == atos && jobj != NULL) { // NULL reference is allowed duke@435: Handle ob_h = Handle(current_thread, JNIHandles::resolve_external_guard(jobj)); duke@435: NULL_CHECK(ob_h, JVMTI_ERROR_INVALID_OBJECT); duke@435: KlassHandle ob_kh = KlassHandle(current_thread, ob_h()->klass()); duke@435: NULL_CHECK(ob_kh, JVMTI_ERROR_INVALID_OBJECT); duke@435: duke@435: // Method return type signature. duke@435: char* ty_sign = 1 + strchr(signature->as_C_string(), ')'); duke@435: duke@435: if (!VM_GetOrSetLocal::is_assignable(ty_sign, Klass::cast(ob_kh()), current_thread)) { duke@435: return JVMTI_ERROR_TYPE_MISMATCH; duke@435: } duke@435: *ret_ob_h = ob_h; duke@435: } duke@435: return JVMTI_ERROR_NONE; duke@435: } /* end check_top_frame */ duke@435: duke@435: duke@435: // ForceEarlyReturn follows the PopFrame approach in many aspects. duke@435: // Main difference is on the last stage in the interpreter. duke@435: // The PopFrame stops method execution to continue execution duke@435: // from the same method call instruction. duke@435: // The ForceEarlyReturn forces return from method so the execution duke@435: // continues at the bytecode following the method call. duke@435: duke@435: // Threads_lock NOT held, java_thread not protected by lock duke@435: // java_thread - pre-checked duke@435: duke@435: jvmtiError duke@435: JvmtiEnvBase::force_early_return(JavaThread* java_thread, jvalue value, TosState tos) { duke@435: JavaThread* current_thread = JavaThread::current(); duke@435: HandleMark hm(current_thread); duke@435: uint32_t debug_bits = 0; duke@435: duke@435: // Check if java_thread is fully suspended duke@435: if (!is_thread_fully_suspended(java_thread, duke@435: true /* wait for suspend completion */, duke@435: &debug_bits)) { duke@435: return JVMTI_ERROR_THREAD_NOT_SUSPENDED; duke@435: } duke@435: duke@435: // retreive or create the state duke@435: JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread); duke@435: duke@435: // Check to see if a ForceEarlyReturn was already in progress duke@435: if (state->is_earlyret_pending()) { duke@435: // Probably possible for JVMTI clients to trigger this, but the duke@435: // JPDA backend shouldn't allow this to happen duke@435: return JVMTI_ERROR_INTERNAL; duke@435: } duke@435: { duke@435: // The same as for PopFrame. Workaround bug: duke@435: // 4812902: popFrame hangs if the method is waiting at a synchronize duke@435: // Catch this condition and return an error to avoid hanging. duke@435: // Now JVMTI spec allows an implementation to bail out with an opaque duke@435: // frame error. duke@435: OSThread* osThread = java_thread->osthread(); duke@435: if (osThread->get_state() == MONITOR_WAIT) { duke@435: return JVMTI_ERROR_OPAQUE_FRAME; duke@435: } duke@435: } duke@435: Handle ret_ob_h = Handle(); duke@435: jvmtiError err = check_top_frame(current_thread, java_thread, value, tos, &ret_ob_h); duke@435: if (err != JVMTI_ERROR_NONE) { duke@435: return err; duke@435: } duke@435: assert(tos != atos || value.l == NULL || ret_ob_h() != NULL, duke@435: "return object oop must not be NULL if jobject is not NULL"); duke@435: duke@435: // Update the thread state to reflect that the top frame must be duke@435: // forced to return. duke@435: // The current frame will be returned later when the suspended duke@435: // thread is resumed and right before returning from VM to Java. duke@435: // (see call_VM_base() in assembler_.cpp). duke@435: duke@435: state->set_earlyret_pending(); duke@435: state->set_earlyret_oop(ret_ob_h()); duke@435: state->set_earlyret_value(value, tos); duke@435: duke@435: // Set pending step flag for this early return. duke@435: // It is cleared when next step event is posted. duke@435: state->set_pending_step_for_earlyret(); duke@435: duke@435: return JVMTI_ERROR_NONE; duke@435: } /* end force_early_return */ duke@435: duke@435: void duke@435: JvmtiMonitorClosure::do_monitor(ObjectMonitor* mon) { duke@435: if ( _error != JVMTI_ERROR_NONE) { duke@435: // Error occurred in previous iteration so no need to add duke@435: // to the list. duke@435: return; duke@435: } duke@435: if (mon->owner() == _java_thread ) { duke@435: // Filter out on stack monitors collected during stack walk. duke@435: oop obj = (oop)mon->object(); duke@435: bool found = false; duke@435: for (int j = 0; j < _owned_monitors_list->length(); j++) { duke@435: jobject jobj = ((jvmtiMonitorStackDepthInfo*)_owned_monitors_list->at(j))->monitor; duke@435: oop check = JNIHandles::resolve(jobj); duke@435: if (check == obj) { duke@435: // On stack monitor already collected during the stack walk. duke@435: found = true; duke@435: break; duke@435: } duke@435: } duke@435: if (found == false) { duke@435: // This is off stack monitor (e.g. acquired via jni MonitorEnter). duke@435: jvmtiError err; duke@435: jvmtiMonitorStackDepthInfo *jmsdi; duke@435: err = _env->allocate(sizeof(jvmtiMonitorStackDepthInfo), (unsigned char **)&jmsdi); duke@435: if (err != JVMTI_ERROR_NONE) { duke@435: _error = err; duke@435: return; duke@435: } duke@435: Handle hobj(obj); duke@435: jmsdi->monitor = _env->jni_reference(_calling_thread, hobj); duke@435: // stack depth is unknown for this monitor. duke@435: jmsdi->stack_depth = -1; duke@435: _owned_monitors_list->append(jmsdi); duke@435: } duke@435: } duke@435: } duke@435: duke@435: #endif // !JVMTI_KERNEL