golang调试工具delve
Delve is a debugger for the Go programming language. The goal of the project is to provide a simple, full featured debugging tool for Go. Delve should be easy to invoke and easy to use. Chances are if you're using a debugger, things aren't going your way. With that in mind, Delve should stay out of your way as much as possible.
golang调试工具delve
什么是delve
golang推荐的专门go语言调试工具 用来替代gdb
golang组织说delve能更好的理解go语言

golang说delve更能理解go语言 //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 on amd64.
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地址
//github.com/derekparker/delve

delve的安装
$ go get -u github.com/derekparker/delve/cmd/dlv

$GOPATH/bin目录下会包含dlv可执行程序
$ 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的stack
(dlv) goroutine <goroutine_id> stack
0  0x0000000000466e70 in syscall.Syscall
at /usr/local/go/src/syscall/asm_linux_amd64.s:27
...

打印所有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
...
让命令直接输出到文件 那么可以使用shell工具
$ cat dlv.sh
#!/bin/bash
dlv attach <PID> <<EOF
goroutines -t
quit
n
EOF


进入调试模式的几种方法:
1、dlv attach pid
类似与gdb attach pid 可以对正在运行的进程直接进行调试(pid为进程号)。

2、dlv run|debug
run命令已被debug命令取代 运行dlv debug test.go会先编译go源文件
同时执行attach命令进入调试模式 该命令会在当前目录下生成一个名为debug的可执行二进制文件 退出调试模式会自动被删除。

3、dlv exec executable_file
直接从二进制文件启动调试模式。

4、dlv core executable_file core_file
以core文件启动调试 通常进行dlv的目的就是为了找出可执行文件core的原因 通过core文件可直接找出具体进程异常的信息。

dlv trace
该命令最直接的用途是可以追踪代码里函数的调用轨迹 可通过help查看其调用方式