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