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