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