I am not a big proponent of gdb. If you *really* know what you are doing, gdb shouldn’t be required. But, every now and then, you come across code that has used function pointers exclusively and then, hand-debugging becomes painful. gdb to the rescue.
You’ll need the following pre-requisites to use gdb to debug a segmentation fault:
1) make sure you have compiled the executable WITH debugging symbols. i.e. the "-g" flag. eg
gcc -g -o hello hello.c
Without debugging symbols, gdb won’t be able to do much.
2) Linux should core-dump on segmentation fault. Set:
ulimit -c unlimited
(man ulimit for more info)
Now just run that the excutable that is segfaulting. As soon as it segfaults, you should get an output something like "Segmentation fault (core dumped)". ls in your working directory and you will find a new core file has been created (probably with the name core.{pid})
Now, we just have to tell gdb to analyze this core. Here’s how
gdb {executable} {dump file}
eg. gdb hello core.1324
Check out the output spit out by gdb and make sure that all debugging symbols have been loaded.
Now, on the gdb prompt:
(gdb) bt
(bt = backtrace .. prints stack strace)
with this backtrace you’ll now know *exactly* where the program segfaulted. The code file, line number and the call which was the culprit.
You can even analyze variable values on any frame. Just change to that frame:
(gdb) frame {num}
eg. (gdb) frame 2
and use:
(gdb) info locals
(gdb) info args
to query the values of local variables and passed arguments, respectively.
With all this info, you can pin down the exact reason for the segfault pretty easily. Saves a lot of time!
May 25, 2009 at 1:51 pm |
You said “If you *really* know what you are doing, gdb shouldn’t be required.”. That’s an invalid statement. A debugger will save _anyone_ time when debugging.
More gdb info here: http://www.pixelbeat.org/programming/debugger/
May 26, 2009 at 8:54 am |
Yes, I agree that a debugger is a real time-saver, but many a times, it doesn’t enhance the understanding of why something is done the way it is done. Result: we end up doing temporary bugfixes, rather than really solving the problem and the code base becomes messy.
Without a debugger, although a bit time consuming, there is a better understanding of the code and thats what I meant by *really knowing what you are doing*. Usually, the fixes are cleaner in this case.
But, I must concede that some errors *should* be debugged by a debugger. Saves a lot of hassle.
(Btw.. thank you for the link)
December 13, 2010 at 10:30 am |
This is a good introduction. Good to see it being promoted. Here’s a tutorial which I found very interesting and useful: http://eric.bachard.free.fr/UTBM_LO22/P07/C/Documentation/C/tutorial_gdb/0-gdb.html
May 31, 2011 at 3:11 am |
Thanks for the info shared on debugging using gdb
November 27, 2011 at 9:13 am |
timeshare help series…
[…]Debugging a segmentation fault using gdb « Tech Rants[…]…
March 17, 2012 at 1:11 am |
Hi,
Thanks for this post. It helps.
I have though one little problem:
info locals doesn’t work for any frame at all.
I get “No symbol table info available” for any of the frame. What went wrong? (I compiled all the files -ggdb)
March 17, 2012 at 4:01 pm |
I suspect the problem is that the linked libraries do not have debugging information even if your main program does. If you are on Redhat-based systems, you can install *-debuginfo packages to get this debug information.
March 17, 2012 at 9:31 pm
Thanks.
so it should be something like glibc-deuginfo right?
I’ll check that 🙂
April 1, 2012 at 2:07 pm |
[…] Debugging a segmentation fault using gdb « Tech RantsMay 22, 2009 … You’ll need the following pre-requisites to use gdb to debug a segmentation fault: 1) make sure you have compiled the executable WITH … […]
April 28, 2013 at 2:00 pm |
This article very useful for system programming lecture. Thank you.
May 25, 2013 at 12:33 pm |
Nice One dude.!
Thanks for the easy explanation.
May 25, 2013 at 12:33 pm |
Reblogged this on $ha$hank and commented:
Some-Information about Break-point is yet to be added.!!
June 9, 2013 at 12:36 pm |
Good one bro…Thanks 🙂
June 24, 2013 at 7:03 pm |
[…] then it seg faults. Following this post I get […]