< prev index next >

src/hotspot/share/prims/jvmtiEnv.cpp

Print this page

  49 #include "prims/jvmtiClassFileReconstituter.hpp"
  50 #include "prims/jvmtiCodeBlobEvents.hpp"
  51 #include "prims/jvmtiExtensions.hpp"
  52 #include "prims/jvmtiGetLoadedClasses.hpp"
  53 #include "prims/jvmtiImpl.hpp"
  54 #include "prims/jvmtiManageCapabilities.hpp"
  55 #include "prims/jvmtiRawMonitor.hpp"
  56 #include "prims/jvmtiRedefineClasses.hpp"
  57 #include "prims/jvmtiTagMap.hpp"
  58 #include "prims/jvmtiThreadState.inline.hpp"
  59 #include "prims/jvmtiUtil.hpp"
  60 #include "runtime/arguments.hpp"
  61 #include "runtime/deoptimization.hpp"
  62 #include "runtime/fieldDescriptor.inline.hpp"
  63 #include "runtime/handles.inline.hpp"
  64 #include "runtime/interfaceSupport.inline.hpp"
  65 #include "runtime/javaCalls.hpp"
  66 #include "runtime/jfieldIDWorkaround.hpp"
  67 #include "runtime/jniHandles.inline.hpp"
  68 #include "runtime/objectMonitor.inline.hpp"

  69 #include "runtime/osThread.hpp"
  70 #include "runtime/reflectionUtils.hpp"
  71 #include "runtime/signature.hpp"
  72 #include "runtime/thread.inline.hpp"
  73 #include "runtime/threadHeapSampler.hpp"
  74 #include "runtime/threadSMR.hpp"
  75 #include "runtime/timerTrace.hpp"
  76 #include "runtime/vframe.inline.hpp"
  77 #include "runtime/vmThread.hpp"
  78 #include "services/threadService.hpp"
  79 #include "utilities/exceptions.hpp"
  80 #include "utilities/preserveException.hpp"
  81 #include "utilities/utf8.hpp"
  82 
  83 
  84 #define FIXLATER 0 // REMOVE this when completed.
  85 
  86  // FIXLATER: hook into JvmtiTrace
  87 #define TraceJVMTICalls false
  88 

 123 //
 124 // end file prefix
 125 
 126   //
 127   // Memory Management functions
 128   //
 129 
 130 // mem_ptr - pre-checked for NULL
 131 jvmtiError
 132 JvmtiEnv::Allocate(jlong size, unsigned char** mem_ptr) {
 133   return allocate(size, mem_ptr);
 134 } /* end Allocate */
 135 
 136 
 137 // mem - NULL is a valid value, must be checked
 138 jvmtiError
 139 JvmtiEnv::Deallocate(unsigned char* mem) {
 140   return deallocate(mem);
 141 } /* end Deallocate */
 142 
 143 // java_thread - protected by ThreadsListHandle and pre-checked
 144 // data - NULL is a valid value, must be checked
 145 jvmtiError
 146 JvmtiEnv::SetThreadLocalStorage(JavaThread* java_thread, const void* data) {
 147   JvmtiThreadState* state = java_thread->jvmti_thread_state();

















 148   if (state == NULL) {
 149     if (data == NULL) {
 150       // leaving state unset same as data set to NULL
 151       return JVMTI_ERROR_NONE;
 152     }
 153     // otherwise, create the state
 154     state = JvmtiThreadState::state_for(java_thread);


 155     if (state == NULL) {
 156       return JVMTI_ERROR_THREAD_NOT_ALIVE;
 157     }
 158   }
 159   state->env_thread_state(this)->set_agent_thread_local_storage_data((void*)data);
 160   return JVMTI_ERROR_NONE;
 161 } /* end SetThreadLocalStorage */
 162 
 163 
 164 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
 165 // data_ptr - pre-checked for NULL
 166 jvmtiError
 167 JvmtiEnv::GetThreadLocalStorage(jthread thread, void** data_ptr) {
 168   JavaThread* current_thread = JavaThread::current();
 169   if (thread == NULL) {
 170     JvmtiThreadState* state = current_thread->jvmti_thread_state();
 171     *data_ptr = (state == NULL) ? NULL :
 172       state->env_thread_state(this)->get_agent_thread_local_storage_data();
 173   } else {
 174     // jvmti_GetThreadLocalStorage is "in native" and doesn't transition
 175     // the thread to _thread_in_vm. However, when the TLS for a thread
 176     // other than the current thread is required we need to transition
 177     // from native so as to resolve the jthread.
 178 
 179     MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, current_thread));
 180     ThreadInVMfromNative __tiv(current_thread);
 181     VM_ENTRY_BASE(jvmtiError, JvmtiEnv::GetThreadLocalStorage , current_thread)
 182     debug_only(VMNativeEntryWrapper __vew;)
 183 
 184     JavaThread* java_thread = NULL;


 185     ThreadsListHandle tlh(current_thread);
 186     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, NULL);

 187     if (err != JVMTI_ERROR_NONE) {
 188       return err;
 189     }
 190 
 191     JvmtiThreadState* state = java_thread->jvmti_thread_state();


 192     *data_ptr = (state == NULL) ? NULL :
 193       state->env_thread_state(this)->get_agent_thread_local_storage_data();
 194   }
 195   return JVMTI_ERROR_NONE;
 196 } /* end GetThreadLocalStorage */
 197 
 198   //
 199   // Module functions
 200   //
 201 
 202 // module_count_ptr - pre-checked for NULL
 203 // modules_ptr - pre-checked for NULL
 204 jvmtiError
 205 JvmtiEnv::GetAllModules(jint* module_count_ptr, jobject** modules_ptr) {
 206     JvmtiModuleClosure jmc;
 207 
 208     return jmc.get_all_modules(this, module_count_ptr, modules_ptr);
 209 } /* end GetAllModules */
 210 
 211 
 212 // class_loader - NULL is a valid value, must be pre-checked
 213 // package_name - pre-checked for NULL
 214 // module_ptr - pre-checked for NULL
 215 jvmtiError
 216 JvmtiEnv::GetNamedModule(jobject class_loader, const char* package_name, jobject* module_ptr) {
 217   JavaThread* THREAD = JavaThread::current(); // For exception macros.
 218   ResourceMark rm(THREAD);

 219   Handle h_loader (THREAD, JNIHandles::resolve(class_loader));
 220   // Check that loader is a subclass of java.lang.ClassLoader.
 221   if (h_loader.not_null() && !java_lang_ClassLoader::is_subclass(h_loader->klass())) {
 222     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 223   }
 224   oop module = Modules::get_named_module(h_loader, package_name);
 225   *module_ptr = module != NULL ? JNIHandles::make_local(THREAD, module) : NULL;
 226   return JVMTI_ERROR_NONE;
 227 } /* end GetNamedModule */
 228 
 229 
 230 // module - pre-checked for NULL
 231 // to_module - pre-checked for NULL
 232 jvmtiError
 233 JvmtiEnv::AddModuleReads(jobject module, jobject to_module) {
 234   JavaThread* THREAD = JavaThread::current(); // For exception macros.
 235 
 236   // check module
 237   Handle h_module(THREAD, JNIHandles::resolve(module));
 238   if (!java_lang_Module::is_instance(h_module())) {

 515     return set_native_method_prefixes(prefix_count, prefixes);
 516   }
 517 } /* end SetNativeMethodPrefixes */
 518 
 519   //
 520   // Event Management functions
 521   //
 522 
 523 // callbacks - NULL is a valid value, must be checked
 524 // size_of_callbacks - pre-checked to be greater than or equal to 0
 525 jvmtiError
 526 JvmtiEnv::SetEventCallbacks(const jvmtiEventCallbacks* callbacks, jint size_of_callbacks) {
 527   JvmtiEventController::set_event_callbacks(this, callbacks, size_of_callbacks);
 528   return JVMTI_ERROR_NONE;
 529 } /* end SetEventCallbacks */
 530 
 531 
 532 // event_thread - NULL is a valid value, must be checked
 533 jvmtiError
 534 JvmtiEnv::SetEventNotificationMode(jvmtiEventMode mode, jvmtiEvent event_type, jthread event_thread,   ...) {
















 535   if (event_thread == NULL) {
 536     // Can be called at Agent_OnLoad() time with event_thread == NULL
 537     // when Thread::current() does not work yet so we cannot create a
 538     // ThreadsListHandle that is common to both thread-specific and
 539     // global code paths.
 540 
 541     // event_type must be valid
 542     if (!JvmtiEventController::is_valid_event_type(event_type)) {
 543       return JVMTI_ERROR_INVALID_EVENT_TYPE;
 544     }
 545 
 546     bool enabled = (mode == JVMTI_ENABLE);
 547 
 548     // assure that needed capabilities are present
 549     if (enabled && !JvmtiUtil::has_event_capability(event_type, get_capabilities())) {
 550       return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
 551     }
 552 
 553     if (event_type == JVMTI_EVENT_CLASS_FILE_LOAD_HOOK && enabled) {
 554       record_class_file_load_hook_enabled();
 555     }
 556 
 557     JvmtiEventController::set_user_enabled(this, (JavaThread*) NULL, event_type, enabled);
 558   } else {
 559     // We have a specified event_thread.
 560     JavaThread* java_thread = NULL;


 561     ThreadsListHandle tlh;
 562     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), event_thread, &java_thread, NULL);

 563     if (err != JVMTI_ERROR_NONE) {
 564       return err;
 565     }
 566 
 567     // event_type must be valid
 568     if (!JvmtiEventController::is_valid_event_type(event_type)) {
 569       return JVMTI_ERROR_INVALID_EVENT_TYPE;
 570     }
 571 
 572     // global events cannot be controlled at thread level.
 573     if (JvmtiEventController::is_global_event(event_type)) {
 574       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 575     }
 576 
 577     bool enabled = (mode == JVMTI_ENABLE);
 578 
 579     // assure that needed capabilities are present
 580     if (enabled && !JvmtiUtil::has_event_capability(event_type, get_capabilities())) {
 581       return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
 582     }
 583 
 584     if (event_type == JVMTI_EVENT_CLASS_FILE_LOAD_HOOK && enabled) {
 585       record_class_file_load_hook_enabled();
 586     }
 587     JvmtiEventController::set_user_enabled(this, java_thread, event_type, enabled);
 588   }
 589 
 590   return JVMTI_ERROR_NONE;
 591 } /* end SetEventNotificationMode */
 592 
 593   //
 594   // Capability functions
 595   //
 596 
 597 // capabilities_ptr - pre-checked for NULL
 598 jvmtiError
 599 JvmtiEnv::GetPotentialCapabilities(jvmtiCapabilities* capabilities_ptr) {
 600   JvmtiManageCapabilities::get_potential_capabilities(get_capabilities(),
 601                                                       get_prohibited_capabilities(),
 602                                                       capabilities_ptr);
 603   return JVMTI_ERROR_NONE;
 604 } /* end GetPotentialCapabilities */
 605 
 606 
 607 // capabilities_ptr - pre-checked for NULL

 820     LogConfiguration::configure_stdout(level, true, LOG_TAGS(jni, resolve));
 821     break;
 822   default:
 823     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 824   };
 825   return JVMTI_ERROR_NONE;
 826 } /* end SetVerboseFlag */
 827 
 828 
 829 // format_ptr - pre-checked for NULL
 830 jvmtiError
 831 JvmtiEnv::GetJLocationFormat(jvmtiJlocationFormat* format_ptr) {
 832   *format_ptr = JVMTI_JLOCATION_JVMBCI;
 833   return JVMTI_ERROR_NONE;
 834 } /* end GetJLocationFormat */
 835 
 836   //
 837   // Thread functions
 838   //
 839 
 840 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
 841 // thread_state_ptr - pre-checked for NULL
 842 jvmtiError
 843 JvmtiEnv::GetThreadState(jthread thread, jint* thread_state_ptr) {
 844   JavaThread* current_thread = JavaThread::current();
 845   JavaThread* java_thread = NULL;
 846   oop thread_oop = NULL;

 847   ThreadsListHandle tlh(current_thread);
 848 
 849   if (thread == NULL) {
 850     java_thread = current_thread;
 851     thread_oop = java_thread->threadObj();
 852 
 853     if (thread_oop == NULL || !thread_oop->is_a(vmClasses::Thread_klass())) {
 854       return JVMTI_ERROR_INVALID_THREAD;
 855     }
 856   } else {
 857     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
 858     if (err != JVMTI_ERROR_NONE) {
 859       // We got an error code so we don't have a JavaThread *, but
 860       // only return an error from here if we didn't get a valid
 861       // thread_oop.


 862       if (thread_oop == NULL) {
 863         return err;
 864       }
 865       // We have a valid thread_oop so we can return some thread state.
 866     }
 867   }
 868 
 869   // get most state bits
 870   jint state = (jint)java_lang_Thread::get_thread_status(thread_oop);
 871 
 872   if (java_thread != NULL) {
 873     // We have a JavaThread* so add more state bits.
 874     JavaThreadState jts = java_thread->thread_state();
 875 
 876     if (java_thread->is_suspended()) {
 877       state |= JVMTI_THREAD_STATE_SUSPENDED;
 878     }
 879     if (jts == _thread_in_native) {
 880       state |= JVMTI_THREAD_STATE_IN_NATIVE;
 881     }
 882     if (java_thread->is_interrupted(false)) {
 883       state |= JVMTI_THREAD_STATE_INTERRUPTED;
 884     }
 885   }
 886 
 887   *thread_state_ptr = state;

 888   return JVMTI_ERROR_NONE;
 889 } /* end GetThreadState */
 890 
 891 
 892 // thread_ptr - pre-checked for NULL
 893 jvmtiError
 894 JvmtiEnv::GetCurrentThread(jthread* thread_ptr) {
 895   JavaThread* current_thread  = JavaThread::current();
 896   *thread_ptr = (jthread)JNIHandles::make_local(current_thread, current_thread->threadObj());


 897   return JVMTI_ERROR_NONE;
 898 } /* end GetCurrentThread */
 899 
 900 
 901 // threads_count_ptr - pre-checked for NULL
 902 // threads_ptr - pre-checked for NULL
 903 jvmtiError
 904 JvmtiEnv::GetAllThreads(jint* threads_count_ptr, jthread** threads_ptr) {
 905   int nthreads        = 0;
 906   Handle *thread_objs = NULL;
 907   Thread* current_thread = Thread::current();
 908   ResourceMark rm(current_thread);
 909   HandleMark hm(current_thread);
 910 
 911   // enumerate threads (including agent threads)
 912   ThreadsListEnumerator tle(current_thread, true);
 913   nthreads = tle.num_threads();
 914   *threads_count_ptr = nthreads;
 915 
 916   if (nthreads == 0) {
 917     *threads_ptr = NULL;
 918     return JVMTI_ERROR_NONE;
 919   }
 920 
 921   thread_objs = NEW_RESOURCE_ARRAY(Handle, nthreads);
 922   NULL_CHECK(thread_objs, JVMTI_ERROR_OUT_OF_MEMORY);
 923 
 924   for (int i = 0; i < nthreads; i++) {
 925     thread_objs[i] = Handle(tle.get_threadObj(i));
 926   }
 927 
 928   jthread *jthreads  = new_jthreadArray(nthreads, thread_objs);
 929   NULL_CHECK(jthreads, JVMTI_ERROR_OUT_OF_MEMORY);
 930 
 931   *threads_ptr = jthreads;
 932   return JVMTI_ERROR_NONE;
 933 } /* end GetAllThreads */
 934 
 935 
 936 // java_thread - protected by ThreadsListHandle and pre-checked
 937 jvmtiError
 938 JvmtiEnv::SuspendThread(JavaThread* java_thread) {
 939   // don't allow hidden thread suspend request.
 940   if (java_thread->is_hidden_from_external_view()) {
 941     return JVMTI_ERROR_NONE;
 942   }
 943   if (java_thread->is_suspended()) {
 944     return JVMTI_ERROR_THREAD_SUSPENDED;


 945   }
 946   if (!JvmtiSuspendControl::suspend(java_thread)) {
 947     // Either the thread is already suspended or
 948     // it was in the process of exiting.
 949     if (java_thread->is_exiting()) {
 950       return JVMTI_ERROR_THREAD_NOT_ALIVE;
 951     }
 952     return JVMTI_ERROR_THREAD_SUSPENDED;
 953   }
 954   return JVMTI_ERROR_NONE;




 955 } /* end SuspendThread */
 956 
 957 
 958 // request_count - pre-checked to be greater than or equal to 0
 959 // request_list - pre-checked for NULL
 960 // results - pre-checked for NULL
 961 jvmtiError
 962 JvmtiEnv::SuspendThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
 963   int self_index = -1;
 964   int needSafepoint = 0;  // > 0 if we need a safepoint
 965   JavaThread* current = JavaThread::current();


 966   ThreadsListHandle tlh(current);

 967   for (int i = 0; i < request_count; i++) {
 968     JavaThread *java_thread = NULL;
 969     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), request_list[i], &java_thread, NULL);
 970     if (err != JVMTI_ERROR_NONE) {
 971       results[i] = err;
 972       continue;
 973     }
 974     // don't allow hidden thread suspend request.
 975     if (java_thread->is_hidden_from_external_view()) {
 976       results[i] = JVMTI_ERROR_NONE;  // indicate successful suspend
 977       continue;
 978     }
 979     if (java_thread->is_suspended()) {
 980       results[i] = JVMTI_ERROR_THREAD_SUSPENDED;
 981       continue;
 982     }
 983     if (java_thread == current) {
 984       self_index = i;
 985       continue;
 986     }
 987     if (!JvmtiSuspendControl::suspend(java_thread)) {
 988       // Either the thread is already suspended or
 989       // it was in the process of exiting.
 990       if (java_thread->is_exiting()) {
 991         results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
 992         continue;
 993       }
 994       results[i] = JVMTI_ERROR_THREAD_SUSPENDED;
 995       continue;
 996     }
 997     results[i] = JVMTI_ERROR_NONE;  // indicate successful suspend
 998   }
 999   if (self_index >= 0) {
1000     if (!JvmtiSuspendControl::suspend(current)) {
1001       // Either the thread is already suspended or
1002       // it was in the process of exiting.
1003       if (current->is_exiting()) {
1004         results[self_index] = JVMTI_ERROR_THREAD_NOT_ALIVE;
1005       } else {
1006         results[self_index] = JVMTI_ERROR_THREAD_SUSPENDED;
1007       }
1008     } else {
1009       results[self_index] = JVMTI_ERROR_NONE;  // indicate successful suspend
1010     }




1011   }
1012   // per-thread suspend results returned via results parameter
1013   return JVMTI_ERROR_NONE;
1014 } /* end SuspendThreadList */
1015 
1016 
1017 // java_thread - protected by ThreadsListHandle and pre-checked
1018 jvmtiError
1019 JvmtiEnv::ResumeThread(JavaThread* java_thread) {
1020   // don't allow hidden thread resume request.
1021   if (java_thread->is_hidden_from_external_view()) {
1022     return JVMTI_ERROR_NONE;
















1023   }
1024   if (!java_thread->is_suspended()) {
1025     return JVMTI_ERROR_THREAD_NOT_SUSPENDED;















1026   }
1027   if (!JvmtiSuspendControl::resume(java_thread)) {
1028     return JVMTI_ERROR_INTERNAL;







1029   }
1030   return JVMTI_ERROR_NONE;
















1031 } /* end ResumeThread */
1032 
1033 
1034 // request_count - pre-checked to be greater than or equal to 0
1035 // request_list - pre-checked for NULL
1036 // results - pre-checked for NULL
1037 jvmtiError
1038 JvmtiEnv::ResumeThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {



1039   ThreadsListHandle tlh;

1040   for (int i = 0; i < request_count; i++) {
1041     JavaThread* java_thread = NULL;
1042     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), request_list[i], &java_thread, NULL);
1043     if (err != JVMTI_ERROR_NONE) {
1044       results[i] = err;
1045       continue;
1046     }
1047     // don't allow hidden thread resume request.
1048     if (java_thread->is_hidden_from_external_view()) {
1049       results[i] = JVMTI_ERROR_NONE;  // indicate successful resume
1050       continue;
1051     }
1052     if (!java_thread->is_suspended()) {
1053       results[i] = JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1054       continue;
























1055     }

1056 
1057     if (!JvmtiSuspendControl::resume(java_thread)) {
1058       results[i] = JVMTI_ERROR_INTERNAL;
1059       continue;









1060     }


1061 
1062     results[i] = JVMTI_ERROR_NONE;  // indicate successful resume






1063   }
1064   // per-thread resume results returned via results parameter
1065   return JVMTI_ERROR_NONE;
1066 } /* end ResumeThreadList */
1067 
1068 
1069 // java_thread - protected by ThreadsListHandle and pre-checked
1070 jvmtiError
1071 JvmtiEnv::StopThread(JavaThread* java_thread, jobject exception) {
1072   oop e = JNIHandles::resolve_external_guard(exception);
1073   NULL_CHECK(e, JVMTI_ERROR_NULL_POINTER);
1074 
1075   JavaThread::send_async_exception(java_thread, e);
1076 
1077   return JVMTI_ERROR_NONE;
1078 
1079 } /* end StopThread */
1080 
1081 
1082 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1083 jvmtiError
1084 JvmtiEnv::InterruptThread(jthread thread) {
1085   JavaThread* current_thread  = JavaThread::current();
1086   JavaThread* java_thread = NULL;




1087   ThreadsListHandle tlh(current_thread);
1088   jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, NULL);

1089   if (err != JVMTI_ERROR_NONE) {
1090     return err;
1091   }















1092   // Really this should be a Java call to Thread.interrupt to ensure the same
1093   // semantics, however historically this has not been done for some reason.
1094   // So we continue with that (which means we don't interact with any Java-level
1095   // Interruptible object) but we must set the Java-level interrupted state.
1096   java_lang_Thread::set_interrupted(JNIHandles::resolve(thread), true);
1097   java_thread->interrupt();
1098 
1099   return JVMTI_ERROR_NONE;
1100 } /* end InterruptThread */
1101 
1102 
1103 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1104 // info_ptr - pre-checked for NULL
1105 jvmtiError
1106 JvmtiEnv::GetThreadInfo(jthread thread, jvmtiThreadInfo* info_ptr) {
1107   JavaThread* current_thread = JavaThread::current();
1108   ResourceMark rm(current_thread);
1109   HandleMark hm(current_thread);


1110 
1111   ThreadsListHandle tlh(current_thread);
1112 
1113   // if thread is NULL the current thread is used
1114   oop thread_oop = NULL;
1115   if (thread == NULL) {
1116     thread_oop = current_thread->threadObj();

1117     if (thread_oop == NULL || !thread_oop->is_a(vmClasses::Thread_klass())) {
1118       return JVMTI_ERROR_INVALID_THREAD;
1119     }
1120   } else {
1121     JavaThread* java_thread = NULL;
1122     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1123     if (err != JVMTI_ERROR_NONE) {
1124       // We got an error code so we don't have a JavaThread *, but
1125       // only return an error from here if we didn't get a valid
1126       // thread_oop.


1127       if (thread_oop == NULL) {
1128         return err;
1129       }
1130       // We have a valid thread_oop so we can return some thread info.
1131     }
1132   }

1133 
1134   Handle thread_obj(current_thread, thread_oop);
1135   Handle name;
1136   ThreadPriority priority;
1137   Handle     thread_group;
1138   Handle context_class_loader;
1139   bool          is_daemon;
1140 
1141   name = Handle(current_thread, java_lang_Thread::name(thread_obj()));
1142   priority = java_lang_Thread::priority(thread_obj());
1143   thread_group = Handle(current_thread, java_lang_Thread::threadGroup(thread_obj()));
1144   is_daemon = java_lang_Thread::is_daemon(thread_obj());
















1145 
1146   oop loader = java_lang_Thread::context_class_loader(thread_obj());


1147   context_class_loader = Handle(current_thread, loader);
1148 
1149   { const char *n;
1150 
1151     if (name() != NULL) {
1152       n = java_lang_String::as_utf8_string(name());
1153     } else {
1154       int utf8_length = 0;
1155       n = UNICODE::as_utf8((jchar*) NULL, utf8_length);
1156     }
1157 
1158     info_ptr->name = (char *) jvmtiMalloc(strlen(n)+1);
1159     if (info_ptr->name == NULL)
1160       return JVMTI_ERROR_OUT_OF_MEMORY;
1161 
1162     strcpy(info_ptr->name, n);
1163   }
1164   info_ptr->is_daemon = is_daemon;
1165   info_ptr->priority  = priority;
1166 
1167   info_ptr->context_class_loader = (context_class_loader.is_null()) ? NULL :
1168                                      jni_reference(context_class_loader);
1169   info_ptr->thread_group = jni_reference(thread_group);
1170 
1171   return JVMTI_ERROR_NONE;
1172 } /* end GetThreadInfo */
1173 
1174 
1175 // java_thread - protected by ThreadsListHandle and pre-checked
1176 // owned_monitor_count_ptr - pre-checked for NULL
1177 // owned_monitors_ptr - pre-checked for NULL
1178 jvmtiError
1179 JvmtiEnv::GetOwnedMonitorInfo(JavaThread* java_thread, jint* owned_monitor_count_ptr, jobject** owned_monitors_ptr) {
1180   jvmtiError err = JVMTI_ERROR_NONE;
1181   JavaThread* calling_thread = JavaThread::current();
1182 
1183   EscapeBarrier eb(true, calling_thread, java_thread);
1184   if (!eb.deoptimize_objects(MaxJavaStackTraceDepth)) {
1185     return JVMTI_ERROR_OUT_OF_MEMORY;
1186   }
1187 
1188   // growable array of jvmti monitors info on the C-heap
1189   GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list =
1190       new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<jvmtiMonitorStackDepthInfo*>(1, mtServiceability);
1191 
1192   // It is only safe to perform the direct operation on the current
1193   // thread. All other usage needs to use a direct handshake for safety.
1194   if (java_thread == calling_thread) {
1195     err = get_owned_monitors(calling_thread, java_thread, owned_monitors_list);















1196   } else {
1197     // get owned monitors info with handshake
1198     GetOwnedMonitorInfoClosure op(calling_thread, this, owned_monitors_list);
1199     Handshake::execute(&op, java_thread);
1200     err = op.result();














1201   }

1202   jint owned_monitor_count = owned_monitors_list->length();
1203   if (err == JVMTI_ERROR_NONE) {
1204     if ((err = allocate(owned_monitor_count * sizeof(jobject *),
1205                       (unsigned char**)owned_monitors_ptr)) == JVMTI_ERROR_NONE) {
1206       // copy into the returned array
1207       for (int i = 0; i < owned_monitor_count; i++) {
1208         (*owned_monitors_ptr)[i] =
1209           ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->monitor;
1210       }
1211       *owned_monitor_count_ptr = owned_monitor_count;
1212     }
1213   }
1214   // clean up.
1215   for (int i = 0; i < owned_monitor_count; i++) {
1216     deallocate((unsigned char*)owned_monitors_list->at(i));
1217   }
1218   delete owned_monitors_list;
1219 
1220   return err;
1221 } /* end GetOwnedMonitorInfo */
1222 
1223 
1224 // java_thread - protected by ThreadsListHandle and pre-checked
1225 // monitor_info_count_ptr - pre-checked for NULL
1226 // monitor_info_ptr - pre-checked for NULL
1227 jvmtiError
1228 JvmtiEnv::GetOwnedMonitorStackDepthInfo(JavaThread* java_thread, jint* monitor_info_count_ptr, jvmtiMonitorStackDepthInfo** monitor_info_ptr) {
1229   jvmtiError err = JVMTI_ERROR_NONE;
1230   JavaThread* calling_thread = JavaThread::current();
1231 
1232   EscapeBarrier eb(true, calling_thread, java_thread);
1233   if (!eb.deoptimize_objects(MaxJavaStackTraceDepth)) {
1234     return JVMTI_ERROR_OUT_OF_MEMORY;
1235   }
1236 
1237   // growable array of jvmti monitors info on the C-heap
1238   GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list =
1239          new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<jvmtiMonitorStackDepthInfo*>(1, mtServiceability);
1240 
1241   // It is only safe to perform the direct operation on the current
1242   // thread. All other usage needs to use a direct handshake for safety.
1243   if (java_thread == calling_thread) {
1244     err = get_owned_monitors(calling_thread, java_thread, owned_monitors_list);
1245   } else {
1246     // get owned monitors info with handshake
1247     GetOwnedMonitorInfoClosure op(calling_thread, this, owned_monitors_list);
1248     Handshake::execute(&op, java_thread);
1249     err = op.result();
1250   }
1251 






























1252   jint owned_monitor_count = owned_monitors_list->length();
1253   if (err == JVMTI_ERROR_NONE) {
1254     if ((err = allocate(owned_monitor_count * sizeof(jvmtiMonitorStackDepthInfo),
1255                       (unsigned char**)monitor_info_ptr)) == JVMTI_ERROR_NONE) {
1256       // copy to output array.
1257       for (int i = 0; i < owned_monitor_count; i++) {
1258         (*monitor_info_ptr)[i].monitor =
1259           ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->monitor;
1260         (*monitor_info_ptr)[i].stack_depth =
1261           ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->stack_depth;
1262       }
1263     }
1264     *monitor_info_count_ptr = owned_monitor_count;
1265   }
1266 
1267   // clean up.
1268   for (int i = 0; i < owned_monitor_count; i++) {
1269     deallocate((unsigned char*)owned_monitors_list->at(i));
1270   }
1271   delete owned_monitors_list;
1272 
1273   return err;
1274 } /* end GetOwnedMonitorStackDepthInfo */
1275 
1276 
1277 // java_thread - protected by ThreadsListHandle and pre-checked
1278 // monitor_ptr - pre-checked for NULL
1279 jvmtiError
1280 JvmtiEnv::GetCurrentContendedMonitor(JavaThread* java_thread, jobject* monitor_ptr) {
1281   jvmtiError err = JVMTI_ERROR_NONE;
1282   JavaThread* calling_thread = JavaThread::current();



1283 
1284   // It is only safe to perform the direct operation on the current
1285   // thread. All other usage needs to use a direct handshake for safety.
1286   if (java_thread == calling_thread) {





























1287     err = get_current_contended_monitor(calling_thread, java_thread, monitor_ptr);
1288   } else {
1289     // get contended monitor information with handshake
1290     GetCurrentContendedMonitorClosure op(calling_thread, this, monitor_ptr);
1291     Handshake::execute(&op, java_thread);
1292     err = op.result();
1293   }
1294   return err;
1295 } /* end GetCurrentContendedMonitor */
1296 
1297 
1298 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1299 // proc - pre-checked for NULL
1300 // arg - NULL is a valid value, must be checked
1301 jvmtiError
1302 JvmtiEnv::RunAgentThread(jthread thread, jvmtiStartFunction proc, const void* arg, jint priority) {
1303   JavaThread* current_thread = JavaThread::current();
1304 
1305   JavaThread* java_thread = NULL;
1306   oop thread_oop = NULL;
1307   ThreadsListHandle tlh(current_thread);
1308   jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1309   if (err != JVMTI_ERROR_NONE) {
1310     // We got an error code so we don't have a JavaThread *, but
1311     // only return an error from here if we didn't get a valid
1312     // thread_oop.
1313     if (thread_oop == NULL) {
1314       return err;
1315     }
1316     // We have a valid thread_oop.
1317   }
1318 
1319   if (java_thread != NULL) {
1320     // 'thread' refers to an existing JavaThread.
1321     return JVMTI_ERROR_INVALID_THREAD;
1322   }



1323 
1324   if (priority < JVMTI_THREAD_MIN_PRIORITY || priority > JVMTI_THREAD_MAX_PRIORITY) {
1325     return JVMTI_ERROR_INVALID_PRIORITY;
1326   }
1327 
1328   Handle thread_hndl(current_thread, thread_oop);
1329 
1330   JvmtiAgentThread* new_thread = new JvmtiAgentThread(this, proc, arg);
1331 
1332   // At this point it may be possible that no osthread was created for the
1333   // JavaThread due to lack of resources.
1334   if (new_thread->osthread() == NULL) {
1335     // The new thread is not known to Thread-SMR yet so we can just delete.
1336     delete new_thread;
1337     return JVMTI_ERROR_OUT_OF_MEMORY;
1338   }
1339 
1340   JavaThread::start_internal_daemon(current_thread, new_thread, thread_hndl,
1341                                     (ThreadPriority)priority);
1342 

1368     Handle system_thread_group(current_thread, Universe::system_thread_group());
1369     *groups_ptr[0] = jni_reference(system_thread_group);
1370   }
1371 
1372   return JVMTI_ERROR_NONE;
1373 } /* end GetTopThreadGroups */
1374 
1375 
1376 // info_ptr - pre-checked for NULL
1377 jvmtiError
1378 JvmtiEnv::GetThreadGroupInfo(jthreadGroup group, jvmtiThreadGroupInfo* info_ptr) {
1379   Thread* current_thread = Thread::current();
1380   ResourceMark rm(current_thread);
1381   HandleMark hm(current_thread);
1382 
1383   Handle group_obj (current_thread, JNIHandles::resolve_external_guard(group));
1384   NULL_CHECK(group_obj(), JVMTI_ERROR_INVALID_THREAD_GROUP);
1385 
1386   const char* name;
1387   Handle parent_group;
1388   bool is_daemon;
1389   ThreadPriority max_priority;
1390 
1391   name         = java_lang_ThreadGroup::name(group_obj());
1392   parent_group = Handle(current_thread, java_lang_ThreadGroup::parent(group_obj()));
1393   is_daemon    = java_lang_ThreadGroup::is_daemon(group_obj());
1394   max_priority = java_lang_ThreadGroup::maxPriority(group_obj());
1395 
1396   info_ptr->is_daemon    = is_daemon;
1397   info_ptr->max_priority = max_priority;
1398   info_ptr->parent       = jni_reference(parent_group);
1399 
1400   if (name != NULL) {
1401     info_ptr->name = (char*)jvmtiMalloc(strlen(name)+1);
1402     NULL_CHECK(info_ptr->name, JVMTI_ERROR_OUT_OF_MEMORY);
1403     strcpy(info_ptr->name, name);
1404   } else {
1405     info_ptr->name = NULL;
1406   }
1407 




1408   return JVMTI_ERROR_NONE;
1409 } /* end GetThreadGroupInfo */
1410 
1411 
1412 // thread_count_ptr - pre-checked for NULL
1413 // threads_ptr - pre-checked for NULL
1414 // group_count_ptr - pre-checked for NULL
1415 // groups_ptr - pre-checked for NULL
1416 jvmtiError
1417 JvmtiEnv::GetThreadGroupChildren(jthreadGroup group, jint* thread_count_ptr, jthread** threads_ptr, jint* group_count_ptr, jthreadGroup** groups_ptr) {
1418   JavaThread* current_thread = JavaThread::current();
1419   oop group_obj = JNIHandles::resolve_external_guard(group);
1420   NULL_CHECK(group_obj, JVMTI_ERROR_INVALID_THREAD_GROUP);
1421 
1422   Handle *thread_objs = NULL;
1423   Handle *group_objs  = NULL;
1424   int nthreads = 0;
1425   int ngroups = 0;
1426   int hidden_threads = 0;
1427 
1428   ResourceMark rm(current_thread);
1429   HandleMark hm(current_thread);
1430 
1431   Handle group_hdl(current_thread, group_obj);
1432 
1433   { // Cannot allow thread or group counts to change.
1434     ObjectLocker ol(group_hdl, current_thread);
1435 
1436     nthreads = java_lang_ThreadGroup::nthreads(group_hdl());
1437     ngroups  = java_lang_ThreadGroup::ngroups(group_hdl());
1438 
1439     if (nthreads > 0) {
1440       ThreadsListHandle tlh(current_thread);
1441       objArrayOop threads = java_lang_ThreadGroup::threads(group_hdl());
1442       assert(nthreads <= threads->length(), "too many threads");
1443       thread_objs = NEW_RESOURCE_ARRAY(Handle,nthreads);
1444       for (int i = 0, j = 0; i < nthreads; i++) {
1445         oop thread_obj = threads->obj_at(i);
1446         assert(thread_obj != NULL, "thread_obj is NULL");
1447         JavaThread *java_thread = NULL;
1448         jvmtiError err = JvmtiExport::cv_oop_to_JavaThread(tlh.list(), thread_obj, &java_thread);
1449         if (err == JVMTI_ERROR_NONE) {
1450           // Have a valid JavaThread*.
1451           if (java_thread->is_hidden_from_external_view()) {
1452             // Filter out hidden java threads.
1453             hidden_threads++;
1454             continue;
1455           }
1456         } else {
1457           // We couldn't convert thread_obj into a JavaThread*.
1458           if (err == JVMTI_ERROR_INVALID_THREAD) {
1459             // The thread_obj does not refer to a java.lang.Thread object
1460             // so skip it.
1461             hidden_threads++;
1462             continue;
1463           }
1464           // We have a valid thread_obj, but no JavaThread*; the caller
1465           // can still have limited use for the thread_obj.
1466         }
1467         thread_objs[j++] = Handle(current_thread, thread_obj);
1468       }
1469       nthreads -= hidden_threads;
1470     } // ThreadsListHandle is destroyed here.
1471 
1472     if (ngroups > 0) {
1473       objArrayOop groups = java_lang_ThreadGroup::groups(group_hdl());
1474       assert(ngroups <= groups->length(), "too many groups");
1475       group_objs = NEW_RESOURCE_ARRAY(Handle,ngroups);
1476       for (int i = 0; i < ngroups; i++) {
1477         oop group_obj = groups->obj_at(i);
1478         assert(group_obj != NULL, "group_obj != NULL");
1479         group_objs[i] = Handle(current_thread, group_obj);
1480       }
1481     }
1482   } // ThreadGroup unlocked here
1483 
1484   *group_count_ptr  = ngroups;
1485   *thread_count_ptr = nthreads;
1486   *threads_ptr     = new_jthreadArray(nthreads, thread_objs);
1487   *groups_ptr      = new_jthreadGroupArray(ngroups, group_objs);
1488   if ((nthreads > 0) && (*threads_ptr == NULL)) {
1489     return JVMTI_ERROR_OUT_OF_MEMORY;
1490   }
1491   if ((ngroups > 0) && (*groups_ptr == NULL)) {
1492     return JVMTI_ERROR_OUT_OF_MEMORY;
1493   }
1494 
1495   return JVMTI_ERROR_NONE;
1496 } /* end GetThreadGroupChildren */
1497 
1498 
1499   //
1500   // Stack Frame functions
1501   //
1502 
1503 // java_thread - protected by ThreadsListHandle and pre-checked
1504 // max_frame_count - pre-checked to be greater than or equal to 0
1505 // frame_buffer - pre-checked for NULL
1506 // count_ptr - pre-checked for NULL
1507 jvmtiError
1508 JvmtiEnv::GetStackTrace(JavaThread* java_thread, jint start_depth, jint max_frame_count, jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
1509   jvmtiError err = JVMTI_ERROR_NONE;





























1510 
1511   // It is only safe to perform the direct operation on the current
1512   // thread. All other usage needs to use a direct handshake for safety.
1513   if (java_thread == JavaThread::current()) {
1514     err = get_stack_trace(java_thread, start_depth, max_frame_count, frame_buffer, count_ptr);
1515   } else {
1516     // Get stack trace with handshake.
1517     GetStackTraceClosure op(this, start_depth, max_frame_count, frame_buffer, count_ptr);
1518     Handshake::execute(&op, java_thread);
1519     err = op.result();
1520   }
1521 
1522   return err;
1523 } /* end GetStackTrace */
1524 
1525 
1526 // max_frame_count - pre-checked to be greater than or equal to 0
1527 // stack_info_ptr - pre-checked for NULL
1528 // thread_count_ptr - pre-checked for NULL
1529 jvmtiError

1533 
1534   // JVMTI get stack traces at safepoint.
1535   VM_GetAllStackTraces op(this, calling_thread, max_frame_count);
1536   VMThread::execute(&op);
1537   *thread_count_ptr = op.final_thread_count();
1538   *stack_info_ptr = op.stack_info();
1539   err = op.result();
1540   return err;
1541 } /* end GetAllStackTraces */
1542 
1543 
1544 // thread_count - pre-checked to be greater than or equal to 0
1545 // thread_list - pre-checked for NULL
1546 // max_frame_count - pre-checked to be greater than or equal to 0
1547 // stack_info_ptr - pre-checked for NULL
1548 jvmtiError
1549 JvmtiEnv::GetThreadListStackTraces(jint thread_count, const jthread* thread_list, jint max_frame_count, jvmtiStackInfo** stack_info_ptr) {
1550   jvmtiError err = JVMTI_ERROR_NONE;
1551 
1552   if (thread_count == 1) {


1553     // Use direct handshake if we need to get only one stack trace.
1554     JavaThread *current_thread = JavaThread::current();
1555     ThreadsListHandle tlh(current_thread);

1556     JavaThread *java_thread;
1557     err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), *thread_list, &java_thread, NULL);

1558     if (err != JVMTI_ERROR_NONE) {
1559       return err;
1560     }
1561 
1562     GetSingleStackTraceClosure op(this, current_thread, *thread_list, max_frame_count);












1563     Handshake::execute(&op, java_thread);
1564     err = op.result();
1565     if (err == JVMTI_ERROR_NONE) {
1566       *stack_info_ptr = op.stack_info();
1567     }
1568   } else {
1569     // JVMTI get stack traces at safepoint.
1570     VM_GetThreadListStackTraces op(this, thread_count, thread_list, max_frame_count);
1571     VMThread::execute(&op);
1572     err = op.result();
1573     if (err == JVMTI_ERROR_NONE) {
1574       *stack_info_ptr = op.stack_info();
1575     }
1576   }
1577   return err;
1578 } /* end GetThreadListStackTraces */
1579 
1580 
1581 // java_thread - protected by ThreadsListHandle and pre-checked
1582 // count_ptr - pre-checked for NULL
1583 jvmtiError
1584 JvmtiEnv::GetFrameCount(JavaThread* java_thread, jint* count_ptr) {
1585   jvmtiError err = JVMTI_ERROR_NONE;




1586 
1587   // retrieve or create JvmtiThreadState.
1588   JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
1589   if (state == NULL) {
1590     return JVMTI_ERROR_THREAD_NOT_ALIVE;














1591   }
1592 
1593   // It is only safe to perform the direct operation on the current
1594   // thread. All other usage needs to use a direct handshake for safety.
1595   if (java_thread == JavaThread::current()) {
1596     err = get_frame_count(state, count_ptr);
1597   } else {
1598     // get java stack frame count with handshake.
1599     GetFrameCountClosure op(this, state, count_ptr);
1600     Handshake::execute(&op, java_thread);
1601     err = op.result();
1602   }
1603   return err;
1604 } /* end GetFrameCount */
1605 
1606 
1607 // java_thread - protected by ThreadsListHandle and pre-checked
1608 jvmtiError
1609 JvmtiEnv::PopFrame(JavaThread* java_thread) {
1610   // retrieve or create the state
1611   JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
1612   if (state == NULL) {
1613     return JVMTI_ERROR_THREAD_NOT_ALIVE;
1614   }
1615 
1616   // Eagerly reallocate scalar replaced objects.
1617   JavaThread* current_thread = JavaThread::current();
1618   EscapeBarrier eb(true, current_thread, java_thread);
1619   if (!eb.deoptimize_objects(1)) {
1620     // Reallocation of scalar replaced objects failed -> return with error
1621     return JVMTI_ERROR_OUT_OF_MEMORY;
1622   }
1623 
1624   MutexLocker mu(JvmtiThreadState_lock);
1625   UpdateForPopTopFrameClosure op(state);
1626   if (java_thread == current_thread) {
1627     op.doit(java_thread, true /* self */);
1628   } else {
1629     Handshake::execute(&op, java_thread);
1630   }
1631   return op.result();
1632 } /* end PopFrame */
1633 
1634 
1635 // java_thread - protected by ThreadsListHandle and pre-checked
1636 // depth - pre-checked as non-negative
1637 // method_ptr - pre-checked for NULL
1638 // location_ptr - pre-checked for NULL
1639 jvmtiError
1640 JvmtiEnv::GetFrameLocation(JavaThread* java_thread, jint depth, jmethodID* method_ptr, jlocation* location_ptr) {
1641   jvmtiError err = JVMTI_ERROR_NONE;
























1642 
1643   // It is only safe to perform the direct operation on the current
1644   // thread. All other usage needs to use a direct handshake for safety.
1645   if (java_thread == JavaThread::current()) {
1646     err = get_frame_location(java_thread, depth, method_ptr, location_ptr);
1647   } else {
1648     // JVMTI get java stack frame location via direct handshake.
1649     GetFrameLocationClosure op(this, depth, method_ptr, location_ptr);
1650     Handshake::execute(&op, java_thread);
1651     err = op.result();
1652   }
1653   return err;
1654 } /* end GetFrameLocation */
1655 
1656 
1657 // java_thread - protected by ThreadsListHandle and pre-checked
1658 // depth - pre-checked as non-negative
1659 jvmtiError
1660 JvmtiEnv::NotifyFramePop(JavaThread* java_thread, jint depth) {
1661   JvmtiThreadState *state = JvmtiThreadState::state_for(java_thread);































1662   if (state == NULL) {
1663     return JVMTI_ERROR_THREAD_NOT_ALIVE;
1664   }
1665 
1666   SetFramePopClosure op(this, state, depth);
1667   MutexLocker mu(JvmtiThreadState_lock);
1668   if (java_thread == JavaThread::current()) {
1669     op.doit(java_thread, true /* self */);
1670   } else {
1671     Handshake::execute(&op, java_thread);
1672   }
1673   return op.result();
1674 } /* end NotifyFramePop */
1675 
1676 
1677   //
1678   // Force Early Return functions
1679   //
1680 
1681 // java_thread - protected by ThreadsListHandle and pre-checked
1682 jvmtiError
1683 JvmtiEnv::ForceEarlyReturnObject(JavaThread* java_thread, jobject value) {
1684   jvalue val;
1685   val.l = value;
1686   return force_early_return(java_thread, val, atos);
1687 } /* end ForceEarlyReturnObject */
1688 

1884 jvmtiError
1885 JvmtiEnv::IterateOverInstancesOfClass(oop k_mirror, jvmtiHeapObjectFilter object_filter, jvmtiHeapObjectCallback heap_object_callback, const void* user_data) {
1886   if (java_lang_Class::is_primitive(k_mirror)) {
1887     // DO PRIMITIVE CLASS PROCESSING
1888     return JVMTI_ERROR_NONE;
1889   }
1890   Klass* klass = java_lang_Class::as_Klass(k_mirror);
1891   if (klass == NULL) {
1892     return JVMTI_ERROR_INVALID_CLASS;
1893   }
1894   TraceTime t("IterateOverInstancesOfClass", TRACETIME_LOG(Debug, jvmti, objecttagging));
1895   JvmtiTagMap::tag_map_for(this)->iterate_over_heap(object_filter, klass, heap_object_callback, user_data);
1896   return JVMTI_ERROR_NONE;
1897 } /* end IterateOverInstancesOfClass */
1898 
1899 
1900   //
1901   // Local Variable functions
1902   //
1903 
1904 // java_thread - protected by ThreadsListHandle and pre-checked
1905 // depth - pre-checked as non-negative
1906 // value_ptr - pre-checked for NULL
1907 jvmtiError
1908 JvmtiEnv::GetLocalObject(JavaThread* java_thread, jint depth, jint slot, jobject* value_ptr) {


1909   JavaThread* current_thread = JavaThread::current();
1910   // rm object is created to clean up the javaVFrame created in
1911   // doit_prologue(), but after doit() is finished with it.
1912   ResourceMark rm(current_thread);


1913 
1914   VM_GetOrSetLocal op(java_thread, current_thread, depth, slot);
1915   VMThread::execute(&op);
1916   jvmtiError err = op.result();
1917   if (err != JVMTI_ERROR_NONE) {
1918     return err;




1919   } else {
1920     *value_ptr = op.value().l;
1921     return JVMTI_ERROR_NONE;










1922   }

1923 } /* end GetLocalObject */
1924 
1925 // java_thread - protected by ThreadsListHandle and pre-checked
1926 // depth - pre-checked as non-negative
1927 // value - pre-checked for NULL
1928 jvmtiError
1929 JvmtiEnv::GetLocalInstance(JavaThread* java_thread, jint depth, jobject* value_ptr){


1930   JavaThread* current_thread = JavaThread::current();
1931   // rm object is created to clean up the javaVFrame created in
1932   // doit_prologue(), but after doit() is finished with it.
1933   ResourceMark rm(current_thread);


1934 
1935   VM_GetReceiver op(java_thread, current_thread, depth);
1936   VMThread::execute(&op);
1937   jvmtiError err = op.result();
1938   if (err != JVMTI_ERROR_NONE) {
1939     return err;




1940   } else {
1941     *value_ptr = op.value().l;
1942     return JVMTI_ERROR_NONE;










1943   }

1944 } /* end GetLocalInstance */
1945 
1946 
1947 // java_thread - protected by ThreadsListHandle and pre-checked
1948 // depth - pre-checked as non-negative
1949 // value_ptr - pre-checked for NULL
1950 jvmtiError
1951 JvmtiEnv::GetLocalInt(JavaThread* java_thread, jint depth, jint slot, jint* value_ptr) {



1952   // rm object is created to clean up the javaVFrame created in
1953   // doit_prologue(), but after doit() is finished with it.
1954   ResourceMark rm;


1955 
1956   VM_GetOrSetLocal op(java_thread, depth, slot, T_INT);
1957   VMThread::execute(&op);
1958   *value_ptr = op.value().i;
1959   return op.result();




















1960 } /* end GetLocalInt */
1961 
1962 
1963 // java_thread - protected by ThreadsListHandle and pre-checked
1964 // depth - pre-checked as non-negative
1965 // value_ptr - pre-checked for NULL
1966 jvmtiError
1967 JvmtiEnv::GetLocalLong(JavaThread* java_thread, jint depth, jint slot, jlong* value_ptr) {



1968   // rm object is created to clean up the javaVFrame created in
1969   // doit_prologue(), but after doit() is finished with it.
1970   ResourceMark rm;


1971 
1972   VM_GetOrSetLocal op(java_thread, depth, slot, T_LONG);
1973   VMThread::execute(&op);
1974   *value_ptr = op.value().j;
1975   return op.result();




















1976 } /* end GetLocalLong */
1977 
1978 
1979 // java_thread - protected by ThreadsListHandle and pre-checked
1980 // depth - pre-checked as non-negative
1981 // value_ptr - pre-checked for NULL
1982 jvmtiError
1983 JvmtiEnv::GetLocalFloat(JavaThread* java_thread, jint depth, jint slot, jfloat* value_ptr) {



1984   // rm object is created to clean up the javaVFrame created in
1985   // doit_prologue(), but after doit() is finished with it.
1986   ResourceMark rm;


1987 
1988   VM_GetOrSetLocal op(java_thread, depth, slot, T_FLOAT);
1989   VMThread::execute(&op);
1990   *value_ptr = op.value().f;
1991   return op.result();




















1992 } /* end GetLocalFloat */
1993 
1994 
1995 // java_thread - protected by ThreadsListHandle and pre-checked
1996 // depth - pre-checked as non-negative
1997 // value_ptr - pre-checked for NULL
1998 jvmtiError
1999 JvmtiEnv::GetLocalDouble(JavaThread* java_thread, jint depth, jint slot, jdouble* value_ptr) {



2000   // rm object is created to clean up the javaVFrame created in
2001   // doit_prologue(), but after doit() is finished with it.
2002   ResourceMark rm;


2003 
2004   VM_GetOrSetLocal op(java_thread, depth, slot, T_DOUBLE);
2005   VMThread::execute(&op);
2006   *value_ptr = op.value().d;
2007   return op.result();




















2008 } /* end GetLocalDouble */
2009 
2010 
2011 // java_thread - protected by ThreadsListHandle and pre-checked
2012 // depth - pre-checked as non-negative
2013 jvmtiError
2014 JvmtiEnv::SetLocalObject(JavaThread* java_thread, jint depth, jint slot, jobject value) {
2015   // rm object is created to clean up the javaVFrame created in
2016   // doit_prologue(), but after doit() is finished with it.
2017   ResourceMark rm;
2018   jvalue val;
2019   val.l = value;
2020   VM_GetOrSetLocal op(java_thread, depth, slot, T_OBJECT, val);
2021   VMThread::execute(&op);
2022   return op.result();
2023 } /* end SetLocalObject */
2024 
2025 
2026 // java_thread - protected by ThreadsListHandle and pre-checked
2027 // depth - pre-checked as non-negative

3288 jvmtiError
3289 JvmtiEnv::SetExtensionEventCallback(jint extension_event_index, jvmtiExtensionEvent callback) {
3290   return JvmtiExtensions::set_event_callback(this, extension_event_index, callback);
3291 } /* end SetExtensionEventCallback */
3292 
3293   //
3294   // Timers functions
3295   //
3296 
3297 // info_ptr - pre-checked for NULL
3298 jvmtiError
3299 JvmtiEnv::GetCurrentThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) {
3300   os::current_thread_cpu_time_info(info_ptr);
3301   return JVMTI_ERROR_NONE;
3302 } /* end GetCurrentThreadCpuTimerInfo */
3303 
3304 
3305 // nanos_ptr - pre-checked for NULL
3306 jvmtiError
3307 JvmtiEnv::GetCurrentThreadCpuTime(jlong* nanos_ptr) {










3308   *nanos_ptr = os::current_thread_cpu_time();
3309   return JVMTI_ERROR_NONE;
3310 } /* end GetCurrentThreadCpuTime */
3311 
3312 
3313 // info_ptr - pre-checked for NULL
3314 jvmtiError
3315 JvmtiEnv::GetThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) {
3316   os::thread_cpu_time_info(info_ptr);
3317   return JVMTI_ERROR_NONE;
3318 } /* end GetThreadCpuTimerInfo */
3319 
3320 
3321 // java_thread - protected by ThreadsListHandle and pre-checked
3322 // nanos_ptr - pre-checked for NULL
3323 jvmtiError
3324 JvmtiEnv::GetThreadCpuTime(JavaThread* java_thread, jlong* nanos_ptr) {
3325   *nanos_ptr = os::thread_cpu_time(java_thread);
3326   return JVMTI_ERROR_NONE;
3327 } /* end GetThreadCpuTime */

  49 #include "prims/jvmtiClassFileReconstituter.hpp"
  50 #include "prims/jvmtiCodeBlobEvents.hpp"
  51 #include "prims/jvmtiExtensions.hpp"
  52 #include "prims/jvmtiGetLoadedClasses.hpp"
  53 #include "prims/jvmtiImpl.hpp"
  54 #include "prims/jvmtiManageCapabilities.hpp"
  55 #include "prims/jvmtiRawMonitor.hpp"
  56 #include "prims/jvmtiRedefineClasses.hpp"
  57 #include "prims/jvmtiTagMap.hpp"
  58 #include "prims/jvmtiThreadState.inline.hpp"
  59 #include "prims/jvmtiUtil.hpp"
  60 #include "runtime/arguments.hpp"
  61 #include "runtime/deoptimization.hpp"
  62 #include "runtime/fieldDescriptor.inline.hpp"
  63 #include "runtime/handles.inline.hpp"
  64 #include "runtime/interfaceSupport.inline.hpp"
  65 #include "runtime/javaCalls.hpp"
  66 #include "runtime/jfieldIDWorkaround.hpp"
  67 #include "runtime/jniHandles.inline.hpp"
  68 #include "runtime/objectMonitor.inline.hpp"
  69 #include "runtime/os.hpp"
  70 #include "runtime/osThread.hpp"
  71 #include "runtime/reflectionUtils.hpp"
  72 #include "runtime/signature.hpp"
  73 #include "runtime/thread.inline.hpp"
  74 #include "runtime/threadHeapSampler.hpp"
  75 #include "runtime/threadSMR.hpp"
  76 #include "runtime/timerTrace.hpp"
  77 #include "runtime/vframe.inline.hpp"
  78 #include "runtime/vmThread.hpp"
  79 #include "services/threadService.hpp"
  80 #include "utilities/exceptions.hpp"
  81 #include "utilities/preserveException.hpp"
  82 #include "utilities/utf8.hpp"
  83 
  84 
  85 #define FIXLATER 0 // REMOVE this when completed.
  86 
  87  // FIXLATER: hook into JvmtiTrace
  88 #define TraceJVMTICalls false
  89 

 124 //
 125 // end file prefix
 126 
 127   //
 128   // Memory Management functions
 129   //
 130 
 131 // mem_ptr - pre-checked for NULL
 132 jvmtiError
 133 JvmtiEnv::Allocate(jlong size, unsigned char** mem_ptr) {
 134   return allocate(size, mem_ptr);
 135 } /* end Allocate */
 136 
 137 
 138 // mem - NULL is a valid value, must be checked
 139 jvmtiError
 140 JvmtiEnv::Deallocate(unsigned char* mem) {
 141   return deallocate(mem);
 142 } /* end Deallocate */
 143 
 144 // Threads_lock NOT held
 145 // data - NULL is a valid value, must be checked
 146 jvmtiError
 147 JvmtiEnv::SetThreadLocalStorage(jthread thread, const void* data) {
 148   jvmtiError err = JVMTI_ERROR_NONE;
 149   JavaThread* java_thread = NULL;
 150   JvmtiThreadState* state = NULL;
 151   JvmtiVTMTDisabler vtmt_disabler;
 152   oop thread_obj = NULL;
 153   JavaThread* current = JavaThread::current();
 154 
 155   if (thread == NULL) {
 156     java_thread = current;
 157     state = java_thread->jvmti_thread_state();
 158   } else {
 159     ThreadsListHandle tlh;
 160     err = get_threadOop_and_JavaThread(tlh.list(), thread, &java_thread, &thread_obj);
 161     if (err != JVMTI_ERROR_NONE) {
 162       return err;
 163     }
 164     state = java_lang_Thread::jvmti_thread_state(thread_obj);
 165   }
 166   if (state == NULL) {
 167     if (data == NULL) {
 168       // leaving state unset same as data set to NULL
 169       return JVMTI_ERROR_NONE;
 170     }
 171     // otherwise, create the state
 172     HandleMark hm(current);
 173     Handle thread_handle(current, thread_obj);
 174     state = JvmtiThreadState::state_for(java_thread, thread_handle);
 175     if (state == NULL) {
 176       return JVMTI_ERROR_THREAD_NOT_ALIVE;
 177     }
 178   }
 179   state->env_thread_state(this)->set_agent_thread_local_storage_data((void*)data);
 180   return JVMTI_ERROR_NONE;
 181 } /* end SetThreadLocalStorage */
 182 
 183 

 184 // data_ptr - pre-checked for NULL
 185 jvmtiError
 186 JvmtiEnv::GetThreadLocalStorage(jthread thread, void** data_ptr) {
 187   JavaThread* current_thread = JavaThread::current();
 188   if (thread == NULL) {
 189     JvmtiThreadState* state = current_thread->jvmti_thread_state();
 190     *data_ptr = (state == NULL) ? NULL :
 191       state->env_thread_state(this)->get_agent_thread_local_storage_data();
 192   } else {
 193     // jvmti_GetThreadLocalStorage is "in native" and doesn't transition
 194     // the thread to _thread_in_vm. However, when the TLS for a thread
 195     // other than the current thread is required we need to transition
 196     // from native so as to resolve the jthread.
 197 
 198     MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, current_thread));
 199     ThreadInVMfromNative __tiv(current_thread);
 200     VM_ENTRY_BASE(jvmtiError, JvmtiEnv::GetThreadLocalStorage , current_thread)
 201     debug_only(VMNativeEntryWrapper __vew;)
 202 
 203     JavaThread* java_thread = NULL;
 204     oop thread_obj = NULL;
 205     JvmtiVTMTDisabler vtmt_disabler;
 206     ThreadsListHandle tlh(current_thread);
 207 
 208     jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, &java_thread, &thread_obj);
 209     if (err != JVMTI_ERROR_NONE) {
 210       return err;
 211     }
 212 
 213     HandleMark hm(current_thread);
 214     Handle thread_handle(current_thread, thread_obj);
 215     JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread, thread_handle);
 216     *data_ptr = (state == NULL) ? NULL :
 217       state->env_thread_state(this)->get_agent_thread_local_storage_data();
 218   }
 219   return JVMTI_ERROR_NONE;
 220 } /* end GetThreadLocalStorage */
 221 
 222   //
 223   // Module functions
 224   //
 225 
 226 // module_count_ptr - pre-checked for NULL
 227 // modules_ptr - pre-checked for NULL
 228 jvmtiError
 229 JvmtiEnv::GetAllModules(jint* module_count_ptr, jobject** modules_ptr) {
 230     JvmtiModuleClosure jmc;
 231 
 232     return jmc.get_all_modules(this, module_count_ptr, modules_ptr);
 233 } /* end GetAllModules */
 234 
 235 
 236 // class_loader - NULL is a valid value, must be pre-checked
 237 // package_name - pre-checked for NULL
 238 // module_ptr - pre-checked for NULL
 239 jvmtiError
 240 JvmtiEnv::GetNamedModule(jobject class_loader, const char* package_name, jobject* module_ptr) {
 241   JavaThread* THREAD = JavaThread::current(); // For exception macros.
 242   ResourceMark rm(THREAD);
 243 
 244   Handle h_loader (THREAD, JNIHandles::resolve(class_loader));
 245   // Check that loader is a subclass of java.lang.ClassLoader.
 246   if (h_loader.not_null() && !java_lang_ClassLoader::is_subclass(h_loader->klass())) {
 247     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 248   }
 249   oop module = Modules::get_named_module(h_loader, package_name);
 250   *module_ptr = module != NULL ? JNIHandles::make_local(THREAD, module) : NULL;
 251   return JVMTI_ERROR_NONE;
 252 } /* end GetNamedModule */
 253 
 254 
 255 // module - pre-checked for NULL
 256 // to_module - pre-checked for NULL
 257 jvmtiError
 258 JvmtiEnv::AddModuleReads(jobject module, jobject to_module) {
 259   JavaThread* THREAD = JavaThread::current(); // For exception macros.
 260 
 261   // check module
 262   Handle h_module(THREAD, JNIHandles::resolve(module));
 263   if (!java_lang_Module::is_instance(h_module())) {

 540     return set_native_method_prefixes(prefix_count, prefixes);
 541   }
 542 } /* end SetNativeMethodPrefixes */
 543 
 544   //
 545   // Event Management functions
 546   //
 547 
 548 // callbacks - NULL is a valid value, must be checked
 549 // size_of_callbacks - pre-checked to be greater than or equal to 0
 550 jvmtiError
 551 JvmtiEnv::SetEventCallbacks(const jvmtiEventCallbacks* callbacks, jint size_of_callbacks) {
 552   JvmtiEventController::set_event_callbacks(this, callbacks, size_of_callbacks);
 553   return JVMTI_ERROR_NONE;
 554 } /* end SetEventCallbacks */
 555 
 556 
 557 // event_thread - NULL is a valid value, must be checked
 558 jvmtiError
 559 JvmtiEnv::SetEventNotificationMode(jvmtiEventMode mode, jvmtiEvent event_type, jthread event_thread,   ...) {
 560   bool enabled = (mode == JVMTI_ENABLE);
 561 
 562   // event_type must be valid
 563   if (!JvmtiEventController::is_valid_event_type(event_type)) {
 564     return JVMTI_ERROR_INVALID_EVENT_TYPE;
 565   }
 566 
 567   // assure that needed capabilities are present
 568   if (enabled && !JvmtiUtil::has_event_capability(event_type, get_capabilities())) {
 569     return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
 570   }
 571 
 572   if (event_type == JVMTI_EVENT_CLASS_FILE_LOAD_HOOK && enabled) {
 573     record_class_file_load_hook_enabled();
 574   }
 575 
 576   if (event_thread == NULL) {
 577     // Can be called at Agent_OnLoad() time with event_thread == NULL
 578     // when Thread::current() does not work yet so we cannot create a
 579     // ThreadsListHandle that is common to both thread-specific and
 580     // global code paths.
 581 
 582     JvmtiEventController::set_user_enabled(this, (JavaThread*) NULL, (oop) NULL, event_type, enabled);
















 583   } else {
 584     // We have a specified event_thread.
 585     JavaThread* java_thread = NULL;
 586     oop thread_obj = NULL;
 587     JvmtiVTMTDisabler vtmt_disabler;
 588     ThreadsListHandle tlh;
 589 
 590     jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), event_thread, &java_thread, &thread_obj);
 591     if (err != JVMTI_ERROR_NONE) {
 592       return err;
 593     }
 594 





 595     // global events cannot be controlled at thread level.
 596     if (JvmtiEventController::is_global_event(event_type)) {
 597       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 598     }
 599 
 600     JvmtiEventController::set_user_enabled(this, java_thread, thread_obj, event_type, enabled);










 601   }
 602 
 603   return JVMTI_ERROR_NONE;
 604 } /* end SetEventNotificationMode */
 605 
 606   //
 607   // Capability functions
 608   //
 609 
 610 // capabilities_ptr - pre-checked for NULL
 611 jvmtiError
 612 JvmtiEnv::GetPotentialCapabilities(jvmtiCapabilities* capabilities_ptr) {
 613   JvmtiManageCapabilities::get_potential_capabilities(get_capabilities(),
 614                                                       get_prohibited_capabilities(),
 615                                                       capabilities_ptr);
 616   return JVMTI_ERROR_NONE;
 617 } /* end GetPotentialCapabilities */
 618 
 619 
 620 // capabilities_ptr - pre-checked for NULL

 833     LogConfiguration::configure_stdout(level, true, LOG_TAGS(jni, resolve));
 834     break;
 835   default:
 836     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 837   };
 838   return JVMTI_ERROR_NONE;
 839 } /* end SetVerboseFlag */
 840 
 841 
 842 // format_ptr - pre-checked for NULL
 843 jvmtiError
 844 JvmtiEnv::GetJLocationFormat(jvmtiJlocationFormat* format_ptr) {
 845   *format_ptr = JVMTI_JLOCATION_JVMBCI;
 846   return JVMTI_ERROR_NONE;
 847 } /* end GetJLocationFormat */
 848 
 849   //
 850   // Thread functions
 851   //
 852 

 853 // thread_state_ptr - pre-checked for NULL
 854 jvmtiError
 855 JvmtiEnv::GetThreadState(jthread thread, jint* thread_state_ptr) {
 856   JavaThread* current_thread = JavaThread::current();
 857   JavaThread* java_thread = NULL;
 858   oop thread_oop = NULL;
 859   JvmtiVTMTDisabler vtmt_disabler;
 860   ThreadsListHandle tlh(current_thread);
 861 
 862   if (thread == NULL) {
 863     java_thread = current_thread;
 864     thread_oop = get_vthread_or_thread_oop(java_thread);
 865 
 866     if (thread_oop == NULL || !thread_oop->is_a(vmClasses::Thread_klass())) {
 867       return JVMTI_ERROR_INVALID_THREAD;
 868     }
 869   } else {
 870     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
 871     if (err != JVMTI_ERROR_NONE) {
 872       // We got an error code so we don't have a JavaThread *, but
 873       // only return an error from here if we didn't get a valid
 874       // thread_oop.
 875       // In a vthread case the cv_external_thread_to_JavaThread is expected to correctly set
 876       // the thread_oop and return JVMTI_ERROR_INVALID_THREAD which we ignore here.
 877       if (thread_oop == NULL) {
 878         return err;
 879       }
 880       // We have a valid thread_oop so we can return some thread state.
 881     }
 882   }
 883 
 884   // Support for virtual thread
 885   if (java_lang_VirtualThread::is_instance(thread_oop)) {
 886     *thread_state_ptr = JvmtiEnvBase::get_vthread_state(thread_oop);
 887   } else {
 888     *thread_state_ptr = JvmtiEnvBase::get_thread_state(thread_oop, java_thread);











 889   }
 890   // java_thread can be suspended only if its virtual or carrier thread object is suspended.
 891   assert(java_thread == NULL || !java_thread->is_suspended() ||
 892          (*thread_state_ptr | JVMTI_THREAD_STATE_SUSPENDED) != 0, "sanity check");
 893   return JVMTI_ERROR_NONE;
 894 } /* end GetThreadState */
 895 
 896 
 897 // thread_ptr - pre-checked for NULL
 898 jvmtiError
 899 JvmtiEnv::GetCurrentThread(jthread* thread_ptr) {
 900   JavaThread* cur_thread = JavaThread::current();
 901   oop thread_oop = get_vthread_or_thread_oop(cur_thread);
 902 
 903   *thread_ptr = (jthread)JNIHandles::make_local(cur_thread, thread_oop);
 904   return JVMTI_ERROR_NONE;
 905 } /* end GetCurrentThread */
 906 
 907 
 908 // threads_count_ptr - pre-checked for NULL
 909 // threads_ptr - pre-checked for NULL
 910 jvmtiError
 911 JvmtiEnv::GetAllThreads(jint* threads_count_ptr, jthread** threads_ptr) {
 912   int nthreads        = 0;
 913   Handle *thread_objs = NULL;
 914   Thread* current_thread = Thread::current();
 915   ResourceMark rm(current_thread);
 916   HandleMark hm(current_thread);
 917 
 918   // enumerate threads (including agent threads)
 919   ThreadsListEnumerator tle(current_thread, true);
 920   nthreads = tle.num_threads();
 921   *threads_count_ptr = nthreads;
 922 
 923   if (nthreads == 0) {
 924     *threads_ptr = NULL;
 925     return JVMTI_ERROR_NONE;
 926   }
 927 
 928   thread_objs = NEW_RESOURCE_ARRAY(Handle, nthreads);
 929   NULL_CHECK(thread_objs, JVMTI_ERROR_OUT_OF_MEMORY);
 930 
 931   for (int i = 0; i < nthreads; i++) {
 932     thread_objs[i] = Handle(tle.get_threadObj(i));
 933   }
 934 
 935   jthread *jthreads  = new_jthreadArray(nthreads, thread_objs);
 936   NULL_CHECK(jthreads, JVMTI_ERROR_OUT_OF_MEMORY);
 937 
 938   *threads_ptr = jthreads;
 939   return JVMTI_ERROR_NONE;
 940 } /* end GetAllThreads */
 941 
 942 

 943 jvmtiError
 944 JvmtiEnv::SuspendThread(jthread thread) {
 945   JavaThread* java_thread = NULL;
 946   oop thread_oop = NULL;
 947   JvmtiVTMTDisabler vtmt_disabler;
 948   ThreadsListHandle tlh;
 949 
 950   jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
 951   if (err != JVMTI_ERROR_NONE) {
 952     return err;
 953   }
 954   if (java_thread != NULL && java_thread == JavaThread::current()) {
 955     // current thread will be suspended in the ~JvmtiVTMTDisabler
 956     vtmt_disabler.set_self_suspend();




 957   }
 958   err = suspend_thread(thread_oop,
 959                        java_thread,
 960                        true,  // single suspend
 961                        NULL); // no need for extra safepoint
 962   return err;
 963 } /* end SuspendThread */
 964 
 965 
 966 // request_count - pre-checked to be greater than or equal to 0
 967 // request_list - pre-checked for NULL
 968 // results - pre-checked for NULL
 969 jvmtiError
 970 JvmtiEnv::SuspendThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
 971   oop thread_oop = NULL;
 972   JavaThread *java_thread = NULL;
 973   JavaThread* current = JavaThread::current();
 974 
 975   JvmtiVTMTDisabler vtmt_disabler;
 976   ThreadsListHandle tlh(current);
 977 
 978   for (int i = 0; i < request_count; i++) {
 979     jthread thread = request_list[i];
 980     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
 981     if (err != JVMTI_ERROR_NONE) {
 982       if (thread_oop == NULL || err != JVMTI_ERROR_INVALID_THREAD) {
 983         results[i] = err;



















 984         continue;
 985       }


 986     }
 987     if (java_thread == current) {
 988       // current thread will be suspended in the ~JvmtiVTMTDisabler
 989       vtmt_disabler.set_self_suspend();










 990     }
 991     results[i] = suspend_thread(thread_oop,
 992                                 java_thread,
 993                                 true, // single suspend
 994                                 NULL);
 995   }
 996   // per-thread suspend results returned via results parameter
 997   return JVMTI_ERROR_NONE;
 998 } /* end SuspendThreadList */
 999 
1000 

1001 jvmtiError
1002 JvmtiEnv::SuspendAllVirtualThreads(jint except_count, const jthread* except_list) {
1003   jvmtiError err = JvmtiEnvBase::check_thread_list(except_count, except_list);
1004   if (err != JVMTI_ERROR_NONE) {
1005     return err;
1006   }
1007   if (!JvmtiExport::can_support_virtual_threads()) {
1008     return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
1009   }
1010   ResourceMark rm;
1011   JvmtiVTMTDisabler vtmt_disabler;
1012   GrowableArray<jthread>* elist = new GrowableArray<jthread>(except_count);
1013 
1014   // Collect threads from except_list which resumed status must be restored.
1015   for (int idx = 0; idx < except_count; idx++) {
1016     jthread thread = except_list[idx];
1017     oop thread_oop = JNIHandles::resolve_external_guard(thread);
1018     if (!JvmtiVTSuspender::is_vthread_suspended(thread_oop)) {
1019         // is resumed, so its resumed status must be restored
1020         elist->append(except_list[idx]);
1021     }
1022   }
1023 
1024   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *java_thread = jtiwh.next(); ) {
1025     oop vt_oop = java_thread->mounted_vthread();
1026     if (!java_thread->is_exiting() &&
1027         !java_thread->is_jvmti_agent_thread() &&
1028         !java_thread->is_hidden_from_external_view() &&
1029         vt_oop != NULL &&
1030         java_lang_VirtualThread::is_instance(vt_oop) &&
1031         JvmtiEnvBase::is_vthread_alive(vt_oop) &&
1032         !JvmtiVTSuspender::is_vthread_suspended(vt_oop) &&
1033         !is_in_thread_list(except_count, except_list, vt_oop)
1034     ) {
1035       suspend_thread(vt_oop,
1036                      java_thread,
1037                      false, // suspend all
1038                      NULL);
1039     }
1040   }
1041   JvmtiVTSuspender::register_all_vthreads_suspend();
1042 
1043   // Restore resumed state for threads from except list that were resumed before.
1044   for (int idx = 0; idx < elist->length(); idx++) {
1045     jthread thread = elist->at(idx);
1046     oop thread_oop = JNIHandles::resolve_external_guard(thread);
1047     if (JvmtiVTSuspender::is_vthread_suspended(thread_oop)) {
1048       JvmtiVTSuspender::register_vthread_resume(thread_oop);
1049     }
1050   }
1051   return JVMTI_ERROR_NONE;
1052 } /* end SuspendAllVirtualThreads */
1053 
1054 
1055 jvmtiError
1056 JvmtiEnv::ResumeThread(jthread thread) {
1057   JavaThread* java_thread = NULL;
1058   oop thread_oop = NULL;
1059   JvmtiVTMTDisabler vtmt_disabler;
1060   ThreadsListHandle tlh;
1061 
1062   jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1063   if (err != JVMTI_ERROR_NONE) {
1064     return err;
1065   }
1066   err = resume_thread(thread_oop, java_thread, true); // single suspend
1067   return err;
1068 } /* end ResumeThread */
1069 
1070 
1071 // request_count - pre-checked to be greater than or equal to 0
1072 // request_list - pre-checked for NULL
1073 // results - pre-checked for NULL
1074 jvmtiError
1075 JvmtiEnv::ResumeThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
1076   oop thread_oop = NULL;
1077   JavaThread* java_thread = NULL;
1078   JvmtiVTMTDisabler vtmt_disabler;
1079   ThreadsListHandle tlh;
1080 
1081   for (int i = 0; i < request_count; i++) {
1082     jthread thread = request_list[i];
1083     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1084     if (err != JVMTI_ERROR_NONE) {
1085       if (thread_oop == NULL || err != JVMTI_ERROR_INVALID_THREAD) {
1086         results[i] = err;
1087         continue;
1088       }



1089     }
1090     results[i] = resume_thread(thread_oop, java_thread, true); // single suspend
1091   }
1092   // per-thread resume results returned via results parameter
1093   return JVMTI_ERROR_NONE;
1094 } /* end ResumeThreadList */
1095 
1096 
1097 jvmtiError
1098 JvmtiEnv::ResumeAllVirtualThreads(jint except_count, const jthread* except_list) {
1099   jvmtiError err = JvmtiEnvBase::check_thread_list(except_count, except_list);
1100   if (err != JVMTI_ERROR_NONE) {
1101     return err;
1102   }
1103   if (!JvmtiExport::can_support_virtual_threads()) {
1104     return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
1105   }
1106   ResourceMark rm;
1107   JvmtiVTMTDisabler vtmt_disabler;
1108   GrowableArray<jthread>* elist = new GrowableArray<jthread>(except_count);
1109 
1110   // Collect threads from except_list which suspended status must be restored.
1111   for (int idx = 0; idx < except_count; idx++) {
1112     jthread thread = except_list[idx];
1113     oop thread_oop = JNIHandles::resolve_external_guard(thread);
1114     if (JvmtiVTSuspender::is_vthread_suspended(thread_oop)) {
1115       // is suspended, so its suspended status must be restored
1116       elist->append(except_list[idx]);
1117     }
1118   }
1119 
1120   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *java_thread = jtiwh.next(); ) {
1121     oop vt_oop = java_thread->mounted_vthread();
1122     if (!java_thread->is_exiting() &&
1123         !java_thread->is_jvmti_agent_thread() &&
1124         !java_thread->is_hidden_from_external_view() &&
1125         vt_oop != NULL &&
1126         java_lang_VirtualThread::is_instance(vt_oop) &&
1127         JvmtiEnvBase::is_vthread_alive(vt_oop) &&
1128         JvmtiVTSuspender::is_vthread_suspended(vt_oop) &&
1129         !is_in_thread_list(except_count, except_list, vt_oop)
1130     ) {
1131       resume_thread(vt_oop, java_thread, false); // suspend all
1132     }
1133   }
1134   JvmtiVTSuspender::register_all_vthreads_resume();
1135 
1136   // Restore suspended state for threads from except list that were suspended before.
1137   for (int idx = 0; idx < elist->length(); idx++) {
1138     jthread thread = elist->at(idx);
1139     oop thread_oop = JNIHandles::resolve_external_guard(thread);
1140     if (!JvmtiVTSuspender::is_vthread_suspended(thread_oop)) {
1141       JvmtiVTSuspender::register_vthread_suspend(thread_oop);
1142     }
1143   }

1144   return JVMTI_ERROR_NONE;
1145 } /* end ResumeAllVirtualThreads */
1146 
1147 
1148 // java_thread - protected by ThreadsListHandle and pre-checked
1149 jvmtiError
1150 JvmtiEnv::StopThread(JavaThread* java_thread, jobject exception) {
1151   oop e = JNIHandles::resolve_external_guard(exception);
1152   NULL_CHECK(e, JVMTI_ERROR_NULL_POINTER);
1153 
1154   JavaThread::send_async_exception(java_thread, e);
1155 
1156   return JVMTI_ERROR_NONE;
1157 
1158 } /* end StopThread */
1159 
1160 

1161 jvmtiError
1162 JvmtiEnv::InterruptThread(jthread thread) {
1163   JavaThread* current_thread  = JavaThread::current();
1164   JavaThread* java_thread = NULL;
1165   oop thread_obj = NULL;
1166   HandleMark hm(current_thread);
1167 
1168   JvmtiVTMTDisabler vtmt_disabler;
1169   ThreadsListHandle tlh(current_thread);
1170 
1171   jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, &java_thread, &thread_obj);
1172   if (err != JVMTI_ERROR_NONE) {
1173     return err;
1174   }
1175 
1176   // Support for virtual threads
1177   if (java_lang_VirtualThread::is_instance(thread_obj)) {
1178     Handle obj(current_thread, thread_obj);
1179     JavaValue result(T_VOID);
1180     JavaCalls::call_virtual(&result,
1181                             obj,
1182                             vmClasses::Thread_klass(),
1183                             vmSymbols::interrupt_method_name(),
1184                             vmSymbols::void_method_signature(),
1185                             current_thread);
1186 
1187     return JVMTI_ERROR_NONE;
1188   }
1189 
1190   // Really this should be a Java call to Thread.interrupt to ensure the same
1191   // semantics, however historically this has not been done for some reason.
1192   // So we continue with that (which means we don't interact with any Java-level
1193   // Interruptible object) but we must set the Java-level interrupted state.
1194   java_lang_Thread::set_interrupted(thread_obj, true);
1195   java_thread->interrupt();
1196 
1197   return JVMTI_ERROR_NONE;
1198 } /* end InterruptThread */
1199 
1200 

1201 // info_ptr - pre-checked for NULL
1202 jvmtiError
1203 JvmtiEnv::GetThreadInfo(jthread thread, jvmtiThreadInfo* info_ptr) {
1204   JavaThread* current_thread = JavaThread::current();
1205   ResourceMark rm(current_thread);
1206   HandleMark hm(current_thread);
1207   JavaThread* java_thread = NULL;
1208   oop thread_oop = NULL;
1209 
1210   ThreadsListHandle tlh(current_thread);
1211 
1212   // if thread is NULL the current thread is used

1213   if (thread == NULL) {
1214     java_thread = JavaThread::current();
1215     thread_oop = get_vthread_or_thread_oop(java_thread);
1216     if (thread_oop == NULL || !thread_oop->is_a(vmClasses::Thread_klass())) {
1217       return JVMTI_ERROR_INVALID_THREAD;
1218     }
1219   } else {

1220     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1221     if (err != JVMTI_ERROR_NONE) {
1222       // We got an error code so we don't have a JavaThread *, but
1223       // only return an error from here if we didn't get a valid
1224       // thread_oop.
1225       // In the virtual thread case the cv_external_thread_to_JavaThread is expected to correctly set
1226       // the thread_oop and return JVMTI_ERROR_INVALID_THREAD which we ignore here.
1227       if (thread_oop == NULL) {
1228         return err;
1229       }

1230     }
1231   }
1232   // We have a valid thread_oop so we can return some thread info.
1233 
1234   Handle thread_obj(current_thread, thread_oop);
1235   Handle name;
1236   ThreadPriority priority;
1237   Handle     thread_group;
1238   Handle context_class_loader;
1239   bool          is_daemon;
1240 
1241   name = Handle(current_thread, java_lang_Thread::name(thread_obj()));
1242 
1243   // Support for virtual threads
1244   if (java_lang_VirtualThread::is_instance(thread_obj())) {
1245     priority = (ThreadPriority)JVMTI_THREAD_NORM_PRIORITY;
1246     is_daemon = true;
1247     if (java_lang_VirtualThread::state(thread_obj()) == java_lang_VirtualThread::TERMINATED) {
1248       thread_group = Handle(current_thread, NULL);
1249     } else {
1250       thread_group = Handle(current_thread, java_lang_Thread_VirtualThreads::get_THREAD_GROUP());
1251     }
1252   } else {
1253     priority = java_lang_Thread::priority(thread_obj());
1254     is_daemon = java_lang_Thread::is_daemon(thread_obj());
1255     if (java_lang_Thread::get_thread_status(thread_obj()) == JavaThreadStatus::TERMINATED) {
1256       thread_group = Handle(current_thread, NULL);
1257     } else {
1258       thread_group = Handle(current_thread, java_lang_Thread::threadGroup(thread_obj()));
1259     }
1260   }
1261 
1262   oop loader = java_lang_Thread::context_class_loader(thread_obj());
1263   if (loader == java_lang_Thread_ClassLoaders::get_NOT_SUPPORTED())
1264     loader = NULL;
1265   context_class_loader = Handle(current_thread, loader);
1266 
1267   { const char *n;
1268 
1269     if (name() != NULL) {
1270       n = java_lang_String::as_utf8_string(name());
1271     } else {
1272       int utf8_length = 0;
1273       n = UNICODE::as_utf8((jchar*) NULL, utf8_length);
1274     }
1275 
1276     info_ptr->name = (char *) jvmtiMalloc(strlen(n)+1);
1277     if (info_ptr->name == NULL)
1278       return JVMTI_ERROR_OUT_OF_MEMORY;
1279 
1280     strcpy(info_ptr->name, n);
1281   }
1282   info_ptr->is_daemon = is_daemon;
1283   info_ptr->priority  = priority;
1284 
1285   info_ptr->context_class_loader = (context_class_loader.is_null()) ? NULL :
1286                                     jni_reference(context_class_loader);
1287   info_ptr->thread_group = jni_reference(thread_group);
1288 
1289   return JVMTI_ERROR_NONE;
1290 } /* end GetThreadInfo */
1291 
1292 
1293 // Threads_lock NOT held
1294 // owned_monitor_count_ptr - pre-checked for NULL
1295 // owned_monitors_ptr - pre-checked for NULL
1296 jvmtiError
1297 JvmtiEnv::GetOwnedMonitorInfo(jthread thread, jint* owned_monitor_count_ptr, jobject** owned_monitors_ptr) {
1298   jvmtiError err = JVMTI_ERROR_NONE;
1299   JavaThread* calling_thread = JavaThread::current();
1300   JavaThread* java_thread = NULL;
1301   HandleMark hm(calling_thread);
1302   oop thread_oop = NULL;


1303 
1304   // growable array of jvmti monitors info on the C-heap
1305   GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list =
1306       new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<jvmtiMonitorStackDepthInfo*>(1, mtServiceability);
1307 
1308 
1309   JvmtiVTMTDisabler vtmt_disabler;
1310   ThreadsListHandle tlh(calling_thread);
1311 
1312   err = get_threadOop_and_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1313   if (err != JVMTI_ERROR_NONE) {
1314     return err;
1315   }
1316 
1317   // Support for virtual threads
1318   if (java_lang_VirtualThread::is_instance(thread_oop)) {
1319     // there is no monitor info to collect if target virtual thread is unmounted
1320     if (java_thread != NULL) {
1321       VThreadGetOwnedMonitorInfoClosure op(this,
1322                                            Handle(calling_thread, thread_oop),
1323                                            owned_monitors_list);
1324       Handshake::execute(&op, java_thread);
1325       err = op.result();
1326     }
1327   } else {
1328     EscapeBarrier eb(true, calling_thread, java_thread);
1329     if (!eb.deoptimize_objects(MaxJavaStackTraceDepth)) {
1330       return JVMTI_ERROR_OUT_OF_MEMORY;
1331     }
1332 
1333     if (JvmtiEnvBase::cthread_with_continuation(java_thread)) {
1334       // Carrier thread with a mounted continuation case.
1335       // No contended monitor can be owned by carrier thread in this case.
1336     } else if (java_thread == calling_thread) {
1337       // It is only safe to make a direct call on the current thread.
1338       // All other usage needs to use a direct handshake for safety.
1339       err = get_owned_monitors(calling_thread, java_thread, owned_monitors_list);
1340     } else {
1341       // get owned monitors info with handshake
1342       GetOwnedMonitorInfoClosure op(calling_thread, this, owned_monitors_list);
1343       Handshake::execute(&op, java_thread);
1344       err = op.result();
1345     }
1346   }
1347 
1348   jint owned_monitor_count = owned_monitors_list->length();
1349   if (err == JVMTI_ERROR_NONE) {
1350     if ((err = allocate(owned_monitor_count * sizeof(jobject *),
1351                       (unsigned char**)owned_monitors_ptr)) == JVMTI_ERROR_NONE) {
1352       // copy into the returned array
1353       for (int i = 0; i < owned_monitor_count; i++) {
1354         (*owned_monitors_ptr)[i] =
1355           ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->monitor;
1356       }
1357       *owned_monitor_count_ptr = owned_monitor_count;
1358     }
1359   }
1360   // clean up.
1361   for (int i = 0; i < owned_monitor_count; i++) {
1362     deallocate((unsigned char*)owned_monitors_list->at(i));
1363   }
1364   delete owned_monitors_list;
1365 
1366   return err;
1367 } /* end GetOwnedMonitorInfo */
1368 
1369 
1370 // Threads_lock NOT held
1371 // monitor_info_count_ptr - pre-checked for NULL
1372 // monitor_info_ptr - pre-checked for NULL
1373 jvmtiError
1374 JvmtiEnv::GetOwnedMonitorStackDepthInfo(jthread thread, jint* monitor_info_count_ptr, jvmtiMonitorStackDepthInfo** monitor_info_ptr) {
1375   jvmtiError err = JVMTI_ERROR_NONE;
1376   JavaThread* calling_thread = JavaThread::current();
1377   JavaThread* java_thread = NULL;
1378   HandleMark hm(calling_thread);
1379   oop thread_oop = NULL;


1380 
1381   // growable array of jvmti monitors info on the C-heap
1382   GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list =
1383          new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<jvmtiMonitorStackDepthInfo*>(1, mtServiceability);
1384 
1385   JvmtiVTMTDisabler vtmt_disabler;
1386   ThreadsListHandle tlh(calling_thread);
1387 
1388   err = get_threadOop_and_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1389   if (err != JVMTI_ERROR_NONE) {
1390     return err;



1391   }
1392 
1393   // Support for virtual threads
1394   if (java_lang_VirtualThread::is_instance(thread_oop)) {
1395     // there is no monitor info to collect if target virtual thread is unmounted
1396     if (java_thread != NULL) {
1397       VThreadGetOwnedMonitorInfoClosure op(this,
1398                                            Handle(calling_thread, thread_oop),
1399                                            owned_monitors_list);
1400       Handshake::execute(&op, java_thread);
1401       err = op.result();
1402     }
1403   } else {
1404     EscapeBarrier eb(true, calling_thread, java_thread);
1405     if (!eb.deoptimize_objects(MaxJavaStackTraceDepth)) {
1406       return JVMTI_ERROR_OUT_OF_MEMORY;
1407     }
1408 
1409     if (JvmtiEnvBase::cthread_with_continuation(java_thread)) {
1410       // Carrier thread with a mounted continuation case.
1411       // No contended monitor can be owned by carrier thread in this case.
1412     } else if (java_thread == calling_thread) {
1413       // It is only safe to make a direct call on the current thread.
1414       // All other usage needs to use a direct handshake for safety.
1415       err = get_owned_monitors(calling_thread, java_thread, owned_monitors_list);
1416     } else {
1417       // get owned monitors info with handshake
1418       GetOwnedMonitorInfoClosure op(calling_thread, this, owned_monitors_list);
1419       Handshake::execute(&op, java_thread);
1420       err = op.result();
1421     }
1422   }
1423   jint owned_monitor_count = owned_monitors_list->length();
1424   if (err == JVMTI_ERROR_NONE) {
1425     if ((err = allocate(owned_monitor_count * sizeof(jvmtiMonitorStackDepthInfo),
1426                         (unsigned char**)monitor_info_ptr)) == JVMTI_ERROR_NONE) {
1427       // copy to output array.
1428       for (int i = 0; i < owned_monitor_count; i++) {
1429         (*monitor_info_ptr)[i].monitor =
1430           ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->monitor;
1431         (*monitor_info_ptr)[i].stack_depth =
1432           ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->stack_depth;
1433       }
1434     }
1435     *monitor_info_count_ptr = owned_monitor_count;
1436   }
1437 
1438   // clean up.
1439   for (int i = 0; i < owned_monitor_count; i++) {
1440     deallocate((unsigned char*)owned_monitors_list->at(i));
1441   }
1442   delete owned_monitors_list;
1443 
1444   return err;
1445 } /* end GetOwnedMonitorStackDepthInfo */
1446 
1447 
1448 // Threads_lock NOT held
1449 // monitor_ptr - pre-checked for NULL
1450 jvmtiError
1451 JvmtiEnv::GetCurrentContendedMonitor(jthread thread, jobject* monitor_ptr) {
1452   jvmtiError err = JVMTI_ERROR_NONE;
1453   JavaThread* calling_thread = JavaThread::current();
1454   JavaThread* java_thread = NULL;
1455   HandleMark hm(calling_thread);
1456   oop thread_oop = NULL;
1457 
1458   JvmtiVTMTDisabler vtmt_disabler;
1459   ThreadsListHandle tlh(calling_thread);
1460 
1461   err = get_threadOop_and_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1462   if (err != JVMTI_ERROR_NONE) {
1463     return err;
1464   }
1465 
1466   // Support for virtual threads
1467   if (java_lang_VirtualThread::is_instance(thread_oop)) {
1468     // there is no monitor info to collect if target virtual thread is unmounted
1469     if (java_thread != NULL) {
1470       VThreadGetCurrentContendedMonitorClosure op(this,
1471                                                   Handle(calling_thread, thread_oop),
1472                                                   monitor_ptr);
1473       Handshake::execute(&op, java_thread);
1474       err = op.result();
1475     } else {
1476       *monitor_ptr = NULL;
1477       if (!JvmtiEnvBase::is_vthread_alive(thread_oop)) {
1478         err = JVMTI_ERROR_THREAD_NOT_ALIVE;
1479       }
1480     }
1481     return err;
1482   }
1483   if (JvmtiEnvBase::cthread_with_continuation(java_thread)) {
1484     // Carrier thread with a mounted continuation case.
1485     // No contended monitor can be owned by carrier thread in this case.
1486     *monitor_ptr = NULL;
1487   } else if (java_thread == calling_thread) {
1488     // It is only safe to make a direct call on the current thread.
1489     // All other usage needs to use a direct handshake for safety.
1490     err = get_current_contended_monitor(calling_thread, java_thread, monitor_ptr);
1491   } else {
1492     // get contended monitor information with handshake
1493     GetCurrentContendedMonitorClosure op(calling_thread, this, monitor_ptr);
1494     Handshake::execute(&op, java_thread);
1495     err = op.result();
1496   }
1497   return err;
1498 } /* end GetCurrentContendedMonitor */
1499 
1500 

1501 // proc - pre-checked for NULL
1502 // arg - NULL is a valid value, must be checked
1503 jvmtiError
1504 JvmtiEnv::RunAgentThread(jthread thread, jvmtiStartFunction proc, const void* arg, jint priority) {
1505   JavaThread* current_thread = JavaThread::current();
1506 
1507   JavaThread* java_thread = NULL;
1508   oop thread_oop = NULL;
1509   ThreadsListHandle tlh(current_thread);
1510   jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1511   if (err != JVMTI_ERROR_NONE) {
1512     // We got an error code so we don't have a JavaThread *, but
1513     // only return an error from here if we didn't get a valid
1514     // thread_oop.
1515     if (thread_oop == NULL) {
1516       return err;
1517     }
1518     // We have a valid thread_oop.
1519   }
1520 
1521   if (java_thread != NULL) {
1522     // 'thread' refers to an existing JavaThread.
1523     return JVMTI_ERROR_INVALID_THREAD;
1524   }
1525   if (java_lang_VirtualThread::is_instance(thread_oop)) {
1526     return JVMTI_ERROR_INVALID_THREAD;
1527   }
1528 
1529   if (priority < JVMTI_THREAD_MIN_PRIORITY || priority > JVMTI_THREAD_MAX_PRIORITY) {
1530     return JVMTI_ERROR_INVALID_PRIORITY;
1531   }
1532 
1533   Handle thread_hndl(current_thread, thread_oop);
1534 
1535   JvmtiAgentThread* new_thread = new JvmtiAgentThread(this, proc, arg);
1536 
1537   // At this point it may be possible that no osthread was created for the
1538   // JavaThread due to lack of resources.
1539   if (new_thread->osthread() == NULL) {
1540     // The new thread is not known to Thread-SMR yet so we can just delete.
1541     delete new_thread;
1542     return JVMTI_ERROR_OUT_OF_MEMORY;
1543   }
1544 
1545   JavaThread::start_internal_daemon(current_thread, new_thread, thread_hndl,
1546                                     (ThreadPriority)priority);
1547 

1573     Handle system_thread_group(current_thread, Universe::system_thread_group());
1574     *groups_ptr[0] = jni_reference(system_thread_group);
1575   }
1576 
1577   return JVMTI_ERROR_NONE;
1578 } /* end GetTopThreadGroups */
1579 
1580 
1581 // info_ptr - pre-checked for NULL
1582 jvmtiError
1583 JvmtiEnv::GetThreadGroupInfo(jthreadGroup group, jvmtiThreadGroupInfo* info_ptr) {
1584   Thread* current_thread = Thread::current();
1585   ResourceMark rm(current_thread);
1586   HandleMark hm(current_thread);
1587 
1588   Handle group_obj (current_thread, JNIHandles::resolve_external_guard(group));
1589   NULL_CHECK(group_obj(), JVMTI_ERROR_INVALID_THREAD_GROUP);
1590 
1591   const char* name;
1592   Handle parent_group;

1593   ThreadPriority max_priority;
1594 
1595   name         = java_lang_ThreadGroup::name(group_obj());
1596   parent_group = Handle(current_thread, java_lang_ThreadGroup::parent(group_obj()));

1597   max_priority = java_lang_ThreadGroup::maxPriority(group_obj());
1598 




1599   if (name != NULL) {
1600     info_ptr->name = (char*)jvmtiMalloc(strlen(name)+1);
1601     NULL_CHECK(info_ptr->name, JVMTI_ERROR_OUT_OF_MEMORY);
1602     strcpy(info_ptr->name, name);
1603   } else {
1604     info_ptr->name = NULL;
1605   }
1606 
1607   info_ptr->parent       = jni_reference(parent_group);
1608   info_ptr->max_priority = max_priority;
1609   info_ptr->is_daemon    = JNI_FALSE;
1610 
1611   return JVMTI_ERROR_NONE;
1612 } /* end GetThreadGroupInfo */
1613 

1614 // thread_count_ptr - pre-checked for NULL
1615 // threads_ptr - pre-checked for NULL
1616 // group_count_ptr - pre-checked for NULL
1617 // groups_ptr - pre-checked for NULL
1618 jvmtiError
1619 JvmtiEnv::GetThreadGroupChildren(jthreadGroup group, jint* thread_count_ptr, jthread** threads_ptr, jint* group_count_ptr, jthreadGroup** groups_ptr) {
1620   JavaThread* current_thread = JavaThread::current();
1621   oop group_obj = JNIHandles::resolve_external_guard(group);
1622   NULL_CHECK(group_obj, JVMTI_ERROR_INVALID_THREAD_GROUP);
1623 
1624   Handle *thread_objs = NULL;
1625   Handle *group_objs  = NULL;
1626   int nthreads = 0;
1627   int ngroups = 0;
1628   int hidden_threads = 0;
1629 
1630   ResourceMark rm(current_thread);
1631   HandleMark hm(current_thread);
1632 
1633   Handle group_hdl(current_thread, group_obj);
1634 
1635   nthreads = get_live_threads(current_thread, group_hdl, &thread_objs);
1636   ngroups = get_subgroups(current_thread, group_hdl, &group_objs);
















































1637 
1638   *group_count_ptr  = ngroups;
1639   *thread_count_ptr = nthreads;
1640   *threads_ptr     = new_jthreadArray(nthreads, thread_objs);
1641   *groups_ptr      = new_jthreadGroupArray(ngroups, group_objs);
1642   if ((nthreads > 0) && (*threads_ptr == NULL)) {
1643     return JVMTI_ERROR_OUT_OF_MEMORY;
1644   }
1645   if ((ngroups > 0) && (*groups_ptr == NULL)) {
1646     return JVMTI_ERROR_OUT_OF_MEMORY;
1647   }
1648 
1649   return JVMTI_ERROR_NONE;
1650 } /* end GetThreadGroupChildren */
1651 
1652 
1653   //
1654   // Stack Frame functions
1655   //
1656 
1657 // Threads_lock NOT held
1658 // max_frame_count - pre-checked to be greater than or equal to 0
1659 // frame_buffer - pre-checked for NULL
1660 // count_ptr - pre-checked for NULL
1661 jvmtiError
1662 JvmtiEnv::GetStackTrace(jthread thread, jint start_depth, jint max_frame_count, jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
1663   jvmtiError err = JVMTI_ERROR_NONE;
1664   JavaThread* java_thread = NULL;
1665   JavaThread* current_thread = JavaThread::current();
1666   HandleMark hm(current_thread);
1667   oop thread_obj = NULL;
1668 
1669   JvmtiVTMTDisabler vtmt_disabler;
1670   ThreadsListHandle tlh(current_thread);
1671 
1672   err = get_threadOop_and_JavaThread(tlh.list(), thread, &java_thread, &thread_obj);
1673   if (err != JVMTI_ERROR_NONE) {
1674     return err;
1675   }
1676 
1677   // Support for virtual threads
1678   if (java_lang_VirtualThread::is_instance(thread_obj)) {
1679     if (java_thread == NULL) { // target virtual thread is unmounted
1680       ResourceMark rm(current_thread);
1681 
1682       VM_VThreadGetStackTrace op(this, Handle(current_thread, thread_obj),
1683                                  start_depth, max_frame_count,
1684                                  frame_buffer, count_ptr);
1685       VMThread::execute(&op);
1686       return op.result();
1687     }
1688     VThreadGetStackTraceClosure op(this, Handle(current_thread, thread_obj),
1689                                    start_depth, max_frame_count, frame_buffer, count_ptr);
1690     Handshake::execute(&op, java_thread);
1691     return op.result();
1692   }
1693 
1694   // It is only safe to perform the direct operation on the current
1695   // thread. All other usage needs to use a direct handshake for safety.
1696   if (java_thread == JavaThread::current()) {
1697     err = get_stack_trace(java_thread, start_depth, max_frame_count, frame_buffer, count_ptr);
1698   } else {
1699     // Get stack trace with handshake.
1700     GetStackTraceClosure op(this, start_depth, max_frame_count, frame_buffer, count_ptr);
1701     Handshake::execute(&op, java_thread);
1702     err = op.result();
1703   }
1704 
1705   return err;
1706 } /* end GetStackTrace */
1707 
1708 
1709 // max_frame_count - pre-checked to be greater than or equal to 0
1710 // stack_info_ptr - pre-checked for NULL
1711 // thread_count_ptr - pre-checked for NULL
1712 jvmtiError

1716 
1717   // JVMTI get stack traces at safepoint.
1718   VM_GetAllStackTraces op(this, calling_thread, max_frame_count);
1719   VMThread::execute(&op);
1720   *thread_count_ptr = op.final_thread_count();
1721   *stack_info_ptr = op.stack_info();
1722   err = op.result();
1723   return err;
1724 } /* end GetAllStackTraces */
1725 
1726 
1727 // thread_count - pre-checked to be greater than or equal to 0
1728 // thread_list - pre-checked for NULL
1729 // max_frame_count - pre-checked to be greater than or equal to 0
1730 // stack_info_ptr - pre-checked for NULL
1731 jvmtiError
1732 JvmtiEnv::GetThreadListStackTraces(jint thread_count, const jthread* thread_list, jint max_frame_count, jvmtiStackInfo** stack_info_ptr) {
1733   jvmtiError err = JVMTI_ERROR_NONE;
1734 
1735   if (thread_count == 1) {
1736     JvmtiVTMTDisabler vtmt_disabler;
1737 
1738     // Use direct handshake if we need to get only one stack trace.
1739     JavaThread *current_thread = JavaThread::current();
1740     ThreadsListHandle tlh(current_thread);
1741     jthread thread = thread_list[0];
1742     JavaThread *java_thread;
1743     oop thread_obj = NULL;
1744     err = get_threadOop_and_JavaThread(tlh.list(), thread, &java_thread, &thread_obj);
1745     if (err != JVMTI_ERROR_NONE) {
1746       return err;
1747     }
1748 
1749     // Support for virtual threads
1750     if (java_lang_VirtualThread::is_instance(thread_obj)) {
1751       if (java_thread == NULL) { // target virtual thread is unmounted
1752         ResourceMark rm(current_thread);
1753         MultipleStackTracesCollector collector(this, max_frame_count);
1754         collector.fill_frames(thread, java_thread, thread_obj);
1755         collector.allocate_and_fill_stacks(1);
1756         *stack_info_ptr = collector.stack_info();
1757         return collector.result();
1758       }
1759     }
1760 
1761     GetSingleStackTraceClosure op(this, current_thread, thread, max_frame_count);
1762     Handshake::execute(&op, java_thread);
1763     err = op.result();
1764     if (err == JVMTI_ERROR_NONE) {
1765       *stack_info_ptr = op.stack_info();
1766     }
1767   } else {
1768     // JVMTI get stack traces at safepoint.
1769     VM_GetThreadListStackTraces op(this, thread_count, thread_list, max_frame_count);
1770     VMThread::execute(&op);
1771     err = op.result();
1772     if (err == JVMTI_ERROR_NONE) {
1773       *stack_info_ptr = op.stack_info();
1774     }
1775   }
1776   return err;
1777 } /* end GetThreadListStackTraces */
1778 
1779 
1780 // Threads_lock NOT held
1781 // count_ptr - pre-checked for NULL
1782 jvmtiError
1783 JvmtiEnv::GetFrameCount(jthread thread, jint* count_ptr) {
1784   jvmtiError err = JVMTI_ERROR_NONE;
1785   JavaThread* java_thread = NULL;
1786   JavaThread* current_thread = JavaThread::current();
1787   HandleMark hm(current_thread);
1788   oop thread_obj = NULL;
1789 
1790   JvmtiVTMTDisabler vtmt_disabler;
1791   ThreadsListHandle tlh(current_thread);
1792 
1793   err = get_threadOop_and_JavaThread(tlh.list(), thread, &java_thread, &thread_obj);
1794   if (err != JVMTI_ERROR_NONE) {
1795     return err;
1796   }
1797 
1798   // Support for virtual threads
1799   if (java_lang_VirtualThread::is_instance(thread_obj)) {
1800     if (java_thread == NULL) { // target virtual thread is unmounted
1801       VM_VThreadGetFrameCount op(this, Handle(current_thread, thread_obj),  count_ptr);
1802       VMThread::execute(&op);
1803       return op.result();
1804     }
1805     VThreadGetFrameCountClosure op(this, Handle(current_thread, thread_obj), count_ptr);
1806     Handshake::execute(&op, java_thread);
1807     return op.result();
1808   }
1809 
1810   // It is only safe to perform the direct operation on the current
1811   // thread. All other usage needs to use a direct handshake for safety.
1812   if (java_thread == JavaThread::current()) {
1813     err = get_frame_count(java_thread, count_ptr);
1814   } else {
1815     // get java stack frame count with handshake.
1816     GetFrameCountClosure op(this, count_ptr);
1817     Handshake::execute(&op, java_thread);
1818     err = op.result();
1819   }
1820   return err;
1821 } /* end GetFrameCount */
1822 
1823 
1824 // java_thread - protected by ThreadsListHandle and pre-checked
1825 jvmtiError
1826 JvmtiEnv::PopFrame(JavaThread* java_thread) {
1827   // retrieve or create the state
1828   JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
1829   if (state == NULL) {
1830     return JVMTI_ERROR_THREAD_NOT_ALIVE;
1831   }
1832 
1833   // Eagerly reallocate scalar replaced objects.
1834   JavaThread* current_thread = JavaThread::current();
1835   EscapeBarrier eb(true, current_thread, java_thread);
1836   if (!eb.deoptimize_objects(1)) {
1837     // Reallocation of scalar replaced objects failed -> return with error
1838     return JVMTI_ERROR_OUT_OF_MEMORY;
1839   }
1840 
1841   MutexLocker mu(JvmtiThreadState_lock);
1842   UpdateForPopTopFrameClosure op(state);
1843   if (java_thread == current_thread) {
1844     op.doit(java_thread, true /* self */);
1845   } else {
1846     Handshake::execute(&op, java_thread);
1847   }
1848   return op.result();
1849 } /* end PopFrame */
1850 
1851 
1852 // Threads_lock NOT held
1853 // depth - pre-checked as non-negative
1854 // method_ptr - pre-checked for NULL
1855 // location_ptr - pre-checked for NULL
1856 jvmtiError
1857 JvmtiEnv::GetFrameLocation(jthread thread, jint depth, jmethodID* method_ptr, jlocation* location_ptr) {
1858   jvmtiError err = JVMTI_ERROR_NONE;
1859   JavaThread* java_thread = NULL;
1860   JavaThread* current_thread = JavaThread::current();
1861   HandleMark hm(current_thread);
1862   oop thread_obj = NULL;
1863 
1864   JvmtiVTMTDisabler vtmt_disabler;
1865   ThreadsListHandle tlh(current_thread);
1866 
1867   err = get_threadOop_and_JavaThread(tlh.list(), thread, &java_thread, &thread_obj);
1868   if (err != JVMTI_ERROR_NONE) {
1869     return err;
1870   }
1871 
1872   // Support for virtual threads
1873   if (java_lang_VirtualThread::is_instance(thread_obj)) {
1874     if (java_thread == NULL) { // target virtual thread is unmounted
1875       err = get_frame_location(thread_obj, depth, method_ptr, location_ptr);
1876       return err;
1877     }
1878     VThreadGetFrameLocationClosure op(this, Handle(current_thread, thread_obj),
1879                                       depth, method_ptr, location_ptr);
1880     Handshake::execute(&op, java_thread);
1881     return op.result();
1882   }
1883 
1884   // It is only safe to perform the direct operation on the current
1885   // thread. All other usage needs to use a direct handshake for safety.
1886   if (java_thread == JavaThread::current()) {
1887     err = get_frame_location(java_thread, depth, method_ptr, location_ptr);
1888   } else {
1889     // JVMTI get java stack frame location via direct handshake.
1890     GetFrameLocationClosure op(this, depth, method_ptr, location_ptr);
1891     Handshake::execute(&op, java_thread);
1892     err = op.result();
1893   }
1894   return err;
1895 } /* end GetFrameLocation */
1896 
1897 
1898 // Threads_lock NOT held, java_thread not protected by lock
1899 // depth - pre-checked as non-negative
1900 jvmtiError
1901 JvmtiEnv::NotifyFramePop(jthread thread, jint depth) {
1902   jvmtiError err = JVMTI_ERROR_NONE;
1903   ResourceMark rm;
1904   JavaThread* java_thread = NULL;
1905   oop thread_obj = NULL;
1906   JvmtiVTMTDisabler vtmt_disabler;
1907   ThreadsListHandle tlh;
1908 
1909   err = get_threadOop_and_JavaThread(tlh.list(), thread, &java_thread, &thread_obj);
1910   if (err != JVMTI_ERROR_NONE) {
1911     return err;
1912   }
1913 
1914   JavaThread* current = JavaThread::current();
1915   HandleMark hm(current);
1916   Handle thread_handle(current, thread_obj);
1917 
1918   // Support for virtual threads
1919   if (java_lang_VirtualThread::is_instance(thread_obj)) {
1920     if (java_thread == NULL) {
1921       // java_thread is NULL if virtual thread is unmounted
1922       JvmtiThreadState *state = JvmtiThreadState::state_for(java_thread, thread_handle);
1923       if (state == NULL) {
1924         return JVMTI_ERROR_THREAD_NOT_ALIVE;
1925       }
1926       MutexLocker mu(JvmtiThreadState_lock);
1927       int frame_number = state->count_frames() - depth;
1928       state->env_thread_state(this)->set_frame_pop(frame_number);
1929       return JVMTI_ERROR_NONE;
1930     }
1931   }
1932 
1933   JvmtiThreadState *state = JvmtiThreadState::state_for(java_thread, thread_handle);
1934   if (state == NULL) {
1935     return JVMTI_ERROR_THREAD_NOT_ALIVE;
1936   }
1937 
1938   SetFramePopClosure op(this, state, depth);
1939   MutexLocker mu(current, JvmtiThreadState_lock);
1940   if (java_thread == current) {
1941     op.doit(java_thread, true /* self */);
1942   } else {
1943     Handshake::execute(&op, java_thread);
1944   }
1945   return op.result();
1946 } /* end NotifyFramePop */
1947 
1948 
1949   //
1950   // Force Early Return functions
1951   //
1952 
1953 // java_thread - protected by ThreadsListHandle and pre-checked
1954 jvmtiError
1955 JvmtiEnv::ForceEarlyReturnObject(JavaThread* java_thread, jobject value) {
1956   jvalue val;
1957   val.l = value;
1958   return force_early_return(java_thread, val, atos);
1959 } /* end ForceEarlyReturnObject */
1960 

2156 jvmtiError
2157 JvmtiEnv::IterateOverInstancesOfClass(oop k_mirror, jvmtiHeapObjectFilter object_filter, jvmtiHeapObjectCallback heap_object_callback, const void* user_data) {
2158   if (java_lang_Class::is_primitive(k_mirror)) {
2159     // DO PRIMITIVE CLASS PROCESSING
2160     return JVMTI_ERROR_NONE;
2161   }
2162   Klass* klass = java_lang_Class::as_Klass(k_mirror);
2163   if (klass == NULL) {
2164     return JVMTI_ERROR_INVALID_CLASS;
2165   }
2166   TraceTime t("IterateOverInstancesOfClass", TRACETIME_LOG(Debug, jvmti, objecttagging));
2167   JvmtiTagMap::tag_map_for(this)->iterate_over_heap(object_filter, klass, heap_object_callback, user_data);
2168   return JVMTI_ERROR_NONE;
2169 } /* end IterateOverInstancesOfClass */
2170 
2171 
2172   //
2173   // Local Variable functions
2174   //
2175 
2176 // Threads_lock NOT held
2177 // depth - pre-checked as non-negative
2178 // value_ptr - pre-checked for NULL
2179 jvmtiError
2180 JvmtiEnv::GetLocalObject(jthread thread, jint depth, jint slot, jobject* value_ptr) {
2181   jvmtiError err = JVMTI_ERROR_NONE;
2182   JavaThread* java_thread = NULL;
2183   JavaThread* current_thread = JavaThread::current();
2184   // rm object is created to clean up the javaVFrame created in
2185   // doit_prologue(), but after doit() is finished with it.
2186   ResourceMark rm(current_thread);
2187   HandleMark hm(current_thread);
2188   oop thread_obj = JNIHandles::resolve_external_guard(thread);
2189 
2190   if (java_lang_VirtualThread::is_instance(thread_obj)) {
2191     // Support for virtual threads
2192     VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2193                                    current_thread, depth, slot);
2194     VMThread::execute(&op);
2195     err = op.result();
2196     if (err == JVMTI_ERROR_NONE) {
2197       *value_ptr = op.value().l;
2198     }
2199   } else {
2200     // Support for ordinary threads
2201     ThreadsListHandle tlh(current_thread);
2202     err = get_JavaThread(tlh.list(), thread, &java_thread);
2203     if (err != JVMTI_ERROR_NONE) {
2204       return err;
2205     }
2206     VM_GetOrSetLocal op(java_thread, current_thread, depth, slot);
2207     VMThread::execute(&op);
2208     err = op.result();
2209     if (err == JVMTI_ERROR_NONE) {
2210       *value_ptr = op.value().l;
2211     }
2212   }
2213   return err;
2214 } /* end GetLocalObject */
2215 
2216 // Threads_lock NOT held
2217 // depth - pre-checked as non-negative
2218 // value - pre-checked for NULL
2219 jvmtiError
2220 JvmtiEnv::GetLocalInstance(jthread thread, jint depth, jobject* value_ptr){
2221   jvmtiError err = JVMTI_ERROR_NONE;
2222   JavaThread* java_thread = NULL;
2223   JavaThread* current_thread = JavaThread::current();
2224   // rm object is created to clean up the javaVFrame created in
2225   // doit_prologue(), but after doit() is finished with it.
2226   ResourceMark rm(current_thread);
2227   HandleMark hm(current_thread);
2228   oop thread_obj = JNIHandles::resolve_external_guard(thread);
2229 
2230   if (java_lang_VirtualThread::is_instance(thread_obj)) {
2231     // Support for virtual threads
2232     VM_VirtualThreadGetReceiver op(this, Handle(current_thread, thread_obj),
2233                                  current_thread, depth);
2234     VMThread::execute(&op);
2235     err = op.result();
2236     if (err == JVMTI_ERROR_NONE) {
2237       *value_ptr = op.value().l;
2238     }
2239   } else {
2240     // Support for ordinary threads
2241     ThreadsListHandle tlh(current_thread);
2242     err = get_JavaThread(tlh.list(), thread, &java_thread);
2243     if (err != JVMTI_ERROR_NONE) {
2244       return err;
2245     }
2246     VM_GetReceiver op(java_thread, current_thread, depth);
2247     VMThread::execute(&op);
2248     err = op.result();
2249     if (err == JVMTI_ERROR_NONE) {
2250       *value_ptr = op.value().l;
2251     }
2252   }
2253   return err;
2254 } /* end GetLocalInstance */
2255 
2256 
2257 // Threads_lock NOT held
2258 // depth - pre-checked as non-negative
2259 // value_ptr - pre-checked for NULL
2260 jvmtiError
2261 JvmtiEnv::GetLocalInt(jthread thread, jint depth, jint slot, jint* value_ptr) {
2262   jvmtiError err = JVMTI_ERROR_NONE;
2263   JavaThread* java_thread = NULL;
2264   JavaThread* current_thread = JavaThread::current();
2265   // rm object is created to clean up the javaVFrame created in
2266   // doit_prologue(), but after doit() is finished with it.
2267   ResourceMark rm(current_thread);
2268   HandleMark hm(current_thread);
2269   oop thread_obj = JNIHandles::resolve_external_guard(thread);
2270 
2271   if (java_lang_VirtualThread::is_instance(thread_obj)) {
2272     // Support for virtual threads
2273     VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2274                                    depth, slot, T_INT);
2275     VMThread::execute(&op);
2276     err = op.result();
2277     if (err == JVMTI_ERROR_NONE) {
2278       *value_ptr = op.value().i;
2279     }
2280   } else {
2281     // Support for ordinary threads
2282     ThreadsListHandle tlh(current_thread);
2283     err = get_JavaThread(tlh.list(), thread, &java_thread);
2284     if (err != JVMTI_ERROR_NONE) {
2285       return err;
2286     }
2287     VM_GetOrSetLocal op(java_thread, depth, slot, T_INT);
2288     VMThread::execute(&op);
2289     err = op.result();
2290     if (err == JVMTI_ERROR_NONE) {
2291       *value_ptr = op.value().i;
2292     }
2293   }
2294   return err;
2295 } /* end GetLocalInt */
2296 
2297 
2298 // Threads_lock NOT held
2299 // depth - pre-checked as non-negative
2300 // value_ptr - pre-checked for NULL
2301 jvmtiError
2302 JvmtiEnv::GetLocalLong(jthread thread, jint depth, jint slot, jlong* value_ptr) {
2303   jvmtiError err = JVMTI_ERROR_NONE;
2304   JavaThread* java_thread = NULL;
2305   JavaThread* current_thread = JavaThread::current();
2306   // rm object is created to clean up the javaVFrame created in
2307   // doit_prologue(), but after doit() is finished with it.
2308   ResourceMark rm(current_thread);
2309   HandleMark hm(current_thread);
2310   oop thread_obj = JNIHandles::resolve_external_guard(thread);
2311 
2312   if (java_lang_VirtualThread::is_instance(thread_obj)) {
2313     // Support for virtual threads
2314     VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2315                                    depth, slot, T_LONG);
2316     VMThread::execute(&op);
2317     err = op.result();
2318     if (err == JVMTI_ERROR_NONE) {
2319       *value_ptr = op.value().j;
2320     }
2321   } else {
2322     // Support for ordinary threads
2323     ThreadsListHandle tlh(current_thread);
2324     err = get_JavaThread(tlh.list(), thread, &java_thread);
2325     if (err != JVMTI_ERROR_NONE) {
2326       return err;
2327     }
2328     VM_GetOrSetLocal op(java_thread, depth, slot, T_LONG);
2329     VMThread::execute(&op);
2330     err = op.result();
2331     if (err == JVMTI_ERROR_NONE) {
2332       *value_ptr = op.value().j;
2333     }
2334   }
2335   return err;
2336 } /* end GetLocalLong */
2337 
2338 
2339 // Threads_lock NOT held
2340 // depth - pre-checked as non-negative
2341 // value_ptr - pre-checked for NULL
2342 jvmtiError
2343 JvmtiEnv::GetLocalFloat(jthread thread, jint depth, jint slot, jfloat* value_ptr) {
2344   jvmtiError err = JVMTI_ERROR_NONE;
2345   JavaThread* java_thread = NULL;
2346   JavaThread* current_thread = JavaThread::current();
2347   // rm object is created to clean up the javaVFrame created in
2348   // doit_prologue(), but after doit() is finished with it.
2349   ResourceMark rm(current_thread);
2350   HandleMark hm(current_thread);
2351   oop thread_obj = JNIHandles::resolve_external_guard(thread);
2352 
2353   if (java_lang_VirtualThread::is_instance(thread_obj)) {
2354     // Support for virtual threads
2355     VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2356                                    depth, slot, T_FLOAT);
2357     VMThread::execute(&op);
2358     err = op.result();
2359     if (err == JVMTI_ERROR_NONE) {
2360       *value_ptr = op.value().f;
2361     }
2362   } else {
2363     // Support for ordinary threads
2364     ThreadsListHandle tlh(current_thread);
2365     err = get_JavaThread(tlh.list(), thread, &java_thread);
2366     if (err != JVMTI_ERROR_NONE) {
2367       return err;
2368     }
2369     VM_GetOrSetLocal op(java_thread, depth, slot, T_FLOAT);
2370     VMThread::execute(&op);
2371     err = op.result();
2372     if (err == JVMTI_ERROR_NONE) {
2373       *value_ptr = op.value().f;
2374     }
2375   }
2376   return err;
2377 } /* end GetLocalFloat */
2378 
2379 
2380 // Threads_lock NOT held
2381 // depth - pre-checked as non-negative
2382 // value_ptr - pre-checked for NULL
2383 jvmtiError
2384 JvmtiEnv::GetLocalDouble(jthread thread, jint depth, jint slot, jdouble* value_ptr) {
2385   jvmtiError err = JVMTI_ERROR_NONE;
2386   JavaThread* java_thread = NULL;
2387   JavaThread* current_thread = JavaThread::current();
2388   // rm object is created to clean up the javaVFrame created in
2389   // doit_prologue(), but after doit() is finished with it.
2390   ResourceMark rm(current_thread);
2391   HandleMark hm(current_thread);
2392   oop thread_obj = JNIHandles::resolve_external_guard(thread);
2393 
2394   if (java_lang_VirtualThread::is_instance(thread_obj)) {
2395     // Support for virtual threads
2396     VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2397                                    depth, slot, T_DOUBLE);
2398     VMThread::execute(&op);
2399     err = op.result();
2400     if (err == JVMTI_ERROR_NONE) {
2401       *value_ptr = op.value().d;
2402     }
2403   } else {
2404     // Support for ordinary threads
2405     ThreadsListHandle tlh(current_thread);
2406     err = get_JavaThread(tlh.list(), thread, &java_thread);
2407     if (err != JVMTI_ERROR_NONE) {
2408       return err;
2409     }
2410     VM_GetOrSetLocal op(java_thread, depth, slot, T_DOUBLE);
2411     VMThread::execute(&op);
2412     err = op.result();
2413     if (err == JVMTI_ERROR_NONE) {
2414       *value_ptr = op.value().d;
2415     }
2416   }
2417   return err;
2418 } /* end GetLocalDouble */
2419 
2420 
2421 // java_thread - protected by ThreadsListHandle and pre-checked
2422 // depth - pre-checked as non-negative
2423 jvmtiError
2424 JvmtiEnv::SetLocalObject(JavaThread* java_thread, jint depth, jint slot, jobject value) {
2425   // rm object is created to clean up the javaVFrame created in
2426   // doit_prologue(), but after doit() is finished with it.
2427   ResourceMark rm;
2428   jvalue val;
2429   val.l = value;
2430   VM_GetOrSetLocal op(java_thread, depth, slot, T_OBJECT, val);
2431   VMThread::execute(&op);
2432   return op.result();
2433 } /* end SetLocalObject */
2434 
2435 
2436 // java_thread - protected by ThreadsListHandle and pre-checked
2437 // depth - pre-checked as non-negative

3698 jvmtiError
3699 JvmtiEnv::SetExtensionEventCallback(jint extension_event_index, jvmtiExtensionEvent callback) {
3700   return JvmtiExtensions::set_event_callback(this, extension_event_index, callback);
3701 } /* end SetExtensionEventCallback */
3702 
3703   //
3704   // Timers functions
3705   //
3706 
3707 // info_ptr - pre-checked for NULL
3708 jvmtiError
3709 JvmtiEnv::GetCurrentThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) {
3710   os::current_thread_cpu_time_info(info_ptr);
3711   return JVMTI_ERROR_NONE;
3712 } /* end GetCurrentThreadCpuTimerInfo */
3713 
3714 
3715 // nanos_ptr - pre-checked for NULL
3716 jvmtiError
3717 JvmtiEnv::GetCurrentThreadCpuTime(jlong* nanos_ptr) {
3718   Thread* thread = Thread::current();
3719 
3720   // Surprizingly the GetCurrentThreadCpuTime is used by non-JavaThread's.
3721   if (thread->is_Java_thread()) {
3722     oop thread_obj = get_vthread_or_thread_oop(JavaThread::cast(thread));
3723 
3724     if (java_lang_VirtualThread::is_instance(thread_obj)) {
3725       return JVMTI_ERROR_INVALID_THREAD;
3726     }
3727   }
3728   *nanos_ptr = os::current_thread_cpu_time();
3729   return JVMTI_ERROR_NONE;
3730 } /* end GetCurrentThreadCpuTime */
3731 
3732 
3733 // info_ptr - pre-checked for NULL
3734 jvmtiError
3735 JvmtiEnv::GetThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) {
3736   os::thread_cpu_time_info(info_ptr);
3737   return JVMTI_ERROR_NONE;
3738 } /* end GetThreadCpuTimerInfo */
3739 
3740 
3741 // java_thread - protected by ThreadsListHandle and pre-checked
3742 // nanos_ptr - pre-checked for NULL
3743 jvmtiError
3744 JvmtiEnv::GetThreadCpuTime(JavaThread* java_thread, jlong* nanos_ptr) {
3745   *nanos_ptr = os::thread_cpu_time(java_thread);
3746   return JVMTI_ERROR_NONE;
3747 } /* end GetThreadCpuTime */
< prev index next >