/* * Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. *
*/
class EnterInterpOnlyModeClosure : public HandshakeClosure { private: bool _completed; public:
EnterInterpOnlyModeClosure() : HandshakeClosure("EnterInterpOnlyMode"), _completed(false) { } void do_thread(Thread* th) {
JavaThread* jt = JavaThread::cast(th);
JvmtiThreadState* state = jt->jvmti_thread_state();
assert(state != NULL, "sanity check");
assert(state->get_thread() == jt, "handshake unsafe conditions"); if (!state->is_pending_interp_only_mode()) {
_completed = true; return; // The pending flag has been already cleared, so bail out.
}
state->set_pending_interp_only_mode(false); // Clear the pending flag.
// invalidate_cur_stack_depth is called in enter_interp_only_mode
state->enter_interp_only_mode();
Continuation::set_cont_fastpath_thread_state(jt);
if (jt->has_last_Java_frame()) { // If running in fullspeed mode, single stepping is implemented // as follows: first, the interpreter does not dispatch to // compiled code for threads that have single stepping enabled; // second, we deoptimize all compiled java frames on the thread's stack when // interpreted-only mode is enabled the first time for a given // thread (nothing to do if no Java frames yet).
ResourceMark resMark; for (StackFrameStream fst(jt, false/* update */, false /* process_frames */); !fst.is_done(); fst.next()) { if (fst.current()->can_be_deoptimized()) {
Deoptimization::deoptimize(jt, *fst.current());
}
}
}
_completed = true;
} bool completed() { return _completed;
}
};
/////////////////////////////////////////////////////////////// // // JvmtiEventControllerPrivate // // Private internal implementation methods for JvmtiEventController. // // These methods are thread safe either because they are called // in early VM initialization which is single threaded, or they // hold the JvmtiThreadState_lock. //
void JvmtiEventControllerPrivate::set_should_post_single_step(bool on) { // we have permission to do this, VM op doesn't
JvmtiExport::set_should_post_single_step(on);
}
// When _on == true, we use the safepoint interpreter dispatch table // to allow us to find the single step points. Otherwise, we switch // back to the regular interpreter dispatch table. // Note: We call Interpreter::notice_safepoints() and ignore_safepoints() // in a VM_Operation to safely make the dispatch table switch. We // no longer rely on the safepoint mechanism to do any of this work // for us. void VM_ChangeSingleStep::doit() {
log_debug(interpreter, safepoint)("changing single step to '%s'", _on ? "on" : "off");
JvmtiEventControllerPrivate::set_should_post_single_step(_on); if (_on) {
Interpreter::notice_safepoints();
} else {
Interpreter::ignore_safepoints();
}
}
assert(state != NULL, "sanity check"); if (state->is_pending_interp_only_mode()) { return; // An EnterInterpOnlyModeClosure handshake is already pending for execution.
} // This flag will be cleared in EnterInterpOnlyModeClosure handshake.
state->set_pending_interp_only_mode(true); if (target == NULL) { // an unmounted virtual thread return; // EnterInterpOnlyModeClosure will be executed right after mount.
}
EnterInterpOnlyModeClosure hs; if (target->is_handshake_safe_for(current)) {
hs.do_thread(target);
} else {
assert(state->get_thread() != NULL, "sanity check");
Handshake::execute(&hs, target);
guarantee(hs.completed(), "Handshake failed: Target thread is not alive?");
}
}
void
JvmtiEventControllerPrivate::leave_interp_only_mode(JvmtiThreadState *state) {
EC_TRACE(("[%s] # Leaving interpreter only mode",
JvmtiTrace::safe_get_thread_name(state->get_thread_or_saved()))); if (state->is_pending_interp_only_mode()) {
state->set_pending_interp_only_mode(false); // Just clear the pending flag.
assert(!state->is_interp_only_mode(), "sanity check"); return;
}
state->leave_interp_only_mode();
}
void
JvmtiEventControllerPrivate::trace_changed(JvmtiThreadState *state, jlong now_enabled, jlong changed) { #ifdef JVMTI_TRACE if (JvmtiTrace::trace_event_controller()) {
SafeResourceMark rm; // traces standard events only for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {
jlong bit = JvmtiEventEnabled::bit_for((jvmtiEvent)ei); if (changed & bit) { // it changed, print it
log_trace(jvmti)("[%s] # %s event %s",
JvmtiTrace::safe_get_thread_name(state->get_thread_or_saved()),
(now_enabled & bit)? "Enabling" : "Disabling", JvmtiTrace::event_name((jvmtiEvent)ei));
}
}
} #endif/*JVMTI_TRACE */
}
void
JvmtiEventControllerPrivate::trace_changed(jlong now_enabled, jlong changed) { #ifdef JVMTI_TRACE if (JvmtiTrace::trace_event_controller()) {
SafeResourceMark rm; // traces standard events only for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {
jlong bit = JvmtiEventEnabled::bit_for((jvmtiEvent)ei); if (changed & bit) { // it changed, print it
log_trace(jvmti)("[-] # %s event %s",
(now_enabled & bit)? "Enabling" : "Disabling", JvmtiTrace::event_name((jvmtiEvent)ei));
}
}
} #endif/*JVMTI_TRACE */
}
void
JvmtiEventControllerPrivate::flush_object_free_events(JvmtiEnvBase* env) { // Some of the objects recorded by this env may have died. If we're // (potentially) changing the enable state for ObjectFree events, we // need to ensure the env is cleaned up and any events that should // be posted are posted.
JvmtiTagMap* tag_map = env->tag_map_acquire(); if (tag_map != NULL) {
tag_map->flush_object_free_events();
}
}
void
JvmtiEventControllerPrivate::set_enabled_events_with_lock(JvmtiEnvBase* env, jlong now_enabled) { // The state for ObjectFree events must be enabled or disabled // under the TagMap lock, to allow pending object posting events to complete.
JvmtiTagMap* tag_map = env->tag_map_acquire(); if (tag_map != NULL) {
MutexLocker ml(tag_map->lock(), Mutex::_no_safepoint_check_flag);
env->env_event_enable()->_event_enabled.set_bits(now_enabled);
} else {
env->env_event_enable()->_event_enabled.set_bits(now_enabled);
}
}
// For the specified env: compute the currently truly enabled events // set external state accordingly. // Return value and set value must include all events. // But outside this class, only non-thread-filtered events can be queried..
jlong
JvmtiEventControllerPrivate::recompute_env_enabled(JvmtiEnvBase* env) {
jlong was_enabled = env->env_event_enable()->_event_enabled.get_bits();
jlong now_enabled =
env->env_event_enable()->_event_callback_enabled.get_bits() &
env->env_event_enable()->_event_user_enabled.get_bits();
switch (env->phase()) { case JVMTI_PHASE_PRIMORDIAL: case JVMTI_PHASE_ONLOAD: // only these events allowed in primordial or onload phase
now_enabled &= (EARLY_EVENT_BITS & ~THREAD_FILTERED_EVENT_BITS); break; case JVMTI_PHASE_START: // only these events allowed in start phase
now_enabled &= EARLY_EVENT_BITS; break; case JVMTI_PHASE_LIVE: // all events allowed during live phase break; case JVMTI_PHASE_DEAD: // no events allowed when dead
now_enabled = 0; break; default:
assert(false, "no other phases - sanity check"); break;
}
// Set/reset the event enabled under the tagmap lock.
set_enabled_events_with_lock(env, now_enabled);
// For the specified env and thread: compute the currently truly enabled events // set external state accordingly. Only thread-filtered events are included.
jlong
JvmtiEventControllerPrivate::recompute_env_thread_enabled(JvmtiEnvThreadState* ets, JvmtiThreadState* state) {
JvmtiEnv *env = ets->get_env();
// for frame pops and field watchs, computed enabled state // is only true if an event has been requested if (!ets->has_frame_pops()) {
now_enabled &= ~FRAME_POP_BIT;
} if (*((int *)JvmtiExport::get_field_access_count_addr()) == 0) {
now_enabled &= ~FIELD_ACCESS_BIT;
} if (*((int *)JvmtiExport::get_field_modification_count_addr()) == 0) {
now_enabled &= ~FIELD_MODIFICATION_BIT;
}
switch (JvmtiEnv::get_phase()) { case JVMTI_PHASE_DEAD: // no events allowed when dead
now_enabled = 0; break; default: break;
}
// if anything changed do update if (now_enabled != was_enabled) {
// will we really send these events to this thread x env
ets->event_enable()->_event_enabled.set_bits(now_enabled);
// If the enabled status of the single step or breakpoint events changed, // the location status may need to change as well.
jlong changed = now_enabled ^ was_enabled; if (changed & SINGLE_STEP_BIT) {
ets->reset_current_location(JVMTI_EVENT_SINGLE_STEP, (now_enabled & SINGLE_STEP_BIT) != 0);
} if (changed & BREAKPOINT_BIT) {
ets->reset_current_location(JVMTI_EVENT_BREAKPOINT, (now_enabled & BREAKPOINT_BIT) != 0);
}
trace_changed(state, now_enabled, changed);
} return now_enabled;
}
// For the specified thread: compute the currently truly enabled events // set external state accordingly. Only thread-filtered events are included.
jlong
JvmtiEventControllerPrivate::recompute_thread_enabled(JvmtiThreadState *state) { if (state == NULL) { // associated JavaThread is exiting return (jlong)0;
}
julong was_any_env_enabled = state->thread_event_enable()->_event_enabled.get_bits();
julong any_env_enabled = 0; // JVMTI_EVENT_FRAME_POP can be disabled (in the case FRAME_POP_BIT is not set), // but we need to set interp_only if some JvmtiEnvThreadState has frame pop set // to clear the request bool has_frame_pops = false;
{ // This iteration will include JvmtiEnvThreadStates whose environments // have been disposed. These JvmtiEnvThreadStates must not be filtered // as recompute must be called on them to disable their events,
JvmtiEnvThreadStateIterator it(state); for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
any_env_enabled |= recompute_env_thread_enabled(ets, state);
has_frame_pops |= ets->has_frame_pops();
}
}
if (any_env_enabled != was_any_env_enabled) { // mark if event is truly enabled on this thread in any environment
state->thread_event_enable()->_event_enabled.set_bits(any_env_enabled);
if (state->get_thread() != NULL) { // The JavaThread for carrier or mounted virtual thread case. // Update the cached value for thread-specific should_post_on_exceptions value. bool should_post_on_exceptions = (any_env_enabled & SHOULD_POST_ON_EXCEPTIONS_BITS) != 0;
state->set_should_post_on_exceptions(should_post_on_exceptions);
}
} // compute interp_only mode bool should_be_interp = (any_env_enabled & INTERP_EVENT_BITS) != 0 || has_frame_pops; bool is_now_interp = state->is_interp_only_mode();
if (should_be_interp != is_now_interp) { if (should_be_interp) {
enter_interp_only_mode(state);
} else {
leave_interp_only_mode(state);
}
} return any_env_enabled;
}
// Compute truly enabled events - meaning if the event can and could be // sent. An event is truly enabled if it is user enabled on the thread // or globally user enabled, but only if there is a callback or event hook // for it and, for field watch and frame pop, one has been set. // Compute if truly enabled, per thread, per environment, per combination // (thread x environment), and overall. These merges are true if any is true. // True per thread if some environment has callback set and the event is globally // enabled or enabled for this thread. // True per environment if the callback is set and the event is globally // enabled in this environment or enabled for any thread in this environment. // True per combination if the environment has the callback set and the // event is globally enabled in this environment or the event is enabled // for this thread and environment. // // All states transitions dependent on these transitions are also handled here. void
JvmtiEventControllerPrivate::recompute_enabled() {
assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
// event enabled for any thread in any environment
julong was_any_env_thread_enabled = JvmtiEventController::_universal_global_event_enabled.get_bits();
julong any_env_thread_enabled = 0;
EC_TRACE(("[-] # recompute enabled - before " JULONG_FORMAT_X, was_any_env_thread_enabled));
// Compute non-thread-filtered events. // This must be done separately from thread-filtered events, since some // events can occur before any threads exist.
JvmtiEnvIterator it; for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) {
any_env_thread_enabled |= recompute_env_enabled(env);
}
// We need to create any missing jvmti_thread_state if there are globally set thread // filtered events and there weren't last time if ( (any_env_thread_enabled & THREAD_FILTERED_EVENT_BITS) != 0 &&
(was_any_env_thread_enabled & THREAD_FILTERED_EVENT_BITS) == 0) { for (JavaThreadIteratorWithHandle jtiwh; JavaThread *tp = jtiwh.next(); ) {
oop jt_oop = tp->threadObj();
// create the thread state if missing, state_for_while_locked() makes tp->is_exiting() check
JvmtiThreadState* state = JvmtiThreadState::state_for_while_locked(tp, jt_oop);
// create the thread state for mounted virtual thread if missing
oop vt_oop = tp->jvmti_vthread(); if (vt_oop != NULL && java_lang_VirtualThread::is_instance(vt_oop)) {
state = JvmtiThreadState::state_for_while_locked(tp, vt_oop);
}
}
}
// compute and set thread-filtered events
JvmtiThreadState *state = JvmtiThreadState::first(); if (state != nullptr) { // If we have a JvmtiThreadState, then we've reached the point where // threads can exist so create a ThreadsListHandle to protect them. // The held JvmtiThreadState_lock prevents exiting JavaThreads from // being removed from the JvmtiThreadState list we're about to walk // so this ThreadsListHandle exists just to satisfy the lower level sanity // checks that the target JavaThreads are protected.
ThreadsListHandle tlh; for (; state != nullptr; state = state->next()) {
any_env_thread_enabled |= recompute_thread_enabled(state);
}
}
// need this if we want thread events or we need them to init data
JvmtiExport::set_should_post_thread_life((any_env_thread_enabled & NEED_THREAD_LIFE_EVENTS) != 0);
// If single stepping is turned on or off, execute the VM op to change it. if (delta & SINGLE_STEP_BIT) { switch (JvmtiEnv::get_phase()) { case JVMTI_PHASE_DEAD: // If the VM is dying we can't execute VM ops break; case JVMTI_PHASE_LIVE: {
VM_ChangeSingleStep op((any_env_thread_enabled & SINGLE_STEP_BIT) != 0);
VMThread::execute(&op); break;
} default:
assert(false, "should never come here before live phase"); break;
}
}
// set global truly enabled, that is, any thread in any environment
JvmtiEventController::_universal_global_event_enabled.set_bits(any_env_thread_enabled);
// set global should_post_on_exceptions
JvmtiExport::set_should_post_on_exceptions((any_env_thread_enabled & SHOULD_POST_ON_EXCEPTIONS_BITS) != 0);
}
EC_TRACE(("[-] # recompute enabled - after " JULONG_FORMAT_X, any_env_thread_enabled));
}
void
JvmtiEventControllerPrivate::thread_started(JavaThread *thread) {
assert(thread == Thread::current(), "must be current thread");
assert(JvmtiEnvBase::environments_might_exist(), "to enter event controller, JVM TI environments must exist");
// if we have any thread filtered events globally enabled, create/update the thread state if ((JvmtiEventController::_universal_global_event_enabled.get_bits() & THREAD_FILTERED_EVENT_BITS) != 0) {
MutexLocker mu(JvmtiThreadState_lock); // create the thread state if missing
JvmtiThreadState *state = JvmtiThreadState::state_for_while_locked(thread); if (state != NULL) { // skip threads with no JVMTI thread state
recompute_thread_enabled(state);
}
}
}
void
JvmtiEventControllerPrivate::thread_ended(JavaThread *thread) { // Removes the JvmtiThreadState associated with the specified thread. // May be called after all environments have been disposed.
assert(JvmtiThreadState_lock->is_locked(), "sanity check");
// As the bits for both standard (jvmtiEvent) and extension // (jvmtiExtEvents) are stored in the same word we cast here to // jvmtiEvent to set/clear the bit for this extension event.
jvmtiEvent event_type = (jvmtiEvent)extension_event_index;
// Prevent a possible race condition where events are re-enabled by a call to // set event callbacks, where the DisposeEnvironment occurs after the boiler-plate // environment check and before the lock is acquired. // We can safely do the is_valid check now, as JvmtiThreadState_lock is held. bool enabling = (callback != NULL) && (env->is_valid());
// update the callback
jvmtiExtEventCallbacks* ext_callbacks = env->ext_callbacks(); switch (extension_event_index) { case EXT_EVENT_CLASS_UNLOAD :
ext_callbacks->ClassUnload = callback; break; case EXT_EVENT_VIRTUAL_THREAD_MOUNT :
ext_callbacks->VirtualThreadMount = callback; break; case EXT_EVENT_VIRTUAL_THREAD_UNMOUNT :
ext_callbacks->VirtualThreadUnmount = callback; break; default:
ShouldNotReachHere();
}
// update the callback enable/disable bit
jlong enabled_bits = env->env_event_enable()->_event_callback_enabled.get_bits();
jlong bit_for = JvmtiEventEnabled::bit_for(event_type); if (enabling) {
enabled_bits |= bit_for;
} else {
enabled_bits &= ~bit_for;
}
env->env_event_enable()->_event_callback_enabled.set_bits(enabled_bits);
if (JvmtiEnvBase::is_vm_live()) { // if we didn't initialize event info already (this is a late // launched environment), do it now.
event_init();
}
env->initialize();
// add the JvmtiEnvThreadState to each JvmtiThreadState for (JvmtiThreadState *state = JvmtiThreadState::first(); state != NULL; state = state->next()) {
state->add_env(env);
assert((JvmtiEnv*)(state->env_thread_state(env)->get_env()) == env, "sanity check");
}
JvmtiEventControllerPrivate::recompute_enabled();
}
// Before the environment is marked disposed, disable all events on this // environment (by zapping the callbacks). As a result, the disposed // environment will not call event handlers.
set_event_callbacks(env, NULL, 0); for (jint extension_event_index = EXT_MIN_EVENT_TYPE_VAL;
extension_event_index <= EXT_MAX_EVENT_TYPE_VAL;
++extension_event_index) {
set_extension_event_callback(env, extension_event_index, NULL);
}
// Let the environment finish disposing itself.
env->env_dispose();
}
#ifdef ASSERT // check that our idea and the spec's idea of threaded events match for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {
jlong bit = JvmtiEventEnabled::bit_for((jvmtiEvent)ei);
assert(((THREAD_FILTERED_EVENT_BITS & bit) != 0) == JvmtiUtil::event_threaded(ei), "thread filtered event list does not match");
} #endif
_initialized = true;
}
void
JvmtiEventControllerPrivate::vm_start() { // some events are now able to be enabled (phase has changed)
JvmtiEventControllerPrivate::recompute_enabled();
}
if (Threads::number_of_threads() == 0) { // during early VM start-up locks don't exist, but we are safely single threaded, // call the functionality without holding the JvmtiThreadState_lock.
JvmtiEventControllerPrivate::set_user_enabled(env, thread, Handle(), event_type, enabled);
} else {
Thread* current = Thread::current();
HandleMark hmi(current);
Handle thread_oop_h = Handle(current, thread_oop);
MutexLocker mu(JvmtiThreadState_lock);
JvmtiEventControllerPrivate::set_user_enabled(env, thread, thread_oop_h, event_type, enabled);
}
}
void
JvmtiEventController::set_event_callbacks(JvmtiEnvBase *env, const jvmtiEventCallbacks* callbacks,
jint size_of_callbacks) { if (Threads::number_of_threads() == 0) { // during early VM start-up locks don't exist, but we are safely single threaded, // call the functionality without holding the JvmtiThreadState_lock.
JvmtiEventControllerPrivate::set_event_callbacks(env, callbacks, size_of_callbacks);
} else {
MutexLocker mu(JvmtiThreadState_lock);
JvmtiEventControllerPrivate::set_event_callbacks(env, callbacks, size_of_callbacks);
}
}
// Called by just mounted virtual thread if pending_interp_only_mode() is set. void
JvmtiEventController::enter_interp_only_mode() {
Thread *current = Thread::current();
EnterInterpOnlyModeClosure hs;
hs.do_thread(current);
}
void
JvmtiEventController::thread_started(JavaThread *thread) { // operates only on the current thread // JvmtiThreadState_lock grabbed only if needed.
JvmtiEventControllerPrivate::thread_started(thread);
}
void
JvmtiEventController::thread_ended(JavaThread *thread) { // operates only on the current thread // JvmtiThreadState_lock grabbed only if needed.
JvmtiEventControllerPrivate::thread_ended(thread);
}
void
JvmtiEventController::env_initialize(JvmtiEnvBase *env) { if (Threads::number_of_threads() == 0) { // during early VM start-up locks don't exist, but we are safely single threaded, // call the functionality without holding the JvmtiThreadState_lock.
JvmtiEventControllerPrivate::env_initialize(env);
} else {
MutexLocker mu(JvmtiThreadState_lock);
JvmtiEventControllerPrivate::env_initialize(env);
}
}
void
JvmtiEventController::env_dispose(JvmtiEnvBase *env) { if (Threads::number_of_threads() == 0) { // during early VM start-up locks don't exist, but we are safely single threaded, // call the functionality without holding the JvmtiThreadState_lock.
JvmtiEventControllerPrivate::env_dispose(env);
} else {
MutexLocker mu(JvmtiThreadState_lock);
JvmtiEventControllerPrivate::env_dispose(env);
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.