devel安装和使用

1,522 阅读2分钟

介绍

Delve 是 Go 程序的源代码级调试器。

Delve 使您能够通过控制流程的执行与您的程序进行交互, 评估变量,并提供线程/goroutine 状态、CPU 寄存器状态等信息。

这个工具的目标是为调试 Go 程序提供一个简单而强大的界面。

项目地址

github.com/go-delve/de…

安装

支持mac,linux,win平台安装.

mac install

$ git clone https://github.com/go-delve/delve
$ cd delve
$ go install github.com/go-delve/delve/cmd/dlv

确认安装成功

dlv version

操作

获取帮助信息

dlv

结果

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.
  dap         [EXPERIMENTAL] Starts a 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.
      --allow-non-terminal-interactive   Allows interactive sessions of Delve that don't have a terminal as stdin, stdout and stderr
      --api-version int                  Selects 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                 Checks that the version of Go in use is compatible with Delve. (default true)
      --disable-aslr                     Disables address space randomization
      --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 "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目录

dlv debug

输入help获得命令帮助

常用调试命令

break

设置断点

break main.main

breakpoints

打印活动断点的信息。

continue

运行直到断点或程序终止。

disassemble

反汇编, 显示当前函数plan9汇编代码

step

步进

print

执行表达式,可以打印当前函数变量

args

打印函数参数

locals

打印本地变量

参考文章

详解Go中内存分配源码实现

详解Go语言调度循环源码实现