云计算、AI、云原生、大数据等一站式技术学习平台

网站首页 > 教程文章 正文

golang 调试工具--Delve

jxf315 2025-01-05 18:14:57 教程文章 29 ℃

Go调试工具--Delve

Delve 旨在成为一个非常简单和强大的工具, 但如果您不习惯在编译语言中使用源代码级调试器, 可能会感到困惑。本文档将提供开始调试 Go 程序所需的所有信息。

github.com/go-delve/delve

github地址:

> # git clone https://github.com/go-delve/delve

> # cd delve

> # go install github.com/go-delve/delve/cmd/dlv

或者

> # go get -u github.com/go-delve/delve/cmd/dlv

加入环境变量

> # vim /etc/profile

export PATH=/root/go/bin:$PATH

1 使用:

> # dlv version

Delve Debugger

Version: 1.7.2

Build: $Id: 5b6f24e7fbcad3fe3bc574d3763b2f20afa1d6a1 $

> # dlv --help

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

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

这个工具的目标是为调试Go程序提供一个简单而强大的接口.

使用“--”将标志传递给正在调试的程序,例如:

`dlv exec ./hello -- server --config conf/config.toml`

......

支持的命令太多了, 在这里我们主要介绍它的调试命令——debug

2.dlv debug

首先使用dlv debug --help 查看其的帮助信息:

> # dlv debug --help

编译禁用优化的程序, 启动并附加到该程序。

默认情况下, 没有参数, Delve将编译当前目录中的"main"包, 并开始调试它。或者, 您可以指定一个包名, Delve将编译该包, 并开始一个新的调试会话。

Usage:

dlv debug [package] [flags]

Flags:

--output string 二进制文件的输出路径. (default "debug")

Global Flags:和上面的一样,这里省略

举例说明:

首先要进行调试的代码为:

test.go

package main

import (

"fmt"

"time"

)

func counting(c chan<- int){

for i := 0; i < 10; i++{

time.Sleep(2 * time.Second)

c <- i

}

close(c)

}

func main() {

msg := "Starting main"

fmt.Println(msg)

bus := make(chan int)

msg = "starting a gofunc"

go counting(bus)

for count := range bus{

fmt.Println("count : ", count)

}

}

> # dlv debug test.go

Type 'help' for list of commands.

(dlv) help

The following commands are available:

Running the program:

call ------------------------ Resumes process, injecting a function call (EXPERIMENTAL!!!)

continue (alias: c) --------- Run until breakpoint or program termination.

next (alias: n) ------------- Step over to next source line.

rebuild --------------------- Rebuild the target executable and restarts it. It does not work if the executable was not built by delve.

restart (alias: r) ---------- Restart process.

step (alias: s) ------------- Single step through program.

step-instruction (alias: si) Single step a single cpu instruction.

stepout (alias: so) --------- Step out of the current function.

Manipulating breakpoints:

break (alias: b) ------- Sets a breakpoint.

breakpoints (alias: bp) Print out info for active breakpoints.

clear ------------------ Deletes breakpoint.

clearall --------------- Deletes multiple breakpoints.

condition (alias: cond) Set breakpoint condition.

on --------------------- Executes a command when a breakpoint is hit.

toggle ----------------- Toggles on or off a breakpoint.

trace (alias: t) ------- Set tracepoint.

watch ------------------ Set watchpoint.

Viewing program variables and memory:

args ----------------- Print function arguments.

display -------------- Print value of an expression every time the program stops.

examinemem (alias: x) Examine raw memory at the given address.

locals --------------- Print local variables.

print (alias: p) ----- Evaluate an expression.

regs ----------------- Print contents of CPU registers.

set ------------------ Changes the value of a variable.

vars ----------------- Print package variables.

whatis --------------- Prints type of an expression.

Listing and switching between threads and goroutines:

goroutine (alias: gr) -- Shows or changes current goroutine

goroutines (alias: grs) List program goroutines.

thread (alias: tr) ----- Switch to the specified thread.

threads ---------------- Print out info for every traced thread.

Viewing the call stack and selecting frames:

deferred --------- Executes command in the context of a deferred call.

down ------------- Move the current frame down.

frame ------------ Set the current frame, or execute command on a different frame.

stack (alias: bt) Print stack trace.

up --------------- Move the current frame up.

Other commands:

config --------------------- Changes configuration parameters.

disassemble (alias: disass) Disassembler.

dump ----------------------- Creates a core dump from the current process state

edit (alias: ed) ----------- Open where you are in $DELVE_EDITOR or $EDITOR

exit (alias: quit | q) ----- Exit the debugger.

funcs ---------------------- Print list of functions.

help (alias: h) ------------ Prints the help message.

libraries ------------------ List loaded dynamic libraries

list (alias: ls | l) ------- Show source code.

source --------------------- Executes a file containing a list of delve commands

sources -------------------- Print list of source files.

types ---------------------- Print list of types

Type help followed by a command for full documentation.

(dlv) continue # 运行到断点或程序终止

Starting main

count : 0

count : 1

count : 2

count : 3

count : 4

count : 5

count : 6

count : 7

count : 8

count : 9

Process 266267 has exited with status 0

(dlv) restart # 重启进程

(dlv) help config # 查看帮助

(dlv) b main.main # 在main函数处添加断点

Breakpoint 1 set at 0x4b8a98 for main.main() ./test.go:16

(dlv) b main.counting # 在counting函数处添加断点

Breakpoint 2 set at 0x4b89f3 for main.counting() ./test.go:8

(dlv) c # 继续程序运行, continue的缩写

> main.main() ./test.go:16 (hits goroutine(1):1 total:1) (PC: 0x4b8a98)

11: c <- i

12: }

13: close(c)

14: }

15:

=> 16: func main() {

17: msg := "Starting main"

18: fmt.Println(msg)

19: bus := make(chan int)

20: msg = "starting a gofunc"

21: go counting(bus)

(dlv)

翻译:

> # dlv --help

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

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

这个工具的目标是为调试Go程序提供一个简单而强大的接口.

使用“--”将标志传递给正在调试的程序,例如:

`dlv exec ./hello -- server --config conf/config.toml`

Usage:

dlv [command]

Available Commands:

attach 连接到正在运行的流程并开始调试.

connect 连接到无头调试服务器.

core 检查核心转储.

debug 编译并开始调试当前目录下的主包或指定的包.

exec 执行预编译的二进制文件,并开始调试会话.

help 帮助信息

run 弃用的命令。使用“debug”替代它.

test 编译测试二进制文件并开始调试程序.

trace 编译并开始跟踪程序.

version 打印版本.

Flags:

--accept-multiclient 允许无头服务器接受多个客户机连接。注意,服务器API不是可重入的,客户端必须协调.

--api-version int 无头时选择API版本. (default 1)

--backend string 后端选择:

default 在macOS上使用lldb,其他地方都是本地的.

native 本地后端.

lldb 使用lldb-server或debugserver.

rr 使用mozilla rr (https://github.com/mozilla/rr).

(default "default") 默认使用的是default

--build-flags string 生成标志,以传递给编译器.

--headless 仅在headless模式下运行调试服务器.

--init string 初始化文件,由终端客户端执行.

-l, --listen string 调试服务器监听地址. (default "localhost:0")

--log 启用调试服务器日志记录.

--log-output string 应该产生调试输出的组件的逗号分隔列表,可能的值为:

debugger 记录调试器命令

gdbwire 日志连接到gdbserial后端

lldbout 将输出从debugserver/lldb复制到标准输出

debuglineerr 读取.debug_line时日志可恢复错误

rpc 记录所有RPC消息

fncall 日志函数调用协议

minidump 日志minidump加载

使用--log启用日志时,默认为“debugger”.

--wd string 用于运行程序的工作目录. (default ".")

使用"dlv [command] --help"获取有关命令的详细信息.

> # dlv debug test.go

Type 'help' for list of commands.

(dlv) help

The following commands are available:

args ------------------------ 打印函数参数.

break (alias: b) ------------ 设置断点.

breakpoints (alias: bp) ----- 输出活动断点的信息.

call ------------------------ 恢复进程, 注入一个函数调用(还在实验阶段!!)

clear ----------------------- 删除断点.

clearall -------------------- 删除多个断点.

condition (alias: cond) ----- 设置断点条件.

config ---------------------- 修改配置参数.

continue (alias: c) --------- 运行到断点或程序终止.

deferred -------------------- 在延迟调用的上下文中执行命令.

disassemble (alias: disass) - 反汇编程序.

down ------------------------ 将当前帧向下移动.

edit (alias: ed) ------------ 在$DELVE_EDITOR或$EDITOR中打开你所在的位置

exit (alias: quit | q) ------ 退出调试器.

frame ----------------------- 设置当前帧, 或在不同的帧上执行命令.

funcs ----------------------- 打印函数列表.

goroutine ------------------- 显示或更改当前goroutine

goroutines ------------------ 列举程序goroutines.

help (alias: h) ------------- 打印帮助信息.

list (alias: ls | l) -------- 显示源代码.

locals ---------------------- 打印局部变量.

next (alias: n) ------------- 转到下一个源行.

on -------------------------- 在命中断点时执行命令.

print (alias: p) ------------ 计算一个表达式.

regs ------------------------ 打印CPU寄存器的内容.

restart (alias: r) ---------- 重启进程.

set ------------------------- 更改变量的值.

source ---------------------- 执行包含delve命令列表的文件

sources --------------------- 打印源文件列表.

stack (alias: bt) ----------- 打印堆栈跟踪信息.

step (alias: s) ------------- 单步执行程序.

step-instruction (alias: si) 单步执行一条cpu指令.

stepout --------------------- 跳出当前函数.

thread (alias: tr) ---------- 切换到指定的线程.

threads --------------------- 打印每个跟踪线程的信息.

trace (alias: t) ------------ 设置跟踪点.

types ----------------------- 打印类型列表

up -------------------------- 向上移动当前帧.

vars ------------------------ 打印包变量.

whatis ---------------------- 打印表达式的类型.

在命令前键入help来获得命令的完整文档, 如help goroutine

Tags:

最近发表
标签列表