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