Autopsy  4.19.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-2021 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.nio.file.Path;
28 import java.nio.file.Paths;
29 import java.util.concurrent.ExecutionException;
30 import java.util.logging.Level;
31 import javax.swing.SwingWorker;
32 import org.openide.awt.ActionID;
33 import org.openide.awt.ActionReference;
34 import org.openide.awt.ActionRegistration;
35 import org.openide.util.HelpCtx;
36 import org.openide.util.NbBundle.Messages;
37 import org.openide.util.actions.CallableSystemAction;
43 
49 @ActionID(category = "Help", id = "org.sleuthkit.autopsy.actions.ThreadDumpAction")
50 @ActionRegistration(displayName = "#CTL_DumpThreadAction", lazy = false)
51 @ActionReference(path = "Menu/Help", position = 1750)
52 @Messages({
53  "CTL_DumpThreadAction=Thread Dump"
54 })
55 public final class ThreadDumpAction extends CallableSystemAction implements ActionListener {
56 
57  private static final long serialVersionUID = 1L;
58  private static final Logger logger = Logger.getLogger(ThreadDumpAction.class.getName());
59 
60  @Override
61  public void performAction() {
62  (new ThreadDumper()).run();
63  }
64 
65  @Override
66  public String getName() {
67  return Bundle.CTL_DumpThreadAction();
68  }
69 
70  @Override
71  public HelpCtx getHelpCtx() {
72  return HelpCtx.DEFAULT_HELP;
73  }
74 
79  private final class ThreadDumper extends SwingWorker<File, Void> {
80 
81  @Override
82  protected File doInBackground() throws Exception {
83  return createThreadDump();
84  }
85 
86  @Override
87  protected void done() {
88  File dumpFile = null;
89  try {
90  dumpFile = get();
91  Desktop.getDesktop().open(dumpFile);
92  } catch (ExecutionException | InterruptedException ex) {
93  logger.log(Level.SEVERE, "Failure occurred while creating thread dump file", ex);
94  } catch (IOException ex) {
95  if (dumpFile != null) {
96  logger.log(Level.WARNING, "Failed to open thread dump file in external viewer: " + dumpFile.getAbsolutePath(), ex);
97  } else {
98  logger.log(Level.SEVERE, "Failed to create thread dump file.", ex);
99  }
100  }
101  }
102 
108  private File createThreadDump() throws IOException {
109 
110  // generate thread dump
111  String threadDump = ThreadUtils.generateThreadDump();
112 
113  File dumpFile = createFilePath().toFile();
114  try (BufferedWriter writer = new BufferedWriter(new FileWriter(dumpFile, true))) {
115  writer.write(threadDump);
116  }
117 
118  return dumpFile;
119  }
120 
126  private Path createFilePath() {
127  String fileName = "ThreadDump_" + TimeStampUtils.createTimeStamp() + ".txt";
128  if (Case.isCaseOpen()) {
129  return Paths.get(Case.getCurrentCase().getLogDirectoryPath(), fileName);
130  }
131  return Paths.get(PlatformUtil.getLogDirectory(), fileName);
132  }
133  }
134 
135 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

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