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