1 /* 2 * Copyright (c) 2003, 2024, 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 "oops/oopHandle.hpp" 31 #include "prims/jvmtiEventController.hpp" 32 #include "prims/jvmtiExport.hpp" 33 #include "runtime/javaThread.hpp" 34 #include "runtime/mutexLocker.hpp" 35 #include "runtime/threads.hpp" 36 #include "utilities/growableArray.hpp" 37 38 // 39 // Forward Declarations 40 // 41 42 class JvmtiEnvBase; 43 class JvmtiEnvThreadState; 44 class JvmtiDynamicCodeEventCollector; 45 46 class JvmtiDeferredEvent; 47 class JvmtiDeferredEventQueue; 48 49 enum JvmtiClassLoadKind { 50 jvmti_class_load_kind_load = 100, 51 jvmti_class_load_kind_retransform, 52 jvmti_class_load_kind_redefine 53 }; 54 55 /////////////////////////////////////////////////////////////// 56 // 57 // class JvmtiEnvThreadStateIterator 58 // 59 // The only safe means of iterating through the JvmtiEnvThreadStates 60 // in a JvmtiThreadState. 61 // Note that this iteratation includes invalid environments pending 62 // deallocation -- in fact, some uses depend on this behavior. 63 // 64 class JvmtiEnvThreadStateIterator : public StackObj { 65 private: 66 JvmtiThreadState* state; 67 public: 68 JvmtiEnvThreadStateIterator(JvmtiThreadState* thread_state); 69 ~JvmtiEnvThreadStateIterator(); 70 JvmtiEnvThreadState* first(); 71 JvmtiEnvThreadState* next(JvmtiEnvThreadState* ets); 72 }; 73 74 /////////////////////////////////////////////////////////////// 75 // 76 // class JvmtiVTMSTransitionDisabler 77 // 78 // Virtual Thread Mount State Transition (VTMS transition) mechanism 79 // 80 class JvmtiVTMSTransitionDisabler : public AnyObj { 81 private: 82 static volatile int _VTMS_transition_disable_for_one_count; // transitions for one virtual thread are disabled while it is positive 83 static volatile int _VTMS_transition_disable_for_all_count; // transitions for all virtual threads are disabled while it is positive 84 static volatile bool _SR_mode; // there is an active suspender or resumer 85 static volatile int _sync_protocol_enabled_count; // current number of JvmtiVTMSTransitionDisablers enabled sync protocol 86 static volatile bool _sync_protocol_enabled_permanently; // seen a suspender: JvmtiVTMSTransitionDisabler protocol is enabled permanently 87 88 bool _is_SR; // is suspender or resumer 89 jthread _thread; // virtual thread to disable transitions for, no-op if it is a platform thread 90 91 DEBUG_ONLY(static void print_info();) 92 void VTMS_transition_disable_for_one(); 93 void VTMS_transition_disable_for_all(); 94 void VTMS_transition_enable_for_one(); 95 void VTMS_transition_enable_for_all(); 96 97 public: 98 static bool _VTMS_notify_jvmti_events; // enable notifications from VirtualThread about VTMS events 99 static bool VTMS_notify_jvmti_events() { return _VTMS_notify_jvmti_events; } 100 static void set_VTMS_notify_jvmti_events(bool val) { _VTMS_notify_jvmti_events = val; } 101 102 static void inc_sync_protocol_enabled_count() { Atomic::inc(&_sync_protocol_enabled_count); } 103 static void dec_sync_protocol_enabled_count() { Atomic::dec(&_sync_protocol_enabled_count); } 104 static int sync_protocol_enabled_count() { return Atomic::load(&_sync_protocol_enabled_count); } 105 static bool sync_protocol_enabled_permanently() { return Atomic::load(&_sync_protocol_enabled_permanently); } 106 107 static bool sync_protocol_enabled() { return sync_protocol_enabled_permanently() || sync_protocol_enabled_count() > 0; } 108 109 // parameter is_SR: suspender or resumer 110 JvmtiVTMSTransitionDisabler(bool is_SR = false); 111 JvmtiVTMSTransitionDisabler(jthread thread); 112 ~JvmtiVTMSTransitionDisabler(); 113 114 // set VTMS transition bit value in JavaThread and java.lang.VirtualThread object 115 static void set_is_in_VTMS_transition(JavaThread* thread, jobject vthread, bool in_trans); 116 117 static void start_VTMS_transition(jthread vthread, bool is_mount); 118 static void finish_VTMS_transition(jthread vthread, bool is_mount); 119 120 static void VTMS_vthread_start(jobject vthread); 121 static void VTMS_vthread_end(jobject vthread); 122 123 static void VTMS_vthread_mount(jobject vthread, bool hide); 124 static void VTMS_vthread_unmount(jobject vthread, bool hide); 125 126 static void VTMS_mount_begin(jobject vthread); 127 static void VTMS_mount_end(jobject vthread); 128 129 static void VTMS_unmount_begin(jobject vthread, bool last_unmount); 130 static void VTMS_unmount_end(jobject vthread); 131 }; 132 133 /////////////////////////////////////////////////////////////// 134 // 135 // class VirtualThreadList 136 // 137 // Used for Virtual Threads Suspend/Resume management. 138 // It's a list of thread IDs. 139 // 140 class VirtualThreadList : public GrowableArrayCHeap<int64_t, mtServiceability> { 141 public: 142 VirtualThreadList() : GrowableArrayCHeap<int64_t, mtServiceability>(0) {} 143 void invalidate() { clear(); } 144 }; 145 146 /////////////////////////////////////////////////////////////// 147 // 148 // class JvmtiVTSuspender 149 // 150 // Virtual Threads Suspend/Resume management 151 // 152 class JvmtiVTSuspender : AllStatic { 153 private: 154 // Suspend modes for virtual threads 155 typedef enum SR_Mode { 156 SR_none = 0, 157 SR_ind = 1, 158 SR_all = 2 159 } SR_Mode; 160 161 static SR_Mode _SR_mode; 162 static VirtualThreadList* _suspended_list; 163 static VirtualThreadList* _not_suspended_list; 164 165 public: 166 static void register_all_vthreads_suspend(); 167 static void register_all_vthreads_resume(); 168 static void register_vthread_suspend(oop vt); 169 static void register_vthread_resume(oop vt); 170 static bool is_vthread_suspended(oop vt); 171 static bool is_vthread_suspended(int64_t thread_id); 172 }; 173 174 /////////////////////////////////////////////////////////////// 175 // 176 // class JvmtiThreadState 177 // 178 // The Jvmti state for each thread (across all JvmtiEnv): 179 // 1. Local table of enabled events. 180 class JvmtiThreadState : public CHeapObj<mtInternal> { 181 private: 182 friend class JvmtiEnv; 183 JavaThread *_thread; 184 JavaThread *_thread_saved; 185 OopHandle _thread_oop_h; 186 // Jvmti Events that cannot be posted in their current context. 187 JvmtiDeferredEventQueue* _jvmti_event_queue; 188 bool _is_virtual; // state belongs to a virtual thread 189 bool _hide_single_stepping; 190 bool _pending_interp_only_mode; 191 bool _pending_step_for_popframe; 192 bool _pending_step_for_earlyret; 193 bool _top_frame_is_exiting; 194 int _hide_level; 195 196 public: 197 enum ExceptionState { 198 ES_CLEARED, 199 ES_DETECTED, 200 ES_CAUGHT 201 }; 202 203 private: 204 ExceptionState _exception_state; 205 206 // Used to send class being redefined/retransformed and kind of transform 207 // info to the class file load hook event handler. 208 Klass* _class_being_redefined; 209 JvmtiClassLoadKind _class_load_kind; 210 GrowableArray<Klass*>* _classes_being_redefined; 211 212 // This is only valid when is_interp_only_mode() returns true 213 int _cur_stack_depth; 214 int _saved_interp_only_mode; 215 216 JvmtiThreadEventEnable _thread_event_enable; 217 218 // for support of JvmtiEnvThreadState 219 JvmtiEnvThreadState* _head_env_thread_state; 220 221 // doubly-linked linear list of active thread state 222 // needed in order to iterate the list without holding Threads_lock 223 static JvmtiThreadState *_head; 224 JvmtiThreadState *_next; 225 JvmtiThreadState *_prev; 226 227 // holds the current dynamic code event collector, null if no event collector in use 228 JvmtiDynamicCodeEventCollector* _dynamic_code_event_collector; 229 // holds the current vm object alloc event collector, null if no event collector in use 230 JvmtiVMObjectAllocEventCollector* _vm_object_alloc_event_collector; 231 // holds the current sampled object alloc event collector, null if no event collector in use 232 JvmtiSampledObjectAllocEventCollector* _sampled_object_alloc_event_collector; 233 234 // Should only be created by factory methods 235 JvmtiThreadState(JavaThread *thread, oop thread_oop); 236 237 friend class JvmtiEnvThreadStateIterator; 238 inline JvmtiEnvThreadState* head_env_thread_state(); 239 inline void set_head_env_thread_state(JvmtiEnvThreadState* ets); 240 241 static bool _seen_interp_only_mode; // interp_only_mode was requested at least once 242 243 public: 244 ~JvmtiThreadState(); 245 246 // is event_type enabled and usable for this thread in any environment? 247 bool is_enabled(jvmtiEvent event_type) { 248 return _thread_event_enable.is_enabled(event_type); 249 } 250 251 JvmtiThreadEventEnable *thread_event_enable() { 252 return &_thread_event_enable; 253 } 254 255 // Must only be called in situations where the state is for the current thread and 256 // the environment can not go away. To be safe, the returned JvmtiEnvThreadState 257 // must be used in such a way as there can be no intervening safepoints. 258 inline JvmtiEnvThreadState* env_thread_state(JvmtiEnvBase *env); 259 260 static void periodic_clean_up(); 261 262 // Return true if any thread has entered interp_only_mode at any point during the JVMs execution. 263 static bool seen_interp_only_mode() { 264 return _seen_interp_only_mode; 265 } 266 267 void add_env(JvmtiEnvBase *env); 268 269 // The pending_interp_only_mode is set when the interp_only_mode is triggered. 270 // It is cleared by EnterInterpOnlyModeClosure handshake. 271 bool is_pending_interp_only_mode() { return _pending_interp_only_mode; } 272 void set_pending_interp_only_mode(bool val) { _pending_interp_only_mode = val; } 273 274 // Used by the interpreter for fullspeed debugging support 275 bool is_interp_only_mode() { 276 return _thread == nullptr ? _saved_interp_only_mode != 0 : _thread->is_interp_only_mode(); 277 } 278 void enter_interp_only_mode(); 279 void leave_interp_only_mode(); 280 281 static void unbind_from(JvmtiThreadState* state, JavaThread* thread); 282 static void bind_to(JvmtiThreadState* state, JavaThread* thread); 283 static void process_pending_interp_only(JavaThread* current); 284 285 // access to the linked list of all JVMTI thread states 286 static JvmtiThreadState *first() { 287 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check"); 288 return _head; 289 } 290 291 JvmtiThreadState *next() { 292 return _next; 293 } 294 295 // Current stack depth is only valid when is_interp_only_mode() returns true. 296 // These functions should only be called at a safepoint - usually called from same thread. 297 // Returns the number of Java activations on the stack. 298 int cur_stack_depth(); 299 void invalidate_cur_stack_depth(); 300 void incr_cur_stack_depth(); 301 void decr_cur_stack_depth(); 302 303 int count_frames(); 304 305 inline JavaThread *get_thread() { return _thread; } 306 inline JavaThread *get_thread_or_saved(); // return _thread_saved if _thread is null 307 308 // Needed for virtual threads as they can migrate to different JavaThread's. 309 // Also used for carrier threads to clear/restore _thread. 310 void set_thread(JavaThread* thread); 311 oop get_thread_oop(); 312 313 inline bool is_virtual() { return _is_virtual; } // the _thread is virtual 314 315 inline bool is_exception_detected() { return _exception_state == ES_DETECTED; } 316 inline bool is_exception_caught() { return _exception_state == ES_CAUGHT; } 317 318 inline void set_exception_detected() { _exception_state = ES_DETECTED; } 319 inline void set_exception_caught() { _exception_state = ES_CAUGHT; } 320 321 inline void clear_exception_state() { _exception_state = ES_CLEARED; } 322 323 // We need to save and restore exception state inside JvmtiEventMark 324 inline ExceptionState get_exception_state() { return _exception_state; } 325 inline void restore_exception_state(ExceptionState state) { _exception_state = state; } 326 327 inline void clear_hide_single_stepping() { 328 if (_hide_level > 0) { 329 _hide_level--; 330 } else { 331 assert(_hide_single_stepping, "hide_single_stepping is out of phase"); 332 _hide_single_stepping = false; 333 } 334 } 335 inline bool hide_single_stepping() { return _hide_single_stepping; } 336 inline void set_hide_single_stepping() { 337 if (_hide_single_stepping) { 338 _hide_level++; 339 } else { 340 assert(_hide_level == 0, "hide_level is out of phase"); 341 _hide_single_stepping = true; 342 } 343 } 344 345 // Step pending flag is set when PopFrame is called and it is cleared 346 // when step for the Pop Frame is completed. 347 // This logic is used to distinguish b/w step for pop frame and repeat step. 348 void set_pending_step_for_popframe() { _pending_step_for_popframe = true; } 349 void clr_pending_step_for_popframe() { _pending_step_for_popframe = false; } 350 bool is_pending_step_for_popframe() { return _pending_step_for_popframe; } 351 void process_pending_step_for_popframe(); 352 353 // Step pending flag is set when ForceEarlyReturn is called and it is cleared 354 // when step for the ForceEarlyReturn is completed. 355 // This logic is used to distinguish b/w step for early return and repeat step. 356 void set_pending_step_for_earlyret() { _pending_step_for_earlyret = true; } 357 void clr_pending_step_for_earlyret() { _pending_step_for_earlyret = false; } 358 bool is_pending_step_for_earlyret() { return _pending_step_for_earlyret; } 359 void process_pending_step_for_earlyret(); 360 361 // For synchronization between NotifyFramePop and FramePop posting code. 362 void set_top_frame_is_exiting() { _top_frame_is_exiting = true; } 363 void clr_top_frame_is_exiting() { _top_frame_is_exiting = false; } 364 bool top_frame_is_exiting() { return _top_frame_is_exiting; } 365 366 // Setter and getter method is used to send redefined class info 367 // when class file load hook event is posted. 368 // It is set while loading redefined class and cleared before the 369 // class file load hook event is posted. 370 inline void set_class_being_redefined(Klass* k, JvmtiClassLoadKind kind) { 371 _class_being_redefined = k; 372 _class_load_kind = kind; 373 } 374 375 inline void clear_class_being_redefined() { 376 _class_being_redefined = nullptr; 377 _class_load_kind = jvmti_class_load_kind_load; 378 } 379 380 inline Klass* get_class_being_redefined() { 381 return _class_being_redefined; 382 } 383 384 inline JvmtiClassLoadKind get_class_load_kind() { 385 return _class_load_kind; 386 } 387 388 // Get the classes that are currently being redefined by this thread. 389 inline GrowableArray<Klass*>* get_classes_being_redefined() { 390 return _classes_being_redefined; 391 } 392 393 inline void set_classes_being_redefined(GrowableArray<Klass*>* redef_classes) { 394 _classes_being_redefined = redef_classes; 395 } 396 397 // RedefineClasses support 398 // The bug 6214132 caused the verification to fail. 399 // 400 // What is done at verification: 401 // (This seems to only apply to the old verifier.) 402 // When the verifier makes calls into the VM to ask questions about 403 // the class being verified, it will pass the jclass to JVM_* functions. 404 // The jclass is always pointing to the mirror of _the_class. 405 // ~28 JVM_* functions called by the verifier for the information 406 // about CP entries and klass structure should check the jvmtiThreadState 407 // info about equivalent klass versions and use it to replace a Klass* 408 // of _the_class with a Klass* of _scratch_class. The function 409 // class_to_verify_considering_redefinition() must be called for it. 410 // 411 // Note again, that this redirection happens only for the verifier thread. 412 // Other threads have very small overhead by checking the existence 413 // of the jvmtiThreadSate and the information about klasses equivalence. 414 // No JNI functions need to be changed, they don't reference the klass guts. 415 // The JavaThread pointer is already available in all JVM_* functions 416 // used by the verifier, so there is no extra performance issue with it. 417 418 private: 419 Klass* _the_class_for_redefinition_verification; 420 Klass* _scratch_class_for_redefinition_verification; 421 422 public: 423 inline void set_class_versions_map(Klass* the_class, 424 Klass* scratch_class) { 425 _the_class_for_redefinition_verification = the_class; 426 _scratch_class_for_redefinition_verification = scratch_class; 427 } 428 429 inline void clear_class_versions_map() { set_class_versions_map(nullptr, nullptr); } 430 431 static inline 432 Klass* class_to_verify_considering_redefinition(Klass* klass, 433 JavaThread *thread) { 434 JvmtiThreadState *state = thread->jvmti_thread_state(); 435 if (state != nullptr && state->_the_class_for_redefinition_verification != nullptr) { 436 if (state->_the_class_for_redefinition_verification == klass) { 437 klass = state->_scratch_class_for_redefinition_verification; 438 } 439 } 440 return klass; 441 } 442 443 // Todo: get rid of this! 444 private: 445 bool _debuggable; 446 public: 447 // Should the thread be enumerated by jvmtiInternal::GetAllThreads? 448 bool is_debuggable() { return _debuggable; } 449 // If a thread cannot be suspended (has no valid last_java_frame) then it gets marked !debuggable 450 void set_debuggable(bool debuggable) { _debuggable = debuggable; } 451 452 public: 453 454 // Thread local event collector setter and getter methods. 455 JvmtiDynamicCodeEventCollector* get_dynamic_code_event_collector() { 456 return _dynamic_code_event_collector; 457 } 458 JvmtiVMObjectAllocEventCollector* get_vm_object_alloc_event_collector() { 459 return _vm_object_alloc_event_collector; 460 } 461 JvmtiSampledObjectAllocEventCollector* get_sampled_object_alloc_event_collector() { 462 return _sampled_object_alloc_event_collector; 463 } 464 void set_dynamic_code_event_collector(JvmtiDynamicCodeEventCollector* collector) { 465 _dynamic_code_event_collector = collector; 466 } 467 void set_vm_object_alloc_event_collector(JvmtiVMObjectAllocEventCollector* collector) { 468 _vm_object_alloc_event_collector = collector; 469 } 470 void set_sampled_object_alloc_event_collector(JvmtiSampledObjectAllocEventCollector* collector) { 471 _sampled_object_alloc_event_collector = collector; 472 } 473 474 475 // 476 // Frame routines 477 // 478 479 public: 480 481 // true when the thread was suspended with a pointer to the last Java frame. 482 bool has_last_frame() { return _thread->has_last_Java_frame(); } 483 484 void update_for_pop_top_frame(); 485 486 // already holding JvmtiThreadState_lock - retrieve or create JvmtiThreadState 487 // Can return null if JavaThread is exiting. 488 // Callers are responsible to call recompute_thread_filtered() to update event bits 489 // if thread-filtered events are enabled globally. 490 static JvmtiThreadState *state_for_while_locked(JavaThread *thread, oop thread_oop = nullptr); 491 // retrieve or create JvmtiThreadState 492 // Can return null if JavaThread is exiting. 493 // Calls recompute_thread_filtered() to update event bits if thread-filtered events are enabled globally. 494 static JvmtiThreadState *state_for(JavaThread *thread, Handle thread_handle = Handle()); 495 496 // JVMTI ForceEarlyReturn support 497 498 // This is set to earlyret_pending to signal that top Java frame 499 // should be returned immediately 500 public: 501 int _earlyret_state; 502 TosState _earlyret_tos; 503 jvalue _earlyret_value; 504 oop _earlyret_oop; // Used to return an oop result into Java code from 505 // ForceEarlyReturnObject, GC-preserved 506 507 // Setting and clearing earlyret_state 508 // earlyret_pending indicates that a ForceEarlyReturn() has been 509 // requested and not yet been completed. 510 public: 511 enum EarlyretState { 512 earlyret_inactive = 0, 513 earlyret_pending = 1 514 }; 515 516 void set_earlyret_pending(void) { _earlyret_state = earlyret_pending; } 517 void clr_earlyret_pending(void) { _earlyret_state = earlyret_inactive; } 518 bool is_earlyret_pending(void) { return (_earlyret_state == earlyret_pending); } 519 520 TosState earlyret_tos() { return _earlyret_tos; } 521 oop earlyret_oop() const { return _earlyret_oop; } 522 void set_earlyret_oop (oop x) { _earlyret_oop = x; } 523 jvalue earlyret_value() { return _earlyret_value; } 524 void set_earlyret_value(jvalue val, TosState tos) { _earlyret_tos = tos; _earlyret_value = val; } 525 void clr_earlyret_value() { _earlyret_tos = ilgl; _earlyret_value.j = 0L; } 526 527 static ByteSize earlyret_state_offset() { return byte_offset_of(JvmtiThreadState, _earlyret_state); } 528 static ByteSize earlyret_tos_offset() { return byte_offset_of(JvmtiThreadState, _earlyret_tos); } 529 static ByteSize earlyret_oop_offset() { return byte_offset_of(JvmtiThreadState, _earlyret_oop); } 530 static ByteSize earlyret_value_offset() { return byte_offset_of(JvmtiThreadState, _earlyret_value); } 531 532 void oops_do(OopClosure* f, NMethodClosure* cf) NOT_JVMTI_RETURN; // GC support 533 void nmethods_do(NMethodClosure* cf) NOT_JVMTI_RETURN; 534 535 public: 536 void set_should_post_on_exceptions(bool val); 537 538 // Thread local event queue, which doesn't require taking the Service_lock. 539 void enqueue_event(JvmtiDeferredEvent* event) NOT_JVMTI_RETURN; 540 void post_events(JvmtiEnv* env); 541 void run_nmethod_entry_barriers(); 542 }; 543 544 #endif // SHARE_PRIMS_JVMTITHREADSTATE_HPP