sharing:eclipse 调试技巧

除了普通的断点调试很仔细的讲解了

  1. 条件断点
  2. 异常断点
  3. 调用栈跳转
  4. 远程调试
  5. 运行时改值
原地址:

Effective Java Debugging with Eclipse

by Jordi Böhme Lópezeclipsesource.comJanuary 8th 2013

Learning to write code efficiently is only one step towards becoming a coding master. Another step is learning to debug. When it comes to legacy or 3rd party code you will need to debug in order to understand how the code behaves. In this post I will give you some basic tips on how to master your debugger and become more efficient.

This is a follow up to My Top 10 Tips on how to be more productive with the Eclipse IDE(which you should read before this one). I’ll assume you already know how to launch a Debug configuration for your project in your Workspace.

Note: Click on the images to reveal a larger screenshot.

1. Breakpoints

Basic Breakpoints


This is essential and you probably already know how to use it – the Breakpoint.

In case you’re not familiar with them, you create a breakpoint by double-clicking on the left side of a line of code. The execution of your application will then halt at that point so you can analyze the state and hopefully find the cause of your misbehaving code.

Exception Breakpoints


Sometimes an exception occurs, but you are not sure where (e.g. no stack trace available) or the exception is not logged and not visible. This is the right time to create a global breakpoint that pulls the hand brake if a certain exception occurs.

You’ll find this in the Breakpoints view of the Debug perspective.

Conditional Breakpoints


To speed up debugging by not stopping at a breakpoint everytime, you can add a condition. The breakpoint will only stop your application if the condition is fulfilled – and will not unnecessarily disturb your hunt for the bug.

You can get there with a right-click on your break point > Breakpoint Properties...

2. Detail Formatter


While debugging and inspecting variables/objects in your debug perspective, you might encounter textual representations of objects that are not really meaningful. Usually the .toString() method on an object is used for the debug UI. It’s not the greatest idea to implement the  .toString() method just for debugging purposes. Instead you can set up a detail formatter that takes care of the textual representation of your object.

You can get there by right-clicking on the variable in the Variables view, then selectingNew Detail Formatter....

3. Logic Structure


Here you can see what a difference a small click can make. Both Views show the same  Mapobject. By activating  Logic Structure in your  Variables view, you get the “contents” and structure of objects presented as you would expect it for a human reader.

4. Drop To Frame


In case your application stopped at your break point and the interesting things had already happened, you can “Drop To Frame”. This means you can jump back in your stack and then start debugging from there…

5. Inspect and Watch


Inspect provides you basically with the same functionality as the  Variables View – you can unfold and inspect the various values of the object.  Watch on the other hand enables you to observe a specific Object and see how it changes over time.

6. Change Variable Values


The Variables View can do more than just display values. You can also edit values. I don’t think there is much more to say about it

7. Step Into, Over and Return – Shortcuts

Try to memorize the following function keys. They will greatly speed up your debugging by avoiding constantly having to use your mouse.

  • F5 – Step Into
  • F6 – Step Over
  • F7 – Step Return
  • F8 – Run until next breakpoint is reached

8. Step Filtering


With “Step Filters” you can filter out types that you do not wish to see or step through while debugging. This helps you to stay focused on the actual code and does not expose you to the inner workings where you might not even have the source. Have a look at  Holger’s Effective Mockito post about Step Filtering in conjunction with Mockito.

You’ll find this in: Preferences > Java > Debug > Step Filtering

9. Remote Debugging


With Remote Debugging you can debug an application running on a different machine. The debugging itself is like any other app that you usually launch from within Eclipse. This is what you need to do:

  1. Make sure the source code on your machine and the application on the other machine are in sync.
  2. Start the application on the other machine with the following command line parameters:

    suspend=y/n declares whether the JVM of the application you want to debug will wait for Eclipse to connect or if it will start right away.
    address=5005 configures the TCP port to use for remote debugging.

  3. Create a Remote Java Application Debug Launch Configuration.
  4. Specify the Host and Port for your remote application.
  5. Add all the source projects of the application you want to debug to the Source tab.

10. Heap dump + Memory Analyzer

Sometimes you need a bigger picture of the inner workings of your application than you can get with line-by-line debugging – especially for finding memory leaks. In this case you are better off with the Eclipse Memory Analyzer (MAT). My colleague Ian wrote a blog post about how to create a heap dump and analyze it.

These are just a few tips regarding debugging. If you have feedback or something to add, please feel free to leave a comment below.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值