< prev index next >

src/hotspot/share/services/threadService.cpp

Print this page

   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"

  26 #include "classfile/systemDictionary.hpp"
  27 #include "classfile/vmClasses.hpp"
  28 #include "classfile/vmSymbols.hpp"
  29 #include "gc/shared/oopStorageSet.hpp"
  30 #include "memory/allocation.hpp"
  31 #include "memory/heapInspection.hpp"
  32 #include "memory/oopFactory.hpp"
  33 #include "memory/resourceArea.hpp"
  34 #include "memory/universe.hpp"
  35 #include "oops/instanceKlass.hpp"
  36 #include "oops/klass.inline.hpp"
  37 #include "oops/objArrayKlass.hpp"
  38 #include "oops/objArrayOop.inline.hpp"
  39 #include "oops/oop.inline.hpp"
  40 #include "oops/oopHandle.inline.hpp"
  41 #include "prims/jvmtiRawMonitor.hpp"
  42 #include "runtime/atomic.hpp"
  43 #include "runtime/handles.inline.hpp"
  44 #include "runtime/init.hpp"
  45 #include "runtime/objectMonitor.inline.hpp"

 555     _last->set_next(ts);
 556   }
 557   _last = ts;
 558 }
 559 
 560 void ThreadDumpResult::metadata_do(void f(Metadata*)) {
 561   for (ThreadSnapshot* ts = _snapshots; ts != NULL; ts = ts->next()) {
 562     ts->metadata_do(f);
 563   }
 564 }
 565 
 566 ThreadsList* ThreadDumpResult::t_list() {
 567   return _setter.list();
 568 }
 569 
 570 StackFrameInfo::StackFrameInfo(javaVFrame* jvf, bool with_lock_info) {
 571   _method = jvf->method();
 572   _bci = jvf->bci();
 573   _class_holder = OopHandle(_thread_service_storage, _method->method_holder()->klass_holder());
 574   _locked_monitors = NULL;

 575   if (with_lock_info) {
 576     Thread* current_thread = Thread::current();
 577     ResourceMark rm(current_thread);
 578     HandleMark hm(current_thread);
 579     GrowableArray<MonitorInfo*>* list = jvf->locked_monitors();
 580     int length = list->length();
 581     if (length > 0) {
 582       _locked_monitors = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<OopHandle>(length, mtServiceability);
 583       for (int i = 0; i < length; i++) {
 584         MonitorInfo* monitor = list->at(i);
 585         assert(monitor->owner() != NULL, "This monitor must have an owning object");
 586         _locked_monitors->append(OopHandle(_thread_service_storage, monitor->owner()));
 587       }
 588     }
 589   }
 590 }
 591 
 592 StackFrameInfo::~StackFrameInfo() {
 593   if (_locked_monitors != NULL) {
 594     for (int i = 0; i < _locked_monitors->length(); i++) {
 595       _locked_monitors->at(i).release(_thread_service_storage);
 596     }
 597     delete _locked_monitors;
 598   }
 599   _class_holder.release(_thread_service_storage);

 600 }
 601 
 602 void StackFrameInfo::metadata_do(void f(Metadata*)) {
 603   f(_method);
 604 }
 605 
 606 void StackFrameInfo::print_on(outputStream* st) const {
 607   ResourceMark rm;
 608   java_lang_Throwable::print_stack_element(st, method(), bci());
 609   int len = (_locked_monitors != NULL ? _locked_monitors->length() : 0);
 610   for (int i = 0; i < len; i++) {
 611     oop o = _locked_monitors->at(i).resolve();
 612     st->print_cr("\t- locked <" INTPTR_FORMAT "> (a %s)", p2i(o), o->klass()->external_name());
 613   }
 614 
 615 }
 616 
 617 // Iterate through monitor cache to find JNI locked monitors
 618 class InflatedMonitorsClosure: public MonitorClosure {
 619 private:

 642   }
 643 }
 644 
 645 void ThreadStackTrace::add_jni_locked_monitor(oop object) {
 646   _jni_locked_monitors->append(OopHandle(_thread_service_storage, object));
 647 }
 648 
 649 ThreadStackTrace::~ThreadStackTrace() {
 650   for (int i = 0; i < _frames->length(); i++) {
 651     delete _frames->at(i);
 652   }
 653   delete _frames;
 654   if (_jni_locked_monitors != NULL) {
 655     for (int i = 0; i < _jni_locked_monitors->length(); i++) {
 656       _jni_locked_monitors->at(i).release(_thread_service_storage);
 657     }
 658     delete _jni_locked_monitors;
 659   }
 660 }
 661 
 662 void ThreadStackTrace::dump_stack_at_safepoint(int maxDepth) {
 663   assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
 664 
 665   if (_thread->has_last_Java_frame()) {
 666     RegisterMap reg_map(_thread);
 667     vframe* start_vf = _thread->last_java_vframe(&reg_map);



 668     int count = 0;
 669     for (vframe* f = start_vf; f; f = f->sender() ) {
 670       if (maxDepth >= 0 && count == maxDepth) {
 671         // Skip frames if more than maxDepth
 672         break;
 673       }
 674       if (f->is_java_frame()) {
 675         javaVFrame* jvf = javaVFrame::cast(f);
 676         add_stack_frame(jvf);
 677         count++;
 678       } else {
 679         // Ignore non-Java frames
 680       }
 681     }
 682   }
 683 
 684   if (_with_locked_monitors) {
 685     // Iterate inflated monitors and find monitors locked by this thread
 686     // not found in the stack
 687     InflatedMonitorsClosure imc(this);

 704       assert(monitor != NULL, "must be a Java object");
 705       if (monitor == object) {
 706         found = true;
 707         break;
 708       }
 709     }
 710   }
 711   return found;
 712 }
 713 
 714 Handle ThreadStackTrace::allocate_fill_stack_trace_element_array(TRAPS) {
 715   InstanceKlass* ik = vmClasses::StackTraceElement_klass();
 716   assert(ik != NULL, "must be loaded in 1.4+");
 717 
 718   // Allocate an array of java/lang/StackTraceElement object
 719   objArrayOop ste = oopFactory::new_objArray(ik, _depth, CHECK_NH);
 720   objArrayHandle backtrace(THREAD, ste);
 721   for (int j = 0; j < _depth; j++) {
 722     StackFrameInfo* frame = _frames->at(j);
 723     methodHandle mh(THREAD, frame->method());
 724     oop element = java_lang_StackTraceElement::create(mh, frame->bci(), CHECK_NH);

 725     backtrace->obj_at_put(j, element);
 726   }
 727   return backtrace;
 728 }
 729 
 730 void ThreadStackTrace::add_stack_frame(javaVFrame* jvf) {
 731   StackFrameInfo* frame = new StackFrameInfo(jvf, _with_locked_monitors);
 732   _frames->append(frame);
 733   _depth++;
 734 }
 735 
 736 void ThreadStackTrace::metadata_do(void f(Metadata*)) {
 737   int length = _frames->length();
 738   for (int i = 0; i < length; i++) {
 739     _frames->at(i)->metadata_do(f);
 740   }
 741 }
 742 
 743 
 744 ConcurrentLocksDump::~ConcurrentLocksDump() {

 919   if (blocker_object != NULL) {
 920     _blocker_object = OopHandle(_thread_service_storage, blocker_object);
 921   }
 922   if (blocker_object_owner != NULL) {
 923     _blocker_object_owner = OopHandle(_thread_service_storage, blocker_object_owner);
 924   }
 925 }
 926 
 927 oop ThreadSnapshot::blocker_object() const           { return _blocker_object.resolve(); }
 928 oop ThreadSnapshot::blocker_object_owner() const     { return _blocker_object_owner.resolve(); }
 929 
 930 ThreadSnapshot::~ThreadSnapshot() {
 931   _blocker_object.release(_thread_service_storage);
 932   _blocker_object_owner.release(_thread_service_storage);
 933   _threadObj.release(_thread_service_storage);
 934 
 935   delete _stack_trace;
 936   delete _concurrent_locks;
 937 }
 938 
 939 void ThreadSnapshot::dump_stack_at_safepoint(int max_depth, bool with_locked_monitors) {
 940   _stack_trace = new ThreadStackTrace(_thread, with_locked_monitors);
 941   _stack_trace->dump_stack_at_safepoint(max_depth);
 942 }
 943 
 944 
 945 void ThreadSnapshot::metadata_do(void f(Metadata*)) {
 946   if (_stack_trace != NULL) {
 947     _stack_trace->metadata_do(f);
 948   }
 949 }
 950 
 951 
 952 DeadlockCycle::DeadlockCycle() {
 953   _threads = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<JavaThread*>(INITIAL_ARRAY_SIZE, mtServiceability);
 954   _next = NULL;
 955 }
 956 
 957 DeadlockCycle::~DeadlockCycle() {
 958   delete _threads;
 959 }
 960 
 961 void DeadlockCycle::print_on_with(ThreadsList * t_list, outputStream* st) const {

   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/javaClasses.inline.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "classfile/vmClasses.hpp"
  29 #include "classfile/vmSymbols.hpp"
  30 #include "gc/shared/oopStorageSet.hpp"
  31 #include "memory/allocation.hpp"
  32 #include "memory/heapInspection.hpp"
  33 #include "memory/oopFactory.hpp"
  34 #include "memory/resourceArea.hpp"
  35 #include "memory/universe.hpp"
  36 #include "oops/instanceKlass.hpp"
  37 #include "oops/klass.inline.hpp"
  38 #include "oops/objArrayKlass.hpp"
  39 #include "oops/objArrayOop.inline.hpp"
  40 #include "oops/oop.inline.hpp"
  41 #include "oops/oopHandle.inline.hpp"
  42 #include "prims/jvmtiRawMonitor.hpp"
  43 #include "runtime/atomic.hpp"
  44 #include "runtime/handles.inline.hpp"
  45 #include "runtime/init.hpp"
  46 #include "runtime/objectMonitor.inline.hpp"

 556     _last->set_next(ts);
 557   }
 558   _last = ts;
 559 }
 560 
 561 void ThreadDumpResult::metadata_do(void f(Metadata*)) {
 562   for (ThreadSnapshot* ts = _snapshots; ts != NULL; ts = ts->next()) {
 563     ts->metadata_do(f);
 564   }
 565 }
 566 
 567 ThreadsList* ThreadDumpResult::t_list() {
 568   return _setter.list();
 569 }
 570 
 571 StackFrameInfo::StackFrameInfo(javaVFrame* jvf, bool with_lock_info) {
 572   _method = jvf->method();
 573   _bci = jvf->bci();
 574   _class_holder = OopHandle(_thread_service_storage, _method->method_holder()->klass_holder());
 575   _locked_monitors = NULL;
 576   _cont_scope_name = OopHandle(_thread_service_storage, (jvf->continuation() != NULL) ? jdk_internal_vm_ContinuationScope::name(jdk_internal_vm_Continuation::scope(jvf->continuation())) : (oop)NULL);
 577   if (with_lock_info) {
 578     Thread* current_thread = Thread::current();
 579     ResourceMark rm(current_thread);
 580     HandleMark hm(current_thread);
 581     GrowableArray<MonitorInfo*>* list = jvf->locked_monitors();
 582     int length = list->length();
 583     if (length > 0) {
 584       _locked_monitors = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<OopHandle>(length, mtServiceability);
 585       for (int i = 0; i < length; i++) {
 586         MonitorInfo* monitor = list->at(i);
 587         assert(monitor->owner() != NULL, "This monitor must have an owning object");
 588         _locked_monitors->append(OopHandle(_thread_service_storage, monitor->owner()));
 589       }
 590     }
 591   }
 592 }
 593 
 594 StackFrameInfo::~StackFrameInfo() {
 595   if (_locked_monitors != NULL) {
 596     for (int i = 0; i < _locked_monitors->length(); i++) {
 597       _locked_monitors->at(i).release(_thread_service_storage);
 598     }
 599     delete _locked_monitors;
 600   }
 601   _class_holder.release(_thread_service_storage);
 602   _cont_scope_name.release(_thread_service_storage);
 603 }
 604 
 605 void StackFrameInfo::metadata_do(void f(Metadata*)) {
 606   f(_method);
 607 }
 608 
 609 void StackFrameInfo::print_on(outputStream* st) const {
 610   ResourceMark rm;
 611   java_lang_Throwable::print_stack_element(st, method(), bci());
 612   int len = (_locked_monitors != NULL ? _locked_monitors->length() : 0);
 613   for (int i = 0; i < len; i++) {
 614     oop o = _locked_monitors->at(i).resolve();
 615     st->print_cr("\t- locked <" INTPTR_FORMAT "> (a %s)", p2i(o), o->klass()->external_name());
 616   }
 617 
 618 }
 619 
 620 // Iterate through monitor cache to find JNI locked monitors
 621 class InflatedMonitorsClosure: public MonitorClosure {
 622 private:

 645   }
 646 }
 647 
 648 void ThreadStackTrace::add_jni_locked_monitor(oop object) {
 649   _jni_locked_monitors->append(OopHandle(_thread_service_storage, object));
 650 }
 651 
 652 ThreadStackTrace::~ThreadStackTrace() {
 653   for (int i = 0; i < _frames->length(); i++) {
 654     delete _frames->at(i);
 655   }
 656   delete _frames;
 657   if (_jni_locked_monitors != NULL) {
 658     for (int i = 0; i < _jni_locked_monitors->length(); i++) {
 659       _jni_locked_monitors->at(i).release(_thread_service_storage);
 660     }
 661     delete _jni_locked_monitors;
 662   }
 663 }
 664 
 665 void ThreadStackTrace::dump_stack_at_safepoint(int maxDepth, bool full) {
 666   assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
 667 
 668   if (_thread->has_last_Java_frame()) {
 669     RegisterMap reg_map(_thread, true, false);
 670 
 671     vframe* start_vf = !full && _thread->last_continuation(java_lang_VirtualThread::vthread_scope()) != NULL
 672       ? _thread->vthread_carrier_last_java_vframe(&reg_map)
 673       : _thread->last_java_vframe(&reg_map);
 674     int count = 0;
 675     for (vframe* f = start_vf; f; f = f->sender() ) {
 676       if (maxDepth >= 0 && count == maxDepth) {
 677         // Skip frames if more than maxDepth
 678         break;
 679       }
 680       if (f->is_java_frame()) {
 681         javaVFrame* jvf = javaVFrame::cast(f);
 682         add_stack_frame(jvf);
 683         count++;
 684       } else {
 685         // Ignore non-Java frames
 686       }
 687     }
 688   }
 689 
 690   if (_with_locked_monitors) {
 691     // Iterate inflated monitors and find monitors locked by this thread
 692     // not found in the stack
 693     InflatedMonitorsClosure imc(this);

 710       assert(monitor != NULL, "must be a Java object");
 711       if (monitor == object) {
 712         found = true;
 713         break;
 714       }
 715     }
 716   }
 717   return found;
 718 }
 719 
 720 Handle ThreadStackTrace::allocate_fill_stack_trace_element_array(TRAPS) {
 721   InstanceKlass* ik = vmClasses::StackTraceElement_klass();
 722   assert(ik != NULL, "must be loaded in 1.4+");
 723 
 724   // Allocate an array of java/lang/StackTraceElement object
 725   objArrayOop ste = oopFactory::new_objArray(ik, _depth, CHECK_NH);
 726   objArrayHandle backtrace(THREAD, ste);
 727   for (int j = 0; j < _depth; j++) {
 728     StackFrameInfo* frame = _frames->at(j);
 729     methodHandle mh(THREAD, frame->method());
 730     Handle contScopeNameH(THREAD, frame->cont_scope_name().resolve());
 731     oop element = java_lang_StackTraceElement::create(mh, frame->bci(), contScopeNameH, CHECK_NH);
 732     backtrace->obj_at_put(j, element);
 733   }
 734   return backtrace;
 735 }
 736 
 737 void ThreadStackTrace::add_stack_frame(javaVFrame* jvf) {
 738   StackFrameInfo* frame = new StackFrameInfo(jvf, _with_locked_monitors);
 739   _frames->append(frame);
 740   _depth++;
 741 }
 742 
 743 void ThreadStackTrace::metadata_do(void f(Metadata*)) {
 744   int length = _frames->length();
 745   for (int i = 0; i < length; i++) {
 746     _frames->at(i)->metadata_do(f);
 747   }
 748 }
 749 
 750 
 751 ConcurrentLocksDump::~ConcurrentLocksDump() {

 926   if (blocker_object != NULL) {
 927     _blocker_object = OopHandle(_thread_service_storage, blocker_object);
 928   }
 929   if (blocker_object_owner != NULL) {
 930     _blocker_object_owner = OopHandle(_thread_service_storage, blocker_object_owner);
 931   }
 932 }
 933 
 934 oop ThreadSnapshot::blocker_object() const           { return _blocker_object.resolve(); }
 935 oop ThreadSnapshot::blocker_object_owner() const     { return _blocker_object_owner.resolve(); }
 936 
 937 ThreadSnapshot::~ThreadSnapshot() {
 938   _blocker_object.release(_thread_service_storage);
 939   _blocker_object_owner.release(_thread_service_storage);
 940   _threadObj.release(_thread_service_storage);
 941 
 942   delete _stack_trace;
 943   delete _concurrent_locks;
 944 }
 945 
 946 void ThreadSnapshot::dump_stack_at_safepoint(int max_depth, bool with_locked_monitors, bool full) {
 947   _stack_trace = new ThreadStackTrace(_thread, with_locked_monitors);
 948   _stack_trace->dump_stack_at_safepoint(max_depth, full);
 949 }
 950 
 951 
 952 void ThreadSnapshot::metadata_do(void f(Metadata*)) {
 953   if (_stack_trace != NULL) {
 954     _stack_trace->metadata_do(f);
 955   }
 956 }
 957 
 958 
 959 DeadlockCycle::DeadlockCycle() {
 960   _threads = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<JavaThread*>(INITIAL_ARRAY_SIZE, mtServiceability);
 961   _next = NULL;
 962 }
 963 
 964 DeadlockCycle::~DeadlockCycle() {
 965   delete _threads;
 966 }
 967 
 968 void DeadlockCycle::print_on_with(ThreadsList * t_list, outputStream* st) const {
< prev index next >