< prev index next >

src/hotspot/share/prims/jvmtiEnvBase.cpp

Print this page




 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(&reg_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(SafepointSynchronize::is_at_safepoint() ||
 821          java_thread->is_thread_fully_suspended(false, &debug_bits) ||
 822          current_thread == java_thread->active_handshaker(),
 823          "at safepoint / handshake or target thread is suspended");
 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(&reg_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   assert(SafepointSynchronize::is_at_safepoint() ||
1160          Thread::current() == thr->active_handshaker(),
1161          "must be at safepoint or at direct handshake");
1162 
1163   jint state = 0;
1164   struct StackInfoNode *node = NEW_RESOURCE_OBJ(struct StackInfoNode);
1165   jvmtiStackInfo *infop = &(node->info);
1166   node->next = head();
1167   set_head(node);
1168   infop->frame_count = 0;
1169   infop->thread = jt;
1170 
1171   if (thread_oop != NULL) {
1172     // get most state bits
1173     state = (jint)java_lang_Thread::get_thread_status(thread_oop);
1174   }
1175 
1176   if (thr != NULL) {    // add more state bits if there is a JavaThead to query
1177     // same as is_being_ext_suspended() but without locking
1178     if (thr->is_ext_suspended() || thr->is_external_suspend()) {
1179       state |= JVMTI_THREAD_STATE_SUSPENDED;
1180     }
1181     JavaThreadState jts = thr->thread_state();


1185     if (thr->is_interrupted(false)) {
1186       state |= JVMTI_THREAD_STATE_INTERRUPTED;
1187     }
1188   }
1189   infop->state = state;
1190 
1191   if (thr != NULL && (state & JVMTI_THREAD_STATE_ALIVE) != 0) {
1192     infop->frame_buffer = NEW_RESOURCE_ARRAY(jvmtiFrameInfo, max_frame_count());
1193     env()->get_stack_trace(thr, 0, max_frame_count(),
1194                            infop->frame_buffer, &(infop->frame_count));
1195   } else {
1196     infop->frame_buffer = NULL;
1197     infop->frame_count = 0;
1198   }
1199   _frame_count_total += infop->frame_count;
1200 }
1201 
1202 // Based on the stack information in the linked list, allocate memory
1203 // block to return and fill it from the info in the linked list.
1204 void
1205 MultipleStackTracesCollector::allocate_and_fill_stacks(jint thread_count) {
1206   // do I need to worry about alignment issues?
1207   jlong alloc_size =  thread_count       * sizeof(jvmtiStackInfo)
1208                     + _frame_count_total * sizeof(jvmtiFrameInfo);
1209   env()->allocate(alloc_size, (unsigned char **)&_stack_info);
1210 
1211   // pointers to move through the newly allocated space as it is filled in
1212   jvmtiStackInfo *si = _stack_info + thread_count;      // bottom of stack info
1213   jvmtiFrameInfo *fi = (jvmtiFrameInfo *)si;            // is the top of frame info
1214 
1215   // copy information in resource area into allocated buffer
1216   // insert stack info backwards since linked list is backwards
1217   // insert frame info forwards
1218   // walk the StackInfoNodes
1219   for (struct StackInfoNode *sin = head(); sin != NULL; sin = sin->next) {
1220     jint frame_count = sin->info.frame_count;
1221     size_t frames_size = frame_count * sizeof(jvmtiFrameInfo);
1222     --si;
1223     memcpy(si, &(sin->info), sizeof(jvmtiStackInfo));
1224     if (frames_size == 0) {
1225       si->frame_buffer = NULL;


1234          "the last copied frame info must be the last record");
1235 }
1236 
1237 
1238 void
1239 VM_GetThreadListStackTraces::doit() {
1240   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1241 
1242   ResourceMark rm;
1243   ThreadsListHandle tlh;
1244   for (int i = 0; i < _thread_count; ++i) {
1245     jthread jt = _thread_list[i];
1246     JavaThread* java_thread = NULL;
1247     oop thread_oop = NULL;
1248     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), jt, &java_thread, &thread_oop);
1249     if (err != JVMTI_ERROR_NONE) {
1250       // We got an error code so we don't have a JavaThread *, but
1251       // only return an error from here if we didn't get a valid
1252       // thread_oop.
1253       if (thread_oop == NULL) {
1254         _collector.set_result(err);
1255         return;
1256       }
1257       // We have a valid thread_oop.
1258     }
1259     _collector.fill_frames(jt, java_thread, thread_oop);
1260   }
1261   _collector.allocate_and_fill_stacks(_thread_count);
1262 }
1263 
1264 void
1265 GetSingleStackTraceClosure::do_thread(Thread *target) {
1266   assert(target->is_Java_thread(), "just checking");
1267   JavaThread *jt = (JavaThread *)target;
1268   oop thread_oop = jt->threadObj();
1269 
1270   if (!jt->is_exiting() && (jt->threadObj() != NULL)) {
1271     ResourceMark rm;
1272     _collector.fill_frames((jthread)JNIHandles::make_local(_calling_thread, thread_oop),
1273                            jt, thread_oop);
1274     _collector.allocate_and_fill_stacks(1);
1275     _collector.set_result(JVMTI_ERROR_NONE);
1276   }

1277 }
1278 
1279 void
1280 VM_GetAllStackTraces::doit() {
1281   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1282 
1283   ResourceMark rm;
1284   _final_thread_count = 0;
1285   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
1286     oop thread_oop = jt->threadObj();
1287     if (thread_oop != NULL &&
1288         !jt->is_exiting() &&
1289         java_lang_Thread::is_alive(thread_oop) &&
1290         !jt->is_hidden_from_external_view()) {
1291       ++_final_thread_count;
1292       // Handle block of the calling thread is used to create local refs.
1293       _collector.fill_frames((jthread)JNIHandles::make_local(_calling_thread, thread_oop),
1294                              jt, thread_oop);
1295     }
1296   }
1297   _collector.allocate_and_fill_stacks(_final_thread_count);
1298 }
1299 
1300 // Verifies that the top frame is a java frame in an expected state.
1301 // Deoptimizes frame if needed.
1302 // Checks that the frame method signature matches the return type (tos).
1303 // HandleMark must be defined in the caller only.
1304 // It is to keep a ret_ob_h handle alive after return to the caller.
1305 jvmtiError
1306 JvmtiEnvBase::check_top_frame(JavaThread* current_thread, JavaThread* java_thread,
1307                               jvalue value, TosState tos, Handle* ret_ob_h) {
1308   ResourceMark rm(current_thread);
1309 
1310   vframe *vf = vframeFor(java_thread, 0);
1311   NULL_CHECK(vf, JVMTI_ERROR_NO_MORE_FRAMES);
1312 
1313   javaVFrame *jvf = (javaVFrame*) vf;
1314   if (!vf->is_java_frame() || jvf->method()->is_native()) {
1315     return JVMTI_ERROR_OPAQUE_FRAME;
1316   }
1317 


1529   JavaThread *jt = (JavaThread *)target;
1530   if (!jt->is_exiting() && (jt->threadObj() != NULL)) {
1531     _result = ((JvmtiEnvBase *)_env)->get_owned_monitors(_calling_thread,
1532                                                          jt,
1533                                                          _owned_monitors_list);
1534   }
1535 }
1536 
1537 void
1538 GetCurrentContendedMonitorClosure::do_thread(Thread *target) {
1539   assert(target->is_Java_thread(), "just checking");
1540   JavaThread *jt = (JavaThread *)target;
1541   if (!jt->is_exiting() && (jt->threadObj() != NULL)) {
1542     _result = ((JvmtiEnvBase *)_env)->get_current_contended_monitor(_calling_thread,
1543                                                                     jt,
1544                                                                     _owned_monitor_ptr);
1545   }
1546 }
1547 
1548 void
1549 GetStackTraceClosure::do_thread(Thread *target) {
1550   assert(target->is_Java_thread(), "just checking");
1551   JavaThread *jt = (JavaThread *)target;
1552   if (!jt->is_exiting() && (jt->threadObj() != NULL)) {
1553     _result = ((JvmtiEnvBase *)_env)->get_stack_trace(jt,

1554                                                       _start_depth, _max_count,
1555                                                       _frame_buffer, _count_ptr);
1556   }
1557 }
1558 
1559 void
1560 VM_GetFrameCount::doit() {
1561   _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
1562   JavaThread* jt = _state->get_thread();
1563   ThreadsListHandle tlh;
1564   if (jt != NULL && tlh.includes(jt) && !jt->is_exiting() && jt->threadObj() != NULL) {
1565     _result = ((JvmtiEnvBase*)_env)->get_frame_count(_state, _count_ptr);
1566   }
1567 }
1568 
1569 void
1570 VM_GetFrameLocation::doit() {
1571   _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
1572   ThreadsListHandle tlh;
1573   if (_java_thread != NULL && tlh.includes(_java_thread)
< prev index next >