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系统变量中,这样使用起来才方便
1 | go get -u github.com/derekparker/delve/cmd/dlv |
(2)使用
dlv debug main.go
<1>打断点
1 | break (alias: b) ------------ Sets a breakpoint. |
最初我是直接设置的断点行数,后来发现这种姿势不对,应该是
1 | (dlv) b main.main |
(2)执行下一步
next (alias: n) ------------- Step over to next source line.
(3)进入函数
step (alias: s) ------------- Single step through program.
(3)移动当前帧到上一步
1 | up -------------------------- Move the current frame up. |
(4)下面以l和p示例列出所有的用法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 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 |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步