Path: blob/master/src/hotspot/share/prims/jvmtiEventController.cpp
41145 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "interpreter/interpreter.hpp"26#include "jvmtifiles/jvmtiEnv.hpp"27#include "logging/log.hpp"28#include "memory/resourceArea.hpp"29#include "prims/jvmtiEventController.hpp"30#include "prims/jvmtiEventController.inline.hpp"31#include "prims/jvmtiExport.hpp"32#include "prims/jvmtiImpl.hpp"33#include "prims/jvmtiTagMap.hpp"34#include "prims/jvmtiThreadState.inline.hpp"35#include "runtime/deoptimization.hpp"36#include "runtime/frame.inline.hpp"37#include "runtime/stackFrameStream.inline.hpp"38#include "runtime/thread.inline.hpp"39#include "runtime/threadSMR.hpp"40#include "runtime/vframe.hpp"41#include "runtime/vframe_hp.hpp"42#include "runtime/vmThread.hpp"43#include "runtime/vmOperations.hpp"4445#ifdef JVMTI_TRACE46#define EC_TRACE(out) do { \47if (JvmtiTrace::trace_event_controller()) { \48SafeResourceMark rm; \49log_trace(jvmti) out; \50} \51} while (0)52#else53#define EC_TRACE(out)54#endif /*JVMTI_TRACE */5556// bits for standard events5758static const jlong SINGLE_STEP_BIT = (((jlong)1) << (JVMTI_EVENT_SINGLE_STEP - TOTAL_MIN_EVENT_TYPE_VAL));59static const jlong FRAME_POP_BIT = (((jlong)1) << (JVMTI_EVENT_FRAME_POP - TOTAL_MIN_EVENT_TYPE_VAL));60static const jlong BREAKPOINT_BIT = (((jlong)1) << (JVMTI_EVENT_BREAKPOINT - TOTAL_MIN_EVENT_TYPE_VAL));61static const jlong FIELD_ACCESS_BIT = (((jlong)1) << (JVMTI_EVENT_FIELD_ACCESS - TOTAL_MIN_EVENT_TYPE_VAL));62static const jlong FIELD_MODIFICATION_BIT = (((jlong)1) << (JVMTI_EVENT_FIELD_MODIFICATION - TOTAL_MIN_EVENT_TYPE_VAL));63static const jlong METHOD_ENTRY_BIT = (((jlong)1) << (JVMTI_EVENT_METHOD_ENTRY - TOTAL_MIN_EVENT_TYPE_VAL));64static const jlong METHOD_EXIT_BIT = (((jlong)1) << (JVMTI_EVENT_METHOD_EXIT - TOTAL_MIN_EVENT_TYPE_VAL));65static const jlong CLASS_FILE_LOAD_HOOK_BIT = (((jlong)1) << (JVMTI_EVENT_CLASS_FILE_LOAD_HOOK - TOTAL_MIN_EVENT_TYPE_VAL));66static const jlong NATIVE_METHOD_BIND_BIT = (((jlong)1) << (JVMTI_EVENT_NATIVE_METHOD_BIND - TOTAL_MIN_EVENT_TYPE_VAL));67static const jlong VM_START_BIT = (((jlong)1) << (JVMTI_EVENT_VM_START - TOTAL_MIN_EVENT_TYPE_VAL));68static const jlong VM_INIT_BIT = (((jlong)1) << (JVMTI_EVENT_VM_INIT - TOTAL_MIN_EVENT_TYPE_VAL));69static const jlong VM_DEATH_BIT = (((jlong)1) << (JVMTI_EVENT_VM_DEATH - TOTAL_MIN_EVENT_TYPE_VAL));70static const jlong CLASS_LOAD_BIT = (((jlong)1) << (JVMTI_EVENT_CLASS_LOAD - TOTAL_MIN_EVENT_TYPE_VAL));71static const jlong CLASS_PREPARE_BIT = (((jlong)1) << (JVMTI_EVENT_CLASS_PREPARE - TOTAL_MIN_EVENT_TYPE_VAL));72static const jlong THREAD_START_BIT = (((jlong)1) << (JVMTI_EVENT_THREAD_START - TOTAL_MIN_EVENT_TYPE_VAL));73static const jlong THREAD_END_BIT = (((jlong)1) << (JVMTI_EVENT_THREAD_END - TOTAL_MIN_EVENT_TYPE_VAL));74static const jlong EXCEPTION_THROW_BIT = (((jlong)1) << (JVMTI_EVENT_EXCEPTION - TOTAL_MIN_EVENT_TYPE_VAL));75static const jlong EXCEPTION_CATCH_BIT = (((jlong)1) << (JVMTI_EVENT_EXCEPTION_CATCH - TOTAL_MIN_EVENT_TYPE_VAL));76static const jlong MONITOR_CONTENDED_ENTER_BIT = (((jlong)1) << (JVMTI_EVENT_MONITOR_CONTENDED_ENTER - TOTAL_MIN_EVENT_TYPE_VAL));77static const jlong MONITOR_CONTENDED_ENTERED_BIT = (((jlong)1) << (JVMTI_EVENT_MONITOR_CONTENDED_ENTERED - TOTAL_MIN_EVENT_TYPE_VAL));78static const jlong MONITOR_WAIT_BIT = (((jlong)1) << (JVMTI_EVENT_MONITOR_WAIT - TOTAL_MIN_EVENT_TYPE_VAL));79static const jlong MONITOR_WAITED_BIT = (((jlong)1) << (JVMTI_EVENT_MONITOR_WAITED - TOTAL_MIN_EVENT_TYPE_VAL));80static const jlong DYNAMIC_CODE_GENERATED_BIT = (((jlong)1) << (JVMTI_EVENT_DYNAMIC_CODE_GENERATED - TOTAL_MIN_EVENT_TYPE_VAL));81static const jlong DATA_DUMP_BIT = (((jlong)1) << (JVMTI_EVENT_DATA_DUMP_REQUEST - TOTAL_MIN_EVENT_TYPE_VAL));82static const jlong COMPILED_METHOD_LOAD_BIT = (((jlong)1) << (JVMTI_EVENT_COMPILED_METHOD_LOAD - TOTAL_MIN_EVENT_TYPE_VAL));83static const jlong COMPILED_METHOD_UNLOAD_BIT = (((jlong)1) << (JVMTI_EVENT_COMPILED_METHOD_UNLOAD - TOTAL_MIN_EVENT_TYPE_VAL));84static const jlong GARBAGE_COLLECTION_START_BIT = (((jlong)1) << (JVMTI_EVENT_GARBAGE_COLLECTION_START - TOTAL_MIN_EVENT_TYPE_VAL));85static const jlong GARBAGE_COLLECTION_FINISH_BIT = (((jlong)1) << (JVMTI_EVENT_GARBAGE_COLLECTION_FINISH - TOTAL_MIN_EVENT_TYPE_VAL));86static const jlong OBJECT_FREE_BIT = (((jlong)1) << (JVMTI_EVENT_OBJECT_FREE - TOTAL_MIN_EVENT_TYPE_VAL));87static const jlong RESOURCE_EXHAUSTED_BIT = (((jlong)1) << (JVMTI_EVENT_RESOURCE_EXHAUSTED - TOTAL_MIN_EVENT_TYPE_VAL));88static const jlong VM_OBJECT_ALLOC_BIT = (((jlong)1) << (JVMTI_EVENT_VM_OBJECT_ALLOC - TOTAL_MIN_EVENT_TYPE_VAL));89static const jlong SAMPLED_OBJECT_ALLOC_BIT = (((jlong)1) << (JVMTI_EVENT_SAMPLED_OBJECT_ALLOC - TOTAL_MIN_EVENT_TYPE_VAL));9091// bits for extension events92static const jlong CLASS_UNLOAD_BIT = (((jlong)1) << (EXT_EVENT_CLASS_UNLOAD - TOTAL_MIN_EVENT_TYPE_VAL));939495static const jlong MONITOR_BITS = MONITOR_CONTENDED_ENTER_BIT | MONITOR_CONTENDED_ENTERED_BIT |96MONITOR_WAIT_BIT | MONITOR_WAITED_BIT;97static const jlong EXCEPTION_BITS = EXCEPTION_THROW_BIT | EXCEPTION_CATCH_BIT;98static const jlong INTERP_EVENT_BITS = SINGLE_STEP_BIT | METHOD_ENTRY_BIT | METHOD_EXIT_BIT |99FRAME_POP_BIT | FIELD_ACCESS_BIT | FIELD_MODIFICATION_BIT;100static const jlong THREAD_FILTERED_EVENT_BITS = INTERP_EVENT_BITS | EXCEPTION_BITS | MONITOR_BITS |101BREAKPOINT_BIT | CLASS_LOAD_BIT | CLASS_PREPARE_BIT | THREAD_END_BIT |102SAMPLED_OBJECT_ALLOC_BIT;103static const jlong NEED_THREAD_LIFE_EVENTS = THREAD_FILTERED_EVENT_BITS | THREAD_START_BIT;104static const jlong EARLY_EVENT_BITS = CLASS_FILE_LOAD_HOOK_BIT | CLASS_LOAD_BIT | CLASS_PREPARE_BIT |105VM_START_BIT | VM_INIT_BIT | VM_DEATH_BIT | NATIVE_METHOD_BIND_BIT |106THREAD_START_BIT | THREAD_END_BIT |107COMPILED_METHOD_LOAD_BIT | COMPILED_METHOD_UNLOAD_BIT |108DYNAMIC_CODE_GENERATED_BIT;109static const jlong GLOBAL_EVENT_BITS = ~THREAD_FILTERED_EVENT_BITS;110static const jlong SHOULD_POST_ON_EXCEPTIONS_BITS = EXCEPTION_BITS | METHOD_EXIT_BIT | FRAME_POP_BIT;111112///////////////////////////////////////////////////////////////113//114// JvmtiEventEnabled115//116117JvmtiEventEnabled::JvmtiEventEnabled() {118clear();119}120121122void JvmtiEventEnabled::clear() {123_enabled_bits = 0;124#ifndef PRODUCT125_init_guard = JEE_INIT_GUARD;126#endif127}128129void JvmtiEventEnabled::set_enabled(jvmtiEvent event_type, bool enabled) {130jlong bits = get_bits();131jlong mask = bit_for(event_type);132if (enabled) {133bits |= mask;134} else {135bits &= ~mask;136}137set_bits(bits);138}139140141///////////////////////////////////////////////////////////////142//143// JvmtiEnvThreadEventEnable144//145146JvmtiEnvThreadEventEnable::JvmtiEnvThreadEventEnable() {147_event_user_enabled.clear();148_event_enabled.clear();149}150151152JvmtiEnvThreadEventEnable::~JvmtiEnvThreadEventEnable() {153_event_user_enabled.clear();154_event_enabled.clear();155}156157158///////////////////////////////////////////////////////////////159//160// JvmtiThreadEventEnable161//162163JvmtiThreadEventEnable::JvmtiThreadEventEnable() {164_event_enabled.clear();165}166167168JvmtiThreadEventEnable::~JvmtiThreadEventEnable() {169_event_enabled.clear();170}171172173///////////////////////////////////////////////////////////////174//175// JvmtiEnvEventEnable176//177178JvmtiEnvEventEnable::JvmtiEnvEventEnable() {179_event_user_enabled.clear();180_event_callback_enabled.clear();181_event_enabled.clear();182}183184185JvmtiEnvEventEnable::~JvmtiEnvEventEnable() {186_event_user_enabled.clear();187_event_callback_enabled.clear();188_event_enabled.clear();189}190191192///////////////////////////////////////////////////////////////193//194// EnterInterpOnlyModeClosure195//196197class EnterInterpOnlyModeClosure : public HandshakeClosure {198private:199bool _completed;200public:201EnterInterpOnlyModeClosure() : HandshakeClosure("EnterInterpOnlyMode"), _completed(false) { }202void do_thread(Thread* th) {203JavaThread* jt = th->as_Java_thread();204JvmtiThreadState* state = jt->jvmti_thread_state();205206// Set up the current stack depth for later tracking207state->invalidate_cur_stack_depth();208209state->enter_interp_only_mode();210211if (jt->has_last_Java_frame()) {212// If running in fullspeed mode, single stepping is implemented213// as follows: first, the interpreter does not dispatch to214// compiled code for threads that have single stepping enabled;215// second, we deoptimize all compiled java frames on the thread's stack when216// interpreted-only mode is enabled the first time for a given217// thread (nothing to do if no Java frames yet).218ResourceMark resMark;219for (StackFrameStream fst(jt, false /* update */, false /* process_frames */); !fst.is_done(); fst.next()) {220if (fst.current()->can_be_deoptimized()) {221Deoptimization::deoptimize(jt, *fst.current());222}223}224}225_completed = true;226}227bool completed() {228return _completed;229}230};231232233///////////////////////////////////////////////////////////////234//235// VM_ChangeSingleStep236//237238class VM_ChangeSingleStep : public VM_Operation {239private:240bool _on;241242public:243VM_ChangeSingleStep(bool on);244VMOp_Type type() const { return VMOp_ChangeSingleStep; }245bool allow_nested_vm_operations() const { return true; }246void doit(); // method definition is after definition of JvmtiEventControllerPrivate because of scoping247};248249250VM_ChangeSingleStep::VM_ChangeSingleStep(bool on)251: _on(on)252{253}254255256257258///////////////////////////////////////////////////////////////259//260// JvmtiEventControllerPrivate261//262// Private internal implementation methods for JvmtiEventController.263//264// These methods are thread safe either because they are called265// in early VM initialization which is single threaded, or they266// hold the JvmtiThreadState_lock.267//268269class JvmtiEventControllerPrivate : public AllStatic {270static bool _initialized;271public:272static void set_should_post_single_step(bool on);273static void enter_interp_only_mode(JvmtiThreadState *state);274static void leave_interp_only_mode(JvmtiThreadState *state);275static void recompute_enabled();276static jlong recompute_env_enabled(JvmtiEnvBase* env);277static jlong recompute_env_thread_enabled(JvmtiEnvThreadState* ets, JvmtiThreadState* state);278static jlong recompute_thread_enabled(JvmtiThreadState *state);279static void event_init();280281static void set_user_enabled(JvmtiEnvBase *env, JavaThread *thread,282jvmtiEvent event_type, bool enabled);283static void set_event_callbacks(JvmtiEnvBase *env,284const jvmtiEventCallbacks* callbacks,285jint size_of_callbacks);286287static void set_extension_event_callback(JvmtiEnvBase *env,288jint extension_event_index,289jvmtiExtensionEvent callback);290291static void set_frame_pop(JvmtiEnvThreadState *env_thread, JvmtiFramePop fpop);292static void clear_frame_pop(JvmtiEnvThreadState *env_thread, JvmtiFramePop fpop);293static void clear_to_frame_pop(JvmtiEnvThreadState *env_thread, JvmtiFramePop fpop);294static void change_field_watch(jvmtiEvent event_type, bool added);295296static void thread_started(JavaThread *thread);297static void thread_ended(JavaThread *thread);298299static void env_initialize(JvmtiEnvBase *env);300static void env_dispose(JvmtiEnvBase *env);301302static void vm_start();303static void vm_init();304static void vm_death();305306static void trace_changed(JvmtiThreadState *state, jlong now_enabled, jlong changed);307static void trace_changed(jlong now_enabled, jlong changed);308309static void flush_object_free_events(JvmtiEnvBase *env);310static void set_enabled_events_with_lock(JvmtiEnvBase *env, jlong now_enabled);311};312313bool JvmtiEventControllerPrivate::_initialized = false;314315void JvmtiEventControllerPrivate::set_should_post_single_step(bool on) {316// we have permission to do this, VM op doesn't317JvmtiExport::set_should_post_single_step(on);318}319320321// When _on == true, we use the safepoint interpreter dispatch table322// to allow us to find the single step points. Otherwise, we switch323// back to the regular interpreter dispatch table.324// Note: We call Interpreter::notice_safepoints() and ignore_safepoints()325// in a VM_Operation to safely make the dispatch table switch. We326// no longer rely on the safepoint mechanism to do any of this work327// for us.328void VM_ChangeSingleStep::doit() {329log_debug(interpreter, safepoint)("changing single step to '%s'", _on ? "on" : "off");330JvmtiEventControllerPrivate::set_should_post_single_step(_on);331if (_on) {332Interpreter::notice_safepoints();333} else {334Interpreter::ignore_safepoints();335}336}337338339void JvmtiEventControllerPrivate::enter_interp_only_mode(JvmtiThreadState *state) {340EC_TRACE(("[%s] # Entering interpreter only mode",341JvmtiTrace::safe_get_thread_name(state->get_thread())));342EnterInterpOnlyModeClosure hs;343JavaThread *target = state->get_thread();344Thread *current = Thread::current();345if (target->is_handshake_safe_for(current)) {346hs.do_thread(target);347} else {348Handshake::execute(&hs, target);349guarantee(hs.completed(), "Handshake failed: Target thread is not alive?");350}351}352353354void355JvmtiEventControllerPrivate::leave_interp_only_mode(JvmtiThreadState *state) {356EC_TRACE(("[%s] # Leaving interpreter only mode",357JvmtiTrace::safe_get_thread_name(state->get_thread())));358state->leave_interp_only_mode();359}360361362void363JvmtiEventControllerPrivate::trace_changed(JvmtiThreadState *state, jlong now_enabled, jlong changed) {364#ifdef JVMTI_TRACE365if (JvmtiTrace::trace_event_controller()) {366SafeResourceMark rm;367// traces standard events only368for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {369jlong bit = JvmtiEventEnabled::bit_for((jvmtiEvent)ei);370if (changed & bit) {371// it changed, print it372log_trace(jvmti)("[%s] # %s event %s",373JvmtiTrace::safe_get_thread_name(state->get_thread()),374(now_enabled & bit)? "Enabling" : "Disabling", JvmtiTrace::event_name((jvmtiEvent)ei));375}376}377}378#endif /*JVMTI_TRACE */379}380381382void383JvmtiEventControllerPrivate::trace_changed(jlong now_enabled, jlong changed) {384#ifdef JVMTI_TRACE385if (JvmtiTrace::trace_event_controller()) {386SafeResourceMark rm;387// traces standard events only388for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {389jlong bit = JvmtiEventEnabled::bit_for((jvmtiEvent)ei);390if (changed & bit) {391// it changed, print it392log_trace(jvmti)("[-] # %s event %s",393(now_enabled & bit)? "Enabling" : "Disabling", JvmtiTrace::event_name((jvmtiEvent)ei));394}395}396}397#endif /*JVMTI_TRACE */398}399400401void402JvmtiEventControllerPrivate::flush_object_free_events(JvmtiEnvBase* env) {403// Some of the objects recorded by this env may have died. If we're404// (potentially) changing the enable state for ObjectFree events, we405// need to ensure the env is cleaned up and any events that should406// be posted are posted.407JvmtiTagMap* tag_map = env->tag_map_acquire();408if (tag_map != NULL) {409tag_map->flush_object_free_events();410}411}412413void414JvmtiEventControllerPrivate::set_enabled_events_with_lock(JvmtiEnvBase* env, jlong now_enabled) {415// The state for ObjectFree events must be enabled or disabled416// under the TagMap lock, to allow pending object posting events to complete.417JvmtiTagMap* tag_map = env->tag_map_acquire();418if (tag_map != NULL) {419MutexLocker ml(tag_map->lock(), Mutex::_no_safepoint_check_flag);420env->env_event_enable()->_event_enabled.set_bits(now_enabled);421} else {422env->env_event_enable()->_event_enabled.set_bits(now_enabled);423}424}425426// For the specified env: compute the currently truly enabled events427// set external state accordingly.428// Return value and set value must include all events.429// But outside this class, only non-thread-filtered events can be queried..430jlong431JvmtiEventControllerPrivate::recompute_env_enabled(JvmtiEnvBase* env) {432jlong was_enabled = env->env_event_enable()->_event_enabled.get_bits();433jlong now_enabled =434env->env_event_enable()->_event_callback_enabled.get_bits() &435env->env_event_enable()->_event_user_enabled.get_bits();436437switch (env->phase()) {438case JVMTI_PHASE_PRIMORDIAL:439case JVMTI_PHASE_ONLOAD:440// only these events allowed in primordial or onload phase441now_enabled &= (EARLY_EVENT_BITS & ~THREAD_FILTERED_EVENT_BITS);442break;443case JVMTI_PHASE_START:444// only these events allowed in start phase445now_enabled &= EARLY_EVENT_BITS;446break;447case JVMTI_PHASE_LIVE:448// all events allowed during live phase449break;450case JVMTI_PHASE_DEAD:451// no events allowed when dead452now_enabled = 0;453break;454default:455assert(false, "no other phases - sanity check");456break;457}458459// Set/reset the event enabled under the tagmap lock.460set_enabled_events_with_lock(env, now_enabled);461462trace_changed(now_enabled, (now_enabled ^ was_enabled) & ~THREAD_FILTERED_EVENT_BITS);463464return now_enabled;465}466467468// For the specified env and thread: compute the currently truly enabled events469// set external state accordingly. Only thread-filtered events are included.470jlong471JvmtiEventControllerPrivate::recompute_env_thread_enabled(JvmtiEnvThreadState* ets, JvmtiThreadState* state) {472JvmtiEnv *env = ets->get_env();473474jlong was_enabled = ets->event_enable()->_event_enabled.get_bits();475jlong now_enabled = THREAD_FILTERED_EVENT_BITS &476env->env_event_enable()->_event_callback_enabled.get_bits() &477(env->env_event_enable()->_event_user_enabled.get_bits() |478ets->event_enable()->_event_user_enabled.get_bits());479480// for frame pops and field watchs, computed enabled state481// is only true if an event has been requested482if (!ets->has_frame_pops()) {483now_enabled &= ~FRAME_POP_BIT;484}485if (*((int *)JvmtiExport::get_field_access_count_addr()) == 0) {486now_enabled &= ~FIELD_ACCESS_BIT;487}488if (*((int *)JvmtiExport::get_field_modification_count_addr()) == 0) {489now_enabled &= ~FIELD_MODIFICATION_BIT;490}491492switch (JvmtiEnv::get_phase()) {493case JVMTI_PHASE_DEAD:494// no events allowed when dead495now_enabled = 0;496break;497default:498break;499}500501// if anything changed do update502if (now_enabled != was_enabled) {503504// will we really send these events to this thread x env505ets->event_enable()->_event_enabled.set_bits(now_enabled);506507// If the enabled status of the single step or breakpoint events changed,508// the location status may need to change as well.509jlong changed = now_enabled ^ was_enabled;510if (changed & SINGLE_STEP_BIT) {511ets->reset_current_location(JVMTI_EVENT_SINGLE_STEP, (now_enabled & SINGLE_STEP_BIT) != 0);512}513if (changed & BREAKPOINT_BIT) {514ets->reset_current_location(JVMTI_EVENT_BREAKPOINT, (now_enabled & BREAKPOINT_BIT) != 0);515}516trace_changed(state, now_enabled, changed);517}518return now_enabled;519}520521522// For the specified thread: compute the currently truly enabled events523// set external state accordingly. Only thread-filtered events are included.524jlong525JvmtiEventControllerPrivate::recompute_thread_enabled(JvmtiThreadState *state) {526if (state == NULL) {527// associated JavaThread is exiting528return (jlong)0;529}530531julong was_any_env_enabled = state->thread_event_enable()->_event_enabled.get_bits();532julong any_env_enabled = 0;533// JVMTI_EVENT_FRAME_POP can be disabled (in the case FRAME_POP_BIT is not set),534// but we need to set interp_only if some JvmtiEnvThreadState has frame pop set535// to clear the request536bool has_frame_pops = false;537538{539// This iteration will include JvmtiEnvThreadStates whose environments540// have been disposed. These JvmtiEnvThreadStates must not be filtered541// as recompute must be called on them to disable their events,542JvmtiEnvThreadStateIterator it(state);543for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {544any_env_enabled |= recompute_env_thread_enabled(ets, state);545has_frame_pops |= ets->has_frame_pops();546}547}548549if (any_env_enabled != was_any_env_enabled) {550// mark if event is truly enabled on this thread in any environment551state->thread_event_enable()->_event_enabled.set_bits(any_env_enabled);552553// update the JavaThread cached value for thread-specific should_post_on_exceptions value554bool should_post_on_exceptions = (any_env_enabled & SHOULD_POST_ON_EXCEPTIONS_BITS) != 0;555state->set_should_post_on_exceptions(should_post_on_exceptions);556}557558// compute interp_only mode559bool should_be_interp = (any_env_enabled & INTERP_EVENT_BITS) != 0 || has_frame_pops;560bool is_now_interp = state->is_interp_only_mode();561562if (should_be_interp != is_now_interp) {563if (should_be_interp) {564enter_interp_only_mode(state);565} else {566leave_interp_only_mode(state);567}568}569570return any_env_enabled;571}572573574// Compute truly enabled events - meaning if the event can and could be575// sent. An event is truly enabled if it is user enabled on the thread576// or globally user enabled, but only if there is a callback or event hook577// for it and, for field watch and frame pop, one has been set.578// Compute if truly enabled, per thread, per environment, per combination579// (thread x environment), and overall. These merges are true if any is true.580// True per thread if some environment has callback set and the event is globally581// enabled or enabled for this thread.582// True per environment if the callback is set and the event is globally583// enabled in this environment or enabled for any thread in this environment.584// True per combination if the environment has the callback set and the585// event is globally enabled in this environment or the event is enabled586// for this thread and environment.587//588// All states transitions dependent on these transitions are also handled here.589void590JvmtiEventControllerPrivate::recompute_enabled() {591assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");592593// event enabled for any thread in any environment594julong was_any_env_thread_enabled = JvmtiEventController::_universal_global_event_enabled.get_bits();595julong any_env_thread_enabled = 0;596597EC_TRACE(("[-] # recompute enabled - before " JULONG_FORMAT_X, was_any_env_thread_enabled));598599// compute non-thread-filters events.600// This must be done separately from thread-filtered events, since some601// events can occur before any threads exist.602JvmtiEnvIterator it;603for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) {604any_env_thread_enabled |= recompute_env_enabled(env);605}606607// We need to create any missing jvmti_thread_state if there are globally set thread608// filtered events and there weren't last time609if ( (any_env_thread_enabled & THREAD_FILTERED_EVENT_BITS) != 0 &&610(was_any_env_thread_enabled & THREAD_FILTERED_EVENT_BITS) == 0) {611for (JavaThreadIteratorWithHandle jtiwh; JavaThread *tp = jtiwh.next(); ) {612// state_for_while_locked() makes tp->is_exiting() check613JvmtiThreadState::state_for_while_locked(tp); // create the thread state if missing614}615}616617// compute and set thread-filtered events618for (JvmtiThreadState *state = JvmtiThreadState::first(); state != NULL; state = state->next()) {619any_env_thread_enabled |= recompute_thread_enabled(state);620}621622// set universal state (across all envs and threads)623jlong delta = any_env_thread_enabled ^ was_any_env_thread_enabled;624if (delta != 0) {625JvmtiExport::set_should_post_field_access((any_env_thread_enabled & FIELD_ACCESS_BIT) != 0);626JvmtiExport::set_should_post_field_modification((any_env_thread_enabled & FIELD_MODIFICATION_BIT) != 0);627JvmtiExport::set_should_post_class_load((any_env_thread_enabled & CLASS_LOAD_BIT) != 0);628JvmtiExport::set_should_post_class_file_load_hook((any_env_thread_enabled & CLASS_FILE_LOAD_HOOK_BIT) != 0);629JvmtiExport::set_should_post_native_method_bind((any_env_thread_enabled & NATIVE_METHOD_BIND_BIT) != 0);630JvmtiExport::set_should_post_dynamic_code_generated((any_env_thread_enabled & DYNAMIC_CODE_GENERATED_BIT) != 0);631JvmtiExport::set_should_post_data_dump((any_env_thread_enabled & DATA_DUMP_BIT) != 0);632JvmtiExport::set_should_post_class_prepare((any_env_thread_enabled & CLASS_PREPARE_BIT) != 0);633JvmtiExport::set_should_post_class_unload((any_env_thread_enabled & CLASS_UNLOAD_BIT) != 0);634JvmtiExport::set_should_post_monitor_contended_enter((any_env_thread_enabled & MONITOR_CONTENDED_ENTER_BIT) != 0);635JvmtiExport::set_should_post_monitor_contended_entered((any_env_thread_enabled & MONITOR_CONTENDED_ENTERED_BIT) != 0);636JvmtiExport::set_should_post_monitor_wait((any_env_thread_enabled & MONITOR_WAIT_BIT) != 0);637JvmtiExport::set_should_post_monitor_waited((any_env_thread_enabled & MONITOR_WAITED_BIT) != 0);638JvmtiExport::set_should_post_garbage_collection_start((any_env_thread_enabled & GARBAGE_COLLECTION_START_BIT) != 0);639JvmtiExport::set_should_post_garbage_collection_finish((any_env_thread_enabled & GARBAGE_COLLECTION_FINISH_BIT) != 0);640JvmtiExport::set_should_post_object_free((any_env_thread_enabled & OBJECT_FREE_BIT) != 0);641JvmtiExport::set_should_post_resource_exhausted((any_env_thread_enabled & RESOURCE_EXHAUSTED_BIT) != 0);642JvmtiExport::set_should_post_compiled_method_load((any_env_thread_enabled & COMPILED_METHOD_LOAD_BIT) != 0);643JvmtiExport::set_should_post_compiled_method_unload((any_env_thread_enabled & COMPILED_METHOD_UNLOAD_BIT) != 0);644JvmtiExport::set_should_post_vm_object_alloc((any_env_thread_enabled & VM_OBJECT_ALLOC_BIT) != 0);645JvmtiExport::set_should_post_sampled_object_alloc((any_env_thread_enabled & SAMPLED_OBJECT_ALLOC_BIT) != 0);646647// need this if we want thread events or we need them to init data648JvmtiExport::set_should_post_thread_life((any_env_thread_enabled & NEED_THREAD_LIFE_EVENTS) != 0);649650// If single stepping is turned on or off, execute the VM op to change it.651if (delta & SINGLE_STEP_BIT) {652switch (JvmtiEnv::get_phase()) {653case JVMTI_PHASE_DEAD:654// If the VM is dying we can't execute VM ops655break;656case JVMTI_PHASE_LIVE: {657VM_ChangeSingleStep op((any_env_thread_enabled & SINGLE_STEP_BIT) != 0);658VMThread::execute(&op);659break;660}661default:662assert(false, "should never come here before live phase");663break;664}665}666667// set global truly enabled, that is, any thread in any environment668JvmtiEventController::_universal_global_event_enabled.set_bits(any_env_thread_enabled);669670// set global should_post_on_exceptions671JvmtiExport::set_should_post_on_exceptions((any_env_thread_enabled & SHOULD_POST_ON_EXCEPTIONS_BITS) != 0);672673}674675EC_TRACE(("[-] # recompute enabled - after " JULONG_FORMAT_X, any_env_thread_enabled));676}677678679void680JvmtiEventControllerPrivate::thread_started(JavaThread *thread) {681assert(thread == Thread::current(), "must be current thread");682assert(JvmtiEnvBase::environments_might_exist(), "to enter event controller, JVM TI environments must exist");683684EC_TRACE(("[%s] # thread started", JvmtiTrace::safe_get_thread_name(thread)));685686// if we have any thread filtered events globally enabled, create/update the thread state687if ((JvmtiEventController::_universal_global_event_enabled.get_bits() & THREAD_FILTERED_EVENT_BITS) != 0) {688MutexLocker mu(JvmtiThreadState_lock);689// create the thread state if missing690JvmtiThreadState *state = JvmtiThreadState::state_for_while_locked(thread);691if (state != NULL) { // skip threads with no JVMTI thread state692recompute_thread_enabled(state);693}694}695}696697698void699JvmtiEventControllerPrivate::thread_ended(JavaThread *thread) {700// Removes the JvmtiThreadState associated with the specified thread.701// May be called after all environments have been disposed.702assert(JvmtiThreadState_lock->is_locked(), "sanity check");703704EC_TRACE(("[%s] # thread ended", JvmtiTrace::safe_get_thread_name(thread)));705706JvmtiThreadState *state = thread->jvmti_thread_state();707assert(state != NULL, "else why are we here?");708delete state;709}710711void JvmtiEventControllerPrivate::set_event_callbacks(JvmtiEnvBase *env,712const jvmtiEventCallbacks* callbacks,713jint size_of_callbacks) {714assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");715EC_TRACE(("[*] # set event callbacks"));716717// May be changing the event handler for ObjectFree.718flush_object_free_events(env);719720env->set_event_callbacks(callbacks, size_of_callbacks);721jlong enabled_bits = 0;722for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {723jvmtiEvent evt_t = (jvmtiEvent)ei;724if (env->has_callback(evt_t)) {725enabled_bits |= JvmtiEventEnabled::bit_for(evt_t);726}727}728env->env_event_enable()->_event_callback_enabled.set_bits(enabled_bits);729recompute_enabled();730}731732void733JvmtiEventControllerPrivate::set_extension_event_callback(JvmtiEnvBase *env,734jint extension_event_index,735jvmtiExtensionEvent callback)736{737assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");738EC_TRACE(("[*] # set extension event callback"));739740// extension events are allocated below JVMTI_MIN_EVENT_TYPE_VAL741assert(extension_event_index >= (jint)EXT_MIN_EVENT_TYPE_VAL &&742extension_event_index <= (jint)EXT_MAX_EVENT_TYPE_VAL, "sanity check");743744745// As the bits for both standard (jvmtiEvent) and extension746// (jvmtiExtEvents) are stored in the same word we cast here to747// jvmtiEvent to set/clear the bit for this extension event.748jvmtiEvent event_type = (jvmtiEvent)extension_event_index;749750// Prevent a possible race condition where events are re-enabled by a call to751// set event callbacks, where the DisposeEnvironment occurs after the boiler-plate752// environment check and before the lock is acquired.753// We can safely do the is_valid check now, as JvmtiThreadState_lock is held.754bool enabling = (callback != NULL) && (env->is_valid());755env->env_event_enable()->set_user_enabled(event_type, enabling);756757// update the callback758jvmtiExtEventCallbacks* ext_callbacks = env->ext_callbacks();759switch (extension_event_index) {760case EXT_EVENT_CLASS_UNLOAD :761ext_callbacks->ClassUnload = callback;762break;763default:764ShouldNotReachHere();765}766767// update the callback enable/disable bit768jlong enabled_bits = env->env_event_enable()->_event_callback_enabled.get_bits();769jlong bit_for = JvmtiEventEnabled::bit_for(event_type);770if (enabling) {771enabled_bits |= bit_for;772} else {773enabled_bits &= ~bit_for;774}775env->env_event_enable()->_event_callback_enabled.set_bits(enabled_bits);776777recompute_enabled();778}779780781void782JvmtiEventControllerPrivate::env_initialize(JvmtiEnvBase *env) {783assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");784EC_TRACE(("[*] # env initialize"));785786if (JvmtiEnvBase::is_vm_live()) {787// if we didn't initialize event info already (this is a late788// launched environment), do it now.789event_init();790}791792env->initialize();793794// add the JvmtiEnvThreadState to each JvmtiThreadState795for (JvmtiThreadState *state = JvmtiThreadState::first(); state != NULL; state = state->next()) {796state->add_env(env);797assert((JvmtiEnv*)(state->env_thread_state(env)->get_env()) == env, "sanity check");798}799JvmtiEventControllerPrivate::recompute_enabled();800}801802803void804JvmtiEventControllerPrivate::env_dispose(JvmtiEnvBase *env) {805assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");806EC_TRACE(("[*] # env dispose"));807808// Before the environment is marked disposed, disable all events on this809// environment (by zapping the callbacks). As a result, the disposed810// environment will not call event handlers.811set_event_callbacks(env, NULL, 0);812for (jint extension_event_index = EXT_MIN_EVENT_TYPE_VAL;813extension_event_index <= EXT_MAX_EVENT_TYPE_VAL;814++extension_event_index) {815set_extension_event_callback(env, extension_event_index, NULL);816}817818// Let the environment finish disposing itself.819env->env_dispose();820}821822823void824JvmtiEventControllerPrivate::set_user_enabled(JvmtiEnvBase *env, JavaThread *thread,825jvmtiEvent event_type, bool enabled) {826assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");827828EC_TRACE(("[%s] # user %s event %s",829thread==NULL? "ALL": JvmtiTrace::safe_get_thread_name(thread),830enabled? "enabled" : "disabled", JvmtiTrace::event_name(event_type)));831832if (event_type == JVMTI_EVENT_OBJECT_FREE) {833flush_object_free_events(env);834}835836if (thread == NULL) {837env->env_event_enable()->set_user_enabled(event_type, enabled);838} else {839// create the thread state (if it didn't exist before)840JvmtiThreadState *state = JvmtiThreadState::state_for_while_locked(thread);841if (state != NULL) {842state->env_thread_state(env)->event_enable()->set_user_enabled(event_type, enabled);843}844}845recompute_enabled();846}847848849void850JvmtiEventControllerPrivate::set_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {851EC_TRACE(("[%s] # set frame pop - frame=%d",852JvmtiTrace::safe_get_thread_name(ets->get_thread()),853fpop.frame_number() ));854855ets->get_frame_pops()->set(fpop);856recompute_thread_enabled(ets->get_thread()->jvmti_thread_state());857}858859860void861JvmtiEventControllerPrivate::clear_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {862EC_TRACE(("[%s] # clear frame pop - frame=%d",863JvmtiTrace::safe_get_thread_name(ets->get_thread()),864fpop.frame_number() ));865866ets->get_frame_pops()->clear(fpop);867recompute_thread_enabled(ets->get_thread()->jvmti_thread_state());868}869870871void872JvmtiEventControllerPrivate::clear_to_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {873int cleared_cnt = ets->get_frame_pops()->clear_to(fpop);874875EC_TRACE(("[%s] # clear to frame pop - frame=%d, count=%d",876JvmtiTrace::safe_get_thread_name(ets->get_thread()),877fpop.frame_number(),878cleared_cnt ));879880if (cleared_cnt > 0) {881recompute_thread_enabled(ets->get_thread()->jvmti_thread_state());882}883}884885void886JvmtiEventControllerPrivate::change_field_watch(jvmtiEvent event_type, bool added) {887int *count_addr;888889switch (event_type) {890case JVMTI_EVENT_FIELD_MODIFICATION:891count_addr = (int *)JvmtiExport::get_field_modification_count_addr();892break;893case JVMTI_EVENT_FIELD_ACCESS:894count_addr = (int *)JvmtiExport::get_field_access_count_addr();895break;896default:897assert(false, "incorrect event");898return;899}900901EC_TRACE(("[-] # change field watch - %s %s count=%d",902event_type==JVMTI_EVENT_FIELD_MODIFICATION? "modification" : "access",903added? "add" : "remove",904*count_addr));905906if (added) {907(*count_addr)++;908if (*count_addr == 1) {909recompute_enabled();910}911} else {912if (*count_addr > 0) {913(*count_addr)--;914if (*count_addr == 0) {915recompute_enabled();916}917} else {918assert(false, "field watch out of phase");919}920}921}922923void924JvmtiEventControllerPrivate::event_init() {925assert(JvmtiThreadState_lock->is_locked(), "sanity check");926927if (_initialized) {928return;929}930931EC_TRACE(("[-] # VM live"));932933#ifdef ASSERT934// check that our idea and the spec's idea of threaded events match935for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {936jlong bit = JvmtiEventEnabled::bit_for((jvmtiEvent)ei);937assert(((THREAD_FILTERED_EVENT_BITS & bit) != 0) == JvmtiUtil::event_threaded(ei),938"thread filtered event list does not match");939}940#endif941942_initialized = true;943}944945void946JvmtiEventControllerPrivate::vm_start() {947// some events are now able to be enabled (phase has changed)948JvmtiEventControllerPrivate::recompute_enabled();949}950951952void953JvmtiEventControllerPrivate::vm_init() {954event_init();955956// all the events are now able to be enabled (phase has changed)957JvmtiEventControllerPrivate::recompute_enabled();958}959960961void962JvmtiEventControllerPrivate::vm_death() {963// events are disabled (phase has changed)964JvmtiEventControllerPrivate::recompute_enabled();965}966967968///////////////////////////////////////////////////////////////969//970// JvmtiEventController971//972973JvmtiEventEnabled JvmtiEventController::_universal_global_event_enabled;974975bool976JvmtiEventController::is_global_event(jvmtiEvent event_type) {977assert(is_valid_event_type(event_type), "invalid event type");978jlong bit_for = ((jlong)1) << (event_type - TOTAL_MIN_EVENT_TYPE_VAL);979return((bit_for & GLOBAL_EVENT_BITS)!=0);980}981982void983JvmtiEventController::set_user_enabled(JvmtiEnvBase *env, JavaThread *thread, jvmtiEvent event_type, bool enabled) {984if (Threads::number_of_threads() == 0) {985// during early VM start-up locks don't exist, but we are safely single threaded,986// call the functionality without holding the JvmtiThreadState_lock.987JvmtiEventControllerPrivate::set_user_enabled(env, thread, event_type, enabled);988} else {989MutexLocker mu(JvmtiThreadState_lock);990JvmtiEventControllerPrivate::set_user_enabled(env, thread, event_type, enabled);991}992}993994995void996JvmtiEventController::set_event_callbacks(JvmtiEnvBase *env,997const jvmtiEventCallbacks* callbacks,998jint size_of_callbacks) {999if (Threads::number_of_threads() == 0) {1000// during early VM start-up locks don't exist, but we are safely single threaded,1001// call the functionality without holding the JvmtiThreadState_lock.1002JvmtiEventControllerPrivate::set_event_callbacks(env, callbacks, size_of_callbacks);1003} else {1004MutexLocker mu(JvmtiThreadState_lock);1005JvmtiEventControllerPrivate::set_event_callbacks(env, callbacks, size_of_callbacks);1006}1007}10081009void1010JvmtiEventController::set_extension_event_callback(JvmtiEnvBase *env,1011jint extension_event_index,1012jvmtiExtensionEvent callback) {1013if (Threads::number_of_threads() == 0) {1014JvmtiEventControllerPrivate::set_extension_event_callback(env, extension_event_index, callback);1015} else {1016MutexLocker mu(JvmtiThreadState_lock);1017JvmtiEventControllerPrivate::set_extension_event_callback(env, extension_event_index, callback);1018}1019}10201021void1022JvmtiEventController::set_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {1023assert(JvmtiThreadState_lock->is_locked(), "Must be locked.");1024JvmtiEventControllerPrivate::set_frame_pop(ets, fpop);1025}10261027void1028JvmtiEventController::clear_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {1029assert(JvmtiThreadState_lock->is_locked(), "Must be locked.");1030JvmtiEventControllerPrivate::clear_frame_pop(ets, fpop);1031}10321033void1034JvmtiEventController::change_field_watch(jvmtiEvent event_type, bool added) {1035MutexLocker mu(JvmtiThreadState_lock);1036JvmtiEventControllerPrivate::change_field_watch(event_type, added);1037}10381039void1040JvmtiEventController::thread_started(JavaThread *thread) {1041// operates only on the current thread1042// JvmtiThreadState_lock grabbed only if needed.1043JvmtiEventControllerPrivate::thread_started(thread);1044}10451046void1047JvmtiEventController::thread_ended(JavaThread *thread) {1048// operates only on the current thread1049// JvmtiThreadState_lock grabbed only if needed.1050JvmtiEventControllerPrivate::thread_ended(thread);1051}10521053void1054JvmtiEventController::env_initialize(JvmtiEnvBase *env) {1055if (Threads::number_of_threads() == 0) {1056// during early VM start-up locks don't exist, but we are safely single threaded,1057// call the functionality without holding the JvmtiThreadState_lock.1058JvmtiEventControllerPrivate::env_initialize(env);1059} else {1060MutexLocker mu(JvmtiThreadState_lock);1061JvmtiEventControllerPrivate::env_initialize(env);1062}1063}10641065void1066JvmtiEventController::env_dispose(JvmtiEnvBase *env) {1067if (Threads::number_of_threads() == 0) {1068// during early VM start-up locks don't exist, but we are safely single threaded,1069// call the functionality without holding the JvmtiThreadState_lock.1070JvmtiEventControllerPrivate::env_dispose(env);1071} else {1072MutexLocker mu(JvmtiThreadState_lock);1073JvmtiEventControllerPrivate::env_dispose(env);1074}1075}107610771078void1079JvmtiEventController::vm_start() {1080if (JvmtiEnvBase::environments_might_exist()) {1081MutexLocker mu(JvmtiThreadState_lock);1082JvmtiEventControllerPrivate::vm_start();1083}1084}10851086void1087JvmtiEventController::vm_init() {1088if (JvmtiEnvBase::environments_might_exist()) {1089MutexLocker mu(JvmtiThreadState_lock);1090JvmtiEventControllerPrivate::vm_init();1091}1092}10931094void1095JvmtiEventController::vm_death() {1096if (JvmtiEnvBase::environments_might_exist()) {1097MutexLocker mu(JvmtiThreadState_lock);1098JvmtiEventControllerPrivate::vm_death();1099}1100}110111021103