本文介绍了Xcode中的LLDB Python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've just discovered this handy feature of LLDB that allows me to write Python scripts that have access to variables in the frame when I'm on a breakpoint in LLDB. However I'm having a few issues when using it in Xcode (v4.5.2). Firstly, I can't find anywhere that says where I should keep these Python scripts so that I can import them from the command line in LLDB. Secondly, after I type script into LLDB the keyboard input goes a bit wrong: backspace doesn't delete the character on the screen, but effectively deletes it from the command. So typing primt<bsp><bsp><bsp>int effectively means print, but it still comes up as primtint on the terminal. This is just aesthetic but it's quite annoying!

有人能指出我一些将Xcode与LLDB一起使用的Xcode专用资源吗?

Can anyone point me to some Xcode-specific resources for using Python with LLDB?

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

Here is another interesting link that says you can use Python to create custom summaries for variables using Python, but I can't find anything related to that.

推荐答案

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

Between Xcode, lldb, and the Python interpreter there are some problems with the interactive console, unfortunately. Please do file a bug report at http://bugreport.apple.com/ - I don't know if there is a bug report about this specific issue already, although problems in general here are known. You may want to use the command line lldb tool if you're exploring the interactive python scripting interface; that works better.

我将所有用于lldb的python脚本都放在了~/lldb中.在我的~/.lldbinit文件中,将它们作为来源.例如,我有~/lldb/stopifcaller.py,即

I put all my python scripts for lldb in ~/lldb. In my ~/.lldbinit file I source them in. For instance, I have ~/lldb/stopifcaller.py which is

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,它将自动恢复执行.本质上,只有foo()调用bar()时,断点1才会停止.请注意,在执行command script import ~/...时Xcode 4.5 lldb可能存在问题-您可能需要键入主目录的完整路径(/Users/benwad/或其他内容).我不能肯定地记得-Xcode 4.5出现了一些波浪线扩展问题,已经修复了一段时间.

I would put the command script import in my ~/.lldbinit file to load it automatically, if that's what I wanted. This particular example adds a python command to breakpoint #1 -- when lldb stops at the breakpoint, it will look at the calling function. If the calling function is not foo, it will automatically resume execution. In essence, breakpoint 1 will only stop if foo() calls bar(). Note that there may be a problem with Xcode 4.5 lldb in doing command script import ~/... -- you may need to type out the full path to your home directory (/Users/benwad/ or whatever). I can't remember for sure - there were a few tilde-expansion problems with Xcode 4.5 that have been fixed for a while.

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

You can add simple type summaries to your ~/.lldbinit directly. For instance, if I'm debugging lldb itself, ConstString objects have only one field of interest to me normally, the m_string ivar. So I have

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

或者如果它是类型addr_t,我总是希望将其格式化为十六进制格式,这样我就可以了

Or if it's the type addr_t, I always want that formatted as hex so I have

type format add -f x lldb::addr_t

如果要向lldb添加新命令,将有一个~/lldb/sayhello.py

If you want to add a new command to lldb, you would have a python file like ~/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')

,您将像这样将其加载到lldb中

and you would load it in to lldb like

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

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

Most of the time when you're adding a command written in python you'll use the shlex and optparse libraries so the command can do option parsing, and you'll add a __doc__ string - I omitted those things to keep the example simple.

这篇关于Xcode中的LLDB Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 11:24