Is it possible to set a breakpoint so that the program stops executing after an instruction has made certain condition true
?
A class has a variable named currency
and I want to make a breakpoint to make the program stop at any line after currency == 20
. I do not know the line number where currency
is changed, so this would be like putting a breakpoint in every line of my class [ed. where currency
gets changed].
Is there any way to accomplish that (besides adding a breakpoint at each line)?
I am using NetBeans, but a solution in any other IDE is welcome.
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.
Set line breakpointsClick the gutter at the executable line of code where you want to set the breakpoint. Alternatively, place the caret at the line and press Ctrl+F8 .
There are six types of breakpoints as follows: Automatic. Conditional. Request.
A more sophisticated approach uses dynamic (or "conditional") breakpoints. These are not bound to a particular line, but rather to a particular situation. When you run it, the PowerShell debugger will automatically stop the script whenever a new value is assigned to the variable $a.
Here is a link on setting conditional breakpoints in NetBeans: http://www.java-hair.com/?p=61
The relevant text:
In NetBeans, create a breakpoint, the right click on the little pink square that signifies the break. Click on “Customize”. When the customize dialog comes up, check “Condition” and fill in the condition. Above is an example of this dialog in NetBeans.
They also cover Eclipse and JDeveloper.
Edit: in response to your comment - no, this can't be done the way you want it. The way a debugger sets a breakpoint is by changing a byte in the instruction where the breakpoint is set. When the instruction is evaluated, the presence of that byte transfers control to the debugger, who replaces the byte with whatever was there before. When execution is resumed, the debugger moves the instruction pointer to the instruction where the breakpoint was set.
A conditional breakpoint on a single line is then easy to implement - when control transfers to the debugger, he simply checks the condition, and if it is not true then he resumes execution automatically.
So, how would it work if you didn't have to attach the conditional breakpoint to a line? It would have to be attached to every single line (just as you surmised you would need to do to achieve this effect otherwise). I haven't had the need to do this, but I imagine it would be undesirable as it would slow program execution considerably. A better approach might be to use your IDE to search through the code for all instances of currency to see where it might be set, and put conditional breakpoints around there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With