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