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, refArrayHandle 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, refArrayHandle *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   refArrayOop groups = (refArrayOop)result.get_oop();
 887 
 888   *count_ptr = groups == nullptr ? 0 : groups->length();
 889   *group_objs_p = refArrayHandle(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 (fr.is_heap_frame()) {
1375       assert(state->is_virtual(), "invariant");
1376       fr.deoptimize(nullptr, jvf->stack_chunk());
1377     } else {
1378       Deoptimization::deoptimize(thread, fr);
1379     }
1380   }
1381   ets->set_frame_pop(frame_number);
1382   return JVMTI_ERROR_NONE;
1383 }
1384 
1385 jvmtiError
1386 JvmtiEnvBase::clear_all_frame_pops(JvmtiThreadState* state) {
1387   JvmtiEnvThreadState* ets = state->env_thread_state(this);
1388   ets->clear_all_frame_pops();
1389   return JVMTI_ERROR_NONE;
1390 }
1391 
1392 bool
1393 JvmtiEnvBase::is_cthread_with_mounted_vthread(JavaThread* jt) {
1394   oop thread_oop = jt->threadObj();
1395   assert(thread_oop != nullptr, "sanity check");
1396   oop mounted_vt = jt->jvmti_vthread();
1397 
1398   return mounted_vt != nullptr && mounted_vt != thread_oop;
1399 }
1400 
1401 bool
1402 JvmtiEnvBase::is_cthread_with_continuation(JavaThread* jt) {
1403   const ContinuationEntry* cont_entry = nullptr;
1404   if (jt->has_last_Java_frame()) {
1405     cont_entry = jt->vthread_continuation();
1406   }
1407   return cont_entry != nullptr && is_cthread_with_mounted_vthread(jt);
1408 }
1409 
1410 // Check if VirtualThread or BoundVirtualThread is suspended.
1411 bool
1412 JvmtiEnvBase::is_vthread_suspended(oop vt_oop, JavaThread* jt) {
1413   bool suspended = false;
1414   if (java_lang_VirtualThread::is_instance(vt_oop)) {
1415     suspended = JvmtiVTSuspender::is_vthread_suspended(vt_oop);
1416   } else if (vt_oop->is_a(vmClasses::BoundVirtualThread_klass())) {
1417     suspended = jt->is_suspended();
1418   }
1419   return suspended;
1420 }
1421 
1422 // If (thread == null) then return current thread object.
1423 // Otherwise return JNIHandles::resolve_external_guard(thread).
1424 oop
1425 JvmtiEnvBase::current_thread_obj_or_resolve_external_guard(jthread thread) {
1426   oop thread_obj = JNIHandles::resolve_external_guard(thread);
1427   if (thread == nullptr) {
1428     thread_obj = get_vthread_or_thread_oop(JavaThread::current());
1429   }
1430   return thread_obj;
1431 }
1432 
1433 jvmtiError
1434 JvmtiEnvBase::get_threadOop_and_JavaThread(ThreadsList* t_list, jthread thread, JavaThread* cur_thread,
1435                                            JavaThread** jt_pp, oop* thread_oop_p) {
1436   JavaThread* java_thread = nullptr;
1437   oop thread_oop = nullptr;
1438 
1439   if (thread == nullptr) {
1440     if (cur_thread == nullptr) { // cur_thread can be null when called from a VM_op
1441       return JVMTI_ERROR_INVALID_THREAD;
1442     }
1443     java_thread = cur_thread;
1444     thread_oop = get_vthread_or_thread_oop(java_thread);
1445     if (thread_oop == nullptr || !thread_oop->is_a(vmClasses::Thread_klass())) {
1446       return JVMTI_ERROR_INVALID_THREAD;
1447     }
1448   } else {
1449     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(t_list, thread, &java_thread, &thread_oop);
1450     if (err != JVMTI_ERROR_NONE) {
1451       // We got an error code so we don't have a JavaThread*, but only return
1452       // an error from here if we didn't get a valid thread_oop. In a vthread case
1453       // the cv_external_thread_to_JavaThread is expected to correctly set the
1454       // thread_oop and return JVMTI_ERROR_INVALID_THREAD which we ignore here.
1455       if (thread_oop == nullptr || err != JVMTI_ERROR_INVALID_THREAD) {
1456         *thread_oop_p = thread_oop;
1457         return err;
1458       }
1459     }
1460     if (java_thread == nullptr && java_lang_VirtualThread::is_instance(thread_oop)) {
1461       java_thread = get_JavaThread_or_null(thread_oop);
1462     }
1463   }
1464   *jt_pp = java_thread;
1465   *thread_oop_p = thread_oop;
1466   if (java_lang_VirtualThread::is_instance(thread_oop) &&
1467       !JvmtiEnvBase::is_vthread_alive(thread_oop)) {
1468     return JVMTI_ERROR_THREAD_NOT_ALIVE;
1469   }
1470   return JVMTI_ERROR_NONE;
1471 }
1472 
1473 // Check for JVMTI_ERROR_NOT_SUSPENDED and JVMTI_ERROR_OPAQUE_FRAME errors.
1474 // Used in PopFrame and ForceEarlyReturn implementations.
1475 jvmtiError
1476 JvmtiEnvBase::check_non_suspended_or_opaque_frame(JavaThread* jt, oop thr_obj, bool self) {
1477   bool is_virtual = thr_obj != nullptr && thr_obj->is_a(vmClasses::BaseVirtualThread_klass());
1478 
1479   if (is_virtual) {
1480     if (!is_JavaThread_current(jt, thr_obj)) {
1481       if (!is_vthread_suspended(thr_obj, jt)) {
1482         return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1483       }
1484       if (jt == nullptr) { // unmounted virtual thread
1485         return JVMTI_ERROR_OPAQUE_FRAME;
1486       }
1487     }
1488   } else { // platform thread
1489     if (!self && !jt->is_suspended() &&
1490         !jt->is_carrier_thread_suspended()) {
1491       return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1492     }
1493   }
1494   return JVMTI_ERROR_NONE;
1495 }
1496 
1497 jvmtiError
1498 JvmtiEnvBase::get_object_monitor_usage(JavaThread* calling_thread, jobject object, jvmtiMonitorUsage* info_ptr) {
1499   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1500   Thread* current_thread = VMThread::vm_thread();
1501   assert(current_thread == Thread::current(), "must be");
1502 
1503   HandleMark hm(current_thread);
1504   Handle hobj;
1505 
1506   // Check arguments
1507   {
1508     oop mirror = JNIHandles::resolve_external_guard(object);
1509     NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
1510     NULL_CHECK(info_ptr, JVMTI_ERROR_NULL_POINTER);
1511 
1512     hobj = Handle(current_thread, mirror);
1513   }
1514 
1515   ThreadsListHandle tlh(current_thread);
1516   JavaThread *owning_thread = nullptr;
1517   jvmtiMonitorUsage ret = {
1518       nullptr, 0, 0, nullptr, 0, nullptr
1519   };
1520 
1521   uint32_t debug_bits = 0;
1522   // first derive the object's owner and entry_count (if any)
1523   owning_thread = ObjectSynchronizer::get_lock_owner(tlh.list(), hobj);
1524   if (owning_thread != nullptr) {
1525     oop thread_oop = get_vthread_or_thread_oop(owning_thread);
1526     bool is_virtual = thread_oop->is_a(vmClasses::BaseVirtualThread_klass());
1527     if (is_virtual) {
1528       thread_oop = nullptr;
1529     }
1530     Handle th(current_thread, thread_oop);
1531     ret.owner = (jthread)jni_reference(calling_thread, th);
1532 
1533     // The recursions field of a monitor does not reflect recursions
1534     // as lightweight locks before inflating the monitor are not included.
1535     // We have to count the number of recursive monitor entries the hard way.
1536     // We pass a handle to survive any GCs along the way.
1537     ret.entry_count = is_virtual ? 0 : count_locked_objects(owning_thread, hobj);
1538   }
1539   // implied else: entry_count == 0
1540 
1541   jint nWant = 0, nWait = 0;
1542   markWord mark = hobj->mark();
1543   ResourceMark rm(current_thread);
1544   GrowableArray<JavaThread*>* wantList = nullptr;
1545 
1546   ObjectMonitor* mon = mark.has_monitor()
1547       ? ObjectSynchronizer::read_monitor(hobj(), mark)
1548       : nullptr;
1549 
1550   if (mon != nullptr) {
1551     // this object has a heavyweight monitor
1552     nWant = mon->contentions(); // # of threads contending for monitor entry, but not re-entry
1553     nWait = mon->waiters();     // # of threads waiting for notification,
1554                                 // or to re-enter monitor, in Object.wait()
1555 
1556     // Get the actual set of threads trying to enter, or re-enter, the monitor.
1557     wantList = Threads::get_pending_threads(tlh.list(), nWant + nWait, (address)mon);
1558     nWant = wantList->length();
1559   } else {
1560     // this object has a lightweight monitor
1561   }
1562 
1563   jint skipped = 0;
1564   if (mon != nullptr) {
1565     // Robustness: the actual waiting list can be smaller.
1566     // The nWait count we got from the mon->waiters() may include the re-entering
1567     // the monitor threads after being notified. Here we are correcting the actual
1568     // number of the waiting threads by excluding those re-entering the monitor.
1569     nWait = 0;
1570     for (ObjectWaiter* waiter = mon->first_waiter();
1571          waiter != nullptr && (nWait == 0 || waiter != mon->first_waiter());
1572          waiter = mon->next_waiter(waiter)) {
1573       JavaThread *w = mon->thread_of_waiter(waiter);
1574       if (w == nullptr) {
1575         skipped++;
1576       } else {
1577         oop thread_oop = get_vthread_or_thread_oop(w);
1578         if (thread_oop->is_a(vmClasses::BaseVirtualThread_klass())) {
1579           skipped++;
1580         }
1581       }
1582       nWait++;
1583     }
1584   }
1585   ret.waiter_count = nWant;
1586   ret.notify_waiter_count = nWait - skipped;
1587 
1588   // Allocate memory for heavyweight and lightweight monitor.
1589   jvmtiError err;
1590   err = allocate(ret.waiter_count * sizeof(jthread *), (unsigned char**)&ret.waiters);
1591   if (err != JVMTI_ERROR_NONE) {
1592     return err;
1593   }
1594   err = allocate(ret.notify_waiter_count * sizeof(jthread *),
1595                  (unsigned char**)&ret.notify_waiters);
1596   if (err != JVMTI_ERROR_NONE) {
1597     deallocate((unsigned char*)ret.waiters);
1598     return err;
1599   }
1600 
1601   // now derive the rest of the fields
1602   if (mon != nullptr) {
1603     // this object has a heavyweight monitor
1604 
1605     // null out memory for robustness
1606     if (ret.waiters != nullptr) {
1607       memset(ret.waiters, 0, ret.waiter_count * sizeof(jthread *));
1608     }
1609     if (ret.notify_waiters != nullptr) {
1610       memset(ret.notify_waiters, 0, ret.notify_waiter_count * sizeof(jthread *));
1611     }
1612 
1613     if (ret.waiter_count > 0) { // we have contending threads waiting to enter/re-enter the monitor
1614       // identify threads waiting to enter and re-enter the monitor
1615       // get_pending_threads returns only java thread so we do not need to
1616       // check for non java threads.
1617       for (int i = 0; i < nWant; i++) {
1618         JavaThread *pending_thread = wantList->at(i);
1619         Handle th(current_thread, get_vthread_or_thread_oop(pending_thread));
1620         ret.waiters[i] = (jthread)jni_reference(calling_thread, th);
1621       }
1622     }
1623     if (ret.notify_waiter_count > 0) { // we have threads waiting to be notified in Object.wait()
1624       ObjectWaiter *waiter = mon->first_waiter();
1625       jint skipped = 0;
1626       for (int i = 0; i < nWait; i++) {
1627         JavaThread *w = mon->thread_of_waiter(waiter);
1628         bool is_virtual;
1629         if (w == nullptr) {
1630           is_virtual = true;
1631         } else {
1632           oop thread_oop = get_vthread_or_thread_oop(w);
1633           is_virtual = thread_oop->is_a(vmClasses::BaseVirtualThread_klass());
1634         }
1635         if (is_virtual) {
1636           skipped++;
1637         } else {
1638           // If the thread was found on the ObjectWaiter list, then
1639           // it has not been notified.
1640           Handle th(current_thread, w->threadObj());
1641           ret.notify_waiters[i - skipped] = (jthread)jni_reference(calling_thread, th);
1642         }
1643         waiter = mon->next_waiter(waiter);
1644       }
1645     }
1646   } else {
1647     // this object has a lightweight monitor and we have nothing more
1648     // to do here because the defaults are just fine.
1649   }
1650 
1651   // we don't update return parameter unless everything worked
1652   *info_ptr = ret;
1653 
1654   return JVMTI_ERROR_NONE;
1655 }
1656 
1657 jvmtiError
1658 JvmtiEnvBase::check_thread_list(jint count, const jthread* list) {
1659   if (list == nullptr && count != 0) {
1660     return JVMTI_ERROR_NULL_POINTER;
1661   }
1662   for (int i = 0; i < count; i++) {
1663     jthread thread = list[i];
1664     oop thread_oop = JNIHandles::resolve_external_guard(thread);
1665     if (thread_oop == nullptr || !thread_oop->is_a(vmClasses::BaseVirtualThread_klass())) {
1666       return JVMTI_ERROR_INVALID_THREAD;
1667     }
1668   }
1669   return JVMTI_ERROR_NONE;
1670 }
1671 
1672 bool
1673 JvmtiEnvBase::is_in_thread_list(jint count, const jthread* list, oop jt_oop) {
1674   for (int idx = 0; idx < count; idx++) {
1675     jthread thread = list[idx];
1676     oop thread_oop = JNIHandles::resolve_external_guard(thread);
1677     if (thread_oop == jt_oop) {
1678       return true;
1679     }
1680   }
1681   return false;
1682 }
1683 
1684 class VM_SetNotifyJvmtiEventsMode : public VM_Operation {
1685 private:
1686   bool _enable;
1687 
1688   static void correct_jvmti_thread_state(JavaThread* jt) {
1689     oop  ct_oop = jt->threadObj();
1690     oop  vt_oop = jt->vthread();
1691     JvmtiThreadState* jt_state = jt->jvmti_thread_state();
1692     JvmtiThreadState* ct_state = java_lang_Thread::jvmti_thread_state(jt->threadObj());
1693     JvmtiThreadState* vt_state = vt_oop != nullptr ? java_lang_Thread::jvmti_thread_state(vt_oop) : nullptr;
1694     bool virt = vt_oop != nullptr && java_lang_VirtualThread::is_instance(vt_oop);
1695 
1696     // Correct jt->jvmti_thread_state() and jt->jvmti_vthread().
1697     // It was not maintained while notifyJvmti was disabled.
1698     if (virt) {
1699       jt->set_jvmti_thread_state(nullptr);  // reset jt->jvmti_thread_state()
1700       jt->set_jvmti_vthread(vt_oop);        // restore jt->jvmti_vthread()
1701     } else {
1702       jt->set_jvmti_thread_state(ct_state); // restore jt->jvmti_thread_state()
1703       jt->set_jvmti_vthread(ct_oop);        // restore jt->jvmti_vthread()
1704     }
1705   }
1706 
1707   // This function is called only if _enable == true.
1708   // Iterates over all JavaThread's, restores jt->jvmti_thread_state() and
1709   // jt->jvmti_vthread() for VTMS transition protocol.
1710   void correct_jvmti_thread_states() {
1711     for (JavaThread* jt : ThreadsListHandle()) {
1712       if (jt->is_in_vthread_transition()) {
1713         continue; // no need in JvmtiThreadState correction below if in transition
1714       }
1715       correct_jvmti_thread_state(jt);
1716     }
1717   }
1718 
1719 public:
1720   VMOp_Type type() const { return VMOp_SetNotifyJvmtiEventsMode; }
1721   bool allow_nested_vm_operations() const { return false; }
1722   VM_SetNotifyJvmtiEventsMode(bool enable) : _enable(enable) {
1723   }
1724 
1725   void doit() {
1726     if (_enable) {
1727       correct_jvmti_thread_states();
1728     }
1729     MountUnmountDisabler::set_notify_jvmti_events(_enable);
1730   }
1731 };
1732 
1733 // This function is to support agents loaded into running VM.
1734 // Must be called in thread-in-native mode.
1735 bool
1736 JvmtiEnvBase::enable_virtual_threads_notify_jvmti() {
1737   if (!Continuations::enabled()) {
1738     return false;
1739   }
1740   if (MountUnmountDisabler::notify_jvmti_events()) {
1741     return false; // already enabled
1742   }
1743   VM_SetNotifyJvmtiEventsMode op(true);
1744   VMThread::execute(&op);
1745   return true;
1746 }
1747 
1748 // This function is used in WhiteBox, only needed to test the function above.
1749 // It is unsafe to use this function when virtual threads are executed.
1750 // Must be called in thread-in-native mode.
1751 bool
1752 JvmtiEnvBase::disable_virtual_threads_notify_jvmti() {
1753   if (!Continuations::enabled()) {
1754     return false;
1755   }
1756   if (!MountUnmountDisabler::notify_jvmti_events()) {
1757     return false; // already disabled
1758   }
1759   MountUnmountDisabler disabler(true); // ensure there are no other disablers
1760   VM_SetNotifyJvmtiEventsMode op(false);
1761   VMThread::execute(&op);
1762   return true;
1763 }
1764 
1765 // java_thread - protected by ThreadsListHandle
1766 jvmtiError
1767 JvmtiEnvBase::suspend_thread(oop thread_oop, JavaThread* java_thread, bool single_suspend) {
1768   JavaThread* current = JavaThread::current();
1769   HandleMark hm(current);
1770   Handle thread_h(current, thread_oop);
1771   bool is_virtual = java_lang_VirtualThread::is_instance(thread_h());
1772 
1773   // Unmounted vthread case.
1774 
1775   if (is_virtual && java_thread == nullptr) {
1776     assert(single_suspend, "sanity check");
1777     if (JvmtiVTSuspender::is_vthread_suspended(thread_h())) {
1778       return JVMTI_ERROR_THREAD_SUSPENDED;
1779     }
1780     JvmtiVTSuspender::register_vthread_suspend(thread_h());
1781     return JVMTI_ERROR_NONE;
1782   }
1783 
1784   // Platform thread or mounted vthread cases.
1785 
1786   assert(java_thread != nullptr, "sanity check");
1787 
1788   // Don't allow hidden thread suspend request.
1789   if (java_thread->is_hidden_from_external_view()) {
1790     return JVMTI_ERROR_NONE;
1791   }
1792 
1793   // An attempt to handshake-suspend a thread carrying a virtual thread will result in
1794   // suspension of mounted virtual thread. So, we just mark it as suspended
1795   // and it will be actually suspended at virtual thread unmount transition.
1796   bool is_thread_carrying = is_thread_carrying_vthread(java_thread, thread_h());
1797   if (is_thread_carrying) {
1798     return java_thread->set_carrier_thread_suspended() ? JVMTI_ERROR_NONE : JVMTI_ERROR_THREAD_SUSPENDED;
1799   } else {
1800     // Platform thread (not carrying vthread) or mounted vthread cases.
1801     assert(thread_h() != nullptr, "sanity check");
1802     assert(single_suspend || thread_h()->is_a(vmClasses::BaseVirtualThread_klass()),
1803            "SuspendAllVirtualThreads should never suspend non-virtual threads");
1804 
1805     // Ideally we would just need to check java_thread->is_suspended(), but we have to
1806     // consider the case of trying to suspend a thread that was previously suspended while
1807     // carrying a vthread but has already unmounted it.
1808     if (java_thread->is_suspended() || (!is_virtual && java_thread->is_carrier_thread_suspended())) {
1809       return JVMTI_ERROR_THREAD_SUSPENDED;
1810     }
1811     if (!java_thread->java_suspend(is_virtual && single_suspend)) {
1812       // Thread is already suspended or in process of exiting.
1813       if (java_thread->is_exiting()) {
1814         // The thread was in the process of exiting.
1815         return JVMTI_ERROR_THREAD_NOT_ALIVE;
1816       }
1817       return JVMTI_ERROR_THREAD_SUSPENDED;
1818     }
1819     return JVMTI_ERROR_NONE;
1820   }
1821 }
1822 
1823 // java_thread - protected by ThreadsListHandle
1824 jvmtiError
1825 JvmtiEnvBase::resume_thread(oop thread_oop, JavaThread* java_thread, bool single_resume) {
1826   JavaThread* current = JavaThread::current();
1827   HandleMark hm(current);
1828   Handle thread_h(current, thread_oop);
1829   bool is_virtual = java_lang_VirtualThread::is_instance(thread_h());
1830 
1831   // Unmounted vthread case.
1832 
1833   if (is_virtual && java_thread == nullptr) {
1834     assert(single_resume, "sanity check");
1835     if (!JvmtiVTSuspender::is_vthread_suspended(thread_h())) {
1836       return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1837     }
1838     JvmtiVTSuspender::register_vthread_resume(thread_h());
1839     return JVMTI_ERROR_NONE;
1840   }
1841 
1842   // Platform thread or mounted vthread cases.
1843 
1844   assert(java_thread != nullptr, "sanity check");
1845 
1846   // Don't allow hidden thread resume request.
1847   if (java_thread->is_hidden_from_external_view()) {
1848     return JVMTI_ERROR_NONE;
1849   }
1850 
1851   bool is_thread_carrying = is_thread_carrying_vthread(java_thread, thread_h());
1852   if (is_thread_carrying) {
1853     return java_thread->clear_carrier_thread_suspended() ? JVMTI_ERROR_NONE : JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1854   } else {
1855     // Platform thread (not carrying vthread) or mounted vthread cases.
1856 
1857     assert(thread_h() != nullptr, "sanity check");
1858     assert(single_resume || thread_h()->is_a(vmClasses::BaseVirtualThread_klass()),
1859            "ResumeAllVirtualThreads should never resume non-virtual threads");
1860 
1861     // Ideally we would not have to check this but we have to consider the case
1862     // of trying to resume a thread that was previously suspended while carrying
1863     // a vthread but has already unmounted it.
1864     if (!is_virtual && java_thread->is_carrier_thread_suspended()) {
1865       bool res = java_thread->clear_carrier_thread_suspended();
1866       assert(res, "resume operations running concurrently?");
1867       return JVMTI_ERROR_NONE;
1868     }
1869 
1870     if (!java_thread->java_resume(is_virtual && single_resume)) {
1871       return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1872     }
1873     return JVMTI_ERROR_NONE;
1874   }
1875 }
1876 
1877 ResourceTracker::ResourceTracker(JvmtiEnv* env) {
1878   _env = env;
1879   _allocations = new (mtServiceability) GrowableArray<unsigned char*>(20, mtServiceability);
1880   _failed = false;
1881 }
1882 ResourceTracker::~ResourceTracker() {
1883   if (_failed) {
1884     for (int i=0; i<_allocations->length(); i++) {
1885       _env->deallocate(_allocations->at(i));
1886     }
1887   }
1888   delete _allocations;
1889 }
1890 
1891 jvmtiError ResourceTracker::allocate(jlong size, unsigned char** mem_ptr) {
1892   unsigned char *ptr;
1893   jvmtiError err = _env->allocate(size, &ptr);
1894   if (err == JVMTI_ERROR_NONE) {
1895     _allocations->append(ptr);
1896     *mem_ptr = ptr;
1897   } else {
1898     *mem_ptr = nullptr;
1899     _failed = true;
1900   }
1901   return err;
1902  }
1903 
1904 unsigned char* ResourceTracker::allocate(jlong size) {
1905   unsigned char* ptr;
1906   allocate(size, &ptr);
1907   return ptr;
1908 }
1909 
1910 char* ResourceTracker::strdup(const char* str) {
1911   char *dup_str = (char*)allocate(strlen(str)+1);
1912   if (dup_str != nullptr) {
1913     strcpy(dup_str, str);
1914   }
1915   return dup_str;
1916 }
1917 
1918 struct StackInfoNode {
1919   struct StackInfoNode *next;
1920   jvmtiStackInfo info;
1921 };
1922 
1923 // Create a jvmtiStackInfo inside a linked list node and create a
1924 // buffer for the frame information, both allocated as resource objects.
1925 // Fill in both the jvmtiStackInfo and the jvmtiFrameInfo.
1926 // Note that either or both of thr and thread_oop
1927 // may be null if the thread is new or has exited.
1928 void
1929 MultipleStackTracesCollector::fill_frames(jthread jt, JavaThread *thr, oop thread_oop) {
1930 #ifdef ASSERT
1931   Thread *current_thread = Thread::current();
1932   assert(SafepointSynchronize::is_at_safepoint() ||
1933          thr == nullptr ||
1934          thr->is_handshake_safe_for(current_thread),
1935          "unmounted virtual thread / call by myself / at safepoint / at handshake");
1936 #endif
1937 
1938   jint state = 0;
1939   struct StackInfoNode *node = NEW_RESOURCE_OBJ(struct StackInfoNode);
1940   jvmtiStackInfo *infop = &(node->info);
1941 
1942   node->next = head();
1943   set_head(node);
1944   infop->frame_count = 0;
1945   infop->frame_buffer = nullptr;
1946   infop->thread = jt;
1947 
1948   if (java_lang_VirtualThread::is_instance(thread_oop)) {
1949     state = JvmtiEnvBase::get_vthread_state(thread_oop, thr);
1950 
1951     if ((state & JVMTI_THREAD_STATE_ALIVE) != 0) {
1952       javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(thread_oop);
1953       infop->frame_buffer = NEW_RESOURCE_ARRAY(jvmtiFrameInfo, max_frame_count());
1954       _result = env()->get_stack_trace(jvf, 0, max_frame_count(),
1955                                        infop->frame_buffer, &(infop->frame_count));
1956     }
1957   } else {
1958     state = JvmtiEnvBase::get_thread_state(thread_oop, thr);
1959     if (thr != nullptr && (state & JVMTI_THREAD_STATE_ALIVE) != 0) {
1960       infop->frame_buffer = NEW_RESOURCE_ARRAY(jvmtiFrameInfo, max_frame_count());
1961       _result = env()->get_stack_trace(thr, 0, max_frame_count(),
1962                                        infop->frame_buffer, &(infop->frame_count));
1963     }
1964   }
1965   _frame_count_total += infop->frame_count;
1966   infop->state = state;
1967 }
1968 
1969 // Based on the stack information in the linked list, allocate memory
1970 // block to return and fill it from the info in the linked list.
1971 void
1972 MultipleStackTracesCollector::allocate_and_fill_stacks(jint thread_count) {
1973   // do I need to worry about alignment issues?
1974   jlong alloc_size =  thread_count       * sizeof(jvmtiStackInfo)
1975                     + _frame_count_total * sizeof(jvmtiFrameInfo);
1976   env()->allocate(alloc_size, (unsigned char **)&_stack_info);
1977 
1978   // pointers to move through the newly allocated space as it is filled in
1979   jvmtiStackInfo *si = _stack_info + thread_count;      // bottom of stack info
1980   jvmtiFrameInfo *fi = (jvmtiFrameInfo *)si;            // is the top of frame info
1981 
1982   // copy information in resource area into allocated buffer
1983   // insert stack info backwards since linked list is backwards
1984   // insert frame info forwards
1985   // walk the StackInfoNodes
1986   for (struct StackInfoNode *sin = head(); sin != nullptr; sin = sin->next) {
1987     jint frame_count = sin->info.frame_count;
1988     size_t frames_size = frame_count * sizeof(jvmtiFrameInfo);
1989     --si;
1990     memcpy(si, &(sin->info), sizeof(jvmtiStackInfo));
1991     if (frames_size == 0) {
1992       si->frame_buffer = nullptr;
1993     } else {
1994       memcpy(fi, sin->info.frame_buffer, frames_size);
1995       si->frame_buffer = fi;  // point to the new allocated copy of the frames
1996       fi += frame_count;
1997     }
1998   }
1999   assert(si == _stack_info, "the last copied stack info must be the first record");
2000   assert((unsigned char *)fi == ((unsigned char *)_stack_info) + alloc_size,
2001          "the last copied frame info must be the last record");
2002 }
2003 
2004 // AdapterClosure is to make use of JvmtiUnitedHandshakeClosure objects from
2005 // Handshake::execute() which is unaware of the do_vthread() member functions.
2006 class AdapterClosure : public HandshakeClosure {
2007   JvmtiUnitedHandshakeClosure* _hs_cl;
2008   Handle _target_h;
2009 
2010  public:
2011   AdapterClosure(JvmtiUnitedHandshakeClosure* hs_cl, Handle target_h)
2012       : HandshakeClosure(hs_cl->name()), _hs_cl(hs_cl), _target_h(target_h) {}
2013 
2014   virtual void do_thread(Thread* target) {
2015     if (java_lang_VirtualThread::is_instance(_target_h())) {
2016       _hs_cl->do_vthread(_target_h); // virtual thread
2017     } else {
2018       _hs_cl->do_thread(target);     // platform thread
2019     }
2020   }
2021 };
2022 
2023 // Supports platform and virtual threads.
2024 // MountUnmountDisabler is always set by this function.
2025 void
2026 JvmtiHandshake::execute(JvmtiUnitedHandshakeClosure* hs_cl, jthread target) {
2027   JavaThread* current = JavaThread::current();
2028   HandleMark hm(current);
2029   MountUnmountDisabler disabler(target);
2030   ThreadsListHandle tlh(current);
2031   JavaThread* java_thread = nullptr;
2032   oop thread_obj = nullptr;
2033 
2034   jvmtiError err = JvmtiEnvBase::get_threadOop_and_JavaThread(tlh.list(), target, current, &java_thread, &thread_obj);
2035   if (err != JVMTI_ERROR_NONE) {
2036     hs_cl->set_result(err);
2037     return;
2038   }
2039   Handle target_h(current, thread_obj);
2040   execute(hs_cl, &tlh, java_thread, target_h);
2041 }
2042 
2043 // Supports platform and virtual threads.
2044 // A virtual thread is always identified by the target_h oop handle.
2045 // The target_jt is always nullptr for an unmounted virtual thread.
2046 // MountUnmountDisabler has to be set before call to this function.
2047 void
2048 JvmtiHandshake::execute(JvmtiUnitedHandshakeClosure* hs_cl, ThreadsListHandle* tlh,
2049                         JavaThread* target_jt, Handle target_h) {
2050   JavaThread* current = JavaThread::current();
2051   bool is_virtual = java_lang_VirtualThread::is_instance(target_h());
2052   bool self = target_jt == current;
2053 
2054   assert(!Continuations::enabled() || self || !is_virtual || current->is_vthread_transition_disabler(), "sanity check");
2055 
2056   hs_cl->set_target_jt(target_jt);   // can be needed in the virtual thread case
2057   hs_cl->set_is_virtual(is_virtual); // can be needed in the virtual thread case
2058   hs_cl->set_self(self);             // needed when suspend is required for non-current target thread
2059 
2060   if (is_virtual) {                // virtual thread
2061     if (!JvmtiEnvBase::is_vthread_alive(target_h())) {
2062       return;
2063     }
2064     if (target_jt == nullptr) {    // unmounted virtual thread
2065       hs_cl->do_vthread(target_h); // execute handshake closure callback on current thread directly
2066     }
2067   }
2068   if (target_jt != nullptr) {      // mounted virtual or platform thread
2069     AdapterClosure acl(hs_cl, target_h);
2070     if (self) {                    // target platform thread is current
2071       acl.do_thread(target_jt);    // execute handshake closure callback on current thread directly
2072     } else {
2073       Handshake::execute(&acl, tlh, target_jt); // delegate to Handshake implementation
2074     }
2075   }
2076 }
2077 
2078 void
2079 VM_GetThreadListStackTraces::doit() {
2080   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
2081 
2082   ResourceMark rm;
2083   ThreadsListHandle tlh;
2084   for (int i = 0; i < _thread_count; ++i) {
2085     jthread jt = _thread_list[i];
2086     JavaThread* java_thread = nullptr;
2087     oop thread_oop = nullptr;
2088     jvmtiError err = JvmtiEnvBase::get_threadOop_and_JavaThread(tlh.list(), jt, nullptr, &java_thread, &thread_oop);
2089 
2090     if (err != JVMTI_ERROR_NONE) {
2091       // We got an error code so we don't have a JavaThread *, but
2092       // only return an error from here if we didn't get a valid
2093       // thread_oop.
2094       // In the virtual thread case the get_threadOop_and_JavaThread is expected to correctly set
2095       // the thread_oop and return JVMTI_ERROR_THREAD_NOT_ALIVE which we ignore here.
2096       // The corresponding thread state will be recorded in the jvmtiStackInfo.state.
2097       if (thread_oop == nullptr) {
2098         _collector.set_result(err);
2099         return;
2100       }
2101       // We have a valid thread_oop.
2102     }
2103     _collector.fill_frames(jt, java_thread, thread_oop);
2104   }
2105   _collector.allocate_and_fill_stacks(_thread_count);
2106 }
2107 
2108 void
2109 GetSingleStackTraceClosure::doit() {
2110   JavaThread *jt = _target_jt;
2111   oop thread_oop = JNIHandles::resolve_external_guard(_jthread);
2112 
2113   if ((jt == nullptr || !jt->is_exiting()) && thread_oop != nullptr) {
2114     ResourceMark rm;
2115     _collector.fill_frames(_jthread, jt, thread_oop);
2116     _collector.allocate_and_fill_stacks(1);
2117     set_result(_collector.result());
2118   }
2119 }
2120 
2121 void
2122 GetSingleStackTraceClosure::do_thread(Thread *target) {
2123   assert(_target_jt == JavaThread::cast(target), "sanity check");
2124   doit();
2125 }
2126 
2127 void
2128 GetSingleStackTraceClosure::do_vthread(Handle target_h) {
2129   // Use jvmti_vthread() instead of vthread() as target could have temporarily changed
2130   // identity to carrier thread (see VirtualThread.switchToCarrierThread).
2131   assert(_target_jt == nullptr || _target_jt->jvmti_vthread() == target_h(), "sanity check");
2132   doit();
2133 }
2134 
2135 void
2136 VM_GetAllStackTraces::doit() {
2137   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
2138 
2139   ResourceMark rm;
2140   _final_thread_count = 0;
2141   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
2142     oop thread_oop = jt->threadObj();
2143     if (thread_oop != nullptr &&
2144         !jt->is_exiting() &&
2145         java_lang_Thread::is_alive(thread_oop) &&
2146         !jt->is_hidden_from_external_view() &&
2147         !thread_oop->is_a(vmClasses::BoundVirtualThread_klass())) {
2148       ++_final_thread_count;
2149       // Handle block of the calling thread is used to create local refs.
2150       _collector.fill_frames((jthread)JNIHandles::make_local(_calling_thread, thread_oop),
2151                              jt, thread_oop);
2152     }
2153   }
2154   _collector.allocate_and_fill_stacks(_final_thread_count);
2155 }
2156 
2157 // Verifies that the top frame is a java frame in an expected state.
2158 // Deoptimizes frame if needed.
2159 // Checks that the frame method signature matches the return type (tos).
2160 // HandleMark must be defined in the caller only.
2161 // It is to keep a ret_ob_h handle alive after return to the caller.
2162 jvmtiError
2163 JvmtiEnvBase::check_top_frame(Thread* current_thread, JavaThread* java_thread,
2164                               jvalue value, TosState tos, Handle* ret_ob_h) {
2165   ResourceMark rm(current_thread);
2166 
2167   javaVFrame* jvf = jvf_for_thread_and_depth(java_thread, 0);
2168   NULL_CHECK(jvf, JVMTI_ERROR_NO_MORE_FRAMES);
2169 
2170   if (jvf->method()->is_native()) {
2171     return JVMTI_ERROR_OPAQUE_FRAME;
2172   }
2173 
2174   // Prevent ForceEarlyReturnVoid from returning early from value class constructor as
2175   // the instance fields are strictly-initialized fields.
2176   if ((tos == vtos) && jvf->method()->is_object_constructor() && jvf->method()->method_holder()->is_inline_klass()) {
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   _state->invalidate_cur_stack_depth();
2322 }
2323 
2324 void
2325 JvmtiMonitorClosure::do_monitor(ObjectMonitor* mon) {
2326   if ( _error != JVMTI_ERROR_NONE) {
2327     // Error occurred in previous iteration so no need to add
2328     // to the list.
2329     return;
2330   }
2331   // Filter out on stack monitors collected during stack walk.
2332   oop obj = mon->object();
2333 
2334   if (obj == nullptr) {
2335     // This can happen if JNI code drops all references to the
2336     // owning object.
2337     return;
2338   }
2339 
2340   bool found = false;
2341   for (int j = 0; j < _owned_monitors_list->length(); j++) {
2342     jobject jobj = ((jvmtiMonitorStackDepthInfo*)_owned_monitors_list->at(j))->monitor;
2343     oop check = JNIHandles::resolve(jobj);
2344     if (check == obj) {
2345       // On stack monitor already collected during the stack walk.
2346       found = true;
2347       break;
2348     }
2349   }
2350   if (found == false) {
2351     // This is off stack monitor (e.g. acquired via jni MonitorEnter).
2352     jvmtiError err;
2353     jvmtiMonitorStackDepthInfo *jmsdi;
2354     err = _env->allocate(sizeof(jvmtiMonitorStackDepthInfo), (unsigned char **)&jmsdi);
2355     if (err != JVMTI_ERROR_NONE) {
2356       _error = err;
2357       return;
2358     }
2359     Handle hobj(Thread::current(), obj);
2360     jmsdi->monitor = _env->jni_reference(_calling_thread, hobj);
2361     // stack depth is unknown for this monitor.
2362     jmsdi->stack_depth = -1;
2363     _owned_monitors_list->append(jmsdi);
2364   }
2365 }
2366 
2367 GrowableArray<OopHandle>* JvmtiModuleClosure::_tbl = nullptr;
2368 
2369 void JvmtiModuleClosure::do_module(ModuleEntry* entry) {
2370   assert_locked_or_safepoint(Module_lock);
2371   OopHandle module = entry->module_handle();
2372   guarantee(module.resolve() != nullptr, "module object is null");
2373   _tbl->push(module);
2374 }
2375 
2376 jvmtiError
2377 JvmtiModuleClosure::get_all_modules(JvmtiEnv* env, jint* module_count_ptr, jobject** modules_ptr) {
2378   ResourceMark rm;
2379   MutexLocker mcld(ClassLoaderDataGraph_lock);
2380   MutexLocker ml(Module_lock);
2381 
2382   _tbl = new GrowableArray<OopHandle>(77);
2383   if (_tbl == nullptr) {
2384     return JVMTI_ERROR_OUT_OF_MEMORY;
2385   }
2386 
2387   // Iterate over all the modules loaded to the system.
2388   ClassLoaderDataGraph::modules_do_keepalive(&do_module);
2389 
2390   jint len = _tbl->length();
2391   guarantee(len > 0, "at least one module must be present");
2392 
2393   jobject* array = (jobject*)env->jvmtiMalloc((jlong)(len * sizeof(jobject)));
2394   if (array == nullptr) {
2395     return JVMTI_ERROR_OUT_OF_MEMORY;
2396   }
2397   for (jint idx = 0; idx < len; idx++) {
2398     array[idx] = JNIHandles::make_local(_tbl->at(idx).resolve());
2399   }
2400   _tbl = nullptr;
2401   *modules_ptr = array;
2402   *module_count_ptr = len;
2403   return JVMTI_ERROR_NONE;
2404 }
2405 
2406 void
2407 UpdateForPopTopFrameClosure::doit(Thread *target) {
2408   Thread* current_thread  = Thread::current();
2409   HandleMark hm(current_thread);
2410   JavaThread* java_thread = JavaThread::cast(target);
2411 
2412   if (java_thread->is_exiting()) {
2413     return; /* JVMTI_ERROR_THREAD_NOT_ALIVE (default) */
2414   }
2415   assert(java_thread == _state->get_thread(), "Must be");
2416 
2417   // Check to see if a PopFrame was already in progress
2418   if (java_thread->popframe_condition() != JavaThread::popframe_inactive) {
2419     // Probably possible for JVMTI clients to trigger this, but the
2420     // JPDA backend shouldn't allow this to happen
2421     _result = JVMTI_ERROR_INTERNAL;
2422     return;
2423   }
2424 
2425   // Was workaround bug
2426   //    4812902: popFrame hangs if the method is waiting at a synchronize
2427   // Catch this condition and return an error to avoid hanging.
2428   // Now JVMTI spec allows an implementation to bail out with an opaque frame error.
2429   OSThread* osThread = java_thread->osthread();
2430   if (osThread->get_state() == MONITOR_WAIT) {
2431     _result = JVMTI_ERROR_OPAQUE_FRAME;
2432     return;
2433   }
2434 
2435   ResourceMark rm(current_thread);
2436   // Check if there is more than one Java frame in this thread, that the top two frames
2437   // are Java (not native) frames, and that there is no intervening VM frame
2438   int frame_count = 0;
2439   bool is_interpreted[2];
2440   intptr_t *frame_sp[2];
2441   // The 2-nd arg of constructor is needed to stop iterating at java entry frame.
2442   for (vframeStream vfs(java_thread, true, false /* process_frames */); !vfs.at_end(); vfs.next()) {
2443     methodHandle mh(current_thread, vfs.method());
2444     if (mh->is_native()) {
2445       _result = JVMTI_ERROR_OPAQUE_FRAME;
2446       return;
2447     }
2448     is_interpreted[frame_count] = vfs.is_interpreted_frame();
2449     frame_sp[frame_count] = vfs.frame_id();
2450     if (++frame_count > 1) break;
2451   }
2452   if (frame_count < 2)  {
2453     // We haven't found two adjacent non-native Java frames on the top.
2454     // There can be two situations here:
2455     //  1. There are no more java frames
2456     //  2. Two top java frames are separated by non-java native frames
2457     if (JvmtiEnvBase::jvf_for_thread_and_depth(java_thread, 1) == nullptr) {
2458       _result = JVMTI_ERROR_NO_MORE_FRAMES;
2459       return;
2460     } else {
2461       // Intervening non-java native or VM frames separate java frames.
2462       // Current implementation does not support this. See bug #5031735.
2463       // In theory it is possible to pop frames in such cases.
2464       _result = JVMTI_ERROR_OPAQUE_FRAME;
2465       return;
2466     }
2467   }
2468 
2469   // If any of the top 2 frames is a compiled one, need to deoptimize it
2470   for (int i = 0; i < 2; i++) {
2471     if (!is_interpreted[i]) {
2472       Deoptimization::deoptimize_frame(java_thread, frame_sp[i]);
2473     }
2474   }
2475 
2476   // Update the thread state to reflect that the top frame is popped
2477   // so that cur_stack_depth is maintained properly and all frameIDs
2478   // are invalidated.
2479   // The current frame will be popped later when the suspended thread
2480   // is resumed and right before returning from VM to Java.
2481   // (see call_VM_base() in assembler_<cpu>.cpp).
2482 
2483   // It's fine to update the thread state here because no JVMTI events
2484   // shall be posted for this PopFrame.
2485 
2486   _state->update_for_pop_top_frame();
2487   java_thread->set_popframe_condition(JavaThread::popframe_pending_bit);
2488   // Set pending step flag for this popframe and it is cleared when next
2489   // step event is posted.
2490   _state->set_pending_step_for_popframe();
2491   _result = JVMTI_ERROR_NONE;
2492 }
2493 
2494 void
2495 SetOrClearFramePopClosure::do_thread(Thread *target) {
2496   Thread* current = Thread::current();
2497   ResourceMark rm(current); // vframes are resource allocated
2498   JavaThread* java_thread = JavaThread::cast(target);
2499 
2500   if (java_thread->is_exiting()) {
2501     return; // JVMTI_ERROR_THREAD_NOT_ALIVE (default)
2502   }
2503 
2504   if (!_self && !java_thread->is_suspended()) {
2505     _result = JVMTI_ERROR_THREAD_NOT_SUSPENDED;
2506     return;
2507   }
2508   if (!_set) { // ClearAllFramePops
2509     _result = _env->clear_all_frame_pops(_state);
2510     return;
2511   }
2512   if (!java_thread->has_last_Java_frame()) {
2513     _result = JVMTI_ERROR_NO_MORE_FRAMES;
2514     return;
2515   }
2516   assert(_state->get_thread() == java_thread, "Must be");
2517 
2518   RegisterMap reg_map(java_thread,
2519                       RegisterMap::UpdateMap::include,
2520                       RegisterMap::ProcessFrames::skip,
2521                       RegisterMap::WalkContinuation::include);
2522   javaVFrame* jvf = JvmtiEnvBase::get_cthread_last_java_vframe(java_thread, &reg_map);
2523   _result = _env->set_frame_pop(_state, jvf, _depth);
2524 }
2525 
2526 void
2527 SetOrClearFramePopClosure::do_vthread(Handle target_h) {
2528   Thread* current = Thread::current();
2529   ResourceMark rm(current); // vframes are resource allocated
2530 
2531   if (!_self && !JvmtiVTSuspender::is_vthread_suspended(target_h())) {
2532     _result = JVMTI_ERROR_THREAD_NOT_SUSPENDED;
2533     return;
2534   }
2535   if (!_set) { // ClearAllFramePops
2536     _result = _env->clear_all_frame_pops(_state);
2537     return;
2538   }
2539   assert(_state->get_thread() == _target_jt, "sanity check");
2540 
2541   javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(target_h());
2542   _result = _env->set_frame_pop(_state, jvf, _depth);
2543 }
2544 
2545 void
2546 GetOwnedMonitorInfoClosure::do_thread(Thread *target) {
2547   JavaThread *jt = JavaThread::cast(target);
2548   if (!jt->is_exiting() && (jt->threadObj() != nullptr)) {
2549     _result = ((JvmtiEnvBase *)_env)->get_owned_monitors(_calling_thread,
2550                                                          jt,
2551                                                          _owned_monitors_list);
2552   }
2553 }
2554 
2555 void
2556 GetOwnedMonitorInfoClosure::do_vthread(Handle target_h) {
2557   Thread* current = Thread::current();
2558   ResourceMark rm(current); // vframes are resource allocated
2559   HandleMark hm(current);
2560 
2561   javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(target_h());
2562 
2563   if (_target_jt == nullptr || (!_target_jt->is_exiting() && _target_jt->threadObj() != nullptr)) {
2564     _result = ((JvmtiEnvBase *)_env)->get_owned_monitors(_calling_thread,
2565                                                          _target_jt,
2566                                                          jvf,
2567                                                          _owned_monitors_list,
2568                                                          target_h());
2569   }
2570 }
2571 
2572 void
2573 GetCurrentContendedMonitorClosure::do_thread(Thread *target) {
2574   JavaThread *jt = JavaThread::cast(target);
2575   if (!jt->is_exiting() && (jt->threadObj() != nullptr)) {
2576     _result = ((JvmtiEnvBase *)_env)->get_current_contended_monitor(_calling_thread,
2577                                                                     jt,
2578                                                                     _owned_monitor_ptr,
2579                                                                     _is_virtual);
2580   }
2581 }
2582 
2583 void
2584 GetCurrentContendedMonitorClosure::do_vthread(Handle target_h) {
2585   if (_target_jt == nullptr) {
2586     ObjectMonitor *mon = java_lang_VirtualThread::current_pending_monitor(target_h());
2587     if (mon != nullptr) {
2588       *_owned_monitor_ptr = JNIHandles::make_local(_calling_thread, mon->object());
2589     }
2590     _result = JVMTI_ERROR_NONE; // target virtual thread is unmounted
2591     return;
2592   }
2593   // mounted virtual thread case
2594   do_thread(_target_jt);
2595 }
2596 
2597 void
2598 GetStackTraceClosure::do_thread(Thread *target) {
2599   Thread* current = Thread::current();
2600   ResourceMark rm(current);
2601 
2602   JavaThread *jt = JavaThread::cast(target);
2603   if (!jt->is_exiting() && jt->threadObj() != nullptr) {
2604     _result = ((JvmtiEnvBase *)_env)->get_stack_trace(jt,
2605                                                       _start_depth, _max_count,
2606                                                       _frame_buffer, _count_ptr);
2607   }
2608 }
2609 
2610 void
2611 GetStackTraceClosure::do_vthread(Handle target_h) {
2612   Thread* current = Thread::current();
2613   ResourceMark rm(current);
2614 
2615   javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(target_h());
2616   _result = ((JvmtiEnvBase *)_env)->get_stack_trace(jvf,
2617                                                     _start_depth, _max_count,
2618                                                     _frame_buffer, _count_ptr);
2619 }
2620 
2621 #ifdef ASSERT
2622 void
2623 PrintStackTraceClosure::do_thread_impl(Thread *target) {
2624   JavaThread *java_thread = JavaThread::cast(target);
2625   Thread *current_thread = Thread::current();
2626 
2627   ResourceMark rm (current_thread);
2628   const char* tname = JvmtiTrace::safe_get_thread_name(java_thread);
2629   oop t_oop = java_thread->jvmti_vthread();
2630   t_oop = t_oop == nullptr ? java_thread->threadObj() : t_oop;
2631   bool is_vt_suspended = java_lang_VirtualThread::is_instance(t_oop) && JvmtiVTSuspender::is_vthread_suspended(t_oop);
2632 
2633   log_error(jvmti)("%s(%s) exiting: %d is_susp: %d is_thread_susp: %d is_vthread_susp: %d "
2634                    "is_VTMS_transition_disabler: %d, is_in_VTMS_transition = %d\n",
2635                    tname, java_thread->name(), java_thread->is_exiting(),
2636                    java_thread->is_suspended(), java_thread->is_carrier_thread_suspended(), is_vt_suspended,
2637                    java_thread->is_vthread_transition_disabler(), java_thread->is_in_vthread_transition());
2638 
2639   if (java_thread->has_last_Java_frame()) {
2640     RegisterMap reg_map(java_thread,
2641                         RegisterMap::UpdateMap::include,
2642                         RegisterMap::ProcessFrames::include,
2643                         RegisterMap::WalkContinuation::skip);
2644     ResourceMark rm(current_thread);
2645     HandleMark hm(current_thread);
2646     javaVFrame *jvf = java_thread->last_java_vframe(&reg_map);
2647     while (jvf != nullptr) {
2648       log_error(jvmti)("  %s:%d",
2649                        jvf->method()->external_name(),
2650                        jvf->method()->line_number_from_bci(jvf->bci()));
2651       jvf = jvf->java_sender();
2652     }
2653   }
2654   log_error(jvmti)("\n");
2655 }
2656 
2657 void
2658 PrintStackTraceClosure::do_thread(Thread *target) {
2659   JavaThread *java_thread = JavaThread::cast(target);
2660   Thread *current_thread = Thread::current();
2661 
2662   assert(SafepointSynchronize::is_at_safepoint() ||
2663          java_thread->is_handshake_safe_for(current_thread),
2664          "call by myself / at safepoint / at handshake");
2665 
2666   PrintStackTraceClosure::do_thread_impl(target);
2667 }
2668 #endif
2669 
2670 void
2671 GetFrameCountClosure::do_thread(Thread *target) {
2672   JavaThread* jt = JavaThread::cast(target);
2673   assert(target == jt, "just checking");
2674 
2675   if (!jt->is_exiting() && jt->threadObj() != nullptr) {
2676     _result = ((JvmtiEnvBase*)_env)->get_frame_count(jt, _count_ptr);
2677   }
2678 }
2679 
2680 void
2681 GetFrameCountClosure::do_vthread(Handle target_h) {
2682   _result = ((JvmtiEnvBase*)_env)->get_frame_count(target_h(), _count_ptr);
2683 }
2684 
2685 void
2686 GetFrameLocationClosure::do_thread(Thread *target) {
2687   JavaThread *jt = JavaThread::cast(target);
2688   assert(target == jt, "just checking");
2689 
2690   if (!jt->is_exiting() && jt->threadObj() != nullptr) {
2691     _result = ((JvmtiEnvBase*)_env)->get_frame_location(jt, _depth,
2692                                                         _method_ptr, _location_ptr);
2693   }
2694 }
2695 
2696 void
2697 GetFrameLocationClosure::do_vthread(Handle target_h) {
2698   _result = ((JvmtiEnvBase*)_env)->get_frame_location(target_h(), _depth,
2699                                                       _method_ptr, _location_ptr);
2700 }