首页 > 解决方案 > 使用 Python 脚本时 Xcode lldb 调试器中的空 lldb.frame 变量

问题描述

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

我使用命令脚本 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 解释器并执行与上面相同的代码行时,我没有收到任何错误。

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

标签: pythonc++xcodelldb

解决方案


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

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


推荐阅读