CSC300 / CSC402: Trace

Contents [0/5]

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

(Click here for one slide per page)


Programmatic Debugging [1/5]

With IntelliJ IDEA's debugger, it can be hard to visualize the whole state of the program.

Professor James Riely wrote the Trace class to draw pictures of your program as it executes.

Trace uses graphviz.

run and draw [2/5]

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 [3/5]

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 [4/5]

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 [5/5]

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.