CSC300: Trace

Contents [0/6]

Video [1/6]
Programmatic Debugging [2/6]
run and draw [3/6]
drawSteps [4/6]
functions [5/6]
drawStepsOfMethod [6/6]

(Click here for one slide per page)


Video [1/6]

Open Playlist

Programmatic Debugging [2/6]

With Eclipse debugger, it can be hard to visualize the whole state of the program

I wrote the Trace class to draw pictures of your program as it executes

Trace uses graphviz

run and draw [3/6]

01
02
03
04
05
06
07
08
09
10
11
12
13
package algs11;
import stdlib.*;
public class Hello {
  public static void main (String[] args) {
    Trace.run ();
    int i = 0;
    while (i < 3) {
      Trace.draw ();
      StdOut.println (i);
      i = i + 1;
    }
  }
}

The stdlib package contains a class Trace which can be used to visualize the execution of programs.

Trace.run() causes the program to be run in a debugger.

Trace.draw() causes a drawing to be created. The drawings will be created in a folder on your Desktop. To change the location, or for further discussion, see here.

drawSteps [4/6]

01
02
03
04
05
06
07
08
09
10
11
12
13
package algs11;
import stdlib.*;
public class Hello {
  public static void main (String[] args) {
    Trace.drawSteps (); 
    Trace.run ();
    int i = 0;
    while (i < 3) {
      StdOut.println (i);
      i = i + 1;
    }
  }
}

Trace.drawSteps() causes a drawing to be created at every step of execution.

Trace.drawSteps() must be called before Trace.run()

functions [5/6]

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
package algs11;
import stdlib.*;
public class Hello {
  public static void main (String[] args) {
    Trace.drawSteps (); 
    Trace.run ();
    f (0);
    g (0);
  }
  public static void f (int i) {
    StdOut.println (i);    
  }
  public static void g (int i) {
    if (i < 3) {
      StdOut.println (i);
      g (i + 1);
    }   
  } 
}

Method calls are shown as red boxes

Method name and line number are listed

Arrow shows where the function will return to

drawStepsOfMethod [6/6]

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
package algs11;
import stdlib.*;
public class Hello {
  public static void main (String[] args) {
    Trace.drawStepsOfMethod ("f"); 
    Trace.drawStepsOfMethod ("g"); 
    Trace.run ();
    f (0);
    g (0);
  }
  public static void f (int i) {
    StdOut.println (i);    
  }
  public static void g (int i) {
    if (i < 3) {
      StdOut.println (i);
      g (i + 1);
    }   
  } 
}

drawStepsOfMethod() causes a drawing to be created at every step of the named method.

drawStepsOfMethod() can be called more than once.


Revised: 2008/03/17 13:01