Search code examples
pythongdblldb

LLDB convenience function in Python


Is it possible to write an LLDB extension in Python which provides a function which can be used in expressions at the LLDB command-line, similar to this for GDB?

I've searched the LLDB Python API documentation and found how to add commands, type formatters, and synthetic child providers. I'm in the process of creating scripts using these facilities (with underlying code portable between LLDB and GDB). We have a convenience function which we use in GDB, and are considering writing more, but if that's not possible in LLDB we might use some sort of command instead.


Solution

  • A lot of the functionality in these pseudo-functions - like finding elements in the containing stack - aren't really germane to expression evaluation. They are in gdb's print because in gdb that's much more of a general purpose "tell me stuff about the debugee" command. In lldb, some of the reporting provided by these functions would in lldb be more appropriate as separate commands.

    lldb's print is an attempt to say "what would happen if this code were run right here in this debugee". lldb's expression evaluator tries to be as close to a language accurate embedded compiler as is workable, acting "as if the code was run in the current frame". So it is very selective about introducing non-language features.

    Note, if you need some special test or access methods in your lldb expression s, you can introduce your own functions to the expression evaluator. The command:

    (lldb) expr bool $_isvoid(void *ptr) { return ptr == nullptr; }

    will make a function called $_isvoid that you can call in other expressions. This won't however, have the feature of the gdb ones, that it can be called regardless of whether the target is running or not. It will make a real function in the debuggee's memory and actually call it.