dlv安装
dlv下载之git clone方式
git clone github.com/go-delve/de… $GOPATH/src/github.com/go-delve/delve
GOPATH替换为真实目录,如下面实例,Mac环境变量在.bash_profile文件下配置,故$GOPATH=/Users/hanyuxia/go
export PATH="$PATH:$HOME/.rvm/bin:/Users/mac/go/bin:/Users/mac/go/go1.18/bin"
export GOROOT="/Users/mac/go/go" ///Users/mac/go/go 是 /Users/mac/go/go1.18 的软链
export GOPATH="/Users/mac/go"
dlv 安装
cd $GOPATH/src/github.com/go-delve/delve
make install
命令执行完后,会返回如下提示,表明自动为dlv生成了证书并对dlv进行签名.需要注意,此处使用codesign对dlv进行签名,没有签名的程序会受到一些限制,例如无法作为调试程序
go install -tags=macnative "-ldflags=-s -X main.Build=2b97231e305c239bcb746234bf67485dbd3b965f" github.com/go-delve/delve/cmd/dlv
codesign -s dlv-cert /Users/mac/go/bin/dlv
提示
- 在Mac上配置Go语言开发环境的时候,经常碰到的问题就是dlv调用总是不成功,无法启动应用,无法调试等等,大部分是问题都与Mac的安全机制有关.
- command not found: dlv 是由于在安装完毕后,在$GOPATH/bin目录下生成一个名为dlv的可执行文件,此种情况将GOROOT配置到PATH中即可
安装成功标识,执行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]
Available Commands:
attach Attach to running process and begin debugging.
connect Connect to a headless debug server with a terminal client.
core Examine a core dump.
dap Starts a headless TCP server communicating via Debug Adaptor Protocol (DAP).
debug Compile and begin debugging main package in current directory, or the package specified.
exec Execute a precompiled binary, and begin a debug session.
help Help about any command
run Deprecated command. Use 'debug' instead.
test Compile test binary and begin debugging program.
trace Compile and begin tracing program.
version Prints version.
Flags:
--accept-multiclient Allows a headless server to accept multiple client connections via JSON-RPC or DAP.
--allow-non-terminal-interactive Allows interactive sessions of Delve that don't have a terminal as stdin, stdout and stderr
--api-version int Selects JSON-RPC API version when headless. New clients should use v2. Can be reset via RPCServer.SetApiVersion. See Documentation/api/json-rpc/README.md. (default 1)
--backend string Backend selection (see 'dlv help backend'). (default "default")
--build-flags string Build flags, to be passed to the compiler. For example: --build-flags="-tags=integration -mod=vendor -cover -v"
--check-go-version Exits if the version of Go in use is not compatible (too old or too new) with the version of Delve. (default true)
--disable-aslr Disables address space randomization
--headless Run debug server only, in headless mode. Server will accept both JSON-RPC or DAP client connections.
-h, --help help for dlv
--init string Init file, executed by the terminal client.
-l, --listen string Debugging server listen address. (default "127.0.0.1:0")
--log Enable debugging server logging.
--log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log').
--log-output string Comma separated list of components that should produce debug output (see 'dlv help log')
--only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true)
-r, --redirect stringArray Specifies redirect rules for target process (see 'dlv help redirect')
--wd string Working directory for running the program.
Additional help topics:
dlv backend Help about the --backend flag.
dlv log Help about logging flags.
dlv redirect Help about file redirection.
Use "dlv [command] --help" for more information about a command.
调试示例
提示如果遇到以下错误,将Go的版本提升到1.16以上或者执行时加参数
--check-go-version=false
Go version 1.15.3 is too old for this version of Delve (minimum supported version 1.16)
调试示例代码
func main() {
fmt.Printf("%s", "test")
}
本文以调试bin文件为示例
编译要求,必须附带-gcflags=all="-N -l",这样dlv可以打印导出变量信息; #go build -gcflags=all="-N -l" main.go
调试步骤
- 执行调试
dlv exec main
- 添加断点
- 方法1: b + 方法名设置断点
break(b) runtime.rt0_go- 方法2: b + 调试脚本名:代码行号
(dlv) b /Users/mac/go/go1.18/src/runtime/asm_amd64.s:151 Breakpoint 2 set at 0x105ec23 for runtime.rt0_go() /Users/mac/go/go1.18/src/runtime/asm_amd64.s:151 - 逐行执行代码,不进入函数: n(next)
- 退出调试 exit