Print more logging messages during debugging

Sometimes, in debug mode,  more logging messages could be very helpful. We can change code to add more logs. But it’s not efficient as the code need to be re-compiled and later some log message may need to be removed after debugging.

Java debugger provides very powerful way to achieve this, which allows to print more messages and even more.

Take Eclipse for example, here I have a simple HelloWorld to calculate the sum from 0 to 99. In debugging, I want to see how sum changed in the loop but I don’t want to stop application and click “Continue” every time.

12-12-2014 5-31-25 PM

So first, I added a break point on line 8. Next, I edited the “Breakpoint Properties”. In the conditional text input, you can print some message first and make the return of expression as false.

12-12-2014 5-35-40 PM

To return false will make debugger not stop at line 8, but just print the log messages I want.

12-12-2014 5-37-57 PM

You can see it’s totally non-intrusive. Actually, you can put more complicated statements there and also you can control when to return true or false. It’s very flexible. In other IDEs, such as JetBrains IDEA and Netbeans, there are similar functions.

Add watchpoint for variables in Eclipse

Sometimes, in debugging, we want program stopped when some variables are accessed or modified. To set traditional breakpoints for this case, you have to spend a lot of time to find all places to access or modify variables. It’s even harder when variables are passed to other object or the derivation hierarchy is complicated. In this case, watchpoint is a good helper.

In Eclipse, it’s quite easy to set watchpoint. In “Outline” view, you can right click on the variable you want to want and select “Toggle Watchpoint“. 2014-11-06_13-21-43

Then you can find a new watchpoint in “Breakpoints” view. You can see the icons of breakpoint and watchpoint are different.

2014-11-06_13-24-02

By default, watchpoint will be hit when accessing or modifying the watched variable. To change it, we can simply right-click the watchpoing and select “Breakpoint Properties…“.2014-11-06_13-27-11