1 /*
2 * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
454 (env->env_event_enable()->_event_user_enabled.get_bits() |
455 ets->event_enable()->_event_user_enabled.get_bits());
456
457 // for frame pops and field watchs, computed enabled state
458 // is only true if an event has been requested
459 if (!ets->has_frame_pops()) {
460 now_enabled &= ~FRAME_POP_BIT;
461 }
462 if (*((int *)JvmtiExport::get_field_access_count_addr()) == 0) {
463 now_enabled &= ~FIELD_ACCESS_BIT;
464 }
465 if (*((int *)JvmtiExport::get_field_modification_count_addr()) == 0) {
466 now_enabled &= ~FIELD_MODIFICATION_BIT;
467 }
468
469 switch (JvmtiEnv::get_phase()) {
470 case JVMTI_PHASE_DEAD:
471 // no events allowed when dead
472 now_enabled = 0;
473 break;
474 }
475
476 // if anything changed do update
477 if (now_enabled != was_enabled) {
478
479 // will we really send these events to this thread x env
480 ets->event_enable()->_event_enabled.set_bits(now_enabled);
481
482 // If the enabled status of the single step or breakpoint events changed,
483 // the location status may need to change as well.
484 jlong changed = now_enabled ^ was_enabled;
485 if (changed & SINGLE_STEP_BIT) {
486 ets->reset_current_location(JVMTI_EVENT_SINGLE_STEP, (now_enabled & SINGLE_STEP_BIT) != 0);
487 }
488 if (changed & BREAKPOINT_BIT) {
489 ets->reset_current_location(JVMTI_EVENT_BREAKPOINT, (now_enabled & BREAKPOINT_BIT) != 0);
490 }
491 trace_changed(state, now_enabled, changed);
492 }
493 return now_enabled;
494 }
495
496
497 // For the specified thread: compute the currently truly enabled events
498 // set external state accordingly. Only thread-filtered events are included.
499 jlong
500 JvmtiEventControllerPrivate::recompute_thread_enabled(JvmtiThreadState *state) {
501 if (state == NULL) {
502 // associated JavaThread is exiting
503 return (jlong)0;
504 }
505
506 jlong was_any_env_enabled = state->thread_event_enable()->_event_enabled.get_bits();
507 jlong any_env_enabled = 0;
508
509 {
510 // This iteration will include JvmtiEnvThreadStates whoses environments
511 // have been disposed. These JvmtiEnvThreadStates must not be filtered
512 // as recompute must be called on them to disable their events,
513 JvmtiEnvThreadStateIterator it(state);
514 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
515 any_env_enabled |= recompute_env_thread_enabled(ets, state);
516 }
517 }
518
519 if (any_env_enabled != was_any_env_enabled) {
520 // mark if event is truly enabled on this thread in any environment
521 state->thread_event_enable()->_event_enabled.set_bits(any_env_enabled);
522
523 // compute interp_only mode
524 bool should_be_interp = (any_env_enabled & INTERP_EVENT_BITS) != 0;
525 bool is_now_interp = state->is_interp_only_mode();
526
527 if (should_be_interp != is_now_interp) {
544 // Compute truly enabled events - meaning if the event can and could be
545 // sent. An event is truly enabled if it is user enabled on the thread
546 // or globally user enabled, but only if there is a callback or event hook
547 // for it and, for field watch and frame pop, one has been set.
548 // Compute if truly enabled, per thread, per environment, per combination
549 // (thread x environment), and overall. These merges are true if any is true.
550 // True per thread if some environment has callback set and the event is globally
551 // enabled or enabled for this thread.
552 // True per environment if the callback is set and the event is globally
553 // enabled in this environment or enabled for any thread in this environment.
554 // True per combination if the environment has the callback set and the
555 // event is globally enabled in this environment or the event is enabled
556 // for this thread and environment.
557 //
558 // All states transitions dependent on these transitions are also handled here.
559 void
560 JvmtiEventControllerPrivate::recompute_enabled() {
561 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
562
563 // event enabled for any thread in any environment
564 jlong was_any_env_thread_enabled = JvmtiEventController::_universal_global_event_enabled.get_bits();
565 jlong any_env_thread_enabled = 0;
566
567 EC_TRACE(("[-] # recompute enabled - before " UINT64_FORMAT_X, was_any_env_thread_enabled));
568
569 // compute non-thread-filters events.
570 // This must be done separately from thread-filtered events, since some
571 // events can occur before any threads exist.
572 JvmtiEnvIterator it;
573 for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) {
574 any_env_thread_enabled |= recompute_env_enabled(env);
575 }
576
577 // We need to create any missing jvmti_thread_state if there are globally set thread
578 // filtered events and there weren't last time
579 if ( (any_env_thread_enabled & THREAD_FILTERED_EVENT_BITS) != 0 &&
580 (was_any_env_thread_enabled & THREAD_FILTERED_EVENT_BITS) == 0) {
581 {
582 MutexLocker mu(Threads_lock); //hold the Threads_lock for the iteration
583 for (JavaThread *tp = Threads::first(); tp != NULL; tp = tp->next()) {
584 // state_for_while_locked() makes tp->is_exiting() check
585 JvmtiThreadState::state_for_while_locked(tp); // create the thread state if missing
586 }
587 }// release Threads_lock
627 break;
628 case JVMTI_PHASE_LIVE: {
629 VM_ChangeSingleStep op((any_env_thread_enabled & SINGLE_STEP_BIT) != 0);
630 VMThread::execute(&op);
631 break;
632 }
633 default:
634 assert(false, "should never come here before live phase");
635 break;
636 }
637 }
638
639 // set global truly enabled, that is, any thread in any environment
640 JvmtiEventController::_universal_global_event_enabled.set_bits(any_env_thread_enabled);
641
642 // set global should_post_on_exceptions
643 JvmtiExport::set_should_post_on_exceptions((any_env_thread_enabled & SHOULD_POST_ON_EXCEPTIONS_BITS) != 0);
644
645 }
646
647 EC_TRACE(("[-] # recompute enabled - after " UINT64_FORMAT_X, any_env_thread_enabled));
648 }
649
650
651 void
652 JvmtiEventControllerPrivate::thread_started(JavaThread *thread) {
653 assert(thread->is_Java_thread(), "Must be JavaThread");
654 assert(thread == Thread::current(), "must be current thread");
655 assert(JvmtiEnvBase::environments_might_exist(), "to enter event controller, JVM TI environments must exist");
656
657 EC_TRACE(("[%s] # thread started", JvmtiTrace::safe_get_thread_name(thread)));
658
659 // if we have any thread filtered events globally enabled, create/update the thread state
660 if ((JvmtiEventController::_universal_global_event_enabled.get_bits() & THREAD_FILTERED_EVENT_BITS) != 0) {
661 MutexLocker mu(JvmtiThreadState_lock);
662 // create the thread state if missing
663 JvmtiThreadState *state = JvmtiThreadState::state_for_while_locked(thread);
664 if (state != NULL) { // skip threads with no JVMTI thread state
665 recompute_thread_enabled(state);
666 }
667 }
|
1 /*
2 * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
454 (env->env_event_enable()->_event_user_enabled.get_bits() |
455 ets->event_enable()->_event_user_enabled.get_bits());
456
457 // for frame pops and field watchs, computed enabled state
458 // is only true if an event has been requested
459 if (!ets->has_frame_pops()) {
460 now_enabled &= ~FRAME_POP_BIT;
461 }
462 if (*((int *)JvmtiExport::get_field_access_count_addr()) == 0) {
463 now_enabled &= ~FIELD_ACCESS_BIT;
464 }
465 if (*((int *)JvmtiExport::get_field_modification_count_addr()) == 0) {
466 now_enabled &= ~FIELD_MODIFICATION_BIT;
467 }
468
469 switch (JvmtiEnv::get_phase()) {
470 case JVMTI_PHASE_DEAD:
471 // no events allowed when dead
472 now_enabled = 0;
473 break;
474 default:
475 break;
476 }
477
478 // if anything changed do update
479 if (now_enabled != was_enabled) {
480
481 // will we really send these events to this thread x env
482 ets->event_enable()->_event_enabled.set_bits(now_enabled);
483
484 // If the enabled status of the single step or breakpoint events changed,
485 // the location status may need to change as well.
486 jlong changed = now_enabled ^ was_enabled;
487 if (changed & SINGLE_STEP_BIT) {
488 ets->reset_current_location(JVMTI_EVENT_SINGLE_STEP, (now_enabled & SINGLE_STEP_BIT) != 0);
489 }
490 if (changed & BREAKPOINT_BIT) {
491 ets->reset_current_location(JVMTI_EVENT_BREAKPOINT, (now_enabled & BREAKPOINT_BIT) != 0);
492 }
493 trace_changed(state, now_enabled, changed);
494 }
495 return now_enabled;
496 }
497
498
499 // For the specified thread: compute the currently truly enabled events
500 // set external state accordingly. Only thread-filtered events are included.
501 jlong
502 JvmtiEventControllerPrivate::recompute_thread_enabled(JvmtiThreadState *state) {
503 if (state == NULL) {
504 // associated JavaThread is exiting
505 return (jlong)0;
506 }
507
508 julong was_any_env_enabled = state->thread_event_enable()->_event_enabled.get_bits();
509 julong any_env_enabled = 0;
510
511 {
512 // This iteration will include JvmtiEnvThreadStates whoses environments
513 // have been disposed. These JvmtiEnvThreadStates must not be filtered
514 // as recompute must be called on them to disable their events,
515 JvmtiEnvThreadStateIterator it(state);
516 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
517 any_env_enabled |= recompute_env_thread_enabled(ets, state);
518 }
519 }
520
521 if (any_env_enabled != was_any_env_enabled) {
522 // mark if event is truly enabled on this thread in any environment
523 state->thread_event_enable()->_event_enabled.set_bits(any_env_enabled);
524
525 // compute interp_only mode
526 bool should_be_interp = (any_env_enabled & INTERP_EVENT_BITS) != 0;
527 bool is_now_interp = state->is_interp_only_mode();
528
529 if (should_be_interp != is_now_interp) {
546 // Compute truly enabled events - meaning if the event can and could be
547 // sent. An event is truly enabled if it is user enabled on the thread
548 // or globally user enabled, but only if there is a callback or event hook
549 // for it and, for field watch and frame pop, one has been set.
550 // Compute if truly enabled, per thread, per environment, per combination
551 // (thread x environment), and overall. These merges are true if any is true.
552 // True per thread if some environment has callback set and the event is globally
553 // enabled or enabled for this thread.
554 // True per environment if the callback is set and the event is globally
555 // enabled in this environment or enabled for any thread in this environment.
556 // True per combination if the environment has the callback set and the
557 // event is globally enabled in this environment or the event is enabled
558 // for this thread and environment.
559 //
560 // All states transitions dependent on these transitions are also handled here.
561 void
562 JvmtiEventControllerPrivate::recompute_enabled() {
563 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
564
565 // event enabled for any thread in any environment
566 julong was_any_env_thread_enabled = JvmtiEventController::_universal_global_event_enabled.get_bits();
567 julong any_env_thread_enabled = 0;
568
569 EC_TRACE(("[-] # recompute enabled - before " JULONG_FORMAT_X, was_any_env_thread_enabled));
570
571 // compute non-thread-filters events.
572 // This must be done separately from thread-filtered events, since some
573 // events can occur before any threads exist.
574 JvmtiEnvIterator it;
575 for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) {
576 any_env_thread_enabled |= recompute_env_enabled(env);
577 }
578
579 // We need to create any missing jvmti_thread_state if there are globally set thread
580 // filtered events and there weren't last time
581 if ( (any_env_thread_enabled & THREAD_FILTERED_EVENT_BITS) != 0 &&
582 (was_any_env_thread_enabled & THREAD_FILTERED_EVENT_BITS) == 0) {
583 {
584 MutexLocker mu(Threads_lock); //hold the Threads_lock for the iteration
585 for (JavaThread *tp = Threads::first(); tp != NULL; tp = tp->next()) {
586 // state_for_while_locked() makes tp->is_exiting() check
587 JvmtiThreadState::state_for_while_locked(tp); // create the thread state if missing
588 }
589 }// release Threads_lock
629 break;
630 case JVMTI_PHASE_LIVE: {
631 VM_ChangeSingleStep op((any_env_thread_enabled & SINGLE_STEP_BIT) != 0);
632 VMThread::execute(&op);
633 break;
634 }
635 default:
636 assert(false, "should never come here before live phase");
637 break;
638 }
639 }
640
641 // set global truly enabled, that is, any thread in any environment
642 JvmtiEventController::_universal_global_event_enabled.set_bits(any_env_thread_enabled);
643
644 // set global should_post_on_exceptions
645 JvmtiExport::set_should_post_on_exceptions((any_env_thread_enabled & SHOULD_POST_ON_EXCEPTIONS_BITS) != 0);
646
647 }
648
649 EC_TRACE(("[-] # recompute enabled - after " JULONG_FORMAT_X, any_env_thread_enabled));
650 }
651
652
653 void
654 JvmtiEventControllerPrivate::thread_started(JavaThread *thread) {
655 assert(thread->is_Java_thread(), "Must be JavaThread");
656 assert(thread == Thread::current(), "must be current thread");
657 assert(JvmtiEnvBase::environments_might_exist(), "to enter event controller, JVM TI environments must exist");
658
659 EC_TRACE(("[%s] # thread started", JvmtiTrace::safe_get_thread_name(thread)));
660
661 // if we have any thread filtered events globally enabled, create/update the thread state
662 if ((JvmtiEventController::_universal_global_event_enabled.get_bits() & THREAD_FILTERED_EVENT_BITS) != 0) {
663 MutexLocker mu(JvmtiThreadState_lock);
664 // create the thread state if missing
665 JvmtiThreadState *state = JvmtiThreadState::state_for_while_locked(thread);
666 if (state != NULL) { // skip threads with no JVMTI thread state
667 recompute_thread_enabled(state);
668 }
669 }
|