dlv调试golang(linux 界面操作)

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.


目录结构

调试所用代码

文件名为:hello.go
代码内容如下

package main 
import ( 
	"fmt" 
	"time" 
) 
func Test() { 
	fmt.Println("hello") 
	time.Sleep(1000 * 1000 * 100) 
} 
func Test2() { 
	fmt.Println("world") 
	time.Sleep(1000 * 1000 * 100) 
} 
func main() { 
	for i := 0; i < 2; i++ { 
		go Test() 
		go Test2() 
	} 
	time.Sleep(1000 * 1000 * 2000) 
	fmt.Println("end") 
}

dlv trace追踪调用轨迹

dlv trace hello.go


dlv trace hello.go Test

调试模式

使用命令dlv debug hello.go进入调试模式


调试模式基本命令

进入调试模式后可以使用如下命令进行debug

1、打断点:

使用b 函数名进行打断点,或者b GoFile:line打断点


2、带条件的断点

cond(condition) exp 该命令针对对某个断点,只有表达式成立时才会被中断,如:
condition 3 i == 1

3、查看所有断点

使用bp命令可以看到刚才打的所有断点


4、重启当前进程

使用r(restart)重启当前进程
如果刚执行
dlv debug hello.go,进程已经起来,不用执行
如果进程已经结算或需要重新开启则需要运行该命令

5、继续执行到断点处

c(continue)命令执行到断点处

6、逐行执行代码,不进入函数内

n(next)

7、逐行执行代码,遇到函数会跳进内部

s(step)

8、on

当运行到某断点时执行相应命令
断点可以是名词(在设置断点时可命名断点)或者编号
on 3 p i 表示运行到断点3时打印变量i

9:stepout使用s命令进入某个函数后,执行它可跳出函数
10:
si(step-instruction)单步单核执行代码
11:
args查看被调用函数所传入的参数值
12:
locals查看所有局部变量
13:
locals var_name查看某个具体变量,var_name可以使正则
14:
clear清除单个断点
15:
clearall清除所有断点
16:
list打印当前断点位置的源代码
17:
bt打印当前栈信息
18:
goroutines显示所有协程
19:
goroutine协程切换
20:
frame切换栈
21:
regs打印寄存器内容
22:
source执行一个含有dlv命令的文件
23:
trace类似于打断点,但不会中断,同时会输出一行提示信息

一般用vscode的调试工具来做调试,基本就能实行上面的命令行,也非常的方便,只是在linux的时候 要调试只能用命令来处理,

界面   操作   dlv
发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章