GDB Debugger

Disclaimer

The GNU GDB debugger is provided with most Unix systems by the Free Software Foundation. Much of the following information is summarized from the gdb man pages from linux3. Type the command man gdb at the Unix prompt to view the man pages directly.

The complete "official" GDB user's manual from the Free Software Foundation is available at http://www.gnu.org/software/gdb/documentation.


Introduction

The purpose of a debugger such as GDB is to allow you to see what is going on ``inside'' another program while it executes--or what another program was doing at the moment it crashed.

GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:

To use the debugger, your program must be compiled and linked with the "-g" switch. This tells the compiler and linker to keep information that the debugger needs. The "-g" switch should be part of the CCFLAGS in your makefile.

Debugger Basics

Let's assume that the name of your executable is a.out. To debug a.out type the command gdb a.out. This command starts GDB which takes control of a.out.

You can now use GDB commands to control the execution of a.out. You can force a.out to stop executing at a particular line of code, look at the contents of variables, etc.

Many commonly used commands can be abbreviated with a single lette. All GDB commands can be invoked using the shortest unique abbreviation for the command. The most frequently used GDB commands are


When your program crashes

When your program crashes (eg segmentation fault) a very large file named "core" is left behind. You can use GDB to examine this core file to determine where and hopefully why your program crashed.

Again assume that your executable is named a.out. You run a.out and get a segementation fault and a core file is left behind. To examine the core file, execute the command gdb a.out just like when you're debugging a.out. GDB will detect the core file and see that it came from a.out and will read it.

You can now use the GDB command where to display the function call stack and determine which function (and which line number in that function) was executing when your program crashed. You can use the GDB command up and down to move up and down in the stack to look at variables, etc.


Dennis Frey
Last modified: Tues Nov 11, 2003 3:20pm