本文是Go语言系列文章Golang Notes的其中一篇,完整的文章列表请去总章查看。
Go语言的Debug工具对于一直写高级语言的程序员来说有点陌生,而对于写C常年和GDB打交道的程序员来说,则非常熟悉。
MAC下的官方安装教程:Installation on OSX。
环境完备的情况下只要直接使用go的下载命令即可:
$ go get -u github.com/go-delve/delve/cmd/dlv
如果没有设置过$GOPATH
的话,软件包会安装在~/go
下。然后需要把~/go/bin
加入到$PATH
里,就可以全局使用这个debug工具了。
MAC下使用的时候可能遇到如下问题:
$ dlv debug goroutine.go
could not launch process: debugserver or lldb-server not found: install XCode's command line tools or lldb-server
需要重新安装xcode的命令行工具:
$ xcode-select --install
完成后即可正常使用:
$ dlv debug goroutine.go
Type 'help' for list of commands.
(dlv)
dlv可以以两种模式进行运行:
命令行下使用dlv首先需要了解dlv这个命令本身的使用方法:dlv help:
在线的文档可以在github上看到:delve/Documentation/usage/dlv.md。类似debug、exec等,在这个主页面里都可以看得到,不过打开也没什么细节,就是一些选项参数的说明,和$ dlv connect ...
打印出来的没差别。
经过上面的选择后,就可以进入到dlv的交互界面了,同样的,可以通过(dlv) help来查看交互命令选项。
在调试过程中,有些时候你需要做一些细节观察行为,比如说打印出某些变量的值之类的。dlv提供了一些go语法的子集表达式,可以在交互界面直接使用:Expressions。
实际的debug范例可以看这篇:使用Delve进行Golang代码的调试,算是讲解得很贴近日常工作使用了。
$ dlv help
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.
core Examine a core dump.
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. 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.
--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-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 ".")
Use "dlv [command] --help" for more information about a command.
(dlv) help
The following commands are available:
args ------------------------ Print function arguments.
break (alias: b) ------------ Sets a breakpoint.
breakpoints (alias: bp) ----- Print out info for active breakpoints.
call ------------------------ Resumes process, injecting a function call (EXPERIMENTAL!!!)
clear ----------------------- Deletes breakpoint.
clearall -------------------- Deletes multiple breakpoints.
condition (alias: cond) ----- Set breakpoint condition.
config ---------------------- Changes configuration parameters.
continue (alias: c) --------- Run until breakpoint or program termination.
deferred -------------------- Executes command in the context of a deferred call.
disassemble (alias: disass) - Disassembler.
down ------------------------ Move the current frame down.
edit (alias: ed) ------------ Open where you are in $DELVE_EDITOR or $EDITOR
exit (alias: quit | q) ------ Exit the debugger.
frame ----------------------- Set the current frame, or execute command on a different frame.
funcs ----------------------- Print list of functions.
goroutine ------------------- Shows or changes current goroutine
goroutines ------------------ List program goroutines.
help (alias: h) ------------- Prints the help message.
list (alias: ls | l) -------- Show source code.
locals ---------------------- Print local variables.
next (alias: n) ------------- Step over to next source line.
on -------------------------- Executes a command when a breakpoint is hit.
print (alias: p) ------------ Evaluate an expression.
regs ------------------------ Print contents of CPU registers.
restart (alias: r) ---------- Restart process.
set ------------------------- Changes the value of a variable.
source ---------------------- Executes a file containing a list of delve commands
sources --------------------- Print list of source files.
stack (alias: bt) ----------- Print stack trace.
step (alias: s) ------------- Single step through program.
step-instruction (alias: si) Single step a single cpu instruction.
stepout --------------------- Step out of the current function.
thread (alias: tr) ---------- Switch to the specified thread.
threads --------------------- Print out info for every traced thread.
trace (alias: t) ------------ Set tracepoint.
types ----------------------- Print list of types
up -------------------------- Move the current frame up.
vars ------------------------ Print package variables.
whatis ---------------------- Prints type of an expression.
Type help followed by a command for full documentation.
EOF