Skip to content

Instantly share code, notes, and snippets.

@dehengxu
Last active July 15, 2018 04:40
Show Gist options
  • Save dehengxu/15825d6533a86f3b7ab99112cd9007e5 to your computer and use it in GitHub Desktop.
Save dehengxu/15825d6533a86f3b7ab99112cd9007e5 to your computer and use it in GitHub Desktop.

使用 gdb 在 macOS 上进行调试

步骤1: 为 gdb 签名

  1. 在你的系统KeyChain 中创建一个自签名的根证书,用途选择 代码签名codesign,我的证书常用名为 gdb-cert
  2. 配置该证书的 codesign 为始终信任
  3. 用这个证书来为 gdb 程序签名。 codesign -fs gdb-cert /usr/local/Cellar/gdb/8.0.1/bin/gdb

步骤2: 启动 gdb

  1. gdb
  2. 通过 file 指令加载目标执行文件。file ./build/a.out
  3. 通过 break 设置文件的断点行数。 break main.c 33 设置 main.c 文件的断点在 33 行。
  4. 输入 run 启动程序

对于运行中的程序,可以通过 gdb --pid xxxx 的形式来挂载程序到 gdb 中。

是不是看起来很简单,但是其中有几个个主要的坑,需要特别留神,不然我也不用浪费宝贵的水资源了

坑1:签名的存储区一定要在 系统[System] 区,否则 macOS 不会授予 admin 权限。 坑2:你的当前用户拥有 admin 权限,如果没有请自行解决,否则即便启动 gdb 也会提示无法提取对应的进程。 坑3:被调试的执行程序一定要具备调试符号,symbol。 通过 gcc 等编译的程序需要为 CFLAGS增加 -O0 -g3 等调试参数。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment