Ifdef-ize the hopscotch hash stuff for non-x86.
[sbcl.git] / src / code / debug.lisp
blob427d82a90016e922c84d53e22fe28e1ab2b7241d
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 (if (listp args)
735 (format stream "~{ ~_~S~}" args)
736 (format stream " ~S" args)))))
737 (when info
738 (format stream " [~{~(~A~)~^,~}]" info)))
739 (when print-frame-source
740 (let* ((loc (sb!di:frame-code-location frame))
741 (path (and (sb!di::compiled-debug-fun-p
742 (sb!di:code-location-debug-fun loc))
743 (handler-case (sb!di:code-location-debug-source loc)
744 (sb!di:no-debug-blocks ())
745 (:no-error (source)
746 (sb!di:debug-source-namestring source))))))
747 (when (or (eq print-frame-source :always)
748 ;; Avoid showing sources for internals,
749 ;; it will either fail anyway due to all the SB! and
750 ;; reader conditionals or show something nobody has
751 ;; any iterest in.
752 (not (eql (search "SYS:SRC;" path) 0)))
753 (handler-case
754 (let ((source (handler-case
755 (code-location-source-form loc 0)
756 (error (c)
757 (format stream "~& error finding frame source: ~A" c)))))
758 (format stream "~% source: ~S" source))
759 (sb!di:debug-condition ()
760 ;; This is mostly noise.
761 (when (eq :always print-frame-source)
762 (format stream "~& no source available for frame")))
763 (error (c)
764 (format stream "~& error printing frame source: ~A" c)))))))
766 ;;;; INVOKE-DEBUGGER
768 (defvar *debugger-hook* nil
769 "This is either NIL or a function of two arguments, a condition and the value
770 of *DEBUGGER-HOOK*. This function can either handle the condition or return
771 which causes the standard debugger to execute. The system passes the value
772 of this variable to the function because it binds *DEBUGGER-HOOK* to NIL
773 around the invocation.")
775 (defvar *invoke-debugger-hook* nil
776 "This is either NIL or a designator for a function of two arguments,
777 to be run when the debugger is about to be entered. The function is
778 run with *INVOKE-DEBUGGER-HOOK* bound to NIL to minimize recursive
779 errors, and receives as arguments the condition that triggered
780 debugger entry and the previous value of *INVOKE-DEBUGGER-HOOK*
782 This mechanism is an SBCL extension similar to the standard *DEBUGGER-HOOK*.
783 In contrast to *DEBUGGER-HOOK*, it is observed by INVOKE-DEBUGGER even when
784 called by BREAK.")
786 ;;; These are bound on each invocation of INVOKE-DEBUGGER.
787 (defvar *debug-restarts*)
788 (defvar *debug-condition*)
789 (defvar *nested-debug-condition*)
791 ;;; Oh, what a tangled web we weave when we preserve backwards
792 ;;; compatibility with 1968-style use of global variables to control
793 ;;; per-stream i/o properties; there's really no way to get this
794 ;;; quite right, but we do what we can.
795 (defun funcall-with-debug-io-syntax (fun &rest rest)
796 (declare (type function fun))
797 ;; Try to force the other special variables into a useful state.
798 (let (;; Protect from WITH-STANDARD-IO-SYNTAX some variables where
799 ;; any default we might use is less useful than just reusing
800 ;; the global values.
801 (original-package *package*)
802 (original-print-pretty *print-pretty*))
803 (with-standard-io-syntax
804 (with-sane-io-syntax
805 (let (;; We want the printer and reader to be in a useful
806 ;; state, regardless of where the debugger was invoked
807 ;; in the program. WITH-STANDARD-IO-SYNTAX and
808 ;; WITH-SANE-IO-SYNTAX do much of what we want, but
809 ;; * It doesn't affect our internal special variables
810 ;; like *CURRENT-LEVEL-IN-PRINT*.
811 ;; * It isn't customizable.
812 ;; * It sets *PACKAGE* to COMMON-LISP-USER, which is not
813 ;; helpful behavior for a debugger.
814 ;; * There's no particularly good debugger default for
815 ;; *PRINT-PRETTY*, since T is usually what you want
816 ;; -- except absolutely not what you want when you're
817 ;; debugging failures in PRINT-OBJECT logic.
818 ;; We try to address all these issues with explicit
819 ;; rebindings here.
820 (*current-level-in-print* 0)
821 (*package* original-package)
822 (*print-pretty* original-print-pretty)
823 ;; Clear the circularity machinery to try to to reduce the
824 ;; pain from sharing the circularity table across all
825 ;; streams; if these are not rebound here, then setting
826 ;; *PRINT-CIRCLE* within the debugger when debugging in a
827 ;; state where something circular was being printed (e.g.,
828 ;; because the debugger was entered on an error in a
829 ;; PRINT-OBJECT method) makes a hopeless mess. Binding them
830 ;; here does seem somewhat ugly because it makes it more
831 ;; difficult to debug the printing-of-circularities code
832 ;; itself; however, as far as I (WHN, 2004-05-29) can see,
833 ;; that's almost entirely academic as long as there's one
834 ;; shared *C-H-T* for all streams (i.e., it's already
835 ;; unreasonably difficult to debug print-circle machinery
836 ;; given the buggy crosstalk between the debugger streams
837 ;; and the stream you're trying to watch), and any fix for
838 ;; that buggy arrangement will likely let this hack go away
839 ;; naturally.
840 (sb!impl::*circularity-hash-table* . nil)
841 (sb!impl::*circularity-counter* . nil)
842 (*readtable* *debug-readtable*))
843 (progv
844 ;; (Why NREVERSE? PROGV makes the later entries have
845 ;; precedence over the earlier entries.
846 ;; *DEBUG-PRINT-VARIABLE-ALIST* is called an alist, so it's
847 ;; expected that its earlier entries have precedence. And
848 ;; the earlier-has-precedence behavior is mostly more
849 ;; convenient, so that programmers can use PUSH or LIST* to
850 ;; customize *DEBUG-PRINT-VARIABLE-ALIST*.)
851 (nreverse (mapcar #'car *debug-print-variable-alist*))
852 (nreverse (mapcar #'cdr *debug-print-variable-alist*))
853 (apply fun rest)))))))
855 ;;; This function is not inlined so it shows up in the backtrace; that
856 ;;; can be rather handy when one has to debug the interplay between
857 ;;; *INVOKE-DEBUGGER-HOOK* and *DEBUGGER-HOOK*.
858 (declaim (notinline run-hook))
859 (defun run-hook (variable condition)
860 (let ((old-hook (symbol-value variable)))
861 (when old-hook
862 (progv (list variable) (list nil)
863 (funcall old-hook condition old-hook)))))
865 ;;; We can bind *stack-top-hint* to a symbol, in which case this function will
866 ;;; resolve that hint lazily before we enter the debugger.
867 (defun resolve-stack-top-hint ()
868 (let ((hint *stack-top-hint*)
869 (*stack-top-hint* nil))
870 (cond
871 ;; No hint, just keep the debugger guts out.
872 ((not hint)
873 (find-caller-frame))
874 ;; Interrupted. Look for the interrupted frame -- if we don't find one
875 ;; this falls back to the next case.
876 ((and (eq hint 'invoke-interruption)
877 (find-interrupted-frame)))
878 ;; Name of the first uninteresting frame.
879 ((symbolp hint)
880 (find-caller-of-named-frame hint))
881 ;; We already have a resolved hint.
883 hint))))
885 (defun invoke-debugger (condition)
886 "Enter the debugger."
887 (let ((*stack-top-hint* (resolve-stack-top-hint)))
888 ;; call *INVOKE-DEBUGGER-HOOK* first, so that *DEBUGGER-HOOK* is not
889 ;; called when the debugger is disabled
890 (run-hook '*invoke-debugger-hook* condition)
891 (run-hook '*debugger-hook* condition)
892 ;; We definitely want *PACKAGE* to be of valid type.
894 ;; Elsewhere in the system, we use the SANE-PACKAGE function for
895 ;; this, but here causing an exception just as we're trying to handle
896 ;; an exception would be confusing, so instead we use a special hack.
897 (unless (package-name *package*)
898 (setf *package* (find-package :cl-user))
899 (format *error-output*
900 "The value of ~S was not an undeleted PACKAGE. It has been ~
901 reset to ~S."
902 '*package* *package*))
903 ;; Before we start our own output, finish any pending output.
904 ;; Otherwise, if the user tried to track the progress of his program
905 ;; using PRINT statements, he'd tend to lose the last line of output
906 ;; or so, which'd be confusing.
907 (flush-standard-output-streams)
908 (funcall-with-debug-io-syntax #'%invoke-debugger condition)))
910 (defun %print-debugger-invocation-reason (condition stream)
911 (format stream "~2&")
912 ;; Note: Ordinarily it's only a matter of taste whether to use
913 ;; FORMAT "~<...~:>" or to use PPRINT-LOGICAL-BLOCK directly, but
914 ;; until bug 403 is fixed, PPRINT-LOGICAL-BLOCK (STREAM NIL) is
915 ;; definitely preferred, because the FORMAT alternative was acting odd.
916 (pprint-logical-block (stream nil)
917 (format stream
918 "debugger invoked on a ~S~@[ in thread ~_~A~]: ~2I~_~A"
919 (type-of condition)
920 #!+sb-thread sb!thread:*current-thread*
921 #!-sb-thread nil
922 condition))
923 (terpri stream))
925 (defun %invoke-debugger (condition)
926 (let ((*debug-condition* condition)
927 (*debug-restarts* (compute-restarts condition))
928 (*nested-debug-condition* nil))
929 (handler-case
930 ;; (The initial output here goes to *ERROR-OUTPUT*, because the
931 ;; initial output is not interactive, just an error message, and
932 ;; when people redirect *ERROR-OUTPUT*, they could reasonably
933 ;; expect to see error messages logged there, regardless of what
934 ;; the debugger does afterwards.)
935 (unless (typep condition 'step-condition)
936 (%print-debugger-invocation-reason condition *error-output*))
937 (error (condition)
938 (setf *nested-debug-condition* condition)
939 (let ((ndc-type (type-of *nested-debug-condition*)))
940 (format *error-output*
941 "~&~@<(A ~S was caught when trying to print ~S when ~
942 entering the debugger. Printing was aborted and the ~
943 ~S was stored in ~S.)~@:>~%"
944 ndc-type
945 '*debug-condition*
946 ndc-type
947 '*nested-debug-condition*))
948 (when (typep *nested-debug-condition* 'cell-error)
949 ;; what we really want to know when it's e.g. an UNBOUND-VARIABLE:
950 (format *error-output*
951 "~&(CELL-ERROR-NAME ~S) = ~S~%"
952 '*nested-debug-condition*
953 (cell-error-name *nested-debug-condition*)))))
955 (let ((background-p (sb!thread::debugger-wait-until-foreground-thread
956 *debug-io*)))
958 ;; After the initial error/condition/whatever announcement to
959 ;; *ERROR-OUTPUT*, we become interactive, and should talk on
960 ;; *DEBUG-IO* from now on. (KLUDGE: This is a normative
961 ;; statement, not a description of reality.:-| There's a lot of
962 ;; older debugger code which was written to do i/o on whatever
963 ;; stream was in fashion at the time, and not all of it has
964 ;; been converted to behave this way. -- WHN 2000-11-16)
966 (unwind-protect
967 (let (;; We used to bind *STANDARD-OUTPUT* to *DEBUG-IO*
968 ;; here as well, but that is probably bogus since it
969 ;; removes the users ability to do output to a redirected
970 ;; *S-O*. Now we just rebind it so that users can temporarily
971 ;; frob it. FIXME: This and other "what gets bound when"
972 ;; behaviour should be documented in the manual.
973 (*standard-output* *standard-output*)
974 ;; This seems reasonable: e.g. if the user has redirected
975 ;; *ERROR-OUTPUT* to some log file, it's probably wrong
976 ;; to send errors which occur in interactive debugging to
977 ;; that file, and right to send them to *DEBUG-IO*.
978 (*error-output* *debug-io*))
979 (unless (typep condition 'step-condition)
980 (when *debug-beginner-help-p*
981 (format *debug-io*
982 "~%~@<Type HELP for debugger help, or ~
983 (SB-EXT:EXIT) to exit from SBCL.~:@>~2%"))
984 (show-restarts *debug-restarts* *debug-io*))
985 (internal-debug))
986 (when background-p
987 (sb!thread::release-foreground))))))
989 ;;; this function is for use in *INVOKE-DEBUGGER-HOOK* when ordinary
990 ;;; ANSI behavior has been suppressed by the "--disable-debugger"
991 ;;; command-line option
992 (defun debugger-disabled-hook (condition previous-hook)
993 (declare (ignore previous-hook))
994 ;; There is no one there to interact with, so report the
995 ;; condition and terminate the program.
996 (let ((*suppress-print-errors* t)
997 (condition-error-message
998 #.(format nil "A nested error within --disable-debugger error ~
999 handling prevents displaying the original error. Attempting ~
1000 to print a backtrace."))
1001 (backtrace-error-message
1002 #.(format nil "A nested error within --disable-debugger error ~
1003 handling prevents printing the backtrace. Sorry, exiting.")))
1004 (labels
1005 ((failure-quit (&key abort)
1006 (/show0 "in FAILURE-QUIT (in --disable-debugger debugger hook)")
1007 (exit :code 1 :abort abort))
1008 (display-condition ()
1009 (handler-case
1010 (handler-case
1011 (print-condition)
1012 (condition ()
1013 ;; printing failed, try to describe it
1014 (describe-condition)))
1015 (condition ()
1016 ;; ok, give up trying to display the error and inform the user about it
1017 (finish-output *error-output*)
1018 (%primitive print condition-error-message))))
1019 (print-condition ()
1020 (format *error-output*
1021 "~&~@<Unhandled ~S~@[ in thread ~S~]: ~2I~_~A~:>~2%"
1022 (type-of condition)
1023 #!+sb-thread sb!thread:*current-thread*
1024 #!-sb-thread nil
1025 condition)
1026 (finish-output *error-output*))
1027 (describe-condition ()
1028 (format *error-output*
1029 "~&Unhandled ~S~@[ in thread ~S~]:~%"
1030 (type-of condition)
1031 #!+sb-thread sb!thread:*current-thread*
1032 #!-sb-thread nil)
1033 (describe condition *error-output*)
1034 (finish-output *error-output*))
1035 (display-backtrace ()
1036 (handler-case
1037 (print-backtrace :stream *error-output*
1038 :from :interrupted-frame
1039 :print-thread t
1040 :emergency-best-effort t)
1041 (condition ()
1042 (values)))
1043 (finish-output *error-output*)))
1044 ;; This HANDLER-CASE is here mostly to stop output immediately
1045 ;; (and fall through to QUIT) when there's an I/O error. Thus,
1046 ;; when we're run under a shell script or something, we can die
1047 ;; cleanly when the script dies (and our pipes are cut), instead
1048 ;; of falling into ldb or something messy like that. Similarly, we
1049 ;; can terminate cleanly even if BACKTRACE dies because of bugs in
1050 ;; user PRINT-OBJECT methods. Separate the error handling of the
1051 ;; two phases to maximize the chance of emitting some useful
1052 ;; information.
1053 (handler-case
1054 (progn
1055 (display-condition)
1056 (display-backtrace)
1057 (format *error-output*
1058 "~%unhandled condition in --disable-debugger mode, quitting~%")
1059 (finish-output *error-output*)
1060 (failure-quit))
1061 (condition ()
1062 ;; We IGNORE-ERRORS here because even %PRIMITIVE PRINT can
1063 ;; fail when our output streams are blown away, as e.g. when
1064 ;; we're running under a Unix shell script and it dies somehow
1065 ;; (e.g. because of a SIGINT). In that case, we might as well
1066 ;; just give it up for a bad job, and stop trying to notify
1067 ;; the user of anything.
1069 ;; Actually, the only way I've run across to exercise the
1070 ;; problem is to have more than one layer of shell script.
1071 ;; I have a shell script which does
1072 ;; time nice -10 sh make.sh "$1" 2>&1 | tee make.tmp
1073 ;; and the problem occurs when I interrupt this with Ctrl-C
1074 ;; under Linux 2.2.14-5.0 and GNU bash, version 1.14.7(1).
1075 ;; I haven't figured out whether it's bash, time, tee, Linux, or
1076 ;; what that is responsible, but that it's possible at all
1077 ;; means that we should IGNORE-ERRORS here. -- WHN 2001-04-24
1078 (ignore-errors
1079 (%primitive print backtrace-error-message))
1080 (failure-quit :abort t))))))
1082 (defvar *old-debugger-hook* nil)
1084 ;;; halt-on-failures and prompt-on-failures modes, suitable for
1085 ;;; noninteractive and interactive use respectively
1086 (defun disable-debugger ()
1087 "When invoked, this function will turn off both the SBCL debugger
1088 and LDB (the low-level debugger). See also ENABLE-DEBUGGER."
1089 ;; *DEBUG-IO* used to be set here to *ERROR-OUTPUT* which is sort
1090 ;; of unexpected but mostly harmless, but then ENABLE-DEBUGGER had
1091 ;; to set it to a suitable value again and be very careful,
1092 ;; especially if the user has also set it. -- MG 2005-07-15
1093 (unless (eq *invoke-debugger-hook* 'debugger-disabled-hook)
1094 (setf *old-debugger-hook* *invoke-debugger-hook*
1095 *invoke-debugger-hook* 'debugger-disabled-hook))
1096 ;; This is not inside the UNLESS to ensure that LDB is disabled
1097 ;; regardless of what the old value of *INVOKE-DEBUGGER-HOOK* was.
1098 ;; This might matter for example when restoring a core.
1099 (sb!alien:alien-funcall (sb!alien:extern-alien "disable_lossage_handler"
1100 (function sb!alien:void))))
1102 (defun enable-debugger ()
1103 "Restore the debugger if it has been turned off by DISABLE-DEBUGGER."
1104 (when (eql *invoke-debugger-hook* 'debugger-disabled-hook)
1105 (setf *invoke-debugger-hook* *old-debugger-hook*
1106 *old-debugger-hook* nil))
1107 (sb!alien:alien-funcall (sb!alien:extern-alien "enable_lossage_handler"
1108 (function sb!alien:void))))
1110 (defun show-restarts (restarts s)
1111 (cond ((null restarts)
1112 (format s
1113 "~&(no restarts: If you didn't do this on purpose, ~
1114 please report it as a bug.)~%"))
1116 (format s "~&restarts (invokable by number or by ~
1117 possibly-abbreviated name):~%")
1118 (let ((count 0)
1119 (names-used '(nil))
1120 (max-name-len 0))
1121 (dolist (restart restarts)
1122 (let ((name (restart-name restart)))
1123 (when name
1124 (let ((len (length (princ-to-string name))))
1125 (when (> len max-name-len)
1126 (setf max-name-len len))))))
1127 (unless (zerop max-name-len)
1128 (incf max-name-len 3))
1129 (dolist (restart restarts)
1130 (let ((name (restart-name restart)))
1131 ;; FIXME: maybe it would be better to display later names
1132 ;; in parens instead of brakets, not just omit them fully.
1133 ;; Call BREAK, call BREAK in the debugger, and tell me
1134 ;; it's not confusing looking. --NS 20050310
1135 (cond ((member name names-used)
1136 (format s "~& ~2D: ~V@T~A~%" count max-name-len restart))
1138 (format s "~& ~2D: [~VA] ~A~%"
1139 count (- max-name-len 3) name restart)
1140 (push name names-used))))
1141 (incf count))))))
1143 (defvar *debug-loop-fun* #'debug-loop-fun
1144 "A function taking no parameters that starts the low-level debug loop.")
1146 ;;; When the debugger is invoked due to a stepper condition, we don't
1147 ;;; want to print the current frame before the first prompt for aesthetic
1148 ;;; reasons.
1149 (defvar *suppress-frame-print* nil)
1151 ;;; This calls DEBUG-LOOP, performing some simple initializations
1152 ;;; before doing so. INVOKE-DEBUGGER calls this to actually get into
1153 ;;; the debugger. SB!KERNEL::ERROR-ERROR calls this in emergencies
1154 ;;; to get into a debug prompt as quickly as possible with as little
1155 ;;; risk as possible for stepping on whatever is causing recursive
1156 ;;; errors.
1157 (defun internal-debug ()
1158 (let ((*in-the-debugger* t)
1159 (*read-suppress* nil))
1160 (unless (typep *debug-condition* 'step-condition)
1161 (clear-input *debug-io*))
1162 (let ((*suppress-frame-print* (typep *debug-condition* 'step-condition)))
1163 (funcall *debug-loop-fun*))))
1165 ;;;; DEBUG-LOOP
1167 ;;; Note: This defaulted to T in CMU CL. The changed default in SBCL
1168 ;;; was motivated by desire to play nicely with ILISP.
1169 (defvar *flush-debug-errors* nil
1170 "When set, avoid calling INVOKE-DEBUGGER recursively when errors occur while
1171 executing in the debugger.")
1173 (defun debug-read (stream eof-restart)
1174 (declare (type stream stream))
1175 (let* ((eof-marker (cons nil nil))
1176 (form (read stream nil eof-marker)))
1177 (if (eq form eof-marker)
1178 (invoke-restart eof-restart)
1179 form)))
1181 (defun debug-loop-fun ()
1182 (let* ((*debug-command-level* (1+ *debug-command-level*))
1183 (*real-stack-top* (sb!di:top-frame))
1184 (*stack-top* (or *stack-top-hint* *real-stack-top*))
1185 (*stack-top-hint* nil)
1186 (*current-frame* *stack-top*))
1187 (handler-bind ((sb!di:debug-condition
1188 (lambda (condition)
1189 (princ condition *debug-io*)
1190 (/show0 "handling d-c by THROWing DEBUG-LOOP-CATCHER")
1191 (throw 'debug-loop-catcher nil))))
1192 (cond (*suppress-frame-print*
1193 (setf *suppress-frame-print* nil))
1195 (terpri *debug-io*)
1196 (print-frame-call *current-frame* *debug-io* :print-frame-source t)))
1197 (loop
1198 (catch 'debug-loop-catcher
1199 (handler-bind ((error (lambda (condition)
1200 (when *flush-debug-errors*
1201 (clear-input *debug-io*)
1202 (princ condition *debug-io*)
1203 (format *debug-io*
1204 "~&error flushed (because ~
1205 ~S is set)"
1206 '*flush-debug-errors*)
1207 (/show0 "throwing DEBUG-LOOP-CATCHER")
1208 (throw 'debug-loop-catcher nil)))))
1209 ;; We have to bind LEVEL for the restart function created
1210 ;; by WITH-SIMPLE-RESTART, and we need the explicit ABORT
1211 ;; restart that exists now so that EOF from read can drop
1212 ;; one debugger level.
1213 (let ((level *debug-command-level*)
1214 (restart-commands (make-restart-commands))
1215 (abort-restart-for-eof (find-restart 'abort)))
1216 (flush-standard-output-streams)
1217 (debug-prompt *debug-io*)
1218 (force-output *debug-io*)
1219 (with-simple-restart (abort
1220 "~@<Reduce debugger level (to debug level ~W).~@:>"
1221 level)
1222 (let* ((exp (debug-read *debug-io* abort-restart-for-eof))
1223 (cmd-fun (debug-command-p exp restart-commands)))
1224 (cond ((not cmd-fun)
1225 (debug-eval-print exp))
1226 ((consp cmd-fun)
1227 (format *debug-io*
1228 "~&Your command, ~S, is ambiguous:~%"
1229 exp)
1230 (dolist (ele cmd-fun)
1231 (format *debug-io* " ~A~%" ele)))
1233 (funcall cmd-fun))))))))))))
1235 (defvar *auto-eval-in-frame* t
1236 "When set (the default), evaluations in the debugger's command loop occur
1237 relative to the current frame's environment without the need of debugger
1238 forms that explicitly control this kind of evaluation.")
1240 (defun debug-eval (expr)
1241 (cond ((not (and (fboundp 'compile) *auto-eval-in-frame*))
1242 (eval expr))
1243 ((frame-has-debug-vars-p *current-frame*)
1244 (sb!di:eval-in-frame *current-frame* expr))
1246 (format *debug-io* "; No debug variables for current frame: ~
1247 using EVAL instead of EVAL-IN-FRAME.~%")
1248 (eval expr))))
1250 (defun debug-eval-print (expr)
1251 (/noshow "entering DEBUG-EVAL-PRINT" expr)
1252 (let ((values (multiple-value-list
1253 (interactive-eval expr :eval #'debug-eval))))
1254 (/noshow "done with EVAL in DEBUG-EVAL-PRINT")
1255 (dolist (value values)
1256 (fresh-line *debug-io*)
1257 (prin1 value *debug-io*)))
1258 (force-output *debug-io*))
1260 ;;;; debug loop functions
1262 ;;; These commands are functions, not really commands, so that users
1263 ;;; can get their hands on the values returned.
1265 (defun var-valid-in-frame-p (var location &optional (frame *current-frame*))
1266 ;; arg count errors are checked before anything is set up but they
1267 ;; are reporeted in *elsewhere*, which is after start-pc saved in the
1268 ;; debug function, defeating the checks.
1269 (and (not (sb!di::all-args-available-p frame))
1270 (eq (sb!di:debug-var-validity var location) :valid)))
1272 (eval-when (:execute :compile-toplevel)
1274 (sb!xc:defmacro define-var-operation (ref-or-set &optional value-var)
1275 `(let* ((temp (etypecase name
1276 (symbol (sb!di:debug-fun-symbol-vars
1277 (sb!di:frame-debug-fun *current-frame*)
1278 name))
1279 (simple-string (sb!di:ambiguous-debug-vars
1280 (sb!di:frame-debug-fun *current-frame*)
1281 name))))
1282 (location (sb!di:frame-code-location *current-frame*))
1283 ;; Let's only deal with valid variables.
1284 (vars (remove-if-not (lambda (v)
1285 (var-valid-in-frame-p v location))
1286 temp)))
1287 (declare (list vars))
1288 (cond ((null vars)
1289 (error "No known valid variables match ~S." name))
1290 ((= (length vars) 1)
1291 ,(ecase ref-or-set
1292 (:ref
1293 '(sb!di:debug-var-value (car vars) *current-frame*))
1294 (:set
1295 `(setf (sb!di:debug-var-value (car vars) *current-frame*)
1296 ,value-var))))
1298 ;; Since we have more than one, first see whether we have
1299 ;; any variables that exactly match the specification.
1300 (let* ((name (etypecase name
1301 (symbol (symbol-name name))
1302 (simple-string name)))
1303 ;; FIXME: REMOVE-IF-NOT is deprecated, use STRING/=
1304 ;; instead.
1305 (exact (remove-if-not (lambda (v)
1306 (string= (sb!di:debug-var-symbol-name v)
1307 name))
1308 vars))
1309 (vars (or exact vars)))
1310 (declare (simple-string name)
1311 (list exact vars))
1312 (cond
1313 ;; Check now for only having one variable.
1314 ((= (length vars) 1)
1315 ,(ecase ref-or-set
1316 (:ref
1317 '(sb!di:debug-var-value (car vars) *current-frame*))
1318 (:set
1319 `(setf (sb!di:debug-var-value (car vars) *current-frame*)
1320 ,value-var))))
1321 ;; If there weren't any exact matches, flame about
1322 ;; ambiguity unless all the variables have the same
1323 ;; name.
1324 ((and (not exact)
1325 (find-if-not
1326 (lambda (v)
1327 (string= (sb!di:debug-var-symbol-name v)
1328 (sb!di:debug-var-symbol-name (car vars))))
1329 (cdr vars)))
1330 (error "specification ambiguous:~%~{ ~A~%~}"
1331 (mapcar #'sb!di:debug-var-symbol-name
1332 (delete-duplicates
1333 vars :test #'string=
1334 :key #'sb!di:debug-var-symbol-name))))
1335 ;; All names are the same, so see whether the user
1336 ;; ID'ed one of them.
1337 (id-supplied
1338 (let ((v (find id vars :key #'sb!di:debug-var-id)))
1339 (unless v
1340 (error
1341 "invalid variable ID, ~W: should have been one of ~S"
1343 (mapcar #'sb!di:debug-var-id vars)))
1344 ,(ecase ref-or-set
1345 (:ref
1346 '(sb!di:debug-var-value v *current-frame*))
1347 (:set
1348 `(setf (sb!di:debug-var-value v *current-frame*)
1349 ,value-var)))))
1351 (error "Specify variable ID to disambiguate ~S. Use one of ~S."
1352 name
1353 (mapcar #'sb!di:debug-var-id vars)))))))))
1355 ) ; EVAL-WHEN
1357 ;;; FIXME: This doesn't work. It would be real nice we could make it
1358 ;;; work! Alas, it doesn't seem to work in CMU CL X86 either..
1359 (defun var (name &optional (id 0 id-supplied))
1360 "Return a variable's value if possible. NAME is a simple-string or symbol.
1361 If it is a simple-string, it is an initial substring of the variable's name.
1362 If name is a symbol, it has the same name and package as the variable whose
1363 value this function returns. If the symbol is uninterned, then the variable
1364 has the same name as the symbol, but it has no package.
1366 If name is the initial substring of variables with different names, then
1367 this return no values after displaying the ambiguous names. If name
1368 determines multiple variables with the same name, then you must use the
1369 optional id argument to specify which one you want. If you left id
1370 unspecified, then this returns no values after displaying the distinguishing
1371 id values.
1373 The result of this function is limited to the availability of variable
1374 information. This is SETF'able."
1375 (define-var-operation :ref))
1376 (defun (setf var) (value name &optional (id 0 id-supplied))
1377 (define-var-operation :set value))
1379 ;;; This returns the COUNT'th arg as the user sees it from args, the
1380 ;;; result of SB!DI:DEBUG-FUN-LAMBDA-LIST. If this returns a
1381 ;;; potential DEBUG-VAR from the lambda-list, then the second value is
1382 ;;; T. If this returns a keyword symbol or a value from a rest arg,
1383 ;;; then the second value is NIL.
1385 ;;; FIXME: There's probably some way to merge the code here with
1386 ;;; FRAME-ARGS-AS-LIST. (A fair amount of logic is already shared
1387 ;;; through LAMBDA-LIST-ELEMENT-DISPATCH, but I suspect more could be.)
1388 (declaim (ftype (function (index list)) nth-arg))
1389 (defun nth-arg (count args)
1390 (let ((n count))
1391 (dolist (ele args (error "The argument specification ~S is out of range."
1393 (lambda-list-element-dispatch ele
1394 :required ((if (zerop n) (return (values ele t))))
1395 :optional ((if (zerop n) (return (values (second ele) t))))
1396 :keyword ((cond ((zerop n)
1397 (return (values (second ele) nil)))
1398 ((zerop (decf n))
1399 (return (values (third ele) t)))))
1400 :deleted ((if (zerop n) (return (values ele t))))
1401 :rest ((let ((var (second ele)))
1402 (lambda-var-dispatch var (sb!di:frame-code-location
1403 *current-frame*)
1404 (error "unused &REST argument before n'th argument")
1405 (dolist (value
1406 (sb!di:debug-var-value var *current-frame*)
1407 (error
1408 "The argument specification ~S is out of range."
1410 (if (zerop n)
1411 (return-from nth-arg (values value nil))
1412 (decf n)))
1413 (error "invalid &REST argument before n'th argument")))))
1414 (decf n))))
1416 (defun arg (n)
1417 "Return the N'th argument's value if possible. Argument zero is the first
1418 argument in a frame's default printed representation. Count keyword/value
1419 pairs as separate arguments."
1420 (when (sb!di::all-args-available-p *current-frame*)
1421 (return-from arg
1422 (early-frame-nth-arg n *current-frame*)))
1423 (multiple-value-bind (var lambda-var-p)
1424 (nth-arg n (handler-case (sb!di:debug-fun-lambda-list
1425 (sb!di:frame-debug-fun *current-frame*))
1426 (sb!di:lambda-list-unavailable ()
1427 (error "No argument values are available."))))
1428 (if lambda-var-p
1429 (lambda-var-dispatch var (sb!di:frame-code-location *current-frame*)
1430 (error "Unused arguments have no values.")
1431 (sb!di:debug-var-value var *current-frame*)
1432 (error "invalid argument value"))
1433 var)))
1435 ;;;; machinery for definition of debug loop commands
1437 (defvar *debug-commands* nil)
1439 ;;; Interface to *DEBUG-COMMANDS*. No required arguments in args are
1440 ;;; permitted.
1441 (defmacro !def-debug-command (name args &rest body)
1442 (let ((fun-name (symbolicate name "-DEBUG-COMMAND")))
1443 `(progn
1444 (setf *debug-commands*
1445 (remove ,name *debug-commands* :key #'car :test #'string=))
1446 (defun ,fun-name ,args
1447 (unless *in-the-debugger*
1448 (error "invoking debugger command while outside the debugger"))
1449 ,@body)
1450 (push (cons ,name #',fun-name) *debug-commands*)
1451 ',fun-name)))
1453 (defun !def-debug-command-alias (new-name existing-name)
1454 (let ((pair (assoc existing-name *debug-commands* :test #'string=)))
1455 (unless pair (error "unknown debug command name: ~S" existing-name))
1456 (push (cons new-name (cdr pair)) *debug-commands*))
1457 new-name)
1459 ;;; This takes a symbol and uses its name to find a debugger command,
1460 ;;; using initial substring matching. It returns the command function
1461 ;;; if form identifies only one command, but if form is ambiguous,
1462 ;;; this returns a list of the command names. If there are no matches,
1463 ;;; this returns nil. Whenever the loop that looks for a set of
1464 ;;; possibilities encounters an exact name match, we return that
1465 ;;; command function immediately.
1466 (defun debug-command-p (form &optional other-commands)
1467 (if (or (symbolp form) (integerp form))
1468 (let* ((name
1469 (if (symbolp form)
1470 (symbol-name form)
1471 (format nil "~W" form)))
1472 (len (length name))
1473 (res nil))
1474 (declare (simple-string name)
1475 (fixnum len)
1476 (list res))
1478 ;; Find matching commands, punting if exact match.
1479 (flet ((match-command (ele)
1480 (let* ((str (car ele))
1481 (str-len (length str)))
1482 (declare (simple-string str)
1483 (fixnum str-len))
1484 (cond ((< str-len len))
1485 ((= str-len len)
1486 (when (string= name str :end1 len :end2 len)
1487 (return-from debug-command-p (cdr ele))))
1488 ((string= name str :end1 len :end2 len)
1489 (push ele res))))))
1490 (mapc #'match-command *debug-commands*)
1491 (mapc #'match-command other-commands))
1493 ;; Return the right value.
1494 (cond ((not res) nil)
1495 ((= (length res) 1)
1496 (cdar res))
1497 (t ; Just return the names.
1498 (do ((cmds res (cdr cmds)))
1499 ((not cmds) res)
1500 (setf (car cmds) (caar cmds))))))))
1502 ;;; Return a list of debug commands (in the same format as
1503 ;;; *DEBUG-COMMANDS*) that invoke each active restart.
1505 ;;; Two commands are made for each restart: one for the number, and
1506 ;;; one for the restart name (unless it's been shadowed by an earlier
1507 ;;; restart of the same name, or it is NIL).
1508 (defun make-restart-commands (&optional (restarts *debug-restarts*))
1509 (let ((commands)
1510 (num 0)) ; better be the same as show-restarts!
1511 (dolist (restart restarts)
1512 (let ((name (string (restart-name restart))))
1513 (let ((restart-fun
1514 (lambda ()
1515 (/show0 "in restart-command closure, about to i-r-i")
1516 (invoke-restart-interactively restart))))
1517 (push (cons (prin1-to-string num) restart-fun) commands)
1518 (unless (or (null (restart-name restart))
1519 (find name commands :key #'car :test #'string=))
1520 (push (cons name restart-fun) commands))))
1521 (incf num))
1522 commands))
1524 ;;;; frame-changing commands
1526 (!def-debug-command "UP" ()
1527 (let ((next (sb!di:frame-up *current-frame*)))
1528 (cond (next
1529 (setf *current-frame* next)
1530 (print-frame-call next *debug-io*))
1532 (format *debug-io* "~&Top of stack.")))))
1534 (!def-debug-command "DOWN" ()
1535 (let ((next (sb!di:frame-down *current-frame*)))
1536 (cond (next
1537 (setf *current-frame* next)
1538 (print-frame-call next *debug-io*))
1540 (format *debug-io* "~&Bottom of stack.")))))
1542 (!def-debug-command-alias "D" "DOWN")
1544 (!def-debug-command "BOTTOM" ()
1545 (do ((prev *current-frame* lead)
1546 (lead (sb!di:frame-down *current-frame*) (sb!di:frame-down lead)))
1547 ((null lead)
1548 (setf *current-frame* prev)
1549 (print-frame-call prev *debug-io*))))
1551 (!def-debug-command-alias "B" "BOTTOM")
1553 (!def-debug-command "FRAME" (&optional
1554 (n (read-prompting-maybe "frame number: ")))
1555 (setf *current-frame*
1556 (multiple-value-bind (next-frame-fun limit-string)
1557 (if (< n (sb!di:frame-number *current-frame*))
1558 (values #'sb!di:frame-up "top")
1559 (values #'sb!di:frame-down "bottom"))
1560 (do ((frame *current-frame*))
1561 ((= n (sb!di:frame-number frame))
1562 frame)
1563 (let ((next-frame (funcall next-frame-fun frame)))
1564 (cond (next-frame
1565 (setf frame next-frame))
1567 (format *debug-io*
1568 "The ~A of the stack was encountered.~%"
1569 limit-string)
1570 (return frame)))))))
1571 (print-frame-call *current-frame* *debug-io*))
1573 (!def-debug-command-alias "F" "FRAME")
1575 ;;;; commands for entering and leaving the debugger
1577 (!def-debug-command "TOPLEVEL" ()
1578 (throw 'toplevel-catcher nil))
1580 ;;; make T safe
1581 (!def-debug-command-alias "TOP" "TOPLEVEL")
1583 (!def-debug-command "RESTART" ()
1584 (/show0 "doing RESTART debug-command")
1585 (let ((num (read-if-available :prompt)))
1586 (when (eq num :prompt)
1587 (show-restarts *debug-restarts* *debug-io*)
1588 (write-string "restart: " *debug-io*)
1589 (force-output *debug-io*)
1590 (setf num (read *debug-io*)))
1591 (let ((restart (typecase num
1592 (unsigned-byte
1593 (nth num *debug-restarts*))
1594 (symbol
1595 (find num *debug-restarts* :key #'restart-name
1596 :test (lambda (sym1 sym2)
1597 (string= (symbol-name sym1)
1598 (symbol-name sym2)))))
1600 (format *debug-io* "~S is invalid as a restart name.~%"
1601 num)
1602 (return-from restart-debug-command nil)))))
1603 (/show0 "got RESTART")
1604 (if restart
1605 (invoke-restart-interactively restart)
1606 (princ "There is no such restart." *debug-io*)))))
1608 ;;;; information commands
1610 (!def-debug-command "HELP" ()
1611 ;; CMU CL had a little toy pager here, but "if you aren't running
1612 ;; ILISP (or a smart windowing system, or something) you deserve to
1613 ;; lose", so we've dropped it in SBCL. However, in case some
1614 ;; desperate holdout is running this on a dumb terminal somewhere,
1615 ;; we tell him where to find the message stored as a string.
1616 (format *debug-io*
1617 "~&~A~2%(The HELP string is stored in ~S.)~%"
1618 *debug-help-string*
1619 '*debug-help-string*))
1621 (!def-debug-command-alias "?" "HELP")
1623 (!def-debug-command "ERROR" ()
1624 (format *debug-io* "~A~%" *debug-condition*)
1625 (show-restarts *debug-restarts* *debug-io*))
1627 (!def-debug-command "BACKTRACE" ()
1628 (print-backtrace :count (read-if-available most-positive-fixnum)))
1630 (!def-debug-command "PRINT" ()
1631 (print-frame-call *current-frame* *debug-io*))
1633 (!def-debug-command-alias "P" "PRINT")
1635 (!def-debug-command "LIST-LOCALS" ()
1636 (let ((d-fun (sb!di:frame-debug-fun *current-frame*)))
1637 #!+sb-fasteval
1638 (when (typep (sb!di:debug-fun-name d-fun nil)
1639 '(cons (eql sb!interpreter::.eval.)))
1640 (let ((env (arg 1)))
1641 (when (typep env 'sb!interpreter:basic-env)
1642 (return-from list-locals-debug-command
1643 (sb!interpreter:list-locals env)))))
1644 (if (sb!di:debug-var-info-available d-fun)
1645 (let ((*standard-output* *debug-io*)
1646 (location (sb!di:frame-code-location *current-frame*))
1647 (prefix (read-if-available nil))
1648 (any-p nil)
1649 (any-valid-p nil)
1650 (more-context nil)
1651 (more-count nil))
1652 (dolist (v (sb!di:ambiguous-debug-vars
1653 d-fun
1654 (if prefix (string prefix) "")))
1655 (setf any-p t)
1656 (when (var-valid-in-frame-p v location)
1657 (setf any-valid-p t)
1658 (case (sb!di::debug-var-info v)
1659 (:more-context
1660 (setf more-context (sb!di:debug-var-value v *current-frame*)))
1661 (:more-count
1662 (setf more-count (sb!di:debug-var-value v *current-frame*)))
1664 (format *debug-io* "~S~:[#~W~;~*~] = ~S~%"
1665 (sb!di:debug-var-symbol v)
1666 (zerop (sb!di:debug-var-id v))
1667 (sb!di:debug-var-id v)
1668 (sb!di:debug-var-value v *current-frame*))))))
1669 (when (and more-context more-count)
1670 (format *debug-io* "~S = ~S~%"
1671 'more
1672 (multiple-value-list (sb!c:%more-arg-values more-context 0 more-count))))
1673 (cond
1674 ((not any-p)
1675 (format *debug-io*
1676 "There are no local variables ~@[starting with ~A ~]~
1677 in the function."
1678 prefix))
1679 ((not any-valid-p)
1680 (format *debug-io*
1681 "All variables ~@[starting with ~A ~]currently ~
1682 have invalid values."
1683 prefix))))
1684 (write-line "There is no variable information available."
1685 *debug-io*))))
1687 (!def-debug-command-alias "L" "LIST-LOCALS")
1689 (!def-debug-command "SOURCE" ()
1690 (print (code-location-source-form (sb!di:frame-code-location *current-frame*)
1691 (read-if-available 0))
1692 *debug-io*))
1694 ;;;; source location printing
1696 (defun code-location-source-form (location context &optional (errorp t))
1697 (let* ((start-location (maybe-block-start-location location))
1698 (form-num (sb!di:code-location-form-number start-location)))
1699 (multiple-value-bind (translations form)
1700 (sb!di:get-toplevel-form start-location)
1701 (declare (notinline warn))
1702 (cond ((< form-num (length translations))
1703 (sb!di:source-path-context form
1704 (svref translations form-num)
1705 context))
1707 (funcall (if errorp #'error #'warn)
1708 "~@<Bogus form-number: the source file has ~
1709 probably changed too much to cope with.~:@>"))))))
1712 ;;; start single-stepping
1713 (!def-debug-command "START" ()
1714 (if (typep *debug-condition* 'step-condition)
1715 (format *debug-io* "~&Already single-stepping.~%")
1716 (let ((restart (find-restart 'continue *debug-condition*)))
1717 (cond (restart
1718 (sb!impl::enable-stepping)
1719 (invoke-restart restart))
1721 (format *debug-io* "~&Non-continuable error, cannot start stepping.~%"))))))
1723 (defmacro !def-step-command (command-name restart-name)
1724 `(!def-debug-command ,command-name ()
1725 (if (typep *debug-condition* 'step-condition)
1726 (let ((restart (find-restart ',restart-name *debug-condition*)))
1727 (aver restart)
1728 (invoke-restart restart))
1729 (format *debug-io* "~&Not currently single-stepping. (Use START to activate the single-stepper)~%"))))
1731 (!def-step-command "STEP" step-into)
1732 (!def-step-command "NEXT" step-next)
1733 (!def-step-command "STOP" step-continue)
1735 (!def-debug-command-alias "S" "STEP")
1736 (!def-debug-command-alias "N" "NEXT")
1738 (!def-debug-command "OUT" ()
1739 (if (typep *debug-condition* 'step-condition)
1740 (if sb!impl::*step-out*
1741 (let ((restart (find-restart 'step-out *debug-condition*)))
1742 (aver restart)
1743 (invoke-restart restart))
1744 (format *debug-io* "~&OUT can only be used step out of frames that were originally stepped into with STEP.~%"))
1745 (format *debug-io* "~&Not currently single-stepping. (Use START to activate the single-stepper)~%")))
1747 ;;; miscellaneous commands
1749 (!def-debug-command "DESCRIBE" ()
1750 (let* ((curloc (sb!di:frame-code-location *current-frame*))
1751 (debug-fun (sb!di:code-location-debug-fun curloc))
1752 (function (sb!di:debug-fun-fun debug-fun)))
1753 (if function
1754 (describe function)
1755 (format *debug-io* "can't figure out the function for this frame"))))
1757 (!def-debug-command "SLURP" ()
1758 (loop while (read-char-no-hang *standard-input*)))
1760 ;;; RETURN-FROM-FRAME and RESTART-FRAME
1762 (defun unwind-to-frame-and-call (frame thunk)
1763 #!+unwind-to-frame-and-call-vop
1764 (flet ((sap-int/fixnum (sap)
1765 ;; On unithreaded X86 *BINDING-STACK-POINTER* and
1766 ;; *CURRENT-CATCH-BLOCK* are negative, so we need to jump through
1767 ;; some hoops to make these calculated values negative too.
1768 (ash (truly-the (signed-byte #.sb!vm:n-word-bits)
1769 (sap-int sap))
1770 (- sb!vm::n-fixnum-tag-bits))))
1771 ;; To properly unwind the stack, we need three pieces of information:
1772 ;; * The unwind block that should be active after the unwind
1773 ;; * The catch block that should be active after the unwind
1774 ;; * The values that the binding stack pointer should have after the
1775 ;; unwind.
1776 (let ((block (sap-int/fixnum (find-enclosing-catch-block frame)))
1777 (unbind-to (find-binding-stack-pointer frame)))
1778 ;; This VOP will run the neccessary cleanup forms, reset the fp, and
1779 ;; then call the supplied function.
1780 (sb!vm::%primitive sb!vm::unwind-to-frame-and-call
1781 (sb!di::frame-pointer frame)
1782 (find-enclosing-uwp frame)
1783 (lambda ()
1784 ;; Before calling the user-specified
1785 ;; function, we need to restore the binding
1786 ;; stack and the catch block. The unwind block
1787 ;; is taken care of by the VOP.
1788 (sb!vm::%primitive sb!vm::unbind-to-here
1789 unbind-to)
1790 (setf sb!vm::*current-catch-block* block)
1791 (funcall thunk)))))
1792 #!-unwind-to-frame-and-call-vop
1793 (let ((tag (gensym)))
1794 (sb!di:replace-frame-catch-tag frame
1795 'sb!c:debug-catch-tag
1796 tag)
1797 (throw tag thunk)))
1799 #!+unwind-to-frame-and-call-vop
1800 (defun find-binding-stack-pointer (frame)
1801 (let ((debug-fun (sb!di:frame-debug-fun frame)))
1802 (if (eq (sb!di:debug-fun-kind debug-fun) :external)
1803 ;; XEPs do not bind anything, nothing to restore.
1804 ;; But they may call other code through SATISFIES
1805 ;; declaration, check that the interrupt is actually in the XEP.
1806 (and (sb!di::compiled-frame-escaped frame)
1807 sb!kernel::*interr-current-bsp*)
1808 (let* ((compiled-debug-fun (and
1809 (typep debug-fun 'sb!di::compiled-debug-fun)
1810 (sb!di::compiled-debug-fun-compiler-debug-fun debug-fun)))
1811 (bsp-save-offset (and compiled-debug-fun
1812 (sb!c::compiled-debug-fun-bsp-save compiled-debug-fun))))
1813 (when bsp-save-offset
1814 (sb!di::sub-access-debug-var-slot (sb!di::frame-pointer frame) bsp-save-offset))))))
1816 (defun find-enclosing-catch-block (frame)
1817 ;; Walk the catch block chain looking for the first entry with an address
1818 ;; higher than the pointer for FRAME or a null pointer.
1819 (let* ((frame-pointer (sb!di::frame-pointer frame))
1820 (current-block (int-sap (ldb (byte #.sb!vm:n-word-bits 0)
1821 (ash sb!vm::*current-catch-block*
1822 sb!vm:n-fixnum-tag-bits))))
1823 (enclosing-block (loop for block = current-block
1824 then (sap-ref-sap block
1825 (* sb!vm:catch-block-previous-catch-slot
1826 sb!vm::n-word-bytes))
1827 when (or (zerop (sap-int block))
1828 #!+stack-grows-downward-not-upward
1829 (sap> block frame-pointer)
1830 #!-stack-grows-downward-not-upward
1831 (sap< block frame-pointer))
1832 return block)))
1833 enclosing-block))
1835 (defun find-enclosing-uwp (frame)
1836 ;; Walk the UWP chain looking for the first entry with an address
1837 ;; higher than the pointer for FRAME or a null pointer.
1838 (let* ((frame-pointer (sb!di::frame-pointer frame))
1839 (current-uwp (int-sap (ldb (byte #.sb!vm:n-word-bits 0)
1840 (ash sb!vm::*current-unwind-protect-block*
1841 sb!vm:n-fixnum-tag-bits))))
1842 (enclosing-uwp (loop for uwp-block = current-uwp
1843 then (sap-ref-sap uwp-block
1844 sb!vm:unwind-block-uwp-slot)
1845 when (or (zerop (sap-int uwp-block))
1846 #!+stack-grows-downward-not-upward
1847 (sap> uwp-block frame-pointer)
1848 #!-stack-grows-downward-not-upward
1849 (sap< uwp-block frame-pointer))
1850 return uwp-block)))
1851 enclosing-uwp))
1853 (!def-debug-command "RETURN" (&optional
1854 (return (read-prompting-maybe
1855 "return: ")))
1856 (if (frame-has-debug-tag-p *current-frame*)
1857 (let* ((code-location (sb!di:frame-code-location *current-frame*))
1858 (values (multiple-value-list
1859 (funcall (sb!di:preprocess-for-eval return code-location)
1860 *current-frame*))))
1861 (unwind-to-frame-and-call *current-frame* (lambda ()
1862 (values-list values))))
1863 (format *debug-io*
1864 "~@<can't find a tag for this frame ~
1865 ~2I~_(hint: try increasing the DEBUG optimization quality ~
1866 and recompiling)~:@>")))
1868 (!def-debug-command "RESTART-FRAME" ()
1869 (if (frame-has-debug-tag-p *current-frame*)
1870 (multiple-value-bind (fname args) (frame-call *current-frame*)
1871 (multiple-value-bind (fun arglist ok)
1872 (if (and (legal-fun-name-p fname) (fboundp fname))
1873 (values (fdefinition fname) args t)
1874 (values (sb!di:debug-fun-fun (sb!di:frame-debug-fun *current-frame*))
1875 (frame-args-as-list *current-frame*)
1876 nil))
1877 (when (and fun
1878 (or ok
1879 (y-or-n-p "~@<No global function for the frame, but we ~
1880 do have access to a function object that we ~
1881 can try to call -- but if it is normally part ~
1882 of a closure, then this is NOT going to end well.~_~_~
1883 Try it anyways?~:@>")))
1884 (unwind-to-frame-and-call *current-frame*
1885 (lambda ()
1886 ;; Ensure TCO.
1887 (declare (optimize (debug 0)))
1888 (apply fun arglist))))
1889 (format *debug-io*
1890 "Can't restart ~S: no function for frame."
1891 *current-frame*)))
1892 (format *debug-io*
1893 "~@<Can't restart ~S: tag not found. ~
1894 ~2I~_(hint: try increasing the DEBUG optimization quality ~
1895 and recompiling)~:@>"
1896 *current-frame*)))
1898 (defun frame-has-debug-tag-p (frame)
1899 #!+unwind-to-frame-and-call-vop
1900 ;; XEPs do not bind anything, nothing to restore
1901 (find-binding-stack-pointer frame)
1902 #!-unwind-to-frame-and-call-vop
1903 (find 'sb!c:debug-catch-tag (sb!di::frame-catches frame) :key #'car))
1905 (defun frame-has-debug-vars-p (frame)
1906 (sb!di:debug-var-info-available
1907 (sb!di:code-location-debug-fun
1908 (sb!di:frame-code-location frame))))
1910 ;;;; debug loop command utilities
1912 (defun read-prompting-maybe (prompt)
1913 (unless (sb!int:listen-skip-whitespace *debug-io*)
1914 (princ prompt *debug-io*)
1915 (force-output *debug-io*))
1916 (read *debug-io*))
1918 (defun read-if-available (default)
1919 (if (sb!int:listen-skip-whitespace *debug-io*)
1920 (read *debug-io*)
1921 default))