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 #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"
  40 
  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
  43 // underrun error.
  44 static const int UNKNOWN_STACK_DEPTH = -99;
  45 
  46 ///////////////////////////////////////////////////////////////
  47 //
  48 // class JvmtiThreadState
  49 //
  50 // Instances of JvmtiThreadState hang off of each thread.
  51 // Thread local storage for JVMTI.
  52 //
  53 
  54 JvmtiThreadState *JvmtiThreadState::_head = nullptr;
  55 bool JvmtiThreadState::_seen_interp_only_mode = false;
  56 
  57 JvmtiThreadState::JvmtiThreadState(JavaThread* thread, oop thread_oop)
  58   : _thread_event_enable() {
  59   assert(JvmtiThreadState_lock->is_locked(), "sanity check");
  60   _thread               = thread;
  61   _thread_saved         = nullptr;
  62   _exception_state      = ES_CLEARED;
  63   _debuggable           = true;
  64   _hide_single_stepping = false;
  65   _pending_interp_only_mode = false;
  66   _hide_level           = 0;
  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;
  79 
  80   // JVMTI ForceEarlyReturn support
  81   _pending_step_for_earlyret = false;
  82   _earlyret_state = earlyret_inactive;
  83   _earlyret_tos = ilgl;
  84   _earlyret_value.j = 0L;
  85   _earlyret_oop = nullptr;
  86   _jvmti_event_queue = nullptr;
  87   _is_virtual = false;
  88 
  89   _thread_oop_h = OopHandle(JvmtiExport::jvmti_oop_storage(), thread_oop);
  90 
  91   // add all the JvmtiEnvThreadState to the new JvmtiThreadState
  92   {
  93     JvmtiEnvIterator it;
  94     for (JvmtiEnvBase* env = it.first(); env != nullptr; env = it.next(env)) {
  95       if (env->is_valid()) {
  96         add_env(env);
  97       }
  98     }
  99   }
 100 
 101   // link us into the list
 102   {
 103     // The thread state list manipulation code must not have safepoints.
 104     // See periodic_clean_up().
 105     debug_only(NoSafepointVerifier nosafepoint;)
 106 
 107     _prev = nullptr;
 108     _next = _head;
 109     if (_head != nullptr) {
 110       _head->_prev = this;
 111     }
 112     _head = this;
 113   }
 114 
 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);
 118   }
 119 
 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);
 125     }
 126     thread->set_interp_only_mode(0);
 127   }
 128 }
 129 
 130 
 131 JvmtiThreadState::~JvmtiThreadState()   {
 132   assert(JvmtiThreadState_lock->is_locked(), "sanity check");
 133 
 134   if (_classes_being_redefined != nullptr) {
 135     delete _classes_being_redefined; // free the GrowableArray on C heap
 136   }
 137 
 138   // clear this as the state for the thread
 139   get_thread()->set_jvmti_thread_state(nullptr);
 140 
 141   // zap our env thread states
 142   {
 143     JvmtiEnvBase::entering_dying_thread_env_iteration();
 144     JvmtiEnvThreadStateIterator it(this);
 145     for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ) {
 146       JvmtiEnvThreadState* zap = ets;
 147       ets = it.next(ets);
 148       delete zap;
 149     }
 150     JvmtiEnvBase::leaving_dying_thread_env_iteration();
 151   }
 152 
 153   // remove us from the list
 154   {
 155     // The thread state list manipulation code must not have safepoints.
 156     // See periodic_clean_up().
 157     debug_only(NoSafepointVerifier nosafepoint;)
 158 
 159     if (_prev == nullptr) {
 160       assert(_head == this, "sanity check");
 161       _head = _next;
 162     } else {
 163       assert(_head != this, "sanity check");
 164       _prev->_next = _next;
 165     }
 166     if (_next != nullptr) {
 167       _next->_prev = _prev;
 168     }
 169     _next = nullptr;
 170     _prev = nullptr;
 171   }
 172   if (get_thread_oop() != nullptr) {
 173     java_lang_Thread::set_jvmti_thread_state(get_thread_oop(), nullptr);
 174   }
 175   _thread_oop_h.release(JvmtiExport::jvmti_oop_storage());
 176 }
 177 
 178 
 179 void
 180 JvmtiThreadState::periodic_clean_up() {
 181   assert(SafepointSynchronize::is_at_safepoint(), "at safepoint");
 182 
 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()) {
 195         previous_ets = ets;
 196         ets = it.next(ets);
 197       } else {
 198         // This one isn't valid, remove it from the list and deallocate it
 199         JvmtiEnvThreadState* defunct_ets = ets;
 200         ets = ets->next();
 201         if (previous_ets == nullptr) {
 202           assert(state->head_env_thread_state() == defunct_ets, "sanity check");
 203           state->set_head_env_thread_state(ets);
 204         } else {
 205           previous_ets->set_next(ets);
 206         }
 207         delete defunct_ets;
 208       }
 209     }
 210   }
 211 }
 212 
 213 //
 214 // Virtual Threads Mount State transition (VTMS transition) mechanism
 215 //
 216 
 217 // VTMS transitions for one virtual thread are disabled while it is positive
 218 volatile int JvmtiVTMSTransitionDisabler::_VTMS_transition_disable_for_one_count = 0;
 219 
 220 // VTMS transitions for all virtual threads are disabled while it is positive
 221 volatile int JvmtiVTMSTransitionDisabler::_VTMS_transition_disable_for_all_count = 0;
 222 
 223 // There is an active suspender or resumer.
 224 volatile bool JvmtiVTMSTransitionDisabler::_SR_mode = false;
 225 
 226 // Notifications from VirtualThread about VTMS events are enabled.
 227 bool JvmtiVTMSTransitionDisabler::_VTMS_notify_jvmti_events = false;
 228 
 229 // The JvmtiVTMSTransitionDisabler sync protocol is enabled if this count > 0.
 230 volatile int JvmtiVTMSTransitionDisabler::_sync_protocol_enabled_count = 0;
 231 
 232 // JvmtiVTMSTraansitionDisabler sync protocol is enabled permanently after seeing a suspender.
 233 volatile bool JvmtiVTMSTransitionDisabler::_sync_protocol_enabled_permanently = false;
 234 
 235 #ifdef ASSERT
 236 void
 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());
 245     }
 246     ResourceMark rm;
 247     // Handshake with target.
 248     PrintStackTraceClosure pstc;
 249     Handshake::execute(&pstc, java_thread);
 250   }
 251 }
 252 #endif
 253 
 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)
 258 {
 259   if (!Continuations::enabled()) {
 260     return; // JvmtiVTMSTransitionDisabler is no-op without virtual threads
 261   }
 262   if (Thread::current_or_null() == nullptr) {
 263     return;  // Detached thread, can be a call from Agent_OnLoad.
 264   }
 265   if (!sync_protocol_enabled_permanently()) {
 266     JvmtiVTMSTransitionDisabler::inc_sync_protocol_enabled_count();
 267   }
 268   if (_thread != nullptr) {
 269     VTMS_transition_disable_for_one(); // disable VTMS transitions for one virtual thread
 270   } else {
 271     VTMS_transition_disable_for_all(); // disable VTMS transitions for all virtual threads
 272   }
 273 }
 274 
 275 // disable VTMS transitions for all virtual threads
 276 JvmtiVTMSTransitionDisabler::JvmtiVTMSTransitionDisabler(bool is_SR)
 277   : _is_SR(is_SR), _thread(nullptr)
 278 {
 279   if (!Continuations::enabled()) {
 280     return; // JvmtiVTMSTransitionDisabler is no-op without virtual threads
 281   }
 282   if (Thread::current_or_null() == nullptr) {
 283     return;  // Detached thread, can be a call from Agent_OnLoad.
 284   }
 285   if (!sync_protocol_enabled_permanently()) {
 286     JvmtiVTMSTransitionDisabler::inc_sync_protocol_enabled_count();
 287     if (is_SR) {
 288       Atomic::store(&_sync_protocol_enabled_permanently, true);
 289     }
 290   }
 291   VTMS_transition_disable_for_all();
 292 }
 293 
 294 JvmtiVTMSTransitionDisabler::~JvmtiVTMSTransitionDisabler() {
 295   if (!Continuations::enabled()) {
 296     return; // JvmtiVTMSTransitionDisabler is a no-op without virtual threads
 297   }
 298   if (Thread::current_or_null() == nullptr) {
 299     return;  // Detached thread, can be a call from Agent_OnLoad.
 300   }
 301   if (_thread != nullptr) {
 302     VTMS_transition_enable_for_one(); // enable VTMS transitions for one virtual thread
 303   } else {
 304     VTMS_transition_enable_for_all(); // enable VTMS transitions for all virtual threads
 305   }
 306   if (!sync_protocol_enabled_permanently()) {
 307     JvmtiVTMSTransitionDisabler::dec_sync_protocol_enabled_count();
 308   }
 309 }
 310 
 311 // disable VTMS transitions for one virtual thread
 312 void
 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
 320   }
 321   MonitorLocker ml(JvmtiVTMSTransition_lock);
 322 
 323   while (_SR_mode) { // suspender or resumer is a JvmtiVTMSTransitionDisabler monopolist
 324     ml.wait(10); // wait while there is an active suspender or resumer
 325   }
 326   Atomic::inc(&_VTMS_transition_disable_for_one_count);
 327   java_lang_Thread::inc_VTMS_transition_disable_count(vth());
 328 
 329   while (java_lang_Thread::is_in_VTMS_transition(vth())) {
 330     ml.wait(10); // wait while the virtual thread is in transition
 331   }
 332 #ifdef ASSERT
 333   thread->set_is_VTMS_transition_disabler(true);
 334 #endif
 335 }
 336 
 337 // disable VTMS transitions for all virtual threads
 338 void
 339 JvmtiVTMSTransitionDisabler::VTMS_transition_disable_for_all() {
 340   JavaThread* thread = JavaThread::current();
 341   int attempts = 50000;
 342   {
 343     MonitorLocker ml(JvmtiVTMSTransition_lock);
 344 
 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.
 349     }
 350     if (_is_SR) {
 351       _SR_mode = true;
 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.
 355       }
 356     }
 357     Atomic::inc(&_VTMS_transition_disable_for_all_count);
 358 
 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()) {
 363         if (ml.wait(10)) {
 364           attempts--;
 365         }
 366         DEBUG_ONLY(if (attempts == 0) break;)
 367       }
 368     }
 369     assert(!thread->is_VTMS_transition_disabler(), "VTMS_transition sanity check");
 370 #ifdef ASSERT
 371     if (attempts > 0) {
 372       thread->set_is_VTMS_transition_disabler(true);
 373     }
 374 #endif
 375   }
 376 #ifdef ASSERT
 377     if (attempts == 0) {
 378       print_info();
 379       fatal("stuck in JvmtiVTMSTransitionDisabler::VTMS_transition_disable");
 380     }
 381 #endif
 382 }
 383 
 384 // enable VTMS transitions for one virtual thread
 385 void
 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
 392   }
 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) {
 397     ml.notify_all();
 398   }
 399 #ifdef ASSERT
 400   thread->set_is_VTMS_transition_disabler(false);
 401 #endif
 402 }
 403 
 404 // enable VTMS transitions for all virtual threads
 405 void
 406 JvmtiVTMSTransitionDisabler::VTMS_transition_enable_for_all() {
 407   JavaThread* current = JavaThread::current();
 408   {
 409     MonitorLocker ml(JvmtiVTMSTransition_lock);
 410     assert(_VTMS_transition_disable_for_all_count > 0, "VTMS_transition sanity check");
 411 
 412     if (_is_SR) {  // Disabler is suspender or resumer.
 413       _SR_mode = false;
 414     }
 415     Atomic::dec(&_VTMS_transition_disable_for_all_count);
 416     if (_VTMS_transition_disable_for_all_count == 0 || _is_SR) {
 417       ml.notify_all();
 418     }
 419 #ifdef ASSERT
 420     current->set_is_VTMS_transition_disabler(false);
 421 #endif
 422   }
 423 }
 424 
 425 void
 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");
 430 
 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);
 436 
 437   if (!sync_protocol_enabled()) {
 438     thread->set_is_in_VTMS_transition(true);
 439     return;
 440   }
 441   HandleMark hm(thread);
 442   Handle vth = Handle(thread, vt);
 443   int attempts = 50000;
 444 
 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.
 448 
 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)
 453   ) {
 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);
 458 
 459     while (true) {
 460       MonitorLocker ml(JvmtiVTMSTransition_lock);
 461 
 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)
 468       ) {
 469         // Block while transitions are disabled or there are suspend requests.
 470         if (ml.wait(10)) {
 471           attempts--;
 472         }
 473         DEBUG_ONLY(if (attempts == 0) break;)
 474         continue;  // ~ThreadBlockInVM has handshake-based suspend point.
 475       }
 476       thread->set_VTMS_transition_mark(true);
 477       java_lang_Thread::set_is_in_VTMS_transition(vth(), true);
 478       break;
 479     }
 480   }
 481 #ifdef ASSERT
 482   if (attempts == 0) {
 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));
 485     print_info();
 486     fatal("stuck in JvmtiVTMSTransitionDisabler::start_VTMS_transition");
 487   }
 488 #endif
 489   // Enter VTMS transition section.
 490   thread->set_is_in_VTMS_transition(true);
 491 }
 492 
 493 void
 494 JvmtiVTMSTransitionDisabler::finish_VTMS_transition(jthread vthread, bool is_mount) {
 495   JavaThread* thread = JavaThread::current();
 496 
 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);
 503 
 504   if (!sync_protocol_enabled()) {
 505     return;
 506   }
 507   int64_t thread_id = java_lang_Thread::thread_id(vt);
 508 
 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);
 513     ml.notify_all();
 514   }
 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()) {
 519     while (true) {
 520       MonitorLocker ml(JvmtiVTMSTransition_lock);
 521 
 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))
 525       ) {
 526         // Block while there are suspend requests.
 527         if (ml.wait(10)) {
 528           attempts--;
 529         }
 530         DEBUG_ONLY(if (attempts == 0) break;)
 531         continue;
 532       }
 533       break;
 534     }
 535   }
 536 #ifdef ASSERT
 537   if (attempts == 0) {
 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));
 540     print_info();
 541     fatal("stuck in JvmtiVTMSTransitionDisabler::finish_VTMS_transition");
 542   }
 543 #endif
 544 }
 545 
 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);
 551 }
 552 
 553 void
 554 JvmtiVTMSTransitionDisabler::VTMS_vthread_start(jobject vthread) {
 555   VTMS_mount_end(vthread);
 556   JavaThread* thread = JavaThread::current();
 557 
 558   assert(!thread->is_in_VTMS_transition(), "sanity check");
 559   assert(!thread->is_in_tmp_VTMS_transition(), "sanity check");
 560 
 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);
 570   }
 571   if (JvmtiExport::should_post_vthread_start()) {
 572     JvmtiExport::post_vthread_start(vthread);
 573   }
 574   // post VirtualThreadMount event after VirtualThreadStart
 575   if (JvmtiExport::should_post_vthread_mount()) {
 576     JvmtiExport::post_vthread_mount(vthread);
 577   }
 578 }
 579 
 580 void
 581 JvmtiVTMSTransitionDisabler::VTMS_vthread_end(jobject vthread) {
 582   JavaThread* thread = JavaThread::current();
 583 
 584   assert(!thread->is_in_VTMS_transition(), "sanity check");
 585   assert(!thread->is_in_tmp_VTMS_transition(), "sanity check");
 586 
 587   // post VirtualThreadUnmount event before VirtualThreadEnd
 588   if (JvmtiExport::should_post_vthread_unmount()) {
 589     JvmtiExport::post_vthread_unmount(vthread);
 590   }
 591   if (JvmtiExport::should_post_vthread_end()) {
 592     JvmtiExport::post_vthread_end(vthread);
 593   }
 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");
 599   }
 600   thread->rebind_to_jvmti_thread_state_of(thread->threadObj());
 601 }
 602 
 603 void
 604 JvmtiVTMSTransitionDisabler::VTMS_vthread_mount(jobject vthread, bool hide) {
 605   if (hide) {
 606     VTMS_mount_begin(vthread);
 607   } else {
 608     VTMS_mount_end(vthread);
 609     if (JvmtiExport::should_post_vthread_mount()) {
 610       JvmtiExport::post_vthread_mount(vthread);
 611     }
 612   }
 613 }
 614 
 615 void
 616 JvmtiVTMSTransitionDisabler::VTMS_vthread_unmount(jobject vthread, bool hide) {
 617   if (hide) {
 618     if (JvmtiExport::should_post_vthread_unmount()) {
 619       JvmtiExport::post_vthread_unmount(vthread);
 620     }
 621     VTMS_unmount_begin(vthread, /* last_unmount */ false);
 622   } else {
 623     VTMS_unmount_end(vthread);
 624   }
 625 }
 626 
 627 void
 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);
 633 }
 634 
 635 void
 636 JvmtiVTMSTransitionDisabler::VTMS_mount_end(jobject vthread) {
 637   JavaThread* thread = JavaThread::current();
 638   oop vt = JNIHandles::resolve(vthread);
 639 
 640   thread->rebind_to_jvmti_thread_state_of(vt);
 641 
 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);
 645 }
 646 
 647 void
 648 JvmtiVTMSTransitionDisabler::VTMS_unmount_begin(jobject vthread, bool last_unmount) {
 649   JavaThread* thread = JavaThread::current();
 650 
 651   assert(!thread->is_in_tmp_VTMS_transition(), "sanity check");
 652   assert(!thread->is_in_VTMS_transition(), "sanity check");
 653 
 654   start_VTMS_transition(vthread, /* is_mount */ false);
 655   if (!last_unmount) {
 656     thread->rebind_to_jvmti_thread_state_of(thread->threadObj());
 657   }
 658 }
 659 
 660 void
 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);
 666 
 667   if (thread->pending_jvmti_unmount_event()) {
 668     assert(java_lang_VirtualThread::is_preempted(JNIHandles::resolve(vthread)), "should be marked preempted");
 669     JvmtiExport::post_vthread_unmount(vthread);
 670     thread->set_pending_jvmti_unmount_event(false);
 671   }
 672 }
 673 
 674 
 675 //
 676 // Virtual Threads Suspend/Resume management
 677 //
 678 
 679 JvmtiVTSuspender::SR_Mode
 680 JvmtiVTSuspender::_SR_mode = SR_none;
 681 
 682 VirtualThreadList*
 683 JvmtiVTSuspender::_suspended_list = new VirtualThreadList();
 684 
 685 VirtualThreadList*
 686 JvmtiVTSuspender::_not_suspended_list = new VirtualThreadList();
 687 
 688 void
 689 JvmtiVTSuspender::register_all_vthreads_suspend() {
 690   MonitorLocker ml(JvmtiVTMSTransition_lock);
 691 
 692   _SR_mode = SR_all;
 693   _suspended_list->invalidate();
 694   _not_suspended_list->invalidate();
 695 }
 696 
 697 void
 698 JvmtiVTSuspender::register_all_vthreads_resume() {
 699   MonitorLocker ml(JvmtiVTMSTransition_lock);
 700 
 701   _SR_mode = SR_none;
 702   _suspended_list->invalidate();
 703   _not_suspended_list->invalidate();
 704 }
 705 
 706 void
 707 JvmtiVTSuspender::register_vthread_suspend(oop vt) {
 708   int64_t id = java_lang_Thread::thread_id(vt);
 709   MonitorLocker ml(JvmtiVTMSTransition_lock);
 710 
 711   if (_SR_mode == SR_all) {
 712     assert(_not_suspended_list->contains(id),
 713            "register_vthread_suspend sanity check");
 714     _not_suspended_list->remove(id);
 715   } else {
 716     assert(!_suspended_list->contains(id),
 717            "register_vthread_suspend sanity check");
 718     _SR_mode = SR_ind;
 719     _suspended_list->append(id);
 720   }
 721 }
 722 
 723 void
 724 JvmtiVTSuspender::register_vthread_resume(oop vt) {
 725   int64_t id = java_lang_Thread::thread_id(vt);
 726   MonitorLocker ml(JvmtiVTMSTransition_lock);
 727 
 728   if (_SR_mode == SR_all) {
 729     assert(!_not_suspended_list->contains(id),
 730            "register_vthread_resume sanity check");
 731     _not_suspended_list->append(id);
 732   } else if (_SR_mode == SR_ind) {
 733     assert(_suspended_list->contains(id),
 734            "register_vthread_resume check");
 735     _suspended_list->remove(id);
 736     if (_suspended_list->length() == 0) {
 737       _SR_mode = SR_none;
 738     }
 739   } else {
 740     assert(false, "register_vthread_resume: no suspend mode enabled");
 741   }
 742 }
 743 
 744 bool
 745 JvmtiVTSuspender::is_vthread_suspended(int64_t thread_id) {
 746   bool suspend_is_needed =
 747    (_SR_mode == SR_all && !_not_suspended_list->contains(thread_id)) ||
 748    (_SR_mode == SR_ind && _suspended_list->contains(thread_id));
 749 
 750   return suspend_is_needed;
 751 }
 752 
 753 bool
 754 JvmtiVTSuspender::is_vthread_suspended(oop vt) {
 755   return is_vthread_suspended(java_lang_Thread::thread_id(vt));
 756 }
 757 
 758 void JvmtiThreadState::add_env(JvmtiEnvBase *env) {
 759   assert(JvmtiThreadState_lock->is_locked(), "sanity check");
 760 
 761   JvmtiEnvThreadState *new_ets = new JvmtiEnvThreadState(this, env);
 762   // add this environment thread state to the end of the list (order is important)
 763   {
 764     // list deallocation (which occurs at a safepoint) cannot occur simultaneously
 765     debug_only(NoSafepointVerifier nosafepoint;)
 766 
 767     JvmtiEnvThreadStateIterator it(this);
 768     JvmtiEnvThreadState* previous_ets = nullptr;
 769     for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
 770       previous_ets = ets;
 771     }
 772     if (previous_ets == nullptr) {
 773       set_head_env_thread_state(new_ets);
 774     } else {
 775       previous_ets->set_next(new_ets);
 776     }
 777   }
 778 }
 779 
 780 void JvmtiThreadState::enter_interp_only_mode() {
 781   assert(_thread != nullptr, "sanity check");
 782   _seen_interp_only_mode = true;
 783   _thread->increment_interp_only_mode();
 784   invalidate_cur_stack_depth();
 785 }
 786 
 787 void JvmtiThreadState::leave_interp_only_mode() {
 788   assert(is_interp_only_mode(), "leaving interp only when not in interp only mode");
 789   if (_thread == nullptr) {
 790     // Unmounted virtual thread updates the saved value.
 791     --_saved_interp_only_mode;
 792   } else {
 793     _thread->decrement_interp_only_mode();
 794   }
 795 }
 796 
 797 
 798 // Helper routine used in several places
 799 int JvmtiThreadState::count_frames() {
 800   JavaThread* thread = get_thread_or_saved();
 801   javaVFrame *jvf;
 802   ResourceMark rm;
 803   if (thread == nullptr) {
 804     oop thread_obj = get_thread_oop();
 805     jvf = JvmtiEnvBase::get_vthread_jvf(thread_obj);
 806   } else {
 807 #ifdef ASSERT
 808     Thread *current_thread = Thread::current();
 809 #endif
 810     assert(SafepointSynchronize::is_at_safepoint() ||
 811            thread->is_handshake_safe_for(current_thread),
 812            "call by myself / at safepoint / at handshake");
 813     if (!thread->has_last_Java_frame()) return 0;  // No Java frames.
 814     // TBD: This might need to be corrected for detached carrier threads.
 815     RegisterMap reg_map(thread,
 816                         RegisterMap::UpdateMap::skip,
 817                         RegisterMap::ProcessFrames::skip,
 818                         RegisterMap::WalkContinuation::include);
 819     jvf = thread->last_java_vframe(&reg_map);
 820     jvf = JvmtiEnvBase::check_and_skip_hidden_frames(thread, jvf);
 821   }
 822   return (int)JvmtiEnvBase::get_frame_count(jvf);
 823 }
 824 
 825 
 826 void JvmtiThreadState::invalidate_cur_stack_depth() {
 827   assert(SafepointSynchronize::is_at_safepoint() ||
 828          get_thread()->is_handshake_safe_for(Thread::current()),
 829          "bad synchronization with owner thread");
 830 
 831   _cur_stack_depth = UNKNOWN_STACK_DEPTH;
 832 }
 833 
 834 void JvmtiThreadState::incr_cur_stack_depth() {
 835   guarantee(JavaThread::current() == get_thread(), "must be current thread");
 836 
 837   if (!is_interp_only_mode()) {
 838     _cur_stack_depth = UNKNOWN_STACK_DEPTH;
 839   }
 840   if (_cur_stack_depth != UNKNOWN_STACK_DEPTH) {
 841     ++_cur_stack_depth;
 842   }
 843 }
 844 
 845 void JvmtiThreadState::decr_cur_stack_depth() {
 846   guarantee(JavaThread::current() == get_thread(), "must be current thread");
 847 
 848   if (!is_interp_only_mode()) {
 849     _cur_stack_depth = UNKNOWN_STACK_DEPTH;
 850   }
 851   if (_cur_stack_depth != UNKNOWN_STACK_DEPTH) {
 852     --_cur_stack_depth;
 853     assert(_cur_stack_depth >= 0, "incr/decr_cur_stack_depth mismatch");
 854   }
 855 }
 856 
 857 int JvmtiThreadState::cur_stack_depth() {
 858   Thread *current = Thread::current();
 859   guarantee(get_thread()->is_handshake_safe_for(current),
 860             "must be current thread or direct handshake");
 861 
 862   if (!is_interp_only_mode() || _cur_stack_depth == UNKNOWN_STACK_DEPTH) {
 863     _cur_stack_depth = count_frames();
 864   } else {
 865 #ifdef ASSERT
 866     if (EnableJVMTIStackDepthAsserts) {
 867       // heavy weight assert
 868       jint num_frames = count_frames();
 869       assert(_cur_stack_depth == num_frames, "cur_stack_depth out of sync _cur_stack_depth: %d num_frames: %d", _cur_stack_depth, num_frames);
 870     }
 871 #endif
 872   }
 873   return _cur_stack_depth;
 874 }
 875 
 876 void JvmtiThreadState::process_pending_step_for_popframe() {
 877   // We are single stepping as the last part of the PopFrame() dance
 878   // so we have some house keeping to do.
 879 
 880   JavaThread *thr = get_thread();
 881   if (thr->popframe_condition() != JavaThread::popframe_inactive) {
 882     // If the popframe_condition field is not popframe_inactive, then
 883     // we missed all of the popframe_field cleanup points:
 884     //
 885     // - unpack_frames() was not called (nothing to deopt)
 886     // - remove_activation_preserving_args_entry() was not called
 887     //   (did not get suspended in a call_vm() family call and did
 888     //   not complete a call_vm() family call on the way here)
 889     thr->clear_popframe_condition();
 890   }
 891 
 892   // clearing the flag indicates we are done with the PopFrame() dance
 893   clr_pending_step_for_popframe();
 894 
 895   // If exception was thrown in this frame, need to reset jvmti thread state.
 896   // Single stepping may not get enabled correctly by the agent since
 897   // exception state is passed in MethodExit event which may be sent at some
 898   // time in the future. JDWP agent ignores MethodExit events if caused by
 899   // an exception.
 900   //
 901   if (is_exception_detected()) {
 902     clear_exception_state();
 903   }
 904   // If step is pending for popframe then it may not be
 905   // a repeat step. The new_bci and method_id is same as current_bci
 906   // and current method_id after pop and step for recursive calls.
 907   // Force the step by clearing the last location.
 908   JvmtiEnvThreadStateIterator it(this);
 909   for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
 910     ets->clear_current_location();
 911   }
 912 }
 913 
 914 
 915 // Class:     JvmtiThreadState
 916 // Function:  update_for_pop_top_frame
 917 // Description:
 918 //   This function removes any frame pop notification request for
 919 //   the top frame and invalidates both the current stack depth and
 920 //   all cached frameIDs.
 921 //
 922 // Called by: PopFrame
 923 //
 924 void JvmtiThreadState::update_for_pop_top_frame() {
 925   if (is_interp_only_mode()) {
 926     // remove any frame pop notification request for the top frame
 927     // in any environment
 928     int popframe_number = cur_stack_depth();
 929     {
 930       JvmtiEnvThreadStateIterator it(this);
 931       for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
 932         if (ets->is_frame_pop(popframe_number)) {
 933           ets->clear_frame_pop(popframe_number);
 934         }
 935       }
 936     }
 937     // force stack depth to be recalculated
 938     invalidate_cur_stack_depth();
 939   } else {
 940     assert(!is_enabled(JVMTI_EVENT_FRAME_POP), "Must have no framepops set");
 941   }
 942 }
 943 
 944 
 945 void JvmtiThreadState::process_pending_step_for_earlyret() {
 946   // We are single stepping as the last part of the ForceEarlyReturn
 947   // dance so we have some house keeping to do.
 948 
 949   if (is_earlyret_pending()) {
 950     // If the earlyret_state field is not earlyret_inactive, then
 951     // we missed all of the earlyret_field cleanup points:
 952     //
 953     // - remove_activation() was not called
 954     //   (did not get suspended in a call_vm() family call and did
 955     //   not complete a call_vm() family call on the way here)
 956     //
 957     // One legitimate way for us to miss all the cleanup points is
 958     // if we got here right after handling a compiled return. If that
 959     // is the case, then we consider our return from compiled code to
 960     // complete the ForceEarlyReturn request and we clear the condition.
 961     clr_earlyret_pending();
 962     set_earlyret_oop(nullptr);
 963     clr_earlyret_value();
 964   }
 965 
 966   // clearing the flag indicates we are done with
 967   // the ForceEarlyReturn() dance
 968   clr_pending_step_for_earlyret();
 969 
 970   // If exception was thrown in this frame, need to reset jvmti thread state.
 971   // Single stepping may not get enabled correctly by the agent since
 972   // exception state is passed in MethodExit event which may be sent at some
 973   // time in the future. JDWP agent ignores MethodExit events if caused by
 974   // an exception.
 975   //
 976   if (is_exception_detected()) {
 977     clear_exception_state();
 978   }
 979   // If step is pending for earlyret then it may not be a repeat step.
 980   // The new_bci and method_id is same as current_bci and current
 981   // method_id after earlyret and step for recursive calls.
 982   // Force the step by clearing the last location.
 983   JvmtiEnvThreadStateIterator it(this);
 984   for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
 985     ets->clear_current_location();
 986   }
 987 }
 988 
 989 void JvmtiThreadState::oops_do(OopClosure* f, NMethodClosure* cf) {
 990   f->do_oop((oop*) &_earlyret_oop);
 991 
 992   // Keep nmethods from unloading on the event queue
 993   if (_jvmti_event_queue != nullptr) {
 994     _jvmti_event_queue->oops_do(f, cf);
 995   }
 996 }
 997 
 998 void JvmtiThreadState::nmethods_do(NMethodClosure* cf) {
 999   // Keep nmethods from unloading on the event queue
1000   if (_jvmti_event_queue != nullptr) {
1001     _jvmti_event_queue->nmethods_do(cf);
1002   }
1003 }
1004 
1005 // Thread local event queue.
1006 void JvmtiThreadState::enqueue_event(JvmtiDeferredEvent* event) {
1007   if (_jvmti_event_queue == nullptr) {
1008     _jvmti_event_queue = new JvmtiDeferredEventQueue();
1009   }
1010   // copy the event
1011   _jvmti_event_queue->enqueue(*event);
1012 }
1013 
1014 void JvmtiThreadState::post_events(JvmtiEnv* env) {
1015   if (_jvmti_event_queue != nullptr) {
1016     _jvmti_event_queue->post(env);  // deletes each queue node
1017     delete _jvmti_event_queue;
1018     _jvmti_event_queue = nullptr;
1019   }
1020 }
1021 
1022 void JvmtiThreadState::run_nmethod_entry_barriers() {
1023   if (_jvmti_event_queue != nullptr) {
1024     _jvmti_event_queue->run_nmethod_entry_barriers();
1025   }
1026 }
1027 
1028 oop JvmtiThreadState::get_thread_oop() {
1029   return _thread_oop_h.resolve();
1030 }
1031 
1032 void JvmtiThreadState::set_thread(JavaThread* thread) {
1033   _thread_saved = nullptr;  // Common case.
1034   if (!_is_virtual && thread == nullptr) {
1035     // Save JavaThread* if carrier thread is being detached.
1036     _thread_saved = _thread;
1037   }
1038   _thread = thread;
1039 }