iOS 64位程序调试环境搭建

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ### 一、背景 调试ios程序经常使用gdb,但是gdb还未支持arm64,需要使用XCode的lldb调试IOS下64位程序。因为xcode默认调试工具是lldb,开始各种尝试在xcode中使用Debug-》Attach to Process 直接attach进程,但是只能看见进程却无法挂接。最终发现可以使用lldb命令行方式调试,lldb调试方法跟gdb最大的不同,在于前者是用OSX中的lldb远程连接debugserver,由debugserver作为lldb和iOS的中转,执行命令和返回结果;而后者是gdb直接运行在iOS上。但对于一般的开发、调试来说,区别不大。 ### 二、基本知识 首先了解下各个iphone对应的cpu类型,这些知识将在调试准备工作中用到: |-------------------------------|--------| | 设备 | Cpu类型 | | iPhone 4 | armv7 | | iPhone 4s | armv7 | | iPhone 5 | armv7s | | iPhone 5c | armv7s | | iPhone 5s | armv64 | | iPhone 6 | armv64 | | iPhone 6 Plus | armv64 | | iPad 2 | armv7 | | iPad mini | armv7 | | iPad with Retina display | armv7s | | iPad Air | armv64 | | iPad Air 2 | armv64 | | iPad mini with Retina display | armv64 | | iPad mini 2 | armv64 | | iPad mini 3 | armv64 | | iPad touch 5 | armvv7 | ### 三、lldb调试 #### 3.1 准备工作 #### 使用lldb调试需要准备服务端程序,即debugserver。在默认情况下,iOS上并没有安装debugserver,只有在设备连接过一次Xcode,并在Window-》Devices菜单中添加此设备后,debugserver才会被Xcode安装到iOS的"/Developer/usr/bin/"目录下。确定有了debugserver后再一次执行以下步骤: #### 1、拷贝debugserver到本地计算机中: scp root@iOSDeviceIP:/Developer/usr/bin/debugserver ~/debugserver 2、由于ldid不支持的fat二进制文件,所以要给debugserver瘦身,通过lipo指定要支持的指令类型,以arm64为例: lipo -thin arm64 ~/debugserver -output ~/debugserver 3、给debugserver添加task_for_pid权限,保存以下内容为ent.xml文件: com.apple.springboard.debugapplications get-task-allow task_for_pid-allow run-unsigned-code 然后执行以下命令添加权限: ldid -Sent.xml debugserver 4、给debugserver重新签名,保存以下内容为entitlements.plist文件: com.apple.springboard.debugapplications run-unsigned-code get-task-allow task_for_pid-allow 然后运行以下命令给的debugserver签名: codesign -s - --entitlements entitlements.plist -f debugserver 5、到此准备工作完成,重新拷贝debugserver回手机中: scp ~/debugserver root@iOSDeviceIP:/usr/bin/debugserver #### 3.2 调试 利用的lldb调试首先需要的启动debugserver的并,attach到要调试的进程,然后在通过lldb连接debugserver,具体步骤如下: 1、 从shell登录ios设备: ssh root@iOSDeviceIP 然后会提示输入root密码,输入即可,如下图所示: 2、 如果是第一次使用还需给debugserver添加可执行属性: chmod +x /usr/bin/debugserver 3、 接下来直接使用debugserver来attach一个进程,debugserver命令行格式如下: debugserver [] host: [ ...] 其中各个参数选项如下: |-------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | Option | Effect | | -a process | Attach debugserver to process . The process can be a pid or executable name. | | -d integer | Assign the waitfor-duration. | | -f ? | ? | | -g | Turn on debugging. | | -i integer | Assign the waitfor-interval. | | -l filename | Log to file. Set filename to stdout to log to standard output. | | -t | Use task ID instead of process ID. | | -v | Verbose. | | -w ? | ? | | -x method --launch=method | How to launch the program. Can be one of: ·auto: Auto-detect the best launch method to use. ·fork: Launch program using fork(2) and exec(3). ·posix: Launch program using posix_spawn(2). ·backboard: Launch program via BackBoard Services. | | --lockdown | Obtain parameters from lockdown (?) | 其中最常用的参数为-a,后接进程名或进程pid,命令如下: debugserver *:1234 -a "Clover" debugserver *:1234 -a 444 至此ios设备端操作完毕。 4、 另打开一个shell,启动lldb客户端,直接输入lldb进入lldb命令行模式。 5、 在lldb命令行的连接debugserver: process connect connect://iOSDeviceIP:1234 在我实际使用过程中,此操作需要较长时间,连接成功后如下图所示: 6、 接下来就是一系列lldb相关命令操作: ### 四、lldb & gdb命令 可能大家使用较多的是gdb,对gdb命令更为熟悉,为了方便大家查阅、对比lldb和gdb命令,罗列出了一些常用命令供大家参考。 EXECUTION COMMANDS |---------------------------|-------------------------------------------------------| | GDB | LLDB | | Launch a process no arguments. || | (gdb) run (gdb) r | (lldb) process launch (lldb) run (lldb) r | | Launch a process with arguments . || | (gdb) run (gdb) r | (lldb) process launch -- (lldb) r | | Launch a process for with arguments a.out 1 2 3 without having to supply the args every time. || |

相关推荐
m0_748251729 小时前
Android webview 打开本地H5项目(Cocos游戏以及Unity游戏)
android·游戏·unity
gantengsheng9 小时前
基于51单片机和OLED12864的小游戏《贪吃蛇》
单片机·嵌入式硬件·游戏·51单片机
264玫瑰资源库9 小时前
从零开始C++棋牌游戏开发之第三篇:游戏的界面布局设计
开发语言·c++·python·游戏·pygame·源代码管理
264玫瑰资源库9 小时前
从零开始C++游戏开发之第七篇:游戏状态机与回合管理
开发语言·c++·游戏
windwind200014 小时前
游戏关卡设计方法的杂感
游戏·关卡设计
白乐天_n1 天前
腾讯游戏安全移动赛题Tencent2016A
安全·游戏
这是我581 天前
C++打小怪游戏
c++·其他·游戏·visual studio·小怪·大型·怪物
tealcwu1 天前
【游戏设计原理】21 - 解谜游戏的设计
游戏·游戏策划
清梦20201 天前
经典问题---跳跃游戏II(贪心算法)
算法·游戏·贪心算法
tealcwu1 天前
【游戏设计原理】22 - 石头剪刀布
游戏·游戏策划