1 /*
   2  * Copyright (c) 2003, 2022, 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 "precompiled.hpp"
  26 #include "classfile/javaClasses.inline.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "classfile/vmClasses.hpp"
  29 #include "classfile/vmSymbols.hpp"
  30 #include "gc/shared/oopStorageSet.hpp"
  31 #include "memory/allocation.hpp"
  32 #include "memory/heapInspection.hpp"
  33 #include "memory/oopFactory.hpp"
  34 #include "memory/resourceArea.hpp"
  35 #include "memory/universe.hpp"
  36 #include "oops/instanceKlass.hpp"
  37 #include "oops/klass.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 "runtime/atomic.hpp"
  44 #include "runtime/handles.inline.hpp"
  45 #include "runtime/init.hpp"
  46 #include "runtime/javaThread.inline.hpp"
  47 #include "runtime/objectMonitor.inline.hpp"
  48 #include "runtime/threads.hpp"
  49 #include "runtime/threadSMR.inline.hpp"
  50 #include "runtime/vframe.hpp"
  51 #include "runtime/vmThread.hpp"
  52 #include "runtime/vmOperations.hpp"
  53 #include "services/threadService.hpp"
  54 
  55 // TODO: we need to define a naming convention for perf counters
  56 // to distinguish counters for:
  57 //   - standard JSR174 use
  58 //   - Hotspot extension (public and committed)
  59 //   - Hotspot extension (private/internal and uncommitted)
  60 
  61 // Default is disabled.
  62 bool ThreadService::_thread_monitoring_contention_enabled = false;
  63 bool ThreadService::_thread_cpu_time_enabled = false;
  64 bool ThreadService::_thread_allocated_memory_enabled = false;
  65 
  66 PerfCounter*  ThreadService::_total_threads_count = NULL;
  67 PerfVariable* ThreadService::_live_threads_count = NULL;
  68 PerfVariable* ThreadService::_peak_threads_count = NULL;
  69 PerfVariable* ThreadService::_daemon_threads_count = NULL;
  70 volatile int ThreadService::_atomic_threads_count = 0;
  71 volatile int ThreadService::_atomic_daemon_threads_count = 0;
  72 
  73 ThreadDumpResult* ThreadService::_threaddump_list = NULL;
  74 
  75 static const int INITIAL_ARRAY_SIZE = 10;
  76 
  77 // OopStorage for thread stack sampling
  78 static OopStorage* _thread_service_storage = NULL;
  79 
  80 void ThreadService::init() {
  81   EXCEPTION_MARK;
  82 
  83   // These counters are for java.lang.management API support.
  84   // They are created even if -XX:-UsePerfData is set and in
  85   // that case, they will be allocated on C heap.
  86 
  87   _total_threads_count =
  88                 PerfDataManager::create_counter(JAVA_THREADS, "started",
  89                                                 PerfData::U_Events, CHECK);
  90 
  91   _live_threads_count =
  92                 PerfDataManager::create_variable(JAVA_THREADS, "live",
  93                                                  PerfData::U_None, CHECK);
  94 
  95   _peak_threads_count =
  96                 PerfDataManager::create_variable(JAVA_THREADS, "livePeak",
  97                                                  PerfData::U_None, CHECK);
  98 
  99   _daemon_threads_count =
 100                 PerfDataManager::create_variable(JAVA_THREADS, "daemon",
 101                                                  PerfData::U_None, CHECK);
 102 
 103   if (os::is_thread_cpu_time_supported()) {
 104     _thread_cpu_time_enabled = true;
 105   }
 106 
 107   _thread_allocated_memory_enabled = true; // Always on, so enable it
 108 
 109   // Initialize OopStorage for thread stack sampling walking
 110   _thread_service_storage = OopStorageSet::create_strong("ThreadService OopStorage",
 111                                                          mtServiceability);
 112 }
 113 
 114 void ThreadService::reset_peak_thread_count() {
 115   // Acquire the lock to update the peak thread count
 116   // to synchronize with thread addition and removal.
 117   MutexLocker mu(Threads_lock);
 118   _peak_threads_count->set_value(get_live_thread_count());
 119 }
 120 
 121 static bool is_hidden_thread(JavaThread *thread) {
 122   // hide VM internal or JVMTI agent threads
 123   return thread->is_hidden_from_external_view() || thread->is_jvmti_agent_thread();
 124 }
 125 
 126 void ThreadService::add_thread(JavaThread* thread, bool daemon) {
 127   assert(Threads_lock->owned_by_self(), "must have threads lock");
 128 
 129   // Do not count hidden threads
 130   if (is_hidden_thread(thread)) {
 131     return;
 132   }
 133 
 134   _total_threads_count->inc();
 135   _live_threads_count->inc();
 136   Atomic::inc(&_atomic_threads_count);
 137   int count = _atomic_threads_count;
 138 
 139   if (count > _peak_threads_count->get_value()) {
 140     _peak_threads_count->set_value(count);
 141   }
 142 
 143   if (daemon) {
 144     _daemon_threads_count->inc();
 145     Atomic::inc(&_atomic_daemon_threads_count);
 146   }
 147 }
 148 
 149 void ThreadService::decrement_thread_counts(JavaThread* jt, bool daemon) {
 150   Atomic::dec(&_atomic_threads_count);
 151 
 152   if (daemon) {
 153     Atomic::dec(&_atomic_daemon_threads_count);
 154   }
 155 }
 156 
 157 void ThreadService::remove_thread(JavaThread* thread, bool daemon) {
 158   assert(Threads_lock->owned_by_self(), "must have threads lock");
 159 
 160   // Do not count hidden threads
 161   if (is_hidden_thread(thread)) {
 162     return;
 163   }
 164 
 165   assert(!thread->is_terminated(), "must not be terminated");
 166   if (!thread->is_exiting()) {
 167     // We did not get here via JavaThread::exit() so current_thread_exiting()
 168     // was not called, e.g., JavaThread::cleanup_failed_attach_current_thread().
 169     decrement_thread_counts(thread, daemon);
 170   }
 171 
 172   int daemon_count = _atomic_daemon_threads_count;
 173   int count = _atomic_threads_count;
 174 
 175   // Counts are incremented at the same time, but atomic counts are
 176   // decremented earlier than perf counts.
 177   assert(_live_threads_count->get_value() > count,
 178     "thread count mismatch %d : %d",
 179     (int)_live_threads_count->get_value(), count);
 180 
 181   _live_threads_count->dec(1);
 182   if (daemon) {
 183     assert(_daemon_threads_count->get_value() > daemon_count,
 184       "thread count mismatch %d : %d",
 185       (int)_daemon_threads_count->get_value(), daemon_count);
 186 
 187     _daemon_threads_count->dec(1);
 188   }
 189 
 190   // Counts are incremented at the same time, but atomic counts are
 191   // decremented earlier than perf counts.
 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   assert(_live_threads_count->get_value() >= count,
 196     "thread count mismatch %d : %d",
 197     (int)_live_threads_count->get_value(), count);
 198   assert(_live_threads_count->get_value() > 0 ||
 199     (_live_threads_count->get_value() == 0 && count == 0 &&
 200     _daemon_threads_count->get_value() == 0 && daemon_count == 0),
 201     "thread counts should reach 0 at the same time, live %d,%d daemon %d,%d",
 202     (int)_live_threads_count->get_value(), count,
 203     (int)_daemon_threads_count->get_value(), daemon_count);
 204   assert(_daemon_threads_count->get_value() > 0 ||
 205     (_daemon_threads_count->get_value() == 0 && daemon_count == 0),
 206     "thread counts should reach 0 at the same time, daemon %d,%d",
 207     (int)_daemon_threads_count->get_value(), daemon_count);
 208 }
 209 
 210 void ThreadService::current_thread_exiting(JavaThread* jt, bool daemon) {
 211   // Do not count hidden threads
 212   if (is_hidden_thread(jt)) {
 213     return;
 214   }
 215 
 216   assert(jt == JavaThread::current(), "Called by current thread");
 217   assert(!jt->is_terminated() && jt->is_exiting(), "must be exiting");
 218 
 219   decrement_thread_counts(jt, daemon);
 220 }
 221 
 222 // FIXME: JVMTI should call this function
 223 Handle ThreadService::get_current_contended_monitor(JavaThread* thread) {
 224   assert(thread != NULL, "should be non-NULL");
 225   debug_only(Thread::check_for_dangling_thread_pointer(thread);)
 226 
 227   // This function can be called on a target JavaThread that is not
 228   // the caller and we are not at a safepoint. So it is possible for
 229   // the waiting or pending condition to be over/stale and for the
 230   // first stage of async deflation to clear the object field in
 231   // the ObjectMonitor. It is also possible for the object to be
 232   // inflated again and to be associated with a completely different
 233   // ObjectMonitor by the time this object reference is processed
 234   // by the caller.
 235   ObjectMonitor *wait_obj = thread->current_waiting_monitor();
 236 
 237   oop obj = NULL;
 238   if (wait_obj != NULL) {
 239     // thread is doing an Object.wait() call
 240     obj = wait_obj->object();
 241   } else {
 242     ObjectMonitor *enter_obj = thread->current_pending_monitor();
 243     if (enter_obj != NULL) {
 244       // thread is trying to enter() an ObjectMonitor.
 245       obj = enter_obj->object();
 246     }
 247   }
 248 
 249   Handle h(Thread::current(), obj);
 250   return h;
 251 }
 252 
 253 bool ThreadService::set_thread_monitoring_contention(bool flag) {
 254   MutexLocker m(Management_lock);
 255 
 256   bool prev = _thread_monitoring_contention_enabled;
 257   _thread_monitoring_contention_enabled = flag;
 258 
 259   return prev;
 260 }
 261 
 262 bool ThreadService::set_thread_cpu_time_enabled(bool flag) {
 263   MutexLocker m(Management_lock);
 264 
 265   bool prev = _thread_cpu_time_enabled;
 266   _thread_cpu_time_enabled = flag;
 267 
 268   return prev;
 269 }
 270 
 271 bool ThreadService::set_thread_allocated_memory_enabled(bool flag) {
 272   MutexLocker m(Management_lock);
 273 
 274   bool prev = _thread_allocated_memory_enabled;
 275   _thread_allocated_memory_enabled = flag;
 276 
 277   return prev;
 278 }
 279 
 280 void ThreadService::metadata_do(void f(Metadata*)) {
 281   for (ThreadDumpResult* dump = _threaddump_list; dump != NULL; dump = dump->next()) {
 282     dump->metadata_do(f);
 283   }
 284 }
 285 
 286 void ThreadService::add_thread_dump(ThreadDumpResult* dump) {
 287   MutexLocker ml(Management_lock);
 288   if (_threaddump_list == NULL) {
 289     _threaddump_list = dump;
 290   } else {
 291     dump->set_next(_threaddump_list);
 292     _threaddump_list = dump;
 293   }
 294 }
 295 
 296 void ThreadService::remove_thread_dump(ThreadDumpResult* dump) {
 297   MutexLocker ml(Management_lock);
 298 
 299   ThreadDumpResult* prev = NULL;
 300   bool found = false;
 301   for (ThreadDumpResult* d = _threaddump_list; d != NULL; prev = d, d = d->next()) {
 302     if (d == dump) {
 303       if (prev == NULL) {
 304         _threaddump_list = dump->next();
 305       } else {
 306         prev->set_next(dump->next());
 307       }
 308       found = true;
 309       break;
 310     }
 311   }
 312   assert(found, "The threaddump result to be removed must exist.");
 313 }
 314 
 315 // Dump stack trace of threads specified in the given threads array.
 316 // Returns StackTraceElement[][] each element is the stack trace of a thread in
 317 // the corresponding entry in the given threads array
 318 Handle ThreadService::dump_stack_traces(GrowableArray<instanceHandle>* threads,
 319                                         int num_threads,
 320                                         TRAPS) {
 321   assert(num_threads > 0, "just checking");
 322 
 323   ThreadDumpResult dump_result;
 324   VM_ThreadDump op(&dump_result,
 325                    threads,
 326                    num_threads,
 327                    -1,    /* entire stack */
 328                    false, /* with locked monitors */
 329                    false  /* with locked synchronizers */);
 330   VMThread::execute(&op);
 331 
 332   // Allocate the resulting StackTraceElement[][] object
 333 
 334   ResourceMark rm(THREAD);
 335   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_StackTraceElement_array(), true, CHECK_NH);
 336   ObjArrayKlass* ik = ObjArrayKlass::cast(k);
 337   objArrayOop r = oopFactory::new_objArray(ik, num_threads, CHECK_NH);
 338   objArrayHandle result_obj(THREAD, r);
 339 
 340   int num_snapshots = dump_result.num_snapshots();
 341   assert(num_snapshots == num_threads, "Must have num_threads thread snapshots");
 342   assert(num_snapshots == 0 || dump_result.t_list_has_been_set(), "ThreadsList must have been set if we have a snapshot");
 343   int i = 0;
 344   for (ThreadSnapshot* ts = dump_result.snapshots(); ts != NULL; i++, ts = ts->next()) {
 345     ThreadStackTrace* stacktrace = ts->get_stack_trace();
 346     if (stacktrace == NULL) {
 347       // No stack trace
 348       result_obj->obj_at_put(i, NULL);
 349     } else {
 350       // Construct an array of java/lang/StackTraceElement object
 351       Handle backtrace_h = stacktrace->allocate_fill_stack_trace_element_array(CHECK_NH);
 352       result_obj->obj_at_put(i, backtrace_h());
 353     }
 354   }
 355 
 356   return result_obj;
 357 }
 358 
 359 void ThreadService::reset_contention_count_stat(JavaThread* thread) {
 360   ThreadStatistics* stat = thread->get_thread_stat();
 361   if (stat != NULL) {
 362     stat->reset_count_stat();
 363   }
 364 }
 365 
 366 void ThreadService::reset_contention_time_stat(JavaThread* thread) {
 367   ThreadStatistics* stat = thread->get_thread_stat();
 368   if (stat != NULL) {
 369     stat->reset_time_stat();
 370   }
 371 }
 372 
 373 bool ThreadService::is_virtual_or_carrier_thread(JavaThread* jt) {
 374   oop threadObj = jt->threadObj();
 375   if (threadObj != NULL && threadObj->is_a(vmClasses::BasicVirtualThread_klass())) {
 376     // a virtual thread backed by JavaThread
 377     return true;
 378   }
 379   if (jt->is_vthread_mounted()) {
 380     // carrier thread
 381     return true;
 382   }
 383   return false;
 384 }
 385 
 386 // Find deadlocks involving raw monitors, object monitors and concurrent locks
 387 // if concurrent_locks is true.
 388 // We skip virtual thread carriers under the assumption that the current scheduler, ForkJoinPool,
 389 // doesn't hold any locks while mounting a virtual thread, so any owned monitor (or j.u.c., lock for that matter)
 390 // on that JavaThread must be owned by the virtual thread, and we don't support deadlock detection for virtual threads.
 391 DeadlockCycle* ThreadService::find_deadlocks_at_safepoint(ThreadsList * t_list, bool concurrent_locks) {
 392   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
 393 
 394   // This code was modified from the original Threads::find_deadlocks code.
 395   int globalDfn = 0, thisDfn;
 396   ObjectMonitor* waitingToLockMonitor = NULL;
 397   JvmtiRawMonitor* waitingToLockRawMonitor = NULL;
 398   oop waitingToLockBlocker = NULL;
 399   bool blocked_on_monitor = false;
 400   JavaThread *currentThread, *previousThread;
 401   int num_deadlocks = 0;
 402 
 403   // Initialize the depth-first-number for each JavaThread.
 404   JavaThreadIterator jti(t_list);
 405   for (JavaThread* jt = jti.first(); jt != NULL; jt = jti.next()) {
 406     if (!is_virtual_or_carrier_thread(jt)) {
 407       jt->set_depth_first_number(-1);
 408     }
 409   }
 410 
 411   DeadlockCycle* deadlocks = NULL;
 412   DeadlockCycle* last = NULL;
 413   DeadlockCycle* cycle = new DeadlockCycle();
 414   for (JavaThread* jt = jti.first(); jt != NULL; jt = jti.next()) {
 415     if (is_virtual_or_carrier_thread(jt)) {
 416       // skip virtual and carrier threads
 417       continue;
 418     }
 419     if (jt->depth_first_number() >= 0) {
 420       // this thread was already visited
 421       continue;
 422     }
 423 
 424     thisDfn = globalDfn;
 425     jt->set_depth_first_number(globalDfn++);
 426     previousThread = jt;
 427     currentThread = jt;
 428 
 429     cycle->reset();
 430 
 431     // The ObjectMonitor* can't be async deflated since we are at a safepoint.
 432     // When there is a deadlock, all the monitors involved in the dependency
 433     // cycle must be contended and heavyweight. So we only care about the
 434     // heavyweight monitor a thread is waiting to lock.
 435     waitingToLockMonitor = jt->current_pending_monitor();
 436     // JVM TI raw monitors can also be involved in deadlocks, and we can be
 437     // waiting to lock both a raw monitor and ObjectMonitor at the same time.
 438     // It isn't clear how to make deadlock detection work correctly if that
 439     // happens.
 440     waitingToLockRawMonitor = jt->current_pending_raw_monitor();
 441 
 442     if (concurrent_locks) {
 443       waitingToLockBlocker = jt->current_park_blocker();
 444     }
 445 
 446     while (waitingToLockMonitor != NULL ||
 447            waitingToLockRawMonitor != NULL ||
 448            waitingToLockBlocker != NULL) {
 449       cycle->add_thread(currentThread);
 450       // Give preference to the raw monitor
 451       if (waitingToLockRawMonitor != NULL) {
 452         Thread* owner = waitingToLockRawMonitor->owner();
 453         if (owner != NULL && // the raw monitor could be released at any time
 454             owner->is_Java_thread()) {
 455           currentThread = JavaThread::cast(owner);
 456         }
 457       } else if (waitingToLockMonitor != NULL) {
 458         if (waitingToLockMonitor->is_owner_anonymous() || waitingToLockMonitor->owner() != NULL) {
 459           currentThread = Threads::owning_thread_from_monitor(t_list, waitingToLockMonitor);
 460           if (currentThread == NULL) {
 461             // This function is called at a safepoint so the JavaThread
 462             // that owns waitingToLockMonitor should be findable, but
 463             // if it is not findable, then the previous currentThread is
 464             // blocked permanently. We record this as a deadlock.
 465             num_deadlocks++;
 466 
 467             // add this cycle to the deadlocks list
 468             if (deadlocks == NULL) {
 469               deadlocks = cycle;
 470             } else {
 471               last->set_next(cycle);
 472             }
 473             last = cycle;
 474             cycle = new DeadlockCycle();
 475             break;
 476           }
 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 != NULL ? java_lang_Thread::thread(threadObj) : NULL;
 485           } else {
 486             currentThread = NULL;
 487           }
 488         }
 489       }
 490 
 491       if (currentThread == NULL || 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 == NULL) {
 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(NULL), _last(NULL), _next(NULL), _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(NULL), _last(NULL), _next(NULL), _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 != NULL) {
 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 == NULL) {
 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 != NULL; 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 = NULL;
 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 (ResourceObj::C_HEAP, mtServiceability) GrowableArray<OopHandle>(length, mtServiceability);
 606       for (int i = 0; i < length; i++) {
 607         MonitorInfo* monitor = list->at(i);
 608         assert(monitor->owner() != NULL, "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 != NULL) {
 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 != NULL ? _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 (ResourceObj::C_HEAP, 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 (ResourceObj::C_HEAP, mtServiceability) GrowableArray<OopHandle>(INITIAL_ARRAY_SIZE, mtServiceability);
 662   } else {
 663     _jni_locked_monitors = NULL;
 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 != NULL) {
 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, ObjectMonitorsHashtable* table, 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 
 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     if (table != nullptr) {
 721       // Get the ObjectMonitors locked by the target thread, if any,
 722       // and does not include any where owner is set to a stack lock
 723       // address in the target thread:
 724       ObjectMonitorsHashtable::PtrList* list = table->get_entry(_thread);
 725       if (list != nullptr) {
 726         ObjectSynchronizer::monitors_iterate(&imc, list, _thread);
 727       }
 728     } else {
 729       ObjectSynchronizer::monitors_iterate(&imc, _thread);
 730     }
 731   }
 732 }
 733 
 734 
 735 bool ThreadStackTrace::is_owned_monitor_on_stack(oop object) {
 736   assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
 737 
 738   bool found = false;
 739   int num_frames = get_stack_depth();
 740   for (int depth = 0; depth < num_frames; depth++) {
 741     StackFrameInfo* frame = stack_frame_at(depth);
 742     int len = frame->num_locked_monitors();
 743     GrowableArray<OopHandle>* locked_monitors = frame->locked_monitors();
 744     for (int j = 0; j < len; j++) {
 745       oop monitor = locked_monitors->at(j).resolve();
 746       assert(monitor != NULL, "must be a Java object");
 747       if (monitor == object) {
 748         found = true;
 749         break;
 750       }
 751     }
 752   }
 753   return found;
 754 }
 755 
 756 Handle ThreadStackTrace::allocate_fill_stack_trace_element_array(TRAPS) {
 757   InstanceKlass* ik = vmClasses::StackTraceElement_klass();
 758   assert(ik != NULL, "must be loaded in 1.4+");
 759 
 760   // Allocate an array of java/lang/StackTraceElement object
 761   objArrayOop ste = oopFactory::new_objArray(ik, _depth, CHECK_NH);
 762   objArrayHandle backtrace(THREAD, ste);
 763   for (int j = 0; j < _depth; j++) {
 764     StackFrameInfo* frame = _frames->at(j);
 765     methodHandle mh(THREAD, frame->method());
 766     oop element = java_lang_StackTraceElement::create(mh, frame->bci(), CHECK_NH);
 767     backtrace->obj_at_put(j, element);
 768   }
 769   return backtrace;
 770 }
 771 
 772 void ThreadStackTrace::add_stack_frame(javaVFrame* jvf) {
 773   StackFrameInfo* frame = new StackFrameInfo(jvf, _with_locked_monitors);
 774   _frames->append(frame);
 775   _depth++;
 776 }
 777 
 778 void ThreadStackTrace::metadata_do(void f(Metadata*)) {
 779   int length = _frames->length();
 780   for (int i = 0; i < length; i++) {
 781     _frames->at(i)->metadata_do(f);
 782   }
 783 }
 784 
 785 
 786 ConcurrentLocksDump::~ConcurrentLocksDump() {
 787   if (_retain_map_on_free) {
 788     return;
 789   }
 790 
 791   for (ThreadConcurrentLocks* t = _map; t != NULL;)  {
 792     ThreadConcurrentLocks* tcl = t;
 793     t = t->next();
 794     delete tcl;
 795   }
 796 }
 797 
 798 void ConcurrentLocksDump::dump_at_safepoint() {
 799   // dump all locked concurrent locks
 800   assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
 801 
 802   GrowableArray<oop>* aos_objects = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<oop>(INITIAL_ARRAY_SIZE, mtServiceability);
 803 
 804   // Find all instances of AbstractOwnableSynchronizer
 805   HeapInspection::find_instances_at_safepoint(vmClasses::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass(),
 806                                               aos_objects);
 807   // Build a map of thread to its owned AQS locks
 808   build_map(aos_objects);
 809 
 810   delete aos_objects;
 811 }
 812 
 813 
 814 // build a map of JavaThread to all its owned AbstractOwnableSynchronizer
 815 void ConcurrentLocksDump::build_map(GrowableArray<oop>* aos_objects) {
 816   int length = aos_objects->length();
 817   for (int i = 0; i < length; i++) {
 818     oop o = aos_objects->at(i);
 819     oop owner_thread_obj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(o);
 820     if (owner_thread_obj != NULL) {
 821       // See comments in ThreadConcurrentLocks to see how this
 822       // JavaThread* is protected.
 823       JavaThread* thread = java_lang_Thread::thread(owner_thread_obj);
 824       assert(o->is_instance(), "Must be an instanceOop");
 825       add_lock(thread, (instanceOop) o);
 826     }
 827   }
 828 }
 829 
 830 void ConcurrentLocksDump::add_lock(JavaThread* thread, instanceOop o) {
 831   ThreadConcurrentLocks* tcl = thread_concurrent_locks(thread);
 832   if (tcl != NULL) {
 833     tcl->add_lock(o);
 834     return;
 835   }
 836 
 837   // First owned lock found for this thread
 838   tcl = new ThreadConcurrentLocks(thread);
 839   tcl->add_lock(o);
 840   if (_map == NULL) {
 841     _map = tcl;
 842   } else {
 843     _last->set_next(tcl);
 844   }
 845   _last = tcl;
 846 }
 847 
 848 ThreadConcurrentLocks* ConcurrentLocksDump::thread_concurrent_locks(JavaThread* thread) {
 849   for (ThreadConcurrentLocks* tcl = _map; tcl != NULL; tcl = tcl->next()) {
 850     if (tcl->java_thread() == thread) {
 851       return tcl;
 852     }
 853   }
 854   return NULL;
 855 }
 856 
 857 void ConcurrentLocksDump::print_locks_on(JavaThread* t, outputStream* st) {
 858   st->print_cr("   Locked ownable synchronizers:");
 859   ThreadConcurrentLocks* tcl = thread_concurrent_locks(t);
 860   GrowableArray<OopHandle>* locks = (tcl != NULL ? tcl->owned_locks() : NULL);
 861   if (locks == NULL || locks->is_empty()) {
 862     st->print_cr("\t- None");
 863     st->cr();
 864     return;
 865   }
 866 
 867   for (int i = 0; i < locks->length(); i++) {
 868     oop obj = locks->at(i).resolve();
 869     st->print_cr("\t- <" INTPTR_FORMAT "> (a %s)", p2i(obj), obj->klass()->external_name());
 870   }
 871   st->cr();
 872 }
 873 
 874 ThreadConcurrentLocks::ThreadConcurrentLocks(JavaThread* thread) {
 875   _thread = thread;
 876   _owned_locks = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<OopHandle>(INITIAL_ARRAY_SIZE, mtServiceability);
 877   _next = NULL;
 878 }
 879 
 880 ThreadConcurrentLocks::~ThreadConcurrentLocks() {
 881   for (int i = 0; i < _owned_locks->length(); i++) {
 882     _owned_locks->at(i).release(_thread_service_storage);
 883   }
 884   delete _owned_locks;
 885 }
 886 
 887 void ThreadConcurrentLocks::add_lock(instanceOop o) {
 888   _owned_locks->append(OopHandle(_thread_service_storage, o));
 889 }
 890 
 891 ThreadStatistics::ThreadStatistics() {
 892   _contended_enter_count = 0;
 893   _monitor_wait_count = 0;
 894   _sleep_count = 0;
 895   _count_pending_reset = false;
 896   _timer_pending_reset = false;
 897   memset((void*) _perf_recursion_counts, 0, sizeof(_perf_recursion_counts));
 898 }
 899 
 900 oop ThreadSnapshot::threadObj() const { return _threadObj.resolve(); }
 901 
 902 void ThreadSnapshot::initialize(ThreadsList * t_list, JavaThread* thread) {
 903   _thread = thread;
 904   oop threadObj = thread->threadObj();
 905   _threadObj = OopHandle(_thread_service_storage, threadObj);
 906 
 907   ThreadStatistics* stat = thread->get_thread_stat();
 908   _contended_enter_ticks = stat->contended_enter_ticks();
 909   _contended_enter_count = stat->contended_enter_count();
 910   _monitor_wait_ticks = stat->monitor_wait_ticks();
 911   _monitor_wait_count = stat->monitor_wait_count();
 912   _sleep_ticks = stat->sleep_ticks();
 913   _sleep_count = stat->sleep_count();
 914 
 915   // If thread is still attaching then threadObj will be NULL.
 916   _thread_status = threadObj == NULL ? JavaThreadStatus::NEW
 917                                      : java_lang_Thread::get_thread_status(threadObj);
 918 
 919   _is_suspended = thread->is_suspended();
 920   _is_in_native = (thread->thread_state() == _thread_in_native);
 921 
 922   Handle obj = ThreadService::get_current_contended_monitor(thread);
 923 
 924   oop blocker_object = NULL;
 925   oop blocker_object_owner = NULL;
 926 
 927   if (thread->is_vthread_mounted() && thread->vthread() != threadObj) { // ThreadSnapshot only captures platform threads
 928     _thread_status = JavaThreadStatus::IN_OBJECT_WAIT;
 929     oop vthread = thread->vthread();
 930     assert(vthread != NULL, "");
 931     blocker_object = vthread;
 932     blocker_object_owner = vthread;
 933   } else if (_thread_status == JavaThreadStatus::BLOCKED_ON_MONITOR_ENTER ||
 934       _thread_status == JavaThreadStatus::IN_OBJECT_WAIT ||
 935       _thread_status == JavaThreadStatus::IN_OBJECT_WAIT_TIMED) {
 936 
 937     if (obj() == NULL) {
 938       // monitor no longer exists; thread is not blocked
 939       _thread_status = JavaThreadStatus::RUNNABLE;
 940     } else {
 941       blocker_object = obj();
 942       JavaThread* owner = ObjectSynchronizer::get_lock_owner(t_list, obj);
 943       if ((owner == NULL && _thread_status == JavaThreadStatus::BLOCKED_ON_MONITOR_ENTER)
 944           || (owner != NULL && owner->is_attaching_via_jni())) {
 945         // ownership information of the monitor is not available
 946         // (may no longer be owned or releasing to some other thread)
 947         // make this thread in RUNNABLE state.
 948         // And when the owner thread is in attaching state, the java thread
 949         // is not completely initialized. For example thread name and id
 950         // and may not be set, so hide the attaching thread.
 951         _thread_status = JavaThreadStatus::RUNNABLE;
 952         blocker_object = NULL;
 953       } else if (owner != NULL) {
 954         blocker_object_owner = owner->threadObj();
 955       }
 956     }
 957   } else if (_thread_status == JavaThreadStatus::PARKED || _thread_status == JavaThreadStatus::PARKED_TIMED) {
 958     blocker_object = thread->current_park_blocker();
 959     if (blocker_object != NULL && blocker_object->is_a(vmClasses::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass())) {
 960       blocker_object_owner = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(blocker_object);
 961     }
 962   }
 963 
 964   if (blocker_object != NULL) {
 965     _blocker_object = OopHandle(_thread_service_storage, blocker_object);
 966   }
 967   if (blocker_object_owner != NULL) {
 968     _blocker_object_owner = OopHandle(_thread_service_storage, blocker_object_owner);
 969   }
 970 }
 971 
 972 oop ThreadSnapshot::blocker_object() const           { return _blocker_object.resolve(); }
 973 oop ThreadSnapshot::blocker_object_owner() const     { return _blocker_object_owner.resolve(); }
 974 
 975 ThreadSnapshot::~ThreadSnapshot() {
 976   _blocker_object.release(_thread_service_storage);
 977   _blocker_object_owner.release(_thread_service_storage);
 978   _threadObj.release(_thread_service_storage);
 979 
 980   delete _stack_trace;
 981   delete _concurrent_locks;
 982 }
 983 
 984 void ThreadSnapshot::dump_stack_at_safepoint(int max_depth, bool with_locked_monitors,
 985                                              ObjectMonitorsHashtable* table, bool full) {
 986   _stack_trace = new ThreadStackTrace(_thread, with_locked_monitors);
 987   _stack_trace->dump_stack_at_safepoint(max_depth, table, full);
 988 }
 989 
 990 
 991 void ThreadSnapshot::metadata_do(void f(Metadata*)) {
 992   if (_stack_trace != NULL) {
 993     _stack_trace->metadata_do(f);
 994   }
 995 }
 996 
 997 
 998 DeadlockCycle::DeadlockCycle() {
 999   _threads = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<JavaThread*>(INITIAL_ARRAY_SIZE, mtServiceability);
1000   _next = NULL;
1001 }
1002 
1003 DeadlockCycle::~DeadlockCycle() {
1004   delete _threads;
1005 }
1006 
1007 void DeadlockCycle::print_on_with(ThreadsList * t_list, outputStream* st) const {
1008   st->cr();
1009   st->print_cr("Found one Java-level deadlock:");
1010   st->print("=============================");
1011 
1012   JavaThread* currentThread;
1013   JvmtiRawMonitor* waitingToLockRawMonitor;
1014   oop waitingToLockBlocker;
1015   int len = _threads->length();
1016   for (int i = 0; i < len; i++) {
1017     currentThread = _threads->at(i);
1018     // The ObjectMonitor* can't be async deflated since we are at a safepoint.
1019     ObjectMonitor* waitingToLockMonitor = currentThread->current_pending_monitor();
1020     waitingToLockRawMonitor = currentThread->current_pending_raw_monitor();
1021     waitingToLockBlocker = currentThread->current_park_blocker();
1022     st->cr();
1023     st->print_cr("\"%s\":", currentThread->name());
1024     const char* owner_desc = ",\n  which is held by";
1025 
1026     // Note: As the JVM TI "monitor contended enter" event callback is executed after ObjectMonitor
1027     // sets the current pending monitor, it is possible to then see a pending raw monitor as well.
1028     if (waitingToLockRawMonitor != NULL) {
1029       st->print("  waiting to lock JVM TI raw monitor " INTPTR_FORMAT, p2i(waitingToLockRawMonitor));
1030       Thread* owner = waitingToLockRawMonitor->owner();
1031       // Could be NULL as the raw monitor could be released at any time if held by non-JavaThread
1032       if (owner != NULL) {
1033         if (owner->is_Java_thread()) {
1034           currentThread = JavaThread::cast(owner);
1035           st->print_cr("%s \"%s\"", owner_desc, currentThread->name());
1036         } else {
1037           st->print_cr(",\n  which has now been released");
1038         }
1039       } else {
1040         st->print_cr("%s non-Java thread=" PTR_FORMAT, owner_desc, p2i(owner));
1041       }
1042     }
1043 
1044     if (waitingToLockMonitor != NULL) {
1045       st->print("  waiting to lock monitor " INTPTR_FORMAT, p2i(waitingToLockMonitor));
1046       oop obj = waitingToLockMonitor->object();
1047       st->print(" (object " INTPTR_FORMAT ", a %s)", p2i(obj),
1048                  obj->klass()->external_name());
1049 
1050       if (!currentThread->current_pending_monitor_is_from_java()) {
1051         owner_desc = "\n  in JNI, which is held by";
1052       }
1053       currentThread = Threads::owning_thread_from_monitor(t_list, waitingToLockMonitor);
1054       if (currentThread == NULL) {
1055         // The deadlock was detected at a safepoint so the JavaThread
1056         // that owns waitingToLockMonitor should be findable, but
1057         // if it is not findable, then the previous currentThread is
1058         // blocked permanently.
1059         st->print_cr("%s UNKNOWN_owner_addr=" PTR_FORMAT, owner_desc,
1060                   p2i(waitingToLockMonitor->owner()));
1061         continue;
1062       }
1063     } else {
1064       st->print("  waiting for ownable synchronizer " INTPTR_FORMAT ", (a %s)",
1065                 p2i(waitingToLockBlocker),
1066                 waitingToLockBlocker->klass()->external_name());
1067       assert(waitingToLockBlocker->is_a(vmClasses::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass()),
1068              "Must be an AbstractOwnableSynchronizer");
1069       oop ownerObj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(waitingToLockBlocker);
1070       currentThread = java_lang_Thread::thread(ownerObj);
1071       assert(currentThread != NULL, "AbstractOwnableSynchronizer owning thread is unexpectedly NULL");
1072     }
1073     st->print_cr("%s \"%s\"", owner_desc, currentThread->name());
1074   }
1075 
1076   st->cr();
1077 
1078   // Print stack traces
1079   bool oldJavaMonitorsInStackTrace = JavaMonitorsInStackTrace;
1080   JavaMonitorsInStackTrace = true;
1081   st->print_cr("Java stack information for the threads listed above:");
1082   st->print_cr("===================================================");
1083   for (int j = 0; j < len; j++) {
1084     currentThread = _threads->at(j);
1085     st->print_cr("\"%s\":", currentThread->name());
1086     currentThread->print_stack_on(st);
1087   }
1088   JavaMonitorsInStackTrace = oldJavaMonitorsInStackTrace;
1089 }
1090 
1091 ThreadsListEnumerator::ThreadsListEnumerator(Thread* cur_thread,
1092                                              bool include_jvmti_agent_threads,
1093                                              bool include_jni_attaching_threads) {
1094   assert(cur_thread == Thread::current(), "Check current thread");
1095 
1096   int init_size = ThreadService::get_live_thread_count();
1097   _threads_array = new GrowableArray<instanceHandle>(init_size);
1098 
1099   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
1100     // skips JavaThreads in the process of exiting
1101     // and also skips VM internal JavaThreads
1102     // Threads in _thread_new or _thread_new_trans state are included.
1103     // i.e. threads have been started but not yet running.
1104     if (jt->threadObj() == NULL   ||
1105         jt->is_exiting() ||
1106         !java_lang_Thread::is_alive(jt->threadObj())   ||
1107         jt->is_hidden_from_external_view()) {
1108       continue;
1109     }
1110 
1111     // skip agent threads
1112     if (!include_jvmti_agent_threads && jt->is_jvmti_agent_thread()) {
1113       continue;
1114     }
1115 
1116     // skip jni threads in the process of attaching
1117     if (!include_jni_attaching_threads && jt->is_attaching_via_jni()) {
1118       continue;
1119     }
1120 
1121     instanceHandle h(cur_thread, (instanceOop) jt->threadObj());
1122     _threads_array->append(h);
1123   }
1124 }