当前位置:网站首页>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
边栏推荐
- In the past six months, it has been invested by five "giants", and this intelligent driving "dark horse" is sought after by capital
- The picgo shortcut is amazing. This person thinks exactly the same as me
- [每日一氵]Latex 的通讯作者怎么搞
- Learn selenium to simulate mouse operation, and you can be lazy a little bit
- Automatique, intelligent, visuel! Forte conviction des huit conceptions derrière la solution sslo
- Principle of SSM framework
- 超视频时代,什么样的技术会成为底座?
- Factory high-precision positioning management system, digital safety production management
- Korean AI team plagiarizes shock academia! One tutor with 51 students, or plagiarism recidivist
- 德国iF多项大奖加冕,这副耳机有多强?音珀GTW 270 Hybrid深度评测
猜你喜欢
实现数字永生还有多久?元宇宙全息真人分身#8i
学会了selenium 模拟鼠标操作,你就可以偷懒点点点了
Crypto Daily:孙宇晨在MC12上倡议用数字化技术解决全球问题
[IDM] IDM downloader installation
运动捕捉系统原理
How to adjust the size of computer photos to what you want
怎麼用MySQL語言進行行列裝置?
Factory high-precision positioning management system, digital safety production management
[open source data] open source data set for cross modal (MRI, Meg, eye movement) human spatial memory research based on virtual reality scenes
StoneDB 为国产数据库添砖加瓦,基于 MySQL 的一体化实时 HTAP 数据库正式开源!
随机推荐
ADS算力芯片的多模型架构研究
电脑照片尺寸如何调整成自己想要的
最新NLP赛事实践总结!
How does win11 set user permissions? Win11 method of setting user permissions
idea启动Command line is too long问题处理
Sales management system of lightweight enterprises based on PHP
【Hot100】20. Valid parentheses
【Hot100】17. Letter combination of telephone number
How to adjust the size of computer photos to what you want
vim用户自动命令示例
【Hot100】19. Delete the penultimate node of the linked list
2023 spring recruitment Internship - personal interview process and face-to-face experience sharing
Use Tencent cloud to build a map bed service
部门来了个拿25k出来的00后测试卷王,老油条表示真干不过,已被...
C#/VB. Net merge PDF document
智慧党建: 穿越时空的信仰 | 7·1 献礼
The picgo shortcut is amazing. This person thinks exactly the same as me
Im instant messaging develops a message delivery scheme for 10000 people
周少剑,很少见
【SQL语句】请问这边为什么select出了两个上海,查询出了不同的count我想让他变成一个上海,count只显示一个总和