Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/prims/jvmtiEventController.cpp
32285 views
/*1* Copyright (c) 2003, 2014, 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 "memory/resourceArea.hpp"28#include "prims/jvmtiEventController.hpp"29#include "prims/jvmtiEventController.inline.hpp"30#include "prims/jvmtiExport.hpp"31#include "prims/jvmtiImpl.hpp"32#include "prims/jvmtiThreadState.inline.hpp"33#include "runtime/frame.hpp"34#include "runtime/thread.hpp"35#include "runtime/vframe.hpp"36#include "runtime/vframe_hp.hpp"37#include "runtime/vmThread.hpp"38#include "runtime/vm_operations.hpp"3940PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC4142#ifdef JVMTI_TRACE43#define EC_TRACE(out) do { \44if (JvmtiTrace::trace_event_controller()) { \45SafeResourceMark rm; \46tty->print_cr out; \47} \48} while (0)49#else50#define EC_TRACE(out)51#endif /*JVMTI_TRACE */5253// bits for standard events5455static const jlong SINGLE_STEP_BIT = (((jlong)1) << (JVMTI_EVENT_SINGLE_STEP - TOTAL_MIN_EVENT_TYPE_VAL));56static const jlong FRAME_POP_BIT = (((jlong)1) << (JVMTI_EVENT_FRAME_POP - TOTAL_MIN_EVENT_TYPE_VAL));57static const jlong BREAKPOINT_BIT = (((jlong)1) << (JVMTI_EVENT_BREAKPOINT - TOTAL_MIN_EVENT_TYPE_VAL));58static const jlong FIELD_ACCESS_BIT = (((jlong)1) << (JVMTI_EVENT_FIELD_ACCESS - TOTAL_MIN_EVENT_TYPE_VAL));59static const jlong FIELD_MODIFICATION_BIT = (((jlong)1) << (JVMTI_EVENT_FIELD_MODIFICATION - TOTAL_MIN_EVENT_TYPE_VAL));60static const jlong METHOD_ENTRY_BIT = (((jlong)1) << (JVMTI_EVENT_METHOD_ENTRY - TOTAL_MIN_EVENT_TYPE_VAL));61static const jlong METHOD_EXIT_BIT = (((jlong)1) << (JVMTI_EVENT_METHOD_EXIT - TOTAL_MIN_EVENT_TYPE_VAL));62static const jlong CLASS_FILE_LOAD_HOOK_BIT = (((jlong)1) << (JVMTI_EVENT_CLASS_FILE_LOAD_HOOK - TOTAL_MIN_EVENT_TYPE_VAL));63static const jlong NATIVE_METHOD_BIND_BIT = (((jlong)1) << (JVMTI_EVENT_NATIVE_METHOD_BIND - TOTAL_MIN_EVENT_TYPE_VAL));64static const jlong VM_START_BIT = (((jlong)1) << (JVMTI_EVENT_VM_START - TOTAL_MIN_EVENT_TYPE_VAL));65static const jlong VM_INIT_BIT = (((jlong)1) << (JVMTI_EVENT_VM_INIT - TOTAL_MIN_EVENT_TYPE_VAL));66static const jlong VM_DEATH_BIT = (((jlong)1) << (JVMTI_EVENT_VM_DEATH - TOTAL_MIN_EVENT_TYPE_VAL));67static const jlong CLASS_LOAD_BIT = (((jlong)1) << (JVMTI_EVENT_CLASS_LOAD - TOTAL_MIN_EVENT_TYPE_VAL));68static const jlong CLASS_PREPARE_BIT = (((jlong)1) << (JVMTI_EVENT_CLASS_PREPARE - TOTAL_MIN_EVENT_TYPE_VAL));69static const jlong THREAD_START_BIT = (((jlong)1) << (JVMTI_EVENT_THREAD_START - TOTAL_MIN_EVENT_TYPE_VAL));70static const jlong THREAD_END_BIT = (((jlong)1) << (JVMTI_EVENT_THREAD_END - TOTAL_MIN_EVENT_TYPE_VAL));71static const jlong EXCEPTION_THROW_BIT = (((jlong)1) << (JVMTI_EVENT_EXCEPTION - TOTAL_MIN_EVENT_TYPE_VAL));72static const jlong EXCEPTION_CATCH_BIT = (((jlong)1) << (JVMTI_EVENT_EXCEPTION_CATCH - TOTAL_MIN_EVENT_TYPE_VAL));73static const jlong MONITOR_CONTENDED_ENTER_BIT = (((jlong)1) << (JVMTI_EVENT_MONITOR_CONTENDED_ENTER - TOTAL_MIN_EVENT_TYPE_VAL));74static const jlong MONITOR_CONTENDED_ENTERED_BIT = (((jlong)1) << (JVMTI_EVENT_MONITOR_CONTENDED_ENTERED - TOTAL_MIN_EVENT_TYPE_VAL));75static const jlong MONITOR_WAIT_BIT = (((jlong)1) << (JVMTI_EVENT_MONITOR_WAIT - TOTAL_MIN_EVENT_TYPE_VAL));76static const jlong MONITOR_WAITED_BIT = (((jlong)1) << (JVMTI_EVENT_MONITOR_WAITED - TOTAL_MIN_EVENT_TYPE_VAL));77static const jlong DYNAMIC_CODE_GENERATED_BIT = (((jlong)1) << (JVMTI_EVENT_DYNAMIC_CODE_GENERATED - TOTAL_MIN_EVENT_TYPE_VAL));78static const jlong DATA_DUMP_BIT = (((jlong)1) << (JVMTI_EVENT_DATA_DUMP_REQUEST - TOTAL_MIN_EVENT_TYPE_VAL));79static const jlong COMPILED_METHOD_LOAD_BIT = (((jlong)1) << (JVMTI_EVENT_COMPILED_METHOD_LOAD - TOTAL_MIN_EVENT_TYPE_VAL));80static const jlong COMPILED_METHOD_UNLOAD_BIT = (((jlong)1) << (JVMTI_EVENT_COMPILED_METHOD_UNLOAD - TOTAL_MIN_EVENT_TYPE_VAL));81static const jlong GARBAGE_COLLECTION_START_BIT = (((jlong)1) << (JVMTI_EVENT_GARBAGE_COLLECTION_START - TOTAL_MIN_EVENT_TYPE_VAL));82static const jlong GARBAGE_COLLECTION_FINISH_BIT = (((jlong)1) << (JVMTI_EVENT_GARBAGE_COLLECTION_FINISH - TOTAL_MIN_EVENT_TYPE_VAL));83static const jlong OBJECT_FREE_BIT = (((jlong)1) << (JVMTI_EVENT_OBJECT_FREE - TOTAL_MIN_EVENT_TYPE_VAL));84static const jlong RESOURCE_EXHAUSTED_BIT = (((jlong)1) << (JVMTI_EVENT_RESOURCE_EXHAUSTED - TOTAL_MIN_EVENT_TYPE_VAL));85static const jlong VM_OBJECT_ALLOC_BIT = (((jlong)1) << (JVMTI_EVENT_VM_OBJECT_ALLOC - TOTAL_MIN_EVENT_TYPE_VAL));8687// bits for extension events88static const jlong CLASS_UNLOAD_BIT = (((jlong)1) << (EXT_EVENT_CLASS_UNLOAD - TOTAL_MIN_EVENT_TYPE_VAL));899091static const jlong MONITOR_BITS = MONITOR_CONTENDED_ENTER_BIT | MONITOR_CONTENDED_ENTERED_BIT |92MONITOR_WAIT_BIT | MONITOR_WAITED_BIT;93static const jlong EXCEPTION_BITS = EXCEPTION_THROW_BIT | EXCEPTION_CATCH_BIT;94static const jlong INTERP_EVENT_BITS = SINGLE_STEP_BIT | METHOD_ENTRY_BIT | METHOD_EXIT_BIT |95FRAME_POP_BIT | FIELD_ACCESS_BIT | FIELD_MODIFICATION_BIT;96static const jlong THREAD_FILTERED_EVENT_BITS = INTERP_EVENT_BITS | EXCEPTION_BITS | MONITOR_BITS |97BREAKPOINT_BIT | CLASS_LOAD_BIT | CLASS_PREPARE_BIT | THREAD_END_BIT;98static const jlong NEED_THREAD_LIFE_EVENTS = THREAD_FILTERED_EVENT_BITS | THREAD_START_BIT;99static const jlong EARLY_EVENT_BITS = CLASS_FILE_LOAD_HOOK_BIT |100VM_START_BIT | VM_INIT_BIT | VM_DEATH_BIT | NATIVE_METHOD_BIND_BIT |101THREAD_START_BIT | THREAD_END_BIT |102DYNAMIC_CODE_GENERATED_BIT;103static const jlong GLOBAL_EVENT_BITS = ~THREAD_FILTERED_EVENT_BITS;104static const jlong SHOULD_POST_ON_EXCEPTIONS_BITS = EXCEPTION_BITS | METHOD_EXIT_BIT | FRAME_POP_BIT;105106///////////////////////////////////////////////////////////////107//108// JvmtiEventEnabled109//110111JvmtiEventEnabled::JvmtiEventEnabled() {112clear();113}114115116void JvmtiEventEnabled::clear() {117_enabled_bits = 0;118#ifndef PRODUCT119_init_guard = JEE_INIT_GUARD;120#endif121}122123void JvmtiEventEnabled::set_enabled(jvmtiEvent event_type, bool enabled) {124jlong bits = get_bits();125jlong mask = bit_for(event_type);126if (enabled) {127bits |= mask;128} else {129bits &= ~mask;130}131set_bits(bits);132}133134135///////////////////////////////////////////////////////////////136//137// JvmtiEnvThreadEventEnable138//139140JvmtiEnvThreadEventEnable::JvmtiEnvThreadEventEnable() {141_event_user_enabled.clear();142_event_enabled.clear();143}144145146JvmtiEnvThreadEventEnable::~JvmtiEnvThreadEventEnable() {147_event_user_enabled.clear();148_event_enabled.clear();149}150151152///////////////////////////////////////////////////////////////153//154// JvmtiThreadEventEnable155//156157JvmtiThreadEventEnable::JvmtiThreadEventEnable() {158_event_enabled.clear();159}160161162JvmtiThreadEventEnable::~JvmtiThreadEventEnable() {163_event_enabled.clear();164}165166167///////////////////////////////////////////////////////////////168//169// JvmtiEnvEventEnable170//171172JvmtiEnvEventEnable::JvmtiEnvEventEnable() {173_event_user_enabled.clear();174_event_callback_enabled.clear();175_event_enabled.clear();176}177178179JvmtiEnvEventEnable::~JvmtiEnvEventEnable() {180_event_user_enabled.clear();181_event_callback_enabled.clear();182_event_enabled.clear();183}184185186///////////////////////////////////////////////////////////////187//188// VM_EnterInterpOnlyMode189//190191class VM_EnterInterpOnlyMode : public VM_Operation {192private:193JvmtiThreadState *_state;194195public:196VM_EnterInterpOnlyMode(JvmtiThreadState *state);197198bool allow_nested_vm_operations() const { return true; }199VMOp_Type type() const { return VMOp_EnterInterpOnlyMode; }200void doit();201202// to do: this same function is in jvmtiImpl - should be in one place203bool can_be_deoptimized(vframe* vf) {204return (vf->is_compiled_frame() && vf->fr().can_be_deoptimized());205}206};207208VM_EnterInterpOnlyMode::VM_EnterInterpOnlyMode(JvmtiThreadState *state)209: _state(state)210{211}212213214void VM_EnterInterpOnlyMode::doit() {215// Set up the current stack depth for later tracking216_state->invalidate_cur_stack_depth();217218_state->enter_interp_only_mode();219220JavaThread *thread = _state->get_thread();221if (thread->has_last_Java_frame()) {222// If running in fullspeed mode, single stepping is implemented223// as follows: first, the interpreter does not dispatch to224// compiled code for threads that have single stepping enabled;225// second, we deoptimize all methods on the thread's stack when226// interpreted-only mode is enabled the first time for a given227// thread (nothing to do if no Java frames yet).228int num_marked = 0;229ResourceMark resMark;230RegisterMap rm(thread, false);231for (vframe* vf = thread->last_java_vframe(&rm); vf; vf = vf->sender()) {232if (can_be_deoptimized(vf)) {233((compiledVFrame*) vf)->code()->mark_for_deoptimization();234++num_marked;235}236}237if (num_marked > 0) {238VM_Deoptimize op;239VMThread::execute(&op);240}241}242}243244245///////////////////////////////////////////////////////////////246//247// VM_ChangeSingleStep248//249250class VM_ChangeSingleStep : public VM_Operation {251private:252bool _on;253254public:255VM_ChangeSingleStep(bool on);256VMOp_Type type() const { return VMOp_ChangeSingleStep; }257bool allow_nested_vm_operations() const { return true; }258void doit(); // method definition is after definition of JvmtiEventControllerPrivate because of scoping259};260261262VM_ChangeSingleStep::VM_ChangeSingleStep(bool on)263: _on(on != 0)264{265}266267268269270///////////////////////////////////////////////////////////////271//272// JvmtiEventControllerPrivate273//274// Private internal implementation methods for JvmtiEventController.275//276// These methods are thread safe either because they are called277// in early VM initialization which is single threaded, or they278// hold the JvmtiThreadState_lock.279//280281class JvmtiEventControllerPrivate : public AllStatic {282static bool _initialized;283public:284static void set_should_post_single_step(bool on);285static void enter_interp_only_mode(JvmtiThreadState *state);286static void leave_interp_only_mode(JvmtiThreadState *state);287static void recompute_enabled();288static jlong recompute_env_enabled(JvmtiEnvBase* env);289static jlong recompute_env_thread_enabled(JvmtiEnvThreadState* ets, JvmtiThreadState* state);290static jlong recompute_thread_enabled(JvmtiThreadState *state);291static void event_init();292293static void set_user_enabled(JvmtiEnvBase *env, JavaThread *thread,294jvmtiEvent event_type, bool enabled);295static void set_event_callbacks(JvmtiEnvBase *env,296const jvmtiEventCallbacks* callbacks,297jint size_of_callbacks);298299static void set_extension_event_callback(JvmtiEnvBase *env,300jint extension_event_index,301jvmtiExtensionEvent callback);302303static void set_frame_pop(JvmtiEnvThreadState *env_thread, JvmtiFramePop fpop);304static void clear_frame_pop(JvmtiEnvThreadState *env_thread, JvmtiFramePop fpop);305static void clear_to_frame_pop(JvmtiEnvThreadState *env_thread, JvmtiFramePop fpop);306static void change_field_watch(jvmtiEvent event_type, bool added);307308static void thread_started(JavaThread *thread);309static void thread_ended(JavaThread *thread);310311static void env_initialize(JvmtiEnvBase *env);312static void env_dispose(JvmtiEnvBase *env);313314static void vm_start();315static void vm_init();316static void vm_death();317318static void trace_changed(JvmtiThreadState *state, jlong now_enabled, jlong changed);319static void trace_changed(jlong now_enabled, jlong changed);320};321322bool JvmtiEventControllerPrivate::_initialized = false;323324void JvmtiEventControllerPrivate::set_should_post_single_step(bool on) {325// we have permission to do this, VM op doesn't326JvmtiExport::set_should_post_single_step(on);327}328329330// This change must always be occur when at a safepoint.331// Being at a safepoint causes the interpreter to use the332// safepoint dispatch table which we overload to find single333// step points. Just to be sure that it has been set, we334// call notice_safepoints when turning on single stepping.335// When we leave our current safepoint, should_post_single_step336// will be checked by the interpreter, and the table kept337// or changed accordingly.338void VM_ChangeSingleStep::doit() {339JvmtiEventControllerPrivate::set_should_post_single_step(_on);340if (_on) {341Interpreter::notice_safepoints();342}343}344345346void JvmtiEventControllerPrivate::enter_interp_only_mode(JvmtiThreadState *state) {347EC_TRACE(("JVMTI [%s] # Entering interpreter only mode",348JvmtiTrace::safe_get_thread_name(state->get_thread())));349350VM_EnterInterpOnlyMode op(state);351VMThread::execute(&op);352}353354355void356JvmtiEventControllerPrivate::leave_interp_only_mode(JvmtiThreadState *state) {357EC_TRACE(("JVMTI [%s] # Leaving interpreter only mode",358JvmtiTrace::safe_get_thread_name(state->get_thread())));359state->leave_interp_only_mode();360}361362363void364JvmtiEventControllerPrivate::trace_changed(JvmtiThreadState *state, jlong now_enabled, jlong changed) {365#ifdef JVMTI_TRACE366if (JvmtiTrace::trace_event_controller()) {367SafeResourceMark rm;368// traces standard events only369for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {370jlong bit = JvmtiEventEnabled::bit_for((jvmtiEvent)ei);371if (changed & bit) {372// it changed, print it373tty->print_cr("JVMTI [%s] # %s event %s",374JvmtiTrace::safe_get_thread_name(state->get_thread()),375(now_enabled & bit)? "Enabling" : "Disabling", JvmtiTrace::event_name((jvmtiEvent)ei));376}377}378}379#endif /*JVMTI_TRACE */380}381382383void384JvmtiEventControllerPrivate::trace_changed(jlong now_enabled, jlong changed) {385#ifdef JVMTI_TRACE386if (JvmtiTrace::trace_event_controller()) {387SafeResourceMark rm;388// traces standard events only389for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {390jlong bit = JvmtiEventEnabled::bit_for((jvmtiEvent)ei);391if (changed & bit) {392// it changed, print it393tty->print_cr("JVMTI [-] # %s event %s",394(now_enabled & bit)? "Enabling" : "Disabling", JvmtiTrace::event_name((jvmtiEvent)ei));395}396}397}398#endif /*JVMTI_TRACE */399}400401402// For the specified env: compute the currently truly enabled events403// set external state accordingly.404// Return value and set value must include all events.405// But outside this class, only non-thread-filtered events can be queried..406jlong407JvmtiEventControllerPrivate::recompute_env_enabled(JvmtiEnvBase* env) {408jlong was_enabled = env->env_event_enable()->_event_enabled.get_bits();409jlong now_enabled =410env->env_event_enable()->_event_callback_enabled.get_bits() &411env->env_event_enable()->_event_user_enabled.get_bits();412413switch (JvmtiEnv::get_phase()) {414case JVMTI_PHASE_PRIMORDIAL:415case JVMTI_PHASE_ONLOAD:416// only these events allowed in primordial or onload phase417now_enabled &= (EARLY_EVENT_BITS & ~THREAD_FILTERED_EVENT_BITS);418break;419case JVMTI_PHASE_START:420// only these events allowed in start phase421now_enabled &= EARLY_EVENT_BITS;422break;423case JVMTI_PHASE_LIVE:424// all events allowed during live phase425break;426case JVMTI_PHASE_DEAD:427// no events allowed when dead428now_enabled = 0;429break;430default:431assert(false, "no other phases - sanity check");432break;433}434435// will we really send these events to this env436env->env_event_enable()->_event_enabled.set_bits(now_enabled);437438trace_changed(now_enabled, (now_enabled ^ was_enabled) & ~THREAD_FILTERED_EVENT_BITS);439440return now_enabled;441}442443444// For the specified env and thread: compute the currently truly enabled events445// set external state accordingly. Only thread-filtered events are included.446jlong447JvmtiEventControllerPrivate::recompute_env_thread_enabled(JvmtiEnvThreadState* ets, JvmtiThreadState* state) {448JvmtiEnv *env = ets->get_env();449450jlong was_enabled = ets->event_enable()->_event_enabled.get_bits();451jlong now_enabled = THREAD_FILTERED_EVENT_BITS &452env->env_event_enable()->_event_callback_enabled.get_bits() &453(env->env_event_enable()->_event_user_enabled.get_bits() |454ets->event_enable()->_event_user_enabled.get_bits());455456// for frame pops and field watchs, computed enabled state457// is only true if an event has been requested458if (!ets->has_frame_pops()) {459now_enabled &= ~FRAME_POP_BIT;460}461if (*((int *)JvmtiExport::get_field_access_count_addr()) == 0) {462now_enabled &= ~FIELD_ACCESS_BIT;463}464if (*((int *)JvmtiExport::get_field_modification_count_addr()) == 0) {465now_enabled &= ~FIELD_MODIFICATION_BIT;466}467468switch (JvmtiEnv::get_phase()) {469case JVMTI_PHASE_DEAD:470// no events allowed when dead471now_enabled = 0;472break;473}474475// if anything changed do update476if (now_enabled != was_enabled) {477478// will we really send these events to this thread x env479ets->event_enable()->_event_enabled.set_bits(now_enabled);480481// If the enabled status of the single step or breakpoint events changed,482// the location status may need to change as well.483jlong changed = now_enabled ^ was_enabled;484if (changed & SINGLE_STEP_BIT) {485ets->reset_current_location(JVMTI_EVENT_SINGLE_STEP, (now_enabled & SINGLE_STEP_BIT) != 0);486}487if (changed & BREAKPOINT_BIT) {488ets->reset_current_location(JVMTI_EVENT_BREAKPOINT, (now_enabled & BREAKPOINT_BIT) != 0);489}490trace_changed(state, now_enabled, changed);491}492return now_enabled;493}494495496// For the specified thread: compute the currently truly enabled events497// set external state accordingly. Only thread-filtered events are included.498jlong499JvmtiEventControllerPrivate::recompute_thread_enabled(JvmtiThreadState *state) {500if (state == NULL) {501// associated JavaThread is exiting502return (jlong)0;503}504505jlong was_any_env_enabled = state->thread_event_enable()->_event_enabled.get_bits();506jlong any_env_enabled = 0;507508{509// This iteration will include JvmtiEnvThreadStates whoses environments510// have been disposed. These JvmtiEnvThreadStates must not be filtered511// as recompute must be called on them to disable their events,512JvmtiEnvThreadStateIterator it(state);513for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {514any_env_enabled |= recompute_env_thread_enabled(ets, state);515}516}517518if (any_env_enabled != was_any_env_enabled) {519// mark if event is truly enabled on this thread in any environment520state->thread_event_enable()->_event_enabled.set_bits(any_env_enabled);521522// compute interp_only mode523bool should_be_interp = (any_env_enabled & INTERP_EVENT_BITS) != 0;524bool is_now_interp = state->is_interp_only_mode();525526if (should_be_interp != is_now_interp) {527if (should_be_interp) {528enter_interp_only_mode(state);529} else {530leave_interp_only_mode(state);531}532}533534// update the JavaThread cached value for thread-specific should_post_on_exceptions value535bool should_post_on_exceptions = (any_env_enabled & SHOULD_POST_ON_EXCEPTIONS_BITS) != 0;536state->set_should_post_on_exceptions(should_post_on_exceptions);537}538539return any_env_enabled;540}541542543// Compute truly enabled events - meaning if the event can and could be544// sent. An event is truly enabled if it is user enabled on the thread545// or globally user enabled, but only if there is a callback or event hook546// for it and, for field watch and frame pop, one has been set.547// Compute if truly enabled, per thread, per environment, per combination548// (thread x environment), and overall. These merges are true if any is true.549// True per thread if some environment has callback set and the event is globally550// enabled or enabled for this thread.551// True per environment if the callback is set and the event is globally552// enabled in this environment or enabled for any thread in this environment.553// True per combination if the environment has the callback set and the554// event is globally enabled in this environment or the event is enabled555// for this thread and environment.556//557// All states transitions dependent on these transitions are also handled here.558void559JvmtiEventControllerPrivate::recompute_enabled() {560assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");561562// event enabled for any thread in any environment563jlong was_any_env_thread_enabled = JvmtiEventController::_universal_global_event_enabled.get_bits();564jlong any_env_thread_enabled = 0;565566EC_TRACE(("JVMTI [-] # recompute enabled - before %llx", was_any_env_thread_enabled));567568// compute non-thread-filters events.569// This must be done separately from thread-filtered events, since some570// events can occur before any threads exist.571JvmtiEnvIterator it;572for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) {573any_env_thread_enabled |= recompute_env_enabled(env);574}575576// We need to create any missing jvmti_thread_state if there are globally set thread577// filtered events and there weren't last time578if ( (any_env_thread_enabled & THREAD_FILTERED_EVENT_BITS) != 0 &&579(was_any_env_thread_enabled & THREAD_FILTERED_EVENT_BITS) == 0) {580assert(JvmtiEnv::is_vm_live() || (JvmtiEnv::get_phase()==JVMTI_PHASE_START),581"thread filtered events should not be enabled when VM not in start or live phase");582{583MutexLocker mu(Threads_lock); //hold the Threads_lock for the iteration584for (JavaThread *tp = Threads::first(); tp != NULL; tp = tp->next()) {585// state_for_while_locked() makes tp->is_exiting() check586JvmtiThreadState::state_for_while_locked(tp); // create the thread state if missing587}588}// release Threads_lock589}590591// compute and set thread-filtered events592for (JvmtiThreadState *state = JvmtiThreadState::first(); state != NULL; state = state->next()) {593any_env_thread_enabled |= recompute_thread_enabled(state);594}595596// set universal state (across all envs and threads)597jlong delta = any_env_thread_enabled ^ was_any_env_thread_enabled;598if (delta != 0) {599JvmtiExport::set_should_post_field_access((any_env_thread_enabled & FIELD_ACCESS_BIT) != 0);600JvmtiExport::set_should_post_field_modification((any_env_thread_enabled & FIELD_MODIFICATION_BIT) != 0);601JvmtiExport::set_should_post_class_load((any_env_thread_enabled & CLASS_LOAD_BIT) != 0);602JvmtiExport::set_should_post_class_file_load_hook((any_env_thread_enabled & CLASS_FILE_LOAD_HOOK_BIT) != 0);603JvmtiExport::set_should_post_native_method_bind((any_env_thread_enabled & NATIVE_METHOD_BIND_BIT) != 0);604JvmtiExport::set_should_post_dynamic_code_generated((any_env_thread_enabled & DYNAMIC_CODE_GENERATED_BIT) != 0);605JvmtiExport::set_should_post_data_dump((any_env_thread_enabled & DATA_DUMP_BIT) != 0);606JvmtiExport::set_should_post_class_prepare((any_env_thread_enabled & CLASS_PREPARE_BIT) != 0);607JvmtiExport::set_should_post_class_unload((any_env_thread_enabled & CLASS_UNLOAD_BIT) != 0);608JvmtiExport::set_should_post_monitor_contended_enter((any_env_thread_enabled & MONITOR_CONTENDED_ENTER_BIT) != 0);609JvmtiExport::set_should_post_monitor_contended_entered((any_env_thread_enabled & MONITOR_CONTENDED_ENTERED_BIT) != 0);610JvmtiExport::set_should_post_monitor_wait((any_env_thread_enabled & MONITOR_WAIT_BIT) != 0);611JvmtiExport::set_should_post_monitor_waited((any_env_thread_enabled & MONITOR_WAITED_BIT) != 0);612JvmtiExport::set_should_post_garbage_collection_start((any_env_thread_enabled & GARBAGE_COLLECTION_START_BIT) != 0);613JvmtiExport::set_should_post_garbage_collection_finish((any_env_thread_enabled & GARBAGE_COLLECTION_FINISH_BIT) != 0);614JvmtiExport::set_should_post_object_free((any_env_thread_enabled & OBJECT_FREE_BIT) != 0);615JvmtiExport::set_should_post_resource_exhausted((any_env_thread_enabled & RESOURCE_EXHAUSTED_BIT) != 0);616JvmtiExport::set_should_post_compiled_method_load((any_env_thread_enabled & COMPILED_METHOD_LOAD_BIT) != 0);617JvmtiExport::set_should_post_compiled_method_unload((any_env_thread_enabled & COMPILED_METHOD_UNLOAD_BIT) != 0);618JvmtiExport::set_should_post_vm_object_alloc((any_env_thread_enabled & VM_OBJECT_ALLOC_BIT) != 0);619620// need this if we want thread events or we need them to init data621JvmtiExport::set_should_post_thread_life((any_env_thread_enabled & NEED_THREAD_LIFE_EVENTS) != 0);622623// If single stepping is turned on or off, execute the VM op to change it.624if (delta & SINGLE_STEP_BIT) {625switch (JvmtiEnv::get_phase()) {626case JVMTI_PHASE_DEAD:627// If the VM is dying we can't execute VM ops628break;629case JVMTI_PHASE_LIVE: {630VM_ChangeSingleStep op((any_env_thread_enabled & SINGLE_STEP_BIT) != 0);631VMThread::execute(&op);632break;633}634default:635assert(false, "should never come here before live phase");636break;637}638}639640// set global truly enabled, that is, any thread in any environment641JvmtiEventController::_universal_global_event_enabled.set_bits(any_env_thread_enabled);642643// set global should_post_on_exceptions644JvmtiExport::set_should_post_on_exceptions((any_env_thread_enabled & SHOULD_POST_ON_EXCEPTIONS_BITS) != 0);645646}647648EC_TRACE(("JVMTI [-] # recompute enabled - after %llx", any_env_thread_enabled));649}650651652void653JvmtiEventControllerPrivate::thread_started(JavaThread *thread) {654assert(thread->is_Java_thread(), "Must be JavaThread");655assert(thread == Thread::current(), "must be current thread");656assert(JvmtiEnvBase::environments_might_exist(), "to enter event controller, JVM TI environments must exist");657658EC_TRACE(("JVMTI [%s] # thread started", JvmtiTrace::safe_get_thread_name(thread)));659660// if we have any thread filtered events globally enabled, create/update the thread state661if ((JvmtiEventController::_universal_global_event_enabled.get_bits() & THREAD_FILTERED_EVENT_BITS) != 0) {662MutexLocker mu(JvmtiThreadState_lock);663// create the thread state if missing664JvmtiThreadState *state = JvmtiThreadState::state_for_while_locked(thread);665if (state != NULL) { // skip threads with no JVMTI thread state666recompute_thread_enabled(state);667}668}669}670671672void673JvmtiEventControllerPrivate::thread_ended(JavaThread *thread) {674// Removes the JvmtiThreadState associated with the specified thread.675// May be called after all environments have been disposed.676assert(JvmtiThreadState_lock->is_locked(), "sanity check");677678EC_TRACE(("JVMTI [%s] # thread ended", JvmtiTrace::safe_get_thread_name(thread)));679680JvmtiThreadState *state = thread->jvmti_thread_state();681assert(state != NULL, "else why are we here?");682delete state;683}684685void JvmtiEventControllerPrivate::set_event_callbacks(JvmtiEnvBase *env,686const jvmtiEventCallbacks* callbacks,687jint size_of_callbacks) {688assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");689EC_TRACE(("JVMTI [*] # set event callbacks"));690691env->set_event_callbacks(callbacks, size_of_callbacks);692jlong enabled_bits = 0;693for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {694jvmtiEvent evt_t = (jvmtiEvent)ei;695if (env->has_callback(evt_t)) {696enabled_bits |= JvmtiEventEnabled::bit_for(evt_t);697}698}699env->env_event_enable()->_event_callback_enabled.set_bits(enabled_bits);700recompute_enabled();701}702703void704JvmtiEventControllerPrivate::set_extension_event_callback(JvmtiEnvBase *env,705jint extension_event_index,706jvmtiExtensionEvent callback)707{708assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");709EC_TRACE(("JVMTI [*] # set extension event callback"));710711// extension events are allocated below JVMTI_MIN_EVENT_TYPE_VAL712assert(extension_event_index >= (jint)EXT_MIN_EVENT_TYPE_VAL &&713extension_event_index <= (jint)EXT_MAX_EVENT_TYPE_VAL, "sanity check");714715716// As the bits for both standard (jvmtiEvent) and extension717// (jvmtiExtEvents) are stored in the same word we cast here to718// jvmtiEvent to set/clear the bit for this extension event.719jvmtiEvent event_type = (jvmtiEvent)extension_event_index;720721// Prevent a possible race condition where events are re-enabled by a call to722// set event callbacks, where the DisposeEnvironment occurs after the boiler-plate723// environment check and before the lock is acquired.724// We can safely do the is_valid check now, as JvmtiThreadState_lock is held.725bool enabling = (callback != NULL) && (env->is_valid());726env->env_event_enable()->set_user_enabled(event_type, enabling);727728// update the callback729jvmtiExtEventCallbacks* ext_callbacks = env->ext_callbacks();730switch (extension_event_index) {731case EXT_EVENT_CLASS_UNLOAD :732ext_callbacks->ClassUnload = callback;733break;734default:735ShouldNotReachHere();736}737738// update the callback enable/disable bit739jlong enabled_bits = env->env_event_enable()->_event_callback_enabled.get_bits();740jlong bit_for = JvmtiEventEnabled::bit_for(event_type);741if (enabling) {742enabled_bits |= bit_for;743} else {744enabled_bits &= ~bit_for;745}746env->env_event_enable()->_event_callback_enabled.set_bits(enabled_bits);747748recompute_enabled();749}750751752void753JvmtiEventControllerPrivate::env_initialize(JvmtiEnvBase *env) {754assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");755EC_TRACE(("JVMTI [*] # env initialize"));756757if (JvmtiEnvBase::is_vm_live()) {758// if we didn't initialize event info already (this is a late759// launched environment), do it now.760event_init();761}762763env->initialize();764765// add the JvmtiEnvThreadState to each JvmtiThreadState766for (JvmtiThreadState *state = JvmtiThreadState::first(); state != NULL; state = state->next()) {767state->add_env(env);768assert((JvmtiEnv*)(state->env_thread_state(env)->get_env()) == env, "sanity check");769}770JvmtiEventControllerPrivate::recompute_enabled();771}772773774void775JvmtiEventControllerPrivate::env_dispose(JvmtiEnvBase *env) {776assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");777EC_TRACE(("JVMTI [*] # env dispose"));778779// Before the environment is marked disposed, disable all events on this780// environment (by zapping the callbacks). As a result, the disposed781// environment will not call event handlers.782set_event_callbacks(env, NULL, 0);783for (jint extension_event_index = EXT_MIN_EVENT_TYPE_VAL;784extension_event_index <= EXT_MAX_EVENT_TYPE_VAL;785++extension_event_index) {786set_extension_event_callback(env, extension_event_index, NULL);787}788789// Let the environment finish disposing itself.790env->env_dispose();791}792793794void795JvmtiEventControllerPrivate::set_user_enabled(JvmtiEnvBase *env, JavaThread *thread,796jvmtiEvent event_type, bool enabled) {797assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");798799EC_TRACE(("JVMTI [%s] # user %s event %s",800thread==NULL? "ALL": JvmtiTrace::safe_get_thread_name(thread),801enabled? "enabled" : "disabled", JvmtiTrace::event_name(event_type)));802803if (thread == NULL) {804env->env_event_enable()->set_user_enabled(event_type, enabled);805} else {806// create the thread state (if it didn't exist before)807JvmtiThreadState *state = JvmtiThreadState::state_for_while_locked(thread);808if (state != NULL) {809state->env_thread_state(env)->event_enable()->set_user_enabled(event_type, enabled);810}811}812recompute_enabled();813}814815816void817JvmtiEventControllerPrivate::set_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {818EC_TRACE(("JVMTI [%s] # set frame pop - frame=%d",819JvmtiTrace::safe_get_thread_name(ets->get_thread()),820fpop.frame_number() ));821822ets->get_frame_pops()->set(fpop);823recompute_thread_enabled(ets->get_thread()->jvmti_thread_state());824}825826827void828JvmtiEventControllerPrivate::clear_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {829EC_TRACE(("JVMTI [%s] # clear frame pop - frame=%d",830JvmtiTrace::safe_get_thread_name(ets->get_thread()),831fpop.frame_number() ));832833ets->get_frame_pops()->clear(fpop);834recompute_thread_enabled(ets->get_thread()->jvmti_thread_state());835}836837838void839JvmtiEventControllerPrivate::clear_to_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {840int cleared_cnt = ets->get_frame_pops()->clear_to(fpop);841842EC_TRACE(("JVMTI [%s] # clear to frame pop - frame=%d, count=%d",843JvmtiTrace::safe_get_thread_name(ets->get_thread()),844fpop.frame_number(),845cleared_cnt ));846847if (cleared_cnt > 0) {848recompute_thread_enabled(ets->get_thread()->jvmti_thread_state());849}850}851852void853JvmtiEventControllerPrivate::change_field_watch(jvmtiEvent event_type, bool added) {854int *count_addr;855856switch (event_type) {857case JVMTI_EVENT_FIELD_MODIFICATION:858count_addr = (int *)JvmtiExport::get_field_modification_count_addr();859break;860case JVMTI_EVENT_FIELD_ACCESS:861count_addr = (int *)JvmtiExport::get_field_access_count_addr();862break;863default:864assert(false, "incorrect event");865return;866}867868EC_TRACE(("JVMTI [-] # change field watch - %s %s count=%d",869event_type==JVMTI_EVENT_FIELD_MODIFICATION? "modification" : "access",870added? "add" : "remove",871*count_addr));872873if (added) {874(*count_addr)++;875if (*count_addr == 1) {876recompute_enabled();877}878} else {879if (*count_addr > 0) {880(*count_addr)--;881if (*count_addr == 0) {882recompute_enabled();883}884} else {885assert(false, "field watch out of phase");886}887}888}889890void891JvmtiEventControllerPrivate::event_init() {892assert(JvmtiThreadState_lock->is_locked(), "sanity check");893894if (_initialized) {895return;896}897898EC_TRACE(("JVMTI [-] # VM live"));899900#ifdef ASSERT901// check that our idea and the spec's idea of threaded events match902for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {903jlong bit = JvmtiEventEnabled::bit_for((jvmtiEvent)ei);904assert(((THREAD_FILTERED_EVENT_BITS & bit) != 0) == JvmtiUtil::event_threaded(ei),905"thread filtered event list does not match");906}907#endif908909_initialized = true;910}911912void913JvmtiEventControllerPrivate::vm_start() {914// some events are now able to be enabled (phase has changed)915JvmtiEventControllerPrivate::recompute_enabled();916}917918919void920JvmtiEventControllerPrivate::vm_init() {921event_init();922923// all the events are now able to be enabled (phase has changed)924JvmtiEventControllerPrivate::recompute_enabled();925}926927928void929JvmtiEventControllerPrivate::vm_death() {930// events are disabled (phase has changed)931JvmtiEventControllerPrivate::recompute_enabled();932}933934935///////////////////////////////////////////////////////////////936//937// JvmtiEventController938//939940JvmtiEventEnabled JvmtiEventController::_universal_global_event_enabled;941942bool943JvmtiEventController::is_global_event(jvmtiEvent event_type) {944assert(is_valid_event_type(event_type), "invalid event type");945jlong bit_for = ((jlong)1) << (event_type - TOTAL_MIN_EVENT_TYPE_VAL);946return((bit_for & GLOBAL_EVENT_BITS)!=0);947}948949void950JvmtiEventController::set_user_enabled(JvmtiEnvBase *env, JavaThread *thread, jvmtiEvent event_type, bool enabled) {951if (Threads::number_of_threads() == 0) {952// during early VM start-up locks don't exist, but we are safely single threaded,953// call the functionality without holding the JvmtiThreadState_lock.954JvmtiEventControllerPrivate::set_user_enabled(env, thread, event_type, enabled);955} else {956MutexLocker mu(JvmtiThreadState_lock);957JvmtiEventControllerPrivate::set_user_enabled(env, thread, event_type, enabled);958}959}960961962void963JvmtiEventController::set_event_callbacks(JvmtiEnvBase *env,964const jvmtiEventCallbacks* callbacks,965jint size_of_callbacks) {966if (Threads::number_of_threads() == 0) {967// during early VM start-up locks don't exist, but we are safely single threaded,968// call the functionality without holding the JvmtiThreadState_lock.969JvmtiEventControllerPrivate::set_event_callbacks(env, callbacks, size_of_callbacks);970} else {971MutexLocker mu(JvmtiThreadState_lock);972JvmtiEventControllerPrivate::set_event_callbacks(env, callbacks, size_of_callbacks);973}974}975976void977JvmtiEventController::set_extension_event_callback(JvmtiEnvBase *env,978jint extension_event_index,979jvmtiExtensionEvent callback) {980if (Threads::number_of_threads() == 0) {981JvmtiEventControllerPrivate::set_extension_event_callback(env, extension_event_index, callback);982} else {983MutexLocker mu(JvmtiThreadState_lock);984JvmtiEventControllerPrivate::set_extension_event_callback(env, extension_event_index, callback);985}986}987988989990991void992JvmtiEventController::set_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {993MutexLocker mu(JvmtiThreadState_lock);994JvmtiEventControllerPrivate::set_frame_pop(ets, fpop);995}996997998void999JvmtiEventController::clear_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {1000MutexLocker mu(JvmtiThreadState_lock);1001JvmtiEventControllerPrivate::clear_frame_pop(ets, fpop);1002}100310041005void1006JvmtiEventController::clear_to_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {1007MutexLocker mu(JvmtiThreadState_lock);1008JvmtiEventControllerPrivate::clear_to_frame_pop(ets, fpop);1009}10101011void1012JvmtiEventController::change_field_watch(jvmtiEvent event_type, bool added) {1013MutexLocker mu(JvmtiThreadState_lock);1014JvmtiEventControllerPrivate::change_field_watch(event_type, added);1015}10161017void1018JvmtiEventController::thread_started(JavaThread *thread) {1019// operates only on the current thread1020// JvmtiThreadState_lock grabbed only if needed.1021JvmtiEventControllerPrivate::thread_started(thread);1022}10231024void1025JvmtiEventController::thread_ended(JavaThread *thread) {1026// operates only on the current thread1027// JvmtiThreadState_lock grabbed only if needed.1028JvmtiEventControllerPrivate::thread_ended(thread);1029}10301031void1032JvmtiEventController::env_initialize(JvmtiEnvBase *env) {1033if (Threads::number_of_threads() == 0) {1034// during early VM start-up locks don't exist, but we are safely single threaded,1035// call the functionality without holding the JvmtiThreadState_lock.1036JvmtiEventControllerPrivate::env_initialize(env);1037} else {1038MutexLocker mu(JvmtiThreadState_lock);1039JvmtiEventControllerPrivate::env_initialize(env);1040}1041}10421043void1044JvmtiEventController::env_dispose(JvmtiEnvBase *env) {1045if (Threads::number_of_threads() == 0) {1046// during early VM start-up locks don't exist, but we are safely single threaded,1047// call the functionality without holding the JvmtiThreadState_lock.1048JvmtiEventControllerPrivate::env_dispose(env);1049} else {1050MutexLocker mu(JvmtiThreadState_lock);1051JvmtiEventControllerPrivate::env_dispose(env);1052}1053}105410551056void1057JvmtiEventController::vm_start() {1058if (JvmtiEnvBase::environments_might_exist()) {1059MutexLocker mu(JvmtiThreadState_lock);1060JvmtiEventControllerPrivate::vm_start();1061}1062}10631064void1065JvmtiEventController::vm_init() {1066if (JvmtiEnvBase::environments_might_exist()) {1067MutexLocker mu(JvmtiThreadState_lock);1068JvmtiEventControllerPrivate::vm_init();1069}1070}10711072void1073JvmtiEventController::vm_death() {1074if (JvmtiEnvBase::environments_might_exist()) {1075MutexLocker mu(JvmtiThreadState_lock);1076JvmtiEventControllerPrivate::vm_death();1077}1078}107910801081