gdb 安装

在 mac OS 上安装

brew install gdb

也可以从源码进安装,官网,目前GDB对下列语言进行了支持

Ada
Assembly
C
C++
D
Fortran
Go
Objective-C
OpenCL
Modula-2
Pascal
Rust

安装后还不能直接使用,MacOS 对安全性有效高的要求,不允许没有许可的程序运行其它程序

gdb helloworld
(gdb) run

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

提示please check gdb is codesigned - see taskgated(8),原因是还没有为gdb签名,需要在系统的Keychain中创建用于签名的证书

创建certificate

步骤如下:

  1. Launch Keychain Access application: Applications > Utilities > Keychain Access.
  2. From the Keychains list on the left, right-click on the System item and select Unlock Keychain “System”.
  3. From the toolbar, go to Keychain Access > Certificate Assistant > Create a Certificate.
  4. Choose a name (e.g. gdb-cert).
  5. Set Identity Type to Self Signed Root.
  6. Set Certificate Type to Code Signing.
  7. Check the Let me override defaults checkbox.
  8. At this point, you can go on with the installation process until you get the Specify a Location For The Certificate dialogue box. Here you need to set Keychain to System. Finally, you can click on the Create button.
  9. After these steps, you can see the new certificate under System keychains. From the contextual menu of the newly created certificate (right-click on it) select the Get info option. In the dialogue box, expand the Trust item and set Code signing to Always Trust.
  10. Then, from the Keychains list on the left, right-click on the System item and select Lock Keychain “System”.
  11. Finally, reboot your system.

gdb签名

在创建你自己的证书后,就可以给gdb进行签名了,在签名前要先配置gdb-entitlement.xml文件,它允许MacOs信任gdb

<?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.allow-jit</key>
    <true/>
    <key>com.apple.security.cs.allow-unsigned-executable-memory</key>
    <true/>
    <key>com.apple.security.cs.allow-dyld-environment-variables</key>
    <true/>
    <key>com.apple.security.cs.disable-library-validation</key>
    <true/>
    <key>com.apple.security.cs.disable-executable-page-protection</key>
    <true/>
    <key>com.apple.security.cs.debugger</key>
    <true/>
    <key>com.apple.security.get-task-allow</key>
    <true/>
</dict>
</plist>

在命令行运行

codesign --entitlements gdb-entitlement.xml -fs gdb-cert "$(which gdb)"

-fs: 要填写你刚刚生成的证书的名子,$(which gdb): 是gdb的安装路径

接着在home目录创建~/.gdbinit文件,并写入如下内容

echo "set startup-with-shell off" >> ~/.gdbinit

编译文件

为了解决No symbol table is loaded问题,可以在编译时加入-ggdb

gcc hello_world.c -o hello_world -ggdb

# 使用示例
gdb hello_world
(gdb) list
1	#include <stdio.h>
2
3	int my_strlen(char *string);
4
5	int main(int argc, char const *argv[])
6	{
7	  // char *string;
8	  // string = "hello world haha";
9	  char string[] = "hello world haha";
10	  printf("The string '%s' len is \"%d\"\n", string, my_strlen(string));
(gdb) run
Starting program: /Users/ga/m/book/c-lang/ch02-basic/hello_world
[New Thread 0x2603 of process 6002]
[New Thread 0x1803 of process 6002]
warning: unhandled dyld version (16)
The string 'hello world haha' len is "16"
[Inferior 1 (process 6002) exited normally]

使用gdb调试golang

本博没有成功,一直提示错误No symbol table is loaded. Use the "file" command. ,后来查看了Go的官方文档,官方建议使用Delve进行调试。官方原文

Note that Delve is a better alternative to GDB when debugging Go programs built with the standard toolchain. It understands the Go runtime, data structures, and expressions better than GDB. Delve currently supports Linux, OSX, and Windows on amd64. For the most up-to-date list of supported platforms, please see the Delve documentation.

并且在官方给出的示例中,也不保证你照着运行也可以成功

In short, the instructions below should be taken only as a guide to how to use GDB when it works, not as a guarantee of success. Besides this overview you might want to consult the GDB manual.

最后祝你一切顺利