[关闭]
@qinyun 2017-09-30T01:16:40.000000Z 字数 10706 阅读 1436

你知道怎么用LLDB调试Swift吗?

未分类

---#使用LLDB调试Swift

摘要

作为开发者,我们工作70%的时间都用于调试。20%用于架构设计和团队沟通,仅有10%的时间用于写代码。

调试好比犯罪电影中同时扮演罪犯和侦探的角色。
— Filipe Fortes via Twitter

因此如何让这70%的时间变得高效显得尤为重要。LLDB是个很好的选择。尽管Xcode拥有功能完善的调试面板,控制台仍然是调试中重要的组成部分。接下来我会探讨一些工作中常用到的一些LLDB调试技巧。

从哪开始?

LLDB拥有大量有用的调试工具。我会选一些重要的命令讨论:

  1. 获取变量值:expressioneprintpop
  2. 获取执行环境+特定语言命令:bugreportframelanguage
  3. 执行流程控制:processbreakpointthreadwatchpoint
  4. 其他:commandplatformgui

下面列出了LLDB常用命令的描述和例子。你可以保存该图片方便以后查阅。


大图地址

1.获取变量值和状态

命令:expressioneprintpop

调试工具最基本的功能是打印和修改变量的值。expressione就是这样的工具。你可以在运行时执行几乎任何表达式或命令。

假设你正在调试valueOfLifeWithoutSumOf()方法,该方法用于两数相加并与42相减。

现在运行得到了错误的结果。你可以像下图这样修改代码尝试定位问题:

更好的方式是使用expression命令在运行时修改变量的值。设置断点并运行。

按照LLDB格式打印变量的值:

  1. (lldb) e <variable>

完全相似的方式执行表达式:

  1. (lldb) e <expression>

  1. (lldb) e sum
  2. (Int) $R0 = 6 // 当前调试会话中可以使用$R0代替该变量
  3. (lldb) e sum = 4 // 修改sum变量的值
  4. (lldb) e sum
  5. (Int) $R2 = 4 // 调试期间sum值变为4

expression命令有一些选项。LLDB使用双破折号--分隔选项和表达式:

  1. (lldb) expression <some flags> -- <variable>

expression拥有大约30个选项。这些选择都值得你来探索。终端输入以下命令可以获取详细的文档:

  1. > lldb
  2. > (lldb) help # To explore all available commands
  3. > (lldb) help expression # To explore all expressions related sub-commands

下面列出了几个比较常用的选项:

假如现在有个logger对象。该对象拥有一些字符串和结构体作为属性。如果只想打印层级1的属性,可使用-D选项:

  1. (lldb) e -D 1 -- logger
  2. (LLDB_Debugger_Exploration.Logger) $R5 = 0x0000608000087e90 {
  3. currentClassName = "ViewController"
  4. debuggerStruct ={...}
  5. }

LLDB默认会递归打印对象的所有属性,展示非常详尽的内容:

  1. (lldb) e -- logger
  2. (LLDB_Debugger_Exploration.Logger) $R6 = 0x0000608000087e90 {
  3. currentClassName = "ViewController"
  4. debuggerStruct = (methodName = "name", lineNumber = 2, commandCounter = 23)
  5. }

也可以像如下使用 e -O --或者别名 po来打印:

  1. (lldb) po logger
  2. <Logger: 0x608000087e90>

这样直接打印可读性不高。通过实现 CustomStringConvertible协议中的var description: String { return ...}属性,po会返回可读性更好的描述。

在这段开头也提到了print命令。除了print命令没有可用选项无需传递参数外,print <expression/variable>expression --<expression/variable>几乎一样。

2.获取执行环境+特定语言命令

bugreportframelanguage

你是否经常需要拷贝粘贴日志到任务管理器中来定位问题。LLDB的bugreport命令可以生成一份详细的app当前状态的报告。该命令对于想要延迟追踪定位问题非常有用。为了保存app的状态,你可以使用bugreport来生成报告。

  1. (lldb) bugreport unwind --outfile <path to output file>

生成的报告如下:

!Example of bugreport command output

frame命令可以打印出当前线程的栈帧:

使用如下命令来快速了解你在哪和当前执行环境:

  1. (lldb) frame info
  2. frame #0: 0x000000010bbe4b4d LLDB-Debugger-Exploration`ViewController.valueOfLifeWithoutSumOf(a=2, b=2, self=0x00007fa0c1406900) -> Int at ViewController.swift:96

该信息对文章后面提到的断点管理非常有用。

LLDB有一些针对特定语言的命令。这些命令有针对C++,Objective-C,Swift和RenderScript的。这篇文章中只讨论针对swift的两个命令:demanglerefcount

demangle命令正如其名字描述的一样用于修复损坏的Swift类型名(编译期间为避免命名空间问题而产生)。想要了解更多可以观看WWDC14 session - "Advanced Swift Debugging in LLDB"

refcount命令的命名也非常直观,可用于显示对象的引用计数。让我们回到前面讨论过的用于输出的对象 - logger

  1. (lldb) language swift refcount logger
  2. refcount data: (strong = 4, weak = 0)

这对于调试查找内存泄露问题是很有帮助的。

3.执行流程控制

processbreakpointthread

这是我最喜欢的部分。使用LLDB的这些命令(尤其是breakpoint)可以让调试变得更自动化。有助于提升调试速度。

process用于控制要调试的进程,可把LLDB依附到特定target或从target上解绑。运行target时Xcode已经为我们做好了将LLDB依附到该进程的工作,所以这里不再讨论LLDB的依附。可通过阅读苹果手册-"Using LLDB as a Standalone Debugger"来了解如何依附LLDB到进程中。

process status用于打印当前进程及断点处的相关信息:

  1. (lldb) process status
  2. Process 27408 stopped
  3. * thread #1, queue = 'com.apple.main-thread', stop reason = step over
  4. frame #0: 0x000000010bbe4889 LLDB-Debugger-Exploration`ViewController.viewDidLoad(self=0x00007fa0c1406900) -> () at ViewController.swift:69
  5. 66
  6. 67 let a = 2, b = 2
  7. 68 let result = valueOfLifeWithoutSumOf(a, and: b)
  8. -> 69 print(result)
  9. 70
  10. 71
  11. 72

使用如下命令可继续执行:

  1. (lldb) process continue
  2. (lldb) c // 等同于上面命令

这等同于Xcode调试面板中 "continue" 按钮:

breakpoint用于断点的各种操作。这里不会讨论这些太常见的命令:breakpoint enablebreakpoint disable,和breakpoint delete

使用list子命令可以打印出所有断点:

  1. (lldb) breakpoint list
  2. Current breakpoints:
  3. 1: file = '/Users/Ahmed/Desktop/Recent/LLDB-Debugger-Exploration/LLDB-Debugger-Exploration/ViewController.swift', line = 95, exact_match = 0, locations = 1, resolved = 1, hit count = 1
  4. 1.1: where = LLDB-Debugger-Exploration`LLDB_Debugger_Exploration.ViewController.valueOfLifeWithoutSumOf (Swift.Int, and : Swift.Int) -> Swift.Int + 27 at ViewController.swift:95, address = 0x0000000107f3eb3b, resolved, hit count = 1
  5. 2: file = '/Users/Ahmed/Desktop/Recent/LLDB-Debugger-Exploration/LLDB-Debugger-Exploration/ViewController.swift', line = 60, exact_match = 0, locations = 1, resolved = 1, hit count = 1
  6. 2.1: where = LLDB-Debugger-Exploration`LLDB_Debugger_Exploration.ViewController.viewDidLoad () -> () + 521 at ViewController.swift:60, address = 0x0000000107f3e609, resolved, hit count = 1

列表中的第一个数字是断点ID,可用于引用对应的断点。下面从控制台设置一个新的断点:

  1. (lldb) breakpoint set -f ViewController.swift -l 96
  2. Breakpoint 3: where = LLDB-Debugger-Exploration`LLDB_Debugger_Exploration.ViewController.valueOfLifeWithoutSumOf (Swift.Int, and : Swift.Int) -> Swift.Int + 45 at ViewController.swift:96, address = 0x0000000107f3eb4d

上面例子中-f选项用于指明断点所在的文件。-l选项用于指明断点所在的行数。对于上面例子有个更简洁的表达:

  1. (lldb) b ViewController.swift:96

也可以使用以命令对某个方法设置断点:

  1. (lldb) breakpoint set --func-regex valueOfLifeWithoutSumOf
  2. (lldb) b -r valueOfLifeWithoutSumOf // 上面命令的精简版

有时候需要断点仅命中一次。命中后随即删除该断点。通过如下命令行可以实现:

  1. (lldb) breakpoint set --one-shot -f ViewController.swift -l 90
  2. (lldb) br s -o -f ViewController.swift -l 91 // 上面命令的精简版

下面进入最有趣的部分-断点自动化。你知道可以设置特定动作用于断点发生时执行吗?调试时你是否喜欢在代码中使用print()来打印你感兴趣的值?下面有个更好的方法。

通过breakpoint command可以设置命中断点后执行的命令。甚至可以设置不会打断执行的"透明"断点。从技术上来说"透明"断点已然会打断执行,但通过在命令链中添加continue命令可以变得无感知。

  1. (lldb) b ViewController.swift:96 // 设置断点
  2. Breakpoint 2: where = LLDB-Debugger-Exploration`LLDB_Debugger_Exploration.ViewController.valueOfLifeWithoutSumOf (Swift.Int, and : Swift.Int) -> Swift.Int + 45 at ViewController.swift:96, address = 0x000000010c555b4d
  3. (lldb) breakpoint command add 2 // Setup some commands
  4. Enter your debugger command(s). Type 'DONE' to end.
  5. > p sum // Print value of "sum" variable
  6. > p a + b // Evaluate a + b
  7. > DONE

通过breakpoint command list <breakpoint id>命令可以检查一遍命令是否都正确:

  1. (lldb) breakpoint command list 2
  2. Breakpoint 2:
  3. Breakpoint commands:
  4. p sum
  5. p a + b

当断点命中时控制台会有如下输出:

  1. Process 36612 resuming
  2. p sum
  3. (Int) $R0 = 6
  4. p a + b
  5. (Int) $R1 = 4

这正是我们所期望的。甚至可以在命令链的后面添加continue来防止执行被打断。

  1. (lldb) breakpoint command add 2 // Setup some commands
  2. Enter your debugger command(s). Type 'DONE' to end.
  3. > p sum // Print value of "sum" variable
  4. > p a + b // Evaluate a + b
  5. > continue // Resume right after first hit
  6. > DONE

输出结果:

  1. p sum
  2. (Int) $R0 = 6
  3. p a + b
  4. (Int) $R1 = 4
  5. continue
  6. Process 36863 resuming
  7. Command #3 'continue' continued the target.

通过thread和其子命令可以完全的控制执行流程:step-overstep-instep-outcontinue。这些命令等同于Xcode调试面板里用于控制执行流程的按钮。

对于这些命令也有预定义的快捷命令:

  1. (lldb) thread step-over
  2. (lldb) next // 等同于"thread step-over"
  3. (lldb) n // 等同于"next"
  4. (lldb) thread step-in
  5. (lldb) step // 等同于"thread step-in"
  6. (lldb) s // 等同于"step"

通过info子命令可获取关于当前线程的更多信息:

  1. (lldb) thread info
  2. thread #1: tid = 0x17de17, 0x0000000109429a90 LLDB-Debugger-Exploration`ViewController.sumOf(a=2, b=2, self=0x00007fe775507390) -> Int at ViewController.swift:90, queue = 'com.apple.main-thread', stop reason = step in

list子命令用于显示当前所有活跃线程:

  1. (lldb) thread list
  2. Process 50693 stopped
  3. * thread #1: tid = 0x17de17, 0x0000000109429a90 LLDB-Debugger-Exploration`ViewController.sumOf(a=2, b=2, self=0x00007fe775507390) -> Int at ViewController.swift:90, queue = 'com.apple.main-thread', stop reason = step in
  4. thread #2: tid = 0x17df4a, 0x000000010daa4dc6 libsystem_kernel.dylib`kevent_qos + 10, queue = 'com.apple.libdispatch-manager'
  5. thread #3: tid = 0x17df4b, 0x000000010daa444e libsystem_kernel.dylib`__workq_kernreturn + 10
  6. thread #5: tid = 0x17df4e, 0x000000010da9c34a libsystem_kernel.dylib`mach_msg_trap + 10, name = 'com.apple.uikit.eventfetch-thread'

其他

commandplatformgui

LLDB中有一个用于管理其他命令的命令。听起来很奇怪,但非常有用。首先,该命令允许你从文件执行LLDB命令。因此你可以创建一个包含大量有用命令的脚本当做一个命令来执行。下面是一个包含两条命令的文件:

  1. thread info // 显示当前线程信息
  2. br list // 显示所有断点

真正命令执行如下:

  1. (lldb) command source /Users/Ahmed/Desktop/lldb-test-script
  2. Executing commands in '/Users/Ahmed/Desktop/lldb-test-script'.
  3. thread info
  4. thread #1: tid = 0x17de17, 0x0000000109429a90 LLDB-Debugger-Exploration`ViewController.sumOf(a=2, b=2, self=0x00007fe775507390) -> Int at ViewController.swift:90, queue = 'com.apple.main-thread', stop reason = step in
  5. br list
  6. Current breakpoints:
  7. 1: file = '/Users/Ahmed/Desktop/Recent/LLDB-Debugger-Exploration/LLDB-Debugger-Exploration/ViewController.swift', line = 60, exact_match = 0, locations = 1, resolved = 1, hit count = 0
  8. 1.1: where = LLDB-Debugger-Exploration`LLDB_Debugger_Exploration.ViewController.viewDidLoad () -> () + 521 at ViewController.swift:60, address = 0x0000000109429609, resolved, hit count = 0

不幸的是,无法在执行脚本文件时传递参数(除非在脚本文件中创建一个可用变量)。

script子命令提供了一些高级用法,可用于管理(adddeleteimportlist)自定义的Python脚本。script是的命令自动化变成了可能。更详尽的信息请查阅Python scripting for LLDB指南。示例中,我们创建了一个功能非常简单的script.py脚本,该脚本仅包含了print_hello()命令用于打印"Hello Debug个!"到控制台:

  1. import lldb
  2. def print_hello(debugger, command, result, internal_dict):
  3. print "Hello Debugger!"
  4. def __lldb_init_module(debugger, internal_dict):
  5. debugger.HandleCommand('command script add -f script.print_hello print_hello') // Handle script initialization and add command from this module
  6. print 'The "print_hello" python command has been installed and is ready for use.' // Print confirmation that everything works

接下来需要导入该Python脚本,导入后可直接使用脚本命令:

  1. (lldb) command import ~/Desktop/script.py
  2. The "print_hello" python command has been installed and is ready for use.
  3. (lldb) print_hello
  4. Hello Debugger!

status子命令可用于快速查看当前平台信息。status会打印出:SDK路径,处理器架构,操作系统版本和该SDK支持的可用设备列表。

  1. (lldb) platform status
  2. Platform: ios-simulator
  3. Triple: x86_64-apple-macosx
  4. OS Version: 10.12.5 (16F73)
  5. Kernel: Darwin Kernel Version 16.6.0: Fri Apr 14 16:21:16 PDT 2017; root:xnu-3789.60.24~6/RELEASE_X86_64
  6. Hostname: 127.0.0.1
  7. WorkingDir: /
  8. SDK Path: "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk"
  9. Available devices:
  10. 614F8701-3D93-4B43-AE86-46A42FEB905A: iPhone 4s
  11. CD516CF7-2AE7-4127-92DF-F536FE56BA22: iPhone 5
  12. 0D76F30F-2332-4E0C-9F00-B86F009D59A3: iPhone 5s
  13. 3084003F-7626-462A-825B-193E6E5B9AA7: iPhone 6
  14. ...

Xcode中无法使用LLDB的可视化模式,但可以在终端中使用。

  1. (lldb) gui // 在Xcode中执行gui命令会报错:该命令只能在无交互的终端执行

!This is how LLDB GUI mode looks like

结论

这篇文章大致描绘出了LLDB的强大。尽管LLDB存在于我们的开发环境中,但大多数人并没有发掘出它的能量。希望这篇关于LLDB基本功能和自动化调试的概览能对大家有所帮助。

文章遗漏了LLDB的很多功能。像可视调试技巧就没有提到。如果你对这样的主题感兴趣,请在下面的评论中留言。能写一些大家感兴趣的东西很高兴。

鼓励你打开终端,开启LLLDB,输入help 。很详细的文档会呈现出来。尽管这是一个很耗时的过程,但仍然期望你能花时间去阅读这个文档。只有精通你的生产工具才能变得真正的高效。

参考


查看英文原文Debugging Swift code with LLDB

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注