CIS 22 - Data Structures: Using the Borland C++ Integrated Debugger
1.
Single-Stepping.
The simplest, but also one of the most revealing,
things you can do with a debugger is to single-step through your program.
First, open the project for your program. Then press F7 or the F8
key. The first time you do this, the program will be recompiled with debugging
information included in it. Then the program will start “running” in single-step
mode. you will see that the first executable line of the program, probably
main(), is highlighted.
As you continue to press F7 or F8,
the highlighted line will move through the listing, following the path
of program control. The highlight marks the line about to be
executed.
2.
Step Over Versus Trace Into.
The F8 key will cause control to step
over any function call it encounters, treating the call as a single
line of code. Conversely, the F7 key will cause control to trace
into the function, so that you can single-step through all the
function's statements. You can switch between these two keys whenever you
want, so you can trace into some functions but not others.
Sometimes just finding out where the control goes
in a program is enough to find a bug. Once you know that a program is wandering
off somewhere it's not supposed to, the reason is easy to discover and
fix.
3.
Starting Over.
If you want to start over at the beginning of the
program, select Terminate Program from the Debug menu.
4.
Endless Loops.
In most cases you can terminate a program you're
debugging by selecting Terminate Program from the Debug menu.
However, if the program is running full speed in an endless loop, you'll
need to press CTRL-ALT-DEL to have Windows terminate it.
5.
A C++ Peculiarity.
The debugger has a limitation specific to C++, at
least in its default configuration: You can't single-step through an inline
function. This includes class member functions that are defined within
the class specification. The debugger threats such functions as a single
line of code, even with the F7 key. This is frustrating, especially
if the bug lies within such a function.
You can fix this by forcing all functions to be
“out-of-line” (that is, to be real functions instead of inline functions),
even if they are defined inside a class specification. To do this, select
Project from the Options menu, double-click on the Compiler
topic, double-click on the Debugging subtopic, and check the
box called Out-of-line Inline Functions. Now you can trace into
any member function, no matter where it's defined.
6.
Breakpoints.
Sometimes it's not practical to single-step from
the beginning of the program to the place where the bug is. Suppose, you
have a loop that does something 10,000 times, and you know the bug is somewhere
in the program lines following the loop. No one has time to single-step
around a loop 10,000 times, or even 100. The solution to this is the breakpoint.
In the situation just described you should install a breakpoint after the
loop but before the section of code with the bug in it. Then you can run
the program full speed through the loop, and it will stop at the breakpoint.
>From there, you can start single-stepping.
To install a breakpoint, position the cursor on
the line where you want control to stop. Then select Toggle Breakpoint
from the Debug menu. The selected line will be highlighted (in a
different color than the line about to be executed). Now select Run
from the Debug menu. The program will run full speed until it reaches
the breakpoint, then stop.
7.
Program Output.
While you're debugging a
program you may want to see what kind of output it is generating. Since
the EasyWin program being debugged is actually a separate Windows program,
you can examine its window by switching to the Windows Task List and double-clicking
on its name. This will bring its window into view. The cursor in the program's
window will be a stop sign, indicating that the program is temporarily
stopped by the debugger. You can see whatever output the program has sent
to the window up to that point.
8.
Watches.
Usually you need to know not only where your program
goes, but also what is happening to one or more variables. You can monitor
a variable's value by setting a watch on it.
To set a watch, position the cursor on the
variable's name anywhere in the listing. Then select Add Watch from
the Debug menu. A dialog called Watch Properties will appear.
The variable name should already be displayed in the Expression
field. If it's not, you can type it in. Click on OK to make the
dialog to go away. Another - faster - approach to setting a watch is to
click the right mouse button and select Add Watch from the speed menu that
appears.
When you set the first watch, a new window called
Watch will appear, with the variable name and the value of the variable
shown in it. As you single-step through the program, this will change.
You can place as many variables in the Watch window as you want,
and monitor them all simultaneously.
The most effective way to use the Watch window
is to size it so you can see it and your program's Edit window at
the same time. This way you can see where program control is from the highlighted
line in the Edit window, and also see the corresponding valuse of
the watch variables.
Sanam Dadasheva and Michael Gorenburg