BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Discussing 5+ Ways to Trace Java Execution

Discussing 5+ Ways to Trace Java Execution

This item in japanese

Bookmarks
Zviki Cohen has published Five ways for tracing Java execution, based on his experience exploring and having to understand code he didn't write. He's found that simply reading source code (and in some cases decompiling), can be a very tedious and error prone process. Instead, he recommends five different runtime tracing methods to observe Java code while it is executing, eliminating many of the disadvantages of trying to learn someone else's code. Here is a condensed list of his five suggestions:

 

  1. The Basic: Breakpoints and Step-by-Step Execution "Start with the simplest way: set up breakpoints and start tracing your execution. It is best when: You need a quick and simple solution, you have all the code and you know where you want to stop. You need elaborate information in a given point (arguments, local variables, etc.)."
  2. The Primal: Debug Messages "We continue by setting up debug messages. The simplest way is to use System.out.println statements to print out messages to the console. It is best when: You own the code and you have a good idea of what you're looking for. Very good solution for event handlers. It's high performance makes it practical for understanding which event is fired when throughout the execution of complex flows."
  3. The Hot Shot: Dynamic Proxy "An improvement over simple debug messages. Dynamic Proxy is a special Java feature which enables the developer to introduce a proxy class, sitting in front of a given class and intercepting all the calls through a given interface. It is best when: It's a great solution for event handlers. You can set up a dummy event handler with a generic proxy in seconds and see the sequence of events. This is simplest and quickest method when it comes to understanding event handlers."
  4. The Brute Force: Run-time Profiler "Profilers are powerful tools that trace all the calls in the system through special JVM hooks. However, it's like using a 10 pound hammer on a half inch nail. It is best when: You want a complete picture for a very specific operation (i.e. very short execution flow)."
  5. The New Age: Aspects "Aspect Oriented Programming (AOP) is a non-trivial idea. Without going into the concept of Aspects, I'm just looking at the bottom line: it's a quick and easy way of intercepting the execution of your code. You can selectively set hooks around methods, constructors, field access, etc., without modifying the original code. In these hooks you can print debug messages. It is best when: You want to trace the execution of a code you can rebuild."

Until February 2007, Zviki Cohen was an architect and senior consultant for Amdocs, and is now a private entrepreneur in the software space.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • Does it qualify as anything?

    by Shay Banon,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    This type of blog posts always make me laugh. It is right up there with: If you want to perform a repeating operation N times, use a for loop.

  • Re: Does it qualify as anything?

    by Michael Krumlauf,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    While the contents of the post may seem obvious to an experienced developer, those with less experience may find it timely and helpful. I know that for me it never hurts to review the basics from time to time.

  • Re: Does it qualify as anything?

    by Shay Banon,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Start a debugger and do a step by step execution? Add debug messages? Common, there is a limit for "less experienced". Again, my "for loop example" stands.

  • Re: Does it qualify as anything?

    by Scott Delap,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    While it does seem trivial on one hand, I can't tell you how many programmers I've seen that don't know how to effectively use a debugger.

  • Where's DTrace?

    by Ralph Poellath,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Shouldn't DTrace at least be mentioned in tis list?

  • Re: Where's DTrace?

    by Alex Popescu,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Shouldn't DTrace at least be mentioned in tis list?


    I guess it should, but before that DTrace should be supported on multiple OSes, isn't it? (I've heard it was added to Mac OS Leopard, but I don't know if it runs on Linux or Win boxes).

    ./alex
    --
    .w( the_mindstorm )p.
    Alexandru Popescu
    Senior Software Eng.
    InfoQ Techlead/Co-founder

  • MaintainJ for Java execution trace

    by Choudary Kothapalli,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    MaintainJ uses AspectJ to capture the execution trace. It then generates runtime sequence and class diagrams from that trace. MaintainJ has a simple UI that works and shows what a developer needs to quickly understand complex applications.And it is very easy to get started with MaintainJ.

    Check the video demos at maintainj.com/userGuide.jsp?param=demos . Check the testimonials at www.maintainj.com/smf/index.php?board=6.0

    Regards,
    Choudary Kothapalli.

  • Aspects

    by Andrew Sutherland,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Aspects can actually be used to trace the execution of code without having to rebuild it if you use load-time weaving.

  • Another tool for that

    by Rejeev Divakaran,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    I have written a small tool for tracing method calls.
    Please see rejeev.blogspot.com/2009/04/method-tracing.html

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT