1 ;;;; the DESCRIBE system
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 ;;; SB-IMPL, not SB!IMPL, since we're built in warm load.
13 (in-package "SB-IMPL")
15 ;;;; Utils, move elsewhere.
17 (defun class-name-or-class (class)
18 (let ((name (class-name class)))
19 (if (eq class (find-class name nil))
24 (if (typep x 'generic-function)
25 (sb-pcl:generic-function-name x)
28 ;;; Prints X on a single line, limiting output length by *PRINT-RIGHT-MARGIN*
29 ;;; -- good for printing object parts, etc.
30 (defun prin1-to-line (x &key (columns 1) (reserve 0))
31 (let* ((line (write-to-string x :escape t :readably nil :lines 2 :circle t))
32 (p (position #\newline line))
33 (limit (truncate (- *print-right-margin* reserve) columns)))
34 (flet ((trunc (&optional end)
35 (let ((line-end (- limit 2)))
36 (with-output-to-string (s)
37 (write-string line s :end (if end
40 (write-string ".." s)))))
43 ((> (length line) limit)
48 (defun describe (object &optional (stream-designator *standard-output*))
50 "Print a description of OBJECT to STREAM-DESIGNATOR."
51 (let ((stream (out-synonym-of stream-designator))
52 (*print-right-margin* (or *print-right-margin* 72)))
53 ;; Until sbcl-0.8.0.x, we did
54 ;; (FRESH-LINE STREAM)
55 ;; (PPRINT-LOGICAL-BLOCK (STREAM NIL)
57 ;; here. However, ANSI's specification of DEFUN DESCRIBE,
58 ;; DESCRIBE exists as an interface primarily to manage argument
59 ;; defaulting (including conversion of arguments T and NIL into
60 ;; stream objects) and to inhibit any return values from
62 ;; doesn't mention either FRESH-LINEing or PPRINT-LOGICAL-BLOCKing,
63 ;; and the example of typical DESCRIBE-OBJECT behavior in ANSI's
64 ;; specification of DESCRIBE-OBJECT will work poorly if we do them
65 ;; here. (The example method for DESCRIBE-OBJECT does its own
66 ;; FRESH-LINEing, which is a physical directive which works poorly
67 ;; inside a pretty-printer logical block.)
68 (describe-object object stream)
69 ;; We don't TERPRI here either (any more since sbcl-0.8.0.x), because
70 ;; again ANSI's specification of DESCRIBE doesn't mention it and
71 ;; ANSI's example of DESCRIBE-OBJECT does its own final TERPRI.
78 ;;;; * Each interesting class has a primary method of its own.
80 ;;;; * Output looks like
82 ;;;; object-self-string
83 ;;;; [object-type-string]
92 ;;;; * The newline policy that gets the whitespace right is for
93 ;;;; each block to both start and end with a newline.
95 (defgeneric object-self-string (x))
97 (defmethod object-self-string (x)
100 (defmethod object-self-string ((x symbol))
101 (let ((*package* (find-package :keyword)))
102 (prin1-to-string x)))
104 (defgeneric object-type-string (x))
106 (defmethod object-type-string (x)
107 (let ((type (class-name-or-class (class-of x))))
109 (string-downcase type)
110 (prin1-to-string type))))
112 (defmethod object-type-string ((x cons))
113 (if (listp (cdr x)) "list" "cons"))
115 (defmethod object-type-string ((x hash-table))
118 (defmethod object-type-string ((x condition))
121 (defmethod object-type-string ((x structure-object))
124 (defmethod object-type-string ((x standard-object))
127 (defmethod object-type-string ((x function))
129 (simple-fun "compiled function")
130 (closure "compiled closure")
132 (sb-eval:interpreted-function
133 "interpreted function")
137 "funcallable-instance")))
139 (defmethod object-type-string ((x stream))
142 (defmethod object-type-string ((x sb-gray:fundamental-stream))
145 (defmethod object-type-string ((x package))
148 (defmethod object-type-string ((x array))
149 (cond ((or (stringp x) (bit-vector-p x))
150 (format nil "~@[simple-~*~]~A"
151 (typep x 'simple-array)
153 (base-string "base-string")
157 (if (simple-vector-p x)
159 (format nil "~@[simple ~*~]~@[specialized ~*~]~:[array~;vector~]"
160 (typep x 'simple-array)
161 (neq t (array-element-type x))
164 (defmethod object-type-string ((x character))
166 (standard-char "standard-char")
167 (base-char "base-char")
170 (defun print-standard-describe-header (x stream)
171 (format stream "~&~A~% [~A]~%"
172 (object-self-string x)
173 (object-type-string x)))
175 (defgeneric describe-object (x stream))
179 (defmethod describe-object ((x t) s)
180 (print-standard-describe-header x s))
182 (defmethod describe-object ((x cons) s)
183 (print-standard-describe-header x s)
184 (describe-function x nil s))
186 (defmethod describe-object ((x function) s)
187 (print-standard-describe-header x s)
188 (describe-function nil x s))
190 (defmethod describe-object ((x class) s)
191 (print-standard-describe-header x s)
192 (describe-class nil x s)
193 (describe-instance x s))
195 (defmethod describe-object ((x sb-pcl::slot-object) s)
196 (print-standard-describe-header x s)
197 (describe-instance x s))
199 (defmethod describe-object ((x character) s)
200 (print-standard-describe-header x s)
201 (format s "~%:_Char-code: ~S" (char-code x))
202 (format s "~%:_Char-name: ~A~%_" (char-name x)))
204 (defmethod describe-object ((x array) s)
205 (print-standard-describe-header x s)
206 (format s "~%Element-type: ~S" (array-element-type x))
208 (if (array-has-fill-pointer-p x)
209 (format s "~%Fill-pointer: ~S~%Size: ~S"
211 (array-total-size x))
212 (format s "~%Length: ~S" (length x)))
213 (format s "~%Dimensions: ~S" (array-dimensions x)))
214 (let ((*print-array* nil))
215 (unless (typep x 'simple-array)
216 (format s "~%Adjustable: ~A" (if (adjustable-array-p x) "yes" "no"))
217 (multiple-value-bind (to offset) (array-displacement x)
218 (if (format s "~%Displaced-to: ~A~%Displaced-offset: ~S"
221 (format s "~%Displaced: no"))))
222 (when (and (not (array-displacement x)) (array-header-p x))
223 (format s "~%Storage vector: ~A"
224 (prin1-to-line (array-storage-vector x))))
227 (defmethod describe-object ((x hash-table) s)
228 (print-standard-describe-header x s)
229 ;; Don't print things which are already apparent from the printed
230 ;; representation -- COUNT, TEST, and WEAKNESS
231 (format s "~%Occupancy: ~,1F" (float (/ (hash-table-count x)
232 (hash-table-size x))))
233 (format s "~%Rehash-threshold: ~S" (hash-table-rehash-threshold x))
234 (format s "~%Rehash-size: ~S" (hash-table-rehash-size x))
235 (format s "~%Size: ~S" (hash-table-size x))
236 (format s "~%Synchronized: ~A" (if (hash-table-synchronized-p x) "yes" "no"))
239 (defmethod describe-object ((symbol symbol) stream)
240 (print-standard-describe-header symbol stream)
241 ;; Describe the value cell.
242 (let* ((kind (info :variable :kind symbol))
244 (:special "a special variable")
245 (:macro "a symbol macro")
246 (:constant "a constant variable")
247 (:global "a global variable")
248 (:unknown "an undefined variable")
249 (:alien "an alien variable"))))
250 (when (or (not (eq :unknown kind)) (boundp symbol))
251 (pprint-logical-block (stream nil)
252 (format stream "~%~A names ~A:" symbol wot)
253 (pprint-indent :block 2 stream)
254 (when (eq (info :variable :where-from symbol) :declared)
255 (format stream "~@:_Declared type: ~S"
256 (type-specifier (info :variable :type symbol))))
259 (let ((info (info :variable :alien-info symbol)))
260 (format stream "~@:_Value: ~S" (eval symbol))
261 (format stream "~@:_Type: ~S"
262 (sb-alien-internals:unparse-alien-type
263 (sb-alien::heap-alien-info-type info)))
264 (format stream "~@:_Address: #x~8,'0X"
265 (sap-int (eval (sb-alien::heap-alien-info-sap-form info))))))
267 (let ((expansion (info :variable :macro-expansion symbol)))
268 (format stream "~@:_Expansion: ~S" expansion)))
270 (format stream "~:@_Value: ~S" (symbol-value symbol)))
271 ((not (eq kind :unknown))
272 (format stream "~:@_Currently unbound.")))
273 (describe-documentation symbol 'variable stream)
276 ;; TODO: We could grovel over all packages looking for and
277 ;; reporting other phenomena, e.g. IMPORT and SHADOW, or
278 ;; availability in some package even after (SYMBOL-PACKAGE SYMBOL) has
281 ;; TODO: It might also be nice to describe (find-package symbol)
282 ;; if one exists. Maybe not all the exports, etc, but the package
284 (describe-function symbol nil stream)
285 (describe-class symbol nil stream)
288 (let* ((kind (info :type :kind symbol))
291 (or (info :type :expander symbol) t))
293 (or (info :type :translator symbol) t)))))
295 (pprint-newline :mandatory stream)
296 (pprint-logical-block (stream nil)
297 (pprint-indent :block 2 stream)
298 (format stream "~A names a ~@[primitive~* ~]type-specifier:"
300 (eq kind :primitive))
301 (describe-documentation symbol 'type stream (eq t fun))
303 (describe-lambda-list (if (eq :primitive kind)
304 (%fun-lambda-list fun)
305 (info :type :lambda-list symbol))
307 (when (eq (%fun-fun fun) (%fun-fun (constant-type-expander t)))
308 (format stream "~@:_Expansion: ~S" (funcall fun (list symbol))))))
311 ;; Print out properties.
312 (let ((plist (symbol-plist symbol)))
314 (pprint-logical-block (stream nil)
315 (format stream "~%Symbol-plist:")
316 (pprint-indent :block 2 stream)
317 (sb-pcl::doplist (key value) plist
318 (format stream "~@:_~A -> ~A"
319 (prin1-to-line key :columns 2 :reserve 5)
320 (prin1-to-line value :columns 2 :reserve 5))))
323 (defmethod describe-object ((package package) stream)
324 (print-standard-describe-header package stream)
325 (pprint-logical-block (stream nil)
326 (describe-documentation package t stream)
327 (flet ((humanize (list)
328 (sort (mapcar (lambda (x)
335 (describe-stuff label list stream :escape nil)))
336 (let ((implemented (humanize (package-implemented-by-list package)))
337 (implements (humanize (package-implements-list package)))
338 (nicks (humanize (package-nicknames package)))
339 (uses (humanize (package-use-list package)))
340 (used (humanize (package-used-by-list package)))
341 (shadows (humanize (package-shadowing-symbols package)))
342 (this (list (package-name package)))
344 (do-external-symbols (ext package)
346 (setf exports (humanize exports))
347 (when (package-locked-p package)
348 (format stream "~@:_Locked."))
349 (when (set-difference implemented this :test #'string=)
350 (out "Implemented-by-list" implemented))
351 (when (set-difference implements this :test #'string=)
352 (out "Implements-list" implements))
353 (out "Nicknames" nicks)
354 (out "Use-list" uses)
355 (out "Used-by-list" used)
356 (out "Shadows" shadows)
357 (out "Exports" exports)
358 (format stream "~@:_~S internal symbols."
359 (package-internal-symbol-count package))))
362 ;;;; Helpers to deal with shared functionality
364 (defun describe-class (name class stream)
365 (let* ((by-name (not class))
366 (name (if class (class-name class) name))
367 (class (if class class (find-class name nil))))
369 (let ((metaclass-name (class-name (class-of class))))
370 (pprint-logical-block (stream nil)
372 (format stream "~%~A names the ~(~A~) ~S:"
376 (pprint-indent :block 2 stream))
377 (describe-documentation class t stream)
378 (when (sb-mop:class-finalized-p class)
379 (describe-stuff "Class precedence-list"
380 (mapcar #'class-name-or-class (sb-mop:class-precedence-list class))
382 (describe-stuff "Direct superclasses"
383 (mapcar #'class-name-or-class (sb-mop:class-direct-superclasses class))
385 (let ((subs (mapcar #'class-name-or-class (sb-mop:class-direct-subclasses class))))
387 (describe-stuff "Direct subclasses" subs stream)
388 (format stream "~@:_No subclasses.")))
389 (unless (sb-mop:class-finalized-p class)
390 (format stream "~@:_Not yet finalized."))
391 (if (eq 'structure-class metaclass-name)
392 (let* ((dd (find-defstruct-description name))
393 (slots (dd-slots dd)))
395 (format stream "~@:_Slots:~:{~@:_ ~S~
396 ~@:_ Type: ~A ~@[~A~]~
398 (mapcar (lambda (dsd)
402 (unless (eq t (dsd-raw-type dsd))
406 (format stream "~@:_No slots.")))
407 (let ((slots (sb-mop:class-direct-slots class)))
409 (format stream "~@:_Direct slots:~:{~@:_ ~S~
411 ~@[~@:_ Allocation: ~S~]~
412 ~@[~@:_ Initargs: ~{~S~^, ~}~]~
413 ~@[~@:_ Initform: ~S~]~
414 ~@[~@:_ Readers: ~{~S~^, ~}~]~
415 ~@[~@:_ Writers: ~{~S~^, ~}~]~
416 ~@[~@:_ Documentation:~@:_ ~@<~@;~A~:>~]~}"
417 (mapcar (lambda (slotd)
418 (list (sb-mop:slot-definition-name slotd)
419 (let ((type (sb-mop:slot-definition-type slotd)))
420 (unless (eq t type) type))
421 (let ((alloc (sb-mop:slot-definition-allocation slotd)))
422 (unless (eq :instance alloc) alloc))
423 (sb-mop:slot-definition-initargs slotd)
424 (sb-mop:slot-definition-initform slotd)
425 (sb-mop:slot-definition-readers slotd)
426 (sb-mop:slot-definition-writers slotd)
427 ;; FIXME: does this get the prefix right?
428 (quiet-doc slotd t)))
430 (format stream "~@:_No direct slots."))))
431 (pprint-newline :mandatory stream))))))
433 (defun describe-instance (object stream)
434 (let* ((class (class-of object))
435 (slotds (sb-mop:class-slots class))
436 (max-slot-name-length 0)
439 ;; Figure out a good width for the slot-name column.
440 (flet ((adjust-slot-name-length (name)
441 (setf max-slot-name-length
442 (max max-slot-name-length (length (symbol-name name))))))
443 (dolist (slotd slotds)
444 (adjust-slot-name-length (sb-mop:slot-definition-name slotd))
445 (push slotd (getf plist (sb-mop:slot-definition-allocation slotd))))
446 (setf max-slot-name-length (min (+ max-slot-name-length 3) 30)))
448 ;; Now that we know the width, we can print.
449 (flet ((describe-slot (name value)
450 (format stream "~% ~A~VT = ~A" name max-slot-name-length
451 (prin1-to-line value))))
452 (sb-pcl::doplist (allocation slots) plist
453 (format stream "~%Slots with ~S allocation:" allocation)
454 (dolist (slotd (nreverse slots))
456 (sb-mop:slot-definition-name slotd)
457 (sb-pcl::slot-value-or-default object (sb-mop:slot-definition-name slotd))))))
459 (format stream "~@:_No slots."))
462 (defun quiet-doc (object type)
463 (handler-bind ((warning #'muffle-warning))
464 (documentation object type)))
466 (defun describe-documentation (object type stream &optional undoc newline)
467 (let ((doc (quiet-doc object type)))
469 (format stream "~@:_Documentation:~@:_")
470 (pprint-logical-block (stream nil :per-line-prefix " ")
473 (format stream "~@:_(undocumented)")))
475 (pprint-newline :mandatory stream))))
477 (defun describe-stuff (label list stream &key (escape t))
480 (format stream "~@:_~A:~@<~;~{ ~S~^,~:_~}~;~:>" label list)
481 (format stream "~@:_~A:~@<~;~{ ~A~^,~:_~}~;~:>" label list))))
483 (defun describe-lambda-list (lambda-list stream)
484 (format stream "~@:_Lambda-list: ~:A" lambda-list))
486 (defun describe-function-source (function stream)
487 (if (compiled-function-p function)
488 (let* ((code (fun-code-header (%fun-fun function)))
489 (info (sb-kernel:%code-debug-info code)))
491 (let ((source (sb-c::debug-info-source info)))
493 (let ((namestring (sb-c::debug-source-namestring source)))
494 ;; This used to also report the times the source was created
495 ;; and compiled, but that seems more like noise than useful
496 ;; information -- but FWIW that are to be had as
497 ;; SB-C::DEBUG-SOUCE-CREATED/COMPILED.
499 (format stream "~@:_Source file: ~A" namestring))
500 ((sb-di:debug-source-form source)
501 (format stream "~@:_Source form:~@:_ ~S"
502 (sb-di:debug-source-form source)))
503 (t (bug "Don't know how to use a DEBUG-SOURCE without ~
504 a namestring or a form."))))))))
506 (let ((source (sb-eval:interpreted-function-source-location function)))
508 (let ((namestring (sb-c:definition-source-location-namestring source)))
510 (format stream "~@:_Source file: ~A" namestring)))))))
512 (defun describe-function (name function stream)
513 (let ((name (if function (fun-name function) name)))
514 (if (not (or function (and (legal-fun-name-p name) (fboundp name))))
515 ;; Not defined, but possibly the type is declared, or we have
516 ;; compiled calls to it.
517 (when (legal-fun-name-p name)
518 (multiple-value-bind (from sure) (info :function :where-from name)
519 (when (or (eq :declared from) (and sure (eq :assumed from)))
520 (pprint-logical-block (stream nil)
521 (format stream "~%~A names an undefined function" name)
522 (pprint-indent :block 2 stream)
523 (format stream "~@:_~:(~A~) type: ~S"
525 (type-specifier (info :function :type name)))))))
527 (multiple-value-bind (fun what lambda-list ftype from inline
529 (cond ((and (not function) (symbolp name) (special-operator-p name))
530 (let ((fun (symbol-function name)))
531 (values fun "a special operator" (%fun-lambda-list fun))))
532 ((and (not function) (symbolp name) (macro-function name))
533 (let ((fun (macro-function name)))
534 (values fun "a macro" (%fun-lambda-list fun))))
536 (let ((fun (or function (fdefinition name))))
537 (multiple-value-bind (ftype from)
539 (values (%fun-type function) "Derived")
540 (let ((ctype (info :function :type name)))
541 (values (when ctype (type-specifier ctype))
543 ;; Ensure lazy pickup of information
545 (sb-c::maybe-update-info-for-gf name)
546 (ecase (info :function :where-from name)
547 (:declared "Declared")
548 ;; This is hopefully clearer to users
549 ((:defined-method :defined) "Derived"))))))
550 (if (typep fun 'generic-function)
553 (sb-mop:generic-function-lambda-list fun)
557 (or (sb-mop:generic-function-methods fun)
560 (if (compiled-function-p fun)
561 "a compiled function"
562 "an interpreted function")
563 (%fun-lambda-list fun)
568 (info :function :inlinep name)
569 (info :function :inline-expansion-designator name)))))))))
570 (pprint-logical-block (stream nil)
572 (format stream "~%~A names ~A:" name what)
573 (pprint-indent :block 2 stream))
574 (describe-lambda-list lambda-list stream)
575 (when (and ftype from)
576 (format stream "~@:_~A type: ~S" from ftype))
577 (describe-documentation name 'function stream)
579 (format stream "~@:_Inline proclamation: ~A (~:[no ~;~]inline expansion available)"
583 (format stream "~@:_Method-combination: ~S"
584 (sb-pcl::method-combination-type-name
585 (sb-pcl:generic-function-method-combination fun)))
586 (cond ((eq :none methods)
587 (format stream "~@:_No methods."))
589 (pprint-newline :mandatory stream)
590 (pprint-logical-block (stream nil)
591 (format stream "Methods:")
592 (dolist (method methods)
593 (pprint-indent :block 2 stream)
594 (format stream "~@:_(~A ~{~S ~}~:S)"
596 (method-qualifiers method)
597 (sb-pcl::unparse-specializers fun (sb-mop:method-specializers method)))
598 (pprint-indent :block 4 stream)
599 (describe-documentation method t stream nil))))))
600 (describe-function-source fun stream)
603 (awhen (and (legal-fun-name-p name) (compiler-macro-function name))
604 (pprint-logical-block (stream nil)
605 (format stream "~@:_~A has a compiler-macro:" name)
606 (pprint-indent :block 2 stream)
607 (describe-documentation it t stream)
608 (describe-function-source it stream))
610 (when (and (consp name) (eq 'setf (car name)) (not (cddr name)))
611 (let* ((name2 (second name))
612 (inverse (info :setf :inverse name2))
613 (expander (info :setf :expander name2)))
615 (pprint-logical-block (stream nil)
616 (format stream "~&~A has setf-expansion: ~S"
618 (pprint-indent :block 2 stream)
619 (describe-documentation name2 'setf stream))
622 (pprint-logical-block (stream nil)
623 (format stream "~&~A has a complex setf-expansion:"
625 (pprint-indent :block 2 stream)
626 (describe-documentation name2 'setf stream t))
629 (describe-function `(setf ,name) nil stream))))