MacOS + gdb 亲测有效

MacOS使用GDB调试程序

系统信息: macOS Mojave 10.14.6

安装GDB

安装最新

brew install gdb --HEAD

调试程序出错

错误信息

Unable to find Mach task port for process-id 57906: (os/kern) failure (0x5).
 (please check gdb is codesigned - see taskgated(8))

解决方法

  • 打开钥匙串访问
  • 创建证书MacOS + gdb 亲测有效_第1张图片

    一直点继续,知道下面这一步。选择系统

    如果选择系统,有些人会成功,有些人会失败,比如我,选择系统时会出现下面的错误

    重复上面的步骤,知道要选择登录/系统这一步。此时选择登录,成功创建后如下

可以看到刚刚创建的: 登录 - 我的证书 - gdb_cert


  1. 解锁钥匙串系统
  2. 选中gdb_cert并拖动到系统

    可以看到系统 - 我的证书 - gdb_bert
  3. 更改信任

    双击或右击gdb_cert,打开简介,将信任更改为始终信任
    MacOS + gdb 亲测有效_第2张图片
  • 设置系统偏好
    系统偏好设置 - 用户与群组 - 登录选项 - 解锁 - 编辑

    打开目录实用工具 - 解锁 - 启用Root用户
  • 创建gdb.xml


<plist version="1.0">
<dict>
    <key>com.apple.security.cs.debuggerkey>
    <true/>
dict>
plist>
  • 打开终端
    在终端输入
codesign --entitlements gdb.xml -fs gdb_cert $(which gdb)
  • 重启电脑

后续

运行gdb

第一次运行gdb xxx可能卡死,如下。

[New Thread 0x2703 of process 757]

杀死程序,重新执行即可。

错误参考

1.No symbol table is loaded. Use the “file” command.

使用gdb调试程序时,出现以下错误和警告

  1. 错误
No symbol table is loaded.  Use the "file" command.
  1. 警告
warning: `/BuildRoot/Library/Caches/com.apple.xbs/Binaries/Libc_darwin/install/TempContent/Objects/Libc.build/libsystem_darwin.dylib.build/Objects-normal/x86_64/bsd.o': can't open to read symbols: No such file or directory.
warning: `/BuildRoot/Library/Caches/com.apple.xbs/Binaries/Libc_darwin/install/TempContent/Objects/Libc.build/libsystem_darwin.dylib.build/Objects-normal/x86_64/darwin_vers.o': can't open to read symbols: No such file or directory.
warning: `/BuildRoot/Library/Caches/com.apple.xbs/Binaries/Libc_darwin/install/TempContent/Objects/Libc.build/libsystem_darwin.dylib.build/Objects-normal/x86_64/dirstat.o': can't open to read symbols: No such file or directory.
warning: `/BuildRoot/Library/Caches/com.apple.xbs/Binaries/Libc_darwin/install/TempContent/Objects/Libc.build/libsystem_darwin.dylib.build/Objects-normal/x86_64/dirstat_collection.o': can't open to read symbols: No such file or directory.
warning: `/BuildRoot/Library/Caches/com.apple.xbs/Binaries/Libc_darwin/install/TempContent/Objects/Libc.build/libsystem_darwin.dylib.build/Objects-normal/x86_64/err.o': can't open to read symbols: No such file or directory.
warning: `/BuildRoot/Library/Caches/com.apple.xbs/Binaries/Libc_darwin/install/TempContent/Objects/Libc.build/libsystem_darwin.dylib.build/Objects-normal/x86_64/exception.o': can't open to read symbols: No such file or directory.
warning: `/BuildRoot/Library/Caches/com.apple.xbs/Binaries/Libc_darwin/install/TempContent/Objects/Libc.build/libsystem_darwin.dylib.build/Objects-normal/x86_64/init.o': can't open to read symbols: No such file or directory.
warning: `/BuildRoot/Library/Caches/com.apple.xbs/Binaries/Libc_darwin/install/TempContent/Objects/Libc.build/libsystem_darwin.dylib.build/Objects-normal/x86_64/mach.o': can't open to read symbols: No such file or directory.
warning: `/BuildRoot/Library/Caches/com.apple.xbs/Binaries/Libc_darwin/install/TempContent/Objects/Libc.build/libsystem_darwin.dylib.build/Objects-normal/x86_64/stdio.o': can't open to read symbols: No such file or directory.
warning: `/BuildRoot/Library/Caches/com.apple.xbs/Binaries/Libc_darwin/install/TempContent/Objects/Libc.build/libsystem_darwin.dylib.build/Objects-normal/x86_64/stdlib.o': can't open to read symbols: No such file or directory.
warning: `/BuildRoot/Library/Caches/com.apple.xbs/Binaries/Libc_darwin/install/TempContent/Objects/Libc.build/libsystem_darwin.dylib.build/Objects-normal/x86_64/string.o': can't open to read symbols: No such file or directory.
warning: `/BuildRoot/Library/Caches/com.apple.xbs/Binaries/Libc_darwin/install/TempContent/Objects/Libc.build/libsystem_darwin.dylib.build/Objects-normal/x86_64/variant.o': can't open to read symbols: No such file or directory.

解决方法:
使用g++编译程序时,添加-ggdb选项

g++ -ggdb main.cpp -o main -std=c++11 -Wall

sudo gdb main

参考

  1. https://stackoverflow.com/questions/52699661/macos-mojave-how-to-achieve-codesign-to-enable-debugging-gdb

你可能感兴趣的:(常用工具-配置)