一. 關(guān)于delve
delve 是go語(yǔ)言的調(diào)試器楼入,delve的目標(biāo)是為go提供一個(gè)簡(jiǎn)潔哥捕、功能齊全的debug工具,delve易于調(diào)用和使用嘉熊。
二. 安裝
為了能夠編譯delve遥赚,需要安裝Go 1.10或更高版本
安裝好go后,直接go get即可安裝阐肤,更多安裝教程見(jiàn):https://github.com/go-delve/delve/tree/master/Documentation/installation
go get github.com/go-delve/delve/cmd/dlv
安裝好后凫佛,在終端執(zhí)行dlv或者dlv help 會(huì)看到dlv的幫助信息讲坎,則說(shuō)明安裝成功
dlv常用命令
- dlv help # 顯示dlv幫助信息
- dlv version # 顯示dlv版本信息
- dlv attach # 連接到正在運(yùn)行的go進(jìn)程并開(kāi)始調(diào)試
- dlv connect # 連接到調(diào)試服務(wù)器,用于遠(yuǎn)程調(diào)試愧薛,需要遠(yuǎn)程有二進(jìn)制文件和源碼及dlv工具
- dlv core
- dlv dap
- dlv debug # 默認(rèn)在當(dāng)前目錄中編譯并開(kāi)始調(diào)試go main 包晨炕,或者指定模塊路徑
- dlv exec # 執(zhí)行已經(jīng)編譯好的二進(jìn)制文件,并開(kāi)始調(diào)試
- dlv run # 不推薦使用毫炉,用debug代替
- dlv test # 編譯測(cè)試二進(jìn)制文件瓮栗,并開(kāi)始調(diào)試,即調(diào)試測(cè)試文件
- dlv trace # 編譯并開(kāi)始追蹤程序
debug 和 exec是比較常用的命令
三. 入門(mén)手冊(cè)
delve的目標(biāo)是成為一個(gè)簡(jiǎn)潔而強(qiáng)大的工具瞄勾。但如果你不習(xí)慣在編譯語(yǔ)言中使用源碼調(diào)試费奸,則可能令人困惑。本文檔將提供開(kāi)始調(diào)試go程序所需的全部信息进陡。
調(diào)試入門(mén)程序
調(diào)試?yán)映绦蛉缦?br>
├── go.mod
├── go.sum
├── main.go
├── test
└── utils
├── util.go
└── util_test.go
調(diào)試程序主要有三個(gè)文件愿阐,main.go、util.go趾疚、util_test.go缨历,內(nèi)容如下,比較簡(jiǎn)單糙麦,go包管理工具使用的是go module辛孵,模塊名為test
module test
go 1.14
// main.go
package main
import (
"fmt"
"test/utils"
)
func main() {
for i := 0; i < 10; i++ {
fmt.Println(i)
}
fmt.Println(utils.Add(100, 2300))
}
// utils/util.go
package utils
func Add(a, b int) int {
return a + b
}
// utils/utils_test.go
package utils
import "testing"
func TestAdd(t *testing.T) {
if Add(100, 200) != 300 {
t.Error("100 + 200 != 300")
}
}
調(diào)試main 包
$ dlv debug # 開(kāi)始調(diào)試main包
Type 'help' for list of commands.
(dlv) b main.main # 在main.main 方法上打斷點(diǎn)
Breakpoint 1 set at 0x10d0fd8 for main.main() ./main.go:8
(dlv) bp # 打印當(dāng)前斷點(diǎn)
Breakpoint runtime-fatal-throw at 0x1038420 for runtime.fatalthrow() /usr/local/go/src/runtime/panic.go:1162 (0)
Breakpoint unrecovered-panic at 0x10384a0 for runtime.fatalpanic() /usr/local/go/src/runtime/panic.go:1189 (0)
print runtime.curg._panic.arg
Breakpoint 1 at 0x10d0fd8 for main.main() ./main.go:8 (0)
(dlv) c # 繼續(xù)執(zhí)行,直到下一個(gè)斷點(diǎn)停止
> main.main() ./main.go:8 (hits goroutine(1):1 total:1) (PC: 0x10d0fd8)
3: import (
4: "fmt"
5: "test/utils"
6: )
7:
=> 8: func main() {
9: for i := 0; i < 10; i++ {
10: fmt.Println(i)
11: }
12: fmt.Println(utils.Add(100, 2300))
13: }
(dlv) b main.go:12 # 在main.go 文件中的12行打一個(gè)斷點(diǎn)
Breakpoint 2 set at 0x10d10b8 for main.main() ./main.go:12
(dlv) n # 下一步喳资,遇到函數(shù)不進(jìn)入
> main.main() ./main.go:9 (PC: 0x10d0fef)
4: "fmt"
5: "test/utils"
6: )
7:
8: func main() {
=> 9: for i := 0; i < 10; i++ {
10: fmt.Println(i)
11: }
12: fmt.Println(utils.Add(100, 2300))
13: }
(dlv) s # 單步執(zhí)行程序觉吭,遇到函數(shù)進(jìn)入
> main.main() ./main.go:10 (PC: 0x10d1007)
5: "test/utils"
6: )
7:
8: func main() {
9: for i := 0; i < 10; i++ {
=> 10: fmt.Println(i)
11: }
12: fmt.Println(utils.Add(100, 2300))
13: }
(dlv) c
0
1
2
3
4
5
6
7
8
9
> main.main() ./main.go:12 (hits goroutine(1):1 total:1) (PC: 0x10d10b8)
7:
8: func main() {
9: for i := 0; i < 10; i++ {
10: fmt.Println(i)
11: }
=> 12: fmt.Println(utils.Add(100, 2300))
13: }
(dlv) c
2400
Process 53433 has exited with status 0
(dlv)
調(diào)試測(cè)試包
$ dlv test test/utils # 開(kāi)始調(diào)試utils下的測(cè)試包
Type 'help' for list of commands.
(dlv) bp # 打印出當(dāng)前的端點(diǎn)
Breakpoint runtime-fatal-throw at 0x103c3a0 for runtime.fatalthrow() /usr/local/go/src/runtime/panic.go:1162 (0)
Breakpoint unrecovered-panic at 0x103c420 for runtime.fatalpanic() /usr/local/go/src/runtime/panic.go:1189 (0)
print runtime.curg._panic.arg
(dlv) funcs test.Test*
(dlv) b TestAdd # 在TestAdd方法上打一個(gè)端點(diǎn)
Breakpoint 1 set at 0x1166273 for test/utils.TestAdd() ./utils/util_test.go:5
(dlv) bp
Breakpoint runtime-fatal-throw at 0x103c3a0 for runtime.fatalthrow() /usr/local/go/src/runtime/panic.go:1162 (0)
Breakpoint unrecovered-panic at 0x103c420 for runtime.fatalpanic() /usr/local/go/src/runtime/panic.go:1189 (0)
print runtime.curg._panic.arg
Breakpoint 1 at 0x1166273 for test/utils.TestAdd() ./utils/util_test.go:5 (0)
(dlv) c # continue 繼續(xù)執(zhí)行
> test/utils.TestAdd() ./utils/util_test.go:5 (hits goroutine(4):1 total:1) (PC: 0x1166273)
1: package utils
2:
3: import "testing"
4:
=> 5: func TestAdd(t *testing.T) {
6: if Add(100, 200) != 300 {
7: t.Error("100 + 200 != 300")
8: }
9: }
(dlv) c
PASS
Process 52711 has exited with status 0
(dlv)
用法詳解
用法詳解見(jiàn)wiki:https://github.com/go-delve/delve/tree/master/Documentation/cli,沒(méi)有寫(xiě)詳細(xì)的內(nèi)容仆邓,后續(xù)會(huì)不定時(shí)補(bǔ)充
運(yùn)行命令
- continue # 運(yùn)行到斷點(diǎn)或程序結(jié)束鲜滩,簡(jiǎn)寫(xiě) c
- next # 跳到下一個(gè)源碼行,簡(jiǎn)寫(xiě) n
- rebuild # 重新編譯目標(biāo)文件并執(zhí)行节值,如果可執(zhí)行文件不是通過(guò)dlv編譯的就不起作用
- restart # 重啟程序
- step # 單步執(zhí)行程序徙硅,遇到函數(shù)進(jìn)入,簡(jiǎn)寫(xiě) s
- step-instruction # 單cpu指令執(zhí)行搞疗,簡(jiǎn)寫(xiě) si
- step-out # 跳出當(dāng)前函數(shù)嗓蘑,簡(jiǎn)寫(xiě) so
操作斷點(diǎn)
- break # 打斷點(diǎn),簡(jiǎn)寫(xiě) b
- breakpoints # 查看斷點(diǎn)列表匿乃,簡(jiǎn)寫(xiě) bp
- clear # 刪除斷點(diǎn)
- clearall # 刪除所有斷點(diǎn)
- condition # 設(shè)置斷點(diǎn)條件
- on # 在命中斷點(diǎn)時(shí)執(zhí)行
- trace # 設(shè)置追蹤點(diǎn)
查看程序變量和內(nèi)存
- args # 打印函數(shù)變量
- locals # 打印局部變量
- print # 打印變量的值桩皿,如print a
- regs # 打印cpu 寄存器內(nèi)容
- set # 設(shè)置變量的值,如 set a = 100
- vars # 打印包變量
- whatis # 查看變量類(lèi)型幢炸,如 whatis a泄隔,輸出 int
列出和選擇線程或協(xié)程
- goroutines # 查看所有協(xié)程
- goroutines # 選擇指定協(xié)程,如 goroutines 1
- threads # 列出所有線程
- thread # 選擇指定線程宛徊,如 thread 2208678
查看調(diào)用堆棧并選擇幀
其他命令
- funcs # 列出函數(shù)列表
- exit # 退出程序
- help # 幫助信息
- list # 顯示源代碼
- sources # 打印源碼文件列表
- types # 打印類(lèi)型列表
vscode 中配置使用dlv調(diào)試go程序
在vscode debug 的設(shè)置中配置launch.json文件
image.png
mode 設(shè)置為debug時(shí)佛嬉,program的內(nèi)容${fileDirname}即可逻澳,mode 設(shè)置為exec時(shí),program的值為二進(jìn)制文件的路徑暖呕,通過(guò)設(shè)置mode的值斜做,即可調(diào)試源碼和二進(jìn)制程序(也需要有源碼)。mode模式為auto時(shí)湾揽,測(cè)試了下瓤逼,vscode 并不能通過(guò)program的內(nèi)容來(lái)判斷是debug還是exec
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
// https://github.com/golang/vscode-go/blob/master/docs/debugging.md
"name": "Launch",
"type": "go",
"request": "launch", // launch attach
"mode": "exec", // launch: auto debug remote test exec, attach: local, remote
// "program": "${fileDirname}",
"program": "/xxx/mod/test/test",
"env": {},
"args": []
}
]
}
遠(yuǎn)程調(diào)試
遠(yuǎn)程調(diào)試時(shí),需要在遠(yuǎn)程也有源碼钝腺、二進(jìn)制包和dlv工具
在遠(yuǎn)端執(zhí)行dlv命令
dlv debug --headless --listen=:8989 --api-version=2 --accept-multiclient
#用degbug方式啟動(dòng)遠(yuǎn)程應(yīng)用程序
dlv exec --headless --listen=:8989 ./test --api-version=2 --accept-multiclient
# exec執(zhí)行當(dāng)前目錄下的test二進(jìn)制文件
--listen:指定調(diào)試端口
--api-version:指定api版本抛姑,默認(rèn)是1
--accept-multiclient:接受多個(gè)client調(diào)試
在vscode中線下好源碼,和遠(yuǎn)端的源碼結(jié)構(gòu)一致艳狐。launch.json配置如下:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "attach",
"mode": "remote",
"remotePath": "/xxx/mod/test",
"host": "xxx",
"port": 8989,
}
]
}
在vscode中打好斷點(diǎn)后,就可以進(jìn)行遠(yuǎn)程調(diào)試了