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