最近在研究iOS的逆向,学到了动态调试LLDB这块,之前在开发的时候只是进行正常的打断点和打印参数,没有去具体的了解整个调试流程。这次做个相关知识的学习并进行个总结。

什么是LLDB?

LLDB是Low Level Debugger的简称,在iOS开发的调试中LLDB是经常使用的,LLDB是Xcode内置的动态调试工具。

LLVM 是一个模块化和可重用的编译器和工具链技术的集合,创始人是 Chris Lattner,也是Swift之父
LLDB 是 LLVM 的子项目,基于LLVM提供的库和Clang构建的优秀的本地调试器。

 

什么叫动态调试?

/Library/Desktop/Xcode.app/Contents/Developer

将程序运行起来,通过下断点、打印等方式,查看参数、返回值、函数调用流程等。

iOS-动态调试LLDB

Xcode动态调试:

  • 关于GCC、LLVM、GDB、LLDB

    Xcode的编译器发展历程:GCC → LLVM

    Xcode的调试器发展历程:GDB → LLDB

  • debugserver⼀开始存放在Mac的Xcode⾥面

    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/De

    viceSupport/9.1/DeveloperDiskImage.dmg/usr/bin/debugserver

  • 当Xcode识别到手机设备时,Xcode会自动将debugserver安装到iPhone上 /Developer/usr/bin/debugserver

  • Xcode调试的局限性

    一般情况下,只能调试通过Xcode安装的APP。

  •  

常⽤LLDB指令:

1、 指令的格式

<command> [<subcommand> [<subcommand>...]] <action> [-options [option-

value]] [argument [argument...]]

  • 命令

  • :子命令

  • :命令操作

  • :命令选项

  • :命令参数

比如给test函数设置断点

breakpoint set -n test

iOS-动态调试LLDB

2、help

  • 查看指令的用法

  • ⽐如help breakpoint、help breakpoint set

3、 expression --

  • 执行一个表达式

    :命令选项

    -- :命令选项结束符,表示所有的命令选项已经设置完毕,如果没有命令选项,--可以 省略

    :需要执行的表达式

expression self.view.backgroundColor = [UIColor redColor]

  • expression、expression --和指令print、p、call的效果⼀一样

  • expression -O --和指令po的效果⼀一样

4、thread backtrace

  • 打印线程的堆栈信息

  • 和指令bt的效果一样

5、thread return [<返回值>]

  • 让函数直接返回某个值,不会执行断点后面的代码

6、frame variable [<变量名>]

  • 打印当前栈帧的变量

7、thread continue、continue、c :程序继续运⾏

8、thread step-over、next、n :单步运行,把子函数当做整体一步执⾏

9、thread step-in、step、s :单步运⾏,遇到子函数会进⼊子函数

10、thread step-out、finish :直接执⾏完当前函数的所有代码,返回到上一个函数

11、thread step-inst-over、nexti、ni

12、thread step-inst、stepi、si

  • si、ni和s、n类似

  • s、n是源码级别

  • si、ni是汇编指令级别

13、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 函数名

14、breakpoint list

  • 列出所有的断点(每个断点都有⾃己的编号)

15、breakpoint disable 断点编号 :禁⽤断点

16、breakpoint enable 断点编号 :启⽤断点

17、breakpoint delete 断点编号 :删除断点

18、breakpoint command add 断点编号

给断点预先设置需要执⾏的命令,到触发断点时,就会按顺序执⾏

19、breakpoint command list 断点编号 查看某个断点设置的命令

20、breakpoint command delete 断点编号 删除某个断点设置的命令

21、内存断点—— 在内存数据发⽣改变的时候触发

  • watchpoint set variable 变量

  • watchpoint set variable self->age

  • watchpoint set expression 地址

  • watchpoint set expression &(self->_age)

  • watchpoint list

  • watchpoint disable 断点编号

  • watchpoint enable 断点编号

  • watchpoint delete 断点编号

  • watchpoint command add 断点编号

  • watchpoint command list 断点编号

  • watchpoint command delete 断点编号

22、image lookup

  • image lookup -t 类型 :查找某个类型的信息

  • image lookup -a 地址 :根据内存地址查找在模块中的位置

  • image lookup -n 符号或者函数名 :查找某个符号或者函数的位置

23、image list

  • 列出所加载的模块信息

  • image list -o -f

    打印出模块的偏移地址、全路径

小技巧

敲Enter,会⾃动执行上次的命令 绝⼤部分指令都可以使用缩写

相关文章: