literg
literg

Reputation: 512

Stop on breakpoint when class present in stacktrace

Is there a way in Intellij to create conditional breakpoint when specific class is present in the stacktrace? Alternatively when specific breakpoint was passed in this stacktrace.

Example given: Let's imagine that we have some utility method Utils.doSomething() and I want to stop there only when this method was executed from SomeClass.doSomething().

I do know that there is 'Disable until breakpoint is hit' but this makes code execution stop there anytime it is hit after the initial condition is met.

Example:

class SomeClass {
    void doSomething() {
        int y = 1; // Breakpoint here
        Utils.doSomething();
        Utils.doSomething();
    }

    void doSomethingElse() {
        Utils.doSomething();
    }
}

static class Utils {
    static void doSomething() {
        int x = 1; // Breakpoint with condition here
    }
}

@Test
public void test() {
    SomeClass someInstance = new SomeClass();
    someInstance.doSomething();
    someInstance.doSomethingElse();
    someInstance.doSomething();
}

Breakpoint on int y = 1;

Breakpoint with condition on int x = 1;

It is stopping in Utils.doSomething() when executed first from SomeClass.doSomething() but it is not stopping when Utils.doSomething() is executed again on next line.

It is correctly not stopping when executed through SomeClass.doSomethingElse()

Upvotes: 6

Views: 823

Answers (3)

Joshua Goldberg
Joshua Goldberg

Reputation: 5333

You can use a single breakpoint with a breakpoint filter, though the documentation could use some improvement. You'd want a "Caller filter" for this case, and I find the easiest way to create it is to debug until you're stopped at that breakpoint (perhaps Force Run to Cursor) and then use breakpoint intentions to add the filter:

With the debugger is stopped at the breakpoint, make sure the cursor is on the line with the breakpoint if needed, and then use opt-Enter to get the inspections menu, which will now include options to Stop only if... or Do not stop if... using the immediate calling method and class in the stack.

enter image description here

You can also enter Caller filters directly from the breakpoint popup when it's expanded, but I find the semantics confusing there (include vs exclude in a filter to stop or not stop). The intentions make it very clear how to make the debugger stop/not stop in the given case.

Note, I don't think Caller filters work for the general case in the question's title (when you need to search back further than one step in the stack), but it will work for examples like the one in the question.

Upvotes: 0

literg
literg

Reputation: 512

I have came up with adding such condition, it is not ideal but does the job

Arrays.asList((Thread.currentThread().getStackTrace())).stream()
    .anyMatch(ste -> ste.declaringClass.equals("debugging.SomeClass"))

Upvotes: 5

AleksW
AleksW

Reputation: 713

I think what you're looking for is here:

https://www.jetbrains.com/help/idea/configuring-breakpoints.html

To set a breakpoint the current one depends on, select it from the Disabled until selected breakpoint hit drop-down list. Once dependency has been set, the current breakpoint is disabled until selected one is hit.

Choose Disable again radio button to disable the current breakpoint after selected breakpoint was hit.

Choose Leave enable radio button to keep the current breakpoint enabled after selected breakpoint was hit.

So it looks like you will need to set one on the SomeClass.doSomething() Line, then wherever you need it in the Utils.doSomething() method

Upvotes: 2

Related Questions