首页 > 解决方案 > VSCode、WSL 和 Docker - 使用 gdbserver 进行调试

问题描述

我想用 VSCode 调试 C++ 应用程序,在 WSL 内部的 docker 容器中运行。

我目前的设置:

我已经在容器上运行了 gdbserver:

gdbserver 172.17.0.2:9999 /path/to/app

如何将 VScode 附加到 gdbserver,在 WSL 上的容器中运行?

这是我已经尝试过的方法以及结果:

  1.      {
             "name": "Simple Native Plugin",
             "type": "gdb",
             "request": "attach",
             "executable": "./app",
             "target": "172.17.0.2:9999",
             "cwd": "${workspaceRoot}",
             "remote": true
         },
    
    

结果:

Could not start debugger process, does the program exist in filesystem?
Error: spawn gdb ENOENT
  1.   {
          "name": "(gdb) Attach - WSL - Native Plugin",
          "type": "gdb",
          "request": "attach",
          "target" : "172.17.0.2:9999",
          "remote" : true,
          "executable": "${workspaceFolder}/app",
          "gdbpath": "/usr/bin/gdbserver",
          "cwd": "${workspaceRoot}",
    
      },
    

结果:

undefined/bin/bash: line 0: exec: -q: invalid option
exec: usage: exec [-cl] [-a name] [command [arguments ...]] [redirection ...]
During startup program exited with code 2.
Exiting
  1.  {
          "name": "(gdb) Launch - WSL - CPPdbg Plugin",
          "type": "cppdbg",
          "request": "launch",
          "remote" : true,
          "miDebuggerServerAddress": "172.17.0.2:9999",
          "miDebuggerPath": "/usr/bin/gdbserver",
          "program": "${workspaceFolder}/app",
          "args": [],
          "stopAtEntry": false,
          "cwd": "${workspaceRoot}",
          "externalConsole": true,
          "linux": {
              "MIMode": "gdb",
          },
          "windows": {
              "MIMode": "gdb"
          }
      }
    

结果:

Starting: "/usr/bin/gdbserver" --interpreter=mi
Unknown argument: --interpreter=mi
"/usr/bin/gdbserver" exited with code 1 (0x1).

标签: visual-studio-codevscode-debuggervscode-remotegdbserver

解决方案


你的第二次尝试几乎是正确的。尝试输入路径 togdb而不是gdbserverin gdbpath


推荐阅读