48 #include "logging/log.hpp"
49 #include "logging/logStream.hpp"
50 #include "memory/oopFactory.hpp"
51 #include "memory/resourceArea.hpp"
52 #include "memory/universe.hpp"
53 #include "oops/fieldInfo.hpp"
54 #include "oops/fieldStreams.inline.hpp"
55 #include "oops/instanceKlass.inline.hpp"
56 #include "oops/instanceMirrorKlass.hpp"
57 #include "oops/klass.hpp"
58 #include "oops/klass.inline.hpp"
59 #include "oops/method.inline.hpp"
60 #include "oops/objArrayKlass.hpp"
61 #include "oops/objArrayOop.inline.hpp"
62 #include "oops/oopCast.inline.hpp"
63 #include "oops/oop.inline.hpp"
64 #include "oops/symbol.hpp"
65 #include "oops/recordComponent.hpp"
66 #include "oops/typeArrayOop.inline.hpp"
67 #include "prims/jvmtiExport.hpp"
68 #include "prims/methodHandles.hpp"
69 #include "prims/resolvedMethodTable.hpp"
70 #include "runtime/continuationEntry.inline.hpp"
71 #include "runtime/continuationJavaClasses.inline.hpp"
72 #include "runtime/fieldDescriptor.inline.hpp"
73 #include "runtime/frame.inline.hpp"
74 #include "runtime/handles.inline.hpp"
75 #include "runtime/handshake.hpp"
76 #include "runtime/init.hpp"
77 #include "runtime/interfaceSupport.inline.hpp"
78 #include "runtime/java.hpp"
79 #include "runtime/javaCalls.hpp"
80 #include "runtime/javaThread.hpp"
81 #include "runtime/jniHandles.inline.hpp"
82 #include "runtime/reflectionUtils.hpp"
83 #include "runtime/safepoint.hpp"
84 #include "runtime/safepointVerifiers.hpp"
85 #include "runtime/threadSMR.hpp"
86 #include "runtime/vframe.inline.hpp"
87 #include "runtime/vm_version.hpp"
88 #include "utilities/align.hpp"
89 #include "utilities/globalDefinitions.hpp"
90 #include "utilities/growableArray.hpp"
91 #include "utilities/preserveException.hpp"
92 #include "utilities/utf8.hpp"
93 #if INCLUDE_JVMCI
94 #include "jvmci/jvmciJavaClasses.hpp"
95 #endif
96
97 #define DECLARE_INJECTED_FIELD(klass, name, signature, may_be_java) \
98 { VM_CLASS_ID(klass), VM_SYMBOL_ENUM_NAME(name##_name), VM_SYMBOL_ENUM_NAME(signature), may_be_java },
99
100 InjectedField JavaClasses::_injected_fields[] = {
101 ALL_INJECTED_FIELDS(DECLARE_INJECTED_FIELD)
102 };
103
104 // Register native methods of Object
1857 }
1858
1859 // Read thread status value from threadStatus field in java.lang.Thread java class.
1860 JavaThreadStatus java_lang_Thread::get_thread_status(oop java_thread) {
1861 // Make sure the caller is operating on behalf of the VM or is
1862 // running VM code (state == _thread_in_vm).
1863 assert(Threads_lock->owned_by_self() || Thread::current()->is_VM_thread() ||
1864 JavaThread::current()->thread_state() == _thread_in_vm,
1865 "Java Thread is not running in vm");
1866 GET_FIELDHOLDER_FIELD(java_thread, get_thread_status, JavaThreadStatus::NEW /* not initialized */);
1867 }
1868
1869 ByteSize java_lang_Thread::thread_id_offset() {
1870 return in_ByteSize(_tid_offset);
1871 }
1872
1873 oop java_lang_Thread::park_blocker(oop java_thread) {
1874 return java_thread->obj_field(_park_blocker_offset);
1875 }
1876
1877 oop java_lang_Thread::async_get_stack_trace(oop java_thread, TRAPS) {
1878 ThreadsListHandle tlh(JavaThread::current());
1879 JavaThread* thread;
1880 bool is_virtual = java_lang_VirtualThread::is_instance(java_thread);
1881 if (is_virtual) {
1882 oop carrier_thread = java_lang_VirtualThread::carrier_thread(java_thread);
1883 if (carrier_thread == nullptr) {
1884 return nullptr;
1885 }
1886 thread = java_lang_Thread::thread(carrier_thread);
1887 } else {
1888 thread = java_lang_Thread::thread(java_thread);
1889 }
1890 if (thread == nullptr) {
1891 return nullptr;
1892 }
1893
1894 class GetStackTraceClosure : public HandshakeClosure {
1895 public:
1896 const Handle _java_thread;
1981
1982 // Convert to StackTraceElement array
1983 InstanceKlass* k = vmClasses::StackTraceElement_klass();
1984 assert(k != nullptr, "must be loaded in 1.4+");
1985 if (k->should_be_initialized()) {
1986 k->initialize(CHECK_NULL);
1987 }
1988 objArrayHandle trace = oopFactory::new_objArray_handle(k, gstc._depth, CHECK_NULL);
1989
1990 for (int i = 0; i < gstc._depth; i++) {
1991 methodHandle method(THREAD, gstc._methods->at(i));
1992 oop element = java_lang_StackTraceElement::create(method,
1993 gstc._bcis->at(i),
1994 CHECK_NULL);
1995 trace->obj_at_put(i, element);
1996 }
1997
1998 return trace();
1999 }
2000
2001 const char* java_lang_Thread::thread_status_name(oop java_thread) {
2002 JavaThreadStatus status = get_thread_status(java_thread);
2003 switch (status) {
2004 case JavaThreadStatus::NEW : return "NEW";
2005 case JavaThreadStatus::RUNNABLE : return "RUNNABLE";
2006 case JavaThreadStatus::SLEEPING : return "TIMED_WAITING (sleeping)";
2007 case JavaThreadStatus::IN_OBJECT_WAIT : return "WAITING (on object monitor)";
2008 case JavaThreadStatus::IN_OBJECT_WAIT_TIMED : return "TIMED_WAITING (on object monitor)";
2009 case JavaThreadStatus::PARKED : return "WAITING (parking)";
2010 case JavaThreadStatus::PARKED_TIMED : return "TIMED_WAITING (parking)";
2011 case JavaThreadStatus::BLOCKED_ON_MONITOR_ENTER : return "BLOCKED (on object monitor)";
2012 case JavaThreadStatus::TERMINATED : return "TERMINATED";
2013 default : return "UNKNOWN";
2014 };
2015 }
2016 int java_lang_ThreadGroup::_parent_offset;
2017 int java_lang_ThreadGroup::_name_offset;
2018 int java_lang_ThreadGroup::_maxPriority_offset;
2019 int java_lang_ThreadGroup::_daemon_offset;
2020
2056 InstanceKlass* k = vmClasses::ThreadGroup_klass();
2057 THREADGROUP_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2058 }
2059
2060 #if INCLUDE_CDS
2061 void java_lang_ThreadGroup::serialize_offsets(SerializeClosure* f) {
2062 THREADGROUP_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2063 }
2064 #endif
2065
2066
2067 // java_lang_VirtualThread
2068
2069 int java_lang_VirtualThread::static_vthread_scope_offset;
2070 int java_lang_VirtualThread::_carrierThread_offset;
2071 int java_lang_VirtualThread::_continuation_offset;
2072 int java_lang_VirtualThread::_state_offset;
2073 int java_lang_VirtualThread::_next_offset;
2074 int java_lang_VirtualThread::_onWaitingList_offset;
2075 int java_lang_VirtualThread::_notified_offset;
2076 int java_lang_VirtualThread::_timeout_offset;
2077 int java_lang_VirtualThread::_objectWaiter_offset;
2078
2079 #define VTHREAD_FIELDS_DO(macro) \
2080 macro(static_vthread_scope_offset, k, "VTHREAD_SCOPE", continuationscope_signature, true); \
2081 macro(_carrierThread_offset, k, "carrierThread", thread_signature, false); \
2082 macro(_continuation_offset, k, "cont", continuation_signature, false); \
2083 macro(_state_offset, k, "state", int_signature, false); \
2084 macro(_next_offset, k, "next", vthread_signature, false); \
2085 macro(_onWaitingList_offset, k, "onWaitingList", bool_signature, false); \
2086 macro(_notified_offset, k, "notified", bool_signature, false); \
2087 macro(_timeout_offset, k, "timeout", long_signature, false);
2088
2089
2090 void java_lang_VirtualThread::compute_offsets() {
2091 InstanceKlass* k = vmClasses::VirtualThread_klass();
2092 VTHREAD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2093 VTHREAD_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
2094 }
2095
2096 bool java_lang_VirtualThread::is_instance(oop obj) {
2097 return obj != nullptr && is_subclass(obj->klass());
2098 }
2099
2100 oop java_lang_VirtualThread::carrier_thread(oop vthread) {
2101 oop thread = vthread->obj_field(_carrierThread_offset);
2102 return thread;
2103 }
2104
2105 oop java_lang_VirtualThread::continuation(oop vthread) {
2106 oop cont = vthread->obj_field(_continuation_offset);
2136 bool java_lang_VirtualThread::set_onWaitingList(oop vthread, OopHandle& list_head) {
2137 jboolean* addr = vthread->field_addr<jboolean>(_onWaitingList_offset);
2138 jboolean vthread_on_list = Atomic::load(addr);
2139 if (!vthread_on_list) {
2140 vthread_on_list = Atomic::cmpxchg(addr, (jboolean)JNI_FALSE, (jboolean)JNI_TRUE);
2141 if (!vthread_on_list) {
2142 for (;;) {
2143 oop head = list_head.resolve();
2144 java_lang_VirtualThread::set_next(vthread, head);
2145 if (list_head.cmpxchg(head, vthread) == head) return true;
2146 }
2147 }
2148 }
2149 return false; // already on waiting list
2150 }
2151
2152 void java_lang_VirtualThread::set_notified(oop vthread, jboolean value) {
2153 vthread->bool_field_put_volatile(_notified_offset, value);
2154 }
2155
2156 jlong java_lang_VirtualThread::timeout(oop vthread) {
2157 return vthread->long_field(_timeout_offset);
2158 }
2159
2160 void java_lang_VirtualThread::set_timeout(oop vthread, jlong value) {
2161 vthread->long_field_put(_timeout_offset, value);
2162 }
2163
2164 JavaThreadStatus java_lang_VirtualThread::map_state_to_thread_status(int state) {
2165 JavaThreadStatus status = JavaThreadStatus::NEW;
2166 switch (state & ~SUSPENDED) {
2167 case NEW:
2168 status = JavaThreadStatus::NEW;
2169 break;
2170 case STARTED:
2171 case RUNNING:
2172 case PARKING:
2173 case TIMED_PARKING:
2174 case UNPARKED:
2175 case YIELDING:
3350 LIVESTACKFRAMEINFO_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3351 }
3352 #endif
3353
3354 void java_lang_LiveStackFrameInfo::set_monitors(oop obj, oop value) {
3355 obj->obj_field_put(_monitors_offset, value);
3356 }
3357
3358 void java_lang_LiveStackFrameInfo::set_locals(oop obj, oop value) {
3359 obj->obj_field_put(_locals_offset, value);
3360 }
3361
3362 void java_lang_LiveStackFrameInfo::set_operands(oop obj, oop value) {
3363 obj->obj_field_put(_operands_offset, value);
3364 }
3365
3366 void java_lang_LiveStackFrameInfo::set_mode(oop obj, int value) {
3367 obj->int_field_put(_mode_offset, value);
3368 }
3369
3370
3371 // java_lang_AccessibleObject
3372
3373 int java_lang_reflect_AccessibleObject::_override_offset;
3374
3375 #define ACCESSIBLEOBJECT_FIELDS_DO(macro) \
3376 macro(_override_offset, k, "override", bool_signature, false)
3377
3378 void java_lang_reflect_AccessibleObject::compute_offsets() {
3379 InstanceKlass* k = vmClasses::reflect_AccessibleObject_klass();
3380 ACCESSIBLEOBJECT_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3381 }
3382
3383 #if INCLUDE_CDS
3384 void java_lang_reflect_AccessibleObject::serialize_offsets(SerializeClosure* f) {
3385 ACCESSIBLEOBJECT_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3386 }
3387 #endif
3388
3389 jboolean java_lang_reflect_AccessibleObject::override(oop reflect) {
3390 return (jboolean) reflect->bool_field(_override_offset);
|
48 #include "logging/log.hpp"
49 #include "logging/logStream.hpp"
50 #include "memory/oopFactory.hpp"
51 #include "memory/resourceArea.hpp"
52 #include "memory/universe.hpp"
53 #include "oops/fieldInfo.hpp"
54 #include "oops/fieldStreams.inline.hpp"
55 #include "oops/instanceKlass.inline.hpp"
56 #include "oops/instanceMirrorKlass.hpp"
57 #include "oops/klass.hpp"
58 #include "oops/klass.inline.hpp"
59 #include "oops/method.inline.hpp"
60 #include "oops/objArrayKlass.hpp"
61 #include "oops/objArrayOop.inline.hpp"
62 #include "oops/oopCast.inline.hpp"
63 #include "oops/oop.inline.hpp"
64 #include "oops/symbol.hpp"
65 #include "oops/recordComponent.hpp"
66 #include "oops/typeArrayOop.inline.hpp"
67 #include "prims/jvmtiExport.hpp"
68 #include "prims/jvmtiThreadState.hpp" // for JvmtiVTMSTransitionDisabler
69 #include "prims/methodHandles.hpp"
70 #include "prims/resolvedMethodTable.hpp"
71 #include "runtime/continuationEntry.inline.hpp"
72 #include "runtime/continuationJavaClasses.inline.hpp"
73 #include "runtime/fieldDescriptor.inline.hpp"
74 #include "runtime/frame.inline.hpp"
75 #include "runtime/handles.inline.hpp"
76 #include "runtime/handshake.hpp"
77 #include "runtime/init.hpp"
78 #include "runtime/interfaceSupport.inline.hpp"
79 #include "runtime/java.hpp"
80 #include "runtime/javaCalls.hpp"
81 #include "runtime/javaThread.hpp"
82 #include "runtime/jniHandles.inline.hpp"
83 #include "runtime/reflectionUtils.hpp"
84 #include "runtime/safepoint.hpp"
85 #include "runtime/safepointVerifiers.hpp"
86 #include "runtime/synchronizer.inline.hpp"
87 #include "runtime/threadSMR.hpp"
88 #include "runtime/vframe.inline.hpp"
89 #include "runtime/vm_version.hpp"
90 #include "utilities/align.hpp"
91 #include "utilities/globalDefinitions.hpp"
92 #include "utilities/growableArray.hpp"
93 #include "utilities/preserveException.hpp"
94 #include "utilities/utf8.hpp"
95 #if INCLUDE_JVMCI
96 #include "jvmci/jvmciJavaClasses.hpp"
97 #endif
98
99 #define DECLARE_INJECTED_FIELD(klass, name, signature, may_be_java) \
100 { VM_CLASS_ID(klass), VM_SYMBOL_ENUM_NAME(name##_name), VM_SYMBOL_ENUM_NAME(signature), may_be_java },
101
102 InjectedField JavaClasses::_injected_fields[] = {
103 ALL_INJECTED_FIELDS(DECLARE_INJECTED_FIELD)
104 };
105
106 // Register native methods of Object
1859 }
1860
1861 // Read thread status value from threadStatus field in java.lang.Thread java class.
1862 JavaThreadStatus java_lang_Thread::get_thread_status(oop java_thread) {
1863 // Make sure the caller is operating on behalf of the VM or is
1864 // running VM code (state == _thread_in_vm).
1865 assert(Threads_lock->owned_by_self() || Thread::current()->is_VM_thread() ||
1866 JavaThread::current()->thread_state() == _thread_in_vm,
1867 "Java Thread is not running in vm");
1868 GET_FIELDHOLDER_FIELD(java_thread, get_thread_status, JavaThreadStatus::NEW /* not initialized */);
1869 }
1870
1871 ByteSize java_lang_Thread::thread_id_offset() {
1872 return in_ByteSize(_tid_offset);
1873 }
1874
1875 oop java_lang_Thread::park_blocker(oop java_thread) {
1876 return java_thread->obj_field(_park_blocker_offset);
1877 }
1878
1879
1880 struct LockInfo {
1881 enum { // should be synced with jdk.internal.vm.ThreadSnapshot.ThreadLock constants
1882 PARKING_TO_WAIT = 0,
1883 ELEMINATED_SCALAR_REPLACED = 1,
1884 ELEMINATED_MONITOR = 2,
1885 LOCKED = 3,
1886 WAITING_TO_LOCK = 4,
1887 WAITING_ON = 5,
1888 WAITING_TO_RELOCK = 6,
1889 OWNABLE_SYNCHRONIZER = 7,
1890 };
1891
1892 int _depth;
1893 int _type;
1894 oop _obj;
1895
1896 LockInfo(int depth, int type, oop obj) : _depth(depth), _type(type), _obj(obj) {}
1897 LockInfo() : _depth(0), _type(0), _obj(nullptr) {}
1898 };
1899
1900 class GetThreadSnapshotClosure : public HandshakeClosure {
1901 public:
1902 Handle _java_thread;
1903 JavaThread* _thread;
1904 int _depth;
1905 bool _retry_handshake;
1906 GrowableArray<Method*>* _methods;
1907 GrowableArray<int>* _bcis;
1908 JavaThreadStatus _thread_status;
1909 oop _name;
1910 bool _with_locks;
1911 bool _found_first_lock;
1912 GrowableArray<LockInfo>* _locks;
1913
1914 GetThreadSnapshotClosure(Handle java_thread, JavaThread* thread, bool with_locks) :
1915 HandshakeClosure("GetThreadSnapshotClosure"), _java_thread(java_thread), _thread(thread),
1916 _depth(0), _retry_handshake(false),
1917 _methods(nullptr), _bcis(nullptr),
1918 _thread_status(), _name(nullptr),
1919 _with_locks(with_locks), _found_first_lock(false), _locks(nullptr) { }
1920 virtual ~GetThreadSnapshotClosure() {
1921 delete _methods;
1922 delete _bcis;
1923 if (_locks != nullptr) {
1924 delete _locks;
1925 }
1926 }
1927
1928 bool read_reset_retry() {
1929 bool ret = _retry_handshake;
1930 // If we re-execute the handshake this method need to return false
1931 // when the handshake cannot be performed. (E.g. thread terminating)
1932 _retry_handshake = false;
1933 return ret;
1934 }
1935
1936 void detect_locks(javaVFrame* jvf, int depth) {
1937 Thread* current = Thread::current();
1938
1939 if (depth == 0 && !_found_first_lock) {
1940 // See javaVFrame::print_lock_info_on() for some other cases:
1941 // "waiting to re-lock in wait", "waiting on the Class initialization monitor".
1942
1943 // If this is the first frame and it is java.lang.Object.wait(...)
1944 // then print out the receiver.
1945 if (jvf->method()->name() == vmSymbols::wait_name() &&
1946 jvf->method()->method_holder()->name() == vmSymbols::java_lang_Object()) {
1947 StackValueCollection* locs = jvf->locals();
1948 if (locs->is_empty()) {
1949 _locks->push(LockInfo(depth, LockInfo::WAITING_ON, nullptr));
1950 } else {
1951 int type = LockInfo::WAITING_ON;
1952 StackValue* sv = locs->at(0);
1953 if (sv->type() == T_OBJECT) {
1954 Handle o = locs->at(0)->get_obj();
1955 if (_thread_status == JavaThreadStatus::BLOCKED_ON_MONITOR_ENTER) {
1956 type = LockInfo::WAITING_TO_RELOCK;;
1957 }
1958 _locks->push(LockInfo(depth, type, o()));
1959 }
1960 }
1961 _found_first_lock = true;
1962 } else {
1963 oop park_blocker = java_lang_Thread::park_blocker(_java_thread());
1964 if (park_blocker != nullptr) {
1965 _locks->push(LockInfo(depth, LockInfo::PARKING_TO_WAIT, park_blocker));
1966 _found_first_lock = true;
1967 }
1968 }
1969 }
1970
1971 GrowableArray<MonitorInfo*>* mons = jvf->monitors();
1972 if (!mons->is_empty()) {
1973 for (int index = (mons->length() - 1); index >= 0; index--) {
1974 MonitorInfo* monitor = mons->at(index);
1975 if (monitor->eliminated() && jvf->is_compiled_frame()) { // Eliminated in compiled code
1976 if (monitor->owner_is_scalar_replaced()) {
1977 Klass* k = java_lang_Class::as_Klass(monitor->owner_klass());
1978 _locks->push(LockInfo(depth, LockInfo::ELEMINATED_SCALAR_REPLACED, k->klass_holder()));
1979 } else {
1980 Handle obj(current, monitor->owner());
1981 if (obj() != nullptr) {
1982 _locks->push(LockInfo(depth, LockInfo::ELEMINATED_MONITOR, obj()));
1983 }
1984 }
1985 continue;
1986 }
1987 if (monitor->owner() != nullptr) {
1988 // the monitor is associated with an object, i.e., it is locked
1989 int type = LockInfo::LOCKED;
1990
1991 if (depth == 0 && !_found_first_lock) {
1992 ObjectMonitor* pending_moninor = java_lang_VirtualThread::is_instance(_java_thread())
1993 ? java_lang_VirtualThread::current_pending_monitor(_java_thread())
1994 : jvf->thread()->current_pending_monitor();
1995
1996 markWord mark = monitor->owner()->mark();
1997 // The first stage of async deflation does not affect any field
1998 // used by this comparison so the ObjectMonitor* is usable here.
1999 if (mark.has_monitor()) {
2000 ObjectMonitor* mon = ObjectSynchronizer::read_monitor(current, monitor->owner(), mark);
2001 if (// if the monitor is null we must be in the process of locking
2002 mon == nullptr ||
2003 // we have marked ourself as pending on this monitor
2004 mon == pending_moninor ||
2005 // we are not the owner of this monitor
2006 false/*!mon->is_entered(thread())*/) {
2007 type = LockInfo::WAITING_TO_LOCK;
2008 }
2009 }
2010 }
2011 _locks->push(LockInfo(depth, type, monitor->owner()));
2012
2013 _found_first_lock = true;
2014 }
2015 }
2016 }
2017 }
2018
2019 void do_thread(Thread* th) override {
2020 if (!Thread::current()->is_Java_thread()) {
2021 _retry_handshake = true;
2022 return;
2023 }
2024
2025 bool is_virtual = java_lang_VirtualThread::is_instance(_java_thread());
2026 if (_thread != nullptr) {
2027 if (is_virtual) {
2028 // mounted vthread, use carrier thread state
2029 oop carrier_thread = java_lang_VirtualThread::carrier_thread(_java_thread());
2030 _thread_status = java_lang_Thread::get_thread_status(carrier_thread);
2031 } else {
2032 _thread_status = java_lang_Thread::get_thread_status(_java_thread());
2033 }
2034 } else {
2035 // unmounted vthread
2036 int vt_state = java_lang_VirtualThread::state(_java_thread());
2037 _thread_status = java_lang_VirtualThread::map_state_to_thread_status(vt_state);
2038 }
2039 _name = nullptr; //java_lang_Thread::name(_java_thread());
2040
2041 if (_thread != nullptr && !_thread->has_last_Java_frame()) {
2042 // stack trace is empty
2043 return;
2044 }
2045
2046 bool carrier = false;
2047
2048 if (is_virtual) {
2049 if (_thread != nullptr) {
2050 // if (thread->vthread() != _java_thread()) // We might be inside a System.executeOnCarrierThread
2051 const ContinuationEntry* ce = _thread->vthread_continuation();
2052 if (ce == nullptr || ce->cont_oop(_thread) != java_lang_VirtualThread::continuation(_java_thread())) {
2053 // TODO: handle
2054 }
2055 }
2056 } else {
2057 carrier = (_thread->vthread_continuation() != nullptr);
2058 }
2059
2060 ResourceMark rm(Thread::current());
2061
2062 const int max_depth = MaxJavaStackTraceDepth;
2063 const bool skip_hidden = !ShowHiddenFrames;
2064
2065 // Pick minimum length that will cover most cases
2066 int init_length = 64;
2067 _methods = new (mtInternal) GrowableArray<Method*>(init_length, mtInternal);
2068 _bcis = new (mtInternal) GrowableArray<int>(init_length, mtInternal);
2069
2070 if (_with_locks) {
2071 _locks = new (mtInternal) GrowableArray<LockInfo>(init_length, mtInternal);
2072 }
2073 int total_count = 0;
2074
2075 vframeStream vfst(_thread != nullptr
2076 ? vframeStream(_thread, false, false, carrier)
2077 : vframeStream(java_lang_VirtualThread::continuation(_java_thread())));
2078
2079 for (;
2080 !vfst.at_end() && (max_depth == 0 || max_depth != total_count);
2081 vfst.next()) {
2082 if (_with_locks) {
2083 detect_locks(vfst.asJavaVFrame(), total_count);
2084 }
2085 if (skip_hidden && (vfst.method()->is_hidden() ||
2086 vfst.method()->is_continuation_enter_intrinsic())) {
2087 continue;
2088 }
2089 _methods->push(vfst.method());
2090 _bcis->push(vfst.bci());
2091 total_count++;
2092 }
2093
2094 _depth = total_count;
2095 }
2096 };
2097
2098 oop java_lang_Thread::async_get_stack_trace(oop java_thread, TRAPS) {
2099 ThreadsListHandle tlh(JavaThread::current());
2100 JavaThread* thread;
2101 bool is_virtual = java_lang_VirtualThread::is_instance(java_thread);
2102 if (is_virtual) {
2103 oop carrier_thread = java_lang_VirtualThread::carrier_thread(java_thread);
2104 if (carrier_thread == nullptr) {
2105 return nullptr;
2106 }
2107 thread = java_lang_Thread::thread(carrier_thread);
2108 } else {
2109 thread = java_lang_Thread::thread(java_thread);
2110 }
2111 if (thread == nullptr) {
2112 return nullptr;
2113 }
2114
2115 class GetStackTraceClosure : public HandshakeClosure {
2116 public:
2117 const Handle _java_thread;
2202
2203 // Convert to StackTraceElement array
2204 InstanceKlass* k = vmClasses::StackTraceElement_klass();
2205 assert(k != nullptr, "must be loaded in 1.4+");
2206 if (k->should_be_initialized()) {
2207 k->initialize(CHECK_NULL);
2208 }
2209 objArrayHandle trace = oopFactory::new_objArray_handle(k, gstc._depth, CHECK_NULL);
2210
2211 for (int i = 0; i < gstc._depth; i++) {
2212 methodHandle method(THREAD, gstc._methods->at(i));
2213 oop element = java_lang_StackTraceElement::create(method,
2214 gstc._bcis->at(i),
2215 CHECK_NULL);
2216 trace->obj_at_put(i, element);
2217 }
2218
2219 return trace();
2220 }
2221
2222 oop java_lang_Thread::get_thread_snapshot(jobject jthread, bool with_locks, TRAPS) {
2223 ThreadsListHandle tlh(JavaThread::current());
2224
2225 oop java_thread = JNIHandles::resolve(jthread);
2226
2227 // wrapper to auto delete JvmtiVTMSTransitionDisabler
2228 class TransitionDisabler {
2229 JvmtiVTMSTransitionDisabler* _transition_disabler;
2230 public:
2231 TransitionDisabler() : _transition_disabler(nullptr) {}
2232 ~TransitionDisabler() {
2233 if (_transition_disabler != nullptr) {
2234 delete _transition_disabler;
2235 }
2236 }
2237 void init(jobject jthread) {
2238 _transition_disabler = new (mtInternal) JvmtiVTMSTransitionDisabler(jthread);
2239 }
2240 } transition_disabler;
2241
2242 JavaThread* thread = nullptr;
2243 bool is_virtual = java_lang_VirtualThread::is_instance(java_thread);
2244
2245 if (is_virtual) {
2246 // 1st need to disable mount/unmount transitions
2247 transition_disabler.init(jthread);
2248
2249 oop carrier_thread = java_lang_VirtualThread::carrier_thread(java_thread);
2250 if (carrier_thread != nullptr) {
2251 thread = java_lang_Thread::thread(carrier_thread);
2252 } else {
2253 // TODO: need to "suspend" the VT like VirtualThread.getStackTrace does
2254 }
2255 } else {
2256 thread = java_lang_Thread::thread(java_thread);
2257 }
2258
2259 // Handshake with target
2260 ResourceMark rm(THREAD);
2261 HandleMark hm(THREAD);
2262 GetThreadSnapshotClosure cl(Handle(THREAD, java_thread), thread, with_locks);
2263 if (thread == nullptr) {
2264 // unmounted vthread, execute on the current thread
2265 cl.do_thread(nullptr);
2266 } else {
2267 do {
2268 Handshake::execute(&cl, &tlh, thread);
2269 } while (cl.read_reset_retry());
2270 }
2271
2272 // Convert to StackTraceElement array
2273 InstanceKlass* k = vmClasses::StackTraceElement_klass();
2274 assert(k != nullptr, "must be loaded in 1.4+");
2275 if (k->should_be_initialized()) {
2276 k->initialize(CHECK_NULL);
2277 }
2278
2279 objArrayHandle trace = oopFactory::new_objArray_handle(k, cl._depth, CHECK_NULL);
2280
2281 InstanceKlass* lock_klass = nullptr;
2282 if (with_locks) {
2283 Symbol* sym = vmSymbols::jdk_internal_vm_ThreadLock();
2284 Klass* k = SystemDictionary::resolve_or_fail(sym, true, CHECK_NULL);
2285 lock_klass = InstanceKlass::cast(k);
2286 }
2287
2288 int max_locks = cl._locks != nullptr ? cl._locks->length() : 0;
2289 for (int i = 0; i < cl._depth; i++) {
2290 methodHandle method(THREAD, cl._methods->at(i));
2291 oop element = java_lang_StackTraceElement::create(method, cl._bcis->at(i), CHECK_NULL);
2292 trace->obj_at_put(i, element);
2293 }
2294 // call static StackTraceElement[] of(StackTraceElement[] stackTrace)
2295 // to properly initialize STE.
2296 {
2297 JavaValue result(T_OBJECT);
2298 JavaCalls::call_static(&result,
2299 k, // vmClasses::StackTraceElement_klass()
2300 vmSymbols::java_lang_StackTraceElement_of_name(),
2301 vmSymbols::java_lang_StackTraceElement_of_signature(),
2302 trace,
2303 CHECK_NULL);
2304 // the method return the same trace object
2305 }
2306
2307 objArrayHandle locks;
2308 int lock_index = 0;
2309 if (with_locks && max_locks > 0) {
2310 locks = oopFactory::new_objArray_handle(lock_klass, max_locks, CHECK_NULL);
2311 for (int n = 0; n < max_locks; n++) {
2312 LockInfo* lock_info = cl._locks->adr_at(lock_index++);
2313 oop o = lock_info->_obj;
2314 Handle lock_object = Handle(THREAD, o);
2315
2316 // TODO: allocate and fill in the VM
2317 JavaCallArguments args;
2318 args.push_int(lock_info->_depth);
2319 args.push_int(lock_info->_type);
2320 args.push_oop(lock_object);
2321 Handle lock = JavaCalls::construct_new_instance(lock_klass,
2322 vmSymbols::jdk_internal_vm_ThreadLock_ctor_signature(), &args, CHECK_NULL);
2323
2324 locks->obj_at_put(n, lock());
2325 }
2326 }
2327
2328 Symbol* snapshot_name = vmSymbols::jdk_internal_vm_ThreadSnapshot();
2329 Klass* snapshot_klass = SystemDictionary::resolve_or_fail(snapshot_name, true, CHECK_NULL);
2330 if (snapshot_klass->should_be_initialized()) {
2331 snapshot_klass->initialize(CHECK_NULL);
2332 }
2333
2334 // TODO: allocate and fill in the VM
2335 JavaCallArguments args;
2336 args.push_oop(trace);
2337 args.push_oop(locks);
2338 //args.push_oop(Handle(THREAD, cl._name));
2339 args.push_int((int)cl._thread_status);
2340 Handle snapshot = JavaCalls::construct_new_instance(InstanceKlass::cast(snapshot_klass),
2341 vmSymbols::jdk_internal_vm_ThreadSnapshot_ctor_signature(), &args, CHECK_NULL);
2342 return snapshot();
2343 }
2344
2345 const char* java_lang_Thread::thread_status_name(oop java_thread) {
2346 JavaThreadStatus status = get_thread_status(java_thread);
2347 switch (status) {
2348 case JavaThreadStatus::NEW : return "NEW";
2349 case JavaThreadStatus::RUNNABLE : return "RUNNABLE";
2350 case JavaThreadStatus::SLEEPING : return "TIMED_WAITING (sleeping)";
2351 case JavaThreadStatus::IN_OBJECT_WAIT : return "WAITING (on object monitor)";
2352 case JavaThreadStatus::IN_OBJECT_WAIT_TIMED : return "TIMED_WAITING (on object monitor)";
2353 case JavaThreadStatus::PARKED : return "WAITING (parking)";
2354 case JavaThreadStatus::PARKED_TIMED : return "TIMED_WAITING (parking)";
2355 case JavaThreadStatus::BLOCKED_ON_MONITOR_ENTER : return "BLOCKED (on object monitor)";
2356 case JavaThreadStatus::TERMINATED : return "TERMINATED";
2357 default : return "UNKNOWN";
2358 };
2359 }
2360 int java_lang_ThreadGroup::_parent_offset;
2361 int java_lang_ThreadGroup::_name_offset;
2362 int java_lang_ThreadGroup::_maxPriority_offset;
2363 int java_lang_ThreadGroup::_daemon_offset;
2364
2400 InstanceKlass* k = vmClasses::ThreadGroup_klass();
2401 THREADGROUP_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2402 }
2403
2404 #if INCLUDE_CDS
2405 void java_lang_ThreadGroup::serialize_offsets(SerializeClosure* f) {
2406 THREADGROUP_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2407 }
2408 #endif
2409
2410
2411 // java_lang_VirtualThread
2412
2413 int java_lang_VirtualThread::static_vthread_scope_offset;
2414 int java_lang_VirtualThread::_carrierThread_offset;
2415 int java_lang_VirtualThread::_continuation_offset;
2416 int java_lang_VirtualThread::_state_offset;
2417 int java_lang_VirtualThread::_next_offset;
2418 int java_lang_VirtualThread::_onWaitingList_offset;
2419 int java_lang_VirtualThread::_notified_offset;
2420 int java_lang_VirtualThread::_interruptible_wait_offset;
2421 int java_lang_VirtualThread::_timeout_offset;
2422 int java_lang_VirtualThread::_objectWaiter_offset;
2423
2424 #define VTHREAD_FIELDS_DO(macro) \
2425 macro(static_vthread_scope_offset, k, "VTHREAD_SCOPE", continuationscope_signature, true); \
2426 macro(_carrierThread_offset, k, "carrierThread", thread_signature, false); \
2427 macro(_continuation_offset, k, "cont", continuation_signature, false); \
2428 macro(_state_offset, k, "state", int_signature, false); \
2429 macro(_next_offset, k, "next", vthread_signature, false); \
2430 macro(_onWaitingList_offset, k, "onWaitingList", bool_signature, false); \
2431 macro(_notified_offset, k, "notified", bool_signature, false); \
2432 macro(_interruptible_wait_offset, k, "interruptableWait", bool_signature, false); \
2433 macro(_timeout_offset, k, "timeout", long_signature, false);
2434
2435
2436 void java_lang_VirtualThread::compute_offsets() {
2437 InstanceKlass* k = vmClasses::VirtualThread_klass();
2438 VTHREAD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2439 VTHREAD_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
2440 }
2441
2442 bool java_lang_VirtualThread::is_instance(oop obj) {
2443 return obj != nullptr && is_subclass(obj->klass());
2444 }
2445
2446 oop java_lang_VirtualThread::carrier_thread(oop vthread) {
2447 oop thread = vthread->obj_field(_carrierThread_offset);
2448 return thread;
2449 }
2450
2451 oop java_lang_VirtualThread::continuation(oop vthread) {
2452 oop cont = vthread->obj_field(_continuation_offset);
2482 bool java_lang_VirtualThread::set_onWaitingList(oop vthread, OopHandle& list_head) {
2483 jboolean* addr = vthread->field_addr<jboolean>(_onWaitingList_offset);
2484 jboolean vthread_on_list = Atomic::load(addr);
2485 if (!vthread_on_list) {
2486 vthread_on_list = Atomic::cmpxchg(addr, (jboolean)JNI_FALSE, (jboolean)JNI_TRUE);
2487 if (!vthread_on_list) {
2488 for (;;) {
2489 oop head = list_head.resolve();
2490 java_lang_VirtualThread::set_next(vthread, head);
2491 if (list_head.cmpxchg(head, vthread) == head) return true;
2492 }
2493 }
2494 }
2495 return false; // already on waiting list
2496 }
2497
2498 void java_lang_VirtualThread::set_notified(oop vthread, jboolean value) {
2499 vthread->bool_field_put_volatile(_notified_offset, value);
2500 }
2501
2502 void java_lang_VirtualThread::set_interruptible_wait(oop vthread, jboolean value) {
2503 vthread->bool_field_put_volatile(_interruptible_wait_offset, value);
2504 }
2505
2506 jlong java_lang_VirtualThread::timeout(oop vthread) {
2507 return vthread->long_field(_timeout_offset);
2508 }
2509
2510 void java_lang_VirtualThread::set_timeout(oop vthread, jlong value) {
2511 vthread->long_field_put(_timeout_offset, value);
2512 }
2513
2514 JavaThreadStatus java_lang_VirtualThread::map_state_to_thread_status(int state) {
2515 JavaThreadStatus status = JavaThreadStatus::NEW;
2516 switch (state & ~SUSPENDED) {
2517 case NEW:
2518 status = JavaThreadStatus::NEW;
2519 break;
2520 case STARTED:
2521 case RUNNING:
2522 case PARKING:
2523 case TIMED_PARKING:
2524 case UNPARKED:
2525 case YIELDING:
3700 LIVESTACKFRAMEINFO_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3701 }
3702 #endif
3703
3704 void java_lang_LiveStackFrameInfo::set_monitors(oop obj, oop value) {
3705 obj->obj_field_put(_monitors_offset, value);
3706 }
3707
3708 void java_lang_LiveStackFrameInfo::set_locals(oop obj, oop value) {
3709 obj->obj_field_put(_locals_offset, value);
3710 }
3711
3712 void java_lang_LiveStackFrameInfo::set_operands(oop obj, oop value) {
3713 obj->obj_field_put(_operands_offset, value);
3714 }
3715
3716 void java_lang_LiveStackFrameInfo::set_mode(oop obj, int value) {
3717 obj->int_field_put(_mode_offset, value);
3718 }
3719
3720 // java_lang_AccessibleObject
3721
3722 int java_lang_reflect_AccessibleObject::_override_offset;
3723
3724 #define ACCESSIBLEOBJECT_FIELDS_DO(macro) \
3725 macro(_override_offset, k, "override", bool_signature, false)
3726
3727 void java_lang_reflect_AccessibleObject::compute_offsets() {
3728 InstanceKlass* k = vmClasses::reflect_AccessibleObject_klass();
3729 ACCESSIBLEOBJECT_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3730 }
3731
3732 #if INCLUDE_CDS
3733 void java_lang_reflect_AccessibleObject::serialize_offsets(SerializeClosure* f) {
3734 ACCESSIBLEOBJECT_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3735 }
3736 #endif
3737
3738 jboolean java_lang_reflect_AccessibleObject::override(oop reflect) {
3739 return (jboolean) reflect->bool_field(_override_offset);
|