3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!DEBUG")
14 ;;;; variables and constants
16 ;;; things to consider when tweaking these values:
17 ;;; * We're afraid to just default them to NIL and NIL, in case the
18 ;;; user inadvertently causes a hairy data structure to be printed
19 ;;; when he inadvertently enters the debugger.
20 ;;; * We don't want to truncate output too much. These days anyone
21 ;;; can easily run their Lisp in a windowing system or under Emacs,
22 ;;; so it's not the end of the world even if the worst case is a
23 ;;; few thousand lines of output.
24 ;;; * As condition :REPORT methods are converted to use the pretty
25 ;;; printer, they acquire *PRINT-LEVEL* constraints, so e.g. under
26 ;;; sbcl-0.7.1.28's old value of *DEBUG-PRINT-LEVEL*=3, an
27 ;;; ARG-COUNT-ERROR printed as
28 ;;; error while parsing arguments to DESTRUCTURING-BIND:
29 ;;; invalid number of elements in
31 ;;; to satisfy lambda list
33 ;;; exactly 2 expected, but 5 found
35 ;;; FIXME: These variables were deprecated in late February 2004, and
36 ;;; can probably be removed in about a year.
37 (defvar *debug-print-level* 5
39 "(This is deprecated in favor of *DEBUG-PRINT-VARIABLE-ALIST*.)
41 *PRINT-LEVEL* for the debugger")
42 (defvar *debug-print-length* 7
44 "(This is deprecated in favor of *DEBUG-PRINT-VARIABLE-ALIST*.)
46 *PRINT-LENGTH* for the debugger")
48 (defvar *debug-print-variable-alist* nil
50 "an association list describing new bindings for special variables
51 (typically *PRINT-FOO* variables) to be used within the debugger, e.g.
52 ((*PRINT-LENGTH* . 10) (*PRINT-LEVEL* . 6) (*PRINT-PRETTY* . NIL))")
54 (defvar *debug-readtable*
55 ;; KLUDGE: This can't be initialized in a cold toplevel form,
56 ;; because the *STANDARD-READTABLE* isn't initialized until after
57 ;; cold toplevel forms have run. So instead we initialize it
58 ;; immediately after *STANDARD-READTABLE*. -- WHN 20000205
61 "*READTABLE* for the debugger")
63 (defvar *in-the-debugger* nil
65 "This is T while in the debugger.")
67 ;;; nestedness inside debugger command loops
68 (defvar *debug-command-level* 0)
70 ;;; If this is bound before the debugger is invoked, it is used as the
71 ;;; stack top by the debugger.
72 (defvar *stack-top-hint* nil)
74 (defvar *stack-top* nil)
75 (defvar *real-stack-top* nil)
77 (defvar *current-frame* nil)
79 ;;; Beginner-oriented help messages are important because you end up
80 ;;; in the debugger whenever something bad happens, or if you try to
81 ;;; get out of the system with Ctrl-C or (EXIT) or EXIT or whatever.
82 ;;; But after memorizing them the wasted screen space gets annoying..
83 (defvar *debug-beginner-help-p* t
84 "Should the debugger display beginner-oriented help messages?")
86 (defun debug-prompt (stream)
87 (sb!thread::get-foreground)
90 (sb!di:frame-number *current-frame*)
91 (> *debug-command-level* 1)
92 *debug-command-level*))
94 (defparameter *debug-help-string*
95 "The debug prompt is square brackets, with number(s) indicating the current
96 control stack level and, if you've entered the debugger recursively, how
97 deeply recursed you are.
98 Any command -- including the name of a restart -- may be uniquely abbreviated.
99 The debugger rebinds various special variables for controlling i/o, sometimes
100 to defaults (much like WITH-STANDARD-IO-SYNTAX does) and sometimes to
101 its own special values, e.g. SB-DEBUG:*DEBUG-PRINT-LEVEL*.
102 Debug commands do not affect *, //, and similar variables, but evaluation in
103 the debug loop does affect these variables.
104 SB-DEBUG:*FLUSH-DEBUG-ERRORS* controls whether errors at the debug prompt
105 drop you deeper into the debugger.
107 Getting in and out of the debugger:
108 RESTART invokes restart numbered as shown (prompt if not given).
109 ERROR prints the error condition and restart cases.
110 The number of any restart, or its name, or a unique abbreviation for its
111 name, is a valid command, and is the same as using RESTART to invoke
115 U up frame D down frame
116 B bottom frame F n frame n (n=0 for top frame)
119 BACKTRACE [n] shows n frames going down the stack.
120 LIST-LOCALS, L lists locals in current function.
121 PRINT, P displays current function call.
122 SOURCE [n] displays frame's source form with n levels of enclosing forms.
124 Breakpoints and steps:
125 LIST-LOCATIONS [{function | :C}] List the locations for breakpoints.
126 Specify :C for the current frame.
128 LIST-BREAKPOINTS List the active breakpoints.
129 Abbreviations: LB, LBP
130 DELETE-BREAKPOINT [n] Remove breakpoint n or all breakpoints.
131 Abbreviations: DEL, DBP
132 BREAKPOINT {n | :end | :start} [:break form] [:function function]
133 [{:print form}*] [:condition form]
135 Abbreviations: BR, BP
136 STEP [n] Step to the next location or step n times.
138 Function and macro commands:
140 Return the n'th argument in the current frame.
141 (SB-DEBUG:VAR string-or-symbol [id])
142 Returns the value of the specified variable in the current frame.
146 [EXPERIMENTAL] Return the values resulting from evaluation of expr
147 from the current frame, if this frame was compiled with a sufficiently
148 high DEBUG optimization quality.
150 Discard all pending input on *STANDARD-INPUT*. (This can be
151 useful when the debugger was invoked to handle an error in
152 deeply nested input syntax, and now the reader is confused.)")
154 ;;; This is used to communicate to DEBUG-LOOP that we are at a step breakpoint.
155 (define-condition step-condition (simple-condition) ())
157 ;;;; breakpoint state
159 (defvar *only-block-start-locations* nil
161 "When true, the LIST-LOCATIONS command only displays block start locations.
162 Otherwise, all locations are displayed.")
164 (defvar *print-location-kind* nil
166 "When true, list the code location type in the LIST-LOCATIONS command.")
168 ;;; a list of the types of code-locations that should not be stepped
169 ;;; to and should not be listed when listing breakpoints
170 (defvar *bad-code-location-types* '(:call-site :internal-error))
171 (declaim (type list *bad-code-location-types*))
173 ;;; code locations of the possible breakpoints
174 (defvar *possible-breakpoints*)
175 (declaim (type list *possible-breakpoints*))
177 ;;; a list of the made and active breakpoints, each is a
178 ;;; BREAKPOINT-INFO structure
179 (defvar *breakpoints* nil)
180 (declaim (type list *breakpoints*))
182 ;;; a list of BREAKPOINT-INFO structures of the made and active step
184 (defvar *step-breakpoints* nil)
185 (declaim (type list *step-breakpoints*))
187 ;;; the number of times left to step
188 (defvar *number-of-steps* 1)
189 (declaim (type integer *number-of-steps*))
191 ;;; This is used when listing and setting breakpoints.
192 (defvar *default-breakpoint-debug-fun* nil)
193 (declaim (type (or list sb!di:debug-fun) *default-breakpoint-debug-fun*))
195 ;;;; code location utilities
197 ;;; Return the first code-location in the passed debug block.
198 (defun first-code-location (debug-block)
200 (first-code-location nil))
201 (sb!di:do-debug-block-locations (code-location debug-block)
203 (setf first-code-location code-location)
205 first-code-location))
207 ;;; Return a list of the next code-locations following the one passed.
208 ;;; One of the *BAD-CODE-LOCATION-TYPES* will not be returned.
209 (defun next-code-locations (code-location)
210 (let ((debug-block (sb!di:code-location-debug-block code-location))
211 (block-code-locations nil))
212 (sb!di:do-debug-block-locations (block-code-location debug-block)
213 (unless (member (sb!di:code-location-kind block-code-location)
214 *bad-code-location-types*)
215 (push block-code-location block-code-locations)))
216 (setf block-code-locations (nreverse block-code-locations))
217 (let* ((code-loc-list (rest (member code-location block-code-locations
218 :test #'sb!di:code-location=)))
219 (next-list (cond (code-loc-list
220 (list (first code-loc-list)))
221 ((map 'list #'first-code-location
222 (sb!di:debug-block-successors debug-block)))
224 (when (and (= (length next-list) 1)
225 (sb!di:code-location= (first next-list) code-location))
226 (setf next-list (next-code-locations (first next-list))))
229 ;;; Return a list of code-locations of the possible breakpoints of DEBUG-FUN.
230 (defun possible-breakpoints (debug-fun)
231 (let ((possible-breakpoints nil))
232 (sb!di:do-debug-fun-blocks (debug-block debug-fun)
233 (unless (sb!di:debug-block-elsewhere-p debug-block)
234 (if *only-block-start-locations*
235 (push (first-code-location debug-block) possible-breakpoints)
236 (sb!di:do-debug-block-locations (code-location debug-block)
237 (when (not (member (sb!di:code-location-kind code-location)
238 *bad-code-location-types*))
239 (push code-location possible-breakpoints))))))
240 (nreverse possible-breakpoints)))
242 ;;; Search the info-list for the item passed (CODE-LOCATION,
243 ;;; DEBUG-FUN, or BREAKPOINT-INFO). If the item passed is a debug
244 ;;; function then kind will be compared if it was specified. The kind
245 ;;; if also compared if a breakpoint-info is passed since it's in the
246 ;;; breakpoint. The info structure is returned if found.
247 (defun location-in-list (place info-list &optional (kind nil))
248 (when (breakpoint-info-p place)
249 (setf kind (sb!di:breakpoint-kind (breakpoint-info-breakpoint place)))
250 (setf place (breakpoint-info-place place)))
251 (cond ((sb!di:code-location-p place)
252 (find place info-list
253 :key #'breakpoint-info-place
254 :test (lambda (x y) (and (sb!di:code-location-p y)
255 (sb!di:code-location= x y)))))
257 (find place info-list
258 :test (lambda (x-debug-fun y-info)
259 (let ((y-place (breakpoint-info-place y-info))
260 (y-breakpoint (breakpoint-info-breakpoint
262 (and (sb!di:debug-fun-p y-place)
263 (eq x-debug-fun y-place)
265 (eq kind (sb!di:breakpoint-kind
266 y-breakpoint))))))))))
268 ;;; If LOC is an unknown location, then try to find the block start
269 ;;; location. Used by source printing to some information instead of
270 ;;; none for the user.
271 (defun maybe-block-start-location (loc)
272 (if (sb!di:code-location-unknown-p loc)
273 (let* ((block (sb!di:code-location-debug-block loc))
274 (start (sb!di:do-debug-block-locations (loc block)
276 (cond ((and (not (sb!di:debug-block-elsewhere-p block))
278 ;; FIXME: Why output on T instead of *DEBUG-FOO* or something?
279 (format t "~%unknown location: using block start~%")
285 ;;;; the BREAKPOINT-INFO structure
287 ;;; info about a made breakpoint
288 (defstruct (breakpoint-info (:copier nil)
289 (:constructor %make-breakpoint-info))
290 ;; where we are going to stop
292 :type (or sb!di:code-location sb!di:debug-fun)
294 ;; the breakpoint returned by SB!DI:MAKE-BREAKPOINT
295 (breakpoint (missing-arg) :type sb!di:breakpoint :read-only t)
296 ;; the function returned from SB!DI:PREPROCESS-FOR-EVAL. If result is
297 ;; non-NIL, drop into the debugger.
298 (break #'identity :type function :read-only t)
299 ;; the function returned from SB!DI:PREPROCESS-FOR-EVAL. If result is
300 ;; non-NIL, eval (each) print and print results.
301 (condition #'identity :type function :read-only t)
302 ;; the list of functions from SB!DI:PREPROCESS-FOR-EVAL to evaluate.
303 ;; Results are conditionally printed. CAR of each element is the
304 ;; function, CDR is the form it goes with.
305 (print nil :type list :read-only t)
306 ;; the number used when listing the possible breakpoints within a
307 ;; function; or could also be a symbol such as START or END
308 (code-location-selector (missing-arg) :type (or symbol integer) :read-only t)
309 ;; the number used when listing the active breakpoints, and when
310 ;; deleting breakpoints
311 (breakpoint-number (missing-arg) :type integer :read-only t))
313 (defun create-breakpoint-info (place breakpoint code-location-selector
314 &key (break #'identity)
315 (condition #'identity) (print nil))
317 (sort *breakpoints* #'< :key #'breakpoint-info-breakpoint-number))
318 (let ((breakpoint-number
319 (do ((i 1 (incf i)) (breakpoints *breakpoints* (rest breakpoints)))
320 ((or (> i (length *breakpoints*))
321 (not (= i (breakpoint-info-breakpoint-number
322 (first breakpoints)))))
325 (%make-breakpoint-info :place place
326 :breakpoint breakpoint
327 :code-location-selector code-location-selector
328 :breakpoint-number breakpoint-number
333 (defun print-breakpoint-info (breakpoint-info)
334 (let ((place (breakpoint-info-place breakpoint-info))
335 (bp-number (breakpoint-info-breakpoint-number breakpoint-info)))
336 (case (sb!di:breakpoint-kind (breakpoint-info-breakpoint breakpoint-info))
338 (print-code-location-source-form place 0)
342 (breakpoint-info-code-location-selector breakpoint-info)
343 (sb!di:debug-fun-name (sb!di:code-location-debug-fun place))))
345 (format t "~&~S: FUN-START in ~S" bp-number
346 (sb!di:debug-fun-name place)))
348 (format t "~&~S: FUN-END in ~S" bp-number
349 (sb!di:debug-fun-name place))))))
351 ;;;; MAIN-HOOK-FUN for steps and breakpoints
353 ;;; This must be passed as the hook function. It keeps track of where
354 ;;; STEP breakpoints are.
355 (defun main-hook-fun (current-frame breakpoint &optional return-vals
357 (setf *default-breakpoint-debug-fun*
358 (sb!di:frame-debug-fun current-frame))
359 (dolist (step-info *step-breakpoints*)
360 (sb!di:delete-breakpoint (breakpoint-info-breakpoint step-info))
361 (let ((bp-info (location-in-list step-info *breakpoints*)))
363 (sb!di:activate-breakpoint (breakpoint-info-breakpoint bp-info)))))
364 (let ((*stack-top-hint* current-frame)
366 (location-in-list (sb!di:breakpoint-what breakpoint)
368 (sb!di:breakpoint-kind breakpoint)))
370 (location-in-list (sb!di:breakpoint-what breakpoint)
372 (sb!di:breakpoint-kind breakpoint)))
376 (setf *step-breakpoints* nil)
377 (labels ((build-string (str)
378 (setf string (concatenate 'string string str)))
379 (print-common-info ()
381 (with-output-to-string (*standard-output*)
383 (format t "~%Return values: ~S" return-vals))
385 (when (breakpoint-info-print bp-hit-info)
387 (print-frame-call current-frame))
388 (dolist (print (breakpoint-info-print bp-hit-info))
389 (format t "~& ~S = ~S" (rest print)
390 (funcall (first print) current-frame))))))))
392 (setf break (funcall (breakpoint-info-break bp-hit-info)
394 (setf condition (funcall (breakpoint-info-condition bp-hit-info)
396 (cond ((and bp-hit-info step-hit-info (= 1 *number-of-steps*))
397 (build-string (format nil "~&*Step (to a breakpoint)*"))
400 ((and bp-hit-info step-hit-info break)
401 (build-string (format nil "~&*Step (to a breakpoint)*"))
404 ((and bp-hit-info step-hit-info)
406 (format t "~A" string)
407 (decf *number-of-steps*)
408 (set-step-breakpoint current-frame))
409 ((and step-hit-info (= 1 *number-of-steps*))
410 (build-string "*Step*")
411 (break (make-condition 'step-condition :format-control string)))
413 (decf *number-of-steps*)
414 (set-step-breakpoint current-frame))
417 (build-string (format nil "~&*Breakpoint hit*")))
421 (format t "~A" string)))
423 (break "unknown breakpoint"))))))
425 ;;; Set breakpoints at the next possible code-locations. After calling
426 ;;; this, either (CONTINUE) if in the debugger or just let program flow
427 ;;; return if in a hook function.
428 (defun set-step-breakpoint (frame)
430 ((sb!di:debug-block-elsewhere-p (sb!di:code-location-debug-block
431 (sb!di:frame-code-location frame)))
432 ;; FIXME: FORMAT T is used for error output here and elsewhere in
434 (format t "cannot step, in elsewhere code~%"))
436 (let* ((code-location (sb!di:frame-code-location frame))
437 (next-code-locations (next-code-locations code-location)))
440 (dolist (code-location next-code-locations)
441 (let ((bp-info (location-in-list code-location *breakpoints*)))
443 (sb!di:deactivate-breakpoint (breakpoint-info-breakpoint
445 (let ((bp (sb!di:make-breakpoint #'main-hook-fun code-location
446 :kind :code-location)))
447 (sb!di:activate-breakpoint bp)
448 (push (create-breakpoint-info code-location bp 0)
449 *step-breakpoints*))))
451 (let* ((debug-fun (sb!di:frame-debug-fun *current-frame*))
452 (bp (sb!di:make-breakpoint #'main-hook-fun debug-fun
454 (sb!di:activate-breakpoint bp)
455 (push (create-breakpoint-info debug-fun bp 0)
456 *step-breakpoints*))))))))
460 ;;; ANSI specifies that this macro shall exist, even if only as a
461 ;;; trivial placeholder like this.
462 (defmacro step (form)
463 "This is a trivial placeholder implementation of the CL:STEP macro required
464 by the ANSI spec, simply expanding to `(LET () ,FORM). A more featureful
465 version would be welcome, we just haven't written it."
471 (defun backtrace (&optional (count most-positive-fixnum)
472 (*standard-output* *debug-io*))
474 "Show a listing of the call stack going down from the current frame. In the
475 debugger, the current frame is indicated by the prompt. COUNT is how many
477 (fresh-line *standard-output*)
478 (do ((frame (if *in-the-debugger* *current-frame* (sb!di:top-frame))
479 (sb!di:frame-down frame))
480 (count count (1- count)))
481 ((or (null frame) (zerop count)))
482 (print-frame-call frame :number t))
483 (fresh-line *standard-output*)
486 (defun backtrace-as-list (&optional (count most-positive-fixnum))
487 #!+sb-doc "Return a list representing the current BACKTRACE."
488 (do ((reversed-result nil)
489 (frame (if *in-the-debugger* *current-frame* (sb!di:top-frame))
490 (sb!di:frame-down frame))
491 (count count (1- count)))
492 ((or (null frame) (zerop count))
493 (nreverse reversed-result))
494 (push (frame-call-as-list frame) reversed-result)))
496 (defun frame-call-as-list (frame)
497 (cons (sb!di:debug-fun-name (sb!di:frame-debug-fun frame))
498 (frame-args-as-list frame)))
502 (eval-when (:compile-toplevel :execute)
504 ;;; This is a convenient way to express what to do for each type of
505 ;;; lambda-list element.
506 (sb!xc:defmacro lambda-list-element-dispatch (element
517 (ecase (car ,element)
518 (:optional ,@optional)
520 (:keyword ,@keyword)))
522 (aver (eq ,element :deleted))
525 (sb!xc:defmacro lambda-var-dispatch (variable location deleted valid other)
526 (let ((var (gensym)))
527 `(let ((,var ,variable))
528 (cond ((eq ,var :deleted) ,deleted)
529 ((eq (sb!di:debug-var-validity ,var ,location) :valid)
535 ;;; This is used in constructing arg lists for debugger printing when
536 ;;; the arg list is unavailable, some arg is unavailable or unused, etc.
537 (defstruct (unprintable-object
538 (:constructor make-unprintable-object (string))
539 (:print-object (lambda (x s)
540 (print-unreadable-object (x s)
541 (write-string (unprintable-object-string x)
546 ;;; Extract the function argument values for a debug frame.
547 (defun frame-args-as-list (frame)
548 (let ((debug-fun (sb!di:frame-debug-fun frame))
549 (loc (sb!di:frame-code-location frame))
550 (reversed-result nil))
553 (dolist (ele (sb!di:debug-fun-lambda-list debug-fun))
554 (lambda-list-element-dispatch ele
555 :required ((push (frame-call-arg ele loc frame) reversed-result))
556 :optional ((push (frame-call-arg (second ele) loc frame)
558 :keyword ((push (second ele) reversed-result)
559 (push (frame-call-arg (third ele) loc frame)
561 :deleted ((push (frame-call-arg ele loc frame) reversed-result))
562 :rest ((lambda-var-dispatch (second ele) loc
565 (setf reversed-result
566 (append (reverse (sb!di:debug-var-value
570 (push (make-unprintable-object
571 "unavailable &REST argument")
573 ;; As long as we do an ordinary return (as opposed to SIGNALing
574 ;; a CONDITION) from the DOLIST above:
575 (nreverse reversed-result))
576 (sb!di:lambda-list-unavailable
578 (make-unprintable-object "unavailable lambda list")))))
580 ;;; Print FRAME with verbosity level 1. If we hit a &REST arg, then
581 ;;; print as many of the values as possible, punting the loop over
582 ;;; lambda-list variables since any other arguments will be in the
583 ;;; &REST arg's list of values.
584 (defun print-frame-call-1 (frame)
585 (let ((debug-fun (sb!di:frame-debug-fun frame)))
587 (pprint-logical-block (*standard-output* nil :prefix "(" :suffix ")")
588 (let ((args (ensure-printable-object (frame-args-as-list frame))))
589 ;; Since we go to some trouble to make nice informative function
590 ;; names like (PRINT-OBJECT :AROUND (CLOWN T)), let's make sure
591 ;; that they aren't truncated by *PRINT-LENGTH* and *PRINT-LEVEL*.
592 (let ((*print-length* nil)
594 (prin1 (ensure-printable-object (sb!di:debug-fun-name debug-fun))))
595 ;; For the function arguments, we can just print normally.
597 (format t "~{ ~_~S~}" args)
598 (format t " ~S" args))))
600 (when (sb!di:debug-fun-kind debug-fun)
602 (prin1 (sb!di:debug-fun-kind debug-fun))
605 (defun ensure-printable-object (object)
607 (with-open-stream (out (make-broadcast-stream))
611 (declare (ignore cond))
612 (make-unprintable-object "error printing object"))))
614 (defun frame-call-arg (var location frame)
615 (lambda-var-dispatch var location
616 (make-unprintable-object "unused argument")
617 (sb!di:debug-var-value var frame)
618 (make-unprintable-object "unavailable argument")))
620 ;;; Prints a representation of the function call causing FRAME to
621 ;;; exist. VERBOSITY indicates the level of information to output;
622 ;;; zero indicates just printing the DEBUG-FUN's name, and one
623 ;;; indicates displaying call-like, one-liner format with argument
625 (defun print-frame-call (frame &key (verbosity 1) (number nil))
629 (format t "~&~S: " (sb!di:frame-number frame)))
630 (format t "~S" frame))
633 (format t "~&~S: " (sb!di:frame-number frame)))
634 (print-frame-call-1 frame)))
635 (when (>= verbosity 2)
636 (let ((loc (sb!di:frame-code-location frame)))
639 (sb!di:code-location-debug-block loc)
640 (format t "~%source: ")
641 (print-code-location-source-form loc 0))
642 (sb!di:debug-condition (ignore) ignore)
643 (error (c) (format t "error finding source: ~A" c))))))
647 (defvar *debugger-hook* nil
649 "This is either NIL or a function of two arguments, a condition and the value
650 of *DEBUGGER-HOOK*. This function can either handle the condition or return
651 which causes the standard debugger to execute. The system passes the value
652 of this variable to the function because it binds *DEBUGGER-HOOK* to NIL
653 around the invocation.")
655 (defvar *invoke-debugger-hook* nil
657 "This is either NIL or a designator for a function of two arguments,
658 to be run when the debugger is about to be entered. The function is
659 run with *INVOKE-DEBUGGER-HOOK* bound to NIL to minimize recursive
660 errors, and receives as arguments the condition that triggered
661 debugger entry and the previous value of *INVOKE-DEBUGGER-HOOK*
663 This mechanism is an SBCL extension similar to the standard *DEBUGGER-HOOK*.
664 In contrast to *DEBUGGER-HOOK*, it is observed by INVOKE-DEBUGGER even when
667 ;;; These are bound on each invocation of INVOKE-DEBUGGER.
668 (defvar *debug-restarts*)
669 (defvar *debug-condition*)
670 (defvar *nested-debug-condition*)
672 ;;; Oh, what a tangled web we weave when we preserve backwards
673 ;;; compatibility with 1968-style use of global variables to control
674 ;;; per-stream i/o properties; there's really no way to get this
675 ;;; quite right, but we do what we can.
676 (defun funcall-with-debug-io-syntax (fun &rest rest)
677 (declare (type function fun))
678 ;; Try to force the other special variables into a useful state.
679 (let (;; Protect from WITH-STANDARD-IO-SYNTAX some variables where
680 ;; any default we might use is less useful than just reusing
681 ;; the global values.
682 (original-package *package*)
683 (original-print-pretty *print-pretty*))
684 (with-standard-io-syntax
685 (let (;; We want the printer and reader to be in a useful state,
686 ;; regardless of where the debugger was invoked in the
687 ;; program. WITH-STANDARD-IO-SYNTAX did much of what we
689 ;; * It doesn't affect our internal special variables
690 ;; like *CURRENT-LEVEL-IN-PRINT*.
691 ;; * It isn't customizable.
692 ;; * It doesn't set *PRINT-READABLY* to the same value
693 ;; as the toplevel default.
694 ;; * It sets *PACKAGE* to COMMON-LISP-USER, which is not
695 ;; helpful behavior for a debugger.
696 ;; * There's no particularly good debugger default for
697 ;; *PRINT-PRETTY*, since T is usually what you want
698 ;; -- except absolutely not what you want when you're
699 ;; debugging failures in PRINT-OBJECT logic.
700 ;; We try to address all these issues with explicit
702 (sb!kernel:*current-level-in-print* 0)
703 (*package* original-package)
704 (*print-pretty* original-print-pretty)
705 (*print-readably* nil)
706 ;; Clear the circularity machinery to try to to reduce the
707 ;; pain from sharing the circularity table across all
708 ;; streams; if these are not rebound here, then setting
709 ;; *PRINT-CIRCLE* within the debugger when debugging in a
710 ;; state where something circular was being printed (e.g.,
711 ;; because the debugger was entered on an error in a
712 ;; PRINT-OBJECT method) makes a hopeless mess. Binding them
713 ;; here does seem somewhat ugly because it makes it more
714 ;; difficult to debug the printing-of-circularities code
715 ;; itself; however, as far as I (WHN, 2004-05-29) can see,
716 ;; that's almost entirely academic as long as there's one
717 ;; shared *C-H-T* for all streams (i.e., it's already
718 ;; unreasonably difficult to debug print-circle machinery
719 ;; given the buggy crosstalk between the debugger streams
720 ;; and the stream you're trying to watch), and any fix for
721 ;; that buggy arrangement will likely let this hack go away
723 (sb!impl::*circularity-hash-table* . nil)
724 (sb!impl::*circularity-counter* . nil)
725 ;; These rebindings are now (as of early 2004) deprecated,
726 ;; with the new *PRINT-VAR-ALIST* mechanism preferred.
727 (*print-length* *debug-print-length*)
728 (*print-level* *debug-print-level*)
729 (*readtable* *debug-readtable*))
731 ;; (Why NREVERSE? PROGV makes the later entries have
732 ;; precedence over the earlier entries.
733 ;; *DEBUG-PRINT-VARIABLE-ALIST* is called an alist, so it's
734 ;; expected that its earlier entries have precedence. And
735 ;; the earlier-has-precedence behavior is mostly more
736 ;; convenient, so that programmers can use PUSH or LIST* to
737 ;; customize *DEBUG-PRINT-VARIABLE-ALIST*.)
738 (nreverse (mapcar #'car *debug-print-variable-alist*))
739 (nreverse (mapcar #'cdr *debug-print-variable-alist*))
740 (apply fun rest))))))
742 ;;; the ordinary ANSI case of INVOKE-DEBUGGER, when not suppressed by
743 ;;; command-line --disable-debugger option
744 (defun invoke-debugger (condition)
746 "Enter the debugger."
748 (let ((old-hook *debugger-hook*))
750 (let ((*debugger-hook* nil))
751 (funcall old-hook condition old-hook))))
752 (let ((old-hook *invoke-debugger-hook*))
754 (let ((*invoke-debugger-hook* nil))
755 (funcall old-hook condition old-hook))))
757 ;; Note: CMU CL had (SB-UNIX:UNIX-SIGSETMASK 0) here, to reset the
758 ;; signal state in the case that we wind up in the debugger as a
759 ;; result of something done by a signal handler. It's not
760 ;; altogether obvious that this is necessary, and indeed SBCL has
761 ;; not been doing it since 0.7.8.5. But nobody seems altogether
763 ;; -- dan 2003.11.11, based on earlier comment of WHN 2002-09-28
765 ;; We definitely want *PACKAGE* to be of valid type.
767 ;; Elsewhere in the system, we use the SANE-PACKAGE function for
768 ;; this, but here causing an exception just as we're trying to handle
769 ;; an exception would be confusing, so instead we use a special hack.
770 (unless (and (packagep *package*)
771 (package-name *package*))
772 (setf *package* (find-package :cl-user))
773 (format *error-output*
774 "The value of ~S was not an undeleted PACKAGE. It has been
776 '*package* *package*))
778 ;; Before we start our own output, finish any pending output.
779 ;; Otherwise, if the user tried to track the progress of his program
780 ;; using PRINT statements, he'd tend to lose the last line of output
781 ;; or so, which'd be confusing.
782 (flush-standard-output-streams)
784 (funcall-with-debug-io-syntax #'%invoke-debugger condition))
786 (defun %invoke-debugger (condition)
788 (let ((*debug-condition* condition)
789 (*debug-restarts* (compute-restarts condition))
790 (*nested-debug-condition* nil))
792 ;; (The initial output here goes to *ERROR-OUTPUT*, because the
793 ;; initial output is not interactive, just an error message, and
794 ;; when people redirect *ERROR-OUTPUT*, they could reasonably
795 ;; expect to see error messages logged there, regardless of what
796 ;; the debugger does afterwards.)
797 (format *error-output*
798 "~2&~@<debugger invoked on a ~S in thread ~A: ~
800 (type-of *debug-condition*)
801 (sb!thread:current-thread-id)
804 (setf *nested-debug-condition* condition)
805 (let ((ndc-type (type-of *nested-debug-condition*)))
806 (format *error-output*
807 "~&~@<(A ~S was caught when trying to print ~S when ~
808 entering the debugger. Printing was aborted and the ~
809 ~S was stored in ~S.)~@:>~%"
813 '*nested-debug-condition*))
814 (when (typep condition 'cell-error)
815 ;; what we really want to know when it's e.g. an UNBOUND-VARIABLE:
816 (format *error-output*
817 "~&(CELL-ERROR-NAME ~S) = ~S~%"
819 (cell-error-name *debug-condition*)))))
821 (let ((background-p (sb!thread::debugger-wait-until-foreground-thread
824 ;; After the initial error/condition/whatever announcement to
825 ;; *ERROR-OUTPUT*, we become interactive, and should talk on
826 ;; *DEBUG-IO* from now on. (KLUDGE: This is a normative
827 ;; statement, not a description of reality.:-| There's a lot of
828 ;; older debugger code which was written to do i/o on whatever
829 ;; stream was in fashion at the time, and not all of it has
830 ;; been converted to behave this way. -- WHN 2000-11-16)
833 (let (;; FIXME: Rebinding *STANDARD-OUTPUT* here seems wrong,
834 ;; violating the principle of least surprise, and making
835 ;; it impossible for the user to do reasonable things
836 ;; like using PRINT at the debugger prompt to send output
837 ;; to the program's ordinary (possibly
838 ;; redirected-to-a-file) *STANDARD-OUTPUT*. (CMU CL
839 ;; used to rebind *STANDARD-INPUT* here too, but that's
840 ;; been fixed already.)
841 (*standard-output* *debug-io*)
842 ;; This seems reasonable: e.g. if the user has redirected
843 ;; *ERROR-OUTPUT* to some log file, it's probably wrong
844 ;; to send errors which occur in interactive debugging to
845 ;; that file, and right to send them to *DEBUG-IO*.
846 (*error-output* *debug-io*))
847 (unless (typep condition 'step-condition)
848 (when *debug-beginner-help-p*
850 "~%~@<You can type HELP for debugger help, or ~
851 (SB-EXT:QUIT) to exit from SBCL.~:@>~2%"))
852 (show-restarts *debug-restarts* *debug-io*))
855 (sb!thread::release-foreground))))))
857 ;;; this function is for use in *INVOKE-DEBUGGER-HOOK* when ordinary
858 ;;; ANSI behavior has been suppressed by the "--disable-debugger"
859 ;;; command-line option
860 (defun debugger-disabled-hook (condition me)
861 (declare (ignore me))
862 ;; There is no one there to interact with, so report the
863 ;; condition and terminate the program.
864 (flet ((failure-quit (&key recklessly-p)
865 (/show0 "in FAILURE-QUIT (in --disable-debugger debugger hook)")
866 (quit :unix-status 1 :recklessly-p recklessly-p)))
867 ;; This HANDLER-CASE is here mostly to stop output immediately
868 ;; (and fall through to QUIT) when there's an I/O error. Thus,
869 ;; when we're run under a shell script or something, we can die
870 ;; cleanly when the script dies (and our pipes are cut), instead
871 ;; of falling into ldb or something messy like that. Similarly, we
872 ;; can terminate cleanly even if BACKTRACE dies because of bugs in
873 ;; user PRINT-OBJECT methods.
876 (format *error-output*
877 "~&~@<unhandled condition (of type ~S): ~2I~_~A~:>~2%"
880 ;; Flush *ERROR-OUTPUT* even before the BACKTRACE, so that
881 ;; even if we hit an error within BACKTRACE (e.g. a bug in
882 ;; the debugger's own frame-walking code, or a bug in a user
883 ;; PRINT-OBJECT method) we'll at least have the CONDITION
884 ;; printed out before we die.
885 (finish-output *error-output*)
886 ;; (Where to truncate the BACKTRACE is of course arbitrary, but
887 ;; it seems as though we should at least truncate it somewhere.)
888 (sb!debug:backtrace 128 *error-output*)
891 "~%unhandled condition in --disable-debugger mode, quitting~%")
892 (finish-output *error-output*)
895 ;; We IGNORE-ERRORS here because even %PRIMITIVE PRINT can
896 ;; fail when our output streams are blown away, as e.g. when
897 ;; we're running under a Unix shell script and it dies somehow
898 ;; (e.g. because of a SIGINT). In that case, we might as well
899 ;; just give it up for a bad job, and stop trying to notify
900 ;; the user of anything.
902 ;; Actually, the only way I've run across to exercise the
903 ;; problem is to have more than one layer of shell script.
904 ;; I have a shell script which does
905 ;; time nice -10 sh make.sh "$1" 2>&1 | tee make.tmp
906 ;; and the problem occurs when I interrupt this with Ctrl-C
907 ;; under Linux 2.2.14-5.0 and GNU bash, version 1.14.7(1).
908 ;; I haven't figured out whether it's bash, time, tee, Linux, or
909 ;; what that is responsible, but that it's possible at all
910 ;; means that we should IGNORE-ERRORS here. -- WHN 2001-04-24
913 "Argh! error within --disable-debugger error handling"))
914 (failure-quit :recklessly-p t)))))
916 ;;; halt-on-failures and prompt-on-failures modes, suitable for
917 ;;; noninteractive and interactive use respectively
918 (defun disable-debugger ()
919 (when (eql *invoke-debugger-hook* nil)
920 (setf *debug-io* *error-output*
921 *invoke-debugger-hook* 'debugger-disabled-hook)))
923 (defun enable-debugger ()
924 (when (eql *invoke-debugger-hook* 'debugger-disabled-hook)
925 (setf *invoke-debugger-hook* nil)))
927 (setf *debug-io* *query-io*)
929 (defun show-restarts (restarts s)
930 (cond ((null restarts)
932 "~&(no restarts: If you didn't do this on purpose, ~
933 please report it as a bug.)~%"))
935 (format s "~&restarts (invokable by number or by ~
936 possibly-abbreviated name):~%")
940 (dolist (restart restarts)
941 (let ((name (restart-name restart)))
943 (let ((len (length (princ-to-string name))))
944 (when (> len max-name-len)
945 (setf max-name-len len))))))
946 (unless (zerop max-name-len)
947 (incf max-name-len 3))
948 (dolist (restart restarts)
949 (let ((name (restart-name restart)))
950 (cond ((member name names-used)
951 (format s "~& ~2D: ~V@T~A~%" count max-name-len restart))
953 (format s "~& ~2D: [~VA] ~A~%"
954 count (- max-name-len 3) name restart)
955 (push name names-used))))
958 (defvar *debug-loop-fun* #'debug-loop-fun
959 "a function taking no parameters that starts the low-level debug loop")
961 ;;; This calls DEBUG-LOOP, performing some simple initializations
962 ;;; before doing so. INVOKE-DEBUGGER calls this to actually get into
963 ;;; the debugger. SB!KERNEL::ERROR-ERROR calls this in emergencies
964 ;;; to get into a debug prompt as quickly as possible with as little
965 ;;; risk as possible for stepping on whatever is causing recursive
967 (defun internal-debug ()
968 (let ((*in-the-debugger* t)
969 (*read-suppress* nil))
970 (unless (typep *debug-condition* 'step-condition)
971 (clear-input *debug-io*))
972 (funcall *debug-loop-fun*)))
976 ;;; Note: This defaulted to T in CMU CL. The changed default in SBCL
977 ;;; was motivated by desire to play nicely with ILISP.
978 (defvar *flush-debug-errors* nil
980 "When set, avoid calling INVOKE-DEBUGGER recursively when errors occur while
981 executing in the debugger.")
983 (defun debug-loop-fun ()
984 (let* ((*debug-command-level* (1+ *debug-command-level*))
985 (*real-stack-top* (sb!di:top-frame))
986 (*stack-top* (or *stack-top-hint* *real-stack-top*))
987 (*stack-top-hint* nil)
988 (*current-frame* *stack-top*))
989 (handler-bind ((sb!di:debug-condition
991 (princ condition *debug-io*)
992 (/show0 "handling d-c by THROWing DEBUG-LOOP-CATCHER")
993 (throw 'debug-loop-catcher nil))))
995 (print-frame-call *current-frame* :verbosity 2)
997 (catch 'debug-loop-catcher
998 (handler-bind ((error (lambda (condition)
999 (when *flush-debug-errors*
1000 (clear-input *debug-io*)
1002 ;; FIXME: Doing input on *DEBUG-IO*
1003 ;; and output on T seems broken.
1005 "~&error flushed (because ~
1007 '*flush-debug-errors*)
1008 (/show0 "throwing DEBUG-LOOP-CATCHER")
1009 (throw 'debug-loop-catcher nil)))))
1010 ;; We have to bind LEVEL for the restart function created by
1011 ;; WITH-SIMPLE-RESTART.
1012 (let ((level *debug-command-level*)
1013 (restart-commands (make-restart-commands)))
1014 (with-simple-restart (abort
1015 "~@<Reduce debugger level (to debug level ~W).~@:>"
1017 (debug-prompt *debug-io*)
1018 (force-output *debug-io*)
1019 (let* ((exp (read *debug-io*))
1020 (cmd-fun (debug-command-p exp restart-commands)))
1021 (cond ((not cmd-fun)
1022 (debug-eval-print exp))
1024 (format t "~&Your command, ~S, is ambiguous:~%"
1026 (dolist (ele cmd-fun)
1027 (format t " ~A~%" ele)))
1029 (funcall cmd-fun))))))))))))
1031 ;;; FIXME: We could probably use INTERACTIVE-EVAL for much of this logic.
1032 (defun debug-eval-print (expr)
1033 (/noshow "entering DEBUG-EVAL-PRINT" expr)
1034 (/noshow (fboundp 'compile))
1035 (setq +++ ++ ++ + + - - expr)
1036 (let* ((values (multiple-value-list (eval -)))
1037 (*standard-output* *debug-io*))
1038 (/noshow "done with EVAL in DEBUG-EVAL-PRINT")
1040 (if values (prin1 (car values)))
1041 (dolist (x (cdr values))
1044 (setq /// // // / / values)
1045 (setq *** ** ** * * (car values))
1046 ;; Make sure that nobody passes back an unbound marker.
1050 ;; FIXME: The way INTERACTIVE-EVAL does this seems better.
1051 (princ "Setting * to NIL (was unbound marker)."))))
1053 ;;;; debug loop functions
1055 ;;; These commands are functions, not really commands, so that users
1056 ;;; can get their hands on the values returned.
1058 (eval-when (:execute :compile-toplevel)
1060 (sb!xc:defmacro define-var-operation (ref-or-set &optional value-var)
1061 `(let* ((temp (etypecase name
1062 (symbol (sb!di:debug-fun-symbol-vars
1063 (sb!di:frame-debug-fun *current-frame*)
1065 (simple-string (sb!di:ambiguous-debug-vars
1066 (sb!di:frame-debug-fun *current-frame*)
1068 (location (sb!di:frame-code-location *current-frame*))
1069 ;; Let's only deal with valid variables.
1070 (vars (remove-if-not (lambda (v)
1071 (eq (sb!di:debug-var-validity v location)
1074 (declare (list vars))
1076 (error "No known valid variables match ~S." name))
1077 ((= (length vars) 1)
1080 '(sb!di:debug-var-value (car vars) *current-frame*))
1082 `(setf (sb!di:debug-var-value (car vars) *current-frame*)
1085 ;; Since we have more than one, first see whether we have
1086 ;; any variables that exactly match the specification.
1087 (let* ((name (etypecase name
1088 (symbol (symbol-name name))
1089 (simple-string name)))
1090 ;; FIXME: REMOVE-IF-NOT is deprecated, use STRING/=
1092 (exact (remove-if-not (lambda (v)
1093 (string= (sb!di:debug-var-symbol-name v)
1096 (vars (or exact vars)))
1097 (declare (simple-string name)
1100 ;; Check now for only having one variable.
1101 ((= (length vars) 1)
1104 '(sb!di:debug-var-value (car vars) *current-frame*))
1106 `(setf (sb!di:debug-var-value (car vars) *current-frame*)
1108 ;; If there weren't any exact matches, flame about
1109 ;; ambiguity unless all the variables have the same
1114 (string= (sb!di:debug-var-symbol-name v)
1115 (sb!di:debug-var-symbol-name (car vars))))
1117 (error "specification ambiguous:~%~{ ~A~%~}"
1118 (mapcar #'sb!di:debug-var-symbol-name
1120 vars :test #'string=
1121 :key #'sb!di:debug-var-symbol-name))))
1122 ;; All names are the same, so see whether the user
1123 ;; ID'ed one of them.
1125 (let ((v (find id vars :key #'sb!di:debug-var-id)))
1128 "invalid variable ID, ~W: should have been one of ~S"
1130 (mapcar #'sb!di:debug-var-id vars)))
1133 '(sb!di:debug-var-value v *current-frame*))
1135 `(setf (sb!di:debug-var-value v *current-frame*)
1138 (error "Specify variable ID to disambiguate ~S. Use one of ~S."
1140 (mapcar #'sb!di:debug-var-id vars)))))))))
1144 ;;; FIXME: This doesn't work. It would be real nice we could make it
1145 ;;; work! Alas, it doesn't seem to work in CMU CL X86 either..
1146 (defun var (name &optional (id 0 id-supplied))
1148 "Return a variable's value if possible. NAME is a simple-string or symbol.
1149 If it is a simple-string, it is an initial substring of the variable's name.
1150 If name is a symbol, it has the same name and package as the variable whose
1151 value this function returns. If the symbol is uninterned, then the variable
1152 has the same name as the symbol, but it has no package.
1154 If name is the initial substring of variables with different names, then
1155 this return no values after displaying the ambiguous names. If name
1156 determines multiple variables with the same name, then you must use the
1157 optional id argument to specify which one you want. If you left id
1158 unspecified, then this returns no values after displaying the distinguishing
1161 The result of this function is limited to the availability of variable
1162 information. This is SETF'able."
1163 (define-var-operation :ref))
1164 (defun (setf var) (value name &optional (id 0 id-supplied))
1165 (define-var-operation :set value))
1167 ;;; This returns the COUNT'th arg as the user sees it from args, the
1168 ;;; result of SB!DI:DEBUG-FUN-LAMBDA-LIST. If this returns a
1169 ;;; potential DEBUG-VAR from the lambda-list, then the second value is
1170 ;;; T. If this returns a keyword symbol or a value from a rest arg,
1171 ;;; then the second value is NIL.
1173 ;;; FIXME: There's probably some way to merge the code here with
1174 ;;; FRAME-ARGS-AS-LIST. (A fair amount of logic is already shared
1175 ;;; through LAMBDA-LIST-ELEMENT-DISPATCH, but I suspect more could be.)
1176 (declaim (ftype (function (index list)) nth-arg))
1177 (defun nth-arg (count args)
1179 (dolist (ele args (error "The argument specification ~S is out of range."
1181 (lambda-list-element-dispatch ele
1182 :required ((if (zerop n) (return (values ele t))))
1183 :optional ((if (zerop n) (return (values (second ele) t))))
1184 :keyword ((cond ((zerop n)
1185 (return (values (second ele) nil)))
1187 (return (values (third ele) t)))))
1188 :deleted ((if (zerop n) (return (values ele t))))
1189 :rest ((let ((var (second ele)))
1190 (lambda-var-dispatch var (sb!di:frame-code-location
1192 (error "unused &REST argument before n'th argument")
1194 (sb!di:debug-var-value var *current-frame*)
1196 "The argument specification ~S is out of range."
1199 (return-from nth-arg (values value nil))
1201 (error "invalid &REST argument before n'th argument")))))
1206 "Return the N'th argument's value if possible. Argument zero is the first
1207 argument in a frame's default printed representation. Count keyword/value
1208 pairs as separate arguments."
1209 (multiple-value-bind (var lambda-var-p)
1210 (nth-arg n (handler-case (sb!di:debug-fun-lambda-list
1211 (sb!di:frame-debug-fun *current-frame*))
1212 (sb!di:lambda-list-unavailable ()
1213 (error "No argument values are available."))))
1215 (lambda-var-dispatch var (sb!di:frame-code-location *current-frame*)
1216 (error "Unused arguments have no values.")
1217 (sb!di:debug-var-value var *current-frame*)
1218 (error "invalid argument value"))
1221 ;;;; machinery for definition of debug loop commands
1223 (defvar *debug-commands* nil)
1225 ;;; Interface to *DEBUG-COMMANDS*. No required arguments in args are
1227 (defmacro !def-debug-command (name args &rest body)
1228 (let ((fun-name (symbolicate name "-DEBUG-COMMAND")))
1230 (setf *debug-commands*
1231 (remove ,name *debug-commands* :key #'car :test #'string=))
1232 (defun ,fun-name ,args
1233 (unless *in-the-debugger*
1234 (error "invoking debugger command while outside the debugger"))
1236 (push (cons ,name #',fun-name) *debug-commands*)
1239 (defun !def-debug-command-alias (new-name existing-name)
1240 (let ((pair (assoc existing-name *debug-commands* :test #'string=)))
1241 (unless pair (error "unknown debug command name: ~S" existing-name))
1242 (push (cons new-name (cdr pair)) *debug-commands*))
1245 ;;; This takes a symbol and uses its name to find a debugger command,
1246 ;;; using initial substring matching. It returns the command function
1247 ;;; if form identifies only one command, but if form is ambiguous,
1248 ;;; this returns a list of the command names. If there are no matches,
1249 ;;; this returns nil. Whenever the loop that looks for a set of
1250 ;;; possibilities encounters an exact name match, we return that
1251 ;;; command function immediately.
1252 (defun debug-command-p (form &optional other-commands)
1253 (if (or (symbolp form) (integerp form))
1257 (format nil "~W" form)))
1260 (declare (simple-string name)
1264 ;; Find matching commands, punting if exact match.
1265 (flet ((match-command (ele)
1266 (let* ((str (car ele))
1267 (str-len (length str)))
1268 (declare (simple-string str)
1270 (cond ((< str-len len))
1272 (when (string= name str :end1 len :end2 len)
1273 (return-from debug-command-p (cdr ele))))
1274 ((string= name str :end1 len :end2 len)
1276 (mapc #'match-command *debug-commands*)
1277 (mapc #'match-command other-commands))
1279 ;; Return the right value.
1280 (cond ((not res) nil)
1283 (t ; Just return the names.
1284 (do ((cmds res (cdr cmds)))
1286 (setf (car cmds) (caar cmds))))))))
1288 ;;; Return a list of debug commands (in the same format as
1289 ;;; *DEBUG-COMMANDS*) that invoke each active restart.
1291 ;;; Two commands are made for each restart: one for the number, and
1292 ;;; one for the restart name (unless it's been shadowed by an earlier
1293 ;;; restart of the same name, or it is NIL).
1294 (defun make-restart-commands (&optional (restarts *debug-restarts*))
1296 (num 0)) ; better be the same as show-restarts!
1297 (dolist (restart restarts)
1298 (let ((name (string (restart-name restart))))
1301 (/show0 "in restart-command closure, about to i-r-i")
1302 (invoke-restart-interactively restart))))
1303 (push (cons (prin1-to-string num) restart-fun) commands)
1304 (unless (or (null (restart-name restart))
1305 (find name commands :key #'car :test #'string=))
1306 (push (cons name restart-fun) commands))))
1310 ;;;; frame-changing commands
1312 (!def-debug-command "UP" ()
1313 (let ((next (sb!di:frame-up *current-frame*)))
1315 (setf *current-frame* next)
1316 (print-frame-call next))
1318 (format t "~&Top of stack.")))))
1320 (!def-debug-command "DOWN" ()
1321 (let ((next (sb!di:frame-down *current-frame*)))
1323 (setf *current-frame* next)
1324 (print-frame-call next))
1326 (format t "~&Bottom of stack.")))))
1328 (!def-debug-command-alias "D" "DOWN")
1330 ;;; CMU CL had this command, but SBCL doesn't, since it's redundant
1331 ;;; with "FRAME 0", and it interferes with abbreviations for the
1332 ;;; TOPLEVEL restart.
1333 ;;;(!def-debug-command "TOP" ()
1334 ;;; (do ((prev *current-frame* lead)
1335 ;;; (lead (sb!di:frame-up *current-frame*) (sb!di:frame-up lead)))
1337 ;;; (setf *current-frame* prev)
1338 ;;; (print-frame-call prev))))
1340 (!def-debug-command "BOTTOM" ()
1341 (do ((prev *current-frame* lead)
1342 (lead (sb!di:frame-down *current-frame*) (sb!di:frame-down lead)))
1344 (setf *current-frame* prev)
1345 (print-frame-call prev))))
1347 (!def-debug-command-alias "B" "BOTTOM")
1349 (!def-debug-command "FRAME" (&optional
1350 (n (read-prompting-maybe "frame number: ")))
1351 (setf *current-frame*
1352 (multiple-value-bind (next-frame-fun limit-string)
1353 (if (< n (sb!di:frame-number *current-frame*))
1354 (values #'sb!di:frame-up "top")
1355 (values #'sb!di:frame-down "bottom"))
1356 (do ((frame *current-frame*))
1357 ((= n (sb!di:frame-number frame))
1359 (let ((next-frame (funcall next-frame-fun frame)))
1361 (setf frame next-frame))
1364 "The ~A of the stack was encountered.~%"
1366 (return frame)))))))
1367 (print-frame-call *current-frame*))
1369 (!def-debug-command-alias "F" "FRAME")
1371 ;;;; commands for entering and leaving the debugger
1373 ;;; CMU CL supported this QUIT debug command, but SBCL provides this
1374 ;;; functionality with a restart instead. (The QUIT debug command was
1375 ;;; removed because it's confusing to have "quit" mean two different
1376 ;;; things in the system, "restart the top level REPL" in the debugger
1377 ;;; and "terminate the Lisp system" as the SB-EXT:QUIT function.)
1379 ;;;(!def-debug-command "QUIT" ()
1380 ;;; (throw 'sb!impl::toplevel-catcher nil))
1382 ;;; CMU CL supported this GO debug command, but SBCL doesn't -- in
1383 ;;; SBCL you just type the CONTINUE restart name instead (or "C" or
1384 ;;; "RESTART CONTINUE", that's OK too).
1385 ;;;(!def-debug-command "GO" ()
1386 ;;; (continue *debug-condition*)
1387 ;;; (error "There is no restart named CONTINUE."))
1389 (!def-debug-command "RESTART" ()
1390 (/show0 "doing RESTART debug-command")
1391 (let ((num (read-if-available :prompt)))
1392 (when (eq num :prompt)
1393 (show-restarts *debug-restarts* *debug-io*)
1394 (write-string "restart: ")
1396 (setf num (read *debug-io*)))
1397 (let ((restart (typecase num
1399 (nth num *debug-restarts*))
1401 (find num *debug-restarts* :key #'restart-name
1402 :test (lambda (sym1 sym2)
1403 (string= (symbol-name sym1)
1404 (symbol-name sym2)))))
1406 (format t "~S is invalid as a restart name.~%" num)
1407 (return-from restart-debug-command nil)))))
1408 (/show0 "got RESTART")
1410 (invoke-restart-interactively restart)
1411 ;; FIXME: Even if this isn't handled by WARN, it probably
1412 ;; shouldn't go to *STANDARD-OUTPUT*, but *ERROR-OUTPUT* or
1413 ;; *QUERY-IO* or something. Look through this file to
1414 ;; straighten out stream usage.
1415 (princ "There is no such restart.")))))
1417 ;;;; information commands
1419 (!def-debug-command "HELP" ()
1420 ;; CMU CL had a little toy pager here, but "if you aren't running
1421 ;; ILISP (or a smart windowing system, or something) you deserve to
1422 ;; lose", so we've dropped it in SBCL. However, in case some
1423 ;; desperate holdout is running this on a dumb terminal somewhere,
1424 ;; we tell him where to find the message stored as a string.
1426 "~&~A~2%(The HELP string is stored in ~S.)~%"
1428 '*debug-help-string*))
1430 (!def-debug-command-alias "?" "HELP")
1432 (!def-debug-command "ERROR" ()
1433 (format *debug-io* "~A~%" *debug-condition*)
1434 (show-restarts *debug-restarts* *debug-io*))
1436 (!def-debug-command "BACKTRACE" ()
1437 (backtrace (read-if-available most-positive-fixnum)))
1439 (!def-debug-command "PRINT" ()
1440 (print-frame-call *current-frame*))
1442 (!def-debug-command-alias "P" "PRINT")
1444 (!def-debug-command "LIST-LOCALS" ()
1445 (let ((d-fun (sb!di:frame-debug-fun *current-frame*)))
1446 (if (sb!di:debug-var-info-available d-fun)
1447 (let ((*standard-output* *debug-io*)
1448 (location (sb!di:frame-code-location *current-frame*))
1449 (prefix (read-if-available nil))
1452 (dolist (v (sb!di:ambiguous-debug-vars
1454 (if prefix (string prefix) "")))
1456 (when (eq (sb!di:debug-var-validity v location) :valid)
1457 (setf any-valid-p t)
1458 (format t "~S~:[#~W~;~*~] = ~S~%"
1459 (sb!di:debug-var-symbol v)
1460 (zerop (sb!di:debug-var-id v))
1461 (sb!di:debug-var-id v)
1462 (sb!di:debug-var-value v *current-frame*))))
1466 (format t "There are no local variables ~@[starting with ~A ~]~
1470 (format t "All variables ~@[starting with ~A ~]currently ~
1471 have invalid values."
1473 (write-line "There is no variable information available."))))
1475 (!def-debug-command-alias "L" "LIST-LOCALS")
1477 (!def-debug-command "SOURCE" ()
1479 (print-code-location-source-form (sb!di:frame-code-location *current-frame*)
1480 (read-if-available 0)))
1482 ;;;; source location printing
1484 ;;; We cache a stream to the last valid file debug source so that we
1485 ;;; won't have to repeatedly open the file.
1487 ;;; KLUDGE: This sounds like a bug, not a feature. Opening files is fast
1488 ;;; in the 1990s, so the benefit is negligible, less important than the
1489 ;;; potential of extra confusion if someone changes the source during
1490 ;;; a debug session and the change doesn't show up. And removing this
1491 ;;; would simplify the system, which I like. -- WHN 19990903
1492 (defvar *cached-debug-source* nil)
1493 (declaim (type (or sb!di:debug-source null) *cached-debug-source*))
1494 (defvar *cached-source-stream* nil)
1495 (declaim (type (or stream null) *cached-source-stream*))
1497 ;;; To suppress the read-time evaluation #. macro during source read,
1498 ;;; *READTABLE* is modified. *READTABLE* is cached to avoid
1499 ;;; copying it each time, and invalidated when the
1500 ;;; *CACHED-DEBUG-SOURCE* has changed.
1501 (defvar *cached-readtable* nil)
1502 (declaim (type (or readtable null) *cached-readtable*))
1505 (setq *cached-debug-source* nil *cached-source-stream* nil
1506 *cached-readtable* nil))
1507 *before-save-initializations*)
1509 ;;; We also cache the last toplevel form that we printed a source for
1510 ;;; so that we don't have to do repeated reads and calls to
1511 ;;; FORM-NUMBER-TRANSLATIONS.
1512 (defvar *cached-toplevel-form-offset* nil)
1513 (declaim (type (or index null) *cached-toplevel-form-offset*))
1514 (defvar *cached-toplevel-form*)
1515 (defvar *cached-form-number-translations*)
1517 ;;; Given a code location, return the associated form-number
1518 ;;; translations and the actual top level form. We check our cache ---
1519 ;;; if there is a miss, we dispatch on the kind of the debug source.
1520 (defun get-toplevel-form (location)
1521 (let ((d-source (sb!di:code-location-debug-source location)))
1522 (if (and (eq d-source *cached-debug-source*)
1523 (eql (sb!di:code-location-toplevel-form-offset location)
1524 *cached-toplevel-form-offset*))
1525 (values *cached-form-number-translations* *cached-toplevel-form*)
1526 (let* ((offset (sb!di:code-location-toplevel-form-offset location))
1528 (ecase (sb!di:debug-source-from d-source)
1529 (:file (get-file-toplevel-form location))
1530 (:lisp (svref (sb!di:debug-source-name d-source) offset)))))
1531 (setq *cached-toplevel-form-offset* offset)
1532 (values (setq *cached-form-number-translations*
1533 (sb!di:form-number-translations res offset))
1534 (setq *cached-toplevel-form* res))))))
1536 ;;; Locate the source file (if it still exists) and grab the top level
1537 ;;; form. If the file is modified, we use the top level form offset
1538 ;;; instead of the recorded character offset.
1539 (defun get-file-toplevel-form (location)
1540 (let* ((d-source (sb!di:code-location-debug-source location))
1541 (tlf-offset (sb!di:code-location-toplevel-form-offset location))
1542 (local-tlf-offset (- tlf-offset
1543 (sb!di:debug-source-root-number d-source)))
1545 (aref (or (sb!di:debug-source-start-positions d-source)
1546 (error "no start positions map"))
1548 (name (sb!di:debug-source-name d-source)))
1549 (unless (eq d-source *cached-debug-source*)
1550 (unless (and *cached-source-stream*
1551 (equal (pathname *cached-source-stream*)
1553 (setq *cached-readtable* nil)
1554 (when *cached-source-stream* (close *cached-source-stream*))
1555 (setq *cached-source-stream* (open name :if-does-not-exist nil))
1556 (unless *cached-source-stream*
1557 (error "The source file no longer exists:~% ~A" (namestring name)))
1558 (format t "~%; file: ~A~%" (namestring name)))
1560 (setq *cached-debug-source*
1561 (if (= (sb!di:debug-source-created d-source)
1562 (file-write-date name))
1566 ((eq *cached-debug-source* d-source)
1567 (file-position *cached-source-stream* char-offset))
1569 (format t "~%; File has been modified since compilation:~%; ~A~@
1570 ; Using form offset instead of character position.~%"
1572 (file-position *cached-source-stream* 0)
1573 (let ((*read-suppress* t))
1574 (dotimes (i local-tlf-offset)
1575 (read *cached-source-stream*)))))
1576 (unless *cached-readtable*
1577 (setq *cached-readtable* (copy-readtable))
1578 (set-dispatch-macro-character
1580 (lambda (stream sub-char &rest rest)
1581 (declare (ignore rest sub-char))
1582 (let ((token (read stream t nil t)))
1583 (format nil "#.~S" token)))
1584 *cached-readtable*))
1585 (let ((*readtable* *cached-readtable*))
1586 (read *cached-source-stream*))))
1588 (defun print-code-location-source-form (location context)
1589 (let* ((location (maybe-block-start-location location))
1590 (form-num (sb!di:code-location-form-number location)))
1591 (multiple-value-bind (translations form) (get-toplevel-form location)
1592 (unless (< form-num (length translations))
1593 (error "The source path no longer exists."))
1594 (prin1 (sb!di:source-path-context form
1595 (svref translations form-num)
1598 ;;; breakpoint and step commands
1600 ;;; Step to the next code-location.
1601 (!def-debug-command "STEP" ()
1602 (setf *number-of-steps* (read-if-available 1))
1603 (set-step-breakpoint *current-frame*)
1604 (continue *debug-condition*)
1605 (error "couldn't continue"))
1607 ;;; List possible breakpoint locations, which ones are active, and
1608 ;;; where the CONTINUE restart will transfer control. Set
1609 ;;; *POSSIBLE-BREAKPOINTS* to the code-locations which can then be
1610 ;;; used by sbreakpoint.
1611 (!def-debug-command "LIST-LOCATIONS" ()
1612 (let ((df (read-if-available *default-breakpoint-debug-fun*)))
1614 (setf df (sb!di:fun-debug-fun (eval df)))
1615 (setf *default-breakpoint-debug-fun* df))
1617 (not *default-breakpoint-debug-fun*))
1618 (setf df (sb!di:frame-debug-fun *current-frame*))
1619 (setf *default-breakpoint-debug-fun* df)))
1620 (setf *possible-breakpoints* (possible-breakpoints df)))
1621 (let ((continue-at (sb!di:frame-code-location *current-frame*)))
1622 (let ((active (location-in-list *default-breakpoint-debug-fun*
1623 *breakpoints* :fun-start))
1624 (here (sb!di:code-location=
1625 (sb!di:debug-fun-start-location
1626 *default-breakpoint-debug-fun*) continue-at)))
1627 (when (or active here)
1628 (format t "::FUN-START ")
1629 (when active (format t " *Active*"))
1630 (when here (format t " *Continue here*"))))
1632 (let ((prev-location nil)
1637 (let ((this-num (1- this-num)))
1638 (if (= prev-num this-num)
1639 (format t "~&~W: " prev-num)
1640 (format t "~&~W-~W: " prev-num this-num)))
1641 (print-code-location-source-form prev-location 0)
1642 (when *print-location-kind*
1643 (format t "~S " (sb!di:code-location-kind prev-location)))
1644 (when (location-in-list prev-location *breakpoints*)
1645 (format t " *Active*"))
1646 (when (sb!di:code-location= prev-location continue-at)
1647 (format t " *Continue here*")))))
1649 (dolist (code-location *possible-breakpoints*)
1650 (when (or *print-location-kind*
1651 (location-in-list code-location *breakpoints*)
1652 (sb!di:code-location= code-location continue-at)
1654 (not (eq (sb!di:code-location-debug-source code-location)
1655 (sb!di:code-location-debug-source prev-location)))
1656 (not (eq (sb!di:code-location-toplevel-form-offset
1658 (sb!di:code-location-toplevel-form-offset
1660 (not (eq (sb!di:code-location-form-number code-location)
1661 (sb!di:code-location-form-number prev-location))))
1663 (setq prev-location code-location prev-num this-num))
1667 (when (location-in-list *default-breakpoint-debug-fun*
1670 (format t "~&::FUN-END *Active* "))))
1672 (!def-debug-command-alias "LL" "LIST-LOCATIONS")
1674 ;;; Set breakpoint at the given number.
1675 (!def-debug-command "BREAKPOINT" ()
1676 (let ((index (read-prompting-maybe "location number, :START, or :END: "))
1680 (print-functions nil)
1683 (place *default-breakpoint-debug-fun*))
1684 (flet ((get-command-line ()
1685 (let ((command-line nil)
1688 (let ((next-input (read-if-available unique)))
1689 (when (eq next-input unique) (return))
1690 (push next-input command-line)))
1691 (nreverse command-line)))
1692 (set-vars-from-command-line (command-line)
1693 (do ((arg (pop command-line) (pop command-line)))
1696 (:condition (setf condition (pop command-line)))
1697 (:print (push (pop command-line) print))
1698 (:break (setf break (pop command-line)))
1700 (setf function (eval (pop command-line)))
1701 (setf *default-breakpoint-debug-fun*
1702 (sb!di:fun-debug-fun function))
1703 (setf place *default-breakpoint-debug-fun*)
1704 (setf *possible-breakpoints*
1705 (possible-breakpoints
1706 *default-breakpoint-debug-fun*))))))
1708 (let ((code-loc (sb!di:debug-fun-start-location place)))
1709 (setf bp (sb!di:make-breakpoint #'main-hook-fun
1712 (setf break (sb!di:preprocess-for-eval break code-loc))
1713 (setf condition (sb!di:preprocess-for-eval condition code-loc))
1714 (dolist (form print)
1715 (push (cons (sb!di:preprocess-for-eval form code-loc) form)
1719 (sb!di:make-breakpoint #'main-hook-fun
1723 ;; FIXME: These and any other old (COERCE `(LAMBDA ..) ..)
1724 ;; forms should be converted to shiny new (LAMBDA ..) forms.
1725 ;; (Search the sources for "coerce.*\(lambda".)
1726 (coerce `(lambda (dummy)
1727 (declare (ignore dummy)) ,break)
1729 (setf condition (coerce `(lambda (dummy)
1730 (declare (ignore dummy)) ,condition)
1732 (dolist (form print)
1734 (coerce `(lambda (dummy)
1735 (declare (ignore dummy)) ,form) 'function)
1738 (setup-code-location ()
1739 (setf place (nth index *possible-breakpoints*))
1740 (setf bp (sb!di:make-breakpoint #'main-hook-fun place
1741 :kind :code-location))
1742 (dolist (form print)
1744 (sb!di:preprocess-for-eval form place)
1747 (setf break (sb!di:preprocess-for-eval break place))
1748 (setf condition (sb!di:preprocess-for-eval condition place))))
1749 (set-vars-from-command-line (get-command-line))
1751 ((or (eq index :start) (eq index :s))
1753 ((or (eq index :end) (eq index :e))
1756 (setup-code-location)))
1757 (sb!di:activate-breakpoint bp)
1758 (let* ((new-bp-info (create-breakpoint-info place bp index
1760 :print print-functions
1761 :condition condition))
1762 (old-bp-info (location-in-list new-bp-info *breakpoints*)))
1764 (sb!di:deactivate-breakpoint (breakpoint-info-breakpoint
1766 (setf *breakpoints* (remove old-bp-info *breakpoints*))
1767 (format t "previous breakpoint removed~%"))
1768 (push new-bp-info *breakpoints*))
1769 (print-breakpoint-info (first *breakpoints*))
1770 (format t "~&added"))))
1772 (!def-debug-command-alias "BP" "BREAKPOINT")
1774 ;;; List all breakpoints which are set.
1775 (!def-debug-command "LIST-BREAKPOINTS" ()
1777 (sort *breakpoints* #'< :key #'breakpoint-info-breakpoint-number))
1778 (dolist (info *breakpoints*)
1779 (print-breakpoint-info info)))
1781 (!def-debug-command-alias "LB" "LIST-BREAKPOINTS")
1782 (!def-debug-command-alias "LBP" "LIST-BREAKPOINTS")
1784 ;;; Remove breakpoint N, or remove all breakpoints if no N given.
1785 (!def-debug-command "DELETE-BREAKPOINT" ()
1786 (let* ((index (read-if-available nil))
1788 (find index *breakpoints* :key #'breakpoint-info-breakpoint-number)))
1790 (sb!di:delete-breakpoint (breakpoint-info-breakpoint bp-info))
1791 (setf *breakpoints* (remove bp-info *breakpoints*))
1792 (format t "breakpoint ~S removed~%" index))
1793 (index (format t "The breakpoint doesn't exist."))
1795 (dolist (ele *breakpoints*)
1796 (sb!di:delete-breakpoint (breakpoint-info-breakpoint ele)))
1797 (setf *breakpoints* nil)
1798 (format t "all breakpoints deleted~%")))))
1800 (!def-debug-command-alias "DBP" "DELETE-BREAKPOINT")
1802 ;;; miscellaneous commands
1804 (!def-debug-command "DESCRIBE" ()
1805 (let* ((curloc (sb!di:frame-code-location *current-frame*))
1806 (debug-fun (sb!di:code-location-debug-fun curloc))
1807 (function (sb!di:debug-fun-fun debug-fun)))
1810 (format t "can't figure out the function for this frame"))))
1812 (!def-debug-command "SLURP" ()
1813 (loop while (read-char-no-hang *standard-input*)))
1815 (!def-debug-command "RETURN" (&optional
1816 (return (read-prompting-maybe
1818 (let ((tag (find-if (lambda (x)
1819 (and (typep (car x) 'symbol)
1820 (not (symbol-package (car x)))
1821 (string= (car x) "SB-DEBUG-CATCH-TAG")))
1822 (sb!di::frame-catches *current-frame*))))
1825 (funcall (sb!di:preprocess-for-eval
1827 (sb!di:frame-code-location *current-frame*))
1829 (format t "~@<can't find a tag for this frame ~
1830 ~2I~_(hint: try increasing the DEBUG optimization quality ~
1831 and recompiling)~:@>"))))
1833 ;;;; debug loop command utilities
1835 (defun read-prompting-maybe (prompt)
1836 (unless (sb!int:listen-skip-whitespace *debug-io*)
1841 (defun read-if-available (default)
1842 (if (sb!int:listen-skip-whitespace *debug-io*)