问题:

本地 MacOS 上通过 GDB 调试 golang 程序,结果提示 No symbol table is loaded. Use the "file" command

解决:

SHELL
go build -gcflags "-N -l" -ldflags=-compressdwarf=false main.go

问题:

SHELL
(gdb) run

Starting program: /usr/local/bin/fabnacci

Unable to find Mach task port for process-id 23330: (os/kern) failure (0x5).

(please check gdb is codesigned - see taskgated(8))

解决:

  1. 创建证书:
  2. 打开 Keychain Access 应用程序(/Applications/Utilities/Keychain Access.app)
  3. 执行菜单 钥匙串访问 -> 证书助理 -> 创建证书
  4. 创建一个位置位于系统的代码签名自签名根证书,并添加信任。
  5. Mojave (10.14) 之前的系统,直接执行:codesign -fs gdb-cert $(which gdb)
  6. 如果是 Mojave (10.14) 之后的系统,先创建一个文件 gdb-entitlement.xml,内容为:
SHELL
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">

<dict>

<key>com.apple.security.cs.debugger</key>

<true/>

</dict>

</plist>
  1. codesign --entitlements gdb-entitlement.xml -fs gdb-cert $(which gdb)

参考阅读#

Debugging Go Program