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