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
34 (defvar *debug-print-variable-alist
* nil
36 "an association list describing new bindings for special variables
37 to be used within the debugger. Eg.
39 ((*PRINT-LENGTH* . 10) (*PRINT-LEVEL* . 6) (*PRINT-PRETTY* . NIL))
41 The variables in the CAR positions are bound to the values in the CDR
42 during the execution of some debug commands. When evaluating arbitrary
43 expressions in the debugger, the normal values of the printer control
44 variables are in effect.
46 Initially empty, *DEBUG-PRINT-VARIABLE-ALIST* is typically used to
47 provide bindings for printer control variables.")
49 (defvar *debug-readtable
*
50 ;; KLUDGE: This can't be initialized in a cold toplevel form,
51 ;; because the *STANDARD-READTABLE* isn't initialized until after
52 ;; cold toplevel forms have run. So instead we initialize it
53 ;; immediately after *STANDARD-READTABLE*. -- WHN 20000205
56 "*READTABLE* for the debugger")
58 (defvar *in-the-debugger
* nil
60 "This is T while in the debugger.")
62 ;;; nestedness inside debugger command loops
63 (defvar *debug-command-level
* 0)
65 ;;; If this is bound before the debugger is invoked, it is used as the stack
66 ;;; top by the debugger. It can either be the first interesting frame, or the
67 ;;; name of the last uninteresting frame.
68 (defvar *stack-top-hint
* nil
)
70 (defvar *real-stack-top
* nil
)
71 (defvar *stack-top
* nil
)
73 (defvar *current-frame
* nil
)
75 ;;; Beginner-oriented help messages are important because you end up
76 ;;; in the debugger whenever something bad happens, or if you try to
77 ;;; get out of the system with Ctrl-C or (EXIT) or EXIT or whatever.
78 ;;; But after memorizing them the wasted screen space gets annoying..
79 (defvar *debug-beginner-help-p
* t
81 "Should the debugger display beginner-oriented help messages?")
83 (defun debug-prompt (stream)
84 (sb!thread
::get-foreground
)
87 (sb!di
:frame-number
*current-frame
*)
88 (> *debug-command-level
* 1)
89 *debug-command-level
*))
91 (defparameter *debug-help-string
*
92 "The debug prompt is square brackets, with number(s) indicating the current
93 control stack level and, if you've entered the debugger recursively, how
94 deeply recursed you are.
95 Any command -- including the name of a restart -- may be uniquely abbreviated.
96 The debugger rebinds various special variables for controlling i/o, sometimes
97 to defaults (much like WITH-STANDARD-IO-SYNTAX does) and sometimes to
98 its own special values, based on SB-EXT:*DEBUG-PRINT-VARIABLE-ALIST*.
99 Debug commands do not affect *, //, and similar variables, but evaluation in
100 the debug loop does affect these variables.
101 SB-DEBUG:*FLUSH-DEBUG-ERRORS* controls whether errors at the debug prompt
102 drop you deeper into the debugger. The default NIL allows recursive entry
105 Getting in and out of the debugger:
106 TOPLEVEL, TOP exits debugger and returns to top level REPL
107 RESTART invokes restart numbered as shown (prompt if not given).
108 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 UP up frame DOWN down frame
116 BOTTOM bottom frame FRAME 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 frame.
121 PRINT, P displays function call for current frame.
122 SOURCE [n] displays frame's source form with n levels of enclosing forms.
125 START Selects the CONTINUE restart if one exists and starts
126 single-stepping. Single stepping affects only code compiled with
127 under high DEBUG optimization quality. See User Manual for details.
128 STEP Steps into the current form.
129 NEXT Steps over the current form.
130 OUT Stops stepping temporarily, but resumes it when the topmost frame that
131 was stepped into returns.
132 STOP Stops single-stepping.
134 Function and macro commands:
136 Return the n'th argument in the current frame.
137 (SB-DEBUG:VAR string-or-symbol [id])
138 Returns the value of the specified variable in the current frame.
142 Return the values resulting from evaluation of expr from the
143 current frame, if this frame was compiled with a sufficiently high
144 DEBUG optimization quality.
147 Restart execution of the current frame, if this frame is for a
148 global function which was compiled with a sufficiently high
149 DEBUG optimization quality.
152 Discard all pending input on *STANDARD-INPUT*. (This can be
153 useful when the debugger was invoked to handle an error in
154 deeply nested input syntax, and now the reader is confused.)")
156 (defmacro with-debug-io-syntax
(() &body body
)
157 (let ((thunk (sb!xc
:gensym
"THUNK")))
158 `(dx-flet ((,thunk
()
160 (funcall-with-debug-io-syntax #',thunk
))))
162 ;;; If LOC is an unknown location, then try to find the block start
163 ;;; location. Used by source printing to some information instead of
164 ;;; none for the user.
165 (defun maybe-block-start-location (loc)
166 (if (sb!di
:code-location-unknown-p loc
)
167 (let* ((block (sb!di
:code-location-debug-block loc
))
168 (start (sb!di
:do-debug-block-locations
(loc block
)
170 (cond ((and (not (sb!di
:debug-block-elsewhere-p block
))
172 (format *debug-io
* "~%unknown location: using block start~%")
180 (declaim (unsigned-byte *backtrace-frame-count
*))
181 (defvar *backtrace-frame-count
* 1000
183 "Default number of frames to backtrace. Defaults to 1000.")
185 (declaim (type (member :minimal
:normal
:full
) *method-frame-style
*))
186 (defvar *method-frame-style
* :normal
188 "Determines how frames corresponding to method functions are represented in
189 backtraces. Possible values are :MINIMAL, :NORMAL, and :FULL.
191 :MINIMAL represents them as
193 (<gf-name> ...args...)
195 if all arguments are available, and only a single method is applicable to
196 the arguments -- otherwise behaves as :NORMAL.
198 :NORMAL represents them as
200 ((:method <gf-name> [<qualifier>*] (<specializer>*)) ...args...)
202 The frame is then followed by either [fast-method] or [slow-method],
203 designating the kind of method function. (See below.)
205 :FULL represents them using the actual funcallable method function name:
207 ((sb-pcl:fast-method <gf-name> [<qualifier>*] (<specializer>*)) ...args...)
211 ((sb-pcl:slow-method <gf-name> [<qualifier>*] (<specializer>*)) ...args...)
213 In the this case arguments may include values internal to SBCL's method
214 dispatch machinery.")
216 (define-deprecated-variable :early
"1.1.4.9" *show-entry-point-details
*
219 (define-deprecated-function :early
"1.2.15" backtrace
(print-backtrace)
220 (&optional
(count *backtrace-frame-count
*) (stream *debug-io
*))
221 (print-backtrace :count count
:stream stream
))
223 (define-deprecated-function :early
"1.2.15" backtrace-as-list
(list-backtrace)
224 (&optional
(count *backtrace-frame-count
*))
225 (list-backtrace :count count
))
227 (defun backtrace-start-frame (frame-designator)
228 (let ((here (sb!di
:top-frame
)))
229 (labels ((current-frame ()
231 ;; Our caller's caller.
233 do
(setf frame
(or (sb!di
:frame-down frame
) frame
)))
235 (interrupted-frame ()
236 (or (nth-value 1 (find-interrupted-name-and-frame))
238 (cond ((eq :current-frame frame-designator
)
240 ((eq :interrupted-frame frame-designator
)
242 ((eq :debugger-frame frame-designator
)
243 (if (and *in-the-debugger
* *current-frame
*)
245 (interrupted-frame)))
246 ((sb!di
:frame-p frame-designator
)
249 (error "Invalid designator for initial backtrace frame: ~S"
250 frame-designator
))))))
252 (defun map-backtrace (function &key
254 (from :debugger-frame
)
255 (count *backtrace-frame-count
*))
257 "Calls the designated FUNCTION with each frame on the call stack.
258 Returns the last value returned by FUNCTION.
260 COUNT is the number of frames to backtrace, defaulting to
261 *BACKTRACE-FRAME-COUNT*.
263 START is the number of the frame the backtrace should start from.
265 FROM specifies the frame relative to which the frames are numbered. Possible
266 values are an explicit SB-DI:FRAME object, and the
267 keywords :CURRENT-FRAME, :INTERRUPTED-FRAME, and :DEBUGGER-FRAME. Default
271 specifies the caller of MAP-BACKTRACE.
274 specifies the first interrupted frame on the stack \(typically the frame
275 where the error occurred, as opposed to error handling frames) if any,
276 otherwise behaving as :CURRENT-FRAME.
279 specifies the currently debugged frame when inside the debugger, and
280 behaves as :INTERRUPTED-FRAME outside the debugger.
282 (loop with result
= nil
284 for frame
= (backtrace-start-frame from
)
285 then
(sb!di
:frame-down frame
)
287 when
(<= start index
) do
288 (if (minusp (decf count
))
290 (setf result
(funcall function frame
)))
291 finally
(return result
)))
293 (defun print-backtrace (&key
296 (from :debugger-frame
)
297 (count *backtrace-frame-count
*)
299 (print-frame-source nil
)
300 (method-frame-style *method-frame-style
*))
302 "Print a listing of the call stack to STREAM, defaulting to *DEBUG-IO*.
304 COUNT is the number of frames to backtrace, defaulting to
305 *BACKTRACE-FRAME-COUNT*.
307 START is the number of the frame the backtrace should start from.
309 FROM specifies the frame relative to which the frames are numbered. Possible
310 values are an explicit SB-DI:FRAME object, and the
311 keywords :CURRENT-FRAME, :INTERRUPTED-FRAME, and :DEBUGGER-FRAME. Default
315 specifies the caller of PRINT-BACKTRACE.
318 specifies the first interrupted frame on the stack \(typically the frame
319 where the error occured, as opposed to error handling frames) if any,
320 otherwise behaving as :CURRENT-FRAME.
323 specifies the currently debugged frame when inside the debugger, and
324 behaves as :INTERRUPTED-FRAME outside the debugger.
326 If PRINT-THREAD is true (default), backtrace is preceded by printing the
327 thread object the backtrace is from.
329 If PRINT-FRAME-SOURCE is true (default is false), each frame is followed by
330 printing the currently executing source form in the function responsible for
331 that frame, when available. Requires the function to have been compiled at
332 DEBUG 2 or higher. If PRINT-FRAME-SOURCE is :ALWAYS, it also reports \"no
333 source available\" for frames for which were compiled at lower debug settings.
335 METHOD-FRAME-STYLE (defaulting to *METHOD-FRAME-STYLE*), determines how frames
336 corresponding to method functions are printed. Possible values
337 are :MINIMAL, :NORMAL, and :FULL. See *METHOD-FRAME-STYLE* for more
339 (with-debug-io-syntax ()
342 (format stream
"Backtrace for: ~S~%" sb
!thread
:*current-thread
*))
343 (let ((*suppress-print-errors
* (if (subtypep 'serious-condition
*suppress-print-errors
*)
344 *suppress-print-errors
*
348 (handler-bind ((print-not-readable #'print-unreadably
))
349 (map-backtrace (lambda (frame)
350 (print-frame-call frame stream
352 :method-frame-style method-frame-style
353 :print-frame-source print-frame-source
)
355 :from
(backtrace-start-frame from
)
361 (defun list-backtrace (&key
362 (count *backtrace-frame-count
*)
364 (from :debugger-frame
)
365 (method-frame-style *method-frame-style
*))
367 "Returns a list describing the call stack. Each frame is represented
372 where the name describes the function responsible for the frame. The name
373 might not be bound to the actual function object. Unavailable arguments are
374 represented by dummy objects that print as #<unavailable argument>. Objects
375 with dynamic-extent allocation by the current thread are represented by
376 substitutes to avoid references to them from leaking outside their legal
379 COUNT is the number of frames to backtrace, defaulting to
380 *BACKTRACE-FRAME-COUNT*.
382 START is the number of the frame the backtrace should start from.
384 FROM specifies the frame relative to which the frames are numbered. Possible
385 values are an explicit SB-DI:FRAME object, and the
386 keywords :CURRENT-FRAME, :INTERRUPTED-FRAME, and :DEBUGGER-FRAME. Default
390 specifies the caller of LIST-BACKTRACE.
393 specifies the first interrupted frame on the stack \(typically the frame
394 where the error occured, as opposed to error handling frames) if any,
395 otherwise behaving as :CURRENT-FRAME.
398 specifies the currently debugged frame when inside the debugger, and
399 behaves as :INTERRUPTED-FRAME outside the debugger.
401 METHOD-FRAME-STYLE (defaulting to *METHOD-FRAME-STYLE*), determines how frames
402 corresponding to method functions are printed. Possible values
403 are :MINIMAL, :NORMAL, and :FULL. See *METHOD-FRAME-STYLE* for more
408 (push (frame-call-as-list frame
:method-frame-style method-frame-style
)
412 :from
(backtrace-start-frame from
))
413 (nreverse rbacktrace
)))
415 (defun frame-call-as-list (frame &key
(method-frame-style *method-frame-style
*))
416 (multiple-value-bind (name args info
)
417 (frame-call frame
:method-frame-style method-frame-style
418 :replace-dynamic-extent-objects t
)
419 (values (cons name args
) info
)))
421 (defun replace-dynamic-extent-object (obj)
422 (if (stack-allocated-p obj
)
423 (make-unprintable-object
425 (format nil
"dynamic-extent: ~S" obj
)
427 "error printing dynamic-extent object")))
430 (defun stack-allocated-p (obj)
432 "Returns T if OBJ is allocated on the stack of the current
433 thread, NIL otherwise."
434 (with-pinned-objects (obj)
435 (let ((sap (int-sap (get-lisp-obj-address obj
))))
436 (when (sb!vm
:control-stack-pointer-valid-p sap nil
)
441 (eval-when (:compile-toplevel
:execute
)
443 ;;; This is a convenient way to express what to do for each type of
444 ;;; lambda-list element.
445 (sb!xc
:defmacro lambda-list-element-dispatch
(element
457 (ecase (car ,element
)
458 (:optional
,@optional
)
463 (aver (eq ,element
:deleted
))
466 (sb!xc
:defmacro lambda-var-dispatch
(variable location deleted valid other
)
467 (let ((var (gensym)))
468 `(let ((,var
,variable
))
469 (cond ((eq ,var
:deleted
) ,deleted
)
470 ((eq (sb!di
:debug-var-validity
,var
,location
) :valid
)
476 ;;; Extract the function argument values for a debug frame.
477 (defun map-frame-args (thunk frame
)
478 (let ((debug-fun (sb!di
:frame-debug-fun frame
)))
479 (dolist (element (sb!di
:debug-fun-lambda-list debug-fun
))
480 (funcall thunk element
))))
482 ;;; Since arg-count checking happens before any of the stack locations
483 ;;; and registers are overwritten all the arguments, including the
484 ;;; extra ones, can be precisely recovered.
485 #!+precise-arg-count-error
486 (defun arg-count-error-frame-nth-arg (n frame
)
487 (let* ((escaped (sb!di
::compiled-frame-escaped frame
))
488 (pointer (sb!di
::frame-pointer frame
))
489 (arg-count (sb!di
::sub-access-debug-var-slot
490 pointer sb
!c
:arg-count-sc escaped
)))
493 (sb!di
::sub-access-debug-var-slot
495 (sb!c
:standard-arg-location-sc n
)
497 (error "Index ~a out of bounds for ~a supplied argument~:p." n arg-count
))))
499 #!+precise-arg-count-error
500 (defun arg-count-error-frame-args (frame)
501 (let* ((escaped (sb!di
::compiled-frame-escaped frame
))
502 (pointer (sb!di
::frame-pointer frame
))
503 (arg-count (sb!di
::sub-access-debug-var-slot
504 pointer sb
!c
:arg-count-sc escaped
)))
505 (loop for i below arg-count
506 collect
(sb!di
::sub-access-debug-var-slot
508 (sb!c
:standard-arg-location-sc i
)
511 (defun frame-args-as-list (frame)
512 #!+precise-arg-count-error
513 (when (sb!di
::tl-invalid-arg-count-error-p frame
)
514 (return-from frame-args-as-list
515 (arg-count-error-frame-args frame
)))
517 (let ((location (sb!di
:frame-code-location frame
))
518 (reversed-result nil
))
522 (lambda-list-element-dispatch element
523 :required
((push (frame-call-arg element location frame
) reversed-result
))
524 :optional
((push (frame-call-arg (second element
) location frame
)
526 :keyword
((push (second element
) reversed-result
)
527 (push (frame-call-arg (third element
) location frame
)
529 :deleted
((push (frame-call-arg element location frame
) reversed-result
))
530 :rest
((lambda-var-dispatch (second element
) location
532 (let ((rest (sb!di
:debug-var-value
(second element
) frame
)))
534 (setf reversed-result
(append (reverse rest
) reversed-result
))
535 (push (make-unprintable-object "unavailable &REST argument")
537 (return-from enumerating
))
538 (push (make-unprintable-object
539 "unavailable &REST argument")
541 :more
((lambda-var-dispatch (second element
) location
543 (let ((context (sb!di
:debug-var-value
(second element
) frame
))
544 (count (sb!di
:debug-var-value
(third element
) frame
)))
545 (setf reversed-result
548 (sb!c
::%more-arg-values context
0 count
)))
550 (return-from enumerating
))
551 (push (make-unprintable-object "unavailable &MORE argument")
554 (nreverse reversed-result
))
555 (sb!di
:lambda-list-unavailable
()
556 (make-unprintable-object "unavailable lambda list"))))
558 (defun clean-xep (frame name args info
)
559 (values (second name
)
560 #!-precise-arg-count-error
562 (let* ((count (first args
))
563 (real-args (rest args
)))
564 (if (and (integerp count
)
565 (sb!di
::tl-invalid-arg-count-error-p frame
))
566 ;; So, this is a cheap trick -- but makes backtraces for
567 ;; too-many-arguments-errors much, much easier to to
570 for arg
= (if real-args
572 (make-unprintable-object "unknown"))
577 #!+precise-arg-count-error
578 (if (and (consp args
)
579 ;; ARG-COUNT-ERROR-FRAME-ARGS doesn't include arg-count
580 (not (sb!di
::tl-invalid-arg-count-error-p frame
)))
583 (if (eq (car name
) 'sb
!c
::tl-xep
)
587 (defun clean-&more-processor
(name args info
)
588 (values (second name
)
590 (let* ((more (last args
2))
591 (context (first more
))
592 (count (second more
)))
597 (sb!c
:%more-arg-values context
0 count
))
599 (make-unprintable-object "more unavailable arguments")))))
603 (defun clean-fast-method (name args style info
)
604 (declare (type (member :minimal
:normal
:full
) style
))
605 (multiple-value-bind (cname cargs
)
606 ;; Make no attempt to simplify the display if ARGS could not be found
607 ;; due to low (OPTIMIZE (DEBUG)) quality in the method.
608 (if (or (eq style
:full
) (not (listp args
)))
610 (let ((gf-name (second name
))
611 (real-args (the list
(cddr args
)))) ; strip .PV. and .N-M-CALL.
612 (if (and (eq style
:minimal
)
614 (notany #'sb
!impl
::unprintable-object-p real-args
)
615 (singleton-p (compute-applicable-methods
616 (fdefinition gf-name
) real-args
)))
617 (values gf-name real-args
)
618 (values (cons :method
(cdr name
)) real-args
))))
619 (values cname cargs
(cons :fast-method info
))))
621 (defun clean-frame-call (frame name method-frame-style info
)
622 (let ((args (frame-args-as-list frame
)))
625 ((sb!c
::xep sb
!c
::tl-xep
)
626 (clean-xep frame name args info
))
627 ((sb!c
::&more-processor
)
628 (clean-&more-processor name args info
))
629 ((sb!c
::&optional-processor
)
630 (clean-frame-call frame
(second name
) method-frame-style
632 ((sb!pcl
::fast-method
)
633 (clean-fast-method name args method-frame-style info
))
635 (values name args info
)))
636 (values name args info
))))
638 (defun frame-call (frame &key
(method-frame-style *method-frame-style
*)
639 replace-dynamic-extent-objects
)
641 "Returns as multiple values a descriptive name for the function responsible
642 for FRAME, arguments that that function, and a list providing additional
643 information about the frame.
645 Unavailable arguments are represented using dummy-objects printing as
646 #<unavailable argument>.
648 METHOD-FRAME-STYLE (defaulting to *METHOD-FRAME-STYLE*), determines how frames
649 corresponding to method functions are printed. Possible values
650 are :MINIMAL, :NORMAL, and :FULL. See *METHOD-FRAME-STYLE* for more
653 If REPLACE-DYNAMIC-EXTENT-OBJECTS is true, objects allocated on the stack of
654 the current thread are replaced with dummy objects which can safely escape."
655 (let* ((debug-fun (sb!di
:frame-debug-fun frame
))
656 (kind (sb!di
:debug-fun-kind debug-fun
)))
657 (multiple-value-bind (name args info
)
658 (clean-frame-call frame
659 (or (sb!di
:debug-fun-closure-name debug-fun frame
)
660 (sb!di
:debug-fun-name debug-fun
))
662 (when kind
(list kind
)))
663 (let ((args (if (and (consp args
) replace-dynamic-extent-objects
)
664 (mapcar #'replace-dynamic-extent-object args
)
666 (values name args info
)))))
668 (defun ensure-printable-object (object)
670 (with-open-stream (out (make-broadcast-stream))
674 (declare (ignore cond
))
675 (make-unprintable-object "error printing object"))))
677 (defun frame-call-arg (var location frame
)
678 (lambda-var-dispatch var location
679 (make-unprintable-object "unused argument")
680 (sb!di
:debug-var-value var frame
)
681 (make-unprintable-object "unavailable argument")))
683 ;;; Prints a representation of the function call causing FRAME to
684 ;;; exist. VERBOSITY indicates the level of information to output;
685 ;;; zero indicates just printing the DEBUG-FUN's name, and one
686 ;;; indicates displaying call-like, one-liner format with argument
688 (defun print-frame-call (frame stream
689 &key print-frame-source
691 (method-frame-style *method-frame-style
*))
693 (format stream
"~&~S: " (if (integerp number
)
695 (sb!di
:frame-number frame
))))
696 (multiple-value-bind (name args info
)
697 (frame-call frame
:method-frame-style method-frame-style
)
698 (pprint-logical-block (stream nil
:prefix
"(" :suffix
")")
699 (let ((*print-pretty
* nil
)
701 ;; Since we go to some trouble to make nice informative
702 ;; function names like (PRINT-OBJECT :AROUND (CLOWN T)), let's
703 ;; make sure that they aren't truncated by *PRINT-LENGTH* and
705 (let ((*print-length
* nil
)
707 (name (ensure-printable-object name
)))
708 (write name
:stream stream
:escape t
:pretty
(equal '(lambda ()) name
)))
710 ;; For the function arguments, we can just print normally. If
711 ;; we hit a &REST arg, then print as many of the values as
712 ;; possible, punting the loop over lambda-list variables since
713 ;; any other arguments will be in the &REST arg's list of
715 (let ((args (ensure-printable-object args
)))
717 (format stream
"~{ ~_~S~}" args
)
718 (format stream
" ~S" args
)))))
720 (format stream
" [~{~(~A~)~^,~}]" info
)))
721 (when print-frame-source
722 (let ((loc (sb!di
:frame-code-location frame
)))
724 (let ((source (handler-case
725 (code-location-source-form loc
0)
727 (format stream
"~& error finding frame source: ~A" c
)))))
728 (format stream
"~% source: ~S" source
))
729 (sb!di
:debug-condition
()
730 ;; This is mostly noise.
731 (when (eq :always print-frame-source
)
732 (format stream
"~& no source available for frame")))
734 (format stream
"~& error printing frame source: ~A" c
))))))
738 (defvar *debugger-hook
* nil
740 "This is either NIL or a function of two arguments, a condition and the value
741 of *DEBUGGER-HOOK*. This function can either handle the condition or return
742 which causes the standard debugger to execute. The system passes the value
743 of this variable to the function because it binds *DEBUGGER-HOOK* to NIL
744 around the invocation.")
746 (defvar *invoke-debugger-hook
* nil
748 "This is either NIL or a designator for a function of two arguments,
749 to be run when the debugger is about to be entered. The function is
750 run with *INVOKE-DEBUGGER-HOOK* bound to NIL to minimize recursive
751 errors, and receives as arguments the condition that triggered
752 debugger entry and the previous value of *INVOKE-DEBUGGER-HOOK*
754 This mechanism is an SBCL extension similar to the standard *DEBUGGER-HOOK*.
755 In contrast to *DEBUGGER-HOOK*, it is observed by INVOKE-DEBUGGER even when
758 ;;; These are bound on each invocation of INVOKE-DEBUGGER.
759 (defvar *debug-restarts
*)
760 (defvar *debug-condition
*)
761 (defvar *nested-debug-condition
*)
763 ;;; Oh, what a tangled web we weave when we preserve backwards
764 ;;; compatibility with 1968-style use of global variables to control
765 ;;; per-stream i/o properties; there's really no way to get this
766 ;;; quite right, but we do what we can.
767 (defun funcall-with-debug-io-syntax (fun &rest rest
)
768 (declare (type function fun
))
769 ;; Try to force the other special variables into a useful state.
770 (let (;; Protect from WITH-STANDARD-IO-SYNTAX some variables where
771 ;; any default we might use is less useful than just reusing
772 ;; the global values.
773 (original-package *package
*)
774 (original-print-pretty *print-pretty
*))
775 (with-standard-io-syntax
777 (let (;; We want the printer and reader to be in a useful
778 ;; state, regardless of where the debugger was invoked
779 ;; in the program. WITH-STANDARD-IO-SYNTAX and
780 ;; WITH-SANE-IO-SYNTAX do much of what we want, but
781 ;; * It doesn't affect our internal special variables
782 ;; like *CURRENT-LEVEL-IN-PRINT*.
783 ;; * It isn't customizable.
784 ;; * It sets *PACKAGE* to COMMON-LISP-USER, which is not
785 ;; helpful behavior for a debugger.
786 ;; * There's no particularly good debugger default for
787 ;; *PRINT-PRETTY*, since T is usually what you want
788 ;; -- except absolutely not what you want when you're
789 ;; debugging failures in PRINT-OBJECT logic.
790 ;; We try to address all these issues with explicit
792 (*current-level-in-print
* 0)
793 (*package
* original-package
)
794 (*print-pretty
* original-print-pretty
)
795 ;; Clear the circularity machinery to try to to reduce the
796 ;; pain from sharing the circularity table across all
797 ;; streams; if these are not rebound here, then setting
798 ;; *PRINT-CIRCLE* within the debugger when debugging in a
799 ;; state where something circular was being printed (e.g.,
800 ;; because the debugger was entered on an error in a
801 ;; PRINT-OBJECT method) makes a hopeless mess. Binding them
802 ;; here does seem somewhat ugly because it makes it more
803 ;; difficult to debug the printing-of-circularities code
804 ;; itself; however, as far as I (WHN, 2004-05-29) can see,
805 ;; that's almost entirely academic as long as there's one
806 ;; shared *C-H-T* for all streams (i.e., it's already
807 ;; unreasonably difficult to debug print-circle machinery
808 ;; given the buggy crosstalk between the debugger streams
809 ;; and the stream you're trying to watch), and any fix for
810 ;; that buggy arrangement will likely let this hack go away
812 (sb!impl
::*circularity-hash-table
* . nil
)
813 (sb!impl
::*circularity-counter
* . nil
)
814 (*readtable
* *debug-readtable
*))
816 ;; (Why NREVERSE? PROGV makes the later entries have
817 ;; precedence over the earlier entries.
818 ;; *DEBUG-PRINT-VARIABLE-ALIST* is called an alist, so it's
819 ;; expected that its earlier entries have precedence. And
820 ;; the earlier-has-precedence behavior is mostly more
821 ;; convenient, so that programmers can use PUSH or LIST* to
822 ;; customize *DEBUG-PRINT-VARIABLE-ALIST*.)
823 (nreverse (mapcar #'car
*debug-print-variable-alist
*))
824 (nreverse (mapcar #'cdr
*debug-print-variable-alist
*))
825 (apply fun rest
)))))))
827 ;;; This function is not inlined so it shows up in the backtrace; that
828 ;;; can be rather handy when one has to debug the interplay between
829 ;;; *INVOKE-DEBUGGER-HOOK* and *DEBUGGER-HOOK*.
830 (declaim (notinline run-hook
))
831 (defun run-hook (variable condition
)
832 (let ((old-hook (symbol-value variable
)))
834 (progv (list variable
) (list nil
)
835 (funcall old-hook condition old-hook
)))))
837 ;;; We can bind *stack-top-hint* to a symbol, in which case this function will
838 ;;; resolve that hint lazily before we enter the debugger.
839 (defun resolve-stack-top-hint ()
840 (let ((hint *stack-top-hint
*)
841 (*stack-top-hint
* nil
))
843 ;; No hint, just keep the debugger guts out.
845 (find-caller-name-and-frame))
846 ;; Interrupted. Look for the interrupted frame -- if we don't find one
847 ;; this falls back to the next case.
848 ((and (eq hint
'invoke-interruption
)
849 (nth-value 1 (find-interrupted-name-and-frame))))
850 ;; Name of the first uninteresting frame.
852 (find-caller-of-named-frame hint
))
853 ;; We already have a resolved hint.
857 (defun invoke-debugger (condition)
859 "Enter the debugger."
861 (let ((*stack-top-hint
* (resolve-stack-top-hint)))
863 ;; call *INVOKE-DEBUGGER-HOOK* first, so that *DEBUGGER-HOOK* is not
864 ;; called when the debugger is disabled
865 (run-hook '*invoke-debugger-hook
* condition
)
866 (run-hook '*debugger-hook
* condition
)
868 ;; We definitely want *PACKAGE* to be of valid type.
870 ;; Elsewhere in the system, we use the SANE-PACKAGE function for
871 ;; this, but here causing an exception just as we're trying to handle
872 ;; an exception would be confusing, so instead we use a special hack.
873 (unless (package-name *package
*)
874 (setf *package
* (find-package :cl-user
))
875 (format *error-output
*
876 "The value of ~S was not an undeleted PACKAGE. It has been ~
878 '*package
* *package
*))
880 ;; Before we start our own output, finish any pending output.
881 ;; Otherwise, if the user tried to track the progress of his program
882 ;; using PRINT statements, he'd tend to lose the last line of output
883 ;; or so, which'd be confusing.
884 (flush-standard-output-streams)
886 (funcall-with-debug-io-syntax #'%invoke-debugger condition
)))
888 (defun %print-debugger-invocation-reason
(condition stream
)
889 (format stream
"~2&")
890 ;; Note: Ordinarily it's only a matter of taste whether to use
891 ;; FORMAT "~<...~:>" or to use PPRINT-LOGICAL-BLOCK directly, but
892 ;; until bug 403 is fixed, PPRINT-LOGICAL-BLOCK (STREAM NIL) is
893 ;; definitely preferred, because the FORMAT alternative was acting odd.
894 (pprint-logical-block (stream nil
)
896 "debugger invoked on a ~S~@[ in thread ~_~A~]: ~2I~_~A"
898 #!+sb-thread sb
!thread
:*current-thread
*
903 (defun %invoke-debugger
(condition)
904 (let ((*debug-condition
* condition
)
905 (*debug-restarts
* (compute-restarts condition
))
906 (*nested-debug-condition
* nil
))
908 ;; (The initial output here goes to *ERROR-OUTPUT*, because the
909 ;; initial output is not interactive, just an error message, and
910 ;; when people redirect *ERROR-OUTPUT*, they could reasonably
911 ;; expect to see error messages logged there, regardless of what
912 ;; the debugger does afterwards.)
913 (unless (typep condition
'step-condition
)
914 (%print-debugger-invocation-reason condition
*error-output
*))
916 (setf *nested-debug-condition
* condition
)
917 (let ((ndc-type (type-of *nested-debug-condition
*)))
918 (format *error-output
*
919 "~&~@<(A ~S was caught when trying to print ~S when ~
920 entering the debugger. Printing was aborted and the ~
921 ~S was stored in ~S.)~@:>~%"
925 '*nested-debug-condition
*))
926 (when (typep *nested-debug-condition
* 'cell-error
)
927 ;; what we really want to know when it's e.g. an UNBOUND-VARIABLE:
928 (format *error-output
*
929 "~&(CELL-ERROR-NAME ~S) = ~S~%"
930 '*nested-debug-condition
*
931 (cell-error-name *nested-debug-condition
*)))))
933 (let ((background-p (sb!thread
::debugger-wait-until-foreground-thread
936 ;; After the initial error/condition/whatever announcement to
937 ;; *ERROR-OUTPUT*, we become interactive, and should talk on
938 ;; *DEBUG-IO* from now on. (KLUDGE: This is a normative
939 ;; statement, not a description of reality.:-| There's a lot of
940 ;; older debugger code which was written to do i/o on whatever
941 ;; stream was in fashion at the time, and not all of it has
942 ;; been converted to behave this way. -- WHN 2000-11-16)
945 (let (;; We used to bind *STANDARD-OUTPUT* to *DEBUG-IO*
946 ;; here as well, but that is probably bogus since it
947 ;; removes the users ability to do output to a redirected
948 ;; *S-O*. Now we just rebind it so that users can temporarily
949 ;; frob it. FIXME: This and other "what gets bound when"
950 ;; behaviour should be documented in the manual.
951 (*standard-output
* *standard-output
*)
952 ;; This seems reasonable: e.g. if the user has redirected
953 ;; *ERROR-OUTPUT* to some log file, it's probably wrong
954 ;; to send errors which occur in interactive debugging to
955 ;; that file, and right to send them to *DEBUG-IO*.
956 (*error-output
* *debug-io
*))
957 (unless (typep condition
'step-condition
)
958 (when *debug-beginner-help-p
*
960 "~%~@<Type HELP for debugger help, or ~
961 (SB-EXT:EXIT) to exit from SBCL.~:@>~2%"))
962 (show-restarts *debug-restarts
* *debug-io
*))
965 (sb!thread
::release-foreground
))))))
967 ;;; this function is for use in *INVOKE-DEBUGGER-HOOK* when ordinary
968 ;;; ANSI behavior has been suppressed by the "--disable-debugger"
969 ;;; command-line option
970 (defun debugger-disabled-hook (condition previous-hook
)
971 (declare (ignore previous-hook
))
972 ;; There is no one there to interact with, so report the
973 ;; condition and terminate the program.
974 (let ((*suppress-print-errors
* t
)
975 (condition-error-message
976 #.
(format nil
"A nested error within --disable-debugger error ~
977 handling prevents displaying the original error. Attempting ~
978 to print a backtrace."))
979 (backtrace-error-message
980 #.
(format nil
"A nested error within --disable-debugger error ~
981 handling prevents printing the backtrace. Sorry, exiting.")))
983 ((failure-quit (&key abort
)
984 (/show0
"in FAILURE-QUIT (in --disable-debugger debugger hook)")
985 (exit :code
1 :abort abort
))
986 (display-condition ()
991 ;; printing failed, try to describe it
992 (describe-condition)))
994 ;; ok, give up trying to display the error and inform the user about it
995 (finish-output *error-output
*)
996 (%primitive print condition-error-message
))))
998 (format *error-output
*
999 "~&~@<Unhandled ~S~@[ in thread ~S~]: ~2I~_~A~:>~2%"
1001 #!+sb-thread sb
!thread
:*current-thread
*
1004 (finish-output *error-output
*))
1005 (describe-condition ()
1006 (format *error-output
*
1007 "~&Unhandled ~S~@[ in thread ~S~]:~%"
1009 #!+sb-thread sb
!thread
:*current-thread
*
1011 (describe condition
*error-output
*)
1012 (finish-output *error-output
*))
1013 (display-backtrace ()
1015 (print-backtrace :stream
*error-output
*
1016 :from
:interrupted-frame
1020 (finish-output *error-output
*)))
1021 ;; This HANDLER-CASE is here mostly to stop output immediately
1022 ;; (and fall through to QUIT) when there's an I/O error. Thus,
1023 ;; when we're run under a shell script or something, we can die
1024 ;; cleanly when the script dies (and our pipes are cut), instead
1025 ;; of falling into ldb or something messy like that. Similarly, we
1026 ;; can terminate cleanly even if BACKTRACE dies because of bugs in
1027 ;; user PRINT-OBJECT methods. Separate the error handling of the
1028 ;; two phases to maximize the chance of emitting some useful
1034 (format *error-output
*
1035 "~%unhandled condition in --disable-debugger mode, quitting~%")
1036 (finish-output *error-output
*)
1039 ;; We IGNORE-ERRORS here because even %PRIMITIVE PRINT can
1040 ;; fail when our output streams are blown away, as e.g. when
1041 ;; we're running under a Unix shell script and it dies somehow
1042 ;; (e.g. because of a SIGINT). In that case, we might as well
1043 ;; just give it up for a bad job, and stop trying to notify
1044 ;; the user of anything.
1046 ;; Actually, the only way I've run across to exercise the
1047 ;; problem is to have more than one layer of shell script.
1048 ;; I have a shell script which does
1049 ;; time nice -10 sh make.sh "$1" 2>&1 | tee make.tmp
1050 ;; and the problem occurs when I interrupt this with Ctrl-C
1051 ;; under Linux 2.2.14-5.0 and GNU bash, version 1.14.7(1).
1052 ;; I haven't figured out whether it's bash, time, tee, Linux, or
1053 ;; what that is responsible, but that it's possible at all
1054 ;; means that we should IGNORE-ERRORS here. -- WHN 2001-04-24
1056 (%primitive print backtrace-error-message
))
1057 (failure-quit :abort t
))))))
1059 (defvar *old-debugger-hook
* nil
)
1061 ;;; halt-on-failures and prompt-on-failures modes, suitable for
1062 ;;; noninteractive and interactive use respectively
1063 (defun disable-debugger ()
1065 "When invoked, this function will turn off both the SBCL debugger
1066 and LDB (the low-level debugger). See also ENABLE-DEBUGGER."
1067 ;; *DEBUG-IO* used to be set here to *ERROR-OUTPUT* which is sort
1068 ;; of unexpected but mostly harmless, but then ENABLE-DEBUGGER had
1069 ;; to set it to a suitable value again and be very careful,
1070 ;; especially if the user has also set it. -- MG 2005-07-15
1071 (unless (eq *invoke-debugger-hook
* 'debugger-disabled-hook
)
1072 (setf *old-debugger-hook
* *invoke-debugger-hook
*
1073 *invoke-debugger-hook
* 'debugger-disabled-hook
))
1074 ;; This is not inside the UNLESS to ensure that LDB is disabled
1075 ;; regardless of what the old value of *INVOKE-DEBUGGER-HOOK* was.
1076 ;; This might matter for example when restoring a core.
1077 (sb!alien
:alien-funcall
(sb!alien
:extern-alien
"disable_lossage_handler"
1078 (function sb
!alien
:void
))))
1080 (defun enable-debugger ()
1082 "Restore the debugger if it has been turned off by DISABLE-DEBUGGER."
1083 (when (eql *invoke-debugger-hook
* 'debugger-disabled-hook
)
1084 (setf *invoke-debugger-hook
* *old-debugger-hook
*
1085 *old-debugger-hook
* nil
))
1086 (sb!alien
:alien-funcall
(sb!alien
:extern-alien
"enable_lossage_handler"
1087 (function sb
!alien
:void
))))
1089 (defun show-restarts (restarts s
)
1090 (cond ((null restarts
)
1092 "~&(no restarts: If you didn't do this on purpose, ~
1093 please report it as a bug.)~%"))
1095 (format s
"~&restarts (invokable by number or by ~
1096 possibly-abbreviated name):~%")
1100 (dolist (restart restarts
)
1101 (let ((name (restart-name restart
)))
1103 (let ((len (length (princ-to-string name
))))
1104 (when (> len max-name-len
)
1105 (setf max-name-len len
))))))
1106 (unless (zerop max-name-len
)
1107 (incf max-name-len
3))
1108 (dolist (restart restarts
)
1109 (let ((name (restart-name restart
)))
1110 ;; FIXME: maybe it would be better to display later names
1111 ;; in parens instead of brakets, not just omit them fully.
1112 ;; Call BREAK, call BREAK in the debugger, and tell me
1113 ;; it's not confusing looking. --NS 20050310
1114 (cond ((member name names-used
)
1115 (format s
"~& ~2D: ~V@T~A~%" count max-name-len restart
))
1117 (format s
"~& ~2D: [~VA] ~A~%"
1118 count
(- max-name-len
3) name restart
)
1119 (push name names-used
))))
1122 (defvar *debug-loop-fun
* #'debug-loop-fun
1124 "A function taking no parameters that starts the low-level debug loop.")
1126 ;;; When the debugger is invoked due to a stepper condition, we don't
1127 ;;; want to print the current frame before the first prompt for aesthetic
1129 (defvar *suppress-frame-print
* nil
)
1131 ;;; This calls DEBUG-LOOP, performing some simple initializations
1132 ;;; before doing so. INVOKE-DEBUGGER calls this to actually get into
1133 ;;; the debugger. SB!KERNEL::ERROR-ERROR calls this in emergencies
1134 ;;; to get into a debug prompt as quickly as possible with as little
1135 ;;; risk as possible for stepping on whatever is causing recursive
1137 (defun internal-debug ()
1138 (let ((*in-the-debugger
* t
)
1139 (*read-suppress
* nil
))
1140 (unless (typep *debug-condition
* 'step-condition
)
1141 (clear-input *debug-io
*))
1142 (let ((*suppress-frame-print
* (typep *debug-condition
* 'step-condition
)))
1143 (funcall *debug-loop-fun
*))))
1147 ;;; Note: This defaulted to T in CMU CL. The changed default in SBCL
1148 ;;; was motivated by desire to play nicely with ILISP.
1149 (defvar *flush-debug-errors
* nil
1151 "When set, avoid calling INVOKE-DEBUGGER recursively when errors occur while
1152 executing in the debugger.")
1154 (defun debug-read (stream eof-restart
)
1155 (declare (type stream stream
))
1156 (let* ((eof-marker (cons nil nil
))
1157 (form (read stream nil eof-marker
)))
1158 (if (eq form eof-marker
)
1159 (invoke-restart eof-restart
)
1162 (defun debug-loop-fun ()
1163 (let* ((*debug-command-level
* (1+ *debug-command-level
*))
1164 (*real-stack-top
* (sb!di
:top-frame
))
1165 (*stack-top
* (or *stack-top-hint
* *real-stack-top
*))
1166 (*stack-top-hint
* nil
)
1167 (*current-frame
* *stack-top
*))
1168 (handler-bind ((sb!di
:debug-condition
1170 (princ condition
*debug-io
*)
1171 (/show0
"handling d-c by THROWing DEBUG-LOOP-CATCHER")
1172 (throw 'debug-loop-catcher nil
))))
1173 (cond (*suppress-frame-print
*
1174 (setf *suppress-frame-print
* nil
))
1177 (print-frame-call *current-frame
* *debug-io
* :print-frame-source t
)))
1179 (catch 'debug-loop-catcher
1180 (handler-bind ((error (lambda (condition)
1181 (when *flush-debug-errors
*
1182 (clear-input *debug-io
*)
1183 (princ condition
*debug-io
*)
1185 "~&error flushed (because ~
1187 '*flush-debug-errors
*)
1188 (/show0
"throwing DEBUG-LOOP-CATCHER")
1189 (throw 'debug-loop-catcher nil
)))))
1190 ;; We have to bind LEVEL for the restart function created
1191 ;; by WITH-SIMPLE-RESTART, and we need the explicit ABORT
1192 ;; restart that exists now so that EOF from read can drop
1193 ;; one debugger level.
1194 (let ((level *debug-command-level
*)
1195 (restart-commands (make-restart-commands))
1196 (abort-restart-for-eof (find-restart 'abort
)))
1197 (flush-standard-output-streams)
1198 (debug-prompt *debug-io
*)
1199 (force-output *debug-io
*)
1200 (with-simple-restart (abort
1201 "~@<Reduce debugger level (to debug level ~W).~@:>"
1203 (let* ((exp (debug-read *debug-io
* abort-restart-for-eof
))
1204 (cmd-fun (debug-command-p exp restart-commands
)))
1205 (cond ((not cmd-fun
)
1206 (debug-eval-print exp
))
1209 "~&Your command, ~S, is ambiguous:~%"
1211 (dolist (ele cmd-fun
)
1212 (format *debug-io
* " ~A~%" ele
)))
1214 (funcall cmd-fun
))))))))))))
1216 (defvar *auto-eval-in-frame
* t
1218 "When set (the default), evaluations in the debugger's command loop occur
1219 relative to the current frame's environment without the need of debugger
1220 forms that explicitly control this kind of evaluation.")
1222 (defun debug-eval (expr)
1223 (cond ((not (and (fboundp 'compile
) *auto-eval-in-frame
*))
1225 ((frame-has-debug-vars-p *current-frame
*)
1226 (sb!di
:eval-in-frame
*current-frame
* expr
))
1228 (format *debug-io
* "; No debug variables for current frame: ~
1229 using EVAL instead of EVAL-IN-FRAME.~%")
1232 (defun debug-eval-print (expr)
1233 (/noshow
"entering DEBUG-EVAL-PRINT" expr
)
1234 (let ((values (multiple-value-list
1235 (interactive-eval expr
:eval
#'debug-eval
))))
1236 (/noshow
"done with EVAL in DEBUG-EVAL-PRINT")
1237 (dolist (value values
)
1238 (fresh-line *debug-io
*)
1239 (prin1 value
*debug-io
*)))
1240 (force-output *debug-io
*))
1242 ;;;; debug loop functions
1244 ;;; These commands are functions, not really commands, so that users
1245 ;;; can get their hands on the values returned.
1247 (defun var-valid-in-frame-p (var location
&optional
(frame *current-frame
*))
1248 ;; arg count errors are checked before anything is set up but they
1249 ;; are reporeted in *elsewhere*, which is after start-pc saved in the
1250 ;; debug function, defeating the checks.
1251 (and (not (sb!di
::tl-invalid-arg-count-error-p frame
))
1252 (eq (sb!di
:debug-var-validity var location
) :valid
)))
1254 (eval-when (:execute
:compile-toplevel
)
1256 (sb!xc
:defmacro define-var-operation
(ref-or-set &optional value-var
)
1257 `(let* ((temp (etypecase name
1258 (symbol (sb!di
:debug-fun-symbol-vars
1259 (sb!di
:frame-debug-fun
*current-frame
*)
1261 (simple-string (sb!di
:ambiguous-debug-vars
1262 (sb!di
:frame-debug-fun
*current-frame
*)
1264 (location (sb!di
:frame-code-location
*current-frame
*))
1265 ;; Let's only deal with valid variables.
1266 (vars (remove-if-not (lambda (v)
1267 (var-valid-in-frame-p v location
))
1269 (declare (list vars
))
1271 (error "No known valid variables match ~S." name
))
1272 ((= (length vars
) 1)
1275 '(sb!di
:debug-var-value
(car vars
) *current-frame
*))
1277 `(setf (sb!di
:debug-var-value
(car vars
) *current-frame
*)
1280 ;; Since we have more than one, first see whether we have
1281 ;; any variables that exactly match the specification.
1282 (let* ((name (etypecase name
1283 (symbol (symbol-name name
))
1284 (simple-string name
)))
1285 ;; FIXME: REMOVE-IF-NOT is deprecated, use STRING/=
1287 (exact (remove-if-not (lambda (v)
1288 (string= (sb!di
:debug-var-symbol-name v
)
1291 (vars (or exact vars
)))
1292 (declare (simple-string name
)
1295 ;; Check now for only having one variable.
1296 ((= (length vars
) 1)
1299 '(sb!di
:debug-var-value
(car vars
) *current-frame
*))
1301 `(setf (sb!di
:debug-var-value
(car vars
) *current-frame
*)
1303 ;; If there weren't any exact matches, flame about
1304 ;; ambiguity unless all the variables have the same
1309 (string= (sb!di
:debug-var-symbol-name v
)
1310 (sb!di
:debug-var-symbol-name
(car vars
))))
1312 (error "specification ambiguous:~%~{ ~A~%~}"
1313 (mapcar #'sb
!di
:debug-var-symbol-name
1315 vars
:test
#'string
=
1316 :key
#'sb
!di
:debug-var-symbol-name
))))
1317 ;; All names are the same, so see whether the user
1318 ;; ID'ed one of them.
1320 (let ((v (find id vars
:key
#'sb
!di
:debug-var-id
)))
1323 "invalid variable ID, ~W: should have been one of ~S"
1325 (mapcar #'sb
!di
:debug-var-id vars
)))
1328 '(sb!di
:debug-var-value v
*current-frame
*))
1330 `(setf (sb!di
:debug-var-value v
*current-frame
*)
1333 (error "Specify variable ID to disambiguate ~S. Use one of ~S."
1335 (mapcar #'sb
!di
:debug-var-id vars
)))))))))
1339 ;;; FIXME: This doesn't work. It would be real nice we could make it
1340 ;;; work! Alas, it doesn't seem to work in CMU CL X86 either..
1341 (defun var (name &optional
(id 0 id-supplied
))
1343 "Return a variable's value if possible. NAME is a simple-string or symbol.
1344 If it is a simple-string, it is an initial substring of the variable's name.
1345 If name is a symbol, it has the same name and package as the variable whose
1346 value this function returns. If the symbol is uninterned, then the variable
1347 has the same name as the symbol, but it has no package.
1349 If name is the initial substring of variables with different names, then
1350 this return no values after displaying the ambiguous names. If name
1351 determines multiple variables with the same name, then you must use the
1352 optional id argument to specify which one you want. If you left id
1353 unspecified, then this returns no values after displaying the distinguishing
1356 The result of this function is limited to the availability of variable
1357 information. This is SETF'able."
1358 (define-var-operation :ref
))
1359 (defun (setf var
) (value name
&optional
(id 0 id-supplied
))
1360 (define-var-operation :set value
))
1362 ;;; This returns the COUNT'th arg as the user sees it from args, the
1363 ;;; result of SB!DI:DEBUG-FUN-LAMBDA-LIST. If this returns a
1364 ;;; potential DEBUG-VAR from the lambda-list, then the second value is
1365 ;;; T. If this returns a keyword symbol or a value from a rest arg,
1366 ;;; then the second value is NIL.
1368 ;;; FIXME: There's probably some way to merge the code here with
1369 ;;; FRAME-ARGS-AS-LIST. (A fair amount of logic is already shared
1370 ;;; through LAMBDA-LIST-ELEMENT-DISPATCH, but I suspect more could be.)
1371 (declaim (ftype (function (index list
)) nth-arg
))
1372 (defun nth-arg (count args
)
1374 (dolist (ele args
(error "The argument specification ~S is out of range."
1376 (lambda-list-element-dispatch ele
1377 :required
((if (zerop n
) (return (values ele t
))))
1378 :optional
((if (zerop n
) (return (values (second ele
) t
))))
1379 :keyword
((cond ((zerop n
)
1380 (return (values (second ele
) nil
)))
1382 (return (values (third ele
) t
)))))
1383 :deleted
((if (zerop n
) (return (values ele t
))))
1384 :rest
((let ((var (second ele
)))
1385 (lambda-var-dispatch var
(sb!di
:frame-code-location
1387 (error "unused &REST argument before n'th argument")
1389 (sb!di
:debug-var-value var
*current-frame
*)
1391 "The argument specification ~S is out of range."
1394 (return-from nth-arg
(values value nil
))
1396 (error "invalid &REST argument before n'th argument")))))
1401 "Return the N'th argument's value if possible. Argument zero is the first
1402 argument in a frame's default printed representation. Count keyword/value
1403 pairs as separate arguments."
1404 #!+precise-arg-count-error
1405 (when (sb!di
::tl-invalid-arg-count-error-p
*current-frame
*)
1407 (arg-count-error-frame-nth-arg n
*current-frame
*)))
1408 (multiple-value-bind (var lambda-var-p
)
1409 (nth-arg n
(handler-case (sb!di
:debug-fun-lambda-list
1410 (sb!di
:frame-debug-fun
*current-frame
*))
1411 (sb!di
:lambda-list-unavailable
()
1412 (error "No argument values are available."))))
1414 (lambda-var-dispatch var
(sb!di
:frame-code-location
*current-frame
*)
1415 (error "Unused arguments have no values.")
1416 (sb!di
:debug-var-value var
*current-frame
*)
1417 (error "invalid argument value"))
1420 ;;;; machinery for definition of debug loop commands
1422 (defvar *debug-commands
* nil
)
1424 ;;; Interface to *DEBUG-COMMANDS*. No required arguments in args are
1426 (defmacro !def-debug-command
(name args
&rest body
)
1427 (let ((fun-name (symbolicate name
"-DEBUG-COMMAND")))
1429 (setf *debug-commands
*
1430 (remove ,name
*debug-commands
* :key
#'car
:test
#'string
=))
1431 (defun ,fun-name
,args
1432 (unless *in-the-debugger
*
1433 (error "invoking debugger command while outside the debugger"))
1435 (push (cons ,name
#',fun-name
) *debug-commands
*)
1438 (defun !def-debug-command-alias
(new-name existing-name
)
1439 (let ((pair (assoc existing-name
*debug-commands
* :test
#'string
=)))
1440 (unless pair
(error "unknown debug command name: ~S" existing-name
))
1441 (push (cons new-name
(cdr pair
)) *debug-commands
*))
1444 ;;; This takes a symbol and uses its name to find a debugger command,
1445 ;;; using initial substring matching. It returns the command function
1446 ;;; if form identifies only one command, but if form is ambiguous,
1447 ;;; this returns a list of the command names. If there are no matches,
1448 ;;; this returns nil. Whenever the loop that looks for a set of
1449 ;;; possibilities encounters an exact name match, we return that
1450 ;;; command function immediately.
1451 (defun debug-command-p (form &optional other-commands
)
1452 (if (or (symbolp form
) (integerp form
))
1456 (format nil
"~W" form
)))
1459 (declare (simple-string name
)
1463 ;; Find matching commands, punting if exact match.
1464 (flet ((match-command (ele)
1465 (let* ((str (car ele
))
1466 (str-len (length str
)))
1467 (declare (simple-string str
)
1469 (cond ((< str-len len
))
1471 (when (string= name str
:end1 len
:end2 len
)
1472 (return-from debug-command-p
(cdr ele
))))
1473 ((string= name str
:end1 len
:end2 len
)
1475 (mapc #'match-command
*debug-commands
*)
1476 (mapc #'match-command other-commands
))
1478 ;; Return the right value.
1479 (cond ((not res
) nil
)
1482 (t ; Just return the names.
1483 (do ((cmds res
(cdr cmds
)))
1485 (setf (car cmds
) (caar cmds
))))))))
1487 ;;; Return a list of debug commands (in the same format as
1488 ;;; *DEBUG-COMMANDS*) that invoke each active restart.
1490 ;;; Two commands are made for each restart: one for the number, and
1491 ;;; one for the restart name (unless it's been shadowed by an earlier
1492 ;;; restart of the same name, or it is NIL).
1493 (defun make-restart-commands (&optional
(restarts *debug-restarts
*))
1495 (num 0)) ; better be the same as show-restarts!
1496 (dolist (restart restarts
)
1497 (let ((name (string (restart-name restart
))))
1500 (/show0
"in restart-command closure, about to i-r-i")
1501 (invoke-restart-interactively restart
))))
1502 (push (cons (prin1-to-string num
) restart-fun
) commands
)
1503 (unless (or (null (restart-name restart
))
1504 (find name commands
:key
#'car
:test
#'string
=))
1505 (push (cons name restart-fun
) commands
))))
1509 ;;;; frame-changing commands
1511 (!def-debug-command
"UP" ()
1512 (let ((next (sb!di
:frame-up
*current-frame
*)))
1514 (setf *current-frame
* next
)
1515 (print-frame-call next
*debug-io
*))
1517 (format *debug-io
* "~&Top of stack.")))))
1519 (!def-debug-command
"DOWN" ()
1520 (let ((next (sb!di
:frame-down
*current-frame
*)))
1522 (setf *current-frame
* next
)
1523 (print-frame-call next
*debug-io
*))
1525 (format *debug-io
* "~&Bottom of stack.")))))
1527 (!def-debug-command-alias
"D" "DOWN")
1529 (!def-debug-command
"BOTTOM" ()
1530 (do ((prev *current-frame
* lead
)
1531 (lead (sb!di
:frame-down
*current-frame
*) (sb!di
:frame-down lead
)))
1533 (setf *current-frame
* prev
)
1534 (print-frame-call prev
*debug-io
*))))
1536 (!def-debug-command-alias
"B" "BOTTOM")
1538 (!def-debug-command
"FRAME" (&optional
1539 (n (read-prompting-maybe "frame number: ")))
1540 (setf *current-frame
*
1541 (multiple-value-bind (next-frame-fun limit-string
)
1542 (if (< n
(sb!di
:frame-number
*current-frame
*))
1543 (values #'sb
!di
:frame-up
"top")
1544 (values #'sb
!di
:frame-down
"bottom"))
1545 (do ((frame *current-frame
*))
1546 ((= n
(sb!di
:frame-number frame
))
1548 (let ((next-frame (funcall next-frame-fun frame
)))
1550 (setf frame next-frame
))
1553 "The ~A of the stack was encountered.~%"
1555 (return frame
)))))))
1556 (print-frame-call *current-frame
* *debug-io
*))
1558 (!def-debug-command-alias
"F" "FRAME")
1560 ;;;; commands for entering and leaving the debugger
1562 (!def-debug-command
"TOPLEVEL" ()
1563 (throw 'toplevel-catcher nil
))
1566 (!def-debug-command-alias
"TOP" "TOPLEVEL")
1568 (!def-debug-command
"RESTART" ()
1569 (/show0
"doing RESTART debug-command")
1570 (let ((num (read-if-available :prompt
)))
1571 (when (eq num
:prompt
)
1572 (show-restarts *debug-restarts
* *debug-io
*)
1573 (write-string "restart: " *debug-io
*)
1574 (force-output *debug-io
*)
1575 (setf num
(read *debug-io
*)))
1576 (let ((restart (typecase num
1578 (nth num
*debug-restarts
*))
1580 (find num
*debug-restarts
* :key
#'restart-name
1581 :test
(lambda (sym1 sym2
)
1582 (string= (symbol-name sym1
)
1583 (symbol-name sym2
)))))
1585 (format *debug-io
* "~S is invalid as a restart name.~%"
1587 (return-from restart-debug-command nil
)))))
1588 (/show0
"got RESTART")
1590 (invoke-restart-interactively restart
)
1591 (princ "There is no such restart." *debug-io
*)))))
1593 ;;;; information commands
1595 (!def-debug-command
"HELP" ()
1596 ;; CMU CL had a little toy pager here, but "if you aren't running
1597 ;; ILISP (or a smart windowing system, or something) you deserve to
1598 ;; lose", so we've dropped it in SBCL. However, in case some
1599 ;; desperate holdout is running this on a dumb terminal somewhere,
1600 ;; we tell him where to find the message stored as a string.
1602 "~&~A~2%(The HELP string is stored in ~S.)~%"
1604 '*debug-help-string
*))
1606 (!def-debug-command-alias
"?" "HELP")
1608 (!def-debug-command
"ERROR" ()
1609 (format *debug-io
* "~A~%" *debug-condition
*)
1610 (show-restarts *debug-restarts
* *debug-io
*))
1612 (!def-debug-command
"BACKTRACE" ()
1613 (print-backtrace :count
(read-if-available most-positive-fixnum
)))
1615 (!def-debug-command
"PRINT" ()
1616 (print-frame-call *current-frame
* *debug-io
*))
1618 (!def-debug-command-alias
"P" "PRINT")
1620 (!def-debug-command
"LIST-LOCALS" ()
1621 (let ((d-fun (sb!di
:frame-debug-fun
*current-frame
*)))
1622 (if (sb!di
:debug-var-info-available d-fun
)
1623 (let ((*standard-output
* *debug-io
*)
1624 (location (sb!di
:frame-code-location
*current-frame
*))
1625 (prefix (read-if-available nil
))
1630 (dolist (v (sb!di
:ambiguous-debug-vars
1632 (if prefix
(string prefix
) "")))
1634 (when (var-valid-in-frame-p v location
)
1635 (setf any-valid-p t
)
1636 (case (sb!di
::debug-var-info v
)
1638 (setf more-context
(sb!di
:debug-var-value v
*current-frame
*)))
1640 (setf more-count
(sb!di
:debug-var-value v
*current-frame
*))))
1641 (format *debug-io
* "~S~:[#~W~;~*~] = ~S~%"
1642 (sb!di
:debug-var-symbol v
)
1643 (zerop (sb!di
:debug-var-id v
))
1644 (sb!di
:debug-var-id v
)
1645 (sb!di
:debug-var-value v
*current-frame
*))))
1646 (when (and more-context more-count
)
1647 (format *debug-io
* "~S = ~S~%"
1649 (multiple-value-list (sb!c
:%more-arg-values more-context
0 more-count
))))
1653 "There are no local variables ~@[starting with ~A ~]~
1658 "All variables ~@[starting with ~A ~]currently ~
1659 have invalid values."
1661 (write-line "There is no variable information available."
1664 (!def-debug-command-alias
"L" "LIST-LOCALS")
1666 (!def-debug-command
"SOURCE" ()
1667 (print (code-location-source-form (sb!di
:frame-code-location
*current-frame
*)
1668 (read-if-available 0))
1671 ;;;; source location printing
1673 (defun code-location-source-form (location context
&optional
(errorp t
))
1674 (let* ((start-location (maybe-block-start-location location
))
1675 (form-num (sb!di
:code-location-form-number start-location
)))
1676 (multiple-value-bind (translations form
)
1677 (sb!di
:get-toplevel-form start-location
)
1678 (cond ((< form-num
(length translations
))
1679 (sb!di
:source-path-context form
1680 (svref translations form-num
)
1683 (funcall (if errorp
#'error
#'warn
)
1684 "~@<Bogus form-number: the source file has ~
1685 probably changed too much to cope with.~:@>"))))))
1688 ;;; start single-stepping
1689 (!def-debug-command
"START" ()
1690 (if (typep *debug-condition
* 'step-condition
)
1691 (format *debug-io
* "~&Already single-stepping.~%")
1692 (let ((restart (find-restart 'continue
*debug-condition
*)))
1694 (sb!impl
::enable-stepping
)
1695 (invoke-restart restart
))
1697 (format *debug-io
* "~&Non-continuable error, cannot start stepping.~%"))))))
1699 (defmacro !def-step-command
(command-name restart-name
)
1700 `(!def-debug-command
,command-name
()
1701 (if (typep *debug-condition
* 'step-condition
)
1702 (let ((restart (find-restart ',restart-name
*debug-condition
*)))
1704 (invoke-restart restart
))
1705 (format *debug-io
* "~&Not currently single-stepping. (Use START to activate the single-stepper)~%"))))
1707 (!def-step-command
"STEP" step-into
)
1708 (!def-step-command
"NEXT" step-next
)
1709 (!def-step-command
"STOP" step-continue
)
1711 (!def-debug-command-alias
"S" "STEP")
1712 (!def-debug-command-alias
"N" "NEXT")
1714 (!def-debug-command
"OUT" ()
1715 (if (typep *debug-condition
* 'step-condition
)
1716 (if sb
!impl
::*step-out
*
1717 (let ((restart (find-restart 'step-out
*debug-condition
*)))
1719 (invoke-restart restart
))
1720 (format *debug-io
* "~&OUT can only be used step out of frames that were originally stepped into with STEP.~%"))
1721 (format *debug-io
* "~&Not currently single-stepping. (Use START to activate the single-stepper)~%")))
1723 ;;; miscellaneous commands
1725 (!def-debug-command
"DESCRIBE" ()
1726 (let* ((curloc (sb!di
:frame-code-location
*current-frame
*))
1727 (debug-fun (sb!di
:code-location-debug-fun curloc
))
1728 (function (sb!di
:debug-fun-fun debug-fun
)))
1731 (format *debug-io
* "can't figure out the function for this frame"))))
1733 (!def-debug-command
"SLURP" ()
1734 (loop while
(read-char-no-hang *standard-input
*)))
1736 ;;; RETURN-FROM-FRAME and RESTART-FRAME
1738 (defun unwind-to-frame-and-call (frame thunk
)
1739 #!+unwind-to-frame-and-call-vop
1740 (flet ((sap-int/fixnum
(sap)
1741 ;; On unithreaded X86 *BINDING-STACK-POINTER* and
1742 ;; *CURRENT-CATCH-BLOCK* are negative, so we need to jump through
1743 ;; some hoops to make these calculated values negative too.
1744 (ash (truly-the (signed-byte #.sb
!vm
:n-word-bits
)
1746 (- sb
!vm
::n-fixnum-tag-bits
))))
1747 ;; To properly unwind the stack, we need three pieces of information:
1748 ;; * The unwind block that should be active after the unwind
1749 ;; * The catch block that should be active after the unwind
1750 ;; * The values that the binding stack pointer should have after the
1752 (let ((block (sap-int/fixnum
(find-enclosing-catch-block frame
)))
1753 (unbind-to (find-binding-stack-pointer frame
)))
1754 ;; This VOP will run the neccessary cleanup forms, reset the fp, and
1755 ;; then call the supplied function.
1756 (sb!vm
::%primitive sb
!vm
::unwind-to-frame-and-call
1757 (sb!di
::frame-pointer frame
)
1758 (find-enclosing-uwp frame
)
1760 ;; Before calling the user-specified
1761 ;; function, we need to restore the binding
1762 ;; stack and the catch block. The unwind block
1763 ;; is taken care of by the VOP.
1764 (sb!vm
::%primitive sb
!vm
::unbind-to-here
1766 (setf sb
!vm
::*current-catch-block
* block
)
1768 #!-unwind-to-frame-and-call-vop
1769 (let ((tag (gensym)))
1770 (sb!di
:replace-frame-catch-tag frame
1771 'sb
!c
:debug-catch-tag
1775 #!+unwind-to-frame-and-call-vop
1776 (defun find-binding-stack-pointer (frame)
1777 (let ((debug-fun (sb!di
:frame-debug-fun frame
)))
1778 (if (eq (sb!di
:debug-fun-kind debug-fun
) :external
)
1779 ;; XEPs do not bind anything, nothing to restore.
1780 ;; But they may call other code through SATISFIES
1781 ;; declaration, check that the interrupt is actually in the XEP.
1782 (and (sb!di
::compiled-frame-escaped frame
)
1783 sb
!kernel
::*interr-current-bsp
*)
1784 (let* ((compiled-debug-fun (and
1785 (typep debug-fun
'sb
!di
::compiled-debug-fun
)
1786 (sb!di
::compiled-debug-fun-compiler-debug-fun debug-fun
)))
1787 (bsp-save-offset (and compiled-debug-fun
1788 (sb!c
::compiled-debug-fun-bsp-save compiled-debug-fun
))))
1789 (when bsp-save-offset
1790 (sb!di
::sub-access-debug-var-slot
(sb!di
::frame-pointer frame
) bsp-save-offset
))))))
1792 (defun find-enclosing-catch-block (frame)
1793 ;; Walk the catch block chain looking for the first entry with an address
1794 ;; higher than the pointer for FRAME or a null pointer.
1795 (let* ((frame-pointer (sb!di
::frame-pointer frame
))
1796 (current-block (int-sap (ldb (byte #.sb
!vm
:n-word-bits
0)
1797 (ash sb
!vm
::*current-catch-block
*
1798 sb
!vm
:n-fixnum-tag-bits
))))
1799 (enclosing-block (loop for block
= current-block
1800 then
(sap-ref-sap block
1801 (* sb
!vm
:catch-block-previous-catch-slot
1802 sb
!vm
::n-word-bytes
))
1803 when
(or (zerop (sap-int block
))
1804 #!+stack-grows-downward-not-upward
1805 (sap> block frame-pointer
)
1806 #!-stack-grows-downward-not-upward
1807 (sap< block frame-pointer
))
1811 (defun find-enclosing-uwp (frame)
1812 ;; Walk the UWP chain looking for the first entry with an address
1813 ;; higher than the pointer for FRAME or a null pointer.
1814 (let* ((frame-pointer (sb!di
::frame-pointer frame
))
1815 (current-uwp (int-sap (ldb (byte #.sb
!vm
:n-word-bits
0)
1816 (ash sb
!vm
::*current-unwind-protect-block
*
1817 sb
!vm
:n-fixnum-tag-bits
))))
1818 (enclosing-uwp (loop for uwp-block
= current-uwp
1819 then
(sap-ref-sap uwp-block
1820 sb
!vm
:unwind-block-current-uwp-slot
)
1821 when
(or (zerop (sap-int uwp-block
))
1822 #!+stack-grows-downward-not-upward
1823 (sap> uwp-block frame-pointer
)
1824 #!-stack-grows-downward-not-upward
1825 (sap< uwp-block frame-pointer
))
1829 (!def-debug-command
"RETURN" (&optional
1830 (return (read-prompting-maybe
1832 (if (frame-has-debug-tag-p *current-frame
*)
1833 (let* ((code-location (sb!di
:frame-code-location
*current-frame
*))
1834 (values (multiple-value-list
1835 (funcall (sb!di
:preprocess-for-eval return code-location
)
1837 (unwind-to-frame-and-call *current-frame
* (lambda ()
1838 (values-list values
))))
1840 "~@<can't find a tag for this frame ~
1841 ~2I~_(hint: try increasing the DEBUG optimization quality ~
1842 and recompiling)~:@>")))
1844 (!def-debug-command
"RESTART-FRAME" ()
1845 (if (frame-has-debug-tag-p *current-frame
*)
1846 (multiple-value-bind (fname args
) (frame-call *current-frame
*)
1847 (multiple-value-bind (fun arglist ok
)
1848 (if (and (legal-fun-name-p fname
) (fboundp fname
))
1849 (values (fdefinition fname
) args t
)
1850 (values (sb!di
:debug-fun-fun
(sb!di
:frame-debug-fun
*current-frame
*))
1851 (frame-args-as-list *current-frame
*)
1855 (y-or-n-p "~@<No global function for the frame, but we ~
1856 do have access to a function object that we ~
1857 can try to call -- but if it is normally part ~
1858 of a closure, then this is NOT going to end well.~_~_~
1859 Try it anyways?~:@>")))
1860 (unwind-to-frame-and-call *current-frame
*
1863 (declare (optimize (debug 0)))
1864 (apply fun arglist
))))
1866 "Can't restart ~S: no function for frame."
1869 "~@<Can't restart ~S: tag not found. ~
1870 ~2I~_(hint: try increasing the DEBUG optimization quality ~
1871 and recompiling)~:@>"
1874 (defun frame-has-debug-tag-p (frame)
1875 #!+unwind-to-frame-and-call-vop
1876 ;; XEPs do not bind anything, nothing to restore
1877 (find-binding-stack-pointer frame
)
1878 #!-unwind-to-frame-and-call-vop
1879 (find 'sb
!c
:debug-catch-tag
(sb!di
::frame-catches frame
) :key
#'car
))
1881 (defun frame-has-debug-vars-p (frame)
1882 (sb!di
:debug-var-info-available
1883 (sb!di
:code-location-debug-fun
1884 (sb!di
:frame-code-location frame
))))
1886 ;;;; debug loop command utilities
1888 (defun read-prompting-maybe (prompt)
1889 (unless (sb!int
:listen-skip-whitespace
*debug-io
*)
1890 (princ prompt
*debug-io
*)
1891 (force-output *debug-io
*))
1894 (defun read-if-available (default)
1895 (if (sb!int
:listen-skip-whitespace
*debug-io
*)