2
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
25
#include "precompiled.hpp"
26
#include "classfile/javaClasses.inline.hpp"
27
#include "jvmtifiles/jvmtiEnv.hpp"
28
#include "memory/resourceArea.hpp"
29
#include "oops/oopHandle.inline.hpp"
30
#include "prims/jvmtiEnvBase.hpp"
31
#include "prims/jvmtiEventController.inline.hpp"
32
#include "prims/jvmtiImpl.hpp"
33
#include "prims/jvmtiThreadState.inline.hpp"
34
#include "runtime/handles.inline.hpp"
35
#include "runtime/interfaceSupport.inline.hpp"
36
#include "runtime/jniHandles.inline.hpp"
37
#include "runtime/safepointVerifiers.hpp"
38
#include "runtime/stackFrameStream.inline.hpp"
39
#include "runtime/vframe.hpp"
41
// marker for when the stack depth has been reset and is now unknown.
42
// any negative number would work but small ones might obscure an
44
static const int UNKNOWN_STACK_DEPTH = -99;
46
///////////////////////////////////////////////////////////////
48
// class JvmtiThreadState
50
// Instances of JvmtiThreadState hang off of each thread.
51
// Thread local storage for JVMTI.
54
JvmtiThreadState *JvmtiThreadState::_head = nullptr;
55
bool JvmtiThreadState::_seen_interp_only_mode = false;
57
JvmtiThreadState::JvmtiThreadState(JavaThread* thread, oop thread_oop)
58
: _thread_event_enable() {
59
assert(JvmtiThreadState_lock->is_locked(), "sanity check");
61
_thread_saved = nullptr;
62
_exception_state = ES_CLEARED;
64
_hide_single_stepping = false;
65
_pending_interp_only_mode = false;
67
_pending_step_for_popframe = false;
68
_class_being_redefined = nullptr;
69
_class_load_kind = jvmti_class_load_kind_load;
70
_classes_being_redefined = nullptr;
71
_head_env_thread_state = nullptr;
72
_dynamic_code_event_collector = nullptr;
73
_vm_object_alloc_event_collector = nullptr;
74
_sampled_object_alloc_event_collector = nullptr;
75
_the_class_for_redefinition_verification = nullptr;
76
_scratch_class_for_redefinition_verification = nullptr;
77
_cur_stack_depth = UNKNOWN_STACK_DEPTH;
78
_saved_interp_only_mode = 0;
80
// JVMTI ForceEarlyReturn support
81
_pending_step_for_earlyret = false;
82
_earlyret_state = earlyret_inactive;
84
_earlyret_value.j = 0L;
85
_earlyret_oop = nullptr;
86
_jvmti_event_queue = nullptr;
89
_thread_oop_h = OopHandle(JvmtiExport::jvmti_oop_storage(), thread_oop);
91
// add all the JvmtiEnvThreadState to the new JvmtiThreadState
94
for (JvmtiEnvBase* env = it.first(); env != nullptr; env = it.next(env)) {
95
if (env->is_valid()) {
101
// link us into the list
103
// The thread state list manipulation code must not have safepoints.
104
// See periodic_clean_up().
105
debug_only(NoSafepointVerifier nosafepoint;)
109
if (_head != nullptr) {
115
if (thread_oop != nullptr) {
116
java_lang_Thread::set_jvmti_thread_state(thread_oop, this);
117
_is_virtual = java_lang_VirtualThread::is_instance(thread_oop);
120
if (thread != nullptr) {
121
if (thread_oop == nullptr || thread->jvmti_vthread() == nullptr || thread->jvmti_vthread() == thread_oop) {
122
// The JavaThread for carrier or mounted virtual thread case.
123
// Set this only if thread_oop is current thread->jvmti_vthread().
124
thread->set_jvmti_thread_state(this);
126
thread->set_interp_only_mode(0);
131
JvmtiThreadState::~JvmtiThreadState() {
132
assert(JvmtiThreadState_lock->is_locked(), "sanity check");
134
if (_classes_being_redefined != nullptr) {
135
delete _classes_being_redefined; // free the GrowableArray on C heap
138
// clear this as the state for the thread
139
get_thread()->set_jvmti_thread_state(nullptr);
141
// zap our env thread states
143
JvmtiEnvBase::entering_dying_thread_env_iteration();
144
JvmtiEnvThreadStateIterator it(this);
145
for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ) {
146
JvmtiEnvThreadState* zap = ets;
150
JvmtiEnvBase::leaving_dying_thread_env_iteration();
153
// remove us from the list
155
// The thread state list manipulation code must not have safepoints.
156
// See periodic_clean_up().
157
debug_only(NoSafepointVerifier nosafepoint;)
159
if (_prev == nullptr) {
160
assert(_head == this, "sanity check");
163
assert(_head != this, "sanity check");
164
_prev->_next = _next;
166
if (_next != nullptr) {
167
_next->_prev = _prev;
172
if (get_thread_oop() != nullptr) {
173
java_lang_Thread::set_jvmti_thread_state(get_thread_oop(), nullptr);
175
_thread_oop_h.release(JvmtiExport::jvmti_oop_storage());
180
JvmtiThreadState::periodic_clean_up() {
181
assert(SafepointSynchronize::is_at_safepoint(), "at safepoint");
183
// This iteration is initialized with "_head" instead of "JvmtiThreadState::first()"
184
// because the latter requires the JvmtiThreadState_lock.
185
// This iteration is safe at a safepoint as well, see the NoSafepointVerifier
186
// asserts at all list manipulation sites.
187
for (JvmtiThreadState *state = _head; state != nullptr; state = state->next()) {
188
// For each environment thread state corresponding to an invalid environment
189
// unlink it from the list and deallocate it.
190
JvmtiEnvThreadStateIterator it(state);
191
JvmtiEnvThreadState* previous_ets = nullptr;
192
JvmtiEnvThreadState* ets = it.first();
193
while (ets != nullptr) {
194
if (ets->get_env()->is_valid()) {
198
// This one isn't valid, remove it from the list and deallocate it
199
JvmtiEnvThreadState* defunct_ets = ets;
201
if (previous_ets == nullptr) {
202
assert(state->head_env_thread_state() == defunct_ets, "sanity check");
203
state->set_head_env_thread_state(ets);
205
previous_ets->set_next(ets);
214
// Virtual Threads Mount State transition (VTMS transition) mechanism
217
// VTMS transitions for one virtual thread are disabled while it is positive
218
volatile int JvmtiVTMSTransitionDisabler::_VTMS_transition_disable_for_one_count = 0;
220
// VTMS transitions for all virtual threads are disabled while it is positive
221
volatile int JvmtiVTMSTransitionDisabler::_VTMS_transition_disable_for_all_count = 0;
223
// There is an active suspender or resumer.
224
volatile bool JvmtiVTMSTransitionDisabler::_SR_mode = false;
226
// Notifications from VirtualThread about VTMS events are enabled.
227
bool JvmtiVTMSTransitionDisabler::_VTMS_notify_jvmti_events = false;
229
// The JvmtiVTMSTransitionDisabler sync protocol is enabled if this count > 0.
230
volatile int JvmtiVTMSTransitionDisabler::_sync_protocol_enabled_count = 0;
232
// JvmtiVTMSTraansitionDisabler sync protocol is enabled permanently after seeing a suspender.
233
volatile bool JvmtiVTMSTransitionDisabler::_sync_protocol_enabled_permanently = false;
237
JvmtiVTMSTransitionDisabler::print_info() {
238
log_error(jvmti)("_VTMS_transition_disable_for_one_count: %d\n", _VTMS_transition_disable_for_one_count);
239
log_error(jvmti)("_VTMS_transition_disable_for_all_count: %d\n\n", _VTMS_transition_disable_for_all_count);
240
int attempts = 10000;
241
for (JavaThreadIteratorWithHandle jtiwh; JavaThread *java_thread = jtiwh.next(); ) {
242
if (java_thread->VTMS_transition_mark()) {
243
log_error(jvmti)("jt: %p VTMS_transition_mark: %d\n",
244
(void*)java_thread, java_thread->VTMS_transition_mark());
247
// Handshake with target.
248
PrintStackTraceClosure pstc;
249
Handshake::execute(&pstc, java_thread);
254
// disable VTMS transitions for one virtual thread
255
// no-op if thread is non-null and not a virtual thread
256
JvmtiVTMSTransitionDisabler::JvmtiVTMSTransitionDisabler(jthread thread)
257
: _is_SR(false), _thread(thread)
259
if (!Continuations::enabled()) {
260
return; // JvmtiVTMSTransitionDisabler is no-op without virtual threads
262
if (Thread::current_or_null() == nullptr) {
263
return; // Detached thread, can be a call from Agent_OnLoad.
265
if (!sync_protocol_enabled_permanently()) {
266
JvmtiVTMSTransitionDisabler::inc_sync_protocol_enabled_count();
268
if (_thread != nullptr) {
269
VTMS_transition_disable_for_one(); // disable VTMS transitions for one virtual thread
271
VTMS_transition_disable_for_all(); // disable VTMS transitions for all virtual threads
275
// disable VTMS transitions for all virtual threads
276
JvmtiVTMSTransitionDisabler::JvmtiVTMSTransitionDisabler(bool is_SR)
277
: _is_SR(is_SR), _thread(nullptr)
279
if (!Continuations::enabled()) {
280
return; // JvmtiVTMSTransitionDisabler is no-op without virtual threads
282
if (Thread::current_or_null() == nullptr) {
283
return; // Detached thread, can be a call from Agent_OnLoad.
285
if (!sync_protocol_enabled_permanently()) {
286
JvmtiVTMSTransitionDisabler::inc_sync_protocol_enabled_count();
288
Atomic::store(&_sync_protocol_enabled_permanently, true);
291
VTMS_transition_disable_for_all();
294
JvmtiVTMSTransitionDisabler::~JvmtiVTMSTransitionDisabler() {
295
if (!Continuations::enabled()) {
296
return; // JvmtiVTMSTransitionDisabler is a no-op without virtual threads
298
if (Thread::current_or_null() == nullptr) {
299
return; // Detached thread, can be a call from Agent_OnLoad.
301
if (_thread != nullptr) {
302
VTMS_transition_enable_for_one(); // enable VTMS transitions for one virtual thread
304
VTMS_transition_enable_for_all(); // enable VTMS transitions for all virtual threads
306
if (!sync_protocol_enabled_permanently()) {
307
JvmtiVTMSTransitionDisabler::dec_sync_protocol_enabled_count();
311
// disable VTMS transitions for one virtual thread
313
JvmtiVTMSTransitionDisabler::VTMS_transition_disable_for_one() {
314
assert(_thread != nullptr, "sanity check");
315
JavaThread* thread = JavaThread::current();
316
HandleMark hm(thread);
317
Handle vth = Handle(thread, JNIHandles::resolve_external_guard(_thread));
318
if (!java_lang_VirtualThread::is_instance(vth())) {
319
return; // no-op if _thread is not a virtual thread
321
MonitorLocker ml(JvmtiVTMSTransition_lock);
323
while (_SR_mode) { // suspender or resumer is a JvmtiVTMSTransitionDisabler monopolist
324
ml.wait(10); // wait while there is an active suspender or resumer
326
Atomic::inc(&_VTMS_transition_disable_for_one_count);
327
java_lang_Thread::inc_VTMS_transition_disable_count(vth());
329
while (java_lang_Thread::is_in_VTMS_transition(vth())) {
330
ml.wait(10); // wait while the virtual thread is in transition
333
thread->set_is_VTMS_transition_disabler(true);
337
// disable VTMS transitions for all virtual threads
339
JvmtiVTMSTransitionDisabler::VTMS_transition_disable_for_all() {
340
JavaThread* thread = JavaThread::current();
341
int attempts = 50000;
343
MonitorLocker ml(JvmtiVTMSTransition_lock);
345
assert(!thread->is_in_tmp_VTMS_transition(), "sanity check");
346
assert(!thread->is_in_VTMS_transition(), "VTMS_transition sanity check");
347
while (_SR_mode) { // Suspender or resumer is a JvmtiVTMSTransitionDisabler monopolist.
348
ml.wait(10); // Wait while there is an active suspender or resumer.
352
while (_VTMS_transition_disable_for_all_count > 0 ||
353
_VTMS_transition_disable_for_one_count > 0) {
354
ml.wait(10); // Wait while there is any active jvmtiVTMSTransitionDisabler.
357
Atomic::inc(&_VTMS_transition_disable_for_all_count);
359
// Block while some mount/unmount transitions are in progress.
360
// Debug version fails and prints diagnostic information.
361
for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
362
while (jt->VTMS_transition_mark()) {
366
DEBUG_ONLY(if (attempts == 0) break;)
369
assert(!thread->is_VTMS_transition_disabler(), "VTMS_transition sanity check");
372
thread->set_is_VTMS_transition_disabler(true);
379
fatal("stuck in JvmtiVTMSTransitionDisabler::VTMS_transition_disable");
384
// enable VTMS transitions for one virtual thread
386
JvmtiVTMSTransitionDisabler::VTMS_transition_enable_for_one() {
387
JavaThread* thread = JavaThread::current();
388
HandleMark hm(thread);
389
Handle vth = Handle(thread, JNIHandles::resolve_external_guard(_thread));
390
if (!java_lang_VirtualThread::is_instance(vth())) {
391
return; // no-op if _thread is not a virtual thread
393
MonitorLocker ml(JvmtiVTMSTransition_lock);
394
java_lang_Thread::dec_VTMS_transition_disable_count(vth());
395
Atomic::dec(&_VTMS_transition_disable_for_one_count);
396
if (_VTMS_transition_disable_for_one_count == 0) {
400
thread->set_is_VTMS_transition_disabler(false);
404
// enable VTMS transitions for all virtual threads
406
JvmtiVTMSTransitionDisabler::VTMS_transition_enable_for_all() {
407
JavaThread* current = JavaThread::current();
409
MonitorLocker ml(JvmtiVTMSTransition_lock);
410
assert(_VTMS_transition_disable_for_all_count > 0, "VTMS_transition sanity check");
412
if (_is_SR) { // Disabler is suspender or resumer.
415
Atomic::dec(&_VTMS_transition_disable_for_all_count);
416
if (_VTMS_transition_disable_for_all_count == 0 || _is_SR) {
420
current->set_is_VTMS_transition_disabler(false);
426
JvmtiVTMSTransitionDisabler::start_VTMS_transition(jthread vthread, bool is_mount) {
427
JavaThread* thread = JavaThread::current();
428
oop vt = JNIHandles::resolve_external_guard(vthread);
429
assert(!thread->is_in_VTMS_transition(), "VTMS_transition sanity check");
431
// Avoid using MonitorLocker on performance critical path, use
432
// two-level synchronization with lock-free operations on state bits.
433
assert(!thread->VTMS_transition_mark(), "sanity check");
434
thread->set_VTMS_transition_mark(true); // Try to enter VTMS transition section optmistically.
435
java_lang_Thread::set_is_in_VTMS_transition(vt, true);
437
if (!sync_protocol_enabled()) {
438
thread->set_is_in_VTMS_transition(true);
441
HandleMark hm(thread);
442
Handle vth = Handle(thread, vt);
443
int attempts = 50000;
445
// Do not allow suspends inside VTMS transitions.
446
// Block while transitions are disabled or there are suspend requests.
447
int64_t thread_id = java_lang_Thread::thread_id(vth()); // Cannot use oops while blocked.
449
if (_VTMS_transition_disable_for_all_count > 0 ||
450
java_lang_Thread::VTMS_transition_disable_count(vth()) > 0 ||
451
thread->is_suspended() ||
452
JvmtiVTSuspender::is_vthread_suspended(thread_id)
454
// Slow path: undo unsuccessful optimistic set of the VTMS_transition_mark.
455
// It can cause an extra waiting cycle for VTMS transition disablers.
456
thread->set_VTMS_transition_mark(false);
457
java_lang_Thread::set_is_in_VTMS_transition(vth(), false);
460
MonitorLocker ml(JvmtiVTMSTransition_lock);
462
// Do not allow suspends inside VTMS transitions.
463
// Block while transitions are disabled or there are suspend requests.
464
if (_VTMS_transition_disable_for_all_count > 0 ||
465
java_lang_Thread::VTMS_transition_disable_count(vth()) > 0 ||
466
thread->is_suspended() ||
467
JvmtiVTSuspender::is_vthread_suspended(thread_id)
469
// Block while transitions are disabled or there are suspend requests.
473
DEBUG_ONLY(if (attempts == 0) break;)
474
continue; // ~ThreadBlockInVM has handshake-based suspend point.
476
thread->set_VTMS_transition_mark(true);
477
java_lang_Thread::set_is_in_VTMS_transition(vth(), true);
483
log_error(jvmti)("start_VTMS_transition: thread->is_suspended: %d is_vthread_suspended: %d\n\n",
484
thread->is_suspended(), JvmtiVTSuspender::is_vthread_suspended(thread_id));
486
fatal("stuck in JvmtiVTMSTransitionDisabler::start_VTMS_transition");
489
// Enter VTMS transition section.
490
thread->set_is_in_VTMS_transition(true);
494
JvmtiVTMSTransitionDisabler::finish_VTMS_transition(jthread vthread, bool is_mount) {
495
JavaThread* thread = JavaThread::current();
497
assert(thread->is_in_VTMS_transition(), "sanity check");
498
thread->set_is_in_VTMS_transition(false);
499
oop vt = JNIHandles::resolve_external_guard(vthread);
500
java_lang_Thread::set_is_in_VTMS_transition(vt, false);
501
assert(thread->VTMS_transition_mark(), "sanity check");
502
thread->set_VTMS_transition_mark(false);
504
if (!sync_protocol_enabled()) {
507
int64_t thread_id = java_lang_Thread::thread_id(vt);
509
// Unblock waiting VTMS transition disablers.
510
if (_VTMS_transition_disable_for_one_count > 0 ||
511
_VTMS_transition_disable_for_all_count > 0) {
512
MonitorLocker ml(JvmtiVTMSTransition_lock);
515
// In unmount case the carrier thread is attached after unmount transition.
516
// Check and block it if there was external suspend request.
517
int attempts = 10000;
518
if (!is_mount && thread->is_carrier_thread_suspended()) {
520
MonitorLocker ml(JvmtiVTMSTransition_lock);
522
// Block while there are suspend requests.
523
if ((!is_mount && thread->is_carrier_thread_suspended()) ||
524
(is_mount && JvmtiVTSuspender::is_vthread_suspended(thread_id))
526
// Block while there are suspend requests.
530
DEBUG_ONLY(if (attempts == 0) break;)
538
log_error(jvmti)("finish_VTMS_transition: thread->is_suspended: %d is_vthread_suspended: %d\n\n",
539
thread->is_suspended(), JvmtiVTSuspender::is_vthread_suspended(thread_id));
541
fatal("stuck in JvmtiVTMSTransitionDisabler::finish_VTMS_transition");
546
// set VTMS transition bit value in JavaThread and java.lang.VirtualThread object
547
void JvmtiVTMSTransitionDisabler::set_is_in_VTMS_transition(JavaThread* thread, jobject vthread, bool in_trans) {
548
oop vt = JNIHandles::resolve_external_guard(vthread);
549
java_lang_Thread::set_is_in_VTMS_transition(vt, in_trans);
550
thread->set_is_in_VTMS_transition(in_trans);
554
JvmtiVTMSTransitionDisabler::VTMS_vthread_start(jobject vthread) {
555
VTMS_mount_end(vthread);
556
JavaThread* thread = JavaThread::current();
558
assert(!thread->is_in_VTMS_transition(), "sanity check");
559
assert(!thread->is_in_tmp_VTMS_transition(), "sanity check");
561
// If interp_only_mode has been enabled then we must eagerly create JvmtiThreadState
562
// objects for globally enabled virtual thread filtered events. Otherwise,
563
// it is an important optimization to create JvmtiThreadState objects lazily.
564
// This optimization is disabled when watchpoint capabilities are present. It is to
565
// work around a bug with virtual thread frames which can be not deoptimized in time.
566
if (JvmtiThreadState::seen_interp_only_mode() ||
567
JvmtiExport::should_post_field_access() ||
568
JvmtiExport::should_post_field_modification()){
569
JvmtiEventController::thread_started(thread);
571
if (JvmtiExport::should_post_vthread_start()) {
572
JvmtiExport::post_vthread_start(vthread);
574
// post VirtualThreadMount event after VirtualThreadStart
575
if (JvmtiExport::should_post_vthread_mount()) {
576
JvmtiExport::post_vthread_mount(vthread);
581
JvmtiVTMSTransitionDisabler::VTMS_vthread_end(jobject vthread) {
582
JavaThread* thread = JavaThread::current();
584
assert(!thread->is_in_VTMS_transition(), "sanity check");
585
assert(!thread->is_in_tmp_VTMS_transition(), "sanity check");
587
// post VirtualThreadUnmount event before VirtualThreadEnd
588
if (JvmtiExport::should_post_vthread_unmount()) {
589
JvmtiExport::post_vthread_unmount(vthread);
591
if (JvmtiExport::should_post_vthread_end()) {
592
JvmtiExport::post_vthread_end(vthread);
594
VTMS_unmount_begin(vthread, /* last_unmount */ true);
595
if (thread->jvmti_thread_state() != nullptr) {
596
JvmtiExport::cleanup_thread(thread);
597
assert(thread->jvmti_thread_state() == nullptr, "should be null");
598
assert(java_lang_Thread::jvmti_thread_state(JNIHandles::resolve(vthread)) == nullptr, "should be null");
600
thread->rebind_to_jvmti_thread_state_of(thread->threadObj());
604
JvmtiVTMSTransitionDisabler::VTMS_vthread_mount(jobject vthread, bool hide) {
606
VTMS_mount_begin(vthread);
608
VTMS_mount_end(vthread);
609
if (JvmtiExport::should_post_vthread_mount()) {
610
JvmtiExport::post_vthread_mount(vthread);
616
JvmtiVTMSTransitionDisabler::VTMS_vthread_unmount(jobject vthread, bool hide) {
618
if (JvmtiExport::should_post_vthread_unmount()) {
619
JvmtiExport::post_vthread_unmount(vthread);
621
VTMS_unmount_begin(vthread, /* last_unmount */ false);
623
VTMS_unmount_end(vthread);
628
JvmtiVTMSTransitionDisabler::VTMS_mount_begin(jobject vthread) {
629
JavaThread* thread = JavaThread::current();
630
assert(!thread->is_in_tmp_VTMS_transition(), "sanity check");
631
assert(!thread->is_in_VTMS_transition(), "sanity check");
632
start_VTMS_transition(vthread, /* is_mount */ true);
636
JvmtiVTMSTransitionDisabler::VTMS_mount_end(jobject vthread) {
637
JavaThread* thread = JavaThread::current();
638
oop vt = JNIHandles::resolve(vthread);
640
thread->rebind_to_jvmti_thread_state_of(vt);
642
assert(thread->is_in_VTMS_transition(), "sanity check");
643
assert(!thread->is_in_tmp_VTMS_transition(), "sanity check");
644
finish_VTMS_transition(vthread, /* is_mount */ true);
648
JvmtiVTMSTransitionDisabler::VTMS_unmount_begin(jobject vthread, bool last_unmount) {
649
JavaThread* thread = JavaThread::current();
651
assert(!thread->is_in_tmp_VTMS_transition(), "sanity check");
652
assert(!thread->is_in_VTMS_transition(), "sanity check");
654
start_VTMS_transition(vthread, /* is_mount */ false);
656
thread->rebind_to_jvmti_thread_state_of(thread->threadObj());
661
JvmtiVTMSTransitionDisabler::VTMS_unmount_end(jobject vthread) {
662
JavaThread* thread = JavaThread::current();
663
assert(thread->is_in_VTMS_transition(), "sanity check");
664
assert(!thread->is_in_tmp_VTMS_transition(), "sanity check");
665
finish_VTMS_transition(vthread, /* is_mount */ false);
670
// Virtual Threads Suspend/Resume management
673
JvmtiVTSuspender::SR_Mode
674
JvmtiVTSuspender::_SR_mode = SR_none;
677
JvmtiVTSuspender::_suspended_list = new VirtualThreadList();
680
JvmtiVTSuspender::_not_suspended_list = new VirtualThreadList();
683
JvmtiVTSuspender::register_all_vthreads_suspend() {
684
MonitorLocker ml(JvmtiVTMSTransition_lock);
687
_suspended_list->invalidate();
688
_not_suspended_list->invalidate();
692
JvmtiVTSuspender::register_all_vthreads_resume() {
693
MonitorLocker ml(JvmtiVTMSTransition_lock);
696
_suspended_list->invalidate();
697
_not_suspended_list->invalidate();
701
JvmtiVTSuspender::register_vthread_suspend(oop vt) {
702
int64_t id = java_lang_Thread::thread_id(vt);
703
MonitorLocker ml(JvmtiVTMSTransition_lock);
705
if (_SR_mode == SR_all) {
706
assert(_not_suspended_list->contains(id),
707
"register_vthread_suspend sanity check");
708
_not_suspended_list->remove(id);
710
assert(!_suspended_list->contains(id),
711
"register_vthread_suspend sanity check");
713
_suspended_list->append(id);
718
JvmtiVTSuspender::register_vthread_resume(oop vt) {
719
int64_t id = java_lang_Thread::thread_id(vt);
720
MonitorLocker ml(JvmtiVTMSTransition_lock);
722
if (_SR_mode == SR_all) {
723
assert(!_not_suspended_list->contains(id),
724
"register_vthread_resume sanity check");
725
_not_suspended_list->append(id);
726
} else if (_SR_mode == SR_ind) {
727
assert(_suspended_list->contains(id),
728
"register_vthread_resume check");
729
_suspended_list->remove(id);
730
if (_suspended_list->length() == 0) {
734
assert(false, "register_vthread_resume: no suspend mode enabled");
739
JvmtiVTSuspender::is_vthread_suspended(int64_t thread_id) {
740
bool suspend_is_needed =
741
(_SR_mode == SR_all && !_not_suspended_list->contains(thread_id)) ||
742
(_SR_mode == SR_ind && _suspended_list->contains(thread_id));
744
return suspend_is_needed;
748
JvmtiVTSuspender::is_vthread_suspended(oop vt) {
749
return is_vthread_suspended(java_lang_Thread::thread_id(vt));
752
void JvmtiThreadState::add_env(JvmtiEnvBase *env) {
753
assert(JvmtiThreadState_lock->is_locked(), "sanity check");
755
JvmtiEnvThreadState *new_ets = new JvmtiEnvThreadState(this, env);
756
// add this environment thread state to the end of the list (order is important)
758
// list deallocation (which occurs at a safepoint) cannot occur simultaneously
759
debug_only(NoSafepointVerifier nosafepoint;)
761
JvmtiEnvThreadStateIterator it(this);
762
JvmtiEnvThreadState* previous_ets = nullptr;
763
for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
766
if (previous_ets == nullptr) {
767
set_head_env_thread_state(new_ets);
769
previous_ets->set_next(new_ets);
774
void JvmtiThreadState::enter_interp_only_mode() {
775
assert(_thread != nullptr, "sanity check");
776
_seen_interp_only_mode = true;
777
_thread->increment_interp_only_mode();
778
invalidate_cur_stack_depth();
781
void JvmtiThreadState::leave_interp_only_mode() {
782
assert(is_interp_only_mode(), "leaving interp only when not in interp only mode");
783
if (_thread == nullptr) {
784
// Unmounted virtual thread updates the saved value.
785
--_saved_interp_only_mode;
787
_thread->decrement_interp_only_mode();
792
// Helper routine used in several places
793
int JvmtiThreadState::count_frames() {
794
JavaThread* thread = get_thread_or_saved();
797
if (thread == nullptr) {
798
oop thread_obj = get_thread_oop();
799
jvf = JvmtiEnvBase::get_vthread_jvf(thread_obj);
802
Thread *current_thread = Thread::current();
804
assert(SafepointSynchronize::is_at_safepoint() ||
805
thread->is_handshake_safe_for(current_thread),
806
"call by myself / at safepoint / at handshake");
807
if (!thread->has_last_Java_frame()) return 0; // No Java frames.
808
// TBD: This might need to be corrected for detached carrier threads.
809
RegisterMap reg_map(thread,
810
RegisterMap::UpdateMap::skip,
811
RegisterMap::ProcessFrames::skip,
812
RegisterMap::WalkContinuation::include);
813
jvf = thread->last_java_vframe(®_map);
814
jvf = JvmtiEnvBase::check_and_skip_hidden_frames(thread, jvf);
816
return (int)JvmtiEnvBase::get_frame_count(jvf);
820
void JvmtiThreadState::invalidate_cur_stack_depth() {
821
assert(SafepointSynchronize::is_at_safepoint() ||
822
get_thread()->is_handshake_safe_for(Thread::current()),
823
"bad synchronization with owner thread");
825
_cur_stack_depth = UNKNOWN_STACK_DEPTH;
828
void JvmtiThreadState::incr_cur_stack_depth() {
829
guarantee(JavaThread::current() == get_thread(), "must be current thread");
831
if (!is_interp_only_mode()) {
832
_cur_stack_depth = UNKNOWN_STACK_DEPTH;
834
if (_cur_stack_depth != UNKNOWN_STACK_DEPTH) {
839
void JvmtiThreadState::decr_cur_stack_depth() {
840
guarantee(JavaThread::current() == get_thread(), "must be current thread");
842
if (!is_interp_only_mode()) {
843
_cur_stack_depth = UNKNOWN_STACK_DEPTH;
845
if (_cur_stack_depth != UNKNOWN_STACK_DEPTH) {
847
assert(_cur_stack_depth >= 0, "incr/decr_cur_stack_depth mismatch");
851
int JvmtiThreadState::cur_stack_depth() {
852
Thread *current = Thread::current();
853
guarantee(get_thread()->is_handshake_safe_for(current),
854
"must be current thread or direct handshake");
856
if (!is_interp_only_mode() || _cur_stack_depth == UNKNOWN_STACK_DEPTH) {
857
_cur_stack_depth = count_frames();
860
if (EnableJVMTIStackDepthAsserts) {
861
// heavy weight assert
862
jint num_frames = count_frames();
863
assert(_cur_stack_depth == num_frames, "cur_stack_depth out of sync _cur_stack_depth: %d num_frames: %d", _cur_stack_depth, num_frames);
867
return _cur_stack_depth;
870
void JvmtiThreadState::process_pending_step_for_popframe() {
871
// We are single stepping as the last part of the PopFrame() dance
872
// so we have some house keeping to do.
874
JavaThread *thr = get_thread();
875
if (thr->popframe_condition() != JavaThread::popframe_inactive) {
876
// If the popframe_condition field is not popframe_inactive, then
877
// we missed all of the popframe_field cleanup points:
879
// - unpack_frames() was not called (nothing to deopt)
880
// - remove_activation_preserving_args_entry() was not called
881
// (did not get suspended in a call_vm() family call and did
882
// not complete a call_vm() family call on the way here)
883
thr->clear_popframe_condition();
886
// clearing the flag indicates we are done with the PopFrame() dance
887
clr_pending_step_for_popframe();
889
// If exception was thrown in this frame, need to reset jvmti thread state.
890
// Single stepping may not get enabled correctly by the agent since
891
// exception state is passed in MethodExit event which may be sent at some
892
// time in the future. JDWP agent ignores MethodExit events if caused by
895
if (is_exception_detected()) {
896
clear_exception_state();
898
// If step is pending for popframe then it may not be
899
// a repeat step. The new_bci and method_id is same as current_bci
900
// and current method_id after pop and step for recursive calls.
901
// Force the step by clearing the last location.
902
JvmtiEnvThreadStateIterator it(this);
903
for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
904
ets->clear_current_location();
909
// Class: JvmtiThreadState
910
// Function: update_for_pop_top_frame
912
// This function removes any frame pop notification request for
913
// the top frame and invalidates both the current stack depth and
914
// all cached frameIDs.
916
// Called by: PopFrame
918
void JvmtiThreadState::update_for_pop_top_frame() {
919
if (is_interp_only_mode()) {
920
// remove any frame pop notification request for the top frame
921
// in any environment
922
int popframe_number = cur_stack_depth();
924
JvmtiEnvThreadStateIterator it(this);
925
for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
926
if (ets->is_frame_pop(popframe_number)) {
927
ets->clear_frame_pop(popframe_number);
931
// force stack depth to be recalculated
932
invalidate_cur_stack_depth();
934
assert(!is_enabled(JVMTI_EVENT_FRAME_POP), "Must have no framepops set");
939
void JvmtiThreadState::process_pending_step_for_earlyret() {
940
// We are single stepping as the last part of the ForceEarlyReturn
941
// dance so we have some house keeping to do.
943
if (is_earlyret_pending()) {
944
// If the earlyret_state field is not earlyret_inactive, then
945
// we missed all of the earlyret_field cleanup points:
947
// - remove_activation() was not called
948
// (did not get suspended in a call_vm() family call and did
949
// not complete a call_vm() family call on the way here)
951
// One legitimate way for us to miss all the cleanup points is
952
// if we got here right after handling a compiled return. If that
953
// is the case, then we consider our return from compiled code to
954
// complete the ForceEarlyReturn request and we clear the condition.
955
clr_earlyret_pending();
956
set_earlyret_oop(nullptr);
957
clr_earlyret_value();
960
// clearing the flag indicates we are done with
961
// the ForceEarlyReturn() dance
962
clr_pending_step_for_earlyret();
964
// If exception was thrown in this frame, need to reset jvmti thread state.
965
// Single stepping may not get enabled correctly by the agent since
966
// exception state is passed in MethodExit event which may be sent at some
967
// time in the future. JDWP agent ignores MethodExit events if caused by
970
if (is_exception_detected()) {
971
clear_exception_state();
973
// If step is pending for earlyret then it may not be a repeat step.
974
// The new_bci and method_id is same as current_bci and current
975
// method_id after earlyret and step for recursive calls.
976
// Force the step by clearing the last location.
977
JvmtiEnvThreadStateIterator it(this);
978
for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
979
ets->clear_current_location();
983
void JvmtiThreadState::oops_do(OopClosure* f, NMethodClosure* cf) {
984
f->do_oop((oop*) &_earlyret_oop);
986
// Keep nmethods from unloading on the event queue
987
if (_jvmti_event_queue != nullptr) {
988
_jvmti_event_queue->oops_do(f, cf);
992
void JvmtiThreadState::nmethods_do(NMethodClosure* cf) {
993
// Keep nmethods from unloading on the event queue
994
if (_jvmti_event_queue != nullptr) {
995
_jvmti_event_queue->nmethods_do(cf);
999
// Thread local event queue.
1000
void JvmtiThreadState::enqueue_event(JvmtiDeferredEvent* event) {
1001
if (_jvmti_event_queue == nullptr) {
1002
_jvmti_event_queue = new JvmtiDeferredEventQueue();
1005
_jvmti_event_queue->enqueue(*event);
1008
void JvmtiThreadState::post_events(JvmtiEnv* env) {
1009
if (_jvmti_event_queue != nullptr) {
1010
_jvmti_event_queue->post(env); // deletes each queue node
1011
delete _jvmti_event_queue;
1012
_jvmti_event_queue = nullptr;
1016
void JvmtiThreadState::run_nmethod_entry_barriers() {
1017
if (_jvmti_event_queue != nullptr) {
1018
_jvmti_event_queue->run_nmethod_entry_barriers();
1022
oop JvmtiThreadState::get_thread_oop() {
1023
return _thread_oop_h.resolve();
1026
void JvmtiThreadState::set_thread(JavaThread* thread) {
1027
_thread_saved = nullptr; // Common case.
1028
if (!_is_virtual && thread == nullptr) {
1029
// Save JavaThread* if carrier thread is being detached.
1030
_thread_saved = _thread;