为什么用VS代码和WSL调试Go语言会挂起Delve?

iq0todco  于 2023-02-01  发布在  Go
关注(0)|答案(3)|浏览(242)

I am trying to debug a Go program in VS Code, using WSL. The program compiles and runs, but when trying to debug with dlv dap, it seems to hang and never reach the first breakpoint. I am using go 1.17, dlv 1.7.2 and Ubuntu 20.04. The problem is replicated with the simple Hello World program:

package main

import "fmt"

func main() {
    fmt.Println("hello world")
}

If I chose Run->Run Without Debugging, the Debug Consol shows
Starting: /mnt/f/mattm/home/go/bin/dlv-dap dap --check-go-version=false --only-same-user=false --listen=127.0.0.1:53252 --log=true --log-output=rpc --log-dest=3 from /mnt/f/mattm/home/hw
DAP server listening at: 127.0.0.1:53252
hello world
Process 17851 has exited with status 0 dlv dap (17754) exited with code: 0
The program has run and exited successfully (though why start dlv if not debugging?) But if I try to Run->Start Debugging, the console window only shows:
Starting: /mnt/f/mattm/home/go/bin/dlv-dap dap --check-go-version=false --only-same-user=false --listen=127.0.0.1:56084 --log=true --log-output=rpc --log-dest=3 from /mnt/f/mattm/home/hw
DAP server listening at: 127.0.0.1:56084
The program does not appear to execute, VS Code never shows execution reaching a breakpoint. (The outcome is the same whether or not I've set breakpoints). Task Manager does show a dlv-dap process running; it appears to be hung and waiting for something. But what? If I terminate debugging under VS Code, the dlv-dap process remains. It is as though VS Code spun off the dlv process and then forgotten about it. Do I need to prompt VS Code to connect to the DAP server to proceed? How do I do that?

Addendum: I am using the following launch configuration:

"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${fileDirname}",
"logOutput": "dap,debugger",
"showLog": true,
"dlvFlags" : ["--only-same-user=false"]

After turning on verbose dlv logging (as in the config above), I get the following:
Starting: /mnt/f/mattm/home/go/bin/dlv-dap dap --check-go-version=false --only-same-user=false --listen=127.0.0.1:51444 --log=true --log-output=dap,debugger --log-dest=3 from /mnt/f/mattm/home/breezepms/hello-world DAP server listening at: 127.0.0.1:51444 2021-10-17T16:35:27+01:00 debug layer=dap DAP server pid = 7019 2021-10-17T16:35:27+01:00 debug layer=dap DAP connection started 2021-10-17T16:35:27+01:00 debug layer=dap [<- from client]{"seq":1,"type":"request","command":"initialize","arguments":{"clientID":"vscode","clientName":"Visual Studio Code","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,"success":true,"command":"initialize","body":{"supportsConfigurationDoneRequest":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 client]{"seq":2,"type":"request","command":"launch","arguments":{"name":"Launch file","type":"go","request":"launch","mode":"debug","program":"./hello-world.go","logOutput":"dap,debugger","showLog":true,"dlvFlags":["--only-same-user=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":[],"dlvToolPath":"/mnt/f/mattm/home/go/bin/dlv-dap","env":{},"__buildDir":"/mnt/f/mattm/home/breezepms/hello-world","__sessionId":"8e201eb7-88ec-452f-896d-3d5e98596c05"}} 2021-10-17T16:35:27+01:00 debug layer=dap debug backend is 'default' 2021-10-17T16:35:27+01:00 debug layer=dap building program './hello-world.go' in '/mnt/f/mattm/home/breezepms/hello-world' with flags '' 2021-10-17T16:35:29+01:00 debug layer=dap running binary '/tmp/__debug_bin3449446367' in '.' 2021-10-17T16:35:29+01:00 info layer=debugger launching process with args: [/tmp/__debug_bin3449446367]

hfyxw5xn

hfyxw5xn1#

上面的评论帮助我找到了一个解决方案。问题不在于VS代码本身,而是底层的dlv无法正常工作。在互联网上有一些关于这个问题的文档https://github.com/microsoft/WSL/issues/2977。dlv在WSL 1中不工作。虽然我认为我已经升级到WSL 2,* 我没有升级我的Ubuntu安装到WSL 2*。在WSL 2下重新安装Ubuntu 20.04后,(并重新安装go和dlv)我能够从VS代码调试。

92dk7w1h

92dk7w1h2#

我想我来晚了,但无论如何,这是不可能得到深入工作的WSL,但由于WSL 2成为可用的,现在你可以。
在我的情况下,我可以通过以下步骤来做到这一点:
1-在WSL 2(或远程主机)中安装“深入研究”

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-在VSCode中设置launch.json

...
{
    "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
这些链接救了我的命,希望这能有所帮助。

wgx48brx

wgx48brx3#

我在wsl2下使用delve和windows10下使用vscode工作室进行了vscode调试配置,感谢@Potemkin在这里给了我关于这个主题的第一个见解。
以下是我的工作配置:
1.在wsl2中安装深度调试服务器:

git clone https://github.com/go-delve/delve
cd delve
go install github.com/go-delve/delve/cmd/dlv

参见:www.example.comhttps://github.com/go-delve/delve/tree/master/Documentation/installation#installation
PS:如果您的GOBIN目录不在您引用的PATH变量中,您必须使用绝对路径执行dlv二进制文件:$HOME/go/bin/dlv
1.在wsl2中运行深入调试服务器:

cd <your-golang-project>
dlv debug ./main.go --headless --listen=127.0.0.1:12345 --accept-multiclient --continue --only-same-user=false

当您使用wsl2和windows时,only-same-user参数非常重要,否则您可能会收到以下错误消息:closing connection from different user (127.0.0.1:34406): connections to localhost are only accepted from the same UNIX user for security reasons.

    • 此外,如果没有在listen参数中明确说明www.example.com环回地址,它对我不起作用**(只是使用listen =:12345不起作用)!127.0.0.1 loopback adress in the listen parameter it wasnt working for me**(just using listen=:12345 didnt worked)!

有关使用delve和vscode进行远程调试的详细信息,请参阅以下链接:www.example.comhttps://github.com/golang/vscode-go/blob/master/docs/debugging.md#remote-debugging
1.在vscode studio的"运行和调试"选项卡中使用此launch.json并开始调试:

// Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Example Debug WSL2",
            "type": "go",
            "request": "attach",
            "mode": "remote",
            "port": 12345,
            "host": "127.0.0.1"
        }
    ]
}

我还尝试使用"debugAdapter":"dlv-dap"配置在我的launch.json中,但当到达断点时,它以错误消息"找不到文件..."结束。使用默认的遗留debugAdapeter与上述配置一起使用,它工作正常。

相关问题