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. || |

相关推荐
coder-pig2 小时前
Holopix AI + TRAE SOLO | 复刻 GBA 游戏-“口袋妖怪“
人工智能·游戏
wanhengidc3 小时前
在线服务器的应用场景都有哪些?
运维·服务器·科技·游戏·智能手机·云计算
UWA6 小时前
Gears 实测室:第八期・全面透视4X游戏性能瓶颈
游戏·性能优化·游戏开发·uwa
yuegu7779 小时前
Electron for鸿蒙PC实战项目之麻将游戏
游戏·electron·harmonyos
搬砖的青蛙19 小时前
游戏运行库整合包,可避免99%游戏启动问题,支持xp-11系统
游戏·运行库·xp-11
嘻哈baby1 天前
游戏/远程桌面的网络延迟优化:从TCP拥塞控制到智能选路
网络·tcp/ip·游戏
Unity打怪升级1 天前
【Unity精品源码】Ultimate Character Controller:高级角色控制器完整解决方案
游戏·unity·ue5·游戏引擎·godot·游戏程序·cocos2d
米芝鱼1 天前
Unity读取Excel转换为二进制数据文件与自定义数据读写
游戏·unity·游戏引擎·excel·urp
YANshangqian1 天前
Opera GX(游戏浏览器)
游戏
米芝鱼1 天前
UnityURP3D管线自定义功能shader
游戏·unity·shader·urp·着色器