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