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