Use one checkpoint counter for another checkpoint condition

I am using Visual Studio 2008 for C # code.

I would only like to break the breakpoint if another breakpoint was deleted (and broken). Is there any way to do this?

I would suggest that as a subtask, it would be nice to access the information that the debugger has.

The rationale for this is that I am only interested in breaking a certain breakpoint with a certain call (and at a certain moment when one of these functions is executed in a freeze frame). Perhaps I should use callstack instead? Another reason is that it would be interesting to have programmatic access to materials that the debugger knows about.

Thanks.

+6
source share
1 answer

Using a 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 our code is 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"); } } 

Suppose now that you set breakpoint1 to Console.WriteLine("test1"); and breakpoint2 on Console.WriteLine("test2"); . You want to break breakpoint 2 only when breakpoint 1 has been deleted 2 times. In this case, you will need to set the Hit Count... property for breapoint1 to break when the hit count is equal to 2 . Then in When Hit... check the Print a message property and in the text box: {breakVariable = 1} :

when break

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

condition

If you want breakpoint2 to become inactive after a hit, you can use the When Hit... property again by setting its Print a message value to {breakVariable=0} .

Macro use

This approach is much more complicated, especially if you do not like VBA (for example, I :)), but you may be interested, since it does not require any changes in the application code. Let two macros be defined:

 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 breakpoint 1, you still want to set the Hit Count... property as before, but now in the When Hit... property, instead of checking Print a message check Run a macro and select the SetMyBreakpoint procedure. It is very important that you provide the full path to the code file in the Breakpoints.Add method and the correct line (you can check the API to find other ways to set a breakpoint, for example, in a function instead of a code file). One small caveat here - I noticed that automatically deleting a second breakpoint does not always work - but maybe it was my Visual Studio.

Using the call stack

You can use the Condition property of breakpoints again - look at this question to find some details.

+7
source

Source: https://habr.com/ru/post/903340/


All Articles