lldb:实现接受用户输入的自定义命令

lldb: implement custom command taking user input

我正在使用 python 通过自定义命令 gm 扩展 lldb,然后调用 C++ - 函数 cli(const char* params)。 所以可以暂停 xcode(从而启动 lldb)并输入...

(lldb) gm set value

然后触发调用cli("set value")

C++ 函数 cli 可以利用 std::cout 来打印一些状态,但是我不能实现这个函数 "interactive",即消耗用户输入:

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

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

问题:如何使 cli 在消耗(并进一步处理)用户输入的意义上具有交互性?


为了进一步展示我想要实现的目标:有内置的 lldb 命令,如 expr(不带参数)进入交互模式:

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

我想在我自己的命令中有类似的行为,即输入 gm 然后交互地询问参数:

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

为了完整起见,请参阅当前用于调用 cli 函数的 python 脚本:

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.'

.lldbinit 文件中注册此脚本的行:

command script import ~/my_commands.py

内部 lldb 保留了一个 "I/O Handlers" 的堆栈,因此例如 expr 只是将 "Expr I/O Handler" 压入堆栈,收集输入直到它完成,然后将自己弹出堆栈并运行命令。

看起来像是 SB class (SBInputReader) 在 C++ SB API 中执行此操作的第一个草图,但我认为它不完整而且还不完整目前接触 Python。所以我认为从 Python 开始还没有足够的连接让你做到这一点。