Eclipse conditional breakpoint - only after another breakpoint

I am debugging an application that makes many method calls. I want, for example, to 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 methodA only after methodB is methodB . I really don't want to change my code (say by inserting a boolean value and making a conditional breakpoint on that boolean ).

Is this possible in Eclipse? Or are there better options?

+6
source share
5 answers

A) Just use a hit count of 1000.

It works only if methodA inside the loop is NOT under some condition.

B) Use of the condition

Place a breakpoint on the first statement inside the method. Form. picture

[Here the method A == test and I put a breakpoint on line 14]

enter image description here

Right-click the breakpoint and select the Breakpoint properties option and add the condition below.

 StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace(); StackTraceElement e = stacktrace[2]; return (e.getLineNumber() == 9); 

Here 9 means the line number where the second call to method A (or test) is made. Pay attention to the same in the code and change this.

Check javadoc for StackTraceElement and instead of line number you can also use method name. then you can break only when you call the xyz function.

enter image description here

C) Wait for the eclipse Oxygen (4.7)

In the next version of eclipse, JDT will provide trigger points on breakpoints. So you can say that hit point y is ONLY if breakpoint x was reached before.

However, you can only pause a breakpoint on the [stacktrace] method stream.

Example: you can stop at a breakpoint only when the call flow:

methodA() --> methodB() --> methodC() --> methodD() NOT on

methodA() --> methodC() --> methodD() , etc.

See bug for more details. Add your comments / suggestions to this error.

+5
source

I use system properties for this.

Example

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 stop at it after calling methodB() , but without adding additional variables for the code or using counters.

Breakpoint for setting a property

First add a breakpoint inside methodB() and make this condition. In its state, we will set the system property to true . We do not want to stop inside methodB() , so the condition will return false :

 System.setProperty("enable.methodA.breakpoint", "true"); return false; 

See GIF below:

Adding the first condition to <code> methodB () </code>

Breakpoint that validates a property

Now add a breakpoint to methodA() , also with the condition. In this case, we first get the value of the set set property:

 String p = System.getProperty("enable.methodA.breakpoint", "false"); 

Then we analyze it as logical and return:

 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 out this step in the GIF below:

Add a bookmark that checks the system property.

Launch

Now, if we run this class in debug mode, the bookmark in methodA() will only be suspended after calling methodB() :

<code> methodA () </code> only pauses after <code>; methodB () </ code>

Disabling a breakpoint

If methodA() is called many, many times after methodB() , and we want to test it only once, we can eventually delete it. For example, suppose our main() method looks like this:

 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 return the false property so as not to stop at this breakpoint:

 String p = System.getProperty("enable.methodA.breakpoint", "false"); System.setProperty("enable.methodA.breakpoint", "false"); return Boolean.valueOf(p).booleanValue(); 

This, of course, is just a simple example - the sky is the limit with this trick.

+2
source

You can use a conditional breakpoint in eclipse for conditions based on variable values.

http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fbreakpoints%2Fref-condition_option.htm

+1
source

Just set a breakpoint on methodA , and when you hit this breakpoint, add a second breakpoint to methodB and continue.

0
source

What about?

 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(); //first breakpoint here? methodA(); //2nd breakpoint here? } 
0
source

Source: https://habr.com/ru/post/988764/


All Articles