Xcode 中的 LLDB Python 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14232208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:29:08  来源:igfitidea点击:

LLDB Python scripting in Xcode

pythonxcodescriptinglldb

提问by benwad

I've just discovered thishandy 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 scriptinto 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>inteffectively means print, but it still comes up as primtinton the terminal. This is just aesthetic but it's quite annoying!

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

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

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

EDIT: Hereis 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.

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

回答by Jason Molenda

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.

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

I put all my python scripts for lldb in ~/lldb. In my ~/.lldbinitfile I source them in. For instance, I have ~/lldb/stopifcaller.pywhich is

我将所有用于 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()

I would put the command script importin my ~/.lldbinitfile 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.

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

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

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

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

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

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

type format add -f x lldb::addr_t

If you want to add a new command to lldb, you would have a python file like ~/lldb/sayhello.py,

如果你想向 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')

and you would load it in to lldb like

你会把它加载到 lldb 中

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

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

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