Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/management/ThreadMXBean/ThreadDump.java
38821 views
/*1* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* Thread Dump utility class for printing25* @author Mandy Chung26*/2728import java.lang.management.*;29import java.util.*;3031public class ThreadDump {32private static final String INDENT = " ";3334public static void printThreadInfo(ThreadInfo ti) {35StringBuilder sb = new StringBuilder("\"" + ti.getThreadName() + "\"" +36" Id=" + ti.getThreadId() +37" in " + ti.getThreadState());38if (ti.getLockName() != null) {39sb.append(" on lock=" + ti.getLockName());40}41if (ti.isSuspended()) {42sb.append(" (suspended)");43}44if (ti.isInNative()) {45sb.append(" (running in native)");46}47System.out.println(sb.toString());48if (ti.getLockOwnerName() != null) {49System.out.println(INDENT + " owned by " + ti.getLockOwnerName() +50" Id=" + ti.getLockOwnerId());51}52StackTraceElement[] stacktrace = ti.getStackTrace();53MonitorInfo[] monitors = ti.getLockedMonitors();54for (int i = 0; i < stacktrace.length; i++) {55StackTraceElement ste = stacktrace[i];56System.out.println(INDENT + "at " + ste.toString());5758for (MonitorInfo mi : monitors) {59if (mi.getLockedStackDepth() == i) {60System.out.println(INDENT + " - locked " + mi);61}62}63}64System.out.println();65}6667public static void printStack(StackTraceElement[] stack) {68System.out.println(INDENT + "Stack: (length = " + stack.length + ")");69for (int j = 0; j < stack.length; j++) {70System.out.println(INDENT + INDENT + stack[j]);71}72System.out.println();73}7475public static void dumpStacks() {76// Get stack traces of all Threads77Map m = Thread.getAllStackTraces();78Set s = m.entrySet();79Iterator iter = s.iterator();8081Map.Entry entry;82while (iter.hasNext()) {83entry = (Map.Entry) iter.next();84Thread t = (Thread) entry.getKey();85StackTraceElement[] stack = (StackTraceElement[]) entry.getValue();86System.out.println(t);87printStack(stack);88}89}9091public static void printLockInfo(LockInfo[] locks) {92System.out.println(INDENT + "Locked synchronizers: count = " + locks.length);93for (LockInfo li : locks) {94System.out.println(INDENT + " - " + li);95}96}9798static ThreadMXBean tmbean = ManagementFactory.getThreadMXBean();99public static void threadDump() {100System.out.println("Full Java thread dump");101ThreadInfo[] tinfos = tmbean.dumpAllThreads(true, true);102for (ThreadInfo ti : tinfos) {103printThreadInfo(ti);104LockInfo[] syncs = ti.getLockedSynchronizers();105printLockInfo(syncs);106System.out.println();107}108}109110}111112113