7

I'm using Visual Studio 2008 on C# code.

I would like to only break on a breakpoint if another breakpoint has been hit (and broken upon.) Is there a way of doing that?

I would imagine as a subproblem it would be nice to get access to the information that the debugger has.

The rationale for this is I'm interested in only breaking on a certain breakpoint given a certain callstack (and at a certain point in the execution of one of those functions in the callstack). Perhaps I should be using the callstack instead? Another reason is it would be interesting to have programmatic access to the stuff that the debugger knows about.

Thanks.

user420667
  • 6,552
  • 15
  • 51
  • 83

1 Answers1

7

Using local variable

The easiest way to create such a conditional breakpoint would be to create a new thread-static variable (or just static if it should be global). Suppose that our code looks as follows:

class Program
{
#if DEBUG
    [ThreadStatic]
    static int breakVariable = 0;
#endif

    static void Main(string[] args)
    {
        TestMethod2();

        TestMethod1();
        TestMethod2();

        TestMethod2();

        TestMethod1();
        TestMethod2();
    }

    static void TestMethod1()
    {
        Console.WriteLine("test1");
    }

    static void TestMethod2()
    {
        Console.WriteLine("test2");
    }
}

Let's now assume that you set a breakpoint1 on Console.WriteLine("test1"); and breakpoint2 on Console.WriteLine("test2");. You would like to break at the breakpoint2 only when the breakpoint1 was hit 2 times. In this case you would need to set Hit Count... property of the breapoint1 to break when the hit count is equal to 2. Then in the When Hit... property check Print a message and in the textbox type: {breakVariable = 1}:

when break

Then set the property Condition... of the breakpoint2 to breakVariable == 1 and check Is true:

condition

If you would like the breakpoint2 to become inactive after being hit you may again use When Hit... property setting its Print a message value to {breakVariable=0}.

Using macro

This approach is much harder especially if you don't like VBA (like me:) ) but you may be interested as it does not require any changes in the application code. Let's define two macros:

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics

Public Module Module1

    Public Sub SetMyBreakpoint()
        Dim bps As EnvDTE.Breakpoints
        bps = DTE.Debugger.Breakpoints.Add(File:="C:\MyProject\ConsoleApplication1\Program.cs", _
                                           Line:=25)
        Dim bp As EnvDTE80.Breakpoint2
        For Each bp In bps
            bp.Tag = "mytag"
            ' Add this line only if you want the breakpoint to be removed on hit
            ' Although I don't know why - it does not always worked :(
            bp.Macro = "Macros.MyMacros.Module1.RemoveMyBreakpoint"
        Next
    End Sub

    Public Sub RemoveMyBreakpoint()
        Dim bp As EnvDTE.Breakpoint
        For Each bp In DTE.Debugger.Breakpoints
            If (bp.Tag = "mytag") Then
                bp.Delete()
            End If
        Next
    End Sub
End Module

Now for the breakpoint1 you still want to set the Hit Count... property as previously but now in the When Hit... property instead of checking Print a message check Run a macro and select the SetMyBreakpoint procedure. It's very important that you provide the full path of the code file in the Breakpoints.Add method and the correct line (you may check the API to find other ways to set a breakpoint, like on the function instead a code file). One small caveat here - I observed that the automatic removal of the second breakpoint does not always worked - but maybe it was my Visual Studio.

Using call stack

You may again use the Condition property of the breakpoint - have a look at this question to find some details.

Community
  • 1
  • 1
Sebastian
  • 3,764
  • 21
  • 28
  • 2
    I'm sorry your answer won't receive more upvotes as my questions don't draw enough attention. You definitely deserve more. The macro thing is exactly what I was looking for. Should I accept now or wait to draw more attention? :-P – user420667 Dec 08 '11 at 18:22
  • 1
    Haha thanks:) But no problem with the lack of attention. I'm glad that I helped and I would be thankful if you could accept the answer. – Sebastian Dec 08 '11 at 21:04