1 GDB与Delve
Gdb无法很好的支持golang的协程机制,delve为golang而生。Why?
GDB does not understand Goprograms well. The stack management, threading, and runtime contain aspectsthat differ enough from the execution model GDB expects that they can confusethe debugger, even when the program is compiled with gccgo.
In short, the instructions belowshould be taken only as a guide to how to use GDB when it works, not as aguarantee of success.
In time, a more Go-centric debugging architecture may be required.
总结一下上面说的话。
- Go 的debug工具有GDB这个玩意,但是目前貌似工作的不咋滴
- 目前官方只是给你介绍介绍这个玩意怎么用,但是不保证能成功
- 实话说,我们需要一个更懂 Go 的调试器
最后一句话透露了本质,目前还没有一个非常好的调试工具。难道我们只能在不断打日志然后 build 然后再打日志中调试程序吗?当然不是,下面我来介绍一个专门为 Go而生的 debug 工具 Delve。
Delve目的就是为了解决开发者在使用 GDB 调试中遇到的各种各样的问题。
2 delve初级使用
常用命令
2.1 直接debug
vim mstest.go
package main import ( "fmt" "sync" "time" ) func dostuff(wg *sync.WaitGroup, i int) { fmt.Printf("goroutine id %d\n", i) time.Sleep(300 * time.Second) fmt.Printf("goroutine id %d\n", i) wg.Done() } func main() { var wg sync.WaitGroup workers := 10 wg.Add(workers) for i := 0; i< workers; i++ { go dostuff(&wg, i) } wg.Wait() }
使用命令dlv debug mstest.go , 对上面的代码进行调试。
2.2 attach 到已经在运行的程序上
[root@wangyoujin bin]# dlv attach 27419
Type 'help' for list of commands.
(dlv) b main.main
Breakpoint 1 set at 0x856778 formain.main() /home/wangyoujin/18code/nb-iot-platform-able/src/ac-lwm2m/main.go:22
(dlv) n
> runtime.futex()/usr/lib/golang/src/runtime/sys_linux_amd64.s:427 (PC: 0x467157)
(dlv) break/home/wangyoujin/18code/nb-iot-platform-able/src/ac-lwm2m/main.go:22
Command failed: Breakpoint exists at/home/wangyoujin/18code/nb-iot-platform-able/src/ac-lwm2m/main.go:22 at 856778
(dlv) clear
Command failed: notenough arguments
(dlv) clearall
Breakpoint 1 cleared at0x856778 for main.main()/home/wangyoujin/18code/nb-iot-platform-able/src/ac-lwm2m/main.go:22
(dlv) break/home/wangyoujin/18code/nb-iot-platform-able/src/ac-common-go/dtls/listener.go:96
s, err := l.openSSL(addr)
if err != nil {
log.Printf("open ssl: %v", err)
continue
}
Breakpoint 2 set at 0x77e393 forac-common-go/dtls.(*listener).reading()/home/wangyoujin/18code/nb-iot-platform-able/src/ac-common-go/dtls/listener.go:96
(dlv) c
>ac-common-go/dtls.(*listener).reading()/home/wangyoujin/18code/nb-iot-platform-able/src/ac-common-go/dtls/listener.go:96(hits goroutine(85):1 total:1) (PC: 0x77e393)
(dlv) print s //查看此处 变量s的内容
*ac-common-go/dtls.ssl {
w:io.Writer(*ac-common-go/dtls.peerWriter) *{
conn:net.PacketConn(*net.UDPConn) ...,
addr:net.Addr(*net.UDPAddr) ...,},
addr:net.Addr(*net.UDPAddr) *{
IP:net.IP len: 16, cap: 16, [0,0,0,0,0,0,0,0,0,0,255,255,127,0,0,1],
Port: 56752,
Zone:"",},
closed:0,
istream:chan []uint8 {
qcount:0,
dataqsiz:8,
buf:unsafe.Pointer(0xc4201d5b00),
elemsize:24,
closed:0,
elemtype:*runtime._type {
size:24,
ptrdata:8,
hash:942571231,
tflag:2,
align:8,
fieldalign:8,
kind:23,
alg:*(*runtime.typeAlg)(0xde7400),
gcdata:*1,
str:23644,
ptrToThis:214400,},
sendx:0,
recvx:0,
recvq:waitq<[]uint8> {
first:*sudog<[]uint8> nil,
last:*sudog<[]uint8> nil,},
sendq:waitq<[]uint8> {
first:*sudog<[]uint8> nil,
last:*sudog<[]uint8> nil,},
lock:runtime.mutex {key: 0},},
ostream:chan ac-common-go/dtls.result {
qcount:0,
dataqsiz:256,
buf:unsafe.Pointer(0xc4202de000),
elemsize:56,
closed:0,
elemtype:*runtime._type {
size:56,
ptrdata:40,
hash:1442787700,
tflag:7,
align:8,
fieldalign:8,
kind:25,
alg:*(*runtime.typeAlg)(0xde7400),
gcdata:*31,
str:50567,
ptrToThis:220096,},
sendx:3,
recvx:3,
recvq:waitq<ac-common-go/dtls.result> {
first:*(*sudog<ac-common-go/dtls.result>)(0xc4201db1d0),
last:*(*sudog<ac-common-go/dtls.result>)(0xc4201db1d0),},
sendq:waitq<ac-common-go/dtls.result> {
first:*sudog<ac-common-go/dtls.result> nil,
last:*sudog<ac-common-go/dtls.result> nil,},
lock:runtime.mutex {key: 0},},
session:ac-common-go/dtls.session {
mu:(*sync.Mutex)(0xc42043b0b8),
bio:ac-common-go/dtls.bio(*ac-common-go/dtls.ssl) ...,
session:*(*ac-common-go/dtls._Ctype_struct_dtls_session)(0x7fe250009760),},}
(dlv) c
> ac-common-go/dtls.(*listener).reading()/home/wangyoujin/18code/nb-iot-platform-able/src/ac-common-go/dtls/listener.go:96(hits goroutine(85):2 total:2) (PC: 0x77e393)
(dlv) clearall
Breakpoint 2 cleared at 0x77e393 forac-common-go/dtls.(*listener).reading() /home/wangyoujin/18code/nb-iot-platform-able/src/ac-common-go/dtls/listener.go:96
(dlv) continue