views:

65

answers:

5

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.

+4  A: 

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.

Tejs
thanks! any clue how to accomplish this in eclipse? although now I know the terminology and that it's possible I can probably google it pretty easily
froadie
Unfortunately, no. I last used Eclipse some three to four years ago and barely remember it =/
Tejs
Ok, I found out how by trying the same technique :) Maybe I'll post it as a new answer. Thanks for the help
froadie
+1  A: 

Most IDEs allow for conditional break points for this very reason. In Visual Studio, you can right click on the red dot for the breakpoint in the margin and open the condition dialog from there. You can also get at the condition dialog from the breakpoint window in Visual Studio. I am not familiar with Eclipse.

Tom Cabanski
+2  A: 

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();
Allon Guralnek
cool! and is there any sort of method that can easily disable/enable debugging code when switching between development and production environments?
froadie
Yes. Use the #ifdef directive: `#ifdef DEBUG` <newline> `MessageBox.Show("This is a debug version!");` <newline> `#endif`. You can define new directives with `#define BETA_BUILD`.
Allon Guralnek
Also, all calls to the `Debug` class won't exist when building the Release configuration, because it has the `ConditionalAttribute` applied, which has a similar effect to `#ifdef`, only at a method or class level. You can apply the attribute to your own methods and classes.
Allon Guralnek
+2  A: 

Setting a conditional breakpoint in Eclipse (thanks for all the Visual Studio answers!):

Set a breakpoint. Right-click and choose "Breakpoint Properties...". Check "Enable Condition" and enter your condition code into the text area.

froadie
+1  A: 

If conditional breakpoints aren't supported by your IDE, add in an if statement and break inside of it.

if (variable == 3) {
    // Stub code to attach breakpoint.
    1 = 1;
}
Dean J