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