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/atomic.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.inline.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/vmThread.hpp"
  57 #include "runtime/vmOperations.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   Atomic::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     Atomic::inc(&_atomic_daemon_threads_count);
 153   }
 154 }
 155 
 156 void ThreadService::decrement_thread_counts(JavaThread* jt, bool daemon) {
 157   Atomic::dec(&_atomic_threads_count);
 158 
 159   if (daemon) {
 160     Atomic::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(&reg_map)
 697       : _thread->last_java_vframe(&reg_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 GetThreadSnapshotClosure: 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 
1165     Blocker(Type type, OopHandle obj): _type(type), _obj(obj) {}
1166     Blocker(): _type(NOTHING), _obj(nullptr) {}
1167 
1168     bool is_empty() const {
1169       return _type == NOTHING;
1170     }
1171   };
1172 
1173   Handle _thread_h;
1174   JavaThread* _java_thread;
1175   int _frame_count; // length of _methods and _bcis arrays
1176   GrowableArray<Method*>* _methods;
1177   GrowableArray<int>* _bcis;
1178   JavaThreadStatus _thread_status;
1179   OopHandle _thread_name;
1180   GrowableArray<OwnedLock>* _locks;
1181   Blocker _blocker;
1182 
1183   GetThreadSnapshotClosure(Handle thread_h, JavaThread* java_thread):
1184     HandshakeClosure("GetThreadSnapshotClosure"),
1185     _thread_h(thread_h), _java_thread(java_thread),
1186     _frame_count(0), _methods(nullptr), _bcis(nullptr),
1187     _thread_status(), _thread_name(nullptr),
1188     _locks(nullptr), _blocker() {
1189   }
1190   virtual ~GetThreadSnapshotClosure() {
1191     delete _methods;
1192     delete _bcis;
1193     _thread_name.release(oop_storage());
1194     if (_locks != nullptr) {
1195       for (int i = 0; i < _locks->length(); i++) {
1196         _locks->at(i)._obj.release(oop_storage());
1197       }
1198       delete _locks;
1199     }
1200     _blocker._obj.release(oop_storage());
1201   }
1202 
1203 private:
1204   void detect_locks(javaVFrame* jvf, int depth) {
1205     Thread* current = Thread::current();
1206 
1207     if (depth == 0 && _blocker.is_empty()) {
1208       // If this is the first frame and it is java.lang.Object.wait(...)
1209       // then print out the receiver.
1210       if (jvf->method()->name() == vmSymbols::wait_name() &&
1211         jvf->method()->method_holder()->name() == vmSymbols::java_lang_Object()) {
1212         OopHandle lock_object;
1213         StackValueCollection* locs = jvf->locals();
1214         if (!locs->is_empty()) {
1215           StackValue* sv = locs->at(0);
1216           if (sv->type() == T_OBJECT) {
1217             Handle o = locs->at(0)->get_obj();
1218             lock_object = OopHandle(oop_storage(), o());
1219           }
1220         }
1221         _blocker = Blocker(Blocker::WAITING_ON, lock_object);
1222       }
1223     }
1224 
1225     GrowableArray<MonitorInfo*>* mons = jvf->monitors();
1226     if (!mons->is_empty()) {
1227       for (int index = (mons->length() - 1); index >= 0; index--) {
1228         MonitorInfo* monitor = mons->at(index);
1229         if (monitor->eliminated() && jvf->is_compiled_frame()) { // Eliminated in compiled code
1230           if (monitor->owner_is_scalar_replaced()) {
1231             Klass* k = java_lang_Class::as_Klass(monitor->owner_klass());
1232             _locks->push(OwnedLock(depth, OwnedLock::ELIMINATED, OopHandle(oop_storage(), k->klass_holder())));
1233           } else {
1234             Handle owner(current, monitor->owner());
1235             if (owner.not_null()) {
1236               Klass* k = owner->klass();
1237               _locks->push(OwnedLock(depth, OwnedLock::ELIMINATED, OopHandle(oop_storage(), k->klass_holder())));
1238             }
1239           }
1240           continue;
1241         }
1242         if (monitor->owner() != nullptr) {
1243           // the monitor is associated with an object, i.e., it is locked
1244 
1245           if (depth == 0 && _blocker.is_empty()) {
1246             ObjectMonitor* pending_moninor = java_lang_VirtualThread::is_instance(_thread_h())
1247               ? java_lang_VirtualThread::current_pending_monitor(_thread_h())
1248               : jvf->thread()->current_pending_monitor();
1249 
1250             markWord mark = monitor->owner()->mark();
1251             // The first stage of async deflation does not affect any field
1252             // used by this comparison so the ObjectMonitor* is usable here.
1253             if (mark.has_monitor()) {
1254               ObjectMonitor* mon = ObjectSynchronizer::read_monitor(current, monitor->owner(), mark);
1255               if (// if the monitor is null we must be in the process of locking
1256                   mon == nullptr ||
1257                   // we have marked ourself as pending on this monitor
1258                   mon == pending_moninor ||
1259                   // we are not the owner of this monitor
1260                   (_java_thread != nullptr && !mon->is_entered(_java_thread))) {
1261                 _blocker = Blocker(Blocker::WAITING_TO_LOCK, OopHandle(oop_storage(), monitor->owner()));
1262                 continue; // go to next monitor
1263               }
1264             }
1265           }
1266           _locks->push(OwnedLock(depth, OwnedLock::LOCKED, OopHandle(oop_storage(), monitor->owner())));
1267         }
1268       }
1269     }
1270   }
1271 
1272 public:
1273   void do_thread(Thread* th) override {
1274     Thread* current = Thread::current();
1275 
1276     bool is_virtual = java_lang_VirtualThread::is_instance(_thread_h());
1277     if (_java_thread != nullptr) {
1278       if (is_virtual) {
1279         // mounted vthread, use carrier thread state
1280         oop carrier_thread = java_lang_VirtualThread::carrier_thread(_thread_h());
1281         _thread_status = java_lang_Thread::get_thread_status(carrier_thread);
1282       } else {
1283         _thread_status = java_lang_Thread::get_thread_status(_thread_h());
1284       }
1285     } else {
1286       // unmounted vthread
1287       int vt_state = java_lang_VirtualThread::state(_thread_h());
1288       _thread_status = java_lang_VirtualThread::map_state_to_thread_status(vt_state);
1289     }
1290     _thread_name = OopHandle(oop_storage(), java_lang_Thread::name(_thread_h()));
1291 
1292     if (_java_thread != nullptr && !_java_thread->has_last_Java_frame()) {
1293       // stack trace is empty
1294       return;
1295     }
1296 
1297     bool vthread_carrier = !is_virtual && (_java_thread != nullptr) && (_java_thread->vthread_continuation() != nullptr);
1298 
1299     oop park_blocker = java_lang_Thread::park_blocker(_thread_h());
1300     if (park_blocker != nullptr) {
1301       _blocker = Blocker(Blocker::PARK_BLOCKER, OopHandle(oop_storage(), park_blocker));
1302     }
1303 
1304     ResourceMark rm(current);
1305     HandleMark hm(current);
1306 
1307     const int max_depth = MaxJavaStackTraceDepth;
1308     const bool skip_hidden = !ShowHiddenFrames;
1309 
1310     // Pick minimum length that will cover most cases
1311     int init_length = 64;
1312     _methods = new (mtInternal) GrowableArray<Method*>(init_length, mtInternal);
1313     _bcis = new (mtInternal) GrowableArray<int>(init_length, mtInternal);
1314     _locks = new (mtInternal) GrowableArray<OwnedLock>(init_length, mtInternal);
1315     int total_count = 0;
1316 
1317     vframeStream vfst(_java_thread != nullptr
1318       ? vframeStream(_java_thread, false, true, vthread_carrier)
1319       : vframeStream(java_lang_VirtualThread::continuation(_thread_h())));
1320 
1321     for (;
1322       !vfst.at_end() && (max_depth == 0 || max_depth != total_count);
1323       vfst.next()) {
1324 
1325       detect_locks(vfst.asJavaVFrame(), total_count);
1326 
1327       if (skip_hidden && (vfst.method()->is_hidden() ||
1328         vfst.method()->is_continuation_enter_intrinsic())) {
1329         continue;
1330       }
1331       _methods->push(vfst.method());
1332       _bcis->push(vfst.bci());
1333       total_count++;
1334     }
1335 
1336     _frame_count = total_count;
1337   }
1338 };
1339 
1340 class jdk_internal_vm_ThreadLock: AllStatic {
1341   static bool _inited;
1342   static int _depth_offset;
1343   static int _typeOrdinal_offset;
1344   static int _obj_offset;
1345 
1346   static void compute_offsets(InstanceKlass* klass, TRAPS) {
1347     JavaClasses::compute_offset(_depth_offset, klass, "depth", vmSymbols::int_signature(), false);
1348     JavaClasses::compute_offset(_typeOrdinal_offset, klass, "typeOrdinal", vmSymbols::int_signature(), false);
1349     JavaClasses::compute_offset(_obj_offset, klass, "obj", vmSymbols::object_signature(), false);
1350   }
1351 public:
1352   static void init(InstanceKlass* klass, TRAPS) {
1353     if (!_inited) {
1354       compute_offsets(klass, CHECK);
1355       _inited = true;
1356     }
1357   }
1358 
1359   static Handle create(InstanceKlass* klass, int depth, int type_ordinal, OopHandle obj, TRAPS) {
1360     init(klass, CHECK_NH);
1361     Handle result = klass->allocate_instance_handle(CHECK_NH);
1362     result->int_field_put(_depth_offset, depth);
1363     result->int_field_put(_typeOrdinal_offset, type_ordinal);
1364     result->obj_field_put(_obj_offset, obj.resolve());
1365     return result;
1366   }
1367 };
1368 
1369 bool jdk_internal_vm_ThreadLock::_inited = false;
1370 int jdk_internal_vm_ThreadLock::_depth_offset;
1371 int jdk_internal_vm_ThreadLock::_typeOrdinal_offset;
1372 int jdk_internal_vm_ThreadLock::_obj_offset;
1373 
1374 class jdk_internal_vm_ThreadSnapshot: AllStatic {
1375   static bool _inited;
1376   static int _name_offset;
1377   static int _threadStatus_offset;
1378   static int _carrierThread_offset;
1379   static int _stackTrace_offset;
1380   static int _locks_offset;
1381   static int _blockerTypeOrdinal_offset;
1382   static int _blockerObject_offset;
1383 
1384   static void compute_offsets(InstanceKlass* klass, TRAPS) {
1385     JavaClasses::compute_offset(_name_offset, klass, "name", vmSymbols::string_signature(), false);
1386     JavaClasses::compute_offset(_threadStatus_offset, klass, "threadStatus", vmSymbols::int_signature(), false);
1387     JavaClasses::compute_offset(_carrierThread_offset, klass, "carrierThread", vmSymbols::thread_signature(), false);
1388     JavaClasses::compute_offset(_stackTrace_offset, klass, "stackTrace", vmSymbols::java_lang_StackTraceElement_array(), false);
1389     JavaClasses::compute_offset(_locks_offset, klass, "locks", vmSymbols::jdk_internal_vm_ThreadLock_array(), false);
1390     JavaClasses::compute_offset(_blockerTypeOrdinal_offset, klass, "blockerTypeOrdinal", vmSymbols::int_signature(), false);
1391     JavaClasses::compute_offset(_blockerObject_offset, klass, "blockerObject", vmSymbols::object_signature(), false);
1392   }
1393 public:
1394   static void init(InstanceKlass* klass, TRAPS) {
1395     if (!_inited) {
1396       compute_offsets(klass, CHECK);
1397       _inited = true;
1398     }
1399   }
1400 
1401   static Handle allocate(InstanceKlass* klass, TRAPS) {
1402     init(klass, CHECK_NH);
1403     Handle h_k = klass->allocate_instance_handle(CHECK_NH);
1404     return h_k;
1405   }
1406 
1407   static void set_name(oop snapshot, oop name) {
1408     snapshot->obj_field_put(_name_offset, name);
1409   }
1410   static void set_thread_status(oop snapshot, int status) {
1411     snapshot->int_field_put(_threadStatus_offset, status);
1412   }
1413   static void set_carrier_thread(oop snapshot, oop carrier_thread) {
1414     snapshot->obj_field_put(_carrierThread_offset, carrier_thread);
1415   }
1416   static void set_stack_trace(oop snapshot, oop trace) {
1417     snapshot->obj_field_put(_stackTrace_offset, trace);
1418   }
1419   static void set_locks(oop snapshot, oop locks) {
1420     snapshot->obj_field_put(_locks_offset, locks);
1421   }
1422   static void set_blocker(oop snapshot, int type_ordinal, oop lock) {
1423     snapshot->int_field_put(_blockerTypeOrdinal_offset, type_ordinal);
1424     snapshot->obj_field_put(_blockerObject_offset, lock);
1425   }
1426 };
1427 
1428 bool jdk_internal_vm_ThreadSnapshot::_inited = false;
1429 int jdk_internal_vm_ThreadSnapshot::_name_offset;
1430 int jdk_internal_vm_ThreadSnapshot::_threadStatus_offset;
1431 int jdk_internal_vm_ThreadSnapshot::_carrierThread_offset;
1432 int jdk_internal_vm_ThreadSnapshot::_stackTrace_offset;
1433 int jdk_internal_vm_ThreadSnapshot::_locks_offset;
1434 int jdk_internal_vm_ThreadSnapshot::_blockerTypeOrdinal_offset;
1435 int jdk_internal_vm_ThreadSnapshot::_blockerObject_offset;
1436 
1437 oop ThreadSnapshotFactory::get_thread_snapshot(jobject jthread, TRAPS) {
1438   ThreadsListHandle tlh(THREAD);
1439 
1440   ResourceMark rm(THREAD);
1441   HandleMark   hm(THREAD);
1442   Handle thread_h(THREAD, JNIHandles::resolve(jthread));
1443 
1444   // wrapper to auto delete JvmtiVTMSTransitionDisabler
1445   class TransitionDisabler {
1446     JvmtiVTMSTransitionDisabler* _transition_disabler;
1447   public:
1448     TransitionDisabler(): _transition_disabler(nullptr) {}
1449     ~TransitionDisabler() {
1450       reset();
1451     }
1452     void init(jobject jthread) {
1453       _transition_disabler = new (mtInternal) JvmtiVTMSTransitionDisabler(jthread);
1454     }
1455     void reset() {
1456       if (_transition_disabler != nullptr) {
1457         delete _transition_disabler;
1458         _transition_disabler = nullptr;
1459       }
1460     }
1461   } transition_disabler;
1462 
1463   JavaThread* java_thread = nullptr;
1464   bool is_virtual = java_lang_VirtualThread::is_instance(thread_h());
1465   Handle carrier_thread;
1466   if (is_virtual) {
1467     // 1st need to disable mount/unmount transitions
1468     transition_disabler.init(jthread);
1469 
1470     carrier_thread = Handle(THREAD, java_lang_VirtualThread::carrier_thread(thread_h()));
1471     if (carrier_thread != nullptr) {
1472       java_thread = java_lang_Thread::thread(carrier_thread());
1473     }
1474   } else {
1475     java_thread = java_lang_Thread::thread(thread_h());
1476   }
1477 
1478   // Handshake with target
1479   GetThreadSnapshotClosure cl(thread_h, java_thread);
1480   if (java_thread == nullptr) {
1481     // unmounted vthread, execute on the current thread
1482     cl.do_thread(nullptr);
1483   } else {
1484     Handshake::execute(&cl, &tlh, java_thread);
1485   }
1486 
1487   // all info is collected, can enable transitions.
1488   transition_disabler.reset();
1489 
1490   // StackTrace
1491   InstanceKlass* ste_klass = vmClasses::StackTraceElement_klass();
1492   assert(ste_klass != nullptr, "must be loaded");
1493 
1494   objArrayHandle trace = oopFactory::new_objArray_handle(ste_klass, cl._frame_count, CHECK_NULL);
1495 
1496   for (int i = 0; i < cl._frame_count; i++) {
1497     methodHandle method(THREAD, cl._methods->at(i));
1498     oop element = java_lang_StackTraceElement::create(method, cl._bcis->at(i), CHECK_NULL);
1499     trace->obj_at_put(i, element);
1500   }
1501 
1502   // Locks
1503   Symbol* lock_sym = vmSymbols::jdk_internal_vm_ThreadLock();
1504   Klass* lock_k = SystemDictionary::resolve_or_fail(lock_sym, true, CHECK_NULL);
1505   InstanceKlass* lock_klass = InstanceKlass::cast(lock_k);
1506 
1507   objArrayHandle locks;
1508   if (cl._locks != nullptr && cl._locks->length() > 0) {
1509     locks = oopFactory::new_objArray_handle(lock_klass, cl._locks->length(), CHECK_NULL);
1510     for (int n = 0; n < cl._locks->length(); n++) {
1511       GetThreadSnapshotClosure::OwnedLock* lock_info = cl._locks->adr_at(n);
1512 
1513       Handle lock = jdk_internal_vm_ThreadLock::create(lock_klass,
1514         lock_info->_frame_depth, lock_info->_type, lock_info->_obj, CHECK_NULL);
1515       locks->obj_at_put(n, lock());
1516     }
1517   }
1518 
1519   // call static StackTraceElement[] StackTraceElement.of(StackTraceElement[] stackTrace)
1520   // to properly initialize STEs.
1521   JavaValue result(T_OBJECT);
1522   JavaCalls::call_static(&result,
1523     ste_klass,
1524     vmSymbols::java_lang_StackTraceElement_of_name(),
1525     vmSymbols::java_lang_StackTraceElement_of_signature(),
1526     trace,
1527     CHECK_NULL);
1528   // the method return the same trace array
1529 
1530   Symbol* snapshot_klass_name = vmSymbols::jdk_internal_vm_ThreadSnapshot();
1531   Klass* snapshot_klass = SystemDictionary::resolve_or_fail(snapshot_klass_name, true, CHECK_NULL);
1532   if (snapshot_klass->should_be_initialized()) {
1533     snapshot_klass->initialize(CHECK_NULL);
1534   }
1535 
1536   Handle snapshot = jdk_internal_vm_ThreadSnapshot::allocate(InstanceKlass::cast(snapshot_klass), CHECK_NULL);
1537   jdk_internal_vm_ThreadSnapshot::set_name(snapshot(), cl._thread_name.resolve());
1538   jdk_internal_vm_ThreadSnapshot::set_thread_status(snapshot(), (int)cl._thread_status);
1539   jdk_internal_vm_ThreadSnapshot::set_carrier_thread(snapshot(), carrier_thread());
1540   jdk_internal_vm_ThreadSnapshot::set_stack_trace(snapshot(), trace());
1541   jdk_internal_vm_ThreadSnapshot::set_locks(snapshot(), locks());
1542   if (!cl._blocker.is_empty()) {
1543     jdk_internal_vm_ThreadSnapshot::set_blocker(snapshot(), cl._blocker._type, cl._blocker._obj.resolve());
1544   }
1545   return snapshot();
1546 }
1547 
1548 #endif // INCLUDE_JVMTI
1549