Declare COERCE and two helpers as EXPLICIT-CHECK.
[sbcl.git] / src / code / debug.lisp
blobcd89d66ecbabdc0b5be3e3a1d5639df85291c78d
1 ;;;; the debugger
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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
30 ;;; #
31 ;;; to satisfy lambda list
32 ;;; #:
33 ;;; exactly 2 expected, but 5 found
34 (defvar *debug-print-variable-alist* nil
35 #!+sb-doc
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
54 nil
55 #!+sb-doc
56 "*READTABLE* for the debugger")
58 (defvar *in-the-debugger* nil
59 #!+sb-doc
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
80 #!+sb-doc
81 "Should the debugger display beginner-oriented help messages?")
83 (defun debug-prompt (stream)
84 (sb!thread::get-foreground)
85 (format stream
86 "~%~W~:[~;[~W~]] "
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
103 to debugger.
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
112 that restart.
114 Changing frames:
115 UP up frame DOWN down frame
116 BOTTOM bottom frame FRAME n frame n (n=0 for top frame)
118 Inspecting frames:
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.
124 Stepping:
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:
135 (SB-DEBUG:ARG n)
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.
140 Other commands:
141 RETURN expr
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.
146 RESTART-FRAME
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.
151 SLURP
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 ()
159 ,@body))
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)
169 (return loc))))
170 (cond ((and (not (sb!di:debug-block-elsewhere-p block))
171 start)
172 (format *debug-io* "~%unknown location: using block start~%")
173 start)
175 loc)))
176 loc))
178 ;;;; BACKTRACE
180 (declaim (unsigned-byte *backtrace-frame-count*))
181 (defvar *backtrace-frame-count* 1000
182 #!+sb-doc
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
187 #!+sb-doc
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*
217 :value nil)
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 ()
230 (let ((frame here))
231 ;; Our caller's caller.
232 (loop repeat 2
233 do (setf frame (or (sb!di:frame-down frame) frame)))
234 frame))
235 (interrupted-frame ()
236 (or (nth-value 1 (find-interrupted-name-and-frame))
237 (current-frame))))
238 (cond ((eq :current-frame frame-designator)
239 (current-frame))
240 ((eq :interrupted-frame frame-designator)
241 (interrupted-frame))
242 ((eq :debugger-frame frame-designator)
243 (if (and *in-the-debugger* *current-frame*)
244 *current-frame*
245 (interrupted-frame)))
246 ((sb!di:frame-p frame-designator)
247 frame-designator)
249 (error "Invalid designator for initial backtrace frame: ~S"
250 frame-designator))))))
252 (defun map-backtrace (function &key
253 (start 0)
254 (from :debugger-frame)
255 (count *backtrace-frame-count*))
256 #!+sb-doc
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
268 is :DEBUGGER-FRAME.
270 :CURRENT-FRAME
271 specifies the caller of MAP-BACKTRACE.
273 :INTERRUPTED-FRAME
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.
278 :DEBUGGER-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
283 for index upfrom 0
284 for frame = (backtrace-start-frame from)
285 then (sb!di:frame-down frame)
286 until (null frame)
287 when (<= start index) do
288 (if (minusp (decf count))
289 (return result)
290 (setf result (funcall function frame)))
291 finally (return result)))
293 (defun print-backtrace (&key
294 (stream *debug-io*)
295 (start 0)
296 (from :debugger-frame)
297 (count *backtrace-frame-count*)
298 (print-thread t)
299 (print-frame-source nil)
300 (method-frame-style *method-frame-style*))
301 #!+sb-doc
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
312 is :DEBUGGER-FRAME.
314 :CURRENT-FRAME
315 specifies the caller of PRINT-BACKTRACE.
317 :INTERRUPTED-FRAME
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.
322 :DEBUGGER-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
338 information."
339 (with-debug-io-syntax ()
340 (fresh-line stream)
341 (when print-thread
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*
345 'serious-condition))
346 (*print-circle* t)
347 (n start))
348 (handler-bind ((print-not-readable #'print-unreadably))
349 (map-backtrace (lambda (frame)
350 (print-frame-call frame stream
351 :number n
352 :method-frame-style method-frame-style
353 :print-frame-source print-frame-source)
354 (incf n))
355 :from (backtrace-start-frame from)
356 :start start
357 :count count)))
358 (fresh-line stream)
359 (values)))
361 (defun list-backtrace (&key
362 (count *backtrace-frame-count*)
363 (start 0)
364 (from :debugger-frame)
365 (method-frame-style *method-frame-style*))
366 #!+sb-doc
367 "Returns a list describing the call stack. Each frame is represented
368 by a sublist:
370 \(<name> ...args...)
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
377 extent.
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
387 is :DEBUGGER-FRAME.
389 :CURRENT-FRAME
390 specifies the caller of LIST-BACKTRACE.
392 :INTERRUPTED-FRAME
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.
397 :DEBUGGER-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
404 information."
405 (let (rbacktrace)
406 (map-backtrace
407 (lambda (frame)
408 (push (frame-call-as-list frame :method-frame-style method-frame-style)
409 rbacktrace))
410 :count count
411 :start start
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
424 (handler-case
425 (format nil "dynamic-extent: ~S" obj)
426 (error ()
427 "error printing dynamic-extent object")))
428 obj))
430 (defun stack-allocated-p (obj)
431 #!+sb-doc
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)
437 t))))
439 ;;;; frame printing
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
446 &key
447 required
448 optional
449 rest
450 keyword
451 more
452 deleted)
453 `(etypecase ,element
454 (sb!di:debug-var
455 ,@required)
456 (cons
457 (ecase (car ,element)
458 (:optional ,@optional)
459 (:rest ,@rest)
460 (:keyword ,@keyword)
461 (:more ,@more)))
462 (symbol
463 (aver (eq ,element :deleted))
464 ,@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)
471 ,valid)
472 (t ,other)))))
474 ) ; EVAL-WHEN
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)))
491 (if (and (>= n 0)
492 (< n arg-count))
493 (sb!di::sub-access-debug-var-slot
494 pointer
495 (sb!c:standard-arg-location-sc n)
496 escaped)
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
507 pointer
508 (sb!c:standard-arg-location-sc i)
509 escaped))))
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)))
516 (handler-case
517 (let ((location (sb!di:frame-code-location frame))
518 (reversed-result nil))
519 (block enumerating
520 (map-frame-args
521 (lambda (element)
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)
525 reversed-result))
526 :keyword ((push (second element) reversed-result)
527 (push (frame-call-arg (third element) location frame)
528 reversed-result))
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)))
533 (if (listp rest)
534 (setf reversed-result (append (reverse rest) reversed-result))
535 (push (make-unprintable-object "unavailable &REST argument")
536 reversed-result))
537 (return-from enumerating))
538 (push (make-unprintable-object
539 "unavailable &REST argument")
540 reversed-result)))
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
546 (append (reverse
547 (multiple-value-list
548 (sb!c::%more-arg-values context 0 count)))
549 reversed-result))
550 (return-from enumerating))
551 (push (make-unprintable-object "unavailable &MORE argument")
552 reversed-result)))))
553 frame))
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
561 (if (consp args)
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
568 ;; understand.
569 (loop repeat count
570 for arg = (if real-args
571 (pop real-args)
572 (make-unprintable-object "unknown"))
573 collect arg)
574 real-args))
575 args)
576 ;; Clip arg-count.
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)))
581 (rest args)
582 args)
583 (if (eq (car name) 'sb!c::tl-xep)
584 (cons :tl info)
585 info)))
587 (defun clean-&more-processor (name args info)
588 (values (second name)
589 (if (consp args)
590 (let* ((more (last args 2))
591 (context (first more))
592 (count (second more)))
593 (append
594 (butlast args 2)
595 (if (fixnump count)
596 (multiple-value-list
597 (sb!c:%more-arg-values context 0 count))
598 (list
599 (make-unprintable-object "more unavailable arguments")))))
600 args)
601 (cons :more info)))
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)))
609 (values name 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)
613 (fboundp gf-name)
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)))
623 (if (consp name)
624 (case (first name)
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
631 info))
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)
640 #!+sb-doc
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
651 information.
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))
661 method-frame-style
662 (when kind (list kind)))
663 (let ((args (if (and (consp args) replace-dynamic-extent-objects)
664 (mapcar #'replace-dynamic-extent-object args)
665 args)))
666 (values name args info)))))
668 (defun ensure-printable-object (object)
669 (handler-case
670 (with-open-stream (out (make-broadcast-stream))
671 (prin1 object out)
672 object)
673 (error (cond)
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
687 ;;; values.
688 (defun print-frame-call (frame stream
689 &key print-frame-source
690 number
691 (method-frame-style *method-frame-style*))
692 (when number
693 (format stream "~&~S: " (if (integerp number)
694 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)
700 (*print-circle* t))
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
704 ;; *PRINT-LEVEL*.
705 (let ((*print-length* nil)
706 (*print-level* 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
714 ;; values.
715 (let ((args (ensure-printable-object args)))
716 (if (listp args)
717 (format stream "~{ ~_~S~}" args)
718 (format stream " ~S" args)))))
719 (when info
720 (format stream " [~{~(~A~)~^,~}]" info)))
721 (when print-frame-source
722 (let ((loc (sb!di:frame-code-location frame)))
723 (handler-case
724 (let ((source (handler-case
725 (code-location-source-form loc 0)
726 (error (c)
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")))
733 (error (c)
734 (format stream "~& error printing frame source: ~A" c))))))
736 ;;;; INVOKE-DEBUGGER
738 (defvar *debugger-hook* nil
739 #!+sb-doc
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
747 #!+sb-doc
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
756 called by BREAK.")
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
776 (with-sane-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
791 ;; rebindings here.
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
811 ;; naturally.
812 (sb!impl::*circularity-hash-table* . nil)
813 (sb!impl::*circularity-counter* . nil)
814 (*readtable* *debug-readtable*))
815 (progv
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)))
833 (when old-hook
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))
842 (cond
843 ;; No hint, just keep the debugger guts out.
844 ((not hint)
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.
851 ((symbolp hint)
852 (find-caller-of-named-frame hint))
853 ;; We already have a resolved hint.
855 hint))))
857 (defun invoke-debugger (condition)
858 #!+sb-doc
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 ~
877 reset to ~S."
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)
895 (format stream
896 "debugger invoked on a ~S~@[ in thread ~_~A~]: ~2I~_~A"
897 (type-of condition)
898 #!+sb-thread sb!thread:*current-thread*
899 #!-sb-thread nil
900 condition))
901 (terpri stream))
903 (defun %invoke-debugger (condition)
904 (let ((*debug-condition* condition)
905 (*debug-restarts* (compute-restarts condition))
906 (*nested-debug-condition* nil))
907 (handler-case
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*))
915 (error (condition)
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.)~@:>~%"
922 ndc-type
923 '*debug-condition*
924 ndc-type
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
934 *debug-io*)))
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)
944 (unwind-protect
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*
959 (format *debug-io*
960 "~%~@<Type HELP for debugger help, or ~
961 (SB-EXT:EXIT) to exit from SBCL.~:@>~2%"))
962 (show-restarts *debug-restarts* *debug-io*))
963 (internal-debug))
964 (when background-p
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.")))
982 (labels
983 ((failure-quit (&key abort)
984 (/show0 "in FAILURE-QUIT (in --disable-debugger debugger hook)")
985 (exit :code 1 :abort abort))
986 (display-condition ()
987 (handler-case
988 (handler-case
989 (print-condition)
990 (condition ()
991 ;; printing failed, try to describe it
992 (describe-condition)))
993 (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))))
997 (print-condition ()
998 (format *error-output*
999 "~&~@<Unhandled ~S~@[ in thread ~S~]: ~2I~_~A~:>~2%"
1000 (type-of condition)
1001 #!+sb-thread sb!thread:*current-thread*
1002 #!-sb-thread nil
1003 condition)
1004 (finish-output *error-output*))
1005 (describe-condition ()
1006 (format *error-output*
1007 "~&Unhandled ~S~@[ in thread ~S~]:~%"
1008 (type-of condition)
1009 #!+sb-thread sb!thread:*current-thread*
1010 #!-sb-thread nil)
1011 (describe condition *error-output*)
1012 (finish-output *error-output*))
1013 (display-backtrace ()
1014 (handler-case
1015 (print-backtrace :stream *error-output*
1016 :from :interrupted-frame
1017 :print-thread t)
1018 (condition ()
1019 (values)))
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
1029 ;; information.
1030 (handler-case
1031 (progn
1032 (display-condition)
1033 (display-backtrace)
1034 (format *error-output*
1035 "~%unhandled condition in --disable-debugger mode, quitting~%")
1036 (finish-output *error-output*)
1037 (failure-quit))
1038 (condition ()
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
1055 (ignore-errors
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 ()
1064 #!+sb-doc
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 ()
1081 #!+sb-doc
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)
1091 (format s
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):~%")
1097 (let ((count 0)
1098 (names-used '(nil))
1099 (max-name-len 0))
1100 (dolist (restart restarts)
1101 (let ((name (restart-name restart)))
1102 (when name
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))))
1120 (incf count))))))
1122 (defvar *debug-loop-fun* #'debug-loop-fun
1123 #!+sb-doc
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
1128 ;;; reasons.
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
1136 ;;; errors.
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*))))
1145 ;;;; DEBUG-LOOP
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
1150 #!+sb-doc
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)
1160 form)))
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
1169 (lambda (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))
1176 (terpri *debug-io*)
1177 (print-frame-call *current-frame* *debug-io* :print-frame-source t)))
1178 (loop
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*)
1184 (format *debug-io*
1185 "~&error flushed (because ~
1186 ~S is set)"
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).~@:>"
1202 level)
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))
1207 ((consp cmd-fun)
1208 (format *debug-io*
1209 "~&Your command, ~S, is ambiguous:~%"
1210 exp)
1211 (dolist (ele cmd-fun)
1212 (format *debug-io* " ~A~%" ele)))
1214 (funcall cmd-fun))))))))))))
1216 (defvar *auto-eval-in-frame* t
1217 #!+sb-doc
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*))
1224 (eval expr))
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.~%")
1230 (eval expr))))
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*)
1260 name))
1261 (simple-string (sb!di:ambiguous-debug-vars
1262 (sb!di:frame-debug-fun *current-frame*)
1263 name))))
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))
1268 temp)))
1269 (declare (list vars))
1270 (cond ((null vars)
1271 (error "No known valid variables match ~S." name))
1272 ((= (length vars) 1)
1273 ,(ecase ref-or-set
1274 (:ref
1275 '(sb!di:debug-var-value (car vars) *current-frame*))
1276 (:set
1277 `(setf (sb!di:debug-var-value (car vars) *current-frame*)
1278 ,value-var))))
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/=
1286 ;; instead.
1287 (exact (remove-if-not (lambda (v)
1288 (string= (sb!di:debug-var-symbol-name v)
1289 name))
1290 vars))
1291 (vars (or exact vars)))
1292 (declare (simple-string name)
1293 (list exact vars))
1294 (cond
1295 ;; Check now for only having one variable.
1296 ((= (length vars) 1)
1297 ,(ecase ref-or-set
1298 (:ref
1299 '(sb!di:debug-var-value (car vars) *current-frame*))
1300 (:set
1301 `(setf (sb!di:debug-var-value (car vars) *current-frame*)
1302 ,value-var))))
1303 ;; If there weren't any exact matches, flame about
1304 ;; ambiguity unless all the variables have the same
1305 ;; name.
1306 ((and (not exact)
1307 (find-if-not
1308 (lambda (v)
1309 (string= (sb!di:debug-var-symbol-name v)
1310 (sb!di:debug-var-symbol-name (car vars))))
1311 (cdr vars)))
1312 (error "specification ambiguous:~%~{ ~A~%~}"
1313 (mapcar #'sb!di:debug-var-symbol-name
1314 (delete-duplicates
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.
1319 (id-supplied
1320 (let ((v (find id vars :key #'sb!di:debug-var-id)))
1321 (unless v
1322 (error
1323 "invalid variable ID, ~W: should have been one of ~S"
1325 (mapcar #'sb!di:debug-var-id vars)))
1326 ,(ecase ref-or-set
1327 (:ref
1328 '(sb!di:debug-var-value v *current-frame*))
1329 (:set
1330 `(setf (sb!di:debug-var-value v *current-frame*)
1331 ,value-var)))))
1333 (error "Specify variable ID to disambiguate ~S. Use one of ~S."
1334 name
1335 (mapcar #'sb!di:debug-var-id vars)))))))))
1337 ) ; EVAL-WHEN
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))
1342 #!+sb-doc
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
1354 id values.
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)
1373 (let ((n count))
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)))
1381 ((zerop (decf n))
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
1386 *current-frame*)
1387 (error "unused &REST argument before n'th argument")
1388 (dolist (value
1389 (sb!di:debug-var-value var *current-frame*)
1390 (error
1391 "The argument specification ~S is out of range."
1393 (if (zerop n)
1394 (return-from nth-arg (values value nil))
1395 (decf n)))
1396 (error "invalid &REST argument before n'th argument")))))
1397 (decf n))))
1399 (defun arg (n)
1400 #!+sb-doc
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*)
1406 (return-from arg
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."))))
1413 (if lambda-var-p
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"))
1418 var)))
1420 ;;;; machinery for definition of debug loop commands
1422 (defvar *debug-commands* nil)
1424 ;;; Interface to *DEBUG-COMMANDS*. No required arguments in args are
1425 ;;; permitted.
1426 (defmacro !def-debug-command (name args &rest body)
1427 (let ((fun-name (symbolicate name "-DEBUG-COMMAND")))
1428 `(progn
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"))
1434 ,@body)
1435 (push (cons ,name #',fun-name) *debug-commands*)
1436 ',fun-name)))
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*))
1442 new-name)
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))
1453 (let* ((name
1454 (if (symbolp form)
1455 (symbol-name form)
1456 (format nil "~W" form)))
1457 (len (length name))
1458 (res nil))
1459 (declare (simple-string name)
1460 (fixnum len)
1461 (list res))
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)
1468 (fixnum str-len))
1469 (cond ((< str-len len))
1470 ((= 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)
1474 (push ele res))))))
1475 (mapc #'match-command *debug-commands*)
1476 (mapc #'match-command other-commands))
1478 ;; Return the right value.
1479 (cond ((not res) nil)
1480 ((= (length res) 1)
1481 (cdar res))
1482 (t ; Just return the names.
1483 (do ((cmds res (cdr cmds)))
1484 ((not cmds) res)
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*))
1494 (let ((commands)
1495 (num 0)) ; better be the same as show-restarts!
1496 (dolist (restart restarts)
1497 (let ((name (string (restart-name restart))))
1498 (let ((restart-fun
1499 (lambda ()
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))))
1506 (incf num))
1507 commands))
1509 ;;;; frame-changing commands
1511 (!def-debug-command "UP" ()
1512 (let ((next (sb!di:frame-up *current-frame*)))
1513 (cond (next
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*)))
1521 (cond (next
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)))
1532 ((null 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))
1547 frame)
1548 (let ((next-frame (funcall next-frame-fun frame)))
1549 (cond (next-frame
1550 (setf frame next-frame))
1552 (format *debug-io*
1553 "The ~A of the stack was encountered.~%"
1554 limit-string)
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))
1565 ;;; make T safe
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
1577 (unsigned-byte
1578 (nth num *debug-restarts*))
1579 (symbol
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.~%"
1586 num)
1587 (return-from restart-debug-command nil)))))
1588 (/show0 "got RESTART")
1589 (if 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.
1601 (format *debug-io*
1602 "~&~A~2%(The HELP string is stored in ~S.)~%"
1603 *debug-help-string*
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))
1626 (any-p nil)
1627 (any-valid-p nil)
1628 (more-context nil)
1629 (more-count nil))
1630 (dolist (v (sb!di:ambiguous-debug-vars
1631 d-fun
1632 (if prefix (string prefix) "")))
1633 (setf any-p t)
1634 (when (var-valid-in-frame-p v location)
1635 (setf any-valid-p t)
1636 (case (sb!di::debug-var-info v)
1637 (:more-context
1638 (setf more-context (sb!di:debug-var-value v *current-frame*)))
1639 (:more-count
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~%"
1648 'more
1649 (multiple-value-list (sb!c:%more-arg-values more-context 0 more-count))))
1650 (cond
1651 ((not any-p)
1652 (format *debug-io*
1653 "There are no local variables ~@[starting with ~A ~]~
1654 in the function."
1655 prefix))
1656 ((not any-valid-p)
1657 (format *debug-io*
1658 "All variables ~@[starting with ~A ~]currently ~
1659 have invalid values."
1660 prefix))))
1661 (write-line "There is no variable information available."
1662 *debug-io*))))
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))
1669 *debug-io*))
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)
1681 context))
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*)))
1693 (cond (restart
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*)))
1703 (aver restart)
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*)))
1718 (aver restart)
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)))
1729 (if function
1730 (describe function)
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)
1745 (sap-int sap))
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
1751 ;; unwind.
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)
1759 (lambda ()
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
1765 unbind-to)
1766 (setf sb!vm::*current-catch-block* block)
1767 (funcall thunk)))))
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
1772 tag)
1773 (throw tag thunk)))
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))
1808 return block)))
1809 enclosing-block))
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))
1826 return uwp-block)))
1827 enclosing-uwp))
1829 (!def-debug-command "RETURN" (&optional
1830 (return (read-prompting-maybe
1831 "return: ")))
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)
1836 *current-frame*))))
1837 (unwind-to-frame-and-call *current-frame* (lambda ()
1838 (values-list values))))
1839 (format *debug-io*
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*)
1852 nil))
1853 (when (and fun
1854 (or ok
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*
1861 (lambda ()
1862 ;; Ensure TCO.
1863 (declare (optimize (debug 0)))
1864 (apply fun arglist))))
1865 (format *debug-io*
1866 "Can't restart ~S: no function for frame."
1867 *current-frame*)))
1868 (format *debug-io*
1869 "~@<Can't restart ~S: tag not found. ~
1870 ~2I~_(hint: try increasing the DEBUG optimization quality ~
1871 and recompiling)~:@>"
1872 *current-frame*)))
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*))
1892 (read *debug-io*))
1894 (defun read-if-available (default)
1895 (if (sb!int:listen-skip-whitespace *debug-io*)
1896 (read *debug-io*)
1897 default))