|
| 1 | +/* ### |
| 2 | + * IP: GHIDRA |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +// Developer Script to dump PDB information from a set of PDB files to output files. |
| 17 | +// User selects an input file in which each line of text is a tab-separated input/output pair |
| 18 | +// of PDB file and output text file. Blank lines and lines beginning with # are ignored. Example: |
| 19 | +// myFile1.pdb myFile1.pdb.txt |
| 20 | +// myFile2.pdb myFile2.pdb.txt |
| 21 | +// |
| 22 | +//@category PDB |
| 23 | + |
| 24 | +import java.io.*; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +import ghidra.app.script.GhidraScript; |
| 29 | +import ghidra.app.util.bin.format.pdb2.pdbreader.*; |
| 30 | +import ghidra.util.exception.CancelledException; |
| 31 | + |
| 32 | +public class PdbDeveloperDumpSetScript extends GhidraScript { |
| 33 | + |
| 34 | + private record IOEntry(String input, String output) {}; |
| 35 | + |
| 36 | + @Override |
| 37 | + protected void run() throws Exception { |
| 38 | + File controlFile = askFile( |
| 39 | + "Choose control file with a tab-separated PDB-input/text-output pair per line", "OK"); |
| 40 | + if (controlFile == null) { |
| 41 | + printerr("Canceled execution: no input file"); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + List<IOEntry> entries = parseControlFile(controlFile); |
| 46 | + if (entries == null) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + dumpFiles(entries); |
| 51 | + } |
| 52 | + |
| 53 | + private List<IOEntry> parseControlFile(File controlFile) throws IOException { |
| 54 | + List<IOEntry> entries = new ArrayList<>(); |
| 55 | + |
| 56 | + try (BufferedReader bufferedReader = new BufferedReader(new FileReader(controlFile))) { |
| 57 | + String line; |
| 58 | + int lineNumber = 0; |
| 59 | + while ((line = bufferedReader.readLine()) != null) { |
| 60 | + lineNumber++; |
| 61 | + if (line.isBlank() || line.trim().startsWith("#")) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + String[] parts = line.split("\\t"); |
| 65 | + if (parts.length != 2) { |
| 66 | + printerr("Canceled execution (no files processed): control file, line " + |
| 67 | + lineNumber + " does not contain a valid, tab-delimited pair: \"" + line + |
| 68 | + "\""); |
| 69 | + return null; |
| 70 | + } |
| 71 | + File inputFile = new File(parts[0]); |
| 72 | + if (!inputFile.exists()) { |
| 73 | + printerr("Canceled execution (no files processed): control file, line " + |
| 74 | + lineNumber + ", input file does not exist: " + parts[0]); |
| 75 | + return null; |
| 76 | + } |
| 77 | + entries.add(new IOEntry(parts[0], parts[1])); |
| 78 | + } |
| 79 | + } |
| 80 | + return entries; |
| 81 | + } |
| 82 | + |
| 83 | + private void dumpFiles(List<IOEntry> entries) throws CancelledException, PdbException { |
| 84 | + for (IOEntry entry : entries) { |
| 85 | + monitor.checkCanceled(); |
| 86 | + println("Processing PDB Dump of: " + entry.input()); |
| 87 | + try (AbstractPdb pdb = |
| 88 | + PdbParser.parse(entry.input(), new PdbReaderOptions(), monitor)) { |
| 89 | + pdb.deserialize(monitor); |
| 90 | + try (BufferedWriter bufferedWriter = |
| 91 | + new BufferedWriter(new FileWriter(new File(entry.output())))) { |
| 92 | + outputHeaderMessage(bufferedWriter, entry.input()); |
| 93 | + pdb.dumpDirectory(bufferedWriter); |
| 94 | + pdb.dumpSubStreams(bufferedWriter); |
| 95 | + println("Results located in: " + entry.output()); |
| 96 | + } |
| 97 | + catch (IOException ioe) { |
| 98 | + printerr("Error writing output file: " + ioe.getMessage()); |
| 99 | + } |
| 100 | + } |
| 101 | + catch (IOException ioe) { |
| 102 | + printerr("Error processing PDB file: " + ioe.getMessage()); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private void outputHeaderMessage(BufferedWriter bufferedWriter, String name) |
| 108 | + throws IOException { |
| 109 | + bufferedWriter.append(getClass().getSimpleName() + " dump of: " + name + |
| 110 | + "\nWARNING: FORMAT SUBJECT TO CHANGE WITHOUT NOTICE--DO NOT PARSE\n\n"); |
| 111 | + } |
| 112 | + |
| 113 | +} |
0 commit comments