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;
1052 return 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 Thread* current_thread = VMThread::vm_thread();
942 assert(current_thread == Thread::current(), "must be");
943
944 HandleMark hm(current_thread);
945 Handle hobj;
946
947 // Check arguments
948 {
949 oop mirror = JNIHandles::resolve_external_guard(object);
950 NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
951 NULL_CHECK(info_ptr, JVMTI_ERROR_NULL_POINTER);
952
953 hobj = Handle(current_thread, mirror);
954 }
955
956 ThreadsListHandle tlh(current_thread);
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 // This monitor is owned so we have to find the owning JavaThread.
996 owning_thread = Threads::owning_thread_from_monitor_owner(tlh.list(), owner);
997 assert(owning_thread != NULL, "owning JavaThread must not be NULL");
998 Handle th(current_thread, owning_thread->threadObj());
999 ret.owner = (jthread)jni_reference(calling_thread, th);
1000 }
1001
1002 if (owning_thread != NULL) { // monitor is owned
1003 // The recursions field of a monitor does not reflect recursions
1004 // as lightweight locks before inflating the monitor are not included.
1005 // We have to count the number of recursive monitor entries the hard way.
1006 // We pass a handle to survive any GCs along the way.
1007 ResourceMark rm(current_thread);
1008 ret.entry_count = count_locked_objects(owning_thread, hobj);
1009 }
1010 // implied else: entry_count == 0
1011 }
1012
1013 jint nWant = 0, nWait = 0;
1014 if (mon != NULL) {
1015 // this object has a heavyweight monitor
1016 nWant = mon->contentions(); // # of threads contending for monitor
1017 nWait = mon->waiters(); // # of threads in Object.wait()
1018 ret.waiter_count = nWant + nWait;
1019 ret.notify_waiter_count = nWait;
1020 } else {
1021 // this object has a lightweight monitor
1022 ret.waiter_count = 0;
1023 ret.notify_waiter_count = 0;
1024 }
1025
1026 // Allocate memory for heavyweight and lightweight monitor.
1027 jvmtiError err;
1030 return err;
1031 }
1032 err = allocate(ret.notify_waiter_count * sizeof(jthread *),
1033 (unsigned char**)&ret.notify_waiters);
1034 if (err != JVMTI_ERROR_NONE) {
1035 deallocate((unsigned char*)ret.waiters);
1036 return err;
1037 }
1038
1039 // now derive the rest of the fields
1040 if (mon != NULL) {
1041 // this object has a heavyweight monitor
1042
1043 // Number of waiters may actually be less than the waiter count.
1044 // So NULL out memory so that unused memory will be NULL.
1045 memset(ret.waiters, 0, ret.waiter_count * sizeof(jthread *));
1046 memset(ret.notify_waiters, 0, ret.notify_waiter_count * sizeof(jthread *));
1047
1048 if (ret.waiter_count > 0) {
1049 // we have contending and/or waiting threads
1050 if (nWant > 0) {
1051 // we have contending threads
1052 ResourceMark rm(current_thread);
1053 // get_pending_threads returns only java thread so we do not need to
1054 // check for non java threads.
1055 GrowableArray<JavaThread*>* wantList = Threads::get_pending_threads(tlh.list(), nWant, (address)mon);
1056 if (wantList->length() < nWant) {
1057 // robustness: the pending list has gotten smaller
1058 nWant = wantList->length();
1059 }
1060 for (int i = 0; i < nWant; i++) {
1061 JavaThread *pending_thread = wantList->at(i);
1062 Handle th(current_thread, pending_thread->threadObj());
1063 ret.waiters[i] = (jthread)jni_reference(calling_thread, th);
1064 }
1065 }
1066 if (nWait > 0) {
1067 // we have threads in Object.wait()
1068 int offset = nWant; // add after any contending threads
1069 ObjectWaiter *waiter = mon->first_waiter();
1070 for (int i = 0, j = 0; i < nWait; i++) {
1071 if (waiter == NULL) {
1072 // robustness: the waiting list has gotten smaller
1073 nWait = j;
1074 break;
1075 }
1076 Thread *t = mon->thread_of_waiter(waiter);
1077 if (t != NULL && t->is_Java_thread()) {
1078 JavaThread *wjava_thread = (JavaThread *)t;
1079 // If the thread was found on the ObjectWaiter list, then
1080 // it has not been notified. This thread can't change the
1081 // state of the monitor so it doesn't need to be suspended.
|