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         address currentOwner = (address)waitingToLockMonitor->owner();
 459         if (currentOwner != NULL) {
 460           currentThread = Threads::owning_thread_from_monitor_owner(t_list,
 461                                                                     currentOwner);
 462           if (currentThread == NULL) {
 463             // This function is called at a safepoint so the JavaThread
 464             // that owns waitingToLockMonitor should be findable, but
 465             // if it is not findable, then the previous currentThread is
 466             // blocked permanently. We record this as a deadlock.
 467             num_deadlocks++;
 468 
 469             // add this cycle to the deadlocks list
 470             if (deadlocks == NULL) {
 471               deadlocks = cycle;
 472             } else {
 473               last->set_next(cycle);
 474             }
 475             last = cycle;
 476             cycle = new DeadlockCycle();
 477             break;
 478           }
 479         }
 480       } else {
 481         if (concurrent_locks) {
 482           if (waitingToLockBlocker->is_a(vmClasses::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass())) {
 483             oop threadObj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(waitingToLockBlocker);
 484             // This JavaThread (if there is one) is protected by the
 485             // ThreadsListSetter in VM_FindDeadlocks::doit().
 486             currentThread = threadObj != NULL ? java_lang_Thread::thread(threadObj) : NULL;
 487           } else {
 488             currentThread = NULL;
 489           }
 490         }
 491       }
 492 
 493       if (currentThread == NULL || is_virtual_or_carrier_thread(currentThread)) {
 494         // No dependency on another thread
 495         break;
 496       }
 497       if (currentThread->depth_first_number() < 0) {
 498         // First visit to this thread
 499         currentThread->set_depth_first_number(globalDfn++);
 500       } else if (currentThread->depth_first_number() < thisDfn) {
 501         // Thread already visited, and not on a (new) cycle
 502         break;
 503       } else if (currentThread == previousThread) {
 504         // Self-loop, ignore
 505         break;
 506       } else {
 507         // We have a (new) cycle
 508         num_deadlocks++;
 509 
 510         // add this cycle to the deadlocks list
 511         if (deadlocks == NULL) {
 512           deadlocks = cycle;
 513         } else {
 514           last->set_next(cycle);
 515         }
 516         last = cycle;
 517         cycle = new DeadlockCycle();
 518         break;
 519       }
 520       previousThread = currentThread;
 521       waitingToLockMonitor = (ObjectMonitor*)currentThread->current_pending_monitor();
 522       if (concurrent_locks) {
 523         waitingToLockBlocker = currentThread->current_park_blocker();
 524       }
 525     }
 526 
 527   }
 528   delete cycle;
 529   return deadlocks;
 530 }
 531 
 532 ThreadDumpResult::ThreadDumpResult() : _num_threads(0), _num_snapshots(0), _snapshots(NULL), _last(NULL), _next(NULL), _setter() {
 533 
 534   // Create a new ThreadDumpResult object and append to the list.
 535   // If GC happens before this function returns, Method*
 536   // in the stack trace will be visited.
 537   ThreadService::add_thread_dump(this);
 538 }
 539 
 540 ThreadDumpResult::ThreadDumpResult(int num_threads) : _num_threads(num_threads), _num_snapshots(0), _snapshots(NULL), _last(NULL), _next(NULL), _setter() {
 541   // Create a new ThreadDumpResult object and append to the list.
 542   // If GC happens before this function returns, oops
 543   // will be visited.
 544   ThreadService::add_thread_dump(this);
 545 }
 546 
 547 ThreadDumpResult::~ThreadDumpResult() {
 548   ThreadService::remove_thread_dump(this);
 549 
 550   // free all the ThreadSnapshot objects created during
 551   // the VM_ThreadDump operation
 552   ThreadSnapshot* ts = _snapshots;
 553   while (ts != NULL) {
 554     ThreadSnapshot* p = ts;
 555     ts = ts->next();
 556     delete p;
 557   }
 558 }
 559 
 560 ThreadSnapshot* ThreadDumpResult::add_thread_snapshot() {
 561   ThreadSnapshot* ts = new ThreadSnapshot();
 562   link_thread_snapshot(ts);
 563   return ts;
 564 }
 565 
 566 ThreadSnapshot* ThreadDumpResult::add_thread_snapshot(JavaThread* thread) {
 567   ThreadSnapshot* ts = new ThreadSnapshot();
 568   link_thread_snapshot(ts);
 569   ts->initialize(t_list(), thread);
 570   return ts;
 571 }
 572 
 573 void ThreadDumpResult::link_thread_snapshot(ThreadSnapshot* ts) {
 574   assert(_num_threads == 0 || _num_snapshots < _num_threads,
 575          "_num_snapshots must be less than _num_threads");
 576   _num_snapshots++;
 577   if (_snapshots == NULL) {
 578     _snapshots = ts;
 579   } else {
 580     _last->set_next(ts);
 581   }
 582   _last = ts;
 583 }
 584 
 585 void ThreadDumpResult::metadata_do(void f(Metadata*)) {
 586   for (ThreadSnapshot* ts = _snapshots; ts != NULL; ts = ts->next()) {
 587     ts->metadata_do(f);
 588   }
 589 }
 590 
 591 ThreadsList* ThreadDumpResult::t_list() {
 592   return _setter.list();
 593 }
 594 
 595 StackFrameInfo::StackFrameInfo(javaVFrame* jvf, bool with_lock_info) {
 596   _method = jvf->method();
 597   _bci = jvf->bci();
 598   _class_holder = OopHandle(_thread_service_storage, _method->method_holder()->klass_holder());
 599   _locked_monitors = NULL;
 600   if (with_lock_info) {
 601     Thread* current_thread = Thread::current();
 602     ResourceMark rm(current_thread);
 603     HandleMark hm(current_thread);
 604     GrowableArray<MonitorInfo*>* list = jvf->locked_monitors();
 605     int length = list->length();
 606     if (length > 0) {
 607       _locked_monitors = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<OopHandle>(length, mtServiceability);
 608       for (int i = 0; i < length; i++) {
 609         MonitorInfo* monitor = list->at(i);
 610         assert(monitor->owner() != NULL, "This monitor must have an owning object");
 611         _locked_monitors->append(OopHandle(_thread_service_storage, monitor->owner()));
 612       }
 613     }
 614   }
 615 }
 616 
 617 StackFrameInfo::~StackFrameInfo() {
 618   if (_locked_monitors != NULL) {
 619     for (int i = 0; i < _locked_monitors->length(); i++) {
 620       _locked_monitors->at(i).release(_thread_service_storage);
 621     }
 622     delete _locked_monitors;
 623   }
 624   _class_holder.release(_thread_service_storage);
 625 }
 626 
 627 void StackFrameInfo::metadata_do(void f(Metadata*)) {
 628   f(_method);
 629 }
 630 
 631 void StackFrameInfo::print_on(outputStream* st) const {
 632   ResourceMark rm;
 633   java_lang_Throwable::print_stack_element(st, method(), bci());
 634   int len = (_locked_monitors != NULL ? _locked_monitors->length() : 0);
 635   for (int i = 0; i < len; i++) {
 636     oop o = _locked_monitors->at(i).resolve();
 637     st->print_cr("\t- locked <" INTPTR_FORMAT "> (a %s)", p2i(o), o->klass()->external_name());
 638   }
 639 }
 640 
 641 // Iterate through monitor cache to find JNI locked monitors
 642 class InflatedMonitorsClosure: public MonitorClosure {
 643 private:
 644   ThreadStackTrace* _stack_trace;
 645 public:
 646   InflatedMonitorsClosure(ThreadStackTrace* st) {
 647     _stack_trace = st;
 648   }
 649   void do_monitor(ObjectMonitor* mid) {
 650     oop object = mid->object();
 651     if (!_stack_trace->is_owned_monitor_on_stack(object)) {
 652       _stack_trace->add_jni_locked_monitor(object);
 653     }
 654   }
 655 };
 656 
 657 ThreadStackTrace::ThreadStackTrace(JavaThread* t, bool with_locked_monitors) {
 658   _thread = t;
 659   _frames = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<StackFrameInfo*>(INITIAL_ARRAY_SIZE, mtServiceability);
 660   _depth = 0;
 661   _with_locked_monitors = with_locked_monitors;
 662   if (_with_locked_monitors) {
 663     _jni_locked_monitors = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<OopHandle>(INITIAL_ARRAY_SIZE, mtServiceability);
 664   } else {
 665     _jni_locked_monitors = NULL;
 666   }
 667 }
 668 
 669 void ThreadStackTrace::add_jni_locked_monitor(oop object) {
 670   _jni_locked_monitors->append(OopHandle(_thread_service_storage, object));
 671 }
 672 
 673 ThreadStackTrace::~ThreadStackTrace() {
 674   for (int i = 0; i < _frames->length(); i++) {
 675     delete _frames->at(i);
 676   }
 677   delete _frames;
 678   if (_jni_locked_monitors != NULL) {
 679     for (int i = 0; i < _jni_locked_monitors->length(); i++) {
 680       _jni_locked_monitors->at(i).release(_thread_service_storage);
 681     }
 682     delete _jni_locked_monitors;
 683   }
 684 }
 685 
 686 void ThreadStackTrace::dump_stack_at_safepoint(int maxDepth, ObjectMonitorsHashtable* table, bool full) {
 687   assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
 688 
 689   if (_thread->has_last_Java_frame()) {
 690     RegisterMap reg_map(_thread,
 691                         RegisterMap::UpdateMap::include,
 692                         RegisterMap::ProcessFrames::include,
 693                         RegisterMap::WalkContinuation::skip);
 694 
 695     // If full, we want to print both vthread and carrier frames
 696     vframe* start_vf = !full && _thread->is_vthread_mounted()
 697       ? _thread->carrier_last_java_vframe(&reg_map)
 698       : _thread->last_java_vframe(&reg_map);
 699     int count = 0;
 700     for (vframe* f = start_vf; f; f = f->sender() ) {
 701       if (maxDepth >= 0 && count == maxDepth) {
 702         // Skip frames if more than maxDepth
 703         break;
 704       }
 705       if (!full && f->is_vthread_entry()) {
 706         break;
 707       }
 708       if (f->is_java_frame()) {
 709         javaVFrame* jvf = javaVFrame::cast(f);
 710         add_stack_frame(jvf);
 711         count++;
 712       } else {
 713         // Ignore non-Java frames
 714       }
 715     }
 716   }
 717 
 718   if (_with_locked_monitors) {
 719     // Iterate inflated monitors and find monitors locked by this thread
 720     // that are not found in the stack, e.g. JNI locked monitors:
 721     InflatedMonitorsClosure imc(this);
 722     if (table != nullptr) {
 723       // Get the ObjectMonitors locked by the target thread, if any,
 724       // and does not include any where owner is set to a stack lock
 725       // address in the target thread:
 726       ObjectMonitorsHashtable::PtrList* list = table->get_entry(_thread);
 727       if (list != nullptr) {
 728         ObjectSynchronizer::monitors_iterate(&imc, list, _thread);
 729       }
 730     } else {
 731       ObjectSynchronizer::monitors_iterate(&imc, _thread);
 732     }
 733   }
 734 }
 735 
 736 
 737 bool ThreadStackTrace::is_owned_monitor_on_stack(oop object) {
 738   assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
 739 
 740   bool found = false;
 741   int num_frames = get_stack_depth();
 742   for (int depth = 0; depth < num_frames; depth++) {
 743     StackFrameInfo* frame = stack_frame_at(depth);
 744     int len = frame->num_locked_monitors();
 745     GrowableArray<OopHandle>* locked_monitors = frame->locked_monitors();
 746     for (int j = 0; j < len; j++) {
 747       oop monitor = locked_monitors->at(j).resolve();
 748       assert(monitor != NULL, "must be a Java object");
 749       if (monitor == object) {
 750         found = true;
 751         break;
 752       }
 753     }
 754   }
 755   return found;
 756 }
 757 
 758 Handle ThreadStackTrace::allocate_fill_stack_trace_element_array(TRAPS) {
 759   InstanceKlass* ik = vmClasses::StackTraceElement_klass();
 760   assert(ik != NULL, "must be loaded in 1.4+");
 761 
 762   // Allocate an array of java/lang/StackTraceElement object
 763   objArrayOop ste = oopFactory::new_objArray(ik, _depth, CHECK_NH);
 764   objArrayHandle backtrace(THREAD, ste);
 765   for (int j = 0; j < _depth; j++) {
 766     StackFrameInfo* frame = _frames->at(j);
 767     methodHandle mh(THREAD, frame->method());
 768     oop element = java_lang_StackTraceElement::create(mh, frame->bci(), CHECK_NH);
 769     backtrace->obj_at_put(j, element);
 770   }
 771   return backtrace;
 772 }
 773 
 774 void ThreadStackTrace::add_stack_frame(javaVFrame* jvf) {
 775   StackFrameInfo* frame = new StackFrameInfo(jvf, _with_locked_monitors);
 776   _frames->append(frame);
 777   _depth++;
 778 }
 779 
 780 void ThreadStackTrace::metadata_do(void f(Metadata*)) {
 781   int length = _frames->length();
 782   for (int i = 0; i < length; i++) {
 783     _frames->at(i)->metadata_do(f);
 784   }
 785 }
 786 
 787 
 788 ConcurrentLocksDump::~ConcurrentLocksDump() {
 789   if (_retain_map_on_free) {
 790     return;
 791   }
 792 
 793   for (ThreadConcurrentLocks* t = _map; t != NULL;)  {
 794     ThreadConcurrentLocks* tcl = t;
 795     t = t->next();
 796     delete tcl;
 797   }
 798 }
 799 
 800 void ConcurrentLocksDump::dump_at_safepoint() {
 801   // dump all locked concurrent locks
 802   assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
 803 
 804   GrowableArray<oop>* aos_objects = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<oop>(INITIAL_ARRAY_SIZE, mtServiceability);
 805 
 806   // Find all instances of AbstractOwnableSynchronizer
 807   HeapInspection::find_instances_at_safepoint(vmClasses::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass(),
 808                                               aos_objects);
 809   // Build a map of thread to its owned AQS locks
 810   build_map(aos_objects);
 811 
 812   delete aos_objects;
 813 }
 814 
 815 
 816 // build a map of JavaThread to all its owned AbstractOwnableSynchronizer
 817 void ConcurrentLocksDump::build_map(GrowableArray<oop>* aos_objects) {
 818   int length = aos_objects->length();
 819   for (int i = 0; i < length; i++) {
 820     oop o = aos_objects->at(i);
 821     oop owner_thread_obj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(o);
 822     if (owner_thread_obj != NULL) {
 823       // See comments in ThreadConcurrentLocks to see how this
 824       // JavaThread* is protected.
 825       JavaThread* thread = java_lang_Thread::thread(owner_thread_obj);
 826       assert(o->is_instance(), "Must be an instanceOop");
 827       add_lock(thread, (instanceOop) o);
 828     }
 829   }
 830 }
 831 
 832 void ConcurrentLocksDump::add_lock(JavaThread* thread, instanceOop o) {
 833   ThreadConcurrentLocks* tcl = thread_concurrent_locks(thread);
 834   if (tcl != NULL) {
 835     tcl->add_lock(o);
 836     return;
 837   }
 838 
 839   // First owned lock found for this thread
 840   tcl = new ThreadConcurrentLocks(thread);
 841   tcl->add_lock(o);
 842   if (_map == NULL) {
 843     _map = tcl;
 844   } else {
 845     _last->set_next(tcl);
 846   }
 847   _last = tcl;
 848 }
 849 
 850 ThreadConcurrentLocks* ConcurrentLocksDump::thread_concurrent_locks(JavaThread* thread) {
 851   for (ThreadConcurrentLocks* tcl = _map; tcl != NULL; tcl = tcl->next()) {
 852     if (tcl->java_thread() == thread) {
 853       return tcl;
 854     }
 855   }
 856   return NULL;
 857 }
 858 
 859 void ConcurrentLocksDump::print_locks_on(JavaThread* t, outputStream* st) {
 860   st->print_cr("   Locked ownable synchronizers:");
 861   ThreadConcurrentLocks* tcl = thread_concurrent_locks(t);
 862   GrowableArray<OopHandle>* locks = (tcl != NULL ? tcl->owned_locks() : NULL);
 863   if (locks == NULL || locks->is_empty()) {
 864     st->print_cr("\t- None");
 865     st->cr();
 866     return;
 867   }
 868 
 869   for (int i = 0; i < locks->length(); i++) {
 870     oop obj = locks->at(i).resolve();
 871     st->print_cr("\t- <" INTPTR_FORMAT "> (a %s)", p2i(obj), obj->klass()->external_name());
 872   }
 873   st->cr();
 874 }
 875 
 876 ThreadConcurrentLocks::ThreadConcurrentLocks(JavaThread* thread) {
 877   _thread = thread;
 878   _owned_locks = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<OopHandle>(INITIAL_ARRAY_SIZE, mtServiceability);
 879   _next = NULL;
 880 }
 881 
 882 ThreadConcurrentLocks::~ThreadConcurrentLocks() {
 883   for (int i = 0; i < _owned_locks->length(); i++) {
 884     _owned_locks->at(i).release(_thread_service_storage);
 885   }
 886   delete _owned_locks;
 887 }
 888 
 889 void ThreadConcurrentLocks::add_lock(instanceOop o) {
 890   _owned_locks->append(OopHandle(_thread_service_storage, o));
 891 }
 892 
 893 ThreadStatistics::ThreadStatistics() {
 894   _contended_enter_count = 0;
 895   _monitor_wait_count = 0;
 896   _sleep_count = 0;
 897   _count_pending_reset = false;
 898   _timer_pending_reset = false;
 899   memset((void*) _perf_recursion_counts, 0, sizeof(_perf_recursion_counts));
 900 }
 901 
 902 oop ThreadSnapshot::threadObj() const { return _threadObj.resolve(); }
 903 
 904 void ThreadSnapshot::initialize(ThreadsList * t_list, JavaThread* thread) {
 905   _thread = thread;
 906   oop threadObj = thread->threadObj();
 907   _threadObj = OopHandle(_thread_service_storage, threadObj);
 908 
 909   ThreadStatistics* stat = thread->get_thread_stat();
 910   _contended_enter_ticks = stat->contended_enter_ticks();
 911   _contended_enter_count = stat->contended_enter_count();
 912   _monitor_wait_ticks = stat->monitor_wait_ticks();
 913   _monitor_wait_count = stat->monitor_wait_count();
 914   _sleep_ticks = stat->sleep_ticks();
 915   _sleep_count = stat->sleep_count();
 916 
 917   // If thread is still attaching then threadObj will be NULL.
 918   _thread_status = threadObj == NULL ? JavaThreadStatus::NEW
 919                                      : java_lang_Thread::get_thread_status(threadObj);
 920 
 921   _is_suspended = thread->is_suspended();
 922   _is_in_native = (thread->thread_state() == _thread_in_native);
 923 
 924   Handle obj = ThreadService::get_current_contended_monitor(thread);
 925 
 926   oop blocker_object = NULL;
 927   oop blocker_object_owner = NULL;
 928 
 929   if (thread->is_vthread_mounted() && thread->vthread() != threadObj) { // ThreadSnapshot only captures platform threads
 930     _thread_status = JavaThreadStatus::IN_OBJECT_WAIT;
 931     oop vthread = thread->vthread();
 932     assert(vthread != NULL, "");
 933     blocker_object = vthread;
 934     blocker_object_owner = vthread;
 935   } else if (_thread_status == JavaThreadStatus::BLOCKED_ON_MONITOR_ENTER ||
 936       _thread_status == JavaThreadStatus::IN_OBJECT_WAIT ||
 937       _thread_status == JavaThreadStatus::IN_OBJECT_WAIT_TIMED) {
 938 
 939     if (obj() == NULL) {
 940       // monitor no longer exists; thread is not blocked
 941       _thread_status = JavaThreadStatus::RUNNABLE;
 942     } else {
 943       blocker_object = obj();
 944       JavaThread* owner = ObjectSynchronizer::get_lock_owner(t_list, obj);
 945       if ((owner == NULL && _thread_status == JavaThreadStatus::BLOCKED_ON_MONITOR_ENTER)
 946           || (owner != NULL && owner->is_attaching_via_jni())) {
 947         // ownership information of the monitor is not available
 948         // (may no longer be owned or releasing to some other thread)
 949         // make this thread in RUNNABLE state.
 950         // And when the owner thread is in attaching state, the java thread
 951         // is not completely initialized. For example thread name and id
 952         // and may not be set, so hide the attaching thread.
 953         _thread_status = JavaThreadStatus::RUNNABLE;
 954         blocker_object = NULL;
 955       } else if (owner != NULL) {
 956         blocker_object_owner = owner->threadObj();
 957       }
 958     }
 959   } else if (_thread_status == JavaThreadStatus::PARKED || _thread_status == JavaThreadStatus::PARKED_TIMED) {
 960     blocker_object = thread->current_park_blocker();
 961     if (blocker_object != NULL && blocker_object->is_a(vmClasses::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass())) {
 962       blocker_object_owner = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(blocker_object);
 963     }
 964   }
 965 
 966   if (blocker_object != NULL) {
 967     _blocker_object = OopHandle(_thread_service_storage, blocker_object);
 968   }
 969   if (blocker_object_owner != NULL) {
 970     _blocker_object_owner = OopHandle(_thread_service_storage, blocker_object_owner);
 971   }
 972 }
 973 
 974 oop ThreadSnapshot::blocker_object() const           { return _blocker_object.resolve(); }
 975 oop ThreadSnapshot::blocker_object_owner() const     { return _blocker_object_owner.resolve(); }
 976 
 977 ThreadSnapshot::~ThreadSnapshot() {
 978   _blocker_object.release(_thread_service_storage);
 979   _blocker_object_owner.release(_thread_service_storage);
 980   _threadObj.release(_thread_service_storage);
 981 
 982   delete _stack_trace;
 983   delete _concurrent_locks;
 984 }
 985 
 986 void ThreadSnapshot::dump_stack_at_safepoint(int max_depth, bool with_locked_monitors,
 987                                              ObjectMonitorsHashtable* table, bool full) {
 988   _stack_trace = new ThreadStackTrace(_thread, with_locked_monitors);
 989   _stack_trace->dump_stack_at_safepoint(max_depth, table, full);
 990 }
 991 
 992 
 993 void ThreadSnapshot::metadata_do(void f(Metadata*)) {
 994   if (_stack_trace != NULL) {
 995     _stack_trace->metadata_do(f);
 996   }
 997 }
 998 
 999 
1000 DeadlockCycle::DeadlockCycle() {
1001   _threads = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<JavaThread*>(INITIAL_ARRAY_SIZE, mtServiceability);
1002   _next = NULL;
1003 }
1004 
1005 DeadlockCycle::~DeadlockCycle() {
1006   delete _threads;
1007 }
1008 
1009 void DeadlockCycle::print_on_with(ThreadsList * t_list, outputStream* st) const {
1010   st->cr();
1011   st->print_cr("Found one Java-level deadlock:");
1012   st->print("=============================");
1013 
1014   JavaThread* currentThread;
1015   JvmtiRawMonitor* waitingToLockRawMonitor;
1016   oop waitingToLockBlocker;
1017   int len = _threads->length();
1018   for (int i = 0; i < len; i++) {
1019     currentThread = _threads->at(i);
1020     // The ObjectMonitor* can't be async deflated since we are at a safepoint.
1021     ObjectMonitor* waitingToLockMonitor = currentThread->current_pending_monitor();
1022     waitingToLockRawMonitor = currentThread->current_pending_raw_monitor();
1023     waitingToLockBlocker = currentThread->current_park_blocker();
1024     st->cr();
1025     st->print_cr("\"%s\":", currentThread->name());
1026     const char* owner_desc = ",\n  which is held by";
1027 
1028     // Note: As the JVM TI "monitor contended enter" event callback is executed after ObjectMonitor
1029     // sets the current pending monitor, it is possible to then see a pending raw monitor as well.
1030     if (waitingToLockRawMonitor != NULL) {
1031       st->print("  waiting to lock JVM TI raw monitor " INTPTR_FORMAT, p2i(waitingToLockRawMonitor));
1032       Thread* owner = waitingToLockRawMonitor->owner();
1033       // Could be NULL as the raw monitor could be released at any time if held by non-JavaThread
1034       if (owner != NULL) {
1035         if (owner->is_Java_thread()) {
1036           currentThread = JavaThread::cast(owner);
1037           st->print_cr("%s \"%s\"", owner_desc, currentThread->name());
1038         } else {
1039           st->print_cr(",\n  which has now been released");
1040         }
1041       } else {
1042         st->print_cr("%s non-Java thread=" PTR_FORMAT, owner_desc, p2i(owner));
1043       }
1044     }
1045 
1046     if (waitingToLockMonitor != NULL) {
1047       st->print("  waiting to lock monitor " INTPTR_FORMAT, p2i(waitingToLockMonitor));
1048       oop obj = waitingToLockMonitor->object();
1049       st->print(" (object " INTPTR_FORMAT ", a %s)", p2i(obj),
1050                  obj->klass()->external_name());
1051 
1052       if (!currentThread->current_pending_monitor_is_from_java()) {
1053         owner_desc = "\n  in JNI, which is held by";
1054       }
1055       currentThread = Threads::owning_thread_from_monitor_owner(t_list,
1056                                                                 (address)waitingToLockMonitor->owner());
1057       if (currentThread == NULL) {
1058         // The deadlock was detected at a safepoint so the JavaThread
1059         // that owns waitingToLockMonitor should be findable, but
1060         // if it is not findable, then the previous currentThread is
1061         // blocked permanently.
1062         st->print_cr("%s UNKNOWN_owner_addr=" PTR_FORMAT, owner_desc,
1063                   p2i(waitingToLockMonitor->owner()));
1064         continue;
1065       }
1066     } else {
1067       st->print("  waiting for ownable synchronizer " INTPTR_FORMAT ", (a %s)",
1068                 p2i(waitingToLockBlocker),
1069                 waitingToLockBlocker->klass()->external_name());
1070       assert(waitingToLockBlocker->is_a(vmClasses::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass()),
1071              "Must be an AbstractOwnableSynchronizer");
1072       oop ownerObj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(waitingToLockBlocker);
1073       currentThread = java_lang_Thread::thread(ownerObj);
1074       assert(currentThread != NULL, "AbstractOwnableSynchronizer owning thread is unexpectedly NULL");
1075     }
1076     st->print_cr("%s \"%s\"", owner_desc, currentThread->name());
1077   }
1078 
1079   st->cr();
1080 
1081   // Print stack traces
1082   bool oldJavaMonitorsInStackTrace = JavaMonitorsInStackTrace;
1083   JavaMonitorsInStackTrace = true;
1084   st->print_cr("Java stack information for the threads listed above:");
1085   st->print_cr("===================================================");
1086   for (int j = 0; j < len; j++) {
1087     currentThread = _threads->at(j);
1088     st->print_cr("\"%s\":", currentThread->name());
1089     currentThread->print_stack_on(st);
1090   }
1091   JavaMonitorsInStackTrace = oldJavaMonitorsInStackTrace;
1092 }
1093 
1094 ThreadsListEnumerator::ThreadsListEnumerator(Thread* cur_thread,
1095                                              bool include_jvmti_agent_threads,
1096                                              bool include_jni_attaching_threads) {
1097   assert(cur_thread == Thread::current(), "Check current thread");
1098 
1099   int init_size = ThreadService::get_live_thread_count();
1100   _threads_array = new GrowableArray<instanceHandle>(init_size);
1101 
1102   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
1103     // skips JavaThreads in the process of exiting
1104     // and also skips VM internal JavaThreads
1105     // Threads in _thread_new or _thread_new_trans state are included.
1106     // i.e. threads have been started but not yet running.
1107     if (jt->threadObj() == NULL   ||
1108         jt->is_exiting() ||
1109         !java_lang_Thread::is_alive(jt->threadObj())   ||
1110         jt->is_hidden_from_external_view()) {
1111       continue;
1112     }
1113 
1114     // skip agent threads
1115     if (!include_jvmti_agent_threads && jt->is_jvmti_agent_thread()) {
1116       continue;
1117     }
1118 
1119     // skip jni threads in the process of attaching
1120     if (!include_jni_attaching_threads && jt->is_attaching_via_jni()) {
1121       continue;
1122     }
1123 
1124     instanceHandle h(cur_thread, (instanceOop) jt->threadObj());
1125     _threads_array->append(h);
1126   }
1127 }