Is there any gcc option I can set that will give me the line number of the segmentation fault?
I know I can:
Edits:
bt
/ where
on gdb give No stack.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).
On a Unix operating system such as Linux, a "segmentation violation" (also known as "signal 11", "SIGSEGV", "segmentation fault" or, abbreviated, "sig11" or "segfault") is a signal sent by the kernel to a process when the system has detected that the process was attempting to access a memory address that does not ...
When Segmentation fault 11 occurs, it means that a program has attempted to access a memory location that it's not allowed to access. The error can also occur if the application tries to access memory in a method that isn't allowed.
I don't know of a gcc option, but you should be able to run the application with gdb and then when it crashes, type where
to take a look at the stack when it exited, which should get you close.
$ gdb blah (gdb) run (gdb) where
Edit for completeness:
You should also make sure to build the application with debug flags on using the -g
gcc option to include line numbers in the executable.
Another option is to use the bt
(backtrace) command.
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 :-)
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