Go delve 介绍

发布时间: 更新时间: 总字数:1216 阅读时间:3m 作者: IP上海 分享 网址

delve是Go编程语言的调试器。

介绍

  • Delve 的目标是为 Go 提供一个简单、功能齐全的调试工具
  • Delve 易于调用和使用

安装

# Install the latest release:
$ go install github.com/go-delve/delve/cmd/dlv@latest

# Install at a specific version or pseudo-version:
$ go install github.com/go-delve/delve/cmd/dlv@v1.7.3
  • help
dlv --help ...
$ 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 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
  test        Compile test binary and begin debugging program.
  trace       Compile and begin tracing program.
  version     Prints version.

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.
  • debug help
dlv debug . ...
$ dlv debug .
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.
    packages ------------------- Print list of packages.
    source --------------------- Executes a file containing a list of delve commands
    sources -------------------- Print list of source files.
    target --------------------- Manages child process debugging.
    transcript ----------------- Appends command output to a file.
    types ---------------------- Print list of types

Type help followed by a command for full documentation.
(dlv)

使用

package main

import (
	"fmt"
)

func tribonacci(n int) int {
	if n < 2 {
		return n
	}
	if n == 2 {
		return 1
	}

	cache := make([]int, n+1)
	cache[0], cache[1], cache[2] = 0, 1, 1
	for i := 3; i <= n; i++ {
		cache[i] = cache[i-1] + cache[i-2] + cache[i-3]
	}
	return cache[n]
}

func main() {
	fmt.Println(tribonacci(15))
}

debug

使用的命令

  • break(b) 设置断点
    • b xxx/databases/mysql.init.0
  • breakpoints(bp) 查看断点
  • clearall 清理断点
  • continue(c) 执行到下一个断点
  • next(n) 下一步
  • set 设置变量
  • funcs <func-name> 搜索函数
  • reset(r) 重置
  • step(s) 进入函数
  • list(l) 查看文件
    • l <file>.go:n
  • exit 退出
$ dlv debug .
Type 'help' for list of commands.
(dlv) b main.main
Breakpoint 1 set at 0x10ae96e for main.main() ./main.go:23
(dlv) l main.go:15
Showing /Users/xiexianbin/Downloads/abc/main.go:15 (PC: 0x10ae78f)
    10:		}
    11:		if n == 2 {
    12:			return 1
    13:		}
    14:
    15:		cache := make([]int, n+1)
    16:		cache[0], cache[1], cache[2] = 0, 1, 1
    17:		for i := 3; i <= n; i++ {
    18:			cache[i] = cache[i-1] + cache[i-2] + cache[i-3]
    19:		}
    20:		return cache[n]
(dlv) b main.go:18
Breakpoint 2 set at 0x10ae82f for main.tribonacci() ./main.go:18
(dlv) bp
Breakpoint runtime-fatal-throw (enabled) at 0x437960,0x437a60 for (multiple functions)() <multiple locations>:0 (0)
Breakpoint unrecovered-panic (enabled) at 0x437dc0 for runtime.fatalpanic() /usr/lib/go-1.20/src/runtime/panic.go:1141 (0)
	print runtime.curg._panic.arg
Breakpoint 1 (enabled) at 0x49cb0a for main.main() ./main.go:23 (0)
Breakpoint 2 (enabled) at 0x49c9b9 for main.tribonacci() ./main.go:18 (0)
(dlv) c
> main.main() ./main.go:23 (hits goroutine(1):1 total:1) (PC: 0x49cb0a)
    18:			cache[i] = cache[i-1] + cache[i-2] + cache[i-3]
    19:		}
    20:		return cache[n]
    21:	}
    22:
=>  23:	func main() {
    24:		fmt.Println(tribonacci(15))
    25:	}
(dlv) n
> main.main() ./main.go:24 (PC: 0x49cb18)
    19:		}
    20:		return cache[n]
    21:	}
    22:
    23:	func main() {
=>  24:		fmt.Println(tribonacci(15))
    25:	}
(dlv) s
> main.tribonacci() ./main.go:7 (PC: 0x49c8ca)
     2:
     3:	import (
     4:		"fmt"
     5:	)
     6:
=>   7:	func tribonacci(n int) int {
     8:		if n < 2 {
     9:			return n
    10:		}
    11:		if n == 2 {
    12:			return 1
(dlv) c
> main.tribonacci() ./main.go:18 (hits goroutine(1):1 total:1) (PC: 0x49c9b9)
    13:		}
    14:
    15:		cache := make([]int, n+1)
    16:		cache[0], cache[1], cache[2] = 0, 1, 1
    17:		for i := 3; i <= n; i++ {
=>  18:			cache[i] = cache[i-1] + cache[i-2] + cache[i-3]
    19:		}
    20:		return cache[n]
    21:	}
    22:
    23:	func main() {
(dlv) p i
3
(dlv) p n
15
...
(dlv) locals
cache = []int len: 16, cap: 16, [...]
i = 5
(dlv) set i = 2
(dlv) locals
cache = []int len: 16, cap: 16, [...]
i = 2
(dlv) clearall main.main
Breakpoint 1 cleared at 0x49cb0a for main.main() ./main.go:23
(dlv) funcs tribonacci
main.tribonacci
(dlv) exit
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数