You probably know some post of this kind. “n things about debugging” is a popular topic. For a good reason! I guess that I spent about 1 hour per day debugging my applications. That’s quite a lot time. This makes it even more important to know all the handy features, that makes your life easier and helps you to safe some time. And it justifies another post on this topic!

- First Tip: Don’t debug too much!

Crazy statement to start a post about debugging. ;-) But it must be said! Try to split up your complex logic into many isolated units and write Unit-Tests to check the correct behaviour of your code. I see it very often, that people are clicking through large web-applications, filling out multiple forms, switching to different pages in the flow, just to check a calculation logic on the last page and after that implementing big parts of the logic in the debug view. Before starting your tomcat always ask yourself: Is there a way to check the behaviour with a unit test? But you can find tons of good material about good code design out there, from here I will focus on a few eclipse debugging tips, which you maybe don’t know or you have forgotten over the time.

你应该看过一些如《关于调试的N件事》这类很流行的帖子 。假设我每天花费1小时在调试我的应用程序上的话,那累积起来的话也是很大量的时间。由于这个原因,用这些时间来重视并了解所有使我们调试更方便的功能。那能为你省下一些时间,也将会使你的生活更安逸、轻松。同时也表明其它关于此主题的帖子也是很有价值的。



- Breakpoint View : Conditional Breakpoints

Extreme useful if you are interested only in a special constellation of your application. For example if you want to see the 13th run in a loop or you are debugging functionality in an abstract super class and you only want to see one concrete implementation. You can setup the conditions in the breakpoint-view or with the contextmenu on the blue breakpoint-marker next to your code (“Breakpoint Properties”). You can choose to suspend when your code-snippet gets true or when the value of your snippet changes.
Conditional Breakpoints
Conditional Breakpoints II

- 断点视图 : 条件断点

如果你只对应用中的某部分感兴趣的话,这个功能非常有用。例如,如果你要在第13次循环的时候检查程序,或者在一个抽象父类中调试某些功能,而你只关注其中一个具体的实现。你可以在断点视图中设置条件,或者通过代码旁边的蓝色断点标记的右键菜单("Breakpoint Properties")设置。你可以在条件代码片段为true的时候暂停程序,或者当代码片段的值改变的时候挂起程序。

Conditional Breakpoints
Conditional Breakpoints II

- Variables View: Show Logical Structure

If you want to see the values of a Map or a List in the variables view, it’s not always that easy with the default setting of eclipse. If you are using a HashMap for example, you have to click through the physical entries and you are confronted with implementation details of a HashMap. But there is a small button above the variables – “Show Logical Structure”. Very handy, especially if you don’t have meaningful toString()-methods for the objects in your structure. My boss showed me this feature a few weeks ago. You know, he is the guy who is working with PowerPoint or Excel the most time. What a shame for a developer like me ;-)

Without “Show Logical Structure”
Logical Structure I
With “Show Logical Structure”
Logical Structure II

- 变量视图:展示逻辑结构

如果你需要在变量视图查看一个Map对象或者List对象的值,对于eclipse的默认设置,一般并不是那么容易。假设,你使用的是HashMap,你需要点击遍历各个实体条目,并面临各种HashMap的实现细节。但是,在变量视图上面有一个叫做“Show Logical Structure”的按钮。它非常有用,尤其当你的对象的toString()方法所展示的信息并不友好的时候。我的老大在几周前为我展示了这个功能。你知道,他经常跟PowerPoint或者Excel打交道。对于我这么一个开发人员,多丢脸哦 ;-)

未开启“Show Logical Structure” 
Logical Structure I 
开启“Show Logical Structure” 
Logical Structure II 

- Variables View: Change Value…

Instead of restarting your debug session with some slightly changed input data, let’s say entered in a web-form – you can also change the values of your variables directly during debugging. Sometimes you can safe some time and sometimes you can simulate some strange behaviour with that feature a bit easier.
Change Value

- Display View

Do you know the “Display View”? You can activate it during debugging via “Window” -> “Show View” -> “Display”. Now there should be a blank new view in your Eclipse. You can use this view to enter and evaluate new code. The code is executed within the context of the current debugging positions, which means that you can use all your variables and even content assist. To execute your code, just mark it and use the context-menu or CTRL+U (execute) or CTRL+SHIFT+I (inspect).
Display View

- 变量视图:更改变量值...


Change Value

- Display视图

你知道“Display视图”吗?你可以在调试过程中,通过“Window” -> “Show View” -> “Display”来激活它。现在,你的eclipse应该是一个空白的新视图。你可以使用这个视图,输入或者演算一些新的代码。这些代码在当前的调试位置的上下文环境中被执行,这意味着,你可以使用所有变量甚至是内容助手。要执行你的代码的话,只需标记它,并使用右键菜单或者CTRL+U(执行)或者 CTRL+SHIFT+I (检查).

Display View

- Navigation: Drop to Frame

I think everybody knows “Step Into”, “Step over” and maybe “Step return”. That are the basics to navigate through your code while you are debugging. I want to name 2 further ways to navigate, which I like very much. The first is “Drop to Frame”. With that feature you have the possibility to go back in time ;-) You can simply go to a point in your java stackframe where you have been before. It happens to me quite often, that I am debugging and then missing the point where I have to pay attention. With the “Drop to Frame”-Feature I can then run the code again very simple.
Drop to Frame

- Navigation: Step into Selection

The second one is “Step into Selection”. This is a pretty easy one, but not many people are using it. For that, you just have to press Ctrl+Alt and click on a method name, where you want to go. Very handy, very fast. Compared to the usual “Step Into” this is much better for example if you want to enter a method with many parameters and you one step trough all the parameter evaluations. “Run to line” is also a nice feature. Just place your cursor in front of the line where you want to stop and hit “CTRL+R”.
Step Into Selection

- 导航: Drop to Frame

我想任何人都知道“Step Into”, “Step over”,而且可能也知道“Step return”. 这些是调试时的基本导航功能。我想提两个我非常喜欢的导航的高级方法。第一个是“Drop to Frame”. 使用这个特性你可以及时回退 ;-)  你可以直接回退到运行过的java堆栈帧中某一帧。当我调试时,一不小心错过了某个关注的代码行时,我常常用这个回退一帧。使用“Drop to Frame”这个特性我可以简单的重新运行某帧代码。
Drop to Frame

- 导航: Step into Selection

第二个是“Step into Selection”. 这也是很简单,但很多人使用的一个。要使用它,你只需要按 Ctrl+Alt 同时点击一个你想运行到的方法名。非常方便,非常快速。相比“Step Into”这个非常方便,想想你像跟踪进一个有很多参数的函数,如果用“Step Into”你必须进入,退出每个参数计算过程,才能进入到你真正想进的函数。“Run to line”也是很棒的特性。只需要把光标放在你想运行到的哪一行前面,然后按“CTRL+R”就可以了。
Step Into Selection

- Navigation: Use your keyboard

You are faster, if you avoid to use the mouse. You should know at least the following Shortcuts:

  • F5 – “Step Into”
  • F6 – “Step Over”
  • F7 – “Step Return”
  • F8 – “Resume”
  • Ctrl+Shift+B – “Toggle Breakpoint”
  • Ctrl+Shift+I – “Inspect”


- Breakpoint View: Watchpoints

What the hell is changing this variable?! Sometimes it is usefull to create a watchpoint. Then the debugger stops, whenever the watched field is changed or just read – you can configure that. Just doubleclick on a field, then you can see the watchpoint in the Breakpoint View and edit the properties. You can even configure a hit count, means that the debugger only stops, when the entered hit count is reached. This also works for usual breakpoints.
Watch Point

- 导航器:使用键盘


  • F5 – “单步进入”
  • F6 – “单步执行并跳过”
  • F7 – “单步执行并返回”
  • F8 – “继续执行”
  • Ctrl+Shift+B – “添加断点”
  • Ctrl+Shift+I – “检查”

- 断点视图:Watchpoints

如果改变这个变量会有什么结果?! 有时候创建一个watchpoint对于调试会非常有用。调试器会停止,不管这个被观察的字段是被修改还是被读取 - 你可以自己来配置决定。你只需双击一个字段,然后你就可以在断点视图中看到watchpoint,并且可以编辑它的属性。你甚至可以一个访问次数,这意味着当变量被访问的次数达到这个数量的时候,调试器会停止。这对于普通的断点也适用。

Watch Point

- Human Readable Objects

The Variables View is using the toString-Method of your objects to display the value. Because of this it is very, very helpful
to provide good toString-implementations. The javadoc of the default-toString-implementation in java.lang.Object recommends the same:

* Returns a string representation of the object. In general, the * <code>toString</code> method returns a string that * "textually represents" this object. The result should * be a concise but informative representation that is easy for a * person to read. * It is recommended that all subclasses override this method. [...]

You can have a look at the ToStringBuilder in commons-lang. It provides some functionality to write – quote from the javadoc – “good and consistent” toString-methods.

Example from ToStringBuilder – Default
Example from ToStringBuilder – Multiline-Style
If you cannot modify the toString-implementation, for example if you are working with frameworks or you have to use a foreign API, it might be an option to create an “Detail Formatter” in Eclipse. To do that, you have to right click an object in the variables view and click on “New Detail Formatter…”. Then you can provide some code to display this type of Object in the future.
Detail Formatter

- 友好的可读的对象



你可以参考commons-lang中的 ToStringBuilder。它提供一些功能来编写 (引用自javadoc ) "良好并连贯” 的toString方法。


ToStringBuilder例子 – 多行文本风格

如果你不能修改toString实现,例如你现在使用框架或者一个外部的API,你可以有另外一个选择,在Eclipse中创建一个“Detail Formatter”。你需要在变量视图中右击一个对象,并点击“New Detail Formatter…”。然后你就可以输入一些代码,用于展现这个对象。