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 (defvar *debug-print-level* 3
18 "*PRINT-LEVEL* for the debugger")
20 (defvar *debug-print-length* 5
22 "*PRINT-LENGTH* for the debugger")
24 (defvar *debug-readtable*
25 ;; KLUDGE: This can't be initialized in a cold toplevel form,
26 ;; because the *STANDARD-READTABLE* isn't initialized until after
27 ;; cold toplevel forms have run. So instead we initialize it
28 ;; immediately after *STANDARD-READTABLE*. -- WHN 20000205
31 "*READTABLE* for the debugger")
33 (defvar *in-the-debugger* nil
35 "This is T while in the debugger.")
37 ;;; nestedness inside debugger command loops
38 (defvar *debug-command-level* 0)
40 (defvar *stack-top-hint* nil
42 "If this is bound before the debugger is invoked, it is used as the stack
43 top by the debugger.")
44 (defvar *stack-top* nil)
45 (defvar *real-stack-top* nil)
47 (defvar *current-frame* nil)
49 (defun debug-prompt (stream)
51 ;; old behavior, will probably go away in sbcl-0.7.x
52 (format stream "~%~D" (sb!di:frame-number *current-frame*))
53 (dotimes (i *debug-command-level*)
54 (write-char #\] stream))
55 (write-char #\space stream)
57 ;; planned new behavior, delayed since it will break ILISP
61 (sb!di:frame-number *current-frame*)
62 (> *debug-command-level* 1)
63 *debug-command-level*))
65 (defparameter *debug-help-string*
66 "The prompt is right square brackets, the number indicating how many
67 recursive command loops you are in.
68 Any command may be uniquely abbreviated.
69 The debugger rebinds various special variables for controlling i/o, sometimes
70 to defaults (much like WITH-STANDARD-IO-SYNTAX does) and sometimes to
71 its own special values, e.g. SB-DEBUG:*DEBUG-PRINT-LEVEL*.
72 Debug commands do not affect * and friends, but evaluation in the debug loop
73 does affect these variables.
74 SB-DEBUG:*FLUSH-DEBUG-ERRORS* controls whether errors at the debug prompt
75 drop you into deeper into the debugger.
77 Getting in and out of the debugger:
78 RESTART invokes restart numbered as shown (prompt if not given).
79 ERROR prints the error condition and restart cases.
80 The name of any restart, or its number, is a valid command, and is the same
81 as using RESTART to invoke that restart.
84 U up frame D down frame
85 B bottom frame F n frame n (n=0 for top frame)
88 BACKTRACE [n] shows n frames going down the stack.
89 LIST-LOCALS, L lists locals in current function.
90 PRINT, P displays current function call.
91 SOURCE [n] displays frame's source form with n levels of enclosing forms.
93 Breakpoints and steps:
94 LIST-LOCATIONS [{function | :C}] List the locations for breakpoints.
95 Specify :C for the current frame.
97 LIST-BREAKPOINTS List the active breakpoints.
98 Abbreviations: LB, LBP
99 DELETE-BREAKPOINT [n] Remove breakpoint n or all breakpoints.
100 Abbreviations: DEL, DBP
101 BREAKPOINT {n | :end | :start} [:break form] [:function function]
102 [{:print form}*] [:condition form]
104 Abbreviations: BR, BP
105 STEP [n] Step to the next location or step n times.
107 Function and macro commands:
108 (SB-DEBUG:DEBUG-RETURN expression)
109 Exit the debugger, returning expression's values from the current frame.
111 Return the n'th argument in the current frame.
112 (SB-DEBUG:VAR string-or-symbol [id])
113 Returns the value of the specified variable in the current frame.")
115 ;;; This is used to communicate to DEBUG-LOOP that we are at a step breakpoint.
116 (define-condition step-condition (simple-condition) ())
118 ;;;; breakpoint state
120 (defvar *only-block-start-locations* nil
122 "When true, the LIST-LOCATIONS command only displays block start locations.
123 Otherwise, all locations are displayed.")
125 (defvar *print-location-kind* nil
127 "When true, list the code location type in the LIST-LOCATIONS command.")
129 ;;; a list of the types of code-locations that should not be stepped
130 ;;; to and should not be listed when listing breakpoints
131 (defvar *bad-code-location-types* '(:call-site :internal-error))
132 (declaim (type list *bad-code-location-types*))
134 ;;; code locations of the possible breakpoints
135 (defvar *possible-breakpoints*)
136 (declaim (type list *possible-breakpoints*))
138 ;;; a list of the made and active breakpoints, each is a
139 ;;; BREAKPOINT-INFO structure
140 (defvar *breakpoints* nil)
141 (declaim (type list *breakpoints*))
143 ;;; a list of BREAKPOINT-INFO structures of the made and active step
145 (defvar *step-breakpoints* nil)
146 (declaim (type list *step-breakpoints*))
148 ;;; the number of times left to step
149 (defvar *number-of-steps* 1)
150 (declaim (type integer *number-of-steps*))
152 ;;; This is used when listing and setting breakpoints.
153 (defvar *default-breakpoint-debug-function* nil)
154 (declaim (type (or list sb!di:debug-function) *default-breakpoint-debug-function*))
156 ;;;; code location utilities
158 ;;; Return the first code-location in the passed debug block.
159 (defun first-code-location (debug-block)
161 (first-code-location nil))
162 (sb!di:do-debug-block-locations (code-location debug-block)
164 (setf first-code-location code-location)
166 first-code-location))
168 ;;; Return a list of the next code-locations following the one passed.
169 ;;; One of the *BAD-CODE-LOCATION-TYPES* will not be returned.
170 (defun next-code-locations (code-location)
171 (let ((debug-block (sb!di:code-location-debug-block code-location))
172 (block-code-locations nil))
173 (sb!di:do-debug-block-locations (block-code-location debug-block)
174 (unless (member (sb!di:code-location-kind block-code-location)
175 *bad-code-location-types*)
176 (push block-code-location block-code-locations)))
177 (setf block-code-locations (nreverse block-code-locations))
178 (let* ((code-loc-list (rest (member code-location block-code-locations
179 :test #'sb!di:code-location=)))
180 (next-list (cond (code-loc-list
181 (list (first code-loc-list)))
182 ((map 'list #'first-code-location
183 (sb!di:debug-block-successors debug-block)))
185 (when (and (= (length next-list) 1)
186 (sb!di:code-location= (first next-list) code-location))
187 (setf next-list (next-code-locations (first next-list))))
190 ;;; Returns a list of code-locations of the possible breakpoints of the
191 ;;; debug-function passed.
192 (defun possible-breakpoints (debug-function)
193 (let ((possible-breakpoints nil))
194 (sb!di:do-debug-function-blocks (debug-block debug-function)
195 (unless (sb!di:debug-block-elsewhere-p debug-block)
196 (if *only-block-start-locations*
197 (push (first-code-location debug-block) possible-breakpoints)
198 (sb!di:do-debug-block-locations (code-location debug-block)
199 (when (not (member (sb!di:code-location-kind code-location)
200 *bad-code-location-types*))
201 (push code-location possible-breakpoints))))))
202 (nreverse possible-breakpoints)))
204 ;;; Searches the info-list for the item passed (code-location,
205 ;;; debug-function, or breakpoint-info). If the item passed is a debug
206 ;;; function then kind will be compared if it was specified. The kind
207 ;;; if also compared if a breakpoint-info is passed since it's in the
208 ;;; breakpoint. The info structure is returned if found.
209 (defun location-in-list (place info-list &optional (kind nil))
210 (when (breakpoint-info-p place)
211 (setf kind (sb!di:breakpoint-kind (breakpoint-info-breakpoint place)))
212 (setf place (breakpoint-info-place place)))
213 (cond ((sb!di:code-location-p place)
214 (find place info-list
215 :key #'breakpoint-info-place
216 :test #'(lambda (x y) (and (sb!di:code-location-p y)
217 (sb!di:code-location= x y)))))
219 (find place info-list
220 :test #'(lambda (x-debug-function y-info)
221 (let ((y-place (breakpoint-info-place y-info))
222 (y-breakpoint (breakpoint-info-breakpoint
224 (and (sb!di:debug-function-p y-place)
225 (eq x-debug-function y-place)
227 (eq kind (sb!di:breakpoint-kind
228 y-breakpoint))))))))))
230 ;;; If LOC is an unknown location, then try to find the block start
231 ;;; location. Used by source printing to some information instead of
232 ;;; none for the user.
233 (defun maybe-block-start-location (loc)
234 (if (sb!di:code-location-unknown-p loc)
235 (let* ((block (sb!di:code-location-debug-block loc))
236 (start (sb!di:do-debug-block-locations (loc block)
238 (cond ((and (not (sb!di:debug-block-elsewhere-p block))
240 ;; FIXME: Why output on T instead of *DEBUG-FOO* or something?
241 (format t "~%unknown location: using block start~%")
247 ;;;; the BREAKPOINT-INFO structure
249 ;;; info about a made breakpoint
250 (defstruct (breakpoint-info (:copier nil))
251 ;; where we are going to stop
252 (place (required-argument)
253 :type (or sb!di:code-location sb!di:debug-function))
254 ;; the breakpoint returned by sb!di:make-breakpoint
255 (breakpoint (required-argument) :type sb!di:breakpoint)
256 ;; the function returned from sb!di:preprocess-for-eval. If result is
257 ;; non-NIL, drop into the debugger.
258 (break #'identity :type function)
259 ;; the function returned from sb!di:preprocess-for-eval. If result is
260 ;; non-NIL, eval (each) print and print results.
261 (condition #'identity :type function)
262 ;; the list of functions from sb!di:preprocess-for-eval to evaluate.
263 ;; Results are conditionally printed. Car of each element is the
264 ;; function, cdr is the form it goes with.
265 (print nil :type list)
266 ;; the number used when listing the possible breakpoints within a
267 ;; function. Could also be a symbol such as start or end.
268 (code-location-number (required-argument) :type (or symbol integer))
269 ;; the number used when listing the breakpoints active and to delete
271 (breakpoint-number (required-argument) :type integer))
273 ;;; Return a new BREAKPOINT-INFO structure with the info passed.
274 (defun create-breakpoint-info (place breakpoint code-location-number
275 &key (break #'identity)
276 (condition #'identity) (print nil))
278 (sort *breakpoints* #'< :key #'breakpoint-info-breakpoint-number))
279 (let ((breakpoint-number
280 (do ((i 1 (incf i)) (breakpoints *breakpoints* (rest breakpoints)))
281 ((or (> i (length *breakpoints*))
282 (not (= i (breakpoint-info-breakpoint-number
283 (first breakpoints)))))
286 (make-breakpoint-info :place place :breakpoint breakpoint
287 :code-location-number code-location-number
288 :breakpoint-number breakpoint-number
289 :break break :condition condition :print print)))
291 ;;; Print the breakpoint info for the breakpoint-info structure passed.
292 (defun print-breakpoint-info (breakpoint-info)
293 (let ((place (breakpoint-info-place breakpoint-info))
294 (bp-number (breakpoint-info-breakpoint-number breakpoint-info))
295 (loc-number (breakpoint-info-code-location-number breakpoint-info)))
296 (case (sb!di:breakpoint-kind (breakpoint-info-breakpoint breakpoint-info))
298 (print-code-location-source-form place 0)
303 (sb!di:debug-function-name (sb!di:code-location-debug-function
306 (format t "~&~S: FUNCTION-START in ~S" bp-number
307 (sb!di:debug-function-name place)))
309 (format t "~&~S: FUNCTION-END in ~S" bp-number
310 (sb!di:debug-function-name place))))))
312 ;;;; MAIN-HOOK-FUNCTION for steps and breakpoints
314 ;;; This must be passed as the hook function. It keeps track of where
315 ;;; STEP breakpoints are.
316 (defun main-hook-function (current-frame breakpoint &optional return-vals
318 (setf *default-breakpoint-debug-function*
319 (sb!di:frame-debug-function current-frame))
320 (dolist (step-info *step-breakpoints*)
321 (sb!di:delete-breakpoint (breakpoint-info-breakpoint step-info))
322 (let ((bp-info (location-in-list step-info *breakpoints*)))
324 (sb!di:activate-breakpoint (breakpoint-info-breakpoint bp-info)))))
325 (let ((*stack-top-hint* current-frame)
327 (location-in-list (sb!di:breakpoint-what breakpoint)
329 (sb!di:breakpoint-kind breakpoint)))
331 (location-in-list (sb!di:breakpoint-what breakpoint)
333 (sb!di:breakpoint-kind breakpoint)))
337 (setf *step-breakpoints* nil)
338 (labels ((build-string (str)
339 (setf string (concatenate 'string string str)))
340 (print-common-info ()
342 (with-output-to-string (*standard-output*)
343 (when function-end-cookie
344 (format t "~%Return values: ~S" return-vals))
346 (when (breakpoint-info-print bp-hit-info)
348 (print-frame-call current-frame))
349 (dolist (print (breakpoint-info-print bp-hit-info))
350 (format t "~& ~S = ~S" (rest print)
351 (funcall (first print) current-frame))))))))
353 (setf break (funcall (breakpoint-info-break bp-hit-info)
355 (setf condition (funcall (breakpoint-info-condition bp-hit-info)
357 (cond ((and bp-hit-info step-hit-info (= 1 *number-of-steps*))
358 (build-string (format nil "~&*Step (to a breakpoint)*"))
361 ((and bp-hit-info step-hit-info break)
362 (build-string (format nil "~&*Step (to a breakpoint)*"))
365 ((and bp-hit-info step-hit-info)
367 (format t "~A" string)
368 (decf *number-of-steps*)
369 (set-step-breakpoint current-frame))
370 ((and step-hit-info (= 1 *number-of-steps*))
371 (build-string "*Step*")
372 (break (make-condition 'step-condition :format-control string)))
374 (decf *number-of-steps*)
375 (set-step-breakpoint current-frame))
378 (build-string (format nil "~&*Breakpoint hit*")))
382 (format t "~A" string)))
384 (break "error in main-hook-function: unknown breakpoint"))))))
386 ;;; Set breakpoints at the next possible code-locations. After calling
387 ;;; this, either (CONTINUE) if in the debugger or just let program flow
388 ;;; return if in a hook function.
389 (defun set-step-breakpoint (frame)
391 ((sb!di:debug-block-elsewhere-p (sb!di:code-location-debug-block
392 (sb!di:frame-code-location frame)))
393 ;; FIXME: FORMAT T is used for error output here and elsewhere in
395 (format t "cannot step, in elsewhere code~%"))
397 (let* ((code-location (sb!di:frame-code-location frame))
398 (next-code-locations (next-code-locations code-location)))
401 (dolist (code-location next-code-locations)
402 (let ((bp-info (location-in-list code-location *breakpoints*)))
404 (sb!di:deactivate-breakpoint (breakpoint-info-breakpoint
406 (let ((bp (sb!di:make-breakpoint #'main-hook-function code-location
407 :kind :code-location)))
408 (sb!di:activate-breakpoint bp)
409 (push (create-breakpoint-info code-location bp 0)
410 *step-breakpoints*))))
412 (let* ((debug-function (sb!di:frame-debug-function *current-frame*))
413 (bp (sb!di:make-breakpoint #'main-hook-function debug-function
414 :kind :function-end)))
415 (sb!di:activate-breakpoint bp)
416 (push (create-breakpoint-info debug-function bp 0)
417 *step-breakpoints*))))))))
421 ;;; ANSI specifies that this macro shall exist, even if only as a
422 ;;; trivial placeholder like this.
423 (defmacro step (form)
424 "a trivial placeholder implementation of the CL:STEP macro required by
431 (defun backtrace (&optional (count most-positive-fixnum)
432 (*standard-output* *debug-io*))
434 "Show a listing of the call stack going down from the current frame. In the
435 debugger, the current frame is indicated by the prompt. Count is how many
437 (fresh-line *standard-output*)
438 (do ((frame (if *in-the-debugger* *current-frame* (sb!di:top-frame))
439 (sb!di:frame-down frame))
440 (count count (1- count)))
441 ((or (null frame) (zerop count)))
442 (print-frame-call frame :number t))
443 (fresh-line *standard-output*)
448 (eval-when (:compile-toplevel :execute)
450 ;;; This is a convenient way to express what to do for each type of
451 ;;; lambda-list element.
452 (sb!xc:defmacro lambda-list-element-dispatch (element
463 (ecase (car ,element)
464 (:optional ,@optional)
466 (:keyword ,@keyword)))
468 (aver (eq ,element :deleted))
471 (sb!xc:defmacro lambda-var-dispatch (variable location deleted valid other)
472 (let ((var (gensym)))
473 `(let ((,var ,variable))
474 (cond ((eq ,var :deleted) ,deleted)
475 ((eq (sb!di:debug-var-validity ,var ,location) :valid)
481 ;;; This is used in constructing arg lists for debugger printing when
482 ;;; the arg list is unavailable, some arg is unavailable or unused,
484 (defstruct (unprintable-object
485 (:constructor make-unprintable-object (string))
486 (:print-object (lambda (x s)
487 (print-unreadable-object (x s :type t)
488 (write-string (unprintable-object-string x)
493 ;;; Print FRAME with verbosity level 1. If we hit a &REST arg, then
494 ;;; print as many of the values as possible, punting the loop over
495 ;;; lambda-list variables since any other arguments will be in the
496 ;;; &REST arg's list of values.
497 (defun print-frame-call-1 (frame)
498 (let* ((d-fun (sb!di:frame-debug-function frame))
499 (loc (sb!di:frame-code-location frame))
500 (results (list (sb!di:debug-function-name d-fun))))
502 (dolist (ele (sb!di:debug-function-lambda-list d-fun))
503 (lambda-list-element-dispatch ele
504 :required ((push (frame-call-arg ele loc frame) results))
505 :optional ((push (frame-call-arg (second ele) loc frame) results))
506 :keyword ((push (second ele) results)
507 (push (frame-call-arg (third ele) loc frame) results))
508 :deleted ((push (frame-call-arg ele loc frame) results))
509 :rest ((lambda-var-dispatch (second ele) loc
513 (append (reverse (sb!di:debug-var-value
517 (push (make-unprintable-object
518 "unavailable &REST argument")
520 (sb!di:lambda-list-unavailable
522 (push (make-unprintable-object "lambda list unavailable") results)))
523 (pprint-logical-block (*standard-output* nil)
524 (let ((x (nreverse (mapcar #'ensure-printable-object results))))
525 (format t "(~@<~S~{ ~_~S~}~:>)" (first x) (rest x))))
526 (when (sb!di:debug-function-kind d-fun)
528 (prin1 (sb!di:debug-function-kind d-fun))
531 (defun ensure-printable-object (object)
533 (with-open-stream (out (make-broadcast-stream))
537 (declare (ignore cond))
538 (make-unprintable-object "error printing object"))))
540 (defun frame-call-arg (var location frame)
541 (lambda-var-dispatch var location
542 (make-unprintable-object "unused argument")
543 (sb!di:debug-var-value var frame)
544 (make-unprintable-object "unavailable argument")))
546 ;;; Prints a representation of the function call causing FRAME to
547 ;;; exist. VERBOSITY indicates the level of information to output;
548 ;;; zero indicates just printing the debug-function's name, and one
549 ;;; indicates displaying call-like, one-liner format with argument
551 (defun print-frame-call (frame &key (verbosity 1) (number nil))
555 (format t "~&~S: " (sb!di:frame-number frame)))
556 (format t "~S" frame))
559 (format t "~&~S: " (sb!di:frame-number frame)))
560 (print-frame-call-1 frame)))
561 (when (>= verbosity 2)
562 (let ((loc (sb!di:frame-code-location frame)))
565 (sb!di:code-location-debug-block loc)
566 (format t "~%source: ")
567 (print-code-location-source-form loc 0))
568 (sb!di:debug-condition (ignore) ignore)
569 (error (c) (format t "error finding source: ~A" c))))))
573 (defvar *debugger-hook* nil
575 "This is either NIL or a function of two arguments, a condition and the value
576 of *DEBUGGER-HOOK*. This function can either handle the condition or return
577 which causes the standard debugger to execute. The system passes the value
578 of this variable to the function because it binds *DEBUGGER-HOOK* to NIL
579 around the invocation.")
581 ;;; These are bound on each invocation of INVOKE-DEBUGGER.
582 (defvar *debug-restarts*)
583 (defvar *debug-condition*)
585 (defun invoke-debugger (condition)
587 "Enter the debugger."
588 (let ((old-hook *debugger-hook*))
590 (let ((*debugger-hook* nil))
591 (funcall old-hook condition old-hook))))
592 (sb!unix:unix-sigsetmask 0)
594 ;; Elsewhere in the system, we use the SANE-PACKAGE function for
595 ;; this, but here causing an exception just as we're trying to handle
596 ;; an exception would be confusing, so instead we use a special hack.
597 (unless (and (packagep *package*)
598 (package-name *package*))
599 (setf *package* (find-package :cl-user))
600 (format *error-output*
601 "The value of ~S was not an undeleted PACKAGE. It has been
603 '*package* *package*))
604 (let (;; Save *PACKAGE* to protect it from WITH-STANDARD-IO-SYNTAX.
605 (original-package *package*))
606 (with-standard-io-syntax
607 (let* ((*debug-condition* condition)
608 (*debug-restarts* (compute-restarts condition))
609 ;; We want the i/o subsystem to be in a known, useful
610 ;; state, regardless of where the debugger was invoked in
611 ;; the program. WITH-STANDARD-IO-SYNTAX does some of that,
613 ;; 1. It doesn't affect our internal special variables
614 ;; like *CURRENT-LEVEL*.
615 ;; 2. It isn't customizable.
616 ;; 3. It doesn't set *PRINT-READABLY* or *PRINT-PRETTY*
617 ;; to the same value as the toplevel default.
618 ;; 4. It sets *PACKAGE* to COMMON-LISP-USER, which is not
619 ;; helpful behavior for a debugger.
620 ;; We try to remedy all these problems with explicit
622 (sb!kernel:*current-level* 0)
623 (*print-length* *debug-print-length*)
624 (*print-level* *debug-print-level*)
625 (*readtable* *debug-readtable*)
626 (*print-readably* nil)
628 (*package* original-package))
630 ;; Before we start our own output, finish any pending output.
631 ;; Otherwise, if the user tried to track the progress of
632 ;; his program using PRINT statements, he'd tend to lose
633 ;; the last line of output or so, and get confused.
634 (flush-standard-output-streams)
636 ;; (The initial output here goes to *ERROR-OUTPUT*, because the
637 ;; initial output is not interactive, just an error message,
638 ;; and when people redirect *ERROR-OUTPUT*, they could
639 ;; reasonably expect to see error messages logged there,
640 ;; regardless of what the debugger does afterwards.)
642 (format *error-output*
643 "~2&~@<debugger invoked on condition of type ~S: ~
645 (type-of *debug-condition*)
648 (format *error-output*
649 "~&(caught ~S trying to print ~S when entering debugger)~%"
651 '*debug-condition*)))
653 ;; After the initial error/condition/whatever announcement to
654 ;; *ERROR-OUTPUT*, we become interactive, and should talk on
655 ;; *DEBUG-IO* from now on. (KLUDGE: This is a normative
656 ;; statement, not a description of reality.:-| There's a lot of
657 ;; older debugger code which was written to do i/o on whatever
658 ;; stream was in fashion at the time, and not all of it has
659 ;; been converted to behave this way. -- WHN 2000-11-16)
660 (let (;; FIXME: The first two bindings here seem wrong,
661 ;; violating the principle of least surprise, and making
662 ;; it impossible for the user to do reasonable things
663 ;; like using PRINT at the debugger prompt to send output
664 ;; to the program's ordinary (possibly
665 ;; redirected-to-a-file) *STANDARD-OUTPUT*, or using
666 ;; PEEK-CHAR or some such thing on the program's ordinary
667 ;; (possibly also redirected) *STANDARD-INPUT*.
668 (*standard-input* *debug-io*)
669 (*standard-output* *debug-io*)
670 ;; This seems reasonable: e.g. if the user has redirected
671 ;; *ERROR-OUTPUT* to some log file, it's probably wrong
672 ;; to send errors which occur in interactive debugging to
673 ;; that file, and right to send them to *DEBUG-IO*.
674 (*error-output* *debug-io*))
675 (unless (typep condition 'step-condition)
677 "~%~@<Within the debugger, you can type HELP for help. At ~
678 any command prompt (within the debugger or not) you can ~
679 type (SB-EXT:QUIT) to terminate the SBCL executable. ~
680 The condition which caused the debugger to be entered ~
681 is bound to ~S.~:@>~2%"
683 (show-restarts *debug-restarts* *debug-io*)
685 (internal-debug))))))
687 (defun show-restarts (restarts s)
689 (format s "~&restarts:~%")
693 (dolist (restart restarts)
694 (let ((name (restart-name restart)))
696 (let ((len (length (princ-to-string name))))
697 (when (> len max-name-len)
698 (setf max-name-len len))))))
699 (unless (zerop max-name-len)
700 (incf max-name-len 3))
701 (dolist (restart restarts)
702 (let ((name (restart-name restart)))
703 (cond ((member name names-used)
704 (format s "~& ~2D: ~@VT~A~%" count max-name-len restart))
706 (format s "~& ~2D: [~VA] ~A~%"
707 count (- max-name-len 3) name restart)
708 (push name names-used))))
711 ;;; This calls DEBUG-LOOP, performing some simple initializations
712 ;;; before doing so. INVOKE-DEBUGGER calls this to actually get into
713 ;;; the debugger. SB!KERNEL::ERROR-ERROR calls this in emergencies
714 ;;; to get into a debug prompt as quickly as possible with as little
715 ;;; risk as possible for stepping on whatever is causing recursive
717 (defun internal-debug ()
718 (let ((*in-the-debugger* t)
719 (*read-suppress* nil))
720 (unless (typep *debug-condition* 'step-condition)
721 (clear-input *debug-io*))
723 #!+mp (sb!mp:without-scheduling (debug-loop))))
727 ;;; Note: This defaulted to T in CMU CL. The changed default in SBCL
728 ;;; was motivated by desire to play nicely with ILISP.
729 (defvar *flush-debug-errors* nil
731 "When set, avoid calling INVOKE-DEBUGGER recursively when errors occur while
732 executing in the debugger.")
735 (let* ((*debug-command-level* (1+ *debug-command-level*))
736 (*real-stack-top* (sb!di:top-frame))
737 (*stack-top* (or *stack-top-hint* *real-stack-top*))
738 (*stack-top-hint* nil)
739 (*current-frame* *stack-top*))
740 (handler-bind ((sb!di:debug-condition (lambda (condition)
741 (princ condition *debug-io*)
742 (throw 'debug-loop-catcher nil))))
744 (print-frame-call *current-frame* :verbosity 2)
746 (catch 'debug-loop-catcher
747 (handler-bind ((error #'(lambda (condition)
748 (when *flush-debug-errors*
749 (clear-input *debug-io*)
751 ;; FIXME: Doing input on *DEBUG-IO*
752 ;; and output on T seems broken.
754 "~&error flushed (because ~
756 '*flush-debug-errors*)
757 (throw 'debug-loop-catcher nil)))))
758 ;; We have to bind level for the restart function created by
759 ;; WITH-SIMPLE-RESTART.
760 (let ((level *debug-command-level*)
761 (restart-commands (make-restart-commands)))
762 (with-simple-restart (abort
763 "Reduce debugger level (to debug level ~D)."
765 (debug-prompt *debug-io*)
766 (force-output *debug-io*)
767 (let ((input (sb!int:get-stream-command *debug-io*)))
769 (let ((cmd-fun (debug-command-p
770 (sb!int:stream-command-name input)
774 (error "unknown stream-command: ~S" input))
776 (error "ambiguous debugger command: ~S" cmd-fun))
779 (sb!int:stream-command-args input))))))
782 (cmd-fun (debug-command-p exp
785 (debug-eval-print exp))
788 "~&Your command, ~S, is ambiguous:~%"
790 (dolist (ele cmd-fun)
791 (format t " ~A~%" ele)))
793 (funcall cmd-fun)))))))))))))))
795 ;;; FIXME: As far as I know, the CMU CL X86 codebase has never
796 ;;; supported access to the environment of the debugged function. It
797 ;;; would be really, really nice to make that work! (Until then,
798 ;;; non-NIL *AUTO-EVAL-IN-FRAME* seems to be useless, and as of
799 ;;; sbcl-0.6.10 it even seemed to be actively harmful, since the
800 ;;; debugger gets confused when trying to unwind the frames which
801 ;;; arise in SIGINT interrupts. So it's set to NIL.)
802 (defvar *auto-eval-in-frame* nil
804 "When set, evaluations in the debugger's command loop occur relative
805 to the current frame's environment without the need of debugger
806 forms that explicitly control this kind of evaluation. In an ideal
807 world, the default would be T, but since unfortunately the X86
808 debugger support isn't good enough to make this useful, the
809 default is NIL instead.")
811 ;;; FIXME: We could probably use INTERACTIVE-EVAL for much of this logic.
812 (defun debug-eval-print (expr)
813 (/noshow "entering DEBUG-EVAL-PRINT" expr)
814 (/noshow (fboundp 'compile))
815 (/noshow (and (fboundp 'compile) *auto-eval-in-frame*))
816 (setq +++ ++ ++ + + - - expr)
817 (let* ((values (multiple-value-list
818 (if (and (fboundp 'compile) *auto-eval-in-frame*)
819 (sb!di:eval-in-frame *current-frame* -)
821 (*standard-output* *debug-io*))
822 (/noshow "done with EVAL in DEBUG-EVAL-PRINT")
824 (if values (prin1 (car values)))
825 (dolist (x (cdr values))
828 (setq /// // // / / values)
829 (setq *** ** ** * * (car values))
830 ;; Make sure that nobody passes back an unbound marker.
834 ;; FIXME: The way INTERACTIVE-EVAL does this seems better.
835 (princ "Setting * to NIL (was unbound marker)."))))
837 ;;;; debug loop functions
839 ;;; These commands are functions, not really commands, so that users
840 ;;; can get their hands on the values returned.
842 (eval-when (:execute :compile-toplevel)
844 (sb!xc:defmacro define-var-operation (ref-or-set &optional value-var)
845 `(let* ((temp (etypecase name
846 (symbol (sb!di:debug-function-symbol-variables
847 (sb!di:frame-debug-function *current-frame*)
849 (simple-string (sb!di:ambiguous-debug-vars
850 (sb!di:frame-debug-function *current-frame*)
852 (location (sb!di:frame-code-location *current-frame*))
853 ;; Let's only deal with valid variables.
854 (vars (remove-if-not #'(lambda (v)
855 (eq (sb!di:debug-var-validity v location)
858 (declare (list vars))
860 (error "No known valid variables match ~S." name))
864 '(sb!di:debug-var-value (car vars) *current-frame*))
866 `(setf (sb!di:debug-var-value (car vars) *current-frame*)
869 ;; Since we have more than one, first see whether we have
870 ;; any variables that exactly match the specification.
871 (let* ((name (etypecase name
872 (symbol (symbol-name name))
873 (simple-string name)))
874 ;; FIXME: REMOVE-IF-NOT is deprecated, use STRING/=
876 (exact (remove-if-not (lambda (v)
877 (string= (sb!di:debug-var-symbol-name v)
880 (vars (or exact vars)))
881 (declare (simple-string name)
884 ;; Check now for only having one variable.
888 '(sb!di:debug-var-value (car vars) *current-frame*))
890 `(setf (sb!di:debug-var-value (car vars) *current-frame*)
892 ;; If there weren't any exact matches, flame about
893 ;; ambiguity unless all the variables have the same
898 (string= (sb!di:debug-var-symbol-name v)
899 (sb!di:debug-var-symbol-name (car vars))))
901 (error "specification ambiguous:~%~{ ~A~%~}"
902 (mapcar #'sb!di:debug-var-symbol-name
905 :key #'sb!di:debug-var-symbol-name))))
906 ;; All names are the same, so see whether the user
907 ;; ID'ed one of them.
909 (let ((v (find id vars :key #'sb!di:debug-var-id)))
912 "invalid variable ID, ~D: should have been one of ~S"
914 (mapcar #'sb!di:debug-var-id vars)))
917 '(sb!di:debug-var-value v *current-frame*))
919 `(setf (sb!di:debug-var-value v *current-frame*)
922 (error "Specify variable ID to disambiguate ~S. Use one of ~S."
924 (mapcar #'sb!di:debug-var-id vars)))))))))
928 ;;; FIXME: This doesn't work. It would be real nice we could make it
929 ;;; work! Alas, it doesn't seem to work in CMU CL X86 either..
930 (defun var (name &optional (id 0 id-supplied))
932 "Return a variable's value if possible. NAME is a simple-string or symbol.
933 If it is a simple-string, it is an initial substring of the variable's name.
934 If name is a symbol, it has the same name and package as the variable whose
935 value this function returns. If the symbol is uninterned, then the variable
936 has the same name as the symbol, but it has no package.
938 If name is the initial substring of variables with different names, then
939 this return no values after displaying the ambiguous names. If name
940 determines multiple variables with the same name, then you must use the
941 optional id argument to specify which one you want. If you left id
942 unspecified, then this returns no values after displaying the distinguishing
945 The result of this function is limited to the availability of variable
946 information. This is SETF'able."
947 (define-var-operation :ref))
948 (defun (setf var) (value name &optional (id 0 id-supplied))
949 (define-var-operation :set value))
951 ;;; This returns the COUNT'th arg as the user sees it from args, the
952 ;;; result of SB!DI:DEBUG-FUNCTION-LAMBDA-LIST. If this returns a
953 ;;; potential DEBUG-VAR from the lambda-list, then the second value is
954 ;;; T. If this returns a keyword symbol or a value from a rest arg,
955 ;;; then the second value is NIL.
956 (declaim (ftype (function (index list)) nth-arg))
957 (defun nth-arg (count args)
959 (dolist (ele args (error "The argument specification ~S is out of range."
961 (lambda-list-element-dispatch ele
962 :required ((if (zerop n) (return (values ele t))))
963 :optional ((if (zerop n) (return (values (second ele) t))))
964 :keyword ((cond ((zerop n)
965 (return (values (second ele) nil)))
967 (return (values (third ele) t)))))
968 :deleted ((if (zerop n) (return (values ele t))))
969 :rest ((let ((var (second ele)))
970 (lambda-var-dispatch var (sb!di:frame-code-location
972 (error "unused &REST argument before n'th
975 (sb!di:debug-var-value var *current-frame*)
977 "The argument specification ~S is out of range."
980 (return-from nth-arg (values value nil))
982 (error "invalid &REST argument before n'th argument")))))
987 "Returns the N'th argument's value if possible. Argument zero is the first
988 argument in a frame's default printed representation. Count keyword/value
989 pairs as separate arguments."
990 (multiple-value-bind (var lambda-var-p)
991 (nth-arg n (handler-case (sb!di:debug-function-lambda-list
992 (sb!di:frame-debug-function *current-frame*))
993 (sb!di:lambda-list-unavailable ()
994 (error "No argument values are available."))))
996 (lambda-var-dispatch var (sb!di:frame-code-location *current-frame*)
997 (error "Unused arguments have no values.")
998 (sb!di:debug-var-value var *current-frame*)
999 (error "invalid argument value"))
1002 ;;;; machinery for definition of debug loop commands
1004 (defvar *debug-commands* nil)
1006 ;;; Interface to *DEBUG-COMMANDS*. No required arguments in args are
1008 (defmacro !def-debug-command (name args &rest body)
1009 (let ((fun-name (symbolicate name "-DEBUG-COMMAND")))
1011 (setf *debug-commands*
1012 (remove ,name *debug-commands* :key #'car :test #'string=))
1013 (defun ,fun-name ,args
1014 (unless *in-the-debugger*
1015 (error "invoking debugger command while outside the debugger"))
1017 (push (cons ,name #',fun-name) *debug-commands*)
1020 (defun !def-debug-command-alias (new-name existing-name)
1021 (let ((pair (assoc existing-name *debug-commands* :test #'string=)))
1022 (unless pair (error "unknown debug command name: ~S" existing-name))
1023 (push (cons new-name (cdr pair)) *debug-commands*))
1026 ;;; This takes a symbol and uses its name to find a debugger command,
1027 ;;; using initial substring matching. It returns the command function
1028 ;;; if form identifies only one command, but if form is ambiguous,
1029 ;;; this returns a list of the command names. If there are no matches,
1030 ;;; this returns nil. Whenever the loop that looks for a set of
1031 ;;; possibilities encounters an exact name match, we return that
1032 ;;; command function immediately.
1033 (defun debug-command-p (form &optional other-commands)
1034 (if (or (symbolp form) (integerp form))
1038 (format nil "~D" form)))
1041 (declare (simple-string name)
1045 ;; Find matching commands, punting if exact match.
1046 (flet ((match-command (ele)
1047 (let* ((str (car ele))
1048 (str-len (length str)))
1049 (declare (simple-string str)
1051 (cond ((< str-len len))
1053 (when (string= name str :end1 len :end2 len)
1054 (return-from debug-command-p (cdr ele))))
1055 ((string= name str :end1 len :end2 len)
1057 (mapc #'match-command *debug-commands*)
1058 (mapc #'match-command other-commands))
1060 ;; Return the right value.
1061 (cond ((not res) nil)
1064 (t ; Just return the names.
1065 (do ((cmds res (cdr cmds)))
1067 (setf (car cmds) (caar cmds))))))))
1069 ;;; Return a list of debug commands (in the same format as
1070 ;;; *debug-commands*) that invoke each active restart.
1072 ;;; Two commands are made for each restart: one for the number, and
1073 ;;; one for the restart name (unless it's been shadowed by an earlier
1074 ;;; restart of the same name, or it is NIL).
1075 (defun make-restart-commands (&optional (restarts *debug-restarts*))
1077 (num 0)) ; better be the same as show-restarts!
1078 (dolist (restart restarts)
1079 (let ((name (string (restart-name restart))))
1081 #'(lambda () (invoke-restart-interactively restart))))
1082 (push (cons (format nil "~d" num) restart-fun) commands)
1083 (unless (or (null (restart-name restart))
1084 (find name commands :key #'car :test #'string=))
1085 (push (cons name restart-fun) commands))))
1089 ;;;; frame-changing commands
1091 (!def-debug-command "UP" ()
1092 (let ((next (sb!di:frame-up *current-frame*)))
1094 (setf *current-frame* next)
1095 (print-frame-call next))
1097 (format t "~&Top of stack.")))))
1099 (!def-debug-command "DOWN" ()
1100 (let ((next (sb!di:frame-down *current-frame*)))
1102 (setf *current-frame* next)
1103 (print-frame-call next))
1105 (format t "~&Bottom of stack.")))))
1107 (!def-debug-command-alias "D" "DOWN")
1109 ;;; CMU CL had this command, but SBCL doesn't, since it's redundant
1110 ;;; with "FRAME 0", and it interferes with abbreviations for the
1111 ;;; TOPLEVEL restart.
1112 ;;;(!def-debug-command "TOP" ()
1113 ;;; (do ((prev *current-frame* lead)
1114 ;;; (lead (sb!di:frame-up *current-frame*) (sb!di:frame-up lead)))
1116 ;;; (setf *current-frame* prev)
1117 ;;; (print-frame-call prev))))
1119 (!def-debug-command "BOTTOM" ()
1120 (do ((prev *current-frame* lead)
1121 (lead (sb!di:frame-down *current-frame*) (sb!di:frame-down lead)))
1123 (setf *current-frame* prev)
1124 (print-frame-call prev))))
1126 (!def-debug-command-alias "B" "BOTTOM")
1128 (!def-debug-command "FRAME" (&optional
1129 (n (read-prompting-maybe "frame number: ")))
1130 (setf *current-frame*
1131 (multiple-value-bind (next-frame-fun limit-string)
1132 (if (< n (sb!di:frame-number *current-frame*))
1133 (values #'sb!di:frame-up "top")
1134 (values #'sb!di:frame-down "bottom"))
1135 (do ((frame *current-frame*))
1136 ((= n (sb!di:frame-number frame))
1138 (let ((next-frame (funcall next-frame-fun frame)))
1140 (setf frame next-frame))
1143 "The ~A of the stack was encountered.~%"
1145 (return frame)))))))
1146 (print-frame-call *current-frame*))
1148 (!def-debug-command-alias "F" "FRAME")
1150 ;;;; commands for entering and leaving the debugger
1152 ;;; CMU CL supported this QUIT debug command, but SBCL provides this
1153 ;;; functionality with a restart instead. (The QUIT debug command was
1154 ;;; removed because it's confusing to have "quit" mean two different
1155 ;;; things in the system, "restart the top level REPL" in the debugger
1156 ;;; and "terminate the Lisp system" as the SB-EXT:QUIT function.)
1158 ;;;(!def-debug-command "QUIT" ()
1159 ;;; (throw 'sb!impl::top-level-catcher nil))
1161 ;;; CMU CL supported this GO debug command, but SBCL doesn't -- in
1162 ;;; SBCL you just type the CONTINUE restart name instead (or "RESTART
1163 ;;; CONTINUE", that's OK too).
1165 ;;;(!def-debug-command "GO" ()
1166 ;;; (continue *debug-condition*)
1167 ;;; (error "There is no restart named CONTINUE."))
1169 (!def-debug-command "RESTART" ()
1170 (let ((num (read-if-available :prompt)))
1171 (when (eq num :prompt)
1172 (show-restarts *debug-restarts* *debug-io*)
1173 (write-string "restart: ")
1175 (setf num (read *standard-input*)))
1176 (let ((restart (typecase num
1178 (nth num *debug-restarts*))
1180 (find num *debug-restarts* :key #'restart-name
1181 :test #'(lambda (sym1 sym2)
1182 (string= (symbol-name sym1)
1183 (symbol-name sym2)))))
1185 (format t "~S is invalid as a restart name.~%" num)
1186 (return-from restart-debug-command nil)))))
1188 (invoke-restart-interactively restart)
1189 ;; FIXME: Even if this isn't handled by WARN, it probably
1190 ;; shouldn't go to *STANDARD-OUTPUT*, but *ERROR-OUTPUT* or
1191 ;; *QUERY-IO* or something. Look through this file to
1192 ;; straighten out stream usage.
1193 (princ "There is no such restart.")))))
1195 ;;;; information commands
1197 (!def-debug-command "HELP" ()
1198 ;; CMU CL had a little toy pager here, but "if you aren't running
1199 ;; ILISP (or a smart windowing system, or something) you deserve to
1200 ;; lose", so we've dropped it in SBCL. However, in case some
1201 ;; desperate holdout is running this on a dumb terminal somewhere,
1202 ;; we tell him where to find the message stored as a string.
1204 "~&~A~2%(The HELP string is stored in ~S.)~%"
1206 '*debug-help-string*))
1208 (!def-debug-command-alias "?" "HELP")
1210 (!def-debug-command "ERROR" ()
1211 (format *debug-io* "~A~%" *debug-condition*)
1212 (show-restarts *debug-restarts* *debug-io*))
1214 (!def-debug-command "BACKTRACE" ()
1215 (backtrace (read-if-available most-positive-fixnum)))
1217 (!def-debug-command "PRINT" ()
1218 (print-frame-call *current-frame*))
1220 (!def-debug-command-alias "P" "PRINT")
1222 (!def-debug-command "LIST-LOCALS" ()
1223 (let ((d-fun (sb!di:frame-debug-function *current-frame*)))
1224 (if (sb!di:debug-var-info-available d-fun)
1225 (let ((*standard-output* *debug-io*)
1226 (location (sb!di:frame-code-location *current-frame*))
1227 (prefix (read-if-available nil))
1230 (dolist (v (sb!di:ambiguous-debug-vars
1232 (if prefix (string prefix) "")))
1234 (when (eq (sb!di:debug-var-validity v location) :valid)
1235 (setf any-valid-p t)
1236 (format t "~S~:[#~D~;~*~] = ~S~%"
1237 (sb!di:debug-var-symbol v)
1238 (zerop (sb!di:debug-var-id v))
1239 (sb!di:debug-var-id v)
1240 (sb!di:debug-var-value v *current-frame*))))
1244 (format t "There are no local variables ~@[starting with ~A ~]~
1248 (format t "All variables ~@[starting with ~A ~]currently ~
1249 have invalid values."
1251 (write-line "There is no variable information available."))))
1253 (!def-debug-command-alias "L" "LIST-LOCALS")
1255 (!def-debug-command "SOURCE" ()
1257 (print-code-location-source-form (sb!di:frame-code-location *current-frame*)
1258 (read-if-available 0)))
1260 ;;;; source location printing
1262 ;;; We cache a stream to the last valid file debug source so that we
1263 ;;; won't have to repeatedly open the file.
1265 ;;; KLUDGE: This sounds like a bug, not a feature. Opening files is fast
1266 ;;; in the 1990s, so the benefit is negligible, less important than the
1267 ;;; potential of extra confusion if someone changes the source during
1268 ;;; a debug session and the change doesn't show up. And removing this
1269 ;;; would simplify the system, which I like. -- WHN 19990903
1270 (defvar *cached-debug-source* nil)
1271 (declaim (type (or sb!di:debug-source null) *cached-debug-source*))
1272 (defvar *cached-source-stream* nil)
1273 (declaim (type (or stream null) *cached-source-stream*))
1275 ;;; To suppress the read-time evaluation #. macro during source read,
1276 ;;; *READTABLE* is modified. *READTABLE* is cached to avoid
1277 ;;; copying it each time, and invalidated when the
1278 ;;; *CACHED-DEBUG-SOURCE* has changed.
1279 (defvar *cached-readtable* nil)
1280 (declaim (type (or readtable null) *cached-readtable*))
1282 (pushnew #'(lambda ()
1283 (setq *cached-debug-source* nil *cached-source-stream* nil
1284 *cached-readtable* nil))
1285 sb!int:*before-save-initializations*)
1287 ;;; We also cache the last top-level form that we printed a source for
1288 ;;; so that we don't have to do repeated reads and calls to
1289 ;;; FORM-NUMBER-TRANSLATIONS.
1290 (defvar *cached-top-level-form-offset* nil)
1291 (declaim (type (or index null) *cached-top-level-form-offset*))
1292 (defvar *cached-top-level-form*)
1293 (defvar *cached-form-number-translations*)
1295 ;;; Given a code location, return the associated form-number
1296 ;;; translations and the actual top-level form. We check our cache ---
1297 ;;; if there is a miss, we dispatch on the kind of the debug source.
1298 (defun get-top-level-form (location)
1299 (let ((d-source (sb!di:code-location-debug-source location)))
1300 (if (and (eq d-source *cached-debug-source*)
1301 (eql (sb!di:code-location-top-level-form-offset location)
1302 *cached-top-level-form-offset*))
1303 (values *cached-form-number-translations* *cached-top-level-form*)
1304 (let* ((offset (sb!di:code-location-top-level-form-offset location))
1306 (ecase (sb!di:debug-source-from d-source)
1307 (:file (get-file-top-level-form location))
1308 (:lisp (svref (sb!di:debug-source-name d-source) offset)))))
1309 (setq *cached-top-level-form-offset* offset)
1310 (values (setq *cached-form-number-translations*
1311 (sb!di:form-number-translations res offset))
1312 (setq *cached-top-level-form* res))))))
1314 ;;; Locate the source file (if it still exists) and grab the top-level
1315 ;;; form. If the file is modified, we use the top-level-form offset
1316 ;;; instead of the recorded character offset.
1317 (defun get-file-top-level-form (location)
1318 (let* ((d-source (sb!di:code-location-debug-source location))
1319 (tlf-offset (sb!di:code-location-top-level-form-offset location))
1320 (local-tlf-offset (- tlf-offset
1321 (sb!di:debug-source-root-number d-source)))
1323 (aref (or (sb!di:debug-source-start-positions d-source)
1324 (error "no start positions map"))
1326 (name (sb!di:debug-source-name d-source)))
1327 (unless (eq d-source *cached-debug-source*)
1328 (unless (and *cached-source-stream*
1329 (equal (pathname *cached-source-stream*)
1331 (setq *cached-readtable* nil)
1332 (when *cached-source-stream* (close *cached-source-stream*))
1333 (setq *cached-source-stream* (open name :if-does-not-exist nil))
1334 (unless *cached-source-stream*
1335 (error "The source file no longer exists:~% ~A" (namestring name)))
1336 (format t "~%; file: ~A~%" (namestring name)))
1338 (setq *cached-debug-source*
1339 (if (= (sb!di:debug-source-created d-source)
1340 (file-write-date name))
1344 ((eq *cached-debug-source* d-source)
1345 (file-position *cached-source-stream* char-offset))
1347 (format t "~%; File has been modified since compilation:~%; ~A~@
1348 ; Using form offset instead of character position.~%"
1350 (file-position *cached-source-stream* 0)
1351 (let ((*read-suppress* t))
1352 (dotimes (i local-tlf-offset)
1353 (read *cached-source-stream*)))))
1354 (unless *cached-readtable*
1355 (setq *cached-readtable* (copy-readtable))
1356 (set-dispatch-macro-character
1358 #'(lambda (stream sub-char &rest rest)
1359 (declare (ignore rest sub-char))
1360 (let ((token (read stream t nil t)))
1361 (format nil "#.~S" token)))
1362 *cached-readtable*))
1363 (let ((*readtable* *cached-readtable*))
1364 (read *cached-source-stream*))))
1366 (defun print-code-location-source-form (location context)
1367 (let* ((location (maybe-block-start-location location))
1368 (form-num (sb!di:code-location-form-number location)))
1369 (multiple-value-bind (translations form) (get-top-level-form location)
1370 (unless (< form-num (length translations))
1371 (error "The source path no longer exists."))
1372 (prin1 (sb!di:source-path-context form
1373 (svref translations form-num)
1376 ;;; breakpoint and step commands
1378 ;;; Step to the next code-location.
1379 (!def-debug-command "STEP" ()
1380 (setf *number-of-steps* (read-if-available 1))
1381 (set-step-breakpoint *current-frame*)
1382 (continue *debug-condition*)
1383 (error "couldn't continue"))
1385 ;;; List possible breakpoint locations, which ones are active, and
1386 ;;; where the CONTINUE restart will transfer control. Set
1387 ;;; *POSSIBLE-BREAKPOINTS* to the code-locations which can then be
1388 ;;; used by sbreakpoint.
1389 (!def-debug-command "LIST-LOCATIONS" ()
1390 (let ((df (read-if-available *default-breakpoint-debug-function*)))
1392 (setf df (sb!di:function-debug-function (eval df)))
1393 (setf *default-breakpoint-debug-function* df))
1395 (not *default-breakpoint-debug-function*))
1396 (setf df (sb!di:frame-debug-function *current-frame*))
1397 (setf *default-breakpoint-debug-function* df)))
1398 (setf *possible-breakpoints* (possible-breakpoints df)))
1399 (let ((continue-at (sb!di:frame-code-location *current-frame*)))
1400 (let ((active (location-in-list *default-breakpoint-debug-function*
1401 *breakpoints* :function-start))
1402 (here (sb!di:code-location=
1403 (sb!di:debug-function-start-location
1404 *default-breakpoint-debug-function*) continue-at)))
1405 (when (or active here)
1406 (format t "::FUNCTION-START ")
1407 (when active (format t " *Active*"))
1408 (when here (format t " *Continue here*"))))
1410 (let ((prev-location nil)
1415 (let ((this-num (1- this-num)))
1416 (if (= prev-num this-num)
1417 (format t "~&~D: " prev-num)
1418 (format t "~&~D-~D: " prev-num this-num)))
1419 (print-code-location-source-form prev-location 0)
1420 (when *print-location-kind*
1421 (format t "~S " (sb!di:code-location-kind prev-location)))
1422 (when (location-in-list prev-location *breakpoints*)
1423 (format t " *Active*"))
1424 (when (sb!di:code-location= prev-location continue-at)
1425 (format t " *Continue here*")))))
1427 (dolist (code-location *possible-breakpoints*)
1428 (when (or *print-location-kind*
1429 (location-in-list code-location *breakpoints*)
1430 (sb!di:code-location= code-location continue-at)
1432 (not (eq (sb!di:code-location-debug-source code-location)
1433 (sb!di:code-location-debug-source prev-location)))
1434 (not (eq (sb!di:code-location-top-level-form-offset
1436 (sb!di:code-location-top-level-form-offset
1438 (not (eq (sb!di:code-location-form-number code-location)
1439 (sb!di:code-location-form-number prev-location))))
1441 (setq prev-location code-location prev-num this-num))
1445 (when (location-in-list *default-breakpoint-debug-function*
1448 (format t "~&::FUNCTION-END *Active* "))))
1450 (!def-debug-command-alias "LL" "LIST-LOCATIONS")
1452 ;;; Set breakpoint at the given number.
1453 (!def-debug-command "BREAKPOINT" ()
1454 (let ((index (read-prompting-maybe "location number, :START, or :END: "))
1458 (print-functions nil)
1461 (place *default-breakpoint-debug-function*))
1462 (flet ((get-command-line ()
1463 (let ((command-line nil)
1466 (let ((next-input (read-if-available unique)))
1467 (when (eq next-input unique) (return))
1468 (push next-input command-line)))
1469 (nreverse command-line)))
1470 (set-vars-from-command-line (command-line)
1471 (do ((arg (pop command-line) (pop command-line)))
1474 (:condition (setf condition (pop command-line)))
1475 (:print (push (pop command-line) print))
1476 (:break (setf break (pop command-line)))
1478 (setf function (eval (pop command-line)))
1479 (setf *default-breakpoint-debug-function*
1480 (sb!di:function-debug-function function))
1481 (setf place *default-breakpoint-debug-function*)
1482 (setf *possible-breakpoints*
1483 (possible-breakpoints
1484 *default-breakpoint-debug-function*))))))
1485 (setup-function-start ()
1486 (let ((code-loc (sb!di:debug-function-start-location place)))
1487 (setf bp (sb!di:make-breakpoint #'main-hook-function
1489 :kind :function-start))
1490 (setf break (sb!di:preprocess-for-eval break code-loc))
1491 (setf condition (sb!di:preprocess-for-eval condition code-loc))
1492 (dolist (form print)
1493 (push (cons (sb!di:preprocess-for-eval form code-loc) form)
1495 (setup-function-end ()
1497 (sb!di:make-breakpoint #'main-hook-function
1499 :kind :function-end))
1501 ;; FIXME: These and any other old (COERCE `(LAMBDA ..) ..)
1502 ;; forms should be converted to shiny new (LAMBDA ..) forms.
1503 ;; (Search the sources for "coerce.*\(lambda".)
1504 (coerce `(lambda (dummy)
1505 (declare (ignore dummy)) ,break)
1507 (setf condition (coerce `(lambda (dummy)
1508 (declare (ignore dummy)) ,condition)
1510 (dolist (form print)
1512 (coerce `(lambda (dummy)
1513 (declare (ignore dummy)) ,form) 'function)
1516 (setup-code-location ()
1517 (setf place (nth index *possible-breakpoints*))
1518 (setf bp (sb!di:make-breakpoint #'main-hook-function
1520 :kind :code-location))
1521 (dolist (form print)
1523 (sb!di:preprocess-for-eval form place)
1526 (setf break (sb!di:preprocess-for-eval break place))
1527 (setf condition (sb!di:preprocess-for-eval condition place))))
1528 (set-vars-from-command-line (get-command-line))
1530 ((or (eq index :start) (eq index :s))
1531 (setup-function-start))
1532 ((or (eq index :end) (eq index :e))
1533 (setup-function-end))
1535 (setup-code-location)))
1536 (sb!di:activate-breakpoint bp)
1537 (let* ((new-bp-info (create-breakpoint-info place bp index
1539 :print print-functions
1540 :condition condition))
1541 (old-bp-info (location-in-list new-bp-info *breakpoints*)))
1543 (sb!di:deactivate-breakpoint (breakpoint-info-breakpoint
1545 (setf *breakpoints* (remove old-bp-info *breakpoints*))
1546 (format t "previous breakpoint removed~%"))
1547 (push new-bp-info *breakpoints*))
1548 (print-breakpoint-info (first *breakpoints*))
1549 (format t "~&added"))))
1551 (!def-debug-command-alias "BP" "BREAKPOINT")
1553 ;;; List all breakpoints which are set.
1554 (!def-debug-command "LIST-BREAKPOINTS" ()
1556 (sort *breakpoints* #'< :key #'breakpoint-info-breakpoint-number))
1557 (dolist (info *breakpoints*)
1558 (print-breakpoint-info info)))
1560 (!def-debug-command-alias "LB" "LIST-BREAKPOINTS")
1561 (!def-debug-command-alias "LBP" "LIST-BREAKPOINTS")
1563 ;;; Remove breakpoint N, or remove all breakpoints if no N given.
1564 (!def-debug-command "DELETE-BREAKPOINT" ()
1565 (let* ((index (read-if-available nil))
1567 (find index *breakpoints* :key #'breakpoint-info-breakpoint-number)))
1569 (sb!di:delete-breakpoint (breakpoint-info-breakpoint bp-info))
1570 (setf *breakpoints* (remove bp-info *breakpoints*))
1571 (format t "breakpoint ~S removed~%" index))
1572 (index (format t "The breakpoint doesn't exist."))
1574 (dolist (ele *breakpoints*)
1575 (sb!di:delete-breakpoint (breakpoint-info-breakpoint ele)))
1576 (setf *breakpoints* nil)
1577 (format t "all breakpoints deleted~%")))))
1579 (!def-debug-command-alias "DBP" "DELETE-BREAKPOINT")
1581 ;;; miscellaneous commands
1583 (!def-debug-command "DESCRIBE" ()
1584 (let* ((curloc (sb!di:frame-code-location *current-frame*))
1585 (debug-fun (sb!di:code-location-debug-function curloc))
1586 (function (sb!di:debug-function-function debug-fun)))
1589 (format t "can't figure out the function for this frame"))))
1591 ;;;; debug loop command utilities
1593 (defun read-prompting-maybe (prompt &optional (in *standard-input*)
1594 (out *standard-output*))
1595 (unless (sb!int:listen-skip-whitespace in)
1600 (defun read-if-available (default &optional (stream *standard-input*))
1601 (if (sb!int:listen-skip-whitespace stream)