799 err = allocate(sizeof(jvmtiMonitorStackDepthInfo), (unsigned char **)&jmsdi);
800 if (err != JVMTI_ERROR_NONE) {
801 return err;
802 }
803 Handle hobj(Thread::current(), obj);
804 jmsdi->monitor = jni_reference(calling_thread, hobj);
805 jmsdi->stack_depth = stack_depth;
806 owned_monitors_list->append(jmsdi);
807 }
808
809 return err;
810 }
811
812 jvmtiError
813 JvmtiEnvBase::get_stack_trace(JavaThread *java_thread,
814 jint start_depth, jint max_count,
815 jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
816 #ifdef ASSERT
817 uint32_t debug_bits = 0;
818 #endif
819 assert((SafepointSynchronize::is_at_safepoint() ||
820 java_thread->is_thread_fully_suspended(false, &debug_bits)),
821 "at safepoint or target thread is suspended");
822 int count = 0;
823 if (java_thread->has_last_Java_frame()) {
824 RegisterMap reg_map(java_thread);
825 Thread* current_thread = Thread::current();
826 ResourceMark rm(current_thread);
827 javaVFrame *jvf = java_thread->last_java_vframe(®_map);
828 HandleMark hm(current_thread);
829 if (start_depth != 0) {
830 if (start_depth > 0) {
831 for (int j = 0; j < start_depth && jvf != NULL; j++) {
832 jvf = jvf->java_sender();
833 }
834 if (jvf == NULL) {
835 // start_depth is deeper than the stack depth
836 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
837 }
838 } else { // start_depth < 0
839 // we are referencing the starting depth based on the oldest
840 // part of the stack.
841 // optimize to limit the number of times that java_sender() is called
842 javaVFrame *jvf_cursor = jvf;
843 javaVFrame *jvf_prev = NULL;
844 javaVFrame *jvf_prev_prev = NULL;
845 int j = 0;
1137
1138 char* ResourceTracker::strdup(const char* str) {
1139 char *dup_str = (char*)allocate(strlen(str)+1);
1140 if (dup_str != NULL) {
1141 strcpy(dup_str, str);
1142 }
1143 return dup_str;
1144 }
1145
1146 struct StackInfoNode {
1147 struct StackInfoNode *next;
1148 jvmtiStackInfo info;
1149 };
1150
1151 // Create a jvmtiStackInfo inside a linked list node and create a
1152 // buffer for the frame information, both allocated as resource objects.
1153 // Fill in both the jvmtiStackInfo and the jvmtiFrameInfo.
1154 // Note that either or both of thr and thread_oop
1155 // may be null if the thread is new or has exited.
1156 void
1157 VM_GetMultipleStackTraces::fill_frames(jthread jt, JavaThread *thr, oop thread_oop) {
1158 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1159
1160 jint state = 0;
1161 struct StackInfoNode *node = NEW_RESOURCE_OBJ(struct StackInfoNode);
1162 jvmtiStackInfo *infop = &(node->info);
1163 node->next = head();
1164 set_head(node);
1165 infop->frame_count = 0;
1166 infop->thread = jt;
1167
1168 if (thread_oop != NULL) {
1169 // get most state bits
1170 state = (jint)java_lang_Thread::get_thread_status(thread_oop);
1171 }
1172
1173 if (thr != NULL) { // add more state bits if there is a JavaThead to query
1174 // same as is_being_ext_suspended() but without locking
1175 if (thr->is_ext_suspended() || thr->is_external_suspend()) {
1176 state |= JVMTI_THREAD_STATE_SUSPENDED;
1177 }
1178 JavaThreadState jts = thr->thread_state();
1182 if (thr->is_interrupted(false)) {
1183 state |= JVMTI_THREAD_STATE_INTERRUPTED;
1184 }
1185 }
1186 infop->state = state;
1187
1188 if (thr != NULL && (state & JVMTI_THREAD_STATE_ALIVE) != 0) {
1189 infop->frame_buffer = NEW_RESOURCE_ARRAY(jvmtiFrameInfo, max_frame_count());
1190 env()->get_stack_trace(thr, 0, max_frame_count(),
1191 infop->frame_buffer, &(infop->frame_count));
1192 } else {
1193 infop->frame_buffer = NULL;
1194 infop->frame_count = 0;
1195 }
1196 _frame_count_total += infop->frame_count;
1197 }
1198
1199 // Based on the stack information in the linked list, allocate memory
1200 // block to return and fill it from the info in the linked list.
1201 void
1202 VM_GetMultipleStackTraces::allocate_and_fill_stacks(jint thread_count) {
1203 // do I need to worry about alignment issues?
1204 jlong alloc_size = thread_count * sizeof(jvmtiStackInfo)
1205 + _frame_count_total * sizeof(jvmtiFrameInfo);
1206 env()->allocate(alloc_size, (unsigned char **)&_stack_info);
1207
1208 // pointers to move through the newly allocated space as it is filled in
1209 jvmtiStackInfo *si = _stack_info + thread_count; // bottom of stack info
1210 jvmtiFrameInfo *fi = (jvmtiFrameInfo *)si; // is the top of frame info
1211
1212 // copy information in resource area into allocated buffer
1213 // insert stack info backwards since linked list is backwards
1214 // insert frame info forwards
1215 // walk the StackInfoNodes
1216 for (struct StackInfoNode *sin = head(); sin != NULL; sin = sin->next) {
1217 jint frame_count = sin->info.frame_count;
1218 size_t frames_size = frame_count * sizeof(jvmtiFrameInfo);
1219 --si;
1220 memcpy(si, &(sin->info), sizeof(jvmtiStackInfo));
1221 if (frames_size == 0) {
1222 si->frame_buffer = NULL;
1231 "the last copied frame info must be the last record");
1232 }
1233
1234
1235 void
1236 VM_GetThreadListStackTraces::doit() {
1237 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1238
1239 ResourceMark rm;
1240 ThreadsListHandle tlh;
1241 for (int i = 0; i < _thread_count; ++i) {
1242 jthread jt = _thread_list[i];
1243 JavaThread* java_thread = NULL;
1244 oop thread_oop = NULL;
1245 jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), jt, &java_thread, &thread_oop);
1246 if (err != JVMTI_ERROR_NONE) {
1247 // We got an error code so we don't have a JavaThread *, but
1248 // only return an error from here if we didn't get a valid
1249 // thread_oop.
1250 if (thread_oop == NULL) {
1251 set_result(err);
1252 return;
1253 }
1254 // We have a valid thread_oop.
1255 }
1256 fill_frames(jt, java_thread, thread_oop);
1257 }
1258 allocate_and_fill_stacks(_thread_count);
1259 }
1260
1261 void
1262 VM_GetAllStackTraces::doit() {
1263 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1264
1265 ResourceMark rm;
1266 _final_thread_count = 0;
1267 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
1268 oop thread_oop = jt->threadObj();
1269 if (thread_oop != NULL &&
1270 !jt->is_exiting() &&
1271 java_lang_Thread::is_alive(thread_oop) &&
1272 !jt->is_hidden_from_external_view()) {
1273 ++_final_thread_count;
1274 // Handle block of the calling thread is used to create local refs.
1275 fill_frames((jthread)JNIHandles::make_local(_calling_thread, thread_oop),
1276 jt, thread_oop);
1277 }
1278 }
1279 allocate_and_fill_stacks(_final_thread_count);
1280 }
1281
1282 // Verifies that the top frame is a java frame in an expected state.
1283 // Deoptimizes frame if needed.
1284 // Checks that the frame method signature matches the return type (tos).
1285 // HandleMark must be defined in the caller only.
1286 // It is to keep a ret_ob_h handle alive after return to the caller.
1287 jvmtiError
1288 JvmtiEnvBase::check_top_frame(JavaThread* current_thread, JavaThread* java_thread,
1289 jvalue value, TosState tos, Handle* ret_ob_h) {
1290 ResourceMark rm(current_thread);
1291
1292 vframe *vf = vframeFor(java_thread, 0);
1293 NULL_CHECK(vf, JVMTI_ERROR_NO_MORE_FRAMES);
1294
1295 javaVFrame *jvf = (javaVFrame*) vf;
1296 if (!vf->is_java_frame() || jvf->method()->is_native()) {
1297 return JVMTI_ERROR_OPAQUE_FRAME;
1298 }
1299
1511 JavaThread *jt = (JavaThread *)target;
1512 if (!jt->is_exiting() && (jt->threadObj() != NULL)) {
1513 _result = ((JvmtiEnvBase *)_env)->get_owned_monitors(_calling_thread,
1514 jt,
1515 _owned_monitors_list);
1516 }
1517 }
1518
1519 void
1520 GetCurrentContendedMonitorClosure::do_thread(Thread *target) {
1521 assert(target->is_Java_thread(), "just checking");
1522 JavaThread *jt = (JavaThread *)target;
1523 if (!jt->is_exiting() && (jt->threadObj() != NULL)) {
1524 _result = ((JvmtiEnvBase *)_env)->get_current_contended_monitor(_calling_thread,
1525 jt,
1526 _owned_monitor_ptr);
1527 }
1528 }
1529
1530 void
1531 VM_GetStackTrace::doit() {
1532 _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
1533 ThreadsListHandle tlh;
1534 if (_java_thread != NULL && tlh.includes(_java_thread)
1535 && !_java_thread->is_exiting() && _java_thread->threadObj() != NULL) {
1536 _result = ((JvmtiEnvBase *)_env)->get_stack_trace(_java_thread,
1537 _start_depth, _max_count,
1538 _frame_buffer, _count_ptr);
1539 }
1540 }
1541
1542 void
1543 VM_GetFrameCount::doit() {
1544 _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
1545 JavaThread* jt = _state->get_thread();
1546 ThreadsListHandle tlh;
1547 if (jt != NULL && tlh.includes(jt) && !jt->is_exiting() && jt->threadObj() != NULL) {
1548 _result = ((JvmtiEnvBase*)_env)->get_frame_count(_state, _count_ptr);
1549 }
1550 }
1551
1552 void
1553 VM_GetFrameLocation::doit() {
1554 _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
1555 ThreadsListHandle tlh;
1556 if (_java_thread != NULL && tlh.includes(_java_thread)
|
799 err = allocate(sizeof(jvmtiMonitorStackDepthInfo), (unsigned char **)&jmsdi);
800 if (err != JVMTI_ERROR_NONE) {
801 return err;
802 }
803 Handle hobj(Thread::current(), obj);
804 jmsdi->monitor = jni_reference(calling_thread, hobj);
805 jmsdi->stack_depth = stack_depth;
806 owned_monitors_list->append(jmsdi);
807 }
808
809 return err;
810 }
811
812 jvmtiError
813 JvmtiEnvBase::get_stack_trace(JavaThread *java_thread,
814 jint start_depth, jint max_count,
815 jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
816 #ifdef ASSERT
817 uint32_t debug_bits = 0;
818 #endif
819 Thread *current_thread = Thread::current();
820 assert(current_thread == java_thread ||
821 SafepointSynchronize::is_at_safepoint() ||
822 current_thread == java_thread->active_handshaker(),
823 "call by myself / at safepoint / at handshake");
824 int count = 0;
825 if (java_thread->has_last_Java_frame()) {
826 RegisterMap reg_map(java_thread);
827 ResourceMark rm(current_thread);
828 javaVFrame *jvf = java_thread->last_java_vframe(®_map);
829 HandleMark hm(current_thread);
830 if (start_depth != 0) {
831 if (start_depth > 0) {
832 for (int j = 0; j < start_depth && jvf != NULL; j++) {
833 jvf = jvf->java_sender();
834 }
835 if (jvf == NULL) {
836 // start_depth is deeper than the stack depth
837 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
838 }
839 } else { // start_depth < 0
840 // we are referencing the starting depth based on the oldest
841 // part of the stack.
842 // optimize to limit the number of times that java_sender() is called
843 javaVFrame *jvf_cursor = jvf;
844 javaVFrame *jvf_prev = NULL;
845 javaVFrame *jvf_prev_prev = NULL;
846 int j = 0;
1138
1139 char* ResourceTracker::strdup(const char* str) {
1140 char *dup_str = (char*)allocate(strlen(str)+1);
1141 if (dup_str != NULL) {
1142 strcpy(dup_str, str);
1143 }
1144 return dup_str;
1145 }
1146
1147 struct StackInfoNode {
1148 struct StackInfoNode *next;
1149 jvmtiStackInfo info;
1150 };
1151
1152 // Create a jvmtiStackInfo inside a linked list node and create a
1153 // buffer for the frame information, both allocated as resource objects.
1154 // Fill in both the jvmtiStackInfo and the jvmtiFrameInfo.
1155 // Note that either or both of thr and thread_oop
1156 // may be null if the thread is new or has exited.
1157 void
1158 MultipleStackTracesCollector::fill_frames(jthread jt, JavaThread *thr, oop thread_oop) {
1159 Thread *current_thread = Thread::current();
1160 assert(current_thread == thr ||
1161 SafepointSynchronize::is_at_safepoint() ||
1162 current_thread == thr->active_handshaker(),
1163 "call by myself / at safepoint / at handshake");
1164
1165 jint state = 0;
1166 struct StackInfoNode *node = NEW_RESOURCE_OBJ(struct StackInfoNode);
1167 jvmtiStackInfo *infop = &(node->info);
1168 node->next = head();
1169 set_head(node);
1170 infop->frame_count = 0;
1171 infop->thread = jt;
1172
1173 if (thread_oop != NULL) {
1174 // get most state bits
1175 state = (jint)java_lang_Thread::get_thread_status(thread_oop);
1176 }
1177
1178 if (thr != NULL) { // add more state bits if there is a JavaThead to query
1179 // same as is_being_ext_suspended() but without locking
1180 if (thr->is_ext_suspended() || thr->is_external_suspend()) {
1181 state |= JVMTI_THREAD_STATE_SUSPENDED;
1182 }
1183 JavaThreadState jts = thr->thread_state();
1187 if (thr->is_interrupted(false)) {
1188 state |= JVMTI_THREAD_STATE_INTERRUPTED;
1189 }
1190 }
1191 infop->state = state;
1192
1193 if (thr != NULL && (state & JVMTI_THREAD_STATE_ALIVE) != 0) {
1194 infop->frame_buffer = NEW_RESOURCE_ARRAY(jvmtiFrameInfo, max_frame_count());
1195 env()->get_stack_trace(thr, 0, max_frame_count(),
1196 infop->frame_buffer, &(infop->frame_count));
1197 } else {
1198 infop->frame_buffer = NULL;
1199 infop->frame_count = 0;
1200 }
1201 _frame_count_total += infop->frame_count;
1202 }
1203
1204 // Based on the stack information in the linked list, allocate memory
1205 // block to return and fill it from the info in the linked list.
1206 void
1207 MultipleStackTracesCollector::allocate_and_fill_stacks(jint thread_count) {
1208 // do I need to worry about alignment issues?
1209 jlong alloc_size = thread_count * sizeof(jvmtiStackInfo)
1210 + _frame_count_total * sizeof(jvmtiFrameInfo);
1211 env()->allocate(alloc_size, (unsigned char **)&_stack_info);
1212
1213 // pointers to move through the newly allocated space as it is filled in
1214 jvmtiStackInfo *si = _stack_info + thread_count; // bottom of stack info
1215 jvmtiFrameInfo *fi = (jvmtiFrameInfo *)si; // is the top of frame info
1216
1217 // copy information in resource area into allocated buffer
1218 // insert stack info backwards since linked list is backwards
1219 // insert frame info forwards
1220 // walk the StackInfoNodes
1221 for (struct StackInfoNode *sin = head(); sin != NULL; sin = sin->next) {
1222 jint frame_count = sin->info.frame_count;
1223 size_t frames_size = frame_count * sizeof(jvmtiFrameInfo);
1224 --si;
1225 memcpy(si, &(sin->info), sizeof(jvmtiStackInfo));
1226 if (frames_size == 0) {
1227 si->frame_buffer = NULL;
1236 "the last copied frame info must be the last record");
1237 }
1238
1239
1240 void
1241 VM_GetThreadListStackTraces::doit() {
1242 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1243
1244 ResourceMark rm;
1245 ThreadsListHandle tlh;
1246 for (int i = 0; i < _thread_count; ++i) {
1247 jthread jt = _thread_list[i];
1248 JavaThread* java_thread = NULL;
1249 oop thread_oop = NULL;
1250 jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), jt, &java_thread, &thread_oop);
1251 if (err != JVMTI_ERROR_NONE) {
1252 // We got an error code so we don't have a JavaThread *, but
1253 // only return an error from here if we didn't get a valid
1254 // thread_oop.
1255 if (thread_oop == NULL) {
1256 _collector.set_result(err);
1257 return;
1258 }
1259 // We have a valid thread_oop.
1260 }
1261 _collector.fill_frames(jt, java_thread, thread_oop);
1262 }
1263 _collector.allocate_and_fill_stacks(_thread_count);
1264 }
1265
1266 void
1267 GetSingleStackTraceClosure::do_thread(Thread *target) {
1268 assert(target->is_Java_thread(), "just checking");
1269 JavaThread *jt = (JavaThread *)target;
1270 oop thread_oop = jt->threadObj();
1271
1272 if (!jt->is_exiting() && (thread_oop != NULL)) {
1273 ResourceMark rm;
1274 _collector.fill_frames((jthread)JNIHandles::make_local(_calling_thread, thread_oop),
1275 jt, thread_oop);
1276 _collector.allocate_and_fill_stacks(1);
1277 }
1278 }
1279
1280 void
1281 VM_GetAllStackTraces::doit() {
1282 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1283
1284 ResourceMark rm;
1285 _final_thread_count = 0;
1286 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
1287 oop thread_oop = jt->threadObj();
1288 if (thread_oop != NULL &&
1289 !jt->is_exiting() &&
1290 java_lang_Thread::is_alive(thread_oop) &&
1291 !jt->is_hidden_from_external_view()) {
1292 ++_final_thread_count;
1293 // Handle block of the calling thread is used to create local refs.
1294 _collector.fill_frames((jthread)JNIHandles::make_local(_calling_thread, thread_oop),
1295 jt, thread_oop);
1296 }
1297 }
1298 _collector.allocate_and_fill_stacks(_final_thread_count);
1299 }
1300
1301 // Verifies that the top frame is a java frame in an expected state.
1302 // Deoptimizes frame if needed.
1303 // Checks that the frame method signature matches the return type (tos).
1304 // HandleMark must be defined in the caller only.
1305 // It is to keep a ret_ob_h handle alive after return to the caller.
1306 jvmtiError
1307 JvmtiEnvBase::check_top_frame(JavaThread* current_thread, JavaThread* java_thread,
1308 jvalue value, TosState tos, Handle* ret_ob_h) {
1309 ResourceMark rm(current_thread);
1310
1311 vframe *vf = vframeFor(java_thread, 0);
1312 NULL_CHECK(vf, JVMTI_ERROR_NO_MORE_FRAMES);
1313
1314 javaVFrame *jvf = (javaVFrame*) vf;
1315 if (!vf->is_java_frame() || jvf->method()->is_native()) {
1316 return JVMTI_ERROR_OPAQUE_FRAME;
1317 }
1318
1530 JavaThread *jt = (JavaThread *)target;
1531 if (!jt->is_exiting() && (jt->threadObj() != NULL)) {
1532 _result = ((JvmtiEnvBase *)_env)->get_owned_monitors(_calling_thread,
1533 jt,
1534 _owned_monitors_list);
1535 }
1536 }
1537
1538 void
1539 GetCurrentContendedMonitorClosure::do_thread(Thread *target) {
1540 assert(target->is_Java_thread(), "just checking");
1541 JavaThread *jt = (JavaThread *)target;
1542 if (!jt->is_exiting() && (jt->threadObj() != NULL)) {
1543 _result = ((JvmtiEnvBase *)_env)->get_current_contended_monitor(_calling_thread,
1544 jt,
1545 _owned_monitor_ptr);
1546 }
1547 }
1548
1549 void
1550 GetStackTraceClosure::do_thread(Thread *target) {
1551 assert(target->is_Java_thread(), "just checking");
1552 JavaThread *jt = (JavaThread *)target;
1553 if (!jt->is_exiting() && (jt->threadObj() != NULL)) {
1554 _result = ((JvmtiEnvBase *)_env)->get_stack_trace(jt,
1555 _start_depth, _max_count,
1556 _frame_buffer, _count_ptr);
1557 }
1558 }
1559
1560 void
1561 VM_GetFrameCount::doit() {
1562 _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
1563 JavaThread* jt = _state->get_thread();
1564 ThreadsListHandle tlh;
1565 if (jt != NULL && tlh.includes(jt) && !jt->is_exiting() && jt->threadObj() != NULL) {
1566 _result = ((JvmtiEnvBase*)_env)->get_frame_count(_state, _count_ptr);
1567 }
1568 }
1569
1570 void
1571 VM_GetFrameLocation::doit() {
1572 _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
1573 ThreadsListHandle tlh;
1574 if (_java_thread != NULL && tlh.includes(_java_thread)
|