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