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_get_owned_monitor_info                = false;
1286 
1287 bool              JvmtiExport::_early_vmstart_recorded                    = false;
1288 
1289 bool              JvmtiExport::_should_post_single_step                   = false;
1290 bool              JvmtiExport::_should_post_field_access                  = false;
1291 bool              JvmtiExport::_should_post_field_modification            = false;
1292 bool              JvmtiExport::_should_post_class_load                    = false;
1293 bool              JvmtiExport::_should_post_class_prepare                 = false;
1294 bool              JvmtiExport::_should_post_class_unload                  = false;
1295 bool              JvmtiExport::_should_post_thread_life                   = false;
1296 bool              JvmtiExport::_should_clean_up_heap_objects              = false;
1297 bool              JvmtiExport::_should_post_native_method_bind            = false;
1298 bool              JvmtiExport::_should_post_dynamic_code_generated        = false;
1299 bool              JvmtiExport::_should_post_data_dump                     = false;
1300 bool              JvmtiExport::_should_post_compiled_method_load          = false;
1301 bool              JvmtiExport::_should_post_compiled_method_unload        = false;
1302 bool              JvmtiExport::_should_post_monitor_contended_enter       = false;
1303 bool              JvmtiExport::_should_post_monitor_contended_entered     = false;
1304 bool              JvmtiExport::_should_post_monitor_wait                  = false;
1305 bool              JvmtiExport::_should_post_monitor_waited                = false;
1306 bool              JvmtiExport::_should_post_garbage_collection_start      = false;
1307 bool              JvmtiExport::_should_post_garbage_collection_finish     = false;
1308 bool              JvmtiExport::_should_post_object_free                   = false;
1309 bool              JvmtiExport::_should_post_resource_exhausted            = false;
1310 bool              JvmtiExport::_should_post_vm_object_alloc               = false;
1311 bool              JvmtiExport::_should_post_sampled_object_alloc          = false;
1312 bool              JvmtiExport::_should_post_on_exceptions                 = false;
1313 bool              JvmtiExport::_should_post_vthread_start                 = false;
1314 bool              JvmtiExport::_should_post_vthread_end                   = false;
1315 bool              JvmtiExport::_should_post_vthread_mount                 = false;
1316 bool              JvmtiExport::_should_post_vthread_unmount               = false;
1317 
1318 ////////////////////////////////////////////////////////////////////////////////////////////////
1319 
1320 
1321 //
1322 // JVMTI single step management
1323 //
1324 void JvmtiExport::at_single_stepping_point(JavaThread *thread, Method* method, address location) {
1325   assert(JvmtiExport::should_post_single_step(), "must be single stepping");
1326 
1327   HandleMark hm(thread);
1328   methodHandle mh(thread, method);
1329 
1330   // update information about current location and post a step event
1331   JvmtiThreadState *state = get_jvmti_thread_state(thread);
1332   if (state == nullptr) {
1333     return;
1334   }
1335   EVT_TRIG_TRACE(JVMTI_EVENT_SINGLE_STEP, ("[%s] Trg Single Step triggered",
1336                       JvmtiTrace::safe_get_thread_name(thread)));
1337   if (!state->hide_single_stepping()) {
1338     if (state->is_pending_step_for_popframe()) {
1339       state->process_pending_step_for_popframe();
1340     }
1341     if (state->is_pending_step_for_earlyret()) {
1342       state->process_pending_step_for_earlyret();
1343     }
1344     JvmtiExport::post_single_step(thread, mh(), location);
1345   }
1346 }
1347 
1348 
1349 void JvmtiExport::expose_single_stepping(JvmtiThreadState* state) {
1350   assert(state != nullptr, "must be non-null");
1351   state->clear_hide_single_stepping();
1352 }
1353 
1354 
1355 JvmtiThreadState* JvmtiExport::hide_single_stepping(JavaThread *thread) {
1356   JvmtiThreadState *state = get_jvmti_thread_state(thread);
1357   if (state != nullptr && state->is_enabled(JVMTI_EVENT_SINGLE_STEP)) {
1358     state->set_hide_single_stepping();
1359     return state;
1360   } else {
1361     return nullptr;
1362   }
1363 }
1364 
1365 void JvmtiExport::post_class_load(JavaThread *thread, Klass* klass) {
1366   if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) {
1367     return;
1368   }
1369   HandleMark hm(thread);
1370 
1371   JvmtiThreadState *state = get_jvmti_thread_state(thread);
1372   if (state == nullptr) {
1373     return;
1374   }
1375   if (thread->should_hide_jvmti_events()) {
1376     // All events can be disabled if current thread is doing a Java upcall originated by JVMTI.
1377     // ClassLoad events are important for JDWP agent but not expected during such upcalls.
1378     // Catch if this invariant is broken.
1379     assert(!thread->is_in_java_upcall(), "unexpected ClassLoad event during JVMTI upcall");
1380     return;
1381   }
1382 
1383   EVT_TRIG_TRACE(JVMTI_EVENT_CLASS_LOAD, ("[%s] Trg Class Load triggered",
1384                       JvmtiTrace::safe_get_thread_name(thread)));
1385   JvmtiEnvThreadStateIterator it(state);
1386   for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
1387     if (ets->is_enabled(JVMTI_EVENT_CLASS_LOAD)) {
1388       JvmtiEnv *env = ets->get_env();
1389       if (env->phase() == JVMTI_PHASE_PRIMORDIAL) {
1390         continue;
1391       }
1392       EVT_TRACE(JVMTI_EVENT_CLASS_LOAD, ("[%s] Evt Class Load sent %s",
1393                                          JvmtiTrace::safe_get_thread_name(thread),
1394                                          klass==nullptr? "null" : klass->external_name() ));
1395       JvmtiClassEventMark jem(thread, klass);
1396       JVMTI_JAVA_THREAD_EVENT_CALLBACK_BLOCK(thread)
1397       jvmtiEventClassLoad callback = env->callbacks()->ClassLoad;
1398       if (callback != nullptr) {
1399         (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_class());
1400       }
1401     }
1402   }
1403 }
1404 
1405 
1406 void JvmtiExport::post_class_prepare(JavaThread *thread, Klass* klass) {
1407   if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) {
1408     return;
1409   }
1410   HandleMark hm(thread);
1411 
1412   JvmtiThreadState *state = get_jvmti_thread_state(thread);
1413   if (state == nullptr) {
1414     return;
1415   }
1416   if (thread->should_hide_jvmti_events()) {
1417     // All events can be disabled if current thread is doing a Java upcall originated by JVMTI.
1418     // ClassPrepare events are important for JDWP agent but not expected during such upcalls.
1419     // Catch if this invariant is broken.
1420     assert(!thread->is_in_java_upcall(), "unexpected ClassPrepare event during JVMTI upcall");
1421     return;
1422   }
1423 
1424   EVT_TRIG_TRACE(JVMTI_EVENT_CLASS_PREPARE, ("[%s] Trg Class Prepare triggered",
1425                       JvmtiTrace::safe_get_thread_name(thread)));
1426   JvmtiEnvThreadStateIterator it(state);
1427   for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
1428     if (ets->is_enabled(JVMTI_EVENT_CLASS_PREPARE)) {
1429       JvmtiEnv *env = ets->get_env();
1430       if (env->phase() == JVMTI_PHASE_PRIMORDIAL) {
1431         continue;
1432       }
1433       EVT_TRACE(JVMTI_EVENT_CLASS_PREPARE, ("[%s] Evt Class Prepare sent %s",
1434                                             JvmtiTrace::safe_get_thread_name(thread),
1435                                             klass==nullptr? "null" : klass->external_name() ));
1436       JvmtiClassEventMark jem(thread, klass);
1437       JVMTI_JAVA_THREAD_EVENT_CALLBACK_BLOCK(thread)
1438       jvmtiEventClassPrepare callback = env->callbacks()->ClassPrepare;
1439       if (callback != nullptr) {
1440         (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_class());
1441       }
1442     }
1443   }
1444 }
1445 
1446 void JvmtiExport::post_class_unload(Klass* klass) {
1447   if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) {
1448     return;
1449   }
1450 
1451   // postings to the service thread so that it can perform them in a safe
1452   // context and in-order.
1453   ResourceMark rm;
1454   // JvmtiDeferredEvent copies the string.
1455   JvmtiDeferredEvent event = JvmtiDeferredEvent::class_unload_event(klass->name()->as_C_string());
1456   ServiceThread::enqueue_deferred_event(&event);
1457 }
1458 
1459 
1460 void JvmtiExport::post_class_unload_internal(const char* name) {
1461   if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) {
1462     return;
1463   }
1464   assert(Thread::current()->is_service_thread(), "must be called from ServiceThread");
1465   JavaThread *thread = JavaThread::current();
1466   HandleMark hm(thread);
1467 
1468   EVT_TRIG_TRACE(EXT_EVENT_CLASS_UNLOAD, ("[?] Trg Class Unload triggered" ));
1469   if (JvmtiEventController::is_enabled((jvmtiEvent)EXT_EVENT_CLASS_UNLOAD)) {
1470 
1471     JvmtiEnvIterator it;
1472     for (JvmtiEnv* env = it.first(); env != nullptr; env = it.next(env)) {
1473       if (env->phase() == JVMTI_PHASE_PRIMORDIAL) {
1474         continue;
1475       }
1476       if (env->is_enabled((jvmtiEvent)EXT_EVENT_CLASS_UNLOAD)) {
1477         EVT_TRACE(EXT_EVENT_CLASS_UNLOAD, ("[?] Evt Class Unload sent %s", name));
1478 
1479         JvmtiEventMark jem(thread);
1480         JVMTI_JAVA_THREAD_EVENT_CALLBACK_BLOCK(thread)
1481         jvmtiExtensionEvent callback = env->ext_callbacks()->ClassUnload;
1482         if (callback != nullptr) {
1483           (*callback)(env->jvmti_external(), jem.jni_env(), name);
1484         }
1485       }
1486     }
1487   }
1488 }
1489 
1490 
1491 void JvmtiExport::post_thread_start(JavaThread *thread) {
1492   if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) {
1493     return;
1494   }
1495   assert(thread->thread_state() == _thread_in_vm, "must be in vm state");
1496 
1497   if (thread->is_aot_thread()) {
1498     // The AOT thread is hidden from view but has no thread oop when it starts due
1499     // to bootstrapping complexity, so we check for it before checking for bound
1500     // virtual threads. When exiting it is filtered out due to being hidden.
1501     return;
1502   }
1503 
1504   EVT_TRIG_TRACE(JVMTI_EVENT_THREAD_START, ("[%s] Trg Thread Start event triggered",
1505                       JvmtiTrace::safe_get_thread_name(thread)));
1506 
1507   // do JVMTI thread initialization (if needed)
1508   JvmtiEventController::thread_started(thread);
1509 
1510   if (thread->threadObj()->is_a(vmClasses::BoundVirtualThread_klass())) {
1511     if (JvmtiExport::can_support_virtual_threads()) {
1512       // Check for VirtualThreadStart event instead.
1513       HandleMark hm(thread);
1514       Handle vthread(thread, thread->threadObj());
1515       JvmtiExport::post_vthread_start((jthread)vthread.raw_value());
1516     }
1517     return;
1518   }
1519 
1520   // Do not post thread start event for hidden java thread.
1521   if (JvmtiEventController::is_enabled(JVMTI_EVENT_THREAD_START) &&
1522       !thread->is_hidden_from_external_view()) {
1523     JvmtiEnvIterator it;
1524     for (JvmtiEnv* env = it.first(); env != nullptr; env = it.next(env)) {
1525       if (env->phase() == JVMTI_PHASE_PRIMORDIAL) {
1526         continue;
1527       }
1528       if (env->is_enabled(JVMTI_EVENT_THREAD_START)) {
1529         EVT_TRACE(JVMTI_EVENT_THREAD_START, ("[%s] Evt Thread Start event sent",
1530                      JvmtiTrace::safe_get_thread_name(thread) ));
1531 
1532         JvmtiVirtualThreadEventMark jem(thread);
1533         JVMTI_JAVA_THREAD_EVENT_CALLBACK_BLOCK(thread)
1534         jvmtiEventThreadStart callback = env->callbacks()->ThreadStart;
1535         if (callback != nullptr) {
1536           (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread());
1537         }
1538       }
1539     }
1540   }
1541 }
1542 
1543 
1544 void JvmtiExport::post_thread_end(JavaThread *thread) {
1545   if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) {
1546     return;
1547   }
1548   EVT_TRIG_TRACE(JVMTI_EVENT_THREAD_END, ("[%s] Trg Thread End event triggered",
1549                       JvmtiTrace::safe_get_thread_name(thread)));
1550 
1551   JvmtiThreadState *state = get_jvmti_thread_state(thread);
1552   if (state == nullptr) {
1553     return;
1554   }
1555 
1556   if (thread->threadObj()->is_a(vmClasses::BoundVirtualThread_klass())) {
1557     if (JvmtiExport::can_support_virtual_threads()) {
1558       // Check for VirtualThreadEnd event instead.
1559       HandleMark hm(thread);
1560       Handle vthread(thread, thread->threadObj());
1561       JvmtiExport::post_vthread_end((jthread)vthread.raw_value());
1562     }
1563     return;
1564   }
1565 
1566   // Do not post thread end event for hidden java thread.
1567   if (state->is_enabled(JVMTI_EVENT_THREAD_END) &&
1568       !thread->is_hidden_from_external_view()) {
1569 
1570     JvmtiEnvThreadStateIterator it(state);
1571     for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
1572       JvmtiEnv *env = ets->get_env();
1573       if (env->phase() == JVMTI_PHASE_PRIMORDIAL) {
1574         continue;
1575       }
1576       if (ets->is_enabled(JVMTI_EVENT_THREAD_END)) {
1577         EVT_TRACE(JVMTI_EVENT_THREAD_END, ("[%s] Evt Thread End event sent",
1578                      JvmtiTrace::safe_get_thread_name(thread) ));
1579 
1580         JvmtiVirtualThreadEventMark jem(thread);
1581         JVMTI_JAVA_THREAD_EVENT_CALLBACK_BLOCK(thread)
1582         jvmtiEventThreadEnd callback = env->callbacks()->ThreadEnd;
1583         if (callback != nullptr) {
1584           (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread());
1585         }
1586       }
1587     }
1588   }
1589 }
1590 
1591 
1592 void JvmtiExport::post_vthread_start(jobject vthread) {
1593   if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) {
1594     return;
1595   }
1596   EVT_TRIG_TRACE(JVMTI_EVENT_VIRTUAL_THREAD_START, ("[%p] Trg Virtual Thread Start event triggered", vthread));
1597 
1598   JavaThread *thread = JavaThread::current();
1599   assert(!thread->is_hidden_from_external_view(), "carrier threads can't be hidden");
1600 
1601   if (JvmtiEventController::is_enabled(JVMTI_EVENT_VIRTUAL_THREAD_START)) {
1602     JvmtiEnvIterator it;
1603     for (JvmtiEnv* env = it.first(); env != nullptr; env = it.next(env)) {
1604       if (env->phase() == JVMTI_PHASE_PRIMORDIAL) {
1605         continue;
1606       }
1607       if (env->is_enabled(JVMTI_EVENT_VIRTUAL_THREAD_START)) {
1608         EVT_TRACE(JVMTI_EVENT_VIRTUAL_THREAD_START, ("[%p] Evt Virtual Thread Start event sent", vthread));
1609 
1610         JvmtiVirtualThreadEventMark jem(thread);
1611         JVMTI_JAVA_THREAD_EVENT_CALLBACK_BLOCK(thread)
1612         jvmtiEventVirtualThreadStart callback = env->callbacks()->VirtualThreadStart;
1613         if (callback != nullptr) {
1614           (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread());
1615         }
1616       }
1617     }
1618   }
1619 }
1620 
1621 void JvmtiExport::post_vthread_end(jobject vthread) {
1622   if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) {
1623     return;
1624   }
1625   EVT_TRIG_TRACE(JVMTI_EVENT_VIRTUAL_THREAD_END, ("[%p] Trg Virtual Thread End event triggered", vthread));
1626 
1627   JavaThread *thread = JavaThread::current();
1628   assert(!thread->is_hidden_from_external_view(), "carrier threads can't be hidden");
1629 
1630   JvmtiThreadState *state = get_jvmti_thread_state(thread);
1631   if (state == nullptr) {
1632     return;
1633   }
1634 
1635   if (state->is_enabled(JVMTI_EVENT_VIRTUAL_THREAD_END)) {
1636     JvmtiEnvThreadStateIterator it(state);
1637 
1638     for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
1639       JvmtiEnv *env = ets->get_env();
1640       if (env->phase() == JVMTI_PHASE_PRIMORDIAL) {
1641         continue;
1642       }
1643       if (ets->is_enabled(JVMTI_EVENT_VIRTUAL_THREAD_END)) {
1644         EVT_TRACE(JVMTI_EVENT_VIRTUAL_THREAD_END, ("[%p] Evt Virtual Thread End event sent", vthread));
1645 
1646         JvmtiVirtualThreadEventMark jem(thread);
1647         JVMTI_JAVA_THREAD_EVENT_CALLBACK_BLOCK(thread)
1648         jvmtiEventVirtualThreadEnd callback = env->callbacks()->VirtualThreadEnd;
1649         if (callback != nullptr) {
1650           (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread());
1651         }
1652       }
1653     }
1654   }
1655 }
1656 
1657 void JvmtiExport::post_vthread_mount(jobject vthread) {
1658   if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) {
1659     return;
1660   }
1661   JavaThread *thread = JavaThread::current();
1662   HandleMark hm(thread);
1663   EVT_TRIG_TRACE(EXT_EVENT_VIRTUAL_THREAD_MOUNT, ("[%p] Trg Virtual Thread Mount event triggered", vthread));
1664 
1665   JvmtiThreadState *state = get_jvmti_thread_state(thread);
1666   if (state == nullptr) {
1667     return;
1668   }
1669 
1670   if (state->is_enabled((jvmtiEvent)EXT_EVENT_VIRTUAL_THREAD_MOUNT)) {
1671     JvmtiEnvThreadStateIterator it(state);
1672 
1673     for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
1674       JvmtiEnv *env = ets->get_env();
1675       if (env->phase() == JVMTI_PHASE_PRIMORDIAL) {
1676         continue;
1677       }
1678       if (ets->is_enabled((jvmtiEvent)EXT_EVENT_VIRTUAL_THREAD_MOUNT)) {
1679         EVT_TRACE(EXT_EVENT_VIRTUAL_THREAD_MOUNT, ("[%p] Evt Virtual Thread Mount event sent", vthread));
1680 
1681         JvmtiVirtualThreadEventMark jem(thread);
1682         JVMTI_JAVA_THREAD_EVENT_CALLBACK_BLOCK(thread)
1683         jvmtiExtensionEvent callback = env->ext_callbacks()->VirtualThreadMount;
1684         if (callback != nullptr) {
1685           (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread());
1686         }
1687       }
1688     }
1689   }
1690 }
1691 
1692 void JvmtiExport::post_vthread_unmount(jobject vthread) {
1693   if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) {
1694     return;
1695   }
1696   JavaThread *thread = JavaThread::current();
1697   HandleMark hm(thread);
1698   EVT_TRIG_TRACE(EXT_EVENT_VIRTUAL_THREAD_UNMOUNT, ("[%p] Trg Virtual Thread Unmount event triggered", vthread));
1699 
1700   JvmtiThreadState *state = get_jvmti_thread_state(thread);
1701   if (state == nullptr) {
1702     return;
1703   }
1704 
1705   if (state->is_enabled((jvmtiEvent)EXT_EVENT_VIRTUAL_THREAD_UNMOUNT)) {
1706     JvmtiEnvThreadStateIterator it(state);
1707 
1708     for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
1709       JvmtiEnv *env = ets->get_env();
1710       if (env->phase() == JVMTI_PHASE_PRIMORDIAL) {
1711         continue;
1712       }
1713       if (ets->is_enabled((jvmtiEvent)EXT_EVENT_VIRTUAL_THREAD_UNMOUNT)) {
1714         EVT_TRACE(EXT_EVENT_VIRTUAL_THREAD_UNMOUNT, ("[%p] Evt Virtual Thread Unmount event sent", vthread));
1715 
1716         JvmtiVirtualThreadEventMark jem(thread);
1717         JVMTI_JAVA_THREAD_EVENT_CALLBACK_BLOCK(thread)
1718         jvmtiExtensionEvent callback = env->ext_callbacks()->VirtualThreadUnmount;
1719         if (callback != nullptr) {
1720           (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread());
1721         }
1722       }
1723     }
1724   }
1725 }
1726 
1727 bool JvmtiExport::has_frame_pops(JavaThread* thread) {
1728   if (!can_post_frame_pop()) {
1729     return false;
1730   }
1731   JvmtiThreadState *state = thread->jvmti_thread_state();
1732   if (state == nullptr) {
1733     return false;
1734   }
1735   JvmtiEnvThreadStateIterator it(state);
1736   for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
1737     if (ets->has_frame_pops()) {
1738       return true;
1739     }
1740   }
1741   return false;
1742 }
1743 
1744 void JvmtiExport::continuation_yield_cleanup(JavaThread* thread, jint continuation_frame_count) {
1745   if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) {
1746     return;
1747   }
1748 
1749   assert(thread == JavaThread::current(), "must be");
1750   JvmtiThreadState *state = thread->jvmti_thread_state();
1751   if (state == nullptr) {
1752     return;
1753   }
1754   state->invalidate_cur_stack_depth();
1755 
1756   // Clear frame_pop requests in frames popped by yield
1757   if (can_post_frame_pop()) {
1758     JvmtiEnvThreadStateIterator it(state);
1759     int top_frame_num = state->cur_stack_depth() + continuation_frame_count;
1760 
1761     for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
1762       if (!ets->has_frame_pops()) {
1763         continue;
1764       }
1765       for (int frame_idx = 0; frame_idx < continuation_frame_count; frame_idx++) {
1766         int frame_num = top_frame_num - frame_idx;
1767 
1768         if (!state->is_virtual() && ets->is_frame_pop(frame_num)) {
1769           // remove the frame's entry
1770           MutexLocker mu(JvmtiThreadState_lock);
1771           ets->clear_frame_pop(frame_num);
1772         }
1773       }
1774     }
1775   }
1776 }
1777 
1778 void JvmtiExport::post_object_free(JvmtiEnv* env, GrowableArray<jlong>* objects) {
1779   assert(objects != nullptr, "Nothing to post");
1780 
1781   JavaThread *javaThread = JavaThread::current();
1782   if (javaThread->should_hide_jvmti_events()) {
1783     return;
1784   }
1785   if (!env->is_enabled(JVMTI_EVENT_OBJECT_FREE)) {
1786     return; // the event type has been already disabled
1787   }
1788 
1789   EVT_TRIG_TRACE(JVMTI_EVENT_OBJECT_FREE, ("[?] Trg Object Free triggered" ));
1790   EVT_TRACE(JVMTI_EVENT_OBJECT_FREE, ("[?] Evt Object Free sent"));
1791 
1792   JvmtiThreadEventMark jem(javaThread);
1793   JVMTI_JAVA_THREAD_EVENT_CALLBACK_BLOCK(javaThread)
1794   jvmtiEventObjectFree callback = env->callbacks()->ObjectFree;
1795   if (callback != nullptr) {
1796     for (int index = 0; index < objects->length(); index++) {
1797       (*callback)(env->jvmti_external(), objects->at(index));
1798     }
1799   }
1800 }
1801 
1802 void JvmtiExport::post_resource_exhausted(jint resource_exhausted_flags, const char* description) {
1803 
1804   JavaThread *thread  = JavaThread::current();
1805 
1806   if (thread->should_hide_jvmti_events()) {
1807     return;
1808   }
1809 
1810   log_error(jvmti)("Posting Resource Exhausted event: %s",
1811                    description != nullptr ? description : "unknown");
1812 
1813   // JDK-8213834: handlers of ResourceExhausted may attempt some analysis
1814   // which often requires running java.
1815   // This will cause problems on threads not able to run java, e.g. compiler
1816   // threads. To forestall these problems, we therefore suppress sending this
1817   // event from threads which are not able to run java.
1818   if (!thread->can_call_java()) {
1819     return;
1820   }
1821 
1822   EVT_TRIG_TRACE(JVMTI_EVENT_RESOURCE_EXHAUSTED, ("Trg resource exhausted event triggered" ));
1823 
1824   JvmtiEnvIterator it;
1825   for (JvmtiEnv* env = it.first(); env != nullptr; env = it.next(env)) {
1826     if (env->is_enabled(JVMTI_EVENT_RESOURCE_EXHAUSTED)) {
1827       EVT_TRACE(JVMTI_EVENT_RESOURCE_EXHAUSTED, ("Evt resource exhausted event sent" ));
1828 
1829       JvmtiThreadEventMark jem(thread);
1830       JVMTI_JAVA_THREAD_EVENT_CALLBACK_BLOCK(thread)
1831       jvmtiEventResourceExhausted callback = env->callbacks()->ResourceExhausted;
1832       if (callback != nullptr) {
1833         (*callback)(env->jvmti_external(), jem.jni_env(),
1834                     resource_exhausted_flags, nullptr, description);
1835       }
1836     }
1837   }
1838 }
1839 
1840 void JvmtiExport::post_method_entry(JavaThread *thread, Method* method, frame current_frame) {
1841   HandleMark hm(thread);
1842   methodHandle mh(thread, method);
1843 
1844   JvmtiThreadState *state = get_jvmti_thread_state(thread);
1845   if (state == nullptr || !state->is_interp_only_mode()) {
1846     // for any thread that actually wants method entry, interp_only_mode is set
1847     return;
1848   }
1849   if (mh->jvmti_mount_transition() || thread->should_hide_jvmti_events()) {
1850     return;
1851   }
1852   EVT_TRIG_TRACE(JVMTI_EVENT_METHOD_ENTRY, ("[%s] Trg Method Entry triggered %s.%s",
1853                      JvmtiTrace::safe_get_thread_name(thread),
1854                      (mh() == nullptr) ? "null" : mh()->klass_name()->as_C_string(),
1855                      (mh() == nullptr) ? "null" : mh()->name()->as_C_string() ));
1856 
1857   state->incr_cur_stack_depth();
1858 
1859   if (state->is_enabled(JVMTI_EVENT_METHOD_ENTRY)) {
1860     JvmtiEnvThreadStateIterator it(state);
1861     for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
1862       if (ets->is_enabled(JVMTI_EVENT_METHOD_ENTRY)) {
1863         EVT_TRACE(JVMTI_EVENT_METHOD_ENTRY, ("[%s] Evt Method Entry sent %s.%s",
1864                                              JvmtiTrace::safe_get_thread_name(thread),
1865                                              (mh() == nullptr) ? "null" : mh()->klass_name()->as_C_string(),
1866                                              (mh() == nullptr) ? "null" : mh()->name()->as_C_string() ));
1867 
1868         JvmtiEnv *env = ets->get_env();
1869         JvmtiMethodEventMark jem(thread, mh);
1870         JVMTI_JAVA_THREAD_EVENT_CALLBACK_BLOCK(thread)
1871         jvmtiEventMethodEntry callback = env->callbacks()->MethodEntry;
1872         if (callback != nullptr) {
1873           (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_methodID());
1874         }
1875       }
1876     }
1877   }
1878 }
1879 
1880 void JvmtiExport::post_method_exit(JavaThread* thread, Method* method, frame current_frame) {
1881   // At this point we only have the address of a "raw result" and
1882   // we just call into the interpreter to convert this into a jvalue.
1883   // This method always makes transition to vm and back where GC can happen.
1884   // So it is needed to preserve result and then restore it
1885   // even if events are not actually posted.
1886   // Saving oop_result into value.j is deferred until jvmti state is ready.
1887   HandleMark hm(thread);
1888   methodHandle mh(thread, method);
1889   Handle result;
1890   oop oop_result;
1891   jvalue value;
1892   value.j = 0L;
1893   BasicType type = current_frame.interpreter_frame_result(&oop_result, &value);
1894   assert(mh->is_native() || type == T_VOID || current_frame.interpreter_frame_expression_stack_size() > 0,
1895          "Stack shouldn't be empty");
1896   if (is_reference_type(type)) {
1897     result = Handle(thread, oop_result);
1898   }
1899   JvmtiThreadState* state; // should be initialized in vm state only
1900   JavaThread* current = thread; // for JRT_BLOCK
1901   bool interp_only; // might be changed in JRT_BLOCK_END
1902   JRT_BLOCK
1903     state = get_jvmti_thread_state(thread);
1904     interp_only = state != nullptr && state->is_interp_only_mode();
1905     if (interp_only) {
1906       if (state->is_enabled(JVMTI_EVENT_METHOD_EXIT)) {
1907         // Deferred saving Object result into value.
1908         if (is_reference_type(type)) {
1909           value.l = JNIHandles::make_local(thread, result());
1910         }
1911       }
1912 
1913       // Do not allow NotifyFramePop to add new FramePop event request at
1914       // depth 0 as it is already late in the method exiting dance.
1915       state->set_top_frame_is_exiting();
1916 
1917       post_method_exit_inner(thread, mh, state, false /* not exception exit */, current_frame, value);
1918     }
1919   JRT_BLOCK_END
1920   if (interp_only) {
1921     // The JRT_BLOCK_END can safepoint in ThreadInVMfromJava destructor. Now it is safe to allow
1922     // adding FramePop event requests as no safepoint can happen before removing activation.
1923     state->clr_top_frame_is_exiting();
1924   }
1925   if (result.not_null() && !mh->is_native()) {
1926     // We have to restore the oop on the stack for interpreter frames
1927     *(oop*)current_frame.interpreter_frame_tos_address() = result();
1928   }
1929 }
1930 
1931 void JvmtiExport::post_method_exit_inner(JavaThread* thread,
1932                                          methodHandle& mh,
1933                                          JvmtiThreadState *state,
1934                                          bool exception_exit,
1935                                          frame current_frame,
1936                                          jvalue& value) {
1937   if (mh->jvmti_mount_transition() || thread->should_hide_jvmti_events()) {
1938     return;
1939   }
1940 
1941   EVT_TRIG_TRACE(JVMTI_EVENT_METHOD_EXIT, ("[%s] Trg Method Exit triggered %s.%s",
1942                                            JvmtiTrace::safe_get_thread_name(thread),
1943                                            (mh() == nullptr) ? "null" : mh()->klass_name()->as_C_string(),
1944                                            (mh() == nullptr) ? "null" : mh()->name()->as_C_string() ));
1945 
1946   if (state->is_enabled(JVMTI_EVENT_METHOD_EXIT)) {
1947     JvmtiEnvThreadStateIterator it(state);
1948     for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
1949       if (ets->is_enabled(JVMTI_EVENT_METHOD_EXIT)) {
1950         EVT_TRACE(JVMTI_EVENT_METHOD_EXIT, ("[%s] Evt Method Exit sent %s.%s",
1951                                             JvmtiTrace::safe_get_thread_name(thread),
1952                                             (mh() == nullptr) ? "null" : mh()->klass_name()->as_C_string(),
1953                                             (mh() == nullptr) ? "null" : mh()->name()->as_C_string() ));
1954 
1955         JvmtiEnv *env = ets->get_env();
1956         JvmtiMethodEventMark jem(thread, mh);
1957         JVMTI_JAVA_THREAD_EVENT_CALLBACK_BLOCK(thread)
1958         jvmtiEventMethodExit callback = env->callbacks()->MethodExit;
1959         if (callback != nullptr) {
1960           (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
1961                       jem.jni_methodID(), exception_exit,  value);
1962         }
1963       }
1964     }
1965   }
1966 
1967   JvmtiEnvThreadStateIterator it(state);
1968   for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
1969     if (ets->has_frame_pops()) {
1970       int cur_frame_number = state->cur_stack_depth();
1971 
1972       if (ets->is_frame_pop(cur_frame_number)) {
1973         // we have a NotifyFramePop entry for this frame.
1974         // now check that this env/thread wants this event
1975         if (ets->is_enabled(JVMTI_EVENT_FRAME_POP)) {
1976           EVT_TRACE(JVMTI_EVENT_FRAME_POP, ("[%s] Evt Frame Pop sent %s.%s",
1977                                             JvmtiTrace::safe_get_thread_name(thread),
1978                                             (mh() == nullptr) ? "null" : mh()->klass_name()->as_C_string(),
1979                                             (mh() == nullptr) ? "null" : mh()->name()->as_C_string() ));
1980 
1981           // we also need to issue a frame pop event for this frame
1982           JvmtiEnv *env = ets->get_env();
1983           JvmtiMethodEventMark jem(thread, mh);
1984           JVMTI_JAVA_THREAD_EVENT_CALLBACK_BLOCK(thread)
1985           jvmtiEventFramePop callback = env->callbacks()->FramePop;
1986           if (callback != nullptr) {
1987             (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
1988                         jem.jni_methodID(), exception_exit);
1989           }
1990         }
1991         // remove the frame's entry
1992         {
1993           MutexLocker mu(JvmtiThreadState_lock);
1994           // Need to recheck the condition as the JVMTI ClearAllFramePops can do its work at a safepoint.
1995           if (ets->is_frame_pop(cur_frame_number)) {
1996             ets->clear_frame_pop(cur_frame_number);
1997           }
1998         }
1999       }
2000     }
2001   }
2002 
2003   state->decr_cur_stack_depth();
2004 }
2005 
2006 
2007 // Todo: inline this for optimization
2008 void JvmtiExport::post_single_step(JavaThread *thread, Method* method, address location) {
2009   HandleMark hm(thread);
2010   methodHandle mh(thread, method);
2011 
2012   JvmtiThreadState *state = get_jvmti_thread_state(thread);
2013   if (state == nullptr) {
2014     return;
2015   }
2016   if (mh->jvmti_mount_transition() || thread->should_hide_jvmti_events()) {
2017     return;
2018   }
2019 
2020   JvmtiEnvThreadStateIterator it(state);
2021   for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
2022     ets->compare_and_set_current_location(mh(), location, JVMTI_EVENT_SINGLE_STEP);
2023     if (!ets->single_stepping_posted() && ets->is_enabled(JVMTI_EVENT_SINGLE_STEP)) {
2024       EVT_TRACE(JVMTI_EVENT_SINGLE_STEP, ("[%s] Evt Single Step sent %s.%s @ %zd",
2025                     JvmtiTrace::safe_get_thread_name(thread),
2026                     (mh() == nullptr) ? "null" : mh()->klass_name()->as_C_string(),
2027                     (mh() == nullptr) ? "null" : mh()->name()->as_C_string(),
2028                     location - mh()->code_base() ));
2029 
2030       JvmtiEnv *env = ets->get_env();
2031       JvmtiLocationEventMark jem(thread, mh, location);
2032       JVMTI_JAVA_THREAD_EVENT_CALLBACK_BLOCK(thread)
2033       jvmtiEventSingleStep callback = env->callbacks()->SingleStep;
2034       if (callback != nullptr) {
2035         (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2036                     jem.jni_methodID(), jem.location());
2037       }
2038 
2039       ets->set_single_stepping_posted();
2040     }
2041   }
2042 }
2043 
2044 void JvmtiExport::post_exception_throw(JavaThread *thread, Method* method, address location, oop exception) {
2045   HandleMark hm(thread);
2046   methodHandle mh(thread, method);
2047   Handle exception_handle(thread, exception);
2048   // The KeepStackGCProcessedMark below keeps the target thread and its stack fully
2049   // GC processed across this scope. This is needed because there is a stack walk
2050   // below with safepoint polls inside of it. After such safepoints, we have to
2051   // ensure the stack is sufficiently processed.
2052   KeepStackGCProcessedMark ksgcpm(thread);
2053 
2054   JvmtiThreadState *state = get_jvmti_thread_state(thread);
2055   if (state == nullptr) {
2056     return;
2057   }
2058   if (thread->should_hide_jvmti_events()) {
2059     return;
2060   }
2061 
2062   EVT_TRIG_TRACE(JVMTI_EVENT_EXCEPTION, ("[%s] Trg Exception thrown triggered",
2063                       JvmtiTrace::safe_get_thread_name(thread)));
2064   if (!state->is_exception_detected()) {
2065     state->set_exception_detected();
2066     JvmtiEnvThreadStateIterator it(state);
2067     for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
2068       if (ets->is_enabled(JVMTI_EVENT_EXCEPTION) && (exception != nullptr)) {
2069 
2070         EVT_TRACE(JVMTI_EVENT_EXCEPTION,
2071                      ("[%s] Evt Exception thrown sent %s.%s @ %zd",
2072                       JvmtiTrace::safe_get_thread_name(thread),
2073                       (mh() == nullptr) ? "null" : mh()->klass_name()->as_C_string(),
2074                       (mh() == nullptr) ? "null" : mh()->name()->as_C_string(),
2075                       location - mh()->code_base() ));
2076 
2077         JvmtiEnv *env = ets->get_env();
2078         JvmtiExceptionEventMark jem(thread, mh, location, exception_handle);
2079 
2080         // It's okay to clear these exceptions here because we duplicate
2081         // this lookup in InterpreterRuntime::exception_handler_for_exception.
2082         EXCEPTION_MARK;
2083 
2084         bool should_repeat;
2085         vframeStream st(thread);
2086         assert(!st.at_end(), "cannot be at end");
2087         Method* current_method = nullptr;
2088         // A GC may occur during the Method::fast_exception_handler_bci_for()
2089         // call below if it needs to load the constraint class. Using a
2090         // methodHandle to keep the 'current_method' from being deallocated
2091         // if GC happens.
2092         methodHandle current_mh = methodHandle(thread, current_method);
2093         int current_bci = -1;
2094         do {
2095           current_method = st.method();
2096           current_mh = methodHandle(thread, current_method);
2097           current_bci = st.bci();
2098           do {
2099             should_repeat = false;
2100             Klass* eh_klass = exception_handle()->klass();
2101             current_bci = Method::fast_exception_handler_bci_for(
2102               current_mh, eh_klass, current_bci, THREAD);
2103             if (HAS_PENDING_EXCEPTION) {
2104               exception_handle = Handle(thread, PENDING_EXCEPTION);
2105               CLEAR_PENDING_EXCEPTION;
2106               should_repeat = true;
2107             }
2108           } while (should_repeat && (current_bci != -1));
2109           st.next();
2110         } while ((current_bci < 0) && (!st.at_end()));
2111 
2112         jmethodID catch_jmethodID;
2113         if (current_bci < 0) {
2114           catch_jmethodID = nullptr;
2115           current_bci = 0;
2116         } else {
2117           catch_jmethodID = jem.to_jmethodID(current_mh);
2118         }
2119 
2120         JVMTI_JAVA_THREAD_EVENT_CALLBACK_BLOCK(thread)
2121         jvmtiEventException callback = env->callbacks()->Exception;
2122         if (callback != nullptr) {
2123           (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2124                       jem.jni_methodID(), jem.location(),
2125                       jem.exception(),
2126                       catch_jmethodID, current_bci);
2127         }
2128       }
2129     }
2130   }
2131 
2132   // frames may get popped because of this throw, be safe - invalidate cached depth
2133   state->invalidate_cur_stack_depth();
2134 }
2135 
2136 
2137 void JvmtiExport::notice_unwind_due_to_exception(JavaThread *thread, Method* method, address location, oop exception, bool in_handler_frame) {
2138   HandleMark hm(thread);
2139   methodHandle mh(thread, method);
2140   Handle exception_handle(thread, exception);
2141 
2142   JvmtiThreadState *state = get_jvmti_thread_state(thread);
2143   if (state == nullptr) {
2144     return;
2145   }
2146   EVT_TRIG_TRACE(JVMTI_EVENT_EXCEPTION_CATCH,
2147                     ("[%s] Trg unwind_due_to_exception triggered %s.%s @ %s%zd - %s",
2148                      JvmtiTrace::safe_get_thread_name(thread),
2149                      (mh() == nullptr) ? "null" : mh()->klass_name()->as_C_string(),
2150                      (mh() == nullptr) ? "null" : mh()->name()->as_C_string(),
2151                      location == nullptr ? "no location:" : "",
2152                      location == nullptr ? 0 : location - mh()->code_base(),
2153                      in_handler_frame? "in handler frame" : "not handler frame" ));
2154 
2155   if (state->is_exception_detected()) {
2156 
2157     state->invalidate_cur_stack_depth();
2158     if (!in_handler_frame) {
2159       // Not in exception handler.
2160       if(state->is_interp_only_mode()) {
2161         // method exit and frame pop events are posted only in interp mode.
2162         // When these events are enabled code should be in running in interp mode.
2163         jvalue no_value;
2164         no_value.j = 0L;
2165         JvmtiExport::post_method_exit_inner(thread, mh, state, true, thread->last_frame(), no_value);
2166         // The cached cur_stack_depth might have changed from the
2167         // operations of frame pop or method exit. We are not 100% sure
2168         // the cached cur_stack_depth is still valid depth so invalidate
2169         // it.
2170         state->invalidate_cur_stack_depth();
2171       }
2172     } else {
2173       // In exception handler frame. Report exception catch.
2174       assert(location != nullptr, "must be a known location");
2175       // Update cur_stack_depth - the frames above the current frame
2176       // have been unwound due to this exception:
2177       assert(!state->is_exception_caught(), "exception must not be caught yet.");
2178       state->set_exception_caught();
2179 
2180       if (mh->jvmti_mount_transition() || thread->should_hide_jvmti_events()) {
2181         return;
2182       }
2183       JvmtiEnvThreadStateIterator it(state);
2184       for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
2185         if (ets->is_enabled(JVMTI_EVENT_EXCEPTION_CATCH) && (exception_handle() != nullptr)) {
2186           EVT_TRACE(JVMTI_EVENT_EXCEPTION_CATCH,
2187                      ("[%s] Evt ExceptionCatch sent %s.%s @ %zd",
2188                       JvmtiTrace::safe_get_thread_name(thread),
2189                       (mh() == nullptr) ? "null" : mh()->klass_name()->as_C_string(),
2190                       (mh() == nullptr) ? "null" : mh()->name()->as_C_string(),
2191                       location - mh()->code_base() ));
2192 
2193           JvmtiEnv *env = ets->get_env();
2194           JvmtiExceptionEventMark jem(thread, mh, location, exception_handle);
2195           JVMTI_JAVA_THREAD_EVENT_CALLBACK_BLOCK(thread)
2196           jvmtiEventExceptionCatch callback = env->callbacks()->ExceptionCatch;
2197           if (callback != nullptr) {
2198             (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2199                       jem.jni_methodID(), jem.location(),
2200                       jem.exception());
2201           }
2202         }
2203       }
2204     }
2205   }
2206 }
2207 
2208 oop JvmtiExport::jni_GetField_probe(JavaThread *thread, jobject jobj, oop obj,
2209                                     Klass* klass, jfieldID fieldID, bool is_static) {
2210   if (*((int *)get_field_access_count_addr()) > 0 && thread->has_last_Java_frame()) {
2211     // At least one field access watch is set so we have more work to do.
2212     post_field_access_by_jni(thread, obj, klass, fieldID, is_static);
2213     // event posting can block so refetch oop if we were passed a jobj
2214     if (jobj != nullptr) return JNIHandles::resolve_non_null(jobj);
2215   }
2216   return obj;
2217 }
2218 
2219 void JvmtiExport::post_field_access_by_jni(JavaThread *thread, oop obj,
2220                                            Klass* klass, jfieldID fieldID, bool is_static) {
2221   // We must be called with a Java context in order to provide reasonable
2222   // values for the klazz, method, and location fields. The callers of this
2223   // function don't make the call unless there is a Java context.
2224   // The last java frame might be compiled in 2 cases:
2225   // 1) Field events and interp_only mode are not enabled for this thread.
2226   // This method is called from any thread. The thread filtering is done later.
2227   // 2) The same JNI call is stll executing after event was enabled.
2228   // In this case the last frame is only marked for deoptimization but still remains compiled.
2229   assert(thread->has_last_Java_frame(), "must be called with a Java context");
2230 
2231   if (thread->should_hide_jvmti_events()) {
2232     return;
2233   }
2234 
2235   ResourceMark rm;
2236   fieldDescriptor fd;
2237   // if get_field_descriptor finds fieldID to be invalid, then we just bail
2238   bool valid_fieldID = JvmtiEnv::get_field_descriptor(klass, fieldID, &fd);
2239   assert(valid_fieldID == true,"post_field_access_by_jni called with invalid fieldID");
2240   if (!valid_fieldID) return;
2241   // field accesses are not watched so bail
2242   if (!fd.is_field_access_watched()) return;
2243 
2244   HandleMark hm(thread);
2245   Handle h_obj;
2246   if (!is_static) {
2247     // non-static field accessors have an object, but we need a handle
2248     assert(obj != nullptr, "non-static needs an object");
2249     h_obj = Handle(thread, obj);
2250   }
2251 
2252   RegisterMap reg_map(thread,
2253                       RegisterMap::UpdateMap::skip,
2254                       RegisterMap::ProcessFrames::skip,
2255                       RegisterMap::WalkContinuation::skip);
2256   javaVFrame *jvf = thread->last_java_vframe(&reg_map);
2257   Method* method = jvf->method();
2258   address address = jvf->method()->code_base();
2259 
2260   post_field_access(thread, method, address, klass, h_obj, fieldID);
2261 }
2262 
2263 void JvmtiExport::post_field_access(JavaThread *thread, Method* method,
2264   address location, Klass* field_klass, Handle object, jfieldID field) {
2265 
2266   HandleMark hm(thread);
2267   methodHandle mh(thread, method);
2268 
2269   JvmtiThreadState *state = get_jvmti_thread_state(thread);
2270   if (state == nullptr) {
2271     return;
2272   }
2273   if (thread->should_hide_jvmti_events()) {
2274     return;
2275   }
2276 
2277   EVT_TRIG_TRACE(JVMTI_EVENT_FIELD_ACCESS, ("[%s] Trg Field Access event triggered",
2278                       JvmtiTrace::safe_get_thread_name(thread)));
2279   JvmtiEnvThreadStateIterator it(state);
2280   for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
2281     if (ets->is_enabled(JVMTI_EVENT_FIELD_ACCESS)) {
2282       EVT_TRACE(JVMTI_EVENT_FIELD_ACCESS, ("[%s] Evt Field Access event sent %s.%s @ %zd",
2283                      JvmtiTrace::safe_get_thread_name(thread),
2284                      (mh() == nullptr) ? "null" : mh()->klass_name()->as_C_string(),
2285                      (mh() == nullptr) ? "null" : mh()->name()->as_C_string(),
2286                      location - mh()->code_base() ));
2287 
2288       JvmtiEnv *env = ets->get_env();
2289       JvmtiLocationEventMark jem(thread, mh, location);
2290       jclass field_jclass = jem.to_jclass(field_klass);
2291       jobject field_jobject = jem.to_jobject(object());
2292       JVMTI_JAVA_THREAD_EVENT_CALLBACK_BLOCK(thread)
2293       jvmtiEventFieldAccess callback = env->callbacks()->FieldAccess;
2294       if (callback != nullptr) {
2295         (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2296                     jem.jni_methodID(), jem.location(),
2297                     field_jclass, field_jobject, field);
2298       }
2299     }
2300   }
2301 }
2302 
2303 oop JvmtiExport::jni_SetField_probe(JavaThread *thread, jobject jobj, oop obj,
2304                                     Klass* klass, jfieldID fieldID, bool is_static,
2305                                     char sig_type, jvalue *value) {
2306   if (*((int *)get_field_modification_count_addr()) > 0 && thread->has_last_Java_frame()) {
2307     // At least one field modification watch is set so we have more work to do.
2308     post_field_modification_by_jni(thread, obj, klass, fieldID, is_static, sig_type, value);
2309     // event posting can block so refetch oop if we were passed a jobj
2310     if (jobj != nullptr) return JNIHandles::resolve_non_null(jobj);
2311   }
2312   return obj;
2313 }
2314 
2315 void JvmtiExport::post_field_modification_by_jni(JavaThread *thread, oop obj,
2316                                                  Klass* klass, jfieldID fieldID, bool is_static,
2317                                                  char sig_type, jvalue *value) {
2318   // We must be called with a Java context in order to provide reasonable
2319   // values for the klazz, method, and location fields. The callers of this
2320   // function don't make the call unless there is a Java context.
2321   // The last java frame might be compiled in 2 cases:
2322   // 1) Field events and interp_only mode are not enabled for this thread.
2323   // This method is called from any thread. The thread filtering is done later.
2324   // 2) The same JNI call is stll executing after event was enabled.
2325   // In this case the last frame is only marked for deoptimization but still remains compiled.
2326   assert(thread->has_last_Java_frame(), "must be called with Java context");
2327 
2328   if (thread->should_hide_jvmti_events()) {
2329     return;
2330   }
2331 
2332   ResourceMark rm;
2333   fieldDescriptor fd;
2334   // if get_field_descriptor finds fieldID to be invalid, then we just bail
2335   bool valid_fieldID = JvmtiEnv::get_field_descriptor(klass, fieldID, &fd);
2336   assert(valid_fieldID == true,"post_field_modification_by_jni called with invalid fieldID");
2337   if (!valid_fieldID) return;
2338   // field modifications are not watched so bail
2339   if (!fd.is_field_modification_watched()) return;
2340 
2341   HandleMark hm(thread);
2342 
2343   Handle h_obj;
2344   if (!is_static) {
2345     // non-static field accessors have an object, but we need a handle
2346     assert(obj != nullptr, "non-static needs an object");
2347     h_obj = Handle(thread, obj);
2348   }
2349 
2350   RegisterMap reg_map(thread,
2351                       RegisterMap::UpdateMap::skip,
2352                       RegisterMap::ProcessFrames::skip,
2353                       RegisterMap::WalkContinuation::skip);
2354   javaVFrame *jvf = thread->last_java_vframe(&reg_map);
2355   Method* method = jvf->method();
2356   address address = jvf->method()->code_base();
2357 
2358   post_field_modification(thread, method, address,
2359                           klass, h_obj, fieldID, sig_type, value);
2360 }
2361 
2362 void JvmtiExport::post_raw_field_modification(JavaThread *thread, Method* method,
2363   address location, Klass* field_klass, Handle object, jfieldID field,
2364   char sig_type, jvalue *value) {
2365 
2366   if (thread->should_hide_jvmti_events()) {
2367     return;
2368   }
2369 
2370   if (sig_type == JVM_SIGNATURE_INT || sig_type == JVM_SIGNATURE_BOOLEAN ||
2371       sig_type == JVM_SIGNATURE_BYTE || sig_type == JVM_SIGNATURE_CHAR ||
2372       sig_type == JVM_SIGNATURE_SHORT) {
2373     // 'I' instructions are used for byte, char, short and int.
2374     // determine which it really is, and convert
2375     fieldDescriptor fd;
2376     bool found = JvmtiEnv::get_field_descriptor(field_klass, field, &fd);
2377     // should be found (if not, leave as is)
2378     if (found) {
2379       jint ival = value->i;
2380       // convert value from int to appropriate type
2381       switch (fd.field_type()) {
2382       case T_BOOLEAN:
2383         sig_type = JVM_SIGNATURE_BOOLEAN;
2384         value->i = 0; // clear it
2385         value->z = (jboolean)ival;
2386         break;
2387       case T_BYTE:
2388         sig_type = JVM_SIGNATURE_BYTE;
2389         value->i = 0; // clear it
2390         value->b = (jbyte)ival;
2391         break;
2392       case T_CHAR:
2393         sig_type = JVM_SIGNATURE_CHAR;
2394         value->i = 0; // clear it
2395         value->c = (jchar)ival;
2396         break;
2397       case T_SHORT:
2398         sig_type = JVM_SIGNATURE_SHORT;
2399         value->i = 0; // clear it
2400         value->s = (jshort)ival;
2401         break;
2402       case T_INT:
2403         // nothing to do
2404         break;
2405       default:
2406         // this is an integer instruction, should be one of above
2407         ShouldNotReachHere();
2408         break;
2409       }
2410     }
2411   }
2412 
2413   assert(sig_type != JVM_SIGNATURE_ARRAY, "array should have sig_type == 'L'");
2414   bool handle_created = false;
2415 
2416   // convert oop to JNI handle.
2417   if (sig_type == JVM_SIGNATURE_CLASS) {
2418     handle_created = true;
2419     value->l = (jobject)JNIHandles::make_local(thread, cast_to_oop(value->l));
2420   }
2421 
2422   post_field_modification(thread, method, location, field_klass, object, field, sig_type, value);
2423 
2424   // Destroy the JNI handle allocated above.
2425   if (handle_created) {
2426     JNIHandles::destroy_local(value->l);
2427   }
2428 }
2429 
2430 void JvmtiExport::post_field_modification(JavaThread *thread, Method* method,
2431   address location, Klass* field_klass, Handle object, jfieldID field,
2432   char sig_type, jvalue *value_ptr) {
2433 
2434   HandleMark hm(thread);
2435   methodHandle mh(thread, method);
2436 
2437   JvmtiThreadState *state = get_jvmti_thread_state(thread);
2438   if (state == nullptr) {
2439     return;
2440   }
2441   if (thread->should_hide_jvmti_events()) {
2442     return;
2443   }
2444 
2445   EVT_TRIG_TRACE(JVMTI_EVENT_FIELD_MODIFICATION,
2446                      ("[%s] Trg Field Modification event triggered",
2447                       JvmtiTrace::safe_get_thread_name(thread)));
2448   JvmtiEnvThreadStateIterator it(state);
2449   for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
2450     if (ets->is_enabled(JVMTI_EVENT_FIELD_MODIFICATION)) {
2451       EVT_TRACE(JVMTI_EVENT_FIELD_MODIFICATION,
2452                    ("[%s] Evt Field Modification event sent %s.%s @ %zd",
2453                     JvmtiTrace::safe_get_thread_name(thread),
2454                     (mh() == nullptr) ? "null" : mh()->klass_name()->as_C_string(),
2455                     (mh() == nullptr) ? "null" : mh()->name()->as_C_string(),
2456                     location - mh()->code_base() ));
2457 
2458       JvmtiEnv *env = ets->get_env();
2459       JvmtiLocationEventMark jem(thread, mh, location);
2460       jclass field_jclass = jem.to_jclass(field_klass);
2461       jobject field_jobject = jem.to_jobject(object());
2462       JVMTI_JAVA_THREAD_EVENT_CALLBACK_BLOCK(thread)
2463       jvmtiEventFieldModification callback = env->callbacks()->FieldModification;
2464       if (callback != nullptr) {
2465         (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2466                     jem.jni_methodID(), jem.location(),
2467                     field_jclass, field_jobject, field, sig_type, *value_ptr);
2468       }
2469     }
2470   }
2471 }
2472 
2473 void JvmtiExport::post_native_method_bind(Method* method, address* function_ptr) {
2474   JavaThread* thread = JavaThread::current();
2475   assert(thread->thread_state() == _thread_in_vm, "must be in vm state");
2476 
2477   HandleMark hm(thread);
2478   methodHandle mh(thread, method);
2479 
2480   if (thread->should_hide_jvmti_events()) {
2481     return;
2482   }
2483   EVT_TRIG_TRACE(JVMTI_EVENT_NATIVE_METHOD_BIND, ("[%s] Trg Native Method Bind event triggered",
2484                       JvmtiTrace::safe_get_thread_name(thread)));
2485 
2486   if (JvmtiEventController::is_enabled(JVMTI_EVENT_NATIVE_METHOD_BIND)) {
2487     JvmtiEnvIterator it;
2488     for (JvmtiEnv* env = it.first(); env != nullptr; env = it.next(env)) {
2489       if (env->is_enabled(JVMTI_EVENT_NATIVE_METHOD_BIND)) {
2490         EVT_TRACE(JVMTI_EVENT_NATIVE_METHOD_BIND, ("[%s] Evt Native Method Bind event sent",
2491                      JvmtiTrace::safe_get_thread_name(thread) ));
2492 
2493         JvmtiMethodEventMark jem(thread, mh);
2494         JVMTI_JAVA_THREAD_EVENT_CALLBACK_BLOCK(thread)
2495         JNIEnv* jni_env = (env->phase() == JVMTI_PHASE_PRIMORDIAL) ? nullptr : jem.jni_env();
2496         jvmtiEventNativeMethodBind callback = env->callbacks()->NativeMethodBind;
2497         if (callback != nullptr) {
2498           (*callback)(env->jvmti_external(), jni_env, jem.jni_thread(),
2499                       jem.jni_methodID(), (void*)(*function_ptr), (void**)function_ptr);
2500         }
2501       }
2502     }
2503   }
2504 }
2505 
2506 // Returns a record containing inlining information for the given nmethod
2507 static jvmtiCompiledMethodLoadInlineRecord* create_inline_record(nmethod* nm) {
2508   jint numstackframes = 0;
2509   jvmtiCompiledMethodLoadInlineRecord* record = (jvmtiCompiledMethodLoadInlineRecord*)NEW_RESOURCE_OBJ(jvmtiCompiledMethodLoadInlineRecord);
2510   record->header.kind = JVMTI_CMLR_INLINE_INFO;
2511   record->header.next = nullptr;
2512   record->header.majorinfoversion = JVMTI_CMLR_MAJOR_VERSION_1;
2513   record->header.minorinfoversion = JVMTI_CMLR_MINOR_VERSION_0;
2514   record->numpcs = 0;
2515   for(PcDesc* p = nm->scopes_pcs_begin(); p < nm->scopes_pcs_end(); p++) {
2516    if(p->scope_decode_offset() == DebugInformationRecorder::serialized_null) continue;
2517    record->numpcs++;
2518   }
2519   record->pcinfo = (PCStackInfo*)(NEW_RESOURCE_ARRAY(PCStackInfo, record->numpcs));
2520   int scope = 0;
2521   for(PcDesc* p = nm->scopes_pcs_begin(); p < nm->scopes_pcs_end(); p++) {
2522     if(p->scope_decode_offset() == DebugInformationRecorder::serialized_null) continue;
2523     void* pc_address = (void*)p->real_pc(nm);
2524     assert(pc_address != nullptr, "pc_address must be non-null");
2525     record->pcinfo[scope].pc = pc_address;
2526     numstackframes=0;
2527     for(ScopeDesc* sd = nm->scope_desc_at(p->real_pc(nm));sd != nullptr;sd = sd->sender()) {
2528       numstackframes++;
2529     }
2530     assert(numstackframes != 0, "numstackframes must be nonzero.");
2531     record->pcinfo[scope].methods = (jmethodID *)NEW_RESOURCE_ARRAY(jmethodID, numstackframes);
2532     record->pcinfo[scope].bcis = (jint *)NEW_RESOURCE_ARRAY(jint, numstackframes);
2533     record->pcinfo[scope].numstackframes = numstackframes;
2534     int stackframe = 0;
2535     for(ScopeDesc* sd = nm->scope_desc_at(p->real_pc(nm));sd != nullptr;sd = sd->sender()) {
2536       // 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()
2537       guarantee(sd->method() != nullptr, "sd->method() cannot be null.");
2538       record->pcinfo[scope].methods[stackframe] = sd->method()->jmethod_id();
2539       record->pcinfo[scope].bcis[stackframe] = sd->bci();
2540       stackframe++;
2541     }
2542     scope++;
2543   }
2544   return record;
2545 }
2546 
2547 void JvmtiExport::post_compiled_method_load(nmethod *nm) {
2548   guarantee(!nm->is_unloading(), "nmethod isn't unloaded or unloading");
2549   if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) {
2550     return;
2551   }
2552   JavaThread* thread = JavaThread::current();
2553 
2554   assert(!thread->should_hide_jvmti_events(), "compiled method load events are not allowed in critical sections");
2555 
2556   EVT_TRIG_TRACE(JVMTI_EVENT_COMPILED_METHOD_LOAD,
2557                  ("[%s] method compile load event triggered",
2558                  JvmtiTrace::safe_get_thread_name(thread)));
2559 
2560   JvmtiEnvIterator it;
2561   for (JvmtiEnv* env = it.first(); env != nullptr; env = it.next(env)) {
2562     post_compiled_method_load(env, nm);
2563   }
2564 }
2565 
2566 // post a COMPILED_METHOD_LOAD event for a given environment
2567 void JvmtiExport::post_compiled_method_load(JvmtiEnv* env, nmethod *nm) {
2568   if (env->phase() == JVMTI_PHASE_PRIMORDIAL || !env->is_enabled(JVMTI_EVENT_COMPILED_METHOD_LOAD)) {
2569     return;
2570   }
2571   jvmtiEventCompiledMethodLoad callback = env->callbacks()->CompiledMethodLoad;
2572   if (callback == nullptr) {
2573     return;
2574   }
2575   JavaThread* thread = JavaThread::current();
2576 
2577   assert(!thread->should_hide_jvmti_events(), "compiled method load events are not allowed in critical sections");
2578 
2579   EVT_TRACE(JVMTI_EVENT_COMPILED_METHOD_LOAD,
2580            ("[%s] method compile load event sent %s.%s  ",
2581             JvmtiTrace::safe_get_thread_name(thread),
2582             (nm->method() == nullptr) ? "null" : nm->method()->klass_name()->as_C_string(),
2583             (nm->method() == nullptr) ? "null" : nm->method()->name()->as_C_string()));
2584   ResourceMark rm(thread);
2585   HandleMark hm(thread);
2586 
2587   // Add inlining information
2588   jvmtiCompiledMethodLoadInlineRecord* inlinerecord = create_inline_record(nm);
2589   // Pass inlining information through the void pointer
2590   JvmtiCompiledMethodLoadEventMark jem(thread, nm, inlinerecord);
2591   JVMTI_JAVA_THREAD_EVENT_CALLBACK_BLOCK(thread)
2592   (*callback)(env->jvmti_external(), jem.jni_methodID(),
2593               jem.code_size(), jem.code_data(), jem.map_length(),
2594               jem.map(), jem.compile_info());
2595 }
2596 
2597 void JvmtiExport::post_dynamic_code_generated_internal(const char *name, const void *code_begin, const void *code_end) {
2598   assert(name != nullptr && name[0] != '\0', "sanity check");
2599 
2600   JavaThread* thread = JavaThread::current();
2601 
2602   assert(!thread->should_hide_jvmti_events(), "dynamic code generated events are not allowed in critical sections");
2603 
2604   // In theory everyone coming thru here is in_vm but we need to be certain
2605   // because a callee will do a vm->native transition
2606   ThreadInVMfromUnknown __tiv;
2607 
2608   EVT_TRIG_TRACE(JVMTI_EVENT_DYNAMIC_CODE_GENERATED,
2609                  ("[%s] method dynamic code generated event triggered",
2610                  JvmtiTrace::safe_get_thread_name(thread)));
2611   JvmtiEnvIterator it;
2612   for (JvmtiEnv* env = it.first(); env != nullptr; env = it.next(env)) {
2613     if (env->is_enabled(JVMTI_EVENT_DYNAMIC_CODE_GENERATED)) {
2614       EVT_TRACE(JVMTI_EVENT_DYNAMIC_CODE_GENERATED,
2615                 ("[%s] dynamic code generated event sent for %s",
2616                 JvmtiTrace::safe_get_thread_name(thread), name));
2617       JvmtiEventMark jem(thread);
2618       JVMTI_JAVA_THREAD_EVENT_CALLBACK_BLOCK(thread)
2619       jint length = (jint)pointer_delta(code_end, code_begin, sizeof(char));
2620       jvmtiEventDynamicCodeGenerated callback = env->callbacks()->DynamicCodeGenerated;
2621       if (callback != nullptr) {
2622         (*callback)(env->jvmti_external(), name, (void*)code_begin, length);
2623       }
2624     }
2625   }
2626 }
2627 
2628 void JvmtiExport::post_dynamic_code_generated(const char *name, const void *code_begin, const void *code_end) {
2629   jvmtiPhase phase = JvmtiEnv::get_phase();
2630   if (phase == JVMTI_PHASE_PRIMORDIAL || phase == JVMTI_PHASE_START) {
2631     post_dynamic_code_generated_internal(name, code_begin, code_end);
2632   } else {
2633     // It may not be safe to post the event from this thread.  Defer all
2634     // postings to the service thread so that it can perform them in a safe
2635     // context and in-order.
2636     JvmtiDeferredEvent event = JvmtiDeferredEvent::dynamic_code_generated_event(
2637         name, code_begin, code_end);
2638     ServiceThread::enqueue_deferred_event(&event);
2639   }
2640 }
2641 
2642 
2643 // post a DYNAMIC_CODE_GENERATED event for a given environment
2644 // used by GenerateEvents
2645 void JvmtiExport::post_dynamic_code_generated(JvmtiEnv* env, const char *name,
2646                                               const void *code_begin, const void *code_end)
2647 {
2648   JavaThread* thread = JavaThread::current();
2649 
2650   assert(!thread->should_hide_jvmti_events(), "dynamic code generated events are not allowed in critical sections");
2651 
2652   EVT_TRIG_TRACE(JVMTI_EVENT_DYNAMIC_CODE_GENERATED,
2653                  ("[%s] dynamic code generated event triggered (by GenerateEvents)",
2654                   JvmtiTrace::safe_get_thread_name(thread)));
2655   if (env->is_enabled(JVMTI_EVENT_DYNAMIC_CODE_GENERATED)) {
2656     EVT_TRACE(JVMTI_EVENT_DYNAMIC_CODE_GENERATED,
2657               ("[%s] dynamic code generated event sent for %s",
2658                JvmtiTrace::safe_get_thread_name(thread), name));
2659     JvmtiEventMark jem(thread);
2660     JVMTI_JAVA_THREAD_EVENT_CALLBACK_BLOCK(thread)
2661     jint length = (jint)pointer_delta(code_end, code_begin, sizeof(char));
2662     jvmtiEventDynamicCodeGenerated callback = env->callbacks()->DynamicCodeGenerated;
2663     if (callback != nullptr) {
2664       (*callback)(env->jvmti_external(), name, (void*)code_begin, length);
2665     }
2666   }
2667 }
2668 
2669 // post a DynamicCodeGenerated event while holding locks in the VM.
2670 void JvmtiExport::post_dynamic_code_generated_while_holding_locks(const char* name,
2671                                                                   address code_begin, address code_end)
2672 {
2673   JavaThread* thread = JavaThread::current();
2674   // register the stub with the current dynamic code event collector
2675   // Cannot take safepoint here so do not use state_for to get
2676   // jvmti thread state.
2677   // The collector and/or state might be null if JvmtiDynamicCodeEventCollector
2678   // has been initialized while JVMTI_EVENT_DYNAMIC_CODE_GENERATED was disabled.
2679   JvmtiThreadState *state = get_jvmti_thread_state(thread, false /* allow_suspend */);
2680   if (state != nullptr) {
2681     JvmtiDynamicCodeEventCollector *collector = state->get_dynamic_code_event_collector();
2682     if (collector != nullptr) {
2683       collector->register_stub(name, code_begin, code_end);
2684     }
2685   }
2686 }
2687 
2688 // Collect all the vm internally allocated objects which are visible to java world
2689 void JvmtiExport::record_vm_internal_object_allocation(oop obj) {
2690   Thread* thread = Thread::current_or_null();
2691   if (thread != nullptr && thread->is_Java_thread())  {
2692     // Can not take safepoint here.
2693     NoSafepointVerifier no_sfpt;
2694     // Cannot take safepoint here so do not use state_for to get
2695     // jvmti thread state.
2696     JvmtiThreadState *state = JavaThread::cast(thread)->jvmti_thread_state();
2697     if (state != nullptr) {
2698       // state is non null when VMObjectAllocEventCollector is enabled.
2699       JvmtiVMObjectAllocEventCollector *collector;
2700       collector = state->get_vm_object_alloc_event_collector();
2701       if (collector != nullptr && collector->is_enabled()) {
2702         // Don't record classes as these will be notified via the ClassLoad
2703         // event.
2704         if (obj->klass() != vmClasses::Class_klass()) {
2705           collector->record_allocation(obj);
2706         }
2707       }
2708     }
2709   }
2710 }
2711 
2712 // Collect all the sampled allocated objects.
2713 void JvmtiExport::record_sampled_internal_object_allocation(oop obj) {
2714   Thread* thread = Thread::current_or_null();
2715   if (thread != nullptr && thread->is_Java_thread())  {
2716     // Can not take safepoint here.
2717     NoSafepointVerifier no_sfpt;
2718     // Cannot take safepoint here so do not use state_for to get
2719     // jvmti thread state.
2720     JvmtiThreadState *state = JavaThread::cast(thread)->jvmti_thread_state();
2721     if (state != nullptr) {
2722       // state is non null when SampledObjectAllocEventCollector is enabled.
2723       JvmtiSampledObjectAllocEventCollector *collector;
2724       collector = state->get_sampled_object_alloc_event_collector();
2725 
2726       if (collector != nullptr && collector->is_enabled()) {
2727         collector->record_allocation(obj);
2728       }
2729     }
2730   }
2731 }
2732 
2733 void JvmtiExport::post_garbage_collection_finish() {
2734   Thread *thread = Thread::current(); // this event is posted from VM-Thread.
2735   EVT_TRIG_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
2736                  ("[%s] garbage collection finish event triggered",
2737                   JvmtiTrace::safe_get_thread_name(thread)));
2738   JvmtiEnvIterator it;
2739   for (JvmtiEnv* env = it.first(); env != nullptr; env = it.next(env)) {
2740     if (env->is_enabled(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH)) {
2741       EVT_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
2742                 ("[%s] garbage collection finish event sent",
2743                  JvmtiTrace::safe_get_thread_name(thread)));
2744       JVMTI_THREAD_EVENT_CALLBACK_BLOCK(thread)
2745       // JNIEnv is null here because this event is posted from VM Thread
2746       jvmtiEventGarbageCollectionFinish callback = env->callbacks()->GarbageCollectionFinish;
2747       if (callback != nullptr) {
2748         (*callback)(env->jvmti_external());
2749       }
2750     }
2751   }
2752 }
2753 
2754 void JvmtiExport::post_garbage_collection_start() {
2755   Thread* thread = Thread::current(); // this event is posted from vm-thread.
2756   EVT_TRIG_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_START,
2757                  ("[%s] garbage collection start event triggered",
2758                   JvmtiTrace::safe_get_thread_name(thread)));
2759   JvmtiEnvIterator it;
2760   for (JvmtiEnv* env = it.first(); env != nullptr; env = it.next(env)) {
2761     if (env->is_enabled(JVMTI_EVENT_GARBAGE_COLLECTION_START)) {
2762       EVT_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_START,
2763                 ("[%s] garbage collection start event sent",
2764                  JvmtiTrace::safe_get_thread_name(thread)));
2765       JVMTI_THREAD_EVENT_CALLBACK_BLOCK(thread)
2766       // JNIEnv is null here because this event is posted from VM Thread
2767       jvmtiEventGarbageCollectionStart callback = env->callbacks()->GarbageCollectionStart;
2768       if (callback != nullptr) {
2769         (*callback)(env->jvmti_external());
2770       }
2771     }
2772   }
2773 }
2774 
2775 void JvmtiExport::post_data_dump() {
2776   Thread *thread = Thread::current();
2777   EVT_TRIG_TRACE(JVMTI_EVENT_DATA_DUMP_REQUEST,
2778                  ("[%s] data dump request event triggered",
2779                   JvmtiTrace::safe_get_thread_name(thread)));
2780   JvmtiEnvIterator it;
2781   for (JvmtiEnv* env = it.first(); env != nullptr; env = it.next(env)) {
2782     if (env->is_enabled(JVMTI_EVENT_DATA_DUMP_REQUEST)) {
2783       EVT_TRACE(JVMTI_EVENT_DATA_DUMP_REQUEST,
2784                 ("[%s] data dump request event sent",
2785                  JvmtiTrace::safe_get_thread_name(thread)));
2786      JVMTI_THREAD_EVENT_CALLBACK_BLOCK(thread)
2787      // JNIEnv is null here because this event is posted from VM Thread
2788      jvmtiEventDataDumpRequest callback = env->callbacks()->DataDumpRequest;
2789      if (callback != nullptr) {
2790        (*callback)(env->jvmti_external());
2791      }
2792     }
2793   }
2794 }
2795 
2796 void JvmtiExport::post_monitor_contended_enter(JavaThread *thread, ObjectMonitor *obj_mntr) {
2797   oop object = obj_mntr->object();
2798   HandleMark hm(thread);
2799   Handle h(thread, object);
2800 
2801   JvmtiThreadState *state = get_jvmti_thread_state(thread);
2802   if (state == nullptr) {
2803     return;
2804   }
2805   if (thread->should_hide_jvmti_events()) {
2806     return;
2807   }
2808 
2809   EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTER,
2810                      ("[%s] monitor contended enter event triggered",
2811                       JvmtiTrace::safe_get_thread_name(thread)));
2812   JvmtiEnvThreadStateIterator it(state);
2813   for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
2814     if (ets->is_enabled(JVMTI_EVENT_MONITOR_CONTENDED_ENTER)) {
2815       EVT_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTER,
2816                    ("[%s] monitor contended enter event sent",
2817                     JvmtiTrace::safe_get_thread_name(thread)));
2818       JvmtiMonitorEventMark  jem(thread, h());
2819       JvmtiEnv *env = ets->get_env();
2820       JVMTI_THREAD_EVENT_CALLBACK_BLOCK(thread)
2821       jvmtiEventMonitorContendedEnter callback = env->callbacks()->MonitorContendedEnter;
2822       if (callback != nullptr) {
2823         (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_object());
2824       }
2825     }
2826   }
2827 }
2828 
2829 void JvmtiExport::post_monitor_contended_entered(JavaThread *thread, ObjectMonitor *obj_mntr) {
2830   oop object = obj_mntr->object();
2831   HandleMark hm(thread);
2832   Handle h(thread, object);
2833 
2834   JvmtiThreadState *state = get_jvmti_thread_state(thread);
2835   if (state == nullptr) {
2836     return;
2837   }
2838   if (thread->should_hide_jvmti_events()) {
2839     return;
2840   }
2841 
2842   EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTERED,
2843                      ("[%s] monitor contended entered event triggered",
2844                       JvmtiTrace::safe_get_thread_name(thread)));
2845 
2846   JvmtiEnvThreadStateIterator it(state);
2847   for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
2848     if (ets->is_enabled(JVMTI_EVENT_MONITOR_CONTENDED_ENTERED)) {
2849       EVT_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTERED,
2850                    ("[%s] monitor contended enter event sent",
2851                     JvmtiTrace::safe_get_thread_name(thread)));
2852       JvmtiMonitorEventMark  jem(thread, h());
2853       JvmtiEnv *env = ets->get_env();
2854       JVMTI_THREAD_EVENT_CALLBACK_BLOCK(thread)
2855       jvmtiEventMonitorContendedEntered callback = env->callbacks()->MonitorContendedEntered;
2856       if (callback != nullptr) {
2857         (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_object());
2858       }
2859     }
2860   }
2861 }
2862 
2863 void JvmtiExport::post_monitor_wait(JavaThread *thread, oop object,
2864                                           jlong timeout) {
2865   HandleMark hm(thread);
2866   Handle h(thread, object);
2867 
2868   JvmtiThreadState *state = get_jvmti_thread_state(thread);
2869   if (state == nullptr) {
2870     return;
2871   }
2872   if (thread->should_hide_jvmti_events()) {
2873     return;
2874   }
2875 
2876   EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_WAIT,
2877                      ("[%s] monitor wait event triggered",
2878                       JvmtiTrace::safe_get_thread_name(thread)));
2879   JvmtiEnvThreadStateIterator it(state);
2880   for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
2881     if (ets->is_enabled(JVMTI_EVENT_MONITOR_WAIT)) {
2882       EVT_TRACE(JVMTI_EVENT_MONITOR_WAIT,
2883                    ("[%s] monitor wait event sent",
2884                     JvmtiTrace::safe_get_thread_name(thread)));
2885       JvmtiMonitorEventMark  jem(thread, h());
2886       JvmtiEnv *env = ets->get_env();
2887       JVMTI_THREAD_EVENT_CALLBACK_BLOCK(thread)
2888       jvmtiEventMonitorWait callback = env->callbacks()->MonitorWait;
2889       if (callback != nullptr) {
2890         (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2891                     jem.jni_object(), timeout);
2892       }
2893     }
2894   }
2895 }
2896 
2897 void JvmtiExport::post_monitor_waited(JavaThread *thread, ObjectMonitor *obj_mntr, jboolean timed_out) {
2898   oop object = obj_mntr->object();
2899   HandleMark hm(thread);
2900   Handle h(thread, object);
2901 
2902   JvmtiThreadState *state = get_jvmti_thread_state(thread);
2903   if (state == nullptr) {
2904     return;
2905   }
2906   if (thread->should_hide_jvmti_events()) {
2907     return;
2908   }
2909 
2910   EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_WAITED,
2911                      ("[%s] monitor waited event triggered",
2912                       JvmtiTrace::safe_get_thread_name(thread)));
2913   JvmtiEnvThreadStateIterator it(state);
2914   for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
2915     if (ets->is_enabled(JVMTI_EVENT_MONITOR_WAITED)) {
2916       EVT_TRACE(JVMTI_EVENT_MONITOR_WAITED,
2917                    ("[%s] monitor waited event sent",
2918                     JvmtiTrace::safe_get_thread_name(thread)));
2919       JvmtiMonitorEventMark  jem(thread, h());
2920       JvmtiEnv *env = ets->get_env();
2921       JVMTI_THREAD_EVENT_CALLBACK_BLOCK(thread)
2922       jvmtiEventMonitorWaited callback = env->callbacks()->MonitorWaited;
2923       if (callback != nullptr) {
2924         (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2925                     jem.jni_object(), timed_out);
2926       }
2927     }
2928   }
2929 }
2930 
2931 void JvmtiExport::vthread_post_monitor_waited(JavaThread *current, ObjectMonitor *obj_mntr, jboolean timed_out) {
2932   Handle vthread(current, current->vthread());
2933 
2934   // Finish the VTMS transition temporarily to post the event.
2935   MountUnmountDisabler::end_transition(current, vthread(), true /*is_mount*/, false /*is_thread_start*/);
2936 
2937   // Post event.
2938   JvmtiExport::post_monitor_waited(current, obj_mntr, timed_out);
2939 
2940   // Go back to VTMS transition state.
2941   MountUnmountDisabler::start_transition(current, vthread(), false /*is_mount*/, false /*is_thread_start*/);
2942 }
2943 
2944 void JvmtiExport::post_vm_object_alloc(JavaThread *thread, oop object) {
2945   if (object == nullptr) {
2946     return;
2947   }
2948   if (thread->should_hide_jvmti_events()) {
2949     return;
2950   }
2951   HandleMark hm(thread);
2952   Handle h(thread, object);
2953 
2954   EVT_TRIG_TRACE(JVMTI_EVENT_VM_OBJECT_ALLOC, ("[%s] Trg vm object alloc triggered",
2955                       JvmtiTrace::safe_get_thread_name(thread)));
2956   JvmtiEnvIterator it;
2957   for (JvmtiEnv* env = it.first(); env != nullptr; env = it.next(env)) {
2958     if (env->is_enabled(JVMTI_EVENT_VM_OBJECT_ALLOC)) {
2959       EVT_TRACE(JVMTI_EVENT_VM_OBJECT_ALLOC, ("[%s] Evt vmobject alloc sent %s",
2960                                          JvmtiTrace::safe_get_thread_name(thread),
2961                                          object==nullptr? "null" : object->klass()->external_name()));
2962 
2963       JvmtiObjectAllocEventMark jem(thread, h());
2964       JVMTI_JAVA_THREAD_EVENT_CALLBACK_BLOCK(thread)
2965       jvmtiEventVMObjectAlloc callback = env->callbacks()->VMObjectAlloc;
2966       if (callback != nullptr) {
2967         (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2968                     jem.jni_jobject(), jem.jni_class(), jem.size());
2969       }
2970     }
2971   }
2972 }
2973 
2974 void JvmtiExport::post_sampled_object_alloc(JavaThread *thread, oop object) {
2975   HandleMark hm(thread);
2976   Handle h(thread, object);
2977 
2978   JvmtiThreadState *state = get_jvmti_thread_state(thread);
2979   if (state == nullptr) {
2980     return;
2981   }
2982   if (object == nullptr) {
2983     return;
2984   }
2985   if (thread->should_hide_jvmti_events()) {
2986     return;
2987   }
2988 
2989   EVT_TRIG_TRACE(JVMTI_EVENT_SAMPLED_OBJECT_ALLOC,
2990                  ("[%s] Trg sampled object alloc triggered",
2991                   JvmtiTrace::safe_get_thread_name(thread)));
2992   JvmtiEnvThreadStateIterator it(state);
2993   for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
2994     if (ets->is_enabled(JVMTI_EVENT_SAMPLED_OBJECT_ALLOC)) {
2995       EVT_TRACE(JVMTI_EVENT_SAMPLED_OBJECT_ALLOC,
2996                 ("[%s] Evt sampled object alloc sent %s",
2997                  JvmtiTrace::safe_get_thread_name(thread),
2998                  object == nullptr ? "null" : object->klass()->external_name()));
2999 
3000       JvmtiEnv *env = ets->get_env();
3001       JvmtiObjectAllocEventMark jem(thread, h());
3002       JVMTI_JAVA_THREAD_EVENT_CALLBACK_BLOCK(thread)
3003       jvmtiEventSampledObjectAlloc callback = env->callbacks()->SampledObjectAlloc;
3004       if (callback != nullptr) {
3005         (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
3006                     jem.jni_jobject(), jem.jni_class(), jem.size());
3007       }
3008     }
3009   }
3010 }
3011 
3012 ////////////////////////////////////////////////////////////////////////////////////////////////
3013 
3014 void JvmtiExport::cleanup_thread(JavaThread* thread) {
3015   assert(JavaThread::current() == thread, "thread is not current");
3016   MutexLocker mu(thread, JvmtiThreadState_lock);
3017 
3018   if (thread->jvmti_thread_state() != nullptr) {
3019     // This has to happen after the thread state is removed, which is
3020     // why it is not in post_thread_end_event like its complement
3021     // Maybe both these functions should be rolled into the posts?
3022     JvmtiEventController::thread_ended(thread);
3023   }
3024 }
3025 
3026 void JvmtiExport::clear_detected_exception(JavaThread* thread) {
3027   assert(JavaThread::current() == thread, "thread is not current");
3028 
3029   JvmtiThreadState* state = thread->jvmti_thread_state();
3030   if (state != nullptr) {
3031     state->clear_exception_state();
3032   }
3033 }
3034 
3035 // Onload raw monitor transition.
3036 void JvmtiExport::transition_pending_onload_raw_monitors() {
3037   JvmtiPendingMonitors::transition_raw_monitors();
3038 }
3039 
3040 // Setup current current thread for event collection.
3041 void JvmtiEventCollector::setup_jvmti_thread_state() {
3042   // set this event collector to be the current one.
3043   JvmtiThreadState* state = JvmtiThreadState::state_for(JavaThread::current());
3044   // state can only be null if the current thread is exiting which
3045   // should not happen since we're trying to configure for event collection
3046   guarantee(state != nullptr, "exiting thread called setup_jvmti_thread_state");
3047   if (is_vm_object_alloc_event()) {
3048     JvmtiVMObjectAllocEventCollector *prev = state->get_vm_object_alloc_event_collector();
3049 
3050     // If we have a previous collector and it is disabled, it means this allocation came from a
3051     // callback induced VM Object allocation, do not register this collector then.
3052     if (prev && !prev->is_enabled()) {
3053       return;
3054     }
3055     _prev = prev;
3056     state->set_vm_object_alloc_event_collector((JvmtiVMObjectAllocEventCollector *)this);
3057   } else if (is_dynamic_code_event()) {
3058     _prev = state->get_dynamic_code_event_collector();
3059     state->set_dynamic_code_event_collector((JvmtiDynamicCodeEventCollector *)this);
3060   } else if (is_sampled_object_alloc_event()) {
3061     JvmtiSampledObjectAllocEventCollector *prev = state->get_sampled_object_alloc_event_collector();
3062 
3063     if (prev) {
3064       // JvmtiSampledObjectAllocEventCollector wants only one active collector
3065       // enabled. This allows to have a collector detect a user code requiring
3066       // a sample in the callback.
3067       return;
3068     }
3069     state->set_sampled_object_alloc_event_collector((JvmtiSampledObjectAllocEventCollector*) this);
3070   }
3071 
3072   _unset_jvmti_thread_state = true;
3073 }
3074 
3075 // Unset current event collection in this thread and reset it with previous
3076 // collector.
3077 void JvmtiEventCollector::unset_jvmti_thread_state() {
3078   if (!_unset_jvmti_thread_state) {
3079     return;
3080   }
3081 
3082   JvmtiThreadState* state = JavaThread::current()->jvmti_thread_state();
3083   if (state != nullptr) {
3084     // restore the previous event collector (if any)
3085     if (is_vm_object_alloc_event()) {
3086       if (state->get_vm_object_alloc_event_collector() == this) {
3087         state->set_vm_object_alloc_event_collector((JvmtiVMObjectAllocEventCollector *)_prev);
3088       } else {
3089         // this thread's jvmti state was created during the scope of
3090         // the event collector.
3091       }
3092     } else if (is_dynamic_code_event()) {
3093       if (state->get_dynamic_code_event_collector() == this) {
3094         state->set_dynamic_code_event_collector((JvmtiDynamicCodeEventCollector *)_prev);
3095       } else {
3096         // this thread's jvmti state was created during the scope of
3097         // the event collector.
3098       }
3099     } else if (is_sampled_object_alloc_event()) {
3100       if (state->get_sampled_object_alloc_event_collector() == this) {
3101         state->set_sampled_object_alloc_event_collector((JvmtiSampledObjectAllocEventCollector*)_prev);
3102       } else {
3103         // this thread's jvmti state was created during the scope of
3104         // the event collector.
3105       }
3106     }
3107   }
3108 }
3109 
3110 // create the dynamic code event collector
3111 JvmtiDynamicCodeEventCollector::JvmtiDynamicCodeEventCollector() : _code_blobs(nullptr) {
3112   if (JvmtiExport::should_post_dynamic_code_generated()) {
3113     setup_jvmti_thread_state();
3114   }
3115 }
3116 
3117 // iterate over any code blob descriptors collected and post a
3118 // DYNAMIC_CODE_GENERATED event to the profiler.
3119 JvmtiDynamicCodeEventCollector::~JvmtiDynamicCodeEventCollector() {
3120   assert(!JavaThread::current()->owns_locks(), "all locks must be released to post deferred events");
3121  // iterate over any code blob descriptors that we collected
3122  if (_code_blobs != nullptr) {
3123    for (int i=0; i<_code_blobs->length(); i++) {
3124      JvmtiCodeBlobDesc* blob = _code_blobs->at(i);
3125      JvmtiExport::post_dynamic_code_generated(blob->name(), blob->code_begin(), blob->code_end());
3126      FreeHeap(blob);
3127    }
3128    delete _code_blobs;
3129  }
3130  unset_jvmti_thread_state();
3131 }
3132 
3133 // register a stub
3134 void JvmtiDynamicCodeEventCollector::register_stub(const char* name, address start, address end) {
3135  if (_code_blobs == nullptr) {
3136    _code_blobs = new (mtServiceability) GrowableArray<JvmtiCodeBlobDesc*>(1, mtServiceability);
3137  }
3138  _code_blobs->append(new JvmtiCodeBlobDesc(name, start, end));
3139 }
3140 
3141 // Setup current thread to record vm allocated objects.
3142 JvmtiObjectAllocEventCollector::JvmtiObjectAllocEventCollector() :
3143     _allocated(nullptr), _enable(false), _post_callback(nullptr) {
3144 }
3145 
3146 // Post vm_object_alloc event for vm allocated objects visible to java
3147 // world.
3148 void JvmtiObjectAllocEventCollector::generate_call_for_allocated() {
3149   if (_allocated) {
3150     set_enabled(false);
3151     for (int i = 0; i < _allocated->length(); i++) {
3152       oop obj = _allocated->at(i).resolve();
3153       _post_callback(JavaThread::current(), obj);
3154       // Release OopHandle
3155       _allocated->at(i).release(JvmtiExport::jvmti_oop_storage());
3156 
3157     }
3158     delete _allocated, _allocated = nullptr;
3159   }
3160 }
3161 
3162 void JvmtiObjectAllocEventCollector::record_allocation(oop obj) {
3163   assert(is_enabled(), "Object alloc event collector is not enabled");
3164   if (_allocated == nullptr) {
3165     _allocated = new (mtServiceability) GrowableArray<OopHandle>(1, mtServiceability);
3166   }
3167   _allocated->push(OopHandle(JvmtiExport::jvmti_oop_storage(), obj));
3168 }
3169 
3170 NoJvmtiEventsMark::NoJvmtiEventsMark() {
3171   Thread* thread = Thread::current_or_null();
3172   if (thread != nullptr && thread->is_Java_thread())  {
3173     JavaThread* current_thread = JavaThread::cast(thread);
3174     current_thread->disable_jvmti_events();
3175   }
3176 }
3177 
3178 NoJvmtiEventsMark::~NoJvmtiEventsMark() {
3179   Thread* thread = Thread::current_or_null();
3180   if (thread != nullptr && thread->is_Java_thread())  {
3181     JavaThread* current_thread = JavaThread::cast(thread);
3182     current_thread->enable_jvmti_events();
3183   }
3184 };
3185 
3186 // Setup current thread to record vm allocated objects.
3187 JvmtiVMObjectAllocEventCollector::JvmtiVMObjectAllocEventCollector() {
3188   if (JvmtiExport::should_post_vm_object_alloc()) {
3189     _enable = true;
3190     setup_jvmti_thread_state();
3191     _post_callback = JvmtiExport::post_vm_object_alloc;
3192   }
3193 }
3194 
3195 JvmtiVMObjectAllocEventCollector::~JvmtiVMObjectAllocEventCollector() {
3196   if (_enable) {
3197     generate_call_for_allocated();
3198   }
3199   unset_jvmti_thread_state();
3200 }
3201 
3202 bool JvmtiSampledObjectAllocEventCollector::object_alloc_is_safe_to_sample() {
3203   Thread* thread = Thread::current();
3204   // Really only sample allocations if this is a JavaThread and not the compiler
3205   // thread.
3206   if (!thread->is_Java_thread() || thread->is_Compiler_thread()) {
3207     return false;
3208   }
3209 
3210   // If the current thread is attaching from native and its Java thread object
3211   // is being allocated, things are not ready for allocation sampling.
3212   JavaThread* jt = JavaThread::cast(thread);
3213   if (jt->is_attaching_via_jni() && jt->threadObj() == nullptr) {
3214     return false;
3215   }
3216 
3217   return true;
3218 }
3219 
3220 // Setup current thread to record sampled allocated objects.
3221 void JvmtiSampledObjectAllocEventCollector::start() {
3222   if (JvmtiExport::should_post_sampled_object_alloc()) {
3223     if (!object_alloc_is_safe_to_sample()) {
3224       return;
3225     }
3226 
3227     _enable = true;
3228     setup_jvmti_thread_state();
3229     _post_callback = JvmtiExport::post_sampled_object_alloc;
3230   }
3231 }
3232 
3233 JvmtiSampledObjectAllocEventCollector::~JvmtiSampledObjectAllocEventCollector() {
3234   if (!_enable) {
3235     return;
3236   }
3237 
3238   generate_call_for_allocated();
3239   unset_jvmti_thread_state();
3240 
3241   // Unset the sampling collector as present in assertion mode only.
3242   assert(Thread::current()->is_Java_thread(),
3243          "Should always be in a Java thread");
3244 }
3245 
3246 JvmtiGCMarker::JvmtiGCMarker() {
3247   // if there aren't any JVMTI environments then nothing to do
3248   if (!JvmtiEnv::environments_might_exist()) {
3249     return;
3250   }
3251 
3252   if (JvmtiExport::should_post_garbage_collection_start()) {
3253     JvmtiExport::post_garbage_collection_start();
3254   }
3255 
3256   if (SafepointSynchronize::is_at_safepoint()) {
3257     // Do clean up tasks that need to be done at a safepoint
3258     JvmtiEnvBase::check_for_periodic_clean_up();
3259   }
3260 }
3261 
3262 JvmtiGCMarker::~JvmtiGCMarker() {
3263   // if there aren't any JVMTI environments then nothing to do
3264   if (!JvmtiEnv::environments_might_exist()) {
3265     return;
3266   }
3267 
3268   // JVMTI notify gc finish
3269   if (JvmtiExport::should_post_garbage_collection_finish()) {
3270     JvmtiExport::post_garbage_collection_finish();
3271   }
3272 }