使用 Python 脚本时 Xcode lldb 调试器中的空 lldb.frame 变量

Empty lldb.frame variables in Xcode lldb debugger when using Python scripts

我目前正在为 C++ 中的自定义对象开发绘图命令。我正在使用 Xcode v10.1.

我使用 command script import test.py 导入我自己的命令,其中有一个函数如下:

import lldb

def test_function(debugger, command, result, dict):
        obj = lldb.frame.FindVariable("custom_object")
        print(obj)

def __lldb_init_module (debugger, dict):
  debugger.HandleCommand('command script add -f test.test_function test')

其中 自定义对象 是我想在 Python 脚本中使用的对象。

如果我刚刚打开调试器,我会收到错误消息:

obj = lldb.frame.FindVariable("custom_object")
AttributeError: 'NoneType' object has no attribute 'FindVariable'

然而,当我直接打开 Xcode 中的嵌入式 Python 解释器并执行与上面相同的代码行时,我没有得到任何错误。

如果我现在再次执行我的自定义命令,它将对调试器相同范围内的所有对象成功执行。

lldb.frame 未在基于 lldb python 的命令中定义 - 这就是为什么它是 NoneTypelldb.{process, thread,frame} 只是为了在交互式脚本解释器中方便。但这对于命令来说没有意义——它可能会在停止挂钩、断点命令等中获得 运行 以依赖其进程和线程的某些全局状态。毕竟,您可以让两个线程同时命中同一个断点。所以没有唯一的“lldb.thread”。

最好使用将 SBExecutionContext 作为第三个参数的命令函数版本(参见:https://lldb.llvm.org/use/python-reference.html)并从该参数获取线程和帧。