// Live decompile. Args: address-or-regex [maxResults]. Matches names by regex (full qualified name) or entry address. import ghidra.app.script.GhidraScript; import ghidra.app.decompiler.*; import ghidra.program.model.address.*; import ghidra.program.model.listing.*; import java.util.regex.*; import java.io.*; public class Decompile extends GhidraScript { public void run() throws Exception { String q = getScriptArgs()[0]; int max = getScriptArgs().length > 1 ? Integer.parseInt(getScriptArgs()[1]) : 20; DecompInterface di = new DecompInterface(); DecompileOptions opts = new DecompileOptions(); opts.setMaxPayloadMBytes(1024); // UseActiveItem-sized switches overflow the 50MB default di.setOptions(opts); di.openProgram(currentProgram); Function only = null; if (q.matches("(0x)?[0-9a-fA-F]{6,8}")) { Address a = toAddr(q.replace("0x", "")); only = getFunctionContaining(a); } Pattern p = Pattern.compile(q); PrintStream o = new PrintStream(new FileOutputStream(getScriptArgs()[2])); int n = 0; for (Function f : currentProgram.getListing().getFunctions(true)) { if (only != null ? f != only : !p.matcher(f.getName(true)).find()) continue; if (n++ >= max) { o.println("// ... more matches truncated"); break; } DecompileResults r = di.decompileFunction(f, Integer.parseInt(System.getenv().getOrDefault("DECOMP_TIMEOUT", "180")), monitor); o.println("//@@ " + f.getEntryPoint() + " " + f.getName(true)); o.println(r.decompileCompleted() ? r.getDecompiledFunction().getC() : "/* failed: " + r.getErrorMessage() + " */"); } o.close(); di.dispose(); } }