关于C ++:在Windows上使用gdb调试MinGW程序,不会在断言失败时终止

Debugging MinGW program with gdb on Windows, not terminating at assert failure

如何在窗口上设置gdb,以使其不允许断言失败的程序终止? 我打算检查程序中的堆栈跟踪和变量。

例如,运行在gdb中用MinGW'g++ -g test.cpp -o test'编译的test.cpp程序:

1
2
#include <cassert>
int main(int  argc, char ** argv) { assert(1==2); return 0; }

给出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ gdb test.exe
GNU gdb 6.8
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type"show copying"
and"show warranty" for details.
This GDB was configured as"i686-pc-mingw32"...
(gdb) r
Starting program: f:\\code/test.exe
[New thread 4616.0x1200]
Error: dll starting at 0x77030000 not found.
Error: dll starting at 0x75f80000 not found.
Error: dll starting at 0x77030000 not found.
Error: dll starting at 0x76f30000 not found.
Assertion failed: 1==2, file test.cpp, line 2

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

Program exited with code 03.
(gdb)

我希望能够阻止程序立即终止,就像Visual Studio的调试器和Linux上的gdb那样。 我进行了搜索,发现了一些有关捕获信号的内容,但似乎找不到关于如何设置gdb来完成此操作的好文章。


发现断点可以用以下行放在.gdbinit文件中:

1
2
set breakpoint pending on
b exit

这样就无需为Windows输入yes。


只需在出口处设置一个断点:

(gdb) b exit


将最新的(2017年3月)msys2与gcc 6.3和gdb 7.12.1结合使用时,应使用:

1
break _exit

即使用_exit而不是exit。 我希望这在其他情况下也能起作用,因为我希望exit会调用_exit实际退出。