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