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