1 /*
   2  * Copyright (c) 2003, 2017, 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 
 512   {
 513     // This iteration will include JvmtiEnvThreadStates whoses environments
 514     // have been disposed.  These JvmtiEnvThreadStates must not be filtered
 515     // as recompute must be called on them to disable their events,
 516     JvmtiEnvThreadStateIterator it(state);
 517     for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
 518       any_env_enabled |= recompute_env_thread_enabled(ets, state);
 519     }
 520   }
 521 
 522   if (any_env_enabled != was_any_env_enabled) {
 523     // mark if event is truly enabled on this thread in any environment
 524     state->thread_event_enable()->_event_enabled.set_bits(any_env_enabled);
 525 
 526     // compute interp_only mode
 527     bool should_be_interp = (any_env_enabled & INTERP_EVENT_BITS) != 0;
 528     bool is_now_interp = state->is_interp_only_mode();
 529 
 530     if (should_be_interp != is_now_interp) {
 531       if (should_be_interp) {
 532         enter_interp_only_mode(state);
 533       } else {
 534         leave_interp_only_mode(state);
 535       }
 536     }
 537 
 538     // update the JavaThread cached value for thread-specific should_post_on_exceptions value
 539     bool should_post_on_exceptions = (any_env_enabled & SHOULD_POST_ON_EXCEPTIONS_BITS) != 0;
 540     state->set_should_post_on_exceptions(should_post_on_exceptions);
 541   }
 542 
 543   return any_env_enabled;
 544 }
 545 
 546 
 547 // Compute truly enabled events - meaning if the event can and could be
 548 // sent.  An event is truly enabled if it is user enabled on the thread
 549 // or globally user enabled, but only if there is a callback or event hook
 550 // for it and, for field watch and frame pop, one has been set.
 551 // Compute if truly enabled, per thread, per environment, per combination
 552 // (thread x environment), and overall.  These merges are true if any is true.
 553 // True per thread if some environment has callback set and the event is globally
 554 // enabled or enabled for this thread.
 555 // True per environment if the callback is set and the event is globally
 556 // enabled in this environment or enabled for any thread in this environment.
 557 // True per combination if the environment has the callback set and the
 558 // event is globally enabled in this environment or the event is enabled
 559 // for this thread and environment.
 560 //
 561 // All states transitions dependent on these transitions are also handled here.
 562 void
 563 JvmtiEventControllerPrivate::recompute_enabled() {
 564   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
 565 
 566   // event enabled for any thread in any environment
 567   julong was_any_env_thread_enabled = JvmtiEventController::_universal_global_event_enabled.get_bits();
 568   julong any_env_thread_enabled = 0;
 569 
 570   EC_TRACE(("[-] # recompute enabled - before " JULONG_FORMAT_X, was_any_env_thread_enabled));
 571 
 572   // compute non-thread-filters events.
 573   // This must be done separately from thread-filtered events, since some
 574   // events can occur before any threads exist.
 575   JvmtiEnvIterator it;
 576   for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) {
 577     any_env_thread_enabled |= recompute_env_enabled(env);
 578   }
 579 
 580   // We need to create any missing jvmti_thread_state if there are globally set thread
 581   // filtered events and there weren't last time
 582   if (    (any_env_thread_enabled & THREAD_FILTERED_EVENT_BITS) != 0 &&
 583       (was_any_env_thread_enabled & THREAD_FILTERED_EVENT_BITS) == 0) {
 584     for (JavaThreadIteratorWithHandle jtiwh; JavaThread *tp = jtiwh.next(); ) {
 585       // state_for_while_locked() makes tp->is_exiting() check
 586       JvmtiThreadState::state_for_while_locked(tp);  // create the thread state if missing
 587     }
 588   }
 589 
 590   // compute and set thread-filtered events
 591   for (JvmtiThreadState *state = JvmtiThreadState::first(); state != NULL; state = state->next()) {
 592     any_env_thread_enabled |= recompute_thread_enabled(state);
 593   }
 594 
 595   // set universal state (across all envs and threads)
 596   jlong delta = any_env_thread_enabled ^ was_any_env_thread_enabled;
 597   if (delta != 0) {
 598     JvmtiExport::set_should_post_field_access((any_env_thread_enabled & FIELD_ACCESS_BIT) != 0);
 599     JvmtiExport::set_should_post_field_modification((any_env_thread_enabled & FIELD_MODIFICATION_BIT) != 0);
 600     JvmtiExport::set_should_post_class_load((any_env_thread_enabled & CLASS_LOAD_BIT) != 0);
 601     JvmtiExport::set_should_post_class_file_load_hook((any_env_thread_enabled & CLASS_FILE_LOAD_HOOK_BIT) != 0);
 602     JvmtiExport::set_should_post_native_method_bind((any_env_thread_enabled & NATIVE_METHOD_BIND_BIT) != 0);
 603     JvmtiExport::set_should_post_dynamic_code_generated((any_env_thread_enabled & DYNAMIC_CODE_GENERATED_BIT) != 0);
 604     JvmtiExport::set_should_post_data_dump((any_env_thread_enabled & DATA_DUMP_BIT) != 0);
 605     JvmtiExport::set_should_post_class_prepare((any_env_thread_enabled & CLASS_PREPARE_BIT) != 0);
 606     JvmtiExport::set_should_post_class_unload((any_env_thread_enabled & CLASS_UNLOAD_BIT) != 0);
 607     JvmtiExport::set_should_post_monitor_contended_enter((any_env_thread_enabled & MONITOR_CONTENDED_ENTER_BIT) != 0);
 608     JvmtiExport::set_should_post_monitor_contended_entered((any_env_thread_enabled & MONITOR_CONTENDED_ENTERED_BIT) != 0);
 609     JvmtiExport::set_should_post_monitor_wait((any_env_thread_enabled & MONITOR_WAIT_BIT) != 0);
 610     JvmtiExport::set_should_post_monitor_waited((any_env_thread_enabled & MONITOR_WAITED_BIT) != 0);
 611     JvmtiExport::set_should_post_garbage_collection_start((any_env_thread_enabled & GARBAGE_COLLECTION_START_BIT) != 0);
 612     JvmtiExport::set_should_post_garbage_collection_finish((any_env_thread_enabled & GARBAGE_COLLECTION_FINISH_BIT) != 0);
 613     JvmtiExport::set_should_post_object_free((any_env_thread_enabled & OBJECT_FREE_BIT) != 0);
 614     JvmtiExport::set_should_post_resource_exhausted((any_env_thread_enabled & RESOURCE_EXHAUSTED_BIT) != 0);
 615     JvmtiExport::set_should_post_compiled_method_load((any_env_thread_enabled & COMPILED_METHOD_LOAD_BIT) != 0);
 616     JvmtiExport::set_should_post_compiled_method_unload((any_env_thread_enabled & COMPILED_METHOD_UNLOAD_BIT) != 0);
 617     JvmtiExport::set_should_post_vm_object_alloc((any_env_thread_enabled & VM_OBJECT_ALLOC_BIT) != 0);
 618 
 619     // need this if we want thread events or we need them to init data
 620     JvmtiExport::set_should_post_thread_life((any_env_thread_enabled & NEED_THREAD_LIFE_EVENTS) != 0);
 621 
 622     // If single stepping is turned on or off, execute the VM op to change it.
 623     if (delta & SINGLE_STEP_BIT) {
 624       switch (JvmtiEnv::get_phase()) {
 625       case JVMTI_PHASE_DEAD:
 626         // If the VM is dying we can't execute VM ops
 627         break;
 628       case JVMTI_PHASE_LIVE: {
 629         VM_ChangeSingleStep op((any_env_thread_enabled & SINGLE_STEP_BIT) != 0);
 630         VMThread::execute(&op);
 631         break;
 632       }
 633       default:
 634         assert(false, "should never come here before live phase");
 635         break;
 636       }
 637     }
 638 
 639     // set global truly enabled, that is, any thread in any environment
 640     JvmtiEventController::_universal_global_event_enabled.set_bits(any_env_thread_enabled);
 641 
 642     // set global should_post_on_exceptions
 643     JvmtiExport::set_should_post_on_exceptions((any_env_thread_enabled & SHOULD_POST_ON_EXCEPTIONS_BITS) != 0);
 644 
 645   }
 646 
 647   EC_TRACE(("[-] # recompute enabled - after " JULONG_FORMAT_X, any_env_thread_enabled));
 648 }
 649 
 650 
 651 void
 652 JvmtiEventControllerPrivate::thread_started(JavaThread *thread) {
 653   assert(thread->is_Java_thread(), "Must be JavaThread");
 654   assert(thread == Thread::current(), "must be current thread");
 655   assert(JvmtiEnvBase::environments_might_exist(), "to enter event controller, JVM TI environments must exist");
 656 
 657   EC_TRACE(("[%s] # thread started", JvmtiTrace::safe_get_thread_name(thread)));
 658 
 659   // if we have any thread filtered events globally enabled, create/update the thread state
 660   if ((JvmtiEventController::_universal_global_event_enabled.get_bits() & THREAD_FILTERED_EVENT_BITS) != 0) {
 661     MutexLocker mu(JvmtiThreadState_lock);
 662     // create the thread state if missing
 663     JvmtiThreadState *state = JvmtiThreadState::state_for_while_locked(thread);
 664     if (state != NULL) {    // skip threads with no JVMTI thread state
 665       recompute_thread_enabled(state);
 666     }
 667   }
 668 }
 669 
 670 
 671 void
 672 JvmtiEventControllerPrivate::thread_ended(JavaThread *thread) {
 673   // Removes the JvmtiThreadState associated with the specified thread.
 674   // May be called after all environments have been disposed.
 675   assert(JvmtiThreadState_lock->is_locked(), "sanity check");
 676 
 677   EC_TRACE(("[%s] # thread ended", JvmtiTrace::safe_get_thread_name(thread)));
 678 
 679   JvmtiThreadState *state = thread->jvmti_thread_state();
 680   assert(state != NULL, "else why are we here?");
 681   delete state;
 682 }
 683 
 684 void JvmtiEventControllerPrivate::set_event_callbacks(JvmtiEnvBase *env,
 685                                                       const jvmtiEventCallbacks* callbacks,
 686                                                       jint size_of_callbacks) {
 687   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
 688   EC_TRACE(("[*] # set event callbacks"));
 689 
 690   env->set_event_callbacks(callbacks, size_of_callbacks);
 691   jlong enabled_bits = 0;
 692   for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {
 693     jvmtiEvent evt_t = (jvmtiEvent)ei;
 694     if (env->has_callback(evt_t)) {
 695       enabled_bits |= JvmtiEventEnabled::bit_for(evt_t);
 696     }
 697   }
 698   env->env_event_enable()->_event_callback_enabled.set_bits(enabled_bits);
 699   recompute_enabled();
 700 }
 701 
 702 void
 703 JvmtiEventControllerPrivate::set_extension_event_callback(JvmtiEnvBase *env,
 704                                                           jint extension_event_index,
 705                                                           jvmtiExtensionEvent callback)
 706 {
 707   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
 708   EC_TRACE(("[*] # set extension event callback"));
 709 
 710   // extension events are allocated below JVMTI_MIN_EVENT_TYPE_VAL
 711   assert(extension_event_index >= (jint)EXT_MIN_EVENT_TYPE_VAL &&
 712          extension_event_index <= (jint)EXT_MAX_EVENT_TYPE_VAL, "sanity check");
 713 
 714 
 715   // As the bits for both standard (jvmtiEvent) and extension
 716   // (jvmtiExtEvents) are stored in the same word we cast here to
 717   // jvmtiEvent to set/clear the bit for this extension event.
 718   jvmtiEvent event_type = (jvmtiEvent)extension_event_index;
 719 
 720   // Prevent a possible race condition where events are re-enabled by a call to
 721   // set event callbacks, where the DisposeEnvironment occurs after the boiler-plate
 722   // environment check and before the lock is acquired.
 723   // We can safely do the is_valid check now, as JvmtiThreadState_lock is held.
 724   bool enabling = (callback != NULL) && (env->is_valid());
 725   env->env_event_enable()->set_user_enabled(event_type, enabling);
 726 
 727   // update the callback
 728   jvmtiExtEventCallbacks* ext_callbacks = env->ext_callbacks();
 729   switch (extension_event_index) {
 730     case EXT_EVENT_CLASS_UNLOAD :
 731       ext_callbacks->ClassUnload = callback;
 732       break;
 733     default:
 734       ShouldNotReachHere();
 735   }
 736 
 737   // update the callback enable/disable bit
 738   jlong enabled_bits = env->env_event_enable()->_event_callback_enabled.get_bits();
 739   jlong bit_for = JvmtiEventEnabled::bit_for(event_type);
 740   if (enabling) {
 741     enabled_bits |= bit_for;
 742   } else {
 743     enabled_bits &= ~bit_for;
 744   }
 745   env->env_event_enable()->_event_callback_enabled.set_bits(enabled_bits);
 746 
 747   recompute_enabled();
 748 }
 749 
 750 
 751 void
 752 JvmtiEventControllerPrivate::env_initialize(JvmtiEnvBase *env) {
 753   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
 754   EC_TRACE(("[*] # env initialize"));
 755 
 756   if (JvmtiEnvBase::is_vm_live()) {
 757     // if we didn't initialize event info already (this is a late
 758     // launched environment), do it now.
 759     event_init();
 760   }
 761 
 762   env->initialize();
 763 
 764   // add the JvmtiEnvThreadState to each JvmtiThreadState
 765   for (JvmtiThreadState *state = JvmtiThreadState::first(); state != NULL; state = state->next()) {
 766     state->add_env(env);
 767     assert((JvmtiEnv*)(state->env_thread_state(env)->get_env()) == env, "sanity check");
 768   }
 769   JvmtiEventControllerPrivate::recompute_enabled();
 770 }
 771 
 772 
 773 void
 774 JvmtiEventControllerPrivate::env_dispose(JvmtiEnvBase *env) {
 775   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
 776   EC_TRACE(("[*] # env dispose"));
 777 
 778   // Before the environment is marked disposed, disable all events on this
 779   // environment (by zapping the callbacks).  As a result, the disposed
 780   // environment will not call event handlers.
 781   set_event_callbacks(env, NULL, 0);
 782   for (jint extension_event_index = EXT_MIN_EVENT_TYPE_VAL;
 783        extension_event_index <= EXT_MAX_EVENT_TYPE_VAL;
 784        ++extension_event_index) {
 785     set_extension_event_callback(env, extension_event_index, NULL);
 786   }
 787 
 788   // Let the environment finish disposing itself.
 789   env->env_dispose();
 790 }
 791 
 792 
 793 void
 794 JvmtiEventControllerPrivate::set_user_enabled(JvmtiEnvBase *env, JavaThread *thread,
 795                                           jvmtiEvent event_type, bool enabled) {
 796   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
 797 
 798   EC_TRACE(("[%s] # user %s event %s",
 799             thread==NULL? "ALL": JvmtiTrace::safe_get_thread_name(thread),
 800             enabled? "enabled" : "disabled", JvmtiTrace::event_name(event_type)));
 801 
 802   if (thread == NULL) {
 803     env->env_event_enable()->set_user_enabled(event_type, enabled);
 804   } else {
 805     // create the thread state (if it didn't exist before)
 806     JvmtiThreadState *state = JvmtiThreadState::state_for_while_locked(thread);
 807     if (state != NULL) {
 808       state->env_thread_state(env)->event_enable()->set_user_enabled(event_type, enabled);
 809     }
 810   }
 811   recompute_enabled();
 812 }
 813 
 814 
 815 void
 816 JvmtiEventControllerPrivate::set_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
 817   EC_TRACE(("[%s] # set frame pop - frame=%d",
 818             JvmtiTrace::safe_get_thread_name(ets->get_thread()),
 819             fpop.frame_number() ));
 820 
 821   ets->get_frame_pops()->set(fpop);
 822   recompute_thread_enabled(ets->get_thread()->jvmti_thread_state());
 823 }
 824 
 825 
 826 void
 827 JvmtiEventControllerPrivate::clear_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
 828   EC_TRACE(("[%s] # clear frame pop - frame=%d",
 829             JvmtiTrace::safe_get_thread_name(ets->get_thread()),
 830             fpop.frame_number() ));
 831 
 832   ets->get_frame_pops()->clear(fpop);
 833   recompute_thread_enabled(ets->get_thread()->jvmti_thread_state());
 834 }
 835 
 836 
 837 void
 838 JvmtiEventControllerPrivate::clear_to_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
 839   int cleared_cnt = ets->get_frame_pops()->clear_to(fpop);
 840 
 841   EC_TRACE(("[%s] # clear to frame pop - frame=%d, count=%d",
 842             JvmtiTrace::safe_get_thread_name(ets->get_thread()),
 843             fpop.frame_number(),
 844             cleared_cnt ));
 845 
 846   if (cleared_cnt > 0) {
 847     recompute_thread_enabled(ets->get_thread()->jvmti_thread_state());
 848   }
 849 }
 850 
 851 void
 852 JvmtiEventControllerPrivate::change_field_watch(jvmtiEvent event_type, bool added) {
 853   int *count_addr;
 854 
 855   switch (event_type) {
 856   case JVMTI_EVENT_FIELD_MODIFICATION:
 857     count_addr = (int *)JvmtiExport::get_field_modification_count_addr();
 858     break;
 859   case JVMTI_EVENT_FIELD_ACCESS:
 860     count_addr = (int *)JvmtiExport::get_field_access_count_addr();
 861     break;
 862   default:
 863     assert(false, "incorrect event");
 864     return;
 865   }
 866 
 867   EC_TRACE(("[-] # change field watch - %s %s count=%d",
 868             event_type==JVMTI_EVENT_FIELD_MODIFICATION? "modification" : "access",
 869             added? "add" : "remove",
 870             *count_addr));
 871 
 872   if (added) {
 873     (*count_addr)++;
 874     if (*count_addr == 1) {
 875       recompute_enabled();
 876     }
 877   } else {
 878     if (*count_addr > 0) {
 879       (*count_addr)--;
 880       if (*count_addr == 0) {
 881         recompute_enabled();
 882       }
 883     } else {
 884       assert(false, "field watch out of phase");
 885     }
 886   }
 887 }
 888 
 889 void
 890 JvmtiEventControllerPrivate::event_init() {
 891   assert(JvmtiThreadState_lock->is_locked(), "sanity check");
 892 
 893   if (_initialized) {
 894     return;
 895   }
 896 
 897   EC_TRACE(("[-] # VM live"));
 898 
 899 #ifdef ASSERT
 900   // check that our idea and the spec's idea of threaded events match
 901   for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {
 902     jlong bit = JvmtiEventEnabled::bit_for((jvmtiEvent)ei);
 903     assert(((THREAD_FILTERED_EVENT_BITS & bit) != 0) == JvmtiUtil::event_threaded(ei),
 904            "thread filtered event list does not match");
 905   }
 906 #endif
 907 
 908   _initialized = true;
 909 }
 910 
 911 void
 912 JvmtiEventControllerPrivate::vm_start() {
 913   // some events are now able to be enabled (phase has changed)
 914   JvmtiEventControllerPrivate::recompute_enabled();
 915 }
 916 
 917 
 918 void
 919 JvmtiEventControllerPrivate::vm_init() {
 920   event_init();
 921 
 922   // all the events are now able to be enabled (phase has changed)
 923   JvmtiEventControllerPrivate::recompute_enabled();
 924 }
 925 
 926 
 927 void
 928 JvmtiEventControllerPrivate::vm_death() {
 929   // events are disabled (phase has changed)
 930   JvmtiEventControllerPrivate::recompute_enabled();
 931 }
 932 
 933 
 934 ///////////////////////////////////////////////////////////////
 935 //
 936 // JvmtiEventController
 937 //
 938 
 939 JvmtiEventEnabled JvmtiEventController::_universal_global_event_enabled;
 940 
 941 bool
 942 JvmtiEventController::is_global_event(jvmtiEvent event_type) {
 943   assert(is_valid_event_type(event_type), "invalid event type");
 944   jlong bit_for = ((jlong)1) << (event_type - TOTAL_MIN_EVENT_TYPE_VAL);
 945   return((bit_for & GLOBAL_EVENT_BITS)!=0);
 946 }
 947 
 948 void
 949 JvmtiEventController::set_user_enabled(JvmtiEnvBase *env, JavaThread *thread, jvmtiEvent event_type, bool enabled) {
 950   if (Threads::number_of_threads() == 0) {
 951     // during early VM start-up locks don't exist, but we are safely single threaded,
 952     // call the functionality without holding the JvmtiThreadState_lock.
 953     JvmtiEventControllerPrivate::set_user_enabled(env, thread, event_type, enabled);
 954   } else {
 955     MutexLocker mu(JvmtiThreadState_lock);
 956     JvmtiEventControllerPrivate::set_user_enabled(env, thread, event_type, enabled);
 957   }
 958 }
 959 
 960 
 961 void
 962 JvmtiEventController::set_event_callbacks(JvmtiEnvBase *env,
 963                                           const jvmtiEventCallbacks* callbacks,
 964                                           jint size_of_callbacks) {
 965   if (Threads::number_of_threads() == 0) {
 966     // during early VM start-up locks don't exist, but we are safely single threaded,
 967     // call the functionality without holding the JvmtiThreadState_lock.
 968     JvmtiEventControllerPrivate::set_event_callbacks(env, callbacks, size_of_callbacks);
 969   } else {
 970     MutexLocker mu(JvmtiThreadState_lock);
 971     JvmtiEventControllerPrivate::set_event_callbacks(env, callbacks, size_of_callbacks);
 972   }
 973 }
 974 
 975 void
 976 JvmtiEventController::set_extension_event_callback(JvmtiEnvBase *env,
 977                                                    jint extension_event_index,
 978                                                    jvmtiExtensionEvent callback) {
 979   if (Threads::number_of_threads() == 0) {
 980     JvmtiEventControllerPrivate::set_extension_event_callback(env, extension_event_index, callback);
 981   } else {
 982     MutexLocker mu(JvmtiThreadState_lock);
 983     JvmtiEventControllerPrivate::set_extension_event_callback(env, extension_event_index, callback);
 984   }
 985 }
 986 
 987 
 988 
 989 
 990 void
 991 JvmtiEventController::set_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
 992   MutexLockerEx mu(SafepointSynchronize::is_at_safepoint() ? NULL : JvmtiThreadState_lock);
 993   JvmtiEventControllerPrivate::set_frame_pop(ets, fpop);
 994 }
 995 
 996 
 997 void
 998 JvmtiEventController::clear_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
 999   MutexLockerEx mu(SafepointSynchronize::is_at_safepoint() ? NULL : JvmtiThreadState_lock);
1000   JvmtiEventControllerPrivate::clear_frame_pop(ets, fpop);
1001 }
1002 
1003 
1004 void
1005 JvmtiEventController::clear_to_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
1006   MutexLockerEx mu(SafepointSynchronize::is_at_safepoint() ? NULL : JvmtiThreadState_lock);
1007   JvmtiEventControllerPrivate::clear_to_frame_pop(ets, fpop);
1008 }
1009 
1010 void
1011 JvmtiEventController::change_field_watch(jvmtiEvent event_type, bool added) {
1012   MutexLocker mu(JvmtiThreadState_lock);
1013   JvmtiEventControllerPrivate::change_field_watch(event_type, added);
1014 }
1015 
1016 void
1017 JvmtiEventController::thread_started(JavaThread *thread) {
1018   // operates only on the current thread
1019   // JvmtiThreadState_lock grabbed only if needed.
1020   JvmtiEventControllerPrivate::thread_started(thread);
1021 }
1022 
1023 void
1024 JvmtiEventController::thread_ended(JavaThread *thread) {
1025   // operates only on the current thread
1026   // JvmtiThreadState_lock grabbed only if needed.
1027   JvmtiEventControllerPrivate::thread_ended(thread);
1028 }
1029 
1030 void
1031 JvmtiEventController::env_initialize(JvmtiEnvBase *env) {
1032   if (Threads::number_of_threads() == 0) {
1033     // during early VM start-up locks don't exist, but we are safely single threaded,
1034     // call the functionality without holding the JvmtiThreadState_lock.
1035     JvmtiEventControllerPrivate::env_initialize(env);
1036   } else {
1037     MutexLocker mu(JvmtiThreadState_lock);
1038     JvmtiEventControllerPrivate::env_initialize(env);
1039   }
1040 }
1041 
1042 void
1043 JvmtiEventController::env_dispose(JvmtiEnvBase *env) {
1044   if (Threads::number_of_threads() == 0) {
1045     // during early VM start-up locks don't exist, but we are safely single threaded,
1046     // call the functionality without holding the JvmtiThreadState_lock.
1047     JvmtiEventControllerPrivate::env_dispose(env);
1048   } else {
1049     MutexLocker mu(JvmtiThreadState_lock);
1050     JvmtiEventControllerPrivate::env_dispose(env);
1051   }
1052 }
1053 
1054 
1055 void
1056 JvmtiEventController::vm_start() {
1057   if (JvmtiEnvBase::environments_might_exist()) {
1058     MutexLocker mu(JvmtiThreadState_lock);
1059     JvmtiEventControllerPrivate::vm_start();
1060   }
1061 }
1062 
1063 void
1064 JvmtiEventController::vm_init() {
1065   if (JvmtiEnvBase::environments_might_exist()) {
1066     MutexLocker mu(JvmtiThreadState_lock);
1067     JvmtiEventControllerPrivate::vm_init();
1068   }
1069 }
1070 
1071 void
1072 JvmtiEventController::vm_death() {
1073   if (JvmtiEnvBase::environments_might_exist()) {
1074     MutexLocker mu(JvmtiThreadState_lock);
1075     JvmtiEventControllerPrivate::vm_death();
1076   }
1077 }