首页 文章

Xcode中的LLDB Python脚本

提问于
浏览
5

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

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

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

1 回答

  • 13

    不幸的是,在Xcode,lldb和Python解释器之间,交互式控制台存在一些问题 . 请在http://bugreport.apple.com/提交错误报告 - 我不会重新探索交互式python脚本界面;效果更好 .

    我把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()
    

    我会将 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}"
    

    或者,如果它是类型 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)
    

    大多数情况下,当您使用 shlexoptparse 库时,命令可以执行选项解析,并且您将添加 __doc__ 字符串 - 我省略了这些以保持示例简单 .

相关问题