Autopsy  4.17.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
ThreadDumpAction.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2020 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.actions;
20 
21 import java.awt.Desktop;
22 import java.awt.event.ActionListener;
23 import java.io.BufferedWriter;
24 import java.io.File;
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;
49 
55 @ActionID(category = "Help", id = "org.sleuthkit.autopsy.actions.ThreadDumpAction")
56 @ActionRegistration(displayName = "#CTL_DumpThreadAction", lazy = false)
57 @ActionReference(path = "Menu/Help", position = 1750)
58 @Messages({
59  "CTL_DumpThreadAction=Thread Dump"
60 })
61 public final class ThreadDumpAction extends CallableSystemAction implements ActionListener {
62 
63  private static final long serialVersionUID = 1L;
64  private static final Logger logger = Logger.getLogger(ThreadDumpAction.class.getName());
65 
66  private static final DateFormat DATE_FORMAT = new SimpleDateFormat("MM-dd-yyyy-HH-mm-ss-SSSS");
67 
68  @Override
69  public void performAction() {
70  (new ThreadDumper()).run();
71  }
72 
73  @Override
74  public String getName() {
75  return Bundle.CTL_DumpThreadAction();
76  }
77 
78  @Override
79  public HelpCtx getHelpCtx() {
80  return HelpCtx.DEFAULT_HELP;
81  }
82 
87  private final class ThreadDumper extends SwingWorker<File, Void> {
88 
89  @Override
90  protected File doInBackground() throws Exception {
91  return createThreadDump();
92  }
93 
94  @Override
95  protected void done() {
96  File dumpFile = null;
97  try {
98  dumpFile = get();
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);
105  } else {
106  logger.log(Level.SEVERE, "Failed to create thread dump file.", ex);
107  }
108  }
109  }
110 
116  private File createThreadDump() throws IOException {
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());
123  writer.write("\n");
124  }
125 
126  long[] deadlockThreadIds = threadMXBean.findDeadlockedThreads();
127  if (deadlockThreadIds != null) {
128  writer.write("-------------------List of Deadlocked Thread IDs ---------------------");
129  String idsList = (Arrays
130  .stream(deadlockThreadIds)
131  .boxed()
132  .collect(Collectors.toList()))
133  .stream().map(n -> String.valueOf(n))
134  .collect(Collectors.joining("-", "{", "}"));
135  writer.write(idsList);
136  }
137  }
138 
139  return dumpFile;
140  }
141 
147  private Path createFilePath() {
148  String fileName = "ThreadDump_" + DATE_FORMAT.format(new Date()) + ".txt";
149  if (Case.isCaseOpen()) {
150  return Paths.get(Case.getCurrentCase().getLogDirectoryPath(), fileName);
151  }
152  return Paths.get(PlatformUtil.getLogDirectory(), fileName);
153  }
154  }
155 
156 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2021 Basis Technology. Generated on: Tue Jan 19 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.