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/classLoaderDataGraph.hpp"
  27 #include "classfile/javaClasses.inline.hpp"
  28 #include "classfile/moduleEntry.hpp"
  29 #include "classfile/symbolTable.hpp"
  30 #include "classfile/vmSymbols.hpp"
  31 #include "jvmtifiles/jvmtiEnv.hpp"
  32 #include "memory/iterator.hpp"
  33 #include "memory/resourceArea.hpp"
  34 #include "oops/klass.inline.hpp"
  35 #include "oops/objArrayKlass.hpp"
  36 #include "oops/objArrayOop.hpp"
  37 #include "oops/oop.inline.hpp"
  38 #include "oops/oopHandle.inline.hpp"
  39 #include "prims/jvmtiEnvBase.hpp"
  40 #include "prims/jvmtiEventController.inline.hpp"
  41 #include "prims/jvmtiExtensions.hpp"
  42 #include "prims/jvmtiImpl.hpp"
  43 #include "prims/jvmtiManageCapabilities.hpp"
  44 #include "prims/jvmtiTagMap.hpp"
  45 #include "prims/jvmtiThreadState.inline.hpp"
  46 #include "runtime/continuationEntry.inline.hpp"
  47 #include "runtime/deoptimization.hpp"
  48 #include "runtime/frame.inline.hpp"
  49 #include "runtime/handles.inline.hpp"
  50 #include "runtime/interfaceSupport.inline.hpp"
  51 #include "runtime/javaCalls.hpp"
  52 #include "runtime/javaThread.inline.hpp"
  53 #include "runtime/jfieldIDWorkaround.hpp"
  54 #include "runtime/jniHandles.inline.hpp"
  55 #include "runtime/objectMonitor.inline.hpp"
  56 #include "runtime/osThread.hpp"
  57 #include "runtime/signature.hpp"
  58 #include "runtime/stackWatermarkSet.inline.hpp"
  59 #include "runtime/synchronizer.inline.hpp"
  60 #include "runtime/threads.hpp"
  61 #include "runtime/threadSMR.inline.hpp"
  62 #include "runtime/vframe.inline.hpp"
  63 #include "runtime/vframe_hp.hpp"
  64 #include "runtime/vmThread.hpp"
  65 #include "runtime/vmOperations.hpp"
  66 #include "services/threadService.hpp"
  67 
  68 
  69 ///////////////////////////////////////////////////////////////
  70 //
  71 // JvmtiEnvBase
  72 //
  73 
  74 JvmtiEnvBase* JvmtiEnvBase::_head_environment = nullptr;
  75 
  76 bool JvmtiEnvBase::_globally_initialized = false;
  77 volatile bool JvmtiEnvBase::_needs_clean_up = false;
  78 
  79 jvmtiPhase JvmtiEnvBase::_phase = JVMTI_PHASE_PRIMORDIAL;
  80 
  81 volatile int JvmtiEnvBase::_dying_thread_env_iteration_count = 0;
  82 
  83 extern jvmtiInterface_1_ jvmti_Interface;
  84 extern jvmtiInterface_1_ jvmtiTrace_Interface;
  85 
  86 
  87 // perform initializations that must occur before any JVMTI environments
  88 // are released but which should only be initialized once (no matter
  89 // how many environments are created).
  90 void
  91 JvmtiEnvBase::globally_initialize() {
  92   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
  93   assert(_globally_initialized == false, "bad call");
  94 
  95   JvmtiManageCapabilities::initialize();
  96 
  97   // register extension functions and events
  98   JvmtiExtensions::register_extensions();
  99 
 100 #ifdef JVMTI_TRACE
 101   JvmtiTrace::initialize();
 102 #endif
 103 
 104   _globally_initialized = true;
 105 }
 106 
 107 
 108 void
 109 JvmtiEnvBase::initialize() {
 110   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
 111 
 112   // Add this environment to the end of the environment list (order is important)
 113   {
 114     // This block of code must not contain any safepoints, as list deallocation
 115     // (which occurs at a safepoint) cannot occur simultaneously with this list
 116     // addition.  Note: NoSafepointVerifier cannot, currently, be used before
 117     // threads exist.
 118     JvmtiEnvIterator it;
 119     JvmtiEnvBase *previous_env = nullptr;
 120     for (JvmtiEnvBase* env = it.first(); env != nullptr; env = it.next(env)) {
 121       previous_env = env;
 122     }
 123     if (previous_env == nullptr) {
 124       _head_environment = this;
 125     } else {
 126       previous_env->set_next_environment(this);
 127     }
 128   }
 129 
 130   if (_globally_initialized == false) {
 131     globally_initialize();
 132   }
 133 }
 134 
 135 jvmtiPhase
 136 JvmtiEnvBase::phase() {
 137   // For the JVMTI environments possessed the can_generate_early_vmstart:
 138   //   replace JVMTI_PHASE_PRIMORDIAL with JVMTI_PHASE_START
 139   if (_phase == JVMTI_PHASE_PRIMORDIAL &&
 140       JvmtiExport::early_vmstart_recorded() &&
 141       early_vmstart_env()) {
 142     return JVMTI_PHASE_START;
 143   }
 144   return _phase; // Normal case
 145 }
 146 
 147 bool
 148 JvmtiEnvBase::is_valid() {
 149   jlong value = 0;
 150 
 151   // This object might not be a JvmtiEnvBase so we can't assume
 152   // the _magic field is properly aligned. Get the value in a safe
 153   // way and then check against JVMTI_MAGIC.
 154 
 155   switch (sizeof(_magic)) {
 156   case 2:
 157     value = Bytes::get_native_u2((address)&_magic);
 158     break;
 159 
 160   case 4:
 161     value = Bytes::get_native_u4((address)&_magic);
 162     break;
 163 
 164   case 8:
 165     value = Bytes::get_native_u8((address)&_magic);
 166     break;
 167 
 168   default:
 169     guarantee(false, "_magic field is an unexpected size");
 170   }
 171 
 172   return value == JVMTI_MAGIC;
 173 }
 174 
 175 
 176 bool
 177 JvmtiEnvBase::use_version_1_0_semantics() {
 178   int major, minor, micro;
 179 
 180   JvmtiExport::decode_version_values(_version, &major, &minor, &micro);
 181   return major == 1 && minor == 0;  // micro version doesn't matter here
 182 }
 183 
 184 
 185 bool
 186 JvmtiEnvBase::use_version_1_1_semantics() {
 187   int major, minor, micro;
 188 
 189   JvmtiExport::decode_version_values(_version, &major, &minor, &micro);
 190   return major == 1 && minor == 1;  // micro version doesn't matter here
 191 }
 192 
 193 bool
 194 JvmtiEnvBase::use_version_1_2_semantics() {
 195   int major, minor, micro;
 196 
 197   JvmtiExport::decode_version_values(_version, &major, &minor, &micro);
 198   return major == 1 && minor == 2;  // micro version doesn't matter here
 199 }
 200 
 201 
 202 JvmtiEnvBase::JvmtiEnvBase(jint version) : _env_event_enable() {
 203   _version = version;
 204   _env_local_storage = nullptr;
 205   _tag_map = nullptr;
 206   _native_method_prefix_count = 0;
 207   _native_method_prefixes = nullptr;
 208   _next = nullptr;
 209   _class_file_load_hook_ever_enabled = false;
 210 
 211   // Moot since ClassFileLoadHook not yet enabled.
 212   // But "true" will give a more predictable ClassFileLoadHook behavior
 213   // for environment creation during ClassFileLoadHook.
 214   _is_retransformable = true;
 215 
 216   // all callbacks initially null
 217   memset(&_event_callbacks, 0, sizeof(jvmtiEventCallbacks));
 218   memset(&_ext_event_callbacks, 0, sizeof(jvmtiExtEventCallbacks));
 219 
 220   // all capabilities initially off
 221   memset(&_current_capabilities, 0, sizeof(_current_capabilities));
 222 
 223   // all prohibited capabilities initially off
 224   memset(&_prohibited_capabilities, 0, sizeof(_prohibited_capabilities));
 225 
 226   _magic = JVMTI_MAGIC;
 227 
 228   JvmtiEventController::env_initialize((JvmtiEnv*)this);
 229 
 230 #ifdef JVMTI_TRACE
 231   _jvmti_external.functions = TraceJVMTI != nullptr ? &jvmtiTrace_Interface : &jvmti_Interface;
 232 #else
 233   _jvmti_external.functions = &jvmti_Interface;
 234 #endif
 235 }
 236 
 237 
 238 void
 239 JvmtiEnvBase::dispose() {
 240 
 241 #ifdef JVMTI_TRACE
 242   JvmtiTrace::shutdown();
 243 #endif
 244 
 245   // Dispose of event info and let the event controller call us back
 246   // in a locked state (env_dispose, below)
 247   JvmtiEventController::env_dispose(this);
 248 }
 249 
 250 void
 251 JvmtiEnvBase::env_dispose() {
 252   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
 253 
 254   // We have been entered with all events disabled on this environment.
 255   // A race to re-enable events (by setting callbacks) is prevented by
 256   // checking for a valid environment when setting callbacks (while
 257   // holding the JvmtiThreadState_lock).
 258 
 259   // Mark as invalid.
 260   _magic = DISPOSED_MAGIC;
 261 
 262   // Relinquish all capabilities.
 263   jvmtiCapabilities *caps = get_capabilities();
 264   JvmtiManageCapabilities::relinquish_capabilities(caps, caps, caps);
 265 
 266   // Same situation as with events (see above)
 267   set_native_method_prefixes(0, nullptr);
 268 
 269   JvmtiTagMap* tag_map_to_clear = tag_map_acquire();
 270   // A tag map can be big, clear it now to save memory until
 271   // the destructor runs.
 272   if (tag_map_to_clear != nullptr) {
 273     tag_map_to_clear->clear();
 274   }
 275 
 276   _needs_clean_up = true;
 277 }
 278 
 279 
 280 JvmtiEnvBase::~JvmtiEnvBase() {
 281   assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
 282 
 283   // There is a small window of time during which the tag map of a
 284   // disposed environment could have been reallocated.
 285   // Make sure it is gone.
 286   JvmtiTagMap* tag_map_to_deallocate = _tag_map;
 287   set_tag_map(nullptr);
 288   // A tag map can be big, deallocate it now
 289   if (tag_map_to_deallocate != nullptr) {
 290     delete tag_map_to_deallocate;
 291   }
 292 
 293   _magic = BAD_MAGIC;
 294 }
 295 
 296 
 297 void
 298 JvmtiEnvBase::periodic_clean_up() {
 299   assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
 300 
 301   // JvmtiEnvBase reference is saved in JvmtiEnvThreadState. So
 302   // clean up JvmtiThreadState before deleting JvmtiEnv pointer.
 303   JvmtiThreadState::periodic_clean_up();
 304 
 305   // Unlink all invalid environments from the list of environments
 306   // and deallocate them
 307   JvmtiEnvIterator it;
 308   JvmtiEnvBase* previous_env = nullptr;
 309   JvmtiEnvBase* env = it.first();
 310   while (env != nullptr) {
 311     if (env->is_valid()) {
 312       previous_env = env;
 313       env = it.next(env);
 314     } else {
 315       // This one isn't valid, remove it from the list and deallocate it
 316       JvmtiEnvBase* defunct_env = env;
 317       env = it.next(env);
 318       if (previous_env == nullptr) {
 319         _head_environment = env;
 320       } else {
 321         previous_env->set_next_environment(env);
 322       }
 323       delete defunct_env;
 324     }
 325   }
 326 
 327 }
 328 
 329 
 330 void
 331 JvmtiEnvBase::check_for_periodic_clean_up() {
 332   assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
 333 
 334   class ThreadInsideIterationClosure: public ThreadClosure {
 335    private:
 336     bool _inside;
 337    public:
 338     ThreadInsideIterationClosure() : _inside(false) {};
 339 
 340     void do_thread(Thread* thread) {
 341       _inside |= thread->is_inside_jvmti_env_iteration();
 342     }
 343 
 344     bool is_inside_jvmti_env_iteration() {
 345       return _inside;
 346     }
 347   };
 348 
 349   if (_needs_clean_up) {
 350     // Check if we are currently iterating environment,
 351     // deallocation should not occur if we are
 352     ThreadInsideIterationClosure tiic;
 353     Threads::threads_do(&tiic);
 354     if (!tiic.is_inside_jvmti_env_iteration() &&
 355              !is_inside_dying_thread_env_iteration()) {
 356       _needs_clean_up = false;
 357       JvmtiEnvBase::periodic_clean_up();
 358     }
 359   }
 360 }
 361 
 362 
 363 void
 364 JvmtiEnvBase::record_first_time_class_file_load_hook_enabled() {
 365   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(),
 366          "sanity check");
 367 
 368   if (!_class_file_load_hook_ever_enabled) {
 369     _class_file_load_hook_ever_enabled = true;
 370 
 371     if (get_capabilities()->can_retransform_classes) {
 372       _is_retransformable = true;
 373     } else {
 374       _is_retransformable = false;
 375 
 376       // cannot add retransform capability after ClassFileLoadHook has been enabled
 377       get_prohibited_capabilities()->can_retransform_classes = 1;
 378     }
 379   }
 380 }
 381 
 382 
 383 void
 384 JvmtiEnvBase::record_class_file_load_hook_enabled() {
 385   if (!_class_file_load_hook_ever_enabled) {
 386     if (Threads::number_of_threads() == 0) {
 387       record_first_time_class_file_load_hook_enabled();
 388     } else {
 389       MutexLocker mu(JvmtiThreadState_lock);
 390       record_first_time_class_file_load_hook_enabled();
 391     }
 392   }
 393 }
 394 
 395 
 396 jvmtiError
 397 JvmtiEnvBase::set_native_method_prefixes(jint prefix_count, char** prefixes) {
 398   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(),
 399          "sanity check");
 400 
 401   int old_prefix_count = get_native_method_prefix_count();
 402   char **old_prefixes = get_native_method_prefixes();
 403 
 404   // allocate and install the new prefixex
 405   if (prefix_count == 0 || !is_valid()) {
 406     _native_method_prefix_count = 0;
 407     _native_method_prefixes = nullptr;
 408   } else {
 409     // there are prefixes, allocate an array to hold them, and fill it
 410     char** new_prefixes = (char**)os::malloc((prefix_count) * sizeof(char*), mtInternal);
 411     if (new_prefixes == nullptr) {
 412       return JVMTI_ERROR_OUT_OF_MEMORY;
 413     }
 414     for (int i = 0; i < prefix_count; i++) {
 415       char* prefix = prefixes[i];
 416       if (prefix == nullptr) {
 417         for (int j = 0; j < (i-1); j++) {
 418           os::free(new_prefixes[j]);
 419         }
 420         os::free(new_prefixes);
 421         return JVMTI_ERROR_NULL_POINTER;
 422       }
 423       prefix = os::strdup(prefixes[i]);
 424       if (prefix == nullptr) {
 425         for (int j = 0; j < (i-1); j++) {
 426           os::free(new_prefixes[j]);
 427         }
 428         os::free(new_prefixes);
 429         return JVMTI_ERROR_OUT_OF_MEMORY;
 430       }
 431       new_prefixes[i] = prefix;
 432     }
 433     _native_method_prefix_count = prefix_count;
 434     _native_method_prefixes = new_prefixes;
 435   }
 436 
 437   // now that we know the new prefixes have been successfully installed we can
 438   // safely remove the old ones
 439   if (old_prefix_count != 0) {
 440     for (int i = 0; i < old_prefix_count; i++) {
 441       os::free(old_prefixes[i]);
 442     }
 443     os::free(old_prefixes);
 444   }
 445 
 446   return JVMTI_ERROR_NONE;
 447 }
 448 
 449 
 450 // Collect all the prefixes which have been set in any JVM TI environments
 451 // by the SetNativeMethodPrefix(es) functions.  Be sure to maintain the
 452 // order of environments and the order of prefixes within each environment.
 453 // Return in a resource allocated array.
 454 char**
 455 JvmtiEnvBase::get_all_native_method_prefixes(int* count_ptr) {
 456   assert(Threads::number_of_threads() == 0 ||
 457          SafepointSynchronize::is_at_safepoint() ||
 458          JvmtiThreadState_lock->is_locked(),
 459          "sanity check");
 460 
 461   int total_count = 0;
 462   GrowableArray<char*>* prefix_array =new GrowableArray<char*>(5);
 463 
 464   JvmtiEnvIterator it;
 465   for (JvmtiEnvBase* env = it.first(); env != nullptr; env = it.next(env)) {
 466     int prefix_count = env->get_native_method_prefix_count();
 467     char** prefixes = env->get_native_method_prefixes();
 468     for (int j = 0; j < prefix_count; j++) {
 469       // retrieve a prefix and so that it is safe against asynchronous changes
 470       // copy it into the resource area
 471       char* prefix = prefixes[j];
 472       char* prefix_copy = NEW_RESOURCE_ARRAY(char, strlen(prefix)+1);
 473       strcpy(prefix_copy, prefix);
 474       prefix_array->at_put_grow(total_count++, prefix_copy);
 475     }
 476   }
 477 
 478   char** all_prefixes = NEW_RESOURCE_ARRAY(char*, total_count);
 479   char** p = all_prefixes;
 480   for (int i = 0; i < total_count; ++i) {
 481     *p++ = prefix_array->at(i);
 482   }
 483   *count_ptr = total_count;
 484   return all_prefixes;
 485 }
 486 
 487 void
 488 JvmtiEnvBase::set_event_callbacks(const jvmtiEventCallbacks* callbacks,
 489                                                jint size_of_callbacks) {
 490   assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
 491 
 492   size_t byte_cnt = sizeof(jvmtiEventCallbacks);
 493 
 494   // clear in either case to be sure we got any gap between sizes
 495   memset(&_event_callbacks, 0, byte_cnt);
 496 
 497   // Now that JvmtiThreadState_lock is held, prevent a possible race condition where events
 498   // are re-enabled by a call to set event callbacks where the DisposeEnvironment
 499   // occurs after the boiler-plate environment check and before the lock is acquired.
 500   if (callbacks != nullptr && is_valid()) {
 501     if (size_of_callbacks < (jint)byte_cnt) {
 502       byte_cnt = size_of_callbacks;
 503     }
 504     memcpy(&_event_callbacks, callbacks, byte_cnt);
 505   }
 506 }
 507 
 508 
 509 // In the fullness of time, all users of the method should instead
 510 // directly use allocate, besides being cleaner and faster, this will
 511 // mean much better out of memory handling
 512 unsigned char *
 513 JvmtiEnvBase::jvmtiMalloc(jlong size) {
 514   unsigned char* mem = nullptr;
 515   jvmtiError result = allocate(size, &mem);
 516   assert(result == JVMTI_ERROR_NONE, "Allocate failed");
 517   return mem;
 518 }
 519 
 520 
 521 // Handle management
 522 
 523 jobject JvmtiEnvBase::jni_reference(Handle hndl) {
 524   return JNIHandles::make_local(hndl());
 525 }
 526 
 527 jobject JvmtiEnvBase::jni_reference(JavaThread *thread, Handle hndl) {
 528   return JNIHandles::make_local(thread, hndl());
 529 }
 530 
 531 void JvmtiEnvBase::destroy_jni_reference(jobject jobj) {
 532   JNIHandles::destroy_local(jobj);
 533 }
 534 
 535 void JvmtiEnvBase::destroy_jni_reference(JavaThread *thread, jobject jobj) {
 536   JNIHandles::destroy_local(jobj); // thread is unused.
 537 }
 538 
 539 //
 540 // Threads
 541 //
 542 
 543 jthread *
 544 JvmtiEnvBase::new_jthreadArray(int length, Handle *handles) {
 545   if (length == 0) {
 546     return nullptr;
 547   }
 548 
 549   jthread* objArray = (jthread *) jvmtiMalloc(sizeof(jthread) * length);
 550   NULL_CHECK(objArray, nullptr);
 551 
 552   for (int i = 0; i < length; i++) {
 553     objArray[i] = (jthread)jni_reference(handles[i]);
 554   }
 555   return objArray;
 556 }
 557 
 558 jthreadGroup *
 559 JvmtiEnvBase::new_jthreadGroupArray(int length, objArrayHandle groups) {
 560   if (length == 0) {
 561     return nullptr;
 562   }
 563 
 564   jthreadGroup* objArray = (jthreadGroup *) jvmtiMalloc(sizeof(jthreadGroup) * length);
 565   NULL_CHECK(objArray, nullptr);
 566 
 567   for (int i = 0; i < length; i++) {
 568     objArray[i] = (jthreadGroup)JNIHandles::make_local(groups->obj_at(i));
 569   }
 570   return objArray;
 571 }
 572 
 573 // Return the vframe on the specified thread and depth, null if no such frame.
 574 // The thread and the oops in the returned vframe might not have been processed.
 575 javaVFrame*
 576 JvmtiEnvBase::jvf_for_thread_and_depth(JavaThread* java_thread, jint depth) {
 577   if (!java_thread->has_last_Java_frame()) {
 578     return nullptr;
 579   }
 580   RegisterMap reg_map(java_thread,
 581                       RegisterMap::UpdateMap::include,
 582                       RegisterMap::ProcessFrames::skip,
 583                       RegisterMap::WalkContinuation::include);
 584   javaVFrame *jvf = java_thread->last_java_vframe(&reg_map);
 585 
 586   jvf = JvmtiEnvBase::check_and_skip_hidden_frames(java_thread, jvf);
 587 
 588   for (int d = 0; jvf != nullptr && d < depth; d++) {
 589     jvf = jvf->java_sender();
 590   }
 591   return jvf;
 592 }
 593 
 594 //
 595 // utilities: JNI objects
 596 //
 597 
 598 
 599 jclass
 600 JvmtiEnvBase::get_jni_class_non_null(Klass* k) {
 601   assert(k != nullptr, "k != null");
 602   Thread *thread = Thread::current();
 603   return (jclass)jni_reference(Handle(thread, k->java_mirror()));
 604 }
 605 
 606 //
 607 // Field Information
 608 //
 609 
 610 bool
 611 JvmtiEnvBase::get_field_descriptor(Klass* k, jfieldID field, fieldDescriptor* fd) {
 612   if (!jfieldIDWorkaround::is_valid_jfieldID(k, field)) {
 613     return false;
 614   }
 615   bool found = false;
 616   if (jfieldIDWorkaround::is_static_jfieldID(field)) {
 617     JNIid* id = jfieldIDWorkaround::from_static_jfieldID(field);
 618     found = id->find_local_field(fd);
 619   } else {
 620     // Non-static field. The fieldID is really the offset of the field within the object.
 621     int offset = jfieldIDWorkaround::from_instance_jfieldID(k, field);
 622     found = InstanceKlass::cast(k)->find_field_from_offset(offset, false, fd);
 623   }
 624   return found;
 625 }
 626 
 627 bool
 628 JvmtiEnvBase::is_vthread_alive(oop vt) {
 629   oop cont = java_lang_VirtualThread::continuation(vt);
 630   return !jdk_internal_vm_Continuation::done(cont) &&
 631          java_lang_VirtualThread::state(vt) != java_lang_VirtualThread::NEW;
 632 }
 633 
 634 // Return JavaThread if virtual thread is mounted, null otherwise.
 635 JavaThread* JvmtiEnvBase::get_JavaThread_or_null(oop vthread) {
 636   oop carrier_thread = java_lang_VirtualThread::carrier_thread(vthread);
 637   if (carrier_thread == nullptr) {
 638     return nullptr;
 639   }
 640 
 641   JavaThread* java_thread = java_lang_Thread::thread(carrier_thread);
 642 
 643   // This could be a different thread to the current one. So we need to ensure that
 644   // processing has started before we are allowed to read the continuation oop of
 645   // another thread, as it is a direct root of that other thread.
 646   StackWatermarkSet::start_processing(java_thread, StackWatermarkKind::gc);
 647 
 648   oop cont = java_lang_VirtualThread::continuation(vthread);
 649   assert(cont != nullptr, "must be");
 650   assert(Continuation::continuation_scope(cont) == java_lang_VirtualThread::vthread_scope(), "must be");
 651   return Continuation::is_continuation_mounted(java_thread, cont) ? java_thread : nullptr;
 652 }
 653 
 654 javaVFrame*
 655 JvmtiEnvBase::check_and_skip_hidden_frames(bool is_in_VTMS_transition, javaVFrame* jvf) {
 656   // The second condition is needed to hide notification methods.
 657   if (!is_in_VTMS_transition && (jvf == nullptr || !jvf->method()->jvmti_mount_transition())) {
 658     return jvf;  // No frames to skip.
 659   }
 660   // Find jvf with a method annotated with @JvmtiMountTransition.
 661   for ( ; jvf != nullptr; jvf = jvf->java_sender()) {
 662     if (jvf->method()->jvmti_mount_transition()) {  // Cannot actually appear in an unmounted continuation; they're never frozen.
 663       jvf = jvf->java_sender();  // Skip annotated method.
 664       break;
 665     }
 666     if (jvf->method()->changes_current_thread()) {
 667       break;
 668     }
 669     // Skip frame above annotated method.
 670   }
 671   return jvf;
 672 }
 673 
 674 javaVFrame*
 675 JvmtiEnvBase::check_and_skip_hidden_frames(JavaThread* jt, javaVFrame* jvf) {
 676   jvf = check_and_skip_hidden_frames(jt->is_in_VTMS_transition(), jvf);
 677   return jvf;
 678 }
 679 
 680 javaVFrame*
 681 JvmtiEnvBase::check_and_skip_hidden_frames(oop vthread, javaVFrame* jvf) {
 682   JvmtiThreadState* state = java_lang_Thread::jvmti_thread_state(vthread);
 683   if (state == nullptr) {
 684     // nothing to skip
 685     return jvf;
 686   }
 687   jvf = check_and_skip_hidden_frames(java_lang_Thread::is_in_VTMS_transition(vthread), jvf);
 688   return jvf;
 689 }
 690 
 691 javaVFrame*
 692 JvmtiEnvBase::get_vthread_jvf(oop vthread) {
 693   assert(java_lang_VirtualThread::state(vthread) != java_lang_VirtualThread::NEW, "sanity check");
 694   assert(java_lang_VirtualThread::state(vthread) != java_lang_VirtualThread::TERMINATED, "sanity check");
 695 
 696   Thread* cur_thread = Thread::current();
 697   oop cont = java_lang_VirtualThread::continuation(vthread);
 698   javaVFrame* jvf = nullptr;
 699 
 700   JavaThread* java_thread = get_JavaThread_or_null(vthread);
 701   if (java_thread != nullptr) {
 702     if (!java_thread->has_last_Java_frame()) {
 703       // TBD: This is a temporary work around to avoid a guarantee caused by
 704       // the native enterSpecial frame on the top. No frames will be found
 705       // by the JVMTI functions such as GetStackTrace.
 706       return nullptr;
 707     }
 708     vframeStream vfs(java_thread);
 709     jvf = vfs.at_end() ? nullptr : vfs.asJavaVFrame();
 710     jvf = check_and_skip_hidden_frames(java_thread, jvf);
 711   } else {
 712     vframeStream vfs(cont);
 713     jvf = vfs.at_end() ? nullptr : vfs.asJavaVFrame();
 714     jvf = check_and_skip_hidden_frames(vthread, jvf);
 715   }
 716   return jvf;
 717 }
 718 
 719 // Return correct javaVFrame for a carrier (non-virtual) thread.
 720 // It strips vthread frames at the top if there are any.
 721 javaVFrame*
 722 JvmtiEnvBase::get_cthread_last_java_vframe(JavaThread* jt, RegisterMap* reg_map_p) {
 723   // Strip vthread frames in case of carrier thread with mounted continuation.
 724   bool cthread_with_cont = JvmtiEnvBase::is_cthread_with_continuation(jt);
 725   javaVFrame *jvf = cthread_with_cont ? jt->carrier_last_java_vframe(reg_map_p)
 726                                       : jt->last_java_vframe(reg_map_p);
 727   // Skip hidden frames only for carrier threads
 728   // which are in non-temporary VTMS transition.
 729   if (jt->is_in_VTMS_transition()) {
 730     jvf = check_and_skip_hidden_frames(jt, jvf);
 731   }
 732   return jvf;
 733 }
 734 
 735 jint
 736 JvmtiEnvBase::get_thread_state_base(oop thread_oop, JavaThread* jt) {
 737   jint state = 0;
 738 
 739   if (thread_oop != nullptr) {
 740     // Get most state bits.
 741     state = (jint)java_lang_Thread::get_thread_status(thread_oop);
 742   }
 743   if (jt != nullptr) {
 744     // We have a JavaThread* so add more state bits.
 745     JavaThreadState jts = jt->thread_state();
 746 
 747     if (jt->is_carrier_thread_suspended() ||
 748         ((jt->jvmti_vthread() == nullptr || jt->jvmti_vthread() == thread_oop) && jt->is_suspended())) {
 749       // Suspended non-virtual thread.
 750       state |= JVMTI_THREAD_STATE_SUSPENDED;
 751     }
 752     if (jts == _thread_in_native) {
 753       state |= JVMTI_THREAD_STATE_IN_NATIVE;
 754     }
 755     if (jt->is_interrupted(false)) {
 756       state |= JVMTI_THREAD_STATE_INTERRUPTED;
 757     }
 758   }
 759   return state;
 760 }
 761 
 762 jint
 763 JvmtiEnvBase::get_thread_state(oop thread_oop, JavaThread* jt) {
 764   jint state = 0;
 765 
 766   if (is_thread_carrying_vthread(jt, thread_oop)) {
 767     state = (jint)java_lang_Thread::get_thread_status(thread_oop);
 768 
 769     // This is for extra safety. Other bits are not expected nor needed.
 770     state &= (JVMTI_THREAD_STATE_ALIVE | JVMTI_THREAD_STATE_INTERRUPTED);
 771 
 772     if (jt->is_carrier_thread_suspended()) {
 773       state |= JVMTI_THREAD_STATE_SUSPENDED;
 774     }
 775     // It's okay for the JVMTI state to be reported as WAITING when waiting
 776     // for something other than an Object.wait. So, we treat a thread carrying
 777     // a virtual thread as waiting indefinitely which is not runnable.
 778     // It is why the RUNNABLE bit is not needed and the WAITING bits are added.
 779     state |= JVMTI_THREAD_STATE_WAITING | JVMTI_THREAD_STATE_WAITING_INDEFINITELY;
 780   } else {
 781     state = get_thread_state_base(thread_oop, jt);
 782   }
 783   return state;
 784 }
 785 
 786 jint
 787 JvmtiEnvBase::get_vthread_state(oop thread_oop, JavaThread* java_thread) {
 788   jint state = 0;
 789   bool ext_suspended = JvmtiVTSuspender::is_vthread_suspended(thread_oop);
 790   jint interrupted = java_lang_Thread::interrupted(thread_oop);
 791 
 792   if (java_thread != nullptr) {
 793     // If virtual thread is blocked on a monitor enter the BLOCKED_ON_MONITOR_ENTER bit
 794     // is set for carrier thread instead of virtual.
 795     // Other state bits except filtered ones are expected to be the same.
 796     oop ct_oop = java_lang_VirtualThread::carrier_thread(thread_oop);
 797     jint filtered_bits = JVMTI_THREAD_STATE_SUSPENDED | JVMTI_THREAD_STATE_INTERRUPTED;
 798 
 799     // This call can trigger a safepoint, so thread_oop must not be used after it.
 800     state = get_thread_state_base(ct_oop, java_thread) & ~filtered_bits;
 801   } else {
 802     int vt_state = java_lang_VirtualThread::state(thread_oop);
 803     state = (jint)java_lang_VirtualThread::map_state_to_thread_status(vt_state);
 804   }
 805   // Ensure the thread has not exited after retrieving suspended/interrupted values.
 806   if ((state & JVMTI_THREAD_STATE_ALIVE) != 0) {
 807     if (ext_suspended) {
 808       state |= JVMTI_THREAD_STATE_SUSPENDED;
 809     }
 810     if (interrupted) {
 811       state |= JVMTI_THREAD_STATE_INTERRUPTED;
 812     }
 813   }
 814   return state;
 815 }
 816 
 817 jint
 818 JvmtiEnvBase::get_thread_or_vthread_state(oop thread_oop, JavaThread* java_thread) {
 819   jint state = 0;
 820   if (java_lang_VirtualThread::is_instance(thread_oop)) {
 821     state = JvmtiEnvBase::get_vthread_state(thread_oop, java_thread);
 822   } else {
 823     state = JvmtiEnvBase::get_thread_state(thread_oop, java_thread);
 824   }
 825   return state;
 826 }
 827 
 828 jvmtiError
 829 JvmtiEnvBase::get_live_threads(JavaThread* current_thread, Handle group_hdl, jint *count_ptr, Handle **thread_objs_p) {
 830   jint count = 0;
 831   Handle *thread_objs = nullptr;
 832   ThreadsListEnumerator tle(current_thread, /* include_jvmti_agent_threads */ true);
 833   int nthreads = tle.num_threads();
 834   if (nthreads > 0) {
 835     thread_objs = NEW_RESOURCE_ARRAY_RETURN_NULL(Handle, nthreads);
 836     NULL_CHECK(thread_objs, JVMTI_ERROR_OUT_OF_MEMORY);
 837     for (int i = 0; i < nthreads; i++) {
 838       Handle thread = tle.get_threadObj(i);
 839       if (thread()->is_a(vmClasses::Thread_klass()) && java_lang_Thread::threadGroup(thread()) == group_hdl()) {
 840         thread_objs[count++] = thread;
 841       }
 842     }
 843   }
 844   *thread_objs_p = thread_objs;
 845   *count_ptr = count;
 846   return JVMTI_ERROR_NONE;
 847 }
 848 
 849 jvmtiError
 850 JvmtiEnvBase::get_subgroups(JavaThread* current_thread, Handle group_hdl, jint *count_ptr, objArrayHandle *group_objs_p) {
 851 
 852   // This call collects the strong and weak groups
 853   JavaThread* THREAD = current_thread;
 854   JavaValue result(T_OBJECT);
 855   JavaCalls::call_virtual(&result,
 856                           group_hdl,
 857                           vmClasses::ThreadGroup_klass(),
 858                           SymbolTable::new_permanent_symbol("subgroupsAsArray"),
 859                           vmSymbols::void_threadgroup_array_signature(),
 860                           THREAD);
 861   if (HAS_PENDING_EXCEPTION) {
 862     Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
 863     CLEAR_PENDING_EXCEPTION;
 864     if (ex_name == vmSymbols::java_lang_OutOfMemoryError()) {
 865       return JVMTI_ERROR_OUT_OF_MEMORY;
 866     } else {
 867       return JVMTI_ERROR_INTERNAL;
 868     }
 869   }
 870 
 871   assert(result.get_type() == T_OBJECT, "just checking");
 872   objArrayOop groups = (objArrayOop)result.get_oop();
 873 
 874   *count_ptr = groups == nullptr ? 0 : groups->length();
 875   *group_objs_p = objArrayHandle(current_thread, groups);
 876 
 877   return JVMTI_ERROR_NONE;
 878 }
 879 
 880 //
 881 // Object Monitor Information
 882 //
 883 
 884 //
 885 // Count the number of objects for a lightweight monitor. The hobj
 886 // parameter is object that owns the monitor so this routine will
 887 // count the number of times the same object was locked by frames
 888 // in java_thread.
 889 //
 890 jint
 891 JvmtiEnvBase::count_locked_objects(JavaThread *java_thread, Handle hobj) {
 892   jint ret = 0;
 893   if (!java_thread->has_last_Java_frame()) {
 894     return ret;  // no Java frames so no monitors
 895   }
 896 
 897   Thread* current_thread = Thread::current();
 898   ResourceMark rm(current_thread);
 899   HandleMark   hm(current_thread);
 900   RegisterMap  reg_map(java_thread,
 901                        RegisterMap::UpdateMap::include,
 902                        RegisterMap::ProcessFrames::include,
 903                        RegisterMap::WalkContinuation::skip);
 904 
 905   for (javaVFrame *jvf = java_thread->last_java_vframe(&reg_map); jvf != nullptr;
 906        jvf = jvf->java_sender()) {
 907     GrowableArray<MonitorInfo*>* mons = jvf->monitors();
 908     if (!mons->is_empty()) {
 909       for (int i = 0; i < mons->length(); i++) {
 910         MonitorInfo *mi = mons->at(i);
 911         if (mi->owner_is_scalar_replaced()) continue;
 912 
 913         // see if owner of the monitor is our object
 914         if (mi->owner() != nullptr && mi->owner() == hobj()) {
 915           ret++;
 916         }
 917       }
 918     }
 919   }
 920   return ret;
 921 }
 922 
 923 jvmtiError
 924 JvmtiEnvBase::get_current_contended_monitor(JavaThread *calling_thread, JavaThread *java_thread,
 925                                             jobject *monitor_ptr, bool is_virtual) {
 926   Thread *current_thread = Thread::current();
 927   assert(java_thread->is_handshake_safe_for(current_thread),
 928          "call by myself or at handshake");
 929   if (!is_virtual && JvmtiEnvBase::is_cthread_with_continuation(java_thread)) {
 930     // Carrier thread with a mounted continuation case.
 931     // No contended monitor can be owned by carrier thread in this case.
 932     *monitor_ptr = nullptr;
 933     return JVMTI_ERROR_NONE;
 934   }
 935   oop obj = nullptr;
 936   // The ObjectMonitor* can't be async deflated since we are either
 937   // at a safepoint or the calling thread is operating on itself so
 938   // it cannot leave the underlying wait()/enter() call.
 939   ObjectMonitor *mon = java_thread->current_waiting_monitor();
 940   if (mon == nullptr) {
 941     // thread is not doing an Object.wait() call
 942     mon = java_thread->current_pending_monitor();
 943     if (mon != nullptr) {
 944       // The thread is trying to enter() an ObjectMonitor.
 945       obj = mon->object();
 946       assert(obj != nullptr, "ObjectMonitor should have a valid object!");
 947     }
 948   } else {
 949     // thread is doing an Object.wait() call
 950     oop thread_oop = get_vthread_or_thread_oop(java_thread);
 951     jint state = get_thread_or_vthread_state(thread_oop, java_thread);
 952 
 953     if (state & JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER) {
 954       // thread is re-entering the monitor in an Object.wait() call
 955       obj = mon->object();
 956       assert(obj != nullptr, "Object.wait() should have an object");
 957     }
 958   }
 959 
 960   if (obj == nullptr) {
 961     *monitor_ptr = nullptr;
 962   } else {
 963     HandleMark hm(current_thread);
 964     Handle     hobj(current_thread, obj);
 965     *monitor_ptr = jni_reference(calling_thread, hobj);
 966   }
 967   return JVMTI_ERROR_NONE;
 968 }
 969 
 970 jvmtiError
 971 JvmtiEnvBase::get_owned_monitors(JavaThread *calling_thread, JavaThread* java_thread,
 972                                  GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list) {
 973   // Note:
 974   // calling_thread is the thread that requested the list of monitors for java_thread.
 975   // java_thread is the thread owning the monitors.
 976   // current_thread is the thread executing this code, can be a non-JavaThread (e.g. VM Thread).
 977   // And they all may be different threads.
 978   jvmtiError err = JVMTI_ERROR_NONE;
 979   Thread *current_thread = Thread::current();
 980   assert(java_thread->is_handshake_safe_for(current_thread),
 981          "call by myself or at handshake");
 982 
 983   if (JvmtiEnvBase::is_cthread_with_continuation(java_thread)) {
 984     // Carrier thread with a mounted continuation case.
 985     // No contended monitor can be owned by carrier thread in this case.
 986     return JVMTI_ERROR_NONE;
 987   }
 988   if (java_thread->has_last_Java_frame()) {
 989     ResourceMark rm(current_thread);
 990     HandleMark   hm(current_thread);
 991     RegisterMap  reg_map(java_thread,
 992                          RegisterMap::UpdateMap::include,
 993                          RegisterMap::ProcessFrames::include,
 994                          RegisterMap::WalkContinuation::skip);
 995 
 996     int depth = 0;
 997     for (javaVFrame *jvf = get_cthread_last_java_vframe(java_thread, &reg_map);
 998          jvf != nullptr; jvf = jvf->java_sender()) {
 999       if (MaxJavaStackTraceDepth == 0 || depth++ < MaxJavaStackTraceDepth) {  // check for stack too deep
1000         // add locked objects for this frame into list
1001         err = get_locked_objects_in_frame(calling_thread, java_thread, jvf, owned_monitors_list, depth-1);
1002         if (err != JVMTI_ERROR_NONE) {
1003           return err;
1004         }
1005       }
1006     }
1007   }
1008 
1009   // Get off stack monitors. (e.g. acquired via jni MonitorEnter).
1010   JvmtiMonitorClosure jmc(calling_thread, owned_monitors_list, this);
1011   ObjectSynchronizer::owned_monitors_iterate(&jmc, java_thread);
1012   err = jmc.error();
1013 
1014   return err;
1015 }
1016 
1017 jvmtiError
1018 JvmtiEnvBase::get_owned_monitors(JavaThread* calling_thread, JavaThread* java_thread, javaVFrame* jvf,
1019                                  GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list) {
1020   jvmtiError err = JVMTI_ERROR_NONE;
1021   Thread *current_thread = Thread::current();
1022   assert(java_thread->is_handshake_safe_for(current_thread),
1023          "call by myself or at handshake");
1024 
1025   int depth = 0;
1026   for ( ; jvf != nullptr; jvf = jvf->java_sender()) {
1027     if (MaxJavaStackTraceDepth == 0 || depth++ < MaxJavaStackTraceDepth) {  // check for stack too deep
1028       // Add locked objects for this frame into list.
1029       err = get_locked_objects_in_frame(calling_thread, java_thread, jvf, owned_monitors_list, depth - 1);
1030       if (err != JVMTI_ERROR_NONE) {
1031         return err;
1032       }
1033     }
1034   }
1035 
1036   // Get off stack monitors. (e.g. acquired via jni MonitorEnter).
1037   JvmtiMonitorClosure jmc(calling_thread, owned_monitors_list, this);
1038   ObjectSynchronizer::owned_monitors_iterate(&jmc, java_thread);
1039   err = jmc.error();
1040 
1041   return err;
1042 }
1043 
1044 // Save JNI local handles for any objects that this frame owns.
1045 jvmtiError
1046 JvmtiEnvBase::get_locked_objects_in_frame(JavaThread* calling_thread, JavaThread* java_thread,
1047                                  javaVFrame *jvf, GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitors_list, jint stack_depth) {
1048   jvmtiError err = JVMTI_ERROR_NONE;
1049   Thread* current_thread = Thread::current();
1050   ResourceMark rm(current_thread);
1051   HandleMark   hm(current_thread);
1052 
1053   GrowableArray<MonitorInfo*>* mons = jvf->monitors();
1054   if (mons->is_empty()) {
1055     return err;  // this javaVFrame holds no monitors
1056   }
1057 
1058   oop wait_obj = nullptr;
1059   {
1060     // The ObjectMonitor* can't be async deflated since we are either
1061     // at a safepoint or the calling thread is operating on itself so
1062     // it cannot leave the underlying wait() call.
1063     // Save object of current wait() call (if any) for later comparison.
1064     ObjectMonitor *mon = java_thread->current_waiting_monitor();
1065     if (mon != nullptr) {
1066       wait_obj = mon->object();
1067     }
1068   }
1069   oop pending_obj = nullptr;
1070   {
1071     // The ObjectMonitor* can't be async deflated since we are either
1072     // at a safepoint or the calling thread is operating on itself so
1073     // it cannot leave the underlying enter() call.
1074     // Save object of current enter() call (if any) for later comparison.
1075     ObjectMonitor *mon = java_thread->current_pending_monitor();
1076     if (mon != nullptr) {
1077       pending_obj = mon->object();
1078     }
1079   }
1080 
1081   for (int i = 0; i < mons->length(); i++) {
1082     MonitorInfo *mi = mons->at(i);
1083 
1084     if (mi->owner_is_scalar_replaced()) continue;
1085 
1086     oop obj = mi->owner();
1087     if (obj == nullptr) {
1088       // this monitor doesn't have an owning object so skip it
1089       continue;
1090     }
1091 
1092     if (wait_obj == obj) {
1093       // the thread is waiting on this monitor so it isn't really owned
1094       continue;
1095     }
1096 
1097     if (pending_obj == obj) {
1098       // the thread is pending on this monitor so it isn't really owned
1099       continue;
1100     }
1101 
1102     if (owned_monitors_list->length() > 0) {
1103       // Our list has at least one object on it so we have to check
1104       // for recursive object locking
1105       bool found = false;
1106       for (int j = 0; j < owned_monitors_list->length(); j++) {
1107         jobject jobj = ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(j))->monitor;
1108         oop check = JNIHandles::resolve(jobj);
1109         if (check == obj) {
1110           found = true;  // we found the object
1111           break;
1112         }
1113       }
1114 
1115       if (found) {
1116         // already have this object so don't include it
1117         continue;
1118       }
1119     }
1120 
1121     // add the owning object to our list
1122     jvmtiMonitorStackDepthInfo *jmsdi;
1123     err = allocate(sizeof(jvmtiMonitorStackDepthInfo), (unsigned char **)&jmsdi);
1124     if (err != JVMTI_ERROR_NONE) {
1125         return err;
1126     }
1127     Handle hobj(Thread::current(), obj);
1128     jmsdi->monitor = jni_reference(calling_thread, hobj);
1129     jmsdi->stack_depth = stack_depth;
1130     owned_monitors_list->append(jmsdi);
1131   }
1132 
1133   return err;
1134 }
1135 
1136 jvmtiError
1137 JvmtiEnvBase::get_stack_trace(javaVFrame *jvf,
1138                               jint start_depth, jint max_count,
1139                               jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
1140   Thread *current_thread = Thread::current();
1141   ResourceMark rm(current_thread);
1142   HandleMark hm(current_thread);
1143   int count = 0;
1144 
1145   if (start_depth != 0) {
1146     if (start_depth > 0) {
1147       for (int j = 0; j < start_depth && jvf != nullptr; j++) {
1148         jvf = jvf->java_sender();
1149       }
1150       if (jvf == nullptr) {
1151         // start_depth is deeper than the stack depth.
1152         return JVMTI_ERROR_ILLEGAL_ARGUMENT;
1153       }
1154     } else { // start_depth < 0
1155       // We are referencing the starting depth based on the oldest
1156       // part of the stack.
1157       // Optimize to limit the number of times that java_sender() is called.
1158       javaVFrame *jvf_cursor = jvf;
1159       javaVFrame *jvf_prev = nullptr;
1160       javaVFrame *jvf_prev_prev = nullptr;
1161       int j = 0;
1162       while (jvf_cursor != nullptr) {
1163         jvf_prev_prev = jvf_prev;
1164         jvf_prev = jvf_cursor;
1165         for (j = 0; j > start_depth && jvf_cursor != nullptr; j--) {
1166           jvf_cursor = jvf_cursor->java_sender();
1167         }
1168       }
1169       if (j == start_depth) {
1170         // Previous pointer is exactly where we want to start.
1171         jvf = jvf_prev;
1172       } else {
1173         // We need to back up further to get to the right place.
1174         if (jvf_prev_prev == nullptr) {
1175           // The -start_depth is greater than the stack depth.
1176           return JVMTI_ERROR_ILLEGAL_ARGUMENT;
1177         }
1178         // j is now the number of frames on the stack starting with
1179         // jvf_prev, we start from jvf_prev_prev and move older on
1180         // the stack that many, and the result is -start_depth frames
1181         // remaining.
1182         jvf = jvf_prev_prev;
1183         for (; j < 0; j++) {
1184           jvf = jvf->java_sender();
1185         }
1186       }
1187     }
1188   }
1189   for (; count < max_count && jvf != nullptr; count++) {
1190     frame_buffer[count].method = jvf->method()->jmethod_id();
1191     frame_buffer[count].location = (jvf->method()->is_native() ? -1 : jvf->bci());
1192     jvf = jvf->java_sender();
1193   }
1194   *count_ptr = count;
1195   return JVMTI_ERROR_NONE;
1196 }
1197 
1198 jvmtiError
1199 JvmtiEnvBase::get_stack_trace(JavaThread *java_thread,
1200                               jint start_depth, jint max_count,
1201                               jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
1202   Thread *current_thread = Thread::current();
1203   assert(SafepointSynchronize::is_at_safepoint() ||
1204          java_thread->is_handshake_safe_for(current_thread),
1205          "call by myself / at safepoint / at handshake");
1206   int count = 0;
1207   jvmtiError err = JVMTI_ERROR_NONE;
1208 
1209   if (java_thread->has_last_Java_frame()) {
1210     RegisterMap reg_map(java_thread,
1211                         RegisterMap::UpdateMap::include,
1212                         RegisterMap::ProcessFrames::skip,
1213                         RegisterMap::WalkContinuation::skip);
1214     ResourceMark rm(current_thread);
1215     javaVFrame *jvf = get_cthread_last_java_vframe(java_thread, &reg_map);
1216 
1217     err = get_stack_trace(jvf, start_depth, max_count, frame_buffer, count_ptr);
1218   } else {
1219     *count_ptr = 0;
1220     if (start_depth != 0) {
1221       // no frames and there is a starting depth
1222       err = JVMTI_ERROR_ILLEGAL_ARGUMENT;
1223     }
1224   }
1225   return err;
1226 }
1227 
1228 jint
1229 JvmtiEnvBase::get_frame_count(javaVFrame *jvf) {
1230   int count = 0;
1231 
1232   while (jvf != nullptr) {
1233     jvf = jvf->java_sender();
1234     count++;
1235   }
1236   return count;
1237 }
1238 
1239 jvmtiError
1240 JvmtiEnvBase::get_frame_count(JavaThread* jt, jint *count_ptr) {
1241   Thread *current_thread = Thread::current();
1242   assert(current_thread == jt ||
1243          SafepointSynchronize::is_at_safepoint() ||
1244          jt->is_handshake_safe_for(current_thread),
1245          "call by myself / at safepoint / at handshake");
1246 
1247   if (!jt->has_last_Java_frame()) { // no Java frames
1248     *count_ptr = 0;
1249   } else {
1250     ResourceMark rm(current_thread);
1251     RegisterMap reg_map(jt,
1252                         RegisterMap::UpdateMap::include,
1253                         RegisterMap::ProcessFrames::include,
1254                         RegisterMap::WalkContinuation::skip);
1255     javaVFrame *jvf = get_cthread_last_java_vframe(jt, &reg_map);
1256 
1257     *count_ptr = get_frame_count(jvf);
1258   }
1259   return JVMTI_ERROR_NONE;
1260 }
1261 
1262 jvmtiError
1263 JvmtiEnvBase::get_frame_count(oop vthread_oop, jint *count_ptr) {
1264   Thread *current_thread = Thread::current();
1265   ResourceMark rm(current_thread);
1266   javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(vthread_oop);
1267 
1268   *count_ptr = get_frame_count(jvf);
1269   return JVMTI_ERROR_NONE;
1270 }
1271 
1272 jvmtiError
1273 JvmtiEnvBase::get_frame_location(javaVFrame* jvf, jint depth,
1274                                  jmethodID* method_ptr, jlocation* location_ptr) {
1275   int cur_depth = 0;
1276 
1277   while (jvf != nullptr && cur_depth < depth) {
1278     jvf = jvf->java_sender();
1279     cur_depth++;
1280   }
1281   assert(depth >= cur_depth, "ran out of frames too soon");
1282   if (jvf == nullptr) {
1283     return JVMTI_ERROR_NO_MORE_FRAMES;
1284   }
1285   Method* method = jvf->method();
1286   if (method->is_native()) {
1287     *location_ptr = -1;
1288   } else {
1289     *location_ptr = jvf->bci();
1290   }
1291   *method_ptr = method->jmethod_id();
1292   return JVMTI_ERROR_NONE;
1293 }
1294 
1295 jvmtiError
1296 JvmtiEnvBase::get_frame_location(JavaThread *java_thread, jint depth,
1297                                  jmethodID* method_ptr, jlocation* location_ptr) {
1298   Thread* current = Thread::current();
1299   assert(java_thread->is_handshake_safe_for(current),
1300          "call by myself or at handshake");
1301   if (!java_thread->has_last_Java_frame()) {
1302     return JVMTI_ERROR_NO_MORE_FRAMES;
1303   }
1304   ResourceMark rm(current);
1305   HandleMark hm(current);
1306   RegisterMap reg_map(java_thread,
1307                       RegisterMap::UpdateMap::include,
1308                       RegisterMap::ProcessFrames::skip,
1309                       RegisterMap::WalkContinuation::include);
1310   javaVFrame* jvf = JvmtiEnvBase::get_cthread_last_java_vframe(java_thread, &reg_map);
1311 
1312   return get_frame_location(jvf, depth, method_ptr, location_ptr);
1313 }
1314 
1315 jvmtiError
1316 JvmtiEnvBase::get_frame_location(oop vthread_oop, jint depth,
1317                                  jmethodID* method_ptr, jlocation* location_ptr) {
1318   Thread* current = Thread::current();
1319   ResourceMark rm(current);
1320   HandleMark hm(current);
1321   javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(vthread_oop);
1322 
1323   return get_frame_location(jvf, depth, method_ptr, location_ptr);
1324 }
1325 
1326 jvmtiError
1327 JvmtiEnvBase::set_frame_pop(JvmtiThreadState* state, javaVFrame* jvf, jint depth) {
1328   for (int d = 0; jvf != nullptr && d < depth; d++) {
1329     jvf = jvf->java_sender();
1330   }
1331   if (jvf == nullptr) {
1332     return JVMTI_ERROR_NO_MORE_FRAMES;
1333   }
1334   if (jvf->method()->is_native()) {
1335     return JVMTI_ERROR_OPAQUE_FRAME;
1336   }
1337   assert(jvf->frame_pointer() != nullptr, "frame pointer mustn't be null");
1338   int frame_number = (int)get_frame_count(jvf);
1339   state->env_thread_state((JvmtiEnvBase*)this)->set_frame_pop(frame_number);
1340   return JVMTI_ERROR_NONE;
1341 }
1342 
1343 bool
1344 JvmtiEnvBase::is_cthread_with_mounted_vthread(JavaThread* jt) {
1345   oop thread_oop = jt->threadObj();
1346   assert(thread_oop != nullptr, "sanity check");
1347   oop mounted_vt = jt->jvmti_vthread();
1348 
1349   return mounted_vt != nullptr && mounted_vt != thread_oop;
1350 }
1351 
1352 bool
1353 JvmtiEnvBase::is_cthread_with_continuation(JavaThread* jt) {
1354   const ContinuationEntry* cont_entry = nullptr;
1355   if (jt->has_last_Java_frame()) {
1356     cont_entry = jt->vthread_continuation();
1357   }
1358   return cont_entry != nullptr && is_cthread_with_mounted_vthread(jt);
1359 }
1360 
1361 // Check if VirtualThread or BoundVirtualThread is suspended.
1362 bool
1363 JvmtiEnvBase::is_vthread_suspended(oop vt_oop, JavaThread* jt) {
1364   bool suspended = false;
1365   if (java_lang_VirtualThread::is_instance(vt_oop)) {
1366     suspended = JvmtiVTSuspender::is_vthread_suspended(vt_oop);
1367   }
1368   if (vt_oop->is_a(vmClasses::BoundVirtualThread_klass())) {
1369     suspended = jt->is_suspended();
1370   }
1371   return suspended;
1372 }
1373 
1374 // If (thread == null) then return current thread object.
1375 // Otherwise return JNIHandles::resolve_external_guard(thread).
1376 oop
1377 JvmtiEnvBase::current_thread_obj_or_resolve_external_guard(jthread thread) {
1378   oop thread_obj = JNIHandles::resolve_external_guard(thread);
1379   if (thread == nullptr) {
1380     thread_obj = get_vthread_or_thread_oop(JavaThread::current());
1381   }
1382   return thread_obj;
1383 }
1384 
1385 jvmtiError
1386 JvmtiEnvBase::get_threadOop_and_JavaThread(ThreadsList* t_list, jthread thread, JavaThread* cur_thread,
1387                                            JavaThread** jt_pp, oop* thread_oop_p) {
1388   JavaThread* java_thread = nullptr;
1389   oop thread_oop = nullptr;
1390 
1391   if (thread == nullptr) {
1392     if (cur_thread == nullptr) { // cur_thread can be null when called from a VM_op
1393       return JVMTI_ERROR_INVALID_THREAD;
1394     }
1395     java_thread = cur_thread;
1396     thread_oop = get_vthread_or_thread_oop(java_thread);
1397     if (thread_oop == nullptr || !thread_oop->is_a(vmClasses::Thread_klass())) {
1398       return JVMTI_ERROR_INVALID_THREAD;
1399     }
1400   } else {
1401     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(t_list, thread, &java_thread, &thread_oop);
1402     if (err != JVMTI_ERROR_NONE) {
1403       // We got an error code so we don't have a JavaThread*, but only return
1404       // an error from here if we didn't get a valid thread_oop. In a vthread case
1405       // the cv_external_thread_to_JavaThread is expected to correctly set the
1406       // thread_oop and return JVMTI_ERROR_INVALID_THREAD which we ignore here.
1407       if (thread_oop == nullptr || err != JVMTI_ERROR_INVALID_THREAD) {
1408         *thread_oop_p = thread_oop;
1409         return err;
1410       }
1411     }
1412     if (java_thread == nullptr && java_lang_VirtualThread::is_instance(thread_oop)) {
1413       java_thread = get_JavaThread_or_null(thread_oop);
1414     }
1415   }
1416   *jt_pp = java_thread;
1417   *thread_oop_p = thread_oop;
1418   if (java_lang_VirtualThread::is_instance(thread_oop) &&
1419       !JvmtiEnvBase::is_vthread_alive(thread_oop)) {
1420     return JVMTI_ERROR_THREAD_NOT_ALIVE;
1421   }
1422   return JVMTI_ERROR_NONE;
1423 }
1424 
1425 // Check for JVMTI_ERROR_NOT_SUSPENDED and JVMTI_ERROR_OPAQUE_FRAME errors.
1426 // Used in PopFrame and ForceEarlyReturn implementations.
1427 jvmtiError
1428 JvmtiEnvBase::check_non_suspended_or_opaque_frame(JavaThread* jt, oop thr_obj, bool self) {
1429   bool is_virtual = thr_obj != nullptr && thr_obj->is_a(vmClasses::BaseVirtualThread_klass());
1430 
1431   if (is_virtual) {
1432     if (!is_JavaThread_current(jt, thr_obj)) {
1433       if (!is_vthread_suspended(thr_obj, jt)) {
1434         return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1435       }
1436       if (jt == nullptr) { // unmounted virtual thread
1437         return JVMTI_ERROR_OPAQUE_FRAME;
1438       }
1439     }
1440   } else { // platform thread
1441     if (!self && !jt->is_suspended() &&
1442         !jt->is_carrier_thread_suspended()) {
1443       return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1444     }
1445   }
1446   return JVMTI_ERROR_NONE;
1447 }
1448 
1449 jvmtiError
1450 JvmtiEnvBase::get_object_monitor_usage(JavaThread* calling_thread, jobject object, jvmtiMonitorUsage* info_ptr) {
1451   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1452   Thread* current_thread = VMThread::vm_thread();
1453   assert(current_thread == Thread::current(), "must be");
1454 
1455   HandleMark hm(current_thread);
1456   Handle hobj;
1457 
1458   // Check arguments
1459   {
1460     oop mirror = JNIHandles::resolve_external_guard(object);
1461     NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
1462     NULL_CHECK(info_ptr, JVMTI_ERROR_NULL_POINTER);
1463 
1464     hobj = Handle(current_thread, mirror);
1465   }
1466 
1467   ThreadsListHandle tlh(current_thread);
1468   JavaThread *owning_thread = nullptr;
1469   jvmtiMonitorUsage ret = {
1470       nullptr, 0, 0, nullptr, 0, nullptr
1471   };
1472 
1473   uint32_t debug_bits = 0;
1474   // first derive the object's owner and entry_count (if any)
1475   owning_thread = ObjectSynchronizer::get_lock_owner(tlh.list(), hobj);
1476   if (owning_thread != nullptr) {
1477     oop thread_oop = get_vthread_or_thread_oop(owning_thread);
1478     bool is_virtual = thread_oop->is_a(vmClasses::BaseVirtualThread_klass());
1479     if (is_virtual) {
1480       thread_oop = nullptr;
1481     }
1482     Handle th(current_thread, thread_oop);
1483     ret.owner = (jthread)jni_reference(calling_thread, th);
1484 
1485     // The recursions field of a monitor does not reflect recursions
1486     // as lightweight locks before inflating the monitor are not included.
1487     // We have to count the number of recursive monitor entries the hard way.
1488     // We pass a handle to survive any GCs along the way.
1489     ret.entry_count = is_virtual ? 0 : count_locked_objects(owning_thread, hobj);
1490   }
1491   // implied else: entry_count == 0
1492 
1493   jint nWant = 0, nWait = 0;
1494   markWord mark = hobj->mark();
1495   ResourceMark rm(current_thread);
1496   GrowableArray<JavaThread*>* wantList = nullptr;
1497 
1498   ObjectMonitor* mon = mark.has_monitor()
1499       ? ObjectSynchronizer::read_monitor(current_thread, hobj(), mark)
1500       : nullptr;
1501 
1502   if (mon != nullptr) {
1503     assert(mon != nullptr, "must have monitor");
1504     // this object has a heavyweight monitor
1505     nWant = mon->contentions(); // # of threads contending for monitor entry, but not re-entry
1506     nWait = mon->waiters();     // # of threads waiting for notification,
1507                                 // or to re-enter monitor, in Object.wait()
1508 
1509     // Get the actual set of threads trying to enter, or re-enter, the monitor.
1510     wantList = Threads::get_pending_threads(tlh.list(), nWant + nWait, (address)mon);
1511     nWant = wantList->length();
1512   } else {
1513     // this object has a lightweight monitor
1514   }
1515 
1516   jint skipped = 0;
1517   if (mon != nullptr) {
1518     // Robustness: the actual waiting list can be smaller.
1519     // The nWait count we got from the mon->waiters() may include the re-entering
1520     // the monitor threads after being notified. Here we are correcting the actual
1521     // number of the waiting threads by excluding those re-entering the monitor.
1522     nWait = 0;
1523     for (ObjectWaiter* waiter = mon->first_waiter();
1524          waiter != nullptr && (nWait == 0 || waiter != mon->first_waiter());
1525          waiter = mon->next_waiter(waiter)) {
1526       JavaThread *w = mon->thread_of_waiter(waiter);
1527       oop thread_oop = get_vthread_or_thread_oop(w);
1528       if (thread_oop->is_a(vmClasses::BaseVirtualThread_klass())) {
1529         skipped++;
1530       }
1531       nWait++;
1532     }
1533   }
1534   ret.waiter_count = nWant;
1535   ret.notify_waiter_count = nWait - skipped;
1536 
1537   // Allocate memory for heavyweight and lightweight monitor.
1538   jvmtiError err;
1539   err = allocate(ret.waiter_count * sizeof(jthread *), (unsigned char**)&ret.waiters);
1540   if (err != JVMTI_ERROR_NONE) {
1541     return err;
1542   }
1543   err = allocate(ret.notify_waiter_count * sizeof(jthread *),
1544                  (unsigned char**)&ret.notify_waiters);
1545   if (err != JVMTI_ERROR_NONE) {
1546     deallocate((unsigned char*)ret.waiters);
1547     return err;
1548   }
1549 
1550   // now derive the rest of the fields
1551   if (mon != nullptr) {
1552     // this object has a heavyweight monitor
1553 
1554     // null out memory for robustness
1555     if (ret.waiters != nullptr) {
1556       memset(ret.waiters, 0, ret.waiter_count * sizeof(jthread *));
1557     }
1558     if (ret.notify_waiters != nullptr) {
1559       memset(ret.notify_waiters, 0, ret.notify_waiter_count * sizeof(jthread *));
1560     }
1561 
1562     if (ret.waiter_count > 0) { // we have contending threads waiting to enter/re-enter the monitor
1563       // identify threads waiting to enter and re-enter the monitor
1564       // get_pending_threads returns only java thread so we do not need to
1565       // check for non java threads.
1566       for (int i = 0; i < nWant; i++) {
1567         JavaThread *pending_thread = wantList->at(i);
1568         Handle th(current_thread, get_vthread_or_thread_oop(pending_thread));
1569         ret.waiters[i] = (jthread)jni_reference(calling_thread, th);
1570       }
1571     }
1572     if (ret.notify_waiter_count > 0) { // we have threads waiting to be notified in Object.wait()
1573       ObjectWaiter *waiter = mon->first_waiter();
1574       jint skipped = 0;
1575       for (int i = 0; i < nWait; i++) {
1576         JavaThread *w = mon->thread_of_waiter(waiter);
1577         oop thread_oop = get_vthread_or_thread_oop(w);
1578         bool is_virtual = thread_oop->is_a(vmClasses::BaseVirtualThread_klass());
1579         assert(w != nullptr, "sanity check");
1580         if (is_virtual) {
1581           skipped++;
1582         } else {
1583           // If the thread was found on the ObjectWaiter list, then
1584           // it has not been notified.
1585           Handle th(current_thread, get_vthread_or_thread_oop(w));
1586           ret.notify_waiters[i - skipped] = (jthread)jni_reference(calling_thread, th);
1587         }
1588         waiter = mon->next_waiter(waiter);
1589       }
1590     }
1591   } else {
1592     // this object has a lightweight monitor and we have nothing more
1593     // to do here because the defaults are just fine.
1594   }
1595 
1596   // we don't update return parameter unless everything worked
1597   *info_ptr = ret;
1598 
1599   return JVMTI_ERROR_NONE;
1600 }
1601 
1602 jvmtiError
1603 JvmtiEnvBase::check_thread_list(jint count, const jthread* list) {
1604   if (list == nullptr && count != 0) {
1605     return JVMTI_ERROR_NULL_POINTER;
1606   }
1607   for (int i = 0; i < count; i++) {
1608     jthread thread = list[i];
1609     oop thread_oop = JNIHandles::resolve_external_guard(thread);
1610     if (thread_oop == nullptr || !thread_oop->is_a(vmClasses::BaseVirtualThread_klass())) {
1611       return JVMTI_ERROR_INVALID_THREAD;
1612     }
1613   }
1614   return JVMTI_ERROR_NONE;
1615 }
1616 
1617 bool
1618 JvmtiEnvBase::is_in_thread_list(jint count, const jthread* list, oop jt_oop) {
1619   for (int idx = 0; idx < count; idx++) {
1620     jthread thread = list[idx];
1621     oop thread_oop = JNIHandles::resolve_external_guard(thread);
1622     if (thread_oop == jt_oop) {
1623       return true;
1624     }
1625   }
1626   return false;
1627 }
1628 
1629 class VM_SetNotifyJvmtiEventsMode : public VM_Operation {
1630 private:
1631   bool _enable;
1632 
1633   static void correct_jvmti_thread_state(JavaThread* jt) {
1634     oop  ct_oop = jt->threadObj();
1635     oop  vt_oop = jt->vthread();
1636     JvmtiThreadState* jt_state = jt->jvmti_thread_state();
1637     JvmtiThreadState* ct_state = java_lang_Thread::jvmti_thread_state(jt->threadObj());
1638     JvmtiThreadState* vt_state = vt_oop != nullptr ? java_lang_Thread::jvmti_thread_state(vt_oop) : nullptr;
1639     bool virt = vt_oop != nullptr && java_lang_VirtualThread::is_instance(vt_oop);
1640 
1641     // Correct jt->jvmti_thread_state() and jt->jvmti_vthread().
1642     // It was not maintained while notifyJvmti was disabled.
1643     if (virt) {
1644       jt->set_jvmti_thread_state(nullptr);  // reset jt->jvmti_thread_state()
1645       jt->set_jvmti_vthread(vt_oop);        // restore jt->jvmti_vthread()
1646     } else {
1647       jt->set_jvmti_thread_state(ct_state); // restore jt->jvmti_thread_state()
1648       jt->set_jvmti_vthread(ct_oop);        // restore jt->jvmti_vthread()
1649     }
1650   }
1651 
1652   // This function is called only if _enable == true.
1653   // Iterates over all JavaThread's, restores jt->jvmti_thread_state() and
1654   // jt->jvmti_vthread() for VTMS transition protocol.
1655   void correct_jvmti_thread_states() {
1656     for (JavaThread* jt : ThreadsListHandle()) {
1657       if (jt->is_in_VTMS_transition()) {
1658         jt->set_VTMS_transition_mark(true);
1659         continue; // no need in JvmtiThreadState correction below if in transition
1660       }
1661       correct_jvmti_thread_state(jt);
1662     }
1663   }
1664 
1665 public:
1666   VMOp_Type type() const { return VMOp_SetNotifyJvmtiEventsMode; }
1667   bool allow_nested_vm_operations() const { return false; }
1668   VM_SetNotifyJvmtiEventsMode(bool enable) : _enable(enable) {
1669   }
1670 
1671   void doit() {
1672     if (_enable) {
1673       correct_jvmti_thread_states();
1674     }
1675     JvmtiVTMSTransitionDisabler::set_VTMS_notify_jvmti_events(_enable);
1676   }
1677 };
1678 
1679 // This function is to support agents loaded into running VM.
1680 // Must be called in thread-in-native mode.
1681 bool
1682 JvmtiEnvBase::enable_virtual_threads_notify_jvmti() {
1683   if (!Continuations::enabled()) {
1684     return false;
1685   }
1686   if (JvmtiVTMSTransitionDisabler::VTMS_notify_jvmti_events()) {
1687     return false; // already enabled
1688   }
1689   VM_SetNotifyJvmtiEventsMode op(true);
1690   VMThread::execute(&op);
1691   return true;
1692 }
1693 
1694 // This function is used in WhiteBox, only needed to test the function above.
1695 // It is unsafe to use this function when virtual threads are executed.
1696 // Must be called in thread-in-native mode.
1697 bool
1698 JvmtiEnvBase::disable_virtual_threads_notify_jvmti() {
1699   if (!Continuations::enabled()) {
1700     return false;
1701   }
1702   if (!JvmtiVTMSTransitionDisabler::VTMS_notify_jvmti_events()) {
1703     return false; // already disabled
1704   }
1705   JvmtiVTMSTransitionDisabler disabler(true); // ensure there are no other disablers
1706   VM_SetNotifyJvmtiEventsMode op(false);
1707   VMThread::execute(&op);
1708   return true;
1709 }
1710 
1711 // java_thread - protected by ThreadsListHandle
1712 jvmtiError
1713 JvmtiEnvBase::suspend_thread(oop thread_oop, JavaThread* java_thread, bool single_suspend,
1714                              int* need_safepoint_p) {
1715   JavaThread* current = JavaThread::current();
1716   HandleMark hm(current);
1717   Handle thread_h(current, thread_oop);
1718   bool is_virtual = java_lang_VirtualThread::is_instance(thread_h());
1719 
1720   if (is_virtual) {
1721     if (single_suspend) {
1722       if (JvmtiVTSuspender::is_vthread_suspended(thread_h())) {
1723         return JVMTI_ERROR_THREAD_SUSPENDED;
1724       }
1725       JvmtiVTSuspender::register_vthread_suspend(thread_h());
1726       // Check if virtual thread is mounted and there is a java_thread.
1727       // A non-null java_thread is always passed in the !single_suspend case.
1728       oop carrier_thread = java_lang_VirtualThread::carrier_thread(thread_h());
1729       java_thread = carrier_thread == nullptr ? nullptr : java_lang_Thread::thread(carrier_thread);
1730     }
1731     // The java_thread can be still blocked in VTMS transition after a previous JVMTI resume call.
1732     // There is no need to suspend the java_thread in this case. After vthread unblocking,
1733     // it will check for ext_suspend request and suspend itself if necessary.
1734     if (java_thread == nullptr || java_thread->is_suspended()) {
1735       // We are done if the virtual thread is unmounted or
1736       // the java_thread is externally suspended.
1737       return JVMTI_ERROR_NONE;
1738     }
1739     // The virtual thread is mounted: suspend the java_thread.
1740   }
1741   // Don't allow hidden thread suspend request.
1742   if (java_thread->is_hidden_from_external_view()) {
1743     return JVMTI_ERROR_NONE;
1744   }
1745   bool is_thread_carrying = is_thread_carrying_vthread(java_thread, thread_h());
1746 
1747   // A case of non-virtual thread.
1748   if (!is_virtual) {
1749     // Thread.suspend() is used in some tests. It sets jt->is_suspended() only.
1750     if (java_thread->is_carrier_thread_suspended() ||
1751         (!is_thread_carrying && java_thread->is_suspended())) {
1752       return JVMTI_ERROR_THREAD_SUSPENDED;
1753     }
1754     java_thread->set_carrier_thread_suspended();
1755   }
1756   assert(!java_thread->is_in_VTMS_transition(), "sanity check");
1757 
1758   assert(!single_suspend || (!is_virtual && java_thread->is_carrier_thread_suspended()) ||
1759           (is_virtual && JvmtiVTSuspender::is_vthread_suspended(thread_h())),
1760          "sanity check");
1761 
1762   // An attempt to handshake-suspend a thread carrying a virtual thread will result in
1763   // suspension of mounted virtual thread. So, we just mark it as suspended
1764   // and it will be actually suspended at virtual thread unmount transition.
1765   if (!is_thread_carrying) {
1766     assert(thread_h() != nullptr, "sanity check");
1767     assert(single_suspend || thread_h()->is_a(vmClasses::BaseVirtualThread_klass()),
1768            "SuspendAllVirtualThreads should never suspend non-virtual threads");
1769     // Case of mounted virtual or attached carrier thread.
1770     if (!JvmtiSuspendControl::suspend(java_thread)) {
1771       // Thread is already suspended or in process of exiting.
1772       if (java_thread->is_exiting()) {
1773         // The thread was in the process of exiting.
1774         return JVMTI_ERROR_THREAD_NOT_ALIVE;
1775       }
1776       return JVMTI_ERROR_THREAD_SUSPENDED;
1777     }
1778   }
1779   return JVMTI_ERROR_NONE;
1780 }
1781 
1782 // java_thread - protected by ThreadsListHandle
1783 jvmtiError
1784 JvmtiEnvBase::resume_thread(oop thread_oop, JavaThread* java_thread, bool single_resume) {
1785   JavaThread* current = JavaThread::current();
1786   HandleMark hm(current);
1787   Handle thread_h(current, thread_oop);
1788   bool is_virtual = java_lang_VirtualThread::is_instance(thread_h());
1789 
1790   if (is_virtual) {
1791     if (single_resume) {
1792       if (!JvmtiVTSuspender::is_vthread_suspended(thread_h())) {
1793         return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1794       }
1795       JvmtiVTSuspender::register_vthread_resume(thread_h());
1796       // Check if virtual thread is mounted and there is a java_thread.
1797       // A non-null java_thread is always passed in the !single_resume case.
1798       oop carrier_thread = java_lang_VirtualThread::carrier_thread(thread_h());
1799       java_thread = carrier_thread == nullptr ? nullptr : java_lang_Thread::thread(carrier_thread);
1800     }
1801     // The java_thread can be still blocked in VTMS transition after a previous JVMTI suspend call.
1802     // There is no need to resume the java_thread in this case. After vthread unblocking,
1803     // it will check for is_vthread_suspended request and remain resumed if necessary.
1804     if (java_thread == nullptr || !java_thread->is_suspended()) {
1805       // We are done if the virtual thread is unmounted or
1806       // the java_thread is not externally suspended.
1807       return JVMTI_ERROR_NONE;
1808     }
1809     // The virtual thread is mounted and java_thread is supended: resume the java_thread.
1810   }
1811   // Don't allow hidden thread resume request.
1812   if (java_thread->is_hidden_from_external_view()) {
1813     return JVMTI_ERROR_NONE;
1814   }
1815   bool is_thread_carrying = is_thread_carrying_vthread(java_thread, thread_h());
1816 
1817   // A case of a non-virtual thread.
1818   if (!is_virtual) {
1819     if (!java_thread->is_carrier_thread_suspended() &&
1820         (is_thread_carrying || !java_thread->is_suspended())) {
1821       return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1822     }
1823     java_thread->clear_carrier_thread_suspended();
1824   }
1825   assert(!java_thread->is_in_VTMS_transition(), "sanity check");
1826 
1827   if (!is_thread_carrying) {
1828     assert(thread_h() != nullptr, "sanity check");
1829     assert(single_resume || thread_h()->is_a(vmClasses::BaseVirtualThread_klass()),
1830            "ResumeAllVirtualThreads should never resume non-virtual threads");
1831     if (java_thread->is_suspended()) {
1832       if (!JvmtiSuspendControl::resume(java_thread)) {
1833         return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1834       }
1835     }
1836   }
1837   return JVMTI_ERROR_NONE;
1838 }
1839 
1840 ResourceTracker::ResourceTracker(JvmtiEnv* env) {
1841   _env = env;
1842   _allocations = new (mtServiceability) GrowableArray<unsigned char*>(20, mtServiceability);
1843   _failed = false;
1844 }
1845 ResourceTracker::~ResourceTracker() {
1846   if (_failed) {
1847     for (int i=0; i<_allocations->length(); i++) {
1848       _env->deallocate(_allocations->at(i));
1849     }
1850   }
1851   delete _allocations;
1852 }
1853 
1854 jvmtiError ResourceTracker::allocate(jlong size, unsigned char** mem_ptr) {
1855   unsigned char *ptr;
1856   jvmtiError err = _env->allocate(size, &ptr);
1857   if (err == JVMTI_ERROR_NONE) {
1858     _allocations->append(ptr);
1859     *mem_ptr = ptr;
1860   } else {
1861     *mem_ptr = nullptr;
1862     _failed = true;
1863   }
1864   return err;
1865  }
1866 
1867 unsigned char* ResourceTracker::allocate(jlong size) {
1868   unsigned char* ptr;
1869   allocate(size, &ptr);
1870   return ptr;
1871 }
1872 
1873 char* ResourceTracker::strdup(const char* str) {
1874   char *dup_str = (char*)allocate(strlen(str)+1);
1875   if (dup_str != nullptr) {
1876     strcpy(dup_str, str);
1877   }
1878   return dup_str;
1879 }
1880 
1881 struct StackInfoNode {
1882   struct StackInfoNode *next;
1883   jvmtiStackInfo info;
1884 };
1885 
1886 // Create a jvmtiStackInfo inside a linked list node and create a
1887 // buffer for the frame information, both allocated as resource objects.
1888 // Fill in both the jvmtiStackInfo and the jvmtiFrameInfo.
1889 // Note that either or both of thr and thread_oop
1890 // may be null if the thread is new or has exited.
1891 void
1892 MultipleStackTracesCollector::fill_frames(jthread jt, JavaThread *thr, oop thread_oop) {
1893 #ifdef ASSERT
1894   Thread *current_thread = Thread::current();
1895   assert(SafepointSynchronize::is_at_safepoint() ||
1896          thr == nullptr ||
1897          thr->is_handshake_safe_for(current_thread),
1898          "unmounted virtual thread / call by myself / at safepoint / at handshake");
1899 #endif
1900 
1901   jint state = 0;
1902   struct StackInfoNode *node = NEW_RESOURCE_OBJ(struct StackInfoNode);
1903   jvmtiStackInfo *infop = &(node->info);
1904 
1905   node->next = head();
1906   set_head(node);
1907   infop->frame_count = 0;
1908   infop->frame_buffer = nullptr;
1909   infop->thread = jt;
1910 
1911   if (java_lang_VirtualThread::is_instance(thread_oop)) {
1912     state = JvmtiEnvBase::get_vthread_state(thread_oop, thr);
1913 
1914     if ((state & JVMTI_THREAD_STATE_ALIVE) != 0) {
1915       javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(thread_oop);
1916       infop->frame_buffer = NEW_RESOURCE_ARRAY(jvmtiFrameInfo, max_frame_count());
1917       _result = env()->get_stack_trace(jvf, 0, max_frame_count(),
1918                                        infop->frame_buffer, &(infop->frame_count));
1919     }
1920   } else {
1921     state = JvmtiEnvBase::get_thread_state(thread_oop, thr);
1922     if (thr != nullptr && (state & JVMTI_THREAD_STATE_ALIVE) != 0) {
1923       infop->frame_buffer = NEW_RESOURCE_ARRAY(jvmtiFrameInfo, max_frame_count());
1924       _result = env()->get_stack_trace(thr, 0, max_frame_count(),
1925                                        infop->frame_buffer, &(infop->frame_count));
1926     }
1927   }
1928   _frame_count_total += infop->frame_count;
1929   infop->state = state;
1930 }
1931 
1932 // Based on the stack information in the linked list, allocate memory
1933 // block to return and fill it from the info in the linked list.
1934 void
1935 MultipleStackTracesCollector::allocate_and_fill_stacks(jint thread_count) {
1936   // do I need to worry about alignment issues?
1937   jlong alloc_size =  thread_count       * sizeof(jvmtiStackInfo)
1938                     + _frame_count_total * sizeof(jvmtiFrameInfo);
1939   env()->allocate(alloc_size, (unsigned char **)&_stack_info);
1940 
1941   // pointers to move through the newly allocated space as it is filled in
1942   jvmtiStackInfo *si = _stack_info + thread_count;      // bottom of stack info
1943   jvmtiFrameInfo *fi = (jvmtiFrameInfo *)si;            // is the top of frame info
1944 
1945   // copy information in resource area into allocated buffer
1946   // insert stack info backwards since linked list is backwards
1947   // insert frame info forwards
1948   // walk the StackInfoNodes
1949   for (struct StackInfoNode *sin = head(); sin != nullptr; sin = sin->next) {
1950     jint frame_count = sin->info.frame_count;
1951     size_t frames_size = frame_count * sizeof(jvmtiFrameInfo);
1952     --si;
1953     memcpy(si, &(sin->info), sizeof(jvmtiStackInfo));
1954     if (frames_size == 0) {
1955       si->frame_buffer = nullptr;
1956     } else {
1957       memcpy(fi, sin->info.frame_buffer, frames_size);
1958       si->frame_buffer = fi;  // point to the new allocated copy of the frames
1959       fi += frame_count;
1960     }
1961   }
1962   assert(si == _stack_info, "the last copied stack info must be the first record");
1963   assert((unsigned char *)fi == ((unsigned char *)_stack_info) + alloc_size,
1964          "the last copied frame info must be the last record");
1965 }
1966 
1967 // AdapterClosure is to make use of JvmtiUnitedHandshakeClosure objects from
1968 // Handshake::execute() which is unaware of the do_vthread() member functions.
1969 class AdapterClosure : public HandshakeClosure {
1970   JvmtiUnitedHandshakeClosure* _hs_cl;
1971   Handle _target_h;
1972 
1973  public:
1974   AdapterClosure(JvmtiUnitedHandshakeClosure* hs_cl, Handle target_h)
1975       : HandshakeClosure(hs_cl->name()), _hs_cl(hs_cl), _target_h(target_h) {}
1976 
1977   virtual void do_thread(Thread* target) {
1978     if (java_lang_VirtualThread::is_instance(_target_h())) {
1979       _hs_cl->do_vthread(_target_h); // virtual thread
1980     } else {
1981       _hs_cl->do_thread(target);     // platform thread
1982     }
1983   }
1984 };
1985 
1986 // Supports platform and virtual threads.
1987 // JvmtiVTMSTransitionDisabler is always set by this function.
1988 void
1989 JvmtiHandshake::execute(JvmtiUnitedHandshakeClosure* hs_cl, jthread target) {
1990   JavaThread* current = JavaThread::current();
1991   HandleMark hm(current);
1992 
1993   JvmtiVTMSTransitionDisabler disabler(target);
1994   ThreadsListHandle tlh(current);
1995   JavaThread* java_thread = nullptr;
1996   oop thread_obj = nullptr;
1997 
1998   jvmtiError err = JvmtiEnvBase::get_threadOop_and_JavaThread(tlh.list(), target, current, &java_thread, &thread_obj);
1999   if (err != JVMTI_ERROR_NONE) {
2000     hs_cl->set_result(err);
2001     return;
2002   }
2003   Handle target_h(current, thread_obj);
2004   execute(hs_cl, &tlh, java_thread, target_h);
2005 }
2006 
2007 // Supports platform and virtual threads.
2008 // A virtual thread is always identified by the target_h oop handle.
2009 // The target_jt is always nullptr for an unmounted virtual thread.
2010 // JvmtiVTMSTransitionDisabler has to be set before call to this function.
2011 void
2012 JvmtiHandshake::execute(JvmtiUnitedHandshakeClosure* hs_cl, ThreadsListHandle* tlh,
2013                         JavaThread* target_jt, Handle target_h) {
2014   JavaThread* current = JavaThread::current();
2015   bool is_virtual = java_lang_VirtualThread::is_instance(target_h());
2016   bool self = target_jt == current;
2017 
2018   assert(!Continuations::enabled() || self || !is_virtual || current->is_VTMS_transition_disabler(), "sanity check");
2019 
2020   hs_cl->set_target_jt(target_jt);   // can be needed in the virtual thread case
2021   hs_cl->set_is_virtual(is_virtual); // can be needed in the virtual thread case
2022   hs_cl->set_self(self);             // needed when suspend is required for non-current target thread
2023 
2024   if (is_virtual) {                // virtual thread
2025     if (!JvmtiEnvBase::is_vthread_alive(target_h())) {
2026       return;
2027     }
2028     if (target_jt == nullptr) {    // unmounted virtual thread
2029       hs_cl->do_vthread(target_h); // execute handshake closure callback on current thread directly
2030     }
2031   }
2032   if (target_jt != nullptr) {      // mounted virtual or platform thread
2033     AdapterClosure acl(hs_cl, target_h);
2034     if (self) {                    // target platform thread is current
2035       acl.do_thread(target_jt);    // execute handshake closure callback on current thread directly
2036     } else {
2037       Handshake::execute(&acl, tlh, target_jt); // delegate to Handshake implementation
2038     }
2039   }
2040 }
2041 
2042 void
2043 VM_GetThreadListStackTraces::doit() {
2044   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
2045 
2046   ResourceMark rm;
2047   ThreadsListHandle tlh;
2048   for (int i = 0; i < _thread_count; ++i) {
2049     jthread jt = _thread_list[i];
2050     JavaThread* java_thread = nullptr;
2051     oop thread_oop = nullptr;
2052     jvmtiError err = JvmtiEnvBase::get_threadOop_and_JavaThread(tlh.list(), jt, nullptr, &java_thread, &thread_oop);
2053 
2054     if (err != JVMTI_ERROR_NONE) {
2055       // We got an error code so we don't have a JavaThread *, but
2056       // only return an error from here if we didn't get a valid
2057       // thread_oop.
2058       // In the virtual thread case the get_threadOop_and_JavaThread is expected to correctly set
2059       // the thread_oop and return JVMTI_ERROR_THREAD_NOT_ALIVE which we ignore here.
2060       // The corresponding thread state will be recorded in the jvmtiStackInfo.state.
2061       if (thread_oop == nullptr) {
2062         _collector.set_result(err);
2063         return;
2064       }
2065       // We have a valid thread_oop.
2066     }
2067     _collector.fill_frames(jt, java_thread, thread_oop);
2068   }
2069   _collector.allocate_and_fill_stacks(_thread_count);
2070 }
2071 
2072 void
2073 GetSingleStackTraceClosure::doit() {
2074   JavaThread *jt = _target_jt;
2075   oop thread_oop = JNIHandles::resolve_external_guard(_jthread);
2076 
2077   if ((jt == nullptr || !jt->is_exiting()) && thread_oop != nullptr) {
2078     ResourceMark rm;
2079     _collector.fill_frames(_jthread, jt, thread_oop);
2080     _collector.allocate_and_fill_stacks(1);
2081     set_result(_collector.result());
2082   }
2083 }
2084 
2085 void
2086 GetSingleStackTraceClosure::do_thread(Thread *target) {
2087   assert(_target_jt == JavaThread::cast(target), "sanity check");
2088   doit();
2089 }
2090 
2091 void
2092 GetSingleStackTraceClosure::do_vthread(Handle target_h) {
2093   // Use jvmti_vthread() instead of vthread() as target could have temporarily changed
2094   // identity to carrier thread (see VirtualThread.switchToCarrierThread).
2095   assert(_target_jt == nullptr || _target_jt->jvmti_vthread() == target_h(), "sanity check");
2096   doit();
2097 }
2098 
2099 void
2100 VM_GetAllStackTraces::doit() {
2101   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
2102 
2103   ResourceMark rm;
2104   _final_thread_count = 0;
2105   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
2106     oop thread_oop = jt->threadObj();
2107     if (thread_oop != nullptr &&
2108         !jt->is_exiting() &&
2109         java_lang_Thread::is_alive(thread_oop) &&
2110         !jt->is_hidden_from_external_view() &&
2111         !thread_oop->is_a(vmClasses::BoundVirtualThread_klass())) {
2112       ++_final_thread_count;
2113       // Handle block of the calling thread is used to create local refs.
2114       _collector.fill_frames((jthread)JNIHandles::make_local(_calling_thread, thread_oop),
2115                              jt, thread_oop);
2116     }
2117   }
2118   _collector.allocate_and_fill_stacks(_final_thread_count);
2119 }
2120 
2121 // Verifies that the top frame is a java frame in an expected state.
2122 // Deoptimizes frame if needed.
2123 // Checks that the frame method signature matches the return type (tos).
2124 // HandleMark must be defined in the caller only.
2125 // It is to keep a ret_ob_h handle alive after return to the caller.
2126 jvmtiError
2127 JvmtiEnvBase::check_top_frame(Thread* current_thread, JavaThread* java_thread,
2128                               jvalue value, TosState tos, Handle* ret_ob_h) {
2129   ResourceMark rm(current_thread);
2130 
2131   javaVFrame* jvf = jvf_for_thread_and_depth(java_thread, 0);
2132   NULL_CHECK(jvf, JVMTI_ERROR_NO_MORE_FRAMES);
2133 
2134   if (jvf->method()->is_native()) {
2135     return JVMTI_ERROR_OPAQUE_FRAME;
2136   }
2137 
2138   // If the frame is a compiled one, need to deoptimize it.
2139   if (jvf->is_compiled_frame()) {
2140     if (!jvf->fr().can_be_deoptimized()) {
2141       return JVMTI_ERROR_OPAQUE_FRAME;
2142     }
2143     Deoptimization::deoptimize_frame(java_thread, jvf->fr().id());
2144   }
2145 
2146   // Get information about method return type
2147   Symbol* signature = jvf->method()->signature();
2148 
2149   ResultTypeFinder rtf(signature);
2150   TosState fr_tos = as_TosState(rtf.type());
2151   if (fr_tos != tos) {
2152     if (tos != itos || (fr_tos != btos && fr_tos != ztos && fr_tos != ctos && fr_tos != stos)) {
2153       return JVMTI_ERROR_TYPE_MISMATCH;
2154     }
2155   }
2156 
2157   // Check that the jobject class matches the return type signature.
2158   jobject jobj = value.l;
2159   if (tos == atos && jobj != nullptr) { // null reference is allowed
2160     Handle ob_h(current_thread, JNIHandles::resolve_external_guard(jobj));
2161     NULL_CHECK(ob_h, JVMTI_ERROR_INVALID_OBJECT);
2162     Klass* ob_k = ob_h()->klass();
2163     NULL_CHECK(ob_k, JVMTI_ERROR_INVALID_OBJECT);
2164 
2165     // Method return type signature.
2166     char* ty_sign = 1 + strchr(signature->as_C_string(), JVM_SIGNATURE_ENDFUNC);
2167 
2168     if (!VM_GetOrSetLocal::is_assignable(ty_sign, ob_k, current_thread)) {
2169       return JVMTI_ERROR_TYPE_MISMATCH;
2170     }
2171     *ret_ob_h = ob_h;
2172   }
2173   return JVMTI_ERROR_NONE;
2174 } /* end check_top_frame */
2175 
2176 
2177 // ForceEarlyReturn<type> follows the PopFrame approach in many aspects.
2178 // Main difference is on the last stage in the interpreter.
2179 // The PopFrame stops method execution to continue execution
2180 // from the same method call instruction.
2181 // The ForceEarlyReturn forces return from method so the execution
2182 // continues at the bytecode following the method call.
2183 
2184 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
2185 
2186 jvmtiError
2187 JvmtiEnvBase::force_early_return(jthread thread, jvalue value, TosState tos) {
2188   JavaThread* current_thread = JavaThread::current();
2189   HandleMark hm(current_thread);
2190 
2191   JvmtiVTMSTransitionDisabler disabler(thread);
2192   ThreadsListHandle tlh(current_thread);
2193 
2194   JavaThread* java_thread = nullptr;
2195   oop thread_obj = nullptr;
2196   jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2197 
2198   if (err != JVMTI_ERROR_NONE) {
2199     return err;
2200   }
2201   Handle thread_handle(current_thread, thread_obj);
2202   bool self = java_thread == current_thread;
2203 
2204   err = check_non_suspended_or_opaque_frame(java_thread, thread_obj, self);
2205   if (err != JVMTI_ERROR_NONE) {
2206     return err;
2207   }
2208 
2209   // retrieve or create the state
2210   JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
2211   if (state == nullptr) {
2212     return JVMTI_ERROR_THREAD_NOT_ALIVE;
2213   }
2214 
2215   // Eagerly reallocate scalar replaced objects.
2216   EscapeBarrier eb(true, current_thread, java_thread);
2217   if (!eb.deoptimize_objects(0)) {
2218     // Reallocation of scalar replaced objects failed -> return with error
2219     return JVMTI_ERROR_OUT_OF_MEMORY;
2220   }
2221 
2222   MutexLocker mu(JvmtiThreadState_lock);
2223   SetForceEarlyReturn op(state, value, tos);
2224   JvmtiHandshake::execute(&op, &tlh, java_thread, thread_handle);
2225   return op.result();
2226 }
2227 
2228 void
2229 SetForceEarlyReturn::doit(Thread *target) {
2230   JavaThread* java_thread = JavaThread::cast(target);
2231   Thread* current_thread = Thread::current();
2232   HandleMark   hm(current_thread);
2233 
2234   if (java_thread->is_exiting()) {
2235     return; /* JVMTI_ERROR_THREAD_NOT_ALIVE (default) */
2236   }
2237 
2238   // Check to see if a ForceEarlyReturn was already in progress
2239   if (_state->is_earlyret_pending()) {
2240     // Probably possible for JVMTI clients to trigger this, but the
2241     // JPDA backend shouldn't allow this to happen
2242     _result = JVMTI_ERROR_INTERNAL;
2243     return;
2244   }
2245   {
2246     // The same as for PopFrame. Workaround bug:
2247     //  4812902: popFrame hangs if the method is waiting at a synchronize
2248     // Catch this condition and return an error to avoid hanging.
2249     // Now JVMTI spec allows an implementation to bail out with an opaque
2250     // frame error.
2251     OSThread* osThread = java_thread->osthread();
2252     if (osThread->get_state() == MONITOR_WAIT) {
2253       _result = JVMTI_ERROR_OPAQUE_FRAME;
2254       return;
2255     }
2256   }
2257 
2258   Handle ret_ob_h;
2259   _result = JvmtiEnvBase::check_top_frame(current_thread, java_thread, _value, _tos, &ret_ob_h);
2260   if (_result != JVMTI_ERROR_NONE) {
2261     return;
2262   }
2263   assert(_tos != atos || _value.l == nullptr || ret_ob_h() != nullptr,
2264          "return object oop must not be null if jobject is not null");
2265 
2266   // Update the thread state to reflect that the top frame must be
2267   // forced to return.
2268   // The current frame will be returned later when the suspended
2269   // thread is resumed and right before returning from VM to Java.
2270   // (see call_VM_base() in assembler_<cpu>.cpp).
2271 
2272   _state->set_earlyret_pending();
2273   _state->set_earlyret_oop(ret_ob_h());
2274   _state->set_earlyret_value(_value, _tos);
2275 
2276   // Set pending step flag for this early return.
2277   // It is cleared when next step event is posted.
2278   _state->set_pending_step_for_earlyret();
2279 }
2280 
2281 void
2282 JvmtiMonitorClosure::do_monitor(ObjectMonitor* mon) {
2283   if ( _error != JVMTI_ERROR_NONE) {
2284     // Error occurred in previous iteration so no need to add
2285     // to the list.
2286     return;
2287   }
2288   // Filter out on stack monitors collected during stack walk.
2289   oop obj = mon->object();
2290 
2291   if (obj == nullptr) {
2292     // This can happen if JNI code drops all references to the
2293     // owning object.
2294     return;
2295   }
2296 
2297   bool found = false;
2298   for (int j = 0; j < _owned_monitors_list->length(); j++) {
2299     jobject jobj = ((jvmtiMonitorStackDepthInfo*)_owned_monitors_list->at(j))->monitor;
2300     oop check = JNIHandles::resolve(jobj);
2301     if (check == obj) {
2302       // On stack monitor already collected during the stack walk.
2303       found = true;
2304       break;
2305     }
2306   }
2307   if (found == false) {
2308     // This is off stack monitor (e.g. acquired via jni MonitorEnter).
2309     jvmtiError err;
2310     jvmtiMonitorStackDepthInfo *jmsdi;
2311     err = _env->allocate(sizeof(jvmtiMonitorStackDepthInfo), (unsigned char **)&jmsdi);
2312     if (err != JVMTI_ERROR_NONE) {
2313       _error = err;
2314       return;
2315     }
2316     Handle hobj(Thread::current(), obj);
2317     jmsdi->monitor = _env->jni_reference(_calling_thread, hobj);
2318     // stack depth is unknown for this monitor.
2319     jmsdi->stack_depth = -1;
2320     _owned_monitors_list->append(jmsdi);
2321   }
2322 }
2323 
2324 GrowableArray<OopHandle>* JvmtiModuleClosure::_tbl = nullptr;
2325 
2326 void JvmtiModuleClosure::do_module(ModuleEntry* entry) {
2327   assert_locked_or_safepoint(Module_lock);
2328   OopHandle module = entry->module_handle();
2329   guarantee(module.resolve() != nullptr, "module object is null");
2330   _tbl->push(module);
2331 }
2332 
2333 jvmtiError
2334 JvmtiModuleClosure::get_all_modules(JvmtiEnv* env, jint* module_count_ptr, jobject** modules_ptr) {
2335   ResourceMark rm;
2336   MutexLocker mcld(ClassLoaderDataGraph_lock);
2337   MutexLocker ml(Module_lock);
2338 
2339   _tbl = new GrowableArray<OopHandle>(77);
2340   if (_tbl == nullptr) {
2341     return JVMTI_ERROR_OUT_OF_MEMORY;
2342   }
2343 
2344   // Iterate over all the modules loaded to the system.
2345   ClassLoaderDataGraph::modules_do(&do_module);
2346 
2347   jint len = _tbl->length();
2348   guarantee(len > 0, "at least one module must be present");
2349 
2350   jobject* array = (jobject*)env->jvmtiMalloc((jlong)(len * sizeof(jobject)));
2351   if (array == nullptr) {
2352     return JVMTI_ERROR_OUT_OF_MEMORY;
2353   }
2354   for (jint idx = 0; idx < len; idx++) {
2355     array[idx] = JNIHandles::make_local(_tbl->at(idx).resolve());
2356   }
2357   _tbl = nullptr;
2358   *modules_ptr = array;
2359   *module_count_ptr = len;
2360   return JVMTI_ERROR_NONE;
2361 }
2362 
2363 void
2364 UpdateForPopTopFrameClosure::doit(Thread *target) {
2365   Thread* current_thread  = Thread::current();
2366   HandleMark hm(current_thread);
2367   JavaThread* java_thread = JavaThread::cast(target);
2368 
2369   if (java_thread->is_exiting()) {
2370     return; /* JVMTI_ERROR_THREAD_NOT_ALIVE (default) */
2371   }
2372   assert(java_thread == _state->get_thread(), "Must be");
2373 
2374   // Check to see if a PopFrame was already in progress
2375   if (java_thread->popframe_condition() != JavaThread::popframe_inactive) {
2376     // Probably possible for JVMTI clients to trigger this, but the
2377     // JPDA backend shouldn't allow this to happen
2378     _result = JVMTI_ERROR_INTERNAL;
2379     return;
2380   }
2381 
2382   // Was workaround bug
2383   //    4812902: popFrame hangs if the method is waiting at a synchronize
2384   // Catch this condition and return an error to avoid hanging.
2385   // Now JVMTI spec allows an implementation to bail out with an opaque frame error.
2386   OSThread* osThread = java_thread->osthread();
2387   if (osThread->get_state() == MONITOR_WAIT) {
2388     _result = JVMTI_ERROR_OPAQUE_FRAME;
2389     return;
2390   }
2391 
2392   ResourceMark rm(current_thread);
2393   // Check if there is more than one Java frame in this thread, that the top two frames
2394   // are Java (not native) frames, and that there is no intervening VM frame
2395   int frame_count = 0;
2396   bool is_interpreted[2];
2397   intptr_t *frame_sp[2];
2398   // The 2-nd arg of constructor is needed to stop iterating at java entry frame.
2399   for (vframeStream vfs(java_thread, true, false /* process_frames */); !vfs.at_end(); vfs.next()) {
2400     methodHandle mh(current_thread, vfs.method());
2401     if (mh->is_native()) {
2402       _result = JVMTI_ERROR_OPAQUE_FRAME;
2403       return;
2404     }
2405     is_interpreted[frame_count] = vfs.is_interpreted_frame();
2406     frame_sp[frame_count] = vfs.frame_id();
2407     if (++frame_count > 1) break;
2408   }
2409   if (frame_count < 2)  {
2410     // We haven't found two adjacent non-native Java frames on the top.
2411     // There can be two situations here:
2412     //  1. There are no more java frames
2413     //  2. Two top java frames are separated by non-java native frames
2414     if (JvmtiEnvBase::jvf_for_thread_and_depth(java_thread, 1) == nullptr) {
2415       _result = JVMTI_ERROR_NO_MORE_FRAMES;
2416       return;
2417     } else {
2418       // Intervening non-java native or VM frames separate java frames.
2419       // Current implementation does not support this. See bug #5031735.
2420       // In theory it is possible to pop frames in such cases.
2421       _result = JVMTI_ERROR_OPAQUE_FRAME;
2422       return;
2423     }
2424   }
2425 
2426   // If any of the top 2 frames is a compiled one, need to deoptimize it
2427   for (int i = 0; i < 2; i++) {
2428     if (!is_interpreted[i]) {
2429       Deoptimization::deoptimize_frame(java_thread, frame_sp[i]);
2430     }
2431   }
2432 
2433   // Update the thread state to reflect that the top frame is popped
2434   // so that cur_stack_depth is maintained properly and all frameIDs
2435   // are invalidated.
2436   // The current frame will be popped later when the suspended thread
2437   // is resumed and right before returning from VM to Java.
2438   // (see call_VM_base() in assembler_<cpu>.cpp).
2439 
2440   // It's fine to update the thread state here because no JVMTI events
2441   // shall be posted for this PopFrame.
2442 
2443   _state->update_for_pop_top_frame();
2444   java_thread->set_popframe_condition(JavaThread::popframe_pending_bit);
2445   // Set pending step flag for this popframe and it is cleared when next
2446   // step event is posted.
2447   _state->set_pending_step_for_popframe();
2448   _result = JVMTI_ERROR_NONE;
2449 }
2450 
2451 void
2452 SetFramePopClosure::do_thread(Thread *target) {
2453   Thread* current = Thread::current();
2454   ResourceMark rm(current); // vframes are resource allocated
2455   JavaThread* java_thread = JavaThread::cast(target);
2456 
2457   if (java_thread->is_exiting()) {
2458     return; // JVMTI_ERROR_THREAD_NOT_ALIVE (default)
2459   }
2460 
2461   if (!_self && !java_thread->is_suspended()) {
2462     _result = JVMTI_ERROR_THREAD_NOT_SUSPENDED;
2463     return;
2464   }
2465   if (!java_thread->has_last_Java_frame()) {
2466     _result = JVMTI_ERROR_NO_MORE_FRAMES;
2467     return;
2468   }
2469   assert(_state->get_thread_or_saved() == java_thread, "Must be");
2470 
2471   RegisterMap reg_map(java_thread,
2472                       RegisterMap::UpdateMap::include,
2473                       RegisterMap::ProcessFrames::skip,
2474                       RegisterMap::WalkContinuation::include);
2475   javaVFrame* jvf = JvmtiEnvBase::get_cthread_last_java_vframe(java_thread, &reg_map);
2476   _result = ((JvmtiEnvBase*)_env)->set_frame_pop(_state, jvf, _depth);
2477 }
2478 
2479 void
2480 SetFramePopClosure::do_vthread(Handle target_h) {
2481   Thread* current = Thread::current();
2482   ResourceMark rm(current); // vframes are resource allocated
2483 
2484   if (!_self && !JvmtiVTSuspender::is_vthread_suspended(target_h())) {
2485     _result = JVMTI_ERROR_THREAD_NOT_SUSPENDED;
2486     return;
2487   }
2488   javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(target_h());
2489   _result = ((JvmtiEnvBase*)_env)->set_frame_pop(_state, jvf, _depth);
2490 }
2491 
2492 void
2493 GetOwnedMonitorInfoClosure::do_thread(Thread *target) {
2494   JavaThread *jt = JavaThread::cast(target);
2495   if (!jt->is_exiting() && (jt->threadObj() != nullptr)) {
2496     _result = ((JvmtiEnvBase *)_env)->get_owned_monitors(_calling_thread,
2497                                                          jt,
2498                                                          _owned_monitors_list);
2499   }
2500 }
2501 
2502 void
2503 GetOwnedMonitorInfoClosure::do_vthread(Handle target_h) {
2504   assert(_target_jt != nullptr, "sanity check");
2505   Thread* current = Thread::current();
2506   ResourceMark rm(current); // vframes are resource allocated
2507   HandleMark hm(current);
2508 
2509   javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(target_h());
2510 
2511   if (!_target_jt->is_exiting() && _target_jt->threadObj() != nullptr) {
2512     _result = ((JvmtiEnvBase *)_env)->get_owned_monitors(_calling_thread,
2513                                                          _target_jt,
2514                                                          jvf,
2515                                                          _owned_monitors_list);
2516   }
2517 }
2518 
2519 void
2520 GetCurrentContendedMonitorClosure::do_thread(Thread *target) {
2521   JavaThread *jt = JavaThread::cast(target);
2522   if (!jt->is_exiting() && (jt->threadObj() != nullptr)) {
2523     _result = ((JvmtiEnvBase *)_env)->get_current_contended_monitor(_calling_thread,
2524                                                                     jt,
2525                                                                     _owned_monitor_ptr,
2526                                                                     _is_virtual);
2527   }
2528 }
2529 
2530 void
2531 GetCurrentContendedMonitorClosure::do_vthread(Handle target_h) {
2532   if (_target_jt == nullptr) {
2533     _result = JVMTI_ERROR_NONE; // target virtual thread is unmounted
2534     return;
2535   }
2536   // mounted virtual thread case
2537   do_thread(_target_jt);
2538 }
2539 
2540 void
2541 GetStackTraceClosure::do_thread(Thread *target) {
2542   Thread* current = Thread::current();
2543   ResourceMark rm(current);
2544 
2545   JavaThread *jt = JavaThread::cast(target);
2546   if (!jt->is_exiting() && jt->threadObj() != nullptr) {
2547     _result = ((JvmtiEnvBase *)_env)->get_stack_trace(jt,
2548                                                       _start_depth, _max_count,
2549                                                       _frame_buffer, _count_ptr);
2550   }
2551 }
2552 
2553 void
2554 GetStackTraceClosure::do_vthread(Handle target_h) {
2555   Thread* current = Thread::current();
2556   ResourceMark rm(current);
2557 
2558   javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(target_h());
2559   _result = ((JvmtiEnvBase *)_env)->get_stack_trace(jvf,
2560                                                     _start_depth, _max_count,
2561                                                     _frame_buffer, _count_ptr);
2562 }
2563 
2564 #ifdef ASSERT
2565 void
2566 PrintStackTraceClosure::do_thread_impl(Thread *target) {
2567   JavaThread *java_thread = JavaThread::cast(target);
2568   Thread *current_thread = Thread::current();
2569 
2570   ResourceMark rm (current_thread);
2571   const char* tname = JvmtiTrace::safe_get_thread_name(java_thread);
2572   oop t_oop = java_thread->jvmti_vthread();
2573   t_oop = t_oop == nullptr ? java_thread->threadObj() : t_oop;
2574   bool is_vt_suspended = java_lang_VirtualThread::is_instance(t_oop) && JvmtiVTSuspender::is_vthread_suspended(t_oop);
2575 
2576   log_error(jvmti)("%s(%s) exiting: %d is_susp: %d is_thread_susp: %d is_vthread_susp: %d "
2577                    "is_VTMS_transition_disabler: %d, is_in_VTMS_transition = %d\n",
2578                    tname, java_thread->name(), java_thread->is_exiting(),
2579                    java_thread->is_suspended(), java_thread->is_carrier_thread_suspended(), is_vt_suspended,
2580                    java_thread->is_VTMS_transition_disabler(), java_thread->is_in_VTMS_transition());
2581 
2582   if (java_thread->has_last_Java_frame()) {
2583     RegisterMap reg_map(java_thread,
2584                         RegisterMap::UpdateMap::include,
2585                         RegisterMap::ProcessFrames::include,
2586                         RegisterMap::WalkContinuation::skip);
2587     ResourceMark rm(current_thread);
2588     HandleMark hm(current_thread);
2589     javaVFrame *jvf = java_thread->last_java_vframe(&reg_map);
2590     while (jvf != nullptr) {
2591       log_error(jvmti)("  %s:%d",
2592                        jvf->method()->external_name(),
2593                        jvf->method()->line_number_from_bci(jvf->bci()));
2594       jvf = jvf->java_sender();
2595     }
2596   }
2597   log_error(jvmti)("\n");
2598 }
2599 
2600 void
2601 PrintStackTraceClosure::do_thread(Thread *target) {
2602   JavaThread *java_thread = JavaThread::cast(target);
2603   Thread *current_thread = Thread::current();
2604 
2605   assert(SafepointSynchronize::is_at_safepoint() ||
2606          java_thread->is_handshake_safe_for(current_thread),
2607          "call by myself / at safepoint / at handshake");
2608 
2609   PrintStackTraceClosure::do_thread_impl(target);
2610 }
2611 #endif
2612 
2613 void
2614 GetFrameCountClosure::do_thread(Thread *target) {
2615   JavaThread* jt = JavaThread::cast(target);
2616   assert(target == jt, "just checking");
2617 
2618   if (!jt->is_exiting() && jt->threadObj() != nullptr) {
2619     _result = ((JvmtiEnvBase*)_env)->get_frame_count(jt, _count_ptr);
2620   }
2621 }
2622 
2623 void
2624 GetFrameCountClosure::do_vthread(Handle target_h) {
2625   _result = ((JvmtiEnvBase*)_env)->get_frame_count(target_h(), _count_ptr);
2626 }
2627 
2628 void
2629 GetFrameLocationClosure::do_thread(Thread *target) {
2630   JavaThread *jt = JavaThread::cast(target);
2631   assert(target == jt, "just checking");
2632 
2633   if (!jt->is_exiting() && jt->threadObj() != nullptr) {
2634     _result = ((JvmtiEnvBase*)_env)->get_frame_location(jt, _depth,
2635                                                         _method_ptr, _location_ptr);
2636   }
2637 }
2638 
2639 void
2640 GetFrameLocationClosure::do_vthread(Handle target_h) {
2641   _result = ((JvmtiEnvBase*)_env)->get_frame_location(target_h(), _depth,
2642                                                       _method_ptr, _location_ptr);
2643 }