19 package org.sleuthkit.autopsy.actions;
21 import java.awt.Desktop;
22 import java.awt.event.ActionListener;
23 import java.io.BufferedWriter;
25 import java.io.FileWriter;
26 import java.io.IOException;
27 import java.lang.management.ManagementFactory;
28 import java.lang.management.ThreadInfo;
29 import java.lang.management.ThreadMXBean;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.text.DateFormat;
33 import java.text.SimpleDateFormat;
34 import java.util.Arrays;
35 import java.util.Date;
36 import java.util.concurrent.ExecutionException;
37 import java.util.logging.Level;
38 import java.util.stream.Collectors;
39 import javax.swing.SwingWorker;
40 import org.openide.awt.ActionID;
41 import org.openide.awt.ActionReference;
42 import org.openide.awt.ActionRegistration;
43 import org.openide.util.HelpCtx;
44 import org.openide.util.NbBundle.Messages;
45 import org.openide.util.actions.CallableSystemAction;
55 @ActionID(category =
"Help",
id =
"org.sleuthkit.autopsy.actions.ThreadDumpAction")
56 @ActionRegistration(displayName =
"#CTL_DumpThreadAction", lazy =
false)
57 @ActionReference(path =
"Menu/Help", position = 1750)
59 "CTL_DumpThreadAction=Thread Dump"
61 public final class ThreadDumpAction extends CallableSystemAction implements ActionListener {
63 private static final long serialVersionUID = 1L;
66 private static final DateFormat DATE_FORMAT =
new SimpleDateFormat(
"MM-dd-yyyy-HH-mm-ss-SSSS");
75 return Bundle.CTL_DumpThreadAction();
80 return HelpCtx.DEFAULT_HELP;
91 return createThreadDump();
99 Desktop.getDesktop().open(dumpFile);
100 }
catch (ExecutionException | InterruptedException ex) {
101 logger.log(Level.SEVERE,
"Failure occurred while creating thread dump file", ex);
102 }
catch (IOException ex) {
103 if (dumpFile != null) {
104 logger.log(Level.WARNING,
"Failed to open thread dump file in external viewer: " + dumpFile.getAbsolutePath(), ex);
106 logger.log(Level.SEVERE,
"Failed to create thread dump file.", ex);
117 File dumpFile = createFilePath().toFile();
118 try (BufferedWriter writer =
new BufferedWriter(
new FileWriter(dumpFile,
true))) {
119 ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
120 ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), 100);
121 for (ThreadInfo threadInfo : threadInfos) {
122 writer.write(threadInfo.toString());
126 long[] deadlockThreadIds = threadMXBean.findDeadlockedThreads();
127 if (deadlockThreadIds != null) {
128 writer.write(
"-------------------List of Deadlocked Thread IDs ---------------------");
129 String idsList = (Arrays
130 .stream(deadlockThreadIds)
132 .collect(Collectors.toList()))
133 .stream().map(n -> String.valueOf(n))
134 .collect(Collectors.joining(
"-",
"{",
"}"));
135 writer.write(idsList);
148 String fileName =
"ThreadDump_" + DATE_FORMAT.format(
new Date()) +
".txt";
String getLogDirectoryPath()
static Case getCurrentCase()
synchronized static Logger getLogger(String name)
static boolean isCaseOpen()