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