3 ;;;; This software is part of the SBCL system. See the README file for
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
31 ;;; to satisfy lambda list
33 ;;; exactly 2 expected, but 5 found
34 (defvar *debug-print-level* 5
36 "*PRINT-LEVEL* for the debugger")
37 (defvar *debug-print-length* 7
39 "*PRINT-LENGTH* for the debugger")
41 (defvar *debug-readtable*
42 ;; KLUDGE: This can't be initialized in a cold toplevel form,
43 ;; because the *STANDARD-READTABLE* isn't initialized until after
44 ;; cold toplevel forms have run. So instead we initialize it
45 ;; immediately after *STANDARD-READTABLE*. -- WHN 20000205
48 "*READTABLE* for the debugger")
50 (defvar *in-the-debugger* nil
52 "This is T while in the debugger.")
54 ;;; nestedness inside debugger command loops
55 (defvar *debug-command-level* 0)
57 ;;; If this is bound before the debugger is invoked, it is used as the
58 ;;; stack top by the debugger.
59 (defvar *stack-top-hint* nil)
61 (defvar *stack-top* nil)
62 (defvar *real-stack-top* nil)
64 (defvar *current-frame* nil)
66 ;;; Beginner-oriented help messages are important because you end up
67 ;;; in the debugger whenever something bad happens, or if you try to
68 ;;; get out of the system with Ctrl-C or (EXIT) or EXIT or whatever.
69 ;;; But after memorizing them the wasted screen space gets annoying..
70 (defvar *debug-beginner-help-p* t
71 "Should the debugger display beginner-oriented help messages?")
73 (defun debug-prompt (stream)
76 (sb!di:frame-number *current-frame*)
77 (> *debug-command-level* 1)
78 *debug-command-level*))
80 (defparameter *debug-help-string*
81 "The prompt is right square brackets, the number indicating how many
82 recursive command loops you are in.
83 Any command may be uniquely abbreviated.
84 The debugger rebinds various special variables for controlling i/o, sometimes
85 to defaults (much like WITH-STANDARD-IO-SYNTAX does) and sometimes to
86 its own special values, e.g. SB-DEBUG:*DEBUG-PRINT-LEVEL*.
87 Debug commands do not affect * and friends, but evaluation in the debug loop
88 does affect these variables.
89 SB-DEBUG:*FLUSH-DEBUG-ERRORS* controls whether errors at the debug prompt
90 drop you into deeper into the debugger.
92 Getting in and out of the debugger:
93 RESTART invokes restart numbered as shown (prompt if not given).
94 ERROR prints the error condition and restart cases.
95 The name of any restart, or its number, is a valid command, and is the same
96 as using RESTART to invoke that restart.
99 U up frame D down frame
100 B bottom frame F n frame n (n=0 for top frame)
103 BACKTRACE [n] shows n frames going down the stack.
104 LIST-LOCALS, L lists locals in current function.
105 PRINT, P displays current function call.
106 SOURCE [n] displays frame's source form with n levels of enclosing forms.
108 Breakpoints and steps:
109 LIST-LOCATIONS [{function | :C}] List the locations for breakpoints.
110 Specify :C for the current frame.
112 LIST-BREAKPOINTS List the active breakpoints.
113 Abbreviations: LB, LBP
114 DELETE-BREAKPOINT [n] Remove breakpoint n or all breakpoints.
115 Abbreviations: DEL, DBP
116 BREAKPOINT {n | :end | :start} [:break form] [:function function]
117 [{:print form}*] [:condition form]
119 Abbreviations: BR, BP
120 STEP [n] Step to the next location or step n times.
122 Function and macro commands:
123 (SB-DEBUG:DEBUG-RETURN expression)
124 Exit the debugger, returning expression's values from the current frame.
126 Return the n'th argument in the current frame.
127 (SB-DEBUG:VAR string-or-symbol [id])
128 Returns the value of the specified variable in the current frame.")
130 ;;; This is used to communicate to DEBUG-LOOP that we are at a step breakpoint.
131 (define-condition step-condition (simple-condition) ())
133 ;;;; breakpoint state
135 (defvar *only-block-start-locations* nil
137 "When true, the LIST-LOCATIONS command only displays block start locations.
138 Otherwise, all locations are displayed.")
140 (defvar *print-location-kind* nil
142 "When true, list the code location type in the LIST-LOCATIONS command.")
144 ;;; a list of the types of code-locations that should not be stepped
145 ;;; to and should not be listed when listing breakpoints
146 (defvar *bad-code-location-types* '(:call-site :internal-error))
147 (declaim (type list *bad-code-location-types*))
149 ;;; code locations of the possible breakpoints
150 (defvar *possible-breakpoints*)
151 (declaim (type list *possible-breakpoints*))
153 ;;; a list of the made and active breakpoints, each is a
154 ;;; BREAKPOINT-INFO structure
155 (defvar *breakpoints* nil)
156 (declaim (type list *breakpoints*))
158 ;;; a list of BREAKPOINT-INFO structures of the made and active step
160 (defvar *step-breakpoints* nil)
161 (declaim (type list *step-breakpoints*))
163 ;;; the number of times left to step
164 (defvar *number-of-steps* 1)
165 (declaim (type integer *number-of-steps*))
167 ;;; This is used when listing and setting breakpoints.
168 (defvar *default-breakpoint-debug-fun* nil)
169 (declaim (type (or list sb!di:debug-fun) *default-breakpoint-debug-fun*))
171 ;;;; code location utilities
173 ;;; Return the first code-location in the passed debug block.
174 (defun first-code-location (debug-block)
176 (first-code-location nil))
177 (sb!di:do-debug-block-locations (code-location debug-block)
179 (setf first-code-location code-location)
181 first-code-location))
183 ;;; Return a list of the next code-locations following the one passed.
184 ;;; One of the *BAD-CODE-LOCATION-TYPES* will not be returned.
185 (defun next-code-locations (code-location)
186 (let ((debug-block (sb!di:code-location-debug-block code-location))
187 (block-code-locations nil))
188 (sb!di:do-debug-block-locations (block-code-location debug-block)
189 (unless (member (sb!di:code-location-kind block-code-location)
190 *bad-code-location-types*)
191 (push block-code-location block-code-locations)))
192 (setf block-code-locations (nreverse block-code-locations))
193 (let* ((code-loc-list (rest (member code-location block-code-locations
194 :test #'sb!di:code-location=)))
195 (next-list (cond (code-loc-list
196 (list (first code-loc-list)))
197 ((map 'list #'first-code-location
198 (sb!di:debug-block-successors debug-block)))
200 (when (and (= (length next-list) 1)
201 (sb!di:code-location= (first next-list) code-location))
202 (setf next-list (next-code-locations (first next-list))))
205 ;;; Return a list of code-locations of the possible breakpoints of DEBUG-FUN.
206 (defun possible-breakpoints (debug-fun)
207 (let ((possible-breakpoints nil))
208 (sb!di:do-debug-fun-blocks (debug-block debug-fun)
209 (unless (sb!di:debug-block-elsewhere-p debug-block)
210 (if *only-block-start-locations*
211 (push (first-code-location debug-block) possible-breakpoints)
212 (sb!di:do-debug-block-locations (code-location debug-block)
213 (when (not (member (sb!di:code-location-kind code-location)
214 *bad-code-location-types*))
215 (push code-location possible-breakpoints))))))
216 (nreverse possible-breakpoints)))
218 ;;; Search the info-list for the item passed (CODE-LOCATION,
219 ;;; DEBUG-FUN, or BREAKPOINT-INFO). If the item passed is a debug
220 ;;; function then kind will be compared if it was specified. The kind
221 ;;; if also compared if a breakpoint-info is passed since it's in the
222 ;;; breakpoint. The info structure is returned if found.
223 (defun location-in-list (place info-list &optional (kind nil))
224 (when (breakpoint-info-p place)
225 (setf kind (sb!di:breakpoint-kind (breakpoint-info-breakpoint place)))
226 (setf place (breakpoint-info-place place)))
227 (cond ((sb!di:code-location-p place)
228 (find place info-list
229 :key #'breakpoint-info-place
230 :test (lambda (x y) (and (sb!di:code-location-p y)
231 (sb!di:code-location= x y)))))
233 (find place info-list
234 :test (lambda (x-debug-fun y-info)
235 (let ((y-place (breakpoint-info-place y-info))
236 (y-breakpoint (breakpoint-info-breakpoint
238 (and (sb!di:debug-fun-p y-place)
239 (eq x-debug-fun y-place)
241 (eq kind (sb!di:breakpoint-kind
242 y-breakpoint))))))))))
244 ;;; If LOC is an unknown location, then try to find the block start
245 ;;; location. Used by source printing to some information instead of
246 ;;; none for the user.
247 (defun maybe-block-start-location (loc)
248 (if (sb!di:code-location-unknown-p loc)
249 (let* ((block (sb!di:code-location-debug-block loc))
250 (start (sb!di:do-debug-block-locations (loc block)
252 (cond ((and (not (sb!di:debug-block-elsewhere-p block))
254 ;; FIXME: Why output on T instead of *DEBUG-FOO* or something?
255 (format t "~%unknown location: using block start~%")
261 ;;;; the BREAKPOINT-INFO structure
263 ;;; info about a made breakpoint
264 (defstruct (breakpoint-info (:copier nil))
265 ;; where we are going to stop
266 (place (missing-arg) :type (or sb!di:code-location sb!di:debug-fun))
267 ;; the breakpoint returned by sb!di:make-breakpoint
268 (breakpoint (missing-arg) :type sb!di:breakpoint)
269 ;; the function returned from SB!DI:PREPROCESS-FOR-EVAL. If result is
270 ;; non-NIL, drop into the debugger.
271 (break #'identity :type function)
272 ;; the function returned from sb!di:preprocess-for-eval. If result is
273 ;; non-NIL, eval (each) print and print results.
274 (condition #'identity :type function)
275 ;; the list of functions from sb!di:preprocess-for-eval to evaluate.
276 ;; Results are conditionally printed. Car of each element is the
277 ;; function, cdr is the form it goes with.
278 (print nil :type list)
279 ;; the number used when listing the possible breakpoints within a
280 ;; function. Could also be a symbol such as start or end.
281 (code-location-number (missing-arg) :type (or symbol integer))
282 ;; the number used when listing the breakpoints active and to delete
284 (breakpoint-number (missing-arg) :type integer))
286 ;;; Return a new BREAKPOINT-INFO structure with the info passed.
287 (defun create-breakpoint-info (place breakpoint code-location-number
288 &key (break #'identity)
289 (condition #'identity) (print nil))
291 (sort *breakpoints* #'< :key #'breakpoint-info-breakpoint-number))
292 (let ((breakpoint-number
293 (do ((i 1 (incf i)) (breakpoints *breakpoints* (rest breakpoints)))
294 ((or (> i (length *breakpoints*))
295 (not (= i (breakpoint-info-breakpoint-number
296 (first breakpoints)))))
299 (make-breakpoint-info :place place :breakpoint breakpoint
300 :code-location-number code-location-number
301 :breakpoint-number breakpoint-number
302 :break break :condition condition :print print)))
304 ;;; Print the breakpoint info for the breakpoint-info structure passed.
305 (defun print-breakpoint-info (breakpoint-info)
306 (let ((place (breakpoint-info-place breakpoint-info))
307 (bp-number (breakpoint-info-breakpoint-number breakpoint-info))
308 (loc-number (breakpoint-info-code-location-number breakpoint-info)))
309 (case (sb!di:breakpoint-kind (breakpoint-info-breakpoint breakpoint-info))
311 (print-code-location-source-form place 0)
316 (sb!di:debug-fun-name (sb!di:code-location-debug-fun
319 (format t "~&~S: FUN-START in ~S" bp-number
320 (sb!di:debug-fun-name place)))
322 (format t "~&~S: FUN-END in ~S" bp-number
323 (sb!di:debug-fun-name place))))))
325 ;;;; MAIN-HOOK-FUN for steps and breakpoints
327 ;;; This must be passed as the hook function. It keeps track of where
328 ;;; STEP breakpoints are.
329 (defun main-hook-fun (current-frame breakpoint &optional return-vals
331 (setf *default-breakpoint-debug-fun*
332 (sb!di:frame-debug-fun current-frame))
333 (dolist (step-info *step-breakpoints*)
334 (sb!di:delete-breakpoint (breakpoint-info-breakpoint step-info))
335 (let ((bp-info (location-in-list step-info *breakpoints*)))
337 (sb!di:activate-breakpoint (breakpoint-info-breakpoint bp-info)))))
338 (let ((*stack-top-hint* current-frame)
340 (location-in-list (sb!di:breakpoint-what breakpoint)
342 (sb!di:breakpoint-kind breakpoint)))
344 (location-in-list (sb!di:breakpoint-what breakpoint)
346 (sb!di:breakpoint-kind breakpoint)))
350 (setf *step-breakpoints* nil)
351 (labels ((build-string (str)
352 (setf string (concatenate 'string string str)))
353 (print-common-info ()
355 (with-output-to-string (*standard-output*)
357 (format t "~%Return values: ~S" return-vals))
359 (when (breakpoint-info-print bp-hit-info)
361 (print-frame-call current-frame))
362 (dolist (print (breakpoint-info-print bp-hit-info))
363 (format t "~& ~S = ~S" (rest print)
364 (funcall (first print) current-frame))))))))
366 (setf break (funcall (breakpoint-info-break bp-hit-info)
368 (setf condition (funcall (breakpoint-info-condition bp-hit-info)
370 (cond ((and bp-hit-info step-hit-info (= 1 *number-of-steps*))
371 (build-string (format nil "~&*Step (to a breakpoint)*"))
374 ((and bp-hit-info step-hit-info break)
375 (build-string (format nil "~&*Step (to a breakpoint)*"))
378 ((and bp-hit-info step-hit-info)
380 (format t "~A" string)
381 (decf *number-of-steps*)
382 (set-step-breakpoint current-frame))
383 ((and step-hit-info (= 1 *number-of-steps*))
384 (build-string "*Step*")
385 (break (make-condition 'step-condition :format-control string)))
387 (decf *number-of-steps*)
388 (set-step-breakpoint current-frame))
391 (build-string (format nil "~&*Breakpoint hit*")))
395 (format t "~A" string)))
397 (break "unknown breakpoint"))))))
399 ;;; Set breakpoints at the next possible code-locations. After calling
400 ;;; this, either (CONTINUE) if in the debugger or just let program flow
401 ;;; return if in a hook function.
402 (defun set-step-breakpoint (frame)
404 ((sb!di:debug-block-elsewhere-p (sb!di:code-location-debug-block
405 (sb!di:frame-code-location frame)))
406 ;; FIXME: FORMAT T is used for error output here and elsewhere in
408 (format t "cannot step, in elsewhere code~%"))
410 (let* ((code-location (sb!di:frame-code-location frame))
411 (next-code-locations (next-code-locations code-location)))
414 (dolist (code-location next-code-locations)
415 (let ((bp-info (location-in-list code-location *breakpoints*)))
417 (sb!di:deactivate-breakpoint (breakpoint-info-breakpoint
419 (let ((bp (sb!di:make-breakpoint #'main-hook-fun code-location
420 :kind :code-location)))
421 (sb!di:activate-breakpoint bp)
422 (push (create-breakpoint-info code-location bp 0)
423 *step-breakpoints*))))
425 (let* ((debug-fun (sb!di:frame-debug-fun *current-frame*))
426 (bp (sb!di:make-breakpoint #'main-hook-fun debug-fun
428 (sb!di:activate-breakpoint bp)
429 (push (create-breakpoint-info debug-fun bp 0)
430 *step-breakpoints*))))))))
434 ;;; ANSI specifies that this macro shall exist, even if only as a
435 ;;; trivial placeholder like this.
436 (defmacro step (form)
437 "a trivial placeholder implementation of the CL:STEP macro required by
444 (defun backtrace (&optional (count most-positive-fixnum)
445 (*standard-output* *debug-io*))
447 "Show a listing of the call stack going down from the current frame. In the
448 debugger, the current frame is indicated by the prompt. COUNT is how many
450 (fresh-line *standard-output*)
451 (do ((frame (if *in-the-debugger* *current-frame* (sb!di:top-frame))
452 (sb!di:frame-down frame))
453 (count count (1- count)))
454 ((or (null frame) (zerop count)))
455 (print-frame-call frame :number t))
456 (fresh-line *standard-output*)
461 (eval-when (:compile-toplevel :execute)
463 ;;; This is a convenient way to express what to do for each type of
464 ;;; lambda-list element.
465 (sb!xc:defmacro lambda-list-element-dispatch (element
476 (ecase (car ,element)
477 (:optional ,@optional)
479 (:keyword ,@keyword)))
481 (aver (eq ,element :deleted))
484 (sb!xc:defmacro lambda-var-dispatch (variable location deleted valid other)
485 (let ((var (gensym)))
486 `(let ((,var ,variable))
487 (cond ((eq ,var :deleted) ,deleted)
488 ((eq (sb!di:debug-var-validity ,var ,location) :valid)
494 ;;; This is used in constructing arg lists for debugger printing when
495 ;;; the arg list is unavailable, some arg is unavailable or unused, etc.
496 (defstruct (unprintable-object
497 (:constructor make-unprintable-object (string))
498 (:print-object (lambda (x s)
499 (print-unreadable-object (x s)
500 (write-string (unprintable-object-string x)
505 ;;; Print FRAME with verbosity level 1. If we hit a &REST arg, then
506 ;;; print as many of the values as possible, punting the loop over
507 ;;; lambda-list variables since any other arguments will be in the
508 ;;; &REST arg's list of values.
509 (defun print-frame-call-1 (frame)
510 (let ((debug-fun (sb!di:frame-debug-fun frame))
511 (loc (sb!di:frame-code-location frame))
514 ;; Construct function arguments in REVERSED-ARGS.
516 (dolist (ele (sb!di:debug-fun-lambda-list debug-fun))
517 (lambda-list-element-dispatch ele
518 :required ((push (frame-call-arg ele loc frame) reversed-args))
519 :optional ((push (frame-call-arg (second ele) loc frame)
521 :keyword ((push (second ele) reversed-args)
522 (push (frame-call-arg (third ele) loc frame)
524 :deleted ((push (frame-call-arg ele loc frame) reversed-args))
525 :rest ((lambda-var-dispatch (second ele) loc
529 (append (reverse (sb!di:debug-var-value
533 (push (make-unprintable-object
534 "unavailable &REST argument")
536 (sb!di:lambda-list-unavailable
538 (push (make-unprintable-object "lambda list unavailable")
541 (pprint-logical-block (*standard-output* nil :prefix "(" :suffix ")")
542 (let ((args (nreverse (mapcar #'ensure-printable-object reversed-args))))
543 ;; Since we go to some trouble to make nice informative function
544 ;; names like (PRINT-OBJECT :AROUND (CLOWN T)), let's make sure
545 ;; that they aren't truncated by *PRINT-LENGTH* and *PRINT-LEVEL*.
546 (let ((*print-length* nil)
548 (prin1 (ensure-printable-object (sb!di:debug-fun-name debug-fun))))
549 ;; For the function arguments, we can just print normally.
550 (format t "~{ ~_~S~}" args)))
552 (when (sb!di:debug-fun-kind debug-fun)
554 (prin1 (sb!di:debug-fun-kind debug-fun))
557 (defun ensure-printable-object (object)
559 (with-open-stream (out (make-broadcast-stream))
563 (declare (ignore cond))
564 (make-unprintable-object "error printing object"))))
566 (defun frame-call-arg (var location frame)
567 (lambda-var-dispatch var location
568 (make-unprintable-object "unused argument")
569 (sb!di:debug-var-value var frame)
570 (make-unprintable-object "unavailable argument")))
572 ;;; Prints a representation of the function call causing FRAME to
573 ;;; exist. VERBOSITY indicates the level of information to output;
574 ;;; zero indicates just printing the DEBUG-FUN's name, and one
575 ;;; indicates displaying call-like, one-liner format with argument
577 (defun print-frame-call (frame &key (verbosity 1) (number nil))
581 (format t "~&~S: " (sb!di:frame-number frame)))
582 (format t "~S" frame))
585 (format t "~&~S: " (sb!di:frame-number frame)))
586 (print-frame-call-1 frame)))
587 (when (>= verbosity 2)
588 (let ((loc (sb!di:frame-code-location frame)))
591 (sb!di:code-location-debug-block loc)
592 (format t "~%source: ")
593 (print-code-location-source-form loc 0))
594 (sb!di:debug-condition (ignore) ignore)
595 (error (c) (format t "error finding source: ~A" c))))))
599 (defvar *debugger-hook* nil
601 "This is either NIL or a function of two arguments, a condition and the value
602 of *DEBUGGER-HOOK*. This function can either handle the condition or return
603 which causes the standard debugger to execute. The system passes the value
604 of this variable to the function because it binds *DEBUGGER-HOOK* to NIL
605 around the invocation.")
607 ;;; These are bound on each invocation of INVOKE-DEBUGGER.
608 (defvar *debug-restarts*)
609 (defvar *debug-condition*)
610 (defvar *nested-debug-condition*)
612 (defun invoke-debugger (condition)
614 "Enter the debugger."
615 (let ((old-hook *debugger-hook*))
617 (let ((*debugger-hook* nil))
618 (funcall old-hook condition old-hook))))
619 ;; FIXME: No-one seems to know what this is for. Nothing is noticeably
620 ;; broken on sunos...
621 #!-sunos (sb!unix:unix-sigsetmask 0)
623 ;; Elsewhere in the system, we use the SANE-PACKAGE function for
624 ;; this, but here causing an exception just as we're trying to handle
625 ;; an exception would be confusing, so instead we use a special hack.
626 (unless (and (packagep *package*)
627 (package-name *package*))
628 (setf *package* (find-package :cl-user))
629 (format *error-output*
630 "The value of ~S was not an undeleted PACKAGE. It has been
632 '*package* *package*))
633 (let (;; Save *PACKAGE* to protect it from WITH-STANDARD-IO-SYNTAX.
634 (original-package *package*))
635 (with-standard-io-syntax
636 (let* ((*debug-condition* condition)
637 (*debug-restarts* (compute-restarts condition))
638 ;; We want the i/o subsystem to be in a known, useful
639 ;; state, regardless of where the debugger was invoked in
640 ;; the program. WITH-STANDARD-IO-SYNTAX does some of that,
642 ;; 1. It doesn't affect our internal special variables
643 ;; like *CURRENT-LEVEL-IN-PRINT*.
644 ;; 2. It isn't customizable.
645 ;; 3. It doesn't set *PRINT-READABLY* or *PRINT-PRETTY*
646 ;; to the same value as the toplevel default.
647 ;; 4. It sets *PACKAGE* to COMMON-LISP-USER, which is not
648 ;; helpful behavior for a debugger.
649 ;; We try to remedy all these problems with explicit
651 (sb!kernel:*current-level-in-print* 0)
652 (*print-length* *debug-print-length*)
653 (*print-level* *debug-print-level*)
654 (*readtable* *debug-readtable*)
655 (*print-readably* nil)
657 (*package* original-package)
658 (*nested-debug-condition* nil))
660 ;; Before we start our own output, finish any pending output.
661 ;; Otherwise, if the user tried to track the progress of
662 ;; his program using PRINT statements, he'd tend to lose
663 ;; the last line of output or so, and get confused.
664 (flush-standard-output-streams)
666 ;; (The initial output here goes to *ERROR-OUTPUT*, because the
667 ;; initial output is not interactive, just an error message,
668 ;; and when people redirect *ERROR-OUTPUT*, they could
669 ;; reasonably expect to see error messages logged there,
670 ;; regardless of what the debugger does afterwards.)
672 (format *error-output*
673 "~2&~@<debugger invoked on condition of type ~S: ~
675 (type-of *debug-condition*)
678 (setf *nested-debug-condition* condition)
679 (let ((ndc-type (type-of *nested-debug-condition*)))
680 (format *error-output*
681 "~&~@<(A ~S was caught when trying to print ~S when ~
682 entering the debugger. Printing was aborted and the ~
683 ~S was stored in ~S.)~@:>~%"
687 '*nested-debug-condition*))
688 (when (typep condition 'cell-error)
689 ;; what we really want to know when it's e.g. an UNBOUND-VARIABLE:
690 (format *error-output*
691 "~&(CELL-ERROR-NAME ~S) = ~S~%"
693 (cell-error-name *debug-condition*)))))
695 ;; After the initial error/condition/whatever announcement to
696 ;; *ERROR-OUTPUT*, we become interactive, and should talk on
697 ;; *DEBUG-IO* from now on. (KLUDGE: This is a normative
698 ;; statement, not a description of reality.:-| There's a lot of
699 ;; older debugger code which was written to do i/o on whatever
700 ;; stream was in fashion at the time, and not all of it has
701 ;; been converted to behave this way. -- WHN 2000-11-16)
702 (let (;; FIXME: The first two bindings here seem wrong,
703 ;; violating the principle of least surprise, and making
704 ;; it impossible for the user to do reasonable things
705 ;; like using PRINT at the debugger prompt to send output
706 ;; to the program's ordinary (possibly
707 ;; redirected-to-a-file) *STANDARD-OUTPUT*, or using
708 ;; PEEK-CHAR or some such thing on the program's ordinary
709 ;; (possibly also redirected) *STANDARD-INPUT*.
710 (*standard-input* *debug-io*)
711 (*standard-output* *debug-io*)
712 ;; This seems reasonable: e.g. if the user has redirected
713 ;; *ERROR-OUTPUT* to some log file, it's probably wrong
714 ;; to send errors which occur in interactive debugging to
715 ;; that file, and right to send them to *DEBUG-IO*.
716 (*error-output* *debug-io*))
717 (unless (typep condition 'step-condition)
718 (when *debug-beginner-help-p*
720 "~%~@<Within the debugger, you can type HELP for help. ~
721 At any command prompt (within the debugger or not) you ~
722 can type (SB-EXT:QUIT) to terminate the SBCL ~
723 executable. The condition which caused the debugger to ~
724 be entered is bound to ~S. You can suppress this ~
725 message by clearing ~S.~:@>~2%"
727 '*debug-beginner-help-p*))
728 (show-restarts *debug-restarts* *debug-io*))
729 (internal-debug))))))
731 (defun show-restarts (restarts s)
732 (cond ((null restarts)
734 "~&(no restarts: If you didn't do this on purpose, ~
735 please report it as a bug.)~%"))
737 (format s "~&restarts:~%")
741 (dolist (restart restarts)
742 (let ((name (restart-name restart)))
744 (let ((len (length (princ-to-string name))))
745 (when (> len max-name-len)
746 (setf max-name-len len))))))
747 (unless (zerop max-name-len)
748 (incf max-name-len 3))
749 (dolist (restart restarts)
750 (let ((name (restart-name restart)))
751 (cond ((member name names-used)
752 (format s "~& ~2D: ~V@T~A~%" count max-name-len restart))
754 (format s "~& ~2D: [~VA] ~A~%"
755 count (- max-name-len 3) name restart)
756 (push name names-used))))
759 ;;; This calls DEBUG-LOOP, performing some simple initializations
760 ;;; before doing so. INVOKE-DEBUGGER calls this to actually get into
761 ;;; the debugger. SB!KERNEL::ERROR-ERROR calls this in emergencies
762 ;;; to get into a debug prompt as quickly as possible with as little
763 ;;; risk as possible for stepping on whatever is causing recursive
765 (defun internal-debug ()
766 (let ((*in-the-debugger* t)
767 (*read-suppress* nil))
768 (unless (typep *debug-condition* 'step-condition)
769 (clear-input *debug-io*))
771 #!+mp (sb!mp:without-scheduling (debug-loop))))
775 ;;; Note: This defaulted to T in CMU CL. The changed default in SBCL
776 ;;; was motivated by desire to play nicely with ILISP.
777 (defvar *flush-debug-errors* nil
779 "When set, avoid calling INVOKE-DEBUGGER recursively when errors occur while
780 executing in the debugger.")
783 (let* ((*debug-command-level* (1+ *debug-command-level*))
784 (*real-stack-top* (sb!di:top-frame))
785 (*stack-top* (or *stack-top-hint* *real-stack-top*))
786 (*stack-top-hint* nil)
787 (*current-frame* *stack-top*))
788 (handler-bind ((sb!di:debug-condition
790 (princ condition *debug-io*)
791 (/show0 "handling d-c by THROWing DEBUG-LOOP-CATCHER")
792 (throw 'debug-loop-catcher nil))))
794 (print-frame-call *current-frame* :verbosity 2)
796 (catch 'debug-loop-catcher
797 (handler-bind ((error (lambda (condition)
798 (when *flush-debug-errors*
799 (clear-input *debug-io*)
801 ;; FIXME: Doing input on *DEBUG-IO*
802 ;; and output on T seems broken.
804 "~&error flushed (because ~
806 '*flush-debug-errors*)
807 (/show0 "throwing DEBUG-LOOP-CATCHER")
808 (throw 'debug-loop-catcher nil)))))
809 ;; We have to bind level for the restart function created by
810 ;; WITH-SIMPLE-RESTART.
811 (let ((level *debug-command-level*)
812 (restart-commands (make-restart-commands)))
813 (with-simple-restart (abort
814 "Reduce debugger level (to debug level ~W)."
816 (debug-prompt *debug-io*)
817 (force-output *debug-io*)
818 (let ((input (sb!int:get-stream-command *debug-io*)))
820 (let ((cmd-fun (debug-command-p
821 (sb!int:stream-command-name input)
825 (error "unknown stream-command: ~S" input))
827 (error "ambiguous debugger command: ~S" cmd-fun))
830 (sb!int:stream-command-args input))))))
833 (cmd-fun (debug-command-p exp
836 (debug-eval-print exp))
839 "~&Your command, ~S, is ambiguous:~%"
841 (dolist (ele cmd-fun)
842 (format t " ~A~%" ele)))
844 (funcall cmd-fun)))))))))))))))
846 ;;; FIXME: We could probably use INTERACTIVE-EVAL for much of this logic.
847 (defun debug-eval-print (expr)
848 (/noshow "entering DEBUG-EVAL-PRINT" expr)
849 (/noshow (fboundp 'compile))
850 (setq +++ ++ ++ + + - - expr)
851 (let* ((values (multiple-value-list (eval -)))
852 (*standard-output* *debug-io*))
853 (/noshow "done with EVAL in DEBUG-EVAL-PRINT")
855 (if values (prin1 (car values)))
856 (dolist (x (cdr values))
859 (setq /// // // / / values)
860 (setq *** ** ** * * (car values))
861 ;; Make sure that nobody passes back an unbound marker.
865 ;; FIXME: The way INTERACTIVE-EVAL does this seems better.
866 (princ "Setting * to NIL (was unbound marker)."))))
868 ;;;; debug loop functions
870 ;;; These commands are functions, not really commands, so that users
871 ;;; can get their hands on the values returned.
873 (eval-when (:execute :compile-toplevel)
875 (sb!xc:defmacro define-var-operation (ref-or-set &optional value-var)
876 `(let* ((temp (etypecase name
877 (symbol (sb!di:debug-fun-symbol-vars
878 (sb!di:frame-debug-fun *current-frame*)
880 (simple-string (sb!di:ambiguous-debug-vars
881 (sb!di:frame-debug-fun *current-frame*)
883 (location (sb!di:frame-code-location *current-frame*))
884 ;; Let's only deal with valid variables.
885 (vars (remove-if-not (lambda (v)
886 (eq (sb!di:debug-var-validity v location)
889 (declare (list vars))
891 (error "No known valid variables match ~S." name))
895 '(sb!di:debug-var-value (car vars) *current-frame*))
897 `(setf (sb!di:debug-var-value (car vars) *current-frame*)
900 ;; Since we have more than one, first see whether we have
901 ;; any variables that exactly match the specification.
902 (let* ((name (etypecase name
903 (symbol (symbol-name name))
904 (simple-string name)))
905 ;; FIXME: REMOVE-IF-NOT is deprecated, use STRING/=
907 (exact (remove-if-not (lambda (v)
908 (string= (sb!di:debug-var-symbol-name v)
911 (vars (or exact vars)))
912 (declare (simple-string name)
915 ;; Check now for only having one variable.
919 '(sb!di:debug-var-value (car vars) *current-frame*))
921 `(setf (sb!di:debug-var-value (car vars) *current-frame*)
923 ;; If there weren't any exact matches, flame about
924 ;; ambiguity unless all the variables have the same
929 (string= (sb!di:debug-var-symbol-name v)
930 (sb!di:debug-var-symbol-name (car vars))))
932 (error "specification ambiguous:~%~{ ~A~%~}"
933 (mapcar #'sb!di:debug-var-symbol-name
936 :key #'sb!di:debug-var-symbol-name))))
937 ;; All names are the same, so see whether the user
938 ;; ID'ed one of them.
940 (let ((v (find id vars :key #'sb!di:debug-var-id)))
943 "invalid variable ID, ~W: should have been one of ~S"
945 (mapcar #'sb!di:debug-var-id vars)))
948 '(sb!di:debug-var-value v *current-frame*))
950 `(setf (sb!di:debug-var-value v *current-frame*)
953 (error "Specify variable ID to disambiguate ~S. Use one of ~S."
955 (mapcar #'sb!di:debug-var-id vars)))))))))
959 ;;; FIXME: This doesn't work. It would be real nice we could make it
960 ;;; work! Alas, it doesn't seem to work in CMU CL X86 either..
961 (defun var (name &optional (id 0 id-supplied))
963 "Return a variable's value if possible. NAME is a simple-string or symbol.
964 If it is a simple-string, it is an initial substring of the variable's name.
965 If name is a symbol, it has the same name and package as the variable whose
966 value this function returns. If the symbol is uninterned, then the variable
967 has the same name as the symbol, but it has no package.
969 If name is the initial substring of variables with different names, then
970 this return no values after displaying the ambiguous names. If name
971 determines multiple variables with the same name, then you must use the
972 optional id argument to specify which one you want. If you left id
973 unspecified, then this returns no values after displaying the distinguishing
976 The result of this function is limited to the availability of variable
977 information. This is SETF'able."
978 (define-var-operation :ref))
979 (defun (setf var) (value name &optional (id 0 id-supplied))
980 (define-var-operation :set value))
982 ;;; This returns the COUNT'th arg as the user sees it from args, the
983 ;;; result of SB!DI:DEBUG-FUN-LAMBDA-LIST. If this returns a
984 ;;; potential DEBUG-VAR from the lambda-list, then the second value is
985 ;;; T. If this returns a keyword symbol or a value from a rest arg,
986 ;;; then the second value is NIL.
987 (declaim (ftype (function (index list)) nth-arg))
988 (defun nth-arg (count args)
990 (dolist (ele args (error "The argument specification ~S is out of range."
992 (lambda-list-element-dispatch ele
993 :required ((if (zerop n) (return (values ele t))))
994 :optional ((if (zerop n) (return (values (second ele) t))))
995 :keyword ((cond ((zerop n)
996 (return (values (second ele) nil)))
998 (return (values (third ele) t)))))
999 :deleted ((if (zerop n) (return (values ele t))))
1000 :rest ((let ((var (second ele)))
1001 (lambda-var-dispatch var (sb!di:frame-code-location
1003 (error "unused &REST argument before n'th
1006 (sb!di:debug-var-value var *current-frame*)
1008 "The argument specification ~S is out of range."
1011 (return-from nth-arg (values value nil))
1013 (error "invalid &REST argument before n'th argument")))))
1018 "Return the N'th argument's value if possible. Argument zero is the first
1019 argument in a frame's default printed representation. Count keyword/value
1020 pairs as separate arguments."
1021 (multiple-value-bind (var lambda-var-p)
1022 (nth-arg n (handler-case (sb!di:debug-fun-lambda-list
1023 (sb!di:frame-debug-fun *current-frame*))
1024 (sb!di:lambda-list-unavailable ()
1025 (error "No argument values are available."))))
1027 (lambda-var-dispatch var (sb!di:frame-code-location *current-frame*)
1028 (error "Unused arguments have no values.")
1029 (sb!di:debug-var-value var *current-frame*)
1030 (error "invalid argument value"))
1033 ;;;; machinery for definition of debug loop commands
1035 (defvar *debug-commands* nil)
1037 ;;; Interface to *DEBUG-COMMANDS*. No required arguments in args are
1039 (defmacro !def-debug-command (name args &rest body)
1040 (let ((fun-name (symbolicate name "-DEBUG-COMMAND")))
1042 (setf *debug-commands*
1043 (remove ,name *debug-commands* :key #'car :test #'string=))
1044 (defun ,fun-name ,args
1045 (unless *in-the-debugger*
1046 (error "invoking debugger command while outside the debugger"))
1048 (push (cons ,name #',fun-name) *debug-commands*)
1051 (defun !def-debug-command-alias (new-name existing-name)
1052 (let ((pair (assoc existing-name *debug-commands* :test #'string=)))
1053 (unless pair (error "unknown debug command name: ~S" existing-name))
1054 (push (cons new-name (cdr pair)) *debug-commands*))
1057 ;;; This takes a symbol and uses its name to find a debugger command,
1058 ;;; using initial substring matching. It returns the command function
1059 ;;; if form identifies only one command, but if form is ambiguous,
1060 ;;; this returns a list of the command names. If there are no matches,
1061 ;;; this returns nil. Whenever the loop that looks for a set of
1062 ;;; possibilities encounters an exact name match, we return that
1063 ;;; command function immediately.
1064 (defun debug-command-p (form &optional other-commands)
1065 (if (or (symbolp form) (integerp form))
1069 (format nil "~W" form)))
1072 (declare (simple-string name)
1076 ;; Find matching commands, punting if exact match.
1077 (flet ((match-command (ele)
1078 (let* ((str (car ele))
1079 (str-len (length str)))
1080 (declare (simple-string str)
1082 (cond ((< str-len len))
1084 (when (string= name str :end1 len :end2 len)
1085 (return-from debug-command-p (cdr ele))))
1086 ((string= name str :end1 len :end2 len)
1088 (mapc #'match-command *debug-commands*)
1089 (mapc #'match-command other-commands))
1091 ;; Return the right value.
1092 (cond ((not res) nil)
1095 (t ; Just return the names.
1096 (do ((cmds res (cdr cmds)))
1098 (setf (car cmds) (caar cmds))))))))
1100 ;;; Return a list of debug commands (in the same format as
1101 ;;; *DEBUG-COMMANDS*) that invoke each active restart.
1103 ;;; Two commands are made for each restart: one for the number, and
1104 ;;; one for the restart name (unless it's been shadowed by an earlier
1105 ;;; restart of the same name, or it is NIL).
1106 (defun make-restart-commands (&optional (restarts *debug-restarts*))
1108 (num 0)) ; better be the same as show-restarts!
1109 (dolist (restart restarts)
1110 (let ((name (string (restart-name restart))))
1113 (/show0 "in restart-command closure, about to i-r-i")
1114 (invoke-restart-interactively restart))))
1115 (push (cons (prin1-to-string num) restart-fun) commands)
1116 (unless (or (null (restart-name restart))
1117 (find name commands :key #'car :test #'string=))
1118 (push (cons name restart-fun) commands))))
1122 ;;;; frame-changing commands
1124 (!def-debug-command "UP" ()
1125 (let ((next (sb!di:frame-up *current-frame*)))
1127 (setf *current-frame* next)
1128 (print-frame-call next))
1130 (format t "~&Top of stack.")))))
1132 (!def-debug-command "DOWN" ()
1133 (let ((next (sb!di:frame-down *current-frame*)))
1135 (setf *current-frame* next)
1136 (print-frame-call next))
1138 (format t "~&Bottom of stack.")))))
1140 (!def-debug-command-alias "D" "DOWN")
1142 ;;; CMU CL had this command, but SBCL doesn't, since it's redundant
1143 ;;; with "FRAME 0", and it interferes with abbreviations for the
1144 ;;; TOPLEVEL restart.
1145 ;;;(!def-debug-command "TOP" ()
1146 ;;; (do ((prev *current-frame* lead)
1147 ;;; (lead (sb!di:frame-up *current-frame*) (sb!di:frame-up lead)))
1149 ;;; (setf *current-frame* prev)
1150 ;;; (print-frame-call prev))))
1152 (!def-debug-command "BOTTOM" ()
1153 (do ((prev *current-frame* lead)
1154 (lead (sb!di:frame-down *current-frame*) (sb!di:frame-down lead)))
1156 (setf *current-frame* prev)
1157 (print-frame-call prev))))
1159 (!def-debug-command-alias "B" "BOTTOM")
1161 (!def-debug-command "FRAME" (&optional
1162 (n (read-prompting-maybe "frame number: ")))
1163 (setf *current-frame*
1164 (multiple-value-bind (next-frame-fun limit-string)
1165 (if (< n (sb!di:frame-number *current-frame*))
1166 (values #'sb!di:frame-up "top")
1167 (values #'sb!di:frame-down "bottom"))
1168 (do ((frame *current-frame*))
1169 ((= n (sb!di:frame-number frame))
1171 (let ((next-frame (funcall next-frame-fun frame)))
1173 (setf frame next-frame))
1176 "The ~A of the stack was encountered.~%"
1178 (return frame)))))))
1179 (print-frame-call *current-frame*))
1181 (!def-debug-command-alias "F" "FRAME")
1183 ;;;; commands for entering and leaving the debugger
1185 ;;; CMU CL supported this QUIT debug command, but SBCL provides this
1186 ;;; functionality with a restart instead. (The QUIT debug command was
1187 ;;; removed because it's confusing to have "quit" mean two different
1188 ;;; things in the system, "restart the top level REPL" in the debugger
1189 ;;; and "terminate the Lisp system" as the SB-EXT:QUIT function.)
1191 ;;;(!def-debug-command "QUIT" ()
1192 ;;; (throw 'sb!impl::toplevel-catcher nil))
1194 ;;; CMU CL supported this GO debug command, but SBCL doesn't -- in
1195 ;;; SBCL you just type the CONTINUE restart name instead (or "RESTART
1196 ;;; CONTINUE", that's OK too).
1198 ;;;(!def-debug-command "GO" ()
1199 ;;; (continue *debug-condition*)
1200 ;;; (error "There is no restart named CONTINUE."))
1202 (!def-debug-command "RESTART" ()
1203 (/show0 "doing RESTART debug-command")
1204 (let ((num (read-if-available :prompt)))
1205 (when (eq num :prompt)
1206 (show-restarts *debug-restarts* *debug-io*)
1207 (write-string "restart: ")
1209 (setf num (read *standard-input*)))
1210 (let ((restart (typecase num
1212 (nth num *debug-restarts*))
1214 (find num *debug-restarts* :key #'restart-name
1215 :test (lambda (sym1 sym2)
1216 (string= (symbol-name sym1)
1217 (symbol-name sym2)))))
1219 (format t "~S is invalid as a restart name.~%" num)
1220 (return-from restart-debug-command nil)))))
1221 (/show0 "got RESTART")
1223 (invoke-restart-interactively restart)
1224 ;; FIXME: Even if this isn't handled by WARN, it probably
1225 ;; shouldn't go to *STANDARD-OUTPUT*, but *ERROR-OUTPUT* or
1226 ;; *QUERY-IO* or something. Look through this file to
1227 ;; straighten out stream usage.
1228 (princ "There is no such restart.")))))
1230 ;;;; information commands
1232 (!def-debug-command "HELP" ()
1233 ;; CMU CL had a little toy pager here, but "if you aren't running
1234 ;; ILISP (or a smart windowing system, or something) you deserve to
1235 ;; lose", so we've dropped it in SBCL. However, in case some
1236 ;; desperate holdout is running this on a dumb terminal somewhere,
1237 ;; we tell him where to find the message stored as a string.
1239 "~&~A~2%(The HELP string is stored in ~S.)~%"
1241 '*debug-help-string*))
1243 (!def-debug-command-alias "?" "HELP")
1245 (!def-debug-command "ERROR" ()
1246 (format *debug-io* "~A~%" *debug-condition*)
1247 (show-restarts *debug-restarts* *debug-io*))
1249 (!def-debug-command "BACKTRACE" ()
1250 (backtrace (read-if-available most-positive-fixnum)))
1252 (!def-debug-command "PRINT" ()
1253 (print-frame-call *current-frame*))
1255 (!def-debug-command-alias "P" "PRINT")
1257 (!def-debug-command "LIST-LOCALS" ()
1258 (let ((d-fun (sb!di:frame-debug-fun *current-frame*)))
1259 (if (sb!di:debug-var-info-available d-fun)
1260 (let ((*standard-output* *debug-io*)
1261 (location (sb!di:frame-code-location *current-frame*))
1262 (prefix (read-if-available nil))
1265 (dolist (v (sb!di:ambiguous-debug-vars
1267 (if prefix (string prefix) "")))
1269 (when (eq (sb!di:debug-var-validity v location) :valid)
1270 (setf any-valid-p t)
1271 (format t "~S~:[#~W~;~*~] = ~S~%"
1272 (sb!di:debug-var-symbol v)
1273 (zerop (sb!di:debug-var-id v))
1274 (sb!di:debug-var-id v)
1275 (sb!di:debug-var-value v *current-frame*))))
1279 (format t "There are no local variables ~@[starting with ~A ~]~
1283 (format t "All variables ~@[starting with ~A ~]currently ~
1284 have invalid values."
1286 (write-line "There is no variable information available."))))
1288 (!def-debug-command-alias "L" "LIST-LOCALS")
1290 (!def-debug-command "SOURCE" ()
1292 (print-code-location-source-form (sb!di:frame-code-location *current-frame*)
1293 (read-if-available 0)))
1295 ;;;; source location printing
1297 ;;; We cache a stream to the last valid file debug source so that we
1298 ;;; won't have to repeatedly open the file.
1300 ;;; KLUDGE: This sounds like a bug, not a feature. Opening files is fast
1301 ;;; in the 1990s, so the benefit is negligible, less important than the
1302 ;;; potential of extra confusion if someone changes the source during
1303 ;;; a debug session and the change doesn't show up. And removing this
1304 ;;; would simplify the system, which I like. -- WHN 19990903
1305 (defvar *cached-debug-source* nil)
1306 (declaim (type (or sb!di:debug-source null) *cached-debug-source*))
1307 (defvar *cached-source-stream* nil)
1308 (declaim (type (or stream null) *cached-source-stream*))
1310 ;;; To suppress the read-time evaluation #. macro during source read,
1311 ;;; *READTABLE* is modified. *READTABLE* is cached to avoid
1312 ;;; copying it each time, and invalidated when the
1313 ;;; *CACHED-DEBUG-SOURCE* has changed.
1314 (defvar *cached-readtable* nil)
1315 (declaim (type (or readtable null) *cached-readtable*))
1318 (setq *cached-debug-source* nil *cached-source-stream* nil
1319 *cached-readtable* nil))
1320 *before-save-initializations*)
1322 ;;; We also cache the last toplevel form that we printed a source for
1323 ;;; so that we don't have to do repeated reads and calls to
1324 ;;; FORM-NUMBER-TRANSLATIONS.
1325 (defvar *cached-toplevel-form-offset* nil)
1326 (declaim (type (or index null) *cached-toplevel-form-offset*))
1327 (defvar *cached-toplevel-form*)
1328 (defvar *cached-form-number-translations*)
1330 ;;; Given a code location, return the associated form-number
1331 ;;; translations and the actual top level form. We check our cache ---
1332 ;;; if there is a miss, we dispatch on the kind of the debug source.
1333 (defun get-toplevel-form (location)
1334 (let ((d-source (sb!di:code-location-debug-source location)))
1335 (if (and (eq d-source *cached-debug-source*)
1336 (eql (sb!di:code-location-toplevel-form-offset location)
1337 *cached-toplevel-form-offset*))
1338 (values *cached-form-number-translations* *cached-toplevel-form*)
1339 (let* ((offset (sb!di:code-location-toplevel-form-offset location))
1341 (ecase (sb!di:debug-source-from d-source)
1342 (:file (get-file-toplevel-form location))
1343 (:lisp (svref (sb!di:debug-source-name d-source) offset)))))
1344 (setq *cached-toplevel-form-offset* offset)
1345 (values (setq *cached-form-number-translations*
1346 (sb!di:form-number-translations res offset))
1347 (setq *cached-toplevel-form* res))))))
1349 ;;; Locate the source file (if it still exists) and grab the top level
1350 ;;; form. If the file is modified, we use the top level form offset
1351 ;;; instead of the recorded character offset.
1352 (defun get-file-toplevel-form (location)
1353 (let* ((d-source (sb!di:code-location-debug-source location))
1354 (tlf-offset (sb!di:code-location-toplevel-form-offset location))
1355 (local-tlf-offset (- tlf-offset
1356 (sb!di:debug-source-root-number d-source)))
1358 (aref (or (sb!di:debug-source-start-positions d-source)
1359 (error "no start positions map"))
1361 (name (sb!di:debug-source-name d-source)))
1362 (unless (eq d-source *cached-debug-source*)
1363 (unless (and *cached-source-stream*
1364 (equal (pathname *cached-source-stream*)
1366 (setq *cached-readtable* nil)
1367 (when *cached-source-stream* (close *cached-source-stream*))
1368 (setq *cached-source-stream* (open name :if-does-not-exist nil))
1369 (unless *cached-source-stream*
1370 (error "The source file no longer exists:~% ~A" (namestring name)))
1371 (format t "~%; file: ~A~%" (namestring name)))
1373 (setq *cached-debug-source*
1374 (if (= (sb!di:debug-source-created d-source)
1375 (file-write-date name))
1379 ((eq *cached-debug-source* d-source)
1380 (file-position *cached-source-stream* char-offset))
1382 (format t "~%; File has been modified since compilation:~%; ~A~@
1383 ; Using form offset instead of character position.~%"
1385 (file-position *cached-source-stream* 0)
1386 (let ((*read-suppress* t))
1387 (dotimes (i local-tlf-offset)
1388 (read *cached-source-stream*)))))
1389 (unless *cached-readtable*
1390 (setq *cached-readtable* (copy-readtable))
1391 (set-dispatch-macro-character
1393 (lambda (stream sub-char &rest rest)
1394 (declare (ignore rest sub-char))
1395 (let ((token (read stream t nil t)))
1396 (format nil "#.~S" token)))
1397 *cached-readtable*))
1398 (let ((*readtable* *cached-readtable*))
1399 (read *cached-source-stream*))))
1401 (defun print-code-location-source-form (location context)
1402 (let* ((location (maybe-block-start-location location))
1403 (form-num (sb!di:code-location-form-number location)))
1404 (multiple-value-bind (translations form) (get-toplevel-form location)
1405 (unless (< form-num (length translations))
1406 (error "The source path no longer exists."))
1407 (prin1 (sb!di:source-path-context form
1408 (svref translations form-num)
1411 ;;; breakpoint and step commands
1413 ;;; Step to the next code-location.
1414 (!def-debug-command "STEP" ()
1415 (setf *number-of-steps* (read-if-available 1))
1416 (set-step-breakpoint *current-frame*)
1417 (continue *debug-condition*)
1418 (error "couldn't continue"))
1420 ;;; List possible breakpoint locations, which ones are active, and
1421 ;;; where the CONTINUE restart will transfer control. Set
1422 ;;; *POSSIBLE-BREAKPOINTS* to the code-locations which can then be
1423 ;;; used by sbreakpoint.
1424 (!def-debug-command "LIST-LOCATIONS" ()
1425 (let ((df (read-if-available *default-breakpoint-debug-fun*)))
1427 (setf df (sb!di:fun-debug-fun (eval df)))
1428 (setf *default-breakpoint-debug-fun* df))
1430 (not *default-breakpoint-debug-fun*))
1431 (setf df (sb!di:frame-debug-fun *current-frame*))
1432 (setf *default-breakpoint-debug-fun* df)))
1433 (setf *possible-breakpoints* (possible-breakpoints df)))
1434 (let ((continue-at (sb!di:frame-code-location *current-frame*)))
1435 (let ((active (location-in-list *default-breakpoint-debug-fun*
1436 *breakpoints* :fun-start))
1437 (here (sb!di:code-location=
1438 (sb!di:debug-fun-start-location
1439 *default-breakpoint-debug-fun*) continue-at)))
1440 (when (or active here)
1441 (format t "::FUN-START ")
1442 (when active (format t " *Active*"))
1443 (when here (format t " *Continue here*"))))
1445 (let ((prev-location nil)
1450 (let ((this-num (1- this-num)))
1451 (if (= prev-num this-num)
1452 (format t "~&~W: " prev-num)
1453 (format t "~&~W-~W: " prev-num this-num)))
1454 (print-code-location-source-form prev-location 0)
1455 (when *print-location-kind*
1456 (format t "~S " (sb!di:code-location-kind prev-location)))
1457 (when (location-in-list prev-location *breakpoints*)
1458 (format t " *Active*"))
1459 (when (sb!di:code-location= prev-location continue-at)
1460 (format t " *Continue here*")))))
1462 (dolist (code-location *possible-breakpoints*)
1463 (when (or *print-location-kind*
1464 (location-in-list code-location *breakpoints*)
1465 (sb!di:code-location= code-location continue-at)
1467 (not (eq (sb!di:code-location-debug-source code-location)
1468 (sb!di:code-location-debug-source prev-location)))
1469 (not (eq (sb!di:code-location-toplevel-form-offset
1471 (sb!di:code-location-toplevel-form-offset
1473 (not (eq (sb!di:code-location-form-number code-location)
1474 (sb!di:code-location-form-number prev-location))))
1476 (setq prev-location code-location prev-num this-num))
1480 (when (location-in-list *default-breakpoint-debug-fun*
1483 (format t "~&::FUN-END *Active* "))))
1485 (!def-debug-command-alias "LL" "LIST-LOCATIONS")
1487 ;;; Set breakpoint at the given number.
1488 (!def-debug-command "BREAKPOINT" ()
1489 (let ((index (read-prompting-maybe "location number, :START, or :END: "))
1493 (print-functions nil)
1496 (place *default-breakpoint-debug-fun*))
1497 (flet ((get-command-line ()
1498 (let ((command-line nil)
1501 (let ((next-input (read-if-available unique)))
1502 (when (eq next-input unique) (return))
1503 (push next-input command-line)))
1504 (nreverse command-line)))
1505 (set-vars-from-command-line (command-line)
1506 (do ((arg (pop command-line) (pop command-line)))
1509 (:condition (setf condition (pop command-line)))
1510 (:print (push (pop command-line) print))
1511 (:break (setf break (pop command-line)))
1513 (setf function (eval (pop command-line)))
1514 (setf *default-breakpoint-debug-fun*
1515 (sb!di:fun-debug-fun function))
1516 (setf place *default-breakpoint-debug-fun*)
1517 (setf *possible-breakpoints*
1518 (possible-breakpoints
1519 *default-breakpoint-debug-fun*))))))
1521 (let ((code-loc (sb!di:debug-fun-start-location place)))
1522 (setf bp (sb!di:make-breakpoint #'main-hook-fun
1525 (setf break (sb!di:preprocess-for-eval break code-loc))
1526 (setf condition (sb!di:preprocess-for-eval condition code-loc))
1527 (dolist (form print)
1528 (push (cons (sb!di:preprocess-for-eval form code-loc) form)
1532 (sb!di:make-breakpoint #'main-hook-fun
1536 ;; FIXME: These and any other old (COERCE `(LAMBDA ..) ..)
1537 ;; forms should be converted to shiny new (LAMBDA ..) forms.
1538 ;; (Search the sources for "coerce.*\(lambda".)
1539 (coerce `(lambda (dummy)
1540 (declare (ignore dummy)) ,break)
1542 (setf condition (coerce `(lambda (dummy)
1543 (declare (ignore dummy)) ,condition)
1545 (dolist (form print)
1547 (coerce `(lambda (dummy)
1548 (declare (ignore dummy)) ,form) 'function)
1551 (setup-code-location ()
1552 (setf place (nth index *possible-breakpoints*))
1553 (setf bp (sb!di:make-breakpoint #'main-hook-fun place
1554 :kind :code-location))
1555 (dolist (form print)
1557 (sb!di:preprocess-for-eval form place)
1560 (setf break (sb!di:preprocess-for-eval break place))
1561 (setf condition (sb!di:preprocess-for-eval condition place))))
1562 (set-vars-from-command-line (get-command-line))
1564 ((or (eq index :start) (eq index :s))
1566 ((or (eq index :end) (eq index :e))
1569 (setup-code-location)))
1570 (sb!di:activate-breakpoint bp)
1571 (let* ((new-bp-info (create-breakpoint-info place bp index
1573 :print print-functions
1574 :condition condition))
1575 (old-bp-info (location-in-list new-bp-info *breakpoints*)))
1577 (sb!di:deactivate-breakpoint (breakpoint-info-breakpoint
1579 (setf *breakpoints* (remove old-bp-info *breakpoints*))
1580 (format t "previous breakpoint removed~%"))
1581 (push new-bp-info *breakpoints*))
1582 (print-breakpoint-info (first *breakpoints*))
1583 (format t "~&added"))))
1585 (!def-debug-command-alias "BP" "BREAKPOINT")
1587 ;;; List all breakpoints which are set.
1588 (!def-debug-command "LIST-BREAKPOINTS" ()
1590 (sort *breakpoints* #'< :key #'breakpoint-info-breakpoint-number))
1591 (dolist (info *breakpoints*)
1592 (print-breakpoint-info info)))
1594 (!def-debug-command-alias "LB" "LIST-BREAKPOINTS")
1595 (!def-debug-command-alias "LBP" "LIST-BREAKPOINTS")
1597 ;;; Remove breakpoint N, or remove all breakpoints if no N given.
1598 (!def-debug-command "DELETE-BREAKPOINT" ()
1599 (let* ((index (read-if-available nil))
1601 (find index *breakpoints* :key #'breakpoint-info-breakpoint-number)))
1603 (sb!di:delete-breakpoint (breakpoint-info-breakpoint bp-info))
1604 (setf *breakpoints* (remove bp-info *breakpoints*))
1605 (format t "breakpoint ~S removed~%" index))
1606 (index (format t "The breakpoint doesn't exist."))
1608 (dolist (ele *breakpoints*)
1609 (sb!di:delete-breakpoint (breakpoint-info-breakpoint ele)))
1610 (setf *breakpoints* nil)
1611 (format t "all breakpoints deleted~%")))))
1613 (!def-debug-command-alias "DBP" "DELETE-BREAKPOINT")
1615 ;;; miscellaneous commands
1617 (!def-debug-command "DESCRIBE" ()
1618 (let* ((curloc (sb!di:frame-code-location *current-frame*))
1619 (debug-fun (sb!di:code-location-debug-fun curloc))
1620 (function (sb!di:debug-fun-fun debug-fun)))
1623 (format t "can't figure out the function for this frame"))))
1625 ;;;; debug loop command utilities
1627 (defun read-prompting-maybe (prompt &optional (in *standard-input*)
1628 (out *standard-output*))
1629 (unless (sb!int:listen-skip-whitespace in)
1634 (defun read-if-available (default &optional (stream *standard-input*))
1635 (if (sb!int:listen-skip-whitespace stream)