-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrderedCallGraphGenerator.java
109 lines (90 loc) · 2.97 KB
/
OrderedCallGraphGenerator.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
//Generates an ordered call graph for either current function or whole program
//@category Functions
//@author gemesa
import ghidra.app.script.GhidraScript;
import ghidra.program.model.listing.*;
import ghidra.program.model.address.Address;
import ghidra.program.model.symbol.*;
import java.util.*;
public class OrderedCallGraphGenerator extends GhidraScript {
private Set<String> visited = new HashSet<>();
private StringBuilder graph = new StringBuilder();
private boolean analyzeWholeProgram = false;
@Override
public void run() throws Exception {
String choice = askChoice("Call graph analysis", "Select analysis scope",
Arrays.asList("Current function", "Whole program"),
"Current function");
analyzeWholeProgram = choice.equals("Whole program");
if (analyzeWholeProgram) {
generateProgramCallGraph();
}
else {
generateCurrentFunctionCallGraph();
}
println("\n" + graph.toString());
}
private void generateProgramCallGraph() throws Exception {
FunctionManager functionManager = currentProgram.getFunctionManager();
Iterator<Function> functions = functionManager.getFunctions(true);
while (functions.hasNext()) {
Function func = functions.next();
if (!visited.contains(func.getName())) {
printOrderedCallGraph(func, 0);
}
}
}
private void generateCurrentFunctionCallGraph() throws Exception {
Function currentFunction = getFunctionContaining(currentAddress);
if (currentFunction == null) {
println("No function selected!");
return;
}
printOrderedCallGraph(currentFunction, 0);
}
private void printOrderedCallGraph(Function function, int depth) throws Exception {
String funcName = function.getName();
String address =
function.isExternal() ? "EXTERNAL:" + String.format("%08x", function.getID())
: function.getEntryPoint().toString();
if (visited.contains(funcName)) {
graph.append(" ".repeat(depth))
.append(funcName)
.append(" @ ")
.append(address);
if (!function.isExternal()) {
graph.append(" [already visited!]\n");
}
else {
graph.append("\n");
}
return;
}
visited.add(funcName);
graph.append(" ".repeat(depth))
.append(funcName)
.append(" @ ")
.append(address)
.append("\n");
if (!function.isExternal()) {
List<Function> orderedFunctions = new ArrayList<>();
Address start = function.getEntryPoint();
Address end = function.getBody().getMaxAddress();
Listing listing = currentProgram.getListing();
InstructionIterator instructions = listing.getInstructions(start, true);
while (instructions.hasNext()) {
Instruction instr = instructions.next();
if (instr.getAddress().compareTo(end) > 0)
break;
Reference[] refs = instr.getReferencesFrom();
for (Reference ref : refs) {
Function calledFunc = getFunctionAt(ref.getToAddress());
if (calledFunc != null && !orderedFunctions.contains(calledFunc)) {
orderedFunctions.add(calledFunc);
printOrderedCallGraph(calledFunc, depth + 1);
}
}
}
}
}
}