Debugging and counting breakpoint hits

2019-02-11 16:52发布

问题:

Sometimes when I examine a code I didn’t write, I launch eclipse in debug mode and use figures to understand a program. For example, if they are n items retrieved from the DB, it can be interesting to know that there’re n processed items in a service, etc.

When loops are used, things get more complicated: if we’re in a “while” loop, the number of execution is not defined and if there are alternatives, the flow of execution can greatly change.

For this purpose, I sometimes set a breakpoint in a portion of code and count how many times we reach it.

Of course, it’s not very convenient and I wonder if there is a way to count the number of breakpoint hits. I know that Eclipse can suspend execution after a fixed number of hits but I just want Eclipse to count them in a normal flow of execution.

I’d be glad to hear your opinions about it.

Thanks!

回答1:

You can add a condition into your breakpoint. The simplest one could look something like this:

System.err.println("Passing checkpoint");
return false;

You can also extend it by calling your own static class:

org.foo.StaticCounter.COUNTER++;
System.err.println("Passing checkpoint " + org.foo.StaticCounter.COUNTER);
return false;


回答2:

As an alternative to counting breakpoints, you could use a profiling tool, such as the one here.