[已解决]如何通过VSCode调试Golang代码

[已解决]如何通过VSCode调试Golang代码

  • 明确说明你想要做什么 我想通过调试(主要是为了在运行时检查一些值)一个开源项目[1]的代码来学习它。但是我在VSCode中设置调试环境时遇到了问题。我想我对VSCode和Golang都不太熟悉,所以非常感谢任何建议。

  • 陈述问题并报告确切的错误信息或程序行为 克隆项目以及设置断点等都没有问题。然而,该项目是一个客户端/服务器架构。所以我不确定我设置的调试方式是否正确。

    1. 点击 调试和运行 按钮,这会创建一个内容如下的 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": "Launch Package",
                  "type": "go",
                  "request": "launch",
                  "mode": "auto",
                  "program": "${fileDirname}"
              }
          ]
      }
      
    2. VSCode的调试控制台打印:

      Starting: /home/jason/go/bin/dlv dap --check-go-version=false --listen=127.0.0.1:41767 --log-dest=3 from /home/jason/workspace/github.com/chlin501/serf/.vscode
      DAP server listening at: 127.0.0.1:41767
      
  • 问题 我不知道如何触发整个流程,以便我能在运行时观察值。通常这个项目的流程是,例如通过 ./bin/serf agent -node=foo -bind=127.0.0.1:5000 -rpc-addr=127.0.0.1:7373 启动一个服务器进程;然后发出一个客户端命令,例如 ./bin/serf join 127.0.0.1:5001。现在,如果我在VSCode终端中运行这些,调试环境中什么也不会发生,不像简单的教程那样可以在运行时观察变量和调用堆栈值。我应该如何配置才能实现这个目标?谢谢

[1]. GitHub - hashicorp/serf: Service orchestration and management tool.


更多关于[已解决]如何通过VSCode调试Golang代码的实战教程也可以访问 https://www.itying.com/category-94-b0.html

2 回复

看起来应该使用 Attach to Process 功能。

步骤:

  1. 构建二进制文件
  2. 启动服务器进程,例如 ./bin/serf agent ...
  3. 点击 Run and Debug,选择 Attach to Process,并使用以下 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": "Attach to Process",
                "type": "go",
                "request": "attach",
                "mode": "local",
                "processId": 47854 // 此值可通过 ps -ef | grep serf 命令找到
            }
        ]
    }
    
  4. 在左侧面板的 Run and Debug 部分点击附加(三角形)按钮(或按 F5)。当进程附加成功后,变量和调用堆栈信息将显示在左侧面板。
  5. 在 vscode 的 TERMINAL 中执行 ./bin/serf join ... 命令。现在,运行时信息将在左侧的变量和调用堆栈面板中更新。

更多关于[已解决]如何通过VSCode调试Golang代码的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


针对调试客户端/服务器架构的Go项目,您需要调整调试配置以启动正确的可执行文件并传递必要的参数。以下是具体步骤和示例配置:

  1. 修改 launch.json 配置:将 program 指向项目的 main 包路径,并使用 args 传递命令行参数。例如,对于 serf 项目,调试服务器进程的配置如下:
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug Serf Server",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${workspaceFolder}/cmd/serf",
            "args": [
                "agent",
                "-node=foo",
                "-bind=127.0.0.1:5000",
                "-rpc-addr=127.0.0.1:7373"
            ],
            "cwd": "${workspaceFolder}"
        }
    ]
}
  1. 调试客户端进程:如果需要调试客户端命令(如 serf join),可以创建另一个调试配置:
{
    "name": "Debug Serf Client",
    "type": "go",
    "request": "launch",
    "mode": "auto",
    "program": "${workspaceFolder}/cmd/serf",
    "args": [
        "join",
        "127.0.0.1:5001"
    ],
    "cwd": "${workspaceFolder}"
}
  1. 设置断点并启动调试

    • 在代码中设置断点(例如在 cmd/serf/main.go 或相关业务逻辑文件中)。
    • 在VSCode的调试侧边栏中选择对应的配置(如“Debug Serf Server”)。
    • F5 启动调试,程序将在断点处暂停,此时可以检查变量值、调用堆栈等信息。
  2. 同时调试多个进程:如果需要同时调试服务器和客户端,可以启动两个独立的VSCode窗口,分别加载项目并运行不同的调试配置。或者使用 dlv 命令行工具附加到已运行的进程:

    • 首先正常启动服务器进程(例如在终端中运行命令)。
    • 然后创建调试配置并附加到该进程:
{
    "name": "Attach to Serf Process",
    "type": "go",
    "request": "attach",
    "mode": "local",
    "processId": 12345  // 替换为实际的进程ID
}
  1. 使用条件断点:如果需要在特定条件下触发断点(例如当客户端发送特定请求时),可以在VSCode中设置条件断点。右键点击断点图标,选择“编辑断点”,然后输入Go表达式(如 nodeName == "foo")。

通过以上配置,您可以在VSCode中完整调试客户端/服务器交互流程,实时观察变量状态和程序执行路径。

回到顶部