golang dlv远程调试,delve安装

安装

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

一、安装
照着 github 上 delve 项目的安装说明操作,go mod模式下推荐使用第二种方式。
1.拉取最新 delve 项目代码到本地,编译安装。

# 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)

原因是 Golang 官网被墙了,这里手动修改go.mod文件,把项目地址替换为 github 上的地址,如:

# 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 连接到服务器上的dlv进程,就可以在本机远程调试了。

需要注意的是

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

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

posted @ 2020-03-27 15:50  kissrule  阅读(3563)  评论(0编辑  收藏  举报