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