【问题标题】:Compounds debugging in VS Code with delayed launch在 VS Code 中使用延迟启动进行复合调试
【发布时间】:2017-08-09 14:46:49
【问题描述】:

我正在尝试在 VS Code 的调试器中启动多个需要相互通信的程序,并创建了一个带有启动每个可执行文件的化合物的 launch.json。这些程序同时启动,并且都尝试同时连接到主机。 VS Code 中是否有任何方法可以在每个可执行文件的启动之间显式设置某种时间延迟,比如 250 毫秒左右?

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Host",
      "type": "cppdbg",
      "request": "launch",
      "program": "/home/user/build/bin/host",
      "args": [],
      "stopAtEntry": false,
      "cwd": "/home/user/build/bin",
      "environment": [],
      "externalConsole": true,
      "linux": {
        "MIMode": "gdb",
        "setupCommands": [
          {
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
          }
        ]
      }
    },
    {
      "name": "Node A",
      "type": "cppdbg",
      "request": "launch",
      "program": "/home/user/build/bin/Node_A",
      "args": ["ArgA", "ArgB", "ArgC"],
      "stopAtEntry": false,
      "cwd": "/home/user/build/bin",
      "environment": [],
      "externalConsole": true,
      "linux": {
        "MIMode": "gdb",
        "setupCommands": [
          {
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
          }
        ]
      }
    },
    {
      "name": "Node B",
      "type": "cppdbg",
      "request": "launch",
      "program": "/home/user/build/bin/Node_B",
      "args": ["ArgA", "ArgB", "ArgC"],
      "stopAtEntry": false,
      "cwd": "/home/user/build/bin",
      "environment": [],
      "externalConsole": true,
      "linux": {
        "MIMode": "gdb",
        "setupCommands": [
          {
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
          }
        ]
      }
    }
  ],
  "compounds": [
    {
      "name": "System",
      "configurations": ["Host", "Node A", "Node B"]
    }
  ]
}

【问题讨论】:

    标签: debugging visual-studio-code


    【解决方案1】:

    是的,您可以添加一个预启动任务,该任务将休眠 x 秒。

    假设您在 Node.js 上有一个客户端和服务器,并且服务器 db 连接需要更长的时间来加载,这会导致客户端出现问题。

    在 vscode 上延迟客户端调试器在 Mac OS X 上会像这样工作

    首先在与launch.json文件相同的文件夹中创建一个名为tasks.json的任务,它将在启动客户端之前构建一个shell命令。

    {
      // See https://go.microsoft.com/fwlink/?LinkId=733558
      // for the documentation about the tasks.json format
      "version": "2.0.0",
      "tasks": [
        {
          "label": "Sleepdelay",
          "type": "shell",
          "command": "sleep 6",
          "windows": {
            "command": "ping 127.0.0.1 -n 6 > nul"
          },
          "group": "none",
          "presentation": {
            "reveal": "silent",
            "panel": "new"
          }
        }
      ]
    }
    

    现在将以下预任务添加到您的 launch.json 文件中以调用该任务

    {
      "configurations": [
        {
          "type": "chrome",
          "request": "launch",
          "name": "Client",
          "url": "http://localhost:9090",
          "webRoot": "${workspaceFolder}/client/src",
          "breakOnLoad": true,
          "sourceMapPathOverrides": {
            "webpack:///./src/*": "${webRoot}/*"
          },
          "preLaunchTask": "Sleepdelay"
          //"runtimeExecutable": "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
        },
        {
          "type": "node",
          "request": "launch",
          "name": "Server",
          "program": "${workspaceFolder}/server/server.js",
          "envFile": "${workspaceFolder}/server/.env",
          "cwd": "${workspaceFolder}/server/"
        }
      ],
      "compounds": [
        {
          "name": "Server/Client",
          "configurations": ["Server", "Client"]
        }
      ]
    }
    

    sleep 命令在 Linux 和 MAC OS X 上可用。对于 Windows,只需使用这个 hack 代替它:

    ping 127.0.0.1 -n 6 > nul

    现在您有一个简单的方法可以在服务器之前延迟启动客户端。

    【讨论】:

      猜你喜欢
      • 2019-05-30
      • 1970-01-01
      • 2019-06-26
      • 1970-01-01
      • 2022-01-11
      • 2019-06-06
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      相关资源
      最近更新 更多