GDB 和 MacOS

MacOS 的适配这块确实给我气得不轻,LLDB 我又不想用(那谁让 LLDB 哪怕是本地 Debug 都给我上传到 LLDB 服务器上呢?)

具体的话……其实就两个方案,签名和虚拟机/Docker。但是我本人完全不推荐使用签名的方案,这是因为系统架构原因,M 系列的(使用 ARM 体系)的 MacBook 完全没法 Debug x86_64 系统编译而成的文件(反过来也是)——所以我给出的方案是 Docker。(不过说实话这个方案也有够麻烦的)

声明:无论使用什么方案,只要能跑的方案都是好方案,我采用 Docker 的方案只是在电量消耗、性能、复杂程度之间的取舍罢了。如果你的系统不是 ARM 的,那完全可以用签名的方案,那样一劳永逸。

安装 Docker

MacOS 上其实我完全非常不建议使用原生 Docker,有个东西叫 OrbStack,可以去下载这个东西,如果你的 Mac 上面有 Brew 的话,直接 brew install 就完事了。

选择镜像

其实选啥镜像都行,我这里给一个我的 Docker Compose 的配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 该文件部分配置为 AI Generate
services:
pwndbg:
image: skysider/pwndocker:latest
container_name: pwndbg-env
stdin_open: true
tty: true
cap_add:
- SYS_PTRACE
security_opt:
- seccomp:unconfined
volumes:
- ./workspace:/workspace
- ./binaries:/binaries
working_dir: /workspace
command: /bin/bash

# Alternative pwndbg image
pwndbg-alt:
image: disconnect3d/pwndbg:latest
container_name: pwndbg-alt-env
stdin_open: true
tty: true
cap_add:
- SYS_PTRACE
security_opt:
- seccomp:unconfined
volumes:
- ./workspace:/workspace
- ./binaries:/binaries
working_dir: /workspace
command: /bin/bash
profiles: ["alt"] # Use with --profile alt to run this instead

安装 qemu(可选)

还是那句话,我的系统不是 x86_64 架构的,所以直接 GDB 依然还是会报错的。说实话,这个可选也是白写,因为只要使用 Mac 就大概率会遇到这个问题——二进制文件的编译环境和当前系统环境不一致。

当然,安装其实也就几条指令:

1
2
3
docker-compose exec pwndbg bash
apt-get update # 在容器里执行
apt install qemu-user-static

你问我这点东西在 Mac 上不也能做到吗?答案是——Mac 不支持 qemu user

好了完事。

远程调试

等会儿?远程调试?

是的,要不然你咋用 GDB,GDB 用的还是你自己的系统运行的啊。

1
qemu-<system> -g <port> <file_path> & # 例如 qemu-x86_64 -g 1234 <file_path> &

然后用 GDB 的远程调试:

1
2
gdb -q
target remote <port>

这样差不多就可以进行动态调试了。

所以,为什么最推荐的环境还是 Windows 下的 WSL 不是没有道理的。