Debugging
A compiled executable in release mode does not have references of either
source-code variable names or line numbers. An executable
compiled in debug mode retains these references. This allows:
- program tracing - suspend the execution of the program when
it reaches a specified line (called breakpoint) in the source
file or even suspend it after every line. Thus allowing line by
line execution.
- variable lookup - observe the values of the variables.
- variable range checking - generates an exception when
an array index is out of range
Debug mode executable is larger in size and slower, but otherwise
performs similarly to release mode executable. You can easily switch
between compilation modes through the window in the top toolbar.
Breakpoints
Breakpoint is a line in the program where the execution
stops. Tracing the program step-by-step may become tedious, especially
if the program is large. Instead a breakpoint may be set at the
particular line of interest.
To add a breakpoint in Microsoft Visual Studio (MSVS), click the
marker bar located on the left margin of the editor view beside the
line of code where you want to add a breakpoint. A red dot is
displayed in the marker bar. To clear (remove) a breakpoint, click on
the red dot associated with it.
Tracing Program
To start tracing a built program, press F5 or click on
the green triangle on the toolbar. This opens the tracing windows:
- Variables view shows the values of the variables.
- Debug shows program stack, lists breakpoints and displays
program compilation output.
The program execution stops at the first breakpoint. Note that if you
did not specify any breakpoints, the program will run to
completion. Instead of selecting breakpoints, you can right-click on
the desired line and select "run to cursor" from the dropdown
menu. This will suspend the program at the selected line.
Alternatively, pressing F10 would start the program and suspend it on
the first executable line.
Hitting "step over" F10 key or clicking on the
appropriate icon on the toolbar, advances the program
execution exactly one line. Repeatedly "stepping over" the lines of
the program in this fashion allows the programmer to trace the program
execution in detail.
Observing Variables with "Autos" and "Locals" Window
As you trace your program, the values of the variables can be observed
in the "Autos" and "Locals" tabs of the debugging window (usually
located in the bottom left corner of the MSVS). "Locals" shows all the
variables that are in scope at the particular step of program
execution. The variables that changed their values since the last step
are shown in red.
"Autos" shows only the variables three lines above and three lines
below the current statement. The amount of information shown is
limited and it allows you to focus on the variables that are relevant
to the context. This is particularly helpful if the program is large,
the number of in-scope variables is big and the "Locals" tab becomes
crowded.
Displaying Arrays in Watch Window
The values of the local variables are displayed by the MSVS
automatically in the "Autos" and "Locals" tab of the debugging
window. However, some values cannot be automatically shown. For
example, once an array is passed to a function as a parameter, its size
information is lost. The debugger cannot present the array and its
contents properly.
The debugger window has "Watch" tab to display
arbitrary values. For example, you can instruct it to compute and
display an expression based on the values of the variables. Watch can
be used to display arrays once its size information is lost. For
that, in the name column enter the name of the array, then "comma"
then the array size you want to observe. Once entered, the watch can
be expanded to observe all values of the array. See figure.
Observing Program Stack
As you trace your program, it is possible to examine program stack
frames of all the caller functions with their respective local
variables. MSVS display program stack in "Call Stack" tab of the
bottom-right debug windows. The yellow arrow indicates the function
frame where the execution is currently suspended. By double-clicking
on the particular function frame in the stack, you can observe the
variables that are local to the selected frame the "Locals" tab of
this window.