Noeud:How to Use a Core Dump, Noeud « Next »:Finding Out Where You Are - Backtrace, Noeud « Previous »:Core Dumps - What Are They?, Noeud « Up »:An example debugging session using gdb
To use the core dump, we give it as another argument to gdb
:
bash$ gdb ecount2 core Core was generated by `ecount2 example'. Program terminated with signal 11, Segmentation fault. Reading symbols from /lib/libc.so.6...done. Loaded symbols for /lib/libc.so.6 Reading symbols from /lib/ld-linux.so.2...done. Loaded symbols for /lib/ld-linux.so.2 #0 strcpy (dest=0x0, src=0xbffffa9d "example") at ../sysdeps/generic/strcpy.c:40 40 ../sysdeps/generic/strcpy.c: No such file or directory. (gdb)
Here we see that gdb
loads the executable as usual, but also loads the core file. It can tell us what arguments the program was called with, and why the core was dumped (Segmentation Fault in this case).
gdb
then loads the symbol tables of all the shared libraries that our program had loaded at the time it crashed, so it has all the information at its fingertips.
Next we have a line starting with #0
which tells us that the program crashed in routine strcpy()
with two arguments, dest=0x0
and src=0xbffffa9d
, which gdb
helpfully expands to show that it points to the string example
.
This function is in one of the system libraries, so although gdb
knows which source file and line the crash occurred on, it has not got access to the source and complains. Thanks to the wonders of Open Source, we could get the source code for the appropriate library and tell gdb
via its directory
command. Try help directory
in gdb
for more details.
Usually, you don't really need to get into library source, unless you know you are using a locally produced or bleeding edge library, so we won't worry about that.
If we look up the manpage for strcpy()
, we find that it takes two parameters, the first a char * pointing to the space to store the copy, and secondly the string to be copied from.
In the line starting #0 we see that the first parameter is a NULL pointer. Trying to write to this is what caused the segmentation fault which killed the program.
That's all well and good, but where in our code is this happening?