1 /* 2 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/systemDictionary.hpp" 27 #include "code/nmethod.hpp" 28 #include "code/pcDesc.hpp" 29 #include "code/scopeDesc.hpp" 30 #include "interpreter/interpreter.hpp" 31 #include "jvmtifiles/jvmtiEnv.hpp" 32 #include "memory/resourceArea.hpp" 33 #include "oops/objArrayKlass.hpp" 34 #include "oops/objArrayOop.hpp" 35 #include "oops/oop.inline.hpp" 36 #include "prims/jvmtiCodeBlobEvents.hpp" 37 #include "prims/jvmtiEventController.hpp" 38 #include "prims/jvmtiEventController.inline.hpp" 39 #include "prims/jvmtiExport.hpp" 40 #include "prims/jvmtiImpl.hpp" 41 #include "prims/jvmtiManageCapabilities.hpp" 42 #include "prims/jvmtiRawMonitor.hpp" 43 #include "prims/jvmtiRedefineClasses.hpp" 44 #include "prims/jvmtiTagMap.hpp" 45 #include "prims/jvmtiThreadState.inline.hpp" 46 #include "runtime/arguments.hpp" 47 #include "runtime/handles.hpp" 48 #include "runtime/interfaceSupport.hpp" 49 #include "runtime/javaCalls.hpp" 50 #include "runtime/objectMonitor.hpp" 51 #include "runtime/objectMonitor.inline.hpp" 52 #include "runtime/os.inline.hpp" 53 #include "runtime/thread.inline.hpp" 54 #include "runtime/vframe.hpp" 55 #include "services/attachListener.hpp" 56 #include "services/serviceUtil.hpp" 57 #include "utilities/macros.hpp" 58 #if INCLUDE_ALL_GCS 59 #include "gc/parallel/psMarkSweep.hpp" 60 #endif // INCLUDE_ALL_GCS 61 62 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 63 64 #ifdef JVMTI_TRACE 65 #define EVT_TRACE(evt,out) if ((JvmtiTrace::event_trace_flags(evt) & JvmtiTrace::SHOW_EVENT_SENT) != 0) { SafeResourceMark rm; tty->print_cr out; } 66 #define EVT_TRIG_TRACE(evt,out) if ((JvmtiTrace::event_trace_flags(evt) & JvmtiTrace::SHOW_EVENT_TRIGGER) != 0) { SafeResourceMark rm; tty->print_cr out; } 67 #else 68 #define EVT_TRIG_TRACE(evt,out) 69 #define EVT_TRACE(evt,out) 70 #endif 71 72 /////////////////////////////////////////////////////////////// 73 // 74 // JvmtiEventTransition 75 // 76 // TO DO -- 77 // more handle purging 78 79 // Use this for JavaThreads and state is _thread_in_vm. 80 class JvmtiJavaThreadEventTransition : StackObj { 81 private: 82 ResourceMark _rm; 83 ThreadToNativeFromVM _transition; 84 HandleMark _hm; 85 86 public: 87 JvmtiJavaThreadEventTransition(JavaThread *thread) : 88 _rm(), 89 _transition(thread), 90 _hm(thread) {}; 91 }; 92 93 // For JavaThreads which are not in _thread_in_vm state 94 // and other system threads use this. 95 class JvmtiThreadEventTransition : StackObj { 96 private: 97 ResourceMark _rm; 98 HandleMark _hm; 99 JavaThreadState _saved_state; 100 JavaThread *_jthread; 101 102 public: 103 JvmtiThreadEventTransition(Thread *thread) : _rm(), _hm() { 104 if (thread->is_Java_thread()) { 105 _jthread = (JavaThread *)thread; 106 _saved_state = _jthread->thread_state(); 107 if (_saved_state == _thread_in_Java) { 108 ThreadStateTransition::transition_from_java(_jthread, _thread_in_native); 109 } else { 110 ThreadStateTransition::transition(_jthread, _saved_state, _thread_in_native); 111 } 112 } else { 113 _jthread = NULL; 114 } 115 } 116 117 ~JvmtiThreadEventTransition() { 118 if (_jthread != NULL) 119 ThreadStateTransition::transition_from_native(_jthread, _saved_state); 120 } 121 }; 122 123 124 /////////////////////////////////////////////////////////////// 125 // 126 // JvmtiEventMark 127 // 128 129 class JvmtiEventMark : public StackObj { 130 private: 131 JavaThread *_thread; 132 JNIEnv* _jni_env; 133 bool _exception_detected; 134 bool _exception_caught; 135 #if 0 136 JNIHandleBlock* _hblock; 137 #endif 138 139 public: 140 JvmtiEventMark(JavaThread *thread) : _thread(thread), 141 _jni_env(thread->jni_environment()) { 142 #if 0 143 _hblock = thread->active_handles(); 144 _hblock->clear_thoroughly(); // so we can be safe 145 #else 146 // we want to use the code above - but that needs the JNIHandle changes - later... 147 // for now, steal JNI push local frame code 148 JvmtiThreadState *state = thread->jvmti_thread_state(); 149 // we are before an event. 150 // Save current jvmti thread exception state. 151 if (state != NULL) { 152 _exception_detected = state->is_exception_detected(); 153 _exception_caught = state->is_exception_caught(); 154 } else { 155 _exception_detected = false; 156 _exception_caught = false; 157 } 158 159 JNIHandleBlock* old_handles = thread->active_handles(); 160 JNIHandleBlock* new_handles = JNIHandleBlock::allocate_block(thread); 161 assert(new_handles != NULL, "should not be NULL"); 162 new_handles->set_pop_frame_link(old_handles); 163 thread->set_active_handles(new_handles); 164 #endif 165 assert(thread == JavaThread::current(), "thread must be current!"); 166 thread->frame_anchor()->make_walkable(thread); 167 }; 168 169 ~JvmtiEventMark() { 170 #if 0 171 _hblock->clear(); // for consistency with future correct behavior 172 #else 173 // we want to use the code above - but that needs the JNIHandle changes - later... 174 // for now, steal JNI pop local frame code 175 JNIHandleBlock* old_handles = _thread->active_handles(); 176 JNIHandleBlock* new_handles = old_handles->pop_frame_link(); 177 assert(new_handles != NULL, "should not be NULL"); 178 _thread->set_active_handles(new_handles); 179 // Note that we set the pop_frame_link to NULL explicitly, otherwise 180 // the release_block call will release the blocks. 181 old_handles->set_pop_frame_link(NULL); 182 JNIHandleBlock::release_block(old_handles, _thread); // may block 183 #endif 184 185 JvmtiThreadState* state = _thread->jvmti_thread_state(); 186 // we are continuing after an event. 187 if (state != NULL) { 188 // Restore the jvmti thread exception state. 189 if (_exception_detected) { 190 state->set_exception_detected(); 191 } 192 if (_exception_caught) { 193 state->set_exception_caught(); 194 } 195 } 196 } 197 198 #if 0 199 jobject to_jobject(oop obj) { return obj == NULL? NULL : _hblock->allocate_handle_fast(obj); } 200 #else 201 // we want to use the code above - but that needs the JNIHandle changes - later... 202 // for now, use regular make_local 203 jobject to_jobject(oop obj) { return JNIHandles::make_local(_thread,obj); } 204 #endif 205 206 jclass to_jclass(Klass* klass) { return (klass == NULL ? NULL : (jclass)to_jobject(klass->java_mirror())); } 207 208 jmethodID to_jmethodID(methodHandle method) { return method->jmethod_id(); } 209 210 JNIEnv* jni_env() { return _jni_env; } 211 }; 212 213 class JvmtiThreadEventMark : public JvmtiEventMark { 214 private: 215 jthread _jt; 216 217 public: 218 JvmtiThreadEventMark(JavaThread *thread) : 219 JvmtiEventMark(thread) { 220 _jt = (jthread)(to_jobject(thread->threadObj())); 221 }; 222 jthread jni_thread() { return _jt; } 223 }; 224 225 class JvmtiClassEventMark : public JvmtiThreadEventMark { 226 private: 227 jclass _jc; 228 229 public: 230 JvmtiClassEventMark(JavaThread *thread, Klass* klass) : 231 JvmtiThreadEventMark(thread) { 232 _jc = to_jclass(klass); 233 }; 234 jclass jni_class() { return _jc; } 235 }; 236 237 class JvmtiMethodEventMark : public JvmtiThreadEventMark { 238 private: 239 jmethodID _mid; 240 241 public: 242 JvmtiMethodEventMark(JavaThread *thread, methodHandle method) : 243 JvmtiThreadEventMark(thread), 244 _mid(to_jmethodID(method)) {}; 245 jmethodID jni_methodID() { return _mid; } 246 }; 247 248 class JvmtiLocationEventMark : public JvmtiMethodEventMark { 249 private: 250 jlocation _loc; 251 252 public: 253 JvmtiLocationEventMark(JavaThread *thread, methodHandle method, address location) : 254 JvmtiMethodEventMark(thread, method), 255 _loc(location - method->code_base()) {}; 256 jlocation location() { return _loc; } 257 }; 258 259 class JvmtiExceptionEventMark : public JvmtiLocationEventMark { 260 private: 261 jobject _exc; 262 263 public: 264 JvmtiExceptionEventMark(JavaThread *thread, methodHandle method, address location, Handle exception) : 265 JvmtiLocationEventMark(thread, method, location), 266 _exc(to_jobject(exception())) {}; 267 jobject exception() { return _exc; } 268 }; 269 270 class JvmtiClassFileLoadEventMark : public JvmtiThreadEventMark { 271 private: 272 const char *_class_name; 273 jobject _jloader; 274 jobject _protection_domain; 275 jclass _class_being_redefined; 276 277 public: 278 JvmtiClassFileLoadEventMark(JavaThread *thread, Symbol* name, 279 Handle class_loader, Handle prot_domain, KlassHandle *class_being_redefined) : JvmtiThreadEventMark(thread) { 280 _class_name = name != NULL? name->as_utf8() : NULL; 281 _jloader = (jobject)to_jobject(class_loader()); 282 _protection_domain = (jobject)to_jobject(prot_domain()); 283 if (class_being_redefined == NULL) { 284 _class_being_redefined = NULL; 285 } else { 286 _class_being_redefined = (jclass)to_jclass((*class_being_redefined)()); 287 } 288 }; 289 const char *class_name() { 290 return _class_name; 291 } 292 jobject jloader() { 293 return _jloader; 294 } 295 jobject protection_domain() { 296 return _protection_domain; 297 } 298 jclass class_being_redefined() { 299 return _class_being_redefined; 300 } 301 }; 302 303 ////////////////////////////////////////////////////////////////////////////// 304 305 int JvmtiExport::_field_access_count = 0; 306 int JvmtiExport::_field_modification_count = 0; 307 308 bool JvmtiExport::_can_access_local_variables = false; 309 bool JvmtiExport::_can_hotswap_or_post_breakpoint = false; 310 bool JvmtiExport::_can_modify_any_class = false; 311 bool JvmtiExport::_can_walk_any_space = false; 312 313 bool JvmtiExport::_has_redefined_a_class = false; 314 bool JvmtiExport::_all_dependencies_are_recorded = false; 315 316 // 317 // field access management 318 // 319 320 // interpreter generator needs the address of the counter 321 address JvmtiExport::get_field_access_count_addr() { 322 // We don't grab a lock because we don't want to 323 // serialize field access between all threads. This means that a 324 // thread on another processor can see the wrong count value and 325 // may either miss making a needed call into post_field_access() 326 // or will make an unneeded call into post_field_access(). We pay 327 // this price to avoid slowing down the VM when we aren't watching 328 // field accesses. 329 // Other access/mutation safe by virtue of being in VM state. 330 return (address)(&_field_access_count); 331 } 332 333 // 334 // field modification management 335 // 336 337 // interpreter generator needs the address of the counter 338 address JvmtiExport::get_field_modification_count_addr() { 339 // We don't grab a lock because we don't 340 // want to serialize field modification between all threads. This 341 // means that a thread on another processor can see the wrong 342 // count value and may either miss making a needed call into 343 // post_field_modification() or will make an unneeded call into 344 // post_field_modification(). We pay this price to avoid slowing 345 // down the VM when we aren't watching field modifications. 346 // Other access/mutation safe by virtue of being in VM state. 347 return (address)(&_field_modification_count); 348 } 349 350 351 /////////////////////////////////////////////////////////////// 352 // Functions needed by java.lang.instrument for starting up javaagent. 353 /////////////////////////////////////////////////////////////// 354 355 jint 356 JvmtiExport::get_jvmti_interface(JavaVM *jvm, void **penv, jint version) { 357 // The JVMTI_VERSION_INTERFACE_JVMTI part of the version number 358 // has already been validated in JNI GetEnv(). 359 int major, minor, micro; 360 361 // micro version doesn't matter here (yet?) 362 decode_version_values(version, &major, &minor, µ); 363 switch (major) { 364 case 1: 365 switch (minor) { 366 case 0: // version 1.0.<micro> is recognized 367 case 1: // version 1.1.<micro> is recognized 368 case 2: // version 1.2.<micro> is recognized 369 break; 370 371 default: 372 return JNI_EVERSION; // unsupported minor version number 373 } 374 break; 375 default: 376 return JNI_EVERSION; // unsupported major version number 377 } 378 379 if (JvmtiEnv::get_phase() == JVMTI_PHASE_LIVE) { 380 JavaThread* current_thread = (JavaThread*) ThreadLocalStorage::thread(); 381 // transition code: native to VM 382 ThreadInVMfromNative __tiv(current_thread); 383 VM_ENTRY_BASE(jvmtiEnv*, JvmtiExport::get_jvmti_interface, current_thread) 384 debug_only(VMNativeEntryWrapper __vew;) 385 386 JvmtiEnv *jvmti_env = JvmtiEnv::create_a_jvmti(version); 387 *penv = jvmti_env->jvmti_external(); // actual type is jvmtiEnv* -- not to be confused with JvmtiEnv* 388 return JNI_OK; 389 390 } else if (JvmtiEnv::get_phase() == JVMTI_PHASE_ONLOAD) { 391 // not live, no thread to transition 392 JvmtiEnv *jvmti_env = JvmtiEnv::create_a_jvmti(version); 393 *penv = jvmti_env->jvmti_external(); // actual type is jvmtiEnv* -- not to be confused with JvmtiEnv* 394 return JNI_OK; 395 396 } else { 397 // Called at the wrong time 398 *penv = NULL; 399 return JNI_EDETACHED; 400 } 401 } 402 403 void 404 JvmtiExport::add_default_read_edges(Handle h_module, TRAPS) { 405 if (!Universe::is_module_initialized()) { 406 return; // extra safety 407 } 408 assert(!h_module.is_null(), "module should always be set"); 409 410 // Invoke the transformedByAgent method 411 JavaValue result(T_VOID); 412 JavaCalls::call_static(&result, 413 SystemDictionary::module_Modules_klass(), 414 vmSymbols::transformedByAgent_name(), 415 vmSymbols::transformedByAgent_signature(), 416 h_module, 417 THREAD); 418 419 if (HAS_PENDING_EXCEPTION) { 420 java_lang_Throwable::print(PENDING_EXCEPTION, tty); 421 CLEAR_PENDING_EXCEPTION; 422 return; 423 } 424 } 425 426 void 427 JvmtiExport::decode_version_values(jint version, int * major, int * minor, 428 int * micro) { 429 *major = (version & JVMTI_VERSION_MASK_MAJOR) >> JVMTI_VERSION_SHIFT_MAJOR; 430 *minor = (version & JVMTI_VERSION_MASK_MINOR) >> JVMTI_VERSION_SHIFT_MINOR; 431 *micro = (version & JVMTI_VERSION_MASK_MICRO) >> JVMTI_VERSION_SHIFT_MICRO; 432 } 433 434 void JvmtiExport::enter_primordial_phase() { 435 JvmtiEnvBase::set_phase(JVMTI_PHASE_PRIMORDIAL); 436 } 437 438 void JvmtiExport::enter_start_phase() { 439 JvmtiManageCapabilities::recompute_always_capabilities(); 440 JvmtiEnvBase::set_phase(JVMTI_PHASE_START); 441 } 442 443 void JvmtiExport::enter_onload_phase() { 444 JvmtiEnvBase::set_phase(JVMTI_PHASE_ONLOAD); 445 } 446 447 void JvmtiExport::enter_live_phase() { 448 JvmtiEnvBase::set_phase(JVMTI_PHASE_LIVE); 449 } 450 451 // 452 // JVMTI events that the VM posts to the debugger and also startup agent 453 // and call the agent's premain() for java.lang.instrument. 454 // 455 456 void JvmtiExport::post_vm_start() { 457 EVT_TRIG_TRACE(JVMTI_EVENT_VM_START, ("JVMTI Trg VM start event triggered" )); 458 459 // can now enable some events 460 JvmtiEventController::vm_start(); 461 462 JvmtiEnvIterator it; 463 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 464 if (env->is_enabled(JVMTI_EVENT_VM_START)) { 465 EVT_TRACE(JVMTI_EVENT_VM_START, ("JVMTI Evt VM start event sent" )); 466 467 JavaThread *thread = JavaThread::current(); 468 JvmtiThreadEventMark jem(thread); 469 JvmtiJavaThreadEventTransition jet(thread); 470 jvmtiEventVMStart callback = env->callbacks()->VMStart; 471 if (callback != NULL) { 472 (*callback)(env->jvmti_external(), jem.jni_env()); 473 } 474 } 475 } 476 } 477 478 479 void JvmtiExport::post_vm_initialized() { 480 EVT_TRIG_TRACE(JVMTI_EVENT_VM_INIT, ("JVMTI Trg VM init event triggered" )); 481 482 // can now enable events 483 JvmtiEventController::vm_init(); 484 485 JvmtiEnvIterator it; 486 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 487 if (env->is_enabled(JVMTI_EVENT_VM_INIT)) { 488 EVT_TRACE(JVMTI_EVENT_VM_INIT, ("JVMTI Evt VM init event sent" )); 489 490 JavaThread *thread = JavaThread::current(); 491 JvmtiThreadEventMark jem(thread); 492 JvmtiJavaThreadEventTransition jet(thread); 493 jvmtiEventVMInit callback = env->callbacks()->VMInit; 494 if (callback != NULL) { 495 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread()); 496 } 497 } 498 } 499 } 500 501 502 void JvmtiExport::post_vm_death() { 503 EVT_TRIG_TRACE(JVMTI_EVENT_VM_DEATH, ("JVMTI Trg VM death event triggered" )); 504 505 JvmtiEnvIterator it; 506 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 507 if (env->is_enabled(JVMTI_EVENT_VM_DEATH)) { 508 EVT_TRACE(JVMTI_EVENT_VM_DEATH, ("JVMTI Evt VM death event sent" )); 509 510 JavaThread *thread = JavaThread::current(); 511 JvmtiEventMark jem(thread); 512 JvmtiJavaThreadEventTransition jet(thread); 513 jvmtiEventVMDeath callback = env->callbacks()->VMDeath; 514 if (callback != NULL) { 515 (*callback)(env->jvmti_external(), jem.jni_env()); 516 } 517 } 518 } 519 520 JvmtiEnvBase::set_phase(JVMTI_PHASE_DEAD); 521 JvmtiEventController::vm_death(); 522 } 523 524 char** 525 JvmtiExport::get_all_native_method_prefixes(int* count_ptr) { 526 // Have to grab JVMTI thread state lock to be sure environment doesn't 527 // go away while we iterate them. No locks during VM bring-up. 528 if (Threads::number_of_threads() == 0 || SafepointSynchronize::is_at_safepoint()) { 529 return JvmtiEnvBase::get_all_native_method_prefixes(count_ptr); 530 } else { 531 MutexLocker mu(JvmtiThreadState_lock); 532 return JvmtiEnvBase::get_all_native_method_prefixes(count_ptr); 533 } 534 } 535 536 class JvmtiClassFileLoadHookPoster : public StackObj { 537 private: 538 Symbol* _h_name; 539 Handle _class_loader; 540 Handle _h_protection_domain; 541 unsigned char ** _data_ptr; 542 unsigned char ** _end_ptr; 543 JavaThread * _thread; 544 jint _curr_len; 545 unsigned char * _curr_data; 546 JvmtiEnv * _curr_env; 547 JvmtiCachedClassFileData ** _cached_class_file_ptr; 548 JvmtiThreadState * _state; 549 KlassHandle * _h_class_being_redefined; 550 JvmtiClassLoadKind _load_kind; 551 552 public: 553 inline JvmtiClassFileLoadHookPoster(Symbol* h_name, Handle class_loader, 554 Handle h_protection_domain, 555 unsigned char **data_ptr, unsigned char **end_ptr, 556 JvmtiCachedClassFileData **cache_ptr) { 557 _h_name = h_name; 558 _class_loader = class_loader; 559 _h_protection_domain = h_protection_domain; 560 _data_ptr = data_ptr; 561 _end_ptr = end_ptr; 562 _thread = JavaThread::current(); 563 _curr_len = *end_ptr - *data_ptr; 564 _curr_data = *data_ptr; 565 _curr_env = NULL; 566 _cached_class_file_ptr = cache_ptr; 567 568 _state = _thread->jvmti_thread_state(); 569 if (_state != NULL) { 570 _h_class_being_redefined = _state->get_class_being_redefined(); 571 _load_kind = _state->get_class_load_kind(); 572 Klass* klass = (_h_class_being_redefined == NULL) ? NULL : (*_h_class_being_redefined)(); 573 if (_load_kind != jvmti_class_load_kind_load && klass != NULL) { 574 ModuleEntry* module_entry = InstanceKlass::cast(klass)->module(); 575 assert(module_entry != NULL, "module_entry should always be set"); 576 if (module_entry->is_named() && 577 module_entry->jlrM_module() != NULL && 578 !module_entry->has_default_read_edges()) { 579 if (!module_entry->set_has_default_read_edges()) { 580 // We won a potential race. 581 // Add read edges to the unnamed modules of the bootstrap and app class loaders 582 Handle class_module(_thread, JNIHandles::resolve(module_entry->jlrM_module())); // Obtain j.l.r.Module 583 JvmtiExport::add_default_read_edges(class_module, _thread); 584 } 585 } 586 } 587 // Clear class_being_redefined flag here. The action 588 // from agent handler could generate a new class file load 589 // hook event and if it is not cleared the new event generated 590 // from regular class file load could have this stale redefined 591 // class handle info. 592 _state->clear_class_being_redefined(); 593 } else { 594 // redefine and retransform will always set the thread state 595 _h_class_being_redefined = (KlassHandle *) NULL; 596 _load_kind = jvmti_class_load_kind_load; 597 } 598 } 599 600 void post() { 601 // EVT_TRIG_TRACE(JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, 602 // ("JVMTI [%s] class file load hook event triggered", 603 // JvmtiTrace::safe_get_thread_name(_thread))); 604 post_all_envs(); 605 copy_modified_data(); 606 } 607 608 private: 609 void post_all_envs() { 610 if (_load_kind != jvmti_class_load_kind_retransform) { 611 // for class load and redefine, 612 // call the non-retransformable agents 613 JvmtiEnvIterator it; 614 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 615 if (!env->is_retransformable() && env->is_enabled(JVMTI_EVENT_CLASS_FILE_LOAD_HOOK)) { 616 // non-retransformable agents cannot retransform back, 617 // so no need to cache the original class file bytes 618 post_to_env(env, false); 619 } 620 } 621 } 622 JvmtiEnvIterator it; 623 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 624 // retransformable agents get all events 625 if (env->is_retransformable() && env->is_enabled(JVMTI_EVENT_CLASS_FILE_LOAD_HOOK)) { 626 // retransformable agents need to cache the original class file 627 // bytes if changes are made via the ClassFileLoadHook 628 post_to_env(env, true); 629 } 630 } 631 } 632 633 void post_to_env(JvmtiEnv* env, bool caching_needed) { 634 unsigned char *new_data = NULL; 635 jint new_len = 0; 636 // EVT_TRACE(JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, 637 // ("JVMTI [%s] class file load hook event sent %s data_ptr = %d, data_len = %d", 638 // JvmtiTrace::safe_get_thread_name(_thread), 639 // _h_name == NULL ? "NULL" : _h_name->as_utf8(), 640 // _curr_data, _curr_len )); 641 JvmtiClassFileLoadEventMark jem(_thread, _h_name, _class_loader, 642 _h_protection_domain, 643 _h_class_being_redefined); 644 JvmtiJavaThreadEventTransition jet(_thread); 645 JNIEnv* jni_env = (JvmtiEnv::get_phase() == JVMTI_PHASE_PRIMORDIAL)? 646 NULL : jem.jni_env(); 647 jvmtiEventClassFileLoadHook callback = env->callbacks()->ClassFileLoadHook; 648 if (callback != NULL) { 649 (*callback)(env->jvmti_external(), jni_env, 650 jem.class_being_redefined(), 651 jem.jloader(), jem.class_name(), 652 jem.protection_domain(), 653 _curr_len, _curr_data, 654 &new_len, &new_data); 655 } 656 if (new_data != NULL) { 657 // this agent has modified class data. 658 if (caching_needed && *_cached_class_file_ptr == NULL) { 659 // data has been changed by the new retransformable agent 660 // and it hasn't already been cached, cache it 661 JvmtiCachedClassFileData *p; 662 p = (JvmtiCachedClassFileData *)os::malloc( 663 offset_of(JvmtiCachedClassFileData, data) + _curr_len, mtInternal); 664 if (p == NULL) { 665 vm_exit_out_of_memory(offset_of(JvmtiCachedClassFileData, data) + _curr_len, 666 OOM_MALLOC_ERROR, 667 "unable to allocate cached copy of original class bytes"); 668 } 669 p->length = _curr_len; 670 memcpy(p->data, _curr_data, _curr_len); 671 *_cached_class_file_ptr = p; 672 } 673 674 if (_curr_data != *_data_ptr) { 675 // curr_data is previous agent modified class data. 676 // And this has been changed by the new agent so 677 // we can delete it now. 678 _curr_env->Deallocate(_curr_data); 679 } 680 681 // Class file data has changed by the current agent. 682 _curr_data = new_data; 683 _curr_len = new_len; 684 // Save the current agent env we need this to deallocate the 685 // memory allocated by this agent. 686 _curr_env = env; 687 } 688 } 689 690 void copy_modified_data() { 691 // if one of the agent has modified class file data. 692 // Copy modified class data to new resources array. 693 if (_curr_data != *_data_ptr) { 694 *_data_ptr = NEW_RESOURCE_ARRAY(u1, _curr_len); 695 memcpy(*_data_ptr, _curr_data, _curr_len); 696 *_end_ptr = *_data_ptr + _curr_len; 697 _curr_env->Deallocate(_curr_data); 698 } 699 } 700 }; 701 702 bool JvmtiExport::_should_post_class_file_load_hook = false; 703 704 // this entry is for class file load hook on class load, redefine and retransform 705 void JvmtiExport::post_class_file_load_hook(Symbol* h_name, 706 Handle class_loader, 707 Handle h_protection_domain, 708 unsigned char **data_ptr, 709 unsigned char **end_ptr, 710 JvmtiCachedClassFileData **cache_ptr) { 711 if (JvmtiEnv::get_phase() <= JVMTI_PHASE_PRIMORDIAL) { 712 return; 713 } 714 715 JvmtiClassFileLoadHookPoster poster(h_name, class_loader, 716 h_protection_domain, 717 data_ptr, end_ptr, 718 cache_ptr); 719 poster.post(); 720 } 721 722 void JvmtiExport::report_unsupported(bool on) { 723 // If any JVMTI service is turned on, we need to exit before native code 724 // tries to access nonexistant services. 725 if (on) { 726 vm_exit_during_initialization("Java Kernel does not support JVMTI."); 727 } 728 } 729 730 731 static inline Klass* oop_to_klass(oop obj) { 732 Klass* k = obj->klass(); 733 734 // if the object is a java.lang.Class then return the java mirror 735 if (k == SystemDictionary::Class_klass()) { 736 if (!java_lang_Class::is_primitive(obj)) { 737 k = java_lang_Class::as_Klass(obj); 738 assert(k != NULL, "class for non-primitive mirror must exist"); 739 } 740 } 741 return k; 742 } 743 744 class JvmtiVMObjectAllocEventMark : public JvmtiClassEventMark { 745 private: 746 jobject _jobj; 747 jlong _size; 748 public: 749 JvmtiVMObjectAllocEventMark(JavaThread *thread, oop obj) : JvmtiClassEventMark(thread, oop_to_klass(obj)) { 750 _jobj = (jobject)to_jobject(obj); 751 _size = obj->size() * wordSize; 752 }; 753 jobject jni_jobject() { return _jobj; } 754 jlong size() { return _size; } 755 }; 756 757 class JvmtiCompiledMethodLoadEventMark : public JvmtiMethodEventMark { 758 private: 759 jint _code_size; 760 const void *_code_data; 761 jint _map_length; 762 jvmtiAddrLocationMap *_map; 763 const void *_compile_info; 764 public: 765 JvmtiCompiledMethodLoadEventMark(JavaThread *thread, nmethod *nm, void* compile_info_ptr = NULL) 766 : JvmtiMethodEventMark(thread,methodHandle(thread, nm->method())) { 767 _code_data = nm->insts_begin(); 768 _code_size = nm->insts_size(); 769 _compile_info = compile_info_ptr; // Set void pointer of compiledMethodLoad Event. Default value is NULL. 770 JvmtiCodeBlobEvents::build_jvmti_addr_location_map(nm, &_map, &_map_length); 771 } 772 ~JvmtiCompiledMethodLoadEventMark() { 773 FREE_C_HEAP_ARRAY(jvmtiAddrLocationMap, _map); 774 } 775 776 jint code_size() { return _code_size; } 777 const void *code_data() { return _code_data; } 778 jint map_length() { return _map_length; } 779 const jvmtiAddrLocationMap* map() { return _map; } 780 const void *compile_info() { return _compile_info; } 781 }; 782 783 784 785 class JvmtiMonitorEventMark : public JvmtiThreadEventMark { 786 private: 787 jobject _jobj; 788 public: 789 JvmtiMonitorEventMark(JavaThread *thread, oop object) 790 : JvmtiThreadEventMark(thread){ 791 _jobj = to_jobject(object); 792 } 793 jobject jni_object() { return _jobj; } 794 }; 795 796 /////////////////////////////////////////////////////////////// 797 // 798 // pending CompiledMethodUnload support 799 // 800 801 void JvmtiExport::post_compiled_method_unload( 802 jmethodID method, const void *code_begin) { 803 JavaThread* thread = JavaThread::current(); 804 EVT_TRIG_TRACE(JVMTI_EVENT_COMPILED_METHOD_UNLOAD, 805 ("JVMTI [%s] method compile unload event triggered", 806 JvmtiTrace::safe_get_thread_name(thread))); 807 808 // post the event for each environment that has this event enabled. 809 JvmtiEnvIterator it; 810 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 811 if (env->is_enabled(JVMTI_EVENT_COMPILED_METHOD_UNLOAD)) { 812 813 EVT_TRACE(JVMTI_EVENT_COMPILED_METHOD_UNLOAD, 814 ("JVMTI [%s] class compile method unload event sent jmethodID " PTR_FORMAT, 815 JvmtiTrace::safe_get_thread_name(thread), method)); 816 817 ResourceMark rm(thread); 818 819 JvmtiEventMark jem(thread); 820 JvmtiJavaThreadEventTransition jet(thread); 821 jvmtiEventCompiledMethodUnload callback = env->callbacks()->CompiledMethodUnload; 822 if (callback != NULL) { 823 (*callback)(env->jvmti_external(), method, code_begin); 824 } 825 } 826 } 827 } 828 829 /////////////////////////////////////////////////////////////// 830 // 831 // JvmtiExport 832 // 833 834 void JvmtiExport::post_raw_breakpoint(JavaThread *thread, Method* method, address location) { 835 HandleMark hm(thread); 836 methodHandle mh(thread, method); 837 838 JvmtiThreadState *state = thread->jvmti_thread_state(); 839 if (state == NULL) { 840 return; 841 } 842 EVT_TRIG_TRACE(JVMTI_EVENT_BREAKPOINT, ("JVMTI [%s] Trg Breakpoint triggered", 843 JvmtiTrace::safe_get_thread_name(thread))); 844 JvmtiEnvThreadStateIterator it(state); 845 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 846 ets->compare_and_set_current_location(mh(), location, JVMTI_EVENT_BREAKPOINT); 847 if (!ets->breakpoint_posted() && ets->is_enabled(JVMTI_EVENT_BREAKPOINT)) { 848 ThreadState old_os_state = thread->osthread()->get_state(); 849 thread->osthread()->set_state(BREAKPOINTED); 850 EVT_TRACE(JVMTI_EVENT_BREAKPOINT, ("JVMTI [%s] Evt Breakpoint sent %s.%s @ %d", 851 JvmtiTrace::safe_get_thread_name(thread), 852 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), 853 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(), 854 location - mh()->code_base() )); 855 856 JvmtiEnv *env = ets->get_env(); 857 JvmtiLocationEventMark jem(thread, mh, location); 858 JvmtiJavaThreadEventTransition jet(thread); 859 jvmtiEventBreakpoint callback = env->callbacks()->Breakpoint; 860 if (callback != NULL) { 861 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), 862 jem.jni_methodID(), jem.location()); 863 } 864 865 ets->set_breakpoint_posted(); 866 thread->osthread()->set_state(old_os_state); 867 } 868 } 869 } 870 871 ////////////////////////////////////////////////////////////////////////////// 872 873 bool JvmtiExport::_can_get_source_debug_extension = false; 874 bool JvmtiExport::_can_maintain_original_method_order = false; 875 bool JvmtiExport::_can_post_interpreter_events = false; 876 bool JvmtiExport::_can_post_on_exceptions = false; 877 bool JvmtiExport::_can_post_breakpoint = false; 878 bool JvmtiExport::_can_post_field_access = false; 879 bool JvmtiExport::_can_post_field_modification = false; 880 bool JvmtiExport::_can_post_method_entry = false; 881 bool JvmtiExport::_can_post_method_exit = false; 882 bool JvmtiExport::_can_pop_frame = false; 883 bool JvmtiExport::_can_force_early_return = false; 884 885 bool JvmtiExport::_should_post_single_step = false; 886 bool JvmtiExport::_should_post_field_access = false; 887 bool JvmtiExport::_should_post_field_modification = false; 888 bool JvmtiExport::_should_post_class_load = false; 889 bool JvmtiExport::_should_post_class_prepare = false; 890 bool JvmtiExport::_should_post_class_unload = false; 891 bool JvmtiExport::_should_post_thread_life = false; 892 bool JvmtiExport::_should_clean_up_heap_objects = false; 893 bool JvmtiExport::_should_post_native_method_bind = false; 894 bool JvmtiExport::_should_post_dynamic_code_generated = false; 895 bool JvmtiExport::_should_post_data_dump = false; 896 bool JvmtiExport::_should_post_compiled_method_load = false; 897 bool JvmtiExport::_should_post_compiled_method_unload = false; 898 bool JvmtiExport::_should_post_monitor_contended_enter = false; 899 bool JvmtiExport::_should_post_monitor_contended_entered = false; 900 bool JvmtiExport::_should_post_monitor_wait = false; 901 bool JvmtiExport::_should_post_monitor_waited = false; 902 bool JvmtiExport::_should_post_garbage_collection_start = false; 903 bool JvmtiExport::_should_post_garbage_collection_finish = false; 904 bool JvmtiExport::_should_post_object_free = false; 905 bool JvmtiExport::_should_post_resource_exhausted = false; 906 bool JvmtiExport::_should_post_vm_object_alloc = false; 907 bool JvmtiExport::_should_post_on_exceptions = false; 908 909 //////////////////////////////////////////////////////////////////////////////////////////////// 910 911 912 // 913 // JVMTI single step management 914 // 915 void JvmtiExport::at_single_stepping_point(JavaThread *thread, Method* method, address location) { 916 assert(JvmtiExport::should_post_single_step(), "must be single stepping"); 917 918 HandleMark hm(thread); 919 methodHandle mh(thread, method); 920 921 // update information about current location and post a step event 922 JvmtiThreadState *state = thread->jvmti_thread_state(); 923 if (state == NULL) { 924 return; 925 } 926 EVT_TRIG_TRACE(JVMTI_EVENT_SINGLE_STEP, ("JVMTI [%s] Trg Single Step triggered", 927 JvmtiTrace::safe_get_thread_name(thread))); 928 if (!state->hide_single_stepping()) { 929 if (state->is_pending_step_for_popframe()) { 930 state->process_pending_step_for_popframe(); 931 } 932 if (state->is_pending_step_for_earlyret()) { 933 state->process_pending_step_for_earlyret(); 934 } 935 JvmtiExport::post_single_step(thread, mh(), location); 936 } 937 } 938 939 940 void JvmtiExport::expose_single_stepping(JavaThread *thread) { 941 JvmtiThreadState *state = thread->jvmti_thread_state(); 942 if (state != NULL) { 943 state->clear_hide_single_stepping(); 944 } 945 } 946 947 948 bool JvmtiExport::hide_single_stepping(JavaThread *thread) { 949 JvmtiThreadState *state = thread->jvmti_thread_state(); 950 if (state != NULL && state->is_enabled(JVMTI_EVENT_SINGLE_STEP)) { 951 state->set_hide_single_stepping(); 952 return true; 953 } else { 954 return false; 955 } 956 } 957 958 void JvmtiExport::post_class_load(JavaThread *thread, Klass* klass) { 959 if (JvmtiEnv::get_phase() <= JVMTI_PHASE_PRIMORDIAL) { 960 return; 961 } 962 HandleMark hm(thread); 963 KlassHandle kh(thread, klass); 964 965 EVT_TRIG_TRACE(JVMTI_EVENT_CLASS_LOAD, ("JVMTI [%s] Trg Class Load triggered", 966 JvmtiTrace::safe_get_thread_name(thread))); 967 JvmtiThreadState* state = thread->jvmti_thread_state(); 968 if (state == NULL) { 969 return; 970 } 971 JvmtiEnvThreadStateIterator it(state); 972 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 973 if (ets->is_enabled(JVMTI_EVENT_CLASS_LOAD)) { 974 EVT_TRACE(JVMTI_EVENT_CLASS_LOAD, ("JVMTI [%s] Evt Class Load sent %s", 975 JvmtiTrace::safe_get_thread_name(thread), 976 kh()==NULL? "NULL" : kh()->external_name() )); 977 978 JvmtiEnv *env = ets->get_env(); 979 JvmtiClassEventMark jem(thread, kh()); 980 JvmtiJavaThreadEventTransition jet(thread); 981 jvmtiEventClassLoad callback = env->callbacks()->ClassLoad; 982 if (callback != NULL) { 983 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_class()); 984 } 985 } 986 } 987 } 988 989 990 void JvmtiExport::post_class_prepare(JavaThread *thread, Klass* klass) { 991 if (JvmtiEnv::get_phase() <= JVMTI_PHASE_PRIMORDIAL) { 992 return; 993 } 994 HandleMark hm(thread); 995 KlassHandle kh(thread, klass); 996 997 EVT_TRIG_TRACE(JVMTI_EVENT_CLASS_PREPARE, ("JVMTI [%s] Trg Class Prepare triggered", 998 JvmtiTrace::safe_get_thread_name(thread))); 999 JvmtiThreadState* state = thread->jvmti_thread_state(); 1000 if (state == NULL) { 1001 return; 1002 } 1003 JvmtiEnvThreadStateIterator it(state); 1004 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 1005 if (ets->is_enabled(JVMTI_EVENT_CLASS_PREPARE)) { 1006 EVT_TRACE(JVMTI_EVENT_CLASS_PREPARE, ("JVMTI [%s] Evt Class Prepare sent %s", 1007 JvmtiTrace::safe_get_thread_name(thread), 1008 kh()==NULL? "NULL" : kh()->external_name() )); 1009 1010 JvmtiEnv *env = ets->get_env(); 1011 JvmtiClassEventMark jem(thread, kh()); 1012 JvmtiJavaThreadEventTransition jet(thread); 1013 jvmtiEventClassPrepare callback = env->callbacks()->ClassPrepare; 1014 if (callback != NULL) { 1015 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_class()); 1016 } 1017 } 1018 } 1019 } 1020 1021 void JvmtiExport::post_class_unload(Klass* klass) { 1022 if (JvmtiEnv::get_phase() <= JVMTI_PHASE_PRIMORDIAL) { 1023 return; 1024 } 1025 Thread *thread = Thread::current(); 1026 HandleMark hm(thread); 1027 KlassHandle kh(thread, klass); 1028 1029 EVT_TRIG_TRACE(EXT_EVENT_CLASS_UNLOAD, ("JVMTI [?] Trg Class Unload triggered" )); 1030 if (JvmtiEventController::is_enabled((jvmtiEvent)EXT_EVENT_CLASS_UNLOAD)) { 1031 assert(thread->is_VM_thread(), "wrong thread"); 1032 1033 // get JavaThread for whom we are proxy 1034 JavaThread *real_thread = 1035 (JavaThread *)((VMThread *)thread)->vm_operation()->calling_thread(); 1036 1037 JvmtiEnvIterator it; 1038 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 1039 if (env->is_enabled((jvmtiEvent)EXT_EVENT_CLASS_UNLOAD)) { 1040 EVT_TRACE(EXT_EVENT_CLASS_UNLOAD, ("JVMTI [?] Evt Class Unload sent %s", 1041 kh()==NULL? "NULL" : kh()->external_name() )); 1042 1043 // do everything manually, since this is a proxy - needs special care 1044 JNIEnv* jni_env = real_thread->jni_environment(); 1045 jthread jt = (jthread)JNIHandles::make_local(real_thread, real_thread->threadObj()); 1046 jclass jk = (jclass)JNIHandles::make_local(real_thread, kh()->java_mirror()); 1047 1048 // Before we call the JVMTI agent, we have to set the state in the 1049 // thread for which we are proxying. 1050 JavaThreadState prev_state = real_thread->thread_state(); 1051 assert(((Thread *)real_thread)->is_ConcurrentGC_thread() || 1052 (real_thread->is_Java_thread() && prev_state == _thread_blocked), 1053 "should be ConcurrentGCThread or JavaThread at safepoint"); 1054 real_thread->set_thread_state(_thread_in_native); 1055 1056 jvmtiExtensionEvent callback = env->ext_callbacks()->ClassUnload; 1057 if (callback != NULL) { 1058 (*callback)(env->jvmti_external(), jni_env, jt, jk); 1059 } 1060 1061 assert(real_thread->thread_state() == _thread_in_native, 1062 "JavaThread should be in native"); 1063 real_thread->set_thread_state(prev_state); 1064 1065 JNIHandles::destroy_local(jk); 1066 JNIHandles::destroy_local(jt); 1067 } 1068 } 1069 } 1070 } 1071 1072 1073 void JvmtiExport::post_thread_start(JavaThread *thread) { 1074 if (JvmtiEnv::get_phase() <= JVMTI_PHASE_PRIMORDIAL) { 1075 return; 1076 } 1077 assert(thread->thread_state() == _thread_in_vm, "must be in vm state"); 1078 1079 EVT_TRIG_TRACE(JVMTI_EVENT_THREAD_START, ("JVMTI [%s] Trg Thread Start event triggered", 1080 JvmtiTrace::safe_get_thread_name(thread))); 1081 1082 // do JVMTI thread initialization (if needed) 1083 JvmtiEventController::thread_started(thread); 1084 1085 // Do not post thread start event for hidden java thread. 1086 if (JvmtiEventController::is_enabled(JVMTI_EVENT_THREAD_START) && 1087 !thread->is_hidden_from_external_view()) { 1088 JvmtiEnvIterator it; 1089 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 1090 if (env->is_enabled(JVMTI_EVENT_THREAD_START)) { 1091 EVT_TRACE(JVMTI_EVENT_THREAD_START, ("JVMTI [%s] Evt Thread Start event sent", 1092 JvmtiTrace::safe_get_thread_name(thread) )); 1093 1094 JvmtiThreadEventMark jem(thread); 1095 JvmtiJavaThreadEventTransition jet(thread); 1096 jvmtiEventThreadStart callback = env->callbacks()->ThreadStart; 1097 if (callback != NULL) { 1098 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread()); 1099 } 1100 } 1101 } 1102 } 1103 } 1104 1105 1106 void JvmtiExport::post_thread_end(JavaThread *thread) { 1107 if (JvmtiEnv::get_phase() <= JVMTI_PHASE_PRIMORDIAL) { 1108 return; 1109 } 1110 EVT_TRIG_TRACE(JVMTI_EVENT_THREAD_END, ("JVMTI [%s] Trg Thread End event triggered", 1111 JvmtiTrace::safe_get_thread_name(thread))); 1112 1113 JvmtiThreadState *state = thread->jvmti_thread_state(); 1114 if (state == NULL) { 1115 return; 1116 } 1117 1118 // Do not post thread end event for hidden java thread. 1119 if (state->is_enabled(JVMTI_EVENT_THREAD_END) && 1120 !thread->is_hidden_from_external_view()) { 1121 1122 JvmtiEnvThreadStateIterator it(state); 1123 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 1124 if (ets->is_enabled(JVMTI_EVENT_THREAD_END)) { 1125 EVT_TRACE(JVMTI_EVENT_THREAD_END, ("JVMTI [%s] Evt Thread End event sent", 1126 JvmtiTrace::safe_get_thread_name(thread) )); 1127 1128 JvmtiEnv *env = ets->get_env(); 1129 JvmtiThreadEventMark jem(thread); 1130 JvmtiJavaThreadEventTransition jet(thread); 1131 jvmtiEventThreadEnd callback = env->callbacks()->ThreadEnd; 1132 if (callback != NULL) { 1133 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread()); 1134 } 1135 } 1136 } 1137 } 1138 } 1139 1140 void JvmtiExport::post_object_free(JvmtiEnv* env, jlong tag) { 1141 assert(SafepointSynchronize::is_at_safepoint(), "must be executed at safepoint"); 1142 assert(env->is_enabled(JVMTI_EVENT_OBJECT_FREE), "checking"); 1143 1144 EVT_TRIG_TRACE(JVMTI_EVENT_OBJECT_FREE, ("JVMTI [?] Trg Object Free triggered" )); 1145 EVT_TRACE(JVMTI_EVENT_OBJECT_FREE, ("JVMTI [?] Evt Object Free sent")); 1146 1147 jvmtiEventObjectFree callback = env->callbacks()->ObjectFree; 1148 if (callback != NULL) { 1149 (*callback)(env->jvmti_external(), tag); 1150 } 1151 } 1152 1153 void JvmtiExport::post_resource_exhausted(jint resource_exhausted_flags, const char* description) { 1154 EVT_TRIG_TRACE(JVMTI_EVENT_RESOURCE_EXHAUSTED, ("JVMTI Trg resource exhausted event triggered" )); 1155 1156 JvmtiEnvIterator it; 1157 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 1158 if (env->is_enabled(JVMTI_EVENT_RESOURCE_EXHAUSTED)) { 1159 EVT_TRACE(JVMTI_EVENT_RESOURCE_EXHAUSTED, ("JVMTI Evt resource exhausted event sent" )); 1160 1161 JavaThread *thread = JavaThread::current(); 1162 JvmtiThreadEventMark jem(thread); 1163 JvmtiJavaThreadEventTransition jet(thread); 1164 jvmtiEventResourceExhausted callback = env->callbacks()->ResourceExhausted; 1165 if (callback != NULL) { 1166 (*callback)(env->jvmti_external(), jem.jni_env(), 1167 resource_exhausted_flags, NULL, description); 1168 } 1169 } 1170 } 1171 } 1172 1173 void JvmtiExport::post_method_entry(JavaThread *thread, Method* method, frame current_frame) { 1174 HandleMark hm(thread); 1175 methodHandle mh(thread, method); 1176 1177 EVT_TRIG_TRACE(JVMTI_EVENT_METHOD_ENTRY, ("JVMTI [%s] Trg Method Entry triggered %s.%s", 1178 JvmtiTrace::safe_get_thread_name(thread), 1179 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), 1180 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() )); 1181 1182 JvmtiThreadState* state = thread->jvmti_thread_state(); 1183 if (state == NULL || !state->is_interp_only_mode()) { 1184 // for any thread that actually wants method entry, interp_only_mode is set 1185 return; 1186 } 1187 1188 state->incr_cur_stack_depth(); 1189 1190 if (state->is_enabled(JVMTI_EVENT_METHOD_ENTRY)) { 1191 JvmtiEnvThreadStateIterator it(state); 1192 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 1193 if (ets->is_enabled(JVMTI_EVENT_METHOD_ENTRY)) { 1194 EVT_TRACE(JVMTI_EVENT_METHOD_ENTRY, ("JVMTI [%s] Evt Method Entry sent %s.%s", 1195 JvmtiTrace::safe_get_thread_name(thread), 1196 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), 1197 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() )); 1198 1199 JvmtiEnv *env = ets->get_env(); 1200 JvmtiMethodEventMark jem(thread, mh); 1201 JvmtiJavaThreadEventTransition jet(thread); 1202 jvmtiEventMethodEntry callback = env->callbacks()->MethodEntry; 1203 if (callback != NULL) { 1204 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_methodID()); 1205 } 1206 } 1207 } 1208 } 1209 } 1210 1211 void JvmtiExport::post_method_exit(JavaThread *thread, Method* method, frame current_frame) { 1212 HandleMark hm(thread); 1213 methodHandle mh(thread, method); 1214 1215 EVT_TRIG_TRACE(JVMTI_EVENT_METHOD_EXIT, ("JVMTI [%s] Trg Method Exit triggered %s.%s", 1216 JvmtiTrace::safe_get_thread_name(thread), 1217 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), 1218 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() )); 1219 1220 JvmtiThreadState *state = thread->jvmti_thread_state(); 1221 if (state == NULL || !state->is_interp_only_mode()) { 1222 // for any thread that actually wants method exit, interp_only_mode is set 1223 return; 1224 } 1225 1226 // return a flag when a method terminates by throwing an exception 1227 // i.e. if an exception is thrown and it's not caught by the current method 1228 bool exception_exit = state->is_exception_detected() && !state->is_exception_caught(); 1229 1230 1231 if (state->is_enabled(JVMTI_EVENT_METHOD_EXIT)) { 1232 Handle result; 1233 jvalue value; 1234 value.j = 0L; 1235 1236 // if the method hasn't been popped because of an exception then we populate 1237 // the return_value parameter for the callback. At this point we only have 1238 // the address of a "raw result" and we just call into the interpreter to 1239 // convert this into a jvalue. 1240 if (!exception_exit) { 1241 oop oop_result; 1242 BasicType type = current_frame.interpreter_frame_result(&oop_result, &value); 1243 if (type == T_OBJECT || type == T_ARRAY) { 1244 result = Handle(thread, oop_result); 1245 } 1246 } 1247 1248 JvmtiEnvThreadStateIterator it(state); 1249 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 1250 if (ets->is_enabled(JVMTI_EVENT_METHOD_EXIT)) { 1251 EVT_TRACE(JVMTI_EVENT_METHOD_EXIT, ("JVMTI [%s] Evt Method Exit sent %s.%s", 1252 JvmtiTrace::safe_get_thread_name(thread), 1253 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), 1254 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() )); 1255 1256 JvmtiEnv *env = ets->get_env(); 1257 JvmtiMethodEventMark jem(thread, mh); 1258 if (result.not_null()) { 1259 value.l = JNIHandles::make_local(thread, result()); 1260 } 1261 JvmtiJavaThreadEventTransition jet(thread); 1262 jvmtiEventMethodExit callback = env->callbacks()->MethodExit; 1263 if (callback != NULL) { 1264 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), 1265 jem.jni_methodID(), exception_exit, value); 1266 } 1267 } 1268 } 1269 } 1270 1271 if (state->is_enabled(JVMTI_EVENT_FRAME_POP)) { 1272 JvmtiEnvThreadStateIterator it(state); 1273 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 1274 int cur_frame_number = state->cur_stack_depth(); 1275 1276 if (ets->is_frame_pop(cur_frame_number)) { 1277 // we have a NotifyFramePop entry for this frame. 1278 // now check that this env/thread wants this event 1279 if (ets->is_enabled(JVMTI_EVENT_FRAME_POP)) { 1280 EVT_TRACE(JVMTI_EVENT_FRAME_POP, ("JVMTI [%s] Evt Frame Pop sent %s.%s", 1281 JvmtiTrace::safe_get_thread_name(thread), 1282 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), 1283 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() )); 1284 1285 // we also need to issue a frame pop event for this frame 1286 JvmtiEnv *env = ets->get_env(); 1287 JvmtiMethodEventMark jem(thread, mh); 1288 JvmtiJavaThreadEventTransition jet(thread); 1289 jvmtiEventFramePop callback = env->callbacks()->FramePop; 1290 if (callback != NULL) { 1291 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), 1292 jem.jni_methodID(), exception_exit); 1293 } 1294 } 1295 // remove the frame's entry 1296 ets->clear_frame_pop(cur_frame_number); 1297 } 1298 } 1299 } 1300 1301 state->decr_cur_stack_depth(); 1302 } 1303 1304 1305 // Todo: inline this for optimization 1306 void JvmtiExport::post_single_step(JavaThread *thread, Method* method, address location) { 1307 HandleMark hm(thread); 1308 methodHandle mh(thread, method); 1309 1310 JvmtiThreadState *state = thread->jvmti_thread_state(); 1311 if (state == NULL) { 1312 return; 1313 } 1314 JvmtiEnvThreadStateIterator it(state); 1315 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 1316 ets->compare_and_set_current_location(mh(), location, JVMTI_EVENT_SINGLE_STEP); 1317 if (!ets->single_stepping_posted() && ets->is_enabled(JVMTI_EVENT_SINGLE_STEP)) { 1318 EVT_TRACE(JVMTI_EVENT_SINGLE_STEP, ("JVMTI [%s] Evt Single Step sent %s.%s @ %d", 1319 JvmtiTrace::safe_get_thread_name(thread), 1320 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), 1321 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(), 1322 location - mh()->code_base() )); 1323 1324 JvmtiEnv *env = ets->get_env(); 1325 JvmtiLocationEventMark jem(thread, mh, location); 1326 JvmtiJavaThreadEventTransition jet(thread); 1327 jvmtiEventSingleStep callback = env->callbacks()->SingleStep; 1328 if (callback != NULL) { 1329 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), 1330 jem.jni_methodID(), jem.location()); 1331 } 1332 1333 ets->set_single_stepping_posted(); 1334 } 1335 } 1336 } 1337 1338 1339 void JvmtiExport::post_exception_throw(JavaThread *thread, Method* method, address location, oop exception) { 1340 HandleMark hm(thread); 1341 methodHandle mh(thread, method); 1342 Handle exception_handle(thread, exception); 1343 1344 JvmtiThreadState *state = thread->jvmti_thread_state(); 1345 if (state == NULL) { 1346 return; 1347 } 1348 1349 EVT_TRIG_TRACE(JVMTI_EVENT_EXCEPTION, ("JVMTI [%s] Trg Exception thrown triggered", 1350 JvmtiTrace::safe_get_thread_name(thread))); 1351 if (!state->is_exception_detected()) { 1352 state->set_exception_detected(); 1353 JvmtiEnvThreadStateIterator it(state); 1354 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 1355 if (ets->is_enabled(JVMTI_EVENT_EXCEPTION) && (exception != NULL)) { 1356 1357 EVT_TRACE(JVMTI_EVENT_EXCEPTION, 1358 ("JVMTI [%s] Evt Exception thrown sent %s.%s @ %d", 1359 JvmtiTrace::safe_get_thread_name(thread), 1360 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), 1361 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(), 1362 location - mh()->code_base() )); 1363 1364 JvmtiEnv *env = ets->get_env(); 1365 JvmtiExceptionEventMark jem(thread, mh, location, exception_handle); 1366 1367 // It's okay to clear these exceptions here because we duplicate 1368 // this lookup in InterpreterRuntime::exception_handler_for_exception. 1369 EXCEPTION_MARK; 1370 1371 bool should_repeat; 1372 vframeStream st(thread); 1373 assert(!st.at_end(), "cannot be at end"); 1374 Method* current_method = NULL; 1375 // A GC may occur during the Method::fast_exception_handler_bci_for() 1376 // call below if it needs to load the constraint class. Using a 1377 // methodHandle to keep the 'current_method' from being deallocated 1378 // if GC happens. 1379 methodHandle current_mh = methodHandle(thread, current_method); 1380 int current_bci = -1; 1381 do { 1382 current_method = st.method(); 1383 current_mh = methodHandle(thread, current_method); 1384 current_bci = st.bci(); 1385 do { 1386 should_repeat = false; 1387 KlassHandle eh_klass(thread, exception_handle()->klass()); 1388 current_bci = Method::fast_exception_handler_bci_for( 1389 current_mh, eh_klass, current_bci, THREAD); 1390 if (HAS_PENDING_EXCEPTION) { 1391 exception_handle = Handle(thread, PENDING_EXCEPTION); 1392 CLEAR_PENDING_EXCEPTION; 1393 should_repeat = true; 1394 } 1395 } while (should_repeat && (current_bci != -1)); 1396 st.next(); 1397 } while ((current_bci < 0) && (!st.at_end())); 1398 1399 jmethodID catch_jmethodID; 1400 if (current_bci < 0) { 1401 catch_jmethodID = 0; 1402 current_bci = 0; 1403 } else { 1404 catch_jmethodID = jem.to_jmethodID(current_mh); 1405 } 1406 1407 JvmtiJavaThreadEventTransition jet(thread); 1408 jvmtiEventException callback = env->callbacks()->Exception; 1409 if (callback != NULL) { 1410 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), 1411 jem.jni_methodID(), jem.location(), 1412 jem.exception(), 1413 catch_jmethodID, current_bci); 1414 } 1415 } 1416 } 1417 } 1418 1419 // frames may get popped because of this throw, be safe - invalidate cached depth 1420 state->invalidate_cur_stack_depth(); 1421 } 1422 1423 1424 void JvmtiExport::notice_unwind_due_to_exception(JavaThread *thread, Method* method, address location, oop exception, bool in_handler_frame) { 1425 HandleMark hm(thread); 1426 methodHandle mh(thread, method); 1427 Handle exception_handle(thread, exception); 1428 1429 JvmtiThreadState *state = thread->jvmti_thread_state(); 1430 if (state == NULL) { 1431 return; 1432 } 1433 EVT_TRIG_TRACE(JVMTI_EVENT_EXCEPTION_CATCH, 1434 ("JVMTI [%s] Trg unwind_due_to_exception triggered %s.%s @ %s%d - %s", 1435 JvmtiTrace::safe_get_thread_name(thread), 1436 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), 1437 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(), 1438 location==0? "no location:" : "", 1439 location==0? 0 : location - mh()->code_base(), 1440 in_handler_frame? "in handler frame" : "not handler frame" )); 1441 1442 if (state->is_exception_detected()) { 1443 1444 state->invalidate_cur_stack_depth(); 1445 if (!in_handler_frame) { 1446 // Not in exception handler. 1447 if(state->is_interp_only_mode()) { 1448 // method exit and frame pop events are posted only in interp mode. 1449 // When these events are enabled code should be in running in interp mode. 1450 JvmtiExport::post_method_exit(thread, method, thread->last_frame()); 1451 // The cached cur_stack_depth might have changed from the 1452 // operations of frame pop or method exit. We are not 100% sure 1453 // the cached cur_stack_depth is still valid depth so invalidate 1454 // it. 1455 state->invalidate_cur_stack_depth(); 1456 } 1457 } else { 1458 // In exception handler frame. Report exception catch. 1459 assert(location != NULL, "must be a known location"); 1460 // Update cur_stack_depth - the frames above the current frame 1461 // have been unwound due to this exception: 1462 assert(!state->is_exception_caught(), "exception must not be caught yet."); 1463 state->set_exception_caught(); 1464 1465 JvmtiEnvThreadStateIterator it(state); 1466 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 1467 if (ets->is_enabled(JVMTI_EVENT_EXCEPTION_CATCH) && (exception_handle() != NULL)) { 1468 EVT_TRACE(JVMTI_EVENT_EXCEPTION_CATCH, 1469 ("JVMTI [%s] Evt ExceptionCatch sent %s.%s @ %d", 1470 JvmtiTrace::safe_get_thread_name(thread), 1471 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), 1472 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(), 1473 location - mh()->code_base() )); 1474 1475 JvmtiEnv *env = ets->get_env(); 1476 JvmtiExceptionEventMark jem(thread, mh, location, exception_handle); 1477 JvmtiJavaThreadEventTransition jet(thread); 1478 jvmtiEventExceptionCatch callback = env->callbacks()->ExceptionCatch; 1479 if (callback != NULL) { 1480 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), 1481 jem.jni_methodID(), jem.location(), 1482 jem.exception()); 1483 } 1484 } 1485 } 1486 } 1487 } 1488 } 1489 1490 oop JvmtiExport::jni_GetField_probe(JavaThread *thread, jobject jobj, oop obj, 1491 Klass* klass, jfieldID fieldID, bool is_static) { 1492 if (*((int *)get_field_access_count_addr()) > 0 && thread->has_last_Java_frame()) { 1493 // At least one field access watch is set so we have more work 1494 // to do. This wrapper is used by entry points that allow us 1495 // to create handles in post_field_access_by_jni(). 1496 post_field_access_by_jni(thread, obj, klass, fieldID, is_static); 1497 // event posting can block so refetch oop if we were passed a jobj 1498 if (jobj != NULL) return JNIHandles::resolve_non_null(jobj); 1499 } 1500 return obj; 1501 } 1502 1503 oop JvmtiExport::jni_GetField_probe_nh(JavaThread *thread, jobject jobj, oop obj, 1504 Klass* klass, jfieldID fieldID, bool is_static) { 1505 if (*((int *)get_field_access_count_addr()) > 0 && thread->has_last_Java_frame()) { 1506 // At least one field access watch is set so we have more work 1507 // to do. This wrapper is used by "quick" entry points that don't 1508 // allow us to create handles in post_field_access_by_jni(). We 1509 // override that with a ResetNoHandleMark. 1510 ResetNoHandleMark rnhm; 1511 post_field_access_by_jni(thread, obj, klass, fieldID, is_static); 1512 // event posting can block so refetch oop if we were passed a jobj 1513 if (jobj != NULL) return JNIHandles::resolve_non_null(jobj); 1514 } 1515 return obj; 1516 } 1517 1518 void JvmtiExport::post_field_access_by_jni(JavaThread *thread, oop obj, 1519 Klass* klass, jfieldID fieldID, bool is_static) { 1520 // We must be called with a Java context in order to provide reasonable 1521 // values for the klazz, method, and location fields. The callers of this 1522 // function don't make the call unless there is a Java context. 1523 assert(thread->has_last_Java_frame(), "must be called with a Java context"); 1524 1525 ResourceMark rm; 1526 fieldDescriptor fd; 1527 // if get_field_descriptor finds fieldID to be invalid, then we just bail 1528 bool valid_fieldID = JvmtiEnv::get_field_descriptor(klass, fieldID, &fd); 1529 assert(valid_fieldID == true,"post_field_access_by_jni called with invalid fieldID"); 1530 if (!valid_fieldID) return; 1531 // field accesses are not watched so bail 1532 if (!fd.is_field_access_watched()) return; 1533 1534 HandleMark hm(thread); 1535 KlassHandle h_klass(thread, klass); 1536 Handle h_obj; 1537 if (!is_static) { 1538 // non-static field accessors have an object, but we need a handle 1539 assert(obj != NULL, "non-static needs an object"); 1540 h_obj = Handle(thread, obj); 1541 } 1542 post_field_access(thread, 1543 thread->last_frame().interpreter_frame_method(), 1544 thread->last_frame().interpreter_frame_bcp(), 1545 h_klass, h_obj, fieldID); 1546 } 1547 1548 void JvmtiExport::post_field_access(JavaThread *thread, Method* method, 1549 address location, KlassHandle field_klass, Handle object, jfieldID field) { 1550 1551 HandleMark hm(thread); 1552 methodHandle mh(thread, method); 1553 1554 JvmtiThreadState *state = thread->jvmti_thread_state(); 1555 if (state == NULL) { 1556 return; 1557 } 1558 EVT_TRIG_TRACE(JVMTI_EVENT_FIELD_ACCESS, ("JVMTI [%s] Trg Field Access event triggered", 1559 JvmtiTrace::safe_get_thread_name(thread))); 1560 JvmtiEnvThreadStateIterator it(state); 1561 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 1562 if (ets->is_enabled(JVMTI_EVENT_FIELD_ACCESS)) { 1563 EVT_TRACE(JVMTI_EVENT_FIELD_ACCESS, ("JVMTI [%s] Evt Field Access event sent %s.%s @ %d", 1564 JvmtiTrace::safe_get_thread_name(thread), 1565 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), 1566 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(), 1567 location - mh()->code_base() )); 1568 1569 JvmtiEnv *env = ets->get_env(); 1570 JvmtiLocationEventMark jem(thread, mh, location); 1571 jclass field_jclass = jem.to_jclass(field_klass()); 1572 jobject field_jobject = jem.to_jobject(object()); 1573 JvmtiJavaThreadEventTransition jet(thread); 1574 jvmtiEventFieldAccess callback = env->callbacks()->FieldAccess; 1575 if (callback != NULL) { 1576 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), 1577 jem.jni_methodID(), jem.location(), 1578 field_jclass, field_jobject, field); 1579 } 1580 } 1581 } 1582 } 1583 1584 oop JvmtiExport::jni_SetField_probe(JavaThread *thread, jobject jobj, oop obj, 1585 Klass* klass, jfieldID fieldID, bool is_static, 1586 char sig_type, jvalue *value) { 1587 if (*((int *)get_field_modification_count_addr()) > 0 && thread->has_last_Java_frame()) { 1588 // At least one field modification watch is set so we have more work 1589 // to do. This wrapper is used by entry points that allow us 1590 // to create handles in post_field_modification_by_jni(). 1591 post_field_modification_by_jni(thread, obj, klass, fieldID, is_static, sig_type, value); 1592 // event posting can block so refetch oop if we were passed a jobj 1593 if (jobj != NULL) return JNIHandles::resolve_non_null(jobj); 1594 } 1595 return obj; 1596 } 1597 1598 oop JvmtiExport::jni_SetField_probe_nh(JavaThread *thread, jobject jobj, oop obj, 1599 Klass* klass, jfieldID fieldID, bool is_static, 1600 char sig_type, jvalue *value) { 1601 if (*((int *)get_field_modification_count_addr()) > 0 && thread->has_last_Java_frame()) { 1602 // At least one field modification watch is set so we have more work 1603 // to do. This wrapper is used by "quick" entry points that don't 1604 // allow us to create handles in post_field_modification_by_jni(). We 1605 // override that with a ResetNoHandleMark. 1606 ResetNoHandleMark rnhm; 1607 post_field_modification_by_jni(thread, obj, klass, fieldID, is_static, sig_type, value); 1608 // event posting can block so refetch oop if we were passed a jobj 1609 if (jobj != NULL) return JNIHandles::resolve_non_null(jobj); 1610 } 1611 return obj; 1612 } 1613 1614 void JvmtiExport::post_field_modification_by_jni(JavaThread *thread, oop obj, 1615 Klass* klass, jfieldID fieldID, bool is_static, 1616 char sig_type, jvalue *value) { 1617 // We must be called with a Java context in order to provide reasonable 1618 // values for the klazz, method, and location fields. The callers of this 1619 // function don't make the call unless there is a Java context. 1620 assert(thread->has_last_Java_frame(), "must be called with Java context"); 1621 1622 ResourceMark rm; 1623 fieldDescriptor fd; 1624 // if get_field_descriptor finds fieldID to be invalid, then we just bail 1625 bool valid_fieldID = JvmtiEnv::get_field_descriptor(klass, fieldID, &fd); 1626 assert(valid_fieldID == true,"post_field_modification_by_jni called with invalid fieldID"); 1627 if (!valid_fieldID) return; 1628 // field modifications are not watched so bail 1629 if (!fd.is_field_modification_watched()) return; 1630 1631 HandleMark hm(thread); 1632 1633 Handle h_obj; 1634 if (!is_static) { 1635 // non-static field accessors have an object, but we need a handle 1636 assert(obj != NULL, "non-static needs an object"); 1637 h_obj = Handle(thread, obj); 1638 } 1639 KlassHandle h_klass(thread, klass); 1640 post_field_modification(thread, 1641 thread->last_frame().interpreter_frame_method(), 1642 thread->last_frame().interpreter_frame_bcp(), 1643 h_klass, h_obj, fieldID, sig_type, value); 1644 } 1645 1646 void JvmtiExport::post_raw_field_modification(JavaThread *thread, Method* method, 1647 address location, KlassHandle field_klass, Handle object, jfieldID field, 1648 char sig_type, jvalue *value) { 1649 1650 if (sig_type == 'I' || sig_type == 'Z' || sig_type == 'C' || sig_type == 'S') { 1651 // 'I' instructions are used for byte, char, short and int. 1652 // determine which it really is, and convert 1653 fieldDescriptor fd; 1654 bool found = JvmtiEnv::get_field_descriptor(field_klass(), field, &fd); 1655 // should be found (if not, leave as is) 1656 if (found) { 1657 jint ival = value->i; 1658 // convert value from int to appropriate type 1659 switch (fd.field_type()) { 1660 case T_BOOLEAN: 1661 sig_type = 'Z'; 1662 value->i = 0; // clear it 1663 value->z = (jboolean)ival; 1664 break; 1665 case T_BYTE: 1666 sig_type = 'B'; 1667 value->i = 0; // clear it 1668 value->b = (jbyte)ival; 1669 break; 1670 case T_CHAR: 1671 sig_type = 'C'; 1672 value->i = 0; // clear it 1673 value->c = (jchar)ival; 1674 break; 1675 case T_SHORT: 1676 sig_type = 'S'; 1677 value->i = 0; // clear it 1678 value->s = (jshort)ival; 1679 break; 1680 case T_INT: 1681 // nothing to do 1682 break; 1683 default: 1684 // this is an integer instruction, should be one of above 1685 ShouldNotReachHere(); 1686 break; 1687 } 1688 } 1689 } 1690 1691 assert(sig_type != '[', "array should have sig_type == 'L'"); 1692 bool handle_created = false; 1693 1694 // convert oop to JNI handle. 1695 if (sig_type == 'L') { 1696 handle_created = true; 1697 value->l = (jobject)JNIHandles::make_local(thread, (oop)value->l); 1698 } 1699 1700 post_field_modification(thread, method, location, field_klass, object, field, sig_type, value); 1701 1702 // Destroy the JNI handle allocated above. 1703 if (handle_created) { 1704 JNIHandles::destroy_local(value->l); 1705 } 1706 } 1707 1708 void JvmtiExport::post_field_modification(JavaThread *thread, Method* method, 1709 address location, KlassHandle field_klass, Handle object, jfieldID field, 1710 char sig_type, jvalue *value_ptr) { 1711 1712 HandleMark hm(thread); 1713 methodHandle mh(thread, method); 1714 1715 JvmtiThreadState *state = thread->jvmti_thread_state(); 1716 if (state == NULL) { 1717 return; 1718 } 1719 EVT_TRIG_TRACE(JVMTI_EVENT_FIELD_MODIFICATION, 1720 ("JVMTI [%s] Trg Field Modification event triggered", 1721 JvmtiTrace::safe_get_thread_name(thread))); 1722 1723 JvmtiEnvThreadStateIterator it(state); 1724 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 1725 if (ets->is_enabled(JVMTI_EVENT_FIELD_MODIFICATION)) { 1726 EVT_TRACE(JVMTI_EVENT_FIELD_MODIFICATION, 1727 ("JVMTI [%s] Evt Field Modification event sent %s.%s @ %d", 1728 JvmtiTrace::safe_get_thread_name(thread), 1729 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(), 1730 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(), 1731 location - mh()->code_base() )); 1732 1733 JvmtiEnv *env = ets->get_env(); 1734 JvmtiLocationEventMark jem(thread, mh, location); 1735 jclass field_jclass = jem.to_jclass(field_klass()); 1736 jobject field_jobject = jem.to_jobject(object()); 1737 JvmtiJavaThreadEventTransition jet(thread); 1738 jvmtiEventFieldModification callback = env->callbacks()->FieldModification; 1739 if (callback != NULL) { 1740 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), 1741 jem.jni_methodID(), jem.location(), 1742 field_jclass, field_jobject, field, sig_type, *value_ptr); 1743 } 1744 } 1745 } 1746 } 1747 1748 void JvmtiExport::post_native_method_bind(Method* method, address* function_ptr) { 1749 JavaThread* thread = JavaThread::current(); 1750 assert(thread->thread_state() == _thread_in_vm, "must be in vm state"); 1751 1752 HandleMark hm(thread); 1753 methodHandle mh(thread, method); 1754 1755 EVT_TRIG_TRACE(JVMTI_EVENT_NATIVE_METHOD_BIND, ("JVMTI [%s] Trg Native Method Bind event triggered", 1756 JvmtiTrace::safe_get_thread_name(thread))); 1757 1758 if (JvmtiEventController::is_enabled(JVMTI_EVENT_NATIVE_METHOD_BIND)) { 1759 JvmtiEnvIterator it; 1760 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 1761 if (env->is_enabled(JVMTI_EVENT_NATIVE_METHOD_BIND)) { 1762 EVT_TRACE(JVMTI_EVENT_NATIVE_METHOD_BIND, ("JVMTI [%s] Evt Native Method Bind event sent", 1763 JvmtiTrace::safe_get_thread_name(thread) )); 1764 1765 JvmtiMethodEventMark jem(thread, mh); 1766 JvmtiJavaThreadEventTransition jet(thread); 1767 JNIEnv* jni_env = JvmtiEnv::get_phase() == JVMTI_PHASE_PRIMORDIAL? NULL : jem.jni_env(); 1768 jvmtiEventNativeMethodBind callback = env->callbacks()->NativeMethodBind; 1769 if (callback != NULL) { 1770 (*callback)(env->jvmti_external(), jni_env, jem.jni_thread(), 1771 jem.jni_methodID(), (void*)(*function_ptr), (void**)function_ptr); 1772 } 1773 } 1774 } 1775 } 1776 } 1777 1778 // Returns a record containing inlining information for the given nmethod 1779 jvmtiCompiledMethodLoadInlineRecord* create_inline_record(nmethod* nm) { 1780 jint numstackframes = 0; 1781 jvmtiCompiledMethodLoadInlineRecord* record = (jvmtiCompiledMethodLoadInlineRecord*)NEW_RESOURCE_OBJ(jvmtiCompiledMethodLoadInlineRecord); 1782 record->header.kind = JVMTI_CMLR_INLINE_INFO; 1783 record->header.next = NULL; 1784 record->header.majorinfoversion = JVMTI_CMLR_MAJOR_VERSION_1; 1785 record->header.minorinfoversion = JVMTI_CMLR_MINOR_VERSION_0; 1786 record->numpcs = 0; 1787 for(PcDesc* p = nm->scopes_pcs_begin(); p < nm->scopes_pcs_end(); p++) { 1788 if(p->scope_decode_offset() == DebugInformationRecorder::serialized_null) continue; 1789 record->numpcs++; 1790 } 1791 record->pcinfo = (PCStackInfo*)(NEW_RESOURCE_ARRAY(PCStackInfo, record->numpcs)); 1792 int scope = 0; 1793 for(PcDesc* p = nm->scopes_pcs_begin(); p < nm->scopes_pcs_end(); p++) { 1794 if(p->scope_decode_offset() == DebugInformationRecorder::serialized_null) continue; 1795 void* pc_address = (void*)p->real_pc(nm); 1796 assert(pc_address != NULL, "pc_address must be non-null"); 1797 record->pcinfo[scope].pc = pc_address; 1798 numstackframes=0; 1799 for(ScopeDesc* sd = nm->scope_desc_at(p->real_pc(nm));sd != NULL;sd = sd->sender()) { 1800 numstackframes++; 1801 } 1802 assert(numstackframes != 0, "numstackframes must be nonzero."); 1803 record->pcinfo[scope].methods = (jmethodID *)NEW_RESOURCE_ARRAY(jmethodID, numstackframes); 1804 record->pcinfo[scope].bcis = (jint *)NEW_RESOURCE_ARRAY(jint, numstackframes); 1805 record->pcinfo[scope].numstackframes = numstackframes; 1806 int stackframe = 0; 1807 for(ScopeDesc* sd = nm->scope_desc_at(p->real_pc(nm));sd != NULL;sd = sd->sender()) { 1808 // 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() 1809 assert(sd->method() != NULL, "sd->method() cannot be null."); 1810 record->pcinfo[scope].methods[stackframe] = sd->method()->jmethod_id(); 1811 record->pcinfo[scope].bcis[stackframe] = sd->bci(); 1812 stackframe++; 1813 } 1814 scope++; 1815 } 1816 return record; 1817 } 1818 1819 void JvmtiExport::post_compiled_method_load(nmethod *nm) { 1820 JavaThread* thread = JavaThread::current(); 1821 1822 EVT_TRIG_TRACE(JVMTI_EVENT_COMPILED_METHOD_LOAD, 1823 ("JVMTI [%s] method compile load event triggered", 1824 JvmtiTrace::safe_get_thread_name(thread))); 1825 1826 JvmtiEnvIterator it; 1827 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 1828 if (env->is_enabled(JVMTI_EVENT_COMPILED_METHOD_LOAD)) { 1829 1830 EVT_TRACE(JVMTI_EVENT_COMPILED_METHOD_LOAD, 1831 ("JVMTI [%s] class compile method load event sent %s.%s ", 1832 JvmtiTrace::safe_get_thread_name(thread), 1833 (nm->method() == NULL) ? "NULL" : nm->method()->klass_name()->as_C_string(), 1834 (nm->method() == NULL) ? "NULL" : nm->method()->name()->as_C_string())); 1835 ResourceMark rm(thread); 1836 HandleMark hm(thread); 1837 1838 // Add inlining information 1839 jvmtiCompiledMethodLoadInlineRecord* inlinerecord = create_inline_record(nm); 1840 // Pass inlining information through the void pointer 1841 JvmtiCompiledMethodLoadEventMark jem(thread, nm, inlinerecord); 1842 JvmtiJavaThreadEventTransition jet(thread); 1843 jvmtiEventCompiledMethodLoad callback = env->callbacks()->CompiledMethodLoad; 1844 if (callback != NULL) { 1845 (*callback)(env->jvmti_external(), jem.jni_methodID(), 1846 jem.code_size(), jem.code_data(), jem.map_length(), 1847 jem.map(), jem.compile_info()); 1848 } 1849 } 1850 } 1851 } 1852 1853 1854 // post a COMPILED_METHOD_LOAD event for a given environment 1855 void JvmtiExport::post_compiled_method_load(JvmtiEnv* env, const jmethodID method, const jint length, 1856 const void *code_begin, const jint map_length, 1857 const jvmtiAddrLocationMap* map) 1858 { 1859 JavaThread* thread = JavaThread::current(); 1860 EVT_TRIG_TRACE(JVMTI_EVENT_COMPILED_METHOD_LOAD, 1861 ("JVMTI [%s] method compile load event triggered (by GenerateEvents)", 1862 JvmtiTrace::safe_get_thread_name(thread))); 1863 if (env->is_enabled(JVMTI_EVENT_COMPILED_METHOD_LOAD)) { 1864 1865 EVT_TRACE(JVMTI_EVENT_COMPILED_METHOD_LOAD, 1866 ("JVMTI [%s] class compile method load event sent (by GenerateEvents), jmethodID=" PTR_FORMAT, 1867 JvmtiTrace::safe_get_thread_name(thread), method)); 1868 1869 JvmtiEventMark jem(thread); 1870 JvmtiJavaThreadEventTransition jet(thread); 1871 jvmtiEventCompiledMethodLoad callback = env->callbacks()->CompiledMethodLoad; 1872 if (callback != NULL) { 1873 (*callback)(env->jvmti_external(), method, 1874 length, code_begin, map_length, 1875 map, NULL); 1876 } 1877 } 1878 } 1879 1880 void JvmtiExport::post_dynamic_code_generated_internal(const char *name, const void *code_begin, const void *code_end) { 1881 assert(name != NULL && name[0] != '\0', "sanity check"); 1882 1883 JavaThread* thread = JavaThread::current(); 1884 // In theory everyone coming thru here is in_vm but we need to be certain 1885 // because a callee will do a vm->native transition 1886 ThreadInVMfromUnknown __tiv; 1887 1888 EVT_TRIG_TRACE(JVMTI_EVENT_DYNAMIC_CODE_GENERATED, 1889 ("JVMTI [%s] method dynamic code generated event triggered", 1890 JvmtiTrace::safe_get_thread_name(thread))); 1891 JvmtiEnvIterator it; 1892 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 1893 if (env->is_enabled(JVMTI_EVENT_DYNAMIC_CODE_GENERATED)) { 1894 EVT_TRACE(JVMTI_EVENT_DYNAMIC_CODE_GENERATED, 1895 ("JVMTI [%s] dynamic code generated event sent for %s", 1896 JvmtiTrace::safe_get_thread_name(thread), name)); 1897 JvmtiEventMark jem(thread); 1898 JvmtiJavaThreadEventTransition jet(thread); 1899 jint length = (jint)pointer_delta(code_end, code_begin, sizeof(char)); 1900 jvmtiEventDynamicCodeGenerated callback = env->callbacks()->DynamicCodeGenerated; 1901 if (callback != NULL) { 1902 (*callback)(env->jvmti_external(), name, (void*)code_begin, length); 1903 } 1904 } 1905 } 1906 } 1907 1908 void JvmtiExport::post_dynamic_code_generated(const char *name, const void *code_begin, const void *code_end) { 1909 jvmtiPhase phase = JvmtiEnv::get_phase(); 1910 if (phase == JVMTI_PHASE_PRIMORDIAL || phase == JVMTI_PHASE_START) { 1911 post_dynamic_code_generated_internal(name, code_begin, code_end); 1912 } else { 1913 // It may not be safe to post the event from this thread. Defer all 1914 // postings to the service thread so that it can perform them in a safe 1915 // context and in-order. 1916 MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag); 1917 JvmtiDeferredEvent event = JvmtiDeferredEvent::dynamic_code_generated_event( 1918 name, code_begin, code_end); 1919 JvmtiDeferredEventQueue::enqueue(event); 1920 } 1921 } 1922 1923 1924 // post a DYNAMIC_CODE_GENERATED event for a given environment 1925 // used by GenerateEvents 1926 void JvmtiExport::post_dynamic_code_generated(JvmtiEnv* env, const char *name, 1927 const void *code_begin, const void *code_end) 1928 { 1929 JavaThread* thread = JavaThread::current(); 1930 EVT_TRIG_TRACE(JVMTI_EVENT_DYNAMIC_CODE_GENERATED, 1931 ("JVMTI [%s] dynamic code generated event triggered (by GenerateEvents)", 1932 JvmtiTrace::safe_get_thread_name(thread))); 1933 if (env->is_enabled(JVMTI_EVENT_DYNAMIC_CODE_GENERATED)) { 1934 EVT_TRACE(JVMTI_EVENT_DYNAMIC_CODE_GENERATED, 1935 ("JVMTI [%s] dynamic code generated event sent for %s", 1936 JvmtiTrace::safe_get_thread_name(thread), name)); 1937 JvmtiEventMark jem(thread); 1938 JvmtiJavaThreadEventTransition jet(thread); 1939 jint length = (jint)pointer_delta(code_end, code_begin, sizeof(char)); 1940 jvmtiEventDynamicCodeGenerated callback = env->callbacks()->DynamicCodeGenerated; 1941 if (callback != NULL) { 1942 (*callback)(env->jvmti_external(), name, (void*)code_begin, length); 1943 } 1944 } 1945 } 1946 1947 // post a DynamicCodeGenerated event while holding locks in the VM. 1948 void JvmtiExport::post_dynamic_code_generated_while_holding_locks(const char* name, 1949 address code_begin, address code_end) 1950 { 1951 // register the stub with the current dynamic code event collector 1952 JvmtiThreadState* state = JvmtiThreadState::state_for(JavaThread::current()); 1953 // state can only be NULL if the current thread is exiting which 1954 // should not happen since we're trying to post an event 1955 guarantee(state != NULL, "attempt to register stub via an exiting thread"); 1956 JvmtiDynamicCodeEventCollector* collector = state->get_dynamic_code_event_collector(); 1957 guarantee(collector != NULL, "attempt to register stub without event collector"); 1958 collector->register_stub(name, code_begin, code_end); 1959 } 1960 1961 // Collect all the vm internally allocated objects which are visible to java world 1962 void JvmtiExport::record_vm_internal_object_allocation(oop obj) { 1963 Thread* thread = ThreadLocalStorage::thread(); 1964 if (thread != NULL && thread->is_Java_thread()) { 1965 // Can not take safepoint here. 1966 No_Safepoint_Verifier no_sfpt; 1967 // Can not take safepoint here so can not use state_for to get 1968 // jvmti thread state. 1969 JvmtiThreadState *state = ((JavaThread*)thread)->jvmti_thread_state(); 1970 if (state != NULL ) { 1971 // state is non NULL when VMObjectAllocEventCollector is enabled. 1972 JvmtiVMObjectAllocEventCollector *collector; 1973 collector = state->get_vm_object_alloc_event_collector(); 1974 if (collector != NULL && collector->is_enabled()) { 1975 // Don't record classes as these will be notified via the ClassLoad 1976 // event. 1977 if (obj->klass() != SystemDictionary::Class_klass()) { 1978 collector->record_allocation(obj); 1979 } 1980 } 1981 } 1982 } 1983 } 1984 1985 void JvmtiExport::post_garbage_collection_finish() { 1986 Thread *thread = Thread::current(); // this event is posted from VM-Thread. 1987 EVT_TRIG_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, 1988 ("JVMTI [%s] garbage collection finish event triggered", 1989 JvmtiTrace::safe_get_thread_name(thread))); 1990 JvmtiEnvIterator it; 1991 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 1992 if (env->is_enabled(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH)) { 1993 EVT_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, 1994 ("JVMTI [%s] garbage collection finish event sent ", 1995 JvmtiTrace::safe_get_thread_name(thread))); 1996 JvmtiThreadEventTransition jet(thread); 1997 // JNIEnv is NULL here because this event is posted from VM Thread 1998 jvmtiEventGarbageCollectionFinish callback = env->callbacks()->GarbageCollectionFinish; 1999 if (callback != NULL) { 2000 (*callback)(env->jvmti_external()); 2001 } 2002 } 2003 } 2004 } 2005 2006 void JvmtiExport::post_garbage_collection_start() { 2007 Thread* thread = Thread::current(); // this event is posted from vm-thread. 2008 EVT_TRIG_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_START, 2009 ("JVMTI [%s] garbage collection start event triggered", 2010 JvmtiTrace::safe_get_thread_name(thread))); 2011 JvmtiEnvIterator it; 2012 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 2013 if (env->is_enabled(JVMTI_EVENT_GARBAGE_COLLECTION_START)) { 2014 EVT_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_START, 2015 ("JVMTI [%s] garbage collection start event sent ", 2016 JvmtiTrace::safe_get_thread_name(thread))); 2017 JvmtiThreadEventTransition jet(thread); 2018 // JNIEnv is NULL here because this event is posted from VM Thread 2019 jvmtiEventGarbageCollectionStart callback = env->callbacks()->GarbageCollectionStart; 2020 if (callback != NULL) { 2021 (*callback)(env->jvmti_external()); 2022 } 2023 } 2024 } 2025 } 2026 2027 void JvmtiExport::post_data_dump() { 2028 Thread *thread = Thread::current(); 2029 EVT_TRIG_TRACE(JVMTI_EVENT_DATA_DUMP_REQUEST, 2030 ("JVMTI [%s] data dump request event triggered", 2031 JvmtiTrace::safe_get_thread_name(thread))); 2032 JvmtiEnvIterator it; 2033 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 2034 if (env->is_enabled(JVMTI_EVENT_DATA_DUMP_REQUEST)) { 2035 EVT_TRACE(JVMTI_EVENT_DATA_DUMP_REQUEST, 2036 ("JVMTI [%s] data dump request event sent ", 2037 JvmtiTrace::safe_get_thread_name(thread))); 2038 JvmtiThreadEventTransition jet(thread); 2039 // JNIEnv is NULL here because this event is posted from VM Thread 2040 jvmtiEventDataDumpRequest callback = env->callbacks()->DataDumpRequest; 2041 if (callback != NULL) { 2042 (*callback)(env->jvmti_external()); 2043 } 2044 } 2045 } 2046 } 2047 2048 void JvmtiExport::post_monitor_contended_enter(JavaThread *thread, ObjectMonitor *obj_mntr) { 2049 oop object = (oop)obj_mntr->object(); 2050 if (!ServiceUtil::visible_oop(object)) { 2051 // Ignore monitor contended enter for vm internal object. 2052 return; 2053 } 2054 JvmtiThreadState *state = thread->jvmti_thread_state(); 2055 if (state == NULL) { 2056 return; 2057 } 2058 2059 HandleMark hm(thread); 2060 Handle h(thread, object); 2061 2062 EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTER, 2063 ("JVMTI [%s] montior contended enter event triggered", 2064 JvmtiTrace::safe_get_thread_name(thread))); 2065 2066 JvmtiEnvThreadStateIterator it(state); 2067 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 2068 if (ets->is_enabled(JVMTI_EVENT_MONITOR_CONTENDED_ENTER)) { 2069 EVT_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTER, 2070 ("JVMTI [%s] monitor contended enter event sent", 2071 JvmtiTrace::safe_get_thread_name(thread))); 2072 JvmtiMonitorEventMark jem(thread, h()); 2073 JvmtiEnv *env = ets->get_env(); 2074 JvmtiThreadEventTransition jet(thread); 2075 jvmtiEventMonitorContendedEnter callback = env->callbacks()->MonitorContendedEnter; 2076 if (callback != NULL) { 2077 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_object()); 2078 } 2079 } 2080 } 2081 } 2082 2083 void JvmtiExport::post_monitor_contended_entered(JavaThread *thread, ObjectMonitor *obj_mntr) { 2084 oop object = (oop)obj_mntr->object(); 2085 if (!ServiceUtil::visible_oop(object)) { 2086 // Ignore monitor contended entered for vm internal object. 2087 return; 2088 } 2089 JvmtiThreadState *state = thread->jvmti_thread_state(); 2090 if (state == NULL) { 2091 return; 2092 } 2093 2094 HandleMark hm(thread); 2095 Handle h(thread, object); 2096 2097 EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTERED, 2098 ("JVMTI [%s] montior contended entered event triggered", 2099 JvmtiTrace::safe_get_thread_name(thread))); 2100 2101 JvmtiEnvThreadStateIterator it(state); 2102 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 2103 if (ets->is_enabled(JVMTI_EVENT_MONITOR_CONTENDED_ENTERED)) { 2104 EVT_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTERED, 2105 ("JVMTI [%s] monitor contended enter event sent", 2106 JvmtiTrace::safe_get_thread_name(thread))); 2107 JvmtiMonitorEventMark jem(thread, h()); 2108 JvmtiEnv *env = ets->get_env(); 2109 JvmtiThreadEventTransition jet(thread); 2110 jvmtiEventMonitorContendedEntered callback = env->callbacks()->MonitorContendedEntered; 2111 if (callback != NULL) { 2112 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_object()); 2113 } 2114 } 2115 } 2116 } 2117 2118 void JvmtiExport::post_monitor_wait(JavaThread *thread, oop object, 2119 jlong timeout) { 2120 JvmtiThreadState *state = thread->jvmti_thread_state(); 2121 if (state == NULL) { 2122 return; 2123 } 2124 2125 HandleMark hm(thread); 2126 Handle h(thread, object); 2127 2128 EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_WAIT, 2129 ("JVMTI [%s] montior wait event triggered", 2130 JvmtiTrace::safe_get_thread_name(thread))); 2131 2132 JvmtiEnvThreadStateIterator it(state); 2133 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 2134 if (ets->is_enabled(JVMTI_EVENT_MONITOR_WAIT)) { 2135 EVT_TRACE(JVMTI_EVENT_MONITOR_WAIT, 2136 ("JVMTI [%s] monitor wait event sent ", 2137 JvmtiTrace::safe_get_thread_name(thread))); 2138 JvmtiMonitorEventMark jem(thread, h()); 2139 JvmtiEnv *env = ets->get_env(); 2140 JvmtiThreadEventTransition jet(thread); 2141 jvmtiEventMonitorWait callback = env->callbacks()->MonitorWait; 2142 if (callback != NULL) { 2143 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), 2144 jem.jni_object(), timeout); 2145 } 2146 } 2147 } 2148 } 2149 2150 void JvmtiExport::post_monitor_waited(JavaThread *thread, ObjectMonitor *obj_mntr, jboolean timed_out) { 2151 oop object = (oop)obj_mntr->object(); 2152 if (!ServiceUtil::visible_oop(object)) { 2153 // Ignore monitor waited for vm internal object. 2154 return; 2155 } 2156 JvmtiThreadState *state = thread->jvmti_thread_state(); 2157 if (state == NULL) { 2158 return; 2159 } 2160 2161 HandleMark hm(thread); 2162 Handle h(thread, object); 2163 2164 EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_WAITED, 2165 ("JVMTI [%s] montior waited event triggered", 2166 JvmtiTrace::safe_get_thread_name(thread))); 2167 2168 JvmtiEnvThreadStateIterator it(state); 2169 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 2170 if (ets->is_enabled(JVMTI_EVENT_MONITOR_WAITED)) { 2171 EVT_TRACE(JVMTI_EVENT_MONITOR_WAITED, 2172 ("JVMTI [%s] monitor waited event sent ", 2173 JvmtiTrace::safe_get_thread_name(thread))); 2174 JvmtiMonitorEventMark jem(thread, h()); 2175 JvmtiEnv *env = ets->get_env(); 2176 JvmtiThreadEventTransition jet(thread); 2177 jvmtiEventMonitorWaited callback = env->callbacks()->MonitorWaited; 2178 if (callback != NULL) { 2179 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), 2180 jem.jni_object(), timed_out); 2181 } 2182 } 2183 } 2184 } 2185 2186 2187 void JvmtiExport::post_vm_object_alloc(JavaThread *thread, oop object) { 2188 EVT_TRIG_TRACE(JVMTI_EVENT_VM_OBJECT_ALLOC, ("JVMTI [%s] Trg vm object alloc triggered", 2189 JvmtiTrace::safe_get_thread_name(thread))); 2190 if (object == NULL) { 2191 return; 2192 } 2193 HandleMark hm(thread); 2194 Handle h(thread, object); 2195 JvmtiEnvIterator it; 2196 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) { 2197 if (env->is_enabled(JVMTI_EVENT_VM_OBJECT_ALLOC)) { 2198 EVT_TRACE(JVMTI_EVENT_VM_OBJECT_ALLOC, ("JVMTI [%s] Evt vmobject alloc sent %s", 2199 JvmtiTrace::safe_get_thread_name(thread), 2200 object==NULL? "NULL" : java_lang_Class::as_Klass(object)->external_name())); 2201 2202 JvmtiVMObjectAllocEventMark jem(thread, h()); 2203 JvmtiJavaThreadEventTransition jet(thread); 2204 jvmtiEventVMObjectAlloc callback = env->callbacks()->VMObjectAlloc; 2205 if (callback != NULL) { 2206 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), 2207 jem.jni_jobject(), jem.jni_class(), jem.size()); 2208 } 2209 } 2210 } 2211 } 2212 2213 //////////////////////////////////////////////////////////////////////////////////////////////// 2214 2215 void JvmtiExport::cleanup_thread(JavaThread* thread) { 2216 assert(JavaThread::current() == thread, "thread is not current"); 2217 MutexLocker mu(JvmtiThreadState_lock); 2218 2219 if (thread->jvmti_thread_state() != NULL) { 2220 // This has to happen after the thread state is removed, which is 2221 // why it is not in post_thread_end_event like its complement 2222 // Maybe both these functions should be rolled into the posts? 2223 JvmtiEventController::thread_ended(thread); 2224 } 2225 } 2226 2227 void JvmtiExport::clear_detected_exception(JavaThread* thread) { 2228 assert(JavaThread::current() == thread, "thread is not current"); 2229 2230 JvmtiThreadState* state = thread->jvmti_thread_state(); 2231 if (state != NULL) { 2232 state->clear_exception_detected(); 2233 } 2234 } 2235 2236 void JvmtiExport::oops_do(OopClosure* f) { 2237 JvmtiCurrentBreakpoints::oops_do(f); 2238 JvmtiVMObjectAllocEventCollector::oops_do_for_all_threads(f); 2239 } 2240 2241 size_t JvmtiExport::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f) { 2242 return JvmtiTagMap::weak_oops_do(is_alive, f); 2243 } 2244 2245 void JvmtiExport::gc_epilogue() { 2246 JvmtiCurrentBreakpoints::gc_epilogue(); 2247 } 2248 2249 // Onload raw monitor transition. 2250 void JvmtiExport::transition_pending_onload_raw_monitors() { 2251 JvmtiPendingMonitors::transition_raw_monitors(); 2252 } 2253 2254 //////////////////////////////////////////////////////////////////////////////////////////////// 2255 2256 // type for the Agent_OnAttach entry point 2257 extern "C" { 2258 typedef jint (JNICALL *OnAttachEntry_t)(JavaVM*, char *, void *); 2259 } 2260 2261 jint JvmtiExport::load_agent_library(AttachOperation* op, outputStream* st) { 2262 char ebuf[1024]; 2263 char buffer[JVM_MAXPATHLEN]; 2264 void* library = NULL; 2265 jint result = JNI_ERR; 2266 const char *on_attach_symbols[] = AGENT_ONATTACH_SYMBOLS; 2267 size_t num_symbol_entries = ARRAY_SIZE(on_attach_symbols); 2268 2269 // get agent name and options 2270 const char* agent = op->arg(0); 2271 const char* absParam = op->arg(1); 2272 const char* options = op->arg(2); 2273 2274 // The abs paramter should be "true" or "false" 2275 bool is_absolute_path = (absParam != NULL) && (strcmp(absParam,"true")==0); 2276 2277 // Initially marked as invalid. It will be set to valid if we can find the agent 2278 AgentLibrary *agent_lib = new AgentLibrary(agent, options, is_absolute_path, NULL); 2279 2280 // Check for statically linked in agent. If not found then if the path is 2281 // absolute we attempt to load the library. Otherwise we try to load it 2282 // from the standard dll directory. 2283 2284 if (!os::find_builtin_agent(agent_lib, on_attach_symbols, num_symbol_entries)) { 2285 if (is_absolute_path) { 2286 library = os::dll_load(agent, ebuf, sizeof ebuf); 2287 } else { 2288 // Try to load the agent from the standard dll directory 2289 if (os::dll_build_name(buffer, sizeof(buffer), Arguments::get_dll_dir(), 2290 agent)) { 2291 library = os::dll_load(buffer, ebuf, sizeof ebuf); 2292 } 2293 if (library == NULL) { 2294 // not found - try local path 2295 char ns[1] = {0}; 2296 if (os::dll_build_name(buffer, sizeof(buffer), ns, agent)) { 2297 library = os::dll_load(buffer, ebuf, sizeof ebuf); 2298 } 2299 } 2300 } 2301 if (library != NULL) { 2302 agent_lib->set_os_lib(library); 2303 agent_lib->set_valid(); 2304 } 2305 } 2306 // If the library was loaded then we attempt to invoke the Agent_OnAttach 2307 // function 2308 if (agent_lib->valid()) { 2309 // Lookup the Agent_OnAttach function 2310 OnAttachEntry_t on_attach_entry = NULL; 2311 on_attach_entry = CAST_TO_FN_PTR(OnAttachEntry_t, 2312 os::find_agent_function(agent_lib, false, on_attach_symbols, num_symbol_entries)); 2313 if (on_attach_entry == NULL) { 2314 // Agent_OnAttach missing - unload library 2315 if (!agent_lib->is_static_lib()) { 2316 os::dll_unload(library); 2317 } 2318 delete agent_lib; 2319 } else { 2320 // Invoke the Agent_OnAttach function 2321 JavaThread* THREAD = JavaThread::current(); 2322 { 2323 extern struct JavaVM_ main_vm; 2324 JvmtiThreadEventMark jem(THREAD); 2325 JvmtiJavaThreadEventTransition jet(THREAD); 2326 2327 result = (*on_attach_entry)(&main_vm, (char*)options, NULL); 2328 } 2329 2330 // Agent_OnAttach may have used JNI 2331 if (HAS_PENDING_EXCEPTION) { 2332 CLEAR_PENDING_EXCEPTION; 2333 } 2334 2335 // If OnAttach returns JNI_OK then we add it to the list of 2336 // agent libraries so that we can call Agent_OnUnload later. 2337 if (result == JNI_OK) { 2338 Arguments::add_loaded_agent(agent_lib); 2339 } else { 2340 delete agent_lib; 2341 } 2342 2343 // Agent_OnAttach executed so completion status is JNI_OK 2344 st->print_cr("%d", result); 2345 result = JNI_OK; 2346 } 2347 } 2348 return result; 2349 } 2350 2351 //////////////////////////////////////////////////////////////////////////////////////////////// 2352 2353 // Setup current current thread for event collection. 2354 void JvmtiEventCollector::setup_jvmti_thread_state() { 2355 // set this event collector to be the current one. 2356 JvmtiThreadState* state = JvmtiThreadState::state_for(JavaThread::current()); 2357 // state can only be NULL if the current thread is exiting which 2358 // should not happen since we're trying to configure for event collection 2359 guarantee(state != NULL, "exiting thread called setup_jvmti_thread_state"); 2360 if (is_vm_object_alloc_event()) { 2361 _prev = state->get_vm_object_alloc_event_collector(); 2362 state->set_vm_object_alloc_event_collector((JvmtiVMObjectAllocEventCollector *)this); 2363 } else if (is_dynamic_code_event()) { 2364 _prev = state->get_dynamic_code_event_collector(); 2365 state->set_dynamic_code_event_collector((JvmtiDynamicCodeEventCollector *)this); 2366 } 2367 } 2368 2369 // Unset current event collection in this thread and reset it with previous 2370 // collector. 2371 void JvmtiEventCollector::unset_jvmti_thread_state() { 2372 JvmtiThreadState* state = JavaThread::current()->jvmti_thread_state(); 2373 if (state != NULL) { 2374 // restore the previous event collector (if any) 2375 if (is_vm_object_alloc_event()) { 2376 if (state->get_vm_object_alloc_event_collector() == this) { 2377 state->set_vm_object_alloc_event_collector((JvmtiVMObjectAllocEventCollector *)_prev); 2378 } else { 2379 // this thread's jvmti state was created during the scope of 2380 // the event collector. 2381 } 2382 } else { 2383 if (is_dynamic_code_event()) { 2384 if (state->get_dynamic_code_event_collector() == this) { 2385 state->set_dynamic_code_event_collector((JvmtiDynamicCodeEventCollector *)_prev); 2386 } else { 2387 // this thread's jvmti state was created during the scope of 2388 // the event collector. 2389 } 2390 } 2391 } 2392 } 2393 } 2394 2395 // create the dynamic code event collector 2396 JvmtiDynamicCodeEventCollector::JvmtiDynamicCodeEventCollector() : _code_blobs(NULL) { 2397 if (JvmtiExport::should_post_dynamic_code_generated()) { 2398 setup_jvmti_thread_state(); 2399 } 2400 } 2401 2402 // iterate over any code blob descriptors collected and post a 2403 // DYNAMIC_CODE_GENERATED event to the profiler. 2404 JvmtiDynamicCodeEventCollector::~JvmtiDynamicCodeEventCollector() { 2405 assert(!JavaThread::current()->owns_locks(), "all locks must be released to post deferred events"); 2406 // iterate over any code blob descriptors that we collected 2407 if (_code_blobs != NULL) { 2408 for (int i=0; i<_code_blobs->length(); i++) { 2409 JvmtiCodeBlobDesc* blob = _code_blobs->at(i); 2410 JvmtiExport::post_dynamic_code_generated(blob->name(), blob->code_begin(), blob->code_end()); 2411 FreeHeap(blob); 2412 } 2413 delete _code_blobs; 2414 } 2415 unset_jvmti_thread_state(); 2416 } 2417 2418 // register a stub 2419 void JvmtiDynamicCodeEventCollector::register_stub(const char* name, address start, address end) { 2420 if (_code_blobs == NULL) { 2421 _code_blobs = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JvmtiCodeBlobDesc*>(1,true); 2422 } 2423 _code_blobs->append(new JvmtiCodeBlobDesc(name, start, end)); 2424 } 2425 2426 // Setup current thread to record vm allocated objects. 2427 JvmtiVMObjectAllocEventCollector::JvmtiVMObjectAllocEventCollector() : _allocated(NULL) { 2428 if (JvmtiExport::should_post_vm_object_alloc()) { 2429 _enable = true; 2430 setup_jvmti_thread_state(); 2431 } else { 2432 _enable = false; 2433 } 2434 } 2435 2436 // Post vm_object_alloc event for vm allocated objects visible to java 2437 // world. 2438 JvmtiVMObjectAllocEventCollector::~JvmtiVMObjectAllocEventCollector() { 2439 if (_allocated != NULL) { 2440 set_enabled(false); 2441 for (int i = 0; i < _allocated->length(); i++) { 2442 oop obj = _allocated->at(i); 2443 if (ServiceUtil::visible_oop(obj)) { 2444 JvmtiExport::post_vm_object_alloc(JavaThread::current(), obj); 2445 } 2446 } 2447 delete _allocated; 2448 } 2449 unset_jvmti_thread_state(); 2450 } 2451 2452 void JvmtiVMObjectAllocEventCollector::record_allocation(oop obj) { 2453 assert(is_enabled(), "VM object alloc event collector is not enabled"); 2454 if (_allocated == NULL) { 2455 _allocated = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<oop>(1, true); 2456 } 2457 _allocated->push(obj); 2458 } 2459 2460 // GC support. 2461 void JvmtiVMObjectAllocEventCollector::oops_do(OopClosure* f) { 2462 if (_allocated != NULL) { 2463 for(int i=_allocated->length() - 1; i >= 0; i--) { 2464 if (_allocated->at(i) != NULL) { 2465 f->do_oop(_allocated->adr_at(i)); 2466 } 2467 } 2468 } 2469 } 2470 2471 void JvmtiVMObjectAllocEventCollector::oops_do_for_all_threads(OopClosure* f) { 2472 // no-op if jvmti not enabled 2473 if (!JvmtiEnv::environments_might_exist()) { 2474 return; 2475 } 2476 2477 // Runs at safepoint. So no need to acquire Threads_lock. 2478 for (JavaThread *jthr = Threads::first(); jthr != NULL; jthr = jthr->next()) { 2479 JvmtiThreadState *state = jthr->jvmti_thread_state(); 2480 if (state != NULL) { 2481 JvmtiVMObjectAllocEventCollector *collector; 2482 collector = state->get_vm_object_alloc_event_collector(); 2483 while (collector != NULL) { 2484 collector->oops_do(f); 2485 collector = (JvmtiVMObjectAllocEventCollector *)collector->get_prev(); 2486 } 2487 } 2488 } 2489 } 2490 2491 2492 // Disable collection of VMObjectAlloc events 2493 NoJvmtiVMObjectAllocMark::NoJvmtiVMObjectAllocMark() : _collector(NULL) { 2494 // a no-op if VMObjectAlloc event is not enabled 2495 if (!JvmtiExport::should_post_vm_object_alloc()) { 2496 return; 2497 } 2498 Thread* thread = ThreadLocalStorage::thread(); 2499 if (thread != NULL && thread->is_Java_thread()) { 2500 JavaThread* current_thread = (JavaThread*)thread; 2501 JvmtiThreadState *state = current_thread->jvmti_thread_state(); 2502 if (state != NULL) { 2503 JvmtiVMObjectAllocEventCollector *collector; 2504 collector = state->get_vm_object_alloc_event_collector(); 2505 if (collector != NULL && collector->is_enabled()) { 2506 _collector = collector; 2507 _collector->set_enabled(false); 2508 } 2509 } 2510 } 2511 } 2512 2513 // Re-Enable collection of VMObjectAlloc events (if previously enabled) 2514 NoJvmtiVMObjectAllocMark::~NoJvmtiVMObjectAllocMark() { 2515 if (was_enabled()) { 2516 _collector->set_enabled(true); 2517 } 2518 }; 2519 2520 JvmtiGCMarker::JvmtiGCMarker() { 2521 // if there aren't any JVMTI environments then nothing to do 2522 if (!JvmtiEnv::environments_might_exist()) { 2523 return; 2524 } 2525 2526 if (JvmtiExport::should_post_garbage_collection_start()) { 2527 JvmtiExport::post_garbage_collection_start(); 2528 } 2529 2530 if (SafepointSynchronize::is_at_safepoint()) { 2531 // Do clean up tasks that need to be done at a safepoint 2532 JvmtiEnvBase::check_for_periodic_clean_up(); 2533 } 2534 } 2535 2536 JvmtiGCMarker::~JvmtiGCMarker() { 2537 // if there aren't any JVMTI environments then nothing to do 2538 if (!JvmtiEnv::environments_might_exist()) { 2539 return; 2540 } 2541 2542 // JVMTI notify gc finish 2543 if (JvmtiExport::should_post_garbage_collection_finish()) { 2544 JvmtiExport::post_garbage_collection_finish(); 2545 } 2546 }