常用的LLDB指令

LLDB指令的基本格式

  [ [...]]  [-options [option- value]] [argument [argument...]]

对应着

命令 子命令 命令操作 命令选项 命令参数

例如给函数test设置断点:

breakpoint set -n test

help指令

help指令可以帮助我们快速查找LLDB指令的使用方法

help breakpoint
help breakpoint set

expression -- 指令

expression指令被用来执行一个表达式

expression self.view.backgroundColor = [UIColor redColor]
//或者
expression -- self.view.backgroundColor = [UIColor redColor]
  • expression、expression --和指令print、p、call效果等同
  • expression -O -- 和指令po效果等同
(lldb) expression -O -- self.view
>
(lldb) po self.view
>

expression后的 -- 表示命令选项结束符,表示所有的命令选项已经设置完毕,如果没有命令选项,--可以省略。如果expression之后有命令选项,则--不能省略。

thread backtrace

thread backtrace指令的作用是打印线程的堆栈信息,效果和 bt 的效果相同。

thread return []

让函数返回某个值,不会执行之后的代码。如果函数有返回值,在后面跟上返回值,如果函数没有返回值,就直接使用thread return即可

frame variable []

打印当前栈帧的变量

(lldb) frame variable
(ViewController *) self = 0x00007f8f7b0091e0
(SEL) _cmd = "touchesBegan:withEvent:"
(__NSSetM *) touches = 0x00006000020c13e0 1 element
(UITouchesEvent *) event = 0x00006000011c43c0
(lldb) frame variable self
(ViewController *) self = 0x00007f8f7b0091e0

thread相关指令

以下指令从左到右依次表示:指令全称、指令简称、极简指令

  • process continue、continue、c :让程序跳过断点继续运行
  • thread step-over、next、n :单步运行,将子函数当做整体一步执行
  • thread step-in、step、s :单步运行,遇到子函数会进入子函数
  • thread step-out、finish :直接执行完当前函数的所有代码,返回上一个函数
  • thread step-inst-over、nexti、ni
  • thread step-inst、stepi、si

si、ni和s、n指令类似,但是s、n是源码级别,si、ni是汇编指令级别。每一句OC代码会有一条或多条汇编指令构成,s、n指令表示一步一步执行每一句OC代码,而si、ni表示一步一步执行汇编指令。

breakpoint相关指令

breakpoint set

设置断点

  • breakpoint set -a 函数地址
  • breakpoint set -n 函数名
  • breakpoint set -n test
  • breakpoint set -n touchesBegan:withEvent:
  • breakpoint set -n "-[ViewController touchesBegan:withEvent:]"
  • breakpoint set -r 正则表达式
 此处跟上正则表达式,会将所有匹配到的方法都加上断点
  • breakpoint set -s 动态库 -n 函数名
    将指定动态库的指定函数打上断点

breakpoint list

列出所有的断点,每个断点都有单独的编号

breakpoint disable 断点编号

禁用断点

breakpoint enable 断点编号

启用断点

breakpoint delete 断点编号

删除断点

breakpoint command add 断点编号

给指定断点编号的断点预先设置需要执行的命令,到触发断点时,就会按顺序执行预先设置的命令

(lldb) breakpoint command add 5
Enter your debugger command(s).  Type 'DONE' to end.
> po self.view
> p self.view.backgroundColor = [UIColor yellowColor];
> DONE

(lldb) c
Process 76025 resuming
 po self.view
>
 p self.view.backgroundColor = [UIColor yellowColor];
(UICachedDeviceRGBColor *) $5 = 0x00006000035b5cc0

breakpoint command list 断点编号

查看某个编号的断点所有预先设置的命令

(lldb) breakpoint command list 5
Breakpoint 5:
    Breakpoint commands:
      po self.view
      p self.view.backgroundColor = [UIColor yellowColor];

breakpoint command delete 断点编号

删除指定编号断点的所有预设命令

内存断点watchpoint

给指定的内存下断点,当内存中的数据发生改变时会触发

watchpoint set variable 变量

对指定的变量设置内存断点,当变量值改变的时候会触发

watchpoint set variable self->_age
注意:此处不能使用self.age
(lldb) watchpoint set variable self->_age
Watchpoint created: Watchpoint 1: addr = 0x7f9861205220 size = 8 state = enabled type = w
    watchpoint spec = 'self->_age'
    new value: 0

Watchpoint 1 hit:
old value: 0
new value: 20

watchpoint set expression 内存地址

对指定内存地址设置断点,作用和watchpoint set variable相同

watchpoint list

列出所有的内存断点

(lldb) watchpoint list
Number of supported hardware watchpoints: 4
Current watchpoints:
Watchpoint 1: addr = 0x7f9861205220 size = 8 state = enabled type = w
    watchpoint spec = 'self->_age'
    old value: 0
    new value: 20

watchpoint disable 断点编号

禁用内存断点

watchpoint enable 断点编号

启用内存断点

watchpoint delete 断点编号

删除内存断点

watchpoint command add 断点编号

给指定断点编号的内存断点预先设置需要执行的命令,到触发内存断点时,就会按顺序执行预先设置的命令

watchpoint command list 断点编号

查看某个编号的内存断点所有预先设置的命令

watchpoint command delete 断点编号

删除指定编号内存断点的所有预设命令

image模块查询指令

image lookup

模块查询指令

  • image lookup -t 类型
    查找某个类型的信息
(lldb) image lookup -t NSUInteger
Best match found in /Users/pinba/Library/Developer/Xcode/DerivedData/LLDBDemo-dkmvkvkytmalsoensevrvixujugm/Build/Products/Debug-iphonesimulator/LLDBDemo.app/LLDBDemo:
id = {0x1000000ab}, name = "NSUInteger", byte-size = 8, decl = NSObjCRuntime.h:13, compiler_type = "typedef NSUInteger"
     typedef 'NSUInteger': id = {0x1000000b8}, name = "long unsigned int", qualified = "unsigned long", byte-size = 8, compiler_type = "unsigned long"
  • image lookup -a 内存地址
    根据内存地址查找在模块中的位置
(lldb) image lookup -a 0x00000001088b7cdb
      Address: LLDBDemo[0x0000000100000cdb] (LLDBDemo.__TEXT.__text + 187)
      Summary: LLDBDemo`-[ViewController touchesBegan:withEvent:] + 123 at ViewController.m:26:15
  • image lookup -n 符号或函数名
    查找某个符号或者函数的位置
(lldb) image lookup -n "-[ViewController touchesBegan:withEvent:]"
2 matches found in /Users/pinba/Library/Developer/Xcode/DerivedData/LLDBDemo-dkmvkvkytmalsoensevrvixujugm/Build/Products/Debug-iphonesimulator/LLDBDemo.app/LLDBDemo:
        Address: LLDBDemo[0x0000000100000c60] (LLDBDemo.__TEXT.__text + 64)
        Summary: LLDBDemo`-[ViewController touchesBegan:withEvent:] at ViewController.m:22        Address: LLDBDemo[0x0000000100000c60] (LLDBDemo.__TEXT.__text + 64)
        Summary: LLDBDemo`-[ViewController touchesBegan:withEvent:] at ViewController.m:22

image list

列出所有所加载的模块信息

(lldb) image list
[  0] 927601BD-5A00-319C-B8AF-8D501BB5D849 0x0000000100c01000 /Users/pinba/Library/Developer/Xcode/DerivedData/LLDBDemo-dkmvkvkytmalsoensevrvixujugm/Build/Products/Debug-iphonesimulator/LLDBDemo.app/LLDBDemo 
[  1] EEA931D0-403E-3BC8-862A-CBA037DE4A74 0x000000010d352000 /usr/lib/dyld 
[  2] 75369F31-702D-364A-95C3-8AFA9DD4B3A2 0x0000000100c0d000 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/dyld_sim 
......
  • image list -o -f
    打印出模块的偏移地址、全路径
lldb) image list -o -f
[  0] 0x0000000000c01000 /Users/pinba/Library/Developer/Xcode/DerivedData/LLDBDemo-dkmvkvkytmalsoensevrvixujugm/Build/Products/Debug-iphonesimulator/LLDBDemo.app/LLDBDemo
[  1] 0x000000010d352000 /usr/lib/dyld
[  2] 0x0000000100c0d000 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/dyld_sim
......

LLDB小技巧

  • 每次敲Enter键,都会自动执行上次的命令
  • 绝大部分的指令都可以使用缩写
(lldb) breakpoint list
(lldb) br li
(lldb) br l

(lldb) breakpoint set -n test
(lldb) br s -n test

你可能感兴趣的:(常用的LLDB指令)