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