| Name: _____________________ | Class: Comp 217 |
| SSN/ID: _____________________ | Section & Group: ____________ |
The GNU Debugger (gdb)
A symbolic debugger is a program that aids programmers in finding logical errors, by allowing them to execute their program in a controlled manner. More precisely, a debugger allows a programmer to step through a program one statement at a time, and examine the values of variables (and expressions) following the execution of each statement. This allows a programmer to find the statement responsible for a logical error involving a particular variable by stepping through the program a statement at a time and examining the value of variable after each statement executes.
Although there are several debuggers available on UNIX systems, we will look at the GNU symbolic debugger, gdb. In addition to allowing programmers to edit and compile their programs, emacs also interacts with gdb.
Getting Started
Whereas a high-level program operates on variables, a machine-level program operates on addresses. A compiler must therefore translate each variable into an address and must store (among other things) these associations of variables with addresses in a special dictionary, called a symbol table so that it can look up an identifier when it encounters a reference to it and find its address.
The symbol table is usually discarded when compilation is complete. Some way is needed to instruct the compiler to keep it for use by the debugger. For the GNU C++ compiler, this is done with the -g switch.
To use gdb to debug a binary file prog (including lib), one can invoke it from the command line:
Gdb Commands
Quitting gdb
Setting Breakpoints
Setting a breakpoint permits you to mark a particular line in your
program (called a breakpoint) so that when execution reaches
that line, program execution will be suspended, allowing you to enter
a gdb command.
To set a breakpoint:
Removing Breakpoints
Starting Execution of Program
Resuming Execution at a Breakpoint
Once you have suspended execution at a particular statement, you can
resume execution in several ways:
By contrast, the step command will execute the next
statement, but will step into function calls. That is, if the
statement pointed to by the arrow contains a function call, then the
next statement to be executed will be the first line of that
function. The step command enables you to execute a
called function one statement at a time, whereas the next
command confines you to your main (or currently executing)
function. When execution passes from one function to another, gdb
displays the name of the function, plus the value of each argument
passed to its parameters, allowing you to examine what is
happening when the function is called and to check if its
parameters are receiving the arguments you were expecting them to
get.
More?
You can learn about these gdb commands and others using the help command:
source_lab05_sphere.cpp
/* Program to compute the surface area and volume of a sphere
* having a given radius.
* Input: radius
* Output: radius and volume
-------------------------------------------------------------------*/
#include <iostream>
using namespace std;
const double PI = 3.14159265359;
void GetRadius( double & radius);
void ShowResults( double rad, double area, double vol);
int main()
{
cout << "Program computes surface area and volume of a sphere.\n";
double radius, // radius of sphere
surfaceArea, // its surface area
volume; // its volume
GetRadius( radius);
volume = surfaceArea * radius / 3.0;
surfaceArea = 4.0 * PI * radius * radius;
ShowResults(radius, surfaceArea, volume);
return 0;
}
void GetRadius(double & rad)
{
cout << "Enter radius of sphere: ";
cin >> rad;
}
void ShowResults(double rad, double area, double vol)
{
cout << "Radius of sphere is " << rad << " inches\n";
cout << "Its surface area is " << area << "sq. inches\n"
<< "Its volume is " << vol << " cubic inches.\n\n";
}
LAB Exercise 2b: Using the gnu Debugger gdb
GetRadius( radius);
(Note that blank lines and declarations are passed over.) Use
next to "step over" this function call. You should be
prompted to enter a radius. Do so (but do not enter '3'). The arrow should move to the
first assignment statement. Use the print command to
display the value of radius. What output is produced?
Where is the arrow pointing in the source window?
Use the display command to display the value of
volume. What output (if any) is produced?
But we just assigned a value to volume, didn't we? Let's
check the values used in the assignment. Print the value of
radius. What is it? Is it correct?
Print the value of surfaceArea. What is it? Is it correct?
One at the first output statement.
Another at the call to function ShowResults().
When execution reaches the first breakpoint, display the
values of PI, radius, surfaceArea, and volume.
Now give the info display command. What output is produced?
Are the parameter values correct?
Hand In: This lab handout with the answers filled in attached
to a listing of your final program
(use the enscript command from your Programming Style Sheet to print it out:
enscript -E -G -2rj -M Letter -PECT2_PS <filename> or a2ps <filename>).
Ricky J. Sethi <rickys at sethi.org>
Last modified: Wed Aug 31 15:30:45 PDT 2005