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

时间:2021-07-13 08:52:54

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

我正在尝试在 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

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

{
    "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 版本有问题。有时,当我在进入 main 之前告诉 gdb 中断时,它会随机退出,这导致我进入 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

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:

setupCommands 在断言失败时中断,将以下代码添加到 launch.json 中的 "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 } ... } 时:

launch.json

说明:

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

相关问题