【发布时间】: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