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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 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 // 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. // that call it and all functions that it calls.
//import ghidra.app.plugin.core.navigation.locationreferences.ReferenceUtils;
import java.util.*; import java.util.*;
import ghidra.app.plugin.core.navigation.locationreferences.ReferenceUtils;
import ghidra.app.script.GhidraScript; import ghidra.app.script.GhidraScript;
import ghidra.program.model.address.*; import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.*; import ghidra.program.model.listing.FunctionManager;
import ghidra.program.model.symbol.*;
import ghidra.program.util.FunctionSignatureFieldLocation;
import ghidra.util.exception.CancelledException;
public class PrintFunctionCallTreesScript extends GhidraScript { public class PrintFunctionCallTreesScript extends GhidraScript {
@ -42,19 +37,9 @@ public class PrintFunctionCallTreesScript extends GhidraScript {
printOutgoingCalls(function); printOutgoingCalls(function);
} }
private void printIncomingCalls(Function function) throws CancelledException { private void printIncomingCalls(Function function) {
Address functionAddress = function.getEntryPoint();
FunctionSignatureFieldLocation location = Set<Function> callingFunctions = function.getCallingFunctions(monitor);
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);
}
}
// sort them by address // sort them by address
List<Function> list = new ArrayList<>(callingFunctions); List<Function> list = new ArrayList<>(callingFunctions);
@ -66,15 +51,8 @@ public class PrintFunctionCallTreesScript extends GhidraScript {
} }
private void printOutgoingCalls(Function function) { private void printOutgoingCalls(Function function) {
AddressSetView functionBody = function.getBody();
Set<Reference> references = getReferencesFrom(currentProgram, functionBody); Set<Function> outgoingFunctions = function.getCalledFunctions(monitor);
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);
}
// sort them by address // sort them by address
List<Function> list = new ArrayList<>(outgoingFunctions); 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() { private Function getCurrentFunction() {
FunctionManager functionManager = currentProgram.getFunctionManager(); FunctionManager functionManager = currentProgram.getFunctionManager();
return functionManager.getFunctionContaining(currentAddress); return functionManager.getFunctionContaining(currentAddress);

View file

@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -2717,58 +2717,54 @@ public class FunctionDB extends DatabaseObject implements Function {
@Override @Override
public Set<Function> getCallingFunctions(TaskMonitor monitor) { public Set<Function> getCallingFunctions(TaskMonitor monitor) {
monitor = TaskMonitor.dummyIfNull(monitor); monitor = TaskMonitor.dummyIfNull(monitor);
Set<Function> set = new HashSet<>(); Set<Function> callers = new HashSet<>();
ReferenceIterator iter = program.getReferenceManager().getReferencesTo(getEntryPoint()); ReferenceIterator iter = program.getReferenceManager().getReferencesTo(getEntryPoint());
while (iter.hasNext()) { while (iter.hasNext()) {
if (monitor.isCancelled()) { if (monitor.isCancelled()) {
return set; break;
} }
Reference reference = iter.next(); Reference reference = iter.next();
if (!reference.getReferenceType().isCall()) {
continue;
}
Address fromAddress = reference.getFromAddress(); Address fromAddress = reference.getFromAddress();
Function callerFunction = manager.getFunctionContaining(fromAddress); Function callerFunction = manager.getFunctionContaining(fromAddress);
if (callerFunction != null) { if (callerFunction != null) {
set.add(callerFunction); callers.add(callerFunction);
} }
} }
return set; return callers;
} }
@Override @Override
public Set<Function> getCalledFunctions(TaskMonitor monitor) { public Set<Function> getCalledFunctions(TaskMonitor monitor) {
monitor = TaskMonitor.dummyIfNull(monitor); monitor = TaskMonitor.dummyIfNull(monitor);
Set<Function> set = new HashSet<>(); Set<Function> callees = new HashSet<>();
Set<Reference> references = getReferencesFromBody(monitor); ReferenceManager refManager = program.getReferenceManager();
for (Reference reference : references) { AddressRangeIterator rangeIter = getBody().getAddressRanges();
if (monitor.isCancelled()) {
return set;
}
Address toAddress = reference.getToAddress();
Function calledFunction = manager.getFunctionAt(toAddress);
if (calledFunction != null) {
set.add(calledFunction);
}
}
return set;
}
private Set<Reference> getReferencesFromBody(TaskMonitor monitor) { while (rangeIter.hasNext()) {
Set<Reference> set = new HashSet<>(); AddressRange range = rangeIter.next();
ReferenceManager referenceManager = program.getReferenceManager(); ReferenceIterator refIter = refManager.getReferenceIterator(range.getMinAddress());
AddressSetView addresses = getBody(); while (refIter.hasNext()) {
AddressIterator addressIterator = addresses.getAddresses(true); if (monitor.isCancelled()) {
while (addressIterator.hasNext()) { return callees;
if (monitor.isCancelled()) { }
return set; Reference ref = refIter.next();
} if (!range.contains(ref.getFromAddress())) {
Address address = addressIterator.next(); break; // exhausted all addresses in the AddressRange, check next AddressRange
Reference[] referencesFrom = referenceManager.getReferencesFrom(address); }
if (referencesFrom != null) { if (!ref.getReferenceType().isCall()) {
for (Reference reference : referencesFrom) { continue; // reference is not a call, check next reference
set.add(reference); }
Function callee = manager.getFunctionAt(ref.getToAddress());
if (callee != null) { // sanity check
callees.add(callee);
} }
} }
} }
return set; return callees;
} }
@Override @Override