golang调试工具delve
之前一直在烦心不知道怎么打印所有goroutine的stack,最近终于发现一个该工具。
- 什么是delve
delve是golang推荐的专门go语言调试工具,用来替代gdb,因为:golang组织说delve能更好的理解go语言。
-
golang说delve更能理解go语言:https://golang.org/doc/gdb
Note that Delve is a better alternative to GDB when debugging Go programs built with the standard toolchain. It understands the Go runtime, data structures, and expressions better than GDB. Delve currently supports Linux, OSX, and Windows onamd64
. For the most up-to-date list of supported platforms, please see the Delve documentation.GDB does not understand Go programs well. The stack management, threading, and runtime contain aspects that differ enough from the execution model GDB expects that they can confuse the debugger and cause incorrect results even when the program is compiled with gccgo. As a consequence, although GDB can be useful in some situations (e.g., debugging Cgo code, or debugging the runtime itself), it is not a reliable debugger for Go programs, particularly heavily concurrent ones. Moreover, it is not a priority for the Go project to address these issues, which are difficult.
delve的github地址:https://github.com/derekparker/delve
- delve的安装
delve的项目里面说的很详细,不列出了。
本人使用的是Linux,偷懒直接下载binary包就可以了。
$ go get -u github.com/derekparker/delve/cmd/dlv
在$GOPATH/bin目录下会包含dlv可执行程序。可以直接运行dlv启动delve。
$ dlv
Delve is a source level debugger for Go programs.
Delve enables you to interact with your program by controlling the execution of the process,
evaluating variables, and providing information of thread / goroutine state, CPU register state and more.
The goal of this tool is to provide a simple yet powerful interface for debugging Go programs.
Pass flags to the program you are debugging using `--`, for example:
`dlv exec ./hello -- server --config conf/config.toml`
Usage:
dlv [command]
...
参数很多,很复杂,自己根据需要按需采用就行。
- delve常用命令行
- attach到进程PID
$ dlv attach <pid>
Type 'help' for list of commands.
(dlv) help
- 打印所有的goroutines列表
(dlv) goroutines
[10 goroutines]
Goroutine 1 - User: /usr/local/go/src/syscall/asm_linux_amd64.s:27 syscall.Syscall (0x466e70) (thread 14023)
Goroutine 2 - User: /usr/local/go/src/runtime/proc.go:292 runtime.gopark (0x42800a)
Goroutine 3 - User: /usr/local/go/src/runtime/proc.go:292 runtime.gopark (0x42800a)
Goroutine 17 - User: /usr/local/go/src/runtime/proc.go:292 runtime.gopark (0x42800a)
Goroutine 18 - User: /usr/local/go/src/runtime/sigqueue.go:139 os/signal.signal_recv (0x43b5b6)
Goroutine 19 - User: /usr/local/go/src/runtime/proc.go:292 runtime.gopark (0x42800a)
Goroutine 20 - User: ./main.go:43 main.setupSignalHandler.func1 (0x49318c)
Goroutine 21 - User: /usr/local/go/src/runtime/time.go:102 time.Sleep (0x442ac6)
Goroutine 22 - User: /usr/local/go/src/runtime/time.go:102 time.Sleep (0x442ac6)
Goroutine 23 - User: /usr/local/go/src/runtime/lock_futex.go:227 runtime.notetsleepg (0x40c9a2)
- 打印单个goroutine的stack
(dlv) goroutine <goroutine_id> stack
0 0x0000000000466e70 in syscall.Syscall
at /usr/local/go/src/syscall/asm_linux_amd64.s:27
1 0x000000000046692f in syscall.read
at /usr/local/go/src/syscall/zsyscall_linux_amd64.go:749
2 0x0000000000466449 in syscall.Read
at /usr/local/go/src/syscall/syscall_unix.go:162
3 0x0000000000468798 in internal/poll.(*FD).Read
at /usr/local/go/src/internal/poll/fd_unix.go:153
4 0x0000000000469b8e in os.(*File).read
at /usr/local/go/src/os/file_unix.go:226
5 0x0000000000468f7a in os.(*File).Read
at /usr/local/go/src/os/file.go:107
6 0x00000000004658a6 in io.ReadAtLeast
at /usr/local/go/src/io/io.go:309
7 0x0000000000465a18 in io.ReadFull
at /usr/local/go/src/io/io.go:327
...
- 打印所有goroutine的stack
(dlv) goroutines -t
[10 goroutines]
Goroutine 1 - User: /usr/local/go/src/syscall/asm_linux_amd64.s:27 syscall.Syscall (0x466e70) (thread 14023)
0 0x0000000000466e70 in syscall.Syscall
at /usr/local/go/src/syscall/asm_linux_amd64.s:27
1 0x000000000046692f in syscall.read
at /usr/local/go/src/syscall/zsyscall_linux_amd64.go:749
2 0x0000000000466449 in syscall.Read
at /usr/local/go/src/syscall/syscall_unix.go:162
...
(truncated)
Goroutine 2 - User: /usr/local/go/src/runtime/proc.go:292 runtime.gopark (0x42800a)
0 0x000000000042800a in runtime.gopark
at /usr/local/go/src/runtime/proc.go:292
1 0x00000000004280be in runtime.goparkunlock
at /usr/local/go/src/runtime/proc.go:297
2 0x0000000000427e4c in runtime.forcegchelper
at /usr/local/go/src/runtime/proc.go:248
3 0x0000000000451671 in runtime.goexit
at /usr/local/go/src/runtime/asm_amd64.s:2361
...
- 等等等。。。,不列了,用到的时候再去查文档就行
有疑问加站长微信联系(非本文作者)