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