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 to and
130 ;;; 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 breakpoint-info
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. One of
169 ;;; 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, debug-function,
205 ;;; or breakpoint-info). If the item passed is a debug function then kind will
206 ;;; be compared if it was specified. The kind if also compared if a
207 ;;; breakpoint-info is passed since it's in the breakpoint. The info structure
208 ;;; 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 location.
231 ;;; Used by source printing to some information instead of none for the user.
232 (defun maybe-block-start-location (loc)
233 (if (sb!di:code-location-unknown-p loc)
234 (let* ((block (sb!di:code-location-debug-block loc))
235 (start (sb!di:do-debug-block-locations (loc block)
237 (cond ((and (not (sb!di:debug-block-elsewhere-p block))
239 ;; FIXME: Why output on T instead of *DEBUG-FOO* or something?
240 (format t "~%unknown location: using block start~%")
246 ;;;; the BREAKPOINT-INFO structure
248 ;;; info about a made breakpoint
249 (defstruct breakpoint-info
250 ;; where we are going to stop
251 (place (required-argument)
252 :type (or sb!di:code-location sb!di:debug-function))
253 ;; the breakpoint returned by sb!di:make-breakpoint
254 (breakpoint (required-argument) :type sb!di:breakpoint)
255 ;; the function returned from sb!di:preprocess-for-eval. If result is
256 ;; non-NIL, drop into the debugger.
257 (break #'identity :type function)
258 ;; the function returned from sb!di:preprocess-for-eval. If result is
259 ;; non-NIL, eval (each) print and print results.
260 (condition #'identity :type function)
261 ;; the list of functions from sb!di:preprocess-for-eval to evaluate. Results
262 ;; are conditionally printed. Car of each element is the function, cdr is the
263 ;; form it goes with.
264 (print nil :type list)
265 ;; the number used when listing the possible breakpoints within a function.
266 ;; Could also be a symbol such as start or end.
267 (code-location-number (required-argument) :type (or symbol integer))
268 ;; the number used when listing the breakpoints active and to delete
270 (breakpoint-number (required-argument) :type integer))
272 ;;; Return a new BREAKPOINT-INFO structure with the info passed.
273 (defun create-breakpoint-info (place breakpoint code-location-number
274 &key (break #'identity)
275 (condition #'identity) (print nil))
277 (sort *breakpoints* #'< :key #'breakpoint-info-breakpoint-number))
278 (let ((breakpoint-number
279 (do ((i 1 (incf i)) (breakpoints *breakpoints* (rest breakpoints)))
280 ((or (> i (length *breakpoints*))
281 (not (= i (breakpoint-info-breakpoint-number
282 (first breakpoints)))))
285 (make-breakpoint-info :place place :breakpoint breakpoint
286 :code-location-number code-location-number
287 :breakpoint-number breakpoint-number
288 :break break :condition condition :print print)))
290 ;;; Print the breakpoint info for the breakpoint-info structure passed.
291 (defun print-breakpoint-info (breakpoint-info)
292 (let ((place (breakpoint-info-place breakpoint-info))
293 (bp-number (breakpoint-info-breakpoint-number breakpoint-info))
294 (loc-number (breakpoint-info-code-location-number breakpoint-info)))
295 (case (sb!di:breakpoint-kind (breakpoint-info-breakpoint breakpoint-info))
297 (print-code-location-source-form place 0)
302 (sb!di:debug-function-name (sb!di:code-location-debug-function
305 (format t "~&~S: FUNCTION-START in ~S" bp-number
306 (sb!di:debug-function-name place)))
308 (format t "~&~S: FUNCTION-END in ~S" bp-number
309 (sb!di:debug-function-name place))))))
311 ;;;; MAIN-HOOK-FUNCTION for steps and breakpoints
313 ;;; This must be passed as the hook function. It keeps track of where step
315 (defun main-hook-function (current-frame breakpoint &optional return-vals
317 (setf *default-breakpoint-debug-function*
318 (sb!di:frame-debug-function current-frame))
319 (dolist (step-info *step-breakpoints*)
320 (sb!di:delete-breakpoint (breakpoint-info-breakpoint step-info))
321 (let ((bp-info (location-in-list step-info *breakpoints*)))
323 (sb!di:activate-breakpoint (breakpoint-info-breakpoint bp-info)))))
324 (let ((*stack-top-hint* current-frame)
326 (location-in-list (sb!di:breakpoint-what breakpoint)
328 (sb!di:breakpoint-kind breakpoint)))
330 (location-in-list (sb!di:breakpoint-what breakpoint)
332 (sb!di:breakpoint-kind breakpoint)))
336 (setf *step-breakpoints* nil)
337 (labels ((build-string (str)
338 (setf string (concatenate 'string string str)))
339 (print-common-info ()
341 (with-output-to-string (*standard-output*)
342 (when function-end-cookie
343 (format t "~%Return values: ~S" return-vals))
345 (when (breakpoint-info-print bp-hit-info)
347 (print-frame-call current-frame))
348 (dolist (print (breakpoint-info-print bp-hit-info))
349 (format t "~& ~S = ~S" (rest print)
350 (funcall (first print) current-frame))))))))
352 (setf break (funcall (breakpoint-info-break bp-hit-info)
354 (setf condition (funcall (breakpoint-info-condition bp-hit-info)
356 (cond ((and bp-hit-info step-hit-info (= 1 *number-of-steps*))
357 (build-string (format nil "~&*Step (to a breakpoint)*"))
360 ((and bp-hit-info step-hit-info break)
361 (build-string (format nil "~&*Step (to a breakpoint)*"))
364 ((and bp-hit-info step-hit-info)
366 (format t "~A" string)
367 (decf *number-of-steps*)
368 (set-step-breakpoint current-frame))
369 ((and step-hit-info (= 1 *number-of-steps*))
370 (build-string "*Step*")
371 (break (make-condition 'step-condition :format-control string)))
373 (decf *number-of-steps*)
374 (set-step-breakpoint current-frame))
377 (build-string (format nil "~&*Breakpoint hit*")))
381 (format t "~A" string)))
383 (break "error in main-hook-function: unknown breakpoint"))))))
385 ;;; Set breakpoints at the next possible code-locations. After calling
386 ;;; this, either (CONTINUE) if in the debugger or just let program flow
387 ;;; return if in a hook function.
388 (defun set-step-breakpoint (frame)
390 ((sb!di:debug-block-elsewhere-p (sb!di:code-location-debug-block
391 (sb!di:frame-code-location frame)))
392 ;; FIXME: FORMAT T is used for error output here and elsewhere in
394 (format t "cannot step, in elsewhere code~%"))
396 (let* ((code-location (sb!di:frame-code-location frame))
397 (next-code-locations (next-code-locations code-location)))
400 (dolist (code-location next-code-locations)
401 (let ((bp-info (location-in-list code-location *breakpoints*)))
403 (sb!di:deactivate-breakpoint (breakpoint-info-breakpoint
405 (let ((bp (sb!di:make-breakpoint #'main-hook-function code-location
406 :kind :code-location)))
407 (sb!di:activate-breakpoint bp)
408 (push (create-breakpoint-info code-location bp 0)
409 *step-breakpoints*))))
411 (let* ((debug-function (sb!di:frame-debug-function *current-frame*))
412 (bp (sb!di:make-breakpoint #'main-hook-function debug-function
413 :kind :function-end)))
414 (sb!di:activate-breakpoint bp)
415 (push (create-breakpoint-info debug-function bp 0)
416 *step-breakpoints*))))))))
420 ;;; ANSI specifies that this macro shall exist, even if only as a
421 ;;; trivial placeholder like this.
422 (defmacro step (form)
423 "a trivial placeholder implementation of the CL:STEP macro required by
430 (defun backtrace (&optional (count most-positive-fixnum)
431 (*standard-output* *debug-io*))
433 "Show a listing of the call stack going down from the current frame. In the
434 debugger, the current frame is indicated by the prompt. Count is how many
436 (fresh-line *standard-output*)
437 (do ((frame (if *in-the-debugger* *current-frame* (sb!di:top-frame))
438 (sb!di:frame-down frame))
439 (count count (1- count)))
440 ((or (null frame) (zerop count)))
441 (print-frame-call frame :number t))
442 (fresh-line *standard-output*)
447 (eval-when (:compile-toplevel :execute)
449 ;;; This is a convenient way to express what to do for each type of lambda-list
451 (sb!xc:defmacro lambda-list-element-dispatch (element
462 (ecase (car ,element)
463 (:optional ,@optional)
465 (:keyword ,@keyword)))
467 (assert (eq ,element :deleted))
470 (sb!xc:defmacro lambda-var-dispatch (variable location deleted valid other)
471 (let ((var (gensym)))
472 `(let ((,var ,variable))
473 (cond ((eq ,var :deleted) ,deleted)
474 ((eq (sb!di:debug-var-validity ,var ,location) :valid)
480 ;;; This is used in constructing arg lists for debugger printing when
481 ;;; the arg list is unavailable, some arg is unavailable or unused,
483 (defstruct (unprintable-object
484 (:constructor make-unprintable-object (string))
485 (:print-object (lambda (x s)
486 (print-unreadable-object (x s :type t)
487 (write-string (unprintable-object-string x)
491 ;;; Print FRAME with verbosity level 1. If we hit a &REST arg, then
492 ;;; print as many of the values as possible, punting the loop over
493 ;;; lambda-list variables since any other arguments will be in the
494 ;;; &REST arg's list of values.
495 (defun print-frame-call-1 (frame)
496 (let* ((d-fun (sb!di:frame-debug-function frame))
497 (loc (sb!di:frame-code-location frame))
498 (results (list (sb!di:debug-function-name d-fun))))
500 (dolist (ele (sb!di:debug-function-lambda-list d-fun))
501 (lambda-list-element-dispatch ele
502 :required ((push (frame-call-arg ele loc frame) results))
503 :optional ((push (frame-call-arg (second ele) loc frame) results))
504 :keyword ((push (second ele) results)
505 (push (frame-call-arg (third ele) loc frame) results))
506 :deleted ((push (frame-call-arg ele loc frame) results))
507 :rest ((lambda-var-dispatch (second ele) loc
511 (append (reverse (sb!di:debug-var-value
515 (push (make-unprintable-object
516 "unavailable &REST argument")
518 (sb!di:lambda-list-unavailable
520 (push (make-unprintable-object "lambda list unavailable") results)))
521 (pprint-logical-block (*standard-output* nil)
522 (let ((x (nreverse (mapcar #'ensure-printable-object results))))
523 (format t "(~@<~S~{ ~_~S~}~:>)" (first x) (rest x))))
524 (when (sb!di:debug-function-kind d-fun)
526 (prin1 (sb!di:debug-function-kind d-fun))
529 (defun ensure-printable-object (object)
531 (with-open-stream (out (make-broadcast-stream))
535 (declare (ignore cond))
536 (make-unprintable-object "error printing object"))))
538 (defun frame-call-arg (var location frame)
539 (lambda-var-dispatch var location
540 (make-unprintable-object "unused argument")
541 (sb!di:debug-var-value var frame)
542 (make-unprintable-object "unavailable argument")))
544 ;;; Prints a representation of the function call causing FRAME to
545 ;;; exist. VERBOSITY indicates the level of information to output;
546 ;;; zero indicates just printing the debug-function's name, and one
547 ;;; indicates displaying call-like, one-liner format with argument
549 (defun print-frame-call (frame &key (verbosity 1) (number nil))
553 (format t "~&~S: " (sb!di:frame-number frame)))
554 (format t "~S" frame))
557 (format t "~&~S: " (sb!di:frame-number frame)))
558 (print-frame-call-1 frame)))
559 (when (>= verbosity 2)
560 (let ((loc (sb!di:frame-code-location frame)))
563 (sb!di:code-location-debug-block loc)
564 (format t "~%source: ")
565 (print-code-location-source-form loc 0))
566 (sb!di:debug-condition (ignore) ignore)
567 (error (c) (format t "error finding source: ~A" c))))))
571 (defvar *debugger-hook* nil
573 "This is either NIL or a function of two arguments, a condition and the value
574 of *DEBUGGER-HOOK*. This function can either handle the condition or return
575 which causes the standard debugger to execute. The system passes the value
576 of this variable to the function because it binds *DEBUGGER-HOOK* to NIL
577 around the invocation.")
579 ;;; These are bound on each invocation of INVOKE-DEBUGGER.
580 (defvar *debug-restarts*)
581 (defvar *debug-condition*)
583 ;;; Print *DEBUG-CONDITION*, taking care to avoid recursive invocation
584 ;;; of the debugger in case of a problem (e.g. a bug in the PRINT-OBJECT
585 ;;; method for *DEBUG-CONDITION*).
586 (defun princ-debug-condition-carefully (stream)
587 (handler-case (princ *debug-condition* stream)
590 " (caught ~S when trying to print ~S)"
592 '*debug-condition*)))
595 (defun invoke-debugger (condition)
597 "Enter the debugger."
598 (let ((old-hook *debugger-hook*))
600 (let ((*debugger-hook* nil))
601 (funcall old-hook condition old-hook))))
602 (sb!unix:unix-sigsetmask 0)
604 ;; Elsewhere in the system, we use the SANE-PACKAGE function for
605 ;; this, but here causing an exception just as we're trying to handle
606 ;; an exception would be confusing, so instead we use a special hack.
607 (unless (and (packagep *package*)
608 (package-name *package*))
609 (setf *package* (find-package :cl-user))
610 (format *error-output*
611 "The value of ~S was not an undeleted PACKAGE. It has been
613 '*package* *package*))
614 (let (;; Save *PACKAGE* to protect it from WITH-STANDARD-IO-SYNTAX.
615 (original-package *package*))
616 (with-standard-io-syntax
617 (let* ((*debug-condition* condition)
618 (*debug-restarts* (compute-restarts condition))
619 ;; We want the i/o subsystem to be in a known, useful
620 ;; state, regardless of where the debugger was invoked in
621 ;; the program. WITH-STANDARD-IO-SYNTAX does some of that,
623 ;; 1. It doesn't affect our internal special variables
624 ;; like *CURRENT-LEVEL*.
625 ;; 2. It isn't customizable.
626 ;; 3. It doesn't set *PRINT-READABLY* or *PRINT-PRETTY*
627 ;; to the same value as the toplevel default.
628 ;; 4. It sets *PACKAGE* to COMMON-LISP-USER, which is not
629 ;; helpful behavior for a debugger.
630 ;; We try to remedy all these problems with explicit
632 (sb!kernel:*current-level* 0)
633 (*print-length* *debug-print-length*)
634 (*print-level* *debug-print-level*)
635 (*readtable* *debug-readtable*)
636 (*print-readably* nil)
638 (*package* original-package))
640 ;; The initial output here goes to *ERROR-OUTPUT*, because the
641 ;; initial output is not interactive, just an error message,
642 ;; and when people redirect *ERROR-OUTPUT*, they could
643 ;; reasonably expect to see error messages logged there,
644 ;; regardless of what the debugger does afterwards.
645 #!+sb-show (sb!conditions::show-condition *debug-condition*
647 (format *error-output*
648 "~2&debugger invoked on condition of type ~S:~% "
649 (type-of *debug-condition*))
650 (princ-debug-condition-carefully *error-output*)
651 (terpri *error-output*)
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 to send output to the program's
664 ;; ordinary (possibly redirected-to-a-file)
665 ;; *STANDARD-OUTPUT*, or using PEEK-CHAR or some such
666 ;; thing on the program's ordinary (possibly also
667 ;; 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 "~%~@<entering the debugger because of ~S~:@_~:@_~
678 Within the debugger, you can type HELP for help. At ~
679 any command prompt (inside the debugger or not) you can ~
680 type (SB-EXT:QUIT) to terminate the SBCL executable.~:@>~2%"
682 (show-restarts *debug-restarts* *debug-io*)
684 (internal-debug))))))
686 (defun show-restarts (restarts s)
688 (format s "~&restarts:~%")
692 (dolist (restart restarts)
693 (let ((name (restart-name restart)))
695 (let ((len (length (princ-to-string name))))
696 (when (> len max-name-len)
697 (setf max-name-len len))))))
698 (unless (zerop max-name-len)
699 (incf max-name-len 3))
700 (dolist (restart restarts)
701 (let ((name (restart-name restart)))
702 (cond ((member name names-used)
703 (format s "~& ~2D: ~@VT~A~%" count max-name-len restart))
705 (format s "~& ~2D: [~VA] ~A~%"
706 count (- max-name-len 3) name restart)
707 (push name names-used))))
710 ;;; This calls DEBUG-LOOP, performing some simple initializations
711 ;;; before doing so. INVOKE-DEBUGGER calls this to actually get into
712 ;;; the debugger. SB!CONDITIONS::ERROR-ERROR calls this in emergencies
713 ;;; to get into a debug prompt as quickly as possible with as little
714 ;;; risk as possible for stepping on whatever is causing recursive
716 (defun internal-debug ()
717 (let ((*in-the-debugger* t)
718 (*read-suppress* nil))
719 (unless (typep *debug-condition* 'step-condition)
720 (clear-input *debug-io*))
722 #!+mp (sb!mp:without-scheduling (debug-loop))))
726 ;;; Note: This defaulted to T in CMU CL. The changed default in SBCL
727 ;;; was motivated by desire to play nicely with ILISP.
728 (defvar *flush-debug-errors* nil
730 "When set, avoid calling INVOKE-DEBUGGER recursively when errors occur while
731 executing in the debugger.")
734 (let* ((*debug-command-level* (1+ *debug-command-level*))
735 (*real-stack-top* (sb!di:top-frame))
736 (*stack-top* (or *stack-top-hint* *real-stack-top*))
737 (*stack-top-hint* nil)
738 (*current-frame* *stack-top*))
739 (handler-bind ((sb!di:debug-condition (lambda (condition)
740 (princ condition *debug-io*)
741 (throw 'debug-loop-catcher nil))))
743 (print-frame-call *current-frame* :verbosity 2)
745 (catch 'debug-loop-catcher
746 (handler-bind ((error #'(lambda (condition)
747 (when *flush-debug-errors*
748 (clear-input *debug-io*)
750 ;; FIXME: Doing input on *DEBUG-IO*
751 ;; and output on T seems broken.
753 "~&error flushed (because ~
755 '*flush-debug-errors*)
756 (throw 'debug-loop-catcher nil)))))
757 ;; We have to bind level for the restart function created by
758 ;; WITH-SIMPLE-RESTART.
759 (let ((level *debug-command-level*)
760 (restart-commands (make-restart-commands)))
761 (with-simple-restart (abort
762 "Reduce debugger level (to debug level ~D)."
764 (debug-prompt *debug-io*)
765 (force-output *debug-io*)
766 (let ((input (sb!int:get-stream-command *debug-io*)))
768 (let ((cmd-fun (debug-command-p
769 (sb!int:stream-command-name input)
773 (error "unknown stream-command: ~S" input))
775 (error "ambiguous debugger command: ~S" cmd-fun))
778 (sb!int:stream-command-args input))))))
781 (cmd-fun (debug-command-p exp
784 (debug-eval-print exp))
787 "~&Your command, ~S, is ambiguous:~%"
789 (dolist (ele cmd-fun)
790 (format t " ~A~%" ele)))
792 (funcall cmd-fun)))))))))))))))
794 (defvar *auto-eval-in-frame* t
796 "When set (the default), evaluations in the debugger's command loop occur
797 relative to the current frame's environment without the need of debugger
798 forms that explicitly control this kind of evaluation.")
800 ;;; FIXME: We could probably use INTERACTIVE-EVAL for much of this logic.
801 (defun debug-eval-print (exp)
802 (setq +++ ++ ++ + + - - exp)
803 (let* ((values (multiple-value-list
804 (if (and (fboundp 'compile) *auto-eval-in-frame*)
805 (sb!di:eval-in-frame *current-frame* -)
807 (*standard-output* *debug-io*))
809 (if values (prin1 (car values)))
810 (dolist (x (cdr values))
813 (setq /// // // / / values)
814 (setq *** ** ** * * (car values))
815 ;; Make sure that nobody passes back an unbound marker.
819 ;; FIXME: The way INTERACTIVE-EVAL does this seems better.
820 (princ "Setting * to NIL (was unbound marker)."))))
822 ;;;; debug loop functions
824 ;;; These commands are functions, not really commands, so that users can get
825 ;;; their hands on the values returned.
827 (eval-when (:execute :compile-toplevel)
829 (sb!xc:defmacro define-var-operation (ref-or-set &optional value-var)
830 `(let* ((temp (etypecase name
831 (symbol (sb!di:debug-function-symbol-variables
832 (sb!di:frame-debug-function *current-frame*)
834 (simple-string (sb!di:ambiguous-debug-vars
835 (sb!di:frame-debug-function *current-frame*)
837 (location (sb!di:frame-code-location *current-frame*))
838 ;; Let's only deal with valid variables.
839 (vars (remove-if-not #'(lambda (v)
840 (eq (sb!di:debug-var-validity v location)
843 (declare (list vars))
845 (error "No known valid variables match ~S." name))
849 '(sb!di:debug-var-value (car vars) *current-frame*))
851 `(setf (sb!di:debug-var-value (car vars) *current-frame*)
854 ;; Since we have more than one, first see whether we have any
855 ;; variables that exactly match the specification.
856 (let* ((name (etypecase name
857 (symbol (symbol-name name))
858 (simple-string name)))
859 ;; FIXME: REMOVE-IF-NOT is deprecated, use STRING/=
861 (exact (remove-if-not (lambda (v)
862 (string= (sb!di:debug-var-symbol-name v)
865 (vars (or exact vars)))
866 (declare (simple-string name)
869 ;; Check now for only having one variable.
873 '(sb!di:debug-var-value (car vars) *current-frame*))
875 `(setf (sb!di:debug-var-value (car vars) *current-frame*)
877 ;; If there weren't any exact matches, flame about
878 ;; ambiguity unless all the variables have the same
883 (string= (sb!di:debug-var-symbol-name v)
884 (sb!di:debug-var-symbol-name (car vars))))
886 (error "specification ambiguous:~%~{ ~A~%~}"
887 (mapcar #'sb!di:debug-var-symbol-name
890 :key #'sb!di:debug-var-symbol-name))))
891 ;; All names are the same, so see whether the user
892 ;; ID'ed one of them.
894 (let ((v (find id vars :key #'sb!di:debug-var-id)))
897 "invalid variable ID, ~D: should have been one of ~S"
899 (mapcar #'sb!di:debug-var-id vars)))
902 '(sb!di:debug-var-value v *current-frame*))
904 `(setf (sb!di:debug-var-value v *current-frame*)
907 (error "Specify variable ID to disambiguate ~S. Use one of ~S."
909 (mapcar #'sb!di:debug-var-id vars)))))))))
913 (defun var (name &optional (id 0 id-supplied))
915 "Returns a variable's value if possible. Name is a simple-string or symbol.
916 If it is a simple-string, it is an initial substring of the variable's name.
917 If name is a symbol, it has the same name and package as the variable whose
918 value this function returns. If the symbol is uninterned, then the variable
919 has the same name as the symbol, but it has no package.
921 If name is the initial substring of variables with different names, then
922 this return no values after displaying the ambiguous names. If name
923 determines multiple variables with the same name, then you must use the
924 optional id argument to specify which one you want. If you left id
925 unspecified, then this returns no values after displaying the distinguishing
928 The result of this function is limited to the availability of variable
929 information. This is SETF'able."
930 (define-var-operation :ref))
931 (defun (setf var) (value name &optional (id 0 id-supplied))
932 (define-var-operation :set value))
934 ;;; This returns the COUNT'th arg as the user sees it from args, the
935 ;;; result of SB!DI:DEBUG-FUNCTION-LAMBDA-LIST. If this returns a
936 ;;; potential DEBUG-VAR from the lambda-list, then the second value is
937 ;;; T. If this returns a keyword symbol or a value from a rest arg,
938 ;;; then the second value is NIL.
939 (declaim (ftype (function (index list)) nth-arg))
940 (defun nth-arg (count args)
942 (dolist (ele args (error "The argument specification ~S is out of range."
944 (lambda-list-element-dispatch ele
945 :required ((if (zerop n) (return (values ele t))))
946 :optional ((if (zerop n) (return (values (second ele) t))))
947 :keyword ((cond ((zerop n)
948 (return (values (second ele) nil)))
950 (return (values (third ele) t)))))
951 :deleted ((if (zerop n) (return (values ele t))))
952 :rest ((let ((var (second ele)))
953 (lambda-var-dispatch var (sb!di:frame-code-location
955 (error "unused &REST argument before n'th argument")
957 (sb!di:debug-var-value var *current-frame*)
959 "The argument specification ~S is out of range."
962 (return-from nth-arg (values value nil))
964 (error "invalid &REST argument before n'th argument")))))
969 "Returns the N'th argument's value if possible. Argument zero is the first
970 argument in a frame's default printed representation. Count keyword/value
971 pairs as separate arguments."
972 (multiple-value-bind (var lambda-var-p)
973 (nth-arg n (handler-case (sb!di:debug-function-lambda-list
974 (sb!di:frame-debug-function *current-frame*))
975 (sb!di:lambda-list-unavailable ()
976 (error "No argument values are available."))))
978 (lambda-var-dispatch var (sb!di:frame-code-location *current-frame*)
979 (error "Unused arguments have no values.")
980 (sb!di:debug-var-value var *current-frame*)
981 (error "invalid argument value"))
984 ;;;; machinery for definition of debug loop commands
986 (defvar *debug-commands* nil)
988 ;;; Interface to *DEBUG-COMMANDS*. No required arguments in args are
991 ;;; FIXME: This is not needed in the target Lisp system.
992 (defmacro def-debug-command (name args &rest body)
993 (let ((fun-name (intern (concatenate 'simple-string name "-DEBUG-COMMAND"))))
995 (setf *debug-commands*
996 (remove ,name *debug-commands* :key #'car :test #'string=))
997 (defun ,fun-name ,args
998 (unless *in-the-debugger*
999 (error "invoking debugger command while outside the debugger"))
1001 (push (cons ,name #',fun-name) *debug-commands*)
1004 (defun def-debug-command-alias (new-name existing-name)
1005 (let ((pair (assoc existing-name *debug-commands* :test #'string=)))
1006 (unless pair (error "unknown debug command name: ~S" existing-name))
1007 (push (cons new-name (cdr pair)) *debug-commands*))
1010 ;;; This takes a symbol and uses its name to find a debugger command, using
1011 ;;; initial substring matching. It returns the command function if form
1012 ;;; identifies only one command, but if form is ambiguous, this returns a list
1013 ;;; of the command names. If there are no matches, this returns nil. Whenever
1014 ;;; the loop that looks for a set of possibilities encounters an exact name
1015 ;;; match, we return that command function immediately.
1016 (defun debug-command-p (form &optional other-commands)
1017 (if (or (symbolp form) (integerp form))
1021 (format nil "~D" form)))
1024 (declare (simple-string name)
1028 ;; Find matching commands, punting if exact match.
1029 (flet ((match-command (ele)
1030 (let* ((str (car ele))
1031 (str-len (length str)))
1032 (declare (simple-string str)
1034 (cond ((< str-len len))
1036 (when (string= name str :end1 len :end2 len)
1037 (return-from debug-command-p (cdr ele))))
1038 ((string= name str :end1 len :end2 len)
1040 (mapc #'match-command *debug-commands*)
1041 (mapc #'match-command other-commands))
1043 ;; Return the right value.
1044 (cond ((not res) nil)
1047 (t ; Just return the names.
1048 (do ((cmds res (cdr cmds)))
1050 (setf (car cmds) (caar cmds))))))))
1052 ;;; Returns a list of debug commands (in the same format as *debug-commands*)
1053 ;;; that invoke each active restart.
1055 ;;; Two commands are made for each restart: one for the number, and one for
1056 ;;; the restart name (unless it's been shadowed by an earlier restart of the
1058 (defun make-restart-commands (&optional (restarts *debug-restarts*))
1060 (num 0)) ; better be the same as show-restarts!
1061 (dolist (restart restarts)
1062 (let ((name (string (restart-name restart))))
1063 (unless (find name commands :key #'car :test #'string=)
1066 (invoke-restart-interactively restart))))
1067 (push (cons name restart-fun) commands)
1068 (push (cons (format nil "~D" num) restart-fun) commands))))
1072 ;;;; frame-changing commands
1074 (def-debug-command "UP" ()
1075 (let ((next (sb!di:frame-up *current-frame*)))
1077 (setf *current-frame* next)
1078 (print-frame-call next))
1080 (format t "~&Top of stack.")))))
1082 (def-debug-command "DOWN" ()
1083 (let ((next (sb!di:frame-down *current-frame*)))
1085 (setf *current-frame* next)
1086 (print-frame-call next))
1088 (format t "~&Bottom of stack.")))))
1090 (def-debug-command-alias "D" "DOWN")
1092 ;;; CMU CL had this command, but SBCL doesn't, since
1093 ;;; it's redundant with "FRAME 0", and it interferes with abbreviations
1094 ;;; for the TOPLEVEL restart.
1095 ;;;(def-debug-command "TOP" ()
1096 ;;; (do ((prev *current-frame* lead)
1097 ;;; (lead (sb!di:frame-up *current-frame*) (sb!di:frame-up lead)))
1099 ;;; (setf *current-frame* prev)
1100 ;;; (print-frame-call prev))))
1102 (def-debug-command "BOTTOM" ()
1103 (do ((prev *current-frame* lead)
1104 (lead (sb!di:frame-down *current-frame*) (sb!di:frame-down lead)))
1106 (setf *current-frame* prev)
1107 (print-frame-call prev))))
1109 (def-debug-command-alias "B" "BOTTOM")
1111 (def-debug-command "FRAME" (&optional
1112 (n (read-prompting-maybe "frame number: ")))
1113 (setf *current-frame*
1114 (multiple-value-bind (next-frame-fun limit-string)
1115 (if (< n (sb!di:frame-number *current-frame*))
1116 (values #'sb!di:frame-up "top")
1117 (values #'sb!di:frame-down "bottom"))
1118 (do ((frame *current-frame*))
1119 ((= n (sb!di:frame-number frame))
1121 (let ((next-frame (funcall next-frame-fun frame)))
1123 (setf frame next-frame))
1126 "The ~A of the stack was encountered.~%"
1128 (return frame)))))))
1129 (print-frame-call *current-frame*))
1131 (def-debug-command-alias "F" "FRAME")
1133 ;;;; commands for entering and leaving the debugger
1135 ;;; CMU CL supported this QUIT debug command, but SBCL provides this
1136 ;;; functionality with a restart instead. (The QUIT debug command was
1137 ;;; removed because it's confusing to have "quit" mean two different
1138 ;;; things in the system, "restart the top level REPL" in the debugger
1139 ;;; and "terminate the Lisp system" as the SB-EXT:QUIT function.)
1141 ;;;(def-debug-command "QUIT" ()
1142 ;;; (throw 'sb!impl::top-level-catcher nil))
1144 ;;; CMU CL supported this GO debug command, but SBCL doesn't -- just
1145 ;;; type the CONTINUE restart name.
1146 ;;;(def-debug-command "GO" ()
1147 ;;; (continue *debug-condition*)
1148 ;;; (error "There is no restart named CONTINUE."))
1150 (def-debug-command "RESTART" ()
1151 (let ((num (read-if-available :prompt)))
1152 (when (eq num :prompt)
1153 (show-restarts *debug-restarts* *debug-io*)
1154 (write-string "restart: ")
1156 (setf num (read *standard-input*)))
1157 (let ((restart (typecase num
1159 (nth num *debug-restarts*))
1161 (find num *debug-restarts* :key #'restart-name
1162 :test #'(lambda (sym1 sym2)
1163 (string= (symbol-name sym1)
1164 (symbol-name sym2)))))
1166 (format t "~S is invalid as a restart name.~%" num)
1167 (return-from restart-debug-command nil)))))
1169 (invoke-restart-interactively restart)
1170 ;; FIXME: Even if this isn't handled by WARN, it probably
1171 ;; shouldn't go to *STANDARD-OUTPUT*, but *ERROR-OUTPUT* or
1172 ;; *QUERY-IO* or something. Look through this file to
1173 ;; straighten out stream usage.
1174 (princ "There is no such restart.")))))
1176 ;;;; information commands
1178 (def-debug-command "HELP" ()
1179 ;; CMU CL had a little toy pager here, but "if you aren't running
1180 ;; ILISP (or a smart windowing system, or something) you deserve to
1181 ;; lose", so we've dropped it in SBCL. However, in case some
1182 ;; desperate holdout is running this on a dumb terminal somewhere,
1183 ;; we tell him where to find the message stored as a string.
1185 "~&~a~2%(The HELP string is stored in ~S.)~%"
1187 '*debug-help-string*))
1189 (def-debug-command-alias "?" "HELP")
1191 (def-debug-command "ERROR" ()
1192 (format *debug-io* "~A~%" *debug-condition*)
1193 (show-restarts *debug-restarts* *debug-io*))
1195 (def-debug-command "BACKTRACE" ()
1196 (backtrace (read-if-available most-positive-fixnum)))
1198 (def-debug-command "PRINT" ()
1199 (print-frame-call *current-frame*))
1201 (def-debug-command-alias "P" "PRINT")
1203 (def-debug-command "LIST-LOCALS" ()
1204 (let ((d-fun (sb!di:frame-debug-function *current-frame*)))
1205 (if (sb!di:debug-var-info-available d-fun)
1206 (let ((*standard-output* *debug-io*)
1207 (location (sb!di:frame-code-location *current-frame*))
1208 (prefix (read-if-available nil))
1211 (dolist (v (sb!di:ambiguous-debug-vars
1213 (if prefix (string prefix) "")))
1215 (when (eq (sb!di:debug-var-validity v location) :valid)
1216 (setf any-valid-p t)
1217 (format t "~S~:[#~D~;~*~] = ~S~%"
1218 (sb!di:debug-var-symbol v)
1219 (zerop (sb!di:debug-var-id v))
1220 (sb!di:debug-var-id v)
1221 (sb!di:debug-var-value v *current-frame*))))
1225 (format t "There are no local variables ~@[starting with ~A ~]~
1229 (format t "All variables ~@[starting with ~A ~]currently ~
1230 have invalid values."
1232 (write-line "There is no variable information available."))))
1234 (def-debug-command-alias "L" "LIST-LOCALS")
1236 (def-debug-command "SOURCE" ()
1238 (print-code-location-source-form (sb!di:frame-code-location *current-frame*)
1239 (read-if-available 0)))
1241 ;;;; source location printing
1243 ;;; We cache a stream to the last valid file debug source so that we
1244 ;;; won't have to repeatedly open the file.
1246 ;;; KLUDGE: This sounds like a bug, not a feature. Opening files is fast
1247 ;;; in the 1990s, so the benefit is negligible, less important than the
1248 ;;; potential of extra confusion if someone changes the source during
1249 ;;; a debug session and the change doesn't show up. And removing this
1250 ;;; would simplify the system, which I like. -- WHN 19990903
1251 (defvar *cached-debug-source* nil)
1252 (declaim (type (or sb!di:debug-source null) *cached-debug-source*))
1253 (defvar *cached-source-stream* nil)
1254 (declaim (type (or stream null) *cached-source-stream*))
1256 ;;; To suppress the read-time evaluation #. macro during source read,
1257 ;;; *READTABLE* is modified. *READTABLE* is cached to avoid
1258 ;;; copying it each time, and invalidated when the
1259 ;;; *CACHED-DEBUG-SOURCE* has changed.
1260 (defvar *cached-readtable* nil)
1261 (declaim (type (or readtable null) *cached-readtable*))
1263 (pushnew #'(lambda ()
1264 (setq *cached-debug-source* nil *cached-source-stream* nil
1265 *cached-readtable* nil))
1266 sb!int:*before-save-initializations*)
1268 ;;; We also cache the last top-level form that we printed a source for
1269 ;;; so that we don't have to do repeated reads and calls to
1270 ;;; FORM-NUMBER-TRANSLATIONS.
1271 (defvar *cached-top-level-form-offset* nil)
1272 (declaim (type (or index null) *cached-top-level-form-offset*))
1273 (defvar *cached-top-level-form*)
1274 (defvar *cached-form-number-translations*)
1276 ;;; Given a code location, return the associated form-number
1277 ;;; translations and the actual top-level form. We check our cache ---
1278 ;;; if there is a miss, we dispatch on the kind of the debug source.
1279 (defun get-top-level-form (location)
1280 (let ((d-source (sb!di:code-location-debug-source location)))
1281 (if (and (eq d-source *cached-debug-source*)
1282 (eql (sb!di:code-location-top-level-form-offset location)
1283 *cached-top-level-form-offset*))
1284 (values *cached-form-number-translations* *cached-top-level-form*)
1285 (let* ((offset (sb!di:code-location-top-level-form-offset location))
1287 (ecase (sb!di:debug-source-from d-source)
1288 (:file (get-file-top-level-form location))
1289 (:lisp (svref (sb!di:debug-source-name d-source) offset)))))
1290 (setq *cached-top-level-form-offset* offset)
1291 (values (setq *cached-form-number-translations*
1292 (sb!di:form-number-translations res offset))
1293 (setq *cached-top-level-form* res))))))
1295 ;;; Locate the source file (if it still exists) and grab the top-level
1296 ;;; form. If the file is modified, we use the top-level-form offset
1297 ;;; instead of the recorded character offset.
1298 (defun get-file-top-level-form (location)
1299 (let* ((d-source (sb!di:code-location-debug-source location))
1300 (tlf-offset (sb!di:code-location-top-level-form-offset location))
1301 (local-tlf-offset (- tlf-offset
1302 (sb!di:debug-source-root-number d-source)))
1304 (aref (or (sb!di:debug-source-start-positions d-source)
1305 (error "no start positions map"))
1307 (name (sb!di:debug-source-name d-source)))
1308 (unless (eq d-source *cached-debug-source*)
1309 (unless (and *cached-source-stream*
1310 (equal (pathname *cached-source-stream*)
1312 (setq *cached-readtable* nil)
1313 (when *cached-source-stream* (close *cached-source-stream*))
1314 (setq *cached-source-stream* (open name :if-does-not-exist nil))
1315 (unless *cached-source-stream*
1316 (error "The source file no longer exists:~% ~A" (namestring name)))
1317 (format t "~%; file: ~A~%" (namestring name)))
1319 (setq *cached-debug-source*
1320 (if (= (sb!di:debug-source-created d-source)
1321 (file-write-date name))
1325 ((eq *cached-debug-source* d-source)
1326 (file-position *cached-source-stream* char-offset))
1328 (format t "~%; File has been modified since compilation:~%; ~A~@
1329 ; Using form offset instead of character position.~%"
1331 (file-position *cached-source-stream* 0)
1332 (let ((*read-suppress* t))
1333 (dotimes (i local-tlf-offset)
1334 (read *cached-source-stream*)))))
1335 (unless *cached-readtable*
1336 (setq *cached-readtable* (copy-readtable))
1337 (set-dispatch-macro-character
1339 #'(lambda (stream sub-char &rest rest)
1340 (declare (ignore rest sub-char))
1341 (let ((token (read stream t nil t)))
1342 (format nil "#.~S" token)))
1343 *cached-readtable*))
1344 (let ((*readtable* *cached-readtable*))
1345 (read *cached-source-stream*))))
1347 (defun print-code-location-source-form (location context)
1348 (let* ((location (maybe-block-start-location location))
1349 (form-num (sb!di:code-location-form-number location)))
1350 (multiple-value-bind (translations form) (get-top-level-form location)
1351 (unless (< form-num (length translations))
1352 (error "The source path no longer exists."))
1353 (prin1 (sb!di:source-path-context form
1354 (svref translations form-num)
1357 ;;; breakpoint and step commands
1359 ;;; Step to the next code-location.
1360 (def-debug-command "STEP" ()
1361 (setf *number-of-steps* (read-if-available 1))
1362 (set-step-breakpoint *current-frame*)
1363 (continue *debug-condition*)
1364 (error "couldn't continue"))
1366 ;;; List possible breakpoint locations, which ones are active, and
1367 ;;; where the CONTINUE restart will transfer control. Set
1368 ;;; *POSSIBLE-BREAKPOINTS* to the code-locations which can then be
1369 ;;; used by sbreakpoint.
1370 (def-debug-command "LIST-LOCATIONS" ()
1371 (let ((df (read-if-available *default-breakpoint-debug-function*)))
1373 (setf df (sb!di:function-debug-function (eval df)))
1374 (setf *default-breakpoint-debug-function* df))
1376 (not *default-breakpoint-debug-function*))
1377 (setf df (sb!di:frame-debug-function *current-frame*))
1378 (setf *default-breakpoint-debug-function* df)))
1379 (setf *possible-breakpoints* (possible-breakpoints df)))
1380 (let ((continue-at (sb!di:frame-code-location *current-frame*)))
1381 (let ((active (location-in-list *default-breakpoint-debug-function*
1382 *breakpoints* :function-start))
1383 (here (sb!di:code-location=
1384 (sb!di:debug-function-start-location
1385 *default-breakpoint-debug-function*) continue-at)))
1386 (when (or active here)
1387 (format t "::FUNCTION-START ")
1388 (when active (format t " *Active*"))
1389 (when here (format t " *Continue here*"))))
1391 (let ((prev-location nil)
1396 (let ((this-num (1- this-num)))
1397 (if (= prev-num this-num)
1398 (format t "~&~D: " prev-num)
1399 (format t "~&~D-~D: " prev-num this-num)))
1400 (print-code-location-source-form prev-location 0)
1401 (when *print-location-kind*
1402 (format t "~S " (sb!di:code-location-kind prev-location)))
1403 (when (location-in-list prev-location *breakpoints*)
1404 (format t " *Active*"))
1405 (when (sb!di:code-location= prev-location continue-at)
1406 (format t " *Continue here*")))))
1408 (dolist (code-location *possible-breakpoints*)
1409 (when (or *print-location-kind*
1410 (location-in-list code-location *breakpoints*)
1411 (sb!di:code-location= code-location continue-at)
1413 (not (eq (sb!di:code-location-debug-source code-location)
1414 (sb!di:code-location-debug-source prev-location)))
1415 (not (eq (sb!di:code-location-top-level-form-offset
1417 (sb!di:code-location-top-level-form-offset
1419 (not (eq (sb!di:code-location-form-number code-location)
1420 (sb!di:code-location-form-number prev-location))))
1422 (setq prev-location code-location prev-num this-num))
1426 (when (location-in-list *default-breakpoint-debug-function*
1429 (format t "~&::FUNCTION-END *Active* "))))
1431 (def-debug-command-alias "LL" "LIST-LOCATIONS")
1433 ;;; Set breakpoint at the given number.
1434 (def-debug-command "BREAKPOINT" ()
1435 (let ((index (read-prompting-maybe "location number, :START, or :END: "))
1439 (print-functions nil)
1442 (place *default-breakpoint-debug-function*))
1443 (flet ((get-command-line ()
1444 (let ((command-line nil)
1447 (let ((next-input (read-if-available unique)))
1448 (when (eq next-input unique) (return))
1449 (push next-input command-line)))
1450 (nreverse command-line)))
1451 (set-vars-from-command-line (command-line)
1452 (do ((arg (pop command-line) (pop command-line)))
1455 (:condition (setf condition (pop command-line)))
1456 (:print (push (pop command-line) print))
1457 (:break (setf break (pop command-line)))
1459 (setf function (eval (pop command-line)))
1460 (setf *default-breakpoint-debug-function*
1461 (sb!di:function-debug-function function))
1462 (setf place *default-breakpoint-debug-function*)
1463 (setf *possible-breakpoints*
1464 (possible-breakpoints
1465 *default-breakpoint-debug-function*))))))
1466 (setup-function-start ()
1467 (let ((code-loc (sb!di:debug-function-start-location place)))
1468 (setf bp (sb!di:make-breakpoint #'main-hook-function
1470 :kind :function-start))
1471 (setf break (sb!di:preprocess-for-eval break code-loc))
1472 (setf condition (sb!di:preprocess-for-eval condition code-loc))
1473 (dolist (form print)
1474 (push (cons (sb!di:preprocess-for-eval form code-loc) form)
1476 (setup-function-end ()
1478 (sb!di:make-breakpoint #'main-hook-function
1480 :kind :function-end))
1482 ;; FIXME: These and any other old (COERCE `(LAMBDA ..) ..)
1483 ;; forms should be converted to shiny new (LAMBDA ..) forms.
1484 ;; (Search the sources for "coerce.*\(lambda".)
1485 (coerce `(lambda (dummy)
1486 (declare (ignore dummy)) ,break)
1488 (setf condition (coerce `(lambda (dummy)
1489 (declare (ignore dummy)) ,condition)
1491 (dolist (form print)
1493 (coerce `(lambda (dummy)
1494 (declare (ignore dummy)) ,form) 'function)
1497 (setup-code-location ()
1498 (setf place (nth index *possible-breakpoints*))
1499 (setf bp (sb!di:make-breakpoint #'main-hook-function
1501 :kind :code-location))
1502 (dolist (form print)
1504 (sb!di:preprocess-for-eval form place)
1507 (setf break (sb!di:preprocess-for-eval break place))
1508 (setf condition (sb!di:preprocess-for-eval condition place))))
1509 (set-vars-from-command-line (get-command-line))
1511 ((or (eq index :start) (eq index :s))
1512 (setup-function-start))
1513 ((or (eq index :end) (eq index :e))
1514 (setup-function-end))
1516 (setup-code-location)))
1517 (sb!di:activate-breakpoint bp)
1518 (let* ((new-bp-info (create-breakpoint-info place bp index
1520 :print print-functions
1521 :condition condition))
1522 (old-bp-info (location-in-list new-bp-info *breakpoints*)))
1524 (sb!di:deactivate-breakpoint (breakpoint-info-breakpoint
1526 (setf *breakpoints* (remove old-bp-info *breakpoints*))
1527 (format t "previous breakpoint removed~%"))
1528 (push new-bp-info *breakpoints*))
1529 (print-breakpoint-info (first *breakpoints*))
1530 (format t "~&added"))))
1532 (def-debug-command-alias "BP" "BREAKPOINT")
1534 ;;; List all breakpoints which are set.
1535 (def-debug-command "LIST-BREAKPOINTS" ()
1537 (sort *breakpoints* #'< :key #'breakpoint-info-breakpoint-number))
1538 (dolist (info *breakpoints*)
1539 (print-breakpoint-info info)))
1541 (def-debug-command-alias "LB" "LIST-BREAKPOINTS")
1542 (def-debug-command-alias "LBP" "LIST-BREAKPOINTS")
1544 ;;; Remove breakpoint N, or remove all breakpoints if no N given.
1545 (def-debug-command "DELETE-BREAKPOINT" ()
1546 (let* ((index (read-if-available nil))
1548 (find index *breakpoints* :key #'breakpoint-info-breakpoint-number)))
1550 (sb!di:delete-breakpoint (breakpoint-info-breakpoint bp-info))
1551 (setf *breakpoints* (remove bp-info *breakpoints*))
1552 (format t "breakpoint ~S removed~%" index))
1553 (index (format t "The breakpoint doesn't exist."))
1555 (dolist (ele *breakpoints*)
1556 (sb!di:delete-breakpoint (breakpoint-info-breakpoint ele)))
1557 (setf *breakpoints* nil)
1558 (format t "all breakpoints deleted~%")))))
1560 (def-debug-command-alias "DBP" "DELETE-BREAKPOINT")
1562 ;;; miscellaneous commands
1564 (def-debug-command "DESCRIBE" ()
1565 (let* ((curloc (sb!di:frame-code-location *current-frame*))
1566 (debug-fun (sb!di:code-location-debug-function curloc))
1567 (function (sb!di:debug-function-function debug-fun)))
1570 (format t "can't figure out the function for this frame"))))
1572 ;;;; debug loop command utilities
1574 (defun read-prompting-maybe (prompt &optional (in *standard-input*)
1575 (out *standard-output*))
1576 (unless (sb!int:listen-skip-whitespace in)
1581 (defun read-if-available (default &optional (stream *standard-input*))
1582 (if (sb!int:listen-skip-whitespace stream)