238

How does one determine where the mistake is in the code that causes a segmentation fault?

Can my compiler (gcc) show the location of the fault in the program?

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
  • 9
    No gcc/gdb cannot. You can find out _where_ the segfault occured, but the actual error could be at a totally different location. –  May 20 '10 at 18:51

9 Answers9

330

GCC can't do that but GDB (a debugger) sure can. Compile you program using the -g switch, like this:

gcc program.c -g

Then use gdb:

$ gdb ./a.out
(gdb) run
<segfault happens here>
(gdb) backtrace
<offending code is shown here>

Here is a nice tutorial to get you started with GDB.

Where the segfault occurs is generally only a clue as to where "the mistake which causes" it is in the code. The given location is not necessarily where the problem resides.

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
nc3b
  • 15,562
  • 5
  • 51
  • 63
  • 40
    Note that where the segfault occurs is generally only a clue as to where "the mistake which causes" it is in the code. An important clue, but it's not necessarily where the problem resides. – mpez0 May 20 '10 at 18:55
  • 13
    You can also use ( bt full ) to get more details. – ant2009 May 21 '10 at 03:37
  • 4
    I find this useful: https://www.gnu.org/software/gcc/bugs/segfault.html – Loves Probability Dec 10 '16 at 05:25
  • 5
    Use `bt` as a shorthand for `backtrace`. – rustyx May 27 '19 at 07:53
  • does it matter where the -g switch goes in my compiling line? – Ushumgallu Mar 22 '22 at 23:08
  • @mpez0 Can you clarify with an example? – Mehdi Charife Jan 30 '23 at 11:36
  • 1
    @MehdiCharife You get a segfault when dereferencing a bad pointer, not when the pointer information is bad. That is, your valid pointer can go bad because the target is freed, moved, or otherwise no longer valid, or because the pointer itself is not initialized or is changed and now invalid. The system can not detect those events, only the bad dereference that occurs later. – mpez0 Feb 01 '23 at 22:38
  • use ```gdb --args ./a.out -option``` if your program need some command line arguments – Alec.Zhou Mar 20 '23 at 07:15
83

Also, you can give valgrind a try: if you install valgrind and run

valgrind --leak-check=full <program>

then it will run your program and display stack traces for any segfaults, as well as any invalid memory reads or writes and memory leaks. It's really quite useful.

NAND
  • 663
  • 8
  • 22
jwkpiano1
  • 951
  • 5
  • 4
  • 7
    +1 , Valgrind is so much faster / easier to use to spot memory errors. On non-optimized builds with debugging symbols, it tells you _exactly_ where a segfault happened and why. – Tim Post May 21 '10 at 16:38
  • 1
    Sadly my segfault disappears when compiling with -g -O0 and combined with valgrind. – JohnMudd Jan 19 '18 at 22:30
  • 5
    `--leak-check=full` will not help to debug segfaults. It is useful only for debugging memory leaks. – ks1322 Sep 04 '18 at 09:49
  • @JohnMudd I have a segfault only appear about 1% of the input files tested, if you repeat the failed input it will not fail. My problem was caused by multithreading. So far I have not figured out the line of code causing this problem. I am using retry to cover up this problem for now. If use -g option, fault goes away! – Kemin Zhou Apr 14 '20 at 19:25
27

There are a number of tools available which help debugging segmentation faults and I would like to add my favorite tool to the list: Address Sanitizers (often abbreviated ASAN).

Modern¹ compilers come with the handy -fsanitize=address flag, adding some compile time and run time overhead which does more error checking.

According to the documentation these checks include catching segmentation faults by default. The advantage here is that you get a stack trace similar to gdb's output, but without running the program inside a debugger. An 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 slightly more complicated than what gdb would output but there are upsides:

  • There is no need to reproduce the problem to receive a stack trace. Simply enabling the flag during development is enough.

  • ASANs catch a lot more than just segmentation faults. Many out of bounds accesses will be caught even if that memory area was accessible to the process.


¹ That is Clang 3.1+ and GCC 4.8+.

asynts
  • 2,213
  • 2
  • 21
  • 35
  • 3
    This is most helpful to me. I have a very subtle bug that happen randomly with a frequency about 1%. I process large number of input files with (16 major steps; each one done by a different C or C++ binary). One later step will trigger segmentation fault only randomly because of multi-threading. It is hard to debug. This option triggered the debug information output at least it gave me a start point for code review to find the location of the bug. – Kemin Zhou Apr 15 '20 at 03:15
25

You could also use a core dump and then examine it with gdb. To get useful information you also need to compile with the -g flag.

Whenever you get the message:

 Segmentation fault (core dumped)

a core file is written into your current directory. And you can examine it with the command

 gdb your_program core_file

The file contains the state of the memory when the program crashed. A core dump can be useful during the deployment of your software.

Make sure your system doesn't set the core dump file size to zero. You can set it to unlimited with:

ulimit -c unlimited

Careful though! that core dumps can become huge.

Mathieu Rodic
  • 6,637
  • 2
  • 43
  • 49
Lucas
  • 13,679
  • 13
  • 62
  • 94
8

All of the above answers are correct and recommended; this answer is intended only as a last-resort if none of the aforementioned approaches can be used.

If all else fails, you can always recompile your program with various temporary debug-print statements (e.g. fprintf(stderr, "CHECKPOINT REACHED @ %s:%i\n", __FILE__, __LINE__);) sprinkled throughout what you believe to be the relevant parts of your code. Then run the program, and observe what the was last debug-print printed just before the crash occurred -- you know your program got that far, so the crash must have happened after that point. Add or remove debug-prints, recompile, and run the test again, until you have narrowed it down to a single line of code. At that point you can fix the bug and remove all of the temporary debug-prints.

It's quite tedious, but it has the advantage of working just about anywhere -- the only times it might not is if you don't have access to stdout or stderr for some reason, or if the bug you are trying to fix is a race-condition whose behavior changes when the timing of the program changes (since the debug-prints will slow down the program and change its timing)

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
2

Lucas's answer about core dumps is good. In my .cshrc I have:

alias core 'ls -lt core; echo where | gdb -core=core -silent; echo "\n"'

to display the backtrace by entering 'core'. And the date stamp, to ensure I am looking at the right file :(.

Added: If there is a stack corruption bug, then the backtrace applied to the core dump is often garbage. In this case, running the program within gdb can give better results, as per the accepted answer (assuming the fault is easily reproducible). And also beware of multiple processes dumping core simultaneously; some OS's add the PID to the name of the core file.

Joseph Quinsey
  • 9,553
  • 10
  • 54
  • 77
  • 6
    and don't forget `ulimit -c unlimited` to enable core dumps in the first place. – James Morris May 20 '10 at 19:14
  • @James: Correct. Lucas already mentioned this. And for those of us who are still stuck in the csh, use 'limit'. And I've never been able to read the CYGWIN stackdumps (but I haven't tried for 2 or 3 years). – Joseph Quinsey May 20 '10 at 19:44
0

This is a crude way to find the exact line after which there was the segmentation fault.

  1. Define line logging function
#include \<iostream> 

void log(int line) {  
std::cout << line << std::endl;  
}
  1. find and replace all the semicolon after the log function with "; log(_LINE_);"

  2. Make sure that the semicolons replaced with functions in the for (;;) loops are removed

0

In case any of you (like me!) were looking for this same question but with gfortran, not gcc, the compiler is much more powerful these days and before resorting to the use of the debugger, you can also try out these compile options. For me, this identified exactly the line of code where the error occurred and which variable I was accessing out of bounds to cause the segmentation fault error.

-O0 -g -Wall -fcheck=all -fbacktrace
ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86
0

If you have a reproducible exception like segmentation fault, you can use a tool like a debugger to reproduce the error.

I used to find source code location for even non-reproducible error. It's based on the Microsoft compiler tool chain. But it's based on a idea.

  1. Save the MAP file for each binary (DLL,EXE) before you give it to the customer.
  2. If an exception occurs, lookup the address in the MAP file and determine the function whose start address is just below the exception address. As a result you know the function, where the exception occurred.
  3. Subtract the function start address from the exception address. The result is the offset in the function.
  4. Recompile the source file containing the function with assembly listing enabled. Extract the function's assembly listing.
  5. The assembly includes the offset of each instruction in the function. Lookup the source code line, that matches the offset in the function.
  6. Evaluate the assembler code for the specific source code line. The offset points exactly the assembler instruction that caused the thrown exception. Evaluate the code of this single source code line. With a bit of experience with the compiler output you can say what caused the exception.
  7. Be aware the reason for the exception might be at a totally different location. e.g. the code dereferenced a NULL pointer, but the actual reason, why the pointer is NULL can be somewhere else.

The steps 6. and 7. are beneficial since you asked only for the line of code. But I recommend that you should be aware of it.

I hope you get a similar environment with the GCC compiler for your platform. If you don't have a usable MAP file, use the tool chain tools to get the addresses of the the function. I am sure the ELF file format supports this.

harper
  • 13,345
  • 8
  • 56
  • 105