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