【golang源码分析】编译和调试源码

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还是比较麻烦的

参考:

http://www.langdebuqing.com/others%20notebook/mac%20%E5%AE%89%E8%A3%85%20gdb%20%E8%B8%A9%E5%9D%91.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值