172ae72addc9dbe6f66f8be46ed53379772595ac
[sbcl.git] / src / code / inspect.lisp
1 ;;;; the CL:INSPECT function
2
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.
11
12 (in-package "SB-IMPL") ;(SB-IMPL, not SB!IMPL, since we're built in warm load.)
13
14 (defparameter *inspect-length* 10)
15
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-"))
19
20 (defun inspect (object)
21   (catch 'quit-inspect
22     (%inspect object *standard-output*))
23   (values))
24
25 (defvar *inspected*)
26 (setf (documentation '*inspected* 'variable)
27       "the value currently being inspected in CL:INSPECT")
28
29 (defvar *help-for-inspect*
30   "
31 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.
41 ")
42
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)
48       (named-let reread ()
49         (format s "~&> ")
50         (force-output)
51         (let (;; KMP idiom, using stream itself as EOF value
52               (command (read *standard-input* nil *standard-input*)))
53           (typecase command
54             (stream ; i.e. EOF
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))
61             (integer
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)))
69                               (t
70                                (%inspect value s)
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)))
77                      (t
78                       (format s "~%Enter a valid index (~:[0-~D~;0~]).~%"
79                               (= elements-length 1) (1- elements-length))
80                       (return-from %inspect (reread))))))
81             (symbol
82              (case (find-symbol (symbol-name command) *keyword-package*)
83                ((:q :e)
84                 (throw 'quit-inspect nil))
85                (:u
86                 (return-from %inspect))
87                (:r
88                 (return-from %inspect (redisplay)))
89                ((:h :? :help)
90                 (write-string *help-for-inspect* s)
91                 (return-from %inspect (reread)))
92                (t
93                 (eval-for-inspect command s)
94                 (return-from %inspect (reread)))))
95             (t
96              (eval-for-inspect command s)
97              (return-from %inspect (reread)))))))))
98
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~%~}" /)))
111
112 (defun tty-display-inspected-parts (description named-p elements stream)
113   (format stream "~%~A" description)
114   (let ((index 0))
115     (dolist (element elements)
116       (if named-p
117           (destructuring-bind (name . value) element
118             (format stream "~W. ~A: ~W~%" index name
119                     (if (eq value *inspect-unbound-object-marker*)
120                         "unbound"
121                         value)))
122           (format stream "~W. ~W~%" index element))
123       (incf index))))
124 \f
125 ;;;; INSPECTED-PARTS
126
127 ;;; Destructure an object for inspection, returning
128 ;;;   (VALUES DESCRIPTION NAMED-P ELEMENTS),
129 ;;; where..
130 ;;;
131 ;;;   DESCRIPTION is a summary description of the destructured object,
132 ;;;   e.g. "The object is a CONS.~%".
133 ;;;
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.
137 ;;;
138 ;;;   ELEMENTS is a list of the component parts of OBJECT (whose
139 ;;;   representation is determined by NAMED-P).
140 ;;;
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))
145
146 (defmethod inspected-parts ((object symbol))
147   (values (format nil "The object is a SYMBOL.~%" object)
148           t
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)))))
158
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))
166               parts-list)))))
167
168 (defmethod inspected-parts ((object structure-object))
169   (values (format nil "The object is a STRUCTURE-OBJECT of type ~S.~%"
170                   (type-of object))
171           t
172           (inspected-structure-elements object)))
173
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)))))
183
184 (defmethod inspected-parts ((object standard-object))
185   (values (format nil "The object is a STANDARD-OBJECT of type ~S.~%"
186                   (type-of object))
187           t
188           (inspected-standard-object-elements object)))
189
190 (defmethod inspected-parts ((object funcallable-instance))
191   (values (format nil "The object is a FUNCALLABLE-INSTANCE of type ~S.~%"
192                   (type-of object))
193           t
194           (inspected-structure-elements object)))
195
196 (defmethod inspected-parts ((object function))
197   (let* ((type (sb-kernel:get-type object))
198          (object (if (= type sb-vm:closure-header-widetag)
199                      (sb-kernel:%closure-fun object)
200                      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.
206                     )
207             t
208             nil)))
209
210 (defmethod inspected-parts ((object vector))
211   (values (format nil
212                   "The object is a ~:[~;displaced ~]VECTOR of length ~D.~%"
213                   (and (array-header-p object)
214                        (%array-displaced-p object))
215                   (length object))
216           nil
217           ;; FIXME: Should we respect *INSPECT-LENGTH* here? If not, what
218           ;; does *INSPECT-LENGTH* mean?
219           (coerce object 'list)))
220
221 (defun inspected-index-string (index rev-dimensions)
222   (if (null rev-dimensions)
223       "[]"
224       (let ((list nil))
225         (dolist (dim rev-dimensions)
226           (multiple-value-bind (q r) (floor index dim)
227             (setq index q)
228             (push r list)))
229         (format nil "[~D~{,~D~}]" (car list) (cdr list)))))
230
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?
238     (dotimes (i length)
239       (push (cons (format nil
240                           "~A "
241                           (inspected-index-string i (reverse dimensions)))
242                   (aref reference-array i))
243             reversed-elements))
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))
249                     dimensions)
250             t
251             (nreverse reversed-elements))))
252
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)))
257
258 (defun inspected-parts-of-simple-cons (object)
259   (values "The object is a CONS.
260 "
261           t
262           (list (cons 'car (car object))
263                 (cons 'cdr (cdr object)))))
264
265 (defun inspected-parts-of-nontrivial-list (object)
266   (let ((length 0)
267         (in-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)
272                        t
273                        (nreverse reversed-elements)))))
274       (loop
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).~%"))
280              ((consp in-list)
281               (push (cons length (pop in-list)) reversed-elements)
282               (incf length))
283              (t
284               (push (cons 'rest in-list) reversed-elements)
285               (done "The object is an improper list of length ~S.~%")))))))
286
287 (defmethod inspected-parts ((object t))
288   (values (format nil "The object is an ATOM:~%  ~W~%" object) nil nil))