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