12 The Debugger Tool

12.5 An example debugging session

To better understand how you can make use of the debugger, try working through the following example session. In this example, you define the factorial function, save the definition to a file on disk, compile that file and then call the function erroneously.

1. Choose File > New in the listener, or the podium window.

A new file is created and displayed in the editor. If you have not already invoked the editor, it is started for you automatically.

2. In the new file, define the functionfac to calculate factorial numbers.

(defun fac (n)
  (if (= n 1) 1
    (* n (fac (- n 1)))))

3. In the editor, choose File > Save and type a filename when prompted in the echo area.

4. In the editor, choose File > Compile and Load to compile the file and load it into the environment.

The editor switches to the output view while compilation takes place. When prompted, press Space to return to the text view. Thefac function is now available to you for use in the environment.

5. In the listener, callfac erroneously with a string argument.

(fac "turtle")

The environment notices the error: The arguments of= should be numbers, and one of them is not.

6. Choose Debug > Debugger to invoke the debugger tool.

Take a moment to examine the backtrace that is printed in the Backtrace area.

7. Starting from the selected frame, select the next three frames in the Backtrace area in turn to examine the state of the variables which were passed to the functions in each call frame. Pay particular attention to thefac function.

The error displayed in the Condition area informs you that the= function is called with two arguments: the integer 1 and the string "turtle". Clearly, the two arguments are not of the same type, and this has caused entry into the debugger. However, the arguments were passed to= byfac, and so the real problem lies in thefac function.

In this case, the solution is to ensure thatfac generates an appropriate error if it is given an argument which is not an integer.

8. Double-click on the lineFAC in the Backtrace area of the debugger tool.

The editor appears, with the cursor placed at the beginning of the definition for fac. Double-clicking on a line in the Backtrace area is a shortcut for choosing Frame > Find Source.

9. Edit the definition of thefac function so that an extraif statement is placed around the main clause of the function. The definition offac now reads as follows:

(defun fac (n) 
  (if (integerp n)
      (if (= n 1) 1
        (* n (fac (- n 1)))) 
    (pprint "Error: argument must be an integer")))

The function now checks that the argument it has been passed is an integer, before proceeding to evaluate the factorial. If an integer has not been passed, an appropriate error message is generated.

10. Choose File > Save and File > Compile and Load again, to save, recompile and load the new definition into the environment.

11. Click on the Abort button in the debugger tool, to destroy the tool and return the listener to the top level loop.

12. In the listener, type another call tofac, once again specifying a string as an argument. Note that the correct error message is generated.

This next part of the example shows you how you can use the various restarts which are listed as commands in the Restarts menu.

1. Callfac again with a new argument, but this time type the wordlength incorrectly.

(fac (legnth "turtle"))

2. Choose Debug > Debugger to invoke the debugger tool.

You can spot immediately what has gone wrong here, so the simplest strategy is to abort the debugger and callfac again with a correct argument.

3. Click Abort in the debugger tool.

4. Evaluate the following form in the listener.

(fac (length "turtle"))

This time, the call tofac returns the correct result.

Common LispWorks User Guide, Liquid Common Lisp Version 5.0 - 18 OCT 1996

Generated with Harlequin WebMaker