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