首页 > 其他分享 >GO语言调试利器dlv快速上手

GO语言调试利器dlv快速上手

时间:2023-07-17 15:22:06浏览次数:37  
标签:bin GOROOT 利器 dlv go GO runtime root

GO语言调试利器dlv快速上手

 

golang 安装

 tar -xvf go1.15.2.linux-arm64.tar.gz  -C  /usr/local/go

复制代码
[root@centos7 ~]# ls  /usr/local/go
go
[root@centos7 ~]# ls  /usr/local/go/go/
api  AUTHORS  bin  CONTRIBUTING.md  CONTRIBUTORS  doc  favicon.ico  lib  LICENSE  misc  PATENTS  pkg  README.md  robots.txt  SECURITY.md  src  test  VERSION
[root@centos7 ~]# 
复制代码

 

 

复制代码
[root@centos7 ~]# go  version
go version go1.15.2 linux/arm64
[root@centos7 ~]# cat go.source 
export GOPATH=/opt/gopath
export GOROOT=/usr/local/go/go
export GOARCH=arm64
export GOOS=linux
export GOTOOLS=/pkg/tool
#export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/bin:/bin
export PATH=$PATH:$GOROOT/bin
[root@centos7 ~]# 
复制代码

 

 增加
export PATH=$PATH:$GOPATH/bin
复制代码
[root@centos7 ~]# cat go.source 
export GOPATH=/opt/gopath
export GOROOT=/usr/local/go15.2/go
export GOARCH=arm64
export GOOS=linux
export GOTOOLS=/pkg/tool
#export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/bin:/bin
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
[root@centos7 ~]# ls /opt/gopath/bin/dlv 
/opt/gopath/bin/dlv
[root@centos7 ~]# 
复制代码

 

一、dlv的安装

1)下载dlv

git clone https://github.com/go-delve/delve.git $GOPATH/src/github.com/go-delve/delve

或者 go get github.com/derekparker/delve/cmd/dlv

root@cloud:~# go version
go version go1.14.10 linux/arm64

 

2)安装

cd $GOPATH/src/github.com/go-delve/delve

make install

 

复制代码
root@cloud:~/delve# make install -j $(nproc)
go install "-ldflags=-X main.Build=1b2357092c8fee7406988d8191f71059700c5399" github.com/go-delve/delve/cmd/dlv
root@cloud:~/delve# dlv -h
Delve is a source level debugger for Go programs.

Delve enables you to interact with your program by controlling the execution of the process,
evaluating variables, and providing information of thread / goroutine state, CPU register state and more.

The goal of this tool is to provide a simple yet powerful interface for debugging Go programs.

Pass flags to the program you are debugging using `--`, for example:

`dlv exec ./hello -- server --config conf/config.toml`
复制代码

 

 

复制代码
(dlv) bp
Breakpoint runtime-fatal-throw at 0x429a0 for runtime.fatalthrow() GOROOT/src/runtime/panic.go:1162 (0)
Breakpoint unrecovered-panic at 0x42a00 for runtime.fatalpanic() GOROOT/src/runtime/panic.go:1189 (0)
        print runtime.curg._panic.arg
Breakpoint 1 at 0x3edc70 for gvisor.dev/gvisor/pkg/sentry/mm.(*MemoryManager).MMap() pkg/sentry/mm/syscalls.go:75 (1)
(dlv) clear 1
Breakpoint 1 cleared at 0x3edc70 for gvisor.dev/gvisor/pkg/sentry/mm.(*MemoryManager).MMap() pkg/sentry/mm/syscalls.go:75
(dlv) bp
Breakpoint runtime-fatal-throw at 0x429a0 for runtime.fatalthrow() GOROOT/src/runtime/panic.go:1162 (0)
Breakpoint unrecovered-panic at 0x42a00 for runtime.fatalpanic() GOROOT/src/runtime/panic.go:1189 (0)
        print runtime.curg._panic.arg
(dlv) quit
Would you like to kill the process? [Y/n] n
root@cloud:~/delve# 
复制代码

 

dlv和golang

 

复制代码
root@cloud:~/delve# dlv -h

Command 'dlv' not found, did you mean:

  command 'ddv' from deb ncbi-tools-x11
  command 'delv' from deb dnsutils
  command 'dav' from deb dav-text
  command 'lv' from deb lv
  command 'dlg' from deb pccts
  command 'dll' from deb brickos

Try: apt install <deb name>

root@cloud:~/delve# source ~/hyper/go_source 
root@cloud:~/delve# dlv -h
Delve is a source level debugger for Go programs.

Delve enables you to interact with your program by controlling the execution of the process,
evaluating variables, and providing information of thread / goroutine state, CPU register state and more.

The goal of this tool is to provide a simple yet powerful interface for debugging Go programs.

Pass flags to the program you are debugging using `--`, for example:

`dlv exec ./hello -- server --config conf/config.toml`
复制代码

 

查看协程

复制代码
(dlv) goroutines
Goroutine 1 - User: GOROOT/src/runtime/sema.go:56 sync.runtime_Semacquire (0x73ac8) [semacquire]
Goroutine 2 - User: GOROOT/src/runtime/proc.go:307 runtime.gopark (0x450c0) [force gc (idle)]
Goroutine 3 - User: GOROOT/src/runtime/proc.go:307 runtime.gopark (0x450c0) [GC sweep wait]
Goroutine 4 - User: GOROOT/src/runtime/proc.go:307 runtime.gopark (0x450c0) [GC scavenge wait]
Goroutine 5 - User: GOROOT/src/runtime/proc.go:307 runtime.gopark (0x450c0) [GC worker (idle)]
Goroutine 6 - User: GOROOT/src/runtime/proc.go:307 runtime.gopark (0x450c0) [GC worker (idle)]
Goroutine 7 - User: GOROOT/src/runtime/proc.go:307 runtime.gopark (0x450c0) [GC worker (idle)]
Goroutine 8 - User: GOROOT/src/runtime/proc.go:307 runtime.gopark (0x450c0) [GC worker (idle)]
Goroutine 9 - User: GOROOT/src/runtime/proc.go:307 runtime.gopark (0x450c0) [GC worker (idle)]
Goroutine 10 - User: GOROOT/src/runtime/proc.go:307 runtime.gopark (0x450c0) [GC worker (idle)]
Goroutine 11 - User: GOROOT/src/runtime/proc.go:307 runtime.gopark (0x450c0) [GC worker (idle)]
Goroutine 12 - User: GOROOT/src/runtime/proc.go:307 runtime.gopark (0x450c0) [GC worker (idle)]
Goroutine 13 - User: GOROOT/src/runtime/proc.go:307 runtime.gopark (0x450c0) [GC worker (idle)]
Goroutine 14 - User: GOROOT/src/runtime/proc.go:307 runtime.gopark (0x450c0) [GC worker (idle)]
复制代码

 

 

could not open debug info

复制代码
[root@centos7 ~]# cat go.source 
export GOPATH=/opt/gopath
export GOROOT=/usr/local/go15.2/go
export GOARCH=arm64
export GOOS=linux
export GOTOOLS=/pkg/tool
#export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/bin:/bin
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
[root@centos7 ~]# ls /opt/gopath/bin/dlv 
/opt/gopath/bin/dlv
[root@centos7 ~]# ps -elf | grep nginx | grep ingress
4 S 101       64638  64619  0  80   0 -     7 SyS_rt 03:05 ?        00:00:00 /usr/bin/dumb-init -- /nginx-ingress-controller --publish-service=ingress-nginx/ingress-nginx-controller --election-id=ingress-controller-leader --ingress-class=nginx --configmap=ingress-nginx/ingress-nginx-controller --validating-webhook=:8443 --validating-webhook-certificate=/usr/local/certificates/cert --validating-webhook-key=/usr/local/certificates/key
4 S 101       64679  64638  0  80   0 - 11673 SyS_ep 03:05 ?        00:00:13 /nginx-ingress-controller --publish-service=ingress-nginx/ingress-nginx-controller --election-id=ingress-controller-leader --ingress-class=nginx --configmap=ingress-nginx/ingress-nginx-controller --validating-webhook=:8443 --validating-webhook-certificate=/usr/local/certificates/cert --validating-webhook-key=/usr/local/certificates/key
[root@centos7 ~]# dlv attach 64679
could not attach to pid 64679: could not open debug info
[root@centos7 ~]# 
复制代码

 

 

一、dlv的安装

1)下载dlv

git clone https://github.com/go-delve/delve.git $GOPATH/src/github.com/go-delve/delve

或者 go get github.com/derekparker/delve/cmd/dlv

2)安装

cd $GOPATH/src/github.com/go-delve/delve

make install

二、dlv简要使用说明

2.1、获取帮助信息

安装后执行dlv -h将会看到帮助信息:

image

上面的信息只是列出了命令列表,具体使用方法没有给出,我们可以执行dlv help + 具体命令来查看详细说明,

比如我们执行dlv help attach:

image

2.2、进入调试模式

1)dlv attach pid:类似与gdb attach pid,可以对正在运行的进程直接进行调试(pid为进程号)。

2)dlv debug:运行dlv debug test.go会先编译go源文件,同时执行attach命令进入调试模式,该命令会在当前目录下生成一个名为debug的可执行二进制文件,退出调试模式会自动被删除。

3)dlv exec executable_file :直接从二进制文件启动调试模式。如果要带参数执行需要添加--,如dlv exec executable_file -- -f xxx.conf

4)dlv core executable_file core_file:以core文件启动调试,通常进行dlv的目的就是为了找出可执行文件core的原因,通过core文件可直接找出具体进程异常的信息。

3、常用调试方法

3.1 dlv trace追踪调用轨迹

该命令最直接的用途是可以追踪代码里函数的调用轨迹,

如下源代码,现用trace命令跟踪其调轨迹。

package main import ( "fmt" "time" ) func Test() { fmt.Println("hello") time.Sleep(1000 * 1000 * 100) } func Test2() { fmt.Println("world") time.Sleep(1000 * 1000 * 100) } func main() { for i := 0; i < 2; i++ { go Test() go Test2() } time.Sleep(1000 * 1000 * 2000) fmt.Println("end") }

运行结果,这里看除了Test,test2也被追踪:

$ dlv trace hello.go Test

> goroutine(19): main.Test2()

> goroutine(21): main.Test2()

> goroutine(18): main.Test()

world

hello

world

> goroutine(20): main.Test()

hello

=> ()

=> ()

=> ()

=> ()

end

3.2 调试模式基本命令

这里用上节的源码作为示例进行调试。开始调试:dlv debug hello.go

1)b(break):打断点

设置断点,当需要设置多个断点时,为了断点可识别可进行自定义命名。进入调试模式后先打断点。

例:b Test

b test.go:13

image

2)r(restart):重启当前进程

类似gdb里的run,如果刚执行dlv debug hello.go,进程已经起来,不用执行。如果进程已结算或需要重新开始则需要执行r

3)c(continue):继续执行到断点处

image

4)bp:查看所有断点

image

5)on  :当运行到某断点时执行相应命令

断点可以是名称(在设置断点时可命名断点)或者编号,例如on 3 p i表示运行到断点3时打印变量i。

image

6)cond(condition)   :有条件的断点

针对某个断点,只有表达式成立才会被中断。例:

condition 3 i==1

image

image

7)n(next):逐行执行代码,不进入函数内

8)s(step):逐行执行代码,遇到函数会跳进内部

9)stepout:当使用s命令进入某个函数后,执行它可跳出函数

10)si(step-instruction):单步单核执行代码

如果不希望多协程并发执行可以使用该命令,这在多协程调试时极为方便。

11)args:查看被调用函数所传入的参数值

12)locals:查看所有局部变量

locals var_name:查看具体某个变量,var_name可以是正则表达式。

13)clear:清除单个断点

14)clearall:清除所有断点

15)list:打印当前断点位置的源代码

list后面加行号可以展示该行附近的源代码,要注意该行必须是代码行而不能是空行。

16)bt:打印当前栈信息。

3.3 多协程调试

1)goroutines:显示所有协程

image

2)goroutine:协程切换

先执行goroutine 7表示切换到7号协程上

3.4 其他命令

1)frame:切换栈。

2)regs:打印寄存器内容。

3)sources:打印所有源代码文件路径

4)source:执行一个含有dlv命令的文件

source命令允许将dlv命令放在一个文件中,然后逐行执行文件内的命令。

5)trace:类似于打断点,但不会中断,同时会输出一行提示信息

标签:bin,GOROOT,利器,dlv,go,GO,runtime,root
From: https://www.cnblogs.com/cqx6388/p/17560208.html

相关文章

  • python操作mongodb实现读写分离
    读写分离默认情况下,MongoClient实例将查询发送到副本集的主要成员。要使用副节点作为查询,以实现读写分离,我们必须更改读取首选项:读取首选项在模块pymongo.ReadPreference下:PRIMARY:从主节点中读取(默认)PRIMARY_PREFERRED:优先从主节点读取,如果不可用则从副节点读取、SECOND......
  • MongoDB副本集的搭建和管理(高可用)
    使得mongodb具备自动故障转移、高可用、读写分离。副本集默认情况下读写都只是通过主库,副节点只是备份数据而已,但是可以设置副节点允许读操作,这样就可以做成读写分离。使用pymongo的时候也可以设置,还可以动态设置库、集合的readPreference。详细的后面再写。。。如果主节点不可......
  • Mongodb从入门到熟悉
    RDB:数据库→表(列)→行Mongo:数据库→集合→文档SQL和Mongodb的关系映射表(里面还有一些增删改查等等操作与SQL的对应的语句):https://www.mongodb.com/docs/manual/reference/sql-comparison/mongodb数据格式是以BSON(二进制json)存储的。https://www.mongodb.......
  • python操作mongodb基本使用
    使用pymongo,具体可以参考官方文档:语法上基本和原生mongodb是一样的,所以非常容易入手...https://pymongo.readthedocs.io/en/stable/tutorial.html日常使用importpprintimportpymongofrompymongo.collectionimportCollectionfrompymongo.databaseimportDatabasei......
  • Scrapy在pipeline中集成mongodb
    settings.py中设置配置项MONGODB_HOST="127.0.0.1"MONGODB_PORT=27017MONGODB_DB_NAME="bang123"pipelines.py:fromscrapy.pipelines.imagesimportImagesPipelinefromitemadapterimportis_item,ItemAdapterclassBang123Pipeline:......
  • django项目中分多个models.py创建table
    django项目每个app都默认有一个model.py文件。当app需要大量建立table,且需要分类时,能否建立多个model.py文件?。1、在app文件夹下创建一个models文件夹(myapp/models/):2、将app文件夹中models.py文件删除(也可以剪切到models文件夹中):3、models文件夹下新建__init__.py:4、......
  • 五、Gorm
    5.1、gorm介绍是当今比较热门的golang的orm操作数据库的工具,使用上主要是把struct类和数据库表进行映射,操作数据库时无需手写sql。本质就是提供一组函数来帮助我们快速拼接sql语句,尽量减少sql的编写。gorm文档和gorm中文文档安装包:goget-ugorm.io/gormgoget......
  • 【6.0】Django框架之路由层
    【一】路由匹配#路由匹配path('test',views.test),path('testadd',views.testadd),无法跳转到testaddurl方法第一个参数是正则表达式只要第一个参数正则表达式能够匹配到内容,就会立刻停止匹配,执行视图函数#路由匹配path('test/',views.test),path('testadd......
  • 【9.0】Django框架之模型层
    【一】前言Django自带的sqlite3数据对日期格式不敏感,处理的时候容易出错【1】测试脚本测试脚本当我们只想要测试Django中的某一个py文件的内容时,我们可以不需要书写前后端交互的形式,而是直接写一个测试脚本即可测试环境的准备在测试文件中书写下面内容这内容其......
  • 【8.0】Django框架之模板层
    【一】模板语法的传值{{}}:变量相关{%%}:逻辑相关【1】数据准备路由#模板语法传值url(r'^index/',views.index),【2】基本数据类型(1)视图defindex(request):#模板语法可以传递的后端Python数据类型#整型a=123#浮点型b=11.11......