45 #include "prims/jvmtiClassFileReconstituter.hpp"
46 #include "prims/jvmtiCodeBlobEvents.hpp"
47 #include "prims/jvmtiExtensions.hpp"
48 #include "prims/jvmtiGetLoadedClasses.hpp"
49 #include "prims/jvmtiImpl.hpp"
50 #include "prims/jvmtiManageCapabilities.hpp"
51 #include "prims/jvmtiRawMonitor.hpp"
52 #include "prims/jvmtiRedefineClasses.hpp"
53 #include "prims/jvmtiTagMap.hpp"
54 #include "prims/jvmtiThreadState.inline.hpp"
55 #include "prims/jvmtiUtil.hpp"
56 #include "runtime/arguments.hpp"
57 #include "runtime/deoptimization.hpp"
58 #include "runtime/interfaceSupport.hpp"
59 #include "runtime/javaCalls.hpp"
60 #include "runtime/jfieldIDWorkaround.hpp"
61 #include "runtime/osThread.hpp"
62 #include "runtime/reflectionUtils.hpp"
63 #include "runtime/signature.hpp"
64 #include "runtime/thread.inline.hpp"
65 #include "runtime/timerTrace.hpp"
66 #include "runtime/vframe.hpp"
67 #include "runtime/vmThread.hpp"
68 #include "services/threadService.hpp"
69 #include "utilities/exceptions.hpp"
70 #include "utilities/preserveException.hpp"
71
72
73 #define FIXLATER 0 // REMOVE this when completed.
74
75 // FIXLATER: hook into JvmtiTrace
76 #define TraceJVMTICalls false
77
78 JvmtiEnv::JvmtiEnv(jint version) : JvmtiEnvBase(version) {
79 }
80
81 JvmtiEnv::~JvmtiEnv() {
82 }
83
84 JvmtiEnv*
145 if (state == NULL) {
146 return JVMTI_ERROR_THREAD_NOT_ALIVE;
147 }
148 }
149 state->env_thread_state(this)->set_agent_thread_local_storage_data((void*)data);
150 return JVMTI_ERROR_NONE;
151 } /* end SetThreadLocalStorage */
152
153
154 // Threads_lock NOT held
155 // thread - NOT pre-checked
156 // data_ptr - pre-checked for NULL
157 jvmtiError
158 JvmtiEnv::GetThreadLocalStorage(jthread thread, void** data_ptr) {
159 JavaThread* current_thread = JavaThread::current();
160 if (thread == NULL) {
161 JvmtiThreadState* state = current_thread->jvmti_thread_state();
162 *data_ptr = (state == NULL) ? NULL :
163 state->env_thread_state(this)->get_agent_thread_local_storage_data();
164 } else {
165
166 // jvmti_GetThreadLocalStorage is "in native" and doesn't transition
167 // the thread to _thread_in_vm. However, when the TLS for a thread
168 // other than the current thread is required we need to transition
169 // from native so as to resolve the jthread.
170
171 ThreadInVMfromNative __tiv(current_thread);
172 VM_ENTRY_BASE(jvmtiError, JvmtiEnv::GetThreadLocalStorage , current_thread)
173 debug_only(VMNativeEntryWrapper __vew;)
174
175 oop thread_oop = JNIHandles::resolve_external_guard(thread);
176 if (thread_oop == NULL) {
177 return JVMTI_ERROR_INVALID_THREAD;
178 }
179 if (!thread_oop->is_a(SystemDictionary::Thread_klass())) {
180 return JVMTI_ERROR_INVALID_THREAD;
181 }
182 JavaThread* java_thread = java_lang_Thread::thread(thread_oop);
183 if (java_thread == NULL) {
184 return JVMTI_ERROR_THREAD_NOT_ALIVE;
185 }
186 JvmtiThreadState* state = java_thread->jvmti_thread_state();
187 *data_ptr = (state == NULL) ? NULL :
188 state->env_thread_state(this)->get_agent_thread_local_storage_data();
189 }
190 return JVMTI_ERROR_NONE;
191 } /* end GetThreadLocalStorage */
192
193 //
194 // Module functions
195 //
196
197 // module_count_ptr - pre-checked for NULL
198 // modules_ptr - pre-checked for NULL
199 jvmtiError
200 JvmtiEnv::GetAllModules(jint* module_count_ptr, jobject** modules_ptr) {
201 JvmtiModuleClosure jmc;
202
203 return jmc.get_all_modules(this, module_count_ptr, modules_ptr);
204 } /* end GetAllModules */
205
501 return set_native_method_prefixes(prefix_count, prefixes);
502 }
503 } /* end SetNativeMethodPrefixes */
504
505 //
506 // Event Management functions
507 //
508
509 // callbacks - NULL is a valid value, must be checked
510 // size_of_callbacks - pre-checked to be greater than or equal to 0
511 jvmtiError
512 JvmtiEnv::SetEventCallbacks(const jvmtiEventCallbacks* callbacks, jint size_of_callbacks) {
513 JvmtiEventController::set_event_callbacks(this, callbacks, size_of_callbacks);
514 return JVMTI_ERROR_NONE;
515 } /* end SetEventCallbacks */
516
517
518 // event_thread - NULL is a valid value, must be checked
519 jvmtiError
520 JvmtiEnv::SetEventNotificationMode(jvmtiEventMode mode, jvmtiEvent event_type, jthread event_thread, ...) {
521 JavaThread* java_thread = NULL;
522 if (event_thread != NULL) {
523 oop thread_oop = JNIHandles::resolve_external_guard(event_thread);
524 if (thread_oop == NULL) {
525 return JVMTI_ERROR_INVALID_THREAD;
526 }
527 if (!thread_oop->is_a(SystemDictionary::Thread_klass())) {
528 return JVMTI_ERROR_INVALID_THREAD;
529 }
530 java_thread = java_lang_Thread::thread(thread_oop);
531 if (java_thread == NULL) {
532 return JVMTI_ERROR_THREAD_NOT_ALIVE;
533 }
534 }
535
536 // event_type must be valid
537 if (!JvmtiEventController::is_valid_event_type(event_type)) {
538 return JVMTI_ERROR_INVALID_EVENT_TYPE;
539 }
540
541 // global events cannot be controlled at thread level.
542 if (java_thread != NULL && JvmtiEventController::is_global_event(event_type)) {
543 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
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 JvmtiEventController::set_user_enabled(this, java_thread, event_type, enabled);
557
558 return JVMTI_ERROR_NONE;
559 } /* end SetEventNotificationMode */
560
561 //
562 // Capability functions
563 //
564
565 // capabilities_ptr - pre-checked for NULL
566 jvmtiError
567 JvmtiEnv::GetPotentialCapabilities(jvmtiCapabilities* capabilities_ptr) {
568 JvmtiManageCapabilities::get_potential_capabilities(get_capabilities(),
569 get_prohibited_capabilities(),
570 capabilities_ptr);
571 return JVMTI_ERROR_NONE;
572 } /* end GetPotentialCapabilities */
573
574
575 // capabilities_ptr - pre-checked for NULL
576 jvmtiError
800 return JVMTI_ERROR_NONE;
801 } /* end SetVerboseFlag */
802
803
804 // format_ptr - pre-checked for NULL
805 jvmtiError
806 JvmtiEnv::GetJLocationFormat(jvmtiJlocationFormat* format_ptr) {
807 *format_ptr = JVMTI_JLOCATION_JVMBCI;
808 return JVMTI_ERROR_NONE;
809 } /* end GetJLocationFormat */
810
811 //
812 // Thread functions
813 //
814
815 // Threads_lock NOT held
816 // thread - NOT pre-checked
817 // thread_state_ptr - pre-checked for NULL
818 jvmtiError
819 JvmtiEnv::GetThreadState(jthread thread, jint* thread_state_ptr) {
820 jint state;
821 oop thread_oop;
822 JavaThread* thr;
823
824 if (thread == NULL) {
825 thread_oop = JavaThread::current()->threadObj();
826 } else {
827 thread_oop = JNIHandles::resolve_external_guard(thread);
828 }
829
830 if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass())) {
831 return JVMTI_ERROR_INVALID_THREAD;
832 }
833
834 // get most state bits
835 state = (jint)java_lang_Thread::get_thread_status(thread_oop);
836
837 // add more state bits
838 thr = java_lang_Thread::thread(thread_oop);
839 if (thr != NULL) {
840 JavaThreadState jts = thr->thread_state();
841
842 if (thr->is_being_ext_suspended()) {
843 state |= JVMTI_THREAD_STATE_SUSPENDED;
844 }
845 if (jts == _thread_in_native) {
846 state |= JVMTI_THREAD_STATE_IN_NATIVE;
847 }
848 OSThread* osThread = thr->osthread();
849 if (osThread != NULL && osThread->interrupted()) {
850 state |= JVMTI_THREAD_STATE_INTERRUPTED;
851 }
852 }
853
854 *thread_state_ptr = state;
855 return JVMTI_ERROR_NONE;
856 } /* end GetThreadState */
857
858
859 // thread_ptr - pre-checked for NULL
860 jvmtiError
861 JvmtiEnv::GetCurrentThread(jthread* thread_ptr) {
862 JavaThread* current_thread = JavaThread::current();
863 *thread_ptr = (jthread)JNIHandles::make_local(current_thread, current_thread->threadObj());
864 return JVMTI_ERROR_NONE;
865 } /* end GetCurrentThread */
866
867
868 // threads_count_ptr - pre-checked for NULL
874 ResourceMark rm;
875 HandleMark hm;
876
877 // enumerate threads (including agent threads)
878 ThreadsListEnumerator tle(Thread::current(), true);
879 nthreads = tle.num_threads();
880 *threads_count_ptr = nthreads;
881
882 if (nthreads == 0) {
883 *threads_ptr = NULL;
884 return JVMTI_ERROR_NONE;
885 }
886
887 thread_objs = NEW_RESOURCE_ARRAY(Handle, nthreads);
888 NULL_CHECK(thread_objs, JVMTI_ERROR_OUT_OF_MEMORY);
889
890 for (int i=0; i < nthreads; i++) {
891 thread_objs[i] = Handle(tle.get_threadObj(i));
892 }
893
894 // have to make global handles outside of Threads_lock
895 jthread *jthreads = new_jthreadArray(nthreads, thread_objs);
896 NULL_CHECK(jthreads, JVMTI_ERROR_OUT_OF_MEMORY);
897
898 *threads_ptr = jthreads;
899 return JVMTI_ERROR_NONE;
900 } /* end GetAllThreads */
901
902
903 // Threads_lock NOT held, java_thread not protected by lock
904 // java_thread - pre-checked
905 jvmtiError
906 JvmtiEnv::SuspendThread(JavaThread* java_thread) {
907 // don't allow hidden thread suspend request.
908 if (java_thread->is_hidden_from_external_view()) {
909 return (JVMTI_ERROR_NONE);
910 }
911
912 {
913 MutexLockerEx ml(java_thread->SR_lock(), Mutex::_no_safepoint_check_flag);
914 if (java_thread->is_external_suspend()) {
918 if (java_thread->is_exiting()) { // thread is in the process of exiting
919 return (JVMTI_ERROR_THREAD_NOT_ALIVE);
920 }
921 java_thread->set_external_suspend();
922 }
923
924 if (!JvmtiSuspendControl::suspend(java_thread)) {
925 // the thread was in the process of exiting
926 return (JVMTI_ERROR_THREAD_NOT_ALIVE);
927 }
928 return JVMTI_ERROR_NONE;
929 } /* end SuspendThread */
930
931
932 // request_count - pre-checked to be greater than or equal to 0
933 // request_list - pre-checked for NULL
934 // results - pre-checked for NULL
935 jvmtiError
936 JvmtiEnv::SuspendThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
937 int needSafepoint = 0; // > 0 if we need a safepoint
938 for (int i = 0; i < request_count; i++) {
939 JavaThread *java_thread = get_JavaThread(request_list[i]);
940 if (java_thread == NULL) {
941 results[i] = JVMTI_ERROR_INVALID_THREAD;
942 continue;
943 }
944 // the thread has not yet run or has exited (not on threads list)
945 if (java_thread->threadObj() == NULL) {
946 results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
947 continue;
948 }
949 if (java_lang_Thread::thread(java_thread->threadObj()) == NULL) {
950 results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
951 continue;
952 }
953 // don't allow hidden thread suspend request.
954 if (java_thread->is_hidden_from_external_view()) {
955 results[i] = JVMTI_ERROR_NONE; // indicate successful suspend
956 continue;
957 }
958
959 {
960 MutexLockerEx ml(java_thread->SR_lock(), Mutex::_no_safepoint_check_flag);
961 if (java_thread->is_external_suspend()) {
962 // don't allow nested external suspend requests.
963 results[i] = JVMTI_ERROR_THREAD_SUSPENDED;
964 continue;
965 }
966 if (java_thread->is_exiting()) { // thread is in the process of exiting
967 results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
968 continue;
969 }
970 java_thread->set_external_suspend();
971 }
972 if (java_thread->thread_state() == _thread_in_native) {
1001 if (java_thread->is_hidden_from_external_view()) {
1002 return JVMTI_ERROR_NONE;
1003 }
1004
1005 if (!java_thread->is_being_ext_suspended()) {
1006 return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1007 }
1008
1009 if (!JvmtiSuspendControl::resume(java_thread)) {
1010 return JVMTI_ERROR_INTERNAL;
1011 }
1012 return JVMTI_ERROR_NONE;
1013 } /* end ResumeThread */
1014
1015
1016 // request_count - pre-checked to be greater than or equal to 0
1017 // request_list - pre-checked for NULL
1018 // results - pre-checked for NULL
1019 jvmtiError
1020 JvmtiEnv::ResumeThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
1021 for (int i = 0; i < request_count; i++) {
1022 JavaThread *java_thread = get_JavaThread(request_list[i]);
1023 if (java_thread == NULL) {
1024 results[i] = JVMTI_ERROR_INVALID_THREAD;
1025 continue;
1026 }
1027 // don't allow hidden thread resume request.
1028 if (java_thread->is_hidden_from_external_view()) {
1029 results[i] = JVMTI_ERROR_NONE; // indicate successful resume
1030 continue;
1031 }
1032 if (!java_thread->is_being_ext_suspended()) {
1033 results[i] = JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1034 continue;
1035 }
1036
1037 if (!JvmtiSuspendControl::resume(java_thread)) {
1038 results[i] = JVMTI_ERROR_INTERNAL;
1039 continue;
1040 }
1041
1042 results[i] = JVMTI_ERROR_NONE; // indicate successful suspend
1043 }
1044 // per-thread resume results returned via results parameter
1045 return JVMTI_ERROR_NONE;
1046 } /* end ResumeThreadList */
1047
1048
1049 // Threads_lock NOT held, java_thread not protected by lock
1050 // java_thread - pre-checked
1051 jvmtiError
1052 JvmtiEnv::StopThread(JavaThread* java_thread, jobject exception) {
1053 oop e = JNIHandles::resolve_external_guard(exception);
1054 NULL_CHECK(e, JVMTI_ERROR_NULL_POINTER);
1055
1056 JavaThread::send_async_exception(java_thread->threadObj(), e);
1057
1058 return JVMTI_ERROR_NONE;
1059
1060 } /* end StopThread */
1061
1062
1063 // Threads_lock NOT held
1064 // thread - NOT pre-checked
1065 jvmtiError
1066 JvmtiEnv::InterruptThread(jthread thread) {
1067 oop thread_oop = JNIHandles::resolve_external_guard(thread);
1068 if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass()))
1069 return JVMTI_ERROR_INVALID_THREAD;
1070
1071 JavaThread* current_thread = JavaThread::current();
1072
1073 // Todo: this is a duplicate of JVM_Interrupt; share code in future
1074 // Ensure that the C++ Thread and OSThread structures aren't freed before we operate
1075 MutexLockerEx ml(current_thread->threadObj() == thread_oop ? NULL : Threads_lock);
1076 // We need to re-resolve the java_thread, since a GC might have happened during the
1077 // acquire of the lock
1078
1079 JavaThread* java_thread = java_lang_Thread::thread(JNIHandles::resolve_external_guard(thread));
1080 NULL_CHECK(java_thread, JVMTI_ERROR_THREAD_NOT_ALIVE);
1081
1082 Thread::interrupt(java_thread);
1083
1084 return JVMTI_ERROR_NONE;
1085 } /* end InterruptThread */
1086
1087
1088 // Threads_lock NOT held
1089 // thread - NOT pre-checked
1090 // info_ptr - pre-checked for NULL
1091 jvmtiError
1092 JvmtiEnv::GetThreadInfo(jthread thread, jvmtiThreadInfo* info_ptr) {
1093 ResourceMark rm;
1094 HandleMark hm;
1095
1096 JavaThread* current_thread = JavaThread::current();
1097
1098 // if thread is NULL the current thread is used
1099 oop thread_oop;
1100 if (thread == NULL) {
1101 thread_oop = current_thread->threadObj();
1102 } else {
1103 thread_oop = JNIHandles::resolve_external_guard(thread);
1104 }
1105 if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass()))
1106 return JVMTI_ERROR_INVALID_THREAD;
1107
1108 Handle thread_obj(current_thread, thread_oop);
1109 Handle name;
1110 ThreadPriority priority;
1111 Handle thread_group;
1112 Handle context_class_loader;
1113 bool is_daemon;
1114
1115 { MutexLocker mu(Threads_lock);
1116
1117 name = Handle(current_thread, java_lang_Thread::name(thread_obj()));
1118 priority = java_lang_Thread::priority(thread_obj());
1119 thread_group = Handle(current_thread, java_lang_Thread::threadGroup(thread_obj()));
1120 is_daemon = java_lang_Thread::is_daemon(thread_obj());
1121
1122 oop loader = java_lang_Thread::context_class_loader(thread_obj());
1123 context_class_loader = Handle(current_thread, loader);
1124 }
1125 { const char *n;
1126
1255 // It is only safe to perform the direct operation on the current
1256 // thread. All other usage needs to use a vm-safepoint-op for safety.
1257 if (java_thread == calling_thread) {
1258 err = get_current_contended_monitor(calling_thread, java_thread, monitor_ptr);
1259 } else {
1260 // get contended monitor information at safepoint.
1261 VM_GetCurrentContendedMonitor op(this, calling_thread, java_thread, monitor_ptr);
1262 VMThread::execute(&op);
1263 err = op.result();
1264 }
1265 return err;
1266 } /* end GetCurrentContendedMonitor */
1267
1268
1269 // Threads_lock NOT held
1270 // thread - NOT pre-checked
1271 // proc - pre-checked for NULL
1272 // arg - NULL is a valid value, must be checked
1273 jvmtiError
1274 JvmtiEnv::RunAgentThread(jthread thread, jvmtiStartFunction proc, const void* arg, jint priority) {
1275 oop thread_oop = JNIHandles::resolve_external_guard(thread);
1276 if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass())) {
1277 return JVMTI_ERROR_INVALID_THREAD;
1278 }
1279 if (priority < JVMTI_THREAD_MIN_PRIORITY || priority > JVMTI_THREAD_MAX_PRIORITY) {
1280 return JVMTI_ERROR_INVALID_PRIORITY;
1281 }
1282
1283 //Thread-self
1284 JavaThread* current_thread = JavaThread::current();
1285
1286 Handle thread_hndl(current_thread, thread_oop);
1287 {
1288 MutexLocker mu(Threads_lock); // grab Threads_lock
1289
1290 JvmtiAgentThread *new_thread = new JvmtiAgentThread(this, proc, arg);
1291
1292 // At this point it may be possible that no osthread was created for the
1293 // JavaThread due to lack of memory.
1294 if (new_thread == NULL || new_thread->osthread() == NULL) {
1295 if (new_thread) delete new_thread;
1296 return JVMTI_ERROR_OUT_OF_MEMORY;
1297 }
1298
1299 java_lang_Thread::set_thread(thread_hndl(), new_thread);
1300 java_lang_Thread::set_priority(thread_hndl(), (ThreadPriority)priority);
1301 java_lang_Thread::set_daemon(thread_hndl());
1302
1303 new_thread->set_threadObj(thread_hndl());
1304 Threads::add(new_thread);
1305 Thread::start(new_thread);
1306 } // unlock Threads_lock
1307
1308 return JVMTI_ERROR_NONE;
1309 } /* end RunAgentThread */
1310
1311 //
1312 // Thread Group functions
1313 //
1314
1315 // group_count_ptr - pre-checked for NULL
1377 return JVMTI_ERROR_NONE;
1378 } /* end GetThreadGroupInfo */
1379
1380
1381 // thread_count_ptr - pre-checked for NULL
1382 // threads_ptr - pre-checked for NULL
1383 // group_count_ptr - pre-checked for NULL
1384 // groups_ptr - pre-checked for NULL
1385 jvmtiError
1386 JvmtiEnv::GetThreadGroupChildren(jthreadGroup group, jint* thread_count_ptr, jthread** threads_ptr, jint* group_count_ptr, jthreadGroup** groups_ptr) {
1387 JavaThread* current_thread = JavaThread::current();
1388 oop group_obj = (oop) JNIHandles::resolve_external_guard(group);
1389 NULL_CHECK(group_obj, JVMTI_ERROR_INVALID_THREAD_GROUP);
1390
1391 Handle *thread_objs = NULL;
1392 Handle *group_objs = NULL;
1393 int nthreads = 0;
1394 int ngroups = 0;
1395 int hidden_threads = 0;
1396
1397 ResourceMark rm;
1398 HandleMark hm;
1399
1400 Handle group_hdl(current_thread, group_obj);
1401
1402 { MutexLocker mu(Threads_lock);
1403
1404 nthreads = java_lang_ThreadGroup::nthreads(group_hdl());
1405 ngroups = java_lang_ThreadGroup::ngroups(group_hdl());
1406
1407 if (nthreads > 0) {
1408 objArrayOop threads = java_lang_ThreadGroup::threads(group_hdl());
1409 assert(nthreads <= threads->length(), "too many threads");
1410 thread_objs = NEW_RESOURCE_ARRAY(Handle,nthreads);
1411 for (int i=0, j=0; i<nthreads; i++) {
1412 oop thread_obj = threads->obj_at(i);
1413 assert(thread_obj != NULL, "thread_obj is NULL");
1414 JavaThread *javathread = java_lang_Thread::thread(thread_obj);
1415 // Filter out hidden java threads.
1416 if (javathread != NULL && javathread->is_hidden_from_external_view()) {
1417 hidden_threads++;
1418 continue;
1419 }
1420 thread_objs[j++] = Handle(current_thread, thread_obj);
1421 }
1422 nthreads -= hidden_threads;
1423 }
1424 if (ngroups > 0) {
1425 objArrayOop groups = java_lang_ThreadGroup::groups(group_hdl());
1426 assert(ngroups <= groups->length(), "too many threads");
1427 group_objs = NEW_RESOURCE_ARRAY(Handle,ngroups);
1428 for (int i=0; i<ngroups; i++) {
1429 oop group_obj = groups->obj_at(i);
1430 assert(group_obj != NULL, "group_obj != NULL");
1431 group_objs[i] = Handle(current_thread, group_obj);
1432 }
1433 }
1434 }
1435
1436 // have to make global handles outside of Threads_lock
1437 *group_count_ptr = ngroups;
1438 *thread_count_ptr = nthreads;
1439 *threads_ptr = new_jthreadArray(nthreads, thread_objs);
1440 *groups_ptr = new_jthreadGroupArray(ngroups, group_objs);
1441 if ((nthreads > 0) && (*threads_ptr == NULL)) {
1442 return JVMTI_ERROR_OUT_OF_MEMORY;
1443 }
1444 if ((ngroups > 0) && (*groups_ptr == NULL)) {
1445 return JVMTI_ERROR_OUT_OF_MEMORY;
1446 }
1539 }
1540 return err;
1541 } /* end GetFrameCount */
1542
1543
1544 // Threads_lock NOT held, java_thread not protected by lock
1545 // java_thread - pre-checked
1546 jvmtiError
1547 JvmtiEnv::PopFrame(JavaThread* java_thread) {
1548 JavaThread* current_thread = JavaThread::current();
1549 HandleMark hm(current_thread);
1550 uint32_t debug_bits = 0;
1551
1552 // retrieve or create the state
1553 JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
1554 if (state == NULL) {
1555 return JVMTI_ERROR_THREAD_NOT_ALIVE;
1556 }
1557
1558 // Check if java_thread is fully suspended
1559 if (!is_thread_fully_suspended(java_thread, true /* wait for suspend completion */, &debug_bits)) {
1560 return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1561 }
1562 // Check to see if a PopFrame was already in progress
1563 if (java_thread->popframe_condition() != JavaThread::popframe_inactive) {
1564 // Probably possible for JVMTI clients to trigger this, but the
1565 // JPDA backend shouldn't allow this to happen
1566 return JVMTI_ERROR_INTERNAL;
1567 }
1568
1569 {
1570 // Was workaround bug
1571 // 4812902: popFrame hangs if the method is waiting at a synchronize
1572 // Catch this condition and return an error to avoid hanging.
1573 // Now JVMTI spec allows an implementation to bail out with an opaque frame error.
1574 OSThread* osThread = java_thread->osthread();
1575 if (osThread->get_state() == MONITOR_WAIT) {
1576 return JVMTI_ERROR_OPAQUE_FRAME;
1577 }
1578 }
1579
1669 }
1670 return err;
1671 } /* end GetFrameLocation */
1672
1673
1674 // Threads_lock NOT held, java_thread not protected by lock
1675 // java_thread - pre-checked
1676 // java_thread - unchecked
1677 // depth - pre-checked as non-negative
1678 jvmtiError
1679 JvmtiEnv::NotifyFramePop(JavaThread* java_thread, jint depth) {
1680 jvmtiError err = JVMTI_ERROR_NONE;
1681 ResourceMark rm;
1682 uint32_t debug_bits = 0;
1683
1684 JvmtiThreadState *state = JvmtiThreadState::state_for(java_thread);
1685 if (state == NULL) {
1686 return JVMTI_ERROR_THREAD_NOT_ALIVE;
1687 }
1688
1689 if (!JvmtiEnv::is_thread_fully_suspended(java_thread, true, &debug_bits)) {
1690 return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1691 }
1692
1693 if (TraceJVMTICalls) {
1694 JvmtiSuspendControl::print();
1695 }
1696
1697 vframe *vf = vframeFor(java_thread, depth);
1698 if (vf == NULL) {
1699 return JVMTI_ERROR_NO_MORE_FRAMES;
1700 }
1701
1702 if (!vf->is_java_frame() || ((javaVFrame*) vf)->method()->is_native()) {
1703 return JVMTI_ERROR_OPAQUE_FRAME;
1704 }
1705
1706 assert(vf->frame_pointer() != NULL, "frame pointer mustn't be NULL");
1707
1708 // It is only safe to perform the direct operation on the current
1709 // thread. All other usage needs to use a vm-safepoint-op for safety.
|
45 #include "prims/jvmtiClassFileReconstituter.hpp"
46 #include "prims/jvmtiCodeBlobEvents.hpp"
47 #include "prims/jvmtiExtensions.hpp"
48 #include "prims/jvmtiGetLoadedClasses.hpp"
49 #include "prims/jvmtiImpl.hpp"
50 #include "prims/jvmtiManageCapabilities.hpp"
51 #include "prims/jvmtiRawMonitor.hpp"
52 #include "prims/jvmtiRedefineClasses.hpp"
53 #include "prims/jvmtiTagMap.hpp"
54 #include "prims/jvmtiThreadState.inline.hpp"
55 #include "prims/jvmtiUtil.hpp"
56 #include "runtime/arguments.hpp"
57 #include "runtime/deoptimization.hpp"
58 #include "runtime/interfaceSupport.hpp"
59 #include "runtime/javaCalls.hpp"
60 #include "runtime/jfieldIDWorkaround.hpp"
61 #include "runtime/osThread.hpp"
62 #include "runtime/reflectionUtils.hpp"
63 #include "runtime/signature.hpp"
64 #include "runtime/thread.inline.hpp"
65 #include "runtime/threadSMR.hpp"
66 #include "runtime/timerTrace.hpp"
67 #include "runtime/vframe.hpp"
68 #include "runtime/vmThread.hpp"
69 #include "services/threadService.hpp"
70 #include "utilities/exceptions.hpp"
71 #include "utilities/preserveException.hpp"
72
73
74 #define FIXLATER 0 // REMOVE this when completed.
75
76 // FIXLATER: hook into JvmtiTrace
77 #define TraceJVMTICalls false
78
79 JvmtiEnv::JvmtiEnv(jint version) : JvmtiEnvBase(version) {
80 }
81
82 JvmtiEnv::~JvmtiEnv() {
83 }
84
85 JvmtiEnv*
146 if (state == NULL) {
147 return JVMTI_ERROR_THREAD_NOT_ALIVE;
148 }
149 }
150 state->env_thread_state(this)->set_agent_thread_local_storage_data((void*)data);
151 return JVMTI_ERROR_NONE;
152 } /* end SetThreadLocalStorage */
153
154
155 // Threads_lock NOT held
156 // thread - NOT pre-checked
157 // data_ptr - pre-checked for NULL
158 jvmtiError
159 JvmtiEnv::GetThreadLocalStorage(jthread thread, void** data_ptr) {
160 JavaThread* current_thread = JavaThread::current();
161 if (thread == NULL) {
162 JvmtiThreadState* state = current_thread->jvmti_thread_state();
163 *data_ptr = (state == NULL) ? NULL :
164 state->env_thread_state(this)->get_agent_thread_local_storage_data();
165 } else {
166 // jvmti_GetThreadLocalStorage is "in native" and doesn't transition
167 // the thread to _thread_in_vm. However, when the TLS for a thread
168 // other than the current thread is required we need to transition
169 // from native so as to resolve the jthread.
170
171 ThreadInVMfromNative __tiv(current_thread);
172 VM_ENTRY_BASE(jvmtiError, JvmtiEnv::GetThreadLocalStorage , current_thread)
173 debug_only(VMNativeEntryWrapper __vew;)
174
175 JavaThread* java_thread = NULL;
176 ThreadsListHandle tlh(current_thread);
177 jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, NULL);
178 if (err != JVMTI_ERROR_NONE) {
179 return err;
180 }
181
182 JvmtiThreadState* state = java_thread->jvmti_thread_state();
183 *data_ptr = (state == NULL) ? NULL :
184 state->env_thread_state(this)->get_agent_thread_local_storage_data();
185 }
186 return JVMTI_ERROR_NONE;
187 } /* end GetThreadLocalStorage */
188
189 //
190 // Module functions
191 //
192
193 // module_count_ptr - pre-checked for NULL
194 // modules_ptr - pre-checked for NULL
195 jvmtiError
196 JvmtiEnv::GetAllModules(jint* module_count_ptr, jobject** modules_ptr) {
197 JvmtiModuleClosure jmc;
198
199 return jmc.get_all_modules(this, module_count_ptr, modules_ptr);
200 } /* end GetAllModules */
201
497 return set_native_method_prefixes(prefix_count, prefixes);
498 }
499 } /* end SetNativeMethodPrefixes */
500
501 //
502 // Event Management functions
503 //
504
505 // callbacks - NULL is a valid value, must be checked
506 // size_of_callbacks - pre-checked to be greater than or equal to 0
507 jvmtiError
508 JvmtiEnv::SetEventCallbacks(const jvmtiEventCallbacks* callbacks, jint size_of_callbacks) {
509 JvmtiEventController::set_event_callbacks(this, callbacks, size_of_callbacks);
510 return JVMTI_ERROR_NONE;
511 } /* end SetEventCallbacks */
512
513
514 // event_thread - NULL is a valid value, must be checked
515 jvmtiError
516 JvmtiEnv::SetEventNotificationMode(jvmtiEventMode mode, jvmtiEvent event_type, jthread event_thread, ...) {
517 if (event_thread == NULL) {
518 // Can be called at Agent_OnLoad() time with event_thread == NULL
519 // when Thread::current() does not work yet so we cannot create a
520 // ThreadsListHandle that is common to both thread-specific and
521 // global code paths.
522
523 // event_type must be valid
524 if (!JvmtiEventController::is_valid_event_type(event_type)) {
525 return JVMTI_ERROR_INVALID_EVENT_TYPE;
526 }
527
528 bool enabled = (mode == JVMTI_ENABLE);
529
530 // assure that needed capabilities are present
531 if (enabled && !JvmtiUtil::has_event_capability(event_type, get_capabilities())) {
532 return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
533 }
534
535 if (event_type == JVMTI_EVENT_CLASS_FILE_LOAD_HOOK && enabled) {
536 record_class_file_load_hook_enabled();
537 }
538 JvmtiEventController::set_user_enabled(this, (JavaThread*) NULL, event_type, enabled);
539 } else {
540 // We have a specified event_thread.
541
542 JavaThread* java_thread = NULL;
543 ThreadsListHandle tlh;
544 jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), event_thread, &java_thread, NULL);
545 if (err != JVMTI_ERROR_NONE) {
546 return err;
547 }
548
549 // event_type must be valid
550 if (!JvmtiEventController::is_valid_event_type(event_type)) {
551 return JVMTI_ERROR_INVALID_EVENT_TYPE;
552 }
553
554 // global events cannot be controlled at thread level.
555 if (JvmtiEventController::is_global_event(event_type)) {
556 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
557 }
558
559 bool enabled = (mode == JVMTI_ENABLE);
560
561 // assure that needed capabilities are present
562 if (enabled && !JvmtiUtil::has_event_capability(event_type, get_capabilities())) {
563 return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
564 }
565
566 if (event_type == JVMTI_EVENT_CLASS_FILE_LOAD_HOOK && enabled) {
567 record_class_file_load_hook_enabled();
568 }
569 JvmtiEventController::set_user_enabled(this, java_thread, event_type, enabled);
570 }
571
572 return JVMTI_ERROR_NONE;
573 } /* end SetEventNotificationMode */
574
575 //
576 // Capability functions
577 //
578
579 // capabilities_ptr - pre-checked for NULL
580 jvmtiError
581 JvmtiEnv::GetPotentialCapabilities(jvmtiCapabilities* capabilities_ptr) {
582 JvmtiManageCapabilities::get_potential_capabilities(get_capabilities(),
583 get_prohibited_capabilities(),
584 capabilities_ptr);
585 return JVMTI_ERROR_NONE;
586 } /* end GetPotentialCapabilities */
587
588
589 // capabilities_ptr - pre-checked for NULL
590 jvmtiError
814 return JVMTI_ERROR_NONE;
815 } /* end SetVerboseFlag */
816
817
818 // format_ptr - pre-checked for NULL
819 jvmtiError
820 JvmtiEnv::GetJLocationFormat(jvmtiJlocationFormat* format_ptr) {
821 *format_ptr = JVMTI_JLOCATION_JVMBCI;
822 return JVMTI_ERROR_NONE;
823 } /* end GetJLocationFormat */
824
825 //
826 // Thread functions
827 //
828
829 // Threads_lock NOT held
830 // thread - NOT pre-checked
831 // thread_state_ptr - pre-checked for NULL
832 jvmtiError
833 JvmtiEnv::GetThreadState(jthread thread, jint* thread_state_ptr) {
834 JavaThread* current_thread = JavaThread::current();
835 JavaThread* java_thread = NULL;
836 oop thread_oop = NULL;
837 ThreadsListHandle tlh(current_thread);
838
839 if (thread == NULL) {
840 java_thread = current_thread;
841 thread_oop = java_thread->threadObj();
842
843 if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass())) {
844 return JVMTI_ERROR_INVALID_THREAD;
845 }
846 } else {
847 jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
848 if (err != JVMTI_ERROR_NONE) {
849 // We got an error code so we don't have a JavaThread *, but
850 // only return an error from here if we didn't get a valid
851 // thread_oop.
852 if (thread_oop == NULL) {
853 return err;
854 }
855 // We have a valid thread_oop so we can return some thread state.
856 }
857 }
858
859 // get most state bits
860 jint state = (jint)java_lang_Thread::get_thread_status(thread_oop);
861
862 if (java_thread != NULL) {
863 // We have a JavaThread* so add more state bits.
864 JavaThreadState jts = java_thread->thread_state();
865
866 if (java_thread->is_being_ext_suspended()) {
867 state |= JVMTI_THREAD_STATE_SUSPENDED;
868 }
869 if (jts == _thread_in_native) {
870 state |= JVMTI_THREAD_STATE_IN_NATIVE;
871 }
872 OSThread* osThread = java_thread->osthread();
873 if (osThread != NULL && osThread->interrupted()) {
874 state |= JVMTI_THREAD_STATE_INTERRUPTED;
875 }
876 }
877
878 *thread_state_ptr = state;
879 return JVMTI_ERROR_NONE;
880 } /* end GetThreadState */
881
882
883 // thread_ptr - pre-checked for NULL
884 jvmtiError
885 JvmtiEnv::GetCurrentThread(jthread* thread_ptr) {
886 JavaThread* current_thread = JavaThread::current();
887 *thread_ptr = (jthread)JNIHandles::make_local(current_thread, current_thread->threadObj());
888 return JVMTI_ERROR_NONE;
889 } /* end GetCurrentThread */
890
891
892 // threads_count_ptr - pre-checked for NULL
898 ResourceMark rm;
899 HandleMark hm;
900
901 // enumerate threads (including agent threads)
902 ThreadsListEnumerator tle(Thread::current(), true);
903 nthreads = tle.num_threads();
904 *threads_count_ptr = nthreads;
905
906 if (nthreads == 0) {
907 *threads_ptr = NULL;
908 return JVMTI_ERROR_NONE;
909 }
910
911 thread_objs = NEW_RESOURCE_ARRAY(Handle, nthreads);
912 NULL_CHECK(thread_objs, JVMTI_ERROR_OUT_OF_MEMORY);
913
914 for (int i=0; i < nthreads; i++) {
915 thread_objs[i] = Handle(tle.get_threadObj(i));
916 }
917
918 jthread *jthreads = new_jthreadArray(nthreads, thread_objs);
919 NULL_CHECK(jthreads, JVMTI_ERROR_OUT_OF_MEMORY);
920
921 *threads_ptr = jthreads;
922 return JVMTI_ERROR_NONE;
923 } /* end GetAllThreads */
924
925
926 // Threads_lock NOT held, java_thread not protected by lock
927 // java_thread - pre-checked
928 jvmtiError
929 JvmtiEnv::SuspendThread(JavaThread* java_thread) {
930 // don't allow hidden thread suspend request.
931 if (java_thread->is_hidden_from_external_view()) {
932 return (JVMTI_ERROR_NONE);
933 }
934
935 {
936 MutexLockerEx ml(java_thread->SR_lock(), Mutex::_no_safepoint_check_flag);
937 if (java_thread->is_external_suspend()) {
941 if (java_thread->is_exiting()) { // thread is in the process of exiting
942 return (JVMTI_ERROR_THREAD_NOT_ALIVE);
943 }
944 java_thread->set_external_suspend();
945 }
946
947 if (!JvmtiSuspendControl::suspend(java_thread)) {
948 // the thread was in the process of exiting
949 return (JVMTI_ERROR_THREAD_NOT_ALIVE);
950 }
951 return JVMTI_ERROR_NONE;
952 } /* end SuspendThread */
953
954
955 // request_count - pre-checked to be greater than or equal to 0
956 // request_list - pre-checked for NULL
957 // results - pre-checked for NULL
958 jvmtiError
959 JvmtiEnv::SuspendThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
960 int needSafepoint = 0; // > 0 if we need a safepoint
961 ThreadsListHandle tlh;
962 for (int i = 0; i < request_count; i++) {
963 JavaThread *java_thread = NULL;
964 jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), request_list[i], &java_thread, NULL);
965 if (err != JVMTI_ERROR_NONE) {
966 results[i] = err;
967 continue;
968 }
969 // don't allow hidden thread suspend request.
970 if (java_thread->is_hidden_from_external_view()) {
971 results[i] = JVMTI_ERROR_NONE; // indicate successful suspend
972 continue;
973 }
974
975 {
976 MutexLockerEx ml(java_thread->SR_lock(), Mutex::_no_safepoint_check_flag);
977 if (java_thread->is_external_suspend()) {
978 // don't allow nested external suspend requests.
979 results[i] = JVMTI_ERROR_THREAD_SUSPENDED;
980 continue;
981 }
982 if (java_thread->is_exiting()) { // thread is in the process of exiting
983 results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
984 continue;
985 }
986 java_thread->set_external_suspend();
987 }
988 if (java_thread->thread_state() == _thread_in_native) {
1017 if (java_thread->is_hidden_from_external_view()) {
1018 return JVMTI_ERROR_NONE;
1019 }
1020
1021 if (!java_thread->is_being_ext_suspended()) {
1022 return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1023 }
1024
1025 if (!JvmtiSuspendControl::resume(java_thread)) {
1026 return JVMTI_ERROR_INTERNAL;
1027 }
1028 return JVMTI_ERROR_NONE;
1029 } /* end ResumeThread */
1030
1031
1032 // request_count - pre-checked to be greater than or equal to 0
1033 // request_list - pre-checked for NULL
1034 // results - pre-checked for NULL
1035 jvmtiError
1036 JvmtiEnv::ResumeThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
1037 ThreadsListHandle tlh;
1038 for (int i = 0; i < request_count; i++) {
1039 JavaThread* java_thread = NULL;
1040 jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), request_list[i], &java_thread, NULL);
1041 if (err != JVMTI_ERROR_NONE) {
1042 results[i] = err;
1043 continue;
1044 }
1045 // don't allow hidden thread resume request.
1046 if (java_thread->is_hidden_from_external_view()) {
1047 results[i] = JVMTI_ERROR_NONE; // indicate successful resume
1048 continue;
1049 }
1050 if (!java_thread->is_being_ext_suspended()) {
1051 results[i] = JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1052 continue;
1053 }
1054
1055 if (!JvmtiSuspendControl::resume(java_thread)) {
1056 results[i] = JVMTI_ERROR_INTERNAL;
1057 continue;
1058 }
1059
1060 results[i] = JVMTI_ERROR_NONE; // indicate successful resume
1061 }
1062 // per-thread resume results returned via results parameter
1063 return JVMTI_ERROR_NONE;
1064 } /* end ResumeThreadList */
1065
1066
1067 // Threads_lock NOT held, java_thread not protected by lock
1068 // java_thread - pre-checked
1069 jvmtiError
1070 JvmtiEnv::StopThread(JavaThread* java_thread, jobject exception) {
1071 oop e = JNIHandles::resolve_external_guard(exception);
1072 NULL_CHECK(e, JVMTI_ERROR_NULL_POINTER);
1073
1074 JavaThread::send_async_exception(java_thread->threadObj(), e);
1075
1076 return JVMTI_ERROR_NONE;
1077
1078 } /* end StopThread */
1079
1080
1081 // Threads_lock NOT held
1082 // thread - NOT pre-checked
1083 jvmtiError
1084 JvmtiEnv::InterruptThread(jthread thread) {
1085 // TODO: this is very similar to JVM_Interrupt(); share code in future
1086 JavaThread* current_thread = JavaThread::current();
1087 JavaThread* java_thread = NULL;
1088 ThreadsListHandle tlh(current_thread);
1089 jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, NULL);
1090 if (err != JVMTI_ERROR_NONE) {
1091 return err;
1092 }
1093
1094 Thread::interrupt(java_thread);
1095
1096 return JVMTI_ERROR_NONE;
1097 } /* end InterruptThread */
1098
1099
1100 // Threads_lock NOT held
1101 // thread - NOT pre-checked
1102 // info_ptr - pre-checked for NULL
1103 jvmtiError
1104 JvmtiEnv::GetThreadInfo(jthread thread, jvmtiThreadInfo* info_ptr) {
1105 ResourceMark rm;
1106 HandleMark hm;
1107
1108 JavaThread* current_thread = JavaThread::current();
1109 ThreadsListHandle tlh(current_thread);
1110
1111 // if thread is NULL the current thread is used
1112 oop thread_oop = NULL;
1113 if (thread == NULL) {
1114 thread_oop = current_thread->threadObj();
1115 if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass())) {
1116 return JVMTI_ERROR_INVALID_THREAD;
1117 }
1118 } else {
1119 JavaThread* java_thread = NULL;
1120 jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1121 if (err != JVMTI_ERROR_NONE) {
1122 // We got an error code so we don't have a JavaThread *, but
1123 // only return an error from here if we didn't get a valid
1124 // thread_oop.
1125 if (thread_oop == NULL) {
1126 return err;
1127 }
1128 // We have a valid thread_oop so we can return some thread info.
1129 }
1130 }
1131
1132 Handle thread_obj(current_thread, thread_oop);
1133 Handle name;
1134 ThreadPriority priority;
1135 Handle thread_group;
1136 Handle context_class_loader;
1137 bool is_daemon;
1138
1139 { MutexLocker mu(Threads_lock);
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
1279 // It is only safe to perform the direct operation on the current
1280 // thread. All other usage needs to use a vm-safepoint-op for safety.
1281 if (java_thread == calling_thread) {
1282 err = get_current_contended_monitor(calling_thread, java_thread, monitor_ptr);
1283 } else {
1284 // get contended monitor information at safepoint.
1285 VM_GetCurrentContendedMonitor op(this, calling_thread, java_thread, monitor_ptr);
1286 VMThread::execute(&op);
1287 err = op.result();
1288 }
1289 return err;
1290 } /* end GetCurrentContendedMonitor */
1291
1292
1293 // Threads_lock NOT held
1294 // thread - NOT pre-checked
1295 // proc - pre-checked for NULL
1296 // arg - NULL is a valid value, must be checked
1297 jvmtiError
1298 JvmtiEnv::RunAgentThread(jthread thread, jvmtiStartFunction proc, const void* arg, jint priority) {
1299 JavaThread* current_thread = JavaThread::current();
1300
1301 JavaThread* java_thread = NULL;
1302 oop thread_oop = NULL;
1303 ThreadsListHandle tlh(current_thread);
1304 jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1305 if (err != JVMTI_ERROR_NONE) {
1306 // We got an error code so we don't have a JavaThread *, but
1307 // only return an error from here if we didn't get a valid
1308 // thread_oop.
1309 if (thread_oop == NULL) {
1310 return err;
1311 }
1312 // We have a valid thread_oop.
1313 }
1314
1315 if (java_thread != NULL) {
1316 // 'thread' refers to an existing JavaThread.
1317 return JVMTI_ERROR_INVALID_THREAD;
1318 }
1319
1320 if (priority < JVMTI_THREAD_MIN_PRIORITY || priority > JVMTI_THREAD_MAX_PRIORITY) {
1321 return JVMTI_ERROR_INVALID_PRIORITY;
1322 }
1323
1324 Handle thread_hndl(current_thread, thread_oop);
1325 {
1326 MutexLocker mu(Threads_lock); // grab Threads_lock
1327
1328 JvmtiAgentThread *new_thread = new JvmtiAgentThread(this, proc, arg);
1329
1330 // At this point it may be possible that no osthread was created for the
1331 // JavaThread due to lack of memory.
1332 if (new_thread == NULL || new_thread->osthread() == NULL) {
1333 if (new_thread != NULL) {
1334 new_thread->smr_delete();
1335 }
1336 return JVMTI_ERROR_OUT_OF_MEMORY;
1337 }
1338
1339 java_lang_Thread::set_thread(thread_hndl(), new_thread);
1340 java_lang_Thread::set_priority(thread_hndl(), (ThreadPriority)priority);
1341 java_lang_Thread::set_daemon(thread_hndl());
1342
1343 new_thread->set_threadObj(thread_hndl());
1344 Threads::add(new_thread);
1345 Thread::start(new_thread);
1346 } // unlock Threads_lock
1347
1348 return JVMTI_ERROR_NONE;
1349 } /* end RunAgentThread */
1350
1351 //
1352 // Thread Group functions
1353 //
1354
1355 // group_count_ptr - pre-checked for NULL
1417 return JVMTI_ERROR_NONE;
1418 } /* end GetThreadGroupInfo */
1419
1420
1421 // thread_count_ptr - pre-checked for NULL
1422 // threads_ptr - pre-checked for NULL
1423 // group_count_ptr - pre-checked for NULL
1424 // groups_ptr - pre-checked for NULL
1425 jvmtiError
1426 JvmtiEnv::GetThreadGroupChildren(jthreadGroup group, jint* thread_count_ptr, jthread** threads_ptr, jint* group_count_ptr, jthreadGroup** groups_ptr) {
1427 JavaThread* current_thread = JavaThread::current();
1428 oop group_obj = (oop) JNIHandles::resolve_external_guard(group);
1429 NULL_CHECK(group_obj, JVMTI_ERROR_INVALID_THREAD_GROUP);
1430
1431 Handle *thread_objs = NULL;
1432 Handle *group_objs = NULL;
1433 int nthreads = 0;
1434 int ngroups = 0;
1435 int hidden_threads = 0;
1436
1437 ResourceMark rm(current_thread);
1438 HandleMark hm(current_thread);
1439
1440 Handle group_hdl(current_thread, group_obj);
1441
1442 { // Cannot allow thread or group counts to change.
1443 MutexLocker mu(Threads_lock);
1444
1445 nthreads = java_lang_ThreadGroup::nthreads(group_hdl());
1446 ngroups = java_lang_ThreadGroup::ngroups(group_hdl());
1447
1448 if (nthreads > 0) {
1449 ThreadsListHandle tlh(current_thread);
1450 objArrayOop threads = java_lang_ThreadGroup::threads(group_hdl());
1451 assert(nthreads <= threads->length(), "too many threads");
1452 thread_objs = NEW_RESOURCE_ARRAY(Handle,nthreads);
1453 for (int i=0, j=0; i<nthreads; i++) {
1454 oop thread_obj = threads->obj_at(i);
1455 assert(thread_obj != NULL, "thread_obj is NULL");
1456 JavaThread *java_thread = NULL;
1457 jvmtiError err = JvmtiExport::cv_oop_to_JavaThread(tlh.list(), thread_obj, &java_thread);
1458 if (err == JVMTI_ERROR_NONE) {
1459 // Have a valid JavaThread*.
1460 if (java_thread->is_hidden_from_external_view()) {
1461 // Filter out hidden java threads.
1462 hidden_threads++;
1463 continue;
1464 }
1465 } else {
1466 // We couldn't convert thread_obj into a JavaThread*.
1467 if (err == JVMTI_ERROR_INVALID_THREAD) {
1468 // The thread_obj does not refer to a java.lang.Thread object
1469 // so skip it.
1470 hidden_threads++;
1471 continue;
1472 }
1473 // We have a valid thread_obj, but no JavaThread*; the caller
1474 // can still have limited use for the thread_obj.
1475 }
1476 thread_objs[j++] = Handle(current_thread, thread_obj);
1477 }
1478 nthreads -= hidden_threads;
1479 } // ThreadsListHandle is destroyed here.
1480
1481 if (ngroups > 0) {
1482 objArrayOop groups = java_lang_ThreadGroup::groups(group_hdl());
1483 assert(ngroups <= groups->length(), "too many groups");
1484 group_objs = NEW_RESOURCE_ARRAY(Handle,ngroups);
1485 for (int i=0; i<ngroups; i++) {
1486 oop group_obj = groups->obj_at(i);
1487 assert(group_obj != NULL, "group_obj != NULL");
1488 group_objs[i] = Handle(current_thread, group_obj);
1489 }
1490 }
1491 }
1492
1493 // have to make global handles outside of Threads_lock
1494 *group_count_ptr = ngroups;
1495 *thread_count_ptr = nthreads;
1496 *threads_ptr = new_jthreadArray(nthreads, thread_objs);
1497 *groups_ptr = new_jthreadGroupArray(ngroups, group_objs);
1498 if ((nthreads > 0) && (*threads_ptr == NULL)) {
1499 return JVMTI_ERROR_OUT_OF_MEMORY;
1500 }
1501 if ((ngroups > 0) && (*groups_ptr == NULL)) {
1502 return JVMTI_ERROR_OUT_OF_MEMORY;
1503 }
1596 }
1597 return err;
1598 } /* end GetFrameCount */
1599
1600
1601 // Threads_lock NOT held, java_thread not protected by lock
1602 // java_thread - pre-checked
1603 jvmtiError
1604 JvmtiEnv::PopFrame(JavaThread* java_thread) {
1605 JavaThread* current_thread = JavaThread::current();
1606 HandleMark hm(current_thread);
1607 uint32_t debug_bits = 0;
1608
1609 // retrieve or create the state
1610 JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
1611 if (state == NULL) {
1612 return JVMTI_ERROR_THREAD_NOT_ALIVE;
1613 }
1614
1615 // Check if java_thread is fully suspended
1616 if (!java_thread->is_thread_fully_suspended(true /* wait for suspend completion */, &debug_bits)) {
1617 return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1618 }
1619 // Check to see if a PopFrame was already in progress
1620 if (java_thread->popframe_condition() != JavaThread::popframe_inactive) {
1621 // Probably possible for JVMTI clients to trigger this, but the
1622 // JPDA backend shouldn't allow this to happen
1623 return JVMTI_ERROR_INTERNAL;
1624 }
1625
1626 {
1627 // Was workaround bug
1628 // 4812902: popFrame hangs if the method is waiting at a synchronize
1629 // Catch this condition and return an error to avoid hanging.
1630 // Now JVMTI spec allows an implementation to bail out with an opaque frame error.
1631 OSThread* osThread = java_thread->osthread();
1632 if (osThread->get_state() == MONITOR_WAIT) {
1633 return JVMTI_ERROR_OPAQUE_FRAME;
1634 }
1635 }
1636
1726 }
1727 return err;
1728 } /* end GetFrameLocation */
1729
1730
1731 // Threads_lock NOT held, java_thread not protected by lock
1732 // java_thread - pre-checked
1733 // java_thread - unchecked
1734 // depth - pre-checked as non-negative
1735 jvmtiError
1736 JvmtiEnv::NotifyFramePop(JavaThread* java_thread, jint depth) {
1737 jvmtiError err = JVMTI_ERROR_NONE;
1738 ResourceMark rm;
1739 uint32_t debug_bits = 0;
1740
1741 JvmtiThreadState *state = JvmtiThreadState::state_for(java_thread);
1742 if (state == NULL) {
1743 return JVMTI_ERROR_THREAD_NOT_ALIVE;
1744 }
1745
1746 if (!java_thread->is_thread_fully_suspended(true, &debug_bits)) {
1747 return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1748 }
1749
1750 if (TraceJVMTICalls) {
1751 JvmtiSuspendControl::print();
1752 }
1753
1754 vframe *vf = vframeFor(java_thread, depth);
1755 if (vf == NULL) {
1756 return JVMTI_ERROR_NO_MORE_FRAMES;
1757 }
1758
1759 if (!vf->is_java_frame() || ((javaVFrame*) vf)->method()->is_native()) {
1760 return JVMTI_ERROR_OPAQUE_FRAME;
1761 }
1762
1763 assert(vf->frame_pointer() != NULL, "frame pointer mustn't be NULL");
1764
1765 // It is only safe to perform the direct operation on the current
1766 // thread. All other usage needs to use a vm-safepoint-op for safety.
|