安装

Golang debug 推荐使用 Delve 工具,项目地址:https://github.com/derekparker/delve

go mod
# cd $GOPATH/src/
# git clone https://github.com/derekparker/delve.git
# cd delve/cmd/dlv/
# go build
# go install
go build
go: golang.org/x/crypto@v0.0.0-20180614174826-fd5f17ee7299: unrecognized import path "golang.org/x/crypto" (https fetch: Get https://golang.org/x/crypto?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: golang.org/x/sys@v0.0.0-20180614134839-8883426083c0: unrecognized import path "golang.org/x/sys" (https fetch: Get https://golang.org/x/sys?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: golang.org/x/arch@v0.0.0-20171004143515-077ac972c2e4: unrecognized import path "golang.org/x/arch" (https fetch: Get https://golang.org/x/arch?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go.mod
# vim ../../go.mod
# 添加下面替换:
replace (
        golang.org/x/arch v0.0.0-20171004143515-077ac972c2e4 => github.com/golang/arch v0.0.0-20171004143515-077ac972c2e4
        golang.org/x/crypto v0.0.0-20180614174826-fd5f17ee7299 => github.com/golang/crypto v0.0.0-20180614174826-fd5f17ee7299
        golang.org/x/sys v0.0.0-20180614134839-8883426083c0 => github.com/golang/sys v0.0.0-20180614134839-8883426083c0
)

https://studygolang.com/articles/17204?fr=sidebar
========================================================================

远程调试

因为不知道delvel 是如何设置源码的,本地编译的上传到服务器上,服务器要调试看不到源码,很是忧伤,所以干脆使用远程调试吧:

在服务器上 ps x|grep game 查找到gameserver的进程pid

dlv attach $PID --headless --api-version=2 --log --listen=:8181

示例:

➜  test git:(master) ✗ ~/gopath/bin/dlv attach 24393 --headless --api-version=2 --log --listen=:8181
API server listening at: [::]:8181
2020-03-27T17:42:59+08:00 info layer=debugger attaching to pid 24393
Could not attach to pid 24393: this could be caused by a kernel security setting, try writing "0" to /proc/sys/kernel/yama/ptrace_scope

出现这种问题的话,使用sudo➜  test git:(master) ✗ sudo ~/gopath/bin/dlv attach 24521 --headless --api-version=2 --log --listen=:8181
API server listening at: [::]:8181
2020-03-27T17:45:44+08:00 info layer=debugger attaching to pid 24521

 

本机只要输入:

dlv connect www.example.com:8181

需要注意的是

本机quit 以后,远程dlv进程也会结束。
本机没有dlv connect,远程dlv直接关闭会导致 远程调试进程PID直接退出(很是忧伤)
本机dlv输入quit以后,会让你选择是否关闭调试进程,这个有时候也方便,不过大多数都是选择N 不关闭调试进程PID

https://www.cnblogs.com/ayanmw/p/8995178.html