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 "precompiled.hpp" 26 #include "classfile/classLoaderExt.hpp" 27 #include "classfile/javaClasses.inline.hpp" 28 #include "classfile/stringTable.hpp" 29 #include "classfile/modules.hpp" 30 #include "classfile/systemDictionary.hpp" 31 #include "classfile/vmClasses.hpp" 32 #include "classfile/vmSymbols.hpp" 33 #include "gc/shared/collectedHeap.hpp" 34 #include "interpreter/bytecodeStream.hpp" 35 #include "interpreter/interpreter.hpp" 36 #include "jfr/jfrEvents.hpp" 37 #include "jvmtifiles/jvmtiEnv.hpp" 38 #include "logging/log.hpp" 39 #include "logging/logConfiguration.hpp" 40 #include "memory/allocation.hpp" 41 #include "memory/resourceArea.hpp" 42 #include "memory/universe.hpp" 43 #include "oops/instanceKlass.hpp" 44 #include "oops/klass.inline.hpp" 45 #include "oops/objArrayOop.inline.hpp" 46 #include "oops/oop.inline.hpp" 47 #include "prims/jniCheck.hpp" 48 #include "prims/jvm_misc.hpp" 49 #include "prims/jvmtiAgentThread.hpp" 50 #include "prims/jvmtiClassFileReconstituter.hpp" 51 #include "prims/jvmtiCodeBlobEvents.hpp" 52 #include "prims/jvmtiExtensions.hpp" 53 #include "prims/jvmtiGetLoadedClasses.hpp" 54 #include "prims/jvmtiImpl.hpp" 55 #include "prims/jvmtiManageCapabilities.hpp" 56 #include "prims/jvmtiRawMonitor.hpp" 57 #include "prims/jvmtiRedefineClasses.hpp" 58 #include "prims/jvmtiTagMap.hpp" 59 #include "prims/jvmtiThreadState.inline.hpp" 60 #include "prims/jvmtiUtil.hpp" 61 #include "runtime/arguments.hpp" 62 #include "runtime/deoptimization.hpp" 63 #include "runtime/fieldDescriptor.inline.hpp" 64 #include "runtime/handles.inline.hpp" 65 #include "runtime/interfaceSupport.inline.hpp" 66 #include "runtime/javaCalls.hpp" 67 #include "runtime/javaThread.inline.hpp" 68 #include "runtime/jfieldIDWorkaround.hpp" 69 #include "runtime/jniHandles.inline.hpp" 70 #include "runtime/objectMonitor.inline.hpp" 71 #include "runtime/os.hpp" 72 #include "runtime/osThread.hpp" 73 #include "runtime/reflectionUtils.hpp" 74 #include "runtime/signature.hpp" 75 #include "runtime/threadHeapSampler.hpp" 76 #include "runtime/threads.hpp" 77 #include "runtime/threadSMR.hpp" 78 #include "runtime/timerTrace.hpp" 79 #include "runtime/vframe.inline.hpp" 80 #include "runtime/vmThread.hpp" 81 #include "services/threadService.hpp" 82 #include "utilities/exceptions.hpp" 83 #include "utilities/preserveException.hpp" 84 #include "utilities/utf8.hpp" 85 86 87 #define FIXLATER 0 // REMOVE this when completed. 88 89 // FIXLATER: hook into JvmtiTrace 90 #define TraceJVMTICalls false 91 92 JvmtiEnv::JvmtiEnv(jint version) : JvmtiEnvBase(version) { 93 } 94 95 JvmtiEnv::~JvmtiEnv() { 96 } 97 98 JvmtiEnv* 99 JvmtiEnv::create_a_jvmti(jint version) { 100 return new JvmtiEnv(version); 101 } 102 103 // VM operation class to copy jni function table at safepoint. 104 // More than one java threads or jvmti agents may be reading/ 105 // modifying jni function tables. To reduce the risk of bad 106 // interaction b/w these threads it is copied at safepoint. 107 class VM_JNIFunctionTableCopier : public VM_Operation { 108 private: 109 const struct JNINativeInterface_ *_function_table; 110 public: 111 VM_JNIFunctionTableCopier(const struct JNINativeInterface_ *func_tbl) { 112 _function_table = func_tbl; 113 }; 114 115 VMOp_Type type() const { return VMOp_JNIFunctionTableCopier; } 116 void doit() { 117 copy_jni_function_table(_function_table); 118 }; 119 }; 120 121 // 122 // Do not change the "prefix" marker below, everything above it is copied 123 // unchanged into the filled stub, everything below is controlled by the 124 // stub filler (only method bodies are carried forward, and then only for 125 // functionality still in the spec). 126 // 127 // end file prefix 128 129 // 130 // Memory Management functions 131 // 132 133 // mem_ptr - pre-checked for null 134 jvmtiError 135 JvmtiEnv::Allocate(jlong size, unsigned char** mem_ptr) { 136 return allocate(size, mem_ptr); 137 } /* end Allocate */ 138 139 140 // mem - null is a valid value, must be checked 141 jvmtiError 142 JvmtiEnv::Deallocate(unsigned char* mem) { 143 return deallocate(mem); 144 } /* end Deallocate */ 145 146 // thread - NOT protected by ThreadsListHandle and NOT pre-checked 147 // data - null is a valid value, must be checked 148 jvmtiError 149 JvmtiEnv::SetThreadLocalStorage(jthread thread, const void* data) { 150 JavaThread* current = JavaThread::current(); 151 JvmtiThreadState* state = nullptr; 152 JvmtiVTMSTransitionDisabler disabler(thread); 153 ThreadsListHandle tlh(current); 154 155 JavaThread* java_thread = nullptr; 156 oop thread_obj = nullptr; 157 if (thread == nullptr) { 158 java_thread = current; 159 state = java_thread->jvmti_thread_state(); 160 } else { 161 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current, &java_thread, &thread_obj); 162 if (err != JVMTI_ERROR_NONE) { 163 return err; 164 } 165 state = java_lang_Thread::jvmti_thread_state(thread_obj); 166 } 167 if (state == nullptr) { 168 if (data == nullptr) { 169 // leaving state unset same as data set to null 170 return JVMTI_ERROR_NONE; 171 } 172 // otherwise, create the state 173 HandleMark hm(current); 174 Handle thread_handle(current, thread_obj); 175 state = JvmtiThreadState::state_for(java_thread, thread_handle); 176 if (state == nullptr) { 177 return JVMTI_ERROR_THREAD_NOT_ALIVE; 178 } 179 } 180 state->env_thread_state(this)->set_agent_thread_local_storage_data((void*)data); 181 return JVMTI_ERROR_NONE; 182 } /* end SetThreadLocalStorage */ 183 184 185 // thread - NOT protected by ThreadsListHandle and NOT pre-checked 186 // data_ptr - pre-checked for null 187 jvmtiError 188 JvmtiEnv::GetThreadLocalStorage(jthread thread, void** data_ptr) { 189 JavaThread* current_thread = JavaThread::current(); 190 if (thread == nullptr) { 191 JvmtiThreadState* state = current_thread->jvmti_thread_state(); 192 *data_ptr = (state == nullptr) ? nullptr : 193 state->env_thread_state(this)->get_agent_thread_local_storage_data(); 194 } else { 195 // jvmti_GetThreadLocalStorage is "in native" and doesn't transition 196 // the thread to _thread_in_vm. However, when the TLS for a thread 197 // other than the current thread is required we need to transition 198 // from native so as to resolve the jthread. 199 200 MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, current_thread)); 201 ThreadInVMfromNative __tiv(current_thread); 202 VM_ENTRY_BASE(jvmtiError, JvmtiEnv::GetThreadLocalStorage , current_thread) 203 debug_only(VMNativeEntryWrapper __vew;) 204 205 JvmtiVTMSTransitionDisabler disabler(thread); 206 ThreadsListHandle tlh(current_thread); 207 208 JavaThread* java_thread = nullptr; 209 oop thread_obj = nullptr; 210 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj); 211 if (err != JVMTI_ERROR_NONE) { 212 return err; 213 } 214 215 HandleMark hm(current_thread); 216 Handle thread_handle(current_thread, thread_obj); 217 JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread, thread_handle); 218 *data_ptr = (state == nullptr) ? nullptr : 219 state->env_thread_state(this)->get_agent_thread_local_storage_data(); 220 } 221 return JVMTI_ERROR_NONE; 222 } /* end GetThreadLocalStorage */ 223 224 // 225 // Module functions 226 // 227 228 // module_count_ptr - pre-checked for null 229 // modules_ptr - pre-checked for null 230 jvmtiError 231 JvmtiEnv::GetAllModules(jint* module_count_ptr, jobject** modules_ptr) { 232 JvmtiModuleClosure jmc; 233 234 return jmc.get_all_modules(this, module_count_ptr, modules_ptr); 235 } /* end GetAllModules */ 236 237 238 // class_loader - null is a valid value, must be pre-checked 239 // package_name - pre-checked for null 240 // module_ptr - pre-checked for null 241 jvmtiError 242 JvmtiEnv::GetNamedModule(jobject class_loader, const char* package_name, jobject* module_ptr) { 243 JavaThread* THREAD = JavaThread::current(); // For exception macros. 244 ResourceMark rm(THREAD); 245 246 Handle h_loader (THREAD, JNIHandles::resolve(class_loader)); 247 // Check that loader is a subclass of java.lang.ClassLoader. 248 if (h_loader.not_null() && !java_lang_ClassLoader::is_subclass(h_loader->klass())) { 249 return JVMTI_ERROR_ILLEGAL_ARGUMENT; 250 } 251 oop module = Modules::get_named_module(h_loader, package_name); 252 *module_ptr = module != nullptr ? JNIHandles::make_local(THREAD, module) : nullptr; 253 return JVMTI_ERROR_NONE; 254 } /* end GetNamedModule */ 255 256 257 // module - pre-checked for null 258 // to_module - pre-checked for null 259 jvmtiError 260 JvmtiEnv::AddModuleReads(jobject module, jobject to_module) { 261 JavaThread* THREAD = JavaThread::current(); // For exception macros. 262 263 // check module 264 Handle h_module(THREAD, JNIHandles::resolve(module)); 265 if (!java_lang_Module::is_instance(h_module())) { 266 return JVMTI_ERROR_INVALID_MODULE; 267 } 268 // check to_module 269 Handle h_to_module(THREAD, JNIHandles::resolve(to_module)); 270 if (!java_lang_Module::is_instance(h_to_module())) { 271 return JVMTI_ERROR_INVALID_MODULE; 272 } 273 return JvmtiExport::add_module_reads(h_module, h_to_module, THREAD); 274 } /* end AddModuleReads */ 275 276 277 // module - pre-checked for null 278 // pkg_name - pre-checked for null 279 // to_module - pre-checked for null 280 jvmtiError 281 JvmtiEnv::AddModuleExports(jobject module, const char* pkg_name, jobject to_module) { 282 JavaThread* THREAD = JavaThread::current(); // For exception macros. 283 Handle h_pkg = java_lang_String::create_from_str(pkg_name, THREAD); 284 285 // check module 286 Handle h_module(THREAD, JNIHandles::resolve(module)); 287 if (!java_lang_Module::is_instance(h_module())) { 288 return JVMTI_ERROR_INVALID_MODULE; 289 } 290 // check to_module 291 Handle h_to_module(THREAD, JNIHandles::resolve(to_module)); 292 if (!java_lang_Module::is_instance(h_to_module())) { 293 return JVMTI_ERROR_INVALID_MODULE; 294 } 295 return JvmtiExport::add_module_exports(h_module, h_pkg, h_to_module, THREAD); 296 } /* end AddModuleExports */ 297 298 299 // module - pre-checked for null 300 // pkg_name - pre-checked for null 301 // to_module - pre-checked for null 302 jvmtiError 303 JvmtiEnv::AddModuleOpens(jobject module, const char* pkg_name, jobject to_module) { 304 JavaThread* THREAD = JavaThread::current(); // For exception macros. 305 Handle h_pkg = java_lang_String::create_from_str(pkg_name, THREAD); 306 307 // check module 308 Handle h_module(THREAD, JNIHandles::resolve(module)); 309 if (!java_lang_Module::is_instance(h_module())) { 310 return JVMTI_ERROR_INVALID_MODULE; 311 } 312 // check to_module 313 Handle h_to_module(THREAD, JNIHandles::resolve(to_module)); 314 if (!java_lang_Module::is_instance(h_to_module())) { 315 return JVMTI_ERROR_INVALID_MODULE; 316 } 317 return JvmtiExport::add_module_opens(h_module, h_pkg, h_to_module, THREAD); 318 } /* end AddModuleOpens */ 319 320 321 // module - pre-checked for null 322 // service - pre-checked for null 323 jvmtiError 324 JvmtiEnv::AddModuleUses(jobject module, jclass service) { 325 JavaThread* THREAD = JavaThread::current(); // For exception macros. 326 327 // check module 328 Handle h_module(THREAD, JNIHandles::resolve(module)); 329 if (!java_lang_Module::is_instance(h_module())) { 330 return JVMTI_ERROR_INVALID_MODULE; 331 } 332 // check service 333 Handle h_service(THREAD, JNIHandles::resolve_external_guard(service)); 334 if (!java_lang_Class::is_instance(h_service()) || 335 java_lang_Class::is_primitive(h_service())) { 336 return JVMTI_ERROR_INVALID_CLASS; 337 } 338 return JvmtiExport::add_module_uses(h_module, h_service, THREAD); 339 } /* end AddModuleUses */ 340 341 342 // module - pre-checked for null 343 // service - pre-checked for null 344 // impl_class - pre-checked for null 345 jvmtiError 346 JvmtiEnv::AddModuleProvides(jobject module, jclass service, jclass impl_class) { 347 JavaThread* THREAD = JavaThread::current(); // For exception macros. 348 349 // check module 350 Handle h_module(THREAD, JNIHandles::resolve(module)); 351 if (!java_lang_Module::is_instance(h_module())) { 352 return JVMTI_ERROR_INVALID_MODULE; 353 } 354 // check service 355 Handle h_service(THREAD, JNIHandles::resolve_external_guard(service)); 356 if (!java_lang_Class::is_instance(h_service()) || 357 java_lang_Class::is_primitive(h_service())) { 358 return JVMTI_ERROR_INVALID_CLASS; 359 } 360 // check impl_class 361 Handle h_impl_class(THREAD, JNIHandles::resolve_external_guard(impl_class)); 362 if (!java_lang_Class::is_instance(h_impl_class()) || 363 java_lang_Class::is_primitive(h_impl_class())) { 364 return JVMTI_ERROR_INVALID_CLASS; 365 } 366 return JvmtiExport::add_module_provides(h_module, h_service, h_impl_class, THREAD); 367 } /* end AddModuleProvides */ 368 369 // module - pre-checked for null 370 // is_modifiable_class_ptr - pre-checked for null 371 jvmtiError 372 JvmtiEnv::IsModifiableModule(jobject module, jboolean* is_modifiable_module_ptr) { 373 JavaThread* current = JavaThread::current(); 374 375 // check module 376 Handle h_module(current, JNIHandles::resolve(module)); 377 if (!java_lang_Module::is_instance(h_module())) { 378 return JVMTI_ERROR_INVALID_MODULE; 379 } 380 381 *is_modifiable_module_ptr = JNI_TRUE; 382 return JVMTI_ERROR_NONE; 383 } /* end IsModifiableModule */ 384 385 386 // 387 // Class functions 388 // 389 390 // class_count_ptr - pre-checked for null 391 // classes_ptr - pre-checked for null 392 jvmtiError 393 JvmtiEnv::GetLoadedClasses(jint* class_count_ptr, jclass** classes_ptr) { 394 return JvmtiGetLoadedClasses::getLoadedClasses(this, class_count_ptr, classes_ptr); 395 } /* end GetLoadedClasses */ 396 397 398 // initiating_loader - null is a valid value, must be checked 399 // class_count_ptr - pre-checked for null 400 // classes_ptr - pre-checked for null 401 jvmtiError 402 JvmtiEnv::GetClassLoaderClasses(jobject initiating_loader, jint* class_count_ptr, jclass** classes_ptr) { 403 return JvmtiGetLoadedClasses::getClassLoaderClasses(this, initiating_loader, 404 class_count_ptr, classes_ptr); 405 } /* end GetClassLoaderClasses */ 406 407 // k_mirror - may be primitive, this must be checked 408 // is_modifiable_class_ptr - pre-checked for null 409 jvmtiError 410 JvmtiEnv::IsModifiableClass(oop k_mirror, jboolean* is_modifiable_class_ptr) { 411 *is_modifiable_class_ptr = VM_RedefineClasses::is_modifiable_class(k_mirror)? 412 JNI_TRUE : JNI_FALSE; 413 return JVMTI_ERROR_NONE; 414 } /* end IsModifiableClass */ 415 416 // class_count - pre-checked to be greater than or equal to 0 417 // classes - pre-checked for null 418 jvmtiError 419 JvmtiEnv::RetransformClasses(jint class_count, const jclass* classes) { 420 //TODO: add locking 421 422 int index; 423 JavaThread* current_thread = JavaThread::current(); 424 ResourceMark rm(current_thread); 425 426 jvmtiClassDefinition* class_definitions = 427 NEW_RESOURCE_ARRAY(jvmtiClassDefinition, class_count); 428 NULL_CHECK(class_definitions, JVMTI_ERROR_OUT_OF_MEMORY); 429 430 for (index = 0; index < class_count; index++) { 431 HandleMark hm(current_thread); 432 433 jclass jcls = classes[index]; 434 oop k_mirror = JNIHandles::resolve_external_guard(jcls); 435 if (k_mirror == nullptr) { 436 return JVMTI_ERROR_INVALID_CLASS; 437 } 438 if (!k_mirror->is_a(vmClasses::Class_klass())) { 439 return JVMTI_ERROR_INVALID_CLASS; 440 } 441 442 if (!VM_RedefineClasses::is_modifiable_class(k_mirror)) { 443 return JVMTI_ERROR_UNMODIFIABLE_CLASS; 444 } 445 446 Klass* klass = java_lang_Class::as_Klass(k_mirror); 447 448 jint status = klass->jvmti_class_status(); 449 if (status & (JVMTI_CLASS_STATUS_ERROR)) { 450 return JVMTI_ERROR_INVALID_CLASS; 451 } 452 453 InstanceKlass* ik = InstanceKlass::cast(klass); 454 if (ik->get_cached_class_file_bytes() == nullptr) { 455 // Not cached, we need to reconstitute the class file from the 456 // VM representation. We don't attach the reconstituted class 457 // bytes to the InstanceKlass here because they have not been 458 // validated and we're not at a safepoint. 459 JvmtiClassFileReconstituter reconstituter(ik); 460 if (reconstituter.get_error() != JVMTI_ERROR_NONE) { 461 return reconstituter.get_error(); 462 } 463 464 class_definitions[index].class_byte_count = (jint)reconstituter.class_file_size(); 465 class_definitions[index].class_bytes = (unsigned char*) 466 reconstituter.class_file_bytes(); 467 } else { 468 // it is cached, get it from the cache 469 class_definitions[index].class_byte_count = ik->get_cached_class_file_len(); 470 class_definitions[index].class_bytes = ik->get_cached_class_file_bytes(); 471 } 472 class_definitions[index].klass = jcls; 473 } 474 EventRetransformClasses event; 475 VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_retransform); 476 VMThread::execute(&op); 477 jvmtiError error = op.check_error(); 478 if (error == JVMTI_ERROR_NONE) { 479 event.set_classCount(class_count); 480 event.set_redefinitionId(op.id()); 481 event.commit(); 482 } 483 return error; 484 } /* end RetransformClasses */ 485 486 487 // class_count - pre-checked to be greater than or equal to 0 488 // class_definitions - pre-checked for null 489 jvmtiError 490 JvmtiEnv::RedefineClasses(jint class_count, const jvmtiClassDefinition* class_definitions) { 491 //TODO: add locking 492 EventRedefineClasses event; 493 VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_redefine); 494 VMThread::execute(&op); 495 jvmtiError error = op.check_error(); 496 if (error == JVMTI_ERROR_NONE) { 497 event.set_classCount(class_count); 498 event.set_redefinitionId(op.id()); 499 event.commit(); 500 } 501 return error; 502 } /* end RedefineClasses */ 503 504 505 // 506 // Object functions 507 // 508 509 // size_ptr - pre-checked for null 510 jvmtiError 511 JvmtiEnv::GetObjectSize(jobject object, jlong* size_ptr) { 512 oop mirror = JNIHandles::resolve_external_guard(object); 513 NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT); 514 *size_ptr = (jlong)mirror->size() * wordSize; 515 return JVMTI_ERROR_NONE; 516 } /* end GetObjectSize */ 517 518 // 519 // Method functions 520 // 521 522 // prefix - null is a valid value, must be checked 523 jvmtiError 524 JvmtiEnv::SetNativeMethodPrefix(const char* prefix) { 525 return prefix == nullptr? 526 SetNativeMethodPrefixes(0, nullptr) : 527 SetNativeMethodPrefixes(1, (char**)&prefix); 528 } /* end SetNativeMethodPrefix */ 529 530 531 // prefix_count - pre-checked to be greater than or equal to 0 532 // prefixes - pre-checked for null 533 jvmtiError 534 JvmtiEnv::SetNativeMethodPrefixes(jint prefix_count, char** prefixes) { 535 // Have to grab JVMTI thread state lock to be sure that some thread 536 // isn't accessing the prefixes at the same time we are setting them. 537 // No locks during VM bring-up. 538 if (Threads::number_of_threads() == 0) { 539 return set_native_method_prefixes(prefix_count, prefixes); 540 } else { 541 MutexLocker mu(JvmtiThreadState_lock); 542 return set_native_method_prefixes(prefix_count, prefixes); 543 } 544 } /* end SetNativeMethodPrefixes */ 545 546 // 547 // Event Management functions 548 // 549 550 // callbacks - null is a valid value, must be checked 551 // size_of_callbacks - pre-checked to be greater than or equal to 0 552 jvmtiError 553 JvmtiEnv::SetEventCallbacks(const jvmtiEventCallbacks* callbacks, jint size_of_callbacks) { 554 JvmtiVTMSTransitionDisabler disabler; 555 JvmtiEventController::set_event_callbacks(this, callbacks, size_of_callbacks); 556 return JVMTI_ERROR_NONE; 557 } /* end SetEventCallbacks */ 558 559 560 // event_thread - null is a valid value, must be checked 561 jvmtiError 562 JvmtiEnv::SetEventNotificationMode(jvmtiEventMode mode, jvmtiEvent event_type, jthread event_thread, ...) { 563 bool enabled = (mode == JVMTI_ENABLE); 564 565 // event_type must be valid 566 if (!JvmtiEventController::is_valid_event_type(event_type)) { 567 return JVMTI_ERROR_INVALID_EVENT_TYPE; 568 } 569 570 // assure that needed capabilities are present 571 if (enabled && !JvmtiUtil::has_event_capability(event_type, get_capabilities())) { 572 return JVMTI_ERROR_MUST_POSSESS_CAPABILITY; 573 } 574 575 if (event_type == JVMTI_EVENT_CLASS_FILE_LOAD_HOOK && enabled) { 576 record_class_file_load_hook_enabled(); 577 } 578 JvmtiVTMSTransitionDisabler disabler; 579 580 if (event_thread == nullptr) { 581 // Can be called at Agent_OnLoad() time with event_thread == nullptr 582 // when Thread::current() does not work yet so we cannot create a 583 // ThreadsListHandle that is common to both thread-specific and 584 // global code paths. 585 586 JvmtiEventController::set_user_enabled(this, nullptr, (oop) nullptr, event_type, enabled); 587 } else { 588 // We have a specified event_thread. 589 JavaThread* current = JavaThread::current(); 590 ThreadsListHandle tlh(current); 591 592 JavaThread* java_thread = nullptr; 593 oop thread_obj = nullptr; 594 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), event_thread, current, &java_thread, &thread_obj); 595 if (err != JVMTI_ERROR_NONE) { 596 return err; 597 } 598 599 // global events cannot be controlled at thread level. 600 if (JvmtiEventController::is_global_event(event_type)) { 601 return JVMTI_ERROR_ILLEGAL_ARGUMENT; 602 } 603 604 JvmtiEventController::set_user_enabled(this, java_thread, thread_obj, event_type, enabled); 605 } 606 607 return JVMTI_ERROR_NONE; 608 } /* end SetEventNotificationMode */ 609 610 // 611 // Capability functions 612 // 613 614 // capabilities_ptr - pre-checked for null 615 jvmtiError 616 JvmtiEnv::GetPotentialCapabilities(jvmtiCapabilities* capabilities_ptr) { 617 JvmtiManageCapabilities::get_potential_capabilities(get_capabilities(), 618 get_prohibited_capabilities(), 619 capabilities_ptr); 620 return JVMTI_ERROR_NONE; 621 } /* end GetPotentialCapabilities */ 622 623 624 // capabilities_ptr - pre-checked for null 625 jvmtiError 626 JvmtiEnv::AddCapabilities(const jvmtiCapabilities* capabilities_ptr) { 627 return JvmtiManageCapabilities::add_capabilities(get_capabilities(), 628 get_prohibited_capabilities(), 629 capabilities_ptr, 630 get_capabilities()); 631 } /* end AddCapabilities */ 632 633 634 // capabilities_ptr - pre-checked for null 635 jvmtiError 636 JvmtiEnv::RelinquishCapabilities(const jvmtiCapabilities* capabilities_ptr) { 637 JvmtiManageCapabilities::relinquish_capabilities(get_capabilities(), capabilities_ptr, get_capabilities()); 638 return JVMTI_ERROR_NONE; 639 } /* end RelinquishCapabilities */ 640 641 642 // capabilities_ptr - pre-checked for null 643 jvmtiError 644 JvmtiEnv::GetCapabilities(jvmtiCapabilities* capabilities_ptr) { 645 JvmtiManageCapabilities::copy_capabilities(get_capabilities(), capabilities_ptr); 646 return JVMTI_ERROR_NONE; 647 } /* end GetCapabilities */ 648 649 // 650 // Class Loader Search functions 651 // 652 653 // segment - pre-checked for null 654 jvmtiError 655 JvmtiEnv::AddToBootstrapClassLoaderSearch(const char* segment) { 656 jvmtiPhase phase = get_phase(); 657 if (phase == JVMTI_PHASE_ONLOAD) { 658 Arguments::append_sysclasspath(segment); 659 return JVMTI_ERROR_NONE; 660 } else if (use_version_1_0_semantics()) { 661 // This JvmtiEnv requested version 1.0 semantics and this function 662 // is only allowed in the ONLOAD phase in version 1.0 so we need to 663 // return an error here. 664 return JVMTI_ERROR_WRONG_PHASE; 665 } else if (phase == JVMTI_PHASE_LIVE) { 666 // The phase is checked by the wrapper that called this function, 667 // but this thread could be racing with the thread that is 668 // terminating the VM so we check one more time. 669 670 // create the zip entry 671 ClassPathZipEntry* zip_entry = ClassLoader::create_class_path_zip_entry(segment, true); 672 if (zip_entry == nullptr) { 673 return JVMTI_ERROR_ILLEGAL_ARGUMENT; 674 } 675 676 // add the jar file to the bootclasspath 677 log_info(class, load)("opened: %s", zip_entry->name()); 678 #if INCLUDE_CDS 679 ClassLoaderExt::append_boot_classpath(zip_entry); 680 #else 681 ClassLoader::add_to_boot_append_entries(zip_entry); 682 #endif 683 return JVMTI_ERROR_NONE; 684 } else { 685 return JVMTI_ERROR_WRONG_PHASE; 686 } 687 688 } /* end AddToBootstrapClassLoaderSearch */ 689 690 691 // segment - pre-checked for null 692 jvmtiError 693 JvmtiEnv::AddToSystemClassLoaderSearch(const char* segment) { 694 jvmtiPhase phase = get_phase(); 695 696 if (phase == JVMTI_PHASE_ONLOAD) { 697 for (SystemProperty* p = Arguments::system_properties(); p != nullptr; p = p->next()) { 698 if (strcmp("java.class.path", p->key()) == 0) { 699 p->append_value(segment); 700 break; 701 } 702 } 703 return JVMTI_ERROR_NONE; 704 } else if (phase == JVMTI_PHASE_LIVE) { 705 // The phase is checked by the wrapper that called this function, 706 // but this thread could be racing with the thread that is 707 // terminating the VM so we check one more time. 708 JavaThread* THREAD = JavaThread::current(); // For exception macros. 709 HandleMark hm(THREAD); 710 711 // create the zip entry (which will open the zip file and hence 712 // check that the segment is indeed a zip file). 713 ClassPathZipEntry* zip_entry = ClassLoader::create_class_path_zip_entry(segment, false); 714 if (zip_entry == nullptr) { 715 return JVMTI_ERROR_ILLEGAL_ARGUMENT; 716 } 717 delete zip_entry; // no longer needed 718 719 Handle loader(THREAD, SystemDictionary::java_system_loader()); 720 721 // need the path as java.lang.String 722 Handle path = java_lang_String::create_from_platform_dependent_str(segment, THREAD); 723 if (HAS_PENDING_EXCEPTION) { 724 CLEAR_PENDING_EXCEPTION; 725 return JVMTI_ERROR_INTERNAL; 726 } 727 728 // Invoke the appendToClassPathForInstrumentation method - if the method 729 // is not found it means the loader doesn't support adding to the class path 730 // in the live phase. 731 { 732 JavaValue res(T_VOID); 733 JavaCalls::call_special(&res, 734 loader, 735 loader->klass(), 736 vmSymbols::appendToClassPathForInstrumentation_name(), 737 vmSymbols::appendToClassPathForInstrumentation_signature(), 738 path, 739 THREAD); 740 if (HAS_PENDING_EXCEPTION) { 741 Symbol* ex_name = PENDING_EXCEPTION->klass()->name(); 742 CLEAR_PENDING_EXCEPTION; 743 744 if (ex_name == vmSymbols::java_lang_NoSuchMethodError()) { 745 return JVMTI_ERROR_CLASS_LOADER_UNSUPPORTED; 746 } else { 747 return JVMTI_ERROR_INTERNAL; 748 } 749 } 750 } 751 752 return JVMTI_ERROR_NONE; 753 } else { 754 return JVMTI_ERROR_WRONG_PHASE; 755 } 756 } /* end AddToSystemClassLoaderSearch */ 757 758 // 759 // General functions 760 // 761 762 // phase_ptr - pre-checked for null 763 jvmtiError 764 JvmtiEnv::GetPhase(jvmtiPhase* phase_ptr) { 765 *phase_ptr = phase(); 766 return JVMTI_ERROR_NONE; 767 } /* end GetPhase */ 768 769 770 jvmtiError 771 JvmtiEnv::DisposeEnvironment() { 772 dispose(); 773 return JVMTI_ERROR_NONE; 774 } /* end DisposeEnvironment */ 775 776 777 // data - null is a valid value, must be checked 778 jvmtiError 779 JvmtiEnv::SetEnvironmentLocalStorage(const void* data) { 780 set_env_local_storage(data); 781 return JVMTI_ERROR_NONE; 782 } /* end SetEnvironmentLocalStorage */ 783 784 785 // data_ptr - pre-checked for null 786 jvmtiError 787 JvmtiEnv::GetEnvironmentLocalStorage(void** data_ptr) { 788 *data_ptr = (void*)get_env_local_storage(); 789 return JVMTI_ERROR_NONE; 790 } /* end GetEnvironmentLocalStorage */ 791 792 // version_ptr - pre-checked for null 793 jvmtiError 794 JvmtiEnv::GetVersionNumber(jint* version_ptr) { 795 *version_ptr = JVMTI_VERSION; 796 return JVMTI_ERROR_NONE; 797 } /* end GetVersionNumber */ 798 799 800 // name_ptr - pre-checked for null 801 jvmtiError 802 JvmtiEnv::GetErrorName(jvmtiError error, char** name_ptr) { 803 if (error < JVMTI_ERROR_NONE || error > JVMTI_ERROR_MAX) { 804 return JVMTI_ERROR_ILLEGAL_ARGUMENT; 805 } 806 const char *name = JvmtiUtil::error_name(error); 807 if (name == nullptr) { 808 return JVMTI_ERROR_ILLEGAL_ARGUMENT; 809 } 810 size_t len = strlen(name) + 1; 811 jvmtiError err = allocate(len, (unsigned char**)name_ptr); 812 if (err == JVMTI_ERROR_NONE) { 813 memcpy(*name_ptr, name, len); 814 } 815 return err; 816 } /* end GetErrorName */ 817 818 819 jvmtiError 820 JvmtiEnv::SetVerboseFlag(jvmtiVerboseFlag flag, jboolean value) { 821 LogLevelType level = value == 0 ? LogLevel::Off : LogLevel::Info; 822 switch (flag) { 823 case JVMTI_VERBOSE_OTHER: 824 // ignore 825 break; 826 case JVMTI_VERBOSE_CLASS: 827 LogConfiguration::configure_stdout(level, false, LOG_TAGS(class, unload)); 828 LogConfiguration::configure_stdout(level, false, LOG_TAGS(class, load)); 829 break; 830 case JVMTI_VERBOSE_GC: 831 LogConfiguration::configure_stdout(level, true, LOG_TAGS(gc)); 832 break; 833 case JVMTI_VERBOSE_JNI: 834 level = value == 0 ? LogLevel::Off : LogLevel::Debug; 835 LogConfiguration::configure_stdout(level, true, LOG_TAGS(jni, resolve)); 836 break; 837 default: 838 return JVMTI_ERROR_ILLEGAL_ARGUMENT; 839 }; 840 return JVMTI_ERROR_NONE; 841 } /* end SetVerboseFlag */ 842 843 844 // format_ptr - pre-checked for null 845 jvmtiError 846 JvmtiEnv::GetJLocationFormat(jvmtiJlocationFormat* format_ptr) { 847 *format_ptr = JVMTI_JLOCATION_JVMBCI; 848 return JVMTI_ERROR_NONE; 849 } /* end GetJLocationFormat */ 850 851 // 852 // Thread functions 853 // 854 855 // thread - NOT protected by ThreadsListHandle and NOT pre-checked 856 // thread_state_ptr - pre-checked for null 857 jvmtiError 858 JvmtiEnv::GetThreadState(jthread thread, jint* thread_state_ptr) { 859 JavaThread* current_thread = JavaThread::current(); 860 JvmtiVTMSTransitionDisabler disabler(thread); 861 ThreadsListHandle tlh(current_thread); 862 863 JavaThread* java_thread = nullptr; 864 oop thread_oop = nullptr; 865 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_oop); 866 if (err != JVMTI_ERROR_NONE && err != JVMTI_ERROR_THREAD_NOT_ALIVE) { 867 // We got an error code so we don't have a JavaThread*, but only 868 // return an error from here if the error is not because the thread 869 // is a virtual thread. 870 return err; 871 } 872 *thread_state_ptr = JvmtiEnvBase::get_thread_or_vthread_state(thread_oop, java_thread); 873 return JVMTI_ERROR_NONE; 874 } /* end GetThreadState */ 875 876 877 // thread_ptr - pre-checked for null 878 jvmtiError 879 JvmtiEnv::GetCurrentThread(jthread* thread_ptr) { 880 JavaThread* cur_thread = JavaThread::current(); 881 oop thread_oop = get_vthread_or_thread_oop(cur_thread); 882 883 *thread_ptr = (jthread)JNIHandles::make_local(cur_thread, thread_oop); 884 return JVMTI_ERROR_NONE; 885 } /* end GetCurrentThread */ 886 887 888 // threads_count_ptr - pre-checked for null 889 // threads_ptr - pre-checked for null 890 jvmtiError 891 JvmtiEnv::GetAllThreads(jint* threads_count_ptr, jthread** threads_ptr) { 892 int nthreads = 0; 893 Handle *thread_objs = nullptr; 894 Thread* current_thread = Thread::current(); 895 ResourceMark rm(current_thread); 896 HandleMark hm(current_thread); 897 898 // enumerate threads (including agent threads) 899 ThreadsListEnumerator tle(current_thread, true); 900 nthreads = tle.num_threads(); 901 *threads_count_ptr = nthreads; 902 903 if (nthreads == 0) { 904 *threads_ptr = nullptr; 905 return JVMTI_ERROR_NONE; 906 } 907 908 thread_objs = NEW_RESOURCE_ARRAY(Handle, nthreads); 909 NULL_CHECK(thread_objs, JVMTI_ERROR_OUT_OF_MEMORY); 910 911 for (int i = 0; i < nthreads; i++) { 912 thread_objs[i] = Handle(tle.get_threadObj(i)); 913 } 914 915 jthread *jthreads = new_jthreadArray(nthreads, thread_objs); 916 NULL_CHECK(jthreads, JVMTI_ERROR_OUT_OF_MEMORY); 917 918 *threads_ptr = jthreads; 919 return JVMTI_ERROR_NONE; 920 } /* end GetAllThreads */ 921 922 923 // thread - NOT protected by ThreadsListHandle and NOT pre-checked 924 jvmtiError 925 JvmtiEnv::SuspendThread(jthread thread) { 926 JavaThread* current = JavaThread::current(); 927 HandleMark hm(current); 928 Handle self_tobj; 929 930 jvmtiError err; 931 { 932 JvmtiVTMSTransitionDisabler disabler(true); 933 ThreadsListHandle tlh(current); 934 JavaThread* java_thread = nullptr; 935 oop thread_oop = nullptr; 936 937 err = get_threadOop_and_JavaThread(tlh.list(), thread, current, &java_thread, &thread_oop); 938 if (err != JVMTI_ERROR_NONE) { 939 return err; 940 } 941 942 // Do not use JvmtiVTMSTransitionDisabler in context of self suspend to avoid deadlocks. 943 if (java_thread != current) { 944 err = suspend_thread(thread_oop, java_thread, /* single_suspend */ true, nullptr); 945 return err; 946 } 947 // protect thread_oop as a safepoint can be reached in disabler destructor 948 self_tobj = Handle(current, thread_oop); 949 } 950 // Do self suspend for current JavaThread. 951 err = suspend_thread(self_tobj(), current, /* single_suspend */ true, nullptr); 952 return err; 953 } /* end SuspendThread */ 954 955 956 // request_count - pre-checked to be greater than or equal to 0 957 // request_list - pre-checked for null 958 // results - pre-checked for null 959 jvmtiError 960 JvmtiEnv::SuspendThreadList(jint request_count, const jthread* request_list, jvmtiError* results) { 961 JavaThread* current = JavaThread::current(); 962 HandleMark hm(current); 963 Handle self_tobj; 964 int self_idx = -1; 965 966 { 967 JvmtiVTMSTransitionDisabler disabler(true); 968 ThreadsListHandle tlh(current); 969 970 for (int i = 0; i < request_count; i++) { 971 JavaThread *java_thread = nullptr; 972 oop thread_oop = nullptr; 973 jthread thread = request_list[i]; 974 jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop); 975 976 if (thread_oop != nullptr && 977 java_lang_VirtualThread::is_instance(thread_oop) && 978 !JvmtiEnvBase::is_vthread_alive(thread_oop)) { 979 err = JVMTI_ERROR_THREAD_NOT_ALIVE; 980 } 981 if (err != JVMTI_ERROR_NONE) { 982 if (thread_oop == nullptr || err != JVMTI_ERROR_INVALID_THREAD) { 983 results[i] = err; 984 continue; 985 } 986 } 987 if (java_thread == current) { 988 self_idx = i; 989 self_tobj = Handle(current, thread_oop); 990 continue; // self suspend after all other suspends 991 } 992 results[i] = suspend_thread(thread_oop, java_thread, /* single_suspend */ true, nullptr); 993 } 994 } 995 // Self suspend after all other suspends if necessary. 996 // Do not use JvmtiVTMSTransitionDisabler in context of self suspend to avoid deadlocks. 997 if (self_tobj() != nullptr) { 998 // there should not be any error for current java_thread 999 results[self_idx] = suspend_thread(self_tobj(), current, /* single_suspend */ true, nullptr); 1000 } 1001 // per-thread suspend results returned via results parameter 1002 return JVMTI_ERROR_NONE; 1003 } /* end SuspendThreadList */ 1004 1005 1006 jvmtiError 1007 JvmtiEnv::SuspendAllVirtualThreads(jint except_count, const jthread* except_list) { 1008 if (get_capabilities()->can_support_virtual_threads == 0) { 1009 return JVMTI_ERROR_MUST_POSSESS_CAPABILITY; 1010 } 1011 JavaThread* current = JavaThread::current(); 1012 HandleMark hm(current); 1013 Handle self_tobj; 1014 1015 { 1016 ResourceMark rm(current); 1017 JvmtiVTMSTransitionDisabler disabler(true); 1018 ThreadsListHandle tlh(current); 1019 GrowableArray<jthread>* elist = new GrowableArray<jthread>(except_count); 1020 1021 jvmtiError err = JvmtiEnvBase::check_thread_list(except_count, except_list); 1022 if (err != JVMTI_ERROR_NONE) { 1023 return err; 1024 } 1025 1026 // Collect threads from except_list for which resumed status must be restored (only for VirtualThread case) 1027 for (int idx = 0; idx < except_count; idx++) { 1028 jthread thread = except_list[idx]; 1029 oop thread_oop = JNIHandles::resolve_external_guard(thread); 1030 if (java_lang_VirtualThread::is_instance(thread_oop) && !JvmtiVTSuspender::is_vthread_suspended(thread_oop)) { 1031 // is not suspended, so its resumed status must be restored 1032 elist->append(except_list[idx]); 1033 } 1034 } 1035 1036 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *java_thread = jtiwh.next(); ) { 1037 oop vt_oop = java_thread->jvmti_vthread(); 1038 if (!java_thread->is_exiting() && 1039 !java_thread->is_jvmti_agent_thread() && 1040 !java_thread->is_hidden_from_external_view() && 1041 vt_oop != nullptr && 1042 ((java_lang_VirtualThread::is_instance(vt_oop) && 1043 JvmtiEnvBase::is_vthread_alive(vt_oop) && 1044 !JvmtiVTSuspender::is_vthread_suspended(vt_oop)) || 1045 (vt_oop->is_a(vmClasses::BoundVirtualThread_klass()) && !java_thread->is_suspended())) && 1046 !is_in_thread_list(except_count, except_list, vt_oop) 1047 ) { 1048 if (java_thread == current) { 1049 self_tobj = Handle(current, vt_oop); 1050 continue; // self suspend after all other suspends 1051 } 1052 suspend_thread(vt_oop, java_thread, /* single_suspend */ false, nullptr); 1053 } 1054 } 1055 JvmtiVTSuspender::register_all_vthreads_suspend(); 1056 1057 // Restore resumed state for threads from except list that were not suspended before. 1058 for (int idx = 0; idx < elist->length(); idx++) { 1059 jthread thread = elist->at(idx); 1060 oop thread_oop = JNIHandles::resolve_external_guard(thread); 1061 if (JvmtiVTSuspender::is_vthread_suspended(thread_oop)) { 1062 JvmtiVTSuspender::register_vthread_resume(thread_oop); 1063 } 1064 } 1065 } 1066 // Self suspend after all other suspends if necessary. 1067 // Do not use JvmtiVTMSTransitionDisabler in context of self suspend to avoid deadlocks. 1068 if (self_tobj() != nullptr) { 1069 suspend_thread(self_tobj(), current, /* single_suspend */ false, nullptr); 1070 } 1071 return JVMTI_ERROR_NONE; 1072 } /* end SuspendAllVirtualThreads */ 1073 1074 1075 // thread - NOT protected by ThreadsListHandle and NOT pre-checked 1076 jvmtiError 1077 JvmtiEnv::ResumeThread(jthread thread) { 1078 JvmtiVTMSTransitionDisabler disabler(true); 1079 JavaThread* current = JavaThread::current(); 1080 ThreadsListHandle tlh(current); 1081 1082 JavaThread* java_thread = nullptr; 1083 oop thread_oop = nullptr; 1084 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current, &java_thread, &thread_oop); 1085 if (err != JVMTI_ERROR_NONE) { 1086 return err; 1087 } 1088 err = resume_thread(thread_oop, java_thread, /* single_resume */ true); 1089 return err; 1090 } /* end ResumeThread */ 1091 1092 1093 // request_count - pre-checked to be greater than or equal to 0 1094 // request_list - pre-checked for null 1095 // results - pre-checked for null 1096 jvmtiError 1097 JvmtiEnv::ResumeThreadList(jint request_count, const jthread* request_list, jvmtiError* results) { 1098 oop thread_oop = nullptr; 1099 JavaThread* java_thread = nullptr; 1100 JvmtiVTMSTransitionDisabler disabler(true); 1101 ThreadsListHandle tlh; 1102 1103 for (int i = 0; i < request_count; i++) { 1104 jthread thread = request_list[i]; 1105 jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop); 1106 1107 if (thread_oop != nullptr && 1108 java_lang_VirtualThread::is_instance(thread_oop) && 1109 !JvmtiEnvBase::is_vthread_alive(thread_oop)) { 1110 err = JVMTI_ERROR_THREAD_NOT_ALIVE; 1111 } 1112 if (err != JVMTI_ERROR_NONE) { 1113 if (thread_oop == nullptr || err != JVMTI_ERROR_INVALID_THREAD) { 1114 results[i] = err; 1115 continue; 1116 } 1117 } 1118 results[i] = resume_thread(thread_oop, java_thread, /* single_resume */ true); 1119 } 1120 // per-thread resume results returned via results parameter 1121 return JVMTI_ERROR_NONE; 1122 } /* end ResumeThreadList */ 1123 1124 1125 jvmtiError 1126 JvmtiEnv::ResumeAllVirtualThreads(jint except_count, const jthread* except_list) { 1127 if (get_capabilities()->can_support_virtual_threads == 0) { 1128 return JVMTI_ERROR_MUST_POSSESS_CAPABILITY; 1129 } 1130 jvmtiError err = JvmtiEnvBase::check_thread_list(except_count, except_list); 1131 if (err != JVMTI_ERROR_NONE) { 1132 return err; 1133 } 1134 ResourceMark rm; 1135 JvmtiVTMSTransitionDisabler disabler(true); 1136 GrowableArray<jthread>* elist = new GrowableArray<jthread>(except_count); 1137 1138 // Collect threads from except_list for which suspended status must be restored (only for VirtualThread case) 1139 for (int idx = 0; idx < except_count; idx++) { 1140 jthread thread = except_list[idx]; 1141 oop thread_oop = JNIHandles::resolve_external_guard(thread); 1142 if (java_lang_VirtualThread::is_instance(thread_oop) && JvmtiVTSuspender::is_vthread_suspended(thread_oop)) { 1143 // is suspended, so its suspended status must be restored 1144 elist->append(except_list[idx]); 1145 } 1146 } 1147 1148 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *java_thread = jtiwh.next(); ) { 1149 oop vt_oop = java_thread->jvmti_vthread(); 1150 if (!java_thread->is_exiting() && 1151 !java_thread->is_jvmti_agent_thread() && 1152 !java_thread->is_hidden_from_external_view() && 1153 vt_oop != nullptr && 1154 ((java_lang_VirtualThread::is_instance(vt_oop) && 1155 JvmtiEnvBase::is_vthread_alive(vt_oop) && 1156 JvmtiVTSuspender::is_vthread_suspended(vt_oop)) || 1157 (vt_oop->is_a(vmClasses::BoundVirtualThread_klass()) && java_thread->is_suspended())) && 1158 !is_in_thread_list(except_count, except_list, vt_oop) 1159 ) { 1160 resume_thread(vt_oop, java_thread, /* single_resume */ false); 1161 } 1162 } 1163 JvmtiVTSuspender::register_all_vthreads_resume(); 1164 1165 // Restore suspended state for threads from except list that were suspended before. 1166 for (int idx = 0; idx < elist->length(); idx++) { 1167 jthread thread = elist->at(idx); 1168 oop thread_oop = JNIHandles::resolve_external_guard(thread); 1169 if (!JvmtiVTSuspender::is_vthread_suspended(thread_oop)) { 1170 JvmtiVTSuspender::register_vthread_suspend(thread_oop); 1171 } 1172 } 1173 return JVMTI_ERROR_NONE; 1174 } /* end ResumeAllVirtualThreads */ 1175 1176 1177 jvmtiError 1178 JvmtiEnv::StopThread(jthread thread, jobject exception) { 1179 JavaThread* current_thread = JavaThread::current(); 1180 1181 JvmtiVTMSTransitionDisabler disabler(thread); 1182 ThreadsListHandle tlh(current_thread); 1183 JavaThread* java_thread = nullptr; 1184 oop thread_oop = nullptr; 1185 1186 NULL_CHECK(thread, JVMTI_ERROR_INVALID_THREAD); 1187 1188 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_oop); 1189 1190 bool is_virtual = thread_oop != nullptr && thread_oop->is_a(vmClasses::BaseVirtualThread_klass()); 1191 1192 if (is_virtual && !is_JavaThread_current(java_thread, thread_oop)) { 1193 if (!is_vthread_suspended(thread_oop, java_thread)) { 1194 return JVMTI_ERROR_THREAD_NOT_SUSPENDED; 1195 } 1196 if (java_thread == nullptr) { // unmounted virtual thread 1197 return JVMTI_ERROR_OPAQUE_FRAME; 1198 } 1199 } 1200 if (err != JVMTI_ERROR_NONE) { 1201 return err; 1202 } 1203 oop e = JNIHandles::resolve_external_guard(exception); 1204 NULL_CHECK(e, JVMTI_ERROR_NULL_POINTER); 1205 1206 JavaThread::send_async_exception(java_thread, e); 1207 1208 return JVMTI_ERROR_NONE; 1209 1210 } /* end StopThread */ 1211 1212 1213 // thread - NOT protected by ThreadsListHandle and NOT pre-checked 1214 jvmtiError 1215 JvmtiEnv::InterruptThread(jthread thread) { 1216 JavaThread* current_thread = JavaThread::current(); 1217 HandleMark hm(current_thread); 1218 1219 JvmtiVTMSTransitionDisabler disabler(thread); 1220 ThreadsListHandle tlh(current_thread); 1221 1222 JavaThread* java_thread = nullptr; 1223 oop thread_obj = nullptr; 1224 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj); 1225 if (err != JVMTI_ERROR_NONE) { 1226 return err; 1227 } 1228 1229 if (java_lang_VirtualThread::is_instance(thread_obj)) { 1230 // For virtual threads we have to call into Java to interrupt: 1231 Handle obj(current_thread, thread_obj); 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 JavaThread* current_thread = JavaThread::current(); 2722 jint result = 0; 2723 if (!java_lang_Class::is_primitive(k_mirror)) { 2724 Klass* k = java_lang_Class::as_Klass(k_mirror); 2725 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS); 2726 result = k->modifier_flags(); 2727 2728 // Reset the deleted ACC_SUPER bit (deleted in compute_modifier_flags()). 2729 if (k->is_super()) { 2730 result |= JVM_ACC_SUPER; 2731 } 2732 } else { 2733 result = (JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC); 2734 } 2735 *modifiers_ptr = result; 2736 2737 return JVMTI_ERROR_NONE; 2738 } /* end GetClassModifiers */ 2739 2740 2741 // k_mirror - may be primitive, this must be checked 2742 // method_count_ptr - pre-checked for null 2743 // methods_ptr - pre-checked for null 2744 jvmtiError 2745 JvmtiEnv::GetClassMethods(oop k_mirror, jint* method_count_ptr, jmethodID** methods_ptr) { 2746 JavaThread* current_thread = JavaThread::current(); 2747 HandleMark hm(current_thread); 2748 2749 if (java_lang_Class::is_primitive(k_mirror)) { 2750 *method_count_ptr = 0; 2751 *methods_ptr = (jmethodID*) jvmtiMalloc(0 * sizeof(jmethodID)); 2752 return JVMTI_ERROR_NONE; 2753 } 2754 Klass* k = java_lang_Class::as_Klass(k_mirror); 2755 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS); 2756 2757 // Return CLASS_NOT_PREPARED error as per JVMTI spec. 2758 if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) { 2759 return JVMTI_ERROR_CLASS_NOT_PREPARED; 2760 } 2761 2762 if (!k->is_instance_klass()) { 2763 *method_count_ptr = 0; 2764 *methods_ptr = (jmethodID*) jvmtiMalloc(0 * sizeof(jmethodID)); 2765 return JVMTI_ERROR_NONE; 2766 } 2767 InstanceKlass* ik = InstanceKlass::cast(k); 2768 // Allocate the result and fill it in 2769 int result_length = ik->methods()->length(); 2770 jmethodID* result_list = (jmethodID*)jvmtiMalloc(result_length * sizeof(jmethodID)); 2771 int index; 2772 bool jmethodids_found = true; 2773 int skipped = 0; // skip overpass methods 2774 2775 for (index = 0; index < result_length; index++) { 2776 Method* m = ik->methods()->at(index); 2777 // Depending on can_maintain_original_method_order capability use the original 2778 // method ordering indices stored in the class, so we can emit jmethodIDs in 2779 // the order they appeared in the class file or just copy in current order. 2780 int result_index = JvmtiExport::can_maintain_original_method_order() ? ik->method_ordering()->at(index) : index; 2781 assert(result_index >= 0 && result_index < result_length, "invalid original method index"); 2782 if (m->is_overpass()) { 2783 result_list[result_index] = nullptr; 2784 skipped++; 2785 continue; 2786 } 2787 jmethodID id; 2788 if (jmethodids_found) { 2789 id = m->find_jmethod_id_or_null(); 2790 if (id == nullptr) { 2791 // If we find an uninitialized value, make sure there is 2792 // enough space for all the uninitialized values we might 2793 // find. 2794 ik->ensure_space_for_methodids(index); 2795 jmethodids_found = false; 2796 id = m->jmethod_id(); 2797 } 2798 } else { 2799 id = m->jmethod_id(); 2800 } 2801 result_list[result_index] = id; 2802 } 2803 2804 // Fill in return value. 2805 if (skipped > 0) { 2806 // copy results skipping null methodIDs 2807 *methods_ptr = (jmethodID*)jvmtiMalloc((result_length - skipped) * sizeof(jmethodID)); 2808 *method_count_ptr = result_length - skipped; 2809 for (index = 0, skipped = 0; index < result_length; index++) { 2810 if (result_list[index] == nullptr) { 2811 skipped++; 2812 } else { 2813 (*methods_ptr)[index - skipped] = result_list[index]; 2814 } 2815 } 2816 deallocate((unsigned char *)result_list); 2817 } else { 2818 *method_count_ptr = result_length; 2819 *methods_ptr = result_list; 2820 } 2821 2822 return JVMTI_ERROR_NONE; 2823 } /* end GetClassMethods */ 2824 2825 2826 // k_mirror - may be primitive, this must be checked 2827 // field_count_ptr - pre-checked for null 2828 // fields_ptr - pre-checked for null 2829 jvmtiError 2830 JvmtiEnv::GetClassFields(oop k_mirror, jint* field_count_ptr, jfieldID** fields_ptr) { 2831 if (java_lang_Class::is_primitive(k_mirror)) { 2832 *field_count_ptr = 0; 2833 *fields_ptr = (jfieldID*) jvmtiMalloc(0 * sizeof(jfieldID)); 2834 return JVMTI_ERROR_NONE; 2835 } 2836 JavaThread* current_thread = JavaThread::current(); 2837 HandleMark hm(current_thread); 2838 Klass* k = java_lang_Class::as_Klass(k_mirror); 2839 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS); 2840 2841 // Return CLASS_NOT_PREPARED error as per JVMTI spec. 2842 if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) { 2843 return JVMTI_ERROR_CLASS_NOT_PREPARED; 2844 } 2845 2846 if (!k->is_instance_klass()) { 2847 *field_count_ptr = 0; 2848 *fields_ptr = (jfieldID*) jvmtiMalloc(0 * sizeof(jfieldID)); 2849 return JVMTI_ERROR_NONE; 2850 } 2851 2852 InstanceKlass* ik = InstanceKlass::cast(k); 2853 2854 FilteredJavaFieldStream flds(ik); 2855 2856 int result_count = flds.field_count(); 2857 2858 // Allocate the result and fill it in. 2859 jfieldID* result_list = (jfieldID*)jvmtiMalloc(result_count * sizeof(jfieldID)); 2860 for (int i = 0; i < result_count; i++, flds.next()) { 2861 result_list[i] = jfieldIDWorkaround::to_jfieldID(ik, flds.offset(), 2862 flds.access_flags().is_static()); 2863 } 2864 assert(flds.done(), "just checking"); 2865 2866 // Fill in the results 2867 *field_count_ptr = result_count; 2868 *fields_ptr = result_list; 2869 2870 return JVMTI_ERROR_NONE; 2871 } /* end GetClassFields */ 2872 2873 2874 // k_mirror - may be primitive, this must be checked 2875 // interface_count_ptr - pre-checked for null 2876 // interfaces_ptr - pre-checked for null 2877 jvmtiError 2878 JvmtiEnv::GetImplementedInterfaces(oop k_mirror, jint* interface_count_ptr, jclass** interfaces_ptr) { 2879 { 2880 if (java_lang_Class::is_primitive(k_mirror)) { 2881 *interface_count_ptr = 0; 2882 *interfaces_ptr = (jclass*) jvmtiMalloc(0 * sizeof(jclass)); 2883 return JVMTI_ERROR_NONE; 2884 } 2885 JavaThread* current_thread = JavaThread::current(); 2886 HandleMark hm(current_thread); 2887 Klass* k = java_lang_Class::as_Klass(k_mirror); 2888 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS); 2889 2890 // Return CLASS_NOT_PREPARED error as per JVMTI spec. 2891 if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) 2892 return JVMTI_ERROR_CLASS_NOT_PREPARED; 2893 2894 if (!k->is_instance_klass()) { 2895 *interface_count_ptr = 0; 2896 *interfaces_ptr = (jclass*) jvmtiMalloc(0 * sizeof(jclass)); 2897 return JVMTI_ERROR_NONE; 2898 } 2899 2900 Array<InstanceKlass*>* interface_list = InstanceKlass::cast(k)->local_interfaces(); 2901 const int result_length = (interface_list == nullptr ? 0 : interface_list->length()); 2902 jclass* result_list = (jclass*) jvmtiMalloc(result_length * sizeof(jclass)); 2903 for (int i_index = 0; i_index < result_length; i_index += 1) { 2904 InstanceKlass* klass_at = interface_list->at(i_index); 2905 assert(klass_at->is_klass(), "interfaces must be Klass*s"); 2906 assert(klass_at->is_interface(), "interfaces must be interfaces"); 2907 oop mirror_at = klass_at->java_mirror(); 2908 Handle handle_at = Handle(current_thread, mirror_at); 2909 result_list[i_index] = (jclass) jni_reference(handle_at); 2910 } 2911 *interface_count_ptr = result_length; 2912 *interfaces_ptr = result_list; 2913 } 2914 2915 return JVMTI_ERROR_NONE; 2916 } /* end GetImplementedInterfaces */ 2917 2918 2919 // k_mirror - may be primitive, this must be checked 2920 // minor_version_ptr - pre-checked for null 2921 // major_version_ptr - pre-checked for null 2922 jvmtiError 2923 JvmtiEnv::GetClassVersionNumbers(oop k_mirror, jint* minor_version_ptr, jint* major_version_ptr) { 2924 if (java_lang_Class::is_primitive(k_mirror)) { 2925 return JVMTI_ERROR_ABSENT_INFORMATION; 2926 } 2927 Klass* klass = java_lang_Class::as_Klass(k_mirror); 2928 2929 jint status = klass->jvmti_class_status(); 2930 if (status & (JVMTI_CLASS_STATUS_ERROR)) { 2931 return JVMTI_ERROR_INVALID_CLASS; 2932 } 2933 if (status & (JVMTI_CLASS_STATUS_ARRAY)) { 2934 return JVMTI_ERROR_ABSENT_INFORMATION; 2935 } 2936 2937 InstanceKlass* ik = InstanceKlass::cast(klass); 2938 *minor_version_ptr = ik->minor_version(); 2939 *major_version_ptr = ik->major_version(); 2940 2941 return JVMTI_ERROR_NONE; 2942 } /* end GetClassVersionNumbers */ 2943 2944 2945 // k_mirror - may be primitive, this must be checked 2946 // constant_pool_count_ptr - pre-checked for null 2947 // constant_pool_byte_count_ptr - pre-checked for null 2948 // constant_pool_bytes_ptr - pre-checked for null 2949 jvmtiError 2950 JvmtiEnv::GetConstantPool(oop k_mirror, jint* constant_pool_count_ptr, jint* constant_pool_byte_count_ptr, unsigned char** constant_pool_bytes_ptr) { 2951 if (java_lang_Class::is_primitive(k_mirror)) { 2952 return JVMTI_ERROR_ABSENT_INFORMATION; 2953 } 2954 2955 Klass* klass = java_lang_Class::as_Klass(k_mirror); 2956 Thread *thread = Thread::current(); 2957 ResourceMark rm(thread); 2958 2959 jint status = klass->jvmti_class_status(); 2960 if (status & (JVMTI_CLASS_STATUS_ERROR)) { 2961 return JVMTI_ERROR_INVALID_CLASS; 2962 } 2963 if (status & (JVMTI_CLASS_STATUS_ARRAY)) { 2964 return JVMTI_ERROR_ABSENT_INFORMATION; 2965 } 2966 2967 InstanceKlass* ik = InstanceKlass::cast(klass); 2968 JvmtiConstantPoolReconstituter reconstituter(ik); 2969 if (reconstituter.get_error() != JVMTI_ERROR_NONE) { 2970 return reconstituter.get_error(); 2971 } 2972 2973 unsigned char *cpool_bytes; 2974 int cpool_size = reconstituter.cpool_size(); 2975 if (reconstituter.get_error() != JVMTI_ERROR_NONE) { 2976 return reconstituter.get_error(); 2977 } 2978 jvmtiError res = allocate(cpool_size, &cpool_bytes); 2979 if (res != JVMTI_ERROR_NONE) { 2980 return res; 2981 } 2982 reconstituter.copy_cpool_bytes(cpool_bytes); 2983 if (reconstituter.get_error() != JVMTI_ERROR_NONE) { 2984 return reconstituter.get_error(); 2985 } 2986 2987 constantPoolHandle constants(thread, ik->constants()); 2988 *constant_pool_count_ptr = constants->length(); 2989 *constant_pool_byte_count_ptr = cpool_size; 2990 *constant_pool_bytes_ptr = cpool_bytes; 2991 2992 return JVMTI_ERROR_NONE; 2993 } /* end GetConstantPool */ 2994 2995 2996 // k_mirror - may be primitive, this must be checked 2997 // is_interface_ptr - pre-checked for null 2998 jvmtiError 2999 JvmtiEnv::IsInterface(oop k_mirror, jboolean* is_interface_ptr) { 3000 { 3001 bool result = false; 3002 if (!java_lang_Class::is_primitive(k_mirror)) { 3003 Klass* k = java_lang_Class::as_Klass(k_mirror); 3004 if (k != nullptr && k->is_interface()) { 3005 result = true; 3006 } 3007 } 3008 *is_interface_ptr = result; 3009 } 3010 3011 return JVMTI_ERROR_NONE; 3012 } /* end IsInterface */ 3013 3014 3015 // k_mirror - may be primitive, this must be checked 3016 // is_array_class_ptr - pre-checked for null 3017 jvmtiError 3018 JvmtiEnv::IsArrayClass(oop k_mirror, jboolean* is_array_class_ptr) { 3019 { 3020 bool result = false; 3021 if (!java_lang_Class::is_primitive(k_mirror)) { 3022 Klass* k = java_lang_Class::as_Klass(k_mirror); 3023 if (k != nullptr && k->is_array_klass()) { 3024 result = true; 3025 } 3026 } 3027 *is_array_class_ptr = result; 3028 } 3029 3030 return JVMTI_ERROR_NONE; 3031 } /* end IsArrayClass */ 3032 3033 3034 // k_mirror - may be primitive, this must be checked 3035 // classloader_ptr - pre-checked for null 3036 jvmtiError 3037 JvmtiEnv::GetClassLoader(oop k_mirror, jobject* classloader_ptr) { 3038 { 3039 if (java_lang_Class::is_primitive(k_mirror)) { 3040 *classloader_ptr = (jclass) jni_reference(Handle()); 3041 return JVMTI_ERROR_NONE; 3042 } 3043 JavaThread* current_thread = JavaThread::current(); 3044 HandleMark hm(current_thread); 3045 Klass* k = java_lang_Class::as_Klass(k_mirror); 3046 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS); 3047 3048 oop result_oop = k->class_loader(); 3049 if (result_oop == nullptr) { 3050 *classloader_ptr = (jclass) jni_reference(Handle()); 3051 return JVMTI_ERROR_NONE; 3052 } 3053 Handle result_handle = Handle(current_thread, result_oop); 3054 jclass result_jnihandle = (jclass) jni_reference(result_handle); 3055 *classloader_ptr = result_jnihandle; 3056 } 3057 return JVMTI_ERROR_NONE; 3058 } /* end GetClassLoader */ 3059 3060 3061 // k_mirror - may be primitive, this must be checked 3062 // source_debug_extension_ptr - pre-checked for null 3063 jvmtiError 3064 JvmtiEnv::GetSourceDebugExtension(oop k_mirror, char** source_debug_extension_ptr) { 3065 { 3066 if (java_lang_Class::is_primitive(k_mirror)) { 3067 return JVMTI_ERROR_ABSENT_INFORMATION; 3068 } 3069 Klass* k = java_lang_Class::as_Klass(k_mirror); 3070 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS); 3071 if (!k->is_instance_klass()) { 3072 return JVMTI_ERROR_ABSENT_INFORMATION; 3073 } 3074 const char* sde = InstanceKlass::cast(k)->source_debug_extension(); 3075 NULL_CHECK(sde, JVMTI_ERROR_ABSENT_INFORMATION); 3076 3077 { 3078 *source_debug_extension_ptr = (char *) jvmtiMalloc(strlen(sde)+1); 3079 strcpy(*source_debug_extension_ptr, sde); 3080 } 3081 } 3082 3083 return JVMTI_ERROR_NONE; 3084 } /* end GetSourceDebugExtension */ 3085 3086 // 3087 // Object functions 3088 // 3089 3090 // hash_code_ptr - pre-checked for null 3091 jvmtiError 3092 JvmtiEnv::GetObjectHashCode(jobject object, jint* hash_code_ptr) { 3093 oop mirror = JNIHandles::resolve_external_guard(object); 3094 NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT); 3095 NULL_CHECK(hash_code_ptr, JVMTI_ERROR_NULL_POINTER); 3096 3097 { 3098 jint result = (jint) mirror->identity_hash(); 3099 *hash_code_ptr = result; 3100 } 3101 return JVMTI_ERROR_NONE; 3102 } /* end GetObjectHashCode */ 3103 3104 3105 // info_ptr - pre-checked for null 3106 jvmtiError 3107 JvmtiEnv::GetObjectMonitorUsage(jobject object, jvmtiMonitorUsage* info_ptr) { 3108 // This needs to be performed at a safepoint to gather stable data 3109 // because monitor owner / waiters might not be suspended. 3110 VM_GetObjectMonitorUsage op(this, JavaThread::current(), object, info_ptr); 3111 VMThread::execute(&op); 3112 return op.result(); 3113 } /* end GetObjectMonitorUsage */ 3114 3115 3116 // 3117 // Field functions 3118 // 3119 3120 // name_ptr - null is a valid value, must be checked 3121 // signature_ptr - null is a valid value, must be checked 3122 // generic_ptr - null is a valid value, must be checked 3123 jvmtiError 3124 JvmtiEnv::GetFieldName(fieldDescriptor* fdesc_ptr, char** name_ptr, char** signature_ptr, char** generic_ptr) { 3125 JavaThread* current_thread = JavaThread::current(); 3126 ResourceMark rm(current_thread); 3127 if (name_ptr == nullptr) { 3128 // just don't return the name 3129 } else { 3130 const char* fieldName = fdesc_ptr->name()->as_C_string(); 3131 *name_ptr = (char*) jvmtiMalloc(strlen(fieldName) + 1); 3132 if (*name_ptr == nullptr) 3133 return JVMTI_ERROR_OUT_OF_MEMORY; 3134 strcpy(*name_ptr, fieldName); 3135 } 3136 if (signature_ptr== nullptr) { 3137 // just don't return the signature 3138 } else { 3139 const char* fieldSignature = fdesc_ptr->signature()->as_C_string(); 3140 *signature_ptr = (char*) jvmtiMalloc(strlen(fieldSignature) + 1); 3141 if (*signature_ptr == nullptr) 3142 return JVMTI_ERROR_OUT_OF_MEMORY; 3143 strcpy(*signature_ptr, fieldSignature); 3144 } 3145 if (generic_ptr != nullptr) { 3146 *generic_ptr = nullptr; 3147 Symbol* soop = fdesc_ptr->generic_signature(); 3148 if (soop != nullptr) { 3149 const char* gen_sig = soop->as_C_string(); 3150 if (gen_sig != nullptr) { 3151 jvmtiError err = allocate(strlen(gen_sig) + 1, (unsigned char **)generic_ptr); 3152 if (err != JVMTI_ERROR_NONE) { 3153 return err; 3154 } 3155 strcpy(*generic_ptr, gen_sig); 3156 } 3157 } 3158 } 3159 return JVMTI_ERROR_NONE; 3160 } /* end GetFieldName */ 3161 3162 3163 // declaring_class_ptr - pre-checked for null 3164 jvmtiError 3165 JvmtiEnv::GetFieldDeclaringClass(fieldDescriptor* fdesc_ptr, jclass* declaring_class_ptr) { 3166 // As for the GetFieldDeclaringClass method, the XSL generated C++ code that calls it has 3167 // a jclass of the relevant class or a subclass of it, which is fine in terms of ensuring 3168 // the holder is kept alive. 3169 *declaring_class_ptr = get_jni_class_non_null(fdesc_ptr->field_holder()); 3170 return JVMTI_ERROR_NONE; 3171 } /* end GetFieldDeclaringClass */ 3172 3173 3174 // modifiers_ptr - pre-checked for null 3175 jvmtiError 3176 JvmtiEnv::GetFieldModifiers(fieldDescriptor* fdesc_ptr, jint* modifiers_ptr) { 3177 3178 AccessFlags resultFlags = fdesc_ptr->access_flags(); 3179 jint result = resultFlags.as_field_flags(); 3180 *modifiers_ptr = result; 3181 3182 return JVMTI_ERROR_NONE; 3183 } /* end GetFieldModifiers */ 3184 3185 3186 // is_synthetic_ptr - pre-checked for null 3187 jvmtiError 3188 JvmtiEnv::IsFieldSynthetic(fieldDescriptor* fdesc_ptr, jboolean* is_synthetic_ptr) { 3189 *is_synthetic_ptr = fdesc_ptr->is_synthetic(); 3190 return JVMTI_ERROR_NONE; 3191 } /* end IsFieldSynthetic */ 3192 3193 3194 // 3195 // Method functions 3196 // 3197 3198 // method - pre-checked for validity, but may be null meaning obsolete method 3199 // name_ptr - null is a valid value, must be checked 3200 // signature_ptr - null is a valid value, must be checked 3201 // generic_ptr - null is a valid value, must be checked 3202 jvmtiError 3203 JvmtiEnv::GetMethodName(Method* method, char** name_ptr, char** signature_ptr, char** generic_ptr) { 3204 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID); 3205 JavaThread* current_thread = JavaThread::current(); 3206 3207 ResourceMark rm(current_thread); // get the utf8 name and signature 3208 if (name_ptr == nullptr) { 3209 // just don't return the name 3210 } else { 3211 const char* utf8_name = (const char *) method->name()->as_utf8(); 3212 *name_ptr = (char *) jvmtiMalloc(strlen(utf8_name)+1); 3213 strcpy(*name_ptr, utf8_name); 3214 } 3215 if (signature_ptr == nullptr) { 3216 // just don't return the signature 3217 } else { 3218 const char* utf8_signature = (const char *) method->signature()->as_utf8(); 3219 *signature_ptr = (char *) jvmtiMalloc(strlen(utf8_signature) + 1); 3220 strcpy(*signature_ptr, utf8_signature); 3221 } 3222 3223 if (generic_ptr != nullptr) { 3224 *generic_ptr = nullptr; 3225 Symbol* soop = method->generic_signature(); 3226 if (soop != nullptr) { 3227 const char* gen_sig = soop->as_C_string(); 3228 if (gen_sig != nullptr) { 3229 jvmtiError err = allocate(strlen(gen_sig) + 1, (unsigned char **)generic_ptr); 3230 if (err != JVMTI_ERROR_NONE) { 3231 return err; 3232 } 3233 strcpy(*generic_ptr, gen_sig); 3234 } 3235 } 3236 } 3237 return JVMTI_ERROR_NONE; 3238 } /* end GetMethodName */ 3239 3240 3241 // method - pre-checked for validity, but may be null meaning obsolete method 3242 // declaring_class_ptr - pre-checked for null 3243 jvmtiError 3244 JvmtiEnv::GetMethodDeclaringClass(Method* method, jclass* declaring_class_ptr) { 3245 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID); 3246 Klass* k = method->method_holder(); 3247 Handle holder(Thread::current(), k->klass_holder()); // keep the klass alive 3248 (*declaring_class_ptr) = get_jni_class_non_null(k); 3249 return JVMTI_ERROR_NONE; 3250 } /* end GetMethodDeclaringClass */ 3251 3252 3253 // method - pre-checked for validity, but may be null meaning obsolete method 3254 // modifiers_ptr - pre-checked for null 3255 jvmtiError 3256 JvmtiEnv::GetMethodModifiers(Method* method, jint* modifiers_ptr) { 3257 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID); 3258 (*modifiers_ptr) = method->access_flags().as_method_flags(); 3259 return JVMTI_ERROR_NONE; 3260 } /* end GetMethodModifiers */ 3261 3262 3263 // method - pre-checked for validity, but may be null meaning obsolete method 3264 // max_ptr - pre-checked for null 3265 jvmtiError 3266 JvmtiEnv::GetMaxLocals(Method* method, jint* max_ptr) { 3267 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID); 3268 // get max stack 3269 (*max_ptr) = method->max_locals(); 3270 return JVMTI_ERROR_NONE; 3271 } /* end GetMaxLocals */ 3272 3273 3274 // method - pre-checked for validity, but may be null meaning obsolete method 3275 // size_ptr - pre-checked for null 3276 jvmtiError 3277 JvmtiEnv::GetArgumentsSize(Method* method, jint* size_ptr) { 3278 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID); 3279 // get size of arguments 3280 3281 (*size_ptr) = method->size_of_parameters(); 3282 return JVMTI_ERROR_NONE; 3283 } /* end GetArgumentsSize */ 3284 3285 3286 // method - pre-checked for validity, but may be null meaning obsolete method 3287 // entry_count_ptr - pre-checked for null 3288 // table_ptr - pre-checked for null 3289 jvmtiError 3290 JvmtiEnv::GetLineNumberTable(Method* method, jint* entry_count_ptr, jvmtiLineNumberEntry** table_ptr) { 3291 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID); 3292 if (!method->has_linenumber_table()) { 3293 return (JVMTI_ERROR_ABSENT_INFORMATION); 3294 } 3295 3296 // The line number table is compressed so we don't know how big it is until decompressed. 3297 // Decompression is really fast so we just do it twice. 3298 3299 // Compute size of table 3300 jint num_entries = 0; 3301 CompressedLineNumberReadStream stream(method->compressed_linenumber_table()); 3302 while (stream.read_pair()) { 3303 num_entries++; 3304 } 3305 jvmtiLineNumberEntry *jvmti_table = 3306 (jvmtiLineNumberEntry *)jvmtiMalloc(num_entries * (sizeof(jvmtiLineNumberEntry))); 3307 3308 // Fill jvmti table 3309 if (num_entries > 0) { 3310 int index = 0; 3311 CompressedLineNumberReadStream stream(method->compressed_linenumber_table()); 3312 while (stream.read_pair()) { 3313 jvmti_table[index].start_location = (jlocation) stream.bci(); 3314 jvmti_table[index].line_number = (jint) stream.line(); 3315 index++; 3316 } 3317 assert(index == num_entries, "sanity check"); 3318 } 3319 3320 // Set up results 3321 (*entry_count_ptr) = num_entries; 3322 (*table_ptr) = jvmti_table; 3323 3324 return JVMTI_ERROR_NONE; 3325 } /* end GetLineNumberTable */ 3326 3327 3328 // method - pre-checked for validity, but may be null meaning obsolete method 3329 // start_location_ptr - pre-checked for null 3330 // end_location_ptr - pre-checked for null 3331 jvmtiError 3332 JvmtiEnv::GetMethodLocation(Method* method, jlocation* start_location_ptr, jlocation* end_location_ptr) { 3333 3334 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID); 3335 // get start and end location 3336 (*end_location_ptr) = (jlocation) (method->code_size() - 1); 3337 if (method->code_size() == 0) { 3338 // there is no code so there is no start location 3339 (*start_location_ptr) = (jlocation)(-1); 3340 } else { 3341 (*start_location_ptr) = (jlocation)(0); 3342 } 3343 3344 return JVMTI_ERROR_NONE; 3345 } /* end GetMethodLocation */ 3346 3347 3348 // method - pre-checked for validity, but may be null meaning obsolete method 3349 // entry_count_ptr - pre-checked for null 3350 // table_ptr - pre-checked for null 3351 jvmtiError 3352 JvmtiEnv::GetLocalVariableTable(Method* method, jint* entry_count_ptr, jvmtiLocalVariableEntry** table_ptr) { 3353 3354 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID); 3355 JavaThread* current_thread = JavaThread::current(); 3356 3357 // does the klass have any local variable information? 3358 InstanceKlass* ik = method->method_holder(); 3359 if (!ik->has_localvariable_table()) { 3360 return (JVMTI_ERROR_ABSENT_INFORMATION); 3361 } 3362 3363 ConstantPool* constants = method->constants(); 3364 NULL_CHECK(constants, JVMTI_ERROR_ABSENT_INFORMATION); 3365 3366 // in the vm localvariable table representation, 6 consecutive elements in the table 3367 // represent a 6-tuple of shorts 3368 // [start_pc, length, name_index, descriptor_index, signature_index, index] 3369 jint num_entries = method->localvariable_table_length(); 3370 jvmtiLocalVariableEntry *jvmti_table = (jvmtiLocalVariableEntry *) 3371 jvmtiMalloc(num_entries * (sizeof(jvmtiLocalVariableEntry))); 3372 3373 if (num_entries > 0) { 3374 LocalVariableTableElement* table = method->localvariable_table_start(); 3375 for (int i = 0; i < num_entries; i++) { 3376 // get the 5 tuple information from the vm table 3377 jlocation start_location = (jlocation) table[i].start_bci; 3378 jint length = (jint) table[i].length; 3379 int name_index = (int) table[i].name_cp_index; 3380 int signature_index = (int) table[i].descriptor_cp_index; 3381 int generic_signature_index = (int) table[i].signature_cp_index; 3382 jint slot = (jint) table[i].slot; 3383 3384 // get utf8 name and signature 3385 char *name_buf = nullptr; 3386 char *sig_buf = nullptr; 3387 char *gen_sig_buf = nullptr; 3388 { 3389 ResourceMark rm(current_thread); 3390 3391 const char *utf8_name = (const char *) constants->symbol_at(name_index)->as_utf8(); 3392 name_buf = (char *) jvmtiMalloc(strlen(utf8_name)+1); 3393 strcpy(name_buf, utf8_name); 3394 3395 const char *utf8_signature = (const char *) constants->symbol_at(signature_index)->as_utf8(); 3396 sig_buf = (char *) jvmtiMalloc(strlen(utf8_signature)+1); 3397 strcpy(sig_buf, utf8_signature); 3398 3399 if (generic_signature_index > 0) { 3400 const char *utf8_gen_sign = (const char *) 3401 constants->symbol_at(generic_signature_index)->as_utf8(); 3402 gen_sig_buf = (char *) jvmtiMalloc(strlen(utf8_gen_sign)+1); 3403 strcpy(gen_sig_buf, utf8_gen_sign); 3404 } 3405 } 3406 3407 // fill in the jvmti local variable table 3408 jvmti_table[i].start_location = start_location; 3409 jvmti_table[i].length = length; 3410 jvmti_table[i].name = name_buf; 3411 jvmti_table[i].signature = sig_buf; 3412 jvmti_table[i].generic_signature = gen_sig_buf; 3413 jvmti_table[i].slot = slot; 3414 } 3415 } 3416 3417 // set results 3418 (*entry_count_ptr) = num_entries; 3419 (*table_ptr) = jvmti_table; 3420 3421 return JVMTI_ERROR_NONE; 3422 } /* end GetLocalVariableTable */ 3423 3424 3425 // method - pre-checked for validity, but may be null meaning obsolete method 3426 // bytecode_count_ptr - pre-checked for null 3427 // bytecodes_ptr - pre-checked for null 3428 jvmtiError 3429 JvmtiEnv::GetBytecodes(Method* method, jint* bytecode_count_ptr, unsigned char** bytecodes_ptr) { 3430 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID); 3431 3432 methodHandle mh(Thread::current(), method); 3433 jint size = (jint)mh->code_size(); 3434 jvmtiError err = allocate(size, bytecodes_ptr); 3435 if (err != JVMTI_ERROR_NONE) { 3436 return err; 3437 } 3438 3439 (*bytecode_count_ptr) = size; 3440 // get byte codes 3441 JvmtiClassFileReconstituter::copy_bytecodes(mh, *bytecodes_ptr); 3442 3443 return JVMTI_ERROR_NONE; 3444 } /* end GetBytecodes */ 3445 3446 3447 // method - pre-checked for validity, but may be null meaning obsolete method 3448 // is_native_ptr - pre-checked for null 3449 jvmtiError 3450 JvmtiEnv::IsMethodNative(Method* method, jboolean* is_native_ptr) { 3451 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID); 3452 (*is_native_ptr) = method->is_native(); 3453 return JVMTI_ERROR_NONE; 3454 } /* end IsMethodNative */ 3455 3456 3457 // method - pre-checked for validity, but may be null meaning obsolete method 3458 // is_synthetic_ptr - pre-checked for null 3459 jvmtiError 3460 JvmtiEnv::IsMethodSynthetic(Method* method, jboolean* is_synthetic_ptr) { 3461 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID); 3462 (*is_synthetic_ptr) = method->is_synthetic(); 3463 return JVMTI_ERROR_NONE; 3464 } /* end IsMethodSynthetic */ 3465 3466 3467 // method - pre-checked for validity, but may be null meaning obsolete method 3468 // is_obsolete_ptr - pre-checked for null 3469 jvmtiError 3470 JvmtiEnv::IsMethodObsolete(Method* method, jboolean* is_obsolete_ptr) { 3471 if (use_version_1_0_semantics() && 3472 get_capabilities()->can_redefine_classes == 0) { 3473 // This JvmtiEnv requested version 1.0 semantics and this function 3474 // requires the can_redefine_classes capability in version 1.0 so 3475 // we need to return an error here. 3476 return JVMTI_ERROR_MUST_POSSESS_CAPABILITY; 3477 } 3478 3479 if (method == nullptr || method->is_obsolete()) { 3480 *is_obsolete_ptr = true; 3481 } else { 3482 *is_obsolete_ptr = false; 3483 } 3484 return JVMTI_ERROR_NONE; 3485 } /* end IsMethodObsolete */ 3486 3487 // 3488 // Raw Monitor functions 3489 // 3490 3491 // name - pre-checked for null 3492 // monitor_ptr - pre-checked for null 3493 jvmtiError 3494 JvmtiEnv::CreateRawMonitor(const char* name, jrawMonitorID* monitor_ptr) { 3495 JvmtiRawMonitor* rmonitor = new (std::nothrow) JvmtiRawMonitor(name); 3496 NULL_CHECK(rmonitor, JVMTI_ERROR_OUT_OF_MEMORY); 3497 3498 *monitor_ptr = (jrawMonitorID)rmonitor; 3499 3500 return JVMTI_ERROR_NONE; 3501 } /* end CreateRawMonitor */ 3502 3503 3504 // rmonitor - pre-checked for validity 3505 jvmtiError 3506 JvmtiEnv::DestroyRawMonitor(JvmtiRawMonitor * rmonitor) { 3507 if (Threads::number_of_threads() == 0) { 3508 // Remove this monitor from pending raw monitors list 3509 // if it has entered in onload or start phase. 3510 JvmtiPendingMonitors::destroy(rmonitor); 3511 } else { 3512 Thread* thread = Thread::current(); 3513 if (rmonitor->owner() == thread) { 3514 // The caller owns this monitor which we are about to destroy. 3515 // We exit the underlying synchronization object so that the 3516 // "delete monitor" call below can work without an assertion 3517 // failure on systems that don't like destroying synchronization 3518 // objects that are locked. 3519 int r; 3520 int recursion = rmonitor->recursions(); 3521 for (int i = 0; i <= recursion; i++) { 3522 r = rmonitor->raw_exit(thread); 3523 assert(r == JvmtiRawMonitor::M_OK, "raw_exit should have worked"); 3524 if (r != JvmtiRawMonitor::M_OK) { // robustness 3525 return JVMTI_ERROR_INTERNAL; 3526 } 3527 } 3528 } 3529 if (rmonitor->owner() != nullptr) { 3530 // The caller is trying to destroy a monitor that is locked by 3531 // someone else. While this is not forbidden by the JVMTI 3532 // spec, it will cause an assertion failure on systems that don't 3533 // like destroying synchronization objects that are locked. 3534 // We indicate a problem with the error return (and leak the 3535 // monitor's memory). 3536 return JVMTI_ERROR_NOT_MONITOR_OWNER; 3537 } 3538 } 3539 3540 delete rmonitor; 3541 3542 return JVMTI_ERROR_NONE; 3543 } /* end DestroyRawMonitor */ 3544 3545 3546 // rmonitor - pre-checked for validity 3547 jvmtiError 3548 JvmtiEnv::RawMonitorEnter(JvmtiRawMonitor * rmonitor) { 3549 if (Threads::number_of_threads() == 0) { 3550 // No JavaThreads exist so JvmtiRawMonitor enter cannot be 3551 // used, add this raw monitor to the pending list. 3552 // The pending monitors will be actually entered when 3553 // the VM is setup. 3554 // See transition_pending_raw_monitors in create_vm() 3555 // in thread.cpp. 3556 JvmtiPendingMonitors::enter(rmonitor); 3557 } else { 3558 Thread* thread = Thread::current(); 3559 // 8266889: raw_enter changes Java thread state, needs WXWrite 3560 MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread)); 3561 rmonitor->raw_enter(thread); 3562 } 3563 return JVMTI_ERROR_NONE; 3564 } /* end RawMonitorEnter */ 3565 3566 3567 // rmonitor - pre-checked for validity 3568 jvmtiError 3569 JvmtiEnv::RawMonitorExit(JvmtiRawMonitor * rmonitor) { 3570 jvmtiError err = JVMTI_ERROR_NONE; 3571 3572 if (Threads::number_of_threads() == 0) { 3573 // No JavaThreads exist so just remove this monitor from the pending list. 3574 // Bool value from exit is false if rmonitor is not in the list. 3575 if (!JvmtiPendingMonitors::exit(rmonitor)) { 3576 err = JVMTI_ERROR_NOT_MONITOR_OWNER; 3577 } 3578 } else { 3579 Thread* thread = Thread::current(); 3580 int r = rmonitor->raw_exit(thread); 3581 if (r == JvmtiRawMonitor::M_ILLEGAL_MONITOR_STATE) { 3582 err = JVMTI_ERROR_NOT_MONITOR_OWNER; 3583 } 3584 } 3585 return err; 3586 } /* end RawMonitorExit */ 3587 3588 3589 // rmonitor - pre-checked for validity 3590 jvmtiError 3591 JvmtiEnv::RawMonitorWait(JvmtiRawMonitor * rmonitor, jlong millis) { 3592 Thread* thread = Thread::current(); 3593 // 8266889: raw_wait changes Java thread state, needs WXWrite 3594 MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread)); 3595 int r = rmonitor->raw_wait(millis, thread); 3596 3597 switch (r) { 3598 case JvmtiRawMonitor::M_INTERRUPTED: 3599 return JVMTI_ERROR_INTERRUPT; 3600 case JvmtiRawMonitor::M_ILLEGAL_MONITOR_STATE: 3601 return JVMTI_ERROR_NOT_MONITOR_OWNER; 3602 default: 3603 return JVMTI_ERROR_NONE; 3604 } 3605 } /* end RawMonitorWait */ 3606 3607 3608 // rmonitor - pre-checked for validity 3609 jvmtiError 3610 JvmtiEnv::RawMonitorNotify(JvmtiRawMonitor * rmonitor) { 3611 Thread* thread = Thread::current(); 3612 int r = rmonitor->raw_notify(thread); 3613 3614 if (r == JvmtiRawMonitor::M_ILLEGAL_MONITOR_STATE) { 3615 return JVMTI_ERROR_NOT_MONITOR_OWNER; 3616 } 3617 return JVMTI_ERROR_NONE; 3618 } /* end RawMonitorNotify */ 3619 3620 3621 // rmonitor - pre-checked for validity 3622 jvmtiError 3623 JvmtiEnv::RawMonitorNotifyAll(JvmtiRawMonitor * rmonitor) { 3624 Thread* thread = Thread::current(); 3625 int r = rmonitor->raw_notifyAll(thread); 3626 3627 if (r == JvmtiRawMonitor::M_ILLEGAL_MONITOR_STATE) { 3628 return JVMTI_ERROR_NOT_MONITOR_OWNER; 3629 } 3630 return JVMTI_ERROR_NONE; 3631 } /* end RawMonitorNotifyAll */ 3632 3633 3634 // 3635 // JNI Function Interception functions 3636 // 3637 3638 3639 // function_table - pre-checked for null 3640 jvmtiError 3641 JvmtiEnv::SetJNIFunctionTable(const jniNativeInterface* function_table) { 3642 // Copy jni function table at safepoint. 3643 VM_JNIFunctionTableCopier copier(function_table); 3644 VMThread::execute(&copier); 3645 3646 return JVMTI_ERROR_NONE; 3647 } /* end SetJNIFunctionTable */ 3648 3649 3650 // function_table - pre-checked for null 3651 jvmtiError 3652 JvmtiEnv::GetJNIFunctionTable(jniNativeInterface** function_table) { 3653 *function_table=(jniNativeInterface*)jvmtiMalloc(sizeof(jniNativeInterface)); 3654 if (*function_table == nullptr) 3655 return JVMTI_ERROR_OUT_OF_MEMORY; 3656 memcpy(*function_table,(JavaThread::current())->get_jni_functions(),sizeof(jniNativeInterface)); 3657 return JVMTI_ERROR_NONE; 3658 } /* end GetJNIFunctionTable */ 3659 3660 3661 // 3662 // Event Management functions 3663 // 3664 3665 jvmtiError 3666 JvmtiEnv::GenerateEvents(jvmtiEvent event_type) { 3667 // can only generate two event types 3668 if (event_type != JVMTI_EVENT_COMPILED_METHOD_LOAD && 3669 event_type != JVMTI_EVENT_DYNAMIC_CODE_GENERATED) { 3670 return JVMTI_ERROR_ILLEGAL_ARGUMENT; 3671 } 3672 3673 // for compiled_method_load events we must check that the environment 3674 // has the can_generate_compiled_method_load_events capability. 3675 if (event_type == JVMTI_EVENT_COMPILED_METHOD_LOAD) { 3676 if (get_capabilities()->can_generate_compiled_method_load_events == 0) { 3677 return JVMTI_ERROR_MUST_POSSESS_CAPABILITY; 3678 } 3679 return JvmtiCodeBlobEvents::generate_compiled_method_load_events(this); 3680 } else { 3681 return JvmtiCodeBlobEvents::generate_dynamic_code_events(this); 3682 } 3683 3684 } /* end GenerateEvents */ 3685 3686 3687 // 3688 // Extension Mechanism functions 3689 // 3690 3691 // extension_count_ptr - pre-checked for null 3692 // extensions - pre-checked for null 3693 jvmtiError 3694 JvmtiEnv::GetExtensionFunctions(jint* extension_count_ptr, jvmtiExtensionFunctionInfo** extensions) { 3695 return JvmtiExtensions::get_functions(this, extension_count_ptr, extensions); 3696 } /* end GetExtensionFunctions */ 3697 3698 3699 // extension_count_ptr - pre-checked for null 3700 // extensions - pre-checked for null 3701 jvmtiError 3702 JvmtiEnv::GetExtensionEvents(jint* extension_count_ptr, jvmtiExtensionEventInfo** extensions) { 3703 return JvmtiExtensions::get_events(this, extension_count_ptr, extensions); 3704 } /* end GetExtensionEvents */ 3705 3706 3707 // callback - null is a valid value, must be checked 3708 jvmtiError 3709 JvmtiEnv::SetExtensionEventCallback(jint extension_event_index, jvmtiExtensionEvent callback) { 3710 return JvmtiExtensions::set_event_callback(this, extension_event_index, callback); 3711 } /* end SetExtensionEventCallback */ 3712 3713 // 3714 // Timers functions 3715 // 3716 3717 // info_ptr - pre-checked for null 3718 jvmtiError 3719 JvmtiEnv::GetCurrentThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) { 3720 os::current_thread_cpu_time_info(info_ptr); 3721 return JVMTI_ERROR_NONE; 3722 } /* end GetCurrentThreadCpuTimerInfo */ 3723 3724 3725 // nanos_ptr - pre-checked for null 3726 jvmtiError 3727 JvmtiEnv::GetCurrentThreadCpuTime(jlong* nanos_ptr) { 3728 Thread* thread = Thread::current(); 3729 3730 // Surprisingly the GetCurrentThreadCpuTime is used by non-JavaThread's. 3731 if (thread->is_Java_thread()) { 3732 if (JavaThread::cast(thread)->is_vthread_mounted()) { 3733 // No support for a VirtualThread (yet). 3734 return JVMTI_ERROR_UNSUPPORTED_OPERATION; 3735 } 3736 } 3737 *nanos_ptr = os::current_thread_cpu_time(); 3738 return JVMTI_ERROR_NONE; 3739 } /* end GetCurrentThreadCpuTime */ 3740 3741 3742 // info_ptr - pre-checked for null 3743 jvmtiError 3744 JvmtiEnv::GetThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) { 3745 os::thread_cpu_time_info(info_ptr); 3746 return JVMTI_ERROR_NONE; 3747 } /* end GetThreadCpuTimerInfo */ 3748 3749 3750 // nanos_ptr - pre-checked for null 3751 jvmtiError 3752 JvmtiEnv::GetThreadCpuTime(jthread thread, jlong* nanos_ptr) { 3753 JavaThread* current_thread = JavaThread::current(); 3754 ThreadsListHandle tlh(current_thread); 3755 JavaThread* java_thread = nullptr; 3756 oop thread_oop = nullptr; 3757 3758 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_oop); 3759 3760 if (thread_oop != nullptr && thread_oop->is_a(vmClasses::BaseVirtualThread_klass())) { 3761 // No support for virtual threads (yet). 3762 return JVMTI_ERROR_UNSUPPORTED_OPERATION; 3763 } 3764 if (err != JVMTI_ERROR_NONE) { 3765 return err; 3766 } 3767 NULL_CHECK(nanos_ptr, JVMTI_ERROR_NULL_POINTER); 3768 3769 *nanos_ptr = os::thread_cpu_time(java_thread); 3770 return JVMTI_ERROR_NONE; 3771 } /* end GetThreadCpuTime */ 3772 3773 3774 // info_ptr - pre-checked for null 3775 jvmtiError 3776 JvmtiEnv::GetTimerInfo(jvmtiTimerInfo* info_ptr) { 3777 os::javaTimeNanos_info(info_ptr); 3778 return JVMTI_ERROR_NONE; 3779 } /* end GetTimerInfo */ 3780 3781 3782 // nanos_ptr - pre-checked for null 3783 jvmtiError 3784 JvmtiEnv::GetTime(jlong* nanos_ptr) { 3785 *nanos_ptr = os::javaTimeNanos(); 3786 return JVMTI_ERROR_NONE; 3787 } /* end GetTime */ 3788 3789 3790 // processor_count_ptr - pre-checked for null 3791 jvmtiError 3792 JvmtiEnv::GetAvailableProcessors(jint* processor_count_ptr) { 3793 *processor_count_ptr = os::active_processor_count(); 3794 return JVMTI_ERROR_NONE; 3795 } /* end GetAvailableProcessors */ 3796 3797 jvmtiError 3798 JvmtiEnv::SetHeapSamplingInterval(jint sampling_interval) { 3799 if (sampling_interval < 0) { 3800 return JVMTI_ERROR_ILLEGAL_ARGUMENT; 3801 } 3802 ThreadHeapSampler::set_sampling_interval(sampling_interval); 3803 return JVMTI_ERROR_NONE; 3804 } /* end SetHeapSamplingInterval */ 3805 3806 // 3807 // System Properties functions 3808 // 3809 3810 // count_ptr - pre-checked for null 3811 // property_ptr - pre-checked for null 3812 jvmtiError 3813 JvmtiEnv::GetSystemProperties(jint* count_ptr, char*** property_ptr) { 3814 jvmtiError err = JVMTI_ERROR_NONE; 3815 3816 // Get the number of readable properties. 3817 *count_ptr = Arguments::PropertyList_readable_count(Arguments::system_properties()); 3818 3819 // Allocate memory to hold the exact number of readable properties. 3820 err = allocate(*count_ptr * sizeof(char *), (unsigned char **)property_ptr); 3821 if (err != JVMTI_ERROR_NONE) { 3822 return err; 3823 } 3824 int readable_count = 0; 3825 // Loop through the system properties until all the readable properties are found. 3826 for (SystemProperty* p = Arguments::system_properties(); p != nullptr && readable_count < *count_ptr; p = p->next()) { 3827 if (p->readable()) { 3828 const char *key = p->key(); 3829 char **tmp_value = *property_ptr+readable_count; 3830 readable_count++; 3831 err = allocate((strlen(key)+1) * sizeof(char), (unsigned char**)tmp_value); 3832 if (err == JVMTI_ERROR_NONE) { 3833 strcpy(*tmp_value, key); 3834 } else { 3835 // clean up previously allocated memory. 3836 for (int j = 0; j < readable_count; j++) { 3837 Deallocate((unsigned char*)*property_ptr+j); 3838 } 3839 Deallocate((unsigned char*)property_ptr); 3840 break; 3841 } 3842 } 3843 } 3844 assert(err != JVMTI_ERROR_NONE || readable_count == *count_ptr, "Bad readable property count"); 3845 return err; 3846 } /* end GetSystemProperties */ 3847 3848 3849 // property - pre-checked for null 3850 // value_ptr - pre-checked for null 3851 jvmtiError 3852 JvmtiEnv::GetSystemProperty(const char* property, char** value_ptr) { 3853 jvmtiError err = JVMTI_ERROR_NONE; 3854 const char *value; 3855 3856 // Return JVMTI_ERROR_NOT_AVAILABLE if property is not readable or doesn't exist. 3857 value = Arguments::PropertyList_get_readable_value(Arguments::system_properties(), property); 3858 if (value == nullptr) { 3859 err = JVMTI_ERROR_NOT_AVAILABLE; 3860 } else { 3861 err = allocate((strlen(value)+1) * sizeof(char), (unsigned char **)value_ptr); 3862 if (err == JVMTI_ERROR_NONE) { 3863 strcpy(*value_ptr, value); 3864 } 3865 } 3866 return err; 3867 } /* end GetSystemProperty */ 3868 3869 3870 // property - pre-checked for null 3871 // value - null is a valid value, must be checked 3872 jvmtiError 3873 JvmtiEnv::SetSystemProperty(const char* property, const char* value_ptr) { 3874 for (SystemProperty* p = Arguments::system_properties(); p != nullptr; p = p->next()) { 3875 if (strcmp(property, p->key()) == 0) { 3876 if (p->writeable()) { 3877 if (p->set_value(value_ptr, AllocFailStrategy::RETURN_NULL)) { 3878 return JVMTI_ERROR_NONE; 3879 } else { 3880 return JVMTI_ERROR_OUT_OF_MEMORY; 3881 } 3882 } else { 3883 // We found a property, but it's not writeable 3884 return JVMTI_ERROR_NOT_AVAILABLE; 3885 } 3886 } 3887 } 3888 3889 // We cannot find a property of the given name 3890 return JVMTI_ERROR_NOT_AVAILABLE; 3891 } /* end SetSystemProperty */