新公司工作用的是Mac Pro,最近在学golang,偶尔需要用一下GDB,但发现Mac上面要想使用gdb真心不容易啊,这里记录一下自己遇到的问题(当然你不一定遇到,仅供参考)。

问题1

安装的话,使用brew安装即可。但是可能因为权限的问题,最终软连接没有生成,你的gdb可能没有被软连接到PATH里面,导致你敲gdb命令的时候报命令不存在,这种的话你就自己创建一个软链接到系统的PATH下即可。比如我的:

➜  ~ type gdb
gdb is /usr/local/bin/gdb
➜  ~ ll /usr/local/bin/gdb
lrwxr-xr-x  1 Allan  admin    26B 10  9 09:35 /usr/local/bin/gdb -> ../Cellar/gdb/7.12/bin/gdb

问题2

gdb运行程序的时候报如下错误:

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

这个需要给gdb做签名才可以,步骤如下(来自stackoverflow):

  • Open application “Keychain Access” (/Applications/Utilities/Keychain Access.app)
  • Open menu /Keychain Access/Certificate Assistant/Create a Certificate...
  • Choose a name (gdb-cert in the example), set “Identity Type” to “Self Signed Root”, set “Certificate Type” to “Code Signing” and select the “Let me override defaults”. Click “Continue”. You might want to extend the predefined 365 days period to 3650 days.
  • Click several times on “Continue” until you get to the “Specify a Location For The Certificate” screen, then set “Keychain to System”.
  • If you can't store the certificate in the “System” keychain, create it in the “login” keychain, then export it. You can then import it into the “System” keychain.
  • In keychains select “System”, and you should find your new certificate. Use the context menu for the certificate, select “Get Info”, open the “Trust” item, and set “Code Signing” to “Always Trust”.
  • You must quit “Keychain Access” application in order to use the certificate and restart “taskgated” service by killing the current running “taskgated” process. Alternatively you can restart your computer.
  • Finally you can sign gdb: sudo codesign -s gdb-cert /usr/local/bin/gdb

运气好的话,这样可能就可以用了,但是我的还不行,出现了问题3.

问题3

gdb运行程序的时候报如下错误:

During startup program terminated with signal ?, Unknown signal.

好吧,这个和Mac的SIP服务有关。苹果在新版本的系统中增加了这个保护,这个保护就是不让软件使用root权限去运行,对于普通用户是个好事,但对于开发者就不是很方便了(我就知道这个保护是的某些mac下的软件无法运行,比如TotalTerminal)。所以为了使用gdb我关掉了它(当然还有其他解决方案,但我觉得这样更好):

  • 重启Mac,开机的时候按住Cmd+R进入Recovery模式
  • 从工具菜单中启动终端,输入csrutil disable命令,即可关闭SIP服务。
  • 重启电脑。

当然日后想重开SIP的话,重复上面的步骤,把disable换成enable就可以了

这样就可以愉快的使用GDB了。

PS:如果使用GDB调试go,一定要选择高版本,低版本不支持。