1 ;;;; the 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-INSPECT")
17 ;;; The inspector views LISP objects as being composed of parts. A
18 ;;; list, for example, would be divided into its members, and a
19 ;;; instance into its slots. These parts are stored in a list. The
20 ;;; first two elements of this list are for bookkeeping. The first
21 ;;; element is a preamble string that will be displayed before the
22 ;;; object. The second element is a boolean value that indicates
23 ;;; whether a label will be printed in front of a value, or just the
24 ;;; value. Symbols and instances need to display both a slot name and
25 ;;; a value, while lists, vectors, and atoms need only display a
26 ;;; value. If the second member of a parts list is t, then the third
27 ;;; and successive members must be an association list of slot names
28 ;;; and values. When the second slot is nil, the third and successive
29 ;;; slots must be the parts of an object.
31 ;;; *INSPECT-OBJECT-STACK* is an assoc list of objects to their parts.
32 (defvar *inspect-object-stack* ())
34 (defparameter *inspect-length* 10)
36 #-sb-fluid (declaim (inline numbered-parts-p))
37 (defun numbered-parts-p (parts)
40 (defconstant parts-offset 2)
42 (defun nth-parts (parts n)
43 (if (numbered-parts-p parts)
44 (cdr (nth (+ n parts-offset) parts))
45 (nth (+ n parts-offset) parts)))
47 (defun inspect (object)
49 (input-loop object (describe-parts object) *standard-output*)
50 (setf *inspect-object-stack* nil)))
52 ;;; When *ILLEGAL-OBJECT-MARKER* occurs in a parts list, it indicates that that
54 (defvar *illegal-object-marker* (cons nil nil))
56 (defun input-loop (object parts s)
57 (tty-display-object parts s)
61 (let ((command (read))
62 ;; Use 2 less than length because first 2 elements are bookkeeping.
63 (parts-len-2 (- (length parts) 2)))
66 (cond ((< -1 command parts-len-2)
67 (cond ((eq (nth-parts parts command) *illegal-object-marker*)
68 (format s "~%That slot is unbound.~%"))
70 (push (cons object parts) *inspect-object-stack*)
71 (setf object (nth-parts parts command))
72 (setf parts (describe-parts object))
73 (tty-display-object parts s))))
76 (format s "~%This object contains nothing to inspect.~%~%")
77 (format s "~%Enter a VALID number (~:[0-~D~;0~]).~%~%"
78 (= parts-len-2 1) (1- parts-len-2))))))
80 (case (find-symbol (symbol-name command) *keyword-package*)
84 (cond (*inspect-object-stack*
85 (setf object (caar *inspect-object-stack*))
86 (setf parts (cdar *inspect-object-stack*))
87 (pop *inspect-object-stack*)
88 (tty-display-object parts s))
89 (t (format s "~%Bottom of Stack.~%"))))
91 (setf parts (describe-parts object))
92 (tty-display-object parts s))
94 (tty-display-object parts s))
98 (do-inspect-eval command s))))
100 (do-inspect-eval command s))))))
102 (defun do-inspect-eval (command stream)
103 (let ((result-list (restart-case (multiple-value-list (eval command))
104 (nil () :report "Return to the inspector."
105 (format stream "~%returning to the inspector~%")
106 (return-from do-inspect-eval nil)))))
107 (setf /// // // / / result-list)
108 (setf +++ ++ ++ + + - - command)
109 (setf *** ** ** * * (car /))
110 (format stream "~&~{~S~%~}" /)))
114 (write-line "inspector help:" s)
115 (write-line " R - recompute current object." s)
116 (write-line " D - redisplay current object." s)
117 (write-line " U - Move upward through the object stack." s)
118 (write-line " Q, E - Quit inspector." s)
119 (write-line " ?, H, Help - Show this help." s))
121 (defun tty-display-object (parts stream)
122 (format stream "~%~A" (car parts))
123 (let ((numbered-parts-p (numbered-parts-p parts))
124 (parts (cddr parts)))
125 (do ((part parts (cdr part))
129 (format stream "~D. ~A: ~A~%" i (caar part)
130 (if (eq (cdar part) *illegal-object-marker*)
133 (format stream "~D. ~A~%" i (car part))))))
137 (defun describe-parts (object)
139 (symbol (describe-symbol-parts object))
140 (instance (describe-instance-parts object :structure))
142 (if (sb-kernel:funcallable-instance-p object)
143 (describe-instance-parts object :funcallable-instance)
144 (describe-function-parts object)))
145 (vector (describe-vector-parts object))
146 (array (describe-array-parts object))
147 (cons (describe-cons-parts object))
148 (t (describe-atomic-parts object))))
150 (defun describe-symbol-parts (object)
151 (list (format nil "~S is a symbol.~%" object) t
152 (cons "Value" (if (boundp object)
153 (symbol-value object)
154 *illegal-object-marker*))
155 (cons "Function" (if (fboundp object)
156 (symbol-function object)
157 *illegal-object-marker*))
158 (cons "Plist" (symbol-plist object))
159 (cons "Package" (symbol-package object))))
161 (defun describe-instance-parts (object kind)
162 (let ((info (layout-info (sb-kernel:layout-of object)))
164 (push (format nil "~S is a ~(~A~).~%" object kind) parts-list)
166 (when (sb-kernel::defstruct-description-p info)
167 (dolist (dd-slot (dd-slots info) (nreverse parts-list))
168 (push (cons (dsd-%name dd-slot)
169 (funcall (dsd-accessor dd-slot) object))
172 (defun describe-function-parts (object)
173 (let* ((type (sb-kernel:get-type object))
174 (object (if (= type sb-vm:closure-header-type)
175 (sb-kernel:%closure-function object)
177 (list (format nil "Function ~S.~@[~%Argument List: ~A~]." object
178 (sb-kernel:%function-arglist object)
179 ;; Defined-from stuff used to be here. Someone took
180 ;; it out. FIXME: We should make it easy to get
181 ;; to DESCRIBE from the inspector.
185 (defun describe-vector-parts (object)
186 (list* (format nil "The object is a ~:[~;displaced ~]vector of length ~D.~%"
187 (and (sb-impl::array-header-p object)
188 (sb-impl::%array-displaced-p object))
191 (coerce object 'list)))
193 (defun describe-cons-parts (object)
194 (list* (format nil "The object is a LIST of length ~D.~%" (length object))
198 (defun index-string (index rev-dimensions)
199 (if (null rev-dimensions)
202 (dolist (dim rev-dimensions)
203 (multiple-value-bind (q r) (floor index dim)
206 (format nil "[~D~{,~D~}]" (car list) (cdr list)))))
208 (defun describe-array-parts (object)
209 (let* ((length (min (array-total-size object) *inspect-length*))
210 (reference-array (make-array length :displaced-to object))
211 (dimensions (array-dimensions object))
213 (push (format nil "The object is ~:[a displaced~;an~] array of ~A.~%~
214 Its dimensions are ~S.~%"
215 (array-element-type object)
216 (and (sb-impl::array-header-p object)
217 (sb-impl::%array-displaced-p object))
221 (dotimes (i length (nreverse parts))
222 (push (cons (format nil "~A " (index-string i (reverse dimensions)))
223 (aref reference-array i))
226 (defun describe-atomic-parts (object)
227 (list (format nil "The object is an atom.~%") nil object))