Restore buildability of #+ultrafutex
[sbcl.git] / doc / manual / debugger.texinfo
blob42ed6af63f77f60c3d184a4a2d315cef94b2f487
1 @node Debugger
2 @comment  node-name,  next,  previous,  up
3 @chapter Debugger
4 @cindex Debugger
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.
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
24 @node Debugger Entry
25 @comment  node-name,  next,  previous,  up
26 @section Debugger Entry
28 @menu
29 * Debugger Banner::
30 * Debugger Invocation::
31 @end menu
33 @node Debugger Banner
34 @comment  node-name,  next,  previous,  up
35 @subsection Debugger Banner
37 When you enter the debugger, it looks something like this:
39 @example
40 debugger invoked on a TYPE-ERROR in thread 11184:
41   The value 3 is not of type LIST.
43 You can type HELP for debugger help, or (SB-EXT:QUIT) to exit from SBCL.
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)
50 @end example
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}.
56 This is followed by the ``beginner help line'', which appears only if
57 @code{sb-debug:*debug-beginner-help-p*} is true (default).
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.
64 The current frame appears right underneath the restarts, immediately
65 followed by the debugger prompt.
67 @node Debugger Invocation
68 @comment  node-name,  next,  previous,  up
69 @subsection Debugger Invocation
71 The debugger is invoked when:
73 @itemize
75 @item
76 @code{error} is called, and the condition it signals is not handled.
78 @item
79 @code{break} is called, or @code{signal} is called with a condition
80 that matches the current @code{*break-on-signals*}.
82 @item
83 the debugger is explicitly entered with the @code{invoke-debugger}
84 function.
86 @end itemize
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.
100 @include var-sb-ext-star-invoke-debugger-hook-star.texinfo
102 @node  Debugger Command Loop
103 @comment  node-name,  next,  previous,  up
104 @section Debugger Command Loop
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.
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.
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.
125 It is possible to override the normal printing behaviour in the
126 debugger by using the @code{sb-ext:*debug-print-variable-alist*}.
128 @include var-sb-ext-star-debug-print-variable-alist-star.texinfo
130 @node  Stack Frames
131 @comment  node-name,  next,  previous,  up
132 @section Stack Frames
133 @cindex Stack frames
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:
139 @itemize
141 @item
142 @dfn{variables} (@pxref{Variable Access}), which are the values being operated
145 @item
146 @dfn{arguments} to the call (which are really just particularly
147 interesting variables).
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.
154 @end itemize
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
164 @node  Stack Motion
165 @comment  node-name,  next,  previous,  up
166 @subsection Stack Motion
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:
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
177 @deffn {Debugger Command} @nopkg{down}
178 Move down to the next lower frame.
179 @end deffn
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
186 @deffn {Debugger Command} @nopkg{bottom}
187 Move to the lowest frame.
188 @end deffn
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
197 @node  How Arguments are Printed
198 @comment  node-name,  next,  previous,  up
199 @subsection How Arguments are Printed
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:
205 @lisp
206 (myfun (+ 3 4) 'a)
207 @end lisp
209 would look like this:
211 @example
212 (MYFUN 7 A)
213 @end example
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:
219 @lisp
220 (subseq "foo" 1)
221 @end lisp
223 would look like this:
225 @example
226 (SUBSEQ "foo" 1 3)
227 @end example
229 And this call:
231 @lisp
232 (string-upcase "test case")
233 @end lisp
235 would look like this:
237 @example
238 (STRING-UPCASE "test case" :START 0 :END NIL)
239 @end example
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.
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:
250 @lisp
251 (format t "~A is a ~A." "This" 'test)
252 @end lisp
254 would look like this:
256 @example
257 (FORMAT T "~A is a ~A." "This" 'TEST)
258 @end example
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.
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.
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}
282 @node  Function Names
283 @comment  node-name,  next,  previous,  up
284 @subsection Function Names
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 @var{name})} and @code{(LABELS @var{name})} respectively.
289 Anonymous lambdas will appear as @code{(LAMBDA @var{lambda-list})}.
291 @menu
292 * Entry Point Details::
293 @end menu
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
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.
309 With recursive or block compiled functions, an additional
310 @code{external} frame may appear before the frame representing the first
311 call to the recursive function or entry to the compiled block. This is a
312 consequence of the way the compiler works: there is nothing odd with
313 your program. You may also see @code{cleanup} frames during the
314 execution of @code{unwind-protect} cleanup code, and @code{optional} for
315 variable argument entry points.
317 @node  Debug Tail Recursion
318 @comment  node-name,  next,  previous,  up
319 @subsection Debug Tail Recursion
320 @cindex Tail recursion
321 @cindex Recursion, tail
323 The compiler is ``properly tail recursive.'' If a function call is in
324 a tail-recursive position, the stack frame will be deallocated
325 @emph{at the time of the call}, rather than after the call returns.
326 Consider this backtrace:
328 @example
329 (BAR ...)
330 (FOO ...)
331 @end example
333 Because of tail recursion, it is not necessarily the case that
334 @code{FOO} directly called @code{BAR}.  It may be that @code{FOO}
335 called some other function @code{FOO2} which then called @code{BAR}
336 tail-recursively, as in this example:
338 @lisp
339 (defun foo ()
340   ...
341   (foo2 ...)
342   ...)
344 (defun foo2 (...)
345   ...
346   (bar ...))
348 (defun bar (...)
349   ...)
350 @end lisp
352 Usually the elimination of tail-recursive frames makes debugging more
353 pleasant, since these frames are mostly uninformative.  If there is any
354 doubt about how one function called another, it can usually be
355 eliminated by finding the source location in the calling frame.
356 @xref{Source Location Printing}.
358 The elimination of tail-recursive frames can be prevented by disabling
359 tail-recursion optimization, which happens when the @code{debug}
360 optimization quality is greater than @code{2}.
361 @xref{Debugger Policy Control}.
363 @comment FIXME: reinstate this link once the chapter is in the manual.
364 @c For a more thorough discussion of tail recursion, @ref{tail-recursion}.
366 @node Unknown Locations and Interrupts
367 @comment  node-name,  next,  previous,  up
368 @subsection Unknown Locations and Interrupts
369 @cindex Unknown code locations
370 @cindex Locations, unknown
371 @cindex Interrupts
372 @cindex Errors, run-time
374 The debugger operates using special debugging information attached to
375 the compiled code.  This debug information tells the debugger what it
376 needs to know about the locations in the code where the debugger can
377 be invoked.  If the debugger somehow encounters a location not
378 described in the debug information, then it is said to be
379 @dfn{unknown}.  If the code location for a frame is unknown, then some
380 variables may be inaccessible, and the source location cannot be
381 precisely displayed.
383 There are three reasons why a code location could be unknown:
385 @itemize
387 @item
388 There is inadequate debug information due to the value of the @code{debug}
389 optimization quality.  @xref{Debugger Policy Control}.
391 @item
392 The debugger was entered because of an interrupt such as @key{C-c}.
394 @item
395 A hardware error such as ``@samp{bus error}'' occurred in code that was
396 compiled unsafely due to the value of the @code{safety} optimization
397 quality.
398 @comment FIXME: reinstate link when section on optimize qualities exists.
399 @c  @xref{optimize-declaration}.
401 @end itemize
403 In the last two cases, the values of argument variables are
404 accessible, but may be incorrect.  For more details on when variable
405 values are accessible, see @ref{Variable Value Availability}.
407 It is possible for an interrupt to happen when a function call or
408 return is in progress.  The debugger may then flame out with some
409 obscure error or insist that the bottom of the stack has been reached,
410 when the real problem is that the current stack frame can't be
411 located.  If this happens, return from the interrupt and try again.
414 @node Variable Access
415 @comment  node-name,  next,  previous,  up
416 @section Variable Access
417 @cindex Debug variables
418 @cindex Variables, debugger access
420 There are two ways to access the current frame's local variables in
421 the debugger: @command{list-locals} and @code{sb-debug:var}.
423 The debugger doesn't really understand lexical scoping; it has just
424 one namespace for all the variables in the current stack frame.  If a
425 symbol is the name of multiple variables in the same function, then
426 the reference appears ambiguous, even though lexical scoping specifies
427 which value is visible at any given source location.  If the scopes of
428 the two variables are not nested, then the debugger can resolve the
429 ambiguity by observing that only one variable is accessible.
431 When there are ambiguous variables, the evaluator assigns each one a
432 small integer identifier.  The @code{sb-debug:var} function uses this
433 identifier to distinguish between ambiguous variables.  The
434 @command{list-locals} command prints the identifier.  In the
435 following example, there are two variables named @code{X}.  The first
436 one has identifier 0 (which is not printed), the second one has
437 identifier 1.
439 @example
440 X  =  1
441 X#1  =  2
442 @end example
444 @deffn {Debugger Command} @nopkg{list-locals} [@var{prefix}]
445 This command prints the name and value of all variables in the current
446 frame whose name has the specified @var{prefix}.  @var{prefix} may be
447 a string or a symbol.  If no @var{prefix} is given, then all available
448 variables are printed.  If a variable has a potentially ambiguous
449 name, then the name is printed with a ``@code{#@var{identifier}}''
450 suffix, where @var{identifier} is the small integer used to make the
451 name unique.
452 @end deffn
454 @defun @sbdebug{var} @var{name} &optional @var{identifier}
455 This function returns the value of the variable in the current frame
456 with the specified @var{name}.  If supplied, @var{identifier}
457 determines which value to return when there are ambiguous variables.
459 When @var{name} is a symbol, it is interpreted as the symbol name of
460 the variable, i.e. the package is significant.  If @var{name} is an
461 uninterned symbol (gensym), then return the value of the uninterned
462 variable with the same name.  If @var{name} is a string,
463 @code{sb-debug:var} interprets it as the prefix of a variable name
464 that must unambiguously complete to the name of a valid variable.
466 @var{identifier} is used to disambiguate the variable name; use
467 @command{list-locals} to find out the identifiers.
468 @end defun
471 @menu
472 * Variable Value Availability::
473 * Note On Lexical Variable Access::
474 @end menu
476 @node Variable Value Availability
477 @comment  node-name,  next,  previous,  up
478 @subsection Variable Value Availability
479 @cindex Availability of debug variables
480 @cindex Validity of debug variables
481 @cindex Debug optimization quality
483 The value of a variable may be unavailable to the debugger in portions
484 of the program where Lisp says that the variable is defined.  If a
485 variable value is not available, the debugger will not let you read or
486 write that variable.  With one exception, the debugger will never
487 display an incorrect value for a variable.  Rather than displaying
488 incorrect values, the debugger tells you the value is unavailable.
490 The one exception is this: if you interrupt (e.g., with @key{C-c}) or
491 if there is an unexpected hardware error such as ``@samp{bus error}''
492 (which should only happen in unsafe code), then the values displayed
493 for arguments to the interrupted frame might be
494 incorrect.@footnote{Since the location of an interrupt or hardware
495 error will always be an unknown location, non-argument variable values
496 will never be available in the interrupted frame.  @xref{Unknown
497 Locations and Interrupts}.}  This exception applies only to the
498 interrupted frame: any frame farther down the stack will be fine.
500 The value of a variable may be unavailable for these reasons:
502 @itemize
504 @item
505 The value of the @code{debug} optimization quality may have omitted debug
506 information needed to determine whether the variable is available.
507 Unless a variable is an argument, its value will only be available when
508 @code{debug} is at least @code{2}.
510 @item
511 The compiler did lifetime analysis and determined that the value was no longer
512 needed, even though its scope had not been exited.  Lifetime analysis is
513 inhibited when the @code{debug} optimization quality is @code{3}.
515 @item
516 The variable's name is an uninterned symbol (gensym).  To save space, the
517 compiler only dumps debug information about uninterned variables when the
518 @code{debug} optimization quality is @code{3}.
520 @item
521 The frame's location is unknown (@pxref{Unknown Locations and
522 Interrupts}) because the debugger was entered due to an interrupt or
523 unexpected hardware error.  Under these conditions the values of
524 arguments will be available, but might be incorrect.  This is the
525 exception mentioned above.
527 @item
528 The variable (or the code referencing it) was optimized out
529 of existence.  Variables with no reads are always optimized away.  The
530 degree to which the compiler deletes variables will depend on the
531 value of the @code{compilation-speed} optimization quality, but most
532 source-level optimizations are done under all compilation policies.
534 @item
535 The variable is never set and its definition looks like
536 @lisp
537 (LET ((var1 var2))
538    ...)
539 @end lisp
540 In this case, @code{var1} is substituted with @code{var2}.
542 @item
543 The variable is never set and is referenced exactly once.  In this
544 case, the reference is substituted with the variable initial value.
546 @end itemize
548 Since it is especially useful to be able to get the arguments to a
549 function, argument variables are treated specially when the
550 @code{speed} optimization quality is less than @code{3} and the
551 @code{debug} quality is at least @code{1}.  With this compilation
552 policy, the values of argument variables are almost always available
553 everywhere in the function, even at unknown locations.  For
554 non-argument variables, @code{debug} must be at least @code{2} for
555 values to be available, and even then, values are only available at
556 known locations.
559 @node  Note On Lexical Variable Access
560 @comment  node-name,  next,  previous,  up
561 @subsection Note On Lexical Variable Access
563 When the debugger command loop establishes variable bindings for
564 available variables, these variable bindings have lexical scope and
565 dynamic extent.@footnote{The variable bindings are actually created
566 using the Lisp @code{symbol-macrolet} special form.}  You can close
567 over them, but such closures can't be used as upward function arguments.
569 You can also set local variables using @code{setq}, but if the
570 variable was closed over in the original source and never set, then
571 setting the variable in the debugger may not change the value in all
572 the functions the variable is defined in.  Another risk of setting
573 variables is that you may assign a value of a type that the compiler
574 proved the variable could never take on.  This may result in bad
575 things happening.
578 @node Source Location Printing
579 @comment  node-name,  next,  previous,  up
580 @section Source Location Printing
581 @cindex Source location printing, debugger
583 One of the debugger's capabilities is source level debugging of
584 compiled code.  These commands display the source location for the
585 current frame:
587 @deffn {Debugger Command} @nopkg{source} [@var{context}]
588 This command displays the file that the current frame's function was
589 defined from (if it was defined from a file), and then the source form
590 responsible for generating the code that the current frame was
591 executing.  If @var{context} is specified, then it is an integer
592 specifying the number of enclosing levels of list structure to print.
593 @end deffn
595 The source form for a location in the code is the innermost list present
596 in the original source that encloses the form responsible for generating
597 that code.  If the actual source form is not a list, then some enclosing
598 list will be printed.  For example, if the source form was a reference
599 to the variable @code{*some-random-special*}, then the innermost
600 enclosing evaluated form will be printed.  Here are some possible
601 enclosing forms:
603 @lisp
604 (let ((a *some-random-special*))
605   ...)
607 (+ *some-random-special* ...)
608 @end lisp
610 If the code at a location was generated from the expansion of a macro
611 or a source-level compiler optimization, then the form in the original
612 source that expanded into that code will be printed.  Suppose the file
613 @file{/usr/me/mystuff.lisp} looked like this:
615 @lisp
616 (defmacro mymac ()
617   '(myfun))
619 (defun foo ()
620   (mymac)
621   ...)
622 @end lisp
624 If @code{foo} has called @code{myfun}, and is waiting for it to
625 return, then the @command{source} command would print:
627 @example
628 ; File: /usr/me/mystuff.lisp
630 (MYMAC)
631 @end example
633 Note that the macro use was printed, not the actual function call form,
634 @code{(myfun)}.
636 If enclosing source is printed by giving an argument to
637 @command{source} or @command{vsource}, then the actual source form is
638 marked by wrapping it in a list whose first element is
639 @samp{#:***HERE***}.  In the previous example, @code{source 1} would
640 print:
642 @example
643 ; File: /usr/me/mystuff.lisp
645 (DEFUN FOO ()
646   (#:***HERE***
647    (MYMAC))
648   ...)
649 @end example
652 @menu
653 * How the Source is Found::
654 * Source Location Availability::
655 @end menu
657 @node  How the Source is Found
658 @comment  node-name,  next,  previous,  up
659 @subsection How the Source is Found
661 If the code was defined from Lisp by @code{compile} or
662 @code{eval}, then the source can always be reliably located.  If the
663 code was defined from a @file{fasl} file created by
664 @code{compile-file}, then the debugger gets the source forms it
665 prints by reading them from the original source file.  This is a
666 potential problem, since the source file might have moved or changed
667 since the time it was compiled.
669 The source file is opened using the @code{truename} of the source file
670 pathname originally given to the compiler.  This is an absolute pathname
671 with all logical names and symbolic links expanded.  If the file can't
672 be located using this name, then the debugger gives up and signals an
673 error.
675 If the source file can be found, but has been modified since the time it was
676 compiled, the debugger prints this warning:
678 @example
679 ; File has been modified since compilation:
680 ;   @var{filename}
681 ; Using form offset instead of character position.
682 @end example
684 where @var{filename} is the name of the source file.  It then proceeds
685 using a robust but not foolproof heuristic for locating the source.
686 This heuristic works if:
688 @itemize
690 @item
691 No top-level forms before the top-level form containing the source
692 have been added or deleted, and
694 @item
695 The top-level form containing the source has not been modified much.
696 (More precisely, none of the list forms beginning before the source
697 form have been added or deleted.)
699 @end itemize
701 If the heuristic doesn't work, the displayed source will be wrong, but will
702 probably be near the actual source.  If the ``shape'' of the top-level form in
703 the source file is too different from the original form, then an error will be
704 signaled.  When the heuristic is used, the source location commands are
705 noticeably slowed.
707 Source location printing can also be confused if (after the source was
708 compiled) a read-macro you used in the code was redefined to expand
709 into something different, or if a read-macro ever returns the same
710 @code{eq} list twice.  If you don't define read macros and don't use
711 @code{##} in perverted ways, you don't need to worry about this.
714 @node  Source Location Availability
715 @comment  node-name,  next,  previous,  up
716 @subsection Source Location Availability
717 @cindex Debug optimization quality
718 @cindex Block, basic
719 @cindex Block, start location
721 Source location information is only available when the @code{debug}
722 optimization quality is at least @code{2}.  If source location
723 information is unavailable, the source commands will give an error
724 message.
726 If source location information is available, but the source location
727 is unknown because of an interrupt or unexpected hardware error
728 (@pxref{Unknown Locations and Interrupts}), then the command will
729 print:
731 @example
732 Unknown location: using block start.
733 @end example
735 and then proceed to print the source location for the start of the
736 @emph{basic block} enclosing the code location.  It's a bit
737 complicated to explain exactly what a basic block is, but here are
738 some properties of the block start location:
740 @itemize
742 @item The block start location may be the same as the true location.
744 @item The block start location will never be later in the
745 program's flow of control than the true location.
747 @item No conditional control structures (such as @code{if},
748 @code{cond}, @code{or}) will intervene between the block start and the
749 true location (but note that some conditionals present in the original
750 source could be optimized away.)  Function calls @emph{do not} end
751 basic blocks.
753 @item The head of a loop will be the start of a block.
755 @item The programming language concept of ``block structure'' and the
756 Lisp @code{block} special form are totally unrelated to the compiler's
757 basic block.
759 @end itemize
761 In other words, the true location lies between the printed location and the
762 next conditional (but watch out because the compiler may have changed the
763 program on you.)
766 @node Debugger Policy Control
767 @comment  node-name,  next,  previous,  up
768 @section Debugger Policy Control
769 @cindex Policy, debugger
770 @cindex Debug optimization quality
771 @cindex Optimize declaration
772 @cindex Inline expansion
773 @cindex Semi-inline expansion
775 The compilation policy specified by @code{optimize} declarations
776 affects the behavior seen in the debugger.  The @code{debug} quality
777 directly affects the debugger by controlling the amount of debugger
778 information dumped.  Other optimization qualities have indirect but
779 observable effects due to changes in the way compilation is done.
781 Unlike the other optimization qualities (which are compared in relative value
782 to evaluate tradeoffs), the @code{debug} optimization quality is directly
783 translated to a level of debug information.  This absolute interpretation
784 allows the user to count on a particular amount of debug information being
785 available even when the values of the other qualities are changed during
786 compilation.  These are the levels of debug information that correspond to the
787 values of the @code{debug} quality:
789 @table @code
791 @item 0
792 Only the function name and enough information to allow the stack to
793 be parsed.
795 @item > 0
796 Any level greater than @code{0} gives level @code{0} plus all argument
797 variables.  Values will only be accessible if the argument variable is
798 never set and @code{speed} is not @code{3}.  SBCL allows any real
799 value for optimization qualities.  It may be useful to specify
800 @code{0.5} to get backtrace argument display without argument
801 documentation.
803 @item 1
804 Level @code{1} provides argument documentation (printed argument lists) and
805 derived argument/result type information.  This makes @code{describe}
806 more informative, and allows the compiler to do compile-time argument
807 count and type checking for any calls compiled at run-time.  This is
808 the default.
810 @item 2
811 Level @code{1} plus all interned local variables, source location
812 information, and lifetime information that tells the debugger when
813 arguments are available (even when @code{speed} is @code{3} or the
814 argument is set).
816 @item > 2
817 Any level greater than @code{2} gives level @code{2} and in addition
818 disables tail-call optimization, so that the backtrace will contain
819 frames for all invoked functions, even those in tail positions.
821 @item 3
822 Level @code{2} plus all uninterned variables.  In addition, lifetime
823 analysis is disabled (even when @code{speed} is @code{3}), ensuring
824 that all variable values are available at any known location within
825 the scope of the binding.  This has a speed penalty in addition to the
826 obvious space penalty.
828 Inlining of local functions is inhibited so that they may be
829 @code{trace}d.
831 @item > (max speed space)
832 If @code{debug} is greater than both @code{speed} and @code{space},
833 the command @command{return} can be used to continue execution by
834 returning a value from the current stack frame.
836 @item > (max speed space compilation-speed)
837 If @code{debug} is greater than all of @code{speed}, @code{space} and
838 @code{compilation-speed} the code will be steppable (@pxref{Single Stepping}).
840 @end table
842 As you can see, if the @code{speed} quality is @code{3}, debugger performance is
843 degraded.  This effect comes from the elimination of argument variable
844 special-casing (@pxref{Variable Value Availability}).  Some degree of
845 speed/debuggability tradeoff is unavoidable, but the effect is not too drastic
846 when @code{debug} is at least @code{2}.
848 In addition to @code{inline} and @code{notinline} declarations, the
849 relative values of the @code{speed} and @code{space} qualities also
850 change whether functions are inline expanded.
851 @comment FIXME: link to section about inline expansion when it exists
852 @c (\pxlref{inline-expansion}.)
853 If a function is inline expanded, then
854 there will be no frame to represent the call, and the arguments will
855 be treated like any other local variable.  Functions may also be
856 ``semi-inline'', in which case there is a frame to represent the call,
857 but the call is to an optimized local version of the function, not to
858 the original function.
861 @node  Exiting Commands
862 @comment  node-name,  next,  previous,  up
863 @section Exiting Commands
865 These commands get you out of the debugger.
867 @deffn {Debugger Command} @nopkg{toplevel}
868 Throw to top level.
869 @end deffn
871 @deffn {Debugger Command} @nopkg{restart} [@var{n}]
872 Invokes the @var{n}th restart case as displayed by the @code{error}
873 command.  If @var{n} is not specified, the available restart cases are
874 reported.
875 @end deffn
877 @deffn {Debugger Command} @nopkg{continue}
878 Calls @code{continue} on the condition given to @code{debug}.  If there is no
879 restart case named @var{continue}, then an error is signaled.
880 @end deffn
882 @deffn {Debugger Command} @nopkg{abort}
883 Calls @code{abort} on the condition given to @code{debug}.  This is
884 useful for popping debug command loop levels or aborting to top level,
885 as the case may be.
886 @end deffn
888 @deffn {Debugger Command} @nopkg{return} @var{value}
889 Returns @var{value} from the current stack frame.  This command is
890 available when the @code{debug} optimization quality is greater than
891 both @code{speed} and @code{space}.  Care must be taken that the value
892 is of the same type as SBCL expects the stack frame to return.
893 @end deffn
895 @deffn {Debugger Command} @nopkg{restart-frame}
896 Restarts execution of the current stack frame. This command is
897 available when the @code{debug} optimization quality is greater than
898 both @code{speed} and @code{space} and when the frame is for a global
899 function. If the function is redefined in the debugger before the frame
900 is restarted, the new function will be used.
901 @end deffn
903 @node  Information Commands
904 @comment  node-name,  next,  previous,  up
905 @section Information Commands
907 Most of these commands print information about the current frame or
908 function, but a few show general information.
910 @deffn {Debugger Command} @nopkg{help}
911 @deffnx {Debugger Command} @nopkg{?}
912 Displays a synopsis of debugger commands.
913 @end deffn
915 @deffn {Debugger Command} @nopkg{describe}
916 Calls @code{describe} on the current function and displays the number of
917 local variables.
918 @end deffn
920 @deffn {Debugger Command} @nopkg{print}
921 Displays the current function call as it would be displayed by moving to
922 this frame.
923 @end deffn
925 @deffn {Debugger Command} @nopkg{error}
926 Prints the condition given to @code{invoke-debugger} and the active
927 proceed cases.
928 @end deffn
930 @deffn {Debugger Command} @nopkg{backtrace} [@var{n}]
931 Displays all the frames from the current to the bottom. Only shows
932 @var{n} frames if specified. The printing is controlled by
933 @code{*debug-print-variable-alist*}.
934 @end deffn
936 @c The new instrumentation based single stepper doesn't support
937 @c the following commands, but BREAKPOINT at least should be
938 @c resurrectable via (TRACE FOO :BREAK T).
940 @c @cindex Breakpoints
942 @c SBCL supports setting of breakpoints inside compiled functions and
943 @c stepping of compiled code.  Breakpoints can only be set at known
944 @c locations (@pxref{Unknown Locations and Interrupts}), so these
945 @c commands are largely useless unless the @code{debug} optimize quality
946 @c is at least @code{2} (@pxref{Debugger Policy Control}).  These
947 @c commands manipulate breakpoints:
949 @c @deffn {Debugger Command} breakpoint @var{location} [@var{option} @var{value}]*
950 @c Set a breakpoint in some function.  @var{location} may be an integer
951 @c code location number (as displayed by @command{list-locations}) or a
952 @c keyword.  The keyword can be used to indicate setting a breakpoint at
953 @c the function start (@code{:start}, @code{:s}) or function end
954 @c (@code{:end}, @code{:e}).  The @command{breakpoint} command has
955 @c @code{:condition}, @code{:break}, @code{:print} and @code{:function}
956 @c options which work similarly to the @code{trace} options.
957 @c @end deffn
959 @c @deffn {Debugger Command} list-locations [@var{function}]
960 @c @deffnx {Debugger Command} ll  [@var{function}]
961 @c List all the code locations in the current frame's function, or in
962 @c @var{function} if it is supplied.  The display format is the code
963 @c location number, a colon and then the source form for that location:
965 @c @example
966 @c 3: (1- N)
967 @c @end example
969 @c If consecutive locations have the same source, then a numeric range
970 @c like @code{3-5:} will be printed.  For example, a default function
971 @c call has a known location both immediately before and after the call,
972 @c which would result in two code locations with the same source.  The
973 @c listed function becomes the new default function for breakpoint
974 @c setting (via the @command{breakpoint}) command.
975 @c @end deffn
977 @c @deffn {Debugger Command} list-breakpoints
978 @c @deffnx {Debugger Command} lb
979 @c List all currently active breakpoints with their breakpoint number.
980 @c @end deffn
982 @c @deffn {Debugger Command} delete-breakpoint [@var{number}]
983 @c @deffnx {Debugger Command} db  [@var{number}]
984 @c Delete a breakpoint specified by its breakpoint number.  If no number
985 @c is specified, delete all breakpoints.
986 @c @end deffn
988 @c @menu
989 @c * Breakpoint Example::
990 @c @end menu
992 @c @node  Breakpoint Example,  , Breakpoint Commands, Breakpoint Commands
993 @c @comment  node-name,  next,  previous,  up
994 @c @subsection Breakpoint Example
996 @c Consider this definition of the factorial function:
998 @c @lisp
999 @c (defun ! (n)
1000 @c   (if (zerop n)
1001 @c       1
1002 @c       (* n (! (1- n)))))
1003 @c @end lisp
1005 @c This debugger session demonstrates the use of breakpoints:
1007 @c @example
1008 @c * (break)  ; invoke debugger
1010 @c debugger invoked on a SIMPLE-CONDITION in thread 11184: break
1012 @c restarts (invokable by number or by possibly-abbreviated name):
1013 @c   0: [CONTINUE] Return from BREAK.
1014 @c   1: [ABORT   ] Reduce debugger level (leaving debugger, returning to toplevel).
1015 @c   2: [TOPLEVEL] Restart at toplevel READ/EVAL/PRINT loop.
1016 @c ("varargs entry for top level local call BREAK" "break")
1017 @c 0] ll #'!
1019 @c 0-1: (SB-INT:NAMED-LAMBDA ! (N) (BLOCK ! (IF (ZEROP N) 1 (* N (! #)))))
1020 @c 2: (BLOCK ! (IF (ZEROP N) 1 (* N (! (1- N)))))
1021 @c 3: (ZEROP N)
1022 @c 4: (* N (! (1- N)))
1023 @c 5: (1- N)
1024 @c 6: (! (1- N))
1025 @c 7-8: (* N (! (1- N)))
1026 @c 9-10: (IF (ZEROP N) 1 (* N (! (1- N))))
1027 @c 0] br 4
1029 @c (* N (! (1- N)))
1030 @c 1: 4 in !
1031 @c added
1032 @c 0] toplevel
1034 @c FIXME: SBCL errored out, and not in the expected way ... Copying the
1035 @c output verbatim from the CMUCL manual for now.
1037 @c common-lisp-user> (! 10) ; Call the function
1039 @c *Breakpoint hit*
1041 @c Restarts:
1042 @c   0: [CONTINUE] Return from BREAK.
1043 @c   1: [ABORT   ] Return to Top-Level.
1045 @c Debug  (type H for help)
1047 @c (! 10) ; We are now in first call (arg 10) before the multiply
1048 @c Source: (* N (! (1- N)))
1049 @c 3] st
1051 @c *Step*
1053 @c (! 10) ; We have finished evaluation of (1- n)
1054 @c Source: (1- N)
1055 @c 3] st
1057 @c *Breakpoint hit*
1059 @c Restarts:
1060 @c   0: [CONTINUE] Return from BREAK.
1061 @c   1: [ABORT   ] Return to Top-Level.
1063 @c Debug  (type H for help)
1065 @c (! 9) ; We hit the breakpoint in the recursive call
1066 @c Source: (* N (! (1- N)))
1067 @c 3]
1068 @c @end example
1071 @node  Function Tracing
1072 @comment  node-name,  next,  previous,  up
1073 @section Function Tracing
1074 @cindex Tracing
1075 @cindex Function, tracing
1077 The tracer causes selected functions to print their arguments and
1078 their results whenever they are called.  Options allow conditional
1079 printing of the trace information and conditional breakpoints on
1080 function entry or exit.
1082 @include macro-common-lisp-trace.texinfo
1084 @include macro-common-lisp-untrace.texinfo
1086 @include var-sb-debug-star-trace-indentation-step-star.texinfo
1088 @include var-sb-debug-star-max-trace-indentation-star.texinfo
1090 @include var-sb-debug-star-trace-encapsulate-default-star.texinfo
1092 @include var-sb-debug-star-trace-report-default-star.texinfo
1094 @comment FIXME rudi 2004-03-26: encapsulate is (per TODO file as of
1095 @comment 0.8.9) in a state of flux.  When it's sorted out, revive the
1096 @comment cmucl documentation.
1098 @node Single Stepping
1099 @comment  node-name,  next,  previous,  up
1100 @section Single Stepping
1101 @cindex Stepper
1102 @cindex Single Stepping
1104 SBCL includes an instrumentation based single-stepper for compiled
1105 code, that can be invoked via the @code{step} macro, or from within
1106 the debugger. @xref{Debugger Policy Control}, for details on enabling
1107 stepping for compiled code.
1109 The following debugger commands are used for controlling single stepping.
1111 @deffn {Debugger Command} @nopkg{start}
1112 Selects the @code{continue} restart if one exists and starts single stepping.
1113 None of the other single stepping commands can be used before stepping has
1114 been started either by using @code{start} or by using the standard
1115 @code{step} macro.
1116 @end deffn
1118 @deffn {Debugger Command} @nopkg{step}
1119 Steps into the current form. Stepping will be resumed when the next
1120 form that has been compiled with stepper instrumentation is evaluated.
1121 @end deffn
1123 @deffn {Debugger Command} @nopkg{next}
1124 Steps over the current form. Stepping will be disabled until evaluation of
1125 the form is complete.
1126 @end deffn
1128 @deffn {Debugger Command} @nopkg{out}
1129 Steps out of the current frame. Stepping will be disabled until the
1130 topmost stack frame that had been stepped into returns.
1131 @end deffn
1133 @deffn {Debugger Command} @nopkg{stop}
1134 Stops the single stepper and resumes normal execution.
1135 @end deffn
1137 @include macro-common-lisp-step.texinfo
1139 @node Enabling and Disabling the Debugger
1140 @comment  node-name,  next,  previous,  up
1141 @section Enabling and Disabling the Debugger
1143 @cindex debugger, enabling
1144 @cindex debugger, disabling
1145 @cindex disabling debugger
1146 @cindex ldb, enabling
1147 @cindex ldb, disabling
1148 @cindex disabling ldb
1150 In certain contexts (e.g., non-interactive applications), it may be
1151 desirable to turn off the SBCL debugger (and possibly re-enable it).
1152 The functions here control the debugger.
1154 @include fun-sb-ext-disable-debugger.texinfo
1156 @include fun-sb-ext-enable-debugger.texinfo