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