1 /* 2 * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/classLoaderDataGraph.hpp" 27 #include "classfile/javaClasses.inline.hpp" 28 #include "classfile/moduleEntry.hpp" 29 #include "classfile/symbolTable.hpp" 30 #include "classfile/vmSymbols.hpp" 31 #include "jvmtifiles/jvmtiEnv.hpp" 32 #include "memory/iterator.hpp" 33 #include "memory/resourceArea.hpp" 34 #include "oops/klass.inline.hpp" 35 #include "oops/objArrayKlass.hpp" 36 #include "oops/objArrayOop.hpp" 37 #include "oops/oop.inline.hpp" 38 #include "oops/oopHandle.inline.hpp" 39 #include "prims/jvmtiEnvBase.hpp" 40 #include "prims/jvmtiEventController.inline.hpp" 41 #include "prims/jvmtiExtensions.hpp" 42 #include "prims/jvmtiImpl.hpp" 43 #include "prims/jvmtiManageCapabilities.hpp" 44 #include "prims/jvmtiTagMap.hpp" 45 #include "prims/jvmtiThreadState.inline.hpp" 46 #include "runtime/continuationEntry.inline.hpp" 47 #include "runtime/deoptimization.hpp" 48 #include "runtime/frame.inline.hpp" 49 #include "runtime/handles.inline.hpp" 50 #include "runtime/interfaceSupport.inline.hpp" 51 #include "runtime/javaCalls.hpp" 52 #include "runtime/javaThread.inline.hpp" 53 #include "runtime/jfieldIDWorkaround.hpp" 54 #include "runtime/jniHandles.inline.hpp" 55 #include "runtime/objectMonitor.inline.hpp" 56 #include "runtime/osThread.hpp" 57 #include "runtime/signature.hpp" 58 #include "runtime/stackWatermarkSet.inline.hpp" 59 #include "runtime/synchronizer.inline.hpp" 60 #include "runtime/threads.hpp" 61 #include "runtime/threadSMR.inline.hpp" 62 #include "runtime/vframe.inline.hpp" 63 #include "runtime/vframe_hp.hpp" 64 #include "runtime/vmThread.hpp" 65 #include "runtime/vmOperations.hpp" 66 #include "services/threadService.hpp" 67 68 69 /////////////////////////////////////////////////////////////// 70 // 71 // JvmtiEnvBase 72 // 73 74 JvmtiEnvBase* JvmtiEnvBase::_head_environment = nullptr; 75 76 bool JvmtiEnvBase::_globally_initialized = false; 77 volatile bool JvmtiEnvBase::_needs_clean_up = false; 78 79 jvmtiPhase JvmtiEnvBase::_phase = JVMTI_PHASE_PRIMORDIAL; 80 81 volatile int JvmtiEnvBase::_dying_thread_env_iteration_count = 0; 82 83 extern jvmtiInterface_1_ jvmti_Interface; 84 extern jvmtiInterface_1_ jvmtiTrace_Interface; 85 86 87 // perform initializations that must occur before any JVMTI environments 88 // are released but which should only be initialized once (no matter 89 // how many environments are created). 90 void 91 JvmtiEnvBase::globally_initialize() { 92 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check"); 93 assert(_globally_initialized == false, "bad call"); 94 95 JvmtiManageCapabilities::initialize(); 96 97 // register extension functions and events 98 JvmtiExtensions::register_extensions(); 99 100 #ifdef JVMTI_TRACE 101 JvmtiTrace::initialize(); 102 #endif 103 104 _globally_initialized = true; 105 } 106 107 108 void 109 JvmtiEnvBase::initialize() { 110 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check"); 111 112 // Add this environment to the end of the environment list (order is important) 113 { 114 // This block of code must not contain any safepoints, as list deallocation 115 // (which occurs at a safepoint) cannot occur simultaneously with this list 116 // addition. Note: NoSafepointVerifier cannot, currently, be used before 117 // threads exist. 118 JvmtiEnvIterator it; 119 JvmtiEnvBase *previous_env = nullptr; 120 for (JvmtiEnvBase* env = it.first(); env != nullptr; env = it.next(env)) { 121 previous_env = env; 122 } 123 if (previous_env == nullptr) { 124 _head_environment = this; 125 } else { 126 previous_env->set_next_environment(this); 127 } 128 } 129 130 if (_globally_initialized == false) { 131 globally_initialize(); 132 } 133 } 134 135 jvmtiPhase 136 JvmtiEnvBase::phase() { 137 // For the JVMTI environments possessed the can_generate_early_vmstart: 138 // replace JVMTI_PHASE_PRIMORDIAL with JVMTI_PHASE_START 139 if (_phase == JVMTI_PHASE_PRIMORDIAL && 140 JvmtiExport::early_vmstart_recorded() && 141 early_vmstart_env()) { 142 return JVMTI_PHASE_START; 143 } 144 return _phase; // Normal case 145 } 146 147 bool 148 JvmtiEnvBase::is_valid() { 149 jlong value = 0; 150 151 // This object might not be a JvmtiEnvBase so we can't assume 152 // the _magic field is properly aligned. Get the value in a safe 153 // way and then check against JVMTI_MAGIC. 154 155 switch (sizeof(_magic)) { 156 case 2: 157 value = Bytes::get_native_u2((address)&_magic); 158 break; 159 160 case 4: 161 value = Bytes::get_native_u4((address)&_magic); 162 break; 163 164 case 8: 165 value = Bytes::get_native_u8((address)&_magic); 166 break; 167 168 default: 169 guarantee(false, "_magic field is an unexpected size"); 170 } 171 172 return value == JVMTI_MAGIC; 173 } 174 175 176 bool 177 JvmtiEnvBase::use_version_1_0_semantics() { 178 int major, minor, micro; 179 180 JvmtiExport::decode_version_values(_version, &major, &minor, µ); 181 return major == 1 && minor == 0; // micro version doesn't matter here 182 } 183 184 185 bool 186 JvmtiEnvBase::use_version_1_1_semantics() { 187 int major, minor, micro; 188 189 JvmtiExport::decode_version_values(_version, &major, &minor, µ); 190 return major == 1 && minor == 1; // micro version doesn't matter here 191 } 192 193 bool 194 JvmtiEnvBase::use_version_1_2_semantics() { 195 int major, minor, micro; 196 197 JvmtiExport::decode_version_values(_version, &major, &minor, µ); 198 return major == 1 && minor == 2; // micro version doesn't matter here 199 } 200 201 202 JvmtiEnvBase::JvmtiEnvBase(jint version) : _env_event_enable() { 203 _version = version; 204 _env_local_storage = nullptr; 205 _tag_map = nullptr; 206 _native_method_prefix_count = 0; 207 _native_method_prefixes = nullptr; 208 _next = nullptr; 209 _class_file_load_hook_ever_enabled = false; 210 211 // Moot since ClassFileLoadHook not yet enabled. 212 // But "true" will give a more predictable ClassFileLoadHook behavior 213 // for environment creation during ClassFileLoadHook. 214 _is_retransformable = true; 215 216 // all callbacks initially null 217 memset(&_event_callbacks, 0, sizeof(jvmtiEventCallbacks)); 218 memset(&_ext_event_callbacks, 0, sizeof(jvmtiExtEventCallbacks)); 219 220 // all capabilities initially off 221 memset(&_current_capabilities, 0, sizeof(_current_capabilities)); 222 223 // all prohibited capabilities initially off 224 memset(&_prohibited_capabilities, 0, sizeof(_prohibited_capabilities)); 225 226 _magic = JVMTI_MAGIC; 227 228 JvmtiEventController::env_initialize((JvmtiEnv*)this); 229 230 #ifdef JVMTI_TRACE 231 _jvmti_external.functions = TraceJVMTI != nullptr ? &jvmtiTrace_Interface : &jvmti_Interface; 232 #else 233 _jvmti_external.functions = &jvmti_Interface; 234 #endif 235 } 236 237 238 void 239 JvmtiEnvBase::dispose() { 240 241 #ifdef JVMTI_TRACE 242 JvmtiTrace::shutdown(); 243 #endif 244 245 // Dispose of event info and let the event controller call us back 246 // in a locked state (env_dispose, below) 247 JvmtiEventController::env_dispose(this); 248 } 249 250 void 251 JvmtiEnvBase::env_dispose() { 252 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check"); 253 254 // We have been entered with all events disabled on this environment. 255 // A race to re-enable events (by setting callbacks) is prevented by 256 // checking for a valid environment when setting callbacks (while 257 // holding the JvmtiThreadState_lock). 258 259 // Mark as invalid. 260 _magic = DISPOSED_MAGIC; 261 262 // Relinquish all capabilities. 263 jvmtiCapabilities *caps = get_capabilities(); 264 JvmtiManageCapabilities::relinquish_capabilities(caps, caps, caps); 265 266 // Same situation as with events (see above) 267 set_native_method_prefixes(0, nullptr); 268 269 JvmtiTagMap* tag_map_to_clear = tag_map_acquire(); 270 // A tag map can be big, clear it now to save memory until 271 // the destructor runs. 272 if (tag_map_to_clear != nullptr) { 273 tag_map_to_clear->clear(); 274 } 275 276 _needs_clean_up = true; 277 } 278 279 280 JvmtiEnvBase::~JvmtiEnvBase() { 281 assert(SafepointSynchronize::is_at_safepoint(), "sanity check"); 282 283 // There is a small window of time during which the tag map of a 284 // disposed environment could have been reallocated. 285 // Make sure it is gone. 286 JvmtiTagMap* tag_map_to_deallocate = _tag_map; 287 set_tag_map(nullptr); 288 // A tag map can be big, deallocate it now 289 if (tag_map_to_deallocate != nullptr) { 290 delete tag_map_to_deallocate; 291 } 292 293 _magic = BAD_MAGIC; 294 } 295 296 297 void 298 JvmtiEnvBase::periodic_clean_up() { 299 assert(SafepointSynchronize::is_at_safepoint(), "sanity check"); 300 301 // JvmtiEnvBase reference is saved in JvmtiEnvThreadState. So 302 // clean up JvmtiThreadState before deleting JvmtiEnv pointer. 303 JvmtiThreadState::periodic_clean_up(); 304 305 // Unlink all invalid environments from the list of environments 306 // and deallocate them 307 JvmtiEnvIterator it; 308 JvmtiEnvBase* previous_env = nullptr; 309 JvmtiEnvBase* env = it.first(); 310 while (env != nullptr) { 311 if (env->is_valid()) { 312 previous_env = env; 313 env = it.next(env); 314 } else { 315 // This one isn't valid, remove it from the list and deallocate it 316 JvmtiEnvBase* defunct_env = env; 317 env = it.next(env); 318 if (previous_env == nullptr) { 319 _head_environment = env; 320 } else { 321 previous_env->set_next_environment(env); 322 } 323 delete defunct_env; 324 } 325 } 326 327 } 328 329 330 void 331 JvmtiEnvBase::check_for_periodic_clean_up() { 332 assert(SafepointSynchronize::is_at_safepoint(), "sanity check"); 333 334 class ThreadInsideIterationClosure: public ThreadClosure { 335 private: 336 bool _inside; 337 public: 338 ThreadInsideIterationClosure() : _inside(false) {}; 339 340 void do_thread(Thread* thread) { 341 _inside |= thread->is_inside_jvmti_env_iteration(); 342 } 343 344 bool is_inside_jvmti_env_iteration() { 345 return _inside; 346 } 347 }; 348 349 if (_needs_clean_up) { 350 // Check if we are currently iterating environment, 351 // deallocation should not occur if we are 352 ThreadInsideIterationClosure tiic; 353 Threads::threads_do(&tiic); 354 if (!tiic.is_inside_jvmti_env_iteration() && 355 !is_inside_dying_thread_env_iteration()) { 356 _needs_clean_up = false; 357 JvmtiEnvBase::periodic_clean_up(); 358 } 359 } 360 } 361 362 363 void 364 JvmtiEnvBase::record_first_time_class_file_load_hook_enabled() { 365 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), 366 "sanity check"); 367 368 if (!_class_file_load_hook_ever_enabled) { 369 _class_file_load_hook_ever_enabled = true; 370 371 if (get_capabilities()->can_retransform_classes) { 372 _is_retransformable = true; 373 } else { 374 _is_retransformable = false; 375 376 // cannot add retransform capability after ClassFileLoadHook has been enabled 377 get_prohibited_capabilities()->can_retransform_classes = 1; 378 } 379 } 380 } 381 382 383 void 384 JvmtiEnvBase::record_class_file_load_hook_enabled() { 385 if (!_class_file_load_hook_ever_enabled) { 386 if (Threads::number_of_threads() == 0) { 387 record_first_time_class_file_load_hook_enabled(); 388 } else { 389 MutexLocker mu(JvmtiThreadState_lock); 390 record_first_time_class_file_load_hook_enabled(); 391 } 392 } 393 } 394 395 396 jvmtiError 397 JvmtiEnvBase::set_native_method_prefixes(jint prefix_count, char** prefixes) { 398 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), 399 "sanity check"); 400 401 int old_prefix_count = get_native_method_prefix_count(); 402 char **old_prefixes = get_native_method_prefixes(); 403 404 // allocate and install the new prefixex 405 if (prefix_count == 0 || !is_valid()) { 406 _native_method_prefix_count = 0; 407 _native_method_prefixes = nullptr; 408 } else { 409 // there are prefixes, allocate an array to hold them, and fill it 410 char** new_prefixes = (char**)os::malloc((prefix_count) * sizeof(char*), mtInternal); 411 if (new_prefixes == nullptr) { 412 return JVMTI_ERROR_OUT_OF_MEMORY; 413 } 414 for (int i = 0; i < prefix_count; i++) { 415 char* prefix = prefixes[i]; 416 if (prefix == nullptr) { 417 for (int j = 0; j < (i-1); j++) { 418 os::free(new_prefixes[j]); 419 } 420 os::free(new_prefixes); 421 return JVMTI_ERROR_NULL_POINTER; 422 } 423 prefix = os::strdup(prefixes[i]); 424 if (prefix == nullptr) { 425 for (int j = 0; j < (i-1); j++) { 426 os::free(new_prefixes[j]); 427 } 428 os::free(new_prefixes); 429 return JVMTI_ERROR_OUT_OF_MEMORY; 430 } 431 new_prefixes[i] = prefix; 432 } 433 _native_method_prefix_count = prefix_count; 434 _native_method_prefixes = new_prefixes; 435 } 436 437 // now that we know the new prefixes have been successfully installed we can 438 // safely remove the old ones 439 if (old_prefix_count != 0) { 440 for (int i = 0; i < old_prefix_count; i++) { 441 os::free(old_prefixes[i]); 442 } 443 os::free(old_prefixes); 444 } 445 446 return JVMTI_ERROR_NONE; 447 } 448 449 450 // Collect all the prefixes which have been set in any JVM TI environments 451 // by the SetNativeMethodPrefix(es) functions. Be sure to maintain the 452 // order of environments and the order of prefixes within each environment. 453 // Return in a resource allocated array. 454 char** 455 JvmtiEnvBase::get_all_native_method_prefixes(int* count_ptr) { 456 assert(Threads::number_of_threads() == 0 || 457 SafepointSynchronize::is_at_safepoint() || 458 JvmtiThreadState_lock->is_locked(), 459 "sanity check"); 460 461 int total_count = 0; 462 GrowableArray<char*>* prefix_array =new GrowableArray<char*>(5); 463 464 JvmtiEnvIterator it; 465 for (JvmtiEnvBase* env = it.first(); env != nullptr; env = it.next(env)) { 466 int prefix_count = env->get_native_method_prefix_count(); 467 char** prefixes = env->get_native_method_prefixes(); 468 for (int j = 0; j < prefix_count; j++) { 469 // retrieve a prefix and so that it is safe against asynchronous changes 470 // copy it into the resource area 471 char* prefix = prefixes[j]; 472 char* prefix_copy = NEW_RESOURCE_ARRAY(char, strlen(prefix)+1); 473 strcpy(prefix_copy, prefix); 474 prefix_array->at_put_grow(total_count++, prefix_copy); 475 } 476 } 477 478 char** all_prefixes = NEW_RESOURCE_ARRAY(char*, total_count); 479 char** p = all_prefixes; 480 for (int i = 0; i < total_count; ++i) { 481 *p++ = prefix_array->at(i); 482 } 483 *count_ptr = total_count; 484 return all_prefixes; 485 } 486 487 void 488 JvmtiEnvBase::set_event_callbacks(const jvmtiEventCallbacks* callbacks, 489 jint size_of_callbacks) { 490 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check"); 491 492 size_t byte_cnt = sizeof(jvmtiEventCallbacks); 493 494 // clear in either case to be sure we got any gap between sizes 495 memset(&_event_callbacks, 0, byte_cnt); 496 497 // Now that JvmtiThreadState_lock is held, prevent a possible race condition where events 498 // are re-enabled by a call to set event callbacks where the DisposeEnvironment 499 // occurs after the boiler-plate environment check and before the lock is acquired. 500 if (callbacks != nullptr && is_valid()) { 501 if (size_of_callbacks < (jint)byte_cnt) { 502 byte_cnt = size_of_callbacks; 503 } 504 memcpy(&_event_callbacks, callbacks, byte_cnt); 505 } 506 } 507 508 509 // In the fullness of time, all users of the method should instead 510 // directly use allocate, besides being cleaner and faster, this will 511 // mean much better out of memory handling 512 unsigned char * 513 JvmtiEnvBase::jvmtiMalloc(jlong size) { 514 unsigned char* mem = nullptr; 515 jvmtiError result = allocate(size, &mem); 516 assert(result == JVMTI_ERROR_NONE, "Allocate failed"); 517 return mem; 518 } 519 520 521 // Handle management 522 523 jobject JvmtiEnvBase::jni_reference(Handle hndl) { 524 return JNIHandles::make_local(hndl()); 525 } 526 527 jobject JvmtiEnvBase::jni_reference(JavaThread *thread, Handle hndl) { 528 return JNIHandles::make_local(thread, hndl()); 529 } 530 531 void JvmtiEnvBase::destroy_jni_reference(jobject jobj) { 532 JNIHandles::destroy_local(jobj); 533 } 534 535 void JvmtiEnvBase::destroy_jni_reference(JavaThread *thread, jobject jobj) { 536 JNIHandles::destroy_local(jobj); // thread is unused. 537 } 538 539 // 540 // Threads 541 // 542 543 jthread * 544 JvmtiEnvBase::new_jthreadArray(int length, Handle *handles) { 545 if (length == 0) { 546 return nullptr; 547 } 548 549 jthread* objArray = (jthread *) jvmtiMalloc(sizeof(jthread) * length); 550 NULL_CHECK(objArray, nullptr); 551 552 for (int i = 0; i < length; i++) { 553 objArray[i] = (jthread)jni_reference(handles[i]); 554 } 555 return objArray; 556 } 557 558 jthreadGroup * 559 JvmtiEnvBase::new_jthreadGroupArray(int length, objArrayHandle groups) { 560 if (length == 0) { 561 return nullptr; 562 } 563 564 jthreadGroup* objArray = (jthreadGroup *) jvmtiMalloc(sizeof(jthreadGroup) * length); 565 NULL_CHECK(objArray, nullptr); 566 567 for (int i = 0; i < length; i++) { 568 objArray[i] = (jthreadGroup)JNIHandles::make_local(groups->obj_at(i)); 569 } 570 return objArray; 571 } 572 573 // Return the vframe on the specified thread and depth, null if no such frame. 574 // The thread and the oops in the returned vframe might not have been processed. 575 javaVFrame* 576 JvmtiEnvBase::jvf_for_thread_and_depth(JavaThread* java_thread, jint depth) { 577 if (!java_thread->has_last_Java_frame()) { 578 return nullptr; 579 } 580 RegisterMap reg_map(java_thread, 581 RegisterMap::UpdateMap::include, 582 RegisterMap::ProcessFrames::skip, 583 RegisterMap::WalkContinuation::include); 584 javaVFrame *jvf = java_thread->last_java_vframe(®_map); 585 586 jvf = JvmtiEnvBase::check_and_skip_hidden_frames(java_thread, jvf); 587 588 for (int d = 0; jvf != nullptr && d < depth; d++) { 589 jvf = jvf->java_sender(); 590 } 591 return jvf; 592 } 593 594 // 595 // utilities: JNI objects 596 // 597 598 599 jclass 600 JvmtiEnvBase::get_jni_class_non_null(Klass* k) { 601 assert(k != nullptr, "k != null"); 602 assert(k->is_loader_alive(), "Must be alive"); 603 Thread *thread = Thread::current(); 604 return (jclass)jni_reference(Handle(thread, k->java_mirror())); 605 } 606 607 // 608 // Field Information 609 // 610 611 bool 612 JvmtiEnvBase::get_field_descriptor(Klass* k, jfieldID field, fieldDescriptor* fd) { 613 if (!jfieldIDWorkaround::is_valid_jfieldID(k, field)) { 614 return false; 615 } 616 bool found = false; 617 if (jfieldIDWorkaround::is_static_jfieldID(field)) { 618 JNIid* id = jfieldIDWorkaround::from_static_jfieldID(field); 619 found = id->find_local_field(fd); 620 } else { 621 // Non-static field. The fieldID is really the offset of the field within the object. 622 int offset = jfieldIDWorkaround::from_instance_jfieldID(k, field); 623 found = InstanceKlass::cast(k)->find_field_from_offset(offset, false, fd); 624 } 625 return found; 626 } 627 628 bool 629 JvmtiEnvBase::is_vthread_alive(oop vt) { 630 oop cont = java_lang_VirtualThread::continuation(vt); 631 return !jdk_internal_vm_Continuation::done(cont) && 632 java_lang_VirtualThread::state(vt) != java_lang_VirtualThread::NEW; 633 } 634 635 // Return JavaThread if virtual thread is mounted, null otherwise. 636 JavaThread* JvmtiEnvBase::get_JavaThread_or_null(oop vthread) { 637 oop carrier_thread = java_lang_VirtualThread::carrier_thread(vthread); 638 if (carrier_thread == nullptr) { 639 return nullptr; 640 } 641 642 JavaThread* java_thread = java_lang_Thread::thread(carrier_thread); 643 644 // This could be a different thread to the current one. So we need to ensure that 645 // processing has started before we are allowed to read the continuation oop of 646 // another thread, as it is a direct root of that other thread. 647 StackWatermarkSet::start_processing(java_thread, StackWatermarkKind::gc); 648 649 oop cont = java_lang_VirtualThread::continuation(vthread); 650 assert(cont != nullptr, "must be"); 651 assert(Continuation::continuation_scope(cont) == java_lang_VirtualThread::vthread_scope(), "must be"); 652 return Continuation::is_continuation_mounted(java_thread, cont) ? java_thread : nullptr; 653 } 654 655 javaVFrame* 656 JvmtiEnvBase::check_and_skip_hidden_frames(bool is_in_VTMS_transition, javaVFrame* jvf) { 657 // The second condition is needed to hide notification methods. 658 if (!is_in_VTMS_transition && (jvf == nullptr || !jvf->method()->jvmti_mount_transition())) { 659 return jvf; // No frames to skip. 660 } 661 // Find jvf with a method annotated with @JvmtiMountTransition. 662 for ( ; jvf != nullptr; jvf = jvf->java_sender()) { 663 if (jvf->method()->jvmti_mount_transition()) { // Cannot actually appear in an unmounted continuation; they're never frozen. 664 jvf = jvf->java_sender(); // Skip annotated method. 665 break; 666 } 667 if (jvf->method()->changes_current_thread()) { 668 break; 669 } 670 // Skip frame above annotated method. 671 } 672 return jvf; 673 } 674 675 javaVFrame* 676 JvmtiEnvBase::check_and_skip_hidden_frames(JavaThread* jt, javaVFrame* jvf) { 677 jvf = check_and_skip_hidden_frames(jt->is_in_VTMS_transition(), jvf); 678 return jvf; 679 } 680 681 javaVFrame* 682 JvmtiEnvBase::check_and_skip_hidden_frames(oop vthread, javaVFrame* jvf) { 683 JvmtiThreadState* state = java_lang_Thread::jvmti_thread_state(vthread); 684 if (state == nullptr) { 685 // nothing to skip 686 return jvf; 687 } 688 jvf = check_and_skip_hidden_frames(java_lang_Thread::is_in_VTMS_transition(vthread), jvf); 689 return jvf; 690 } 691 692 javaVFrame* 693 JvmtiEnvBase::get_vthread_jvf(oop vthread) { 694 assert(java_lang_VirtualThread::state(vthread) != java_lang_VirtualThread::NEW, "sanity check"); 695 assert(java_lang_VirtualThread::state(vthread) != java_lang_VirtualThread::TERMINATED, "sanity check"); 696 697 Thread* cur_thread = Thread::current(); 698 oop cont = java_lang_VirtualThread::continuation(vthread); 699 javaVFrame* jvf = nullptr; 700 701 JavaThread* java_thread = get_JavaThread_or_null(vthread); 702 if (java_thread != nullptr) { 703 if (!java_thread->has_last_Java_frame()) { 704 // TBD: This is a temporary work around to avoid a guarantee caused by 705 // the native enterSpecial frame on the top. No frames will be found 706 // by the JVMTI functions such as GetStackTrace. 707 return nullptr; 708 } 709 vframeStream vfs(java_thread); 710 jvf = vfs.at_end() ? nullptr : vfs.asJavaVFrame(); 711 jvf = check_and_skip_hidden_frames(java_thread, jvf); 712 } else { 713 vframeStream vfs(cont); 714 jvf = vfs.at_end() ? nullptr : vfs.asJavaVFrame(); 715 jvf = check_and_skip_hidden_frames(vthread, jvf); 716 } 717 return jvf; 718 } 719 720 // Return correct javaVFrame for a carrier (non-virtual) thread. 721 // It strips vthread frames at the top if there are any. 722 javaVFrame* 723 JvmtiEnvBase::get_cthread_last_java_vframe(JavaThread* jt, RegisterMap* reg_map_p) { 724 // Strip vthread frames in case of carrier thread with mounted continuation. 725 bool cthread_with_cont = JvmtiEnvBase::is_cthread_with_continuation(jt); 726 javaVFrame *jvf = cthread_with_cont ? jt->carrier_last_java_vframe(reg_map_p) 727 : jt->last_java_vframe(reg_map_p); 728 // Skip hidden frames only for carrier threads 729 // which are in non-temporary VTMS transition. 730 if (jt->is_in_VTMS_transition()) { 731 jvf = check_and_skip_hidden_frames(jt, jvf); 732 } 733 return jvf; 734 } 735 736 jint 737 JvmtiEnvBase::get_thread_state_base(oop thread_oop, JavaThread* jt) { 738 jint state = 0; 739 740 if (thread_oop != nullptr) { 741 // Get most state bits. 742 state = (jint)java_lang_Thread::get_thread_status(thread_oop); 743 } 744 if (jt != nullptr) { 745 // We have a JavaThread* so add more state bits. 746 JavaThreadState jts = jt->thread_state(); 747 748 if (jt->is_carrier_thread_suspended() || 749 ((jt->jvmti_vthread() == nullptr || jt->jvmti_vthread() == thread_oop) && jt->is_suspended())) { 750 // Suspended non-virtual thread. 751 state |= JVMTI_THREAD_STATE_SUSPENDED; 752 } 753 if (jts == _thread_in_native) { 754 state |= JVMTI_THREAD_STATE_IN_NATIVE; 755 } 756 if (jt->is_interrupted(false)) { 757 state |= JVMTI_THREAD_STATE_INTERRUPTED; 758 } 759 } 760 return state; 761 } 762 763 jint 764 JvmtiEnvBase::get_thread_state(oop thread_oop, JavaThread* jt) { 765 jint state = 0; 766 767 if (is_thread_carrying_vthread(jt, thread_oop)) { 768 state = (jint)java_lang_Thread::get_thread_status(thread_oop); 769 770 // This is for extra safety. Other bits are not expected nor needed. 771 state &= (JVMTI_THREAD_STATE_ALIVE | JVMTI_THREAD_STATE_INTERRUPTED); 772 773 if (jt->is_carrier_thread_suspended()) { 774 state |= JVMTI_THREAD_STATE_SUSPENDED; 775 } 776 // It's okay for the JVMTI state to be reported as WAITING when waiting 777 // for something other than an Object.wait. So, we treat a thread carrying 778 // a virtual thread as waiting indefinitely which is not runnable. 779 // It is why the RUNNABLE bit is not needed and the WAITING bits are added. 780 state |= JVMTI_THREAD_STATE_WAITING | JVMTI_THREAD_STATE_WAITING_INDEFINITELY; 781 } else { 782 state = get_thread_state_base(thread_oop, jt); 783 } 784 return state; 785 } 786 787 jint 788 JvmtiEnvBase::get_vthread_state(oop thread_oop, JavaThread* java_thread) { 789 jint state = 0; 790 bool ext_suspended = JvmtiVTSuspender::is_vthread_suspended(thread_oop); 791 jint interrupted = java_lang_Thread::interrupted(thread_oop); 792 793 if (java_thread != nullptr) { 794 // If virtual thread is blocked on a monitor enter the BLOCKED_ON_MONITOR_ENTER bit 795 // is set for carrier thread instead of virtual. 796 // Other state bits except filtered ones are expected to be the same. 797 oop ct_oop = java_lang_VirtualThread::carrier_thread(thread_oop); 798 jint filtered_bits = JVMTI_THREAD_STATE_SUSPENDED | JVMTI_THREAD_STATE_INTERRUPTED; 799 800 // This call can trigger a safepoint, so thread_oop must not be used after it. 801 state = get_thread_state_base(ct_oop, java_thread) & ~filtered_bits; 802 } else { 803 int vt_state = java_lang_VirtualThread::state(thread_oop); 804 state = (jint)java_lang_VirtualThread::map_state_to_thread_status(vt_state); 805 } 806 // Ensure the thread has not exited after retrieving suspended/interrupted values. 807 if ((state & JVMTI_THREAD_STATE_ALIVE) != 0) { 808 if (ext_suspended) { 809 state |= JVMTI_THREAD_STATE_SUSPENDED; 810 } 811 if (interrupted) { 812 state |= JVMTI_THREAD_STATE_INTERRUPTED; 813 } 814 } 815 return state; 816 } 817 818 jint 819 JvmtiEnvBase::get_thread_or_vthread_state(oop thread_oop, JavaThread* java_thread) { 820 jint state = 0; 821 if (java_lang_VirtualThread::is_instance(thread_oop)) { 822 state = JvmtiEnvBase::get_vthread_state(thread_oop, java_thread); 823 } else { 824 state = JvmtiEnvBase::get_thread_state(thread_oop, java_thread); 825 } 826 return state; 827 } 828 829 jvmtiError 830 JvmtiEnvBase::get_live_threads(JavaThread* current_thread, Handle group_hdl, jint *count_ptr, Handle **thread_objs_p) { 831 jint count = 0; 832 Handle *thread_objs = nullptr; 833 ThreadsListEnumerator tle(current_thread, /* include_jvmti_agent_threads */ true); 834 int nthreads = tle.num_threads(); 835 if (nthreads > 0) { 836 thread_objs = NEW_RESOURCE_ARRAY_RETURN_NULL(Handle, nthreads); 837 NULL_CHECK(thread_objs, JVMTI_ERROR_OUT_OF_MEMORY); 838 for (int i = 0; i < nthreads; i++) { 839 Handle thread = tle.get_threadObj(i); 840 if (thread()->is_a(vmClasses::Thread_klass()) && java_lang_Thread::threadGroup(thread()) == group_hdl()) { 841 thread_objs[count++] = thread; 842 } 843 } 844 } 845 *thread_objs_p = thread_objs; 846 *count_ptr = count; 847 return JVMTI_ERROR_NONE; 848 } 849 850 jvmtiError 851 JvmtiEnvBase::get_subgroups(JavaThread* current_thread, Handle group_hdl, jint *count_ptr, objArrayHandle *group_objs_p) { 852 853 // This call collects the strong and weak groups 854 JavaThread* THREAD = current_thread; 855 JavaValue result(T_OBJECT); 856 JavaCalls::call_virtual(&result, 857 group_hdl, 858 vmClasses::ThreadGroup_klass(), 859 SymbolTable::new_permanent_symbol("subgroupsAsArray"), 860 vmSymbols::void_threadgroup_array_signature(), 861 THREAD); 862 if (HAS_PENDING_EXCEPTION) { 863 Symbol* ex_name = PENDING_EXCEPTION->klass()->name(); 864 CLEAR_PENDING_EXCEPTION; 865 if (ex_name == vmSymbols::java_lang_OutOfMemoryError()) { 866 return JVMTI_ERROR_OUT_OF_MEMORY; 867 } else { 868 return JVMTI_ERROR_INTERNAL; 869 } 870 } 871 872 assert(result.get_type() == T_OBJECT, "just checking"); 873 objArrayOop groups = (objArrayOop)result.get_oop(); 874 875 *count_ptr = groups == nullptr ? 0 : groups->length(); 876 *group_objs_p = objArrayHandle(current_thread, groups); 877 878 return JVMTI_ERROR_NONE; 879 } 880 881 // 882 // Object Monitor Information 883 // 884 885 // 886 // Count the number of objects for a lightweight monitor. The hobj 887 // parameter is object that owns the monitor so this routine will 888 // count the number of times the same object was locked by frames 889 // in java_thread. 890 // 891 jint 892 JvmtiEnvBase::count_locked_objects(JavaThread *java_thread, Handle hobj) { 893 jint ret = 0; 894 if (!java_thread->has_last_Java_frame()) { 895 return ret; // no Java frames so no monitors 896 } 897 898 Thread* current_thread = Thread::current(); 899 ResourceMark rm(current_thread); 900 HandleMark hm(current_thread); 901 RegisterMap reg_map(java_thread, 902 RegisterMap::UpdateMap::include, 903 RegisterMap::ProcessFrames::include, 904 RegisterMap::WalkContinuation::skip); 905 906 for (javaVFrame *jvf = java_thread->last_java_vframe(®_map); jvf != nullptr; 907 jvf = jvf->java_sender()) { 908 GrowableArray<MonitorInfo*>* mons = jvf->monitors(); 909 if (!mons->is_empty()) { 910 for (int i = 0; i < mons->length(); i++) { 911 MonitorInfo *mi = mons->at(i); 912 if (mi->owner_is_scalar_replaced()) continue; 913 914 // see if owner of the monitor is our object 915 if (mi->owner() != nullptr && mi->owner() == hobj()) { 916 ret++; 917 } 918 } 919 } 920 } 921 return ret; 922 } 923 924 jvmtiError 925 JvmtiEnvBase::get_current_contended_monitor(JavaThread *calling_thread, JavaThread *java_thread, 926 jobject *monitor_ptr, bool is_virtual) { 927 Thread *current_thread = Thread::current(); 928 assert(java_thread->is_handshake_safe_for(current_thread), 929 "call by myself or at handshake"); 930 if (!is_virtual && JvmtiEnvBase::is_cthread_with_continuation(java_thread)) { 931 // Carrier thread with a mounted continuation case. 932 // No contended monitor can be owned by carrier thread in this case. 933 *monitor_ptr = nullptr; 934 return JVMTI_ERROR_NONE; 935 } 936 oop obj = nullptr; 937 // The ObjectMonitor* can't be async deflated since we are either 938 // at a safepoint or the calling thread is operating on itself so 939 // it cannot leave the underlying wait()/enter() call. 940 ObjectMonitor *mon = java_thread->current_waiting_monitor(); 941 if (mon == nullptr) { 942 // thread is not doing an Object.wait() call 943 mon = java_thread->current_pending_monitor(); 944 if (mon != nullptr) { 945 // The thread is trying to enter() an ObjectMonitor. 946 obj = mon->object(); 947 assert(obj != nullptr, "ObjectMonitor should have a valid object!"); 948 } 949 } else { 950 // thread is doing an Object.wait() call 951 oop thread_oop = get_vthread_or_thread_oop(java_thread); 952 jint state = get_thread_or_vthread_state(thread_oop, java_thread); 953 954 if (state & JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER) { 955 // thread is re-entering the monitor in an Object.wait() call 956 obj = mon->object(); 957 assert(obj != nullptr, "Object.wait() should have an object"); 958 } 959 } 960 961 if (obj == nullptr) { 962 *monitor_ptr = nullptr; 963 } else { 964 HandleMark hm(current_thread); 965 Handle hobj(current_thread, obj); 966 *monitor_ptr = jni_reference(calling_thread, hobj); 967 } 968 return JVMTI_ERROR_NONE; 969 } 970 971 jvmtiError 972 JvmtiEnvBase::get_owned_monitors(JavaThread *calling_thread, JavaThread* java_thread, 973 GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list) { 974 // Note: 975 // calling_thread is the thread that requested the list of monitors for java_thread. 976 // java_thread is the thread owning the monitors. 977 // current_thread is the thread executing this code, can be a non-JavaThread (e.g. VM Thread). 978 // And they all may be different threads. 979 jvmtiError err = JVMTI_ERROR_NONE; 980 Thread *current_thread = Thread::current(); 981 assert(java_thread->is_handshake_safe_for(current_thread), 982 "call by myself or at handshake"); 983 984 if (JvmtiEnvBase::is_cthread_with_continuation(java_thread)) { 985 // Carrier thread with a mounted continuation case. 986 // No contended monitor can be owned by carrier thread in this case. 987 return JVMTI_ERROR_NONE; 988 } 989 if (java_thread->has_last_Java_frame()) { 990 ResourceMark rm(current_thread); 991 HandleMark hm(current_thread); 992 RegisterMap reg_map(java_thread, 993 RegisterMap::UpdateMap::include, 994 RegisterMap::ProcessFrames::include, 995 RegisterMap::WalkContinuation::skip); 996 997 int depth = 0; 998 for (javaVFrame *jvf = get_cthread_last_java_vframe(java_thread, ®_map); 999 jvf != nullptr; jvf = jvf->java_sender()) { 1000 if (MaxJavaStackTraceDepth == 0 || depth++ < MaxJavaStackTraceDepth) { // check for stack too deep 1001 // add locked objects for this frame into list 1002 err = get_locked_objects_in_frame(calling_thread, java_thread, jvf, owned_monitors_list, depth-1); 1003 if (err != JVMTI_ERROR_NONE) { 1004 return err; 1005 } 1006 } 1007 } 1008 } 1009 1010 // Get off stack monitors. (e.g. acquired via jni MonitorEnter). 1011 JvmtiMonitorClosure jmc(calling_thread, owned_monitors_list, this); 1012 ObjectSynchronizer::owned_monitors_iterate(&jmc, java_thread); 1013 err = jmc.error(); 1014 1015 return err; 1016 } 1017 1018 jvmtiError 1019 JvmtiEnvBase::get_owned_monitors(JavaThread* calling_thread, JavaThread* carrier, javaVFrame* jvf, 1020 GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list, oop vthread) { 1021 jvmtiError err = JVMTI_ERROR_NONE; 1022 Thread *current_thread = Thread::current(); 1023 assert(carrier == nullptr || carrier->is_handshake_safe_for(current_thread), 1024 "call by myself or at handshake"); 1025 1026 int depth = 0; 1027 for ( ; jvf != nullptr; jvf = jvf->java_sender()) { 1028 if (MaxJavaStackTraceDepth == 0 || depth++ < MaxJavaStackTraceDepth) { // check for stack too deep 1029 // Add locked objects for this frame into list. 1030 err = get_locked_objects_in_frame(calling_thread, carrier, jvf, owned_monitors_list, depth - 1, vthread); 1031 if (err != JVMTI_ERROR_NONE) { 1032 return err; 1033 } 1034 } 1035 } 1036 1037 // Get off stack monitors. (e.g. acquired via jni MonitorEnter). 1038 JvmtiMonitorClosure jmc(calling_thread, owned_monitors_list, this); 1039 ObjectSynchronizer::owned_monitors_iterate(&jmc, carrier != nullptr ? carrier->threadObj() : vthread); 1040 err = jmc.error(); 1041 1042 return err; 1043 } 1044 1045 // Save JNI local handles for any objects that this frame owns. 1046 jvmtiError 1047 JvmtiEnvBase::get_locked_objects_in_frame(JavaThread* calling_thread, JavaThread* target, 1048 javaVFrame *jvf, GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitors_list, 1049 jint stack_depth, oop vthread) { 1050 jvmtiError err = JVMTI_ERROR_NONE; 1051 Thread* current_thread = Thread::current(); 1052 ResourceMark rm(current_thread); 1053 HandleMark hm(current_thread); 1054 1055 GrowableArray<MonitorInfo*>* mons = jvf->monitors(); 1056 if (mons->is_empty()) { 1057 return err; // this javaVFrame holds no monitors 1058 } 1059 1060 oop wait_obj = nullptr; 1061 { 1062 // The ObjectMonitor* can't be async deflated since we are either 1063 // at a safepoint or the calling thread is operating on itself so 1064 // it cannot leave the underlying wait() call. 1065 // Save object of current wait() call (if any) for later comparison. 1066 if (target != nullptr) { 1067 ObjectMonitor *mon = target->current_waiting_monitor(); 1068 if (mon != nullptr) wait_obj = mon->object(); 1069 } 1070 } 1071 oop pending_obj = nullptr; 1072 { 1073 // The ObjectMonitor* can't be async deflated since we are either 1074 // at a safepoint or the calling thread is operating on itself so 1075 // it cannot leave the underlying enter() call. 1076 // Save object of current enter() call (if any) for later comparison. 1077 if (target != nullptr) { 1078 ObjectMonitor *mon = target->current_pending_monitor(); 1079 if (mon != nullptr) pending_obj = mon->object(); 1080 } else { 1081 assert(vthread != nullptr, "no vthread oop"); 1082 oop oopCont = java_lang_VirtualThread::continuation(vthread); 1083 assert(oopCont != nullptr, "vthread with no continuation"); 1084 stackChunkOop chunk = jdk_internal_vm_Continuation::tail(oopCont); 1085 assert(chunk != nullptr, "unmounted vthread should have a chunk"); 1086 ObjectMonitor *mon = chunk->current_pending_monitor(); 1087 if (mon != nullptr) pending_obj = mon->object(); 1088 } 1089 } 1090 1091 for (int i = 0; i < mons->length(); i++) { 1092 MonitorInfo *mi = mons->at(i); 1093 1094 if (mi->owner_is_scalar_replaced()) continue; 1095 1096 oop obj = mi->owner(); 1097 if (obj == nullptr) { 1098 // this monitor doesn't have an owning object so skip it 1099 continue; 1100 } 1101 1102 if (wait_obj == obj) { 1103 // the thread is waiting on this monitor so it isn't really owned 1104 continue; 1105 } 1106 1107 if (pending_obj == obj) { 1108 // the thread is pending on this monitor so it isn't really owned 1109 continue; 1110 } 1111 1112 if (owned_monitors_list->length() > 0) { 1113 // Our list has at least one object on it so we have to check 1114 // for recursive object locking 1115 bool found = false; 1116 for (int j = 0; j < owned_monitors_list->length(); j++) { 1117 jobject jobj = ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(j))->monitor; 1118 oop check = JNIHandles::resolve(jobj); 1119 if (check == obj) { 1120 found = true; // we found the object 1121 break; 1122 } 1123 } 1124 1125 if (found) { 1126 // already have this object so don't include it 1127 continue; 1128 } 1129 } 1130 1131 // add the owning object to our list 1132 jvmtiMonitorStackDepthInfo *jmsdi; 1133 err = allocate(sizeof(jvmtiMonitorStackDepthInfo), (unsigned char **)&jmsdi); 1134 if (err != JVMTI_ERROR_NONE) { 1135 return err; 1136 } 1137 Handle hobj(Thread::current(), obj); 1138 jmsdi->monitor = jni_reference(calling_thread, hobj); 1139 jmsdi->stack_depth = stack_depth; 1140 owned_monitors_list->append(jmsdi); 1141 } 1142 1143 return err; 1144 } 1145 1146 jvmtiError 1147 JvmtiEnvBase::get_stack_trace(javaVFrame *jvf, 1148 jint start_depth, jint max_count, 1149 jvmtiFrameInfo* frame_buffer, jint* count_ptr) { 1150 Thread *current_thread = Thread::current(); 1151 ResourceMark rm(current_thread); 1152 HandleMark hm(current_thread); 1153 int count = 0; 1154 1155 if (start_depth != 0) { 1156 if (start_depth > 0) { 1157 for (int j = 0; j < start_depth && jvf != nullptr; j++) { 1158 jvf = jvf->java_sender(); 1159 } 1160 if (jvf == nullptr) { 1161 // start_depth is deeper than the stack depth. 1162 return JVMTI_ERROR_ILLEGAL_ARGUMENT; 1163 } 1164 } else { // start_depth < 0 1165 // We are referencing the starting depth based on the oldest 1166 // part of the stack. 1167 // Optimize to limit the number of times that java_sender() is called. 1168 javaVFrame *jvf_cursor = jvf; 1169 javaVFrame *jvf_prev = nullptr; 1170 javaVFrame *jvf_prev_prev = nullptr; 1171 int j = 0; 1172 while (jvf_cursor != nullptr) { 1173 jvf_prev_prev = jvf_prev; 1174 jvf_prev = jvf_cursor; 1175 for (j = 0; j > start_depth && jvf_cursor != nullptr; j--) { 1176 jvf_cursor = jvf_cursor->java_sender(); 1177 } 1178 } 1179 if (j == start_depth) { 1180 // Previous pointer is exactly where we want to start. 1181 jvf = jvf_prev; 1182 } else { 1183 // We need to back up further to get to the right place. 1184 if (jvf_prev_prev == nullptr) { 1185 // The -start_depth is greater than the stack depth. 1186 return JVMTI_ERROR_ILLEGAL_ARGUMENT; 1187 } 1188 // j is now the number of frames on the stack starting with 1189 // jvf_prev, we start from jvf_prev_prev and move older on 1190 // the stack that many, and the result is -start_depth frames 1191 // remaining. 1192 jvf = jvf_prev_prev; 1193 for (; j < 0; j++) { 1194 jvf = jvf->java_sender(); 1195 } 1196 } 1197 } 1198 } 1199 for (; count < max_count && jvf != nullptr; count++) { 1200 frame_buffer[count].method = jvf->method()->jmethod_id(); 1201 frame_buffer[count].location = (jvf->method()->is_native() ? -1 : jvf->bci()); 1202 jvf = jvf->java_sender(); 1203 } 1204 *count_ptr = count; 1205 return JVMTI_ERROR_NONE; 1206 } 1207 1208 jvmtiError 1209 JvmtiEnvBase::get_stack_trace(JavaThread *java_thread, 1210 jint start_depth, jint max_count, 1211 jvmtiFrameInfo* frame_buffer, jint* count_ptr) { 1212 Thread *current_thread = Thread::current(); 1213 assert(SafepointSynchronize::is_at_safepoint() || 1214 java_thread->is_handshake_safe_for(current_thread), 1215 "call by myself / at safepoint / at handshake"); 1216 int count = 0; 1217 jvmtiError err = JVMTI_ERROR_NONE; 1218 1219 if (java_thread->has_last_Java_frame()) { 1220 RegisterMap reg_map(java_thread, 1221 RegisterMap::UpdateMap::include, 1222 RegisterMap::ProcessFrames::skip, 1223 RegisterMap::WalkContinuation::skip); 1224 ResourceMark rm(current_thread); 1225 javaVFrame *jvf = get_cthread_last_java_vframe(java_thread, ®_map); 1226 1227 err = get_stack_trace(jvf, start_depth, max_count, frame_buffer, count_ptr); 1228 } else { 1229 *count_ptr = 0; 1230 if (start_depth != 0) { 1231 // no frames and there is a starting depth 1232 err = JVMTI_ERROR_ILLEGAL_ARGUMENT; 1233 } 1234 } 1235 return err; 1236 } 1237 1238 jint 1239 JvmtiEnvBase::get_frame_count(javaVFrame *jvf) { 1240 int count = 0; 1241 1242 while (jvf != nullptr) { 1243 jvf = jvf->java_sender(); 1244 count++; 1245 } 1246 return count; 1247 } 1248 1249 jvmtiError 1250 JvmtiEnvBase::get_frame_count(JavaThread* jt, jint *count_ptr) { 1251 Thread *current_thread = Thread::current(); 1252 assert(current_thread == jt || 1253 SafepointSynchronize::is_at_safepoint() || 1254 jt->is_handshake_safe_for(current_thread), 1255 "call by myself / at safepoint / at handshake"); 1256 1257 if (!jt->has_last_Java_frame()) { // no Java frames 1258 *count_ptr = 0; 1259 } else { 1260 ResourceMark rm(current_thread); 1261 RegisterMap reg_map(jt, 1262 RegisterMap::UpdateMap::include, 1263 RegisterMap::ProcessFrames::include, 1264 RegisterMap::WalkContinuation::skip); 1265 javaVFrame *jvf = get_cthread_last_java_vframe(jt, ®_map); 1266 1267 *count_ptr = get_frame_count(jvf); 1268 } 1269 return JVMTI_ERROR_NONE; 1270 } 1271 1272 jvmtiError 1273 JvmtiEnvBase::get_frame_count(oop vthread_oop, jint *count_ptr) { 1274 Thread *current_thread = Thread::current(); 1275 ResourceMark rm(current_thread); 1276 javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(vthread_oop); 1277 1278 *count_ptr = get_frame_count(jvf); 1279 return JVMTI_ERROR_NONE; 1280 } 1281 1282 jvmtiError 1283 JvmtiEnvBase::get_frame_location(javaVFrame* jvf, jint depth, 1284 jmethodID* method_ptr, jlocation* location_ptr) { 1285 int cur_depth = 0; 1286 1287 while (jvf != nullptr && cur_depth < depth) { 1288 jvf = jvf->java_sender(); 1289 cur_depth++; 1290 } 1291 assert(depth >= cur_depth, "ran out of frames too soon"); 1292 if (jvf == nullptr) { 1293 return JVMTI_ERROR_NO_MORE_FRAMES; 1294 } 1295 Method* method = jvf->method(); 1296 if (method->is_native()) { 1297 *location_ptr = -1; 1298 } else { 1299 *location_ptr = jvf->bci(); 1300 } 1301 *method_ptr = method->jmethod_id(); 1302 return JVMTI_ERROR_NONE; 1303 } 1304 1305 jvmtiError 1306 JvmtiEnvBase::get_frame_location(JavaThread *java_thread, jint depth, 1307 jmethodID* method_ptr, jlocation* location_ptr) { 1308 Thread* current = Thread::current(); 1309 assert(java_thread->is_handshake_safe_for(current), 1310 "call by myself or at handshake"); 1311 if (!java_thread->has_last_Java_frame()) { 1312 return JVMTI_ERROR_NO_MORE_FRAMES; 1313 } 1314 ResourceMark rm(current); 1315 HandleMark hm(current); 1316 RegisterMap reg_map(java_thread, 1317 RegisterMap::UpdateMap::include, 1318 RegisterMap::ProcessFrames::skip, 1319 RegisterMap::WalkContinuation::include); 1320 javaVFrame* jvf = JvmtiEnvBase::get_cthread_last_java_vframe(java_thread, ®_map); 1321 1322 return get_frame_location(jvf, depth, method_ptr, location_ptr); 1323 } 1324 1325 jvmtiError 1326 JvmtiEnvBase::get_frame_location(oop vthread_oop, jint depth, 1327 jmethodID* method_ptr, jlocation* location_ptr) { 1328 Thread* current = Thread::current(); 1329 ResourceMark rm(current); 1330 HandleMark hm(current); 1331 javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(vthread_oop); 1332 1333 return get_frame_location(jvf, depth, method_ptr, location_ptr); 1334 } 1335 1336 jvmtiError 1337 JvmtiEnvBase::set_frame_pop(JvmtiThreadState* state, javaVFrame* jvf, jint depth) { 1338 for (int d = 0; jvf != nullptr && d < depth; d++) { 1339 jvf = jvf->java_sender(); 1340 } 1341 if (jvf == nullptr) { 1342 return JVMTI_ERROR_NO_MORE_FRAMES; 1343 } 1344 if (jvf->method()->is_native()) { 1345 return JVMTI_ERROR_OPAQUE_FRAME; 1346 } 1347 assert(jvf->frame_pointer() != nullptr, "frame pointer mustn't be null"); 1348 int frame_number = (int)get_frame_count(jvf); 1349 state->env_thread_state((JvmtiEnvBase*)this)->set_frame_pop(frame_number); 1350 return JVMTI_ERROR_NONE; 1351 } 1352 1353 bool 1354 JvmtiEnvBase::is_cthread_with_mounted_vthread(JavaThread* jt) { 1355 oop thread_oop = jt->threadObj(); 1356 assert(thread_oop != nullptr, "sanity check"); 1357 oop mounted_vt = jt->jvmti_vthread(); 1358 1359 return mounted_vt != nullptr && mounted_vt != thread_oop; 1360 } 1361 1362 bool 1363 JvmtiEnvBase::is_cthread_with_continuation(JavaThread* jt) { 1364 const ContinuationEntry* cont_entry = nullptr; 1365 if (jt->has_last_Java_frame()) { 1366 cont_entry = jt->vthread_continuation(); 1367 } 1368 return cont_entry != nullptr && is_cthread_with_mounted_vthread(jt); 1369 } 1370 1371 // Check if VirtualThread or BoundVirtualThread is suspended. 1372 bool 1373 JvmtiEnvBase::is_vthread_suspended(oop vt_oop, JavaThread* jt) { 1374 bool suspended = false; 1375 if (java_lang_VirtualThread::is_instance(vt_oop)) { 1376 suspended = JvmtiVTSuspender::is_vthread_suspended(vt_oop); 1377 } 1378 if (vt_oop->is_a(vmClasses::BoundVirtualThread_klass())) { 1379 suspended = jt->is_suspended(); 1380 } 1381 return suspended; 1382 } 1383 1384 // If (thread == null) then return current thread object. 1385 // Otherwise return JNIHandles::resolve_external_guard(thread). 1386 oop 1387 JvmtiEnvBase::current_thread_obj_or_resolve_external_guard(jthread thread) { 1388 oop thread_obj = JNIHandles::resolve_external_guard(thread); 1389 if (thread == nullptr) { 1390 thread_obj = get_vthread_or_thread_oop(JavaThread::current()); 1391 } 1392 return thread_obj; 1393 } 1394 1395 jvmtiError 1396 JvmtiEnvBase::get_threadOop_and_JavaThread(ThreadsList* t_list, jthread thread, JavaThread* cur_thread, 1397 JavaThread** jt_pp, oop* thread_oop_p) { 1398 JavaThread* java_thread = nullptr; 1399 oop thread_oop = nullptr; 1400 1401 if (thread == nullptr) { 1402 if (cur_thread == nullptr) { // cur_thread can be null when called from a VM_op 1403 return JVMTI_ERROR_INVALID_THREAD; 1404 } 1405 java_thread = cur_thread; 1406 thread_oop = get_vthread_or_thread_oop(java_thread); 1407 if (thread_oop == nullptr || !thread_oop->is_a(vmClasses::Thread_klass())) { 1408 return JVMTI_ERROR_INVALID_THREAD; 1409 } 1410 } else { 1411 jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(t_list, thread, &java_thread, &thread_oop); 1412 if (err != JVMTI_ERROR_NONE) { 1413 // We got an error code so we don't have a JavaThread*, but only return 1414 // an error from here if we didn't get a valid thread_oop. In a vthread case 1415 // the cv_external_thread_to_JavaThread is expected to correctly set the 1416 // thread_oop and return JVMTI_ERROR_INVALID_THREAD which we ignore here. 1417 if (thread_oop == nullptr || err != JVMTI_ERROR_INVALID_THREAD) { 1418 *thread_oop_p = thread_oop; 1419 return err; 1420 } 1421 } 1422 if (java_thread == nullptr && java_lang_VirtualThread::is_instance(thread_oop)) { 1423 java_thread = get_JavaThread_or_null(thread_oop); 1424 } 1425 } 1426 *jt_pp = java_thread; 1427 *thread_oop_p = thread_oop; 1428 if (java_lang_VirtualThread::is_instance(thread_oop) && 1429 !JvmtiEnvBase::is_vthread_alive(thread_oop)) { 1430 return JVMTI_ERROR_THREAD_NOT_ALIVE; 1431 } 1432 return JVMTI_ERROR_NONE; 1433 } 1434 1435 // Check for JVMTI_ERROR_NOT_SUSPENDED and JVMTI_ERROR_OPAQUE_FRAME errors. 1436 // Used in PopFrame and ForceEarlyReturn implementations. 1437 jvmtiError 1438 JvmtiEnvBase::check_non_suspended_or_opaque_frame(JavaThread* jt, oop thr_obj, bool self) { 1439 bool is_virtual = thr_obj != nullptr && thr_obj->is_a(vmClasses::BaseVirtualThread_klass()); 1440 1441 if (is_virtual) { 1442 if (!is_JavaThread_current(jt, thr_obj)) { 1443 if (!is_vthread_suspended(thr_obj, jt)) { 1444 return JVMTI_ERROR_THREAD_NOT_SUSPENDED; 1445 } 1446 if (jt == nullptr) { // unmounted virtual thread 1447 return JVMTI_ERROR_OPAQUE_FRAME; 1448 } 1449 } 1450 } else { // platform thread 1451 if (!self && !jt->is_suspended() && 1452 !jt->is_carrier_thread_suspended()) { 1453 return JVMTI_ERROR_THREAD_NOT_SUSPENDED; 1454 } 1455 } 1456 return JVMTI_ERROR_NONE; 1457 } 1458 1459 jvmtiError 1460 JvmtiEnvBase::get_object_monitor_usage(JavaThread* calling_thread, jobject object, jvmtiMonitorUsage* info_ptr) { 1461 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 1462 Thread* current_thread = VMThread::vm_thread(); 1463 assert(current_thread == Thread::current(), "must be"); 1464 1465 HandleMark hm(current_thread); 1466 Handle hobj; 1467 1468 // Check arguments 1469 { 1470 oop mirror = JNIHandles::resolve_external_guard(object); 1471 NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT); 1472 NULL_CHECK(info_ptr, JVMTI_ERROR_NULL_POINTER); 1473 1474 hobj = Handle(current_thread, mirror); 1475 } 1476 1477 ThreadsListHandle tlh(current_thread); 1478 JavaThread *owning_thread = nullptr; 1479 jvmtiMonitorUsage ret = { 1480 nullptr, 0, 0, nullptr, 0, nullptr 1481 }; 1482 1483 uint32_t debug_bits = 0; 1484 // first derive the object's owner and entry_count (if any) 1485 owning_thread = ObjectSynchronizer::get_lock_owner(tlh.list(), hobj); 1486 if (owning_thread != nullptr) { 1487 oop thread_oop = get_vthread_or_thread_oop(owning_thread); 1488 bool is_virtual = thread_oop->is_a(vmClasses::BaseVirtualThread_klass()); 1489 if (is_virtual) { 1490 thread_oop = nullptr; 1491 } 1492 Handle th(current_thread, thread_oop); 1493 ret.owner = (jthread)jni_reference(calling_thread, th); 1494 1495 // The recursions field of a monitor does not reflect recursions 1496 // as lightweight locks before inflating the monitor are not included. 1497 // We have to count the number of recursive monitor entries the hard way. 1498 // We pass a handle to survive any GCs along the way. 1499 ret.entry_count = is_virtual ? 0 : count_locked_objects(owning_thread, hobj); 1500 } 1501 // implied else: entry_count == 0 1502 1503 jint nWant = 0, nWait = 0; 1504 markWord mark = hobj->mark(); 1505 ResourceMark rm(current_thread); 1506 GrowableArray<JavaThread*>* wantList = nullptr; 1507 1508 ObjectMonitor* mon = mark.has_monitor() 1509 ? ObjectSynchronizer::read_monitor(current_thread, hobj(), mark) 1510 : nullptr; 1511 1512 if (mon != nullptr) { 1513 // this object has a heavyweight monitor 1514 nWant = mon->contentions(); // # of threads contending for monitor entry, but not re-entry 1515 nWait = mon->waiters(); // # of threads waiting for notification, 1516 // or to re-enter monitor, in Object.wait() 1517 1518 // Get the actual set of threads trying to enter, or re-enter, the monitor. 1519 wantList = Threads::get_pending_threads(tlh.list(), nWant + nWait, (address)mon); 1520 nWant = wantList->length(); 1521 } else { 1522 // this object has a lightweight monitor 1523 } 1524 1525 jint skipped = 0; 1526 if (mon != nullptr) { 1527 // Robustness: the actual waiting list can be smaller. 1528 // The nWait count we got from the mon->waiters() may include the re-entering 1529 // the monitor threads after being notified. Here we are correcting the actual 1530 // number of the waiting threads by excluding those re-entering the monitor. 1531 nWait = 0; 1532 for (ObjectWaiter* waiter = mon->first_waiter(); 1533 waiter != nullptr && (nWait == 0 || waiter != mon->first_waiter()); 1534 waiter = mon->next_waiter(waiter)) { 1535 JavaThread *w = mon->thread_of_waiter(waiter); 1536 if (w == nullptr) { 1537 skipped++; 1538 } else { 1539 oop thread_oop = get_vthread_or_thread_oop(w); 1540 if (thread_oop->is_a(vmClasses::BaseVirtualThread_klass())) { 1541 skipped++; 1542 } 1543 } 1544 nWait++; 1545 } 1546 } 1547 ret.waiter_count = nWant; 1548 ret.notify_waiter_count = nWait - skipped; 1549 1550 // Allocate memory for heavyweight and lightweight monitor. 1551 jvmtiError err; 1552 err = allocate(ret.waiter_count * sizeof(jthread *), (unsigned char**)&ret.waiters); 1553 if (err != JVMTI_ERROR_NONE) { 1554 return err; 1555 } 1556 err = allocate(ret.notify_waiter_count * sizeof(jthread *), 1557 (unsigned char**)&ret.notify_waiters); 1558 if (err != JVMTI_ERROR_NONE) { 1559 deallocate((unsigned char*)ret.waiters); 1560 return err; 1561 } 1562 1563 // now derive the rest of the fields 1564 if (mon != nullptr) { 1565 // this object has a heavyweight monitor 1566 1567 // null out memory for robustness 1568 if (ret.waiters != nullptr) { 1569 memset(ret.waiters, 0, ret.waiter_count * sizeof(jthread *)); 1570 } 1571 if (ret.notify_waiters != nullptr) { 1572 memset(ret.notify_waiters, 0, ret.notify_waiter_count * sizeof(jthread *)); 1573 } 1574 1575 if (ret.waiter_count > 0) { // we have contending threads waiting to enter/re-enter the monitor 1576 // identify threads waiting to enter and re-enter the monitor 1577 // get_pending_threads returns only java thread so we do not need to 1578 // check for non java threads. 1579 for (int i = 0; i < nWant; i++) { 1580 JavaThread *pending_thread = wantList->at(i); 1581 Handle th(current_thread, get_vthread_or_thread_oop(pending_thread)); 1582 ret.waiters[i] = (jthread)jni_reference(calling_thread, th); 1583 } 1584 } 1585 if (ret.notify_waiter_count > 0) { // we have threads waiting to be notified in Object.wait() 1586 ObjectWaiter *waiter = mon->first_waiter(); 1587 jint skipped = 0; 1588 for (int i = 0; i < nWait; i++) { 1589 JavaThread *w = mon->thread_of_waiter(waiter); 1590 bool is_virtual; 1591 if (w == nullptr) { 1592 is_virtual = true; 1593 } else { 1594 oop thread_oop = get_vthread_or_thread_oop(w); 1595 is_virtual = thread_oop->is_a(vmClasses::BaseVirtualThread_klass()); 1596 } 1597 if (is_virtual) { 1598 skipped++; 1599 } else { 1600 // If the thread was found on the ObjectWaiter list, then 1601 // it has not been notified. 1602 Handle th(current_thread, w->threadObj()); 1603 ret.notify_waiters[i - skipped] = (jthread)jni_reference(calling_thread, th); 1604 } 1605 waiter = mon->next_waiter(waiter); 1606 } 1607 } 1608 } else { 1609 // this object has a lightweight monitor and we have nothing more 1610 // to do here because the defaults are just fine. 1611 } 1612 1613 // we don't update return parameter unless everything worked 1614 *info_ptr = ret; 1615 1616 return JVMTI_ERROR_NONE; 1617 } 1618 1619 jvmtiError 1620 JvmtiEnvBase::check_thread_list(jint count, const jthread* list) { 1621 if (list == nullptr && count != 0) { 1622 return JVMTI_ERROR_NULL_POINTER; 1623 } 1624 for (int i = 0; i < count; i++) { 1625 jthread thread = list[i]; 1626 oop thread_oop = JNIHandles::resolve_external_guard(thread); 1627 if (thread_oop == nullptr || !thread_oop->is_a(vmClasses::BaseVirtualThread_klass())) { 1628 return JVMTI_ERROR_INVALID_THREAD; 1629 } 1630 } 1631 return JVMTI_ERROR_NONE; 1632 } 1633 1634 bool 1635 JvmtiEnvBase::is_in_thread_list(jint count, const jthread* list, oop jt_oop) { 1636 for (int idx = 0; idx < count; idx++) { 1637 jthread thread = list[idx]; 1638 oop thread_oop = JNIHandles::resolve_external_guard(thread); 1639 if (thread_oop == jt_oop) { 1640 return true; 1641 } 1642 } 1643 return false; 1644 } 1645 1646 class VM_SetNotifyJvmtiEventsMode : public VM_Operation { 1647 private: 1648 bool _enable; 1649 1650 static void correct_jvmti_thread_state(JavaThread* jt) { 1651 oop ct_oop = jt->threadObj(); 1652 oop vt_oop = jt->vthread(); 1653 JvmtiThreadState* jt_state = jt->jvmti_thread_state(); 1654 JvmtiThreadState* ct_state = java_lang_Thread::jvmti_thread_state(jt->threadObj()); 1655 JvmtiThreadState* vt_state = vt_oop != nullptr ? java_lang_Thread::jvmti_thread_state(vt_oop) : nullptr; 1656 bool virt = vt_oop != nullptr && java_lang_VirtualThread::is_instance(vt_oop); 1657 1658 // Correct jt->jvmti_thread_state() and jt->jvmti_vthread(). 1659 // It was not maintained while notifyJvmti was disabled. 1660 if (virt) { 1661 jt->set_jvmti_thread_state(nullptr); // reset jt->jvmti_thread_state() 1662 jt->set_jvmti_vthread(vt_oop); // restore jt->jvmti_vthread() 1663 } else { 1664 jt->set_jvmti_thread_state(ct_state); // restore jt->jvmti_thread_state() 1665 jt->set_jvmti_vthread(ct_oop); // restore jt->jvmti_vthread() 1666 } 1667 } 1668 1669 // This function is called only if _enable == true. 1670 // Iterates over all JavaThread's, restores jt->jvmti_thread_state() and 1671 // jt->jvmti_vthread() for VTMS transition protocol. 1672 void correct_jvmti_thread_states() { 1673 for (JavaThread* jt : ThreadsListHandle()) { 1674 if (jt->is_in_VTMS_transition()) { 1675 jt->set_VTMS_transition_mark(true); 1676 continue; // no need in JvmtiThreadState correction below if in transition 1677 } 1678 correct_jvmti_thread_state(jt); 1679 } 1680 } 1681 1682 public: 1683 VMOp_Type type() const { return VMOp_SetNotifyJvmtiEventsMode; } 1684 bool allow_nested_vm_operations() const { return false; } 1685 VM_SetNotifyJvmtiEventsMode(bool enable) : _enable(enable) { 1686 } 1687 1688 void doit() { 1689 if (_enable) { 1690 correct_jvmti_thread_states(); 1691 } 1692 JvmtiVTMSTransitionDisabler::set_VTMS_notify_jvmti_events(_enable); 1693 } 1694 }; 1695 1696 // This function is to support agents loaded into running VM. 1697 // Must be called in thread-in-native mode. 1698 bool 1699 JvmtiEnvBase::enable_virtual_threads_notify_jvmti() { 1700 if (!Continuations::enabled()) { 1701 return false; 1702 } 1703 if (JvmtiVTMSTransitionDisabler::VTMS_notify_jvmti_events()) { 1704 return false; // already enabled 1705 } 1706 VM_SetNotifyJvmtiEventsMode op(true); 1707 VMThread::execute(&op); 1708 return true; 1709 } 1710 1711 // This function is used in WhiteBox, only needed to test the function above. 1712 // It is unsafe to use this function when virtual threads are executed. 1713 // Must be called in thread-in-native mode. 1714 bool 1715 JvmtiEnvBase::disable_virtual_threads_notify_jvmti() { 1716 if (!Continuations::enabled()) { 1717 return false; 1718 } 1719 if (!JvmtiVTMSTransitionDisabler::VTMS_notify_jvmti_events()) { 1720 return false; // already disabled 1721 } 1722 JvmtiVTMSTransitionDisabler disabler(true); // ensure there are no other disablers 1723 VM_SetNotifyJvmtiEventsMode op(false); 1724 VMThread::execute(&op); 1725 return true; 1726 } 1727 1728 // java_thread - protected by ThreadsListHandle 1729 jvmtiError 1730 JvmtiEnvBase::suspend_thread(oop thread_oop, JavaThread* java_thread, bool single_suspend, 1731 int* need_safepoint_p) { 1732 JavaThread* current = JavaThread::current(); 1733 HandleMark hm(current); 1734 Handle thread_h(current, thread_oop); 1735 bool is_virtual = java_lang_VirtualThread::is_instance(thread_h()); 1736 1737 if (is_virtual) { 1738 if (single_suspend) { 1739 if (JvmtiVTSuspender::is_vthread_suspended(thread_h())) { 1740 return JVMTI_ERROR_THREAD_SUSPENDED; 1741 } 1742 JvmtiVTSuspender::register_vthread_suspend(thread_h()); 1743 // Check if virtual thread is mounted and there is a java_thread. 1744 // A non-null java_thread is always passed in the !single_suspend case. 1745 oop carrier_thread = java_lang_VirtualThread::carrier_thread(thread_h()); 1746 java_thread = carrier_thread == nullptr ? nullptr : java_lang_Thread::thread(carrier_thread); 1747 } 1748 // The java_thread can be still blocked in VTMS transition after a previous JVMTI resume call. 1749 // There is no need to suspend the java_thread in this case. After vthread unblocking, 1750 // it will check for ext_suspend request and suspend itself if necessary. 1751 if (java_thread == nullptr || java_thread->is_suspended()) { 1752 // We are done if the virtual thread is unmounted or 1753 // the java_thread is externally suspended. 1754 return JVMTI_ERROR_NONE; 1755 } 1756 // The virtual thread is mounted: suspend the java_thread. 1757 } 1758 // Don't allow hidden thread suspend request. 1759 if (java_thread->is_hidden_from_external_view()) { 1760 return JVMTI_ERROR_NONE; 1761 } 1762 bool is_thread_carrying = is_thread_carrying_vthread(java_thread, thread_h()); 1763 1764 // A case of non-virtual thread. 1765 if (!is_virtual) { 1766 // Thread.suspend() is used in some tests. It sets jt->is_suspended() only. 1767 if (java_thread->is_carrier_thread_suspended() || 1768 (!is_thread_carrying && java_thread->is_suspended())) { 1769 return JVMTI_ERROR_THREAD_SUSPENDED; 1770 } 1771 java_thread->set_carrier_thread_suspended(); 1772 } 1773 assert(!java_thread->is_in_VTMS_transition(), "sanity check"); 1774 1775 assert(!single_suspend || (!is_virtual && java_thread->is_carrier_thread_suspended()) || 1776 (is_virtual && JvmtiVTSuspender::is_vthread_suspended(thread_h())), 1777 "sanity check"); 1778 1779 // An attempt to handshake-suspend a thread carrying a virtual thread will result in 1780 // suspension of mounted virtual thread. So, we just mark it as suspended 1781 // and it will be actually suspended at virtual thread unmount transition. 1782 if (!is_thread_carrying) { 1783 assert(thread_h() != nullptr, "sanity check"); 1784 assert(single_suspend || thread_h()->is_a(vmClasses::BaseVirtualThread_klass()), 1785 "SuspendAllVirtualThreads should never suspend non-virtual threads"); 1786 // Case of mounted virtual or attached carrier thread. 1787 if (!JvmtiSuspendControl::suspend(java_thread)) { 1788 // Thread is already suspended or in process of exiting. 1789 if (java_thread->is_exiting()) { 1790 // The thread was in the process of exiting. 1791 return JVMTI_ERROR_THREAD_NOT_ALIVE; 1792 } 1793 return JVMTI_ERROR_THREAD_SUSPENDED; 1794 } 1795 } 1796 return JVMTI_ERROR_NONE; 1797 } 1798 1799 // java_thread - protected by ThreadsListHandle 1800 jvmtiError 1801 JvmtiEnvBase::resume_thread(oop thread_oop, JavaThread* java_thread, bool single_resume) { 1802 JavaThread* current = JavaThread::current(); 1803 HandleMark hm(current); 1804 Handle thread_h(current, thread_oop); 1805 bool is_virtual = java_lang_VirtualThread::is_instance(thread_h()); 1806 1807 if (is_virtual) { 1808 if (single_resume) { 1809 if (!JvmtiVTSuspender::is_vthread_suspended(thread_h())) { 1810 return JVMTI_ERROR_THREAD_NOT_SUSPENDED; 1811 } 1812 JvmtiVTSuspender::register_vthread_resume(thread_h()); 1813 // Check if virtual thread is mounted and there is a java_thread. 1814 // A non-null java_thread is always passed in the !single_resume case. 1815 oop carrier_thread = java_lang_VirtualThread::carrier_thread(thread_h()); 1816 java_thread = carrier_thread == nullptr ? nullptr : java_lang_Thread::thread(carrier_thread); 1817 } 1818 // The java_thread can be still blocked in VTMS transition after a previous JVMTI suspend call. 1819 // There is no need to resume the java_thread in this case. After vthread unblocking, 1820 // it will check for is_vthread_suspended request and remain resumed if necessary. 1821 if (java_thread == nullptr || !java_thread->is_suspended()) { 1822 // We are done if the virtual thread is unmounted or 1823 // the java_thread is not externally suspended. 1824 return JVMTI_ERROR_NONE; 1825 } 1826 // The virtual thread is mounted and java_thread is supended: resume the java_thread. 1827 } 1828 // Don't allow hidden thread resume request. 1829 if (java_thread->is_hidden_from_external_view()) { 1830 return JVMTI_ERROR_NONE; 1831 } 1832 bool is_thread_carrying = is_thread_carrying_vthread(java_thread, thread_h()); 1833 1834 // A case of a non-virtual thread. 1835 if (!is_virtual) { 1836 if (!java_thread->is_carrier_thread_suspended() && 1837 (is_thread_carrying || !java_thread->is_suspended())) { 1838 return JVMTI_ERROR_THREAD_NOT_SUSPENDED; 1839 } 1840 java_thread->clear_carrier_thread_suspended(); 1841 } 1842 assert(!java_thread->is_in_VTMS_transition(), "sanity check"); 1843 1844 if (!is_thread_carrying) { 1845 assert(thread_h() != nullptr, "sanity check"); 1846 assert(single_resume || thread_h()->is_a(vmClasses::BaseVirtualThread_klass()), 1847 "ResumeAllVirtualThreads should never resume non-virtual threads"); 1848 if (java_thread->is_suspended()) { 1849 if (!JvmtiSuspendControl::resume(java_thread)) { 1850 return JVMTI_ERROR_THREAD_NOT_SUSPENDED; 1851 } 1852 } 1853 } 1854 return JVMTI_ERROR_NONE; 1855 } 1856 1857 ResourceTracker::ResourceTracker(JvmtiEnv* env) { 1858 _env = env; 1859 _allocations = new (mtServiceability) GrowableArray<unsigned char*>(20, mtServiceability); 1860 _failed = false; 1861 } 1862 ResourceTracker::~ResourceTracker() { 1863 if (_failed) { 1864 for (int i=0; i<_allocations->length(); i++) { 1865 _env->deallocate(_allocations->at(i)); 1866 } 1867 } 1868 delete _allocations; 1869 } 1870 1871 jvmtiError ResourceTracker::allocate(jlong size, unsigned char** mem_ptr) { 1872 unsigned char *ptr; 1873 jvmtiError err = _env->allocate(size, &ptr); 1874 if (err == JVMTI_ERROR_NONE) { 1875 _allocations->append(ptr); 1876 *mem_ptr = ptr; 1877 } else { 1878 *mem_ptr = nullptr; 1879 _failed = true; 1880 } 1881 return err; 1882 } 1883 1884 unsigned char* ResourceTracker::allocate(jlong size) { 1885 unsigned char* ptr; 1886 allocate(size, &ptr); 1887 return ptr; 1888 } 1889 1890 char* ResourceTracker::strdup(const char* str) { 1891 char *dup_str = (char*)allocate(strlen(str)+1); 1892 if (dup_str != nullptr) { 1893 strcpy(dup_str, str); 1894 } 1895 return dup_str; 1896 } 1897 1898 struct StackInfoNode { 1899 struct StackInfoNode *next; 1900 jvmtiStackInfo info; 1901 }; 1902 1903 // Create a jvmtiStackInfo inside a linked list node and create a 1904 // buffer for the frame information, both allocated as resource objects. 1905 // Fill in both the jvmtiStackInfo and the jvmtiFrameInfo. 1906 // Note that either or both of thr and thread_oop 1907 // may be null if the thread is new or has exited. 1908 void 1909 MultipleStackTracesCollector::fill_frames(jthread jt, JavaThread *thr, oop thread_oop) { 1910 #ifdef ASSERT 1911 Thread *current_thread = Thread::current(); 1912 assert(SafepointSynchronize::is_at_safepoint() || 1913 thr == nullptr || 1914 thr->is_handshake_safe_for(current_thread), 1915 "unmounted virtual thread / call by myself / at safepoint / at handshake"); 1916 #endif 1917 1918 jint state = 0; 1919 struct StackInfoNode *node = NEW_RESOURCE_OBJ(struct StackInfoNode); 1920 jvmtiStackInfo *infop = &(node->info); 1921 1922 node->next = head(); 1923 set_head(node); 1924 infop->frame_count = 0; 1925 infop->frame_buffer = nullptr; 1926 infop->thread = jt; 1927 1928 if (java_lang_VirtualThread::is_instance(thread_oop)) { 1929 state = JvmtiEnvBase::get_vthread_state(thread_oop, thr); 1930 1931 if ((state & JVMTI_THREAD_STATE_ALIVE) != 0) { 1932 javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(thread_oop); 1933 infop->frame_buffer = NEW_RESOURCE_ARRAY(jvmtiFrameInfo, max_frame_count()); 1934 _result = env()->get_stack_trace(jvf, 0, max_frame_count(), 1935 infop->frame_buffer, &(infop->frame_count)); 1936 } 1937 } else { 1938 state = JvmtiEnvBase::get_thread_state(thread_oop, thr); 1939 if (thr != nullptr && (state & JVMTI_THREAD_STATE_ALIVE) != 0) { 1940 infop->frame_buffer = NEW_RESOURCE_ARRAY(jvmtiFrameInfo, max_frame_count()); 1941 _result = env()->get_stack_trace(thr, 0, max_frame_count(), 1942 infop->frame_buffer, &(infop->frame_count)); 1943 } 1944 } 1945 _frame_count_total += infop->frame_count; 1946 infop->state = state; 1947 } 1948 1949 // Based on the stack information in the linked list, allocate memory 1950 // block to return and fill it from the info in the linked list. 1951 void 1952 MultipleStackTracesCollector::allocate_and_fill_stacks(jint thread_count) { 1953 // do I need to worry about alignment issues? 1954 jlong alloc_size = thread_count * sizeof(jvmtiStackInfo) 1955 + _frame_count_total * sizeof(jvmtiFrameInfo); 1956 env()->allocate(alloc_size, (unsigned char **)&_stack_info); 1957 1958 // pointers to move through the newly allocated space as it is filled in 1959 jvmtiStackInfo *si = _stack_info + thread_count; // bottom of stack info 1960 jvmtiFrameInfo *fi = (jvmtiFrameInfo *)si; // is the top of frame info 1961 1962 // copy information in resource area into allocated buffer 1963 // insert stack info backwards since linked list is backwards 1964 // insert frame info forwards 1965 // walk the StackInfoNodes 1966 for (struct StackInfoNode *sin = head(); sin != nullptr; sin = sin->next) { 1967 jint frame_count = sin->info.frame_count; 1968 size_t frames_size = frame_count * sizeof(jvmtiFrameInfo); 1969 --si; 1970 memcpy(si, &(sin->info), sizeof(jvmtiStackInfo)); 1971 if (frames_size == 0) { 1972 si->frame_buffer = nullptr; 1973 } else { 1974 memcpy(fi, sin->info.frame_buffer, frames_size); 1975 si->frame_buffer = fi; // point to the new allocated copy of the frames 1976 fi += frame_count; 1977 } 1978 } 1979 assert(si == _stack_info, "the last copied stack info must be the first record"); 1980 assert((unsigned char *)fi == ((unsigned char *)_stack_info) + alloc_size, 1981 "the last copied frame info must be the last record"); 1982 } 1983 1984 // AdapterClosure is to make use of JvmtiUnitedHandshakeClosure objects from 1985 // Handshake::execute() which is unaware of the do_vthread() member functions. 1986 class AdapterClosure : public HandshakeClosure { 1987 JvmtiUnitedHandshakeClosure* _hs_cl; 1988 Handle _target_h; 1989 1990 public: 1991 AdapterClosure(JvmtiUnitedHandshakeClosure* hs_cl, Handle target_h) 1992 : HandshakeClosure(hs_cl->name()), _hs_cl(hs_cl), _target_h(target_h) {} 1993 1994 virtual void do_thread(Thread* target) { 1995 if (java_lang_VirtualThread::is_instance(_target_h())) { 1996 _hs_cl->do_vthread(_target_h); // virtual thread 1997 } else { 1998 _hs_cl->do_thread(target); // platform thread 1999 } 2000 } 2001 }; 2002 2003 // Supports platform and virtual threads. 2004 // JvmtiVTMSTransitionDisabler is always set by this function. 2005 void 2006 JvmtiHandshake::execute(JvmtiUnitedHandshakeClosure* hs_cl, jthread target) { 2007 JavaThread* current = JavaThread::current(); 2008 HandleMark hm(current); 2009 2010 JvmtiVTMSTransitionDisabler disabler(target); 2011 ThreadsListHandle tlh(current); 2012 JavaThread* java_thread = nullptr; 2013 oop thread_obj = nullptr; 2014 2015 jvmtiError err = JvmtiEnvBase::get_threadOop_and_JavaThread(tlh.list(), target, current, &java_thread, &thread_obj); 2016 if (err != JVMTI_ERROR_NONE) { 2017 hs_cl->set_result(err); 2018 return; 2019 } 2020 Handle target_h(current, thread_obj); 2021 execute(hs_cl, &tlh, java_thread, target_h); 2022 } 2023 2024 // Supports platform and virtual threads. 2025 // A virtual thread is always identified by the target_h oop handle. 2026 // The target_jt is always nullptr for an unmounted virtual thread. 2027 // JvmtiVTMSTransitionDisabler has to be set before call to this function. 2028 void 2029 JvmtiHandshake::execute(JvmtiUnitedHandshakeClosure* hs_cl, ThreadsListHandle* tlh, 2030 JavaThread* target_jt, Handle target_h) { 2031 JavaThread* current = JavaThread::current(); 2032 bool is_virtual = java_lang_VirtualThread::is_instance(target_h()); 2033 bool self = target_jt == current; 2034 2035 assert(!Continuations::enabled() || self || !is_virtual || current->is_VTMS_transition_disabler(), "sanity check"); 2036 2037 hs_cl->set_target_jt(target_jt); // can be needed in the virtual thread case 2038 hs_cl->set_is_virtual(is_virtual); // can be needed in the virtual thread case 2039 hs_cl->set_self(self); // needed when suspend is required for non-current target thread 2040 2041 if (is_virtual) { // virtual thread 2042 if (!JvmtiEnvBase::is_vthread_alive(target_h())) { 2043 return; 2044 } 2045 if (target_jt == nullptr) { // unmounted virtual thread 2046 hs_cl->do_vthread(target_h); // execute handshake closure callback on current thread directly 2047 } 2048 } 2049 if (target_jt != nullptr) { // mounted virtual or platform thread 2050 AdapterClosure acl(hs_cl, target_h); 2051 if (self) { // target platform thread is current 2052 acl.do_thread(target_jt); // execute handshake closure callback on current thread directly 2053 } else { 2054 Handshake::execute(&acl, tlh, target_jt); // delegate to Handshake implementation 2055 } 2056 } 2057 } 2058 2059 void 2060 VM_GetThreadListStackTraces::doit() { 2061 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 2062 2063 ResourceMark rm; 2064 ThreadsListHandle tlh; 2065 for (int i = 0; i < _thread_count; ++i) { 2066 jthread jt = _thread_list[i]; 2067 JavaThread* java_thread = nullptr; 2068 oop thread_oop = nullptr; 2069 jvmtiError err = JvmtiEnvBase::get_threadOop_and_JavaThread(tlh.list(), jt, nullptr, &java_thread, &thread_oop); 2070 2071 if (err != JVMTI_ERROR_NONE) { 2072 // We got an error code so we don't have a JavaThread *, but 2073 // only return an error from here if we didn't get a valid 2074 // thread_oop. 2075 // In the virtual thread case the get_threadOop_and_JavaThread is expected to correctly set 2076 // the thread_oop and return JVMTI_ERROR_THREAD_NOT_ALIVE which we ignore here. 2077 // The corresponding thread state will be recorded in the jvmtiStackInfo.state. 2078 if (thread_oop == nullptr) { 2079 _collector.set_result(err); 2080 return; 2081 } 2082 // We have a valid thread_oop. 2083 } 2084 _collector.fill_frames(jt, java_thread, thread_oop); 2085 } 2086 _collector.allocate_and_fill_stacks(_thread_count); 2087 } 2088 2089 void 2090 GetSingleStackTraceClosure::doit() { 2091 JavaThread *jt = _target_jt; 2092 oop thread_oop = JNIHandles::resolve_external_guard(_jthread); 2093 2094 if ((jt == nullptr || !jt->is_exiting()) && thread_oop != nullptr) { 2095 ResourceMark rm; 2096 _collector.fill_frames(_jthread, jt, thread_oop); 2097 _collector.allocate_and_fill_stacks(1); 2098 set_result(_collector.result()); 2099 } 2100 } 2101 2102 void 2103 GetSingleStackTraceClosure::do_thread(Thread *target) { 2104 assert(_target_jt == JavaThread::cast(target), "sanity check"); 2105 doit(); 2106 } 2107 2108 void 2109 GetSingleStackTraceClosure::do_vthread(Handle target_h) { 2110 // Use jvmti_vthread() instead of vthread() as target could have temporarily changed 2111 // identity to carrier thread (see VirtualThread.switchToCarrierThread). 2112 assert(_target_jt == nullptr || _target_jt->jvmti_vthread() == target_h(), "sanity check"); 2113 doit(); 2114 } 2115 2116 void 2117 VM_GetAllStackTraces::doit() { 2118 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 2119 2120 ResourceMark rm; 2121 _final_thread_count = 0; 2122 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) { 2123 oop thread_oop = jt->threadObj(); 2124 if (thread_oop != nullptr && 2125 !jt->is_exiting() && 2126 java_lang_Thread::is_alive(thread_oop) && 2127 !jt->is_hidden_from_external_view() && 2128 !thread_oop->is_a(vmClasses::BoundVirtualThread_klass())) { 2129 ++_final_thread_count; 2130 // Handle block of the calling thread is used to create local refs. 2131 _collector.fill_frames((jthread)JNIHandles::make_local(_calling_thread, thread_oop), 2132 jt, thread_oop); 2133 } 2134 } 2135 _collector.allocate_and_fill_stacks(_final_thread_count); 2136 } 2137 2138 // Verifies that the top frame is a java frame in an expected state. 2139 // Deoptimizes frame if needed. 2140 // Checks that the frame method signature matches the return type (tos). 2141 // HandleMark must be defined in the caller only. 2142 // It is to keep a ret_ob_h handle alive after return to the caller. 2143 jvmtiError 2144 JvmtiEnvBase::check_top_frame(Thread* current_thread, JavaThread* java_thread, 2145 jvalue value, TosState tos, Handle* ret_ob_h) { 2146 ResourceMark rm(current_thread); 2147 2148 javaVFrame* jvf = jvf_for_thread_and_depth(java_thread, 0); 2149 NULL_CHECK(jvf, JVMTI_ERROR_NO_MORE_FRAMES); 2150 2151 if (jvf->method()->is_native()) { 2152 return JVMTI_ERROR_OPAQUE_FRAME; 2153 } 2154 2155 // If the frame is a compiled one, need to deoptimize it. 2156 if (jvf->is_compiled_frame()) { 2157 if (!jvf->fr().can_be_deoptimized()) { 2158 return JVMTI_ERROR_OPAQUE_FRAME; 2159 } 2160 Deoptimization::deoptimize_frame(java_thread, jvf->fr().id()); 2161 } 2162 2163 // Get information about method return type 2164 Symbol* signature = jvf->method()->signature(); 2165 2166 ResultTypeFinder rtf(signature); 2167 TosState fr_tos = as_TosState(rtf.type()); 2168 if (fr_tos != tos) { 2169 if (tos != itos || (fr_tos != btos && fr_tos != ztos && fr_tos != ctos && fr_tos != stos)) { 2170 return JVMTI_ERROR_TYPE_MISMATCH; 2171 } 2172 } 2173 2174 // Check that the jobject class matches the return type signature. 2175 jobject jobj = value.l; 2176 if (tos == atos && jobj != nullptr) { // null reference is allowed 2177 Handle ob_h(current_thread, JNIHandles::resolve_external_guard(jobj)); 2178 NULL_CHECK(ob_h, JVMTI_ERROR_INVALID_OBJECT); 2179 Klass* ob_k = ob_h()->klass(); 2180 NULL_CHECK(ob_k, JVMTI_ERROR_INVALID_OBJECT); 2181 2182 // Method return type signature. 2183 char* ty_sign = 1 + strchr(signature->as_C_string(), JVM_SIGNATURE_ENDFUNC); 2184 2185 if (!VM_GetOrSetLocal::is_assignable(ty_sign, ob_k, current_thread)) { 2186 return JVMTI_ERROR_TYPE_MISMATCH; 2187 } 2188 *ret_ob_h = ob_h; 2189 } 2190 return JVMTI_ERROR_NONE; 2191 } /* end check_top_frame */ 2192 2193 2194 // ForceEarlyReturn<type> follows the PopFrame approach in many aspects. 2195 // Main difference is on the last stage in the interpreter. 2196 // The PopFrame stops method execution to continue execution 2197 // from the same method call instruction. 2198 // The ForceEarlyReturn forces return from method so the execution 2199 // continues at the bytecode following the method call. 2200 2201 // thread - NOT protected by ThreadsListHandle and NOT pre-checked 2202 2203 jvmtiError 2204 JvmtiEnvBase::force_early_return(jthread thread, jvalue value, TosState tos) { 2205 JavaThread* current_thread = JavaThread::current(); 2206 HandleMark hm(current_thread); 2207 2208 JvmtiVTMSTransitionDisabler disabler(thread); 2209 ThreadsListHandle tlh(current_thread); 2210 2211 JavaThread* java_thread = nullptr; 2212 oop thread_obj = nullptr; 2213 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj); 2214 2215 if (err != JVMTI_ERROR_NONE) { 2216 return err; 2217 } 2218 Handle thread_handle(current_thread, thread_obj); 2219 bool self = java_thread == current_thread; 2220 2221 err = check_non_suspended_or_opaque_frame(java_thread, thread_obj, self); 2222 if (err != JVMTI_ERROR_NONE) { 2223 return err; 2224 } 2225 2226 // retrieve or create the state 2227 JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread); 2228 if (state == nullptr) { 2229 return JVMTI_ERROR_THREAD_NOT_ALIVE; 2230 } 2231 2232 // Eagerly reallocate scalar replaced objects. 2233 EscapeBarrier eb(true, current_thread, java_thread); 2234 if (!eb.deoptimize_objects(0)) { 2235 // Reallocation of scalar replaced objects failed -> return with error 2236 return JVMTI_ERROR_OUT_OF_MEMORY; 2237 } 2238 2239 MutexLocker mu(JvmtiThreadState_lock); 2240 SetForceEarlyReturn op(state, value, tos); 2241 JvmtiHandshake::execute(&op, &tlh, java_thread, thread_handle); 2242 return op.result(); 2243 } 2244 2245 void 2246 SetForceEarlyReturn::doit(Thread *target) { 2247 JavaThread* java_thread = JavaThread::cast(target); 2248 Thread* current_thread = Thread::current(); 2249 HandleMark hm(current_thread); 2250 2251 if (java_thread->is_exiting()) { 2252 return; /* JVMTI_ERROR_THREAD_NOT_ALIVE (default) */ 2253 } 2254 2255 // Check to see if a ForceEarlyReturn was already in progress 2256 if (_state->is_earlyret_pending()) { 2257 // Probably possible for JVMTI clients to trigger this, but the 2258 // JPDA backend shouldn't allow this to happen 2259 _result = JVMTI_ERROR_INTERNAL; 2260 return; 2261 } 2262 { 2263 // The same as for PopFrame. Workaround bug: 2264 // 4812902: popFrame hangs if the method is waiting at a synchronize 2265 // Catch this condition and return an error to avoid hanging. 2266 // Now JVMTI spec allows an implementation to bail out with an opaque 2267 // frame error. 2268 OSThread* osThread = java_thread->osthread(); 2269 if (osThread->get_state() == MONITOR_WAIT) { 2270 _result = JVMTI_ERROR_OPAQUE_FRAME; 2271 return; 2272 } 2273 } 2274 2275 Handle ret_ob_h; 2276 _result = JvmtiEnvBase::check_top_frame(current_thread, java_thread, _value, _tos, &ret_ob_h); 2277 if (_result != JVMTI_ERROR_NONE) { 2278 return; 2279 } 2280 assert(_tos != atos || _value.l == nullptr || ret_ob_h() != nullptr, 2281 "return object oop must not be null if jobject is not null"); 2282 2283 // Update the thread state to reflect that the top frame must be 2284 // forced to return. 2285 // The current frame will be returned later when the suspended 2286 // thread is resumed and right before returning from VM to Java. 2287 // (see call_VM_base() in assembler_<cpu>.cpp). 2288 2289 _state->set_earlyret_pending(); 2290 _state->set_earlyret_oop(ret_ob_h()); 2291 _state->set_earlyret_value(_value, _tos); 2292 2293 // Set pending step flag for this early return. 2294 // It is cleared when next step event is posted. 2295 _state->set_pending_step_for_earlyret(); 2296 } 2297 2298 void 2299 JvmtiMonitorClosure::do_monitor(ObjectMonitor* mon) { 2300 if ( _error != JVMTI_ERROR_NONE) { 2301 // Error occurred in previous iteration so no need to add 2302 // to the list. 2303 return; 2304 } 2305 // Filter out on stack monitors collected during stack walk. 2306 oop obj = mon->object(); 2307 2308 if (obj == nullptr) { 2309 // This can happen if JNI code drops all references to the 2310 // owning object. 2311 return; 2312 } 2313 2314 bool found = false; 2315 for (int j = 0; j < _owned_monitors_list->length(); j++) { 2316 jobject jobj = ((jvmtiMonitorStackDepthInfo*)_owned_monitors_list->at(j))->monitor; 2317 oop check = JNIHandles::resolve(jobj); 2318 if (check == obj) { 2319 // On stack monitor already collected during the stack walk. 2320 found = true; 2321 break; 2322 } 2323 } 2324 if (found == false) { 2325 // This is off stack monitor (e.g. acquired via jni MonitorEnter). 2326 jvmtiError err; 2327 jvmtiMonitorStackDepthInfo *jmsdi; 2328 err = _env->allocate(sizeof(jvmtiMonitorStackDepthInfo), (unsigned char **)&jmsdi); 2329 if (err != JVMTI_ERROR_NONE) { 2330 _error = err; 2331 return; 2332 } 2333 Handle hobj(Thread::current(), obj); 2334 jmsdi->monitor = _env->jni_reference(_calling_thread, hobj); 2335 // stack depth is unknown for this monitor. 2336 jmsdi->stack_depth = -1; 2337 _owned_monitors_list->append(jmsdi); 2338 } 2339 } 2340 2341 GrowableArray<OopHandle>* JvmtiModuleClosure::_tbl = nullptr; 2342 2343 void JvmtiModuleClosure::do_module(ModuleEntry* entry) { 2344 assert_locked_or_safepoint(Module_lock); 2345 OopHandle module = entry->module_handle(); 2346 guarantee(module.resolve() != nullptr, "module object is null"); 2347 _tbl->push(module); 2348 } 2349 2350 jvmtiError 2351 JvmtiModuleClosure::get_all_modules(JvmtiEnv* env, jint* module_count_ptr, jobject** modules_ptr) { 2352 ResourceMark rm; 2353 MutexLocker mcld(ClassLoaderDataGraph_lock); 2354 MutexLocker ml(Module_lock); 2355 2356 _tbl = new GrowableArray<OopHandle>(77); 2357 if (_tbl == nullptr) { 2358 return JVMTI_ERROR_OUT_OF_MEMORY; 2359 } 2360 2361 // Iterate over all the modules loaded to the system. 2362 ClassLoaderDataGraph::modules_do_keepalive(&do_module); 2363 2364 jint len = _tbl->length(); 2365 guarantee(len > 0, "at least one module must be present"); 2366 2367 jobject* array = (jobject*)env->jvmtiMalloc((jlong)(len * sizeof(jobject))); 2368 if (array == nullptr) { 2369 return JVMTI_ERROR_OUT_OF_MEMORY; 2370 } 2371 for (jint idx = 0; idx < len; idx++) { 2372 array[idx] = JNIHandles::make_local(_tbl->at(idx).resolve()); 2373 } 2374 _tbl = nullptr; 2375 *modules_ptr = array; 2376 *module_count_ptr = len; 2377 return JVMTI_ERROR_NONE; 2378 } 2379 2380 void 2381 UpdateForPopTopFrameClosure::doit(Thread *target) { 2382 Thread* current_thread = Thread::current(); 2383 HandleMark hm(current_thread); 2384 JavaThread* java_thread = JavaThread::cast(target); 2385 2386 if (java_thread->is_exiting()) { 2387 return; /* JVMTI_ERROR_THREAD_NOT_ALIVE (default) */ 2388 } 2389 assert(java_thread == _state->get_thread(), "Must be"); 2390 2391 // Check to see if a PopFrame was already in progress 2392 if (java_thread->popframe_condition() != JavaThread::popframe_inactive) { 2393 // Probably possible for JVMTI clients to trigger this, but the 2394 // JPDA backend shouldn't allow this to happen 2395 _result = JVMTI_ERROR_INTERNAL; 2396 return; 2397 } 2398 2399 // Was workaround bug 2400 // 4812902: popFrame hangs if the method is waiting at a synchronize 2401 // Catch this condition and return an error to avoid hanging. 2402 // Now JVMTI spec allows an implementation to bail out with an opaque frame error. 2403 OSThread* osThread = java_thread->osthread(); 2404 if (osThread->get_state() == MONITOR_WAIT) { 2405 _result = JVMTI_ERROR_OPAQUE_FRAME; 2406 return; 2407 } 2408 2409 ResourceMark rm(current_thread); 2410 // Check if there is more than one Java frame in this thread, that the top two frames 2411 // are Java (not native) frames, and that there is no intervening VM frame 2412 int frame_count = 0; 2413 bool is_interpreted[2]; 2414 intptr_t *frame_sp[2]; 2415 // The 2-nd arg of constructor is needed to stop iterating at java entry frame. 2416 for (vframeStream vfs(java_thread, true, false /* process_frames */); !vfs.at_end(); vfs.next()) { 2417 methodHandle mh(current_thread, vfs.method()); 2418 if (mh->is_native()) { 2419 _result = JVMTI_ERROR_OPAQUE_FRAME; 2420 return; 2421 } 2422 is_interpreted[frame_count] = vfs.is_interpreted_frame(); 2423 frame_sp[frame_count] = vfs.frame_id(); 2424 if (++frame_count > 1) break; 2425 } 2426 if (frame_count < 2) { 2427 // We haven't found two adjacent non-native Java frames on the top. 2428 // There can be two situations here: 2429 // 1. There are no more java frames 2430 // 2. Two top java frames are separated by non-java native frames 2431 if (JvmtiEnvBase::jvf_for_thread_and_depth(java_thread, 1) == nullptr) { 2432 _result = JVMTI_ERROR_NO_MORE_FRAMES; 2433 return; 2434 } else { 2435 // Intervening non-java native or VM frames separate java frames. 2436 // Current implementation does not support this. See bug #5031735. 2437 // In theory it is possible to pop frames in such cases. 2438 _result = JVMTI_ERROR_OPAQUE_FRAME; 2439 return; 2440 } 2441 } 2442 2443 // If any of the top 2 frames is a compiled one, need to deoptimize it 2444 for (int i = 0; i < 2; i++) { 2445 if (!is_interpreted[i]) { 2446 Deoptimization::deoptimize_frame(java_thread, frame_sp[i]); 2447 } 2448 } 2449 2450 // Update the thread state to reflect that the top frame is popped 2451 // so that cur_stack_depth is maintained properly and all frameIDs 2452 // are invalidated. 2453 // The current frame will be popped later when the suspended thread 2454 // is resumed and right before returning from VM to Java. 2455 // (see call_VM_base() in assembler_<cpu>.cpp). 2456 2457 // It's fine to update the thread state here because no JVMTI events 2458 // shall be posted for this PopFrame. 2459 2460 _state->update_for_pop_top_frame(); 2461 java_thread->set_popframe_condition(JavaThread::popframe_pending_bit); 2462 // Set pending step flag for this popframe and it is cleared when next 2463 // step event is posted. 2464 _state->set_pending_step_for_popframe(); 2465 _result = JVMTI_ERROR_NONE; 2466 } 2467 2468 void 2469 SetFramePopClosure::do_thread(Thread *target) { 2470 Thread* current = Thread::current(); 2471 ResourceMark rm(current); // vframes are resource allocated 2472 JavaThread* java_thread = JavaThread::cast(target); 2473 2474 if (java_thread->is_exiting()) { 2475 return; // JVMTI_ERROR_THREAD_NOT_ALIVE (default) 2476 } 2477 2478 if (!_self && !java_thread->is_suspended()) { 2479 _result = JVMTI_ERROR_THREAD_NOT_SUSPENDED; 2480 return; 2481 } 2482 if (!java_thread->has_last_Java_frame()) { 2483 _result = JVMTI_ERROR_NO_MORE_FRAMES; 2484 return; 2485 } 2486 assert(_state->get_thread_or_saved() == java_thread, "Must be"); 2487 2488 RegisterMap reg_map(java_thread, 2489 RegisterMap::UpdateMap::include, 2490 RegisterMap::ProcessFrames::skip, 2491 RegisterMap::WalkContinuation::include); 2492 javaVFrame* jvf = JvmtiEnvBase::get_cthread_last_java_vframe(java_thread, ®_map); 2493 _result = ((JvmtiEnvBase*)_env)->set_frame_pop(_state, jvf, _depth); 2494 } 2495 2496 void 2497 SetFramePopClosure::do_vthread(Handle target_h) { 2498 Thread* current = Thread::current(); 2499 ResourceMark rm(current); // vframes are resource allocated 2500 2501 if (!_self && !JvmtiVTSuspender::is_vthread_suspended(target_h())) { 2502 _result = JVMTI_ERROR_THREAD_NOT_SUSPENDED; 2503 return; 2504 } 2505 javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(target_h()); 2506 _result = ((JvmtiEnvBase*)_env)->set_frame_pop(_state, jvf, _depth); 2507 } 2508 2509 void 2510 GetOwnedMonitorInfoClosure::do_thread(Thread *target) { 2511 JavaThread *jt = JavaThread::cast(target); 2512 if (!jt->is_exiting() && (jt->threadObj() != nullptr)) { 2513 _result = ((JvmtiEnvBase *)_env)->get_owned_monitors(_calling_thread, 2514 jt, 2515 _owned_monitors_list); 2516 } 2517 } 2518 2519 void 2520 GetOwnedMonitorInfoClosure::do_vthread(Handle target_h) { 2521 Thread* current = Thread::current(); 2522 ResourceMark rm(current); // vframes are resource allocated 2523 HandleMark hm(current); 2524 2525 javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(target_h()); 2526 2527 if (_target_jt == nullptr || (!_target_jt->is_exiting() && _target_jt->threadObj() != nullptr)) { 2528 _result = ((JvmtiEnvBase *)_env)->get_owned_monitors(_calling_thread, 2529 _target_jt, 2530 jvf, 2531 _owned_monitors_list, 2532 target_h()); 2533 } 2534 } 2535 2536 void 2537 GetCurrentContendedMonitorClosure::do_thread(Thread *target) { 2538 JavaThread *jt = JavaThread::cast(target); 2539 if (!jt->is_exiting() && (jt->threadObj() != nullptr)) { 2540 _result = ((JvmtiEnvBase *)_env)->get_current_contended_monitor(_calling_thread, 2541 jt, 2542 _owned_monitor_ptr, 2543 _is_virtual); 2544 } 2545 } 2546 2547 void 2548 GetCurrentContendedMonitorClosure::do_vthread(Handle target_h) { 2549 if (_target_jt == nullptr) { 2550 oop cont = java_lang_VirtualThread::continuation(target_h()); 2551 assert(cont != nullptr, "vthread with no continuation"); 2552 stackChunkOop chunk = jdk_internal_vm_Continuation::tail(cont); 2553 assert(chunk != nullptr, "unmounted vthread should have a chunk"); 2554 if (chunk->current_pending_monitor() != nullptr) { 2555 *_owned_monitor_ptr = JNIHandles::make_local(_calling_thread, chunk->current_pending_monitor()->object()); 2556 } 2557 _result = JVMTI_ERROR_NONE; // target virtual thread is unmounted 2558 return; 2559 } 2560 // mounted virtual thread case 2561 do_thread(_target_jt); 2562 } 2563 2564 void 2565 GetStackTraceClosure::do_thread(Thread *target) { 2566 Thread* current = Thread::current(); 2567 ResourceMark rm(current); 2568 2569 JavaThread *jt = JavaThread::cast(target); 2570 if (!jt->is_exiting() && jt->threadObj() != nullptr) { 2571 _result = ((JvmtiEnvBase *)_env)->get_stack_trace(jt, 2572 _start_depth, _max_count, 2573 _frame_buffer, _count_ptr); 2574 } 2575 } 2576 2577 void 2578 GetStackTraceClosure::do_vthread(Handle target_h) { 2579 Thread* current = Thread::current(); 2580 ResourceMark rm(current); 2581 2582 javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(target_h()); 2583 _result = ((JvmtiEnvBase *)_env)->get_stack_trace(jvf, 2584 _start_depth, _max_count, 2585 _frame_buffer, _count_ptr); 2586 } 2587 2588 #ifdef ASSERT 2589 void 2590 PrintStackTraceClosure::do_thread_impl(Thread *target) { 2591 JavaThread *java_thread = JavaThread::cast(target); 2592 Thread *current_thread = Thread::current(); 2593 2594 ResourceMark rm (current_thread); 2595 const char* tname = JvmtiTrace::safe_get_thread_name(java_thread); 2596 oop t_oop = java_thread->jvmti_vthread(); 2597 t_oop = t_oop == nullptr ? java_thread->threadObj() : t_oop; 2598 bool is_vt_suspended = java_lang_VirtualThread::is_instance(t_oop) && JvmtiVTSuspender::is_vthread_suspended(t_oop); 2599 2600 log_error(jvmti)("%s(%s) exiting: %d is_susp: %d is_thread_susp: %d is_vthread_susp: %d " 2601 "is_VTMS_transition_disabler: %d, is_in_VTMS_transition = %d\n", 2602 tname, java_thread->name(), java_thread->is_exiting(), 2603 java_thread->is_suspended(), java_thread->is_carrier_thread_suspended(), is_vt_suspended, 2604 java_thread->is_VTMS_transition_disabler(), java_thread->is_in_VTMS_transition()); 2605 2606 if (java_thread->has_last_Java_frame()) { 2607 RegisterMap reg_map(java_thread, 2608 RegisterMap::UpdateMap::include, 2609 RegisterMap::ProcessFrames::include, 2610 RegisterMap::WalkContinuation::skip); 2611 ResourceMark rm(current_thread); 2612 HandleMark hm(current_thread); 2613 javaVFrame *jvf = java_thread->last_java_vframe(®_map); 2614 while (jvf != nullptr) { 2615 log_error(jvmti)(" %s:%d", 2616 jvf->method()->external_name(), 2617 jvf->method()->line_number_from_bci(jvf->bci())); 2618 jvf = jvf->java_sender(); 2619 } 2620 } 2621 log_error(jvmti)("\n"); 2622 } 2623 2624 void 2625 PrintStackTraceClosure::do_thread(Thread *target) { 2626 JavaThread *java_thread = JavaThread::cast(target); 2627 Thread *current_thread = Thread::current(); 2628 2629 assert(SafepointSynchronize::is_at_safepoint() || 2630 java_thread->is_handshake_safe_for(current_thread), 2631 "call by myself / at safepoint / at handshake"); 2632 2633 PrintStackTraceClosure::do_thread_impl(target); 2634 } 2635 #endif 2636 2637 void 2638 GetFrameCountClosure::do_thread(Thread *target) { 2639 JavaThread* jt = JavaThread::cast(target); 2640 assert(target == jt, "just checking"); 2641 2642 if (!jt->is_exiting() && jt->threadObj() != nullptr) { 2643 _result = ((JvmtiEnvBase*)_env)->get_frame_count(jt, _count_ptr); 2644 } 2645 } 2646 2647 void 2648 GetFrameCountClosure::do_vthread(Handle target_h) { 2649 _result = ((JvmtiEnvBase*)_env)->get_frame_count(target_h(), _count_ptr); 2650 } 2651 2652 void 2653 GetFrameLocationClosure::do_thread(Thread *target) { 2654 JavaThread *jt = JavaThread::cast(target); 2655 assert(target == jt, "just checking"); 2656 2657 if (!jt->is_exiting() && jt->threadObj() != nullptr) { 2658 _result = ((JvmtiEnvBase*)_env)->get_frame_location(jt, _depth, 2659 _method_ptr, _location_ptr); 2660 } 2661 } 2662 2663 void 2664 GetFrameLocationClosure::do_vthread(Handle target_h) { 2665 _result = ((JvmtiEnvBase*)_env)->get_frame_location(target_h(), _depth, 2666 _method_ptr, _location_ptr); 2667 }