1 /*
   2  * Copyright (c) 2003, 2019, 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/systemDictionary.hpp"
  27 #include "memory/allocation.hpp"
  28 #include "memory/heapInspection.hpp"
  29 #include "memory/oopFactory.hpp"
  30 #include "memory/resourceArea.hpp"
  31 #include "oops/instanceKlass.hpp"
  32 #include "oops/objArrayKlass.hpp"
  33 #include "oops/objArrayOop.inline.hpp"
  34 #include "oops/oop.inline.hpp"
  35 #include "prims/jvmtiRawMonitor.hpp"
  36 #include "runtime/atomic.hpp"
  37 #include "runtime/handles.inline.hpp"
  38 #include "runtime/init.hpp"
  39 #include "runtime/objectMonitor.inline.hpp"
  40 #include "runtime/thread.inline.hpp"
  41 #include "runtime/threadSMR.inline.hpp"
  42 #include "runtime/vframe.hpp"
  43 #include "runtime/vmThread.hpp"
  44 #include "runtime/vmOperations.hpp"
  45 #include "services/threadService.hpp"
  46 
  47 // TODO: we need to define a naming convention for perf counters
  48 // to distinguish counters for:
  49 //   - standard JSR174 use
  50 //   - Hotspot extension (public and committed)
  51 //   - Hotspot extension (private/internal and uncommitted)
  52 
  53 // Default is disabled.
  54 bool ThreadService::_thread_monitoring_contention_enabled = false;
  55 bool ThreadService::_thread_cpu_time_enabled = false;
  56 bool ThreadService::_thread_allocated_memory_enabled = false;
  57 
  58 PerfCounter*  ThreadService::_total_threads_count = NULL;
  59 PerfVariable* ThreadService::_live_threads_count = NULL;
  60 PerfVariable* ThreadService::_peak_threads_count = NULL;
  61 PerfVariable* ThreadService::_daemon_threads_count = NULL;
  62 volatile int ThreadService::_atomic_threads_count = 0;
  63 volatile int ThreadService::_atomic_daemon_threads_count = 0;
  64 
  65 ThreadDumpResult* ThreadService::_threaddump_list = NULL;
  66 
  67 static const int INITIAL_ARRAY_SIZE = 10;
  68 
  69 void ThreadService::init() {
  70   EXCEPTION_MARK;
  71 
  72   // These counters are for java.lang.management API support.
  73   // They are created even if -XX:-UsePerfData is set and in
  74   // that case, they will be allocated on C heap.
  75 
  76   _total_threads_count =
  77                 PerfDataManager::create_counter(JAVA_THREADS, "started",
  78                                                 PerfData::U_Events, CHECK);
  79 
  80   _live_threads_count =
  81                 PerfDataManager::create_variable(JAVA_THREADS, "live",
  82                                                  PerfData::U_None, CHECK);
  83 
  84   _peak_threads_count =
  85                 PerfDataManager::create_variable(JAVA_THREADS, "livePeak",
  86                                                  PerfData::U_None, CHECK);
  87 
  88   _daemon_threads_count =
  89                 PerfDataManager::create_variable(JAVA_THREADS, "daemon",
  90                                                  PerfData::U_None, CHECK);
  91 
  92   if (os::is_thread_cpu_time_supported()) {
  93     _thread_cpu_time_enabled = true;
  94   }
  95 
  96   _thread_allocated_memory_enabled = true; // Always on, so enable it
  97 }
  98 
  99 void ThreadService::reset_peak_thread_count() {
 100   // Acquire the lock to update the peak thread count
 101   // to synchronize with thread addition and removal.
 102   MutexLocker mu(Threads_lock);
 103   _peak_threads_count->set_value(get_live_thread_count());
 104 }
 105 
 106 static bool is_hidden_thread(JavaThread *thread) {
 107   // hide VM internal or JVMTI agent threads
 108   return thread->is_hidden_from_external_view() || thread->is_jvmti_agent_thread();
 109 }
 110 
 111 void ThreadService::add_thread(JavaThread* thread, bool daemon) {
 112   assert(Threads_lock->owned_by_self(), "must have threads lock");
 113 
 114   // Do not count hidden threads
 115   if (is_hidden_thread(thread)) {
 116     return;
 117   }
 118 
 119   _total_threads_count->inc();
 120   _live_threads_count->inc();
 121   Atomic::inc(&_atomic_threads_count);
 122   int count = _atomic_threads_count;
 123 
 124   if (count > _peak_threads_count->get_value()) {
 125     _peak_threads_count->set_value(count);
 126   }
 127 
 128   if (daemon) {
 129     _daemon_threads_count->inc();
 130     Atomic::inc(&_atomic_daemon_threads_count);
 131   }
 132 }
 133 
 134 void ThreadService::decrement_thread_counts(JavaThread* jt, bool daemon) {
 135   Atomic::dec(&_atomic_threads_count);
 136 
 137   if (daemon) {
 138     Atomic::dec(&_atomic_daemon_threads_count);
 139   }
 140 }
 141 
 142 void ThreadService::remove_thread(JavaThread* thread, bool daemon) {
 143   assert(Threads_lock->owned_by_self(), "must have threads lock");
 144 
 145   // Do not count hidden threads
 146   if (is_hidden_thread(thread)) {
 147     return;
 148   }
 149 
 150   assert(!thread->is_terminated(), "must not be terminated");
 151   if (!thread->is_exiting()) {
 152     // JavaThread::exit() skipped calling current_thread_exiting()
 153     decrement_thread_counts(thread, daemon);
 154   }
 155 
 156   int daemon_count = _atomic_daemon_threads_count;
 157   int count = _atomic_threads_count;
 158 
 159   // Counts are incremented at the same time, but atomic counts are
 160   // decremented earlier than perf counts.
 161   assert(_live_threads_count->get_value() > count,
 162     "thread count mismatch %d : %d",
 163     (int)_live_threads_count->get_value(), count);
 164 
 165   _live_threads_count->dec(1);
 166   if (daemon) {
 167     assert(_daemon_threads_count->get_value() > daemon_count,
 168       "thread count mismatch %d : %d",
 169       (int)_daemon_threads_count->get_value(), daemon_count);
 170 
 171     _daemon_threads_count->dec(1);
 172   }
 173 
 174   // Counts are incremented at the same time, but atomic counts are
 175   // decremented earlier than perf counts.
 176   assert(_daemon_threads_count->get_value() >= daemon_count,
 177     "thread count mismatch %d : %d",
 178     (int)_daemon_threads_count->get_value(), daemon_count);
 179   assert(_live_threads_count->get_value() >= count,
 180     "thread count mismatch %d : %d",
 181     (int)_live_threads_count->get_value(), count);
 182   assert(_live_threads_count->get_value() > 0 ||
 183     (_live_threads_count->get_value() == 0 && count == 0 &&
 184     _daemon_threads_count->get_value() == 0 && daemon_count == 0),
 185     "thread counts should reach 0 at the same time, live %d,%d daemon %d,%d",
 186     (int)_live_threads_count->get_value(), count,
 187     (int)_daemon_threads_count->get_value(), daemon_count);
 188   assert(_daemon_threads_count->get_value() > 0 ||
 189     (_daemon_threads_count->get_value() == 0 && daemon_count == 0),
 190     "thread counts should reach 0 at the same time, daemon %d,%d",
 191     (int)_daemon_threads_count->get_value(), daemon_count);
 192 }
 193 
 194 void ThreadService::current_thread_exiting(JavaThread* jt, bool daemon) {
 195   // Do not count hidden threads
 196   if (is_hidden_thread(jt)) {
 197     return;
 198   }
 199 
 200   assert(jt == JavaThread::current(), "Called by current thread");
 201   assert(!jt->is_terminated() && jt->is_exiting(), "must be exiting");
 202 
 203   decrement_thread_counts(jt, daemon);
 204 }
 205 
 206 // FIXME: JVMTI should call this function
 207 Handle ThreadService::get_current_contended_monitor(JavaThread* thread) {
 208   assert(thread != NULL, "should be non-NULL");
 209   debug_only(Thread::check_for_dangling_thread_pointer(thread);)
 210 
 211   // This function can be called on a target JavaThread that is not
 212   // the caller and we are not at a safepoint. This ObjectMonitorHandle
 213   // keeps the ObjectMonitor from being async deflated so the object
 214   // reference we fetch remains non-NULL.
 215   ObjectMonitorHandle omh;
 216   ObjectMonitor *wait_obj = thread->current_waiting_monitor(&omh);
 217 
 218   oop obj = NULL;
 219   if (wait_obj != NULL) {
 220     // thread is doing an Object.wait() call
 221     obj = (oop) wait_obj->object();
 222     assert(obj != NULL, "Object.wait() should have an object");
 223   } else {
 224     ObjectMonitor *enter_obj = thread->current_pending_monitor(&omh);
 225     if (enter_obj != NULL) {
 226       // thread is trying to enter() an ObjectMonitor.
 227       obj = (oop) enter_obj->object();
 228       assert(obj != NULL, "ObjectMonitor should have an associated object!");
 229     }
 230   }
 231 
 232   Handle h(Thread::current(), obj);
 233   return h;
 234 }
 235 
 236 bool ThreadService::set_thread_monitoring_contention(bool flag) {
 237   MutexLocker m(Management_lock);
 238 
 239   bool prev = _thread_monitoring_contention_enabled;
 240   _thread_monitoring_contention_enabled = flag;
 241 
 242   return prev;
 243 }
 244 
 245 bool ThreadService::set_thread_cpu_time_enabled(bool flag) {
 246   MutexLocker m(Management_lock);
 247 
 248   bool prev = _thread_cpu_time_enabled;
 249   _thread_cpu_time_enabled = flag;
 250 
 251   return prev;
 252 }
 253 
 254 bool ThreadService::set_thread_allocated_memory_enabled(bool flag) {
 255   MutexLocker m(Management_lock);
 256 
 257   bool prev = _thread_allocated_memory_enabled;
 258   _thread_allocated_memory_enabled = flag;
 259 
 260   return prev;
 261 }
 262 
 263 // GC support
 264 void ThreadService::oops_do(OopClosure* f) {
 265   for (ThreadDumpResult* dump = _threaddump_list; dump != NULL; dump = dump->next()) {
 266     dump->oops_do(f);
 267   }
 268 }
 269 
 270 void ThreadService::metadata_do(void f(Metadata*)) {
 271   for (ThreadDumpResult* dump = _threaddump_list; dump != NULL; dump = dump->next()) {
 272     dump->metadata_do(f);
 273   }
 274 }
 275 
 276 void ThreadService::add_thread_dump(ThreadDumpResult* dump) {
 277   MutexLocker ml(Management_lock);
 278   if (_threaddump_list == NULL) {
 279     _threaddump_list = dump;
 280   } else {
 281     dump->set_next(_threaddump_list);
 282     _threaddump_list = dump;
 283   }
 284 }
 285 
 286 void ThreadService::remove_thread_dump(ThreadDumpResult* dump) {
 287   MutexLocker ml(Management_lock);
 288 
 289   ThreadDumpResult* prev = NULL;
 290   bool found = false;
 291   for (ThreadDumpResult* d = _threaddump_list; d != NULL; prev = d, d = d->next()) {
 292     if (d == dump) {
 293       if (prev == NULL) {
 294         _threaddump_list = dump->next();
 295       } else {
 296         prev->set_next(dump->next());
 297       }
 298       found = true;
 299       break;
 300     }
 301   }
 302   assert(found, "The threaddump result to be removed must exist.");
 303 }
 304 
 305 // Dump stack trace of threads specified in the given threads array.
 306 // Returns StackTraceElement[][] each element is the stack trace of a thread in
 307 // the corresponding entry in the given threads array
 308 Handle ThreadService::dump_stack_traces(GrowableArray<instanceHandle>* threads,
 309                                         int num_threads,
 310                                         TRAPS) {
 311   assert(num_threads > 0, "just checking");
 312 
 313   ThreadDumpResult dump_result;
 314   VM_ThreadDump op(&dump_result,
 315                    threads,
 316                    num_threads,
 317                    -1,    /* entire stack */
 318                    false, /* with locked monitors */
 319                    false  /* with locked synchronizers */);
 320   VMThread::execute(&op);
 321 
 322   // Allocate the resulting StackTraceElement[][] object
 323 
 324   ResourceMark rm(THREAD);
 325   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_StackTraceElement_array(), true, CHECK_NH);
 326   ObjArrayKlass* ik = ObjArrayKlass::cast(k);
 327   objArrayOop r = oopFactory::new_objArray(ik, num_threads, CHECK_NH);
 328   objArrayHandle result_obj(THREAD, r);
 329 
 330   int num_snapshots = dump_result.num_snapshots();
 331   assert(num_snapshots == num_threads, "Must have num_threads thread snapshots");
 332   assert(num_snapshots == 0 || dump_result.t_list_has_been_set(), "ThreadsList must have been set if we have a snapshot");
 333   int i = 0;
 334   for (ThreadSnapshot* ts = dump_result.snapshots(); ts != NULL; i++, ts = ts->next()) {
 335     ThreadStackTrace* stacktrace = ts->get_stack_trace();
 336     if (stacktrace == NULL) {
 337       // No stack trace
 338       result_obj->obj_at_put(i, NULL);
 339     } else {
 340       // Construct an array of java/lang/StackTraceElement object
 341       Handle backtrace_h = stacktrace->allocate_fill_stack_trace_element_array(CHECK_NH);
 342       result_obj->obj_at_put(i, backtrace_h());
 343     }
 344   }
 345 
 346   return result_obj;
 347 }
 348 
 349 void ThreadService::reset_contention_count_stat(JavaThread* thread) {
 350   ThreadStatistics* stat = thread->get_thread_stat();
 351   if (stat != NULL) {
 352     stat->reset_count_stat();
 353   }
 354 }
 355 
 356 void ThreadService::reset_contention_time_stat(JavaThread* thread) {
 357   ThreadStatistics* stat = thread->get_thread_stat();
 358   if (stat != NULL) {
 359     stat->reset_time_stat();
 360   }
 361 }
 362 
 363 // Find deadlocks involving raw monitors, object monitors and concurrent locks
 364 // if concurrent_locks is true.
 365 DeadlockCycle* ThreadService::find_deadlocks_at_safepoint(ThreadsList * t_list, bool concurrent_locks) {
 366   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
 367 
 368   // This code was modified from the original Threads::find_deadlocks code.
 369   int globalDfn = 0, thisDfn;
 370   // This code is called at a safepoint so this ObjectMonitorHandle
 371   // is not strictly necessary.
 372   ObjectMonitorHandle omh;
 373   ObjectMonitor* waitingToLockMonitor = NULL;
 374   JvmtiRawMonitor* waitingToLockRawMonitor = NULL;
 375   oop waitingToLockBlocker = NULL;
 376   bool blocked_on_monitor = false;
 377   JavaThread *currentThread, *previousThread;
 378   int num_deadlocks = 0;
 379 
 380   // Initialize the depth-first-number for each JavaThread.
 381   JavaThreadIterator jti(t_list);
 382   for (JavaThread* jt = jti.first(); jt != NULL; jt = jti.next()) {
 383     jt->set_depth_first_number(-1);
 384   }
 385 
 386   DeadlockCycle* deadlocks = NULL;
 387   DeadlockCycle* last = NULL;
 388   DeadlockCycle* cycle = new DeadlockCycle();
 389   for (JavaThread* jt = jti.first(); jt != NULL; jt = jti.next()) {
 390     if (jt->depth_first_number() >= 0) {
 391       // this thread was already visited
 392       continue;
 393     }
 394 
 395     thisDfn = globalDfn;
 396     jt->set_depth_first_number(globalDfn++);
 397     previousThread = jt;
 398     currentThread = jt;
 399 
 400     cycle->reset();
 401 
 402     if (waitingToLockMonitor != NULL) {
 403       // Done with the current waitingToLockMonitor value so release
 404       // the ObjectMonitorHandle manually before we use it again:
 405       omh.unset_om_ptr();
 406     }
 407     // When there is a deadlock, all the monitors involved in the dependency
 408     // cycle must be contended and heavyweight. So we only care about the
 409     // heavyweight monitor a thread is waiting to lock.
 410     waitingToLockMonitor = jt->current_pending_monitor(&omh);
 411     // JVM TI raw monitors can also be involved in deadlocks, and we can be
 412     // waiting to lock both a raw monitor and ObjectMonitor at the same time.
 413     // It isn't clear how to make deadlock detection work correctly if that
 414     // happens.
 415     waitingToLockRawMonitor = jt->current_pending_raw_monitor();
 416 
 417     if (concurrent_locks) {
 418       waitingToLockBlocker = jt->current_park_blocker();
 419     }
 420 
 421     while (waitingToLockMonitor != NULL ||
 422            waitingToLockRawMonitor != NULL ||
 423            waitingToLockBlocker != NULL) {
 424       cycle->add_thread(currentThread);
 425       // Give preference to the raw monitor
 426       if (waitingToLockRawMonitor != NULL) {
 427         Thread* owner = waitingToLockRawMonitor->owner();
 428         if (owner != NULL && // the raw monitor could be released at any time
 429             owner->is_Java_thread()) {
 430           // only JavaThreads can be reported here
 431           currentThread = (JavaThread*) owner;
 432         }
 433       } else if (waitingToLockMonitor != NULL) {
 434         address currentOwner = (address)waitingToLockMonitor->owner();
 435         if (currentOwner != NULL) {
 436           currentThread = Threads::owning_thread_from_monitor_owner(t_list,
 437                                                                     currentOwner);
 438           if (currentThread == NULL) {
 439             // This function is called at a safepoint so the JavaThread
 440             // that owns waitingToLockMonitor should be findable, but
 441             // if it is not findable, then the previous currentThread is
 442             // blocked permanently. We record this as a deadlock.
 443             num_deadlocks++;
 444 
 445             cycle->set_deadlock(true);
 446 
 447             // add this cycle to the deadlocks list
 448             if (deadlocks == NULL) {
 449               deadlocks = cycle;
 450             } else {
 451               last->set_next(cycle);
 452             }
 453             last = cycle;
 454             cycle = new DeadlockCycle();
 455             break;
 456           }
 457         }
 458       } else {
 459         if (concurrent_locks) {
 460           if (waitingToLockBlocker->is_a(SystemDictionary::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass())) {
 461             oop threadObj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(waitingToLockBlocker);
 462             // This JavaThread (if there is one) is protected by the
 463             // ThreadsListSetter in VM_FindDeadlocks::doit().
 464             currentThread = threadObj != NULL ? java_lang_Thread::thread(threadObj) : NULL;
 465           } else {
 466             currentThread = NULL;
 467           }
 468         }
 469       }
 470 
 471       if (currentThread == NULL) {
 472         // No dependency on another thread
 473         break;
 474       }
 475       if (currentThread->depth_first_number() < 0) {
 476         // First visit to this thread
 477         currentThread->set_depth_first_number(globalDfn++);
 478       } else if (currentThread->depth_first_number() < thisDfn) {
 479         // Thread already visited, and not on a (new) cycle
 480         break;
 481       } else if (currentThread == previousThread) {
 482         // Self-loop, ignore
 483         break;
 484       } else {
 485         // We have a (new) cycle
 486         num_deadlocks++;
 487 
 488         cycle->set_deadlock(true);
 489 
 490         // add this cycle to the deadlocks list
 491         if (deadlocks == NULL) {
 492           deadlocks = cycle;
 493         } else {
 494           last->set_next(cycle);
 495         }
 496         last = cycle;
 497         cycle = new DeadlockCycle();
 498         break;
 499       }
 500       previousThread = currentThread;
 501       if (waitingToLockMonitor != NULL) {
 502         // Done with the current waitingToLockMonitor value so release
 503         // the ObjectMonitorHandle manually before we use it again:
 504         omh.unset_om_ptr();
 505       }
 506       waitingToLockMonitor = (ObjectMonitor*)currentThread->current_pending_monitor(&omh);
 507       if (concurrent_locks) {
 508         waitingToLockBlocker = currentThread->current_park_blocker();
 509       }
 510     }
 511 
 512   }
 513   delete cycle;
 514   return deadlocks;
 515 }
 516 
 517 ThreadDumpResult::ThreadDumpResult() : _num_threads(0), _num_snapshots(0), _snapshots(NULL), _last(NULL), _next(NULL), _setter() {
 518 
 519   // Create a new ThreadDumpResult object and append to the list.
 520   // If GC happens before this function returns, Method*
 521   // in the stack trace will be visited.
 522   ThreadService::add_thread_dump(this);
 523 }
 524 
 525 ThreadDumpResult::ThreadDumpResult(int num_threads) : _num_threads(num_threads), _num_snapshots(0), _snapshots(NULL), _last(NULL), _next(NULL), _setter() {
 526   // Create a new ThreadDumpResult object and append to the list.
 527   // If GC happens before this function returns, oops
 528   // will be visited.
 529   ThreadService::add_thread_dump(this);
 530 }
 531 
 532 ThreadDumpResult::~ThreadDumpResult() {
 533   ThreadService::remove_thread_dump(this);
 534 
 535   // free all the ThreadSnapshot objects created during
 536   // the VM_ThreadDump operation
 537   ThreadSnapshot* ts = _snapshots;
 538   while (ts != NULL) {
 539     ThreadSnapshot* p = ts;
 540     ts = ts->next();
 541     delete p;
 542   }
 543 }
 544 
 545 ThreadSnapshot* ThreadDumpResult::add_thread_snapshot() {
 546   ThreadSnapshot* ts = new ThreadSnapshot();
 547   link_thread_snapshot(ts);
 548   return ts;
 549 }
 550 
 551 ThreadSnapshot* ThreadDumpResult::add_thread_snapshot(JavaThread* thread) {
 552   // Note: it is very important that the ThreadSnapshot* gets linked before
 553   // ThreadSnapshot::initialize gets called. This is to ensure that
 554   // ThreadSnapshot::oops_do can get called prior to the field
 555   // ThreadSnapshot::_threadObj being assigned a value (to prevent a dangling
 556   // oop).
 557   ThreadSnapshot* ts = new ThreadSnapshot();
 558   link_thread_snapshot(ts);
 559   ts->initialize(t_list(), thread);
 560   return ts;
 561 }
 562 
 563 void ThreadDumpResult::link_thread_snapshot(ThreadSnapshot* ts) {
 564   assert(_num_threads == 0 || _num_snapshots < _num_threads,
 565          "_num_snapshots must be less than _num_threads");
 566   _num_snapshots++;
 567   if (_snapshots == NULL) {
 568     _snapshots = ts;
 569   } else {
 570     _last->set_next(ts);
 571   }
 572   _last = ts;
 573 }
 574 
 575 void ThreadDumpResult::oops_do(OopClosure* f) {
 576   for (ThreadSnapshot* ts = _snapshots; ts != NULL; ts = ts->next()) {
 577     ts->oops_do(f);
 578   }
 579 }
 580 
 581 void ThreadDumpResult::metadata_do(void f(Metadata*)) {
 582   for (ThreadSnapshot* ts = _snapshots; ts != NULL; ts = ts->next()) {
 583     ts->metadata_do(f);
 584   }
 585 }
 586 
 587 ThreadsList* ThreadDumpResult::t_list() {
 588   return _setter.list();
 589 }
 590 
 591 StackFrameInfo::StackFrameInfo(javaVFrame* jvf, bool with_lock_info) {
 592   _method = jvf->method();
 593   _bci = jvf->bci();
 594   _class_holder = _method->method_holder()->klass_holder();
 595   _locked_monitors = NULL;
 596   if (with_lock_info) {
 597     ResourceMark rm;
 598     GrowableArray<MonitorInfo*>* list = jvf->locked_monitors();
 599     int length = list->length();
 600     if (length > 0) {
 601       _locked_monitors = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<oop>(length, true);
 602       for (int i = 0; i < length; i++) {
 603         MonitorInfo* monitor = list->at(i);
 604         assert(monitor->owner() != NULL, "This monitor must have an owning object");
 605         _locked_monitors->append(monitor->owner());
 606       }
 607     }
 608   }
 609 }
 610 
 611 void StackFrameInfo::oops_do(OopClosure* f) {
 612   if (_locked_monitors != NULL) {
 613     int length = _locked_monitors->length();
 614     for (int i = 0; i < length; i++) {
 615       f->do_oop((oop*) _locked_monitors->adr_at(i));
 616     }
 617   }
 618   f->do_oop(&_class_holder);
 619 }
 620 
 621 void StackFrameInfo::metadata_do(void f(Metadata*)) {
 622   f(_method);
 623 }
 624 
 625 void StackFrameInfo::print_on(outputStream* st) const {
 626   ResourceMark rm;
 627   java_lang_Throwable::print_stack_element(st, method(), bci());
 628   int len = (_locked_monitors != NULL ? _locked_monitors->length() : 0);
 629   for (int i = 0; i < len; i++) {
 630     oop o = _locked_monitors->at(i);
 631     st->print_cr("\t- locked <" INTPTR_FORMAT "> (a %s)", p2i(o), o->klass()->external_name());
 632   }
 633 
 634 }
 635 
 636 // Iterate through monitor cache to find JNI locked monitors
 637 class InflatedMonitorsClosure: public MonitorClosure {
 638 private:
 639   ThreadStackTrace* _stack_trace;
 640   Thread* _thread;
 641 public:
 642   InflatedMonitorsClosure(Thread* t, ThreadStackTrace* st) {
 643     _thread = t;
 644     _stack_trace = st;
 645   }
 646   void do_monitor(ObjectMonitor* mid) {
 647     if (mid->owner() == _thread) {
 648       oop object = (oop) 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 
 656 ThreadStackTrace::ThreadStackTrace(JavaThread* t, bool with_locked_monitors) {
 657   _thread = t;
 658   _frames = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<StackFrameInfo*>(INITIAL_ARRAY_SIZE, true);
 659   _depth = 0;
 660   _with_locked_monitors = with_locked_monitors;
 661   if (_with_locked_monitors) {
 662     _jni_locked_monitors = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<oop>(INITIAL_ARRAY_SIZE, true);
 663   } else {
 664     _jni_locked_monitors = NULL;
 665   }
 666 }
 667 
 668 ThreadStackTrace::~ThreadStackTrace() {
 669   for (int i = 0; i < _frames->length(); i++) {
 670     delete _frames->at(i);
 671   }
 672   delete _frames;
 673   if (_jni_locked_monitors != NULL) {
 674     delete _jni_locked_monitors;
 675   }
 676 }
 677 
 678 void ThreadStackTrace::dump_stack_at_safepoint(int maxDepth) {
 679   assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
 680 
 681   if (_thread->has_last_Java_frame()) {
 682     RegisterMap reg_map(_thread);
 683     vframe* start_vf = _thread->last_java_vframe(&reg_map);
 684     int count = 0;
 685     for (vframe* f = start_vf; f; f = f->sender() ) {
 686       if (maxDepth >= 0 && count == maxDepth) {
 687         // Skip frames if more than maxDepth
 688         break;
 689       }
 690       if (f->is_java_frame()) {
 691         javaVFrame* jvf = javaVFrame::cast(f);
 692         add_stack_frame(jvf);
 693         count++;
 694       } else {
 695         // Ignore non-Java frames
 696       }
 697     }
 698   }
 699 
 700   if (_with_locked_monitors) {
 701     // Iterate inflated monitors and find monitors locked by this thread
 702     // not found in the stack
 703     InflatedMonitorsClosure imc(_thread, this);
 704     ObjectSynchronizer::monitors_iterate(&imc);
 705   }
 706 }
 707 
 708 
 709 bool ThreadStackTrace::is_owned_monitor_on_stack(oop object) {
 710   assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
 711 
 712   bool found = false;
 713   int num_frames = get_stack_depth();
 714   for (int depth = 0; depth < num_frames; depth++) {
 715     StackFrameInfo* frame = stack_frame_at(depth);
 716     int len = frame->num_locked_monitors();
 717     GrowableArray<oop>* locked_monitors = frame->locked_monitors();
 718     for (int j = 0; j < len; j++) {
 719       oop monitor = locked_monitors->at(j);
 720       assert(monitor != NULL, "must be a Java object");
 721       if (monitor == object) {
 722         found = true;
 723         break;
 724       }
 725     }
 726   }
 727   return found;
 728 }
 729 
 730 Handle ThreadStackTrace::allocate_fill_stack_trace_element_array(TRAPS) {
 731   InstanceKlass* ik = SystemDictionary::StackTraceElement_klass();
 732   assert(ik != NULL, "must be loaded in 1.4+");
 733 
 734   // Allocate an array of java/lang/StackTraceElement object
 735   objArrayOop ste = oopFactory::new_objArray(ik, _depth, CHECK_NH);
 736   objArrayHandle backtrace(THREAD, ste);
 737   for (int j = 0; j < _depth; j++) {
 738     StackFrameInfo* frame = _frames->at(j);
 739     methodHandle mh(THREAD, frame->method());
 740     oop element = java_lang_StackTraceElement::create(mh, frame->bci(), CHECK_NH);
 741     backtrace->obj_at_put(j, element);
 742   }
 743   return backtrace;
 744 }
 745 
 746 void ThreadStackTrace::add_stack_frame(javaVFrame* jvf) {
 747   StackFrameInfo* frame = new StackFrameInfo(jvf, _with_locked_monitors);
 748   _frames->append(frame);
 749   _depth++;
 750 }
 751 
 752 void ThreadStackTrace::oops_do(OopClosure* f) {
 753   int length = _frames->length();
 754   for (int i = 0; i < length; i++) {
 755     _frames->at(i)->oops_do(f);
 756   }
 757 
 758   length = (_jni_locked_monitors != NULL ? _jni_locked_monitors->length() : 0);
 759   for (int j = 0; j < length; j++) {
 760     f->do_oop((oop*) _jni_locked_monitors->adr_at(j));
 761   }
 762 }
 763 
 764 void ThreadStackTrace::metadata_do(void f(Metadata*)) {
 765   int length = _frames->length();
 766   for (int i = 0; i < length; i++) {
 767     _frames->at(i)->metadata_do(f);
 768   }
 769 }
 770 
 771 
 772 ConcurrentLocksDump::~ConcurrentLocksDump() {
 773   if (_retain_map_on_free) {
 774     return;
 775   }
 776 
 777   for (ThreadConcurrentLocks* t = _map; t != NULL;)  {
 778     ThreadConcurrentLocks* tcl = t;
 779     t = t->next();
 780     delete tcl;
 781   }
 782 }
 783 
 784 void ConcurrentLocksDump::dump_at_safepoint() {
 785   // dump all locked concurrent locks
 786   assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
 787 
 788   GrowableArray<oop>* aos_objects = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<oop>(INITIAL_ARRAY_SIZE, true /* C_heap */);
 789 
 790   // Find all instances of AbstractOwnableSynchronizer
 791   HeapInspection::find_instances_at_safepoint(SystemDictionary::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass(),
 792                                               aos_objects);
 793   // Build a map of thread to its owned AQS locks
 794   build_map(aos_objects);
 795 
 796   delete aos_objects;
 797 }
 798 
 799 
 800 // build a map of JavaThread to all its owned AbstractOwnableSynchronizer
 801 void ConcurrentLocksDump::build_map(GrowableArray<oop>* aos_objects) {
 802   int length = aos_objects->length();
 803   for (int i = 0; i < length; i++) {
 804     oop o = aos_objects->at(i);
 805     oop owner_thread_obj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(o);
 806     if (owner_thread_obj != NULL) {
 807       // See comments in ThreadConcurrentLocks to see how this
 808       // JavaThread* is protected.
 809       JavaThread* thread = java_lang_Thread::thread(owner_thread_obj);
 810       assert(o->is_instance(), "Must be an instanceOop");
 811       add_lock(thread, (instanceOop) o);
 812     }
 813   }
 814 }
 815 
 816 void ConcurrentLocksDump::add_lock(JavaThread* thread, instanceOop o) {
 817   ThreadConcurrentLocks* tcl = thread_concurrent_locks(thread);
 818   if (tcl != NULL) {
 819     tcl->add_lock(o);
 820     return;
 821   }
 822 
 823   // First owned lock found for this thread
 824   tcl = new ThreadConcurrentLocks(thread);
 825   tcl->add_lock(o);
 826   if (_map == NULL) {
 827     _map = tcl;
 828   } else {
 829     _last->set_next(tcl);
 830   }
 831   _last = tcl;
 832 }
 833 
 834 ThreadConcurrentLocks* ConcurrentLocksDump::thread_concurrent_locks(JavaThread* thread) {
 835   for (ThreadConcurrentLocks* tcl = _map; tcl != NULL; tcl = tcl->next()) {
 836     if (tcl->java_thread() == thread) {
 837       return tcl;
 838     }
 839   }
 840   return NULL;
 841 }
 842 
 843 void ConcurrentLocksDump::print_locks_on(JavaThread* t, outputStream* st) {
 844   st->print_cr("   Locked ownable synchronizers:");
 845   ThreadConcurrentLocks* tcl = thread_concurrent_locks(t);
 846   GrowableArray<instanceOop>* locks = (tcl != NULL ? tcl->owned_locks() : NULL);
 847   if (locks == NULL || locks->is_empty()) {
 848     st->print_cr("\t- None");
 849     st->cr();
 850     return;
 851   }
 852 
 853   for (int i = 0; i < locks->length(); i++) {
 854     instanceOop obj = locks->at(i);
 855     st->print_cr("\t- <" INTPTR_FORMAT "> (a %s)", p2i(obj), obj->klass()->external_name());
 856   }
 857   st->cr();
 858 }
 859 
 860 ThreadConcurrentLocks::ThreadConcurrentLocks(JavaThread* thread) {
 861   _thread = thread;
 862   _owned_locks = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<instanceOop>(INITIAL_ARRAY_SIZE, true);
 863   _next = NULL;
 864 }
 865 
 866 ThreadConcurrentLocks::~ThreadConcurrentLocks() {
 867   delete _owned_locks;
 868 }
 869 
 870 void ThreadConcurrentLocks::add_lock(instanceOop o) {
 871   _owned_locks->append(o);
 872 }
 873 
 874 void ThreadConcurrentLocks::oops_do(OopClosure* f) {
 875   int length = _owned_locks->length();
 876   for (int i = 0; i < length; i++) {
 877     f->do_oop((oop*) _owned_locks->adr_at(i));
 878   }
 879 }
 880 
 881 ThreadStatistics::ThreadStatistics() {
 882   _contended_enter_count = 0;
 883   _monitor_wait_count = 0;
 884   _sleep_count = 0;
 885   _count_pending_reset = false;
 886   _timer_pending_reset = false;
 887   memset((void*) _perf_recursion_counts, 0, sizeof(_perf_recursion_counts));
 888 }
 889 
 890 void ThreadSnapshot::initialize(ThreadsList * t_list, JavaThread* thread) {
 891   _thread = thread;
 892   _threadObj = thread->threadObj();
 893 
 894   ThreadStatistics* stat = thread->get_thread_stat();
 895   _contended_enter_ticks = stat->contended_enter_ticks();
 896   _contended_enter_count = stat->contended_enter_count();
 897   _monitor_wait_ticks = stat->monitor_wait_ticks();
 898   _monitor_wait_count = stat->monitor_wait_count();
 899   _sleep_ticks = stat->sleep_ticks();
 900   _sleep_count = stat->sleep_count();
 901 
 902   _thread_status = java_lang_Thread::get_thread_status(_threadObj);
 903   _is_ext_suspended = thread->is_being_ext_suspended();
 904   _is_in_native = (thread->thread_state() == _thread_in_native);
 905 
 906   if (_thread_status == java_lang_Thread::BLOCKED_ON_MONITOR_ENTER ||
 907       _thread_status == java_lang_Thread::IN_OBJECT_WAIT ||
 908       _thread_status == java_lang_Thread::IN_OBJECT_WAIT_TIMED) {
 909 
 910     Handle obj = ThreadService::get_current_contended_monitor(thread);
 911     if (obj() == NULL) {
 912       // monitor no longer exists; thread is not blocked
 913       _thread_status = java_lang_Thread::RUNNABLE;
 914     } else {
 915       _blocker_object = obj();
 916       JavaThread* owner = ObjectSynchronizer::get_lock_owner(t_list, obj);
 917       if ((owner == NULL && _thread_status == java_lang_Thread::BLOCKED_ON_MONITOR_ENTER)
 918           || (owner != NULL && owner->is_attaching_via_jni())) {
 919         // ownership information of the monitor is not available
 920         // (may no longer be owned or releasing to some other thread)
 921         // make this thread in RUNNABLE state.
 922         // And when the owner thread is in attaching state, the java thread
 923         // is not completely initialized. For example thread name and id
 924         // and may not be set, so hide the attaching thread.
 925         _thread_status = java_lang_Thread::RUNNABLE;
 926         _blocker_object = NULL;
 927       } else if (owner != NULL) {
 928         _blocker_object_owner = owner->threadObj();
 929       }
 930     }
 931   }
 932 
 933   // Support for JSR-166 locks
 934   if (_thread_status == java_lang_Thread::PARKED || _thread_status == java_lang_Thread::PARKED_TIMED) {
 935     _blocker_object = thread->current_park_blocker();
 936     if (_blocker_object != NULL && _blocker_object->is_a(SystemDictionary::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass())) {
 937       _blocker_object_owner = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(_blocker_object);
 938     }
 939   }
 940 }
 941 
 942 ThreadSnapshot::~ThreadSnapshot() {
 943   delete _stack_trace;
 944   delete _concurrent_locks;
 945 }
 946 
 947 void ThreadSnapshot::dump_stack_at_safepoint(int max_depth, bool with_locked_monitors) {
 948   _stack_trace = new ThreadStackTrace(_thread, with_locked_monitors);
 949   _stack_trace->dump_stack_at_safepoint(max_depth);
 950 }
 951 
 952 
 953 void ThreadSnapshot::oops_do(OopClosure* f) {
 954   f->do_oop(&_threadObj);
 955   f->do_oop(&_blocker_object);
 956   f->do_oop(&_blocker_object_owner);
 957   if (_stack_trace != NULL) {
 958     _stack_trace->oops_do(f);
 959   }
 960   if (_concurrent_locks != NULL) {
 961     _concurrent_locks->oops_do(f);
 962   }
 963 }
 964 
 965 void ThreadSnapshot::metadata_do(void f(Metadata*)) {
 966   if (_stack_trace != NULL) {
 967     _stack_trace->metadata_do(f);
 968   }
 969 }
 970 
 971 
 972 DeadlockCycle::DeadlockCycle() {
 973   _is_deadlock = false;
 974   _threads = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JavaThread*>(INITIAL_ARRAY_SIZE, true);
 975   _next = NULL;
 976 }
 977 
 978 DeadlockCycle::~DeadlockCycle() {
 979   delete _threads;
 980 }
 981 
 982 void DeadlockCycle::print_on_with(ThreadsList * t_list, outputStream* st) const {
 983   st->cr();
 984   st->print_cr("Found one Java-level deadlock:");
 985   st->print("=============================");
 986 
 987   JavaThread* currentThread;
 988   JvmtiRawMonitor* waitingToLockRawMonitor;
 989   oop waitingToLockBlocker;
 990   int len = _threads->length();
 991   for (int i = 0; i < len; i++) {
 992     currentThread = _threads->at(i);
 993     // This code is called at a safepoint so this ObjectMonitorHandle
 994     // is not strictly necessary.
 995     ObjectMonitorHandle omh;
 996     ObjectMonitor* waitingToLockMonitor = currentThread->current_pending_monitor(&omh);
 997     waitingToLockRawMonitor = currentThread->current_pending_raw_monitor();
 998     waitingToLockBlocker = currentThread->current_park_blocker();
 999     st->cr();
1000     st->print_cr("\"%s\":", currentThread->get_thread_name());
1001     const char* owner_desc = ",\n  which is held by";
1002 
1003     // Note: As the JVM TI "monitor contended enter" event callback is executed after ObjectMonitor
1004     // sets the current pending monitor, it is possible to then see a pending raw monitor as well.
1005     if (waitingToLockRawMonitor != NULL) {
1006       st->print("  waiting to lock JVM TI raw monitor " INTPTR_FORMAT, p2i(waitingToLockRawMonitor));
1007       Thread* owner = waitingToLockRawMonitor->owner();
1008       // Could be NULL as the raw monitor could be released at any time if held by non-JavaThread
1009       if (owner != NULL) {
1010         if (owner->is_Java_thread()) {
1011           currentThread = (JavaThread*) owner;
1012           st->print_cr("%s \"%s\"", owner_desc, currentThread->get_thread_name());
1013         } else {
1014           st->print_cr(",\n  which has now been released");
1015         }
1016       } else {
1017         st->print_cr("%s non-Java thread=" PTR_FORMAT, owner_desc, p2i(owner));
1018       }
1019     }
1020 
1021     if (waitingToLockMonitor != NULL) {
1022       st->print("  waiting to lock monitor " INTPTR_FORMAT, p2i(waitingToLockMonitor));
1023       oop obj = (oop)waitingToLockMonitor->object();
1024       st->print(" (object " INTPTR_FORMAT ", a %s)", p2i(obj),
1025                  obj->klass()->external_name());
1026 
1027       if (!currentThread->current_pending_monitor_is_from_java()) {
1028         owner_desc = "\n  in JNI, which is held by";
1029       }
1030       currentThread = Threads::owning_thread_from_monitor_owner(t_list,
1031                                                                 (address)waitingToLockMonitor->owner());
1032       if (currentThread == NULL) {
1033         // The deadlock was detected at a safepoint so the JavaThread
1034         // that owns waitingToLockMonitor should be findable, but
1035         // if it is not findable, then the previous currentThread is
1036         // blocked permanently.
1037         st->print_cr("%s UNKNOWN_owner_addr=" PTR_FORMAT, owner_desc,
1038                   p2i(waitingToLockMonitor->owner()));
1039         continue;
1040       }
1041     } else {
1042       st->print("  waiting for ownable synchronizer " INTPTR_FORMAT ", (a %s)",
1043                 p2i(waitingToLockBlocker),
1044                 waitingToLockBlocker->klass()->external_name());
1045       assert(waitingToLockBlocker->is_a(SystemDictionary::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass()),
1046              "Must be an AbstractOwnableSynchronizer");
1047       oop ownerObj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(waitingToLockBlocker);
1048       currentThread = java_lang_Thread::thread(ownerObj);
1049       assert(currentThread != NULL, "AbstractOwnableSynchronizer owning thread is unexpectedly NULL");
1050     }
1051     st->print_cr("%s \"%s\"", owner_desc, currentThread->get_thread_name());
1052   }
1053 
1054   st->cr();
1055 
1056   // Print stack traces
1057   bool oldJavaMonitorsInStackTrace = JavaMonitorsInStackTrace;
1058   JavaMonitorsInStackTrace = true;
1059   st->print_cr("Java stack information for the threads listed above:");
1060   st->print_cr("===================================================");
1061   for (int j = 0; j < len; j++) {
1062     currentThread = _threads->at(j);
1063     st->print_cr("\"%s\":", currentThread->get_thread_name());
1064     currentThread->print_stack_on(st);
1065   }
1066   JavaMonitorsInStackTrace = oldJavaMonitorsInStackTrace;
1067 }
1068 
1069 ThreadsListEnumerator::ThreadsListEnumerator(Thread* cur_thread,
1070                                              bool include_jvmti_agent_threads,
1071                                              bool include_jni_attaching_threads) {
1072   assert(cur_thread == Thread::current(), "Check current thread");
1073 
1074   int init_size = ThreadService::get_live_thread_count();
1075   _threads_array = new GrowableArray<instanceHandle>(init_size);
1076 
1077   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
1078     // skips JavaThreads in the process of exiting
1079     // and also skips VM internal JavaThreads
1080     // Threads in _thread_new or _thread_new_trans state are included.
1081     // i.e. threads have been started but not yet running.
1082     if (jt->threadObj() == NULL   ||
1083         jt->is_exiting() ||
1084         !java_lang_Thread::is_alive(jt->threadObj())   ||
1085         jt->is_hidden_from_external_view()) {
1086       continue;
1087     }
1088 
1089     // skip agent threads
1090     if (!include_jvmti_agent_threads && jt->is_jvmti_agent_thread()) {
1091       continue;
1092     }
1093 
1094     // skip jni threads in the process of attaching
1095     if (!include_jni_attaching_threads && jt->is_attaching_via_jni()) {
1096       continue;
1097     }
1098 
1099     instanceHandle h(cur_thread, (instanceOop) jt->threadObj());
1100     _threads_array->append(h);
1101   }
1102 }