无法在VSCode中使用断点调试Golang。

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

Can't debug Golang with breakpoints in VSCode

问题

我在VS Code中使用断点调试Golang应用程序。
调试器报错说找不到文件,但实际上文件是存在的。

你们中有人知道如何在VS Code中启用Golang应用程序的断点吗?

调试器日志:

Debuggee is not running. Setting breakpoints without halting.
All cleared
Creating on: /home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go:63
Creating on: /home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go:84
All cleared
All set:[]
SetBreakPointsResponse
2021-10-21T12:52:17+02:00 debug layer=rpc <- RPCServer.CreateBreakpoint(rpc2.CreateBreakpointIn{"Breakpoint":{"id":0,"name":"","addr":0,"addrs":null,"file":"/home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go","line":63,"Cond":"","HitCond":"","continue":false,"traceReturn":false,"goroutine":false,"stacktrace":0,"LoadArgs":{"FollowPointers":true,"MaxVariableRecurse":1,"MaxStringLen":64,"MaxArrayValues":64,"MaxStructFields":-1},"LoadLocals":{"FollowPointers":true,"MaxVariableRecurse":1,"MaxStringLen":64,"MaxArrayValues":64,"MaxStructFields":-1},"WatchExpr":"","WatchType":0,"hitCount":null,"totalHitCount":0,"disabled":false}})
2021-10-21T12:52:17+02:00 debug layer=rpc -> *rpc2.CreateBreakpointOut{"Breakpoint":{"id":0,"name":"","addr":0,"addrs":null,"file":"","line":0,"Cond":"","HitCond":"","continue":false,"traceReturn":false,"goroutine":false,"stacktrace":0,"LoadArgs":null,"LoadLocals":null,"WatchExpr":"","WatchType":0,"hitCount":null,"totalHitCount":0,"disabled":false}} error: "could not find file /home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go"
2021-10-21T12:52:17+02:00 debug layer=rpc <- RPCServer.CreateBreakpoint(rpc2.CreateBreakpointIn{"Breakpoint":{"id":0,"name":"","addr":0,"addrs":null,"file":"/home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go","line":84,"Cond":"","HitCond":"","continue":false,"traceReturn":false,"goroutine":false,"stacktrace":0,"LoadArgs":{"FollowPointers":true,"MaxVariableRecurse":1,"MaxStringLen":64,"MaxArrayValues":64,"MaxStructFields":-1},"LoadLocals":{"FollowPointers":true,"MaxVariableRecurse":1,"MaxStringLen":64,"MaxArrayValues":64,"MaxStructFields":-1},"WatchExpr":"","WatchType":0,"hitCount":null,"totalHitCount":0,"disabled":false}})
2021-10-21T12:52:17+02:00 debug layer=rpc -> *rpc2.CreateBreakpointOut{"Breakpoint":{"id":0,"name":"","addr":0,"addrs":null,"file":"","line":0,"Cond":"","HitCond":"","continue":false,"traceReturn":false,"goroutine":false,"stacktrace":0,"LoadArgs":null,"LoadLocals":null,"WatchExpr":"","WatchType":0,"hitCount":null,"totalHitCount":0,"disabled":false}} error: "could not find file /home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go"
2
Error on CreateBreakpoint: could not find file /home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go

文件存在:

gbajson@misio:~$ ls -l /home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go
-rw-r--r-- 1 gbajson gbajson 2961 Oct 21 12:22 /home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go

我已经检查过文件权限,问题不在于文件权限。
我还按照dlv的旧版本的步骤进行了操作:
https://github.com/golang/vscode-go/blob/master/docs/debugging-legacy.md#selecting-legacy-debug-adapter

调试器配置

  • workspace.code-workspace

    "launch": {
    "version": "0.2.0",
    "configurations": [
    {

          "name": "Debug Go",
          "type": "go",
          "request": "launch",
          "mode": "auto",
          "program": "${fileDirname}",
          "debugAdapter": "legacy",
          "env": {},
          "args": [],
          "showLog": true,
          "logOutput": "rpc",
          "trace": "log"
      },
    
  • settings.json

{
    "go.delveConfig": {
        "debugAdapter": "legacy",
    },
}
英文:

I debug Golang application with breakpoints in VS Code.
Debugger complains that can't find file, which exists.

Does anyone of you know how to enable breakpoints for Go application in VS Code?

Debugger logs:

Debuggee is not running. Setting breakpoints without halting.
All cleared
Creating on: /home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go:63
Creating on: /home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go:84
All cleared
All set:[]
SetBreakPointsResponse
2021-10-21T12:52:17+02:00 debug layer=rpc &lt;- RPCServer.CreateBreakpoint(rpc2.CreateBreakpointIn{&quot;Breakpoint&quot;:{&quot;id&quot;:0,&quot;name&quot;:&quot;&quot;,&quot;addr&quot;:0,&quot;addrs&quot;:null,&quot;file&quot;:&quot;/home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go&quot;,&quot;line&quot;:63,&quot;Cond&quot;:&quot;&quot;,&quot;HitCond&quot;:&quot;&quot;,&quot;continue&quot;:false,&quot;traceReturn&quot;:false,&quot;goroutine&quot;:false,&quot;stacktrace&quot;:0,&quot;LoadArgs&quot;:{&quot;FollowPointers&quot;:true,&quot;MaxVariableRecurse&quot;:1,&quot;MaxStringLen&quot;:64,&quot;MaxArrayValues&quot;:64,&quot;MaxStructFields&quot;:-1},&quot;LoadLocals&quot;:{&quot;FollowPointers&quot;:true,&quot;MaxVariableRecurse&quot;:1,&quot;MaxStringLen&quot;:64,&quot;MaxArrayValues&quot;:64,&quot;MaxStructFields&quot;:-1},&quot;WatchExpr&quot;:&quot;&quot;,&quot;WatchType&quot;:0,&quot;hitCount&quot;:null,&quot;totalHitCount&quot;:0,&quot;disabled&quot;:false}})
2021-10-21T12:52:17+02:00 debug layer=rpc -&gt; *rpc2.CreateBreakpointOut{&quot;Breakpoint&quot;:{&quot;id&quot;:0,&quot;name&quot;:&quot;&quot;,&quot;addr&quot;:0,&quot;addrs&quot;:null,&quot;file&quot;:&quot;&quot;,&quot;line&quot;:0,&quot;Cond&quot;:&quot;&quot;,&quot;HitCond&quot;:&quot;&quot;,&quot;continue&quot;:false,&quot;traceReturn&quot;:false,&quot;goroutine&quot;:false,&quot;stacktrace&quot;:0,&quot;LoadArgs&quot;:null,&quot;LoadLocals&quot;:null,&quot;WatchExpr&quot;:&quot;&quot;,&quot;WatchType&quot;:0,&quot;hitCount&quot;:null,&quot;totalHitCount&quot;:0,&quot;disabled&quot;:false}} error: &quot;could not find file /home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go&quot;
2021-10-21T12:52:17+02:00 debug layer=rpc &lt;- RPCServer.CreateBreakpoint(rpc2.CreateBreakpointIn{&quot;Breakpoint&quot;:{&quot;id&quot;:0,&quot;name&quot;:&quot;&quot;,&quot;addr&quot;:0,&quot;addrs&quot;:null,&quot;file&quot;:&quot;/home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go&quot;,&quot;line&quot;:84,&quot;Cond&quot;:&quot;&quot;,&quot;HitCond&quot;:&quot;&quot;,&quot;continue&quot;:false,&quot;traceReturn&quot;:false,&quot;goroutine&quot;:false,&quot;stacktrace&quot;:0,&quot;LoadArgs&quot;:{&quot;FollowPointers&quot;:true,&quot;MaxVariableRecurse&quot;:1,&quot;MaxStringLen&quot;:64,&quot;MaxArrayValues&quot;:64,&quot;MaxStructFields&quot;:-1},&quot;LoadLocals&quot;:{&quot;FollowPointers&quot;:true,&quot;MaxVariableRecurse&quot;:1,&quot;MaxStringLen&quot;:64,&quot;MaxArrayValues&quot;:64,&quot;MaxStructFields&quot;:-1},&quot;WatchExpr&quot;:&quot;&quot;,&quot;WatchType&quot;:0,&quot;hitCount&quot;:null,&quot;totalHitCount&quot;:0,&quot;disabled&quot;:false}})
2021-10-21T12:52:17+02:00 debug layer=rpc -&gt; *rpc2.CreateBreakpointOut{&quot;Breakpoint&quot;:{&quot;id&quot;:0,&quot;name&quot;:&quot;&quot;,&quot;addr&quot;:0,&quot;addrs&quot;:null,&quot;file&quot;:&quot;&quot;,&quot;line&quot;:0,&quot;Cond&quot;:&quot;&quot;,&quot;HitCond&quot;:&quot;&quot;,&quot;continue&quot;:false,&quot;traceReturn&quot;:false,&quot;goroutine&quot;:false,&quot;stacktrace&quot;:0,&quot;LoadArgs&quot;:null,&quot;LoadLocals&quot;:null,&quot;WatchExpr&quot;:&quot;&quot;,&quot;WatchType&quot;:0,&quot;hitCount&quot;:null,&quot;totalHitCount&quot;:0,&quot;disabled&quot;:false}} error: &quot;could not find file /home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go&quot;
2
Error on CreateBreakpoint: could not find file /home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go

File exists:

gbajson@misio:~$ ls -l /home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go
-rw-r--r-- 1 gbajson gbajson 2961 Oct 21 12:22 /home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go

I already checked that it's not a problem with file permissions.
I also followed up the procedure for the lagacy version of dlv:
https://github.com/golang/vscode-go/blob/master/docs/debugging-legacy.md#selecting-legacy-debug-adapter

Debugger configuration

  • workspace.code-workspace

     &quot;launch&quot;: {
     &quot;version&quot;: &quot;0.2.0&quot;,
     &quot;configurations&quot;: [
     	{
    
     		&quot;name&quot;: &quot;Debug Go&quot;,
     		&quot;type&quot;: &quot;go&quot;,
     		&quot;request&quot;: &quot;launch&quot;,
     		&quot;mode&quot;: &quot;auto&quot;,
     		&quot;program&quot;: &quot;${fileDirname}&quot;,
     		&quot;debugAdapter&quot;: &quot;legacy&quot;,
     		&quot;env&quot;: {},
     		&quot;args&quot;: [],
     		&quot;showLog&quot;: true,
     		&quot;logOutput&quot;: &quot;rpc&quot;,
     		&quot;trace&quot;: &quot;log&quot;
     	},
    
  • settings.json

{
    &quot;go.delveConfig&quot;: {
        &quot;debugAdapter&quot;: &quot;legacy&quot;,
    },

答案1

得分: 3

我找到了问题。VS Code对符号链接的处理不好。
当我在VS Code中设置一个项目的真实路径时,调试器开始正常工作。

gbajson@misio:~$ realpath /home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go
/storage/amoje/Sync/clickr/clickr-node-api/clickr-node-api.go

这个问题也在这里描述:https://github.com/golang/vscode-go/issues/1677

英文:

I found the problem. VS Code doesn't handle symbolic links well.
When I set up a project in VS Code in real path debugger started to work properly.

gbajson@misio:~$ realpath /home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go
/storage/amoje/Sync/clickr/clickr-node-api/clickr-node-api.go

This problem is also described in: https://github.com/golang/vscode-go/issues/1677

答案2

得分: 3

我想在这里分享我的解决方案。在阅读了一些 github问题 关于vs-code-go的问题后,似乎问题是由于remotePathsubstitutePath引起的。经过几次尝试,将remotePath设置为&quot;&quot;最终解决了问题。

    "version": "0.2.0",
    "configurations": [
        {
            "name": "Connect to server",
            "type": "go",
            "request": "attach",
            "mode": "remote",
            "remotePath": "",
            "port": 40000,
            "host": "<host>",
        }
    ]
}
英文:

Wanted to share my solution here.
After reading a few github issues vs-code-go, seems like the issue was a result of remotePath or substitutePath. After a few attempts, setting remotePath to &quot;&quot; finally did the trick.

    &quot;version&quot;: &quot;0.2.0&quot;,
    &quot;configurations&quot;: [
        {
            &quot;name&quot;: &quot;Connect to server&quot;,
            &quot;type&quot;: &quot;go&quot;,
            &quot;request&quot;: &quot;attach&quot;,
            &quot;mode&quot;: &quot;remote&quot;,
            &quot;remotePath&quot;: &quot;&quot;,
            &quot;port&quot;: 40000,
            &quot;host&quot;: &quot;&lt;host&gt;&quot;,
        }
    ]
}

答案3

得分: 1

这是对gbajson的回答的后续内容。

对我来说,符号链接是个问题。使用launch.json中的以下配置解决了这个问题,同时也允许我保留符号链接:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${workspaceFolder}/cmd/server/main.go",
            "envFile": "${workspaceFolder}/.env",
            "substitutePath": [
                {
                    "from": "/home/me/go-workspaces",
                    "to": "/data/projects/workspaces"
                }
            ]
        }
    ]
}

具体来说,就是"substitutePath"部分。

我的工作空间目录实际上是从"/home/me/go-workspaces"符号链接到真实目录"/data/projects/workspaces"。

参考:https://github.com/golang/vscode-go/blob/master/docs/debugging.md#debugging-symlink-directories

英文:

This is a follow-on from the answer by gbajson.

Symbolic links were the problem for me. Using this configuration in launch.json resolved the issue, while allowing me to keep my symlinks:

{
    &quot;version&quot;: &quot;0.2.0&quot;,
    &quot;configurations&quot;: [
        {
            &quot;name&quot;: &quot;Launch&quot;,
            &quot;type&quot;: &quot;go&quot;,
            &quot;request&quot;: &quot;launch&quot;,
            &quot;mode&quot;: &quot;auto&quot;,
            &quot;program&quot;: &quot;${workspaceFolder}/cmd/server/main.go&quot;,
            &quot;envFile&quot;: &quot;${workspaceFolder}/.env&quot;,
            &quot;substitutePath&quot;: [
                {
                    &quot;from&quot;: &quot;/home/me/go-workspaces&quot;,
                    &quot;to&quot;: &quot;/data/projects/workspaces&quot;
                }
            ]
        }
    ]
}

Specifically, it is the "substitutePath" part.

My workspace directory was actually symlinked from "/home/me/go-workspaces" to the real directory "/data/projects/workspaces".

See: https://github.com/golang/vscode-go/blob/master/docs/debugging.md#debugging-symlink-directories

答案4

得分: 0

你可以使用 "sudo ps aux | fgrep {你的进程名称}" 命令来检查你的进程。如果它启动了多个进程,可能需要更改进程的启动模式,而不是守护进程模式。

英文:

You can use "sudo ps aux | fgrep {your process name}" to check your process.If it start more than 1 process, maybe you should change your process start mode without daemon process.

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

发表评论

匿名网友

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

确定