【发布时间】:2014-05-22 21:05:51
【问题描述】:
拿下面的测试程序(用clang 3.4编译,在gdb 7.6.1下运行):
#include <limits.h>
#include <stdio.h>
int main(void)
{
int a = INT_MAX + 1;
int b = INT_MAX + 2;
printf("Result: a = %d, b = %d\n", a, b);
}
我希望能够在此处 (int b = ...)第二 次出现未定义行为时使用 gdb 自动停止。
如果我编译:
clang -fsanitize=undefined -O0 -ggdb3 -o test test.c
...然后在 gdb 下运行程序只会导致它运行完成:
test.c:6:21: runtime error: signed integer overflow: 2147483647 + 1 cannot be represented in type 'int'
test.c:7:21: runtime error: signed integer overflow: 2147483647 + 2 cannot be represented in type 'int'
Result: a = -2147483648, b = -2147483647
[Inferior 1 (process 24185) exited normally]
但如果我使用:
clang -fsanitize=undefined-trap -fsanitize-undefined-trap-on-error -O0 -ggdb3 -o test test.c
...那么我无法在第一次出现后继续:
Program received signal SIGILL, Illegal instruction.
0x0000000000400556 in main () at test.c:6
6 int a = INT_MAX + 1;
(gdb) c
Continuing.
Program terminated with signal SIGILL, Illegal instruction.
The program no longer exists.
是否有可能在(且仅在)未定义的行为被标记时让 gdb 中断,但让程序继续运行?我追求的技术不仅适用于这个例子,而且一般来说,有问题的行可能在循环内,值可能在运行时确定,等等。
【问题讨论】:
-
使用第一种编译方式,在
write上插入断点,查看堆栈跟踪。
标签: c gdb clang undefined-behavior