Easy-Guide to GDB

Why Use a Debugger?

One way to debug code is to write cout statements to examine the run time values of variables, functions, etc. Then you have to recompile and rerun in order to see the results. For instance, if you have just changed a bit of code and want to make sure that the new code executes as you think, you might print variables to see if the code modifies them in the way you predict. Or, if having added or changed a function, you find that the code fails to execute correctly, you might put cout statements at the start of the function to verify that variables are passed correctly. This approach to debugging works well with simple programs with few bugs.

We always assume our programs have no bugs or few bugs, but data shows this isn't true. Using a debugger allows you to accomplish these tasks without repeatedly recompiling. So if you've had to change hundreds of lines of code and you need to monitor the code behavior line by line, perhaps comparing to a known test case, using a debugger can be helpful. Thus, it is useful to learn how to use a debugger although you need not learn how to use it in one lab period.

A debugger allows you to step through a program. At each step the program listing is displayed and before going on, you can check current values of program variables. Before starting program execution under the debugger, the user specifies one or more break points. On command, the program runs until the first break point. The user can then change variablevalues, go on step by step, or set a new break point and ask the program to continue execution to the next break.


The GNU Project Debugger


GDB, the GNU Project debugger, allows 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:

•Start your program, specifying anything that might affect its behavior.
•Make your program stop on specified conditions.
•Examine what has happened, when your program has stopped.
•Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another.

The program being debugged can be written in Ada, C, C++, Objective-C, Pascal (and many other languages). Those programs might be executing on the same machine as GDB (native) or on another machine (remote). GDB can run on most popular UNIX and Microsoft Windows variants.


To use gdb, you must compile the C++ code with the -g option. The -o option allows you to name the object file other than a.out.

EX: g++ -g -o swap swap.cpp

swap.cpp is the source code

swap is the executable code with debugging information included

Typing the start of a command, as long as it is not ambiguous, will suffice.

EX: r works for run

Some Command Descriptions

run

Start a program execution inside gdb from the beginning

quit

Quit gdb

list

List 10 lines of the program

print expr

Print expression, where expression may be a variable name or expression

print var=v

Set a variable var to the value v. i.e. a correction to allow you to continue the run

next

Go to next line

step

Step into next line

continue

Continue from the current place until end of program reaches or you find a break point

break

Stop the run at a given line

EX: break 10 stop before line 10 is executed

A breakpoint makes your program stop whenever a certain point in the program is reached. For each breakpoint, you can add conditions to control in finer detail whether your program stops. You can set breakpoints with the break command and its variants and specify the place where your program should stop by line number, function name or exact address in the program.

watch

A breakpoint that stops when a value is changed.

EX: watch x stop when the value of x changes

A watchpoint is a special breakpoint that stops your program when the value of an expression changes. The expression may be a value of a variable, or it could involve values of one or more variables combined by operators, such as "a + b". You must use a different command to set watchpoints, but aside from that, you can manage a watchpoint like any other breakpoint: you enable, disable, and delete both breakpoints and watchpoints using the same commands.

clear

Clear a breakpoint or watchpoint at given line.

EX: clear 14 Clear breakpoint or watchpoint at line 14

With the clear command you can delete breakpoints according to where they are in your program. With the delete command you can delete individual breakpoints, watchpoints, or catchpoints by specifying their breakpoint numbers.

continue

Resume program execution, at the address where your program last stopped; any breakpoints set at that address are bypassed.

backtrace

A backtrace of the entire stack: one line per frame for all frames in the stack.

You can stop actions at any time by typing the system interrupt character, normally Ctrl-c.

Two sample gdb sessions are included: Do not panic if you don't understand these initially.

Demo of Basic Commands

Sample gdb Debugging Session (more complicated)