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