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