级别: ★☆☆☆☆
作者: Xs.H https://www.jianshu.com/c/b3bd94559163
审校: QiShare 团队 https://www.jianshu.com/c/b3bd94559163
在 iOS 项目开发过程中, 常用到静态分析 (Analyze), 断点(Breakpoint) 和控制台 (Console) 进行代码调试, 其中控制台调试最核心的的就是 LLDB 命令. 本篇文章介绍作者在项目中最常用到的 LLDB 命令调试方法.
一, 简介
LLDB 是新一代高性能调试器. 它构建为一组可重用的组件, 可以高度利用较大的 LLVM 项目中的现有库, 例如 Clang 表达式解析器和 LLVM 反汇编程序. LLDB 是 Mac OS X 上 Xcode 的默认调试器, 支持在桌面和 iOS 设备和模拟器上调试 C,Objective-C 和 C ++. LLDB 项目中的所有代码都是在标准 LLVM 许可证下提供的, 这是一种开源的 "BSD 风格" 许可证. 更多介绍可从 LLDB Homepage http://lldb.llvm.org/ 获取.
二, 帮助
LLDB 命令格式如下: <命令名称> <命令动作> [- 可选项 [可选项的值]] [参数 1 [参数 2...]]
LLDB 命令的各部分由空格分割, 如果参数中包含空格, 则需要使用双引号括起参数, 如果参数中包含双引号或者反斜杠, 则需要使用反斜杠进行转义.
LLDB 命令有非常多的功能, 完全背下来不太容易, 也没必要. 开发者可以使用 help 命令查看相关命令的用法, 甚至可以查看 help 命令的用法.
- (lldb) help help
- Show a list of all debugger commands, or give details about a specific
- command.
- Syntax: help [<cmd-name>]
- Command Options Usage:
- help [-ahu] [<cmd-name> [<cmd-name> [...]]]
- -a ( --hide-aliases )
- Hide aliases in the command list.
- -h ( --show-hidden-commands )
- Include commands prefixed with an Underscore.
- -u ( --hide-user-commands )
- Hide user-defined commands from the list.
- This command takes options and free-form arguments. If your arguments
- resemble option specifiers (i.e., they start with a - or --), you must use
- '--' between the end of the command options and the beginning of the
- arguments.
三, 执行
在 LLDB 中, 执行命令 expression 是最基础的命令, 简写为 expr 或者 e, 语法为: expression <cmd-options> -- <expr>, 它的作用是执行一个表达式, 并输出表达式返回的结果. 示例如下.
- //! 输出 count 的值
- (lldb) expression count
- (NSUInteger) $0 = 10
- //! 执行 string 的拼接字符串方法
- (lldb) expression [string stringByAppendingString:@"732"]
- (__NSCFString *) $1 = 0x00006000006ac870 @"QiShare732"
- //! 修改 view 的颜色
- (lldb) expression self.view.backgroundColor = [UIColor redColor]
- (UICachedDeviceRGBColor *) $2 = 0x0000600001d74780
- (lldb) expression [CATransaction flush]
- //!<因为断点会终止 UI 线程, 执行 [CATransaction flush] 命令可以渲染出修改后的界面
四, 打印
打印命令 print 是最常用的命令, 简写为 pri 或者 p, 语法为: print <expr>, 它的作用是打印变量或者表达式. 通过 help print 会发现 print 其实是 expression -- 命令的简写:'print' is an abbreviation for 'expression --', 其中 -- 标识选项的结束和参数的开始. 同样常用的 expression 简写命令还有 po 和 call. 其中 po 表示 print object, 用来打印对象, call 用来调用某个方法. 示例如下:
- (lldb) expression -- self.view
- (UIView *) $4 = 0x00007f8ca8401690
- (lldb) e self.view
- (UIView *) $5 = 0x00007f8ca8401690
- (lldb) p self.view
- (UIView *) $6 = 0x00007f8ca8401690
- (lldb) po self.view
- <UIView: 0x7f8ca8401690; frame = (0 0; 375 812); autoresize = W+H; layer = <CALayer: 0x6000008a1dc0>>
- (lldb) call self.view
- (UIView *) $8 = 0x00007f8ca8401690
另外, 开发者可以按照 print/<fmt > 的语法为 print 命令指定打印格式:
- p/x //!<以十六进制打印整数
- p/d //!< 以带符号的十进制打印整数
- p/u //!< 以无符号的十进制打印整数
- p/o //!< 以八进制打印整数
- p/t //!< 以二进制打印整数
- p/a //!< 以十六进制打印地址
- p/c //!< 打印字符常量
- p/f //!< 打印浮点数
- p/s //!< 打印字符串
- p/r //!< 格式化打印
示例如下:
- (lldb) p count
- (NSUInteger) $0 = 10
- (lldb) p/t count
- (NSUInteger) $1 = 0b0000000000000000000000000000000000000000000000000000000000001010
- (lldb) p/o count
- (NSUInteger) $2 = 012
- (lldb) p/x count
- (NSUInteger) $3 = 0x000000000000000a
- (lldb) p/x string
- (__NSCFConstantString *) $4 = 0x000000010416a0b8 @"QiShare"
- (lldb) p/c string
- (__NSCFConstantString *) $5 = \xb8\xa0\x16\x04\x01\0\0\0 @"QiShare"
- (lldb) p/s string
- (__NSCFConstantString *) $6 = @"QiShare"
- (lldb) p/a string
- (__NSCFConstantString *) $7 = 0x000000010416a0b8 @"QiShare" @"QiShare"
五, 线程
thread 是线程相关的命令, 语法为 thread <subcommand> [<subcommand-options>], 它可以接受不同的可选参数, 以实现丰富的功能. 其中 thread list,thread backtrace 和 thread return 较为常用. 开发者可以使用 thread list 命令查看当前的所有线程, 示例如下:
- (lldb) thread list
- Process 4031 stopped
- * thread #1: tid = 0x25cac, 0x0000000104167e23 QiDebugDemo`-[QiConsoleViewController testLLDBCommands](self=0x00007f850df0bbf0, _cmd="testLLDBCommands") at QiConsoleViewController.m:34, queue = 'com.apple.main-thread', stop reason = breakpoint 4.1
- thread #2: tid = 0x25d2f, 0x00000001079ff28a libsystem_kernel.dylib`__workq_kernreturn + 10
- thread #3: tid = 0x25d30, 0x00000001079ff28a libsystem_kernel.dylib`__workq_kernreturn + 10
- thread #4: tid = 0x25d31, 0x00000001079ff28a libsystem_kernel.dylib`__workq_kernreturn + 10
- thread #5: tid = 0x25d32, 0x00000001079ff28a libsystem_kernel.dylib`__workq_kernreturn + 10
- thread #6: tid = 0x25d33, 0x00000001079ff28a libsystem_kernel.dylib`__workq_kernreturn + 10
- thread #7: tid = 0x25d3e, 0x00000001079f520a libsystem_kernel.dylib`mach_msg_trap + 10, name = 'com.apple.uikit.eventfetch-thread'
thread backtrace 命令可以方便地供开发者查看线程堆栈信息, 简写为 bt. 比如, 当程序崩溃的时候, 开发者可以查看堆栈调用列表. 示例如下.
- (lldb) thread backtrace
- * thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
- frame #0: 0x0000000104cc2705 libobjc.A.dylib`objc_exception_throw
- frame #1: 0x00000001056704ec CoreFoundation`_CFThrowFormattedException + 194
- frame #2: 0x00000001057a6b00 CoreFoundation`-[__NSArrayI objectAtIndexedSubscript:] + 96
- * frame #3: 0x00000001043a1df7 QiDebugDemo`-[QiConsoleViewController testLLDBCommands](self=0x00007fadc7c50400, _cmd="testLLDBCommands") at QiConsoleViewController.m:33
- frame #4: 0x00000001043a1d5a QiDebugDemo`-[QiConsoleViewController viewDidLoad](self=0x00007fadc7c50400, _cmd="viewDidLoad") at QiConsoleViewController.m:26
- ...
- frame #18: 0x00000001056830be CoreFoundation`__CFRunLoopDoObservers + 430
- frame #19: 0x0000000105683751 CoreFoundation`__CFRunLoopRun + 1537
- frame #20: 0x0000000105682e11 CoreFoundation`CFRunLoopRunSpecific + 625
- frame #21: 0x000000010ddd51dd GraphiCSServices`GSEventRunModal + 62
- frame #22: 0x000000010a1db81d UIKitCore`UIApplicationMain + 140
- frame #23: 0x00000001043a2450 QiDebugDemo`main(argc=1, argv=0x00007ffeeb85df90) at main.m:7
- frame #24: 0x0000000107858575 libdyld.dylib`start + 1
在调试过程中, 开发者可以使用 thread return 命令终端某个方法并返回一个想要的值. 示例如下.
- (lldb) thread return string
- (lldb) continue
- 2019-02-27 17:22:47.323225+0800 QiDebugDemo[5071:222700] resultString: Qi_Share
六, 断点
作者在 iOS 调试方法: 断点这篇文章中介绍过断点的用法. 其实, 可视化的断点都可以使用 LLDB 语法来实现. 比如下图中的 1,2,3,4,5 都能用 LLDB 命令表达.
启用 / 禁用断点(breakpoint enable/disable)
继续执行程序(continue)
执行下一步(next)
进入方法(step)
跳出方法(finish)
在断点相关操作中, 因为 Xcode 已经集成了可视化的断点操作工具, 所以 breakpoint 命令并不被常用. 但是, breakpoint 命令拥有着十分强大的功能, 语法为: breakpoint <subcommand> [<command-options>], 主要命令示例如下.
- //! 查看所有断点
- (lldb) breakpoint list
- //! 为所有类中的 viewDidAppear: 设置断点
- (lldb) breakpoint set -n viewDidAppear:
- //! 为 QiConsoleViewController.m 文件中的 testLLDBCommands 方法设定断点
- (lldb) breakpoint set -f QiConsoleViewController.m -n testLLDBCommands
- //! 为 QiConsoleViewController.m 文件中的第 32 行代码设定断点
- (lldb) breakpoint set -f QiConsoleViewController.m -l 32
- //! 为 handleString: 方法设定条件断点, 条件为 string != nil
- (lldb) breakpoint set - handleString: -c string != nil
七, 观察点
想比较于 breakpoint 是对方法生效的断点, watchpoint 则是对地址生效的断点. watchpoint 类似于 KVO 的工作原理, 当观察到属性地址里面的东西改变时, 就让程序中断, 其语法为: watchpoint <subcommand> [<command-options>]. 其应用场景示例如下.
- (lldb) watchpoint set variable string
- Watchpoint created: Watchpoint 1: addr = 0x7ffeef497360 size = 8 state = enabled type = w
- declare @ '/Users/huangxianshuai/Desktop/Products/QiShare/QiDebugDemo/QiDebugDemo/QiConsoleViewController.m:33'
- watchpoint spec = 'string'
- new value: 0x00000001007670b8
- (lldb) next
- Watchpoint 1 hit:
- old value: 0x00000001007670b8
- new value: 0x0000000100767138
- (lldb) image lookup -a 0x00000001007670b8
- Address: QiDebugDemo[0x00000001000040b8] (QiDebugDemo.__DATA.__cfstring + 32)
- Summary: @"QiShare"
- (lldb) image lookup -a 0x0000000100767138
- Address: QiDebugDemo[0x0000000100004138] (QiDebugDemo.__DATA.__cfstring + 160)
- Summary: @"huang"
image lookup -a 是 image lookup -address 的简写, 可以查看参数地址的内容.
总结
文章仅列举了作者平常使用到的一些 LLDB 命令, 这些命令的确能提高调试效率. 而更多的 LLDB 命令可以从 LLDB Homepage http://lldb.llvm.org/ 或者其他网络资源中获取.
小编微信: 可加并拉入《QiShare 技术交流群》.
关注我们的途径有:
- QiShare(简书) https://www.jianshu.com/u/3db23baa08c7
- QiShare(掘金)
- QiShare(知乎) https://www.zhihu.com/people/edit
- QiShare(GitHub) https://github.com/QiShare
- QiShare(CocoaChina) http://www.cocoachina.com/bbs/u.php?tid=658244
- QiShare(Stack Overflow) https://stackoverflow.com/users/10118400/qishare
- QiShare(微信公众号)
来源: https://juejin.im/post/5c863311e51d4522507757dc