[WPI] [cs2223] [cs2223 text] [News] [Notes] 

cs2223, D97/98: gcb, the GNU Debugger

A debugger is a application which allows you to look inside your own program as it executes. Debugging is used to locate and correct logic errors in a C++ program after all syntatic errors have been eliminated.

The key features of gdb are that it allows you to start and stop your program at any line of your program, to step through your program one line at a time, and to stop your program based on some condition (any expression that could be tested by an if() statement). The places where your program will stop are called breakpoints. Whenever the program is stopped, you can look at the contents of any variable (identifier) in your program. Thus you can use gdb to find logical errors and watch what happens when you make changes in your source code.

A summary description of gdb can be obtained on-line by issuing the shell command man gdb. On-line help is also available within the gdb environment, as explained below.

The debugging process using gdb.


1) Compile your Program Use g++ to eliminate all syntatic errors in your program. It should compile before gdb is used. After your program already compiles, run the compiler again. This time include the -g option:

g++ -g your_program.C    /* use your own file's name */

On some systems, you may get an error message similar to:

cc1plus: warning: `-g' option not supported on this version of GCC

Don't panic. The -g option produces code that is optimized for running under gdb, but it is not essential that this option be used.

2) Invoking gdb Issue the command:

gdb a.out  /* or whatever your executable file is called */


Some copyright information will print and you will get the gdb prompt:

(gdb)

3) Execution State There are two possible executing states for your program: running or stopped. While your program is running, it executes normally. While stopped, you can enter gdb commands. Your program begins in the stopped state, waiting for the first gdb command.

4) Help The gdb command help can be used to obtain help about gdb, any class of gdb commands, or any specific gdb command. You can use the help command any time your programam is stopped -- any time you see the (gdb) prompt. Become familiar with how the help command works.

5) Breakpoints Breakpoints are places in your program where execution will stop and gdb will await your command. Normally these are placed at points where you want to examine or set the contents of program variables (actually, locations in memory that are associated with C language identifiers). The basic gdb breakpoint commands are:

break 37

This sets a breakpoint at line 37 of your source code. Any valid line number can be used.

break foo


This sets a breakpoint at the beginning of the function foo(). Any valid function name can be used.

clear 37
clear foo

These commands clear (delete) breakpoints. Their syntax is the same as the break command.

There are other breakpoint commands. Use the command help breakpoints within gdb for a listing.

6) Running your program First, be sure that breakpoints are set where you want them. Once your program is executing you cannot gracefully stop it to insert breakpoints -- you have to wait for it to stop (or crash) or you can use C-c (Control c) to stop execution of your program and return to gdb. After breakpoints are set, you can use the following commands:

run

This is used to begin execution at the beginning. Normally it is the first command issued after breakpoints are set. If you use this command after your program has begun executing, you will be asked whether to ignore the run command or to restart your program at the beginning.

kill

This is the opposite of run. It immediately halts execution of your (stopped) program.

continue

When your program has stopped at a breakpoint, this command is used to continue program execution. Execution will continue until it stops at the next breakpoint or until your program quits (or crashes).

next

The same as continue except that only the next line of code will be executed and then gdb will await further commands. All commands in the line will be executed, including function calls.

step

This is the same as next except that if the line contains a function call, execution will stop at the first line of that function. This command is useful if you want to observe what is happening both in the main program you are debugging and in the functions it calls.

nexti
stepi

These commands are the same as next and step except that one instruction is executed, instead of one line.

There are other running commands. Use the command help running within gdb for a listing.

7) Examining Data While execution is stopped at a breakpoint, gdb allows you to examine and change any value in any memory location. The main commands are:

print answer

This command prints the contents of memory associated with the C identifier (variable) "answer". Any valid identifier can be used.

set answer = 27

This command puts the number 27 into the memory associated with the C identifier (variable) "answer". Any valid identifier and value can be used. Note that this command is dangerous. It is possible to truly mess up a program's operation by abusing this command.

There are other data commands. Use the command help data within gdb for a listing.

8) Debugging Successful debugging is an art, not a science. Most people find it useful to work with a control flow diagram and/or a listing of their program available while debugging -- typically emacs is kept running in another window. The art is to move through the program in large enough steps to be able to debug the program in a resonable length of time while making small enough steps to be able to understand what is happening. Lots of practice is the best way to become facile in this art. A useful debugging command is

backtrace

This command tells you what happened just before you typed the backtrace command. That can sometimes be helpful if your program crashes while running within gdb.

Another way to tell where you are in a program is to use the command

list
list 69
list foo

This command lists the 10 lines around the "current" line, usually at the last breakpoint. When a number is used, the 10 lines listed are those around the line number entered. When a function name is used, the 10 lines are those surrounding the function definition. Any valid line number or function name can be used.

9) Quit You can quit gdb at any time you see the (gdb) prompt by using the quit command. If a process (your program is) is stopped when you use the quit command, you will be asked if you want to kill the process first. If your program is executing within gdb, C-c (Control-c) can be used to stop your program and return to the (gdb) prompt.

For power users or masochists only

There are many other gdb commands. You can learn about them by using the help command within gdb. An on-line gdb manual is also available. See the Notes page.

After you are already expert in the use of gdb, you might be interested to learn that gdb can be invoked from inside emacs, which can speed up the process of making and implementing program changes. We will not be supporting this option during cs2223 Labs, but you are welcome to experiment on your own, outside of Lab. A summary of how to invoke gdb from inside emacs can be found in the on-line manual. See the Notes page.

--------------------
[WPI Home Page] [cs2223 home page]  [cs2223 text] [News] [Notes] 

Contents ©1994-1998, Norman Wittels
Updated 14Mar98