Search code examples
pythonmacoslldb

How to correctly call python function from lldb


I have this simple python script:

#~/.lldb/scripts/fprint.py

import lldb

def fprint(filePath, text):
    with open(filePath,'a') as f: f.write(text)

def __lldb_init_module(debugger, internal_dict):
    debugger.HandleCommand('command script add -f fprint.fprint fprint')
    print '"fprint(filePath, text)" command is here!'

Which I declare in ~/.lldbinit.

When trying to call it from lldb I get this weird error:

% lldb
"fprint(filePath, text)" command is here!
(lldb) fprint 'tmp.txt' 'Hello World!'
TypeError: fprint() takes exactly 2 arguments (4 given)
(lldb)

So, What am I doing wrong?


Solution

  • You are doing more than just calling a Python function, you are defining a Python backed lldb command-line command. That has a few more requirements, in particular, the Python function implementing the LLDB command must have the right signature. See the section Create a new lldb command using a Python function in the LLDB docs for more details.

    Note, if you just want to call a Python function using lldb's embedded Python interpreter, you can do that with the "script" command:

    (lldb) script fprint("tmp.txt", "Hello world")