Debugger Tips
This page collects various tips about the Visual C++ Debugger.
- What is a debugger?
- How to start the debugger
- How to stop the debugger
- Use the variable window
- Use the watch window
- Removing a variable from the watch window
- Displaying expressions
- Breakpoints
- Step Over/Into/Out
- Contexts
- Displaying chars and strings
- Dereferencing pointers
- Displaying arrays and structures
- Getting the debugger to work in MSVC 5.0
- What is a debugger?
A debugger is a tool which
helps a programmer find errors ("bugs") in a program. You can make
the program stop when it reaches a particular statement: this is called a
"breakpoint." While at a breakpoint, you can examine the values of
your variables and investigate the state of your program. Your C program is
visible in a window at this time. Then you can continue executing from the
breakpoint, perhaps to another breakpoint. Along the way you can set
"watches" to view the values of variables you are interested in.
- How to start the debugger: The Visual C++
debugger is built-in to the Visual C++ development environment. You can
start the debugger by selecting "Build/Debug/Go". Alternatively
you can press F5 to start the debugger. Before starting the debugger, you
probably should set a breakpoint, or your program will simply run to
completion as usual.
- How to stop the debugger: During the debugging
process, you can stop it by selecting "Build/Stop Debugging".
Alternatively you can press "Alt+F5" to stop debugging.
- Use the variable window: Variable
window will display variable values local to a function. Selecting
"View/Variables" will pop up the watch window. There are three
tabs in the lower left corner of the variable window. You can click on
different tabs to display different information:
- The Auto tab displays information about variables used in the current
statement and the previous statement.
- The Locals tab displays information about variables local to the
current function.
- The "this" tab displays information about the object pointed
to by "this".
- Use the watch window: Watch window
displays information about the variables that you are interested in.
Selecting "View/Watch" will pop up the watch window. You can enter
variable names in the "Name" column of the watch window, the
debugger fills in the Type and Value columns with the corresponding
information.
- Removing a variable from the watch window:
Displaying expressions: You can
display more than just simple variables in the watch window. You can enter
expressions there, and their values will be displayed. This can be useful,
for example, in displaying the values of logical expressions controlling if
statements and loops.
- Breakpoints: If you want to stop the
program execution at a particular line, use the mouse to click on that line
and then press F9. When you see a red circle showing up next to that line,
you have successfully put a breakpoint there. Pressing F9 again removes the
breakpoint. You cannot put a breakpoint at a variable declaration line. The
breakpoint is not actually activated until the next time the program reaches
that statement, if the program is running in debug mode.
- Step Over/Into/Out: When single-stepping a
program, and the next statement to be executed is a function call,
"Debug/ Step Over" will execute the entire function, whereas
"Debg/Step Into" will step you into the function.
Use "Step Into" for your own not-quite-debugged functions, and
"Step over" for system functions like printf, or debugged
functions of your own. To run a function until its end and then return to
the caller, click "Debug/Step Out." If you have stepped to the end
of your program, you can click "Debug/Go" to finish execution. The
short-cuts for doing "Step Into","Step Over" and
"Step Out" are listed under the "Debug" menu. For
example, F8 can be used for "Step Into".
- Contexts: Visual C++ will only display
information for global variables and local variables which are in the the
context of execution. If you see something like
score CX0069:Error ,
it means either the variable is out of context or is not defined. For
example, if you have the following program:
#include <stdio.h>
int param = 100;
void main(void)
{
int param;
param = 300;
}
To display the value of the global variable param, you
need to enter ::param in the watch window. To display the
variable param in function main, you just need to
enter param in the watch window.
- Displaying chars and strings: If you declare a
variable of type
char or char *, Visual C++ will
display the variable value as well as the charater or character string it
points to in the "Value" column.
- Dereferencing pointers: If you have a
pointer variable in the watch window, there will be a small button next to
the variable name. By clicking on the button, you can see the memory
location it points to in the "Value" column.
- Displaying arrays and structures: To display
the contents of an array or struct, enter the
variable name into the watch window as usual. You will see a small
button appears next to the variable name. By clicking on the button, you can
expand or contract your view of the variable. The button displays a plus
sign (+) when the variable is displayed in contracted-form, and a minus sign
when it is displayed in expanded form.
- Getting the debugger to work in MSVC 5.0
Several people have reported difficulty in getting debugging to work under
5.0. There are several things you can do:
- First, you need to be sure that the active configuration is set
properly. Pull down the "Build" menu and select "Set
Active Configuration." Be sure that Win32-debug is selected instead
of Win32-release. Now, re-compile your program. You should now be able
to use the debugger.
- When you wish to debug, be sure that you either start the program with
Build -> Debug -> Go, or by hitting F5. If you start the program
by selecting Build -> Execute Foo.exe or by hitting CTRL-F5, any
breakpoints will not be recognized and the debugger will not work.
- Look at the build settings. From the Build menu, select settings, and
verify that "Generate debug info" is checked on the Link tab.
Be sure to re-build if you make a change.
-