Xcode中的LLDB Python脚本

ben*_*wad 6 python scripting xcode lldb

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

有人能指出我使用Python与LLDB的一些Xcode特定资源吗?

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

Jas*_*nda 14

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

我把所有我的python脚本都放到了lldb中~/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()
Run Code Online (Sandbox Code Playgroud)

我会把command script import我的~/.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}"
Run Code Online (Sandbox Code Playgroud)

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

type format add -f x lldb::addr_t
Run Code Online (Sandbox Code Playgroud)

如果你想向lldb添加一个新命令,你会有一个python文件,如~/lldb/sayhello.py,

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')
Run Code Online (Sandbox Code Playgroud)

你会把它加载到lldb之类的

(lldb) comma script import  ~/lldb/sayhello.py
(lldb) hello
hello
(lldb)
Run Code Online (Sandbox Code Playgroud)

大多数情况下,当你添加一个用python编写的命令时,你将使用shlexoptparse库,这样命令可以进行选项解析,你将添加一个__doc__字符串 - 我省略了这些东西以保持示例简单.

  • 我认为我们真的需要在lldb网站上添加一本食谱风格的文档,其中包含一系列简单的问题及其解决方案 - 这可能是这类事情的最佳选择.虽然缺乏时间.:( (5认同)