• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 https://www.cnblogs.com/realjimmy/p/13418508.html

 

 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

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 ~]# 

 

 

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
go语言使用go-sciter创建桌面应用(五)加载元素资源发布时间:2022-07-10
下一篇:
Go接口转换的一个例子发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap