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