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