1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/prims/jvmtiThreadState.hpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,423 @@ 1.4 +/* 1.5 + * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#ifndef SHARE_VM_PRIMS_JVMTITHREADSTATE_HPP 1.29 +#define SHARE_VM_PRIMS_JVMTITHREADSTATE_HPP 1.30 + 1.31 +#include "jvmtifiles/jvmti.h" 1.32 +#include "memory/allocation.hpp" 1.33 +#include "memory/allocation.inline.hpp" 1.34 +#include "prims/jvmtiEventController.hpp" 1.35 +#include "runtime/thread.hpp" 1.36 +#include "utilities/growableArray.hpp" 1.37 + 1.38 +// 1.39 +// Forward Declarations 1.40 +// 1.41 + 1.42 +class JvmtiEnvBase; 1.43 +class JvmtiEnvThreadState; 1.44 +class JvmtiDynamicCodeEventCollector; 1.45 + 1.46 +enum JvmtiClassLoadKind { 1.47 + jvmti_class_load_kind_load = 100, 1.48 + jvmti_class_load_kind_retransform, 1.49 + jvmti_class_load_kind_redefine 1.50 +}; 1.51 + 1.52 +/////////////////////////////////////////////////////////////// 1.53 +// 1.54 +// class JvmtiEnvThreadStateIterator 1.55 +// 1.56 +// The only safe means of iterating through the JvmtiEnvThreadStates 1.57 +// in a JvmtiThreadState. 1.58 +// Note that this iteratation includes invalid environments pending 1.59 +// deallocation -- in fact, some uses depend on this behavior. 1.60 +// 1.61 +class JvmtiEnvThreadStateIterator : public StackObj { 1.62 + private: 1.63 + JvmtiThreadState* state; 1.64 + public: 1.65 + JvmtiEnvThreadStateIterator(JvmtiThreadState* thread_state); 1.66 + ~JvmtiEnvThreadStateIterator(); 1.67 + JvmtiEnvThreadState* first(); 1.68 + JvmtiEnvThreadState* next(JvmtiEnvThreadState* ets); 1.69 +}; 1.70 + 1.71 + 1.72 +/////////////////////////////////////////////////////////////// 1.73 +// 1.74 +// class JvmtiThreadState 1.75 +// 1.76 +// The Jvmti state for each thread (across all JvmtiEnv): 1.77 +// 1. Local table of enabled events. 1.78 +class JvmtiThreadState : public CHeapObj<mtInternal> { 1.79 + private: 1.80 + friend class JvmtiEnv; 1.81 + JavaThread *_thread; 1.82 + bool _exception_detected; 1.83 + bool _exception_caught; 1.84 + bool _hide_single_stepping; 1.85 + bool _pending_step_for_popframe; 1.86 + bool _pending_step_for_earlyret; 1.87 + int _hide_level; 1.88 + 1.89 + // Used to send class being redefined/retransformed and kind of transform 1.90 + // info to the class file load hook event handler. 1.91 + KlassHandle *_class_being_redefined; 1.92 + JvmtiClassLoadKind _class_load_kind; 1.93 + 1.94 + // This is only valid when is_interp_only_mode() returns true 1.95 + int _cur_stack_depth; 1.96 + 1.97 + JvmtiThreadEventEnable _thread_event_enable; 1.98 + 1.99 + // for support of JvmtiEnvThreadState 1.100 + JvmtiEnvThreadState* _head_env_thread_state; 1.101 + 1.102 + // doubly-linked linear list of active thread state 1.103 + // needed in order to iterate the list without holding Threads_lock 1.104 + static JvmtiThreadState *_head; 1.105 + JvmtiThreadState *_next; 1.106 + JvmtiThreadState *_prev; 1.107 + 1.108 + // holds the current dynamic code event collector, NULL if no event collector in use 1.109 + JvmtiDynamicCodeEventCollector* _dynamic_code_event_collector; 1.110 + // holds the current vm object alloc event collector, NULL if no event collector in use 1.111 + JvmtiVMObjectAllocEventCollector* _vm_object_alloc_event_collector; 1.112 + 1.113 + // Should only be created by factory methods 1.114 + JvmtiThreadState(JavaThread *thread); 1.115 + 1.116 + friend class JvmtiEnvThreadStateIterator; 1.117 + inline JvmtiEnvThreadState* head_env_thread_state(); 1.118 + inline void set_head_env_thread_state(JvmtiEnvThreadState* ets); 1.119 + 1.120 + public: 1.121 + ~JvmtiThreadState(); 1.122 + 1.123 + // is event_type enabled and usable for this thread in any enviroments? 1.124 + bool is_enabled(jvmtiEvent event_type) { 1.125 + return _thread_event_enable.is_enabled(event_type); 1.126 + } 1.127 + 1.128 + JvmtiThreadEventEnable *thread_event_enable() { 1.129 + return &_thread_event_enable; 1.130 + } 1.131 + 1.132 + // Must only be called in situations where the state is for the current thread and 1.133 + // the environment can not go away. To be safe, the returned JvmtiEnvThreadState 1.134 + // must be used in such a way as there can be no intervening safepoints. 1.135 + inline JvmtiEnvThreadState* env_thread_state(JvmtiEnvBase *env); 1.136 + 1.137 + static void periodic_clean_up(); 1.138 + 1.139 + void add_env(JvmtiEnvBase *env); 1.140 + 1.141 + // Used by the interpreter for fullspeed debugging support 1.142 + bool is_interp_only_mode() { return _thread->is_interp_only_mode(); } 1.143 + void enter_interp_only_mode(); 1.144 + void leave_interp_only_mode(); 1.145 + 1.146 + // access to the linked list of all JVMTI thread states 1.147 + static JvmtiThreadState *first() { 1.148 + assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check"); 1.149 + return _head; 1.150 + } 1.151 + 1.152 + JvmtiThreadState *next() { 1.153 + return _next; 1.154 + } 1.155 + 1.156 + // Current stack depth is only valid when is_interp_only_mode() returns true. 1.157 + // These functions should only be called at a safepoint - usually called from same thread. 1.158 + // Returns the number of Java activations on the stack. 1.159 + int cur_stack_depth(); 1.160 + void invalidate_cur_stack_depth(); 1.161 + void incr_cur_stack_depth(); 1.162 + void decr_cur_stack_depth(); 1.163 + 1.164 + int count_frames(); 1.165 + 1.166 + inline JavaThread *get_thread() { return _thread; } 1.167 + inline bool is_exception_detected() { return _exception_detected; } 1.168 + inline bool is_exception_caught() { return _exception_caught; } 1.169 + inline void set_exception_detected() { _exception_detected = true; 1.170 + _exception_caught = false; } 1.171 + inline void clear_exception_detected() { 1.172 + _exception_detected = false; 1.173 + assert(_exception_caught == false, "_exception_caught is out of phase"); 1.174 + } 1.175 + inline void set_exception_caught() { _exception_caught = true; 1.176 + _exception_detected = false; } 1.177 + 1.178 + inline void clear_hide_single_stepping() { 1.179 + if (_hide_level > 0) { 1.180 + _hide_level--; 1.181 + } else { 1.182 + assert(_hide_single_stepping, "hide_single_stepping is out of phase"); 1.183 + _hide_single_stepping = false; 1.184 + } 1.185 + } 1.186 + inline bool hide_single_stepping() { return _hide_single_stepping; } 1.187 + inline void set_hide_single_stepping() { 1.188 + if (_hide_single_stepping) { 1.189 + _hide_level++; 1.190 + } else { 1.191 + assert(_hide_level == 0, "hide_level is out of phase"); 1.192 + _hide_single_stepping = true; 1.193 + } 1.194 + } 1.195 + 1.196 + // Step pending flag is set when PopFrame is called and it is cleared 1.197 + // when step for the Pop Frame is completed. 1.198 + // This logic is used to distinguish b/w step for pop frame and repeat step. 1.199 + void set_pending_step_for_popframe() { _pending_step_for_popframe = true; } 1.200 + void clr_pending_step_for_popframe() { _pending_step_for_popframe = false; } 1.201 + bool is_pending_step_for_popframe() { return _pending_step_for_popframe; } 1.202 + void process_pending_step_for_popframe(); 1.203 + 1.204 + // Step pending flag is set when ForceEarlyReturn is called and it is cleared 1.205 + // when step for the ForceEarlyReturn is completed. 1.206 + // This logic is used to distinguish b/w step for early return and repeat step. 1.207 + void set_pending_step_for_earlyret() { _pending_step_for_earlyret = true; } 1.208 + void clr_pending_step_for_earlyret() { _pending_step_for_earlyret = false; } 1.209 + bool is_pending_step_for_earlyret() { return _pending_step_for_earlyret; } 1.210 + void process_pending_step_for_earlyret(); 1.211 + 1.212 + // Setter and getter method is used to send redefined class info 1.213 + // when class file load hook event is posted. 1.214 + // It is set while loading redefined class and cleared before the 1.215 + // class file load hook event is posted. 1.216 + inline void set_class_being_redefined(KlassHandle *h_class, JvmtiClassLoadKind kind) { 1.217 + _class_being_redefined = h_class; 1.218 + _class_load_kind = kind; 1.219 + } 1.220 + 1.221 + inline void clear_class_being_redefined() { 1.222 + _class_being_redefined = NULL; 1.223 + _class_load_kind = jvmti_class_load_kind_load; 1.224 + } 1.225 + 1.226 + inline KlassHandle *get_class_being_redefined() { 1.227 + return _class_being_redefined; 1.228 + } 1.229 + 1.230 + inline JvmtiClassLoadKind get_class_load_kind() { 1.231 + return _class_load_kind; 1.232 + } 1.233 + 1.234 + // RedefineClasses support 1.235 + // The bug 6214132 caused the verification to fail. 1.236 + // 1.237 + // Below is the detailed description of the fix approach taken: 1.238 + // 1. What's done in RedefineClasses() before verification: 1.239 + // a) A reference to the class being redefined (_the_class) and a 1.240 + // reference to new version of the class (_scratch_class) are 1.241 + // saved here for use during the bytecode verification phase of 1.242 + // RedefineClasses. See RedefineVerifyMark for how these fields 1.243 + // are managed. 1.244 + // b) The _java_mirror field from _the_class is copied to the 1.245 + // _java_mirror field in _scratch_class. This means that a jclass 1.246 + // returned for _the_class or _scratch_class will refer to the 1.247 + // same Java mirror. The verifier will see the "one true mirror" 1.248 + // for the class being verified. 1.249 + // 2. What is done at verification: 1.250 + // When the verifier makes calls into the VM to ask questions about 1.251 + // the class being verified, it will pass the jclass to JVM_* functions. 1.252 + // The jclass is always pointing to the mirror of _the_class. 1.253 + // ~28 JVM_* functions called by the verifier for the information 1.254 + // about CP entries and klass structure should check the jvmtiThreadState 1.255 + // info about equivalent klass versions and use it to replace a Klass* 1.256 + // of _the_class with a Klass* of _scratch_class. The function 1.257 + // class_to_verify_considering_redefinition() must be called for it. 1.258 + // 1.259 + // Note again, that this redirection happens only for the verifier thread. 1.260 + // Other threads have very small overhead by checking the existence 1.261 + // of the jvmtiThreadSate and the information about klasses equivalence. 1.262 + // No JNI functions need to be changed, they don't reference the klass guts. 1.263 + // The JavaThread pointer is already available in all JVM_* functions 1.264 + // used by the verifier, so there is no extra performance issue with it. 1.265 + 1.266 + private: 1.267 + KlassHandle *_the_class_for_redefinition_verification; 1.268 + KlassHandle *_scratch_class_for_redefinition_verification; 1.269 + 1.270 + public: 1.271 + inline void set_class_versions_map(KlassHandle *the_class, 1.272 + KlassHandle *scratch_class) { 1.273 + _the_class_for_redefinition_verification = the_class; 1.274 + _scratch_class_for_redefinition_verification = scratch_class; 1.275 + } 1.276 + 1.277 + inline void clear_class_versions_map() { set_class_versions_map(NULL, NULL); } 1.278 + 1.279 + static inline 1.280 + Klass* class_to_verify_considering_redefinition(Klass* klass, 1.281 + JavaThread *thread) { 1.282 + JvmtiThreadState *state = thread->jvmti_thread_state(); 1.283 + if (state != NULL && state->_the_class_for_redefinition_verification != NULL) { 1.284 + if ((*(state->_the_class_for_redefinition_verification))() == klass) { 1.285 + klass = (*(state->_scratch_class_for_redefinition_verification))(); 1.286 + } 1.287 + } 1.288 + return klass; 1.289 + } 1.290 + 1.291 + // Todo: get rid of this! 1.292 + private: 1.293 + bool _debuggable; 1.294 + public: 1.295 + // Should the thread be enumerated by jvmtiInternal::GetAllThreads? 1.296 + bool is_debuggable() { return _debuggable; } 1.297 + // If a thread cannot be suspended (has no valid last_java_frame) then it gets marked !debuggable 1.298 + void set_debuggable(bool debuggable) { _debuggable = debuggable; } 1.299 + 1.300 + public: 1.301 + 1.302 + bool may_be_walked(); 1.303 + 1.304 + // Thread local event collector setter and getter methods. 1.305 + JvmtiDynamicCodeEventCollector* get_dynamic_code_event_collector() { 1.306 + return _dynamic_code_event_collector; 1.307 + } 1.308 + JvmtiVMObjectAllocEventCollector* get_vm_object_alloc_event_collector() { 1.309 + return _vm_object_alloc_event_collector; 1.310 + } 1.311 + void set_dynamic_code_event_collector(JvmtiDynamicCodeEventCollector* collector) { 1.312 + _dynamic_code_event_collector = collector; 1.313 + } 1.314 + void set_vm_object_alloc_event_collector(JvmtiVMObjectAllocEventCollector* collector) { 1.315 + _vm_object_alloc_event_collector = collector; 1.316 + } 1.317 + 1.318 + 1.319 + // 1.320 + // Frame routines 1.321 + // 1.322 + 1.323 + public: 1.324 + 1.325 + // true when the thread was suspended with a pointer to the last Java frame. 1.326 + bool has_last_frame() { return _thread->has_last_Java_frame(); } 1.327 + 1.328 + void update_for_pop_top_frame(); 1.329 + 1.330 + // already holding JvmtiThreadState_lock - retrieve or create JvmtiThreadState 1.331 + // Can return NULL if JavaThread is exiting. 1.332 + inline static JvmtiThreadState *state_for_while_locked(JavaThread *thread) { 1.333 + assert(JvmtiThreadState_lock->is_locked(), "sanity check"); 1.334 + 1.335 + JvmtiThreadState *state = thread->jvmti_thread_state(); 1.336 + if (state == NULL) { 1.337 + if (thread->is_exiting()) { 1.338 + // don't add a JvmtiThreadState to a thread that is exiting 1.339 + return NULL; 1.340 + } 1.341 + 1.342 + state = new JvmtiThreadState(thread); 1.343 + } 1.344 + return state; 1.345 + } 1.346 + 1.347 + // retrieve or create JvmtiThreadState 1.348 + // Can return NULL if JavaThread is exiting. 1.349 + inline static JvmtiThreadState *state_for(JavaThread *thread) { 1.350 + JvmtiThreadState *state = thread->jvmti_thread_state(); 1.351 + if (state == NULL) { 1.352 + MutexLocker mu(JvmtiThreadState_lock); 1.353 + // check again with the lock held 1.354 + state = state_for_while_locked(thread); 1.355 + } else { 1.356 + CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops()); 1.357 + } 1.358 + return state; 1.359 + } 1.360 + 1.361 + // JVMTI ForceEarlyReturn support 1.362 + 1.363 + // This is set to earlyret_pending to signal that top Java frame 1.364 + // should be returned immediately 1.365 + public: 1.366 + int _earlyret_state; 1.367 + TosState _earlyret_tos; 1.368 + jvalue _earlyret_value; 1.369 + oop _earlyret_oop; // Used to return an oop result into Java code from 1.370 + // ForceEarlyReturnObject, GC-preserved 1.371 + 1.372 + // Setting and clearing earlyret_state 1.373 + // earlyret_pending indicates that a ForceEarlyReturn() has been 1.374 + // requested and not yet been completed. 1.375 + public: 1.376 + enum EarlyretState { 1.377 + earlyret_inactive = 0, 1.378 + earlyret_pending = 1 1.379 + }; 1.380 + 1.381 + void set_earlyret_pending(void) { _earlyret_state = earlyret_pending; } 1.382 + void clr_earlyret_pending(void) { _earlyret_state = earlyret_inactive; } 1.383 + bool is_earlyret_pending(void) { return (_earlyret_state == earlyret_pending); } 1.384 + 1.385 + TosState earlyret_tos() { return _earlyret_tos; } 1.386 + oop earlyret_oop() const { return _earlyret_oop; } 1.387 + void set_earlyret_oop (oop x) { _earlyret_oop = x; } 1.388 + jvalue earlyret_value() { return _earlyret_value; } 1.389 + void set_earlyret_value(jvalue val, TosState tos) { _earlyret_tos = tos; _earlyret_value = val; } 1.390 + void clr_earlyret_value() { _earlyret_tos = ilgl; _earlyret_value.j = 0L; } 1.391 + 1.392 + static ByteSize earlyret_state_offset() { return byte_offset_of(JvmtiThreadState, _earlyret_state); } 1.393 + static ByteSize earlyret_tos_offset() { return byte_offset_of(JvmtiThreadState, _earlyret_tos); } 1.394 + static ByteSize earlyret_oop_offset() { return byte_offset_of(JvmtiThreadState, _earlyret_oop); } 1.395 + static ByteSize earlyret_value_offset() { return byte_offset_of(JvmtiThreadState, _earlyret_value); } 1.396 + 1.397 + void oops_do(OopClosure* f) NOT_JVMTI_RETURN; // GC support 1.398 + 1.399 +public: 1.400 + void set_should_post_on_exceptions(bool val) { _thread->set_should_post_on_exceptions_flag(val ? JNI_TRUE : JNI_FALSE); } 1.401 +}; 1.402 + 1.403 +class RedefineVerifyMark : public StackObj { 1.404 + private: 1.405 + JvmtiThreadState *_state; 1.406 + KlassHandle _scratch_class; 1.407 + Handle _scratch_mirror; 1.408 + 1.409 + public: 1.410 + RedefineVerifyMark(KlassHandle *the_class, KlassHandle *scratch_class, 1.411 + JvmtiThreadState *state) : _state(state), _scratch_class(*scratch_class) 1.412 + { 1.413 + _state->set_class_versions_map(the_class, scratch_class); 1.414 + _scratch_mirror = Handle(_scratch_class->java_mirror()); 1.415 + (*scratch_class)->set_java_mirror((*the_class)->java_mirror()); 1.416 + } 1.417 + 1.418 + ~RedefineVerifyMark() { 1.419 + // Restore the scratch class's mirror, so when scratch_class is removed 1.420 + // the correct mirror pointing to it can be cleared. 1.421 + _scratch_class->set_java_mirror(_scratch_mirror()); 1.422 + _state->clear_class_versions_map(); 1.423 + } 1.424 +}; 1.425 + 1.426 +#endif // SHARE_VM_PRIMS_JVMTITHREADSTATE_HPP