1 ;;;; the CL:INSPECT function
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-IMPL") ;(SB-IMPL, not SB!IMPL, since we're built in warm load.)
14 (defparameter *inspect-length* 10)
16 ;;; When *INSPECT-UNBOUND-OBJECT-MARKER* occurs in a parts list, it
17 ;;; indicates that that a slot is unbound.
18 (defvar *inspect-unbound-object-marker* (gensym "INSPECT-UNBOUND-OBJECT-"))
20 (defun inspect (object)
22 (%inspect object *standard-output*))
26 (setf (documentation '*inspected* 'variable)
27 "the value currently being inspected in CL:INSPECT")
29 (defvar *help-for-inspect*
32 Q, E - Quit the inspector.
33 <integer> - Inspect the numbered slot.
34 R - Redisplay current inspected object.
35 U - Move upward/backward to previous inspected object.
36 ?, H, Help - Show this help.
37 <other> - Evaluate the input as an expression.
38 Within the inspector, the special variable SB-EXT:*INSPECTED* is bound
39 to the current inspected object, so that it can be referred to in
40 evaluated expressions.
43 (defun %inspect (*inspected* s)
44 (named-let redisplay () ; "lambda, the ultimate GOTO":-|
45 (multiple-value-bind (description named-p elements)
46 (inspected-parts *inspected*)
47 (tty-display-inspected-parts description named-p elements s)
51 (let (;; KMP idiom, using stream itself as EOF value
52 (command (read *standard-input* nil *standard-input*)))
55 ;; currently-undocumented feature: EOF is handled as Q.
56 ;; If there's ever consensus that this is *the* right
57 ;; thing to do (as opposed to e.g. handling it as U), we
58 ;; could document it. Meanwhile, it seems more Unix-y to
59 ;; do this than to signal an error.
60 (throw 'quit-inspect nil))
62 (let ((elements-length (length elements)))
63 (cond ((< -1 command elements-length)
64 (let* ((element (nth command elements))
65 (value (if named-p (cdr element) element)))
66 (cond ((eq value *inspect-unbound-object-marker*)
67 (format s "~%That slot is unbound.~%")
68 (return-from %inspect (reread)))
71 ;; If we ever return, then we should be
72 ;; looking at *INSPECTED* again.
73 (return-from %inspect (redisplay))))))
74 ((zerop elements-length)
75 (format s "~%The object contains nothing to inspect.~%")
76 (return-from %inspect (reread)))
78 (format s "~%Enter a valid index (~:[0-~D~;0~]).~%"
79 (= elements-length 1) (1- elements-length))
80 (return-from %inspect (reread))))))
82 (case (find-symbol (symbol-name command) *keyword-package*)
84 (throw 'quit-inspect nil))
86 (return-from %inspect))
88 (return-from %inspect (redisplay)))
90 (write-string *help-for-inspect* s)
91 (return-from %inspect (reread)))
93 (eval-for-inspect command s)
94 (return-from %inspect (reread)))))
96 (eval-for-inspect command s)
97 (return-from %inspect (reread)))))))))
99 (defun eval-for-inspect (command stream)
100 (let ((result-list (restart-case (multiple-value-list (eval command))
101 (nil () :report "Return to the inspector."
102 (format stream "~%returning to the inspector~%")
103 (return-from eval-for-inspect nil)))))
104 ;; FIXME: Much of this interactive-EVAL logic is shared with
105 ;; the main REPL EVAL and with the debugger EVAL. The code should
106 ;; be shared explicitly.
107 (setf /// // // / / result-list)
108 (setf +++ ++ ++ + + - - command)
109 (setf *** ** ** * * (car /))
110 (format stream "~&~{~S~%~}" /)))
112 (defun tty-display-inspected-parts (description named-p elements stream)
113 (format stream "~%~A" description)
115 (dolist (element elements)
117 (destructuring-bind (name . value) element
118 (format stream "~W. ~A: ~W~%" index name
119 (if (eq value *inspect-unbound-object-marker*)
122 (format stream "~W. ~W~%" index element))
127 ;;; Destructure an object for inspection, returning
128 ;;; (VALUES DESCRIPTION NAMED-P ELEMENTS),
131 ;;; DESCRIPTION is a summary description of the destructured object,
132 ;;; e.g. "The object is a CONS.~%".
134 ;;; NAMED-P determines what representation is used for elements
135 ;;; of ELEMENTS. If NAMED-P is true, then each element is
136 ;;; (CONS NAME VALUE); otherwise each element is just VALUE.
138 ;;; ELEMENTS is a list of the component parts of OBJECT (whose
139 ;;; representation is determined by NAMED-P).
141 ;;; (The NAMED-P dichotomy is useful because symbols and instances
142 ;;; need to display both a slot name and a value, while lists and
143 ;;; vectors need only display a value.)
144 (defgeneric inspected-parts (object))
146 (defmethod inspected-parts ((object symbol))
147 (values (format nil "The object is a SYMBOL.~%" object)
149 (list (cons "Name" (symbol-name object))
150 (cons "Package" (symbol-package object))
151 (cons "Value" (if (boundp object)
152 (symbol-value object)
153 *inspect-unbound-object-marker*))
154 (cons "Function" (if (fboundp object)
155 (symbol-function object)
156 *inspect-unbound-object-marker*))
157 (cons "Plist" (symbol-plist object)))))
159 (defun inspected-structure-elements (object)
160 (let ((parts-list '())
161 (info (layout-info (sb-kernel:layout-of object))))
162 (when (sb-kernel::defstruct-description-p info)
163 (dolist (dd-slot (dd-slots info) (nreverse parts-list))
164 (push (cons (dsd-%name dd-slot)
165 (funcall (dsd-accessor-name dd-slot) object))
168 (defmethod inspected-parts ((object structure-object))
169 (values (format nil "The object is a STRUCTURE-OBJECT of type ~S.~%"
172 (inspected-structure-elements object)))
174 (defun inspected-standard-object-elements (object)
175 (let ((reversed-elements nil)
176 (class-slots (sb-pcl::class-slots (class-of object))))
177 (dolist (class-slot class-slots (nreverse reversed-elements))
178 (let* ((slot-name (slot-value class-slot 'sb-pcl::name))
179 (slot-value (if (slot-boundp object slot-name)
180 (slot-value object slot-name)
181 *inspect-unbound-object-marker*)))
182 (push (cons slot-name slot-value) reversed-elements)))))
184 (defmethod inspected-parts ((object standard-object))
185 (values (format nil "The object is a STANDARD-OBJECT of type ~S.~%"
188 (inspected-standard-object-elements object)))
190 (defmethod inspected-parts ((object funcallable-instance))
191 (values (format nil "The object is a FUNCALLABLE-INSTANCE of type ~S.~%"
194 (inspected-structure-elements object)))
196 (defmethod inspected-parts ((object function))
197 (let* ((type (sb-kernel:widetag-of object))
198 (object (if (= type sb-vm:closure-header-widetag)
199 (sb-kernel:%closure-fun object)
201 (values (format nil "FUNCTION ~S.~@[~%Argument List: ~A~]." object
202 (sb-kernel:%simple-fun-arglist object)
203 ;; Defined-from stuff used to be here. Someone took
204 ;; it out. FIXME: We should make it easy to get
205 ;; to DESCRIBE from the inspector.
210 (defmethod inspected-parts ((object vector))
212 "The object is a ~:[~;displaced ~]VECTOR of length ~D.~%"
213 (and (array-header-p object)
214 (%array-displaced-p object))
217 ;; FIXME: Should we respect *INSPECT-LENGTH* here? If not, what
218 ;; does *INSPECT-LENGTH* mean?
219 (coerce object 'list)))
221 (defun inspected-index-string (index rev-dimensions)
222 (if (null rev-dimensions)
225 (dolist (dim rev-dimensions)
226 (multiple-value-bind (q r) (floor index dim)
229 (format nil "[~D~{,~D~}]" (car list) (cdr list)))))
231 (defmethod inspected-parts ((object array))
232 (let* ((length (min (array-total-size object) *inspect-length*))
233 (reference-array (make-array length :displaced-to object))
234 (dimensions (array-dimensions object))
235 (reversed-elements nil))
236 ;; FIXME: Should we respect *INSPECT-LENGTH* here? If not, what does
237 ;; *INSPECT-LENGTH* mean?
239 (push (cons (format nil
241 (inspected-index-string i (reverse dimensions)))
242 (aref reference-array i))
244 (values (format nil "The object is ~:[a displaced~;an~] ARRAY of ~A.~%~
245 Its dimensions are ~S.~%"
246 (array-element-type object)
247 (and (array-header-p object)
248 (%array-displaced-p object))
251 (nreverse reversed-elements))))
253 (defmethod inspected-parts ((object cons))
254 (if (consp (cdr object))
255 (inspected-parts-of-nontrivial-list object)
256 (inspected-parts-of-simple-cons object)))
258 (defun inspected-parts-of-simple-cons (object)
259 (values "The object is a CONS.
262 (list (cons 'car (car object))
263 (cons 'cdr (cdr object)))))
265 (defun inspected-parts-of-nontrivial-list (object)
268 (reversed-elements nil))
269 (flet ((done (description-format)
270 (return-from inspected-parts-of-nontrivial-list
271 (values (format nil description-format length)
273 (nreverse reversed-elements)))))
275 (cond ((null in-list)
276 (done "The object is a proper list of length ~S.~%"))
277 ((>= length *inspect-length*)
278 (push (cons 'rest in-list) reversed-elements)
279 (done "The object is a long list (more than ~S elements).~%"))
281 (push (cons length (pop in-list)) reversed-elements)
284 (push (cons 'rest in-list) reversed-elements)
285 (done "The object is an improper list of length ~S.~%")))))))
287 (defmethod inspected-parts ((object t))
288 (values (format nil "The object is an ATOM:~% ~W~%" object) nil nil))