FINDING THE SOURCE OF A SEGFAULT -------------------------------- 1. Compile your program with debugging symbols: % gcc -g -o myprog myprog.c 2. Tell the shell to keep core files: % unlimit coredumpsize - or - % ulimit -c unlimited 3. Run the program: % ./myprog Segmentation Violation (core dumped) You should now have a file in the local directory called core. It might be called core. (e.g. core.71769). I'll assume it's just called core. 4. Run the debugger to find out where the segfault occured: % gdb myprog core Core was generated by `./myprog'. Program terminated with signal 11, Segmentation fault. Reading symbols from . . . #0
in from (gdb) The information from gdb might not be immediately useful. The segfault might have occured inside a library function for example. You want to find out where the problem is in your code, so you will need to trace back from the function where the segfault occured to the function where the real problem resides. 5. backtrace (gdb) bt #0 0x4009527f in strlen () from /lib/libc.so.6 #1 0x0000000a in ?? () #2 0x400684b3 in vfprintf () from /lib/libc.so.6 #3 0x4006ea12 in printf () from /lib/libc.so.6 #4 0x0804848b in func2 (ptr=0xa
) at test.c:18 #5 0x08048466 in func1 (ptr=0xa
) at test.c:12 #6 0x08048439 in main () at test.c:5 (gdb) From the above output, I can see that the segfault happened in strlen, and that strlen was called by an unknown function, which was called by vfprintf, which was called by printf, which was called by my func2 (note the source location), which was called by my func1 (again, note the source location), which was called by my program's main (once again, note the source).