There are a number of tools available that help debug segmentation errors, and I would like to add my favorite tool to the list: Address Sanitizers (often abbreviated ASAN) .
Modern compilers come with a convenient -fsanitize=address , adding some compilation time and runtime, which makes more error checking.
According to the documentation, these checks include detection of segmentation errors by default. The advantage here is that you get a stack trace similar to GDB output, but without running the program inside the debugger. Example:
int main() { volatile int *ptr = (int*)0; *ptr = 0; }
$ gcc -g -fsanitize=address main.c $ ./a.out AddressSanitizer:DEADLYSIGNAL ================================================================= ==4848==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x5654348db1a0 bp 0x7ffc05e39240 sp 0x7ffc05e39230 T0) ==4848==The signal is caused by a WRITE memory access. ==4848==Hint: address points to the zero page. #0 0x5654348db19f in main /tmp/tmp.s3gwjqb8zT/main.c:3 #1 0x7f0e5a052b6a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x26b6a) #2 0x5654348db099 in _start (/tmp/tmp.s3gwjqb8zT/a.out+0x1099) AddressSanitizer can not provide additional info. SUMMARY: AddressSanitizer: SEGV /tmp/tmp.s3gwjqb8zT/main.c:3 in main ==4848==ABORTING
The output is a bit more complicated than gdb would output, but there are pluses:
There is no need to reproduce the problem in order to get a stack trace. Simply turn on the flag during development.
ASANs catch a lot more than just segmentation errors. Many unreachable accesses will be detected even if this area of ββmemory was available to the process.
ΒΉ This is Clang 3.1 + and GCC 4.8 + .
asynts May 11 '19 at 13:30 2019-05-11 13:30
source share