【问题标题】:gdb (MinGW) doesn't break on failed asserts (VSCode config)gdb(MinGW)不会因断言失败而中断(VSCode 配置)
【发布时间】:2021-09-22 07:49:51
【问题描述】:

我正在尝试在 VSCode 中调试一个程序,该程序违反了断言,但没有中断,也不允许我检查调用堆栈或任何东西。相反,程序只是以 exitcode 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。如那里所述,x86_64-w64-mingw32 的 gdb 8.1 有这个错误。由于the installer 中没有更新的版本,我降级到 7.2 为我解决了这个问题。但是,使用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": []
}

我的环境:

  • 平台:Windows 10
  • 工具链:mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0
  • 构建系统:CMake
  • IDE:带有扩展 CMake 工具的 VSCode

【问题讨论】:

  • 既然您在调试控制台中输入了-exec break abort,我也希望在您的配置中也能做到这一点。
  • 我试过这个,但忘了把它放到帖子里。现在已添加。谢谢。

标签: c++ visual-studio-code cmake gdb mingw


【解决方案1】:

我找到了解决办法:

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

"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 abort 询问你gdb,如果应该创建一个挂起的断点,默认为 no。所以将挂起的断点设置为 on,为我解决了这个问题,现在我终于可以调试我的程序了。

【讨论】:

    猜你喜欢
    • 2020-08-28
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    • 2016-04-26
    • 2012-02-01
    相关资源
    最近更新 更多