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