mirror of
https://github.com/NationalSecurityAgency/ghidra.git
synced 2025-10-04 10:19:23 +02:00
GP-5104 get calling/called functions fixes
This commit is contained in:
parent
07d7358970
commit
dadfaae8c9
2 changed files with 40 additions and 112 deletions
|
@ -15,16 +15,11 @@
|
|||
*/
|
||||
// An example script that will print to the console, for a given function, all other functions
|
||||
// that call it and all functions that it calls.
|
||||
//import ghidra.app.plugin.core.navigation.locationreferences.ReferenceUtils;
|
||||
import java.util.*;
|
||||
|
||||
import ghidra.app.plugin.core.navigation.locationreferences.ReferenceUtils;
|
||||
import ghidra.app.script.GhidraScript;
|
||||
import ghidra.program.model.address.*;
|
||||
import ghidra.program.model.listing.*;
|
||||
import ghidra.program.model.symbol.*;
|
||||
import ghidra.program.util.FunctionSignatureFieldLocation;
|
||||
import ghidra.util.exception.CancelledException;
|
||||
import ghidra.program.model.listing.Function;
|
||||
import ghidra.program.model.listing.FunctionManager;
|
||||
|
||||
public class PrintFunctionCallTreesScript extends GhidraScript {
|
||||
|
||||
|
@ -42,19 +37,9 @@ public class PrintFunctionCallTreesScript extends GhidraScript {
|
|||
printOutgoingCalls(function);
|
||||
}
|
||||
|
||||
private void printIncomingCalls(Function function) throws CancelledException {
|
||||
Address functionAddress = function.getEntryPoint();
|
||||
FunctionSignatureFieldLocation location =
|
||||
new FunctionSignatureFieldLocation(function.getProgram(), functionAddress);
|
||||
Set<Address> addresses = ReferenceUtils.getReferenceAddresses(location, monitor);
|
||||
FunctionManager functionManager = currentProgram.getFunctionManager();
|
||||
Set<Function> callingFunctions = new HashSet<>();
|
||||
for (Address fromAddress : addresses) {
|
||||
Function callerFunction = functionManager.getFunctionContaining(fromAddress);
|
||||
if (callerFunction != null) {
|
||||
callingFunctions.add(callerFunction);
|
||||
}
|
||||
}
|
||||
private void printIncomingCalls(Function function) {
|
||||
|
||||
Set<Function> callingFunctions = function.getCallingFunctions(monitor);
|
||||
|
||||
// sort them by address
|
||||
List<Function> list = new ArrayList<>(callingFunctions);
|
||||
|
@ -66,15 +51,8 @@ public class PrintFunctionCallTreesScript extends GhidraScript {
|
|||
}
|
||||
|
||||
private void printOutgoingCalls(Function function) {
|
||||
AddressSetView functionBody = function.getBody();
|
||||
Set<Reference> references = getReferencesFrom(currentProgram, functionBody);
|
||||
Set<Function> outgoingFunctions = new HashSet<>();
|
||||
FunctionManager functionManager = currentProgram.getFunctionManager();
|
||||
for (Reference reference : references) {
|
||||
Address toAddress = reference.getToAddress();
|
||||
Function calledFunction = functionManager.getFunctionAt(toAddress);
|
||||
maybeAddIncomingFunction(outgoingFunctions, reference, calledFunction);
|
||||
}
|
||||
|
||||
Set<Function> outgoingFunctions = function.getCalledFunctions(monitor);
|
||||
|
||||
// sort them by address
|
||||
List<Function> list = new ArrayList<>(outgoingFunctions);
|
||||
|
@ -85,52 +63,6 @@ public class PrintFunctionCallTreesScript extends GhidraScript {
|
|||
}
|
||||
}
|
||||
|
||||
private void maybeAddIncomingFunction(Set<Function> incomingFunctions, Reference reference,
|
||||
Function calledFunction) {
|
||||
if (calledFunction != null) {
|
||||
incomingFunctions.add(calledFunction);
|
||||
}
|
||||
else if (isCallReference(reference)) {
|
||||
// we have a call reference, but no function
|
||||
println("Outgoing function call with no function from " + reference.getFromAddress() +
|
||||
" to " + reference.getToAddress());
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isCallReference(Reference reference) {
|
||||
RefType type = reference.getReferenceType();
|
||||
if (type.isCall()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (type.isIndirect()) {
|
||||
Listing listing = currentProgram.getListing();
|
||||
Instruction instruction = listing.getInstructionAt(reference.getFromAddress());
|
||||
if (instruction != null) {
|
||||
FlowType flowType = instruction.getFlowType();
|
||||
return flowType.isCall();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private Set<Reference> getReferencesFrom(Program program, AddressSetView addresses) {
|
||||
Set<Reference> set = new HashSet<>();
|
||||
ReferenceManager referenceManager = program.getReferenceManager();
|
||||
AddressIterator addressIterator = addresses.getAddresses(true);
|
||||
while (addressIterator.hasNext()) {
|
||||
Address address = addressIterator.next();
|
||||
Reference[] referencesFrom = referenceManager.getReferencesFrom(address);
|
||||
if (referencesFrom != null) {
|
||||
for (Reference reference : referencesFrom) {
|
||||
set.add(reference);
|
||||
}
|
||||
}
|
||||
}
|
||||
return set;
|
||||
}
|
||||
|
||||
private Function getCurrentFunction() {
|
||||
FunctionManager functionManager = currentProgram.getFunctionManager();
|
||||
return functionManager.getFunctionContaining(currentAddress);
|
||||
|
|
|
@ -2717,58 +2717,54 @@ public class FunctionDB extends DatabaseObject implements Function {
|
|||
@Override
|
||||
public Set<Function> getCallingFunctions(TaskMonitor monitor) {
|
||||
monitor = TaskMonitor.dummyIfNull(monitor);
|
||||
Set<Function> set = new HashSet<>();
|
||||
Set<Function> callers = new HashSet<>();
|
||||
ReferenceIterator iter = program.getReferenceManager().getReferencesTo(getEntryPoint());
|
||||
|
||||
while (iter.hasNext()) {
|
||||
if (monitor.isCancelled()) {
|
||||
return set;
|
||||
break;
|
||||
}
|
||||
Reference reference = iter.next();
|
||||
if (!reference.getReferenceType().isCall()) {
|
||||
continue;
|
||||
}
|
||||
Address fromAddress = reference.getFromAddress();
|
||||
Function callerFunction = manager.getFunctionContaining(fromAddress);
|
||||
if (callerFunction != null) {
|
||||
set.add(callerFunction);
|
||||
callers.add(callerFunction);
|
||||
}
|
||||
}
|
||||
return set;
|
||||
return callers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Function> getCalledFunctions(TaskMonitor monitor) {
|
||||
monitor = TaskMonitor.dummyIfNull(monitor);
|
||||
Set<Function> set = new HashSet<>();
|
||||
Set<Reference> references = getReferencesFromBody(monitor);
|
||||
for (Reference reference : references) {
|
||||
if (monitor.isCancelled()) {
|
||||
return set;
|
||||
}
|
||||
Address toAddress = reference.getToAddress();
|
||||
Function calledFunction = manager.getFunctionAt(toAddress);
|
||||
if (calledFunction != null) {
|
||||
set.add(calledFunction);
|
||||
}
|
||||
}
|
||||
return set;
|
||||
}
|
||||
Set<Function> callees = new HashSet<>();
|
||||
ReferenceManager refManager = program.getReferenceManager();
|
||||
AddressRangeIterator rangeIter = getBody().getAddressRanges();
|
||||
|
||||
private Set<Reference> getReferencesFromBody(TaskMonitor monitor) {
|
||||
Set<Reference> set = new HashSet<>();
|
||||
ReferenceManager referenceManager = program.getReferenceManager();
|
||||
AddressSetView addresses = getBody();
|
||||
AddressIterator addressIterator = addresses.getAddresses(true);
|
||||
while (addressIterator.hasNext()) {
|
||||
while (rangeIter.hasNext()) {
|
||||
AddressRange range = rangeIter.next();
|
||||
ReferenceIterator refIter = refManager.getReferenceIterator(range.getMinAddress());
|
||||
while (refIter.hasNext()) {
|
||||
if (monitor.isCancelled()) {
|
||||
return set;
|
||||
return callees;
|
||||
}
|
||||
Address address = addressIterator.next();
|
||||
Reference[] referencesFrom = referenceManager.getReferencesFrom(address);
|
||||
if (referencesFrom != null) {
|
||||
for (Reference reference : referencesFrom) {
|
||||
set.add(reference);
|
||||
Reference ref = refIter.next();
|
||||
if (!range.contains(ref.getFromAddress())) {
|
||||
break; // exhausted all addresses in the AddressRange, check next AddressRange
|
||||
}
|
||||
if (!ref.getReferenceType().isCall()) {
|
||||
continue; // reference is not a call, check next reference
|
||||
}
|
||||
Function callee = manager.getFunctionAt(ref.getToAddress());
|
||||
if (callee != null) { // sanity check
|
||||
callees.add(callee);
|
||||
}
|
||||
}
|
||||
}
|
||||
return set;
|
||||
return callees;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue