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

lldb: implement custom command taking user input

本文关键字:输入 自定义 命令 用户 实现 LLDB      更新时间:2023-10-16

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

(lldb) gm set value

以触发呼叫cli("set value")然后。

C++函数cli可以利用std::cout来打印某种状态,但我不能使这个函数"交互式",即消耗用户输入:

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) $0 = 4

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

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

为了完整起见,请参阅当前用于调用cli-function 的 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 处理程序",因此例如expr只是将"Expr I/O 处理程序"推送到堆栈上,收集输入直到完成,然后从堆栈中弹出自己并运行命令。

在C++ SB API中,有一个看起来像SB类(SBInputReader)的第一个草图,但我认为它不完整,目前还没有暴露给Python。 所以我认为还没有足够的连接让你从 Python 做这件事。