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