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/fieldDescriptor.inline.hpp"
  51 #include "runtime/handles.hpp"
  52 #include "runtime/interfaceSupport.inline.hpp"
  53 #include "runtime/javaCalls.hpp"
  54 #include "runtime/jniHandles.inline.hpp"
  55 #include "runtime/objectMonitor.hpp"
  56 #include "runtime/objectMonitor.inline.hpp"
  57 #include "runtime/os.inline.hpp"
  58 #include "runtime/safepointVerifiers.hpp"
  59 #include "runtime/thread.inline.hpp"
  60 #include "runtime/threadSMR.hpp"
  61 #include "runtime/vframe.inline.hpp"
  62 #include "utilities/macros.hpp"
  63 
  64 #ifdef JVMTI_TRACE
  65 #define EVT_TRACE(evt,out) if ((JvmtiTrace::event_trace_flags(evt) & JvmtiTrace::SHOW_EVENT_SENT) != 0) { SafeResourceMark rm; log_trace(jvmti) out; }
  66 #define EVT_TRIG_TRACE(evt,out) if ((JvmtiTrace::event_trace_flags(evt) & JvmtiTrace::SHOW_EVENT_TRIGGER) != 0) { SafeResourceMark rm; log_trace(jvmti) out; }
  67 #else
  68 #define EVT_TRIG_TRACE(evt,out)
  69 #define EVT_TRACE(evt,out)
  70 #endif
  71 
  72 ///////////////////////////////////////////////////////////////
  73 //
  74 // JvmtiEventTransition
  75 //
  76 // TO DO --
  77 //  more handle purging
  78 
  79 // Use this for JavaThreads and state is  _thread_in_vm.
  80 class JvmtiJavaThreadEventTransition : StackObj {
  81 private:
  82   ResourceMark _rm;
  83   ThreadToNativeFromVM _transition;
  84   HandleMark _hm;
  85 
  86 public:
  87   JvmtiJavaThreadEventTransition(JavaThread *thread) :
  88     _rm(),
  89     _transition(thread),
  90     _hm(thread)  {};
  91 };
  92 
  93 // For JavaThreads which are not in _thread_in_vm state
  94 // and other system threads use this.
  95 class JvmtiThreadEventTransition : StackObj {
  96 private:
  97   ResourceMark _rm;
  98   HandleMark _hm;
  99   JavaThreadState _saved_state;
 100   JavaThread *_jthread;
 101 
 102 public:
 103   JvmtiThreadEventTransition(Thread *thread) : _rm(), _hm() {
 104     if (thread->is_Java_thread()) {
 105        _jthread = (JavaThread *)thread;
 106        _saved_state = _jthread->thread_state();
 107        if (_saved_state == _thread_in_Java) {
 108          ThreadStateTransition::transition_from_java(_jthread, _thread_in_native);
 109        } else {
 110          ThreadStateTransition::transition(_jthread, _saved_state, _thread_in_native);
 111        }
 112     } else {
 113       _jthread = NULL;
 114     }
 115   }
 116 
 117   ~JvmtiThreadEventTransition() {
 118     if (_jthread != NULL)
 119       ThreadStateTransition::transition_from_native(_jthread, _saved_state);
 120   }
 121 };
 122 
 123 
 124 ///////////////////////////////////////////////////////////////
 125 //
 126 // JvmtiEventMark
 127 //
 128 
 129 class JvmtiEventMark : public StackObj {
 130 private:
 131   JavaThread *_thread;
 132   JNIEnv* _jni_env;
 133   JvmtiThreadState::ExceptionState _saved_exception_state;
 134 #if 0
 135   JNIHandleBlock* _hblock;
 136 #endif
 137 
 138 public:
 139   JvmtiEventMark(JavaThread *thread) :  _thread(thread),
 140                                         _jni_env(thread->jni_environment()),
 141                                         _saved_exception_state(JvmtiThreadState::ES_CLEARED) {
 142 #if 0
 143     _hblock = thread->active_handles();
 144     _hblock->clear_thoroughly(); // so we can be safe
 145 #else
 146     // we want to use the code above - but that needs the JNIHandle changes - later...
 147     // for now, steal JNI push local frame code
 148     JvmtiThreadState *state = thread->jvmti_thread_state();
 149     // we are before an event.
 150     // Save current jvmti thread exception state.
 151     if (state != NULL) {
 152       _saved_exception_state = state->get_exception_state();
 153     }
 154 
 155     JNIHandleBlock* old_handles = thread->active_handles();
 156     JNIHandleBlock* new_handles = JNIHandleBlock::allocate_block(thread);
 157     assert(new_handles != NULL, "should not be NULL");
 158     new_handles->set_pop_frame_link(old_handles);
 159     thread->set_active_handles(new_handles);
 160 #endif
 161     assert(thread == JavaThread::current(), "thread must be current!");
 162     thread->frame_anchor()->make_walkable(thread);
 163   };
 164 
 165   ~JvmtiEventMark() {
 166 #if 0
 167     _hblock->clear(); // for consistency with future correct behavior
 168 #else
 169     // we want to use the code above - but that needs the JNIHandle changes - later...
 170     // for now, steal JNI pop local frame code
 171     JNIHandleBlock* old_handles = _thread->active_handles();
 172     JNIHandleBlock* new_handles = old_handles->pop_frame_link();
 173     assert(new_handles != NULL, "should not be NULL");
 174     _thread->set_active_handles(new_handles);
 175     // Note that we set the pop_frame_link to NULL explicitly, otherwise
 176     // the release_block call will release the blocks.
 177     old_handles->set_pop_frame_link(NULL);
 178     JNIHandleBlock::release_block(old_handles, _thread); // may block
 179 #endif
 180 
 181     JvmtiThreadState* state = _thread->jvmti_thread_state();
 182     // we are continuing after an event.
 183     if (state != NULL) {
 184       // Restore the jvmti thread exception state.
 185       state->restore_exception_state(_saved_exception_state);
 186     }
 187   }
 188 
 189 #if 0
 190   jobject to_jobject(oop obj) { return obj == NULL? NULL : _hblock->allocate_handle_fast(obj); }
 191 #else
 192   // we want to use the code above - but that needs the JNIHandle changes - later...
 193   // for now, use regular make_local
 194   jobject to_jobject(oop obj) { return JNIHandles::make_local(_thread,obj); }
 195 #endif
 196 
 197   jclass to_jclass(Klass* klass) { return (klass == NULL ? NULL : (jclass)to_jobject(klass->java_mirror())); }
 198 
 199   jmethodID to_jmethodID(const methodHandle& method) { return method->jmethod_id(); }
 200 
 201   JNIEnv* jni_env() { return _jni_env; }
 202 };
 203 
 204 class JvmtiThreadEventMark : public JvmtiEventMark {
 205 private:
 206   jthread _jt;
 207 
 208 public:
 209   JvmtiThreadEventMark(JavaThread *thread) :
 210     JvmtiEventMark(thread) {
 211     _jt = (jthread)(to_jobject(thread->threadObj()));
 212   };
 213  jthread jni_thread() { return _jt; }
 214 };
 215 
 216 class JvmtiClassEventMark : public JvmtiThreadEventMark {
 217 private:
 218   jclass _jc;
 219 
 220 public:
 221   JvmtiClassEventMark(JavaThread *thread, Klass* klass) :
 222     JvmtiThreadEventMark(thread) {
 223     _jc = to_jclass(klass);
 224   };
 225   jclass jni_class() { return _jc; }
 226 };
 227 
 228 class JvmtiMethodEventMark : public JvmtiThreadEventMark {
 229 private:
 230   jmethodID _mid;
 231 
 232 public:
 233   JvmtiMethodEventMark(JavaThread *thread, const methodHandle& method) :
 234     JvmtiThreadEventMark(thread),
 235     _mid(to_jmethodID(method)) {};
 236   jmethodID jni_methodID() { return _mid; }
 237 };
 238 
 239 class JvmtiLocationEventMark : public JvmtiMethodEventMark {
 240 private:
 241   jlocation _loc;
 242 
 243 public:
 244   JvmtiLocationEventMark(JavaThread *thread, const methodHandle& method, address location) :
 245     JvmtiMethodEventMark(thread, method),
 246     _loc(location - method->code_base()) {};
 247   jlocation location() { return _loc; }
 248 };
 249 
 250 class JvmtiExceptionEventMark : public JvmtiLocationEventMark {
 251 private:
 252   jobject _exc;
 253 
 254 public:
 255   JvmtiExceptionEventMark(JavaThread *thread, const methodHandle& method, address location, Handle exception) :
 256     JvmtiLocationEventMark(thread, method, location),
 257     _exc(to_jobject(exception())) {};
 258   jobject exception() { return _exc; }
 259 };
 260 
 261 class JvmtiClassFileLoadEventMark : public JvmtiThreadEventMark {
 262 private:
 263   const char *_class_name;
 264   jobject _jloader;
 265   jobject _protection_domain;
 266   jclass  _class_being_redefined;
 267 
 268 public:
 269   JvmtiClassFileLoadEventMark(JavaThread *thread, Symbol* name,
 270      Handle class_loader, Handle prot_domain, Klass* class_being_redefined) : JvmtiThreadEventMark(thread) {
 271       _class_name = name != NULL? name->as_utf8() : NULL;
 272       _jloader = (jobject)to_jobject(class_loader());
 273       _protection_domain = (jobject)to_jobject(prot_domain());
 274       if (class_being_redefined == NULL) {
 275         _class_being_redefined = NULL;
 276       } else {
 277         _class_being_redefined = (jclass)to_jclass(class_being_redefined);
 278       }
 279   };
 280   const char *class_name() {
 281     return _class_name;
 282   }
 283   jobject jloader() {
 284     return _jloader;
 285   }
 286   jobject protection_domain() {
 287     return _protection_domain;
 288   }
 289   jclass class_being_redefined() {
 290     return _class_being_redefined;
 291   }
 292 };
 293 
 294 //////////////////////////////////////////////////////////////////////////////
 295 
 296 int               JvmtiExport::_field_access_count                        = 0;
 297 int               JvmtiExport::_field_modification_count                  = 0;
 298 
 299 bool              JvmtiExport::_can_access_local_variables                = false;
 300 bool              JvmtiExport::_can_hotswap_or_post_breakpoint            = false;
 301 bool              JvmtiExport::_can_modify_any_class                      = false;
 302 bool              JvmtiExport::_can_walk_any_space                        = false;
 303 
 304 bool              JvmtiExport::_has_redefined_a_class                     = false;
 305 bool              JvmtiExport::_all_dependencies_are_recorded             = false;
 306 
 307 //
 308 // field access management
 309 //
 310 
 311 // interpreter generator needs the address of the counter
 312 address JvmtiExport::get_field_access_count_addr() {
 313   // We don't grab a lock because we don't want to
 314   // serialize field access between all threads. This means that a
 315   // thread on another processor can see the wrong count value and
 316   // may either miss making a needed call into post_field_access()
 317   // or will make an unneeded call into post_field_access(). We pay
 318   // this price to avoid slowing down the VM when we aren't watching
 319   // field accesses.
 320   // Other access/mutation safe by virtue of being in VM state.
 321   return (address)(&_field_access_count);
 322 }
 323 
 324 //
 325 // field modification management
 326 //
 327 
 328 // interpreter generator needs the address of the counter
 329 address JvmtiExport::get_field_modification_count_addr() {
 330   // We don't grab a lock because we don't
 331   // want to serialize field modification between all threads. This
 332   // means that a thread on another processor can see the wrong
 333   // count value and may either miss making a needed call into
 334   // post_field_modification() or will make an unneeded call into
 335   // post_field_modification(). We pay this price to avoid slowing
 336   // down the VM when we aren't watching field modifications.
 337   // Other access/mutation safe by virtue of being in VM state.
 338   return (address)(&_field_modification_count);
 339 }
 340 
 341 
 342 ///////////////////////////////////////////////////////////////
 343 // Functions needed by java.lang.instrument for starting up javaagent.
 344 ///////////////////////////////////////////////////////////////
 345 
 346 jint
 347 JvmtiExport::get_jvmti_interface(JavaVM *jvm, void **penv, jint version) {
 348   // The JVMTI_VERSION_INTERFACE_JVMTI part of the version number
 349   // has already been validated in JNI GetEnv().
 350   int major, minor, micro;
 351 
 352   // micro version doesn't matter here (yet?)
 353   decode_version_values(version, &major, &minor, &micro);
 354   switch (major) {
 355     case 1:
 356       switch (minor) {
 357         case 0:  // version 1.0.<micro> is recognized
 358         case 1:  // version 1.1.<micro> is recognized
 359         case 2:  // version 1.2.<micro> is recognized
 360           break;
 361 
 362         default:
 363           return JNI_EVERSION;  // unsupported minor version number
 364       }
 365       break;
 366     case 9:
 367       switch (minor) {
 368         case 0:  // version 9.0.<micro> is recognized
 369           break;
 370         default:
 371           return JNI_EVERSION;  // unsupported minor version number
 372       }
 373       break;
 374     case 11:
 375       switch (minor) {
 376         case 0:  // version 11.0.<micro> is recognized
 377           break;
 378         default:
 379           return JNI_EVERSION;  // unsupported minor version number
 380       }
 381       break;
 382     default:
 383       return JNI_EVERSION;  // unsupported major version number
 384   }
 385 
 386   if (JvmtiEnv::get_phase() == JVMTI_PHASE_LIVE) {
 387     JavaThread* current_thread = JavaThread::current();
 388     // transition code: native to VM
 389     ThreadInVMfromNative __tiv(current_thread);
 390     VM_ENTRY_BASE(jvmtiEnv*, JvmtiExport::get_jvmti_interface, current_thread)
 391     debug_only(VMNativeEntryWrapper __vew;)
 392 
 393     JvmtiEnv *jvmti_env = JvmtiEnv::create_a_jvmti(version);
 394     *penv = jvmti_env->jvmti_external();  // actual type is jvmtiEnv* -- not to be confused with JvmtiEnv*
 395     return JNI_OK;
 396 
 397   } else if (JvmtiEnv::get_phase() == JVMTI_PHASE_ONLOAD) {
 398     // not live, no thread to transition
 399     JvmtiEnv *jvmti_env = JvmtiEnv::create_a_jvmti(version);
 400     *penv = jvmti_env->jvmti_external();  // actual type is jvmtiEnv* -- not to be confused with JvmtiEnv*
 401     return JNI_OK;
 402 
 403   } else {
 404     // Called at the wrong time
 405     *penv = NULL;
 406     return JNI_EDETACHED;
 407   }
 408 }
 409 
 410 void
 411 JvmtiExport::add_default_read_edges(Handle h_module, TRAPS) {
 412   if (!Universe::is_module_initialized()) {
 413     return; // extra safety
 414   }
 415   assert(!h_module.is_null(), "module should always be set");
 416 
 417   // Invoke the transformedByAgent method
 418   JavaValue result(T_VOID);
 419   JavaCalls::call_static(&result,
 420                          SystemDictionary::module_Modules_klass(),
 421                          vmSymbols::transformedByAgent_name(),
 422                          vmSymbols::transformedByAgent_signature(),
 423                          h_module,
 424                          THREAD);
 425 
 426   if (HAS_PENDING_EXCEPTION) {
 427     LogTarget(Trace, jvmti) log;
 428     LogStream log_stream(log);
 429     java_lang_Throwable::print(PENDING_EXCEPTION, &log_stream);
 430     log_stream.cr();
 431     CLEAR_PENDING_EXCEPTION;
 432     return;
 433   }
 434 }
 435 
 436 jvmtiError
 437 JvmtiExport::add_module_reads(Handle module, Handle to_module, TRAPS) {
 438   if (!Universe::is_module_initialized()) {
 439     return JVMTI_ERROR_NONE; // extra safety
 440   }
 441   assert(!module.is_null(), "module should always be set");
 442   assert(!to_module.is_null(), "to_module should always be set");
 443 
 444   // Invoke the addReads method
 445   JavaValue result(T_VOID);
 446   JavaCalls::call_static(&result,
 447                          SystemDictionary::module_Modules_klass(),
 448                          vmSymbols::addReads_name(),
 449                          vmSymbols::addReads_signature(),
 450                          module,
 451                          to_module,
 452                          THREAD);
 453 
 454   if (HAS_PENDING_EXCEPTION) {
 455     LogTarget(Trace, jvmti) log;
 456     LogStream log_stream(log);
 457     java_lang_Throwable::print(PENDING_EXCEPTION, &log_stream);
 458     log_stream.cr();
 459     CLEAR_PENDING_EXCEPTION;
 460     return JVMTI_ERROR_INTERNAL;
 461   }
 462   return JVMTI_ERROR_NONE;
 463 }
 464 
 465 jvmtiError
 466 JvmtiExport::add_module_exports(Handle module, Handle pkg_name, Handle to_module, TRAPS) {
 467   if (!Universe::is_module_initialized()) {
 468     return JVMTI_ERROR_NONE; // extra safety
 469   }
 470   assert(!module.is_null(), "module should always be set");
 471   assert(!to_module.is_null(), "to_module should always be set");
 472   assert(!pkg_name.is_null(), "pkg_name should always be set");
 473 
 474   // Invoke the addExports method
 475   JavaValue result(T_VOID);
 476   JavaCalls::call_static(&result,
 477                          SystemDictionary::module_Modules_klass(),
 478                          vmSymbols::addExports_name(),
 479                          vmSymbols::addExports_signature(),
 480                          module,
 481                          pkg_name,
 482                          to_module,
 483                          THREAD);
 484 
 485   if (HAS_PENDING_EXCEPTION) {
 486     Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
 487     LogTarget(Trace, jvmti) log;
 488     LogStream log_stream(log);
 489     java_lang_Throwable::print(PENDING_EXCEPTION, &log_stream);
 490     log_stream.cr();
 491     CLEAR_PENDING_EXCEPTION;
 492     if (ex_name == vmSymbols::java_lang_IllegalArgumentException()) {
 493       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 494     }
 495     return JVMTI_ERROR_INTERNAL;
 496   }
 497   return JVMTI_ERROR_NONE;
 498 }
 499 
 500 jvmtiError
 501 JvmtiExport::add_module_opens(Handle module, Handle pkg_name, Handle to_module, TRAPS) {
 502   if (!Universe::is_module_initialized()) {
 503     return JVMTI_ERROR_NONE; // extra safety
 504   }
 505   assert(!module.is_null(), "module should always be set");
 506   assert(!to_module.is_null(), "to_module should always be set");
 507   assert(!pkg_name.is_null(), "pkg_name should always be set");
 508 
 509   // Invoke the addOpens method
 510   JavaValue result(T_VOID);
 511   JavaCalls::call_static(&result,
 512                          SystemDictionary::module_Modules_klass(),
 513                          vmSymbols::addOpens_name(),
 514                          vmSymbols::addExports_signature(),
 515                          module,
 516                          pkg_name,
 517                          to_module,
 518                          THREAD);
 519 
 520   if (HAS_PENDING_EXCEPTION) {
 521     Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
 522     LogTarget(Trace, jvmti) log;
 523     LogStream log_stream(log);
 524     java_lang_Throwable::print(PENDING_EXCEPTION, &log_stream);
 525     log_stream.cr();
 526     CLEAR_PENDING_EXCEPTION;
 527     if (ex_name == vmSymbols::java_lang_IllegalArgumentException()) {
 528       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 529     }
 530     return JVMTI_ERROR_INTERNAL;
 531   }
 532   return JVMTI_ERROR_NONE;
 533 }
 534 
 535 jvmtiError
 536 JvmtiExport::add_module_uses(Handle module, Handle service, TRAPS) {
 537   if (!Universe::is_module_initialized()) {
 538     return JVMTI_ERROR_NONE; // extra safety
 539   }
 540   assert(!module.is_null(), "module should always be set");
 541   assert(!service.is_null(), "service should always be set");
 542 
 543   // Invoke the addUses method
 544   JavaValue result(T_VOID);
 545   JavaCalls::call_static(&result,
 546                          SystemDictionary::module_Modules_klass(),
 547                          vmSymbols::addUses_name(),
 548                          vmSymbols::addUses_signature(),
 549                          module,
 550                          service,
 551                          THREAD);
 552 
 553   if (HAS_PENDING_EXCEPTION) {
 554     LogTarget(Trace, jvmti) log;
 555     LogStream log_stream(log);
 556     java_lang_Throwable::print(PENDING_EXCEPTION, &log_stream);
 557     log_stream.cr();
 558     CLEAR_PENDING_EXCEPTION;
 559     return JVMTI_ERROR_INTERNAL;
 560   }
 561   return JVMTI_ERROR_NONE;
 562 }
 563 
 564 jvmtiError
 565 JvmtiExport::add_module_provides(Handle module, Handle service, Handle impl_class, TRAPS) {
 566   if (!Universe::is_module_initialized()) {
 567     return JVMTI_ERROR_NONE; // extra safety
 568   }
 569   assert(!module.is_null(), "module should always be set");
 570   assert(!service.is_null(), "service should always be set");
 571   assert(!impl_class.is_null(), "impl_class should always be set");
 572 
 573   // Invoke the addProvides method
 574   JavaValue result(T_VOID);
 575   JavaCalls::call_static(&result,
 576                          SystemDictionary::module_Modules_klass(),
 577                          vmSymbols::addProvides_name(),
 578                          vmSymbols::addProvides_signature(),
 579                          module,
 580                          service,
 581                          impl_class,
 582                          THREAD);
 583 
 584   if (HAS_PENDING_EXCEPTION) {
 585     LogTarget(Trace, jvmti) log;
 586     LogStream log_stream(log);
 587     java_lang_Throwable::print(PENDING_EXCEPTION, &log_stream);
 588     log_stream.cr();
 589     CLEAR_PENDING_EXCEPTION;
 590     return JVMTI_ERROR_INTERNAL;
 591   }
 592   return JVMTI_ERROR_NONE;
 593 }
 594 
 595 void
 596 JvmtiExport::decode_version_values(jint version, int * major, int * minor,
 597                                    int * micro) {
 598   *major = (version & JVMTI_VERSION_MASK_MAJOR) >> JVMTI_VERSION_SHIFT_MAJOR;
 599   *minor = (version & JVMTI_VERSION_MASK_MINOR) >> JVMTI_VERSION_SHIFT_MINOR;
 600   *micro = (version & JVMTI_VERSION_MASK_MICRO) >> JVMTI_VERSION_SHIFT_MICRO;
 601 }
 602 
 603 void JvmtiExport::enter_primordial_phase() {
 604   JvmtiEnvBase::set_phase(JVMTI_PHASE_PRIMORDIAL);
 605 }
 606 
 607 void JvmtiExport::enter_early_start_phase() {
 608   set_early_vmstart_recorded(true);
 609 }
 610 
 611 void JvmtiExport::enter_start_phase() {
 612   JvmtiEnvBase::set_phase(JVMTI_PHASE_START);
 613 }
 614 
 615 void JvmtiExport::enter_onload_phase() {
 616   JvmtiEnvBase::set_phase(JVMTI_PHASE_ONLOAD);
 617 }
 618 
 619 void JvmtiExport::enter_live_phase() {
 620   JvmtiEnvBase::set_phase(JVMTI_PHASE_LIVE);
 621 }
 622 
 623 //
 624 // JVMTI events that the VM posts to the debugger and also startup agent
 625 // and call the agent's premain() for java.lang.instrument.
 626 //
 627 
 628 void JvmtiExport::post_early_vm_start() {
 629   EVT_TRIG_TRACE(JVMTI_EVENT_VM_START, ("Trg Early VM start event triggered" ));
 630 
 631   // can now enable some events
 632   JvmtiEventController::vm_start();
 633 
 634   JvmtiEnvIterator it;
 635   for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
 636     // Only early vmstart envs post early VMStart event
 637     if (env->early_vmstart_env() && env->is_enabled(JVMTI_EVENT_VM_START)) {
 638       EVT_TRACE(JVMTI_EVENT_VM_START, ("Evt Early VM start event sent" ));
 639       JavaThread *thread  = JavaThread::current();
 640       JvmtiThreadEventMark jem(thread);
 641       JvmtiJavaThreadEventTransition jet(thread);
 642       jvmtiEventVMStart callback = env->callbacks()->VMStart;
 643       if (callback != NULL) {
 644         (*callback)(env->jvmti_external(), jem.jni_env());
 645       }
 646     }
 647   }
 648 }
 649 
 650 void JvmtiExport::post_vm_start() {
 651   EVT_TRIG_TRACE(JVMTI_EVENT_VM_START, ("Trg VM start event triggered" ));
 652 
 653   // can now enable some events
 654   JvmtiEventController::vm_start();
 655 
 656   JvmtiEnvIterator it;
 657   for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
 658     // Early vmstart envs do not post normal VMStart event
 659     if (!env->early_vmstart_env() && env->is_enabled(JVMTI_EVENT_VM_START)) {
 660       EVT_TRACE(JVMTI_EVENT_VM_START, ("Evt VM start event sent" ));
 661 
 662       JavaThread *thread  = JavaThread::current();
 663       JvmtiThreadEventMark jem(thread);
 664       JvmtiJavaThreadEventTransition jet(thread);
 665       jvmtiEventVMStart callback = env->callbacks()->VMStart;
 666       if (callback != NULL) {
 667         (*callback)(env->jvmti_external(), jem.jni_env());
 668       }
 669     }
 670   }
 671 }
 672 
 673 
 674 void JvmtiExport::post_vm_initialized() {
 675   EVT_TRIG_TRACE(JVMTI_EVENT_VM_INIT, ("Trg VM init event triggered" ));
 676 
 677   // can now enable events
 678   JvmtiEventController::vm_init();
 679 
 680   JvmtiEnvIterator it;
 681   for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
 682     if (env->is_enabled(JVMTI_EVENT_VM_INIT)) {
 683       EVT_TRACE(JVMTI_EVENT_VM_INIT, ("Evt VM init event sent" ));
 684 
 685       JavaThread *thread  = JavaThread::current();
 686       JvmtiThreadEventMark jem(thread);
 687       JvmtiJavaThreadEventTransition jet(thread);
 688       jvmtiEventVMInit callback = env->callbacks()->VMInit;
 689       if (callback != NULL) {
 690         (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread());
 691       }
 692     }
 693   }
 694 }
 695 
 696 
 697 void JvmtiExport::post_vm_death() {
 698   EVT_TRIG_TRACE(JVMTI_EVENT_VM_DEATH, ("Trg VM death event triggered" ));
 699 
 700   JvmtiEnvIterator it;
 701   for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
 702     if (env->is_enabled(JVMTI_EVENT_VM_DEATH)) {
 703       EVT_TRACE(JVMTI_EVENT_VM_DEATH, ("Evt VM death event sent" ));
 704 
 705       JavaThread *thread  = JavaThread::current();
 706       JvmtiEventMark jem(thread);
 707       JvmtiJavaThreadEventTransition jet(thread);
 708       jvmtiEventVMDeath callback = env->callbacks()->VMDeath;
 709       if (callback != NULL) {
 710         (*callback)(env->jvmti_external(), jem.jni_env());
 711       }
 712     }
 713   }
 714 
 715   JvmtiEnvBase::set_phase(JVMTI_PHASE_DEAD);
 716   JvmtiEventController::vm_death();
 717 }
 718 
 719 char**
 720 JvmtiExport::get_all_native_method_prefixes(int* count_ptr) {
 721   // Have to grab JVMTI thread state lock to be sure environment doesn't
 722   // go away while we iterate them.  No locks during VM bring-up.
 723   if (Threads::number_of_threads() == 0 || SafepointSynchronize::is_at_safepoint()) {
 724     return JvmtiEnvBase::get_all_native_method_prefixes(count_ptr);
 725   } else {
 726     MutexLocker mu(JvmtiThreadState_lock);
 727     return JvmtiEnvBase::get_all_native_method_prefixes(count_ptr);
 728   }
 729 }
 730 
 731 // Convert an external thread reference to a JavaThread found on the
 732 // specified ThreadsList. The ThreadsListHandle in the caller "protects"
 733 // the returned JavaThread *.
 734 //
 735 // If thread_oop_p is not NULL, then the caller wants to use the oop
 736 // after this call so the oop is returned. On success, *jt_pp is set
 737 // to the converted JavaThread * and JVMTI_ERROR_NONE is returned.
 738 // On error, returns various JVMTI_ERROR_* values.
 739 //
 740 jvmtiError
 741 JvmtiExport::cv_external_thread_to_JavaThread(ThreadsList * t_list,
 742                                               jthread thread,
 743                                               JavaThread ** jt_pp,
 744                                               oop * thread_oop_p) {
 745   assert(t_list != NULL, "must have a ThreadsList");
 746   assert(jt_pp != NULL, "must have a return JavaThread pointer");
 747   // thread_oop_p is optional so no assert()
 748 
 749   oop thread_oop = JNIHandles::resolve_external_guard(thread);
 750   if (thread_oop == NULL) {
 751     // NULL jthread, GC'ed jthread or a bad JNI handle.
 752     return JVMTI_ERROR_INVALID_THREAD;
 753   }
 754   // Looks like an oop at this point.
 755 
 756   if (!thread_oop->is_a(SystemDictionary::Thread_klass())) {
 757     // The oop is not a java.lang.Thread.
 758     return JVMTI_ERROR_INVALID_THREAD;
 759   }
 760   // Looks like a java.lang.Thread oop at this point.
 761 
 762   if (thread_oop_p != NULL) {
 763     // Return the oop to the caller; the caller may still want
 764     // the oop even if this function returns an error.
 765     *thread_oop_p = thread_oop;
 766   }
 767 
 768   JavaThread * java_thread = java_lang_Thread::thread(thread_oop);
 769   if (java_thread == NULL) {
 770     // The java.lang.Thread does not contain a JavaThread * so it has
 771     // not yet run or it has died.
 772     return JVMTI_ERROR_THREAD_NOT_ALIVE;
 773   }
 774   // Looks like a live JavaThread at this point.
 775 
 776   // We do not check the EnableThreadSMRExtraValidityChecks option
 777   // for this includes() call because JVM/TI's spec is tighter.
 778   if (!t_list->includes(java_thread)) {
 779     // Not on the JavaThreads list so it is not alive.
 780     return JVMTI_ERROR_THREAD_NOT_ALIVE;
 781   }
 782 
 783   // Return a live JavaThread that is "protected" by the
 784   // ThreadsListHandle in the caller.
 785   *jt_pp = java_thread;
 786 
 787   return JVMTI_ERROR_NONE;
 788 }
 789 
 790 // Convert an oop to a JavaThread found on the specified ThreadsList.
 791 // The ThreadsListHandle in the caller "protects" the returned
 792 // JavaThread *.
 793 //
 794 // On success, *jt_pp is set to the converted JavaThread * and
 795 // JVMTI_ERROR_NONE is returned. On error, returns various
 796 // JVMTI_ERROR_* values.
 797 //
 798 jvmtiError
 799 JvmtiExport::cv_oop_to_JavaThread(ThreadsList * t_list, oop thread_oop,
 800                                   JavaThread ** jt_pp) {
 801   assert(t_list != NULL, "must have a ThreadsList");
 802   assert(thread_oop != NULL, "must have an oop");
 803   assert(jt_pp != NULL, "must have a return JavaThread pointer");
 804 
 805   if (!thread_oop->is_a(SystemDictionary::Thread_klass())) {
 806     // The oop is not a java.lang.Thread.
 807     return JVMTI_ERROR_INVALID_THREAD;
 808   }
 809   // Looks like a java.lang.Thread oop at this point.
 810 
 811   JavaThread * java_thread = java_lang_Thread::thread(thread_oop);
 812   if (java_thread == NULL) {
 813     // The java.lang.Thread does not contain a JavaThread * so it has
 814     // not yet run or it has died.
 815     return JVMTI_ERROR_THREAD_NOT_ALIVE;
 816   }
 817   // Looks like a live JavaThread at this point.
 818 
 819   // We do not check the EnableThreadSMRExtraValidityChecks option
 820   // for this includes() call because JVM/TI's spec is tighter.
 821   if (!t_list->includes(java_thread)) {
 822     // Not on the JavaThreads list so it is not alive.
 823     return JVMTI_ERROR_THREAD_NOT_ALIVE;
 824   }
 825 
 826   // Return a live JavaThread that is "protected" by the
 827   // ThreadsListHandle in the caller.
 828   *jt_pp = java_thread;
 829 
 830   return JVMTI_ERROR_NONE;
 831 }
 832 
 833 class JvmtiClassFileLoadHookPoster : public StackObj {
 834  private:
 835   Symbol*            _h_name;
 836   Handle               _class_loader;
 837   Handle               _h_protection_domain;
 838   unsigned char **     _data_ptr;
 839   unsigned char **     _end_ptr;
 840   JavaThread *         _thread;
 841   jint                 _curr_len;
 842   unsigned char *      _curr_data;
 843   JvmtiEnv *           _curr_env;
 844   JvmtiCachedClassFileData ** _cached_class_file_ptr;
 845   JvmtiThreadState *   _state;
 846   Klass*               _class_being_redefined;
 847   JvmtiClassLoadKind   _load_kind;
 848   bool                 _has_been_modified;
 849 
 850  public:
 851   inline JvmtiClassFileLoadHookPoster(Symbol* h_name, Handle class_loader,
 852                                       Handle h_protection_domain,
 853                                       unsigned char **data_ptr, unsigned char **end_ptr,
 854                                       JvmtiCachedClassFileData **cache_ptr) {
 855     _h_name = h_name;
 856     _class_loader = class_loader;
 857     _h_protection_domain = h_protection_domain;
 858     _data_ptr = data_ptr;
 859     _end_ptr = end_ptr;
 860     _thread = JavaThread::current();
 861     _curr_len = *end_ptr - *data_ptr;
 862     _curr_data = *data_ptr;
 863     _curr_env = NULL;
 864     _cached_class_file_ptr = cache_ptr;
 865     _has_been_modified = false;
 866 
 867     _state = _thread->jvmti_thread_state();
 868     if (_state != NULL) {
 869       _class_being_redefined = _state->get_class_being_redefined();
 870       _load_kind = _state->get_class_load_kind();
 871       Klass* klass = (_class_being_redefined == NULL) ? NULL : _class_being_redefined;
 872       if (_load_kind != jvmti_class_load_kind_load && klass != NULL) {
 873         ModuleEntry* module_entry = InstanceKlass::cast(klass)->module();
 874         assert(module_entry != NULL, "module_entry should always be set");
 875         if (module_entry->is_named() &&
 876             module_entry->module() != NULL &&
 877             !module_entry->has_default_read_edges()) {
 878           if (!module_entry->set_has_default_read_edges()) {
 879             // We won a potential race.
 880             // Add read edges to the unnamed modules of the bootstrap and app class loaders
 881             Handle class_module(_thread, module_entry->module()); // Obtain j.l.r.Module
 882             JvmtiExport::add_default_read_edges(class_module, _thread);
 883           }
 884         }
 885       }
 886       // Clear class_being_redefined flag here. The action
 887       // from agent handler could generate a new class file load
 888       // hook event and if it is not cleared the new event generated
 889       // from regular class file load could have this stale redefined
 890       // class handle info.
 891       _state->clear_class_being_redefined();
 892     } else {
 893       // redefine and retransform will always set the thread state
 894       _class_being_redefined = NULL;
 895       _load_kind = jvmti_class_load_kind_load;
 896     }
 897   }
 898 
 899   void post() {
 900     post_all_envs();
 901     copy_modified_data();
 902   }
 903 
 904   bool has_been_modified() { return _has_been_modified; }
 905 
 906  private:
 907   void post_all_envs() {
 908     if (_load_kind != jvmti_class_load_kind_retransform) {
 909       // for class load and redefine,
 910       // call the non-retransformable agents
 911       JvmtiEnvIterator it;
 912       for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
 913         if (!env->is_retransformable() && env->is_enabled(JVMTI_EVENT_CLASS_FILE_LOAD_HOOK)) {
 914           // non-retransformable agents cannot retransform back,
 915           // so no need to cache the original class file bytes
 916           post_to_env(env, false);
 917         }
 918       }
 919     }
 920     JvmtiEnvIterator it;
 921     for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
 922       // retransformable agents get all events
 923       if (env->is_retransformable() && env->is_enabled(JVMTI_EVENT_CLASS_FILE_LOAD_HOOK)) {
 924         // retransformable agents need to cache the original class file
 925         // bytes if changes are made via the ClassFileLoadHook
 926         post_to_env(env, true);
 927       }
 928     }
 929   }
 930 
 931   void post_to_env(JvmtiEnv* env, bool caching_needed) {
 932     if (env->phase() == JVMTI_PHASE_PRIMORDIAL && !env->early_class_hook_env()) {
 933       return;
 934     }
 935     unsigned char *new_data = NULL;
 936     jint new_len = 0;
 937     JvmtiClassFileLoadEventMark jem(_thread, _h_name, _class_loader,
 938                                     _h_protection_domain,
 939                                     _class_being_redefined);
 940     JvmtiJavaThreadEventTransition jet(_thread);
 941     jvmtiEventClassFileLoadHook callback = env->callbacks()->ClassFileLoadHook;
 942     if (callback != NULL) {
 943       (*callback)(env->jvmti_external(), jem.jni_env(),
 944                   jem.class_being_redefined(),
 945                   jem.jloader(), jem.class_name(),
 946                   jem.protection_domain(),
 947                   _curr_len, _curr_data,
 948                   &new_len, &new_data);
 949     }
 950     if (new_data != NULL) {
 951       // this agent has modified class data.
 952       _has_been_modified = true;
 953       if (caching_needed && *_cached_class_file_ptr == NULL) {
 954         // data has been changed by the new retransformable agent
 955         // and it hasn't already been cached, cache it
 956         JvmtiCachedClassFileData *p;
 957         p = (JvmtiCachedClassFileData *)os::malloc(
 958           offset_of(JvmtiCachedClassFileData, data) + _curr_len, mtInternal);
 959         if (p == NULL) {
 960           vm_exit_out_of_memory(offset_of(JvmtiCachedClassFileData, data) + _curr_len,
 961             OOM_MALLOC_ERROR,
 962             "unable to allocate cached copy of original class bytes");
 963         }
 964         p->length = _curr_len;
 965         memcpy(p->data, _curr_data, _curr_len);
 966         *_cached_class_file_ptr = p;
 967       }
 968 
 969       if (_curr_data != *_data_ptr) {
 970         // curr_data is previous agent modified class data.
 971         // And this has been changed by the new agent so
 972         // we can delete it now.
 973         _curr_env->Deallocate(_curr_data);
 974       }
 975 
 976       // Class file data has changed by the current agent.
 977       _curr_data = new_data;
 978       _curr_len = new_len;
 979       // Save the current agent env we need this to deallocate the
 980       // memory allocated by this agent.
 981       _curr_env = env;
 982     }
 983   }
 984 
 985   void copy_modified_data() {
 986     // if one of the agent has modified class file data.
 987     // Copy modified class data to new resources array.
 988     if (_curr_data != *_data_ptr) {
 989       *_data_ptr = NEW_RESOURCE_ARRAY(u1, _curr_len);
 990       memcpy(*_data_ptr, _curr_data, _curr_len);
 991       *_end_ptr = *_data_ptr + _curr_len;
 992       _curr_env->Deallocate(_curr_data);
 993     }
 994   }
 995 };
 996 
 997 bool JvmtiExport::early_class_hook_env() {
 998   JvmtiEnvIterator it;
 999   for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
1000     if (env->early_class_hook_env()) {
1001       return true;
1002     }
1003   }
1004   return false;
1005 }
1006 
1007 bool JvmtiExport::_should_post_class_file_load_hook = false;
1008 
1009 // this entry is for class file load hook on class load, redefine and retransform
1010 bool JvmtiExport::post_class_file_load_hook(Symbol* h_name,
1011                                             Handle class_loader,
1012                                             Handle h_protection_domain,
1013                                             unsigned char **data_ptr,
1014                                             unsigned char **end_ptr,
1015                                             JvmtiCachedClassFileData **cache_ptr) {
1016   if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) {
1017     return false;
1018   }
1019 
1020   JvmtiClassFileLoadHookPoster poster(h_name, class_loader,
1021                                       h_protection_domain,
1022                                       data_ptr, end_ptr,
1023                                       cache_ptr);
1024   poster.post();
1025   return poster.has_been_modified();
1026 }
1027 
1028 void JvmtiExport::report_unsupported(bool on) {
1029   // If any JVMTI service is turned on, we need to exit before native code
1030   // tries to access nonexistant services.
1031   if (on) {
1032     vm_exit_during_initialization("Java Kernel does not support JVMTI.");
1033   }
1034 }
1035 
1036 
1037 static inline Klass* oop_to_klass(oop obj) {
1038   Klass* k = obj->klass();
1039 
1040   // if the object is a java.lang.Class then return the java mirror
1041   if (k == SystemDictionary::Class_klass()) {
1042     if (!java_lang_Class::is_primitive(obj)) {
1043       k = java_lang_Class::as_Klass(obj);
1044       assert(k != NULL, "class for non-primitive mirror must exist");
1045     }
1046   }
1047   return k;
1048 }
1049 
1050 class JvmtiObjectAllocEventMark : public JvmtiClassEventMark  {
1051  private:
1052    jobject _jobj;
1053    jlong    _size;
1054  public:
1055    JvmtiObjectAllocEventMark(JavaThread *thread, oop obj) : JvmtiClassEventMark(thread, oop_to_klass(obj)) {
1056      _jobj = (jobject)to_jobject(obj);
1057      _size = Universe::heap()->obj_size(obj) * wordSize;
1058    };
1059    jobject jni_jobject() { return _jobj; }
1060    jlong size() { return _size; }
1061 };
1062 
1063 class JvmtiCompiledMethodLoadEventMark : public JvmtiMethodEventMark {
1064  private:
1065   jint _code_size;
1066   const void *_code_data;
1067   jint _map_length;
1068   jvmtiAddrLocationMap *_map;
1069   const void *_compile_info;
1070  public:
1071   JvmtiCompiledMethodLoadEventMark(JavaThread *thread, nmethod *nm, void* compile_info_ptr = NULL)
1072           : JvmtiMethodEventMark(thread,methodHandle(thread, nm->method())) {
1073     _code_data = nm->insts_begin();
1074     _code_size = nm->insts_size();
1075     _compile_info = compile_info_ptr; // Set void pointer of compiledMethodLoad Event. Default value is NULL.
1076     JvmtiCodeBlobEvents::build_jvmti_addr_location_map(nm, &_map, &_map_length);
1077   }
1078   ~JvmtiCompiledMethodLoadEventMark() {
1079      FREE_C_HEAP_ARRAY(jvmtiAddrLocationMap, _map);
1080   }
1081 
1082   jint code_size() { return _code_size; }
1083   const void *code_data() { return _code_data; }
1084   jint map_length() { return _map_length; }
1085   const jvmtiAddrLocationMap* map() { return _map; }
1086   const void *compile_info() { return _compile_info; }
1087 };
1088 
1089 
1090 
1091 class JvmtiMonitorEventMark : public JvmtiThreadEventMark {
1092 private:
1093   jobject _jobj;
1094 public:
1095   JvmtiMonitorEventMark(JavaThread *thread, oop object)
1096           : JvmtiThreadEventMark(thread){
1097      _jobj = to_jobject(object);
1098   }
1099   jobject jni_object() { return _jobj; }
1100 };
1101 
1102 ///////////////////////////////////////////////////////////////
1103 //
1104 // pending CompiledMethodUnload support
1105 //
1106 
1107 void JvmtiExport::post_compiled_method_unload(
1108        jmethodID method, const void *code_begin) {
1109   if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) {
1110     return;
1111   }
1112   JavaThread* thread = JavaThread::current();
1113   EVT_TRIG_TRACE(JVMTI_EVENT_COMPILED_METHOD_UNLOAD,
1114                  ("[%s] method compile unload event triggered",
1115                   JvmtiTrace::safe_get_thread_name(thread)));
1116 
1117   // post the event for each environment that has this event enabled.
1118   JvmtiEnvIterator it;
1119   for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
1120     if (env->is_enabled(JVMTI_EVENT_COMPILED_METHOD_UNLOAD)) {
1121       if (env->phase() == JVMTI_PHASE_PRIMORDIAL) {
1122         continue;
1123       }
1124       EVT_TRACE(JVMTI_EVENT_COMPILED_METHOD_UNLOAD,
1125                 ("[%s] class compile method unload event sent jmethodID " PTR_FORMAT,
1126                  JvmtiTrace::safe_get_thread_name(thread), p2i(method)));
1127 
1128       ResourceMark rm(thread);
1129 
1130       JvmtiEventMark jem(thread);
1131       JvmtiJavaThreadEventTransition jet(thread);
1132       jvmtiEventCompiledMethodUnload callback = env->callbacks()->CompiledMethodUnload;
1133       if (callback != NULL) {
1134         (*callback)(env->jvmti_external(), method, code_begin);
1135       }
1136     }
1137   }
1138 }
1139 
1140 ///////////////////////////////////////////////////////////////
1141 //
1142 // JvmtiExport
1143 //
1144 
1145 void JvmtiExport::post_raw_breakpoint(JavaThread *thread, Method* method, address location) {
1146   HandleMark hm(thread);
1147   methodHandle mh(thread, method);
1148 
1149   JvmtiThreadState *state = thread->jvmti_thread_state();
1150   if (state == NULL) {
1151     return;
1152   }
1153   EVT_TRIG_TRACE(JVMTI_EVENT_BREAKPOINT, ("[%s] Trg Breakpoint triggered",
1154                       JvmtiTrace::safe_get_thread_name(thread)));
1155   JvmtiEnvThreadStateIterator it(state);
1156   for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
1157     ets->compare_and_set_current_location(mh(), location, JVMTI_EVENT_BREAKPOINT);
1158     if (!ets->breakpoint_posted() && ets->is_enabled(JVMTI_EVENT_BREAKPOINT)) {
1159       ThreadState old_os_state = thread->osthread()->get_state();
1160       thread->osthread()->set_state(BREAKPOINTED);
1161       EVT_TRACE(JVMTI_EVENT_BREAKPOINT, ("[%s] Evt Breakpoint sent %s.%s @ " INTX_FORMAT,
1162                      JvmtiTrace::safe_get_thread_name(thread),
1163                      (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
1164                      (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(),
1165                      location - mh()->code_base() ));
1166 
1167       JvmtiEnv *env = ets->get_env();
1168       JvmtiLocationEventMark jem(thread, mh, location);
1169       JvmtiJavaThreadEventTransition jet(thread);
1170       jvmtiEventBreakpoint callback = env->callbacks()->Breakpoint;
1171       if (callback != NULL) {
1172         (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
1173                     jem.jni_methodID(), jem.location());
1174       }
1175 
1176       ets->set_breakpoint_posted();
1177       thread->osthread()->set_state(old_os_state);
1178     }
1179   }
1180 }
1181 
1182 //////////////////////////////////////////////////////////////////////////////
1183 
1184 bool              JvmtiExport::_can_get_source_debug_extension            = false;
1185 bool              JvmtiExport::_can_maintain_original_method_order        = false;
1186 bool              JvmtiExport::_can_post_interpreter_events               = false;
1187 bool              JvmtiExport::_can_post_on_exceptions                    = false;
1188 bool              JvmtiExport::_can_post_breakpoint                       = false;
1189 bool              JvmtiExport::_can_post_field_access                     = false;
1190 bool              JvmtiExport::_can_post_field_modification               = false;
1191 bool              JvmtiExport::_can_post_method_entry                     = false;
1192 bool              JvmtiExport::_can_post_method_exit                      = false;
1193 bool              JvmtiExport::_can_pop_frame                             = false;
1194 bool              JvmtiExport::_can_force_early_return                    = false;
1195 
1196 bool              JvmtiExport::_early_vmstart_recorded                    = false;
1197 
1198 bool              JvmtiExport::_should_post_single_step                   = false;
1199 bool              JvmtiExport::_should_post_field_access                  = false;
1200 bool              JvmtiExport::_should_post_field_modification            = false;
1201 bool              JvmtiExport::_should_post_class_load                    = false;
1202 bool              JvmtiExport::_should_post_class_prepare                 = false;
1203 bool              JvmtiExport::_should_post_class_unload                  = false;
1204 bool              JvmtiExport::_should_post_thread_life                   = false;
1205 bool              JvmtiExport::_should_clean_up_heap_objects              = false;
1206 bool              JvmtiExport::_should_post_native_method_bind            = false;
1207 bool              JvmtiExport::_should_post_dynamic_code_generated        = false;
1208 bool              JvmtiExport::_should_post_data_dump                     = false;
1209 bool              JvmtiExport::_should_post_compiled_method_load          = false;
1210 bool              JvmtiExport::_should_post_compiled_method_unload        = false;
1211 bool              JvmtiExport::_should_post_monitor_contended_enter       = false;
1212 bool              JvmtiExport::_should_post_monitor_contended_entered     = false;
1213 bool              JvmtiExport::_should_post_monitor_wait                  = false;
1214 bool              JvmtiExport::_should_post_monitor_waited                = false;
1215 bool              JvmtiExport::_should_post_garbage_collection_start      = false;
1216 bool              JvmtiExport::_should_post_garbage_collection_finish     = false;
1217 bool              JvmtiExport::_should_post_object_free                   = false;
1218 bool              JvmtiExport::_should_post_resource_exhausted            = false;
1219 bool              JvmtiExport::_should_post_vm_object_alloc               = false;
1220 bool              JvmtiExport::_should_post_sampled_object_alloc          = false;
1221 bool              JvmtiExport::_should_post_on_exceptions                 = false;
1222 
1223 ////////////////////////////////////////////////////////////////////////////////////////////////
1224 
1225 
1226 //
1227 // JVMTI single step management
1228 //
1229 void JvmtiExport::at_single_stepping_point(JavaThread *thread, Method* method, address location) {
1230   assert(JvmtiExport::should_post_single_step(), "must be single stepping");
1231 
1232   HandleMark hm(thread);
1233   methodHandle mh(thread, method);
1234 
1235   // update information about current location and post a step event
1236   JvmtiThreadState *state = thread->jvmti_thread_state();
1237   if (state == NULL) {
1238     return;
1239   }
1240   EVT_TRIG_TRACE(JVMTI_EVENT_SINGLE_STEP, ("[%s] Trg Single Step triggered",
1241                       JvmtiTrace::safe_get_thread_name(thread)));
1242   if (!state->hide_single_stepping()) {
1243     if (state->is_pending_step_for_popframe()) {
1244       state->process_pending_step_for_popframe();
1245     }
1246     if (state->is_pending_step_for_earlyret()) {
1247       state->process_pending_step_for_earlyret();
1248     }
1249     JvmtiExport::post_single_step(thread, mh(), location);
1250   }
1251 }
1252 
1253 
1254 void JvmtiExport::expose_single_stepping(JavaThread *thread) {
1255   JvmtiThreadState *state = thread->jvmti_thread_state();
1256   if (state != NULL) {
1257     state->clear_hide_single_stepping();
1258   }
1259 }
1260 
1261 
1262 bool JvmtiExport::hide_single_stepping(JavaThread *thread) {
1263   JvmtiThreadState *state = thread->jvmti_thread_state();
1264   if (state != NULL && state->is_enabled(JVMTI_EVENT_SINGLE_STEP)) {
1265     state->set_hide_single_stepping();
1266     return true;
1267   } else {
1268     return false;
1269   }
1270 }
1271 
1272 void JvmtiExport::post_class_load(JavaThread *thread, Klass* klass) {
1273   if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) {
1274     return;
1275   }
1276   HandleMark hm(thread);
1277 
1278   EVT_TRIG_TRACE(JVMTI_EVENT_CLASS_LOAD, ("[%s] Trg Class Load triggered",
1279                       JvmtiTrace::safe_get_thread_name(thread)));
1280   JvmtiThreadState* state = thread->jvmti_thread_state();
1281   if (state == NULL) {
1282     return;
1283   }
1284   JvmtiEnvThreadStateIterator it(state);
1285   for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
1286     if (ets->is_enabled(JVMTI_EVENT_CLASS_LOAD)) {
1287       JvmtiEnv *env = ets->get_env();
1288       if (env->phase() == JVMTI_PHASE_PRIMORDIAL) {
1289         continue;
1290       }
1291       EVT_TRACE(JVMTI_EVENT_CLASS_LOAD, ("[%s] Evt Class Load sent %s",
1292                                          JvmtiTrace::safe_get_thread_name(thread),
1293                                          klass==NULL? "NULL" : klass->external_name() ));
1294       JvmtiClassEventMark jem(thread, klass);
1295       JvmtiJavaThreadEventTransition jet(thread);
1296       jvmtiEventClassLoad callback = env->callbacks()->ClassLoad;
1297       if (callback != NULL) {
1298         (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_class());
1299       }
1300     }
1301   }
1302 }
1303 
1304 
1305 void JvmtiExport::post_class_prepare(JavaThread *thread, Klass* klass) {
1306   if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) {
1307     return;
1308   }
1309   HandleMark hm(thread);
1310 
1311   EVT_TRIG_TRACE(JVMTI_EVENT_CLASS_PREPARE, ("[%s] Trg Class Prepare triggered",
1312                       JvmtiTrace::safe_get_thread_name(thread)));
1313   JvmtiThreadState* state = thread->jvmti_thread_state();
1314   if (state == NULL) {
1315     return;
1316   }
1317   JvmtiEnvThreadStateIterator it(state);
1318   for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
1319     if (ets->is_enabled(JVMTI_EVENT_CLASS_PREPARE)) {
1320       JvmtiEnv *env = ets->get_env();
1321       if (env->phase() == JVMTI_PHASE_PRIMORDIAL) {
1322         continue;
1323       }
1324       EVT_TRACE(JVMTI_EVENT_CLASS_PREPARE, ("[%s] Evt Class Prepare sent %s",
1325                                             JvmtiTrace::safe_get_thread_name(thread),
1326                                             klass==NULL? "NULL" : klass->external_name() ));
1327       JvmtiClassEventMark jem(thread, klass);
1328       JvmtiJavaThreadEventTransition jet(thread);
1329       jvmtiEventClassPrepare callback = env->callbacks()->ClassPrepare;
1330       if (callback != NULL) {
1331         (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_class());
1332       }
1333     }
1334   }
1335 }
1336 
1337 void JvmtiExport::post_class_unload(Klass* klass) {
1338   if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) {
1339     return;
1340   }
1341   Thread *thread = Thread::current();
1342   HandleMark hm(thread);
1343 
1344   EVT_TRIG_TRACE(EXT_EVENT_CLASS_UNLOAD, ("[?] Trg Class Unload triggered" ));
1345   if (JvmtiEventController::is_enabled((jvmtiEvent)EXT_EVENT_CLASS_UNLOAD)) {
1346     assert(thread->is_VM_thread(), "wrong thread");
1347 
1348     // get JavaThread for whom we are proxy
1349     Thread *calling_thread = ((VMThread *)thread)->vm_operation()->calling_thread();
1350     if (!calling_thread->is_Java_thread()) {
1351       // cannot post an event to a non-JavaThread
1352       return;
1353     }
1354     JavaThread *real_thread = (JavaThread *)calling_thread;
1355 
1356     JvmtiEnvIterator it;
1357     for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
1358       if (env->phase() == JVMTI_PHASE_PRIMORDIAL) {
1359         continue;
1360       }
1361       if (env->is_enabled((jvmtiEvent)EXT_EVENT_CLASS_UNLOAD)) {
1362         EVT_TRACE(EXT_EVENT_CLASS_UNLOAD, ("[?] Evt Class Unload sent %s",
1363                   klass==NULL? "NULL" : klass->external_name() ));
1364 
1365         // do everything manually, since this is a proxy - needs special care
1366         JNIEnv* jni_env = real_thread->jni_environment();
1367         jthread jt = (jthread)JNIHandles::make_local(real_thread, real_thread->threadObj());
1368         jclass jk = (jclass)JNIHandles::make_local(real_thread, klass->java_mirror());
1369 
1370         // Before we call the JVMTI agent, we have to set the state in the
1371         // thread for which we are proxying.
1372         JavaThreadState prev_state = real_thread->thread_state();
1373         assert(((Thread *)real_thread)->is_ConcurrentGC_thread() ||
1374                (real_thread->is_Java_thread() && prev_state == _thread_blocked),
1375                "should be ConcurrentGCThread or JavaThread at safepoint");
1376         real_thread->set_thread_state(_thread_in_native);
1377 
1378         jvmtiExtensionEvent callback = env->ext_callbacks()->ClassUnload;
1379         if (callback != NULL) {
1380           (*callback)(env->jvmti_external(), jni_env, jt, jk);
1381         }
1382 
1383         assert(real_thread->thread_state() == _thread_in_native,
1384                "JavaThread should be in native");
1385         real_thread->set_thread_state(prev_state);
1386 
1387         JNIHandles::destroy_local(jk);
1388         JNIHandles::destroy_local(jt);
1389       }
1390     }
1391   }
1392 }
1393 
1394 
1395 void JvmtiExport::post_thread_start(JavaThread *thread) {
1396   if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) {
1397     return;
1398   }
1399   assert(thread->thread_state() == _thread_in_vm, "must be in vm state");
1400 
1401   EVT_TRIG_TRACE(JVMTI_EVENT_THREAD_START, ("[%s] Trg Thread Start event triggered",
1402                       JvmtiTrace::safe_get_thread_name(thread)));
1403 
1404   // do JVMTI thread initialization (if needed)
1405   JvmtiEventController::thread_started(thread);
1406 
1407   // Do not post thread start event for hidden java thread.
1408   if (JvmtiEventController::is_enabled(JVMTI_EVENT_THREAD_START) &&
1409       !thread->is_hidden_from_external_view()) {
1410     JvmtiEnvIterator it;
1411     for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
1412       if (env->phase() == JVMTI_PHASE_PRIMORDIAL) {
1413         continue;
1414       }
1415       if (env->is_enabled(JVMTI_EVENT_THREAD_START)) {
1416         EVT_TRACE(JVMTI_EVENT_THREAD_START, ("[%s] Evt Thread Start event sent",
1417                      JvmtiTrace::safe_get_thread_name(thread) ));
1418 
1419         JvmtiThreadEventMark jem(thread);
1420         JvmtiJavaThreadEventTransition jet(thread);
1421         jvmtiEventThreadStart callback = env->callbacks()->ThreadStart;
1422         if (callback != NULL) {
1423           (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread());
1424         }
1425       }
1426     }
1427   }
1428 }
1429 
1430 
1431 void JvmtiExport::post_thread_end(JavaThread *thread) {
1432   if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) {
1433     return;
1434   }
1435   EVT_TRIG_TRACE(JVMTI_EVENT_THREAD_END, ("[%s] Trg Thread End event triggered",
1436                       JvmtiTrace::safe_get_thread_name(thread)));
1437 
1438   JvmtiThreadState *state = thread->jvmti_thread_state();
1439   if (state == NULL) {
1440     return;
1441   }
1442 
1443   // Do not post thread end event for hidden java thread.
1444   if (state->is_enabled(JVMTI_EVENT_THREAD_END) &&
1445       !thread->is_hidden_from_external_view()) {
1446 
1447     JvmtiEnvThreadStateIterator it(state);
1448     for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
1449       if (ets->is_enabled(JVMTI_EVENT_THREAD_END)) {
1450         JvmtiEnv *env = ets->get_env();
1451         if (env->phase() == JVMTI_PHASE_PRIMORDIAL) {
1452           continue;
1453         }
1454         EVT_TRACE(JVMTI_EVENT_THREAD_END, ("[%s] Evt Thread End event sent",
1455                      JvmtiTrace::safe_get_thread_name(thread) ));
1456 
1457         JvmtiThreadEventMark jem(thread);
1458         JvmtiJavaThreadEventTransition jet(thread);
1459         jvmtiEventThreadEnd callback = env->callbacks()->ThreadEnd;
1460         if (callback != NULL) {
1461           (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread());
1462         }
1463       }
1464     }
1465   }
1466 }
1467 
1468 void JvmtiExport::post_object_free(JvmtiEnv* env, jlong tag) {
1469   assert(SafepointSynchronize::is_at_safepoint(), "must be executed at safepoint");
1470   assert(env->is_enabled(JVMTI_EVENT_OBJECT_FREE), "checking");
1471 
1472   EVT_TRIG_TRACE(JVMTI_EVENT_OBJECT_FREE, ("[?] Trg Object Free triggered" ));
1473   EVT_TRACE(JVMTI_EVENT_OBJECT_FREE, ("[?] Evt Object Free sent"));
1474 
1475   jvmtiEventObjectFree callback = env->callbacks()->ObjectFree;
1476   if (callback != NULL) {
1477     (*callback)(env->jvmti_external(), tag);
1478   }
1479 }
1480 
1481 void JvmtiExport::post_resource_exhausted(jint resource_exhausted_flags, const char* description) {
1482   EVT_TRIG_TRACE(JVMTI_EVENT_RESOURCE_EXHAUSTED, ("Trg resource exhausted event triggered" ));
1483 
1484   JvmtiEnvIterator it;
1485   for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
1486     if (env->is_enabled(JVMTI_EVENT_RESOURCE_EXHAUSTED)) {
1487       EVT_TRACE(JVMTI_EVENT_RESOURCE_EXHAUSTED, ("Evt resource exhausted event sent" ));
1488 
1489       JavaThread *thread  = JavaThread::current();
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     JvmtiVMObjectAllocEventCollector *prev = state->get_vm_object_alloc_event_collector();
2736 
2737     // If we have a previous collector and it is disabled, it means this allocation came from a
2738     // callback induced VM Object allocation, do not register this collector then.
2739     if (prev && !prev->is_enabled()) {
2740       return;
2741     }
2742     _prev = prev;
2743     state->set_vm_object_alloc_event_collector((JvmtiVMObjectAllocEventCollector *)this);
2744   } else if (is_dynamic_code_event()) {
2745     _prev = state->get_dynamic_code_event_collector();
2746     state->set_dynamic_code_event_collector((JvmtiDynamicCodeEventCollector *)this);
2747   } else if (is_sampled_object_alloc_event()) {
2748     JvmtiSampledObjectAllocEventCollector *prev = state->get_sampled_object_alloc_event_collector();
2749 
2750     if (prev) {
2751       // JvmtiSampledObjectAllocEventCollector wants only one active collector
2752       // enabled. This allows to have a collector detect a user code requiring
2753       // a sample in the callback.
2754       return;
2755     }
2756     state->set_sampled_object_alloc_event_collector((JvmtiSampledObjectAllocEventCollector*) this);
2757   }
2758 
2759   _unset_jvmti_thread_state = true;
2760 }
2761 
2762 // Unset current event collection in this thread and reset it with previous
2763 // collector.
2764 void JvmtiEventCollector::unset_jvmti_thread_state() {
2765   if (!_unset_jvmti_thread_state) {
2766     return;
2767   }
2768 
2769   JvmtiThreadState* state = JavaThread::current()->jvmti_thread_state();
2770   if (state != NULL) {
2771     // restore the previous event collector (if any)
2772     if (is_vm_object_alloc_event()) {
2773       if (state->get_vm_object_alloc_event_collector() == this) {
2774         state->set_vm_object_alloc_event_collector((JvmtiVMObjectAllocEventCollector *)_prev);
2775       } else {
2776         // this thread's jvmti state was created during the scope of
2777         // the event collector.
2778       }
2779     } else if (is_dynamic_code_event()) {
2780       if (state->get_dynamic_code_event_collector() == this) {
2781         state->set_dynamic_code_event_collector((JvmtiDynamicCodeEventCollector *)_prev);
2782       } else {
2783         // this thread's jvmti state was created during the scope of
2784         // the event collector.
2785       }
2786     } else if (is_sampled_object_alloc_event()) {
2787       if (state->get_sampled_object_alloc_event_collector() == this) {
2788         state->set_sampled_object_alloc_event_collector((JvmtiSampledObjectAllocEventCollector*)_prev);
2789       } else {
2790         // this thread's jvmti state was created during the scope of
2791         // the event collector.
2792       }
2793     }
2794   }
2795 }
2796 
2797 // create the dynamic code event collector
2798 JvmtiDynamicCodeEventCollector::JvmtiDynamicCodeEventCollector() : _code_blobs(NULL) {
2799   if (JvmtiExport::should_post_dynamic_code_generated()) {
2800     setup_jvmti_thread_state();
2801   }
2802 }
2803 
2804 // iterate over any code blob descriptors collected and post a
2805 // DYNAMIC_CODE_GENERATED event to the profiler.
2806 JvmtiDynamicCodeEventCollector::~JvmtiDynamicCodeEventCollector() {
2807   assert(!JavaThread::current()->owns_locks(), "all locks must be released to post deferred events");
2808  // iterate over any code blob descriptors that we collected
2809  if (_code_blobs != NULL) {
2810    for (int i=0; i<_code_blobs->length(); i++) {
2811      JvmtiCodeBlobDesc* blob = _code_blobs->at(i);
2812      JvmtiExport::post_dynamic_code_generated(blob->name(), blob->code_begin(), blob->code_end());
2813      FreeHeap(blob);
2814    }
2815    delete _code_blobs;
2816  }
2817  unset_jvmti_thread_state();
2818 }
2819 
2820 // register a stub
2821 void JvmtiDynamicCodeEventCollector::register_stub(const char* name, address start, address end) {
2822  if (_code_blobs == NULL) {
2823    _code_blobs = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JvmtiCodeBlobDesc*>(1,true);
2824  }
2825  _code_blobs->append(new JvmtiCodeBlobDesc(name, start, end));
2826 }
2827 
2828 // Setup current thread to record vm allocated objects.
2829 JvmtiObjectAllocEventCollector::JvmtiObjectAllocEventCollector() :
2830     _allocated(NULL), _enable(false), _post_callback(NULL) {
2831 }
2832 
2833 // Post vm_object_alloc event for vm allocated objects visible to java
2834 // world.
2835 void JvmtiObjectAllocEventCollector::generate_call_for_allocated() {
2836   if (_allocated) {
2837     set_enabled(false);
2838     for (int i = 0; i < _allocated->length(); i++) {
2839       oop obj = _allocated->at(i);
2840       _post_callback(JavaThread::current(), obj);
2841     }
2842     delete _allocated, _allocated = NULL;
2843   }
2844 }
2845 
2846 void JvmtiObjectAllocEventCollector::record_allocation(oop obj) {
2847   assert(is_enabled(), "Object alloc event collector is not enabled");
2848   if (_allocated == NULL) {
2849     _allocated = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<oop>(1, true);
2850   }
2851   _allocated->push(obj);
2852 }
2853 
2854 // GC support.
2855 void JvmtiObjectAllocEventCollector::oops_do(OopClosure* f) {
2856   if (_allocated) {
2857     for(int i = _allocated->length() - 1; i >= 0; i--) {
2858       if (_allocated->at(i) != NULL) {
2859         f->do_oop(_allocated->adr_at(i));
2860       }
2861     }
2862   }
2863 }
2864 
2865 void JvmtiObjectAllocEventCollector::oops_do_for_all_threads(OopClosure* f) {
2866   // no-op if jvmti not enabled
2867   if (!JvmtiEnv::environments_might_exist()) {
2868     return;
2869   }
2870 
2871   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jthr = jtiwh.next(); ) {
2872     JvmtiThreadState *state = jthr->jvmti_thread_state();
2873     if (state != NULL) {
2874       JvmtiObjectAllocEventCollector *collector;
2875       collector = state->get_vm_object_alloc_event_collector();
2876       while (collector != NULL) {
2877         collector->oops_do(f);
2878         collector = (JvmtiObjectAllocEventCollector*) collector->get_prev();
2879       }
2880 
2881       collector = state->get_sampled_object_alloc_event_collector();
2882       while (collector != NULL) {
2883         collector->oops_do(f);
2884         collector = (JvmtiObjectAllocEventCollector*) collector->get_prev();
2885       }
2886     }
2887   }
2888 }
2889 
2890 
2891 // Disable collection of VMObjectAlloc events
2892 NoJvmtiVMObjectAllocMark::NoJvmtiVMObjectAllocMark() : _collector(NULL) {
2893   // a no-op if VMObjectAlloc event is not enabled
2894   if (!JvmtiExport::should_post_vm_object_alloc()) {
2895     return;
2896   }
2897   Thread* thread = Thread::current_or_null();
2898   if (thread != NULL && thread->is_Java_thread())  {
2899     JavaThread* current_thread = (JavaThread*)thread;
2900     JvmtiThreadState *state = current_thread->jvmti_thread_state();
2901     if (state != NULL) {
2902       JvmtiVMObjectAllocEventCollector *collector;
2903       collector = state->get_vm_object_alloc_event_collector();
2904       if (collector != NULL && collector->is_enabled()) {
2905         _collector = collector;
2906         _collector->set_enabled(false);
2907       }
2908     }
2909   }
2910 }
2911 
2912 // Re-Enable collection of VMObjectAlloc events (if previously enabled)
2913 NoJvmtiVMObjectAllocMark::~NoJvmtiVMObjectAllocMark() {
2914   if (was_enabled()) {
2915     _collector->set_enabled(true);
2916   }
2917 };
2918 
2919 // Setup current thread to record vm allocated objects.
2920 JvmtiVMObjectAllocEventCollector::JvmtiVMObjectAllocEventCollector() {
2921   if (JvmtiExport::should_post_vm_object_alloc()) {
2922     _enable = true;
2923     setup_jvmti_thread_state();
2924     _post_callback = JvmtiExport::post_vm_object_alloc;
2925   }
2926 }
2927 
2928 JvmtiVMObjectAllocEventCollector::~JvmtiVMObjectAllocEventCollector() {
2929   if (_enable) {
2930     generate_call_for_allocated();
2931   }
2932   unset_jvmti_thread_state();
2933 }
2934 
2935 bool JvmtiSampledObjectAllocEventCollector::object_alloc_is_safe_to_sample() {
2936   Thread* thread = Thread::current();
2937   // Really only sample allocations if this is a JavaThread and not the compiler
2938   // thread.
2939   if (!thread->is_Java_thread() || thread->is_Compiler_thread()) {
2940     return false;
2941   }
2942 
2943   if (Compile_lock->owner() == thread ||
2944       MultiArray_lock->owner() == thread) {
2945     return false;
2946   }
2947   return true;
2948 }
2949 
2950 // Setup current thread to record sampled allocated objects.
2951 JvmtiSampledObjectAllocEventCollector::JvmtiSampledObjectAllocEventCollector() {
2952   if (JvmtiExport::should_post_sampled_object_alloc()) {
2953     if (!object_alloc_is_safe_to_sample()) {
2954       return;
2955     }
2956 
2957     _enable = true;
2958     setup_jvmti_thread_state();
2959     _post_callback = JvmtiExport::post_sampled_object_alloc;
2960   }
2961 }
2962 
2963 JvmtiSampledObjectAllocEventCollector::~JvmtiSampledObjectAllocEventCollector() {
2964   if (!_enable) {
2965     return;
2966   }
2967 
2968   generate_call_for_allocated();
2969   unset_jvmti_thread_state();
2970 
2971   // Unset the sampling collector as present in assertion mode only.
2972   assert(Thread::current()->is_Java_thread(),
2973          "Should always be in a Java thread");
2974 }
2975 
2976 JvmtiGCMarker::JvmtiGCMarker() {
2977   // if there aren't any JVMTI environments then nothing to do
2978   if (!JvmtiEnv::environments_might_exist()) {
2979     return;
2980   }
2981 
2982   if (JvmtiExport::should_post_garbage_collection_start()) {
2983     JvmtiExport::post_garbage_collection_start();
2984   }
2985 
2986   if (SafepointSynchronize::is_at_safepoint()) {
2987     // Do clean up tasks that need to be done at a safepoint
2988     JvmtiEnvBase::check_for_periodic_clean_up();
2989   }
2990 }
2991 
2992 JvmtiGCMarker::~JvmtiGCMarker() {
2993   // if there aren't any JVMTI environments then nothing to do
2994   if (!JvmtiEnv::environments_might_exist()) {
2995     return;
2996   }
2997 
2998   // JVMTI notify gc finish
2999   if (JvmtiExport::should_post_garbage_collection_finish()) {
3000     JvmtiExport::post_garbage_collection_finish();
3001   }
3002 }
--- EOF ---