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