1 /*
   2  * Copyright (c) 2003, 2019, 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 "interpreter/interpreter.hpp"
  27 #include "jvmtifiles/jvmtiEnv.hpp"
  28 #include "logging/log.hpp"
  29 #include "memory/resourceArea.hpp"
  30 #include "prims/jvmtiEventController.hpp"
  31 #include "prims/jvmtiEventController.inline.hpp"
  32 #include "prims/jvmtiExport.hpp"
  33 #include "prims/jvmtiImpl.hpp"
  34 #include "prims/jvmtiThreadState.inline.hpp"
  35 #include "runtime/frame.hpp"
  36 #include "runtime/thread.inline.hpp"
  37 #include "runtime/threadSMR.hpp"
  38 #include "runtime/vframe.hpp"
  39 #include "runtime/vframe_hp.hpp"
  40 #include "runtime/vmThread.hpp"
  41 #include "runtime/vmOperations.hpp"
  42 
  43 #ifdef JVMTI_TRACE
  44 #define EC_TRACE(out) do { \
  45   if (JvmtiTrace::trace_event_controller()) { \
  46     SafeResourceMark rm; \
  47     log_trace(jvmti) out; \
  48   } \
  49 } while (0)
  50 #else
  51 #define EC_TRACE(out)
  52 #endif /*JVMTI_TRACE */
  53 
  54 // bits for standard events
  55 
  56 static const jlong  SINGLE_STEP_BIT = (((jlong)1) << (JVMTI_EVENT_SINGLE_STEP - TOTAL_MIN_EVENT_TYPE_VAL));
  57 static const jlong  FRAME_POP_BIT = (((jlong)1) << (JVMTI_EVENT_FRAME_POP - TOTAL_MIN_EVENT_TYPE_VAL));
  58 static const jlong  BREAKPOINT_BIT = (((jlong)1) << (JVMTI_EVENT_BREAKPOINT - TOTAL_MIN_EVENT_TYPE_VAL));
  59 static const jlong  FIELD_ACCESS_BIT = (((jlong)1) << (JVMTI_EVENT_FIELD_ACCESS - TOTAL_MIN_EVENT_TYPE_VAL));
  60 static const jlong  FIELD_MODIFICATION_BIT = (((jlong)1) << (JVMTI_EVENT_FIELD_MODIFICATION - TOTAL_MIN_EVENT_TYPE_VAL));
  61 static const jlong  METHOD_ENTRY_BIT = (((jlong)1) << (JVMTI_EVENT_METHOD_ENTRY - TOTAL_MIN_EVENT_TYPE_VAL));
  62 static const jlong  METHOD_EXIT_BIT = (((jlong)1) << (JVMTI_EVENT_METHOD_EXIT - TOTAL_MIN_EVENT_TYPE_VAL));
  63 static const jlong  CLASS_FILE_LOAD_HOOK_BIT = (((jlong)1) << (JVMTI_EVENT_CLASS_FILE_LOAD_HOOK - TOTAL_MIN_EVENT_TYPE_VAL));
  64 static const jlong  NATIVE_METHOD_BIND_BIT = (((jlong)1) << (JVMTI_EVENT_NATIVE_METHOD_BIND - TOTAL_MIN_EVENT_TYPE_VAL));
  65 static const jlong  VM_START_BIT = (((jlong)1) << (JVMTI_EVENT_VM_START - TOTAL_MIN_EVENT_TYPE_VAL));
  66 static const jlong  VM_INIT_BIT = (((jlong)1) << (JVMTI_EVENT_VM_INIT - TOTAL_MIN_EVENT_TYPE_VAL));
  67 static const jlong  VM_DEATH_BIT = (((jlong)1) << (JVMTI_EVENT_VM_DEATH - TOTAL_MIN_EVENT_TYPE_VAL));
  68 static const jlong  CLASS_LOAD_BIT = (((jlong)1) << (JVMTI_EVENT_CLASS_LOAD - TOTAL_MIN_EVENT_TYPE_VAL));
  69 static const jlong  CLASS_PREPARE_BIT = (((jlong)1) << (JVMTI_EVENT_CLASS_PREPARE - TOTAL_MIN_EVENT_TYPE_VAL));
  70 static const jlong  THREAD_START_BIT = (((jlong)1) << (JVMTI_EVENT_THREAD_START - TOTAL_MIN_EVENT_TYPE_VAL));
  71 static const jlong  THREAD_END_BIT = (((jlong)1) << (JVMTI_EVENT_THREAD_END - TOTAL_MIN_EVENT_TYPE_VAL));
  72 static const jlong  EXCEPTION_THROW_BIT = (((jlong)1) << (JVMTI_EVENT_EXCEPTION - TOTAL_MIN_EVENT_TYPE_VAL));
  73 static const jlong  EXCEPTION_CATCH_BIT = (((jlong)1) << (JVMTI_EVENT_EXCEPTION_CATCH - TOTAL_MIN_EVENT_TYPE_VAL));
  74 static const jlong  MONITOR_CONTENDED_ENTER_BIT = (((jlong)1) << (JVMTI_EVENT_MONITOR_CONTENDED_ENTER - TOTAL_MIN_EVENT_TYPE_VAL));
  75 static const jlong  MONITOR_CONTENDED_ENTERED_BIT = (((jlong)1) << (JVMTI_EVENT_MONITOR_CONTENDED_ENTERED - TOTAL_MIN_EVENT_TYPE_VAL));
  76 static const jlong  MONITOR_WAIT_BIT = (((jlong)1) << (JVMTI_EVENT_MONITOR_WAIT - TOTAL_MIN_EVENT_TYPE_VAL));
  77 static const jlong  MONITOR_WAITED_BIT = (((jlong)1) << (JVMTI_EVENT_MONITOR_WAITED - TOTAL_MIN_EVENT_TYPE_VAL));
  78 static const jlong  DYNAMIC_CODE_GENERATED_BIT = (((jlong)1) << (JVMTI_EVENT_DYNAMIC_CODE_GENERATED - TOTAL_MIN_EVENT_TYPE_VAL));
  79 static const jlong  DATA_DUMP_BIT = (((jlong)1) << (JVMTI_EVENT_DATA_DUMP_REQUEST - TOTAL_MIN_EVENT_TYPE_VAL));
  80 static const jlong  COMPILED_METHOD_LOAD_BIT = (((jlong)1) << (JVMTI_EVENT_COMPILED_METHOD_LOAD - TOTAL_MIN_EVENT_TYPE_VAL));
  81 static const jlong  COMPILED_METHOD_UNLOAD_BIT = (((jlong)1) << (JVMTI_EVENT_COMPILED_METHOD_UNLOAD - TOTAL_MIN_EVENT_TYPE_VAL));
  82 static const jlong  GARBAGE_COLLECTION_START_BIT = (((jlong)1) << (JVMTI_EVENT_GARBAGE_COLLECTION_START - TOTAL_MIN_EVENT_TYPE_VAL));
  83 static const jlong  GARBAGE_COLLECTION_FINISH_BIT = (((jlong)1) << (JVMTI_EVENT_GARBAGE_COLLECTION_FINISH - TOTAL_MIN_EVENT_TYPE_VAL));
  84 static const jlong  OBJECT_FREE_BIT = (((jlong)1) << (JVMTI_EVENT_OBJECT_FREE - TOTAL_MIN_EVENT_TYPE_VAL));
  85 static const jlong  RESOURCE_EXHAUSTED_BIT = (((jlong)1) << (JVMTI_EVENT_RESOURCE_EXHAUSTED - TOTAL_MIN_EVENT_TYPE_VAL));
  86 static const jlong  VM_OBJECT_ALLOC_BIT = (((jlong)1) << (JVMTI_EVENT_VM_OBJECT_ALLOC - TOTAL_MIN_EVENT_TYPE_VAL));
  87 static const jlong  SAMPLED_OBJECT_ALLOC_BIT = (((jlong)1) << (JVMTI_EVENT_SAMPLED_OBJECT_ALLOC - TOTAL_MIN_EVENT_TYPE_VAL));
  88 
  89 // bits for extension events
  90 static const jlong  CLASS_UNLOAD_BIT = (((jlong)1) << (EXT_EVENT_CLASS_UNLOAD - TOTAL_MIN_EVENT_TYPE_VAL));
  91 
  92 
  93 static const jlong  MONITOR_BITS = MONITOR_CONTENDED_ENTER_BIT | MONITOR_CONTENDED_ENTERED_BIT |
  94                           MONITOR_WAIT_BIT | MONITOR_WAITED_BIT;
  95 static const jlong  EXCEPTION_BITS = EXCEPTION_THROW_BIT | EXCEPTION_CATCH_BIT;
  96 static const jlong  INTERP_EVENT_BITS =  SINGLE_STEP_BIT | METHOD_ENTRY_BIT | METHOD_EXIT_BIT |
  97                                 FRAME_POP_BIT | FIELD_ACCESS_BIT | FIELD_MODIFICATION_BIT;
  98 static const jlong  THREAD_FILTERED_EVENT_BITS = INTERP_EVENT_BITS | EXCEPTION_BITS | MONITOR_BITS |
  99                                         BREAKPOINT_BIT | CLASS_LOAD_BIT | CLASS_PREPARE_BIT | THREAD_END_BIT |
 100                                         SAMPLED_OBJECT_ALLOC_BIT;
 101 static const jlong  NEED_THREAD_LIFE_EVENTS = THREAD_FILTERED_EVENT_BITS | THREAD_START_BIT;
 102 static const jlong  EARLY_EVENT_BITS = CLASS_FILE_LOAD_HOOK_BIT | CLASS_LOAD_BIT | CLASS_PREPARE_BIT |
 103                                VM_START_BIT | VM_INIT_BIT | VM_DEATH_BIT | NATIVE_METHOD_BIND_BIT |
 104                                THREAD_START_BIT | THREAD_END_BIT |
 105                                COMPILED_METHOD_LOAD_BIT | COMPILED_METHOD_UNLOAD_BIT |
 106                                DYNAMIC_CODE_GENERATED_BIT;
 107 static const jlong  GLOBAL_EVENT_BITS = ~THREAD_FILTERED_EVENT_BITS;
 108 static const jlong  SHOULD_POST_ON_EXCEPTIONS_BITS = EXCEPTION_BITS | METHOD_EXIT_BIT | FRAME_POP_BIT;
 109 
 110 ///////////////////////////////////////////////////////////////
 111 //
 112 // JvmtiEventEnabled
 113 //
 114 
 115 JvmtiEventEnabled::JvmtiEventEnabled() {
 116   clear();
 117 }
 118 
 119 
 120 void JvmtiEventEnabled::clear() {
 121   _enabled_bits = 0;
 122 #ifndef PRODUCT
 123   _init_guard = JEE_INIT_GUARD;
 124 #endif
 125 }
 126 
 127 void JvmtiEventEnabled::set_enabled(jvmtiEvent event_type, bool enabled) {
 128   jlong bits = get_bits();
 129   jlong mask = bit_for(event_type);
 130   if (enabled) {
 131     bits |= mask;
 132   } else {
 133     bits &= ~mask;
 134   }
 135   set_bits(bits);
 136 }
 137 
 138 
 139 ///////////////////////////////////////////////////////////////
 140 //
 141 // JvmtiEnvThreadEventEnable
 142 //
 143 
 144 JvmtiEnvThreadEventEnable::JvmtiEnvThreadEventEnable() {
 145   _event_user_enabled.clear();
 146   _event_enabled.clear();
 147 }
 148 
 149 
 150 JvmtiEnvThreadEventEnable::~JvmtiEnvThreadEventEnable() {
 151   _event_user_enabled.clear();
 152   _event_enabled.clear();
 153 }
 154 
 155 
 156 ///////////////////////////////////////////////////////////////
 157 //
 158 // JvmtiThreadEventEnable
 159 //
 160 
 161 JvmtiThreadEventEnable::JvmtiThreadEventEnable() {
 162   _event_enabled.clear();
 163 }
 164 
 165 
 166 JvmtiThreadEventEnable::~JvmtiThreadEventEnable() {
 167   _event_enabled.clear();
 168 }
 169 
 170 
 171 ///////////////////////////////////////////////////////////////
 172 //
 173 // JvmtiEnvEventEnable
 174 //
 175 
 176 JvmtiEnvEventEnable::JvmtiEnvEventEnable() {
 177   _event_user_enabled.clear();
 178   _event_callback_enabled.clear();
 179   _event_enabled.clear();
 180 }
 181 
 182 
 183 JvmtiEnvEventEnable::~JvmtiEnvEventEnable() {
 184   _event_user_enabled.clear();
 185   _event_callback_enabled.clear();
 186   _event_enabled.clear();
 187 }
 188 
 189 
 190 ///////////////////////////////////////////////////////////////
 191 //
 192 // VM_EnterInterpOnlyMode
 193 //
 194 
 195 class VM_EnterInterpOnlyMode : public VM_Operation {
 196 private:
 197   JvmtiThreadState *_state;
 198 
 199 public:
 200   VM_EnterInterpOnlyMode(JvmtiThreadState *state);
 201 
 202   bool allow_nested_vm_operations() const        { return true; }
 203   VMOp_Type type() const { return VMOp_EnterInterpOnlyMode; }
 204   void doit();
 205 
 206   // to do: this same function is in jvmtiImpl - should be in one place
 207   bool can_be_deoptimized(vframe* vf) {
 208     return (vf->is_compiled_frame() && vf->fr().can_be_deoptimized());
 209   }
 210 };
 211 
 212 VM_EnterInterpOnlyMode::VM_EnterInterpOnlyMode(JvmtiThreadState *state)
 213   : _state(state)
 214 {
 215 }
 216 
 217 
 218 void VM_EnterInterpOnlyMode::doit() {
 219   // Set up the current stack depth for later tracking
 220   _state->invalidate_cur_stack_depth();
 221 
 222   _state->enter_interp_only_mode();
 223 
 224   JavaThread *thread = _state->get_thread();
 225   if (thread->has_last_Java_frame()) {
 226     // If running in fullspeed mode, single stepping is implemented
 227     // as follows: first, the interpreter does not dispatch to
 228     // compiled code for threads that have single stepping enabled;
 229     // second, we deoptimize all methods on the thread's stack when
 230     // interpreted-only mode is enabled the first time for a given
 231     // thread (nothing to do if no Java frames yet).
 232     int num_marked = 0;
 233     ResourceMark resMark;
 234     RegisterMap rm(thread, false);
 235     for (vframe* vf = thread->last_java_vframe(&rm); vf; vf = vf->sender()) {
 236       if (can_be_deoptimized(vf)) {
 237         ((compiledVFrame*) vf)->code()->mark_for_deoptimization();
 238         ++num_marked;
 239       }
 240     }
 241     if (num_marked > 0) {
 242       VM_Deoptimize op;
 243       VMThread::execute(&op);
 244     }
 245   }
 246 }
 247 
 248 
 249 ///////////////////////////////////////////////////////////////
 250 //
 251 // VM_ChangeSingleStep
 252 //
 253 
 254 class VM_ChangeSingleStep : public VM_Operation {
 255 private:
 256   bool _on;
 257 
 258 public:
 259   VM_ChangeSingleStep(bool on);
 260   VMOp_Type type() const                         { return VMOp_ChangeSingleStep; }
 261   bool allow_nested_vm_operations() const        { return true; }
 262   void doit();   // method definition is after definition of JvmtiEventControllerPrivate because of scoping
 263 };
 264 
 265 
 266 VM_ChangeSingleStep::VM_ChangeSingleStep(bool on)
 267   : _on(on != 0)
 268 {
 269 }
 270 
 271 
 272 
 273 
 274 ///////////////////////////////////////////////////////////////
 275 //
 276 // JvmtiEventControllerPrivate
 277 //
 278 // Private internal implementation methods for JvmtiEventController.
 279 //
 280 // These methods are thread safe either because they are called
 281 // in early VM initialization which is single threaded, or they
 282 // hold the JvmtiThreadState_lock.
 283 //
 284 
 285 class JvmtiEventControllerPrivate : public AllStatic {
 286   static bool _initialized;
 287 public:
 288   static void set_should_post_single_step(bool on);
 289   static void enter_interp_only_mode(JvmtiThreadState *state);
 290   static void leave_interp_only_mode(JvmtiThreadState *state);
 291   static void recompute_enabled();
 292   static jlong recompute_env_enabled(JvmtiEnvBase* env);
 293   static jlong recompute_env_thread_enabled(JvmtiEnvThreadState* ets, JvmtiThreadState* state);
 294   static jlong recompute_thread_enabled(JvmtiThreadState *state);
 295   static void event_init();
 296 
 297   static void set_user_enabled(JvmtiEnvBase *env, JavaThread *thread,
 298                         jvmtiEvent event_type, bool enabled);
 299   static void set_event_callbacks(JvmtiEnvBase *env,
 300                                   const jvmtiEventCallbacks* callbacks,
 301                                   jint size_of_callbacks);
 302 
 303   static void set_extension_event_callback(JvmtiEnvBase *env,
 304                                            jint extension_event_index,
 305                                            jvmtiExtensionEvent callback);
 306 
 307   static void set_frame_pop(JvmtiEnvThreadState *env_thread, JvmtiFramePop fpop);
 308   static void clear_frame_pop(JvmtiEnvThreadState *env_thread, JvmtiFramePop fpop);
 309   static void clear_to_frame_pop(JvmtiEnvThreadState *env_thread, JvmtiFramePop fpop);
 310   static void change_field_watch(jvmtiEvent event_type, bool added);
 311 
 312   static void thread_started(JavaThread *thread);
 313   static void thread_ended(JavaThread *thread);
 314 
 315   static void env_initialize(JvmtiEnvBase *env);
 316   static void env_dispose(JvmtiEnvBase *env);
 317 
 318   static void vm_start();
 319   static void vm_init();
 320   static void vm_death();
 321 
 322   static void trace_changed(JvmtiThreadState *state, jlong now_enabled, jlong changed);
 323   static void trace_changed(jlong now_enabled, jlong changed);
 324 };
 325 
 326 bool JvmtiEventControllerPrivate::_initialized = false;
 327 
 328 void JvmtiEventControllerPrivate::set_should_post_single_step(bool on) {
 329   // we have permission to do this, VM op doesn't
 330   JvmtiExport::set_should_post_single_step(on);
 331 }
 332 
 333 
 334 // This change must always be occur when at a safepoint.
 335 // Being at a safepoint causes the interpreter to use the
 336 // safepoint dispatch table which we overload to find single
 337 // step points.  Just to be sure that it has been set, we
 338 // call notice_safepoints when turning on single stepping.
 339 // When we leave our current safepoint, should_post_single_step
 340 // will be checked by the interpreter, and the table kept
 341 // or changed accordingly.
 342 void VM_ChangeSingleStep::doit() {
 343   log_debug(interpreter, safepoint)("changing single step to '%s'", _on ? "on" : "off");
 344   JvmtiEventControllerPrivate::set_should_post_single_step(_on);
 345   if (_on) {
 346     Interpreter::notice_safepoints();
 347   } else {
 348     Interpreter::ignore_safepoints();
 349   }
 350 }
 351 
 352 
 353 void JvmtiEventControllerPrivate::enter_interp_only_mode(JvmtiThreadState *state) {
 354   EC_TRACE(("[%s] # Entering interpreter only mode",
 355             JvmtiTrace::safe_get_thread_name(state->get_thread())));
 356 
 357   VM_EnterInterpOnlyMode op(state);
 358   VMThread::execute(&op);
 359 }
 360 
 361 
 362 void
 363 JvmtiEventControllerPrivate::leave_interp_only_mode(JvmtiThreadState *state) {
 364   EC_TRACE(("[%s] # Leaving interpreter only mode",
 365             JvmtiTrace::safe_get_thread_name(state->get_thread())));
 366   state->leave_interp_only_mode();
 367 }
 368 
 369 
 370 void
 371 JvmtiEventControllerPrivate::trace_changed(JvmtiThreadState *state, jlong now_enabled, jlong changed) {
 372 #ifdef JVMTI_TRACE
 373   if (JvmtiTrace::trace_event_controller()) {
 374     SafeResourceMark rm;
 375     // traces standard events only
 376     for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {
 377       jlong bit = JvmtiEventEnabled::bit_for((jvmtiEvent)ei);
 378       if (changed & bit) {
 379         // it changed, print it
 380          log_trace(jvmti)("[%s] # %s event %s",
 381                       JvmtiTrace::safe_get_thread_name(state->get_thread()),
 382                       (now_enabled & bit)? "Enabling" : "Disabling", JvmtiTrace::event_name((jvmtiEvent)ei));
 383       }
 384     }
 385   }
 386 #endif /*JVMTI_TRACE */
 387 }
 388 
 389 
 390 void
 391 JvmtiEventControllerPrivate::trace_changed(jlong now_enabled, jlong changed) {
 392 #ifdef JVMTI_TRACE
 393   if (JvmtiTrace::trace_event_controller()) {
 394     SafeResourceMark rm;
 395     // traces standard events only
 396     for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {
 397       jlong bit = JvmtiEventEnabled::bit_for((jvmtiEvent)ei);
 398       if (changed & bit) {
 399         // it changed, print it
 400          log_trace(jvmti)("[-] # %s event %s",
 401                       (now_enabled & bit)? "Enabling" : "Disabling", JvmtiTrace::event_name((jvmtiEvent)ei));
 402       }
 403     }
 404   }
 405 #endif /*JVMTI_TRACE */
 406 }
 407 
 408 
 409 // For the specified env: compute the currently truly enabled events
 410 // set external state accordingly.
 411 // Return value and set value must include all events.
 412 // But outside this class, only non-thread-filtered events can be queried..
 413 jlong
 414 JvmtiEventControllerPrivate::recompute_env_enabled(JvmtiEnvBase* env) {
 415   jlong was_enabled = env->env_event_enable()->_event_enabled.get_bits();
 416   jlong now_enabled =
 417     env->env_event_enable()->_event_callback_enabled.get_bits() &
 418     env->env_event_enable()->_event_user_enabled.get_bits();
 419 
 420   switch (env->phase()) {
 421   case JVMTI_PHASE_PRIMORDIAL:
 422   case JVMTI_PHASE_ONLOAD:
 423     // only these events allowed in primordial or onload phase
 424     now_enabled &= (EARLY_EVENT_BITS & ~THREAD_FILTERED_EVENT_BITS);
 425     break;
 426   case JVMTI_PHASE_START:
 427     // only these events allowed in start phase
 428     now_enabled &= EARLY_EVENT_BITS;
 429     break;
 430   case JVMTI_PHASE_LIVE:
 431     // all events allowed during live phase
 432     break;
 433   case JVMTI_PHASE_DEAD:
 434     // no events allowed when dead
 435     now_enabled = 0;
 436     break;
 437   default:
 438     assert(false, "no other phases - sanity check");
 439     break;
 440   }
 441 
 442   // will we really send these events to this env
 443   env->env_event_enable()->_event_enabled.set_bits(now_enabled);
 444 
 445   trace_changed(now_enabled, (now_enabled ^ was_enabled)  & ~THREAD_FILTERED_EVENT_BITS);
 446 
 447   return now_enabled;
 448 }
 449 
 450 
 451 // For the specified env and thread: compute the currently truly enabled events
 452 // set external state accordingly.  Only thread-filtered events are included.
 453 jlong
 454 JvmtiEventControllerPrivate::recompute_env_thread_enabled(JvmtiEnvThreadState* ets, JvmtiThreadState* state) {
 455   JvmtiEnv *env = ets->get_env();
 456 
 457   jlong was_enabled = ets->event_enable()->_event_enabled.get_bits();
 458   jlong now_enabled =  THREAD_FILTERED_EVENT_BITS &
 459     env->env_event_enable()->_event_callback_enabled.get_bits() &
 460     (env->env_event_enable()->_event_user_enabled.get_bits() |
 461      ets->event_enable()->_event_user_enabled.get_bits());
 462 
 463   // for frame pops and field watchs, computed enabled state
 464   // is only true if an event has been requested
 465   if (!ets->has_frame_pops()) {
 466     now_enabled &= ~FRAME_POP_BIT;
 467   }
 468   if (*((int *)JvmtiExport::get_field_access_count_addr()) == 0) {
 469     now_enabled &= ~FIELD_ACCESS_BIT;
 470   }
 471   if (*((int *)JvmtiExport::get_field_modification_count_addr()) == 0) {
 472     now_enabled &= ~FIELD_MODIFICATION_BIT;
 473   }
 474 
 475   switch (JvmtiEnv::get_phase()) {
 476   case JVMTI_PHASE_DEAD:
 477     // no events allowed when dead
 478     now_enabled = 0;
 479     break;
 480   default:
 481     break;
 482   }
 483 
 484   // if anything changed do update
 485   if (now_enabled != was_enabled) {
 486 
 487     // will we really send these events to this thread x env
 488     ets->event_enable()->_event_enabled.set_bits(now_enabled);
 489 
 490     // If the enabled status of the single step or breakpoint events changed,
 491     // the location status may need to change as well.
 492     jlong changed = now_enabled ^ was_enabled;
 493     if (changed & SINGLE_STEP_BIT) {
 494       ets->reset_current_location(JVMTI_EVENT_SINGLE_STEP, (now_enabled & SINGLE_STEP_BIT) != 0);
 495     }
 496     if (changed & BREAKPOINT_BIT) {
 497       ets->reset_current_location(JVMTI_EVENT_BREAKPOINT,  (now_enabled & BREAKPOINT_BIT) != 0);
 498     }
 499     trace_changed(state, now_enabled, changed);
 500   }
 501   return now_enabled;
 502 }
 503 
 504 
 505 // For the specified thread: compute the currently truly enabled events
 506 // set external state accordingly.  Only thread-filtered events are included.
 507 jlong
 508 JvmtiEventControllerPrivate::recompute_thread_enabled(JvmtiThreadState *state) {
 509   if (state == NULL) {
 510     // associated JavaThread is exiting
 511     return (jlong)0;
 512   }
 513 
 514   julong was_any_env_enabled = state->thread_event_enable()->_event_enabled.get_bits();
 515   julong any_env_enabled = 0;
 516   // JVMTI_EVENT_FRAME_POP can be disabled (in the case FRAME_POP_BIT is not set),
 517   // but we need to set interp_only if some JvmtiEnvThreadState has frame pop set
 518   // to clear the request
 519   bool has_frame_pops = false;
 520 
 521   {
 522     // This iteration will include JvmtiEnvThreadStates whose environments
 523     // have been disposed.  These JvmtiEnvThreadStates must not be filtered
 524     // as recompute must be called on them to disable their events,
 525     JvmtiEnvThreadStateIterator it(state);
 526     for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
 527       any_env_enabled |= recompute_env_thread_enabled(ets, state);
 528       has_frame_pops |= ets->has_frame_pops();
 529     }
 530   }
 531 
 532   if (any_env_enabled != was_any_env_enabled) {
 533     // mark if event is truly enabled on this thread in any environment
 534     state->thread_event_enable()->_event_enabled.set_bits(any_env_enabled);
 535 
 536     // update the JavaThread cached value for thread-specific should_post_on_exceptions value
 537     bool should_post_on_exceptions = (any_env_enabled & SHOULD_POST_ON_EXCEPTIONS_BITS) != 0;
 538     state->set_should_post_on_exceptions(should_post_on_exceptions);
 539   }
 540 
 541   // compute interp_only mode
 542   bool should_be_interp = (any_env_enabled & INTERP_EVENT_BITS) != 0 || has_frame_pops;
 543   bool is_now_interp = state->is_interp_only_mode();
 544 
 545   if (should_be_interp != is_now_interp) {
 546     if (should_be_interp) {
 547       enter_interp_only_mode(state);
 548     } else {
 549       leave_interp_only_mode(state);
 550     }
 551   }
 552 
 553   return any_env_enabled;
 554 }
 555 
 556 
 557 // Compute truly enabled events - meaning if the event can and could be
 558 // sent.  An event is truly enabled if it is user enabled on the thread
 559 // or globally user enabled, but only if there is a callback or event hook
 560 // for it and, for field watch and frame pop, one has been set.
 561 // Compute if truly enabled, per thread, per environment, per combination
 562 // (thread x environment), and overall.  These merges are true if any is true.
 563 // True per thread if some environment has callback set and the event is globally
 564 // enabled or enabled for this thread.
 565 // True per environment if the callback is set and the event is globally
 566 // enabled in this environment or enabled for any thread in this environment.
 567 // True per combination if the environment has the callback set and the
 568 // event is globally enabled in this environment or the event is enabled
 569 // for this thread and environment.
 570 //
 571 // All states transitions dependent on these transitions are also handled here.
 572 void
 573 JvmtiEventControllerPrivate::recompute_enabled() {
 574   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
 575 
 576   // event enabled for any thread in any environment
 577   julong was_any_env_thread_enabled = JvmtiEventController::_universal_global_event_enabled.get_bits();
 578   julong any_env_thread_enabled = 0;
 579 
 580   EC_TRACE(("[-] # recompute enabled - before " JULONG_FORMAT_X, was_any_env_thread_enabled));
 581 
 582   // compute non-thread-filters events.
 583   // This must be done separately from thread-filtered events, since some
 584   // events can occur before any threads exist.
 585   JvmtiEnvIterator it;
 586   for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) {
 587     any_env_thread_enabled |= recompute_env_enabled(env);
 588   }
 589 
 590   // We need to create any missing jvmti_thread_state if there are globally set thread
 591   // filtered events and there weren't last time
 592   if (    (any_env_thread_enabled & THREAD_FILTERED_EVENT_BITS) != 0 &&
 593       (was_any_env_thread_enabled & THREAD_FILTERED_EVENT_BITS) == 0) {
 594     for (JavaThreadIteratorWithHandle jtiwh; JavaThread *tp = jtiwh.next(); ) {
 595       // state_for_while_locked() makes tp->is_exiting() check
 596       JvmtiThreadState::state_for_while_locked(tp);  // create the thread state if missing
 597     }
 598   }
 599 
 600   // compute and set thread-filtered events
 601   for (JvmtiThreadState *state = JvmtiThreadState::first(); state != NULL; state = state->next()) {
 602     any_env_thread_enabled |= recompute_thread_enabled(state);
 603   }
 604 
 605   // set universal state (across all envs and threads)
 606   jlong delta = any_env_thread_enabled ^ was_any_env_thread_enabled;
 607   if (delta != 0) {
 608     JvmtiExport::set_should_post_field_access((any_env_thread_enabled & FIELD_ACCESS_BIT) != 0);
 609     JvmtiExport::set_should_post_field_modification((any_env_thread_enabled & FIELD_MODIFICATION_BIT) != 0);
 610     JvmtiExport::set_should_post_class_load((any_env_thread_enabled & CLASS_LOAD_BIT) != 0);
 611     JvmtiExport::set_should_post_class_file_load_hook((any_env_thread_enabled & CLASS_FILE_LOAD_HOOK_BIT) != 0);
 612     JvmtiExport::set_should_post_native_method_bind((any_env_thread_enabled & NATIVE_METHOD_BIND_BIT) != 0);
 613     JvmtiExport::set_should_post_dynamic_code_generated((any_env_thread_enabled & DYNAMIC_CODE_GENERATED_BIT) != 0);
 614     JvmtiExport::set_should_post_data_dump((any_env_thread_enabled & DATA_DUMP_BIT) != 0);
 615     JvmtiExport::set_should_post_class_prepare((any_env_thread_enabled & CLASS_PREPARE_BIT) != 0);
 616     JvmtiExport::set_should_post_class_unload((any_env_thread_enabled & CLASS_UNLOAD_BIT) != 0);
 617     JvmtiExport::set_should_post_monitor_contended_enter((any_env_thread_enabled & MONITOR_CONTENDED_ENTER_BIT) != 0);
 618     JvmtiExport::set_should_post_monitor_contended_entered((any_env_thread_enabled & MONITOR_CONTENDED_ENTERED_BIT) != 0);
 619     JvmtiExport::set_should_post_monitor_wait((any_env_thread_enabled & MONITOR_WAIT_BIT) != 0);
 620     JvmtiExport::set_should_post_monitor_waited((any_env_thread_enabled & MONITOR_WAITED_BIT) != 0);
 621     JvmtiExport::set_should_post_garbage_collection_start((any_env_thread_enabled & GARBAGE_COLLECTION_START_BIT) != 0);
 622     JvmtiExport::set_should_post_garbage_collection_finish((any_env_thread_enabled & GARBAGE_COLLECTION_FINISH_BIT) != 0);
 623     JvmtiExport::set_should_post_object_free((any_env_thread_enabled & OBJECT_FREE_BIT) != 0);
 624     JvmtiExport::set_should_post_resource_exhausted((any_env_thread_enabled & RESOURCE_EXHAUSTED_BIT) != 0);
 625     JvmtiExport::set_should_post_compiled_method_load((any_env_thread_enabled & COMPILED_METHOD_LOAD_BIT) != 0);
 626     JvmtiExport::set_should_post_compiled_method_unload((any_env_thread_enabled & COMPILED_METHOD_UNLOAD_BIT) != 0);
 627     JvmtiExport::set_should_post_vm_object_alloc((any_env_thread_enabled & VM_OBJECT_ALLOC_BIT) != 0);
 628     JvmtiExport::set_should_post_sampled_object_alloc((any_env_thread_enabled & SAMPLED_OBJECT_ALLOC_BIT) != 0);
 629 
 630     // need this if we want thread events or we need them to init data
 631     JvmtiExport::set_should_post_thread_life((any_env_thread_enabled & NEED_THREAD_LIFE_EVENTS) != 0);
 632 
 633     // If single stepping is turned on or off, execute the VM op to change it.
 634     if (delta & SINGLE_STEP_BIT) {
 635       switch (JvmtiEnv::get_phase()) {
 636       case JVMTI_PHASE_DEAD:
 637         // If the VM is dying we can't execute VM ops
 638         break;
 639       case JVMTI_PHASE_LIVE: {
 640         VM_ChangeSingleStep op((any_env_thread_enabled & SINGLE_STEP_BIT) != 0);
 641         VMThread::execute(&op);
 642         break;
 643       }
 644       default:
 645         assert(false, "should never come here before live phase");
 646         break;
 647       }
 648     }
 649 
 650     // set global truly enabled, that is, any thread in any environment
 651     JvmtiEventController::_universal_global_event_enabled.set_bits(any_env_thread_enabled);
 652 
 653     // set global should_post_on_exceptions
 654     JvmtiExport::set_should_post_on_exceptions((any_env_thread_enabled & SHOULD_POST_ON_EXCEPTIONS_BITS) != 0);
 655 
 656   }
 657 
 658   EC_TRACE(("[-] # recompute enabled - after " JULONG_FORMAT_X, any_env_thread_enabled));
 659 }
 660 
 661 
 662 void
 663 JvmtiEventControllerPrivate::thread_started(JavaThread *thread) {
 664   assert(thread->is_Java_thread(), "Must be JavaThread");
 665   assert(thread == Thread::current(), "must be current thread");
 666   assert(JvmtiEnvBase::environments_might_exist(), "to enter event controller, JVM TI environments must exist");
 667 
 668   EC_TRACE(("[%s] # thread started", JvmtiTrace::safe_get_thread_name(thread)));
 669 
 670   // if we have any thread filtered events globally enabled, create/update the thread state
 671   if ((JvmtiEventController::_universal_global_event_enabled.get_bits() & THREAD_FILTERED_EVENT_BITS) != 0) {
 672     MutexLocker mu(JvmtiThreadState_lock);
 673     // create the thread state if missing
 674     JvmtiThreadState *state = JvmtiThreadState::state_for_while_locked(thread);
 675     if (state != NULL) {    // skip threads with no JVMTI thread state
 676       recompute_thread_enabled(state);
 677     }
 678   }
 679 }
 680 
 681 
 682 void
 683 JvmtiEventControllerPrivate::thread_ended(JavaThread *thread) {
 684   // Removes the JvmtiThreadState associated with the specified thread.
 685   // May be called after all environments have been disposed.
 686   assert(JvmtiThreadState_lock->is_locked(), "sanity check");
 687 
 688   EC_TRACE(("[%s] # thread ended", JvmtiTrace::safe_get_thread_name(thread)));
 689 
 690   JvmtiThreadState *state = thread->jvmti_thread_state();
 691   assert(state != NULL, "else why are we here?");
 692   delete state;
 693 }
 694 
 695 void JvmtiEventControllerPrivate::set_event_callbacks(JvmtiEnvBase *env,
 696                                                       const jvmtiEventCallbacks* callbacks,
 697                                                       jint size_of_callbacks) {
 698   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
 699   EC_TRACE(("[*] # set event callbacks"));
 700 
 701   env->set_event_callbacks(callbacks, size_of_callbacks);
 702   jlong enabled_bits = 0;
 703   for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {
 704     jvmtiEvent evt_t = (jvmtiEvent)ei;
 705     if (env->has_callback(evt_t)) {
 706       enabled_bits |= JvmtiEventEnabled::bit_for(evt_t);
 707     }
 708   }
 709   env->env_event_enable()->_event_callback_enabled.set_bits(enabled_bits);
 710   recompute_enabled();
 711 }
 712 
 713 void
 714 JvmtiEventControllerPrivate::set_extension_event_callback(JvmtiEnvBase *env,
 715                                                           jint extension_event_index,
 716                                                           jvmtiExtensionEvent callback)
 717 {
 718   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
 719   EC_TRACE(("[*] # set extension event callback"));
 720 
 721   // extension events are allocated below JVMTI_MIN_EVENT_TYPE_VAL
 722   assert(extension_event_index >= (jint)EXT_MIN_EVENT_TYPE_VAL &&
 723          extension_event_index <= (jint)EXT_MAX_EVENT_TYPE_VAL, "sanity check");
 724 
 725 
 726   // As the bits for both standard (jvmtiEvent) and extension
 727   // (jvmtiExtEvents) are stored in the same word we cast here to
 728   // jvmtiEvent to set/clear the bit for this extension event.
 729   jvmtiEvent event_type = (jvmtiEvent)extension_event_index;
 730 
 731   // Prevent a possible race condition where events are re-enabled by a call to
 732   // set event callbacks, where the DisposeEnvironment occurs after the boiler-plate
 733   // environment check and before the lock is acquired.
 734   // We can safely do the is_valid check now, as JvmtiThreadState_lock is held.
 735   bool enabling = (callback != NULL) && (env->is_valid());
 736   env->env_event_enable()->set_user_enabled(event_type, enabling);
 737 
 738   // update the callback
 739   jvmtiExtEventCallbacks* ext_callbacks = env->ext_callbacks();
 740   switch (extension_event_index) {
 741     case EXT_EVENT_CLASS_UNLOAD :
 742       ext_callbacks->ClassUnload = callback;
 743       break;
 744     default:
 745       ShouldNotReachHere();
 746   }
 747 
 748   // update the callback enable/disable bit
 749   jlong enabled_bits = env->env_event_enable()->_event_callback_enabled.get_bits();
 750   jlong bit_for = JvmtiEventEnabled::bit_for(event_type);
 751   if (enabling) {
 752     enabled_bits |= bit_for;
 753   } else {
 754     enabled_bits &= ~bit_for;
 755   }
 756   env->env_event_enable()->_event_callback_enabled.set_bits(enabled_bits);
 757 
 758   recompute_enabled();
 759 }
 760 
 761 
 762 void
 763 JvmtiEventControllerPrivate::env_initialize(JvmtiEnvBase *env) {
 764   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
 765   EC_TRACE(("[*] # env initialize"));
 766 
 767   if (JvmtiEnvBase::is_vm_live()) {
 768     // if we didn't initialize event info already (this is a late
 769     // launched environment), do it now.
 770     event_init();
 771   }
 772 
 773   env->initialize();
 774 
 775   // add the JvmtiEnvThreadState to each JvmtiThreadState
 776   for (JvmtiThreadState *state = JvmtiThreadState::first(); state != NULL; state = state->next()) {
 777     state->add_env(env);
 778     assert((JvmtiEnv*)(state->env_thread_state(env)->get_env()) == env, "sanity check");
 779   }
 780   JvmtiEventControllerPrivate::recompute_enabled();
 781 }
 782 
 783 
 784 void
 785 JvmtiEventControllerPrivate::env_dispose(JvmtiEnvBase *env) {
 786   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
 787   EC_TRACE(("[*] # env dispose"));
 788 
 789   // Before the environment is marked disposed, disable all events on this
 790   // environment (by zapping the callbacks).  As a result, the disposed
 791   // environment will not call event handlers.
 792   set_event_callbacks(env, NULL, 0);
 793   for (jint extension_event_index = EXT_MIN_EVENT_TYPE_VAL;
 794        extension_event_index <= EXT_MAX_EVENT_TYPE_VAL;
 795        ++extension_event_index) {
 796     set_extension_event_callback(env, extension_event_index, NULL);
 797   }
 798 
 799   // Let the environment finish disposing itself.
 800   env->env_dispose();
 801 }
 802 
 803 
 804 void
 805 JvmtiEventControllerPrivate::set_user_enabled(JvmtiEnvBase *env, JavaThread *thread,
 806                                           jvmtiEvent event_type, bool enabled) {
 807   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
 808 
 809   EC_TRACE(("[%s] # user %s event %s",
 810             thread==NULL? "ALL": JvmtiTrace::safe_get_thread_name(thread),
 811             enabled? "enabled" : "disabled", JvmtiTrace::event_name(event_type)));
 812 
 813   if (thread == NULL) {
 814     env->env_event_enable()->set_user_enabled(event_type, enabled);
 815   } else {
 816     // create the thread state (if it didn't exist before)
 817     JvmtiThreadState *state = JvmtiThreadState::state_for_while_locked(thread);
 818     if (state != NULL) {
 819       state->env_thread_state(env)->event_enable()->set_user_enabled(event_type, enabled);
 820     }
 821   }
 822   recompute_enabled();
 823 }
 824 
 825 
 826 void
 827 JvmtiEventControllerPrivate::set_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
 828   EC_TRACE(("[%s] # set frame pop - frame=%d",
 829             JvmtiTrace::safe_get_thread_name(ets->get_thread()),
 830             fpop.frame_number() ));
 831 
 832   ets->get_frame_pops()->set(fpop);
 833   recompute_thread_enabled(ets->get_thread()->jvmti_thread_state());
 834 }
 835 
 836 
 837 void
 838 JvmtiEventControllerPrivate::clear_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
 839   EC_TRACE(("[%s] # clear frame pop - frame=%d",
 840             JvmtiTrace::safe_get_thread_name(ets->get_thread()),
 841             fpop.frame_number() ));
 842 
 843   ets->get_frame_pops()->clear(fpop);
 844   recompute_thread_enabled(ets->get_thread()->jvmti_thread_state());
 845 }
 846 
 847 
 848 void
 849 JvmtiEventControllerPrivate::clear_to_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
 850   int cleared_cnt = ets->get_frame_pops()->clear_to(fpop);
 851 
 852   EC_TRACE(("[%s] # clear to frame pop - frame=%d, count=%d",
 853             JvmtiTrace::safe_get_thread_name(ets->get_thread()),
 854             fpop.frame_number(),
 855             cleared_cnt ));
 856 
 857   if (cleared_cnt > 0) {
 858     recompute_thread_enabled(ets->get_thread()->jvmti_thread_state());
 859   }
 860 }
 861 
 862 void
 863 JvmtiEventControllerPrivate::change_field_watch(jvmtiEvent event_type, bool added) {
 864   int *count_addr;
 865 
 866   switch (event_type) {
 867   case JVMTI_EVENT_FIELD_MODIFICATION:
 868     count_addr = (int *)JvmtiExport::get_field_modification_count_addr();
 869     break;
 870   case JVMTI_EVENT_FIELD_ACCESS:
 871     count_addr = (int *)JvmtiExport::get_field_access_count_addr();
 872     break;
 873   default:
 874     assert(false, "incorrect event");
 875     return;
 876   }
 877 
 878   EC_TRACE(("[-] # change field watch - %s %s count=%d",
 879             event_type==JVMTI_EVENT_FIELD_MODIFICATION? "modification" : "access",
 880             added? "add" : "remove",
 881             *count_addr));
 882 
 883   if (added) {
 884     (*count_addr)++;
 885     if (*count_addr == 1) {
 886       recompute_enabled();
 887     }
 888   } else {
 889     if (*count_addr > 0) {
 890       (*count_addr)--;
 891       if (*count_addr == 0) {
 892         recompute_enabled();
 893       }
 894     } else {
 895       assert(false, "field watch out of phase");
 896     }
 897   }
 898 }
 899 
 900 void
 901 JvmtiEventControllerPrivate::event_init() {
 902   assert(JvmtiThreadState_lock->is_locked(), "sanity check");
 903 
 904   if (_initialized) {
 905     return;
 906   }
 907 
 908   EC_TRACE(("[-] # VM live"));
 909 
 910 #ifdef ASSERT
 911   // check that our idea and the spec's idea of threaded events match
 912   for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {
 913     jlong bit = JvmtiEventEnabled::bit_for((jvmtiEvent)ei);
 914     assert(((THREAD_FILTERED_EVENT_BITS & bit) != 0) == JvmtiUtil::event_threaded(ei),
 915            "thread filtered event list does not match");
 916   }
 917 #endif
 918 
 919   _initialized = true;
 920 }
 921 
 922 void
 923 JvmtiEventControllerPrivate::vm_start() {
 924   // some events are now able to be enabled (phase has changed)
 925   JvmtiEventControllerPrivate::recompute_enabled();
 926 }
 927 
 928 
 929 void
 930 JvmtiEventControllerPrivate::vm_init() {
 931   event_init();
 932 
 933   // all the events are now able to be enabled (phase has changed)
 934   JvmtiEventControllerPrivate::recompute_enabled();
 935 }
 936 
 937 
 938 void
 939 JvmtiEventControllerPrivate::vm_death() {
 940   // events are disabled (phase has changed)
 941   JvmtiEventControllerPrivate::recompute_enabled();
 942 }
 943 
 944 
 945 ///////////////////////////////////////////////////////////////
 946 //
 947 // JvmtiEventController
 948 //
 949 
 950 JvmtiEventEnabled JvmtiEventController::_universal_global_event_enabled;
 951 
 952 bool
 953 JvmtiEventController::is_global_event(jvmtiEvent event_type) {
 954   assert(is_valid_event_type(event_type), "invalid event type");
 955   jlong bit_for = ((jlong)1) << (event_type - TOTAL_MIN_EVENT_TYPE_VAL);
 956   return((bit_for & GLOBAL_EVENT_BITS)!=0);
 957 }
 958 
 959 void
 960 JvmtiEventController::set_user_enabled(JvmtiEnvBase *env, JavaThread *thread, jvmtiEvent event_type, bool enabled) {
 961   if (Threads::number_of_threads() == 0) {
 962     // during early VM start-up locks don't exist, but we are safely single threaded,
 963     // call the functionality without holding the JvmtiThreadState_lock.
 964     JvmtiEventControllerPrivate::set_user_enabled(env, thread, event_type, enabled);
 965   } else {
 966     MutexLocker mu(JvmtiThreadState_lock);
 967     JvmtiEventControllerPrivate::set_user_enabled(env, thread, event_type, enabled);
 968   }
 969 }
 970 
 971 
 972 void
 973 JvmtiEventController::set_event_callbacks(JvmtiEnvBase *env,
 974                                           const jvmtiEventCallbacks* callbacks,
 975                                           jint size_of_callbacks) {
 976   if (Threads::number_of_threads() == 0) {
 977     // during early VM start-up locks don't exist, but we are safely single threaded,
 978     // call the functionality without holding the JvmtiThreadState_lock.
 979     JvmtiEventControllerPrivate::set_event_callbacks(env, callbacks, size_of_callbacks);
 980   } else {
 981     MutexLocker mu(JvmtiThreadState_lock);
 982     JvmtiEventControllerPrivate::set_event_callbacks(env, callbacks, size_of_callbacks);
 983   }
 984 }
 985 
 986 void
 987 JvmtiEventController::set_extension_event_callback(JvmtiEnvBase *env,
 988                                                    jint extension_event_index,
 989                                                    jvmtiExtensionEvent callback) {
 990   if (Threads::number_of_threads() == 0) {
 991     JvmtiEventControllerPrivate::set_extension_event_callback(env, extension_event_index, callback);
 992   } else {
 993     MutexLocker mu(JvmtiThreadState_lock);
 994     JvmtiEventControllerPrivate::set_extension_event_callback(env, extension_event_index, callback);
 995   }
 996 }
 997 
 998 
 999 
1000 
1001 void
1002 JvmtiEventController::set_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
1003   MutexLocker mu(SafepointSynchronize::is_at_safepoint() ? NULL : JvmtiThreadState_lock);
1004   JvmtiEventControllerPrivate::set_frame_pop(ets, fpop);
1005 }
1006 
1007 
1008 void
1009 JvmtiEventController::clear_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
1010   MutexLocker mu(SafepointSynchronize::is_at_safepoint() ? NULL : JvmtiThreadState_lock);
1011   JvmtiEventControllerPrivate::clear_frame_pop(ets, fpop);
1012 }
1013 
1014 
1015 void
1016 JvmtiEventController::clear_to_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
1017   MutexLocker mu(SafepointSynchronize::is_at_safepoint() ? NULL : JvmtiThreadState_lock);
1018   JvmtiEventControllerPrivate::clear_to_frame_pop(ets, fpop);
1019 }
1020 
1021 void
1022 JvmtiEventController::change_field_watch(jvmtiEvent event_type, bool added) {
1023   MutexLocker mu(JvmtiThreadState_lock);
1024   JvmtiEventControllerPrivate::change_field_watch(event_type, added);
1025 }
1026 
1027 void
1028 JvmtiEventController::thread_started(JavaThread *thread) {
1029   // operates only on the current thread
1030   // JvmtiThreadState_lock grabbed only if needed.
1031   JvmtiEventControllerPrivate::thread_started(thread);
1032 }
1033 
1034 void
1035 JvmtiEventController::thread_ended(JavaThread *thread) {
1036   // operates only on the current thread
1037   // JvmtiThreadState_lock grabbed only if needed.
1038   JvmtiEventControllerPrivate::thread_ended(thread);
1039 }
1040 
1041 void
1042 JvmtiEventController::env_initialize(JvmtiEnvBase *env) {
1043   if (Threads::number_of_threads() == 0) {
1044     // during early VM start-up locks don't exist, but we are safely single threaded,
1045     // call the functionality without holding the JvmtiThreadState_lock.
1046     JvmtiEventControllerPrivate::env_initialize(env);
1047   } else {
1048     MutexLocker mu(JvmtiThreadState_lock);
1049     JvmtiEventControllerPrivate::env_initialize(env);
1050   }
1051 }
1052 
1053 void
1054 JvmtiEventController::env_dispose(JvmtiEnvBase *env) {
1055   if (Threads::number_of_threads() == 0) {
1056     // during early VM start-up locks don't exist, but we are safely single threaded,
1057     // call the functionality without holding the JvmtiThreadState_lock.
1058     JvmtiEventControllerPrivate::env_dispose(env);
1059   } else {
1060     MutexLocker mu(JvmtiThreadState_lock);
1061     JvmtiEventControllerPrivate::env_dispose(env);
1062   }
1063 }
1064 
1065 
1066 void
1067 JvmtiEventController::vm_start() {
1068   if (JvmtiEnvBase::environments_might_exist()) {
1069     MutexLocker mu(JvmtiThreadState_lock);
1070     JvmtiEventControllerPrivate::vm_start();
1071   }
1072 }
1073 
1074 void
1075 JvmtiEventController::vm_init() {
1076   if (JvmtiEnvBase::environments_might_exist()) {
1077     MutexLocker mu(JvmtiThreadState_lock);
1078     JvmtiEventControllerPrivate::vm_init();
1079   }
1080 }
1081 
1082 void
1083 JvmtiEventController::vm_death() {
1084   if (JvmtiEnvBase::environments_might_exist()) {
1085     MutexLocker mu(JvmtiThreadState_lock);
1086     JvmtiEventControllerPrivate::vm_death();
1087   }
1088 }