Remove some test noise. A drop in the ocean unfortunately.
[sbcl.git] / src / code / debug.lisp
blob1befb884d1eda65fb10755f099a8305115483986
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 (defun backtrace (&optional (count *backtrace-frame-count*) (stream *debug-io*))
220 #!+sb-doc
221 "Replaced by PRINT-BACKTRACE, will eventually be deprecated."
222 (print-backtrace :count count :stream stream))
224 (defun backtrace-as-list (&optional (count *backtrace-frame-count*))
225 #!+sb-doc
226 "Replaced by LIST-BACKTRACE, will eventually be deprecated."
227 (list-backtrace :count count))
229 (defun backtrace-start-frame (frame-designator)
230 (let ((here (sb!di:top-frame)))
231 (labels ((current-frame ()
232 (let ((frame here))
233 ;; Our caller's caller.
234 (loop repeat 2
235 do (setf frame (or (sb!di:frame-down frame) frame)))
236 frame))
237 (interrupted-frame ()
238 (or (nth-value 1 (find-interrupted-name-and-frame))
239 (current-frame))))
240 (cond ((eq :current-frame frame-designator)
241 (current-frame))
242 ((eq :interrupted-frame frame-designator)
243 (interrupted-frame))
244 ((eq :debugger-frame frame-designator)
245 (if (and *in-the-debugger* *current-frame*)
246 *current-frame*
247 (interrupted-frame)))
248 ((sb!di:frame-p frame-designator)
249 frame-designator)
251 (error "Invalid designator for initial backtrace frame: ~S"
252 frame-designator))))))
254 (defun map-backtrace (function &key
255 (start 0)
256 (from :debugger-frame)
257 (count *backtrace-frame-count*))
258 #!+sb-doc
259 "Calls the designated FUNCTION with each frame on the call stack.
260 Returns the last value returned by FUNCTION.
262 COUNT is the number of frames to backtrace, defaulting to
263 *BACKTRACE-FRAME-COUNT*.
265 START is the number of the frame the backtrace should start from.
267 FROM specifies the frame relative to which the frames are numbered. Possible
268 values are an explicit SB-DI:FRAME object, and the
269 keywords :CURRENT-FRAME, :INTERRUPTED-FRAME, and :DEBUGGER-FRAME. Default
270 is :DEBUGGER-FRAME.
272 :CURRENT-FRAME
273 specifies the caller of MAP-BACKTRACE.
275 :INTERRUPTED-FRAME
276 specifies the first interrupted frame on the stack \(typically the frame
277 where the error occurred, as opposed to error handling frames) if any,
278 otherwise behaving as :CURRENT-FRAME.
280 :DEBUGGER-FRAME
281 specifies the currently debugged frame when inside the debugger, and
282 behaves as :INTERRUPTED-FRAME outside the debugger.
284 (loop with result = nil
285 for index upfrom 0
286 for frame = (backtrace-start-frame from)
287 then (sb!di:frame-down frame)
288 until (null frame)
289 when (<= start index) do
290 (if (minusp (decf count))
291 (return result)
292 (setf result (funcall function frame)))
293 finally (return result)))
295 (defun print-backtrace (&key
296 (stream *debug-io*)
297 (start 0)
298 (from :debugger-frame)
299 (count *backtrace-frame-count*)
300 (print-thread t)
301 (print-frame-source nil)
302 (method-frame-style *method-frame-style*))
303 #!+sb-doc
304 "Print a listing of the call stack to STREAM, defaulting to *DEBUG-IO*.
306 COUNT is the number of frames to backtrace, defaulting to
307 *BACKTRACE-FRAME-COUNT*.
309 START is the number of the frame the backtrace should start from.
311 FROM specifies the frame relative to which the frames are numbered. Possible
312 values are an explicit SB-DI:FRAME object, and the
313 keywords :CURRENT-FRAME, :INTERRUPTED-FRAME, and :DEBUGGER-FRAME. Default
314 is :DEBUGGER-FRAME.
316 :CURRENT-FRAME
317 specifies the caller of PRINT-BACKTRACE.
319 :INTERRUPTED-FRAME
320 specifies the first interrupted frame on the stack \(typically the frame
321 where the error occured, as opposed to error handling frames) if any,
322 otherwise behaving as :CURRENT-FRAME.
324 :DEBUGGER-FRAME
325 specifies the currently debugged frame when inside the debugger, and
326 behaves as :INTERRUPTED-FRAME outside the debugger.
328 If PRINT-THREAD is true (default), backtrace is preceded by printing the
329 thread object the backtrace is from.
331 If PRINT-FRAME-SOURCE is true (default is false), each frame is followed by
332 printing the currently executing source form in the function responsible for
333 that frame, when available. Requires the function to have been compiled at
334 DEBUG 2 or higher. If PRINT-FRAME-SOURCE is :ALWAYS, it also reports \"no
335 source available\" for frames for which were compiled at lower debug settings.
337 METHOD-FRAME-STYLE (defaulting to *METHOD-FRAME-STYLE*), determines how frames
338 corresponding to method functions are printed. Possible values
339 are :MINIMAL, :NORMAL, and :FULL. See *METHOD-FRAME-STYLE* for more
340 information."
341 (with-debug-io-syntax ()
342 (fresh-line stream)
343 (when print-thread
344 (format stream "Backtrace for: ~S~%" sb!thread:*current-thread*))
345 (let ((*suppress-print-errors* (if (subtypep 'serious-condition *suppress-print-errors*)
346 *suppress-print-errors*
347 'serious-condition))
348 (*print-circle* t)
349 (n start))
350 (handler-bind ((print-not-readable #'print-unreadably))
351 (map-backtrace (lambda (frame)
352 (print-frame-call frame stream
353 :number n
354 :method-frame-style method-frame-style
355 :print-frame-source print-frame-source)
356 (incf n))
357 :from (backtrace-start-frame from)
358 :start start
359 :count count)))
360 (fresh-line stream)
361 (values)))
363 (defun list-backtrace (&key
364 (count *backtrace-frame-count*)
365 (start 0)
366 (from :debugger-frame)
367 (method-frame-style *method-frame-style*))
368 #!+sb-doc
369 "Returns a list describing the call stack. Each frame is represented
370 by a sublist:
372 \(<name> ...args...)
374 where the name describes the function responsible for the frame. The name
375 might not be bound to the actual function object. Unavailable arguments are
376 represented by dummy objects that print as #<unavailable argument>. Objects
377 with dynamic-extent allocation by the current thread are represented by
378 substitutes to avoid references to them from leaking outside their legal
379 extent.
381 COUNT is the number of frames to backtrace, defaulting to
382 *BACKTRACE-FRAME-COUNT*.
384 START is the number of the frame the backtrace should start from.
386 FROM specifies the frame relative to which the frames are numbered. Possible
387 values are an explicit SB-DI:FRAME object, and the
388 keywords :CURRENT-FRAME, :INTERRUPTED-FRAME, and :DEBUGGER-FRAME. Default
389 is :DEBUGGER-FRAME.
391 :CURRENT-FRAME
392 specifies the caller of LIST-BACKTRACE.
394 :INTERRUPTED-FRAME
395 specifies the first interrupted frame on the stack \(typically the frame
396 where the error occured, as opposed to error handling frames) if any,
397 otherwise behaving as :CURRENT-FRAME.
399 :DEBUGGER-FRAME
400 specifies the currently debugged frame when inside the debugger, and
401 behaves as :INTERRUPTED-FRAME outside the debugger.
403 METHOD-FRAME-STYLE (defaulting to *METHOD-FRAME-STYLE*), determines how frames
404 corresponding to method functions are printed. Possible values
405 are :MINIMAL, :NORMAL, and :FULL. See *METHOD-FRAME-STYLE* for more
406 information."
407 (let (rbacktrace)
408 (map-backtrace
409 (lambda (frame)
410 (push (frame-call-as-list frame :method-frame-style method-frame-style)
411 rbacktrace))
412 :count count
413 :start start
414 :from (backtrace-start-frame from))
415 (nreverse rbacktrace)))
417 (defun frame-call-as-list (frame &key (method-frame-style *method-frame-style*))
418 (multiple-value-bind (name args info)
419 (frame-call frame :method-frame-style method-frame-style
420 :replace-dynamic-extent-objects t)
421 (values (cons name args) info)))
423 (defun replace-dynamic-extent-object (obj)
424 (if (stack-allocated-p obj)
425 (make-unprintable-object
426 (handler-case
427 (format nil "dynamic-extent: ~S" obj)
428 (error ()
429 "error printing dynamic-extent object")))
430 obj))
432 (defun stack-allocated-p (obj)
433 #!+sb-doc
434 "Returns T if OBJ is allocated on the stack of the current
435 thread, NIL otherwise."
436 (with-pinned-objects (obj)
437 (let ((sap (int-sap (get-lisp-obj-address obj))))
438 (when (sb!vm:control-stack-pointer-valid-p sap nil)
439 t))))
441 ;;;; frame printing
443 (eval-when (:compile-toplevel :execute)
445 ;;; This is a convenient way to express what to do for each type of
446 ;;; lambda-list element.
447 (sb!xc:defmacro lambda-list-element-dispatch (element
448 &key
449 required
450 optional
451 rest
452 keyword
453 more
454 deleted)
455 `(etypecase ,element
456 (sb!di:debug-var
457 ,@required)
458 (cons
459 (ecase (car ,element)
460 (:optional ,@optional)
461 (:rest ,@rest)
462 (:keyword ,@keyword)
463 (:more ,@more)))
464 (symbol
465 (aver (eq ,element :deleted))
466 ,@deleted)))
468 (sb!xc:defmacro lambda-var-dispatch (variable location deleted valid other)
469 (let ((var (gensym)))
470 `(let ((,var ,variable))
471 (cond ((eq ,var :deleted) ,deleted)
472 ((eq (sb!di:debug-var-validity ,var ,location) :valid)
473 ,valid)
474 (t ,other)))))
476 ) ; EVAL-WHEN
478 ;;; Extract the function argument values for a debug frame.
479 (defun map-frame-args (thunk frame)
480 (let ((debug-fun (sb!di:frame-debug-fun frame)))
481 (dolist (element (sb!di:debug-fun-lambda-list debug-fun))
482 (funcall thunk element))))
484 ;;; Since arg-count checking happens before any of the stack locations
485 ;;; and registers are overwritten all the arguments, including the
486 ;;; extra ones, can be precisely recovered.
487 #!+precise-arg-count-error
488 (defun arg-count-error-frame-nth-arg (n frame)
489 (let* ((escaped (sb!di::compiled-frame-escaped frame))
490 (pointer (sb!di::frame-pointer frame))
491 (arg-count (sb!di::sub-access-debug-var-slot
492 pointer sb!c:arg-count-sc escaped)))
493 (if (and (>= n 0)
494 (< n arg-count))
495 (sb!di::sub-access-debug-var-slot
496 pointer
497 (sb!c:standard-arg-location-sc n)
498 escaped)
499 (error "Index ~a out of bounds for ~a supplied argument~:p." n arg-count))))
501 #!+precise-arg-count-error
502 (defun arg-count-error-frame-args (frame)
503 (let* ((escaped (sb!di::compiled-frame-escaped frame))
504 (pointer (sb!di::frame-pointer frame))
505 (arg-count (sb!di::sub-access-debug-var-slot
506 pointer sb!c:arg-count-sc escaped)))
507 (loop for i below arg-count
508 collect (sb!di::sub-access-debug-var-slot
509 pointer
510 (sb!c:standard-arg-location-sc i)
511 escaped))))
513 (defun frame-args-as-list (frame)
514 #!+precise-arg-count-error
515 (when (sb!di::tl-invalid-arg-count-error-p frame)
516 (return-from frame-args-as-list
517 (arg-count-error-frame-args frame)))
518 (handler-case
519 (let ((location (sb!di:frame-code-location frame))
520 (reversed-result nil))
521 (block enumerating
522 (map-frame-args
523 (lambda (element)
524 (lambda-list-element-dispatch element
525 :required ((push (frame-call-arg element location frame) reversed-result))
526 :optional ((push (frame-call-arg (second element) location frame)
527 reversed-result))
528 :keyword ((push (second element) reversed-result)
529 (push (frame-call-arg (third element) location frame)
530 reversed-result))
531 :deleted ((push (frame-call-arg element location frame) reversed-result))
532 :rest ((lambda-var-dispatch (second element) location
534 (let ((rest (sb!di:debug-var-value (second element) frame)))
535 (if (listp rest)
536 (setf reversed-result (append (reverse rest) reversed-result))
537 (push (make-unprintable-object "unavailable &REST argument")
538 reversed-result))
539 (return-from enumerating))
540 (push (make-unprintable-object
541 "unavailable &REST argument")
542 reversed-result)))
543 :more ((lambda-var-dispatch (second element) location
545 (let ((context (sb!di:debug-var-value (second element) frame))
546 (count (sb!di:debug-var-value (third element) frame)))
547 (setf reversed-result
548 (append (reverse
549 (multiple-value-list
550 (sb!c::%more-arg-values context 0 count)))
551 reversed-result))
552 (return-from enumerating))
553 (push (make-unprintable-object "unavailable &MORE argument")
554 reversed-result)))))
555 frame))
556 (nreverse reversed-result))
557 (sb!di:lambda-list-unavailable ()
558 (make-unprintable-object "unavailable lambda list"))))
560 (defun clean-xep (frame name args info)
561 (values (second name)
562 #!-precise-arg-count-error
563 (if (consp args)
564 (let* ((count (first args))
565 (real-args (rest args)))
566 (if (and (integerp count)
567 (sb!di::tl-invalid-arg-count-error-p frame))
568 ;; So, this is a cheap trick -- but makes backtraces for
569 ;; too-many-arguments-errors much, much easier to to
570 ;; understand.
571 (loop repeat count
572 for arg = (if real-args
573 (pop real-args)
574 (make-unprintable-object "unknown"))
575 collect arg)
576 real-args))
577 args)
578 ;; Clip arg-count.
579 #!+precise-arg-count-error
580 (if (and (consp args)
581 ;; ARG-COUNT-ERROR-FRAME-ARGS doesn't include arg-count
582 (not (sb!di::tl-invalid-arg-count-error-p frame)))
583 (rest args)
584 args)
585 (if (eq (car name) 'sb!c::tl-xep)
586 (cons :tl info)
587 info)))
589 (defun clean-&more-processor (name args info)
590 (values (second name)
591 (if (consp args)
592 (let* ((more (last args 2))
593 (context (first more))
594 (count (second more)))
595 (append
596 (butlast args 2)
597 (if (fixnump count)
598 (multiple-value-list
599 (sb!c:%more-arg-values context 0 count))
600 (list
601 (make-unprintable-object "more unavailable arguments")))))
602 args)
603 (cons :more info)))
605 (defun clean-fast-method (name args style info)
606 (declare (type (member :minimal :normal :full) style))
607 (multiple-value-bind (cname cargs)
608 ;; Make no attempt to simplify the display if ARGS could not be found
609 ;; due to low (OPTIMIZE (DEBUG)) quality in the method.
610 (if (or (eq style :full) (not (listp args)))
611 (values name args)
612 (let ((gf-name (second name))
613 (real-args (the list (cddr args)))) ; strip .PV. and .N-M-CALL.
614 (if (and (eq style :minimal)
615 (fboundp gf-name)
616 (notany #'sb!impl::unprintable-object-p real-args)
617 (singleton-p (compute-applicable-methods
618 (fdefinition gf-name) real-args)))
619 (values gf-name real-args)
620 (values (cons :method (cdr name)) real-args))))
621 (values cname cargs (cons :fast-method info))))
623 (defun clean-frame-call (frame name method-frame-style info)
624 (let ((args (frame-args-as-list frame)))
625 (if (consp name)
626 (case (first name)
627 ((sb!c::xep sb!c::tl-xep)
628 (clean-xep frame name args info))
629 ((sb!c::&more-processor)
630 (clean-&more-processor name args info))
631 ((sb!c::&optional-processor)
632 (clean-frame-call frame (second name) method-frame-style
633 info))
634 ((sb!pcl::fast-method)
635 (clean-fast-method name args method-frame-style info))
637 (values name args info)))
638 (values name args info))))
640 (defun frame-call (frame &key (method-frame-style *method-frame-style*)
641 replace-dynamic-extent-objects)
642 #!+sb-doc
643 "Returns as multiple values a descriptive name for the function responsible
644 for FRAME, arguments that that function, and a list providing additional
645 information about the frame.
647 Unavailable arguments are represented using dummy-objects printing as
648 #<unavailable argument>.
650 METHOD-FRAME-STYLE (defaulting to *METHOD-FRAME-STYLE*), determines how frames
651 corresponding to method functions are printed. Possible values
652 are :MINIMAL, :NORMAL, and :FULL. See *METHOD-FRAME-STYLE* for more
653 information.
655 If REPLACE-DYNAMIC-EXTENT-OBJECTS is true, objects allocated on the stack of
656 the current thread are replaced with dummy objects which can safely escape."
657 (let* ((debug-fun (sb!di:frame-debug-fun frame))
658 (kind (sb!di:debug-fun-kind debug-fun)))
659 (multiple-value-bind (name args info)
660 (clean-frame-call frame
661 (or (sb!di:debug-fun-closure-name debug-fun frame)
662 (sb!di:debug-fun-name debug-fun))
663 method-frame-style
664 (when kind (list kind)))
665 (let ((args (if (and (consp args) replace-dynamic-extent-objects)
666 (mapcar #'replace-dynamic-extent-object args)
667 args)))
668 (values name args info)))))
670 (defun ensure-printable-object (object)
671 (handler-case
672 (with-open-stream (out (make-broadcast-stream))
673 (prin1 object out)
674 object)
675 (error (cond)
676 (declare (ignore cond))
677 (make-unprintable-object "error printing object"))))
679 (defun frame-call-arg (var location frame)
680 (lambda-var-dispatch var location
681 (make-unprintable-object "unused argument")
682 (sb!di:debug-var-value var frame)
683 (make-unprintable-object "unavailable argument")))
685 ;;; Prints a representation of the function call causing FRAME to
686 ;;; exist. VERBOSITY indicates the level of information to output;
687 ;;; zero indicates just printing the DEBUG-FUN's name, and one
688 ;;; indicates displaying call-like, one-liner format with argument
689 ;;; values.
690 (defun print-frame-call (frame stream
691 &key print-frame-source
692 number
693 (method-frame-style *method-frame-style*))
694 (when number
695 (format stream "~&~S: " (if (integerp number)
696 number
697 (sb!di:frame-number frame))))
698 (multiple-value-bind (name args info)
699 (frame-call frame :method-frame-style method-frame-style)
700 (pprint-logical-block (stream nil :prefix "(" :suffix ")")
701 (let ((*print-pretty* nil)
702 (*print-circle* t))
703 ;; Since we go to some trouble to make nice informative
704 ;; function names like (PRINT-OBJECT :AROUND (CLOWN T)), let's
705 ;; make sure that they aren't truncated by *PRINT-LENGTH* and
706 ;; *PRINT-LEVEL*.
707 (let ((*print-length* nil)
708 (*print-level* nil)
709 (name (ensure-printable-object name)))
710 (write name :stream stream :escape t :pretty (equal '(lambda ()) name)))
712 ;; For the function arguments, we can just print normally. If
713 ;; we hit a &REST arg, then print as many of the values as
714 ;; possible, punting the loop over lambda-list variables since
715 ;; any other arguments will be in the &REST arg's list of
716 ;; values.
717 (let ((args (ensure-printable-object args)))
718 (if (listp args)
719 (format stream "~{ ~_~S~}" args)
720 (format stream " ~S" args)))))
721 (when info
722 (format stream " [~{~(~A~)~^,~}]" info)))
723 (when print-frame-source
724 (let ((loc (sb!di:frame-code-location frame)))
725 (handler-case
726 (let ((source (handler-case
727 (code-location-source-form loc 0)
728 (error (c)
729 (format stream "~& error finding frame source: ~A" c)))))
730 (format stream "~% source: ~S" source))
731 (sb!di:debug-condition ()
732 ;; This is mostly noise.
733 (when (eq :always print-frame-source)
734 (format stream "~& no source available for frame")))
735 (error (c)
736 (format stream "~& error printing frame source: ~A" c))))))
738 ;;;; INVOKE-DEBUGGER
740 (defvar *debugger-hook* nil
741 #!+sb-doc
742 "This is either NIL or a function of two arguments, a condition and the value
743 of *DEBUGGER-HOOK*. This function can either handle the condition or return
744 which causes the standard debugger to execute. The system passes the value
745 of this variable to the function because it binds *DEBUGGER-HOOK* to NIL
746 around the invocation.")
748 (defvar *invoke-debugger-hook* nil
749 #!+sb-doc
750 "This is either NIL or a designator for a function of two arguments,
751 to be run when the debugger is about to be entered. The function is
752 run with *INVOKE-DEBUGGER-HOOK* bound to NIL to minimize recursive
753 errors, and receives as arguments the condition that triggered
754 debugger entry and the previous value of *INVOKE-DEBUGGER-HOOK*
756 This mechanism is an SBCL extension similar to the standard *DEBUGGER-HOOK*.
757 In contrast to *DEBUGGER-HOOK*, it is observed by INVOKE-DEBUGGER even when
758 called by BREAK.")
760 ;;; These are bound on each invocation of INVOKE-DEBUGGER.
761 (defvar *debug-restarts*)
762 (defvar *debug-condition*)
763 (defvar *nested-debug-condition*)
765 ;;; Oh, what a tangled web we weave when we preserve backwards
766 ;;; compatibility with 1968-style use of global variables to control
767 ;;; per-stream i/o properties; there's really no way to get this
768 ;;; quite right, but we do what we can.
769 (defun funcall-with-debug-io-syntax (fun &rest rest)
770 (declare (type function fun))
771 ;; Try to force the other special variables into a useful state.
772 (let (;; Protect from WITH-STANDARD-IO-SYNTAX some variables where
773 ;; any default we might use is less useful than just reusing
774 ;; the global values.
775 (original-package *package*)
776 (original-print-pretty *print-pretty*))
777 (with-standard-io-syntax
778 (with-sane-io-syntax
779 (let (;; We want the printer and reader to be in a useful
780 ;; state, regardless of where the debugger was invoked
781 ;; in the program. WITH-STANDARD-IO-SYNTAX and
782 ;; WITH-SANE-IO-SYNTAX do much of what we want, but
783 ;; * It doesn't affect our internal special variables
784 ;; like *CURRENT-LEVEL-IN-PRINT*.
785 ;; * It isn't customizable.
786 ;; * It sets *PACKAGE* to COMMON-LISP-USER, which is not
787 ;; helpful behavior for a debugger.
788 ;; * There's no particularly good debugger default for
789 ;; *PRINT-PRETTY*, since T is usually what you want
790 ;; -- except absolutely not what you want when you're
791 ;; debugging failures in PRINT-OBJECT logic.
792 ;; We try to address all these issues with explicit
793 ;; rebindings here.
794 (*current-level-in-print* 0)
795 (*package* original-package)
796 (*print-pretty* original-print-pretty)
797 ;; Clear the circularity machinery to try to to reduce the
798 ;; pain from sharing the circularity table across all
799 ;; streams; if these are not rebound here, then setting
800 ;; *PRINT-CIRCLE* within the debugger when debugging in a
801 ;; state where something circular was being printed (e.g.,
802 ;; because the debugger was entered on an error in a
803 ;; PRINT-OBJECT method) makes a hopeless mess. Binding them
804 ;; here does seem somewhat ugly because it makes it more
805 ;; difficult to debug the printing-of-circularities code
806 ;; itself; however, as far as I (WHN, 2004-05-29) can see,
807 ;; that's almost entirely academic as long as there's one
808 ;; shared *C-H-T* for all streams (i.e., it's already
809 ;; unreasonably difficult to debug print-circle machinery
810 ;; given the buggy crosstalk between the debugger streams
811 ;; and the stream you're trying to watch), and any fix for
812 ;; that buggy arrangement will likely let this hack go away
813 ;; naturally.
814 (sb!impl::*circularity-hash-table* . nil)
815 (sb!impl::*circularity-counter* . nil)
816 (*readtable* *debug-readtable*))
817 (progv
818 ;; (Why NREVERSE? PROGV makes the later entries have
819 ;; precedence over the earlier entries.
820 ;; *DEBUG-PRINT-VARIABLE-ALIST* is called an alist, so it's
821 ;; expected that its earlier entries have precedence. And
822 ;; the earlier-has-precedence behavior is mostly more
823 ;; convenient, so that programmers can use PUSH or LIST* to
824 ;; customize *DEBUG-PRINT-VARIABLE-ALIST*.)
825 (nreverse (mapcar #'car *debug-print-variable-alist*))
826 (nreverse (mapcar #'cdr *debug-print-variable-alist*))
827 (apply fun rest)))))))
829 ;;; This function is not inlined so it shows up in the backtrace; that
830 ;;; can be rather handy when one has to debug the interplay between
831 ;;; *INVOKE-DEBUGGER-HOOK* and *DEBUGGER-HOOK*.
832 (declaim (notinline run-hook))
833 (defun run-hook (variable condition)
834 (let ((old-hook (symbol-value variable)))
835 (when old-hook
836 (progv (list variable) (list nil)
837 (funcall old-hook condition old-hook)))))
839 ;;; We can bind *stack-top-hint* to a symbol, in which case this function will
840 ;;; resolve that hint lazily before we enter the debugger.
841 (defun resolve-stack-top-hint ()
842 (let ((hint *stack-top-hint*)
843 (*stack-top-hint* nil))
844 (cond
845 ;; No hint, just keep the debugger guts out.
846 ((not hint)
847 (find-caller-name-and-frame))
848 ;; Interrupted. Look for the interrupted frame -- if we don't find one
849 ;; this falls back to the next case.
850 ((and (eq hint 'invoke-interruption)
851 (nth-value 1 (find-interrupted-name-and-frame))))
852 ;; Name of the first uninteresting frame.
853 ((symbolp hint)
854 (find-caller-of-named-frame hint))
855 ;; We already have a resolved hint.
857 hint))))
859 (defun invoke-debugger (condition)
860 #!+sb-doc
861 "Enter the debugger."
863 (let ((*stack-top-hint* (resolve-stack-top-hint)))
865 ;; call *INVOKE-DEBUGGER-HOOK* first, so that *DEBUGGER-HOOK* is not
866 ;; called when the debugger is disabled
867 (run-hook '*invoke-debugger-hook* condition)
868 (run-hook '*debugger-hook* condition)
870 ;; We definitely want *PACKAGE* to be of valid type.
872 ;; Elsewhere in the system, we use the SANE-PACKAGE function for
873 ;; this, but here causing an exception just as we're trying to handle
874 ;; an exception would be confusing, so instead we use a special hack.
875 (unless (and (packagep *package*)
876 (package-name *package*))
877 (setf *package* (find-package :cl-user))
878 (format *error-output*
879 "The value of ~S was not an undeleted PACKAGE. It has been ~
880 reset to ~S."
881 '*package* *package*))
883 ;; Before we start our own output, finish any pending output.
884 ;; Otherwise, if the user tried to track the progress of his program
885 ;; using PRINT statements, he'd tend to lose the last line of output
886 ;; or so, which'd be confusing.
887 (flush-standard-output-streams)
889 (funcall-with-debug-io-syntax #'%invoke-debugger condition)))
891 (defun %print-debugger-invocation-reason (condition stream)
892 (format stream "~2&")
893 ;; Note: Ordinarily it's only a matter of taste whether to use
894 ;; FORMAT "~<...~:>" or to use PPRINT-LOGICAL-BLOCK directly, but
895 ;; until bug 403 is fixed, PPRINT-LOGICAL-BLOCK (STREAM NIL) is
896 ;; definitely preferred, because the FORMAT alternative was acting odd.
897 (pprint-logical-block (stream nil)
898 (format stream
899 "debugger invoked on a ~S~@[ in thread ~_~A~]: ~2I~_~A"
900 (type-of condition)
901 #!+sb-thread sb!thread:*current-thread*
902 #!-sb-thread nil
903 condition))
904 (terpri stream))
906 (defun %invoke-debugger (condition)
907 (let ((*debug-condition* condition)
908 (*debug-restarts* (compute-restarts condition))
909 (*nested-debug-condition* nil))
910 (handler-case
911 ;; (The initial output here goes to *ERROR-OUTPUT*, because the
912 ;; initial output is not interactive, just an error message, and
913 ;; when people redirect *ERROR-OUTPUT*, they could reasonably
914 ;; expect to see error messages logged there, regardless of what
915 ;; the debugger does afterwards.)
916 (unless (typep condition 'step-condition)
917 (%print-debugger-invocation-reason condition *error-output*))
918 (error (condition)
919 (setf *nested-debug-condition* condition)
920 (let ((ndc-type (type-of *nested-debug-condition*)))
921 (format *error-output*
922 "~&~@<(A ~S was caught when trying to print ~S when ~
923 entering the debugger. Printing was aborted and the ~
924 ~S was stored in ~S.)~@:>~%"
925 ndc-type
926 '*debug-condition*
927 ndc-type
928 '*nested-debug-condition*))
929 (when (typep *nested-debug-condition* 'cell-error)
930 ;; what we really want to know when it's e.g. an UNBOUND-VARIABLE:
931 (format *error-output*
932 "~&(CELL-ERROR-NAME ~S) = ~S~%"
933 '*nested-debug-condition*
934 (cell-error-name *nested-debug-condition*)))))
936 (let ((background-p (sb!thread::debugger-wait-until-foreground-thread
937 *debug-io*)))
939 ;; After the initial error/condition/whatever announcement to
940 ;; *ERROR-OUTPUT*, we become interactive, and should talk on
941 ;; *DEBUG-IO* from now on. (KLUDGE: This is a normative
942 ;; statement, not a description of reality.:-| There's a lot of
943 ;; older debugger code which was written to do i/o on whatever
944 ;; stream was in fashion at the time, and not all of it has
945 ;; been converted to behave this way. -- WHN 2000-11-16)
947 (unwind-protect
948 (let (;; We used to bind *STANDARD-OUTPUT* to *DEBUG-IO*
949 ;; here as well, but that is probably bogus since it
950 ;; removes the users ability to do output to a redirected
951 ;; *S-O*. Now we just rebind it so that users can temporarily
952 ;; frob it. FIXME: This and other "what gets bound when"
953 ;; behaviour should be documented in the manual.
954 (*standard-output* *standard-output*)
955 ;; This seems reasonable: e.g. if the user has redirected
956 ;; *ERROR-OUTPUT* to some log file, it's probably wrong
957 ;; to send errors which occur in interactive debugging to
958 ;; that file, and right to send them to *DEBUG-IO*.
959 (*error-output* *debug-io*))
960 (unless (typep condition 'step-condition)
961 (when *debug-beginner-help-p*
962 (format *debug-io*
963 "~%~@<Type HELP for debugger help, or ~
964 (SB-EXT:EXIT) to exit from SBCL.~:@>~2%"))
965 (show-restarts *debug-restarts* *debug-io*))
966 (internal-debug))
967 (when background-p
968 (sb!thread::release-foreground))))))
970 ;;; this function is for use in *INVOKE-DEBUGGER-HOOK* when ordinary
971 ;;; ANSI behavior has been suppressed by the "--disable-debugger"
972 ;;; command-line option
973 (defun debugger-disabled-hook (condition previous-hook)
974 (declare (ignore previous-hook))
975 ;; There is no one there to interact with, so report the
976 ;; condition and terminate the program.
977 (let ((*suppress-print-errors* t)
978 (condition-error-message
979 #.(format nil "A nested error within --disable-debugger error ~
980 handling prevents displaying the original error. Attempting ~
981 to print a backtrace."))
982 (backtrace-error-message
983 #.(format nil "A nested error within --disable-debugger error ~
984 handling prevents printing the backtrace. Sorry, exiting.")))
985 (labels
986 ((failure-quit (&key abort)
987 (/show0 "in FAILURE-QUIT (in --disable-debugger debugger hook)")
988 (exit :code 1 :abort abort))
989 (display-condition ()
990 (handler-case
991 (handler-case
992 (print-condition)
993 (condition ()
994 ;; printing failed, try to describe it
995 (describe-condition)))
996 (condition ()
997 ;; ok, give up trying to display the error and inform the user about it
998 (finish-output *error-output*)
999 (%primitive print condition-error-message))))
1000 (print-condition ()
1001 (format *error-output*
1002 "~&~@<Unhandled ~S~@[ in thread ~S~]: ~2I~_~A~:>~2%"
1003 (type-of condition)
1004 #!+sb-thread sb!thread:*current-thread*
1005 #!-sb-thread nil
1006 condition)
1007 (finish-output *error-output*))
1008 (describe-condition ()
1009 (format *error-output*
1010 "~&Unhandled ~S~@[ in thread ~S~]:~%"
1011 (type-of condition)
1012 #!+sb-thread sb!thread:*current-thread*
1013 #!-sb-thread nil)
1014 (describe condition *error-output*)
1015 (finish-output *error-output*))
1016 (display-backtrace ()
1017 (handler-case
1018 (print-backtrace :stream *error-output*
1019 :from :interrupted-frame
1020 :print-thread t)
1021 (condition ()
1022 (values)))
1023 (finish-output *error-output*)))
1024 ;; This HANDLER-CASE is here mostly to stop output immediately
1025 ;; (and fall through to QUIT) when there's an I/O error. Thus,
1026 ;; when we're run under a shell script or something, we can die
1027 ;; cleanly when the script dies (and our pipes are cut), instead
1028 ;; of falling into ldb or something messy like that. Similarly, we
1029 ;; can terminate cleanly even if BACKTRACE dies because of bugs in
1030 ;; user PRINT-OBJECT methods. Separate the error handling of the
1031 ;; two phases to maximize the chance of emitting some useful
1032 ;; information.
1033 (handler-case
1034 (progn
1035 (display-condition)
1036 (display-backtrace)
1037 (format *error-output*
1038 "~%unhandled condition in --disable-debugger mode, quitting~%")
1039 (finish-output *error-output*)
1040 (failure-quit))
1041 (condition ()
1042 ;; We IGNORE-ERRORS here because even %PRIMITIVE PRINT can
1043 ;; fail when our output streams are blown away, as e.g. when
1044 ;; we're running under a Unix shell script and it dies somehow
1045 ;; (e.g. because of a SIGINT). In that case, we might as well
1046 ;; just give it up for a bad job, and stop trying to notify
1047 ;; the user of anything.
1049 ;; Actually, the only way I've run across to exercise the
1050 ;; problem is to have more than one layer of shell script.
1051 ;; I have a shell script which does
1052 ;; time nice -10 sh make.sh "$1" 2>&1 | tee make.tmp
1053 ;; and the problem occurs when I interrupt this with Ctrl-C
1054 ;; under Linux 2.2.14-5.0 and GNU bash, version 1.14.7(1).
1055 ;; I haven't figured out whether it's bash, time, tee, Linux, or
1056 ;; what that is responsible, but that it's possible at all
1057 ;; means that we should IGNORE-ERRORS here. -- WHN 2001-04-24
1058 (ignore-errors
1059 (%primitive print backtrace-error-message))
1060 (failure-quit :abort t))))))
1062 (defvar *old-debugger-hook* nil)
1064 ;;; halt-on-failures and prompt-on-failures modes, suitable for
1065 ;;; noninteractive and interactive use respectively
1066 (defun disable-debugger ()
1067 #!+sb-doc
1068 "When invoked, this function will turn off both the SBCL debugger
1069 and LDB (the low-level debugger). See also ENABLE-DEBUGGER."
1070 ;; *DEBUG-IO* used to be set here to *ERROR-OUTPUT* which is sort
1071 ;; of unexpected but mostly harmless, but then ENABLE-DEBUGGER had
1072 ;; to set it to a suitable value again and be very careful,
1073 ;; especially if the user has also set it. -- MG 2005-07-15
1074 (unless (eq *invoke-debugger-hook* 'debugger-disabled-hook)
1075 (setf *old-debugger-hook* *invoke-debugger-hook*
1076 *invoke-debugger-hook* 'debugger-disabled-hook))
1077 ;; This is not inside the UNLESS to ensure that LDB is disabled
1078 ;; regardless of what the old value of *INVOKE-DEBUGGER-HOOK* was.
1079 ;; This might matter for example when restoring a core.
1080 (sb!alien:alien-funcall (sb!alien:extern-alien "disable_lossage_handler"
1081 (function sb!alien:void))))
1083 (defun enable-debugger ()
1084 #!+sb-doc
1085 "Restore the debugger if it has been turned off by DISABLE-DEBUGGER."
1086 (when (eql *invoke-debugger-hook* 'debugger-disabled-hook)
1087 (setf *invoke-debugger-hook* *old-debugger-hook*
1088 *old-debugger-hook* nil))
1089 (sb!alien:alien-funcall (sb!alien:extern-alien "enable_lossage_handler"
1090 (function sb!alien:void))))
1092 (defun show-restarts (restarts s)
1093 (cond ((null restarts)
1094 (format s
1095 "~&(no restarts: If you didn't do this on purpose, ~
1096 please report it as a bug.)~%"))
1098 (format s "~&restarts (invokable by number or by ~
1099 possibly-abbreviated name):~%")
1100 (let ((count 0)
1101 (names-used '(nil))
1102 (max-name-len 0))
1103 (dolist (restart restarts)
1104 (let ((name (restart-name restart)))
1105 (when name
1106 (let ((len (length (princ-to-string name))))
1107 (when (> len max-name-len)
1108 (setf max-name-len len))))))
1109 (unless (zerop max-name-len)
1110 (incf max-name-len 3))
1111 (dolist (restart restarts)
1112 (let ((name (restart-name restart)))
1113 ;; FIXME: maybe it would be better to display later names
1114 ;; in parens instead of brakets, not just omit them fully.
1115 ;; Call BREAK, call BREAK in the debugger, and tell me
1116 ;; it's not confusing looking. --NS 20050310
1117 (cond ((member name names-used)
1118 (format s "~& ~2D: ~V@T~A~%" count max-name-len restart))
1120 (format s "~& ~2D: [~VA] ~A~%"
1121 count (- max-name-len 3) name restart)
1122 (push name names-used))))
1123 (incf count))))))
1125 (defvar *debug-loop-fun* #'debug-loop-fun
1126 #!+sb-doc
1127 "A function taking no parameters that starts the low-level debug loop.")
1129 ;;; When the debugger is invoked due to a stepper condition, we don't
1130 ;;; want to print the current frame before the first prompt for aesthetic
1131 ;;; reasons.
1132 (defvar *suppress-frame-print* nil)
1134 ;;; This calls DEBUG-LOOP, performing some simple initializations
1135 ;;; before doing so. INVOKE-DEBUGGER calls this to actually get into
1136 ;;; the debugger. SB!KERNEL::ERROR-ERROR calls this in emergencies
1137 ;;; to get into a debug prompt as quickly as possible with as little
1138 ;;; risk as possible for stepping on whatever is causing recursive
1139 ;;; errors.
1140 (defun internal-debug ()
1141 (let ((*in-the-debugger* t)
1142 (*read-suppress* nil))
1143 (unless (typep *debug-condition* 'step-condition)
1144 (clear-input *debug-io*))
1145 (let ((*suppress-frame-print* (typep *debug-condition* 'step-condition)))
1146 (funcall *debug-loop-fun*))))
1148 ;;;; DEBUG-LOOP
1150 ;;; Note: This defaulted to T in CMU CL. The changed default in SBCL
1151 ;;; was motivated by desire to play nicely with ILISP.
1152 (defvar *flush-debug-errors* nil
1153 #!+sb-doc
1154 "When set, avoid calling INVOKE-DEBUGGER recursively when errors occur while
1155 executing in the debugger.")
1157 (defun debug-read (stream eof-restart)
1158 (declare (type stream stream))
1159 (let* ((eof-marker (cons nil nil))
1160 (form (read stream nil eof-marker)))
1161 (if (eq form eof-marker)
1162 (invoke-restart eof-restart)
1163 form)))
1165 (defun debug-loop-fun ()
1166 (let* ((*debug-command-level* (1+ *debug-command-level*))
1167 (*real-stack-top* (sb!di:top-frame))
1168 (*stack-top* (or *stack-top-hint* *real-stack-top*))
1169 (*stack-top-hint* nil)
1170 (*current-frame* *stack-top*))
1171 (handler-bind ((sb!di:debug-condition
1172 (lambda (condition)
1173 (princ condition *debug-io*)
1174 (/show0 "handling d-c by THROWing DEBUG-LOOP-CATCHER")
1175 (throw 'debug-loop-catcher nil))))
1176 (cond (*suppress-frame-print*
1177 (setf *suppress-frame-print* nil))
1179 (terpri *debug-io*)
1180 (print-frame-call *current-frame* *debug-io* :print-frame-source t)))
1181 (loop
1182 (catch 'debug-loop-catcher
1183 (handler-bind ((error (lambda (condition)
1184 (when *flush-debug-errors*
1185 (clear-input *debug-io*)
1186 (princ condition *debug-io*)
1187 (format *debug-io*
1188 "~&error flushed (because ~
1189 ~S is set)"
1190 '*flush-debug-errors*)
1191 (/show0 "throwing DEBUG-LOOP-CATCHER")
1192 (throw 'debug-loop-catcher nil)))))
1193 ;; We have to bind LEVEL for the restart function created
1194 ;; by WITH-SIMPLE-RESTART, and we need the explicit ABORT
1195 ;; restart that exists now so that EOF from read can drop
1196 ;; one debugger level.
1197 (let ((level *debug-command-level*)
1198 (restart-commands (make-restart-commands))
1199 (abort-restart-for-eof (find-restart 'abort)))
1200 (flush-standard-output-streams)
1201 (debug-prompt *debug-io*)
1202 (force-output *debug-io*)
1203 (with-simple-restart (abort
1204 "~@<Reduce debugger level (to debug level ~W).~@:>"
1205 level)
1206 (let* ((exp (debug-read *debug-io* abort-restart-for-eof))
1207 (cmd-fun (debug-command-p exp restart-commands)))
1208 (cond ((not cmd-fun)
1209 (debug-eval-print exp))
1210 ((consp cmd-fun)
1211 (format *debug-io*
1212 "~&Your command, ~S, is ambiguous:~%"
1213 exp)
1214 (dolist (ele cmd-fun)
1215 (format *debug-io* " ~A~%" ele)))
1217 (funcall cmd-fun))))))))))))
1219 (defvar *auto-eval-in-frame* t
1220 #!+sb-doc
1221 "When set (the default), evaluations in the debugger's command loop occur
1222 relative to the current frame's environment without the need of debugger
1223 forms that explicitly control this kind of evaluation.")
1225 (defun debug-eval (expr)
1226 (cond ((not (and (fboundp 'compile) *auto-eval-in-frame*))
1227 (eval expr))
1228 ((frame-has-debug-vars-p *current-frame*)
1229 (sb!di:eval-in-frame *current-frame* expr))
1231 (format *debug-io* "; No debug variables for current frame: ~
1232 using EVAL instead of EVAL-IN-FRAME.~%")
1233 (eval expr))))
1235 (defun debug-eval-print (expr)
1236 (/noshow "entering DEBUG-EVAL-PRINT" expr)
1237 (let ((values (multiple-value-list
1238 (interactive-eval expr :eval #'debug-eval))))
1239 (/noshow "done with EVAL in DEBUG-EVAL-PRINT")
1240 (dolist (value values)
1241 (fresh-line *debug-io*)
1242 (prin1 value *debug-io*)))
1243 (force-output *debug-io*))
1245 ;;;; debug loop functions
1247 ;;; These commands are functions, not really commands, so that users
1248 ;;; can get their hands on the values returned.
1250 (defun var-valid-in-frame-p (var location &optional (frame *current-frame*))
1251 ;; arg count errors are checked before anything is set up but they
1252 ;; are reporeted in *elsewhere*, which is after start-pc saved in the
1253 ;; debug function, defeating the checks.
1254 (and (not (sb!di::tl-invalid-arg-count-error-p frame))
1255 (eq (sb!di:debug-var-validity var location) :valid)))
1257 (eval-when (:execute :compile-toplevel)
1259 (sb!xc:defmacro define-var-operation (ref-or-set &optional value-var)
1260 `(let* ((temp (etypecase name
1261 (symbol (sb!di:debug-fun-symbol-vars
1262 (sb!di:frame-debug-fun *current-frame*)
1263 name))
1264 (simple-string (sb!di:ambiguous-debug-vars
1265 (sb!di:frame-debug-fun *current-frame*)
1266 name))))
1267 (location (sb!di:frame-code-location *current-frame*))
1268 ;; Let's only deal with valid variables.
1269 (vars (remove-if-not (lambda (v)
1270 (var-valid-in-frame-p v location))
1271 temp)))
1272 (declare (list vars))
1273 (cond ((null vars)
1274 (error "No known valid variables match ~S." name))
1275 ((= (length vars) 1)
1276 ,(ecase ref-or-set
1277 (:ref
1278 '(sb!di:debug-var-value (car vars) *current-frame*))
1279 (:set
1280 `(setf (sb!di:debug-var-value (car vars) *current-frame*)
1281 ,value-var))))
1283 ;; Since we have more than one, first see whether we have
1284 ;; any variables that exactly match the specification.
1285 (let* ((name (etypecase name
1286 (symbol (symbol-name name))
1287 (simple-string name)))
1288 ;; FIXME: REMOVE-IF-NOT is deprecated, use STRING/=
1289 ;; instead.
1290 (exact (remove-if-not (lambda (v)
1291 (string= (sb!di:debug-var-symbol-name v)
1292 name))
1293 vars))
1294 (vars (or exact vars)))
1295 (declare (simple-string name)
1296 (list exact vars))
1297 (cond
1298 ;; Check now for only having one variable.
1299 ((= (length vars) 1)
1300 ,(ecase ref-or-set
1301 (:ref
1302 '(sb!di:debug-var-value (car vars) *current-frame*))
1303 (:set
1304 `(setf (sb!di:debug-var-value (car vars) *current-frame*)
1305 ,value-var))))
1306 ;; If there weren't any exact matches, flame about
1307 ;; ambiguity unless all the variables have the same
1308 ;; name.
1309 ((and (not exact)
1310 (find-if-not
1311 (lambda (v)
1312 (string= (sb!di:debug-var-symbol-name v)
1313 (sb!di:debug-var-symbol-name (car vars))))
1314 (cdr vars)))
1315 (error "specification ambiguous:~%~{ ~A~%~}"
1316 (mapcar #'sb!di:debug-var-symbol-name
1317 (delete-duplicates
1318 vars :test #'string=
1319 :key #'sb!di:debug-var-symbol-name))))
1320 ;; All names are the same, so see whether the user
1321 ;; ID'ed one of them.
1322 (id-supplied
1323 (let ((v (find id vars :key #'sb!di:debug-var-id)))
1324 (unless v
1325 (error
1326 "invalid variable ID, ~W: should have been one of ~S"
1328 (mapcar #'sb!di:debug-var-id vars)))
1329 ,(ecase ref-or-set
1330 (:ref
1331 '(sb!di:debug-var-value v *current-frame*))
1332 (:set
1333 `(setf (sb!di:debug-var-value v *current-frame*)
1334 ,value-var)))))
1336 (error "Specify variable ID to disambiguate ~S. Use one of ~S."
1337 name
1338 (mapcar #'sb!di:debug-var-id vars)))))))))
1340 ) ; EVAL-WHEN
1342 ;;; FIXME: This doesn't work. It would be real nice we could make it
1343 ;;; work! Alas, it doesn't seem to work in CMU CL X86 either..
1344 (defun var (name &optional (id 0 id-supplied))
1345 #!+sb-doc
1346 "Return a variable's value if possible. NAME is a simple-string or symbol.
1347 If it is a simple-string, it is an initial substring of the variable's name.
1348 If name is a symbol, it has the same name and package as the variable whose
1349 value this function returns. If the symbol is uninterned, then the variable
1350 has the same name as the symbol, but it has no package.
1352 If name is the initial substring of variables with different names, then
1353 this return no values after displaying the ambiguous names. If name
1354 determines multiple variables with the same name, then you must use the
1355 optional id argument to specify which one you want. If you left id
1356 unspecified, then this returns no values after displaying the distinguishing
1357 id values.
1359 The result of this function is limited to the availability of variable
1360 information. This is SETF'able."
1361 (define-var-operation :ref))
1362 (defun (setf var) (value name &optional (id 0 id-supplied))
1363 (define-var-operation :set value))
1365 ;;; This returns the COUNT'th arg as the user sees it from args, the
1366 ;;; result of SB!DI:DEBUG-FUN-LAMBDA-LIST. If this returns a
1367 ;;; potential DEBUG-VAR from the lambda-list, then the second value is
1368 ;;; T. If this returns a keyword symbol or a value from a rest arg,
1369 ;;; then the second value is NIL.
1371 ;;; FIXME: There's probably some way to merge the code here with
1372 ;;; FRAME-ARGS-AS-LIST. (A fair amount of logic is already shared
1373 ;;; through LAMBDA-LIST-ELEMENT-DISPATCH, but I suspect more could be.)
1374 (declaim (ftype (function (index list)) nth-arg))
1375 (defun nth-arg (count args)
1376 (let ((n count))
1377 (dolist (ele args (error "The argument specification ~S is out of range."
1379 (lambda-list-element-dispatch ele
1380 :required ((if (zerop n) (return (values ele t))))
1381 :optional ((if (zerop n) (return (values (second ele) t))))
1382 :keyword ((cond ((zerop n)
1383 (return (values (second ele) nil)))
1384 ((zerop (decf n))
1385 (return (values (third ele) t)))))
1386 :deleted ((if (zerop n) (return (values ele t))))
1387 :rest ((let ((var (second ele)))
1388 (lambda-var-dispatch var (sb!di:frame-code-location
1389 *current-frame*)
1390 (error "unused &REST argument before n'th argument")
1391 (dolist (value
1392 (sb!di:debug-var-value var *current-frame*)
1393 (error
1394 "The argument specification ~S is out of range."
1396 (if (zerop n)
1397 (return-from nth-arg (values value nil))
1398 (decf n)))
1399 (error "invalid &REST argument before n'th argument")))))
1400 (decf n))))
1402 (defun arg (n)
1403 #!+sb-doc
1404 "Return the N'th argument's value if possible. Argument zero is the first
1405 argument in a frame's default printed representation. Count keyword/value
1406 pairs as separate arguments."
1407 #!+precise-arg-count-error
1408 (when (sb!di::tl-invalid-arg-count-error-p *current-frame*)
1409 (return-from arg
1410 (arg-count-error-frame-nth-arg n *current-frame*)))
1411 (multiple-value-bind (var lambda-var-p)
1412 (nth-arg n (handler-case (sb!di:debug-fun-lambda-list
1413 (sb!di:frame-debug-fun *current-frame*))
1414 (sb!di:lambda-list-unavailable ()
1415 (error "No argument values are available."))))
1416 (if lambda-var-p
1417 (lambda-var-dispatch var (sb!di:frame-code-location *current-frame*)
1418 (error "Unused arguments have no values.")
1419 (sb!di:debug-var-value var *current-frame*)
1420 (error "invalid argument value"))
1421 var)))
1423 ;;;; machinery for definition of debug loop commands
1425 (defvar *debug-commands* nil)
1427 ;;; Interface to *DEBUG-COMMANDS*. No required arguments in args are
1428 ;;; permitted.
1429 (defmacro !def-debug-command (name args &rest body)
1430 (let ((fun-name (symbolicate name "-DEBUG-COMMAND")))
1431 `(progn
1432 (setf *debug-commands*
1433 (remove ,name *debug-commands* :key #'car :test #'string=))
1434 (defun ,fun-name ,args
1435 (unless *in-the-debugger*
1436 (error "invoking debugger command while outside the debugger"))
1437 ,@body)
1438 (push (cons ,name #',fun-name) *debug-commands*)
1439 ',fun-name)))
1441 (defun !def-debug-command-alias (new-name existing-name)
1442 (let ((pair (assoc existing-name *debug-commands* :test #'string=)))
1443 (unless pair (error "unknown debug command name: ~S" existing-name))
1444 (push (cons new-name (cdr pair)) *debug-commands*))
1445 new-name)
1447 ;;; This takes a symbol and uses its name to find a debugger command,
1448 ;;; using initial substring matching. It returns the command function
1449 ;;; if form identifies only one command, but if form is ambiguous,
1450 ;;; this returns a list of the command names. If there are no matches,
1451 ;;; this returns nil. Whenever the loop that looks for a set of
1452 ;;; possibilities encounters an exact name match, we return that
1453 ;;; command function immediately.
1454 (defun debug-command-p (form &optional other-commands)
1455 (if (or (symbolp form) (integerp form))
1456 (let* ((name
1457 (if (symbolp form)
1458 (symbol-name form)
1459 (format nil "~W" form)))
1460 (len (length name))
1461 (res nil))
1462 (declare (simple-string name)
1463 (fixnum len)
1464 (list res))
1466 ;; Find matching commands, punting if exact match.
1467 (flet ((match-command (ele)
1468 (let* ((str (car ele))
1469 (str-len (length str)))
1470 (declare (simple-string str)
1471 (fixnum str-len))
1472 (cond ((< str-len len))
1473 ((= str-len len)
1474 (when (string= name str :end1 len :end2 len)
1475 (return-from debug-command-p (cdr ele))))
1476 ((string= name str :end1 len :end2 len)
1477 (push ele res))))))
1478 (mapc #'match-command *debug-commands*)
1479 (mapc #'match-command other-commands))
1481 ;; Return the right value.
1482 (cond ((not res) nil)
1483 ((= (length res) 1)
1484 (cdar res))
1485 (t ; Just return the names.
1486 (do ((cmds res (cdr cmds)))
1487 ((not cmds) res)
1488 (setf (car cmds) (caar cmds))))))))
1490 ;;; Return a list of debug commands (in the same format as
1491 ;;; *DEBUG-COMMANDS*) that invoke each active restart.
1493 ;;; Two commands are made for each restart: one for the number, and
1494 ;;; one for the restart name (unless it's been shadowed by an earlier
1495 ;;; restart of the same name, or it is NIL).
1496 (defun make-restart-commands (&optional (restarts *debug-restarts*))
1497 (let ((commands)
1498 (num 0)) ; better be the same as show-restarts!
1499 (dolist (restart restarts)
1500 (let ((name (string (restart-name restart))))
1501 (let ((restart-fun
1502 (lambda ()
1503 (/show0 "in restart-command closure, about to i-r-i")
1504 (invoke-restart-interactively restart))))
1505 (push (cons (prin1-to-string num) restart-fun) commands)
1506 (unless (or (null (restart-name restart))
1507 (find name commands :key #'car :test #'string=))
1508 (push (cons name restart-fun) commands))))
1509 (incf num))
1510 commands))
1512 ;;;; frame-changing commands
1514 (!def-debug-command "UP" ()
1515 (let ((next (sb!di:frame-up *current-frame*)))
1516 (cond (next
1517 (setf *current-frame* next)
1518 (print-frame-call next *debug-io*))
1520 (format *debug-io* "~&Top of stack.")))))
1522 (!def-debug-command "DOWN" ()
1523 (let ((next (sb!di:frame-down *current-frame*)))
1524 (cond (next
1525 (setf *current-frame* next)
1526 (print-frame-call next *debug-io*))
1528 (format *debug-io* "~&Bottom of stack.")))))
1530 (!def-debug-command-alias "D" "DOWN")
1532 (!def-debug-command "BOTTOM" ()
1533 (do ((prev *current-frame* lead)
1534 (lead (sb!di:frame-down *current-frame*) (sb!di:frame-down lead)))
1535 ((null lead)
1536 (setf *current-frame* prev)
1537 (print-frame-call prev *debug-io*))))
1539 (!def-debug-command-alias "B" "BOTTOM")
1541 (!def-debug-command "FRAME" (&optional
1542 (n (read-prompting-maybe "frame number: ")))
1543 (setf *current-frame*
1544 (multiple-value-bind (next-frame-fun limit-string)
1545 (if (< n (sb!di:frame-number *current-frame*))
1546 (values #'sb!di:frame-up "top")
1547 (values #'sb!di:frame-down "bottom"))
1548 (do ((frame *current-frame*))
1549 ((= n (sb!di:frame-number frame))
1550 frame)
1551 (let ((next-frame (funcall next-frame-fun frame)))
1552 (cond (next-frame
1553 (setf frame next-frame))
1555 (format *debug-io*
1556 "The ~A of the stack was encountered.~%"
1557 limit-string)
1558 (return frame)))))))
1559 (print-frame-call *current-frame* *debug-io*))
1561 (!def-debug-command-alias "F" "FRAME")
1563 ;;;; commands for entering and leaving the debugger
1565 (!def-debug-command "TOPLEVEL" ()
1566 (throw 'toplevel-catcher nil))
1568 ;;; make T safe
1569 (!def-debug-command-alias "TOP" "TOPLEVEL")
1571 (!def-debug-command "RESTART" ()
1572 (/show0 "doing RESTART debug-command")
1573 (let ((num (read-if-available :prompt)))
1574 (when (eq num :prompt)
1575 (show-restarts *debug-restarts* *debug-io*)
1576 (write-string "restart: " *debug-io*)
1577 (force-output *debug-io*)
1578 (setf num (read *debug-io*)))
1579 (let ((restart (typecase num
1580 (unsigned-byte
1581 (nth num *debug-restarts*))
1582 (symbol
1583 (find num *debug-restarts* :key #'restart-name
1584 :test (lambda (sym1 sym2)
1585 (string= (symbol-name sym1)
1586 (symbol-name sym2)))))
1588 (format *debug-io* "~S is invalid as a restart name.~%"
1589 num)
1590 (return-from restart-debug-command nil)))))
1591 (/show0 "got RESTART")
1592 (if restart
1593 (invoke-restart-interactively restart)
1594 (princ "There is no such restart." *debug-io*)))))
1596 ;;;; information commands
1598 (!def-debug-command "HELP" ()
1599 ;; CMU CL had a little toy pager here, but "if you aren't running
1600 ;; ILISP (or a smart windowing system, or something) you deserve to
1601 ;; lose", so we've dropped it in SBCL. However, in case some
1602 ;; desperate holdout is running this on a dumb terminal somewhere,
1603 ;; we tell him where to find the message stored as a string.
1604 (format *debug-io*
1605 "~&~A~2%(The HELP string is stored in ~S.)~%"
1606 *debug-help-string*
1607 '*debug-help-string*))
1609 (!def-debug-command-alias "?" "HELP")
1611 (!def-debug-command "ERROR" ()
1612 (format *debug-io* "~A~%" *debug-condition*)
1613 (show-restarts *debug-restarts* *debug-io*))
1615 (!def-debug-command "BACKTRACE" ()
1616 (print-backtrace :count (read-if-available most-positive-fixnum)))
1618 (!def-debug-command "PRINT" ()
1619 (print-frame-call *current-frame* *debug-io*))
1621 (!def-debug-command-alias "P" "PRINT")
1623 (!def-debug-command "LIST-LOCALS" ()
1624 (let ((d-fun (sb!di:frame-debug-fun *current-frame*)))
1625 (if (sb!di:debug-var-info-available d-fun)
1626 (let ((*standard-output* *debug-io*)
1627 (location (sb!di:frame-code-location *current-frame*))
1628 (prefix (read-if-available nil))
1629 (any-p nil)
1630 (any-valid-p nil)
1631 (more-context nil)
1632 (more-count nil))
1633 (dolist (v (sb!di:ambiguous-debug-vars
1634 d-fun
1635 (if prefix (string prefix) "")))
1636 (setf any-p t)
1637 (when (var-valid-in-frame-p v location)
1638 (setf any-valid-p t)
1639 (case (sb!di::debug-var-info v)
1640 (:more-context
1641 (setf more-context (sb!di:debug-var-value v *current-frame*)))
1642 (:more-count
1643 (setf more-count (sb!di:debug-var-value v *current-frame*))))
1644 (format *debug-io* "~S~:[#~W~;~*~] = ~S~%"
1645 (sb!di:debug-var-symbol v)
1646 (zerop (sb!di:debug-var-id v))
1647 (sb!di:debug-var-id v)
1648 (sb!di:debug-var-value v *current-frame*))))
1649 (when (and more-context more-count)
1650 (format *debug-io* "~S = ~S~%"
1651 'more
1652 (multiple-value-list (sb!c:%more-arg-values more-context 0 more-count))))
1653 (cond
1654 ((not any-p)
1655 (format *debug-io*
1656 "There are no local variables ~@[starting with ~A ~]~
1657 in the function."
1658 prefix))
1659 ((not any-valid-p)
1660 (format *debug-io*
1661 "All variables ~@[starting with ~A ~]currently ~
1662 have invalid values."
1663 prefix))))
1664 (write-line "There is no variable information available."
1665 *debug-io*))))
1667 (!def-debug-command-alias "L" "LIST-LOCALS")
1669 (!def-debug-command "SOURCE" ()
1670 (print (code-location-source-form (sb!di:frame-code-location *current-frame*)
1671 (read-if-available 0))
1672 *debug-io*))
1674 ;;;; source location printing
1676 (defun code-location-source-form (location context &optional (errorp t))
1677 (let* ((start-location (maybe-block-start-location location))
1678 (form-num (sb!di:code-location-form-number start-location)))
1679 (multiple-value-bind (translations form)
1680 (sb!di:get-toplevel-form start-location)
1681 (cond ((< form-num (length translations))
1682 (sb!di:source-path-context form
1683 (svref translations form-num)
1684 context))
1686 (funcall (if errorp #'error #'warn)
1687 "~@<Bogus form-number: the source file has ~
1688 probably changed too much to cope with.~:@>"))))))
1691 ;;; start single-stepping
1692 (!def-debug-command "START" ()
1693 (if (typep *debug-condition* 'step-condition)
1694 (format *debug-io* "~&Already single-stepping.~%")
1695 (let ((restart (find-restart 'continue *debug-condition*)))
1696 (cond (restart
1697 (sb!impl::enable-stepping)
1698 (invoke-restart restart))
1700 (format *debug-io* "~&Non-continuable error, cannot start stepping.~%"))))))
1702 (defmacro !def-step-command (command-name restart-name)
1703 `(!def-debug-command ,command-name ()
1704 (if (typep *debug-condition* 'step-condition)
1705 (let ((restart (find-restart ',restart-name *debug-condition*)))
1706 (aver restart)
1707 (invoke-restart restart))
1708 (format *debug-io* "~&Not currently single-stepping. (Use START to activate the single-stepper)~%"))))
1710 (!def-step-command "STEP" step-into)
1711 (!def-step-command "NEXT" step-next)
1712 (!def-step-command "STOP" step-continue)
1714 (!def-debug-command-alias "S" "STEP")
1715 (!def-debug-command-alias "N" "NEXT")
1717 (!def-debug-command "OUT" ()
1718 (if (typep *debug-condition* 'step-condition)
1719 (if sb!impl::*step-out*
1720 (let ((restart (find-restart 'step-out *debug-condition*)))
1721 (aver restart)
1722 (invoke-restart restart))
1723 (format *debug-io* "~&OUT can only be used step out of frames that were originally stepped into with STEP.~%"))
1724 (format *debug-io* "~&Not currently single-stepping. (Use START to activate the single-stepper)~%")))
1726 ;;; miscellaneous commands
1728 (!def-debug-command "DESCRIBE" ()
1729 (let* ((curloc (sb!di:frame-code-location *current-frame*))
1730 (debug-fun (sb!di:code-location-debug-fun curloc))
1731 (function (sb!di:debug-fun-fun debug-fun)))
1732 (if function
1733 (describe function)
1734 (format *debug-io* "can't figure out the function for this frame"))))
1736 (!def-debug-command "SLURP" ()
1737 (loop while (read-char-no-hang *standard-input*)))
1739 ;;; RETURN-FROM-FRAME and RESTART-FRAME
1741 (defun unwind-to-frame-and-call (frame thunk)
1742 #!+unwind-to-frame-and-call-vop
1743 (flet ((sap-int/fixnum (sap)
1744 ;; On unithreaded X86 *BINDING-STACK-POINTER* and
1745 ;; *CURRENT-CATCH-BLOCK* are negative, so we need to jump through
1746 ;; some hoops to make these calculated values negative too.
1747 (ash (truly-the (signed-byte #.sb!vm:n-word-bits)
1748 (sap-int sap))
1749 (- sb!vm::n-fixnum-tag-bits))))
1750 ;; To properly unwind the stack, we need three pieces of information:
1751 ;; * The unwind block that should be active after the unwind
1752 ;; * The catch block that should be active after the unwind
1753 ;; * The values that the binding stack pointer should have after the
1754 ;; unwind.
1755 (let ((block (sap-int/fixnum (find-enclosing-catch-block frame)))
1756 (unbind-to (find-binding-stack-pointer frame)))
1757 ;; This VOP will run the neccessary cleanup forms, reset the fp, and
1758 ;; then call the supplied function.
1759 (sb!vm::%primitive sb!vm::unwind-to-frame-and-call
1760 (sb!di::frame-pointer frame)
1761 (find-enclosing-uwp frame)
1762 (lambda ()
1763 ;; Before calling the user-specified
1764 ;; function, we need to restore the binding
1765 ;; stack and the catch block. The unwind block
1766 ;; is taken care of by the VOP.
1767 (sb!vm::%primitive sb!vm::unbind-to-here
1768 unbind-to)
1769 (setf sb!vm::*current-catch-block* block)
1770 (funcall thunk)))))
1771 #!-unwind-to-frame-and-call-vop
1772 (let ((tag (gensym)))
1773 (sb!di:replace-frame-catch-tag frame
1774 'sb!c:debug-catch-tag
1775 tag)
1776 (throw tag thunk)))
1778 #!+unwind-to-frame-and-call-vop
1779 (defun find-binding-stack-pointer (frame)
1780 (let ((debug-fun (sb!di:frame-debug-fun frame)))
1781 (if (eq (sb!di:debug-fun-kind debug-fun) :external)
1782 ;; XEPs do not bind anything, nothing to restore.
1783 ;; But they may call other code through SATISFIES
1784 ;; declaration, check that the interrupt is actually in the XEP.
1785 (and (sb!di::compiled-frame-escaped frame)
1786 sb!kernel::*interr-current-bsp*)
1787 (let* ((compiled-debug-fun (and
1788 (typep debug-fun 'sb!di::compiled-debug-fun)
1789 (sb!di::compiled-debug-fun-compiler-debug-fun debug-fun)))
1790 (bsp-save-offset (and compiled-debug-fun
1791 (sb!c::compiled-debug-fun-bsp-save compiled-debug-fun))))
1792 (when bsp-save-offset
1793 (sb!di::sub-access-debug-var-slot (sb!di::frame-pointer frame) bsp-save-offset))))))
1795 (defun find-enclosing-catch-block (frame)
1796 ;; Walk the catch block chain looking for the first entry with an address
1797 ;; higher than the pointer for FRAME or a null pointer.
1798 (let* ((frame-pointer (sb!di::frame-pointer frame))
1799 (current-block (int-sap (ldb (byte #.sb!vm:n-word-bits 0)
1800 (ash sb!vm::*current-catch-block*
1801 sb!vm:n-fixnum-tag-bits))))
1802 (enclosing-block (loop for block = current-block
1803 then (sap-ref-sap block
1804 (* sb!vm:catch-block-previous-catch-slot
1805 sb!vm::n-word-bytes))
1806 when (or (zerop (sap-int block))
1807 #!+stack-grows-downward-not-upward
1808 (sap> block frame-pointer)
1809 #!-stack-grows-downward-not-upward
1810 (sap< block frame-pointer))
1811 return block)))
1812 enclosing-block))
1814 (defun find-enclosing-uwp (frame)
1815 ;; Walk the UWP chain looking for the first entry with an address
1816 ;; higher than the pointer for FRAME or a null pointer.
1817 (let* ((frame-pointer (sb!di::frame-pointer frame))
1818 (current-uwp (int-sap (ldb (byte #.sb!vm:n-word-bits 0)
1819 (ash sb!vm::*current-unwind-protect-block*
1820 sb!vm:n-fixnum-tag-bits))))
1821 (enclosing-uwp (loop for uwp-block = current-uwp
1822 then (sap-ref-sap uwp-block
1823 sb!vm:unwind-block-current-uwp-slot)
1824 when (or (zerop (sap-int uwp-block))
1825 #!+stack-grows-downward-not-upward
1826 (sap> uwp-block frame-pointer)
1827 #!-stack-grows-downward-not-upward
1828 (sap< uwp-block frame-pointer))
1829 return uwp-block)))
1830 enclosing-uwp))
1832 (!def-debug-command "RETURN" (&optional
1833 (return (read-prompting-maybe
1834 "return: ")))
1835 (if (frame-has-debug-tag-p *current-frame*)
1836 (let* ((code-location (sb!di:frame-code-location *current-frame*))
1837 (values (multiple-value-list
1838 (funcall (sb!di:preprocess-for-eval return code-location)
1839 *current-frame*))))
1840 (unwind-to-frame-and-call *current-frame* (lambda ()
1841 (values-list values))))
1842 (format *debug-io*
1843 "~@<can't find a tag for this frame ~
1844 ~2I~_(hint: try increasing the DEBUG optimization quality ~
1845 and recompiling)~:@>")))
1847 (!def-debug-command "RESTART-FRAME" ()
1848 (if (frame-has-debug-tag-p *current-frame*)
1849 (multiple-value-bind (fname args) (frame-call *current-frame*)
1850 (multiple-value-bind (fun arglist ok)
1851 (if (and (legal-fun-name-p fname) (fboundp fname))
1852 (values (fdefinition fname) args t)
1853 (values (sb!di:debug-fun-fun (sb!di:frame-debug-fun *current-frame*))
1854 (frame-args-as-list *current-frame*)
1855 nil))
1856 (when (and fun
1857 (or ok
1858 (y-or-n-p "~@<No global function for the frame, but we ~
1859 do have access to a function object that we ~
1860 can try to call -- but if it is normally part ~
1861 of a closure, then this is NOT going to end well.~_~_~
1862 Try it anyways?~:@>")))
1863 (unwind-to-frame-and-call *current-frame*
1864 (lambda ()
1865 ;; Ensure TCO.
1866 (declare (optimize (debug 0)))
1867 (apply fun arglist))))
1868 (format *debug-io*
1869 "Can't restart ~S: no function for frame."
1870 *current-frame*)))
1871 (format *debug-io*
1872 "~@<Can't restart ~S: tag not found. ~
1873 ~2I~_(hint: try increasing the DEBUG optimization quality ~
1874 and recompiling)~:@>"
1875 *current-frame*)))
1877 (defun frame-has-debug-tag-p (frame)
1878 #!+unwind-to-frame-and-call-vop
1879 ;; XEPs do not bind anything, nothing to restore
1880 (find-binding-stack-pointer frame)
1881 #!-unwind-to-frame-and-call-vop
1882 (find 'sb!c:debug-catch-tag (sb!di::frame-catches frame) :key #'car))
1884 (defun frame-has-debug-vars-p (frame)
1885 (sb!di:debug-var-info-available
1886 (sb!di:code-location-debug-fun
1887 (sb!di:frame-code-location frame))))
1889 ;;;; debug loop command utilities
1891 (defun read-prompting-maybe (prompt)
1892 (unless (sb!int:listen-skip-whitespace *debug-io*)
1893 (princ prompt *debug-io*)
1894 (force-output *debug-io*))
1895 (read *debug-io*))
1897 (defun read-if-available (default)
1898 (if (sb!int:listen-skip-whitespace *debug-io*)
1899 (read *debug-io*)
1900 default))