Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to set or code breakpoints conditionally?

I've been wondering this for a while - is there a way to code/program breakpoints...? Conditionally? For example, can I specify something like - "when this variable becomes this value, break and open the debugger"? (Would be quite useful, especially in long loops when you want to debug loop execution of a late loop value.)

I suppose this may be IDE-specific since debugging is implemented differently in different IDEs... I'd be interested to know how to do this in any IDE, but specifically in Eclipse and Visual Studio.

like image 670
froadie Avatar asked Apr 28 '10 13:04

froadie


2 Answers

This is definitely possible in Visual Studio. You can normally click in the left margin to insert the breakpoint, and then right click on that breakpoint. One of the options in the right click menu is "Condition...", and you can specify a predicate that will tell the debugger only the break on that breakpoint if the predicate is met.

like image 148
Tejs Avatar answered Sep 19 '22 01:09

Tejs


In Visual Studio, you can declaratively set conditional breakpoint, which are like normal breakpoint but will only break when a certain condition is true. The condition can use local variables and whatever is accessible from where the breakpoint is set. Just right click any breakpoint (red dot) and select "Condition...".

In addition, .NET languages can call the Debugger.Break() method to programmatically break the execution. This also can be done within an if statement:

if (count > 8 && Debugger.IsAttached)
   Debugger.Break();
like image 35
Allon Guralnek Avatar answered Sep 20 '22 01:09

Allon Guralnek