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