0N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * Thread Dump utility class for printing
0N/A * @author Mandy Chung
0N/A */
0N/A
0N/Aimport java.lang.management.*;
0N/Aimport java.util.*;
0N/A
0N/Apublic class ThreadDump {
0N/A private static final String INDENT = " ";
0N/A
0N/A public static void printThreadInfo(ThreadInfo ti) {
0N/A StringBuilder sb = new StringBuilder("\"" + ti.getThreadName() + "\"" +
0N/A " Id=" + ti.getThreadId() +
0N/A " in " + ti.getThreadState());
0N/A if (ti.getLockName() != null) {
0N/A sb.append(" on lock=" + ti.getLockName());
0N/A }
0N/A if (ti.isSuspended()) {
0N/A sb.append(" (suspended)");
0N/A }
0N/A if (ti.isInNative()) {
0N/A sb.append(" (running in native)");
0N/A }
0N/A System.out.println(sb.toString());
0N/A if (ti.getLockOwnerName() != null) {
0N/A System.out.println(INDENT + " owned by " + ti.getLockOwnerName() +
0N/A " Id=" + ti.getLockOwnerId());
0N/A }
0N/A StackTraceElement[] stacktrace = ti.getStackTrace();
0N/A MonitorInfo[] monitors = ti.getLockedMonitors();
0N/A for (int i = 0; i < stacktrace.length; i++) {
0N/A StackTraceElement ste = stacktrace[i];
0N/A System.out.println(INDENT + "at " + ste.toString());
0N/A
0N/A for (MonitorInfo mi : monitors) {
0N/A if (mi.getLockedStackDepth() == i) {
0N/A System.out.println(INDENT + " - locked " + mi);
0N/A }
0N/A }
0N/A }
0N/A System.out.println();
0N/A }
0N/A
0N/A public static void printStack(StackTraceElement[] stack) {
0N/A System.out.println(INDENT + "Stack: (length = " + stack.length + ")");
0N/A for (int j = 0; j < stack.length; j++) {
0N/A System.out.println(INDENT + INDENT + stack[j]);
0N/A }
0N/A System.out.println();
0N/A }
0N/A
0N/A public static void dumpStacks() {
0N/A // Get stack traces of all Threads
0N/A Map m = Thread.getAllStackTraces();
0N/A Set s = m.entrySet();
0N/A Iterator iter = s.iterator();
0N/A
0N/A Map.Entry entry;
0N/A while (iter.hasNext()) {
0N/A entry = (Map.Entry) iter.next();
0N/A Thread t = (Thread) entry.getKey();
0N/A StackTraceElement[] stack = (StackTraceElement[]) entry.getValue();
0N/A System.out.println(t);
0N/A printStack(stack);
0N/A }
0N/A }
0N/A
0N/A public static void printLockInfo(LockInfo[] locks) {
0N/A System.out.println(INDENT + "Locked synchronizers: count = " + locks.length);
0N/A for (LockInfo li : locks) {
0N/A System.out.println(INDENT + " - " + li);
0N/A }
0N/A }
0N/A
0N/A static ThreadMXBean tmbean = ManagementFactory.getThreadMXBean();
0N/A public static void threadDump() {
0N/A System.out.println("Full Java thread dump");
0N/A ThreadInfo[] tinfos = tmbean.dumpAllThreads(true, true);
0N/A for (ThreadInfo ti : tinfos) {
0N/A printThreadInfo(ti);
0N/A LockInfo[] syncs = ti.getLockedSynchronizers();
0N/A printLockInfo(syncs);
0N/A System.out.println();
0N/A }
0N/A }
0N/A
0N/A}