1 获取源码和编译
1.1 获取源码
git clone https://github.com/golang/go.git
1.2 编译
禁止优化
export GO_GCFLAGS="-N -l"
编译
sh all.bash
2 调试源码
2.1 准备GDB
安装gdb
brew install gdb
给固定板签名创建证书参考https://segmentfault.com/q/1010000004136334,需要注意的是
如果是之前的系统,直接执行:
codesign -fs gdb-cert $(which gdb)
如果是 Mojave (10.14) 之后的系统,
先创建一个文件 gdb-entitlement.xml
,内容为:
<?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>
</pre>
再执行:
codesign --entitlements gdb-entitlement.xml -fs gdb-cert $(which gdb)
2.2 编译调试
首先禁止压缩(解决No debugging symbols found in hello)
export GOFLAGS="-ldflags=-compressdwarf=false"
编译
god build -gcflags "-l" hello.go
接着gdb
出现
[New Thread 0x2603 of process 34102]
[New Thread 0x1903 of process 34102]
During startup program terminated with signal SIGTRAP, Trace/breakpoint trap.
需要更换 gdb v8.3 到 gdb v8.0.1
brew uninstall gdb
brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/c3128a5c335bd2fa75ffba9d721e9910134e4644/Formula/gdb.rb
完美
(gdb) b main.main
Breakpoint 2 at 0x10963c0: file /opt/hello.go, line 16.
(gdb) r
Starting program: /opt/hello
[New Thread 0x2403 of process 58373]
warning: unhandled dyld version (15)
[New Thread 0x1903 of process 58373]
[New Thread 0x2203 of process 58373]
[New Thread 0x2303 of process 58373]
[New Thread 0x2507 of process 58373]
Thread 2 hit Breakpoint 2, main.main () at /opt/hello.go:16
16 func main() {
(gdb) n
22 c := make(chan int, 3)
(gdb) n
23 go printNumber(1, 3, c)
(gdb) n
24 go printNumber(4, 6, c)
(gdb)
在mac上使用gdb还是比较麻烦的
参考: