golang除錯工具delve
golang除錯工具delve
之前一直在煩心不知道怎麼列印所有goroutine的stack,最近終於發現一個該工具。
- 什麼是delve
delve是golang推薦的專門go語言除錯工具,用來替代gdb,因為:golang組織說delve能更好的理解go語言。
-
golang說delve更能理解go語言:ofollow,noindex">https://golang.org/doc/gdb
Note thatDelve 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 on
amd64
. For the most up-to-date list of supported platforms, please seethe 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 00x0000000000466e70 in syscall.Syscall at /usr/local/go/src/syscall/asm_linux_amd64.s:27 10x000000000046692f in syscall.read at /usr/local/go/src/syscall/zsyscall_linux_amd64.go:749 20x0000000000466449 in syscall.Read at /usr/local/go/src/syscall/syscall_unix.go:162 30x0000000000468798 in internal/poll.(*FD).Read at /usr/local/go/src/internal/poll/fd_unix.go:153 40x0000000000469b8e in os.(*File).read at /usr/local/go/src/os/file_unix.go:226 50x0000000000468f7a in os.(*File).Read at /usr/local/go/src/os/file.go:107 60x00000000004658a6 in io.ReadAtLeast at /usr/local/go/src/io/io.go:309 70x0000000000465a18 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) 00x0000000000466e70 in syscall.Syscall at /usr/local/go/src/syscall/asm_linux_amd64.s:27 10x000000000046692f in syscall.read at /usr/local/go/src/syscall/zsyscall_linux_amd64.go:749 20x0000000000466449 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) 00x000000000042800a in runtime.gopark at /usr/local/go/src/runtime/proc.go:292 10x00000000004280be in runtime.goparkunlock at /usr/local/go/src/runtime/proc.go:297 20x0000000000427e4c in runtime.forcegchelper at /usr/local/go/src/runtime/proc.go:248 30x0000000000451671 in runtime.goexit at /usr/local/go/src/runtime/asm_amd64.s:2361 ...
- 等等等。。。,不列了,用到的時候再去查文件就行