【问题标题】:Stall when debugging with gdbserver in VSCode - "The preLaunchTask 'docker gdb' cannot be tracked."在 VSCode 中使用 gdbserver 进行调试时停止 - “无法跟踪 preLaunchTask 'docker gdb'。”
【发布时间】:2018-06-18 13:33:01
【问题描述】:

总结

我正在尝试在 docker 映像 (Ubuntu) 中调试 C++ 程序,同时在我的主机系统 (OS X) 上使用 VSCode 作为 IDE。经过对 gdbserver 和 VSCode 任务的各种修改,我现在能够成功运行调试器,但是每次启动调试会话时,VSCode 都会挂起 10 秒,然后报告错误消息:

“无法跟踪 preLaunchTask 'docker gdb'。”

如果我点击这个错误,我可以正常调试,但是每次调试时这 10 秒的等待非常令人沮丧。

详情

我的 Docker 镜像是通过以下方式启动的,所以我的源代码安装在“app”目录中。安全设置是我在 Stack Overflow 其他地方找到的,需要允许 gdbserver:

docker run --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -it -v "$(pwd):/app" -w "/app" -p 9091:9091 {imageName} /bin/bash

当我在主机上启动调试会话时,我使用这个启动命令,将本地 gdb 连接到 docker gdbserver,并运行启动前任务:

{
"version": "0.2.0",
"configurations": [
    {
        "name": "Remote unit test",
        "type": "cppdbg",
        "request": "launch",
        "program": "./tests/ConfigurationTest.cpp_TestRunner",
        "miDebuggerServerAddress": "localhost:9091",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceRoot}",
        "environment": [],
        "externalConsole": true,
        "MIMode": "gdb",
        "preLaunchTask": "docker gdb"
    }
]
}

这是启动前任务的定义;它使用 docker exec 杀死任何现有的 gdbserver 并在相关的可执行文件上启动一个新的:

{
"version": "2.0.0",
"tasks": [
    {
        "label":"docker gdb",
        "command": "docker exec {containerName} /bin/bash -c \"pkill gdbserver; gdbserver localhost:9091 ./tests/ConfigurationTest.cpp_TestRunner\"",
        "isBackground": true,
        "type": "shell"
        }
    ]
}

当我启动调试会话时,我会立即得到以下输出,这是预期的:

进程 ./tests/ConfigurationTest.cpp_TestRunner 创建; pid = 1167

监听 9091 端口

此时,gdbserver 已准备就绪,我希望 VSCode 启动它的 gdb。但相反,VSCode 会等待 10 秒钟,然后弹出一个对话框,说“无法跟踪 preLaunchTask 'docker gdb'。”。如果我单击“仍然调试”,调试会话将按预期恢复,并且表现良好。

我尝试了什么

10 秒的等待时间听起来与https://github.com/Microsoft/vscode/issues/37997 非常相似,所以我尝试使用带有'activeOnStart:true' 的problemMatcher,正如那里所建议的那样。这没有效果。

我认为问题可能是 docker exec 命令在前台运行,而 VSCode 正在等待它返回,所以我尝试使用 -d 执行 docker exec (分离模式,在后台运行) ,或者只是在 docker 命令的末尾添加一个“&”。同样,没有效果。

谁能建议我可以做些什么来摆脱这个烦人的 10 秒等待?

非常感谢。

【问题讨论】:

    标签: debugging docker visual-studio-code gdbserver vscode-tasks


    【解决方案1】:

    我今天遇到了同样的问题,我试图用调试器运行 Mocha 测试,而 vscode 正在等待调试器完成 preLaunch 任务,这与我所需要的相反——任务暴露了调试器,所以我需要它在“后台”中运行——不过,这个配置为我修复了它。

    launch.json

    {
        "version": "0.2.0",
        "configurations": [
        {
          "type": "node",
          "request": "attach",
          "name": "Docker: Attach to Mocha",
          "port": 5858,
          "address": "localhost",
          "localRoot": "${workspaceFolder}/server",
          "remoteRoot": "/usr/src/app/server",
          "protocol": "inspector",
          "preLaunchTask": "mocha-docker-debug"
        }
        ]
    }
    

    tasks.json

    {
      // See https://go.microsoft.com/fwlink/?LinkId=733558
      // for the documentation about the tasks.json format
      "version": "2.0.0",
      "tasks": [
          {
            "label": "mocha-docker-debug",
            "type": "shell",
            "command": "docker exec up-ibe-server-node npm run test-debug",
            "group": "test",
            "isBackground": true,
            "presentation": {
              "reveal": "never"
            },
            "problemMatcher": {
              "owner": "mocha",
              "fileLocation": "relative",
              "pattern": [
                {
                  "regexp": "^not\\sok\\s\\d+\\s(.*)$"
                },
                {
                  "regexp": "\\s+(.*)$",
                  "message": 1
                },
                {
                  "regexp": "\\s+at\\s(.*):(\\d+):(\\d+)$",
                  "file": 1,
                  "line": 2,
                  "column": 3
                }
              ],
              "background": {
                  "activeOnStart": true,
                  "beginsPattern":{
                      "regexp": "mocha*"
                  },
                  "endsPattern":{
                      "regexp": "Debugger listening*"
                  }
              },
            }
          }
      ]
    }
    

    这里要注意的主要是isBackground 标志、beginsPatternendsPattern 正则表达式,这告诉“父”启动任务,当 xxx 已从“子”打印到控制台时,它已完成并且vscode 可以继续执行实际任务。

    这可能不是您在运行 C++ 应用时遇到的确切问题,但我认为原因相同。

    【讨论】:

    • 拥有patternbackground 参数也很重要,否则它将不起作用。我必须添加填充符 pattern 类似 "pattern": [ { "regexp": ".*", "file": 1, "location": 2, "message": 3 } ], 以确保 background beginsPatternendsPattern 被使用。
    【解决方案2】:

    我找到了一个非常实用的解决方案。

    我使用以下 preLaunchTask 不需要 isBackground 和正则表达式模式(检测任务执行的开始和结束),只需将 gdbserver 作为后台命令/作业启动并结合一点睡眠:

    {
                "label": "Start GDB remote debugger",
                "type": "shell",
                "options": {
                    "cwd": "${workspaceRoot}/"
                },
                "presentation": {
                    "echo": true,
                    "reveal": "always",
                    "focus": false,
                    "panel": "shared",
                    "revealProblems": "onProblem"
                },
                "command": "clear;./ssh.py 'cd /opt/bin/;(nohup gdbserver localhost:6666 myapp &);sleep 1'",
                "problemMatcher": []
            },
    

    整个gdbserver配置可以在here找到

    根据我的经验,即使在通过 vpn 进行慢速连接时,它也很稳定。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-25
      • 2013-05-15
      • 2020-09-25
      • 2015-06-13
      • 1970-01-01
      • 2014-08-31
      • 1970-01-01
      • 2020-03-23
      相关资源
      最近更新 更多