1 ;;; This is here as a discussion point, not yet a supported interface. If
2 ;;; you would like to use the functions here, or you would like other
3 ;;; functions to be here, join the debate on navel@metacircles.com.
4 ;;; List info at http://lists.metacircles.com/cgi-bin/mailman/listinfo/navel
6 ;;; For the avoidance of doubt, the exported interface is the proposed
7 ;;; supported interface. Anything else is internal, though you're
8 ;;; welcome to argue a case for exporting it.
10 ;;; If you steal the code from this file to cut and paste into your
11 ;;; own project, there will be much wailing and gnashing of teeth.
12 ;;; Your teeth. If need be, we'll kick them for you. This is a
13 ;;; contrib, we're allowed to look in internals. You're an
14 ;;; application programmer, and are not.
17 ;;; 1) structs don't have within-file location info. problem for the
18 ;;; structure itself, accessors and the predicate
19 ;;; 2) what should find-definition-source on a symbol return? there may be
20 ;;; several definitions (class, function, etc)
21 ;;; 3) error handling. Signal random errors, or handle and resignal 'our'
22 ;;; error, or return NIL?
24 ;;; 5) would be nice to have some interface to the compiler that lets us
25 ;;; fake the filename and position, for use with C-M-x
27 (declaim (optimize (debug 1)))
29 (defpackage :sb-introspect
31 (:export "FUNCTION-ARGLIST" "VALID-FUNCTION-NAME-P"
32 "FIND-DEFINITION-SOURCE"
33 "DEFINITION-SOURCE" "DEFINITION-SOURCE-PATHNAME"
34 "DEFINITION-NOT-FOUND" "DEFINITION-NAME"
35 "DEFINITION-SOURCE-FORM-PATH"
36 "DEFINITION-SOURCE-CHARACTER-OFFSET"
37 "FIND-FUNCTION-CALLEES"
38 "FIND-FUNCTION-CALLERS"
40 (in-package :sb-introspect)
43 (defun valid-function-name-p (name)
44 "True if NAME denotes a function name that can be passed to MACRO-FUNCTION or FDEFINITION "
45 (and (sb-int:valid-function-name-p name) t))
47 ;;; FIXME: maybe this should be renamed as FUNCTION-LAMBDA-LIST?
48 (defun function-arglist (function)
49 "Describe the lambda list for the function designator FUNCTION.
50 Works for special-operators, macros, simple functions and generic
51 functions. Signals error if not found"
52 (cond ((valid-function-name-p function)
54 (or (macro-function function) (fdefinition function))))
55 ((typep function 'generic-function)
56 (sb-pcl::generic-function-pretty-arglist function))
57 (t (sb-impl::%simple-fun-arglist
58 (sb-impl::%closure-fun function)))))
60 (defgeneric find-definition-source (thing)
61 (:documentation "Find the source location that defines THING.
62 Returns a DEFINITION-SOURCE object"))
64 ;;; This is an opaque object with accessors as per export list.
65 ;;; Might not be a struct.
67 (defstruct definition-source
68 pathname ; source file, not fasl
73 ;;; This is kludgey. We expect these functions (the underlying functions,
74 ;;; not the closures) to be in static space and so not move ever.
75 ;;; FIXME It's also possibly wrong: not all structures use these vanilla
76 ;;; accessors, e.g. when the :type option is used
77 (defvar *struct-slotplace-reader*
78 (sb-vm::%simple-fun-self #'definition-source-pathname))
79 (defvar *struct-slotplace-writer*
80 (sb-vm::%simple-fun-self #'(setf definition-source-pathname)))
81 (defvar *struct-predicate*
82 (sb-vm::%simple-fun-self #'definition-source-p))
84 ;; Internal-only, don't call this directly
85 (defun find-function-definition-source (o)
87 (sb-kernel:%code-debug-info
88 (sb-kernel:fun-code-header(sb-kernel::%closure-fun o))))
90 (car (sb-c::compiled-debug-info-source debug-info)))
91 (debug-fun (elt (sb-c::compiled-debug-info-fun-map debug-info) 0))
92 (tlf (and debug-fun (sb-c::compiled-debug-fun-tlf-number debug-fun))))
93 ;; HAZARDOUS ASSUMPTION: in CMUCL it's possible to get >1 debug-source
94 ;; for a debug-info (one per file). In SBCL the function that builds
95 ;; debug-sources always produces singleton lists
96 (sb-int:aver (not (cdr (sb-c::compiled-debug-info-source debug-info))))
97 (make-definition-source
99 (and (eql (sb-c::debug-source-from debug-source) :file)
100 (parse-namestring (sb-c::debug-source-name debug-source)))
101 ;; we don't have a real sexp path, annoyingly. Fake one from the
102 ;; top-level form number
105 (elt (sb-c::debug-source-start-positions debug-source) tlf))
106 :form-path (and tlf (list tlf)))))
108 (defmethod find-definition-source ((o function))
110 ((struct-accessor-p o)
111 (find-definition-source (struct-accessor-structure-class o)))
112 ((struct-predicate-p o)
113 (find-definition-source (struct-predicate-structure-class o)))
114 (t (find-function-definition-source o))))
116 (defmethod find-definition-source ((o method))
117 (find-definition-source (or (sb-pcl::method-fast-function o)
118 (sb-pcl:method-function o))))
120 (defmethod find-definition-source (name)
121 (and (valid-function-name-p name)
122 (find-definition-source (or (macro-function name) (fdefinition name)))))
124 ;; these are internal functions, and probably incomplete
125 (defun struct-accessor-p (function)
126 (let ((self (sb-vm::%simple-fun-self function)))
127 ;; FIXME there are other kinds of struct accessor. Fill out this list
128 (member self (list *struct-slotplace-reader*
129 *struct-slotplace-writer*))))
131 (defun struct-predicate-p (function)
132 (let ((self (sb-vm::%simple-fun-self function)))
133 ;; FIXME there may be other structure predicate functions
134 (member self (list *struct-predicate*))))
136 ;; FIXME need one for constructor too, perhaps
138 (defun struct-accessor-structure-class (function)
139 (let ((self (sb-vm::%simple-fun-self function)))
141 ((member self (list *struct-slotplace-reader* *struct-slotplace-writer*))
143 (sb-kernel::classoid-name
144 (sb-kernel::layout-classoid
145 (sb-kernel:%closure-index-ref function 1)))))
148 (defun struct-predicate-structure-class (function)
149 (let ((self (sb-vm::%simple-fun-self function)))
151 ((member self (list *struct-predicate*))
153 (sb-kernel::classoid-name
154 (sb-kernel::layout-classoid
155 (sb-kernel:%closure-index-ref function 0)))))
158 (defmethod find-definition-source ((o structure-class))
159 ;; FIXME we don't get form-number from this, which is a shame
161 (sb-kernel::structure-classoid-constructor
162 (sb-kernel:classoid-cell-classoid
163 (sb-int:info :type :classoid (class-name o))))))
164 (find-definition-source constructor)))
166 ;;;; find callers/callees, liberated from Helmut Eller's code in SLIME
168 ;;; This interface is trmendously experimental.
170 ;;; For the moment I'm taking the view that FDEFN is an internal
171 ;;; object (one out of one CMUCL developer surveyed didn't know what
172 ;;; they were for), so these routines deal in FUNCTIONs
174 ;;; Find callers and callees by looking at the constant pool of
175 ;;; compiled code objects. We assume every fdefn object in the
176 ;;; constant pool corresponds to a call to that function. A better
177 ;;; strategy would be to use the disassembler to find actual
180 (declaim (inline map-code-constants))
181 (defun map-code-constants (code fn)
182 "Call FN for each constant in CODE's constant pool."
183 (check-type code sb-kernel:code-component)
184 (loop for i from sb-vm:code-constants-offset below
185 (sb-kernel:get-header-data code)
186 do (funcall fn (sb-kernel:code-header-ref code i))))
188 (defun find-function-callees (function)
189 "Return functions called by FUNCTION."
192 (sb-kernel:fun-code-header function)
194 (when (sb-kernel:fdefn-p obj)
195 (push (sb-kernel:fdefn-fun obj)
199 (declaim (inline map-allocated-code-components))
200 (defun map-allocated-code-components (spaces fn)
201 "Call FN for each allocated code component in one of SPACES. FN
202 receives the object and its size as arguments. SPACES should be a
203 list of the symbols :dynamic, :static, or :read-only."
204 (dolist (space spaces)
205 (sb-vm::map-allocated-objects
206 (lambda (obj header size)
207 (when (= sb-vm:code-header-widetag header)
208 (funcall fn obj size)))
211 (declaim (inline map-caller-code-components))
212 (defun map-caller-code-components (function spaces fn)
213 "Call FN for each code component with a fdefn for FUNCTION in its
215 (let ((function (coerce function 'function)))
216 (map-allocated-code-components
219 (declare (ignore size))
223 (when (and (sb-kernel:fdefn-p constant)
224 (eq (sb-kernel:fdefn-fun constant)
226 (funcall fn obj))))))))
228 (defun find-function-callers (function &optional (spaces '(:read-only :static
230 "Return functions which call FUNCTION, by searching SPACES for code objects"
231 (let ((referrers '()))
232 (map-caller-code-components
236 (let ((entry (sb-kernel:%code-entry-points code)))
238 (push (princ-to-string code) referrers))
240 (loop for e = entry then (sb-kernel::%simple-fun-next e)
242 do (pushnew e referrers)))))))
245 (provide 'sb-introspect)