使用delve調試golang
目錄
前置要求
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/pkg.createVmSvcScrape
devops/alert-sd-engine/pkg.deleteVmSvcScrape
devops/alert-sd-engine/pkg.getVmSvcScrape
devops/alert-sd-engine/pkg.updateVmSvcScrape
使用break(b)打斷點
根據funcs
找到的函數,使用break
在需要的函數上打斷點
(dlv) break devops/alert-sd-engine/pkg.(*Monitor).getVmSvcScrape
當然也可以使用如下方式將斷點打到文件的某一行
(dlv) b engine.go:196
使用breakpoints
查看當前活動的斷點。
(dlv) breakpoints
Breakpoint runtime-fatal-throw (enabled) at 0x4345e0 for runtime.throw() /usr/local/go/src/runtime/panic.go:1188 (0)
Breakpoint unrecovered-panic (enabled) at 0x434940 for runtime.fatalpanic() /usr/local/go/src/runtime/panic.go:1271 (0)
print runtime.curg._panic.arg
Breakpoint 2 (enabled) at 0x1399452 for devops/alert-sd-engine/pkg.(*Monitor).getVmSvcScrape() .alert-sd-engine/pkg/engine.go:195 (0)
Breakpoint 4 (enabled) at 0x1399479 for devops/alert-sd-engine/pkg.(*Monitor).getVmSvcScrape() ./alert-sd-engine/pkg/engine.go:196 (0)
使用clear
清除斷點
使用clear
使用clearall
可以清除所有斷點
使用goroutines
查看所有協程
(dlv) goroutines
Goroutine 1 - User: /usr/local/go/src/net/fd_unix.go:173 net.(*netFD).accept (0x5f4f55) [IO wait]
Goroutine 2 - User: /usr/local/go/src/runtime/proc.go:367 runtime.gopark (0x4370f6) [force gc (idle) 455958h37m56.413188346s]
Goroutine 3 - User: /usr/local/go/src/runtime/proc.go:367 runtime.gopark (0x4370f6) [GC sweep wait]
Goroutine 4 - User: /usr/local/go/src/runtime/proc.go:367 runtime.gopark (0x4370f6) [GC scavenge wait]
使用goroutine
使用stack(bt)
查看goroutine
的棧資訊
(dlv) goroutine 1 stack
0 0x00000000004370f6 in runtime.gopark
at /usr/local/go/src/runtime/proc.go:367
1 0x000000000042f7fe in runtime.netpollblock
at /usr/local/go/src/runtime/netpoll.go:445
2 0x000000000045efa9 in internal/poll.runtime_pollWait
at /usr/local/go/src/runtime/netpoll.go:229
使用frame
可以設置當前棧位置,使用up
可以向上移動棧,使用down
可以向下移動棧
使用attach
連接到正在運行的進程
使用attach
使用locals
列印當前的局部遍歷,使用-v
可以列印更詳細的資訊
(dlv) locals -v req
req = devops/alert-sd-engine/pkg.Req {
Base: devops/alert-sd-engine/pkg.commData {
Env: 0,
ClusterName: "",
DualActive: false,
Namespace: "",
Name: "",
Endpoints: []devops/alert-sd-engine/pkg.Endpoint len: 0, cap: 0, nil,},
Selector: struct { Appid string "json:\"appId,omitempty\"" } {Appid: ""},}
更多參見官方文檔