DevOps Zone is brought to you in partnership with:

Gerard Davison is a Senior Principal Software Engineer working at Oracle in the UK on SOAP and REST tooling. Currently he is contributing in the area of WADL generation and client generation in the Jersey project and is maintaining the Abbot swing automation project. He also maintain a small holding of Hudson nodes run all those tests. He graduated from the University of Reading with a degree in Human Cybernetic and can't help looking for feedback loops. Gerard is a DZone MVB and is not an employee of DZone and has posted 32 posts at DZone. You can read more from them at their website. View Full User Profile

Post-Hoc Tracing Using a Debugger

02.03.2014
| 5535 views |
  • submit to reddit

One nice little feature of most debuggers that I have been exercising recently is the ability to log information on a breakpoint. This can be a really useful way to understand code without having to modify it or involve byte code modification.

Let's consider this very trivial and inefficient implementation of a function to return the n'th number in the Fibonacci sequence.

    public class Fib {  
      
      
      
        public long fib(long number) {  
            return number < 1 ? 0 :    // Breakpoint here  
                number < 2 ? 1 : fib(number - 2) + fib(number - 1);  
        }  
      
      
      
      
        public static void main(String[] args) {  
      
            Fib fib = new Fib();  
            System.out.println(fib.fib(10L));  
      
        }  
      
    }  

Now we add a simple breakpoint, I am going to modify it slightly so that it doesn't halt; but it will log a simple expression which gives us the current value of number

This gives you the following output for our rather inefficient code:

ebugger connected to local process.
Source breakpoint: Fib.java:11, evaluate(number)=10 (long)
Source breakpoint: Fib.java:11, evaluate(number)=8 (long)
Source breakpoint: Fib.java:11, evaluate(number)=6 (long)
Source breakpoint: Fib.java:11, evaluate(number)=4 (long)
Source breakpoint: Fib.java:11, evaluate(number)=2 (long)
Source breakpoint: Fib.java:11, evaluate(number)=0 (long)
Source breakpoint: Fib.java:11, evaluate(number)=1 (long)
Source breakpoint: Fib.java:11, evaluate(number)=3 (long)
Source breakpoint: Fib.java:11, evaluate(number)=1 (long)
Source breakpoint: Fib.java:11, evaluate(number)=2 (long)
... sometime later
Source breakpoint: Fib.java:11, evaluate(number)=1 (long)
Source breakpoint: Fib.java:11, evaluate(number)=2 (long)
Source breakpoint: Fib.java:11, evaluate(number)=0 (long)
Source breakpoint: Fib.java:11, evaluate(number)=1 (long)
55

So what we have done here is to add trace post-hoc, you might not even have access to the source and still be able to get useful information out of what the code is doing.

Now the obvious take away from the above example is that we are computing the same value over and over again. So here is another version of the code which instead uses a Map to store previously computed values in the sequence. Note also it is more of a pain to add trace to this code than in the previous case without making the Lambda less pretty.

    import java.util.HashMap;  
    import java.util.Map;  
      
    public class Fib {  
      
      
        Map<Long, Long> data = new HashMap<>();  
        {  
            data.put(1L, 1L);  
        }  
      
      
        public long fib(long number) {  
            return data.computeIfAbsent(number,  
                          n -> n < 1 ? 0 : fib(n - 2) + fib(n - 1)); // Breakpoint here  
        }  
      
      
      
      
        public static void main(String[] args) {  
      
            Fib fib = new Fib();  
            System.out.println(fib.fib(10L));  
      
        }  
      
    }  

It is worth noting two things here: first of all, your breakpoint log expression should be evaluating the value of the Lambda parameter n, and the second is that you should always put your expression on a new line so that future developers can easily breakpoint it. (Not matter now much smaller/ prettier/ clever it would look if you put the code on one).

So the trace output is now as follows, much better as each value is only computed the once.

Debugger connected to local process.
Source breakpoint: Fib.java:17, evaluate(n)=10
Source breakpoint: Fib.java:17, evaluate(n)=8
Source breakpoint: Fib.java:17, evaluate(n)=6
Source breakpoint: Fib.java:17, evaluate(n)=4
Source breakpoint: Fib.java:17, evaluate(n)=2
Source breakpoint: Fib.java:17, evaluate(n)=0
Source breakpoint: Fib.java:17, evaluate(n)=3
Source breakpoint: Fib.java:17, evaluate(n)=5
Source breakpoint: Fib.java:17, evaluate(n)=7
Source breakpoint: Fib.java:17, evaluate(n)=9
55

The screen grabs in this blog are from Jdeveloper; but similar features are available in Intelij, Netbeans and if you are a bit crafty you can get something similar in Eclipse.

Published at DZone with permission of Gerard Davison, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)