aoqi@0: /* aoqi@0: * Copyright (c) 2003, 2014, 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 "code/nmethod.hpp" aoqi@0: #include "code/pcDesc.hpp" aoqi@0: #include "code/scopeDesc.hpp" aoqi@0: #include "interpreter/interpreter.hpp" aoqi@0: #include "jvmtifiles/jvmtiEnv.hpp" aoqi@0: #include "memory/resourceArea.hpp" aoqi@0: #include "oops/objArrayKlass.hpp" aoqi@0: #include "oops/objArrayOop.hpp" aoqi@0: #include "prims/jvmtiCodeBlobEvents.hpp" aoqi@0: #include "prims/jvmtiEventController.hpp" aoqi@0: #include "prims/jvmtiEventController.inline.hpp" aoqi@0: #include "prims/jvmtiExport.hpp" aoqi@0: #include "prims/jvmtiImpl.hpp" aoqi@0: #include "prims/jvmtiManageCapabilities.hpp" aoqi@0: #include "prims/jvmtiRawMonitor.hpp" aoqi@0: #include "prims/jvmtiTagMap.hpp" aoqi@0: #include "prims/jvmtiThreadState.inline.hpp" aoqi@0: #include "prims/jvmtiRedefineClasses.hpp" aoqi@0: #include "runtime/arguments.hpp" aoqi@0: #include "runtime/handles.hpp" aoqi@0: #include "runtime/interfaceSupport.hpp" aoqi@0: #include "runtime/objectMonitor.hpp" aoqi@0: #include "runtime/objectMonitor.inline.hpp" aoqi@0: #include "runtime/thread.hpp" aoqi@0: #include "runtime/vframe.hpp" aoqi@0: #include "services/attachListener.hpp" aoqi@0: #include "services/serviceUtil.hpp" aoqi@0: #include "utilities/macros.hpp" aoqi@0: #if INCLUDE_ALL_GCS aoqi@0: #include "gc_implementation/parallelScavenge/psMarkSweep.hpp" aoqi@0: #endif // INCLUDE_ALL_GCS aoqi@0: aoqi@0: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC aoqi@0: aoqi@0: #ifdef JVMTI_TRACE aoqi@0: #define EVT_TRACE(evt,out) if ((JvmtiTrace::event_trace_flags(evt) & JvmtiTrace::SHOW_EVENT_SENT) != 0) { SafeResourceMark rm; tty->print_cr out; } aoqi@0: #define EVT_TRIG_TRACE(evt,out) if ((JvmtiTrace::event_trace_flags(evt) & JvmtiTrace::SHOW_EVENT_TRIGGER) != 0) { SafeResourceMark rm; tty->print_cr out; } aoqi@0: #else aoqi@0: #define EVT_TRIG_TRACE(evt,out) aoqi@0: #define EVT_TRACE(evt,out) aoqi@0: #endif aoqi@0: aoqi@0: /////////////////////////////////////////////////////////////// aoqi@0: // aoqi@0: // JvmtiEventTransition aoqi@0: // aoqi@0: // TO DO -- aoqi@0: // more handle purging aoqi@0: aoqi@0: // Use this for JavaThreads and state is _thread_in_vm. aoqi@0: class JvmtiJavaThreadEventTransition : StackObj { aoqi@0: private: aoqi@0: ResourceMark _rm; aoqi@0: ThreadToNativeFromVM _transition; aoqi@0: HandleMark _hm; aoqi@0: aoqi@0: public: aoqi@0: JvmtiJavaThreadEventTransition(JavaThread *thread) : aoqi@0: _rm(), aoqi@0: _transition(thread), aoqi@0: _hm(thread) {}; aoqi@0: }; aoqi@0: aoqi@0: // For JavaThreads which are not in _thread_in_vm state aoqi@0: // and other system threads use this. aoqi@0: class JvmtiThreadEventTransition : StackObj { aoqi@0: private: aoqi@0: ResourceMark _rm; aoqi@0: HandleMark _hm; aoqi@0: JavaThreadState _saved_state; aoqi@0: JavaThread *_jthread; aoqi@0: aoqi@0: public: aoqi@0: JvmtiThreadEventTransition(Thread *thread) : _rm(), _hm() { aoqi@0: if (thread->is_Java_thread()) { aoqi@0: _jthread = (JavaThread *)thread; aoqi@0: _saved_state = _jthread->thread_state(); aoqi@0: if (_saved_state == _thread_in_Java) { aoqi@0: ThreadStateTransition::transition_from_java(_jthread, _thread_in_native); aoqi@0: } else { aoqi@0: ThreadStateTransition::transition(_jthread, _saved_state, _thread_in_native); aoqi@0: } aoqi@0: } else { aoqi@0: _jthread = NULL; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: ~JvmtiThreadEventTransition() { aoqi@0: if (_jthread != NULL) aoqi@0: ThreadStateTransition::transition_from_native(_jthread, _saved_state); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: /////////////////////////////////////////////////////////////// aoqi@0: // aoqi@0: // JvmtiEventMark aoqi@0: // aoqi@0: aoqi@0: class JvmtiEventMark : public StackObj { aoqi@0: private: aoqi@0: JavaThread *_thread; aoqi@0: JNIEnv* _jni_env; aoqi@0: bool _exception_detected; aoqi@0: bool _exception_caught; aoqi@0: #if 0 aoqi@0: JNIHandleBlock* _hblock; aoqi@0: #endif aoqi@0: aoqi@0: public: aoqi@0: JvmtiEventMark(JavaThread *thread) : _thread(thread), aoqi@0: _jni_env(thread->jni_environment()) { aoqi@0: #if 0 aoqi@0: _hblock = thread->active_handles(); aoqi@0: _hblock->clear_thoroughly(); // so we can be safe aoqi@0: #else aoqi@0: // we want to use the code above - but that needs the JNIHandle changes - later... aoqi@0: // for now, steal JNI push local frame code aoqi@0: JvmtiThreadState *state = thread->jvmti_thread_state(); aoqi@0: // we are before an event. aoqi@0: // Save current jvmti thread exception state. aoqi@0: if (state != NULL) { aoqi@0: _exception_detected = state->is_exception_detected(); aoqi@0: _exception_caught = state->is_exception_caught(); aoqi@0: } else { aoqi@0: _exception_detected = false; aoqi@0: _exception_caught = false; aoqi@0: } aoqi@0: aoqi@0: JNIHandleBlock* old_handles = thread->active_handles(); aoqi@0: JNIHandleBlock* new_handles = JNIHandleBlock::allocate_block(thread); aoqi@0: assert(new_handles != NULL, "should not be NULL"); aoqi@0: new_handles->set_pop_frame_link(old_handles); aoqi@0: thread->set_active_handles(new_handles); aoqi@0: #endif aoqi@0: assert(thread == JavaThread::current(), "thread must be current!"); aoqi@0: thread->frame_anchor()->make_walkable(thread); aoqi@0: }; aoqi@0: aoqi@0: ~JvmtiEventMark() { aoqi@0: #if 0 aoqi@0: _hblock->clear(); // for consistency with future correct behavior aoqi@0: #else aoqi@0: // we want to use the code above - but that needs the JNIHandle changes - later... aoqi@0: // for now, steal JNI pop local frame code aoqi@0: JNIHandleBlock* old_handles = _thread->active_handles(); aoqi@0: JNIHandleBlock* new_handles = old_handles->pop_frame_link(); aoqi@0: assert(new_handles != NULL, "should not be NULL"); aoqi@0: _thread->set_active_handles(new_handles); aoqi@0: // Note that we set the pop_frame_link to NULL explicitly, otherwise aoqi@0: // the release_block call will release the blocks. aoqi@0: old_handles->set_pop_frame_link(NULL); aoqi@0: JNIHandleBlock::release_block(old_handles, _thread); // may block aoqi@0: #endif aoqi@0: aoqi@0: JvmtiThreadState* state = _thread->jvmti_thread_state(); aoqi@0: // we are continuing after an event. aoqi@0: if (state != NULL) { aoqi@0: // Restore the jvmti thread exception state. aoqi@0: if (_exception_detected) { aoqi@0: state->set_exception_detected(); aoqi@0: } aoqi@0: if (_exception_caught) { aoqi@0: state->set_exception_caught(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: #if 0 aoqi@0: jobject to_jobject(oop obj) { return obj == NULL? NULL : _hblock->allocate_handle_fast(obj); } aoqi@0: #else aoqi@0: // we want to use the code above - but that needs the JNIHandle changes - later... aoqi@0: // for now, use regular make_local aoqi@0: jobject to_jobject(oop obj) { return JNIHandles::make_local(_thread,obj); } aoqi@0: #endif aoqi@0: aoqi@0: jclass to_jclass(Klass* klass) { return (klass == NULL ? NULL : (jclass)to_jobject(klass->java_mirror())); } aoqi@0: aoqi@0: jmethodID to_jmethodID(methodHandle method) { return method->jmethod_id(); } aoqi@0: aoqi@0: JNIEnv* jni_env() { return _jni_env; } aoqi@0: }; aoqi@0: aoqi@0: class JvmtiThreadEventMark : public JvmtiEventMark { aoqi@0: private: aoqi@0: jthread _jt; aoqi@0: aoqi@0: public: aoqi@0: JvmtiThreadEventMark(JavaThread *thread) : aoqi@0: JvmtiEventMark(thread) { aoqi@0: _jt = (jthread)(to_jobject(thread->threadObj())); aoqi@0: }; aoqi@0: jthread jni_thread() { return _jt; } aoqi@0: }; aoqi@0: aoqi@0: class JvmtiClassEventMark : public JvmtiThreadEventMark { aoqi@0: private: aoqi@0: jclass _jc; aoqi@0: aoqi@0: public: aoqi@0: JvmtiClassEventMark(JavaThread *thread, Klass* klass) : aoqi@0: JvmtiThreadEventMark(thread) { aoqi@0: _jc = to_jclass(klass); aoqi@0: }; aoqi@0: jclass jni_class() { return _jc; } aoqi@0: }; aoqi@0: aoqi@0: class JvmtiMethodEventMark : public JvmtiThreadEventMark { aoqi@0: private: aoqi@0: jmethodID _mid; aoqi@0: aoqi@0: public: aoqi@0: JvmtiMethodEventMark(JavaThread *thread, methodHandle method) : aoqi@0: JvmtiThreadEventMark(thread), aoqi@0: _mid(to_jmethodID(method)) {}; aoqi@0: jmethodID jni_methodID() { return _mid; } aoqi@0: }; aoqi@0: aoqi@0: class JvmtiLocationEventMark : public JvmtiMethodEventMark { aoqi@0: private: aoqi@0: jlocation _loc; aoqi@0: aoqi@0: public: aoqi@0: JvmtiLocationEventMark(JavaThread *thread, methodHandle method, address location) : aoqi@0: JvmtiMethodEventMark(thread, method), aoqi@0: _loc(location - method->code_base()) {}; aoqi@0: jlocation location() { return _loc; } aoqi@0: }; aoqi@0: aoqi@0: class JvmtiExceptionEventMark : public JvmtiLocationEventMark { aoqi@0: private: aoqi@0: jobject _exc; aoqi@0: aoqi@0: public: aoqi@0: JvmtiExceptionEventMark(JavaThread *thread, methodHandle method, address location, Handle exception) : aoqi@0: JvmtiLocationEventMark(thread, method, location), aoqi@0: _exc(to_jobject(exception())) {}; aoqi@0: jobject exception() { return _exc; } aoqi@0: }; aoqi@0: aoqi@0: class JvmtiClassFileLoadEventMark : public JvmtiThreadEventMark { aoqi@0: private: aoqi@0: const char *_class_name; aoqi@0: jobject _jloader; aoqi@0: jobject _protection_domain; aoqi@0: jclass _class_being_redefined; aoqi@0: aoqi@0: public: aoqi@0: JvmtiClassFileLoadEventMark(JavaThread *thread, Symbol* name, aoqi@0: Handle class_loader, Handle prot_domain, KlassHandle *class_being_redefined) : JvmtiThreadEventMark(thread) { aoqi@0: _class_name = name != NULL? name->as_utf8() : NULL; aoqi@0: _jloader = (jobject)to_jobject(class_loader()); aoqi@0: _protection_domain = (jobject)to_jobject(prot_domain()); aoqi@0: if (class_being_redefined == NULL) { aoqi@0: _class_being_redefined = NULL; aoqi@0: } else { aoqi@0: _class_being_redefined = (jclass)to_jclass((*class_being_redefined)()); aoqi@0: } aoqi@0: }; aoqi@0: const char *class_name() { aoqi@0: return _class_name; aoqi@0: } aoqi@0: jobject jloader() { aoqi@0: return _jloader; aoqi@0: } aoqi@0: jobject protection_domain() { aoqi@0: return _protection_domain; aoqi@0: } aoqi@0: jclass class_being_redefined() { aoqi@0: return _class_being_redefined; aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: ////////////////////////////////////////////////////////////////////////////// aoqi@0: aoqi@0: int JvmtiExport::_field_access_count = 0; aoqi@0: int JvmtiExport::_field_modification_count = 0; aoqi@0: aoqi@0: bool JvmtiExport::_can_access_local_variables = false; aoqi@0: bool JvmtiExport::_can_hotswap_or_post_breakpoint = false; aoqi@0: bool JvmtiExport::_can_modify_any_class = false; aoqi@0: bool JvmtiExport::_can_walk_any_space = false; aoqi@0: aoqi@0: bool JvmtiExport::_has_redefined_a_class = false; aoqi@0: bool JvmtiExport::_all_dependencies_are_recorded = false; aoqi@0: aoqi@0: // aoqi@0: // field access management aoqi@0: // aoqi@0: aoqi@0: // interpreter generator needs the address of the counter aoqi@0: address JvmtiExport::get_field_access_count_addr() { aoqi@0: // We don't grab a lock because we don't want to aoqi@0: // serialize field access between all threads. This means that a aoqi@0: // thread on another processor can see the wrong count value and aoqi@0: // may either miss making a needed call into post_field_access() aoqi@0: // or will make an unneeded call into post_field_access(). We pay aoqi@0: // this price to avoid slowing down the VM when we aren't watching aoqi@0: // field accesses. aoqi@0: // Other access/mutation safe by virtue of being in VM state. aoqi@0: return (address)(&_field_access_count); aoqi@0: } aoqi@0: aoqi@0: // aoqi@0: // field modification management aoqi@0: // aoqi@0: aoqi@0: // interpreter generator needs the address of the counter aoqi@0: address JvmtiExport::get_field_modification_count_addr() { aoqi@0: // We don't grab a lock because we don't aoqi@0: // want to serialize field modification between all threads. This aoqi@0: // means that a thread on another processor can see the wrong aoqi@0: // count value and may either miss making a needed call into aoqi@0: // post_field_modification() or will make an unneeded call into aoqi@0: // post_field_modification(). We pay this price to avoid slowing aoqi@0: // down the VM when we aren't watching field modifications. aoqi@0: // Other access/mutation safe by virtue of being in VM state. aoqi@0: return (address)(&_field_modification_count); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /////////////////////////////////////////////////////////////// aoqi@0: // Functions needed by java.lang.instrument for starting up javaagent. aoqi@0: /////////////////////////////////////////////////////////////// aoqi@0: aoqi@0: jint aoqi@0: JvmtiExport::get_jvmti_interface(JavaVM *jvm, void **penv, jint version) { aoqi@0: // The JVMTI_VERSION_INTERFACE_JVMTI part of the version number aoqi@0: // has already been validated in JNI GetEnv(). aoqi@0: int major, minor, micro; aoqi@0: aoqi@0: // micro version doesn't matter here (yet?) aoqi@0: decode_version_values(version, &major, &minor, µ); aoqi@0: switch (major) { aoqi@0: case 1: aoqi@0: switch (minor) { aoqi@0: case 0: // version 1.0. is recognized aoqi@0: case 1: // version 1.1. is recognized aoqi@0: case 2: // version 1.2. is recognized aoqi@0: break; aoqi@0: aoqi@0: default: aoqi@0: return JNI_EVERSION; // unsupported minor version number aoqi@0: } aoqi@0: break; aoqi@0: default: aoqi@0: return JNI_EVERSION; // unsupported major version number aoqi@0: } aoqi@0: aoqi@0: if (JvmtiEnv::get_phase() == JVMTI_PHASE_LIVE) { aoqi@0: JavaThread* current_thread = (JavaThread*) ThreadLocalStorage::thread(); aoqi@0: // transition code: native to VM aoqi@0: ThreadInVMfromNative __tiv(current_thread); aoqi@0: VM_ENTRY_BASE(jvmtiEnv*, JvmtiExport::get_jvmti_interface, current_thread) aoqi@0: debug_only(VMNativeEntryWrapper __vew;) aoqi@0: aoqi@0: JvmtiEnv *jvmti_env = JvmtiEnv::create_a_jvmti(version); aoqi@0: *penv = jvmti_env->jvmti_external(); // actual type is jvmtiEnv* -- not to be confused with JvmtiEnv* aoqi@0: return JNI_OK; aoqi@0: aoqi@0: } else if (JvmtiEnv::get_phase() == JVMTI_PHASE_ONLOAD) { aoqi@0: // not live, no thread to transition aoqi@0: JvmtiEnv *jvmti_env = JvmtiEnv::create_a_jvmti(version); aoqi@0: *penv = jvmti_env->jvmti_external(); // actual type is jvmtiEnv* -- not to be confused with JvmtiEnv* aoqi@0: return JNI_OK; aoqi@0: aoqi@0: } else { aoqi@0: // Called at the wrong time aoqi@0: *penv = NULL; aoqi@0: return JNI_EDETACHED; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void aoqi@0: JvmtiExport::decode_version_values(jint version, int * major, int * minor, aoqi@0: int * micro) { aoqi@0: *major = (version & JVMTI_VERSION_MASK_MAJOR) >> JVMTI_VERSION_SHIFT_MAJOR; aoqi@0: *minor = (version & JVMTI_VERSION_MASK_MINOR) >> JVMTI_VERSION_SHIFT_MINOR; aoqi@0: *micro = (version & JVMTI_VERSION_MASK_MICRO) >> JVMTI_VERSION_SHIFT_MICRO; aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::enter_primordial_phase() { aoqi@0: JvmtiEnvBase::set_phase(JVMTI_PHASE_PRIMORDIAL); aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::enter_start_phase() { aoqi@0: JvmtiManageCapabilities::recompute_always_capabilities(); aoqi@0: JvmtiEnvBase::set_phase(JVMTI_PHASE_START); aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::enter_onload_phase() { aoqi@0: JvmtiEnvBase::set_phase(JVMTI_PHASE_ONLOAD); aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::enter_live_phase() { aoqi@0: JvmtiEnvBase::set_phase(JVMTI_PHASE_LIVE); aoqi@0: } aoqi@0: aoqi@0: // aoqi@0: // JVMTI events that the VM posts to the debugger and also startup agent aoqi@0: // and call the agent's premain() for java.lang.instrument. aoqi@0: // aoqi@0: aoqi@0: void JvmtiExport::post_vm_start() { aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_VM_START, ("JVMTI Trg VM start event triggered" )); aoqi@0: aoqi@0: // can now enable some events aoqi@0: JvmtiEventController::vm_start(); aoqi@0: aoqi@0: JvmtiEnvIterator it; aoqi@0: for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { aoqi@0: if (env->is_enabled(JVMTI_EVENT_VM_START)) { aoqi@0: EVT_TRACE(JVMTI_EVENT_VM_START, ("JVMTI Evt VM start event sent" )); aoqi@0: aoqi@0: JavaThread *thread = JavaThread::current(); aoqi@0: JvmtiThreadEventMark jem(thread); aoqi@0: JvmtiJavaThreadEventTransition jet(thread); aoqi@0: jvmtiEventVMStart callback = env->callbacks()->VMStart; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), jem.jni_env()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void JvmtiExport::post_vm_initialized() { aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_VM_INIT, ("JVMTI Trg VM init event triggered" )); aoqi@0: aoqi@0: // can now enable events aoqi@0: JvmtiEventController::vm_init(); aoqi@0: aoqi@0: JvmtiEnvIterator it; aoqi@0: for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { aoqi@0: if (env->is_enabled(JVMTI_EVENT_VM_INIT)) { aoqi@0: EVT_TRACE(JVMTI_EVENT_VM_INIT, ("JVMTI Evt VM init event sent" )); aoqi@0: aoqi@0: JavaThread *thread = JavaThread::current(); aoqi@0: JvmtiThreadEventMark jem(thread); aoqi@0: JvmtiJavaThreadEventTransition jet(thread); aoqi@0: jvmtiEventVMInit callback = env->callbacks()->VMInit; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void JvmtiExport::post_vm_death() { aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_VM_DEATH, ("JVMTI Trg VM death event triggered" )); aoqi@0: aoqi@0: JvmtiEnvIterator it; aoqi@0: for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { aoqi@0: if (env->is_enabled(JVMTI_EVENT_VM_DEATH)) { aoqi@0: EVT_TRACE(JVMTI_EVENT_VM_DEATH, ("JVMTI Evt VM death event sent" )); aoqi@0: aoqi@0: JavaThread *thread = JavaThread::current(); aoqi@0: JvmtiEventMark jem(thread); aoqi@0: JvmtiJavaThreadEventTransition jet(thread); aoqi@0: jvmtiEventVMDeath callback = env->callbacks()->VMDeath; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), jem.jni_env()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: JvmtiEnvBase::set_phase(JVMTI_PHASE_DEAD); aoqi@0: JvmtiEventController::vm_death(); aoqi@0: } aoqi@0: aoqi@0: char** aoqi@0: JvmtiExport::get_all_native_method_prefixes(int* count_ptr) { aoqi@0: // Have to grab JVMTI thread state lock to be sure environment doesn't aoqi@0: // go away while we iterate them. No locks during VM bring-up. aoqi@0: if (Threads::number_of_threads() == 0 || SafepointSynchronize::is_at_safepoint()) { aoqi@0: return JvmtiEnvBase::get_all_native_method_prefixes(count_ptr); aoqi@0: } else { aoqi@0: MutexLocker mu(JvmtiThreadState_lock); aoqi@0: return JvmtiEnvBase::get_all_native_method_prefixes(count_ptr); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: class JvmtiClassFileLoadHookPoster : public StackObj { aoqi@0: private: aoqi@0: Symbol* _h_name; aoqi@0: Handle _class_loader; aoqi@0: Handle _h_protection_domain; aoqi@0: unsigned char ** _data_ptr; aoqi@0: unsigned char ** _end_ptr; aoqi@0: JavaThread * _thread; aoqi@0: jint _curr_len; aoqi@0: unsigned char * _curr_data; aoqi@0: JvmtiEnv * _curr_env; aoqi@0: JvmtiCachedClassFileData ** _cached_class_file_ptr; aoqi@0: JvmtiThreadState * _state; aoqi@0: KlassHandle * _h_class_being_redefined; aoqi@0: JvmtiClassLoadKind _load_kind; aoqi@0: aoqi@0: public: aoqi@0: inline JvmtiClassFileLoadHookPoster(Symbol* h_name, Handle class_loader, aoqi@0: Handle h_protection_domain, aoqi@0: unsigned char **data_ptr, unsigned char **end_ptr, aoqi@0: JvmtiCachedClassFileData **cache_ptr) { aoqi@0: _h_name = h_name; aoqi@0: _class_loader = class_loader; aoqi@0: _h_protection_domain = h_protection_domain; aoqi@0: _data_ptr = data_ptr; aoqi@0: _end_ptr = end_ptr; aoqi@0: _thread = JavaThread::current(); aoqi@0: _curr_len = *end_ptr - *data_ptr; aoqi@0: _curr_data = *data_ptr; aoqi@0: _curr_env = NULL; aoqi@0: _cached_class_file_ptr = cache_ptr; aoqi@0: aoqi@0: _state = _thread->jvmti_thread_state(); aoqi@0: if (_state != NULL) { aoqi@0: _h_class_being_redefined = _state->get_class_being_redefined(); aoqi@0: _load_kind = _state->get_class_load_kind(); aoqi@0: // Clear class_being_redefined flag here. The action aoqi@0: // from agent handler could generate a new class file load aoqi@0: // hook event and if it is not cleared the new event generated aoqi@0: // from regular class file load could have this stale redefined aoqi@0: // class handle info. aoqi@0: _state->clear_class_being_redefined(); aoqi@0: } else { aoqi@0: // redefine and retransform will always set the thread state aoqi@0: _h_class_being_redefined = (KlassHandle *) NULL; aoqi@0: _load_kind = jvmti_class_load_kind_load; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void post() { aoqi@0: // EVT_TRIG_TRACE(JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, aoqi@0: // ("JVMTI [%s] class file load hook event triggered", aoqi@0: // JvmtiTrace::safe_get_thread_name(_thread))); aoqi@0: post_all_envs(); aoqi@0: copy_modified_data(); aoqi@0: } aoqi@0: aoqi@0: private: aoqi@0: void post_all_envs() { aoqi@0: if (_load_kind != jvmti_class_load_kind_retransform) { aoqi@0: // for class load and redefine, aoqi@0: // call the non-retransformable agents aoqi@0: JvmtiEnvIterator it; aoqi@0: for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { aoqi@0: if (!env->is_retransformable() && env->is_enabled(JVMTI_EVENT_CLASS_FILE_LOAD_HOOK)) { aoqi@0: // non-retransformable agents cannot retransform back, aoqi@0: // so no need to cache the original class file bytes aoqi@0: post_to_env(env, false); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: JvmtiEnvIterator it; aoqi@0: for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { aoqi@0: // retransformable agents get all events aoqi@0: if (env->is_retransformable() && env->is_enabled(JVMTI_EVENT_CLASS_FILE_LOAD_HOOK)) { aoqi@0: // retransformable agents need to cache the original class file aoqi@0: // bytes if changes are made via the ClassFileLoadHook aoqi@0: post_to_env(env, true); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void post_to_env(JvmtiEnv* env, bool caching_needed) { aoqi@0: unsigned char *new_data = NULL; aoqi@0: jint new_len = 0; aoqi@0: // EVT_TRACE(JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, aoqi@0: // ("JVMTI [%s] class file load hook event sent %s data_ptr = %d, data_len = %d", aoqi@0: // JvmtiTrace::safe_get_thread_name(_thread), aoqi@0: // _h_name == NULL ? "NULL" : _h_name->as_utf8(), aoqi@0: // _curr_data, _curr_len )); aoqi@0: JvmtiClassFileLoadEventMark jem(_thread, _h_name, _class_loader, aoqi@0: _h_protection_domain, aoqi@0: _h_class_being_redefined); aoqi@0: JvmtiJavaThreadEventTransition jet(_thread); aoqi@0: JNIEnv* jni_env = (JvmtiEnv::get_phase() == JVMTI_PHASE_PRIMORDIAL)? aoqi@0: NULL : jem.jni_env(); aoqi@0: jvmtiEventClassFileLoadHook callback = env->callbacks()->ClassFileLoadHook; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), jni_env, aoqi@0: jem.class_being_redefined(), aoqi@0: jem.jloader(), jem.class_name(), aoqi@0: jem.protection_domain(), aoqi@0: _curr_len, _curr_data, aoqi@0: &new_len, &new_data); aoqi@0: } aoqi@0: if (new_data != NULL) { aoqi@0: // this agent has modified class data. aoqi@0: if (caching_needed && *_cached_class_file_ptr == NULL) { aoqi@0: // data has been changed by the new retransformable agent aoqi@0: // and it hasn't already been cached, cache it aoqi@0: JvmtiCachedClassFileData *p; aoqi@0: p = (JvmtiCachedClassFileData *)os::malloc( aoqi@0: offset_of(JvmtiCachedClassFileData, data) + _curr_len, mtInternal); aoqi@0: if (p == NULL) { aoqi@0: vm_exit_out_of_memory(offset_of(JvmtiCachedClassFileData, data) + _curr_len, aoqi@0: OOM_MALLOC_ERROR, aoqi@0: "unable to allocate cached copy of original class bytes"); aoqi@0: } aoqi@0: p->length = _curr_len; aoqi@0: memcpy(p->data, _curr_data, _curr_len); aoqi@0: *_cached_class_file_ptr = p; aoqi@0: } aoqi@0: aoqi@0: if (_curr_data != *_data_ptr) { aoqi@0: // curr_data is previous agent modified class data. aoqi@0: // And this has been changed by the new agent so aoqi@0: // we can delete it now. aoqi@0: _curr_env->Deallocate(_curr_data); aoqi@0: } aoqi@0: aoqi@0: // Class file data has changed by the current agent. aoqi@0: _curr_data = new_data; aoqi@0: _curr_len = new_len; aoqi@0: // Save the current agent env we need this to deallocate the aoqi@0: // memory allocated by this agent. aoqi@0: _curr_env = env; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void copy_modified_data() { aoqi@0: // if one of the agent has modified class file data. aoqi@0: // Copy modified class data to new resources array. aoqi@0: if (_curr_data != *_data_ptr) { aoqi@0: *_data_ptr = NEW_RESOURCE_ARRAY(u1, _curr_len); aoqi@0: memcpy(*_data_ptr, _curr_data, _curr_len); aoqi@0: *_end_ptr = *_data_ptr + _curr_len; aoqi@0: _curr_env->Deallocate(_curr_data); aoqi@0: } aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: bool JvmtiExport::_should_post_class_file_load_hook = false; aoqi@0: aoqi@0: // this entry is for class file load hook on class load, redefine and retransform aoqi@0: void JvmtiExport::post_class_file_load_hook(Symbol* h_name, aoqi@0: Handle class_loader, aoqi@0: Handle h_protection_domain, aoqi@0: unsigned char **data_ptr, aoqi@0: unsigned char **end_ptr, aoqi@0: JvmtiCachedClassFileData **cache_ptr) { aoqi@0: JvmtiClassFileLoadHookPoster poster(h_name, class_loader, aoqi@0: h_protection_domain, aoqi@0: data_ptr, end_ptr, aoqi@0: cache_ptr); aoqi@0: poster.post(); aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::report_unsupported(bool on) { aoqi@0: // If any JVMTI service is turned on, we need to exit before native code aoqi@0: // tries to access nonexistant services. aoqi@0: if (on) { aoqi@0: vm_exit_during_initialization("Java Kernel does not support JVMTI."); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: static inline Klass* oop_to_klass(oop obj) { aoqi@0: Klass* k = obj->klass(); aoqi@0: aoqi@0: // if the object is a java.lang.Class then return the java mirror aoqi@0: if (k == SystemDictionary::Class_klass()) { aoqi@0: if (!java_lang_Class::is_primitive(obj)) { aoqi@0: k = java_lang_Class::as_Klass(obj); aoqi@0: assert(k != NULL, "class for non-primitive mirror must exist"); aoqi@0: } aoqi@0: } aoqi@0: return k; aoqi@0: } aoqi@0: aoqi@0: class JvmtiVMObjectAllocEventMark : public JvmtiClassEventMark { aoqi@0: private: aoqi@0: jobject _jobj; aoqi@0: jlong _size; aoqi@0: public: aoqi@0: JvmtiVMObjectAllocEventMark(JavaThread *thread, oop obj) : JvmtiClassEventMark(thread, oop_to_klass(obj)) { aoqi@0: _jobj = (jobject)to_jobject(obj); aoqi@0: _size = obj->size() * wordSize; aoqi@0: }; aoqi@0: jobject jni_jobject() { return _jobj; } aoqi@0: jlong size() { return _size; } aoqi@0: }; aoqi@0: aoqi@0: class JvmtiCompiledMethodLoadEventMark : public JvmtiMethodEventMark { aoqi@0: private: aoqi@0: jint _code_size; aoqi@0: const void *_code_data; aoqi@0: jint _map_length; aoqi@0: jvmtiAddrLocationMap *_map; aoqi@0: const void *_compile_info; aoqi@0: public: aoqi@0: JvmtiCompiledMethodLoadEventMark(JavaThread *thread, nmethod *nm, void* compile_info_ptr = NULL) aoqi@0: : JvmtiMethodEventMark(thread,methodHandle(thread, nm->method())) { aoqi@0: _code_data = nm->insts_begin(); aoqi@0: _code_size = nm->insts_size(); aoqi@0: _compile_info = compile_info_ptr; // Set void pointer of compiledMethodLoad Event. Default value is NULL. aoqi@0: JvmtiCodeBlobEvents::build_jvmti_addr_location_map(nm, &_map, &_map_length); aoqi@0: } aoqi@0: ~JvmtiCompiledMethodLoadEventMark() { aoqi@0: FREE_C_HEAP_ARRAY(jvmtiAddrLocationMap, _map, mtInternal); aoqi@0: } aoqi@0: aoqi@0: jint code_size() { return _code_size; } aoqi@0: const void *code_data() { return _code_data; } aoqi@0: jint map_length() { return _map_length; } aoqi@0: const jvmtiAddrLocationMap* map() { return _map; } aoqi@0: const void *compile_info() { return _compile_info; } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: aoqi@0: class JvmtiMonitorEventMark : public JvmtiThreadEventMark { aoqi@0: private: aoqi@0: jobject _jobj; aoqi@0: public: aoqi@0: JvmtiMonitorEventMark(JavaThread *thread, oop object) aoqi@0: : JvmtiThreadEventMark(thread){ aoqi@0: _jobj = to_jobject(object); aoqi@0: } aoqi@0: jobject jni_object() { return _jobj; } aoqi@0: }; aoqi@0: aoqi@0: /////////////////////////////////////////////////////////////// aoqi@0: // aoqi@0: // pending CompiledMethodUnload support aoqi@0: // aoqi@0: aoqi@0: void JvmtiExport::post_compiled_method_unload( aoqi@0: jmethodID method, const void *code_begin) { aoqi@0: JavaThread* thread = JavaThread::current(); aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_COMPILED_METHOD_UNLOAD, aoqi@0: ("JVMTI [%s] method compile unload event triggered", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: aoqi@0: // post the event for each environment that has this event enabled. aoqi@0: JvmtiEnvIterator it; aoqi@0: for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { aoqi@0: if (env->is_enabled(JVMTI_EVENT_COMPILED_METHOD_UNLOAD)) { aoqi@0: aoqi@0: EVT_TRACE(JVMTI_EVENT_COMPILED_METHOD_UNLOAD, aoqi@0: ("JVMTI [%s] class compile method unload event sent jmethodID " PTR_FORMAT, aoqi@0: JvmtiTrace::safe_get_thread_name(thread), method)); aoqi@0: aoqi@0: ResourceMark rm(thread); aoqi@0: aoqi@0: JvmtiEventMark jem(thread); aoqi@0: JvmtiJavaThreadEventTransition jet(thread); aoqi@0: jvmtiEventCompiledMethodUnload callback = env->callbacks()->CompiledMethodUnload; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), method, code_begin); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /////////////////////////////////////////////////////////////// aoqi@0: // aoqi@0: // JvmtiExport aoqi@0: // aoqi@0: aoqi@0: void JvmtiExport::post_raw_breakpoint(JavaThread *thread, Method* method, address location) { aoqi@0: HandleMark hm(thread); aoqi@0: methodHandle mh(thread, method); aoqi@0: aoqi@0: JvmtiThreadState *state = thread->jvmti_thread_state(); aoqi@0: if (state == NULL) { aoqi@0: return; aoqi@0: } aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_BREAKPOINT, ("JVMTI [%s] Trg Breakpoint triggered", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: JvmtiEnvThreadStateIterator it(state); aoqi@0: for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { aoqi@0: ets->compare_and_set_current_location(mh(), location, JVMTI_EVENT_BREAKPOINT); aoqi@0: if (!ets->breakpoint_posted() && ets->is_enabled(JVMTI_EVENT_BREAKPOINT)) { aoqi@0: ThreadState old_os_state = thread->osthread()->get_state(); aoqi@0: thread->osthread()->set_state(BREAKPOINTED); aoqi@0: EVT_TRACE(JVMTI_EVENT_BREAKPOINT, ("JVMTI [%s] Evt Breakpoint sent %s.%s @ %d", aoqi@0: JvmtiTrace::safe_get_thread_name(thread), aoqi@0: (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), aoqi@0: (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(), aoqi@0: location - mh()->code_base() )); aoqi@0: aoqi@0: JvmtiEnv *env = ets->get_env(); aoqi@0: JvmtiLocationEventMark jem(thread, mh, location); aoqi@0: JvmtiJavaThreadEventTransition jet(thread); aoqi@0: jvmtiEventBreakpoint callback = env->callbacks()->Breakpoint; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), aoqi@0: jem.jni_methodID(), jem.location()); aoqi@0: } aoqi@0: aoqi@0: ets->set_breakpoint_posted(); aoqi@0: thread->osthread()->set_state(old_os_state); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: ////////////////////////////////////////////////////////////////////////////// aoqi@0: aoqi@0: bool JvmtiExport::_can_get_source_debug_extension = false; aoqi@0: bool JvmtiExport::_can_maintain_original_method_order = false; aoqi@0: bool JvmtiExport::_can_post_interpreter_events = false; aoqi@0: bool JvmtiExport::_can_post_on_exceptions = false; aoqi@0: bool JvmtiExport::_can_post_breakpoint = false; aoqi@0: bool JvmtiExport::_can_post_field_access = false; aoqi@0: bool JvmtiExport::_can_post_field_modification = false; aoqi@0: bool JvmtiExport::_can_post_method_entry = false; aoqi@0: bool JvmtiExport::_can_post_method_exit = false; aoqi@0: bool JvmtiExport::_can_pop_frame = false; aoqi@0: bool JvmtiExport::_can_force_early_return = false; aoqi@0: aoqi@0: bool JvmtiExport::_should_post_single_step = false; aoqi@0: bool JvmtiExport::_should_post_field_access = false; aoqi@0: bool JvmtiExport::_should_post_field_modification = false; aoqi@0: bool JvmtiExport::_should_post_class_load = false; aoqi@0: bool JvmtiExport::_should_post_class_prepare = false; aoqi@0: bool JvmtiExport::_should_post_class_unload = false; aoqi@0: bool JvmtiExport::_should_post_thread_life = false; aoqi@0: bool JvmtiExport::_should_clean_up_heap_objects = false; aoqi@0: bool JvmtiExport::_should_post_native_method_bind = false; aoqi@0: bool JvmtiExport::_should_post_dynamic_code_generated = false; aoqi@0: bool JvmtiExport::_should_post_data_dump = false; aoqi@0: bool JvmtiExport::_should_post_compiled_method_load = false; aoqi@0: bool JvmtiExport::_should_post_compiled_method_unload = false; aoqi@0: bool JvmtiExport::_should_post_monitor_contended_enter = false; aoqi@0: bool JvmtiExport::_should_post_monitor_contended_entered = false; aoqi@0: bool JvmtiExport::_should_post_monitor_wait = false; aoqi@0: bool JvmtiExport::_should_post_monitor_waited = false; aoqi@0: bool JvmtiExport::_should_post_garbage_collection_start = false; aoqi@0: bool JvmtiExport::_should_post_garbage_collection_finish = false; aoqi@0: bool JvmtiExport::_should_post_object_free = false; aoqi@0: bool JvmtiExport::_should_post_resource_exhausted = false; aoqi@0: bool JvmtiExport::_should_post_vm_object_alloc = false; aoqi@0: bool JvmtiExport::_should_post_on_exceptions = false; aoqi@0: aoqi@0: //////////////////////////////////////////////////////////////////////////////////////////////// aoqi@0: aoqi@0: aoqi@0: // aoqi@0: // JVMTI single step management aoqi@0: // aoqi@0: void JvmtiExport::at_single_stepping_point(JavaThread *thread, Method* method, address location) { aoqi@0: assert(JvmtiExport::should_post_single_step(), "must be single stepping"); aoqi@0: aoqi@0: HandleMark hm(thread); aoqi@0: methodHandle mh(thread, method); aoqi@0: aoqi@0: // update information about current location and post a step event aoqi@0: JvmtiThreadState *state = thread->jvmti_thread_state(); aoqi@0: if (state == NULL) { aoqi@0: return; aoqi@0: } aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_SINGLE_STEP, ("JVMTI [%s] Trg Single Step triggered", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: if (!state->hide_single_stepping()) { aoqi@0: if (state->is_pending_step_for_popframe()) { aoqi@0: state->process_pending_step_for_popframe(); aoqi@0: } aoqi@0: if (state->is_pending_step_for_earlyret()) { aoqi@0: state->process_pending_step_for_earlyret(); aoqi@0: } aoqi@0: JvmtiExport::post_single_step(thread, mh(), location); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void JvmtiExport::expose_single_stepping(JavaThread *thread) { aoqi@0: JvmtiThreadState *state = thread->jvmti_thread_state(); aoqi@0: if (state != NULL) { aoqi@0: state->clear_hide_single_stepping(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool JvmtiExport::hide_single_stepping(JavaThread *thread) { aoqi@0: JvmtiThreadState *state = thread->jvmti_thread_state(); aoqi@0: if (state != NULL && state->is_enabled(JVMTI_EVENT_SINGLE_STEP)) { aoqi@0: state->set_hide_single_stepping(); aoqi@0: return true; aoqi@0: } else { aoqi@0: return false; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::post_class_load(JavaThread *thread, Klass* klass) { aoqi@0: HandleMark hm(thread); aoqi@0: KlassHandle kh(thread, klass); aoqi@0: aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_CLASS_LOAD, ("JVMTI [%s] Trg Class Load triggered", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: JvmtiThreadState* state = thread->jvmti_thread_state(); aoqi@0: if (state == NULL) { aoqi@0: return; aoqi@0: } aoqi@0: JvmtiEnvThreadStateIterator it(state); aoqi@0: for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { aoqi@0: if (ets->is_enabled(JVMTI_EVENT_CLASS_LOAD)) { aoqi@0: EVT_TRACE(JVMTI_EVENT_CLASS_LOAD, ("JVMTI [%s] Evt Class Load sent %s", aoqi@0: JvmtiTrace::safe_get_thread_name(thread), aoqi@0: kh()==NULL? "NULL" : kh()->external_name() )); aoqi@0: aoqi@0: JvmtiEnv *env = ets->get_env(); aoqi@0: JvmtiClassEventMark jem(thread, kh()); aoqi@0: JvmtiJavaThreadEventTransition jet(thread); aoqi@0: jvmtiEventClassLoad callback = env->callbacks()->ClassLoad; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_class()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void JvmtiExport::post_class_prepare(JavaThread *thread, Klass* klass) { aoqi@0: HandleMark hm(thread); aoqi@0: KlassHandle kh(thread, klass); aoqi@0: aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_CLASS_PREPARE, ("JVMTI [%s] Trg Class Prepare triggered", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: JvmtiThreadState* state = thread->jvmti_thread_state(); aoqi@0: if (state == NULL) { aoqi@0: return; aoqi@0: } aoqi@0: JvmtiEnvThreadStateIterator it(state); aoqi@0: for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { aoqi@0: if (ets->is_enabled(JVMTI_EVENT_CLASS_PREPARE)) { aoqi@0: EVT_TRACE(JVMTI_EVENT_CLASS_PREPARE, ("JVMTI [%s] Evt Class Prepare sent %s", aoqi@0: JvmtiTrace::safe_get_thread_name(thread), aoqi@0: kh()==NULL? "NULL" : kh()->external_name() )); aoqi@0: aoqi@0: JvmtiEnv *env = ets->get_env(); aoqi@0: JvmtiClassEventMark jem(thread, kh()); aoqi@0: JvmtiJavaThreadEventTransition jet(thread); aoqi@0: jvmtiEventClassPrepare callback = env->callbacks()->ClassPrepare; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_class()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::post_class_unload(Klass* klass) { aoqi@0: Thread *thread = Thread::current(); aoqi@0: HandleMark hm(thread); aoqi@0: KlassHandle kh(thread, klass); aoqi@0: aoqi@0: EVT_TRIG_TRACE(EXT_EVENT_CLASS_UNLOAD, ("JVMTI [?] Trg Class Unload triggered" )); aoqi@0: if (JvmtiEventController::is_enabled((jvmtiEvent)EXT_EVENT_CLASS_UNLOAD)) { aoqi@0: assert(thread->is_VM_thread(), "wrong thread"); aoqi@0: aoqi@0: // get JavaThread for whom we are proxy aoqi@0: JavaThread *real_thread = aoqi@0: (JavaThread *)((VMThread *)thread)->vm_operation()->calling_thread(); aoqi@0: aoqi@0: JvmtiEnvIterator it; aoqi@0: for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { aoqi@0: if (env->is_enabled((jvmtiEvent)EXT_EVENT_CLASS_UNLOAD)) { aoqi@0: EVT_TRACE(EXT_EVENT_CLASS_UNLOAD, ("JVMTI [?] Evt Class Unload sent %s", aoqi@0: kh()==NULL? "NULL" : kh()->external_name() )); aoqi@0: aoqi@0: // do everything manually, since this is a proxy - needs special care aoqi@0: JNIEnv* jni_env = real_thread->jni_environment(); aoqi@0: jthread jt = (jthread)JNIHandles::make_local(real_thread, real_thread->threadObj()); aoqi@0: jclass jk = (jclass)JNIHandles::make_local(real_thread, kh()->java_mirror()); aoqi@0: aoqi@0: // Before we call the JVMTI agent, we have to set the state in the aoqi@0: // thread for which we are proxying. aoqi@0: JavaThreadState prev_state = real_thread->thread_state(); aoqi@0: assert(prev_state == _thread_blocked, "JavaThread should be at safepoint"); aoqi@0: real_thread->set_thread_state(_thread_in_native); aoqi@0: aoqi@0: jvmtiExtensionEvent callback = env->ext_callbacks()->ClassUnload; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), jni_env, jt, jk); aoqi@0: } aoqi@0: aoqi@0: assert(real_thread->thread_state() == _thread_in_native, aoqi@0: "JavaThread should be in native"); aoqi@0: real_thread->set_thread_state(prev_state); aoqi@0: aoqi@0: JNIHandles::destroy_local(jk); aoqi@0: JNIHandles::destroy_local(jt); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void JvmtiExport::post_thread_start(JavaThread *thread) { aoqi@0: assert(thread->thread_state() == _thread_in_vm, "must be in vm state"); aoqi@0: aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_THREAD_START, ("JVMTI [%s] Trg Thread Start event triggered", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: aoqi@0: // do JVMTI thread initialization (if needed) aoqi@0: JvmtiEventController::thread_started(thread); aoqi@0: aoqi@0: // Do not post thread start event for hidden java thread. aoqi@0: if (JvmtiEventController::is_enabled(JVMTI_EVENT_THREAD_START) && aoqi@0: !thread->is_hidden_from_external_view()) { aoqi@0: JvmtiEnvIterator it; aoqi@0: for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { aoqi@0: if (env->is_enabled(JVMTI_EVENT_THREAD_START)) { aoqi@0: EVT_TRACE(JVMTI_EVENT_THREAD_START, ("JVMTI [%s] Evt Thread Start event sent", aoqi@0: JvmtiTrace::safe_get_thread_name(thread) )); aoqi@0: aoqi@0: JvmtiThreadEventMark jem(thread); aoqi@0: JvmtiJavaThreadEventTransition jet(thread); aoqi@0: jvmtiEventThreadStart callback = env->callbacks()->ThreadStart; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void JvmtiExport::post_thread_end(JavaThread *thread) { aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_THREAD_END, ("JVMTI [%s] Trg Thread End event triggered", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: aoqi@0: JvmtiThreadState *state = thread->jvmti_thread_state(); aoqi@0: if (state == NULL) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: // Do not post thread end event for hidden java thread. aoqi@0: if (state->is_enabled(JVMTI_EVENT_THREAD_END) && aoqi@0: !thread->is_hidden_from_external_view()) { aoqi@0: aoqi@0: JvmtiEnvThreadStateIterator it(state); aoqi@0: for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { aoqi@0: if (ets->is_enabled(JVMTI_EVENT_THREAD_END)) { aoqi@0: EVT_TRACE(JVMTI_EVENT_THREAD_END, ("JVMTI [%s] Evt Thread End event sent", aoqi@0: JvmtiTrace::safe_get_thread_name(thread) )); aoqi@0: aoqi@0: JvmtiEnv *env = ets->get_env(); aoqi@0: JvmtiThreadEventMark jem(thread); aoqi@0: JvmtiJavaThreadEventTransition jet(thread); aoqi@0: jvmtiEventThreadEnd callback = env->callbacks()->ThreadEnd; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::post_object_free(JvmtiEnv* env, jlong tag) { aoqi@0: assert(SafepointSynchronize::is_at_safepoint(), "must be executed at safepoint"); aoqi@0: assert(env->is_enabled(JVMTI_EVENT_OBJECT_FREE), "checking"); aoqi@0: aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_OBJECT_FREE, ("JVMTI [?] Trg Object Free triggered" )); aoqi@0: EVT_TRACE(JVMTI_EVENT_OBJECT_FREE, ("JVMTI [?] Evt Object Free sent")); aoqi@0: aoqi@0: jvmtiEventObjectFree callback = env->callbacks()->ObjectFree; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), tag); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::post_resource_exhausted(jint resource_exhausted_flags, const char* description) { aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_RESOURCE_EXHAUSTED, ("JVMTI Trg resource exhausted event triggered" )); aoqi@0: aoqi@0: JvmtiEnvIterator it; aoqi@0: for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { aoqi@0: if (env->is_enabled(JVMTI_EVENT_RESOURCE_EXHAUSTED)) { aoqi@0: EVT_TRACE(JVMTI_EVENT_RESOURCE_EXHAUSTED, ("JVMTI Evt resource exhausted event sent" )); aoqi@0: aoqi@0: JavaThread *thread = JavaThread::current(); aoqi@0: JvmtiThreadEventMark jem(thread); aoqi@0: JvmtiJavaThreadEventTransition jet(thread); aoqi@0: jvmtiEventResourceExhausted callback = env->callbacks()->ResourceExhausted; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), jem.jni_env(), aoqi@0: resource_exhausted_flags, NULL, description); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::post_method_entry(JavaThread *thread, Method* method, frame current_frame) { aoqi@0: HandleMark hm(thread); aoqi@0: methodHandle mh(thread, method); aoqi@0: aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_METHOD_ENTRY, ("JVMTI [%s] Trg Method Entry triggered %s.%s", aoqi@0: JvmtiTrace::safe_get_thread_name(thread), aoqi@0: (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), aoqi@0: (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() )); aoqi@0: aoqi@0: JvmtiThreadState* state = thread->jvmti_thread_state(); aoqi@0: if (state == NULL || !state->is_interp_only_mode()) { aoqi@0: // for any thread that actually wants method entry, interp_only_mode is set aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: state->incr_cur_stack_depth(); aoqi@0: aoqi@0: if (state->is_enabled(JVMTI_EVENT_METHOD_ENTRY)) { aoqi@0: JvmtiEnvThreadStateIterator it(state); aoqi@0: for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { aoqi@0: if (ets->is_enabled(JVMTI_EVENT_METHOD_ENTRY)) { aoqi@0: EVT_TRACE(JVMTI_EVENT_METHOD_ENTRY, ("JVMTI [%s] Evt Method Entry sent %s.%s", aoqi@0: JvmtiTrace::safe_get_thread_name(thread), aoqi@0: (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), aoqi@0: (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() )); aoqi@0: aoqi@0: JvmtiEnv *env = ets->get_env(); aoqi@0: JvmtiMethodEventMark jem(thread, mh); aoqi@0: JvmtiJavaThreadEventTransition jet(thread); aoqi@0: jvmtiEventMethodEntry callback = env->callbacks()->MethodEntry; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_methodID()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::post_method_exit(JavaThread *thread, Method* method, frame current_frame) { aoqi@0: HandleMark hm(thread); aoqi@0: methodHandle mh(thread, method); aoqi@0: aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_METHOD_EXIT, ("JVMTI [%s] Trg Method Exit triggered %s.%s", aoqi@0: JvmtiTrace::safe_get_thread_name(thread), aoqi@0: (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), aoqi@0: (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() )); aoqi@0: aoqi@0: JvmtiThreadState *state = thread->jvmti_thread_state(); aoqi@0: if (state == NULL || !state->is_interp_only_mode()) { aoqi@0: // for any thread that actually wants method exit, interp_only_mode is set aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: // return a flag when a method terminates by throwing an exception aoqi@0: // i.e. if an exception is thrown and it's not caught by the current method aoqi@0: bool exception_exit = state->is_exception_detected() && !state->is_exception_caught(); aoqi@0: aoqi@0: aoqi@0: if (state->is_enabled(JVMTI_EVENT_METHOD_EXIT)) { aoqi@0: Handle result; aoqi@0: jvalue value; aoqi@0: value.j = 0L; aoqi@0: aoqi@0: // if the method hasn't been popped because of an exception then we populate aoqi@0: // the return_value parameter for the callback. At this point we only have aoqi@0: // the address of a "raw result" and we just call into the interpreter to aoqi@0: // convert this into a jvalue. aoqi@0: if (!exception_exit) { aoqi@0: oop oop_result; aoqi@0: BasicType type = current_frame.interpreter_frame_result(&oop_result, &value); aoqi@0: if (type == T_OBJECT || type == T_ARRAY) { aoqi@0: result = Handle(thread, oop_result); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: JvmtiEnvThreadStateIterator it(state); aoqi@0: for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { aoqi@0: if (ets->is_enabled(JVMTI_EVENT_METHOD_EXIT)) { aoqi@0: EVT_TRACE(JVMTI_EVENT_METHOD_EXIT, ("JVMTI [%s] Evt Method Exit sent %s.%s", aoqi@0: JvmtiTrace::safe_get_thread_name(thread), aoqi@0: (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), aoqi@0: (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() )); aoqi@0: aoqi@0: JvmtiEnv *env = ets->get_env(); aoqi@0: JvmtiMethodEventMark jem(thread, mh); aoqi@0: if (result.not_null()) { aoqi@0: value.l = JNIHandles::make_local(thread, result()); aoqi@0: } aoqi@0: JvmtiJavaThreadEventTransition jet(thread); aoqi@0: jvmtiEventMethodExit callback = env->callbacks()->MethodExit; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), aoqi@0: jem.jni_methodID(), exception_exit, value); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (state->is_enabled(JVMTI_EVENT_FRAME_POP)) { aoqi@0: JvmtiEnvThreadStateIterator it(state); aoqi@0: for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { aoqi@0: int cur_frame_number = state->cur_stack_depth(); aoqi@0: aoqi@0: if (ets->is_frame_pop(cur_frame_number)) { aoqi@0: // we have a NotifyFramePop entry for this frame. aoqi@0: // now check that this env/thread wants this event aoqi@0: if (ets->is_enabled(JVMTI_EVENT_FRAME_POP)) { aoqi@0: EVT_TRACE(JVMTI_EVENT_FRAME_POP, ("JVMTI [%s] Evt Frame Pop sent %s.%s", aoqi@0: JvmtiTrace::safe_get_thread_name(thread), aoqi@0: (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), aoqi@0: (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() )); aoqi@0: aoqi@0: // we also need to issue a frame pop event for this frame aoqi@0: JvmtiEnv *env = ets->get_env(); aoqi@0: JvmtiMethodEventMark jem(thread, mh); aoqi@0: JvmtiJavaThreadEventTransition jet(thread); aoqi@0: jvmtiEventFramePop callback = env->callbacks()->FramePop; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), aoqi@0: jem.jni_methodID(), exception_exit); aoqi@0: } aoqi@0: } aoqi@0: // remove the frame's entry aoqi@0: ets->clear_frame_pop(cur_frame_number); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: state->decr_cur_stack_depth(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Todo: inline this for optimization aoqi@0: void JvmtiExport::post_single_step(JavaThread *thread, Method* method, address location) { aoqi@0: HandleMark hm(thread); aoqi@0: methodHandle mh(thread, method); aoqi@0: aoqi@0: JvmtiThreadState *state = thread->jvmti_thread_state(); aoqi@0: if (state == NULL) { aoqi@0: return; aoqi@0: } aoqi@0: JvmtiEnvThreadStateIterator it(state); aoqi@0: for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { aoqi@0: ets->compare_and_set_current_location(mh(), location, JVMTI_EVENT_SINGLE_STEP); aoqi@0: if (!ets->single_stepping_posted() && ets->is_enabled(JVMTI_EVENT_SINGLE_STEP)) { aoqi@0: EVT_TRACE(JVMTI_EVENT_SINGLE_STEP, ("JVMTI [%s] Evt Single Step sent %s.%s @ %d", aoqi@0: JvmtiTrace::safe_get_thread_name(thread), aoqi@0: (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), aoqi@0: (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(), aoqi@0: location - mh()->code_base() )); aoqi@0: aoqi@0: JvmtiEnv *env = ets->get_env(); aoqi@0: JvmtiLocationEventMark jem(thread, mh, location); aoqi@0: JvmtiJavaThreadEventTransition jet(thread); aoqi@0: jvmtiEventSingleStep callback = env->callbacks()->SingleStep; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), aoqi@0: jem.jni_methodID(), jem.location()); aoqi@0: } aoqi@0: aoqi@0: ets->set_single_stepping_posted(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void JvmtiExport::post_exception_throw(JavaThread *thread, Method* method, address location, oop exception) { aoqi@0: HandleMark hm(thread); aoqi@0: methodHandle mh(thread, method); aoqi@0: Handle exception_handle(thread, exception); aoqi@0: aoqi@0: JvmtiThreadState *state = thread->jvmti_thread_state(); aoqi@0: if (state == NULL) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_EXCEPTION, ("JVMTI [%s] Trg Exception thrown triggered", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: if (!state->is_exception_detected()) { aoqi@0: state->set_exception_detected(); aoqi@0: JvmtiEnvThreadStateIterator it(state); aoqi@0: for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { aoqi@0: if (ets->is_enabled(JVMTI_EVENT_EXCEPTION) && (exception != NULL)) { aoqi@0: aoqi@0: EVT_TRACE(JVMTI_EVENT_EXCEPTION, aoqi@0: ("JVMTI [%s] Evt Exception thrown sent %s.%s @ %d", aoqi@0: JvmtiTrace::safe_get_thread_name(thread), aoqi@0: (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), aoqi@0: (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(), aoqi@0: location - mh()->code_base() )); aoqi@0: aoqi@0: JvmtiEnv *env = ets->get_env(); aoqi@0: JvmtiExceptionEventMark jem(thread, mh, location, exception_handle); aoqi@0: aoqi@0: // It's okay to clear these exceptions here because we duplicate aoqi@0: // this lookup in InterpreterRuntime::exception_handler_for_exception. aoqi@0: EXCEPTION_MARK; aoqi@0: aoqi@0: bool should_repeat; aoqi@0: vframeStream st(thread); aoqi@0: assert(!st.at_end(), "cannot be at end"); aoqi@0: Method* current_method = NULL; aoqi@0: // A GC may occur during the Method::fast_exception_handler_bci_for() aoqi@0: // call below if it needs to load the constraint class. Using a aoqi@0: // methodHandle to keep the 'current_method' from being deallocated aoqi@0: // if GC happens. aoqi@0: methodHandle current_mh = methodHandle(thread, current_method); aoqi@0: int current_bci = -1; aoqi@0: do { aoqi@0: current_method = st.method(); aoqi@0: current_mh = methodHandle(thread, current_method); aoqi@0: current_bci = st.bci(); aoqi@0: do { aoqi@0: should_repeat = false; aoqi@0: KlassHandle eh_klass(thread, exception_handle()->klass()); aoqi@0: current_bci = Method::fast_exception_handler_bci_for( aoqi@0: current_mh, eh_klass, current_bci, THREAD); aoqi@0: if (HAS_PENDING_EXCEPTION) { aoqi@0: exception_handle = Handle(thread, PENDING_EXCEPTION); aoqi@0: CLEAR_PENDING_EXCEPTION; aoqi@0: should_repeat = true; aoqi@0: } aoqi@0: } while (should_repeat && (current_bci != -1)); aoqi@0: st.next(); aoqi@0: } while ((current_bci < 0) && (!st.at_end())); aoqi@0: aoqi@0: jmethodID catch_jmethodID; aoqi@0: if (current_bci < 0) { aoqi@0: catch_jmethodID = 0; aoqi@0: current_bci = 0; aoqi@0: } else { aoqi@0: catch_jmethodID = jem.to_jmethodID(current_mh); aoqi@0: } aoqi@0: aoqi@0: JvmtiJavaThreadEventTransition jet(thread); aoqi@0: jvmtiEventException callback = env->callbacks()->Exception; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), aoqi@0: jem.jni_methodID(), jem.location(), aoqi@0: jem.exception(), aoqi@0: catch_jmethodID, current_bci); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // frames may get popped because of this throw, be safe - invalidate cached depth aoqi@0: state->invalidate_cur_stack_depth(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void JvmtiExport::notice_unwind_due_to_exception(JavaThread *thread, Method* method, address location, oop exception, bool in_handler_frame) { aoqi@0: HandleMark hm(thread); aoqi@0: methodHandle mh(thread, method); aoqi@0: Handle exception_handle(thread, exception); aoqi@0: aoqi@0: JvmtiThreadState *state = thread->jvmti_thread_state(); aoqi@0: if (state == NULL) { aoqi@0: return; aoqi@0: } aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_EXCEPTION_CATCH, aoqi@0: ("JVMTI [%s] Trg unwind_due_to_exception triggered %s.%s @ %s%d - %s", aoqi@0: JvmtiTrace::safe_get_thread_name(thread), aoqi@0: (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), aoqi@0: (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(), aoqi@0: location==0? "no location:" : "", aoqi@0: location==0? 0 : location - mh()->code_base(), aoqi@0: in_handler_frame? "in handler frame" : "not handler frame" )); aoqi@0: aoqi@0: if (state->is_exception_detected()) { aoqi@0: aoqi@0: state->invalidate_cur_stack_depth(); aoqi@0: if (!in_handler_frame) { aoqi@0: // Not in exception handler. aoqi@0: if(state->is_interp_only_mode()) { aoqi@0: // method exit and frame pop events are posted only in interp mode. aoqi@0: // When these events are enabled code should be in running in interp mode. aoqi@0: JvmtiExport::post_method_exit(thread, method, thread->last_frame()); aoqi@0: // The cached cur_stack_depth might have changed from the aoqi@0: // operations of frame pop or method exit. We are not 100% sure aoqi@0: // the cached cur_stack_depth is still valid depth so invalidate aoqi@0: // it. aoqi@0: state->invalidate_cur_stack_depth(); aoqi@0: } aoqi@0: } else { aoqi@0: // In exception handler frame. Report exception catch. aoqi@0: assert(location != NULL, "must be a known location"); aoqi@0: // Update cur_stack_depth - the frames above the current frame aoqi@0: // have been unwound due to this exception: aoqi@0: assert(!state->is_exception_caught(), "exception must not be caught yet."); aoqi@0: state->set_exception_caught(); aoqi@0: aoqi@0: JvmtiEnvThreadStateIterator it(state); aoqi@0: for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { aoqi@0: if (ets->is_enabled(JVMTI_EVENT_EXCEPTION_CATCH) && (exception_handle() != NULL)) { aoqi@0: EVT_TRACE(JVMTI_EVENT_EXCEPTION_CATCH, aoqi@0: ("JVMTI [%s] Evt ExceptionCatch sent %s.%s @ %d", aoqi@0: JvmtiTrace::safe_get_thread_name(thread), aoqi@0: (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), aoqi@0: (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(), aoqi@0: location - mh()->code_base() )); aoqi@0: aoqi@0: JvmtiEnv *env = ets->get_env(); aoqi@0: JvmtiExceptionEventMark jem(thread, mh, location, exception_handle); aoqi@0: JvmtiJavaThreadEventTransition jet(thread); aoqi@0: jvmtiEventExceptionCatch callback = env->callbacks()->ExceptionCatch; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), aoqi@0: jem.jni_methodID(), jem.location(), aoqi@0: jem.exception()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: oop JvmtiExport::jni_GetField_probe(JavaThread *thread, jobject jobj, oop obj, aoqi@0: Klass* klass, jfieldID fieldID, bool is_static) { aoqi@0: if (*((int *)get_field_access_count_addr()) > 0 && thread->has_last_Java_frame()) { aoqi@0: // At least one field access watch is set so we have more work aoqi@0: // to do. This wrapper is used by entry points that allow us aoqi@0: // to create handles in post_field_access_by_jni(). aoqi@0: post_field_access_by_jni(thread, obj, klass, fieldID, is_static); aoqi@0: // event posting can block so refetch oop if we were passed a jobj aoqi@0: if (jobj != NULL) return JNIHandles::resolve_non_null(jobj); aoqi@0: } aoqi@0: return obj; aoqi@0: } aoqi@0: aoqi@0: oop JvmtiExport::jni_GetField_probe_nh(JavaThread *thread, jobject jobj, oop obj, aoqi@0: Klass* klass, jfieldID fieldID, bool is_static) { aoqi@0: if (*((int *)get_field_access_count_addr()) > 0 && thread->has_last_Java_frame()) { aoqi@0: // At least one field access watch is set so we have more work aoqi@0: // to do. This wrapper is used by "quick" entry points that don't aoqi@0: // allow us to create handles in post_field_access_by_jni(). We aoqi@0: // override that with a ResetNoHandleMark. aoqi@0: ResetNoHandleMark rnhm; aoqi@0: post_field_access_by_jni(thread, obj, klass, fieldID, is_static); aoqi@0: // event posting can block so refetch oop if we were passed a jobj aoqi@0: if (jobj != NULL) return JNIHandles::resolve_non_null(jobj); aoqi@0: } aoqi@0: return obj; aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::post_field_access_by_jni(JavaThread *thread, oop obj, aoqi@0: Klass* klass, jfieldID fieldID, bool is_static) { aoqi@0: // We must be called with a Java context in order to provide reasonable aoqi@0: // values for the klazz, method, and location fields. The callers of this aoqi@0: // function don't make the call unless there is a Java context. aoqi@0: assert(thread->has_last_Java_frame(), "must be called with a Java context"); aoqi@0: aoqi@0: ResourceMark rm; aoqi@0: fieldDescriptor fd; aoqi@0: // if get_field_descriptor finds fieldID to be invalid, then we just bail aoqi@0: bool valid_fieldID = JvmtiEnv::get_field_descriptor(klass, fieldID, &fd); aoqi@0: assert(valid_fieldID == true,"post_field_access_by_jni called with invalid fieldID"); aoqi@0: if (!valid_fieldID) return; aoqi@0: // field accesses are not watched so bail aoqi@0: if (!fd.is_field_access_watched()) return; aoqi@0: aoqi@0: HandleMark hm(thread); aoqi@0: KlassHandle h_klass(thread, klass); aoqi@0: Handle h_obj; aoqi@0: if (!is_static) { aoqi@0: // non-static field accessors have an object, but we need a handle aoqi@0: assert(obj != NULL, "non-static needs an object"); aoqi@0: h_obj = Handle(thread, obj); aoqi@0: } aoqi@0: post_field_access(thread, aoqi@0: thread->last_frame().interpreter_frame_method(), aoqi@0: thread->last_frame().interpreter_frame_bcp(), aoqi@0: h_klass, h_obj, fieldID); aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::post_field_access(JavaThread *thread, Method* method, aoqi@0: address location, KlassHandle field_klass, Handle object, jfieldID field) { aoqi@0: aoqi@0: HandleMark hm(thread); aoqi@0: methodHandle mh(thread, method); aoqi@0: aoqi@0: JvmtiThreadState *state = thread->jvmti_thread_state(); aoqi@0: if (state == NULL) { aoqi@0: return; aoqi@0: } aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_FIELD_ACCESS, ("JVMTI [%s] Trg Field Access event triggered", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: JvmtiEnvThreadStateIterator it(state); aoqi@0: for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { aoqi@0: if (ets->is_enabled(JVMTI_EVENT_FIELD_ACCESS)) { aoqi@0: EVT_TRACE(JVMTI_EVENT_FIELD_ACCESS, ("JVMTI [%s] Evt Field Access event sent %s.%s @ %d", aoqi@0: JvmtiTrace::safe_get_thread_name(thread), aoqi@0: (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), aoqi@0: (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(), aoqi@0: location - mh()->code_base() )); aoqi@0: aoqi@0: JvmtiEnv *env = ets->get_env(); aoqi@0: JvmtiLocationEventMark jem(thread, mh, location); aoqi@0: jclass field_jclass = jem.to_jclass(field_klass()); aoqi@0: jobject field_jobject = jem.to_jobject(object()); aoqi@0: JvmtiJavaThreadEventTransition jet(thread); aoqi@0: jvmtiEventFieldAccess callback = env->callbacks()->FieldAccess; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), aoqi@0: jem.jni_methodID(), jem.location(), aoqi@0: field_jclass, field_jobject, field); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: oop JvmtiExport::jni_SetField_probe(JavaThread *thread, jobject jobj, oop obj, aoqi@0: Klass* klass, jfieldID fieldID, bool is_static, aoqi@0: char sig_type, jvalue *value) { aoqi@0: if (*((int *)get_field_modification_count_addr()) > 0 && thread->has_last_Java_frame()) { aoqi@0: // At least one field modification watch is set so we have more work aoqi@0: // to do. This wrapper is used by entry points that allow us aoqi@0: // to create handles in post_field_modification_by_jni(). aoqi@0: post_field_modification_by_jni(thread, obj, klass, fieldID, is_static, sig_type, value); aoqi@0: // event posting can block so refetch oop if we were passed a jobj aoqi@0: if (jobj != NULL) return JNIHandles::resolve_non_null(jobj); aoqi@0: } aoqi@0: return obj; aoqi@0: } aoqi@0: aoqi@0: oop JvmtiExport::jni_SetField_probe_nh(JavaThread *thread, jobject jobj, oop obj, aoqi@0: Klass* klass, jfieldID fieldID, bool is_static, aoqi@0: char sig_type, jvalue *value) { aoqi@0: if (*((int *)get_field_modification_count_addr()) > 0 && thread->has_last_Java_frame()) { aoqi@0: // At least one field modification watch is set so we have more work aoqi@0: // to do. This wrapper is used by "quick" entry points that don't aoqi@0: // allow us to create handles in post_field_modification_by_jni(). We aoqi@0: // override that with a ResetNoHandleMark. aoqi@0: ResetNoHandleMark rnhm; aoqi@0: post_field_modification_by_jni(thread, obj, klass, fieldID, is_static, sig_type, value); aoqi@0: // event posting can block so refetch oop if we were passed a jobj aoqi@0: if (jobj != NULL) return JNIHandles::resolve_non_null(jobj); aoqi@0: } aoqi@0: return obj; aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::post_field_modification_by_jni(JavaThread *thread, oop obj, aoqi@0: Klass* klass, jfieldID fieldID, bool is_static, aoqi@0: char sig_type, jvalue *value) { aoqi@0: // We must be called with a Java context in order to provide reasonable aoqi@0: // values for the klazz, method, and location fields. The callers of this aoqi@0: // function don't make the call unless there is a Java context. aoqi@0: assert(thread->has_last_Java_frame(), "must be called with Java context"); aoqi@0: aoqi@0: ResourceMark rm; aoqi@0: fieldDescriptor fd; aoqi@0: // if get_field_descriptor finds fieldID to be invalid, then we just bail aoqi@0: bool valid_fieldID = JvmtiEnv::get_field_descriptor(klass, fieldID, &fd); aoqi@0: assert(valid_fieldID == true,"post_field_modification_by_jni called with invalid fieldID"); aoqi@0: if (!valid_fieldID) return; aoqi@0: // field modifications are not watched so bail aoqi@0: if (!fd.is_field_modification_watched()) return; aoqi@0: aoqi@0: HandleMark hm(thread); aoqi@0: aoqi@0: Handle h_obj; aoqi@0: if (!is_static) { aoqi@0: // non-static field accessors have an object, but we need a handle aoqi@0: assert(obj != NULL, "non-static needs an object"); aoqi@0: h_obj = Handle(thread, obj); aoqi@0: } aoqi@0: KlassHandle h_klass(thread, klass); aoqi@0: post_field_modification(thread, aoqi@0: thread->last_frame().interpreter_frame_method(), aoqi@0: thread->last_frame().interpreter_frame_bcp(), aoqi@0: h_klass, h_obj, fieldID, sig_type, value); aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::post_raw_field_modification(JavaThread *thread, Method* method, aoqi@0: address location, KlassHandle field_klass, Handle object, jfieldID field, aoqi@0: char sig_type, jvalue *value) { aoqi@0: aoqi@0: if (sig_type == 'I' || sig_type == 'Z' || sig_type == 'C' || sig_type == 'S') { aoqi@0: // 'I' instructions are used for byte, char, short and int. aoqi@0: // determine which it really is, and convert aoqi@0: fieldDescriptor fd; aoqi@0: bool found = JvmtiEnv::get_field_descriptor(field_klass(), field, &fd); aoqi@0: // should be found (if not, leave as is) aoqi@0: if (found) { aoqi@0: jint ival = value->i; aoqi@0: // convert value from int to appropriate type aoqi@0: switch (fd.field_type()) { aoqi@0: case T_BOOLEAN: aoqi@0: sig_type = 'Z'; aoqi@0: value->i = 0; // clear it aoqi@0: value->z = (jboolean)ival; aoqi@0: break; aoqi@0: case T_BYTE: aoqi@0: sig_type = 'B'; aoqi@0: value->i = 0; // clear it aoqi@0: value->b = (jbyte)ival; aoqi@0: break; aoqi@0: case T_CHAR: aoqi@0: sig_type = 'C'; aoqi@0: value->i = 0; // clear it aoqi@0: value->c = (jchar)ival; aoqi@0: break; aoqi@0: case T_SHORT: aoqi@0: sig_type = 'S'; aoqi@0: value->i = 0; // clear it aoqi@0: value->s = (jshort)ival; aoqi@0: break; aoqi@0: case T_INT: aoqi@0: // nothing to do aoqi@0: break; aoqi@0: default: aoqi@0: // this is an integer instruction, should be one of above aoqi@0: ShouldNotReachHere(); aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: assert(sig_type != '[', "array should have sig_type == 'L'"); aoqi@0: bool handle_created = false; aoqi@0: aoqi@0: // convert oop to JNI handle. aoqi@0: if (sig_type == 'L') { aoqi@0: handle_created = true; aoqi@0: value->l = (jobject)JNIHandles::make_local(thread, (oop)value->l); aoqi@0: } aoqi@0: aoqi@0: post_field_modification(thread, method, location, field_klass, object, field, sig_type, value); aoqi@0: aoqi@0: // Destroy the JNI handle allocated above. aoqi@0: if (handle_created) { aoqi@0: JNIHandles::destroy_local(value->l); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::post_field_modification(JavaThread *thread, Method* method, aoqi@0: address location, KlassHandle field_klass, Handle object, jfieldID field, aoqi@0: char sig_type, jvalue *value_ptr) { aoqi@0: aoqi@0: HandleMark hm(thread); aoqi@0: methodHandle mh(thread, method); aoqi@0: aoqi@0: JvmtiThreadState *state = thread->jvmti_thread_state(); aoqi@0: if (state == NULL) { aoqi@0: return; aoqi@0: } aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_FIELD_MODIFICATION, aoqi@0: ("JVMTI [%s] Trg Field Modification event triggered", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: aoqi@0: JvmtiEnvThreadStateIterator it(state); aoqi@0: for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { aoqi@0: if (ets->is_enabled(JVMTI_EVENT_FIELD_MODIFICATION)) { aoqi@0: EVT_TRACE(JVMTI_EVENT_FIELD_MODIFICATION, aoqi@0: ("JVMTI [%s] Evt Field Modification event sent %s.%s @ %d", aoqi@0: JvmtiTrace::safe_get_thread_name(thread), aoqi@0: (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), aoqi@0: (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(), aoqi@0: location - mh()->code_base() )); aoqi@0: aoqi@0: JvmtiEnv *env = ets->get_env(); aoqi@0: JvmtiLocationEventMark jem(thread, mh, location); aoqi@0: jclass field_jclass = jem.to_jclass(field_klass()); aoqi@0: jobject field_jobject = jem.to_jobject(object()); aoqi@0: JvmtiJavaThreadEventTransition jet(thread); aoqi@0: jvmtiEventFieldModification callback = env->callbacks()->FieldModification; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), aoqi@0: jem.jni_methodID(), jem.location(), aoqi@0: field_jclass, field_jobject, field, sig_type, *value_ptr); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::post_native_method_bind(Method* method, address* function_ptr) { aoqi@0: JavaThread* thread = JavaThread::current(); aoqi@0: assert(thread->thread_state() == _thread_in_vm, "must be in vm state"); aoqi@0: aoqi@0: HandleMark hm(thread); aoqi@0: methodHandle mh(thread, method); aoqi@0: aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_NATIVE_METHOD_BIND, ("JVMTI [%s] Trg Native Method Bind event triggered", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: aoqi@0: if (JvmtiEventController::is_enabled(JVMTI_EVENT_NATIVE_METHOD_BIND)) { aoqi@0: JvmtiEnvIterator it; aoqi@0: for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { aoqi@0: if (env->is_enabled(JVMTI_EVENT_NATIVE_METHOD_BIND)) { aoqi@0: EVT_TRACE(JVMTI_EVENT_NATIVE_METHOD_BIND, ("JVMTI [%s] Evt Native Method Bind event sent", aoqi@0: JvmtiTrace::safe_get_thread_name(thread) )); aoqi@0: aoqi@0: JvmtiMethodEventMark jem(thread, mh); aoqi@0: JvmtiJavaThreadEventTransition jet(thread); aoqi@0: JNIEnv* jni_env = JvmtiEnv::get_phase() == JVMTI_PHASE_PRIMORDIAL? NULL : jem.jni_env(); aoqi@0: jvmtiEventNativeMethodBind callback = env->callbacks()->NativeMethodBind; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), jni_env, jem.jni_thread(), aoqi@0: jem.jni_methodID(), (void*)(*function_ptr), (void**)function_ptr); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Returns a record containing inlining information for the given nmethod aoqi@0: jvmtiCompiledMethodLoadInlineRecord* create_inline_record(nmethod* nm) { aoqi@0: jint numstackframes = 0; aoqi@0: jvmtiCompiledMethodLoadInlineRecord* record = (jvmtiCompiledMethodLoadInlineRecord*)NEW_RESOURCE_OBJ(jvmtiCompiledMethodLoadInlineRecord); aoqi@0: record->header.kind = JVMTI_CMLR_INLINE_INFO; aoqi@0: record->header.next = NULL; aoqi@0: record->header.majorinfoversion = JVMTI_CMLR_MAJOR_VERSION_1; aoqi@0: record->header.minorinfoversion = JVMTI_CMLR_MINOR_VERSION_0; aoqi@0: record->numpcs = 0; aoqi@0: for(PcDesc* p = nm->scopes_pcs_begin(); p < nm->scopes_pcs_end(); p++) { aoqi@0: if(p->scope_decode_offset() == DebugInformationRecorder::serialized_null) continue; aoqi@0: record->numpcs++; aoqi@0: } aoqi@0: record->pcinfo = (PCStackInfo*)(NEW_RESOURCE_ARRAY(PCStackInfo, record->numpcs)); aoqi@0: int scope = 0; aoqi@0: for(PcDesc* p = nm->scopes_pcs_begin(); p < nm->scopes_pcs_end(); p++) { aoqi@0: if(p->scope_decode_offset() == DebugInformationRecorder::serialized_null) continue; aoqi@0: void* pc_address = (void*)p->real_pc(nm); aoqi@0: assert(pc_address != NULL, "pc_address must be non-null"); aoqi@0: record->pcinfo[scope].pc = pc_address; aoqi@0: numstackframes=0; aoqi@0: for(ScopeDesc* sd = nm->scope_desc_at(p->real_pc(nm));sd != NULL;sd = sd->sender()) { aoqi@0: numstackframes++; aoqi@0: } aoqi@0: assert(numstackframes != 0, "numstackframes must be nonzero."); aoqi@0: record->pcinfo[scope].methods = (jmethodID *)NEW_RESOURCE_ARRAY(jmethodID, numstackframes); aoqi@0: record->pcinfo[scope].bcis = (jint *)NEW_RESOURCE_ARRAY(jint, numstackframes); aoqi@0: record->pcinfo[scope].numstackframes = numstackframes; aoqi@0: int stackframe = 0; aoqi@0: for(ScopeDesc* sd = nm->scope_desc_at(p->real_pc(nm));sd != NULL;sd = sd->sender()) { aoqi@0: // sd->method() can be NULL for stubs but not for nmethods. To be completely robust, include an assert that we should never see a null sd->method() aoqi@0: assert(sd->method() != NULL, "sd->method() cannot be null."); aoqi@0: record->pcinfo[scope].methods[stackframe] = sd->method()->jmethod_id(); aoqi@0: record->pcinfo[scope].bcis[stackframe] = sd->bci(); aoqi@0: stackframe++; aoqi@0: } aoqi@0: scope++; aoqi@0: } aoqi@0: return record; aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::post_compiled_method_load(nmethod *nm) { aoqi@0: JavaThread* thread = JavaThread::current(); aoqi@0: aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_COMPILED_METHOD_LOAD, aoqi@0: ("JVMTI [%s] method compile load event triggered", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: aoqi@0: JvmtiEnvIterator it; aoqi@0: for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { aoqi@0: if (env->is_enabled(JVMTI_EVENT_COMPILED_METHOD_LOAD)) { aoqi@0: aoqi@0: EVT_TRACE(JVMTI_EVENT_COMPILED_METHOD_LOAD, aoqi@0: ("JVMTI [%s] class compile method load event sent %s.%s ", aoqi@0: JvmtiTrace::safe_get_thread_name(thread), aoqi@0: (nm->method() == NULL) ? "NULL" : nm->method()->klass_name()->as_C_string(), aoqi@0: (nm->method() == NULL) ? "NULL" : nm->method()->name()->as_C_string())); aoqi@0: ResourceMark rm(thread); aoqi@0: HandleMark hm(thread); aoqi@0: aoqi@0: // Add inlining information aoqi@0: jvmtiCompiledMethodLoadInlineRecord* inlinerecord = create_inline_record(nm); aoqi@0: // Pass inlining information through the void pointer aoqi@0: JvmtiCompiledMethodLoadEventMark jem(thread, nm, inlinerecord); aoqi@0: JvmtiJavaThreadEventTransition jet(thread); aoqi@0: jvmtiEventCompiledMethodLoad callback = env->callbacks()->CompiledMethodLoad; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), jem.jni_methodID(), aoqi@0: jem.code_size(), jem.code_data(), jem.map_length(), aoqi@0: jem.map(), jem.compile_info()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // post a COMPILED_METHOD_LOAD event for a given environment aoqi@0: void JvmtiExport::post_compiled_method_load(JvmtiEnv* env, const jmethodID method, const jint length, aoqi@0: const void *code_begin, const jint map_length, aoqi@0: const jvmtiAddrLocationMap* map) aoqi@0: { aoqi@0: JavaThread* thread = JavaThread::current(); aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_COMPILED_METHOD_LOAD, aoqi@0: ("JVMTI [%s] method compile load event triggered (by GenerateEvents)", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: if (env->is_enabled(JVMTI_EVENT_COMPILED_METHOD_LOAD)) { aoqi@0: aoqi@0: EVT_TRACE(JVMTI_EVENT_COMPILED_METHOD_LOAD, aoqi@0: ("JVMTI [%s] class compile method load event sent (by GenerateEvents), jmethodID=" PTR_FORMAT, aoqi@0: JvmtiTrace::safe_get_thread_name(thread), method)); aoqi@0: aoqi@0: JvmtiEventMark jem(thread); aoqi@0: JvmtiJavaThreadEventTransition jet(thread); aoqi@0: jvmtiEventCompiledMethodLoad callback = env->callbacks()->CompiledMethodLoad; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), method, aoqi@0: length, code_begin, map_length, aoqi@0: map, NULL); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::post_dynamic_code_generated_internal(const char *name, const void *code_begin, const void *code_end) { aoqi@0: assert(name != NULL && name[0] != '\0', "sanity check"); aoqi@0: aoqi@0: JavaThread* thread = JavaThread::current(); aoqi@0: // In theory everyone coming thru here is in_vm but we need to be certain aoqi@0: // because a callee will do a vm->native transition aoqi@0: ThreadInVMfromUnknown __tiv; aoqi@0: aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_DYNAMIC_CODE_GENERATED, aoqi@0: ("JVMTI [%s] method dynamic code generated event triggered", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: JvmtiEnvIterator it; aoqi@0: for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { aoqi@0: if (env->is_enabled(JVMTI_EVENT_DYNAMIC_CODE_GENERATED)) { aoqi@0: EVT_TRACE(JVMTI_EVENT_DYNAMIC_CODE_GENERATED, aoqi@0: ("JVMTI [%s] dynamic code generated event sent for %s", aoqi@0: JvmtiTrace::safe_get_thread_name(thread), name)); aoqi@0: JvmtiEventMark jem(thread); aoqi@0: JvmtiJavaThreadEventTransition jet(thread); aoqi@0: jint length = (jint)pointer_delta(code_end, code_begin, sizeof(char)); aoqi@0: jvmtiEventDynamicCodeGenerated callback = env->callbacks()->DynamicCodeGenerated; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), name, (void*)code_begin, length); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::post_dynamic_code_generated(const char *name, const void *code_begin, const void *code_end) { aoqi@0: jvmtiPhase phase = JvmtiEnv::get_phase(); aoqi@0: if (phase == JVMTI_PHASE_PRIMORDIAL || phase == JVMTI_PHASE_START) { aoqi@0: post_dynamic_code_generated_internal(name, code_begin, code_end); aoqi@0: } else { aoqi@0: // It may not be safe to post the event from this thread. Defer all aoqi@0: // postings to the service thread so that it can perform them in a safe aoqi@0: // context and in-order. aoqi@0: MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag); aoqi@0: JvmtiDeferredEvent event = JvmtiDeferredEvent::dynamic_code_generated_event( aoqi@0: name, code_begin, code_end); aoqi@0: JvmtiDeferredEventQueue::enqueue(event); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // post a DYNAMIC_CODE_GENERATED event for a given environment aoqi@0: // used by GenerateEvents aoqi@0: void JvmtiExport::post_dynamic_code_generated(JvmtiEnv* env, const char *name, aoqi@0: const void *code_begin, const void *code_end) aoqi@0: { aoqi@0: JavaThread* thread = JavaThread::current(); aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_DYNAMIC_CODE_GENERATED, aoqi@0: ("JVMTI [%s] dynamic code generated event triggered (by GenerateEvents)", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: if (env->is_enabled(JVMTI_EVENT_DYNAMIC_CODE_GENERATED)) { aoqi@0: EVT_TRACE(JVMTI_EVENT_DYNAMIC_CODE_GENERATED, aoqi@0: ("JVMTI [%s] dynamic code generated event sent for %s", aoqi@0: JvmtiTrace::safe_get_thread_name(thread), name)); aoqi@0: JvmtiEventMark jem(thread); aoqi@0: JvmtiJavaThreadEventTransition jet(thread); aoqi@0: jint length = (jint)pointer_delta(code_end, code_begin, sizeof(char)); aoqi@0: jvmtiEventDynamicCodeGenerated callback = env->callbacks()->DynamicCodeGenerated; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), name, (void*)code_begin, length); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // post a DynamicCodeGenerated event while holding locks in the VM. aoqi@0: void JvmtiExport::post_dynamic_code_generated_while_holding_locks(const char* name, aoqi@0: address code_begin, address code_end) aoqi@0: { aoqi@0: // register the stub with the current dynamic code event collector aoqi@0: JvmtiThreadState* state = JvmtiThreadState::state_for(JavaThread::current()); aoqi@0: // state can only be NULL if the current thread is exiting which aoqi@0: // should not happen since we're trying to post an event aoqi@0: guarantee(state != NULL, "attempt to register stub via an exiting thread"); aoqi@0: JvmtiDynamicCodeEventCollector* collector = state->get_dynamic_code_event_collector(); aoqi@0: guarantee(collector != NULL, "attempt to register stub without event collector"); aoqi@0: collector->register_stub(name, code_begin, code_end); aoqi@0: } aoqi@0: aoqi@0: // Collect all the vm internally allocated objects which are visible to java world aoqi@0: void JvmtiExport::record_vm_internal_object_allocation(oop obj) { aoqi@0: Thread* thread = ThreadLocalStorage::thread(); aoqi@0: if (thread != NULL && thread->is_Java_thread()) { aoqi@0: // Can not take safepoint here. aoqi@0: No_Safepoint_Verifier no_sfpt; aoqi@0: // Can not take safepoint here so can not use state_for to get aoqi@0: // jvmti thread state. aoqi@0: JvmtiThreadState *state = ((JavaThread*)thread)->jvmti_thread_state(); aoqi@0: if (state != NULL ) { aoqi@0: // state is non NULL when VMObjectAllocEventCollector is enabled. aoqi@0: JvmtiVMObjectAllocEventCollector *collector; aoqi@0: collector = state->get_vm_object_alloc_event_collector(); aoqi@0: if (collector != NULL && collector->is_enabled()) { aoqi@0: // Don't record classes as these will be notified via the ClassLoad aoqi@0: // event. aoqi@0: if (obj->klass() != SystemDictionary::Class_klass()) { aoqi@0: collector->record_allocation(obj); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::post_garbage_collection_finish() { aoqi@0: Thread *thread = Thread::current(); // this event is posted from VM-Thread. aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, aoqi@0: ("JVMTI [%s] garbage collection finish event triggered", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: JvmtiEnvIterator it; aoqi@0: for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { aoqi@0: if (env->is_enabled(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH)) { aoqi@0: EVT_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, aoqi@0: ("JVMTI [%s] garbage collection finish event sent ", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: JvmtiThreadEventTransition jet(thread); aoqi@0: // JNIEnv is NULL here because this event is posted from VM Thread aoqi@0: jvmtiEventGarbageCollectionFinish callback = env->callbacks()->GarbageCollectionFinish; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::post_garbage_collection_start() { aoqi@0: Thread* thread = Thread::current(); // this event is posted from vm-thread. aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_START, aoqi@0: ("JVMTI [%s] garbage collection start event triggered", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: JvmtiEnvIterator it; aoqi@0: for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { aoqi@0: if (env->is_enabled(JVMTI_EVENT_GARBAGE_COLLECTION_START)) { aoqi@0: EVT_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_START, aoqi@0: ("JVMTI [%s] garbage collection start event sent ", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: JvmtiThreadEventTransition jet(thread); aoqi@0: // JNIEnv is NULL here because this event is posted from VM Thread aoqi@0: jvmtiEventGarbageCollectionStart callback = env->callbacks()->GarbageCollectionStart; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::post_data_dump() { aoqi@0: Thread *thread = Thread::current(); aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_DATA_DUMP_REQUEST, aoqi@0: ("JVMTI [%s] data dump request event triggered", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: JvmtiEnvIterator it; aoqi@0: for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { aoqi@0: if (env->is_enabled(JVMTI_EVENT_DATA_DUMP_REQUEST)) { aoqi@0: EVT_TRACE(JVMTI_EVENT_DATA_DUMP_REQUEST, aoqi@0: ("JVMTI [%s] data dump request event sent ", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: JvmtiThreadEventTransition jet(thread); aoqi@0: // JNIEnv is NULL here because this event is posted from VM Thread aoqi@0: jvmtiEventDataDumpRequest callback = env->callbacks()->DataDumpRequest; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::post_monitor_contended_enter(JavaThread *thread, ObjectMonitor *obj_mntr) { aoqi@0: oop object = (oop)obj_mntr->object(); aoqi@0: if (!ServiceUtil::visible_oop(object)) { aoqi@0: // Ignore monitor contended enter for vm internal object. aoqi@0: return; aoqi@0: } aoqi@0: JvmtiThreadState *state = thread->jvmti_thread_state(); aoqi@0: if (state == NULL) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: HandleMark hm(thread); aoqi@0: Handle h(thread, object); aoqi@0: aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTER, aoqi@0: ("JVMTI [%s] montior contended enter event triggered", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: aoqi@0: JvmtiEnvThreadStateIterator it(state); aoqi@0: for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { aoqi@0: if (ets->is_enabled(JVMTI_EVENT_MONITOR_CONTENDED_ENTER)) { aoqi@0: EVT_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTER, aoqi@0: ("JVMTI [%s] monitor contended enter event sent", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: JvmtiMonitorEventMark jem(thread, h()); aoqi@0: JvmtiEnv *env = ets->get_env(); aoqi@0: JvmtiThreadEventTransition jet(thread); aoqi@0: jvmtiEventMonitorContendedEnter callback = env->callbacks()->MonitorContendedEnter; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_object()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::post_monitor_contended_entered(JavaThread *thread, ObjectMonitor *obj_mntr) { aoqi@0: oop object = (oop)obj_mntr->object(); aoqi@0: if (!ServiceUtil::visible_oop(object)) { aoqi@0: // Ignore monitor contended entered for vm internal object. aoqi@0: return; aoqi@0: } aoqi@0: JvmtiThreadState *state = thread->jvmti_thread_state(); aoqi@0: if (state == NULL) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: HandleMark hm(thread); aoqi@0: Handle h(thread, object); aoqi@0: aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTERED, aoqi@0: ("JVMTI [%s] montior contended entered event triggered", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: aoqi@0: JvmtiEnvThreadStateIterator it(state); aoqi@0: for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { aoqi@0: if (ets->is_enabled(JVMTI_EVENT_MONITOR_CONTENDED_ENTERED)) { aoqi@0: EVT_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTERED, aoqi@0: ("JVMTI [%s] monitor contended enter event sent", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: JvmtiMonitorEventMark jem(thread, h()); aoqi@0: JvmtiEnv *env = ets->get_env(); aoqi@0: JvmtiThreadEventTransition jet(thread); aoqi@0: jvmtiEventMonitorContendedEntered callback = env->callbacks()->MonitorContendedEntered; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_object()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::post_monitor_wait(JavaThread *thread, oop object, aoqi@0: jlong timeout) { aoqi@0: JvmtiThreadState *state = thread->jvmti_thread_state(); aoqi@0: if (state == NULL) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: HandleMark hm(thread); aoqi@0: Handle h(thread, object); aoqi@0: aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_WAIT, aoqi@0: ("JVMTI [%s] montior wait event triggered", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: aoqi@0: JvmtiEnvThreadStateIterator it(state); aoqi@0: for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { aoqi@0: if (ets->is_enabled(JVMTI_EVENT_MONITOR_WAIT)) { aoqi@0: EVT_TRACE(JVMTI_EVENT_MONITOR_WAIT, aoqi@0: ("JVMTI [%s] monitor wait event sent ", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: JvmtiMonitorEventMark jem(thread, h()); aoqi@0: JvmtiEnv *env = ets->get_env(); aoqi@0: JvmtiThreadEventTransition jet(thread); aoqi@0: jvmtiEventMonitorWait callback = env->callbacks()->MonitorWait; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), aoqi@0: jem.jni_object(), timeout); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::post_monitor_waited(JavaThread *thread, ObjectMonitor *obj_mntr, jboolean timed_out) { aoqi@0: oop object = (oop)obj_mntr->object(); aoqi@0: if (!ServiceUtil::visible_oop(object)) { aoqi@0: // Ignore monitor waited for vm internal object. aoqi@0: return; aoqi@0: } aoqi@0: JvmtiThreadState *state = thread->jvmti_thread_state(); aoqi@0: if (state == NULL) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: HandleMark hm(thread); aoqi@0: Handle h(thread, object); aoqi@0: aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_WAITED, aoqi@0: ("JVMTI [%s] montior waited event triggered", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: aoqi@0: JvmtiEnvThreadStateIterator it(state); aoqi@0: for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { aoqi@0: if (ets->is_enabled(JVMTI_EVENT_MONITOR_WAITED)) { aoqi@0: EVT_TRACE(JVMTI_EVENT_MONITOR_WAITED, aoqi@0: ("JVMTI [%s] monitor waited event sent ", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: JvmtiMonitorEventMark jem(thread, h()); aoqi@0: JvmtiEnv *env = ets->get_env(); aoqi@0: JvmtiThreadEventTransition jet(thread); aoqi@0: jvmtiEventMonitorWaited callback = env->callbacks()->MonitorWaited; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), aoqi@0: jem.jni_object(), timed_out); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void JvmtiExport::post_vm_object_alloc(JavaThread *thread, oop object) { aoqi@0: EVT_TRIG_TRACE(JVMTI_EVENT_VM_OBJECT_ALLOC, ("JVMTI [%s] Trg vm object alloc triggered", aoqi@0: JvmtiTrace::safe_get_thread_name(thread))); aoqi@0: if (object == NULL) { aoqi@0: return; aoqi@0: } aoqi@0: HandleMark hm(thread); aoqi@0: Handle h(thread, object); aoqi@0: JvmtiEnvIterator it; aoqi@0: for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { aoqi@0: if (env->is_enabled(JVMTI_EVENT_VM_OBJECT_ALLOC)) { aoqi@0: EVT_TRACE(JVMTI_EVENT_VM_OBJECT_ALLOC, ("JVMTI [%s] Evt vmobject alloc sent %s", aoqi@0: JvmtiTrace::safe_get_thread_name(thread), aoqi@0: object==NULL? "NULL" : java_lang_Class::as_Klass(object)->external_name())); aoqi@0: aoqi@0: JvmtiVMObjectAllocEventMark jem(thread, h()); aoqi@0: JvmtiJavaThreadEventTransition jet(thread); aoqi@0: jvmtiEventVMObjectAlloc callback = env->callbacks()->VMObjectAlloc; aoqi@0: if (callback != NULL) { aoqi@0: (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), aoqi@0: jem.jni_jobject(), jem.jni_class(), jem.size()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: //////////////////////////////////////////////////////////////////////////////////////////////// aoqi@0: aoqi@0: void JvmtiExport::cleanup_thread(JavaThread* thread) { aoqi@0: assert(JavaThread::current() == thread, "thread is not current"); aoqi@0: MutexLocker mu(JvmtiThreadState_lock); aoqi@0: aoqi@0: if (thread->jvmti_thread_state() != NULL) { aoqi@0: // This has to happen after the thread state is removed, which is aoqi@0: // why it is not in post_thread_end_event like its complement aoqi@0: // Maybe both these functions should be rolled into the posts? aoqi@0: JvmtiEventController::thread_ended(thread); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::oops_do(OopClosure* f) { aoqi@0: JvmtiCurrentBreakpoints::oops_do(f); aoqi@0: JvmtiVMObjectAllocEventCollector::oops_do_for_all_threads(f); aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f) { aoqi@0: JvmtiTagMap::weak_oops_do(is_alive, f); aoqi@0: } aoqi@0: aoqi@0: void JvmtiExport::gc_epilogue() { aoqi@0: JvmtiCurrentBreakpoints::gc_epilogue(); aoqi@0: } aoqi@0: aoqi@0: // Onload raw monitor transition. aoqi@0: void JvmtiExport::transition_pending_onload_raw_monitors() { aoqi@0: JvmtiPendingMonitors::transition_raw_monitors(); aoqi@0: } aoqi@0: aoqi@0: //////////////////////////////////////////////////////////////////////////////////////////////// aoqi@0: aoqi@0: // type for the Agent_OnAttach entry point aoqi@0: extern "C" { aoqi@0: typedef jint (JNICALL *OnAttachEntry_t)(JavaVM*, char *, void *); aoqi@0: } aoqi@0: aoqi@0: jint JvmtiExport::load_agent_library(AttachOperation* op, outputStream* st) { aoqi@0: char ebuf[1024]; aoqi@0: char buffer[JVM_MAXPATHLEN]; aoqi@0: void* library = NULL; aoqi@0: jint result = JNI_ERR; aoqi@0: const char *on_attach_symbols[] = AGENT_ONATTACH_SYMBOLS; aoqi@0: size_t num_symbol_entries = ARRAY_SIZE(on_attach_symbols); aoqi@0: aoqi@0: // get agent name and options aoqi@0: const char* agent = op->arg(0); aoqi@0: const char* absParam = op->arg(1); aoqi@0: const char* options = op->arg(2); aoqi@0: aoqi@0: // The abs paramter should be "true" or "false" aoqi@0: bool is_absolute_path = (absParam != NULL) && (strcmp(absParam,"true")==0); aoqi@0: aoqi@0: // Initially marked as invalid. It will be set to valid if we can find the agent aoqi@0: AgentLibrary *agent_lib = new AgentLibrary(agent, options, is_absolute_path, NULL); aoqi@0: aoqi@0: // Check for statically linked in agent. If not found then if the path is aoqi@0: // absolute we attempt to load the library. Otherwise we try to load it aoqi@0: // from the standard dll directory. aoqi@0: aoqi@0: if (!os::find_builtin_agent(agent_lib, on_attach_symbols, num_symbol_entries)) { aoqi@0: if (is_absolute_path) { aoqi@0: library = os::dll_load(agent, ebuf, sizeof ebuf); aoqi@0: } else { aoqi@0: // Try to load the agent from the standard dll directory aoqi@0: if (os::dll_build_name(buffer, sizeof(buffer), Arguments::get_dll_dir(), aoqi@0: agent)) { aoqi@0: library = os::dll_load(buffer, ebuf, sizeof ebuf); aoqi@0: } aoqi@0: if (library == NULL) { aoqi@0: // not found - try local path aoqi@0: char ns[1] = {0}; aoqi@0: if (os::dll_build_name(buffer, sizeof(buffer), ns, agent)) { aoqi@0: library = os::dll_load(buffer, ebuf, sizeof ebuf); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: if (library != NULL) { aoqi@0: agent_lib->set_os_lib(library); aoqi@0: agent_lib->set_valid(); aoqi@0: } aoqi@0: } aoqi@0: // If the library was loaded then we attempt to invoke the Agent_OnAttach aoqi@0: // function aoqi@0: if (agent_lib->valid()) { aoqi@0: // Lookup the Agent_OnAttach function aoqi@0: OnAttachEntry_t on_attach_entry = NULL; aoqi@0: on_attach_entry = CAST_TO_FN_PTR(OnAttachEntry_t, aoqi@0: os::find_agent_function(agent_lib, false, on_attach_symbols, num_symbol_entries)); aoqi@0: if (on_attach_entry == NULL) { aoqi@0: // Agent_OnAttach missing - unload library aoqi@0: if (!agent_lib->is_static_lib()) { aoqi@0: os::dll_unload(library); aoqi@0: } aoqi@0: delete agent_lib; aoqi@0: } else { aoqi@0: // Invoke the Agent_OnAttach function aoqi@0: JavaThread* THREAD = JavaThread::current(); aoqi@0: { aoqi@0: extern struct JavaVM_ main_vm; aoqi@0: JvmtiThreadEventMark jem(THREAD); aoqi@0: JvmtiJavaThreadEventTransition jet(THREAD); aoqi@0: aoqi@0: result = (*on_attach_entry)(&main_vm, (char*)options, NULL); aoqi@0: } aoqi@0: aoqi@0: // Agent_OnAttach may have used JNI aoqi@0: if (HAS_PENDING_EXCEPTION) { aoqi@0: CLEAR_PENDING_EXCEPTION; aoqi@0: } aoqi@0: aoqi@0: // If OnAttach returns JNI_OK then we add it to the list of aoqi@0: // agent libraries so that we can call Agent_OnUnload later. aoqi@0: if (result == JNI_OK) { aoqi@0: Arguments::add_loaded_agent(agent_lib); aoqi@0: } else { aoqi@0: delete agent_lib; aoqi@0: } aoqi@0: aoqi@0: // Agent_OnAttach executed so completion status is JNI_OK aoqi@0: st->print_cr("%d", result); aoqi@0: result = JNI_OK; aoqi@0: } aoqi@0: } aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: //////////////////////////////////////////////////////////////////////////////////////////////// aoqi@0: aoqi@0: // Setup current current thread for event collection. aoqi@0: void JvmtiEventCollector::setup_jvmti_thread_state() { aoqi@0: // set this event collector to be the current one. aoqi@0: JvmtiThreadState* state = JvmtiThreadState::state_for(JavaThread::current()); aoqi@0: // state can only be NULL if the current thread is exiting which aoqi@0: // should not happen since we're trying to configure for event collection aoqi@0: guarantee(state != NULL, "exiting thread called setup_jvmti_thread_state"); aoqi@0: if (is_vm_object_alloc_event()) { aoqi@0: _prev = state->get_vm_object_alloc_event_collector(); aoqi@0: state->set_vm_object_alloc_event_collector((JvmtiVMObjectAllocEventCollector *)this); aoqi@0: } else if (is_dynamic_code_event()) { aoqi@0: _prev = state->get_dynamic_code_event_collector(); aoqi@0: state->set_dynamic_code_event_collector((JvmtiDynamicCodeEventCollector *)this); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Unset current event collection in this thread and reset it with previous aoqi@0: // collector. aoqi@0: void JvmtiEventCollector::unset_jvmti_thread_state() { aoqi@0: JvmtiThreadState* state = JavaThread::current()->jvmti_thread_state(); aoqi@0: if (state != NULL) { aoqi@0: // restore the previous event collector (if any) aoqi@0: if (is_vm_object_alloc_event()) { aoqi@0: if (state->get_vm_object_alloc_event_collector() == this) { aoqi@0: state->set_vm_object_alloc_event_collector((JvmtiVMObjectAllocEventCollector *)_prev); aoqi@0: } else { aoqi@0: // this thread's jvmti state was created during the scope of aoqi@0: // the event collector. aoqi@0: } aoqi@0: } else { aoqi@0: if (is_dynamic_code_event()) { aoqi@0: if (state->get_dynamic_code_event_collector() == this) { aoqi@0: state->set_dynamic_code_event_collector((JvmtiDynamicCodeEventCollector *)_prev); aoqi@0: } else { aoqi@0: // this thread's jvmti state was created during the scope of aoqi@0: // the event collector. aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // create the dynamic code event collector aoqi@0: JvmtiDynamicCodeEventCollector::JvmtiDynamicCodeEventCollector() : _code_blobs(NULL) { aoqi@0: if (JvmtiExport::should_post_dynamic_code_generated()) { aoqi@0: setup_jvmti_thread_state(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // iterate over any code blob descriptors collected and post a aoqi@0: // DYNAMIC_CODE_GENERATED event to the profiler. aoqi@0: JvmtiDynamicCodeEventCollector::~JvmtiDynamicCodeEventCollector() { aoqi@0: assert(!JavaThread::current()->owns_locks(), "all locks must be released to post deferred events"); aoqi@0: // iterate over any code blob descriptors that we collected aoqi@0: if (_code_blobs != NULL) { aoqi@0: for (int i=0; i<_code_blobs->length(); i++) { aoqi@0: JvmtiCodeBlobDesc* blob = _code_blobs->at(i); aoqi@0: JvmtiExport::post_dynamic_code_generated(blob->name(), blob->code_begin(), blob->code_end()); aoqi@0: FreeHeap(blob); aoqi@0: } aoqi@0: delete _code_blobs; aoqi@0: } aoqi@0: unset_jvmti_thread_state(); aoqi@0: } aoqi@0: aoqi@0: // register a stub aoqi@0: void JvmtiDynamicCodeEventCollector::register_stub(const char* name, address start, address end) { aoqi@0: if (_code_blobs == NULL) { aoqi@0: _code_blobs = new (ResourceObj::C_HEAP, mtInternal) GrowableArray(1,true); aoqi@0: } aoqi@0: _code_blobs->append(new JvmtiCodeBlobDesc(name, start, end)); aoqi@0: } aoqi@0: aoqi@0: // Setup current thread to record vm allocated objects. aoqi@0: JvmtiVMObjectAllocEventCollector::JvmtiVMObjectAllocEventCollector() : _allocated(NULL) { aoqi@0: if (JvmtiExport::should_post_vm_object_alloc()) { aoqi@0: _enable = true; aoqi@0: setup_jvmti_thread_state(); aoqi@0: } else { aoqi@0: _enable = false; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Post vm_object_alloc event for vm allocated objects visible to java aoqi@0: // world. aoqi@0: JvmtiVMObjectAllocEventCollector::~JvmtiVMObjectAllocEventCollector() { aoqi@0: if (_allocated != NULL) { aoqi@0: set_enabled(false); aoqi@0: for (int i = 0; i < _allocated->length(); i++) { aoqi@0: oop obj = _allocated->at(i); aoqi@0: if (ServiceUtil::visible_oop(obj)) { aoqi@0: JvmtiExport::post_vm_object_alloc(JavaThread::current(), obj); aoqi@0: } aoqi@0: } aoqi@0: delete _allocated; aoqi@0: } aoqi@0: unset_jvmti_thread_state(); aoqi@0: } aoqi@0: aoqi@0: void JvmtiVMObjectAllocEventCollector::record_allocation(oop obj) { aoqi@0: assert(is_enabled(), "VM object alloc event collector is not enabled"); aoqi@0: if (_allocated == NULL) { aoqi@0: _allocated = new (ResourceObj::C_HEAP, mtInternal) GrowableArray(1, true); aoqi@0: } aoqi@0: _allocated->push(obj); aoqi@0: } aoqi@0: aoqi@0: // GC support. aoqi@0: void JvmtiVMObjectAllocEventCollector::oops_do(OopClosure* f) { aoqi@0: if (_allocated != NULL) { aoqi@0: for(int i=_allocated->length() - 1; i >= 0; i--) { aoqi@0: if (_allocated->at(i) != NULL) { aoqi@0: f->do_oop(_allocated->adr_at(i)); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void JvmtiVMObjectAllocEventCollector::oops_do_for_all_threads(OopClosure* f) { aoqi@0: // no-op if jvmti not enabled aoqi@0: if (!JvmtiEnv::environments_might_exist()) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: // Runs at safepoint. So no need to acquire Threads_lock. aoqi@0: for (JavaThread *jthr = Threads::first(); jthr != NULL; jthr = jthr->next()) { aoqi@0: JvmtiThreadState *state = jthr->jvmti_thread_state(); aoqi@0: if (state != NULL) { aoqi@0: JvmtiVMObjectAllocEventCollector *collector; aoqi@0: collector = state->get_vm_object_alloc_event_collector(); aoqi@0: while (collector != NULL) { aoqi@0: collector->oops_do(f); aoqi@0: collector = (JvmtiVMObjectAllocEventCollector *)collector->get_prev(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Disable collection of VMObjectAlloc events aoqi@0: NoJvmtiVMObjectAllocMark::NoJvmtiVMObjectAllocMark() : _collector(NULL) { aoqi@0: // a no-op if VMObjectAlloc event is not enabled aoqi@0: if (!JvmtiExport::should_post_vm_object_alloc()) { aoqi@0: return; aoqi@0: } aoqi@0: Thread* thread = ThreadLocalStorage::thread(); aoqi@0: if (thread != NULL && thread->is_Java_thread()) { aoqi@0: JavaThread* current_thread = (JavaThread*)thread; aoqi@0: JvmtiThreadState *state = current_thread->jvmti_thread_state(); aoqi@0: if (state != NULL) { aoqi@0: JvmtiVMObjectAllocEventCollector *collector; aoqi@0: collector = state->get_vm_object_alloc_event_collector(); aoqi@0: if (collector != NULL && collector->is_enabled()) { aoqi@0: _collector = collector; aoqi@0: _collector->set_enabled(false); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Re-Enable collection of VMObjectAlloc events (if previously enabled) aoqi@0: NoJvmtiVMObjectAllocMark::~NoJvmtiVMObjectAllocMark() { aoqi@0: if (was_enabled()) { aoqi@0: _collector->set_enabled(true); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: JvmtiGCMarker::JvmtiGCMarker() { aoqi@0: // if there aren't any JVMTI environments then nothing to do aoqi@0: if (!JvmtiEnv::environments_might_exist()) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: if (JvmtiExport::should_post_garbage_collection_start()) { aoqi@0: JvmtiExport::post_garbage_collection_start(); aoqi@0: } aoqi@0: aoqi@0: if (SafepointSynchronize::is_at_safepoint()) { aoqi@0: // Do clean up tasks that need to be done at a safepoint aoqi@0: JvmtiEnvBase::check_for_periodic_clean_up(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: JvmtiGCMarker::~JvmtiGCMarker() { aoqi@0: // if there aren't any JVMTI environments then nothing to do aoqi@0: if (!JvmtiEnv::environments_might_exist()) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: // JVMTI notify gc finish aoqi@0: if (JvmtiExport::should_post_garbage_collection_finish()) { aoqi@0: JvmtiExport::post_garbage_collection_finish(); aoqi@0: } aoqi@0: }