Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use "gcov" even when a segmentation fault happens?

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.

like image 819
josecampos Avatar asked Nov 27 '13 18:11

josecampos


People also ask

How can I fix segmentation fault?

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.

How do I find out what is causing my segmentation fault?

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.

Can you recover from a segfault?

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).

Is segmentation fault a trap?

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.


1 Answers

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.

like image 61
Employed Russian Avatar answered Oct 03 '22 15:10

Employed Russian