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