当前位置:网站首页>Go language source level debugger delve
Go language source level debugger delve
2022-07-01 16:21:00 【frank.】
Hello everyone , I am a frank.
01
Introduce
Delve It's a simple 、 Powerful and easy to use Go Language source level debugger , It's also Go Officially recommended debugger .
02
install
Delve Very simple installation , If readers use Go 1.16 Or later , You can use it directly go install
install :
go install github.com/go-delve/delve/cmd/[email protected]
If readers use less than Go 1.16 Version of , But download it first Delve Source code , And then use go install
install :
git clone https://github.com/go-delve/delve
cd delve
go install github.com/go-delve/delve/cmd/dlv
After installation , have access to go help install
see dlv
Detailed location of executable . I suggest readers to dlv
Executable file , Configuration to PATH environment variable .
It should be noted that , If readers use macOS, You also need to install command line development tools :
xcode-select --install
To avoid every use of dlv All need authorization to use debugger, Readers are advised to open the developer mode :
sudo /usr/sbin/DevToolsSecurity -enable
03
practice
At the completion of Part 02 After all operations in , We use dlv version
Check dlv
Whether the executable program can be used .
We can use dlv
Start a modal session with any available command of , The commonly used command is dlv debug
, dlv exec
and dlv test
. Limited to space , In this paper we introduce dlv debug
How to use .
Sample code :
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
}
Read the above code example we will use to debug the session , It contains a main Function and a sum function ,main Function to define variables a And variables b, call sub function , And assign the returned result to the variable c, Finally, print the variable c Value .
Start a debugging session :
[[email protected] work]# dlv debug
Type 'help' for list of commands.
(dlv)
Read the code above , We use dlv debug
Start a debugging session , Without any parameters ,Delve Compile and start debugging main
package .
We can also specify a file name ,Delve Will compile the specified file main
package , And start a debugging session .
[[email protected] work]# dlv debug main.go
Type 'help' for list of commands.
(dlv)
After the debugging session starts , We can use the debug command to debug the program .
list command :
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)
After the debugging session starts , We can use list The command lists the source code of the specified location , There are two ways , The first way is <package name>.<func name>
, The second way is <file name>:<line number>
.
break command :
dlv debug
Type 'help' for list of commands.
(dlv) break main.main
Breakpoint 1 set at 0x49670a for main.main() ./main.go:7
(dlv)
We can use break Command add breakpoint , and list command , Add the location of the breakpoint , You can also use the above two methods .
We can use breakpoints command , List all breakpoints , have access to clear Command deletes the specified breakpoint , have access to clearall Delete all assertions .
continue、next、step、stepout and print command :
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)
Read the code above , We use Delve After adding breakpoints , perform continue command , The program will execute to the breakpoint ; perform next command , The program continues to execute the next line of code ; perform step command , The program steps into the calling function ; perform stepout command , The program steps out to the calling position of the calling function ; perform print command , Print the value of the specified parameter .
Readers use the above commands , It can meet most debugging scenarios . For ease of understanding , The commands used in the above examples do not use the abbreviated form , In actual use , It will be more convenient to use abbreviated forms .
Abbreviation form :
- break(b)
- continue(c)
- next(n)
- step(s)
- stepout(so)
- print(p)
04
summary
In this article, we briefly introduce Go Language debugger Delve The basic way of use , Readers can put Delve Use up , Replace with print Debug the code in the form of printing .
About Delve Advanced features of , For example, debugging goroutines、 Attach the debugger to an existing process 、 Remote debugging and from VSCode Editor or Goland IDE Use Delve. Interested readers can refer to Delve Help document for .
Reference material :
- https://go.dev/doc/gdb
- https://github.com/go-delve/delve/tree/master/Documentation
边栏推荐
- 怎么用MySQL语言进行行列装置?
- Action after deleting laravel's model
- vim用户自动命令示例
- 数据库系统原理与应用教程(006)—— 编译安装 MySQL5.7(Linux 环境)
- UML旅游管理系统「建议收藏」
- The sharp drop in electricity consumption in Guangdong shows that the substitution of high-tech industries for high-energy consumption industries has achieved preliminary results
- [PHP graduation design] design and implementation of textbook management system based on php+mysql+apache (graduation thesis + program source code) -- textbook management system
- 【Hot100】20. 有效的括号
- Where should older test / development programmers go? Will it be abandoned by the times?
- IM即時通訊開發實現心跳保活遇到的問題
猜你喜欢
What is the digital transformation of manufacturing industry
SQLServer查询: a.id与b.id相同时,a.id对应的a.p在b.id对应的b.p里找不到的话,就显示出这个a.id和a.p
周少剑,很少见
苹果自研基带芯片再次失败,说明了华为海思的技术领先性
Crypto Daily: Sun Yuchen proposed to solve global problems with digital technology on MC12
Five years after graduation, I became a test development engineer with an annual salary of 30w+
Analysis of PostgreSQL storage structure
数据库系统原理与应用教程(004)—— MySQL 安装与配置:重置 MySQL 登录密码(windows 环境)
Go language learning notes - Gorm use - table addition, deletion, modification and query | web framework gin (VIII)
Sweden announced its decision to exclude Huawei 5g equipment, but Huawei has successfully found a new way out
随机推荐
运动捕捉系统原理
Apple's self-developed baseband chip failed again, which shows Huawei Hisilicon's technological leadership
[daily question] 1175 Prime permutation
Go 语言怎么优化重复的 if err != nil 样板代码?
Vscode find and replace the data of all files in a folder
July 1, 2022 Daily: Google's new research: Minerva, using language models to solve quantitative reasoning problems
Task. Run(), Task. Factory. Analysis of behavior inconsistency between startnew() and new task()
Submission lottery - light application server essay solicitation activity (may) award announcement
In the era of super video, what kind of technology will become the base?
Nuxt.js数据预取
process.env.NODE_ENV
數據庫系統原理與應用教程(006)—— 編譯安裝 MySQL5.7(Linux 環境)
基于PHP的轻量企业销售管理系统
【Hot100】20. Valid parentheses
Introduction to RT thread env tool (learning notes)
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
Motion capture system for apple picking robot
Crypto Daily: Sun Yuchen proposed to solve global problems with digital technology on MC12
When ABAP screen switching, refresh the previous screen
Microservice tracking SQL (support Gorm query tracking under isto control)