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