Skip to content

Files

Latest commit

1c74697 · May 26, 2022

History

History
295 lines (230 loc) · 8.64 KB

1.7.md

File metadata and controls

295 lines (230 loc) · 8.64 KB

1.7 Go 调试工具Delve

对于每一个程序员来说,调制程序是必备的技能。目前Go调试工具有GDB、LLDB和Delve几种调试器, 有道是工欲善其事,必先利其器。选择一个比较合适的调试工具尤为重要。Google官方为Golang的调试例子默认使用了GDB然而, 使用GDB调试go程序会遇到goroutine的各类问题,LLDB也会遇到缺乏对Go特性支持的情况。而只有Delve是专门为Go语言设计开发的调试工具。Delve是有Go语言开发的,支持OSXLinuxWindows

官方文档

请注意您必须安装Go 1.5或更高版本。 此外如果使用Go 1.5,您必须设置GO15VENDOREXPERIMENT = 1。

下面以entos7.5 安装使用为例

安装:

go get -u github.com/derekparker/delve/cmd/dlv

示例

package main

import "fmt"

func main() {
   args := []int{1, 2, 3, 4}
   for _, e := range args {
      fmt.Println(e)
   }
}

进入包所在目录,然后输入dlv debug命令进入调试:

$ dlv debug 
Type 'help' for list of commands.
(dlv) 

输入 help 查看delve debug使用说明

(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.

用break在go入口函数main.main设置断点

(dlv) break main.main
Breakpoint 1 set at 0x49ec1b for main.main() ./main.go:5

然后通过breakpoints查看已经设置的所有断点:

(dlv) breakpoints

(dlv) breakpoints
Breakpoint unrecovered-panic at 0x42a890 for runtime.startpanic() /usr/lib/golang/src/runtime/panic.go:577 (0)
	print runtime.curg._panic.arg
Breakpoint 1 at 0x49ec1b for main.main() ./main.go:5 (0)

这里除了我们自己设置的main.main函数断点外,Delve内部已经为panic异常函数设置了一个断点。

通过vars命令可以查看全部包级的变量。因为最终的目标程序可能含有大量的全局变量,我们可以通过一个正则参数选择想查看的全局变量:

(dlv) vars main
main.statictmp_0 = [4]int [...]
main.initdone· = 0
runtime.main_init_done = chan bool nil
runtime.mainStarted = false

然后就可以通过continue命令让程序运行到下一个断点处:

(dlv) continue

(dlv) continue
> main.main() ./main.go:5 (hits goroutine(1):1 total:1) (PC: 0x49ec1b)
     1:	package main
     2:	
     3:	import "fmt"
     4:	
=>   5:	func main() {
     6:		args := []int{1, 2, 3, 4}
     7:		nums := make([]int, 5)
     8:		for _, e := range args {
     9:			nums[e] = e
    10:		}

输入next命令单步执行进入main函数内部:

(dlv) next
> main.main() ./main.go:6 (PC: 0x49ec32)
     1:	package main
     2:	
     3:	import "fmt"
     4:	
     5:	func main() {
=>   6:		args := []int{1, 2, 3, 4}
     7:		nums := make([]int, 5)
     8:		for _, e := range args {
     9:			nums[e] = e
    10:		}
    11:		fmt.Println(nums)

通过args命令查看参数:

(dlv) args
(no args)

通过locals命令查看局部变量:

(dlv) locals
nums = []int len: 4253953, cap: 842350805080, [...]
args = []int len: 842350722936, cap: 4213035, [...]

执行两个next 进入for

(dlv) next
> main.main() ./main.go:7 (PC: 0x49ec88)
     2:	
     3:	import "fmt"
     4:	
     5:	func main() {
     6:		args := []int{1, 2, 3, 4}
=>   7:		nums := make([]int, 5)
     8:		for _, e := range args {
     9:			nums[e] = e
    10:		}
    11:		fmt.Println(nums)
    12:	}

(dlv) next
> main.main() ./main.go:8 (PC: 0x49ecca)
     3:	import "fmt"
     4:	
     5:	func main() {
     6:		args := []int{1, 2, 3, 4}
     7:		nums := make([]int, 5)
=>   8:		for _, e := range args {
     9:			nums[e] = e
    10:		}
    11:		fmt.Println(nums)
    12:	}
(dlv) locals
nums = []int len: 5, cap: 5, [...]
args = []int len: 4, cap: 4, [...]
e = 139827712967264

下面我们通过组合使用break和condition命令,在循环内部设置一个条件断点,当循环变量e等于3时断点生效:

(dlv) break main.go:9
Breakpoint 2 set at 0x49ed4a for main.main() ./main.go:9
(dlv) condition 2 e==3
(dlv) 

然后通过continue执行到刚设置的条件断点,并且输出局部变量:

(dlv) continue
> main.main() ./main.go:9 (hits goroutine(1):1 total:1) (PC: 0x49ed4a)
     4:	
     5:	func main() {
     6:		args := []int{1, 2, 3, 4}
     7:		nums := make([]int, 5)
     8:		for _, e := range args {
=>   9:			nums[e] = e
    10:		}
    11:		fmt.Println(nums)
    12:	}
(dlv) locals
nums = []int len: 5, cap: 5, [...]
args = []int len: 4, cap: 4, [...]
e = 3
(dlv) print nums
[]int len: 5, cap: 5, [0,1,2,0,0]
(dlv) 

我们发现当循环变量e等于3时,nums切片的前3个元素已经正确初始化。

通过stack查看当前执行函数的栈帧信息:

(dlv) stack
0  0x000000000049ed4a in main.main
   at ./main.go:9
1  0x000000000042c0b4 in runtime.main
   at /usr/lib/golang/src/runtime/proc.go:195
2  0x0000000000456281 in runtime.goexit
   at /usr/lib/golang/src/runtime/asm_amd64.s:2337
(dlv) 

或者通过goroutine和goroutines命令查看当前Goroutine相关的信息:

(dlv) goroutine
Thread 11599 at ./main.go:9
Goroutine 1:
	Runtime: ./main.go:9 main.main (0x49ed4a)
	User: ./main.go:9 main.main (0x49ed4a)
	Go: /usr/lib/golang/src/runtime/asm_amd64.s:181 runtime.rt0_go (0x453ada)
	Start: /usr/lib/golang/src/runtime/proc.go:109 runtime.main (0x42bef0)
(dlv) goroutines
* Goroutine 1 - User: ./main.go:9 main.main (0x49ed4a) (thread 11599)
  Goroutine 2 - User: /usr/lib/golang/src/runtime/proc.go:288 runtime.gopark (0x42c4fd)
  Goroutine 3 - User: /usr/lib/golang/src/runtime/proc.go:288 runtime.gopark (0x42c4fd)
  Goroutine 4 - User: /usr/lib/golang/src/runtime/proc.go:288 runtime.gopark (0x42c4fd)
[4 goroutines]
(dlv) 

q退出delve

links