5.3 Example

The following example illustrates how trace may be used as a debugging tool. Suppose that you have defined a function f, and intend its first argument to be a non-negative number. You can trap calls to f where this is not true, providing an entry into the main debugger in these cases. It is then possible for you to investigate how the problem arose.

To do this, you specify a :break option for f using trace . If the form following this option evaluates to a non- nil value upon calling the function, then the debugger is entered. In order to inspect the first argument to the function f , you have access to the variable *traced-arglist* . This variable is bound to a list of the arguments with which the function was called, so the first member of the list corresponds to the first argument of f when tracing f .

CL-USER 12 > (defun f (a1 a2) (+ (sqrt a1) a2))
CL-USER 13 > (trace (f :break (< (car *traced-arglist*) 0)))
CL-USER 14 > (f 9.0 3)
0 F > (9.0 3)
0 F < (6.0)
CL-USER 15 > (f -16.0 3)
0 F > (-16.0 3)
Break on entry to F
  1 (continue) return from break.
  2 (abort) return to level 0.
  3 return to top loop level 0.
  4 Destroy process.
Type :c followed by a number to proceed

LispWorks User Guide - 8 Apr 2005