当前位置:网站首页>Go 语言源码级调试器 Delve
Go 语言源码级调试器 Delve
2022-07-01 16:11:00 【frank.】
大家好,我是 frank。
01
介绍
Delve 是一个简单、强大和易用的 Go 语言源代码层级的调试器,也是 Go 官方推荐使用的调试器。
02
安装
Delve 安装非常简单,如果读者朋友使用的是 Go 1.16 或更高版本,可以直接使用 go install
安装:
go install github.com/go-delve/delve/cmd/[email protected]
如果读者朋友们使用的是低于 Go 1.16 的版本,可是先下载 Delve 源码,然后使用 go install
安装:
git clone https://github.com/go-delve/delve
cd delve
go install github.com/go-delve/delve/cmd/dlv
安装完成之后,可以使用 go help install
查看 dlv
可执行文件的详细位置。我建议读者朋友们将 dlv
可执行文件,配置到 PATH 环境变量。
需要注意的是,如果读者朋友们使用的是 macOS,还需要安装命令行开发工具:
xcode-select --install
为了避免每次使用 dlv 都需要授权允许使用 debugger,建议读者朋友们开启开发者模式:
sudo /usr/sbin/DevToolsSecurity -enable
03
实践
在完成 Part 02 中的所有操作之后,我们使用 dlv version
检查 dlv
可执行程序是否已可以使用。
我们可以使用 dlv
的任意可用命令启动一个调式会话,比较常用的命令是 dlv debug
, dlv exec
和 dlv test
。限于篇幅,本文我们介绍 dlv debug
的使用方法。
示例代码:
package main
import (
"fmt"
)
func main() {
a := 1
b := 2
c := sum(a, b)
fmt.Println(c)
}
func sum(a, b int) int {
res := a + b
return res
}
阅读上面这段我们将用于调试会话的代码示例,它包含一个 main 函数和一个 sum 函数,main 函数中定义变量 a 和变量 b,调用 sub 函数,并将返回结果赋值给变量 c,最后打印变量 c 的值。
启动一个调试会话:
[[email protected] work]# dlv debug
Type 'help' for list of commands.
(dlv)
阅读上面这段代码,我们使用 dlv debug
启动一个调试会话,在没有任何参数的情况下,Delve 编译并开始调试当前目录中的 main
包。
我们也可以指定一个文件名,Delve 将会编译该指定文件的 main
包,并启动一个调试会话。
[[email protected] work]# dlv debug main.go
Type 'help' for list of commands.
(dlv)
调试会话启动后,我们可以使用调试命令进行调试程序。
list 命令:
dlv debug
Type 'help' for list of commands.
(dlv) list main.main
Showing /work/main.go:7 (PC: 0x49670a)
2:
3: import (
4: "fmt"
5: )
6:
7: func main() {
8: a := 1
9: b := 2
10: c := sum(a, b)
11: fmt.Println(c)
12: }
(dlv) list ./main.go:7
Showing /work/main.go:7 (PC: 0x49670a)
2:
3: import (
4: "fmt"
5: )
6:
7: func main() {
8: a := 1
9: b := 2
10: c := sum(a, b)
11: fmt.Println(c)
12: }
(dlv)
调试会话启动后,我们可以使用 list 命令列出指定位置的源码,包含两种方式,第一种方式是 <package name>.<func name>
,第二种方式是 <file name>:<line number>
。
break 命令:
dlv debug
Type 'help' for list of commands.
(dlv) break main.main
Breakpoint 1 set at 0x49670a for main.main() ./main.go:7
(dlv)
我们可以使用 break 命令添加断点,和 list 命令一样,添加断点的位置,也可以使用上述两种方式。
我们可以使用 breakpoints 命令,列出所有断点,可以使用 clear 命令删除指定断点,可以使用 clearall 删除所有断定。
continue、next、step、stepout 和 print 命令:
dlv debug
Type 'help' for list of commands.
(dlv) break main.main
Breakpoint 1 set at 0x49670a for main.main() ./main.go:7
(dlv) continue
> main.main() ./main.go:7 (hits goroutine(1):1 total:1) (PC: 0x49670a)
2:
3: import (
4: "fmt"
5: )
6:
=> 7: func main() {
8: a := 1
9: b := 2
10: c := sum(a, b)
11: fmt.Println(c)
12: }
(dlv) next
> main.main() ./main.go:8 (PC: 0x496718)
3: import (
4: "fmt"
5: )
6:
7: func main() {
=> 8: a := 1
9: b := 2
10: c := sum(a, b)
11: fmt.Println(c)
12: }
13:
(dlv) next
> main.main() ./main.go:9 (PC: 0x496721)
4: "fmt"
5: )
6:
7: func main() {
8: a := 1
=> 9: b := 2
10: c := sum(a, b)
11: fmt.Println(c)
12: }
13:
14: func sum(a, b int) int {
(dlv) next
> main.main() ./main.go:10 (PC: 0x49672a)
5: )
6:
7: func main() {
8: a := 1
9: b := 2
=> 10: c := sum(a, b)
11: fmt.Println(c)
12: }
13:
14: func sum(a, b int) int {
15: res := a + b
(dlv) print a
1
(dlv) print b
2
(dlv) step
> main.sum() ./main.go:14 (PC: 0x4967e0)
9: b := 2
10: c := sum(a, b)
11: fmt.Println(c)
12: }
13:
=> 14: func sum(a, b int) int {
15: res := a + b
16: return res
17: }
(dlv) next
> main.sum() ./main.go:15 (PC: 0x496800)
10: c := sum(a, b)
11: fmt.Println(c)
12: }
13:
14: func sum(a, b int) int {
=> 15: res := a + b
16: return res
17: }
(dlv) next
> main.sum() ./main.go:16 (PC: 0x49680f)
11: fmt.Println(c)
12: }
13:
14: func sum(a, b int) int {
15: res := a + b
=> 16: return res
17: }
(dlv) next
> main.main() ./main.go:10 (PC: 0x496739)
Values returned:
~r0: 3
5: )
6:
7: func main() {
8: a := 1
9: b := 2
=> 10: c := sum(a, b)
11: fmt.Println(c)
12: }
13:
14: func sum(a, b int) int {
15: res := a + b
(dlv) next
> main.main() ./main.go:11 (PC: 0x49673e)
6:
7: func main() {
8: a := 1
9: b := 2
10: c := sum(a, b)
=> 11: fmt.Println(c)
12: }
13:
14: func sum(a, b int) int {
15: res := a + b
16: return res
(dlv) print c
3
(dlv)
阅读上面这段代码,我们使用 Delve 添加断点后,执行 continue 命令,程序将执行到断点位置;执行 next 命令,程序继续执行下一行代码;执行 step 命令,程序步入到调用函数内部;执行 stepout 命令,程序步出到调用函数的调用位置;执行 print 命令,打印指定参数的值。
读者朋友们使用以上命令,可以满足大部分调试场景。为了方便理解,以上示例中使用的命令都没有使用简写形式,在实际使用时,使用简写形式会更加便捷。
简写形式:
- break(b)
- continue(c)
- next(n)
- step(s)
- stepout(so)
- print(p)
04
总结
本文我们简单介绍 Go 语言调试器 Delve 的基本使用方式,读者朋友们可以在程序调试时将 Delve 使用起来,替换使用 print 打印的形式调试代码。
关于 Delve 的高级功能,例如调试 goroutines、将调试器附加到现有进程、远程调试以及从 VSCode 编辑器或 Goland IDE 使用 Delve。感兴趣的读者朋友们可以参考 Delve 的帮助文档。
参考资料:
- https://go.dev/doc/gdb
- https://github.com/go-delve/delve/tree/master/Documentation
边栏推荐
- 一次革命、两股力量、三大环节:《工业能效提升行动计划》背后的“减碳”路线图...
- Use Tencent cloud to build a map bed service
- [IDM] IDM downloader installation
- 【Hot100】19. Delete the penultimate node of the linked list
- Sales management system of lightweight enterprises based on PHP
- 程序员职业生涯真的很短吗?
- The Department came to a Post-00 test paper king who took out 25K. The veteran said it was really dry, but it had been
- 【LeetCode】43. String multiplication
- 瑞典公布决定排除华为5G设备,但是华为已成功找到新出路
- China's intelligent transportation construction from the perspective of "one hour life circle" in Dawan District
猜你喜欢
【LeetCode】43. 字符串相乘
IM即时通讯开发实现心跳保活遇到的问题
ATSs: automatically select samples to eliminate the difference between anchor based and anchor free object detection methods
Win11如何设置用户权限?Win11设置用户权限的方法
Korean AI team plagiarizes shock academia! One tutor with 51 students, or plagiarism recidivist
Huawei issued hcsp-solution-5g security talent certification to help build 5g security talent ecosystem
She is the "HR of others" | ones character
动作捕捉系统用于苹果采摘机器人
[IDM] IDM downloader installation
Sqlserver query: when a.id is the same as b.id, and the A.P corresponding to a.id cannot be found in the B.P corresponding to b.id, the a.id and A.P will be displayed
随机推荐
Crypto Daily:孙宇晨在MC12上倡议用数字化技术解决全球问题
ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification
【Hot100】17. 电话号码的字母组合
周少剑,很少见
学会了selenium 模拟鼠标操作,你就可以偷懒点点点了
【Hot100】20. 有效的括号
vim用户自动命令示例
运动捕捉系统原理
Hardware development notes (9): basic process of hardware development, making a USB to RS232 module (8): create asm1117-3.3v package library and associate principle graphic devices
【观察】数字化时代的咨询往何处走?软通咨询的思与行
芯片供应转向过剩,中国芯片日产增加至10亿,国外芯片将更难受
Research on multi model architecture of ads computing power chip
Pocket Network为Moonbeam和Moonriver RPC层提供支持
瑞典公布决定排除华为5G设备,但是华为已成功找到新出路
Uncover the "intelligence tax" of mousse: spend 4billion on marketing, and only 7 invention patents
Pico, do you want to save or bring consumer VR?
Action after deleting laravel's model
圈铁发音,动感与无噪强强出彩,魔浪HIFIair蓝牙耳机测评
电脑照片尺寸如何调整成自己想要的
Where should older test / development programmers go? Will it be abandoned by the times?