0.6.7.22: removed CVS dollar-Header-dollar tags from sources
[sbcl.git] / src / code / inspect.lisp
1 ;;;; the 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-INSPECT")
13
14 ;;; The inspector views LISP objects as being composed of parts. A
15 ;;; list, for example, would be divided into its members, and a
16 ;;; instance into its slots. These parts are stored in a list. The
17 ;;; first two elements of this list are for bookkeeping. The first
18 ;;; element is a preamble string that will be displayed before the
19 ;;; object. The second element is a boolean value that indicates
20 ;;; whether a label will be printed in front of a value, or just the
21 ;;; value. Symbols and instances need to display both a slot name and
22 ;;; a value, while lists, vectors, and atoms need only display a
23 ;;; value. If the second member of a parts list is t, then the third
24 ;;; and successive members must be an association list of slot names
25 ;;; and values. When the second slot is nil, the third and successive
26 ;;; slots must be the parts of an object.
27
28 ;;; *INSPECT-OBJECT-STACK* is an assoc list of objects to their parts.
29 (defvar *inspect-object-stack* ())
30
31 (defparameter *inspect-length* 10)
32
33 #-sb-fluid (declaim (inline numbered-parts-p))
34 (defun numbered-parts-p (parts)
35   (second parts))
36
37 (defconstant parts-offset 2)
38
39 (defun nth-parts (parts n)
40   (if (numbered-parts-p parts)
41       (cdr (nth (+ n parts-offset) parts))
42       (nth (+ n parts-offset) parts)))
43
44 (defun inspect (object)
45   (unwind-protect
46       (input-loop object (describe-parts object) *standard-output*)
47     (setf *inspect-object-stack* nil)))
48
49 ;;; When *ILLEGAL-OBJECT-MARKER* occurs in a parts list, it indicates that that
50 ;;; slot is unbound.
51 (defvar *illegal-object-marker* (cons nil nil))
52
53 (defun input-loop (object parts s)
54   (tty-display-object parts s)
55   (loop
56     (format s "~&> ")
57     (force-output)
58     (let ((command (read))
59           ;; Use 2 less than length because first 2 elements are bookkeeping.
60           (parts-len-2 (- (length parts) 2)))
61       (typecase command
62         (integer
63          (cond ((< -1 command parts-len-2)
64                 (cond ((eq (nth-parts parts command) *illegal-object-marker*)
65                        (format s "~%That slot is unbound.~%"))
66                       (t
67                        (push (cons object parts) *inspect-object-stack*)
68                        (setf object (nth-parts parts command))
69                        (setf parts (describe-parts object))
70                        (tty-display-object parts s))))
71                (t
72                 (if (= parts-len-2 0)
73                     (format s "~%This object contains nothing to inspect.~%~%")
74                     (format s "~%Enter a VALID number (~:[0-~D~;0~]).~%~%"
75                             (= parts-len-2 1) (1- parts-len-2))))))
76         (symbol
77          (case (find-symbol (symbol-name command) *keyword-package*)
78            ((:q :e)
79             (return object))
80            (:u
81             (cond (*inspect-object-stack*
82                    (setf object (caar *inspect-object-stack*))
83                    (setf parts (cdar *inspect-object-stack*))
84                    (pop *inspect-object-stack*)
85                    (tty-display-object parts s))
86                   (t (format s "~%Bottom of Stack.~%"))))
87            (:r
88             (setf parts (describe-parts object))
89             (tty-display-object parts s))
90            (:d
91             (tty-display-object parts s))
92            ((:h :? :help)
93             (show-help s))
94            (t
95             (do-inspect-eval command s))))
96         (t
97          (do-inspect-eval command s))))))
98
99 (defun do-inspect-eval (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 do-inspect-eval nil)))))
104     (setf /// // // / / result-list)
105     (setf +++ ++ ++ + + - - command)
106     (setf *** ** ** * * (car /))
107     (format stream "~&~{~S~%~}" /)))
108
109 (defun show-help (s)
110   (terpri)
111   (write-line "inspector help:" s)
112   (write-line "  R           -  recompute current object." s)
113   (write-line "  D           -  redisplay current object." s)
114   (write-line "  U           -  Move upward through the object stack." s)
115   (write-line "  Q, E        -  Quit inspector." s)
116   (write-line "  ?, H, Help  -  Show this help." s))
117
118 (defun tty-display-object (parts stream)
119   (format stream "~%~A" (car parts))
120   (let ((numbered-parts-p (numbered-parts-p parts))
121         (parts (cddr parts)))
122     (do ((part parts (cdr part))
123          (i 0 (1+ i)))
124         ((endp part) nil)
125       (if numbered-parts-p
126           (format stream "~D. ~A: ~A~%" i (caar part)
127                   (if (eq (cdar part) *illegal-object-marker*)
128                       "unbound"
129                       (cdar part)))
130           (format stream "~D. ~A~%" i (car part))))))
131 \f
132 ;;;; DESCRIBE-PARTS
133
134 (defun describe-parts (object)
135   (typecase object
136     (symbol (describe-symbol-parts object))
137     (instance (describe-instance-parts object :structure))
138     (function
139      (if (sb-kernel:funcallable-instance-p object)
140          (describe-instance-parts object :funcallable-instance)
141          (describe-function-parts object)))
142     (vector (describe-vector-parts object))
143     (array (describe-array-parts object))
144     (cons (describe-cons-parts object))
145     (t (describe-atomic-parts object))))
146
147 (defun describe-symbol-parts (object)
148   (list (format nil "~S is a symbol.~%" object) t
149         (cons "Value" (if (boundp object)
150                           (symbol-value object)
151                           *illegal-object-marker*))
152         (cons "Function" (if (fboundp object)
153                              (symbol-function object)
154                              *illegal-object-marker*))
155         (cons "Plist" (symbol-plist object))
156         (cons "Package" (symbol-package object))))
157
158 (defun describe-instance-parts (object kind)
159   (let ((info (layout-info (sb-kernel:layout-of object)))
160         (parts-list ()))
161     (push (format nil "~S is a ~(~A~).~%" object kind) parts-list)
162     (push t parts-list)
163     (when (sb-kernel::defstruct-description-p info)
164       (dolist (dd-slot (dd-slots info) (nreverse parts-list))
165         (push (cons (dsd-%name dd-slot)
166                     (funcall (dsd-accessor dd-slot) object))
167               parts-list)))))
168
169 (defun describe-function-parts (object)
170   (let* ((type (sb-kernel:get-type object))
171          (object (if (= type sb-vm:closure-header-type)
172                      (sb-kernel:%closure-function object)
173                      object)))
174     (list (format nil "Function ~S.~@[~%Argument List: ~A~]." object
175                   (sb-kernel:%function-arglist object)
176                   ;; Defined-from stuff used to be here. Someone took
177                   ;; it out. FIXME: We should make it easy to get
178                   ;; to DESCRIBE from the inspector.
179                   )
180           t)))
181
182 (defun describe-vector-parts (object)
183   (list* (format nil "The object is a ~:[~;displaced ~]vector of length ~D.~%"
184                  (and (sb-impl::array-header-p object)
185                       (sb-impl::%array-displaced-p object))
186                  (length object))
187          nil
188          (coerce object 'list)))
189
190 (defun describe-cons-parts (object)
191   (list* (format nil "The object is a LIST of length ~D.~%" (length object))
192          nil
193          object))
194
195 (defun index-string (index rev-dimensions)
196   (if (null rev-dimensions)
197       "[]"
198       (let ((list nil))
199         (dolist (dim rev-dimensions)
200           (multiple-value-bind (q r) (floor index dim)
201             (setq index q)
202             (push r list)))
203         (format nil "[~D~{,~D~}]" (car list) (cdr list)))))
204
205 (defun describe-array-parts (object)
206   (let* ((length (min (array-total-size object) *inspect-length*))
207          (reference-array (make-array length :displaced-to object))
208          (dimensions (array-dimensions object))
209          (parts ()))
210     (push (format nil "The object is ~:[a displaced~;an~] array of ~A.~%~
211                        Its dimensions are ~S.~%"
212                   (array-element-type object)
213                   (and (sb-impl::array-header-p object)
214                        (sb-impl::%array-displaced-p object))
215                   dimensions)
216           parts)
217     (push t parts)
218     (dotimes (i length (nreverse parts))
219       (push (cons (format nil "~A " (index-string i (reverse dimensions)))
220                   (aref reference-array i))
221             parts))))
222
223 (defun describe-atomic-parts (object)
224   (list (format nil "The object is an atom.~%") nil object))