Hack 36. Set Breakpoints
Visual Studio's breakpoints
appear pretty trivial at first, but you can create some that have
quite a bit of intelligence.
Visual Studio offers a powerful
debugger to aid with testing and troubleshooting your applications.
One of the most common uses of a debugger is to set
breakpoints, which are positions in the code
that, when reached, cause the program execution to pause, allowing
the developer to inspect the code and state of the program. When a
breakpoint is reached and the application suspended, the application
is in break mode. In break mode, you, the developer, can
examine and change the values of the program variables. With Visual
Studio 2005, you can even change the program's code
during this time.
In its simplest form, a breakpoint interrupts program execution
whenever it is reached. However, Visual Studio allows for more
intelligent breakpoints, ones that suspend the program only if a
certain condition has been met or if the breakpoint has already been
passed over a certain number of times.
5.2.1. Enter Break Mode Whenever a Particular Line of Code Is Reached
Developers
often want to suspend their
program's execution and enter break mode when a
specific line of code is reached, such as the first line of code for
a particular method or a line of code inside a conditional statement.
The simplest way to add a breakpoint to a particular line of code is
to click in the margin for that line of code. As Figure 5-1 shows, this will add a red circle in the
margin, indicating that a breakpoint has been set
Clicking in the margin toggles the breakpoint for that line of code,
so to remove a breakpoint, click the red circle.
The
F9 key (Debug.ToggleBreakpoint) will also toggle the breakpoint for
the line of code on which the cursor is located.
Breakpoints can also be added through the New Breakpoint dialog. To
launch the New Breakpoint dialog, go to the Debug menu and choose the
New Breakpoint menu option or press
Ctrl-B (Debug.NewBreakpoint). This will
display the New Breakpoint dialog shown in Figure 5-2.
The New Breakpoint dialog has four tabs:
- Function
Used to add a breakpoint to a specific line of a specific function.
- File
The tab shown in Figure 5-2; adds a breakpoint to a
specific line in a specific file in the solution.
- Address
Adds a breakpoint to a specific memory address. Commonly used when
debugging unmanaged code.
- Data
Used to add a breakpoint on a variable. When the variable changes,
the breakpoint will be hit. Data breakpoints can be used only when
debugging unmanaged code.
To add a breakpoint to a specific line of code, use the File tab. As
Figure 5-2 shows, the File tab prompts you for the
file and the line number for the breakpoint.
5.2.2. Enter Break Mode When a Function Is Called
In
addition to being able to break on a
particular line of code, Visual Studio makes it easy to break when a
particular function is invoked. From the Function tab of the
New Breakpoint dialog�shown in Figure 5-3�you can enter the name of the function
where the breakpoint should be added.
In order to set the breakpoint, the function name needs to be
fully qualified. That is, you need to
specify the function name as:
namespace.class.methodName. For example, suppose
you have a C# Solution named skmDataStructures,
which contains, among others, a class named BST
with a method named Add. To place a breakpoint at
this method, simply type in the function name as
skmDataStructures.BST.Add. However, if you
type in just the method name or the class name and method name,
Visual Studio is smart enough to determine the fully qualified name.
In fact, if there are any conflicts, you will be prompted to select
to what method(s) to add breakpoints, as shown in Figure 5-4.
You can also configure the debugger to break on a particular function
call by using the Call Stack
window. When in debug mode, display the Call Stack window by going to
the Debug menu's Window submenu and choosing the
Call Stack menu option. The Call Stack, as its name implies, lists
the functions on the call stack, along with the parameter types and
values. To add a breakpoint through the Call Stack window,
right-click on the function name and choose the Insert Breakpoint
option from the context menu, as shown in Figure 5-5.
Adding breakpoints through the Call Stack window is a handy technique
when debugging recursive functions.
5.2.3. Review Breakpoints in the Breakpoints Window
The
Breakpoints window provides a list of
the current breakpoints and allows you to enable or disable
breakpoints, delete breakpoints, add new breakpoints, and edit the
properties of existing breakpoints. To display the Breakpoints
window, go to the Debug menu's Windows submenu and
select the Breakpoints option. You can also display this window by
pressing Ctrl-Alt-B (Debug.Breakpoints). Figure 5-6
shows the Breakpoints window.
To add a new breakpoint, click the New icon in the upper-left corner,
which will display the New Breakpoint dialog. To delete a breakpoint,
select the breakpoint and click the Delete icon or delete all
breakpoints in one go by clicking the Clear All Breakpoints icon or
pressing
Ctrl-Shift-F9
(Debug.ClearAllBeakpoints). From the Breakpoints window, you can
disable breakpoints without deleting them, either by unchecking a
particular breakpoint or by clicking the Disable All Breakpoints
icon. To edit a breakpoint, select the breakpoint and click the
Properties icon, or right-click on the breakpoint from this window
and choose the Properties option. This will display the Breakpoint
Properties dialog, which is analogous to the New Breakpoint dialog,
but with the selected breakpoint's values shown.
5.2.4. Break Only on Certain Conditions
Breakpoints
in Visual Studio can be configured
to cause the program to enter break mode only when a particular
condition holds. To add a condition
to a breakpoint, view the breakpoint's properties by
selecting the breakpoint from the Breakpoints window and clicking on
the Properties icon. From any of the tabs, you will find a button
titled Condition. Clicking on this will display the
Breakpoint Condition dialog, where you
can specify the condition to be watched. Figure 5-7
shows the Breakpoint Condition dialog.
If you specify a break condition, whenever the debugger reaches the
breakpoint, it evaluates the condition, and if the condition is met,
the program enters break mode. If, however, the condition is not met,
then the program continues execution. The break condition specified
can be any valid debugger expression, which may include the
relational operators
<, >, =
=, !=, and so on, along with many
nonrelational operators, including +,
-, &&,
||, and so on. Variables as well as object
properties can be used in expressions. For a complete list of valid
debugger expressions, refer to:
- http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsdebug/html/vchowusingexpressionsindebugger.asp
Finally, the breakpoint condition can be configured so that the
program enters break mode when the condition is true or when the
condition has changed.
5.2.5. Control How Often to Break on a Breakpoint
Breakpoints,
by default, cause the program to enter
break mode whenever they are hit and their condition, if any, is met.
However, you can configure a breakpoint to enter break mode based on
the breakpoint's hit
count. The hit count of a breakpoint is
the number of times the breakpoint has been reached and the
condition, if specified, has been met. Through the Breakpoint
Properties dialog box, you can indicate when a breakpoint should
cause the program to enter break mode based on its hit count value.
To configure this information, open up the properties for a
breakpoint and click the Hit Count button Clicking on this button
will display the Breakpoint Hit Count dialog, shown in Figure 5-8.
From the Breakpoint Hit Count dialog, you can specify how often a
breakpoint should cause the program to break. As you can see in Figure 5-8, the default is to break always. You can,
however, specify it to break when the hit count equals, is a multiple
of, or is greater than or equal to a specified value, as shown in
Figure 5-9.
With this setting, the breakpoint won't cause the
program to enter break mode until it has been reached five times. The
hit count technique is especially useful when you want to place a
breakpoint within a loop, but are interested in breaking only in
specific cycles of the loop. For example, if you wanted to break only
after the loop had iterated a dozen times, you could set the
breakpoint to break only when its hit count was greater than or equal
to 12. You could also configure the debugger to stop on each tenth
iteration of the loop by configuring the breakpoint to break only
when its hit count is a multiple of 10.
A large part of the life cycle of a computer program is testing and
debugging. Visual Studio's powerful debugger enables
developers to rapidly debug applications. One of the most common
tools used in debugging are breakpoints. Breakpoints in Visual Studio
can be added to specific lines of code or particular functions and
can be given conditions on when to break and how often, based on a
hit count.
�Scott Mitchell
|
No comments:
Post a Comment