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 ;;; If this is bound before the debugger is invoked, it is used as the
41 ;;; stack top by the debugger.
42 (defvar *stack-top-hint* nil)
44 (defvar *stack-top* nil)
45 (defvar *real-stack-top* nil)
47 (defvar *current-frame* nil)
49 ;;; Beginner-oriented help messages are important because you end up
50 ;;; in the debugger whenever something bad happens, or if you try to
51 ;;; get out of the system with Ctrl-C or (EXIT) or EXIT or whatever.
52 ;;; But after memorizing them the wasted screen space gets annoying..
53 (defvar *debug-beginner-help-p* t
54 "Should the debugger display beginner-oriented help messages?")
56 (defun debug-prompt (stream)
58 ;; old behavior, will probably go away in sbcl-0.7.x
59 (format stream "~%~D" (sb!di:frame-number *current-frame*))
60 (dotimes (i *debug-command-level*)
61 (write-char #\] stream))
62 (write-char #\space stream)
64 ;; planned new behavior, delayed since it will break ILISP
68 (sb!di:frame-number *current-frame*)
69 (> *debug-command-level* 1)
70 *debug-command-level*))
72 (defparameter *debug-help-string*
73 "The prompt is right square brackets, the number indicating how many
74 recursive command loops you are in.
75 Any command may be uniquely abbreviated.
76 The debugger rebinds various special variables for controlling i/o, sometimes
77 to defaults (much like WITH-STANDARD-IO-SYNTAX does) and sometimes to
78 its own special values, e.g. SB-DEBUG:*DEBUG-PRINT-LEVEL*.
79 Debug commands do not affect * and friends, but evaluation in the debug loop
80 does affect these variables.
81 SB-DEBUG:*FLUSH-DEBUG-ERRORS* controls whether errors at the debug prompt
82 drop you into deeper into the debugger.
84 Getting in and out of the debugger:
85 RESTART invokes restart numbered as shown (prompt if not given).
86 ERROR prints the error condition and restart cases.
87 The name of any restart, or its number, is a valid command, and is the same
88 as using RESTART to invoke that restart.
91 U up frame D down frame
92 B bottom frame F n frame n (n=0 for top frame)
95 BACKTRACE [n] shows n frames going down the stack.
96 LIST-LOCALS, L lists locals in current function.
97 PRINT, P displays current function call.
98 SOURCE [n] displays frame's source form with n levels of enclosing forms.
100 Breakpoints and steps:
101 LIST-LOCATIONS [{function | :C}] List the locations for breakpoints.
102 Specify :C for the current frame.
104 LIST-BREAKPOINTS List the active breakpoints.
105 Abbreviations: LB, LBP
106 DELETE-BREAKPOINT [n] Remove breakpoint n or all breakpoints.
107 Abbreviations: DEL, DBP
108 BREAKPOINT {n | :end | :start} [:break form] [:function function]
109 [{:print form}*] [:condition form]
111 Abbreviations: BR, BP
112 STEP [n] Step to the next location or step n times.
114 Function and macro commands:
115 (SB-DEBUG:DEBUG-RETURN expression)
116 Exit the debugger, returning expression's values from the current frame.
118 Return the n'th argument in the current frame.
119 (SB-DEBUG:VAR string-or-symbol [id])
120 Returns the value of the specified variable in the current frame.")
122 ;;; This is used to communicate to DEBUG-LOOP that we are at a step breakpoint.
123 (define-condition step-condition (simple-condition) ())
125 ;;;; breakpoint state
127 (defvar *only-block-start-locations* nil
129 "When true, the LIST-LOCATIONS command only displays block start locations.
130 Otherwise, all locations are displayed.")
132 (defvar *print-location-kind* nil
134 "When true, list the code location type in the LIST-LOCATIONS command.")
136 ;;; a list of the types of code-locations that should not be stepped
137 ;;; to and should not be listed when listing breakpoints
138 (defvar *bad-code-location-types* '(:call-site :internal-error))
139 (declaim (type list *bad-code-location-types*))
141 ;;; code locations of the possible breakpoints
142 (defvar *possible-breakpoints*)
143 (declaim (type list *possible-breakpoints*))
145 ;;; a list of the made and active breakpoints, each is a
146 ;;; BREAKPOINT-INFO structure
147 (defvar *breakpoints* nil)
148 (declaim (type list *breakpoints*))
150 ;;; a list of BREAKPOINT-INFO structures of the made and active step
152 (defvar *step-breakpoints* nil)
153 (declaim (type list *step-breakpoints*))
155 ;;; the number of times left to step
156 (defvar *number-of-steps* 1)
157 (declaim (type integer *number-of-steps*))
159 ;;; This is used when listing and setting breakpoints.
160 (defvar *default-breakpoint-debug-fun* nil)
161 (declaim (type (or list sb!di:debug-fun) *default-breakpoint-debug-fun*))
163 ;;;; code location utilities
165 ;;; Return the first code-location in the passed debug block.
166 (defun first-code-location (debug-block)
168 (first-code-location nil))
169 (sb!di:do-debug-block-locations (code-location debug-block)
171 (setf first-code-location code-location)
173 first-code-location))
175 ;;; Return a list of the next code-locations following the one passed.
176 ;;; One of the *BAD-CODE-LOCATION-TYPES* will not be returned.
177 (defun next-code-locations (code-location)
178 (let ((debug-block (sb!di:code-location-debug-block code-location))
179 (block-code-locations nil))
180 (sb!di:do-debug-block-locations (block-code-location debug-block)
181 (unless (member (sb!di:code-location-kind block-code-location)
182 *bad-code-location-types*)
183 (push block-code-location block-code-locations)))
184 (setf block-code-locations (nreverse block-code-locations))
185 (let* ((code-loc-list (rest (member code-location block-code-locations
186 :test #'sb!di:code-location=)))
187 (next-list (cond (code-loc-list
188 (list (first code-loc-list)))
189 ((map 'list #'first-code-location
190 (sb!di:debug-block-successors debug-block)))
192 (when (and (= (length next-list) 1)
193 (sb!di:code-location= (first next-list) code-location))
194 (setf next-list (next-code-locations (first next-list))))
197 ;;; Return a list of code-locations of the possible breakpoints of DEBUG-FUN.
198 (defun possible-breakpoints (debug-fun)
199 (let ((possible-breakpoints nil))
200 (sb!di:do-debug-fun-blocks (debug-block debug-fun)
201 (unless (sb!di:debug-block-elsewhere-p debug-block)
202 (if *only-block-start-locations*
203 (push (first-code-location debug-block) possible-breakpoints)
204 (sb!di:do-debug-block-locations (code-location debug-block)
205 (when (not (member (sb!di:code-location-kind code-location)
206 *bad-code-location-types*))
207 (push code-location possible-breakpoints))))))
208 (nreverse possible-breakpoints)))
210 ;;; Search the info-list for the item passed (CODE-LOCATION,
211 ;;; DEBUG-FUN, or BREAKPOINT-INFO). If the item passed is a debug
212 ;;; function then kind will be compared if it was specified. The kind
213 ;;; if also compared if a breakpoint-info is passed since it's in the
214 ;;; breakpoint. The info structure is returned if found.
215 (defun location-in-list (place info-list &optional (kind nil))
216 (when (breakpoint-info-p place)
217 (setf kind (sb!di:breakpoint-kind (breakpoint-info-breakpoint place)))
218 (setf place (breakpoint-info-place place)))
219 (cond ((sb!di:code-location-p place)
220 (find place info-list
221 :key #'breakpoint-info-place
222 :test #'(lambda (x y) (and (sb!di:code-location-p y)
223 (sb!di:code-location= x y)))))
225 (find place info-list
226 :test #'(lambda (x-debug-fun y-info)
227 (let ((y-place (breakpoint-info-place y-info))
228 (y-breakpoint (breakpoint-info-breakpoint
230 (and (sb!di:debug-fun-p y-place)
231 (eq x-debug-fun y-place)
233 (eq kind (sb!di:breakpoint-kind
234 y-breakpoint))))))))))
236 ;;; If LOC is an unknown location, then try to find the block start
237 ;;; location. Used by source printing to some information instead of
238 ;;; none for the user.
239 (defun maybe-block-start-location (loc)
240 (if (sb!di:code-location-unknown-p loc)
241 (let* ((block (sb!di:code-location-debug-block loc))
242 (start (sb!di:do-debug-block-locations (loc block)
244 (cond ((and (not (sb!di:debug-block-elsewhere-p block))
246 ;; FIXME: Why output on T instead of *DEBUG-FOO* or something?
247 (format t "~%unknown location: using block start~%")
253 ;;;; the BREAKPOINT-INFO structure
255 ;;; info about a made breakpoint
256 (defstruct (breakpoint-info (:copier nil))
257 ;; where we are going to stop
258 (place (missing-arg) :type (or sb!di:code-location sb!di:debug-fun))
259 ;; the breakpoint returned by sb!di:make-breakpoint
260 (breakpoint (missing-arg) :type sb!di:breakpoint)
261 ;; the function returned from SB!DI:PREPROCESS-FOR-EVAL. If result is
262 ;; non-NIL, drop into the debugger.
263 (break #'identity :type function)
264 ;; the function returned from sb!di:preprocess-for-eval. If result is
265 ;; non-NIL, eval (each) print and print results.
266 (condition #'identity :type function)
267 ;; the list of functions from sb!di:preprocess-for-eval to evaluate.
268 ;; Results are conditionally printed. Car of each element is the
269 ;; function, cdr is the form it goes with.
270 (print nil :type list)
271 ;; the number used when listing the possible breakpoints within a
272 ;; function. Could also be a symbol such as start or end.
273 (code-location-number (missing-arg) :type (or symbol integer))
274 ;; the number used when listing the breakpoints active and to delete
276 (breakpoint-number (missing-arg) :type integer))
278 ;;; Return a new BREAKPOINT-INFO structure with the info passed.
279 (defun create-breakpoint-info (place breakpoint code-location-number
280 &key (break #'identity)
281 (condition #'identity) (print nil))
283 (sort *breakpoints* #'< :key #'breakpoint-info-breakpoint-number))
284 (let ((breakpoint-number
285 (do ((i 1 (incf i)) (breakpoints *breakpoints* (rest breakpoints)))
286 ((or (> i (length *breakpoints*))
287 (not (= i (breakpoint-info-breakpoint-number
288 (first breakpoints)))))
291 (make-breakpoint-info :place place :breakpoint breakpoint
292 :code-location-number code-location-number
293 :breakpoint-number breakpoint-number
294 :break break :condition condition :print print)))
296 ;;; Print the breakpoint info for the breakpoint-info structure passed.
297 (defun print-breakpoint-info (breakpoint-info)
298 (let ((place (breakpoint-info-place breakpoint-info))
299 (bp-number (breakpoint-info-breakpoint-number breakpoint-info))
300 (loc-number (breakpoint-info-code-location-number breakpoint-info)))
301 (case (sb!di:breakpoint-kind (breakpoint-info-breakpoint breakpoint-info))
303 (print-code-location-source-form place 0)
308 (sb!di:debug-fun-name (sb!di:code-location-debug-fun
311 (format t "~&~S: FUN-START in ~S" bp-number
312 (sb!di:debug-fun-name place)))
314 (format t "~&~S: FUN-END in ~S" bp-number
315 (sb!di:debug-fun-name place))))))
317 ;;;; MAIN-HOOK-FUNCTION for steps and breakpoints
319 ;;; This must be passed as the hook function. It keeps track of where
320 ;;; STEP breakpoints are.
321 (defun main-hook-function (current-frame breakpoint &optional return-vals
323 (setf *default-breakpoint-debug-fun*
324 (sb!di:frame-debug-fun current-frame))
325 (dolist (step-info *step-breakpoints*)
326 (sb!di:delete-breakpoint (breakpoint-info-breakpoint step-info))
327 (let ((bp-info (location-in-list step-info *breakpoints*)))
329 (sb!di:activate-breakpoint (breakpoint-info-breakpoint bp-info)))))
330 (let ((*stack-top-hint* current-frame)
332 (location-in-list (sb!di:breakpoint-what breakpoint)
334 (sb!di:breakpoint-kind breakpoint)))
336 (location-in-list (sb!di:breakpoint-what breakpoint)
338 (sb!di:breakpoint-kind breakpoint)))
342 (setf *step-breakpoints* nil)
343 (labels ((build-string (str)
344 (setf string (concatenate 'string string str)))
345 (print-common-info ()
347 (with-output-to-string (*standard-output*)
349 (format t "~%Return values: ~S" return-vals))
351 (when (breakpoint-info-print bp-hit-info)
353 (print-frame-call current-frame))
354 (dolist (print (breakpoint-info-print bp-hit-info))
355 (format t "~& ~S = ~S" (rest print)
356 (funcall (first print) current-frame))))))))
358 (setf break (funcall (breakpoint-info-break bp-hit-info)
360 (setf condition (funcall (breakpoint-info-condition bp-hit-info)
362 (cond ((and bp-hit-info step-hit-info (= 1 *number-of-steps*))
363 (build-string (format nil "~&*Step (to a breakpoint)*"))
366 ((and bp-hit-info step-hit-info break)
367 (build-string (format nil "~&*Step (to a breakpoint)*"))
370 ((and bp-hit-info step-hit-info)
372 (format t "~A" string)
373 (decf *number-of-steps*)
374 (set-step-breakpoint current-frame))
375 ((and step-hit-info (= 1 *number-of-steps*))
376 (build-string "*Step*")
377 (break (make-condition 'step-condition :format-control string)))
379 (decf *number-of-steps*)
380 (set-step-breakpoint current-frame))
383 (build-string (format nil "~&*Breakpoint hit*")))
387 (format t "~A" string)))
389 (break "error in main-hook-function: unknown breakpoint"))))))
391 ;;; Set breakpoints at the next possible code-locations. After calling
392 ;;; this, either (CONTINUE) if in the debugger or just let program flow
393 ;;; return if in a hook function.
394 (defun set-step-breakpoint (frame)
396 ((sb!di:debug-block-elsewhere-p (sb!di:code-location-debug-block
397 (sb!di:frame-code-location frame)))
398 ;; FIXME: FORMAT T is used for error output here and elsewhere in
400 (format t "cannot step, in elsewhere code~%"))
402 (let* ((code-location (sb!di:frame-code-location frame))
403 (next-code-locations (next-code-locations code-location)))
406 (dolist (code-location next-code-locations)
407 (let ((bp-info (location-in-list code-location *breakpoints*)))
409 (sb!di:deactivate-breakpoint (breakpoint-info-breakpoint
411 (let ((bp (sb!di:make-breakpoint #'main-hook-function code-location
412 :kind :code-location)))
413 (sb!di:activate-breakpoint bp)
414 (push (create-breakpoint-info code-location bp 0)
415 *step-breakpoints*))))
417 (let* ((debug-fun (sb!di:frame-debug-fun *current-frame*))
418 (bp (sb!di:make-breakpoint #'main-hook-function debug-fun
420 (sb!di:activate-breakpoint bp)
421 (push (create-breakpoint-info debug-fun bp 0)
422 *step-breakpoints*))))))))
426 ;;; ANSI specifies that this macro shall exist, even if only as a
427 ;;; trivial placeholder like this.
428 (defmacro step (form)
429 "a trivial placeholder implementation of the CL:STEP macro required by
436 (defun backtrace (&optional (count most-positive-fixnum)
437 (*standard-output* *debug-io*))
439 "Show a listing of the call stack going down from the current frame. In the
440 debugger, the current frame is indicated by the prompt. COUNT is how many
442 (fresh-line *standard-output*)
443 (do ((frame (if *in-the-debugger* *current-frame* (sb!di:top-frame))
444 (sb!di:frame-down frame))
445 (count count (1- count)))
446 ((or (null frame) (zerop count)))
447 (print-frame-call frame :number t))
448 (fresh-line *standard-output*)
453 (eval-when (:compile-toplevel :execute)
455 ;;; This is a convenient way to express what to do for each type of
456 ;;; lambda-list element.
457 (sb!xc:defmacro lambda-list-element-dispatch (element
468 (ecase (car ,element)
469 (:optional ,@optional)
471 (:keyword ,@keyword)))
473 (aver (eq ,element :deleted))
476 (sb!xc:defmacro lambda-var-dispatch (variable location deleted valid other)
477 (let ((var (gensym)))
478 `(let ((,var ,variable))
479 (cond ((eq ,var :deleted) ,deleted)
480 ((eq (sb!di:debug-var-validity ,var ,location) :valid)
486 ;;; This is used in constructing arg lists for debugger printing when
487 ;;; the arg list is unavailable, some arg is unavailable or unused,
489 (defstruct (unprintable-object
490 (:constructor make-unprintable-object (string))
491 (:print-object (lambda (x s)
492 (print-unreadable-object (x s :type t)
493 (write-string (unprintable-object-string x)
498 ;;; Print FRAME with verbosity level 1. If we hit a &REST arg, then
499 ;;; print as many of the values as possible, punting the loop over
500 ;;; lambda-list variables since any other arguments will be in the
501 ;;; &REST arg's list of values.
502 (defun print-frame-call-1 (frame)
503 (let* ((d-fun (sb!di:frame-debug-fun frame))
504 (loc (sb!di:frame-code-location frame))
505 (results (list (sb!di:debug-fun-name d-fun))))
507 (dolist (ele (sb!di:debug-fun-lambda-list d-fun))
508 (lambda-list-element-dispatch ele
509 :required ((push (frame-call-arg ele loc frame) results))
510 :optional ((push (frame-call-arg (second ele) loc frame) results))
511 :keyword ((push (second ele) results)
512 (push (frame-call-arg (third ele) loc frame) results))
513 :deleted ((push (frame-call-arg ele loc frame) results))
514 :rest ((lambda-var-dispatch (second ele) loc
518 (append (reverse (sb!di:debug-var-value
522 (push (make-unprintable-object
523 "unavailable &REST argument")
525 (sb!di:lambda-list-unavailable
527 (push (make-unprintable-object "lambda list unavailable") results)))
528 (pprint-logical-block (*standard-output* nil)
529 (let ((x (nreverse (mapcar #'ensure-printable-object results))))
530 (format t "(~@<~S~{ ~_~S~}~:>)" (first x) (rest x))))
531 (when (sb!di:debug-fun-kind d-fun)
533 (prin1 (sb!di:debug-fun-kind d-fun))
536 (defun ensure-printable-object (object)
538 (with-open-stream (out (make-broadcast-stream))
542 (declare (ignore cond))
543 (make-unprintable-object "error printing object"))))
545 (defun frame-call-arg (var location frame)
546 (lambda-var-dispatch var location
547 (make-unprintable-object "unused argument")
548 (sb!di:debug-var-value var frame)
549 (make-unprintable-object "unavailable argument")))
551 ;;; Prints a representation of the function call causing FRAME to
552 ;;; exist. VERBOSITY indicates the level of information to output;
553 ;;; zero indicates just printing the DEBUG-FUN's name, and one
554 ;;; indicates displaying call-like, one-liner format with argument
556 (defun print-frame-call (frame &key (verbosity 1) (number nil))
560 (format t "~&~S: " (sb!di:frame-number frame)))
561 (format t "~S" frame))
564 (format t "~&~S: " (sb!di:frame-number frame)))
565 (print-frame-call-1 frame)))
566 (when (>= verbosity 2)
567 (let ((loc (sb!di:frame-code-location frame)))
570 (sb!di:code-location-debug-block loc)
571 (format t "~%source: ")
572 (print-code-location-source-form loc 0))
573 (sb!di:debug-condition (ignore) ignore)
574 (error (c) (format t "error finding source: ~A" c))))))
578 (defvar *debugger-hook* nil
580 "This is either NIL or a function of two arguments, a condition and the value
581 of *DEBUGGER-HOOK*. This function can either handle the condition or return
582 which causes the standard debugger to execute. The system passes the value
583 of this variable to the function because it binds *DEBUGGER-HOOK* to NIL
584 around the invocation.")
586 ;;; These are bound on each invocation of INVOKE-DEBUGGER.
587 (defvar *debug-restarts*)
588 (defvar *debug-condition*)
590 (defun invoke-debugger (condition)
592 "Enter the debugger."
593 (let ((old-hook *debugger-hook*))
595 (let ((*debugger-hook* nil))
596 (funcall old-hook condition old-hook))))
597 (sb!unix:unix-sigsetmask 0)
599 ;; Elsewhere in the system, we use the SANE-PACKAGE function for
600 ;; this, but here causing an exception just as we're trying to handle
601 ;; an exception would be confusing, so instead we use a special hack.
602 (unless (and (packagep *package*)
603 (package-name *package*))
604 (setf *package* (find-package :cl-user))
605 (format *error-output*
606 "The value of ~S was not an undeleted PACKAGE. It has been
608 '*package* *package*))
609 (let (;; Save *PACKAGE* to protect it from WITH-STANDARD-IO-SYNTAX.
610 (original-package *package*))
611 (with-standard-io-syntax
612 (let* ((*debug-condition* condition)
613 (*debug-restarts* (compute-restarts condition))
614 ;; We want the i/o subsystem to be in a known, useful
615 ;; state, regardless of where the debugger was invoked in
616 ;; the program. WITH-STANDARD-IO-SYNTAX does some of that,
618 ;; 1. It doesn't affect our internal special variables
619 ;; like *CURRENT-LEVEL*.
620 ;; 2. It isn't customizable.
621 ;; 3. It doesn't set *PRINT-READABLY* or *PRINT-PRETTY*
622 ;; to the same value as the toplevel default.
623 ;; 4. It sets *PACKAGE* to COMMON-LISP-USER, which is not
624 ;; helpful behavior for a debugger.
625 ;; We try to remedy all these problems with explicit
627 (sb!kernel:*current-level* 0)
628 (*print-length* *debug-print-length*)
629 (*print-level* *debug-print-level*)
630 (*readtable* *debug-readtable*)
631 (*print-readably* nil)
633 (*package* original-package))
635 ;; Before we start our own output, finish any pending output.
636 ;; Otherwise, if the user tried to track the progress of
637 ;; his program using PRINT statements, he'd tend to lose
638 ;; the last line of output or so, and get confused.
639 (flush-standard-output-streams)
641 ;; (The initial output here goes to *ERROR-OUTPUT*, because the
642 ;; initial output is not interactive, just an error message,
643 ;; and when people redirect *ERROR-OUTPUT*, they could
644 ;; reasonably expect to see error messages logged there,
645 ;; regardless of what the debugger does afterwards.)
647 (format *error-output*
648 "~2&~@<debugger invoked on condition of type ~S: ~
650 (type-of *debug-condition*)
653 (format *error-output*
654 "~&(caught ~S trying to print ~S when entering debugger)~%"
656 '*debug-condition*)))
658 ;; After the initial error/condition/whatever announcement to
659 ;; *ERROR-OUTPUT*, we become interactive, and should talk on
660 ;; *DEBUG-IO* from now on. (KLUDGE: This is a normative
661 ;; statement, not a description of reality.:-| There's a lot of
662 ;; older debugger code which was written to do i/o on whatever
663 ;; stream was in fashion at the time, and not all of it has
664 ;; been converted to behave this way. -- WHN 2000-11-16)
665 (let (;; FIXME: The first two bindings here seem wrong,
666 ;; violating the principle of least surprise, and making
667 ;; it impossible for the user to do reasonable things
668 ;; like using PRINT at the debugger prompt to send output
669 ;; to the program's ordinary (possibly
670 ;; redirected-to-a-file) *STANDARD-OUTPUT*, or using
671 ;; PEEK-CHAR or some such thing on the program's ordinary
672 ;; (possibly also redirected) *STANDARD-INPUT*.
673 (*standard-input* *debug-io*)
674 (*standard-output* *debug-io*)
675 ;; This seems reasonable: e.g. if the user has redirected
676 ;; *ERROR-OUTPUT* to some log file, it's probably wrong
677 ;; to send errors which occur in interactive debugging to
678 ;; that file, and right to send them to *DEBUG-IO*.
679 (*error-output* *debug-io*))
680 (unless (typep condition 'step-condition)
681 (when *debug-beginner-help-p*
683 "~%~@<Within the debugger, you can type HELP for help. ~
684 At any command prompt (within the debugger or not) you ~
685 can type (SB-EXT:QUIT) to terminate the SBCL ~
686 executable. The condition which caused the debugger to ~
687 be entered is bound to ~S. You can suppress this ~
688 message by clearing ~S.~:@>~2%"
690 '*debug-beginner-help-p*))
691 (show-restarts *debug-restarts* *debug-io*))
692 (internal-debug))))))
694 (defun show-restarts (restarts s)
696 (format s "~&restarts:~%")
700 (dolist (restart restarts)
701 (let ((name (restart-name restart)))
703 (let ((len (length (princ-to-string name))))
704 (when (> len max-name-len)
705 (setf max-name-len len))))))
706 (unless (zerop max-name-len)
707 (incf max-name-len 3))
708 (dolist (restart restarts)
709 (let ((name (restart-name restart)))
710 (cond ((member name names-used)
711 (format s "~& ~2D: ~@VT~A~%" count max-name-len restart))
713 (format s "~& ~2D: [~VA] ~A~%"
714 count (- max-name-len 3) name restart)
715 (push name names-used))))
718 ;;; This calls DEBUG-LOOP, performing some simple initializations
719 ;;; before doing so. INVOKE-DEBUGGER calls this to actually get into
720 ;;; the debugger. SB!KERNEL::ERROR-ERROR calls this in emergencies
721 ;;; to get into a debug prompt as quickly as possible with as little
722 ;;; risk as possible for stepping on whatever is causing recursive
724 (defun internal-debug ()
725 (let ((*in-the-debugger* t)
726 (*read-suppress* nil))
727 (unless (typep *debug-condition* 'step-condition)
728 (clear-input *debug-io*))
730 #!+mp (sb!mp:without-scheduling (debug-loop))))
734 ;;; Note: This defaulted to T in CMU CL. The changed default in SBCL
735 ;;; was motivated by desire to play nicely with ILISP.
736 (defvar *flush-debug-errors* nil
738 "When set, avoid calling INVOKE-DEBUGGER recursively when errors occur while
739 executing in the debugger.")
742 (let* ((*debug-command-level* (1+ *debug-command-level*))
743 (*real-stack-top* (sb!di:top-frame))
744 (*stack-top* (or *stack-top-hint* *real-stack-top*))
745 (*stack-top-hint* nil)
746 (*current-frame* *stack-top*))
747 (handler-bind ((sb!di:debug-condition (lambda (condition)
748 (princ condition *debug-io*)
749 (throw 'debug-loop-catcher nil))))
751 (print-frame-call *current-frame* :verbosity 2)
753 (catch 'debug-loop-catcher
754 (handler-bind ((error #'(lambda (condition)
755 (when *flush-debug-errors*
756 (clear-input *debug-io*)
758 ;; FIXME: Doing input on *DEBUG-IO*
759 ;; and output on T seems broken.
761 "~&error flushed (because ~
763 '*flush-debug-errors*)
764 (throw 'debug-loop-catcher nil)))))
765 ;; We have to bind level for the restart function created by
766 ;; WITH-SIMPLE-RESTART.
767 (let ((level *debug-command-level*)
768 (restart-commands (make-restart-commands)))
769 (with-simple-restart (abort
770 "Reduce debugger level (to debug level ~D)."
772 (debug-prompt *debug-io*)
773 (force-output *debug-io*)
774 (let ((input (sb!int:get-stream-command *debug-io*)))
776 (let ((cmd-fun (debug-command-p
777 (sb!int:stream-command-name input)
781 (error "unknown stream-command: ~S" input))
783 (error "ambiguous debugger command: ~S" cmd-fun))
786 (sb!int:stream-command-args input))))))
789 (cmd-fun (debug-command-p exp
792 (debug-eval-print exp))
795 "~&Your command, ~S, is ambiguous:~%"
797 (dolist (ele cmd-fun)
798 (format t " ~A~%" ele)))
800 (funcall cmd-fun)))))))))))))))
802 ;;; FIXME: We could probably use INTERACTIVE-EVAL for much of this logic.
803 (defun debug-eval-print (expr)
804 (/noshow "entering DEBUG-EVAL-PRINT" expr)
805 (/noshow (fboundp 'compile))
806 (setq +++ ++ ++ + + - - expr)
807 (let* ((values (multiple-value-list (eval -)))
808 (*standard-output* *debug-io*))
809 (/noshow "done with EVAL in DEBUG-EVAL-PRINT")
811 (if values (prin1 (car values)))
812 (dolist (x (cdr values))
815 (setq /// // // / / values)
816 (setq *** ** ** * * (car values))
817 ;; Make sure that nobody passes back an unbound marker.
821 ;; FIXME: The way INTERACTIVE-EVAL does this seems better.
822 (princ "Setting * to NIL (was unbound marker)."))))
824 ;;;; debug loop functions
826 ;;; These commands are functions, not really commands, so that users
827 ;;; can get their hands on the values returned.
829 (eval-when (:execute :compile-toplevel)
831 (sb!xc:defmacro define-var-operation (ref-or-set &optional value-var)
832 `(let* ((temp (etypecase name
833 (symbol (sb!di:debug-fun-symbol-variables
834 (sb!di:frame-debug-fun *current-frame*)
836 (simple-string (sb!di:ambiguous-debug-vars
837 (sb!di:frame-debug-fun *current-frame*)
839 (location (sb!di:frame-code-location *current-frame*))
840 ;; Let's only deal with valid variables.
841 (vars (remove-if-not #'(lambda (v)
842 (eq (sb!di:debug-var-validity v location)
845 (declare (list vars))
847 (error "No known valid variables match ~S." name))
851 '(sb!di:debug-var-value (car vars) *current-frame*))
853 `(setf (sb!di:debug-var-value (car vars) *current-frame*)
856 ;; Since we have more than one, first see whether we have
857 ;; any variables that exactly match the specification.
858 (let* ((name (etypecase name
859 (symbol (symbol-name name))
860 (simple-string name)))
861 ;; FIXME: REMOVE-IF-NOT is deprecated, use STRING/=
863 (exact (remove-if-not (lambda (v)
864 (string= (sb!di:debug-var-symbol-name v)
867 (vars (or exact vars)))
868 (declare (simple-string name)
871 ;; Check now for only having one variable.
875 '(sb!di:debug-var-value (car vars) *current-frame*))
877 `(setf (sb!di:debug-var-value (car vars) *current-frame*)
879 ;; If there weren't any exact matches, flame about
880 ;; ambiguity unless all the variables have the same
885 (string= (sb!di:debug-var-symbol-name v)
886 (sb!di:debug-var-symbol-name (car vars))))
888 (error "specification ambiguous:~%~{ ~A~%~}"
889 (mapcar #'sb!di:debug-var-symbol-name
892 :key #'sb!di:debug-var-symbol-name))))
893 ;; All names are the same, so see whether the user
894 ;; ID'ed one of them.
896 (let ((v (find id vars :key #'sb!di:debug-var-id)))
899 "invalid variable ID, ~D: should have been one of ~S"
901 (mapcar #'sb!di:debug-var-id vars)))
904 '(sb!di:debug-var-value v *current-frame*))
906 `(setf (sb!di:debug-var-value v *current-frame*)
909 (error "Specify variable ID to disambiguate ~S. Use one of ~S."
911 (mapcar #'sb!di:debug-var-id vars)))))))))
915 ;;; FIXME: This doesn't work. It would be real nice we could make it
916 ;;; work! Alas, it doesn't seem to work in CMU CL X86 either..
917 (defun var (name &optional (id 0 id-supplied))
919 "Return a variable's value if possible. NAME is a simple-string or symbol.
920 If it is a simple-string, it is an initial substring of the variable's name.
921 If name is a symbol, it has the same name and package as the variable whose
922 value this function returns. If the symbol is uninterned, then the variable
923 has the same name as the symbol, but it has no package.
925 If name is the initial substring of variables with different names, then
926 this return no values after displaying the ambiguous names. If name
927 determines multiple variables with the same name, then you must use the
928 optional id argument to specify which one you want. If you left id
929 unspecified, then this returns no values after displaying the distinguishing
932 The result of this function is limited to the availability of variable
933 information. This is SETF'able."
934 (define-var-operation :ref))
935 (defun (setf var) (value name &optional (id 0 id-supplied))
936 (define-var-operation :set value))
938 ;;; This returns the COUNT'th arg as the user sees it from args, the
939 ;;; result of SB!DI:DEBUG-FUN-LAMBDA-LIST. If this returns a
940 ;;; potential DEBUG-VAR from the lambda-list, then the second value is
941 ;;; T. If this returns a keyword symbol or a value from a rest arg,
942 ;;; then the second value is NIL.
943 (declaim (ftype (function (index list)) nth-arg))
944 (defun nth-arg (count args)
946 (dolist (ele args (error "The argument specification ~S is out of range."
948 (lambda-list-element-dispatch ele
949 :required ((if (zerop n) (return (values ele t))))
950 :optional ((if (zerop n) (return (values (second ele) t))))
951 :keyword ((cond ((zerop n)
952 (return (values (second ele) nil)))
954 (return (values (third ele) t)))))
955 :deleted ((if (zerop n) (return (values ele t))))
956 :rest ((let ((var (second ele)))
957 (lambda-var-dispatch var (sb!di:frame-code-location
959 (error "unused &REST argument before n'th
962 (sb!di:debug-var-value var *current-frame*)
964 "The argument specification ~S is out of range."
967 (return-from nth-arg (values value nil))
969 (error "invalid &REST argument before n'th argument")))))
974 "Return the N'th argument's value if possible. Argument zero is the first
975 argument in a frame's default printed representation. Count keyword/value
976 pairs as separate arguments."
977 (multiple-value-bind (var lambda-var-p)
978 (nth-arg n (handler-case (sb!di:debug-fun-lambda-list
979 (sb!di:frame-debug-fun *current-frame*))
980 (sb!di:lambda-list-unavailable ()
981 (error "No argument values are available."))))
983 (lambda-var-dispatch var (sb!di:frame-code-location *current-frame*)
984 (error "Unused arguments have no values.")
985 (sb!di:debug-var-value var *current-frame*)
986 (error "invalid argument value"))
989 ;;;; machinery for definition of debug loop commands
991 (defvar *debug-commands* nil)
993 ;;; Interface to *DEBUG-COMMANDS*. No required arguments in args are
995 (defmacro !def-debug-command (name args &rest body)
996 (let ((fun-name (symbolicate name "-DEBUG-COMMAND")))
998 (setf *debug-commands*
999 (remove ,name *debug-commands* :key #'car :test #'string=))
1000 (defun ,fun-name ,args
1001 (unless *in-the-debugger*
1002 (error "invoking debugger command while outside the debugger"))
1004 (push (cons ,name #',fun-name) *debug-commands*)
1007 (defun !def-debug-command-alias (new-name existing-name)
1008 (let ((pair (assoc existing-name *debug-commands* :test #'string=)))
1009 (unless pair (error "unknown debug command name: ~S" existing-name))
1010 (push (cons new-name (cdr pair)) *debug-commands*))
1013 ;;; This takes a symbol and uses its name to find a debugger command,
1014 ;;; using initial substring matching. It returns the command function
1015 ;;; if form identifies only one command, but if form is ambiguous,
1016 ;;; this returns a list of the command names. If there are no matches,
1017 ;;; this returns nil. Whenever the loop that looks for a set of
1018 ;;; possibilities encounters an exact name match, we return that
1019 ;;; command function immediately.
1020 (defun debug-command-p (form &optional other-commands)
1021 (if (or (symbolp form) (integerp form))
1025 (format nil "~D" form)))
1028 (declare (simple-string name)
1032 ;; Find matching commands, punting if exact match.
1033 (flet ((match-command (ele)
1034 (let* ((str (car ele))
1035 (str-len (length str)))
1036 (declare (simple-string str)
1038 (cond ((< str-len len))
1040 (when (string= name str :end1 len :end2 len)
1041 (return-from debug-command-p (cdr ele))))
1042 ((string= name str :end1 len :end2 len)
1044 (mapc #'match-command *debug-commands*)
1045 (mapc #'match-command other-commands))
1047 ;; Return the right value.
1048 (cond ((not res) nil)
1051 (t ; Just return the names.
1052 (do ((cmds res (cdr cmds)))
1054 (setf (car cmds) (caar cmds))))))))
1056 ;;; Return a list of debug commands (in the same format as
1057 ;;; *debug-commands*) that invoke each active restart.
1059 ;;; Two commands are made for each restart: one for the number, and
1060 ;;; one for the restart name (unless it's been shadowed by an earlier
1061 ;;; restart of the same name, or it is NIL).
1062 (defun make-restart-commands (&optional (restarts *debug-restarts*))
1064 (num 0)) ; better be the same as show-restarts!
1065 (dolist (restart restarts)
1066 (let ((name (string (restart-name restart))))
1068 #'(lambda () (invoke-restart-interactively restart))))
1069 (push (cons (format nil "~d" num) restart-fun) commands)
1070 (unless (or (null (restart-name restart))
1071 (find name commands :key #'car :test #'string=))
1072 (push (cons name restart-fun) commands))))
1076 ;;;; frame-changing commands
1078 (!def-debug-command "UP" ()
1079 (let ((next (sb!di:frame-up *current-frame*)))
1081 (setf *current-frame* next)
1082 (print-frame-call next))
1084 (format t "~&Top of stack.")))))
1086 (!def-debug-command "DOWN" ()
1087 (let ((next (sb!di:frame-down *current-frame*)))
1089 (setf *current-frame* next)
1090 (print-frame-call next))
1092 (format t "~&Bottom of stack.")))))
1094 (!def-debug-command-alias "D" "DOWN")
1096 ;;; CMU CL had this command, but SBCL doesn't, since it's redundant
1097 ;;; with "FRAME 0", and it interferes with abbreviations for the
1098 ;;; TOPLEVEL restart.
1099 ;;;(!def-debug-command "TOP" ()
1100 ;;; (do ((prev *current-frame* lead)
1101 ;;; (lead (sb!di:frame-up *current-frame*) (sb!di:frame-up lead)))
1103 ;;; (setf *current-frame* prev)
1104 ;;; (print-frame-call prev))))
1106 (!def-debug-command "BOTTOM" ()
1107 (do ((prev *current-frame* lead)
1108 (lead (sb!di:frame-down *current-frame*) (sb!di:frame-down lead)))
1110 (setf *current-frame* prev)
1111 (print-frame-call prev))))
1113 (!def-debug-command-alias "B" "BOTTOM")
1115 (!def-debug-command "FRAME" (&optional
1116 (n (read-prompting-maybe "frame number: ")))
1117 (setf *current-frame*
1118 (multiple-value-bind (next-frame-fun limit-string)
1119 (if (< n (sb!di:frame-number *current-frame*))
1120 (values #'sb!di:frame-up "top")
1121 (values #'sb!di:frame-down "bottom"))
1122 (do ((frame *current-frame*))
1123 ((= n (sb!di:frame-number frame))
1125 (let ((next-frame (funcall next-frame-fun frame)))
1127 (setf frame next-frame))
1130 "The ~A of the stack was encountered.~%"
1132 (return frame)))))))
1133 (print-frame-call *current-frame*))
1135 (!def-debug-command-alias "F" "FRAME")
1137 ;;;; commands for entering and leaving the debugger
1139 ;;; CMU CL supported this QUIT debug command, but SBCL provides this
1140 ;;; functionality with a restart instead. (The QUIT debug command was
1141 ;;; removed because it's confusing to have "quit" mean two different
1142 ;;; things in the system, "restart the top level REPL" in the debugger
1143 ;;; and "terminate the Lisp system" as the SB-EXT:QUIT function.)
1145 ;;;(!def-debug-command "QUIT" ()
1146 ;;; (throw 'sb!impl::top-level-catcher nil))
1148 ;;; CMU CL supported this GO debug command, but SBCL doesn't -- in
1149 ;;; SBCL you just type the CONTINUE restart name instead (or "RESTART
1150 ;;; CONTINUE", that's OK too).
1152 ;;;(!def-debug-command "GO" ()
1153 ;;; (continue *debug-condition*)
1154 ;;; (error "There is no restart named CONTINUE."))
1156 (!def-debug-command "RESTART" ()
1157 (let ((num (read-if-available :prompt)))
1158 (when (eq num :prompt)
1159 (show-restarts *debug-restarts* *debug-io*)
1160 (write-string "restart: ")
1162 (setf num (read *standard-input*)))
1163 (let ((restart (typecase num
1165 (nth num *debug-restarts*))
1167 (find num *debug-restarts* :key #'restart-name
1168 :test #'(lambda (sym1 sym2)
1169 (string= (symbol-name sym1)
1170 (symbol-name sym2)))))
1172 (format t "~S is invalid as a restart name.~%" num)
1173 (return-from restart-debug-command nil)))))
1175 (invoke-restart-interactively restart)
1176 ;; FIXME: Even if this isn't handled by WARN, it probably
1177 ;; shouldn't go to *STANDARD-OUTPUT*, but *ERROR-OUTPUT* or
1178 ;; *QUERY-IO* or something. Look through this file to
1179 ;; straighten out stream usage.
1180 (princ "There is no such restart.")))))
1182 ;;;; information commands
1184 (!def-debug-command "HELP" ()
1185 ;; CMU CL had a little toy pager here, but "if you aren't running
1186 ;; ILISP (or a smart windowing system, or something) you deserve to
1187 ;; lose", so we've dropped it in SBCL. However, in case some
1188 ;; desperate holdout is running this on a dumb terminal somewhere,
1189 ;; we tell him where to find the message stored as a string.
1191 "~&~A~2%(The HELP string is stored in ~S.)~%"
1193 '*debug-help-string*))
1195 (!def-debug-command-alias "?" "HELP")
1197 (!def-debug-command "ERROR" ()
1198 (format *debug-io* "~A~%" *debug-condition*)
1199 (show-restarts *debug-restarts* *debug-io*))
1201 (!def-debug-command "BACKTRACE" ()
1202 (backtrace (read-if-available most-positive-fixnum)))
1204 (!def-debug-command "PRINT" ()
1205 (print-frame-call *current-frame*))
1207 (!def-debug-command-alias "P" "PRINT")
1209 (!def-debug-command "LIST-LOCALS" ()
1210 (let ((d-fun (sb!di:frame-debug-fun *current-frame*)))
1211 (if (sb!di:debug-var-info-available d-fun)
1212 (let ((*standard-output* *debug-io*)
1213 (location (sb!di:frame-code-location *current-frame*))
1214 (prefix (read-if-available nil))
1217 (dolist (v (sb!di:ambiguous-debug-vars
1219 (if prefix (string prefix) "")))
1221 (when (eq (sb!di:debug-var-validity v location) :valid)
1222 (setf any-valid-p t)
1223 (format t "~S~:[#~D~;~*~] = ~S~%"
1224 (sb!di:debug-var-symbol v)
1225 (zerop (sb!di:debug-var-id v))
1226 (sb!di:debug-var-id v)
1227 (sb!di:debug-var-value v *current-frame*))))
1231 (format t "There are no local variables ~@[starting with ~A ~]~
1235 (format t "All variables ~@[starting with ~A ~]currently ~
1236 have invalid values."
1238 (write-line "There is no variable information available."))))
1240 (!def-debug-command-alias "L" "LIST-LOCALS")
1242 (!def-debug-command "SOURCE" ()
1244 (print-code-location-source-form (sb!di:frame-code-location *current-frame*)
1245 (read-if-available 0)))
1247 ;;;; source location printing
1249 ;;; We cache a stream to the last valid file debug source so that we
1250 ;;; won't have to repeatedly open the file.
1252 ;;; KLUDGE: This sounds like a bug, not a feature. Opening files is fast
1253 ;;; in the 1990s, so the benefit is negligible, less important than the
1254 ;;; potential of extra confusion if someone changes the source during
1255 ;;; a debug session and the change doesn't show up. And removing this
1256 ;;; would simplify the system, which I like. -- WHN 19990903
1257 (defvar *cached-debug-source* nil)
1258 (declaim (type (or sb!di:debug-source null) *cached-debug-source*))
1259 (defvar *cached-source-stream* nil)
1260 (declaim (type (or stream null) *cached-source-stream*))
1262 ;;; To suppress the read-time evaluation #. macro during source read,
1263 ;;; *READTABLE* is modified. *READTABLE* is cached to avoid
1264 ;;; copying it each time, and invalidated when the
1265 ;;; *CACHED-DEBUG-SOURCE* has changed.
1266 (defvar *cached-readtable* nil)
1267 (declaim (type (or readtable null) *cached-readtable*))
1270 (setq *cached-debug-source* nil *cached-source-stream* nil
1271 *cached-readtable* nil))
1272 *before-save-initializations*)
1274 ;;; We also cache the last top-level form that we printed a source for
1275 ;;; so that we don't have to do repeated reads and calls to
1276 ;;; FORM-NUMBER-TRANSLATIONS.
1277 (defvar *cached-top-level-form-offset* nil)
1278 (declaim (type (or index null) *cached-top-level-form-offset*))
1279 (defvar *cached-top-level-form*)
1280 (defvar *cached-form-number-translations*)
1282 ;;; Given a code location, return the associated form-number
1283 ;;; translations and the actual top-level form. We check our cache ---
1284 ;;; if there is a miss, we dispatch on the kind of the debug source.
1285 (defun get-top-level-form (location)
1286 (let ((d-source (sb!di:code-location-debug-source location)))
1287 (if (and (eq d-source *cached-debug-source*)
1288 (eql (sb!di:code-location-top-level-form-offset location)
1289 *cached-top-level-form-offset*))
1290 (values *cached-form-number-translations* *cached-top-level-form*)
1291 (let* ((offset (sb!di:code-location-top-level-form-offset location))
1293 (ecase (sb!di:debug-source-from d-source)
1294 (:file (get-file-top-level-form location))
1295 (:lisp (svref (sb!di:debug-source-name d-source) offset)))))
1296 (setq *cached-top-level-form-offset* offset)
1297 (values (setq *cached-form-number-translations*
1298 (sb!di:form-number-translations res offset))
1299 (setq *cached-top-level-form* res))))))
1301 ;;; Locate the source file (if it still exists) and grab the top-level
1302 ;;; form. If the file is modified, we use the top-level-form offset
1303 ;;; instead of the recorded character offset.
1304 (defun get-file-top-level-form (location)
1305 (let* ((d-source (sb!di:code-location-debug-source location))
1306 (tlf-offset (sb!di:code-location-top-level-form-offset location))
1307 (local-tlf-offset (- tlf-offset
1308 (sb!di:debug-source-root-number d-source)))
1310 (aref (or (sb!di:debug-source-start-positions d-source)
1311 (error "no start positions map"))
1313 (name (sb!di:debug-source-name d-source)))
1314 (unless (eq d-source *cached-debug-source*)
1315 (unless (and *cached-source-stream*
1316 (equal (pathname *cached-source-stream*)
1318 (setq *cached-readtable* nil)
1319 (when *cached-source-stream* (close *cached-source-stream*))
1320 (setq *cached-source-stream* (open name :if-does-not-exist nil))
1321 (unless *cached-source-stream*
1322 (error "The source file no longer exists:~% ~A" (namestring name)))
1323 (format t "~%; file: ~A~%" (namestring name)))
1325 (setq *cached-debug-source*
1326 (if (= (sb!di:debug-source-created d-source)
1327 (file-write-date name))
1331 ((eq *cached-debug-source* d-source)
1332 (file-position *cached-source-stream* char-offset))
1334 (format t "~%; File has been modified since compilation:~%; ~A~@
1335 ; Using form offset instead of character position.~%"
1337 (file-position *cached-source-stream* 0)
1338 (let ((*read-suppress* t))
1339 (dotimes (i local-tlf-offset)
1340 (read *cached-source-stream*)))))
1341 (unless *cached-readtable*
1342 (setq *cached-readtable* (copy-readtable))
1343 (set-dispatch-macro-character
1345 #'(lambda (stream sub-char &rest rest)
1346 (declare (ignore rest sub-char))
1347 (let ((token (read stream t nil t)))
1348 (format nil "#.~S" token)))
1349 *cached-readtable*))
1350 (let ((*readtable* *cached-readtable*))
1351 (read *cached-source-stream*))))
1353 (defun print-code-location-source-form (location context)
1354 (let* ((location (maybe-block-start-location location))
1355 (form-num (sb!di:code-location-form-number location)))
1356 (multiple-value-bind (translations form) (get-top-level-form location)
1357 (unless (< form-num (length translations))
1358 (error "The source path no longer exists."))
1359 (prin1 (sb!di:source-path-context form
1360 (svref translations form-num)
1363 ;;; breakpoint and step commands
1365 ;;; Step to the next code-location.
1366 (!def-debug-command "STEP" ()
1367 (setf *number-of-steps* (read-if-available 1))
1368 (set-step-breakpoint *current-frame*)
1369 (continue *debug-condition*)
1370 (error "couldn't continue"))
1372 ;;; List possible breakpoint locations, which ones are active, and
1373 ;;; where the CONTINUE restart will transfer control. Set
1374 ;;; *POSSIBLE-BREAKPOINTS* to the code-locations which can then be
1375 ;;; used by sbreakpoint.
1376 (!def-debug-command "LIST-LOCATIONS" ()
1377 (let ((df (read-if-available *default-breakpoint-debug-fun*)))
1379 (setf df (sb!di:fun-debug-fun (eval df)))
1380 (setf *default-breakpoint-debug-fun* df))
1382 (not *default-breakpoint-debug-fun*))
1383 (setf df (sb!di:frame-debug-fun *current-frame*))
1384 (setf *default-breakpoint-debug-fun* df)))
1385 (setf *possible-breakpoints* (possible-breakpoints df)))
1386 (let ((continue-at (sb!di:frame-code-location *current-frame*)))
1387 (let ((active (location-in-list *default-breakpoint-debug-fun*
1388 *breakpoints* :fun-start))
1389 (here (sb!di:code-location=
1390 (sb!di:debug-fun-start-location
1391 *default-breakpoint-debug-fun*) continue-at)))
1392 (when (or active here)
1393 (format t "::FUN-START ")
1394 (when active (format t " *Active*"))
1395 (when here (format t " *Continue here*"))))
1397 (let ((prev-location nil)
1402 (let ((this-num (1- this-num)))
1403 (if (= prev-num this-num)
1404 (format t "~&~D: " prev-num)
1405 (format t "~&~D-~D: " prev-num this-num)))
1406 (print-code-location-source-form prev-location 0)
1407 (when *print-location-kind*
1408 (format t "~S " (sb!di:code-location-kind prev-location)))
1409 (when (location-in-list prev-location *breakpoints*)
1410 (format t " *Active*"))
1411 (when (sb!di:code-location= prev-location continue-at)
1412 (format t " *Continue here*")))))
1414 (dolist (code-location *possible-breakpoints*)
1415 (when (or *print-location-kind*
1416 (location-in-list code-location *breakpoints*)
1417 (sb!di:code-location= code-location continue-at)
1419 (not (eq (sb!di:code-location-debug-source code-location)
1420 (sb!di:code-location-debug-source prev-location)))
1421 (not (eq (sb!di:code-location-top-level-form-offset
1423 (sb!di:code-location-top-level-form-offset
1425 (not (eq (sb!di:code-location-form-number code-location)
1426 (sb!di:code-location-form-number prev-location))))
1428 (setq prev-location code-location prev-num this-num))
1432 (when (location-in-list *default-breakpoint-debug-fun*
1435 (format t "~&::FUN-END *Active* "))))
1437 (!def-debug-command-alias "LL" "LIST-LOCATIONS")
1439 ;;; Set breakpoint at the given number.
1440 (!def-debug-command "BREAKPOINT" ()
1441 (let ((index (read-prompting-maybe "location number, :START, or :END: "))
1445 (print-functions nil)
1448 (place *default-breakpoint-debug-fun*))
1449 (flet ((get-command-line ()
1450 (let ((command-line nil)
1453 (let ((next-input (read-if-available unique)))
1454 (when (eq next-input unique) (return))
1455 (push next-input command-line)))
1456 (nreverse command-line)))
1457 (set-vars-from-command-line (command-line)
1458 (do ((arg (pop command-line) (pop command-line)))
1461 (:condition (setf condition (pop command-line)))
1462 (:print (push (pop command-line) print))
1463 (:break (setf break (pop command-line)))
1465 (setf function (eval (pop command-line)))
1466 (setf *default-breakpoint-debug-fun*
1467 (sb!di:fun-debug-fun function))
1468 (setf place *default-breakpoint-debug-fun*)
1469 (setf *possible-breakpoints*
1470 (possible-breakpoints
1471 *default-breakpoint-debug-fun*))))))
1473 (let ((code-loc (sb!di:debug-fun-start-location place)))
1474 (setf bp (sb!di:make-breakpoint #'main-hook-function
1477 (setf break (sb!di:preprocess-for-eval break code-loc))
1478 (setf condition (sb!di:preprocess-for-eval condition code-loc))
1479 (dolist (form print)
1480 (push (cons (sb!di:preprocess-for-eval form code-loc) form)
1484 (sb!di:make-breakpoint #'main-hook-function
1488 ;; FIXME: These and any other old (COERCE `(LAMBDA ..) ..)
1489 ;; forms should be converted to shiny new (LAMBDA ..) forms.
1490 ;; (Search the sources for "coerce.*\(lambda".)
1491 (coerce `(lambda (dummy)
1492 (declare (ignore dummy)) ,break)
1494 (setf condition (coerce `(lambda (dummy)
1495 (declare (ignore dummy)) ,condition)
1497 (dolist (form print)
1499 (coerce `(lambda (dummy)
1500 (declare (ignore dummy)) ,form) 'function)
1503 (setup-code-location ()
1504 (setf place (nth index *possible-breakpoints*))
1505 (setf bp (sb!di:make-breakpoint #'main-hook-function
1507 :kind :code-location))
1508 (dolist (form print)
1510 (sb!di:preprocess-for-eval form place)
1513 (setf break (sb!di:preprocess-for-eval break place))
1514 (setf condition (sb!di:preprocess-for-eval condition place))))
1515 (set-vars-from-command-line (get-command-line))
1517 ((or (eq index :start) (eq index :s))
1519 ((or (eq index :end) (eq index :e))
1522 (setup-code-location)))
1523 (sb!di:activate-breakpoint bp)
1524 (let* ((new-bp-info (create-breakpoint-info place bp index
1526 :print print-functions
1527 :condition condition))
1528 (old-bp-info (location-in-list new-bp-info *breakpoints*)))
1530 (sb!di:deactivate-breakpoint (breakpoint-info-breakpoint
1532 (setf *breakpoints* (remove old-bp-info *breakpoints*))
1533 (format t "previous breakpoint removed~%"))
1534 (push new-bp-info *breakpoints*))
1535 (print-breakpoint-info (first *breakpoints*))
1536 (format t "~&added"))))
1538 (!def-debug-command-alias "BP" "BREAKPOINT")
1540 ;;; List all breakpoints which are set.
1541 (!def-debug-command "LIST-BREAKPOINTS" ()
1543 (sort *breakpoints* #'< :key #'breakpoint-info-breakpoint-number))
1544 (dolist (info *breakpoints*)
1545 (print-breakpoint-info info)))
1547 (!def-debug-command-alias "LB" "LIST-BREAKPOINTS")
1548 (!def-debug-command-alias "LBP" "LIST-BREAKPOINTS")
1550 ;;; Remove breakpoint N, or remove all breakpoints if no N given.
1551 (!def-debug-command "DELETE-BREAKPOINT" ()
1552 (let* ((index (read-if-available nil))
1554 (find index *breakpoints* :key #'breakpoint-info-breakpoint-number)))
1556 (sb!di:delete-breakpoint (breakpoint-info-breakpoint bp-info))
1557 (setf *breakpoints* (remove bp-info *breakpoints*))
1558 (format t "breakpoint ~S removed~%" index))
1559 (index (format t "The breakpoint doesn't exist."))
1561 (dolist (ele *breakpoints*)
1562 (sb!di:delete-breakpoint (breakpoint-info-breakpoint ele)))
1563 (setf *breakpoints* nil)
1564 (format t "all breakpoints deleted~%")))))
1566 (!def-debug-command-alias "DBP" "DELETE-BREAKPOINT")
1568 ;;; miscellaneous commands
1570 (!def-debug-command "DESCRIBE" ()
1571 (let* ((curloc (sb!di:frame-code-location *current-frame*))
1572 (debug-fun (sb!di:code-location-debug-fun curloc))
1573 (function (sb!di:debug-fun-fun debug-fun)))
1576 (format t "can't figure out the function for this frame"))))
1578 ;;;; debug loop command utilities
1580 (defun read-prompting-maybe (prompt &optional (in *standard-input*)
1581 (out *standard-output*))
1582 (unless (sb!int:listen-skip-whitespace in)
1587 (defun read-if-available (default &optional (stream *standard-input*))
1588 (if (sb!int:listen-skip-whitespace stream)