56

Is there any gcc option I can set that will give me the line number of the segmentation fault?

I know I can:

  1. Debug line by line
  2. Put printfs in the code to narrow down.

Edits:

  1. bt / where on gdb give No stack.
  2. Helpful suggestion
Community
  • 1
  • 1
Rohit
  • 1,710
  • 5
  • 20
  • 29

8 Answers8

79

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.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
rck
  • 2,020
  • 2
  • 23
  • 23
  • Some crashes can corrupt stact contents and then you're probably best with inserting printf's and checking validity of input at various code points. – che Feb 03 '09 at 00:07
  • 2
    you could also try my suggestion below -- it uses glibc's stack unwind routines to print you out a stacktrace w/o having to resort to gdb. – Todd Gamblin Feb 03 '09 at 00:24
  • I find this useful: https://www.gnu.org/software/gcc/bugs/segfault.html – Loves Probability Dec 10 '16 at 05:26
27

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

Jonas Kölker
  • 7,680
  • 3
  • 44
  • 51
11

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.

Community
  • 1
  • 1
Todd Gamblin
  • 58,354
  • 15
  • 89
  • 96
  • The link is broken... the corrected one is: http://stackoverflow.com/questions/77005/how-to-generate-a-stacktrace-when-my-gcc-c-app-crashes – Arto Bendiken Mar 05 '11 at 18:44
8

Run it under valgrind.

anonnn
  • 307
  • 4
  • 2
4

you also need to build with debug flags on -g

You can also open the core dump with gdb (you need -g though).

cbrulak
  • 15,436
  • 20
  • 61
  • 101
4

If all the preceding suggestions to compile with debugging (-g) and run under a debugger (gdb, run, bt) are not working for you, then:

  • Elementary: Maybe you're not running under the debugger, you're just trying to analyze the postmortem core dump. (If you start a debug session, but don't run the program, or if it exits, then when you ask for a backtrace, gdb will say "No stack" -- because there's no running program at all. Don't forget to type "run".) If it segfaulted, don't forget to add the third argument (core) when you run gdb, otherwise you start in the same state, not attached to any particular process or memory image.
  • Difficult: If your program is/was really running but your gdb is saying "No stack" perhaps your stack pointer is badly smashed. In which case, you may be a buffer overflow problem somewhere, severe enough to mash your runtime state entirely. GCC 4.1 supports the ProPolice "Stack Smashing Protector" that is enabled with -fstack-protector-all. It can be added to GCC 3.x with a patch.
Liudvikas Bukys
  • 5,790
  • 3
  • 25
  • 36
1

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.

kiwixz
  • 1,380
  • 15
  • 23
1

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

SoapBox
  • 20,457
  • 3
  • 51
  • 87