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