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