2009-04-28

Dump java stack traces

This is a small utility function to programmatically dump all stack traces of a java process similar to the output of kill -QUIT on unix platforms. Use at your own peril, no warranties, no royalties :)
public static void dumpStackTraces(PrintStream out) {
    synchronized (out) {
        Map<Thread, StackTraceElement[]> sts = Thread.getAllStackTraces();
        for (Thread t : sts.keySet()) {
            StackTraceElement[] st = sts.get(t);
            out.println(t.getName() + "[" + t.getId() + "]" + "(" + t.getState() + (t.isDaemon() ? ")(DAEMON):" : "):"));
            for (int i = 0; i < st.length; ++i) {
                out.println("\t" + st[i]);
            }
        }
    }
}

No comments: