1 /*
   2  * Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/javaClasses.hpp"
  27 #include "interpreter/interpreter.hpp"
  28 #include "jvmtifiles/jvmtiEnv.hpp"
  29 #include "logging/log.hpp"
  30 #include "memory/resourceArea.hpp"
  31 #include "prims/jvmtiEventController.hpp"
  32 #include "prims/jvmtiEventController.inline.hpp"
  33 #include "prims/jvmtiExport.hpp"
  34 #include "prims/jvmtiImpl.hpp"
  35 #include "prims/jvmtiTagMap.hpp"
  36 #include "prims/jvmtiThreadState.inline.hpp"
  37 #include "runtime/deoptimization.hpp"
  38 #include "runtime/frame.inline.hpp"
  39 #include "runtime/stackFrameStream.inline.hpp"
  40 #include "runtime/thread.inline.hpp"
  41 #include "runtime/threadSMR.hpp"
  42 #include "runtime/vframe.hpp"
  43 #include "runtime/vframe_hp.hpp"
  44 #include "runtime/vmThread.hpp"
  45 #include "runtime/vmOperations.hpp"
  46 
  47 #ifdef JVMTI_TRACE
  48 #define EC_TRACE(out) do { \
  49   if (JvmtiTrace::trace_event_controller()) { \
  50     SafeResourceMark rm; \
  51     log_trace(jvmti) out; \
  52   } \
  53 } while (0)
  54 #else
  55 #define EC_TRACE(out)
  56 #endif /*JVMTI_TRACE */
  57 
  58 // bits for standard events
  59 
  60 static const jlong  SINGLE_STEP_BIT = (((jlong)1) << (JVMTI_EVENT_SINGLE_STEP - TOTAL_MIN_EVENT_TYPE_VAL));
  61 static const jlong  FRAME_POP_BIT = (((jlong)1) << (JVMTI_EVENT_FRAME_POP - TOTAL_MIN_EVENT_TYPE_VAL));
  62 static const jlong  BREAKPOINT_BIT = (((jlong)1) << (JVMTI_EVENT_BREAKPOINT - TOTAL_MIN_EVENT_TYPE_VAL));
  63 static const jlong  FIELD_ACCESS_BIT = (((jlong)1) << (JVMTI_EVENT_FIELD_ACCESS - TOTAL_MIN_EVENT_TYPE_VAL));
  64 static const jlong  FIELD_MODIFICATION_BIT = (((jlong)1) << (JVMTI_EVENT_FIELD_MODIFICATION - TOTAL_MIN_EVENT_TYPE_VAL));
  65 static const jlong  METHOD_ENTRY_BIT = (((jlong)1) << (JVMTI_EVENT_METHOD_ENTRY - TOTAL_MIN_EVENT_TYPE_VAL));
  66 static const jlong  METHOD_EXIT_BIT = (((jlong)1) << (JVMTI_EVENT_METHOD_EXIT - TOTAL_MIN_EVENT_TYPE_VAL));
  67 static const jlong  CLASS_FILE_LOAD_HOOK_BIT = (((jlong)1) << (JVMTI_EVENT_CLASS_FILE_LOAD_HOOK - TOTAL_MIN_EVENT_TYPE_VAL));
  68 static const jlong  NATIVE_METHOD_BIND_BIT = (((jlong)1) << (JVMTI_EVENT_NATIVE_METHOD_BIND - TOTAL_MIN_EVENT_TYPE_VAL));
  69 static const jlong  VM_START_BIT = (((jlong)1) << (JVMTI_EVENT_VM_START - TOTAL_MIN_EVENT_TYPE_VAL));
  70 static const jlong  VM_INIT_BIT = (((jlong)1) << (JVMTI_EVENT_VM_INIT - TOTAL_MIN_EVENT_TYPE_VAL));
  71 static const jlong  VM_DEATH_BIT = (((jlong)1) << (JVMTI_EVENT_VM_DEATH - TOTAL_MIN_EVENT_TYPE_VAL));
  72 static const jlong  CLASS_LOAD_BIT = (((jlong)1) << (JVMTI_EVENT_CLASS_LOAD - TOTAL_MIN_EVENT_TYPE_VAL));
  73 static const jlong  CLASS_PREPARE_BIT = (((jlong)1) << (JVMTI_EVENT_CLASS_PREPARE - TOTAL_MIN_EVENT_TYPE_VAL));
  74 static const jlong  THREAD_START_BIT = (((jlong)1) << (JVMTI_EVENT_THREAD_START - TOTAL_MIN_EVENT_TYPE_VAL));
  75 static const jlong  THREAD_END_BIT = (((jlong)1) << (JVMTI_EVENT_THREAD_END - TOTAL_MIN_EVENT_TYPE_VAL));
  76 static const jlong  EXCEPTION_THROW_BIT = (((jlong)1) << (JVMTI_EVENT_EXCEPTION - TOTAL_MIN_EVENT_TYPE_VAL));
  77 static const jlong  EXCEPTION_CATCH_BIT = (((jlong)1) << (JVMTI_EVENT_EXCEPTION_CATCH - TOTAL_MIN_EVENT_TYPE_VAL));
  78 static const jlong  MONITOR_CONTENDED_ENTER_BIT = (((jlong)1) << (JVMTI_EVENT_MONITOR_CONTENDED_ENTER - TOTAL_MIN_EVENT_TYPE_VAL));
  79 static const jlong  MONITOR_CONTENDED_ENTERED_BIT = (((jlong)1) << (JVMTI_EVENT_MONITOR_CONTENDED_ENTERED - TOTAL_MIN_EVENT_TYPE_VAL));
  80 static const jlong  MONITOR_WAIT_BIT = (((jlong)1) << (JVMTI_EVENT_MONITOR_WAIT - TOTAL_MIN_EVENT_TYPE_VAL));
  81 static const jlong  MONITOR_WAITED_BIT = (((jlong)1) << (JVMTI_EVENT_MONITOR_WAITED - TOTAL_MIN_EVENT_TYPE_VAL));
  82 static const jlong  DYNAMIC_CODE_GENERATED_BIT = (((jlong)1) << (JVMTI_EVENT_DYNAMIC_CODE_GENERATED - TOTAL_MIN_EVENT_TYPE_VAL));
  83 static const jlong  DATA_DUMP_BIT = (((jlong)1) << (JVMTI_EVENT_DATA_DUMP_REQUEST - TOTAL_MIN_EVENT_TYPE_VAL));
  84 static const jlong  COMPILED_METHOD_LOAD_BIT = (((jlong)1) << (JVMTI_EVENT_COMPILED_METHOD_LOAD - TOTAL_MIN_EVENT_TYPE_VAL));
  85 static const jlong  COMPILED_METHOD_UNLOAD_BIT = (((jlong)1) << (JVMTI_EVENT_COMPILED_METHOD_UNLOAD - TOTAL_MIN_EVENT_TYPE_VAL));
  86 static const jlong  GARBAGE_COLLECTION_START_BIT = (((jlong)1) << (JVMTI_EVENT_GARBAGE_COLLECTION_START - TOTAL_MIN_EVENT_TYPE_VAL));
  87 static const jlong  GARBAGE_COLLECTION_FINISH_BIT = (((jlong)1) << (JVMTI_EVENT_GARBAGE_COLLECTION_FINISH - TOTAL_MIN_EVENT_TYPE_VAL));
  88 static const jlong  OBJECT_FREE_BIT = (((jlong)1) << (JVMTI_EVENT_OBJECT_FREE - TOTAL_MIN_EVENT_TYPE_VAL));
  89 static const jlong  RESOURCE_EXHAUSTED_BIT = (((jlong)1) << (JVMTI_EVENT_RESOURCE_EXHAUSTED - TOTAL_MIN_EVENT_TYPE_VAL));
  90 static const jlong  VM_OBJECT_ALLOC_BIT = (((jlong)1) << (JVMTI_EVENT_VM_OBJECT_ALLOC - TOTAL_MIN_EVENT_TYPE_VAL));
  91 static const jlong  SAMPLED_OBJECT_ALLOC_BIT = (((jlong)1) << (JVMTI_EVENT_SAMPLED_OBJECT_ALLOC - TOTAL_MIN_EVENT_TYPE_VAL));
  92 static const jlong  VTHREAD_START_BIT = (((jlong)1) << (JVMTI_EVENT_VIRTUAL_THREAD_START - TOTAL_MIN_EVENT_TYPE_VAL));
  93 static const jlong  VTHREAD_END_BIT = (((jlong)1) << (JVMTI_EVENT_VIRTUAL_THREAD_END - TOTAL_MIN_EVENT_TYPE_VAL));
  94 
  95 // bits for extension events
  96 static const jlong  CLASS_UNLOAD_BIT = (((jlong)1) << (EXT_EVENT_CLASS_UNLOAD - TOTAL_MIN_EVENT_TYPE_VAL));
  97 static const jlong  VTHREAD_MOUNT_BIT = (((jlong)1) << (EXT_EVENT_VIRTUAL_THREAD_MOUNT - TOTAL_MIN_EVENT_TYPE_VAL));
  98 static const jlong  VTHREAD_UNMOUNT_BIT = (((jlong)1) << (EXT_EVENT_VIRTUAL_THREAD_UNMOUNT - TOTAL_MIN_EVENT_TYPE_VAL));
  99 
 100 
 101 static const jlong  VTHREAD_BITS = VTHREAD_START_BIT | VTHREAD_END_BIT | VTHREAD_MOUNT_BIT | VTHREAD_UNMOUNT_BIT;
 102 static const jlong  MONITOR_BITS = MONITOR_CONTENDED_ENTER_BIT | MONITOR_CONTENDED_ENTERED_BIT |
 103                           MONITOR_WAIT_BIT | MONITOR_WAITED_BIT;
 104 static const jlong  EXCEPTION_BITS = EXCEPTION_THROW_BIT | EXCEPTION_CATCH_BIT;
 105 static const jlong  INTERP_EVENT_BITS =  SINGLE_STEP_BIT | METHOD_ENTRY_BIT | METHOD_EXIT_BIT |
 106                                 FRAME_POP_BIT | FIELD_ACCESS_BIT | FIELD_MODIFICATION_BIT;
 107 static const jlong  THREAD_FILTERED_EVENT_BITS = INTERP_EVENT_BITS | EXCEPTION_BITS | MONITOR_BITS | VTHREAD_BITS |
 108                                         BREAKPOINT_BIT | CLASS_LOAD_BIT | CLASS_PREPARE_BIT | THREAD_END_BIT |
 109                                         SAMPLED_OBJECT_ALLOC_BIT;
 110 static const jlong  NEED_THREAD_LIFE_EVENTS = THREAD_FILTERED_EVENT_BITS | THREAD_START_BIT;
 111 static const jlong  EARLY_EVENT_BITS = CLASS_FILE_LOAD_HOOK_BIT | CLASS_LOAD_BIT | CLASS_PREPARE_BIT |
 112                                VM_START_BIT | VM_INIT_BIT | VM_DEATH_BIT | NATIVE_METHOD_BIND_BIT |
 113                                THREAD_START_BIT | THREAD_END_BIT |
 114                                COMPILED_METHOD_LOAD_BIT | COMPILED_METHOD_UNLOAD_BIT |
 115                                DYNAMIC_CODE_GENERATED_BIT;
 116 static const jlong  GLOBAL_EVENT_BITS = ~THREAD_FILTERED_EVENT_BITS;
 117 static const jlong  SHOULD_POST_ON_EXCEPTIONS_BITS = EXCEPTION_BITS | METHOD_EXIT_BIT | FRAME_POP_BIT;
 118 
 119 ///////////////////////////////////////////////////////////////
 120 //
 121 // JvmtiEventEnabled
 122 //
 123 
 124 JvmtiEventEnabled::JvmtiEventEnabled() {
 125   clear();
 126 }
 127 
 128 
 129 void JvmtiEventEnabled::clear() {
 130   _enabled_bits = 0;
 131 #ifndef PRODUCT
 132   _init_guard = JEE_INIT_GUARD;
 133 #endif
 134 }
 135 
 136 void JvmtiEventEnabled::set_enabled(jvmtiEvent event_type, bool enabled) {
 137   jlong bits = get_bits();
 138   jlong mask = bit_for(event_type);
 139   if (enabled) {
 140     bits |= mask;
 141   } else {
 142     bits &= ~mask;
 143   }
 144   set_bits(bits);
 145 }
 146 
 147 
 148 ///////////////////////////////////////////////////////////////
 149 //
 150 // JvmtiEnvThreadEventEnable
 151 //
 152 
 153 JvmtiEnvThreadEventEnable::JvmtiEnvThreadEventEnable() {
 154   _event_user_enabled.clear();
 155   _event_enabled.clear();
 156 }
 157 
 158 
 159 JvmtiEnvThreadEventEnable::~JvmtiEnvThreadEventEnable() {
 160   _event_user_enabled.clear();
 161   _event_enabled.clear();
 162 }
 163 
 164 
 165 ///////////////////////////////////////////////////////////////
 166 //
 167 // JvmtiThreadEventEnable
 168 //
 169 
 170 JvmtiThreadEventEnable::JvmtiThreadEventEnable() {
 171   _event_enabled.clear();
 172 }
 173 
 174 
 175 JvmtiThreadEventEnable::~JvmtiThreadEventEnable() {
 176   _event_enabled.clear();
 177 }
 178 
 179 
 180 ///////////////////////////////////////////////////////////////
 181 //
 182 // JvmtiEnvEventEnable
 183 //
 184 
 185 JvmtiEnvEventEnable::JvmtiEnvEventEnable() {
 186   _event_user_enabled.clear();
 187   _event_callback_enabled.clear();
 188   _event_enabled.clear();
 189 }
 190 
 191 
 192 JvmtiEnvEventEnable::~JvmtiEnvEventEnable() {
 193   _event_user_enabled.clear();
 194   _event_callback_enabled.clear();
 195   _event_enabled.clear();
 196 }
 197 
 198 
 199 ///////////////////////////////////////////////////////////////
 200 //
 201 // EnterInterpOnlyModeClosure
 202 //
 203 
 204 class EnterInterpOnlyModeClosure : public HandshakeClosure {
 205  private:
 206   bool _completed;
 207  public:
 208   EnterInterpOnlyModeClosure() : HandshakeClosure("EnterInterpOnlyMode"), _completed(false) { }
 209   void do_thread(Thread* th) {
 210     JavaThread* jt = JavaThread::cast(th);
 211     JvmtiThreadState* state = jt->jvmti_thread_state();
 212 
 213     // invalidate_cur_stack_depth is called in enter_interp_only_mode


 214     state->enter_interp_only_mode();
 215 
 216     if (state->get_thread() != NULL) {
 217       // TODO LOOM: find out why the other place where this is called is insufficient 
 218       Continuation::set_cont_fastpath_thread_state(state->get_thread());
 219     }
 220 
 221     if (jt->has_last_Java_frame()) {
 222       // If running in fullspeed mode, single stepping is implemented
 223       // as follows: first, the interpreter does not dispatch to
 224       // compiled code for threads that have single stepping enabled;
 225       // second, we deoptimize all compiled java frames on the thread's stack when
 226       // interpreted-only mode is enabled the first time for a given
 227       // thread (nothing to do if no Java frames yet).
 228       ResourceMark resMark;
 229       for (StackFrameStream fst(jt, false /* update */, false /* process_frames */); !fst.is_done(); fst.next()) {
 230         if (fst.current()->can_be_deoptimized()) {
 231           Deoptimization::deoptimize(jt, *fst.current());
 232         }
 233       }
 234     }
 235     _completed = true;
 236   }
 237   bool completed() {
 238     return _completed;
 239   }
 240 };
 241 
 242 
 243 ///////////////////////////////////////////////////////////////
 244 //
 245 // VM_ChangeSingleStep
 246 //
 247 
 248 class VM_ChangeSingleStep : public VM_Operation {
 249 private:
 250   bool _on;
 251 
 252 public:
 253   VM_ChangeSingleStep(bool on);
 254   VMOp_Type type() const                         { return VMOp_ChangeSingleStep; }
 255   bool allow_nested_vm_operations() const        { return true; }
 256   void doit();   // method definition is after definition of JvmtiEventControllerPrivate because of scoping
 257 };
 258 
 259 
 260 VM_ChangeSingleStep::VM_ChangeSingleStep(bool on)
 261   : _on(on)
 262 {
 263 }
 264 
 265 
 266 
 267 
 268 ///////////////////////////////////////////////////////////////
 269 //
 270 // JvmtiEventControllerPrivate
 271 //
 272 // Private internal implementation methods for JvmtiEventController.
 273 //
 274 // These methods are thread safe either because they are called
 275 // in early VM initialization which is single threaded, or they
 276 // hold the JvmtiThreadState_lock.
 277 //
 278 
 279 class JvmtiEventControllerPrivate : public AllStatic {
 280   static bool _initialized;
 281 public:
 282   static void set_should_post_single_step(bool on);
 283   static void enter_interp_only_mode(JvmtiThreadState *state);
 284   static void leave_interp_only_mode(JvmtiThreadState *state);
 285   static void recompute_enabled();
 286   static jlong recompute_env_enabled(JvmtiEnvBase* env);
 287   static jlong recompute_env_thread_enabled(JvmtiEnvThreadState* ets, JvmtiThreadState* state);
 288   static jlong recompute_thread_enabled(JvmtiThreadState *state);
 289   static void event_init();
 290 
 291   static void set_user_enabled(JvmtiEnvBase *env, JavaThread *thread, oop thread_oop,
 292                         jvmtiEvent event_type, bool enabled);
 293   static void set_event_callbacks(JvmtiEnvBase *env,
 294                                   const jvmtiEventCallbacks* callbacks,
 295                                   jint size_of_callbacks);
 296 
 297   static void set_extension_event_callback(JvmtiEnvBase *env,
 298                                            jint extension_event_index,
 299                                            jvmtiExtensionEvent callback);
 300 
 301   static void set_frame_pop(JvmtiEnvThreadState *env_thread, JvmtiFramePop fpop);
 302   static void clear_frame_pop(JvmtiEnvThreadState *env_thread, JvmtiFramePop fpop);
 303   static void clear_to_frame_pop(JvmtiEnvThreadState *env_thread, JvmtiFramePop fpop);
 304   static void change_field_watch(jvmtiEvent event_type, bool added);
 305 
 306   static void thread_started(JavaThread *thread);
 307   static void thread_ended(JavaThread *thread);
 308 
 309   static void env_initialize(JvmtiEnvBase *env);
 310   static void env_dispose(JvmtiEnvBase *env);
 311 
 312   static void vm_start();
 313   static void vm_init();
 314   static void vm_death();
 315 
 316   static void trace_changed(JvmtiThreadState *state, jlong now_enabled, jlong changed);
 317   static void trace_changed(jlong now_enabled, jlong changed);
 318 
 319   static void flush_object_free_events(JvmtiEnvBase *env);
 320   static void set_enabled_events_with_lock(JvmtiEnvBase *env, jlong now_enabled);
 321 };
 322 
 323 bool JvmtiEventControllerPrivate::_initialized = false;
 324 
 325 void JvmtiEventControllerPrivate::set_should_post_single_step(bool on) {
 326   // we have permission to do this, VM op doesn't
 327   JvmtiExport::set_should_post_single_step(on);
 328 }
 329 
 330 
 331 // When _on == true, we use the safepoint interpreter dispatch table
 332 // to allow us to find the single step points. Otherwise, we switch
 333 // back to the regular interpreter dispatch table.
 334 // Note: We call Interpreter::notice_safepoints() and ignore_safepoints()
 335 // in a VM_Operation to safely make the dispatch table switch. We
 336 // no longer rely on the safepoint mechanism to do any of this work
 337 // for us.
 338 void VM_ChangeSingleStep::doit() {
 339   log_debug(interpreter, safepoint)("changing single step to '%s'", _on ? "on" : "off");
 340   JvmtiEventControllerPrivate::set_should_post_single_step(_on);
 341   if (_on) {
 342     Interpreter::notice_safepoints();
 343   } else {
 344     Interpreter::ignore_safepoints();
 345   }
 346 }
 347 
 348 
 349 void JvmtiEventControllerPrivate::enter_interp_only_mode(JvmtiThreadState *state) {
 350   EC_TRACE(("[%s] # Entering interpreter only mode",
 351             JvmtiTrace::safe_get_thread_name(state->get_thread_or_saved())));
 352   EnterInterpOnlyModeClosure hs;
 353   JavaThread *target = state->get_thread();
 354   Thread *current = Thread::current();
 355 
 356   // TBD: This is TMP workaround. We need a proper synchronization here.
 357   if (target == NULL) {
 358     state->enter_interp_only_mode(); // increment _saved_interp_only_mode
 359     return;
 360   }
 361 
 362 if (target->is_handshake_safe_for(current)) {
 363     hs.do_thread(target);
 364   } else {
 365     Handshake::execute(&hs, target);
 366     guarantee(hs.completed(), "Handshake failed: Target thread is not alive?");
 367   }
 368 }
 369 
 370 
 371 void
 372 JvmtiEventControllerPrivate::leave_interp_only_mode(JvmtiThreadState *state) {
 373   EC_TRACE(("[%s] # Leaving interpreter only mode",
 374             JvmtiTrace::safe_get_thread_name(state->get_thread_or_saved())));
 375   state->leave_interp_only_mode();
 376 }
 377 
 378 
 379 void
 380 JvmtiEventControllerPrivate::trace_changed(JvmtiThreadState *state, jlong now_enabled, jlong changed) {
 381 #ifdef JVMTI_TRACE
 382   if (JvmtiTrace::trace_event_controller()) {
 383     SafeResourceMark rm;
 384     // traces standard events only
 385     for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {
 386       jlong bit = JvmtiEventEnabled::bit_for((jvmtiEvent)ei);
 387       if (changed & bit) {
 388         // it changed, print it
 389          log_trace(jvmti)("[%s] # %s event %s",
 390                       JvmtiTrace::safe_get_thread_name(state->get_thread_or_saved()),
 391                       (now_enabled & bit)? "Enabling" : "Disabling", JvmtiTrace::event_name((jvmtiEvent)ei));
 392       }
 393     }
 394   }
 395 #endif /*JVMTI_TRACE */
 396 }
 397 
 398 
 399 void
 400 JvmtiEventControllerPrivate::trace_changed(jlong now_enabled, jlong changed) {
 401 #ifdef JVMTI_TRACE
 402   if (JvmtiTrace::trace_event_controller()) {
 403     SafeResourceMark rm;
 404     // traces standard events only
 405     for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {
 406       jlong bit = JvmtiEventEnabled::bit_for((jvmtiEvent)ei);
 407       if (changed & bit) {
 408         // it changed, print it
 409          log_trace(jvmti)("[-] # %s event %s",
 410                       (now_enabled & bit)? "Enabling" : "Disabling", JvmtiTrace::event_name((jvmtiEvent)ei));
 411       }
 412     }
 413   }
 414 #endif /*JVMTI_TRACE */
 415 }
 416 
 417 
 418 void
 419 JvmtiEventControllerPrivate::flush_object_free_events(JvmtiEnvBase* env) {
 420   // Some of the objects recorded by this env may have died.  If we're
 421   // (potentially) changing the enable state for ObjectFree events, we
 422   // need to ensure the env is cleaned up and any events that should
 423   // be posted are posted.
 424   JvmtiTagMap* tag_map = env->tag_map_acquire();
 425   if (tag_map != NULL) {
 426     tag_map->flush_object_free_events();
 427   }
 428 }
 429 
 430 void
 431 JvmtiEventControllerPrivate::set_enabled_events_with_lock(JvmtiEnvBase* env, jlong now_enabled) {
 432   // The state for ObjectFree events must be enabled or disabled
 433   // under the TagMap lock, to allow pending object posting events to complete.
 434   JvmtiTagMap* tag_map = env->tag_map_acquire();
 435   if (tag_map != NULL) {
 436     MutexLocker ml(tag_map->lock(), Mutex::_no_safepoint_check_flag);
 437     env->env_event_enable()->_event_enabled.set_bits(now_enabled);
 438   } else {
 439     env->env_event_enable()->_event_enabled.set_bits(now_enabled);
 440   }
 441 }
 442 
 443 // For the specified env: compute the currently truly enabled events
 444 // set external state accordingly.
 445 // Return value and set value must include all events.
 446 // But outside this class, only non-thread-filtered events can be queried..
 447 jlong
 448 JvmtiEventControllerPrivate::recompute_env_enabled(JvmtiEnvBase* env) {
 449   jlong was_enabled = env->env_event_enable()->_event_enabled.get_bits();
 450   jlong now_enabled =
 451     env->env_event_enable()->_event_callback_enabled.get_bits() &
 452     env->env_event_enable()->_event_user_enabled.get_bits();
 453 
 454   switch (env->phase()) {
 455   case JVMTI_PHASE_PRIMORDIAL:
 456   case JVMTI_PHASE_ONLOAD:
 457     // only these events allowed in primordial or onload phase
 458     now_enabled &= (EARLY_EVENT_BITS & ~THREAD_FILTERED_EVENT_BITS);
 459     break;
 460   case JVMTI_PHASE_START:
 461     // only these events allowed in start phase
 462     now_enabled &= EARLY_EVENT_BITS;
 463     break;
 464   case JVMTI_PHASE_LIVE:
 465     // all events allowed during live phase
 466     break;
 467   case JVMTI_PHASE_DEAD:
 468     // no events allowed when dead
 469     now_enabled = 0;
 470     break;
 471   default:
 472     assert(false, "no other phases - sanity check");
 473     break;
 474   }
 475 
 476   // Set/reset the event enabled under the tagmap lock.
 477   set_enabled_events_with_lock(env, now_enabled);
 478 
 479   trace_changed(now_enabled, (now_enabled ^ was_enabled)  & ~THREAD_FILTERED_EVENT_BITS);
 480 
 481   return now_enabled;
 482 }
 483 
 484 
 485 // For the specified env and thread: compute the currently truly enabled events
 486 // set external state accordingly.  Only thread-filtered events are included.
 487 jlong
 488 JvmtiEventControllerPrivate::recompute_env_thread_enabled(JvmtiEnvThreadState* ets, JvmtiThreadState* state) {
 489   JvmtiEnv *env = ets->get_env();
 490 
 491   jlong was_enabled = ets->event_enable()->_event_enabled.get_bits();
 492   jlong now_enabled =  THREAD_FILTERED_EVENT_BITS &
 493     env->env_event_enable()->_event_callback_enabled.get_bits() &
 494     (env->env_event_enable()->_event_user_enabled.get_bits() |
 495      ets->event_enable()->_event_user_enabled.get_bits());
 496 
 497   // for frame pops and field watchs, computed enabled state
 498   // is only true if an event has been requested
 499   if (!ets->has_frame_pops()) {
 500     now_enabled &= ~FRAME_POP_BIT;
 501   }
 502   if (*((int *)JvmtiExport::get_field_access_count_addr()) == 0) {
 503     now_enabled &= ~FIELD_ACCESS_BIT;
 504   }
 505   if (*((int *)JvmtiExport::get_field_modification_count_addr()) == 0) {
 506     now_enabled &= ~FIELD_MODIFICATION_BIT;
 507   }
 508 
 509   switch (JvmtiEnv::get_phase()) {
 510   case JVMTI_PHASE_DEAD:
 511     // no events allowed when dead
 512     now_enabled = 0;
 513     break;
 514   default:
 515     break;
 516   }
 517 
 518   // if anything changed do update
 519   if (now_enabled != was_enabled) {
 520 
 521     // will we really send these events to this thread x env
 522     ets->event_enable()->_event_enabled.set_bits(now_enabled);
 523 
 524     // If the enabled status of the single step or breakpoint events changed,
 525     // the location status may need to change as well.
 526     jlong changed = now_enabled ^ was_enabled;
 527     if (changed & SINGLE_STEP_BIT) {
 528       ets->reset_current_location(JVMTI_EVENT_SINGLE_STEP, (now_enabled & SINGLE_STEP_BIT) != 0);
 529     }
 530     if (changed & BREAKPOINT_BIT) {
 531       ets->reset_current_location(JVMTI_EVENT_BREAKPOINT,  (now_enabled & BREAKPOINT_BIT) != 0);
 532     }
 533     trace_changed(state, now_enabled, changed);
 534   }
 535   return now_enabled;
 536 }
 537 
 538 
 539 // For the specified thread: compute the currently truly enabled events
 540 // set external state accordingly.  Only thread-filtered events are included.
 541 jlong
 542 JvmtiEventControllerPrivate::recompute_thread_enabled(JvmtiThreadState *state) {
 543   if (state == NULL) {
 544     // associated JavaThread is exiting
 545     return (jlong)0;
 546   }

 547   julong was_any_env_enabled = state->thread_event_enable()->_event_enabled.get_bits();
 548   julong any_env_enabled = 0;
 549   // JVMTI_EVENT_FRAME_POP can be disabled (in the case FRAME_POP_BIT is not set),
 550   // but we need to set interp_only if some JvmtiEnvThreadState has frame pop set
 551   // to clear the request
 552   bool has_frame_pops = false;
 553 
 554   {
 555     // This iteration will include JvmtiEnvThreadStates whose environments
 556     // have been disposed.  These JvmtiEnvThreadStates must not be filtered
 557     // as recompute must be called on them to disable their events,
 558     JvmtiEnvThreadStateIterator it(state);
 559     for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
 560       any_env_enabled |= recompute_env_thread_enabled(ets, state);
 561       has_frame_pops |= ets->has_frame_pops();
 562     }
 563   }
 564 
 565   if (any_env_enabled != was_any_env_enabled) {
 566     // mark if event is truly enabled on this thread in any environment
 567     state->thread_event_enable()->_event_enabled.set_bits(any_env_enabled);
 568 
 569     // pointer to JavaThread can be NULL for unmouted virtual thread
 570     if (state->get_thread() != NULL) {
 571       // update the JavaThread cached value for thread-specific should_post_on_exceptions value
 572       bool should_post_on_exceptions = (any_env_enabled & SHOULD_POST_ON_EXCEPTIONS_BITS) != 0;
 573       state->set_should_post_on_exceptions(should_post_on_exceptions);
 574     }
 575   }
 576   if (state->get_thread() == NULL) {
 577     return any_env_enabled;
 578   }
 579 
 580   // compute interp_only mode
 581   bool should_be_interp = (any_env_enabled & INTERP_EVENT_BITS) != 0 || has_frame_pops;
 582   bool is_now_interp = state->is_interp_only_mode();
 583 
 584   if (should_be_interp != is_now_interp) {
 585     if (should_be_interp) {
 586       enter_interp_only_mode(state);
 587     } else {
 588       leave_interp_only_mode(state);
 589     }
 590   }
 591 
 592   // TBD: there is a race here, so get_thread() can return NULL below.
 593   if (state->get_thread() != NULL) {
 594     Continuation::set_cont_fastpath_thread_state(state->get_thread());
 595   }
 596 
 597   return any_env_enabled;
 598 }
 599 
 600 
 601 // Compute truly enabled events - meaning if the event can and could be
 602 // sent.  An event is truly enabled if it is user enabled on the thread
 603 // or globally user enabled, but only if there is a callback or event hook
 604 // for it and, for field watch and frame pop, one has been set.
 605 // Compute if truly enabled, per thread, per environment, per combination
 606 // (thread x environment), and overall.  These merges are true if any is true.
 607 // True per thread if some environment has callback set and the event is globally
 608 // enabled or enabled for this thread.
 609 // True per environment if the callback is set and the event is globally
 610 // enabled in this environment or enabled for any thread in this environment.
 611 // True per combination if the environment has the callback set and the
 612 // event is globally enabled in this environment or the event is enabled
 613 // for this thread and environment.
 614 //
 615 // All states transitions dependent on these transitions are also handled here.
 616 void
 617 JvmtiEventControllerPrivate::recompute_enabled() {
 618   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
 619 
 620   // event enabled for any thread in any environment
 621   julong was_any_env_thread_enabled = JvmtiEventController::_universal_global_event_enabled.get_bits();
 622   julong any_env_thread_enabled = 0;
 623 
 624   EC_TRACE(("[-] # recompute enabled - before " JULONG_FORMAT_X, was_any_env_thread_enabled));
 625 
 626   // Compute non-thread-filtered events.
 627   // This must be done separately from thread-filtered events, since some
 628   // events can occur before any threads exist.
 629   JvmtiEnvIterator it;
 630   for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) {
 631     any_env_thread_enabled |= recompute_env_enabled(env);
 632   }
 633 
 634   // We need to create any missing jvmti_thread_state if there are globally set thread
 635   // filtered events and there weren't last time
 636   if (    (any_env_thread_enabled & THREAD_FILTERED_EVENT_BITS) != 0 &&
 637       (was_any_env_thread_enabled & THREAD_FILTERED_EVENT_BITS) == 0) {
 638     for (JavaThreadIteratorWithHandle jtiwh; JavaThread *tp = jtiwh.next(); ) {
 639       oop jt_oop = tp->threadObj();
 640 
 641       // create the thread state if missing, state_for_while_locked() makes tp->is_exiting() check
 642       JvmtiThreadState* state = JvmtiThreadState::state_for_while_locked(tp, jt_oop);
 643 
 644       // create the thread state for mounted virtual thread if missing
 645       oop vt_oop = tp->mounted_vthread();
 646       if (vt_oop != NULL && java_lang_VirtualThread::is_instance(vt_oop)) {
 647         state = JvmtiThreadState::state_for_while_locked(tp, vt_oop);
 648       } 
 649     }
 650   }
 651 
 652   // compute and set thread-filtered events
 653   JvmtiThreadState *state = JvmtiThreadState::first();
 654   if (state != nullptr) {
 655     // If we have a JvmtiThreadState, then we've reached the point where
 656     // threads can exist so create a ThreadsListHandle to protect them.
 657     // The held JvmtiThreadState_lock prevents exiting JavaThreads from
 658     // being removed from the JvmtiThreadState list we're about to walk
 659     // so this ThreadsListHandle exists just to satisfy the lower level sanity
 660     // checks that the target JavaThreads are protected.
 661     ThreadsListHandle tlh;
 662     for (; state != nullptr; state = state->next()) {
 663       any_env_thread_enabled |= recompute_thread_enabled(state);
 664     }
 665   }
 666 
 667   // set universal state (across all envs and threads)
 668   jlong delta = any_env_thread_enabled ^ was_any_env_thread_enabled;
 669   if (delta != 0) {
 670     JvmtiExport::set_should_post_field_access((any_env_thread_enabled & FIELD_ACCESS_BIT) != 0);
 671     JvmtiExport::set_should_post_field_modification((any_env_thread_enabled & FIELD_MODIFICATION_BIT) != 0);
 672     JvmtiExport::set_should_post_class_load((any_env_thread_enabled & CLASS_LOAD_BIT) != 0);
 673     JvmtiExport::set_should_post_class_file_load_hook((any_env_thread_enabled & CLASS_FILE_LOAD_HOOK_BIT) != 0);
 674     JvmtiExport::set_should_post_native_method_bind((any_env_thread_enabled & NATIVE_METHOD_BIND_BIT) != 0);
 675     JvmtiExport::set_should_post_dynamic_code_generated((any_env_thread_enabled & DYNAMIC_CODE_GENERATED_BIT) != 0);
 676     JvmtiExport::set_should_post_data_dump((any_env_thread_enabled & DATA_DUMP_BIT) != 0);
 677     JvmtiExport::set_should_post_class_prepare((any_env_thread_enabled & CLASS_PREPARE_BIT) != 0);
 678     JvmtiExport::set_should_post_class_unload((any_env_thread_enabled & CLASS_UNLOAD_BIT) != 0);
 679     JvmtiExport::set_should_post_monitor_contended_enter((any_env_thread_enabled & MONITOR_CONTENDED_ENTER_BIT) != 0);
 680     JvmtiExport::set_should_post_monitor_contended_entered((any_env_thread_enabled & MONITOR_CONTENDED_ENTERED_BIT) != 0);
 681     JvmtiExport::set_should_post_monitor_wait((any_env_thread_enabled & MONITOR_WAIT_BIT) != 0);
 682     JvmtiExport::set_should_post_monitor_waited((any_env_thread_enabled & MONITOR_WAITED_BIT) != 0);
 683     JvmtiExport::set_should_post_garbage_collection_start((any_env_thread_enabled & GARBAGE_COLLECTION_START_BIT) != 0);
 684     JvmtiExport::set_should_post_garbage_collection_finish((any_env_thread_enabled & GARBAGE_COLLECTION_FINISH_BIT) != 0);
 685     JvmtiExport::set_should_post_object_free((any_env_thread_enabled & OBJECT_FREE_BIT) != 0);
 686     JvmtiExport::set_should_post_resource_exhausted((any_env_thread_enabled & RESOURCE_EXHAUSTED_BIT) != 0);
 687     JvmtiExport::set_should_post_compiled_method_load((any_env_thread_enabled & COMPILED_METHOD_LOAD_BIT) != 0);
 688     JvmtiExport::set_should_post_compiled_method_unload((any_env_thread_enabled & COMPILED_METHOD_UNLOAD_BIT) != 0);
 689     JvmtiExport::set_should_post_vm_object_alloc((any_env_thread_enabled & VM_OBJECT_ALLOC_BIT) != 0);
 690     JvmtiExport::set_should_post_sampled_object_alloc((any_env_thread_enabled & SAMPLED_OBJECT_ALLOC_BIT) != 0);
 691     JvmtiExport::set_should_post_vthread_start((any_env_thread_enabled & VTHREAD_START_BIT) != 0);
 692     JvmtiExport::set_should_post_vthread_end((any_env_thread_enabled & VTHREAD_END_BIT) != 0);
 693     JvmtiExport::set_should_post_vthread_mount((any_env_thread_enabled & VTHREAD_MOUNT_BIT) != 0);
 694     JvmtiExport::set_should_post_vthread_unmount((any_env_thread_enabled & VTHREAD_UNMOUNT_BIT) != 0);
 695 
 696     // need this if we want thread events or we need them to init data
 697     JvmtiExport::set_should_post_thread_life((any_env_thread_enabled & NEED_THREAD_LIFE_EVENTS) != 0);
 698 
 699     // If single stepping is turned on or off, execute the VM op to change it.
 700     if (delta & SINGLE_STEP_BIT) {
 701       switch (JvmtiEnv::get_phase()) {
 702       case JVMTI_PHASE_DEAD:
 703         // If the VM is dying we can't execute VM ops
 704         break;
 705       case JVMTI_PHASE_LIVE: {
 706         VM_ChangeSingleStep op((any_env_thread_enabled & SINGLE_STEP_BIT) != 0);
 707         VMThread::execute(&op);
 708         break;
 709       }
 710       default:
 711         assert(false, "should never come here before live phase");
 712         break;
 713       }
 714     }
 715 
 716     // set global truly enabled, that is, any thread in any environment
 717     JvmtiEventController::_universal_global_event_enabled.set_bits(any_env_thread_enabled);
 718 
 719     // set global should_post_on_exceptions
 720     JvmtiExport::set_should_post_on_exceptions((any_env_thread_enabled & SHOULD_POST_ON_EXCEPTIONS_BITS) != 0);
 721 
 722   }
 723 
 724   EC_TRACE(("[-] # recompute enabled - after " JULONG_FORMAT_X, any_env_thread_enabled));
 725 }
 726 
 727 
 728 void
 729 JvmtiEventControllerPrivate::thread_started(JavaThread *thread) {
 730   assert(thread == Thread::current(), "must be current thread");
 731   assert(JvmtiEnvBase::environments_might_exist(), "to enter event controller, JVM TI environments must exist");
 732 
 733   EC_TRACE(("[%s] # thread started", JvmtiTrace::safe_get_thread_name(thread)));
 734 
 735   // if we have any thread filtered events globally enabled, create/update the thread state
 736   if ((JvmtiEventController::_universal_global_event_enabled.get_bits() & THREAD_FILTERED_EVENT_BITS) != 0) {
 737     MutexLocker mu(JvmtiThreadState_lock);
 738     // create the thread state if missing
 739     JvmtiThreadState *state = JvmtiThreadState::state_for_while_locked(thread);
 740     if (state != NULL) {    // skip threads with no JVMTI thread state
 741       recompute_thread_enabled(state);
 742     }
 743   }
 744 }
 745 
 746 
 747 void
 748 JvmtiEventControllerPrivate::thread_ended(JavaThread *thread) {
 749   // Removes the JvmtiThreadState associated with the specified thread.
 750   // May be called after all environments have been disposed.
 751   assert(JvmtiThreadState_lock->is_locked(), "sanity check");
 752 
 753   EC_TRACE(("[%s] # thread ended", JvmtiTrace::safe_get_thread_name(thread)));
 754 
 755   JvmtiThreadState *state = thread->jvmti_thread_state();
 756   assert(state != NULL, "else why are we here?");
 757   delete state;
 758 }
 759 
 760 void JvmtiEventControllerPrivate::set_event_callbacks(JvmtiEnvBase *env,
 761                                                       const jvmtiEventCallbacks* callbacks,
 762                                                       jint size_of_callbacks) {
 763   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
 764   EC_TRACE(("[*] # set event callbacks"));
 765 
 766   // May be changing the event handler for ObjectFree.
 767   flush_object_free_events(env);
 768 
 769   env->set_event_callbacks(callbacks, size_of_callbacks);
 770   jlong enabled_bits = env->env_event_enable()->_event_callback_enabled.get_bits();
 771 
 772   for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {
 773     jvmtiEvent evt_t = (jvmtiEvent)ei;
 774     if (env->has_callback(evt_t)) {
 775       enabled_bits |= JvmtiEventEnabled::bit_for(evt_t);
 776     }
 777   }
 778   env->env_event_enable()->_event_callback_enabled.set_bits(enabled_bits);
 779   recompute_enabled();
 780 }
 781 
 782 void
 783 JvmtiEventControllerPrivate::set_extension_event_callback(JvmtiEnvBase *env,
 784                                                           jint extension_event_index,
 785                                                           jvmtiExtensionEvent callback)
 786 {
 787   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
 788   EC_TRACE(("[*] # set extension event callback"));
 789 
 790   // extension events are allocated below JVMTI_MIN_EVENT_TYPE_VAL
 791   assert(extension_event_index >= (jint)EXT_MIN_EVENT_TYPE_VAL &&
 792          extension_event_index <= (jint)EXT_MAX_EVENT_TYPE_VAL, "sanity check");
 793 
 794 
 795   // As the bits for both standard (jvmtiEvent) and extension
 796   // (jvmtiExtEvents) are stored in the same word we cast here to
 797   // jvmtiEvent to set/clear the bit for this extension event.
 798   jvmtiEvent event_type = (jvmtiEvent)extension_event_index;
 799 
 800   // Prevent a possible race condition where events are re-enabled by a call to
 801   // set event callbacks, where the DisposeEnvironment occurs after the boiler-plate
 802   // environment check and before the lock is acquired.
 803   // We can safely do the is_valid check now, as JvmtiThreadState_lock is held.
 804   bool enabling = (callback != NULL) && (env->is_valid());

 805 
 806   // update the callback
 807   jvmtiExtEventCallbacks* ext_callbacks = env->ext_callbacks();
 808   switch (extension_event_index) {
 809     case EXT_EVENT_CLASS_UNLOAD :
 810       ext_callbacks->ClassUnload = callback;
 811       break;
 812     case EXT_EVENT_VIRTUAL_THREAD_MOUNT :
 813       ext_callbacks->VirtualThreadMount = callback;
 814       break;
 815     case EXT_EVENT_VIRTUAL_THREAD_UNMOUNT :
 816       ext_callbacks->VirtualThreadUnmount = callback;
 817       break;
 818     default:
 819       ShouldNotReachHere();
 820   }
 821 
 822   // update the callback enable/disable bit
 823   jlong enabled_bits = env->env_event_enable()->_event_callback_enabled.get_bits();
 824   jlong bit_for = JvmtiEventEnabled::bit_for(event_type);
 825   if (enabling) {
 826     enabled_bits |= bit_for;
 827   } else {
 828     enabled_bits &= ~bit_for;
 829   }
 830   env->env_event_enable()->_event_callback_enabled.set_bits(enabled_bits);
 831 
 832   recompute_enabled();
 833 }
 834 
 835 
 836 void
 837 JvmtiEventControllerPrivate::env_initialize(JvmtiEnvBase *env) {
 838   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
 839   EC_TRACE(("[*] # env initialize"));
 840 
 841   if (JvmtiEnvBase::is_vm_live()) {
 842     // if we didn't initialize event info already (this is a late
 843     // launched environment), do it now.
 844     event_init();
 845   }
 846 
 847   env->initialize();
 848 
 849   // add the JvmtiEnvThreadState to each JvmtiThreadState
 850   for (JvmtiThreadState *state = JvmtiThreadState::first(); state != NULL; state = state->next()) {
 851     state->add_env(env);
 852     assert((JvmtiEnv*)(state->env_thread_state(env)->get_env()) == env, "sanity check");
 853   }
 854   JvmtiEventControllerPrivate::recompute_enabled();
 855 }
 856 
 857 
 858 void
 859 JvmtiEventControllerPrivate::env_dispose(JvmtiEnvBase *env) {
 860   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
 861   EC_TRACE(("[*] # env dispose"));
 862 
 863   // Before the environment is marked disposed, disable all events on this
 864   // environment (by zapping the callbacks).  As a result, the disposed
 865   // environment will not call event handlers.
 866   set_event_callbacks(env, NULL, 0);
 867   for (jint extension_event_index = EXT_MIN_EVENT_TYPE_VAL;
 868        extension_event_index <= EXT_MAX_EVENT_TYPE_VAL;
 869        ++extension_event_index) {
 870     set_extension_event_callback(env, extension_event_index, NULL);
 871   }
 872 
 873   // Let the environment finish disposing itself.
 874   env->env_dispose();
 875 }
 876 
 877 
 878 void
 879 JvmtiEventControllerPrivate::set_user_enabled(JvmtiEnvBase *env, JavaThread *thread, oop thread_oop,
 880                                           jvmtiEvent event_type, bool enabled) {
 881   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
 882 
 883   EC_TRACE(("[%s] # user %s event %s",
 884             thread==NULL? "ALL": JvmtiTrace::safe_get_thread_name(thread),
 885             enabled? "enabled" : "disabled", JvmtiTrace::event_name(event_type)));
 886 
 887   if (event_type == JVMTI_EVENT_OBJECT_FREE) {
 888     flush_object_free_events(env);
 889   }
 890 
 891   if (thread == NULL && thread_oop == NULL) { // thread can be NULL for unmounted virtual trheads
 892     env->env_event_enable()->set_user_enabled(event_type, enabled);
 893   } else {
 894     // create the thread state (if it didn't exist before)
 895     JvmtiThreadState *state = JvmtiThreadState::state_for_while_locked(thread, thread_oop);
 896     if (state != NULL) {
 897       state->env_thread_state(env)->event_enable()->set_user_enabled(event_type, enabled);
 898     }
 899   }
 900   recompute_enabled();
 901 }
 902 
 903 
 904 void
 905 JvmtiEventControllerPrivate::set_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
 906   EC_TRACE(("[%s] # set frame pop - frame=%d",
 907             JvmtiTrace::safe_get_thread_name(ets->get_thread_or_saved()),
 908             fpop.frame_number() ));
 909 
 910   ets->get_frame_pops()->set(fpop);
 911   recompute_thread_enabled(ets->jvmti_thread_state());
 912 }
 913 
 914 
 915 void
 916 JvmtiEventControllerPrivate::clear_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
 917   EC_TRACE(("[%s] # clear frame pop - frame=%d",
 918             JvmtiTrace::safe_get_thread_name(ets->get_thread_or_saved()),
 919             fpop.frame_number() ));
 920 
 921   ets->get_frame_pops()->clear(fpop);
 922   recompute_thread_enabled(ets->jvmti_thread_state());
 923 }
 924 
 925 
 926 void
 927 JvmtiEventControllerPrivate::clear_to_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
 928   int cleared_cnt = ets->get_frame_pops()->clear_to(fpop);
 929 
 930   EC_TRACE(("[%s] # clear to frame pop - frame=%d, count=%d",
 931             JvmtiTrace::safe_get_thread_name(ets->get_thread_or_saved()),
 932             fpop.frame_number(),
 933             cleared_cnt ));
 934 
 935   if (cleared_cnt > 0) {
 936     recompute_thread_enabled(ets->jvmti_thread_state());
 937   }
 938 }
 939 
 940 void
 941 JvmtiEventControllerPrivate::change_field_watch(jvmtiEvent event_type, bool added) {
 942   int *count_addr;
 943 
 944   switch (event_type) {
 945   case JVMTI_EVENT_FIELD_MODIFICATION:
 946     count_addr = (int *)JvmtiExport::get_field_modification_count_addr();
 947     break;
 948   case JVMTI_EVENT_FIELD_ACCESS:
 949     count_addr = (int *)JvmtiExport::get_field_access_count_addr();
 950     break;
 951   default:
 952     assert(false, "incorrect event");
 953     return;
 954   }
 955 
 956   EC_TRACE(("[-] # change field watch - %s %s count=%d",
 957             event_type==JVMTI_EVENT_FIELD_MODIFICATION? "modification" : "access",
 958             added? "add" : "remove",
 959             *count_addr));
 960 
 961   if (added) {
 962     (*count_addr)++;
 963     if (*count_addr == 1) {
 964       recompute_enabled();
 965     }
 966   } else {
 967     if (*count_addr > 0) {
 968       (*count_addr)--;
 969       if (*count_addr == 0) {
 970         recompute_enabled();
 971       }
 972     } else {
 973       assert(false, "field watch out of phase");
 974     }
 975   }
 976 }
 977 
 978 void
 979 JvmtiEventControllerPrivate::event_init() {
 980   assert(JvmtiThreadState_lock->is_locked(), "sanity check");
 981 
 982   if (_initialized) {
 983     return;
 984   }
 985 
 986   EC_TRACE(("[-] # VM live"));
 987 
 988 #ifdef ASSERT
 989   // check that our idea and the spec's idea of threaded events match
 990   for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {
 991     jlong bit = JvmtiEventEnabled::bit_for((jvmtiEvent)ei);
 992     assert(((THREAD_FILTERED_EVENT_BITS & bit) != 0) == JvmtiUtil::event_threaded(ei),
 993            "thread filtered event list does not match");
 994   }
 995 #endif
 996 
 997   _initialized = true;
 998 }
 999 
1000 void
1001 JvmtiEventControllerPrivate::vm_start() {
1002   // some events are now able to be enabled (phase has changed)
1003   JvmtiEventControllerPrivate::recompute_enabled();
1004 }
1005 
1006 
1007 void
1008 JvmtiEventControllerPrivate::vm_init() {
1009   event_init();
1010 
1011   // all the events are now able to be enabled (phase has changed)
1012   JvmtiEventControllerPrivate::recompute_enabled();
1013 }
1014 
1015 
1016 void
1017 JvmtiEventControllerPrivate::vm_death() {
1018   // events are disabled (phase has changed)
1019   JvmtiEventControllerPrivate::recompute_enabled();
1020 }
1021 
1022 
1023 ///////////////////////////////////////////////////////////////
1024 //
1025 // JvmtiEventController
1026 //
1027 
1028 JvmtiEventEnabled JvmtiEventController::_universal_global_event_enabled;
1029 
1030 bool
1031 JvmtiEventController::is_global_event(jvmtiEvent event_type) {
1032   assert(is_valid_event_type(event_type), "invalid event type");
1033   jlong bit_for = ((jlong)1) << (event_type - TOTAL_MIN_EVENT_TYPE_VAL);
1034   return((bit_for & GLOBAL_EVENT_BITS)!=0);
1035 }
1036 
1037 void
1038 JvmtiEventController::set_user_enabled(JvmtiEnvBase *env, JavaThread *thread, oop thread_oop,
1039                                        jvmtiEvent event_type, bool enabled) {
1040   if (Threads::number_of_threads() == 0) {
1041     // during early VM start-up locks don't exist, but we are safely single threaded,
1042     // call the functionality without holding the JvmtiThreadState_lock.
1043     JvmtiEventControllerPrivate::set_user_enabled(env, thread, NULL, event_type, enabled);
1044   } else {
1045     Thread* current_thread = Thread::current();
1046     HandleMark hm(current_thread);
1047     Handle thread_oop_h = Handle(current_thread, thread_oop);
1048     MutexLocker mu(JvmtiThreadState_lock);
1049     JvmtiEventControllerPrivate::set_user_enabled(env, thread, thread_oop_h(), event_type, enabled);
1050   }
1051 }
1052 
1053 
1054 void
1055 JvmtiEventController::set_event_callbacks(JvmtiEnvBase *env,
1056                                           const jvmtiEventCallbacks* callbacks,
1057                                           jint size_of_callbacks) {
1058   if (Threads::number_of_threads() == 0) {
1059     // during early VM start-up locks don't exist, but we are safely single threaded,
1060     // call the functionality without holding the JvmtiThreadState_lock.
1061     JvmtiEventControllerPrivate::set_event_callbacks(env, callbacks, size_of_callbacks);
1062   } else {
1063     MutexLocker mu(JvmtiThreadState_lock);
1064     JvmtiEventControllerPrivate::set_event_callbacks(env, callbacks, size_of_callbacks);
1065   }
1066 }
1067 
1068 void
1069 JvmtiEventController::set_extension_event_callback(JvmtiEnvBase *env,
1070                                                    jint extension_event_index,
1071                                                    jvmtiExtensionEvent callback) {
1072   if (Threads::number_of_threads() == 0) {
1073     JvmtiEventControllerPrivate::set_extension_event_callback(env, extension_event_index, callback);
1074   } else {
1075     MutexLocker mu(JvmtiThreadState_lock);
1076     JvmtiEventControllerPrivate::set_extension_event_callback(env, extension_event_index, callback);
1077   }
1078 }
1079 
1080 void
1081 JvmtiEventController::set_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
1082   assert(JvmtiThreadState_lock->is_locked(), "Must be locked.");
1083   JvmtiEventControllerPrivate::set_frame_pop(ets, fpop);
1084 }
1085 
1086 void
1087 JvmtiEventController::clear_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
1088   assert(JvmtiThreadState_lock->is_locked(), "Must be locked.");
1089   JvmtiEventControllerPrivate::clear_frame_pop(ets, fpop);
1090 }
1091 
1092 void
1093 JvmtiEventController::change_field_watch(jvmtiEvent event_type, bool added) {
1094   MutexLocker mu(JvmtiThreadState_lock);
1095   JvmtiEventControllerPrivate::change_field_watch(event_type, added);
1096 }
1097 
1098 void
1099 JvmtiEventController::thread_started(JavaThread *thread) {
1100   // operates only on the current thread
1101   // JvmtiThreadState_lock grabbed only if needed.
1102   JvmtiEventControllerPrivate::thread_started(thread);
1103 }
1104 
1105 void
1106 JvmtiEventController::thread_ended(JavaThread *thread) {
1107   // operates only on the current thread
1108   // JvmtiThreadState_lock grabbed only if needed.
1109   JvmtiEventControllerPrivate::thread_ended(thread);
1110 }
1111 
1112 void
1113 JvmtiEventController::env_initialize(JvmtiEnvBase *env) {
1114   if (Threads::number_of_threads() == 0) {
1115     // during early VM start-up locks don't exist, but we are safely single threaded,
1116     // call the functionality without holding the JvmtiThreadState_lock.
1117     JvmtiEventControllerPrivate::env_initialize(env);
1118   } else {
1119     MutexLocker mu(JvmtiThreadState_lock);
1120     JvmtiEventControllerPrivate::env_initialize(env);
1121   }
1122 }
1123 
1124 void
1125 JvmtiEventController::env_dispose(JvmtiEnvBase *env) {
1126   if (Threads::number_of_threads() == 0) {
1127     // during early VM start-up locks don't exist, but we are safely single threaded,
1128     // call the functionality without holding the JvmtiThreadState_lock.
1129     JvmtiEventControllerPrivate::env_dispose(env);
1130   } else {
1131     MutexLocker mu(JvmtiThreadState_lock);
1132     JvmtiEventControllerPrivate::env_dispose(env);
1133   }
1134 }
1135 
1136 
1137 void
1138 JvmtiEventController::vm_start() {
1139   if (JvmtiEnvBase::environments_might_exist()) {
1140     MutexLocker mu(JvmtiThreadState_lock);
1141     JvmtiEventControllerPrivate::vm_start();
1142   }
1143 }
1144 
1145 void
1146 JvmtiEventController::vm_init() {
1147   if (JvmtiEnvBase::environments_might_exist()) {
1148     MutexLocker mu(JvmtiThreadState_lock);
1149     JvmtiEventControllerPrivate::vm_init();
1150   }
1151 }
1152 
1153 void
1154 JvmtiEventController::vm_death() {
1155   if (JvmtiEnvBase::environments_might_exist()) {
1156     MutexLocker mu(JvmtiThreadState_lock);
1157     JvmtiEventControllerPrivate::vm_death();
1158   }
1159 }
--- EOF ---