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