Skip to content

How do you access the flat API in ghidra_scripts? #1919

@ExpHP

Description

@ExpHP

I tried to move a helper function into a script in ghidra_scripts, and am being defeated by what would seem to be the simplest possible thing. Following examples I see elsewhere online, I tried just writing the same code I would write in ghidra's python interpreter:

ghidra_scripts/better.py

from ghidra.program.model.symbol import SourceType

def nameFunction(addr, name):
    """ Set the name of a function regardless of whether one currently exists at that address. """
    addr = toAddr(addr)
    if not createFunction(addr, name):
        getFunctionAt(addr).setName(name, SourceType.USER_DEFINED)

ghidra's interpreter

>>> import better as b
>>> b.nameFunction(0x444140, 'testing123testing')
Traceback (most recent call last):
  File "python", line 1, in <module>
  File "C:\Users\diago\ghidra_scripts\better.py", line 10, in nameFunction
    addr = toAddr(addr)
NameError: global name 'toAddr' is not defined

Okay, so the API funcs are not available to me for whatever reason. Maybe the examples I found were old? I locate the functions on FlatProgramAPI, however, they are instance methods, so if I want to use them in my module I need to create an instance first.

ghidra's interpreter

>>> ghidra.program.flatapi.FlatProgramAPI()
Traceback (most recent call last):
  File "python", line 1, in <module>
TypeError: ghidra.program.flatapi.FlatProgramAPI(): expected 1-2 args; got 0

>>> ghidra.program.flatapi.FlatProgramAPI(currentProgram, monitor)
ghidra.program.flatapi.FlatProgramAPI@28c313e

Okay, so in order for my python module to create a FlatProgramAPI, it will need access to currentProgram and monitor, which are... properties of FlatProgramAPI, making this a total bird-or-the-egg situation! What am I missing here?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions