Lately I'm using Ghidra and I don't find the API to get the control flow graph of a given function. Can someone help me?
Thank you in advance.
EDIT: it is different from the other question (link) because I'm asking for the API.
Reverse Engineering Stack Exchange is a question and answer site for researchers and developers who explore the principles of a system through analysis of its structure, function, and operation. It only takes a minute to sign up.
Sign up to join this communityLately I'm using Ghidra and I don't find the API to get the control flow graph of a given function. Can someone help me?
Thank you in advance.
EDIT: it is different from the other question (link) because I'm asking for the API.
I'm looking for the same thing and for now I found the class PcodeSyntaxTree having a method called getBasicBlocks(), which returns an array of PcodeBlockBasic elements. This second class has methods like getIn and getOut which retrieve incoming and outgoing nodes (basic blocks), respectively. So I think using this methods should be the interface for interacting with the CFG programmatically. But sadly I didn't figure out yet how to get this PcodeSyntaxTree object, but will continue investigating.
I hope this can help you a bit!
links: http://ghidra.re/ghidra_docs/api/ghidra/program/model/pcode/PcodeSyntaxTree.html http://ghidra.re/ghidra_docs/api/ghidra/program/model/pcode/PcodeBlockBasic.html
PS.: Other thing you can do is to study the code of calculateCyclomaticComplexity method, which uses this BasickBlock models, I think I will probably do that.
EDIT: good news I think. I found the DecompleResults class, having the method getHighFunction() which returns a HighFunction object. HighFunction class extends to PcodeSyntaxTree, so it has also the getBasicBlocks method. From that point you can go on.
DecompileResults class is contained in ghidra.app.decompiler, as well as DecompInterface, which has the decompileFunction() method that returns a DecompileResults object.
// Make calls to the decompiler:
// * DecompileResults res = ifc.decompileFunction(func,0,taskmonitor);
https://ghidra.re/ghidra_docs/api/ghidra/app/decompiler/DecompileResults.html https://ghidra.re/ghidra_docs/api/ghidra/program/model/pcode/HighFunction.html
EDIT 2:
I can imagine something like this (in python api):
import ghidra.app.decompiler as decomp
interface = decomp.DecompInterface()
# decompileFunction(function, timeout, monitor)
# according to documentation, function is a Function object, timeout is int,
# and monitor is an OPTIONAL ARGUMENT of TaskMonitor type.
# However, it doesn't say anything about a default value for this argument
# and omitting the arg in the call falls in an error.
results = interface.decompileFunction(func, 0, taskMonitor)
hf = results.getHighFunction()
bbList = hf.getBasicBlocks()
# ...
# ...
# ...