GP-5104 get calling/called functions fixes

This commit is contained in:
James 2024-12-02 08:39:14 -05:00
parent 07d7358970
commit dadfaae8c9
2 changed files with 40 additions and 112 deletions

View file

@ -4,9 +4,9 @@
* 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.
@ -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);