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