I'm debugging an application that does a lot of method calling. I want to, for example, debug methodA
. It is called 1000 times.
But, in my main loop, I only want to start debugging method A after a few statements.
public void methodA()
{
//does something nasty that I want to debug
}
public static void main( String[] args )
{
for (int i=0; i<1000; i++)
{
methodA();
}
methodB();
methodA();
}
I want to start breaking in methodA
only after methodB
is called. I don't really want to change my code (say, inserting a boolean
value and making the breakpoint conditional on that boolean
).
Is something like this possible in Eclipse? Or are there better options?
You can proceed to the next break point by pressing the resume(play) button or press F8. It is not true that you just hit play. If you're on the Java window pressing play, or as put "the green button" runs the program again without debugging it. If you like to use the buttons, ensure you're on the correct window.
How to create a conditional breakpoint. To create a conditional breakpoint, first set a breakpoint on a line (Ctrl+Shift+B), right-click on the breakpoint and select Breakpoint Properties. Then configure a condition that returns a boolean. Eclipse only stops if the conditions returns true.
To set a conditional breakpoint, activate the context menu in the source pane, on the line where you want the breakpoint, and select “Add Conditional Breakpoint”. You'll then see a textbox where you can enter the expression. Press Return to finish.
A) Simply use hit count as 1000.
Its works only if the methodA inside for loop is NOT under some condition.
B) Using condition
Put the break point at the first statement inside methodA.Refer image
[Here methodA == test and I put break point at line number 14]
Right click on the breakpoint and select Breakpoint properties
option and add the below condition.
StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
StackTraceElement e = stacktrace[2];
return (e.getLineNumber() == 9);
Here 9 means the line number where second call to methodA(or test) is made.Find out the same in your code and change this.
Check the javadoc for StackTraceElement and instead of line number you can also use method name also. i.e you can break only when it is calling from the function xyz
.
C) Wait for eclipse Oxygen (4.7)
In next version of eclipse JDT will provide trigger points on breakpoints. So you can say hit breakpoint y ONLY IF breakpoint x was hit before.
With this you can pause on a breakpoint ONLY on a given methods flow [stacktrace].
Ex: You can stop on a breakpoint only when call flow is:
methodA() --> methodB() --> methodC() --> methodD()
NOT on
methodA() --> methodC() --> methodD()
etc.
See this bug for more details. Add your comments/suggestion to this bug.
I use system properties to achieve that.
Suppose we have this class:
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 1000; i++) {
methodA();
}
methodB();
methodA();
}
private static void methodA() {
System.out.println("A");
}
private static void methodB() {
System.out.println("B");
}
}
We want to add a breakpoint inside methodA()
, but only stop at it after calling methodB()
, but without adding extra variables to code or using counters.
First, let's add a breakpoint inside methodB()
and make it condition. In its condition, we will set a system property to true
. We do not want to pause inside methodB()
, so the condition will return false
:
System.setProperty("enable.methodA.breakpoint", "true");
return false;
See the GIF below:
Now, we add a breakpoint to methodA()
, also with a condition. In the condition, we first get the value of the set system property:
String p = System.getProperty("enable.methodA.breakpoint", "false");
Then, we parse it as a boolean and return it:
return Boolean.valueOf(p).booleanValue();
(Note that the default value is "false"
, so the breakpoint will not pause if the property is not set.)
Check this step in the GIF below:
Now, if we run this class in debug mode, the bookmark at methodA()
will only pause after methodB()
is called:
If methodA()
is called many, many times after methodB()
and we only want to check it once, we can unset the property eventually. For example, suppose our main()
method is like this one now:
public static void main(String[] args) {
for (int i = 0; i < 1000; i++) {
methodA();
}
methodB();
methodA();
for (int i = 0; i < 1000; i++) {
methodA();
}
}
We can use this condition, where we set the property back to false so to avoid stopping at this breakpoint again:
String p =
System.getProperty("enable.methodA.breakpoint", "false");
System.setProperty("enable.methodA.breakpoint", "false");
return Boolean.valueOf(p).booleanValue();
This is, of course, only a simple example — the sky is the limit with this trick.
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