Lab 7: The Debugger
In this lab, you will learn to use a new tool called a debugger. The GNU debugger (called “gdb”) will help
you find errors and mistakes in programs.
The debugger is
invoked by typing:
gdb nameOfProgram
A new window
(called the “Source Window”) will be created.
It shows the source code of the program along with toolbars and buttons
for controlling the execution of the program.
The first five
buttons in the toolbar control how the program executes. The first causes the program to start
executing (or “run”). The next four
buttons (“step”, “next”, “finish”, and “continue”) are used to control the
program execution after it has been started.
The “step”
button will execute the currently highlighted line of code. If the line contains a function call then
the debugger will “go into” the function.
You can then trace the execution of the function.
The “next”
button also executes the highlighted line of code but will NOT go into any
called functions. The called functions
will still be executed but the debugger will not show the function as it
executes.
The “finish”
button will cause execution to continue until the current function finishes.
The “continue”
button causes execution to continue until the program finishes.
You can also tell the debugger to stop execution on selected lines. These are called “breakpoints” (think of them as little stop signs in the code). To insert a breakpoint, put the cursor over the line number and click. You should now see a little red square at the beginning of that line. Execution will now stop when it reaches that line. To remove the breakpoint, click the line number again. You can only insert breakpoints on lines that have a “-“ before the line number.
Examining Variable Values
The debugger can also show you the current value of variables as the program executes. After the program has started executing, you can click on the “Local Variables” button (the little house button) and a popup window will show you all declared local variables and their current values. As the program executes these values will automatically be updated.
You can also choose which variables to examine. First, click on the “Watch” button (the tiny glasses button). In the Watch Expressions Window, type the name of the variable and click “Add Watch”. The variable and its current value should appear in the Watch Expressions Window.
Note that you can only examine variables AFTER the program has started executing. You will get an error if the program has not yet started executing.
Lab Activity
The following
program is incorrect. Use the debugger
to fix it. Print out and turn in the
source code of the corrected program.
/*
* This program will print a triangle
consisting
* of * symbols.
*
* The triangle should have the following
form:
*
* height = 1: *
*
* height = 2: *
* **
*
* height = 3: *
* **
* ***
*
* and so on.
*
* The user will specify the height of the
triangle.
*/
#include
<iostream>
using namespace
std;
// Function
Prototypes
void
printAsterix(int);
int main()
{
// Get the height of the triangle
int height;
cout << "Enter the height:
";
cin >> height;
cout << endl;
int currentLine = 0;
for (currentLine = 1; currentLine <=
height; currentLine++);
{
printAsterix( currentLine );
cout << endl;
}
return 0;
}
/*
*
Print the specified number of asterix to the screen
*
*
This function will not print an "endl" at the end
*
of the line.
*
*
Parameters:
*
numberToPrint - an integer specifying the number of
* asterix to print on the screen.
*/
void
printAsterix( int numberToPrint )
{
int counter = 0;
for (counter = 0; counter <=
numberToPrint; counter++)
{
cout << "*";
}
cout << endl;
}