1 /*
   2  * Copyright (c) 2003, 2021, 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/jvmtiTagMap.hpp"
  35 #include "prims/jvmtiThreadState.inline.hpp"
  36 #include "runtime/deoptimization.hpp"
  37 #include "runtime/frame.inline.hpp"
  38 #include "runtime/stackFrameStream.inline.hpp"
  39 #include "runtime/thread.inline.hpp"
  40 #include "runtime/threadSMR.hpp"
  41 #include "runtime/vframe.hpp"
  42 #include "runtime/vframe_hp.hpp"
  43 #include "runtime/vmThread.hpp"
  44 #include "runtime/vmOperations.hpp"
  45 
  46 #ifdef JVMTI_TRACE
  47 #define EC_TRACE(out) do { \
  48   if (JvmtiTrace::trace_event_controller()) { \
  49     SafeResourceMark rm; \
  50     log_trace(jvmti) out; \
  51   } \
  52 } while (0)
  53 #else
  54 #define EC_TRACE(out)
  55 #endif /*JVMTI_TRACE */
  56 
  57 // bits for standard events
  58 
  59 static const jlong  SINGLE_STEP_BIT = (((jlong)1) << (JVMTI_EVENT_SINGLE_STEP - TOTAL_MIN_EVENT_TYPE_VAL));
  60 static const jlong  FRAME_POP_BIT = (((jlong)1) << (JVMTI_EVENT_FRAME_POP - TOTAL_MIN_EVENT_TYPE_VAL));
  61 static const jlong  BREAKPOINT_BIT = (((jlong)1) << (JVMTI_EVENT_BREAKPOINT - TOTAL_MIN_EVENT_TYPE_VAL));
  62 static const jlong  FIELD_ACCESS_BIT = (((jlong)1) << (JVMTI_EVENT_FIELD_ACCESS - TOTAL_MIN_EVENT_TYPE_VAL));
  63 static const jlong  FIELD_MODIFICATION_BIT = (((jlong)1) << (JVMTI_EVENT_FIELD_MODIFICATION - TOTAL_MIN_EVENT_TYPE_VAL));
  64 static const jlong  METHOD_ENTRY_BIT = (((jlong)1) << (JVMTI_EVENT_METHOD_ENTRY - TOTAL_MIN_EVENT_TYPE_VAL));
  65 static const jlong  METHOD_EXIT_BIT = (((jlong)1) << (JVMTI_EVENT_METHOD_EXIT - TOTAL_MIN_EVENT_TYPE_VAL));
  66 static const jlong  CLASS_FILE_LOAD_HOOK_BIT = (((jlong)1) << (JVMTI_EVENT_CLASS_FILE_LOAD_HOOK - TOTAL_MIN_EVENT_TYPE_VAL));
  67 static const jlong  NATIVE_METHOD_BIND_BIT = (((jlong)1) << (JVMTI_EVENT_NATIVE_METHOD_BIND - TOTAL_MIN_EVENT_TYPE_VAL));
  68 static const jlong  VM_START_BIT = (((jlong)1) << (JVMTI_EVENT_VM_START - TOTAL_MIN_EVENT_TYPE_VAL));
  69 static const jlong  VM_INIT_BIT = (((jlong)1) << (JVMTI_EVENT_VM_INIT - TOTAL_MIN_EVENT_TYPE_VAL));
  70 static const jlong  VM_DEATH_BIT = (((jlong)1) << (JVMTI_EVENT_VM_DEATH - TOTAL_MIN_EVENT_TYPE_VAL));
  71 static const jlong  CLASS_LOAD_BIT = (((jlong)1) << (JVMTI_EVENT_CLASS_LOAD - TOTAL_MIN_EVENT_TYPE_VAL));
  72 static const jlong  CLASS_PREPARE_BIT = (((jlong)1) << (JVMTI_EVENT_CLASS_PREPARE - TOTAL_MIN_EVENT_TYPE_VAL));
  73 static const jlong  THREAD_START_BIT = (((jlong)1) << (JVMTI_EVENT_THREAD_START - TOTAL_MIN_EVENT_TYPE_VAL));
  74 static const jlong  THREAD_END_BIT = (((jlong)1) << (JVMTI_EVENT_THREAD_END - TOTAL_MIN_EVENT_TYPE_VAL));
  75 static const jlong  EXCEPTION_THROW_BIT = (((jlong)1) << (JVMTI_EVENT_EXCEPTION - TOTAL_MIN_EVENT_TYPE_VAL));
  76 static const jlong  EXCEPTION_CATCH_BIT = (((jlong)1) << (JVMTI_EVENT_EXCEPTION_CATCH - TOTAL_MIN_EVENT_TYPE_VAL));
  77 static const jlong  MONITOR_CONTENDED_ENTER_BIT = (((jlong)1) << (JVMTI_EVENT_MONITOR_CONTENDED_ENTER - TOTAL_MIN_EVENT_TYPE_VAL));
  78 static const jlong  MONITOR_CONTENDED_ENTERED_BIT = (((jlong)1) << (JVMTI_EVENT_MONITOR_CONTENDED_ENTERED - TOTAL_MIN_EVENT_TYPE_VAL));
  79 static const jlong  MONITOR_WAIT_BIT = (((jlong)1) << (JVMTI_EVENT_MONITOR_WAIT - TOTAL_MIN_EVENT_TYPE_VAL));
  80 static const jlong  MONITOR_WAITED_BIT = (((jlong)1) << (JVMTI_EVENT_MONITOR_WAITED - TOTAL_MIN_EVENT_TYPE_VAL));
  81 static const jlong  DYNAMIC_CODE_GENERATED_BIT = (((jlong)1) << (JVMTI_EVENT_DYNAMIC_CODE_GENERATED - TOTAL_MIN_EVENT_TYPE_VAL));
  82 static const jlong  DATA_DUMP_BIT = (((jlong)1) << (JVMTI_EVENT_DATA_DUMP_REQUEST - TOTAL_MIN_EVENT_TYPE_VAL));
  83 static const jlong  COMPILED_METHOD_LOAD_BIT = (((jlong)1) << (JVMTI_EVENT_COMPILED_METHOD_LOAD - TOTAL_MIN_EVENT_TYPE_VAL));
  84 static const jlong  COMPILED_METHOD_UNLOAD_BIT = (((jlong)1) << (JVMTI_EVENT_COMPILED_METHOD_UNLOAD - TOTAL_MIN_EVENT_TYPE_VAL));
  85 static const jlong  GARBAGE_COLLECTION_START_BIT = (((jlong)1) << (JVMTI_EVENT_GARBAGE_COLLECTION_START - TOTAL_MIN_EVENT_TYPE_VAL));
  86 static const jlong  GARBAGE_COLLECTION_FINISH_BIT = (((jlong)1) << (JVMTI_EVENT_GARBAGE_COLLECTION_FINISH - TOTAL_MIN_EVENT_TYPE_VAL));
  87 static const jlong  OBJECT_FREE_BIT = (((jlong)1) << (JVMTI_EVENT_OBJECT_FREE - TOTAL_MIN_EVENT_TYPE_VAL));
  88 static const jlong  RESOURCE_EXHAUSTED_BIT = (((jlong)1) << (JVMTI_EVENT_RESOURCE_EXHAUSTED - TOTAL_MIN_EVENT_TYPE_VAL));
  89 static const jlong  VM_OBJECT_ALLOC_BIT = (((jlong)1) << (JVMTI_EVENT_VM_OBJECT_ALLOC - TOTAL_MIN_EVENT_TYPE_VAL));
  90 static const jlong  SAMPLED_OBJECT_ALLOC_BIT = (((jlong)1) << (JVMTI_EVENT_SAMPLED_OBJECT_ALLOC - TOTAL_MIN_EVENT_TYPE_VAL));


  91 
  92 // bits for extension events
  93 static const jlong  CLASS_UNLOAD_BIT = (((jlong)1) << (EXT_EVENT_CLASS_UNLOAD - TOTAL_MIN_EVENT_TYPE_VAL));


  94 
  95 

  96 static const jlong  MONITOR_BITS = MONITOR_CONTENDED_ENTER_BIT | MONITOR_CONTENDED_ENTERED_BIT |
  97                           MONITOR_WAIT_BIT | MONITOR_WAITED_BIT;
  98 static const jlong  EXCEPTION_BITS = EXCEPTION_THROW_BIT | EXCEPTION_CATCH_BIT;
  99 static const jlong  INTERP_EVENT_BITS =  SINGLE_STEP_BIT | METHOD_ENTRY_BIT | METHOD_EXIT_BIT |
 100                                 FRAME_POP_BIT | FIELD_ACCESS_BIT | FIELD_MODIFICATION_BIT;
 101 static const jlong  THREAD_FILTERED_EVENT_BITS = INTERP_EVENT_BITS | EXCEPTION_BITS | MONITOR_BITS |
 102                                         BREAKPOINT_BIT | CLASS_LOAD_BIT | CLASS_PREPARE_BIT | THREAD_END_BIT |
 103                                         SAMPLED_OBJECT_ALLOC_BIT;
 104 static const jlong  NEED_THREAD_LIFE_EVENTS = THREAD_FILTERED_EVENT_BITS | THREAD_START_BIT;
 105 static const jlong  EARLY_EVENT_BITS = CLASS_FILE_LOAD_HOOK_BIT | CLASS_LOAD_BIT | CLASS_PREPARE_BIT |
 106                                VM_START_BIT | VM_INIT_BIT | VM_DEATH_BIT | NATIVE_METHOD_BIND_BIT |
 107                                THREAD_START_BIT | THREAD_END_BIT |
 108                                COMPILED_METHOD_LOAD_BIT | COMPILED_METHOD_UNLOAD_BIT |
 109                                DYNAMIC_CODE_GENERATED_BIT;
 110 static const jlong  GLOBAL_EVENT_BITS = ~THREAD_FILTERED_EVENT_BITS;
 111 static const jlong  SHOULD_POST_ON_EXCEPTIONS_BITS = EXCEPTION_BITS | METHOD_EXIT_BIT | FRAME_POP_BIT;
 112 
 113 ///////////////////////////////////////////////////////////////
 114 //
 115 // JvmtiEventEnabled
 116 //
 117 
 118 JvmtiEventEnabled::JvmtiEventEnabled() {
 119   clear();
 120 }
 121 
 122 
 123 void JvmtiEventEnabled::clear() {
 124   _enabled_bits = 0;
 125 #ifndef PRODUCT
 126   _init_guard = JEE_INIT_GUARD;
 127 #endif
 128 }
 129 
 130 void JvmtiEventEnabled::set_enabled(jvmtiEvent event_type, bool enabled) {
 131   jlong bits = get_bits();
 132   jlong mask = bit_for(event_type);
 133   if (enabled) {
 134     bits |= mask;
 135   } else {
 136     bits &= ~mask;
 137   }
 138   set_bits(bits);
 139 }
 140 
 141 
 142 ///////////////////////////////////////////////////////////////
 143 //
 144 // JvmtiEnvThreadEventEnable
 145 //
 146 
 147 JvmtiEnvThreadEventEnable::JvmtiEnvThreadEventEnable() {
 148   _event_user_enabled.clear();
 149   _event_enabled.clear();
 150 }
 151 
 152 
 153 JvmtiEnvThreadEventEnable::~JvmtiEnvThreadEventEnable() {
 154   _event_user_enabled.clear();
 155   _event_enabled.clear();
 156 }
 157 
 158 
 159 ///////////////////////////////////////////////////////////////
 160 //
 161 // JvmtiThreadEventEnable
 162 //
 163 
 164 JvmtiThreadEventEnable::JvmtiThreadEventEnable() {
 165   _event_enabled.clear();
 166 }
 167 
 168 
 169 JvmtiThreadEventEnable::~JvmtiThreadEventEnable() {
 170   _event_enabled.clear();
 171 }
 172 
 173 
 174 ///////////////////////////////////////////////////////////////
 175 //
 176 // JvmtiEnvEventEnable
 177 //
 178 
 179 JvmtiEnvEventEnable::JvmtiEnvEventEnable() {
 180   _event_user_enabled.clear();
 181   _event_callback_enabled.clear();
 182   _event_enabled.clear();
 183 }
 184 
 185 
 186 JvmtiEnvEventEnable::~JvmtiEnvEventEnable() {
 187   _event_user_enabled.clear();
 188   _event_callback_enabled.clear();
 189   _event_enabled.clear();
 190 }
 191 
 192 
 193 ///////////////////////////////////////////////////////////////
 194 //
 195 // EnterInterpOnlyModeClosure
 196 //
 197 
 198 class EnterInterpOnlyModeClosure : public HandshakeClosure {
 199  private:
 200   bool _completed;
 201  public:
 202   EnterInterpOnlyModeClosure() : HandshakeClosure("EnterInterpOnlyMode"), _completed(false) { }
 203   void do_thread(Thread* th) {
 204     JavaThread* jt = JavaThread::cast(th);
 205     JvmtiThreadState* state = jt->jvmti_thread_state();
 206 
 207     // Set up the current stack depth for later tracking
 208     state->invalidate_cur_stack_depth();
 209 
 210     state->enter_interp_only_mode();
 211 





 212     if (jt->has_last_Java_frame()) {
 213       // If running in fullspeed mode, single stepping is implemented
 214       // as follows: first, the interpreter does not dispatch to
 215       // compiled code for threads that have single stepping enabled;
 216       // second, we deoptimize all compiled java frames on the thread's stack when
 217       // interpreted-only mode is enabled the first time for a given
 218       // thread (nothing to do if no Java frames yet).
 219       ResourceMark resMark;
 220       for (StackFrameStream fst(jt, false /* update */, false /* process_frames */); !fst.is_done(); fst.next()) {
 221         if (fst.current()->can_be_deoptimized()) {
 222           Deoptimization::deoptimize(jt, *fst.current());
 223         }
 224       }
 225     }
 226     _completed = true;
 227   }
 228   bool completed() {
 229     return _completed;
 230   }
 231 };
 232 
 233 
 234 ///////////////////////////////////////////////////////////////
 235 //
 236 // VM_ChangeSingleStep
 237 //
 238 
 239 class VM_ChangeSingleStep : public VM_Operation {
 240 private:
 241   bool _on;
 242 
 243 public:
 244   VM_ChangeSingleStep(bool on);
 245   VMOp_Type type() const                         { return VMOp_ChangeSingleStep; }
 246   bool allow_nested_vm_operations() const        { return true; }
 247   void doit();   // method definition is after definition of JvmtiEventControllerPrivate because of scoping
 248 };
 249 
 250 
 251 VM_ChangeSingleStep::VM_ChangeSingleStep(bool on)
 252   : _on(on)
 253 {
 254 }
 255 
 256 
 257 
 258 
 259 ///////////////////////////////////////////////////////////////
 260 //
 261 // JvmtiEventControllerPrivate
 262 //
 263 // Private internal implementation methods for JvmtiEventController.
 264 //
 265 // These methods are thread safe either because they are called
 266 // in early VM initialization which is single threaded, or they
 267 // hold the JvmtiThreadState_lock.
 268 //
 269 
 270 class JvmtiEventControllerPrivate : public AllStatic {
 271   static bool _initialized;
 272 public:
 273   static void set_should_post_single_step(bool on);
 274   static void enter_interp_only_mode(JvmtiThreadState *state);
 275   static void leave_interp_only_mode(JvmtiThreadState *state);
 276   static void recompute_enabled();
 277   static jlong recompute_env_enabled(JvmtiEnvBase* env);
 278   static jlong recompute_env_thread_enabled(JvmtiEnvThreadState* ets, JvmtiThreadState* state);
 279   static jlong recompute_thread_enabled(JvmtiThreadState *state);
 280   static void event_init();
 281 
 282   static void set_user_enabled(JvmtiEnvBase *env, JavaThread *thread,
 283                         jvmtiEvent event_type, bool enabled);
 284   static void set_event_callbacks(JvmtiEnvBase *env,
 285                                   const jvmtiEventCallbacks* callbacks,
 286                                   jint size_of_callbacks);
 287 
 288   static void set_extension_event_callback(JvmtiEnvBase *env,
 289                                            jint extension_event_index,
 290                                            jvmtiExtensionEvent callback);
 291 
 292   static void set_frame_pop(JvmtiEnvThreadState *env_thread, JvmtiFramePop fpop);
 293   static void clear_frame_pop(JvmtiEnvThreadState *env_thread, JvmtiFramePop fpop);
 294   static void clear_to_frame_pop(JvmtiEnvThreadState *env_thread, JvmtiFramePop fpop);
 295   static void change_field_watch(jvmtiEvent event_type, bool added);
 296 
 297   static void thread_started(JavaThread *thread);
 298   static void thread_ended(JavaThread *thread);
 299 
 300   static void env_initialize(JvmtiEnvBase *env);
 301   static void env_dispose(JvmtiEnvBase *env);
 302 
 303   static void vm_start();
 304   static void vm_init();
 305   static void vm_death();
 306 
 307   static void trace_changed(JvmtiThreadState *state, jlong now_enabled, jlong changed);
 308   static void trace_changed(jlong now_enabled, jlong changed);
 309 
 310   static void flush_object_free_events(JvmtiEnvBase *env);
 311   static void set_enabled_events_with_lock(JvmtiEnvBase *env, jlong now_enabled);
 312 };
 313 
 314 bool JvmtiEventControllerPrivate::_initialized = false;
 315 
 316 void JvmtiEventControllerPrivate::set_should_post_single_step(bool on) {
 317   // we have permission to do this, VM op doesn't
 318   JvmtiExport::set_should_post_single_step(on);
 319 }
 320 
 321 
 322 // When _on == true, we use the safepoint interpreter dispatch table
 323 // to allow us to find the single step points. Otherwise, we switch
 324 // back to the regular interpreter dispatch table.
 325 // Note: We call Interpreter::notice_safepoints() and ignore_safepoints()
 326 // in a VM_Operation to safely make the dispatch table switch. We
 327 // no longer rely on the safepoint mechanism to do any of this work
 328 // for us.
 329 void VM_ChangeSingleStep::doit() {
 330   log_debug(interpreter, safepoint)("changing single step to '%s'", _on ? "on" : "off");
 331   JvmtiEventControllerPrivate::set_should_post_single_step(_on);
 332   if (_on) {
 333     Interpreter::notice_safepoints();
 334   } else {
 335     Interpreter::ignore_safepoints();
 336   }
 337 }
 338 
 339 
 340 void JvmtiEventControllerPrivate::enter_interp_only_mode(JvmtiThreadState *state) {
 341   EC_TRACE(("[%s] # Entering interpreter only mode",
 342             JvmtiTrace::safe_get_thread_name(state->get_thread())));
 343   EnterInterpOnlyModeClosure hs;
 344   JavaThread *target = state->get_thread();
 345   Thread *current = Thread::current();
 346   if (target->is_handshake_safe_for(current)) {







 347     hs.do_thread(target);
 348   } else {
 349     Handshake::execute(&hs, target);
 350     guarantee(hs.completed(), "Handshake failed: Target thread is not alive?");
 351   }
 352 }
 353 
 354 
 355 void
 356 JvmtiEventControllerPrivate::leave_interp_only_mode(JvmtiThreadState *state) {
 357   EC_TRACE(("[%s] # Leaving interpreter only mode",
 358             JvmtiTrace::safe_get_thread_name(state->get_thread())));
 359   state->leave_interp_only_mode();
 360 }
 361 
 362 
 363 void
 364 JvmtiEventControllerPrivate::trace_changed(JvmtiThreadState *state, jlong now_enabled, jlong changed) {
 365 #ifdef JVMTI_TRACE
 366   if (JvmtiTrace::trace_event_controller()) {
 367     SafeResourceMark rm;
 368     // traces standard events only
 369     for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {
 370       jlong bit = JvmtiEventEnabled::bit_for((jvmtiEvent)ei);
 371       if (changed & bit) {
 372         // it changed, print it
 373          log_trace(jvmti)("[%s] # %s event %s",
 374                       JvmtiTrace::safe_get_thread_name(state->get_thread()),
 375                       (now_enabled & bit)? "Enabling" : "Disabling", JvmtiTrace::event_name((jvmtiEvent)ei));
 376       }
 377     }
 378   }
 379 #endif /*JVMTI_TRACE */
 380 }
 381 
 382 
 383 void
 384 JvmtiEventControllerPrivate::trace_changed(jlong now_enabled, jlong changed) {
 385 #ifdef JVMTI_TRACE
 386   if (JvmtiTrace::trace_event_controller()) {
 387     SafeResourceMark rm;
 388     // traces standard events only
 389     for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {
 390       jlong bit = JvmtiEventEnabled::bit_for((jvmtiEvent)ei);
 391       if (changed & bit) {
 392         // it changed, print it
 393          log_trace(jvmti)("[-] # %s event %s",
 394                       (now_enabled & bit)? "Enabling" : "Disabling", JvmtiTrace::event_name((jvmtiEvent)ei));
 395       }
 396     }
 397   }
 398 #endif /*JVMTI_TRACE */
 399 }
 400 
 401 
 402 void
 403 JvmtiEventControllerPrivate::flush_object_free_events(JvmtiEnvBase* env) {
 404   // Some of the objects recorded by this env may have died.  If we're
 405   // (potentially) changing the enable state for ObjectFree events, we
 406   // need to ensure the env is cleaned up and any events that should
 407   // be posted are posted.
 408   JvmtiTagMap* tag_map = env->tag_map_acquire();
 409   if (tag_map != NULL) {
 410     tag_map->flush_object_free_events();
 411   }
 412 }
 413 
 414 void
 415 JvmtiEventControllerPrivate::set_enabled_events_with_lock(JvmtiEnvBase* env, jlong now_enabled) {
 416   // The state for ObjectFree events must be enabled or disabled
 417   // under the TagMap lock, to allow pending object posting events to complete.
 418   JvmtiTagMap* tag_map = env->tag_map_acquire();
 419   if (tag_map != NULL) {
 420     MutexLocker ml(tag_map->lock(), Mutex::_no_safepoint_check_flag);
 421     env->env_event_enable()->_event_enabled.set_bits(now_enabled);
 422   } else {
 423     env->env_event_enable()->_event_enabled.set_bits(now_enabled);
 424   }
 425 }
 426 
 427 // For the specified env: compute the currently truly enabled events
 428 // set external state accordingly.
 429 // Return value and set value must include all events.
 430 // But outside this class, only non-thread-filtered events can be queried..
 431 jlong
 432 JvmtiEventControllerPrivate::recompute_env_enabled(JvmtiEnvBase* env) {
 433   jlong was_enabled = env->env_event_enable()->_event_enabled.get_bits();
 434   jlong now_enabled =
 435     env->env_event_enable()->_event_callback_enabled.get_bits() &
 436     env->env_event_enable()->_event_user_enabled.get_bits();
 437 
 438   switch (env->phase()) {
 439   case JVMTI_PHASE_PRIMORDIAL:
 440   case JVMTI_PHASE_ONLOAD:
 441     // only these events allowed in primordial or onload phase
 442     now_enabled &= (EARLY_EVENT_BITS & ~THREAD_FILTERED_EVENT_BITS);
 443     break;
 444   case JVMTI_PHASE_START:
 445     // only these events allowed in start phase
 446     now_enabled &= EARLY_EVENT_BITS;
 447     break;
 448   case JVMTI_PHASE_LIVE:
 449     // all events allowed during live phase
 450     break;
 451   case JVMTI_PHASE_DEAD:
 452     // no events allowed when dead
 453     now_enabled = 0;
 454     break;
 455   default:
 456     assert(false, "no other phases - sanity check");
 457     break;
 458   }
 459 
 460   // Set/reset the event enabled under the tagmap lock.
 461   set_enabled_events_with_lock(env, now_enabled);
 462 
 463   trace_changed(now_enabled, (now_enabled ^ was_enabled)  & ~THREAD_FILTERED_EVENT_BITS);
 464 
 465   return now_enabled;
 466 }
 467 
 468 
 469 // For the specified env and thread: compute the currently truly enabled events
 470 // set external state accordingly.  Only thread-filtered events are included.
 471 jlong
 472 JvmtiEventControllerPrivate::recompute_env_thread_enabled(JvmtiEnvThreadState* ets, JvmtiThreadState* state) {
 473   JvmtiEnv *env = ets->get_env();
 474 
 475   jlong was_enabled = ets->event_enable()->_event_enabled.get_bits();
 476   jlong now_enabled =  THREAD_FILTERED_EVENT_BITS &
 477     env->env_event_enable()->_event_callback_enabled.get_bits() &
 478     (env->env_event_enable()->_event_user_enabled.get_bits() |
 479      ets->event_enable()->_event_user_enabled.get_bits());
 480 
 481   // for frame pops and field watchs, computed enabled state
 482   // is only true if an event has been requested
 483   if (!ets->has_frame_pops()) {
 484     now_enabled &= ~FRAME_POP_BIT;
 485   }
 486   if (*((int *)JvmtiExport::get_field_access_count_addr()) == 0) {
 487     now_enabled &= ~FIELD_ACCESS_BIT;
 488   }
 489   if (*((int *)JvmtiExport::get_field_modification_count_addr()) == 0) {
 490     now_enabled &= ~FIELD_MODIFICATION_BIT;
 491   }
 492 
 493   switch (JvmtiEnv::get_phase()) {
 494   case JVMTI_PHASE_DEAD:
 495     // no events allowed when dead
 496     now_enabled = 0;
 497     break;
 498   default:
 499     break;
 500   }
 501 
 502   // if anything changed do update
 503   if (now_enabled != was_enabled) {
 504 
 505     // will we really send these events to this thread x env
 506     ets->event_enable()->_event_enabled.set_bits(now_enabled);
 507 
 508     // If the enabled status of the single step or breakpoint events changed,
 509     // the location status may need to change as well.
 510     jlong changed = now_enabled ^ was_enabled;
 511     if (changed & SINGLE_STEP_BIT) {
 512       ets->reset_current_location(JVMTI_EVENT_SINGLE_STEP, (now_enabled & SINGLE_STEP_BIT) != 0);
 513     }
 514     if (changed & BREAKPOINT_BIT) {
 515       ets->reset_current_location(JVMTI_EVENT_BREAKPOINT,  (now_enabled & BREAKPOINT_BIT) != 0);
 516     }
 517     trace_changed(state, now_enabled, changed);
 518   }
 519   return now_enabled;
 520 }
 521 
 522 
 523 // For the specified thread: compute the currently truly enabled events
 524 // set external state accordingly.  Only thread-filtered events are included.
 525 jlong
 526 JvmtiEventControllerPrivate::recompute_thread_enabled(JvmtiThreadState *state) {
 527   if (state == NULL) {
 528     // associated JavaThread is exiting
 529     return (jlong)0;
 530   }
 531 
 532   julong was_any_env_enabled = state->thread_event_enable()->_event_enabled.get_bits();
 533   julong any_env_enabled = 0;
 534   // JVMTI_EVENT_FRAME_POP can be disabled (in the case FRAME_POP_BIT is not set),
 535   // but we need to set interp_only if some JvmtiEnvThreadState has frame pop set
 536   // to clear the request
 537   bool has_frame_pops = false;
 538 
 539   {
 540     // This iteration will include JvmtiEnvThreadStates whose environments
 541     // have been disposed.  These JvmtiEnvThreadStates must not be filtered
 542     // as recompute must be called on them to disable their events,
 543     JvmtiEnvThreadStateIterator it(state);
 544     for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
 545       any_env_enabled |= recompute_env_thread_enabled(ets, state);
 546       has_frame_pops |= ets->has_frame_pops();
 547     }
 548   }
 549 
 550   if (any_env_enabled != was_any_env_enabled) {
 551     // mark if event is truly enabled on this thread in any environment
 552     state->thread_event_enable()->_event_enabled.set_bits(any_env_enabled);
 553 
 554     // update the JavaThread cached value for thread-specific should_post_on_exceptions value
 555     bool should_post_on_exceptions = (any_env_enabled & SHOULD_POST_ON_EXCEPTIONS_BITS) != 0;
 556     state->set_should_post_on_exceptions(should_post_on_exceptions);






 557   }
 558 
 559   // compute interp_only mode
 560   bool should_be_interp = (any_env_enabled & INTERP_EVENT_BITS) != 0 || has_frame_pops;
 561   bool is_now_interp = state->is_interp_only_mode();
 562 
 563   if (should_be_interp != is_now_interp) {
 564     if (should_be_interp) {
 565       enter_interp_only_mode(state);
 566     } else {
 567       leave_interp_only_mode(state);
 568     }
 569   }
 570 





 571   return any_env_enabled;
 572 }
 573 
 574 
 575 // Compute truly enabled events - meaning if the event can and could be
 576 // sent.  An event is truly enabled if it is user enabled on the thread
 577 // or globally user enabled, but only if there is a callback or event hook
 578 // for it and, for field watch and frame pop, one has been set.
 579 // Compute if truly enabled, per thread, per environment, per combination
 580 // (thread x environment), and overall.  These merges are true if any is true.
 581 // True per thread if some environment has callback set and the event is globally
 582 // enabled or enabled for this thread.
 583 // True per environment if the callback is set and the event is globally
 584 // enabled in this environment or enabled for any thread in this environment.
 585 // True per combination if the environment has the callback set and the
 586 // event is globally enabled in this environment or the event is enabled
 587 // for this thread and environment.
 588 //
 589 // All states transitions dependent on these transitions are also handled here.
 590 void
 591 JvmtiEventControllerPrivate::recompute_enabled() {
 592   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
 593 
 594   // event enabled for any thread in any environment
 595   julong was_any_env_thread_enabled = JvmtiEventController::_universal_global_event_enabled.get_bits();
 596   julong any_env_thread_enabled = 0;
 597 
 598   EC_TRACE(("[-] # recompute enabled - before " JULONG_FORMAT_X, was_any_env_thread_enabled));
 599 
 600   // compute non-thread-filters events.
 601   // This must be done separately from thread-filtered events, since some
 602   // events can occur before any threads exist.
 603   JvmtiEnvIterator it;
 604   for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) {
 605     any_env_thread_enabled |= recompute_env_enabled(env);
 606   }
 607 
 608   // We need to create any missing jvmti_thread_state if there are globally set thread
 609   // filtered events and there weren't last time
 610   if (    (any_env_thread_enabled & THREAD_FILTERED_EVENT_BITS) != 0 &&
 611       (was_any_env_thread_enabled & THREAD_FILTERED_EVENT_BITS) == 0) {
 612     for (JavaThreadIteratorWithHandle jtiwh; JavaThread *tp = jtiwh.next(); ) {
 613       // state_for_while_locked() makes tp->is_exiting() check
 614       JvmtiThreadState::state_for_while_locked(tp);  // create the thread state if missing








 615     }
 616   }
 617 
 618   // compute and set thread-filtered events
 619   JvmtiThreadState *state = JvmtiThreadState::first();
 620   if (state != nullptr) {
 621     // If we have a JvmtiThreadState, then we've reached the point where
 622     // threads can exist so create a ThreadsListHandle to protect them.
 623     // The held JvmtiThreadState_lock prevents exiting JavaThreads from
 624     // being removed from the JvmtiThreadState list we're about to walk
 625     // so this ThreadsListHandle exists just to satisfy the lower level sanity
 626     // checks that the target JavaThreads are protected.
 627     ThreadsListHandle tlh;
 628     for (; state != nullptr; state = state->next()) {
 629       any_env_thread_enabled |= recompute_thread_enabled(state);
 630     }
 631   }
 632 
 633   // set universal state (across all envs and threads)
 634   jlong delta = any_env_thread_enabled ^ was_any_env_thread_enabled;
 635   if (delta != 0) {
 636     JvmtiExport::set_should_post_field_access((any_env_thread_enabled & FIELD_ACCESS_BIT) != 0);
 637     JvmtiExport::set_should_post_field_modification((any_env_thread_enabled & FIELD_MODIFICATION_BIT) != 0);
 638     JvmtiExport::set_should_post_class_load((any_env_thread_enabled & CLASS_LOAD_BIT) != 0);
 639     JvmtiExport::set_should_post_class_file_load_hook((any_env_thread_enabled & CLASS_FILE_LOAD_HOOK_BIT) != 0);
 640     JvmtiExport::set_should_post_native_method_bind((any_env_thread_enabled & NATIVE_METHOD_BIND_BIT) != 0);
 641     JvmtiExport::set_should_post_dynamic_code_generated((any_env_thread_enabled & DYNAMIC_CODE_GENERATED_BIT) != 0);
 642     JvmtiExport::set_should_post_data_dump((any_env_thread_enabled & DATA_DUMP_BIT) != 0);
 643     JvmtiExport::set_should_post_class_prepare((any_env_thread_enabled & CLASS_PREPARE_BIT) != 0);
 644     JvmtiExport::set_should_post_class_unload((any_env_thread_enabled & CLASS_UNLOAD_BIT) != 0);
 645     JvmtiExport::set_should_post_monitor_contended_enter((any_env_thread_enabled & MONITOR_CONTENDED_ENTER_BIT) != 0);
 646     JvmtiExport::set_should_post_monitor_contended_entered((any_env_thread_enabled & MONITOR_CONTENDED_ENTERED_BIT) != 0);
 647     JvmtiExport::set_should_post_monitor_wait((any_env_thread_enabled & MONITOR_WAIT_BIT) != 0);
 648     JvmtiExport::set_should_post_monitor_waited((any_env_thread_enabled & MONITOR_WAITED_BIT) != 0);
 649     JvmtiExport::set_should_post_garbage_collection_start((any_env_thread_enabled & GARBAGE_COLLECTION_START_BIT) != 0);
 650     JvmtiExport::set_should_post_garbage_collection_finish((any_env_thread_enabled & GARBAGE_COLLECTION_FINISH_BIT) != 0);
 651     JvmtiExport::set_should_post_object_free((any_env_thread_enabled & OBJECT_FREE_BIT) != 0);
 652     JvmtiExport::set_should_post_resource_exhausted((any_env_thread_enabled & RESOURCE_EXHAUSTED_BIT) != 0);
 653     JvmtiExport::set_should_post_compiled_method_load((any_env_thread_enabled & COMPILED_METHOD_LOAD_BIT) != 0);
 654     JvmtiExport::set_should_post_compiled_method_unload((any_env_thread_enabled & COMPILED_METHOD_UNLOAD_BIT) != 0);
 655     JvmtiExport::set_should_post_vm_object_alloc((any_env_thread_enabled & VM_OBJECT_ALLOC_BIT) != 0);
 656     JvmtiExport::set_should_post_sampled_object_alloc((any_env_thread_enabled & SAMPLED_OBJECT_ALLOC_BIT) != 0);




 657 
 658     // need this if we want thread events or we need them to init data
 659     JvmtiExport::set_should_post_thread_life((any_env_thread_enabled & NEED_THREAD_LIFE_EVENTS) != 0);
 660 
 661     // If single stepping is turned on or off, execute the VM op to change it.
 662     if (delta & SINGLE_STEP_BIT) {
 663       switch (JvmtiEnv::get_phase()) {
 664       case JVMTI_PHASE_DEAD:
 665         // If the VM is dying we can't execute VM ops
 666         break;
 667       case JVMTI_PHASE_LIVE: {
 668         VM_ChangeSingleStep op((any_env_thread_enabled & SINGLE_STEP_BIT) != 0);
 669         VMThread::execute(&op);
 670         break;
 671       }
 672       default:
 673         assert(false, "should never come here before live phase");
 674         break;
 675       }
 676     }
 677 
 678     // set global truly enabled, that is, any thread in any environment
 679     JvmtiEventController::_universal_global_event_enabled.set_bits(any_env_thread_enabled);
 680 
 681     // set global should_post_on_exceptions
 682     JvmtiExport::set_should_post_on_exceptions((any_env_thread_enabled & SHOULD_POST_ON_EXCEPTIONS_BITS) != 0);
 683 
 684   }
 685 
 686   EC_TRACE(("[-] # recompute enabled - after " JULONG_FORMAT_X, any_env_thread_enabled));
 687 }
 688 
 689 
 690 void
 691 JvmtiEventControllerPrivate::thread_started(JavaThread *thread) {
 692   assert(thread == Thread::current(), "must be current thread");
 693   assert(JvmtiEnvBase::environments_might_exist(), "to enter event controller, JVM TI environments must exist");
 694 
 695   EC_TRACE(("[%s] # thread started", JvmtiTrace::safe_get_thread_name(thread)));
 696 
 697   // if we have any thread filtered events globally enabled, create/update the thread state
 698   if ((JvmtiEventController::_universal_global_event_enabled.get_bits() & THREAD_FILTERED_EVENT_BITS) != 0) {
 699     MutexLocker mu(JvmtiThreadState_lock);
 700     // create the thread state if missing
 701     JvmtiThreadState *state = JvmtiThreadState::state_for_while_locked(thread);
 702     if (state != NULL) {    // skip threads with no JVMTI thread state
 703       recompute_thread_enabled(state);
 704     }
 705   }
 706 }
 707 
 708 
 709 void
 710 JvmtiEventControllerPrivate::thread_ended(JavaThread *thread) {
 711   // Removes the JvmtiThreadState associated with the specified thread.
 712   // May be called after all environments have been disposed.
 713   assert(JvmtiThreadState_lock->is_locked(), "sanity check");
 714 
 715   EC_TRACE(("[%s] # thread ended", JvmtiTrace::safe_get_thread_name(thread)));
 716 
 717   JvmtiThreadState *state = thread->jvmti_thread_state();
 718   assert(state != NULL, "else why are we here?");
 719   delete state;
 720 }
 721 
 722 void JvmtiEventControllerPrivate::set_event_callbacks(JvmtiEnvBase *env,
 723                                                       const jvmtiEventCallbacks* callbacks,
 724                                                       jint size_of_callbacks) {
 725   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
 726   EC_TRACE(("[*] # set event callbacks"));
 727 
 728   // May be changing the event handler for ObjectFree.
 729   flush_object_free_events(env);
 730 
 731   env->set_event_callbacks(callbacks, size_of_callbacks);
 732   jlong enabled_bits = 0;

 733   for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {
 734     jvmtiEvent evt_t = (jvmtiEvent)ei;
 735     if (env->has_callback(evt_t)) {
 736       enabled_bits |= JvmtiEventEnabled::bit_for(evt_t);
 737     }
 738   }
 739   env->env_event_enable()->_event_callback_enabled.set_bits(enabled_bits);
 740   recompute_enabled();
 741 }
 742 
 743 void
 744 JvmtiEventControllerPrivate::set_extension_event_callback(JvmtiEnvBase *env,
 745                                                           jint extension_event_index,
 746                                                           jvmtiExtensionEvent callback)
 747 {
 748   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
 749   EC_TRACE(("[*] # set extension event callback"));
 750 
 751   // extension events are allocated below JVMTI_MIN_EVENT_TYPE_VAL
 752   assert(extension_event_index >= (jint)EXT_MIN_EVENT_TYPE_VAL &&
 753          extension_event_index <= (jint)EXT_MAX_EVENT_TYPE_VAL, "sanity check");
 754 
 755 
 756   // As the bits for both standard (jvmtiEvent) and extension
 757   // (jvmtiExtEvents) are stored in the same word we cast here to
 758   // jvmtiEvent to set/clear the bit for this extension event.
 759   jvmtiEvent event_type = (jvmtiEvent)extension_event_index;
 760 
 761   // Prevent a possible race condition where events are re-enabled by a call to
 762   // set event callbacks, where the DisposeEnvironment occurs after the boiler-plate
 763   // environment check and before the lock is acquired.
 764   // We can safely do the is_valid check now, as JvmtiThreadState_lock is held.
 765   bool enabling = (callback != NULL) && (env->is_valid());
 766   env->env_event_enable()->set_user_enabled(event_type, enabling);
 767 
 768   // update the callback
 769   jvmtiExtEventCallbacks* ext_callbacks = env->ext_callbacks();
 770   switch (extension_event_index) {
 771     case EXT_EVENT_CLASS_UNLOAD :
 772       ext_callbacks->ClassUnload = callback;
 773       break;






 774     default:
 775       ShouldNotReachHere();
 776   }
 777 
 778   // update the callback enable/disable bit
 779   jlong enabled_bits = env->env_event_enable()->_event_callback_enabled.get_bits();
 780   jlong bit_for = JvmtiEventEnabled::bit_for(event_type);
 781   if (enabling) {
 782     enabled_bits |= bit_for;
 783   } else {
 784     enabled_bits &= ~bit_for;
 785   }
 786   env->env_event_enable()->_event_callback_enabled.set_bits(enabled_bits);
 787 
 788   recompute_enabled();
 789 }
 790 
 791 
 792 void
 793 JvmtiEventControllerPrivate::env_initialize(JvmtiEnvBase *env) {
 794   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
 795   EC_TRACE(("[*] # env initialize"));
 796 
 797   if (JvmtiEnvBase::is_vm_live()) {
 798     // if we didn't initialize event info already (this is a late
 799     // launched environment), do it now.
 800     event_init();
 801   }
 802 
 803   env->initialize();
 804 
 805   // add the JvmtiEnvThreadState to each JvmtiThreadState
 806   for (JvmtiThreadState *state = JvmtiThreadState::first(); state != NULL; state = state->next()) {
 807     state->add_env(env);
 808     assert((JvmtiEnv*)(state->env_thread_state(env)->get_env()) == env, "sanity check");
 809   }
 810   JvmtiEventControllerPrivate::recompute_enabled();
 811 }
 812 
 813 
 814 void
 815 JvmtiEventControllerPrivate::env_dispose(JvmtiEnvBase *env) {
 816   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
 817   EC_TRACE(("[*] # env dispose"));
 818 
 819   // Before the environment is marked disposed, disable all events on this
 820   // environment (by zapping the callbacks).  As a result, the disposed
 821   // environment will not call event handlers.
 822   set_event_callbacks(env, NULL, 0);
 823   for (jint extension_event_index = EXT_MIN_EVENT_TYPE_VAL;
 824        extension_event_index <= EXT_MAX_EVENT_TYPE_VAL;
 825        ++extension_event_index) {
 826     set_extension_event_callback(env, extension_event_index, NULL);
 827   }
 828 
 829   // Let the environment finish disposing itself.
 830   env->env_dispose();
 831 }
 832 
 833 
 834 void
 835 JvmtiEventControllerPrivate::set_user_enabled(JvmtiEnvBase *env, JavaThread *thread,
 836                                           jvmtiEvent event_type, bool enabled) {
 837   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
 838 
 839   EC_TRACE(("[%s] # user %s event %s",
 840             thread==NULL? "ALL": JvmtiTrace::safe_get_thread_name(thread),
 841             enabled? "enabled" : "disabled", JvmtiTrace::event_name(event_type)));
 842 
 843   if (event_type == JVMTI_EVENT_OBJECT_FREE) {
 844     flush_object_free_events(env);
 845   }
 846 
 847   if (thread == NULL) {
 848     env->env_event_enable()->set_user_enabled(event_type, enabled);
 849   } else {
 850     // create the thread state (if it didn't exist before)
 851     JvmtiThreadState *state = JvmtiThreadState::state_for_while_locked(thread);
 852     if (state != NULL) {
 853       state->env_thread_state(env)->event_enable()->set_user_enabled(event_type, enabled);
 854     }
 855   }
 856   recompute_enabled();
 857 }
 858 
 859 
 860 void
 861 JvmtiEventControllerPrivate::set_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
 862   EC_TRACE(("[%s] # set frame pop - frame=%d",
 863             JvmtiTrace::safe_get_thread_name(ets->get_thread()),
 864             fpop.frame_number() ));
 865 
 866   ets->get_frame_pops()->set(fpop);
 867   recompute_thread_enabled(ets->get_thread()->jvmti_thread_state());
 868 }
 869 
 870 
 871 void
 872 JvmtiEventControllerPrivate::clear_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
 873   EC_TRACE(("[%s] # clear frame pop - frame=%d",
 874             JvmtiTrace::safe_get_thread_name(ets->get_thread()),
 875             fpop.frame_number() ));
 876 
 877   ets->get_frame_pops()->clear(fpop);
 878   recompute_thread_enabled(ets->get_thread()->jvmti_thread_state());
 879 }
 880 
 881 
 882 void
 883 JvmtiEventControllerPrivate::clear_to_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
 884   int cleared_cnt = ets->get_frame_pops()->clear_to(fpop);
 885 
 886   EC_TRACE(("[%s] # clear to frame pop - frame=%d, count=%d",
 887             JvmtiTrace::safe_get_thread_name(ets->get_thread()),
 888             fpop.frame_number(),
 889             cleared_cnt ));
 890 
 891   if (cleared_cnt > 0) {
 892     recompute_thread_enabled(ets->get_thread()->jvmti_thread_state());
 893   }
 894 }
 895 
 896 void
 897 JvmtiEventControllerPrivate::change_field_watch(jvmtiEvent event_type, bool added) {
 898   int *count_addr;
 899 
 900   switch (event_type) {
 901   case JVMTI_EVENT_FIELD_MODIFICATION:
 902     count_addr = (int *)JvmtiExport::get_field_modification_count_addr();
 903     break;
 904   case JVMTI_EVENT_FIELD_ACCESS:
 905     count_addr = (int *)JvmtiExport::get_field_access_count_addr();
 906     break;
 907   default:
 908     assert(false, "incorrect event");
 909     return;
 910   }
 911 
 912   EC_TRACE(("[-] # change field watch - %s %s count=%d",
 913             event_type==JVMTI_EVENT_FIELD_MODIFICATION? "modification" : "access",
 914             added? "add" : "remove",
 915             *count_addr));
 916 
 917   if (added) {
 918     (*count_addr)++;
 919     if (*count_addr == 1) {
 920       recompute_enabled();
 921     }
 922   } else {
 923     if (*count_addr > 0) {
 924       (*count_addr)--;
 925       if (*count_addr == 0) {
 926         recompute_enabled();
 927       }
 928     } else {
 929       assert(false, "field watch out of phase");
 930     }
 931   }
 932 }
 933 
 934 void
 935 JvmtiEventControllerPrivate::event_init() {
 936   assert(JvmtiThreadState_lock->is_locked(), "sanity check");
 937 
 938   if (_initialized) {
 939     return;
 940   }
 941 
 942   EC_TRACE(("[-] # VM live"));
 943 
 944 #ifdef ASSERT
 945   // check that our idea and the spec's idea of threaded events match
 946   for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {
 947     jlong bit = JvmtiEventEnabled::bit_for((jvmtiEvent)ei);
 948     assert(((THREAD_FILTERED_EVENT_BITS & bit) != 0) == JvmtiUtil::event_threaded(ei),
 949            "thread filtered event list does not match");
 950   }
 951 #endif
 952 
 953   _initialized = true;
 954 }
 955 
 956 void
 957 JvmtiEventControllerPrivate::vm_start() {
 958   // some events are now able to be enabled (phase has changed)
 959   JvmtiEventControllerPrivate::recompute_enabled();
 960 }
 961 
 962 
 963 void
 964 JvmtiEventControllerPrivate::vm_init() {
 965   event_init();
 966 
 967   // all the events are now able to be enabled (phase has changed)
 968   JvmtiEventControllerPrivate::recompute_enabled();
 969 }
 970 
 971 
 972 void
 973 JvmtiEventControllerPrivate::vm_death() {
 974   // events are disabled (phase has changed)
 975   JvmtiEventControllerPrivate::recompute_enabled();
 976 }
 977 
 978 
 979 ///////////////////////////////////////////////////////////////
 980 //
 981 // JvmtiEventController
 982 //
 983 
 984 JvmtiEventEnabled JvmtiEventController::_universal_global_event_enabled;
 985 
 986 bool
 987 JvmtiEventController::is_global_event(jvmtiEvent event_type) {
 988   assert(is_valid_event_type(event_type), "invalid event type");
 989   jlong bit_for = ((jlong)1) << (event_type - TOTAL_MIN_EVENT_TYPE_VAL);
 990   return((bit_for & GLOBAL_EVENT_BITS)!=0);
 991 }
 992 
 993 void
 994 JvmtiEventController::set_user_enabled(JvmtiEnvBase *env, JavaThread *thread, jvmtiEvent event_type, bool enabled) {

 995   if (Threads::number_of_threads() == 0) {
 996     // during early VM start-up locks don't exist, but we are safely single threaded,
 997     // call the functionality without holding the JvmtiThreadState_lock.
 998     JvmtiEventControllerPrivate::set_user_enabled(env, thread, event_type, enabled);
 999   } else {



1000     MutexLocker mu(JvmtiThreadState_lock);
1001     JvmtiEventControllerPrivate::set_user_enabled(env, thread, event_type, enabled);
1002   }
1003 }
1004 
1005 
1006 void
1007 JvmtiEventController::set_event_callbacks(JvmtiEnvBase *env,
1008                                           const jvmtiEventCallbacks* callbacks,
1009                                           jint size_of_callbacks) {
1010   if (Threads::number_of_threads() == 0) {
1011     // during early VM start-up locks don't exist, but we are safely single threaded,
1012     // call the functionality without holding the JvmtiThreadState_lock.
1013     JvmtiEventControllerPrivate::set_event_callbacks(env, callbacks, size_of_callbacks);
1014   } else {
1015     MutexLocker mu(JvmtiThreadState_lock);
1016     JvmtiEventControllerPrivate::set_event_callbacks(env, callbacks, size_of_callbacks);
1017   }
1018 }
1019 
1020 void
1021 JvmtiEventController::set_extension_event_callback(JvmtiEnvBase *env,
1022                                                    jint extension_event_index,
1023                                                    jvmtiExtensionEvent callback) {
1024   if (Threads::number_of_threads() == 0) {
1025     JvmtiEventControllerPrivate::set_extension_event_callback(env, extension_event_index, callback);
1026   } else {
1027     MutexLocker mu(JvmtiThreadState_lock);
1028     JvmtiEventControllerPrivate::set_extension_event_callback(env, extension_event_index, callback);
1029   }
1030 }
1031 
1032 void
1033 JvmtiEventController::set_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
1034   assert(JvmtiThreadState_lock->is_locked(), "Must be locked.");
1035   JvmtiEventControllerPrivate::set_frame_pop(ets, fpop);
1036 }
1037 
1038 void
1039 JvmtiEventController::clear_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
1040   assert(JvmtiThreadState_lock->is_locked(), "Must be locked.");
1041   JvmtiEventControllerPrivate::clear_frame_pop(ets, fpop);
1042 }
1043 
1044 void
1045 JvmtiEventController::change_field_watch(jvmtiEvent event_type, bool added) {
1046   MutexLocker mu(JvmtiThreadState_lock);
1047   JvmtiEventControllerPrivate::change_field_watch(event_type, added);
1048 }
1049 
1050 void
1051 JvmtiEventController::thread_started(JavaThread *thread) {
1052   // operates only on the current thread
1053   // JvmtiThreadState_lock grabbed only if needed.
1054   JvmtiEventControllerPrivate::thread_started(thread);
1055 }
1056 
1057 void
1058 JvmtiEventController::thread_ended(JavaThread *thread) {
1059   // operates only on the current thread
1060   // JvmtiThreadState_lock grabbed only if needed.
1061   JvmtiEventControllerPrivate::thread_ended(thread);
1062 }
1063 
1064 void
1065 JvmtiEventController::env_initialize(JvmtiEnvBase *env) {
1066   if (Threads::number_of_threads() == 0) {
1067     // during early VM start-up locks don't exist, but we are safely single threaded,
1068     // call the functionality without holding the JvmtiThreadState_lock.
1069     JvmtiEventControllerPrivate::env_initialize(env);
1070   } else {
1071     MutexLocker mu(JvmtiThreadState_lock);
1072     JvmtiEventControllerPrivate::env_initialize(env);
1073   }
1074 }
1075 
1076 void
1077 JvmtiEventController::env_dispose(JvmtiEnvBase *env) {
1078   if (Threads::number_of_threads() == 0) {
1079     // during early VM start-up locks don't exist, but we are safely single threaded,
1080     // call the functionality without holding the JvmtiThreadState_lock.
1081     JvmtiEventControllerPrivate::env_dispose(env);
1082   } else {
1083     MutexLocker mu(JvmtiThreadState_lock);
1084     JvmtiEventControllerPrivate::env_dispose(env);
1085   }
1086 }
1087 
1088 
1089 void
1090 JvmtiEventController::vm_start() {
1091   if (JvmtiEnvBase::environments_might_exist()) {
1092     MutexLocker mu(JvmtiThreadState_lock);
1093     JvmtiEventControllerPrivate::vm_start();
1094   }
1095 }
1096 
1097 void
1098 JvmtiEventController::vm_init() {
1099   if (JvmtiEnvBase::environments_might_exist()) {
1100     MutexLocker mu(JvmtiThreadState_lock);
1101     JvmtiEventControllerPrivate::vm_init();
1102   }
1103 }
1104 
1105 void
1106 JvmtiEventController::vm_death() {
1107   if (JvmtiEnvBase::environments_might_exist()) {
1108     MutexLocker mu(JvmtiThreadState_lock);
1109     JvmtiEventControllerPrivate::vm_death();
1110   }
1111 }
--- EOF ---