src/share/vm/prims/jvmtiEventController.cpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/prims/jvmtiEventController.cpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,1079 @@
     1.4 +/*
     1.5 + * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "interpreter/interpreter.hpp"
    1.30 +#include "jvmtifiles/jvmtiEnv.hpp"
    1.31 +#include "memory/resourceArea.hpp"
    1.32 +#include "prims/jvmtiEventController.hpp"
    1.33 +#include "prims/jvmtiEventController.inline.hpp"
    1.34 +#include "prims/jvmtiExport.hpp"
    1.35 +#include "prims/jvmtiImpl.hpp"
    1.36 +#include "prims/jvmtiThreadState.inline.hpp"
    1.37 +#include "runtime/frame.hpp"
    1.38 +#include "runtime/thread.hpp"
    1.39 +#include "runtime/vframe.hpp"
    1.40 +#include "runtime/vframe_hp.hpp"
    1.41 +#include "runtime/vmThread.hpp"
    1.42 +#include "runtime/vm_operations.hpp"
    1.43 +
    1.44 +PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
    1.45 +
    1.46 +#ifdef JVMTI_TRACE
    1.47 +#define EC_TRACE(out) do { \
    1.48 +  if (JvmtiTrace::trace_event_controller()) { \
    1.49 +    SafeResourceMark rm; \
    1.50 +    tty->print_cr out; \
    1.51 +  } \
    1.52 +} while (0)
    1.53 +#else
    1.54 +#define EC_TRACE(out)
    1.55 +#endif /*JVMTI_TRACE */
    1.56 +
    1.57 +// bits for standard events
    1.58 +
    1.59 +static const jlong  SINGLE_STEP_BIT = (((jlong)1) << (JVMTI_EVENT_SINGLE_STEP - TOTAL_MIN_EVENT_TYPE_VAL));
    1.60 +static const jlong  FRAME_POP_BIT = (((jlong)1) << (JVMTI_EVENT_FRAME_POP - TOTAL_MIN_EVENT_TYPE_VAL));
    1.61 +static const jlong  BREAKPOINT_BIT = (((jlong)1) << (JVMTI_EVENT_BREAKPOINT - TOTAL_MIN_EVENT_TYPE_VAL));
    1.62 +static const jlong  FIELD_ACCESS_BIT = (((jlong)1) << (JVMTI_EVENT_FIELD_ACCESS - TOTAL_MIN_EVENT_TYPE_VAL));
    1.63 +static const jlong  FIELD_MODIFICATION_BIT = (((jlong)1) << (JVMTI_EVENT_FIELD_MODIFICATION - TOTAL_MIN_EVENT_TYPE_VAL));
    1.64 +static const jlong  METHOD_ENTRY_BIT = (((jlong)1) << (JVMTI_EVENT_METHOD_ENTRY - TOTAL_MIN_EVENT_TYPE_VAL));
    1.65 +static const jlong  METHOD_EXIT_BIT = (((jlong)1) << (JVMTI_EVENT_METHOD_EXIT - TOTAL_MIN_EVENT_TYPE_VAL));
    1.66 +static const jlong  CLASS_FILE_LOAD_HOOK_BIT = (((jlong)1) << (JVMTI_EVENT_CLASS_FILE_LOAD_HOOK - TOTAL_MIN_EVENT_TYPE_VAL));
    1.67 +static const jlong  NATIVE_METHOD_BIND_BIT = (((jlong)1) << (JVMTI_EVENT_NATIVE_METHOD_BIND - TOTAL_MIN_EVENT_TYPE_VAL));
    1.68 +static const jlong  VM_START_BIT = (((jlong)1) << (JVMTI_EVENT_VM_START - TOTAL_MIN_EVENT_TYPE_VAL));
    1.69 +static const jlong  VM_INIT_BIT = (((jlong)1) << (JVMTI_EVENT_VM_INIT - TOTAL_MIN_EVENT_TYPE_VAL));
    1.70 +static const jlong  VM_DEATH_BIT = (((jlong)1) << (JVMTI_EVENT_VM_DEATH - TOTAL_MIN_EVENT_TYPE_VAL));
    1.71 +static const jlong  CLASS_LOAD_BIT = (((jlong)1) << (JVMTI_EVENT_CLASS_LOAD - TOTAL_MIN_EVENT_TYPE_VAL));
    1.72 +static const jlong  CLASS_PREPARE_BIT = (((jlong)1) << (JVMTI_EVENT_CLASS_PREPARE - TOTAL_MIN_EVENT_TYPE_VAL));
    1.73 +static const jlong  THREAD_START_BIT = (((jlong)1) << (JVMTI_EVENT_THREAD_START - TOTAL_MIN_EVENT_TYPE_VAL));
    1.74 +static const jlong  THREAD_END_BIT = (((jlong)1) << (JVMTI_EVENT_THREAD_END - TOTAL_MIN_EVENT_TYPE_VAL));
    1.75 +static const jlong  EXCEPTION_THROW_BIT = (((jlong)1) << (JVMTI_EVENT_EXCEPTION - TOTAL_MIN_EVENT_TYPE_VAL));
    1.76 +static const jlong  EXCEPTION_CATCH_BIT = (((jlong)1) << (JVMTI_EVENT_EXCEPTION_CATCH - TOTAL_MIN_EVENT_TYPE_VAL));
    1.77 +static const jlong  MONITOR_CONTENDED_ENTER_BIT = (((jlong)1) << (JVMTI_EVENT_MONITOR_CONTENDED_ENTER - TOTAL_MIN_EVENT_TYPE_VAL));
    1.78 +static const jlong  MONITOR_CONTENDED_ENTERED_BIT = (((jlong)1) << (JVMTI_EVENT_MONITOR_CONTENDED_ENTERED - TOTAL_MIN_EVENT_TYPE_VAL));
    1.79 +static const jlong  MONITOR_WAIT_BIT = (((jlong)1) << (JVMTI_EVENT_MONITOR_WAIT - TOTAL_MIN_EVENT_TYPE_VAL));
    1.80 +static const jlong  MONITOR_WAITED_BIT = (((jlong)1) << (JVMTI_EVENT_MONITOR_WAITED - TOTAL_MIN_EVENT_TYPE_VAL));
    1.81 +static const jlong  DYNAMIC_CODE_GENERATED_BIT = (((jlong)1) << (JVMTI_EVENT_DYNAMIC_CODE_GENERATED - TOTAL_MIN_EVENT_TYPE_VAL));
    1.82 +static const jlong  DATA_DUMP_BIT = (((jlong)1) << (JVMTI_EVENT_DATA_DUMP_REQUEST - TOTAL_MIN_EVENT_TYPE_VAL));
    1.83 +static const jlong  COMPILED_METHOD_LOAD_BIT = (((jlong)1) << (JVMTI_EVENT_COMPILED_METHOD_LOAD - TOTAL_MIN_EVENT_TYPE_VAL));
    1.84 +static const jlong  COMPILED_METHOD_UNLOAD_BIT = (((jlong)1) << (JVMTI_EVENT_COMPILED_METHOD_UNLOAD - TOTAL_MIN_EVENT_TYPE_VAL));
    1.85 +static const jlong  GARBAGE_COLLECTION_START_BIT = (((jlong)1) << (JVMTI_EVENT_GARBAGE_COLLECTION_START - TOTAL_MIN_EVENT_TYPE_VAL));
    1.86 +static const jlong  GARBAGE_COLLECTION_FINISH_BIT = (((jlong)1) << (JVMTI_EVENT_GARBAGE_COLLECTION_FINISH - TOTAL_MIN_EVENT_TYPE_VAL));
    1.87 +static const jlong  OBJECT_FREE_BIT = (((jlong)1) << (JVMTI_EVENT_OBJECT_FREE - TOTAL_MIN_EVENT_TYPE_VAL));
    1.88 +static const jlong  RESOURCE_EXHAUSTED_BIT = (((jlong)1) << (JVMTI_EVENT_RESOURCE_EXHAUSTED - TOTAL_MIN_EVENT_TYPE_VAL));
    1.89 +static const jlong  VM_OBJECT_ALLOC_BIT = (((jlong)1) << (JVMTI_EVENT_VM_OBJECT_ALLOC - TOTAL_MIN_EVENT_TYPE_VAL));
    1.90 +
    1.91 +// bits for extension events
    1.92 +static const jlong  CLASS_UNLOAD_BIT = (((jlong)1) << (EXT_EVENT_CLASS_UNLOAD - TOTAL_MIN_EVENT_TYPE_VAL));
    1.93 +
    1.94 +
    1.95 +static const jlong  MONITOR_BITS = MONITOR_CONTENDED_ENTER_BIT | MONITOR_CONTENDED_ENTERED_BIT |
    1.96 +                          MONITOR_WAIT_BIT | MONITOR_WAITED_BIT;
    1.97 +static const jlong  EXCEPTION_BITS = EXCEPTION_THROW_BIT | EXCEPTION_CATCH_BIT;
    1.98 +static const jlong  INTERP_EVENT_BITS =  SINGLE_STEP_BIT | METHOD_ENTRY_BIT | METHOD_EXIT_BIT |
    1.99 +                                FRAME_POP_BIT | FIELD_ACCESS_BIT | FIELD_MODIFICATION_BIT;
   1.100 +static const jlong  THREAD_FILTERED_EVENT_BITS = INTERP_EVENT_BITS | EXCEPTION_BITS | MONITOR_BITS |
   1.101 +                                        BREAKPOINT_BIT | CLASS_LOAD_BIT | CLASS_PREPARE_BIT | THREAD_END_BIT;
   1.102 +static const jlong  NEED_THREAD_LIFE_EVENTS = THREAD_FILTERED_EVENT_BITS | THREAD_START_BIT;
   1.103 +static const jlong  EARLY_EVENT_BITS = CLASS_FILE_LOAD_HOOK_BIT |
   1.104 +                               VM_START_BIT | VM_INIT_BIT | VM_DEATH_BIT | NATIVE_METHOD_BIND_BIT |
   1.105 +                               THREAD_START_BIT | THREAD_END_BIT |
   1.106 +                               DYNAMIC_CODE_GENERATED_BIT;
   1.107 +static const jlong  GLOBAL_EVENT_BITS = ~THREAD_FILTERED_EVENT_BITS;
   1.108 +static const jlong  SHOULD_POST_ON_EXCEPTIONS_BITS = EXCEPTION_BITS | METHOD_EXIT_BIT | FRAME_POP_BIT;
   1.109 +
   1.110 +///////////////////////////////////////////////////////////////
   1.111 +//
   1.112 +// JvmtiEventEnabled
   1.113 +//
   1.114 +
   1.115 +JvmtiEventEnabled::JvmtiEventEnabled() {
   1.116 +  clear();
   1.117 +}
   1.118 +
   1.119 +
   1.120 +void JvmtiEventEnabled::clear() {
   1.121 +  _enabled_bits = 0;
   1.122 +#ifndef PRODUCT
   1.123 +  _init_guard = JEE_INIT_GUARD;
   1.124 +#endif
   1.125 +}
   1.126 +
   1.127 +void JvmtiEventEnabled::set_enabled(jvmtiEvent event_type, bool enabled) {
   1.128 +  jlong bits = get_bits();
   1.129 +  jlong mask = bit_for(event_type);
   1.130 +  if (enabled) {
   1.131 +    bits |= mask;
   1.132 +  } else {
   1.133 +    bits &= ~mask;
   1.134 +  }
   1.135 +  set_bits(bits);
   1.136 +}
   1.137 +
   1.138 +
   1.139 +///////////////////////////////////////////////////////////////
   1.140 +//
   1.141 +// JvmtiEnvThreadEventEnable
   1.142 +//
   1.143 +
   1.144 +JvmtiEnvThreadEventEnable::JvmtiEnvThreadEventEnable() {
   1.145 +  _event_user_enabled.clear();
   1.146 +  _event_enabled.clear();
   1.147 +}
   1.148 +
   1.149 +
   1.150 +JvmtiEnvThreadEventEnable::~JvmtiEnvThreadEventEnable() {
   1.151 +  _event_user_enabled.clear();
   1.152 +  _event_enabled.clear();
   1.153 +}
   1.154 +
   1.155 +
   1.156 +///////////////////////////////////////////////////////////////
   1.157 +//
   1.158 +// JvmtiThreadEventEnable
   1.159 +//
   1.160 +
   1.161 +JvmtiThreadEventEnable::JvmtiThreadEventEnable() {
   1.162 +  _event_enabled.clear();
   1.163 +}
   1.164 +
   1.165 +
   1.166 +JvmtiThreadEventEnable::~JvmtiThreadEventEnable() {
   1.167 +  _event_enabled.clear();
   1.168 +}
   1.169 +
   1.170 +
   1.171 +///////////////////////////////////////////////////////////////
   1.172 +//
   1.173 +// JvmtiEnvEventEnable
   1.174 +//
   1.175 +
   1.176 +JvmtiEnvEventEnable::JvmtiEnvEventEnable() {
   1.177 +  _event_user_enabled.clear();
   1.178 +  _event_callback_enabled.clear();
   1.179 +  _event_enabled.clear();
   1.180 +}
   1.181 +
   1.182 +
   1.183 +JvmtiEnvEventEnable::~JvmtiEnvEventEnable() {
   1.184 +  _event_user_enabled.clear();
   1.185 +  _event_callback_enabled.clear();
   1.186 +  _event_enabled.clear();
   1.187 +}
   1.188 +
   1.189 +
   1.190 +///////////////////////////////////////////////////////////////
   1.191 +//
   1.192 +// VM_EnterInterpOnlyMode
   1.193 +//
   1.194 +
   1.195 +class VM_EnterInterpOnlyMode : public VM_Operation {
   1.196 +private:
   1.197 +  JvmtiThreadState *_state;
   1.198 +
   1.199 +public:
   1.200 +  VM_EnterInterpOnlyMode(JvmtiThreadState *state);
   1.201 +
   1.202 +  bool allow_nested_vm_operations() const        { return true; }
   1.203 +  VMOp_Type type() const { return VMOp_EnterInterpOnlyMode; }
   1.204 +  void doit();
   1.205 +
   1.206 +  // to do: this same function is in jvmtiImpl - should be in one place
   1.207 +  bool can_be_deoptimized(vframe* vf) {
   1.208 +    return (vf->is_compiled_frame() && vf->fr().can_be_deoptimized());
   1.209 +  }
   1.210 +};
   1.211 +
   1.212 +VM_EnterInterpOnlyMode::VM_EnterInterpOnlyMode(JvmtiThreadState *state)
   1.213 +  : _state(state)
   1.214 +{
   1.215 +}
   1.216 +
   1.217 +
   1.218 +void VM_EnterInterpOnlyMode::doit() {
   1.219 +  // Set up the current stack depth for later tracking
   1.220 +  _state->invalidate_cur_stack_depth();
   1.221 +
   1.222 +  _state->enter_interp_only_mode();
   1.223 +
   1.224 +  JavaThread *thread = _state->get_thread();
   1.225 +  if (thread->has_last_Java_frame()) {
   1.226 +    // If running in fullspeed mode, single stepping is implemented
   1.227 +    // as follows: first, the interpreter does not dispatch to
   1.228 +    // compiled code for threads that have single stepping enabled;
   1.229 +    // second, we deoptimize all methods on the thread's stack when
   1.230 +    // interpreted-only mode is enabled the first time for a given
   1.231 +    // thread (nothing to do if no Java frames yet).
   1.232 +    int num_marked = 0;
   1.233 +    ResourceMark resMark;
   1.234 +    RegisterMap rm(thread, false);
   1.235 +    for (vframe* vf = thread->last_java_vframe(&rm); vf; vf = vf->sender()) {
   1.236 +      if (can_be_deoptimized(vf)) {
   1.237 +        ((compiledVFrame*) vf)->code()->mark_for_deoptimization();
   1.238 +        ++num_marked;
   1.239 +      }
   1.240 +    }
   1.241 +    if (num_marked > 0) {
   1.242 +      VM_Deoptimize op;
   1.243 +      VMThread::execute(&op);
   1.244 +    }
   1.245 +  }
   1.246 +}
   1.247 +
   1.248 +
   1.249 +///////////////////////////////////////////////////////////////
   1.250 +//
   1.251 +// VM_ChangeSingleStep
   1.252 +//
   1.253 +
   1.254 +class VM_ChangeSingleStep : public VM_Operation {
   1.255 +private:
   1.256 +  bool _on;
   1.257 +
   1.258 +public:
   1.259 +  VM_ChangeSingleStep(bool on);
   1.260 +  VMOp_Type type() const                         { return VMOp_ChangeSingleStep; }
   1.261 +  bool allow_nested_vm_operations() const        { return true; }
   1.262 +  void doit();   // method definition is after definition of JvmtiEventControllerPrivate because of scoping
   1.263 +};
   1.264 +
   1.265 +
   1.266 +VM_ChangeSingleStep::VM_ChangeSingleStep(bool on)
   1.267 +  : _on(on != 0)
   1.268 +{
   1.269 +}
   1.270 +
   1.271 +
   1.272 +
   1.273 +
   1.274 +///////////////////////////////////////////////////////////////
   1.275 +//
   1.276 +// JvmtiEventControllerPrivate
   1.277 +//
   1.278 +// Private internal implementation methods for JvmtiEventController.
   1.279 +//
   1.280 +// These methods are thread safe either because they are called
   1.281 +// in early VM initialization which is single threaded, or they
   1.282 +// hold the JvmtiThreadState_lock.
   1.283 +//
   1.284 +
   1.285 +class JvmtiEventControllerPrivate : public AllStatic {
   1.286 +  static bool _initialized;
   1.287 +public:
   1.288 +  static void set_should_post_single_step(bool on);
   1.289 +  static void enter_interp_only_mode(JvmtiThreadState *state);
   1.290 +  static void leave_interp_only_mode(JvmtiThreadState *state);
   1.291 +  static void recompute_enabled();
   1.292 +  static jlong recompute_env_enabled(JvmtiEnvBase* env);
   1.293 +  static jlong recompute_env_thread_enabled(JvmtiEnvThreadState* ets, JvmtiThreadState* state);
   1.294 +  static jlong recompute_thread_enabled(JvmtiThreadState *state);
   1.295 +  static void event_init();
   1.296 +
   1.297 +  static void set_user_enabled(JvmtiEnvBase *env, JavaThread *thread,
   1.298 +                        jvmtiEvent event_type, bool enabled);
   1.299 +  static void set_event_callbacks(JvmtiEnvBase *env,
   1.300 +                                  const jvmtiEventCallbacks* callbacks,
   1.301 +                                  jint size_of_callbacks);
   1.302 +
   1.303 +  static void set_extension_event_callback(JvmtiEnvBase *env,
   1.304 +                                           jint extension_event_index,
   1.305 +                                           jvmtiExtensionEvent callback);
   1.306 +
   1.307 +  static void set_frame_pop(JvmtiEnvThreadState *env_thread, JvmtiFramePop fpop);
   1.308 +  static void clear_frame_pop(JvmtiEnvThreadState *env_thread, JvmtiFramePop fpop);
   1.309 +  static void clear_to_frame_pop(JvmtiEnvThreadState *env_thread, JvmtiFramePop fpop);
   1.310 +  static void change_field_watch(jvmtiEvent event_type, bool added);
   1.311 +
   1.312 +  static void thread_started(JavaThread *thread);
   1.313 +  static void thread_ended(JavaThread *thread);
   1.314 +
   1.315 +  static void env_initialize(JvmtiEnvBase *env);
   1.316 +  static void env_dispose(JvmtiEnvBase *env);
   1.317 +
   1.318 +  static void vm_start();
   1.319 +  static void vm_init();
   1.320 +  static void vm_death();
   1.321 +
   1.322 +  static void trace_changed(JvmtiThreadState *state, jlong now_enabled, jlong changed);
   1.323 +  static void trace_changed(jlong now_enabled, jlong changed);
   1.324 +};
   1.325 +
   1.326 +bool JvmtiEventControllerPrivate::_initialized = false;
   1.327 +
   1.328 +void JvmtiEventControllerPrivate::set_should_post_single_step(bool on) {
   1.329 +  // we have permission to do this, VM op doesn't
   1.330 +  JvmtiExport::set_should_post_single_step(on);
   1.331 +}
   1.332 +
   1.333 +
   1.334 +// This change must always be occur when at a safepoint.
   1.335 +// Being at a safepoint causes the interpreter to use the
   1.336 +// safepoint dispatch table which we overload to find single
   1.337 +// step points.  Just to be sure that it has been set, we
   1.338 +// call notice_safepoints when turning on single stepping.
   1.339 +// When we leave our current safepoint, should_post_single_step
   1.340 +// will be checked by the interpreter, and the table kept
   1.341 +// or changed accordingly.
   1.342 +void VM_ChangeSingleStep::doit() {
   1.343 +  JvmtiEventControllerPrivate::set_should_post_single_step(_on);
   1.344 +  if (_on) {
   1.345 +    Interpreter::notice_safepoints();
   1.346 +  }
   1.347 +}
   1.348 +
   1.349 +
   1.350 +void JvmtiEventControllerPrivate::enter_interp_only_mode(JvmtiThreadState *state) {
   1.351 +  EC_TRACE(("JVMTI [%s] # Entering interpreter only mode",
   1.352 +            JvmtiTrace::safe_get_thread_name(state->get_thread())));
   1.353 +
   1.354 +  VM_EnterInterpOnlyMode op(state);
   1.355 +  VMThread::execute(&op);
   1.356 +}
   1.357 +
   1.358 +
   1.359 +void
   1.360 +JvmtiEventControllerPrivate::leave_interp_only_mode(JvmtiThreadState *state) {
   1.361 +  EC_TRACE(("JVMTI [%s] # Leaving interpreter only mode",
   1.362 +            JvmtiTrace::safe_get_thread_name(state->get_thread())));
   1.363 +  state->leave_interp_only_mode();
   1.364 +}
   1.365 +
   1.366 +
   1.367 +void
   1.368 +JvmtiEventControllerPrivate::trace_changed(JvmtiThreadState *state, jlong now_enabled, jlong changed) {
   1.369 +#ifdef JVMTI_TRACE
   1.370 +  if (JvmtiTrace::trace_event_controller()) {
   1.371 +    SafeResourceMark rm;
   1.372 +    // traces standard events only
   1.373 +    for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {
   1.374 +      jlong bit = JvmtiEventEnabled::bit_for((jvmtiEvent)ei);
   1.375 +      if (changed & bit) {
   1.376 +        // it changed, print it
   1.377 +        tty->print_cr("JVMTI [%s] # %s event %s",
   1.378 +                      JvmtiTrace::safe_get_thread_name(state->get_thread()),
   1.379 +                      (now_enabled & bit)? "Enabling" : "Disabling", JvmtiTrace::event_name((jvmtiEvent)ei));
   1.380 +      }
   1.381 +    }
   1.382 +  }
   1.383 +#endif /*JVMTI_TRACE */
   1.384 +}
   1.385 +
   1.386 +
   1.387 +void
   1.388 +JvmtiEventControllerPrivate::trace_changed(jlong now_enabled, jlong changed) {
   1.389 +#ifdef JVMTI_TRACE
   1.390 +  if (JvmtiTrace::trace_event_controller()) {
   1.391 +    SafeResourceMark rm;
   1.392 +    // traces standard events only
   1.393 +    for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {
   1.394 +      jlong bit = JvmtiEventEnabled::bit_for((jvmtiEvent)ei);
   1.395 +      if (changed & bit) {
   1.396 +        // it changed, print it
   1.397 +        tty->print_cr("JVMTI [-] # %s event %s",
   1.398 +                      (now_enabled & bit)? "Enabling" : "Disabling", JvmtiTrace::event_name((jvmtiEvent)ei));
   1.399 +      }
   1.400 +    }
   1.401 +  }
   1.402 +#endif /*JVMTI_TRACE */
   1.403 +}
   1.404 +
   1.405 +
   1.406 +// For the specified env: compute the currently truly enabled events
   1.407 +// set external state accordingly.
   1.408 +// Return value and set value must include all events.
   1.409 +// But outside this class, only non-thread-filtered events can be queried..
   1.410 +jlong
   1.411 +JvmtiEventControllerPrivate::recompute_env_enabled(JvmtiEnvBase* env) {
   1.412 +  jlong was_enabled = env->env_event_enable()->_event_enabled.get_bits();
   1.413 +  jlong now_enabled =
   1.414 +    env->env_event_enable()->_event_callback_enabled.get_bits() &
   1.415 +    env->env_event_enable()->_event_user_enabled.get_bits();
   1.416 +
   1.417 +  switch (JvmtiEnv::get_phase()) {
   1.418 +  case JVMTI_PHASE_PRIMORDIAL:
   1.419 +  case JVMTI_PHASE_ONLOAD:
   1.420 +    // only these events allowed in primordial or onload phase
   1.421 +    now_enabled &= (EARLY_EVENT_BITS & ~THREAD_FILTERED_EVENT_BITS);
   1.422 +    break;
   1.423 +  case JVMTI_PHASE_START:
   1.424 +    // only these events allowed in start phase
   1.425 +    now_enabled &= EARLY_EVENT_BITS;
   1.426 +    break;
   1.427 +  case JVMTI_PHASE_LIVE:
   1.428 +    // all events allowed during live phase
   1.429 +    break;
   1.430 +  case JVMTI_PHASE_DEAD:
   1.431 +    // no events allowed when dead
   1.432 +    now_enabled = 0;
   1.433 +    break;
   1.434 +  default:
   1.435 +    assert(false, "no other phases - sanity check");
   1.436 +    break;
   1.437 +  }
   1.438 +
   1.439 +  // will we really send these events to this env
   1.440 +  env->env_event_enable()->_event_enabled.set_bits(now_enabled);
   1.441 +
   1.442 +  trace_changed(now_enabled, (now_enabled ^ was_enabled)  & ~THREAD_FILTERED_EVENT_BITS);
   1.443 +
   1.444 +  return now_enabled;
   1.445 +}
   1.446 +
   1.447 +
   1.448 +// For the specified env and thread: compute the currently truly enabled events
   1.449 +// set external state accordingly.  Only thread-filtered events are included.
   1.450 +jlong
   1.451 +JvmtiEventControllerPrivate::recompute_env_thread_enabled(JvmtiEnvThreadState* ets, JvmtiThreadState* state) {
   1.452 +  JvmtiEnv *env = ets->get_env();
   1.453 +
   1.454 +  jlong was_enabled = ets->event_enable()->_event_enabled.get_bits();
   1.455 +  jlong now_enabled =  THREAD_FILTERED_EVENT_BITS &
   1.456 +    env->env_event_enable()->_event_callback_enabled.get_bits() &
   1.457 +    (env->env_event_enable()->_event_user_enabled.get_bits() |
   1.458 +     ets->event_enable()->_event_user_enabled.get_bits());
   1.459 +
   1.460 +  // for frame pops and field watchs, computed enabled state
   1.461 +  // is only true if an event has been requested
   1.462 +  if (!ets->has_frame_pops()) {
   1.463 +    now_enabled &= ~FRAME_POP_BIT;
   1.464 +  }
   1.465 +  if (*((int *)JvmtiExport::get_field_access_count_addr()) == 0) {
   1.466 +    now_enabled &= ~FIELD_ACCESS_BIT;
   1.467 +  }
   1.468 +  if (*((int *)JvmtiExport::get_field_modification_count_addr()) == 0) {
   1.469 +    now_enabled &= ~FIELD_MODIFICATION_BIT;
   1.470 +  }
   1.471 +
   1.472 +  switch (JvmtiEnv::get_phase()) {
   1.473 +  case JVMTI_PHASE_DEAD:
   1.474 +    // no events allowed when dead
   1.475 +    now_enabled = 0;
   1.476 +    break;
   1.477 +  }
   1.478 +
   1.479 +  // if anything changed do update
   1.480 +  if (now_enabled != was_enabled) {
   1.481 +
   1.482 +    // will we really send these events to this thread x env
   1.483 +    ets->event_enable()->_event_enabled.set_bits(now_enabled);
   1.484 +
   1.485 +    // If the enabled status of the single step or breakpoint events changed,
   1.486 +    // the location status may need to change as well.
   1.487 +    jlong changed = now_enabled ^ was_enabled;
   1.488 +    if (changed & SINGLE_STEP_BIT) {
   1.489 +      ets->reset_current_location(JVMTI_EVENT_SINGLE_STEP, (now_enabled & SINGLE_STEP_BIT) != 0);
   1.490 +    }
   1.491 +    if (changed & BREAKPOINT_BIT) {
   1.492 +      ets->reset_current_location(JVMTI_EVENT_BREAKPOINT,  (now_enabled & BREAKPOINT_BIT) != 0);
   1.493 +    }
   1.494 +    trace_changed(state, now_enabled, changed);
   1.495 +  }
   1.496 +  return now_enabled;
   1.497 +}
   1.498 +
   1.499 +
   1.500 +// For the specified thread: compute the currently truly enabled events
   1.501 +// set external state accordingly.  Only thread-filtered events are included.
   1.502 +jlong
   1.503 +JvmtiEventControllerPrivate::recompute_thread_enabled(JvmtiThreadState *state) {
   1.504 +  if (state == NULL) {
   1.505 +    // associated JavaThread is exiting
   1.506 +    return (jlong)0;
   1.507 +  }
   1.508 +
   1.509 +  jlong was_any_env_enabled = state->thread_event_enable()->_event_enabled.get_bits();
   1.510 +  jlong any_env_enabled = 0;
   1.511 +
   1.512 +  {
   1.513 +    // This iteration will include JvmtiEnvThreadStates whoses environments
   1.514 +    // have been disposed.  These JvmtiEnvThreadStates must not be filtered
   1.515 +    // as recompute must be called on them to disable their events,
   1.516 +    JvmtiEnvThreadStateIterator it(state);
   1.517 +    for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
   1.518 +      any_env_enabled |= recompute_env_thread_enabled(ets, state);
   1.519 +    }
   1.520 +  }
   1.521 +
   1.522 +  if (any_env_enabled != was_any_env_enabled) {
   1.523 +    // mark if event is truly enabled on this thread in any environment
   1.524 +    state->thread_event_enable()->_event_enabled.set_bits(any_env_enabled);
   1.525 +
   1.526 +    // compute interp_only mode
   1.527 +    bool should_be_interp = (any_env_enabled & INTERP_EVENT_BITS) != 0;
   1.528 +    bool is_now_interp = state->is_interp_only_mode();
   1.529 +
   1.530 +    if (should_be_interp != is_now_interp) {
   1.531 +      if (should_be_interp) {
   1.532 +        enter_interp_only_mode(state);
   1.533 +      } else {
   1.534 +        leave_interp_only_mode(state);
   1.535 +      }
   1.536 +    }
   1.537 +
   1.538 +    // update the JavaThread cached value for thread-specific should_post_on_exceptions value
   1.539 +    bool should_post_on_exceptions = (any_env_enabled & SHOULD_POST_ON_EXCEPTIONS_BITS) != 0;
   1.540 +    state->set_should_post_on_exceptions(should_post_on_exceptions);
   1.541 +  }
   1.542 +
   1.543 +  return any_env_enabled;
   1.544 +}
   1.545 +
   1.546 +
   1.547 +// Compute truly enabled events - meaning if the event can and could be
   1.548 +// sent.  An event is truly enabled if it is user enabled on the thread
   1.549 +// or globally user enabled, but only if there is a callback or event hook
   1.550 +// for it and, for field watch and frame pop, one has been set.
   1.551 +// Compute if truly enabled, per thread, per environment, per combination
   1.552 +// (thread x environment), and overall.  These merges are true if any is true.
   1.553 +// True per thread if some environment has callback set and the event is globally
   1.554 +// enabled or enabled for this thread.
   1.555 +// True per environment if the callback is set and the event is globally
   1.556 +// enabled in this environment or enabled for any thread in this environment.
   1.557 +// True per combination if the environment has the callback set and the
   1.558 +// event is globally enabled in this environment or the event is enabled
   1.559 +// for this thread and environment.
   1.560 +//
   1.561 +// All states transitions dependent on these transitions are also handled here.
   1.562 +void
   1.563 +JvmtiEventControllerPrivate::recompute_enabled() {
   1.564 +  assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
   1.565 +
   1.566 +  // event enabled for any thread in any environment
   1.567 +  jlong was_any_env_thread_enabled = JvmtiEventController::_universal_global_event_enabled.get_bits();
   1.568 +  jlong any_env_thread_enabled = 0;
   1.569 +
   1.570 +  EC_TRACE(("JVMTI [-] # recompute enabled - before %llx", was_any_env_thread_enabled));
   1.571 +
   1.572 +  // compute non-thread-filters events.
   1.573 +  // This must be done separately from thread-filtered events, since some
   1.574 +  // events can occur before any threads exist.
   1.575 +  JvmtiEnvIterator it;
   1.576 +  for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) {
   1.577 +    any_env_thread_enabled |= recompute_env_enabled(env);
   1.578 +  }
   1.579 +
   1.580 +  // We need to create any missing jvmti_thread_state if there are globally set thread
   1.581 +  // filtered events and there weren't last time
   1.582 +  if (    (any_env_thread_enabled & THREAD_FILTERED_EVENT_BITS) != 0 &&
   1.583 +      (was_any_env_thread_enabled & THREAD_FILTERED_EVENT_BITS) == 0) {
   1.584 +    assert(JvmtiEnv::is_vm_live() || (JvmtiEnv::get_phase()==JVMTI_PHASE_START),
   1.585 +      "thread filtered events should not be enabled when VM not in start or live phase");
   1.586 +    {
   1.587 +      MutexLocker mu(Threads_lock);   //hold the Threads_lock for the iteration
   1.588 +      for (JavaThread *tp = Threads::first(); tp != NULL; tp = tp->next()) {
   1.589 +        // state_for_while_locked() makes tp->is_exiting() check
   1.590 +        JvmtiThreadState::state_for_while_locked(tp);  // create the thread state if missing
   1.591 +      }
   1.592 +    }// release Threads_lock
   1.593 +  }
   1.594 +
   1.595 +  // compute and set thread-filtered events
   1.596 +  for (JvmtiThreadState *state = JvmtiThreadState::first(); state != NULL; state = state->next()) {
   1.597 +    any_env_thread_enabled |= recompute_thread_enabled(state);
   1.598 +  }
   1.599 +
   1.600 +  // set universal state (across all envs and threads)
   1.601 +  jlong delta = any_env_thread_enabled ^ was_any_env_thread_enabled;
   1.602 +  if (delta != 0) {
   1.603 +    JvmtiExport::set_should_post_field_access((any_env_thread_enabled & FIELD_ACCESS_BIT) != 0);
   1.604 +    JvmtiExport::set_should_post_field_modification((any_env_thread_enabled & FIELD_MODIFICATION_BIT) != 0);
   1.605 +    JvmtiExport::set_should_post_class_load((any_env_thread_enabled & CLASS_LOAD_BIT) != 0);
   1.606 +    JvmtiExport::set_should_post_class_file_load_hook((any_env_thread_enabled & CLASS_FILE_LOAD_HOOK_BIT) != 0);
   1.607 +    JvmtiExport::set_should_post_native_method_bind((any_env_thread_enabled & NATIVE_METHOD_BIND_BIT) != 0);
   1.608 +    JvmtiExport::set_should_post_dynamic_code_generated((any_env_thread_enabled & DYNAMIC_CODE_GENERATED_BIT) != 0);
   1.609 +    JvmtiExport::set_should_post_data_dump((any_env_thread_enabled & DATA_DUMP_BIT) != 0);
   1.610 +    JvmtiExport::set_should_post_class_prepare((any_env_thread_enabled & CLASS_PREPARE_BIT) != 0);
   1.611 +    JvmtiExport::set_should_post_class_unload((any_env_thread_enabled & CLASS_UNLOAD_BIT) != 0);
   1.612 +    JvmtiExport::set_should_post_monitor_contended_enter((any_env_thread_enabled & MONITOR_CONTENDED_ENTER_BIT) != 0);
   1.613 +    JvmtiExport::set_should_post_monitor_contended_entered((any_env_thread_enabled & MONITOR_CONTENDED_ENTERED_BIT) != 0);
   1.614 +    JvmtiExport::set_should_post_monitor_wait((any_env_thread_enabled & MONITOR_WAIT_BIT) != 0);
   1.615 +    JvmtiExport::set_should_post_monitor_waited((any_env_thread_enabled & MONITOR_WAITED_BIT) != 0);
   1.616 +    JvmtiExport::set_should_post_garbage_collection_start((any_env_thread_enabled & GARBAGE_COLLECTION_START_BIT) != 0);
   1.617 +    JvmtiExport::set_should_post_garbage_collection_finish((any_env_thread_enabled & GARBAGE_COLLECTION_FINISH_BIT) != 0);
   1.618 +    JvmtiExport::set_should_post_object_free((any_env_thread_enabled & OBJECT_FREE_BIT) != 0);
   1.619 +    JvmtiExport::set_should_post_resource_exhausted((any_env_thread_enabled & RESOURCE_EXHAUSTED_BIT) != 0);
   1.620 +    JvmtiExport::set_should_post_compiled_method_load((any_env_thread_enabled & COMPILED_METHOD_LOAD_BIT) != 0);
   1.621 +    JvmtiExport::set_should_post_compiled_method_unload((any_env_thread_enabled & COMPILED_METHOD_UNLOAD_BIT) != 0);
   1.622 +    JvmtiExport::set_should_post_vm_object_alloc((any_env_thread_enabled & VM_OBJECT_ALLOC_BIT) != 0);
   1.623 +
   1.624 +    // need this if we want thread events or we need them to init data
   1.625 +    JvmtiExport::set_should_post_thread_life((any_env_thread_enabled & NEED_THREAD_LIFE_EVENTS) != 0);
   1.626 +
   1.627 +    // If single stepping is turned on or off, execute the VM op to change it.
   1.628 +    if (delta & SINGLE_STEP_BIT) {
   1.629 +      switch (JvmtiEnv::get_phase()) {
   1.630 +      case JVMTI_PHASE_DEAD:
   1.631 +        // If the VM is dying we can't execute VM ops
   1.632 +        break;
   1.633 +      case JVMTI_PHASE_LIVE: {
   1.634 +        VM_ChangeSingleStep op((any_env_thread_enabled & SINGLE_STEP_BIT) != 0);
   1.635 +        VMThread::execute(&op);
   1.636 +        break;
   1.637 +      }
   1.638 +      default:
   1.639 +        assert(false, "should never come here before live phase");
   1.640 +        break;
   1.641 +      }
   1.642 +    }
   1.643 +
   1.644 +    // set global truly enabled, that is, any thread in any environment
   1.645 +    JvmtiEventController::_universal_global_event_enabled.set_bits(any_env_thread_enabled);
   1.646 +
   1.647 +    // set global should_post_on_exceptions
   1.648 +    JvmtiExport::set_should_post_on_exceptions((any_env_thread_enabled & SHOULD_POST_ON_EXCEPTIONS_BITS) != 0);
   1.649 +
   1.650 +  }
   1.651 +
   1.652 +  EC_TRACE(("JVMTI [-] # recompute enabled - after %llx", any_env_thread_enabled));
   1.653 +}
   1.654 +
   1.655 +
   1.656 +void
   1.657 +JvmtiEventControllerPrivate::thread_started(JavaThread *thread) {
   1.658 +  assert(thread->is_Java_thread(), "Must be JavaThread");
   1.659 +  assert(thread == Thread::current(), "must be current thread");
   1.660 +  assert(JvmtiEnvBase::environments_might_exist(), "to enter event controller, JVM TI environments must exist");
   1.661 +
   1.662 +  EC_TRACE(("JVMTI [%s] # thread started", JvmtiTrace::safe_get_thread_name(thread)));
   1.663 +
   1.664 +  // if we have any thread filtered events globally enabled, create/update the thread state
   1.665 +  if ((JvmtiEventController::_universal_global_event_enabled.get_bits() & THREAD_FILTERED_EVENT_BITS) != 0) {
   1.666 +    MutexLocker mu(JvmtiThreadState_lock);
   1.667 +    // create the thread state if missing
   1.668 +    JvmtiThreadState *state = JvmtiThreadState::state_for_while_locked(thread);
   1.669 +    if (state != NULL) {    // skip threads with no JVMTI thread state
   1.670 +      recompute_thread_enabled(state);
   1.671 +    }
   1.672 +  }
   1.673 +}
   1.674 +
   1.675 +
   1.676 +void
   1.677 +JvmtiEventControllerPrivate::thread_ended(JavaThread *thread) {
   1.678 +  // Removes the JvmtiThreadState associated with the specified thread.
   1.679 +  // May be called after all environments have been disposed.
   1.680 +  assert(JvmtiThreadState_lock->is_locked(), "sanity check");
   1.681 +
   1.682 +  EC_TRACE(("JVMTI [%s] # thread ended", JvmtiTrace::safe_get_thread_name(thread)));
   1.683 +
   1.684 +  JvmtiThreadState *state = thread->jvmti_thread_state();
   1.685 +  assert(state != NULL, "else why are we here?");
   1.686 +  delete state;
   1.687 +}
   1.688 +
   1.689 +void JvmtiEventControllerPrivate::set_event_callbacks(JvmtiEnvBase *env,
   1.690 +                                                      const jvmtiEventCallbacks* callbacks,
   1.691 +                                                      jint size_of_callbacks) {
   1.692 +  assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
   1.693 +  EC_TRACE(("JVMTI [*] # set event callbacks"));
   1.694 +
   1.695 +  env->set_event_callbacks(callbacks, size_of_callbacks);
   1.696 +  jlong enabled_bits = 0;
   1.697 +  for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {
   1.698 +    jvmtiEvent evt_t = (jvmtiEvent)ei;
   1.699 +    if (env->has_callback(evt_t)) {
   1.700 +      enabled_bits |= JvmtiEventEnabled::bit_for(evt_t);
   1.701 +    }
   1.702 +  }
   1.703 +  env->env_event_enable()->_event_callback_enabled.set_bits(enabled_bits);
   1.704 +  recompute_enabled();
   1.705 +}
   1.706 +
   1.707 +void
   1.708 +JvmtiEventControllerPrivate::set_extension_event_callback(JvmtiEnvBase *env,
   1.709 +                                                          jint extension_event_index,
   1.710 +                                                          jvmtiExtensionEvent callback)
   1.711 +{
   1.712 +  assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
   1.713 +  EC_TRACE(("JVMTI [*] # set extension event callback"));
   1.714 +
   1.715 +  // extension events are allocated below JVMTI_MIN_EVENT_TYPE_VAL
   1.716 +  assert(extension_event_index >= (jint)EXT_MIN_EVENT_TYPE_VAL &&
   1.717 +         extension_event_index <= (jint)EXT_MAX_EVENT_TYPE_VAL, "sanity check");
   1.718 +
   1.719 +
   1.720 +  // As the bits for both standard (jvmtiEvent) and extension
   1.721 +  // (jvmtiExtEvents) are stored in the same word we cast here to
   1.722 +  // jvmtiEvent to set/clear the bit for this extension event.
   1.723 +  jvmtiEvent event_type = (jvmtiEvent)extension_event_index;
   1.724 +
   1.725 +  // Prevent a possible race condition where events are re-enabled by a call to
   1.726 +  // set event callbacks, where the DisposeEnvironment occurs after the boiler-plate
   1.727 +  // environment check and before the lock is acquired.
   1.728 +  // We can safely do the is_valid check now, as JvmtiThreadState_lock is held.
   1.729 +  bool enabling = (callback != NULL) && (env->is_valid());
   1.730 +  env->env_event_enable()->set_user_enabled(event_type, enabling);
   1.731 +
   1.732 +  // update the callback
   1.733 +  jvmtiExtEventCallbacks* ext_callbacks = env->ext_callbacks();
   1.734 +  switch (extension_event_index) {
   1.735 +    case EXT_EVENT_CLASS_UNLOAD :
   1.736 +      ext_callbacks->ClassUnload = callback;
   1.737 +      break;
   1.738 +    default:
   1.739 +      ShouldNotReachHere();
   1.740 +  }
   1.741 +
   1.742 +  // update the callback enable/disable bit
   1.743 +  jlong enabled_bits = env->env_event_enable()->_event_callback_enabled.get_bits();
   1.744 +  jlong bit_for = JvmtiEventEnabled::bit_for(event_type);
   1.745 +  if (enabling) {
   1.746 +    enabled_bits |= bit_for;
   1.747 +  } else {
   1.748 +    enabled_bits &= ~bit_for;
   1.749 +  }
   1.750 +  env->env_event_enable()->_event_callback_enabled.set_bits(enabled_bits);
   1.751 +
   1.752 +  recompute_enabled();
   1.753 +}
   1.754 +
   1.755 +
   1.756 +void
   1.757 +JvmtiEventControllerPrivate::env_initialize(JvmtiEnvBase *env) {
   1.758 +  assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
   1.759 +  EC_TRACE(("JVMTI [*] # env initialize"));
   1.760 +
   1.761 +  if (JvmtiEnvBase::is_vm_live()) {
   1.762 +    // if we didn't initialize event info already (this is a late
   1.763 +    // launched environment), do it now.
   1.764 +    event_init();
   1.765 +  }
   1.766 +
   1.767 +  env->initialize();
   1.768 +
   1.769 +  // add the JvmtiEnvThreadState to each JvmtiThreadState
   1.770 +  for (JvmtiThreadState *state = JvmtiThreadState::first(); state != NULL; state = state->next()) {
   1.771 +    state->add_env(env);
   1.772 +    assert((JvmtiEnv*)(state->env_thread_state(env)->get_env()) == env, "sanity check");
   1.773 +  }
   1.774 +  JvmtiEventControllerPrivate::recompute_enabled();
   1.775 +}
   1.776 +
   1.777 +
   1.778 +void
   1.779 +JvmtiEventControllerPrivate::env_dispose(JvmtiEnvBase *env) {
   1.780 +  assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
   1.781 +  EC_TRACE(("JVMTI [*] # env dispose"));
   1.782 +
   1.783 +  // Before the environment is marked disposed, disable all events on this
   1.784 +  // environment (by zapping the callbacks).  As a result, the disposed
   1.785 +  // environment will not call event handlers.
   1.786 +  set_event_callbacks(env, NULL, 0);
   1.787 +  for (jint extension_event_index = EXT_MIN_EVENT_TYPE_VAL;
   1.788 +       extension_event_index <= EXT_MAX_EVENT_TYPE_VAL;
   1.789 +       ++extension_event_index) {
   1.790 +    set_extension_event_callback(env, extension_event_index, NULL);
   1.791 +  }
   1.792 +
   1.793 +  // Let the environment finish disposing itself.
   1.794 +  env->env_dispose();
   1.795 +}
   1.796 +
   1.797 +
   1.798 +void
   1.799 +JvmtiEventControllerPrivate::set_user_enabled(JvmtiEnvBase *env, JavaThread *thread,
   1.800 +                                          jvmtiEvent event_type, bool enabled) {
   1.801 +  assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
   1.802 +
   1.803 +  EC_TRACE(("JVMTI [%s] # user %s event %s",
   1.804 +            thread==NULL? "ALL": JvmtiTrace::safe_get_thread_name(thread),
   1.805 +            enabled? "enabled" : "disabled", JvmtiTrace::event_name(event_type)));
   1.806 +
   1.807 +  if (thread == NULL) {
   1.808 +    env->env_event_enable()->set_user_enabled(event_type, enabled);
   1.809 +  } else {
   1.810 +    // create the thread state (if it didn't exist before)
   1.811 +    JvmtiThreadState *state = JvmtiThreadState::state_for_while_locked(thread);
   1.812 +    if (state != NULL) {
   1.813 +      state->env_thread_state(env)->event_enable()->set_user_enabled(event_type, enabled);
   1.814 +    }
   1.815 +  }
   1.816 +  recompute_enabled();
   1.817 +}
   1.818 +
   1.819 +
   1.820 +void
   1.821 +JvmtiEventControllerPrivate::set_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
   1.822 +  EC_TRACE(("JVMTI [%s] # set frame pop - frame=%d",
   1.823 +            JvmtiTrace::safe_get_thread_name(ets->get_thread()),
   1.824 +            fpop.frame_number() ));
   1.825 +
   1.826 +  ets->get_frame_pops()->set(fpop);
   1.827 +  recompute_thread_enabled(ets->get_thread()->jvmti_thread_state());
   1.828 +}
   1.829 +
   1.830 +
   1.831 +void
   1.832 +JvmtiEventControllerPrivate::clear_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
   1.833 +  EC_TRACE(("JVMTI [%s] # clear frame pop - frame=%d",
   1.834 +            JvmtiTrace::safe_get_thread_name(ets->get_thread()),
   1.835 +            fpop.frame_number() ));
   1.836 +
   1.837 +  ets->get_frame_pops()->clear(fpop);
   1.838 +  recompute_thread_enabled(ets->get_thread()->jvmti_thread_state());
   1.839 +}
   1.840 +
   1.841 +
   1.842 +void
   1.843 +JvmtiEventControllerPrivate::clear_to_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
   1.844 +  int cleared_cnt = ets->get_frame_pops()->clear_to(fpop);
   1.845 +
   1.846 +  EC_TRACE(("JVMTI [%s] # clear to frame pop - frame=%d, count=%d",
   1.847 +            JvmtiTrace::safe_get_thread_name(ets->get_thread()),
   1.848 +            fpop.frame_number(),
   1.849 +            cleared_cnt ));
   1.850 +
   1.851 +  if (cleared_cnt > 0) {
   1.852 +    recompute_thread_enabled(ets->get_thread()->jvmti_thread_state());
   1.853 +  }
   1.854 +}
   1.855 +
   1.856 +void
   1.857 +JvmtiEventControllerPrivate::change_field_watch(jvmtiEvent event_type, bool added) {
   1.858 +  int *count_addr;
   1.859 +
   1.860 +  switch (event_type) {
   1.861 +  case JVMTI_EVENT_FIELD_MODIFICATION:
   1.862 +    count_addr = (int *)JvmtiExport::get_field_modification_count_addr();
   1.863 +    break;
   1.864 +  case JVMTI_EVENT_FIELD_ACCESS:
   1.865 +    count_addr = (int *)JvmtiExport::get_field_access_count_addr();
   1.866 +    break;
   1.867 +  default:
   1.868 +    assert(false, "incorrect event");
   1.869 +    return;
   1.870 +  }
   1.871 +
   1.872 +  EC_TRACE(("JVMTI [-] # change field watch - %s %s count=%d",
   1.873 +            event_type==JVMTI_EVENT_FIELD_MODIFICATION? "modification" : "access",
   1.874 +            added? "add" : "remove",
   1.875 +            *count_addr));
   1.876 +
   1.877 +  if (added) {
   1.878 +    (*count_addr)++;
   1.879 +    if (*count_addr == 1) {
   1.880 +      recompute_enabled();
   1.881 +    }
   1.882 +  } else {
   1.883 +    if (*count_addr > 0) {
   1.884 +      (*count_addr)--;
   1.885 +      if (*count_addr == 0) {
   1.886 +        recompute_enabled();
   1.887 +      }
   1.888 +    } else {
   1.889 +      assert(false, "field watch out of phase");
   1.890 +    }
   1.891 +  }
   1.892 +}
   1.893 +
   1.894 +void
   1.895 +JvmtiEventControllerPrivate::event_init() {
   1.896 +  assert(JvmtiThreadState_lock->is_locked(), "sanity check");
   1.897 +
   1.898 +  if (_initialized) {
   1.899 +    return;
   1.900 +  }
   1.901 +
   1.902 +  EC_TRACE(("JVMTI [-] # VM live"));
   1.903 +
   1.904 +#ifdef ASSERT
   1.905 +  // check that our idea and the spec's idea of threaded events match
   1.906 +  for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {
   1.907 +    jlong bit = JvmtiEventEnabled::bit_for((jvmtiEvent)ei);
   1.908 +    assert(((THREAD_FILTERED_EVENT_BITS & bit) != 0) == JvmtiUtil::event_threaded(ei),
   1.909 +           "thread filtered event list does not match");
   1.910 +  }
   1.911 +#endif
   1.912 +
   1.913 +  _initialized = true;
   1.914 +}
   1.915 +
   1.916 +void
   1.917 +JvmtiEventControllerPrivate::vm_start() {
   1.918 +  // some events are now able to be enabled (phase has changed)
   1.919 +  JvmtiEventControllerPrivate::recompute_enabled();
   1.920 +}
   1.921 +
   1.922 +
   1.923 +void
   1.924 +JvmtiEventControllerPrivate::vm_init() {
   1.925 +  event_init();
   1.926 +
   1.927 +  // all the events are now able to be enabled (phase has changed)
   1.928 +  JvmtiEventControllerPrivate::recompute_enabled();
   1.929 +}
   1.930 +
   1.931 +
   1.932 +void
   1.933 +JvmtiEventControllerPrivate::vm_death() {
   1.934 +  // events are disabled (phase has changed)
   1.935 +  JvmtiEventControllerPrivate::recompute_enabled();
   1.936 +}
   1.937 +
   1.938 +
   1.939 +///////////////////////////////////////////////////////////////
   1.940 +//
   1.941 +// JvmtiEventController
   1.942 +//
   1.943 +
   1.944 +JvmtiEventEnabled JvmtiEventController::_universal_global_event_enabled;
   1.945 +
   1.946 +bool
   1.947 +JvmtiEventController::is_global_event(jvmtiEvent event_type) {
   1.948 +  assert(is_valid_event_type(event_type), "invalid event type");
   1.949 +  jlong bit_for = ((jlong)1) << (event_type - TOTAL_MIN_EVENT_TYPE_VAL);
   1.950 +  return((bit_for & GLOBAL_EVENT_BITS)!=0);
   1.951 +}
   1.952 +
   1.953 +void
   1.954 +JvmtiEventController::set_user_enabled(JvmtiEnvBase *env, JavaThread *thread, jvmtiEvent event_type, bool enabled) {
   1.955 +  if (Threads::number_of_threads() == 0) {
   1.956 +    // during early VM start-up locks don't exist, but we are safely single threaded,
   1.957 +    // call the functionality without holding the JvmtiThreadState_lock.
   1.958 +    JvmtiEventControllerPrivate::set_user_enabled(env, thread, event_type, enabled);
   1.959 +  } else {
   1.960 +    MutexLocker mu(JvmtiThreadState_lock);
   1.961 +    JvmtiEventControllerPrivate::set_user_enabled(env, thread, event_type, enabled);
   1.962 +  }
   1.963 +}
   1.964 +
   1.965 +
   1.966 +void
   1.967 +JvmtiEventController::set_event_callbacks(JvmtiEnvBase *env,
   1.968 +                                          const jvmtiEventCallbacks* callbacks,
   1.969 +                                          jint size_of_callbacks) {
   1.970 +  if (Threads::number_of_threads() == 0) {
   1.971 +    // during early VM start-up locks don't exist, but we are safely single threaded,
   1.972 +    // call the functionality without holding the JvmtiThreadState_lock.
   1.973 +    JvmtiEventControllerPrivate::set_event_callbacks(env, callbacks, size_of_callbacks);
   1.974 +  } else {
   1.975 +    MutexLocker mu(JvmtiThreadState_lock);
   1.976 +    JvmtiEventControllerPrivate::set_event_callbacks(env, callbacks, size_of_callbacks);
   1.977 +  }
   1.978 +}
   1.979 +
   1.980 +void
   1.981 +JvmtiEventController::set_extension_event_callback(JvmtiEnvBase *env,
   1.982 +                                                   jint extension_event_index,
   1.983 +                                                   jvmtiExtensionEvent callback) {
   1.984 +  if (Threads::number_of_threads() == 0) {
   1.985 +    JvmtiEventControllerPrivate::set_extension_event_callback(env, extension_event_index, callback);
   1.986 +  } else {
   1.987 +    MutexLocker mu(JvmtiThreadState_lock);
   1.988 +    JvmtiEventControllerPrivate::set_extension_event_callback(env, extension_event_index, callback);
   1.989 +  }
   1.990 +}
   1.991 +
   1.992 +
   1.993 +
   1.994 +
   1.995 +void
   1.996 +JvmtiEventController::set_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
   1.997 +  MutexLocker mu(JvmtiThreadState_lock);
   1.998 +  JvmtiEventControllerPrivate::set_frame_pop(ets, fpop);
   1.999 +}
  1.1000 +
  1.1001 +
  1.1002 +void
  1.1003 +JvmtiEventController::clear_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
  1.1004 +  MutexLocker mu(JvmtiThreadState_lock);
  1.1005 +  JvmtiEventControllerPrivate::clear_frame_pop(ets, fpop);
  1.1006 +}
  1.1007 +
  1.1008 +
  1.1009 +void
  1.1010 +JvmtiEventController::clear_to_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
  1.1011 +  MutexLocker mu(JvmtiThreadState_lock);
  1.1012 +  JvmtiEventControllerPrivate::clear_to_frame_pop(ets, fpop);
  1.1013 +}
  1.1014 +
  1.1015 +void
  1.1016 +JvmtiEventController::change_field_watch(jvmtiEvent event_type, bool added) {
  1.1017 +  MutexLocker mu(JvmtiThreadState_lock);
  1.1018 +  JvmtiEventControllerPrivate::change_field_watch(event_type, added);
  1.1019 +}
  1.1020 +
  1.1021 +void
  1.1022 +JvmtiEventController::thread_started(JavaThread *thread) {
  1.1023 +  // operates only on the current thread
  1.1024 +  // JvmtiThreadState_lock grabbed only if needed.
  1.1025 +  JvmtiEventControllerPrivate::thread_started(thread);
  1.1026 +}
  1.1027 +
  1.1028 +void
  1.1029 +JvmtiEventController::thread_ended(JavaThread *thread) {
  1.1030 +  // operates only on the current thread
  1.1031 +  // JvmtiThreadState_lock grabbed only if needed.
  1.1032 +  JvmtiEventControllerPrivate::thread_ended(thread);
  1.1033 +}
  1.1034 +
  1.1035 +void
  1.1036 +JvmtiEventController::env_initialize(JvmtiEnvBase *env) {
  1.1037 +  if (Threads::number_of_threads() == 0) {
  1.1038 +    // during early VM start-up locks don't exist, but we are safely single threaded,
  1.1039 +    // call the functionality without holding the JvmtiThreadState_lock.
  1.1040 +    JvmtiEventControllerPrivate::env_initialize(env);
  1.1041 +  } else {
  1.1042 +    MutexLocker mu(JvmtiThreadState_lock);
  1.1043 +    JvmtiEventControllerPrivate::env_initialize(env);
  1.1044 +  }
  1.1045 +}
  1.1046 +
  1.1047 +void
  1.1048 +JvmtiEventController::env_dispose(JvmtiEnvBase *env) {
  1.1049 +  if (Threads::number_of_threads() == 0) {
  1.1050 +    // during early VM start-up locks don't exist, but we are safely single threaded,
  1.1051 +    // call the functionality without holding the JvmtiThreadState_lock.
  1.1052 +    JvmtiEventControllerPrivate::env_dispose(env);
  1.1053 +  } else {
  1.1054 +    MutexLocker mu(JvmtiThreadState_lock);
  1.1055 +    JvmtiEventControllerPrivate::env_dispose(env);
  1.1056 +  }
  1.1057 +}
  1.1058 +
  1.1059 +
  1.1060 +void
  1.1061 +JvmtiEventController::vm_start() {
  1.1062 +  if (JvmtiEnvBase::environments_might_exist()) {
  1.1063 +    MutexLocker mu(JvmtiThreadState_lock);
  1.1064 +    JvmtiEventControllerPrivate::vm_start();
  1.1065 +  }
  1.1066 +}
  1.1067 +
  1.1068 +void
  1.1069 +JvmtiEventController::vm_init() {
  1.1070 +  if (JvmtiEnvBase::environments_might_exist()) {
  1.1071 +    MutexLocker mu(JvmtiThreadState_lock);
  1.1072 +    JvmtiEventControllerPrivate::vm_init();
  1.1073 +  }
  1.1074 +}
  1.1075 +
  1.1076 +void
  1.1077 +JvmtiEventController::vm_death() {
  1.1078 +  if (JvmtiEnvBase::environments_might_exist()) {
  1.1079 +    MutexLocker mu(JvmtiThreadState_lock);
  1.1080 +    JvmtiEventControllerPrivate::vm_death();
  1.1081 +  }
  1.1082 +}

mercurial