3

当我向我的 java 进程发送 SIGQUIT 命令(使用 kill -3 或 kill -QUIT )时,它会将所有堆栈的跟踪打印到 stderr,其中包含有关持有的锁和死锁检测的信息。我可以从程序内部以某种方式触发它吗?每次某个操作花费太长时间时,我都想自动执行此操作。

我知道可以获得堆栈跟踪(请参阅Is there a way to dump a stack trace without throwing an exception in java?Thread dump programmatically /JDI (Java Debugger Interface))。但我想查看全部的所有内容:堆栈跟踪、线程状态、持有的锁、阻塞的锁、死锁检测等,即我发送 SIGQUIT 时得到的所有内容;不仅仅是堆栈跟踪。

4

4 回答 4

5

是的你可以。我一直在成功地使用此代码来调试随机触发的并发错误:

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);
        }
    }
}

它需要 Java 8 和 HotSpot 作为 JVM,因为它模仿 jstack 正在做的事情,除了在同一个进程中。

于 2015-10-21T23:42:11.507 回答
0

不是一个完整的解决方案,但它应该给你方向:

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;
}
于 2012-10-19T08:22:10.113 回答
0

我找到了这个选项

ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();

for (ThreadInfo ti : threadMxBean.dumpAllThreads(true, true)) {
    System.out.print(ti.toString());
}
于 2019-11-06T15:09:41.193 回答
0

我使用此代码获取带有胎面转储信息的字符串

    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();
    }
于 2019-03-01T10:19:35.223 回答