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