in

Platinum Bay

Peace, Love and Visual Studio Team System

.NETicated

Breakpoint Breakdown

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.

Comments

December 11, 2007 5:41 AM

Wow Steve, you've really been working on this :)  I wouldn't have thought there was so much to say about breakpoints!

And honestly, I was going to leave this comment even before I saw that it might win me a Zune :)

 

June 10, 2008 4:11 PM

I find it very frustrating that a data breakpoint can't be set in native code for a managed program using a mixed-mode assembly!

Daniel
 

July 27, 2008 6:25 AM

Hi,

Nice post.

I just wrote a detailed post on hit counts of breakpoints on my blog @

www.technochakra.com/debugging-using-breakpoint-hit-count-for-fun-and-profit

Hopefully you will find it useful.

 

August 11, 2008 6:48 AM

Thanks for your very useful info on breakpoint.

I'm looking for whether it's the truth that a mixed-mode application(in my case, App.exe is written in C#, with a pure-native C++ compiled as DLL) can be set a data breakpoint. I failed to do this in the VS2008 IDE, receiving the error message as :

Data breakpoints are not supported in the Common Language Runtime.

is it supported for my mixed-mode case?

slimzhao
 

Leave a Comment

(required )  
(optional )
(required )  
Add

About Steve

Steve Andrews has been working as a developer for more than 8 years. During this time, he has designed and developed applications in such widely varying areas as trust accounting, medical information management, supply chain management, and retail systems. He has firsthand developer experience with a variety of languages, including Java, VB, and .NET. Most recently, he has been immersed in SharePoint. He is currently employed at RDA Corporation in Philadelphia, PA, as a Software Engineer and a team member in the Architectural Guidance evangelism team. Steve is also an MTCS (x2), ICSOO, and .NET fanatic.
Powered by Community Server (Commercial Edition), by Telligent Systems
© Platinum Bay | Some Rights Reserved Creative Commons License

Disclaimer: The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion. Feel free to challenge me, disagree with me, or tell me I'm completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or annonymous comments) - so keep it polite, please.