1 /* 2 * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_PRIMS_JVMTITHREADSTATE_HPP 26 #define SHARE_PRIMS_JVMTITHREADSTATE_HPP 27 28 #include "jvmtifiles/jvmti.h" 29 #include "memory/allocation.hpp" 30 #include "prims/jvmtiEventController.hpp" 31 #include "runtime/thread.hpp" 32 #include "utilities/growableArray.hpp" 33 34 // 35 // Forward Declarations 36 // 37 38 class JvmtiEnvBase; 39 class JvmtiEnvThreadState; 40 class JvmtiDynamicCodeEventCollector; 41 42 class JvmtiDeferredEvent; 43 class JvmtiDeferredEventQueue; 44 45 enum JvmtiClassLoadKind { 46 jvmti_class_load_kind_load = 100, 47 jvmti_class_load_kind_retransform, 48 jvmti_class_load_kind_redefine 49 }; 50 51 /////////////////////////////////////////////////////////////// 52 // 53 // class JvmtiEnvThreadStateIterator 54 // 55 // The only safe means of iterating through the JvmtiEnvThreadStates 56 // in a JvmtiThreadState. 57 // Note that this iteratation includes invalid environments pending 58 // deallocation -- in fact, some uses depend on this behavior. 59 // 60 class JvmtiEnvThreadStateIterator : public StackObj { 61 private: 62 JvmtiThreadState* state; 63 public: 64 JvmtiEnvThreadStateIterator(JvmtiThreadState* thread_state); 65 ~JvmtiEnvThreadStateIterator(); 66 JvmtiEnvThreadState* first(); 67 JvmtiEnvThreadState* next(JvmtiEnvThreadState* ets); 68 }; 69 70 71 /////////////////////////////////////////////////////////////// 72 // 73 // class JvmtiThreadState 74 // 75 // The Jvmti state for each thread (across all JvmtiEnv): 76 // 1. Local table of enabled events. 77 class JvmtiThreadState : public CHeapObj<mtInternal> { 78 private: 79 friend class JvmtiEnv; 80 JavaThread *_thread; 81 // Jvmti Events that cannot be posted in their current context. 82 JvmtiDeferredEventQueue* _jvmti_event_queue; 83 bool _hide_single_stepping; 84 bool _pending_step_for_popframe; 85 bool _pending_step_for_earlyret; 86 int _hide_level; 87 88 public: 89 enum ExceptionState { 90 ES_CLEARED, 91 ES_DETECTED, 92 ES_CAUGHT 93 }; 94 95 private: 96 ExceptionState _exception_state; 97 98 // Used to send class being redefined/retransformed and kind of transform 99 // info to the class file load hook event handler. 100 Klass* _class_being_redefined; 101 JvmtiClassLoadKind _class_load_kind; 102 103 // This is only valid when is_interp_only_mode() returns true 104 int _cur_stack_depth; 105 106 JvmtiThreadEventEnable _thread_event_enable; 107 108 // for support of JvmtiEnvThreadState 109 JvmtiEnvThreadState* _head_env_thread_state; 110 111 // doubly-linked linear list of active thread state 112 // needed in order to iterate the list without holding Threads_lock 113 static JvmtiThreadState *_head; 114 JvmtiThreadState *_next; 115 JvmtiThreadState *_prev; 116 117 // holds the current dynamic code event collector, NULL if no event collector in use 118 JvmtiDynamicCodeEventCollector* _dynamic_code_event_collector; 119 // holds the current vm object alloc event collector, NULL if no event collector in use 120 JvmtiVMObjectAllocEventCollector* _vm_object_alloc_event_collector; 121 // holds the current sampled object alloc event collector, NULL if no event collector in use 122 JvmtiSampledObjectAllocEventCollector* _sampled_object_alloc_event_collector; 123 124 // Should only be created by factory methods 125 JvmtiThreadState(JavaThread *thread); 126 127 friend class JvmtiEnvThreadStateIterator; 128 inline JvmtiEnvThreadState* head_env_thread_state(); 129 inline void set_head_env_thread_state(JvmtiEnvThreadState* ets); 130 131 public: 132 ~JvmtiThreadState(); 133 134 // is event_type enabled and usable for this thread in any enviroments? is_enabled(jvmtiEvent event_type)135 bool is_enabled(jvmtiEvent event_type) { 136 return _thread_event_enable.is_enabled(event_type); 137 } 138 thread_event_enable()139 JvmtiThreadEventEnable *thread_event_enable() { 140 return &_thread_event_enable; 141 } 142 143 // Must only be called in situations where the state is for the current thread and 144 // the environment can not go away. To be safe, the returned JvmtiEnvThreadState 145 // must be used in such a way as there can be no intervening safepoints. 146 inline JvmtiEnvThreadState* env_thread_state(JvmtiEnvBase *env); 147 148 static void periodic_clean_up(); 149 150 void add_env(JvmtiEnvBase *env); 151 152 // Used by the interpreter for fullspeed debugging support is_interp_only_mode()153 bool is_interp_only_mode() { return _thread->is_interp_only_mode(); } 154 void enter_interp_only_mode(); 155 void leave_interp_only_mode(); 156 157 // access to the linked list of all JVMTI thread states first()158 static JvmtiThreadState *first() { 159 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check"); 160 return _head; 161 } 162 next()163 JvmtiThreadState *next() { 164 return _next; 165 } 166 167 // Current stack depth is only valid when is_interp_only_mode() returns true. 168 // These functions should only be called at a safepoint - usually called from same thread. 169 // Returns the number of Java activations on the stack. 170 int cur_stack_depth(); 171 void invalidate_cur_stack_depth(); 172 void incr_cur_stack_depth(); 173 void decr_cur_stack_depth(); 174 175 int count_frames(); 176 get_thread()177 inline JavaThread *get_thread() { return _thread; } 178 is_exception_detected()179 inline bool is_exception_detected() { return _exception_state == ES_DETECTED; } is_exception_caught()180 inline bool is_exception_caught() { return _exception_state == ES_CAUGHT; } 181 set_exception_detected()182 inline void set_exception_detected() { _exception_state = ES_DETECTED; } set_exception_caught()183 inline void set_exception_caught() { _exception_state = ES_CAUGHT; } 184 clear_exception_state()185 inline void clear_exception_state() { _exception_state = ES_CLEARED; } 186 187 // We need to save and restore exception state inside JvmtiEventMark get_exception_state()188 inline ExceptionState get_exception_state() { return _exception_state; } restore_exception_state(ExceptionState state)189 inline void restore_exception_state(ExceptionState state) { _exception_state = state; } 190 clear_hide_single_stepping()191 inline void clear_hide_single_stepping() { 192 if (_hide_level > 0) { 193 _hide_level--; 194 } else { 195 assert(_hide_single_stepping, "hide_single_stepping is out of phase"); 196 _hide_single_stepping = false; 197 } 198 } hide_single_stepping()199 inline bool hide_single_stepping() { return _hide_single_stepping; } set_hide_single_stepping()200 inline void set_hide_single_stepping() { 201 if (_hide_single_stepping) { 202 _hide_level++; 203 } else { 204 assert(_hide_level == 0, "hide_level is out of phase"); 205 _hide_single_stepping = true; 206 } 207 } 208 209 // Step pending flag is set when PopFrame is called and it is cleared 210 // when step for the Pop Frame is completed. 211 // This logic is used to distinguish b/w step for pop frame and repeat step. set_pending_step_for_popframe()212 void set_pending_step_for_popframe() { _pending_step_for_popframe = true; } clr_pending_step_for_popframe()213 void clr_pending_step_for_popframe() { _pending_step_for_popframe = false; } is_pending_step_for_popframe()214 bool is_pending_step_for_popframe() { return _pending_step_for_popframe; } 215 void process_pending_step_for_popframe(); 216 217 // Step pending flag is set when ForceEarlyReturn is called and it is cleared 218 // when step for the ForceEarlyReturn is completed. 219 // This logic is used to distinguish b/w step for early return and repeat step. set_pending_step_for_earlyret()220 void set_pending_step_for_earlyret() { _pending_step_for_earlyret = true; } clr_pending_step_for_earlyret()221 void clr_pending_step_for_earlyret() { _pending_step_for_earlyret = false; } is_pending_step_for_earlyret()222 bool is_pending_step_for_earlyret() { return _pending_step_for_earlyret; } 223 void process_pending_step_for_earlyret(); 224 225 // Setter and getter method is used to send redefined class info 226 // when class file load hook event is posted. 227 // It is set while loading redefined class and cleared before the 228 // class file load hook event is posted. set_class_being_redefined(Klass * k,JvmtiClassLoadKind kind)229 inline void set_class_being_redefined(Klass* k, JvmtiClassLoadKind kind) { 230 _class_being_redefined = k; 231 _class_load_kind = kind; 232 } 233 clear_class_being_redefined()234 inline void clear_class_being_redefined() { 235 _class_being_redefined = NULL; 236 _class_load_kind = jvmti_class_load_kind_load; 237 } 238 get_class_being_redefined()239 inline Klass* get_class_being_redefined() { 240 return _class_being_redefined; 241 } 242 get_class_load_kind()243 inline JvmtiClassLoadKind get_class_load_kind() { 244 return _class_load_kind; 245 } 246 247 // RedefineClasses support 248 // The bug 6214132 caused the verification to fail. 249 // 250 // Below is the detailed description of the fix approach taken: 251 // 1. What's done in RedefineClasses() before verification: 252 // a) A reference to the class being redefined (_the_class) and a 253 // reference to new version of the class (_scratch_class) are 254 // saved here for use during the bytecode verification phase of 255 // RedefineClasses. See RedefineVerifyMark for how these fields 256 // are managed. 257 // b) The _java_mirror field from _the_class is copied to the 258 // _java_mirror field in _scratch_class. This means that a jclass 259 // returned for _the_class or _scratch_class will refer to the 260 // same Java mirror. The verifier will see the "one true mirror" 261 // for the class being verified. 262 // 2. What is done at verification: 263 // When the verifier makes calls into the VM to ask questions about 264 // the class being verified, it will pass the jclass to JVM_* functions. 265 // The jclass is always pointing to the mirror of _the_class. 266 // ~28 JVM_* functions called by the verifier for the information 267 // about CP entries and klass structure should check the jvmtiThreadState 268 // info about equivalent klass versions and use it to replace a Klass* 269 // of _the_class with a Klass* of _scratch_class. The function 270 // class_to_verify_considering_redefinition() must be called for it. 271 // 272 // Note again, that this redirection happens only for the verifier thread. 273 // Other threads have very small overhead by checking the existence 274 // of the jvmtiThreadSate and the information about klasses equivalence. 275 // No JNI functions need to be changed, they don't reference the klass guts. 276 // The JavaThread pointer is already available in all JVM_* functions 277 // used by the verifier, so there is no extra performance issue with it. 278 279 private: 280 Klass* _the_class_for_redefinition_verification; 281 Klass* _scratch_class_for_redefinition_verification; 282 283 public: set_class_versions_map(Klass * the_class,Klass * scratch_class)284 inline void set_class_versions_map(Klass* the_class, 285 Klass* scratch_class) { 286 _the_class_for_redefinition_verification = the_class; 287 _scratch_class_for_redefinition_verification = scratch_class; 288 } 289 clear_class_versions_map()290 inline void clear_class_versions_map() { set_class_versions_map(NULL, NULL); } 291 292 static inline class_to_verify_considering_redefinition(Klass * klass,JavaThread * thread)293 Klass* class_to_verify_considering_redefinition(Klass* klass, 294 JavaThread *thread) { 295 JvmtiThreadState *state = thread->jvmti_thread_state(); 296 if (state != NULL && state->_the_class_for_redefinition_verification != NULL) { 297 if (state->_the_class_for_redefinition_verification == klass) { 298 klass = state->_scratch_class_for_redefinition_verification; 299 } 300 } 301 return klass; 302 } 303 304 // Todo: get rid of this! 305 private: 306 bool _debuggable; 307 public: 308 // Should the thread be enumerated by jvmtiInternal::GetAllThreads? is_debuggable()309 bool is_debuggable() { return _debuggable; } 310 // If a thread cannot be suspended (has no valid last_java_frame) then it gets marked !debuggable set_debuggable(bool debuggable)311 void set_debuggable(bool debuggable) { _debuggable = debuggable; } 312 313 public: 314 315 bool may_be_walked(); 316 317 // Thread local event collector setter and getter methods. get_dynamic_code_event_collector()318 JvmtiDynamicCodeEventCollector* get_dynamic_code_event_collector() { 319 return _dynamic_code_event_collector; 320 } get_vm_object_alloc_event_collector()321 JvmtiVMObjectAllocEventCollector* get_vm_object_alloc_event_collector() { 322 return _vm_object_alloc_event_collector; 323 } get_sampled_object_alloc_event_collector()324 JvmtiSampledObjectAllocEventCollector* get_sampled_object_alloc_event_collector() { 325 return _sampled_object_alloc_event_collector; 326 } set_dynamic_code_event_collector(JvmtiDynamicCodeEventCollector * collector)327 void set_dynamic_code_event_collector(JvmtiDynamicCodeEventCollector* collector) { 328 _dynamic_code_event_collector = collector; 329 } set_vm_object_alloc_event_collector(JvmtiVMObjectAllocEventCollector * collector)330 void set_vm_object_alloc_event_collector(JvmtiVMObjectAllocEventCollector* collector) { 331 _vm_object_alloc_event_collector = collector; 332 } set_sampled_object_alloc_event_collector(JvmtiSampledObjectAllocEventCollector * collector)333 void set_sampled_object_alloc_event_collector(JvmtiSampledObjectAllocEventCollector* collector) { 334 _sampled_object_alloc_event_collector = collector; 335 } 336 337 338 // 339 // Frame routines 340 // 341 342 public: 343 344 // true when the thread was suspended with a pointer to the last Java frame. has_last_frame()345 bool has_last_frame() { return _thread->has_last_Java_frame(); } 346 347 void update_for_pop_top_frame(); 348 349 // already holding JvmtiThreadState_lock - retrieve or create JvmtiThreadState 350 // Can return NULL if JavaThread is exiting. 351 static JvmtiThreadState *state_for_while_locked(JavaThread *thread); 352 // retrieve or create JvmtiThreadState 353 // Can return NULL if JavaThread is exiting. 354 static JvmtiThreadState *state_for(JavaThread *thread); 355 356 // JVMTI ForceEarlyReturn support 357 358 // This is set to earlyret_pending to signal that top Java frame 359 // should be returned immediately 360 public: 361 int _earlyret_state; 362 TosState _earlyret_tos; 363 jvalue _earlyret_value; 364 oop _earlyret_oop; // Used to return an oop result into Java code from 365 // ForceEarlyReturnObject, GC-preserved 366 367 // Setting and clearing earlyret_state 368 // earlyret_pending indicates that a ForceEarlyReturn() has been 369 // requested and not yet been completed. 370 public: 371 enum EarlyretState { 372 earlyret_inactive = 0, 373 earlyret_pending = 1 374 }; 375 set_earlyret_pending(void)376 void set_earlyret_pending(void) { _earlyret_state = earlyret_pending; } clr_earlyret_pending(void)377 void clr_earlyret_pending(void) { _earlyret_state = earlyret_inactive; } is_earlyret_pending(void)378 bool is_earlyret_pending(void) { return (_earlyret_state == earlyret_pending); } 379 earlyret_tos()380 TosState earlyret_tos() { return _earlyret_tos; } earlyret_oop() const381 oop earlyret_oop() const { return _earlyret_oop; } set_earlyret_oop(oop x)382 void set_earlyret_oop (oop x) { _earlyret_oop = x; } earlyret_value()383 jvalue earlyret_value() { return _earlyret_value; } set_earlyret_value(jvalue val,TosState tos)384 void set_earlyret_value(jvalue val, TosState tos) { _earlyret_tos = tos; _earlyret_value = val; } clr_earlyret_value()385 void clr_earlyret_value() { _earlyret_tos = ilgl; _earlyret_value.j = 0L; } 386 earlyret_state_offset()387 static ByteSize earlyret_state_offset() { return byte_offset_of(JvmtiThreadState, _earlyret_state); } earlyret_tos_offset()388 static ByteSize earlyret_tos_offset() { return byte_offset_of(JvmtiThreadState, _earlyret_tos); } earlyret_oop_offset()389 static ByteSize earlyret_oop_offset() { return byte_offset_of(JvmtiThreadState, _earlyret_oop); } earlyret_value_offset()390 static ByteSize earlyret_value_offset() { return byte_offset_of(JvmtiThreadState, _earlyret_value); } 391 392 void oops_do(OopClosure* f, CodeBlobClosure* cf) NOT_JVMTI_RETURN; // GC support 393 void nmethods_do(CodeBlobClosure* cf) NOT_JVMTI_RETURN; 394 395 public: set_should_post_on_exceptions(bool val)396 void set_should_post_on_exceptions(bool val) { _thread->set_should_post_on_exceptions_flag(val ? JNI_TRUE : JNI_FALSE); } 397 398 // Thread local event queue, which doesn't require taking the Service_lock. 399 void enqueue_event(JvmtiDeferredEvent* event) NOT_JVMTI_RETURN; 400 void post_events(JvmtiEnv* env); 401 void run_nmethod_entry_barriers(); 402 }; 403 404 class RedefineVerifyMark : public StackObj { 405 private: 406 JvmtiThreadState* _state; 407 Klass* _scratch_class; 408 OopHandle _scratch_mirror; 409 410 public: RedefineVerifyMark(Klass * the_class,Klass * scratch_class,JvmtiThreadState * state)411 RedefineVerifyMark(Klass* the_class, Klass* scratch_class, 412 JvmtiThreadState *state) : _state(state), _scratch_class(scratch_class) 413 { 414 _state->set_class_versions_map(the_class, scratch_class); 415 _scratch_mirror = _scratch_class->java_mirror_handle(); 416 _scratch_class->set_java_mirror_handle(the_class->java_mirror_handle()); 417 } 418 ~RedefineVerifyMark()419 ~RedefineVerifyMark() { 420 // Restore the scratch class's mirror, so when scratch_class is removed 421 // the correct mirror pointing to it can be cleared. 422 _scratch_class->set_java_mirror_handle(_scratch_mirror); 423 _state->clear_class_versions_map(); 424 } 425 }; 426 427 #endif // SHARE_PRIMS_JVMTITHREADSTATE_HPP 428