XCode动态调试原理
-
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
动态调试任意APP
debugserver的权限问题
默认情况下,/Developer/usr/bin/debugserver缺少一定的权限,只能调试通过XCode安装的APP,无法调试其他APP,如果希望调试其他APP,需要对debugserver重新签名
,签上2个调试相关的权限
get-task-allow
task_for_pid-allow
如何给debugserver签上权限
- iPhone上的
/Developer
目录是只读
的,无法直接对/Developer/usr/bin/debugserver
签名,需要先把debugserver复制到Mac - 通过Idid命令导出文件以前的签名权限
$ ldid -e debugserver > debugserver.entitlements
给debugserver.plist
文件加上get-task-allow
和task_for_pid-allow
权限
- 通过ldid命令重新签名
$ ldid -Sdebugserver.entitlements debugserver
- 将已经签好权限的debugserver放到/usr/bin目录,便于找到debugserver指令
- 关于权限的签名,也可以使用codesign
# 查看权限信息
$ codesign -d --entitlements - debugserver
# 签名权限
$ codesign -f -s - --entitlements debugserver.entitlements debugserver
# 或者简写为
$ codesign -fs- --entitlements debugserver.entitlements debugserver
让debugserver附加到某个APP进程
$ debugserver *:端口号 -a 进程
-
*:端口号
使用iPhone的某个端口启动debugserver服务(只要不是保留端口号就行) -
-a 进程
输入APP的进程信息(进程ID或者进程名称)
在Mac上启动LLDB,远程连接iPhone上的debugserver服务
- 启动LLDB
$ lldb
(lldb)
- 连接debugserver服务
(lldb) process connect connect://手机IP地址:debugserver服务端口号
- 使用LLDB的c命令让程序先继承运行
(lldb) c
- 接下来就可以使用LLDB命令调试APP
通过debugserver启动APP
$ debugserver -x auto *:端口号 APP的可执行文件路径
常用的LLDB指令
指令的格式
<command> [<subcommand> [<subcommand>...]] <action> [-options [option- value]] [argument [argument...]]
command : 命令
subcommand : 子命令
action : 命令操作
options : 命令选项
argument : 命令参数
(1)help
查看指令的用法
比如help breakpoint 、 help breakpoint set
(2)expression
执行一个表达式
expression 、expression -- 和 指令print、p、call的效果一样
expression -O -- 和指令po的效果一样
(3)thread backtrace
打印线程的堆栈信息
和指令bt
的效果一样
(4)thread return [<expr>]
让函数直接返回某个值,不会执行断点后面的代码
(5)frame variable [<variable-name>]
打印当前栈帧的变量
(6)thread continue
(缩写:continue
或者 c
)
程序继续运行
(7)thread step-out
(缩写:finish
):直接执行完当前函数的所有代码,返回到上一个函数
(8)单步运行:源码级别(指程序员写代码级别)
thread step-over
(缩写:next
或者 n
):把子函数当做整体一步执行
thread step-in
(缩写:step
或者 s
):遇到子函数会进入子函数
(9)单步运行:指令级别(指汇编代码级别)
threa step-inst-over
(缩写:nexti
或者ni
):
threa step-inst
(缩写:stepi
或者si
):
(10)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 函数名
(11)breakpoint list
列出所有的断点(每个断点都有自己的编号)
(12)breakpoint disable 断点编号
禁用编号
(13)breakpoint enable 断点编号
启动断点
(14)breakpoint delete 断点编号
删除断点
(15)breakpoint command add 断点编号
给断点预先设置需要执行的命令,到触发断点时,就会按顺序执行
(16)breakpoint command list 断点编号
查看某个断点设置的命令
(17)breakpoint command delete 断点编号
删除某个断点设置的命令
(18)内存断点
在内存数据发生改变的时候触发
watchpoint set variable 变量
watchpoint set variable self->_age
watchpoint set expression 地址
watchpoint set expression &(self->_age)
watchpoint list
watchpoint disabel 断点编号
watchpoint enable 断点编号
watchpoint delete 断点编号
watchpoint command add 断点编号
watchpoint command list 断点编号
watchpoint command delete 断点编号
(19)image lookup
image lookup -t 类型
查看某个类型的信息
image lookup -a 地址
根据内存地址查找在模块中的位置
image lookup -n 符号或者函数名
查找某个符号或函数的位置
(20)image list
列出所加载的模块信息
image list -o -f
打印出模块的偏移地址、全路径
小技巧:
敲Enter,会自动执行上一次的命令