Linux Bible

Home



Compile this program using the command gcc -g debugme.c -o debugme. Then, execute the program using the command./debugme. $ ./debugme
Segmentation fault (core dumped)
$ file core


core: ELF 32-big LSB core file Intel 80386, version 1 (SYSV), SVR4-style, SVR4-stylee, from 'debugme' On most systems, when you execute./debugme, it immediately causes a segmentation fault and dumps core, as shown in the output listing.


A core dump refers to an application failing and copying all data stored in memory for the application into a file named core in the current directory. That file can be used to help debug a problem with the application. The file output may be called core, or have a numeric extension, such as core.12345 (usually the process ID of the program that died).


If you don't see the core dumped message, try executing the shell command ulimit -c unlimited, which allows programs to drop a memory dump in their current working directory. The program has a bug, so you need to debug it. The first step is to start GDB, using the program name, debugme, and the core file, core, as arguments:
$ gdb debugme core


As you can see near the middle of the figure, GDB displays the name of the executable that created the core file:
' + ,'. Obviously, the displayed name is wrong; it should be debugme. The odd characters and the incorrect program name would give an experienced developer an immediate clue that the program has a significant memory bug. The next line in the figure, the text that reads Program terminated with signal 11, Segmentation fault , explains why the program terminated. A segmentation fault occurs anytime a program attempts to access memory that doesn't explicitly belong to it. GDB also helpfully displays the function it was executing, index_to_the_moon.


TIP
If you don't like the licensing messages (they annoy me), use the -q (or-quiet) option when you start GDB to suppress them. Another useful command-line option is -d dirname, where dirname is the name of a directory, which tells gdb where to find source code (it looks in the current working directory by default).


 
© 2009