< prev index next >

src/hotspot/share/prims/jvmtiEnvBase.cpp

Print this page




 543   }
 544   return objArray;
 545 }
 546 
 547 jthread *
 548 JvmtiEnvBase::new_jthreadArray(int length, Handle *handles) {
 549   return (jthread *) new_jobjectArray(length,handles);
 550 }
 551 
 552 jthreadGroup *
 553 JvmtiEnvBase::new_jthreadGroupArray(int length, Handle *handles) {
 554   return (jthreadGroup *) new_jobjectArray(length,handles);
 555 }
 556 
 557 // return the vframe on the specified thread and depth, NULL if no such frame
 558 vframe*
 559 JvmtiEnvBase::vframeFor(JavaThread* java_thread, jint depth) {
 560   if (!java_thread->has_last_Java_frame()) {
 561     return NULL;
 562   }
 563   RegisterMap reg_map(java_thread);
 564   vframe *vf = java_thread->last_java_vframe(&reg_map);
 565   int d = 0;
 566   while ((vf != NULL) && (d < depth)) {
 567     vf = vf->java_sender();
 568     d++;
 569   }
 570   return vf;
 571 }
 572 
 573 
 574 //
 575 // utilities: JNI objects
 576 //
 577 
 578 
 579 jclass
 580 JvmtiEnvBase::get_jni_class_non_null(Klass* k) {
 581   assert(k != NULL, "k != NULL");
 582   Thread *thread = Thread::current();
 583   return (jclass)jni_reference(Handle(thread, k->java_mirror()));


 606 
 607 //
 608 // Object Monitor Information
 609 //
 610 
 611 //
 612 // Count the number of objects for a lightweight monitor. The hobj
 613 // parameter is object that owns the monitor so this routine will
 614 // count the number of times the same object was locked by frames
 615 // in java_thread.
 616 //
 617 jint
 618 JvmtiEnvBase::count_locked_objects(JavaThread *java_thread, Handle hobj) {
 619   jint ret = 0;
 620   if (!java_thread->has_last_Java_frame()) {
 621     return ret;  // no Java frames so no monitors
 622   }
 623 
 624   ResourceMark rm;
 625   HandleMark   hm;
 626   RegisterMap  reg_map(java_thread);
 627 
 628   for(javaVFrame *jvf=java_thread->last_java_vframe(&reg_map); jvf != NULL;
 629                                                  jvf = jvf->java_sender()) {
 630     GrowableArray<MonitorInfo*>* mons = jvf->monitors();
 631     if (!mons->is_empty()) {
 632       for (int i = 0; i < mons->length(); i++) {
 633         MonitorInfo *mi = mons->at(i);
 634         if (mi->owner_is_scalar_replaced()) continue;
 635 
 636         // see if owner of the monitor is our object
 637         if (mi->owner() != NULL && mi->owner() == hobj()) {
 638           ret++;
 639         }
 640       }
 641     }
 642   }
 643   return ret;
 644 }
 645 
 646 


 679     *monitor_ptr = jni_reference(calling_thread, hobj);
 680   }
 681   return JVMTI_ERROR_NONE;
 682 }
 683 
 684 
 685 jvmtiError
 686 JvmtiEnvBase::get_owned_monitors(JavaThread *calling_thread, JavaThread* java_thread,
 687                                  GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list) {
 688   jvmtiError err = JVMTI_ERROR_NONE;
 689 #ifdef ASSERT
 690   uint32_t debug_bits = 0;
 691 #endif
 692   assert((SafepointSynchronize::is_at_safepoint() ||
 693           java_thread->is_thread_fully_suspended(false, &debug_bits)),
 694          "at safepoint or target thread is suspended");
 695 
 696   if (java_thread->has_last_Java_frame()) {
 697     ResourceMark rm;
 698     HandleMark   hm;
 699     RegisterMap  reg_map(java_thread);
 700 
 701     int depth = 0;
 702     for (javaVFrame *jvf = java_thread->last_java_vframe(&reg_map); jvf != NULL;
 703          jvf = jvf->java_sender()) {
 704       if (MaxJavaStackTraceDepth == 0 || depth++ < MaxJavaStackTraceDepth) {  // check for stack too deep
 705         // add locked objects for this frame into list
 706         err = get_locked_objects_in_frame(calling_thread, java_thread, jvf, owned_monitors_list, depth-1);
 707         if (err != JVMTI_ERROR_NONE) {
 708           return err;
 709         }
 710       }
 711     }
 712   }
 713 
 714   // Get off stack monitors. (e.g. acquired via jni MonitorEnter).
 715   JvmtiMonitorClosure jmc(java_thread, calling_thread, owned_monitors_list, this);
 716   ObjectSynchronizer::monitors_iterate(&jmc);
 717   err = jmc.error();
 718 
 719   return err;


 788         continue;
 789       }
 790     }
 791 
 792     // add the owning object to our list
 793     jvmtiMonitorStackDepthInfo *jmsdi;
 794     err = allocate(sizeof(jvmtiMonitorStackDepthInfo), (unsigned char **)&jmsdi);
 795     if (err != JVMTI_ERROR_NONE) {
 796         return err;
 797     }
 798     Handle hobj(Thread::current(), obj);
 799     jmsdi->monitor = jni_reference(calling_thread, hobj);
 800     jmsdi->stack_depth = stack_depth;
 801     owned_monitors_list->append(jmsdi);
 802   }
 803 
 804   return err;
 805 }
 806 
 807 jvmtiError






























































 808 JvmtiEnvBase::get_stack_trace(JavaThread *java_thread,
 809                               jint start_depth, jint max_count,
 810                               jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
 811 #ifdef ASSERT
 812   uint32_t debug_bits = 0;
 813 #endif
 814   assert((SafepointSynchronize::is_at_safepoint() ||
 815           java_thread->is_thread_fully_suspended(false, &debug_bits)),
 816          "at safepoint or target thread is suspended");
 817   int count = 0;


 818   if (java_thread->has_last_Java_frame()) {
 819     RegisterMap reg_map(java_thread);
 820     Thread* current_thread = Thread::current();
 821     ResourceMark rm(current_thread);
 822     javaVFrame *jvf = java_thread->last_java_vframe(&reg_map);
 823     HandleMark hm(current_thread);
 824     if (start_depth != 0) {
 825       if (start_depth > 0) {
 826         for (int j = 0; j < start_depth && jvf != NULL; j++) {
 827           jvf = jvf->java_sender();
 828         }
 829         if (jvf == NULL) {
 830           // start_depth is deeper than the stack depth
 831           return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 832         }
 833       } else { // start_depth < 0
 834         // we are referencing the starting depth based on the oldest
 835         // part of the stack.
 836         // optimize to limit the number of times that java_sender() is called
 837         javaVFrame *jvf_cursor = jvf;
 838         javaVFrame *jvf_prev = NULL;
 839         javaVFrame *jvf_prev_prev = NULL;
 840         int j = 0;
 841         while (jvf_cursor != NULL) {
 842           jvf_prev_prev = jvf_prev;
 843           jvf_prev = jvf_cursor;
 844           for (j = 0; j > start_depth && jvf_cursor != NULL; j--) {
 845             jvf_cursor = jvf_cursor->java_sender();
 846           }
 847         }
 848         if (j == start_depth) {
 849           // previous pointer is exactly where we want to start
 850           jvf = jvf_prev;
 851         } else {
 852           // we need to back up further to get to the right place
 853           if (jvf_prev_prev == NULL) {
 854             // the -start_depth is greater than the stack depth
 855             return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 856           }
 857           // j now is the number of frames on the stack starting with
 858           // jvf_prev, we start from jvf_prev_prev and move older on
 859           // the stack that many, the result is -start_depth frames
 860           // remaining.
 861           jvf = jvf_prev_prev;
 862           for (; j < 0; j++) {
 863             jvf = jvf->java_sender();
 864           }
 865         }
 866       }
 867     }
 868     for (; count < max_count && jvf != NULL; count++) {
 869       frame_buffer[count].method = jvf->method()->jmethod_id();
 870       frame_buffer[count].location = (jvf->method()->is_native() ? -1 : jvf->bci());
 871       jvf = jvf->java_sender();
 872     }
 873   } else {

 874     if (start_depth != 0) {
 875       // no frames and there is a starting depth
 876       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 877     }
 878   }
 879   *count_ptr = count;
 880   return JVMTI_ERROR_NONE;
 881 }
 882 
 883 jvmtiError
 884 JvmtiEnvBase::get_frame_count(JvmtiThreadState *state, jint *count_ptr) {
 885   assert((state != NULL),
 886          "JavaThread should create JvmtiThreadState before calling this method");
 887   *count_ptr = state->count_frames();
 888   return JVMTI_ERROR_NONE;
 889 }
 890 
 891 jvmtiError
 892 JvmtiEnvBase::get_frame_location(JavaThread *java_thread, jint depth,
 893                                  jmethodID* method_ptr, jlocation* location_ptr) {
 894 #ifdef ASSERT
 895   uint32_t debug_bits = 0;
 896 #endif
 897   assert((SafepointSynchronize::is_at_safepoint() ||
 898           java_thread->is_thread_fully_suspended(false, &debug_bits)),
 899          "at safepoint or target thread is suspended");
 900   Thread* current_thread = Thread::current();


1577 
1578 void
1579 VM_GetFrameCount::doit() {
1580   _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
1581   JavaThread* jt = _state->get_thread();
1582   ThreadsListHandle tlh;
1583   if (jt != NULL && tlh.includes(jt) && !jt->is_exiting() && jt->threadObj() != NULL) {
1584     _result = ((JvmtiEnvBase*)_env)->get_frame_count(_state, _count_ptr);
1585   }
1586 }
1587 
1588 void
1589 VM_GetFrameLocation::doit() {
1590   _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
1591   ThreadsListHandle tlh;
1592   if (_java_thread != NULL && tlh.includes(_java_thread)
1593       && !_java_thread->is_exiting() && _java_thread->threadObj() != NULL) {
1594     _result = ((JvmtiEnvBase*)_env)->get_frame_location(_java_thread, _depth,
1595                                                         _method_ptr, _location_ptr);
1596   }





















































































1597 }


 543   }
 544   return objArray;
 545 }
 546 
 547 jthread *
 548 JvmtiEnvBase::new_jthreadArray(int length, Handle *handles) {
 549   return (jthread *) new_jobjectArray(length,handles);
 550 }
 551 
 552 jthreadGroup *
 553 JvmtiEnvBase::new_jthreadGroupArray(int length, Handle *handles) {
 554   return (jthreadGroup *) new_jobjectArray(length,handles);
 555 }
 556 
 557 // return the vframe on the specified thread and depth, NULL if no such frame
 558 vframe*
 559 JvmtiEnvBase::vframeFor(JavaThread* java_thread, jint depth) {
 560   if (!java_thread->has_last_Java_frame()) {
 561     return NULL;
 562   }
 563   RegisterMap reg_map(java_thread, true, true);
 564   vframe *vf = java_thread->last_java_vframe(&reg_map);
 565   int d = 0;
 566   while ((vf != NULL) && (d < depth)) {
 567     vf = vf->java_sender();
 568     d++;
 569   }
 570   return vf;
 571 }
 572 
 573 
 574 //
 575 // utilities: JNI objects
 576 //
 577 
 578 
 579 jclass
 580 JvmtiEnvBase::get_jni_class_non_null(Klass* k) {
 581   assert(k != NULL, "k != NULL");
 582   Thread *thread = Thread::current();
 583   return (jclass)jni_reference(Handle(thread, k->java_mirror()));


 606 
 607 //
 608 // Object Monitor Information
 609 //
 610 
 611 //
 612 // Count the number of objects for a lightweight monitor. The hobj
 613 // parameter is object that owns the monitor so this routine will
 614 // count the number of times the same object was locked by frames
 615 // in java_thread.
 616 //
 617 jint
 618 JvmtiEnvBase::count_locked_objects(JavaThread *java_thread, Handle hobj) {
 619   jint ret = 0;
 620   if (!java_thread->has_last_Java_frame()) {
 621     return ret;  // no Java frames so no monitors
 622   }
 623 
 624   ResourceMark rm;
 625   HandleMark   hm;
 626   RegisterMap  reg_map(java_thread, true, true);
 627 
 628   for(javaVFrame *jvf=java_thread->last_java_vframe(&reg_map); jvf != NULL;
 629                                                  jvf = jvf->java_sender()) {
 630     GrowableArray<MonitorInfo*>* mons = jvf->monitors();
 631     if (!mons->is_empty()) {
 632       for (int i = 0; i < mons->length(); i++) {
 633         MonitorInfo *mi = mons->at(i);
 634         if (mi->owner_is_scalar_replaced()) continue;
 635 
 636         // see if owner of the monitor is our object
 637         if (mi->owner() != NULL && mi->owner() == hobj()) {
 638           ret++;
 639         }
 640       }
 641     }
 642   }
 643   return ret;
 644 }
 645 
 646 


 679     *monitor_ptr = jni_reference(calling_thread, hobj);
 680   }
 681   return JVMTI_ERROR_NONE;
 682 }
 683 
 684 
 685 jvmtiError
 686 JvmtiEnvBase::get_owned_monitors(JavaThread *calling_thread, JavaThread* java_thread,
 687                                  GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list) {
 688   jvmtiError err = JVMTI_ERROR_NONE;
 689 #ifdef ASSERT
 690   uint32_t debug_bits = 0;
 691 #endif
 692   assert((SafepointSynchronize::is_at_safepoint() ||
 693           java_thread->is_thread_fully_suspended(false, &debug_bits)),
 694          "at safepoint or target thread is suspended");
 695 
 696   if (java_thread->has_last_Java_frame()) {
 697     ResourceMark rm;
 698     HandleMark   hm;
 699     RegisterMap  reg_map(java_thread, true, true);
 700 
 701     int depth = 0;
 702     for (javaVFrame *jvf = java_thread->last_java_vframe(&reg_map); jvf != NULL;
 703          jvf = jvf->java_sender()) {
 704       if (MaxJavaStackTraceDepth == 0 || depth++ < MaxJavaStackTraceDepth) {  // check for stack too deep
 705         // add locked objects for this frame into list
 706         err = get_locked_objects_in_frame(calling_thread, java_thread, jvf, owned_monitors_list, depth-1);
 707         if (err != JVMTI_ERROR_NONE) {
 708           return err;
 709         }
 710       }
 711     }
 712   }
 713 
 714   // Get off stack monitors. (e.g. acquired via jni MonitorEnter).
 715   JvmtiMonitorClosure jmc(java_thread, calling_thread, owned_monitors_list, this);
 716   ObjectSynchronizer::monitors_iterate(&jmc);
 717   err = jmc.error();
 718 
 719   return err;


 788         continue;
 789       }
 790     }
 791 
 792     // add the owning object to our list
 793     jvmtiMonitorStackDepthInfo *jmsdi;
 794     err = allocate(sizeof(jvmtiMonitorStackDepthInfo), (unsigned char **)&jmsdi);
 795     if (err != JVMTI_ERROR_NONE) {
 796         return err;
 797     }
 798     Handle hobj(Thread::current(), obj);
 799     jmsdi->monitor = jni_reference(calling_thread, hobj);
 800     jmsdi->stack_depth = stack_depth;
 801     owned_monitors_list->append(jmsdi);
 802   }
 803 
 804   return err;
 805 }
 806 
 807 jvmtiError
 808 JvmtiEnvBase::get_stack_trace(javaVFrame *jvf,
 809                               jint start_depth, jint max_count,
 810                               jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
 811   Thread* current_thread = Thread::current();
 812   ResourceMark rm(current_thread);
 813   HandleMark hm(current_thread);
 814   int count = 0;
 815 
 816   if (start_depth != 0) {
 817     if (start_depth > 0) {
 818       for (int j = 0; j < start_depth && jvf != NULL; j++) {
 819         jvf = jvf->java_sender();
 820       }
 821       if (jvf == NULL) {
 822         // start_depth is deeper than the stack depth
 823         return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 824       }
 825     } else { // start_depth < 0
 826       // we are referencing the starting depth based on the oldest
 827       // part of the stack.
 828       // optimize to limit the number of times that java_sender() is called
 829       javaVFrame *jvf_cursor = jvf;
 830       javaVFrame *jvf_prev = NULL;
 831       javaVFrame *jvf_prev_prev = NULL;
 832       int j = 0;
 833       while (jvf_cursor != NULL) {
 834         jvf_prev_prev = jvf_prev;
 835         jvf_prev = jvf_cursor;
 836         for (j = 0; j > start_depth && jvf_cursor != NULL; j--) {
 837           jvf_cursor = jvf_cursor->java_sender();
 838         }
 839       }
 840       if (j == start_depth) {
 841         // previous pointer is exactly where we want to start
 842         jvf = jvf_prev;
 843       } else {
 844         // we need to back up further to get to the right place
 845         if (jvf_prev_prev == NULL) {
 846           // the -start_depth is greater than the stack depth
 847           return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 848         }
 849         // j now is the number of frames on the stack starting with
 850         // jvf_prev, we start from jvf_prev_prev and move older on
 851         // the stack that many, the result is -start_depth frames
 852         // remaining.
 853         jvf = jvf_prev_prev;
 854         for (; j < 0; j++) {
 855           jvf = jvf->java_sender();
 856         }
 857       }
 858     }
 859   }
 860   for (; count < max_count && jvf != NULL; count++) {
 861     frame_buffer[count].method = jvf->method()->jmethod_id();
 862     frame_buffer[count].location = (jvf->method()->is_native() ? -1 : jvf->bci());
 863     jvf = jvf->java_sender();
 864   }
 865   *count_ptr = count;
 866   return JVMTI_ERROR_NONE;
 867 }
 868 
 869 jvmtiError
 870 JvmtiEnvBase::get_stack_trace(JavaThread *java_thread,
 871                               jint start_depth, jint max_count,
 872                               jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
 873 #ifdef ASSERT
 874   uint32_t debug_bits = 0;
 875 #endif
 876   assert((SafepointSynchronize::is_at_safepoint() ||
 877           java_thread->is_thread_fully_suspended(false, &debug_bits)),
 878          "at safepoint or target thread is suspended");
 879   int count = 0;
 880   jvmtiError err = JVMTI_ERROR_NONE;
 881 
 882   if (java_thread->has_last_Java_frame()) {
 883     RegisterMap reg_map(java_thread, true, true);
 884     Thread* current_thread = Thread::current();
 885     ResourceMark rm(current_thread);
 886     javaVFrame *jvf = java_thread->last_java_vframe(&reg_map);
 887 
 888     err = get_stack_trace(jvf, start_depth, max_count, frame_buffer, count_ptr);
















































 889   } else {
 890     *count_ptr = 0;
 891     if (start_depth != 0) {
 892       // no frames and there is a starting depth
 893       err = JVMTI_ERROR_ILLEGAL_ARGUMENT;
 894     }
 895   }
 896   return err;

 897 }
 898 
 899 jvmtiError
 900 JvmtiEnvBase::get_frame_count(JvmtiThreadState *state, jint *count_ptr) {
 901   assert((state != NULL),
 902          "JavaThread should create JvmtiThreadState before calling this method");
 903   *count_ptr = state->count_frames();
 904   return JVMTI_ERROR_NONE;
 905 }
 906 
 907 jvmtiError
 908 JvmtiEnvBase::get_frame_location(JavaThread *java_thread, jint depth,
 909                                  jmethodID* method_ptr, jlocation* location_ptr) {
 910 #ifdef ASSERT
 911   uint32_t debug_bits = 0;
 912 #endif
 913   assert((SafepointSynchronize::is_at_safepoint() ||
 914           java_thread->is_thread_fully_suspended(false, &debug_bits)),
 915          "at safepoint or target thread is suspended");
 916   Thread* current_thread = Thread::current();


1593 
1594 void
1595 VM_GetFrameCount::doit() {
1596   _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
1597   JavaThread* jt = _state->get_thread();
1598   ThreadsListHandle tlh;
1599   if (jt != NULL && tlh.includes(jt) && !jt->is_exiting() && jt->threadObj() != NULL) {
1600     _result = ((JvmtiEnvBase*)_env)->get_frame_count(_state, _count_ptr);
1601   }
1602 }
1603 
1604 void
1605 VM_GetFrameLocation::doit() {
1606   _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
1607   ThreadsListHandle tlh;
1608   if (_java_thread != NULL && tlh.includes(_java_thread)
1609       && !_java_thread->is_exiting() && _java_thread->threadObj() != NULL) {
1610     _result = ((JvmtiEnvBase*)_env)->get_frame_location(_java_thread, _depth,
1611                                                         _method_ptr, _location_ptr);
1612   }
1613 }
1614 
1615 void
1616 VM_GetFiberThread::doit() {
1617   oop carrier_thread = java_lang_Fiber::carrier_thread(_fiber_h());
1618   *_carrier_thread_ptr = (jthread)JNIHandles::make_local(_current_thread, carrier_thread);
1619 }
1620 
1621 static
1622 javaVFrame* get_fiber_jvf(Thread* cur_thread, oop fiber) {
1623   oop cont = java_lang_Fiber::continuation(fiber);
1624   javaVFrame* jvf = NULL;
1625 
1626   if (cont != NULL && java_lang_Continuation::is_mounted(cont)) {
1627     oop carrier_thread = java_lang_Fiber::carrier_thread(fiber);
1628     JavaThread* java_thread = java_lang_Thread::thread(carrier_thread);
1629     vframeStream vfs(java_thread, Handle(cur_thread, Continuation::continuation_scope(cont)));
1630     
1631     if (!vfs.at_end()) {
1632       jvf = vfs.asJavaVFrame();
1633     }
1634   } else {
1635     Handle cont_h(cur_thread, cont);
1636     vframeStream vfs(cont_h);
1637 
1638     if (!vfs.at_end()) {
1639       jvf = vfs.asJavaVFrame();
1640     }
1641   }
1642   return jvf;
1643 }
1644 
1645 void
1646 VM_GetFiberStackTrace::doit() {
1647   Thread* cur_thread = Thread::current();
1648   ResourceMark rm(cur_thread);
1649   HandleMark hm(cur_thread);
1650   javaVFrame *jvf = get_fiber_jvf(cur_thread, _fiber_h());
1651   _result = ((JvmtiEnvBase *)_env)->get_stack_trace(jvf,
1652                                                     _start_depth, _max_count,
1653                                                     _frame_buffer, _count_ptr);
1654 }
1655 
1656 void
1657 VM_GetFiberFrameCount::doit() {
1658   Thread* cur_thread = Thread::current();
1659   ResourceMark rm(cur_thread);
1660   HandleMark hm(cur_thread);
1661   javaVFrame *jvf = get_fiber_jvf(cur_thread, _fiber_h());
1662   int count = 0;
1663 
1664   while (jvf != NULL) {
1665     Method* method = jvf->method();
1666     jvf = jvf->java_sender();
1667     count++;
1668   }
1669   *_count_ptr = count;
1670 }
1671 
1672 void
1673 VM_GetFiberFrameLocation::doit() {
1674   Thread* cur_thread = Thread::current();
1675   ResourceMark rm(cur_thread);
1676   HandleMark hm(cur_thread);
1677   javaVFrame *jvf = get_fiber_jvf(cur_thread, _fiber_h());
1678   int  depth = -1;
1679 
1680   while (jvf != NULL && depth < _depth) {
1681     Method* method = jvf->method();
1682     jvf = jvf->java_sender();
1683     depth++;
1684   }
1685 
1686   if (depth < _depth) {
1687     _result = JVMTI_ERROR_NO_MORE_FRAMES;
1688     return;
1689   }
1690 
1691   Method* method = jvf->method();
1692   if (method->is_native()) {
1693     *_location_ptr = -1;
1694   } else {
1695     *_location_ptr = jvf->bci();
1696   }
1697   *_method_ptr = method->jmethod_id();
1698 }
< prev index next >