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