Some questions on debugging in Idea.

Greetings, Gentlemen!

Here are some questions on debugging in Idea. Is it possible to accomplish this without modifying the code of an application?

1) I want to know how many times some line is executed.
2) I want to see the value of expression during the execution (*)
3) I want to stop the execution of my application not in the breakpoint, but only if some expression or variable is equal to some value (for example, stop app if this variable is null). How can I do it?

(*) probably I can use Watches for this. Idea debugger has this feature and I can see the value of some expression during the execution. But when the execution reaches that line second time an old value is cleared. How can I look at expression value in a dynamical way?

1 comment
Comment actions Permalink

1) To my knowledge, there is not a direct way to do this. You could accomplish this indirectly. In the IDEA editor, add a breakpoint on the line. Then right click the breakpoint, select edit and then more (alternatively view the breakpoints via ctrl+shift+F8 or Run > View Breakpoints. Then select the breakpoint in question.) Turn off "suspend" for the breakpoint. Turn on "Log message to console." This will log an expression such as "Breakpoint reached at com.example.Foo.main(Foo.java:21). You can do a Find (Ctrl+F) in the console for that text and it will show a count of the number of matches. The one down side here is that if your have a large amount of output, and the console output buffer starts to roll, you will have an inaccurate count. It's also a bit cumbersome to find the current count when at another breakpoint; you need to scroll through the console output.

A second alternative would be to create class with a static counter. (I'd use an AtomicInteger):

import java.util.concurrent.atomic.AtomicInteger;

public class LineCount
{
    public static AtomicInteger countForLineX = new AtomicInteger();
}


You could create this class outside the application code in a separate (small) module used just for debugging. Or even in a separate project and then just attach it as a dependency Library. For the break point in question, select the "Log Evaluated expression" option and add the statement
LineCount.countForLineX.incrementAndGet()
or for more detail:
"Line 22 in Foo.java reached. New count: " + LineCount.countForLineX.incrementAndGet()
This eliminates the worry about console wrapping. Plus, you can add a watch on the LineCount.CountForLineX field and always be able to see the current count as you are debugging.

2) Since you want to do this dynamically, you can set a breakpoint in an appropriate location. Then right click the breakpoint, select edit and then more (alternatively view the breakpoints via ctrl+shift+F8 or Run > View Breakpoints. Then select the breakpoint in question.) Optionally turn off suspending the breakpoint. Select the "Log evaluated expression" option and enter the desired expression to log.

3) Set the breakpoint.  right click the breakpoint and select edit (alternatively view the breakpoints via ctrl+shift+F8 or Run > View Breakpoints. Then select the breakpoint in question.) Make sure the "Condition" option is selected. Enter the condition that needs to be met in order for the application to be suspended. It myst be a boolean condition and can use any in-scope variables. For example, in a for loop with an counter of 'i', you could add a condition of  i % 10 == 0  to pause only every tenth loop. Or for the example you cite, foo == null.  Another option is you can make the breakpoint conditional on another breakpoint. For example, if both a constructor and a reset() method call the init() method, I can put a breakpoint in the reset method and one in the init() method. then make the one in the init() method conditional on the one in the reset method. (I can make the one in the reset method not suspend the code). Execution will only pause on the init() method if it is called via the reset method and not via a constructor.

One comment about combining 1 & 3 or 2 & 3... if there is a condition on a breakpoint, the logging only occurs when the condition is met.

0

Please sign in to leave a comment.