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

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