logo

Attribute Tracing

JastAdd attribute evaluation can be traced, for example to compute dependencies between attributes or to profile attribute evaluation.

Tracing is enabled by adding the tracing option to the JastAdd command. For example,

$ ./jastadd2 --tracing=compute ...

This generates a new class with the tracing API as an inner class ASTState.Trace. If you need to compile code that uses the tracing API but want to disable tracing to avoid tracing overhead, run JastAdd with --tracing=api - this leaves the ASTState.Trace class as is, but tracing events will not be generated.

Tracing events are handled by a trace receiver, that is, an instance of the functional interface ASTState.TraceReceiver. The trace receiver needs to be registered by calling ASTState.Trace.setReceiver(TraceReceiver).

To implement a trace receiver, write a class which implements the accept method of the TraceReceiver interface. For example:

public class MyReceiver implements ASTState.Trace.Receiver {

  @Override
  public void accept(ASTState.Trace.Event event, ASTNode node, String attribute,
      Object params, Object value) {
    switch (event) {
      case COMPUTE_BEGIN: {
        // ...
        break;
      }
      case COMPUTE_END: {
        // ...
        break;
      }
    }
  }
}

The tracing framework can also trace attribute memoization. See the command line help for available flags to the tracing option and see the generated class ASTState.Trace for all available trace events.

An example of how one can add tracing to an existing JastAdd application can be seen in the tracing branch of ExtendJ.

Using Tracing Information

Tracing can be used to profile attribute evaluation or to optimize memoization. A memoization optimization algorithm based on an older version of the tracing framework is presented in this article: