Eclipse conditional breakpoint - only after other

2019-02-09 16:25发布

问题:

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?

回答1:

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.



回答2:

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:

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:

Running

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

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.



回答3:

You could use conditional breakpoint in eclipse for variable value based conditions.

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



回答4:

Just put a breakpoint on methodA, and when you hit that breakpoint, add your second breakpoint to methodB and continue execution.



回答5:

How 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?
}