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.

1. First, define the ``` fibonacci``` function shown below in a listener.
2. `(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))))`
3. Next, call the function as follows.
4. ``` (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.

5. Type ``` :bq``` at the debugger prompt to perform a quick backtrace. Notice that the problem is in the call to ``` fibonacci``` .
6. 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.

7. Do this now, by typing the following form at the debugger prompt.
8. ``` (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.

9. Type ``` :a``` to return to the top level of the debugger.
10. Type ``` :v``` to display variable information about the current stack frame in the debugger.
11. 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.

12. Type in the form below.
13. ``` (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.

14. Type ``` :error``` to remind yourself of the original error condition you need to handle.
15. 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.

16. Type ``` :c``` to invoke the continue restart, which in this case requires you to return a value to use.
17. When prompted for a form to be evaluated, type ``` nil``` .
18. This causes execution to continue as desired, and you can obtain the final result with no further problems.

LispWorks User Guide - 11 Mar 2008