在 VS Code 中使用 GDB 集成时如何在“launch.json”配置中设置环境变量

在 VS Code 中使用 GDB 集成时如何在“launch.json”配置中设置环境变量

我在 vscode 中的设置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": "(gdb) Launch 25.0_regeragpu",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/25.0_regeragpu/Blazer/MercuryImageComputer/KT/leaf/M31/BrightField/bin/LeafStandalone/Debug/12/LeafStandAlone.x86-64",
            "args": ["-noForcedPatches","${workspaceFolder}/virgo_algo_preq/JobDump/JobInfo_108"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}/25.0_regeragpu/Blazer/MercuryImageComputer/KT/leaf/M31/BrightField/bin/LeafStandalone/Debug/12",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description":  "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

此设置的目的是在 vscode 中使用 GDB 集成。特别是,我指定了以下对,以确保在调试时继承 vscode 终端的 bash 会话中设置的所有环境变量:"externalConsole": false。我还注意到environment配置中有一对命名的。据我了解,如果通过 vscode 启动一个新的 bash 会话进行调试,那么所有环境变量都应该在这environment对中指定。为了尝试在调试时启动新的 bash 选项,我修改了配置文件:

{
    // 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": "(gdb) Launch 25.0_regeragpu",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/25.0_regeragpu/Blazer/MercuryImageComputer/KT/leaf/M31/BrightField/bin/LeafStandalone/Debug/12/LeafStandAlone.x86-64",
            "args": ["-noForcedPatches","${workspaceFolder}/virgo_algo_preq/JobDump/JobInfo_108"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}/25.0_regeragpu/Blazer/MercuryImageComputer/KT/leaf/M31/BrightField/bin/LeafStandalone/Debug/12",
            "environment": [{"CUDA_VISIBLE_DEVICES":"0,1,2,3","CUDA_DEVICE_ORDER":"PCI_BUS_ID"}],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description":  "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

特别是,可以看出我指定了以下对:"environment": [{"CUDA_VISIBLE_DEVICES":"0,1,2,3","CUDA_DEVICE_ORDER":"PCI_BUS_ID"}]"externalConsole": true。然而,当我开始调试时,vscode的调试工具永远处于准备状态,从未进入真正的调试会话。如果我想在vscode中新的控制台中调试,应该如何设置环境变量?

答案1

这是一个例子,你在环境堵塞。

    {
// 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": "g++ - (GDB 9.2) Build and debug active file with RepoCodeInspection",
        "type": "cppdbg",
        "request": "launch",
        "program": "${fileDirname}/bin/${fileBasenameNoExtension}",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [
            {
                "description": "same as commands below by using 'setenv ...",
                "info": "cant debug b/c of libBase/libRecipe now requiring dependency to boost for stacktrace dumps",
                "name": "LD_LIBRARY_PATH",
                "value": "/libs/:./"
            }
        ],
        "externalConsole": false,
        "MIMode": "gdb",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ],
        "logging": {
            "trace": false,
            "traceResponse": false
        },
        "preLaunchTask": "RepoCodeInspection",
        
    },
    {
        "name": "g++ - (GDB 9.2) Attach to a running process",
        "type": "cppdbg",
        "request": "attach",
        "processId":"${command:pickProcess}",
        "program": "${fileDirname}/bin/${fileBasenameNoExtension}",
        "MIMode": "gdb",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ],
        "logging": {
            "trace": false,
            "traceResponse": false
        },
        "preLaunchTask": "RepoCodeInspection",
       
    },
]

}

答案2

要设置多个环境变量,请使用逗号分隔的"name", "value"对。

例子:

"environment": [{"name": "LD_LIBRARY_PATH", "value": "/ld/library/path/"},
                {"name": "CUDA_VISIBLE_DEVICES", "value": "0"}
               ]

答案3

让我们来调试您的问题。首先,请记住:这是一个 JSON 文件,因此我们需要思考将其作为 JSON 文件。

这是我们正在考虑的初始 JSON 对象:

  "environment": [
    {
      "CUDA_VISIBLE_DEVICES": "0,1,2,3",
      "CUDA_DEVICE_ORDER": "PCI_BUS_ID"
    }
  ]

所以我们有一个名为“environment”的JSON 数组(由[&表示),其中包含]单个对象{(由&表示})。那单身的对象然后包含键/值对。问题就在这里。

具有多个键值对的单个对象就像struct在 C 中,而不是一个简单的变量,它有一个单身的键/值对。

从逻辑上讲,我会说我们欲望这是:

  • 我们的“环境”将保存许多“环境变量”
  • 我们的环境变量每个都有一个键和一个值。

重新调整我们的[]&组合{}会产生以下结果:

  "environment": [
    {
      "CUDA_VISIBLE_DEVICES": "0,1,2,3"
    },
    {
      "CUDA_DEVICE_ORDER": "PCI_BUS_ID"
    }
  ]

相关内容