The purpose of this is to demonstrate GDB, the GNU debugger.
GDB stands for The GNU Debugger. GDB is a unix terminal debugger, which can execute your program in a sandbox and allow you to analyze it during runtime. GDB has no GUI, but is very simple to use and in some cases can cut your debugging time from hours to minutes.
To know if your system has GDB, type the command "man gdb" at the command prompt.
In this example, we will find the segmentation fault in a small program, segfault.c.
To start, download segfault.c and open it up in an
editor such as vi or emacs.
As you can see, the segfault.c program creates a char array with Hello stored in it. The main function then calls findA(), which attempts to find the first instance in the string where two 'A' characters occur in a row.
Now, if you compile segfault.c
user@plus:~> gcc -Wall -o segfault segfault.c
and run it at the prompt, you will get the following:
user@plus:~> ./segfaultAs you can see, the program segfault has crashed. This occurs because Hello does not have two consecutive A's. After findA() does not find two consecutive A's in Hello, it continues reading past the end of the array into system memory, causing a segmentation fault.
Calling findNull, will seg fault now
Segmentation fault
user@plus:~>
Engaging the debugger:
We will now use gdb to discover where this segmentation fault occurs in the code
The first step in using gdb is to compile your program with debugger tags. Compiling with debugger tags embeds information about your source code into your executable file. The debugger can then use this to pinpoint where a given error occurs
To add debug tags, add the -g flag to the compile line.
user@plus:~> gcc -g -Wall -o segfault segfault.c
Your program is now compiled with debug tags. Next, start GDB with your program:
user@plus:~> gdb ./segfault
After starting the debugger, you will see the following, which means GDB has launched successfully:
GNU gdb 6.4 Copyright 2005 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i586-suse-linux"...Using host libthread_db library "/lib/libthread_db.so.1". (gdb)
GDB then presents you with a prompt. This prompt is where you input all gdb commands. For right now, we will simply run the program and see what it does. However, a list of extended GDB commands and their functions can be found here.
If you wanted to run this program with arguments, you would run the program in the debugger by typing run followed by the arguments that would normally follow the program name at the command prompt. segfault does not take arguments, so in this case to run the program, simply type run at the prompt:
(gdb) run
Gdb then starts the program:
Starting program: /mnt/castor/seas_home/u/user/segfault
The program then runs, crashing at a segmentation fault:
Calling findNull, will seg fault now
Program received signal SIGSEGV, Segmentation fault.
0x08048467 in findA (newArray=0xbfffeace "Hello\nðêÿ¿Hëÿ¿|èê·à\f")
at segfault.c:34
34 while(newArray[i] != 'A' || newArray[i+1] != 'A'){
After the program crashes, GDB takes over. GDB then uses the tag information embedded
in the executable to give you information about what caused the program to crash.
In this example, GDB tells us:
You can now use the above information to fix the error in your source code.
Another simple gdb command is where. Where
will give you a stack
dump, which means it will give you the calling function of the code that
crashed, then the function that called that function, and recurse upwards through
the functions until you reach the main function. This can be helpful if your
code has significant function depth. In this case, after the program crashes,
the where command will return
the following.
(gdb) whereThis dump means that findA was the function that crashed, and that findA was called by main.
#0 0x08048467 in findA (newArray=0xbfffeace "Hello\nðêÿ¿Hëÿ¿|èê·à\f") at segfault.c:34 #1 0x08048434 in main () at segfault.c:25
(gdb) quit
The program is running. Exit anyway? (y or n) y
You can now fix your code, and repeat the process to see if a segmentation fault still occurs.
Additional Resources:
This example is just a small subset of GDB's capabilities. For additional GDB commands and instructions, see the GDB documentation.
There is another debugger called ddd that is the GUI version of GDB. Learning and using ddd is not necessary for the small programs you will be writing this course. However, the documentation for ddd can be found here.
Created by Jean Griffin
Edited by Diana Palsetia