debugging - VS 代码调试 : Is it possible to supply process-id 'pgrep -x "$myprog "' for use in launch.json (variable "processId") when attach to process-id?

标签 debugging visual-studio-code compilation

我注意到 node.js 有一个调试器“自动附加”功能,如果可能的话我想使用它。但是,我不认为我可以在重新编译我的程序后使用“自动附加”来自动启动调试器。我使用 C/C++/Fortran 使用 makefile(和类似文件)编译我的程序,所以在运行“make”后我得到一个普通的(linux)可执行文件。然后我执行那个二进制文件,并且经常想在这之后立即调试它。我将完整的二进制路径设置为环境变量“$myprog=/home/user/my-dev/bin/myApp”。

所以我所做的是使用“附加到进程”运行调试,每次我必须在下拉列表中找到正确的进程,但我每天、每小时都这样做太多次,它变得乏味并且想要更智能、“更自动”的东西:

我想修改我的 launch.json,而不是“自动附加”(仅适用于 node.js,据我所知),以便它自动提取进程 ID,例如使用shell 命令:'pgrep -x "$myprog"' 并且可以选择将其绑定(bind)到键盘快捷键...我猜行 ""processId": "${command:pickProcess}","需要已修改,请参见下面的示例配置:

 {
            "name": "(gdb) Attach (any)",
            "type": "cppdbg",
            "request": "attach",
            "program": "/home/user/my-dev/bin/myApp",
            "processId": "${command:pickProcess}",
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb"
        },

是否可以修改 launch.json 文件,以便 VS 代码理解变量“processId”应该替换为来自以下的 shell 输出:'pgrep -x "$myprog"'(显然这个 PID 会在每次编译后发生变化和执行要调试的程序)?如果是这样,我想也可以将它绑定(bind)到键盘快捷键......任何人都知道如何实现这一点?

最佳答案

是的,使用 Tasks Shell Input Extension 是可能的.它提供了一个 vscode 命令,可以返回一个 shell 命令的结果。

我工作区的相关部分如下所示。

"launch": {
    "configurations": [
        {
            ...
            "processId": "${input:FindVivadoPID}",
            ...
        }
    ],
    "inputs": [
        {
          "id": "FindVivadoPID",
          "type": "command",
          "command": "shellCommand.execute",
          "args": {
            "command": "pgrep 'ecold.*lnx64.g/vivado' -f",
            "description": "Select your Vivado PID",
            "useFirstResult": true,
          }
        }
    ]
}

每当我运行 Start Debugging 时,它会自动找到相关进程并附加。

This Stack Overflow 问题更笼统地讨论了扩展。

关于debugging - VS 代码调试 : Is it possible to supply process-id 'pgrep -x "$myprog "' for use in launch.json (variable "processId") when attach to process-id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65723608/

相关文章:

Eclipse Debug View 在全屏模式下在断点处弹出

visual-studio-2010 - Visual Studio 2010 调试器正确构建 - 编译器 pdb 和链接器 pdb 不同步?

c++ - 包含 OpenCV 时的 Visual Studio Code "cannot open source file"错误

java - 准备好发布 Java 程序

java - 如何在运行时加载 .class 文件以与 JUnitCore.runclasses() 一起使用

.net - 如何从 Visual Studio 中的另一个解决方案调试项目?

Javascript 与 IE8 - 预期的标识符、字符串或数字

vue.js - 如何让 ESLint 在 Visual Studio Code 中正确格式化和检查规则?

javascript - VS Code - 使用 TextMate 突出显示一些变量

类 vector 的 C++ 编译问题