1 ;;; introspection library
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 ;;; For the avoidance of doubt, the exported interface is the supported
13 ;;; interface. Anything else is internal, though you're welcome to argue a
14 ;;; case for exporting it.
16 ;;; If you steal the code from this file to cut and paste into your
17 ;;; own project, there will be much wailing and gnashing of teeth.
18 ;;; Your teeth. If need be, we'll kick them for you. This is a
19 ;;; contrib, we're allowed to look in internals. You're an
20 ;;; application programmer, and are not.
23 ;;; 1) structs don't have within-file location info. problem for the
24 ;;; structure itself, accessors, the copier and the predicate
25 ;;; 3) error handling. Signal random errors, or handle and resignal 'our'
26 ;;; error, or return NIL?
29 (defpackage :sb-introspect
31 (:export
"ALLOCATION-INFORMATION"
33 "FUNCTION-LAMBDA-LIST"
36 "VALID-FUNCTION-NAME-P"
37 "FIND-DEFINITION-SOURCE"
38 "FIND-DEFINITION-SOURCES-BY-NAME"
40 "DEFINITION-SOURCE-PATHNAME"
41 "DEFINITION-SOURCE-FORM-PATH"
42 "DEFINITION-SOURCE-FORM-NUMBER"
43 "DEFINITION-SOURCE-CHARACTER-OFFSET"
44 "DEFINITION-SOURCE-FILE-WRITE-DATE"
45 "DEFINITION-SOURCE-PLIST"
46 "FIND-FUNCTION-CALLEES"
47 "FIND-FUNCTION-CALLERS"
54 "WHO-SPECIALIZES-DIRECTLY"
55 "WHO-SPECIALIZES-GENERALLY"))
57 (in-package :sb-introspect
)
59 ;;;; Internal interface for SBCL debug info
61 ;;; Here are some tutorial-style type definitions to help understand
62 ;;; the internal SBCL debugging data structures we're using. The
63 ;;; commentary is based on CMUCL's debug internals manual.
65 (deftype debug-info
()
66 "Structure containing all the debug information related to a function.
67 Function objects reference debug-infos which in turn reference
68 debug-sources and so on."
69 'sb-c
::compiled-debug-info
)
71 (deftype debug-source
()
72 "Debug sources describe where to find source code.
73 For example, the debug source for a function compiled from a file will
74 include the pathname of the file and the position of the definition."
77 (deftype debug-function
()
78 "Debug function represent static compile-time information about a function."
79 'sb-c
::compiled-debug-fun
)
81 (declaim (ftype (function (function) debug-info
) function-debug-info
))
82 (defun function-debug-info (function)
83 (let* ((function-object (sb-kernel::%fun-fun function
))
84 (function-header (sb-kernel:fun-code-header function-object
)))
85 (sb-kernel:%code-debug-info function-header
)))
87 (declaim (ftype (function (function) debug-source
) function-debug-source
))
88 (defun function-debug-source (function)
89 (debug-info-source (function-debug-info function
)))
91 (declaim (ftype (function (debug-info) debug-source
) debug-info-source
))
92 (defun debug-info-source (debug-info)
93 (sb-c::debug-info-source debug-info
))
95 (declaim (ftype (function (t debug-info
) debug-function
) debug-info-debug-function
))
96 (defun debug-info-debug-function (function debug-info
)
97 (let ((map (sb-c::compiled-debug-info-fun-map debug-info
))
98 (name (sb-kernel:%simple-fun-name
(sb-kernel:%fun-fun function
))))
103 (sb-c::compiled-debug-fun-p x
)
104 (eq (sb-c::compiled-debug-fun-name x
) name
)))
108 (defun valid-function-name-p (name)
109 "True if NAME denotes a valid function name, ie. one that can be passed to
111 (and (sb-int:valid-function-name-p name
) t
))
113 ;;;; Utilities for code
115 (declaim (inline map-code-constants
))
116 (defun map-code-constants (code fn
)
117 "Call FN for each constant in CODE's constant pool."
118 (check-type code sb-kernel
:code-component
)
119 (loop for i from sb-vm
:code-constants-offset below
120 (sb-kernel:get-header-data code
)
121 do
(funcall fn
(sb-kernel:code-header-ref code i
))))
123 (declaim (inline map-allocated-code-components
))
124 (defun map-allocated-code-components (spaces fn
)
125 "Call FN for each allocated code component in one of SPACES. FN
126 receives the object and its size as arguments. SPACES should be a
127 list of the symbols :dynamic, :static, or :read-only."
128 (dolist (space spaces
)
129 (sb-vm::map-allocated-objects
130 (lambda (obj header size
)
131 (when (= sb-vm
:code-header-widetag header
)
132 (funcall fn obj size
)))
135 (declaim (inline map-caller-code-components
))
136 (defun map-caller-code-components (function spaces fn
)
137 "Call FN for each code component with a fdefn for FUNCTION in its
139 (let ((function (coerce function
'function
)))
140 (map-allocated-code-components
143 (declare (ignore size
))
147 (when (and (sb-kernel:fdefn-p constant
)
148 (eq (sb-kernel:fdefn-fun constant
)
150 (funcall fn obj
))))))))
152 ;;;; Finding definitions
154 (defstruct definition-source
155 ;; Pathname of the source file that the definition was compiled from.
156 ;; This is null if the definition was not compiled from a file.
157 (pathname nil
:type
(or null pathname
))
158 ;; Source-path of the definition within the file.
159 ;; This may be incomplete depending on the debug level at which the
160 ;; source was compiled.
161 (form-path '() :type list
)
162 ;; Depth first number of the form.
163 ;; FORM-PATH above usually contains just the top-level form number,
164 ;; ideally the proper form path could be dervied from the
165 ;; form-number and the tlf-number, but it's a bit complicated and
166 ;; Slime already knows how to deal with form numbers, so delegate
167 ;; that job to Slime.
168 (form-number nil
:type
(or null unsigned-byte
))
169 ;; Character offset of the top-level-form containing the definition.
170 ;; This corresponds to the first element of form-path.
171 (character-offset nil
:type
(or null unsigned-byte
))
172 ;; File-write-date of the source file when compiled.
173 ;; Null if not compiled from a file.
174 (file-write-date nil
:type
(or null unsigned-byte
))
175 ;; plist from WITH-COMPILATION-UNIT
177 ;; Any extra metadata that the caller might be interested in. For
178 ;; example the specializers of the method whose definition-source this
180 (description nil
:type list
))
182 (defun vop-sources-from-fun-templates (name)
183 (let ((fun-info (sb-int:info
:function
:info name
)))
185 (loop for vop in
(sb-c::fun-info-templates fun-info
)
186 for source
= (find-definition-source
187 (sb-c::vop-info-generator-function vop
))
188 do
(setf (definition-source-description source
)
189 (if (sb-c::template-note vop
)
190 (list (sb-c::template-name vop
)
191 (sb-c::template-note vop
))
192 (list (sb-c::template-name vop
))))
195 (defun find-vop-source (name)
196 (let* ((templates (vop-sources-from-fun-templates name
))
197 (vop (gethash name sb-c
::*backend-template-names
*))
199 (sb-c::vop-info-generator-function vop
)))
200 (source (when generator
201 (find-definition-source generator
))))
204 (setf (definition-source-description source
)
206 (cons source templates
))
210 (defun find-definition-sources-by-name (name type
)
211 "Returns a list of DEFINITION-SOURCEs for the objects of type TYPE
212 defined with name NAME. NAME may be a symbol or a extended function
213 name. Type can currently be one of the following:
241 If an unsupported TYPE is requested, the function will return NIL.
243 (flet ((get-class (name)
245 (find-class name nil
)))
246 (real-fdefinition (name)
247 ;; for getting the real function object, even if the
248 ;; function is being profiled
249 (let ((profile-info (gethash name sb-profile
::*profiled-fun-name-
>info
*)))
251 (sb-profile::profile-info-encapsulated-fun profile-info
)
252 (fdefinition name
)))))
256 (when (and (symbolp name
)
257 (member (sb-int:info
:variable
:kind name
)
258 '(:global
:special
:alien
)))
259 (translate-source-location (sb-int:info
:source-location type name
))))
261 (when (and (symbolp name
)
262 (eq (sb-int:info
:variable
:kind name
) :constant
))
263 (translate-source-location (sb-int:info
:source-location type name
))))
265 (when (and (symbolp name
)
266 (eq (sb-int:info
:variable
:kind name
) :macro
))
267 (translate-source-location (sb-int:info
:source-location type name
))))
269 (when (and (symbolp name
)
270 (macro-function name
))
271 (find-definition-source (macro-function name
))))
273 (when (compiler-macro-function name
)
274 (find-definition-source (compiler-macro-function name
))))
276 (let ((converter (sb-int:info
:function
:ir1-convert name
)))
278 (find-definition-source converter
))))
279 ((:function
:generic-function
)
280 (when (and (fboundp name
)
283 (not (macro-function name
))
284 (not (special-operator-p name
)))))
285 (let ((fun (real-fdefinition name
)))
286 (when (eq (not (typep fun
'generic-function
))
287 (not (eq type
:generic-function
)))
288 (find-definition-source fun
)))))
290 ;; Source locations for types are saved separately when the expander
291 ;; is a closure without a good source-location.
292 (let ((loc (sb-int:info
:type
:source-location name
)))
294 (translate-source-location loc
)
295 (let ((expander-fun (sb-int:info
:type
:expander name
)))
296 (when (functionp expander-fun
)
297 (find-definition-source expander-fun
))))))
300 (let ((fun (real-fdefinition name
)))
301 (when (typep fun
'generic-function
)
302 (loop for method in
(sb-mop::generic-function-methods
304 for source
= (find-definition-source method
)
305 when source collect source
)))))
307 (when (and (consp name
)
308 (eq (car name
) 'setf
))
309 (setf name
(cadr name
)))
310 (let ((expander (or (sb-int:info
:setf
:inverse name
)
311 (sb-int:info
:setf
:expander name
))))
313 (find-definition-source
314 (cond ((symbolp expander
) (symbol-function expander
))
315 ((listp expander
) (cdr expander
))
318 (let ((class (get-class name
)))
320 (when (typep class
'sb-pcl
::structure-class
)
321 (find-definition-source class
))
322 (when (sb-int:info
:typed-structure
:info name
)
323 (translate-source-location
324 (sb-int:info
:source-location
:typed-structure name
))))))
326 (let ((class (get-class name
)))
328 (not (typep class
'sb-pcl
::structure-class
)))
329 (when (eq (not (typep class
'sb-pcl
::condition-class
))
330 (not (eq type
:condition
)))
331 (find-definition-source class
)))))
332 ((:method-combination
)
333 (let ((combination-fun
334 (find-method #'sb-mop
:find-method-combination
336 (list (find-class 'generic-function
)
340 (when combination-fun
341 (find-definition-source combination-fun
))))
344 (let ((package (find-package name
)))
346 (find-definition-source package
)))))
347 ;; TRANSFORM and OPTIMIZER handling from swank-sbcl
350 (let ((fun-info (sb-int:info
:function
:info name
)))
352 (loop for xform in
(sb-c::fun-info-transforms fun-info
)
353 for source
= (find-definition-source
354 (sb-c::transform-function xform
))
355 for typespec
= (sb-kernel:type-specifier
356 (sb-c::transform-type xform
))
357 for note
= (sb-c::transform-note xform
)
358 do
(setf (definition-source-description source
)
360 (list (second typespec
) note
)
364 (let ((fun-info (and (symbolp name
)
365 (sb-int:info
:function
:info name
))))
367 (let ((otypes '((sb-c:fun-info-derive-type . sb-c
:derive-type
)
368 (sb-c:fun-info-ltn-annotate . sb-c
:ltn-annotate
)
369 (sb-c:fun-info-optimizer . sb-c
:optimizer
)
370 (sb-c:fun-info-ir2-convert . sb-c
:ir2-convert
)
371 (sb-c::fun-info-stack-allocate-result
372 . sb-c
::stack-allocate-result
))))
373 (loop for
(reader . name
) in otypes
374 for fn
= (funcall reader fun-info
)
376 (let ((source (find-definition-source fn
)))
377 (setf (definition-source-description source
)
381 (let ((loc (sb-int:info
:source-location type name
)))
383 (translate-source-location loc
)
384 (find-vop-source name
))))
386 (let ((loc (sb-int:info
:source-location type name
)))
388 (translate-source-location loc
))))
390 (let* ((transform-fun
391 (or (sb-int:info
:function
:source-transform name
)
392 (and (typep name
'(cons (eql setf
) (cons symbol null
)))
393 (sb-int:info
:function
:source-transform
395 ;; A cons for the :source-transform is essentially the same
396 ;; info that was formerly in :structure-accessor.
397 (accessor (and (consp transform-fun
) (cdr transform-fun
))))
398 ;; Structure accessors have source transforms, but the
399 ;; returned locations will neither show the actual place
400 ;; where it's defined, nor is really interesting.
401 (when (and transform-fun
403 (find-definition-source transform-fun
))))
405 (let ((locations (sb-int:info
:source-location
:declaration name
)))
406 (loop for
(kind loc
) on locations by
#'cddr
408 collect
(let ((loc (translate-source-location loc
)))
409 (setf (definition-source-description loc
)
410 ;; Copy list to ensure that user code
411 ;; cannot mutate the original.
412 (copy-list (sb-int:ensure-list kind
)))
417 (defun find-definition-source (object)
419 ((or sb-pcl
::condition-class sb-pcl
::structure-class
)
420 (let ((classoid (sb-impl::find-classoid
(class-name object
))))
422 (let ((layout (sb-impl::classoid-layout classoid
)))
424 (translate-source-location
425 (sb-kernel::layout-source-location layout
)))))))
428 (find-definition-sources-by-name
429 (sb-pcl::method-combination-type-name object
) :method-combination
)))
431 (translate-source-location (sb-impl::package-source-location object
)))
432 ((or class sb-mop
:slot-definition
)
433 (translate-source-location (sb-pcl::definition-source object
)))
434 ;; Use the PCL definition location information instead of the function
435 ;; debug-info for methods and generic functions. Sometimes the
436 ;; debug-info would point into PCL internals instead of the proper
439 (let ((source (translate-source-location
440 (sb-pcl::definition-source object
))))
442 (setf (definition-source-description source
)
443 (list (sb-mop:generic-function-lambda-list object
))))
446 (let ((source (translate-source-location
447 (sb-pcl::definition-source object
))))
449 (setf (definition-source-description source
)
450 (append (method-qualifiers object
)
451 (if (sb-mop:method-generic-function object
)
452 (sb-pcl::unparse-specializers
453 (sb-mop:method-generic-function object
)
454 (sb-mop:method-specializers object
))
455 (sb-mop:method-specializers object
)))))
458 (sb-eval:interpreted-function
459 (let ((source (translate-source-location
460 (sb-eval:interpreted-function-source-location object
))))
463 (sb-interpreter:interpreted-function
464 (translate-source-location (sb-interpreter:fun-source-location object
)))
466 (find-function-definition-source object
))
467 ((or condition standard-object structure-object
)
468 (find-definition-source (class-of object
)))
470 (error "Don't know how to retrieve source location for a ~S"
473 (defun find-function-definition-source (function)
474 (let* ((debug-info (function-debug-info function
))
475 (debug-source (debug-info-source debug-info
))
476 (debug-fun (debug-info-debug-function function debug-info
))
477 (tlf (if debug-fun
(sb-c::compiled-debug-fun-tlf-number debug-fun
))))
478 (make-definition-source
480 (when (stringp (sb-c::debug-source-namestring debug-source
))
481 (parse-namestring (sb-c::debug-source-namestring debug-source
)))
484 (elt (sb-c::debug-source-start-positions debug-source
) tlf
))
485 :form-path
(if tlf
(list tlf
))
486 :form-number
(sb-c::compiled-debug-fun-form-number debug-fun
)
487 :file-write-date
(sb-c::debug-source-created debug-source
)
488 :plist
(sb-c::debug-source-plist debug-source
))))
490 (defun translate-source-location (location)
492 (make-definition-source
493 :pathname
(let ((n (sb-c:definition-source-location-namestring location
)))
495 (parse-namestring n
)))
497 (let ((number (sb-c:definition-source-location-toplevel-form-number
501 :form-number
(sb-c:definition-source-location-form-number
503 :plist
(sb-c:definition-source-location-plist location
))
504 (make-definition-source)))
506 (sb-int:define-deprecated-function
:late
"1.0.24.5" function-arglist function-lambda-list
508 (function-lambda-list function
))
510 (defun function-lambda-list (function)
511 "Describe the lambda list for the extended function designator FUNCTION.
512 Works for special-operators, macros, simple functions, interpreted functions,
513 and generic functions. Signals an error if FUNCTION is not a valid extended
514 function designator."
515 (cond ((and (symbolp function
) (special-operator-p function
))
516 (function-lambda-list (sb-int:info
:function
:ir1-convert function
)))
517 ((valid-function-name-p function
)
518 (function-lambda-list (or (and (symbolp function
)
519 (macro-function function
))
520 (fdefinition function
))))
521 ((typep function
'generic-function
)
522 (sb-pcl::generic-function-pretty-arglist function
))
524 (sb-kernel:%fun-lambda-list function
))))
526 (defun deftype-lambda-list (typespec-operator)
527 "Returns the lambda list of TYPESPEC-OPERATOR as first return
528 value, and a flag whether the arglist could be found as second
530 (check-type typespec-operator symbol
)
531 ;; Don't return a lambda-list for combinators AND,OR,NOT.
532 (let* ((f (and (sb-int:info
:type
:kind typespec-operator
)
533 (sb-int:info
:type
:expander typespec-operator
)))
534 (f (if (listp f
) (car f
) f
)))
536 (values (sb-kernel:%fun-lambda-list f
) t
)
539 (defun function-type (function-designator)
540 "Returns the ftype of FUNCTION-DESIGNATOR, or NIL."
541 (flet ((ftype-of (function-designator)
542 (sb-kernel:type-specifier
543 (sb-int:proclaimed-ftype function-designator
))))
544 (etypecase function-designator
546 (when (and (fboundp function-designator
)
547 (not (macro-function function-designator
))
548 (not (special-operator-p function-designator
)))
549 (ftype-of function-designator
)))
551 (when (and (sb-int:legal-fun-name-p function-designator
)
552 (fboundp function-designator
))
553 (ftype-of function-designator
)))
555 (function-type (sb-pcl:generic-function-name function-designator
)))
557 ;; Give declared type in globaldb priority over derived type
558 ;; because it contains more accurate information e.g. for
560 (let ((type (function-type (sb-kernel:%fun-name
561 (sb-impl::%fun-fun function-designator
)))))
564 (sb-impl::%fun-type function-designator
)))))))
566 ;;;; find callers/callees, liberated from Helmut Eller's code in SLIME
568 ;;; This interface is tremendously experimental.
570 ;;; For the moment I'm taking the view that FDEFN is an internal
571 ;;; object (one out of one CMUCL developer surveyed didn't know what
572 ;;; they were for), so these routines deal in FUNCTIONs
574 ;;; Find callers and callees by looking at the constant pool of
575 ;;; compiled code objects. We assume every fdefn object in the
576 ;;; constant pool corresponds to a call to that function. A better
577 ;;; strategy would be to use the disassembler to find actual
580 (defun find-function-callees (function)
581 "Return functions called by FUNCTION."
582 (declare (sb-kernel:simple-fun function
))
585 (sb-kernel:fun-code-header function
)
587 (when (sb-kernel:fdefn-p obj
)
588 (push (sb-kernel:fdefn-fun obj
)
592 (defun find-function-callers (function &optional
(spaces '(:read-only
:static
594 "Return functions which call FUNCTION, by searching SPACES for code objects"
595 (let ((referrers '()))
596 (map-caller-code-components
600 (let ((entry (sb-kernel:%code-entry-points code
)))
602 (push (princ-to-string code
) referrers
))
604 (loop for e
= entry then
(sb-kernel::%simple-fun-next e
)
606 do
(pushnew e referrers
)))))))
611 (defun get-simple-fun (functoid)
614 (get-simple-fun (sb-kernel:fdefn-fun functoid
)))
615 ((or null sb-kernel
:funcallable-instance
)
618 ;; FIXME: should use ENCAPSULATION-INFO instead of hardwiring an index.
619 (let ((fun (sb-kernel:%closure-fun functoid
)))
620 (if (and (eq (sb-kernel:%fun-name fun
) 'sb-impl
::encapsulation
)
621 (plusp (sb-kernel:get-closure-length functoid
))
622 (typep (sb-kernel:%closure-index-ref functoid
0) 'sb-impl
::encapsulation-info
))
624 (sb-impl::encapsulation-info-definition
625 (sb-kernel:%closure-index-ref functoid
0)))
628 (sb-kernel:%fun-fun functoid
))))
630 ;; Call FUNCTION with two args, NAME and VALUE, for each value that is
631 ;; either the FDEFINITION or MACRO-FUNCTION of some global name.
633 (defun call-with-each-global-functoid (function)
634 (sb-c::call-with-each-globaldb-name
636 ;; In general it might be unsafe to call INFO with a NAME that is not
637 ;; valid for the kind of info being retrieved, as when the defaulting
638 ;; function tries to perform a sanity-check. But here it's safe.
639 (let ((functoid (or (sb-int:info
:function
:macro-function name
)
640 (sb-int:info
:function
:definition name
))))
642 (funcall function name functoid
))))))
644 (defun collect-xref (kind-index wanted-name
)
646 (call-with-each-global-functoid
647 (lambda (info-name value
)
648 ;; Get a simple-fun for the definition, and an xref array
649 ;; from the table if available.
650 (let* ((simple-fun (get-simple-fun value
))
651 (xrefs (when simple-fun
652 (sb-kernel:%simple-fun-xrefs simple-fun
)))
654 (aref xrefs kind-index
))))
655 ;; Loop through the name/path xref entries in the table
656 (loop for i from
0 below
(length array
) by
2
657 for xref-name
= (aref array i
)
658 for xref-path
= (aref array
(1+ i
))
659 do
(when (equal xref-name wanted-name
)
660 (let ((source-location
661 (find-function-definition-source simple-fun
)))
662 ;; Use the more accurate source path from
664 (setf (definition-source-form-path source-location
)
666 (push (cons info-name source-location
)
670 (defun who-calls (function-name)
671 "Use the xref facility to search for source locations where the
672 global function named FUNCTION-NAME is called. Returns a list of
673 function name, definition-source pairs."
674 (collect-xref #.
(position :calls sb-c
::*xref-kinds
*) function-name
))
676 (defun who-binds (symbol)
677 "Use the xref facility to search for source locations where the
678 special variable SYMBOL is rebound. Returns a list of function name,
679 definition-source pairs."
680 (collect-xref #.
(position :binds sb-c
::*xref-kinds
*) symbol
))
682 (defun who-references (symbol)
683 "Use the xref facility to search for source locations where the
684 special variable or constant SYMBOL is read. Returns a list of function
685 name, definition-source pairs."
686 (collect-xref #.
(position :references sb-c
::*xref-kinds
*) symbol
))
688 (defun who-sets (symbol)
689 "Use the xref facility to search for source locations where the
690 special variable SYMBOL is written to. Returns a list of function name,
691 definition-source pairs."
692 (collect-xref #.
(position :sets sb-c
::*xref-kinds
*) symbol
))
694 (defun who-macroexpands (macro-name)
695 "Use the xref facility to search for source locations where the
696 macro MACRO-NAME is expanded. Returns a list of function name,
697 definition-source pairs."
698 (collect-xref #.
(position :macroexpands sb-c
::*xref-kinds
*) macro-name
))
700 (defun who-specializes-directly (class-designator)
701 "Search for source locations of methods directly specializing on
702 CLASS-DESIGNATOR. Returns an alist of method name, definition-source
705 A method matches the criterion either if it specializes on the same
706 class as CLASS-DESIGNATOR designates (this includes CLASS-EQ
707 specializers), or if it eql-specializes on an instance of the
712 (let ((class (canonicalize-class-designator class-designator
)))
714 (return-from who-specializes-directly nil
))
715 (let ((result (collect-specializing-methods
717 ;; Does SPECL specialize on CLASS directly?
719 (sb-pcl::class-eq-specializer
720 (eq (sb-pcl::specializer-object specl
) class
))
721 (sb-pcl::eql-specializer
722 (let ((obj (sb-mop:eql-specializer-object specl
)))
723 (eq (class-of obj
) class
)))
724 ((not sb-pcl
::standard-specializer
)
727 (eq specl class
)))))))
728 (map-into result
#'(lambda (m)
729 (cons `(method ,(method-generic-function-name m
))
730 (find-definition-source m
)))
733 (defun who-specializes-generally (class-designator)
734 "Search for source locations of methods specializing on
735 CLASS-DESIGNATOR, or a subclass of it. Returns an alist of method
736 name, definition-source pairs.
738 A method matches the criterion either if it specializes on the
739 designated class itself or a subclass of it (this includes CLASS-EQ
740 specializers), or if it eql-specializes on an instance of the
741 designated class or a subclass of it.
745 (let ((class (canonicalize-class-designator class-designator
)))
747 (return-from who-specializes-generally nil
))
748 (let ((result (collect-specializing-methods
750 ;; Does SPECL specialize on CLASS or a subclass
753 (sb-pcl::class-eq-specializer
754 (subtypep (sb-pcl::specializer-object specl
) class
))
755 (sb-pcl::eql-specializer
756 (typep (sb-mop:eql-specializer-object specl
) class
))
757 ((not sb-pcl
::standard-specializer
)
760 (subtypep specl class
)))))))
761 (map-into result
#'(lambda (m)
762 (cons `(method ,(method-generic-function-name m
))
763 (find-definition-source m
)))
766 (defun canonicalize-class-designator (class-designator)
767 (typecase class-designator
768 (symbol (find-class class-designator nil
))
769 (class class-designator
)
772 (defun method-generic-function-name (method)
773 (sb-mop:generic-function-name
(sb-mop:method-generic-function method
)))
775 (defun collect-specializing-methods (predicate)
777 (sb-pcl::map-specializers
779 (when (funcall predicate specl
)
780 (let ((methods (sb-mop:specializer-direct-methods specl
)))
781 (setf result
(append methods result
))))))
782 (delete-duplicates result
)))
785 ;;;; ALLOCATION INTROSPECTION
787 (defun allocation-information (object)
789 "Returns information about the allocation of OBJECT. Primary return value
790 indicates the general type of allocation: :IMMEDIATE, :HEAP, :STACK,
793 Possible secondary return value provides additional information about the
796 For :HEAP objects the secondary value is a plist:
799 Indicates the heap segment the object is allocated in.
802 Is the current generation of the object: 0 for nursery, 6 for pseudo-static
803 generation loaded from core. (GENCGC and :SPACE :DYNAMIC only.)
806 Indicates a \"large\" object subject to non-copying
807 promotion. (GENCGC and :SPACE :DYNAMIC only.)
810 Indicates that the object is allocated in a boxed region. Unboxed
811 allocation is used for eg. specialized arrays after they have survived one
812 collection. (GENCGC and :SPACE :DYNAMIC only.)
815 Indicates that the page(s) on which the object resides are kept live due
816 to conservative references. Note that object may reside on a pinned page
817 even if :PINNED in NIL if the GC has not had the need to mark the the page
818 as pinned. (GENCGC and :SPACE :DYNAMIC only.)
821 Indicates that the page on which the object starts is write-protected,
822 which indicates for :BOXED objects that it hasn't been written to since
823 the last GC of its generation. (GENCGC and :SPACE :DYNAMIC only.)
826 The index of the page the object resides on. (GENGC and :SPACE :DYNAMIC
829 For :STACK objects secondary value is the thread on whose stack the object is
832 Expected use-cases include introspection to gain insight into allocation and
833 GC behaviour and restricting memoization to heap-allocated arguments.
835 Experimental: interface subject to change."
836 ;; FIXME: Would be nice to provide the size of the object as well, though
837 ;; maybe that should be a separate function, and something like MAP-PARTS
838 ;; for mapping over parts of arbitrary objects so users can get "deep sizes"
839 ;; as well if they want to.
841 ;; FIXME: For the memoization use-case possibly we should also provide a
842 ;; simpler HEAP-ALLOCATED-P, since that doesn't require disabling the GC
843 ;; scanning threads for negative answers? Similarly, STACK-ALLOCATED-P for
844 ;; checking if an object has been stack-allocated by a given thread for
845 ;; testing purposes might not come amiss.
846 (if (typep object
'(or fixnum character
847 #.
(if (= sb-vm
:n-word-bits
64) 'single-float
(values))))
848 (values :immediate nil
)
850 (sb-sys:without-gcing
851 ;; Disable GC so the object cannot move to another page while
852 ;; we have the address.
853 (let* ((addr (sb-kernel:get-lisp-obj-address object
))
855 (cond ((< sb-vm
:read-only-space-start addr
856 (ash sb-vm
:*read-only-space-free-pointer
*
857 sb-vm
:n-fixnum-tag-bits
))
859 ((< sb-vm
:static-space-start addr
860 (ash sb-vm
:*static-space-free-pointer
*
861 sb-vm
:n-fixnum-tag-bits
))
863 ((< (sb-kernel:current-dynamic-space-start
) addr
864 (sb-sys:sap-int
(sb-kernel:dynamic-space-free-pointer
)))
868 (if (eq :dynamic space
)
869 (let ((index (sb-vm::find-page-index addr
)))
870 (symbol-macrolet ((page (sb-alien:deref sb-vm
::page-table index
)))
871 (let ((flags (sb-alien:slot page
'sb-vm
::flags
)))
873 :generation
(sb-alien:slot page
'sb-vm
::gen
)
874 :write-protected
(logbitp 0 flags
)
875 :boxed
(logbitp 2 flags
)
876 :pinned
(logbitp 5 flags
)
877 :large
(logbitp 6 flags
)
881 (list :space space
))))))
883 (values :heap plist
))
885 (let ((sap (sb-sys:int-sap
(sb-kernel:get-lisp-obj-address object
))))
886 ;; FIXME: Check other stacks as well.
888 (dolist (thread (sb-thread:list-all-threads
))
889 (let ((c-start (sb-di::descriptor-sap
890 (sb-thread::%symbol-value-in-thread
891 'sb-vm
:*control-stack-start
*
893 (c-end (sb-di::descriptor-sap
894 (sb-thread::%symbol-value-in-thread
895 'sb-vm
:*control-stack-end
*
897 (when (and c-start c-end
)
898 (when (and (sb-sys:sap
<= c-start sap
)
899 (sb-sys:sap
< sap c-end
))
900 (return-from allocation-information
901 (values :stack thread
))))))
903 (when (sb-vm:control-stack-pointer-valid-p sap nil
)
904 (return-from allocation-information
905 (values :stack sb-thread
::*current-thread
*))))
908 (defun map-root (function object
&key simple
(ext t
))
909 "Call FUNCTION with all non-immediate objects pointed to by OBJECT.
912 If SIMPLE is true (default is NIL), elides those pointers that are not
913 notionally part of certain built-in objects, but backpointers to a
914 conceptual parent: eg. elides the pointer from a SYMBOL to the
915 corresponding PACKAGE.
917 If EXT is true (default is T), includes some pointers that are not
918 actually contained in the object, but found in certain well-known
919 indirect containers: FDEFINITIONs, EQL specializers, classes, and
920 thread-local symbol values in other threads fall into this category.
922 NOTE: calling MAP-ROOT with a THREAD does not currently map over
923 conservative roots from the thread registers and interrupt contexts.
925 Experimental: interface subject to change."
926 (let ((fun (coerce function
'function
))
927 (seen (sb-int:alloc-xset
)))
929 (when (and (member (sb-kernel:lowtag-of part
)
930 `(,sb-vm
:instance-pointer-lowtag
931 ,sb-vm
:list-pointer-lowtag
932 ,sb-vm
:fun-pointer-lowtag
933 ,sb-vm
:other-pointer-lowtag
))
934 (not (sb-int:xset-member-p part seen
)))
935 (sb-int:add-to-xset part seen
)
936 (funcall fun part
))))
938 (let ((table sb-pcl
::*eql-specializer-table
*))
939 (call (sb-int:with-locked-system-table
(table)
940 (gethash object table
)))))
942 ((or bignum float sb-sys
:system-area-pointer fixnum
))
944 (call (sb-ext:weak-pointer-value object
)))
948 (when (and ext
(ignore-errors (fboundp object
)))
949 (call (fdefinition object
))))
951 (call (numerator object
))
952 (call (denominator object
)))
954 (call (realpart object
))
955 (call (realpart object
)))
957 (call (sb-kernel:%instance-layout object
))
958 (sb-kernel:do-instance-tagged-slot
(i object
)
959 (call (sb-kernel:%instance-ref object i
)))
961 (when (typep object
'sb-thread
:thread
)
962 (cond ((eq object sb-thread
:*current-thread
*)
963 (dolist (value (sb-thread::%thread-local-references
))
965 (sb-vm::map-stack-references
#'call
))
967 ;; KLUDGE: INTERRUPT-THREAD is Not Nice (tm), but
968 ;; the alternative would be stopping the world...
970 (let ((sem (sb-thread:make-semaphore
))
974 (sb-thread:interrupt-thread
977 (setf refs
(sb-thread::%thread-local-references
))
978 (sb-vm::map-stack-references
(lambda (x) (push x refs
)))
979 (sb-thread:signal-semaphore sem
)))
980 (sb-thread:wait-on-semaphore sem
))
981 (sb-thread:interrupt-thread-error
()))
982 (mapc #'call refs
))))))
984 (if (simple-vector-p object
)
985 (dotimes (i (length object
))
986 (call (aref object i
)))
987 (when (sb-kernel:array-header-p object
)
988 (call (sb-kernel::%array-data-vector object
))
989 (call (sb-kernel::%array-displaced-p object
))
991 (call (sb-kernel::%array-displaced-from object
))))))
992 (sb-kernel:code-component
993 (call (sb-kernel:%code-entry-points object
))
994 (call (sb-kernel:%code-debug-info object
))
995 (loop for i from sb-vm
:code-constants-offset
996 below
(sb-kernel:get-header-data object
)
997 do
(call (sb-kernel:code-header-ref object i
))))
999 (call (sb-kernel:fdefn-name object
))
1000 (call (sb-kernel:fdefn-fun object
)))
1001 (sb-kernel:simple-fun
1003 (call (sb-kernel:%simple-fun-next object
)))
1004 (call (sb-kernel:fun-code-header object
))
1005 (call (sb-kernel:%simple-fun-name object
))
1006 (call (sb-kernel:%simple-fun-arglist object
))
1007 (call (sb-kernel:%simple-fun-type object
))
1008 (call (sb-kernel:%simple-fun-info object
)))
1010 (call (sb-kernel:%closure-fun object
))
1011 (sb-kernel:do-closure-values
(x object
)
1013 (sb-kernel:funcallable-instance
1014 (call (sb-kernel:%funcallable-instance-function object
))
1015 (loop for i from
1 below
(- (1+ (sb-kernel:get-closure-length object
))
1016 sb-vm
::funcallable-instance-info-offset
)
1017 do
(call (sb-kernel:%funcallable-instance-info object i
))))
1020 (dolist (thread (sb-thread:list-all-threads
))
1021 (call (sb-thread:symbol-value-in-thread object thread nil
))))
1023 ;; We don't have GLOBAL-BOUNDP, and there's no ERRORP arg.
1024 (call (sb-ext:symbol-global-value object
))
1025 (unbound-variable ()))
1026 ;; These first two are probably unnecessary.
1027 ;; The functoid values, if present, are in SYMBOL-INFO
1028 ;; which is traversed whether or not EXT was true.
1029 ;; But should we traverse SYMBOL-INFO?
1030 ;; I don't know what is expected of this interface.
1031 (when (and ext
(ignore-errors (fboundp object
)))
1032 (call (fdefinition object
))
1033 (call (macro-function object
))
1034 (let ((class (find-class object nil
)))
1035 (when class
(call class
))))
1036 (call (symbol-plist object
)) ; perhaps SB-KERNEL:SYMBOL-INFO instead?
1037 (call (symbol-name object
))
1039 (call (symbol-package object
))))
1040 (sb-kernel::random-class
1041 (case (sb-kernel:widetag-of object
)
1042 (#.sb-vm
::value-cell-header-widetag
1043 (call (sb-kernel::value-cell-ref object
)))
1045 (warn "~&MAP-ROOT: Unknown widetag ~S: ~S~%"
1046 (sb-kernel:widetag-of object
) object
)))))))