BT

Discussing 5+ Ways to Trace Java Execution

| by R.J. Lorimer Follow 0 Followers on Dec 07, 2007. Estimated reading time: 2 minutes |
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 Stage
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.

Tell us what you think

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

Email me replies to any of my messages in this thread

Does it qualify as anything? by Shay Banon

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

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

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

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

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

Re: Where's DTrace? by Alex Popescu

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

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

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

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

Email me replies to any of my messages in this thread

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

Email me replies to any of my messages in this thread

9 Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT