当前位置:网站首页>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
边栏推荐
- SQLServer查询: a.id与b.id相同时,a.id对应的a.p在b.id对应的b.p里找不到的话,就显示出这个a.id和a.p
- StoneDB 为国产数据库添砖加瓦,基于 MySQL 的一体化实时 HTAP 数据库正式开源!
- Does 1.5.1 in Seata support mysql8?
- Stonedb is building blocks for domestic databases, and the integrated real-time HTAP database based on MySQL is officially open source!
- 最新NLP赛事实践总结!
- [daily news]what happened to the corresponding author of latex
- 圈铁发音,动感与无噪强强出彩,魔浪HIFIair蓝牙耳机测评
- Please, stop painting star! This has nothing to do with patriotism!
- Win11如何設置用戶權限?Win11設置用戶權限的方法
- Uncover the "intelligence tax" of mousse: spend 4billion on marketing, and only 7 invention patents
猜你喜欢

从大湾区“1小时生活圈”看我国智慧交通建设

u本位合约和币本位合约有区别,u本位合约会爆仓吗

PostgreSQL 存储结构浅析

Smart Party Building: faith through time and space | 7.1 dedication

【LeetCode】43. String multiplication

Huawei issued hcsp-solution-5g security talent certification to help build 5g security talent ecosystem

揭秘慕思“智商税”:狂砸40亿搞营销,发明专利仅7项

AVL balanced binary search tree

芯片供应转向过剩,中国芯片日产增加至10亿,国外芯片将更难受

DO280管理应用部署--pod调度控制
随机推荐
[IDM] IDM downloader installation
芯片供应转向过剩,中国芯片日产增加至10亿,国外芯片将更难受
运动捕捉系统原理
Which MySQL functions are currently supported by tablestore in table storage?
表格存储中tablestore 目前支持mysql哪些函数呢?
[SQL statement] Why do you select two Shanghai and query different counts here? I want it to become a Shanghai, and count only displays a sum
There is a difference between u-standard contract and currency standard contract. Will u-standard contract explode
China's intelligent transportation construction from the perspective of "one hour life circle" in Dawan District
程序员职业生涯真的很短吗?
Trace the source of drugs and tamp the safety dike
Embedded development: five revision control best practices
Pico,是要拯救还是带偏消费级VR?
周少剑,很少见
毕业后5年,我成为了年薪30w+的测试开发工程师
Please, stop painting star! This has nothing to do with patriotism!
STM32F1与STM32CubeIDE编程实例-PWM驱动蜂鸣器生产旋律
接口测试框架中的鉴权处理
Pico, do you want to save or bring consumer VR?
Guide for high-end programmers to fish at work
AVL 平衡二叉搜索树