使用delve调试golang
时间:2022-01-08 作者:charlieroro
目录
- 前置要求
-
使用方式
- 使用funcs查找支持的函数
- 使用break(b)打断点
- 使用breakpoints查看当前活动的断点。
- 使用clear清除断点
- 使用goroutines查看所有协程
- 使用stack(bt)查看goroutine的栈信息
- 使用attach连接到正在运行的进程
前置要求
dlv调试要求可执行文件不能删掉调试信息,即-ldflags
参数中不能包含 -w -s
标志。可以使用如下方式查看可执行文件是否有删除调试信息,"not stripped"表示没有删除调试信息
# file alert-sd-engine
alert-sd-engine: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped
使用方式
使用funcs
查找支持的函数
使用funcs
可以打印可以查看调试的函数。可以在后面加上函数的名字或部分名字可以检索出支持的函数,如:
(dlv) funcs VmSvc
devops/alert-sd-engine/pkg.(*Monitor).createVmSvcScrape
devops/alert-sd-engine/pkg.(*Monitor).deleteVmSvcScrape
devops/alert-sd-engine/pkg.(*Monitor).getVmSvcScrape
devops/alert-sd-engine/pkg.(*Monitor).isVmSvcScrapeExist
devops/alert-sd-engine/pkg.(*Monitor).updateVmSvcScrape
devops/alert-sd-engine/域名teVmSvcScrape
devops/alert-sd-engine/域名teVmSvcScrape
devops/alert-sd-engine/域名mSvcScrape
devops/alert-sd-engine/域名teVmSvcScrape
使用break(b)打断点
根据funcs
找到的函数,使用break
在需要的函数上打断点
(dlv) break devops/alert-sd-engine/pkg.(*Monitor).getVmSvcScrape
当然也可以使用如下方式将断点打到文件的某一行
(dlv) b 域名:196
使用breakpoints
查看当前活动的断点。
(dlv) breakpoints
Breakpoint runtime-fatal-throw (enabled) at 0x4345e0 for 域名w() /usr/local/go/src/runtime/域名:1188 (0)
Breakpoint unrecovered-panic (enabled) at 0x434940 for 域名lpanic() /usr/local/go/src/runtime/域名:1271 (0)
print 域名域名
Breakpoint 2 (enabled) at 0x1399452 for devops/alert-sd-engine/pkg.(*Monitor).getVmSvcScrape() .alert-sd-engine/pkg/域名:195 (0)
Breakpoint 4 (enabled) at 0x1399479 for devops/alert-sd-engine/pkg.(*Monitor).getVmSvcScrape() ./alert-sd-engine/pkg/域名:196 (0)
使用clear
清除断点
使用clear
使用clearall
可以清除所有断点
使用goroutines
查看所有协程
(dlv) goroutines
Goroutine 1 - User: /usr/local/go/src/net/域名:173 net.(*netFD).accept (0x5f4f55) [IO wait]
Goroutine 2 - User: /usr/local/go/src/runtime/域名:367 域名rk (0x4370f6) [force gc (idle) 域名88346s]
Goroutine 3 - User: /usr/local/go/src/runtime/域名:367 域名rk (0x4370f6) [GC sweep wait]
Goroutine 4 - User: /usr/local/go/src/runtime/域名:367 域名rk (0x4370f6) [GC scavenge wait]
使用goroutine
使用stack(bt)
查看goroutine
的栈信息
(dlv) goroutine 1 stack
0 0x00000000004370f6 in 域名rk
at /usr/local/go/src/runtime/域名:367
1 0x000000000042f7fe in 域名ollblock
at /usr/local/go/src/runtime/域名:445
2 0x000000000045efa9 in internal/域名ime_pollWait
at /usr/local/go/src/runtime/域名:229
使用frame
可以设置当前栈位置,使用up
可以向上移动栈,使用down
可以向下移动栈
使用attach
连接到正在运行的进程
使用attach
使用locals
打印当前的局部遍历,使用-v
可以打印更详细的信息
(dlv) locals -v req
req = devops/alert-sd-engine/域名 {
Base: devops/alert-sd-engine/域名Data {
Env: 0,
ClusterName: "",
DualActive: false,
Namespace: "",
Name: "",
Endpoints: []devops/alert-sd-engine/域名oint len: 0, cap: 0, nil,},
Selector: struct { Appid string "json:\"appId,omitempty\"" } {Appid: ""},}
更多参见官方文档