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