920 return JVMTI_ERROR_INTERNAL; 921 } 922 #endif 923 924 HandleMark hm(current_thread); 925 javaVFrame *jvf = javaVFrame::cast(vf); 926 Method* method = jvf->method(); 927 if (method->is_native()) { 928 *location_ptr = -1; 929 } else { 930 *location_ptr = jvf->bci(); 931 } 932 *method_ptr = method->jmethod_id(); 933 934 return JVMTI_ERROR_NONE; 935 } 936 937 938 jvmtiError 939 JvmtiEnvBase::get_object_monitor_usage(JavaThread* calling_thread, jobject object, jvmtiMonitorUsage* info_ptr) { 940 HandleMark hm; 941 Handle hobj; 942 943 Thread* current_thread = Thread::current(); 944 bool at_safepoint = SafepointSynchronize::is_at_safepoint(); 945 946 // Check arguments 947 { 948 oop mirror = JNIHandles::resolve_external_guard(object); 949 NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT); 950 NULL_CHECK(info_ptr, JVMTI_ERROR_NULL_POINTER); 951 952 hobj = Handle(current_thread, mirror); 953 } 954 955 JavaThread *owning_thread = NULL; 956 ObjectMonitor *mon = NULL; 957 jvmtiMonitorUsage ret = { 958 NULL, 0, 0, NULL, 0, NULL 959 }; 960 961 uint32_t debug_bits = 0; 962 // first derive the object's owner and entry_count (if any) 963 { 964 // Revoke any biases before querying the mark word 965 if (at_safepoint) { 966 BiasedLocking::revoke_at_safepoint(hobj); 967 } else { 968 BiasedLocking::revoke(hobj, calling_thread); 969 } 970 971 address owner = NULL; 972 { 973 markWord mark = hobj()->mark(); 974 975 if (!mark.has_monitor()) { 976 // this object has a lightweight monitor 977 978 if (mark.has_locker()) { 979 owner = (address)mark.locker(); // save the address of the Lock word 980 } 981 // implied else: no owner 982 } else { 983 // this object has a heavyweight monitor 984 mon = mark.monitor(); 985 986 // The owner field of a heavyweight monitor may be NULL for no 987 // owner, a JavaThread * or it may still be the address of the 988 // Lock word in a JavaThread's stack. A monitor can be inflated 989 // by a non-owning JavaThread, but only the owning JavaThread 990 // can change the owner field from the Lock word to the 991 // JavaThread * and it may not have done that yet. 992 owner = (address)mon->owner(); 993 } 994 } 995 996 if (owner != NULL) { 997 // Use current thread since function can be called from a 998 // JavaThread or the VMThread. 999 ThreadsListHandle tlh; 1000 // This monitor is owned so we have to find the owning JavaThread. 1001 owning_thread = Threads::owning_thread_from_monitor_owner(tlh.list(), owner); 1002 // Cannot assume (owning_thread != NULL) here because this function 1003 // may not have been called at a safepoint and the owning_thread 1004 // might not be suspended. 1005 if (owning_thread != NULL) { 1006 // The monitor's owner either has to be the current thread, at safepoint 1007 // or it has to be suspended. Any of these conditions will prevent both 1008 // contending and waiting threads from modifying the state of 1009 // the monitor. 1010 if (!at_safepoint && !owning_thread->is_thread_fully_suspended(true, &debug_bits)) { 1011 // Don't worry! This return of JVMTI_ERROR_THREAD_NOT_SUSPENDED 1012 // will not make it back to the JVM/TI agent. The error code will 1013 // get intercepted in JvmtiEnv::GetObjectMonitorUsage() which 1014 // will retry the call via a VM_GetObjectMonitorUsage VM op. 1015 return JVMTI_ERROR_THREAD_NOT_SUSPENDED; 1016 } 1017 HandleMark hm; 1018 Handle th(current_thread, owning_thread->threadObj()); 1019 ret.owner = (jthread)jni_reference(calling_thread, th); 1020 } 1021 // implied else: no owner 1022 } // ThreadsListHandle is destroyed here. 1023 1024 if (owning_thread != NULL) { // monitor is owned 1025 // The recursions field of a monitor does not reflect recursions 1026 // as lightweight locks before inflating the monitor are not included. 1027 // We have to count the number of recursive monitor entries the hard way. 1028 // We pass a handle to survive any GCs along the way. 1029 ResourceMark rm; 1030 ret.entry_count = count_locked_objects(owning_thread, hobj); 1031 } 1032 // implied else: entry_count == 0 1033 } 1034 1035 jint nWant = 0, nWait = 0; 1036 if (mon != NULL) { 1037 // this object has a heavyweight monitor 1038 nWant = mon->contentions(); // # of threads contending for monitor 1039 nWait = mon->waiters(); // # of threads in Object.wait() 1040 ret.waiter_count = nWant + nWait; 1041 ret.notify_waiter_count = nWait; 1042 } else { 1043 // this object has a lightweight monitor 1044 ret.waiter_count = 0; 1045 ret.notify_waiter_count = 0; 1046 } 1047 1048 // Allocate memory for heavyweight and lightweight monitor. 1049 jvmtiError err; 1053 } 1054 err = allocate(ret.notify_waiter_count * sizeof(jthread *), 1055 (unsigned char**)&ret.notify_waiters); 1056 if (err != JVMTI_ERROR_NONE) { 1057 deallocate((unsigned char*)ret.waiters); 1058 return err; 1059 } 1060 1061 // now derive the rest of the fields 1062 if (mon != NULL) { 1063 // this object has a heavyweight monitor 1064 1065 // Number of waiters may actually be less than the waiter count. 1066 // So NULL out memory so that unused memory will be NULL. 1067 memset(ret.waiters, 0, ret.waiter_count * sizeof(jthread *)); 1068 memset(ret.notify_waiters, 0, ret.notify_waiter_count * sizeof(jthread *)); 1069 1070 if (ret.waiter_count > 0) { 1071 // we have contending and/or waiting threads 1072 HandleMark hm; 1073 // Use current thread since function can be called from a 1074 // JavaThread or the VMThread. 1075 ThreadsListHandle tlh; 1076 if (nWant > 0) { 1077 // we have contending threads 1078 ResourceMark rm; 1079 // get_pending_threads returns only java thread so we do not need to 1080 // check for non java threads. 1081 GrowableArray<JavaThread*>* wantList = Threads::get_pending_threads(tlh.list(), nWant, (address)mon); 1082 if (wantList->length() < nWant) { 1083 // robustness: the pending list has gotten smaller 1084 nWant = wantList->length(); 1085 } 1086 for (int i = 0; i < nWant; i++) { 1087 JavaThread *pending_thread = wantList->at(i); 1088 // If the monitor has no owner, then a non-suspended contending 1089 // thread could potentially change the state of the monitor by 1090 // entering it. The JVM/TI spec doesn't allow this. 1091 if (owning_thread == NULL && !at_safepoint && 1092 !pending_thread->is_thread_fully_suspended(true, &debug_bits)) { 1093 if (ret.owner != NULL) { 1094 destroy_jni_reference(calling_thread, ret.owner); 1095 } 1096 for (int j = 0; j < i; j++) { 1097 destroy_jni_reference(calling_thread, ret.waiters[j]); 1098 } 1099 deallocate((unsigned char*)ret.waiters); 1100 deallocate((unsigned char*)ret.notify_waiters); 1101 return JVMTI_ERROR_THREAD_NOT_SUSPENDED; 1102 } 1103 Handle th(current_thread, pending_thread->threadObj()); 1104 ret.waiters[i] = (jthread)jni_reference(calling_thread, th); 1105 } 1106 } 1107 if (nWait > 0) { 1108 // we have threads in Object.wait() 1109 int offset = nWant; // add after any contending threads 1110 ObjectWaiter *waiter = mon->first_waiter(); 1111 for (int i = 0, j = 0; i < nWait; i++) { 1112 if (waiter == NULL) { 1113 // robustness: the waiting list has gotten smaller 1114 nWait = j; 1115 break; 1116 } 1117 Thread *t = mon->thread_of_waiter(waiter); 1118 if (t != NULL && t->is_Java_thread()) { 1119 JavaThread *wjava_thread = (JavaThread *)t; 1120 // If the thread was found on the ObjectWaiter list, then 1121 // it has not been notified. This thread can't change the 1122 // state of the monitor so it doesn't need to be suspended. | 920 return JVMTI_ERROR_INTERNAL; 921 } 922 #endif 923 924 HandleMark hm(current_thread); 925 javaVFrame *jvf = javaVFrame::cast(vf); 926 Method* method = jvf->method(); 927 if (method->is_native()) { 928 *location_ptr = -1; 929 } else { 930 *location_ptr = jvf->bci(); 931 } 932 *method_ptr = method->jmethod_id(); 933 934 return JVMTI_ERROR_NONE; 935 } 936 937 938 jvmtiError 939 JvmtiEnvBase::get_object_monitor_usage(JavaThread* calling_thread, jobject object, jvmtiMonitorUsage* info_ptr) { 940 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 941 942 HandleMark hm; 943 Handle hobj; 944 945 Thread* current_thread = VMThread::vm_thread(); 946 assert(current_thread == Thread::current(), "must be"); 947 948 // Check arguments 949 { 950 oop mirror = JNIHandles::resolve_external_guard(object); 951 NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT); 952 NULL_CHECK(info_ptr, JVMTI_ERROR_NULL_POINTER); 953 954 hobj = Handle(current_thread, mirror); 955 } 956 957 JavaThread *owning_thread = NULL; 958 ObjectMonitor *mon = NULL; 959 jvmtiMonitorUsage ret = { 960 NULL, 0, 0, NULL, 0, NULL 961 }; 962 963 uint32_t debug_bits = 0; 964 // first derive the object's owner and entry_count (if any) 965 { 966 // Revoke any biases before querying the mark word 967 BiasedLocking::revoke_at_safepoint(hobj); 968 969 address owner = NULL; 970 { 971 markWord mark = hobj()->mark(); 972 973 if (!mark.has_monitor()) { 974 // this object has a lightweight monitor 975 976 if (mark.has_locker()) { 977 owner = (address)mark.locker(); // save the address of the Lock word 978 } 979 // implied else: no owner 980 } else { 981 // this object has a heavyweight monitor 982 mon = mark.monitor(); 983 984 // The owner field of a heavyweight monitor may be NULL for no 985 // owner, a JavaThread * or it may still be the address of the 986 // Lock word in a JavaThread's stack. A monitor can be inflated 987 // by a non-owning JavaThread, but only the owning JavaThread 988 // can change the owner field from the Lock word to the 989 // JavaThread * and it may not have done that yet. 990 owner = (address)mon->owner(); 991 } 992 } 993 994 if (owner != NULL) { 995 ThreadsListHandle tlh(current_thread); 996 // This monitor is owned so we have to find the owning JavaThread. 997 owning_thread = Threads::owning_thread_from_monitor_owner(tlh.list(), owner); 998 assert(owning_thread != NULL, "owning JavaThread must not be NULL"); 999 Handle th(current_thread, owning_thread->threadObj()); 1000 ret.owner = (jthread)jni_reference(calling_thread, th); 1001 } // ThreadsListHandle is destroyed here. 1002 1003 if (owning_thread != NULL) { // monitor is owned 1004 // The recursions field of a monitor does not reflect recursions 1005 // as lightweight locks before inflating the monitor are not included. 1006 // We have to count the number of recursive monitor entries the hard way. 1007 // We pass a handle to survive any GCs along the way. 1008 ResourceMark rm(current_thread); 1009 ret.entry_count = count_locked_objects(owning_thread, hobj); 1010 } 1011 // implied else: entry_count == 0 1012 } 1013 1014 jint nWant = 0, nWait = 0; 1015 if (mon != NULL) { 1016 // this object has a heavyweight monitor 1017 nWant = mon->contentions(); // # of threads contending for monitor 1018 nWait = mon->waiters(); // # of threads in Object.wait() 1019 ret.waiter_count = nWant + nWait; 1020 ret.notify_waiter_count = nWait; 1021 } else { 1022 // this object has a lightweight monitor 1023 ret.waiter_count = 0; 1024 ret.notify_waiter_count = 0; 1025 } 1026 1027 // Allocate memory for heavyweight and lightweight monitor. 1028 jvmtiError err; 1032 } 1033 err = allocate(ret.notify_waiter_count * sizeof(jthread *), 1034 (unsigned char**)&ret.notify_waiters); 1035 if (err != JVMTI_ERROR_NONE) { 1036 deallocate((unsigned char*)ret.waiters); 1037 return err; 1038 } 1039 1040 // now derive the rest of the fields 1041 if (mon != NULL) { 1042 // this object has a heavyweight monitor 1043 1044 // Number of waiters may actually be less than the waiter count. 1045 // So NULL out memory so that unused memory will be NULL. 1046 memset(ret.waiters, 0, ret.waiter_count * sizeof(jthread *)); 1047 memset(ret.notify_waiters, 0, ret.notify_waiter_count * sizeof(jthread *)); 1048 1049 if (ret.waiter_count > 0) { 1050 // we have contending and/or waiting threads 1051 HandleMark hm; 1052 ThreadsListHandle tlh(current_thread); 1053 if (nWant > 0) { 1054 // we have contending threads 1055 ResourceMark rm(current_thread); 1056 // get_pending_threads returns only java thread so we do not need to 1057 // check for non java threads. 1058 GrowableArray<JavaThread*>* wantList = Threads::get_pending_threads(tlh.list(), nWant, (address)mon); 1059 if (wantList->length() < nWant) { 1060 // robustness: the pending list has gotten smaller 1061 nWant = wantList->length(); 1062 } 1063 for (int i = 0; i < nWant; i++) { 1064 JavaThread *pending_thread = wantList->at(i); 1065 Handle th(current_thread, pending_thread->threadObj()); 1066 ret.waiters[i] = (jthread)jni_reference(calling_thread, th); 1067 } 1068 } 1069 if (nWait > 0) { 1070 // we have threads in Object.wait() 1071 int offset = nWant; // add after any contending threads 1072 ObjectWaiter *waiter = mon->first_waiter(); 1073 for (int i = 0, j = 0; i < nWait; i++) { 1074 if (waiter == NULL) { 1075 // robustness: the waiting list has gotten smaller 1076 nWait = j; 1077 break; 1078 } 1079 Thread *t = mon->thread_of_waiter(waiter); 1080 if (t != NULL && t->is_Java_thread()) { 1081 JavaThread *wjava_thread = (JavaThread *)t; 1082 // If the thread was found on the ObjectWaiter list, then 1083 // it has not been notified. This thread can't change the 1084 // state of the monitor so it doesn't need to be suspended. |