0.8.14.13: Step SBCL, step!
[sbcl.git] / doc / manual / debugger.texinfo
1 @node Debugger
2 @comment  node-name,  next,  previous,  up
3 @chapter Debugger
4 @cindex Debugger
5
6 The SBCL debugger (as the CMUCL debugger it was derived from) has very
7 good support for source-level debugging of compiled code.  Although
8 some other debuggers allow access of variables by name, this seems to
9 be the first Lisp debugger that:
10
11 @itemize
12
13 @item
14 Tells you when a variable doesn't have a value because it hasn't been
15 initialized yet or has already been deallocated, or
16
17 @item
18 Can display the precise source location corresponding to a code
19 location in the debugged program.
20
21 @end itemize
22
23 These features allow the debugging of compiled code to be made almost
24 indistinguishable from interpreted code debugging.
25
26
27 @menu
28 * Starting the Debugger::       
29 * The Debugger Command Loop::   
30 * Controlling Printing in the Debugger::  
31 * Stack Frames::                
32 * Variable Access::             
33 * Source Location Printing::    
34 * Debugger Policy Control::     
35 * Exiting Commands::            
36 * Information Commands::        
37 * Function Tracing::            
38 * Single Stepping::             
39 @end menu
40
41 @node  Starting the Debugger
42 @comment  node-name,  next,  previous,  up
43 @section Starting the Debugger
44
45 The debugger is an interactive command loop that allows a user to examine
46 the function call stack.  The debugger is invoked when:
47
48 @itemize
49
50 @item
51 A @code{serious-condition} is signaled, and it is not handled, or
52
53 @item
54 @code{error} is called, and the condition it signals is not handled,
55 or
56
57 @item
58 the debugger is explicitly entered with the Lisp @code{break} or
59 @code{invoke-debugger} functions.
60
61 @end itemize
62
63 When you enter the TTY debugger, it looks something like this:
64
65 @example
66 debugger invoked on a TYPE-ERROR in thread 11184:
67   The value 3 is not of type LIST.
68 restarts (invokable by number or by possibly-abbreviated name):
69   0: [ABORT   ] Reduce debugger level (leaving debugger, returning to toplevel).
70   1: [TOPLEVEL] Restart at toplevel READ/EVAL/PRINT loop.
71 (CAR 1 3)[:EXTERNAL]
72 0]
73 @end example
74
75 The first group of lines describe what the error was that put us in
76 the debugger.  In this case @code{car} was called on @code{3}.  After
77 @samp{restarts} is a list of all the ways that we can restart
78 execution after this error.  In this case, both options return to
79 top-level.  After printing its banner, the debugger prints the current
80 frame and the debugger prompt.
81
82 When the debugger is invoked by a condition, ANSI mandates that the
83 value of @code{*debugger-hook*}, if any, be called with two arguments:
84 the condition that caused the debugger to be invoked and the previous
85 value of @code{*debugger-hook*}. When this happens,
86 @code{*debugger-hook*} is bound to NIL to prevent recursive
87 errors. However, ANSI also mandates that @code{*debugger-hook*} not be
88 invoked when the debugger is to be entered by the @code{break}
89 function. For users who wish to provide an alternate debugger
90 interface (and thus catch @code{break} entries into the debugger),
91 SBCL provides @code{sb-ext:*invoke-debugger-hook*}, which is invoked
92 during any entry into the debugger.
93
94 @include var-sb-ext-star-invoke-debugger-hook-star.texinfo
95
96
97 @node  The Debugger Command Loop
98 @comment  node-name,  next,  previous,  up
99 @section The Debugger Command Loop
100 @cindex Evaluation, in the debugger
101
102 The debugger is an interactive read-eval-print loop much like the normal
103 top-level, but some symbols are interpreted as debugger commands instead
104 of being evaluated.  A debugger command starts with the symbol name of
105 the command, possibly followed by some arguments on the same line.  Some
106 commands prompt for additional input.  Debugger commands can be
107 abbreviated by any unambiguous prefix: @command{help} can be typed as
108 @samp{h}, @samp{he}, etc.  For convenience, some commands have
109 ambiguous one-letter abbreviations: @samp{f} for @command{frame}.
110 @comment FIXME: what does that last bit mean?
111
112 The package is not significant in debugger commands; any symbol with the
113 name of a debugger command will work.  If you want to show the value of
114 a variable that happens also to be the name of a debugger command, you
115 can use the @command{list-locals} command or the @code{sb-debug:var}
116 function, or you can wrap the variable in a @code{progn} to hide it from
117 the command loop.
118
119 The debugger prompt is ``@code{@var{frame}]}'', where @var{frame} is
120 the number of the current frame.  Frames are numbered starting from
121 zero at the top (most recent call), increasing down to the bottom.
122 The current frame is the frame that commands refer to.  The current
123 frame also provides the lexical environment for evaluation of
124 non-command forms.
125
126  The debugger evaluates forms in the lexical environment of the
127 functions being debugged.  The debugger can only access variables.
128 You can't @code{go} or @code{return-from} into a function, and you
129 can't call local functions.  Special variable references are evaluated
130 with their current value (the innermost binding around the debugger
131 invocation) -- you don't get the value that the special had in the
132 current frame.  For more information on debugger variable access, see
133 @ref{Variable Access}.
134
135
136 @node Controlling Printing in the Debugger
137 @comment  node-name,  next,  previous,  up
138 @section Controlling Printing in the Debugger
139
140 In the debugger, it is possible to override the printing behaviour of
141 the REPL.
142
143 @defvr {Variable} sb-debug:*debug-print-variable-alist*
144
145 An association list describing new bindings for special variables
146 (typically *PRINT-FOO* variables) to be used within the debugger, e.g.
147 @lisp
148 ((*PRINT-LENGTH* . 10) (*PRINT-LEVEL* . 6) (*PRINT-PRETTY* . NIL))
149 @end lisp
150 The variables in the @code{car} position are bound to the values in
151 the @code{cdr} during the execution of some debug commands.  When
152 evaluating arbitrary expressions in the debugger, the normal values of
153 the printer control variables are in effect. @c FIXME: is this correct?
154 @code{*debug-print-variable-alist*} does not contain any bindings
155 initially.
156
157 @end defvr
158
159 @node  Stack Frames
160 @comment  node-name,  next,  previous,  up
161 @section Stack Frames
162 @cindex Stack frames
163
164 A @dfn{stack frame} is the run-time representation of a call to a
165 function; the frame stores the state that a function needs to remember
166 what it is doing.  Frames have:
167
168 @itemize
169
170 @item
171 @dfn{variables} (@pxref{Variable Access}), which are the values being operated
172 on;
173
174 @item
175 @dfn{arguments} to the call (which are really just particularly
176 interesting variables), and
177
178 @item
179 a current location (@pxref{Source Location Printing}), which is the place in
180 the program where the function was running when it stopped to call
181 another function, or because of an interrupt or error.
182
183 @end itemize
184
185
186 @menu
187 * Stack Motion::                
188 * How Arguments are Printed::   
189 * Function Names::              
190 * Funny Frames::                
191 * Debug Tail Recursion::        
192 * Unknown Locations and Interrupts::  
193 @end menu
194
195 @node  Stack Motion
196 @comment  node-name,  next,  previous,  up
197 @subsection Stack Motion
198
199 These commands move to a new stack frame and print the name of the
200 function and the values of its arguments in the style of a Lisp
201 function call:
202
203 @deffn {Debugger Command} up
204 Move up to the next higher frame.  More recent function calls are
205 considered to be higher on the stack.
206 @end deffn
207
208 @deffn {Debugger Command} down
209 Move down to the next lower frame.
210 @end deffn
211
212 @deffn {Debugger Command} top
213 Move to the highest frame, that is, the frame where the debugger was
214 entered.
215 @end deffn
216
217 @deffn {Debugger Command} bottom
218 Move to the lowest frame.
219 @end deffn
220
221 @deffn {Debugger Command} frame [@var{n}]
222 Move to the frame with the specified number.  Prompts for the number if not
223 supplied.  The frame with number 0 is the frame where the debugger
224 was entered.
225 @end deffn
226
227
228 @node  How Arguments are Printed
229 @comment  node-name,  next,  previous,  up
230 @subsection How Arguments are Printed
231
232 A frame is printed to look like a function call, but with the actual
233 argument values in the argument positions.  So the frame for this call
234 in the source:
235
236 @lisp
237 (myfun (+ 3 4) 'a)
238 @end lisp
239
240 would look like this:
241
242 @example
243 (MYFUN 7 A)
244 @end example
245
246 All keyword and optional arguments are displayed with their actual
247 values; if the corresponding argument was not supplied, the value will
248 be the default.  So this call:
249
250 @lisp
251 (subseq "foo" 1)
252 @end lisp
253
254 would look like this:
255
256 @example
257 (SUBSEQ "foo" 1 3)
258 @end example
259
260 And this call:
261
262 @lisp
263 (string-upcase "test case")
264 @end lisp
265
266 would look like this:
267
268 @example
269 (STRING-UPCASE "test case" :START 0 :END NIL)
270 @end example
271
272 The arguments to a function call are displayed by accessing the
273 argument variables.  Although those variables are initialized to the
274 actual argument values, they can be set inside the function; in this
275 case the new value will be displayed.
276
277 @code{&rest} arguments are handled somewhat differently.  The value of
278 the rest argument variable is displayed as the spread-out arguments to
279 the call, so:
280
281 @lisp
282 (format t "~A is a ~A." "This" 'test)
283 @end lisp
284
285 would look like this:
286
287 @example
288 (FORMAT T "~A is a ~A." "This" 'TEST)
289 @end example
290
291 Rest arguments cause an exception to the normal display of keyword
292 arguments in functions that have both @code{&rest} and @code{&key}
293 arguments.  In this case, the keyword argument variables are not
294 displayed at all; the rest arg is displayed instead.  So for these
295 functions, only the keywords actually supplied will be shown, and the
296 values displayed will be the argument values, not values of the
297 (possibly modified) variables.
298
299 If the variable for an argument is never referenced by the function,
300 it will be deleted.  The variable value is then unavailable, so the
301 debugger prints @samp{#<unused-arg>} instead of the value.  Similarly,
302 if for any of a number of reasons the value of the variable is
303 unavailable or not known to be available (@pxref{Variable Access}),
304 then @samp{#<unavailable-arg>} will be printed instead of the argument
305 value.
306
307 @vindex *debug-print-variable-alist*
308 Printing of argument values is controlled by
309 @code{*debug-print-variable-alist*}.  @xref{Controlling Printing in
310 the Debugger}.
311
312
313 @node  Function Names
314 @comment  node-name,  next,  previous,  up
315 @subsection Function Names
316
317 If a function is defined by @code{defun}, @code{labels}, or
318 @code{flet}, then the debugger will print the actual function name
319 after the open parenthesis, like:
320
321 @example
322 (STRING-UPCASE "test case" :START 0 :END NIL)
323 ((SETF AREF) #\a "for" 1)
324 @end example
325
326 Otherwise, the function name is a string, and will be printed in
327 quotes:
328
329 @example
330 ("DEFUN MYFUN" BAR)
331 ("DEFMACRO DO" (DO ((I 0 (1+ I))) ((= I 13))) NIL)
332 ("SETQ *GC-NOTIFY-BEFORE*")
333 @end example
334
335 This string name is derived from the @code{def@var{mumble}} form
336 that encloses or expanded into the lambda, or the outermost enclosing
337 form if there is no @code{def@var{mumble}}.
338
339
340 @node  Funny Frames
341 @comment  node-name,  next,  previous,  up
342 @subsection Funny Frames
343 @cindex External entry points
344 @cindex Entry points, external
345 @cindex Block compilation, debugger implications
346 @cindex External, stack frame kind
347 @cindex Optional, stack frame kind
348 @cindex Cleanup, stack frame kind
349
350 Sometimes the evaluator introduces new functions that are used to
351 implement a user function, but are not directly specified in the
352 source.  The main place this is done is for checking argument type and
353 syntax.  Usually these functions do their thing and then go away, and
354 thus are not seen on the stack in the debugger.  But when you get some
355 sort of error during lambda-list processing, you end up in the
356 debugger on one of these funny frames.
357
358 These funny frames are flagged by printing
359 ``@code{[@var{keyword}]}'' after the parentheses.  For example,
360 this call:
361
362 @lisp
363 (car 'a 'b)
364 @end lisp
365
366 will look like this:
367
368 @example
369 (CAR 2 A)[:EXTERNAL]
370 @end example
371
372 And this call:
373
374 @lisp
375 (string-upcase "test case" :end)
376 @end lisp
377
378 would look like this:
379
380 @example
381 ("SB!INT:&MORE processing" "test case" 1053984 1)[:OPTIONAL]
382 @end example
383
384 As you can see, these frames have only a vague resemblance to the
385 original call.  Fortunately, the error message displayed when you
386 enter the debugger will usually tell you what problem is (in these
387 cases, too many arguments and odd keyword arguments.)  Also, if you go
388 down the stack to the frame for the calling function, you can display
389 the original source.  @xref{Source Location Printing}.
390
391 @c FIXME: is the block-compilation part correct for SBCL?
392
393 With recursive or block compiled functions, an @code{:EXTERNAL} frame
394 may appear before the frame representing the first call to the
395 recursive function or entry to the compiled block. This is a
396 consequence of the way the compiler does block compilation: there is
397 nothing odd with your program. You will also see @code{:CLEANUP}
398 frames during the execution of @code{unwind-protect} cleanup
399 code. Note that inline expansion and open-coding affect what frames
400 are present in the debugger, see @ref{Debugger Policy Control}.
401 @comment FIXME: link here to section about open coding once it exists.
402 @c @ref{open-coding}
403
404
405 @node  Debug Tail Recursion
406 @comment  node-name,  next,  previous,  up
407 @subsection Debug Tail Recursion
408 @cindex Tail recursion
409 @cindex Recursion, tail
410
411 Both the compiler and the interpreter are ``properly tail recursive.''
412 If a function call is in a tail-recursive position, the stack frame
413 will be deallocated @emph{at the time of the call}, rather than after
414 the call returns.  Consider this backtrace:
415
416 @example
417 (BAR ...) 
418 (FOO ...)
419 @end example
420
421 Because of tail recursion, it is not necessarily the case that
422 @code{FOO} directly called @code{BAR}.  It may be that @code{FOO}
423 called some other function @code{FOO2} which then called @code{BAR}
424 tail-recursively, as in this example:
425
426 @lisp
427 (defun foo ()
428   ...
429   (foo2 ...)
430   ...)
431
432 (defun foo2 (...)
433   ...
434   (bar ...))
435
436 (defun bar (...)
437   ...)
438 @end lisp
439
440 Usually the elimination of tail-recursive frames makes debugging more
441 pleasant, since theses frames are mostly uninformative.  If there is
442 any doubt about how one function called another, it can usually be
443 eliminated by finding the source location in the calling frame.
444 @xref{Source Location Printing}.
445
446 The elimination of tail-recursive frames can be prevented by disabling
447 tail-recursion optimization, which happens when the @code{debug}
448 optimization quality is greater than @code{2}.  
449 @xref{Debugger Policy Control}.
450
451 @comment FIXME: reinstate this link once the chapter is in the manual.
452 @c For a more thorough discussion of tail recursion, @ref{tail-recursion}.
453
454
455 @node Unknown Locations and Interrupts
456 @comment  node-name,  next,  previous,  up
457 @subsection Unknown Locations and Interrupts
458 @cindex Unknown code locations
459 @cindex Locations, unknown
460 @cindex Interrupts
461 @cindex Errors, run-time
462
463 The debugger operates using special debugging information attached to
464 the compiled code.  This debug information tells the debugger what it
465 needs to know about the locations in the code where the debugger can
466 be invoked.  If the debugger somehow encounters a location not
467 described in the debug information, then it is said to be
468 @dfn{unknown}.  If the code location for a frame is unknown, then some
469 variables may be inaccessible, and the source location cannot be
470 precisely displayed.
471
472 There are three reasons why a code location could be unknown:
473
474 @itemize
475
476 @item
477 There is inadequate debug information due to the value of the @code{debug}
478 optimization quality.  @xref{Debugger Policy Control}.
479
480 @item
481 The debugger was entered because of an interrupt such as @key{C-c}.
482
483 @item
484 A hardware error such as ``@samp{bus error}'' occurred in code that was
485 compiled unsafely due to the value of the @code{safety} optimization
486 quality.
487 @comment FIXME: reinstate link when section on optimize qualities exists.
488 @c  @xref{optimize-declaration}.
489
490 @end itemize
491
492 In the last two cases, the values of argument variables are
493 accessible, but may be incorrect.  For more details on when variable
494 values are accessible, @ref{Variable Value Availability}.
495
496 It is possible for an interrupt to happen when a function call or
497 return is in progress.  The debugger may then flame out with some
498 obscure error or insist that the bottom of the stack has been reached,
499 when the real problem is that the current stack frame can't be
500 located.  If this happens, return from the interrupt and try again.
501
502
503 @node Variable Access
504 @comment  node-name,  next,  previous,  up
505 @section Variable Access
506 @cindex Debug variables
507 @cindex Variables, debugger access
508
509 There are two ways to access the current frame's local variables in
510 the debugger: @command{list-locals} and @code{sb-debug:var}.
511
512 The debugger doesn't really understand lexical scoping; it has just
513 one namespace for all the variables in the current stack frame.  If a
514 symbol is the name of multiple variables in the same function, then
515 the reference appears ambiguous, even though lexical scoping specifies
516 which value is visible at any given source location.  If the scopes of
517 the two variables are not nested, then the debugger can resolve the
518 ambiguity by observing that only one variable is accessible.
519
520 When there are ambiguous variables, the evaluator assigns each one a
521 small integer identifier.  The @code{sb-debug:var} function uses this
522 identifier to distinguish between ambiguous variables.  The
523 @command{list-locals} command prints the identifier.  In the
524 following example, there are two variables named @code{X}.  The first
525 one has identifier 0 (which is not printed), the second one has
526 identifier 1.
527
528 @example
529 X  =  1
530 X#1  =  2
531 @end example
532
533 @deffn {Debugger Command} list-locals [@var{prefix}]
534 This command prints the name and value of all variables in the current
535 frame whose name has the specified @var{prefix}.  @var{prefix} may be
536 a string or a symbol.  If no @var{prefix} is given, then all available
537 variables are printed.  If a variable has a potentially ambiguous
538 name, then the name is printed with a ``@code{#@var{identifier}}''
539 suffix, where @var{identifier} is the small integer used to make the
540 name unique.
541 @end deffn
542
543 @defun sb-debug:var @var{name} &optional @var{identifier}
544 This function returns the value of the variable in the current frame
545 with the specified @var{name}.  If supplied, @var{identifier}
546 determines which value to return when there are ambiguous variables.
547   
548 When @var{name} is a symbol, it is interpreted as the symbol name of
549 the variable, i.e. the package is significant.  If @var{name} is an
550 uninterned symbol (gensym), then return the value of the uninterned
551 variable with the same name.  If @var{name} is a string,
552 @code{sb-debug:var} interprets it as the prefix of a variable name
553 that must unambiguously complete to the name of a valid variable.
554
555 @var{identifier} is used to disambiguate the variable name; use
556 @command{list-locals} to find out the identifiers.
557 @end defun
558
559
560 @menu
561 * Variable Value Availability::  
562 * Note On Lexical Variable Access::  
563 @end menu
564
565 @node Variable Value Availability
566 @comment  node-name,  next,  previous,  up
567 @subsection Variable Value Availability
568 @cindex Availability of debug variables
569 @cindex Validity of debug variables
570 @cindex Debug optimization quality
571
572 The value of a variable may be unavailable to the debugger in portions
573 of the program where Lisp says that the variable is defined.  If a
574 variable value is not available, the debugger will not let you read or
575 write that variable.  With one exception, the debugger will never
576 display an incorrect value for a variable.  Rather than displaying
577 incorrect values, the debugger tells you the value is unavailable.
578
579 The one exception is this: if you interrupt (e.g., with @key{C-c}) or
580 if there is an unexpected hardware error such as ``@samp{bus error}''
581 (which should only happen in unsafe code), then the values displayed
582 for arguments to the interrupted frame might be
583 incorrect.@footnote{Since the location of an interrupt or hardware
584 error will always be an unknown location, non-argument variable values
585 will never be available in the interrupted frame.  @xref{Unknown
586 Locations and Interrupts}.}  This exception applies only to the
587 interrupted frame: any frame farther down the stack will be fine.
588
589 The value of a variable may be unavailable for these reasons:
590
591 @itemize
592
593 @item
594 The value of the @code{debug} optimization quality may have omitted debug
595 information needed to determine whether the variable is available.
596 Unless a variable is an argument, its value will only be available when
597 @code{debug} is at least @code{2}.
598
599 @item
600 The compiler did lifetime analysis and determined that the value was no longer
601 needed, even though its scope had not been exited.  Lifetime analysis is
602 inhibited when the @code{debug} optimization quality is @code{3}.
603
604 @item
605 The variable's name is an uninterned symbol (gensym).  To save space, the
606 compiler only dumps debug information about uninterned variables when the
607 @code{debug} optimization quality is @code{3}.
608
609 @item
610 The frame's location is unknown (@pxref{Unknown Locations and
611 Interrupts}) because the debugger was entered due to an interrupt or
612 unexpected hardware error.  Under these conditions the values of
613 arguments will be available, but might be incorrect.  This is the
614 exception mentioned above.
615
616 @item
617 The variable (or the code referencing it) was optimized out
618 of existence.  Variables with no reads are always optimized away.  The
619 degree to which the compiler deletes variables will depend on the
620 value of the @code{compilation-speed} optimization quality, but most
621 source-level optimizations are done under all compilation policies.
622
623 @item
624 The variable is never set and its definition looks like
625 @lisp
626 (LET ((var1 var2))
627    ...)
628 @end lisp
629 In this case, @code{var1} is substituted with @code{var2}.
630
631 @item 
632 The variable is never set and is referenced exactly once.  In this
633 case, the reference is substituted with the variable initial value.
634
635 @end itemize
636
637 Since it is especially useful to be able to get the arguments to a
638 function, argument variables are treated specially when the
639 @code{speed} optimization quality is less than @code{3} and the
640 @code{debug} quality is at least @code{1}.  With this compilation
641 policy, the values of argument variables are almost always available
642 everywhere in the function, even at unknown locations.  For
643 non-argument variables, @code{debug} must be at least @code{2} for
644 values to be available, and even then, values are only available at
645 known locations.
646
647
648 @node  Note On Lexical Variable Access
649 @comment  node-name,  next,  previous,  up
650 @subsection Note On Lexical Variable Access
651
652 When the debugger command loop establishes variable bindings for
653 available variables, these variable bindings have lexical scope and
654 dynamic extent.@footnote{The variable bindings are actually created
655 using the Lisp @code{symbol-macrolet} special form.}  You can close
656 over them, but such closures can't be used as upward funargs.
657
658 You can also set local variables using @code{setq}, but if the
659 variable was closed over in the original source and never set, then
660 setting the variable in the debugger may not change the value in all
661 the functions the variable is defined in.  Another risk of setting
662 variables is that you may assign a value of a type that the compiler
663 proved the variable could never take on.  This may result in bad
664 things happening.
665
666
667 @node Source Location Printing
668 @comment  node-name,  next,  previous,  up
669 @section Source Location Printing
670 @cindex Source location printing, debugger
671
672 One of the debugger's capabilities is source level debugging of
673 compiled code.  These commands display the source location for the
674 current frame:
675
676 @deffn {Debugger Command} source [@var{context}]
677 This command displays the file that the current frame's function was
678 defined from (if it was defined from a file), and then the source form
679 responsible for generating the code that the current frame was
680 executing.  If @var{context} is specified, then it is an integer
681 specifying the number of enclosing levels of list structure to print.
682 @end deffn
683
684 The source form for a location in the code is the innermost list present
685 in the original source that encloses the form responsible for generating
686 that code.  If the actual source form is not a list, then some enclosing
687 list will be printed.  For example, if the source form was a reference
688 to the variable @code{*some-random-special*}, then the innermost
689 enclosing evaluated form will be printed.  Here are some possible
690 enclosing forms:
691
692 @lisp
693 (let ((a *some-random-special*))
694   ...)
695
696 (+ *some-random-special* ...)
697 @end lisp
698
699 If the code at a location was generated from the expansion of a macro
700 or a source-level compiler optimization, then the form in the original
701 source that expanded into that code will be printed.  Suppose the file
702 @file{/usr/me/mystuff.lisp} looked like this:
703
704 @lisp
705 (defmacro mymac ()
706   '(myfun))
707
708 (defun foo ()
709   (mymac)
710   ...)
711 @end lisp
712
713 If @code{foo} has called @code{myfun}, and is waiting for it to
714 return, then the @command{source} command would print:
715
716 @example
717 ; File: /usr/me/mystuff.lisp
718
719 (MYMAC)
720 @end example
721
722 Note that the macro use was printed, not the actual function call form,
723 @code{(myfun)}.
724
725 If enclosing source is printed by giving an argument to
726 @command{source} or @command{vsource}, then the actual source form is
727 marked by wrapping it in a list whose first element is
728 @samp{#:***HERE***}.  In the previous example, @code{source 1} would
729 print:
730
731 @example
732 ; File: /usr/me/mystuff.lisp
733
734 (DEFUN FOO ()
735   (#:***HERE***
736    (MYMAC))
737   ...)
738 @end example
739
740
741 @menu
742 * How the Source is Found::     
743 * Source Location Availability::  
744 @end menu
745
746 @node  How the Source is Found
747 @comment  node-name,  next,  previous,  up
748 @subsection How the Source is Found
749
750 If the code was defined from Lisp by @code{compile} or
751 @code{eval}, then the source can always be reliably located.  If the
752 code was defined from a @file{fasl} file created by
753 @code{compile-file}, then the debugger gets the source forms it
754 prints by reading them from the original source file.  This is a
755 potential problem, since the source file might have moved or changed
756 since the time it was compiled.
757
758 The source file is opened using the @code{truename} of the source file
759 pathname originally given to the compiler.  This is an absolute pathname
760 with all logical names and symbolic links expanded.  If the file can't
761 be located using this name, then the debugger gives up and signals an
762 error.
763
764 If the source file can be found, but has been modified since the time it was
765 compiled, the debugger prints this warning:
766
767 @example
768 ; File has been modified since compilation:
769 ;   @var{filename}
770 ; Using form offset instead of character position.
771 @end example
772
773 where @var{filename} is the name of the source file.  It then proceeds
774 using a robust but not foolproof heuristic for locating the source.
775 This heuristic works if:
776
777 @itemize
778
779 @item
780 No top-level forms before the top-level form containing the source
781 have been added or deleted, and
782
783 @item
784 The top-level form containing the source has not been modified much.
785 (More precisely, none of the list forms beginning before the source
786 form have been added or deleted.)
787
788 @end itemize
789
790 If the heuristic doesn't work, the displayed source will be wrong, but will
791 probably be near the actual source.  If the ``shape'' of the top-level form in
792 the source file is too different from the original form, then an error will be
793 signaled.  When the heuristic is used, the the source location commands are
794 noticeably slowed.
795
796 Source location printing can also be confused if (after the source was
797 compiled) a read-macro you used in the code was redefined to expand
798 into something different, or if a read-macro ever returns the same
799 @code{eq} list twice.  If you don't define read macros and don't use
800 @code{##} in perverted ways, you don't need to worry about this.
801
802
803 @node  Source Location Availability
804 @comment  node-name,  next,  previous,  up
805 @subsection Source Location Availability
806 @cindex Debug optimization quality
807 @cindex Block, basic
808 @cindex Block, start location
809
810 Source location information is only available when the @code{debug}
811 optimization quality is at least @code{2}.  If source location
812 information is unavailable, the source commands will give an error
813 message.
814
815 If source location information is available, but the source location
816 is unknown because of an interrupt or unexpected hardware error
817 (@pxref{Unknown Locations and Interrupts}), then the command will
818 print:
819
820 @example
821 Unknown location: using block start.
822 @end example
823
824 and then proceed to print the source location for the start of the
825 @emph{basic block} enclosing the code location.  It's a bit
826 complicated to explain exactly what a basic block is, but here are
827 some properties of the block start location:
828
829 @itemize
830
831 @item The block start location may be the same as the true location.
832
833 @item The block start location will never be later in the the
834 program's flow of control than the true location.
835
836 @item No conditional control structures (such as @code{if},
837 @code{cond}, @code{or}) will intervene between the block start and the
838 true location (but note that some conditionals present in the original
839 source could be optimized away.)  Function calls @emph{do not} end
840 basic blocks.
841
842 @item The head of a loop will be the start of a block.
843
844 @item The programming language concept of ``block structure'' and the
845 Lisp @code{block} special form are totally unrelated to the compiler's
846 basic block.
847
848 @end itemize
849
850 In other words, the true location lies between the printed location and the
851 next conditional (but watch out because the compiler may have changed the
852 program on you.)
853
854
855 @node Debugger Policy Control
856 @comment  node-name,  next,  previous,  up
857 @section Debugger Policy Control
858 @cindex Policy, debugger
859 @cindex Debug optimization quality
860 @cindex Optimize declaration
861 @cindex Inline expansion
862 @cindex Semi-inline expansion
863
864 The compilation policy specified by @code{optimize} declarations
865 affects the behavior seen in the debugger.  The @code{debug} quality
866 directly affects the debugger by controlling the amount of debugger
867 information dumped.  Other optimization qualities have indirect but
868 observable effects due to changes in the way compilation is done.
869
870 Unlike the other optimization qualities (which are compared in relative value
871 to evaluate tradeoffs), the @code{debug} optimization quality is directly
872 translated to a level of debug information.  This absolute interpretation
873 allows the user to count on a particular amount of debug information being
874 available even when the values of the other qualities are changed during
875 compilation.  These are the levels of debug information that correspond to the
876 values of the @code{debug} quality:
877
878 @table @code
879
880 @item 0
881 Only the function name and enough information to allow the stack to
882 be parsed.
883
884 @item > 0
885 Any level greater than @code{0} gives level @code{0} plus all argument
886 variables.  Values will only be accessible if the argument variable is
887 never set and @code{speed} is not @code{3}.  SBCL allows any real
888 value for optimization qualities.  It may be useful to specify
889 @code{0.5} to get backtrace argument display without argument
890 documentation.
891
892 @item 1
893 Level @code{1} provides argument documentation (printed arglists) and
894 derived argument/result type information.  This makes @code{describe}
895 more informative, and allows the compiler to do compile-time argument
896 count and type checking for any calls compiled at run-time.  This is
897 the default.
898
899 @item 2
900 Level @code{1} plus all interned local variables, source location
901 information, and lifetime information that tells the debugger when
902 arguments are available (even when @code{speed} is @code{3} or the
903 argument is set).
904  
905 @item > 2
906 Any level greater than @code{2} gives level @code{2} and in addition
907 disables tail-call optimization, so that the backtrace will contain
908 frames for all invoked functions, even those in tail positions.
909
910 @item 3
911 Level @code{2} plus all uninterned variables.  In addition, lifetime
912 analysis is disabled (even when @code{speed} is @code{3}), ensuring
913 that all variable values are available at any known location within
914 the scope of the binding.  This has a speed penalty in addition to the
915 obvious space penalty.
916
917 @item > (max speed space)
918 If @code{debug} is greater than both @code{speed} and @code{space},
919 the command @command{return} can be used to continue execution by
920 returning a value from the current stack frame.
921
922 If @code{debug} is also at least 2, then  the code is @emph{partially
923 steppable}. If @code{debug} is 3, the code is @emph{fully steppable}.
924 @xref{Single Stepping} for details.
925
926 @end table
927
928 As you can see, if the @code{speed} quality is @code{3}, debugger performance is
929 degraded.  This effect comes from the elimination of argument variable
930 special-casing (@pxref{Variable Value Availability}).  Some degree of
931 speed/debuggability tradeoff is unavoidable, but the effect is not too drastic
932 when @code{debug} is at least @code{2}.
933
934 In addition to @code{inline} and @code{notinline} declarations, the
935 relative values of the @code{speed} and @code{space} qualities also
936 change whether functions are inline expanded.
937 @comment FIXME: link to section about inline expansion when it exists
938 @c (\pxlref{inline-expansion}.)
939 If a function is inline expanded, then
940 there will be no frame to represent the call, and the arguments will
941 be treated like any other local variable.  Functions may also be
942 ``semi-inline'', in which case there is a frame to represent the call,
943 but the call is to an optimized local version of the function, not to
944 the original function.
945
946
947 @node  Exiting Commands
948 @comment  node-name,  next,  previous,  up
949 @section Exiting Commands
950
951 These commands get you out of the debugger.
952
953 @deffn {Debugger Command} toplevel
954 Throw to top level.
955 @end deffn
956
957 @deffn {Debugger Command} restart [@var{n}]
958 Invokes the @var{n}th restart case as displayed by the @code{error}
959 command.  If @var{n} is not specified, the available restart cases are
960 reported.
961 @end deffn
962
963 @deffn {Debugger Command} continue
964 Calls @code{continue} on the condition given to @code{debug}.  If there is no
965 restart case named @var{continue}, then an error is signaled.
966 @end deffn
967
968 @deffn {Debugger Command} abort
969 Calls @code{abort} on the condition given to @code{debug}.  This is
970 useful for popping debug command loop levels or aborting to top level,
971 as the case may be.
972 @end deffn
973
974 @deffn {Debugger Command} return @var{value}
975 Returns @var{value} from the current stack frame.  This command is
976 available when the @code{debug} optimization quality is greater than
977 both @code{speed} and @code{space}.  Care must be taken that the value
978 is of the same type as SBCL expects the stack frame to return.
979 @end deffn
980
981
982 @node  Information Commands
983 @comment  node-name,  next,  previous,  up
984 @section Information Commands
985
986 Most of these commands print information about the current frame or
987 function, but a few show general information.
988
989 @deffn {Debugger Command} help
990 @deffnx {Debugger Command} ?
991 Displays a synopsis of debugger commands.
992 @end deffn
993
994 @deffn {Debugger Command} describe
995 Calls @code{describe} on the current function and displays the number of
996 local variables.
997 @end deffn
998
999 @deffn {Debugger Command} print
1000 Displays the current function call as it would be displayed by moving to
1001 this frame.
1002 @end deffn
1003
1004 @deffn {Debugger Command} error
1005 Prints the condition given to @code{invoke-debugger} and the active
1006 proceed cases.
1007 @end deffn
1008
1009 @deffn {Debugger Command} backtrace [@var{n}]
1010 Displays all the frames from the current to the bottom.  Only shows
1011 @var{n} frames if specified.  The printing is controlled by @code{*debug-print-variable-alist*}.
1012 @end deffn
1013
1014 @deffn {Debugger Command} step
1015 Selects the @code{continue} restart if one exists and starts single stepping.
1016 @xref{Single Stepping}.
1017 @end deffn
1018
1019 @c The new instrumentation based single stepper doesn't support
1020 @c the following commands, but BREAKPOINT at least should be
1021 @c resurrectable via (TRACE FOO :BREAK T).
1022
1023 @c @cindex Breakpoints
1024
1025 @c SBCL supports setting of breakpoints inside compiled functions and
1026 @c stepping of compiled code.  Breakpoints can only be set at at known
1027 @c locations (@pxref{Unknown Locations and Interrupts}), so these
1028 @c commands are largely useless unless the @code{debug} optimize quality
1029 @c is at least @code{2} (@pxref{Debugger Policy Control}).  These
1030 @c commands manipulate breakpoints:
1031
1032 @c @deffn {Debugger Command} breakpoint @var{location} [@var{option} @var{value}]*
1033 @c Set a breakpoint in some function.  @var{location} may be an integer
1034 @c code location number (as displayed by @command{list-locations}) or a
1035 @c keyword.  The keyword can be used to indicate setting a breakpoint at
1036 @c the function start (@code{:start}, @code{:s}) or function end
1037 @c (@code{:end}, @code{:e}).  The @command{breakpoint} command has
1038 @c @code{:condition}, @code{:break}, @code{:print} and @code{:function}
1039 @c options which work similarly to the @code{trace} options.
1040 @c @end deffn
1041
1042 @c @deffn {Debugger Command} list-locations [@var{function}]
1043 @c @deffnx {Debugger Command} ll  [@var{function}]
1044 @c List all the code locations in the current frame's function, or in
1045 @c @var{function} if it is supplied.  The display format is the code
1046 @c location number, a colon and then the source form for that location:
1047
1048 @c @example
1049 @c 3: (1- N)
1050 @c @end example
1051
1052 @c If consecutive locations have the same source, then a numeric range
1053 @c like @code{3-5:} will be printed.  For example, a default function
1054 @c call has a known location both immediately before and after the call,
1055 @c which would result in two code locations with the same source.  The
1056 @c listed function becomes the new default function for breakpoint
1057 @c setting (via the @command{breakpoint}) command.
1058 @c @end deffn
1059
1060 @c @deffn {Debugger Command} list-breakpoints
1061 @c @deffnx {Debugger Command} lb
1062 @c List all currently active breakpoints with their breakpoint number.
1063 @c @end deffn
1064
1065 @c @deffn {Debugger Command} delete-breakpoint [@var{number}]
1066 @c @deffnx {Debugger Command} db  [@var{number}]
1067 @c Delete a breakpoint specified by its breakpoint number.  If no number
1068 @c is specified, delete all breakpoints.
1069 @c @end deffn
1070
1071 @c @menu
1072 @c * Breakpoint Example::          
1073 @c @end menu
1074
1075 @c @node  Breakpoint Example,  , Breakpoint Commands, Breakpoint Commands
1076 @c @comment  node-name,  next,  previous,  up
1077 @c @subsection Breakpoint Example
1078
1079 @c Consider this definition of the factorial function:
1080
1081 @c @lisp
1082 @c (defun ! (n)
1083 @c   (if (zerop n)
1084 @c       1
1085 @c       (* n (! (1- n)))))
1086 @c @end lisp
1087
1088 @c This debugger session demonstrates the use of breakpoints:
1089
1090 @c @example
1091 @c * (break)  ; invoke debugger
1092
1093 @c debugger invoked on a SIMPLE-CONDITION in thread 11184: break
1094
1095 @c restarts (invokable by number or by possibly-abbreviated name):
1096 @c   0: [CONTINUE] Return from BREAK.
1097 @c   1: [ABORT   ] Reduce debugger level (leaving debugger, returning to toplevel).
1098 @c   2: [TOPLEVEL] Restart at toplevel READ/EVAL/PRINT loop.
1099 @c ("varargs entry for top level local call BREAK" "break")
1100 @c 0] ll #'!
1101
1102 @c 0-1: (SB-INT:NAMED-LAMBDA ! (N) (BLOCK ! (IF (ZEROP N) 1 (* N (! #)))))
1103 @c 2: (BLOCK ! (IF (ZEROP N) 1 (* N (! (1- N)))))
1104 @c 3: (ZEROP N)
1105 @c 4: (* N (! (1- N)))
1106 @c 5: (1- N)
1107 @c 6: (! (1- N))
1108 @c 7-8: (* N (! (1- N)))
1109 @c 9-10: (IF (ZEROP N) 1 (* N (! (1- N))))
1110 @c 0] br 4
1111
1112 @c (* N (! (1- N)))
1113 @c 1: 4 in !
1114 @c added
1115 @c 0] toplevel
1116
1117 @c FIXME: SBCL errored out, and not in the expected way ... Copying the
1118 @c output verbatim from the CMUCL manual for now.
1119
1120 @c common-lisp-user> (! 10) ; Call the function
1121
1122 @c *Breakpoint hit*
1123
1124 @c Restarts:
1125 @c   0: [CONTINUE] Return from BREAK.
1126 @c   1: [ABORT   ] Return to Top-Level.
1127
1128 @c Debug  (type H for help)
1129
1130 @c (! 10) ; We are now in first call (arg 10) before the multiply
1131 @c Source: (* N (! (1- N)))
1132 @c 3] st
1133
1134 @c *Step*
1135
1136 @c (! 10) ; We have finished evaluation of (1- n)
1137 @c Source: (1- N)
1138 @c 3] st
1139
1140 @c *Breakpoint hit*
1141
1142 @c Restarts:
1143 @c   0: [CONTINUE] Return from BREAK.
1144 @c   1: [ABORT   ] Return to Top-Level.
1145
1146 @c Debug  (type H for help)
1147
1148 @c (! 9) ; We hit the breakpoint in the recursive call
1149 @c Source: (* N (! (1- N)))
1150 @c 3] 
1151 @c @end example
1152
1153
1154 @node  Function Tracing
1155 @comment  node-name,  next,  previous,  up
1156 @section Function Tracing
1157 @cindex Tracing
1158 @cindex Function, tracing
1159
1160 The tracer causes selected functions to print their arguments and
1161 their results whenever they are called.  Options allow conditional
1162 printing of the trace information and conditional breakpoints on
1163 function entry or exit.
1164
1165 @include macro-common-lisp-trace.texinfo
1166
1167 @include macro-common-lisp-untrace.texinfo
1168
1169 @include var-sb-debug-star-trace-indentation-step-star.texinfo
1170
1171 @include var-sb-debug-star-max-trace-indentation-star.texinfo
1172
1173 @include var-sb-debug-star-trace-encapsulate-default-star.texinfo
1174
1175 @include var-sb-debug-star-trace-values-star.texinfo
1176
1177 @comment FIXME rudi 2004-03-26: encapsulate is (per TODO file as of
1178 @comment 0.8.9) in a state of flux.  When it's sorted out, revive the
1179 @comment cmucl documentation.
1180
1181 @node Single Stepping
1182 @comment  node-name,  next,  previous,  up
1183 @section Single Stepping
1184 @cindex Stepper
1185 @cindex Single Stepping
1186
1187 SBCL includes an instrumentation based single-stepper for compiled
1188 code, that can be invoked via the @code{step} macro, or from within
1189 the debugger. @xref{Debugger Policy Control} for details on enabling
1190 stepping for compiled code.
1191
1192 Compiled code can be unsteppable, partially steppable, or fully steppable.
1193
1194 @table @strong
1195
1196 @item Unsteppable
1197 Single stepping is not possible.
1198
1199 @item Partially steppable
1200 Single stepping is possible at sequential function call granularity:
1201 nested function calls cannot be stepped into, and no intermediate
1202 values are available.
1203
1204 @item Fully steppable
1205 Single stepping is possible at individual function call argument
1206 granularity, nested calls can be stepped into, and intermediate values
1207 are available.
1208
1209 @end table
1210
1211 @include macro-common-lisp-step.texinfo