4.4.5 Example debugging session

This section presents a short interactive debugging session. It starts by defining a routine to calculate Fibonacci Numbers, and then erroneously calls it with a string.

First, define the
fibonacci
function shown below in a listener.

(defun fibonacci (m)

(let ((fib-n-1 1)

(fib-n-2 1)

(index 2))

(loop

(if (= index m) (return fib-n-1))

(incf index)

(psetq fib-n-1 (+ fib-n-1 fib-n-2)

fib-n-2 fib-n-1))))

Next, call the function as follows.

(fibonacci "turtle")

The system generates an error, since
=
expects its arguments to be numbers, and displays several continuation options, so that you can try to find out how the problem arose.

Type
:bq
at the debugger prompt to perform a quick backtrace. Notice that the problem is in the call to
fibonacci
.

Note that the calls to
*%apply-interpreted-function
in the backtrace occur because
fibonacci
is being interpreted.

You should have passed the length of the string as an argument to
fibonacci
, rather than the string itself.

Do this now, by typing the following form at the debugger prompt.

(legnth "turtle")

You intended to call
fibonacci
with the length of the string, but typed in
length
incorrectly. This takes you into the second level of the debugger. Note that the continuation options from your entry into the top level of the debugger are still displayed, and are listed after the new options. You can select any of these options.

Type
:a
to return to the top level of the debugger.

Type
:v
to display variable information about the current stack frame in the debugger.

The following output is displayed:

M : "turtle"

INDEX : 2

FIB-N-2 : 1

FIB-N-1 : 1

You need to set the value of the variable
m
to be the length of the string "turtle", rather than the string itself.

Type in the form below.

(setq m (length "turtle"))

In order to get the original computation to resume using the new value of
m
, you still need to handle the original error.

Type
:error
to remind yourself of the original error condition you need to handle.

You can handle this error by returning
nil
from the call to
=
, which is the result that would have been obtained if
m
had been correctly set.

Type
:c
to invoke the continue restart, which in this case requires you to return a value to use.

When prompted for a form to be evaluated, type
nil
.

This causes execution to continue as desired, and you can obtain the final result with no further problems.