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