duke@435: /* coleenp@4037: * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_PRIMS_JVMTITHREADSTATE_HPP stefank@2314: #define SHARE_VM_PRIMS_JVMTITHREADSTATE_HPP stefank@2314: stefank@2314: #include "jvmtifiles/jvmti.h" stefank@2314: #include "memory/allocation.hpp" stefank@2314: #include "memory/allocation.inline.hpp" stefank@2314: #include "prims/jvmtiEventController.hpp" stefank@2314: #include "runtime/thread.hpp" stefank@2314: #include "utilities/growableArray.hpp" duke@435: duke@435: // duke@435: // Forward Declarations duke@435: // duke@435: duke@435: class JvmtiEnvBase; duke@435: class JvmtiEnvThreadState; duke@435: class JvmtiDynamicCodeEventCollector; duke@435: duke@435: enum JvmtiClassLoadKind { duke@435: jvmti_class_load_kind_load = 100, duke@435: jvmti_class_load_kind_retransform, duke@435: jvmti_class_load_kind_redefine duke@435: }; duke@435: duke@435: /////////////////////////////////////////////////////////////// duke@435: // duke@435: // class JvmtiEnvThreadStateIterator duke@435: // duke@435: // The only safe means of iterating through the JvmtiEnvThreadStates duke@435: // in a JvmtiThreadState. duke@435: // Note that this iteratation includes invalid environments pending duke@435: // deallocation -- in fact, some uses depend on this behavior. duke@435: // duke@435: class JvmtiEnvThreadStateIterator : public StackObj { duke@435: private: duke@435: JvmtiThreadState* state; duke@435: public: duke@435: JvmtiEnvThreadStateIterator(JvmtiThreadState* thread_state); duke@435: ~JvmtiEnvThreadStateIterator(); duke@435: JvmtiEnvThreadState* first(); duke@435: JvmtiEnvThreadState* next(JvmtiEnvThreadState* ets); duke@435: }; duke@435: duke@435: duke@435: /////////////////////////////////////////////////////////////// duke@435: // duke@435: // class JvmtiThreadState duke@435: // duke@435: // The Jvmti state for each thread (across all JvmtiEnv): duke@435: // 1. Local table of enabled events. zgu@3900: class JvmtiThreadState : public CHeapObj { duke@435: private: duke@435: friend class JvmtiEnv; duke@435: JavaThread *_thread; duke@435: bool _exception_detected; duke@435: bool _exception_caught; duke@435: bool _hide_single_stepping; duke@435: bool _pending_step_for_popframe; duke@435: bool _pending_step_for_earlyret; duke@435: int _hide_level; duke@435: duke@435: // Used to send class being redefined/retransformed and kind of transform duke@435: // info to the class file load hook event handler. duke@435: KlassHandle *_class_being_redefined; duke@435: JvmtiClassLoadKind _class_load_kind; duke@435: duke@435: // This is only valid when is_interp_only_mode() returns true duke@435: int _cur_stack_depth; duke@435: duke@435: JvmtiThreadEventEnable _thread_event_enable; duke@435: duke@435: // for support of JvmtiEnvThreadState duke@435: JvmtiEnvThreadState* _head_env_thread_state; duke@435: duke@435: // doubly-linked linear list of active thread state duke@435: // needed in order to iterate the list without holding Threads_lock duke@435: static JvmtiThreadState *_head; duke@435: JvmtiThreadState *_next; duke@435: JvmtiThreadState *_prev; duke@435: duke@435: // holds the current dynamic code event collector, NULL if no event collector in use duke@435: JvmtiDynamicCodeEventCollector* _dynamic_code_event_collector; duke@435: // holds the current vm object alloc event collector, NULL if no event collector in use duke@435: JvmtiVMObjectAllocEventCollector* _vm_object_alloc_event_collector; duke@435: duke@435: // Should only be created by factory methods duke@435: JvmtiThreadState(JavaThread *thread); duke@435: duke@435: friend class JvmtiEnvThreadStateIterator; duke@435: inline JvmtiEnvThreadState* head_env_thread_state(); duke@435: inline void set_head_env_thread_state(JvmtiEnvThreadState* ets); duke@435: duke@435: public: duke@435: ~JvmtiThreadState(); duke@435: duke@435: // is event_type enabled and usable for this thread in any enviroments? duke@435: bool is_enabled(jvmtiEvent event_type) { duke@435: return _thread_event_enable.is_enabled(event_type); duke@435: } duke@435: duke@435: JvmtiThreadEventEnable *thread_event_enable() { duke@435: return &_thread_event_enable; duke@435: } duke@435: duke@435: // Must only be called in situations where the state is for the current thread and duke@435: // the environment can not go away. To be safe, the returned JvmtiEnvThreadState duke@435: // must be used in such a way as there can be no intervening safepoints. duke@435: inline JvmtiEnvThreadState* env_thread_state(JvmtiEnvBase *env); duke@435: duke@435: static void periodic_clean_up(); duke@435: duke@435: void add_env(JvmtiEnvBase *env); duke@435: duke@435: // Used by the interpreter for fullspeed debugging support duke@435: bool is_interp_only_mode() { return _thread->is_interp_only_mode(); } duke@435: void enter_interp_only_mode(); duke@435: void leave_interp_only_mode(); duke@435: duke@435: // access to the linked list of all JVMTI thread states duke@435: static JvmtiThreadState *first() { duke@435: assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check"); duke@435: return _head; duke@435: } duke@435: duke@435: JvmtiThreadState *next() { duke@435: return _next; duke@435: } duke@435: duke@435: // Current stack depth is only valid when is_interp_only_mode() returns true. duke@435: // These functions should only be called at a safepoint - usually called from same thread. duke@435: // Returns the number of Java activations on the stack. duke@435: int cur_stack_depth(); duke@435: void invalidate_cur_stack_depth(); duke@435: void incr_cur_stack_depth(); duke@435: void decr_cur_stack_depth(); duke@435: duke@435: int count_frames(); duke@435: duke@435: inline JavaThread *get_thread() { return _thread; } duke@435: inline bool is_exception_detected() { return _exception_detected; } duke@435: inline bool is_exception_caught() { return _exception_caught; } duke@435: inline void set_exception_detected() { _exception_detected = true; duke@435: _exception_caught = false; } bpittore@3468: inline void clear_exception_detected() { bpittore@3468: _exception_detected = false; bpittore@3468: assert(_exception_caught == false, "_exception_caught is out of phase"); bpittore@3468: } duke@435: inline void set_exception_caught() { _exception_caught = true; duke@435: _exception_detected = false; } duke@435: duke@435: inline void clear_hide_single_stepping() { duke@435: if (_hide_level > 0) { duke@435: _hide_level--; duke@435: } else { duke@435: assert(_hide_single_stepping, "hide_single_stepping is out of phase"); duke@435: _hide_single_stepping = false; duke@435: } duke@435: } duke@435: inline bool hide_single_stepping() { return _hide_single_stepping; } duke@435: inline void set_hide_single_stepping() { duke@435: if (_hide_single_stepping) { duke@435: _hide_level++; duke@435: } else { duke@435: assert(_hide_level == 0, "hide_level is out of phase"); duke@435: _hide_single_stepping = true; duke@435: } duke@435: } duke@435: duke@435: // Step pending flag is set when PopFrame is called and it is cleared duke@435: // when step for the Pop Frame is completed. duke@435: // This logic is used to distinguish b/w step for pop frame and repeat step. duke@435: void set_pending_step_for_popframe() { _pending_step_for_popframe = true; } duke@435: void clr_pending_step_for_popframe() { _pending_step_for_popframe = false; } duke@435: bool is_pending_step_for_popframe() { return _pending_step_for_popframe; } duke@435: void process_pending_step_for_popframe(); duke@435: duke@435: // Step pending flag is set when ForceEarlyReturn is called and it is cleared duke@435: // when step for the ForceEarlyReturn is completed. duke@435: // This logic is used to distinguish b/w step for early return and repeat step. duke@435: void set_pending_step_for_earlyret() { _pending_step_for_earlyret = true; } duke@435: void clr_pending_step_for_earlyret() { _pending_step_for_earlyret = false; } duke@435: bool is_pending_step_for_earlyret() { return _pending_step_for_earlyret; } duke@435: void process_pending_step_for_earlyret(); duke@435: duke@435: // Setter and getter method is used to send redefined class info duke@435: // when class file load hook event is posted. duke@435: // It is set while loading redefined class and cleared before the duke@435: // class file load hook event is posted. duke@435: inline void set_class_being_redefined(KlassHandle *h_class, JvmtiClassLoadKind kind) { duke@435: _class_being_redefined = h_class; duke@435: _class_load_kind = kind; duke@435: } duke@435: duke@435: inline void clear_class_being_redefined() { duke@435: _class_being_redefined = NULL; duke@435: _class_load_kind = jvmti_class_load_kind_load; duke@435: } duke@435: duke@435: inline KlassHandle *get_class_being_redefined() { duke@435: return _class_being_redefined; duke@435: } duke@435: duke@435: inline JvmtiClassLoadKind get_class_load_kind() { duke@435: return _class_load_kind; duke@435: } duke@435: duke@435: // RedefineClasses support duke@435: // The bug 6214132 caused the verification to fail. duke@435: // duke@435: // Below is the detailed description of the fix approach taken: duke@435: // 1. What's done in RedefineClasses() before verification: duke@435: // a) A reference to the class being redefined (_the_class) and a duke@435: // reference to new version of the class (_scratch_class) are duke@435: // saved here for use during the bytecode verification phase of duke@435: // RedefineClasses. See RedefineVerifyMark for how these fields duke@435: // are managed. duke@435: // b) The _java_mirror field from _the_class is copied to the duke@435: // _java_mirror field in _scratch_class. This means that a jclass duke@435: // returned for _the_class or _scratch_class will refer to the duke@435: // same Java mirror. The verifier will see the "one true mirror" duke@435: // for the class being verified. duke@435: // 2. What is done at verification: duke@435: // When the verifier makes calls into the VM to ask questions about duke@435: // the class being verified, it will pass the jclass to JVM_* functions. duke@435: // The jclass is always pointing to the mirror of _the_class. duke@435: // ~28 JVM_* functions called by the verifier for the information duke@435: // about CP entries and klass structure should check the jvmtiThreadState coleenp@4037: // info about equivalent klass versions and use it to replace a Klass* coleenp@4037: // of _the_class with a Klass* of _scratch_class. The function duke@435: // class_to_verify_considering_redefinition() must be called for it. duke@435: // duke@435: // Note again, that this redirection happens only for the verifier thread. duke@435: // Other threads have very small overhead by checking the existence duke@435: // of the jvmtiThreadSate and the information about klasses equivalence. duke@435: // No JNI functions need to be changed, they don't reference the klass guts. duke@435: // The JavaThread pointer is already available in all JVM_* functions duke@435: // used by the verifier, so there is no extra performance issue with it. duke@435: duke@435: private: duke@435: KlassHandle *_the_class_for_redefinition_verification; duke@435: KlassHandle *_scratch_class_for_redefinition_verification; duke@435: duke@435: public: duke@435: inline void set_class_versions_map(KlassHandle *the_class, duke@435: KlassHandle *scratch_class) { duke@435: _the_class_for_redefinition_verification = the_class; duke@435: _scratch_class_for_redefinition_verification = scratch_class; duke@435: } duke@435: duke@435: inline void clear_class_versions_map() { set_class_versions_map(NULL, NULL); } duke@435: duke@435: static inline coleenp@4037: Klass* class_to_verify_considering_redefinition(Klass* klass, duke@435: JavaThread *thread) { duke@435: JvmtiThreadState *state = thread->jvmti_thread_state(); duke@435: if (state != NULL && state->_the_class_for_redefinition_verification != NULL) { duke@435: if ((*(state->_the_class_for_redefinition_verification))() == klass) { duke@435: klass = (*(state->_scratch_class_for_redefinition_verification))(); duke@435: } duke@435: } duke@435: return klass; duke@435: } duke@435: duke@435: // Todo: get rid of this! duke@435: private: duke@435: bool _debuggable; duke@435: public: duke@435: // Should the thread be enumerated by jvmtiInternal::GetAllThreads? duke@435: bool is_debuggable() { return _debuggable; } duke@435: // If a thread cannot be suspended (has no valid last_java_frame) then it gets marked !debuggable duke@435: void set_debuggable(bool debuggable) { _debuggable = debuggable; } duke@435: duke@435: public: duke@435: duke@435: bool may_be_walked(); duke@435: duke@435: // Thread local event collector setter and getter methods. duke@435: JvmtiDynamicCodeEventCollector* get_dynamic_code_event_collector() { duke@435: return _dynamic_code_event_collector; duke@435: } duke@435: JvmtiVMObjectAllocEventCollector* get_vm_object_alloc_event_collector() { duke@435: return _vm_object_alloc_event_collector; duke@435: } duke@435: void set_dynamic_code_event_collector(JvmtiDynamicCodeEventCollector* collector) { duke@435: _dynamic_code_event_collector = collector; duke@435: } duke@435: void set_vm_object_alloc_event_collector(JvmtiVMObjectAllocEventCollector* collector) { duke@435: _vm_object_alloc_event_collector = collector; duke@435: } duke@435: duke@435: duke@435: // duke@435: // Frame routines duke@435: // duke@435: duke@435: public: duke@435: duke@435: // true when the thread was suspended with a pointer to the last Java frame. duke@435: bool has_last_frame() { return _thread->has_last_Java_frame(); } duke@435: duke@435: void update_for_pop_top_frame(); duke@435: duke@435: // already holding JvmtiThreadState_lock - retrieve or create JvmtiThreadState dcubed@1044: // Can return NULL if JavaThread is exiting. duke@435: inline static JvmtiThreadState *state_for_while_locked(JavaThread *thread) { duke@435: assert(JvmtiThreadState_lock->is_locked(), "sanity check"); duke@435: duke@435: JvmtiThreadState *state = thread->jvmti_thread_state(); duke@435: if (state == NULL) { dcubed@1043: if (thread->is_exiting()) { dcubed@1043: // don't add a JvmtiThreadState to a thread that is exiting dcubed@1043: return NULL; dcubed@1043: } dcubed@1043: duke@435: state = new JvmtiThreadState(thread); duke@435: } duke@435: return state; duke@435: } duke@435: duke@435: // retrieve or create JvmtiThreadState dcubed@1044: // Can return NULL if JavaThread is exiting. duke@435: inline static JvmtiThreadState *state_for(JavaThread *thread) { duke@435: JvmtiThreadState *state = thread->jvmti_thread_state(); duke@435: if (state == NULL) { duke@435: MutexLocker mu(JvmtiThreadState_lock); duke@435: // check again with the lock held duke@435: state = state_for_while_locked(thread); duke@435: } else { duke@435: CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops()); duke@435: } duke@435: return state; duke@435: } duke@435: duke@435: // JVMTI ForceEarlyReturn support duke@435: duke@435: // This is set to earlyret_pending to signal that top Java frame duke@435: // should be returned immediately duke@435: public: duke@435: int _earlyret_state; duke@435: TosState _earlyret_tos; duke@435: jvalue _earlyret_value; duke@435: oop _earlyret_oop; // Used to return an oop result into Java code from duke@435: // ForceEarlyReturnObject, GC-preserved duke@435: duke@435: // Setting and clearing earlyret_state duke@435: // earlyret_pending indicates that a ForceEarlyReturn() has been duke@435: // requested and not yet been completed. duke@435: public: duke@435: enum EarlyretState { duke@435: earlyret_inactive = 0, duke@435: earlyret_pending = 1 duke@435: }; duke@435: duke@435: void set_earlyret_pending(void) { _earlyret_state = earlyret_pending; } duke@435: void clr_earlyret_pending(void) { _earlyret_state = earlyret_inactive; } duke@435: bool is_earlyret_pending(void) { return (_earlyret_state == earlyret_pending); } duke@435: duke@435: TosState earlyret_tos() { return _earlyret_tos; } duke@435: oop earlyret_oop() const { return _earlyret_oop; } duke@435: void set_earlyret_oop (oop x) { _earlyret_oop = x; } duke@435: jvalue earlyret_value() { return _earlyret_value; } duke@435: void set_earlyret_value(jvalue val, TosState tos) { _earlyret_tos = tos; _earlyret_value = val; } duke@435: void clr_earlyret_value() { _earlyret_tos = ilgl; _earlyret_value.j = 0L; } duke@435: duke@435: static ByteSize earlyret_state_offset() { return byte_offset_of(JvmtiThreadState, _earlyret_state); } duke@435: static ByteSize earlyret_tos_offset() { return byte_offset_of(JvmtiThreadState, _earlyret_tos); } duke@435: static ByteSize earlyret_oop_offset() { return byte_offset_of(JvmtiThreadState, _earlyret_oop); } duke@435: static ByteSize earlyret_value_offset() { return byte_offset_of(JvmtiThreadState, _earlyret_value); } duke@435: jprovino@4165: void oops_do(OopClosure* f) NOT_JVMTI_RETURN; // GC support dcubed@1648: dcubed@1648: public: dcubed@1648: void set_should_post_on_exceptions(bool val) { _thread->set_should_post_on_exceptions_flag(val ? JNI_TRUE : JNI_FALSE); } duke@435: }; duke@435: duke@435: class RedefineVerifyMark : public StackObj { duke@435: private: duke@435: JvmtiThreadState *_state; coleenp@4037: KlassHandle _scratch_class; coleenp@4037: Handle _scratch_mirror; duke@435: duke@435: public: duke@435: RedefineVerifyMark(KlassHandle *the_class, KlassHandle *scratch_class, coleenp@4037: JvmtiThreadState *state) : _state(state), _scratch_class(*scratch_class) duke@435: { duke@435: _state->set_class_versions_map(the_class, scratch_class); coleenp@4037: _scratch_mirror = Handle(_scratch_class->java_mirror()); duke@435: (*scratch_class)->set_java_mirror((*the_class)->java_mirror()); duke@435: } duke@435: duke@435: ~RedefineVerifyMark() { coleenp@4037: // Restore the scratch class's mirror, so when scratch_class is removed coleenp@4037: // the correct mirror pointing to it can be cleared. coleenp@4037: _scratch_class->set_java_mirror(_scratch_mirror()); duke@435: _state->clear_class_versions_map(); duke@435: } duke@435: }; duke@435: stefank@2314: #endif // SHARE_VM_PRIMS_JVMTITHREADSTATE_HPP