up

Using Debuggers

Debuggers provide the means to follow the execution of programs.

  1. Download the ‘debug.java’ program, and issue the following commands in the given order.

    javac -g debug.java
    The -g flag provides for extra debugging information.
    jdb debug
    This command activates the debugger on the program.
    stop in debug.main
    This command requests a breakpoint in execution when the ‘main’ command is reached.
    stop at debug:7
    This command requests a breakpoint in execution when line 8 is reached
    run
    The command gets the execution started.
    cont
    This command asks the resumption of the execution resumed at breakpoints.
    list
    Lists a segment of the program and shows where the control is.
    next
    Execute the next line
    print k
    Shows the value of ‘k’.
    dump a
    A variant of the ‘print’ instruction, useful mainly for variables holding an object.
    clear
    Erase the existing breakpoints
    stop in Cls.cmethod
    This command requests a breakpoint in execution when the ‘main’ command is reached.
    where
    Shows the execution stack.
    up
    where
    down
    where
    help
    exit
    debug.java
     class debug {
       public static void main(String args[]){
         Cls a;
         int k;
         a = new Cls();
         k = a.imethod();
         for( ; k > 0; k=k-1 ){
            if( k == 5 ){ break; }
         }
         Cls.cmethod( k );
       }
     }
     class Cls{
       int i=1;
       static int j=2;
       int imethod(){
         i =  i + j;
         return i;
       }
       static void cmethod( int x ){
         j = j + x;
       }
     
     }

  2. Try figuring out the execution of the following program.
    class fact { 
       public static void main(String [] args ){ 
         System.out.println( f(6) );               // 3 
       } 
       static int f (int n ){ 
         int v; 
         if( n < 2 ){ v = 1; }                     // 7 
         else       { v = n * f(n-1); }            // 8 
         return v;                                 // 9 
       } 
    } 
     

up