0.pre8.28
[sbcl/lichteblau.git] / src / code / debug.lisp
blobbc64c3bf974afa9aa4bfa010164ed34a6c65c197
1 ;;;; the debugger
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!DEBUG")
14 ;;;; variables and constants
16 ;;; things to consider when tweaking these values:
17 ;;; * We're afraid to just default them to NIL and NIL, in case the
18 ;;; user inadvertently causes a hairy data structure to be printed
19 ;;; when he inadvertently enters the debugger.
20 ;;; * We don't want to truncate output too much. These days anyone
21 ;;; can easily run their Lisp in a windowing system or under Emacs,
22 ;;; so it's not the end of the world even if the worst case is a
23 ;;; few thousand lines of output.
24 ;;; * As condition :REPORT methods are converted to use the pretty
25 ;;; printer, they acquire *PRINT-LEVEL* constraints, so e.g. under
26 ;;; sbcl-0.7.1.28's old value of *DEBUG-PRINT-LEVEL*=3, an
27 ;;; ARG-COUNT-ERROR printed as
28 ;;; error while parsing arguments to DESTRUCTURING-BIND:
29 ;;; invalid number of elements in
30 ;;; #
31 ;;; to satisfy lambda list
32 ;;; #:
33 ;;; exactly 2 expected, but 5 found
34 (defvar *debug-print-level* 5
35 #!+sb-doc
36 "*PRINT-LEVEL* for the debugger")
37 (defvar *debug-print-length* 7
38 #!+sb-doc
39 "*PRINT-LENGTH* for the debugger")
41 (defvar *debug-readtable*
42 ;; KLUDGE: This can't be initialized in a cold toplevel form,
43 ;; because the *STANDARD-READTABLE* isn't initialized until after
44 ;; cold toplevel forms have run. So instead we initialize it
45 ;; immediately after *STANDARD-READTABLE*. -- WHN 20000205
46 nil
47 #!+sb-doc
48 "*READTABLE* for the debugger")
50 (defvar *in-the-debugger* nil
51 #!+sb-doc
52 "This is T while in the debugger.")
54 ;;; nestedness inside debugger command loops
55 (defvar *debug-command-level* 0)
57 ;;; If this is bound before the debugger is invoked, it is used as the
58 ;;; stack top by the debugger.
59 (defvar *stack-top-hint* nil)
61 (defvar *stack-top* nil)
62 (defvar *real-stack-top* nil)
64 (defvar *current-frame* nil)
66 ;;; Beginner-oriented help messages are important because you end up
67 ;;; in the debugger whenever something bad happens, or if you try to
68 ;;; get out of the system with Ctrl-C or (EXIT) or EXIT or whatever.
69 ;;; But after memorizing them the wasted screen space gets annoying..
70 (defvar *debug-beginner-help-p* t
71 "Should the debugger display beginner-oriented help messages?")
73 (defun debug-prompt (stream)
74 (sb!thread::get-foreground)
75 (format stream
76 "~%~W~:[~;[~W~]] "
77 (sb!di:frame-number *current-frame*)
78 (> *debug-command-level* 1)
79 *debug-command-level*))
81 (defparameter *debug-help-string*
82 "The debug prompt is square brackets, with number(s) indicating the current
83 control stack level and, if you've entered the debugger recursively, how
84 deeply recursed you are.
85 Any command -- including the name of a restart -- may be uniquely abbreviated.
86 The debugger rebinds various special variables for controlling i/o, sometimes
87 to defaults (much like WITH-STANDARD-IO-SYNTAX does) and sometimes to
88 its own special values, e.g. SB-DEBUG:*DEBUG-PRINT-LEVEL*.
89 Debug commands do not affect *, //, and similar variables, but evaluation in
90 the debug loop does affect these variables.
91 SB-DEBUG:*FLUSH-DEBUG-ERRORS* controls whether errors at the debug prompt
92 drop you deeper into the debugger.
94 Getting in and out of the debugger:
95 RESTART invokes restart numbered as shown (prompt if not given).
96 ERROR prints the error condition and restart cases.
97 The number of any restart, or its name, or a unique abbreviation for its
98 name, is a valid command, and is the same as using RESTART to invoke
99 that restart.
101 Changing frames:
102 U up frame D down frame
103 B bottom frame F n frame n (n=0 for top frame)
105 Inspecting frames:
106 BACKTRACE [n] shows n frames going down the stack.
107 LIST-LOCALS, L lists locals in current function.
108 PRINT, P displays current function call.
109 SOURCE [n] displays frame's source form with n levels of enclosing forms.
111 Breakpoints and steps:
112 LIST-LOCATIONS [{function | :C}] List the locations for breakpoints.
113 Specify :C for the current frame.
114 Abbreviation: LL
115 LIST-BREAKPOINTS List the active breakpoints.
116 Abbreviations: LB, LBP
117 DELETE-BREAKPOINT [n] Remove breakpoint n or all breakpoints.
118 Abbreviations: DEL, DBP
119 BREAKPOINT {n | :end | :start} [:break form] [:function function]
120 [{:print form}*] [:condition form]
121 Set a breakpoint.
122 Abbreviations: BR, BP
123 STEP [n] Step to the next location or step n times.
125 Function and macro commands:
126 (SB-DEBUG:ARG n)
127 Return the n'th argument in the current frame.
128 (SB-DEBUG:VAR string-or-symbol [id])
129 Returns the value of the specified variable in the current frame.
131 Other commands:
132 RETURN expr
133 [EXPERIMENTAL] Return the values resulting from evaluation of expr
134 from the current frame, if this frame was compiled with a sufficiently
135 high DEBUG optimization quality.
136 SLURP
137 Discard all pending input on *STANDARD-INPUT*. (This can be
138 useful when the debugger was invoked to handle an error in
139 deeply nested input syntax, and now the reader is confused.)")
141 ;;; This is used to communicate to DEBUG-LOOP that we are at a step breakpoint.
142 (define-condition step-condition (simple-condition) ())
144 ;;;; breakpoint state
146 (defvar *only-block-start-locations* nil
147 #!+sb-doc
148 "When true, the LIST-LOCATIONS command only displays block start locations.
149 Otherwise, all locations are displayed.")
151 (defvar *print-location-kind* nil
152 #!+sb-doc
153 "When true, list the code location type in the LIST-LOCATIONS command.")
155 ;;; a list of the types of code-locations that should not be stepped
156 ;;; to and should not be listed when listing breakpoints
157 (defvar *bad-code-location-types* '(:call-site :internal-error))
158 (declaim (type list *bad-code-location-types*))
160 ;;; code locations of the possible breakpoints
161 (defvar *possible-breakpoints*)
162 (declaim (type list *possible-breakpoints*))
164 ;;; a list of the made and active breakpoints, each is a
165 ;;; BREAKPOINT-INFO structure
166 (defvar *breakpoints* nil)
167 (declaim (type list *breakpoints*))
169 ;;; a list of BREAKPOINT-INFO structures of the made and active step
170 ;;; breakpoints
171 (defvar *step-breakpoints* nil)
172 (declaim (type list *step-breakpoints*))
174 ;;; the number of times left to step
175 (defvar *number-of-steps* 1)
176 (declaim (type integer *number-of-steps*))
178 ;;; This is used when listing and setting breakpoints.
179 (defvar *default-breakpoint-debug-fun* nil)
180 (declaim (type (or list sb!di:debug-fun) *default-breakpoint-debug-fun*))
182 ;;;; code location utilities
184 ;;; Return the first code-location in the passed debug block.
185 (defun first-code-location (debug-block)
186 (let ((found nil)
187 (first-code-location nil))
188 (sb!di:do-debug-block-locations (code-location debug-block)
189 (unless found
190 (setf first-code-location code-location)
191 (setf found t)))
192 first-code-location))
194 ;;; Return a list of the next code-locations following the one passed.
195 ;;; One of the *BAD-CODE-LOCATION-TYPES* will not be returned.
196 (defun next-code-locations (code-location)
197 (let ((debug-block (sb!di:code-location-debug-block code-location))
198 (block-code-locations nil))
199 (sb!di:do-debug-block-locations (block-code-location debug-block)
200 (unless (member (sb!di:code-location-kind block-code-location)
201 *bad-code-location-types*)
202 (push block-code-location block-code-locations)))
203 (setf block-code-locations (nreverse block-code-locations))
204 (let* ((code-loc-list (rest (member code-location block-code-locations
205 :test #'sb!di:code-location=)))
206 (next-list (cond (code-loc-list
207 (list (first code-loc-list)))
208 ((map 'list #'first-code-location
209 (sb!di:debug-block-successors debug-block)))
210 (t nil))))
211 (when (and (= (length next-list) 1)
212 (sb!di:code-location= (first next-list) code-location))
213 (setf next-list (next-code-locations (first next-list))))
214 next-list)))
216 ;;; Return a list of code-locations of the possible breakpoints of DEBUG-FUN.
217 (defun possible-breakpoints (debug-fun)
218 (let ((possible-breakpoints nil))
219 (sb!di:do-debug-fun-blocks (debug-block debug-fun)
220 (unless (sb!di:debug-block-elsewhere-p debug-block)
221 (if *only-block-start-locations*
222 (push (first-code-location debug-block) possible-breakpoints)
223 (sb!di:do-debug-block-locations (code-location debug-block)
224 (when (not (member (sb!di:code-location-kind code-location)
225 *bad-code-location-types*))
226 (push code-location possible-breakpoints))))))
227 (nreverse possible-breakpoints)))
229 ;;; Search the info-list for the item passed (CODE-LOCATION,
230 ;;; DEBUG-FUN, or BREAKPOINT-INFO). If the item passed is a debug
231 ;;; function then kind will be compared if it was specified. The kind
232 ;;; if also compared if a breakpoint-info is passed since it's in the
233 ;;; breakpoint. The info structure is returned if found.
234 (defun location-in-list (place info-list &optional (kind nil))
235 (when (breakpoint-info-p place)
236 (setf kind (sb!di:breakpoint-kind (breakpoint-info-breakpoint place)))
237 (setf place (breakpoint-info-place place)))
238 (cond ((sb!di:code-location-p place)
239 (find place info-list
240 :key #'breakpoint-info-place
241 :test (lambda (x y) (and (sb!di:code-location-p y)
242 (sb!di:code-location= x y)))))
244 (find place info-list
245 :test (lambda (x-debug-fun y-info)
246 (let ((y-place (breakpoint-info-place y-info))
247 (y-breakpoint (breakpoint-info-breakpoint
248 y-info)))
249 (and (sb!di:debug-fun-p y-place)
250 (eq x-debug-fun y-place)
251 (or (not kind)
252 (eq kind (sb!di:breakpoint-kind
253 y-breakpoint))))))))))
255 ;;; If LOC is an unknown location, then try to find the block start
256 ;;; location. Used by source printing to some information instead of
257 ;;; none for the user.
258 (defun maybe-block-start-location (loc)
259 (if (sb!di:code-location-unknown-p loc)
260 (let* ((block (sb!di:code-location-debug-block loc))
261 (start (sb!di:do-debug-block-locations (loc block)
262 (return loc))))
263 (cond ((and (not (sb!di:debug-block-elsewhere-p block))
264 start)
265 ;; FIXME: Why output on T instead of *DEBUG-FOO* or something?
266 (format t "~%unknown location: using block start~%")
267 start)
269 loc)))
270 loc))
272 ;;;; the BREAKPOINT-INFO structure
274 ;;; info about a made breakpoint
275 (defstruct (breakpoint-info (:copier nil)
276 (:constructor %make-breakpoint-info))
277 ;; where we are going to stop
278 (place (missing-arg)
279 :type (or sb!di:code-location sb!di:debug-fun)
280 :read-only t)
281 ;; the breakpoint returned by SB!DI:MAKE-BREAKPOINT
282 (breakpoint (missing-arg) :type sb!di:breakpoint :read-only t)
283 ;; the function returned from SB!DI:PREPROCESS-FOR-EVAL. If result is
284 ;; non-NIL, drop into the debugger.
285 (break #'identity :type function :read-only t)
286 ;; the function returned from SB!DI:PREPROCESS-FOR-EVAL. If result is
287 ;; non-NIL, eval (each) print and print results.
288 (condition #'identity :type function :read-only t)
289 ;; the list of functions from SB!DI:PREPROCESS-FOR-EVAL to evaluate.
290 ;; Results are conditionally printed. CAR of each element is the
291 ;; function, CDR is the form it goes with.
292 (print nil :type list :read-only t)
293 ;; the number used when listing the possible breakpoints within a
294 ;; function; or could also be a symbol such as START or END
295 (code-location-selector (missing-arg) :type (or symbol integer) :read-only t)
296 ;; the number used when listing the active breakpoints, and when
297 ;; deleting breakpoints
298 (breakpoint-number (missing-arg) :type integer :read-only t))
300 (defun create-breakpoint-info (place breakpoint code-location-selector
301 &key (break #'identity)
302 (condition #'identity) (print nil))
303 (setf *breakpoints*
304 (sort *breakpoints* #'< :key #'breakpoint-info-breakpoint-number))
305 (let ((breakpoint-number
306 (do ((i 1 (incf i)) (breakpoints *breakpoints* (rest breakpoints)))
307 ((or (> i (length *breakpoints*))
308 (not (= i (breakpoint-info-breakpoint-number
309 (first breakpoints)))))
311 i))))
312 (%make-breakpoint-info :place place
313 :breakpoint breakpoint
314 :code-location-selector code-location-selector
315 :breakpoint-number breakpoint-number
316 :break break
317 :condition condition
318 :print print)))
320 (defun print-breakpoint-info (breakpoint-info)
321 (let ((place (breakpoint-info-place breakpoint-info))
322 (bp-number (breakpoint-info-breakpoint-number breakpoint-info)))
323 (case (sb!di:breakpoint-kind (breakpoint-info-breakpoint breakpoint-info))
324 (:code-location
325 (print-code-location-source-form place 0)
326 (format t
327 "~&~S: ~S in ~S"
328 bp-number
329 (breakpoint-info-code-location-selector breakpoint-info)
330 (sb!di:debug-fun-name (sb!di:code-location-debug-fun place))))
331 (:fun-start
332 (format t "~&~S: FUN-START in ~S" bp-number
333 (sb!di:debug-fun-name place)))
334 (:fun-end
335 (format t "~&~S: FUN-END in ~S" bp-number
336 (sb!di:debug-fun-name place))))))
338 ;;;; MAIN-HOOK-FUN for steps and breakpoints
340 ;;; This must be passed as the hook function. It keeps track of where
341 ;;; STEP breakpoints are.
342 (defun main-hook-fun (current-frame breakpoint &optional return-vals
343 fun-end-cookie)
344 (setf *default-breakpoint-debug-fun*
345 (sb!di:frame-debug-fun current-frame))
346 (dolist (step-info *step-breakpoints*)
347 (sb!di:delete-breakpoint (breakpoint-info-breakpoint step-info))
348 (let ((bp-info (location-in-list step-info *breakpoints*)))
349 (when bp-info
350 (sb!di:activate-breakpoint (breakpoint-info-breakpoint bp-info)))))
351 (let ((*stack-top-hint* current-frame)
352 (step-hit-info
353 (location-in-list (sb!di:breakpoint-what breakpoint)
354 *step-breakpoints*
355 (sb!di:breakpoint-kind breakpoint)))
356 (bp-hit-info
357 (location-in-list (sb!di:breakpoint-what breakpoint)
358 *breakpoints*
359 (sb!di:breakpoint-kind breakpoint)))
360 (break)
361 (condition)
362 (string ""))
363 (setf *step-breakpoints* nil)
364 (labels ((build-string (str)
365 (setf string (concatenate 'string string str)))
366 (print-common-info ()
367 (build-string
368 (with-output-to-string (*standard-output*)
369 (when fun-end-cookie
370 (format t "~%Return values: ~S" return-vals))
371 (when condition
372 (when (breakpoint-info-print bp-hit-info)
373 (format t "~%")
374 (print-frame-call current-frame))
375 (dolist (print (breakpoint-info-print bp-hit-info))
376 (format t "~& ~S = ~S" (rest print)
377 (funcall (first print) current-frame))))))))
378 (when bp-hit-info
379 (setf break (funcall (breakpoint-info-break bp-hit-info)
380 current-frame))
381 (setf condition (funcall (breakpoint-info-condition bp-hit-info)
382 current-frame)))
383 (cond ((and bp-hit-info step-hit-info (= 1 *number-of-steps*))
384 (build-string (format nil "~&*Step (to a breakpoint)*"))
385 (print-common-info)
386 (break string))
387 ((and bp-hit-info step-hit-info break)
388 (build-string (format nil "~&*Step (to a breakpoint)*"))
389 (print-common-info)
390 (break string))
391 ((and bp-hit-info step-hit-info)
392 (print-common-info)
393 (format t "~A" string)
394 (decf *number-of-steps*)
395 (set-step-breakpoint current-frame))
396 ((and step-hit-info (= 1 *number-of-steps*))
397 (build-string "*Step*")
398 (break (make-condition 'step-condition :format-control string)))
399 (step-hit-info
400 (decf *number-of-steps*)
401 (set-step-breakpoint current-frame))
402 (bp-hit-info
403 (when break
404 (build-string (format nil "~&*Breakpoint hit*")))
405 (print-common-info)
406 (if break
407 (break string)
408 (format t "~A" string)))
410 (break "unknown breakpoint"))))))
412 ;;; Set breakpoints at the next possible code-locations. After calling
413 ;;; this, either (CONTINUE) if in the debugger or just let program flow
414 ;;; return if in a hook function.
415 (defun set-step-breakpoint (frame)
416 (cond
417 ((sb!di:debug-block-elsewhere-p (sb!di:code-location-debug-block
418 (sb!di:frame-code-location frame)))
419 ;; FIXME: FORMAT T is used for error output here and elsewhere in
420 ;; the debug code.
421 (format t "cannot step, in elsewhere code~%"))
423 (let* ((code-location (sb!di:frame-code-location frame))
424 (next-code-locations (next-code-locations code-location)))
425 (cond
426 (next-code-locations
427 (dolist (code-location next-code-locations)
428 (let ((bp-info (location-in-list code-location *breakpoints*)))
429 (when bp-info
430 (sb!di:deactivate-breakpoint (breakpoint-info-breakpoint
431 bp-info))))
432 (let ((bp (sb!di:make-breakpoint #'main-hook-fun code-location
433 :kind :code-location)))
434 (sb!di:activate-breakpoint bp)
435 (push (create-breakpoint-info code-location bp 0)
436 *step-breakpoints*))))
438 (let* ((debug-fun (sb!di:frame-debug-fun *current-frame*))
439 (bp (sb!di:make-breakpoint #'main-hook-fun debug-fun
440 :kind :fun-end)))
441 (sb!di:activate-breakpoint bp)
442 (push (create-breakpoint-info debug-fun bp 0)
443 *step-breakpoints*))))))))
445 ;;;; STEP
447 ;;; ANSI specifies that this macro shall exist, even if only as a
448 ;;; trivial placeholder like this.
449 (defmacro step (form)
450 "a trivial placeholder implementation of the CL:STEP macro required by
451 the ANSI spec"
452 `(progn
453 ,form))
455 ;;;; BACKTRACE
457 (defun backtrace (&optional (count most-positive-fixnum)
458 (*standard-output* *debug-io*))
459 #!+sb-doc
460 "Show a listing of the call stack going down from the current frame. In the
461 debugger, the current frame is indicated by the prompt. COUNT is how many
462 frames to show."
463 (fresh-line *standard-output*)
464 (do ((frame (if *in-the-debugger* *current-frame* (sb!di:top-frame))
465 (sb!di:frame-down frame))
466 (count count (1- count)))
467 ((or (null frame) (zerop count)))
468 (print-frame-call frame :number t))
469 (fresh-line *standard-output*)
470 (values))
472 (defun backtrace-as-list (&optional (count most-positive-fixnum))
473 #!+sb-doc "Return a list representing the current BACKTRACE."
474 (do ((reversed-result nil)
475 (frame (if *in-the-debugger* *current-frame* (sb!di:top-frame))
476 (sb!di:frame-down frame))
477 (count count (1- count)))
478 ((or (null frame) (zerop count))
479 (nreverse reversed-result))
480 (push (frame-call-as-list frame) reversed-result)))
482 (defun frame-call-as-list (frame)
483 (cons (sb!di:debug-fun-name (sb!di:frame-debug-fun frame))
484 (frame-args-as-list frame)))
486 ;;;; frame printing
488 (eval-when (:compile-toplevel :execute)
490 ;;; This is a convenient way to express what to do for each type of
491 ;;; lambda-list element.
492 (sb!xc:defmacro lambda-list-element-dispatch (element
493 &key
494 required
495 optional
496 rest
497 keyword
498 deleted)
499 `(etypecase ,element
500 (sb!di:debug-var
501 ,@required)
502 (cons
503 (ecase (car ,element)
504 (:optional ,@optional)
505 (:rest ,@rest)
506 (:keyword ,@keyword)))
507 (symbol
508 (aver (eq ,element :deleted))
509 ,@deleted)))
511 (sb!xc:defmacro lambda-var-dispatch (variable location deleted valid other)
512 (let ((var (gensym)))
513 `(let ((,var ,variable))
514 (cond ((eq ,var :deleted) ,deleted)
515 ((eq (sb!di:debug-var-validity ,var ,location) :valid)
516 ,valid)
517 (t ,other)))))
519 ) ; EVAL-WHEN
521 ;;; This is used in constructing arg lists for debugger printing when
522 ;;; the arg list is unavailable, some arg is unavailable or unused, etc.
523 (defstruct (unprintable-object
524 (:constructor make-unprintable-object (string))
525 (:print-object (lambda (x s)
526 (print-unreadable-object (x s)
527 (write-string (unprintable-object-string x)
528 s))))
529 (:copier nil))
530 string)
532 ;;; Extract the function argument values for a debug frame.
533 (defun frame-args-as-list (frame)
534 (let ((debug-fun (sb!di:frame-debug-fun frame))
535 (loc (sb!di:frame-code-location frame))
536 (reversed-result nil))
537 (handler-case
538 (progn
539 (dolist (ele (sb!di:debug-fun-lambda-list debug-fun))
540 (lambda-list-element-dispatch ele
541 :required ((push (frame-call-arg ele loc frame) reversed-result))
542 :optional ((push (frame-call-arg (second ele) loc frame)
543 reversed-result))
544 :keyword ((push (second ele) reversed-result)
545 (push (frame-call-arg (third ele) loc frame)
546 reversed-result))
547 :deleted ((push (frame-call-arg ele loc frame) reversed-result))
548 :rest ((lambda-var-dispatch (second ele) loc
550 (progn
551 (setf reversed-result
552 (append (reverse (sb!di:debug-var-value
553 (second ele) frame))
554 reversed-result))
555 (return))
556 (push (make-unprintable-object
557 "unavailable &REST argument")
558 reversed-result)))))
559 ;; As long as we do an ordinary return (as opposed to SIGNALing
560 ;; a CONDITION) from the DOLIST above:
561 (nreverse reversed-result))
562 (sb!di:lambda-list-unavailable
564 (make-unprintable-object "unavailable lambda list")))))
566 ;;; Print FRAME with verbosity level 1. If we hit a &REST arg, then
567 ;;; print as many of the values as possible, punting the loop over
568 ;;; lambda-list variables since any other arguments will be in the
569 ;;; &REST arg's list of values.
570 (defun print-frame-call-1 (frame)
571 (let ((debug-fun (sb!di:frame-debug-fun frame)))
573 (pprint-logical-block (*standard-output* nil :prefix "(" :suffix ")")
574 (let ((args (ensure-printable-object (frame-args-as-list frame))))
575 ;; Since we go to some trouble to make nice informative function
576 ;; names like (PRINT-OBJECT :AROUND (CLOWN T)), let's make sure
577 ;; that they aren't truncated by *PRINT-LENGTH* and *PRINT-LEVEL*.
578 (let ((*print-length* nil)
579 (*print-level* nil))
580 (prin1 (ensure-printable-object (sb!di:debug-fun-name debug-fun))))
581 ;; For the function arguments, we can just print normally.
582 (if (listp args)
583 (format t "~{ ~_~S~}" args)
584 (format t " ~S" args))))
586 (when (sb!di:debug-fun-kind debug-fun)
587 (write-char #\[)
588 (prin1 (sb!di:debug-fun-kind debug-fun))
589 (write-char #\]))))
591 (defun ensure-printable-object (object)
592 (handler-case
593 (with-open-stream (out (make-broadcast-stream))
594 (prin1 object out)
595 object)
596 (error (cond)
597 (declare (ignore cond))
598 (make-unprintable-object "error printing object"))))
600 (defun frame-call-arg (var location frame)
601 (lambda-var-dispatch var location
602 (make-unprintable-object "unused argument")
603 (sb!di:debug-var-value var frame)
604 (make-unprintable-object "unavailable argument")))
606 ;;; Prints a representation of the function call causing FRAME to
607 ;;; exist. VERBOSITY indicates the level of information to output;
608 ;;; zero indicates just printing the DEBUG-FUN's name, and one
609 ;;; indicates displaying call-like, one-liner format with argument
610 ;;; values.
611 (defun print-frame-call (frame &key (verbosity 1) (number nil))
612 (cond
613 ((zerop verbosity)
614 (when number
615 (format t "~&~S: " (sb!di:frame-number frame)))
616 (format t "~S" frame))
618 (when number
619 (format t "~&~S: " (sb!di:frame-number frame)))
620 (print-frame-call-1 frame)))
621 (when (>= verbosity 2)
622 (let ((loc (sb!di:frame-code-location frame)))
623 (handler-case
624 (progn
625 (sb!di:code-location-debug-block loc)
626 (format t "~%source: ")
627 (print-code-location-source-form loc 0))
628 (sb!di:debug-condition (ignore) ignore)
629 (error (c) (format t "error finding source: ~A" c))))))
631 ;;;; INVOKE-DEBUGGER
633 (defvar *debugger-hook* nil
634 #!+sb-doc
635 "This is either NIL or a function of two arguments, a condition and the value
636 of *DEBUGGER-HOOK*. This function can either handle the condition or return
637 which causes the standard debugger to execute. The system passes the value
638 of this variable to the function because it binds *DEBUGGER-HOOK* to NIL
639 around the invocation.")
641 ;;; These are bound on each invocation of INVOKE-DEBUGGER.
642 (defvar *debug-restarts*)
643 (defvar *debug-condition*)
644 (defvar *nested-debug-condition*)
646 (defun invoke-debugger (condition)
647 #!+sb-doc
648 "Enter the debugger."
649 (let ((old-hook *debugger-hook*))
650 (when old-hook
651 (let ((*debugger-hook* nil))
652 (funcall old-hook condition old-hook))))
654 ;; If we're a background thread and *background-threads-wait-for-debugger*
655 ;; is NIL, this will invoke a restart
657 ;; Note: CMU CL had (SB-UNIX:UNIX-SIGSETMASK 0) here. I deleted it
658 ;; around sbcl-0.7.8.5 (by which time it had mutated to have a
659 ;; #!-SUNOS prefix and a FIXME note observing that it wasn't needed
660 ;; on SunOS and no one knew why it was needed anywhere else either).
661 ;; So if something mysteriously breaks that has worked since the CMU
662 ;; CL days, that might be why. -- WHN 2002-09-28
664 ;; We definitely want *PACKAGE* to be of valid type.
666 ;; Elsewhere in the system, we use the SANE-PACKAGE function for
667 ;; this, but here causing an exception just as we're trying to handle
668 ;; an exception would be confusing, so instead we use a special hack.
669 (unless (and (packagep *package*)
670 (package-name *package*))
671 (setf *package* (find-package :cl-user))
672 (format *error-output*
673 "The value of ~S was not an undeleted PACKAGE. It has been
674 reset to ~S."
675 '*package* *package*))
677 ;; Try to force the other special variables into a useful state.
678 (let (;; Protect from WITH-STANDARD-IO-SYNTAX some variables where
679 ;; any default we might use is less useful than just reusing
680 ;; the global values.
681 (original-package *package*)
682 (original-print-pretty *print-pretty*))
683 (with-standard-io-syntax
684 (let ((*debug-condition* condition)
685 (*debug-restarts* (compute-restarts condition))
686 (*nested-debug-condition* nil)
687 ;; We want the printer and reader to be in a useful state,
688 ;; regardless of where the debugger was invoked in the
689 ;; program. WITH-STANDARD-IO-SYNTAX did much of what we
690 ;; want, but
691 ;; * It doesn't affect our internal special variables
692 ;; like *CURRENT-LEVEL-IN-PRINT*.
693 ;; * It isn't customizable.
694 ;; * It doesn't set *PRINT-READABLY* to the same value
695 ;; as the toplevel default.
696 ;; * It sets *PACKAGE* to COMMON-LISP-USER, which is not
697 ;; helpful behavior for a debugger.
698 ;; * There's no particularly good debugger default for
699 ;; *PRINT-PRETTY*, since T is usually what you want
700 ;; -- except absolutely not what you want when you're
701 ;; debugging failures in PRINT-OBJECT logic.
702 ;; We try to address all these issues with explicit
703 ;; rebindings here.
704 (sb!kernel:*current-level-in-print* 0)
705 (*print-length* *debug-print-length*)
706 (*print-level* *debug-print-level*)
707 (*readtable* *debug-readtable*)
708 (*print-readably* nil)
709 (*package* original-package)
710 (background-p nil)
711 (*print-pretty* original-print-pretty))
713 ;; Before we start our own output, finish any pending output.
714 ;; Otherwise, if the user tried to track the progress of his
715 ;; program using PRINT statements, he'd tend to lose the last
716 ;; line of output or so, which'd be confusing.
717 (flush-standard-output-streams)
719 ;; (The initial output here goes to *ERROR-OUTPUT*, because the
720 ;; initial output is not interactive, just an error message,
721 ;; and when people redirect *ERROR-OUTPUT*, they could
722 ;; reasonably expect to see error messages logged there,
723 ;; regardless of what the debugger does afterwards.)
724 (handler-case
725 (format *error-output*
726 "~2&~@<debugger invoked on condition of type ~S: ~
727 ~2I~_~A~:>~%"
728 (type-of *debug-condition*)
729 *debug-condition*)
730 (error (condition)
731 (setf *nested-debug-condition* condition)
732 (let ((ndc-type (type-of *nested-debug-condition*)))
733 (format *error-output*
734 "~&~@<(A ~S was caught when trying to print ~S when ~
735 entering the debugger. Printing was aborted and the ~
736 ~S was stored in ~S.)~@:>~%"
737 ndc-type
738 '*debug-condition*
739 ndc-type
740 '*nested-debug-condition*))
741 (when (typep condition 'cell-error)
742 ;; what we really want to know when it's e.g. an UNBOUND-VARIABLE:
743 (format *error-output*
744 "~&(CELL-ERROR-NAME ~S) = ~S~%"
745 '*debug-condition*
746 (cell-error-name *debug-condition*)))))
748 ;; After the initial error/condition/whatever announcement to
749 ;; *ERROR-OUTPUT*, we become interactive, and should talk on
750 ;; *DEBUG-IO* from now on. (KLUDGE: This is a normative
751 ;; statement, not a description of reality.:-| There's a lot of
752 ;; older debugger code which was written to do i/o on whatever
753 ;; stream was in fashion at the time, and not all of it has
754 ;; been converted to behave this way. -- WHN 2000-11-16)
756 (setf background-p
757 (sb!thread::debugger-wait-until-foreground-thread *debug-io*))
758 (unwind-protect
759 (let (;; FIXME: Rebinding *STANDARD-OUTPUT* here seems wrong,
760 ;; violating the principle of least surprise, and making
761 ;; it impossible for the user to do reasonable things
762 ;; like using PRINT at the debugger prompt to send output
763 ;; to the program's ordinary (possibly
764 ;; redirected-to-a-file) *STANDARD-OUTPUT*. (CMU CL
765 ;; used to rebind *STANDARD-INPUT* here too, but that's
766 ;; been fixed already.)
767 (*standard-output* *debug-io*)
768 ;; This seems reasonable: e.g. if the user has redirected
769 ;; *ERROR-OUTPUT* to some log file, it's probably wrong
770 ;; to send errors which occur in interactive debugging to
771 ;; that file, and right to send them to *DEBUG-IO*.
772 (*error-output* *debug-io*))
773 (unless (typep condition 'step-condition)
774 (when *debug-beginner-help-p*
775 (format *debug-io*
776 "~%~@<Within the debugger, you can type HELP for help. ~
777 At any command prompt (within the debugger or not) you ~
778 can type (SB-EXT:QUIT) to terminate the SBCL ~
779 executable. The condition which caused the debugger to ~
780 be entered is bound to ~S. You can suppress this ~
781 message by clearing ~S.~:@>~2%"
782 '*debug-condition*
783 '*debug-beginner-help-p*))
784 (show-restarts *debug-restarts* *debug-io*))
785 (internal-debug))
786 (when background-p (sb!thread::release-foreground)))))))
788 (defun show-restarts (restarts s)
789 (cond ((null restarts)
790 (format s
791 "~&(no restarts: If you didn't do this on purpose, ~
792 please report it as a bug.)~%"))
794 (format s "~&restarts:~%")
795 (let ((count 0)
796 (names-used '(nil))
797 (max-name-len 0))
798 (dolist (restart restarts)
799 (let ((name (restart-name restart)))
800 (when name
801 (let ((len (length (princ-to-string name))))
802 (when (> len max-name-len)
803 (setf max-name-len len))))))
804 (unless (zerop max-name-len)
805 (incf max-name-len 3))
806 (dolist (restart restarts)
807 (let ((name (restart-name restart)))
808 (cond ((member name names-used)
809 (format s "~& ~2D: ~V@T~A~%" count max-name-len restart))
811 (format s "~& ~2D: [~VA] ~A~%"
812 count (- max-name-len 3) name restart)
813 (push name names-used))))
814 (incf count))))))
816 ;;; This calls DEBUG-LOOP, performing some simple initializations
817 ;;; before doing so. INVOKE-DEBUGGER calls this to actually get into
818 ;;; the debugger. SB!KERNEL::ERROR-ERROR calls this in emergencies
819 ;;; to get into a debug prompt as quickly as possible with as little
820 ;;; risk as possible for stepping on whatever is causing recursive
821 ;;; errors.
822 (defun internal-debug ()
823 (let ((*in-the-debugger* t)
824 (*read-suppress* nil))
825 (unless (typep *debug-condition* 'step-condition)
826 (clear-input *debug-io*))
827 (debug-loop)))
829 ;;;; DEBUG-LOOP
831 ;;; Note: This defaulted to T in CMU CL. The changed default in SBCL
832 ;;; was motivated by desire to play nicely with ILISP.
833 (defvar *flush-debug-errors* nil
834 #!+sb-doc
835 "When set, avoid calling INVOKE-DEBUGGER recursively when errors occur while
836 executing in the debugger.")
838 (defun debug-loop ()
839 (let* ((*debug-command-level* (1+ *debug-command-level*))
840 (*real-stack-top* (sb!di:top-frame))
841 (*stack-top* (or *stack-top-hint* *real-stack-top*))
842 (*stack-top-hint* nil)
843 (*current-frame* *stack-top*))
844 (handler-bind ((sb!di:debug-condition
845 (lambda (condition)
846 (princ condition *debug-io*)
847 (/show0 "handling d-c by THROWing DEBUG-LOOP-CATCHER")
848 (throw 'debug-loop-catcher nil))))
849 (fresh-line)
850 (print-frame-call *current-frame* :verbosity 2)
851 (loop
852 (catch 'debug-loop-catcher
853 (handler-bind ((error (lambda (condition)
854 (when *flush-debug-errors*
855 (clear-input *debug-io*)
856 (princ condition)
857 ;; FIXME: Doing input on *DEBUG-IO*
858 ;; and output on T seems broken.
859 (format t
860 "~&error flushed (because ~
861 ~S is set)"
862 '*flush-debug-errors*)
863 (/show0 "throwing DEBUG-LOOP-CATCHER")
864 (throw 'debug-loop-catcher nil)))))
865 ;; We have to bind LEVEL for the restart function created by
866 ;; WITH-SIMPLE-RESTART.
867 (let ((level *debug-command-level*)
868 (restart-commands (make-restart-commands)))
869 (with-simple-restart (abort
870 "~@<Reduce debugger level (to debug level ~W).~@:>"
871 level)
872 (debug-prompt *debug-io*)
873 (force-output *debug-io*)
874 (let ((input (sb!int:get-stream-command *debug-io*)))
875 (cond (input
876 (let ((cmd-fun (debug-command-p
877 (sb!int:stream-command-name input)
878 restart-commands)))
879 (cond
880 ((not cmd-fun)
881 (error "unknown stream-command: ~S" input))
882 ((consp cmd-fun)
883 (error "ambiguous debugger command: ~S" cmd-fun))
885 (apply cmd-fun
886 (sb!int:stream-command-args input))))))
888 (let* ((exp (read *debug-io*))
889 (cmd-fun (debug-command-p exp
890 restart-commands)))
891 (cond ((not cmd-fun)
892 (debug-eval-print exp))
893 ((consp cmd-fun)
894 (format t
895 "~&Your command, ~S, is ambiguous:~%"
896 exp)
897 (dolist (ele cmd-fun)
898 (format t " ~A~%" ele)))
900 (funcall cmd-fun)))))))))))))))
902 ;;; FIXME: We could probably use INTERACTIVE-EVAL for much of this logic.
903 (defun debug-eval-print (expr)
904 (/noshow "entering DEBUG-EVAL-PRINT" expr)
905 (/noshow (fboundp 'compile))
906 (setq +++ ++ ++ + + - - expr)
907 (let* ((values (multiple-value-list (eval -)))
908 (*standard-output* *debug-io*))
909 (/noshow "done with EVAL in DEBUG-EVAL-PRINT")
910 (fresh-line)
911 (if values (prin1 (car values)))
912 (dolist (x (cdr values))
913 (fresh-line)
914 (prin1 x))
915 (setq /// // // / / values)
916 (setq *** ** ** * * (car values))
917 ;; Make sure that nobody passes back an unbound marker.
918 (unless (boundp '*)
919 (setq * nil)
920 (fresh-line)
921 ;; FIXME: The way INTERACTIVE-EVAL does this seems better.
922 (princ "Setting * to NIL (was unbound marker)."))))
924 ;;;; debug loop functions
926 ;;; These commands are functions, not really commands, so that users
927 ;;; can get their hands on the values returned.
929 (eval-when (:execute :compile-toplevel)
931 (sb!xc:defmacro define-var-operation (ref-or-set &optional value-var)
932 `(let* ((temp (etypecase name
933 (symbol (sb!di:debug-fun-symbol-vars
934 (sb!di:frame-debug-fun *current-frame*)
935 name))
936 (simple-string (sb!di:ambiguous-debug-vars
937 (sb!di:frame-debug-fun *current-frame*)
938 name))))
939 (location (sb!di:frame-code-location *current-frame*))
940 ;; Let's only deal with valid variables.
941 (vars (remove-if-not (lambda (v)
942 (eq (sb!di:debug-var-validity v location)
943 :valid))
944 temp)))
945 (declare (list vars))
946 (cond ((null vars)
947 (error "No known valid variables match ~S." name))
948 ((= (length vars) 1)
949 ,(ecase ref-or-set
950 (:ref
951 '(sb!di:debug-var-value (car vars) *current-frame*))
952 (:set
953 `(setf (sb!di:debug-var-value (car vars) *current-frame*)
954 ,value-var))))
956 ;; Since we have more than one, first see whether we have
957 ;; any variables that exactly match the specification.
958 (let* ((name (etypecase name
959 (symbol (symbol-name name))
960 (simple-string name)))
961 ;; FIXME: REMOVE-IF-NOT is deprecated, use STRING/=
962 ;; instead.
963 (exact (remove-if-not (lambda (v)
964 (string= (sb!di:debug-var-symbol-name v)
965 name))
966 vars))
967 (vars (or exact vars)))
968 (declare (simple-string name)
969 (list exact vars))
970 (cond
971 ;; Check now for only having one variable.
972 ((= (length vars) 1)
973 ,(ecase ref-or-set
974 (:ref
975 '(sb!di:debug-var-value (car vars) *current-frame*))
976 (:set
977 `(setf (sb!di:debug-var-value (car vars) *current-frame*)
978 ,value-var))))
979 ;; If there weren't any exact matches, flame about
980 ;; ambiguity unless all the variables have the same
981 ;; name.
982 ((and (not exact)
983 (find-if-not
984 (lambda (v)
985 (string= (sb!di:debug-var-symbol-name v)
986 (sb!di:debug-var-symbol-name (car vars))))
987 (cdr vars)))
988 (error "specification ambiguous:~%~{ ~A~%~}"
989 (mapcar #'sb!di:debug-var-symbol-name
990 (delete-duplicates
991 vars :test #'string=
992 :key #'sb!di:debug-var-symbol-name))))
993 ;; All names are the same, so see whether the user
994 ;; ID'ed one of them.
995 (id-supplied
996 (let ((v (find id vars :key #'sb!di:debug-var-id)))
997 (unless v
998 (error
999 "invalid variable ID, ~W: should have been one of ~S"
1001 (mapcar #'sb!di:debug-var-id vars)))
1002 ,(ecase ref-or-set
1003 (:ref
1004 '(sb!di:debug-var-value v *current-frame*))
1005 (:set
1006 `(setf (sb!di:debug-var-value v *current-frame*)
1007 ,value-var)))))
1009 (error "Specify variable ID to disambiguate ~S. Use one of ~S."
1010 name
1011 (mapcar #'sb!di:debug-var-id vars)))))))))
1013 ) ; EVAL-WHEN
1015 ;;; FIXME: This doesn't work. It would be real nice we could make it
1016 ;;; work! Alas, it doesn't seem to work in CMU CL X86 either..
1017 (defun var (name &optional (id 0 id-supplied))
1018 #!+sb-doc
1019 "Return a variable's value if possible. NAME is a simple-string or symbol.
1020 If it is a simple-string, it is an initial substring of the variable's name.
1021 If name is a symbol, it has the same name and package as the variable whose
1022 value this function returns. If the symbol is uninterned, then the variable
1023 has the same name as the symbol, but it has no package.
1025 If name is the initial substring of variables with different names, then
1026 this return no values after displaying the ambiguous names. If name
1027 determines multiple variables with the same name, then you must use the
1028 optional id argument to specify which one you want. If you left id
1029 unspecified, then this returns no values after displaying the distinguishing
1030 id values.
1032 The result of this function is limited to the availability of variable
1033 information. This is SETF'able."
1034 (define-var-operation :ref))
1035 (defun (setf var) (value name &optional (id 0 id-supplied))
1036 (define-var-operation :set value))
1038 ;;; This returns the COUNT'th arg as the user sees it from args, the
1039 ;;; result of SB!DI:DEBUG-FUN-LAMBDA-LIST. If this returns a
1040 ;;; potential DEBUG-VAR from the lambda-list, then the second value is
1041 ;;; T. If this returns a keyword symbol or a value from a rest arg,
1042 ;;; then the second value is NIL.
1044 ;;; FIXME: There's probably some way to merge the code here with
1045 ;;; FRAME-ARGS-AS-LIST. (A fair amount of logic is already shared
1046 ;;; through LAMBDA-LIST-ELEMENT-DISPATCH, but I suspect more could be.)
1047 (declaim (ftype (function (index list)) nth-arg))
1048 (defun nth-arg (count args)
1049 (let ((n count))
1050 (dolist (ele args (error "The argument specification ~S is out of range."
1052 (lambda-list-element-dispatch ele
1053 :required ((if (zerop n) (return (values ele t))))
1054 :optional ((if (zerop n) (return (values (second ele) t))))
1055 :keyword ((cond ((zerop n)
1056 (return (values (second ele) nil)))
1057 ((zerop (decf n))
1058 (return (values (third ele) t)))))
1059 :deleted ((if (zerop n) (return (values ele t))))
1060 :rest ((let ((var (second ele)))
1061 (lambda-var-dispatch var (sb!di:frame-code-location
1062 *current-frame*)
1063 (error "unused &REST argument before n'th argument")
1064 (dolist (value
1065 (sb!di:debug-var-value var *current-frame*)
1066 (error
1067 "The argument specification ~S is out of range."
1069 (if (zerop n)
1070 (return-from nth-arg (values value nil))
1071 (decf n)))
1072 (error "invalid &REST argument before n'th argument")))))
1073 (decf n))))
1075 (defun arg (n)
1076 #!+sb-doc
1077 "Return the N'th argument's value if possible. Argument zero is the first
1078 argument in a frame's default printed representation. Count keyword/value
1079 pairs as separate arguments."
1080 (multiple-value-bind (var lambda-var-p)
1081 (nth-arg n (handler-case (sb!di:debug-fun-lambda-list
1082 (sb!di:frame-debug-fun *current-frame*))
1083 (sb!di:lambda-list-unavailable ()
1084 (error "No argument values are available."))))
1085 (if lambda-var-p
1086 (lambda-var-dispatch var (sb!di:frame-code-location *current-frame*)
1087 (error "Unused arguments have no values.")
1088 (sb!di:debug-var-value var *current-frame*)
1089 (error "invalid argument value"))
1090 var)))
1092 ;;;; machinery for definition of debug loop commands
1094 (defvar *debug-commands* nil)
1096 ;;; Interface to *DEBUG-COMMANDS*. No required arguments in args are
1097 ;;; permitted.
1098 (defmacro !def-debug-command (name args &rest body)
1099 (let ((fun-name (symbolicate name "-DEBUG-COMMAND")))
1100 `(progn
1101 (setf *debug-commands*
1102 (remove ,name *debug-commands* :key #'car :test #'string=))
1103 (defun ,fun-name ,args
1104 (unless *in-the-debugger*
1105 (error "invoking debugger command while outside the debugger"))
1106 ,@body)
1107 (push (cons ,name #',fun-name) *debug-commands*)
1108 ',fun-name)))
1110 (defun !def-debug-command-alias (new-name existing-name)
1111 (let ((pair (assoc existing-name *debug-commands* :test #'string=)))
1112 (unless pair (error "unknown debug command name: ~S" existing-name))
1113 (push (cons new-name (cdr pair)) *debug-commands*))
1114 new-name)
1116 ;;; This takes a symbol and uses its name to find a debugger command,
1117 ;;; using initial substring matching. It returns the command function
1118 ;;; if form identifies only one command, but if form is ambiguous,
1119 ;;; this returns a list of the command names. If there are no matches,
1120 ;;; this returns nil. Whenever the loop that looks for a set of
1121 ;;; possibilities encounters an exact name match, we return that
1122 ;;; command function immediately.
1123 (defun debug-command-p (form &optional other-commands)
1124 (if (or (symbolp form) (integerp form))
1125 (let* ((name
1126 (if (symbolp form)
1127 (symbol-name form)
1128 (format nil "~W" form)))
1129 (len (length name))
1130 (res nil))
1131 (declare (simple-string name)
1132 (fixnum len)
1133 (list res))
1135 ;; Find matching commands, punting if exact match.
1136 (flet ((match-command (ele)
1137 (let* ((str (car ele))
1138 (str-len (length str)))
1139 (declare (simple-string str)
1140 (fixnum str-len))
1141 (cond ((< str-len len))
1142 ((= str-len len)
1143 (when (string= name str :end1 len :end2 len)
1144 (return-from debug-command-p (cdr ele))))
1145 ((string= name str :end1 len :end2 len)
1146 (push ele res))))))
1147 (mapc #'match-command *debug-commands*)
1148 (mapc #'match-command other-commands))
1150 ;; Return the right value.
1151 (cond ((not res) nil)
1152 ((= (length res) 1)
1153 (cdar res))
1154 (t ; Just return the names.
1155 (do ((cmds res (cdr cmds)))
1156 ((not cmds) res)
1157 (setf (car cmds) (caar cmds))))))))
1159 ;;; Return a list of debug commands (in the same format as
1160 ;;; *DEBUG-COMMANDS*) that invoke each active restart.
1162 ;;; Two commands are made for each restart: one for the number, and
1163 ;;; one for the restart name (unless it's been shadowed by an earlier
1164 ;;; restart of the same name, or it is NIL).
1165 (defun make-restart-commands (&optional (restarts *debug-restarts*))
1166 (let ((commands)
1167 (num 0)) ; better be the same as show-restarts!
1168 (dolist (restart restarts)
1169 (let ((name (string (restart-name restart))))
1170 (let ((restart-fun
1171 (lambda ()
1172 (/show0 "in restart-command closure, about to i-r-i")
1173 (invoke-restart-interactively restart))))
1174 (push (cons (prin1-to-string num) restart-fun) commands)
1175 (unless (or (null (restart-name restart))
1176 (find name commands :key #'car :test #'string=))
1177 (push (cons name restart-fun) commands))))
1178 (incf num))
1179 commands))
1181 ;;;; frame-changing commands
1183 (!def-debug-command "UP" ()
1184 (let ((next (sb!di:frame-up *current-frame*)))
1185 (cond (next
1186 (setf *current-frame* next)
1187 (print-frame-call next))
1189 (format t "~&Top of stack.")))))
1191 (!def-debug-command "DOWN" ()
1192 (let ((next (sb!di:frame-down *current-frame*)))
1193 (cond (next
1194 (setf *current-frame* next)
1195 (print-frame-call next))
1197 (format t "~&Bottom of stack.")))))
1199 (!def-debug-command-alias "D" "DOWN")
1201 ;;; CMU CL had this command, but SBCL doesn't, since it's redundant
1202 ;;; with "FRAME 0", and it interferes with abbreviations for the
1203 ;;; TOPLEVEL restart.
1204 ;;;(!def-debug-command "TOP" ()
1205 ;;; (do ((prev *current-frame* lead)
1206 ;;; (lead (sb!di:frame-up *current-frame*) (sb!di:frame-up lead)))
1207 ;;; ((null lead)
1208 ;;; (setf *current-frame* prev)
1209 ;;; (print-frame-call prev))))
1211 (!def-debug-command "BOTTOM" ()
1212 (do ((prev *current-frame* lead)
1213 (lead (sb!di:frame-down *current-frame*) (sb!di:frame-down lead)))
1214 ((null lead)
1215 (setf *current-frame* prev)
1216 (print-frame-call prev))))
1218 (!def-debug-command-alias "B" "BOTTOM")
1220 (!def-debug-command "FRAME" (&optional
1221 (n (read-prompting-maybe "frame number: ")))
1222 (setf *current-frame*
1223 (multiple-value-bind (next-frame-fun limit-string)
1224 (if (< n (sb!di:frame-number *current-frame*))
1225 (values #'sb!di:frame-up "top")
1226 (values #'sb!di:frame-down "bottom"))
1227 (do ((frame *current-frame*))
1228 ((= n (sb!di:frame-number frame))
1229 frame)
1230 (let ((next-frame (funcall next-frame-fun frame)))
1231 (cond (next-frame
1232 (setf frame next-frame))
1234 (format t
1235 "The ~A of the stack was encountered.~%"
1236 limit-string)
1237 (return frame)))))))
1238 (print-frame-call *current-frame*))
1240 (!def-debug-command-alias "F" "FRAME")
1242 ;;;; commands for entering and leaving the debugger
1244 ;;; CMU CL supported this QUIT debug command, but SBCL provides this
1245 ;;; functionality with a restart instead. (The QUIT debug command was
1246 ;;; removed because it's confusing to have "quit" mean two different
1247 ;;; things in the system, "restart the top level REPL" in the debugger
1248 ;;; and "terminate the Lisp system" as the SB-EXT:QUIT function.)
1250 ;;;(!def-debug-command "QUIT" ()
1251 ;;; (throw 'sb!impl::toplevel-catcher nil))
1253 ;;; CMU CL supported this GO debug command, but SBCL doesn't -- in
1254 ;;; SBCL you just type the CONTINUE restart name instead (or "C" or
1255 ;;; "RESTART CONTINUE", that's OK too).
1256 ;;;(!def-debug-command "GO" ()
1257 ;;; (continue *debug-condition*)
1258 ;;; (error "There is no restart named CONTINUE."))
1260 (!def-debug-command "RESTART" ()
1261 (/show0 "doing RESTART debug-command")
1262 (let ((num (read-if-available :prompt)))
1263 (when (eq num :prompt)
1264 (show-restarts *debug-restarts* *debug-io*)
1265 (write-string "restart: ")
1266 (force-output)
1267 (setf num (read *debug-io*)))
1268 (let ((restart (typecase num
1269 (unsigned-byte
1270 (nth num *debug-restarts*))
1271 (symbol
1272 (find num *debug-restarts* :key #'restart-name
1273 :test (lambda (sym1 sym2)
1274 (string= (symbol-name sym1)
1275 (symbol-name sym2)))))
1277 (format t "~S is invalid as a restart name.~%" num)
1278 (return-from restart-debug-command nil)))))
1279 (/show0 "got RESTART")
1280 (if restart
1281 (invoke-restart-interactively restart)
1282 ;; FIXME: Even if this isn't handled by WARN, it probably
1283 ;; shouldn't go to *STANDARD-OUTPUT*, but *ERROR-OUTPUT* or
1284 ;; *QUERY-IO* or something. Look through this file to
1285 ;; straighten out stream usage.
1286 (princ "There is no such restart.")))))
1288 ;;;; information commands
1290 (!def-debug-command "HELP" ()
1291 ;; CMU CL had a little toy pager here, but "if you aren't running
1292 ;; ILISP (or a smart windowing system, or something) you deserve to
1293 ;; lose", so we've dropped it in SBCL. However, in case some
1294 ;; desperate holdout is running this on a dumb terminal somewhere,
1295 ;; we tell him where to find the message stored as a string.
1296 (format *debug-io*
1297 "~&~A~2%(The HELP string is stored in ~S.)~%"
1298 *debug-help-string*
1299 '*debug-help-string*))
1301 (!def-debug-command-alias "?" "HELP")
1303 (!def-debug-command "ERROR" ()
1304 (format *debug-io* "~A~%" *debug-condition*)
1305 (show-restarts *debug-restarts* *debug-io*))
1307 (!def-debug-command "BACKTRACE" ()
1308 (backtrace (read-if-available most-positive-fixnum)))
1310 (!def-debug-command "PRINT" ()
1311 (print-frame-call *current-frame*))
1313 (!def-debug-command-alias "P" "PRINT")
1315 (!def-debug-command "LIST-LOCALS" ()
1316 (let ((d-fun (sb!di:frame-debug-fun *current-frame*)))
1317 (if (sb!di:debug-var-info-available d-fun)
1318 (let ((*standard-output* *debug-io*)
1319 (location (sb!di:frame-code-location *current-frame*))
1320 (prefix (read-if-available nil))
1321 (any-p nil)
1322 (any-valid-p nil))
1323 (dolist (v (sb!di:ambiguous-debug-vars
1324 d-fun
1325 (if prefix (string prefix) "")))
1326 (setf any-p t)
1327 (when (eq (sb!di:debug-var-validity v location) :valid)
1328 (setf any-valid-p t)
1329 (format t "~S~:[#~W~;~*~] = ~S~%"
1330 (sb!di:debug-var-symbol v)
1331 (zerop (sb!di:debug-var-id v))
1332 (sb!di:debug-var-id v)
1333 (sb!di:debug-var-value v *current-frame*))))
1335 (cond
1336 ((not any-p)
1337 (format t "There are no local variables ~@[starting with ~A ~]~
1338 in the function."
1339 prefix))
1340 ((not any-valid-p)
1341 (format t "All variables ~@[starting with ~A ~]currently ~
1342 have invalid values."
1343 prefix))))
1344 (write-line "There is no variable information available."))))
1346 (!def-debug-command-alias "L" "LIST-LOCALS")
1348 (!def-debug-command "SOURCE" ()
1349 (fresh-line)
1350 (print-code-location-source-form (sb!di:frame-code-location *current-frame*)
1351 (read-if-available 0)))
1353 ;;;; source location printing
1355 ;;; We cache a stream to the last valid file debug source so that we
1356 ;;; won't have to repeatedly open the file.
1358 ;;; KLUDGE: This sounds like a bug, not a feature. Opening files is fast
1359 ;;; in the 1990s, so the benefit is negligible, less important than the
1360 ;;; potential of extra confusion if someone changes the source during
1361 ;;; a debug session and the change doesn't show up. And removing this
1362 ;;; would simplify the system, which I like. -- WHN 19990903
1363 (defvar *cached-debug-source* nil)
1364 (declaim (type (or sb!di:debug-source null) *cached-debug-source*))
1365 (defvar *cached-source-stream* nil)
1366 (declaim (type (or stream null) *cached-source-stream*))
1368 ;;; To suppress the read-time evaluation #. macro during source read,
1369 ;;; *READTABLE* is modified. *READTABLE* is cached to avoid
1370 ;;; copying it each time, and invalidated when the
1371 ;;; *CACHED-DEBUG-SOURCE* has changed.
1372 (defvar *cached-readtable* nil)
1373 (declaim (type (or readtable null) *cached-readtable*))
1375 (pushnew (lambda ()
1376 (setq *cached-debug-source* nil *cached-source-stream* nil
1377 *cached-readtable* nil))
1378 *before-save-initializations*)
1380 ;;; We also cache the last toplevel form that we printed a source for
1381 ;;; so that we don't have to do repeated reads and calls to
1382 ;;; FORM-NUMBER-TRANSLATIONS.
1383 (defvar *cached-toplevel-form-offset* nil)
1384 (declaim (type (or index null) *cached-toplevel-form-offset*))
1385 (defvar *cached-toplevel-form*)
1386 (defvar *cached-form-number-translations*)
1388 ;;; Given a code location, return the associated form-number
1389 ;;; translations and the actual top level form. We check our cache ---
1390 ;;; if there is a miss, we dispatch on the kind of the debug source.
1391 (defun get-toplevel-form (location)
1392 (let ((d-source (sb!di:code-location-debug-source location)))
1393 (if (and (eq d-source *cached-debug-source*)
1394 (eql (sb!di:code-location-toplevel-form-offset location)
1395 *cached-toplevel-form-offset*))
1396 (values *cached-form-number-translations* *cached-toplevel-form*)
1397 (let* ((offset (sb!di:code-location-toplevel-form-offset location))
1398 (res
1399 (ecase (sb!di:debug-source-from d-source)
1400 (:file (get-file-toplevel-form location))
1401 (:lisp (svref (sb!di:debug-source-name d-source) offset)))))
1402 (setq *cached-toplevel-form-offset* offset)
1403 (values (setq *cached-form-number-translations*
1404 (sb!di:form-number-translations res offset))
1405 (setq *cached-toplevel-form* res))))))
1407 ;;; Locate the source file (if it still exists) and grab the top level
1408 ;;; form. If the file is modified, we use the top level form offset
1409 ;;; instead of the recorded character offset.
1410 (defun get-file-toplevel-form (location)
1411 (let* ((d-source (sb!di:code-location-debug-source location))
1412 (tlf-offset (sb!di:code-location-toplevel-form-offset location))
1413 (local-tlf-offset (- tlf-offset
1414 (sb!di:debug-source-root-number d-source)))
1415 (char-offset
1416 (aref (or (sb!di:debug-source-start-positions d-source)
1417 (error "no start positions map"))
1418 local-tlf-offset))
1419 (name (sb!di:debug-source-name d-source)))
1420 (unless (eq d-source *cached-debug-source*)
1421 (unless (and *cached-source-stream*
1422 (equal (pathname *cached-source-stream*)
1423 (pathname name)))
1424 (setq *cached-readtable* nil)
1425 (when *cached-source-stream* (close *cached-source-stream*))
1426 (setq *cached-source-stream* (open name :if-does-not-exist nil))
1427 (unless *cached-source-stream*
1428 (error "The source file no longer exists:~% ~A" (namestring name)))
1429 (format t "~%; file: ~A~%" (namestring name)))
1431 (setq *cached-debug-source*
1432 (if (= (sb!di:debug-source-created d-source)
1433 (file-write-date name))
1434 d-source nil)))
1436 (cond
1437 ((eq *cached-debug-source* d-source)
1438 (file-position *cached-source-stream* char-offset))
1440 (format t "~%; File has been modified since compilation:~%; ~A~@
1441 ; Using form offset instead of character position.~%"
1442 (namestring name))
1443 (file-position *cached-source-stream* 0)
1444 (let ((*read-suppress* t))
1445 (dotimes (i local-tlf-offset)
1446 (read *cached-source-stream*)))))
1447 (unless *cached-readtable*
1448 (setq *cached-readtable* (copy-readtable))
1449 (set-dispatch-macro-character
1450 #\# #\.
1451 (lambda (stream sub-char &rest rest)
1452 (declare (ignore rest sub-char))
1453 (let ((token (read stream t nil t)))
1454 (format nil "#.~S" token)))
1455 *cached-readtable*))
1456 (let ((*readtable* *cached-readtable*))
1457 (read *cached-source-stream*))))
1459 (defun print-code-location-source-form (location context)
1460 (let* ((location (maybe-block-start-location location))
1461 (form-num (sb!di:code-location-form-number location)))
1462 (multiple-value-bind (translations form) (get-toplevel-form location)
1463 (unless (< form-num (length translations))
1464 (error "The source path no longer exists."))
1465 (prin1 (sb!di:source-path-context form
1466 (svref translations form-num)
1467 context)))))
1469 ;;; breakpoint and step commands
1471 ;;; Step to the next code-location.
1472 (!def-debug-command "STEP" ()
1473 (setf *number-of-steps* (read-if-available 1))
1474 (set-step-breakpoint *current-frame*)
1475 (continue *debug-condition*)
1476 (error "couldn't continue"))
1478 ;;; List possible breakpoint locations, which ones are active, and
1479 ;;; where the CONTINUE restart will transfer control. Set
1480 ;;; *POSSIBLE-BREAKPOINTS* to the code-locations which can then be
1481 ;;; used by sbreakpoint.
1482 (!def-debug-command "LIST-LOCATIONS" ()
1483 (let ((df (read-if-available *default-breakpoint-debug-fun*)))
1484 (cond ((consp df)
1485 (setf df (sb!di:fun-debug-fun (eval df)))
1486 (setf *default-breakpoint-debug-fun* df))
1487 ((or (eq ':c df)
1488 (not *default-breakpoint-debug-fun*))
1489 (setf df (sb!di:frame-debug-fun *current-frame*))
1490 (setf *default-breakpoint-debug-fun* df)))
1491 (setf *possible-breakpoints* (possible-breakpoints df)))
1492 (let ((continue-at (sb!di:frame-code-location *current-frame*)))
1493 (let ((active (location-in-list *default-breakpoint-debug-fun*
1494 *breakpoints* :fun-start))
1495 (here (sb!di:code-location=
1496 (sb!di:debug-fun-start-location
1497 *default-breakpoint-debug-fun*) continue-at)))
1498 (when (or active here)
1499 (format t "::FUN-START ")
1500 (when active (format t " *Active*"))
1501 (when here (format t " *Continue here*"))))
1503 (let ((prev-location nil)
1504 (prev-num 0)
1505 (this-num 0))
1506 (flet ((flush ()
1507 (when prev-location
1508 (let ((this-num (1- this-num)))
1509 (if (= prev-num this-num)
1510 (format t "~&~W: " prev-num)
1511 (format t "~&~W-~W: " prev-num this-num)))
1512 (print-code-location-source-form prev-location 0)
1513 (when *print-location-kind*
1514 (format t "~S " (sb!di:code-location-kind prev-location)))
1515 (when (location-in-list prev-location *breakpoints*)
1516 (format t " *Active*"))
1517 (when (sb!di:code-location= prev-location continue-at)
1518 (format t " *Continue here*")))))
1520 (dolist (code-location *possible-breakpoints*)
1521 (when (or *print-location-kind*
1522 (location-in-list code-location *breakpoints*)
1523 (sb!di:code-location= code-location continue-at)
1524 (not prev-location)
1525 (not (eq (sb!di:code-location-debug-source code-location)
1526 (sb!di:code-location-debug-source prev-location)))
1527 (not (eq (sb!di:code-location-toplevel-form-offset
1528 code-location)
1529 (sb!di:code-location-toplevel-form-offset
1530 prev-location)))
1531 (not (eq (sb!di:code-location-form-number code-location)
1532 (sb!di:code-location-form-number prev-location))))
1533 (flush)
1534 (setq prev-location code-location prev-num this-num))
1536 (incf this-num))))
1538 (when (location-in-list *default-breakpoint-debug-fun*
1539 *breakpoints*
1540 :fun-end)
1541 (format t "~&::FUN-END *Active* "))))
1543 (!def-debug-command-alias "LL" "LIST-LOCATIONS")
1545 ;;; Set breakpoint at the given number.
1546 (!def-debug-command "BREAKPOINT" ()
1547 (let ((index (read-prompting-maybe "location number, :START, or :END: "))
1548 (break t)
1549 (condition t)
1550 (print nil)
1551 (print-functions nil)
1552 (function nil)
1553 (bp)
1554 (place *default-breakpoint-debug-fun*))
1555 (flet ((get-command-line ()
1556 (let ((command-line nil)
1557 (unique '(nil)))
1558 (loop
1559 (let ((next-input (read-if-available unique)))
1560 (when (eq next-input unique) (return))
1561 (push next-input command-line)))
1562 (nreverse command-line)))
1563 (set-vars-from-command-line (command-line)
1564 (do ((arg (pop command-line) (pop command-line)))
1565 ((not arg))
1566 (ecase arg
1567 (:condition (setf condition (pop command-line)))
1568 (:print (push (pop command-line) print))
1569 (:break (setf break (pop command-line)))
1570 (:function
1571 (setf function (eval (pop command-line)))
1572 (setf *default-breakpoint-debug-fun*
1573 (sb!di:fun-debug-fun function))
1574 (setf place *default-breakpoint-debug-fun*)
1575 (setf *possible-breakpoints*
1576 (possible-breakpoints
1577 *default-breakpoint-debug-fun*))))))
1578 (setup-fun-start ()
1579 (let ((code-loc (sb!di:debug-fun-start-location place)))
1580 (setf bp (sb!di:make-breakpoint #'main-hook-fun
1581 place
1582 :kind :fun-start))
1583 (setf break (sb!di:preprocess-for-eval break code-loc))
1584 (setf condition (sb!di:preprocess-for-eval condition code-loc))
1585 (dolist (form print)
1586 (push (cons (sb!di:preprocess-for-eval form code-loc) form)
1587 print-functions))))
1588 (setup-fun-end ()
1589 (setf bp
1590 (sb!di:make-breakpoint #'main-hook-fun
1591 place
1592 :kind :fun-end))
1593 (setf break
1594 ;; FIXME: These and any other old (COERCE `(LAMBDA ..) ..)
1595 ;; forms should be converted to shiny new (LAMBDA ..) forms.
1596 ;; (Search the sources for "coerce.*\(lambda".)
1597 (coerce `(lambda (dummy)
1598 (declare (ignore dummy)) ,break)
1599 'function))
1600 (setf condition (coerce `(lambda (dummy)
1601 (declare (ignore dummy)) ,condition)
1602 'function))
1603 (dolist (form print)
1604 (push (cons
1605 (coerce `(lambda (dummy)
1606 (declare (ignore dummy)) ,form) 'function)
1607 form)
1608 print-functions)))
1609 (setup-code-location ()
1610 (setf place (nth index *possible-breakpoints*))
1611 (setf bp (sb!di:make-breakpoint #'main-hook-fun place
1612 :kind :code-location))
1613 (dolist (form print)
1614 (push (cons
1615 (sb!di:preprocess-for-eval form place)
1616 form)
1617 print-functions))
1618 (setf break (sb!di:preprocess-for-eval break place))
1619 (setf condition (sb!di:preprocess-for-eval condition place))))
1620 (set-vars-from-command-line (get-command-line))
1621 (cond
1622 ((or (eq index :start) (eq index :s))
1623 (setup-fun-start))
1624 ((or (eq index :end) (eq index :e))
1625 (setup-fun-end))
1627 (setup-code-location)))
1628 (sb!di:activate-breakpoint bp)
1629 (let* ((new-bp-info (create-breakpoint-info place bp index
1630 :break break
1631 :print print-functions
1632 :condition condition))
1633 (old-bp-info (location-in-list new-bp-info *breakpoints*)))
1634 (when old-bp-info
1635 (sb!di:deactivate-breakpoint (breakpoint-info-breakpoint
1636 old-bp-info))
1637 (setf *breakpoints* (remove old-bp-info *breakpoints*))
1638 (format t "previous breakpoint removed~%"))
1639 (push new-bp-info *breakpoints*))
1640 (print-breakpoint-info (first *breakpoints*))
1641 (format t "~&added"))))
1643 (!def-debug-command-alias "BP" "BREAKPOINT")
1645 ;;; List all breakpoints which are set.
1646 (!def-debug-command "LIST-BREAKPOINTS" ()
1647 (setf *breakpoints*
1648 (sort *breakpoints* #'< :key #'breakpoint-info-breakpoint-number))
1649 (dolist (info *breakpoints*)
1650 (print-breakpoint-info info)))
1652 (!def-debug-command-alias "LB" "LIST-BREAKPOINTS")
1653 (!def-debug-command-alias "LBP" "LIST-BREAKPOINTS")
1655 ;;; Remove breakpoint N, or remove all breakpoints if no N given.
1656 (!def-debug-command "DELETE-BREAKPOINT" ()
1657 (let* ((index (read-if-available nil))
1658 (bp-info
1659 (find index *breakpoints* :key #'breakpoint-info-breakpoint-number)))
1660 (cond (bp-info
1661 (sb!di:delete-breakpoint (breakpoint-info-breakpoint bp-info))
1662 (setf *breakpoints* (remove bp-info *breakpoints*))
1663 (format t "breakpoint ~S removed~%" index))
1664 (index (format t "The breakpoint doesn't exist."))
1666 (dolist (ele *breakpoints*)
1667 (sb!di:delete-breakpoint (breakpoint-info-breakpoint ele)))
1668 (setf *breakpoints* nil)
1669 (format t "all breakpoints deleted~%")))))
1671 (!def-debug-command-alias "DBP" "DELETE-BREAKPOINT")
1673 ;;; miscellaneous commands
1675 (!def-debug-command "DESCRIBE" ()
1676 (let* ((curloc (sb!di:frame-code-location *current-frame*))
1677 (debug-fun (sb!di:code-location-debug-fun curloc))
1678 (function (sb!di:debug-fun-fun debug-fun)))
1679 (if function
1680 (describe function)
1681 (format t "can't figure out the function for this frame"))))
1683 (!def-debug-command "SLURP" ()
1684 (loop while (read-char-no-hang *standard-input*)))
1686 (!def-debug-command "RETURN" (&optional
1687 (return (read-prompting-maybe
1688 "return: ")))
1689 (let ((tag (find-if (lambda (x)
1690 (and (typep (car x) 'symbol)
1691 (not (symbol-package (car x)))
1692 (string= (car x) "SB-DEBUG-CATCH-TAG")))
1693 (sb!di::frame-catches *current-frame*))))
1694 (if tag
1695 (throw (car tag)
1696 (funcall (sb!di:preprocess-for-eval
1697 return
1698 (sb!di:frame-code-location *current-frame*))
1699 *current-frame*))
1700 (format t "~@<can't find a tag for this frame ~
1701 ~2I~_(hint: try increasing the DEBUG optimization quality ~
1702 and recompiling)~:@>"))))
1704 ;;;; debug loop command utilities
1706 (defun read-prompting-maybe (prompt)
1707 (unless (sb!int:listen-skip-whitespace *debug-io*)
1708 (princ prompt)
1709 (force-output))
1710 (read *debug-io*))
1712 (defun read-if-available (default)
1713 (if (sb!int:listen-skip-whitespace *debug-io*)
1714 (read *debug-io*)
1715 default))