2

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

OTOH, it shouldn't be terribly hard to make this work, since the internal classes you would have to shim to the SB API are pretty well established. If somebody was eager for an opportunity to do a little lldb hacking...
I just started investigating the extension possibilities of lldb, so I know pretty little about it. Anyway, this means that I have to check out and extend lldb source code, build it on my own and deploy it to the development environments of each developer, right?
The facility you want to have present would have to be built into lldb since it is currently missing in the Python layer. The lldb development community is very helpful, patches are welcome. There is always a couple rounds of helpful suggestions during patch review, but sound patches get into the tree pretty quickly. Then you would have to wait for the next release cycle to get the fix into official binaries. That's on the order of a couple of times a year. You would have to support this with your own releases till an official version is released, but that should be temporary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.