Using the Borland C++ Debugger
A debugger allows you to view the values of your variables in a "watch
window" as the program is executed. It also allows you to step through
your program one instruction at a time, which lets you watch your variables
as they change.
These notes give you a brief introduction to the Borland C++ debugger.
However, if you use another programming environment, you will likely have
a debugger at your disposal, and it will likely have the same features
outlined below. Learning how to use a "debugger", regardless
of the language which you use, is an important part of programming today.
This handout will get you started. You will be expected to use it the debugger
for the remainder of your programming for COSC120, COSC220, and other courses
requiring programming in C++.
The debugger will not work if your program has syntax errors and/or
doesn't link properly.
A. Starting a Debugging Session.
In Borland C++, open your C++ program in a window.
Be
sure that it has no compiler errors and no linker errors.
Press the F7 key to start the Debugger session.
The debugger will
compile/link your program and you will see a blue band on the 1st instruction
in the
program. The debugger
then awaits further instructions from you.
B. The Watch Window.
To ADD a variable to the Watch Window.
Method 1:
Select Debug/Add Watch.
In the "Expression" box, type in the name of a variable which
you'd like to "watch".
Method 2:
Go to the Watch Window by selecting View/Watch
or, if you have already
opened a watch window, select Window/Watches.
Then right-click the mouse, select Add Watch, and type in the variable
to watch.
or, press Ins (insert key)
while in the watch window and go from there.
Method 3:
In the program, highlight a variable using the mouse; right click; and
select Watch.
To add an ARRAY to the watch window, use the format: arrayName[startIndex],numElements
as in--
numList[0],10
to "watch" the first 10 elements in the array named numList,
or
as in--
numList[50],3
to "watch" 3 consecutive elements in numList, beginning at numList[50].
To go to the Watch Window if it disappears from view:
Select View/Watch
OR
If the window has been previously opened,
Select Window; then select Watches from the list
of open windows.
To resize or move the Watch Window:
Same as for any Windows95 window.
Tip! If you select Window/Tile Horizontal, you can move your Watch Window
to the bottom of the screen and enlarge your program window to take up
the rest of the screen. That way you can view both as you step through
your program.
C. So what?
Now you can execute your program and "watch" your variables change before your very eyes. (If you don't understand the significance of this, think about it awhile longer...) The current instruction will be denoted in blue.
To "trace into":
Press the F7 key each time
you want to execute the very next instruction.
(The
first time you do this, the program's executable will be loaded and the
1st instruction in the
program
will be highlighted.)
To "step over" (to execute a subroutine call without having
to trace each instruction):
Press the F8 key.
Note: For future programming, if your program uses objects, the debugger will trace into the constructor if you press F7 on the declaration of an object. The debugger treats a class' member function just like any other function. Why not?
D. Using Breakpoints
Breakpoints are an important part of debugging. They're like having a stop sign embedded in your program. When your program encounters one, it stops execution and waits.
Why use breakpoints? If you have localized the part of your program which is causing problems, you can set a breakpoint at the beginning of that chunk of code. Then you can run the program to the breakpoint and then trace from there. Remember to keep your watch window in view during this process!
Setting a breakpoint: (Do this AFTER you've started the debugging session!)
When you are in the BC++ debugger, to set a breakpoint click the mouse
in the gray margin at the left of the screen to the immediate left of the
instruction which you want to serve as a breakpoint. (It should be an executable
instruction.)
Once you've set your first breakpoint, you will see little red dots in
the margin to denote points at which breakpoints are allowed.
Breakpoints can be toggled. That is, another click of the mouse on a breakpoint will remove the breakpoint.
Running to a breakpoint:
Select Debug/Run (or press ctrl/F9). The program will run from the 1st instruction to the breakpoint.
If you have more than one breakpoint, each time you select Debug/Run, the program will run to the next breakpoint.
E. To Run the Program to the Cursor Position:
Select Debug/Run To...
F. To RESET the program so that it is ready to execute from the beginning again:
Select Debug/Reset This Process.
A window with CPU information
will pop up. Simply close that window and continue.
G. To QUIT the debugger:
Select Debug/Terminate Process.
H. General information on speeding up compile time and link time.
By default BC++ includes debug information in our object code and your exe code. Thus, the compile/link steps take longer, and the resulting executable code is larger than it needs to be for the final program. If you do not need to use the debugger, you can turn off two switches (one for compiling, one for linking) so that debug information will not be included in your object and exe code. If you later need to use the debugger, turn them back on again.
To turn off the debug switch for the compiler, select Options/Project/Compiler/Debugging and, if there is a check mark in the box marked "Debug information in OBJs", click it to turn it off.
To turn off the debug switch for the linker, select Options/Project/Linker/General and, if there is a check mark in the box marked "Include debug information", click it to turn it off. Both the compiler and the linker debug switches are toggles.