go - 如何使用 VSCode 远程调试 Go 代码

标签 go visual-studio-code vscode-debugger delve

我在需要调试的 docker 容器中运行一个进程。该过程在 docker 的入口点通过dlv debug /go/src/path/to/package --headless --listen=:2345 --log以便稍后在 VSCode 中启用调试。

docker 容器通过以下方式启动docker run --rm -it -p 2345:2345 my_image:tag .注意 delve 的端口是公开的。

在 VSCode 中,我定义了 launch.json如下:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach remote",
            "type": "go",
            "request": "attach",
            "mode": "remote",
            "port": 2345,
            "host": "127.0.0.1",
            "apiVersion": 1
        }
    ]
}

启动“附加远程”VSCode 调试配置后,我得到
enter image description here

它不是很清楚,但该 UI 使我相信我现在已连接到远程 headless 调试器并准备进行调试。我定义了一个断点,我知道它会被我可以发送远程进程的请求击中。我发出那个请求,得到结果,断点一直没有命中,说明我还没有实现远程调试。

我的 VSCode“附加远程”配置有问题吗?我可以用 dlv connect :2345 进行命令行调试并且实际上调试远程进程就好了,这表明 headless 服务器可以正常工作。我宁愿在 VSCode 中使用源代码进行调试。

最佳答案

latest beta of vscode-go 再试一次(2020年4月)(2020年4月之后的任何时候,最新的官方vscode-go版本就足够了)
Microsoft/vscode-go issue 2010包括来自 Ramya Rao 的确认:

The fix from #3108 is available in the latest beta version of this extension. Please do try and share feedback
The latest version of the extension now has the fix to this issue


和:

I can confirm that I am able to hit breakpoints now using AWS SAM to launch a linux container with delve and go binaries compiled from Windows.

For anyone still having this problem (like I was before I edited this comment), take care that the "remotePath" element of your launch.json is the absolute path to the source files as compiled on your local system (not the container).
As implied above - it's the absolute local path that is added to the DWARF compilation unit file table when you compile the binary.

关于go - 如何使用 VSCode 远程调试 Go 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59652312/

相关文章:

multithreading - 当 goroutine 在 I/O 上阻塞时,调度程序如何识别它已停止阻塞?

javascript - VS Code函数去下一个空间

powershell - 即使将 Git Bash 设置为默认值,VSCode 仍会在启动时继续打开 powershell

python - 在 VS Code 中调试时如何提供输入数据?

postgresql - 创建或更新未在GORM中返回更新后的值

go - 软件包handlers/gql.go不在GOROOT中(/usr/local/go/src/handlers/gql.go)

go - 为什么 atomic.StoreUint32 优于 sync.Once 中的正常分配?

javascript - 如何在 Visual Studio Code 中浏览 Javascript 代码?

c++ - 为什么当从 VSCode 使用 GDB 时遇到断点时,GDB 会使我的程序崩溃?

python - 如何修复此 'cmd' 无法识别错误以在 VSCode 中进行调试?