I'm compiling a C program with flags "-Wall -W -pedantic -O0 --coverage" (GCC version 4.8.2). However when a segmentation fault happens on that program I can't extract the coverage, because I don't have the .gcda file...
Does anyone know how can I use gcov even when a segmentation fault happens?
Thanks.
It can be resolved by having a base condition to return from the recursive function. A pointer must point to valid memory before accessing it.
Check shell limits Usually it is the limit on stack size that causes this kind of problem. To check memory limits, use the ulimit command in bash or ksh , or the limit command in csh or tcsh . Try setting the stacksize higher, and then re-run your program to see if the segfault goes away.
On both Windows and Linux, the segfault handler function is passed a "context struct", which includes the state of the registers at the failure site. Ostensibly, this is so people can repair the problem that caused the segfault (it also lets you do nifty things like userspace segment handling).
a Segmentation Fault is really accessing memory that you do not have permission to access ( either because it's not mapped, you don't have permissions, invalid virtual address, etc. ). Depending on the underlying reason, you may want to trap and handle the segmentation fault.
Does anyone know how can I use gcov even when a segmentation fault happens?
The coverage files are normally written by atexit
handler, which requires program to call exit()
. That does not happen when the program dies with SIGSEGV
, which is why you don't get the .gcda
file in that case.
The best solution is to fix whatever bug is causing SIGSEGV
in the first place.
Alternatively, you could install a SIGSEGV
handler, and call exit()
from it. This is not guaranteed to work. For example, if your program hit SIGSEGV
due to heap corruption, it may deadlock or crash again when exit
calls global destructors.
Another possible solution is to run the program under GDB, and call __gcov_flush()
from the debugger when you get SIGSEGV
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With