Tracing functions

The trace facility allows you to debug your program by following the execution of specified functions. When you turn on tracing for a function, the function’s name, arguments, and returned value are printed out while your program is executing.

uLisp allows you to trace up to three functions at a time. The commands trace and untrace allow you to turn on and off tracing for individual functions.

Example

As an example, consider this recursive function bisect, which bisects a list into two halves:

(defun bisect (n lis)
  (if (zerop n)
      (list nil lis)
    (let ((l (bisect (1- n) (cdr lis))))
      (list (cons (car lis) (first l))
            (second l)))))

It takes an integer, n, and a list, lis, and divides the list into two parts so the first half contains n elements:

> (bisect 3 '(a b c d e f g))
((a b c) (d e f g))

We can get an insight into how it works by turning on trace:

9444> (trace bisect)
(bisect)

The trace command returns a list of all the functions currently being traced. Now if we call bisect the calls are traced:

9444> (bisect 3 '(a b c d e f g))
0: (bisect 3 (a b c d e f g))
  1: (bisect 2 (b c d e f g))
    2: (bisect 1 (c d e f g))
      3: (bisect 0 (d e f g))
      3: bisect returned (nil (d e f g))
    2: bisect returned ((c) (d e f g))
  1: bisect returned ((b c) (d e f g))
0: bisect returned ((a b c) (d e f g))
((a b c) (d e f g))

Each line shows:

  • The depth of recursion; eg 2.
  • The equivalent function call at that depth; eg bisect with arguments 1 and (c d e f g), or:
  • The value returned at that depth; eg bisect returned ((c) (d e f g))

Tracing and untracing functions

To trace a function

To turn on tracing for a function call trace followed by the name of the function. For example:

9444> (trace join)
(join bisect)

The call returns a list of all the functions currently being traced.

You can trace up to three functions at a time.

To untrace a function

To turn off tracing for a fuction call untrace followed by the name of the function:

9444> (untrace join)
nil

The call to untrace will return nil.

To turn off tracing

To untrace all functions call untrace with no arguments.

9444> (untrace)
(bisect)

It returns a list of the functions untraced.


Previous: Backtrace

Next: Inserting breaks