1/*
2 * Copyright (c) 2005, 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 * 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                                             (ti.isDaemon() ? " daemon" : "") +
38                                             " Id=" + ti.getThreadId() +
39                                             " in " + ti.getThreadState());
40        if (ti.getLockName() != null) {
41            sb.append(" on lock=" + ti.getLockName());
42        }
43        if (ti.isSuspended()) {
44            sb.append(" (suspended)");
45        }
46        if (ti.isInNative()) {
47            sb.append(" (running in native)");
48        }
49        System.out.println(sb.toString());
50        if (ti.getLockOwnerName() != null) {
51             System.out.println(INDENT + " owned by " + ti.getLockOwnerName() +
52                                " Id=" + ti.getLockOwnerId());
53        }
54        StackTraceElement[] stacktrace = ti.getStackTrace();
55        MonitorInfo[] monitors = ti.getLockedMonitors();
56        for (int i = 0; i < stacktrace.length; i++) {
57            StackTraceElement ste = stacktrace[i];
58            System.out.println(INDENT + "at " + ste.toString());
59
60            for (MonitorInfo mi : monitors) {
61                if (mi.getLockedStackDepth() == i) {
62                    System.out.println(INDENT + "  - locked " + mi);
63                }
64            }
65        }
66        System.out.println();
67    }
68
69    public static void printStack(StackTraceElement[] stack) {
70        System.out.println(INDENT + "Stack: (length = " + stack.length + ")");
71        for (int j = 0; j < stack.length; j++) {
72            System.out.println(INDENT + INDENT + stack[j]);
73        }
74        System.out.println();
75    }
76
77    public static void dumpStacks() {
78        // Get stack traces of all Threads
79        Map m = Thread.getAllStackTraces();
80        Set s = m.entrySet();
81        Iterator iter = s.iterator();
82
83        Map.Entry entry;
84        while (iter.hasNext()) {
85            entry = (Map.Entry) iter.next();
86            Thread t = (Thread) entry.getKey();
87            StackTraceElement[] stack = (StackTraceElement[]) entry.getValue();
88            System.out.println(t);
89            printStack(stack);
90        }
91    }
92
93    public static void printLockInfo(LockInfo[] locks) {
94       System.out.println(INDENT + "Locked synchronizers: count = " + locks.length);
95       for (LockInfo li : locks) {
96           System.out.println(INDENT + "  - " + li);
97       }
98    }
99
100    static ThreadMXBean tmbean = ManagementFactory.getThreadMXBean();
101    public static void threadDump() {
102       System.out.println("Full Java thread dump");
103       ThreadInfo[] tinfos = tmbean.dumpAllThreads(true, true);
104       for (ThreadInfo ti : tinfos) {
105           printThreadInfo(ti);
106           LockInfo[] syncs = ti.getLockedSynchronizers();
107           printLockInfo(syncs);
108           System.out.println();
109       }
110    }
111
112}
113