// Dumps decompiled C of every function + index, strings-with-xrefs, call edges. Arg: output dir import ghidra.app.script.GhidraScript; import ghidra.app.decompiler.*; import ghidra.program.model.address.*; import ghidra.program.model.listing.*; import ghidra.program.model.symbol.*; import ghidra.program.model.data.StringDataInstance; import java.io.*; import java.util.*; import java.util.concurrent.*; import java.util.concurrent.atomic.*; public class DumpAll extends GhidraScript { public void run() throws Exception { File out = new File(getScriptArgs()[0]); out.mkdirs(); Listing listing = currentProgram.getListing(); ReferenceManager rm = currentProgram.getReferenceManager(); try (PrintWriter idx = new PrintWriter(new File(out, "functions.tsv")); PrintWriter calls = new PrintWriter(new File(out, "calls.tsv"))) { for (Function f : listing.getFunctions(true)) { idx.println(f.getEntryPoint() + "\t" + f.getName(true) + "\t" + f.getBody().getNumAddresses()); for (Function c : f.getCalledFunctions(monitor)) calls.println(f.getEntryPoint() + "\t" + c.getEntryPoint()); } } try (PrintWriter st = new PrintWriter(new File(out, "strings.tsv"))) { for (Data d : listing.getDefinedData(true)) { if (!d.hasStringValue()) continue; String s = String.valueOf(d.getValue()).replace("\t", "\\t").replace("\n", "\\n"); StringBuilder who = new StringBuilder(); for (Reference r : rm.getReferencesTo(d.getAddress())) { Function f = getFunctionContaining(r.getFromAddress()); who.append(f == null ? r.getFromAddress().toString() : f.getEntryPoint() + ":" + f.getName(true)).append(' '); } st.println(d.getAddress() + "\t" + s + "\t" + who.toString().trim()); } } List fns = new ArrayList<>(); for (Function f : listing.getFunctions(true)) if (!f.isThunk()) fns.add(f); int nThreads = Math.max(1, Runtime.getRuntime().availableProcessors() - 2); String[] results = new String[fns.size()]; AtomicInteger next = new AtomicInteger(); List threads = new ArrayList<>(); for (int t = 0; t < nThreads; t++) { Thread th = new Thread(() -> { DecompInterface di = new DecompInterface(); DecompileOptions opts = new DecompileOptions(); opts.setMaxPayloadMBytes(1024); // UseActiveItem-sized switches overflow the 50MB default di.setOptions(opts); di.openProgram(currentProgram); int i; while ((i = next.getAndIncrement()) < fns.size()) { Function f = fns.get(i); DecompileResults r = di.decompileFunction(f, 900, TaskMonitor_dummy()); String c = r != null && r.decompileCompleted() ? r.getDecompiledFunction().getC() : "/* decompile failed: " + (r == null ? "" : r.getErrorMessage()) + " */\n"; results[i] = "//@@ " + f.getEntryPoint() + " " + f.getName(true) + "\n" + c + "\n"; if (i % 2000 == 0) System.out.println("decompiled " + i + "/" + fns.size()); } di.dispose(); }); th.start(); threads.add(th); } for (Thread th : threads) th.join(); try (PrintWriter all = new PrintWriter(new File(out, "all.c"))) { for (String s : results) all.print(s); } println("dumped " + fns.size()); } static ghidra.util.task.TaskMonitor TaskMonitor_dummy() { return ghidra.util.task.TaskMonitor.DUMMY; } }