python - Xcode 中的 LLDB Python 脚本

标签 python xcode scripting lldb

我刚刚发现this LLDB 的一个方便功能使我能够编写 Python 脚本,当我位于 LLDB 中的断点时,这些脚本可以访问框架中的变量。但是,我在 Xcode (v4.5.2) 中使用它时遇到了一些问题。首先,我找不到任何地方说明我应该在哪里保存这些 Python 脚本,以便我可以从 LLDB 中的命令行导入它们。其次,在我输入 script 后在 LLDB 中,键盘输入有点错误:退格键不会删除屏幕上的字符,而是有效地将其从命令中删除。所以输入primt<bsp><bsp><bsp>int有效意味着print ,但它仍然显示为 primtint在终端上。这只是美观,但很烦人!

任何人都可以向我指出一些用于将 Python 与 LLDB 结合使用的 Xcode 特定资源吗?

编辑:Here是另一个有趣的链接,它说您可以使用 Python 为变量创建自定义摘要,但我找不到与此相关的任何内容。

最佳答案

不幸的是,Xcode、lldb 和 Python 解释器之间的交互式控制台存在一些问题。请通过 http://bugreport.apple.com/ 提交错误报告- 我不知道是否已经有关于此特定问题的错误报告,尽管这里的一般问题是已知的。如果您正在探索交互式 Python 脚本界面,您可能需要使用命令行 lldb 工具;这样效果更好。

我将 lldb 的所有 python 脚本放在 ~/lldb 中。在我的 ~/.lldbinit 文件中,我将它们作为源。例如,我有 ~/lldb/stopifcaller.py ,它是

import lldb

# Use this like
# (lldb) command script import ~/lldb/stopifcaller.py
# (lldb) br s -n bar
# (lldb) br comm add --script-type python -o "stopifcaller.stop_if_caller(frame, 'foo')" 1

def stop_if_caller(current_frame, function_of_interest):
  thread = current_frame.GetThread()
  if thread.GetNumFrames() > 1:
    if thread.GetFrameAtIndex(1).GetFunctionName() != function_of_interest:
      thread.GetProcess().Continue()

如果这是我想要的,我会将命令脚本导入放入我的~/.lldbinit文件中以自动加载它。这个特定的示例向断点 #1 添加了一个 python 命令——当 lldb 在断点处停止时,它将查看调用函数。如果调用函数不是foo,它将自动恢复执行。本质上,断点 1 仅在 foo() 调用 bar() 时才会停止。请注意,Xcode 4.5 lldb 在执行 command script import ~/... 时可能会出现问题 - 您可能需要输入主目录的完整路径 (/Users/benwad/ 或其他)。我记不清了 - Xcode 4.5 存在一些波浪号扩展问题,这些问题已经修复了一段时间。

您可以直接向 ~/.lldbinit 添加简单类型摘要。例如,如果我正在调试 lldb 本身,ConstString 对象通常只有一个我感兴趣的字段,即 m_string ivar。所以我有

type summary add -w lldb lldb_private::ConstString -s "${var.m_string}"

或者,如果它是 addr_t 类型,我总是希望将其格式化为十六进制,所以我有

type format add -f x lldb::addr_t

如果你想向 lldb 添加新命令,你将有一个像 ~/lldb/sayhello.py 这样的 Python 文件,

import lldb

def say_hello(debugger, command, result, dict):
  print 'hello'

def __lldb_init_module (debugger, dict):
  debugger.HandleCommand('command script add -f sayhello.say_hello hello')

你可以将它加载到 lldb 中

(lldb) comma script import  ~/lldb/sayhello.py
(lldb) hello
hello
(lldb)

大多数情况下,当您添加用 python 编写的命令时,您将使用 shlexoptparse 库,以便该命令可以进行选项解析,并且您将添加一个 __doc__ 字符串 - 为了使示例简单,我省略了这些内容。

关于python - Xcode 中的 LLDB Python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14232208/

相关文章:

command-line - 设置PowerShell脚本以自动执行

python - 在 Python 中 pickle 静态方法

ios - 我们怎样才能停止线程

file - PowerShell:设置内容与 "file already in use"有问题

c# - 将 Lua 与 C#/Mono 结合使用

iphone - 在提交给 Apple 之前更改 .app 文件的名称是否足够?

python - pyspark.ml 管道 : are custom transformers necessary for basic preprocessing tasks?

python - 有关 QTreeView 中的项目和信号的信息 - PyQt5

python - 导入函数带来错误: name not defined

iphone - 在 UIToolBar 中垂直移动 UIBarButtonItems