jvmtiExport.cpp revision 12408:777aaa19c4b1
11556Srgrimes/* 21556Srgrimes * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved. 31556Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 41556Srgrimes * 51556Srgrimes * This code is free software; you can redistribute it and/or modify it 61556Srgrimes * under the terms of the GNU General Public License version 2 only, as 71556Srgrimes * published by the Free Software Foundation. 81556Srgrimes * 91556Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT 101556Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 111556Srgrimes * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 121556Srgrimes * version 2 for more details (a copy is included in the LICENSE file that 131556Srgrimes * accompanied this code). 141556Srgrimes * 151556Srgrimes * You should have received a copy of the GNU General Public License version 161556Srgrimes * 2 along with this work; if not, write to the Free Software Foundation, 171556Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 181556Srgrimes * 191556Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 201556Srgrimes * or visit www.oracle.com if you need additional information or have any 211556Srgrimes * questions. 221556Srgrimes * 231556Srgrimes */ 241556Srgrimes 251556Srgrimes#include "precompiled.hpp" 261556Srgrimes#include "classfile/systemDictionary.hpp" 271556Srgrimes#include "code/nmethod.hpp" 28127499Sgad#include "code/pcDesc.hpp" 29127499Sgad#include "code/scopeDesc.hpp" 30127499Sgad#include "interpreter/interpreter.hpp" 31127499Sgad#include "jvmtifiles/jvmtiEnv.hpp" 32127499Sgad#include "logging/log.hpp" 33127499Sgad#include "logging/logStream.hpp" 34127499Sgad#include "memory/resourceArea.hpp" 351556Srgrimes#include "oops/objArrayKlass.hpp" 361556Srgrimes#include "oops/objArrayOop.hpp" 371556Srgrimes#include "oops/oop.inline.hpp" 3890143Smarkm#include "prims/jvmtiCodeBlobEvents.hpp" 391556Srgrimes#include "prims/jvmtiEventController.hpp" 401556Srgrimes#include "prims/jvmtiEventController.inline.hpp" 411556Srgrimes#include "prims/jvmtiExport.hpp" 421556Srgrimes#include "prims/jvmtiImpl.hpp" 4390143Smarkm#include "prims/jvmtiManageCapabilities.hpp" 441556Srgrimes#include "prims/jvmtiRawMonitor.hpp" 4536049Scharnier#include "prims/jvmtiRedefineClasses.hpp" 4690143Smarkm#include "prims/jvmtiTagMap.hpp" 4736049Scharnier#include "prims/jvmtiThreadState.inline.hpp" 48110391Scharnier#include "runtime/arguments.hpp" 4999110Sobrien#include "runtime/handles.hpp" 5099110Sobrien#include "runtime/interfaceSupport.hpp" 511556Srgrimes#include "runtime/javaCalls.hpp" 521556Srgrimes#include "runtime/objectMonitor.hpp" 53127546Sgad#include "runtime/objectMonitor.inline.hpp" 543296Sdg#include "runtime/os.inline.hpp" 551556Srgrimes#include "runtime/thread.inline.hpp" 561556Srgrimes#include "runtime/vframe.hpp" 571556Srgrimes#include "services/attachListener.hpp" 58137696Scsjp#include "services/serviceUtil.hpp" 591556Srgrimes#include "utilities/macros.hpp" 601556Srgrimes#if INCLUDE_ALL_GCS 611556Srgrimes#include "gc/parallel/psMarkSweep.hpp" 62127149Sgad#endif // INCLUDE_ALL_GCS 631556Srgrimes 64127499Sgad#ifdef JVMTI_TRACE 651556Srgrimes#define EVT_TRACE(evt,out) if ((JvmtiTrace::event_trace_flags(evt) & JvmtiTrace::SHOW_EVENT_SENT) != 0) { SafeResourceMark rm; log_trace(jvmti) out; } 6613514Smpp#define EVT_TRIG_TRACE(evt,out) if ((JvmtiTrace::event_trace_flags(evt) & JvmtiTrace::SHOW_EVENT_TRIGGER) != 0) { SafeResourceMark rm; log_trace(jvmti) out; } 6773367Sache#else 681556Srgrimes#define EVT_TRIG_TRACE(evt,out) 6990143Smarkm#define EVT_TRACE(evt,out) 701556Srgrimes#endif 711556Srgrimes 721556Srgrimes/////////////////////////////////////////////////////////////// 731556Srgrimes// 741556Srgrimes// JvmtiEventTransition 751556Srgrimes// 761556Srgrimes// TO DO -- 77127499Sgad// more handle purging 78127499Sgad 7966377Sbrian// Use this for JavaThreads and state is _thread_in_vm. 80127537Sgadclass JvmtiJavaThreadEventTransition : StackObj { 81127555Sgadprivate: 82127537Sgad ResourceMark _rm; 83127537Sgad ThreadToNativeFromVM _transition; 84127555Sgad HandleMark _hm; 85127537Sgad 86127537Sgadpublic: 87127537Sgad JvmtiJavaThreadEventTransition(JavaThread *thread) : 88129914Sgad _rm(), 89129971Sgad _transition(thread), 90129971Sgad _hm(thread) {}; 91129914Sgad}; 92129971Sgad 93129914Sgad// For JavaThreads which are not in _thread_in_vm state 94127537Sgad// and other system threads use this. 95127537Sgadclass JvmtiThreadEventTransition : StackObj { 96127537Sgadprivate: 97127537Sgad ResourceMark _rm; 98127537Sgad HandleMark _hm; 99127537Sgad JavaThreadState _saved_state; 100127537Sgad JavaThread *_jthread; 101127537Sgad 102130999Sgadpublic: 1031556Srgrimes JvmtiThreadEventTransition(Thread *thread) : _rm(), _hm() { 104127537Sgad if (thread->is_Java_thread()) { 105127537Sgad _jthread = (JavaThread *)thread; 106127537Sgad _saved_state = _jthread->thread_state(); 107127537Sgad if (_saved_state == _thread_in_Java) { 108127537Sgad ThreadStateTransition::transition_from_java(_jthread, _thread_in_native); 109127537Sgad } else { 110127537Sgad ThreadStateTransition::transition(_jthread, _saved_state, _thread_in_native); 1111556Srgrimes } 112127537Sgad } else { 11397966Sjmallett _jthread = NULL; 114127499Sgad } 115127537Sgad } 116127499Sgad 117127499Sgad ~JvmtiThreadEventTransition() { 118127499Sgad if (_jthread != NULL) 119127499Sgad ThreadStateTransition::transition_from_native(_jthread, _saved_state); 120127499Sgad } 121127499Sgad}; 122127499Sgad 123127499Sgad 124127499Sgad/////////////////////////////////////////////////////////////// 125127499Sgad// 126127499Sgad// JvmtiEventMark 127127499Sgad// 128127499Sgad 129127823Sgadclass JvmtiEventMark : public StackObj { 130127499Sgadprivate: 131127499Sgad JavaThread *_thread; 132137696Scsjp JNIEnv* _jni_env; 133127499Sgad bool _exception_detected; 134127499Sgad bool _exception_caught; 135127499Sgad#if 0 136127499Sgad JNIHandleBlock* _hblock; 137127499Sgad#endif 138127536Sgad 139127499Sgadpublic: 140127598Sgad JvmtiEventMark(JavaThread *thread) : _thread(thread), 141127598Sgad _jni_env(thread->jni_environment()) { 142127536Sgad#if 0 143127499Sgad _hblock = thread->active_handles(); 144127499Sgad _hblock->clear_thoroughly(); // so we can be safe 145129952Sgad#else 146127536Sgad // we want to use the code above - but that needs the JNIHandle changes - later... 147127536Sgad // for now, steal JNI push local frame code 148127536Sgad JvmtiThreadState *state = thread->jvmti_thread_state(); 149127536Sgad // we are before an event. 150127536Sgad // Save current jvmti thread exception state. 151127499Sgad if (state != NULL) { 15297875Sjmallett _exception_detected = state->is_exception_detected(); 153129635Sgad _exception_caught = state->is_exception_caught(); 154127538Sgad } else { 155127538Sgad _exception_detected = false; 15690143Smarkm _exception_caught = false; 15797875Sjmallett } 15897875Sjmallett 159127538Sgad JNIHandleBlock* old_handles = thread->active_handles(); 160127538Sgad JNIHandleBlock* new_handles = JNIHandleBlock::allocate_block(thread); 161105831Srwatson assert(new_handles != NULL, "should not be NULL"); 1621556Srgrimes new_handles->set_pop_frame_link(old_handles); 163127843Sgad thread->set_active_handles(new_handles); 16498494Ssobomax#endif 1651556Srgrimes assert(thread == JavaThread::current(), "thread must be current!"); 16690110Simp thread->frame_anchor()->make_walkable(thread); 1671556Srgrimes }; 168127499Sgad 169127499Sgad ~JvmtiEventMark() { 1701556Srgrimes#if 0 171130816Sgad _hblock->clear(); // for consistency with future correct behavior 1721556Srgrimes#else 1731556Srgrimes // we want to use the code above - but that needs the JNIHandle changes - later... 174129914Sgad // for now, steal JNI pop local frame code 175127539Sgad JNIHandleBlock* old_handles = _thread->active_handles(); 176137670Sru JNIHandleBlock* new_handles = old_handles->pop_frame_link(); 177129914Sgad assert(new_handles != NULL, "should not be NULL"); 178127499Sgad _thread->set_active_handles(new_handles); 17990143Smarkm // Note that we set the pop_frame_link to NULL explicitly, otherwise 1801556Srgrimes // the release_block call will release the blocks. 18111809Sache old_handles->set_pop_frame_link(NULL); 182127542Sgad JNIHandleBlock::release_block(old_handles, _thread); // may block 18311809Sache#endif 18497804Stjr 18597804Stjr JvmtiThreadState* state = _thread->jvmti_thread_state(); 18697804Stjr // we are continuing after an event. 1871556Srgrimes if (state != NULL) { 1881556Srgrimes // Restore the jvmti thread exception state. 1891556Srgrimes if (_exception_detected) { 1901556Srgrimes state->set_exception_detected(); 1911556Srgrimes } 1921556Srgrimes if (_exception_caught) { 1931556Srgrimes state->set_exception_caught(); 19498494Ssobomax } 195129914Sgad } 196129914Sgad } 19798494Ssobomax 198129914Sgad#if 0 199129915Sgad jobject to_jobject(oop obj) { return obj == NULL? NULL : _hblock->allocate_handle_fast(obj); } 2001556Srgrimes#else 201137670Sru // we want to use the code above - but that needs the JNIHandle changes - later... 202127542Sgad // for now, use regular make_local 203127542Sgad jobject to_jobject(oop obj) { return JNIHandles::make_local(_thread,obj); } 204127499Sgad#endif 205127499Sgad 206127499Sgad jclass to_jclass(Klass* klass) { return (klass == NULL ? NULL : (jclass)to_jobject(klass->java_mirror())); } 207127499Sgad 208127499Sgad jmethodID to_jmethodID(methodHandle method) { return method->jmethod_id(); } 209127499Sgad 210127499Sgad JNIEnv* jni_env() { return _jni_env; } 21189909Sru}; 21298494Ssobomax 213129967Sgadclass JvmtiThreadEventMark : public JvmtiEventMark { 214127499Sgadprivate: 215127499Sgad jthread _jt; 216127499Sgad 217127499Sgadpublic: 218127499Sgad JvmtiThreadEventMark(JavaThread *thread) : 219127499Sgad JvmtiEventMark(thread) { 220127499Sgad _jt = (jthread)(to_jobject(thread->threadObj())); 221127499Sgad }; 222127499Sgad jthread jni_thread() { return _jt; } 2231556Srgrimes}; 224127499Sgad 2251556Srgrimesclass JvmtiClassEventMark : public JvmtiThreadEventMark { 2261556Srgrimesprivate: 22719068Speter jclass _jc; 22819068Speter 22919068Speterpublic: 23019068Speter JvmtiClassEventMark(JavaThread *thread, Klass* klass) : 23119068Speter JvmtiThreadEventMark(thread) { 23219068Speter _jc = to_jclass(klass); 2331556Srgrimes }; 2341556Srgrimes jclass jni_class() { return _jc; } 2351556Srgrimes}; 236127506Sgad 237127506Sgadclass JvmtiMethodEventMark : public JvmtiThreadEventMark { 238127506Sgadprivate: 239127542Sgad jmethodID _mid; 240127506Sgad 241127506Sgadpublic: 242127499Sgad JvmtiMethodEventMark(JavaThread *thread, methodHandle method) : 243127499Sgad JvmtiThreadEventMark(thread), 244127499Sgad _mid(to_jmethodID(method)) {}; 245127499Sgad jmethodID jni_methodID() { return _mid; } 246127499Sgad}; 247127542Sgad 248127499Sgadclass JvmtiLocationEventMark : public JvmtiMethodEventMark { 249127597Sgadprivate: 250127542Sgad jlocation _loc; 251127542Sgad 252127542Sgadpublic: 253127542Sgad JvmtiLocationEventMark(JavaThread *thread, methodHandle method, address location) : 254127499Sgad JvmtiMethodEventMark(thread, method), 255127499Sgad _loc(location - method->code_base()) {}; 256127499Sgad jlocation location() { return _loc; } 257127499Sgad}; 258127499Sgad 259127542Sgadclass JvmtiExceptionEventMark : public JvmtiLocationEventMark { 2601556Srgrimesprivate: 261127499Sgad jobject _exc; 262116265Sscottl 263126127Sdeischenpublic: 264116265Sscottl JvmtiExceptionEventMark(JavaThread *thread, methodHandle method, address location, Handle exception) : 2651556Srgrimes JvmtiLocationEventMark(thread, method, location), 2661556Srgrimes _exc(to_jobject(exception())) {}; 2671556Srgrimes jobject exception() { return _exc; } 2681556Srgrimes}; 269109502Sjmallett 27090143Smarkmclass JvmtiClassFileLoadEventMark : public JvmtiThreadEventMark { 2711556Srgrimesprivate: 2721556Srgrimes const char *_class_name; 2731556Srgrimes jobject _jloader; 2741556Srgrimes jobject _protection_domain; 2751556Srgrimes jclass _class_being_redefined; 2761556Srgrimes 277109502Sjmallettpublic: 27890143Smarkm JvmtiClassFileLoadEventMark(JavaThread *thread, Symbol* name, 2791556Srgrimes Handle class_loader, Handle prot_domain, KlassHandle *class_being_redefined) : JvmtiThreadEventMark(thread) { 2801556Srgrimes _class_name = name != NULL? name->as_utf8() : NULL; 2811556Srgrimes _jloader = (jobject)to_jobject(class_loader()); 2821556Srgrimes _protection_domain = (jobject)to_jobject(prot_domain()); 2831556Srgrimes if (class_being_redefined == NULL) { 2841556Srgrimes _class_being_redefined = NULL; 2851556Srgrimes } else { 2861556Srgrimes _class_being_redefined = (jclass)to_jclass((*class_being_redefined)()); 2871556Srgrimes } 2881556Srgrimes }; 2891556Srgrimes const char *class_name() { 2901556Srgrimes return _class_name; 291109502Sjmallett } 292109502Sjmallett jobject jloader() { 293109502Sjmallett return _jloader; 2941556Srgrimes } 29590143Smarkm jobject protection_domain() { 2961556Srgrimes return _protection_domain; 2971556Srgrimes } 298109502Sjmallett jclass class_being_redefined() { 29990143Smarkm return _class_being_redefined; 3001556Srgrimes } 3011556Srgrimes}; 302127499Sgad 303127499Sgad////////////////////////////////////////////////////////////////////////////// 304127499Sgad 305127499Sgadint JvmtiExport::_field_access_count = 0; 306127499Sgadint JvmtiExport::_field_modification_count = 0; 307127499Sgad 308127499Sgadbool JvmtiExport::_can_access_local_variables = false; 309127499Sgadbool JvmtiExport::_can_hotswap_or_post_breakpoint = false; 3101556Srgrimesbool JvmtiExport::_can_modify_any_class = false; 311127499Sgadbool JvmtiExport::_can_walk_any_space = false; 312127499Sgad 313127597Sgadbool JvmtiExport::_has_redefined_a_class = false; 314127542Sgadbool JvmtiExport::_all_dependencies_are_recorded = false; 315127542Sgad 316127542Sgad// 317127542Sgad// field access management 318127542Sgad// 319127542Sgad 320127499Sgad// interpreter generator needs the address of the counter 321127499Sgadaddress JvmtiExport::get_field_access_count_addr() { 322127499Sgad // We don't grab a lock because we don't want to 323127499Sgad // serialize field access between all threads. This means that a 324127499Sgad // thread on another processor can see the wrong count value and 3251556Srgrimes // may either miss making a needed call into post_field_access() 3261556Srgrimes // or will make an unneeded call into post_field_access(). We pay 3271556Srgrimes // this price to avoid slowing down the VM when we aren't watching 3281556Srgrimes // field accesses. 3291556Srgrimes // Other access/mutation safe by virtue of being in VM state. 3301556Srgrimes return (address)(&_field_access_count); 331127499Sgad} 332127499Sgad 333127597Sgad// 334127542Sgad// field modification management 335127542Sgad// 336127542Sgad 337127542Sgad// interpreter generator needs the address of the counter 338127542Sgadaddress JvmtiExport::get_field_modification_count_addr() { 339127499Sgad // We don't grab a lock because we don't 340127499Sgad // want to serialize field modification between all threads. This 341127499Sgad // means that a thread on another processor can see the wrong 342127499Sgad // count value and may either miss making a needed call into 343127499Sgad // post_field_modification() or will make an unneeded call into 3441556Srgrimes // post_field_modification(). We pay this price to avoid slowing 3451556Srgrimes // down the VM when we aren't watching field modifications. 3461556Srgrimes // Other access/mutation safe by virtue of being in VM state. 3471556Srgrimes return (address)(&_field_modification_count); 348127499Sgad} 349127499Sgad 350127499Sgad 351127499Sgad/////////////////////////////////////////////////////////////// 3521556Srgrimes// Functions needed by java.lang.instrument for starting up javaagent. 35313020Speter/////////////////////////////////////////////////////////////// 354127499Sgad 355127499Sgadjint 356127499SgadJvmtiExport::get_jvmti_interface(JavaVM *jvm, void **penv, jint version) { 357127499Sgad // The JVMTI_VERSION_INTERFACE_JVMTI part of the version number 35813020Speter // has already been validated in JNI GetEnv(). 3591556Srgrimes int major, minor, micro; 360109502Sjmallett 3611556Srgrimes // micro version doesn't matter here (yet?) 36290143Smarkm decode_version_values(version, &major, &minor, µ); 3631556Srgrimes switch (major) { 3641556Srgrimes case 1: 3651556Srgrimes switch (minor) { 366109502Sjmallett case 0: // version 1.0.<micro> is recognized 3671556Srgrimes case 1: // version 1.1.<micro> is recognized 36890143Smarkm case 2: // version 1.2.<micro> is recognized 3691556Srgrimes break; 3701556Srgrimes 3711556Srgrimes default: 3721556Srgrimes return JNI_EVERSION; // unsupported minor version number 3731556Srgrimes } 3741556Srgrimes break; 3751556Srgrimes case 9: 3761556Srgrimes switch (minor) { 3771556Srgrimes case 0: // version 9.0.<micro> is recognized 378127499Sgad break; 379127499Sgad default: 380127499Sgad return JNI_EVERSION; // unsupported minor version number 381127499Sgad } 382127499Sgad break; 383127499Sgad default: 384127499Sgad return JNI_EVERSION; // unsupported major version number 385127499Sgad } 386127499Sgad 387127499Sgad if (JvmtiEnv::get_phase() == JVMTI_PHASE_LIVE) { 388127499Sgad JavaThread* current_thread = JavaThread::current(); 389127499Sgad // transition code: native to VM 390127499Sgad ThreadInVMfromNative __tiv(current_thread); 391127499Sgad VM_ENTRY_BASE(jvmtiEnv*, JvmtiExport::get_jvmti_interface, current_thread) 3921556Srgrimes debug_only(VMNativeEntryWrapper __vew;) 393127499Sgad 3941556Srgrimes JvmtiEnv *jvmti_env = JvmtiEnv::create_a_jvmti(version); 39586922Sgreen *penv = jvmti_env->jvmti_external(); // actual type is jvmtiEnv* -- not to be confused with JvmtiEnv* 396109502Sjmallett return JNI_OK; 39786922Sgreen 39886922Sgreen } else if (JvmtiEnv::get_phase() == JVMTI_PHASE_ONLOAD) { 3991556Srgrimes // not live, no thread to transition 4001556Srgrimes JvmtiEnv *jvmti_env = JvmtiEnv::create_a_jvmti(version); 4011556Srgrimes *penv = jvmti_env->jvmti_external(); // actual type is jvmtiEnv* -- not to be confused with JvmtiEnv* 4021556Srgrimes return JNI_OK; 4031556Srgrimes 4041556Srgrimes } else { 405129914Sgad // Called at the wrong time 406129914Sgad *penv = NULL; 407137696Scsjp return JNI_EDETACHED; 408137696Scsjp } 409137696Scsjp} 410137696Scsjp 411137696Scsjpvoid 412137696ScsjpJvmtiExport::add_default_read_edges(Handle h_module, TRAPS) { 413137696Scsjp if (!Universe::is_module_initialized()) { 414137696Scsjp return; // extra safety 415129914Sgad } 416129914Sgad assert(!h_module.is_null(), "module should always be set"); 417129914Sgad 418129914Sgad // Invoke the transformedByAgent method 419129914Sgad JavaValue result(T_VOID); 420129914Sgad JavaCalls::call_static(&result, 421129914Sgad SystemDictionary::module_Modules_klass(), 422129914Sgad vmSymbols::transformedByAgent_name(), 423129914Sgad vmSymbols::transformedByAgent_signature(), 424129914Sgad h_module, 425129914Sgad THREAD); 426129914Sgad 427129914Sgad if (HAS_PENDING_EXCEPTION) { 428129914Sgad LogTarget(Trace, jvmti) log; 429127499Sgad LogStreamCHeap log_stream(log); 430127542Sgad java_lang_Throwable::print(PENDING_EXCEPTION, &log_stream); 431127542Sgad log_stream.cr(); 432127499Sgad CLEAR_PENDING_EXCEPTION; 433127499Sgad return; 43489909Sru } 4351556Srgrimes} 4361556Srgrimes 4371556SrgrimesjvmtiError 43890143SmarkmJvmtiExport::add_module_reads(Handle module, Handle to_module, TRAPS) { 439109502Sjmallett if (!Universe::is_module_initialized()) { 4401556Srgrimes return JVMTI_ERROR_NONE; // extra safety 441127499Sgad } 442127823Sgad assert(!module.is_null(), "module should always be set"); 443127823Sgad assert(!to_module.is_null(), "to_module should always be set"); 44497877Sjmallett 445127499Sgad // Invoke the addReads method 446127499Sgad JavaValue result(T_VOID); 447127823Sgad JavaCalls::call_static(&result, 44866377Sbrian SystemDictionary::module_Modules_klass(), 4491556Srgrimes vmSymbols::addReads_name(), 4501556Srgrimes vmSymbols::addReads_signature(), 4511556Srgrimes module, 45253170Skris to_module, 4531556Srgrimes THREAD); 4541556Srgrimes 455127499Sgad if (HAS_PENDING_EXCEPTION) { 4561556Srgrimes LogTarget(Trace, jvmti) log; 457127499Sgad LogStreamCHeap log_stream(log); 458127499Sgad java_lang_Throwable::print(PENDING_EXCEPTION, &log_stream); 459127499Sgad log_stream.cr(); 460127499Sgad CLEAR_PENDING_EXCEPTION; 461127499Sgad return JVMTI_ERROR_INTERNAL; 4621556Srgrimes } 463127499Sgad return JVMTI_ERROR_NONE; 464127499Sgad} 465127499Sgad 466129600SgadjvmtiError 467129600SgadJvmtiExport::add_module_exports(Handle module, Handle pkg_name, Handle to_module, TRAPS) { 468129600Sgad if (!Universe::is_module_initialized()) { 469129600Sgad return JVMTI_ERROR_NONE; // extra safety 470129600Sgad } 471127499Sgad assert(!module.is_null(), "module should always be set"); 472127823Sgad assert(!to_module.is_null(), "to_module should always be set"); 473127499Sgad assert(!pkg_name.is_null(), "pkg_name should always be set"); 474127499Sgad 475127499Sgad // Invoke the addExports method 476127823Sgad JavaValue result(T_VOID); 477127499Sgad JavaCalls::call_static(&result, 478127499Sgad SystemDictionary::module_Modules_klass(), 479127499Sgad vmSymbols::addExports_name(), 480127823Sgad vmSymbols::addExports_signature(), 481127499Sgad module, 482127499Sgad pkg_name, 483127499Sgad to_module, 484127823Sgad THREAD); 485127499Sgad 486127499Sgad if (HAS_PENDING_EXCEPTION) { 487127499Sgad Symbol* ex_name = PENDING_EXCEPTION->klass()->name(); 488127823Sgad LogTarget(Trace, jvmti) log; 489127499Sgad LogStreamCHeap log_stream(log); 490127499Sgad java_lang_Throwable::print(PENDING_EXCEPTION, &log_stream); 491127499Sgad log_stream.cr(); 492127823Sgad CLEAR_PENDING_EXCEPTION; 493127499Sgad if (ex_name == vmSymbols::java_lang_IllegalArgumentException()) { 494127499Sgad return JVMTI_ERROR_ILLEGAL_ARGUMENT; 495127499Sgad } 496127499Sgad return JVMTI_ERROR_INTERNAL; 497127499Sgad } 4981556Srgrimes return JVMTI_ERROR_NONE; 499126127Sdeischen} 5001556Srgrimes 5011556SrgrimesjvmtiError 5021556SrgrimesJvmtiExport::add_module_opens(Handle module, Handle pkg_name, Handle to_module, TRAPS) { 503127499Sgad if (!Universe::is_module_initialized()) { 504127149Sgad return JVMTI_ERROR_NONE; // extra safety 505127544Sgad } 5061556Srgrimes assert(!module.is_null(), "module should always be set"); 507127499Sgad assert(!to_module.is_null(), "to_module should always be set"); 508127149Sgad assert(!pkg_name.is_null(), "pkg_name should always be set"); 509127149Sgad 510127149Sgad // Invoke the addOpens method 511127149Sgad JavaValue result(T_VOID); 512127499Sgad JavaCalls::call_static(&result, 513127499Sgad SystemDictionary::module_Modules_klass(), 514127499Sgad vmSymbols::addOpens_name(), 515127499Sgad vmSymbols::addExports_signature(), 516127499Sgad module, 517127499Sgad pkg_name, 518127499Sgad to_module, 519127823Sgad THREAD); 520127499Sgad 521127499Sgad if (HAS_PENDING_EXCEPTION) { 522127499Sgad Symbol* ex_name = PENDING_EXCEPTION->klass()->name(); 523127499Sgad LogTarget(Trace, jvmti) log; 524127499Sgad LogStreamCHeap log_stream(log); 525127499Sgad java_lang_Throwable::print(PENDING_EXCEPTION, &log_stream); 526127499Sgad log_stream.cr(); 527127499Sgad CLEAR_PENDING_EXCEPTION; 528127499Sgad if (ex_name == vmSymbols::java_lang_IllegalArgumentException()) { 529127499Sgad return JVMTI_ERROR_ILLEGAL_ARGUMENT; 530127499Sgad } 531127499Sgad return JVMTI_ERROR_INTERNAL; 532127499Sgad } 533127499Sgad return JVMTI_ERROR_NONE; 534127499Sgad} 535127499Sgad 536127823SgadjvmtiError 537127499SgadJvmtiExport::add_module_uses(Handle module, Handle service, TRAPS) { 538127499Sgad if (!Universe::is_module_initialized()) { 539127499Sgad return JVMTI_ERROR_NONE; // extra safety 540127499Sgad } 541127823Sgad assert(!module.is_null(), "module should always be set"); 542127823Sgad assert(!service.is_null(), "service should always be set"); 543127499Sgad 544127499Sgad // Invoke the addUses method 545127499Sgad JavaValue result(T_VOID); 546127499Sgad JavaCalls::call_static(&result, 547127823Sgad SystemDictionary::module_Modules_klass(), 548127823Sgad vmSymbols::addUses_name(), 549127499Sgad vmSymbols::addUses_signature(), 550127499Sgad module, 551127499Sgad service, 552127499Sgad THREAD); 553127823Sgad 554127499Sgad if (HAS_PENDING_EXCEPTION) { 555127499Sgad LogTarget(Trace, jvmti) log; 556127499Sgad LogStreamCHeap log_stream(log); 557127499Sgad java_lang_Throwable::print(PENDING_EXCEPTION, &log_stream); 558127823Sgad log_stream.cr(); 559127499Sgad CLEAR_PENDING_EXCEPTION; 560127499Sgad return JVMTI_ERROR_INTERNAL; 561127499Sgad } 562127499Sgad return JVMTI_ERROR_NONE; 563127823Sgad} 564127499Sgad 565127499SgadjvmtiError 566127499SgadJvmtiExport::add_module_provides(Handle module, Handle service, Handle impl_class, TRAPS) { 567127499Sgad if (!Universe::is_module_initialized()) { 568127499Sgad return JVMTI_ERROR_NONE; // extra safety 569127499Sgad } 570127499Sgad assert(!module.is_null(), "module should always be set"); 571127499Sgad assert(!service.is_null(), "service should always be set"); 572127499Sgad assert(!impl_class.is_null(), "impl_class should always be set"); 573130816Sgad 574130816Sgad // Invoke the addProvides method 575130816Sgad JavaValue result(T_VOID); 576130816Sgad JavaCalls::call_static(&result, 577130816Sgad SystemDictionary::module_Modules_klass(), 578130816Sgad vmSymbols::addProvides_name(), 579127149Sgad vmSymbols::addProvides_signature(), 580130816Sgad module, 581130816Sgad service, 582127499Sgad impl_class, 583127149Sgad THREAD); 5841556Srgrimes 58525271Sjkh if (HAS_PENDING_EXCEPTION) { 58625271Sjkh LogTarget(Trace, jvmti) log; 58725271Sjkh LogStreamCHeap log_stream(log); 5881556Srgrimes java_lang_Throwable::print(PENDING_EXCEPTION, &log_stream); 5891556Srgrimes log_stream.cr(); 5901556Srgrimes CLEAR_PENDING_EXCEPTION; 5911556Srgrimes return JVMTI_ERROR_INTERNAL; 592127499Sgad } 59362803Swill return JVMTI_ERROR_NONE; 594127499Sgad} 5951556Srgrimes 5961556Srgrimesvoid 5971556SrgrimesJvmtiExport::decode_version_values(jint version, int * major, int * minor, 598127499Sgad int * micro) { 5991556Srgrimes *major = (version & JVMTI_VERSION_MASK_MAJOR) >> JVMTI_VERSION_SHIFT_MAJOR; 600127499Sgad *minor = (version & JVMTI_VERSION_MASK_MINOR) >> JVMTI_VERSION_SHIFT_MINOR; 6011556Srgrimes *micro = (version & JVMTI_VERSION_MASK_MICRO) >> JVMTI_VERSION_SHIFT_MICRO; 602127499Sgad} 603130999Sgad 6041556Srgrimesvoid JvmtiExport::enter_primordial_phase() { 605130999Sgad JvmtiEnvBase::set_phase(JVMTI_PHASE_PRIMORDIAL); 6061556Srgrimes} 6071556Srgrimes 6081556Srgrimesvoid JvmtiExport::enter_early_start_phase() { 6091556Srgrimes JvmtiManageCapabilities::recompute_always_capabilities(); 6101556Srgrimes set_early_vmstart_recorded(true); 6111556Srgrimes} 6121556Srgrimes 6131556Srgrimesvoid JvmtiExport::enter_start_phase() { 6141556Srgrimes JvmtiManageCapabilities::recompute_always_capabilities(); 615127499Sgad JvmtiEnvBase::set_phase(JVMTI_PHASE_START); 616127499Sgad} 617127499Sgad 618127499Sgadvoid JvmtiExport::enter_onload_phase() { 619127499Sgad JvmtiEnvBase::set_phase(JVMTI_PHASE_ONLOAD); 620127499Sgad} 621127499Sgad 62266377Sbrianvoid JvmtiExport::enter_live_phase() { 6231556Srgrimes JvmtiEnvBase::set_phase(JVMTI_PHASE_LIVE); 6241556Srgrimes} 6251556Srgrimes 626127499Sgad// 627127499Sgad// JVMTI events that the VM posts to the debugger and also startup agent 628127499Sgad// and call the agent's premain() for java.lang.instrument. 629127499Sgad// 630127499Sgad 631127499Sgadvoid JvmtiExport::post_early_vm_start() { 632127602Sgad EVT_TRIG_TRACE(JVMTI_EVENT_VM_START, ("Trg Early VM start event triggered" )); 633127499Sgad 634127499Sgad // can now enable some events 635127499Sgad JvmtiEventController::vm_start(); 636127499Sgad 637127499Sgad JvmtiEnvIterator it; 638127499Sgad for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 639127499Sgad // Only early vmstart envs post early VMStart event 640127542Sgad if (env->early_vmstart_env() && env->is_enabled(JVMTI_EVENT_VM_START)) { 641127499Sgad EVT_TRACE(JVMTI_EVENT_VM_START, ("Evt Early VM start event sent" )); 642127499Sgad JavaThread *thread = JavaThread::current(); 643127499Sgad JvmtiThreadEventMark jem(thread); 644127499Sgad JvmtiJavaThreadEventTransition jet(thread); 645127499Sgad jvmtiEventVMStart callback = env->callbacks()->VMStart; 646127499Sgad if (callback != NULL) { 647127499Sgad (*callback)(env->jvmti_external(), jem.jni_env()); 648127499Sgad } 649127499Sgad } 650127499Sgad } 651127499Sgad} 652127499Sgad 653127499Sgadvoid JvmtiExport::post_vm_start() { 654127499Sgad EVT_TRIG_TRACE(JVMTI_EVENT_VM_START, ("Trg VM start event triggered" )); 655127602Sgad 656127602Sgad // can now enable some events 657127499Sgad JvmtiEventController::vm_start(); 658127602Sgad 659127499Sgad JvmtiEnvIterator it; 660127499Sgad for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 661127499Sgad // Early vmstart envs do not post normal VMStart event 662127499Sgad if (!env->early_vmstart_env() && env->is_enabled(JVMTI_EVENT_VM_START)) { 663127499Sgad EVT_TRACE(JVMTI_EVENT_VM_START, ("Evt VM start event sent" )); 664127499Sgad 665129967Sgad JavaThread *thread = JavaThread::current(); 666127499Sgad JvmtiThreadEventMark jem(thread); 667127499Sgad JvmtiJavaThreadEventTransition jet(thread); 668127499Sgad jvmtiEventVMStart callback = env->callbacks()->VMStart; 669127823Sgad if (callback != NULL) { 670127499Sgad (*callback)(env->jvmti_external(), jem.jni_env()); 671127499Sgad } 672127499Sgad } 673127597Sgad } 674127499Sgad} 675127499Sgad 676127149Sgad 677127539Sgadvoid JvmtiExport::post_vm_initialized() { 678127149Sgad EVT_TRIG_TRACE(JVMTI_EVENT_VM_INIT, ("Trg VM init event triggered" )); 679127149Sgad 680129917Sgad // can now enable events 681129917Sgad JvmtiEventController::vm_init(); 682129917Sgad 683129917Sgad JvmtiEnvIterator it; 684127149Sgad for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 685127149Sgad if (env->is_enabled(JVMTI_EVENT_VM_INIT)) { 686129952Sgad EVT_TRACE(JVMTI_EVENT_VM_INIT, ("Evt VM init event sent" )); 687129952Sgad 688129952Sgad JavaThread *thread = JavaThread::current(); 689129952Sgad JvmtiThreadEventMark jem(thread); 690129952Sgad JvmtiJavaThreadEventTransition jet(thread); 691129952Sgad jvmtiEventVMInit callback = env->callbacks()->VMInit; 692129952Sgad if (callback != NULL) { 693129952Sgad (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread()); 694129952Sgad } 695129952Sgad } 696129952Sgad } 697129967Sgad} 698129952Sgad 699127499Sgad 700127499Sgadvoid JvmtiExport::post_vm_death() { 701127823Sgad EVT_TRIG_TRACE(JVMTI_EVENT_VM_DEATH, ("Trg VM death event triggered" )); 702127499Sgad 703127499Sgad JvmtiEnvIterator it; 704127499Sgad for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 705127149Sgad if (env->is_enabled(JVMTI_EVENT_VM_DEATH)) { 706131010Sgad EVT_TRACE(JVMTI_EVENT_VM_DEATH, ("Evt VM death event sent" )); 707131010Sgad 708131010Sgad JavaThread *thread = JavaThread::current(); 709131010Sgad JvmtiEventMark jem(thread); 710131010Sgad JvmtiJavaThreadEventTransition jet(thread); 711131010Sgad jvmtiEventVMDeath callback = env->callbacks()->VMDeath; 712131010Sgad if (callback != NULL) { 713127499Sgad (*callback)(env->jvmti_external(), jem.jni_env()); 714127499Sgad } 715127499Sgad } 716127539Sgad } 717127539Sgad 718131209Sgad JvmtiEnvBase::set_phase(JVMTI_PHASE_DEAD); 719127499Sgad JvmtiEventController::vm_death(); 720131010Sgad} 721131209Sgad 722131010Sgadchar** 723131010SgadJvmtiExport::get_all_native_method_prefixes(int* count_ptr) { 724127499Sgad // Have to grab JVMTI thread state lock to be sure environment doesn't 725131010Sgad // go away while we iterate them. No locks during VM bring-up. 726131010Sgad if (Threads::number_of_threads() == 0 || SafepointSynchronize::is_at_safepoint()) { 727131010Sgad return JvmtiEnvBase::get_all_native_method_prefixes(count_ptr); 728131010Sgad } else { 729131010Sgad MutexLocker mu(JvmtiThreadState_lock); 730131010Sgad return JvmtiEnvBase::get_all_native_method_prefixes(count_ptr); 731131010Sgad } 732131010Sgad} 733131010Sgad 734131010Sgadclass JvmtiClassFileLoadHookPoster : public StackObj { 735131010Sgad private: 736131209Sgad Symbol* _h_name; 737131010Sgad Handle _class_loader; 738131010Sgad Handle _h_protection_domain; 739131010Sgad unsigned char ** _data_ptr; 740131209Sgad unsigned char ** _end_ptr; 741131209Sgad JavaThread * _thread; 742131209Sgad jint _curr_len; 743131209Sgad unsigned char * _curr_data; 744131010Sgad JvmtiEnv * _curr_env; 745131010Sgad JvmtiCachedClassFileData ** _cached_class_file_ptr; 746131010Sgad JvmtiThreadState * _state; 747131010Sgad KlassHandle * _h_class_being_redefined; 748131010Sgad JvmtiClassLoadKind _load_kind; 749127499Sgad bool _has_been_modified; 750131010Sgad 751131010Sgad public: 752131209Sgad inline JvmtiClassFileLoadHookPoster(Symbol* h_name, Handle class_loader, 753131209Sgad Handle h_protection_domain, 754131209Sgad unsigned char **data_ptr, unsigned char **end_ptr, 755131209Sgad JvmtiCachedClassFileData **cache_ptr) { 756131010Sgad _h_name = h_name; 757131010Sgad _class_loader = class_loader; 758131010Sgad _h_protection_domain = h_protection_domain; 759131010Sgad _data_ptr = data_ptr; 760131209Sgad _end_ptr = end_ptr; 761131209Sgad _thread = JavaThread::current(); 762131209Sgad _curr_len = *end_ptr - *data_ptr; 763131209Sgad _curr_data = *data_ptr; 764131209Sgad _curr_env = NULL; 765131010Sgad _cached_class_file_ptr = cache_ptr; 766131010Sgad _has_been_modified = false; 767131010Sgad 768127499Sgad _state = _thread->jvmti_thread_state(); 769127499Sgad if (_state != NULL) { 770127499Sgad _h_class_being_redefined = _state->get_class_being_redefined(); 771127823Sgad _load_kind = _state->get_class_load_kind(); 772127499Sgad Klass* klass = (_h_class_being_redefined == NULL) ? NULL : (*_h_class_being_redefined)(); 773127149Sgad if (_load_kind != jvmti_class_load_kind_load && klass != NULL) { 774127149Sgad ModuleEntry* module_entry = InstanceKlass::cast(klass)->module(); 775127499Sgad assert(module_entry != NULL, "module_entry should always be set"); 776127499Sgad if (module_entry->is_named() && 77766377Sbrian module_entry->module() != NULL && 77866377Sbrian !module_entry->has_default_read_edges()) { 779127539Sgad if (!module_entry->set_has_default_read_edges()) { 780127602Sgad // We won a potential race. 78166377Sbrian // Add read edges to the unnamed modules of the bootstrap and app class loaders 782127499Sgad Handle class_module(_thread, JNIHandles::resolve(module_entry->module())); // Obtain j.l.r.Module 783127499Sgad JvmtiExport::add_default_read_edges(class_module, _thread); 784127499Sgad } 785127499Sgad } 786127499Sgad } 787127499Sgad // Clear class_being_redefined flag here. The action 788127542Sgad // from agent handler could generate a new class file load 789127499Sgad // hook event and if it is not cleared the new event generated 79066377Sbrian // from regular class file load could have this stale redefined 791127499Sgad // class handle info. 792127499Sgad _state->clear_class_being_redefined(); 793127499Sgad } else { 794127602Sgad // redefine and retransform will always set the thread state 795127602Sgad _h_class_being_redefined = (KlassHandle *) NULL; 796127499Sgad _load_kind = jvmti_class_load_kind_load; 797127499Sgad } 798127499Sgad } 799127602Sgad 800127499Sgad void post() { 801127499Sgad post_all_envs(); 802127499Sgad copy_modified_data(); 80366377Sbrian } 804127499Sgad 805127499Sgad bool has_been_modified() { return _has_been_modified; } 806127509Sgad 807127509Sgad private: 808127509Sgad void post_all_envs() { 809127509Sgad if (_load_kind != jvmti_class_load_kind_retransform) { 810127509Sgad // for class load and redefine, 811127509Sgad // call the non-retransformable agents 812129967Sgad JvmtiEnvIterator it; 813127499Sgad for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 814127499Sgad if (!env->is_retransformable() && env->is_enabled(JVMTI_EVENT_CLASS_FILE_LOAD_HOOK)) { 815127499Sgad // non-retransformable agents cannot retransform back, 816127823Sgad // so no need to cache the original class file bytes 817127499Sgad post_to_env(env, false); 818127499Sgad } 819127499Sgad } 820127499Sgad } 821127499Sgad JvmtiEnvIterator it; 822127499Sgad for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 823127499Sgad // retransformable agents get all events 824127499Sgad if (env->is_retransformable() && env->is_enabled(JVMTI_EVENT_CLASS_FILE_LOAD_HOOK)) { 825127499Sgad // retransformable agents need to cache the original class file 826127539Sgad // bytes if changes are made via the ClassFileLoadHook 827127499Sgad post_to_env(env, true); 828129917Sgad } 829129967Sgad } 830127499Sgad } 831127499Sgad 832127499Sgad void post_to_env(JvmtiEnv* env, bool caching_needed) { 833127499Sgad if (env->phase() == JVMTI_PHASE_PRIMORDIAL && !env->early_class_hook_env()) { 834127499Sgad return; 835127499Sgad } 836127499Sgad unsigned char *new_data = NULL; 837127499Sgad jint new_len = 0; 838127499Sgad JvmtiClassFileLoadEventMark jem(_thread, _h_name, _class_loader, 839127499Sgad _h_protection_domain, 840127499Sgad _h_class_being_redefined); 841127499Sgad JvmtiJavaThreadEventTransition jet(_thread); 842127499Sgad jvmtiEventClassFileLoadHook callback = env->callbacks()->ClassFileLoadHook; 84366377Sbrian if (callback != NULL) { 844127499Sgad (*callback)(env->jvmti_external(), jem.jni_env(), 845127499Sgad jem.class_being_redefined(), 846127542Sgad jem.jloader(), jem.class_name(), 847129953Sgad jem.protection_domain(), 848127542Sgad _curr_len, _curr_data, 849127499Sgad &new_len, &new_data); 850127499Sgad } 851127499Sgad if (new_data != NULL) { 852127499Sgad // this agent has modified class data. 853127499Sgad _has_been_modified = true; 854127499Sgad if (caching_needed && *_cached_class_file_ptr == NULL) { 855127499Sgad // data has been changed by the new retransformable agent 856127499Sgad // and it hasn't already been cached, cache it 857127499Sgad JvmtiCachedClassFileData *p; 858127499Sgad p = (JvmtiCachedClassFileData *)os::malloc( 859127499Sgad offset_of(JvmtiCachedClassFileData, data) + _curr_len, mtInternal); 860127499Sgad if (p == NULL) { 86166377Sbrian vm_exit_out_of_memory(offset_of(JvmtiCachedClassFileData, data) + _curr_len, 862127499Sgad OOM_MALLOC_ERROR, 863127499Sgad "unable to allocate cached copy of original class bytes"); 864127499Sgad } 865127499Sgad p->length = _curr_len; 866127499Sgad memcpy(p->data, _curr_data, _curr_len); 867127499Sgad *_cached_class_file_ptr = p; 868127499Sgad } 869127499Sgad 870127499Sgad if (_curr_data != *_data_ptr) { 871127499Sgad // curr_data is previous agent modified class data. 872127499Sgad // And this has been changed by the new agent so 873127499Sgad // we can delete it now. 874127499Sgad _curr_env->Deallocate(_curr_data); 87566377Sbrian } 876127499Sgad 87766377Sbrian // Class file data has changed by the current agent. 878127499Sgad _curr_data = new_data; 879127499Sgad _curr_len = new_len; 880127499Sgad // Save the current agent env we need this to deallocate the 881127539Sgad // memory allocated by this agent. 882127499Sgad _curr_env = env; 88366377Sbrian } 884127499Sgad } 885127823Sgad 886127499Sgad void copy_modified_data() { 887127823Sgad // if one of the agent has modified class file data. 888129967Sgad // Copy modified class data to new resources array. 889127499Sgad if (_curr_data != *_data_ptr) { 890127499Sgad *_data_ptr = NEW_RESOURCE_ARRAY(u1, _curr_len); 891127823Sgad memcpy(*_data_ptr, _curr_data, _curr_len); 892127499Sgad *_end_ptr = *_data_ptr + _curr_len; 893127499Sgad _curr_env->Deallocate(_curr_data); 89466377Sbrian } 89566377Sbrian } 896127499Sgad}; 897127499Sgad 898127499Sgadbool JvmtiExport::_should_post_class_file_load_hook = false; 899127499Sgad 900127499Sgad// this entry is for class file load hook on class load, redefine and retransform 901127823Sgadbool JvmtiExport::post_class_file_load_hook(Symbol* h_name, 902127823Sgad Handle class_loader, 903127499Sgad Handle h_protection_domain, 904127499Sgad unsigned char **data_ptr, 905127823Sgad unsigned char **end_ptr, 906127499Sgad JvmtiCachedClassFileData **cache_ptr) { 907127499Sgad if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) { 908127499Sgad return false; 909127499Sgad } 910127499Sgad 911127499Sgad JvmtiClassFileLoadHookPoster poster(h_name, class_loader, 912127499Sgad h_protection_domain, 913127499Sgad data_ptr, end_ptr, 914127499Sgad cache_ptr); 915127499Sgad poster.post(); 916127499Sgad return poster.has_been_modified(); 917127823Sgad} 918127499Sgad 919127499Sgadvoid JvmtiExport::report_unsupported(bool on) { 920109502Sjmallett // If any JVMTI service is turned on, we need to exit before native code 921109502Sjmallett // tries to access nonexistant services. 922109502Sjmallett if (on) { 923109502Sjmallett vm_exit_during_initialization("Java Kernel does not support JVMTI."); 924109502Sjmallett } 925130999Sgad} 926109502Sjmallett 927109502Sjmallett 928109502Sjmallettstatic inline Klass* oop_to_klass(oop obj) { 929109502Sjmallett Klass* k = obj->klass(); 930109502Sjmallett 931109502Sjmallett // if the object is a java.lang.Class then return the java mirror 9321556Srgrimes if (k == SystemDictionary::Class_klass()) { 93390110Simp if (!java_lang_Class::is_primitive(obj)) { 9341556Srgrimes k = java_lang_Class::as_Klass(obj); 9351556Srgrimes assert(k != NULL, "class for non-primitive mirror must exist"); 9361556Srgrimes } 93725271Sjkh } 938130999Sgad return k; 93925271Sjkh} 94025271Sjkh 94125271Sjkhclass JvmtiVMObjectAllocEventMark : public JvmtiClassEventMark { 94225271Sjkh private: 94325271Sjkh jobject _jobj; 94425271Sjkh jlong _size; 94525271Sjkh public: 94625271Sjkh JvmtiVMObjectAllocEventMark(JavaThread *thread, oop obj) : JvmtiClassEventMark(thread, oop_to_klass(obj)) { 94725271Sjkh _jobj = (jobject)to_jobject(obj); 94825271Sjkh _size = obj->size() * wordSize; 94925271Sjkh }; 95025271Sjkh jobject jni_jobject() { return _jobj; } 95125271Sjkh jlong size() { return _size; } 95290110Simp}; 95325271Sjkh 95425271Sjkhclass JvmtiCompiledMethodLoadEventMark : public JvmtiMethodEventMark { 95525271Sjkh private: 9561556Srgrimes jint _code_size; 9571556Srgrimes const void *_code_data; 958130999Sgad jint _map_length; 9591556Srgrimes jvmtiAddrLocationMap *_map; 96025271Sjkh const void *_compile_info; 96125271Sjkh public: 96225271Sjkh JvmtiCompiledMethodLoadEventMark(JavaThread *thread, nmethod *nm, void* compile_info_ptr = NULL) 96325271Sjkh : JvmtiMethodEventMark(thread,methodHandle(thread, nm->method())) { 96425271Sjkh _code_data = nm->insts_begin(); 96525271Sjkh _code_size = nm->insts_size(); 96625271Sjkh _compile_info = compile_info_ptr; // Set void pointer of compiledMethodLoad Event. Default value is NULL. 96725271Sjkh JvmtiCodeBlobEvents::build_jvmti_addr_location_map(nm, &_map, &_map_length); 96825271Sjkh } 96925271Sjkh ~JvmtiCompiledMethodLoadEventMark() { 97025271Sjkh FREE_C_HEAP_ARRAY(jvmtiAddrLocationMap, _map); 97190110Simp } 97225271Sjkh 97325271Sjkh jint code_size() { return _code_size; } 97425271Sjkh const void *code_data() { return _code_data; } 97525271Sjkh jint map_length() { return _map_length; } 97625271Sjkh const jvmtiAddrLocationMap* map() { return _map; } 977130999Sgad const void *compile_info() { return _compile_info; } 97825271Sjkh}; 979109504Sjmallett 9801556Srgrimes 9811556Srgrimes 9821556Srgrimesclass JvmtiMonitorEventMark : public JvmtiThreadEventMark { 9831556Srgrimesprivate: 9841556Srgrimes jobject _jobj; 9851556Srgrimespublic: 9861556Srgrimes JvmtiMonitorEventMark(JavaThread *thread, oop object) 98790143Smarkm : JvmtiThreadEventMark(thread){ 98890110Simp _jobj = to_jobject(object); 989127542Sgad } 9901556Srgrimes jobject jni_object() { return _jobj; } 99190143Smarkm}; 9921556Srgrimes 99390143Smarkm/////////////////////////////////////////////////////////////// 9941556Srgrimes// 9951556Srgrimes// pending CompiledMethodUnload support 9961556Srgrimes// 99771578Sjhb 99831552Sdysonvoid JvmtiExport::post_compiled_method_unload( 9991556Srgrimes jmethodID method, const void *code_begin) { 100090110Simp if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) { 10011556Srgrimes return; 10021556Srgrimes } 100371578Sjhb JavaThread* thread = JavaThread::current(); 10041556Srgrimes EVT_TRIG_TRACE(JVMTI_EVENT_COMPILED_METHOD_UNLOAD, 10051556Srgrimes ("[%s] method compile unload event triggered", 10061556Srgrimes JvmtiTrace::safe_get_thread_name(thread))); 10071556Srgrimes 10081556Srgrimes // post the event for each environment that has this event enabled. 10091556Srgrimes JvmtiEnvIterator it; 101069896Smckusick for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 10111556Srgrimes if (env->is_enabled(JVMTI_EVENT_COMPILED_METHOD_UNLOAD)) { 101269896Smckusick if (env->phase() == JVMTI_PHASE_PRIMORDIAL) { 10131556Srgrimes continue; 10141556Srgrimes } 10151556Srgrimes EVT_TRACE(JVMTI_EVENT_COMPILED_METHOD_UNLOAD, 1016130991Sgad ("[%s] class compile method unload event sent jmethodID " PTR_FORMAT, 1017130991Sgad JvmtiTrace::safe_get_thread_name(thread), p2i(method))); 1018130991Sgad 1019130991Sgad ResourceMark rm(thread); 1020130991Sgad 1021130991Sgad JvmtiEventMark jem(thread); 1022130991Sgad JvmtiJavaThreadEventTransition jet(thread); 1023130991Sgad jvmtiEventCompiledMethodUnload callback = env->callbacks()->CompiledMethodUnload; 1024131024Sgad if (callback != NULL) { 1025130991Sgad (*callback)(env->jvmti_external(), method, code_begin); 102653276Speter } 102753276Speter } 102853276Speter } 1029130991Sgad} 1030130991Sgad 1031130991Sgad/////////////////////////////////////////////////////////////// 1032130991Sgad// 1033130991Sgad// JvmtiExport 1034130991Sgad// 1035130991Sgad 1036130991Sgadvoid JvmtiExport::post_raw_breakpoint(JavaThread *thread, Method* method, address location) { 103753276Speter HandleMark hm(thread); 103853276Speter methodHandle mh(thread, method); 103953276Speter 10401556Srgrimes JvmtiThreadState *state = thread->jvmti_thread_state(); 10411556Srgrimes if (state == NULL) { 1042130816Sgad return; 1043130816Sgad } 1044130816Sgad EVT_TRIG_TRACE(JVMTI_EVENT_BREAKPOINT, ("[%s] Trg Breakpoint triggered", 1045130816Sgad JvmtiTrace::safe_get_thread_name(thread))); 1046130816Sgad JvmtiEnvThreadStateIterator it(state); 1047130816Sgad for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 10481556Srgrimes ets->compare_and_set_current_location(mh(), location, JVMTI_EVENT_BREAKPOINT); 104990110Simp if (!ets->breakpoint_posted() && ets->is_enabled(JVMTI_EVENT_BREAKPOINT)) { 10501556Srgrimes ThreadState old_os_state = thread->osthread()->get_state(); 1051127596Sgad thread->osthread()->set_state(BREAKPOINTED); 10521556Srgrimes EVT_TRACE(JVMTI_EVENT_BREAKPOINT, ("[%s] Evt Breakpoint sent %s.%s @ " INTX_FORMAT, 1053127596Sgad JvmtiTrace::safe_get_thread_name(thread), 1054127596Sgad (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), 1055127596Sgad (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(), 1056130816Sgad location - mh()->code_base() )); 1057130816Sgad 1058130816Sgad JvmtiEnv *env = ets->get_env(); 1059130816Sgad JvmtiLocationEventMark jem(thread, mh, location); 1060127596Sgad JvmtiJavaThreadEventTransition jet(thread); 1061127596Sgad jvmtiEventBreakpoint callback = env->callbacks()->Breakpoint; 1062127596Sgad if (callback != NULL) { 1063127596Sgad (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), 1064130816Sgad jem.jni_methodID(), jem.location()); 1065130816Sgad } 1066130816Sgad 1067130816Sgad ets->set_breakpoint_posted(); 1068130816Sgad thread->osthread()->set_state(old_os_state); 1069130816Sgad } 1070130816Sgad } 1071130816Sgad} 1072130972Sgad 1073130816Sgad////////////////////////////////////////////////////////////////////////////// 1074130972Sgad 1075127596Sgadbool JvmtiExport::_can_get_source_debug_extension = false; 10761556Srgrimesbool JvmtiExport::_can_maintain_original_method_order = false; 1077130816Sgadbool JvmtiExport::_can_post_interpreter_events = false; 10781556Srgrimesbool JvmtiExport::_can_post_on_exceptions = false; 10791556Srgrimesbool JvmtiExport::_can_post_breakpoint = false; 10801556Srgrimesbool JvmtiExport::_can_post_field_access = false; 10811556Srgrimesbool JvmtiExport::_can_post_field_modification = false; 10821556Srgrimesbool JvmtiExport::_can_post_method_entry = false; 10831556Srgrimesbool JvmtiExport::_can_post_method_exit = false; 10841556Srgrimesbool JvmtiExport::_can_pop_frame = false; 10851556Srgrimesbool JvmtiExport::_can_force_early_return = false; 10861556Srgrimes 10871556Srgrimesbool JvmtiExport::_early_vmstart_recorded = false; 10881556Srgrimes 10891556Srgrimesbool JvmtiExport::_should_post_single_step = false; 10901556Srgrimesbool JvmtiExport::_should_post_field_access = false; 1091129915Sgadbool JvmtiExport::_should_post_field_modification = false; 10921556Srgrimesbool JvmtiExport::_should_post_class_load = false; 10931556Srgrimesbool JvmtiExport::_should_post_class_prepare = false; 1094129914Sgadbool JvmtiExport::_should_post_class_unload = false; 10951556Srgrimesbool JvmtiExport::_should_post_thread_life = false; 1096102886Sjmallettbool JvmtiExport::_should_clean_up_heap_objects = false; 1097129914Sgadbool JvmtiExport::_should_post_native_method_bind = false; 1098129914Sgadbool JvmtiExport::_should_post_dynamic_code_generated = false; 1099102886Sjmallettbool JvmtiExport::_should_post_data_dump = false; 1100129914Sgadbool JvmtiExport::_should_post_compiled_method_load = false; 1101129914Sgadbool JvmtiExport::_should_post_compiled_method_unload = false; 1102129914Sgadbool JvmtiExport::_should_post_monitor_contended_enter = false; 1103129914Sgadbool JvmtiExport::_should_post_monitor_contended_entered = false; 1104129914Sgadbool JvmtiExport::_should_post_monitor_wait = false; 1105129914Sgadbool JvmtiExport::_should_post_monitor_waited = false; 1106129914Sgadbool JvmtiExport::_should_post_garbage_collection_start = false; 1107129914Sgadbool JvmtiExport::_should_post_garbage_collection_finish = false; 1108129914Sgadbool JvmtiExport::_should_post_object_free = false; 1109129914Sgadbool JvmtiExport::_should_post_resource_exhausted = false; 1110129914Sgadbool JvmtiExport::_should_post_vm_object_alloc = false; 1111129914Sgadbool JvmtiExport::_should_post_on_exceptions = false; 1112102886Sjmallett 11131556Srgrimes//////////////////////////////////////////////////////////////////////////////////////////////// 11141556Srgrimes 11151556Srgrimes 11161556Srgrimes// 111781743Sbrian// JVMTI single step management 1118129634Sgad// 1119129634Sgadvoid JvmtiExport::at_single_stepping_point(JavaThread *thread, Method* method, address location) { 1120129634Sgad assert(JvmtiExport::should_post_single_step(), "must be single stepping"); 1121129914Sgad 1122129914Sgad HandleMark hm(thread); 1123129914Sgad methodHandle mh(thread, method); 11241556Srgrimes 1125129914Sgad // update information about current location and post a step event 1126129914Sgad JvmtiThreadState *state = thread->jvmti_thread_state(); 1127129914Sgad if (state == NULL) { 1128129915Sgad return; 1129129915Sgad } 1130129915Sgad EVT_TRIG_TRACE(JVMTI_EVENT_SINGLE_STEP, ("[%s] Trg Single Step triggered", 1131129915Sgad JvmtiTrace::safe_get_thread_name(thread))); 11321556Srgrimes if (!state->hide_single_stepping()) { 1133129914Sgad if (state->is_pending_step_for_popframe()) { 1134129914Sgad state->process_pending_step_for_popframe(); 1135129914Sgad } 1136129914Sgad if (state->is_pending_step_for_earlyret()) { 11371556Srgrimes state->process_pending_step_for_earlyret(); 1138129914Sgad } 1139129914Sgad JvmtiExport::post_single_step(thread, mh(), location); 1140129914Sgad } 1141129914Sgad} 1142129914Sgad 11431556Srgrimes 1144129914Sgadvoid JvmtiExport::expose_single_stepping(JavaThread *thread) { 11451556Srgrimes JvmtiThreadState *state = thread->jvmti_thread_state(); 1146129914Sgad if (state != NULL) { 1147129914Sgad state->clear_hide_single_stepping(); 11481556Srgrimes } 1149129914Sgad} 1150129914Sgad 1151129914Sgad 1152129914Sgadbool JvmtiExport::hide_single_stepping(JavaThread *thread) { 1153129914Sgad JvmtiThreadState *state = thread->jvmti_thread_state(); 1154129914Sgad if (state != NULL && state->is_enabled(JVMTI_EVENT_SINGLE_STEP)) { 1155129914Sgad state->set_hide_single_stepping(); 1156129914Sgad return true; 1157129914Sgad } else { 1158129914Sgad return false; 1159129914Sgad } 1160129914Sgad} 1161129914Sgad 1162129914Sgadvoid JvmtiExport::post_class_load(JavaThread *thread, Klass* klass) { 1163129914Sgad if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) { 1164129914Sgad return; 1165129914Sgad } 1166129914Sgad HandleMark hm(thread); 1167129914Sgad KlassHandle kh(thread, klass); 1168129914Sgad 1169129914Sgad EVT_TRIG_TRACE(JVMTI_EVENT_CLASS_LOAD, ("[%s] Trg Class Load triggered", 1170129914Sgad JvmtiTrace::safe_get_thread_name(thread))); 1171129914Sgad JvmtiThreadState* state = thread->jvmti_thread_state(); 11721556Srgrimes if (state == NULL) { 1173129914Sgad return; 1174129914Sgad } 11751556Srgrimes JvmtiEnvThreadStateIterator it(state); 11761556Srgrimes for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 11771556Srgrimes if (ets->is_enabled(JVMTI_EVENT_CLASS_LOAD)) { 11781556Srgrimes JvmtiEnv *env = ets->get_env(); 1179137696Scsjp if (env->phase() == JVMTI_PHASE_PRIMORDIAL) { 1180137696Scsjp continue; 1181137696Scsjp } 1182137890Scsjp EVT_TRACE(JVMTI_EVENT_CLASS_LOAD, ("[%s] Evt Class Load sent %s", 1183137696Scsjp JvmtiTrace::safe_get_thread_name(thread), 1184137890Scsjp kh()==NULL? "NULL" : kh()->external_name() )); 1185137890Scsjp JvmtiClassEventMark jem(thread, kh()); 1186137890Scsjp JvmtiJavaThreadEventTransition jet(thread); 1187137890Scsjp jvmtiEventClassLoad callback = env->callbacks()->ClassLoad; 1188137890Scsjp if (callback != NULL) { 1189137696Scsjp (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_class()); 1190137696Scsjp } 11911556Srgrimes } 119290110Simp } 11931556Srgrimes} 1194130973Sgad 11951556Srgrimes 1196127499Sgadvoid JvmtiExport::post_class_prepare(JavaThread *thread, Klass* klass) { 1197127507Sgad if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) { 1198127499Sgad return; 1199127499Sgad } 120026465Scharnier HandleMark hm(thread); 12011556Srgrimes KlassHandle kh(thread, klass); 12021556Srgrimes 1203 EVT_TRIG_TRACE(JVMTI_EVENT_CLASS_PREPARE, ("[%s] Trg Class Prepare triggered", 1204 JvmtiTrace::safe_get_thread_name(thread))); 1205 JvmtiThreadState* state = thread->jvmti_thread_state(); 1206 if (state == NULL) { 1207 return; 1208 } 1209 JvmtiEnvThreadStateIterator it(state); 1210 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 1211 if (ets->is_enabled(JVMTI_EVENT_CLASS_PREPARE)) { 1212 JvmtiEnv *env = ets->get_env(); 1213 if (env->phase() == JVMTI_PHASE_PRIMORDIAL) { 1214 continue; 1215 } 1216 EVT_TRACE(JVMTI_EVENT_CLASS_PREPARE, ("[%s] Evt Class Prepare sent %s", 1217 JvmtiTrace::safe_get_thread_name(thread), 1218 kh()==NULL? "NULL" : kh()->external_name() )); 1219 JvmtiClassEventMark jem(thread, kh()); 1220 JvmtiJavaThreadEventTransition jet(thread); 1221 jvmtiEventClassPrepare callback = env->callbacks()->ClassPrepare; 1222 if (callback != NULL) { 1223 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_class()); 1224 } 1225 } 1226 } 1227} 1228 1229void JvmtiExport::post_class_unload(Klass* klass) { 1230 if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) { 1231 return; 1232 } 1233 Thread *thread = Thread::current(); 1234 HandleMark hm(thread); 1235 KlassHandle kh(thread, klass); 1236 1237 EVT_TRIG_TRACE(EXT_EVENT_CLASS_UNLOAD, ("[?] Trg Class Unload triggered" )); 1238 if (JvmtiEventController::is_enabled((jvmtiEvent)EXT_EVENT_CLASS_UNLOAD)) { 1239 assert(thread->is_VM_thread(), "wrong thread"); 1240 1241 // get JavaThread for whom we are proxy 1242 JavaThread *real_thread = 1243 (JavaThread *)((VMThread *)thread)->vm_operation()->calling_thread(); 1244 1245 JvmtiEnvIterator it; 1246 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 1247 if (env->phase() == JVMTI_PHASE_PRIMORDIAL) { 1248 continue; 1249 } 1250 if (env->is_enabled((jvmtiEvent)EXT_EVENT_CLASS_UNLOAD)) { 1251 EVT_TRACE(EXT_EVENT_CLASS_UNLOAD, ("[?] Evt Class Unload sent %s", 1252 kh()==NULL? "NULL" : kh()->external_name() )); 1253 1254 // do everything manually, since this is a proxy - needs special care 1255 JNIEnv* jni_env = real_thread->jni_environment(); 1256 jthread jt = (jthread)JNIHandles::make_local(real_thread, real_thread->threadObj()); 1257 jclass jk = (jclass)JNIHandles::make_local(real_thread, kh()->java_mirror()); 1258 1259 // Before we call the JVMTI agent, we have to set the state in the 1260 // thread for which we are proxying. 1261 JavaThreadState prev_state = real_thread->thread_state(); 1262 assert(((Thread *)real_thread)->is_ConcurrentGC_thread() || 1263 (real_thread->is_Java_thread() && prev_state == _thread_blocked), 1264 "should be ConcurrentGCThread or JavaThread at safepoint"); 1265 real_thread->set_thread_state(_thread_in_native); 1266 1267 jvmtiExtensionEvent callback = env->ext_callbacks()->ClassUnload; 1268 if (callback != NULL) { 1269 (*callback)(env->jvmti_external(), jni_env, jt, jk); 1270 } 1271 1272 assert(real_thread->thread_state() == _thread_in_native, 1273 "JavaThread should be in native"); 1274 real_thread->set_thread_state(prev_state); 1275 1276 JNIHandles::destroy_local(jk); 1277 JNIHandles::destroy_local(jt); 1278 } 1279 } 1280 } 1281} 1282 1283 1284void JvmtiExport::post_thread_start(JavaThread *thread) { 1285 if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) { 1286 return; 1287 } 1288 assert(thread->thread_state() == _thread_in_vm, "must be in vm state"); 1289 1290 EVT_TRIG_TRACE(JVMTI_EVENT_THREAD_START, ("[%s] Trg Thread Start event triggered", 1291 JvmtiTrace::safe_get_thread_name(thread))); 1292 1293 // do JVMTI thread initialization (if needed) 1294 JvmtiEventController::thread_started(thread); 1295 1296 // Do not post thread start event for hidden java thread. 1297 if (JvmtiEventController::is_enabled(JVMTI_EVENT_THREAD_START) && 1298 !thread->is_hidden_from_external_view()) { 1299 JvmtiEnvIterator it; 1300 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 1301 if (env->phase() == JVMTI_PHASE_PRIMORDIAL) { 1302 continue; 1303 } 1304 if (env->is_enabled(JVMTI_EVENT_THREAD_START)) { 1305 EVT_TRACE(JVMTI_EVENT_THREAD_START, ("[%s] Evt Thread Start event sent", 1306 JvmtiTrace::safe_get_thread_name(thread) )); 1307 1308 JvmtiThreadEventMark jem(thread); 1309 JvmtiJavaThreadEventTransition jet(thread); 1310 jvmtiEventThreadStart callback = env->callbacks()->ThreadStart; 1311 if (callback != NULL) { 1312 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread()); 1313 } 1314 } 1315 } 1316 } 1317} 1318 1319 1320void JvmtiExport::post_thread_end(JavaThread *thread) { 1321 if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) { 1322 return; 1323 } 1324 EVT_TRIG_TRACE(JVMTI_EVENT_THREAD_END, ("[%s] Trg Thread End event triggered", 1325 JvmtiTrace::safe_get_thread_name(thread))); 1326 1327 JvmtiThreadState *state = thread->jvmti_thread_state(); 1328 if (state == NULL) { 1329 return; 1330 } 1331 1332 // Do not post thread end event for hidden java thread. 1333 if (state->is_enabled(JVMTI_EVENT_THREAD_END) && 1334 !thread->is_hidden_from_external_view()) { 1335 1336 JvmtiEnvThreadStateIterator it(state); 1337 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 1338 if (ets->is_enabled(JVMTI_EVENT_THREAD_END)) { 1339 JvmtiEnv *env = ets->get_env(); 1340 if (env->phase() == JVMTI_PHASE_PRIMORDIAL) { 1341 continue; 1342 } 1343 EVT_TRACE(JVMTI_EVENT_THREAD_END, ("[%s] Evt Thread End event sent", 1344 JvmtiTrace::safe_get_thread_name(thread) )); 1345 1346 JvmtiThreadEventMark jem(thread); 1347 JvmtiJavaThreadEventTransition jet(thread); 1348 jvmtiEventThreadEnd callback = env->callbacks()->ThreadEnd; 1349 if (callback != NULL) { 1350 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread()); 1351 } 1352 } 1353 } 1354 } 1355} 1356 1357void JvmtiExport::post_object_free(JvmtiEnv* env, jlong tag) { 1358 assert(SafepointSynchronize::is_at_safepoint(), "must be executed at safepoint"); 1359 assert(env->is_enabled(JVMTI_EVENT_OBJECT_FREE), "checking"); 1360 1361 EVT_TRIG_TRACE(JVMTI_EVENT_OBJECT_FREE, ("[?] Trg Object Free triggered" )); 1362 EVT_TRACE(JVMTI_EVENT_OBJECT_FREE, ("[?] Evt Object Free sent")); 1363 1364 jvmtiEventObjectFree callback = env->callbacks()->ObjectFree; 1365 if (callback != NULL) { 1366 (*callback)(env->jvmti_external(), tag); 1367 } 1368} 1369 1370void JvmtiExport::post_resource_exhausted(jint resource_exhausted_flags, const char* description) { 1371 EVT_TRIG_TRACE(JVMTI_EVENT_RESOURCE_EXHAUSTED, ("Trg resource exhausted event triggered" )); 1372 1373 JvmtiEnvIterator it; 1374 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 1375 if (env->is_enabled(JVMTI_EVENT_RESOURCE_EXHAUSTED)) { 1376 EVT_TRACE(JVMTI_EVENT_RESOURCE_EXHAUSTED, ("Evt resource exhausted event sent" )); 1377 1378 JavaThread *thread = JavaThread::current(); 1379 JvmtiThreadEventMark jem(thread); 1380 JvmtiJavaThreadEventTransition jet(thread); 1381 jvmtiEventResourceExhausted callback = env->callbacks()->ResourceExhausted; 1382 if (callback != NULL) { 1383 (*callback)(env->jvmti_external(), jem.jni_env(), 1384 resource_exhausted_flags, NULL, description); 1385 } 1386 } 1387 } 1388} 1389 1390void JvmtiExport::post_method_entry(JavaThread *thread, Method* method, frame current_frame) { 1391 HandleMark hm(thread); 1392 methodHandle mh(thread, method); 1393 1394 EVT_TRIG_TRACE(JVMTI_EVENT_METHOD_ENTRY, ("[%s] Trg Method Entry triggered %s.%s", 1395 JvmtiTrace::safe_get_thread_name(thread), 1396 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), 1397 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() )); 1398 1399 JvmtiThreadState* state = thread->jvmti_thread_state(); 1400 if (state == NULL || !state->is_interp_only_mode()) { 1401 // for any thread that actually wants method entry, interp_only_mode is set 1402 return; 1403 } 1404 1405 state->incr_cur_stack_depth(); 1406 1407 if (state->is_enabled(JVMTI_EVENT_METHOD_ENTRY)) { 1408 JvmtiEnvThreadStateIterator it(state); 1409 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 1410 if (ets->is_enabled(JVMTI_EVENT_METHOD_ENTRY)) { 1411 EVT_TRACE(JVMTI_EVENT_METHOD_ENTRY, ("[%s] Evt Method Entry sent %s.%s", 1412 JvmtiTrace::safe_get_thread_name(thread), 1413 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), 1414 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() )); 1415 1416 JvmtiEnv *env = ets->get_env(); 1417 JvmtiMethodEventMark jem(thread, mh); 1418 JvmtiJavaThreadEventTransition jet(thread); 1419 jvmtiEventMethodEntry callback = env->callbacks()->MethodEntry; 1420 if (callback != NULL) { 1421 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_methodID()); 1422 } 1423 } 1424 } 1425 } 1426} 1427 1428void JvmtiExport::post_method_exit(JavaThread *thread, Method* method, frame current_frame) { 1429 HandleMark hm(thread); 1430 methodHandle mh(thread, method); 1431 1432 EVT_TRIG_TRACE(JVMTI_EVENT_METHOD_EXIT, ("[%s] Trg Method Exit triggered %s.%s", 1433 JvmtiTrace::safe_get_thread_name(thread), 1434 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), 1435 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() )); 1436 1437 JvmtiThreadState *state = thread->jvmti_thread_state(); 1438 if (state == NULL || !state->is_interp_only_mode()) { 1439 // for any thread that actually wants method exit, interp_only_mode is set 1440 return; 1441 } 1442 1443 // return a flag when a method terminates by throwing an exception 1444 // i.e. if an exception is thrown and it's not caught by the current method 1445 bool exception_exit = state->is_exception_detected() && !state->is_exception_caught(); 1446 1447 1448 if (state->is_enabled(JVMTI_EVENT_METHOD_EXIT)) { 1449 Handle result; 1450 jvalue value; 1451 value.j = 0L; 1452 1453 // if the method hasn't been popped because of an exception then we populate 1454 // the return_value parameter for the callback. At this point we only have 1455 // the address of a "raw result" and we just call into the interpreter to 1456 // convert this into a jvalue. 1457 if (!exception_exit) { 1458 oop oop_result; 1459 BasicType type = current_frame.interpreter_frame_result(&oop_result, &value); 1460 if (type == T_OBJECT || type == T_ARRAY) { 1461 result = Handle(thread, oop_result); 1462 } 1463 } 1464 1465 JvmtiEnvThreadStateIterator it(state); 1466 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 1467 if (ets->is_enabled(JVMTI_EVENT_METHOD_EXIT)) { 1468 EVT_TRACE(JVMTI_EVENT_METHOD_EXIT, ("[%s] Evt Method Exit sent %s.%s", 1469 JvmtiTrace::safe_get_thread_name(thread), 1470 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), 1471 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() )); 1472 1473 JvmtiEnv *env = ets->get_env(); 1474 JvmtiMethodEventMark jem(thread, mh); 1475 if (result.not_null()) { 1476 value.l = JNIHandles::make_local(thread, result()); 1477 } 1478 JvmtiJavaThreadEventTransition jet(thread); 1479 jvmtiEventMethodExit callback = env->callbacks()->MethodExit; 1480 if (callback != NULL) { 1481 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), 1482 jem.jni_methodID(), exception_exit, value); 1483 } 1484 } 1485 } 1486 } 1487 1488 if (state->is_enabled(JVMTI_EVENT_FRAME_POP)) { 1489 JvmtiEnvThreadStateIterator it(state); 1490 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 1491 int cur_frame_number = state->cur_stack_depth(); 1492 1493 if (ets->is_frame_pop(cur_frame_number)) { 1494 // we have a NotifyFramePop entry for this frame. 1495 // now check that this env/thread wants this event 1496 if (ets->is_enabled(JVMTI_EVENT_FRAME_POP)) { 1497 EVT_TRACE(JVMTI_EVENT_FRAME_POP, ("[%s] Evt Frame Pop sent %s.%s", 1498 JvmtiTrace::safe_get_thread_name(thread), 1499 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), 1500 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() )); 1501 1502 // we also need to issue a frame pop event for this frame 1503 JvmtiEnv *env = ets->get_env(); 1504 JvmtiMethodEventMark jem(thread, mh); 1505 JvmtiJavaThreadEventTransition jet(thread); 1506 jvmtiEventFramePop callback = env->callbacks()->FramePop; 1507 if (callback != NULL) { 1508 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), 1509 jem.jni_methodID(), exception_exit); 1510 } 1511 } 1512 // remove the frame's entry 1513 ets->clear_frame_pop(cur_frame_number); 1514 } 1515 } 1516 } 1517 1518 state->decr_cur_stack_depth(); 1519} 1520 1521 1522// Todo: inline this for optimization 1523void JvmtiExport::post_single_step(JavaThread *thread, Method* method, address location) { 1524 HandleMark hm(thread); 1525 methodHandle mh(thread, method); 1526 1527 JvmtiThreadState *state = thread->jvmti_thread_state(); 1528 if (state == NULL) { 1529 return; 1530 } 1531 JvmtiEnvThreadStateIterator it(state); 1532 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 1533 ets->compare_and_set_current_location(mh(), location, JVMTI_EVENT_SINGLE_STEP); 1534 if (!ets->single_stepping_posted() && ets->is_enabled(JVMTI_EVENT_SINGLE_STEP)) { 1535 EVT_TRACE(JVMTI_EVENT_SINGLE_STEP, ("[%s] Evt Single Step sent %s.%s @ " INTX_FORMAT, 1536 JvmtiTrace::safe_get_thread_name(thread), 1537 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), 1538 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(), 1539 location - mh()->code_base() )); 1540 1541 JvmtiEnv *env = ets->get_env(); 1542 JvmtiLocationEventMark jem(thread, mh, location); 1543 JvmtiJavaThreadEventTransition jet(thread); 1544 jvmtiEventSingleStep callback = env->callbacks()->SingleStep; 1545 if (callback != NULL) { 1546 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), 1547 jem.jni_methodID(), jem.location()); 1548 } 1549 1550 ets->set_single_stepping_posted(); 1551 } 1552 } 1553} 1554 1555 1556void JvmtiExport::post_exception_throw(JavaThread *thread, Method* method, address location, oop exception) { 1557 HandleMark hm(thread); 1558 methodHandle mh(thread, method); 1559 Handle exception_handle(thread, exception); 1560 1561 JvmtiThreadState *state = thread->jvmti_thread_state(); 1562 if (state == NULL) { 1563 return; 1564 } 1565 1566 EVT_TRIG_TRACE(JVMTI_EVENT_EXCEPTION, ("[%s] Trg Exception thrown triggered", 1567 JvmtiTrace::safe_get_thread_name(thread))); 1568 if (!state->is_exception_detected()) { 1569 state->set_exception_detected(); 1570 JvmtiEnvThreadStateIterator it(state); 1571 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 1572 if (ets->is_enabled(JVMTI_EVENT_EXCEPTION) && (exception != NULL)) { 1573 1574 EVT_TRACE(JVMTI_EVENT_EXCEPTION, 1575 ("[%s] Evt Exception thrown sent %s.%s @ " INTX_FORMAT, 1576 JvmtiTrace::safe_get_thread_name(thread), 1577 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), 1578 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(), 1579 location - mh()->code_base() )); 1580 1581 JvmtiEnv *env = ets->get_env(); 1582 JvmtiExceptionEventMark jem(thread, mh, location, exception_handle); 1583 1584 // It's okay to clear these exceptions here because we duplicate 1585 // this lookup in InterpreterRuntime::exception_handler_for_exception. 1586 EXCEPTION_MARK; 1587 1588 bool should_repeat; 1589 vframeStream st(thread); 1590 assert(!st.at_end(), "cannot be at end"); 1591 Method* current_method = NULL; 1592 // A GC may occur during the Method::fast_exception_handler_bci_for() 1593 // call below if it needs to load the constraint class. Using a 1594 // methodHandle to keep the 'current_method' from being deallocated 1595 // if GC happens. 1596 methodHandle current_mh = methodHandle(thread, current_method); 1597 int current_bci = -1; 1598 do { 1599 current_method = st.method(); 1600 current_mh = methodHandle(thread, current_method); 1601 current_bci = st.bci(); 1602 do { 1603 should_repeat = false; 1604 KlassHandle eh_klass(thread, exception_handle()->klass()); 1605 current_bci = Method::fast_exception_handler_bci_for( 1606 current_mh, eh_klass, current_bci, THREAD); 1607 if (HAS_PENDING_EXCEPTION) { 1608 exception_handle = Handle(thread, PENDING_EXCEPTION); 1609 CLEAR_PENDING_EXCEPTION; 1610 should_repeat = true; 1611 } 1612 } while (should_repeat && (current_bci != -1)); 1613 st.next(); 1614 } while ((current_bci < 0) && (!st.at_end())); 1615 1616 jmethodID catch_jmethodID; 1617 if (current_bci < 0) { 1618 catch_jmethodID = 0; 1619 current_bci = 0; 1620 } else { 1621 catch_jmethodID = jem.to_jmethodID(current_mh); 1622 } 1623 1624 JvmtiJavaThreadEventTransition jet(thread); 1625 jvmtiEventException callback = env->callbacks()->Exception; 1626 if (callback != NULL) { 1627 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), 1628 jem.jni_methodID(), jem.location(), 1629 jem.exception(), 1630 catch_jmethodID, current_bci); 1631 } 1632 } 1633 } 1634 } 1635 1636 // frames may get popped because of this throw, be safe - invalidate cached depth 1637 state->invalidate_cur_stack_depth(); 1638} 1639 1640 1641void JvmtiExport::notice_unwind_due_to_exception(JavaThread *thread, Method* method, address location, oop exception, bool in_handler_frame) { 1642 HandleMark hm(thread); 1643 methodHandle mh(thread, method); 1644 Handle exception_handle(thread, exception); 1645 1646 JvmtiThreadState *state = thread->jvmti_thread_state(); 1647 if (state == NULL) { 1648 return; 1649 } 1650 EVT_TRIG_TRACE(JVMTI_EVENT_EXCEPTION_CATCH, 1651 ("[%s] Trg unwind_due_to_exception triggered %s.%s @ %s" INTX_FORMAT " - %s", 1652 JvmtiTrace::safe_get_thread_name(thread), 1653 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), 1654 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(), 1655 location==0? "no location:" : "", 1656 location==0? 0 : location - mh()->code_base(), 1657 in_handler_frame? "in handler frame" : "not handler frame" )); 1658 1659 if (state->is_exception_detected()) { 1660 1661 state->invalidate_cur_stack_depth(); 1662 if (!in_handler_frame) { 1663 // Not in exception handler. 1664 if(state->is_interp_only_mode()) { 1665 // method exit and frame pop events are posted only in interp mode. 1666 // When these events are enabled code should be in running in interp mode. 1667 JvmtiExport::post_method_exit(thread, method, thread->last_frame()); 1668 // The cached cur_stack_depth might have changed from the 1669 // operations of frame pop or method exit. We are not 100% sure 1670 // the cached cur_stack_depth is still valid depth so invalidate 1671 // it. 1672 state->invalidate_cur_stack_depth(); 1673 } 1674 } else { 1675 // In exception handler frame. Report exception catch. 1676 assert(location != NULL, "must be a known location"); 1677 // Update cur_stack_depth - the frames above the current frame 1678 // have been unwound due to this exception: 1679 assert(!state->is_exception_caught(), "exception must not be caught yet."); 1680 state->set_exception_caught(); 1681 1682 JvmtiEnvThreadStateIterator it(state); 1683 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 1684 if (ets->is_enabled(JVMTI_EVENT_EXCEPTION_CATCH) && (exception_handle() != NULL)) { 1685 EVT_TRACE(JVMTI_EVENT_EXCEPTION_CATCH, 1686 ("[%s] Evt ExceptionCatch sent %s.%s @ " INTX_FORMAT, 1687 JvmtiTrace::safe_get_thread_name(thread), 1688 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), 1689 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(), 1690 location - mh()->code_base() )); 1691 1692 JvmtiEnv *env = ets->get_env(); 1693 JvmtiExceptionEventMark jem(thread, mh, location, exception_handle); 1694 JvmtiJavaThreadEventTransition jet(thread); 1695 jvmtiEventExceptionCatch callback = env->callbacks()->ExceptionCatch; 1696 if (callback != NULL) { 1697 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), 1698 jem.jni_methodID(), jem.location(), 1699 jem.exception()); 1700 } 1701 } 1702 } 1703 } 1704 } 1705} 1706 1707oop JvmtiExport::jni_GetField_probe(JavaThread *thread, jobject jobj, oop obj, 1708 Klass* klass, jfieldID fieldID, bool is_static) { 1709 if (*((int *)get_field_access_count_addr()) > 0 && thread->has_last_Java_frame()) { 1710 // At least one field access watch is set so we have more work 1711 // to do. This wrapper is used by entry points that allow us 1712 // to create handles in post_field_access_by_jni(). 1713 post_field_access_by_jni(thread, obj, klass, fieldID, is_static); 1714 // event posting can block so refetch oop if we were passed a jobj 1715 if (jobj != NULL) return JNIHandles::resolve_non_null(jobj); 1716 } 1717 return obj; 1718} 1719 1720oop JvmtiExport::jni_GetField_probe_nh(JavaThread *thread, jobject jobj, oop obj, 1721 Klass* klass, jfieldID fieldID, bool is_static) { 1722 if (*((int *)get_field_access_count_addr()) > 0 && thread->has_last_Java_frame()) { 1723 // At least one field access watch is set so we have more work 1724 // to do. This wrapper is used by "quick" entry points that don't 1725 // allow us to create handles in post_field_access_by_jni(). We 1726 // override that with a ResetNoHandleMark. 1727 ResetNoHandleMark rnhm; 1728 post_field_access_by_jni(thread, obj, klass, fieldID, is_static); 1729 // event posting can block so refetch oop if we were passed a jobj 1730 if (jobj != NULL) return JNIHandles::resolve_non_null(jobj); 1731 } 1732 return obj; 1733} 1734 1735void JvmtiExport::post_field_access_by_jni(JavaThread *thread, oop obj, 1736 Klass* klass, jfieldID fieldID, bool is_static) { 1737 // We must be called with a Java context in order to provide reasonable 1738 // values for the klazz, method, and location fields. The callers of this 1739 // function don't make the call unless there is a Java context. 1740 assert(thread->has_last_Java_frame(), "must be called with a Java context"); 1741 1742 ResourceMark rm; 1743 fieldDescriptor fd; 1744 // if get_field_descriptor finds fieldID to be invalid, then we just bail 1745 bool valid_fieldID = JvmtiEnv::get_field_descriptor(klass, fieldID, &fd); 1746 assert(valid_fieldID == true,"post_field_access_by_jni called with invalid fieldID"); 1747 if (!valid_fieldID) return; 1748 // field accesses are not watched so bail 1749 if (!fd.is_field_access_watched()) return; 1750 1751 HandleMark hm(thread); 1752 KlassHandle h_klass(thread, klass); 1753 Handle h_obj; 1754 if (!is_static) { 1755 // non-static field accessors have an object, but we need a handle 1756 assert(obj != NULL, "non-static needs an object"); 1757 h_obj = Handle(thread, obj); 1758 } 1759 post_field_access(thread, 1760 thread->last_frame().interpreter_frame_method(), 1761 thread->last_frame().interpreter_frame_bcp(), 1762 h_klass, h_obj, fieldID); 1763} 1764 1765void JvmtiExport::post_field_access(JavaThread *thread, Method* method, 1766 address location, KlassHandle field_klass, Handle object, jfieldID field) { 1767 1768 HandleMark hm(thread); 1769 methodHandle mh(thread, method); 1770 1771 JvmtiThreadState *state = thread->jvmti_thread_state(); 1772 if (state == NULL) { 1773 return; 1774 } 1775 EVT_TRIG_TRACE(JVMTI_EVENT_FIELD_ACCESS, ("[%s] Trg Field Access event triggered", 1776 JvmtiTrace::safe_get_thread_name(thread))); 1777 JvmtiEnvThreadStateIterator it(state); 1778 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 1779 if (ets->is_enabled(JVMTI_EVENT_FIELD_ACCESS)) { 1780 EVT_TRACE(JVMTI_EVENT_FIELD_ACCESS, ("[%s] Evt Field Access event sent %s.%s @ " INTX_FORMAT, 1781 JvmtiTrace::safe_get_thread_name(thread), 1782 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), 1783 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(), 1784 location - mh()->code_base() )); 1785 1786 JvmtiEnv *env = ets->get_env(); 1787 JvmtiLocationEventMark jem(thread, mh, location); 1788 jclass field_jclass = jem.to_jclass(field_klass()); 1789 jobject field_jobject = jem.to_jobject(object()); 1790 JvmtiJavaThreadEventTransition jet(thread); 1791 jvmtiEventFieldAccess callback = env->callbacks()->FieldAccess; 1792 if (callback != NULL) { 1793 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), 1794 jem.jni_methodID(), jem.location(), 1795 field_jclass, field_jobject, field); 1796 } 1797 } 1798 } 1799} 1800 1801oop JvmtiExport::jni_SetField_probe(JavaThread *thread, jobject jobj, oop obj, 1802 Klass* klass, jfieldID fieldID, bool is_static, 1803 char sig_type, jvalue *value) { 1804 if (*((int *)get_field_modification_count_addr()) > 0 && thread->has_last_Java_frame()) { 1805 // At least one field modification watch is set so we have more work 1806 // to do. This wrapper is used by entry points that allow us 1807 // to create handles in post_field_modification_by_jni(). 1808 post_field_modification_by_jni(thread, obj, klass, fieldID, is_static, sig_type, value); 1809 // event posting can block so refetch oop if we were passed a jobj 1810 if (jobj != NULL) return JNIHandles::resolve_non_null(jobj); 1811 } 1812 return obj; 1813} 1814 1815oop JvmtiExport::jni_SetField_probe_nh(JavaThread *thread, jobject jobj, oop obj, 1816 Klass* klass, jfieldID fieldID, bool is_static, 1817 char sig_type, jvalue *value) { 1818 if (*((int *)get_field_modification_count_addr()) > 0 && thread->has_last_Java_frame()) { 1819 // At least one field modification watch is set so we have more work 1820 // to do. This wrapper is used by "quick" entry points that don't 1821 // allow us to create handles in post_field_modification_by_jni(). We 1822 // override that with a ResetNoHandleMark. 1823 ResetNoHandleMark rnhm; 1824 post_field_modification_by_jni(thread, obj, klass, fieldID, is_static, sig_type, value); 1825 // event posting can block so refetch oop if we were passed a jobj 1826 if (jobj != NULL) return JNIHandles::resolve_non_null(jobj); 1827 } 1828 return obj; 1829} 1830 1831void JvmtiExport::post_field_modification_by_jni(JavaThread *thread, oop obj, 1832 Klass* klass, jfieldID fieldID, bool is_static, 1833 char sig_type, jvalue *value) { 1834 // We must be called with a Java context in order to provide reasonable 1835 // values for the klazz, method, and location fields. The callers of this 1836 // function don't make the call unless there is a Java context. 1837 assert(thread->has_last_Java_frame(), "must be called with Java context"); 1838 1839 ResourceMark rm; 1840 fieldDescriptor fd; 1841 // if get_field_descriptor finds fieldID to be invalid, then we just bail 1842 bool valid_fieldID = JvmtiEnv::get_field_descriptor(klass, fieldID, &fd); 1843 assert(valid_fieldID == true,"post_field_modification_by_jni called with invalid fieldID"); 1844 if (!valid_fieldID) return; 1845 // field modifications are not watched so bail 1846 if (!fd.is_field_modification_watched()) return; 1847 1848 HandleMark hm(thread); 1849 1850 Handle h_obj; 1851 if (!is_static) { 1852 // non-static field accessors have an object, but we need a handle 1853 assert(obj != NULL, "non-static needs an object"); 1854 h_obj = Handle(thread, obj); 1855 } 1856 KlassHandle h_klass(thread, klass); 1857 post_field_modification(thread, 1858 thread->last_frame().interpreter_frame_method(), 1859 thread->last_frame().interpreter_frame_bcp(), 1860 h_klass, h_obj, fieldID, sig_type, value); 1861} 1862 1863void JvmtiExport::post_raw_field_modification(JavaThread *thread, Method* method, 1864 address location, KlassHandle field_klass, Handle object, jfieldID field, 1865 char sig_type, jvalue *value) { 1866 1867 if (sig_type == 'I' || sig_type == 'Z' || sig_type == 'B' || sig_type == 'C' || sig_type == 'S') { 1868 // 'I' instructions are used for byte, char, short and int. 1869 // determine which it really is, and convert 1870 fieldDescriptor fd; 1871 bool found = JvmtiEnv::get_field_descriptor(field_klass(), field, &fd); 1872 // should be found (if not, leave as is) 1873 if (found) { 1874 jint ival = value->i; 1875 // convert value from int to appropriate type 1876 switch (fd.field_type()) { 1877 case T_BOOLEAN: 1878 sig_type = 'Z'; 1879 value->i = 0; // clear it 1880 value->z = (jboolean)ival; 1881 break; 1882 case T_BYTE: 1883 sig_type = 'B'; 1884 value->i = 0; // clear it 1885 value->b = (jbyte)ival; 1886 break; 1887 case T_CHAR: 1888 sig_type = 'C'; 1889 value->i = 0; // clear it 1890 value->c = (jchar)ival; 1891 break; 1892 case T_SHORT: 1893 sig_type = 'S'; 1894 value->i = 0; // clear it 1895 value->s = (jshort)ival; 1896 break; 1897 case T_INT: 1898 // nothing to do 1899 break; 1900 default: 1901 // this is an integer instruction, should be one of above 1902 ShouldNotReachHere(); 1903 break; 1904 } 1905 } 1906 } 1907 1908 assert(sig_type != '[', "array should have sig_type == 'L'"); 1909 bool handle_created = false; 1910 1911 // convert oop to JNI handle. 1912 if (sig_type == 'L') { 1913 handle_created = true; 1914 value->l = (jobject)JNIHandles::make_local(thread, (oop)value->l); 1915 } 1916 1917 post_field_modification(thread, method, location, field_klass, object, field, sig_type, value); 1918 1919 // Destroy the JNI handle allocated above. 1920 if (handle_created) { 1921 JNIHandles::destroy_local(value->l); 1922 } 1923} 1924 1925void JvmtiExport::post_field_modification(JavaThread *thread, Method* method, 1926 address location, KlassHandle field_klass, Handle object, jfieldID field, 1927 char sig_type, jvalue *value_ptr) { 1928 1929 HandleMark hm(thread); 1930 methodHandle mh(thread, method); 1931 1932 JvmtiThreadState *state = thread->jvmti_thread_state(); 1933 if (state == NULL) { 1934 return; 1935 } 1936 EVT_TRIG_TRACE(JVMTI_EVENT_FIELD_MODIFICATION, 1937 ("[%s] Trg Field Modification event triggered", 1938 JvmtiTrace::safe_get_thread_name(thread))); 1939 1940 JvmtiEnvThreadStateIterator it(state); 1941 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 1942 if (ets->is_enabled(JVMTI_EVENT_FIELD_MODIFICATION)) { 1943 EVT_TRACE(JVMTI_EVENT_FIELD_MODIFICATION, 1944 ("[%s] Evt Field Modification event sent %s.%s @ " INTX_FORMAT, 1945 JvmtiTrace::safe_get_thread_name(thread), 1946 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), 1947 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(), 1948 location - mh()->code_base() )); 1949 1950 JvmtiEnv *env = ets->get_env(); 1951 JvmtiLocationEventMark jem(thread, mh, location); 1952 jclass field_jclass = jem.to_jclass(field_klass()); 1953 jobject field_jobject = jem.to_jobject(object()); 1954 JvmtiJavaThreadEventTransition jet(thread); 1955 jvmtiEventFieldModification callback = env->callbacks()->FieldModification; 1956 if (callback != NULL) { 1957 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), 1958 jem.jni_methodID(), jem.location(), 1959 field_jclass, field_jobject, field, sig_type, *value_ptr); 1960 } 1961 } 1962 } 1963} 1964 1965void JvmtiExport::post_native_method_bind(Method* method, address* function_ptr) { 1966 JavaThread* thread = JavaThread::current(); 1967 assert(thread->thread_state() == _thread_in_vm, "must be in vm state"); 1968 1969 HandleMark hm(thread); 1970 methodHandle mh(thread, method); 1971 1972 EVT_TRIG_TRACE(JVMTI_EVENT_NATIVE_METHOD_BIND, ("[%s] Trg Native Method Bind event triggered", 1973 JvmtiTrace::safe_get_thread_name(thread))); 1974 1975 if (JvmtiEventController::is_enabled(JVMTI_EVENT_NATIVE_METHOD_BIND)) { 1976 JvmtiEnvIterator it; 1977 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 1978 if (env->is_enabled(JVMTI_EVENT_NATIVE_METHOD_BIND)) { 1979 EVT_TRACE(JVMTI_EVENT_NATIVE_METHOD_BIND, ("[%s] Evt Native Method Bind event sent", 1980 JvmtiTrace::safe_get_thread_name(thread) )); 1981 1982 JvmtiMethodEventMark jem(thread, mh); 1983 JvmtiJavaThreadEventTransition jet(thread); 1984 JNIEnv* jni_env = (env->phase() == JVMTI_PHASE_PRIMORDIAL) ? NULL : jem.jni_env(); 1985 jvmtiEventNativeMethodBind callback = env->callbacks()->NativeMethodBind; 1986 if (callback != NULL) { 1987 (*callback)(env->jvmti_external(), jni_env, jem.jni_thread(), 1988 jem.jni_methodID(), (void*)(*function_ptr), (void**)function_ptr); 1989 } 1990 } 1991 } 1992 } 1993} 1994 1995// Returns a record containing inlining information for the given nmethod 1996jvmtiCompiledMethodLoadInlineRecord* create_inline_record(nmethod* nm) { 1997 jint numstackframes = 0; 1998 jvmtiCompiledMethodLoadInlineRecord* record = (jvmtiCompiledMethodLoadInlineRecord*)NEW_RESOURCE_OBJ(jvmtiCompiledMethodLoadInlineRecord); 1999 record->header.kind = JVMTI_CMLR_INLINE_INFO; 2000 record->header.next = NULL; 2001 record->header.majorinfoversion = JVMTI_CMLR_MAJOR_VERSION_1; 2002 record->header.minorinfoversion = JVMTI_CMLR_MINOR_VERSION_0; 2003 record->numpcs = 0; 2004 for(PcDesc* p = nm->scopes_pcs_begin(); p < nm->scopes_pcs_end(); p++) { 2005 if(p->scope_decode_offset() == DebugInformationRecorder::serialized_null) continue; 2006 record->numpcs++; 2007 } 2008 record->pcinfo = (PCStackInfo*)(NEW_RESOURCE_ARRAY(PCStackInfo, record->numpcs)); 2009 int scope = 0; 2010 for(PcDesc* p = nm->scopes_pcs_begin(); p < nm->scopes_pcs_end(); p++) { 2011 if(p->scope_decode_offset() == DebugInformationRecorder::serialized_null) continue; 2012 void* pc_address = (void*)p->real_pc(nm); 2013 assert(pc_address != NULL, "pc_address must be non-null"); 2014 record->pcinfo[scope].pc = pc_address; 2015 numstackframes=0; 2016 for(ScopeDesc* sd = nm->scope_desc_at(p->real_pc(nm));sd != NULL;sd = sd->sender()) { 2017 numstackframes++; 2018 } 2019 assert(numstackframes != 0, "numstackframes must be nonzero."); 2020 record->pcinfo[scope].methods = (jmethodID *)NEW_RESOURCE_ARRAY(jmethodID, numstackframes); 2021 record->pcinfo[scope].bcis = (jint *)NEW_RESOURCE_ARRAY(jint, numstackframes); 2022 record->pcinfo[scope].numstackframes = numstackframes; 2023 int stackframe = 0; 2024 for(ScopeDesc* sd = nm->scope_desc_at(p->real_pc(nm));sd != NULL;sd = sd->sender()) { 2025 // 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() 2026 assert(sd->method() != NULL, "sd->method() cannot be null."); 2027 record->pcinfo[scope].methods[stackframe] = sd->method()->jmethod_id(); 2028 record->pcinfo[scope].bcis[stackframe] = sd->bci(); 2029 stackframe++; 2030 } 2031 scope++; 2032 } 2033 return record; 2034} 2035 2036void JvmtiExport::post_compiled_method_load(nmethod *nm) { 2037 if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) { 2038 return; 2039 } 2040 JavaThread* thread = JavaThread::current(); 2041 2042 EVT_TRIG_TRACE(JVMTI_EVENT_COMPILED_METHOD_LOAD, 2043 ("[%s] method compile load event triggered", 2044 JvmtiTrace::safe_get_thread_name(thread))); 2045 2046 JvmtiEnvIterator it; 2047 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 2048 if (env->is_enabled(JVMTI_EVENT_COMPILED_METHOD_LOAD)) { 2049 if (env->phase() == JVMTI_PHASE_PRIMORDIAL) { 2050 continue; 2051 } 2052 EVT_TRACE(JVMTI_EVENT_COMPILED_METHOD_LOAD, 2053 ("[%s] class compile method load event sent %s.%s ", 2054 JvmtiTrace::safe_get_thread_name(thread), 2055 (nm->method() == NULL) ? "NULL" : nm->method()->klass_name()->as_C_string(), 2056 (nm->method() == NULL) ? "NULL" : nm->method()->name()->as_C_string())); 2057 ResourceMark rm(thread); 2058 HandleMark hm(thread); 2059 2060 // Add inlining information 2061 jvmtiCompiledMethodLoadInlineRecord* inlinerecord = create_inline_record(nm); 2062 // Pass inlining information through the void pointer 2063 JvmtiCompiledMethodLoadEventMark jem(thread, nm, inlinerecord); 2064 JvmtiJavaThreadEventTransition jet(thread); 2065 jvmtiEventCompiledMethodLoad callback = env->callbacks()->CompiledMethodLoad; 2066 if (callback != NULL) { 2067 (*callback)(env->jvmti_external(), jem.jni_methodID(), 2068 jem.code_size(), jem.code_data(), jem.map_length(), 2069 jem.map(), jem.compile_info()); 2070 } 2071 } 2072 } 2073} 2074 2075 2076// post a COMPILED_METHOD_LOAD event for a given environment 2077void JvmtiExport::post_compiled_method_load(JvmtiEnv* env, const jmethodID method, const jint length, 2078 const void *code_begin, const jint map_length, 2079 const jvmtiAddrLocationMap* map) 2080{ 2081 if (env->phase() <= JVMTI_PHASE_PRIMORDIAL) { 2082 return; 2083 } 2084 JavaThread* thread = JavaThread::current(); 2085 EVT_TRIG_TRACE(JVMTI_EVENT_COMPILED_METHOD_LOAD, 2086 ("[%s] method compile load event triggered (by GenerateEvents)", 2087 JvmtiTrace::safe_get_thread_name(thread))); 2088 if (env->is_enabled(JVMTI_EVENT_COMPILED_METHOD_LOAD)) { 2089 2090 EVT_TRACE(JVMTI_EVENT_COMPILED_METHOD_LOAD, 2091 ("[%s] class compile method load event sent (by GenerateEvents), jmethodID=" PTR_FORMAT, 2092 JvmtiTrace::safe_get_thread_name(thread), p2i(method))); 2093 2094 JvmtiEventMark jem(thread); 2095 JvmtiJavaThreadEventTransition jet(thread); 2096 jvmtiEventCompiledMethodLoad callback = env->callbacks()->CompiledMethodLoad; 2097 if (callback != NULL) { 2098 (*callback)(env->jvmti_external(), method, 2099 length, code_begin, map_length, 2100 map, NULL); 2101 } 2102 } 2103} 2104 2105void JvmtiExport::post_dynamic_code_generated_internal(const char *name, const void *code_begin, const void *code_end) { 2106 assert(name != NULL && name[0] != '\0', "sanity check"); 2107 2108 JavaThread* thread = JavaThread::current(); 2109 // In theory everyone coming thru here is in_vm but we need to be certain 2110 // because a callee will do a vm->native transition 2111 ThreadInVMfromUnknown __tiv; 2112 2113 EVT_TRIG_TRACE(JVMTI_EVENT_DYNAMIC_CODE_GENERATED, 2114 ("[%s] method dynamic code generated event triggered", 2115 JvmtiTrace::safe_get_thread_name(thread))); 2116 JvmtiEnvIterator it; 2117 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 2118 if (env->is_enabled(JVMTI_EVENT_DYNAMIC_CODE_GENERATED)) { 2119 EVT_TRACE(JVMTI_EVENT_DYNAMIC_CODE_GENERATED, 2120 ("[%s] dynamic code generated event sent for %s", 2121 JvmtiTrace::safe_get_thread_name(thread), name)); 2122 JvmtiEventMark jem(thread); 2123 JvmtiJavaThreadEventTransition jet(thread); 2124 jint length = (jint)pointer_delta(code_end, code_begin, sizeof(char)); 2125 jvmtiEventDynamicCodeGenerated callback = env->callbacks()->DynamicCodeGenerated; 2126 if (callback != NULL) { 2127 (*callback)(env->jvmti_external(), name, (void*)code_begin, length); 2128 } 2129 } 2130 } 2131} 2132 2133void JvmtiExport::post_dynamic_code_generated(const char *name, const void *code_begin, const void *code_end) { 2134 jvmtiPhase phase = JvmtiEnv::get_phase(); 2135 if (phase == JVMTI_PHASE_PRIMORDIAL || phase == JVMTI_PHASE_START) { 2136 post_dynamic_code_generated_internal(name, code_begin, code_end); 2137 } else { 2138 // It may not be safe to post the event from this thread. Defer all 2139 // postings to the service thread so that it can perform them in a safe 2140 // context and in-order. 2141 MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag); 2142 JvmtiDeferredEvent event = JvmtiDeferredEvent::dynamic_code_generated_event( 2143 name, code_begin, code_end); 2144 JvmtiDeferredEventQueue::enqueue(event); 2145 } 2146} 2147 2148 2149// post a DYNAMIC_CODE_GENERATED event for a given environment 2150// used by GenerateEvents 2151void JvmtiExport::post_dynamic_code_generated(JvmtiEnv* env, const char *name, 2152 const void *code_begin, const void *code_end) 2153{ 2154 JavaThread* thread = JavaThread::current(); 2155 EVT_TRIG_TRACE(JVMTI_EVENT_DYNAMIC_CODE_GENERATED, 2156 ("[%s] dynamic code generated event triggered (by GenerateEvents)", 2157 JvmtiTrace::safe_get_thread_name(thread))); 2158 if (env->is_enabled(JVMTI_EVENT_DYNAMIC_CODE_GENERATED)) { 2159 EVT_TRACE(JVMTI_EVENT_DYNAMIC_CODE_GENERATED, 2160 ("[%s] dynamic code generated event sent for %s", 2161 JvmtiTrace::safe_get_thread_name(thread), name)); 2162 JvmtiEventMark jem(thread); 2163 JvmtiJavaThreadEventTransition jet(thread); 2164 jint length = (jint)pointer_delta(code_end, code_begin, sizeof(char)); 2165 jvmtiEventDynamicCodeGenerated callback = env->callbacks()->DynamicCodeGenerated; 2166 if (callback != NULL) { 2167 (*callback)(env->jvmti_external(), name, (void*)code_begin, length); 2168 } 2169 } 2170} 2171 2172// post a DynamicCodeGenerated event while holding locks in the VM. 2173void JvmtiExport::post_dynamic_code_generated_while_holding_locks(const char* name, 2174 address code_begin, address code_end) 2175{ 2176 // register the stub with the current dynamic code event collector 2177 JvmtiThreadState* state = JvmtiThreadState::state_for(JavaThread::current()); 2178 // state can only be NULL if the current thread is exiting which 2179 // should not happen since we're trying to post an event 2180 guarantee(state != NULL, "attempt to register stub via an exiting thread"); 2181 JvmtiDynamicCodeEventCollector* collector = state->get_dynamic_code_event_collector(); 2182 guarantee(collector != NULL, "attempt to register stub without event collector"); 2183 collector->register_stub(name, code_begin, code_end); 2184} 2185 2186// Collect all the vm internally allocated objects which are visible to java world 2187void JvmtiExport::record_vm_internal_object_allocation(oop obj) { 2188 Thread* thread = Thread::current_or_null(); 2189 if (thread != NULL && thread->is_Java_thread()) { 2190 // Can not take safepoint here. 2191 NoSafepointVerifier no_sfpt; 2192 // Can not take safepoint here so can not use state_for to get 2193 // jvmti thread state. 2194 JvmtiThreadState *state = ((JavaThread*)thread)->jvmti_thread_state(); 2195 if (state != NULL ) { 2196 // state is non NULL when VMObjectAllocEventCollector is enabled. 2197 JvmtiVMObjectAllocEventCollector *collector; 2198 collector = state->get_vm_object_alloc_event_collector(); 2199 if (collector != NULL && collector->is_enabled()) { 2200 // Don't record classes as these will be notified via the ClassLoad 2201 // event. 2202 if (obj->klass() != SystemDictionary::Class_klass()) { 2203 collector->record_allocation(obj); 2204 } 2205 } 2206 } 2207 } 2208} 2209 2210void JvmtiExport::post_garbage_collection_finish() { 2211 Thread *thread = Thread::current(); // this event is posted from VM-Thread. 2212 EVT_TRIG_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, 2213 ("[%s] garbage collection finish event triggered", 2214 JvmtiTrace::safe_get_thread_name(thread))); 2215 JvmtiEnvIterator it; 2216 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 2217 if (env->is_enabled(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH)) { 2218 EVT_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, 2219 ("[%s] garbage collection finish event sent", 2220 JvmtiTrace::safe_get_thread_name(thread))); 2221 JvmtiThreadEventTransition jet(thread); 2222 // JNIEnv is NULL here because this event is posted from VM Thread 2223 jvmtiEventGarbageCollectionFinish callback = env->callbacks()->GarbageCollectionFinish; 2224 if (callback != NULL) { 2225 (*callback)(env->jvmti_external()); 2226 } 2227 } 2228 } 2229} 2230 2231void JvmtiExport::post_garbage_collection_start() { 2232 Thread* thread = Thread::current(); // this event is posted from vm-thread. 2233 EVT_TRIG_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_START, 2234 ("[%s] garbage collection start event triggered", 2235 JvmtiTrace::safe_get_thread_name(thread))); 2236 JvmtiEnvIterator it; 2237 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 2238 if (env->is_enabled(JVMTI_EVENT_GARBAGE_COLLECTION_START)) { 2239 EVT_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_START, 2240 ("[%s] garbage collection start event sent", 2241 JvmtiTrace::safe_get_thread_name(thread))); 2242 JvmtiThreadEventTransition jet(thread); 2243 // JNIEnv is NULL here because this event is posted from VM Thread 2244 jvmtiEventGarbageCollectionStart callback = env->callbacks()->GarbageCollectionStart; 2245 if (callback != NULL) { 2246 (*callback)(env->jvmti_external()); 2247 } 2248 } 2249 } 2250} 2251 2252void JvmtiExport::post_data_dump() { 2253 Thread *thread = Thread::current(); 2254 EVT_TRIG_TRACE(JVMTI_EVENT_DATA_DUMP_REQUEST, 2255 ("[%s] data dump request event triggered", 2256 JvmtiTrace::safe_get_thread_name(thread))); 2257 JvmtiEnvIterator it; 2258 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 2259 if (env->is_enabled(JVMTI_EVENT_DATA_DUMP_REQUEST)) { 2260 EVT_TRACE(JVMTI_EVENT_DATA_DUMP_REQUEST, 2261 ("[%s] data dump request event sent", 2262 JvmtiTrace::safe_get_thread_name(thread))); 2263 JvmtiThreadEventTransition jet(thread); 2264 // JNIEnv is NULL here because this event is posted from VM Thread 2265 jvmtiEventDataDumpRequest callback = env->callbacks()->DataDumpRequest; 2266 if (callback != NULL) { 2267 (*callback)(env->jvmti_external()); 2268 } 2269 } 2270 } 2271} 2272 2273void JvmtiExport::post_monitor_contended_enter(JavaThread *thread, ObjectMonitor *obj_mntr) { 2274 oop object = (oop)obj_mntr->object(); 2275 if (!ServiceUtil::visible_oop(object)) { 2276 // Ignore monitor contended enter for vm internal object. 2277 return; 2278 } 2279 JvmtiThreadState *state = thread->jvmti_thread_state(); 2280 if (state == NULL) { 2281 return; 2282 } 2283 2284 HandleMark hm(thread); 2285 Handle h(thread, object); 2286 2287 EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTER, 2288 ("[%s] montior contended enter event triggered", 2289 JvmtiTrace::safe_get_thread_name(thread))); 2290 2291 JvmtiEnvThreadStateIterator it(state); 2292 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 2293 if (ets->is_enabled(JVMTI_EVENT_MONITOR_CONTENDED_ENTER)) { 2294 EVT_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTER, 2295 ("[%s] monitor contended enter event sent", 2296 JvmtiTrace::safe_get_thread_name(thread))); 2297 JvmtiMonitorEventMark jem(thread, h()); 2298 JvmtiEnv *env = ets->get_env(); 2299 JvmtiThreadEventTransition jet(thread); 2300 jvmtiEventMonitorContendedEnter callback = env->callbacks()->MonitorContendedEnter; 2301 if (callback != NULL) { 2302 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_object()); 2303 } 2304 } 2305 } 2306} 2307 2308void JvmtiExport::post_monitor_contended_entered(JavaThread *thread, ObjectMonitor *obj_mntr) { 2309 oop object = (oop)obj_mntr->object(); 2310 if (!ServiceUtil::visible_oop(object)) { 2311 // Ignore monitor contended entered for vm internal object. 2312 return; 2313 } 2314 JvmtiThreadState *state = thread->jvmti_thread_state(); 2315 if (state == NULL) { 2316 return; 2317 } 2318 2319 HandleMark hm(thread); 2320 Handle h(thread, object); 2321 2322 EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTERED, 2323 ("[%s] montior contended entered event triggered", 2324 JvmtiTrace::safe_get_thread_name(thread))); 2325 2326 JvmtiEnvThreadStateIterator it(state); 2327 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 2328 if (ets->is_enabled(JVMTI_EVENT_MONITOR_CONTENDED_ENTERED)) { 2329 EVT_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTERED, 2330 ("[%s] monitor contended enter event sent", 2331 JvmtiTrace::safe_get_thread_name(thread))); 2332 JvmtiMonitorEventMark jem(thread, h()); 2333 JvmtiEnv *env = ets->get_env(); 2334 JvmtiThreadEventTransition jet(thread); 2335 jvmtiEventMonitorContendedEntered callback = env->callbacks()->MonitorContendedEntered; 2336 if (callback != NULL) { 2337 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_object()); 2338 } 2339 } 2340 } 2341} 2342 2343void JvmtiExport::post_monitor_wait(JavaThread *thread, oop object, 2344 jlong timeout) { 2345 JvmtiThreadState *state = thread->jvmti_thread_state(); 2346 if (state == NULL) { 2347 return; 2348 } 2349 2350 HandleMark hm(thread); 2351 Handle h(thread, object); 2352 2353 EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_WAIT, 2354 ("[%s] montior wait event triggered", 2355 JvmtiTrace::safe_get_thread_name(thread))); 2356 2357 JvmtiEnvThreadStateIterator it(state); 2358 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 2359 if (ets->is_enabled(JVMTI_EVENT_MONITOR_WAIT)) { 2360 EVT_TRACE(JVMTI_EVENT_MONITOR_WAIT, 2361 ("[%s] monitor wait event sent", 2362 JvmtiTrace::safe_get_thread_name(thread))); 2363 JvmtiMonitorEventMark jem(thread, h()); 2364 JvmtiEnv *env = ets->get_env(); 2365 JvmtiThreadEventTransition jet(thread); 2366 jvmtiEventMonitorWait callback = env->callbacks()->MonitorWait; 2367 if (callback != NULL) { 2368 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), 2369 jem.jni_object(), timeout); 2370 } 2371 } 2372 } 2373} 2374 2375void JvmtiExport::post_monitor_waited(JavaThread *thread, ObjectMonitor *obj_mntr, jboolean timed_out) { 2376 oop object = (oop)obj_mntr->object(); 2377 if (!ServiceUtil::visible_oop(object)) { 2378 // Ignore monitor waited for vm internal object. 2379 return; 2380 } 2381 JvmtiThreadState *state = thread->jvmti_thread_state(); 2382 if (state == NULL) { 2383 return; 2384 } 2385 2386 HandleMark hm(thread); 2387 Handle h(thread, object); 2388 2389 EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_WAITED, 2390 ("[%s] montior waited event triggered", 2391 JvmtiTrace::safe_get_thread_name(thread))); 2392 2393 JvmtiEnvThreadStateIterator it(state); 2394 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 2395 if (ets->is_enabled(JVMTI_EVENT_MONITOR_WAITED)) { 2396 EVT_TRACE(JVMTI_EVENT_MONITOR_WAITED, 2397 ("[%s] monitor waited event sent", 2398 JvmtiTrace::safe_get_thread_name(thread))); 2399 JvmtiMonitorEventMark jem(thread, h()); 2400 JvmtiEnv *env = ets->get_env(); 2401 JvmtiThreadEventTransition jet(thread); 2402 jvmtiEventMonitorWaited callback = env->callbacks()->MonitorWaited; 2403 if (callback != NULL) { 2404 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), 2405 jem.jni_object(), timed_out); 2406 } 2407 } 2408 } 2409} 2410 2411 2412void JvmtiExport::post_vm_object_alloc(JavaThread *thread, oop object) { 2413 EVT_TRIG_TRACE(JVMTI_EVENT_VM_OBJECT_ALLOC, ("[%s] Trg vm object alloc triggered", 2414 JvmtiTrace::safe_get_thread_name(thread))); 2415 if (object == NULL) { 2416 return; 2417 } 2418 HandleMark hm(thread); 2419 Handle h(thread, object); 2420 JvmtiEnvIterator it; 2421 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 2422 if (env->is_enabled(JVMTI_EVENT_VM_OBJECT_ALLOC)) { 2423 EVT_TRACE(JVMTI_EVENT_VM_OBJECT_ALLOC, ("[%s] Evt vmobject alloc sent %s", 2424 JvmtiTrace::safe_get_thread_name(thread), 2425 object==NULL? "NULL" : object->klass()->external_name())); 2426 2427 JvmtiVMObjectAllocEventMark jem(thread, h()); 2428 JvmtiJavaThreadEventTransition jet(thread); 2429 jvmtiEventVMObjectAlloc callback = env->callbacks()->VMObjectAlloc; 2430 if (callback != NULL) { 2431 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), 2432 jem.jni_jobject(), jem.jni_class(), jem.size()); 2433 } 2434 } 2435 } 2436} 2437 2438//////////////////////////////////////////////////////////////////////////////////////////////// 2439 2440void JvmtiExport::cleanup_thread(JavaThread* thread) { 2441 assert(JavaThread::current() == thread, "thread is not current"); 2442 MutexLocker mu(JvmtiThreadState_lock); 2443 2444 if (thread->jvmti_thread_state() != NULL) { 2445 // This has to happen after the thread state is removed, which is 2446 // why it is not in post_thread_end_event like its complement 2447 // Maybe both these functions should be rolled into the posts? 2448 JvmtiEventController::thread_ended(thread); 2449 } 2450} 2451 2452void JvmtiExport::clear_detected_exception(JavaThread* thread) { 2453 assert(JavaThread::current() == thread, "thread is not current"); 2454 2455 JvmtiThreadState* state = thread->jvmti_thread_state(); 2456 if (state != NULL) { 2457 state->clear_exception_detected(); 2458 } 2459} 2460 2461void JvmtiExport::oops_do(OopClosure* f) { 2462 JvmtiCurrentBreakpoints::oops_do(f); 2463 JvmtiVMObjectAllocEventCollector::oops_do_for_all_threads(f); 2464} 2465 2466void JvmtiExport::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f) { 2467 JvmtiTagMap::weak_oops_do(is_alive, f); 2468} 2469 2470void JvmtiExport::gc_epilogue() { 2471 JvmtiCurrentBreakpoints::gc_epilogue(); 2472} 2473 2474// Onload raw monitor transition. 2475void JvmtiExport::transition_pending_onload_raw_monitors() { 2476 JvmtiPendingMonitors::transition_raw_monitors(); 2477} 2478 2479//////////////////////////////////////////////////////////////////////////////////////////////// 2480#if INCLUDE_SERVICES 2481// Attach is disabled if SERVICES is not included 2482 2483// type for the Agent_OnAttach entry point 2484extern "C" { 2485 typedef jint (JNICALL *OnAttachEntry_t)(JavaVM*, char *, void *); 2486} 2487 2488jint JvmtiExport::load_agent_library(AttachOperation* op, outputStream* st) { 2489 // get agent name and options 2490 const char* agent = op->arg(0); 2491 const char* absParam = op->arg(1); 2492 const char* options = op->arg(2); 2493 2494 return load_agent_library(agent, absParam, options, st); 2495} 2496 2497jint JvmtiExport::load_agent_library(const char *agent, const char *absParam, 2498 const char *options, outputStream* st) { 2499 char ebuf[1024]; 2500 char buffer[JVM_MAXPATHLEN]; 2501 void* library = NULL; 2502 jint result = JNI_ERR; 2503 const char *on_attach_symbols[] = AGENT_ONATTACH_SYMBOLS; 2504 size_t num_symbol_entries = ARRAY_SIZE(on_attach_symbols); 2505 2506 // The abs paramter should be "true" or "false" 2507 bool is_absolute_path = (absParam != NULL) && (strcmp(absParam,"true")==0); 2508 2509 // Initially marked as invalid. It will be set to valid if we can find the agent 2510 AgentLibrary *agent_lib = new AgentLibrary(agent, options, is_absolute_path, NULL); 2511 2512 // Check for statically linked in agent. If not found then if the path is 2513 // absolute we attempt to load the library. Otherwise we try to load it 2514 // from the standard dll directory. 2515 2516 if (!os::find_builtin_agent(agent_lib, on_attach_symbols, num_symbol_entries)) { 2517 if (is_absolute_path) { 2518 library = os::dll_load(agent, ebuf, sizeof ebuf); 2519 } else { 2520 // Try to load the agent from the standard dll directory 2521 if (os::dll_build_name(buffer, sizeof(buffer), Arguments::get_dll_dir(), 2522 agent)) { 2523 library = os::dll_load(buffer, ebuf, sizeof ebuf); 2524 } 2525 if (library == NULL) { 2526 // not found - try local path 2527 char ns[1] = {0}; 2528 if (os::dll_build_name(buffer, sizeof(buffer), ns, agent)) { 2529 library = os::dll_load(buffer, ebuf, sizeof ebuf); 2530 } 2531 } 2532 } 2533 if (library != NULL) { 2534 agent_lib->set_os_lib(library); 2535 agent_lib->set_valid(); 2536 } 2537 } 2538 // If the library was loaded then we attempt to invoke the Agent_OnAttach 2539 // function 2540 if (agent_lib->valid()) { 2541 // Lookup the Agent_OnAttach function 2542 OnAttachEntry_t on_attach_entry = NULL; 2543 on_attach_entry = CAST_TO_FN_PTR(OnAttachEntry_t, 2544 os::find_agent_function(agent_lib, false, on_attach_symbols, num_symbol_entries)); 2545 if (on_attach_entry == NULL) { 2546 // Agent_OnAttach missing - unload library 2547 if (!agent_lib->is_static_lib()) { 2548 os::dll_unload(library); 2549 } 2550 delete agent_lib; 2551 } else { 2552 // Invoke the Agent_OnAttach function 2553 JavaThread* THREAD = JavaThread::current(); 2554 { 2555 extern struct JavaVM_ main_vm; 2556 JvmtiThreadEventMark jem(THREAD); 2557 JvmtiJavaThreadEventTransition jet(THREAD); 2558 2559 result = (*on_attach_entry)(&main_vm, (char*)options, NULL); 2560 } 2561 2562 // Agent_OnAttach may have used JNI 2563 if (HAS_PENDING_EXCEPTION) { 2564 CLEAR_PENDING_EXCEPTION; 2565 } 2566 2567 // If OnAttach returns JNI_OK then we add it to the list of 2568 // agent libraries so that we can call Agent_OnUnload later. 2569 if (result == JNI_OK) { 2570 Arguments::add_loaded_agent(agent_lib); 2571 } else { 2572 delete agent_lib; 2573 } 2574 2575 // Agent_OnAttach executed so completion status is JNI_OK 2576 st->print_cr("%d", result); 2577 result = JNI_OK; 2578 } 2579 } 2580 return result; 2581} 2582 2583#endif // INCLUDE_SERVICES 2584//////////////////////////////////////////////////////////////////////////////////////////////// 2585 2586// Setup current current thread for event collection. 2587void JvmtiEventCollector::setup_jvmti_thread_state() { 2588 // set this event collector to be the current one. 2589 JvmtiThreadState* state = JvmtiThreadState::state_for(JavaThread::current()); 2590 // state can only be NULL if the current thread is exiting which 2591 // should not happen since we're trying to configure for event collection 2592 guarantee(state != NULL, "exiting thread called setup_jvmti_thread_state"); 2593 if (is_vm_object_alloc_event()) { 2594 _prev = state->get_vm_object_alloc_event_collector(); 2595 state->set_vm_object_alloc_event_collector((JvmtiVMObjectAllocEventCollector *)this); 2596 } else if (is_dynamic_code_event()) { 2597 _prev = state->get_dynamic_code_event_collector(); 2598 state->set_dynamic_code_event_collector((JvmtiDynamicCodeEventCollector *)this); 2599 } 2600} 2601 2602// Unset current event collection in this thread and reset it with previous 2603// collector. 2604void JvmtiEventCollector::unset_jvmti_thread_state() { 2605 JvmtiThreadState* state = JavaThread::current()->jvmti_thread_state(); 2606 if (state != NULL) { 2607 // restore the previous event collector (if any) 2608 if (is_vm_object_alloc_event()) { 2609 if (state->get_vm_object_alloc_event_collector() == this) { 2610 state->set_vm_object_alloc_event_collector((JvmtiVMObjectAllocEventCollector *)_prev); 2611 } else { 2612 // this thread's jvmti state was created during the scope of 2613 // the event collector. 2614 } 2615 } else { 2616 if (is_dynamic_code_event()) { 2617 if (state->get_dynamic_code_event_collector() == this) { 2618 state->set_dynamic_code_event_collector((JvmtiDynamicCodeEventCollector *)_prev); 2619 } else { 2620 // this thread's jvmti state was created during the scope of 2621 // the event collector. 2622 } 2623 } 2624 } 2625 } 2626} 2627 2628// create the dynamic code event collector 2629JvmtiDynamicCodeEventCollector::JvmtiDynamicCodeEventCollector() : _code_blobs(NULL) { 2630 if (JvmtiExport::should_post_dynamic_code_generated()) { 2631 setup_jvmti_thread_state(); 2632 } 2633} 2634 2635// iterate over any code blob descriptors collected and post a 2636// DYNAMIC_CODE_GENERATED event to the profiler. 2637JvmtiDynamicCodeEventCollector::~JvmtiDynamicCodeEventCollector() { 2638 assert(!JavaThread::current()->owns_locks(), "all locks must be released to post deferred events"); 2639 // iterate over any code blob descriptors that we collected 2640 if (_code_blobs != NULL) { 2641 for (int i=0; i<_code_blobs->length(); i++) { 2642 JvmtiCodeBlobDesc* blob = _code_blobs->at(i); 2643 JvmtiExport::post_dynamic_code_generated(blob->name(), blob->code_begin(), blob->code_end()); 2644 FreeHeap(blob); 2645 } 2646 delete _code_blobs; 2647 } 2648 unset_jvmti_thread_state(); 2649} 2650 2651// register a stub 2652void JvmtiDynamicCodeEventCollector::register_stub(const char* name, address start, address end) { 2653 if (_code_blobs == NULL) { 2654 _code_blobs = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JvmtiCodeBlobDesc*>(1,true); 2655 } 2656 _code_blobs->append(new JvmtiCodeBlobDesc(name, start, end)); 2657} 2658 2659// Setup current thread to record vm allocated objects. 2660JvmtiVMObjectAllocEventCollector::JvmtiVMObjectAllocEventCollector() : _allocated(NULL) { 2661 if (JvmtiExport::should_post_vm_object_alloc()) { 2662 _enable = true; 2663 setup_jvmti_thread_state(); 2664 } else { 2665 _enable = false; 2666 } 2667} 2668 2669// Post vm_object_alloc event for vm allocated objects visible to java 2670// world. 2671JvmtiVMObjectAllocEventCollector::~JvmtiVMObjectAllocEventCollector() { 2672 if (_allocated != NULL) { 2673 set_enabled(false); 2674 for (int i = 0; i < _allocated->length(); i++) { 2675 oop obj = _allocated->at(i); 2676 if (ServiceUtil::visible_oop(obj)) { 2677 JvmtiExport::post_vm_object_alloc(JavaThread::current(), obj); 2678 } 2679 } 2680 delete _allocated; 2681 } 2682 unset_jvmti_thread_state(); 2683} 2684 2685void JvmtiVMObjectAllocEventCollector::record_allocation(oop obj) { 2686 assert(is_enabled(), "VM object alloc event collector is not enabled"); 2687 if (_allocated == NULL) { 2688 _allocated = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<oop>(1, true); 2689 } 2690 _allocated->push(obj); 2691} 2692 2693// GC support. 2694void JvmtiVMObjectAllocEventCollector::oops_do(OopClosure* f) { 2695 if (_allocated != NULL) { 2696 for(int i=_allocated->length() - 1; i >= 0; i--) { 2697 if (_allocated->at(i) != NULL) { 2698 f->do_oop(_allocated->adr_at(i)); 2699 } 2700 } 2701 } 2702} 2703 2704void JvmtiVMObjectAllocEventCollector::oops_do_for_all_threads(OopClosure* f) { 2705 // no-op if jvmti not enabled 2706 if (!JvmtiEnv::environments_might_exist()) { 2707 return; 2708 } 2709 2710 // Runs at safepoint. So no need to acquire Threads_lock. 2711 for (JavaThread *jthr = Threads::first(); jthr != NULL; jthr = jthr->next()) { 2712 JvmtiThreadState *state = jthr->jvmti_thread_state(); 2713 if (state != NULL) { 2714 JvmtiVMObjectAllocEventCollector *collector; 2715 collector = state->get_vm_object_alloc_event_collector(); 2716 while (collector != NULL) { 2717 collector->oops_do(f); 2718 collector = (JvmtiVMObjectAllocEventCollector *)collector->get_prev(); 2719 } 2720 } 2721 } 2722} 2723 2724 2725// Disable collection of VMObjectAlloc events 2726NoJvmtiVMObjectAllocMark::NoJvmtiVMObjectAllocMark() : _collector(NULL) { 2727 // a no-op if VMObjectAlloc event is not enabled 2728 if (!JvmtiExport::should_post_vm_object_alloc()) { 2729 return; 2730 } 2731 Thread* thread = Thread::current_or_null(); 2732 if (thread != NULL && thread->is_Java_thread()) { 2733 JavaThread* current_thread = (JavaThread*)thread; 2734 JvmtiThreadState *state = current_thread->jvmti_thread_state(); 2735 if (state != NULL) { 2736 JvmtiVMObjectAllocEventCollector *collector; 2737 collector = state->get_vm_object_alloc_event_collector(); 2738 if (collector != NULL && collector->is_enabled()) { 2739 _collector = collector; 2740 _collector->set_enabled(false); 2741 } 2742 } 2743 } 2744} 2745 2746// Re-Enable collection of VMObjectAlloc events (if previously enabled) 2747NoJvmtiVMObjectAllocMark::~NoJvmtiVMObjectAllocMark() { 2748 if (was_enabled()) { 2749 _collector->set_enabled(true); 2750 } 2751}; 2752 2753JvmtiGCMarker::JvmtiGCMarker() { 2754 // if there aren't any JVMTI environments then nothing to do 2755 if (!JvmtiEnv::environments_might_exist()) { 2756 return; 2757 } 2758 2759 if (JvmtiExport::should_post_garbage_collection_start()) { 2760 JvmtiExport::post_garbage_collection_start(); 2761 } 2762 2763 if (SafepointSynchronize::is_at_safepoint()) { 2764 // Do clean up tasks that need to be done at a safepoint 2765 JvmtiEnvBase::check_for_periodic_clean_up(); 2766 } 2767} 2768 2769JvmtiGCMarker::~JvmtiGCMarker() { 2770 // if there aren't any JVMTI environments then nothing to do 2771 if (!JvmtiEnv::environments_might_exist()) { 2772 return; 2773 } 2774 2775 // JVMTI notify gc finish 2776 if (JvmtiExport::should_post_garbage_collection_finish()) { 2777 JvmtiExport::post_garbage_collection_finish(); 2778 } 2779} 2780