Search code examples
pythonc++xcodelldb

lldb: implement custom command taking user input


I am using python to extend lldb by a custom command gm, which calls a C++ - function cli(const char* params) then. So one can pause xcode (thereby launching lldb) and type...

(lldb) gm set value

to trigger a call cli("set value") then.

C++-function cli can make use of std::cout to print some state, but I cannot make this function "interactive", i.e. consuming user input:

void cli(const char* params) {   
    std::cout << "params: " << params << std::endl;  // works

    std::string userInput;
    std::cin >> userInput; // does not work; is simply ignored
}

Question: How can I make cli interactive in a sense that it consumes (and further processes) user input?


To further show what I'd like to achieve: There are built in lldb-commands like expr (without arguments) which enter into an interactive mode:

(lldb) expr
Enter expressions, then terminate with an empty line to evaluate:
1 2+2
2 
(int) $0 = 4

I'd like to have similar behaviour in my own command, i.e. typing in gm and being then asked interactively for params:

(lldb) gm
Enter generic model parameters; Terminate interactive mode with "end":
1 set value
2 params: set value
3 end

Just for completeness, see the python script currently used for calling the cli-function:

def gm(debugger, command, result, internal_dict):

    cmd = "po cli(\""+command+"\")"
    lldb.debugger.HandleCommand(cmd)

# And the initialization code to add your commands 
def __lldb_init_module(debugger, internal_dict):
    debugger.HandleCommand('command script add -f gm.gm gm')
    print 'The "gm" python command has been installed and is ready for use.'

and the line in the .lldbinit-file registering this script:

command script import ~/my_commands.py

Solution

  • Internally lldb keeps a stack of "I/O Handlers" and so for instance expr just pushes the "Expr I/O Handler" onto the stack, collects the input till it is done and then pops itself off the stack and runs the command.

    There's what looks like a first sketch of an SB class (SBInputReader) to do this in the C++ SB API's but I don't think it's complete and it isn't currently exposed to Python. So I don't think there's enough wired up for you do to this from Python yet.