Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse conditional breakpoint - only after other breakpoint

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?

like image 841
Rob Audenaerde Avatar asked Jun 09 '15 07:06

Rob Audenaerde


People also ask

How do you get to the next breakpoint in eclipse?

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 do I keep conditional Breakpoints in eclipse?

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.

How do you set conditional Breakpoints?

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.


2 Answers

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]

enter image description here

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.

enter image description here

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.

like image 89
Chandrayya G K Avatar answered Nov 14 '22 23:11

Chandrayya G K


I use system properties to achieve that.

The 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 only stop at it after calling methodB(), but without adding extra variables to code or using counters.

A breakpoint to set a property

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:

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

A breakpoint that checks the property

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:

Add bookmark that checks system property.

Running

Now, if we run this class in debug mode, the bookmark at methodA() will only pause after methodB() is called:

<code>methodA()</code> is only paused after <code>methodB()</code>

Disabling the breakpoint again

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.

like image 26
brandizzi Avatar answered Nov 15 '22 00:11

brandizzi