blob: e262a377d974f21996eceea0d03d8165dc718246 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * Thread Dump utility class for printing
26 * @author Mandy Chung
27 */
28
29import java.lang.management.*;
30import java.util.*;
31
32public class ThreadDump {
33 private static final String INDENT = " ";
34
35 public static void printThreadInfo(ThreadInfo ti) {
36 StringBuilder sb = new StringBuilder("\"" + ti.getThreadName() + "\"" +
37 " Id=" + ti.getThreadId() +
38 " in " + ti.getThreadState());
39 if (ti.getLockName() != null) {
40 sb.append(" on lock=" + ti.getLockName());
41 }
42 if (ti.isSuspended()) {
43 sb.append(" (suspended)");
44 }
45 if (ti.isInNative()) {
46 sb.append(" (running in native)");
47 }
48 System.out.println(sb.toString());
49 if (ti.getLockOwnerName() != null) {
50 System.out.println(INDENT + " owned by " + ti.getLockOwnerName() +
51 " Id=" + ti.getLockOwnerId());
52 }
53 StackTraceElement[] stacktrace = ti.getStackTrace();
54 MonitorInfo[] monitors = ti.getLockedMonitors();
55 for (int i = 0; i < stacktrace.length; i++) {
56 StackTraceElement ste = stacktrace[i];
57 System.out.println(INDENT + "at " + ste.toString());
58
59 for (MonitorInfo mi : monitors) {
60 if (mi.getLockedStackDepth() == i) {
61 System.out.println(INDENT + " - locked " + mi);
62 }
63 }
64 }
65 System.out.println();
66 }
67
68 public static void printStack(StackTraceElement[] stack) {
69 System.out.println(INDENT + "Stack: (length = " + stack.length + ")");
70 for (int j = 0; j < stack.length; j++) {
71 System.out.println(INDENT + INDENT + stack[j]);
72 }
73 System.out.println();
74 }
75
76 public static void dumpStacks() {
77 // Get stack traces of all Threads
78 Map m = Thread.getAllStackTraces();
79 Set s = m.entrySet();
80 Iterator iter = s.iterator();
81
82 Map.Entry entry;
83 while (iter.hasNext()) {
84 entry = (Map.Entry) iter.next();
85 Thread t = (Thread) entry.getKey();
86 StackTraceElement[] stack = (StackTraceElement[]) entry.getValue();
87 System.out.println(t);
88 printStack(stack);
89 }
90 }
91
92 public static void printLockInfo(LockInfo[] locks) {
93 System.out.println(INDENT + "Locked synchronizers: count = " + locks.length);
94 for (LockInfo li : locks) {
95 System.out.println(INDENT + " - " + li);
96 }
97 }
98
99 static ThreadMXBean tmbean = ManagementFactory.getThreadMXBean();
100 public static void threadDump() {
101 System.out.println("Full Java thread dump");
102 ThreadInfo[] tinfos = tmbean.dumpAllThreads(true, true);
103 for (ThreadInfo ti : tinfos) {
104 printThreadInfo(ti);
105 LockInfo[] syncs = ti.getLockedSynchronizers();
106 printLockInfo(syncs);
107 System.out.println();
108 }
109 }
110
111}