3

When I send a SIGQUIT command to my java process (using kill -3 or kill -QUIT ), it prints a trace of all stacks to stderr, with information about locks held, and deadlock detection. Can I trigger this from inside the program somehow? I want to do this automatically every time a certain operation takes too long.

I know it's possible to get a stack trace (see Is there a way to dump a stack trace without throwing an exception in java?, Thread dump programmatically /JDI (Java Debugger Interface)). But I want to see the whole everything: stack traces, thread states, locks held, locks blocked on, deadlock detection, etc., i.e. everything I get when I sent a SIGQUIT; not just the stack trace.

Community
  • 1
  • 1
Dirk Groeneveld
  • 2,547
  • 2
  • 22
  • 23
  • 1
    This is not a duplicate. The other questions only concern themselves with stack traces. I want the whole everything, stack traces, thread states, locks held, locks blocked on, deadlock detection, etc., i.e. everything I get when I sent a SIGQUIT. The closest answer I've found is to use ThreadMXBean, but that's not a complete solution, and either way now I can't add it to this question. – Dirk Groeneveld Oct 20 '12 at 03:16

4 Answers4

6

Yes you can. I've been using this code successfully to debug randomly triggered concurrency bugs:

package utils.stack;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.management.ManagementFactory;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.function.Supplier;
import javax.management.JMX;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;

public interface DiagnosticCommand {
    String threadPrint(String... args);

    DiagnosticCommand local = ((Supplier<DiagnosticCommand>) () -> {
        try {
            MBeanServer server = ManagementFactory.getPlatformMBeanServer();
            ObjectName name = new ObjectName("com.sun.management", 
                "type", "DiagnosticCommand");
            return JMX.newMBeanProxy(server, name, DiagnosticCommand.class);
        } catch(MalformedObjectNameException e) {
            throw new AssertionError(e);
        }
    }).get();

    static void dump() {
        String print = local.threadPrint();
        Path path = Paths.get(LocalDateTime.now() + ".dump.txt");
        try {
            byte[] bytes = print.getBytes("ASCII");
            Files.write(path, bytes);
        } catch(IOException e) {
            throw new UncheckedIOException(e);
        }
    }
}

It requires Java 8 and HotSpot as the JVM as it mimics what jstack is doing, except from within the same process.

Timo Kinnunen
  • 61
  • 1
  • 2
1

I found this option

ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();

for (ThreadInfo ti : threadMxBean.dumpAllThreads(true, true)) {
    System.out.print(ti.toString());
}
Radu Toader
  • 1,521
  • 16
  • 23
0

Not a complete solution, but it should give you the direction:

public StringBuffer getInfoXmlString() {
StringBuffer serverInfo = new StringBuffer(3000);

serverInfo.append("<ServerInfo>");
ThreadGroup rootGroup = Thread.currentThread().getThreadGroup();
ThreadGroup parent;
while ((parent = rootGroup.getParent()) != null) {
  rootGroup = parent;
}

serverInfo.append("<Threads>\r\n");
serverInfo.append(listThreads(rootGroup, "  "));
serverInfo.append("</Threads>\r\n");

serverInfo.append("</ServerInfo>\r\n");
}

private StringBuffer listThreads(ThreadGroup group, String indent) {
StringBuffer threadInfo = new StringBuffer(5000);
threadInfo.append(indent);
threadInfo.append("<ThreadGroup name=\"");
threadInfo.append(group.getName());
threadInfo.append("\" class=\"");
threadInfo.append(group.getClass().getName());
threadInfo.append("\">\r\n");

int nt = group.activeCount();
Thread[] threads = new Thread[nt * 2 + 10];
nt = group.enumerate(threads, false);

// List every thread in the group
for (int i = 0; i < nt; i++) {
  Thread t = threads[i];
  threadInfo.append(indent);
  threadInfo.append("  <Thread name=\"");
  threadInfo.append(t.getName());
  threadInfo.append("\" class=\"");
  threadInfo.append(t.getClass());
  threadInfo.append("\" alive=\"");
  threadInfo.append(t.isAlive());
  threadInfo.append("\"/>\r\n");
}
// Recursively list all subgroups
int ng = group.activeGroupCount();
ThreadGroup[] groups = new ThreadGroup[ng * 2 + 10];
ng = group.enumerate(groups, false);
for (int i = 0; i < ng; i++) {
  threadInfo.append(listThreads(groups[i], indent + "  "));
}
threadInfo.append(indent);
threadInfo.append("</ThreadGroup>\r\n");

return threadInfo;
}
Udo Klimaschewski
  • 5,150
  • 1
  • 28
  • 41
0

I use this code to get string with tread dump info

    public static String generateThreadDump() {
        final StringBuilder dump = new StringBuilder();
        final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        final ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), 100);
        for (ThreadInfo threadInfo : threadInfos) {
            dump.append('"');
            dump.append(threadInfo.getThreadName());
            dump.append("\" ");
            final Thread.State state = threadInfo.getThreadState();
            dump.append("\n   java.lang.Thread.State: ");
            dump.append(state);
            final StackTraceElement[] stackTraceElements = threadInfo.getStackTrace();
            for (final StackTraceElement stackTraceElement : stackTraceElements) {
                dump.append("\n        at ");
                dump.append(stackTraceElement);
            }
            dump.append("\n\n");
        }
        return dump.toString();
    }
bigspawn
  • 1,727
  • 3
  • 16
  • 16