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   assert(k->is_loader_alive(), "Must be alive");
 603   Thread *thread = Thread::current();
 604   return (jclass)jni_reference(Handle(thread, k->java_mirror()));
 605 }
 606 
 607 //
 608 // Field Information
 609 //
 610 
 611 bool
 612 JvmtiEnvBase::get_field_descriptor(Klass* k, jfieldID field, fieldDescriptor* fd) {
 613   if (!jfieldIDWorkaround::is_valid_jfieldID(k, field)) {
 614     return false;
 615   }
 616   bool found = false;
 617   if (jfieldIDWorkaround::is_static_jfieldID(field)) {
 618     JNIid* id = jfieldIDWorkaround::from_static_jfieldID(field);
 619     found = id->find_local_field(fd);
 620   } else {
 621     // Non-static field. The fieldID is really the offset of the field within the object.
 622     int offset = jfieldIDWorkaround::from_instance_jfieldID(k, field);
 623     found = InstanceKlass::cast(k)->find_field_from_offset(offset, false, fd);
 624   }
 625   return found;
 626 }
 627 
 628 bool
 629 JvmtiEnvBase::is_vthread_alive(oop vt) {
 630   oop cont = java_lang_VirtualThread::continuation(vt);
 631   return !jdk_internal_vm_Continuation::done(cont) &&
 632          java_lang_VirtualThread::state(vt) != java_lang_VirtualThread::NEW;
 633 }
 634 
 635 // Return JavaThread if virtual thread is mounted, null otherwise.
 636 JavaThread* JvmtiEnvBase::get_JavaThread_or_null(oop vthread) {
 637   oop carrier_thread = java_lang_VirtualThread::carrier_thread(vthread);
 638   if (carrier_thread == nullptr) {
 639     return nullptr;
 640   }
 641 
 642   JavaThread* java_thread = java_lang_Thread::thread(carrier_thread);
 643 
 644   // This could be a different thread to the current one. So we need to ensure that
 645   // processing has started before we are allowed to read the continuation oop of
 646   // another thread, as it is a direct root of that other thread.
 647   StackWatermarkSet::start_processing(java_thread, StackWatermarkKind::gc);
 648 
 649   oop cont = java_lang_VirtualThread::continuation(vthread);
 650   assert(cont != nullptr, "must be");
 651   assert(Continuation::continuation_scope(cont) == java_lang_VirtualThread::vthread_scope(), "must be");
 652   return Continuation::is_continuation_mounted(java_thread, cont) ? java_thread : nullptr;
 653 }
 654 
 655 javaVFrame*
 656 JvmtiEnvBase::check_and_skip_hidden_frames(bool is_in_VTMS_transition, javaVFrame* jvf) {
 657   // The second condition is needed to hide notification methods.
 658   if (!is_in_VTMS_transition && (jvf == nullptr || !jvf->method()->jvmti_mount_transition())) {
 659     return jvf;  // No frames to skip.
 660   }
 661   // Find jvf with a method annotated with @JvmtiMountTransition.
 662   for ( ; jvf != nullptr; jvf = jvf->java_sender()) {
 663     if (jvf->method()->jvmti_mount_transition()) {  // Cannot actually appear in an unmounted continuation; they're never frozen.
 664       jvf = jvf->java_sender();  // Skip annotated method.
 665       break;
 666     }
 667     if (jvf->method()->changes_current_thread()) {
 668       break;
 669     }
 670     // Skip frame above annotated method.
 671   }
 672   return jvf;
 673 }
 674 
 675 javaVFrame*
 676 JvmtiEnvBase::check_and_skip_hidden_frames(JavaThread* jt, javaVFrame* jvf) {
 677   jvf = check_and_skip_hidden_frames(jt->is_in_VTMS_transition(), jvf);
 678   return jvf;
 679 }
 680 
 681 javaVFrame*
 682 JvmtiEnvBase::check_and_skip_hidden_frames(oop vthread, javaVFrame* jvf) {
 683   JvmtiThreadState* state = java_lang_Thread::jvmti_thread_state(vthread);
 684   if (state == nullptr) {
 685     // nothing to skip
 686     return jvf;
 687   }
 688   jvf = check_and_skip_hidden_frames(java_lang_Thread::is_in_VTMS_transition(vthread), jvf);
 689   return jvf;
 690 }
 691 
 692 javaVFrame*
 693 JvmtiEnvBase::get_vthread_jvf(oop vthread) {
 694   assert(java_lang_VirtualThread::state(vthread) != java_lang_VirtualThread::NEW, "sanity check");
 695   assert(java_lang_VirtualThread::state(vthread) != java_lang_VirtualThread::TERMINATED, "sanity check");
 696 
 697   Thread* cur_thread = Thread::current();
 698   oop cont = java_lang_VirtualThread::continuation(vthread);
 699   javaVFrame* jvf = nullptr;
 700 
 701   JavaThread* java_thread = get_JavaThread_or_null(vthread);
 702   if (java_thread != nullptr) {
 703     if (!java_thread->has_last_Java_frame()) {
 704       // TBD: This is a temporary work around to avoid a guarantee caused by
 705       // the native enterSpecial frame on the top. No frames will be found
 706       // by the JVMTI functions such as GetStackTrace.
 707       return nullptr;
 708     }
 709     vframeStream vfs(java_thread);
 710     jvf = vfs.at_end() ? nullptr : vfs.asJavaVFrame();
 711     jvf = check_and_skip_hidden_frames(java_thread, jvf);
 712   } else {
 713     vframeStream vfs(cont);
 714     jvf = vfs.at_end() ? nullptr : vfs.asJavaVFrame();
 715     jvf = check_and_skip_hidden_frames(vthread, jvf);
 716   }
 717   return jvf;
 718 }
 719 
 720 // Return correct javaVFrame for a carrier (non-virtual) thread.
 721 // It strips vthread frames at the top if there are any.
 722 javaVFrame*
 723 JvmtiEnvBase::get_cthread_last_java_vframe(JavaThread* jt, RegisterMap* reg_map_p) {
 724   // Strip vthread frames in case of carrier thread with mounted continuation.
 725   bool cthread_with_cont = JvmtiEnvBase::is_cthread_with_continuation(jt);
 726   javaVFrame *jvf = cthread_with_cont ? jt->carrier_last_java_vframe(reg_map_p)
 727                                       : jt->last_java_vframe(reg_map_p);
 728   // Skip hidden frames only for carrier threads
 729   // which are in non-temporary VTMS transition.
 730   if (jt->is_in_VTMS_transition()) {
 731     jvf = check_and_skip_hidden_frames(jt, jvf);
 732   }
 733   return jvf;
 734 }
 735 
 736 jint
 737 JvmtiEnvBase::get_thread_state_base(oop thread_oop, JavaThread* jt) {
 738   jint state = 0;
 739 
 740   if (thread_oop != nullptr) {
 741     // Get most state bits.
 742     state = (jint)java_lang_Thread::get_thread_status(thread_oop);
 743   }
 744   if (jt != nullptr) {
 745     // We have a JavaThread* so add more state bits.
 746     JavaThreadState jts = jt->thread_state();
 747 
 748     if (jt->is_carrier_thread_suspended() ||
 749         ((jt->jvmti_vthread() == nullptr || jt->jvmti_vthread() == thread_oop) && jt->is_suspended())) {
 750       // Suspended non-virtual thread.
 751       state |= JVMTI_THREAD_STATE_SUSPENDED;
 752     }
 753     if (jts == _thread_in_native) {
 754       state |= JVMTI_THREAD_STATE_IN_NATIVE;
 755     }
 756     if (jt->is_interrupted(false)) {
 757       state |= JVMTI_THREAD_STATE_INTERRUPTED;
 758     }
 759   }
 760   return state;
 761 }
 762 
 763 jint
 764 JvmtiEnvBase::get_thread_state(oop thread_oop, JavaThread* jt) {
 765   jint state = 0;
 766 
 767   if (is_thread_carrying_vthread(jt, thread_oop)) {
 768     state = (jint)java_lang_Thread::get_thread_status(thread_oop);
 769 
 770     // This is for extra safety. Other bits are not expected nor needed.
 771     state &= (JVMTI_THREAD_STATE_ALIVE | JVMTI_THREAD_STATE_INTERRUPTED);
 772 
 773     if (jt->is_carrier_thread_suspended()) {
 774       state |= JVMTI_THREAD_STATE_SUSPENDED;
 775     }
 776     // It's okay for the JVMTI state to be reported as WAITING when waiting
 777     // for something other than an Object.wait. So, we treat a thread carrying
 778     // a virtual thread as waiting indefinitely which is not runnable.
 779     // It is why the RUNNABLE bit is not needed and the WAITING bits are added.
 780     state |= JVMTI_THREAD_STATE_WAITING | JVMTI_THREAD_STATE_WAITING_INDEFINITELY;
 781   } else {
 782     state = get_thread_state_base(thread_oop, jt);
 783   }
 784   return state;
 785 }
 786 
 787 jint
 788 JvmtiEnvBase::get_vthread_state(oop thread_oop, JavaThread* java_thread) {
 789   jint state = 0;
 790   bool ext_suspended = JvmtiVTSuspender::is_vthread_suspended(thread_oop);
 791   jint interrupted = java_lang_Thread::interrupted(thread_oop);
 792 
 793   if (java_thread != nullptr) {
 794     // If virtual thread is blocked on a monitor enter the BLOCKED_ON_MONITOR_ENTER bit
 795     // is set for carrier thread instead of virtual.
 796     // Other state bits except filtered ones are expected to be the same.
 797     oop ct_oop = java_lang_VirtualThread::carrier_thread(thread_oop);
 798     jint filtered_bits = JVMTI_THREAD_STATE_SUSPENDED | JVMTI_THREAD_STATE_INTERRUPTED;
 799 
 800     // This call can trigger a safepoint, so thread_oop must not be used after it.
 801     state = get_thread_state_base(ct_oop, java_thread) & ~filtered_bits;
 802   } else {
 803     int vt_state = java_lang_VirtualThread::state(thread_oop);
 804     state = (jint)java_lang_VirtualThread::map_state_to_thread_status(vt_state);
 805   }
 806   // Ensure the thread has not exited after retrieving suspended/interrupted values.
 807   if ((state & JVMTI_THREAD_STATE_ALIVE) != 0) {
 808     if (ext_suspended) {
 809       state |= JVMTI_THREAD_STATE_SUSPENDED;
 810     }
 811     if (interrupted) {
 812       state |= JVMTI_THREAD_STATE_INTERRUPTED;
 813     }
 814   }
 815   return state;
 816 }
 817 
 818 jint
 819 JvmtiEnvBase::get_thread_or_vthread_state(oop thread_oop, JavaThread* java_thread) {
 820   jint state = 0;
 821   if (java_lang_VirtualThread::is_instance(thread_oop)) {
 822     state = JvmtiEnvBase::get_vthread_state(thread_oop, java_thread);
 823   } else {
 824     state = JvmtiEnvBase::get_thread_state(thread_oop, java_thread);
 825   }
 826   return state;
 827 }
 828 
 829 jvmtiError
 830 JvmtiEnvBase::get_live_threads(JavaThread* current_thread, Handle group_hdl, jint *count_ptr, Handle **thread_objs_p) {
 831   jint count = 0;
 832   Handle *thread_objs = nullptr;
 833   ThreadsListEnumerator tle(current_thread, /* include_jvmti_agent_threads */ true);
 834   int nthreads = tle.num_threads();
 835   if (nthreads > 0) {
 836     thread_objs = NEW_RESOURCE_ARRAY_RETURN_NULL(Handle, nthreads);
 837     NULL_CHECK(thread_objs, JVMTI_ERROR_OUT_OF_MEMORY);
 838     for (int i = 0; i < nthreads; i++) {
 839       Handle thread = tle.get_threadObj(i);
 840       if (thread()->is_a(vmClasses::Thread_klass()) && java_lang_Thread::threadGroup(thread()) == group_hdl()) {
 841         thread_objs[count++] = thread;
 842       }
 843     }
 844   }
 845   *thread_objs_p = thread_objs;
 846   *count_ptr = count;
 847   return JVMTI_ERROR_NONE;
 848 }
 849 
 850 jvmtiError
 851 JvmtiEnvBase::get_subgroups(JavaThread* current_thread, Handle group_hdl, jint *count_ptr, objArrayHandle *group_objs_p) {
 852 
 853   // This call collects the strong and weak groups
 854   JavaThread* THREAD = current_thread;
 855   JavaValue result(T_OBJECT);
 856   JavaCalls::call_virtual(&result,
 857                           group_hdl,
 858                           vmClasses::ThreadGroup_klass(),
 859                           SymbolTable::new_permanent_symbol("subgroupsAsArray"),
 860                           vmSymbols::void_threadgroup_array_signature(),
 861                           THREAD);
 862   if (HAS_PENDING_EXCEPTION) {
 863     Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
 864     CLEAR_PENDING_EXCEPTION;
 865     if (ex_name == vmSymbols::java_lang_OutOfMemoryError()) {
 866       return JVMTI_ERROR_OUT_OF_MEMORY;
 867     } else {
 868       return JVMTI_ERROR_INTERNAL;
 869     }
 870   }
 871 
 872   assert(result.get_type() == T_OBJECT, "just checking");
 873   objArrayOop groups = (objArrayOop)result.get_oop();
 874 
 875   *count_ptr = groups == nullptr ? 0 : groups->length();
 876   *group_objs_p = objArrayHandle(current_thread, groups);
 877 
 878   return JVMTI_ERROR_NONE;
 879 }
 880 
 881 //
 882 // Object Monitor Information
 883 //
 884 
 885 //
 886 // Count the number of objects for a lightweight monitor. The hobj
 887 // parameter is object that owns the monitor so this routine will
 888 // count the number of times the same object was locked by frames
 889 // in java_thread.
 890 //
 891 jint
 892 JvmtiEnvBase::count_locked_objects(JavaThread *java_thread, Handle hobj) {
 893   jint ret = 0;
 894   if (!java_thread->has_last_Java_frame()) {
 895     return ret;  // no Java frames so no monitors
 896   }
 897 
 898   Thread* current_thread = Thread::current();
 899   ResourceMark rm(current_thread);
 900   HandleMark   hm(current_thread);
 901   RegisterMap  reg_map(java_thread,
 902                        RegisterMap::UpdateMap::include,
 903                        RegisterMap::ProcessFrames::include,
 904                        RegisterMap::WalkContinuation::skip);
 905 
 906   for (javaVFrame *jvf = java_thread->last_java_vframe(&reg_map); jvf != nullptr;
 907        jvf = jvf->java_sender()) {
 908     GrowableArray<MonitorInfo*>* mons = jvf->monitors();
 909     if (!mons->is_empty()) {
 910       for (int i = 0; i < mons->length(); i++) {
 911         MonitorInfo *mi = mons->at(i);
 912         if (mi->owner_is_scalar_replaced()) continue;
 913 
 914         // see if owner of the monitor is our object
 915         if (mi->owner() != nullptr && mi->owner() == hobj()) {
 916           ret++;
 917         }
 918       }
 919     }
 920   }
 921   return ret;
 922 }
 923 
 924 jvmtiError
 925 JvmtiEnvBase::get_current_contended_monitor(JavaThread *calling_thread, JavaThread *java_thread,
 926                                             jobject *monitor_ptr, bool is_virtual) {
 927   Thread *current_thread = Thread::current();
 928   assert(java_thread->is_handshake_safe_for(current_thread),
 929          "call by myself or at handshake");
 930   if (!is_virtual && JvmtiEnvBase::is_cthread_with_continuation(java_thread)) {
 931     // Carrier thread with a mounted continuation case.
 932     // No contended monitor can be owned by carrier thread in this case.
 933     *monitor_ptr = nullptr;
 934     return JVMTI_ERROR_NONE;
 935   }
 936   oop obj = nullptr;
 937   // The ObjectMonitor* can't be async deflated since we are either
 938   // at a safepoint or the calling thread is operating on itself so
 939   // it cannot leave the underlying wait()/enter() call.
 940   ObjectMonitor *mon = java_thread->current_waiting_monitor();
 941   if (mon == nullptr) {
 942     // thread is not doing an Object.wait() call
 943     mon = java_thread->current_pending_monitor();
 944     if (mon != nullptr) {
 945       // The thread is trying to enter() an ObjectMonitor.
 946       obj = mon->object();
 947       assert(obj != nullptr, "ObjectMonitor should have a valid object!");
 948     }
 949   } else {
 950     // thread is doing an Object.wait() call
 951     oop thread_oop = get_vthread_or_thread_oop(java_thread);
 952     jint state = get_thread_or_vthread_state(thread_oop, java_thread);
 953 
 954     if (state & JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER) {
 955       // thread is re-entering the monitor in an Object.wait() call
 956       obj = mon->object();
 957       assert(obj != nullptr, "Object.wait() should have an object");
 958     }
 959   }
 960 
 961   if (obj == nullptr) {
 962     *monitor_ptr = nullptr;
 963   } else {
 964     HandleMark hm(current_thread);
 965     Handle     hobj(current_thread, obj);
 966     *monitor_ptr = jni_reference(calling_thread, hobj);
 967   }
 968   return JVMTI_ERROR_NONE;
 969 }
 970 
 971 jvmtiError
 972 JvmtiEnvBase::get_owned_monitors(JavaThread *calling_thread, JavaThread* java_thread,
 973                                  GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list) {
 974   // Note:
 975   // calling_thread is the thread that requested the list of monitors for java_thread.
 976   // java_thread is the thread owning the monitors.
 977   // current_thread is the thread executing this code, can be a non-JavaThread (e.g. VM Thread).
 978   // And they all may be different threads.
 979   jvmtiError err = JVMTI_ERROR_NONE;
 980   Thread *current_thread = Thread::current();
 981   assert(java_thread->is_handshake_safe_for(current_thread),
 982          "call by myself or at handshake");
 983 
 984   if (JvmtiEnvBase::is_cthread_with_continuation(java_thread)) {
 985     // Carrier thread with a mounted continuation case.
 986     // No contended monitor can be owned by carrier thread in this case.
 987     return JVMTI_ERROR_NONE;
 988   }
 989   if (java_thread->has_last_Java_frame()) {
 990     ResourceMark rm(current_thread);
 991     HandleMark   hm(current_thread);
 992     RegisterMap  reg_map(java_thread,
 993                          RegisterMap::UpdateMap::include,
 994                          RegisterMap::ProcessFrames::include,
 995                          RegisterMap::WalkContinuation::skip);
 996 
 997     int depth = 0;
 998     for (javaVFrame *jvf = get_cthread_last_java_vframe(java_thread, &reg_map);
 999          jvf != nullptr; jvf = jvf->java_sender()) {
1000       if (MaxJavaStackTraceDepth == 0 || depth++ < MaxJavaStackTraceDepth) {  // check for stack too deep
1001         // add locked objects for this frame into list
1002         err = get_locked_objects_in_frame(calling_thread, java_thread, jvf, owned_monitors_list, depth-1);
1003         if (err != JVMTI_ERROR_NONE) {
1004           return err;
1005         }
1006       }
1007     }
1008   }
1009 
1010   // Get off stack monitors. (e.g. acquired via jni MonitorEnter).
1011   JvmtiMonitorClosure jmc(calling_thread, owned_monitors_list, this);
1012   ObjectSynchronizer::owned_monitors_iterate(&jmc, java_thread);
1013   err = jmc.error();
1014 
1015   return err;
1016 }
1017 
1018 jvmtiError
1019 JvmtiEnvBase::get_owned_monitors(JavaThread* calling_thread, JavaThread* java_thread, javaVFrame* jvf,
1020                                  GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list) {
1021   jvmtiError err = JVMTI_ERROR_NONE;
1022   Thread *current_thread = Thread::current();
1023   assert(java_thread->is_handshake_safe_for(current_thread),
1024          "call by myself or at handshake");
1025 
1026   int depth = 0;
1027   for ( ; jvf != nullptr; jvf = jvf->java_sender()) {
1028     if (MaxJavaStackTraceDepth == 0 || depth++ < MaxJavaStackTraceDepth) {  // check for stack too deep
1029       // Add locked objects for this frame into list.
1030       err = get_locked_objects_in_frame(calling_thread, java_thread, jvf, owned_monitors_list, depth - 1);
1031       if (err != JVMTI_ERROR_NONE) {
1032         return err;
1033       }
1034     }
1035   }
1036 
1037   // Get off stack monitors. (e.g. acquired via jni MonitorEnter).
1038   JvmtiMonitorClosure jmc(calling_thread, owned_monitors_list, this);
1039   ObjectSynchronizer::owned_monitors_iterate(&jmc, java_thread);
1040   err = jmc.error();
1041 
1042   return err;
1043 }
1044 
1045 // Save JNI local handles for any objects that this frame owns.
1046 jvmtiError
1047 JvmtiEnvBase::get_locked_objects_in_frame(JavaThread* calling_thread, JavaThread* java_thread,
1048                                  javaVFrame *jvf, GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitors_list, jint stack_depth) {
1049   jvmtiError err = JVMTI_ERROR_NONE;
1050   Thread* current_thread = Thread::current();
1051   ResourceMark rm(current_thread);
1052   HandleMark   hm(current_thread);
1053 
1054   GrowableArray<MonitorInfo*>* mons = jvf->monitors();
1055   if (mons->is_empty()) {
1056     return err;  // this javaVFrame holds no monitors
1057   }
1058 
1059   oop wait_obj = nullptr;
1060   {
1061     // The ObjectMonitor* can't be async deflated since we are either
1062     // at a safepoint or the calling thread is operating on itself so
1063     // it cannot leave the underlying wait() call.
1064     // Save object of current wait() call (if any) for later comparison.
1065     ObjectMonitor *mon = java_thread->current_waiting_monitor();
1066     if (mon != nullptr) {
1067       wait_obj = mon->object();
1068     }
1069   }
1070   oop pending_obj = nullptr;
1071   {
1072     // The ObjectMonitor* can't be async deflated since we are either
1073     // at a safepoint or the calling thread is operating on itself so
1074     // it cannot leave the underlying enter() call.
1075     // Save object of current enter() call (if any) for later comparison.
1076     ObjectMonitor *mon = java_thread->current_pending_monitor();
1077     if (mon != nullptr) {
1078       pending_obj = mon->object();
1079     }
1080   }
1081 
1082   for (int i = 0; i < mons->length(); i++) {
1083     MonitorInfo *mi = mons->at(i);
1084 
1085     if (mi->owner_is_scalar_replaced()) continue;
1086 
1087     oop obj = mi->owner();
1088     if (obj == nullptr) {
1089       // this monitor doesn't have an owning object so skip it
1090       continue;
1091     }
1092 
1093     if (wait_obj == obj) {
1094       // the thread is waiting on this monitor so it isn't really owned
1095       continue;
1096     }
1097 
1098     if (pending_obj == obj) {
1099       // the thread is pending on this monitor so it isn't really owned
1100       continue;
1101     }
1102 
1103     if (owned_monitors_list->length() > 0) {
1104       // Our list has at least one object on it so we have to check
1105       // for recursive object locking
1106       bool found = false;
1107       for (int j = 0; j < owned_monitors_list->length(); j++) {
1108         jobject jobj = ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(j))->monitor;
1109         oop check = JNIHandles::resolve(jobj);
1110         if (check == obj) {
1111           found = true;  // we found the object
1112           break;
1113         }
1114       }
1115 
1116       if (found) {
1117         // already have this object so don't include it
1118         continue;
1119       }
1120     }
1121 
1122     // add the owning object to our list
1123     jvmtiMonitorStackDepthInfo *jmsdi;
1124     err = allocate(sizeof(jvmtiMonitorStackDepthInfo), (unsigned char **)&jmsdi);
1125     if (err != JVMTI_ERROR_NONE) {
1126         return err;
1127     }
1128     Handle hobj(Thread::current(), obj);
1129     jmsdi->monitor = jni_reference(calling_thread, hobj);
1130     jmsdi->stack_depth = stack_depth;
1131     owned_monitors_list->append(jmsdi);
1132   }
1133 
1134   return err;
1135 }
1136 
1137 jvmtiError
1138 JvmtiEnvBase::get_stack_trace(javaVFrame *jvf,
1139                               jint start_depth, jint max_count,
1140                               jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
1141   Thread *current_thread = Thread::current();
1142   ResourceMark rm(current_thread);
1143   HandleMark hm(current_thread);
1144   int count = 0;
1145 
1146   if (start_depth != 0) {
1147     if (start_depth > 0) {
1148       for (int j = 0; j < start_depth && jvf != nullptr; j++) {
1149         jvf = jvf->java_sender();
1150       }
1151       if (jvf == nullptr) {
1152         // start_depth is deeper than the stack depth.
1153         return JVMTI_ERROR_ILLEGAL_ARGUMENT;
1154       }
1155     } else { // start_depth < 0
1156       // We are referencing the starting depth based on the oldest
1157       // part of the stack.
1158       // Optimize to limit the number of times that java_sender() is called.
1159       javaVFrame *jvf_cursor = jvf;
1160       javaVFrame *jvf_prev = nullptr;
1161       javaVFrame *jvf_prev_prev = nullptr;
1162       int j = 0;
1163       while (jvf_cursor != nullptr) {
1164         jvf_prev_prev = jvf_prev;
1165         jvf_prev = jvf_cursor;
1166         for (j = 0; j > start_depth && jvf_cursor != nullptr; j--) {
1167           jvf_cursor = jvf_cursor->java_sender();
1168         }
1169       }
1170       if (j == start_depth) {
1171         // Previous pointer is exactly where we want to start.
1172         jvf = jvf_prev;
1173       } else {
1174         // We need to back up further to get to the right place.
1175         if (jvf_prev_prev == nullptr) {
1176           // The -start_depth is greater than the stack depth.
1177           return JVMTI_ERROR_ILLEGAL_ARGUMENT;
1178         }
1179         // j is now the number of frames on the stack starting with
1180         // jvf_prev, we start from jvf_prev_prev and move older on
1181         // the stack that many, and the result is -start_depth frames
1182         // remaining.
1183         jvf = jvf_prev_prev;
1184         for (; j < 0; j++) {
1185           jvf = jvf->java_sender();
1186         }
1187       }
1188     }
1189   }
1190   for (; count < max_count && jvf != nullptr; count++) {
1191     frame_buffer[count].method = jvf->method()->jmethod_id();
1192     frame_buffer[count].location = (jvf->method()->is_native() ? -1 : jvf->bci());
1193     jvf = jvf->java_sender();
1194   }
1195   *count_ptr = count;
1196   return JVMTI_ERROR_NONE;
1197 }
1198 
1199 jvmtiError
1200 JvmtiEnvBase::get_stack_trace(JavaThread *java_thread,
1201                               jint start_depth, jint max_count,
1202                               jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
1203   Thread *current_thread = Thread::current();
1204   assert(SafepointSynchronize::is_at_safepoint() ||
1205          java_thread->is_handshake_safe_for(current_thread),
1206          "call by myself / at safepoint / at handshake");
1207   int count = 0;
1208   jvmtiError err = JVMTI_ERROR_NONE;
1209 
1210   if (java_thread->has_last_Java_frame()) {
1211     RegisterMap reg_map(java_thread,
1212                         RegisterMap::UpdateMap::include,
1213                         RegisterMap::ProcessFrames::skip,
1214                         RegisterMap::WalkContinuation::skip);
1215     ResourceMark rm(current_thread);
1216     javaVFrame *jvf = get_cthread_last_java_vframe(java_thread, &reg_map);
1217 
1218     err = get_stack_trace(jvf, start_depth, max_count, frame_buffer, count_ptr);
1219   } else {
1220     *count_ptr = 0;
1221     if (start_depth != 0) {
1222       // no frames and there is a starting depth
1223       err = JVMTI_ERROR_ILLEGAL_ARGUMENT;
1224     }
1225   }
1226   return err;
1227 }
1228 
1229 jint
1230 JvmtiEnvBase::get_frame_count(javaVFrame *jvf) {
1231   int count = 0;
1232 
1233   while (jvf != nullptr) {
1234     jvf = jvf->java_sender();
1235     count++;
1236   }
1237   return count;
1238 }
1239 
1240 jvmtiError
1241 JvmtiEnvBase::get_frame_count(JavaThread* jt, jint *count_ptr) {
1242   Thread *current_thread = Thread::current();
1243   assert(current_thread == jt ||
1244          SafepointSynchronize::is_at_safepoint() ||
1245          jt->is_handshake_safe_for(current_thread),
1246          "call by myself / at safepoint / at handshake");
1247 
1248   if (!jt->has_last_Java_frame()) { // no Java frames
1249     *count_ptr = 0;
1250   } else {
1251     ResourceMark rm(current_thread);
1252     RegisterMap reg_map(jt,
1253                         RegisterMap::UpdateMap::include,
1254                         RegisterMap::ProcessFrames::include,
1255                         RegisterMap::WalkContinuation::skip);
1256     javaVFrame *jvf = get_cthread_last_java_vframe(jt, &reg_map);
1257 
1258     *count_ptr = get_frame_count(jvf);
1259   }
1260   return JVMTI_ERROR_NONE;
1261 }
1262 
1263 jvmtiError
1264 JvmtiEnvBase::get_frame_count(oop vthread_oop, jint *count_ptr) {
1265   Thread *current_thread = Thread::current();
1266   ResourceMark rm(current_thread);
1267   javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(vthread_oop);
1268 
1269   *count_ptr = get_frame_count(jvf);
1270   return JVMTI_ERROR_NONE;
1271 }
1272 
1273 jvmtiError
1274 JvmtiEnvBase::get_frame_location(javaVFrame* jvf, jint depth,
1275                                  jmethodID* method_ptr, jlocation* location_ptr) {
1276   int cur_depth = 0;
1277 
1278   while (jvf != nullptr && cur_depth < depth) {
1279     jvf = jvf->java_sender();
1280     cur_depth++;
1281   }
1282   assert(depth >= cur_depth, "ran out of frames too soon");
1283   if (jvf == nullptr) {
1284     return JVMTI_ERROR_NO_MORE_FRAMES;
1285   }
1286   Method* method = jvf->method();
1287   if (method->is_native()) {
1288     *location_ptr = -1;
1289   } else {
1290     *location_ptr = jvf->bci();
1291   }
1292   *method_ptr = method->jmethod_id();
1293   return JVMTI_ERROR_NONE;
1294 }
1295 
1296 jvmtiError
1297 JvmtiEnvBase::get_frame_location(JavaThread *java_thread, jint depth,
1298                                  jmethodID* method_ptr, jlocation* location_ptr) {
1299   Thread* current = Thread::current();
1300   assert(java_thread->is_handshake_safe_for(current),
1301          "call by myself or at handshake");
1302   if (!java_thread->has_last_Java_frame()) {
1303     return JVMTI_ERROR_NO_MORE_FRAMES;
1304   }
1305   ResourceMark rm(current);
1306   HandleMark hm(current);
1307   RegisterMap reg_map(java_thread,
1308                       RegisterMap::UpdateMap::include,
1309                       RegisterMap::ProcessFrames::skip,
1310                       RegisterMap::WalkContinuation::include);
1311   javaVFrame* jvf = JvmtiEnvBase::get_cthread_last_java_vframe(java_thread, &reg_map);
1312 
1313   return get_frame_location(jvf, depth, method_ptr, location_ptr);
1314 }
1315 
1316 jvmtiError
1317 JvmtiEnvBase::get_frame_location(oop vthread_oop, jint depth,
1318                                  jmethodID* method_ptr, jlocation* location_ptr) {
1319   Thread* current = Thread::current();
1320   ResourceMark rm(current);
1321   HandleMark hm(current);
1322   javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(vthread_oop);
1323 
1324   return get_frame_location(jvf, depth, method_ptr, location_ptr);
1325 }
1326 
1327 jvmtiError
1328 JvmtiEnvBase::set_frame_pop(JvmtiThreadState* state, javaVFrame* jvf, jint depth) {
1329   for (int d = 0; jvf != nullptr && d < depth; d++) {
1330     jvf = jvf->java_sender();
1331   }
1332   if (jvf == nullptr) {
1333     return JVMTI_ERROR_NO_MORE_FRAMES;
1334   }
1335   if (jvf->method()->is_native()) {
1336     return JVMTI_ERROR_OPAQUE_FRAME;
1337   }
1338   assert(jvf->frame_pointer() != nullptr, "frame pointer mustn't be null");
1339   int frame_number = (int)get_frame_count(jvf);
1340   state->env_thread_state((JvmtiEnvBase*)this)->set_frame_pop(frame_number);
1341   return JVMTI_ERROR_NONE;
1342 }
1343 
1344 bool
1345 JvmtiEnvBase::is_cthread_with_mounted_vthread(JavaThread* jt) {
1346   oop thread_oop = jt->threadObj();
1347   assert(thread_oop != nullptr, "sanity check");
1348   oop mounted_vt = jt->jvmti_vthread();
1349 
1350   return mounted_vt != nullptr && mounted_vt != thread_oop;
1351 }
1352 
1353 bool
1354 JvmtiEnvBase::is_cthread_with_continuation(JavaThread* jt) {
1355   const ContinuationEntry* cont_entry = nullptr;
1356   if (jt->has_last_Java_frame()) {
1357     cont_entry = jt->vthread_continuation();
1358   }
1359   return cont_entry != nullptr && is_cthread_with_mounted_vthread(jt);
1360 }
1361 
1362 // Check if VirtualThread or BoundVirtualThread is suspended.
1363 bool
1364 JvmtiEnvBase::is_vthread_suspended(oop vt_oop, JavaThread* jt) {
1365   bool suspended = false;
1366   if (java_lang_VirtualThread::is_instance(vt_oop)) {
1367     suspended = JvmtiVTSuspender::is_vthread_suspended(vt_oop);
1368   }
1369   if (vt_oop->is_a(vmClasses::BoundVirtualThread_klass())) {
1370     suspended = jt->is_suspended();
1371   }
1372   return suspended;
1373 }
1374 
1375 // If (thread == null) then return current thread object.
1376 // Otherwise return JNIHandles::resolve_external_guard(thread).
1377 oop
1378 JvmtiEnvBase::current_thread_obj_or_resolve_external_guard(jthread thread) {
1379   oop thread_obj = JNIHandles::resolve_external_guard(thread);
1380   if (thread == nullptr) {
1381     thread_obj = get_vthread_or_thread_oop(JavaThread::current());
1382   }
1383   return thread_obj;
1384 }
1385 
1386 jvmtiError
1387 JvmtiEnvBase::get_threadOop_and_JavaThread(ThreadsList* t_list, jthread thread, JavaThread* cur_thread,
1388                                            JavaThread** jt_pp, oop* thread_oop_p) {
1389   JavaThread* java_thread = nullptr;
1390   oop thread_oop = nullptr;
1391 
1392   if (thread == nullptr) {
1393     if (cur_thread == nullptr) { // cur_thread can be null when called from a VM_op
1394       return JVMTI_ERROR_INVALID_THREAD;
1395     }
1396     java_thread = cur_thread;
1397     thread_oop = get_vthread_or_thread_oop(java_thread);
1398     if (thread_oop == nullptr || !thread_oop->is_a(vmClasses::Thread_klass())) {
1399       return JVMTI_ERROR_INVALID_THREAD;
1400     }
1401   } else {
1402     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(t_list, thread, &java_thread, &thread_oop);
1403     if (err != JVMTI_ERROR_NONE) {
1404       // We got an error code so we don't have a JavaThread*, but only return
1405       // an error from here if we didn't get a valid thread_oop. In a vthread case
1406       // the cv_external_thread_to_JavaThread is expected to correctly set the
1407       // thread_oop and return JVMTI_ERROR_INVALID_THREAD which we ignore here.
1408       if (thread_oop == nullptr || err != JVMTI_ERROR_INVALID_THREAD) {
1409         *thread_oop_p = thread_oop;
1410         return err;
1411       }
1412     }
1413     if (java_thread == nullptr && java_lang_VirtualThread::is_instance(thread_oop)) {
1414       java_thread = get_JavaThread_or_null(thread_oop);
1415     }
1416   }
1417   *jt_pp = java_thread;
1418   *thread_oop_p = thread_oop;
1419   if (java_lang_VirtualThread::is_instance(thread_oop) &&
1420       !JvmtiEnvBase::is_vthread_alive(thread_oop)) {
1421     return JVMTI_ERROR_THREAD_NOT_ALIVE;
1422   }
1423   return JVMTI_ERROR_NONE;
1424 }
1425 
1426 // Check for JVMTI_ERROR_NOT_SUSPENDED and JVMTI_ERROR_OPAQUE_FRAME errors.
1427 // Used in PopFrame and ForceEarlyReturn implementations.
1428 jvmtiError
1429 JvmtiEnvBase::check_non_suspended_or_opaque_frame(JavaThread* jt, oop thr_obj, bool self) {
1430   bool is_virtual = thr_obj != nullptr && thr_obj->is_a(vmClasses::BaseVirtualThread_klass());
1431 
1432   if (is_virtual) {
1433     if (!is_JavaThread_current(jt, thr_obj)) {
1434       if (!is_vthread_suspended(thr_obj, jt)) {
1435         return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1436       }
1437       if (jt == nullptr) { // unmounted virtual thread
1438         return JVMTI_ERROR_OPAQUE_FRAME;
1439       }
1440     }
1441   } else { // platform thread
1442     if (!self && !jt->is_suspended() &&
1443         !jt->is_carrier_thread_suspended()) {
1444       return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1445     }
1446   }
1447   return JVMTI_ERROR_NONE;
1448 }
1449 
1450 jvmtiError
1451 JvmtiEnvBase::get_object_monitor_usage(JavaThread* calling_thread, jobject object, jvmtiMonitorUsage* info_ptr) {
1452   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1453   Thread* current_thread = VMThread::vm_thread();
1454   assert(current_thread == Thread::current(), "must be");
1455 
1456   HandleMark hm(current_thread);
1457   Handle hobj;
1458 
1459   // Check arguments
1460   {
1461     oop mirror = JNIHandles::resolve_external_guard(object);
1462     NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
1463     NULL_CHECK(info_ptr, JVMTI_ERROR_NULL_POINTER);
1464 
1465     hobj = Handle(current_thread, mirror);
1466   }
1467 
1468   ThreadsListHandle tlh(current_thread);
1469   JavaThread *owning_thread = nullptr;
1470   jvmtiMonitorUsage ret = {
1471       nullptr, 0, 0, nullptr, 0, nullptr
1472   };
1473 
1474   uint32_t debug_bits = 0;
1475   // first derive the object's owner and entry_count (if any)
1476   owning_thread = ObjectSynchronizer::get_lock_owner(tlh.list(), hobj);
1477   if (owning_thread != nullptr) {
1478     oop thread_oop = get_vthread_or_thread_oop(owning_thread);
1479     bool is_virtual = thread_oop->is_a(vmClasses::BaseVirtualThread_klass());
1480     if (is_virtual) {
1481       thread_oop = nullptr;
1482     }
1483     Handle th(current_thread, thread_oop);
1484     ret.owner = (jthread)jni_reference(calling_thread, th);
1485 
1486     // The recursions field of a monitor does not reflect recursions
1487     // as lightweight locks before inflating the monitor are not included.
1488     // We have to count the number of recursive monitor entries the hard way.
1489     // We pass a handle to survive any GCs along the way.
1490     ret.entry_count = is_virtual ? 0 : count_locked_objects(owning_thread, hobj);
1491   }
1492   // implied else: entry_count == 0
1493 
1494   jint nWant = 0, nWait = 0;
1495   markWord mark = hobj->mark();
1496   ResourceMark rm(current_thread);
1497   GrowableArray<JavaThread*>* wantList = nullptr;
1498 
1499   ObjectMonitor* mon = mark.has_monitor()
1500       ? ObjectSynchronizer::read_monitor(current_thread, hobj(), mark)
1501       : nullptr;
1502 
1503   if (mon != nullptr) {
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_keepalive(&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 }