参考:
安装:
$ brew tap homebrew/dupes
$ brew install gdb
安装目录 /usr/local/bin/gdb
查看版本:
$gdb -v
GNU gdb (GDB) 7.11 # GDB 版本必须大于 7.1
如果使用gdb直接调试程序会报以下错误:
$ gdb a.out
Starting program: a.out
Unable to find Mach task port for process-id XXXXX: (os/kern) failure (0x5).
(please check gdb is codesigned - see taskgated(8))
Start Keychain Access application (/Applications/Utilities/Keychain Access.app)
出错的原因是因为mac下,访问其他的进程需要gdb进行数字签名。需要生成一个签名证书且注册到System中。
Finder -> Go -> Utilities -> Keychain Access -> Certificate Assistant -> Create a Certificate ...
Certtificat Type: Code Signing
添加成功后,双击证书, 在Turst section中选择 Always Trust。
然后对于gdb使用证书签名
$ sudo killall taskgated
$ codesign -fs gdb-cert /usr/local/bin/gdb
Debugging Go Code with GDB(官方文档)
发布版本删除调试符号
go build -ldflags “-s -w”
方便调试关闭内联优化
go build -gcflags “-N -l”
$gdb regexp.test -d $GOROOT
(gdb) source ~/go/src/runtime/runtime-gdb.py
(gdb) list
(gdb) list line
(gdb) list file.go:line
(gdb) break line
(gdb) break file.go:line
(gdb) disas
(gdb) bt
(gdb) frame n
(gdb) info locals
(gdb) info args
(gdb) p variable
(gdb) whatis variable
(gdb) info variables regexp
(gdb) x/15xb 0x42121240
Go Extensions
(gdb) p var
(gdb) p $len(var)
(gdb) p $dtype(var)
(gdb) iface var
(gdb) info goroutines
(gdb) goroutine n cmd
(gdb) help goroutine
gdb 对于 interface{} 类型不能够提供反解析。
跨平台的go debug的工具
go get github.com/mailgun/godebug
在需要调试的代码中添加一下代码:
_ = "breakpoint"
启动测试: $ godebug run -instrument= gofiles... [arguments...]
例如:
$ godebug run -instrument=github.com/astaxie/beego/orm main.go
The current commands are:
command | result |
---|---|
h(elp) | show help message |
n(ext) | run the next line |
s(tep) | run for one step |
c(ontinue) | run until the next breakpoint |
l(ist) | show the current line in context of the code around it |
p(rint) [expression] | print a variable or any other Go expression |
q(uit) | exit the program |
godebug还是一个比较新的项目,能扩张的命令也比较少,但是对于一般的调试还是非常方便,能够很好支持golang的各种类型,例如 ** interface{} **
参考: