推荐使用Golang原生调试器delve,gdb不能切换goroutine。
推荐文章Golang程序调试工具介绍(gdb vs dlv)
,分析得很棒! gdb只能切换thread,而不能切换goroutine,将dlv切换goroutine、查看变量的过程摘抄如下:
(dlv) goroutine 6
Switched from 1 to 6 (thread 9022)
在此基础上,执行bt,则可以看到当前goroutine的调用栈情况
(dlv) bt
0 0x0000000000454730 in runtime.systemstack_switch
at /home/lday/Tools/Dev_Tools/Go_Tools/go_1_6_2/src/runtime/asm_amd64.s:245
1 0x000000000040f700 in runtime.mallocgc
at /home/lday/Tools/Dev_Tools/Go_Tools/go_1_6_2/src/runtime/malloc.go:643
2 0x000000000040fc43 in runtime.rawmem
at /home/lday/Tools/Dev_Tools/Go_Tools/go_1_6_2/src/runtime/malloc.go:809
3 0x000000000043c2a5 in runtime.growslice
at /home/lday/Tools/Dev_Tools/Go_Tools/go_1_6_2/src/runtime/slice.go:95
4 0x000000000043c015 in runtime.growslice_n
at /home/lday/Tools/Dev_Tools/Go_Tools/go_1_6_2/src/runtime/slice.go:44
5 0x0000000000459545 in fmt.(*fmt).padString
at /home/lday/Tools/Dev_Tools/Go_Tools/go_1_6_2/src/fmt/format.go:130
6 0x000000000045a13f in fmt.(*fmt).fmt_s
at /home/lday/Tools/Dev_Tools/Go_Tools/go_1_6_2/src/fmt/format.go:322
7 0x000000000045e905 in fmt.(*pp).fmtString
at /home/lday/Tools/Dev_Tools/Go_Tools/go_1_6_2/src/fmt/print.go:518
8 0x000000000046200f in fmt.(*pp).printArg
at /home/lday/Tools/Dev_Tools/Go_Tools/go_1_6_2/src/fmt/print.go:797
9 0x0000000000468a8d in fmt.(*pp).doPrintf
at /home/lday/Tools/Dev_Tools/Go_Tools/go_1_6_2/src/fmt/print.go:1238
10 0x000000000045c654 in fmt.Fprintf
at /home/lday/Tools/Dev_Tools/Go_Tools/go_1_6_2/src/fmt/print.go:188
此时输出了10层调用栈,但似乎最原始的我自身程序dbgTest.go的调用栈没有输出, 可以通过bt加depth参数,设定bt的输出深度,进而找到我们自己的调用栈,例如bt 13
(dlv) bt 13
...
10 0x000000000045c654 in fmt.Fprintf
at /home/lday/Tools/Dev_Tools/Go_Tools/go_1_6_2/src/fmt/print.go:188
11 0x000000000045c74b in fmt.Printf
at /home/lday/Tools/Dev_Tools/Go_Tools/go_1_6_2/src/fmt/print.go:197
12 0x000000000045846f in GoWorks/GoDbg/mylib.RunFunc2
at ./mylib/dbgTest.go:50
13 0x0000000000456df0 in runtime.goexit
at /home/lday/Tools/Dev_Tools/Go_Tools/go_1_6_2/src/runtime/asm_amd64.s:1998
我们看到,我们自己dbgTest.go的调用栈在第12层。当前goroutine已经不再我们自己的调用栈上,而是进入到系统函数的调用中,在这种情况下,使用gdb进行调试时,我们发现,此时我们没有很好的方法能够输出我们需要的调用栈变量信息。dlv可以!此时只需简单的通过frame x cmd就可以输出我们想要的调用栈信息了
(dlv) frame 12 ls
45: time.Sleep(10 * time.Second)
46: waiter.Done()
47: }
48:
49: func RunFunc2(variable string, waiter *sync.WaitGroup) {
=> 50: fmt.Printf("var2:%v\n", variable)
51: time.Sleep(10 * time.Second)
52: waiter.Done()
53: }
54:
55: func RunFunc3(pVariable *[]int, waiter *sync.WaitGroup) {
(dlv) frame 12 print variable
"golang dbg test"
(dlv) frame 12 print waiter
*sync.WaitGroup {
state1: [12]uint8 [0,0,0,0,2,0,0,0,0,0,0,0],
sema: 0,}
dlv还可以attach到一个程序进行调试。
参考:
Getting started with Delve
Debugging Go Code with GDB 官方
Using the gdb debugger with Go
有疑问加站长微信联系(非本文作者)