LLDB常用命令
LLDB
全称Low Level Debugger
,是内置于Xcode
中的动态调试工具,是我们开发中必不可少的工具。
查看指令,进入LLDB
模式,输入help
命令回车;查看指令帮助:

一、设置断点
breakpoint set -n test1
:为test1方法
添加断点,-n
等同于--name
breakpoint set -n test1 -n “-[ViewController save]”
breakpoint list
:为多个方法添加断点
breakpoint disable 1
:第一组断点失效
breakpoint disable 1.1
: 第一组第一个失效
breakpoint enable 1
:第一组断点可用
breakpoint delete 1.1
breakpoint set —selector touchesBegan:withEvent:
breakpoint set --file ViewController.m --selector touchesBegan:withEvent:
breakpoint set -r Game:
: 满足包含Game:
这个字符的方法都添加断点
c
:继续,continue
的缩写。
s
:单步走, step
的简写,遇到嵌套子函数会进去。
n
:单步运,next
的简写,遇到嵌套子函数会当做整体一步执行。
b eatWithObject:
:为eatWithObject:方法添加断点
b -a 0x000006329093
: 为地址为0x000006329093的方法添加断点
二、执行代码
expression self
:注意p == expression
, po == expression -o
查看帮助指令:

我们也可以通过
p
指令,修改函数参数值,如:p str = @"2222"
。
三、函数调用栈
bt
:thread backtrace
的简写,函数调用栈
up
& down
:跳转到前一个、后一个方法
frame select 1
:通过调用栈编号,跳转到对应方法
frame variable
:查看当前方法的局部变量,即方法参数
thread return
:线程代码回滚,而且可以修改参数,但会在上一个方法return,不在继续执行
四、 image命令
image list
,查看当前进程加载了哪些库:

0x0000000109caa000
:这个地址是mach-o的真实内存首地址
,即ASLR
。
逆向开发时,某个方法或者变量的真实地址获取方法:真实地址 = mach-o地址 + ASLR
。注意,这里需要减去一个pageZero
(0x100000000
,64 位下是 4个G),因为所有地址都会加上pageZero。
image list -o -f
: 查看所有进程、库
image lookup -t Person
,查看类对象的内存详情:

五、内存断点
watchpoint set variable p1->_name
watchpoint set expression 0x000006329093

breakpoint command add 1
:为第一组断点添加命令
breakpoint command delete 1
breakpoint command list

六、全局断点
target stop-hook add -o "frame variable"
:为所有断点添加命令,打印方法所有变量
target stop-hook list
target stop-hook delete
:删除所有全局断点
undisplay 2
:全局断点第二个不可用

六、逆向 断点
6.1 首先将mach-o文件在hopper中打开

找到eatWithObject:
在mach-o中的内存地址100001cd0
。
6.2 aslr地址
mach-o内存的首地址:0x000000010735d000
,参考上面的image list
指令

6.3 方法物理地址 = ASLR + 虚拟地址
0x000000010735d000 + 0x1cd0 = 0x10735ECD0

七、自动启动LLDB命令
终端根目录,ls -a
,找到.lldbinit
文件。

如果没有,就自己vim
创建一个,然后添加代码
target stop-hook add -o "frame variable"
当然你也可以添加其他命令。然后打开一个项目,添加一个断点,运行,即会打印该方法的参数变量。
注意:.lldbinit
文件,全局有效,在Xcode启动时自动执行,会影响所有的项目。
这个文件影响的是LLDB应用,不是Xcode,终端输入
lldb
指令,可以直接进入lldb模式,通过process 进程id
,也可以直接调试应用。