Tuesday, December 21, 2010

Command Line Debugging in Java ::: JDB

Ever wondered, how the IDE helps us in debugging the program , or our application by using breakpoints, watches, expressions etc.
In the world of java you can do it easliy by using jdb command line action provided by Java.. lets write down a simple program to perform command line debugging in java.
Lets write down a simple program to add two numbers in java.  
  1. import java.io.*;

  2. public class Sum

  3.  {
  4.    public static void main(String arfs[])
  5.    {

  6.      int i,j;
  7.      i=10;
  8.      j=25;
  9.      System.out.println(" I = " + i + " J = " + j);
  10.      int sum=i+j;
  11.      System.out.println("Sum = " + sum);

  12.    }
  13.  }
Now compile the above code by using
javac -g Sum.java
Here -g option is used to inject the debug information into the generated class files, This can easily be viewed by checking the file size using with and without "g" option. 

Start the Execution of this program by using following command

>java -Xdebug -agentlib:jdwp=transport=dt_s
hmem,address=9999,server=y,suspend=y Sum 
  the message "Listening for transport dt_shmem at address: 9999" is generated showing that the debugger is started with this program and ready to listen the request on the port no 9999. 

Open a separate window and type the following at the command prompt.
> jdb -attach 9999
  Following output will be generated
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
Initializing jdb ...

> VM Started: No frames on the current call stack
main[1] 

Now the debugger is ready to start debugging the program. We just need to pass the jdb specific commands to the debugger.
main[1]: This represents the current call stack on the heap where the execution is.
Now assume main[1] will be your command prompt for the time being.
1. Type cont 
 main[1] cont (PRESS ENTER)
output will be: 
> Set deferred breakpoint Sum.main

Breakpoint hit: "thread=main", Sum.main(), line=10 bci=0
10         i=10;

main[1]
2. Now Type "Stop at Sum:12" to instruct the debugger to stop the execution of the program at the 12 line.
main[1] Stop at Sum:12
Output will be:  Set breakpoint Sum:12
3. Now Type "cont" to continue with the execution of the code.
4. Now as per the program i and j has been initialized, so use the following command to view the values present in i and j.
main[1] print i 
output >>  i = 10
Similarly u can inspect the value of other variables that are being initialized till the execution of the line, otherwise you will get null
5. Now type "cont" to continue the execution.
  It will simply exit the debugger and will fall on to the main prompt.

so "cont" is being used to continue with the execution of the program
 "Stop at Sum:12" to stop the execution of program at a particular line.

 I hope it would be helpful for beginners, to understand how to debug your program through command line. 
Following are the screenshots attached.




For this tutorial i had used the tutorial from the following awesome links: