< prev index next >

src/hotspot/share/prims/jvmtiEnvBase.cpp

Print this page

  39 #include "prims/jvmtiExtensions.hpp"
  40 #include "prims/jvmtiImpl.hpp"
  41 #include "prims/jvmtiManageCapabilities.hpp"
  42 #include "prims/jvmtiTagMap.hpp"
  43 #include "prims/jvmtiThreadState.inline.hpp"
  44 #include "runtime/deoptimization.hpp"
  45 #include "runtime/frame.inline.hpp"
  46 #include "runtime/handles.inline.hpp"
  47 #include "runtime/interfaceSupport.inline.hpp"
  48 #include "runtime/jfieldIDWorkaround.hpp"
  49 #include "runtime/jniHandles.inline.hpp"
  50 #include "runtime/objectMonitor.inline.hpp"
  51 #include "runtime/osThread.hpp"
  52 #include "runtime/signature.hpp"
  53 #include "runtime/thread.inline.hpp"
  54 #include "runtime/threadSMR.hpp"
  55 #include "runtime/vframe.inline.hpp"
  56 #include "runtime/vframe_hp.hpp"
  57 #include "runtime/vmThread.hpp"
  58 #include "runtime/vmOperations.hpp"

  59 
  60 
  61 ///////////////////////////////////////////////////////////////
  62 //
  63 // JvmtiEnvBase
  64 //
  65 
  66 JvmtiEnvBase* JvmtiEnvBase::_head_environment = NULL;
  67 
  68 bool JvmtiEnvBase::_globally_initialized = false;
  69 volatile bool JvmtiEnvBase::_needs_clean_up = false;
  70 
  71 jvmtiPhase JvmtiEnvBase::_phase = JVMTI_PHASE_PRIMORDIAL;
  72 
  73 volatile int JvmtiEnvBase::_dying_thread_env_iteration_count = 0;
  74 
  75 extern jvmtiInterface_1_ jvmti_Interface;
  76 extern jvmtiInterface_1_ jvmtiTrace_Interface;
  77 
  78 

 542 
 543   for (int i=0; i<length; i++) {
 544     objArray[i] = jni_reference(handles[i]);
 545   }
 546   return objArray;
 547 }
 548 
 549 jthread *
 550 JvmtiEnvBase::new_jthreadArray(int length, Handle *handles) {
 551   return (jthread *) new_jobjectArray(length,handles);
 552 }
 553 
 554 jthreadGroup *
 555 JvmtiEnvBase::new_jthreadGroupArray(int length, Handle *handles) {
 556   return (jthreadGroup *) new_jobjectArray(length,handles);
 557 }
 558 
 559 // return the vframe on the specified thread and depth, NULL if no such frame
 560 // The thread and the oops in the returned vframe might not have been process.
 561 vframe*
 562 JvmtiEnvBase::vframeForNoProcess(JavaThread* java_thread, jint depth) {
 563   if (!java_thread->has_last_Java_frame()) {
 564     return NULL;
 565   }
 566   RegisterMap reg_map(java_thread, true /* update_map */, false /* process_frames */);
 567   vframe *vf = java_thread->last_java_vframe(&reg_map);

 568   int d = 0;
 569   while ((vf != NULL) && (d < depth)) {
 570     vf = vf->java_sender();
 571     d++;
 572   }
 573   return vf;
 574 }
 575 
 576 
 577 //
 578 // utilities: JNI objects
 579 //
 580 
 581 
 582 jclass
 583 JvmtiEnvBase::get_jni_class_non_null(Klass* k) {
 584   assert(k != NULL, "k != NULL");
 585   Thread *thread = Thread::current();
 586   return (jclass)jni_reference(Handle(thread, k->java_mirror()));
 587 }

 590 // Field Information
 591 //
 592 
 593 bool
 594 JvmtiEnvBase::get_field_descriptor(Klass* k, jfieldID field, fieldDescriptor* fd) {
 595   if (!jfieldIDWorkaround::is_valid_jfieldID(k, field)) {
 596     return false;
 597   }
 598   bool found = false;
 599   if (jfieldIDWorkaround::is_static_jfieldID(field)) {
 600     JNIid* id = jfieldIDWorkaround::from_static_jfieldID(field);
 601     found = id->find_local_field(fd);
 602   } else {
 603     // Non-static field. The fieldID is really the offset of the field within the object.
 604     int offset = jfieldIDWorkaround::from_instance_jfieldID(k, field);
 605     found = InstanceKlass::cast(k)->find_field_from_offset(offset, false, fd);
 606   }
 607   return found;
 608 }
 609 








































































































































































































 610 //
 611 // Object Monitor Information
 612 //
 613 
 614 //
 615 // Count the number of objects for a lightweight monitor. The hobj
 616 // parameter is object that owns the monitor so this routine will
 617 // count the number of times the same object was locked by frames
 618 // in java_thread.
 619 //
 620 jint
 621 JvmtiEnvBase::count_locked_objects(JavaThread *java_thread, Handle hobj) {
 622   jint ret = 0;
 623   if (!java_thread->has_last_Java_frame()) {
 624     return ret;  // no Java frames so no monitors
 625   }
 626 
 627   Thread* current_thread = Thread::current();
 628   ResourceMark rm(current_thread);
 629   HandleMark   hm(current_thread);
 630   RegisterMap  reg_map(java_thread);
 631 
 632   for(javaVFrame *jvf=java_thread->last_java_vframe(&reg_map); jvf != NULL;
 633                                                  jvf = jvf->java_sender()) {

 634     GrowableArray<MonitorInfo*>* mons = jvf->monitors();
 635     if (!mons->is_empty()) {
 636       for (int i = 0; i < mons->length(); i++) {
 637         MonitorInfo *mi = mons->at(i);
 638         if (mi->owner_is_scalar_replaced()) continue;
 639 
 640         // see if owner of the monitor is our object
 641         if (mi->owner() != NULL && mi->owner() == hobj()) {
 642           ret++;
 643         }
 644       }
 645     }
 646   }
 647   return ret;
 648 }
 649 
 650 
 651 
 652 jvmtiError
 653 JvmtiEnvBase::get_current_contended_monitor(JavaThread *calling_thread, JavaThread *java_thread, jobject *monitor_ptr) {

 654   Thread *current_thread = Thread::current();
 655   assert(java_thread->is_handshake_safe_for(current_thread),
 656          "call by myself or at handshake");






 657   oop obj = NULL;
 658   // The ObjectMonitor* can't be async deflated since we are either
 659   // at a safepoint or the calling thread is operating on itself so
 660   // it cannot leave the underlying wait()/enter() call.
 661   ObjectMonitor *mon = java_thread->current_waiting_monitor();
 662   if (mon == NULL) {
 663     // thread is not doing an Object.wait() call
 664     mon = java_thread->current_pending_monitor();
 665     if (mon != NULL) {
 666       // The thread is trying to enter() an ObjectMonitor.
 667       obj = mon->object();
 668       assert(obj != NULL, "ObjectMonitor should have a valid object!");
 669     }
 670     // implied else: no contended ObjectMonitor
 671   } else {
 672     // thread is doing an Object.wait() call
 673     obj = mon->object();
 674     assert(obj != NULL, "Object.wait() should have an object");
 675   }
 676 
 677   if (obj == NULL) {
 678     *monitor_ptr = NULL;
 679   } else {
 680     HandleMark hm(current_thread);
 681     Handle     hobj(current_thread, obj);
 682     *monitor_ptr = jni_reference(calling_thread, hobj);
 683   }
 684   return JVMTI_ERROR_NONE;
 685 }
 686 
 687 
 688 jvmtiError
 689 JvmtiEnvBase::get_owned_monitors(JavaThread *calling_thread, JavaThread* java_thread,
 690                                  GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list) {
 691   // Note:
 692   // calling_thread is the thread that requested the list of monitors for java_thread.
 693   // java_thread is the thread owning the monitors.
 694   // current_thread is the thread executing this code, can be a non-JavaThread (e.g. VM Thread).
 695   // And they all may be different threads.
 696   jvmtiError err = JVMTI_ERROR_NONE;
 697   Thread *current_thread = Thread::current();
 698   assert(java_thread->is_handshake_safe_for(current_thread),
 699          "call by myself or at handshake");
 700 





 701   if (java_thread->has_last_Java_frame()) {
 702     ResourceMark rm(current_thread);
 703     HandleMark   hm(current_thread);
 704     RegisterMap  reg_map(java_thread);
 705 
 706     int depth = 0;
 707     for (javaVFrame *jvf = java_thread->last_java_vframe(&reg_map); jvf != NULL;

 708          jvf = jvf->java_sender()) {
 709       if (MaxJavaStackTraceDepth == 0 || depth++ < MaxJavaStackTraceDepth) {  // check for stack too deep
 710         // add locked objects for this frame into list
 711         err = get_locked_objects_in_frame(calling_thread, java_thread, jvf, owned_monitors_list, depth-1);
 712         if (err != JVMTI_ERROR_NONE) {
 713           return err;
 714         }
 715       }
 716     }
 717   }
 718 
 719   // Get off stack monitors. (e.g. acquired via jni MonitorEnter).
 720   JvmtiMonitorClosure jmc(calling_thread, owned_monitors_list, this);
 721   ObjectSynchronizer::monitors_iterate(&jmc, java_thread);
 722   err = jmc.error();
 723 
 724   return err;
 725 }
 726 






























 727 // Save JNI local handles for any objects that this frame owns.
 728 jvmtiError
 729 JvmtiEnvBase::get_locked_objects_in_frame(JavaThread* calling_thread, JavaThread* java_thread,
 730                                  javaVFrame *jvf, GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitors_list, jint stack_depth) {
 731   jvmtiError err = JVMTI_ERROR_NONE;
 732   Thread* current_thread = Thread::current();
 733   ResourceMark rm(current_thread);
 734   HandleMark   hm(current_thread);
 735 
 736   GrowableArray<MonitorInfo*>* mons = jvf->monitors();
 737   if (mons->is_empty()) {
 738     return err;  // this javaVFrame holds no monitors
 739   }
 740 
 741   oop wait_obj = NULL;
 742   {
 743     // The ObjectMonitor* can't be async deflated since we are either
 744     // at a safepoint or the calling thread is operating on itself so
 745     // it cannot leave the underlying wait() call.
 746     // Save object of current wait() call (if any) for later comparison.

 799         // already have this object so don't include it
 800         continue;
 801       }
 802     }
 803 
 804     // add the owning object to our list
 805     jvmtiMonitorStackDepthInfo *jmsdi;
 806     err = allocate(sizeof(jvmtiMonitorStackDepthInfo), (unsigned char **)&jmsdi);
 807     if (err != JVMTI_ERROR_NONE) {
 808         return err;
 809     }
 810     Handle hobj(Thread::current(), obj);
 811     jmsdi->monitor = jni_reference(calling_thread, hobj);
 812     jmsdi->stack_depth = stack_depth;
 813     owned_monitors_list->append(jmsdi);
 814   }
 815 
 816   return err;
 817 }
 818 






























































 819 jvmtiError
 820 JvmtiEnvBase::get_stack_trace(JavaThread *java_thread,
 821                               jint start_depth, jint max_count,
 822                               jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
 823 #ifdef ASSERT
 824   uint32_t debug_bits = 0;
 825 #endif
 826   Thread *current_thread = Thread::current();
 827   assert(SafepointSynchronize::is_at_safepoint() ||
 828          java_thread->is_handshake_safe_for(current_thread),
 829          "call by myself / at safepoint / at handshake");
 830   int count = 0;


 831   if (java_thread->has_last_Java_frame()) {
 832     RegisterMap reg_map(java_thread, false /* update_map */, false /* process_frames */);
 833     ResourceMark rm(current_thread);
 834     javaVFrame *jvf = java_thread->last_java_vframe(&reg_map);
 835     HandleMark hm(current_thread);
 836     if (start_depth != 0) {
 837       if (start_depth > 0) {
 838         for (int j = 0; j < start_depth && jvf != NULL; j++) {
 839           jvf = jvf->java_sender();
 840         }
 841         if (jvf == NULL) {
 842           // start_depth is deeper than the stack depth
 843           return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 844         }
 845       } else { // start_depth < 0
 846         // we are referencing the starting depth based on the oldest
 847         // part of the stack.
 848         // optimize to limit the number of times that java_sender() is called
 849         javaVFrame *jvf_cursor = jvf;
 850         javaVFrame *jvf_prev = NULL;
 851         javaVFrame *jvf_prev_prev = NULL;
 852         int j = 0;
 853         while (jvf_cursor != NULL) {
 854           jvf_prev_prev = jvf_prev;
 855           jvf_prev = jvf_cursor;
 856           for (j = 0; j > start_depth && jvf_cursor != NULL; j--) {
 857             jvf_cursor = jvf_cursor->java_sender();
 858           }
 859         }
 860         if (j == start_depth) {
 861           // previous pointer is exactly where we want to start
 862           jvf = jvf_prev;
 863         } else {
 864           // we need to back up further to get to the right place
 865           if (jvf_prev_prev == NULL) {
 866             // the -start_depth is greater than the stack depth
 867             return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 868           }
 869           // j now is the number of frames on the stack starting with
 870           // jvf_prev, we start from jvf_prev_prev and move older on
 871           // the stack that many, the result is -start_depth frames
 872           // remaining.
 873           jvf = jvf_prev_prev;
 874           for (; j < 0; j++) {
 875             jvf = jvf->java_sender();
 876           }
 877         }
 878       }
 879     }
 880     for (; count < max_count && jvf != NULL; count++) {
 881       frame_buffer[count].method = jvf->method()->jmethod_id();
 882       frame_buffer[count].location = (jvf->method()->is_native() ? -1 : jvf->bci());
 883       jvf = jvf->java_sender();
 884     }
 885   } else {

 886     if (start_depth != 0) {
 887       // no frames and there is a starting depth
 888       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 889     }
 890   }
 891   *count_ptr = count;































 892   return JVMTI_ERROR_NONE;
 893 }
 894 
 895 jvmtiError
 896 JvmtiEnvBase::get_frame_count(JvmtiThreadState *state, jint *count_ptr) {
 897   assert((state != NULL),
 898          "JavaThread should create JvmtiThreadState before calling this method");
 899   *count_ptr = state->count_frames();





 900   return JVMTI_ERROR_NONE;
 901 }
 902 
 903 jvmtiError
 904 JvmtiEnvBase::get_frame_location(JavaThread *java_thread, jint depth,
 905                                  jmethodID* method_ptr, jlocation* location_ptr) {
 906 #ifdef ASSERT
 907   uint32_t debug_bits = 0;
 908 #endif
 909   Thread* current_thread = Thread::current();
 910   assert(java_thread->is_handshake_safe_for(current_thread),
 911          "call by myself or at handshake");
 912   ResourceMark rm(current_thread);
 913 
 914   vframe *vf = vframeForNoProcess(java_thread, depth);
 915   if (vf == NULL) {
 916     return JVMTI_ERROR_NO_MORE_FRAMES;
 917   }
 918 
 919   // vframeFor should return a java frame. If it doesn't
 920   // it means we've got an internal error and we return the
 921   // error in product mode. In debug mode we will instead
 922   // attempt to cast the vframe to a javaVFrame and will
 923   // cause an assertion/crash to allow further diagnosis.
 924 #ifdef PRODUCT
 925   if (!vf->is_java_frame()) {
 926     return JVMTI_ERROR_INTERNAL;
 927   }
 928 #endif
 929 
 930   HandleMark hm(current_thread);
 931   javaVFrame *jvf = javaVFrame::cast(vf);
 932   Method* method = jvf->method();
 933   if (method->is_native()) {
 934     *location_ptr = -1;
 935   } else {
 936     *location_ptr = jvf->bci();
 937   }
 938   *method_ptr = method->jmethod_id();
 939 
 940   return JVMTI_ERROR_NONE;
 941 }
 942 

























































































 943 
 944 jvmtiError
 945 JvmtiEnvBase::get_object_monitor_usage(JavaThread* calling_thread, jobject object, jvmtiMonitorUsage* info_ptr) {
 946   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
 947   Thread* current_thread = VMThread::vm_thread();
 948   assert(current_thread == Thread::current(), "must be");
 949 
 950   HandleMark hm(current_thread);
 951   Handle hobj;
 952 
 953   // Check arguments
 954   {
 955     oop mirror = JNIHandles::resolve_external_guard(object);
 956     NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
 957     NULL_CHECK(info_ptr, JVMTI_ERROR_NULL_POINTER);
 958 
 959     hobj = Handle(current_thread, mirror);
 960   }
 961 
 962   ThreadsListHandle tlh(current_thread);

 981         }
 982         // implied else: no owner
 983       } else {
 984         // this object has a heavyweight monitor
 985         mon = mark.monitor();
 986 
 987         // The owner field of a heavyweight monitor may be NULL for no
 988         // owner, a JavaThread * or it may still be the address of the
 989         // Lock word in a JavaThread's stack. A monitor can be inflated
 990         // by a non-owning JavaThread, but only the owning JavaThread
 991         // can change the owner field from the Lock word to the
 992         // JavaThread * and it may not have done that yet.
 993         owner = (address)mon->owner();
 994       }
 995     }
 996 
 997     if (owner != NULL) {
 998       // This monitor is owned so we have to find the owning JavaThread.
 999       owning_thread = Threads::owning_thread_from_monitor_owner(tlh.list(), owner);
1000       assert(owning_thread != NULL, "owning JavaThread must not be NULL");
1001       Handle     th(current_thread, owning_thread->threadObj());
1002       ret.owner = (jthread)jni_reference(calling_thread, th);
1003     }
1004 
1005     if (owning_thread != NULL) {  // monitor is owned
1006       // The recursions field of a monitor does not reflect recursions
1007       // as lightweight locks before inflating the monitor are not included.
1008       // We have to count the number of recursive monitor entries the hard way.
1009       // We pass a handle to survive any GCs along the way.
1010       ret.entry_count = count_locked_objects(owning_thread, hobj);
1011     }
1012     // implied else: entry_count == 0
1013   }
1014 
1015   jint nWant = 0, nWait = 0;
1016   if (mon != NULL) {
1017     // this object has a heavyweight monitor
1018     nWant = mon->contentions(); // # of threads contending for monitor
1019     nWait = mon->waiters();     // # of threads in Object.wait()
1020     ret.waiter_count = nWant + nWait;
1021     ret.notify_waiter_count = nWait;

1044 
1045     // Number of waiters may actually be less than the waiter count.
1046     // So NULL out memory so that unused memory will be NULL.
1047     memset(ret.waiters, 0, ret.waiter_count * sizeof(jthread *));
1048     memset(ret.notify_waiters, 0, ret.notify_waiter_count * sizeof(jthread *));
1049 
1050     if (ret.waiter_count > 0) {
1051       // we have contending and/or waiting threads
1052       if (nWant > 0) {
1053         // we have contending threads
1054         ResourceMark rm(current_thread);
1055         // get_pending_threads returns only java thread so we do not need to
1056         // check for non java threads.
1057         GrowableArray<JavaThread*>* wantList = Threads::get_pending_threads(tlh.list(), nWant, (address)mon);
1058         if (wantList->length() < nWant) {
1059           // robustness: the pending list has gotten smaller
1060           nWant = wantList->length();
1061         }
1062         for (int i = 0; i < nWant; i++) {
1063           JavaThread *pending_thread = wantList->at(i);
1064           Handle th(current_thread, pending_thread->threadObj());
1065           ret.waiters[i] = (jthread)jni_reference(calling_thread, th);
1066         }
1067       }
1068       if (nWait > 0) {
1069         // we have threads in Object.wait()
1070         int offset = nWant;  // add after any contending threads
1071         ObjectWaiter *waiter = mon->first_waiter();
1072         for (int i = 0, j = 0; i < nWait; i++) {
1073           if (waiter == NULL) {
1074             // robustness: the waiting list has gotten smaller
1075             nWait = j;
1076             break;
1077           }
1078           JavaThread *w = mon->thread_of_waiter(waiter);
1079           if (w != NULL) {
1080             // If the thread was found on the ObjectWaiter list, then
1081             // it has not been notified. This thread can't change the
1082             // state of the monitor so it doesn't need to be suspended.
1083             Handle th(current_thread, w->threadObj());
1084             ret.waiters[offset + j] = (jthread)jni_reference(calling_thread, th);
1085             ret.notify_waiters[j++] = (jthread)jni_reference(calling_thread, th);
1086           }
1087           waiter = mon->next_waiter(waiter);
1088         }
1089       }
1090     } // ThreadsListHandle is destroyed here.
1091 
1092     // Adjust count. nWant and nWait count values may be less than original.
1093     ret.waiter_count = nWant + nWait;
1094     ret.notify_waiter_count = nWait;
1095   } else {
1096     // this object has a lightweight monitor and we have nothing more
1097     // to do here because the defaults are just fine.
1098   }
1099 
1100   // we don't update return parameter unless everything worked
1101   *info_ptr = ret;
1102 
1103   return JVMTI_ERROR_NONE;
1104 }
1105 



















































































































































1106 ResourceTracker::ResourceTracker(JvmtiEnv* env) {
1107   _env = env;
1108   _allocations = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<unsigned char*>(20, mtServiceability);
1109   _failed = false;
1110 }
1111 ResourceTracker::~ResourceTracker() {
1112   if (_failed) {
1113     for (int i=0; i<_allocations->length(); i++) {
1114       _env->deallocate(_allocations->at(i));
1115     }
1116   }
1117   delete _allocations;
1118 }
1119 
1120 jvmtiError ResourceTracker::allocate(jlong size, unsigned char** mem_ptr) {
1121   unsigned char *ptr;
1122   jvmtiError err = _env->allocate(size, &ptr);
1123   if (err == JVMTI_ERROR_NONE) {
1124     _allocations->append(ptr);
1125     *mem_ptr = ptr;

1132 
1133 unsigned char* ResourceTracker::allocate(jlong size) {
1134   unsigned char* ptr;
1135   allocate(size, &ptr);
1136   return ptr;
1137 }
1138 
1139 char* ResourceTracker::strdup(const char* str) {
1140   char *dup_str = (char*)allocate(strlen(str)+1);
1141   if (dup_str != NULL) {
1142     strcpy(dup_str, str);
1143   }
1144   return dup_str;
1145 }
1146 
1147 struct StackInfoNode {
1148   struct StackInfoNode *next;
1149   jvmtiStackInfo info;
1150 };
1151 

1152 // Create a jvmtiStackInfo inside a linked list node and create a
1153 // buffer for the frame information, both allocated as resource objects.
1154 // Fill in both the jvmtiStackInfo and the jvmtiFrameInfo.
1155 // Note that either or both of thr and thread_oop
1156 // may be null if the thread is new or has exited.
1157 void
1158 MultipleStackTracesCollector::fill_frames(jthread jt, JavaThread *thr, oop thread_oop) {
1159 #ifdef ASSERT
1160   Thread *current_thread = Thread::current();
1161   assert(SafepointSynchronize::is_at_safepoint() ||

1162          thr->is_handshake_safe_for(current_thread),
1163          "call by myself / at safepoint / at handshake");
1164 #endif
1165 
1166   jint state = 0;
1167   struct StackInfoNode *node = NEW_RESOURCE_OBJ(struct StackInfoNode);
1168   jvmtiStackInfo *infop = &(node->info);

1169   node->next = head();
1170   set_head(node);
1171   infop->frame_count = 0;

1172   infop->thread = jt;
1173 
1174   if (thread_oop != NULL) {
1175     // get most state bits
1176     state = (jint)java_lang_Thread::get_thread_status(thread_oop);
1177   }
1178 
1179   if (thr != NULL) {    // add more state bits if there is a JavaThead to query
1180     if (thr->is_suspended()) {
1181       state |= JVMTI_THREAD_STATE_SUSPENDED;


1182     }
1183     JavaThreadState jts = thr->thread_state();
1184     if (jts == _thread_in_native) {
1185       state |= JVMTI_THREAD_STATE_IN_NATIVE;
1186     }
1187     if (thr->is_interrupted(false)) {
1188       state |= JVMTI_THREAD_STATE_INTERRUPTED;
1189     }
1190   }
1191   infop->state = state;
1192 
1193   if (thr != NULL && (state & JVMTI_THREAD_STATE_ALIVE) != 0) {
1194     infop->frame_buffer = NEW_RESOURCE_ARRAY(jvmtiFrameInfo, max_frame_count());
1195     env()->get_stack_trace(thr, 0, max_frame_count(),
1196                            infop->frame_buffer, &(infop->frame_count));
1197   } else {
1198     infop->frame_buffer = NULL;
1199     infop->frame_count = 0;




1200   }
1201   _frame_count_total += infop->frame_count;

1202 }
1203 
1204 // Based on the stack information in the linked list, allocate memory
1205 // block to return and fill it from the info in the linked list.
1206 void
1207 MultipleStackTracesCollector::allocate_and_fill_stacks(jint thread_count) {
1208   // do I need to worry about alignment issues?
1209   jlong alloc_size =  thread_count       * sizeof(jvmtiStackInfo)
1210                     + _frame_count_total * sizeof(jvmtiFrameInfo);
1211   env()->allocate(alloc_size, (unsigned char **)&_stack_info);
1212 
1213   // pointers to move through the newly allocated space as it is filled in
1214   jvmtiStackInfo *si = _stack_info + thread_count;      // bottom of stack info
1215   jvmtiFrameInfo *fi = (jvmtiFrameInfo *)si;            // is the top of frame info
1216 
1217   // copy information in resource area into allocated buffer
1218   // insert stack info backwards since linked list is backwards
1219   // insert frame info forwards
1220   // walk the StackInfoNodes
1221   for (struct StackInfoNode *sin = head(); sin != NULL; sin = sin->next) {

1235   assert((unsigned char *)fi == ((unsigned char *)_stack_info) + alloc_size,
1236          "the last copied frame info must be the last record");
1237 }
1238 
1239 
1240 void
1241 VM_GetThreadListStackTraces::doit() {
1242   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1243 
1244   ResourceMark rm;
1245   ThreadsListHandle tlh;
1246   for (int i = 0; i < _thread_count; ++i) {
1247     jthread jt = _thread_list[i];
1248     JavaThread* java_thread = NULL;
1249     oop thread_oop = NULL;
1250     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), jt, &java_thread, &thread_oop);
1251     if (err != JVMTI_ERROR_NONE) {
1252       // We got an error code so we don't have a JavaThread *, but
1253       // only return an error from here if we didn't get a valid
1254       // thread_oop.


1255       if (thread_oop == NULL) {
1256         _collector.set_result(err);
1257         return;
1258       }
1259       // We have a valid thread_oop.
1260     }
1261     _collector.fill_frames(jt, java_thread, thread_oop);
1262   }
1263   _collector.allocate_and_fill_stacks(_thread_count);
1264 }
1265 
1266 void
1267 GetSingleStackTraceClosure::do_thread(Thread *target) {
1268   JavaThread *jt = JavaThread::cast(target);
1269   oop thread_oop = jt->threadObj();
1270 
1271   if (!jt->is_exiting() && thread_oop != NULL) {
1272     ResourceMark rm;
1273     _collector.fill_frames(_jthread, jt, thread_oop);
1274     _collector.allocate_and_fill_stacks(1);
1275   }
1276 }
1277 
1278 void
1279 VM_GetAllStackTraces::doit() {
1280   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1281 
1282   ResourceMark rm;
1283   _final_thread_count = 0;
1284   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
1285     oop thread_oop = jt->threadObj();
1286     if (thread_oop != NULL &&
1287         !jt->is_exiting() &&
1288         java_lang_Thread::is_alive(thread_oop) &&
1289         !jt->is_hidden_from_external_view()) {
1290       ++_final_thread_count;
1291       // Handle block of the calling thread is used to create local refs.
1292       _collector.fill_frames((jthread)JNIHandles::make_local(_calling_thread, thread_oop),
1293                              jt, thread_oop);
1294     }
1295   }

1515   for (jint idx = 0; idx < len; idx++) {
1516     array[idx] = JNIHandles::make_local(_tbl->at(idx).resolve());
1517   }
1518   _tbl = NULL;
1519   *modules_ptr = array;
1520   *module_count_ptr = len;
1521   return JVMTI_ERROR_NONE;
1522 }
1523 
1524 void
1525 UpdateForPopTopFrameClosure::doit(Thread *target, bool self) {
1526   Thread* current_thread  = Thread::current();
1527   HandleMark hm(current_thread);
1528   JavaThread* java_thread = JavaThread::cast(target);
1529 
1530   if (java_thread->is_exiting()) {
1531     return; /* JVMTI_ERROR_THREAD_NOT_ALIVE (default) */
1532   }
1533   assert(java_thread == _state->get_thread(), "Must be");
1534 
1535   if (!self && !java_thread->is_suspended()) {
1536     _result = JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1537     return;
1538   }
1539 
1540   // Check to see if a PopFrame was already in progress
1541   if (java_thread->popframe_condition() != JavaThread::popframe_inactive) {
1542     // Probably possible for JVMTI clients to trigger this, but the
1543     // JPDA backend shouldn't allow this to happen
1544     _result = JVMTI_ERROR_INTERNAL;
1545     return;
1546   }
1547 
1548   // Was workaround bug
1549   //    4812902: popFrame hangs if the method is waiting at a synchronize
1550   // Catch this condition and return an error to avoid hanging.
1551   // Now JVMTI spec allows an implementation to bail out with an opaque frame error.
1552   OSThread* osThread = java_thread->osthread();
1553   if (osThread->get_state() == MONITOR_WAIT) {
1554     _result = JVMTI_ERROR_OPAQUE_FRAME;
1555     return;

1605 
1606   // It's fine to update the thread state here because no JVMTI events
1607   // shall be posted for this PopFrame.
1608 
1609   _state->update_for_pop_top_frame();
1610   java_thread->set_popframe_condition(JavaThread::popframe_pending_bit);
1611   // Set pending step flag for this popframe and it is cleared when next
1612   // step event is posted.
1613   _state->set_pending_step_for_popframe();
1614   _result = JVMTI_ERROR_NONE;
1615 }
1616 
1617 void
1618 SetFramePopClosure::doit(Thread *target, bool self) {
1619   ResourceMark rm;
1620   JavaThread* java_thread = JavaThread::cast(target);
1621 
1622   if (java_thread->is_exiting()) {
1623     return; /* JVMTI_ERROR_THREAD_NOT_ALIVE (default) */
1624   }
1625   assert(_state->get_thread() == java_thread, "Must be");


1626 
1627   if (!self && !java_thread->is_suspended()) {
1628     _result = JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1629     return;
1630   }
1631 
1632   vframe *vf = JvmtiEnvBase::vframeForNoProcess(java_thread, _depth);
1633   if (vf == NULL) {
1634     _result = JVMTI_ERROR_NO_MORE_FRAMES;
1635     return;
1636   }
1637 
1638   if (!vf->is_java_frame() || ((javaVFrame*) vf)->method()->is_native()) {
1639     _result = JVMTI_ERROR_OPAQUE_FRAME;
1640     return;
1641   }
1642 
1643   assert(vf->frame_pointer() != NULL, "frame pointer mustn't be NULL");
1644   int frame_number = _state->count_frames() - _depth;
1645   _state->env_thread_state((JvmtiEnvBase*)_env)->set_frame_pop(frame_number);
1646   _result = JVMTI_ERROR_NONE;
1647 }
1648 
1649 void
1650 GetOwnedMonitorInfoClosure::do_thread(Thread *target) {
1651   JavaThread *jt = JavaThread::cast(target);
1652   if (!jt->is_exiting() && (jt->threadObj() != NULL)) {
1653     _result = ((JvmtiEnvBase *)_env)->get_owned_monitors(_calling_thread,
1654                                                          jt,
1655                                                          _owned_monitors_list);
1656   }
1657 }
1658 
1659 void
1660 GetCurrentContendedMonitorClosure::do_thread(Thread *target) {
1661   JavaThread *jt = JavaThread::cast(target);
1662   if (!jt->is_exiting() && (jt->threadObj() != NULL)) {
1663     _result = ((JvmtiEnvBase *)_env)->get_current_contended_monitor(_calling_thread,
1664                                                                     jt,
1665                                                                     _owned_monitor_ptr);

1666   }
1667 }
1668 














1669 void
1670 GetStackTraceClosure::do_thread(Thread *target) {
1671   JavaThread *jt = JavaThread::cast(target);
1672   if (!jt->is_exiting() && jt->threadObj() != NULL) {
1673     _result = ((JvmtiEnvBase *)_env)->get_stack_trace(jt,
1674                                                       _start_depth, _max_count,
1675                                                       _frame_buffer, _count_ptr);
1676   }
1677 }
1678 


















































1679 void
1680 GetFrameCountClosure::do_thread(Thread *target) {
1681   JavaThread* jt = _state->get_thread();
1682   assert(target == jt, "just checking");
1683   if (!jt->is_exiting() && jt->threadObj() != NULL) {
1684     _result = ((JvmtiEnvBase*)_env)->get_frame_count(_state, _count_ptr);
1685   }
1686 }
1687 
1688 void
1689 GetFrameLocationClosure::do_thread(Thread *target) {
1690   JavaThread *jt = JavaThread::cast(target);
1691   if (!jt->is_exiting() && jt->threadObj() != NULL) {
1692     _result = ((JvmtiEnvBase*)_env)->get_frame_location(jt, _depth,
1693                                                         _method_ptr, _location_ptr);
1694   }
1695 }



















































































  39 #include "prims/jvmtiExtensions.hpp"
  40 #include "prims/jvmtiImpl.hpp"
  41 #include "prims/jvmtiManageCapabilities.hpp"
  42 #include "prims/jvmtiTagMap.hpp"
  43 #include "prims/jvmtiThreadState.inline.hpp"
  44 #include "runtime/deoptimization.hpp"
  45 #include "runtime/frame.inline.hpp"
  46 #include "runtime/handles.inline.hpp"
  47 #include "runtime/interfaceSupport.inline.hpp"
  48 #include "runtime/jfieldIDWorkaround.hpp"
  49 #include "runtime/jniHandles.inline.hpp"
  50 #include "runtime/objectMonitor.inline.hpp"
  51 #include "runtime/osThread.hpp"
  52 #include "runtime/signature.hpp"
  53 #include "runtime/thread.inline.hpp"
  54 #include "runtime/threadSMR.hpp"
  55 #include "runtime/vframe.inline.hpp"
  56 #include "runtime/vframe_hp.hpp"
  57 #include "runtime/vmThread.hpp"
  58 #include "runtime/vmOperations.hpp"
  59 #include "services/threadService.hpp"
  60 
  61 
  62 ///////////////////////////////////////////////////////////////
  63 //
  64 // JvmtiEnvBase
  65 //
  66 
  67 JvmtiEnvBase* JvmtiEnvBase::_head_environment = NULL;
  68 
  69 bool JvmtiEnvBase::_globally_initialized = false;
  70 volatile bool JvmtiEnvBase::_needs_clean_up = false;
  71 
  72 jvmtiPhase JvmtiEnvBase::_phase = JVMTI_PHASE_PRIMORDIAL;
  73 
  74 volatile int JvmtiEnvBase::_dying_thread_env_iteration_count = 0;
  75 
  76 extern jvmtiInterface_1_ jvmti_Interface;
  77 extern jvmtiInterface_1_ jvmtiTrace_Interface;
  78 
  79 

 543 
 544   for (int i=0; i<length; i++) {
 545     objArray[i] = jni_reference(handles[i]);
 546   }
 547   return objArray;
 548 }
 549 
 550 jthread *
 551 JvmtiEnvBase::new_jthreadArray(int length, Handle *handles) {
 552   return (jthread *) new_jobjectArray(length,handles);
 553 }
 554 
 555 jthreadGroup *
 556 JvmtiEnvBase::new_jthreadGroupArray(int length, Handle *handles) {
 557   return (jthreadGroup *) new_jobjectArray(length,handles);
 558 }
 559 
 560 // return the vframe on the specified thread and depth, NULL if no such frame
 561 // The thread and the oops in the returned vframe might not have been process.
 562 vframe*
 563 JvmtiEnvBase::vframeForNoProcess(JavaThread* java_thread, jint depth, bool for_cont) {
 564   if (!java_thread->has_last_Java_frame()) {
 565     return NULL;
 566   }
 567   RegisterMap reg_map(java_thread, true /* update_map */, false /* process_frames */, true /* walk_cont */);
 568   vframe *vf = for_cont ? JvmtiEnvBase::get_last_java_vframe(java_thread, &reg_map)
 569                         : java_thread->last_java_vframe(&reg_map);
 570   int d = 0;
 571   while ((vf != NULL) && (d < depth)) {
 572     vf = vf->java_sender();
 573     d++;
 574   }
 575   return vf;
 576 }
 577 
 578 
 579 //
 580 // utilities: JNI objects
 581 //
 582 
 583 
 584 jclass
 585 JvmtiEnvBase::get_jni_class_non_null(Klass* k) {
 586   assert(k != NULL, "k != NULL");
 587   Thread *thread = Thread::current();
 588   return (jclass)jni_reference(Handle(thread, k->java_mirror()));
 589 }

 592 // Field Information
 593 //
 594 
 595 bool
 596 JvmtiEnvBase::get_field_descriptor(Klass* k, jfieldID field, fieldDescriptor* fd) {
 597   if (!jfieldIDWorkaround::is_valid_jfieldID(k, field)) {
 598     return false;
 599   }
 600   bool found = false;
 601   if (jfieldIDWorkaround::is_static_jfieldID(field)) {
 602     JNIid* id = jfieldIDWorkaround::from_static_jfieldID(field);
 603     found = id->find_local_field(fd);
 604   } else {
 605     // Non-static field. The fieldID is really the offset of the field within the object.
 606     int offset = jfieldIDWorkaround::from_instance_jfieldID(k, field);
 607     found = InstanceKlass::cast(k)->find_field_from_offset(offset, false, fd);
 608   }
 609   return found;
 610 }
 611 
 612 extern "C" bool dbg_is_safe(const void* p, intptr_t errvalue);
 613 
 614 javaVFrame*
 615 JvmtiEnvBase::check_and_skip_hidden_frames(bool is_in_VTMT, javaVFrame* jvf) {
 616   // The second condition is needed to hide notification methods.
 617   if (!is_in_VTMT && (jvf == NULL || !jvf->method()->jvmti_mount_transition())) {
 618     return jvf; // no frames to skip
 619   }
 620   javaVFrame* jvf_saved = jvf;
 621   // find jvf with a method annotated with @JvmtiMountTransition
 622   for ( ; jvf != NULL; jvf = jvf->java_sender()) {
 623     if (jvf->method()->jvmti_mount_transition()) {
 624       jvf = jvf->java_sender(); // skip annotated method
 625       break;
 626     }
 627     if (jvf->method()->changes_current_thread()) {
 628       break;
 629     }
 630     // skip frame above annotated method
 631   }
 632   if (jvf == NULL) { // TMP workaround for stability until the root cause is fixed
 633     jvf = jvf_saved;
 634     assert (jvf != NULL, "");
 635     assert (dbg_is_safe(jvf, -1), "");
 636     assert (jvf->register_map() != NULL, "");
 637     assert (dbg_is_safe(jvf->register_map(), -1), "");
 638     jvf->restore_register_map(); // we're returning to a frame we've walked past and might walk from again
 639   }
 640   return jvf;
 641 }
 642 
 643 javaVFrame*
 644 JvmtiEnvBase::check_and_skip_hidden_frames(JavaThread* jt, javaVFrame* jvf) {
 645   jvf = check_and_skip_hidden_frames(jt->is_in_VTMT(), jvf);
 646   return jvf;
 647 }
 648 
 649 javaVFrame*
 650 JvmtiEnvBase::check_and_skip_hidden_frames(oop vthread, javaVFrame* jvf) {
 651   JvmtiThreadState* state = java_lang_Thread::jvmti_thread_state(vthread);
 652   if (state == NULL) {
 653     return jvf; // nothing to skip
 654   }
 655   jvf = check_and_skip_hidden_frames(state->is_in_VTMT(), jvf);
 656   return jvf;
 657 }
 658 
 659 bool
 660 JvmtiEnvBase::is_vthread_alive(oop vt) {
 661   assert(java_lang_VirtualThread::state(vt) != java_lang_VirtualThread::NEW, "sanity check");
 662   return java_lang_VirtualThread::state(vt) != java_lang_VirtualThread::TERMINATED;
 663 }
 664 
 665 javaVFrame*
 666 JvmtiEnvBase::get_vthread_jvf(oop vthread) {
 667   assert(java_lang_VirtualThread::state(vthread) != java_lang_VirtualThread::NEW, "sanity check");
 668   assert(java_lang_VirtualThread::state(vthread) != java_lang_VirtualThread::TERMINATED, "sanity check");
 669 
 670   Thread* cur_thread = Thread::current();
 671   oop cont = java_lang_VirtualThread::continuation(vthread);
 672   javaVFrame* jvf = NULL;
 673 
 674   assert(cont != NULL, "virtual thread continuation must not be NULL");
 675 
 676   oop carrier_thread = java_lang_VirtualThread::carrier_thread(vthread);
 677   // Returned carrier_thread can be NULL for a mounted continuation.
 678   // Then treat it as an unmounted case.
 679   if (jdk_internal_vm_Continuation::is_mounted(cont) && carrier_thread != NULL) {
 680     JavaThread* java_thread = java_lang_Thread::thread(carrier_thread);
 681 
 682     if (!java_thread->has_last_Java_frame()) {
 683       // TBD: This is a temporary work around to avoid a guarantee caused by
 684       // the native enterSpecial frame on the top. No frames will be found
 685       // by the JVMTI functions such as GetStackTrace.
 686       return NULL;
 687     }
 688     vframeStream vfs(java_thread, Handle(cur_thread, Continuation::continuation_scope(cont)));
 689     jvf = vfs.at_end() ? NULL : vfs.asJavaVFrame();
 690     jvf = check_and_skip_hidden_frames(java_thread, jvf);
 691   } else {
 692     vframeStream vfs(cont);
 693     jvf = vfs.at_end() ? NULL : vfs.asJavaVFrame();
 694     jvf = check_and_skip_hidden_frames(vthread, jvf);
 695   }
 696   return jvf;
 697 }
 698 
 699 javaVFrame*
 700 JvmtiEnvBase::get_last_java_vframe(JavaThread* jt, RegisterMap* reg_map_p) {
 701   // Strip vthread frames in case of carrier thread with mounted continuation.
 702   javaVFrame *jvf = JvmtiEnvBase::cthread_with_continuation(jt) ?
 703                         jt->vthread_carrier_last_java_vframe(reg_map_p) :
 704                         jt->last_java_vframe(reg_map_p);
 705   jvf = check_and_skip_hidden_frames(jt, jvf);
 706   return jvf;
 707 }
 708 
 709 jint
 710 JvmtiEnvBase::get_thread_state(oop thread_oop, JavaThread* jt) {
 711   jint state = 0;
 712 
 713   if (thread_oop != NULL) {
 714     // get most state bits
 715     state = (jint)java_lang_Thread::get_thread_status(thread_oop);
 716   }
 717   if (jt != NULL) {
 718     // We have a JavaThread* so add more state bits.
 719     JavaThreadState jts = jt->thread_state();
 720 
 721     if (jt->is_thread_suspended() ||
 722         ((jt->mounted_vthread() == NULL || jt->mounted_vthread() == thread_oop) && jt->is_suspended())) {
 723       // Suspended non-virtual thread.
 724       state |= JVMTI_THREAD_STATE_SUSPENDED;
 725     }
 726     if (jts == _thread_in_native) {
 727       state |= JVMTI_THREAD_STATE_IN_NATIVE;
 728     }
 729     if (jt->is_interrupted(false)) {
 730       state |= JVMTI_THREAD_STATE_INTERRUPTED;
 731     }
 732   }
 733   return state;
 734 }
 735 
 736 jint
 737 JvmtiEnvBase::get_vthread_state(oop thread_oop) {
 738   jshort vt_state = java_lang_VirtualThread::state(thread_oop);
 739   jint state = (jint) java_lang_VirtualThread::map_state_to_thread_status(vt_state);
 740   bool ext_suspended = JvmtiVTSuspender::is_vthread_suspended(thread_oop);
 741 
 742   if (ext_suspended && ((state & JVMTI_THREAD_STATE_ALIVE) != 0)) {
 743     state &= ~java_lang_VirtualThread::RUNNING;
 744     state |= JVMTI_THREAD_STATE_ALIVE | JVMTI_THREAD_STATE_RUNNABLE | JVMTI_THREAD_STATE_SUSPENDED;
 745   }
 746   if (java_lang_Thread::interrupted(thread_oop)) {
 747     state |= JVMTI_THREAD_STATE_INTERRUPTED;
 748   }
 749   return state;
 750 }
 751 
 752 int
 753 JvmtiEnvBase::get_live_threads(JavaThread* current_thread, Handle group_hdl, Handle **thread_objs_p) {
 754   int count = 0;
 755   Handle *thread_objs = NULL;
 756   ThreadsListEnumerator tle(current_thread, true);
 757   int nthreads = tle.num_threads();
 758   if (nthreads > 0) {
 759     thread_objs = NEW_RESOURCE_ARRAY(Handle, nthreads);
 760     NULL_CHECK(thread_objs, JVMTI_ERROR_OUT_OF_MEMORY);
 761     for (int i = 0; i < nthreads; i++) {
 762       Handle thread = tle.get_threadObj(i);
 763       if (thread()->is_a(vmClasses::Thread_klass()) && java_lang_Thread::threadGroup(thread()) == group_hdl()) {
 764         thread_objs[count++] = thread;
 765       }
 766     }
 767   }
 768   *thread_objs_p = thread_objs;
 769   return count;
 770 }
 771 
 772 int
 773 JvmtiEnvBase::get_subgroups(JavaThread* current_thread, Handle group_hdl, Handle **group_objs_p) {
 774   ObjectLocker ol(group_hdl, current_thread);
 775 
 776   int ngroups  = java_lang_ThreadGroup::ngroups(group_hdl());
 777   int nweaks  = java_lang_ThreadGroup::nweaks(group_hdl());
 778 
 779   int count = 0;
 780   Handle *group_objs = NULL;
 781   if (ngroups > 0 || nweaks > 0) {
 782     group_objs = NEW_RESOURCE_ARRAY(Handle, ngroups + nweaks);
 783     NULL_CHECK(group_objs, JVMTI_ERROR_OUT_OF_MEMORY);
 784 
 785     // strongly reachable subgroups
 786     if (ngroups > 0) {
 787       objArrayOop groups = java_lang_ThreadGroup::groups(group_hdl());
 788       for (int j = 0; j < ngroups; j++) {
 789         oop group_obj = groups->obj_at(j);
 790         assert(group_obj != NULL, "group_obj != NULL");
 791         group_objs[count++] = Handle(current_thread, group_obj);
 792       }
 793     }
 794 
 795     // weakly reachable subgroups
 796     if (nweaks > 0) {
 797       objArrayOop weaks = java_lang_ThreadGroup::weaks(group_hdl());
 798       for (int j = 0; j < nweaks; j++) {
 799         oop weak_obj = weaks->obj_at(j);
 800         assert(weak_obj != NULL, "weak_obj != NULL");
 801         oop group_obj = java_lang_ref_Reference::unknown_referent(weak_obj);
 802         if (group_obj != NULL) {
 803           group_objs[count++] = Handle(current_thread, group_obj);
 804         }
 805       }
 806     }
 807   }
 808   *group_objs_p = group_objs;
 809   return count;
 810 }
 811 
 812 //
 813 // Object Monitor Information
 814 //
 815 
 816 //
 817 // Count the number of objects for a lightweight monitor. The hobj
 818 // parameter is object that owns the monitor so this routine will
 819 // count the number of times the same object was locked by frames
 820 // in java_thread.
 821 //
 822 jint
 823 JvmtiEnvBase::count_locked_objects(JavaThread *java_thread, Handle hobj) {
 824   jint ret = 0;
 825   if (!java_thread->has_last_Java_frame()) {
 826     return ret;  // no Java frames so no monitors
 827   }
 828 
 829   Thread* current_thread = Thread::current();
 830   ResourceMark rm(current_thread);
 831   HandleMark   hm(current_thread);
 832   RegisterMap  reg_map(java_thread, true, true);
 833 
 834   for (javaVFrame *jvf = java_thread->last_java_vframe(&reg_map);
 835        jvf != NULL;
 836        jvf = jvf->java_sender()) {
 837     GrowableArray<MonitorInfo*>* mons = jvf->monitors();
 838     if (!mons->is_empty()) {
 839       for (int i = 0; i < mons->length(); i++) {
 840         MonitorInfo *mi = mons->at(i);
 841         if (mi->owner_is_scalar_replaced()) continue;
 842 
 843         // see if owner of the monitor is our object
 844         if (mi->owner() != NULL && mi->owner() == hobj()) {
 845           ret++;
 846         }
 847       }
 848     }
 849   }
 850   return ret;
 851 }
 852 


 853 jvmtiError
 854 JvmtiEnvBase::get_current_contended_monitor(JavaThread *calling_thread, JavaThread *java_thread,
 855                                             jobject *monitor_ptr, bool is_virtual) {
 856   Thread *current_thread = Thread::current();
 857   assert(java_thread->is_handshake_safe_for(current_thread),
 858          "call by myself or at handshake");
 859   if (!is_virtual && JvmtiEnvBase::cthread_with_continuation(java_thread)) {
 860     // Carrier thread with a mounted continuation case.
 861     // No contended monitor can be owned by carrier thread in this case.
 862     *monitor_ptr = nullptr;
 863     return JVMTI_ERROR_NONE;
 864   }
 865   oop obj = NULL;
 866   // The ObjectMonitor* can't be async deflated since we are either
 867   // at a safepoint or the calling thread is operating on itself so
 868   // it cannot leave the underlying wait()/enter() call.
 869   ObjectMonitor *mon = java_thread->current_waiting_monitor();
 870   if (mon == NULL) {
 871     // thread is not doing an Object.wait() call
 872     mon = java_thread->current_pending_monitor();
 873     if (mon != NULL) {
 874       // The thread is trying to enter() an ObjectMonitor.
 875       obj = mon->object();
 876       assert(obj != NULL, "ObjectMonitor should have a valid object!");
 877     }
 878     // implied else: no contended ObjectMonitor
 879   } else {
 880     // thread is doing an Object.wait() call
 881     obj = mon->object();
 882     assert(obj != NULL, "Object.wait() should have an object");
 883   }
 884 
 885   if (obj == NULL) {
 886     *monitor_ptr = NULL;
 887   } else {
 888     HandleMark hm(current_thread);
 889     Handle     hobj(current_thread, obj);
 890     *monitor_ptr = jni_reference(calling_thread, hobj);
 891   }
 892   return JVMTI_ERROR_NONE;
 893 }
 894 

 895 jvmtiError
 896 JvmtiEnvBase::get_owned_monitors(JavaThread *calling_thread, JavaThread* java_thread,
 897                                  GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list) {
 898   // Note:
 899   // calling_thread is the thread that requested the list of monitors for java_thread.
 900   // java_thread is the thread owning the monitors.
 901   // current_thread is the thread executing this code, can be a non-JavaThread (e.g. VM Thread).
 902   // And they all may be different threads.
 903   jvmtiError err = JVMTI_ERROR_NONE;
 904   Thread *current_thread = Thread::current();
 905   assert(java_thread->is_handshake_safe_for(current_thread),
 906          "call by myself or at handshake");
 907 
 908   if (JvmtiEnvBase::cthread_with_continuation(java_thread)) {
 909     // Carrier thread with a mounted continuation case.
 910     // No contended monitor can be owned by carrier thread in this case.
 911     return JVMTI_ERROR_NONE;
 912   }
 913   if (java_thread->has_last_Java_frame()) {
 914     ResourceMark rm(current_thread);
 915     HandleMark   hm(current_thread);
 916     RegisterMap  reg_map(java_thread);
 917 
 918     int depth = 0;
 919     for (javaVFrame *jvf = JvmtiEnvBase::get_last_java_vframe(java_thread, &reg_map);
 920          jvf != NULL;
 921          jvf = jvf->java_sender()) {
 922       if (MaxJavaStackTraceDepth == 0 || depth++ < MaxJavaStackTraceDepth) {  // check for stack too deep
 923         // add locked objects for this frame into list
 924         err = get_locked_objects_in_frame(calling_thread, java_thread, jvf, owned_monitors_list, depth-1);
 925         if (err != JVMTI_ERROR_NONE) {
 926           return err;
 927         }
 928       }
 929     }
 930   }
 931 
 932   // Get off stack monitors. (e.g. acquired via jni MonitorEnter).
 933   JvmtiMonitorClosure jmc(calling_thread, owned_monitors_list, this);
 934   ObjectSynchronizer::monitors_iterate(&jmc, java_thread);
 935   err = jmc.error();
 936 
 937   return err;
 938 }
 939 
 940 jvmtiError
 941 JvmtiEnvBase::get_owned_monitors(JavaThread* calling_thread, JavaThread* java_thread, javaVFrame* jvf,
 942                                  GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list) {
 943   jvmtiError err = JVMTI_ERROR_NONE;
 944 #ifdef ASSERT
 945   uint32_t debug_bits = 0;
 946 #endif
 947   // assert((SafepointSynchronize::is_at_safepoint() ||
 948   //         java_thread->is_thread_fully_suspended(false, &debug_bits)),
 949   //        "at safepoint or target thread is suspended");
 950 
 951   int depth = 0;
 952   for ( ; jvf != NULL; jvf = jvf->java_sender()) {
 953     if (MaxJavaStackTraceDepth == 0 || depth++ < MaxJavaStackTraceDepth) {  // check for stack too deep
 954       // add locked objects for this frame into list
 955       err = get_locked_objects_in_frame(calling_thread, java_thread, jvf, owned_monitors_list, depth-1);
 956       if (err != JVMTI_ERROR_NONE) {
 957         return err;
 958       }
 959     }
 960   }
 961 
 962   // Get off stack monitors. (e.g. acquired via jni MonitorEnter).
 963   JvmtiMonitorClosure jmc(calling_thread, owned_monitors_list, this);
 964   ObjectSynchronizer::monitors_iterate(&jmc, java_thread);
 965   err = jmc.error();
 966 
 967   return err;
 968 }
 969 
 970 // Save JNI local handles for any objects that this frame owns.
 971 jvmtiError
 972 JvmtiEnvBase::get_locked_objects_in_frame(JavaThread* calling_thread, JavaThread* java_thread,
 973                                  javaVFrame *jvf, GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitors_list, jint stack_depth) {
 974   jvmtiError err = JVMTI_ERROR_NONE;
 975   Thread* current_thread = Thread::current();
 976   ResourceMark rm(current_thread);
 977   HandleMark   hm(current_thread);
 978 
 979   GrowableArray<MonitorInfo*>* mons = jvf->monitors();
 980   if (mons->is_empty()) {
 981     return err;  // this javaVFrame holds no monitors
 982   }
 983 
 984   oop wait_obj = NULL;
 985   {
 986     // The ObjectMonitor* can't be async deflated since we are either
 987     // at a safepoint or the calling thread is operating on itself so
 988     // it cannot leave the underlying wait() call.
 989     // Save object of current wait() call (if any) for later comparison.

1042         // already have this object so don't include it
1043         continue;
1044       }
1045     }
1046 
1047     // add the owning object to our list
1048     jvmtiMonitorStackDepthInfo *jmsdi;
1049     err = allocate(sizeof(jvmtiMonitorStackDepthInfo), (unsigned char **)&jmsdi);
1050     if (err != JVMTI_ERROR_NONE) {
1051         return err;
1052     }
1053     Handle hobj(Thread::current(), obj);
1054     jmsdi->monitor = jni_reference(calling_thread, hobj);
1055     jmsdi->stack_depth = stack_depth;
1056     owned_monitors_list->append(jmsdi);
1057   }
1058 
1059   return err;
1060 }
1061 
1062 jvmtiError
1063 JvmtiEnvBase::get_stack_trace(javaVFrame *jvf,
1064                               jint start_depth, jint max_count,
1065                               jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
1066   Thread *current_thread = Thread::current();
1067   ResourceMark rm(current_thread);
1068   HandleMark hm(current_thread);
1069   int count = 0;
1070 
1071   if (start_depth != 0) {
1072     if (start_depth > 0) {
1073       for (int j = 0; j < start_depth && jvf != NULL; j++) {
1074         jvf = jvf->java_sender();
1075       }
1076       if (jvf == NULL) {
1077         // start_depth is deeper than the stack depth
1078         return JVMTI_ERROR_ILLEGAL_ARGUMENT;
1079       }
1080     } else { // start_depth < 0
1081       // we are referencing the starting depth based on the oldest
1082       // part of the stack.
1083       // optimize to limit the number of times that java_sender() is called
1084       javaVFrame *jvf_cursor = jvf;
1085       javaVFrame *jvf_prev = NULL;
1086       javaVFrame *jvf_prev_prev = NULL;
1087       int j = 0;
1088       while (jvf_cursor != NULL) {
1089         jvf_prev_prev = jvf_prev;
1090         jvf_prev = jvf_cursor;
1091         for (j = 0; j > start_depth && jvf_cursor != NULL; j--) {
1092           jvf_cursor = jvf_cursor->java_sender();
1093         }
1094       }
1095       if (j == start_depth) {
1096         // previous pointer is exactly where we want to start
1097         jvf = jvf_prev;
1098       } else {
1099         // we need to back up further to get to the right place
1100         if (jvf_prev_prev == NULL) {
1101           // the -start_depth is greater than the stack depth
1102           return JVMTI_ERROR_ILLEGAL_ARGUMENT;
1103         }
1104         // j now is the number of frames on the stack starting with
1105         // jvf_prev, we start from jvf_prev_prev and move older on
1106         // the stack that many, the result is -start_depth frames
1107         // remaining.
1108         jvf = jvf_prev_prev;
1109         for (; j < 0; j++) {
1110           jvf = jvf->java_sender();
1111         }
1112       }
1113     }
1114   }
1115   for (; count < max_count && jvf != NULL; count++) {
1116     frame_buffer[count].method = jvf->method()->jmethod_id();
1117     frame_buffer[count].location = (jvf->method()->is_native() ? -1 : jvf->bci());
1118     jvf = jvf->java_sender();
1119   }
1120   *count_ptr = count;
1121   return JVMTI_ERROR_NONE;
1122 }
1123 
1124 jvmtiError
1125 JvmtiEnvBase::get_stack_trace(JavaThread *java_thread,
1126                               jint start_depth, jint max_count,
1127                               jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
1128 #ifdef ASSERT
1129   uint32_t debug_bits = 0;
1130 #endif
1131   Thread *current_thread = Thread::current();
1132   assert(SafepointSynchronize::is_at_safepoint() ||
1133          java_thread->is_handshake_safe_for(current_thread),
1134          "call by myself / at safepoint / at handshake");
1135   int count = 0;
1136   jvmtiError err = JVMTI_ERROR_NONE;
1137 
1138   if (java_thread->has_last_Java_frame()) {
1139     RegisterMap reg_map(java_thread, true, true);
1140     ResourceMark rm(current_thread);
1141     javaVFrame *jvf = get_last_java_vframe(java_thread, &reg_map);
1142 
1143     err = get_stack_trace(jvf, start_depth, max_count, frame_buffer, count_ptr);
















































1144   } else {
1145     *count_ptr = 0;
1146     if (start_depth != 0) {
1147       // no frames and there is a starting depth
1148       err = JVMTI_ERROR_ILLEGAL_ARGUMENT;
1149     }
1150   }
1151   return err;
1152 }
1153 
1154 jint
1155 JvmtiEnvBase::get_frame_count(javaVFrame *jvf) {
1156   int count = 0;
1157 
1158   while (jvf != NULL) {
1159     Method* method = jvf->method();
1160     jvf = jvf->java_sender();
1161     count++;
1162   }
1163   return count;
1164 }
1165 
1166 jvmtiError
1167 JvmtiEnvBase::get_frame_count(JavaThread* jt, jint *count_ptr) {
1168   Thread *current_thread = Thread::current();
1169   assert(current_thread == jt ||
1170          SafepointSynchronize::is_at_safepoint() ||
1171          jt->is_handshake_safe_for(current_thread),
1172          "call by myself / at safepoint / at handshake");
1173 
1174   if (!jt->has_last_Java_frame()) { // no Java frames
1175     *count_ptr = 0;
1176   } else {
1177     ResourceMark rm(current_thread);
1178     RegisterMap reg_map(jt, true, true);
1179     javaVFrame *jvf = get_last_java_vframe(jt, &reg_map);
1180 
1181     *count_ptr = get_frame_count(jvf);
1182   }
1183   return JVMTI_ERROR_NONE;
1184 }
1185 
1186 jvmtiError
1187 JvmtiEnvBase::get_frame_count(oop vthread_oop, jint *count_ptr) {
1188   if (!JvmtiEnvBase::is_vthread_alive(vthread_oop)) {
1189     return JVMTI_ERROR_THREAD_NOT_ALIVE;
1190   }
1191   Thread *current_thread = Thread::current();
1192   ResourceMark rm(current_thread);
1193   javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(vthread_oop);
1194 
1195   *count_ptr = get_frame_count(jvf);
1196   return JVMTI_ERROR_NONE;
1197 }
1198 
1199 jvmtiError
1200 JvmtiEnvBase::get_frame_location(JavaThread *java_thread, jint depth,
1201                                  jmethodID* method_ptr, jlocation* location_ptr) {



1202   Thread* current_thread = Thread::current();
1203   assert(java_thread->is_handshake_safe_for(current_thread),
1204          "call by myself or at handshake");
1205   ResourceMark rm(current_thread);
1206 
1207   vframe *vf = vframeForNoProcess(java_thread, depth, true /* for cont */);
1208   if (vf == NULL) {
1209     return JVMTI_ERROR_NO_MORE_FRAMES;
1210   }
1211 
1212   // vframeFor should return a java frame. If it doesn't
1213   // it means we've got an internal error and we return the
1214   // error in product mode. In debug mode we will instead
1215   // attempt to cast the vframe to a javaVFrame and will
1216   // cause an assertion/crash to allow further diagnosis.
1217 #ifdef PRODUCT
1218   if (!vf->is_java_frame()) {
1219     return JVMTI_ERROR_INTERNAL;
1220   }
1221 #endif
1222 
1223   HandleMark hm(current_thread);
1224   javaVFrame *jvf = javaVFrame::cast(vf);
1225   Method* method = jvf->method();
1226   if (method->is_native()) {
1227     *location_ptr = -1;
1228   } else {
1229     *location_ptr = jvf->bci();
1230   }
1231   *method_ptr = method->jmethod_id();
1232 
1233   return JVMTI_ERROR_NONE;
1234 }
1235 
1236 jvmtiError
1237 JvmtiEnvBase::get_frame_location(oop vthread_oop, jint depth,
1238                                  jmethodID* method_ptr, jlocation* location_ptr) {
1239   if (!JvmtiEnvBase::is_vthread_alive(vthread_oop)) {
1240     return JVMTI_ERROR_THREAD_NOT_ALIVE;
1241   }
1242   Thread* cur_thread = Thread::current();
1243   ResourceMark rm(cur_thread);
1244   HandleMark hm(cur_thread);
1245   javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(vthread_oop);
1246   int cur_depth = 0;
1247 
1248   while (jvf != NULL && cur_depth < depth) {
1249     Method* method = jvf->method();
1250     jvf = jvf->java_sender();
1251     cur_depth++;
1252   }
1253   assert(depth >= cur_depth, "ran out of frames too soon");
1254   if (jvf == NULL) {
1255     return JVMTI_ERROR_NO_MORE_FRAMES;
1256   }
1257   Method* method = jvf->method();
1258   if (method->is_native()) {
1259     *location_ptr = -1;
1260   } else {
1261     *location_ptr = jvf->bci();
1262   }
1263   *method_ptr = method->jmethod_id();
1264 
1265   return JVMTI_ERROR_NONE;
1266 }
1267 
1268 bool
1269 JvmtiEnvBase::cthread_with_mounted_vthread(JavaThread* jt) {
1270   oop thread_oop = jt->threadObj();
1271   assert(thread_oop != NULL, "sanity check");
1272   oop mounted_vt = jt->mounted_vthread();
1273 
1274   return (mounted_vt != NULL && mounted_vt != thread_oop);
1275 }
1276 
1277 bool
1278 JvmtiEnvBase::cthread_with_continuation(JavaThread* jt) {
1279   ContinuationEntry* cont = NULL;
1280   if (jt->has_last_Java_frame()) {
1281     cont = jt->last_continuation(java_lang_VirtualThread::vthread_scope());
1282   }
1283   return (cont != NULL && cthread_with_mounted_vthread(jt));
1284 }
1285 
1286 jvmtiError
1287 JvmtiEnvBase::get_threadOop_and_JavaThread(ThreadsList* t_list, jthread thread,
1288                                            JavaThread** jt_pp, oop* thread_oop_p) {
1289   JavaThread* cur_thread = JavaThread::current();
1290   JavaThread* java_thread = NULL;
1291   oop thread_oop = NULL;
1292 
1293   if (thread == NULL) {
1294     java_thread = cur_thread;
1295     thread_oop = get_vthread_or_thread_oop(java_thread);
1296     if (thread_oop == NULL || !thread_oop->is_a(vmClasses::Thread_klass())) {
1297       return JVMTI_ERROR_INVALID_THREAD;
1298     }
1299   } else {
1300     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(t_list, thread, &java_thread, &thread_oop);
1301     if (err != JVMTI_ERROR_NONE) {
1302       // We got an error code so we don't have a JavaThread*, but only return
1303       // an error from here if we didn't get a valid thread_oop. In a vthread case
1304       // the cv_external_thread_to_JavaThread is expected to correctly set the
1305       // thread_oop and return JVMTI_ERROR_INVALID_THREAD which we ignore here.
1306       if (thread_oop == NULL || err != JVMTI_ERROR_INVALID_THREAD) {
1307         return err;
1308       }
1309     }
1310     if (java_thread == NULL && java_lang_VirtualThread::is_instance(thread_oop)) {
1311       oop cont = java_lang_VirtualThread::continuation(thread_oop);
1312       if (jdk_internal_vm_Continuation::is_mounted(cont)) {
1313         oop carrier_thread = java_lang_VirtualThread::carrier_thread(thread_oop);
1314         // Returned carrier_thread can be NULL for a mounted continuation.
1315         if (carrier_thread != NULL) {
1316           java_thread = java_lang_Thread::thread(carrier_thread);
1317         }
1318       }
1319     }
1320   }
1321   *jt_pp = java_thread;
1322   *thread_oop_p = thread_oop;
1323   return JVMTI_ERROR_NONE;
1324 }
1325 
1326 jvmtiError
1327 JvmtiEnvBase::get_object_monitor_usage(JavaThread* calling_thread, jobject object, jvmtiMonitorUsage* info_ptr) {
1328   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1329   Thread* current_thread = VMThread::vm_thread();
1330   assert(current_thread == Thread::current(), "must be");
1331 
1332   HandleMark hm(current_thread);
1333   Handle hobj;
1334 
1335   // Check arguments
1336   {
1337     oop mirror = JNIHandles::resolve_external_guard(object);
1338     NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
1339     NULL_CHECK(info_ptr, JVMTI_ERROR_NULL_POINTER);
1340 
1341     hobj = Handle(current_thread, mirror);
1342   }
1343 
1344   ThreadsListHandle tlh(current_thread);

1363         }
1364         // implied else: no owner
1365       } else {
1366         // this object has a heavyweight monitor
1367         mon = mark.monitor();
1368 
1369         // The owner field of a heavyweight monitor may be NULL for no
1370         // owner, a JavaThread * or it may still be the address of the
1371         // Lock word in a JavaThread's stack. A monitor can be inflated
1372         // by a non-owning JavaThread, but only the owning JavaThread
1373         // can change the owner field from the Lock word to the
1374         // JavaThread * and it may not have done that yet.
1375         owner = (address)mon->owner();
1376       }
1377     }
1378 
1379     if (owner != NULL) {
1380       // This monitor is owned so we have to find the owning JavaThread.
1381       owning_thread = Threads::owning_thread_from_monitor_owner(tlh.list(), owner);
1382       assert(owning_thread != NULL, "owning JavaThread must not be NULL");
1383       Handle     th(current_thread, get_vthread_or_thread_oop(owning_thread));
1384       ret.owner = (jthread)jni_reference(calling_thread, th);
1385     }
1386 
1387     if (owning_thread != NULL) {  // monitor is owned
1388       // The recursions field of a monitor does not reflect recursions
1389       // as lightweight locks before inflating the monitor are not included.
1390       // We have to count the number of recursive monitor entries the hard way.
1391       // We pass a handle to survive any GCs along the way.
1392       ret.entry_count = count_locked_objects(owning_thread, hobj);
1393     }
1394     // implied else: entry_count == 0
1395   }
1396 
1397   jint nWant = 0, nWait = 0;
1398   if (mon != NULL) {
1399     // this object has a heavyweight monitor
1400     nWant = mon->contentions(); // # of threads contending for monitor
1401     nWait = mon->waiters();     // # of threads in Object.wait()
1402     ret.waiter_count = nWant + nWait;
1403     ret.notify_waiter_count = nWait;

1426 
1427     // Number of waiters may actually be less than the waiter count.
1428     // So NULL out memory so that unused memory will be NULL.
1429     memset(ret.waiters, 0, ret.waiter_count * sizeof(jthread *));
1430     memset(ret.notify_waiters, 0, ret.notify_waiter_count * sizeof(jthread *));
1431 
1432     if (ret.waiter_count > 0) {
1433       // we have contending and/or waiting threads
1434       if (nWant > 0) {
1435         // we have contending threads
1436         ResourceMark rm(current_thread);
1437         // get_pending_threads returns only java thread so we do not need to
1438         // check for non java threads.
1439         GrowableArray<JavaThread*>* wantList = Threads::get_pending_threads(tlh.list(), nWant, (address)mon);
1440         if (wantList->length() < nWant) {
1441           // robustness: the pending list has gotten smaller
1442           nWant = wantList->length();
1443         }
1444         for (int i = 0; i < nWant; i++) {
1445           JavaThread *pending_thread = wantList->at(i);
1446           Handle th(current_thread, get_vthread_or_thread_oop(pending_thread));
1447           ret.waiters[i] = (jthread)jni_reference(calling_thread, th);
1448         }
1449       }
1450       if (nWait > 0) {
1451         // we have threads in Object.wait()
1452         int offset = nWant;  // add after any contending threads
1453         ObjectWaiter *waiter = mon->first_waiter();
1454         for (int i = 0, j = 0; i < nWait; i++) {
1455           if (waiter == NULL) {
1456             // robustness: the waiting list has gotten smaller
1457             nWait = j;
1458             break;
1459           }
1460           JavaThread *w = mon->thread_of_waiter(waiter);
1461           if (w != NULL) {
1462             // If the thread was found on the ObjectWaiter list, then
1463             // it has not been notified. This thread can't change the
1464             // state of the monitor so it doesn't need to be suspended.
1465             Handle th(current_thread, get_vthread_or_thread_oop(w));
1466             ret.waiters[offset + j] = (jthread)jni_reference(calling_thread, th);
1467             ret.notify_waiters[j++] = (jthread)jni_reference(calling_thread, th);
1468           }
1469           waiter = mon->next_waiter(waiter);
1470         }
1471       }
1472     } // ThreadsListHandle is destroyed here.
1473 
1474     // Adjust count. nWant and nWait count values may be less than original.
1475     ret.waiter_count = nWant + nWait;
1476     ret.notify_waiter_count = nWait;
1477   } else {
1478     // this object has a lightweight monitor and we have nothing more
1479     // to do here because the defaults are just fine.
1480   }
1481 
1482   // we don't update return parameter unless everything worked
1483   *info_ptr = ret;
1484 
1485   return JVMTI_ERROR_NONE;
1486 }
1487 
1488 jvmtiError
1489 JvmtiEnvBase::check_thread_list(jint count, const jthread* list) {
1490   if (list == NULL && count != 0) {
1491     return JVMTI_ERROR_NULL_POINTER;
1492   }
1493   for (int i = 0; i < count; i++) {
1494     jthread thread = list[i];
1495     oop thread_oop = JNIHandles::resolve_external_guard(thread);
1496     if (thread_oop == NULL || !thread_oop->is_a(vmClasses::VirtualThread_klass())) {
1497       return JVMTI_ERROR_INVALID_THREAD;
1498     }
1499   }
1500   return JVMTI_ERROR_NONE;
1501 }
1502 
1503 bool
1504 JvmtiEnvBase::is_in_thread_list(jint count, const jthread* list, oop jt_oop) {
1505   for (int idx = 0; idx < count; idx++) {
1506     jthread thread = list[idx];
1507     oop thread_oop = JNIHandles::resolve_external_guard(thread);
1508     if (thread_oop == jt_oop) {
1509       return true;
1510     }
1511   }
1512   return false;
1513 }
1514 
1515 jvmtiError
1516 JvmtiEnvBase::suspend_thread(oop thread_oop, JavaThread* java_thread, bool single_suspend,
1517                              int* need_safepoint_p) {
1518   JavaThread* current = JavaThread::current();
1519   HandleMark hm(current);
1520   Handle thread_h(current, thread_oop);
1521   bool is_virtual = java_lang_VirtualThread::is_instance(thread_h());
1522 
1523   if (is_virtual) {
1524     if (single_suspend) {
1525       if (JvmtiVTSuspender::is_vthread_suspended(thread_h())) {
1526         return JVMTI_ERROR_THREAD_SUSPENDED;
1527       }
1528       JvmtiVTSuspender::register_vthread_suspend(thread_h());
1529       // Check if virtual thread is mounted and there is a java_thread.
1530       // A non-NULL java_thread is always passed in the !single_suspend case.
1531       oop carrier_thread = java_lang_VirtualThread::carrier_thread(thread_h());
1532       java_thread = carrier_thread == NULL ? NULL : java_lang_Thread::thread(carrier_thread);
1533     }
1534     // The java_thread can be still blocked in VTMT transition after a previous JVMTI resume call.
1535     // There is no need to suspend the java_thread in this case. After vthread unblocking,
1536     // it will check for ext_suspend request and suspend itself if necessary.
1537     if (java_thread == NULL || java_thread->is_suspended()) {
1538       // We are done if the virtual thread is unmounted or
1539       // the java_thread is externally suspended.
1540       return JVMTI_ERROR_NONE;
1541     }
1542     // The virtual thread is mounted: suspend the java_thread.
1543   }
1544   // Don't allow hidden thread suspend request.
1545   if (java_thread->is_hidden_from_external_view()) {
1546     return JVMTI_ERROR_NONE;
1547   }
1548   bool is_passive_cthread = is_passive_carrier_thread(java_thread, thread_h());
1549 
1550   // A case of non-virtual thread.
1551   if (!is_virtual) {
1552     // Thread.suspend() is used in some tests. It sets jt->is_suspended() only.
1553     if (java_thread->is_thread_suspended() || 
1554         (!is_passive_cthread && java_thread->is_suspended())) {
1555       return JVMTI_ERROR_THREAD_SUSPENDED;
1556     }
1557     java_thread->set_thread_suspended();
1558   }
1559   assert(JvmtiVTMTDisabler::VTMT_count() == 0, "must be 0");
1560   assert(!java_thread->is_in_VTMT(), "sanity check");
1561 
1562   assert(!single_suspend || (!is_virtual && java_thread->is_thread_suspended()) ||
1563           (is_virtual && JvmtiVTSuspender::is_vthread_suspended(thread_h())),
1564          "sanity check");
1565 
1566   // An attempt to handshake-suspend a passive carrier thread will result in
1567   // suspension of mounted virtual thread. So, we just mark it as suspended
1568   // and it will be actually suspended at virtual thread unmount transition.
1569   if (!is_passive_cthread) {
1570     assert(single_suspend || is_virtual, "SuspendAllVirtualThreads should never suspend non-virtual threads");
1571     // Case of mounted virtual or attached carrier thread.
1572     if (!JvmtiSuspendControl::suspend(java_thread)) {
1573       // Thread is already suspended or in process of exiting.
1574       if (java_thread->is_exiting()) {
1575         // The thread was in the process of exiting.
1576         return JVMTI_ERROR_THREAD_NOT_ALIVE;
1577       }
1578     }
1579   }
1580   return JVMTI_ERROR_NONE;
1581 }
1582 
1583 jvmtiError
1584 JvmtiEnvBase::resume_thread(oop thread_oop, JavaThread* java_thread, bool single_suspend) {
1585   JavaThread* current = JavaThread::current();
1586   HandleMark hm(current);
1587   Handle thread_h(current, thread_oop);
1588   bool is_virtual = java_lang_VirtualThread::is_instance(thread_h());
1589 
1590   if (is_virtual) {
1591     if (single_suspend) {
1592       if (!JvmtiVTSuspender::is_vthread_suspended(thread_h())) {
1593         return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1594       }
1595       JvmtiVTSuspender::register_vthread_resume(thread_h());
1596       // Check if virtual thread is mounted and there is a java_thread.
1597       // A non-NULL java_thread is always passed in the !single_suspend case.
1598       oop carrier_thread = java_lang_VirtualThread::carrier_thread(thread_h());
1599       java_thread = carrier_thread == NULL ? NULL : java_lang_Thread::thread(carrier_thread);
1600     }
1601     // The java_thread can be still blocked in VTMT transition after a previous JVMTI suspend call.
1602     // There is no need to resume the java_thread in this case. After vthread unblocking,
1603     // it will check for is_vthread_suspended request and remain resumed if necessary.
1604     if (java_thread == NULL || !java_thread->is_suspended()) {
1605       // We are done if the virtual thread is unmounted or
1606       // the java_thread is not externally suspended.
1607       return JVMTI_ERROR_NONE;
1608     }
1609     // The virtual thread is mounted and java_thread is supended: resume the java_thread.
1610   }
1611   // Don't allow hidden thread resume request.
1612   if (java_thread->is_hidden_from_external_view()) {
1613     return JVMTI_ERROR_NONE;
1614   }
1615   // A case of a non-virtual thread.
1616   if (!is_virtual) {
1617     if (!java_thread->is_thread_suspended() &&
1618         (thread_h() == java_thread->vthread() && !java_thread->is_suspended())) {
1619       return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1620     }
1621     java_thread->clear_thread_suspended();
1622   }
1623   assert(!java_thread->is_in_VTMT(), "sanity check");
1624   if (is_virtual || thread_h() == java_thread->vthread()) {
1625     assert(single_suspend || is_virtual, "ResumeAllVirtualThreads should never resume non-virtual threads");
1626     if (java_thread->is_suspended()) {
1627       if (!JvmtiSuspendControl::resume(java_thread)) {
1628         return JVMTI_ERROR_INTERNAL;
1629       }
1630     }
1631   }
1632   return JVMTI_ERROR_NONE;
1633 }
1634 
1635 ResourceTracker::ResourceTracker(JvmtiEnv* env) {
1636   _env = env;
1637   _allocations = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<unsigned char*>(20, mtServiceability);
1638   _failed = false;
1639 }
1640 ResourceTracker::~ResourceTracker() {
1641   if (_failed) {
1642     for (int i=0; i<_allocations->length(); i++) {
1643       _env->deallocate(_allocations->at(i));
1644     }
1645   }
1646   delete _allocations;
1647 }
1648 
1649 jvmtiError ResourceTracker::allocate(jlong size, unsigned char** mem_ptr) {
1650   unsigned char *ptr;
1651   jvmtiError err = _env->allocate(size, &ptr);
1652   if (err == JVMTI_ERROR_NONE) {
1653     _allocations->append(ptr);
1654     *mem_ptr = ptr;

1661 
1662 unsigned char* ResourceTracker::allocate(jlong size) {
1663   unsigned char* ptr;
1664   allocate(size, &ptr);
1665   return ptr;
1666 }
1667 
1668 char* ResourceTracker::strdup(const char* str) {
1669   char *dup_str = (char*)allocate(strlen(str)+1);
1670   if (dup_str != NULL) {
1671     strcpy(dup_str, str);
1672   }
1673   return dup_str;
1674 }
1675 
1676 struct StackInfoNode {
1677   struct StackInfoNode *next;
1678   jvmtiStackInfo info;
1679 };
1680 
1681 
1682 // Create a jvmtiStackInfo inside a linked list node and create a
1683 // buffer for the frame information, both allocated as resource objects.
1684 // Fill in both the jvmtiStackInfo and the jvmtiFrameInfo.
1685 // Note that either or both of thr and thread_oop
1686 // may be null if the thread is new or has exited.
1687 void
1688 MultipleStackTracesCollector::fill_frames(jthread jt, JavaThread *thr, oop thread_oop) {
1689 #ifdef ASSERT
1690   Thread *current_thread = Thread::current();
1691   assert(SafepointSynchronize::is_at_safepoint() ||
1692          thr == NULL ||
1693          thr->is_handshake_safe_for(current_thread),
1694          "unmounted virtual thread / call by myself / at safepoint / at handshake");
1695 #endif
1696 
1697   jint state = 0;
1698   struct StackInfoNode *node = NEW_RESOURCE_OBJ(struct StackInfoNode);
1699   jvmtiStackInfo *infop = &(node->info);
1700 
1701   node->next = head();
1702   set_head(node);
1703   infop->frame_count = 0;
1704   infop->frame_buffer = NULL;
1705   infop->thread = jt;
1706 
1707   // Support for virtual threads
1708   if (java_lang_VirtualThread::is_instance(thread_oop)) {
1709     state = JvmtiEnvBase::get_vthread_state(thread_oop);

1710 
1711     if ((state & JVMTI_THREAD_STATE_ALIVE) != 0) {
1712       javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(thread_oop);
1713       infop->frame_buffer = NEW_RESOURCE_ARRAY(jvmtiFrameInfo, max_frame_count());
1714       _result = env()->get_stack_trace(jvf, 0, max_frame_count(),
1715                                      infop->frame_buffer, &(infop->frame_count));
1716     }














1717   } else {
1718     state = JvmtiEnvBase::get_thread_state(thread_oop, thr);
1719     if (thr != NULL && (state & JVMTI_THREAD_STATE_ALIVE) != 0) {
1720       infop->frame_buffer = NEW_RESOURCE_ARRAY(jvmtiFrameInfo, max_frame_count());
1721       _result = env()->get_stack_trace(thr, 0, max_frame_count(),
1722                                        infop->frame_buffer, &(infop->frame_count));
1723     }
1724   }
1725   _frame_count_total += infop->frame_count;
1726   infop->state = state;
1727 }
1728 
1729 // Based on the stack information in the linked list, allocate memory
1730 // block to return and fill it from the info in the linked list.
1731 void
1732 MultipleStackTracesCollector::allocate_and_fill_stacks(jint thread_count) {
1733   // do I need to worry about alignment issues?
1734   jlong alloc_size =  thread_count       * sizeof(jvmtiStackInfo)
1735                     + _frame_count_total * sizeof(jvmtiFrameInfo);
1736   env()->allocate(alloc_size, (unsigned char **)&_stack_info);
1737 
1738   // pointers to move through the newly allocated space as it is filled in
1739   jvmtiStackInfo *si = _stack_info + thread_count;      // bottom of stack info
1740   jvmtiFrameInfo *fi = (jvmtiFrameInfo *)si;            // is the top of frame info
1741 
1742   // copy information in resource area into allocated buffer
1743   // insert stack info backwards since linked list is backwards
1744   // insert frame info forwards
1745   // walk the StackInfoNodes
1746   for (struct StackInfoNode *sin = head(); sin != NULL; sin = sin->next) {

1760   assert((unsigned char *)fi == ((unsigned char *)_stack_info) + alloc_size,
1761          "the last copied frame info must be the last record");
1762 }
1763 
1764 
1765 void
1766 VM_GetThreadListStackTraces::doit() {
1767   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1768 
1769   ResourceMark rm;
1770   ThreadsListHandle tlh;
1771   for (int i = 0; i < _thread_count; ++i) {
1772     jthread jt = _thread_list[i];
1773     JavaThread* java_thread = NULL;
1774     oop thread_oop = NULL;
1775     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), jt, &java_thread, &thread_oop);
1776     if (err != JVMTI_ERROR_NONE) {
1777       // We got an error code so we don't have a JavaThread *, but
1778       // only return an error from here if we didn't get a valid
1779       // thread_oop.
1780       // In the virtual thread case the cv_external_thread_to_JavaThread is expected to correctly set
1781       // the thread_oop and return JVMTI_ERROR_INVALID_THREAD which we ignore here.
1782       if (thread_oop == NULL) {
1783         _collector.set_result(err);
1784         return;
1785       }
1786       // We have a valid thread_oop.
1787     }
1788     _collector.fill_frames(jt, java_thread, thread_oop);
1789   }
1790   _collector.allocate_and_fill_stacks(_thread_count);
1791 }
1792 
1793 void
1794 GetSingleStackTraceClosure::do_thread(Thread *target) {
1795     JavaThread *jt = JavaThread::cast(target);
1796     oop thread_oop = jt->threadObj();
1797 
1798     if (!jt->is_exiting() && thread_oop != NULL) {
1799         ResourceMark rm;
1800         _collector.fill_frames(_jthread, jt, thread_oop);
1801         _collector.allocate_and_fill_stacks(1);
1802     }
1803 }
1804 
1805 void
1806 VM_GetAllStackTraces::doit() {
1807   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1808 
1809   ResourceMark rm;
1810   _final_thread_count = 0;
1811   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
1812     oop thread_oop = jt->threadObj();
1813     if (thread_oop != NULL &&
1814         !jt->is_exiting() &&
1815         java_lang_Thread::is_alive(thread_oop) &&
1816         !jt->is_hidden_from_external_view()) {
1817       ++_final_thread_count;
1818       // Handle block of the calling thread is used to create local refs.
1819       _collector.fill_frames((jthread)JNIHandles::make_local(_calling_thread, thread_oop),
1820                              jt, thread_oop);
1821     }
1822   }

2042   for (jint idx = 0; idx < len; idx++) {
2043     array[idx] = JNIHandles::make_local(_tbl->at(idx).resolve());
2044   }
2045   _tbl = NULL;
2046   *modules_ptr = array;
2047   *module_count_ptr = len;
2048   return JVMTI_ERROR_NONE;
2049 }
2050 
2051 void
2052 UpdateForPopTopFrameClosure::doit(Thread *target, bool self) {
2053   Thread* current_thread  = Thread::current();
2054   HandleMark hm(current_thread);
2055   JavaThread* java_thread = JavaThread::cast(target);
2056 
2057   if (java_thread->is_exiting()) {
2058     return; /* JVMTI_ERROR_THREAD_NOT_ALIVE (default) */
2059   }
2060   assert(java_thread == _state->get_thread(), "Must be");
2061 
2062   if (!self && !java_thread->is_suspended() && !java_thread->is_thread_suspended()) {
2063     _result = JVMTI_ERROR_THREAD_NOT_SUSPENDED;
2064     return;
2065   }
2066 
2067   // Check to see if a PopFrame was already in progress
2068   if (java_thread->popframe_condition() != JavaThread::popframe_inactive) {
2069     // Probably possible for JVMTI clients to trigger this, but the
2070     // JPDA backend shouldn't allow this to happen
2071     _result = JVMTI_ERROR_INTERNAL;
2072     return;
2073   }
2074 
2075   // Was workaround bug
2076   //    4812902: popFrame hangs if the method is waiting at a synchronize
2077   // Catch this condition and return an error to avoid hanging.
2078   // Now JVMTI spec allows an implementation to bail out with an opaque frame error.
2079   OSThread* osThread = java_thread->osthread();
2080   if (osThread->get_state() == MONITOR_WAIT) {
2081     _result = JVMTI_ERROR_OPAQUE_FRAME;
2082     return;

2132 
2133   // It's fine to update the thread state here because no JVMTI events
2134   // shall be posted for this PopFrame.
2135 
2136   _state->update_for_pop_top_frame();
2137   java_thread->set_popframe_condition(JavaThread::popframe_pending_bit);
2138   // Set pending step flag for this popframe and it is cleared when next
2139   // step event is posted.
2140   _state->set_pending_step_for_popframe();
2141   _result = JVMTI_ERROR_NONE;
2142 }
2143 
2144 void
2145 SetFramePopClosure::doit(Thread *target, bool self) {
2146   ResourceMark rm;
2147   JavaThread* java_thread = JavaThread::cast(target);
2148 
2149   if (java_thread->is_exiting()) {
2150     return; /* JVMTI_ERROR_THREAD_NOT_ALIVE (default) */
2151   }
2152 
2153   // TBD: This might need to be corrected for detached carrier and virtual threads.
2154   assert(_state->get_thread_or_saved() == java_thread, "Must be");
2155 
2156   if (!self && !java_thread->is_suspended()) {
2157     _result = JVMTI_ERROR_THREAD_NOT_SUSPENDED;
2158     return;
2159   }
2160 
2161   vframe *vf = JvmtiEnvBase::vframeForNoProcess(java_thread, _depth);
2162   if (vf == NULL) {
2163     _result = JVMTI_ERROR_NO_MORE_FRAMES;
2164     return;
2165   }
2166   vf = JvmtiEnvBase::check_and_skip_hidden_frames(java_thread, (javaVFrame*)vf);
2167   if (!vf->is_java_frame() || ((javaVFrame*) vf)->method()->is_native()) {
2168     _result = JVMTI_ERROR_OPAQUE_FRAME;
2169     return;
2170   }
2171 
2172   assert(vf->frame_pointer() != NULL, "frame pointer mustn't be NULL");
2173   int frame_number = _state->count_frames() - _depth;
2174   _state->env_thread_state((JvmtiEnvBase*)_env)->set_frame_pop(frame_number);
2175   _result = JVMTI_ERROR_NONE;
2176 }
2177 
2178 void
2179 GetOwnedMonitorInfoClosure::do_thread(Thread *target) {
2180   JavaThread *jt = JavaThread::cast(target);
2181   if (!jt->is_exiting() && (jt->threadObj() != NULL)) {
2182     _result = ((JvmtiEnvBase *)_env)->get_owned_monitors(_calling_thread,
2183                                                          jt,
2184                                                          _owned_monitors_list);
2185   }
2186 }
2187 
2188 void
2189 GetCurrentContendedMonitorClosure::do_thread(Thread *target) {
2190   JavaThread *jt = JavaThread::cast(target);
2191   if (!jt->is_exiting() && (jt->threadObj() != NULL)) {
2192     _result = ((JvmtiEnvBase *)_env)->get_current_contended_monitor(_calling_thread,
2193                                                                     jt,
2194                                                                     _owned_monitor_ptr,
2195                                                                     _is_virtual);
2196   }
2197 }
2198 
2199 void
2200 VM_VThreadGetStackTrace::doit() {
2201   if (!JvmtiEnvBase::is_vthread_alive(_vthread_h())) {
2202     _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
2203     return;
2204   }
2205   ResourceMark rm;
2206   javaVFrame* jvf = JvmtiEnvBase::get_vthread_jvf(_vthread_h());
2207 
2208   _result = ((JvmtiEnvBase *)_env)->get_stack_trace(jvf,
2209                                                     _start_depth, _max_count,
2210                                                     _frame_buffer, _count_ptr);
2211 }
2212 
2213 void
2214 GetStackTraceClosure::do_thread(Thread *target) {
2215   JavaThread *jt = JavaThread::cast(target);
2216   if (!jt->is_exiting() && jt->threadObj() != NULL) {
2217     _result = ((JvmtiEnvBase *)_env)->get_stack_trace(jt,
2218                                                       _start_depth, _max_count,
2219                                                       _frame_buffer, _count_ptr);
2220   }
2221 }
2222 
2223 #ifdef ASSERT
2224 void
2225 PrintStackTraceClosure::do_thread_impl(Thread *target) {
2226   JavaThread *java_thread = JavaThread::cast(target);
2227   Thread *current_thread = Thread::current();
2228 
2229   ResourceMark rm (current_thread);
2230   const char* tname = JvmtiTrace::safe_get_thread_name(java_thread);
2231   oop t_oop = java_thread->mounted_vthread();
2232   t_oop = t_oop == NULL ? java_thread->threadObj() : t_oop;
2233   bool is_vt_suspended = java_lang_VirtualThread::is_instance(t_oop) && JvmtiVTSuspender::is_vthread_suspended(t_oop);
2234 
2235   tty->print_cr("%s(%s) exiting: %d is_susp: %d is_thread_susp: %d is_vthread_susp: %d is_VTMT_disabler: %d, is_in_VTMT = %d",
2236                 tname, java_thread->name(), java_thread->is_exiting(),
2237                 java_thread->is_suspended(), java_thread->is_thread_suspended(), is_vt_suspended,
2238                 java_thread->is_VTMT_disabler(), java_thread->is_in_VTMT());
2239 
2240   if (java_thread->has_last_Java_frame()) {
2241     RegisterMap reg_map(java_thread, true, true);
2242     ResourceMark rm(current_thread);
2243     HandleMark hm(current_thread);
2244     javaVFrame *jvf = JvmtiEnvBase::get_last_java_vframe(java_thread, &reg_map);
2245     while (jvf != NULL) {
2246       tty->print_cr("  %s:%d",
2247                     jvf->method()->external_name(),
2248                     jvf->method()->line_number_from_bci(jvf->bci()));
2249       jvf = jvf->java_sender();
2250     }
2251   }
2252   tty->print_cr("");
2253 }
2254 
2255 void
2256 PrintStackTraceClosure::do_thread(Thread *target) {
2257   JavaThread *java_thread = JavaThread::cast(target);
2258   Thread *current_thread = Thread::current();
2259 
2260   assert(SafepointSynchronize::is_at_safepoint() ||
2261       java_thread->is_handshake_safe_for(current_thread),
2262          "call by myself / at safepoint / at handshake");
2263 
2264   PrintStackTraceClosure::do_thread_impl(target);
2265 }
2266 #endif
2267 
2268 void
2269 VM_VThreadGetFrameCount::doit() {
2270   _result = ((JvmtiEnvBase*)_env)->get_frame_count(_vthread_h(), _count_ptr);
2271 }
2272 
2273 void
2274 GetFrameCountClosure::do_thread(Thread *target) {
2275   JavaThread* jt = JavaThread::cast(target);
2276   assert(target == jt, "just checking");
2277   if (!jt->is_exiting() && jt->threadObj() != NULL) {
2278     _result = ((JvmtiEnvBase*)_env)->get_frame_count(jt, _count_ptr);
2279   }
2280 }
2281 
2282 void
2283 GetFrameLocationClosure::do_thread(Thread *target) {
2284   JavaThread *jt = JavaThread::cast(target);
2285   if (!jt->is_exiting() && jt->threadObj() != NULL) {
2286     _result = ((JvmtiEnvBase*)_env)->get_frame_location(jt, _depth,
2287                                                         _method_ptr, _location_ptr);
2288   }
2289 }
2290 
2291 void
2292 VThreadGetOwnedMonitorInfoClosure::do_thread(Thread *target) {
2293   if (!JvmtiEnvBase::is_vthread_alive(_vthread_h())) {
2294     _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
2295     return;
2296   }
2297   JavaThread* java_thread = JavaThread::cast(target);
2298   Thread* cur_thread = Thread::current();
2299   ResourceMark rm(cur_thread);
2300   HandleMark hm(cur_thread);
2301 
2302   javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(_vthread_h());
2303 
2304   if (!java_thread->is_exiting() && java_thread->threadObj() != NULL) {
2305     _result = ((JvmtiEnvBase *)_env)->get_owned_monitors((JavaThread*)target,
2306                                                          java_thread,
2307                                                          jvf,
2308                                                          _owned_monitors_list);
2309   }
2310 }
2311 
2312 void
2313 VThreadGetThreadClosure::do_thread(Thread *target) {
2314   assert(target->is_Java_thread(), "just checking");
2315   JavaThread *jt = JavaThread::cast(target);
2316   oop carrier_thread = java_lang_VirtualThread::carrier_thread(_vthread_h());
2317   *_carrier_thread_ptr = (jthread)JNIHandles::make_local(jt, carrier_thread);
2318 }
2319 
2320 void
2321 VThreadGetStackTraceClosure::do_thread(Thread *target) {
2322   assert(target->is_Java_thread(), "just checking");
2323   if (!JvmtiEnvBase::is_vthread_alive(_vthread_h())) {
2324     _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
2325     return;
2326   }
2327   Thread* cur_thread = Thread::current();
2328   ResourceMark rm(cur_thread);
2329   HandleMark hm(cur_thread);
2330 
2331   javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(_vthread_h());
2332   _result = ((JvmtiEnvBase *)_env)->get_stack_trace(jvf,
2333                                                     _start_depth, _max_count,
2334                                                     _frame_buffer, _count_ptr);
2335 }
2336 
2337 void
2338 VThreadGetFrameCountClosure::do_thread(Thread *target) {
2339   assert(target->is_Java_thread(), "just checking");
2340   _result = ((JvmtiEnvBase*)_env)->get_frame_count(_vthread_h(), _count_ptr);
2341 }
2342 
2343 void
2344 VThreadGetFrameLocationClosure::do_thread(Thread *target) {
2345   assert(target->is_Java_thread(), "just checking");
2346   _result = ((JvmtiEnvBase*)_env)->get_frame_location(_vthread_h(), _depth,
2347                                                       _method_ptr, _location_ptr);
2348 }
2349 
2350 void
2351 VThreadGetThreadStateClosure::do_thread(Thread *target) {
2352   assert(target->is_Java_thread(), "just checking");
2353   jshort vthread_state = java_lang_VirtualThread::state(_vthread_h());
2354   oop carrier_thread_oop = java_lang_VirtualThread::carrier_thread(_vthread_h());
2355   jint state;
2356 
2357   if (vthread_state == java_lang_VirtualThread::RUNNING && carrier_thread_oop != NULL) {
2358     state = (jint) java_lang_Thread::get_thread_status(carrier_thread_oop);
2359     JavaThread* java_thread = java_lang_Thread::thread(carrier_thread_oop);
2360     if (java_thread->is_suspended()) {
2361       state |= JVMTI_THREAD_STATE_SUSPENDED;
2362     }
2363   } else {
2364     state = (jint) java_lang_VirtualThread::map_state_to_thread_status(vthread_state);
2365   }
2366   if (java_lang_Thread::interrupted(_vthread_h())) {
2367     state |= JVMTI_THREAD_STATE_INTERRUPTED;
2368   }
2369   *_state_ptr = state;
2370   _result = JVMTI_ERROR_NONE;
2371 }
< prev index next >