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