Lab 7: Debugging with gdb
Introduction
In this lab you will practice using the gdb debugger.
Why you should use a debugger
When you run your program within a debugger, you can stop the
program at critical points and examine the values of variables and
objects. The debugger provides much more capability and flexibility
than debugging a program using print statements. For example:
- The debugger allows you to examine the value of a
variable after your program has crashed. Using print
statements you can only see values before a program crashes.
- If you need to examine the value of a variable that you
previously had not suspected, you don't have to edit, recompile
and re-run your program.
- The debugger allows you to examine values of variables in the
function call stack, e.g., while you are debugging a
function foo(), not only can you print out the local
variables of foo(), you can also print out the local
variables of the function that called foo(), and the
function that called that function, and so on and so forth.
In this lab, we will use gdb, a debugger with a command
line interface. Although the user interface is a bit clunky, you
will find that gdb has many useful features. It
"understands" C and C++ types and syntax, and it works well with source
code that is distributed across multiple files.