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")
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.
28 ;;; *INSPECT-OBJECT-STACK* is an assoc list of objects to their parts.
29 (defvar *inspect-object-stack* ())
31 (defparameter *inspect-length* 10)
33 #-sb-fluid (declaim (inline numbered-parts-p))
34 (defun numbered-parts-p (parts)
37 (defconstant parts-offset 2)
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)))
44 (defun inspect (object)
46 (input-loop object (describe-parts object) *standard-output*)
47 (setf *inspect-object-stack* nil)))
49 ;;; When *ILLEGAL-OBJECT-MARKER* occurs in a parts list, it indicates that that
51 (defvar *illegal-object-marker* (cons nil nil))
53 (defun input-loop (object parts s)
54 (tty-display-object parts s)
58 (let ((command (read))
59 ;; Use 2 less than length because first 2 elements are bookkeeping.
60 (parts-len-2 (- (length parts) 2)))
63 (cond ((< -1 command parts-len-2)
64 (cond ((eq (nth-parts parts command) *illegal-object-marker*)
65 (format s "~%That slot is unbound.~%"))
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))))
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))))))
77 (case (find-symbol (symbol-name command) *keyword-package*)
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.~%"))))
88 (setf parts (describe-parts object))
89 (tty-display-object parts s))
91 (tty-display-object parts s))
95 (do-inspect-eval command s))))
97 (do-inspect-eval command s))))))
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~%~}" /)))
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))
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))
126 (format stream "~D. ~A: ~A~%" i (caar part)
127 (if (eq (cdar part) *illegal-object-marker*)
130 (format stream "~D. ~A~%" i (car part))))))
134 (defun describe-parts (object)
136 (symbol (describe-symbol-parts object))
137 (instance (describe-instance-parts object :structure))
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))))
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))))
158 (defun describe-instance-parts (object kind)
159 (let ((info (layout-info (sb-kernel:layout-of object)))
161 (push (format nil "~S is a ~(~A~).~%" object kind) 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))
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)
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.
182 (defun describe-vector-parts (object)
183 (list* (format nil "The object is a ~:[~;displaced ~]vector of length ~D.~%"
184 (and (array-header-p object)
185 (%array-displaced-p object))
188 (coerce object 'list)))
190 (defun describe-cons-parts (object)
191 (list* (format nil "The object is a LIST of length ~D.~%" (length object))
195 (defun index-string (index rev-dimensions)
196 (if (null rev-dimensions)
199 (dolist (dim rev-dimensions)
200 (multiple-value-bind (q r) (floor index dim)
203 (format nil "[~D~{,~D~}]" (car list) (cdr list)))))
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))
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 (array-header-p object)
214 (%array-displaced-p object))
218 (dotimes (i length (nreverse parts))
219 (push (cons (format nil "~A " (index-string i (reverse dimensions)))
220 (aref reference-array i))
223 (defun describe-atomic-parts (object)
224 (list (format nil "The object is an atom.~%") nil object))