gdb (MinGW) 不会中断失败的断言(VSCode 配置)

gdb (MinGW) doesn't break on failed asserts (VSCode config)

我正在尝试调试 VSCode 中的一个程序,它违反了断言,但没有中断,也不允许我检查调用堆栈或任何东西。相反,程序只是以退出代码 3 退出并打印出以下文本:

Assertion failed!

Program: C:\Users\Sewbacca\Projects\Practice\CppTest\build\Test.exe
File: C:\Users\Sewbacca\Projects\Practice\CppTest\src\main.cpp, Line 6

Expression: false

我尝试将以下命令添加到 .vscode/launch.json 中的 "setupCommands" 但没有成功:

{
    "text": "break _assert (const char *_Message, const char *_File, unsigned _Line)"
},
{
    "text": "break abort"
},
{
    "text": "break exit"
},

旁注:我没有使用 gdb 的经验,也不知道 setupCommands 到底做了什么改变。我本来希望 vscode 将这些直接发送到 gdb。

我唯一的解决方法是在 main() 之前设置一个断点,然后在调试控制台中键入 -exec break abort。然后它将中断任何失败的断言。

编辑: 将以下配置添加到 "setupCommands":

{
    "text": "-exec break abort"
},

导致以下错误消息:

[Window Title]
Visual Studio Code

[Content]
Unable to start debugging. Unexpected GDB output from command "-exec break abort". Undefined MI command: exec

[Open 'launch.json'] [Cancel]

编辑结束

有没有办法自动执行此操作,或者是否有正确的方法告诉 gdb(尤其是在 VSCode 中)中断失败的断言,而不是退出程序?

编辑:

我的配置没有问题。好像我的 gdb 版本有问题。有时当我告诉 gdb 在进入 main 之前中断时它随机退出,这导致我 this issue. As stated there, gdb 8.1 for x86_64-w64-mingw32 has this bug. Since there is no newer version available in the installer, I downgraded to 7.2 which solved this issue for me. However, after using winlibs 版本 11.1.0,问题仍然存在。

编辑结束

提前致谢!

设置:

src/main.cpp


#include <cassert>

int main()
{
    assert(false);
}

CMakeLists.txt

project(Test)

add_executable(${PROJECT_NAME} src/main.cpp)

.vscode/launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            // Resolved by CMake Tools:
            "program": "${command:cmake.launchTargetPath}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [
                {
                    // add the directory where our target was built to the PATHs
                    // it gets resolved by CMake Tools:
                    "name": "PATH",
                    "value": "${env:PATH}:${command:cmake.getLaunchTargetDirectory}"
                }
            ],
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "text": "break _assert (const char *_Message, const char *_File, unsigned _Line)"
                },
                {
                    "text": "break abort"
                },
                {
                    "text": "break exit"
                },
            ]
        }
    ],
    "compounds": []
}

我的环境:

我找到了解决方案:

gdb 在断言失败时中断,将以下代码添加到 launch.json 中的 setupCommands 时:

"setupCommands": {
...
    {
        "text": "set breakpoint pending on",
        "description": "Ensures that a breakpoint for abort will be set!",
        "ignoreFailures": true
    },
    {
        "text": "break abort",
        "description": "Breaks on failed asserts",
        "ignoreFailures": true
    },
    {
        "text": "set breakpoint pending auto",
        "description": "Setting back to default behaviour",
        "ignoreFailures": true
    }
...
}

解释:

为了中断断言失败,我发现这个 Stack Overflow Post 在我手动测试时有效,但在 launch.json 中使用时无效。 我不明白为什么它不会自行中断,但我明白为什么 break abort 不起作用,至少我有一个猜测:符号尚未加载并且 break abortgdb 中询问您是否应创建挂起的断点,默认为否。所以将挂起的断点设置为打开,为我解决了这个问题,现在我终于可以调试我的程序了。