-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPrintCodeHeadless.java
116 lines (105 loc) · 4.82 KB
/
PrintCodeHeadless.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* Copyright (c) 2020 Abc Xyz — All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* analyzeHeadless . temp.gpr -import $BINARY_NAME -preScript PrintCodeHeadless.java $FUNCTION_ADDRESS $TYPE -deleteProject -noanalysis
*/
import ghidra.app.decompiler.DecompInterface;
import ghidra.app.decompiler.DecompileResults;
import ghidra.app.decompiler.DecompiledFunction;
import ghidra.app.util.headless.HeadlessScript;
import ghidra.program.model.address.Address;
import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.Instruction;
import ghidra.program.model.listing.InstructionIterator;
import ghidra.program.model.listing.Listing;
import ghidra.program.model.pcode.PcodeOp;
import ghidra.program.model.pcode.PcodeOpAST;
import ghidra.util.Msg;
import java.util.Iterator;
public class PrintCodeHeadless extends HeadlessScript {
@Override
public void run() throws Exception {
setHeadlessContinuationOption(HeadlessContinuationOption.ABORT);
String[] args = getScriptArgs();
if (args.length < 2) {
Msg.error(this,
String.format("USAGE: %s.java ADDRESS [asm,pcode,pcodehigh,c]",
this.getClass().getSimpleName()));
return;
}
String typeOfPrint = args[1];
DecompInterface ifc = new DecompInterface();
Address addressFunction = getAddressFactory().getAddress(args[0]);
if (addressFunction == null) {
Msg.error(this, String.format("Address not found at %s\n", args[0]));
return;
}
disassemble(addressFunction);
Function f = createFunction(addressFunction, "Test");
if (f == null) {
Msg.error(this, String.format("Function not found at %s", args[0]));
return;
}
Listing listing = currentProgram.getListing();
InstructionIterator instructionIterator = listing.getInstructions(f.getBody(), true);
switch (typeOfPrint) {
case "asm":
StringBuilder instructionsString = new StringBuilder();
while (instructionIterator.hasNext()) {
Instruction instruction = instructionIterator.next();
instructionsString.append(String
.format("%s\t%s\n",
instruction.getAddress(),
instruction));
}
println(instructionsString.toString());
return;
case "pcode":
StringBuilder pcodeString = new StringBuilder();
while (instructionIterator.hasNext()) {
Instruction instruction = instructionIterator.next();
pcodeString.append(String
.format("%s\t%s\n",
instruction.getAddress(),
instruction));
for (PcodeOp pcodeOp : instruction.getPcode()) {
pcodeString.append(String.format("\t%s\n", pcodeOp));
}
}
println(pcodeString.toString());
return;
}
println(String.format("Decompiling %s at %s", f.getName(), addressFunction));
ifc.openProgram(f.getProgram());
DecompileResults decompileResults = ifc.decompileFunction(f, 30, null);
println("Decompilation completed: " + decompileResults.decompileCompleted());
switch (typeOfPrint) {
case "pcodehigh":
Iterator<PcodeOpAST> pcodeOpASTIterator =
decompileResults.getHighFunction().getPcodeOps();
StringBuilder pcodeHighString = new StringBuilder();
while (pcodeOpASTIterator.hasNext()) {
PcodeOpAST pcodeOpAST = pcodeOpASTIterator.next();
pcodeHighString.append(String.format("%s\n", pcodeOpAST));
}
println(pcodeHighString.toString());
return;
case "c":
DecompiledFunction df = decompileResults.getDecompiledFunction();
println(df.getC());
}
}
}