为什么使用VS Code和WSL调试Go时会导致Delve卡住?

huangapple go评论100阅读模式
英文:

Why does debugging Go with VS Code and WSL hang Delve?

问题

我正在尝试在使用WSL的VS Code中调试一个Go程序。该程序可以编译和运行,但是在使用dlv dap进行调试时,似乎会挂起并且永远无法达到第一个断点。我使用的是go 1.17、dlv 1.7.2和Ubuntu 20.04。这个问题在简单的Hello World程序中复现:

package main

import "fmt"

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

如果我选择运行->无调试运行,调试控制台显示:

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

程序已成功运行并退出(尽管如果不进行调试,为什么要启动dlv呢?)。但是,如果我尝试运行->开始调试,控制台窗口只显示:

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

程序似乎没有执行,VS Code从未显示执行达到断点的情况。(无论是否设置了断点,结果都是相同的)。任务管理器显示正在运行一个dlv-dap进程;它似乎被挂起并等待某些东西。但是等待什么呢?如果我在VS Code下终止调试,dlv-dap进程仍然存在。就好像VS Code启动了dlv进程,然后忘记了它。我需要提示VS Code连接到DAP服务器以继续吗?我该如何做到这一点?

补充说明:
我使用以下启动配置:

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

在打开详细的dlv日志记录后(如上面的配置),我得到以下结果:

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]

英文:

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 &quot;fmt&quot;

func main() {
	fmt.Println(&quot;hello world&quot;)
}

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 <br>DAP server listening at: 127.0.0.1:53252 <br>
> hello world <br>
> 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 <br> 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:

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

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]

答案1

得分: 2

上面的评论帮助我找到了解决方案。问题不在于VS Code本身,而是底层的dlv无法正常工作。在互联网上有一些关于这个问题的文档,可以在https://github.com/microsoft/WSL/issues/2977找到。dlv在WSL 1中不起作用。尽管我以为我已经升级到了WSL 2,但我没有将我的Ubuntu安装升级到WSL 2。在重新安装Ubuntu 20.04并安装go和dlv之后,我能够从VS Code进行调试。

英文:

The comments above helped me find a solution. The problem wasn't in VS Code itself, rather underlying dlv was unable to function properly. There is some documentation of the issue around the Internet https://github.com/microsoft/WSL/issues/2977. dlv does not work in WSL 1. Though I thought I had upgraded to WSL 2, I had not upgraded my Ubuntu installation to WLS 2. After re-installing Ubuntu 20.04 under WSL 2, (and reinstalling go and dlv) I was able to debug from VS Code.

答案2

得分: 2

我认为我来晚了,但无论如何,在WSL上无法使用Delve,但是由于现在WSL2可用,您现在可以使用它。

在我的情况下,我按照以下步骤操作:

  1. WSL 2(或远程主机)中安装Delve:
go install github.com/go-delve/delve/cmd/dlv@latest
  1. 使用以下标志(-gcflags "all=-N -l")编译和启动应用程序,以允许Delve收集高级调试信息。例如:
go run cmd/server/main.go -gcflags "all=-N -l"
  1. 要在WSL 2中进行调试,首先需要运行一个无头的Delve服务器:
dlv debug cmd/server/main.go --headless --listen=:2345 --log --api-version=2
  1. 在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

这些链接拯救了我的生命,希望对您有所帮助。

英文:

I think I'm coming late but anyway, it's not possible to get delve working on WSL, but since WSL2 become avaliable now you can.

In my case I could do it by following these steps:

1- Install Delve inside WSL 2 (Or remote host)

go install github.com/go-delve/delve/cmd/dlv@latest

2- Compile and launch my application with the following flag (-gcflags "all=-N -l") to allow Delve to collect advanced debugging information. E.g. :

go run cmd/server/main.go -gcflags &quot;all=-N -l&quot;

3- To debug inside WLS 2, I first had to run a headless Delve server by running:

dlv debug cmd/server/main.go --headless --listen=:2345 --log --api-version=2

4- Setup the launch.json in VSCode

...
{
    &quot;version&quot;: &quot;0.2.0&quot;,
    &quot;configurations&quot;: [
        {
            &quot;name&quot;: &quot;Example Debug WSL2&quot;,
            &quot;type&quot;: &quot;go&quot;,
            &quot;debugAdapter&quot;: &quot;dlv-dap&quot;,
            &quot;request&quot;: &quot;attach&quot;,
            &quot;mode&quot;: &quot;remote&quot;,
            &quot;remotePath&quot;: &quot;${workspaceFolder}&quot;,
            &quot;port&quot;: 2345,
            &quot;host&quot;: &quot;127.0.0.1&quot;
        }
    ]
}
...

Here are some useful documentation for furthermore details.

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

These links saved my life, hope this helps.

答案3

得分: 0

我在使用WSL2和Windows 10下的VSCode调试配置中遇到了很多问题,特别是使用Delve。感谢@Potemkin在这里的帖子给了我一些关于这个主题的第一手见解。

以下是我的工作配置:

  1. 在WSL2中安装Delve调试服务器:
git clone https://github.com/go-delve/delve
cd delve
go install github.com/go-delve/delve/cmd/dlv

参考链接:https://github.com/go-delve/delve/tree/master/Documentation/installation#installation

PS:如果你的GOBIN目录没有在PATH变量中引用,你需要使用绝对路径执行dlv二进制文件:$HOME/go/bin/dlv

  1. 在WSL2中运行Delve调试服务器:
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参数中明确指定127.0.0.1回环地址,对我来说是无法工作的(只使用listen=:12345是不行的)!

关于使用Delve和VSCode进行远程调试的更多信息,请参考:https://github.com/golang/vscode-go/blob/master/docs/debugging.md#remote-debugging

  1. 在VSCode Studio的运行和调试选项卡中使用以下launch.json,并开始调试:
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Example Debug WSL2",
            "type": "go",
            "request": "attach",
            "mode": "remote",
            "port": 12345,
            "host": "127.0.0.1"
        }
    ]
}

我还尝试在launch.json中使用"debugAdapter": "dlv-dap"配置,但是当达到断点时,出现了"could not find file..."的错误消息。使用上述配置中默认的旧版debugAdapter时,一切正常。

英文:

I went through hell with my vscode debug configuration using delve under wsl2 and vscode studio under windows 10. Thanks to @Potemkin post here for giving me the first insights to this topic.

Here is my working configuration:

  1. Install delve debug server in wsl2:
git clone https://github.com/go-delve/delve
cd delve
go install github.com/go-delve/delve/cmd/dlv

See: https://github.com/go-delve/delve/tree/master/Documentation/installation#installation

PS: If your GOBIN directory isnt in your PATH variable referenced, you have to execute the dlv binary by using the absolute path: $HOME/go/bin/dlv

  1. Run delve debug server in wsl2:
cd &lt;your-golang-project&gt;
dlv debug ./main.go --headless --listen=127.0.0.1:12345 --accept-multiclient --continue --only-same-user=false

The only-same-user parameter is very important when you are using wsl2 and windows cause otherwise you could end up having this error message: closing connection from different user (127.0.0.1:34406): connections to localhost are only accepted from the same UNIX user for security reasons.

Furthermore without stating explicitly the 127.0.0.1 loopback adress in the listen parameter it wasnt working for me (just using
listen=:12345 didnt worked)!

Further information about remote debugging with delve and vscode you will find here: https://github.com/golang/vscode-go/blob/master/docs/debugging.md#remote-debugging

  1. Use this launch.json in run and debug tab in vscode studio and start debugging:
    // 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
    &quot;version&quot;: &quot;0.2.0&quot;,
    &quot;configurations&quot;: [
        {
            &quot;name&quot;: &quot;Example Debug WSL2&quot;,
            &quot;type&quot;: &quot;go&quot;,
            &quot;request&quot;: &quot;attach&quot;,
            &quot;mode&quot;: &quot;remote&quot;,
            &quot;port&quot;: 12345,
            &quot;host&quot;: &quot;127.0.0.1&quot;
        }
    ]
}

I also tried to use the "debugAdapter": "dlv-dap" configuration in my launch.json, but that ended up with the error message "could not find file..." when reaching a breakpoint. With the default legacy debugAdapeter as used with the config stated above it works fine.

huangapple
  • 本文由 发表于 2021年10月9日 19:37:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/69506371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定