vscode debug golang code

前提

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

命令行debug的方式

  1. 切换至对应目录
    root@helious:/usr/lib/go-1.18/src/io# go test -c  -gcflags='all=-N -l'  -o ./__debug_bin
    
  2. dlv debug
    dlv exec ./__debug_bin
    
    b funcName
    
    
  3. 直接使用dlv
    dlv test path_to_file -- test.v --test.run TestFuncName
    dlv test io/pipe_test.go -- -test.v --test.run ^TestPipeWriteClose$
    

使用VScode进行debug

前提:

  1. 安装vscode go extension

  2. 设置launch.json定义debug行为的启动方式

    {
    // 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": "Launch golang debug function",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "{workspaceFolder}/{relativeFileDirname}",
            "console": "internalConsole",
            "args": [
                "--",
                "-test.v",
                "-test.run",
                "^FunctionName$",
            ]
        }
    ]
    }
    

注意,如果你和我一样在调试时发现无论如何设置断点,vscode总是断点失败,像是未生效一般。

首先,你需要确保你的程序没有bug,即按照命令行debug的方式是可以正常运行的,那么这是你需要打开vscode的debug输出

修改launch.json,增加如下内容

"configurations": [
        {
            ....
            "console": "internalConsole",
            "logOutput": "dap",
            "showLog": true,
            "trace": "verbose",
        }
    ]

这里我想debug的文件是/usr/lib/go-1.18/src/io/pipe_test.go

vscode输出如下

2023-05-28T12:14:25+08:00 debug layer=dap [-> to client]
{"seq":0,"type":"response","request_seq":4,"success":true,"command":"setBreakpoints","body":{"breakpoints":
[{"verified":false,"message":"could not find file /usr/lib/go-1.18/src/io/pipe_test.go","source":{}},
{"verified":false,"message":"could not find file /usr/lib/go-1.18/src/io/pipe_test.go","source":{}},
{"verified":false,"message":"could not find file /usr/lib/go-1.18/src/io/pipe_test.go","source":{}},
{"verified":false,"message":"could not find file /usr/lib/go-1.18/src/io/pipe_test.go","source":{}},
{"verified":false,"message":"could not find file /usr/lib/go-1.18/src/io/pipe_test.go","source":{}},
{"verified":false,"message":"could not find file /usr/lib/go-1.18/src/io/pipe_test.go","source":{}},
{"verified":false,"message":"could not find file /usr/lib/go-1.18/src/io/pipe_test.go","source":{}}]}}

有些奇怪,看起来像是vscode找不到需要debug的文件,所以导致无法断点。一番搜索后,找到了这个VSCode Breakpoint not hitting/stopping when run in debugger mode – Golang · Issue #2417

这里提到导致问题的原因可能是/usr/lib/go-1.18/src/io/pipe_test.go 是一个软连接目录,所以导致vscode无法找到对应的文件,也就无法下断点。

那我们确认下

root@helious:/usr/lib/go-1.18# pwd
/usr/lib/go-1.18
root@helious:/usr/lib/go-1.18# ls -l
total 12
-rw-r--r-- 1 root root    8 Jul 30  2022 VERSION
lrwxrwxrwx 1 root root   23 Apr 13  2022 api -> ../../share/go-1.18/api
drwxr-xr-x 2 root root 4096 Nov 13  2022 bin
lrwxrwxrwx 1 root root   24 Apr 13  2022 misc -> ../../share/go-1.18/misc
drwxr-xr-x 4 root root 4096 May  4  2022 pkg
lrwxrwxrwx 1 root root   23 Apr 13  2022 src -> ../../share/go-1.18/src
lrwxrwxrwx 1 root root   24 Apr 13  2022 test -> ../../share/go-1.18/test
root@helious:/usr/lib/go-1.18# realpath ./src
/usr/share/go-1.18/src
root@helious:/usr/lib/go-1.18# 

这里我们可以看到,golang的默认安装源码路径实际是在/usr/share/go-1.18/src路径下

那对应的解决办法是在launch.json中添加

"substitutePath": [
        {
                    "from": "/usr/lib/go-1.18/src",
                    "to": "/usr/share/go-1.18/src"
        }
]

结论

  1. 如果你发现无论通过Visual Code 界面怎样断点都不生效,但是通过命令行方式可以正常Debug,那么可能是因为你当前的源码目录是一个相对路径,改为绝对路径即可
  2. 有任何问题打开Debug日志可以帮助分析

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注