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