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 安装使用为例

安装:

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

示例

  1. package main
  2. import "fmt"
  3. func main() {
  4. args := []int{1, 2, 3, 4}
  5. for _, e := range args {
  6. fmt.Println(e)
  7. }
  8. }

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

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

输入 help 查看delve debug使用说明

  1. (dlv) help
  2. The following commands are available:
  3. args ------------------------ Print function arguments.
  4. break (alias: b) ------------ Sets a breakpoint.
  5. breakpoints (alias: bp) ----- Print out info for active breakpoints.
  6. call ------------------------ Resumes process, injecting a function call (EXPERIMENTAL!!!)
  7. clear ----------------------- Deletes breakpoint.
  8. clearall -------------------- Deletes multiple breakpoints.
  9. condition (alias: cond) ----- Set breakpoint condition.
  10. config ---------------------- Changes configuration parameters.
  11. continue (alias: c) --------- Run until breakpoint or program termination.
  12. deferred -------------------- Executes command in the context of a deferred call.
  13. disassemble (alias: disass) - Disassembler.
  14. down ------------------------ Move the current frame down.
  15. edit (alias: ed) ------------ Open where you are in $DELVE_EDITOR or $EDITOR
  16. exit (alias: quit | q) ------ Exit the debugger.
  17. frame ----------------------- Set the current frame, or execute command on a different frame.
  18. funcs ----------------------- Print list of functions.
  19. goroutine ------------------- Shows or changes current goroutine
  20. goroutines ------------------ List program goroutines.
  21. help (alias: h) ------------- Prints the help message.
  22. list (alias: ls | l) -------- Show source code.
  23. locals ---------------------- Print local variables.
  24. next (alias: n) ------------- Step over to next source line.
  25. on -------------------------- Executes a command when a breakpoint is hit.
  26. print (alias: p) ------------ Evaluate an expression.
  27. regs ------------------------ Print contents of CPU registers.
  28. restart (alias: r) ---------- Restart process.
  29. set ------------------------- Changes the value of a variable.
  30. source ---------------------- Executes a file containing a list of delve commands
  31. sources --------------------- Print list of source files.
  32. stack (alias: bt) ----------- Print stack trace.
  33. step (alias: s) ------------- Single step through program.
  34. step-instruction (alias: si) Single step a single cpu instruction.
  35. stepout --------------------- Step out of the current function.
  36. thread (alias: tr) ---------- Switch to the specified thread.
  37. threads --------------------- Print out info for every traced thread.
  38. trace (alias: t) ------------ Set tracepoint.
  39. types ----------------------- Print list of types
  40. up -------------------------- Move the current frame up.
  41. vars ------------------------ Print package variables.
  42. whatis ---------------------- Prints type of an expression.

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

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

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

(dlv) breakpoints

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

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

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

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

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

(dlv) continue

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

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

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

通过args命令查看参数:

  1. (dlv) args
  2. (no args)

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

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

执行两个next 进入for

  1. (dlv) next
  2. > main.main() ./main.go:7 (PC: 0x49ec88)
  3. 2:
  4. 3: import "fmt"
  5. 4:
  6. 5: func main() {
  7. 6: args := []int{1, 2, 3, 4}
  8. => 7: nums := make([]int, 5)
  9. 8: for _, e := range args {
  10. 9: nums[e] = e
  11. 10: }
  12. 11: fmt.Println(nums)
  13. 12: }
  14. (dlv) next
  15. > main.main() ./main.go:8 (PC: 0x49ecca)
  16. 3: import "fmt"
  17. 4:
  18. 5: func main() {
  19. 6: args := []int{1, 2, 3, 4}
  20. 7: nums := make([]int, 5)
  21. => 8: for _, e := range args {
  22. 9: nums[e] = e
  23. 10: }
  24. 11: fmt.Println(nums)
  25. 12: }
  26. (dlv) locals
  27. nums = []int len: 5, cap: 5, [...]
  28. args = []int len: 4, cap: 4, [...]
  29. e = 139827712967264

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

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

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

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

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

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

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

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

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

q退出delve

links