We are all familiar with the breakpoint, but let's break down some of the advanced breakpoint features.
Here's the simple class we are going to use. Create a console application and paste the following code into the program.cs file:
using System;
namespace BreakpointTester
{
class
Program
{
static
void Main(string[] args)
{
int i = 0;
while(i < 10)
{
i = Add(i , 1);
Console.WriteLine(i.ToString());
}
}
static
int Add(int num1, int num2)
{
return num1 + num2; // line 22 – set a breakpoint here
}
}
}
Breakpoints
We should all know that if we click once to the left of a line number (you did enable line numbers, right), you can set a breakpoint. Click again, and it goes away. The keystroke F9 also sets and removes a breakpoint wherever the cursor is. But did you know there are a whole slew of other options if you right-click on a breakpoint in the code window? These features are available for the Standard, Professional and Team Editions of Visual Studio.
Disable Breakpoint
I find myself using the Disable command, either in the menu or in the Debugging tool window, quite a bit. Why? Sometimes I know I don't need to hit an item during a particular run. I can disable the breakpoint, and the debugger will skip right over. I then won't have to recreate it next time. In the Debugger tool window or the Debug menu, you can also enable and disable all breakpoints at once. I find it a good practice to set breakpoints while I'm writing code if I know there might be issues in the section. I can then disable them if they get in the way of testing other parts of the system.
Location
The location dialog allows you to specify the location for a breakpoint. There are four types of Location breakpoints – file, address, function and data.
File
I don't find this one very useful since I can click in the code window to set and remove breakpoints. However, you can enable the checkbox to allow debugging to step through code that is different from the running version.
Address
Available in: Standard, Professional, Team Editions
An address Location breakpoint can be set while the debugger is running. Right-click on a line of disassembly, and set the breakpoint. A word of warning from MSDN though, "Avoid setting breakpoints on system components when you are debugging mixed-mode (native and managed) code. Setting a breakpoint on a system component during mixed-mode debugging can cause the common language runtime to break and the debugger to hang."
Function
Available in: Standard, Professional, Team Editions
Again, I don't find this one very beneficial.
Data
Available in: Express Standard, Professional and Team Editions for Native C++ only
A data breakpoint can only be set in Native mode, and causes the debugger to hit when the values stored in a specified memory location changes.
Condition
Available in: Standard, Professional, Team Editions
Conditions allow a breakpoint to only hit when certain variables in
the code evaluate to true. For instance, you could enter myColor == Color.Blue, if you happen to have a myColor variable.
Hit Count
Available in: Standard, Professional, Team Editions
Sometimes you just want to break after a certain number of cycles.
Go ahead and right click on your line 22 breakpoint, and choose Hit Count. You should see the dialog box below. Choose 'break when the hit count is equal to' and enter '7' in the count box. Run the program, and you will see that the debugger only breaks when the variable is equal to 7.
Filter
Available in: Express Edition with C++, Standard, Professional, Team Editions
Breakpoint filters allow you to specify that a breakpoint should only hit when it is accessed by a specific process or thread. This is a great feature for multi-threaded or component-based applications, but you do need to know the process, thread and/or machine name.
When Hit
When hit allows you to print a message, or run a macro when the breakpoint is hit. You can also choose whether to continue execution or not.
Summary
If you aren't using these advanced breakpoint features, you're missing out on some of the ease baked right into our favorite platform, Visual Studio.