005_golang deubg工具利器dlv

众所周知,python有pdb,c/c++有gdb等工具,golang有没有一款类似的工具呢,答案是肯定的,下面说下dlv工具的用法

Office Website  https://github.com/go-delve/delve

一、

(1)安装<mac/linux>

安装完成后记着find下dlv命令的位置,并加到PATH系统变量中,这样使用起来才方便

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

(2)使用

dlv debug main.go

<1>打断点

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

最初我是直接设置的断点行数,后来发现这种姿势不对,应该是

(dlv) b main.main

(2)执行下一步

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

(3)进入函数

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

(3)移动当前帧到上一步

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

(4)下面以l和p示例列出所有的用法

list (alias: ls | l) -------- Show source code.
print (alias: p) ------------ Evaluate an expression.

(dlv) locals     #Print local variables.打印本地变量
cmd = ("*os/exec.Cmd")(0xc000094160)

(dlv) args       #Print function arguments.打印函数参数
pid = "10751"
~r1 = map[string]float64 nil
~r2 = error nil

(dlv) bp          #Print out info for active breakpoints.打印激活的断点
Breakpoint runtime-fatal-throw at 0x42c230 for runtime.fatalthrow() /usr/lib/golang/src/runtime/panic.go:654 (0)
Breakpoint unrecovered-panic at 0x42c2a0 for runtime.fatalpanic() /usr/lib/golang/src/runtime/panic.go:681 (0)
	print runtime.curg._panic.arg
Breakpoint 1 at 0x4cbed8 for main.main() ./main.go:234 (1)

(dlv) sources    #Print list of source files.  打印源码列表
/data/zookeeper/zookeeper-3.4.9/bin/main.go
/usr/lib/golang/src/bytes/buffer.go
/usr/lib/golang/src/context/context.go
/usr/lib/golang/src/errors/errors.go    


(dlv) bt             #Print stack trace.打印调用堆栈
0  0x00000000004cae09 in main.MergeData
   at ./main.go:143
1  0x00000000004cbf1c in main.main
   at ./main.go:243
2  0x000000000042db75 in runtime.main
   at /usr/lib/golang/src/runtime/proc.go:201
3  0x0000000000459951 in runtime.goexit
   at /usr/lib/golang/src/runtime/asm_amd64.s:1333    

(dlv) stepout      #Step out of the current function.退出当前函数
2019/03/05 15:45:51 fork/exec netstat -npl | awk -F'[ /]*' '/2181/{print $(NF-2)}': no such file or directory
Process has exited with status 1

(dlv) whatis cmd   #Prints type of an expression.打印表达式的类型
*os/exec.Cmd

  

 

posted @ 2019-03-05 15:31  arun_python  阅读(618)  评论(0编辑  收藏  举报