1 /*
   2  * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/classLoaderExt.hpp"
  27 #include "classfile/javaClasses.inline.hpp"
  28 #include "classfile/stringTable.hpp"
  29 #include "classfile/modules.hpp"
  30 #include "classfile/systemDictionary.hpp"
  31 #include "classfile/vmClasses.hpp"
  32 #include "classfile/vmSymbols.hpp"
  33 #include "gc/shared/collectedHeap.hpp"
  34 #include "interpreter/bytecodeStream.hpp"
  35 #include "interpreter/interpreter.hpp"
  36 #include "jfr/jfrEvents.hpp"
  37 #include "jvmtifiles/jvmtiEnv.hpp"
  38 #include "logging/log.hpp"
  39 #include "logging/logConfiguration.hpp"
  40 #include "memory/allocation.hpp"
  41 #include "memory/resourceArea.hpp"
  42 #include "memory/universe.hpp"
  43 #include "oops/instanceKlass.hpp"
  44 #include "oops/klass.inline.hpp"
  45 #include "oops/objArrayOop.inline.hpp"
  46 #include "oops/oop.inline.hpp"
  47 #include "prims/jniCheck.hpp"
  48 #include "prims/jvm_misc.hpp"
  49 #include "prims/jvmtiAgentThread.hpp"
  50 #include "prims/jvmtiClassFileReconstituter.hpp"
  51 #include "prims/jvmtiCodeBlobEvents.hpp"
  52 #include "prims/jvmtiExtensions.hpp"
  53 #include "prims/jvmtiGetLoadedClasses.hpp"
  54 #include "prims/jvmtiImpl.hpp"
  55 #include "prims/jvmtiManageCapabilities.hpp"
  56 #include "prims/jvmtiRawMonitor.hpp"
  57 #include "prims/jvmtiRedefineClasses.hpp"
  58 #include "prims/jvmtiTagMap.hpp"
  59 #include "prims/jvmtiThreadState.inline.hpp"
  60 #include "prims/jvmtiUtil.hpp"
  61 #include "runtime/arguments.hpp"
  62 #include "runtime/deoptimization.hpp"
  63 #include "runtime/fieldDescriptor.inline.hpp"
  64 #include "runtime/handles.inline.hpp"
  65 #include "runtime/interfaceSupport.inline.hpp"
  66 #include "runtime/javaCalls.hpp"
  67 #include "runtime/javaThread.inline.hpp"
  68 #include "runtime/jfieldIDWorkaround.hpp"
  69 #include "runtime/jniHandles.inline.hpp"
  70 #include "runtime/objectMonitor.inline.hpp"
  71 #include "runtime/os.hpp"
  72 #include "runtime/osThread.hpp"
  73 #include "runtime/reflectionUtils.hpp"
  74 #include "runtime/signature.hpp"
  75 #include "runtime/threadHeapSampler.hpp"
  76 #include "runtime/threads.hpp"
  77 #include "runtime/threadSMR.hpp"
  78 #include "runtime/timerTrace.hpp"
  79 #include "runtime/vframe.inline.hpp"
  80 #include "runtime/vmThread.hpp"
  81 #include "services/threadService.hpp"
  82 #include "utilities/exceptions.hpp"
  83 #include "utilities/preserveException.hpp"
  84 #include "utilities/utf8.hpp"
  85 
  86 
  87 #define FIXLATER 0 // REMOVE this when completed.
  88 
  89  // FIXLATER: hook into JvmtiTrace
  90 #define TraceJVMTICalls false
  91 
  92 JvmtiEnv::JvmtiEnv(jint version) : JvmtiEnvBase(version) {
  93 }
  94 
  95 JvmtiEnv::~JvmtiEnv() {
  96 }
  97 
  98 JvmtiEnv*
  99 JvmtiEnv::create_a_jvmti(jint version) {
 100   return new JvmtiEnv(version);
 101 }
 102 
 103 // VM operation class to copy jni function table at safepoint.
 104 // More than one java threads or jvmti agents may be reading/
 105 // modifying jni function tables. To reduce the risk of bad
 106 // interaction b/w these threads it is copied at safepoint.
 107 class VM_JNIFunctionTableCopier : public VM_Operation {
 108  private:
 109   const struct JNINativeInterface_ *_function_table;
 110  public:
 111   VM_JNIFunctionTableCopier(const struct JNINativeInterface_ *func_tbl) {
 112     _function_table = func_tbl;
 113   };
 114 
 115   VMOp_Type type() const { return VMOp_JNIFunctionTableCopier; }
 116   void doit() {
 117     copy_jni_function_table(_function_table);
 118   };
 119 };
 120 
 121 //
 122 // Do not change the "prefix" marker below, everything above it is copied
 123 // unchanged into the filled stub, everything below is controlled by the
 124 // stub filler (only method bodies are carried forward, and then only for
 125 // functionality still in the spec).
 126 //
 127 // end file prefix
 128 
 129   //
 130   // Memory Management functions
 131   //
 132 
 133 // mem_ptr - pre-checked for null
 134 jvmtiError
 135 JvmtiEnv::Allocate(jlong size, unsigned char** mem_ptr) {
 136   return allocate(size, mem_ptr);
 137 } /* end Allocate */
 138 
 139 
 140 // mem - null is a valid value, must be checked
 141 jvmtiError
 142 JvmtiEnv::Deallocate(unsigned char* mem) {
 143   return deallocate(mem);
 144 } /* end Deallocate */
 145 
 146 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
 147 // data - null is a valid value, must be checked
 148 jvmtiError
 149 JvmtiEnv::SetThreadLocalStorage(jthread thread, const void* data) {
 150   JavaThread* current = JavaThread::current();
 151   JvmtiThreadState* state = nullptr;
 152   JvmtiVTMSTransitionDisabler disabler(thread);
 153   ThreadsListHandle tlh(current);
 154 
 155   JavaThread* java_thread = nullptr;
 156   oop thread_obj = nullptr;
 157   if (thread == nullptr) {
 158     java_thread = current;
 159     state = java_thread->jvmti_thread_state();
 160   } else {
 161     jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current, &java_thread, &thread_obj);
 162     if (err != JVMTI_ERROR_NONE) {
 163       return err;
 164     }
 165     state = java_lang_Thread::jvmti_thread_state(thread_obj);
 166   }
 167   if (state == nullptr) {
 168     if (data == nullptr) {
 169       // leaving state unset same as data set to null
 170       return JVMTI_ERROR_NONE;
 171     }
 172     // otherwise, create the state
 173     HandleMark hm(current);
 174     Handle thread_handle(current, thread_obj);
 175     state = JvmtiThreadState::state_for(java_thread, thread_handle);
 176     if (state == nullptr) {
 177       return JVMTI_ERROR_THREAD_NOT_ALIVE;
 178     }
 179   }
 180   state->env_thread_state(this)->set_agent_thread_local_storage_data((void*)data);
 181   return JVMTI_ERROR_NONE;
 182 } /* end SetThreadLocalStorage */
 183 
 184 
 185 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
 186 // data_ptr - pre-checked for null
 187 jvmtiError
 188 JvmtiEnv::GetThreadLocalStorage(jthread thread, void** data_ptr) {
 189   JavaThread* current_thread = JavaThread::current();
 190   if (thread == nullptr) {
 191     JvmtiThreadState* state = current_thread->jvmti_thread_state();
 192     *data_ptr = (state == nullptr) ? nullptr :
 193       state->env_thread_state(this)->get_agent_thread_local_storage_data();
 194   } else {
 195     // jvmti_GetThreadLocalStorage is "in native" and doesn't transition
 196     // the thread to _thread_in_vm. However, when the TLS for a thread
 197     // other than the current thread is required we need to transition
 198     // from native so as to resolve the jthread.
 199 
 200     MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, current_thread));
 201     ThreadInVMfromNative __tiv(current_thread);
 202     VM_ENTRY_BASE(jvmtiError, JvmtiEnv::GetThreadLocalStorage , current_thread)
 203     debug_only(VMNativeEntryWrapper __vew;)
 204 
 205     JvmtiVTMSTransitionDisabler disabler(thread);
 206     ThreadsListHandle tlh(current_thread);
 207 
 208     JavaThread* java_thread = nullptr;
 209     oop thread_obj = nullptr;
 210     jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
 211     if (err != JVMTI_ERROR_NONE) {
 212       return err;
 213     }
 214 
 215     HandleMark hm(current_thread);
 216     Handle thread_handle(current_thread, thread_obj);
 217     JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread, thread_handle);
 218     *data_ptr = (state == nullptr) ? nullptr :
 219       state->env_thread_state(this)->get_agent_thread_local_storage_data();
 220   }
 221   return JVMTI_ERROR_NONE;
 222 } /* end GetThreadLocalStorage */
 223 
 224   //
 225   // Module functions
 226   //
 227 
 228 // module_count_ptr - pre-checked for null
 229 // modules_ptr - pre-checked for null
 230 jvmtiError
 231 JvmtiEnv::GetAllModules(jint* module_count_ptr, jobject** modules_ptr) {
 232     JvmtiModuleClosure jmc;
 233 
 234     return jmc.get_all_modules(this, module_count_ptr, modules_ptr);
 235 } /* end GetAllModules */
 236 
 237 
 238 // class_loader - null is a valid value, must be pre-checked
 239 // package_name - pre-checked for null
 240 // module_ptr - pre-checked for null
 241 jvmtiError
 242 JvmtiEnv::GetNamedModule(jobject class_loader, const char* package_name, jobject* module_ptr) {
 243   JavaThread* THREAD = JavaThread::current(); // For exception macros.
 244   ResourceMark rm(THREAD);
 245 
 246   Handle h_loader (THREAD, JNIHandles::resolve(class_loader));
 247   // Check that loader is a subclass of java.lang.ClassLoader.
 248   if (h_loader.not_null() && !java_lang_ClassLoader::is_subclass(h_loader->klass())) {
 249     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 250   }
 251   oop module = Modules::get_named_module(h_loader, package_name);
 252   *module_ptr = module != nullptr ? JNIHandles::make_local(THREAD, module) : nullptr;
 253   return JVMTI_ERROR_NONE;
 254 } /* end GetNamedModule */
 255 
 256 
 257 // module - pre-checked for null
 258 // to_module - pre-checked for null
 259 jvmtiError
 260 JvmtiEnv::AddModuleReads(jobject module, jobject to_module) {
 261   JavaThread* THREAD = JavaThread::current(); // For exception macros.
 262 
 263   // check module
 264   Handle h_module(THREAD, JNIHandles::resolve(module));
 265   if (!java_lang_Module::is_instance(h_module())) {
 266     return JVMTI_ERROR_INVALID_MODULE;
 267   }
 268   // check to_module
 269   Handle h_to_module(THREAD, JNIHandles::resolve(to_module));
 270   if (!java_lang_Module::is_instance(h_to_module())) {
 271     return JVMTI_ERROR_INVALID_MODULE;
 272   }
 273   return JvmtiExport::add_module_reads(h_module, h_to_module, THREAD);
 274 } /* end AddModuleReads */
 275 
 276 
 277 // module - pre-checked for null
 278 // pkg_name - pre-checked for null
 279 // to_module - pre-checked for null
 280 jvmtiError
 281 JvmtiEnv::AddModuleExports(jobject module, const char* pkg_name, jobject to_module) {
 282   JavaThread* THREAD = JavaThread::current(); // For exception macros.
 283   Handle h_pkg = java_lang_String::create_from_str(pkg_name, THREAD);
 284 
 285   // check module
 286   Handle h_module(THREAD, JNIHandles::resolve(module));
 287   if (!java_lang_Module::is_instance(h_module())) {
 288     return JVMTI_ERROR_INVALID_MODULE;
 289   }
 290   // check to_module
 291   Handle h_to_module(THREAD, JNIHandles::resolve(to_module));
 292   if (!java_lang_Module::is_instance(h_to_module())) {
 293     return JVMTI_ERROR_INVALID_MODULE;
 294   }
 295   return JvmtiExport::add_module_exports(h_module, h_pkg, h_to_module, THREAD);
 296 } /* end AddModuleExports */
 297 
 298 
 299 // module - pre-checked for null
 300 // pkg_name - pre-checked for null
 301 // to_module - pre-checked for null
 302 jvmtiError
 303 JvmtiEnv::AddModuleOpens(jobject module, const char* pkg_name, jobject to_module) {
 304   JavaThread* THREAD = JavaThread::current(); // For exception macros.
 305   Handle h_pkg = java_lang_String::create_from_str(pkg_name, THREAD);
 306 
 307   // check module
 308   Handle h_module(THREAD, JNIHandles::resolve(module));
 309   if (!java_lang_Module::is_instance(h_module())) {
 310     return JVMTI_ERROR_INVALID_MODULE;
 311   }
 312   // check to_module
 313   Handle h_to_module(THREAD, JNIHandles::resolve(to_module));
 314   if (!java_lang_Module::is_instance(h_to_module())) {
 315     return JVMTI_ERROR_INVALID_MODULE;
 316   }
 317   return JvmtiExport::add_module_opens(h_module, h_pkg, h_to_module, THREAD);
 318 } /* end AddModuleOpens */
 319 
 320 
 321 // module - pre-checked for null
 322 // service - pre-checked for null
 323 jvmtiError
 324 JvmtiEnv::AddModuleUses(jobject module, jclass service) {
 325   JavaThread* THREAD = JavaThread::current(); // For exception macros.
 326 
 327   // check module
 328   Handle h_module(THREAD, JNIHandles::resolve(module));
 329   if (!java_lang_Module::is_instance(h_module())) {
 330     return JVMTI_ERROR_INVALID_MODULE;
 331   }
 332   // check service
 333   Handle h_service(THREAD, JNIHandles::resolve_external_guard(service));
 334   if (!java_lang_Class::is_instance(h_service()) ||
 335       java_lang_Class::is_primitive(h_service())) {
 336     return JVMTI_ERROR_INVALID_CLASS;
 337   }
 338   return JvmtiExport::add_module_uses(h_module, h_service, THREAD);
 339 } /* end AddModuleUses */
 340 
 341 
 342 // module - pre-checked for null
 343 // service - pre-checked for null
 344 // impl_class - pre-checked for null
 345 jvmtiError
 346 JvmtiEnv::AddModuleProvides(jobject module, jclass service, jclass impl_class) {
 347   JavaThread* THREAD = JavaThread::current(); // For exception macros.
 348 
 349   // check module
 350   Handle h_module(THREAD, JNIHandles::resolve(module));
 351   if (!java_lang_Module::is_instance(h_module())) {
 352     return JVMTI_ERROR_INVALID_MODULE;
 353   }
 354   // check service
 355   Handle h_service(THREAD, JNIHandles::resolve_external_guard(service));
 356   if (!java_lang_Class::is_instance(h_service()) ||
 357       java_lang_Class::is_primitive(h_service())) {
 358     return JVMTI_ERROR_INVALID_CLASS;
 359   }
 360   // check impl_class
 361   Handle h_impl_class(THREAD, JNIHandles::resolve_external_guard(impl_class));
 362   if (!java_lang_Class::is_instance(h_impl_class()) ||
 363       java_lang_Class::is_primitive(h_impl_class())) {
 364     return JVMTI_ERROR_INVALID_CLASS;
 365   }
 366   return JvmtiExport::add_module_provides(h_module, h_service, h_impl_class, THREAD);
 367 } /* end AddModuleProvides */
 368 
 369 // module - pre-checked for null
 370 // is_modifiable_class_ptr - pre-checked for null
 371 jvmtiError
 372 JvmtiEnv::IsModifiableModule(jobject module, jboolean* is_modifiable_module_ptr) {
 373   JavaThread* current = JavaThread::current();
 374 
 375   // check module
 376   Handle h_module(current, JNIHandles::resolve(module));
 377   if (!java_lang_Module::is_instance(h_module())) {
 378     return JVMTI_ERROR_INVALID_MODULE;
 379   }
 380 
 381   *is_modifiable_module_ptr = JNI_TRUE;
 382   return JVMTI_ERROR_NONE;
 383 } /* end IsModifiableModule */
 384 
 385 
 386   //
 387   // Class functions
 388   //
 389 
 390 // class_count_ptr - pre-checked for null
 391 // classes_ptr - pre-checked for null
 392 jvmtiError
 393 JvmtiEnv::GetLoadedClasses(jint* class_count_ptr, jclass** classes_ptr) {
 394   return JvmtiGetLoadedClasses::getLoadedClasses(this, class_count_ptr, classes_ptr);
 395 } /* end GetLoadedClasses */
 396 
 397 
 398 // initiating_loader - null is a valid value, must be checked
 399 // class_count_ptr - pre-checked for null
 400 // classes_ptr - pre-checked for null
 401 jvmtiError
 402 JvmtiEnv::GetClassLoaderClasses(jobject initiating_loader, jint* class_count_ptr, jclass** classes_ptr) {
 403   return JvmtiGetLoadedClasses::getClassLoaderClasses(this, initiating_loader,
 404                                                   class_count_ptr, classes_ptr);
 405 } /* end GetClassLoaderClasses */
 406 
 407 // k_mirror - may be primitive, this must be checked
 408 // is_modifiable_class_ptr - pre-checked for null
 409 jvmtiError
 410 JvmtiEnv::IsModifiableClass(oop k_mirror, jboolean* is_modifiable_class_ptr) {
 411   *is_modifiable_class_ptr = VM_RedefineClasses::is_modifiable_class(k_mirror)?
 412                                                        JNI_TRUE : JNI_FALSE;
 413   return JVMTI_ERROR_NONE;
 414 } /* end IsModifiableClass */
 415 
 416 // class_count - pre-checked to be greater than or equal to 0
 417 // classes - pre-checked for null
 418 jvmtiError
 419 JvmtiEnv::RetransformClasses(jint class_count, const jclass* classes) {
 420 //TODO: add locking
 421 
 422   int index;
 423   JavaThread* current_thread = JavaThread::current();
 424   ResourceMark rm(current_thread);
 425 
 426   jvmtiClassDefinition* class_definitions =
 427                             NEW_RESOURCE_ARRAY(jvmtiClassDefinition, class_count);
 428   NULL_CHECK(class_definitions, JVMTI_ERROR_OUT_OF_MEMORY);
 429 
 430   for (index = 0; index < class_count; index++) {
 431     HandleMark hm(current_thread);
 432 
 433     jclass jcls = classes[index];
 434     oop k_mirror = JNIHandles::resolve_external_guard(jcls);
 435     if (k_mirror == nullptr) {
 436       return JVMTI_ERROR_INVALID_CLASS;
 437     }
 438     if (!k_mirror->is_a(vmClasses::Class_klass())) {
 439       return JVMTI_ERROR_INVALID_CLASS;
 440     }
 441 
 442     if (!VM_RedefineClasses::is_modifiable_class(k_mirror)) {
 443       return JVMTI_ERROR_UNMODIFIABLE_CLASS;
 444     }
 445 
 446     Klass* klass = java_lang_Class::as_Klass(k_mirror);
 447 
 448     jint status = klass->jvmti_class_status();
 449     if (status & (JVMTI_CLASS_STATUS_ERROR)) {
 450       return JVMTI_ERROR_INVALID_CLASS;
 451     }
 452 
 453     InstanceKlass* ik = InstanceKlass::cast(klass);
 454     if (ik->get_cached_class_file_bytes() == nullptr) {
 455       // Not cached, we need to reconstitute the class file from the
 456       // VM representation. We don't attach the reconstituted class
 457       // bytes to the InstanceKlass here because they have not been
 458       // validated and we're not at a safepoint.
 459       JvmtiClassFileReconstituter reconstituter(ik);
 460       if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
 461         return reconstituter.get_error();
 462       }
 463 
 464       class_definitions[index].class_byte_count = (jint)reconstituter.class_file_size();
 465       class_definitions[index].class_bytes      = (unsigned char*)
 466                                                        reconstituter.class_file_bytes();
 467     } else {
 468       // it is cached, get it from the cache
 469       class_definitions[index].class_byte_count = ik->get_cached_class_file_len();
 470       class_definitions[index].class_bytes      = ik->get_cached_class_file_bytes();
 471     }
 472     class_definitions[index].klass              = jcls;
 473   }
 474   EventRetransformClasses event;
 475   VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_retransform);
 476   VMThread::execute(&op);
 477   jvmtiError error = op.check_error();
 478   if (error == JVMTI_ERROR_NONE) {
 479     event.set_classCount(class_count);
 480     event.set_redefinitionId(op.id());
 481     event.commit();
 482   }
 483   return error;
 484 } /* end RetransformClasses */
 485 
 486 
 487 // class_count - pre-checked to be greater than or equal to 0
 488 // class_definitions - pre-checked for null
 489 jvmtiError
 490 JvmtiEnv::RedefineClasses(jint class_count, const jvmtiClassDefinition* class_definitions) {
 491 //TODO: add locking
 492   EventRedefineClasses event;
 493   VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_redefine);
 494   VMThread::execute(&op);
 495   jvmtiError error = op.check_error();
 496   if (error == JVMTI_ERROR_NONE) {
 497     event.set_classCount(class_count);
 498     event.set_redefinitionId(op.id());
 499     event.commit();
 500   }
 501   return error;
 502 } /* end RedefineClasses */
 503 
 504 
 505   //
 506   // Object functions
 507   //
 508 
 509 // size_ptr - pre-checked for null
 510 jvmtiError
 511 JvmtiEnv::GetObjectSize(jobject object, jlong* size_ptr) {
 512   oop mirror = JNIHandles::resolve_external_guard(object);
 513   NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
 514   *size_ptr = (jlong)mirror->size() * wordSize;
 515   return JVMTI_ERROR_NONE;
 516 } /* end GetObjectSize */
 517 
 518   //
 519   // Method functions
 520   //
 521 
 522 // prefix - null is a valid value, must be checked
 523 jvmtiError
 524 JvmtiEnv::SetNativeMethodPrefix(const char* prefix) {
 525   return prefix == nullptr?
 526               SetNativeMethodPrefixes(0, nullptr) :
 527               SetNativeMethodPrefixes(1, (char**)&prefix);
 528 } /* end SetNativeMethodPrefix */
 529 
 530 
 531 // prefix_count - pre-checked to be greater than or equal to 0
 532 // prefixes - pre-checked for null
 533 jvmtiError
 534 JvmtiEnv::SetNativeMethodPrefixes(jint prefix_count, char** prefixes) {
 535   // Have to grab JVMTI thread state lock to be sure that some thread
 536   // isn't accessing the prefixes at the same time we are setting them.
 537   // No locks during VM bring-up.
 538   if (Threads::number_of_threads() == 0) {
 539     return set_native_method_prefixes(prefix_count, prefixes);
 540   } else {
 541     MutexLocker mu(JvmtiThreadState_lock);
 542     return set_native_method_prefixes(prefix_count, prefixes);
 543   }
 544 } /* end SetNativeMethodPrefixes */
 545 
 546   //
 547   // Event Management functions
 548   //
 549 
 550 // callbacks - null is a valid value, must be checked
 551 // size_of_callbacks - pre-checked to be greater than or equal to 0
 552 jvmtiError
 553 JvmtiEnv::SetEventCallbacks(const jvmtiEventCallbacks* callbacks, jint size_of_callbacks) {
 554   JvmtiVTMSTransitionDisabler disabler;
 555   JvmtiEventController::set_event_callbacks(this, callbacks, size_of_callbacks);
 556   return JVMTI_ERROR_NONE;
 557 } /* end SetEventCallbacks */
 558 
 559 
 560 // event_thread - null is a valid value, must be checked
 561 jvmtiError
 562 JvmtiEnv::SetEventNotificationMode(jvmtiEventMode mode, jvmtiEvent event_type, jthread event_thread,   ...) {
 563   bool enabled = (mode == JVMTI_ENABLE);
 564 
 565   // event_type must be valid
 566   if (!JvmtiEventController::is_valid_event_type(event_type)) {
 567     return JVMTI_ERROR_INVALID_EVENT_TYPE;
 568   }
 569 
 570   // assure that needed capabilities are present
 571   if (enabled && !JvmtiUtil::has_event_capability(event_type, get_capabilities())) {
 572     return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
 573   }
 574 
 575   if (event_type == JVMTI_EVENT_CLASS_FILE_LOAD_HOOK && enabled) {
 576     record_class_file_load_hook_enabled();
 577   }
 578   JvmtiVTMSTransitionDisabler disabler;
 579 
 580   if (event_thread == nullptr) {
 581     // Can be called at Agent_OnLoad() time with event_thread == nullptr
 582     // when Thread::current() does not work yet so we cannot create a
 583     // ThreadsListHandle that is common to both thread-specific and
 584     // global code paths.
 585 
 586     JvmtiEventController::set_user_enabled(this, nullptr, (oop) nullptr, event_type, enabled);
 587   } else {
 588     // We have a specified event_thread.
 589     JavaThread* current = JavaThread::current();
 590     ThreadsListHandle tlh(current);
 591 
 592     JavaThread* java_thread = nullptr;
 593     oop thread_obj = nullptr;
 594     jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), event_thread, current, &java_thread, &thread_obj);
 595     if (err != JVMTI_ERROR_NONE) {
 596       return err;
 597     }
 598 
 599     // global events cannot be controlled at thread level.
 600     if (JvmtiEventController::is_global_event(event_type)) {
 601       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 602     }
 603 
 604     JvmtiEventController::set_user_enabled(this, java_thread, thread_obj, event_type, enabled);
 605   }
 606 
 607   return JVMTI_ERROR_NONE;
 608 } /* end SetEventNotificationMode */
 609 
 610   //
 611   // Capability functions
 612   //
 613 
 614 // capabilities_ptr - pre-checked for null
 615 jvmtiError
 616 JvmtiEnv::GetPotentialCapabilities(jvmtiCapabilities* capabilities_ptr) {
 617   JvmtiManageCapabilities::get_potential_capabilities(get_capabilities(),
 618                                                       get_prohibited_capabilities(),
 619                                                       capabilities_ptr);
 620   return JVMTI_ERROR_NONE;
 621 } /* end GetPotentialCapabilities */
 622 
 623 
 624 // capabilities_ptr - pre-checked for null
 625 jvmtiError
 626 JvmtiEnv::AddCapabilities(const jvmtiCapabilities* capabilities_ptr) {
 627   return JvmtiManageCapabilities::add_capabilities(get_capabilities(),
 628                                                    get_prohibited_capabilities(),
 629                                                    capabilities_ptr,
 630                                                    get_capabilities());
 631 } /* end AddCapabilities */
 632 
 633 
 634 // capabilities_ptr - pre-checked for null
 635 jvmtiError
 636 JvmtiEnv::RelinquishCapabilities(const jvmtiCapabilities* capabilities_ptr) {
 637   JvmtiManageCapabilities::relinquish_capabilities(get_capabilities(), capabilities_ptr, get_capabilities());
 638   return JVMTI_ERROR_NONE;
 639 } /* end RelinquishCapabilities */
 640 
 641 
 642 // capabilities_ptr - pre-checked for null
 643 jvmtiError
 644 JvmtiEnv::GetCapabilities(jvmtiCapabilities* capabilities_ptr) {
 645   JvmtiManageCapabilities::copy_capabilities(get_capabilities(), capabilities_ptr);
 646   return JVMTI_ERROR_NONE;
 647 } /* end GetCapabilities */
 648 
 649   //
 650   // Class Loader Search functions
 651   //
 652 
 653 // segment - pre-checked for null
 654 jvmtiError
 655 JvmtiEnv::AddToBootstrapClassLoaderSearch(const char* segment) {
 656   jvmtiPhase phase = get_phase();
 657   if (phase == JVMTI_PHASE_ONLOAD) {
 658     Arguments::append_sysclasspath(segment);
 659     return JVMTI_ERROR_NONE;
 660   } else if (use_version_1_0_semantics()) {
 661     // This JvmtiEnv requested version 1.0 semantics and this function
 662     // is only allowed in the ONLOAD phase in version 1.0 so we need to
 663     // return an error here.
 664     return JVMTI_ERROR_WRONG_PHASE;
 665   } else if (phase == JVMTI_PHASE_LIVE) {
 666     // The phase is checked by the wrapper that called this function,
 667     // but this thread could be racing with the thread that is
 668     // terminating the VM so we check one more time.
 669 
 670     // create the zip entry
 671     ClassPathZipEntry* zip_entry = ClassLoader::create_class_path_zip_entry(segment, true);
 672     if (zip_entry == nullptr) {
 673       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 674     }
 675 
 676     // add the jar file to the bootclasspath
 677     log_info(class, load)("opened: %s", zip_entry->name());
 678 #if INCLUDE_CDS
 679     ClassLoaderExt::append_boot_classpath(zip_entry);
 680 #else
 681     ClassLoader::add_to_boot_append_entries(zip_entry);
 682 #endif
 683     return JVMTI_ERROR_NONE;
 684   } else {
 685     return JVMTI_ERROR_WRONG_PHASE;
 686   }
 687 
 688 } /* end AddToBootstrapClassLoaderSearch */
 689 
 690 
 691 // segment - pre-checked for null
 692 jvmtiError
 693 JvmtiEnv::AddToSystemClassLoaderSearch(const char* segment) {
 694   jvmtiPhase phase = get_phase();
 695 
 696   if (phase == JVMTI_PHASE_ONLOAD) {
 697     for (SystemProperty* p = Arguments::system_properties(); p != nullptr; p = p->next()) {
 698       if (strcmp("java.class.path", p->key()) == 0) {
 699         p->append_value(segment);
 700         break;
 701       }
 702     }
 703     return JVMTI_ERROR_NONE;
 704   } else if (phase == JVMTI_PHASE_LIVE) {
 705     // The phase is checked by the wrapper that called this function,
 706     // but this thread could be racing with the thread that is
 707     // terminating the VM so we check one more time.
 708     JavaThread* THREAD = JavaThread::current(); // For exception macros.
 709     HandleMark hm(THREAD);
 710 
 711     // create the zip entry (which will open the zip file and hence
 712     // check that the segment is indeed a zip file).
 713     ClassPathZipEntry* zip_entry = ClassLoader::create_class_path_zip_entry(segment, false);
 714     if (zip_entry == nullptr) {
 715       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 716     }
 717     delete zip_entry;   // no longer needed
 718 
 719     Handle loader(THREAD, SystemDictionary::java_system_loader());
 720 
 721     // need the path as java.lang.String
 722     Handle path = java_lang_String::create_from_platform_dependent_str(segment, THREAD);
 723     if (HAS_PENDING_EXCEPTION) {
 724       CLEAR_PENDING_EXCEPTION;
 725       return JVMTI_ERROR_INTERNAL;
 726     }
 727 
 728     // Invoke the appendToClassPathForInstrumentation method - if the method
 729     // is not found it means the loader doesn't support adding to the class path
 730     // in the live phase.
 731     {
 732       JavaValue res(T_VOID);
 733       JavaCalls::call_special(&res,
 734                               loader,
 735                               loader->klass(),
 736                               vmSymbols::appendToClassPathForInstrumentation_name(),
 737                               vmSymbols::appendToClassPathForInstrumentation_signature(),
 738                               path,
 739                               THREAD);
 740       if (HAS_PENDING_EXCEPTION) {
 741         Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
 742         CLEAR_PENDING_EXCEPTION;
 743 
 744         if (ex_name == vmSymbols::java_lang_NoSuchMethodError()) {
 745           return JVMTI_ERROR_CLASS_LOADER_UNSUPPORTED;
 746         } else {
 747           return JVMTI_ERROR_INTERNAL;
 748         }
 749       }
 750     }
 751 
 752     return JVMTI_ERROR_NONE;
 753   } else {
 754     return JVMTI_ERROR_WRONG_PHASE;
 755   }
 756 } /* end AddToSystemClassLoaderSearch */
 757 
 758   //
 759   // General functions
 760   //
 761 
 762 // phase_ptr - pre-checked for null
 763 jvmtiError
 764 JvmtiEnv::GetPhase(jvmtiPhase* phase_ptr) {
 765   *phase_ptr = phase();
 766   return JVMTI_ERROR_NONE;
 767 } /* end GetPhase */
 768 
 769 
 770 jvmtiError
 771 JvmtiEnv::DisposeEnvironment() {
 772   dispose();
 773   return JVMTI_ERROR_NONE;
 774 } /* end DisposeEnvironment */
 775 
 776 
 777 // data - null is a valid value, must be checked
 778 jvmtiError
 779 JvmtiEnv::SetEnvironmentLocalStorage(const void* data) {
 780   set_env_local_storage(data);
 781   return JVMTI_ERROR_NONE;
 782 } /* end SetEnvironmentLocalStorage */
 783 
 784 
 785 // data_ptr - pre-checked for null
 786 jvmtiError
 787 JvmtiEnv::GetEnvironmentLocalStorage(void** data_ptr) {
 788   *data_ptr = (void*)get_env_local_storage();
 789   return JVMTI_ERROR_NONE;
 790 } /* end GetEnvironmentLocalStorage */
 791 
 792 // version_ptr - pre-checked for null
 793 jvmtiError
 794 JvmtiEnv::GetVersionNumber(jint* version_ptr) {
 795   *version_ptr = JVMTI_VERSION;
 796   return JVMTI_ERROR_NONE;
 797 } /* end GetVersionNumber */
 798 
 799 
 800 // name_ptr - pre-checked for null
 801 jvmtiError
 802 JvmtiEnv::GetErrorName(jvmtiError error, char** name_ptr) {
 803   if (error < JVMTI_ERROR_NONE || error > JVMTI_ERROR_MAX) {
 804     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 805   }
 806   const char *name = JvmtiUtil::error_name(error);
 807   if (name == nullptr) {
 808     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 809   }
 810   size_t len = strlen(name) + 1;
 811   jvmtiError err = allocate(len, (unsigned char**)name_ptr);
 812   if (err == JVMTI_ERROR_NONE) {
 813     memcpy(*name_ptr, name, len);
 814   }
 815   return err;
 816 } /* end GetErrorName */
 817 
 818 
 819 jvmtiError
 820 JvmtiEnv::SetVerboseFlag(jvmtiVerboseFlag flag, jboolean value) {
 821   LogLevelType level = value == 0 ? LogLevel::Off : LogLevel::Info;
 822   switch (flag) {
 823   case JVMTI_VERBOSE_OTHER:
 824     // ignore
 825     break;
 826   case JVMTI_VERBOSE_CLASS:
 827     LogConfiguration::configure_stdout(level, false, LOG_TAGS(class, unload));
 828     LogConfiguration::configure_stdout(level, false, LOG_TAGS(class, load));
 829     break;
 830   case JVMTI_VERBOSE_GC:
 831     LogConfiguration::configure_stdout(level, true, LOG_TAGS(gc));
 832     break;
 833   case JVMTI_VERBOSE_JNI:
 834     level = value == 0 ? LogLevel::Off : LogLevel::Debug;
 835     LogConfiguration::configure_stdout(level, true, LOG_TAGS(jni, resolve));
 836     break;
 837   default:
 838     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 839   };
 840   return JVMTI_ERROR_NONE;
 841 } /* end SetVerboseFlag */
 842 
 843 
 844 // format_ptr - pre-checked for null
 845 jvmtiError
 846 JvmtiEnv::GetJLocationFormat(jvmtiJlocationFormat* format_ptr) {
 847   *format_ptr = JVMTI_JLOCATION_JVMBCI;
 848   return JVMTI_ERROR_NONE;
 849 } /* end GetJLocationFormat */
 850 
 851   //
 852   // Thread functions
 853   //
 854 
 855 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
 856 // thread_state_ptr - pre-checked for null
 857 jvmtiError
 858 JvmtiEnv::GetThreadState(jthread thread, jint* thread_state_ptr) {
 859   JavaThread* current_thread = JavaThread::current();
 860   JvmtiVTMSTransitionDisabler disabler(thread);
 861   ThreadsListHandle tlh(current_thread);
 862 
 863   JavaThread* java_thread = nullptr;
 864   oop thread_oop = nullptr;
 865   jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_oop);
 866   if (err != JVMTI_ERROR_NONE && err != JVMTI_ERROR_THREAD_NOT_ALIVE) {
 867     // We got an error code so we don't have a JavaThread*, but only
 868     // return an error from here if the error is not because the thread
 869     // is a virtual thread.
 870     return err;
 871   }
 872   *thread_state_ptr = JvmtiEnvBase::get_thread_or_vthread_state(thread_oop, java_thread);
 873   return JVMTI_ERROR_NONE;
 874 } /* end GetThreadState */
 875 
 876 
 877 // thread_ptr - pre-checked for null
 878 jvmtiError
 879 JvmtiEnv::GetCurrentThread(jthread* thread_ptr) {
 880   JavaThread* cur_thread = JavaThread::current();
 881   oop thread_oop = get_vthread_or_thread_oop(cur_thread);
 882 
 883   *thread_ptr = (jthread)JNIHandles::make_local(cur_thread, thread_oop);
 884   return JVMTI_ERROR_NONE;
 885 } /* end GetCurrentThread */
 886 
 887 
 888 // threads_count_ptr - pre-checked for null
 889 // threads_ptr - pre-checked for null
 890 jvmtiError
 891 JvmtiEnv::GetAllThreads(jint* threads_count_ptr, jthread** threads_ptr) {
 892   int nthreads        = 0;
 893   Handle *thread_objs = nullptr;
 894   Thread* current_thread = Thread::current();
 895   ResourceMark rm(current_thread);
 896   HandleMark hm(current_thread);
 897 
 898   // enumerate threads (including agent threads)
 899   ThreadsListEnumerator tle(current_thread, true);
 900   nthreads = tle.num_threads();
 901   *threads_count_ptr = nthreads;
 902 
 903   if (nthreads == 0) {
 904     *threads_ptr = nullptr;
 905     return JVMTI_ERROR_NONE;
 906   }
 907 
 908   thread_objs = NEW_RESOURCE_ARRAY(Handle, nthreads);
 909   NULL_CHECK(thread_objs, JVMTI_ERROR_OUT_OF_MEMORY);
 910 
 911   for (int i = 0; i < nthreads; i++) {
 912     thread_objs[i] = Handle(tle.get_threadObj(i));
 913   }
 914 
 915   jthread *jthreads  = new_jthreadArray(nthreads, thread_objs);
 916   NULL_CHECK(jthreads, JVMTI_ERROR_OUT_OF_MEMORY);
 917 
 918   *threads_ptr = jthreads;
 919   return JVMTI_ERROR_NONE;
 920 } /* end GetAllThreads */
 921 
 922 
 923 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
 924 jvmtiError
 925 JvmtiEnv::SuspendThread(jthread thread) {
 926   JavaThread* current = JavaThread::current();
 927   HandleMark hm(current);
 928   Handle self_tobj;
 929 
 930   jvmtiError err;
 931   {
 932     JvmtiVTMSTransitionDisabler disabler(true);
 933     ThreadsListHandle tlh(current);
 934     JavaThread* java_thread = nullptr;
 935     oop thread_oop = nullptr;
 936 
 937     err = get_threadOop_and_JavaThread(tlh.list(), thread, current, &java_thread, &thread_oop);
 938     if (err != JVMTI_ERROR_NONE) {
 939       return err;
 940     }
 941 
 942     // Do not use JvmtiVTMSTransitionDisabler in context of self suspend to avoid deadlocks.
 943     if (java_thread != current) {
 944       err = suspend_thread(thread_oop, java_thread, /* single_suspend */ true, nullptr);
 945       return err;
 946     }
 947     // protect thread_oop as a safepoint can be reached in disabler destructor
 948     self_tobj = Handle(current, thread_oop);
 949   }
 950   // Do self suspend for current JavaThread.
 951   err = suspend_thread(self_tobj(), current, /* single_suspend */ true, nullptr);
 952   return err;
 953 } /* end SuspendThread */
 954 
 955 
 956 // request_count - pre-checked to be greater than or equal to 0
 957 // request_list - pre-checked for null
 958 // results - pre-checked for null
 959 jvmtiError
 960 JvmtiEnv::SuspendThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
 961   JavaThread* current = JavaThread::current();
 962   HandleMark hm(current);
 963   Handle self_tobj;
 964   int self_idx = -1;
 965 
 966   {
 967     JvmtiVTMSTransitionDisabler disabler(true);
 968     ThreadsListHandle tlh(current);
 969 
 970     for (int i = 0; i < request_count; i++) {
 971       JavaThread *java_thread = nullptr;
 972       oop thread_oop = nullptr;
 973       jthread thread = request_list[i];
 974       jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
 975 
 976       if (thread_oop != nullptr &&
 977           java_lang_VirtualThread::is_instance(thread_oop) &&
 978           !JvmtiEnvBase::is_vthread_alive(thread_oop)) {
 979         err = JVMTI_ERROR_THREAD_NOT_ALIVE;
 980       }
 981       if (err != JVMTI_ERROR_NONE) {
 982         if (thread_oop == nullptr || err != JVMTI_ERROR_INVALID_THREAD) {
 983           results[i] = err;
 984           continue;
 985         }
 986       }
 987       if (java_thread == current) {
 988         self_idx = i;
 989         self_tobj = Handle(current, thread_oop);
 990         continue; // self suspend after all other suspends
 991       }
 992       results[i] = suspend_thread(thread_oop, java_thread, /* single_suspend */ true, nullptr);
 993     }
 994   }
 995   // Self suspend after all other suspends if necessary.
 996   // Do not use JvmtiVTMSTransitionDisabler in context of self suspend to avoid deadlocks.
 997   if (self_tobj() != nullptr) {
 998     // there should not be any error for current java_thread
 999     results[self_idx] = suspend_thread(self_tobj(), current, /* single_suspend */ true, nullptr);
1000   }
1001   // per-thread suspend results returned via results parameter
1002   return JVMTI_ERROR_NONE;
1003 } /* end SuspendThreadList */
1004 
1005 
1006 jvmtiError
1007 JvmtiEnv::SuspendAllVirtualThreads(jint except_count, const jthread* except_list) {
1008   if (get_capabilities()->can_support_virtual_threads == 0) {
1009     return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
1010   }
1011   JavaThread* current = JavaThread::current();
1012   HandleMark hm(current);
1013   Handle self_tobj;
1014 
1015   {
1016     ResourceMark rm(current);
1017     JvmtiVTMSTransitionDisabler disabler(true);
1018     ThreadsListHandle tlh(current);
1019     GrowableArray<jthread>* elist = new GrowableArray<jthread>(except_count);
1020 
1021     jvmtiError err = JvmtiEnvBase::check_thread_list(except_count, except_list);
1022     if (err != JVMTI_ERROR_NONE) {
1023       return err;
1024     }
1025 
1026     // Collect threads from except_list for which resumed status must be restored (only for VirtualThread case)
1027     for (int idx = 0; idx < except_count; idx++) {
1028       jthread thread = except_list[idx];
1029       oop thread_oop = JNIHandles::resolve_external_guard(thread);
1030       if (java_lang_VirtualThread::is_instance(thread_oop) && !JvmtiVTSuspender::is_vthread_suspended(thread_oop)) {
1031           // is not suspended, so its resumed status must be restored
1032           elist->append(except_list[idx]);
1033       }
1034     }
1035 
1036     for (JavaThreadIteratorWithHandle jtiwh; JavaThread *java_thread = jtiwh.next(); ) {
1037       oop vt_oop = java_thread->jvmti_vthread();
1038       if (!java_thread->is_exiting() &&
1039           !java_thread->is_jvmti_agent_thread() &&
1040           !java_thread->is_hidden_from_external_view() &&
1041           vt_oop != nullptr &&
1042           ((java_lang_VirtualThread::is_instance(vt_oop) &&
1043             JvmtiEnvBase::is_vthread_alive(vt_oop) &&
1044             !JvmtiVTSuspender::is_vthread_suspended(vt_oop)) ||
1045             (vt_oop->is_a(vmClasses::BoundVirtualThread_klass()) && !java_thread->is_suspended())) &&
1046           !is_in_thread_list(except_count, except_list, vt_oop)
1047          ) {
1048         if (java_thread == current) {
1049           self_tobj = Handle(current, vt_oop);
1050           continue; // self suspend after all other suspends
1051         }
1052         suspend_thread(vt_oop, java_thread, /* single_suspend */ false, nullptr);
1053       }
1054     }
1055     JvmtiVTSuspender::register_all_vthreads_suspend();
1056 
1057     // Restore resumed state for threads from except list that were not suspended before.
1058     for (int idx = 0; idx < elist->length(); idx++) {
1059       jthread thread = elist->at(idx);
1060       oop thread_oop = JNIHandles::resolve_external_guard(thread);
1061       if (JvmtiVTSuspender::is_vthread_suspended(thread_oop)) {
1062         JvmtiVTSuspender::register_vthread_resume(thread_oop);
1063       }
1064     }
1065   }
1066   // Self suspend after all other suspends if necessary.
1067   // Do not use JvmtiVTMSTransitionDisabler in context of self suspend to avoid deadlocks.
1068   if (self_tobj() != nullptr) {
1069     suspend_thread(self_tobj(), current, /* single_suspend */ false, nullptr);
1070   }
1071   return JVMTI_ERROR_NONE;
1072 } /* end SuspendAllVirtualThreads */
1073 
1074 
1075 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1076 jvmtiError
1077 JvmtiEnv::ResumeThread(jthread thread) {
1078   JvmtiVTMSTransitionDisabler disabler(true);
1079   JavaThread* current = JavaThread::current();
1080   ThreadsListHandle tlh(current);
1081 
1082   JavaThread* java_thread = nullptr;
1083   oop thread_oop = nullptr;
1084   jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current, &java_thread, &thread_oop);
1085   if (err != JVMTI_ERROR_NONE) {
1086     return err;
1087   }
1088   err = resume_thread(thread_oop, java_thread, /* single_resume */ true);
1089   return err;
1090 } /* end ResumeThread */
1091 
1092 
1093 // request_count - pre-checked to be greater than or equal to 0
1094 // request_list - pre-checked for null
1095 // results - pre-checked for null
1096 jvmtiError
1097 JvmtiEnv::ResumeThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
1098   oop thread_oop = nullptr;
1099   JavaThread* java_thread = nullptr;
1100   JvmtiVTMSTransitionDisabler disabler(true);
1101   ThreadsListHandle tlh;
1102 
1103   for (int i = 0; i < request_count; i++) {
1104     jthread thread = request_list[i];
1105     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1106 
1107     if (thread_oop != nullptr &&
1108         java_lang_VirtualThread::is_instance(thread_oop) &&
1109         !JvmtiEnvBase::is_vthread_alive(thread_oop)) {
1110       err = JVMTI_ERROR_THREAD_NOT_ALIVE;
1111     }
1112     if (err != JVMTI_ERROR_NONE) {
1113       if (thread_oop == nullptr || err != JVMTI_ERROR_INVALID_THREAD) {
1114         results[i] = err;
1115         continue;
1116       }
1117     }
1118     results[i] = resume_thread(thread_oop, java_thread, /* single_resume */ true);
1119   }
1120   // per-thread resume results returned via results parameter
1121   return JVMTI_ERROR_NONE;
1122 } /* end ResumeThreadList */
1123 
1124 
1125 jvmtiError
1126 JvmtiEnv::ResumeAllVirtualThreads(jint except_count, const jthread* except_list) {
1127   if (get_capabilities()->can_support_virtual_threads == 0) {
1128     return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
1129   }
1130   jvmtiError err = JvmtiEnvBase::check_thread_list(except_count, except_list);
1131   if (err != JVMTI_ERROR_NONE) {
1132     return err;
1133   }
1134   ResourceMark rm;
1135   JvmtiVTMSTransitionDisabler disabler(true);
1136   GrowableArray<jthread>* elist = new GrowableArray<jthread>(except_count);
1137 
1138   // Collect threads from except_list for which suspended status must be restored (only for VirtualThread case)
1139   for (int idx = 0; idx < except_count; idx++) {
1140     jthread thread = except_list[idx];
1141     oop thread_oop = JNIHandles::resolve_external_guard(thread);
1142     if (java_lang_VirtualThread::is_instance(thread_oop) && JvmtiVTSuspender::is_vthread_suspended(thread_oop)) {
1143       // is suspended, so its suspended status must be restored
1144       elist->append(except_list[idx]);
1145     }
1146   }
1147 
1148   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *java_thread = jtiwh.next(); ) {
1149     oop vt_oop = java_thread->jvmti_vthread();
1150     if (!java_thread->is_exiting() &&
1151         !java_thread->is_jvmti_agent_thread() &&
1152         !java_thread->is_hidden_from_external_view() &&
1153         vt_oop != nullptr &&
1154         ((java_lang_VirtualThread::is_instance(vt_oop) &&
1155           JvmtiEnvBase::is_vthread_alive(vt_oop) &&
1156           JvmtiVTSuspender::is_vthread_suspended(vt_oop)) ||
1157           (vt_oop->is_a(vmClasses::BoundVirtualThread_klass()) && java_thread->is_suspended())) &&
1158         !is_in_thread_list(except_count, except_list, vt_oop)
1159     ) {
1160       resume_thread(vt_oop, java_thread, /* single_resume */ false);
1161     }
1162   }
1163   JvmtiVTSuspender::register_all_vthreads_resume();
1164 
1165   // Restore suspended state for threads from except list that were suspended before.
1166   for (int idx = 0; idx < elist->length(); idx++) {
1167     jthread thread = elist->at(idx);
1168     oop thread_oop = JNIHandles::resolve_external_guard(thread);
1169     if (!JvmtiVTSuspender::is_vthread_suspended(thread_oop)) {
1170       JvmtiVTSuspender::register_vthread_suspend(thread_oop);
1171     }
1172   }
1173   return JVMTI_ERROR_NONE;
1174 } /* end ResumeAllVirtualThreads */
1175 
1176 
1177 jvmtiError
1178 JvmtiEnv::StopThread(jthread thread, jobject exception) {
1179   JavaThread* current_thread = JavaThread::current();
1180 
1181   JvmtiVTMSTransitionDisabler disabler(thread);
1182   ThreadsListHandle tlh(current_thread);
1183   JavaThread* java_thread = nullptr;
1184   oop thread_oop = nullptr;
1185 
1186   NULL_CHECK(thread, JVMTI_ERROR_INVALID_THREAD);
1187 
1188   jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_oop);
1189 
1190   bool is_virtual = thread_oop != nullptr && thread_oop->is_a(vmClasses::BaseVirtualThread_klass());
1191 
1192   if (is_virtual && !is_JavaThread_current(java_thread, thread_oop)) {
1193     if (!is_vthread_suspended(thread_oop, java_thread)) {
1194       return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1195     }
1196     if (java_thread == nullptr) { // unmounted virtual thread
1197       return JVMTI_ERROR_OPAQUE_FRAME;
1198     }
1199   }
1200   if (err != JVMTI_ERROR_NONE) {
1201     return err;
1202   }
1203   oop e = JNIHandles::resolve_external_guard(exception);
1204   NULL_CHECK(e, JVMTI_ERROR_NULL_POINTER);
1205 
1206   JavaThread::send_async_exception(java_thread, e);
1207 
1208   return JVMTI_ERROR_NONE;
1209 
1210 } /* end StopThread */
1211 
1212 
1213 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1214 jvmtiError
1215 JvmtiEnv::InterruptThread(jthread thread) {
1216   JavaThread* current_thread  = JavaThread::current();
1217   HandleMark hm(current_thread);
1218 
1219   JvmtiVTMSTransitionDisabler disabler(thread);
1220   ThreadsListHandle tlh(current_thread);
1221 
1222   JavaThread* java_thread = nullptr;
1223   oop thread_obj = nullptr;
1224   jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
1225   if (err != JVMTI_ERROR_NONE) {
1226     return err;
1227   }
1228 
1229   if (java_lang_VirtualThread::is_instance(thread_obj)) {
1230     // For virtual threads we have to call into Java to interrupt:
1231     Handle obj(current_thread, thread_obj);
1232     JavaValue result(T_VOID);
1233     JavaCalls::call_virtual(&result,
1234                             obj,
1235                             vmClasses::Thread_klass(),
1236                             vmSymbols::interrupt_method_name(),
1237                             vmSymbols::void_method_signature(),
1238                             current_thread);
1239 
1240     return JVMTI_ERROR_NONE;
1241   }
1242 
1243   // Really this should be a Java call to Thread.interrupt to ensure the same
1244   // semantics, however historically this has not been done for some reason.
1245   // So we continue with that (which means we don't interact with any Java-level
1246   // Interruptible object) but we must set the Java-level interrupted state.
1247   java_lang_Thread::set_interrupted(thread_obj, true);
1248   java_thread->interrupt();
1249 
1250   return JVMTI_ERROR_NONE;
1251 } /* end InterruptThread */
1252 
1253 
1254 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1255 // info_ptr - pre-checked for null
1256 jvmtiError
1257 JvmtiEnv::GetThreadInfo(jthread thread, jvmtiThreadInfo* info_ptr) {
1258   JavaThread* current_thread = JavaThread::current();
1259   ResourceMark rm(current_thread);
1260   HandleMark hm(current_thread);
1261   JavaThread* java_thread = nullptr;
1262   oop thread_oop = nullptr;
1263 
1264   JvmtiVTMSTransitionDisabler disabler(thread);
1265   ThreadsListHandle tlh(current_thread);
1266 
1267   // if thread is null the current thread is used
1268   if (thread == nullptr) {
1269     java_thread = JavaThread::current();
1270     thread_oop = get_vthread_or_thread_oop(java_thread);
1271     if (thread_oop == nullptr || !thread_oop->is_a(vmClasses::Thread_klass())) {
1272       return JVMTI_ERROR_INVALID_THREAD;
1273     }
1274   } else {
1275     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1276     if (err != JVMTI_ERROR_NONE) {
1277       // We got an error code so we don't have a JavaThread *, but
1278       // only return an error from here if we didn't get a valid
1279       // thread_oop.
1280       // In the virtual thread case the cv_external_thread_to_JavaThread is expected to correctly set
1281       // the thread_oop and return JVMTI_ERROR_INVALID_THREAD which we ignore here.
1282       if (thread_oop == nullptr) {
1283         return err;
1284       }
1285     }
1286   }
1287   // We have a valid thread_oop so we can return some thread info.
1288 
1289   Handle thread_obj(current_thread, thread_oop);
1290   Handle name;
1291   ThreadPriority priority;
1292   Handle     thread_group;
1293   Handle context_class_loader;
1294   bool          is_daemon;
1295 
1296   name = Handle(current_thread, java_lang_Thread::name(thread_obj()));
1297 
1298   if (java_lang_VirtualThread::is_instance(thread_obj())) {
1299     priority = (ThreadPriority)JVMTI_THREAD_NORM_PRIORITY;
1300     is_daemon = true;
1301     if (java_lang_VirtualThread::state(thread_obj()) == java_lang_VirtualThread::TERMINATED) {
1302       thread_group = Handle(current_thread, nullptr);
1303     } else {
1304       thread_group = Handle(current_thread, java_lang_Thread_Constants::get_VTHREAD_GROUP());
1305     }
1306   } else {
1307     priority = java_lang_Thread::priority(thread_obj());
1308     is_daemon = java_lang_Thread::is_daemon(thread_obj());
1309     if (java_lang_Thread::get_thread_status(thread_obj()) == JavaThreadStatus::TERMINATED) {
1310       thread_group = Handle(current_thread, nullptr);
1311     } else {
1312       thread_group = Handle(current_thread, java_lang_Thread::threadGroup(thread_obj()));
1313     }
1314   }
1315 
1316   oop loader = java_lang_Thread::context_class_loader(thread_obj());
1317   context_class_loader = Handle(current_thread, loader);
1318 
1319   { const char *n;
1320 
1321     if (name() != nullptr) {
1322       n = java_lang_String::as_utf8_string(name());
1323     } else {
1324       int utf8_length = 0;
1325       n = UNICODE::as_utf8((jchar*) nullptr, utf8_length);
1326     }
1327 
1328     info_ptr->name = (char *) jvmtiMalloc(strlen(n)+1);
1329     if (info_ptr->name == nullptr)
1330       return JVMTI_ERROR_OUT_OF_MEMORY;
1331 
1332     strcpy(info_ptr->name, n);
1333   }
1334   info_ptr->is_daemon = is_daemon;
1335   info_ptr->priority  = priority;
1336 
1337   info_ptr->context_class_loader = (context_class_loader.is_null()) ? nullptr :
1338                                     jni_reference(context_class_loader);
1339   info_ptr->thread_group = jni_reference(thread_group);
1340 
1341   return JVMTI_ERROR_NONE;
1342 } /* end GetThreadInfo */
1343 
1344 
1345 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1346 // owned_monitor_count_ptr - pre-checked for null
1347 // owned_monitors_ptr - pre-checked for null
1348 jvmtiError
1349 JvmtiEnv::GetOwnedMonitorInfo(jthread thread, jint* owned_monitor_count_ptr, jobject** owned_monitors_ptr) {
1350   JavaThread* calling_thread = JavaThread::current();
1351   HandleMark hm(calling_thread);
1352 
1353   // growable array of jvmti monitors info on the C-heap
1354   GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list =
1355       new (mtServiceability) GrowableArray<jvmtiMonitorStackDepthInfo*>(1, mtServiceability);
1356 
1357   JvmtiVTMSTransitionDisabler disabler(thread);
1358   ThreadsListHandle tlh(calling_thread);
1359 
1360   JavaThread* java_thread = nullptr;
1361   oop thread_oop = nullptr;
1362   jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, calling_thread, &java_thread, &thread_oop);
1363   if (err != JVMTI_ERROR_NONE) {
1364     delete owned_monitors_list;
1365     return err;
1366   }
1367 
1368   Handle thread_handle(calling_thread, thread_oop);
1369   EscapeBarrier eb(java_thread != nullptr, calling_thread, java_thread);
1370   if (!eb.deoptimize_objects(MaxJavaStackTraceDepth)) {
1371     delete owned_monitors_list;
1372     return JVMTI_ERROR_OUT_OF_MEMORY;
1373   }
1374   // get owned monitors info with handshake
1375   GetOwnedMonitorInfoClosure op(this, calling_thread, owned_monitors_list);
1376   JvmtiHandshake::execute(&op, &tlh, java_thread, thread_handle);
1377   err = op.result();
1378 
1379   jint owned_monitor_count = owned_monitors_list->length();
1380   if (err == JVMTI_ERROR_NONE) {
1381     if ((err = allocate(owned_monitor_count * sizeof(jobject *),
1382                       (unsigned char**)owned_monitors_ptr)) == JVMTI_ERROR_NONE) {
1383       // copy into the returned array
1384       for (int i = 0; i < owned_monitor_count; i++) {
1385         (*owned_monitors_ptr)[i] =
1386           ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->monitor;
1387       }
1388       *owned_monitor_count_ptr = owned_monitor_count;
1389     }
1390   }
1391   // clean up.
1392   for (int i = 0; i < owned_monitor_count; i++) {
1393     deallocate((unsigned char*)owned_monitors_list->at(i));
1394   }
1395   delete owned_monitors_list;
1396 
1397   return err;
1398 } /* end GetOwnedMonitorInfo */
1399 
1400 
1401 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1402 // monitor_info_count_ptr - pre-checked for null
1403 // monitor_info_ptr - pre-checked for null
1404 jvmtiError
1405 JvmtiEnv::GetOwnedMonitorStackDepthInfo(jthread thread, jint* monitor_info_count_ptr, jvmtiMonitorStackDepthInfo** monitor_info_ptr) {
1406   JavaThread* calling_thread = JavaThread::current();
1407   HandleMark hm(calling_thread);
1408 
1409   // growable array of jvmti monitors info on the C-heap
1410   GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list =
1411          new (mtServiceability) GrowableArray<jvmtiMonitorStackDepthInfo*>(1, mtServiceability);
1412 
1413   JvmtiVTMSTransitionDisabler disabler(thread);
1414   ThreadsListHandle tlh(calling_thread);
1415 
1416   JavaThread* java_thread = nullptr;
1417   oop thread_oop = nullptr;
1418   jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, calling_thread, &java_thread, &thread_oop);
1419   if (err != JVMTI_ERROR_NONE) {
1420     delete owned_monitors_list;
1421     return err;
1422   }
1423 
1424   Handle thread_handle(calling_thread, thread_oop);
1425   EscapeBarrier eb(java_thread != nullptr, calling_thread, java_thread);
1426   if (!eb.deoptimize_objects(MaxJavaStackTraceDepth)) {
1427     delete owned_monitors_list;
1428     return JVMTI_ERROR_OUT_OF_MEMORY;
1429   }
1430   // get owned monitors info with handshake
1431   GetOwnedMonitorInfoClosure op(this, calling_thread, owned_monitors_list);
1432   JvmtiHandshake::execute(&op, &tlh, java_thread, thread_handle);
1433   err = op.result();
1434 
1435   jint owned_monitor_count = owned_monitors_list->length();
1436   if (err == JVMTI_ERROR_NONE) {
1437     if ((err = allocate(owned_monitor_count * sizeof(jvmtiMonitorStackDepthInfo),
1438                         (unsigned char**)monitor_info_ptr)) == JVMTI_ERROR_NONE) {
1439       // copy to output array.
1440       for (int i = 0; i < owned_monitor_count; i++) {
1441         (*monitor_info_ptr)[i].monitor =
1442           ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->monitor;
1443         (*monitor_info_ptr)[i].stack_depth =
1444           ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->stack_depth;
1445       }
1446     }
1447     *monitor_info_count_ptr = owned_monitor_count;
1448   }
1449 
1450   // clean up.
1451   for (int i = 0; i < owned_monitor_count; i++) {
1452     deallocate((unsigned char*)owned_monitors_list->at(i));
1453   }
1454   delete owned_monitors_list;
1455 
1456   return err;
1457 } /* end GetOwnedMonitorStackDepthInfo */
1458 
1459 
1460 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1461 // monitor_ptr - pre-checked for null
1462 jvmtiError
1463 JvmtiEnv::GetCurrentContendedMonitor(jthread thread, jobject* monitor_ptr) {
1464   JavaThread* current = JavaThread::current();
1465 
1466   *monitor_ptr = nullptr;
1467 
1468   // get contended monitor information with handshake
1469   GetCurrentContendedMonitorClosure op(this, current, monitor_ptr);
1470   JvmtiHandshake::execute(&op, thread);
1471   return op.result();
1472 } /* end GetCurrentContendedMonitor */
1473 
1474 
1475 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1476 // proc - pre-checked for null
1477 // arg - null is a valid value, must be checked
1478 jvmtiError
1479 JvmtiEnv::RunAgentThread(jthread thread, jvmtiStartFunction proc, const void* arg, jint priority) {
1480   JavaThread* current_thread = JavaThread::current();
1481 
1482   JavaThread* java_thread = nullptr;
1483   oop thread_oop = nullptr;
1484   ThreadsListHandle tlh(current_thread);
1485   jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1486   if (err != JVMTI_ERROR_NONE) {
1487     // We got an error code so we don't have a JavaThread *, but
1488     // only return an error from here if we didn't get a valid
1489     // thread_oop.
1490     if (thread_oop == nullptr) {
1491       return err;
1492     }
1493     // We have a valid thread_oop.
1494   }
1495 
1496   if (thread_oop->is_a(vmClasses::BaseVirtualThread_klass())) {
1497     // No support for virtual threads.
1498     return JVMTI_ERROR_UNSUPPORTED_OPERATION;
1499   }
1500   if (java_thread != nullptr) {
1501     // 'thread' refers to an existing JavaThread.
1502     return JVMTI_ERROR_INVALID_THREAD;
1503   }
1504 
1505   if (priority < JVMTI_THREAD_MIN_PRIORITY || priority > JVMTI_THREAD_MAX_PRIORITY) {
1506     return JVMTI_ERROR_INVALID_PRIORITY;
1507   }
1508 
1509   Handle thread_hndl(current_thread, thread_oop);
1510 
1511   JvmtiAgentThread* new_thread = new JvmtiAgentThread(this, proc, arg);
1512 
1513   // At this point it may be possible that no osthread was created for the
1514   // JavaThread due to lack of resources.
1515   if (new_thread->osthread() == nullptr) {
1516     // The new thread is not known to Thread-SMR yet so we can just delete.
1517     delete new_thread;
1518     return JVMTI_ERROR_OUT_OF_MEMORY;
1519   }
1520 
1521   JavaThread::start_internal_daemon(current_thread, new_thread, thread_hndl,
1522                                     (ThreadPriority)priority);
1523 
1524   return JVMTI_ERROR_NONE;
1525 } /* end RunAgentThread */
1526 
1527   //
1528   // Thread Group functions
1529   //
1530 
1531 // group_count_ptr - pre-checked for null
1532 // groups_ptr - pre-checked for null
1533 jvmtiError
1534 JvmtiEnv::GetTopThreadGroups(jint* group_count_ptr, jthreadGroup** groups_ptr) {
1535   JavaThread* current_thread = JavaThread::current();
1536 
1537   // Only one top level thread group now.
1538   *group_count_ptr = 1;
1539 
1540   // Allocate memory to store global-refs to the thread groups.
1541   // Assume this area is freed by caller.
1542   *groups_ptr = (jthreadGroup *) jvmtiMalloc((sizeof(jthreadGroup)) * (*group_count_ptr));
1543 
1544   NULL_CHECK(*groups_ptr, JVMTI_ERROR_OUT_OF_MEMORY);
1545 
1546   // Convert oop to Handle, then convert Handle to global-ref.
1547   {
1548     HandleMark hm(current_thread);
1549     Handle system_thread_group(current_thread, Universe::system_thread_group());
1550     *groups_ptr[0] = jni_reference(system_thread_group);
1551   }
1552 
1553   return JVMTI_ERROR_NONE;
1554 } /* end GetTopThreadGroups */
1555 
1556 
1557 // info_ptr - pre-checked for null
1558 jvmtiError
1559 JvmtiEnv::GetThreadGroupInfo(jthreadGroup group, jvmtiThreadGroupInfo* info_ptr) {
1560   Thread* current_thread = Thread::current();
1561   ResourceMark rm(current_thread);
1562   HandleMark hm(current_thread);
1563 
1564   Handle group_obj (current_thread, JNIHandles::resolve_external_guard(group));
1565   NULL_CHECK(group_obj(), JVMTI_ERROR_INVALID_THREAD_GROUP);
1566 
1567   const char* name;
1568   Handle parent_group;
1569   bool is_daemon;
1570   ThreadPriority max_priority;
1571 
1572   name         = java_lang_ThreadGroup::name(group_obj());
1573   parent_group = Handle(current_thread, java_lang_ThreadGroup::parent(group_obj()));
1574   is_daemon    = java_lang_ThreadGroup::is_daemon(group_obj());
1575   max_priority = java_lang_ThreadGroup::maxPriority(group_obj());
1576 
1577   info_ptr->is_daemon    = is_daemon;
1578   info_ptr->max_priority = max_priority;
1579   info_ptr->parent       = jni_reference(parent_group);
1580 
1581   if (name != nullptr) {
1582     info_ptr->name = (char*)jvmtiMalloc(strlen(name)+1);
1583     NULL_CHECK(info_ptr->name, JVMTI_ERROR_OUT_OF_MEMORY);
1584     strcpy(info_ptr->name, name);
1585   } else {
1586     info_ptr->name = nullptr;
1587   }
1588 
1589   return JVMTI_ERROR_NONE;
1590 } /* end GetThreadGroupInfo */
1591 
1592 // thread_count_ptr - pre-checked for null
1593 // threads_ptr - pre-checked for null
1594 // group_count_ptr - pre-checked for null
1595 // groups_ptr - pre-checked for null
1596 jvmtiError
1597 JvmtiEnv::GetThreadGroupChildren(jthreadGroup group, jint* thread_count_ptr, jthread** threads_ptr, jint* group_count_ptr, jthreadGroup** groups_ptr) {
1598   jvmtiError err;
1599   JavaThread* current_thread = JavaThread::current();
1600   oop group_obj = JNIHandles::resolve_external_guard(group);
1601   NULL_CHECK(group_obj, JVMTI_ERROR_INVALID_THREAD_GROUP);
1602 
1603   Handle *thread_objs = nullptr;
1604   objArrayHandle group_objs;
1605   jint nthreads = 0;
1606   jint ngroups = 0;
1607   int hidden_threads = 0;
1608 
1609   ResourceMark rm(current_thread);
1610   HandleMark hm(current_thread);
1611 
1612   Handle group_hdl(current_thread, group_obj);
1613 
1614   err = get_live_threads(current_thread, group_hdl, &nthreads, &thread_objs);
1615   if (err != JVMTI_ERROR_NONE) {
1616     return err;
1617   }
1618   err = get_subgroups(current_thread, group_hdl, &ngroups, &group_objs);
1619   if (err != JVMTI_ERROR_NONE) {
1620     return err;
1621   }
1622 
1623   *group_count_ptr  = ngroups;
1624   *thread_count_ptr = nthreads;
1625   *threads_ptr     = new_jthreadArray(nthreads, thread_objs);
1626   *groups_ptr      = new_jthreadGroupArray(ngroups, group_objs);
1627   if (nthreads > 0 && *threads_ptr == nullptr) {
1628     return JVMTI_ERROR_OUT_OF_MEMORY;
1629   }
1630   if (ngroups > 0 && *groups_ptr == nullptr) {
1631     return JVMTI_ERROR_OUT_OF_MEMORY;
1632   }
1633 
1634   return JVMTI_ERROR_NONE;
1635 } /* end GetThreadGroupChildren */
1636 
1637 
1638   //
1639   // Stack Frame functions
1640   //
1641 
1642 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1643 // max_frame_count - pre-checked to be greater than or equal to 0
1644 // frame_buffer - pre-checked for null
1645 // count_ptr - pre-checked for null
1646 jvmtiError
1647 JvmtiEnv::GetStackTrace(jthread thread, jint start_depth, jint max_frame_count, jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
1648   GetStackTraceClosure op(this, start_depth, max_frame_count, frame_buffer, count_ptr);
1649   JvmtiHandshake::execute(&op, thread);
1650   return op.result();
1651 } /* end GetStackTrace */
1652 
1653 
1654 // max_frame_count - pre-checked to be greater than or equal to 0
1655 // stack_info_ptr - pre-checked for null
1656 // thread_count_ptr - pre-checked for null
1657 jvmtiError
1658 JvmtiEnv::GetAllStackTraces(jint max_frame_count, jvmtiStackInfo** stack_info_ptr, jint* thread_count_ptr) {
1659   jvmtiError err = JVMTI_ERROR_NONE;
1660   JavaThread* calling_thread = JavaThread::current();
1661 
1662   // JVMTI get stack traces at safepoint.
1663   VM_GetAllStackTraces op(this, calling_thread, max_frame_count);
1664   VMThread::execute(&op);
1665   *thread_count_ptr = op.final_thread_count();
1666   *stack_info_ptr = op.stack_info();
1667   err = op.result();
1668   return err;
1669 } /* end GetAllStackTraces */
1670 
1671 
1672 // thread_count - pre-checked to be greater than or equal to 0
1673 // thread_list - pre-checked for null
1674 // max_frame_count - pre-checked to be greater than or equal to 0
1675 // stack_info_ptr - pre-checked for null
1676 jvmtiError
1677 JvmtiEnv::GetThreadListStackTraces(jint thread_count, const jthread* thread_list, jint max_frame_count, jvmtiStackInfo** stack_info_ptr) {
1678   jvmtiError err = JVMTI_ERROR_NONE;
1679 
1680   if (thread_count == 1) {
1681     // Use direct handshake if we need to get only one stack trace.
1682     JavaThread *current_thread = JavaThread::current();
1683 
1684     jthread thread = thread_list[0];
1685 
1686     GetSingleStackTraceClosure op(this, current_thread, thread, max_frame_count);
1687     JvmtiHandshake::execute(&op, thread);
1688     err = op.result();
1689     if (err == JVMTI_ERROR_NONE) {
1690       *stack_info_ptr = op.stack_info();
1691     }
1692   } else {
1693     JvmtiVTMSTransitionDisabler disabler;
1694 
1695     // JVMTI get stack traces at safepoint.
1696     VM_GetThreadListStackTraces op(this, thread_count, thread_list, max_frame_count);
1697     VMThread::execute(&op);
1698     err = op.result();
1699     if (err == JVMTI_ERROR_NONE) {
1700       *stack_info_ptr = op.stack_info();
1701     }
1702   }
1703   return err;
1704 } /* end GetThreadListStackTraces */
1705 
1706 
1707 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1708 // count_ptr - pre-checked for null
1709 jvmtiError
1710 JvmtiEnv::GetFrameCount(jthread thread, jint* count_ptr) {
1711   GetFrameCountClosure op(this, count_ptr);
1712   JvmtiHandshake::execute(&op, thread);
1713   return op.result();
1714 } /* end GetFrameCount */
1715 
1716 
1717 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1718 jvmtiError
1719 JvmtiEnv::PopFrame(jthread thread) {
1720   JavaThread* current_thread = JavaThread::current();
1721   HandleMark hm(current_thread);
1722 
1723   if (thread == nullptr) {
1724     return JVMTI_ERROR_INVALID_THREAD;
1725   }
1726   JvmtiVTMSTransitionDisabler disabler(thread);
1727   ThreadsListHandle tlh(current_thread);
1728 
1729   JavaThread* java_thread = nullptr;
1730   oop thread_obj = nullptr;
1731   jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
1732   Handle thread_handle(current_thread, thread_obj);
1733 
1734   if (err != JVMTI_ERROR_NONE) {
1735     return err;
1736   }
1737   bool self = java_thread == current_thread;
1738 
1739   err = check_non_suspended_or_opaque_frame(java_thread, thread_obj, self);
1740   if (err != JVMTI_ERROR_NONE) {
1741     return err;
1742   }
1743 
1744   // retrieve or create the state
1745   JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
1746   if (state == nullptr) {
1747     return JVMTI_ERROR_THREAD_NOT_ALIVE;
1748   }
1749 
1750   // Eagerly reallocate scalar replaced objects.
1751   EscapeBarrier eb(true, current_thread, java_thread);
1752   if (!eb.deoptimize_objects(1)) {
1753     // Reallocation of scalar replaced objects failed -> return with error
1754     return JVMTI_ERROR_OUT_OF_MEMORY;
1755   }
1756 
1757   MutexLocker mu(JvmtiThreadState_lock);
1758   UpdateForPopTopFrameClosure op(state);
1759   JvmtiHandshake::execute(&op, &tlh, java_thread, thread_handle);
1760   return op.result();
1761 } /* end PopFrame */
1762 
1763 
1764 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1765 // depth - pre-checked as non-negative
1766 // method_ptr - pre-checked for null
1767 // location_ptr - pre-checked for null
1768 jvmtiError
1769 JvmtiEnv::GetFrameLocation(jthread thread, jint depth, jmethodID* method_ptr, jlocation* location_ptr) {
1770   GetFrameLocationClosure op(this, depth, method_ptr, location_ptr);
1771   JvmtiHandshake::execute(&op, thread);
1772   return op.result();
1773 } /* end GetFrameLocation */
1774 
1775 
1776 // Threads_lock NOT held, java_thread not protected by lock
1777 // depth - pre-checked as non-negative
1778 jvmtiError
1779 JvmtiEnv::NotifyFramePop(jthread thread, jint depth) {
1780   ResourceMark rm;
1781   JvmtiVTMSTransitionDisabler disabler(thread);
1782   JavaThread* current = JavaThread::current();
1783   ThreadsListHandle tlh(current);
1784 
1785   JavaThread* java_thread = nullptr;
1786   oop thread_obj = nullptr;
1787   jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current, &java_thread, &thread_obj);
1788   if (err != JVMTI_ERROR_NONE) {
1789     return err;
1790   }
1791 
1792   HandleMark hm(current);
1793   Handle thread_handle(current, thread_obj);
1794   JvmtiThreadState *state = JvmtiThreadState::state_for(java_thread, thread_handle);
1795   if (state == nullptr) {
1796     return JVMTI_ERROR_THREAD_NOT_ALIVE;
1797   }
1798 
1799   SetFramePopClosure op(this, state, depth);
1800   MutexLocker mu(current, JvmtiThreadState_lock);
1801   JvmtiHandshake::execute(&op, &tlh, java_thread, thread_handle);
1802   return op.result();
1803 } /* end NotifyFramePop */
1804 
1805 
1806   //
1807   // Force Early Return functions
1808   //
1809 
1810 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1811 jvmtiError
1812 JvmtiEnv::ForceEarlyReturnObject(jthread thread, jobject value) {
1813   jvalue val;
1814   val.l = value;
1815   return force_early_return(thread, val, atos);
1816 } /* end ForceEarlyReturnObject */
1817 
1818 
1819 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1820 jvmtiError
1821 JvmtiEnv::ForceEarlyReturnInt(jthread thread, jint value) {
1822   jvalue val;
1823   val.i = value;
1824   return force_early_return(thread, val, itos);
1825 } /* end ForceEarlyReturnInt */
1826 
1827 
1828 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1829 jvmtiError
1830 JvmtiEnv::ForceEarlyReturnLong(jthread thread, jlong value) {
1831   jvalue val;
1832   val.j = value;
1833   return force_early_return(thread, val, ltos);
1834 } /* end ForceEarlyReturnLong */
1835 
1836 
1837 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1838 jvmtiError
1839 JvmtiEnv::ForceEarlyReturnFloat(jthread thread, jfloat value) {
1840   jvalue val;
1841   val.f = value;
1842   return force_early_return(thread, val, ftos);
1843 } /* end ForceEarlyReturnFloat */
1844 
1845 
1846 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1847 jvmtiError
1848 JvmtiEnv::ForceEarlyReturnDouble(jthread thread, jdouble value) {
1849   jvalue val;
1850   val.d = value;
1851   return force_early_return(thread, val, dtos);
1852 } /* end ForceEarlyReturnDouble */
1853 
1854 
1855 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1856 jvmtiError
1857 JvmtiEnv::ForceEarlyReturnVoid(jthread thread) {
1858   jvalue val;
1859   val.j = 0L;
1860   return force_early_return(thread, val, vtos);
1861 } /* end ForceEarlyReturnVoid */
1862 
1863 
1864   //
1865   // Heap functions
1866   //
1867 
1868 // klass - null is a valid value, must be checked
1869 // initial_object - null is a valid value, must be checked
1870 // callbacks - pre-checked for null
1871 // user_data - null is a valid value, must be checked
1872 jvmtiError
1873 JvmtiEnv::FollowReferences(jint heap_filter, jclass klass, jobject initial_object, const jvmtiHeapCallbacks* callbacks, const void* user_data) {
1874   // check klass if provided
1875   Klass* k = nullptr;
1876   if (klass != nullptr) {
1877     oop k_mirror = JNIHandles::resolve_external_guard(klass);
1878     if (k_mirror == nullptr) {
1879       return JVMTI_ERROR_INVALID_CLASS;
1880     }
1881     if (java_lang_Class::is_primitive(k_mirror)) {
1882       return JVMTI_ERROR_NONE;
1883     }
1884     k = java_lang_Class::as_Klass(k_mirror);
1885     if (klass == nullptr) {
1886       return JVMTI_ERROR_INVALID_CLASS;
1887     }
1888   }
1889 
1890   if (initial_object != nullptr) {
1891     oop init_obj = JNIHandles::resolve_external_guard(initial_object);
1892     if (init_obj == nullptr) {
1893       return JVMTI_ERROR_INVALID_OBJECT;
1894     }
1895   }
1896 
1897   Thread *thread = Thread::current();
1898   HandleMark hm(thread);
1899 
1900   TraceTime t("FollowReferences", TRACETIME_LOG(Debug, jvmti, objecttagging));
1901   JvmtiTagMap::tag_map_for(this)->follow_references(heap_filter, k, initial_object, callbacks, user_data);
1902   return JVMTI_ERROR_NONE;
1903 } /* end FollowReferences */
1904 
1905 
1906 // klass - null is a valid value, must be checked
1907 // callbacks - pre-checked for null
1908 // user_data - null is a valid value, must be checked
1909 jvmtiError
1910 JvmtiEnv::IterateThroughHeap(jint heap_filter, jclass klass, const jvmtiHeapCallbacks* callbacks, const void* user_data) {
1911   // check klass if provided
1912   Klass* k = nullptr;
1913   if (klass != nullptr) {
1914     oop k_mirror = JNIHandles::resolve_external_guard(klass);
1915     if (k_mirror == nullptr) {
1916       return JVMTI_ERROR_INVALID_CLASS;
1917     }
1918     if (java_lang_Class::is_primitive(k_mirror)) {
1919       return JVMTI_ERROR_NONE;
1920     }
1921     k = java_lang_Class::as_Klass(k_mirror);
1922     if (k == nullptr) {
1923       return JVMTI_ERROR_INVALID_CLASS;
1924     }
1925   }
1926 
1927   TraceTime t("IterateThroughHeap", TRACETIME_LOG(Debug, jvmti, objecttagging));
1928   JvmtiTagMap::tag_map_for(this)->iterate_through_heap(heap_filter, k, callbacks, user_data);
1929   return JVMTI_ERROR_NONE;
1930 } /* end IterateThroughHeap */
1931 
1932 
1933 // tag_ptr - pre-checked for null
1934 jvmtiError
1935 JvmtiEnv::GetTag(jobject object, jlong* tag_ptr) {
1936   oop o = JNIHandles::resolve_external_guard(object);
1937   NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1938   *tag_ptr = JvmtiTagMap::tag_map_for(this)->get_tag(object);
1939   return JVMTI_ERROR_NONE;
1940 } /* end GetTag */
1941 
1942 
1943 jvmtiError
1944 JvmtiEnv::SetTag(jobject object, jlong tag) {
1945   oop o = JNIHandles::resolve_external_guard(object);
1946   NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1947   JvmtiTagMap::tag_map_for(this)->set_tag(object, tag);
1948   return JVMTI_ERROR_NONE;
1949 } /* end SetTag */
1950 
1951 
1952 // tag_count - pre-checked to be greater than or equal to 0
1953 // tags - pre-checked for null
1954 // count_ptr - pre-checked for null
1955 // object_result_ptr - null is a valid value, must be checked
1956 // tag_result_ptr - null is a valid value, must be checked
1957 jvmtiError
1958 JvmtiEnv::GetObjectsWithTags(jint tag_count, const jlong* tags, jint* count_ptr, jobject** object_result_ptr, jlong** tag_result_ptr) {
1959   TraceTime t("GetObjectsWithTags", TRACETIME_LOG(Debug, jvmti, objecttagging));
1960   return JvmtiTagMap::tag_map_for(this)->get_objects_with_tags((jlong*)tags, tag_count, count_ptr, object_result_ptr, tag_result_ptr);
1961 } /* end GetObjectsWithTags */
1962 
1963 
1964 jvmtiError
1965 JvmtiEnv::ForceGarbageCollection() {
1966   Universe::heap()->collect(GCCause::_jvmti_force_gc);
1967   return JVMTI_ERROR_NONE;
1968 } /* end ForceGarbageCollection */
1969 
1970 
1971   //
1972   // Heap (1.0) functions
1973   //
1974 
1975 // object_reference_callback - pre-checked for null
1976 // user_data - null is a valid value, must be checked
1977 jvmtiError
1978 JvmtiEnv::IterateOverObjectsReachableFromObject(jobject object, jvmtiObjectReferenceCallback object_reference_callback, const void* user_data) {
1979   oop o = JNIHandles::resolve_external_guard(object);
1980   NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1981   JvmtiTagMap::tag_map_for(this)->iterate_over_objects_reachable_from_object(object, object_reference_callback, user_data);
1982   return JVMTI_ERROR_NONE;
1983 } /* end IterateOverObjectsReachableFromObject */
1984 
1985 
1986 // heap_root_callback - null is a valid value, must be checked
1987 // stack_ref_callback - null is a valid value, must be checked
1988 // object_ref_callback - null is a valid value, must be checked
1989 // user_data - null is a valid value, must be checked
1990 jvmtiError
1991 JvmtiEnv::IterateOverReachableObjects(jvmtiHeapRootCallback heap_root_callback, jvmtiStackReferenceCallback stack_ref_callback, jvmtiObjectReferenceCallback object_ref_callback, const void* user_data) {
1992   TraceTime t("IterateOverReachableObjects", TRACETIME_LOG(Debug, jvmti, objecttagging));
1993   JvmtiTagMap::tag_map_for(this)->iterate_over_reachable_objects(heap_root_callback, stack_ref_callback, object_ref_callback, user_data);
1994   return JVMTI_ERROR_NONE;
1995 } /* end IterateOverReachableObjects */
1996 
1997 
1998 // heap_object_callback - pre-checked for null
1999 // user_data - null is a valid value, must be checked
2000 jvmtiError
2001 JvmtiEnv::IterateOverHeap(jvmtiHeapObjectFilter object_filter, jvmtiHeapObjectCallback heap_object_callback, const void* user_data) {
2002   TraceTime t("IterateOverHeap", TRACETIME_LOG(Debug, jvmti, objecttagging));
2003   Thread *thread = Thread::current();
2004   HandleMark hm(thread);
2005   JvmtiTagMap::tag_map_for(this)->iterate_over_heap(object_filter, nullptr, heap_object_callback, user_data);
2006   return JVMTI_ERROR_NONE;
2007 } /* end IterateOverHeap */
2008 
2009 
2010 // k_mirror - may be primitive, this must be checked
2011 // heap_object_callback - pre-checked for null
2012 // user_data - null is a valid value, must be checked
2013 jvmtiError
2014 JvmtiEnv::IterateOverInstancesOfClass(oop k_mirror, jvmtiHeapObjectFilter object_filter, jvmtiHeapObjectCallback heap_object_callback, const void* user_data) {
2015   if (java_lang_Class::is_primitive(k_mirror)) {
2016     // DO PRIMITIVE CLASS PROCESSING
2017     return JVMTI_ERROR_NONE;
2018   }
2019   Klass* klass = java_lang_Class::as_Klass(k_mirror);
2020   if (klass == nullptr) {
2021     return JVMTI_ERROR_INVALID_CLASS;
2022   }
2023   TraceTime t("IterateOverInstancesOfClass", TRACETIME_LOG(Debug, jvmti, objecttagging));
2024   JvmtiTagMap::tag_map_for(this)->iterate_over_heap(object_filter, klass, heap_object_callback, user_data);
2025   return JVMTI_ERROR_NONE;
2026 } /* end IterateOverInstancesOfClass */
2027 
2028 
2029   //
2030   // Local Variable functions
2031   //
2032 
2033 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
2034 // depth - pre-checked as non-negative
2035 // value_ptr - pre-checked for null
2036 jvmtiError
2037 JvmtiEnv::GetLocalObject(jthread thread, jint depth, jint slot, jobject* value_ptr) {
2038   JavaThread* current_thread = JavaThread::current();
2039   // rm object is created to clean up the javaVFrame created in
2040   // doit_prologue(), but after doit() is finished with it.
2041   ResourceMark rm(current_thread);
2042   HandleMark hm(current_thread);
2043   JvmtiVTMSTransitionDisabler disabler(thread);
2044   ThreadsListHandle tlh(current_thread);
2045 
2046   JavaThread* java_thread = nullptr;
2047   oop thread_obj = nullptr;
2048   jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2049   if (err != JVMTI_ERROR_NONE) {
2050     return err;
2051   }
2052   bool self = is_JavaThread_current(java_thread, thread_obj);
2053 
2054   if (java_lang_VirtualThread::is_instance(thread_obj)) {
2055     VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2056                                      current_thread, depth, slot, self);
2057     VMThread::execute(&op);
2058     err = op.result();
2059     if (err == JVMTI_ERROR_NONE) {
2060       *value_ptr = op.value().l;
2061     }
2062   } else {
2063     // Support for ordinary threads
2064     VM_GetOrSetLocal op(java_thread, current_thread, depth, slot, self);
2065     VMThread::execute(&op);
2066     err = op.result();
2067     if (err == JVMTI_ERROR_NONE) {
2068       *value_ptr = op.value().l;
2069     }
2070   }
2071   return err;
2072 } /* end GetLocalObject */
2073 
2074 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
2075 // depth - pre-checked as non-negative
2076 // value - pre-checked for null
2077 jvmtiError
2078 JvmtiEnv::GetLocalInstance(jthread thread, jint depth, jobject* value_ptr){
2079   JavaThread* current_thread = JavaThread::current();
2080   // rm object is created to clean up the javaVFrame created in
2081   // doit_prologue(), but after doit() is finished with it.
2082   ResourceMark rm(current_thread);
2083   HandleMark hm(current_thread);
2084   JvmtiVTMSTransitionDisabler disabler(thread);
2085   ThreadsListHandle tlh(current_thread);
2086 
2087   JavaThread* java_thread = nullptr;
2088   oop thread_obj = nullptr;
2089   jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2090   if (err != JVMTI_ERROR_NONE) {
2091     return err;
2092   }
2093   bool self = is_JavaThread_current(java_thread, thread_obj);
2094 
2095   if (java_lang_VirtualThread::is_instance(thread_obj)) {
2096     VM_VirtualThreadGetReceiver op(this, Handle(current_thread, thread_obj),
2097                                    current_thread, depth, self);
2098     VMThread::execute(&op);
2099     err = op.result();
2100     if (err == JVMTI_ERROR_NONE) {
2101       *value_ptr = op.value().l;
2102     }
2103   } else {
2104     // Support for ordinary threads
2105     VM_GetReceiver op(java_thread, current_thread, depth, self);
2106     VMThread::execute(&op);
2107     err = op.result();
2108     if (err == JVMTI_ERROR_NONE) {
2109       *value_ptr = op.value().l;
2110     }
2111   }
2112   return err;
2113 } /* end GetLocalInstance */
2114 
2115 
2116 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
2117 // depth - pre-checked as non-negative
2118 // value_ptr - pre-checked for null
2119 jvmtiError
2120 JvmtiEnv::GetLocalInt(jthread thread, jint depth, jint slot, jint* value_ptr) {
2121   JavaThread* current_thread = JavaThread::current();
2122   // rm object is created to clean up the javaVFrame created in
2123   // doit_prologue(), but after doit() is finished with it.
2124   ResourceMark rm(current_thread);
2125   HandleMark hm(current_thread);
2126   JvmtiVTMSTransitionDisabler disabler(thread);
2127   ThreadsListHandle tlh(current_thread);
2128 
2129   JavaThread* java_thread = nullptr;
2130   oop thread_obj = nullptr;
2131   jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2132   if (err != JVMTI_ERROR_NONE) {
2133     return err;
2134   }
2135   bool self = is_JavaThread_current(java_thread, thread_obj);
2136 
2137   if (java_lang_VirtualThread::is_instance(thread_obj)) {
2138     VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2139                                      depth, slot, T_INT, self);
2140     VMThread::execute(&op);
2141     err = op.result();
2142     if (err == JVMTI_ERROR_NONE) {
2143       *value_ptr = op.value().i;
2144     }
2145   } else {
2146     // Support for ordinary threads
2147     VM_GetOrSetLocal op(java_thread, depth, slot, T_INT, self);
2148     VMThread::execute(&op);
2149     err = op.result();
2150     if (err == JVMTI_ERROR_NONE) {
2151       *value_ptr = op.value().i;
2152     }
2153   }
2154   return err;
2155 } /* end GetLocalInt */
2156 
2157 
2158 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
2159 // depth - pre-checked as non-negative
2160 // value_ptr - pre-checked for null
2161 jvmtiError
2162 JvmtiEnv::GetLocalLong(jthread thread, jint depth, jint slot, jlong* value_ptr) {
2163   JavaThread* current_thread = JavaThread::current();
2164   // rm object is created to clean up the javaVFrame created in
2165   // doit_prologue(), but after doit() is finished with it.
2166   ResourceMark rm(current_thread);
2167   HandleMark hm(current_thread);
2168   JvmtiVTMSTransitionDisabler disabler(thread);
2169   ThreadsListHandle tlh(current_thread);
2170 
2171   JavaThread* java_thread = nullptr;
2172   oop thread_obj = nullptr;
2173   jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2174   if (err != JVMTI_ERROR_NONE) {
2175     return err;
2176   }
2177   bool self = is_JavaThread_current(java_thread, thread_obj);
2178 
2179   if (java_lang_VirtualThread::is_instance(thread_obj)) {
2180     VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2181                                      depth, slot, T_LONG, self);
2182     VMThread::execute(&op);
2183     err = op.result();
2184     if (err == JVMTI_ERROR_NONE) {
2185       *value_ptr = op.value().j;
2186     }
2187   } else {
2188     // Support for ordinary threads
2189     VM_GetOrSetLocal op(java_thread, depth, slot, T_LONG, self);
2190     VMThread::execute(&op);
2191     err = op.result();
2192     if (err == JVMTI_ERROR_NONE) {
2193       *value_ptr = op.value().j;
2194     }
2195   }
2196   return err;
2197 } /* end GetLocalLong */
2198 
2199 
2200 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
2201 // depth - pre-checked as non-negative
2202 // value_ptr - pre-checked for null
2203 jvmtiError
2204 JvmtiEnv::GetLocalFloat(jthread thread, jint depth, jint slot, jfloat* value_ptr) {
2205   JavaThread* current_thread = JavaThread::current();
2206   // rm object is created to clean up the javaVFrame created in
2207   // doit_prologue(), but after doit() is finished with it.
2208   ResourceMark rm(current_thread);
2209   HandleMark hm(current_thread);
2210   JvmtiVTMSTransitionDisabler disabler(thread);
2211   ThreadsListHandle tlh(current_thread);
2212 
2213   JavaThread* java_thread = nullptr;
2214   oop thread_obj = nullptr;
2215   jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2216   if (err != JVMTI_ERROR_NONE) {
2217     return err;
2218   }
2219   bool self = is_JavaThread_current(java_thread, thread_obj);
2220 
2221   if (java_lang_VirtualThread::is_instance(thread_obj)) {
2222     VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2223                                      depth, slot, T_FLOAT, self);
2224     VMThread::execute(&op);
2225     err = op.result();
2226     if (err == JVMTI_ERROR_NONE) {
2227       *value_ptr = op.value().f;
2228     }
2229   } else {
2230     // Support for ordinary threads
2231     VM_GetOrSetLocal op(java_thread, depth, slot, T_FLOAT, self);
2232     VMThread::execute(&op);
2233     err = op.result();
2234     if (err == JVMTI_ERROR_NONE) {
2235       *value_ptr = op.value().f;
2236     }
2237   }
2238   return err;
2239 } /* end GetLocalFloat */
2240 
2241 
2242 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
2243 // depth - pre-checked as non-negative
2244 // value_ptr - pre-checked for null
2245 jvmtiError
2246 JvmtiEnv::GetLocalDouble(jthread thread, jint depth, jint slot, jdouble* value_ptr) {
2247   JavaThread* current_thread = JavaThread::current();
2248   // rm object is created to clean up the javaVFrame created in
2249   // doit_prologue(), but after doit() is finished with it.
2250   ResourceMark rm(current_thread);
2251   HandleMark hm(current_thread);
2252   JvmtiVTMSTransitionDisabler disabler(thread);
2253   ThreadsListHandle tlh(current_thread);
2254 
2255   JavaThread* java_thread = nullptr;
2256   oop thread_obj = nullptr;
2257   jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2258   if (err != JVMTI_ERROR_NONE) {
2259     return err;
2260   }
2261   bool self = is_JavaThread_current(java_thread, thread_obj);
2262 
2263   if (java_lang_VirtualThread::is_instance(thread_obj)) {
2264     VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2265                                      depth, slot, T_DOUBLE, self);
2266     VMThread::execute(&op);
2267     err = op.result();
2268     if (err == JVMTI_ERROR_NONE) {
2269       *value_ptr = op.value().d;
2270     }
2271   } else {
2272     // Support for ordinary threads
2273     VM_GetOrSetLocal op(java_thread, depth, slot, T_DOUBLE, self);
2274     VMThread::execute(&op);
2275     err = op.result();
2276     if (err == JVMTI_ERROR_NONE) {
2277       *value_ptr = op.value().d;
2278     }
2279   }
2280   return err;
2281 } /* end GetLocalDouble */
2282 
2283 
2284 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
2285 // depth - pre-checked as non-negative
2286 jvmtiError
2287 JvmtiEnv::SetLocalObject(jthread thread, jint depth, jint slot, jobject value) {
2288   JavaThread* current_thread = JavaThread::current();
2289   // rm object is created to clean up the javaVFrame created in
2290   // doit_prologue(), but after doit() is finished with it.
2291   ResourceMark rm(current_thread);
2292   HandleMark hm(current_thread);
2293   JvmtiVTMSTransitionDisabler disabler(thread);
2294   ThreadsListHandle tlh(current_thread);
2295 
2296   JavaThread* java_thread = nullptr;
2297   oop thread_obj = nullptr;
2298   jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2299   if (err != JVMTI_ERROR_NONE) {
2300     return err;
2301   }
2302   bool self = is_JavaThread_current(java_thread, thread_obj);
2303   jvalue val;
2304   val.l = value;
2305 
2306   if (java_lang_VirtualThread::is_instance(thread_obj)) {
2307     VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2308                                      depth, slot, T_OBJECT, val, self);
2309     VMThread::execute(&op);
2310     err = op.result();
2311   } else {
2312     // Support for ordinary threads
2313     VM_GetOrSetLocal op(java_thread, depth, slot, T_OBJECT, val, self);
2314     VMThread::execute(&op);
2315     err = op.result();
2316   }
2317   return err;
2318 } /* end SetLocalObject */
2319 
2320 
2321 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
2322 // depth - pre-checked as non-negative
2323 jvmtiError
2324 JvmtiEnv::SetLocalInt(jthread thread, jint depth, jint slot, jint value) {
2325   JavaThread* current_thread = JavaThread::current();
2326   // rm object is created to clean up the javaVFrame created in
2327   // doit_prologue(), but after doit() is finished with it.
2328   ResourceMark rm(current_thread);
2329   HandleMark hm(current_thread);
2330   JvmtiVTMSTransitionDisabler disabler(thread);
2331   ThreadsListHandle tlh(current_thread);
2332 
2333   JavaThread* java_thread = nullptr;
2334   oop thread_obj = nullptr;
2335   jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2336   if (err != JVMTI_ERROR_NONE) {
2337     return err;
2338   }
2339   bool self = is_JavaThread_current(java_thread, thread_obj);
2340   jvalue val;
2341   val.i = value;
2342 
2343   if (java_lang_VirtualThread::is_instance(thread_obj)) {
2344     VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2345                                      depth, slot, T_INT, val, self);
2346     VMThread::execute(&op);
2347     err = op.result();
2348   } else {
2349     // Support for ordinary threads
2350     VM_GetOrSetLocal op(java_thread, depth, slot, T_INT, val, self);
2351     VMThread::execute(&op);
2352     err = op.result();
2353   }
2354   return err;
2355 } /* end SetLocalInt */
2356 
2357 
2358 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
2359 // depth - pre-checked as non-negative
2360 jvmtiError
2361 JvmtiEnv::SetLocalLong(jthread thread, jint depth, jint slot, jlong value) {
2362   JavaThread* current_thread = JavaThread::current();
2363   // rm object is created to clean up the javaVFrame created in
2364   // doit_prologue(), but after doit() is finished with it.
2365   ResourceMark rm(current_thread);
2366   HandleMark hm(current_thread);
2367   JvmtiVTMSTransitionDisabler disabler(thread);
2368   ThreadsListHandle tlh(current_thread);
2369 
2370   JavaThread* java_thread = nullptr;
2371   oop thread_obj = nullptr;
2372   jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2373   if (err != JVMTI_ERROR_NONE) {
2374     return err;
2375   }
2376   bool self = is_JavaThread_current(java_thread, thread_obj);
2377   jvalue val;
2378   val.j = value;
2379 
2380   if (java_lang_VirtualThread::is_instance(thread_obj)) {
2381     VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2382                                      depth, slot, T_LONG, val, self);
2383     VMThread::execute(&op);
2384     err = op.result();
2385   } else {
2386     // Support for ordinary threads
2387     VM_GetOrSetLocal op(java_thread, depth, slot, T_LONG, val, self);
2388     VMThread::execute(&op);
2389     err = op.result();
2390   }
2391   return err;
2392 } /* end SetLocalLong */
2393 
2394 
2395 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
2396 // depth - pre-checked as non-negative
2397 jvmtiError
2398 JvmtiEnv::SetLocalFloat(jthread thread, jint depth, jint slot, jfloat value) {
2399   JavaThread* current_thread = JavaThread::current();
2400   // rm object is created to clean up the javaVFrame created in
2401   // doit_prologue(), but after doit() is finished with it.
2402   ResourceMark rm(current_thread);
2403   HandleMark hm(current_thread);
2404   JvmtiVTMSTransitionDisabler disabler(thread);
2405   ThreadsListHandle tlh(current_thread);
2406 
2407   JavaThread* java_thread = nullptr;
2408   oop thread_obj = nullptr;
2409   jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2410   if (err != JVMTI_ERROR_NONE) {
2411     return err;
2412   }
2413   bool self = is_JavaThread_current(java_thread, thread_obj);
2414   jvalue val;
2415   val.f = value;
2416 
2417   if (java_lang_VirtualThread::is_instance(thread_obj)) {
2418     VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2419                                      depth, slot, T_FLOAT, val, self);
2420     VMThread::execute(&op);
2421     err = op.result();
2422   } else {
2423     // Support for ordinary threads
2424     VM_GetOrSetLocal op(java_thread, depth, slot, T_FLOAT, val, self);
2425     VMThread::execute(&op);
2426     err = op.result();
2427   }
2428   return err;
2429 } /* end SetLocalFloat */
2430 
2431 
2432 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
2433 // depth - pre-checked as non-negative
2434 jvmtiError
2435 JvmtiEnv::SetLocalDouble(jthread thread, jint depth, jint slot, jdouble value) {
2436   JavaThread* current_thread = JavaThread::current();
2437   // rm object is created to clean up the javaVFrame created in
2438   // doit_prologue(), but after doit() is finished with it.
2439   ResourceMark rm(current_thread);
2440   HandleMark hm(current_thread);
2441   JvmtiVTMSTransitionDisabler disabler(thread);
2442   ThreadsListHandle tlh(current_thread);
2443 
2444   JavaThread* java_thread = nullptr;
2445   oop thread_obj = nullptr;
2446   jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2447   if (err != JVMTI_ERROR_NONE) {
2448     return err;
2449   }
2450   bool self = is_JavaThread_current(java_thread, thread_obj);
2451   jvalue val;
2452   val.d = value;
2453 
2454   if (java_lang_VirtualThread::is_instance(thread_obj)) {
2455     VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2456                                      depth, slot, T_DOUBLE, val, self);
2457     VMThread::execute(&op);
2458     err = op.result();
2459   } else {
2460     // Support for ordinary threads
2461     VM_GetOrSetLocal op(java_thread, depth, slot, T_DOUBLE, val, self);
2462     VMThread::execute(&op);
2463     err = op.result();
2464   }
2465   return err;
2466 } /* end SetLocalDouble */
2467 
2468 
2469   //
2470   // Breakpoint functions
2471   //
2472 
2473 // method - pre-checked for validity, but may be null meaning obsolete method
2474 jvmtiError
2475 JvmtiEnv::SetBreakpoint(Method* method, jlocation location) {
2476   NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
2477   if (location < 0) {   // simple invalid location check first
2478     return JVMTI_ERROR_INVALID_LOCATION;
2479   }
2480   // verify that the breakpoint is not past the end of the method
2481   if (location >= (jlocation) method->code_size()) {
2482     return JVMTI_ERROR_INVALID_LOCATION;
2483   }
2484 
2485   ResourceMark rm;
2486   JvmtiBreakpoint bp(method, location);
2487   JvmtiBreakpoints& jvmti_breakpoints = JvmtiCurrentBreakpoints::get_jvmti_breakpoints();
2488   if (jvmti_breakpoints.set(bp) == JVMTI_ERROR_DUPLICATE)
2489     return JVMTI_ERROR_DUPLICATE;
2490 
2491   if (TraceJVMTICalls) {
2492     jvmti_breakpoints.print();
2493   }
2494 
2495   return JVMTI_ERROR_NONE;
2496 } /* end SetBreakpoint */
2497 
2498 
2499 // method - pre-checked for validity, but may be null meaning obsolete method
2500 jvmtiError
2501 JvmtiEnv::ClearBreakpoint(Method* method, jlocation location) {
2502   NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
2503 
2504   if (location < 0) {   // simple invalid location check first
2505     return JVMTI_ERROR_INVALID_LOCATION;
2506   }
2507 
2508   // verify that the breakpoint is not past the end of the method
2509   if (location >= (jlocation) method->code_size()) {
2510     return JVMTI_ERROR_INVALID_LOCATION;
2511   }
2512 
2513   JvmtiBreakpoint bp(method, location);
2514 
2515   JvmtiBreakpoints& jvmti_breakpoints = JvmtiCurrentBreakpoints::get_jvmti_breakpoints();
2516   if (jvmti_breakpoints.clear(bp) == JVMTI_ERROR_NOT_FOUND)
2517     return JVMTI_ERROR_NOT_FOUND;
2518 
2519   if (TraceJVMTICalls) {
2520     jvmti_breakpoints.print();
2521   }
2522 
2523   return JVMTI_ERROR_NONE;
2524 } /* end ClearBreakpoint */
2525 
2526 
2527   //
2528   // Watched Field functions
2529   //
2530 
2531 jvmtiError
2532 JvmtiEnv::SetFieldAccessWatch(fieldDescriptor* fdesc_ptr) {
2533   // make sure we haven't set this watch before
2534   if (fdesc_ptr->is_field_access_watched()) return JVMTI_ERROR_DUPLICATE;
2535   fdesc_ptr->set_is_field_access_watched(true);
2536 
2537   JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_ACCESS, true);
2538 
2539   return JVMTI_ERROR_NONE;
2540 } /* end SetFieldAccessWatch */
2541 
2542 
2543 jvmtiError
2544 JvmtiEnv::ClearFieldAccessWatch(fieldDescriptor* fdesc_ptr) {
2545   // make sure we have a watch to clear
2546   if (!fdesc_ptr->is_field_access_watched()) return JVMTI_ERROR_NOT_FOUND;
2547   fdesc_ptr->set_is_field_access_watched(false);
2548 
2549   JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_ACCESS, false);
2550 
2551   return JVMTI_ERROR_NONE;
2552 } /* end ClearFieldAccessWatch */
2553 
2554 
2555 jvmtiError
2556 JvmtiEnv::SetFieldModificationWatch(fieldDescriptor* fdesc_ptr) {
2557   // make sure we haven't set this watch before
2558   if (fdesc_ptr->is_field_modification_watched()) return JVMTI_ERROR_DUPLICATE;
2559   fdesc_ptr->set_is_field_modification_watched(true);
2560 
2561   JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_MODIFICATION, true);
2562 
2563   return JVMTI_ERROR_NONE;
2564 } /* end SetFieldModificationWatch */
2565 
2566 
2567 jvmtiError
2568 JvmtiEnv::ClearFieldModificationWatch(fieldDescriptor* fdesc_ptr) {
2569    // make sure we have a watch to clear
2570   if (!fdesc_ptr->is_field_modification_watched()) return JVMTI_ERROR_NOT_FOUND;
2571   fdesc_ptr->set_is_field_modification_watched(false);
2572 
2573   JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_MODIFICATION, false);
2574 
2575   return JVMTI_ERROR_NONE;
2576 } /* end ClearFieldModificationWatch */
2577 
2578   //
2579   // Class functions
2580   //
2581 
2582 
2583 // k_mirror - may be primitive, this must be checked
2584 // signature_ptr - null is a valid value, must be checked
2585 // generic_ptr - null is a valid value, must be checked
2586 jvmtiError
2587 JvmtiEnv::GetClassSignature(oop k_mirror, char** signature_ptr, char** generic_ptr) {
2588   ResourceMark rm;
2589   bool isPrimitive = java_lang_Class::is_primitive(k_mirror);
2590   Klass* k = nullptr;
2591   if (!isPrimitive) {
2592     k = java_lang_Class::as_Klass(k_mirror);
2593     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2594   }
2595   if (signature_ptr != nullptr) {
2596     char* result = nullptr;
2597     if (isPrimitive) {
2598       char tchar = type2char(java_lang_Class::primitive_type(k_mirror));
2599       result = (char*) jvmtiMalloc(2);
2600       result[0] = tchar;
2601       result[1] = '\0';
2602     } else {
2603       const char* class_sig = k->signature_name();
2604       result = (char *) jvmtiMalloc(strlen(class_sig)+1);
2605       strcpy(result, class_sig);
2606     }
2607     *signature_ptr = result;
2608   }
2609   if (generic_ptr != nullptr) {
2610     *generic_ptr = nullptr;
2611     if (!isPrimitive && k->is_instance_klass()) {
2612       Symbol* soo = InstanceKlass::cast(k)->generic_signature();
2613       if (soo != nullptr) {
2614         const char *gen_sig = soo->as_C_string();
2615         if (gen_sig != nullptr) {
2616           char* gen_result;
2617           jvmtiError err = allocate(strlen(gen_sig) + 1,
2618                                     (unsigned char **)&gen_result);
2619           if (err != JVMTI_ERROR_NONE) {
2620             return err;
2621           }
2622           strcpy(gen_result, gen_sig);
2623           *generic_ptr = gen_result;
2624         }
2625       }
2626     }
2627   }
2628   return JVMTI_ERROR_NONE;
2629 } /* end GetClassSignature */
2630 
2631 
2632 // k_mirror - may be primitive, this must be checked
2633 // status_ptr - pre-checked for null
2634 jvmtiError
2635 JvmtiEnv::GetClassStatus(oop k_mirror, jint* status_ptr) {
2636   jint result = 0;
2637   if (java_lang_Class::is_primitive(k_mirror)) {
2638     result |= JVMTI_CLASS_STATUS_PRIMITIVE;
2639   } else {
2640     Klass* k = java_lang_Class::as_Klass(k_mirror);
2641     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2642     result = k->jvmti_class_status();
2643   }
2644   *status_ptr = result;
2645 
2646   return JVMTI_ERROR_NONE;
2647 } /* end GetClassStatus */
2648 
2649 
2650 // k_mirror - may be primitive, this must be checked
2651 // source_name_ptr - pre-checked for null
2652 jvmtiError
2653 JvmtiEnv::GetSourceFileName(oop k_mirror, char** source_name_ptr) {
2654   if (java_lang_Class::is_primitive(k_mirror)) {
2655      return JVMTI_ERROR_ABSENT_INFORMATION;
2656   }
2657   Klass* k_klass = java_lang_Class::as_Klass(k_mirror);
2658   NULL_CHECK(k_klass, JVMTI_ERROR_INVALID_CLASS);
2659 
2660   if (!k_klass->is_instance_klass()) {
2661     return JVMTI_ERROR_ABSENT_INFORMATION;
2662   }
2663 
2664   Symbol* sfnOop = InstanceKlass::cast(k_klass)->source_file_name();
2665   NULL_CHECK(sfnOop, JVMTI_ERROR_ABSENT_INFORMATION);
2666   {
2667     JavaThread* current_thread  = JavaThread::current();
2668     ResourceMark rm(current_thread);
2669     const char* sfncp = (const char*) sfnOop->as_C_string();
2670     *source_name_ptr = (char *) jvmtiMalloc(strlen(sfncp)+1);
2671     strcpy(*source_name_ptr, sfncp);
2672   }
2673 
2674   return JVMTI_ERROR_NONE;
2675 } /* end GetSourceFileName */
2676 
2677 
2678 // k_mirror - may be primitive, this must be checked
2679 // modifiers_ptr - pre-checked for null
2680 jvmtiError
2681 JvmtiEnv::GetClassModifiers(oop k_mirror, jint* modifiers_ptr) {
2682   JavaThread* current_thread  = JavaThread::current();
2683   jint result = 0;
2684   if (!java_lang_Class::is_primitive(k_mirror)) {
2685     Klass* k = java_lang_Class::as_Klass(k_mirror);
2686     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2687     result = k->compute_modifier_flags();
2688 
2689     // Reset the deleted  ACC_SUPER bit (deleted in compute_modifier_flags()).
2690     if (k->is_super()) {
2691       result |= JVM_ACC_SUPER;
2692     }
2693   } else {
2694     result = (JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC);
2695   }
2696   *modifiers_ptr = result;
2697 
2698   return JVMTI_ERROR_NONE;
2699 } /* end GetClassModifiers */
2700 
2701 
2702 // k_mirror - may be primitive, this must be checked
2703 // method_count_ptr - pre-checked for null
2704 // methods_ptr - pre-checked for null
2705 jvmtiError
2706 JvmtiEnv::GetClassMethods(oop k_mirror, jint* method_count_ptr, jmethodID** methods_ptr) {
2707   JavaThread* current_thread  = JavaThread::current();
2708   HandleMark hm(current_thread);
2709 
2710   if (java_lang_Class::is_primitive(k_mirror)) {
2711     *method_count_ptr = 0;
2712     *methods_ptr = (jmethodID*) jvmtiMalloc(0 * sizeof(jmethodID));
2713     return JVMTI_ERROR_NONE;
2714   }
2715   Klass* k = java_lang_Class::as_Klass(k_mirror);
2716   NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2717 
2718   // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2719   if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) {
2720     return JVMTI_ERROR_CLASS_NOT_PREPARED;
2721   }
2722 
2723   if (!k->is_instance_klass()) {
2724     *method_count_ptr = 0;
2725     *methods_ptr = (jmethodID*) jvmtiMalloc(0 * sizeof(jmethodID));
2726     return JVMTI_ERROR_NONE;
2727   }
2728   InstanceKlass* ik = InstanceKlass::cast(k);
2729   // Allocate the result and fill it in
2730   int result_length = ik->methods()->length();
2731   jmethodID* result_list = (jmethodID*)jvmtiMalloc(result_length * sizeof(jmethodID));
2732   int index;
2733   bool jmethodids_found = true;
2734   int skipped = 0;  // skip overpass methods
2735 
2736   for (index = 0; index < result_length; index++) {
2737     Method* m = ik->methods()->at(index);
2738     // Depending on can_maintain_original_method_order capability use the original
2739     // method ordering indices stored in the class, so we can emit jmethodIDs in
2740     // the order they appeared in the class file or just copy in current order.
2741     int result_index = JvmtiExport::can_maintain_original_method_order() ? ik->method_ordering()->at(index) : index;
2742     assert(result_index >= 0 && result_index < result_length, "invalid original method index");
2743     if (m->is_overpass()) {
2744       result_list[result_index] = nullptr;
2745       skipped++;
2746       continue;
2747     }
2748     jmethodID id;
2749     if (jmethodids_found) {
2750       id = m->find_jmethod_id_or_null();
2751       if (id == nullptr) {
2752         // If we find an uninitialized value, make sure there is
2753         // enough space for all the uninitialized values we might
2754         // find.
2755         ik->ensure_space_for_methodids(index);
2756         jmethodids_found = false;
2757         id = m->jmethod_id();
2758       }
2759     } else {
2760       id = m->jmethod_id();
2761     }
2762     result_list[result_index] = id;
2763   }
2764 
2765   // Fill in return value.
2766   if (skipped > 0) {
2767     // copy results skipping null methodIDs
2768     *methods_ptr = (jmethodID*)jvmtiMalloc((result_length - skipped) * sizeof(jmethodID));
2769     *method_count_ptr = result_length - skipped;
2770     for (index = 0, skipped = 0; index < result_length; index++) {
2771       if (result_list[index] == nullptr) {
2772         skipped++;
2773       } else {
2774         (*methods_ptr)[index - skipped] = result_list[index];
2775       }
2776     }
2777     deallocate((unsigned char *)result_list);
2778   } else {
2779     *method_count_ptr = result_length;
2780     *methods_ptr = result_list;
2781   }
2782 
2783   return JVMTI_ERROR_NONE;
2784 } /* end GetClassMethods */
2785 
2786 
2787 // k_mirror - may be primitive, this must be checked
2788 // field_count_ptr - pre-checked for null
2789 // fields_ptr - pre-checked for null
2790 jvmtiError
2791 JvmtiEnv::GetClassFields(oop k_mirror, jint* field_count_ptr, jfieldID** fields_ptr) {
2792   if (java_lang_Class::is_primitive(k_mirror)) {
2793     *field_count_ptr = 0;
2794     *fields_ptr = (jfieldID*) jvmtiMalloc(0 * sizeof(jfieldID));
2795     return JVMTI_ERROR_NONE;
2796   }
2797   JavaThread* current_thread = JavaThread::current();
2798   HandleMark hm(current_thread);
2799   Klass* k = java_lang_Class::as_Klass(k_mirror);
2800   NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2801 
2802   // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2803   if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) {
2804     return JVMTI_ERROR_CLASS_NOT_PREPARED;
2805   }
2806 
2807   if (!k->is_instance_klass()) {
2808     *field_count_ptr = 0;
2809     *fields_ptr = (jfieldID*) jvmtiMalloc(0 * sizeof(jfieldID));
2810     return JVMTI_ERROR_NONE;
2811   }
2812 
2813   InstanceKlass* ik = InstanceKlass::cast(k);
2814 
2815   FilteredJavaFieldStream flds(ik);
2816 
2817   int result_count = flds.field_count();
2818 
2819   // Allocate the result and fill it in.
2820   jfieldID* result_list = (jfieldID*)jvmtiMalloc(result_count * sizeof(jfieldID));
2821   for (int i = 0; i < result_count; i++, flds.next()) {
2822     result_list[i] = jfieldIDWorkaround::to_jfieldID(ik, flds.offset(),
2823                                                      flds.access_flags().is_static());
2824   }
2825   assert(flds.done(), "just checking");
2826 
2827   // Fill in the results
2828   *field_count_ptr = result_count;
2829   *fields_ptr = result_list;
2830 
2831   return JVMTI_ERROR_NONE;
2832 } /* end GetClassFields */
2833 
2834 
2835 // k_mirror - may be primitive, this must be checked
2836 // interface_count_ptr - pre-checked for null
2837 // interfaces_ptr - pre-checked for null
2838 jvmtiError
2839 JvmtiEnv::GetImplementedInterfaces(oop k_mirror, jint* interface_count_ptr, jclass** interfaces_ptr) {
2840   {
2841     if (java_lang_Class::is_primitive(k_mirror)) {
2842       *interface_count_ptr = 0;
2843       *interfaces_ptr = (jclass*) jvmtiMalloc(0 * sizeof(jclass));
2844       return JVMTI_ERROR_NONE;
2845     }
2846     JavaThread* current_thread = JavaThread::current();
2847     HandleMark hm(current_thread);
2848     Klass* k = java_lang_Class::as_Klass(k_mirror);
2849     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2850 
2851     // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2852     if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) ))
2853       return JVMTI_ERROR_CLASS_NOT_PREPARED;
2854 
2855     if (!k->is_instance_klass()) {
2856       *interface_count_ptr = 0;
2857       *interfaces_ptr = (jclass*) jvmtiMalloc(0 * sizeof(jclass));
2858       return JVMTI_ERROR_NONE;
2859     }
2860 
2861     Array<InstanceKlass*>* interface_list = InstanceKlass::cast(k)->local_interfaces();
2862     const int result_length = (interface_list == nullptr ? 0 : interface_list->length());
2863     jclass* result_list = (jclass*) jvmtiMalloc(result_length * sizeof(jclass));
2864     for (int i_index = 0; i_index < result_length; i_index += 1) {
2865       InstanceKlass* klass_at = interface_list->at(i_index);
2866       assert(klass_at->is_klass(), "interfaces must be Klass*s");
2867       assert(klass_at->is_interface(), "interfaces must be interfaces");
2868       oop mirror_at = klass_at->java_mirror();
2869       Handle handle_at = Handle(current_thread, mirror_at);
2870       result_list[i_index] = (jclass) jni_reference(handle_at);
2871     }
2872     *interface_count_ptr = result_length;
2873     *interfaces_ptr = result_list;
2874   }
2875 
2876   return JVMTI_ERROR_NONE;
2877 } /* end GetImplementedInterfaces */
2878 
2879 
2880 // k_mirror - may be primitive, this must be checked
2881 // minor_version_ptr - pre-checked for null
2882 // major_version_ptr - pre-checked for null
2883 jvmtiError
2884 JvmtiEnv::GetClassVersionNumbers(oop k_mirror, jint* minor_version_ptr, jint* major_version_ptr) {
2885   if (java_lang_Class::is_primitive(k_mirror)) {
2886     return JVMTI_ERROR_ABSENT_INFORMATION;
2887   }
2888   Klass* klass = java_lang_Class::as_Klass(k_mirror);
2889 
2890   jint status = klass->jvmti_class_status();
2891   if (status & (JVMTI_CLASS_STATUS_ERROR)) {
2892     return JVMTI_ERROR_INVALID_CLASS;
2893   }
2894   if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
2895     return JVMTI_ERROR_ABSENT_INFORMATION;
2896   }
2897 
2898   InstanceKlass* ik = InstanceKlass::cast(klass);
2899   *minor_version_ptr = ik->minor_version();
2900   *major_version_ptr = ik->major_version();
2901 
2902   return JVMTI_ERROR_NONE;
2903 } /* end GetClassVersionNumbers */
2904 
2905 
2906 // k_mirror - may be primitive, this must be checked
2907 // constant_pool_count_ptr - pre-checked for null
2908 // constant_pool_byte_count_ptr - pre-checked for null
2909 // constant_pool_bytes_ptr - pre-checked for null
2910 jvmtiError
2911 JvmtiEnv::GetConstantPool(oop k_mirror, jint* constant_pool_count_ptr, jint* constant_pool_byte_count_ptr, unsigned char** constant_pool_bytes_ptr) {
2912   if (java_lang_Class::is_primitive(k_mirror)) {
2913     return JVMTI_ERROR_ABSENT_INFORMATION;
2914   }
2915 
2916   Klass* klass = java_lang_Class::as_Klass(k_mirror);
2917   Thread *thread = Thread::current();
2918   ResourceMark rm(thread);
2919 
2920   jint status = klass->jvmti_class_status();
2921   if (status & (JVMTI_CLASS_STATUS_ERROR)) {
2922     return JVMTI_ERROR_INVALID_CLASS;
2923   }
2924   if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
2925     return JVMTI_ERROR_ABSENT_INFORMATION;
2926   }
2927 
2928   InstanceKlass* ik = InstanceKlass::cast(klass);
2929   JvmtiConstantPoolReconstituter reconstituter(ik);
2930   if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2931     return reconstituter.get_error();
2932   }
2933 
2934   unsigned char *cpool_bytes;
2935   int cpool_size = reconstituter.cpool_size();
2936   if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2937     return reconstituter.get_error();
2938   }
2939   jvmtiError res = allocate(cpool_size, &cpool_bytes);
2940   if (res != JVMTI_ERROR_NONE) {
2941     return res;
2942   }
2943   reconstituter.copy_cpool_bytes(cpool_bytes);
2944   if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2945     return reconstituter.get_error();
2946   }
2947 
2948   constantPoolHandle  constants(thread, ik->constants());
2949   *constant_pool_count_ptr      = constants->length();
2950   *constant_pool_byte_count_ptr = cpool_size;
2951   *constant_pool_bytes_ptr      = cpool_bytes;
2952 
2953   return JVMTI_ERROR_NONE;
2954 } /* end GetConstantPool */
2955 
2956 
2957 // k_mirror - may be primitive, this must be checked
2958 // is_interface_ptr - pre-checked for null
2959 jvmtiError
2960 JvmtiEnv::IsInterface(oop k_mirror, jboolean* is_interface_ptr) {
2961   {
2962     bool result = false;
2963     if (!java_lang_Class::is_primitive(k_mirror)) {
2964       Klass* k = java_lang_Class::as_Klass(k_mirror);
2965       if (k != nullptr && k->is_interface()) {
2966         result = true;
2967       }
2968     }
2969     *is_interface_ptr = result;
2970   }
2971 
2972   return JVMTI_ERROR_NONE;
2973 } /* end IsInterface */
2974 
2975 
2976 // k_mirror - may be primitive, this must be checked
2977 // is_array_class_ptr - pre-checked for null
2978 jvmtiError
2979 JvmtiEnv::IsArrayClass(oop k_mirror, jboolean* is_array_class_ptr) {
2980   {
2981     bool result = false;
2982     if (!java_lang_Class::is_primitive(k_mirror)) {
2983       Klass* k = java_lang_Class::as_Klass(k_mirror);
2984       if (k != nullptr && k->is_array_klass()) {
2985         result = true;
2986       }
2987     }
2988     *is_array_class_ptr = result;
2989   }
2990 
2991   return JVMTI_ERROR_NONE;
2992 } /* end IsArrayClass */
2993 
2994 
2995 // k_mirror - may be primitive, this must be checked
2996 // classloader_ptr - pre-checked for null
2997 jvmtiError
2998 JvmtiEnv::GetClassLoader(oop k_mirror, jobject* classloader_ptr) {
2999   {
3000     if (java_lang_Class::is_primitive(k_mirror)) {
3001       *classloader_ptr = (jclass) jni_reference(Handle());
3002       return JVMTI_ERROR_NONE;
3003     }
3004     JavaThread* current_thread = JavaThread::current();
3005     HandleMark hm(current_thread);
3006     Klass* k = java_lang_Class::as_Klass(k_mirror);
3007     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
3008 
3009     oop result_oop = k->class_loader();
3010     if (result_oop == nullptr) {
3011       *classloader_ptr = (jclass) jni_reference(Handle());
3012       return JVMTI_ERROR_NONE;
3013     }
3014     Handle result_handle = Handle(current_thread, result_oop);
3015     jclass result_jnihandle = (jclass) jni_reference(result_handle);
3016     *classloader_ptr = result_jnihandle;
3017   }
3018   return JVMTI_ERROR_NONE;
3019 } /* end GetClassLoader */
3020 
3021 
3022 // k_mirror - may be primitive, this must be checked
3023 // source_debug_extension_ptr - pre-checked for null
3024 jvmtiError
3025 JvmtiEnv::GetSourceDebugExtension(oop k_mirror, char** source_debug_extension_ptr) {
3026   {
3027     if (java_lang_Class::is_primitive(k_mirror)) {
3028       return JVMTI_ERROR_ABSENT_INFORMATION;
3029     }
3030     Klass* k = java_lang_Class::as_Klass(k_mirror);
3031     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
3032     if (!k->is_instance_klass()) {
3033       return JVMTI_ERROR_ABSENT_INFORMATION;
3034     }
3035     const char* sde = InstanceKlass::cast(k)->source_debug_extension();
3036     NULL_CHECK(sde, JVMTI_ERROR_ABSENT_INFORMATION);
3037 
3038     {
3039       *source_debug_extension_ptr = (char *) jvmtiMalloc(strlen(sde)+1);
3040       strcpy(*source_debug_extension_ptr, sde);
3041     }
3042   }
3043 
3044   return JVMTI_ERROR_NONE;
3045 } /* end GetSourceDebugExtension */
3046 
3047   //
3048   // Object functions
3049   //
3050 
3051 // hash_code_ptr - pre-checked for null
3052 jvmtiError
3053 JvmtiEnv::GetObjectHashCode(jobject object, jint* hash_code_ptr) {
3054   oop mirror = JNIHandles::resolve_external_guard(object);
3055   NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
3056   NULL_CHECK(hash_code_ptr, JVMTI_ERROR_NULL_POINTER);
3057 
3058   {
3059     jint result = (jint) mirror->identity_hash();
3060     *hash_code_ptr = result;
3061   }
3062   return JVMTI_ERROR_NONE;
3063 } /* end GetObjectHashCode */
3064 
3065 
3066 // info_ptr - pre-checked for null
3067 jvmtiError
3068 JvmtiEnv::GetObjectMonitorUsage(jobject object, jvmtiMonitorUsage* info_ptr) {
3069   // This needs to be performed at a safepoint to gather stable data
3070   // because monitor owner / waiters might not be suspended.
3071   VM_GetObjectMonitorUsage op(this, JavaThread::current(), object, info_ptr);
3072   VMThread::execute(&op);
3073   return op.result();
3074 } /* end GetObjectMonitorUsage */
3075 
3076 
3077   //
3078   // Field functions
3079   //
3080 
3081 // name_ptr - null is a valid value, must be checked
3082 // signature_ptr - null is a valid value, must be checked
3083 // generic_ptr - null is a valid value, must be checked
3084 jvmtiError
3085 JvmtiEnv::GetFieldName(fieldDescriptor* fdesc_ptr, char** name_ptr, char** signature_ptr, char** generic_ptr) {
3086   JavaThread* current_thread  = JavaThread::current();
3087   ResourceMark rm(current_thread);
3088   if (name_ptr == nullptr) {
3089     // just don't return the name
3090   } else {
3091     const char* fieldName = fdesc_ptr->name()->as_C_string();
3092     *name_ptr =  (char*) jvmtiMalloc(strlen(fieldName) + 1);
3093     if (*name_ptr == nullptr)
3094       return JVMTI_ERROR_OUT_OF_MEMORY;
3095     strcpy(*name_ptr, fieldName);
3096   }
3097   if (signature_ptr== nullptr) {
3098     // just don't return the signature
3099   } else {
3100     const char* fieldSignature = fdesc_ptr->signature()->as_C_string();
3101     *signature_ptr = (char*) jvmtiMalloc(strlen(fieldSignature) + 1);
3102     if (*signature_ptr == nullptr)
3103       return JVMTI_ERROR_OUT_OF_MEMORY;
3104     strcpy(*signature_ptr, fieldSignature);
3105   }
3106   if (generic_ptr != nullptr) {
3107     *generic_ptr = nullptr;
3108     Symbol* soop = fdesc_ptr->generic_signature();
3109     if (soop != nullptr) {
3110       const char* gen_sig = soop->as_C_string();
3111       if (gen_sig != nullptr) {
3112         jvmtiError err = allocate(strlen(gen_sig) + 1, (unsigned char **)generic_ptr);
3113         if (err != JVMTI_ERROR_NONE) {
3114           return err;
3115         }
3116         strcpy(*generic_ptr, gen_sig);
3117       }
3118     }
3119   }
3120   return JVMTI_ERROR_NONE;
3121 } /* end GetFieldName */
3122 
3123 
3124 // declaring_class_ptr - pre-checked for null
3125 jvmtiError
3126 JvmtiEnv::GetFieldDeclaringClass(fieldDescriptor* fdesc_ptr, jclass* declaring_class_ptr) {
3127 
3128   *declaring_class_ptr = get_jni_class_non_null(fdesc_ptr->field_holder());
3129   return JVMTI_ERROR_NONE;
3130 } /* end GetFieldDeclaringClass */
3131 
3132 
3133 // modifiers_ptr - pre-checked for null
3134 jvmtiError
3135 JvmtiEnv::GetFieldModifiers(fieldDescriptor* fdesc_ptr, jint* modifiers_ptr) {
3136 
3137   AccessFlags resultFlags = fdesc_ptr->access_flags();
3138   jint result = resultFlags.as_int();
3139   *modifiers_ptr = result;
3140 
3141   return JVMTI_ERROR_NONE;
3142 } /* end GetFieldModifiers */
3143 
3144 
3145 // is_synthetic_ptr - pre-checked for null
3146 jvmtiError
3147 JvmtiEnv::IsFieldSynthetic(fieldDescriptor* fdesc_ptr, jboolean* is_synthetic_ptr) {
3148   *is_synthetic_ptr = fdesc_ptr->is_synthetic();
3149   return JVMTI_ERROR_NONE;
3150 } /* end IsFieldSynthetic */
3151 
3152 
3153   //
3154   // Method functions
3155   //
3156 
3157 // method - pre-checked for validity, but may be null meaning obsolete method
3158 // name_ptr - null is a valid value, must be checked
3159 // signature_ptr - null is a valid value, must be checked
3160 // generic_ptr - null is a valid value, must be checked
3161 jvmtiError
3162 JvmtiEnv::GetMethodName(Method* method, char** name_ptr, char** signature_ptr, char** generic_ptr) {
3163   NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3164   JavaThread* current_thread  = JavaThread::current();
3165 
3166   ResourceMark rm(current_thread); // get the utf8 name and signature
3167   if (name_ptr == nullptr) {
3168     // just don't return the name
3169   } else {
3170     const char* utf8_name = (const char *) method->name()->as_utf8();
3171     *name_ptr = (char *) jvmtiMalloc(strlen(utf8_name)+1);
3172     strcpy(*name_ptr, utf8_name);
3173   }
3174   if (signature_ptr == nullptr) {
3175     // just don't return the signature
3176   } else {
3177     const char* utf8_signature = (const char *) method->signature()->as_utf8();
3178     *signature_ptr = (char *) jvmtiMalloc(strlen(utf8_signature) + 1);
3179     strcpy(*signature_ptr, utf8_signature);
3180   }
3181 
3182   if (generic_ptr != nullptr) {
3183     *generic_ptr = nullptr;
3184     Symbol* soop = method->generic_signature();
3185     if (soop != nullptr) {
3186       const char* gen_sig = soop->as_C_string();
3187       if (gen_sig != nullptr) {
3188         jvmtiError err = allocate(strlen(gen_sig) + 1, (unsigned char **)generic_ptr);
3189         if (err != JVMTI_ERROR_NONE) {
3190           return err;
3191         }
3192         strcpy(*generic_ptr, gen_sig);
3193       }
3194     }
3195   }
3196   return JVMTI_ERROR_NONE;
3197 } /* end GetMethodName */
3198 
3199 
3200 // method - pre-checked for validity, but may be null meaning obsolete method
3201 // declaring_class_ptr - pre-checked for null
3202 jvmtiError
3203 JvmtiEnv::GetMethodDeclaringClass(Method* method, jclass* declaring_class_ptr) {
3204   NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3205   (*declaring_class_ptr) = get_jni_class_non_null(method->method_holder());
3206   return JVMTI_ERROR_NONE;
3207 } /* end GetMethodDeclaringClass */
3208 
3209 
3210 // method - pre-checked for validity, but may be null meaning obsolete method
3211 // modifiers_ptr - pre-checked for null
3212 jvmtiError
3213 JvmtiEnv::GetMethodModifiers(Method* method, jint* modifiers_ptr) {
3214   NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3215   (*modifiers_ptr) = method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
3216   return JVMTI_ERROR_NONE;
3217 } /* end GetMethodModifiers */
3218 
3219 
3220 // method - pre-checked for validity, but may be null meaning obsolete method
3221 // max_ptr - pre-checked for null
3222 jvmtiError
3223 JvmtiEnv::GetMaxLocals(Method* method, jint* max_ptr) {
3224   NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3225   // get max stack
3226   (*max_ptr) = method->max_locals();
3227   return JVMTI_ERROR_NONE;
3228 } /* end GetMaxLocals */
3229 
3230 
3231 // method - pre-checked for validity, but may be null meaning obsolete method
3232 // size_ptr - pre-checked for null
3233 jvmtiError
3234 JvmtiEnv::GetArgumentsSize(Method* method, jint* size_ptr) {
3235   NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3236   // get size of arguments
3237 
3238   (*size_ptr) = method->size_of_parameters();
3239   return JVMTI_ERROR_NONE;
3240 } /* end GetArgumentsSize */
3241 
3242 
3243 // method - pre-checked for validity, but may be null meaning obsolete method
3244 // entry_count_ptr - pre-checked for null
3245 // table_ptr - pre-checked for null
3246 jvmtiError
3247 JvmtiEnv::GetLineNumberTable(Method* method, jint* entry_count_ptr, jvmtiLineNumberEntry** table_ptr) {
3248   NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3249   if (!method->has_linenumber_table()) {
3250     return (JVMTI_ERROR_ABSENT_INFORMATION);
3251   }
3252 
3253   // The line number table is compressed so we don't know how big it is until decompressed.
3254   // Decompression is really fast so we just do it twice.
3255 
3256   // Compute size of table
3257   jint num_entries = 0;
3258   CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
3259   while (stream.read_pair()) {
3260     num_entries++;
3261   }
3262   jvmtiLineNumberEntry *jvmti_table =
3263             (jvmtiLineNumberEntry *)jvmtiMalloc(num_entries * (sizeof(jvmtiLineNumberEntry)));
3264 
3265   // Fill jvmti table
3266   if (num_entries > 0) {
3267     int index = 0;
3268     CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
3269     while (stream.read_pair()) {
3270       jvmti_table[index].start_location = (jlocation) stream.bci();
3271       jvmti_table[index].line_number = (jint) stream.line();
3272       index++;
3273     }
3274     assert(index == num_entries, "sanity check");
3275   }
3276 
3277   // Set up results
3278   (*entry_count_ptr) = num_entries;
3279   (*table_ptr) = jvmti_table;
3280 
3281   return JVMTI_ERROR_NONE;
3282 } /* end GetLineNumberTable */
3283 
3284 
3285 // method - pre-checked for validity, but may be null meaning obsolete method
3286 // start_location_ptr - pre-checked for null
3287 // end_location_ptr - pre-checked for null
3288 jvmtiError
3289 JvmtiEnv::GetMethodLocation(Method* method, jlocation* start_location_ptr, jlocation* end_location_ptr) {
3290 
3291   NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3292   // get start and end location
3293   (*end_location_ptr) = (jlocation) (method->code_size() - 1);
3294   if (method->code_size() == 0) {
3295     // there is no code so there is no start location
3296     (*start_location_ptr) = (jlocation)(-1);
3297   } else {
3298     (*start_location_ptr) = (jlocation)(0);
3299   }
3300 
3301   return JVMTI_ERROR_NONE;
3302 } /* end GetMethodLocation */
3303 
3304 
3305 // method - pre-checked for validity, but may be null meaning obsolete method
3306 // entry_count_ptr - pre-checked for null
3307 // table_ptr - pre-checked for null
3308 jvmtiError
3309 JvmtiEnv::GetLocalVariableTable(Method* method, jint* entry_count_ptr, jvmtiLocalVariableEntry** table_ptr) {
3310 
3311   NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3312   JavaThread* current_thread  = JavaThread::current();
3313 
3314   // does the klass have any local variable information?
3315   InstanceKlass* ik = method->method_holder();
3316   if (!ik->has_localvariable_table()) {
3317     return (JVMTI_ERROR_ABSENT_INFORMATION);
3318   }
3319 
3320   ConstantPool* constants = method->constants();
3321   NULL_CHECK(constants, JVMTI_ERROR_ABSENT_INFORMATION);
3322 
3323   // in the vm localvariable table representation, 6 consecutive elements in the table
3324   // represent a 6-tuple of shorts
3325   // [start_pc, length, name_index, descriptor_index, signature_index, index]
3326   jint num_entries = method->localvariable_table_length();
3327   jvmtiLocalVariableEntry *jvmti_table = (jvmtiLocalVariableEntry *)
3328                 jvmtiMalloc(num_entries * (sizeof(jvmtiLocalVariableEntry)));
3329 
3330   if (num_entries > 0) {
3331     LocalVariableTableElement* table = method->localvariable_table_start();
3332     for (int i = 0; i < num_entries; i++) {
3333       // get the 5 tuple information from the vm table
3334       jlocation start_location = (jlocation) table[i].start_bci;
3335       jint length = (jint) table[i].length;
3336       int name_index = (int) table[i].name_cp_index;
3337       int signature_index = (int) table[i].descriptor_cp_index;
3338       int generic_signature_index = (int) table[i].signature_cp_index;
3339       jint slot = (jint) table[i].slot;
3340 
3341       // get utf8 name and signature
3342       char *name_buf = nullptr;
3343       char *sig_buf = nullptr;
3344       char *gen_sig_buf = nullptr;
3345       {
3346         ResourceMark rm(current_thread);
3347 
3348         const char *utf8_name = (const char *) constants->symbol_at(name_index)->as_utf8();
3349         name_buf = (char *) jvmtiMalloc(strlen(utf8_name)+1);
3350         strcpy(name_buf, utf8_name);
3351 
3352         const char *utf8_signature = (const char *) constants->symbol_at(signature_index)->as_utf8();
3353         sig_buf = (char *) jvmtiMalloc(strlen(utf8_signature)+1);
3354         strcpy(sig_buf, utf8_signature);
3355 
3356         if (generic_signature_index > 0) {
3357           const char *utf8_gen_sign = (const char *)
3358                                        constants->symbol_at(generic_signature_index)->as_utf8();
3359           gen_sig_buf = (char *) jvmtiMalloc(strlen(utf8_gen_sign)+1);
3360           strcpy(gen_sig_buf, utf8_gen_sign);
3361         }
3362       }
3363 
3364       // fill in the jvmti local variable table
3365       jvmti_table[i].start_location = start_location;
3366       jvmti_table[i].length = length;
3367       jvmti_table[i].name = name_buf;
3368       jvmti_table[i].signature = sig_buf;
3369       jvmti_table[i].generic_signature = gen_sig_buf;
3370       jvmti_table[i].slot = slot;
3371     }
3372   }
3373 
3374   // set results
3375   (*entry_count_ptr) = num_entries;
3376   (*table_ptr) = jvmti_table;
3377 
3378   return JVMTI_ERROR_NONE;
3379 } /* end GetLocalVariableTable */
3380 
3381 
3382 // method - pre-checked for validity, but may be null meaning obsolete method
3383 // bytecode_count_ptr - pre-checked for null
3384 // bytecodes_ptr - pre-checked for null
3385 jvmtiError
3386 JvmtiEnv::GetBytecodes(Method* method, jint* bytecode_count_ptr, unsigned char** bytecodes_ptr) {
3387   NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3388 
3389   methodHandle mh(Thread::current(), method);
3390   jint size = (jint)mh->code_size();
3391   jvmtiError err = allocate(size, bytecodes_ptr);
3392   if (err != JVMTI_ERROR_NONE) {
3393     return err;
3394   }
3395 
3396   (*bytecode_count_ptr) = size;
3397   // get byte codes
3398   JvmtiClassFileReconstituter::copy_bytecodes(mh, *bytecodes_ptr);
3399 
3400   return JVMTI_ERROR_NONE;
3401 } /* end GetBytecodes */
3402 
3403 
3404 // method - pre-checked for validity, but may be null meaning obsolete method
3405 // is_native_ptr - pre-checked for null
3406 jvmtiError
3407 JvmtiEnv::IsMethodNative(Method* method, jboolean* is_native_ptr) {
3408   NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3409   (*is_native_ptr) = method->is_native();
3410   return JVMTI_ERROR_NONE;
3411 } /* end IsMethodNative */
3412 
3413 
3414 // method - pre-checked for validity, but may be null meaning obsolete method
3415 // is_synthetic_ptr - pre-checked for null
3416 jvmtiError
3417 JvmtiEnv::IsMethodSynthetic(Method* method, jboolean* is_synthetic_ptr) {
3418   NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3419   (*is_synthetic_ptr) = method->is_synthetic();
3420   return JVMTI_ERROR_NONE;
3421 } /* end IsMethodSynthetic */
3422 
3423 
3424 // method - pre-checked for validity, but may be null meaning obsolete method
3425 // is_obsolete_ptr - pre-checked for null
3426 jvmtiError
3427 JvmtiEnv::IsMethodObsolete(Method* method, jboolean* is_obsolete_ptr) {
3428   if (use_version_1_0_semantics() &&
3429       get_capabilities()->can_redefine_classes == 0) {
3430     // This JvmtiEnv requested version 1.0 semantics and this function
3431     // requires the can_redefine_classes capability in version 1.0 so
3432     // we need to return an error here.
3433     return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
3434   }
3435 
3436   if (method == nullptr || method->is_obsolete()) {
3437     *is_obsolete_ptr = true;
3438   } else {
3439     *is_obsolete_ptr = false;
3440   }
3441   return JVMTI_ERROR_NONE;
3442 } /* end IsMethodObsolete */
3443 
3444   //
3445   // Raw Monitor functions
3446   //
3447 
3448 // name - pre-checked for null
3449 // monitor_ptr - pre-checked for null
3450 jvmtiError
3451 JvmtiEnv::CreateRawMonitor(const char* name, jrawMonitorID* monitor_ptr) {
3452   JvmtiRawMonitor* rmonitor = new (std::nothrow) JvmtiRawMonitor(name);
3453   NULL_CHECK(rmonitor, JVMTI_ERROR_OUT_OF_MEMORY);
3454 
3455   *monitor_ptr = (jrawMonitorID)rmonitor;
3456 
3457   return JVMTI_ERROR_NONE;
3458 } /* end CreateRawMonitor */
3459 
3460 
3461 // rmonitor - pre-checked for validity
3462 jvmtiError
3463 JvmtiEnv::DestroyRawMonitor(JvmtiRawMonitor * rmonitor) {
3464   if (Threads::number_of_threads() == 0) {
3465     // Remove this monitor from pending raw monitors list
3466     // if it has entered in onload or start phase.
3467     JvmtiPendingMonitors::destroy(rmonitor);
3468   } else {
3469     Thread* thread  = Thread::current();
3470     if (rmonitor->owner() == thread) {
3471       // The caller owns this monitor which we are about to destroy.
3472       // We exit the underlying synchronization object so that the
3473       // "delete monitor" call below can work without an assertion
3474       // failure on systems that don't like destroying synchronization
3475       // objects that are locked.
3476       int r;
3477       int recursion = rmonitor->recursions();
3478       for (int i = 0; i <= recursion; i++) {
3479         r = rmonitor->raw_exit(thread);
3480         assert(r == JvmtiRawMonitor::M_OK, "raw_exit should have worked");
3481         if (r != JvmtiRawMonitor::M_OK) {  // robustness
3482           return JVMTI_ERROR_INTERNAL;
3483         }
3484       }
3485     }
3486     if (rmonitor->owner() != nullptr) {
3487       // The caller is trying to destroy a monitor that is locked by
3488       // someone else. While this is not forbidden by the JVMTI
3489       // spec, it will cause an assertion failure on systems that don't
3490       // like destroying synchronization objects that are locked.
3491       // We indicate a problem with the error return (and leak the
3492       // monitor's memory).
3493       return JVMTI_ERROR_NOT_MONITOR_OWNER;
3494     }
3495   }
3496 
3497   delete rmonitor;
3498 
3499   return JVMTI_ERROR_NONE;
3500 } /* end DestroyRawMonitor */
3501 
3502 
3503 // rmonitor - pre-checked for validity
3504 jvmtiError
3505 JvmtiEnv::RawMonitorEnter(JvmtiRawMonitor * rmonitor) {
3506   if (Threads::number_of_threads() == 0) {
3507     // No JavaThreads exist so JvmtiRawMonitor enter cannot be
3508     // used, add this raw monitor to the pending list.
3509     // The pending monitors will be actually entered when
3510     // the VM is setup.
3511     // See transition_pending_raw_monitors in create_vm()
3512     // in thread.cpp.
3513     JvmtiPendingMonitors::enter(rmonitor);
3514   } else {
3515     Thread* thread = Thread::current();
3516     // 8266889: raw_enter changes Java thread state, needs WXWrite
3517     MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread));
3518     rmonitor->raw_enter(thread);
3519   }
3520   return JVMTI_ERROR_NONE;
3521 } /* end RawMonitorEnter */
3522 
3523 
3524 // rmonitor - pre-checked for validity
3525 jvmtiError
3526 JvmtiEnv::RawMonitorExit(JvmtiRawMonitor * rmonitor) {
3527   jvmtiError err = JVMTI_ERROR_NONE;
3528 
3529   if (Threads::number_of_threads() == 0) {
3530     // No JavaThreads exist so just remove this monitor from the pending list.
3531     // Bool value from exit is false if rmonitor is not in the list.
3532     if (!JvmtiPendingMonitors::exit(rmonitor)) {
3533       err = JVMTI_ERROR_NOT_MONITOR_OWNER;
3534     }
3535   } else {
3536     Thread* thread = Thread::current();
3537     int r = rmonitor->raw_exit(thread);
3538     if (r == JvmtiRawMonitor::M_ILLEGAL_MONITOR_STATE) {
3539       err = JVMTI_ERROR_NOT_MONITOR_OWNER;
3540     }
3541   }
3542   return err;
3543 } /* end RawMonitorExit */
3544 
3545 
3546 // rmonitor - pre-checked for validity
3547 jvmtiError
3548 JvmtiEnv::RawMonitorWait(JvmtiRawMonitor * rmonitor, jlong millis) {
3549   Thread* thread = Thread::current();
3550   // 8266889: raw_wait changes Java thread state, needs WXWrite
3551   MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread));
3552   int r = rmonitor->raw_wait(millis, thread);
3553 
3554   switch (r) {
3555   case JvmtiRawMonitor::M_INTERRUPTED:
3556     return JVMTI_ERROR_INTERRUPT;
3557   case JvmtiRawMonitor::M_ILLEGAL_MONITOR_STATE:
3558     return JVMTI_ERROR_NOT_MONITOR_OWNER;
3559   default:
3560     return JVMTI_ERROR_NONE;
3561   }
3562 } /* end RawMonitorWait */
3563 
3564 
3565 // rmonitor - pre-checked for validity
3566 jvmtiError
3567 JvmtiEnv::RawMonitorNotify(JvmtiRawMonitor * rmonitor) {
3568   Thread* thread = Thread::current();
3569   int r = rmonitor->raw_notify(thread);
3570 
3571   if (r == JvmtiRawMonitor::M_ILLEGAL_MONITOR_STATE) {
3572     return JVMTI_ERROR_NOT_MONITOR_OWNER;
3573   }
3574   return JVMTI_ERROR_NONE;
3575 } /* end RawMonitorNotify */
3576 
3577 
3578 // rmonitor - pre-checked for validity
3579 jvmtiError
3580 JvmtiEnv::RawMonitorNotifyAll(JvmtiRawMonitor * rmonitor) {
3581   Thread* thread = Thread::current();
3582   int r = rmonitor->raw_notifyAll(thread);
3583 
3584   if (r == JvmtiRawMonitor::M_ILLEGAL_MONITOR_STATE) {
3585     return JVMTI_ERROR_NOT_MONITOR_OWNER;
3586   }
3587   return JVMTI_ERROR_NONE;
3588 } /* end RawMonitorNotifyAll */
3589 
3590 
3591   //
3592   // JNI Function Interception functions
3593   //
3594 
3595 
3596 // function_table - pre-checked for null
3597 jvmtiError
3598 JvmtiEnv::SetJNIFunctionTable(const jniNativeInterface* function_table) {
3599   // Copy jni function table at safepoint.
3600   VM_JNIFunctionTableCopier copier(function_table);
3601   VMThread::execute(&copier);
3602 
3603   return JVMTI_ERROR_NONE;
3604 } /* end SetJNIFunctionTable */
3605 
3606 
3607 // function_table - pre-checked for null
3608 jvmtiError
3609 JvmtiEnv::GetJNIFunctionTable(jniNativeInterface** function_table) {
3610   *function_table=(jniNativeInterface*)jvmtiMalloc(sizeof(jniNativeInterface));
3611   if (*function_table == nullptr)
3612     return JVMTI_ERROR_OUT_OF_MEMORY;
3613   memcpy(*function_table,(JavaThread::current())->get_jni_functions(),sizeof(jniNativeInterface));
3614   return JVMTI_ERROR_NONE;
3615 } /* end GetJNIFunctionTable */
3616 
3617 
3618   //
3619   // Event Management functions
3620   //
3621 
3622 jvmtiError
3623 JvmtiEnv::GenerateEvents(jvmtiEvent event_type) {
3624   // can only generate two event types
3625   if (event_type != JVMTI_EVENT_COMPILED_METHOD_LOAD &&
3626       event_type != JVMTI_EVENT_DYNAMIC_CODE_GENERATED) {
3627     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
3628   }
3629 
3630   // for compiled_method_load events we must check that the environment
3631   // has the can_generate_compiled_method_load_events capability.
3632   if (event_type == JVMTI_EVENT_COMPILED_METHOD_LOAD) {
3633     if (get_capabilities()->can_generate_compiled_method_load_events == 0) {
3634       return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
3635     }
3636     return JvmtiCodeBlobEvents::generate_compiled_method_load_events(this);
3637   } else {
3638     return JvmtiCodeBlobEvents::generate_dynamic_code_events(this);
3639   }
3640 
3641 } /* end GenerateEvents */
3642 
3643 
3644   //
3645   // Extension Mechanism functions
3646   //
3647 
3648 // extension_count_ptr - pre-checked for null
3649 // extensions - pre-checked for null
3650 jvmtiError
3651 JvmtiEnv::GetExtensionFunctions(jint* extension_count_ptr, jvmtiExtensionFunctionInfo** extensions) {
3652   return JvmtiExtensions::get_functions(this, extension_count_ptr, extensions);
3653 } /* end GetExtensionFunctions */
3654 
3655 
3656 // extension_count_ptr - pre-checked for null
3657 // extensions - pre-checked for null
3658 jvmtiError
3659 JvmtiEnv::GetExtensionEvents(jint* extension_count_ptr, jvmtiExtensionEventInfo** extensions) {
3660   return JvmtiExtensions::get_events(this, extension_count_ptr, extensions);
3661 } /* end GetExtensionEvents */
3662 
3663 
3664 // callback - null is a valid value, must be checked
3665 jvmtiError
3666 JvmtiEnv::SetExtensionEventCallback(jint extension_event_index, jvmtiExtensionEvent callback) {
3667   return JvmtiExtensions::set_event_callback(this, extension_event_index, callback);
3668 } /* end SetExtensionEventCallback */
3669 
3670   //
3671   // Timers functions
3672   //
3673 
3674 // info_ptr - pre-checked for null
3675 jvmtiError
3676 JvmtiEnv::GetCurrentThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) {
3677   os::current_thread_cpu_time_info(info_ptr);
3678   return JVMTI_ERROR_NONE;
3679 } /* end GetCurrentThreadCpuTimerInfo */
3680 
3681 
3682 // nanos_ptr - pre-checked for null
3683 jvmtiError
3684 JvmtiEnv::GetCurrentThreadCpuTime(jlong* nanos_ptr) {
3685   Thread* thread = Thread::current();
3686 
3687   // Surprisingly the GetCurrentThreadCpuTime is used by non-JavaThread's.
3688   if (thread->is_Java_thread()) {
3689     if (JavaThread::cast(thread)->is_vthread_mounted()) {
3690       // No support for a VirtualThread (yet).
3691       return JVMTI_ERROR_UNSUPPORTED_OPERATION;
3692     }
3693   }
3694   *nanos_ptr = os::current_thread_cpu_time();
3695   return JVMTI_ERROR_NONE;
3696 } /* end GetCurrentThreadCpuTime */
3697 
3698 
3699 // info_ptr - pre-checked for null
3700 jvmtiError
3701 JvmtiEnv::GetThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) {
3702   os::thread_cpu_time_info(info_ptr);
3703   return JVMTI_ERROR_NONE;
3704 } /* end GetThreadCpuTimerInfo */
3705 
3706 
3707 // nanos_ptr - pre-checked for null
3708 jvmtiError
3709 JvmtiEnv::GetThreadCpuTime(jthread thread, jlong* nanos_ptr) {
3710   JavaThread* current_thread = JavaThread::current();
3711   ThreadsListHandle tlh(current_thread);
3712   JavaThread* java_thread = nullptr;
3713   oop thread_oop = nullptr;
3714 
3715   jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_oop);
3716 
3717   if (thread_oop != nullptr && thread_oop->is_a(vmClasses::BaseVirtualThread_klass())) {
3718     // No support for virtual threads (yet).
3719     return JVMTI_ERROR_UNSUPPORTED_OPERATION;
3720   }
3721   if (err != JVMTI_ERROR_NONE) {
3722     return err;
3723   }
3724   NULL_CHECK(nanos_ptr, JVMTI_ERROR_NULL_POINTER);
3725 
3726   *nanos_ptr = os::thread_cpu_time(java_thread);
3727   return JVMTI_ERROR_NONE;
3728 } /* end GetThreadCpuTime */
3729 
3730 
3731 // info_ptr - pre-checked for null
3732 jvmtiError
3733 JvmtiEnv::GetTimerInfo(jvmtiTimerInfo* info_ptr) {
3734   os::javaTimeNanos_info(info_ptr);
3735   return JVMTI_ERROR_NONE;
3736 } /* end GetTimerInfo */
3737 
3738 
3739 // nanos_ptr - pre-checked for null
3740 jvmtiError
3741 JvmtiEnv::GetTime(jlong* nanos_ptr) {
3742   *nanos_ptr = os::javaTimeNanos();
3743   return JVMTI_ERROR_NONE;
3744 } /* end GetTime */
3745 
3746 
3747 // processor_count_ptr - pre-checked for null
3748 jvmtiError
3749 JvmtiEnv::GetAvailableProcessors(jint* processor_count_ptr) {
3750   *processor_count_ptr = os::active_processor_count();
3751   return JVMTI_ERROR_NONE;
3752 } /* end GetAvailableProcessors */
3753 
3754 jvmtiError
3755 JvmtiEnv::SetHeapSamplingInterval(jint sampling_interval) {
3756   if (sampling_interval < 0) {
3757     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
3758   }
3759   ThreadHeapSampler::set_sampling_interval(sampling_interval);
3760   return JVMTI_ERROR_NONE;
3761 } /* end SetHeapSamplingInterval */
3762 
3763   //
3764   // System Properties functions
3765   //
3766 
3767 // count_ptr - pre-checked for null
3768 // property_ptr - pre-checked for null
3769 jvmtiError
3770 JvmtiEnv::GetSystemProperties(jint* count_ptr, char*** property_ptr) {
3771   jvmtiError err = JVMTI_ERROR_NONE;
3772 
3773   // Get the number of readable properties.
3774   *count_ptr = Arguments::PropertyList_readable_count(Arguments::system_properties());
3775 
3776   // Allocate memory to hold the exact number of readable properties.
3777   err = allocate(*count_ptr * sizeof(char *), (unsigned char **)property_ptr);
3778   if (err != JVMTI_ERROR_NONE) {
3779     return err;
3780   }
3781   int readable_count = 0;
3782   // Loop through the system properties until all the readable properties are found.
3783   for (SystemProperty* p = Arguments::system_properties(); p != nullptr && readable_count < *count_ptr; p = p->next()) {
3784     if (p->readable()) {
3785       const char *key = p->key();
3786       char **tmp_value = *property_ptr+readable_count;
3787       readable_count++;
3788       err = allocate((strlen(key)+1) * sizeof(char), (unsigned char**)tmp_value);
3789       if (err == JVMTI_ERROR_NONE) {
3790         strcpy(*tmp_value, key);
3791       } else {
3792         // clean up previously allocated memory.
3793         for (int j = 0; j < readable_count; j++) {
3794           Deallocate((unsigned char*)*property_ptr+j);
3795         }
3796         Deallocate((unsigned char*)property_ptr);
3797         break;
3798       }
3799     }
3800   }
3801   assert(err != JVMTI_ERROR_NONE || readable_count == *count_ptr, "Bad readable property count");
3802   return err;
3803 } /* end GetSystemProperties */
3804 
3805 
3806 // property - pre-checked for null
3807 // value_ptr - pre-checked for null
3808 jvmtiError
3809 JvmtiEnv::GetSystemProperty(const char* property, char** value_ptr) {
3810   jvmtiError err = JVMTI_ERROR_NONE;
3811   const char *value;
3812 
3813   // Return JVMTI_ERROR_NOT_AVAILABLE if property is not readable or doesn't exist.
3814   value = Arguments::PropertyList_get_readable_value(Arguments::system_properties(), property);
3815   if (value == nullptr) {
3816     err =  JVMTI_ERROR_NOT_AVAILABLE;
3817   } else {
3818     err = allocate((strlen(value)+1) * sizeof(char), (unsigned char **)value_ptr);
3819     if (err == JVMTI_ERROR_NONE) {
3820       strcpy(*value_ptr, value);
3821     }
3822   }
3823   return err;
3824 } /* end GetSystemProperty */
3825 
3826 
3827 // property - pre-checked for null
3828 // value - null is a valid value, must be checked
3829 jvmtiError
3830 JvmtiEnv::SetSystemProperty(const char* property, const char* value_ptr) {
3831   for (SystemProperty* p = Arguments::system_properties(); p != nullptr; p = p->next()) {
3832     if (strcmp(property, p->key()) == 0) {
3833       if (p->writeable()) {
3834         if (p->set_value(value_ptr, AllocFailStrategy::RETURN_NULL)) {
3835           return JVMTI_ERROR_NONE;
3836         } else {
3837           return JVMTI_ERROR_OUT_OF_MEMORY;
3838         }
3839       } else {
3840         // We found a property, but it's not writeable
3841         return JVMTI_ERROR_NOT_AVAILABLE;
3842       }
3843     }
3844   }
3845 
3846   // We cannot find a property of the given name
3847   return JVMTI_ERROR_NOT_AVAILABLE;
3848 } /* end SetSystemProperty */