1 /*
   2  * Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/classLoaderExt.hpp"
  27 #include "classfile/javaClasses.inline.hpp"
  28 #include "classfile/stringTable.hpp"
  29 #include "classfile/modules.hpp"
  30 #include "classfile/systemDictionary.hpp"
  31 #include "classfile/vmClasses.hpp"
  32 #include "classfile/vmSymbols.hpp"
  33 #include "gc/shared/collectedHeap.hpp"
  34 #include "interpreter/bytecodeStream.hpp"
  35 #include "interpreter/interpreter.hpp"
  36 #include "jfr/jfrEvents.hpp"
  37 #include "jvmtifiles/jvmtiEnv.hpp"
  38 #include "logging/log.hpp"
  39 #include "logging/logConfiguration.hpp"
  40 #include "memory/resourceArea.hpp"
  41 #include "memory/universe.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/jfieldIDWorkaround.hpp"
  67 #include "runtime/jniHandles.inline.hpp"
  68 #include "runtime/objectMonitor.inline.hpp"
  69 #include "runtime/os.hpp"
  70 #include "runtime/osThread.hpp"
  71 #include "runtime/reflectionUtils.hpp"
  72 #include "runtime/signature.hpp"
  73 #include "runtime/thread.inline.hpp"
  74 #include "runtime/threadHeapSampler.hpp"
  75 #include "runtime/threadSMR.hpp"
  76 #include "runtime/timerTrace.hpp"
  77 #include "runtime/vframe.inline.hpp"
  78 #include "runtime/vmThread.hpp"
  79 #include "services/threadService.hpp"
  80 #include "utilities/exceptions.hpp"
  81 #include "utilities/preserveException.hpp"
  82 #include "utilities/utf8.hpp"
  83 
  84 
  85 #define FIXLATER 0 // REMOVE this when completed.
  86 
  87  // FIXLATER: hook into JvmtiTrace
  88 #define TraceJVMTICalls false
  89 
  90 JvmtiEnv::JvmtiEnv(jint version) : JvmtiEnvBase(version) {
  91 }
  92 
  93 JvmtiEnv::~JvmtiEnv() {
  94 }
  95 
  96 JvmtiEnv*
  97 JvmtiEnv::create_a_jvmti(jint version) {
  98   return new JvmtiEnv(version);
  99 }
 100 
 101 // VM operation class to copy jni function table at safepoint.
 102 // More than one java threads or jvmti agents may be reading/
 103 // modifying jni function tables. To reduce the risk of bad
 104 // interaction b/w these threads it is copied at safepoint.
 105 class VM_JNIFunctionTableCopier : public VM_Operation {
 106  private:
 107   const struct JNINativeInterface_ *_function_table;
 108  public:
 109   VM_JNIFunctionTableCopier(const struct JNINativeInterface_ *func_tbl) {
 110     _function_table = func_tbl;
 111   };
 112 
 113   VMOp_Type type() const { return VMOp_JNIFunctionTableCopier; }
 114   void doit() {
 115     copy_jni_function_table(_function_table);
 116   };
 117 };
 118 
 119 //
 120 // Do not change the "prefix" marker below, everything above it is copied
 121 // unchanged into the filled stub, everything below is controlled by the
 122 // stub filler (only method bodies are carried forward, and then only for
 123 // functionality still in the spec).
 124 //
 125 // end file prefix
 126 
 127   //
 128   // Memory Management functions
 129   //
 130 
 131 // mem_ptr - pre-checked for NULL
 132 jvmtiError
 133 JvmtiEnv::Allocate(jlong size, unsigned char** mem_ptr) {
 134   return allocate(size, mem_ptr);
 135 } /* end Allocate */
 136 
 137 
 138 // mem - NULL is a valid value, must be checked
 139 jvmtiError
 140 JvmtiEnv::Deallocate(unsigned char* mem) {
 141   return deallocate(mem);
 142 } /* end Deallocate */
 143 
 144 // Threads_lock NOT held
 145 // data - NULL is a valid value, must be checked
 146 jvmtiError
 147 JvmtiEnv::SetThreadLocalStorage(jthread thread, const void* data) {
 148   jvmtiError err = JVMTI_ERROR_NONE;
 149   JavaThread* java_thread = NULL;
 150   JvmtiThreadState* state = NULL;
 151   JvmtiVTMTDisabler vtmt_disabler;
 152   oop thread_obj = NULL;
 153   JavaThread* current = JavaThread::current();
 154 
 155   if (thread == NULL) {
 156     java_thread = current;
 157     state = java_thread->jvmti_thread_state();
 158   } else {
 159     ThreadsListHandle tlh;
 160     err = get_threadOop_and_JavaThread(tlh.list(), thread, &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 == NULL) {
 167     if (data == NULL) {
 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 == NULL) {
 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 // data_ptr - pre-checked for NULL
 185 jvmtiError
 186 JvmtiEnv::GetThreadLocalStorage(jthread thread, void** data_ptr) {
 187   JavaThread* current_thread = JavaThread::current();
 188   if (thread == NULL) {
 189     JvmtiThreadState* state = current_thread->jvmti_thread_state();
 190     *data_ptr = (state == NULL) ? NULL :
 191       state->env_thread_state(this)->get_agent_thread_local_storage_data();
 192   } else {
 193     // jvmti_GetThreadLocalStorage is "in native" and doesn't transition
 194     // the thread to _thread_in_vm. However, when the TLS for a thread
 195     // other than the current thread is required we need to transition
 196     // from native so as to resolve the jthread.
 197 
 198     MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, current_thread));
 199     ThreadInVMfromNative __tiv(current_thread);
 200     VM_ENTRY_BASE(jvmtiError, JvmtiEnv::GetThreadLocalStorage , current_thread)
 201     debug_only(VMNativeEntryWrapper __vew;)
 202 
 203     JavaThread* java_thread = NULL;
 204     oop thread_obj = NULL;
 205     JvmtiVTMTDisabler vtmt_disabler;
 206     ThreadsListHandle tlh(current_thread);
 207 
 208     jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, &java_thread, &thread_obj);
 209     if (err != JVMTI_ERROR_NONE) {
 210       return err;
 211     }
 212 
 213     HandleMark hm(current_thread);
 214     Handle thread_handle(current_thread, thread_obj);
 215     JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread, thread_handle);
 216     *data_ptr = (state == NULL) ? NULL :
 217       state->env_thread_state(this)->get_agent_thread_local_storage_data();
 218   }
 219   return JVMTI_ERROR_NONE;
 220 } /* end GetThreadLocalStorage */
 221 
 222   //
 223   // Module functions
 224   //
 225 
 226 // module_count_ptr - pre-checked for NULL
 227 // modules_ptr - pre-checked for NULL
 228 jvmtiError
 229 JvmtiEnv::GetAllModules(jint* module_count_ptr, jobject** modules_ptr) {
 230     JvmtiModuleClosure jmc;
 231 
 232     return jmc.get_all_modules(this, module_count_ptr, modules_ptr);
 233 } /* end GetAllModules */
 234 
 235 
 236 // class_loader - NULL is a valid value, must be pre-checked
 237 // package_name - pre-checked for NULL
 238 // module_ptr - pre-checked for NULL
 239 jvmtiError
 240 JvmtiEnv::GetNamedModule(jobject class_loader, const char* package_name, jobject* module_ptr) {
 241   JavaThread* THREAD = JavaThread::current(); // For exception macros.
 242   ResourceMark rm(THREAD);
 243 
 244   Handle h_loader (THREAD, JNIHandles::resolve(class_loader));
 245   // Check that loader is a subclass of java.lang.ClassLoader.
 246   if (h_loader.not_null() && !java_lang_ClassLoader::is_subclass(h_loader->klass())) {
 247     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 248   }
 249   oop module = Modules::get_named_module(h_loader, package_name);
 250   *module_ptr = module != NULL ? JNIHandles::make_local(THREAD, module) : NULL;
 251   return JVMTI_ERROR_NONE;
 252 } /* end GetNamedModule */
 253 
 254 
 255 // module - pre-checked for NULL
 256 // to_module - pre-checked for NULL
 257 jvmtiError
 258 JvmtiEnv::AddModuleReads(jobject module, jobject to_module) {
 259   JavaThread* THREAD = JavaThread::current(); // For exception macros.
 260 
 261   // check module
 262   Handle h_module(THREAD, JNIHandles::resolve(module));
 263   if (!java_lang_Module::is_instance(h_module())) {
 264     return JVMTI_ERROR_INVALID_MODULE;
 265   }
 266   // check to_module
 267   Handle h_to_module(THREAD, JNIHandles::resolve(to_module));
 268   if (!java_lang_Module::is_instance(h_to_module())) {
 269     return JVMTI_ERROR_INVALID_MODULE;
 270   }
 271   return JvmtiExport::add_module_reads(h_module, h_to_module, THREAD);
 272 } /* end AddModuleReads */
 273 
 274 
 275 // module - pre-checked for NULL
 276 // pkg_name - pre-checked for NULL
 277 // to_module - pre-checked for NULL
 278 jvmtiError
 279 JvmtiEnv::AddModuleExports(jobject module, const char* pkg_name, jobject to_module) {
 280   JavaThread* THREAD = JavaThread::current(); // For exception macros.
 281   Handle h_pkg = java_lang_String::create_from_str(pkg_name, THREAD);
 282 
 283   // check module
 284   Handle h_module(THREAD, JNIHandles::resolve(module));
 285   if (!java_lang_Module::is_instance(h_module())) {
 286     return JVMTI_ERROR_INVALID_MODULE;
 287   }
 288   // check to_module
 289   Handle h_to_module(THREAD, JNIHandles::resolve(to_module));
 290   if (!java_lang_Module::is_instance(h_to_module())) {
 291     return JVMTI_ERROR_INVALID_MODULE;
 292   }
 293   return JvmtiExport::add_module_exports(h_module, h_pkg, h_to_module, THREAD);
 294 } /* end AddModuleExports */
 295 
 296 
 297 // module - pre-checked for NULL
 298 // pkg_name - pre-checked for NULL
 299 // to_module - pre-checked for NULL
 300 jvmtiError
 301 JvmtiEnv::AddModuleOpens(jobject module, const char* pkg_name, jobject to_module) {
 302   JavaThread* THREAD = JavaThread::current(); // For exception macros.
 303   Handle h_pkg = java_lang_String::create_from_str(pkg_name, THREAD);
 304 
 305   // check module
 306   Handle h_module(THREAD, JNIHandles::resolve(module));
 307   if (!java_lang_Module::is_instance(h_module())) {
 308     return JVMTI_ERROR_INVALID_MODULE;
 309   }
 310   // check to_module
 311   Handle h_to_module(THREAD, JNIHandles::resolve(to_module));
 312   if (!java_lang_Module::is_instance(h_to_module())) {
 313     return JVMTI_ERROR_INVALID_MODULE;
 314   }
 315   return JvmtiExport::add_module_opens(h_module, h_pkg, h_to_module, THREAD);
 316 } /* end AddModuleOpens */
 317 
 318 
 319 // module - pre-checked for NULL
 320 // service - pre-checked for NULL
 321 jvmtiError
 322 JvmtiEnv::AddModuleUses(jobject module, jclass service) {
 323   JavaThread* THREAD = JavaThread::current(); // For exception macros.
 324 
 325   // check module
 326   Handle h_module(THREAD, JNIHandles::resolve(module));
 327   if (!java_lang_Module::is_instance(h_module())) {
 328     return JVMTI_ERROR_INVALID_MODULE;
 329   }
 330   // check service
 331   Handle h_service(THREAD, JNIHandles::resolve_external_guard(service));
 332   if (!java_lang_Class::is_instance(h_service()) ||
 333       java_lang_Class::is_primitive(h_service())) {
 334     return JVMTI_ERROR_INVALID_CLASS;
 335   }
 336   return JvmtiExport::add_module_uses(h_module, h_service, THREAD);
 337 } /* end AddModuleUses */
 338 
 339 
 340 // module - pre-checked for NULL
 341 // service - pre-checked for NULL
 342 // impl_class - pre-checked for NULL
 343 jvmtiError
 344 JvmtiEnv::AddModuleProvides(jobject module, jclass service, jclass impl_class) {
 345   JavaThread* THREAD = JavaThread::current(); // For exception macros.
 346 
 347   // check module
 348   Handle h_module(THREAD, JNIHandles::resolve(module));
 349   if (!java_lang_Module::is_instance(h_module())) {
 350     return JVMTI_ERROR_INVALID_MODULE;
 351   }
 352   // check service
 353   Handle h_service(THREAD, JNIHandles::resolve_external_guard(service));
 354   if (!java_lang_Class::is_instance(h_service()) ||
 355       java_lang_Class::is_primitive(h_service())) {
 356     return JVMTI_ERROR_INVALID_CLASS;
 357   }
 358   // check impl_class
 359   Handle h_impl_class(THREAD, JNIHandles::resolve_external_guard(impl_class));
 360   if (!java_lang_Class::is_instance(h_impl_class()) ||
 361       java_lang_Class::is_primitive(h_impl_class())) {
 362     return JVMTI_ERROR_INVALID_CLASS;
 363   }
 364   return JvmtiExport::add_module_provides(h_module, h_service, h_impl_class, THREAD);
 365 } /* end AddModuleProvides */
 366 
 367 // module - pre-checked for NULL
 368 // is_modifiable_class_ptr - pre-checked for NULL
 369 jvmtiError
 370 JvmtiEnv::IsModifiableModule(jobject module, jboolean* is_modifiable_module_ptr) {
 371   JavaThread* current = JavaThread::current();
 372 
 373   // check module
 374   Handle h_module(current, JNIHandles::resolve(module));
 375   if (!java_lang_Module::is_instance(h_module())) {
 376     return JVMTI_ERROR_INVALID_MODULE;
 377   }
 378 
 379   *is_modifiable_module_ptr = JNI_TRUE;
 380   return JVMTI_ERROR_NONE;
 381 } /* end IsModifiableModule */
 382 
 383 
 384   //
 385   // Class functions
 386   //
 387 
 388 // class_count_ptr - pre-checked for NULL
 389 // classes_ptr - pre-checked for NULL
 390 jvmtiError
 391 JvmtiEnv::GetLoadedClasses(jint* class_count_ptr, jclass** classes_ptr) {
 392   return JvmtiGetLoadedClasses::getLoadedClasses(this, class_count_ptr, classes_ptr);
 393 } /* end GetLoadedClasses */
 394 
 395 
 396 // initiating_loader - NULL is a valid value, must be checked
 397 // class_count_ptr - pre-checked for NULL
 398 // classes_ptr - pre-checked for NULL
 399 jvmtiError
 400 JvmtiEnv::GetClassLoaderClasses(jobject initiating_loader, jint* class_count_ptr, jclass** classes_ptr) {
 401   return JvmtiGetLoadedClasses::getClassLoaderClasses(this, initiating_loader,
 402                                                   class_count_ptr, classes_ptr);
 403 } /* end GetClassLoaderClasses */
 404 
 405 // k_mirror - may be primitive, this must be checked
 406 // is_modifiable_class_ptr - pre-checked for NULL
 407 jvmtiError
 408 JvmtiEnv::IsModifiableClass(oop k_mirror, jboolean* is_modifiable_class_ptr) {
 409   *is_modifiable_class_ptr = VM_RedefineClasses::is_modifiable_class(k_mirror)?
 410                                                        JNI_TRUE : JNI_FALSE;
 411   return JVMTI_ERROR_NONE;
 412 } /* end IsModifiableClass */
 413 
 414 // class_count - pre-checked to be greater than or equal to 0
 415 // classes - pre-checked for NULL
 416 jvmtiError
 417 JvmtiEnv::RetransformClasses(jint class_count, const jclass* classes) {
 418 //TODO: add locking
 419 
 420   int index;
 421   JavaThread* current_thread = JavaThread::current();
 422   ResourceMark rm(current_thread);
 423 
 424   jvmtiClassDefinition* class_definitions =
 425                             NEW_RESOURCE_ARRAY(jvmtiClassDefinition, class_count);
 426   NULL_CHECK(class_definitions, JVMTI_ERROR_OUT_OF_MEMORY);
 427 
 428   for (index = 0; index < class_count; index++) {
 429     HandleMark hm(current_thread);
 430 
 431     jclass jcls = classes[index];
 432     oop k_mirror = JNIHandles::resolve_external_guard(jcls);
 433     if (k_mirror == NULL) {
 434       return JVMTI_ERROR_INVALID_CLASS;
 435     }
 436     if (!k_mirror->is_a(vmClasses::Class_klass())) {
 437       return JVMTI_ERROR_INVALID_CLASS;
 438     }
 439 
 440     if (!VM_RedefineClasses::is_modifiable_class(k_mirror)) {
 441       return JVMTI_ERROR_UNMODIFIABLE_CLASS;
 442     }
 443 
 444     Klass* klass = java_lang_Class::as_Klass(k_mirror);
 445 
 446     jint status = klass->jvmti_class_status();
 447     if (status & (JVMTI_CLASS_STATUS_ERROR)) {
 448       return JVMTI_ERROR_INVALID_CLASS;
 449     }
 450 
 451     InstanceKlass* ik = InstanceKlass::cast(klass);
 452     if (ik->get_cached_class_file_bytes() == NULL) {
 453       // Not cached, we need to reconstitute the class file from the
 454       // VM representation. We don't attach the reconstituted class
 455       // bytes to the InstanceKlass here because they have not been
 456       // validated and we're not at a safepoint.
 457       JvmtiClassFileReconstituter reconstituter(ik);
 458       if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
 459         return reconstituter.get_error();
 460       }
 461 
 462       class_definitions[index].class_byte_count = (jint)reconstituter.class_file_size();
 463       class_definitions[index].class_bytes      = (unsigned char*)
 464                                                        reconstituter.class_file_bytes();
 465     } else {
 466       // it is cached, get it from the cache
 467       class_definitions[index].class_byte_count = ik->get_cached_class_file_len();
 468       class_definitions[index].class_bytes      = ik->get_cached_class_file_bytes();
 469     }
 470     class_definitions[index].klass              = jcls;
 471   }
 472   EventRetransformClasses event;
 473   VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_retransform);
 474   VMThread::execute(&op);
 475   jvmtiError error = op.check_error();
 476   if (error == JVMTI_ERROR_NONE) {
 477     event.set_classCount(class_count);
 478     event.set_redefinitionId(op.id());
 479     event.commit();
 480   }
 481   return error;
 482 } /* end RetransformClasses */
 483 
 484 
 485 // class_count - pre-checked to be greater than or equal to 0
 486 // class_definitions - pre-checked for NULL
 487 jvmtiError
 488 JvmtiEnv::RedefineClasses(jint class_count, const jvmtiClassDefinition* class_definitions) {
 489 //TODO: add locking
 490   EventRedefineClasses event;
 491   VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_redefine);
 492   VMThread::execute(&op);
 493   jvmtiError error = op.check_error();
 494   if (error == JVMTI_ERROR_NONE) {
 495     event.set_classCount(class_count);
 496     event.set_redefinitionId(op.id());
 497     event.commit();
 498   }
 499   return error;
 500 } /* end RedefineClasses */
 501 
 502 
 503   //
 504   // Object functions
 505   //
 506 
 507 // size_ptr - pre-checked for NULL
 508 jvmtiError
 509 JvmtiEnv::GetObjectSize(jobject object, jlong* size_ptr) {
 510   oop mirror = JNIHandles::resolve_external_guard(object);
 511   NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
 512   *size_ptr = (jlong)mirror->size() * wordSize;
 513   return JVMTI_ERROR_NONE;
 514 } /* end GetObjectSize */
 515 
 516   //
 517   // Method functions
 518   //
 519 
 520 // prefix - NULL is a valid value, must be checked
 521 jvmtiError
 522 JvmtiEnv::SetNativeMethodPrefix(const char* prefix) {
 523   return prefix == NULL?
 524               SetNativeMethodPrefixes(0, NULL) :
 525               SetNativeMethodPrefixes(1, (char**)&prefix);
 526 } /* end SetNativeMethodPrefix */
 527 
 528 
 529 // prefix_count - pre-checked to be greater than or equal to 0
 530 // prefixes - pre-checked for NULL
 531 jvmtiError
 532 JvmtiEnv::SetNativeMethodPrefixes(jint prefix_count, char** prefixes) {
 533   // Have to grab JVMTI thread state lock to be sure that some thread
 534   // isn't accessing the prefixes at the same time we are setting them.
 535   // No locks during VM bring-up.
 536   if (Threads::number_of_threads() == 0) {
 537     return set_native_method_prefixes(prefix_count, prefixes);
 538   } else {
 539     MutexLocker mu(JvmtiThreadState_lock);
 540     return set_native_method_prefixes(prefix_count, prefixes);
 541   }
 542 } /* end SetNativeMethodPrefixes */
 543 
 544   //
 545   // Event Management functions
 546   //
 547 
 548 // callbacks - NULL is a valid value, must be checked
 549 // size_of_callbacks - pre-checked to be greater than or equal to 0
 550 jvmtiError
 551 JvmtiEnv::SetEventCallbacks(const jvmtiEventCallbacks* callbacks, jint size_of_callbacks) {
 552   JvmtiEventController::set_event_callbacks(this, callbacks, size_of_callbacks);
 553   return JVMTI_ERROR_NONE;
 554 } /* end SetEventCallbacks */
 555 
 556 
 557 // event_thread - NULL is a valid value, must be checked
 558 jvmtiError
 559 JvmtiEnv::SetEventNotificationMode(jvmtiEventMode mode, jvmtiEvent event_type, jthread event_thread,   ...) {
 560   bool enabled = (mode == JVMTI_ENABLE);
 561 
 562   // event_type must be valid
 563   if (!JvmtiEventController::is_valid_event_type(event_type)) {
 564     return JVMTI_ERROR_INVALID_EVENT_TYPE;
 565   }
 566 
 567   // assure that needed capabilities are present
 568   if (enabled && !JvmtiUtil::has_event_capability(event_type, get_capabilities())) {
 569     return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
 570   }
 571 
 572   if (event_type == JVMTI_EVENT_CLASS_FILE_LOAD_HOOK && enabled) {
 573     record_class_file_load_hook_enabled();
 574   }
 575 
 576   if (event_thread == NULL) {
 577     // Can be called at Agent_OnLoad() time with event_thread == NULL
 578     // when Thread::current() does not work yet so we cannot create a
 579     // ThreadsListHandle that is common to both thread-specific and
 580     // global code paths.
 581 
 582     JvmtiEventController::set_user_enabled(this, (JavaThread*) NULL, (oop) NULL, event_type, enabled);
















 583   } else {
 584     // We have a specified event_thread.
 585     JavaThread* java_thread = NULL;
 586     oop thread_obj = NULL;
 587     JvmtiVTMTDisabler vtmt_disabler;
 588     ThreadsListHandle tlh;
 589 
 590     jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), event_thread, &java_thread, &thread_obj);
 591     if (err != JVMTI_ERROR_NONE) {
 592       return err;
 593     }
 594 





 595     // global events cannot be controlled at thread level.
 596     if (JvmtiEventController::is_global_event(event_type)) {
 597       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 598     }
 599 
 600     JvmtiEventController::set_user_enabled(this, java_thread, thread_obj, event_type, enabled);










 601   }
 602 
 603   return JVMTI_ERROR_NONE;
 604 } /* end SetEventNotificationMode */
 605 
 606   //
 607   // Capability functions
 608   //
 609 
 610 // capabilities_ptr - pre-checked for NULL
 611 jvmtiError
 612 JvmtiEnv::GetPotentialCapabilities(jvmtiCapabilities* capabilities_ptr) {
 613   JvmtiManageCapabilities::get_potential_capabilities(get_capabilities(),
 614                                                       get_prohibited_capabilities(),
 615                                                       capabilities_ptr);
 616   return JVMTI_ERROR_NONE;
 617 } /* end GetPotentialCapabilities */
 618 
 619 
 620 // capabilities_ptr - pre-checked for NULL
 621 jvmtiError
 622 JvmtiEnv::AddCapabilities(const jvmtiCapabilities* capabilities_ptr) {
 623   return JvmtiManageCapabilities::add_capabilities(get_capabilities(),
 624                                                    get_prohibited_capabilities(),
 625                                                    capabilities_ptr,
 626                                                    get_capabilities());
 627 } /* end AddCapabilities */
 628 
 629 
 630 // capabilities_ptr - pre-checked for NULL
 631 jvmtiError
 632 JvmtiEnv::RelinquishCapabilities(const jvmtiCapabilities* capabilities_ptr) {
 633   JvmtiManageCapabilities::relinquish_capabilities(get_capabilities(), capabilities_ptr, get_capabilities());
 634   return JVMTI_ERROR_NONE;
 635 } /* end RelinquishCapabilities */
 636 
 637 
 638 // capabilities_ptr - pre-checked for NULL
 639 jvmtiError
 640 JvmtiEnv::GetCapabilities(jvmtiCapabilities* capabilities_ptr) {
 641   JvmtiManageCapabilities::copy_capabilities(get_capabilities(), capabilities_ptr);
 642   return JVMTI_ERROR_NONE;
 643 } /* end GetCapabilities */
 644 
 645   //
 646   // Class Loader Search functions
 647   //
 648 
 649 // segment - pre-checked for NULL
 650 jvmtiError
 651 JvmtiEnv::AddToBootstrapClassLoaderSearch(const char* segment) {
 652   jvmtiPhase phase = get_phase();
 653   if (phase == JVMTI_PHASE_ONLOAD) {
 654     Arguments::append_sysclasspath(segment);
 655     return JVMTI_ERROR_NONE;
 656   } else if (use_version_1_0_semantics()) {
 657     // This JvmtiEnv requested version 1.0 semantics and this function
 658     // is only allowed in the ONLOAD phase in version 1.0 so we need to
 659     // return an error here.
 660     return JVMTI_ERROR_WRONG_PHASE;
 661   } else if (phase == JVMTI_PHASE_LIVE) {
 662     // The phase is checked by the wrapper that called this function,
 663     // but this thread could be racing with the thread that is
 664     // terminating the VM so we check one more time.
 665 
 666     // create the zip entry
 667     ClassPathZipEntry* zip_entry = ClassLoader::create_class_path_zip_entry(segment, true);
 668     if (zip_entry == NULL) {
 669       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 670     }
 671 
 672     // add the jar file to the bootclasspath
 673     log_info(class, load)("opened: %s", zip_entry->name());
 674 #if INCLUDE_CDS
 675     ClassLoaderExt::append_boot_classpath(zip_entry);
 676 #else
 677     ClassLoader::add_to_boot_append_entries(zip_entry);
 678 #endif
 679     return JVMTI_ERROR_NONE;
 680   } else {
 681     return JVMTI_ERROR_WRONG_PHASE;
 682   }
 683 
 684 } /* end AddToBootstrapClassLoaderSearch */
 685 
 686 
 687 // segment - pre-checked for NULL
 688 jvmtiError
 689 JvmtiEnv::AddToSystemClassLoaderSearch(const char* segment) {
 690   jvmtiPhase phase = get_phase();
 691 
 692   if (phase == JVMTI_PHASE_ONLOAD) {
 693     for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) {
 694       if (strcmp("java.class.path", p->key()) == 0) {
 695         p->append_value(segment);
 696         break;
 697       }
 698     }
 699     return JVMTI_ERROR_NONE;
 700   } else if (phase == JVMTI_PHASE_LIVE) {
 701     // The phase is checked by the wrapper that called this function,
 702     // but this thread could be racing with the thread that is
 703     // terminating the VM so we check one more time.
 704     JavaThread* THREAD = JavaThread::current(); // For exception macros.
 705     HandleMark hm(THREAD);
 706 
 707     // create the zip entry (which will open the zip file and hence
 708     // check that the segment is indeed a zip file).
 709     ClassPathZipEntry* zip_entry = ClassLoader::create_class_path_zip_entry(segment, false);
 710     if (zip_entry == NULL) {
 711       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 712     }
 713     delete zip_entry;   // no longer needed
 714 
 715     // lock the loader
 716     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 717     ObjectLocker ol(loader, THREAD);
 718 
 719     // need the path as java.lang.String
 720     Handle path = java_lang_String::create_from_platform_dependent_str(segment, THREAD);
 721     if (HAS_PENDING_EXCEPTION) {
 722       CLEAR_PENDING_EXCEPTION;
 723       return JVMTI_ERROR_INTERNAL;
 724     }
 725 
 726     // Invoke the appendToClassPathForInstrumentation method - if the method
 727     // is not found it means the loader doesn't support adding to the class path
 728     // in the live phase.
 729     {
 730       JavaValue res(T_VOID);
 731       JavaCalls::call_special(&res,
 732                               loader,
 733                               loader->klass(),
 734                               vmSymbols::appendToClassPathForInstrumentation_name(),
 735                               vmSymbols::appendToClassPathForInstrumentation_signature(),
 736                               path,
 737                               THREAD);
 738       if (HAS_PENDING_EXCEPTION) {
 739         Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
 740         CLEAR_PENDING_EXCEPTION;
 741 
 742         if (ex_name == vmSymbols::java_lang_NoSuchMethodError()) {
 743           return JVMTI_ERROR_CLASS_LOADER_UNSUPPORTED;
 744         } else {
 745           return JVMTI_ERROR_INTERNAL;
 746         }
 747       }
 748     }
 749 
 750     return JVMTI_ERROR_NONE;
 751   } else {
 752     return JVMTI_ERROR_WRONG_PHASE;
 753   }
 754 } /* end AddToSystemClassLoaderSearch */
 755 
 756   //
 757   // General functions
 758   //
 759 
 760 // phase_ptr - pre-checked for NULL
 761 jvmtiError
 762 JvmtiEnv::GetPhase(jvmtiPhase* phase_ptr) {
 763   *phase_ptr = phase();
 764   return JVMTI_ERROR_NONE;
 765 } /* end GetPhase */
 766 
 767 
 768 jvmtiError
 769 JvmtiEnv::DisposeEnvironment() {
 770   dispose();
 771   return JVMTI_ERROR_NONE;
 772 } /* end DisposeEnvironment */
 773 
 774 
 775 // data - NULL is a valid value, must be checked
 776 jvmtiError
 777 JvmtiEnv::SetEnvironmentLocalStorage(const void* data) {
 778   set_env_local_storage(data);
 779   return JVMTI_ERROR_NONE;
 780 } /* end SetEnvironmentLocalStorage */
 781 
 782 
 783 // data_ptr - pre-checked for NULL
 784 jvmtiError
 785 JvmtiEnv::GetEnvironmentLocalStorage(void** data_ptr) {
 786   *data_ptr = (void*)get_env_local_storage();
 787   return JVMTI_ERROR_NONE;
 788 } /* end GetEnvironmentLocalStorage */
 789 
 790 // version_ptr - pre-checked for NULL
 791 jvmtiError
 792 JvmtiEnv::GetVersionNumber(jint* version_ptr) {
 793   *version_ptr = JVMTI_VERSION;
 794   return JVMTI_ERROR_NONE;
 795 } /* end GetVersionNumber */
 796 
 797 
 798 // name_ptr - pre-checked for NULL
 799 jvmtiError
 800 JvmtiEnv::GetErrorName(jvmtiError error, char** name_ptr) {
 801   if (error < JVMTI_ERROR_NONE || error > JVMTI_ERROR_MAX) {
 802     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 803   }
 804   const char *name = JvmtiUtil::error_name(error);
 805   if (name == NULL) {
 806     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 807   }
 808   size_t len = strlen(name) + 1;
 809   jvmtiError err = allocate(len, (unsigned char**)name_ptr);
 810   if (err == JVMTI_ERROR_NONE) {
 811     memcpy(*name_ptr, name, len);
 812   }
 813   return err;
 814 } /* end GetErrorName */
 815 
 816 
 817 jvmtiError
 818 JvmtiEnv::SetVerboseFlag(jvmtiVerboseFlag flag, jboolean value) {
 819   LogLevelType level = value == 0 ? LogLevel::Off : LogLevel::Info;
 820   switch (flag) {
 821   case JVMTI_VERBOSE_OTHER:
 822     // ignore
 823     break;
 824   case JVMTI_VERBOSE_CLASS:
 825     LogConfiguration::configure_stdout(level, false, LOG_TAGS(class, unload));
 826     LogConfiguration::configure_stdout(level, false, LOG_TAGS(class, load));
 827     break;
 828   case JVMTI_VERBOSE_GC:
 829     LogConfiguration::configure_stdout(level, true, LOG_TAGS(gc));
 830     break;
 831   case JVMTI_VERBOSE_JNI:
 832     level = value == 0 ? LogLevel::Off : LogLevel::Debug;
 833     LogConfiguration::configure_stdout(level, true, LOG_TAGS(jni, resolve));
 834     break;
 835   default:
 836     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 837   };
 838   return JVMTI_ERROR_NONE;
 839 } /* end SetVerboseFlag */
 840 
 841 
 842 // format_ptr - pre-checked for NULL
 843 jvmtiError
 844 JvmtiEnv::GetJLocationFormat(jvmtiJlocationFormat* format_ptr) {
 845   *format_ptr = JVMTI_JLOCATION_JVMBCI;
 846   return JVMTI_ERROR_NONE;
 847 } /* end GetJLocationFormat */
 848 
 849   //
 850   // Thread functions
 851   //
 852 

 853 // thread_state_ptr - pre-checked for NULL
 854 jvmtiError
 855 JvmtiEnv::GetThreadState(jthread thread, jint* thread_state_ptr) {
 856   JavaThread* current_thread = JavaThread::current();
 857   JavaThread* java_thread = NULL;
 858   oop thread_oop = NULL;
 859   JvmtiVTMTDisabler vtmt_disabler;
 860   ThreadsListHandle tlh(current_thread);
 861 
 862   if (thread == NULL) {
 863     java_thread = current_thread;
 864     thread_oop = get_vthread_or_thread_oop(java_thread);
 865 
 866     if (thread_oop == NULL || !thread_oop->is_a(vmClasses::Thread_klass())) {
 867       return JVMTI_ERROR_INVALID_THREAD;
 868     }
 869   } else {
 870     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
 871     if (err != JVMTI_ERROR_NONE) {
 872       // We got an error code so we don't have a JavaThread *, but
 873       // only return an error from here if we didn't get a valid
 874       // thread_oop.
 875       // In a vthread case the cv_external_thread_to_JavaThread is expected to correctly set
 876       // the thread_oop and return JVMTI_ERROR_INVALID_THREAD which we ignore here.
 877       if (thread_oop == NULL) {
 878         return err;
 879       }
 880       // We have a valid thread_oop so we can return some thread state.
 881     }
 882   }
 883 
 884   // Support for virtual thread
 885   if (java_lang_VirtualThread::is_instance(thread_oop)) {
 886     *thread_state_ptr = JvmtiEnvBase::get_vthread_state(thread_oop);
 887   } else {
 888     *thread_state_ptr = JvmtiEnvBase::get_thread_state(thread_oop, java_thread);











 889   }
 890   // java_thread can be suspended only if its virtual or carrier thread object is suspended.
 891   assert(java_thread == NULL || !java_thread->is_suspended() ||
 892          (*thread_state_ptr | JVMTI_THREAD_STATE_SUSPENDED) != 0, "sanity check");
 893   return JVMTI_ERROR_NONE;
 894 } /* end GetThreadState */
 895 
 896 
 897 // thread_ptr - pre-checked for NULL
 898 jvmtiError
 899 JvmtiEnv::GetCurrentThread(jthread* thread_ptr) {
 900   JavaThread* cur_thread = JavaThread::current();
 901   oop thread_oop = get_vthread_or_thread_oop(cur_thread);
 902 
 903   *thread_ptr = (jthread)JNIHandles::make_local(cur_thread, thread_oop);
 904   return JVMTI_ERROR_NONE;
 905 } /* end GetCurrentThread */
 906 
 907 
 908 // threads_count_ptr - pre-checked for NULL
 909 // threads_ptr - pre-checked for NULL
 910 jvmtiError
 911 JvmtiEnv::GetAllThreads(jint* threads_count_ptr, jthread** threads_ptr) {
 912   int nthreads        = 0;
 913   Handle *thread_objs = NULL;
 914   Thread* current_thread = Thread::current();
 915   ResourceMark rm(current_thread);
 916   HandleMark hm(current_thread);
 917 
 918   // enumerate threads (including agent threads)
 919   ThreadsListEnumerator tle(current_thread, true);
 920   nthreads = tle.num_threads();
 921   *threads_count_ptr = nthreads;
 922 
 923   if (nthreads == 0) {
 924     *threads_ptr = NULL;
 925     return JVMTI_ERROR_NONE;
 926   }
 927 
 928   thread_objs = NEW_RESOURCE_ARRAY(Handle, nthreads);
 929   NULL_CHECK(thread_objs, JVMTI_ERROR_OUT_OF_MEMORY);
 930 
 931   for (int i = 0; i < nthreads; i++) {
 932     thread_objs[i] = Handle(tle.get_threadObj(i));
 933   }
 934 
 935   jthread *jthreads  = new_jthreadArray(nthreads, thread_objs);
 936   NULL_CHECK(jthreads, JVMTI_ERROR_OUT_OF_MEMORY);
 937 
 938   *threads_ptr = jthreads;
 939   return JVMTI_ERROR_NONE;
 940 } /* end GetAllThreads */
 941 
 942 

 943 jvmtiError
 944 JvmtiEnv::SuspendThread(jthread thread) {
 945   JavaThread* current = JavaThread::current();
 946   ThreadsListHandle tlh(current);
 947   JavaThread* java_thread = NULL;
 948   oop thread_oop = NULL;
 949   jvmtiError err;
 950 
 951   {
 952     JvmtiVTMTDisabler vtmt_disabler(true);
 953 
 954     err = get_threadOop_and_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
 955     if (err != JVMTI_ERROR_NONE) {
 956       return err;
 957     }
 958 
 959     // Do not use JvmtiVTMTDisabler in context of self suspend to avoid deadlocks.
 960     if (java_thread != current) {
 961       err = suspend_thread(thread_oop, java_thread, true, NULL); // single suspend
 962       return err;
 963     } 
 964   }
 965   // Do self suspend for current JavaThread.
 966   err = suspend_thread(thread_oop, current, true, NULL); // single suspend
 967   return err;
 968 } /* end SuspendThread */
 969 
 970 
 971 // request_count - pre-checked to be greater than or equal to 0
 972 // request_list - pre-checked for NULL
 973 // results - pre-checked for NULL
 974 jvmtiError
 975 JvmtiEnv::SuspendThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {


 976   JavaThread* current = JavaThread::current();
 977   ThreadsListHandle tlh(current);
 978   HandleMark hm(current);
 979   Handle self_tobj = Handle(current, NULL);
 980   int self_idx = -1;
 981 
 982   {
 983     JvmtiVTMTDisabler vtmt_disabler(true);
 984 
 985     for (int i = 0; i < request_count; i++) {
 986       JavaThread *java_thread = NULL;
 987       oop thread_oop = NULL;
 988       jthread thread = request_list[i];
 989       jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
 990       if (err != JVMTI_ERROR_NONE) {
 991         if (thread_oop == NULL || err != JVMTI_ERROR_INVALID_THREAD) {
 992           results[i] = err;
 993           continue;
 994         }









 995       }
 996       if (java_thread == current) {
 997         self_idx = i;
 998         self_tobj = Handle(current, thread_oop);
 999         continue; // self suspend after all other suspends









1000       }
1001       results[i] = suspend_thread(thread_oop, java_thread, true, NULL); // single suspend

1002     }
1003   }
1004   // Self suspend after all other suspends if necessary.
1005   // Do not use JvmtiVTMTDisabler in context of self suspend to avoid deadlocks.
1006   if (self_tobj() != NULL) {
1007     // there should not be any error for current java_thread
1008     results[self_idx] = suspend_thread(self_tobj(), current, true, NULL); // single suspend
1009   }
1010   // per-thread suspend results returned via results parameter
1011   return JVMTI_ERROR_NONE;
1012 } /* end SuspendThreadList */
1013 
1014 

1015 jvmtiError
1016 JvmtiEnv::SuspendAllVirtualThreads(jint except_count, const jthread* except_list) {
1017   JavaThread* current = JavaThread::current();
1018   ThreadsListHandle tlh(current);
1019   jvmtiError err = JvmtiEnvBase::check_thread_list(except_count, except_list);
1020   if (err != JVMTI_ERROR_NONE) {
1021     return err;
1022   }
1023   if (!JvmtiExport::can_support_virtual_threads()) {
1024     return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
1025   }
1026   HandleMark hm(current);
1027   Handle self_tobj = Handle(current, NULL);
1028 
1029   {
1030     ResourceMark rm(current);
1031     JvmtiVTMTDisabler vtmt_disabler(true);
1032     GrowableArray<jthread>* elist = new GrowableArray<jthread>(except_count);
1033 
1034     // Collect threads from except_list which resumed status must be restored.
1035     for (int idx = 0; idx < except_count; idx++) {
1036       jthread thread = except_list[idx];
1037       oop thread_oop = JNIHandles::resolve_external_guard(thread);
1038       if (!JvmtiVTSuspender::is_vthread_suspended(thread_oop)) {
1039           // is resumed, so its resumed status must be restored
1040           elist->append(except_list[idx]);
1041       }
1042     }
1043 
1044     for (JavaThreadIteratorWithHandle jtiwh; JavaThread *java_thread = jtiwh.next(); ) {
1045       oop vt_oop = java_thread->mounted_vthread();
1046       if (!java_thread->is_exiting() &&
1047           !java_thread->is_jvmti_agent_thread() &&
1048           !java_thread->is_hidden_from_external_view() &&
1049           vt_oop != NULL &&
1050           java_lang_VirtualThread::is_instance(vt_oop) &&
1051           JvmtiEnvBase::is_vthread_alive(vt_oop) &&
1052           !JvmtiVTSuspender::is_vthread_suspended(vt_oop) &&
1053           !is_in_thread_list(except_count, except_list, vt_oop)
1054       ) {
1055         if (java_thread == current) {
1056           self_tobj = Handle(current, vt_oop);
1057           continue; // self suspend after all other suspends
1058         }
1059         suspend_thread(vt_oop, java_thread, false, NULL); // suspend all mode
1060       }
1061     }
1062     JvmtiVTSuspender::register_all_vthreads_suspend();
1063 
1064     // Restore resumed state for threads from except list that were resumed before.
1065     for (int idx = 0; idx < elist->length(); idx++) {
1066       jthread thread = elist->at(idx);
1067       oop thread_oop = JNIHandles::resolve_external_guard(thread);
1068       if (JvmtiVTSuspender::is_vthread_suspended(thread_oop)) {
1069         JvmtiVTSuspender::register_vthread_resume(thread_oop);
1070       }
1071     }
1072   }
1073   // Self suspend after all other suspends if necessary.
1074   // Do not use JvmtiVTMTDisabler in context of self suspend to avoid deadlocks.
1075   if (self_tobj() != NULL) {
1076     suspend_thread(self_tobj(), current, false, NULL); // suspend all
1077   }
1078   return JVMTI_ERROR_NONE;
1079 } /* end SuspendAllVirtualThreads */
1080 
1081 
1082 jvmtiError
1083 JvmtiEnv::ResumeThread(jthread thread) {
1084   JavaThread* java_thread = NULL;
1085   oop thread_oop = NULL;
1086   JvmtiVTMTDisabler vtmt_disabler(true);
1087   ThreadsListHandle tlh;
1088 
1089   jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1090   if (err != JVMTI_ERROR_NONE) {
1091     return err;
1092   }
1093   err = resume_thread(thread_oop, java_thread, true); // single suspend
1094   return err;
1095 } /* end ResumeThread */
1096 
1097 
1098 // request_count - pre-checked to be greater than or equal to 0
1099 // request_list - pre-checked for NULL
1100 // results - pre-checked for NULL
1101 jvmtiError
1102 JvmtiEnv::ResumeThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
1103   oop thread_oop = NULL;
1104   JavaThread* java_thread = NULL;
1105   JvmtiVTMTDisabler vtmt_disabler(true);
1106   ThreadsListHandle tlh;
1107 
1108   for (int i = 0; i < request_count; i++) {
1109     jthread thread = request_list[i];
1110     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1111     if (err != JVMTI_ERROR_NONE) {
1112       if (thread_oop == NULL || err != JVMTI_ERROR_INVALID_THREAD) {
1113         results[i] = err;
1114         continue;
1115       }



1116     }
1117     results[i] = resume_thread(thread_oop, java_thread, true); // single suspend
1118   }
1119   // per-thread resume results returned via results parameter
1120   return JVMTI_ERROR_NONE;
1121 } /* end ResumeThreadList */
1122 
1123 
1124 jvmtiError
1125 JvmtiEnv::ResumeAllVirtualThreads(jint except_count, const jthread* except_list) {
1126   jvmtiError err = JvmtiEnvBase::check_thread_list(except_count, except_list);
1127   if (err != JVMTI_ERROR_NONE) {
1128     return err;
1129   }
1130   if (!JvmtiExport::can_support_virtual_threads()) {
1131     return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
1132   }
1133   ResourceMark rm;
1134   JvmtiVTMTDisabler vtmt_disabler(true);
1135   GrowableArray<jthread>* elist = new GrowableArray<jthread>(except_count);
1136 
1137   // Collect threads from except_list which suspended status must be restored.
1138   for (int idx = 0; idx < except_count; idx++) {
1139     jthread thread = except_list[idx];
1140     oop thread_oop = JNIHandles::resolve_external_guard(thread);
1141     if (JvmtiVTSuspender::is_vthread_suspended(thread_oop)) {
1142       // is suspended, so its suspended status must be restored
1143       elist->append(except_list[idx]);
1144     }
1145   }
1146 
1147   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *java_thread = jtiwh.next(); ) {
1148     oop vt_oop = java_thread->mounted_vthread();
1149     if (!java_thread->is_exiting() &&
1150         !java_thread->is_jvmti_agent_thread() &&
1151         !java_thread->is_hidden_from_external_view() &&
1152         vt_oop != NULL &&
1153         java_lang_VirtualThread::is_instance(vt_oop) &&
1154         JvmtiEnvBase::is_vthread_alive(vt_oop) &&
1155         JvmtiVTSuspender::is_vthread_suspended(vt_oop) &&
1156         !is_in_thread_list(except_count, except_list, vt_oop)
1157     ) {
1158       resume_thread(vt_oop, java_thread, false); // suspend all
1159     }
1160   }
1161   JvmtiVTSuspender::register_all_vthreads_resume();
1162 
1163   // Restore suspended state for threads from except list that were suspended before.
1164   for (int idx = 0; idx < elist->length(); idx++) {
1165     jthread thread = elist->at(idx);
1166     oop thread_oop = JNIHandles::resolve_external_guard(thread);
1167     if (!JvmtiVTSuspender::is_vthread_suspended(thread_oop)) {
1168       JvmtiVTSuspender::register_vthread_suspend(thread_oop);
1169     }
1170   }

1171   return JVMTI_ERROR_NONE;
1172 } /* end ResumeAllVirtualThreads */
1173 
1174 
1175 // java_thread - protected by ThreadsListHandle and pre-checked
1176 jvmtiError
1177 JvmtiEnv::StopThread(JavaThread* java_thread, jobject exception) {
1178   oop e = JNIHandles::resolve_external_guard(exception);
1179   NULL_CHECK(e, JVMTI_ERROR_NULL_POINTER);
1180 
1181   JavaThread::send_async_exception(java_thread, e);
1182 
1183   return JVMTI_ERROR_NONE;
1184 
1185 } /* end StopThread */
1186 
1187 

1188 jvmtiError
1189 JvmtiEnv::InterruptThread(jthread thread) {
1190   JavaThread* current_thread  = JavaThread::current();
1191   JavaThread* java_thread = NULL;
1192   oop thread_obj = NULL;
1193   HandleMark hm(current_thread);
1194 
1195   JvmtiVTMTDisabler vtmt_disabler;
1196   ThreadsListHandle tlh(current_thread);
1197 
1198   jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, &java_thread, &thread_obj);
1199   if (err != JVMTI_ERROR_NONE) {
1200     return err;
1201   }
1202 
1203   // Support for virtual threads
1204   if (java_lang_VirtualThread::is_instance(thread_obj)) {
1205     Handle obj(current_thread, thread_obj);
1206     JavaValue result(T_VOID);
1207     JavaCalls::call_virtual(&result,
1208                             obj,
1209                             vmClasses::Thread_klass(),
1210                             vmSymbols::interrupt_method_name(),
1211                             vmSymbols::void_method_signature(),
1212                             current_thread);
1213 
1214     return JVMTI_ERROR_NONE;
1215   }
1216 
1217   // Really this should be a Java call to Thread.interrupt to ensure the same
1218   // semantics, however historically this has not been done for some reason.
1219   // So we continue with that (which means we don't interact with any Java-level
1220   // Interruptible object) but we must set the Java-level interrupted state.
1221   java_lang_Thread::set_interrupted(thread_obj, true);
1222   java_thread->interrupt();
1223 
1224   return JVMTI_ERROR_NONE;
1225 } /* end InterruptThread */
1226 
1227 

1228 // info_ptr - pre-checked for NULL
1229 jvmtiError
1230 JvmtiEnv::GetThreadInfo(jthread thread, jvmtiThreadInfo* info_ptr) {
1231   JavaThread* current_thread = JavaThread::current();
1232   ResourceMark rm(current_thread);
1233   HandleMark hm(current_thread);
1234   JavaThread* java_thread = NULL;
1235   oop thread_oop = NULL;
1236 
1237   ThreadsListHandle tlh(current_thread);
1238 
1239   // if thread is NULL the current thread is used

1240   if (thread == NULL) {
1241     java_thread = JavaThread::current();
1242     thread_oop = get_vthread_or_thread_oop(java_thread);
1243     if (thread_oop == NULL || !thread_oop->is_a(vmClasses::Thread_klass())) {
1244       return JVMTI_ERROR_INVALID_THREAD;
1245     }
1246   } else {

1247     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1248     if (err != JVMTI_ERROR_NONE) {
1249       // We got an error code so we don't have a JavaThread *, but
1250       // only return an error from here if we didn't get a valid
1251       // thread_oop.
1252       // In the virtual thread case the cv_external_thread_to_JavaThread is expected to correctly set
1253       // the thread_oop and return JVMTI_ERROR_INVALID_THREAD which we ignore here.
1254       if (thread_oop == NULL) {
1255         return err;
1256       }

1257     }
1258   }
1259   // We have a valid thread_oop so we can return some thread info.
1260 
1261   Handle thread_obj(current_thread, thread_oop);
1262   Handle name;
1263   ThreadPriority priority;
1264   Handle     thread_group;
1265   Handle context_class_loader;
1266   bool          is_daemon;
1267 
1268   name = Handle(current_thread, java_lang_Thread::name(thread_obj()));
1269 
1270   // Support for virtual threads
1271   if (java_lang_VirtualThread::is_instance(thread_obj())) {
1272     priority = (ThreadPriority)JVMTI_THREAD_NORM_PRIORITY;
1273     is_daemon = true;
1274     if (java_lang_VirtualThread::state(thread_obj()) == java_lang_VirtualThread::TERMINATED) {
1275       thread_group = Handle(current_thread, NULL);
1276     } else {
1277       thread_group = Handle(current_thread, java_lang_Thread_VirtualThreads::get_THREAD_GROUP());
1278     }
1279   } else {
1280     priority = java_lang_Thread::priority(thread_obj());
1281     is_daemon = java_lang_Thread::is_daemon(thread_obj());
1282     if (java_lang_Thread::get_thread_status(thread_obj()) == JavaThreadStatus::TERMINATED) {
1283       thread_group = Handle(current_thread, NULL);
1284     } else {
1285       thread_group = Handle(current_thread, java_lang_Thread::threadGroup(thread_obj()));
1286     }
1287   }
1288 
1289   oop loader = java_lang_Thread::context_class_loader(thread_obj());
1290   if (loader == java_lang_Thread_ClassLoaders::get_NOT_SUPPORTED())
1291     loader = NULL;
1292   context_class_loader = Handle(current_thread, loader);
1293 
1294   { const char *n;
1295 
1296     if (name() != NULL) {
1297       n = java_lang_String::as_utf8_string(name());
1298     } else {
1299       int utf8_length = 0;
1300       n = UNICODE::as_utf8((jchar*) NULL, utf8_length);
1301     }
1302 
1303     info_ptr->name = (char *) jvmtiMalloc(strlen(n)+1);
1304     if (info_ptr->name == NULL)
1305       return JVMTI_ERROR_OUT_OF_MEMORY;
1306 
1307     strcpy(info_ptr->name, n);
1308   }
1309   info_ptr->is_daemon = is_daemon;
1310   info_ptr->priority  = priority;
1311 
1312   info_ptr->context_class_loader = (context_class_loader.is_null()) ? NULL :
1313                                     jni_reference(context_class_loader);
1314   info_ptr->thread_group = jni_reference(thread_group);
1315 
1316   return JVMTI_ERROR_NONE;
1317 } /* end GetThreadInfo */
1318 
1319 
1320 // Threads_lock NOT held
1321 // owned_monitor_count_ptr - pre-checked for NULL
1322 // owned_monitors_ptr - pre-checked for NULL
1323 jvmtiError
1324 JvmtiEnv::GetOwnedMonitorInfo(jthread thread, jint* owned_monitor_count_ptr, jobject** owned_monitors_ptr) {
1325   jvmtiError err = JVMTI_ERROR_NONE;
1326   JavaThread* calling_thread = JavaThread::current();
1327   JavaThread* java_thread = NULL;
1328   HandleMark hm(calling_thread);
1329   oop thread_oop = NULL;


1330 
1331   // growable array of jvmti monitors info on the C-heap
1332   GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list =
1333       new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<jvmtiMonitorStackDepthInfo*>(1, mtServiceability);
1334 
1335 
1336   JvmtiVTMTDisabler vtmt_disabler;
1337   ThreadsListHandle tlh(calling_thread);
1338 
1339   err = get_threadOop_and_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1340   if (err != JVMTI_ERROR_NONE) {
1341     delete owned_monitors_list;
1342     return err;
1343   }
1344 
1345   // Support for virtual threads
1346   if (java_lang_VirtualThread::is_instance(thread_oop)) {
1347     // there is no monitor info to collect if target virtual thread is unmounted
1348     if (java_thread != NULL) {
1349       VThreadGetOwnedMonitorInfoClosure op(this,
1350                                            Handle(calling_thread, thread_oop),
1351                                            owned_monitors_list);
1352       Handshake::execute(&op, java_thread);
1353       err = op.result();
1354     }
1355   } else {
1356     EscapeBarrier eb(true, calling_thread, java_thread);
1357     if (!eb.deoptimize_objects(MaxJavaStackTraceDepth)) {
1358       delete owned_monitors_list;
1359       return JVMTI_ERROR_OUT_OF_MEMORY;
1360     }
1361 
1362     if (java_thread == calling_thread) {
1363       // It is only safe to make a direct call on the current thread.
1364       // All other usage needs to use a direct handshake for safety.
1365       err = get_owned_monitors(calling_thread, java_thread, owned_monitors_list);
1366     } else {
1367       // get owned monitors info with handshake
1368       GetOwnedMonitorInfoClosure op(calling_thread, this, owned_monitors_list);
1369       Handshake::execute(&op, java_thread);
1370       err = op.result();
1371     }
1372   }
1373 
1374   jint owned_monitor_count = owned_monitors_list->length();
1375   if (err == JVMTI_ERROR_NONE) {
1376     if ((err = allocate(owned_monitor_count * sizeof(jobject *),
1377                       (unsigned char**)owned_monitors_ptr)) == JVMTI_ERROR_NONE) {
1378       // copy into the returned array
1379       for (int i = 0; i < owned_monitor_count; i++) {
1380         (*owned_monitors_ptr)[i] =
1381           ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->monitor;
1382       }
1383       *owned_monitor_count_ptr = owned_monitor_count;
1384     }
1385   }
1386   // clean up.
1387   for (int i = 0; i < owned_monitor_count; i++) {
1388     deallocate((unsigned char*)owned_monitors_list->at(i));
1389   }
1390   delete owned_monitors_list;
1391 
1392   return err;
1393 } /* end GetOwnedMonitorInfo */
1394 
1395 
1396 // Threads_lock NOT held
1397 // monitor_info_count_ptr - pre-checked for NULL
1398 // monitor_info_ptr - pre-checked for NULL
1399 jvmtiError
1400 JvmtiEnv::GetOwnedMonitorStackDepthInfo(jthread thread, jint* monitor_info_count_ptr, jvmtiMonitorStackDepthInfo** monitor_info_ptr) {
1401   jvmtiError err = JVMTI_ERROR_NONE;
1402   JavaThread* calling_thread = JavaThread::current();
1403   JavaThread* java_thread = NULL;
1404   HandleMark hm(calling_thread);
1405   oop thread_oop = NULL;


1406 
1407   // growable array of jvmti monitors info on the C-heap
1408   GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list =
1409          new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<jvmtiMonitorStackDepthInfo*>(1, mtServiceability);
1410 
1411   JvmtiVTMTDisabler vtmt_disabler;
1412   ThreadsListHandle tlh(calling_thread);
1413 
1414   err = get_threadOop_and_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1415   if (err != JVMTI_ERROR_NONE) {
1416     delete owned_monitors_list;
1417     return err;


1418   }
1419 
1420   // Support for virtual threads
1421   if (java_lang_VirtualThread::is_instance(thread_oop)) {
1422     // there is no monitor info to collect if target virtual thread is unmounted
1423     if (java_thread != NULL) {
1424       VThreadGetOwnedMonitorInfoClosure op(this,
1425                                            Handle(calling_thread, thread_oop),
1426                                            owned_monitors_list);
1427       Handshake::execute(&op, java_thread);
1428       err = op.result();
1429     }
1430   } else {
1431     EscapeBarrier eb(true, calling_thread, java_thread);
1432     if (!eb.deoptimize_objects(MaxJavaStackTraceDepth)) {
1433       delete owned_monitors_list;
1434       return JVMTI_ERROR_OUT_OF_MEMORY;
1435     }
1436 
1437     if (java_thread == calling_thread) {
1438       // It is only safe to make a direct call on the current thread.
1439       // All other usage needs to use a direct handshake for safety.
1440       err = get_owned_monitors(calling_thread, java_thread, owned_monitors_list);
1441     } else {
1442       // get owned monitors info with handshake
1443       GetOwnedMonitorInfoClosure op(calling_thread, this, owned_monitors_list);
1444       Handshake::execute(&op, java_thread);
1445       err = op.result();
1446     }
1447   }
1448   jint owned_monitor_count = owned_monitors_list->length();
1449   if (err == JVMTI_ERROR_NONE) {
1450     if ((err = allocate(owned_monitor_count * sizeof(jvmtiMonitorStackDepthInfo),
1451                         (unsigned char**)monitor_info_ptr)) == JVMTI_ERROR_NONE) {
1452       // copy to output array.
1453       for (int i = 0; i < owned_monitor_count; i++) {
1454         (*monitor_info_ptr)[i].monitor =
1455           ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->monitor;
1456         (*monitor_info_ptr)[i].stack_depth =
1457           ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->stack_depth;
1458       }
1459     }
1460     *monitor_info_count_ptr = owned_monitor_count;
1461   }
1462 
1463   // clean up.
1464   for (int i = 0; i < owned_monitor_count; i++) {
1465     deallocate((unsigned char*)owned_monitors_list->at(i));
1466   }
1467   delete owned_monitors_list;
1468 
1469   return err;
1470 } /* end GetOwnedMonitorStackDepthInfo */
1471 
1472 
1473 // Threads_lock NOT held
1474 // monitor_ptr - pre-checked for NULL
1475 jvmtiError
1476 JvmtiEnv::GetCurrentContendedMonitor(jthread thread, jobject* monitor_ptr) {
1477   jvmtiError err = JVMTI_ERROR_NONE;
1478   JavaThread* calling_thread = JavaThread::current();
1479   JavaThread* java_thread = NULL;
1480   HandleMark hm(calling_thread);
1481   oop thread_oop = NULL;
1482 
1483   JvmtiVTMTDisabler vtmt_disabler;
1484   ThreadsListHandle tlh(calling_thread);
1485 
1486   err = get_threadOop_and_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1487   if (err != JVMTI_ERROR_NONE) {
1488     return err;
1489   }
1490 
1491   // Support for virtual threads
1492   if (java_lang_VirtualThread::is_instance(thread_oop)) {
1493     // there is no monitor info to collect if target virtual thread is unmounted
1494     if (java_thread != NULL) {
1495       VThreadGetCurrentContendedMonitorClosure op(this,
1496                                                   Handle(calling_thread, thread_oop),
1497                                                   monitor_ptr);
1498       Handshake::execute(&op, java_thread);
1499       err = op.result();
1500     } else {
1501       *monitor_ptr = NULL;
1502       if (!JvmtiEnvBase::is_vthread_alive(thread_oop)) {
1503         err = JVMTI_ERROR_THREAD_NOT_ALIVE;
1504       }
1505     }
1506     return err;
1507   }
1508   if (java_thread == calling_thread) {
1509     // It is only safe to make a direct call on the current thread.
1510     // All other usage needs to use a direct handshake for safety.
1511     err = get_current_contended_monitor(calling_thread, java_thread, monitor_ptr, false);
1512   } else {
1513     // get contended monitor information with handshake
1514     GetCurrentContendedMonitorClosure op(calling_thread, this, monitor_ptr);
1515     Handshake::execute(&op, java_thread);
1516     err = op.result();
1517   }
1518   return err;
1519 } /* end GetCurrentContendedMonitor */
1520 
1521 

1522 // proc - pre-checked for NULL
1523 // arg - NULL is a valid value, must be checked
1524 jvmtiError
1525 JvmtiEnv::RunAgentThread(jthread thread, jvmtiStartFunction proc, const void* arg, jint priority) {
1526   JavaThread* current_thread = JavaThread::current();
1527 
1528   JavaThread* java_thread = NULL;
1529   oop thread_oop = NULL;
1530   ThreadsListHandle tlh(current_thread);
1531   jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1532   if (err != JVMTI_ERROR_NONE) {
1533     // We got an error code so we don't have a JavaThread *, but
1534     // only return an error from here if we didn't get a valid
1535     // thread_oop.
1536     if (thread_oop == NULL) {
1537       return err;
1538     }
1539     // We have a valid thread_oop.
1540   }
1541 
1542   if (java_thread != NULL) {
1543     // 'thread' refers to an existing JavaThread.
1544     return JVMTI_ERROR_INVALID_THREAD;
1545   }
1546   if (java_lang_VirtualThread::is_instance(thread_oop)) {
1547     return JVMTI_ERROR_INVALID_THREAD;
1548   }
1549 
1550   if (priority < JVMTI_THREAD_MIN_PRIORITY || priority > JVMTI_THREAD_MAX_PRIORITY) {
1551     return JVMTI_ERROR_INVALID_PRIORITY;
1552   }
1553 
1554   Handle thread_hndl(current_thread, thread_oop);
1555 
1556   JvmtiAgentThread* new_thread = new JvmtiAgentThread(this, proc, arg);
1557 
1558   // At this point it may be possible that no osthread was created for the
1559   // JavaThread due to lack of resources.
1560   if (new_thread->osthread() == NULL) {
1561     // The new thread is not known to Thread-SMR yet so we can just delete.
1562     delete new_thread;
1563     return JVMTI_ERROR_OUT_OF_MEMORY;
1564   }
1565 
1566   JavaThread::start_internal_daemon(current_thread, new_thread, thread_hndl,
1567                                     (ThreadPriority)priority);
1568 
1569   return JVMTI_ERROR_NONE;
1570 } /* end RunAgentThread */
1571 
1572   //
1573   // Thread Group functions
1574   //
1575 
1576 // group_count_ptr - pre-checked for NULL
1577 // groups_ptr - pre-checked for NULL
1578 jvmtiError
1579 JvmtiEnv::GetTopThreadGroups(jint* group_count_ptr, jthreadGroup** groups_ptr) {
1580   JavaThread* current_thread = JavaThread::current();
1581 
1582   // Only one top level thread group now.
1583   *group_count_ptr = 1;
1584 
1585   // Allocate memory to store global-refs to the thread groups.
1586   // Assume this area is freed by caller.
1587   *groups_ptr = (jthreadGroup *) jvmtiMalloc((sizeof(jthreadGroup)) * (*group_count_ptr));
1588 
1589   NULL_CHECK(*groups_ptr, JVMTI_ERROR_OUT_OF_MEMORY);
1590 
1591   // Convert oop to Handle, then convert Handle to global-ref.
1592   {
1593     HandleMark hm(current_thread);
1594     Handle system_thread_group(current_thread, Universe::system_thread_group());
1595     *groups_ptr[0] = jni_reference(system_thread_group);
1596   }
1597 
1598   return JVMTI_ERROR_NONE;
1599 } /* end GetTopThreadGroups */
1600 
1601 
1602 // info_ptr - pre-checked for NULL
1603 jvmtiError
1604 JvmtiEnv::GetThreadGroupInfo(jthreadGroup group, jvmtiThreadGroupInfo* info_ptr) {
1605   Thread* current_thread = Thread::current();
1606   ResourceMark rm(current_thread);
1607   HandleMark hm(current_thread);
1608 
1609   Handle group_obj (current_thread, JNIHandles::resolve_external_guard(group));
1610   NULL_CHECK(group_obj(), JVMTI_ERROR_INVALID_THREAD_GROUP);
1611 
1612   const char* name;
1613   Handle parent_group;

1614   ThreadPriority max_priority;
1615 
1616   name         = java_lang_ThreadGroup::name(group_obj());
1617   parent_group = Handle(current_thread, java_lang_ThreadGroup::parent(group_obj()));

1618   max_priority = java_lang_ThreadGroup::maxPriority(group_obj());
1619 




1620   if (name != NULL) {
1621     info_ptr->name = (char*)jvmtiMalloc(strlen(name)+1);
1622     NULL_CHECK(info_ptr->name, JVMTI_ERROR_OUT_OF_MEMORY);
1623     strcpy(info_ptr->name, name);
1624   } else {
1625     info_ptr->name = NULL;
1626   }
1627 
1628   info_ptr->parent       = jni_reference(parent_group);
1629   info_ptr->max_priority = max_priority;
1630   info_ptr->is_daemon    = JNI_FALSE;
1631 
1632   return JVMTI_ERROR_NONE;
1633 } /* end GetThreadGroupInfo */
1634 

1635 // thread_count_ptr - pre-checked for NULL
1636 // threads_ptr - pre-checked for NULL
1637 // group_count_ptr - pre-checked for NULL
1638 // groups_ptr - pre-checked for NULL
1639 jvmtiError
1640 JvmtiEnv::GetThreadGroupChildren(jthreadGroup group, jint* thread_count_ptr, jthread** threads_ptr, jint* group_count_ptr, jthreadGroup** groups_ptr) {
1641   JavaThread* current_thread = JavaThread::current();
1642   oop group_obj = JNIHandles::resolve_external_guard(group);
1643   NULL_CHECK(group_obj, JVMTI_ERROR_INVALID_THREAD_GROUP);
1644 
1645   Handle *thread_objs = NULL;
1646   Handle *group_objs  = NULL;
1647   int nthreads = 0;
1648   int ngroups = 0;
1649   int hidden_threads = 0;
1650 
1651   ResourceMark rm(current_thread);
1652   HandleMark hm(current_thread);
1653 
1654   Handle group_hdl(current_thread, group_obj);
1655 
1656   nthreads = get_live_threads(current_thread, group_hdl, &thread_objs);
1657   ngroups = get_subgroups(current_thread, group_hdl, &group_objs);
















































1658 
1659   *group_count_ptr  = ngroups;
1660   *thread_count_ptr = nthreads;
1661   *threads_ptr     = new_jthreadArray(nthreads, thread_objs);
1662   *groups_ptr      = new_jthreadGroupArray(ngroups, group_objs);
1663   if ((nthreads > 0) && (*threads_ptr == NULL)) {
1664     return JVMTI_ERROR_OUT_OF_MEMORY;
1665   }
1666   if ((ngroups > 0) && (*groups_ptr == NULL)) {
1667     return JVMTI_ERROR_OUT_OF_MEMORY;
1668   }
1669 
1670   return JVMTI_ERROR_NONE;
1671 } /* end GetThreadGroupChildren */
1672 
1673 
1674   //
1675   // Stack Frame functions
1676   //
1677 
1678 // Threads_lock NOT held
1679 // max_frame_count - pre-checked to be greater than or equal to 0
1680 // frame_buffer - pre-checked for NULL
1681 // count_ptr - pre-checked for NULL
1682 jvmtiError
1683 JvmtiEnv::GetStackTrace(jthread thread, jint start_depth, jint max_frame_count, jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
1684   jvmtiError err = JVMTI_ERROR_NONE;
1685   JavaThread* java_thread = NULL;
1686   JavaThread* current_thread = JavaThread::current();
1687   HandleMark hm(current_thread);
1688   oop thread_obj = NULL;
1689 
1690   JvmtiVTMTDisabler vtmt_disabler;
1691   ThreadsListHandle tlh(current_thread);
1692 
1693   err = get_threadOop_and_JavaThread(tlh.list(), thread, &java_thread, &thread_obj);
1694   if (err != JVMTI_ERROR_NONE) {
1695     return err;
1696   }
1697 
1698   // Support for virtual threads
1699   if (java_lang_VirtualThread::is_instance(thread_obj)) {
1700     if (java_thread == NULL) { // target virtual thread is unmounted
1701       ResourceMark rm(current_thread);
1702 
1703       VM_VThreadGetStackTrace op(this, Handle(current_thread, thread_obj),
1704                                  start_depth, max_frame_count,
1705                                  frame_buffer, count_ptr);
1706       VMThread::execute(&op);
1707       return op.result();
1708     }
1709     VThreadGetStackTraceClosure op(this, Handle(current_thread, thread_obj),
1710                                    start_depth, max_frame_count, frame_buffer, count_ptr);
1711     Handshake::execute(&op, java_thread);
1712     return op.result();
1713   }
1714 
1715   // It is only safe to perform the direct operation on the current
1716   // thread. All other usage needs to use a direct handshake for safety.
1717   if (java_thread == JavaThread::current()) {
1718     err = get_stack_trace(java_thread, start_depth, max_frame_count, frame_buffer, count_ptr);
1719   } else {
1720     // Get stack trace with handshake.
1721     GetStackTraceClosure op(this, start_depth, max_frame_count, frame_buffer, count_ptr);
1722     Handshake::execute(&op, java_thread);
1723     err = op.result();
1724   }
1725 
1726   return err;
1727 } /* end GetStackTrace */
1728 
1729 
1730 // max_frame_count - pre-checked to be greater than or equal to 0
1731 // stack_info_ptr - pre-checked for NULL
1732 // thread_count_ptr - pre-checked for NULL
1733 jvmtiError
1734 JvmtiEnv::GetAllStackTraces(jint max_frame_count, jvmtiStackInfo** stack_info_ptr, jint* thread_count_ptr) {
1735   jvmtiError err = JVMTI_ERROR_NONE;
1736   JavaThread* calling_thread = JavaThread::current();
1737 
1738   // JVMTI get stack traces at safepoint.
1739   VM_GetAllStackTraces op(this, calling_thread, max_frame_count);
1740   VMThread::execute(&op);
1741   *thread_count_ptr = op.final_thread_count();
1742   *stack_info_ptr = op.stack_info();
1743   err = op.result();
1744   return err;
1745 } /* end GetAllStackTraces */
1746 
1747 
1748 // thread_count - pre-checked to be greater than or equal to 0
1749 // thread_list - pre-checked for NULL
1750 // max_frame_count - pre-checked to be greater than or equal to 0
1751 // stack_info_ptr - pre-checked for NULL
1752 jvmtiError
1753 JvmtiEnv::GetThreadListStackTraces(jint thread_count, const jthread* thread_list, jint max_frame_count, jvmtiStackInfo** stack_info_ptr) {
1754   jvmtiError err = JVMTI_ERROR_NONE;
1755 
1756   if (thread_count == 1) {
1757     JvmtiVTMTDisabler vtmt_disabler;
1758 
1759     // Use direct handshake if we need to get only one stack trace.
1760     JavaThread *current_thread = JavaThread::current();
1761     ThreadsListHandle tlh(current_thread);
1762     jthread thread = thread_list[0];
1763     JavaThread *java_thread;
1764     oop thread_obj = NULL;
1765     err = get_threadOop_and_JavaThread(tlh.list(), thread, &java_thread, &thread_obj);
1766     if (err != JVMTI_ERROR_NONE) {
1767       return err;
1768     }
1769 
1770     // Support for virtual threads
1771     if (java_lang_VirtualThread::is_instance(thread_obj)) {
1772       if (java_thread == NULL) { // target virtual thread is unmounted
1773         ResourceMark rm(current_thread);
1774         MultipleStackTracesCollector collector(this, max_frame_count);
1775         collector.fill_frames(thread, java_thread, thread_obj);
1776         collector.allocate_and_fill_stacks(1);
1777         *stack_info_ptr = collector.stack_info();
1778         return collector.result();
1779       }
1780     }
1781 
1782     GetSingleStackTraceClosure op(this, current_thread, thread, max_frame_count);
1783     Handshake::execute(&op, &tlh, java_thread);
1784     err = op.result();
1785     if (err == JVMTI_ERROR_NONE) {
1786       *stack_info_ptr = op.stack_info();
1787     }
1788   } else {
1789     // JVMTI get stack traces at safepoint.
1790     VM_GetThreadListStackTraces op(this, thread_count, thread_list, max_frame_count);
1791     VMThread::execute(&op);
1792     err = op.result();
1793     if (err == JVMTI_ERROR_NONE) {
1794       *stack_info_ptr = op.stack_info();
1795     }
1796   }
1797   return err;
1798 } /* end GetThreadListStackTraces */
1799 
1800 
1801 // Threads_lock NOT held
1802 // count_ptr - pre-checked for NULL
1803 jvmtiError
1804 JvmtiEnv::GetFrameCount(jthread thread, jint* count_ptr) {
1805   jvmtiError err = JVMTI_ERROR_NONE;
1806   JavaThread* java_thread = NULL;
1807   JavaThread* current_thread = JavaThread::current();
1808   HandleMark hm(current_thread);
1809   oop thread_obj = NULL;
1810 
1811   JvmtiVTMTDisabler vtmt_disabler;
1812   ThreadsListHandle tlh(current_thread);
1813 
1814   err = get_threadOop_and_JavaThread(tlh.list(), thread, &java_thread, &thread_obj);
1815   if (err != JVMTI_ERROR_NONE) {
1816     return err;
1817   }
1818 
1819   // Support for virtual threads
1820   if (java_lang_VirtualThread::is_instance(thread_obj)) {
1821     if (java_thread == NULL) { // target virtual thread is unmounted
1822       VM_VThreadGetFrameCount op(this, Handle(current_thread, thread_obj),  count_ptr);
1823       VMThread::execute(&op);
1824       return op.result();
1825     }
1826     VThreadGetFrameCountClosure op(this, Handle(current_thread, thread_obj), count_ptr);
1827     Handshake::execute(&op, java_thread);
1828     return op.result();
1829   }
1830 
1831   // It is only safe to perform the direct operation on the current
1832   // thread. All other usage needs to use a direct handshake for safety.
1833   if (java_thread == JavaThread::current()) {
1834     err = get_frame_count(java_thread, count_ptr);
1835   } else {
1836     // get java stack frame count with handshake.
1837     GetFrameCountClosure op(this, count_ptr);
1838     Handshake::execute(&op, java_thread);
1839     err = op.result();
1840   }
1841   return err;
1842 } /* end GetFrameCount */
1843 
1844 
1845 // java_thread - protected by ThreadsListHandle and pre-checked
1846 jvmtiError
1847 JvmtiEnv::PopFrame(JavaThread* java_thread) {
1848   JvmtiVTMTDisabler vtmt_disabler;
1849   // retrieve or create the state
1850   JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
1851   if (state == NULL) {
1852     return JVMTI_ERROR_THREAD_NOT_ALIVE;
1853   }
1854 
1855   // Eagerly reallocate scalar replaced objects.
1856   JavaThread* current_thread = JavaThread::current();
1857   EscapeBarrier eb(true, current_thread, java_thread);
1858   if (!eb.deoptimize_objects(1)) {
1859     // Reallocation of scalar replaced objects failed -> return with error
1860     return JVMTI_ERROR_OUT_OF_MEMORY;
1861   }
1862 
1863   MutexLocker mu(JvmtiThreadState_lock);
1864   UpdateForPopTopFrameClosure op(state);
1865   if (java_thread == current_thread) {
1866     op.doit(java_thread, true /* self */);
1867   } else {
1868     Handshake::execute(&op, java_thread);
1869   }
1870   return op.result();
1871 } /* end PopFrame */
1872 
1873 
1874 // Threads_lock NOT held
1875 // depth - pre-checked as non-negative
1876 // method_ptr - pre-checked for NULL
1877 // location_ptr - pre-checked for NULL
1878 jvmtiError
1879 JvmtiEnv::GetFrameLocation(jthread thread, jint depth, jmethodID* method_ptr, jlocation* location_ptr) {
1880   jvmtiError err = JVMTI_ERROR_NONE;
1881   JavaThread* java_thread = NULL;
1882   JavaThread* current_thread = JavaThread::current();
1883   HandleMark hm(current_thread);
1884   oop thread_obj = NULL;
1885 
1886   JvmtiVTMTDisabler vtmt_disabler;
1887   ThreadsListHandle tlh(current_thread);
1888 
1889   err = get_threadOop_and_JavaThread(tlh.list(), thread, &java_thread, &thread_obj);
1890   if (err != JVMTI_ERROR_NONE) {
1891     return err;
1892   }
1893 
1894   // Support for virtual threads
1895   if (java_lang_VirtualThread::is_instance(thread_obj)) {
1896     if (java_thread == NULL) { // target virtual thread is unmounted
1897       err = get_frame_location(thread_obj, depth, method_ptr, location_ptr);
1898       return err;
1899     }
1900     VThreadGetFrameLocationClosure op(this, Handle(current_thread, thread_obj),
1901                                       depth, method_ptr, location_ptr);
1902     Handshake::execute(&op, java_thread);
1903     return op.result();
1904   }
1905 
1906   // It is only safe to perform the direct operation on the current
1907   // thread. All other usage needs to use a direct handshake for safety.
1908   if (java_thread == JavaThread::current()) {
1909     err = get_frame_location(java_thread, depth, method_ptr, location_ptr);
1910   } else {
1911     // JVMTI get java stack frame location via direct handshake.
1912     GetFrameLocationClosure op(this, depth, method_ptr, location_ptr);
1913     Handshake::execute(&op, java_thread);
1914     err = op.result();
1915   }
1916   return err;
1917 } /* end GetFrameLocation */
1918 
1919 
1920 // Threads_lock NOT held, java_thread not protected by lock
1921 // depth - pre-checked as non-negative
1922 jvmtiError
1923 JvmtiEnv::NotifyFramePop(jthread thread, jint depth) {
1924   jvmtiError err = JVMTI_ERROR_NONE;
1925   ResourceMark rm;
1926   JavaThread* java_thread = NULL;
1927   oop thread_obj = NULL;
1928   JvmtiVTMTDisabler vtmt_disabler;
1929   ThreadsListHandle tlh;
1930 
1931   err = get_threadOop_and_JavaThread(tlh.list(), thread, &java_thread, &thread_obj);
1932   if (err != JVMTI_ERROR_NONE) {
1933     return err;
1934   }
1935 
1936   JavaThread* current = JavaThread::current();
1937   HandleMark hm(current);
1938   Handle thread_handle(current, thread_obj);
1939 
1940   // Support for virtual threads
1941   if (java_lang_VirtualThread::is_instance(thread_obj)) {
1942     if (java_thread == NULL) {
1943       // java_thread is NULL if virtual thread is unmounted
1944       JvmtiThreadState *state = JvmtiThreadState::state_for(java_thread, thread_handle);
1945       if (state == NULL) {
1946         return JVMTI_ERROR_THREAD_NOT_ALIVE;
1947       }
1948       MutexLocker mu(JvmtiThreadState_lock);
1949       int frame_number = state->count_frames() - depth;
1950       state->env_thread_state(this)->set_frame_pop(frame_number);
1951       return JVMTI_ERROR_NONE;
1952     }
1953   }
1954 
1955   JvmtiThreadState *state = JvmtiThreadState::state_for(java_thread, thread_handle);
1956   if (state == NULL) {
1957     return JVMTI_ERROR_THREAD_NOT_ALIVE;
1958   }
1959 
1960   SetFramePopClosure op(this, state, depth);
1961   MutexLocker mu(current, JvmtiThreadState_lock);
1962   if (java_thread == current) {
1963     op.doit(java_thread, true /* self */);
1964   } else {
1965     Handshake::execute(&op, java_thread);
1966   }
1967   return op.result();
1968 } /* end NotifyFramePop */
1969 
1970 
1971   //
1972   // Force Early Return functions
1973   //
1974 
1975 // java_thread - protected by ThreadsListHandle and pre-checked
1976 jvmtiError
1977 JvmtiEnv::ForceEarlyReturnObject(JavaThread* java_thread, jobject value) {
1978   jvalue val;
1979   val.l = value;
1980   return force_early_return(java_thread, val, atos);
1981 } /* end ForceEarlyReturnObject */
1982 
1983 
1984 // java_thread - protected by ThreadsListHandle and pre-checked
1985 jvmtiError
1986 JvmtiEnv::ForceEarlyReturnInt(JavaThread* java_thread, jint value) {
1987   jvalue val;
1988   val.i = value;
1989   return force_early_return(java_thread, val, itos);
1990 } /* end ForceEarlyReturnInt */
1991 
1992 
1993 // java_thread - protected by ThreadsListHandle and pre-checked
1994 jvmtiError
1995 JvmtiEnv::ForceEarlyReturnLong(JavaThread* java_thread, jlong value) {
1996   jvalue val;
1997   val.j = value;
1998   return force_early_return(java_thread, val, ltos);
1999 } /* end ForceEarlyReturnLong */
2000 
2001 
2002 // java_thread - protected by ThreadsListHandle and pre-checked
2003 jvmtiError
2004 JvmtiEnv::ForceEarlyReturnFloat(JavaThread* java_thread, jfloat value) {
2005   jvalue val;
2006   val.f = value;
2007   return force_early_return(java_thread, val, ftos);
2008 } /* end ForceEarlyReturnFloat */
2009 
2010 
2011 // java_thread - protected by ThreadsListHandle and pre-checked
2012 jvmtiError
2013 JvmtiEnv::ForceEarlyReturnDouble(JavaThread* java_thread, jdouble value) {
2014   jvalue val;
2015   val.d = value;
2016   return force_early_return(java_thread, val, dtos);
2017 } /* end ForceEarlyReturnDouble */
2018 
2019 
2020 // java_thread - protected by ThreadsListHandle and pre-checked
2021 jvmtiError
2022 JvmtiEnv::ForceEarlyReturnVoid(JavaThread* java_thread) {
2023   jvalue val;
2024   val.j = 0L;
2025   return force_early_return(java_thread, val, vtos);
2026 } /* end ForceEarlyReturnVoid */
2027 
2028 
2029   //
2030   // Heap functions
2031   //
2032 
2033 // klass - NULL is a valid value, must be checked
2034 // initial_object - NULL is a valid value, must be checked
2035 // callbacks - pre-checked for NULL
2036 // user_data - NULL is a valid value, must be checked
2037 jvmtiError
2038 JvmtiEnv::FollowReferences(jint heap_filter, jclass klass, jobject initial_object, const jvmtiHeapCallbacks* callbacks, const void* user_data) {
2039   // check klass if provided
2040   Klass* k = NULL;
2041   if (klass != NULL) {
2042     oop k_mirror = JNIHandles::resolve_external_guard(klass);
2043     if (k_mirror == NULL) {
2044       return JVMTI_ERROR_INVALID_CLASS;
2045     }
2046     if (java_lang_Class::is_primitive(k_mirror)) {
2047       return JVMTI_ERROR_NONE;
2048     }
2049     k = java_lang_Class::as_Klass(k_mirror);
2050     if (klass == NULL) {
2051       return JVMTI_ERROR_INVALID_CLASS;
2052     }
2053   }
2054 
2055   if (initial_object != NULL) {
2056     oop init_obj = JNIHandles::resolve_external_guard(initial_object);
2057     if (init_obj == NULL) {
2058       return JVMTI_ERROR_INVALID_OBJECT;
2059     }
2060   }
2061 
2062   Thread *thread = Thread::current();
2063   HandleMark hm(thread);
2064 
2065   TraceTime t("FollowReferences", TRACETIME_LOG(Debug, jvmti, objecttagging));
2066   JvmtiTagMap::tag_map_for(this)->follow_references(heap_filter, k, initial_object, callbacks, user_data);
2067   return JVMTI_ERROR_NONE;
2068 } /* end FollowReferences */
2069 
2070 
2071 // klass - NULL is a valid value, must be checked
2072 // callbacks - pre-checked for NULL
2073 // user_data - NULL is a valid value, must be checked
2074 jvmtiError
2075 JvmtiEnv::IterateThroughHeap(jint heap_filter, jclass klass, const jvmtiHeapCallbacks* callbacks, const void* user_data) {
2076   // check klass if provided
2077   Klass* k = NULL;
2078   if (klass != NULL) {
2079     oop k_mirror = JNIHandles::resolve_external_guard(klass);
2080     if (k_mirror == NULL) {
2081       return JVMTI_ERROR_INVALID_CLASS;
2082     }
2083     if (java_lang_Class::is_primitive(k_mirror)) {
2084       return JVMTI_ERROR_NONE;
2085     }
2086     k = java_lang_Class::as_Klass(k_mirror);
2087     if (k == NULL) {
2088       return JVMTI_ERROR_INVALID_CLASS;
2089     }
2090   }
2091 
2092   TraceTime t("IterateThroughHeap", TRACETIME_LOG(Debug, jvmti, objecttagging));
2093   JvmtiTagMap::tag_map_for(this)->iterate_through_heap(heap_filter, k, callbacks, user_data);
2094   return JVMTI_ERROR_NONE;
2095 } /* end IterateThroughHeap */
2096 
2097 
2098 // tag_ptr - pre-checked for NULL
2099 jvmtiError
2100 JvmtiEnv::GetTag(jobject object, jlong* tag_ptr) {
2101   oop o = JNIHandles::resolve_external_guard(object);
2102   NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
2103   *tag_ptr = JvmtiTagMap::tag_map_for(this)->get_tag(object);
2104   return JVMTI_ERROR_NONE;
2105 } /* end GetTag */
2106 
2107 
2108 jvmtiError
2109 JvmtiEnv::SetTag(jobject object, jlong tag) {
2110   oop o = JNIHandles::resolve_external_guard(object);
2111   NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
2112   JvmtiTagMap::tag_map_for(this)->set_tag(object, tag);
2113   return JVMTI_ERROR_NONE;
2114 } /* end SetTag */
2115 
2116 
2117 // tag_count - pre-checked to be greater than or equal to 0
2118 // tags - pre-checked for NULL
2119 // count_ptr - pre-checked for NULL
2120 // object_result_ptr - NULL is a valid value, must be checked
2121 // tag_result_ptr - NULL is a valid value, must be checked
2122 jvmtiError
2123 JvmtiEnv::GetObjectsWithTags(jint tag_count, const jlong* tags, jint* count_ptr, jobject** object_result_ptr, jlong** tag_result_ptr) {
2124   TraceTime t("GetObjectsWithTags", TRACETIME_LOG(Debug, jvmti, objecttagging));
2125   return JvmtiTagMap::tag_map_for(this)->get_objects_with_tags((jlong*)tags, tag_count, count_ptr, object_result_ptr, tag_result_ptr);
2126 } /* end GetObjectsWithTags */
2127 
2128 
2129 jvmtiError
2130 JvmtiEnv::ForceGarbageCollection() {
2131   Universe::heap()->collect(GCCause::_jvmti_force_gc);
2132   return JVMTI_ERROR_NONE;
2133 } /* end ForceGarbageCollection */
2134 
2135 
2136   //
2137   // Heap (1.0) functions
2138   //
2139 
2140 // object_reference_callback - pre-checked for NULL
2141 // user_data - NULL is a valid value, must be checked
2142 jvmtiError
2143 JvmtiEnv::IterateOverObjectsReachableFromObject(jobject object, jvmtiObjectReferenceCallback object_reference_callback, const void* user_data) {
2144   oop o = JNIHandles::resolve_external_guard(object);
2145   NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
2146   JvmtiTagMap::tag_map_for(this)->iterate_over_objects_reachable_from_object(object, object_reference_callback, user_data);
2147   return JVMTI_ERROR_NONE;
2148 } /* end IterateOverObjectsReachableFromObject */
2149 
2150 
2151 // heap_root_callback - NULL is a valid value, must be checked
2152 // stack_ref_callback - NULL is a valid value, must be checked
2153 // object_ref_callback - NULL is a valid value, must be checked
2154 // user_data - NULL is a valid value, must be checked
2155 jvmtiError
2156 JvmtiEnv::IterateOverReachableObjects(jvmtiHeapRootCallback heap_root_callback, jvmtiStackReferenceCallback stack_ref_callback, jvmtiObjectReferenceCallback object_ref_callback, const void* user_data) {
2157   TraceTime t("IterateOverReachableObjects", TRACETIME_LOG(Debug, jvmti, objecttagging));
2158   JvmtiTagMap::tag_map_for(this)->iterate_over_reachable_objects(heap_root_callback, stack_ref_callback, object_ref_callback, user_data);
2159   return JVMTI_ERROR_NONE;
2160 } /* end IterateOverReachableObjects */
2161 
2162 
2163 // heap_object_callback - pre-checked for NULL
2164 // user_data - NULL is a valid value, must be checked
2165 jvmtiError
2166 JvmtiEnv::IterateOverHeap(jvmtiHeapObjectFilter object_filter, jvmtiHeapObjectCallback heap_object_callback, const void* user_data) {
2167   TraceTime t("IterateOverHeap", TRACETIME_LOG(Debug, jvmti, objecttagging));
2168   Thread *thread = Thread::current();
2169   HandleMark hm(thread);
2170   JvmtiTagMap::tag_map_for(this)->iterate_over_heap(object_filter, NULL, heap_object_callback, user_data);
2171   return JVMTI_ERROR_NONE;
2172 } /* end IterateOverHeap */
2173 
2174 
2175 // k_mirror - may be primitive, this must be checked
2176 // heap_object_callback - pre-checked for NULL
2177 // user_data - NULL is a valid value, must be checked
2178 jvmtiError
2179 JvmtiEnv::IterateOverInstancesOfClass(oop k_mirror, jvmtiHeapObjectFilter object_filter, jvmtiHeapObjectCallback heap_object_callback, const void* user_data) {
2180   if (java_lang_Class::is_primitive(k_mirror)) {
2181     // DO PRIMITIVE CLASS PROCESSING
2182     return JVMTI_ERROR_NONE;
2183   }
2184   Klass* klass = java_lang_Class::as_Klass(k_mirror);
2185   if (klass == NULL) {
2186     return JVMTI_ERROR_INVALID_CLASS;
2187   }
2188   TraceTime t("IterateOverInstancesOfClass", TRACETIME_LOG(Debug, jvmti, objecttagging));
2189   JvmtiTagMap::tag_map_for(this)->iterate_over_heap(object_filter, klass, heap_object_callback, user_data);
2190   return JVMTI_ERROR_NONE;
2191 } /* end IterateOverInstancesOfClass */
2192 
2193 
2194   //
2195   // Local Variable functions
2196   //
2197 
2198 // Threads_lock NOT held
2199 // depth - pre-checked as non-negative
2200 // value_ptr - pre-checked for NULL
2201 jvmtiError
2202 JvmtiEnv::GetLocalObject(jthread thread, jint depth, jint slot, jobject* value_ptr) {
2203   jvmtiError err = JVMTI_ERROR_NONE;
2204   JavaThread* java_thread = NULL;
2205   JavaThread* current_thread = JavaThread::current();
2206   // rm object is created to clean up the javaVFrame created in
2207   // doit_prologue(), but after doit() is finished with it.
2208   ResourceMark rm(current_thread);
2209   HandleMark hm(current_thread);
2210   oop thread_obj = JNIHandles::resolve_external_guard(thread);
2211 
2212   if (java_lang_VirtualThread::is_instance(thread_obj)) {
2213     // Support for virtual threads
2214     VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2215                                    current_thread, depth, slot);
2216     VMThread::execute(&op);
2217     err = op.result();
2218     if (err == JVMTI_ERROR_NONE) {
2219       *value_ptr = op.value().l;
2220     }
2221   } else {
2222     // Support for ordinary threads
2223     ThreadsListHandle tlh(current_thread);
2224     err = get_JavaThread(tlh.list(), thread, &java_thread);
2225     if (err != JVMTI_ERROR_NONE) {
2226       return err;
2227     }
2228     VM_GetOrSetLocal op(java_thread, current_thread, depth, slot);
2229     VMThread::execute(&op);
2230     err = op.result();
2231     if (err == JVMTI_ERROR_NONE) {
2232       *value_ptr = op.value().l;
2233     }
2234   }
2235   return err;
2236 } /* end GetLocalObject */
2237 
2238 // Threads_lock NOT held
2239 // depth - pre-checked as non-negative
2240 // value - pre-checked for NULL
2241 jvmtiError
2242 JvmtiEnv::GetLocalInstance(jthread thread, jint depth, jobject* value_ptr){
2243   jvmtiError err = JVMTI_ERROR_NONE;
2244   JavaThread* java_thread = NULL;
2245   JavaThread* current_thread = JavaThread::current();
2246   // rm object is created to clean up the javaVFrame created in
2247   // doit_prologue(), but after doit() is finished with it.
2248   ResourceMark rm(current_thread);
2249   HandleMark hm(current_thread);
2250   oop thread_obj = JNIHandles::resolve_external_guard(thread);
2251 
2252   if (java_lang_VirtualThread::is_instance(thread_obj)) {
2253     // Support for virtual threads
2254     VM_VirtualThreadGetReceiver op(this, Handle(current_thread, thread_obj),
2255                                  current_thread, depth);
2256     VMThread::execute(&op);
2257     err = op.result();
2258     if (err == JVMTI_ERROR_NONE) {
2259       *value_ptr = op.value().l;
2260     }
2261   } else {
2262     // Support for ordinary threads
2263     ThreadsListHandle tlh(current_thread);
2264     err = get_JavaThread(tlh.list(), thread, &java_thread);
2265     if (err != JVMTI_ERROR_NONE) {
2266       return err;
2267     }
2268     VM_GetReceiver op(java_thread, current_thread, depth);
2269     VMThread::execute(&op);
2270     err = op.result();
2271     if (err == JVMTI_ERROR_NONE) {
2272       *value_ptr = op.value().l;
2273     }
2274   }
2275   return err;
2276 } /* end GetLocalInstance */
2277 
2278 
2279 // Threads_lock NOT held
2280 // depth - pre-checked as non-negative
2281 // value_ptr - pre-checked for NULL
2282 jvmtiError
2283 JvmtiEnv::GetLocalInt(jthread thread, jint depth, jint slot, jint* value_ptr) {
2284   jvmtiError err = JVMTI_ERROR_NONE;
2285   JavaThread* java_thread = NULL;
2286   JavaThread* current_thread = JavaThread::current();
2287   // rm object is created to clean up the javaVFrame created in
2288   // doit_prologue(), but after doit() is finished with it.
2289   ResourceMark rm(current_thread);
2290   HandleMark hm(current_thread);
2291   oop thread_obj = JNIHandles::resolve_external_guard(thread);
2292 
2293   if (java_lang_VirtualThread::is_instance(thread_obj)) {
2294     // Support for virtual threads
2295     VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2296                                    depth, slot, T_INT);
2297     VMThread::execute(&op);
2298     err = op.result();
2299     if (err == JVMTI_ERROR_NONE) {
2300       *value_ptr = op.value().i;
2301     }
2302   } else {
2303     // Support for ordinary threads
2304     ThreadsListHandle tlh(current_thread);
2305     err = get_JavaThread(tlh.list(), thread, &java_thread);
2306     if (err != JVMTI_ERROR_NONE) {
2307       return err;
2308     }
2309     VM_GetOrSetLocal op(java_thread, depth, slot, T_INT);
2310     VMThread::execute(&op);
2311     err = op.result();
2312     if (err == JVMTI_ERROR_NONE) {
2313       *value_ptr = op.value().i;
2314     }
2315   }
2316   return err;
2317 } /* end GetLocalInt */
2318 
2319 
2320 // Threads_lock NOT held
2321 // depth - pre-checked as non-negative
2322 // value_ptr - pre-checked for NULL
2323 jvmtiError
2324 JvmtiEnv::GetLocalLong(jthread thread, jint depth, jint slot, jlong* value_ptr) {
2325   jvmtiError err = JVMTI_ERROR_NONE;
2326   JavaThread* java_thread = NULL;
2327   JavaThread* current_thread = JavaThread::current();
2328   // rm object is created to clean up the javaVFrame created in
2329   // doit_prologue(), but after doit() is finished with it.
2330   ResourceMark rm(current_thread);
2331   HandleMark hm(current_thread);
2332   oop thread_obj = JNIHandles::resolve_external_guard(thread);
2333 
2334   if (java_lang_VirtualThread::is_instance(thread_obj)) {
2335     // Support for virtual threads
2336     VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2337                                    depth, slot, T_LONG);
2338     VMThread::execute(&op);
2339     err = op.result();
2340     if (err == JVMTI_ERROR_NONE) {
2341       *value_ptr = op.value().j;
2342     }
2343   } else {
2344     // Support for ordinary threads
2345     ThreadsListHandle tlh(current_thread);
2346     err = get_JavaThread(tlh.list(), thread, &java_thread);
2347     if (err != JVMTI_ERROR_NONE) {
2348       return err;
2349     }
2350     VM_GetOrSetLocal op(java_thread, depth, slot, T_LONG);
2351     VMThread::execute(&op);
2352     err = op.result();
2353     if (err == JVMTI_ERROR_NONE) {
2354       *value_ptr = op.value().j;
2355     }
2356   }
2357   return err;
2358 } /* end GetLocalLong */
2359 
2360 
2361 // Threads_lock NOT held
2362 // depth - pre-checked as non-negative
2363 // value_ptr - pre-checked for NULL
2364 jvmtiError
2365 JvmtiEnv::GetLocalFloat(jthread thread, jint depth, jint slot, jfloat* value_ptr) {
2366   jvmtiError err = JVMTI_ERROR_NONE;
2367   JavaThread* java_thread = NULL;
2368   JavaThread* current_thread = JavaThread::current();
2369   // rm object is created to clean up the javaVFrame created in
2370   // doit_prologue(), but after doit() is finished with it.
2371   ResourceMark rm(current_thread);
2372   HandleMark hm(current_thread);
2373   oop thread_obj = JNIHandles::resolve_external_guard(thread);
2374 
2375   if (java_lang_VirtualThread::is_instance(thread_obj)) {
2376     // Support for virtual threads
2377     VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2378                                    depth, slot, T_FLOAT);
2379     VMThread::execute(&op);
2380     err = op.result();
2381     if (err == JVMTI_ERROR_NONE) {
2382       *value_ptr = op.value().f;
2383     }
2384   } else {
2385     // Support for ordinary threads
2386     ThreadsListHandle tlh(current_thread);
2387     err = get_JavaThread(tlh.list(), thread, &java_thread);
2388     if (err != JVMTI_ERROR_NONE) {
2389       return err;
2390     }
2391     VM_GetOrSetLocal op(java_thread, depth, slot, T_FLOAT);
2392     VMThread::execute(&op);
2393     err = op.result();
2394     if (err == JVMTI_ERROR_NONE) {
2395       *value_ptr = op.value().f;
2396     }
2397   }
2398   return err;
2399 } /* end GetLocalFloat */
2400 
2401 
2402 // Threads_lock NOT held
2403 // depth - pre-checked as non-negative
2404 // value_ptr - pre-checked for NULL
2405 jvmtiError
2406 JvmtiEnv::GetLocalDouble(jthread thread, jint depth, jint slot, jdouble* value_ptr) {
2407   jvmtiError err = JVMTI_ERROR_NONE;
2408   JavaThread* java_thread = NULL;
2409   JavaThread* current_thread = JavaThread::current();
2410   // rm object is created to clean up the javaVFrame created in
2411   // doit_prologue(), but after doit() is finished with it.
2412   ResourceMark rm(current_thread);
2413   HandleMark hm(current_thread);
2414   oop thread_obj = JNIHandles::resolve_external_guard(thread);
2415 
2416   if (java_lang_VirtualThread::is_instance(thread_obj)) {
2417     // Support for virtual threads
2418     VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2419                                    depth, slot, T_DOUBLE);
2420     VMThread::execute(&op);
2421     err = op.result();
2422     if (err == JVMTI_ERROR_NONE) {
2423       *value_ptr = op.value().d;
2424     }
2425   } else {
2426     // Support for ordinary threads
2427     ThreadsListHandle tlh(current_thread);
2428     err = get_JavaThread(tlh.list(), thread, &java_thread);
2429     if (err != JVMTI_ERROR_NONE) {
2430       return err;
2431     }
2432     VM_GetOrSetLocal op(java_thread, depth, slot, T_DOUBLE);
2433     VMThread::execute(&op);
2434     err = op.result();
2435     if (err == JVMTI_ERROR_NONE) {
2436       *value_ptr = op.value().d;
2437     }
2438   }
2439   return err;
2440 } /* end GetLocalDouble */
2441 
2442 
2443 // java_thread - protected by ThreadsListHandle and pre-checked
2444 // depth - pre-checked as non-negative
2445 jvmtiError
2446 JvmtiEnv::SetLocalObject(JavaThread* java_thread, jint depth, jint slot, jobject value) {
2447   // rm object is created to clean up the javaVFrame created in
2448   // doit_prologue(), but after doit() is finished with it.
2449   ResourceMark rm;
2450   jvalue val;
2451   val.l = value;
2452   VM_GetOrSetLocal op(java_thread, depth, slot, T_OBJECT, val);
2453   VMThread::execute(&op);
2454   return op.result();
2455 } /* end SetLocalObject */
2456 
2457 
2458 // java_thread - protected by ThreadsListHandle and pre-checked
2459 // depth - pre-checked as non-negative
2460 jvmtiError
2461 JvmtiEnv::SetLocalInt(JavaThread* java_thread, jint depth, jint slot, jint value) {
2462   // rm object is created to clean up the javaVFrame created in
2463   // doit_prologue(), but after doit() is finished with it.
2464   ResourceMark rm;
2465   jvalue val;
2466   val.i = value;
2467   VM_GetOrSetLocal op(java_thread, depth, slot, T_INT, val);
2468   VMThread::execute(&op);
2469   return op.result();
2470 } /* end SetLocalInt */
2471 
2472 
2473 // java_thread - protected by ThreadsListHandle and pre-checked
2474 // depth - pre-checked as non-negative
2475 jvmtiError
2476 JvmtiEnv::SetLocalLong(JavaThread* java_thread, jint depth, jint slot, jlong value) {
2477   // rm object is created to clean up the javaVFrame created in
2478   // doit_prologue(), but after doit() is finished with it.
2479   ResourceMark rm;
2480   jvalue val;
2481   val.j = value;
2482   VM_GetOrSetLocal op(java_thread, depth, slot, T_LONG, val);
2483   VMThread::execute(&op);
2484   return op.result();
2485 } /* end SetLocalLong */
2486 
2487 
2488 // java_thread - protected by ThreadsListHandle and pre-checked
2489 // depth - pre-checked as non-negative
2490 jvmtiError
2491 JvmtiEnv::SetLocalFloat(JavaThread* java_thread, jint depth, jint slot, jfloat value) {
2492   // rm object is created to clean up the javaVFrame created in
2493   // doit_prologue(), but after doit() is finished with it.
2494   ResourceMark rm;
2495   jvalue val;
2496   val.f = value;
2497   VM_GetOrSetLocal op(java_thread, depth, slot, T_FLOAT, val);
2498   VMThread::execute(&op);
2499   return op.result();
2500 } /* end SetLocalFloat */
2501 
2502 
2503 // java_thread - protected by ThreadsListHandle and pre-checked
2504 // depth - pre-checked as non-negative
2505 jvmtiError
2506 JvmtiEnv::SetLocalDouble(JavaThread* java_thread, jint depth, jint slot, jdouble value) {
2507   // rm object is created to clean up the javaVFrame created in
2508   // doit_prologue(), but after doit() is finished with it.
2509   ResourceMark rm;
2510   jvalue val;
2511   val.d = value;
2512   VM_GetOrSetLocal op(java_thread, depth, slot, T_DOUBLE, val);
2513   VMThread::execute(&op);
2514   return op.result();
2515 } /* end SetLocalDouble */
2516 
2517 
2518   //
2519   // Breakpoint functions
2520   //
2521 
2522 // method - pre-checked for validity, but may be NULL meaning obsolete method
2523 jvmtiError
2524 JvmtiEnv::SetBreakpoint(Method* method, jlocation location) {
2525   NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
2526   if (location < 0) {   // simple invalid location check first
2527     return JVMTI_ERROR_INVALID_LOCATION;
2528   }
2529   // verify that the breakpoint is not past the end of the method
2530   if (location >= (jlocation) method->code_size()) {
2531     return JVMTI_ERROR_INVALID_LOCATION;
2532   }
2533 
2534   ResourceMark rm;
2535   JvmtiBreakpoint bp(method, location);
2536   JvmtiBreakpoints& jvmti_breakpoints = JvmtiCurrentBreakpoints::get_jvmti_breakpoints();
2537   if (jvmti_breakpoints.set(bp) == JVMTI_ERROR_DUPLICATE)
2538     return JVMTI_ERROR_DUPLICATE;
2539 
2540   if (TraceJVMTICalls) {
2541     jvmti_breakpoints.print();
2542   }
2543 
2544   return JVMTI_ERROR_NONE;
2545 } /* end SetBreakpoint */
2546 
2547 
2548 // method - pre-checked for validity, but may be NULL meaning obsolete method
2549 jvmtiError
2550 JvmtiEnv::ClearBreakpoint(Method* method, jlocation location) {
2551   NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
2552 
2553   if (location < 0) {   // simple invalid location check first
2554     return JVMTI_ERROR_INVALID_LOCATION;
2555   }
2556 
2557   // verify that the breakpoint is not past the end of the method
2558   if (location >= (jlocation) method->code_size()) {
2559     return JVMTI_ERROR_INVALID_LOCATION;
2560   }
2561 
2562   JvmtiBreakpoint bp(method, location);
2563 
2564   JvmtiBreakpoints& jvmti_breakpoints = JvmtiCurrentBreakpoints::get_jvmti_breakpoints();
2565   if (jvmti_breakpoints.clear(bp) == JVMTI_ERROR_NOT_FOUND)
2566     return JVMTI_ERROR_NOT_FOUND;
2567 
2568   if (TraceJVMTICalls) {
2569     jvmti_breakpoints.print();
2570   }
2571 
2572   return JVMTI_ERROR_NONE;
2573 } /* end ClearBreakpoint */
2574 
2575 
2576   //
2577   // Watched Field functions
2578   //
2579 
2580 jvmtiError
2581 JvmtiEnv::SetFieldAccessWatch(fieldDescriptor* fdesc_ptr) {
2582   // make sure we haven't set this watch before
2583   if (fdesc_ptr->is_field_access_watched()) return JVMTI_ERROR_DUPLICATE;
2584   fdesc_ptr->set_is_field_access_watched(true);
2585 
2586   JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_ACCESS, true);
2587 
2588   return JVMTI_ERROR_NONE;
2589 } /* end SetFieldAccessWatch */
2590 
2591 
2592 jvmtiError
2593 JvmtiEnv::ClearFieldAccessWatch(fieldDescriptor* fdesc_ptr) {
2594   // make sure we have a watch to clear
2595   if (!fdesc_ptr->is_field_access_watched()) return JVMTI_ERROR_NOT_FOUND;
2596   fdesc_ptr->set_is_field_access_watched(false);
2597 
2598   JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_ACCESS, false);
2599 
2600   return JVMTI_ERROR_NONE;
2601 } /* end ClearFieldAccessWatch */
2602 
2603 
2604 jvmtiError
2605 JvmtiEnv::SetFieldModificationWatch(fieldDescriptor* fdesc_ptr) {
2606   // make sure we haven't set this watch before
2607   if (fdesc_ptr->is_field_modification_watched()) return JVMTI_ERROR_DUPLICATE;
2608   fdesc_ptr->set_is_field_modification_watched(true);
2609 
2610   JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_MODIFICATION, true);
2611 
2612   return JVMTI_ERROR_NONE;
2613 } /* end SetFieldModificationWatch */
2614 
2615 
2616 jvmtiError
2617 JvmtiEnv::ClearFieldModificationWatch(fieldDescriptor* fdesc_ptr) {
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 = NULL;
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 != NULL) {
2645     char* result = NULL;
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 != NULL) {
2659     *generic_ptr = NULL;
2660     if (!isPrimitive && k->is_instance_klass()) {
2661       Symbol* soo = InstanceKlass::cast(k)->generic_signature();
2662       if (soo != NULL) {
2663         const char *gen_sig = soo->as_C_string();
2664         if (gen_sig != NULL) {
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   JavaThread* current_thread  = JavaThread::current();
2732   jint result = 0;
2733   if (!java_lang_Class::is_primitive(k_mirror)) {
2734     Klass* k = java_lang_Class::as_Klass(k_mirror);
2735     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2736     result = k->compute_modifier_flags();
2737 
2738     // Reset the deleted  ACC_SUPER bit (deleted in compute_modifier_flags()).
2739     if (k->is_super()) {
2740       result |= JVM_ACC_SUPER;
2741     }
2742   } else {
2743     result = (JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC);
2744   }
2745   *modifiers_ptr = result;
2746 
2747   return JVMTI_ERROR_NONE;
2748 } /* end GetClassModifiers */
2749 
2750 
2751 // k_mirror - may be primitive, this must be checked
2752 // method_count_ptr - pre-checked for NULL
2753 // methods_ptr - pre-checked for NULL
2754 jvmtiError
2755 JvmtiEnv::GetClassMethods(oop k_mirror, jint* method_count_ptr, jmethodID** methods_ptr) {
2756   JavaThread* current_thread  = JavaThread::current();
2757   HandleMark hm(current_thread);
2758 
2759   if (java_lang_Class::is_primitive(k_mirror)) {
2760     *method_count_ptr = 0;
2761     *methods_ptr = (jmethodID*) jvmtiMalloc(0 * sizeof(jmethodID));
2762     return JVMTI_ERROR_NONE;
2763   }
2764   Klass* k = java_lang_Class::as_Klass(k_mirror);
2765   NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2766 
2767   // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2768   if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) {
2769     return JVMTI_ERROR_CLASS_NOT_PREPARED;
2770   }
2771 
2772   if (!k->is_instance_klass()) {
2773     *method_count_ptr = 0;
2774     *methods_ptr = (jmethodID*) jvmtiMalloc(0 * sizeof(jmethodID));
2775     return JVMTI_ERROR_NONE;
2776   }
2777   InstanceKlass* ik = InstanceKlass::cast(k);
2778   // Allocate the result and fill it in
2779   int result_length = ik->methods()->length();
2780   jmethodID* result_list = (jmethodID*)jvmtiMalloc(result_length * sizeof(jmethodID));
2781   int index;
2782   bool jmethodids_found = true;
2783   int skipped = 0;  // skip overpass methods
2784 
2785   for (index = 0; index < result_length; index++) {
2786     Method* m = ik->methods()->at(index);
2787     // Depending on can_maintain_original_method_order capability use the original
2788     // method ordering indices stored in the class, so we can emit jmethodIDs in
2789     // the order they appeared in the class file or just copy in current order.
2790     int result_index = JvmtiExport::can_maintain_original_method_order() ? ik->method_ordering()->at(index) : index;
2791     assert(result_index >= 0 && result_index < result_length, "invalid original method index");
2792     if (m->is_overpass()) {
2793       result_list[result_index] = NULL;
2794       skipped++;
2795       continue;
2796     }
2797     jmethodID id;
2798     if (jmethodids_found) {
2799       id = m->find_jmethod_id_or_null();
2800       if (id == NULL) {
2801         // If we find an uninitialized value, make sure there is
2802         // enough space for all the uninitialized values we might
2803         // find.
2804         ik->ensure_space_for_methodids(index);
2805         jmethodids_found = false;
2806         id = m->jmethod_id();
2807       }
2808     } else {
2809       id = m->jmethod_id();
2810     }
2811     result_list[result_index] = id;
2812   }
2813 
2814   // Fill in return value.
2815   if (skipped > 0) {
2816     // copy results skipping NULL methodIDs
2817     *methods_ptr = (jmethodID*)jvmtiMalloc((result_length - skipped) * sizeof(jmethodID));
2818     *method_count_ptr = result_length - skipped;
2819     for (index = 0, skipped = 0; index < result_length; index++) {
2820       if (result_list[index] == NULL) {
2821         skipped++;
2822       } else {
2823         (*methods_ptr)[index - skipped] = result_list[index];
2824       }
2825     }
2826     deallocate((unsigned char *)result_list);
2827   } else {
2828     *method_count_ptr = result_length;
2829     *methods_ptr = result_list;
2830   }
2831 
2832   return JVMTI_ERROR_NONE;
2833 } /* end GetClassMethods */
2834 
2835 
2836 // k_mirror - may be primitive, this must be checked
2837 // field_count_ptr - pre-checked for NULL
2838 // fields_ptr - pre-checked for NULL
2839 jvmtiError
2840 JvmtiEnv::GetClassFields(oop k_mirror, jint* field_count_ptr, jfieldID** fields_ptr) {
2841   if (java_lang_Class::is_primitive(k_mirror)) {
2842     *field_count_ptr = 0;
2843     *fields_ptr = (jfieldID*) jvmtiMalloc(0 * sizeof(jfieldID));
2844     return JVMTI_ERROR_NONE;
2845   }
2846   JavaThread* current_thread = JavaThread::current();
2847   HandleMark hm(current_thread);
2848   Klass* k = java_lang_Class::as_Klass(k_mirror);
2849   NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2850 
2851   // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2852   if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) {
2853     return JVMTI_ERROR_CLASS_NOT_PREPARED;
2854   }
2855 
2856   if (!k->is_instance_klass()) {
2857     *field_count_ptr = 0;
2858     *fields_ptr = (jfieldID*) jvmtiMalloc(0 * sizeof(jfieldID));
2859     return JVMTI_ERROR_NONE;
2860   }
2861 
2862 
2863   InstanceKlass* ik = InstanceKlass::cast(k);
2864 
2865   int result_count = 0;
2866   // First, count the fields.
2867   FilteredFieldStream flds(ik, true, true);
2868   result_count = flds.field_count();
2869 
2870   // Allocate the result and fill it in
2871   jfieldID* result_list = (jfieldID*) jvmtiMalloc(result_count * sizeof(jfieldID));
2872   // The JVMTI spec requires fields in the order they occur in the class file,
2873   // this is the reverse order of what FieldStream hands out.
2874   int id_index = (result_count - 1);
2875 
2876   for (FilteredFieldStream src_st(ik, true, true); !src_st.eos(); src_st.next()) {
2877     result_list[id_index--] = jfieldIDWorkaround::to_jfieldID(
2878                                             ik, src_st.offset(),
2879                                             src_st.access_flags().is_static());
2880   }
2881   assert(id_index == -1, "just checking");
2882   // Fill in the results
2883   *field_count_ptr = result_count;
2884   *fields_ptr = result_list;
2885 
2886   return JVMTI_ERROR_NONE;
2887 } /* end GetClassFields */
2888 
2889 
2890 // k_mirror - may be primitive, this must be checked
2891 // interface_count_ptr - pre-checked for NULL
2892 // interfaces_ptr - pre-checked for NULL
2893 jvmtiError
2894 JvmtiEnv::GetImplementedInterfaces(oop k_mirror, jint* interface_count_ptr, jclass** interfaces_ptr) {
2895   {
2896     if (java_lang_Class::is_primitive(k_mirror)) {
2897       *interface_count_ptr = 0;
2898       *interfaces_ptr = (jclass*) jvmtiMalloc(0 * sizeof(jclass));
2899       return JVMTI_ERROR_NONE;
2900     }
2901     JavaThread* current_thread = JavaThread::current();
2902     HandleMark hm(current_thread);
2903     Klass* k = java_lang_Class::as_Klass(k_mirror);
2904     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2905 
2906     // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2907     if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) ))
2908       return JVMTI_ERROR_CLASS_NOT_PREPARED;
2909 
2910     if (!k->is_instance_klass()) {
2911       *interface_count_ptr = 0;
2912       *interfaces_ptr = (jclass*) jvmtiMalloc(0 * sizeof(jclass));
2913       return JVMTI_ERROR_NONE;
2914     }
2915 
2916     Array<InstanceKlass*>* interface_list = InstanceKlass::cast(k)->local_interfaces();
2917     const int result_length = (interface_list == NULL ? 0 : interface_list->length());
2918     jclass* result_list = (jclass*) jvmtiMalloc(result_length * sizeof(jclass));
2919     for (int i_index = 0; i_index < result_length; i_index += 1) {
2920       InstanceKlass* klass_at = interface_list->at(i_index);
2921       assert(klass_at->is_klass(), "interfaces must be Klass*s");
2922       assert(klass_at->is_interface(), "interfaces must be interfaces");
2923       oop mirror_at = klass_at->java_mirror();
2924       Handle handle_at = Handle(current_thread, mirror_at);
2925       result_list[i_index] = (jclass) jni_reference(handle_at);
2926     }
2927     *interface_count_ptr = result_length;
2928     *interfaces_ptr = result_list;
2929   }
2930 
2931   return JVMTI_ERROR_NONE;
2932 } /* end GetImplementedInterfaces */
2933 
2934 
2935 // k_mirror - may be primitive, this must be checked
2936 // minor_version_ptr - pre-checked for NULL
2937 // major_version_ptr - pre-checked for NULL
2938 jvmtiError
2939 JvmtiEnv::GetClassVersionNumbers(oop k_mirror, jint* minor_version_ptr, jint* major_version_ptr) {
2940   if (java_lang_Class::is_primitive(k_mirror)) {
2941     return JVMTI_ERROR_ABSENT_INFORMATION;
2942   }
2943   Klass* klass = java_lang_Class::as_Klass(k_mirror);
2944 
2945   jint status = klass->jvmti_class_status();
2946   if (status & (JVMTI_CLASS_STATUS_ERROR)) {
2947     return JVMTI_ERROR_INVALID_CLASS;
2948   }
2949   if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
2950     return JVMTI_ERROR_ABSENT_INFORMATION;
2951   }
2952 
2953   InstanceKlass* ik = InstanceKlass::cast(klass);
2954   *minor_version_ptr = ik->minor_version();
2955   *major_version_ptr = ik->major_version();
2956 
2957   return JVMTI_ERROR_NONE;
2958 } /* end GetClassVersionNumbers */
2959 
2960 
2961 // k_mirror - may be primitive, this must be checked
2962 // constant_pool_count_ptr - pre-checked for NULL
2963 // constant_pool_byte_count_ptr - pre-checked for NULL
2964 // constant_pool_bytes_ptr - pre-checked for NULL
2965 jvmtiError
2966 JvmtiEnv::GetConstantPool(oop k_mirror, jint* constant_pool_count_ptr, jint* constant_pool_byte_count_ptr, unsigned char** constant_pool_bytes_ptr) {
2967   if (java_lang_Class::is_primitive(k_mirror)) {
2968     return JVMTI_ERROR_ABSENT_INFORMATION;
2969   }
2970 
2971   Klass* klass = java_lang_Class::as_Klass(k_mirror);
2972   Thread *thread = Thread::current();
2973   ResourceMark rm(thread);
2974 
2975   jint status = klass->jvmti_class_status();
2976   if (status & (JVMTI_CLASS_STATUS_ERROR)) {
2977     return JVMTI_ERROR_INVALID_CLASS;
2978   }
2979   if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
2980     return JVMTI_ERROR_ABSENT_INFORMATION;
2981   }
2982 
2983   InstanceKlass* ik = InstanceKlass::cast(klass);
2984   JvmtiConstantPoolReconstituter reconstituter(ik);
2985   if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2986     return reconstituter.get_error();
2987   }
2988 
2989   unsigned char *cpool_bytes;
2990   int cpool_size = reconstituter.cpool_size();
2991   if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2992     return reconstituter.get_error();
2993   }
2994   jvmtiError res = allocate(cpool_size, &cpool_bytes);
2995   if (res != JVMTI_ERROR_NONE) {
2996     return res;
2997   }
2998   reconstituter.copy_cpool_bytes(cpool_bytes);
2999   if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
3000     return reconstituter.get_error();
3001   }
3002 
3003   constantPoolHandle  constants(thread, ik->constants());
3004   *constant_pool_count_ptr      = constants->length();
3005   *constant_pool_byte_count_ptr = cpool_size;
3006   *constant_pool_bytes_ptr      = cpool_bytes;
3007 
3008   return JVMTI_ERROR_NONE;
3009 } /* end GetConstantPool */
3010 
3011 
3012 // k_mirror - may be primitive, this must be checked
3013 // is_interface_ptr - pre-checked for NULL
3014 jvmtiError
3015 JvmtiEnv::IsInterface(oop k_mirror, jboolean* is_interface_ptr) {
3016   {
3017     bool result = false;
3018     if (!java_lang_Class::is_primitive(k_mirror)) {
3019       Klass* k = java_lang_Class::as_Klass(k_mirror);
3020       if (k != NULL && k->is_interface()) {
3021         result = true;
3022       }
3023     }
3024     *is_interface_ptr = result;
3025   }
3026 
3027   return JVMTI_ERROR_NONE;
3028 } /* end IsInterface */
3029 
3030 
3031 // k_mirror - may be primitive, this must be checked
3032 // is_array_class_ptr - pre-checked for NULL
3033 jvmtiError
3034 JvmtiEnv::IsArrayClass(oop k_mirror, jboolean* is_array_class_ptr) {
3035   {
3036     bool result = false;
3037     if (!java_lang_Class::is_primitive(k_mirror)) {
3038       Klass* k = java_lang_Class::as_Klass(k_mirror);
3039       if (k != NULL && k->is_array_klass()) {
3040         result = true;
3041       }
3042     }
3043     *is_array_class_ptr = result;
3044   }
3045 
3046   return JVMTI_ERROR_NONE;
3047 } /* end IsArrayClass */
3048 
3049 
3050 // k_mirror - may be primitive, this must be checked
3051 // classloader_ptr - pre-checked for NULL
3052 jvmtiError
3053 JvmtiEnv::GetClassLoader(oop k_mirror, jobject* classloader_ptr) {
3054   {
3055     if (java_lang_Class::is_primitive(k_mirror)) {
3056       *classloader_ptr = (jclass) jni_reference(Handle());
3057       return JVMTI_ERROR_NONE;
3058     }
3059     JavaThread* current_thread = JavaThread::current();
3060     HandleMark hm(current_thread);
3061     Klass* k = java_lang_Class::as_Klass(k_mirror);
3062     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
3063 
3064     oop result_oop = k->class_loader();
3065     if (result_oop == NULL) {
3066       *classloader_ptr = (jclass) jni_reference(Handle());
3067       return JVMTI_ERROR_NONE;
3068     }
3069     Handle result_handle = Handle(current_thread, result_oop);
3070     jclass result_jnihandle = (jclass) jni_reference(result_handle);
3071     *classloader_ptr = result_jnihandle;
3072   }
3073   return JVMTI_ERROR_NONE;
3074 } /* end GetClassLoader */
3075 
3076 
3077 // k_mirror - may be primitive, this must be checked
3078 // source_debug_extension_ptr - pre-checked for NULL
3079 jvmtiError
3080 JvmtiEnv::GetSourceDebugExtension(oop k_mirror, char** source_debug_extension_ptr) {
3081   {
3082     if (java_lang_Class::is_primitive(k_mirror)) {
3083       return JVMTI_ERROR_ABSENT_INFORMATION;
3084     }
3085     Klass* k = java_lang_Class::as_Klass(k_mirror);
3086     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
3087     if (!k->is_instance_klass()) {
3088       return JVMTI_ERROR_ABSENT_INFORMATION;
3089     }
3090     const char* sde = InstanceKlass::cast(k)->source_debug_extension();
3091     NULL_CHECK(sde, JVMTI_ERROR_ABSENT_INFORMATION);
3092 
3093     {
3094       *source_debug_extension_ptr = (char *) jvmtiMalloc(strlen(sde)+1);
3095       strcpy(*source_debug_extension_ptr, sde);
3096     }
3097   }
3098 
3099   return JVMTI_ERROR_NONE;
3100 } /* end GetSourceDebugExtension */
3101 
3102   //
3103   // Object functions
3104   //
3105 
3106 // hash_code_ptr - pre-checked for NULL
3107 jvmtiError
3108 JvmtiEnv::GetObjectHashCode(jobject object, jint* hash_code_ptr) {
3109   oop mirror = JNIHandles::resolve_external_guard(object);
3110   NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
3111   NULL_CHECK(hash_code_ptr, JVMTI_ERROR_NULL_POINTER);
3112 
3113   {
3114     jint result = (jint) mirror->identity_hash();
3115     *hash_code_ptr = result;
3116   }
3117   return JVMTI_ERROR_NONE;
3118 } /* end GetObjectHashCode */
3119 
3120 
3121 // info_ptr - pre-checked for NULL
3122 jvmtiError
3123 JvmtiEnv::GetObjectMonitorUsage(jobject object, jvmtiMonitorUsage* info_ptr) {
3124   // This needs to be performed at a safepoint to gather stable data
3125   // because monitor owner / waiters might not be suspended.
3126   VM_GetObjectMonitorUsage op(this, JavaThread::current(), object, info_ptr);
3127   VMThread::execute(&op);
3128   return op.result();
3129 } /* end GetObjectMonitorUsage */
3130 
3131 
3132   //
3133   // Field functions
3134   //
3135 
3136 // name_ptr - NULL is a valid value, must be checked
3137 // signature_ptr - NULL is a valid value, must be checked
3138 // generic_ptr - NULL is a valid value, must be checked
3139 jvmtiError
3140 JvmtiEnv::GetFieldName(fieldDescriptor* fdesc_ptr, char** name_ptr, char** signature_ptr, char** generic_ptr) {
3141   JavaThread* current_thread  = JavaThread::current();
3142   ResourceMark rm(current_thread);
3143   if (name_ptr == NULL) {
3144     // just don't return the name
3145   } else {
3146     const char* fieldName = fdesc_ptr->name()->as_C_string();
3147     *name_ptr =  (char*) jvmtiMalloc(strlen(fieldName) + 1);
3148     if (*name_ptr == NULL)
3149       return JVMTI_ERROR_OUT_OF_MEMORY;
3150     strcpy(*name_ptr, fieldName);
3151   }
3152   if (signature_ptr== NULL) {
3153     // just don't return the signature
3154   } else {
3155     const char* fieldSignature = fdesc_ptr->signature()->as_C_string();
3156     *signature_ptr = (char*) jvmtiMalloc(strlen(fieldSignature) + 1);
3157     if (*signature_ptr == NULL)
3158       return JVMTI_ERROR_OUT_OF_MEMORY;
3159     strcpy(*signature_ptr, fieldSignature);
3160   }
3161   if (generic_ptr != NULL) {
3162     *generic_ptr = NULL;
3163     Symbol* soop = fdesc_ptr->generic_signature();
3164     if (soop != NULL) {
3165       const char* gen_sig = soop->as_C_string();
3166       if (gen_sig != NULL) {
3167         jvmtiError err = allocate(strlen(gen_sig) + 1, (unsigned char **)generic_ptr);
3168         if (err != JVMTI_ERROR_NONE) {
3169           return err;
3170         }
3171         strcpy(*generic_ptr, gen_sig);
3172       }
3173     }
3174   }
3175   return JVMTI_ERROR_NONE;
3176 } /* end GetFieldName */
3177 
3178 
3179 // declaring_class_ptr - pre-checked for NULL
3180 jvmtiError
3181 JvmtiEnv::GetFieldDeclaringClass(fieldDescriptor* fdesc_ptr, jclass* declaring_class_ptr) {
3182 
3183   *declaring_class_ptr = get_jni_class_non_null(fdesc_ptr->field_holder());
3184   return JVMTI_ERROR_NONE;
3185 } /* end GetFieldDeclaringClass */
3186 
3187 
3188 // modifiers_ptr - pre-checked for NULL
3189 jvmtiError
3190 JvmtiEnv::GetFieldModifiers(fieldDescriptor* fdesc_ptr, jint* modifiers_ptr) {
3191 
3192   AccessFlags resultFlags = fdesc_ptr->access_flags();
3193   jint result = resultFlags.as_int();
3194   *modifiers_ptr = result;
3195 
3196   return JVMTI_ERROR_NONE;
3197 } /* end GetFieldModifiers */
3198 
3199 
3200 // is_synthetic_ptr - pre-checked for NULL
3201 jvmtiError
3202 JvmtiEnv::IsFieldSynthetic(fieldDescriptor* fdesc_ptr, jboolean* is_synthetic_ptr) {
3203   *is_synthetic_ptr = fdesc_ptr->is_synthetic();
3204   return JVMTI_ERROR_NONE;
3205 } /* end IsFieldSynthetic */
3206 
3207 
3208   //
3209   // Method functions
3210   //
3211 
3212 // method - pre-checked for validity, but may be NULL meaning obsolete method
3213 // name_ptr - NULL is a valid value, must be checked
3214 // signature_ptr - NULL is a valid value, must be checked
3215 // generic_ptr - NULL is a valid value, must be checked
3216 jvmtiError
3217 JvmtiEnv::GetMethodName(Method* method, char** name_ptr, char** signature_ptr, char** generic_ptr) {
3218   NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3219   JavaThread* current_thread  = JavaThread::current();
3220 
3221   ResourceMark rm(current_thread); // get the utf8 name and signature
3222   if (name_ptr == NULL) {
3223     // just don't return the name
3224   } else {
3225     const char* utf8_name = (const char *) method->name()->as_utf8();
3226     *name_ptr = (char *) jvmtiMalloc(strlen(utf8_name)+1);
3227     strcpy(*name_ptr, utf8_name);
3228   }
3229   if (signature_ptr == NULL) {
3230     // just don't return the signature
3231   } else {
3232     const char* utf8_signature = (const char *) method->signature()->as_utf8();
3233     *signature_ptr = (char *) jvmtiMalloc(strlen(utf8_signature) + 1);
3234     strcpy(*signature_ptr, utf8_signature);
3235   }
3236 
3237   if (generic_ptr != NULL) {
3238     *generic_ptr = NULL;
3239     Symbol* soop = method->generic_signature();
3240     if (soop != NULL) {
3241       const char* gen_sig = soop->as_C_string();
3242       if (gen_sig != NULL) {
3243         jvmtiError err = allocate(strlen(gen_sig) + 1, (unsigned char **)generic_ptr);
3244         if (err != JVMTI_ERROR_NONE) {
3245           return err;
3246         }
3247         strcpy(*generic_ptr, gen_sig);
3248       }
3249     }
3250   }
3251   return JVMTI_ERROR_NONE;
3252 } /* end GetMethodName */
3253 
3254 
3255 // method - pre-checked for validity, but may be NULL meaning obsolete method
3256 // declaring_class_ptr - pre-checked for NULL
3257 jvmtiError
3258 JvmtiEnv::GetMethodDeclaringClass(Method* method, jclass* declaring_class_ptr) {
3259   NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3260   (*declaring_class_ptr) = get_jni_class_non_null(method->method_holder());
3261   return JVMTI_ERROR_NONE;
3262 } /* end GetMethodDeclaringClass */
3263 
3264 
3265 // method - pre-checked for validity, but may be NULL meaning obsolete method
3266 // modifiers_ptr - pre-checked for NULL
3267 jvmtiError
3268 JvmtiEnv::GetMethodModifiers(Method* method, jint* modifiers_ptr) {
3269   NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3270   (*modifiers_ptr) = method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
3271   return JVMTI_ERROR_NONE;
3272 } /* end GetMethodModifiers */
3273 
3274 
3275 // method - pre-checked for validity, but may be NULL meaning obsolete method
3276 // max_ptr - pre-checked for NULL
3277 jvmtiError
3278 JvmtiEnv::GetMaxLocals(Method* method, jint* max_ptr) {
3279   NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3280   // get max stack
3281   (*max_ptr) = method->max_locals();
3282   return JVMTI_ERROR_NONE;
3283 } /* end GetMaxLocals */
3284 
3285 
3286 // method - pre-checked for validity, but may be NULL meaning obsolete method
3287 // size_ptr - pre-checked for NULL
3288 jvmtiError
3289 JvmtiEnv::GetArgumentsSize(Method* method, jint* size_ptr) {
3290   NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3291   // get size of arguments
3292 
3293   (*size_ptr) = method->size_of_parameters();
3294   return JVMTI_ERROR_NONE;
3295 } /* end GetArgumentsSize */
3296 
3297 
3298 // method - pre-checked for validity, but may be NULL meaning obsolete method
3299 // entry_count_ptr - pre-checked for NULL
3300 // table_ptr - pre-checked for NULL
3301 jvmtiError
3302 JvmtiEnv::GetLineNumberTable(Method* method, jint* entry_count_ptr, jvmtiLineNumberEntry** table_ptr) {
3303   NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3304   if (!method->has_linenumber_table()) {
3305     return (JVMTI_ERROR_ABSENT_INFORMATION);
3306   }
3307 
3308   // The line number table is compressed so we don't know how big it is until decompressed.
3309   // Decompression is really fast so we just do it twice.
3310 
3311   // Compute size of table
3312   jint num_entries = 0;
3313   CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
3314   while (stream.read_pair()) {
3315     num_entries++;
3316   }
3317   jvmtiLineNumberEntry *jvmti_table =
3318             (jvmtiLineNumberEntry *)jvmtiMalloc(num_entries * (sizeof(jvmtiLineNumberEntry)));
3319 
3320   // Fill jvmti table
3321   if (num_entries > 0) {
3322     int index = 0;
3323     CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
3324     while (stream.read_pair()) {
3325       jvmti_table[index].start_location = (jlocation) stream.bci();
3326       jvmti_table[index].line_number = (jint) stream.line();
3327       index++;
3328     }
3329     assert(index == num_entries, "sanity check");
3330   }
3331 
3332   // Set up results
3333   (*entry_count_ptr) = num_entries;
3334   (*table_ptr) = jvmti_table;
3335 
3336   return JVMTI_ERROR_NONE;
3337 } /* end GetLineNumberTable */
3338 
3339 
3340 // method - pre-checked for validity, but may be NULL meaning obsolete method
3341 // start_location_ptr - pre-checked for NULL
3342 // end_location_ptr - pre-checked for NULL
3343 jvmtiError
3344 JvmtiEnv::GetMethodLocation(Method* method, jlocation* start_location_ptr, jlocation* end_location_ptr) {
3345 
3346   NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3347   // get start and end location
3348   (*end_location_ptr) = (jlocation) (method->code_size() - 1);
3349   if (method->code_size() == 0) {
3350     // there is no code so there is no start location
3351     (*start_location_ptr) = (jlocation)(-1);
3352   } else {
3353     (*start_location_ptr) = (jlocation)(0);
3354   }
3355 
3356   return JVMTI_ERROR_NONE;
3357 } /* end GetMethodLocation */
3358 
3359 
3360 // method - pre-checked for validity, but may be NULL meaning obsolete method
3361 // entry_count_ptr - pre-checked for NULL
3362 // table_ptr - pre-checked for NULL
3363 jvmtiError
3364 JvmtiEnv::GetLocalVariableTable(Method* method, jint* entry_count_ptr, jvmtiLocalVariableEntry** table_ptr) {
3365 
3366   NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3367   JavaThread* current_thread  = JavaThread::current();
3368 
3369   // does the klass have any local variable information?
3370   InstanceKlass* ik = method->method_holder();
3371   if (!ik->access_flags().has_localvariable_table()) {
3372     return (JVMTI_ERROR_ABSENT_INFORMATION);
3373   }
3374 
3375   ConstantPool* constants = method->constants();
3376   NULL_CHECK(constants, JVMTI_ERROR_ABSENT_INFORMATION);
3377 
3378   // in the vm localvariable table representation, 6 consecutive elements in the table
3379   // represent a 6-tuple of shorts
3380   // [start_pc, length, name_index, descriptor_index, signature_index, index]
3381   jint num_entries = method->localvariable_table_length();
3382   jvmtiLocalVariableEntry *jvmti_table = (jvmtiLocalVariableEntry *)
3383                 jvmtiMalloc(num_entries * (sizeof(jvmtiLocalVariableEntry)));
3384 
3385   if (num_entries > 0) {
3386     LocalVariableTableElement* table = method->localvariable_table_start();
3387     for (int i = 0; i < num_entries; i++) {
3388       // get the 5 tuple information from the vm table
3389       jlocation start_location = (jlocation) table[i].start_bci;
3390       jint length = (jint) table[i].length;
3391       int name_index = (int) table[i].name_cp_index;
3392       int signature_index = (int) table[i].descriptor_cp_index;
3393       int generic_signature_index = (int) table[i].signature_cp_index;
3394       jint slot = (jint) table[i].slot;
3395 
3396       // get utf8 name and signature
3397       char *name_buf = NULL;
3398       char *sig_buf = NULL;
3399       char *gen_sig_buf = NULL;
3400       {
3401         ResourceMark rm(current_thread);
3402 
3403         const char *utf8_name = (const char *) constants->symbol_at(name_index)->as_utf8();
3404         name_buf = (char *) jvmtiMalloc(strlen(utf8_name)+1);
3405         strcpy(name_buf, utf8_name);
3406 
3407         const char *utf8_signature = (const char *) constants->symbol_at(signature_index)->as_utf8();
3408         sig_buf = (char *) jvmtiMalloc(strlen(utf8_signature)+1);
3409         strcpy(sig_buf, utf8_signature);
3410 
3411         if (generic_signature_index > 0) {
3412           const char *utf8_gen_sign = (const char *)
3413                                        constants->symbol_at(generic_signature_index)->as_utf8();
3414           gen_sig_buf = (char *) jvmtiMalloc(strlen(utf8_gen_sign)+1);
3415           strcpy(gen_sig_buf, utf8_gen_sign);
3416         }
3417       }
3418 
3419       // fill in the jvmti local variable table
3420       jvmti_table[i].start_location = start_location;
3421       jvmti_table[i].length = length;
3422       jvmti_table[i].name = name_buf;
3423       jvmti_table[i].signature = sig_buf;
3424       jvmti_table[i].generic_signature = gen_sig_buf;
3425       jvmti_table[i].slot = slot;
3426     }
3427   }
3428 
3429   // set results
3430   (*entry_count_ptr) = num_entries;
3431   (*table_ptr) = jvmti_table;
3432 
3433   return JVMTI_ERROR_NONE;
3434 } /* end GetLocalVariableTable */
3435 
3436 
3437 // method - pre-checked for validity, but may be NULL meaning obsolete method
3438 // bytecode_count_ptr - pre-checked for NULL
3439 // bytecodes_ptr - pre-checked for NULL
3440 jvmtiError
3441 JvmtiEnv::GetBytecodes(Method* method, jint* bytecode_count_ptr, unsigned char** bytecodes_ptr) {
3442   NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3443 
3444   methodHandle mh(Thread::current(), method);
3445   jint size = (jint)mh->code_size();
3446   jvmtiError err = allocate(size, bytecodes_ptr);
3447   if (err != JVMTI_ERROR_NONE) {
3448     return err;
3449   }
3450 
3451   (*bytecode_count_ptr) = size;
3452   // get byte codes
3453   JvmtiClassFileReconstituter::copy_bytecodes(mh, *bytecodes_ptr);
3454 
3455   return JVMTI_ERROR_NONE;
3456 } /* end GetBytecodes */
3457 
3458 
3459 // method - pre-checked for validity, but may be NULL meaning obsolete method
3460 // is_native_ptr - pre-checked for NULL
3461 jvmtiError
3462 JvmtiEnv::IsMethodNative(Method* method, jboolean* is_native_ptr) {
3463   NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3464   (*is_native_ptr) = method->is_native();
3465   return JVMTI_ERROR_NONE;
3466 } /* end IsMethodNative */
3467 
3468 
3469 // method - pre-checked for validity, but may be NULL meaning obsolete method
3470 // is_synthetic_ptr - pre-checked for NULL
3471 jvmtiError
3472 JvmtiEnv::IsMethodSynthetic(Method* method, jboolean* is_synthetic_ptr) {
3473   NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3474   (*is_synthetic_ptr) = method->is_synthetic();
3475   return JVMTI_ERROR_NONE;
3476 } /* end IsMethodSynthetic */
3477 
3478 
3479 // method - pre-checked for validity, but may be NULL meaning obsolete method
3480 // is_obsolete_ptr - pre-checked for NULL
3481 jvmtiError
3482 JvmtiEnv::IsMethodObsolete(Method* method, jboolean* is_obsolete_ptr) {
3483   if (use_version_1_0_semantics() &&
3484       get_capabilities()->can_redefine_classes == 0) {
3485     // This JvmtiEnv requested version 1.0 semantics and this function
3486     // requires the can_redefine_classes capability in version 1.0 so
3487     // we need to return an error here.
3488     return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
3489   }
3490 
3491   if (method == NULL || method->is_obsolete()) {
3492     *is_obsolete_ptr = true;
3493   } else {
3494     *is_obsolete_ptr = false;
3495   }
3496   return JVMTI_ERROR_NONE;
3497 } /* end IsMethodObsolete */
3498 
3499   //
3500   // Raw Monitor functions
3501   //
3502 
3503 // name - pre-checked for NULL
3504 // monitor_ptr - pre-checked for NULL
3505 jvmtiError
3506 JvmtiEnv::CreateRawMonitor(const char* name, jrawMonitorID* monitor_ptr) {
3507   JvmtiRawMonitor* rmonitor = new JvmtiRawMonitor(name);
3508   NULL_CHECK(rmonitor, JVMTI_ERROR_OUT_OF_MEMORY);
3509 
3510   *monitor_ptr = (jrawMonitorID)rmonitor;
3511 
3512   return JVMTI_ERROR_NONE;
3513 } /* end CreateRawMonitor */
3514 
3515 
3516 // rmonitor - pre-checked for validity
3517 jvmtiError
3518 JvmtiEnv::DestroyRawMonitor(JvmtiRawMonitor * rmonitor) {
3519   if (Threads::number_of_threads() == 0) {
3520     // Remove this monitor from pending raw monitors list
3521     // if it has entered in onload or start phase.
3522     JvmtiPendingMonitors::destroy(rmonitor);
3523   } else {
3524     Thread* thread  = Thread::current();
3525     if (rmonitor->owner() == thread) {
3526       // The caller owns this monitor which we are about to destroy.
3527       // We exit the underlying synchronization object so that the
3528       // "delete monitor" call below can work without an assertion
3529       // failure on systems that don't like destroying synchronization
3530       // objects that are locked.
3531       int r;
3532       int recursion = rmonitor->recursions();
3533       for (int i = 0; i <= recursion; i++) {
3534         r = rmonitor->raw_exit(thread);
3535         assert(r == JvmtiRawMonitor::M_OK, "raw_exit should have worked");
3536         if (r != JvmtiRawMonitor::M_OK) {  // robustness
3537           return JVMTI_ERROR_INTERNAL;
3538         }
3539       }
3540     }
3541     if (rmonitor->owner() != NULL) {
3542       // The caller is trying to destroy a monitor that is locked by
3543       // someone else. While this is not forbidden by the JVMTI
3544       // spec, it will cause an assertion failure on systems that don't
3545       // like destroying synchronization objects that are locked.
3546       // We indicate a problem with the error return (and leak the
3547       // monitor's memory).
3548       return JVMTI_ERROR_NOT_MONITOR_OWNER;
3549     }
3550   }
3551 
3552   delete rmonitor;
3553 
3554   return JVMTI_ERROR_NONE;
3555 } /* end DestroyRawMonitor */
3556 
3557 
3558 // rmonitor - pre-checked for validity
3559 jvmtiError
3560 JvmtiEnv::RawMonitorEnter(JvmtiRawMonitor * rmonitor) {
3561   if (Threads::number_of_threads() == 0) {
3562     // No JavaThreads exist so JvmtiRawMonitor enter cannot be
3563     // used, add this raw monitor to the pending list.
3564     // The pending monitors will be actually entered when
3565     // the VM is setup.
3566     // See transition_pending_raw_monitors in create_vm()
3567     // in thread.cpp.
3568     JvmtiPendingMonitors::enter(rmonitor);
3569   } else {
3570     Thread* thread = Thread::current();
3571     // 8266889: raw_enter changes Java thread state, needs WXWrite
3572     MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread));
3573     rmonitor->raw_enter(thread);
3574   }
3575   return JVMTI_ERROR_NONE;
3576 } /* end RawMonitorEnter */
3577 
3578 
3579 // rmonitor - pre-checked for validity
3580 jvmtiError
3581 JvmtiEnv::RawMonitorExit(JvmtiRawMonitor * rmonitor) {
3582   jvmtiError err = JVMTI_ERROR_NONE;
3583 
3584   if (Threads::number_of_threads() == 0) {
3585     // No JavaThreads exist so just remove this monitor from the pending list.
3586     // Bool value from exit is false if rmonitor is not in the list.
3587     if (!JvmtiPendingMonitors::exit(rmonitor)) {
3588       err = JVMTI_ERROR_NOT_MONITOR_OWNER;
3589     }
3590   } else {
3591     Thread* thread = Thread::current();
3592     int r = rmonitor->raw_exit(thread);
3593     if (r == JvmtiRawMonitor::M_ILLEGAL_MONITOR_STATE) {
3594       err = JVMTI_ERROR_NOT_MONITOR_OWNER;
3595     }
3596   }
3597   return err;
3598 } /* end RawMonitorExit */
3599 
3600 
3601 // rmonitor - pre-checked for validity
3602 jvmtiError
3603 JvmtiEnv::RawMonitorWait(JvmtiRawMonitor * rmonitor, jlong millis) {
3604   Thread* thread = Thread::current();
3605   // 8266889: raw_wait changes Java thread state, needs WXWrite
3606   MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread));
3607   int r = rmonitor->raw_wait(millis, thread);
3608 
3609   switch (r) {
3610   case JvmtiRawMonitor::M_INTERRUPTED:
3611     return JVMTI_ERROR_INTERRUPT;
3612   case JvmtiRawMonitor::M_ILLEGAL_MONITOR_STATE:
3613     return JVMTI_ERROR_NOT_MONITOR_OWNER;
3614   default:
3615     return JVMTI_ERROR_NONE;
3616   }
3617 } /* end RawMonitorWait */
3618 
3619 
3620 // rmonitor - pre-checked for validity
3621 jvmtiError
3622 JvmtiEnv::RawMonitorNotify(JvmtiRawMonitor * rmonitor) {
3623   Thread* thread = Thread::current();
3624   int r = rmonitor->raw_notify(thread);
3625 
3626   if (r == JvmtiRawMonitor::M_ILLEGAL_MONITOR_STATE) {
3627     return JVMTI_ERROR_NOT_MONITOR_OWNER;
3628   }
3629   return JVMTI_ERROR_NONE;
3630 } /* end RawMonitorNotify */
3631 
3632 
3633 // rmonitor - pre-checked for validity
3634 jvmtiError
3635 JvmtiEnv::RawMonitorNotifyAll(JvmtiRawMonitor * rmonitor) {
3636   Thread* thread = Thread::current();
3637   int r = rmonitor->raw_notifyAll(thread);
3638 
3639   if (r == JvmtiRawMonitor::M_ILLEGAL_MONITOR_STATE) {
3640     return JVMTI_ERROR_NOT_MONITOR_OWNER;
3641   }
3642   return JVMTI_ERROR_NONE;
3643 } /* end RawMonitorNotifyAll */
3644 
3645 
3646   //
3647   // JNI Function Interception functions
3648   //
3649 
3650 
3651 // function_table - pre-checked for NULL
3652 jvmtiError
3653 JvmtiEnv::SetJNIFunctionTable(const jniNativeInterface* function_table) {
3654   // Copy jni function table at safepoint.
3655   VM_JNIFunctionTableCopier copier(function_table);
3656   VMThread::execute(&copier);
3657 
3658   return JVMTI_ERROR_NONE;
3659 } /* end SetJNIFunctionTable */
3660 
3661 
3662 // function_table - pre-checked for NULL
3663 jvmtiError
3664 JvmtiEnv::GetJNIFunctionTable(jniNativeInterface** function_table) {
3665   *function_table=(jniNativeInterface*)jvmtiMalloc(sizeof(jniNativeInterface));
3666   if (*function_table == NULL)
3667     return JVMTI_ERROR_OUT_OF_MEMORY;
3668   memcpy(*function_table,(JavaThread::current())->get_jni_functions(),sizeof(jniNativeInterface));
3669   return JVMTI_ERROR_NONE;
3670 } /* end GetJNIFunctionTable */
3671 
3672 
3673   //
3674   // Event Management functions
3675   //
3676 
3677 jvmtiError
3678 JvmtiEnv::GenerateEvents(jvmtiEvent event_type) {
3679   // can only generate two event types
3680   if (event_type != JVMTI_EVENT_COMPILED_METHOD_LOAD &&
3681       event_type != JVMTI_EVENT_DYNAMIC_CODE_GENERATED) {
3682     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
3683   }
3684 
3685   // for compiled_method_load events we must check that the environment
3686   // has the can_generate_compiled_method_load_events capability.
3687   if (event_type == JVMTI_EVENT_COMPILED_METHOD_LOAD) {
3688     if (get_capabilities()->can_generate_compiled_method_load_events == 0) {
3689       return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
3690     }
3691     return JvmtiCodeBlobEvents::generate_compiled_method_load_events(this);
3692   } else {
3693     return JvmtiCodeBlobEvents::generate_dynamic_code_events(this);
3694   }
3695 
3696 } /* end GenerateEvents */
3697 
3698 
3699   //
3700   // Extension Mechanism functions
3701   //
3702 
3703 // extension_count_ptr - pre-checked for NULL
3704 // extensions - pre-checked for NULL
3705 jvmtiError
3706 JvmtiEnv::GetExtensionFunctions(jint* extension_count_ptr, jvmtiExtensionFunctionInfo** extensions) {
3707   return JvmtiExtensions::get_functions(this, extension_count_ptr, extensions);
3708 } /* end GetExtensionFunctions */
3709 
3710 
3711 // extension_count_ptr - pre-checked for NULL
3712 // extensions - pre-checked for NULL
3713 jvmtiError
3714 JvmtiEnv::GetExtensionEvents(jint* extension_count_ptr, jvmtiExtensionEventInfo** extensions) {
3715   return JvmtiExtensions::get_events(this, extension_count_ptr, extensions);
3716 } /* end GetExtensionEvents */
3717 
3718 
3719 // callback - NULL is a valid value, must be checked
3720 jvmtiError
3721 JvmtiEnv::SetExtensionEventCallback(jint extension_event_index, jvmtiExtensionEvent callback) {
3722   return JvmtiExtensions::set_event_callback(this, extension_event_index, callback);
3723 } /* end SetExtensionEventCallback */
3724 
3725   //
3726   // Timers functions
3727   //
3728 
3729 // info_ptr - pre-checked for NULL
3730 jvmtiError
3731 JvmtiEnv::GetCurrentThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) {
3732   os::current_thread_cpu_time_info(info_ptr);
3733   return JVMTI_ERROR_NONE;
3734 } /* end GetCurrentThreadCpuTimerInfo */
3735 
3736 
3737 // nanos_ptr - pre-checked for NULL
3738 jvmtiError
3739 JvmtiEnv::GetCurrentThreadCpuTime(jlong* nanos_ptr) {
3740   Thread* thread = Thread::current();
3741 
3742   // Surprizingly the GetCurrentThreadCpuTime is used by non-JavaThread's.
3743   if (thread->is_Java_thread()) {
3744     oop thread_obj = get_vthread_or_thread_oop(JavaThread::cast(thread));
3745 
3746     if (java_lang_VirtualThread::is_instance(thread_obj)) {
3747       return JVMTI_ERROR_INVALID_THREAD;
3748     }
3749   }
3750   *nanos_ptr = os::current_thread_cpu_time();
3751   return JVMTI_ERROR_NONE;
3752 } /* end GetCurrentThreadCpuTime */
3753 
3754 
3755 // info_ptr - pre-checked for NULL
3756 jvmtiError
3757 JvmtiEnv::GetThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) {
3758   os::thread_cpu_time_info(info_ptr);
3759   return JVMTI_ERROR_NONE;
3760 } /* end GetThreadCpuTimerInfo */
3761 
3762 
3763 // java_thread - protected by ThreadsListHandle and pre-checked
3764 // nanos_ptr - pre-checked for NULL
3765 jvmtiError
3766 JvmtiEnv::GetThreadCpuTime(JavaThread* java_thread, jlong* nanos_ptr) {
3767   *nanos_ptr = os::thread_cpu_time(java_thread);
3768   return JVMTI_ERROR_NONE;
3769 } /* end GetThreadCpuTime */
3770 
3771 
3772 // info_ptr - pre-checked for NULL
3773 jvmtiError
3774 JvmtiEnv::GetTimerInfo(jvmtiTimerInfo* info_ptr) {
3775   os::javaTimeNanos_info(info_ptr);
3776   return JVMTI_ERROR_NONE;
3777 } /* end GetTimerInfo */
3778 
3779 
3780 // nanos_ptr - pre-checked for NULL
3781 jvmtiError
3782 JvmtiEnv::GetTime(jlong* nanos_ptr) {
3783   *nanos_ptr = os::javaTimeNanos();
3784   return JVMTI_ERROR_NONE;
3785 } /* end GetTime */
3786 
3787 
3788 // processor_count_ptr - pre-checked for NULL
3789 jvmtiError
3790 JvmtiEnv::GetAvailableProcessors(jint* processor_count_ptr) {
3791   *processor_count_ptr = os::active_processor_count();
3792   return JVMTI_ERROR_NONE;
3793 } /* end GetAvailableProcessors */
3794 
3795 jvmtiError
3796 JvmtiEnv::SetHeapSamplingInterval(jint sampling_interval) {
3797   if (sampling_interval < 0) {
3798     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
3799   }
3800   ThreadHeapSampler::set_sampling_interval(sampling_interval);
3801   return JVMTI_ERROR_NONE;
3802 } /* end SetHeapSamplingInterval */
3803 
3804   //
3805   // System Properties functions
3806   //
3807 
3808 // count_ptr - pre-checked for NULL
3809 // property_ptr - pre-checked for NULL
3810 jvmtiError
3811 JvmtiEnv::GetSystemProperties(jint* count_ptr, char*** property_ptr) {
3812   jvmtiError err = JVMTI_ERROR_NONE;
3813 
3814   // Get the number of readable properties.
3815   *count_ptr = Arguments::PropertyList_readable_count(Arguments::system_properties());
3816 
3817   // Allocate memory to hold the exact number of readable properties.
3818   err = allocate(*count_ptr * sizeof(char *), (unsigned char **)property_ptr);
3819   if (err != JVMTI_ERROR_NONE) {
3820     return err;
3821   }
3822   int readable_count = 0;
3823   // Loop through the system properties until all the readable properties are found.
3824   for (SystemProperty* p = Arguments::system_properties(); p != NULL && readable_count < *count_ptr; p = p->next()) {
3825     if (p->is_readable()) {
3826       const char *key = p->key();
3827       char **tmp_value = *property_ptr+readable_count;
3828       readable_count++;
3829       err = allocate((strlen(key)+1) * sizeof(char), (unsigned char**)tmp_value);
3830       if (err == JVMTI_ERROR_NONE) {
3831         strcpy(*tmp_value, key);
3832       } else {
3833         // clean up previously allocated memory.
3834         for (int j = 0; j < readable_count; j++) {
3835           Deallocate((unsigned char*)*property_ptr+j);
3836         }
3837         Deallocate((unsigned char*)property_ptr);
3838         break;
3839       }
3840     }
3841   }
3842   assert(err != JVMTI_ERROR_NONE || readable_count == *count_ptr, "Bad readable property count");
3843   return err;
3844 } /* end GetSystemProperties */
3845 
3846 
3847 // property - pre-checked for NULL
3848 // value_ptr - pre-checked for NULL
3849 jvmtiError
3850 JvmtiEnv::GetSystemProperty(const char* property, char** value_ptr) {
3851   jvmtiError err = JVMTI_ERROR_NONE;
3852   const char *value;
3853 
3854   // Return JVMTI_ERROR_NOT_AVAILABLE if property is not readable or doesn't exist.
3855   value = Arguments::PropertyList_get_readable_value(Arguments::system_properties(), property);
3856   if (value == NULL) {
3857     err =  JVMTI_ERROR_NOT_AVAILABLE;
3858   } else {
3859     err = allocate((strlen(value)+1) * sizeof(char), (unsigned char **)value_ptr);
3860     if (err == JVMTI_ERROR_NONE) {
3861       strcpy(*value_ptr, value);
3862     }
3863   }
3864   return err;
3865 } /* end GetSystemProperty */
3866 
3867 
3868 // property - pre-checked for NULL
3869 // value - NULL is a valid value, must be checked
3870 jvmtiError
3871 JvmtiEnv::SetSystemProperty(const char* property, const char* value_ptr) {
3872   jvmtiError err =JVMTI_ERROR_NOT_AVAILABLE;
3873 
3874   for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) {
3875     if (strcmp(property, p->key()) == 0) {
3876       if (p->set_writeable_value(value_ptr)) {
3877         err =  JVMTI_ERROR_NONE;
3878       }
3879     }
3880   }
3881   return err;
3882 } /* end SetSystemProperty */
--- EOF ---