Search code examples
javaeclipsedebuggingjdi

Eclipse: Inline breakpoints


Is there a way to set inline breakpoints (with a plugin) in the Eclipse IDE?

For example when multiple methods are called in the same line, is there a way to set a breakpoint for a specific method call in that line and not for the complete line?

foo(bar(), baz());
           ^

(In this example it would be possible to set a breakpoint at the definition of the method baz, but if it is called by other methods as well, then it might be annoying if you have to continue most of the time.)

If I understand it correctly, the Java Debug Interface would allow this, see Location#codeIndex().


Solution

  • foo(bar(), 
        baz());
    

    The easiest way to do this is to do a line break before baz() and place your breakpoint as normal on the line baz() is found on. In Java, surrounding a method call with a line break in this way, will not impact its evaluation.

    screenshot from eclipse

    Update:

    OP wants to select the invocation of baz and make a breakpoint on the evaluation of that expression itself without any extra conditions on your breakpoint or editing of the source code.

    As far as I know that is not possible with the standard debugging tool shipped with Eclipse.

    However, you can achieve this effect using conditional breakpoints. In Eclipse, you make or select a breakpoint as normal, then access the conditional options in the Breakpoints View.

    Access Breakpoint View through the menu item Window -> Show View -> Breakpoints. Once there, select or create your breakpoint, then check the conditional option. Then you must describe a conditional that suits your surrounding code and needs. Your breakpoint will trigger when the conditional is true. For example, by specifying the call-site line number or method name.

    Breakpoints View in Eclipse, with conditionals enabled

    I based my answer on this SO answer.

    In my own example code I have the following setup:

    a(){
      baz(); 
      m();
    }
    
    m(){
      foo(bar(), baz());
    }
    

    and my breakpoint is positioned in baz(). Adding the conditional statement

    Thread.currentThread().getStackTrace()[2].getMethodName().equals("m")
    

    to my breakpoint means that it triggers only when baz() is called from m(), not from a().