Is there any gcc option I can set that will give me the line number of the segmentation fault?
I know I can:
If all the preceding suggestions to compile with debugging (-g) and run under a debugger (gdb, run, bt) are not working for you, then:
core
) when you run gdb, otherwise you start in the same state, not attached to any particular process or memory image.-fstack-protector-all
. It can be added to GCC 3.x with a patch.The No stack
problem seems to happen when the program exit successfully.
For the record, I had this problem because I had forgotten a return in my code, which made my program exit with failure code.
There is no method for GCC to provide this information, you'll have to rely on an external program like GDB.
GDB can give you the line where a crash occurred with the "bt" (short for "backtrace") command after the program has seg faulted. This will give you not only the line of the crash, but the whole stack of the program (so you can see what called the function where the crash happened).
you also need to build with debug flags on -g
You can also open the core dump with gdb (you need -g though).
Here's a complete shell/gdb session
$ gcc -ggdb myproj.c $ gdb a.out gdb> run --some-option=foo --other-option=bar (gdb will say your program hit a segfault) gdb> bt (gdb prints a stack trace) gdb> q [are you sure, your program is still running]? y $ emacs myproj.c # heh, I know what the error is now...
Happy hacking :-)
You can get gcc to print you a stacktrace when your program gets a SEGV signal, similar to how Java and other friendlier languages handle null pointer exceptions. See my answer here for more details:
The nice thing about this is you can just leave it in your code; you don't need to run things through gdb to get the nice debug output.
If you compile with -g and follow the instructions there, you can use a command-line tool like addr2line to get file/line information from the output.