54 #include "oops/fieldStreams.inline.hpp"
55 #include "oops/instanceKlass.inline.hpp"
56 #include "oops/instanceMirrorKlass.hpp"
57 #include "oops/klass.inline.hpp"
58 #include "oops/method.inline.hpp"
59 #include "oops/objArrayKlass.hpp"
60 #include "oops/objArrayOop.inline.hpp"
61 #include "oops/oop.inline.hpp"
62 #include "oops/oopCast.inline.hpp"
63 #include "oops/recordComponent.hpp"
64 #include "oops/symbol.hpp"
65 #include "oops/typeArrayOop.inline.hpp"
66 #include "prims/jvmtiExport.hpp"
67 #include "prims/methodHandles.hpp"
68 #include "prims/resolvedMethodTable.hpp"
69 #include "runtime/continuationEntry.inline.hpp"
70 #include "runtime/continuationJavaClasses.inline.hpp"
71 #include "runtime/fieldDescriptor.inline.hpp"
72 #include "runtime/frame.inline.hpp"
73 #include "runtime/handles.inline.hpp"
74 #include "runtime/handshake.hpp"
75 #include "runtime/init.hpp"
76 #include "runtime/interfaceSupport.inline.hpp"
77 #include "runtime/java.hpp"
78 #include "runtime/javaCalls.hpp"
79 #include "runtime/javaThread.hpp"
80 #include "runtime/jniHandles.inline.hpp"
81 #include "runtime/reflection.hpp"
82 #include "runtime/safepoint.hpp"
83 #include "runtime/safepointVerifiers.hpp"
84 #include "runtime/threadSMR.hpp"
85 #include "runtime/vframe.inline.hpp"
86 #include "runtime/vm_version.hpp"
87 #include "utilities/align.hpp"
88 #include "utilities/globalDefinitions.hpp"
89 #include "utilities/growableArray.hpp"
90 #include "utilities/preserveException.hpp"
91 #include "utilities/utf8.hpp"
92 #if INCLUDE_JVMCI
93 #include "jvmci/jvmciJavaClasses.hpp"
94 #endif
1887
1888 // Read thread status value from threadStatus field in java.lang.Thread java class.
1889 JavaThreadStatus java_lang_Thread::get_thread_status(oop java_thread) {
1890 // Make sure the caller is operating on behalf of the VM or is
1891 // running VM code (state == _thread_in_vm).
1892 assert(Threads_lock->owned_by_self() || Thread::current()->is_VM_thread() ||
1893 JavaThread::current()->thread_state() == _thread_in_vm ||
1894 JavaThread::current() == java_lang_Thread::thread(java_thread),
1895 "unsafe call to java_lang_Thread::get_thread_status()?");
1896 GET_FIELDHOLDER_FIELD(java_thread, get_thread_status, JavaThreadStatus::NEW /* not initialized */);
1897 }
1898
1899 ByteSize java_lang_Thread::thread_id_offset() {
1900 return in_ByteSize(_tid_offset);
1901 }
1902
1903 oop java_lang_Thread::park_blocker(oop java_thread) {
1904 return java_thread->obj_field_access<MO_RELAXED>(_park_blocker_offset);
1905 }
1906
1907 // Obtain stack trace for platform or mounted virtual thread.
1908 // If jthread is a virtual thread and it has been unmounted (or remounted to different carrier) the method returns null.
1909 // The caller (java.lang.VirtualThread) handles returned nulls via retry.
1910 oop java_lang_Thread::async_get_stack_trace(jobject jthread, TRAPS) {
1911 ThreadsListHandle tlh(THREAD);
1912 JavaThread* java_thread = nullptr;
1913 oop thread_oop;
1914
1915 bool has_java_thread = tlh.cv_internal_thread_to_JavaThread(jthread, &java_thread, &thread_oop);
1916 if (!has_java_thread) {
1917 return nullptr;
1918 }
1919
1920 class GetStackTraceHandshakeClosure : public HandshakeClosure {
1921 public:
1922 const Handle _thread_h;
1923 int _depth;
1924 bool _retry_handshake;
1925 GrowableArray<Method*>* _methods;
1926 GrowableArray<int>* _bcis;
1927
1928 GetStackTraceHandshakeClosure(Handle thread_h) :
1929 HandshakeClosure("GetStackTraceHandshakeClosure"), _thread_h(thread_h), _depth(0), _retry_handshake(false),
1930 _methods(nullptr), _bcis(nullptr) {
1931 }
1932 ~GetStackTraceHandshakeClosure() {
1933 delete _methods;
1934 delete _bcis;
1935 }
1936
1937 bool read_reset_retry() {
1938 bool ret = _retry_handshake;
1939 // If we re-execute the handshake this method need to return false
1940 // when the handshake cannot be performed. (E.g. thread terminating)
1941 _retry_handshake = false;
1942 return ret;
1943 }
1944
1945 void do_thread(Thread* th) {
1946 if (!Thread::current()->is_Java_thread()) {
1947 _retry_handshake = true;
1948 return;
1949 }
1950
1951 JavaThread* java_thread = JavaThread::cast(th);
1952
1953 if (!java_thread->has_last_Java_frame()) {
1954 return;
1955 }
1956
1957 bool carrier = false;
1958 if (java_lang_VirtualThread::is_instance(_thread_h())) {
1959 // Ensure _thread_h is still mounted to java_thread.
1960 const ContinuationEntry* ce = java_thread->vthread_continuation();
1961 if (ce == nullptr || ce->cont_oop(java_thread) != java_lang_VirtualThread::continuation(_thread_h())) {
1962 // Target thread has been unmounted.
1963 return;
1964 }
1965 } else {
1966 carrier = (java_thread->vthread_continuation() != nullptr);
1967 }
1968
1969 const int max_depth = MaxJavaStackTraceDepth;
1970 const bool skip_hidden = !ShowHiddenFrames;
1971
1972 // Pick minimum length that will cover most cases
1973 int init_length = 64;
1974 _methods = new (mtInternal) GrowableArray<Method*>(init_length, mtInternal);
1975 _bcis = new (mtInternal) GrowableArray<int>(init_length, mtInternal);
1976
1977 int total_count = 0;
1978 for (vframeStream vfst(java_thread, false, false, carrier); // we don't process frames as we don't care about oops
1979 !vfst.at_end() && (max_depth == 0 || max_depth != total_count);
1980 vfst.next()) {
1981
1982 if (skip_hidden && (vfst.method()->is_hidden() ||
1983 vfst.method()->is_continuation_enter_intrinsic())) {
1984 continue;
1985 }
1986
1987 _methods->push(vfst.method());
1988 _bcis->push(vfst.bci());
1989 total_count++;
1990 }
1991
1992 _depth = total_count;
1993 }
1994 };
1995
1996 // Handshake with target
1997 ResourceMark rm(THREAD);
1998 HandleMark hm(THREAD);
1999 GetStackTraceHandshakeClosure gsthc(Handle(THREAD, thread_oop));
2000 do {
2001 Handshake::execute(&gsthc, &tlh, java_thread);
2002 } while (gsthc.read_reset_retry());
2003
2004 // Stop if no stack trace is found.
2005 if (gsthc._depth == 0) {
2006 return nullptr;
2007 }
2008
2009 // Convert to StackTraceElement array
2010 InstanceKlass* k = vmClasses::StackTraceElement_klass();
2011 assert(k != nullptr, "must be loaded in 1.4+");
2012 if (k->should_be_initialized()) {
2013 k->initialize(CHECK_NULL);
2014 }
2015 objArrayHandle trace = oopFactory::new_objArray_handle(k, gsthc._depth, CHECK_NULL);
2016
2017 for (int i = 0; i < gsthc._depth; i++) {
2018 methodHandle method(THREAD, gsthc._methods->at(i));
2019 oop element = java_lang_StackTraceElement::create(method,
2020 gsthc._bcis->at(i),
2021 CHECK_NULL);
2022 trace->obj_at_put(i, element);
2023 }
2024
2025 return trace();
2026 }
2027
2028 const char* java_lang_Thread::thread_status_name(oop java_thread) {
2029 JavaThreadStatus status = get_thread_status(java_thread);
2030 switch (status) {
2031 case JavaThreadStatus::NEW : return "NEW";
2032 case JavaThreadStatus::RUNNABLE : return "RUNNABLE";
2033 case JavaThreadStatus::SLEEPING : return "TIMED_WAITING (sleeping)";
2034 case JavaThreadStatus::IN_OBJECT_WAIT : return "WAITING (on object monitor)";
2035 case JavaThreadStatus::IN_OBJECT_WAIT_TIMED : return "TIMED_WAITING (on object monitor)";
2036 case JavaThreadStatus::PARKED : return "WAITING (parking)";
2037 case JavaThreadStatus::PARKED_TIMED : return "TIMED_WAITING (parking)";
2038 case JavaThreadStatus::BLOCKED_ON_MONITOR_ENTER : return "BLOCKED (on object monitor)";
2039 case JavaThreadStatus::TERMINATED : return "TERMINATED";
2040 default : return "UNKNOWN";
2041 };
2042 }
2043 int java_lang_ThreadGroup::_parent_offset;
2044 int java_lang_ThreadGroup::_name_offset;
2045 int java_lang_ThreadGroup::_maxPriority_offset;
2046 int java_lang_ThreadGroup::_daemon_offset;
2047
|
54 #include "oops/fieldStreams.inline.hpp"
55 #include "oops/instanceKlass.inline.hpp"
56 #include "oops/instanceMirrorKlass.hpp"
57 #include "oops/klass.inline.hpp"
58 #include "oops/method.inline.hpp"
59 #include "oops/objArrayKlass.hpp"
60 #include "oops/objArrayOop.inline.hpp"
61 #include "oops/oop.inline.hpp"
62 #include "oops/oopCast.inline.hpp"
63 #include "oops/recordComponent.hpp"
64 #include "oops/symbol.hpp"
65 #include "oops/typeArrayOop.inline.hpp"
66 #include "prims/jvmtiExport.hpp"
67 #include "prims/methodHandles.hpp"
68 #include "prims/resolvedMethodTable.hpp"
69 #include "runtime/continuationEntry.inline.hpp"
70 #include "runtime/continuationJavaClasses.inline.hpp"
71 #include "runtime/fieldDescriptor.inline.hpp"
72 #include "runtime/frame.inline.hpp"
73 #include "runtime/handles.inline.hpp"
74 #include "runtime/init.hpp"
75 #include "runtime/interfaceSupport.inline.hpp"
76 #include "runtime/java.hpp"
77 #include "runtime/javaCalls.hpp"
78 #include "runtime/javaThread.hpp"
79 #include "runtime/jniHandles.inline.hpp"
80 #include "runtime/reflection.hpp"
81 #include "runtime/safepoint.hpp"
82 #include "runtime/safepointVerifiers.hpp"
83 #include "runtime/threadSMR.hpp"
84 #include "runtime/vframe.inline.hpp"
85 #include "runtime/vm_version.hpp"
86 #include "utilities/align.hpp"
87 #include "utilities/globalDefinitions.hpp"
88 #include "utilities/growableArray.hpp"
89 #include "utilities/preserveException.hpp"
90 #include "utilities/utf8.hpp"
91 #if INCLUDE_JVMCI
92 #include "jvmci/jvmciJavaClasses.hpp"
93 #endif
1886
1887 // Read thread status value from threadStatus field in java.lang.Thread java class.
1888 JavaThreadStatus java_lang_Thread::get_thread_status(oop java_thread) {
1889 // Make sure the caller is operating on behalf of the VM or is
1890 // running VM code (state == _thread_in_vm).
1891 assert(Threads_lock->owned_by_self() || Thread::current()->is_VM_thread() ||
1892 JavaThread::current()->thread_state() == _thread_in_vm ||
1893 JavaThread::current() == java_lang_Thread::thread(java_thread),
1894 "unsafe call to java_lang_Thread::get_thread_status()?");
1895 GET_FIELDHOLDER_FIELD(java_thread, get_thread_status, JavaThreadStatus::NEW /* not initialized */);
1896 }
1897
1898 ByteSize java_lang_Thread::thread_id_offset() {
1899 return in_ByteSize(_tid_offset);
1900 }
1901
1902 oop java_lang_Thread::park_blocker(oop java_thread) {
1903 return java_thread->obj_field_access<MO_RELAXED>(_park_blocker_offset);
1904 }
1905
1906 const char* java_lang_Thread::thread_status_name(oop java_thread) {
1907 JavaThreadStatus status = get_thread_status(java_thread);
1908 switch (status) {
1909 case JavaThreadStatus::NEW : return "NEW";
1910 case JavaThreadStatus::RUNNABLE : return "RUNNABLE";
1911 case JavaThreadStatus::SLEEPING : return "TIMED_WAITING (sleeping)";
1912 case JavaThreadStatus::IN_OBJECT_WAIT : return "WAITING (on object monitor)";
1913 case JavaThreadStatus::IN_OBJECT_WAIT_TIMED : return "TIMED_WAITING (on object monitor)";
1914 case JavaThreadStatus::PARKED : return "WAITING (parking)";
1915 case JavaThreadStatus::PARKED_TIMED : return "TIMED_WAITING (parking)";
1916 case JavaThreadStatus::BLOCKED_ON_MONITOR_ENTER : return "BLOCKED (on object monitor)";
1917 case JavaThreadStatus::TERMINATED : return "TERMINATED";
1918 default : return "UNKNOWN";
1919 };
1920 }
1921 int java_lang_ThreadGroup::_parent_offset;
1922 int java_lang_ThreadGroup::_name_offset;
1923 int java_lang_ThreadGroup::_maxPriority_offset;
1924 int java_lang_ThreadGroup::_daemon_offset;
1925
|