Go语言远程调试
1、安装dlv
# 安装dlv
$ go install github.com/go-delve/delve/cmd/dlv@latest
$ dlv version
Delve Debugger
Version: 1.20.1
Build: $Id: 96e65b6c615845d42e0e31d903f6475b0e4ece6e $
2、命令行远程调试
我们远程(Linux服务器)有如下代码:
[root@zsx src]# tree go-dlv/
go-dlv/
├── go.mod
└── main.go
0 directories, 2 files
main.go
文件的内容如下:
[root@zsx go-dlv]# cat main.go
package main
import "fmt"
func main(){
fmt.Println("1")
fmt.Println("2")
fmt.Println("2")
fmt.Println("4")
fmt.Println("5")
age := 10
name := "tom"
fmt.Println(age)
fmt.Println(name)
fmt.Println("6")
fmt.Println("7")
fmt.Println("8")
fmt.Println("9")
fmt.Println("10")
}
对 main.go
进行编译:
[root@zsx go-dlv]# go build -ldflags=-compressdwarf=false -gcflags=all="-N -l" -o main main.go
[root@zsx go-dlv]# ls
go.mod main main.go
远程使用 dlv 启动程序:
[root@zsx go-dlv]# dlv exec ./main --listen=:2345 --headless=true --api-version=2 --check-go-version=false --accept-multiclient
API server listening at: [::]:2345
2023-06-27T20:59:42+08:00 warning layer=rpc Listening for remote connections (connections are not authenticated nor encrypted)
# 可以是带命令行参数启动,在可执⾏程序后面带上--,再后面就是命令行参数
$ dlv --listen=:2345 --headless=true --api-version=2 --check-go-version=false --accept-multiclient exec ./main -- -s 123
在 windows 上进行远程连接:
C:\Users\zhangshixing>dlv connect 192.168.43.195:2345
Type 'help' for list of commands.
(dlv) b main.go:13
Breakpoint 1 set at 0x496956 for main.main() /home/zhangshixing/go_work_space/src/go-dlv/main.go:13
(dlv) c
> main.main() /home/zhangshixing/go_work_space/src/go-dlv/main.go:13 (hits goroutine(1):1 total:1) (PC: 0x496956)
(dlv) p age
10
(dlv) p name
"tom"
(dlv) c
Process 88442 has exited with status 0
(dlv) exit
Remote process has exited. Would you like to kill the headless instance? [Y/n] y
3、vscode远程调试
在远程(Linux服务器)代码目录下启动 dlv:
[root@zsx go-dlv]# dlv debug --headless --listen=:2345 --api-version=2 --accept-multiclient --check-go-version=false
API server listening at: [::]:2345
2023-06-27T21:33:33+08:00 warning layer=rpc Listening for remote connections (connections are not authenticated nor encrypted)
然后在 vscode 中配置远程调试:
删除 "remotePath": "${workspaceFolder}",
这一行。
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Connect to server",
"type": "go",
"request": "attach",
"mode": "remote",
"port": 2345,
"host": "192.168.43.195"
}
]
}
下面我们进行调试,在代码第13行下断点。
点击调试:
查看远端服务器的执行情况:
[root@zsx go-dlv]# dlv debug --headless --listen=:2345 --api-version=2 --accept-multiclient --check-go-version=false
API server listening at: [::]:2345
2023-06-27T21:33:33+08:00 warning layer=rpc Listening for remote connections (connections are not authenticated nor encrypted)
1
2
2
4
5
我们点击继续:
再次查看远端服务器的执行情况:
[root@zsx go-dlv]# dlv debug --headless --listen=:2345 --api-version=2 --accept-multiclient --check-go-version=false
API server listening at: [::]:2345
2023-06-27T21:33:33+08:00 warning layer=rpc Listening for remote connections (connections are not authenticated nor encrypted)
1
2
2
4
5
10
tom
6
7
8
9
10
4、golang远程调试
在远程(Linux服务器)代码目录下启动 dlv:
[root@zsx go-dlv]# dlv debug --headless --listen=:2345 --api-version=2 --accept-multiclient --check-go-version=false
API server listening at: [::]:2345
2023-06-27T21:58:25+08:00 warning layer=rpc Listening for remote connections (connections are not authenticated nor encrypted)
然后在 golang 中配置远程调试:
下面我们进行调试,在代码第13行下断点。
点击调试:
查看远端服务器的执行情况:
[root@zsx go-dlv]# dlv debug --headless --listen=:2345 --api-version=2 --accept-multiclient --check-go-version=false
API server listening at: [::]:2345
2023-06-27T21:58:25+08:00 warning layer=rpc Listening for remote connections (connections are not authenticated nor encrypted)
1
2
2
4
5
我们点击继续:
再次查看远端服务器的执行情况:
[root@zsx go-dlv]# dlv debug --headless --listen=:2345 --api-version=2 --accept-multiclient --check-go-version=false
API server listening at: [::]:2345
2023-06-27T21:58:25+08:00 warning layer=rpc Listening for remote connections (connections are not authenticated nor encrypted)
1
2
2
4
5
10
tom
6
7
8
9
10