< prev index next >

src/hotspot/share/prims/jvmtiEnvBase.cpp

Print this page

 768     // This is for extra safety. Other bits are not expected nor needed.
 769     state &= (JVMTI_THREAD_STATE_ALIVE | JVMTI_THREAD_STATE_INTERRUPTED);
 770 
 771     if (jt->is_carrier_thread_suspended()) {
 772       state |= JVMTI_THREAD_STATE_SUSPENDED;
 773     }
 774     // It's okay for the JVMTI state to be reported as WAITING when waiting
 775     // for something other than an Object.wait. So, we treat a thread carrying
 776     // a virtual thread as waiting indefinitely which is not runnable.
 777     // It is why the RUNNABLE bit is not needed and the WAITING bits are added.
 778     state |= JVMTI_THREAD_STATE_WAITING | JVMTI_THREAD_STATE_WAITING_INDEFINITELY;
 779   } else {
 780     state = get_thread_state_base(thread_oop, jt);
 781   }
 782   return state;
 783 }
 784 
 785 jint
 786 JvmtiEnvBase::get_vthread_state(oop thread_oop, JavaThread* java_thread) {
 787   jint state = 0;

 788   bool ext_suspended = JvmtiVTSuspender::is_vthread_suspended(thread_oop);
 789   jint interrupted = java_lang_Thread::interrupted(thread_oop);
 790 
 791   if (java_thread != nullptr) {
 792     // If virtual thread is blocked on a monitor enter the BLOCKED_ON_MONITOR_ENTER bit
 793     // is set for carrier thread instead of virtual.
 794     // Other state bits except filtered ones are expected to be the same.
 795     oop ct_oop = java_lang_VirtualThread::carrier_thread(thread_oop);
 796     jint filtered_bits = JVMTI_THREAD_STATE_SUSPENDED | JVMTI_THREAD_STATE_INTERRUPTED;
 797 
 798     // This call can trigger a safepoint, so thread_oop must not be used after it.
 799     state = get_thread_state_base(ct_oop, java_thread) & ~filtered_bits;


 800   } else {
 801     int vt_state = java_lang_VirtualThread::state(thread_oop);
 802     state = (jint)java_lang_VirtualThread::map_state_to_thread_status(vt_state);
 803   }
 804   // Ensure the thread has not exited after retrieving suspended/interrupted values.
 805   if ((state & JVMTI_THREAD_STATE_ALIVE) != 0) {
 806     if (ext_suspended) {
 807       state |= JVMTI_THREAD_STATE_SUSPENDED;
 808     }
 809     if (interrupted) {
 810       state |= JVMTI_THREAD_STATE_INTERRUPTED;
 811     }
 812   }
 813   return state;
 814 }
 815 
 816 jvmtiError
 817 JvmtiEnvBase::get_live_threads(JavaThread* current_thread, Handle group_hdl, jint *count_ptr, Handle **thread_objs_p) {
 818   jint count = 0;
 819   Handle *thread_objs = nullptr;

 981          jvf != nullptr; jvf = jvf->java_sender()) {
 982       if (MaxJavaStackTraceDepth == 0 || depth++ < MaxJavaStackTraceDepth) {  // check for stack too deep
 983         // add locked objects for this frame into list
 984         err = get_locked_objects_in_frame(calling_thread, java_thread, jvf, owned_monitors_list, depth-1);
 985         if (err != JVMTI_ERROR_NONE) {
 986           return err;
 987         }
 988       }
 989     }
 990   }
 991 
 992   // Get off stack monitors. (e.g. acquired via jni MonitorEnter).
 993   JvmtiMonitorClosure jmc(calling_thread, owned_monitors_list, this);
 994   ObjectSynchronizer::owned_monitors_iterate(&jmc, java_thread);
 995   err = jmc.error();
 996 
 997   return err;
 998 }
 999 
1000 jvmtiError
1001 JvmtiEnvBase::get_owned_monitors(JavaThread* calling_thread, JavaThread* java_thread, javaVFrame* jvf,
1002                                  GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list) {
1003   jvmtiError err = JVMTI_ERROR_NONE;
1004   Thread *current_thread = Thread::current();
1005   assert(java_thread->is_handshake_safe_for(current_thread),
1006          "call by myself or at handshake");
1007 
1008   int depth = 0;
1009   for ( ; jvf != nullptr; jvf = jvf->java_sender()) {
1010     if (MaxJavaStackTraceDepth == 0 || depth++ < MaxJavaStackTraceDepth) {  // check for stack too deep
1011       // Add locked objects for this frame into list.
1012       err = get_locked_objects_in_frame(calling_thread, java_thread, jvf, owned_monitors_list, depth - 1);
1013       if (err != JVMTI_ERROR_NONE) {
1014         return err;
1015       }
1016     }
1017   }
1018 






1019   // Get off stack monitors. (e.g. acquired via jni MonitorEnter).
1020   JvmtiMonitorClosure jmc(calling_thread, owned_monitors_list, this);
1021   ObjectSynchronizer::owned_monitors_iterate(&jmc, java_thread);
1022   err = jmc.error();
1023 
1024   return err;
1025 }
1026 
1027 // Save JNI local handles for any objects that this frame owns.
1028 jvmtiError
1029 JvmtiEnvBase::get_locked_objects_in_frame(JavaThread* calling_thread, JavaThread* java_thread,
1030                                  javaVFrame *jvf, GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitors_list, jint stack_depth) {

1031   jvmtiError err = JVMTI_ERROR_NONE;
1032   Thread* current_thread = Thread::current();
1033   ResourceMark rm(current_thread);
1034   HandleMark   hm(current_thread);
1035 
1036   GrowableArray<MonitorInfo*>* mons = jvf->monitors();
1037   if (mons->is_empty()) {
1038     return err;  // this javaVFrame holds no monitors
1039   }
1040 
1041   oop wait_obj = nullptr;
1042   {
1043     // The ObjectMonitor* can't be async deflated since we are either
1044     // at a safepoint or the calling thread is operating on itself so
1045     // it cannot leave the underlying wait() call.
1046     // Save object of current wait() call (if any) for later comparison.
1047     ObjectMonitor *mon = java_thread->current_waiting_monitor();
1048     if (mon != nullptr) {
1049       wait_obj = mon->object();
1050     }
1051   }
1052   oop pending_obj = nullptr;
1053   {
1054     // The ObjectMonitor* can't be async deflated since we are either
1055     // at a safepoint or the calling thread is operating on itself so
1056     // it cannot leave the underlying enter() call.
1057     // Save object of current enter() call (if any) for later comparison.
1058     ObjectMonitor *mon = java_thread->current_pending_monitor();
1059     if (mon != nullptr) {
1060       pending_obj = mon->object();








1061     }
1062   }
1063 
1064   for (int i = 0; i < mons->length(); i++) {
1065     MonitorInfo *mi = mons->at(i);
1066 
1067     if (mi->owner_is_scalar_replaced()) continue;
1068 
1069     oop obj = mi->owner();
1070     if (obj == nullptr) {
1071       // this monitor doesn't have an owning object so skip it
1072       continue;
1073     }
1074 
1075     if (wait_obj == obj) {
1076       // the thread is waiting on this monitor so it isn't really owned
1077       continue;
1078     }
1079 
1080     if (pending_obj == obj) {

2560   assert(target == jt, "just checking");
2561 
2562   if (!jt->is_exiting() && jt->threadObj() != nullptr) {
2563     _result = ((JvmtiEnvBase*)_env)->get_frame_location(jt, _depth,
2564                                                         _method_ptr, _location_ptr);
2565   }
2566 }
2567 
2568 void
2569 GetFrameLocationClosure::do_vthread(Handle target_h) {
2570   _result = ((JvmtiEnvBase*)_env)->get_frame_location(target_h(), _depth,
2571                                                       _method_ptr, _location_ptr);
2572 }
2573 
2574 void
2575 VirtualThreadGetOwnedMonitorInfoClosure::do_thread(Thread *target) {
2576   if (!JvmtiEnvBase::is_vthread_alive(_vthread_h())) {
2577     _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
2578     return;
2579   }
2580   JavaThread* java_thread = JavaThread::cast(target);
2581   Thread* cur_thread = Thread::current();
2582   ResourceMark rm(cur_thread);
2583   HandleMark hm(cur_thread);
2584 
2585   javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(_vthread_h());
2586 
2587   if (!java_thread->is_exiting() && java_thread->threadObj() != nullptr) {
2588     _result = ((JvmtiEnvBase *)_env)->get_owned_monitors(java_thread,
2589                                                          java_thread,
2590                                                          jvf,
2591                                                          _owned_monitors_list);

2592   }
2593 }
2594 
2595 void
2596 VirtualThreadGetThreadClosure::do_thread(Thread *target) {
2597   assert(target->is_Java_thread(), "just checking");
2598   JavaThread *jt = JavaThread::cast(target);
2599   oop carrier_thread = java_lang_VirtualThread::carrier_thread(_vthread_h());
2600   *_carrier_thread_ptr = (jthread)JNIHandles::make_local(jt, carrier_thread);
2601 }
2602 
2603 void
2604 VirtualThreadGetThreadStateClosure::do_thread(Thread *target) {
2605   assert(target->is_Java_thread(), "just checking");

2606   int vthread_state = java_lang_VirtualThread::state(_vthread_h());
2607   oop carrier_thread_oop = java_lang_VirtualThread::carrier_thread(_vthread_h());
2608   jint state;
2609 
2610   if (vthread_state == java_lang_VirtualThread::RUNNING && carrier_thread_oop != nullptr) {
2611     state = (jint) java_lang_Thread::get_thread_status(carrier_thread_oop);
2612     JavaThread* java_thread = java_lang_Thread::thread(carrier_thread_oop);
2613     if (java_thread->is_suspended()) {
2614       state |= JVMTI_THREAD_STATE_SUSPENDED;
2615     }


2616   } else {
2617     state = (jint) java_lang_VirtualThread::map_state_to_thread_status(vthread_state);
2618   }
2619   if (java_lang_Thread::interrupted(_vthread_h())) {
2620     state |= JVMTI_THREAD_STATE_INTERRUPTED;
2621   }
2622   *_state_ptr = state;
2623   _result = JVMTI_ERROR_NONE;
2624 }

 768     // This is for extra safety. Other bits are not expected nor needed.
 769     state &= (JVMTI_THREAD_STATE_ALIVE | JVMTI_THREAD_STATE_INTERRUPTED);
 770 
 771     if (jt->is_carrier_thread_suspended()) {
 772       state |= JVMTI_THREAD_STATE_SUSPENDED;
 773     }
 774     // It's okay for the JVMTI state to be reported as WAITING when waiting
 775     // for something other than an Object.wait. So, we treat a thread carrying
 776     // a virtual thread as waiting indefinitely which is not runnable.
 777     // It is why the RUNNABLE bit is not needed and the WAITING bits are added.
 778     state |= JVMTI_THREAD_STATE_WAITING | JVMTI_THREAD_STATE_WAITING_INDEFINITELY;
 779   } else {
 780     state = get_thread_state_base(thread_oop, jt);
 781   }
 782   return state;
 783 }
 784 
 785 jint
 786 JvmtiEnvBase::get_vthread_state(oop thread_oop, JavaThread* java_thread) {
 787   jint state = 0;
 788   bool is_mon_responsible = java_lang_VirtualThread::recheckInterval(thread_oop) > 0;
 789   bool ext_suspended = JvmtiVTSuspender::is_vthread_suspended(thread_oop);
 790   jint interrupted = java_lang_Thread::interrupted(thread_oop);
 791 
 792   if (java_thread != nullptr) {
 793     // If virtual thread is blocked on a monitor enter the BLOCKED_ON_MONITOR_ENTER bit
 794     // is set for carrier thread instead of virtual.
 795     // Other state bits except filtered ones are expected to be the same.
 796     oop ct_oop = java_lang_VirtualThread::carrier_thread(thread_oop);
 797     jint filtered_bits = JVMTI_THREAD_STATE_SUSPENDED | JVMTI_THREAD_STATE_INTERRUPTED;
 798 
 799     // This call can trigger a safepoint, so thread_oop must not be used after it.
 800     state = get_thread_state_base(ct_oop, java_thread) & ~filtered_bits;
 801   } else if (is_mon_responsible) {
 802     state = (jint) JavaThreadStatus::BLOCKED_ON_MONITOR_ENTER;
 803   } else {
 804     int vt_state = java_lang_VirtualThread::state(thread_oop);
 805     state = (jint)java_lang_VirtualThread::map_state_to_thread_status(vt_state);
 806   }
 807   // Ensure the thread has not exited after retrieving suspended/interrupted values.
 808   if ((state & JVMTI_THREAD_STATE_ALIVE) != 0) {
 809     if (ext_suspended) {
 810       state |= JVMTI_THREAD_STATE_SUSPENDED;
 811     }
 812     if (interrupted) {
 813       state |= JVMTI_THREAD_STATE_INTERRUPTED;
 814     }
 815   }
 816   return state;
 817 }
 818 
 819 jvmtiError
 820 JvmtiEnvBase::get_live_threads(JavaThread* current_thread, Handle group_hdl, jint *count_ptr, Handle **thread_objs_p) {
 821   jint count = 0;
 822   Handle *thread_objs = nullptr;

 984          jvf != nullptr; jvf = jvf->java_sender()) {
 985       if (MaxJavaStackTraceDepth == 0 || depth++ < MaxJavaStackTraceDepth) {  // check for stack too deep
 986         // add locked objects for this frame into list
 987         err = get_locked_objects_in_frame(calling_thread, java_thread, jvf, owned_monitors_list, depth-1);
 988         if (err != JVMTI_ERROR_NONE) {
 989           return err;
 990         }
 991       }
 992     }
 993   }
 994 
 995   // Get off stack monitors. (e.g. acquired via jni MonitorEnter).
 996   JvmtiMonitorClosure jmc(calling_thread, owned_monitors_list, this);
 997   ObjectSynchronizer::owned_monitors_iterate(&jmc, java_thread);
 998   err = jmc.error();
 999 
1000   return err;
1001 }
1002 
1003 jvmtiError
1004 JvmtiEnvBase::get_owned_monitors(JavaThread* calling_thread, JavaThread* carrier, javaVFrame* jvf,
1005                                  GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list, oop vthread) {
1006   jvmtiError err = JVMTI_ERROR_NONE;
1007   Thread *current_thread = Thread::current();
1008   assert(carrier == nullptr || carrier->is_handshake_safe_for(current_thread),
1009          "call by myself or at handshake");
1010 
1011   int depth = 0;
1012   for ( ; jvf != nullptr; jvf = jvf->java_sender()) {
1013     if (MaxJavaStackTraceDepth == 0 || depth++ < MaxJavaStackTraceDepth) {  // check for stack too deep
1014       // Add locked objects for this frame into list.
1015       err = get_locked_objects_in_frame(calling_thread, carrier, jvf, owned_monitors_list, depth - 1, vthread);
1016       if (err != JVMTI_ERROR_NONE) {
1017         return err;
1018       }
1019     }
1020   }
1021 
1022   if (carrier == nullptr) {
1023     // vthread gets pinned if monitors are acquired via jni MonitorEnter
1024     // so nothing else to do for unmounted case.
1025     return err;
1026   }
1027 
1028   // Get off stack monitors. (e.g. acquired via jni MonitorEnter).
1029   JvmtiMonitorClosure jmc(calling_thread, owned_monitors_list, this);
1030   ObjectSynchronizer::owned_monitors_iterate(&jmc, carrier);
1031   err = jmc.error();
1032 
1033   return err;
1034 }
1035 
1036 // Save JNI local handles for any objects that this frame owns.
1037 jvmtiError
1038 JvmtiEnvBase::get_locked_objects_in_frame(JavaThread* calling_thread, JavaThread* target,
1039                                  javaVFrame *jvf, GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitors_list,
1040                                  jint stack_depth, oop vthread) {
1041   jvmtiError err = JVMTI_ERROR_NONE;
1042   Thread* current_thread = Thread::current();
1043   ResourceMark rm(current_thread);
1044   HandleMark   hm(current_thread);
1045 
1046   GrowableArray<MonitorInfo*>* mons = jvf->monitors();
1047   if (mons->is_empty()) {
1048     return err;  // this javaVFrame holds no monitors
1049   }
1050 
1051   oop wait_obj = nullptr;
1052   {
1053     // The ObjectMonitor* can't be async deflated since we are either
1054     // at a safepoint or the calling thread is operating on itself so
1055     // it cannot leave the underlying wait() call.
1056     // Save object of current wait() call (if any) for later comparison.
1057     if (target != nullptr) {
1058       ObjectMonitor *mon = target->current_waiting_monitor();
1059       if (mon != nullptr) wait_obj = mon->object();
1060     }
1061   }
1062   oop pending_obj = nullptr;
1063   {
1064     // The ObjectMonitor* can't be async deflated since we are either
1065     // at a safepoint or the calling thread is operating on itself so
1066     // it cannot leave the underlying enter() call.
1067     // Save object of current enter() call (if any) for later comparison.
1068     if (target != nullptr) {
1069       ObjectMonitor *mon = target->current_pending_monitor();
1070       if (mon != nullptr) pending_obj = mon->object();
1071     } else {
1072       assert(vthread != nullptr, "no vthread oop");
1073       oop oopCont = java_lang_VirtualThread::continuation(vthread);
1074       assert(oopCont != nullptr, "vthread with no continuation");
1075       stackChunkOop chunk = jdk_internal_vm_Continuation::tail(oopCont);
1076       assert(chunk != nullptr, "unmounted vthread should have a chunk");
1077       ObjectMonitor *mon = chunk->objectMonitor();
1078       if (mon != nullptr) pending_obj = mon->object();
1079     }
1080   }
1081 
1082   for (int i = 0; i < mons->length(); i++) {
1083     MonitorInfo *mi = mons->at(i);
1084 
1085     if (mi->owner_is_scalar_replaced()) continue;
1086 
1087     oop obj = mi->owner();
1088     if (obj == nullptr) {
1089       // this monitor doesn't have an owning object so skip it
1090       continue;
1091     }
1092 
1093     if (wait_obj == obj) {
1094       // the thread is waiting on this monitor so it isn't really owned
1095       continue;
1096     }
1097 
1098     if (pending_obj == obj) {

2578   assert(target == jt, "just checking");
2579 
2580   if (!jt->is_exiting() && jt->threadObj() != nullptr) {
2581     _result = ((JvmtiEnvBase*)_env)->get_frame_location(jt, _depth,
2582                                                         _method_ptr, _location_ptr);
2583   }
2584 }
2585 
2586 void
2587 GetFrameLocationClosure::do_vthread(Handle target_h) {
2588   _result = ((JvmtiEnvBase*)_env)->get_frame_location(target_h(), _depth,
2589                                                       _method_ptr, _location_ptr);
2590 }
2591 
2592 void
2593 VirtualThreadGetOwnedMonitorInfoClosure::do_thread(Thread *target) {
2594   if (!JvmtiEnvBase::is_vthread_alive(_vthread_h())) {
2595     _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
2596     return;
2597   }
2598   JavaThread* carrier = target != nullptr ? JavaThread::cast(target) : nullptr;
2599   JavaThread* cur_thread = JavaThread::current();
2600   ResourceMark rm(cur_thread);
2601   HandleMark hm(cur_thread);
2602 
2603   javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(_vthread_h());
2604 
2605   if (carrier == nullptr || (!carrier->is_exiting() && carrier->threadObj() != nullptr)) {
2606     _result = ((JvmtiEnvBase *)_env)->get_owned_monitors(cur_thread,
2607                                                          carrier,
2608                                                          jvf,
2609                                                          _owned_monitors_list,
2610                                                          _vthread_h());
2611   }
2612 }
2613 
2614 void
2615 VirtualThreadGetThreadClosure::do_thread(Thread *target) {
2616   assert(target->is_Java_thread(), "just checking");
2617   JavaThread *jt = JavaThread::cast(target);
2618   oop carrier_thread = java_lang_VirtualThread::carrier_thread(_vthread_h());
2619   *_carrier_thread_ptr = (jthread)JNIHandles::make_local(jt, carrier_thread);
2620 }
2621 
2622 void
2623 VirtualThreadGetThreadStateClosure::do_thread(Thread *target) {
2624   assert(target->is_Java_thread(), "just checking");
2625   bool is_mon_responsible = java_lang_VirtualThread::recheckInterval(_vthread_h()) > 0;
2626   int vthread_state = java_lang_VirtualThread::state(_vthread_h());
2627   oop carrier_thread_oop = java_lang_VirtualThread::carrier_thread(_vthread_h());
2628   jint state;
2629 
2630   if (vthread_state == java_lang_VirtualThread::RUNNING && carrier_thread_oop != nullptr) {
2631     state = (jint) java_lang_Thread::get_thread_status(carrier_thread_oop);
2632     JavaThread* java_thread = java_lang_Thread::thread(carrier_thread_oop);
2633     if (java_thread->is_suspended()) {
2634       state |= JVMTI_THREAD_STATE_SUSPENDED;
2635     }
2636   } else if (is_mon_responsible) {
2637     state = (jint) JavaThreadStatus::BLOCKED_ON_MONITOR_ENTER;
2638   } else {
2639     state = (jint) java_lang_VirtualThread::map_state_to_thread_status(vthread_state);
2640   }
2641   if (java_lang_Thread::interrupted(_vthread_h())) {
2642     state |= JVMTI_THREAD_STATE_INTERRUPTED;
2643   }
2644   *_state_ptr = state;
2645   _result = JVMTI_ERROR_NONE;
2646 }
< prev index next >