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

标签 python c++ xcode lldb

我正在使用 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 函数的 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 处理程序”插入堆栈,收集输入直到完成,然后将自身弹出堆栈并运行命令。

这看起来像是 SB 类 (SBInputReader) 的第一个草图,用于在 C++ SB API 中执行此操作,但我认为它并不完整,而且当前尚未暴露给 Python。所以我认为 Python 还没有足够的连接来实现这一点。

关于python - lldb:实现接受用户输入的自定义命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50380468/

相关文章:

python - 去除特定标点符号列表前后的空格

python - gcloud 应用部署 : This deployment has too many files

c++ - 从模板父类导入一个模板成员类型,无需重复名称

C++ 使用继承的父类转换为对象类

ios - Swift:NSFileManager 子类

python - ioctl 数字与 C ioctl 数字不同吗?

Python 窗口调整大小

c++ - 如何将文件中的数据放入 QTable Widget Item 中?

ios - xcode : storyboard does not reflect changes made in code in ViewController. swift

ios - 将自定义字体设置为粗体