1 /*
2 * Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
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 "classfile/javaClasses.inline.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/heapInspection.hpp"
31 #include "memory/oopFactory.hpp"
32 #include "memory/resourceArea.hpp"
33 #include "memory/universe.hpp"
34 #include "nmt/memTag.hpp"
35 #include "oops/instanceKlass.hpp"
36 #include "oops/klass.inline.hpp"
37 #include "oops/method.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 "prims/jvmtiThreadState.hpp"
44 #include "runtime/atomicAccess.hpp"
45 #include "runtime/handles.inline.hpp"
46 #include "runtime/init.hpp"
47 #include "runtime/javaCalls.hpp"
48 #include "runtime/javaThread.inline.hpp"
49 #include "runtime/jniHandles.inline.hpp"
50 #include "runtime/objectMonitor.inline.hpp"
51 #include "runtime/synchronizer.hpp"
52 #include "runtime/thread.inline.hpp"
53 #include "runtime/threads.hpp"
54 #include "runtime/threadSMR.inline.hpp"
55 #include "runtime/vframe.inline.hpp"
56 #include "runtime/vmOperations.hpp"
57 #include "runtime/vmThread.hpp"
58 #include "services/threadService.hpp"
59
60 // TODO: we need to define a naming convention for perf counters
61 // to distinguish counters for:
62 // - standard JSR174 use
63 // - Hotspot extension (public and committed)
64 // - Hotspot extension (private/internal and uncommitted)
65
66 // Default is disabled.
67 bool ThreadService::_thread_monitoring_contention_enabled = false;
68 bool ThreadService::_thread_cpu_time_enabled = false;
69 bool ThreadService::_thread_allocated_memory_enabled = false;
70
71 PerfCounter* ThreadService::_total_threads_count = nullptr;
72 PerfVariable* ThreadService::_live_threads_count = nullptr;
73 PerfVariable* ThreadService::_peak_threads_count = nullptr;
74 PerfVariable* ThreadService::_daemon_threads_count = nullptr;
75 volatile int ThreadService::_atomic_threads_count = 0;
76 volatile int ThreadService::_atomic_daemon_threads_count = 0;
77
78 volatile jlong ThreadService::_exited_allocated_bytes = 0;
79
80 ThreadDumpResult* ThreadService::_threaddump_list = nullptr;
81
82 static const int INITIAL_ARRAY_SIZE = 10;
83
84 // OopStorage for thread stack sampling
85 static OopStorage* _thread_service_storage = nullptr;
86
87 void ThreadService::init() {
88 EXCEPTION_MARK;
89
90 // These counters are for java.lang.management API support.
91 // They are created even if -XX:-UsePerfData is set and in
92 // that case, they will be allocated on C heap.
93
94 _total_threads_count =
95 PerfDataManager::create_counter(JAVA_THREADS, "started",
96 PerfData::U_Events, CHECK);
97
98 _live_threads_count =
99 PerfDataManager::create_variable(JAVA_THREADS, "live",
100 PerfData::U_None, CHECK);
101
102 _peak_threads_count =
103 PerfDataManager::create_variable(JAVA_THREADS, "livePeak",
104 PerfData::U_None, CHECK);
105
106 _daemon_threads_count =
107 PerfDataManager::create_variable(JAVA_THREADS, "daemon",
108 PerfData::U_None, CHECK);
109
110 if (os::is_thread_cpu_time_supported()) {
111 _thread_cpu_time_enabled = true;
112 }
113
114 _thread_allocated_memory_enabled = true; // Always on, so enable it
115
116 // Initialize OopStorage for thread stack sampling walking
117 _thread_service_storage = OopStorageSet::create_strong("ThreadService OopStorage",
118 mtServiceability);
119 }
120
121 void ThreadService::reset_peak_thread_count() {
122 // Acquire the lock to update the peak thread count
123 // to synchronize with thread addition and removal.
124 MutexLocker mu(Threads_lock);
125 _peak_threads_count->set_value(get_live_thread_count());
126 }
127
128 static bool is_hidden_thread(JavaThread *thread) {
129 // hide VM internal or JVMTI agent threads
130 return thread->is_hidden_from_external_view() || thread->is_jvmti_agent_thread();
131 }
132
133 void ThreadService::add_thread(JavaThread* thread, bool daemon) {
134 assert(Threads_lock->owned_by_self(), "must have threads lock");
135
136 // Do not count hidden threads
137 if (is_hidden_thread(thread)) {
138 return;
139 }
140
141 _total_threads_count->inc();
142 _live_threads_count->inc();
143 AtomicAccess::inc(&_atomic_threads_count);
144 int count = _atomic_threads_count;
145
146 if (count > _peak_threads_count->get_value()) {
147 _peak_threads_count->set_value(count);
148 }
149
150 if (daemon) {
151 _daemon_threads_count->inc();
152 AtomicAccess::inc(&_atomic_daemon_threads_count);
153 }
154 }
155
156 void ThreadService::decrement_thread_counts(JavaThread* jt, bool daemon) {
157 AtomicAccess::dec(&_atomic_threads_count);
158
159 if (daemon) {
160 AtomicAccess::dec(&_atomic_daemon_threads_count);
161 }
162 }
163
164 void ThreadService::remove_thread(JavaThread* thread, bool daemon) {
165 assert(Threads_lock->owned_by_self(), "must have threads lock");
166
167 // Include hidden thread allcations in exited_allocated_bytes
168 ThreadService::incr_exited_allocated_bytes(thread->cooked_allocated_bytes());
169
170 // Do not count hidden threads
171 if (is_hidden_thread(thread)) {
172 return;
173 }
174
175 assert(!thread->is_terminated(), "must not be terminated");
176 if (!thread->is_exiting()) {
177 // We did not get here via JavaThread::exit() so current_thread_exiting()
178 // was not called, e.g., JavaThread::cleanup_failed_attach_current_thread().
179 decrement_thread_counts(thread, daemon);
180 }
181
182 int daemon_count = _atomic_daemon_threads_count;
183 int count = _atomic_threads_count;
184
185 // Counts are incremented at the same time, but atomic counts are
186 // decremented earlier than perf counts.
187 assert(_live_threads_count->get_value() > count,
188 "thread count mismatch %d : %d",
189 (int)_live_threads_count->get_value(), count);
190
191 _live_threads_count->dec(1);
192 if (daemon) {
193 assert(_daemon_threads_count->get_value() > daemon_count,
194 "thread count mismatch %d : %d",
195 (int)_daemon_threads_count->get_value(), daemon_count);
196
197 _daemon_threads_count->dec(1);
198 }
199
200 // Counts are incremented at the same time, but atomic counts are
201 // decremented earlier than perf counts.
202 assert(_daemon_threads_count->get_value() >= daemon_count,
203 "thread count mismatch %d : %d",
204 (int)_daemon_threads_count->get_value(), daemon_count);
205 assert(_live_threads_count->get_value() >= count,
206 "thread count mismatch %d : %d",
207 (int)_live_threads_count->get_value(), count);
208 assert(_live_threads_count->get_value() > 0 ||
209 (_live_threads_count->get_value() == 0 && count == 0 &&
210 _daemon_threads_count->get_value() == 0 && daemon_count == 0),
211 "thread counts should reach 0 at the same time, live %d,%d daemon %d,%d",
212 (int)_live_threads_count->get_value(), count,
213 (int)_daemon_threads_count->get_value(), daemon_count);
214 assert(_daemon_threads_count->get_value() > 0 ||
215 (_daemon_threads_count->get_value() == 0 && daemon_count == 0),
216 "thread counts should reach 0 at the same time, daemon %d,%d",
217 (int)_daemon_threads_count->get_value(), daemon_count);
218 }
219
220 void ThreadService::current_thread_exiting(JavaThread* jt, bool daemon) {
221 // Do not count hidden threads
222 if (is_hidden_thread(jt)) {
223 return;
224 }
225
226 assert(jt == JavaThread::current(), "Called by current thread");
227 assert(!jt->is_terminated() && jt->is_exiting(), "must be exiting");
228
229 decrement_thread_counts(jt, daemon);
230 }
231
232 // FIXME: JVMTI should call this function
233 Handle ThreadService::get_current_contended_monitor(JavaThread* thread) {
234 assert(thread != nullptr, "should be non-null");
235 DEBUG_ONLY(Thread::check_for_dangling_thread_pointer(thread);)
236
237 // This function can be called on a target JavaThread that is not
238 // the caller and we are not at a safepoint. So it is possible for
239 // the waiting or pending condition to be over/stale and for the
240 // first stage of async deflation to clear the object field in
241 // the ObjectMonitor. It is also possible for the object to be
242 // inflated again and to be associated with a completely different
243 // ObjectMonitor by the time this object reference is processed
244 // by the caller.
245 ObjectMonitor *wait_obj = thread->current_waiting_monitor();
246
247 oop obj = nullptr;
248 if (wait_obj != nullptr) {
249 // thread is doing an Object.wait() call
250 obj = wait_obj->object();
251 } else {
252 ObjectMonitor *enter_obj = thread->current_pending_monitor();
253 if (enter_obj != nullptr) {
254 // thread is trying to enter() an ObjectMonitor.
255 obj = enter_obj->object();
256 }
257 }
258
259 Handle h(Thread::current(), obj);
260 return h;
261 }
262
263 bool ThreadService::set_thread_monitoring_contention(bool flag) {
264 MutexLocker m(Management_lock);
265
266 bool prev = _thread_monitoring_contention_enabled;
267 _thread_monitoring_contention_enabled = flag;
268
269 return prev;
270 }
271
272 bool ThreadService::set_thread_cpu_time_enabled(bool flag) {
273 MutexLocker m(Management_lock);
274
275 bool prev = _thread_cpu_time_enabled;
276 _thread_cpu_time_enabled = flag;
277
278 return prev;
279 }
280
281 bool ThreadService::set_thread_allocated_memory_enabled(bool flag) {
282 MutexLocker m(Management_lock);
283
284 bool prev = _thread_allocated_memory_enabled;
285 _thread_allocated_memory_enabled = flag;
286
287 return prev;
288 }
289
290 void ThreadService::metadata_do(void f(Metadata*)) {
291 for (ThreadDumpResult* dump = _threaddump_list; dump != nullptr; dump = dump->next()) {
292 dump->metadata_do(f);
293 }
294 }
295
296 void ThreadService::add_thread_dump(ThreadDumpResult* dump) {
297 MutexLocker ml(Management_lock);
298 if (_threaddump_list == nullptr) {
299 _threaddump_list = dump;
300 } else {
301 dump->set_next(_threaddump_list);
302 _threaddump_list = dump;
303 }
304 }
305
306 void ThreadService::remove_thread_dump(ThreadDumpResult* dump) {
307 MutexLocker ml(Management_lock);
308
309 ThreadDumpResult* prev = nullptr;
310 bool found = false;
311 for (ThreadDumpResult* d = _threaddump_list; d != nullptr; prev = d, d = d->next()) {
312 if (d == dump) {
313 if (prev == nullptr) {
314 _threaddump_list = dump->next();
315 } else {
316 prev->set_next(dump->next());
317 }
318 found = true;
319 break;
320 }
321 }
322 assert(found, "The threaddump result to be removed must exist.");
323 }
324
325 // Dump stack trace of threads specified in the given threads array.
326 // Returns StackTraceElement[][] each element is the stack trace of a thread in
327 // the corresponding entry in the given threads array
328 Handle ThreadService::dump_stack_traces(GrowableArray<instanceHandle>* threads,
329 int num_threads,
330 TRAPS) {
331 assert(num_threads > 0, "just checking");
332
333 ThreadDumpResult dump_result;
334 VM_ThreadDump op(&dump_result,
335 threads,
336 num_threads,
337 -1, /* entire stack */
338 false, /* with locked monitors */
339 false /* with locked synchronizers */);
340 VMThread::execute(&op);
341
342 // Allocate the resulting StackTraceElement[][] object
343
344 ResourceMark rm(THREAD);
345 Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_StackTraceElement_array(), true, CHECK_NH);
346 ObjArrayKlass* ik = ObjArrayKlass::cast(k);
347 objArrayOop r = oopFactory::new_objArray(ik, num_threads, CHECK_NH);
348 objArrayHandle result_obj(THREAD, r);
349
350 int num_snapshots = dump_result.num_snapshots();
351 assert(num_snapshots == num_threads, "Must have num_threads thread snapshots");
352 assert(num_snapshots == 0 || dump_result.t_list_has_been_set(), "ThreadsList must have been set if we have a snapshot");
353 int i = 0;
354 for (ThreadSnapshot* ts = dump_result.snapshots(); ts != nullptr; i++, ts = ts->next()) {
355 ThreadStackTrace* stacktrace = ts->get_stack_trace();
356 if (stacktrace == nullptr) {
357 // No stack trace
358 result_obj->obj_at_put(i, nullptr);
359 } else {
360 // Construct an array of java/lang/StackTraceElement object
361 Handle backtrace_h = stacktrace->allocate_fill_stack_trace_element_array(CHECK_NH);
362 result_obj->obj_at_put(i, backtrace_h());
363 }
364 }
365
366 return result_obj;
367 }
368
369 void ThreadService::reset_contention_count_stat(JavaThread* thread) {
370 ThreadStatistics* stat = thread->get_thread_stat();
371 if (stat != nullptr) {
372 stat->reset_count_stat();
373 }
374 }
375
376 void ThreadService::reset_contention_time_stat(JavaThread* thread) {
377 ThreadStatistics* stat = thread->get_thread_stat();
378 if (stat != nullptr) {
379 stat->reset_time_stat();
380 }
381 }
382
383 bool ThreadService::is_virtual_or_carrier_thread(JavaThread* jt) {
384 oop threadObj = jt->threadObj();
385 if (threadObj != nullptr && threadObj->is_a(vmClasses::BaseVirtualThread_klass())) {
386 // a virtual thread backed by JavaThread
387 return true;
388 }
389 if (jt->is_vthread_mounted()) {
390 // carrier thread
391 return true;
392 }
393 return false;
394 }
395
396 // Find deadlocks involving raw monitors, object monitors and concurrent locks
397 // if concurrent_locks is true.
398 // We skip virtual thread carriers under the assumption that the current scheduler, ForkJoinPool,
399 // doesn't hold any locks while mounting a virtual thread, so any owned monitor (or j.u.c., lock for that matter)
400 // on that JavaThread must be owned by the virtual thread, and we don't support deadlock detection for virtual threads.
401 DeadlockCycle* ThreadService::find_deadlocks_at_safepoint(ThreadsList * t_list, bool concurrent_locks) {
402 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
403
404 // This code was modified from the original Threads::find_deadlocks code.
405 int globalDfn = 0, thisDfn;
406 ObjectMonitor* waitingToLockMonitor = nullptr;
407 JvmtiRawMonitor* waitingToLockRawMonitor = nullptr;
408 oop waitingToLockBlocker = nullptr;
409 bool blocked_on_monitor = false;
410 JavaThread *currentThread, *previousThread;
411 int num_deadlocks = 0;
412
413 // Initialize the depth-first-number for each JavaThread.
414 JavaThreadIterator jti(t_list);
415 for (JavaThread* jt = jti.first(); jt != nullptr; jt = jti.next()) {
416 if (!is_virtual_or_carrier_thread(jt)) {
417 jt->set_depth_first_number(-1);
418 }
419 }
420
421 DeadlockCycle* deadlocks = nullptr;
422 DeadlockCycle* last = nullptr;
423 DeadlockCycle* cycle = new DeadlockCycle();
424 for (JavaThread* jt = jti.first(); jt != nullptr; jt = jti.next()) {
425 if (is_virtual_or_carrier_thread(jt)) {
426 // skip virtual and carrier threads
427 continue;
428 }
429 if (jt->depth_first_number() >= 0) {
430 // this thread was already visited
431 continue;
432 }
433
434 thisDfn = globalDfn;
435 jt->set_depth_first_number(globalDfn++);
436 previousThread = jt;
437 currentThread = jt;
438
439 cycle->reset();
440
441 // The ObjectMonitor* can't be async deflated since we are at a safepoint.
442 // When there is a deadlock, all the monitors involved in the dependency
443 // cycle must be contended and heavyweight. So we only care about the
444 // heavyweight monitor a thread is waiting to lock.
445 waitingToLockMonitor = jt->current_pending_monitor();
446 // JVM TI raw monitors can also be involved in deadlocks, and we can be
447 // waiting to lock both a raw monitor and ObjectMonitor at the same time.
448 // It isn't clear how to make deadlock detection work correctly if that
449 // happens.
450 waitingToLockRawMonitor = jt->current_pending_raw_monitor();
451
452 if (concurrent_locks) {
453 waitingToLockBlocker = jt->current_park_blocker();
454 }
455
456 while (waitingToLockMonitor != nullptr ||
457 waitingToLockRawMonitor != nullptr ||
458 waitingToLockBlocker != nullptr) {
459 cycle->add_thread(currentThread);
460 // Give preference to the raw monitor
461 if (waitingToLockRawMonitor != nullptr) {
462 Thread* owner = waitingToLockRawMonitor->owner();
463 if (owner != nullptr && // the raw monitor could be released at any time
464 owner->is_Java_thread()) {
465 currentThread = JavaThread::cast(owner);
466 }
467 } else if (waitingToLockMonitor != nullptr) {
468 if (waitingToLockMonitor->has_owner()) {
469 currentThread = Threads::owning_thread_from_monitor(t_list, waitingToLockMonitor);
470 // If currentThread is null we would like to know if the owner
471 // is an unmounted vthread (no JavaThread*), because if it's not,
472 // it would mean the previous currentThread is blocked permanently
473 // and we should record this as a deadlock. Since there is currently
474 // no fast way to determine if the owner is indeed an unmounted
475 // vthread we never record this as a deadlock. Note: unless there
476 // is a bug in the VM, or a thread exits without releasing monitors
477 // acquired through JNI, null should imply an unmounted vthread owner.
478 }
479 } else {
480 if (concurrent_locks) {
481 if (waitingToLockBlocker->is_a(vmClasses::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass())) {
482 oop threadObj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(waitingToLockBlocker);
483 // This JavaThread (if there is one) is protected by the
484 // ThreadsListSetter in VM_FindDeadlocks::doit().
485 currentThread = threadObj != nullptr ? java_lang_Thread::thread(threadObj) : nullptr;
486 } else {
487 currentThread = nullptr;
488 }
489 }
490 }
491
492 if (currentThread == nullptr || is_virtual_or_carrier_thread(currentThread)) {
493 // No dependency on another thread
494 break;
495 }
496 if (currentThread->depth_first_number() < 0) {
497 // First visit to this thread
498 currentThread->set_depth_first_number(globalDfn++);
499 } else if (currentThread->depth_first_number() < thisDfn) {
500 // Thread already visited, and not on a (new) cycle
501 break;
502 } else if (currentThread == previousThread) {
503 // Self-loop, ignore
504 break;
505 } else {
506 // We have a (new) cycle
507 num_deadlocks++;
508
509 // add this cycle to the deadlocks list
510 if (deadlocks == nullptr) {
511 deadlocks = cycle;
512 } else {
513 last->set_next(cycle);
514 }
515 last = cycle;
516 cycle = new DeadlockCycle();
517 break;
518 }
519 previousThread = currentThread;
520 waitingToLockMonitor = (ObjectMonitor*)currentThread->current_pending_monitor();
521 if (concurrent_locks) {
522 waitingToLockBlocker = currentThread->current_park_blocker();
523 }
524 }
525
526 }
527 delete cycle;
528 return deadlocks;
529 }
530
531 ThreadDumpResult::ThreadDumpResult() : _num_threads(0), _num_snapshots(0), _snapshots(nullptr), _last(nullptr), _next(nullptr), _setter() {
532
533 // Create a new ThreadDumpResult object and append to the list.
534 // If GC happens before this function returns, Method*
535 // in the stack trace will be visited.
536 ThreadService::add_thread_dump(this);
537 }
538
539 ThreadDumpResult::ThreadDumpResult(int num_threads) : _num_threads(num_threads), _num_snapshots(0), _snapshots(nullptr), _last(nullptr), _next(nullptr), _setter() {
540 // Create a new ThreadDumpResult object and append to the list.
541 // If GC happens before this function returns, oops
542 // will be visited.
543 ThreadService::add_thread_dump(this);
544 }
545
546 ThreadDumpResult::~ThreadDumpResult() {
547 ThreadService::remove_thread_dump(this);
548
549 // free all the ThreadSnapshot objects created during
550 // the VM_ThreadDump operation
551 ThreadSnapshot* ts = _snapshots;
552 while (ts != nullptr) {
553 ThreadSnapshot* p = ts;
554 ts = ts->next();
555 delete p;
556 }
557 }
558
559 ThreadSnapshot* ThreadDumpResult::add_thread_snapshot() {
560 ThreadSnapshot* ts = new ThreadSnapshot();
561 link_thread_snapshot(ts);
562 return ts;
563 }
564
565 ThreadSnapshot* ThreadDumpResult::add_thread_snapshot(JavaThread* thread) {
566 ThreadSnapshot* ts = new ThreadSnapshot();
567 link_thread_snapshot(ts);
568 ts->initialize(t_list(), thread);
569 return ts;
570 }
571
572 void ThreadDumpResult::link_thread_snapshot(ThreadSnapshot* ts) {
573 assert(_num_threads == 0 || _num_snapshots < _num_threads,
574 "_num_snapshots must be less than _num_threads");
575 _num_snapshots++;
576 if (_snapshots == nullptr) {
577 _snapshots = ts;
578 } else {
579 _last->set_next(ts);
580 }
581 _last = ts;
582 }
583
584 void ThreadDumpResult::metadata_do(void f(Metadata*)) {
585 for (ThreadSnapshot* ts = _snapshots; ts != nullptr; ts = ts->next()) {
586 ts->metadata_do(f);
587 }
588 }
589
590 ThreadsList* ThreadDumpResult::t_list() {
591 return _setter.list();
592 }
593
594 StackFrameInfo::StackFrameInfo(javaVFrame* jvf, bool with_lock_info) {
595 _method = jvf->method();
596 _bci = jvf->bci();
597 _class_holder = OopHandle(_thread_service_storage, _method->method_holder()->klass_holder());
598 _locked_monitors = nullptr;
599 if (with_lock_info) {
600 Thread* current_thread = Thread::current();
601 ResourceMark rm(current_thread);
602 HandleMark hm(current_thread);
603 GrowableArray<MonitorInfo*>* list = jvf->locked_monitors();
604 int length = list->length();
605 if (length > 0) {
606 _locked_monitors = new (mtServiceability) GrowableArray<OopHandle>(length, mtServiceability);
607 for (int i = 0; i < length; i++) {
608 MonitorInfo* monitor = list->at(i);
609 assert(monitor->owner() != nullptr, "This monitor must have an owning object");
610 _locked_monitors->append(OopHandle(_thread_service_storage, monitor->owner()));
611 }
612 }
613 }
614 }
615
616 StackFrameInfo::~StackFrameInfo() {
617 if (_locked_monitors != nullptr) {
618 for (int i = 0; i < _locked_monitors->length(); i++) {
619 _locked_monitors->at(i).release(_thread_service_storage);
620 }
621 delete _locked_monitors;
622 }
623 _class_holder.release(_thread_service_storage);
624 }
625
626 void StackFrameInfo::metadata_do(void f(Metadata*)) {
627 f(_method);
628 }
629
630 void StackFrameInfo::print_on(outputStream* st) const {
631 ResourceMark rm;
632 java_lang_Throwable::print_stack_element(st, method(), bci());
633 int len = (_locked_monitors != nullptr ? _locked_monitors->length() : 0);
634 for (int i = 0; i < len; i++) {
635 oop o = _locked_monitors->at(i).resolve();
636 st->print_cr("\t- locked <" INTPTR_FORMAT "> (a %s)", p2i(o), o->klass()->external_name());
637 }
638 }
639
640 // Iterate through monitor cache to find JNI locked monitors
641 class InflatedMonitorsClosure: public MonitorClosure {
642 private:
643 ThreadStackTrace* _stack_trace;
644 public:
645 InflatedMonitorsClosure(ThreadStackTrace* st) {
646 _stack_trace = st;
647 }
648 void do_monitor(ObjectMonitor* mid) {
649 oop object = mid->object();
650 if (!_stack_trace->is_owned_monitor_on_stack(object)) {
651 _stack_trace->add_jni_locked_monitor(object);
652 }
653 }
654 };
655
656 ThreadStackTrace::ThreadStackTrace(JavaThread* t, bool with_locked_monitors) {
657 _thread = t;
658 _frames = new (mtServiceability) GrowableArray<StackFrameInfo*>(INITIAL_ARRAY_SIZE, mtServiceability);
659 _depth = 0;
660 _with_locked_monitors = with_locked_monitors;
661 if (_with_locked_monitors) {
662 _jni_locked_monitors = new (mtServiceability) GrowableArray<OopHandle>(INITIAL_ARRAY_SIZE, mtServiceability);
663 } else {
664 _jni_locked_monitors = nullptr;
665 }
666 }
667
668 void ThreadStackTrace::add_jni_locked_monitor(oop object) {
669 _jni_locked_monitors->append(OopHandle(_thread_service_storage, object));
670 }
671
672 ThreadStackTrace::~ThreadStackTrace() {
673 for (int i = 0; i < _frames->length(); i++) {
674 delete _frames->at(i);
675 }
676 delete _frames;
677 if (_jni_locked_monitors != nullptr) {
678 for (int i = 0; i < _jni_locked_monitors->length(); i++) {
679 _jni_locked_monitors->at(i).release(_thread_service_storage);
680 }
681 delete _jni_locked_monitors;
682 }
683 }
684
685 void ThreadStackTrace::dump_stack_at_safepoint(int maxDepth, ObjectMonitorsView* monitors, bool full) {
686 assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
687
688 if (_thread->has_last_Java_frame()) {
689 RegisterMap reg_map(_thread,
690 RegisterMap::UpdateMap::include,
691 RegisterMap::ProcessFrames::include,
692 RegisterMap::WalkContinuation::skip);
693 ResourceMark rm(VMThread::vm_thread());
694 // If full, we want to print both vthread and carrier frames
695 vframe* start_vf = !full && _thread->is_vthread_mounted()
696 ? _thread->carrier_last_java_vframe(®_map)
697 : _thread->last_java_vframe(®_map);
698 int count = 0;
699 for (vframe* f = start_vf; f; f = f->sender() ) {
700 if (maxDepth >= 0 && count == maxDepth) {
701 // Skip frames if more than maxDepth
702 break;
703 }
704 if (!full && f->is_vthread_entry()) {
705 break;
706 }
707 if (f->is_java_frame()) {
708 javaVFrame* jvf = javaVFrame::cast(f);
709 add_stack_frame(jvf);
710 count++;
711 } else {
712 // Ignore non-Java frames
713 }
714 }
715 }
716
717 if (_with_locked_monitors) {
718 // Iterate inflated monitors and find monitors locked by this thread
719 // that are not found in the stack, e.g. JNI locked monitors:
720 InflatedMonitorsClosure imc(this);
721 monitors->visit(&imc, _thread);
722 }
723 }
724
725
726 bool ThreadStackTrace::is_owned_monitor_on_stack(oop object) {
727 assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
728
729 bool found = false;
730 int num_frames = get_stack_depth();
731 for (int depth = 0; depth < num_frames; depth++) {
732 StackFrameInfo* frame = stack_frame_at(depth);
733 int len = frame->num_locked_monitors();
734 GrowableArray<OopHandle>* locked_monitors = frame->locked_monitors();
735 for (int j = 0; j < len; j++) {
736 oop monitor = locked_monitors->at(j).resolve();
737 assert(monitor != nullptr, "must be a Java object");
738 if (monitor == object) {
739 found = true;
740 break;
741 }
742 }
743 }
744 return found;
745 }
746
747 Handle ThreadStackTrace::allocate_fill_stack_trace_element_array(TRAPS) {
748 InstanceKlass* ik = vmClasses::StackTraceElement_klass();
749 assert(ik != nullptr, "must be loaded in 1.4+");
750
751 // Allocate an array of java/lang/StackTraceElement object
752 objArrayOop ste = oopFactory::new_objArray(ik, _depth, CHECK_NH);
753 objArrayHandle backtrace(THREAD, ste);
754 for (int j = 0; j < _depth; j++) {
755 StackFrameInfo* frame = _frames->at(j);
756 methodHandle mh(THREAD, frame->method());
757 oop element = java_lang_StackTraceElement::create(mh, frame->bci(), CHECK_NH);
758 backtrace->obj_at_put(j, element);
759 }
760 return backtrace;
761 }
762
763 void ThreadStackTrace::add_stack_frame(javaVFrame* jvf) {
764 StackFrameInfo* frame = new StackFrameInfo(jvf, _with_locked_monitors);
765 _frames->append(frame);
766 _depth++;
767 }
768
769 void ThreadStackTrace::metadata_do(void f(Metadata*)) {
770 int length = _frames->length();
771 for (int i = 0; i < length; i++) {
772 _frames->at(i)->metadata_do(f);
773 }
774 }
775
776
777 ConcurrentLocksDump::~ConcurrentLocksDump() {
778 if (_retain_map_on_free) {
779 return;
780 }
781
782 for (ThreadConcurrentLocks* t = _map; t != nullptr;) {
783 ThreadConcurrentLocks* tcl = t;
784 t = t->next();
785 delete tcl;
786 }
787 }
788
789 void ConcurrentLocksDump::dump_at_safepoint() {
790 // dump all locked concurrent locks
791 assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
792
793 GrowableArray<oop>* aos_objects = new (mtServiceability) GrowableArray<oop>(INITIAL_ARRAY_SIZE, mtServiceability);
794
795 // Find all instances of AbstractOwnableSynchronizer
796 HeapInspection::find_instances_at_safepoint(vmClasses::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass(),
797 aos_objects);
798 // Build a map of thread to its owned AQS locks
799 build_map(aos_objects);
800
801 delete aos_objects;
802 }
803
804
805 // build a map of JavaThread to all its owned AbstractOwnableSynchronizer
806 void ConcurrentLocksDump::build_map(GrowableArray<oop>* aos_objects) {
807 int length = aos_objects->length();
808 for (int i = 0; i < length; i++) {
809 oop o = aos_objects->at(i);
810 oop owner_thread_obj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(o);
811 if (owner_thread_obj != nullptr) {
812 // See comments in ThreadConcurrentLocks to see how this
813 // JavaThread* is protected.
814 JavaThread* thread = java_lang_Thread::thread(owner_thread_obj);
815 assert(o->is_instance(), "Must be an instanceOop");
816 add_lock(thread, (instanceOop) o);
817 }
818 }
819 }
820
821 void ConcurrentLocksDump::add_lock(JavaThread* thread, instanceOop o) {
822 ThreadConcurrentLocks* tcl = thread_concurrent_locks(thread);
823 if (tcl != nullptr) {
824 tcl->add_lock(o);
825 return;
826 }
827
828 // First owned lock found for this thread
829 tcl = new ThreadConcurrentLocks(thread);
830 tcl->add_lock(o);
831 if (_map == nullptr) {
832 _map = tcl;
833 } else {
834 _last->set_next(tcl);
835 }
836 _last = tcl;
837 }
838
839 ThreadConcurrentLocks* ConcurrentLocksDump::thread_concurrent_locks(JavaThread* thread) {
840 for (ThreadConcurrentLocks* tcl = _map; tcl != nullptr; tcl = tcl->next()) {
841 if (tcl->java_thread() == thread) {
842 return tcl;
843 }
844 }
845 return nullptr;
846 }
847
848 void ConcurrentLocksDump::print_locks_on(JavaThread* t, outputStream* st) {
849 st->print_cr(" Locked ownable synchronizers:");
850 ThreadConcurrentLocks* tcl = thread_concurrent_locks(t);
851 GrowableArray<OopHandle>* locks = (tcl != nullptr ? tcl->owned_locks() : nullptr);
852 if (locks == nullptr || locks->is_empty()) {
853 st->print_cr("\t- None");
854 st->cr();
855 return;
856 }
857
858 for (int i = 0; i < locks->length(); i++) {
859 oop obj = locks->at(i).resolve();
860 st->print_cr("\t- <" INTPTR_FORMAT "> (a %s)", p2i(obj), obj->klass()->external_name());
861 }
862 st->cr();
863 }
864
865 ThreadConcurrentLocks::ThreadConcurrentLocks(JavaThread* thread) {
866 _thread = thread;
867 _owned_locks = new (mtServiceability) GrowableArray<OopHandle>(INITIAL_ARRAY_SIZE, mtServiceability);
868 _next = nullptr;
869 }
870
871 ThreadConcurrentLocks::~ThreadConcurrentLocks() {
872 for (int i = 0; i < _owned_locks->length(); i++) {
873 _owned_locks->at(i).release(_thread_service_storage);
874 }
875 delete _owned_locks;
876 }
877
878 void ThreadConcurrentLocks::add_lock(instanceOop o) {
879 _owned_locks->append(OopHandle(_thread_service_storage, o));
880 }
881
882 ThreadStatistics::ThreadStatistics() {
883 _contended_enter_count = 0;
884 _monitor_wait_count = 0;
885 _sleep_count = 0;
886 _count_pending_reset = false;
887 _timer_pending_reset = false;
888 memset((void*) _perf_recursion_counts, 0, sizeof(_perf_recursion_counts));
889 }
890
891 oop ThreadSnapshot::threadObj() const { return _threadObj.resolve(); }
892
893 void ThreadSnapshot::initialize(ThreadsList * t_list, JavaThread* thread) {
894 _thread = thread;
895 oop threadObj = thread->threadObj();
896 _threadObj = OopHandle(_thread_service_storage, threadObj);
897
898 ThreadStatistics* stat = thread->get_thread_stat();
899 _contended_enter_ticks = stat->contended_enter_ticks();
900 _contended_enter_count = stat->contended_enter_count();
901 _monitor_wait_ticks = stat->monitor_wait_ticks();
902 _monitor_wait_count = stat->monitor_wait_count();
903 _sleep_ticks = stat->sleep_ticks();
904 _sleep_count = stat->sleep_count();
905
906 // If thread is still attaching then threadObj will be null.
907 _thread_status = threadObj == nullptr ? JavaThreadStatus::NEW
908 : java_lang_Thread::get_thread_status(threadObj);
909
910 _is_suspended = thread->is_suspended();
911 _is_in_native = (thread->thread_state() == _thread_in_native);
912
913 Handle obj = ThreadService::get_current_contended_monitor(thread);
914
915 oop blocker_object = nullptr;
916 oop blocker_object_owner = nullptr;
917
918 if (thread->is_vthread_mounted() && thread->vthread() != threadObj) { // ThreadSnapshot only captures platform threads
919 _thread_status = JavaThreadStatus::IN_OBJECT_WAIT;
920 oop vthread = thread->vthread();
921 assert(vthread != nullptr, "");
922 blocker_object = vthread;
923 blocker_object_owner = vthread;
924 } else if (_thread_status == JavaThreadStatus::BLOCKED_ON_MONITOR_ENTER ||
925 _thread_status == JavaThreadStatus::IN_OBJECT_WAIT ||
926 _thread_status == JavaThreadStatus::IN_OBJECT_WAIT_TIMED) {
927
928 if (obj() == nullptr) {
929 // monitor no longer exists; thread is not blocked
930 _thread_status = JavaThreadStatus::RUNNABLE;
931 } else {
932 blocker_object = obj();
933 JavaThread* owner = ObjectSynchronizer::get_lock_owner(t_list, obj);
934 if ((owner == nullptr && _thread_status == JavaThreadStatus::BLOCKED_ON_MONITOR_ENTER)
935 || (owner != nullptr && owner->is_attaching_via_jni())) {
936 // ownership information of the monitor is not available
937 // (may no longer be owned or releasing to some other thread)
938 // make this thread in RUNNABLE state.
939 // And when the owner thread is in attaching state, the java thread
940 // is not completely initialized. For example thread name and id
941 // and may not be set, so hide the attaching thread.
942 _thread_status = JavaThreadStatus::RUNNABLE;
943 blocker_object = nullptr;
944 } else if (owner != nullptr) {
945 blocker_object_owner = owner->threadObj();
946 }
947 }
948 } else if (_thread_status == JavaThreadStatus::PARKED || _thread_status == JavaThreadStatus::PARKED_TIMED) {
949 blocker_object = thread->current_park_blocker();
950 if (blocker_object != nullptr && blocker_object->is_a(vmClasses::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass())) {
951 blocker_object_owner = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(blocker_object);
952 }
953 }
954
955 if (blocker_object != nullptr) {
956 _blocker_object = OopHandle(_thread_service_storage, blocker_object);
957 }
958 if (blocker_object_owner != nullptr) {
959 _blocker_object_owner = OopHandle(_thread_service_storage, blocker_object_owner);
960 }
961 }
962
963 oop ThreadSnapshot::blocker_object() const { return _blocker_object.resolve(); }
964 oop ThreadSnapshot::blocker_object_owner() const { return _blocker_object_owner.resolve(); }
965
966 ThreadSnapshot::~ThreadSnapshot() {
967 _blocker_object.release(_thread_service_storage);
968 _blocker_object_owner.release(_thread_service_storage);
969 _threadObj.release(_thread_service_storage);
970
971 delete _stack_trace;
972 delete _concurrent_locks;
973 }
974
975 void ThreadSnapshot::dump_stack_at_safepoint(int max_depth, bool with_locked_monitors,
976 ObjectMonitorsView* monitors, bool full) {
977 _stack_trace = new ThreadStackTrace(_thread, with_locked_monitors);
978 _stack_trace->dump_stack_at_safepoint(max_depth, monitors, full);
979 }
980
981
982 void ThreadSnapshot::metadata_do(void f(Metadata*)) {
983 if (_stack_trace != nullptr) {
984 _stack_trace->metadata_do(f);
985 }
986 }
987
988
989 DeadlockCycle::DeadlockCycle() {
990 _threads = new (mtServiceability) GrowableArray<JavaThread*>(INITIAL_ARRAY_SIZE, mtServiceability);
991 _next = nullptr;
992 }
993
994 DeadlockCycle::~DeadlockCycle() {
995 delete _threads;
996 }
997
998 void DeadlockCycle::print_on_with(ThreadsList * t_list, outputStream* st) const {
999 st->cr();
1000 st->print_cr("Found one Java-level deadlock:");
1001 st->print("=============================");
1002
1003 JavaThread* currentThread;
1004 JvmtiRawMonitor* waitingToLockRawMonitor;
1005 oop waitingToLockBlocker;
1006 int len = _threads->length();
1007 for (int i = 0; i < len; i++) {
1008 currentThread = _threads->at(i);
1009 // The ObjectMonitor* can't be async deflated since we are at a safepoint.
1010 ObjectMonitor* waitingToLockMonitor = currentThread->current_pending_monitor();
1011 waitingToLockRawMonitor = currentThread->current_pending_raw_monitor();
1012 waitingToLockBlocker = currentThread->current_park_blocker();
1013 st->cr();
1014 st->print_cr("\"%s\":", currentThread->name());
1015 const char* owner_desc = ",\n which is held by";
1016
1017 // Note: As the JVM TI "monitor contended enter" event callback is executed after ObjectMonitor
1018 // sets the current pending monitor, it is possible to then see a pending raw monitor as well.
1019 if (waitingToLockRawMonitor != nullptr) {
1020 st->print(" waiting to lock JVM TI raw monitor " INTPTR_FORMAT, p2i(waitingToLockRawMonitor));
1021 Thread* owner = waitingToLockRawMonitor->owner();
1022 // Could be null as the raw monitor could be released at any time if held by non-JavaThread
1023 if (owner != nullptr) {
1024 if (owner->is_Java_thread()) {
1025 currentThread = JavaThread::cast(owner);
1026 st->print_cr("%s \"%s\"", owner_desc, currentThread->name());
1027 } else {
1028 st->print_cr(",\n which has now been released");
1029 }
1030 } else {
1031 st->print_cr("%s non-Java thread=" PTR_FORMAT, owner_desc, p2i(owner));
1032 }
1033 }
1034
1035 if (waitingToLockMonitor != nullptr) {
1036 st->print(" waiting to lock monitor " INTPTR_FORMAT, p2i(waitingToLockMonitor));
1037 oop obj = waitingToLockMonitor->object();
1038 st->print(" (object " INTPTR_FORMAT ", a %s)", p2i(obj),
1039 obj->klass()->external_name());
1040
1041 if (!currentThread->current_pending_monitor_is_from_java()) {
1042 owner_desc = "\n in JNI, which is held by";
1043 }
1044 currentThread = Threads::owning_thread_from_monitor(t_list, waitingToLockMonitor);
1045 if (currentThread == nullptr) {
1046 // The deadlock was detected at a safepoint so the JavaThread
1047 // that owns waitingToLockMonitor should be findable, but
1048 // if it is not findable, then the previous currentThread is
1049 // blocked permanently.
1050 st->print_cr("%s UNKNOWN_owner_addr=" INT64_FORMAT, owner_desc,
1051 waitingToLockMonitor->owner());
1052 continue;
1053 }
1054 } else {
1055 st->print(" waiting for ownable synchronizer " INTPTR_FORMAT ", (a %s)",
1056 p2i(waitingToLockBlocker),
1057 waitingToLockBlocker->klass()->external_name());
1058 assert(waitingToLockBlocker->is_a(vmClasses::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass()),
1059 "Must be an AbstractOwnableSynchronizer");
1060 oop ownerObj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(waitingToLockBlocker);
1061 currentThread = java_lang_Thread::thread(ownerObj);
1062 assert(currentThread != nullptr, "AbstractOwnableSynchronizer owning thread is unexpectedly null");
1063 }
1064 st->print_cr("%s \"%s\"", owner_desc, currentThread->name());
1065 }
1066
1067 st->cr();
1068
1069 // Print stack traces
1070 bool oldJavaMonitorsInStackTrace = JavaMonitorsInStackTrace;
1071 JavaMonitorsInStackTrace = true;
1072 st->print_cr("Java stack information for the threads listed above:");
1073 st->print_cr("===================================================");
1074 for (int j = 0; j < len; j++) {
1075 currentThread = _threads->at(j);
1076 st->print_cr("\"%s\":", currentThread->name());
1077 currentThread->print_stack_on(st);
1078 }
1079 JavaMonitorsInStackTrace = oldJavaMonitorsInStackTrace;
1080 }
1081
1082 ThreadsListEnumerator::ThreadsListEnumerator(Thread* cur_thread,
1083 bool include_jvmti_agent_threads,
1084 bool include_jni_attaching_threads,
1085 bool include_bound_virtual_threads) {
1086 assert(cur_thread == Thread::current(), "Check current thread");
1087
1088 int init_size = ThreadService::get_live_thread_count();
1089 _threads_array = new GrowableArray<instanceHandle>(init_size);
1090
1091 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
1092 // skips JavaThreads in the process of exiting
1093 // and also skips VM internal JavaThreads
1094 // Threads in _thread_new or _thread_new_trans state are included.
1095 // i.e. threads have been started but not yet running.
1096 if (jt->threadObj() == nullptr ||
1097 jt->is_exiting() ||
1098 !java_lang_Thread::is_alive(jt->threadObj()) ||
1099 jt->is_hidden_from_external_view()) {
1100 continue;
1101 }
1102
1103 // skip agent threads
1104 if (!include_jvmti_agent_threads && jt->is_jvmti_agent_thread()) {
1105 continue;
1106 }
1107
1108 // skip jni threads in the process of attaching
1109 if (!include_jni_attaching_threads && jt->is_attaching_via_jni()) {
1110 continue;
1111 }
1112
1113 // skip instances of BoundVirtualThread
1114 if (!include_bound_virtual_threads && jt->threadObj()->is_a(vmClasses::BoundVirtualThread_klass())) {
1115 continue;
1116 }
1117
1118 instanceHandle h(cur_thread, (instanceOop) jt->threadObj());
1119 _threads_array->append(h);
1120 }
1121 }
1122
1123
1124 // jdk.internal.vm.ThreadSnapshot support
1125 #if INCLUDE_JVMTI
1126
1127 class GetThreadSnapshotHandshakeClosure: public HandshakeClosure {
1128 private:
1129 static OopStorage* oop_storage() {
1130 assert(_thread_service_storage != nullptr, "sanity");
1131 return _thread_service_storage;
1132 }
1133
1134 public:
1135 struct OwnedLock {
1136 // should be synced with ordinals of jdk.internal.vm.ThreadSnapshot.OwnedLockType enum
1137 enum Type {
1138 NOTHING = -1,
1139 LOCKED = 0,
1140 ELIMINATED = 1,
1141 };
1142
1143 int _frame_depth;
1144 Type _type;
1145 // synchronization object (when type == LOCKED) or its klass (type == ELIMINATED)
1146 OopHandle _obj;
1147
1148 OwnedLock(int depth, Type type, OopHandle obj): _frame_depth(depth), _type(type), _obj(obj) {}
1149 OwnedLock(): _frame_depth(0), _type(NOTHING), _obj(nullptr) {}
1150 };
1151
1152 struct Blocker {
1153 // should be synced with ordinals of jdk.internal.vm.ThreadSnapshot.BlockerLockType enum
1154 enum Type {
1155 NOTHING = -1,
1156 PARK_BLOCKER = 0,
1157 WAITING_TO_LOCK = 1,
1158 WAITING_ON = 2,
1159 };
1160
1161 Type _type;
1162 // park blocker or an object the thread waiting on/trying to lock
1163 OopHandle _obj;
1164 // thread that owns park blocker object when park blocker is AbstractOwnableSynchronizer
1165 OopHandle _owner;
1166
1167 Blocker(Type type, OopHandle obj): _type(type), _obj(obj), _owner() {}
1168 Blocker(): _type(NOTHING), _obj(), _owner() {}
1169
1170 bool is_empty() const {
1171 return _type == NOTHING;
1172 }
1173 };
1174
1175 Handle _thread_h;
1176 JavaThread* _java_thread;
1177 int _frame_count; // length of _methods and _bcis arrays
1178 GrowableArray<Method*>* _methods;
1179 GrowableArray<int>* _bcis;
1180 JavaThreadStatus _thread_status;
1181 OopHandle _thread_name;
1182 OopHandle _carrier_thread;
1183 GrowableArray<OwnedLock>* _locks;
1184 Blocker _blocker;
1185
1186 GetThreadSnapshotHandshakeClosure(Handle thread_h):
1187 HandshakeClosure("GetThreadSnapshotHandshakeClosure"),
1188 _thread_h(thread_h), _java_thread(nullptr),
1189 _frame_count(0), _methods(nullptr), _bcis(nullptr),
1190 _thread_status(), _thread_name(nullptr),
1191 _locks(nullptr), _blocker() {
1192 }
1193 virtual ~GetThreadSnapshotHandshakeClosure() {
1194 delete _methods;
1195 delete _bcis;
1196 _thread_name.release(oop_storage());
1197 if (_locks != nullptr) {
1198 for (int i = 0; i < _locks->length(); i++) {
1199 _locks->at(i)._obj.release(oop_storage());
1200 }
1201 delete _locks;
1202 }
1203 _blocker._obj.release(oop_storage());
1204 _blocker._owner.release(oop_storage());
1205 }
1206
1207 private:
1208 void detect_locks(javaVFrame* jvf, int depth) {
1209 Thread* current = Thread::current();
1210
1211 if (depth == 0 && _blocker.is_empty()) {
1212 // If this is the first frame and it is java.lang.Object.wait(...)
1213 // then print out the receiver.
1214 if (jvf->method()->name() == vmSymbols::wait_name() &&
1215 jvf->method()->method_holder()->name() == vmSymbols::java_lang_Object()) {
1216 OopHandle lock_object;
1217 StackValueCollection* locs = jvf->locals();
1218 if (!locs->is_empty()) {
1219 StackValue* sv = locs->at(0);
1220 if (sv->type() == T_OBJECT) {
1221 Handle o = locs->at(0)->get_obj();
1222 lock_object = OopHandle(oop_storage(), o());
1223 }
1224 }
1225 _blocker = Blocker(Blocker::WAITING_ON, lock_object);
1226 }
1227 }
1228
1229 GrowableArray<MonitorInfo*>* mons = jvf->monitors();
1230 if (!mons->is_empty()) {
1231 for (int index = (mons->length() - 1); index >= 0; index--) {
1232 MonitorInfo* monitor = mons->at(index);
1233 if (monitor->eliminated() && jvf->is_compiled_frame()) { // Eliminated in compiled code
1234 if (monitor->owner_is_scalar_replaced()) {
1235 Klass* k = java_lang_Class::as_Klass(monitor->owner_klass());
1236 _locks->push(OwnedLock(depth, OwnedLock::ELIMINATED, OopHandle(oop_storage(), k->klass_holder())));
1237 } else {
1238 Handle owner(current, monitor->owner());
1239 if (owner.not_null()) {
1240 Klass* k = owner->klass();
1241 _locks->push(OwnedLock(depth, OwnedLock::ELIMINATED, OopHandle(oop_storage(), k->klass_holder())));
1242 }
1243 }
1244 continue;
1245 }
1246 if (monitor->owner() != nullptr) {
1247 // the monitor is associated with an object, i.e., it is locked
1248
1249 if (depth == 0 && _blocker.is_empty()) {
1250 ObjectMonitor* pending_moninor = java_lang_VirtualThread::is_instance(_thread_h())
1251 ? java_lang_VirtualThread::current_pending_monitor(_thread_h())
1252 : jvf->thread()->current_pending_monitor();
1253
1254 markWord mark = monitor->owner()->mark();
1255 // The first stage of async deflation does not affect any field
1256 // used by this comparison so the ObjectMonitor* is usable here.
1257 if (mark.has_monitor()) {
1258 ObjectMonitor* mon = ObjectSynchronizer::read_monitor(current, monitor->owner(), mark);
1259 if (// if the monitor is null we must be in the process of locking
1260 mon == nullptr ||
1261 // we have marked ourself as pending on this monitor
1262 mon == pending_moninor ||
1263 // we are not the owner of this monitor
1264 (_java_thread != nullptr && !mon->is_entered(_java_thread))) {
1265 _blocker = Blocker(Blocker::WAITING_TO_LOCK, OopHandle(oop_storage(), monitor->owner()));
1266 continue; // go to next monitor
1267 }
1268 }
1269 }
1270 _locks->push(OwnedLock(depth, OwnedLock::LOCKED, OopHandle(oop_storage(), monitor->owner())));
1271 }
1272 }
1273 }
1274 }
1275
1276 public:
1277 void do_thread(Thread* th) override {
1278 Thread* current = Thread::current();
1279 _java_thread = th != nullptr ? JavaThread::cast(th) : nullptr;
1280
1281 bool is_virtual = java_lang_VirtualThread::is_instance(_thread_h());
1282 if (_java_thread != nullptr) {
1283 if (is_virtual) {
1284 // mounted vthread, use carrier thread state
1285 _carrier_thread = OopHandle(oop_storage(), java_lang_VirtualThread::carrier_thread(_thread_h()));
1286 assert(_carrier_thread.resolve() == _java_thread->threadObj(), "");
1287 _thread_status = java_lang_Thread::get_thread_status(_carrier_thread.resolve());
1288 } else {
1289 _thread_status = java_lang_Thread::get_thread_status(_thread_h());
1290 }
1291 } else {
1292 // unmounted vthread
1293 int vt_state = java_lang_VirtualThread::state(_thread_h());
1294 _thread_status = java_lang_VirtualThread::map_state_to_thread_status(vt_state);
1295 }
1296 _thread_name = OopHandle(oop_storage(), java_lang_Thread::name(_thread_h()));
1297
1298 if (_java_thread != nullptr && !_java_thread->has_last_Java_frame()) {
1299 // stack trace is empty
1300 return;
1301 }
1302
1303 bool vthread_carrier = !is_virtual && (_java_thread != nullptr) && (_java_thread->vthread_continuation() != nullptr);
1304
1305 oop park_blocker = java_lang_Thread::park_blocker(_thread_h());
1306 if (park_blocker != nullptr) {
1307 _blocker = Blocker(Blocker::PARK_BLOCKER, OopHandle(oop_storage(), park_blocker));
1308 if (park_blocker->is_a(vmClasses::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass())) {
1309 // could be stale (unlikely in practice), but it's good enough to see deadlocks
1310 oop ownerObj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(park_blocker);
1311 if (ownerObj != nullptr) {
1312 _blocker._owner = OopHandle(oop_storage(), ownerObj);
1313 }
1314 }
1315 }
1316
1317 ResourceMark rm(current);
1318 HandleMark hm(current);
1319
1320 const int max_depth = MaxJavaStackTraceDepth;
1321 const bool skip_hidden = !ShowHiddenFrames;
1322
1323 // Pick minimum length that will cover most cases
1324 int init_length = 64;
1325 _methods = new (mtInternal) GrowableArray<Method*>(init_length, mtInternal);
1326 _bcis = new (mtInternal) GrowableArray<int>(init_length, mtInternal);
1327 _locks = new (mtInternal) GrowableArray<OwnedLock>(init_length, mtInternal);
1328 int total_count = 0;
1329
1330 vframeStream vfst(_java_thread != nullptr
1331 ? vframeStream(_java_thread, false, true, vthread_carrier)
1332 : vframeStream(java_lang_VirtualThread::continuation(_thread_h())));
1333
1334 for (;
1335 !vfst.at_end() && (max_depth == 0 || max_depth != total_count);
1336 vfst.next()) {
1337
1338 detect_locks(vfst.asJavaVFrame(), total_count);
1339
1340 if (skip_hidden && (vfst.method()->is_hidden() ||
1341 vfst.method()->is_continuation_enter_intrinsic())) {
1342 continue;
1343 }
1344 _methods->push(vfst.method());
1345 _bcis->push(vfst.bci());
1346 total_count++;
1347 }
1348
1349 _frame_count = total_count;
1350 }
1351 };
1352
1353 class jdk_internal_vm_ThreadLock: AllStatic {
1354 static bool _inited;
1355 static int _depth_offset;
1356 static int _typeOrdinal_offset;
1357 static int _obj_offset;
1358
1359 static void compute_offsets(InstanceKlass* klass, TRAPS) {
1360 JavaClasses::compute_offset(_depth_offset, klass, "depth", vmSymbols::int_signature(), false);
1361 JavaClasses::compute_offset(_typeOrdinal_offset, klass, "typeOrdinal", vmSymbols::int_signature(), false);
1362 JavaClasses::compute_offset(_obj_offset, klass, "obj", vmSymbols::object_signature(), false);
1363 }
1364 public:
1365 static void init(InstanceKlass* klass, TRAPS) {
1366 if (!_inited) {
1367 compute_offsets(klass, CHECK);
1368 _inited = true;
1369 }
1370 }
1371
1372 static Handle create(InstanceKlass* klass, int depth, int type_ordinal, OopHandle obj, TRAPS) {
1373 init(klass, CHECK_NH);
1374 Handle result = klass->allocate_instance_handle(CHECK_NH);
1375 result->int_field_put(_depth_offset, depth);
1376 result->int_field_put(_typeOrdinal_offset, type_ordinal);
1377 result->obj_field_put(_obj_offset, obj.resolve());
1378 return result;
1379 }
1380 };
1381
1382 bool jdk_internal_vm_ThreadLock::_inited = false;
1383 int jdk_internal_vm_ThreadLock::_depth_offset;
1384 int jdk_internal_vm_ThreadLock::_typeOrdinal_offset;
1385 int jdk_internal_vm_ThreadLock::_obj_offset;
1386
1387 class jdk_internal_vm_ThreadSnapshot: AllStatic {
1388 static bool _inited;
1389 static int _name_offset;
1390 static int _threadStatus_offset;
1391 static int _carrierThread_offset;
1392 static int _stackTrace_offset;
1393 static int _locks_offset;
1394 static int _blockerTypeOrdinal_offset;
1395 static int _blockerObject_offset;
1396 static int _parkBlockerOwner_offset;
1397
1398 static void compute_offsets(InstanceKlass* klass, TRAPS) {
1399 JavaClasses::compute_offset(_name_offset, klass, "name", vmSymbols::string_signature(), false);
1400 JavaClasses::compute_offset(_threadStatus_offset, klass, "threadStatus", vmSymbols::int_signature(), false);
1401 JavaClasses::compute_offset(_carrierThread_offset, klass, "carrierThread", vmSymbols::thread_signature(), false);
1402 JavaClasses::compute_offset(_stackTrace_offset, klass, "stackTrace", vmSymbols::java_lang_StackTraceElement_array(), false);
1403 JavaClasses::compute_offset(_locks_offset, klass, "locks", vmSymbols::jdk_internal_vm_ThreadLock_array(), false);
1404 JavaClasses::compute_offset(_blockerTypeOrdinal_offset, klass, "blockerTypeOrdinal", vmSymbols::int_signature(), false);
1405 JavaClasses::compute_offset(_blockerObject_offset, klass, "blockerObject", vmSymbols::object_signature(), false);
1406 JavaClasses::compute_offset(_parkBlockerOwner_offset, klass, "parkBlockerOwner", vmSymbols::thread_signature(), false);
1407 }
1408 public:
1409 static void init(InstanceKlass* klass, TRAPS) {
1410 if (!_inited) {
1411 compute_offsets(klass, CHECK);
1412 _inited = true;
1413 }
1414 }
1415
1416 static Handle allocate(InstanceKlass* klass, TRAPS) {
1417 init(klass, CHECK_NH);
1418 Handle h_k = klass->allocate_instance_handle(CHECK_NH);
1419 return h_k;
1420 }
1421
1422 static void set_name(oop snapshot, oop name) {
1423 snapshot->obj_field_put(_name_offset, name);
1424 }
1425 static void set_thread_status(oop snapshot, int status) {
1426 snapshot->int_field_put(_threadStatus_offset, status);
1427 }
1428 static void set_carrier_thread(oop snapshot, oop carrier_thread) {
1429 snapshot->obj_field_put(_carrierThread_offset, carrier_thread);
1430 }
1431 static void set_stack_trace(oop snapshot, oop trace) {
1432 snapshot->obj_field_put(_stackTrace_offset, trace);
1433 }
1434 static void set_locks(oop snapshot, oop locks) {
1435 snapshot->obj_field_put(_locks_offset, locks);
1436 }
1437 static void set_blocker(oop snapshot, int type_ordinal, oop lock, oop owner) {
1438 snapshot->int_field_put(_blockerTypeOrdinal_offset, type_ordinal);
1439 snapshot->obj_field_put(_blockerObject_offset, lock);
1440 snapshot->obj_field_put(_parkBlockerOwner_offset, owner);
1441 }
1442 };
1443
1444 bool jdk_internal_vm_ThreadSnapshot::_inited = false;
1445 int jdk_internal_vm_ThreadSnapshot::_name_offset;
1446 int jdk_internal_vm_ThreadSnapshot::_threadStatus_offset;
1447 int jdk_internal_vm_ThreadSnapshot::_carrierThread_offset;
1448 int jdk_internal_vm_ThreadSnapshot::_stackTrace_offset;
1449 int jdk_internal_vm_ThreadSnapshot::_locks_offset;
1450 int jdk_internal_vm_ThreadSnapshot::_blockerTypeOrdinal_offset;
1451 int jdk_internal_vm_ThreadSnapshot::_blockerObject_offset;
1452 int jdk_internal_vm_ThreadSnapshot::_parkBlockerOwner_offset;
1453
1454 oop ThreadSnapshotFactory::get_thread_snapshot(jobject jthread, TRAPS) {
1455 ThreadsListHandle tlh(THREAD);
1456
1457 ResourceMark rm(THREAD);
1458 HandleMark hm(THREAD);
1459
1460 JavaThread* java_thread = nullptr;
1461 oop thread_oop;
1462 bool has_javathread = tlh.cv_internal_thread_to_JavaThread(jthread, &java_thread, &thread_oop);
1463 assert((has_javathread && thread_oop != nullptr) || !has_javathread, "Missing Thread oop");
1464 bool is_virtual = java_lang_VirtualThread::is_instance(thread_oop); // Deals with null
1465
1466 if (!has_javathread && !is_virtual) {
1467 return nullptr; // thread terminated so not of interest
1468 }
1469
1470 // Handshake with target
1471 Handle thread_h(THREAD, thread_oop);
1472 GetThreadSnapshotHandshakeClosure cl(thread_h);
1473 if (java_lang_VirtualThread::is_instance(thread_oop)) {
1474 Handshake::execute(&cl, thread_oop);
1475 } else {
1476 Handshake::execute(&cl, &tlh, java_thread);
1477 }
1478
1479 // StackTrace
1480 InstanceKlass* ste_klass = vmClasses::StackTraceElement_klass();
1481 assert(ste_klass != nullptr, "must be loaded");
1482
1483 objArrayHandle trace = oopFactory::new_objArray_handle(ste_klass, cl._frame_count, CHECK_NULL);
1484
1485 for (int i = 0; i < cl._frame_count; i++) {
1486 methodHandle method(THREAD, cl._methods->at(i));
1487 oop element = java_lang_StackTraceElement::create(method, cl._bcis->at(i), CHECK_NULL);
1488 trace->obj_at_put(i, element);
1489 }
1490
1491 // Locks
1492 Symbol* lock_sym = vmSymbols::jdk_internal_vm_ThreadLock();
1493 Klass* lock_k = SystemDictionary::resolve_or_fail(lock_sym, true, CHECK_NULL);
1494 InstanceKlass* lock_klass = InstanceKlass::cast(lock_k);
1495
1496 objArrayHandle locks;
1497 if (cl._locks != nullptr && cl._locks->length() > 0) {
1498 locks = oopFactory::new_objArray_handle(lock_klass, cl._locks->length(), CHECK_NULL);
1499 for (int n = 0; n < cl._locks->length(); n++) {
1500 GetThreadSnapshotHandshakeClosure::OwnedLock* lock_info = cl._locks->adr_at(n);
1501
1502 Handle lock = jdk_internal_vm_ThreadLock::create(lock_klass,
1503 lock_info->_frame_depth, lock_info->_type, lock_info->_obj, CHECK_NULL);
1504 locks->obj_at_put(n, lock());
1505 }
1506 }
1507
1508 // call static StackTraceElement[] StackTraceElement.of(StackTraceElement[] stackTrace)
1509 // to properly initialize STEs.
1510 JavaValue result(T_OBJECT);
1511 JavaCalls::call_static(&result,
1512 ste_klass,
1513 vmSymbols::java_lang_StackTraceElement_of_name(),
1514 vmSymbols::java_lang_StackTraceElement_of_signature(),
1515 trace,
1516 CHECK_NULL);
1517 // the method return the same trace array
1518
1519 Symbol* snapshot_klass_name = vmSymbols::jdk_internal_vm_ThreadSnapshot();
1520 Klass* snapshot_klass = SystemDictionary::resolve_or_fail(snapshot_klass_name, true, CHECK_NULL);
1521 if (snapshot_klass->should_be_initialized()) {
1522 snapshot_klass->initialize(CHECK_NULL);
1523 }
1524
1525 Handle snapshot = jdk_internal_vm_ThreadSnapshot::allocate(InstanceKlass::cast(snapshot_klass), CHECK_NULL);
1526 jdk_internal_vm_ThreadSnapshot::set_name(snapshot(), cl._thread_name.resolve());
1527 jdk_internal_vm_ThreadSnapshot::set_thread_status(snapshot(), (int)cl._thread_status);
1528 jdk_internal_vm_ThreadSnapshot::set_carrier_thread(snapshot(), cl._carrier_thread.resolve());
1529 jdk_internal_vm_ThreadSnapshot::set_stack_trace(snapshot(), trace());
1530 jdk_internal_vm_ThreadSnapshot::set_locks(snapshot(), locks());
1531 if (!cl._blocker.is_empty()) {
1532 jdk_internal_vm_ThreadSnapshot::set_blocker(snapshot(),
1533 cl._blocker._type, cl._blocker._obj.resolve(), cl._blocker._owner.resolve());
1534 }
1535 return snapshot();
1536 }
1537
1538 #endif // INCLUDE_JVMTI