9 The Tracer

9.1 Simple tracing

At any time there will be a set of functions (and macros and methods) which are being monitored via the tracer. When a call to one of these functions is made, the tracer normally prints the function's name, arguments and results. More generally, you can specify that particular forms should be executed before or after entering a function, or that certain calls to the function should cause it to enter the debugger. Tracing of a function continues even if the function is redefined.

You can request that a function be traced by using the macro trace in the listener. The names of the functions, macros or generic functions concerned are supplied as arguments to trace. In addition it is possible to restrict tracing to a particular method, rather than a generic function, by specifying the requisite classes for the arguments in the call to trace.

The tracer handles recursive and nested calls to the traced functions. Consider the following example, where a function to calculate factorials is traced:

CL-USER 1 > (defun fac (n)
             (if (= n 1) 1
                 (* n (fac (- n 1)))
             )
            )
FAC

CL-USER 2 > (trace fac)
FAC

CL-USER 3 > (fac 3)
0 FAC > (3)
  1 FAC > (2)
    2 FAC > (1)
    2 FAC < (1)
  1 FAC < (2)
0 FAC < (6)
6

CL-USER 4 >

Upon entry to the outermost call to fac, the tracer prints the level of tracing -- the number of recursive entries to trace -- that has been reached, the function concerned and its argument at the current call. Successive calls produce the appropriate output, each time indented further to help you identify the different calls more easily.

As the function is exited, its results are printed out in an similar fashion, with the indentation appropriate to the call.

Note the > and < symbols printed after function names. The > symbol denotes entry to a function, and the < symbol denotes exit.

Calling trace without arguments produces a list of all the functions currently being traced:

CL-USER 4 > (trace)
(FAC)

CL-USER 5 >

To stop tracing a function, use the macro untrace. The appropriate function names are supplied as arguments.

CL-USER 5 > (untrace fac)
NIL

CL-USER 6 > (fac 4)
24

CL-USER 7 >

Note: All tracing can be disabled by calling untrace with no argument.


FreeLisp User's Guide - 5 FEB 1996

Generated with Harlequin WebMaker