我正在使用WSL调试VS代码中的Go程序。程序编译并运行,但当尝试使用dlv dap进行调试时,它似乎挂起,永远不会到达第一个断点。我使用的是go 1.17、dlv 1.7.2和Ubuntu20.04。这个问题被简单的Hello程序复制:
package main
import "fmt"
func main() {
fmt.Println("hello world")
}
如果我选择了Run->Run不进行调试,则Debug Consol将显示
启动: /mnt/f/mattm/home/go/bin/dlv-dap
DAP服务器监听: 127.0.0.1:53252
你好,世界
进程17851已退出,状态为0 dlv dap (17754),代码:0
程序已成功运行和退出(如果不进行调试,为什么要启动dlv?)但是,如果我试图运行->启动调试,控制台窗口只显示:
启动: /mnt/f/mattm/home/go/bin/dlv-dap
DAP服务器监听: 127.0.0.1:56084
程序似乎没有执行,VS代码从来没有显示执行达到断点。(无论我是否设置了断点,结果都是一样的)。任务管理器确实显示了正在运行的dlv-dap进程;它似乎是挂起的,正在等待什么。但是什么呢?如果我在VS代码下终止调试,则dlv-dap进程将保持不变。就好像VS代码从dlv过程中分离出来,然后忘记了它。是否需要提示VS代码才能连接到DAP服务器?我该怎么做?
增编:我正在使用以下启动配置:
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${fileDirname}",
"logOutput": "dap,debugger",
"showLog": true,
"dlvFlags" : ["--only-same-user=false"]
在打开详细的dlv日志记录之后(如上面的配置所示),我得到以下内容:
启动:/mnt/f/mattm/home/go/bin/dlv调试器--从/mnt/f/mattm/home/breezepms/hello-world DAP服务器侦听: 127.0.0.1:51444 2021-10-17T16:35:27+01:00 layer=dap DAP服务器pid = 7019 2021-10-10-17T16:35:27+01:00调试layer=dap DAP连接启动2021-10-17T16:35:27+01:00调试layer=dap <-从客户端{“seq”:1,“类型”:“请求”,“命令”:“初始化”,参数{“clientID”:“vscode”,"clientName":"Visual代码“,"adapterID":"go","locale":"en-gb","linesStartAt1":true,"columnsStartAt1":true,"pathFormat":"path","supportsVariableType":true,"supportsVariablePaging":true,"supportsRunInTerminalRequest":true,"supportsMemoryReferences":true,"supportsProgressReporting":true,”supportsInvalidatedEvent“:true} 2021-10-17T16:35:27+01:00 debug layer=dap -> to client{"seq":0,"type":"response","request_seq":1、“成功”:“true”、"supportsFunctionBreakpoints":true、"supportsConditionalBreakpoints":true、"supportsEvaluateForHovers":true、"supportsSetVariable":true、"supportsExceptionInfoRequest":true、"supportTerminateDebuggee":true、"supportsDelayedStackTraceLoading":true、"supportsLogPoints":true、"supportsClipboardContext":true、“supportsSteppingGranularity”:true} 2021-10-17T16:35:27+01:00 debug layer=dap <- from客户端{“seq”:2,“键入”:“请求”,“命令”:“启动”,“参数”:{“名称”:“启动文件”,“键入”:“go”,“请求”:“启动”,“模式”:“调试”,“程序”:“./hello-world.goo”,"logOutput":"dap,调试器“,"showLog":true,”dlvFlags“:”--仅同一用户--用户=false“,"__configurationTarget":5,"packagePathToGoModPathMap":{"/mnt/f/mattm/home/breezepms/web/xmlutilities":"/mnt/f/mattm/home/breezepms/web/xmlutilities","/mnt/f/mattm/home/breezepms/ledgers":"/mnt/f/mattm/home/breezepms/ledgers","/mnt/f/mattm/home/breezepms/web":"/mnt/f/mattm/home/breezepms","/mnt/f/mattm/home/breezepms/web/filehandling":"/mnt/f/mattm/home/breezepms/web/filehandling","/mnt/f/mattm/home/breezepms/persistence":"/mnt/f/mattm/home/breezepms/persistence","/mnt/f/mattm/home/breezepms/cmd/csvloader":"/mnt/f/mattm/home/breezepms","/mnt/f/mattm/home/breezepms/hello-world":"/mnt/f/mattm/home/breezepms"},"debugAdapter":"dlv-dap","apiVersion":2,"dlvLoadConfig":{"followPointers":true,"maxVariableRecurse":1,"maxStringLen":64,"maxArrayValues":64,"maxStructFields":-1},"showGlobalVariables":false,"substitutePath":[],"__buildDir":"/mnt/f/mattm/home/breezepms/hello-world","env":{}"__sessionId":"8e201eb7-88ec-452f-896d-3d5e98596c05"}} 2021-10-17T16:35:27+01:00调试layer=dap调试后端是“默认的”2021-10-17T16:35:27+01:00调试layer=dap构建程序。/hello-world.go‘in '/mnt/f/mattm/home/breezepms/hello-world’带有标志‘2021-10-17T16:35:29+01:00 debug layer=dap运行二进制文件’/tmp/_ debug _bin3449446367‘在'.’2021-10-17T16:35带有args的:29+01:00 info layer=debugger启动过程:/tmp/_layer=debugger_bin3449446367
发布于 2021-10-31 16:35:40
上面的评论帮助我找到了一个解决方案。问题不在VS代码本身,而是根本的dlv无法正常工作。关于互联网上的https://github.com/microsoft/WSL/issues/2977,有一些关于这个问题的文档。dlv在WSL 1中不工作。虽然我以为我已经升级到WSL 2,但我还没有将我的Ubuntu安装升级到WLS 2。在WSL 2下重新安装Ubuntu 20.04之后(以及重新安装go和dlv),我能够从VS代码中调试。
发布于 2022-01-11 03:55:50
我想我要迟到了,但是无论如何,我们不可能深入研究WSL,但是既然WSL2变得可以了,你现在就可以了。
在我的例子中,我可以通过以下步骤来做到这一点:
1-在WSL 2 (或远程主机)中安装Delve
go install github.com/go-delve/delve/cmd/dlv@latest
2-使用以下标志(-gcflags "all=-N -l")编译并启动我的应用程序,以允许Delve收集高级调试信息。例如:
go run cmd/server/main.go -gcflags "all=-N -l"
3-要在WLS 2中调试,我首先必须运行一个无头Delve服务器,方法是运行:
dlv debug cmd/server/main.go --headless --listen=:2345 --log --api-version=2
4-在launch.json中设置VSCode
...
{
"version": "0.2.0",
"configurations": [
{
"name": "Example Debug WSL2",
"type": "go",
"debugAdapter": "dlv-dap",
"request": "attach",
"mode": "remote",
"remotePath": "${workspaceFolder}",
"port": 2345,
"host": "127.0.0.1"
}
]
}
...
这里有一些有用的文档来了解更多的细节。
https://github.com/golang/vscode-go/blob/master/docs/debugging.md#remote-debugging
https://go.googlesource.com/vscode-go/+/refs/heads/dev.go2go/docs/debugging.md
https://jerome-ran.medium.com/golang-remote-debugging-with-visual-studio-code-a918de5d83f3
这些链接救了我的命,希望这有帮助。
https://stackoverflow.com/questions/69506371
复制相似问题