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.
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!
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);
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() # ... # ... # ...