Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enter debug mode when a condition is met?

Is there a way to enter debug mode when a certain condition is met?

For example let's say I would want to enter debug mode on the line on which i == 1 becomes true:

using System;

namespace ConditionalDebug
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var r = new Random();
            var i = r.Next(2);
            i += r.Next(2);
            i += r.Next(2);
            i += r.Next(2);
            i += r.Next(2);
            i = 1;
            Console.WriteLine(i);
        }
    }
}

I know it is possible to set conditional breakpoints like:

conditional breakpoint

But of course I couldn't use that since I would have to add a conditional breakpoint for each line in the code where the condition value might get changed and that would get very messy in a real application.

So, is there a way to globally set the condition i == 1 so that the debugger will break on the line on which the condition is met?

Thanks for your help!

like image 857
Răzvan Flavius Panda Avatar asked Jun 08 '12 09:06

Răzvan Flavius Panda


People also ask

How do I add a conditional breakpoint in Chrome?

Conditional breakpoints allow you to break inside a code block when a defined expression evaluates to true. Conditional breakpoints highlight as orange instead of blue. Add a conditional breakpoint by right clicking a line number, selecting Add Conditional Breakpoint , and entering an expression.

How do I start debug mode?

Run the program in debug modeClick the Run icon in the gutter, then select Modify Run Configuration. Enter arguments in the Program arguments field. Click the Run button near the main method. From the menu, select Debug.

How do I apply a conditional breakpoint in Visual Studio?

Right-click the breakpoint symbol and select Conditions (or press Alt + F9, C). Or hover over the breakpoint symbol, select the Settings icon, and then select Conditions in the Breakpoint Settings window.

How do I add a conditional breakpoint in GDB?

A conditional breakpoint insertion can be done by using one single command by using the breakpoint command followed by the word if followed by a condition. In the previous example, the breakpoint can be inserted with the line b 29 if (y == 999).


1 Answers

The short answer is 'No'

The long answer is 'Not really, but kinda'. There are things you can do to get close to the behaviour you want.

  • You can use the Watch window and manually step through code and see it change.
  • You can find all references on the variable and isolate all the statements that can change your variable and place break points.
  • You could do the above, but use a wrapper function (or make it a Property) and only set the variable's value through that function (or property setter) then you have a single point where you can set your conditional break-point to compare the old/new value.
  • You could put a break point on EVERY line and set the condition for all of them at once. You might be able to automate that by writing a VS Plug-in or Macro.
  • It's beyond me - but I'm sure it is possible to do something amazingly complex and awesome like write your own debugger and implement your 'Break whenever X Changes'.
like image 183
Rob P. Avatar answered Oct 13 '22 10:10

Rob P.