How to debug with the C++ debugger
A debugger is an application program that monitors how programs run. It is a
tool to help us find bugs in your executable programs. In particular, it can
tell us how your program flows (i.e., it can "hand"-trace your codes step by
step for you), and it can tell us intermediate values of variables. You can
also specify the debugger to let your program stop at certain points (called
breakpoints) so that you can stop and think what goes wrong in the middle of
execution.
To me, a debugger is the last thing I want to use. It is always best to write
a program with no bugs to start with. No kidding. Bugs can be found without
any use of debuggers. Your brain is the best debugger available. However, a
debugger is a nice tool, especially when you are writing a very large program.
I. Before you start
You must first compile your program before you start, and have your compiled
program ready to run. A debugger is not a tool to find syntax errors.
If your program haven't got successfully compiled, you are not ready to run
your program with the debugger yet.
II. Turning on the debugger
First, have your project window open with your C++ codes. To start, turn on
the debugger by choosing Use Debugger command from the Project
menu. A little bug appears in your project window. Choose Run from
the Project menu to let THINK C++ generate debugging tables. You now
have two additional windows popped up at the bottom of your screen.
- Data window is used to examine and set the values of your
variables.
- Source window contains the source text of your program.
III. Watching the program run
The Source window
The top of the Source window has a six button status panel. These
buttons control the execution of your program. The black arrow to the left
is the current statement arrow, which shows the statement the debugger
is about to execute.
Stepping through statements and functions
Clicking on the Step button lets you execute your program line by
line. The In command executes every statement until it falls into
a function. On the other hand, the Out command executes every
statement until it reaches the end of the current function. Clicking the
Trace button takes you to the next statement even if it has to step
into a function. Stepping, on the other hand, never dives into a
function.
Setting a breakpoint
A breakpoint is a marker you can set in your program so that
execution stops when it reaches the marker. If you click on a hollow diamond
in the leftmost column in the Source window, it turns black to indicate that
you've set a breakpoint there. You can set as many breakpoints as you may
like this way. To remove a breakpoint, just click in the filled diamond.
To remove all breakpoints, choose Clear All Breakpoints command in the
Source menu. To start execution, press the Go button. The
program runs for a few moments and then stops. The current statement arrow is
at your breakpoint.
Stopping the program
To stop your program, click on the Stop button. If your program is
stuck in a loop, you can use the panic button to stop.
IV. Examining and setting variables
Tracing your program's execution lets you see what your program is doing. But
to really fix bugs, you need to be able to examine your variables. That's
what the Data window is for.
The Data window
Expressions you type into the entry field appear in the left column
when you press the enter button (check mark) or when you press the
Return key. If you change your mind and don't want to enter an expression,
press the deselect button (X mark). To clear an expression in the
Data window, select it and choose Clear from the Edit menu.
Examining variables
Suppose you want to watch the value of the k variable in your program.
Click in the Data window, and you will see the insertion point blinking in the
entry field. Type k in the entry field and press the Return key. Then
click on Go button, and you will see the value(s) of k. If you have
a breakpoint set, then execution stops at the breakpoint with the current
value of k shown.
Changing the value of a variable
To change the value of a variable, click on its value in the Data window and
type a new one in the entry field. When you click on the enter button (check
mark), the value of the variable changes.
V. Quitting the debugger
The best way to quit the debugger is to quit your application. You should use
the ExitToShell command in the debugger's Debug menu only when
you can't use your application's Quit command.
Takunari Miyazaki
Office: 230 Deschutes Hall
Phone: (503) 346-1383
E-mail:
miyazaki@cs.uoregon.edu
October 18, 1995