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