src/share/vm/prims/jvmtiEventController.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6680
78bbf4d43a14
parent 0
f90c822e73f8
permissions
-rw-r--r--

merge

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

mercurial