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