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))
54 (*suppress-print-errors*
55 (if (subtypep 'serious-condition *suppress-print-errors*)
56 *suppress-print-errors*
58 ;; Until sbcl-0.8.0.x, we did
59 ;; (FRESH-LINE STREAM)
60 ;; (PPRINT-LOGICAL-BLOCK (STREAM NIL)
62 ;; here. However, ANSI's specification of DEFUN DESCRIBE,
63 ;; DESCRIBE exists as an interface primarily to manage argument
64 ;; defaulting (including conversion of arguments T and NIL into
65 ;; stream objects) and to inhibit any return values from
67 ;; doesn't mention either FRESH-LINEing or PPRINT-LOGICAL-BLOCKing,
68 ;; and the example of typical DESCRIBE-OBJECT behavior in ANSI's
69 ;; specification of DESCRIBE-OBJECT will work poorly if we do them
70 ;; here. (The example method for DESCRIBE-OBJECT does its own
71 ;; FRESH-LINEing, which is a physical directive which works poorly
72 ;; inside a pretty-printer logical block.)
73 (handler-bind ((print-not-readable #'print-unreadably))
74 (describe-object object stream))
75 ;; We don't TERPRI here either (any more since sbcl-0.8.0.x), because
76 ;; again ANSI's specification of DESCRIBE doesn't mention it and
77 ;; ANSI's example of DESCRIBE-OBJECT does its own final TERPRI.
84 ;;;; * Each interesting class has a primary method of its own.
86 ;;;; * Output looks like
88 ;;;; object-self-string
89 ;;;; [object-type-string]
98 ;;;; * The newline policy that gets the whitespace right is for
99 ;;;; each block to both start and end with a newline.
101 (defgeneric object-self-string (x))
103 (defmethod object-self-string (x)
106 (defmethod object-self-string ((x symbol))
107 (let ((*package* (find-package :keyword)))
108 (prin1-to-string x)))
110 (defgeneric object-type-string (x))
112 (defmethod object-type-string (x)
113 (let ((type (class-name-or-class (class-of x))))
115 (string-downcase type)
116 (prin1-to-string type))))
118 (defmethod object-type-string ((x cons))
119 (if (listp (cdr x)) "list" "cons"))
121 (defmethod object-type-string ((x hash-table))
124 (defmethod object-type-string ((x condition))
127 (defmethod object-type-string ((x structure-object))
130 (defmethod object-type-string ((x standard-object))
133 (defmethod object-type-string ((x function))
135 (simple-fun "compiled function")
136 (closure "compiled closure")
138 (sb-eval:interpreted-function
139 "interpreted function")
143 "funcallable-instance")))
145 (defmethod object-type-string ((x stream))
148 (defmethod object-type-string ((x sb-gray:fundamental-stream))
151 (defmethod object-type-string ((x package))
154 (defmethod object-type-string ((x array))
155 (cond ((or (stringp x) (bit-vector-p x))
156 (format nil "~@[simple-~*~]~A"
157 (typep x 'simple-array)
159 (base-string "base-string")
163 (if (simple-vector-p x)
165 (format nil "~@[simple ~*~]~@[specialized ~*~]~:[array~;vector~]"
166 (typep x 'simple-array)
167 (neq t (array-element-type x))
170 (defmethod object-type-string ((x character))
172 (standard-char "standard-char")
173 (base-char "base-char")
176 (defun print-standard-describe-header (x stream)
177 (format stream "~&~A~% [~A]~%"
178 (object-self-string x)
179 (object-type-string x)))
181 (defgeneric describe-object (x stream))
185 (defmethod describe-object ((x t) s)
186 (print-standard-describe-header x s))
188 (defmethod describe-object ((x cons) s)
189 (print-standard-describe-header x s)
190 (describe-function x nil s))
192 (defmethod describe-object ((x function) s)
193 (print-standard-describe-header x s)
194 (describe-function nil x s))
196 (defmethod describe-object ((x class) s)
197 (print-standard-describe-header x s)
198 (describe-class nil x s)
199 (describe-instance x s))
201 (defmethod describe-object ((x sb-pcl::slot-object) s)
202 (print-standard-describe-header x s)
203 (describe-instance x s))
205 (defmethod describe-object ((x character) s)
206 (print-standard-describe-header x s)
207 (format s "~%Char-code: ~S" (char-code x))
208 (format s "~%Char-name: ~A" (char-name x)))
210 (defmethod describe-object ((x array) s)
211 (print-standard-describe-header x s)
212 (format s "~%Element-type: ~S" (array-element-type x))
214 (if (array-has-fill-pointer-p x)
215 (format s "~%Fill-pointer: ~S~%Size: ~S"
217 (array-total-size x))
218 (format s "~%Length: ~S" (length x)))
219 (format s "~%Dimensions: ~S" (array-dimensions x)))
220 (let ((*print-array* nil))
221 (unless (typep x 'simple-array)
222 (format s "~%Adjustable: ~A" (if (adjustable-array-p x) "yes" "no"))
223 (multiple-value-bind (to offset) (array-displacement x)
224 (if (format s "~%Displaced-to: ~A~%Displaced-offset: ~S"
227 (format s "~%Displaced: no"))))
228 (when (and (not (array-displacement x)) (array-header-p x))
229 (format s "~%Storage vector: ~A"
230 (prin1-to-line (array-storage-vector x))))
233 (defmethod describe-object ((x hash-table) s)
234 (print-standard-describe-header x s)
235 ;; Don't print things which are already apparent from the printed
236 ;; representation -- COUNT, TEST, and WEAKNESS
237 (format s "~%Occupancy: ~,1F" (float (/ (hash-table-count x)
238 (hash-table-size x))))
239 (format s "~%Rehash-threshold: ~S" (hash-table-rehash-threshold x))
240 (format s "~%Rehash-size: ~S" (hash-table-rehash-size x))
241 (format s "~%Size: ~S" (hash-table-size x))
242 (format s "~%Synchronized: ~A" (if (hash-table-synchronized-p x) "yes" "no"))
245 (defmethod describe-object ((symbol symbol) stream)
246 (print-standard-describe-header symbol stream)
247 ;; Describe the value cell.
248 (let* ((kind (info :variable :kind symbol))
250 (:special "a special variable")
251 (:macro "a symbol macro")
252 (:constant "a constant variable")
253 (:global "a global variable")
254 (:unknown "an undefined variable")
255 (:alien "an alien variable"))))
256 (when (or (not (eq :unknown kind)) (boundp symbol))
257 (pprint-logical-block (stream nil)
258 (format stream "~@:_~A names ~A:" symbol wot)
259 (pprint-indent :block 2 stream)
260 (when (eq (info :variable :where-from symbol) :declared)
261 (format stream "~@:_Declared type: ~S"
262 (type-specifier (info :variable :type symbol))))
263 (when (info :variable :always-bound symbol)
264 (format stream "~@:_Declared always-bound."))
267 (let ((info (info :variable :alien-info symbol)))
268 (format stream "~@:_Value: ~S" (eval symbol))
269 (format stream "~@:_Type: ~S"
270 (sb-alien-internals:unparse-alien-type
271 (sb-alien::heap-alien-info-type info)))
272 (format stream "~@:_Address: #x~8,'0X"
273 (sap-int (sb-alien::heap-alien-info-sap info)))))
275 (let ((expansion (info :variable :macro-expansion symbol)))
276 (format stream "~@:_Expansion: ~S" expansion)))
278 (format stream "~:@_Value: ~S" (symbol-value symbol)))
279 ((not (eq kind :unknown))
280 (format stream "~:@_Currently unbound.")))
281 (describe-documentation symbol 'variable stream)
284 ;; TODO: We could grovel over all packages looking for and
285 ;; reporting other phenomena, e.g. IMPORT and SHADOW, or
286 ;; availability in some package even after (SYMBOL-PACKAGE SYMBOL) has
289 ;; TODO: It might also be nice to describe (find-package symbol)
290 ;; if one exists. Maybe not all the exports, etc, but the package
292 (describe-function symbol nil stream)
293 (describe-class symbol nil stream)
296 (let* ((kind (info :type :kind symbol))
299 (or (info :type :expander symbol) t))
301 (or (info :type :translator symbol) t)))))
303 (pprint-newline :mandatory stream)
304 (pprint-logical-block (stream nil)
305 (format stream "~@:_~A names a ~@[primitive~* ~]type-specifier:"
307 (eq kind :primitive))
308 (pprint-indent :block 2 stream)
309 (describe-documentation symbol 'type stream (eq t fun))
311 (describe-lambda-list (if (eq :primitive kind)
312 (%fun-lambda-list fun)
313 (info :type :lambda-list symbol))
315 (multiple-value-bind (expansion ok)
316 (handler-case (typexpand-1 symbol)
317 (error () (values nil nil)))
319 (format stream "~@:_Expansion: ~S" expansion)))))
322 (when (or (member symbol sb-c::*policy-qualities*)
323 (assoc symbol sb-c::*policy-dependent-qualities*))
324 (pprint-logical-block (stream nil)
325 (pprint-newline :mandatory stream)
326 (pprint-indent :block 2 stream)
327 (format stream "~A names a~:[ dependent~;n~] optimization policy quality:"
329 (member symbol sb-c::*policy-qualities*))
330 (describe-documentation symbol 'optimize stream t))
333 ;; Print out properties.
334 (let ((plist (symbol-plist symbol)))
336 (pprint-logical-block (stream nil)
337 (format stream "~%Symbol-plist:")
338 (pprint-indent :block 2 stream)
339 (sb-pcl::doplist (key value) plist
340 (format stream "~@:_~A -> ~A"
341 (prin1-to-line key :columns 2 :reserve 5)
342 (prin1-to-line value :columns 2 :reserve 5))))
345 (defmethod describe-object ((package package) stream)
346 (print-standard-describe-header package stream)
347 (pprint-logical-block (stream nil)
348 (describe-documentation package t stream)
349 (flet ((humanize (list)
350 (sort (mapcar (lambda (x)
357 (describe-stuff label list stream :escape nil)))
358 (let ((implemented (humanize (package-implemented-by-list package)))
359 (implements (humanize (package-implements-list package)))
360 (nicks (humanize (package-nicknames package)))
361 (uses (humanize (package-use-list package)))
362 (used (humanize (package-used-by-list package)))
363 (shadows (humanize (package-shadowing-symbols package)))
364 (this (list (package-name package)))
366 (do-external-symbols (ext package)
368 (setf exports (humanize exports))
369 (when (package-locked-p package)
370 (format stream "~@:_Locked."))
371 (when (set-difference implemented this :test #'string=)
372 (out "Implemented-by-list" implemented))
373 (when (set-difference implements this :test #'string=)
374 (out "Implements-list" implements))
375 (out "Nicknames" nicks)
376 (out "Use-list" uses)
377 (out "Used-by-list" used)
378 (out "Shadows" shadows)
379 (out "Exports" exports)
380 (format stream "~@:_~S internal symbols."
381 (package-internal-symbol-count package))))
384 ;;;; Helpers to deal with shared functionality
386 (defun describe-class (name class stream)
387 (let* ((by-name (not class))
388 (name (if class (class-name class) name))
389 (class (if class class (find-class name nil))))
391 (let ((metaclass-name (class-name (class-of class))))
392 (pprint-logical-block (stream nil)
394 (format stream "~@:_~A names the ~(~A~) ~S:"
398 (pprint-indent :block 2 stream))
399 (describe-documentation class t stream)
400 (when (sb-mop:class-finalized-p class)
401 (describe-stuff "Class precedence-list"
402 (mapcar #'class-name-or-class (sb-mop:class-precedence-list class))
404 (describe-stuff "Direct superclasses"
405 (mapcar #'class-name-or-class (sb-mop:class-direct-superclasses class))
407 (let ((subs (mapcar #'class-name-or-class (sb-mop:class-direct-subclasses class))))
409 (describe-stuff "Direct subclasses" subs stream)
410 (format stream "~@:_No subclasses.")))
411 (unless (sb-mop:class-finalized-p class)
412 (format stream "~@:_Not yet finalized."))
413 (if (eq 'structure-class metaclass-name)
414 (let* ((dd (find-defstruct-description name))
415 (slots (dd-slots dd)))
417 (format stream "~@:_Slots:~:{~@:_ ~S~
418 ~@:_ Type: ~A ~@[~A~]~
420 (mapcar (lambda (dsd)
424 (unless (eq t (dsd-raw-type dsd))
428 (format stream "~@:_No slots.")))
429 (let ((slots (sb-mop:class-direct-slots class)))
431 (format stream "~@:_Direct slots:~:{~@:_ ~S~
433 ~@[~@:_ Allocation: ~S~]~
434 ~@[~@:_ Initargs: ~{~S~^, ~}~]~
435 ~@[~@:_ Initform: ~S~]~
436 ~@[~@:_ Readers: ~{~S~^, ~}~]~
437 ~@[~@:_ Writers: ~{~S~^, ~}~]~
438 ~@[~@:_ Documentation:~@:_ ~@<~@;~A~:>~]~}"
439 (mapcar (lambda (slotd)
440 (list (sb-mop:slot-definition-name slotd)
441 (let ((type (sb-mop:slot-definition-type slotd)))
442 (unless (eq t type) type))
443 (let ((alloc (sb-mop:slot-definition-allocation slotd)))
444 (unless (eq :instance alloc) alloc))
445 (sb-mop:slot-definition-initargs slotd)
446 (sb-mop:slot-definition-initform slotd)
447 (sb-mop:slot-definition-readers slotd)
448 (sb-mop:slot-definition-writers slotd)
449 ;; FIXME: does this get the prefix right?
450 (quiet-doc slotd t)))
452 (format stream "~@:_No direct slots."))))
453 (pprint-indent :block 0 stream)
454 (pprint-newline :mandatory stream))))))
456 (defun describe-instance (object stream)
457 (let* ((class (class-of object))
458 (slotds (sb-mop:class-slots class))
459 (max-slot-name-length 0)
462 ;; Figure out a good width for the slot-name column.
463 (flet ((adjust-slot-name-length (name)
464 (setf max-slot-name-length
465 (max max-slot-name-length (length (symbol-name name))))))
466 (dolist (slotd slotds)
467 (adjust-slot-name-length (sb-mop:slot-definition-name slotd))
468 (push slotd (getf plist (sb-mop:slot-definition-allocation slotd))))
469 (setf max-slot-name-length (min (+ max-slot-name-length 3) 30)))
471 ;; Now that we know the width, we can print.
472 (flet ((describe-slot (name value)
473 (format stream "~% ~A~VT = ~A" name max-slot-name-length
474 (prin1-to-line value))))
475 (sb-pcl::doplist (allocation slots) plist
476 (format stream "~%Slots with ~S allocation:" allocation)
477 (dolist (slotd (nreverse slots))
479 (sb-mop:slot-definition-name slotd)
480 (sb-pcl::slot-value-or-default object (sb-mop:slot-definition-name slotd))))))
482 (format stream "~@:_No slots."))
485 (defun quiet-doc (object type)
486 (handler-bind ((warning #'muffle-warning))
487 (documentation object type)))
489 (defun describe-documentation (object type stream &optional undoc newline)
490 (let ((doc (quiet-doc object type)))
492 (format stream "~@:_Documentation:~@:_")
493 (pprint-logical-block (stream nil :per-line-prefix " ")
496 (format stream "~@:_(undocumented)")))
498 (pprint-newline :mandatory stream))))
500 (defun describe-stuff (label list stream &key (escape t))
503 (format stream "~@:_~A:~@<~;~{ ~S~^,~:_~}~;~:>" label list)
504 (format stream "~@:_~A:~@<~;~{ ~A~^,~:_~}~;~:>" label list))))
506 (defun describe-lambda-list (lambda-list stream)
507 (let ((*print-circle* nil)
510 (format stream "~@:_Lambda-list: ~:A" lambda-list)))
512 (defun describe-function-source (function stream)
513 (if (compiled-function-p function)
514 (let* ((code (fun-code-header (%fun-fun function)))
515 (info (sb-kernel:%code-debug-info code)))
517 (let ((source (sb-c::debug-info-source info)))
519 (let ((namestring (sb-c::debug-source-namestring source)))
520 ;; This used to also report the times the source was created
521 ;; and compiled, but that seems more like noise than useful
522 ;; information -- but FWIW that are to be had as
523 ;; SB-C::DEBUG-SOUCE-CREATED/COMPILED.
525 (format stream "~@:_Source file: ~A" namestring))
526 ((sb-di:debug-source-form source)
527 (format stream "~@:_Source form:~@:_ ~S"
528 (sb-di:debug-source-form source)))))))))
530 (let ((source (sb-eval:interpreted-function-source-location function)))
532 (let ((namestring (sb-c:definition-source-location-namestring source)))
534 (format stream "~@:_Source file: ~A" namestring)))))))
536 (defun describe-function (name function stream)
537 (let ((name (if function (fun-name function) name)))
538 (if (not (or function (and (legal-fun-name-p name) (fboundp name))))
539 ;; Not defined, but possibly the type is declared, or we have
540 ;; compiled calls to it.
541 (when (legal-fun-name-p name)
542 (multiple-value-bind (from sure) (info :function :where-from name)
543 (when (or (eq :declared from) (and sure (eq :assumed from)))
544 (pprint-logical-block (stream nil)
545 (format stream "~%~A names an undefined function" name)
546 (pprint-indent :block 2 stream)
547 (format stream "~@:_~:(~A~) type: ~S"
549 (type-specifier (info :function :type name)))))))
551 (multiple-value-bind (fun what lambda-list ftype from inline methods)
552 (cond ((and (not function) (symbolp name) (special-operator-p name))
553 (let ((fun (symbol-function name)))
554 (values fun "a special operator" (%fun-lambda-list fun))))
555 ((and (not function) (symbolp name) (macro-function name))
556 (let ((fun (macro-function name)))
557 (values fun "a macro" (%fun-lambda-list fun))))
559 (let ((fun (or function (fdefinition name))))
560 (multiple-value-bind (ftype from)
562 (values (%fun-type function) :derived)
563 (let ((ctype (info :function :type name)))
564 (values (when ctype (type-specifier ctype))
566 ;; Ensure lazy pickup of information
568 (sb-c::maybe-update-info-for-gf name)
569 (ecase (info :function :where-from name)
570 (:declared :declared)
571 ;; This is hopefully clearer to users
572 ((:defined-method :defined) :derived))))))
573 (if (typep fun 'generic-function)
576 (sb-mop:generic-function-lambda-list fun)
580 (or (sb-mop:generic-function-methods fun)
583 (if (compiled-function-p fun)
584 "a compiled function"
585 "an interpreted function")
586 (%fun-lambda-list fun)
591 (info :function :inlinep name)
592 (info :function :inline-expansion-designator name)))))))))
593 (pprint-logical-block (stream nil)
595 (format stream "~%~A names ~A:" name what)
596 (pprint-indent :block 2 stream))
597 (describe-lambda-list lambda-list stream)
598 (when (and ftype from)
599 (format stream "~@:_~:(~A~) type: ~S" from ftype))
600 (when (eq :declared from)
601 (let ((derived-ftype (%fun-type fun)))
602 (unless (equal derived-ftype ftype)
603 (format stream "~@:_Derived type: ~S" derived-ftype))))
604 (describe-documentation name 'function stream)
606 (format stream "~@:_Inline proclamation: ~A (~:[no ~;~]inline expansion available)"
609 (awhen (info :function :info name)
610 (awhen (sb-c::decode-ir1-attributes (sb-c::fun-info-attributes it))
611 (format stream "~@:_Known attributes: ~(~{~A~^, ~}~)" it)))
613 (format stream "~@:_Method-combination: ~S"
614 (sb-pcl::method-combination-type-name
615 (sb-pcl:generic-function-method-combination fun)))
616 (cond ((eq :none methods)
617 (format stream "~@:_No methods."))
619 (pprint-newline :mandatory stream)
620 (pprint-logical-block (stream nil)
621 (format stream "Methods:")
622 (dolist (method methods)
623 (pprint-indent :block 2 stream)
624 (format stream "~@:_(~A ~{~S ~}~:S)"
626 (method-qualifiers method)
627 (sb-pcl::unparse-specializers fun (sb-mop:method-specializers method)))
628 (pprint-indent :block 4 stream)
629 (describe-documentation method t stream nil))))))
630 (describe-function-source fun stream)
633 (awhen (and (legal-fun-name-p name) (compiler-macro-function name))
634 (pprint-logical-block (stream nil)
635 (format stream "~@:_~A has a compiler-macro:" name)
636 (pprint-indent :block 2 stream)
637 (describe-documentation it t stream)
638 (describe-function-source it stream))
640 (when (and (consp name) (eq 'setf (car name)) (not (cddr name)))
641 (let* ((name2 (second name))
642 (inverse (info :setf :inverse name2))
643 (expander (info :setf :expander name2)))
645 (pprint-logical-block (stream nil)
646 (format stream "~&~A has setf-expansion: ~S"
648 (pprint-indent :block 2 stream)
649 (describe-documentation name2 'setf stream))
652 (pprint-logical-block (stream nil)
653 (format stream "~&~A has a complex setf-expansion:"
655 (pprint-indent :block 2 stream)
656 (describe-lambda-list (%fun-lambda-list expander) stream)
657 (describe-documentation name2 'setf stream t)
658 (describe-function-source expander stream))
661 (describe-function `(setf ,name) nil stream))))