参考:
注意:go需要更新到1.10以上了,否则报错。
方法1,采用go安装:
$ go get -u github.com/go-delve/delve/cmd/dlv
方法2,git普通安装,需设置好$GOPATH环境变量:
$ git clone https://github.com/go-delve/delve.git $GOPATH/src/github.com/go-delve/delve
$ cd $GOPATH/src/github.com/go-delve/delve
$ make install
只要和main packages在同一目录,不需要参数就能运行。
例如以下目录结构:
.
├── github.com/me/foo
├── cmd
│ └── foo
│ └── main.go
├── pkg
│ └── baz
│ ├── bar.go
│ └── bar_test.go
若就在github.com/me/foo/cmd/foo目录,则直接运行dlv debug;否则运行dlv debug github.com/me/foo/cmd/foo;若提供其他参数,则加"—"隔开,dlv debug github.com/me/foo/cmd/foo – -arg1 value。
$ dlv debug github.com/me/foo/cmd/foo
Type 'help' for list of commands.
(dlv) break main.main
Breakpoint 1 set at 0x49ecf3 for main.main() ./test.go:5
(dlv) continue
> main.main() ./test.go:5 (hits goroutine(1):1 total:1) (PC: 0x49ecf3)
1: package main
2:
3: import "fmt"
4:
=> 5: func main() {
6: fmt.Println("delve test")
7: }
(dlv)
也可以测试自己的test suite,命令是dlv test。若没有给定参数则构建当前package。
$ dlv test github.com/me/foo/pkg/baz
Type 'help' for list of commands.
(dlv) funcs test.Test*
/home/me/go/src/github.com/me/foo/pkg/baz/test.TestHi
(dlv) break TestHi
Breakpoint 1 set at 0x536513 for /home/me/go/src/github.com/me/foo/pkg/baz/test.TestHi() ./test_test.go:5
(dlv) continue
> /home/me/go/src/github.com/me/foo/pkg/baz/test.TestHi() ./bar_test.go:5 (hits goroutine(5):1 total:1) (PC: 0x536513)
1: package baz
2:
3: import "testing"
4:
=> 5: func TestHi(t *testing.T) {
6: t.Fatal("implement me!")
7: }
(dlv)
通过funcs命令加正则参数来筛查函数,调试test二进制文件。其他命令见delve help。
调试选项,例如dlv exec ./hello – server --config conf/config.toml。
--accept-multiclient Allows a headless server to accept multiple client connections. Note that the server API is not reentrant and clients will have to coordinate.
--api-version int Selects API version when headless. (default 1)
--backend string Backend selection:
default Uses lldb on macOS, native everywhere else.
native Native backend.
lldb Uses lldb-server or debugserver.
rr Uses mozilla rr (https://github.com/mozilla/rr).
(default "default")
--build-flags string Build flags, to be passed to the compiler.
--check-go-version Checks that the version of Go in use is compatible with Delve. (default true)
--headless Run debug server only, in headless mode.
--init string Init file, executed by the terminal client.
-l, --listen string Debugging server listen address. (default "localhost:0")
--log Enable debugging server logging.
--log-dest string Writes logs to the specified file or file descriptor. If the argument is a number it will be interpreted as a file descriptor, otherwise as a file path. This option will also redirect the "API listening" message in headless mode.
--log-output string Comma separated list of components that should produce debug output, possible values:
debugger Log debugger commands
gdbwire Log connection to gdbserial backend
lldbout Copy output from debugserver/lldb to standard output
debuglineerr Log recoverable errors reading .debug_line
rpc Log all RPC messages
fncall Log function call protocol
minidump Log minidump loading
Defaults to "debugger" when logging is enabled with --log.
--wd string Working directory for running the program. (default ".")
调试模式:
-e, --exec string 需执行和追踪的二进制文件
--output string 二进制文件的输出路径(default "debug")
-p, --pid int 待附加的进程号
-s, --stack int 显示给定深度的stack trace
-t, --test 追踪一个test binary.
常用命令:
#直接调试
$ dlv debug ./main.go
$ b main.main
$ c
$ b main.hi #在func li 里打一个断点
$ b /home/goworkspace/src/github.com/mytest/main.go:20 #使用 "文件:行号"来打断点
$ n #回车,单步执行
$ print #(别名p)输出变量信息 p hostName
$ args #打印出所有的方法参数信息
$ locals #打印所有的本地变量
#附加调试
$ go build main.go
$ ./main
$ ps aux|grep main #查pid
$ dlv attach 29260