Imrazor's Blog

Stay Hungry, Stay Foolish

使用lldb调试别人的程序

文章翻译自 这里

这是一篇粗略的翻译文章,只翻译第二种方法

首先我们需要一台越狱的设备,并且安装好了ssh

debugserver是一个用来远程gdb或lld调试的程序,当一个设备被标记为开发设备,那么这个程序会被安装到/Developer/usr/bin/debugserver路径下

debugserver的调用方式为:

debugserver [<options>] host:<port> [<prog-name> <arg1> <arg2> ...]

你可以在Xcode中找到debugserver,比如挂载/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/ DeviceSupport/7.0.3\ (11B508)/DeveloperDiskImage.dmg后,他的路径为:/Volumes/DeveloperDiskImage/usr/bin/debugserver

想要用起来debugserver,必须要对他进行签名,我们可以创建一个这样的plist文件:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST   1.0//EN" "http://www.apple.com/DTDs/ PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>com.apple.springboard.debugapplications</key> <true/>
    <key>run-unsigned-code</key>
    <true/>
    <key>get-task-allow</key> <true/> <key>task_for_pid-allow</key> <true/>
</dict> 
</plist>

然后这样签名:

codesign -s - --entitlements entitlements.plist -f debugserver

签名后,将debugserver放回到设备的/Developer/usr/bin/debugserver路径下:

scp debugserver root@x.x.x.x:/Developer/usr/bin/debugserver

之后ssh连接设备,在控制台输入:

./debugserver *:1234 -a "YouTube"

当你看到如下输出时,签名后的debugserver就安装成功了:

My-iPhone-5S:~ root# ./debugserver *:1234 -a "YouTube"
debugserver-300.2 for arm64.
Attaching to process YouTube...
Spawning general listening thread.
Spawning kqueue listening thread.
Listening to port 1234 for a connection from *...

远程进行lldb会比较慢,我们可以通过USB调试,USB调试需要用到iPhoneTunnel:

wget http://cgit.sukimashita.com/usbmuxd.git/snapshot/usbmuxd-1.0.8.tar.bz2
tar xjfv usbmuxd-1.0.8.tar.bz2
cd usbmuxd-1.0.8/python-client/
python tcprelay.py -t 1234:1234

之后所有与localhost:1234的连接都会重定向到USB设备的1234端口:

(lldb) process connect connect://localhost:1234
Process 2612 stopped
* thread #1: tid = 0x30d1e, 0x3ba51a84 libsystem_kernel.dylib`mach_msg_trap + 20,   queue = 'com.apple.main-thread, stop reason = signal SIGSTOP
    frame #0: 0x3ba51a84 libsystem_kernel.dylib`mach_msg_trap + 20
libsystem_kernel.dylib`mach_msg_trap + 20:
-> 0x3ba51a84:  pop    {r4, r5, r6, r8}
   0x3ba51a88:  bx     lr

libsystem_kernel.dylib`mach_msg_overwrite_trap:
   0x3ba51a8c:  mov    r12, sp
   0x3ba51a90:  push   {r4, r5, r6, r8}
(lldb) po [[UIApplication sharedApplication] delegate]
<YTAppDelegate: 0x15e635a0>

此时为断点状态,在控制台输入c,即可继续

gdb和lldb的对照在 这里

Comments