1 ;;; introspection library
3 ;;; This is here as a discussion point, not yet a supported interface. If
4 ;;; you would like to use the functions here, or you would like other
5 ;;; functions to be here, join the debate on navel@metacircles.com.
6 ;;; List info at http://lists.metacircles.com/cgi-bin/mailman/listinfo/navel
8 ;;; For the avoidance of doubt, the exported interface is the proposed
9 ;;; supported interface. Anything else is internal, though you're
10 ;;; welcome to argue a case for exporting it.
12 ;;; If you steal the code from this file to cut and paste into your
13 ;;; own project, there will be much wailing and gnashing of teeth.
14 ;;; Your teeth. If need be, we'll kick them for you. This is a
15 ;;; contrib, we're allowed to look in internals. You're an
16 ;;; application programmer, and are not.
19 ;;; 1) structs don't have within-file location info. problem for the
20 ;;; structure itself, accessors and the predicate
21 ;;; 3) error handling. Signal random errors, or handle and resignal 'our'
22 ;;; error, or return NIL?
25 (defpackage :sb-introspect
27 (:export "FUNCTION-ARGLIST"
28 "VALID-FUNCTION-NAME-P"
29 "FIND-DEFINITION-SOURCE"
30 "FIND-DEFINITION-SOURCES-BY-NAME"
32 "DEFINITION-SOURCE-PATHNAME"
33 "DEFINITION-SOURCE-FORM-PATH"
34 "DEFINITION-SOURCE-CHARACTER-OFFSET"
35 "DEFINITION-SOURCE-FILE-WRITE-DATE"
36 "DEFINITION-SOURCE-PLIST"
37 "DEFINITION-NOT-FOUND" "DEFINITION-NAME"
38 "FIND-FUNCTION-CALLEES"
39 "FIND-FUNCTION-CALLERS"
46 (in-package :sb-introspect)
48 ;;;; Internal interface for SBCL debug info
50 ;;; Here are some tutorial-style type definitions to help understand
51 ;;; the internal SBCL debugging data structures we're using. The
52 ;;; commentary is based on CMUCL's debug internals manual.
54 (deftype debug-info ()
55 "Structure containing all the debug information related to a function.
56 Function objects reference debug-infos which in turn reference
57 debug-sources and so on."
58 'sb-c::compiled-debug-info)
60 (deftype debug-source ()
61 "Debug sources describe where to find source code.
62 For example, the debug source for a function compiled from a file will
63 include the pathname of the file and the position of the definition."
66 (deftype debug-function ()
67 "Debug function represent static compile-time information about a function."
68 'sb-c::compiled-debug-fun)
70 (declaim (ftype (function (function) debug-info) function-debug-info))
71 (defun function-debug-info (function)
72 (let* ((function-object (sb-kernel::%closure-fun function))
73 (function-header (sb-kernel:fun-code-header function-object)))
74 (sb-kernel:%code-debug-info function-header)))
76 (declaim (ftype (function (function) debug-source) function-debug-source))
77 (defun function-debug-source (function)
78 (debug-info-source (function-debug-info function)))
80 (declaim (ftype (function (debug-info) debug-source) debug-info-source))
81 (defun debug-info-source (debug-info)
82 (sb-c::debug-info-source debug-info))
84 (declaim (ftype (function (debug-info) debug-function) debug-info-debug-function))
85 (defun debug-info-debug-function (debug-info)
86 (elt (sb-c::compiled-debug-info-fun-map debug-info) 0))
88 (defun valid-function-name-p (name)
89 "True if NAME denotes a function name that can be passed to MACRO-FUNCTION or FDEFINITION "
90 (and (sb-int:valid-function-name-p name) t))
92 ;;;; Finding definitions
94 (defstruct definition-source
95 ;; Pathname of the source file that the definition was compiled from.
96 ;; This is null if the definition was not compiled from a file.
97 (pathname nil :type (or null pathname))
98 ;; Source-path of the definition within the file.
99 ;; This may be incomplete depending on the debug level at which the
100 ;; source was compiled.
101 (form-path '() :type list)
102 ;; Character offset of the top-level-form containing the definition.
103 ;; This corresponds to the first element of form-path.
104 (character-offset nil :type (or null integer))
105 ;; File-write-date of the source file when compiled.
106 ;; Null if not compiled from a file.
107 (file-write-date nil :type (or null integer))
108 ;; plist from WITH-COMPILATION-UNIT
110 ;; Any extra metadata that the caller might be interested in. For
111 ;; example the specializers of the method whose definition-source this
113 (description nil :type list))
115 (defun find-definition-sources-by-name (name type)
116 "Returns a list of DEFINITION-SOURCEs for the objects of type TYPE
117 defined with name NAME. NAME may be a symbol or a extended function
118 name. Type can currently be one of the following:
143 If an unsupported TYPE is requested, the function will return NIL.
151 (find-class name nil)))
152 (real-fdefinition (name)
153 ;; for getting the real function object, even if the
154 ;; function is being profiled
155 (let ((profile-info (gethash name sb-profile::*profiled-fun-name->info*)))
157 (sb-profile::profile-info-encapsulated-fun profile-info)
158 (fdefinition name)))))
162 (when (and (symbolp name)
163 (eq (sb-int:info :variable :kind name) :special))
164 (translate-source-location (sb-int:info :source-location type name))))
166 (when (and (symbolp name)
167 (eq (sb-int:info :variable :kind name) :constant))
168 (translate-source-location (sb-int:info :source-location type name))))
170 (when (and (symbolp name)
171 (eq (sb-int:info :variable :kind name) :macro))
172 (translate-source-location (sb-int:info :source-location type name))))
174 (when (and (symbolp name)
175 (macro-function name))
176 (find-definition-source (macro-function name))))
178 (when (compiler-macro-function name)
179 (find-definition-source (compiler-macro-function name))))
180 ((:function :generic-function)
181 (when (and (fboundp name)
182 (or (not (symbolp name))
183 (not (macro-function name))))
184 (let ((fun (real-fdefinition name)))
185 (when (eq (not (typep fun 'generic-function))
186 (not (eq type :generic-function)))
187 (find-definition-source fun)))))
189 (let ((expander-fun (sb-int:info :type :expander name)))
191 (find-definition-source expander-fun))))
194 (let ((fun (real-fdefinition name)))
195 (when (typep fun 'generic-function)
196 (loop for method in (sb-mop::generic-function-methods
198 for source = (find-definition-source method)
199 when source collect source)))))
201 (when (and (consp name)
202 (eq (car name) 'setf))
203 (setf name (cadr name)))
204 (let ((expander (or (sb-int:info :setf :inverse name)
205 (sb-int:info :setf :expander name))))
207 (sb-introspect:find-definition-source (if (symbolp expander)
208 (symbol-function expander)
211 (let ((class (get-class name)))
213 (when (typep class 'sb-pcl::structure-class)
214 (find-definition-source class))
215 (when (sb-int:info :typed-structure :info name)
216 (translate-source-location
217 (sb-int:info :source-location :typed-structure name))))))
219 (let ((class (get-class name)))
221 (not (typep class 'sb-pcl::structure-class)))
222 (when (eq (not (typep class 'sb-pcl::condition-class))
223 (not (eq type :condition)))
224 (find-definition-source class)))))
225 ((:method-combination)
226 (let ((combination-fun
227 (find-method #'sb-mop:find-method-combination
229 (list (find-class 'generic-function)
233 (when combination-fun
234 (find-definition-source combination-fun))))
237 (let ((package (find-package name)))
239 (find-definition-source package)))))
240 ;; TRANSFORM and OPTIMIZER handling from swank-sbcl
243 (let ((fun-info (sb-int:info :function :info name)))
245 (loop for xform in (sb-c::fun-info-transforms fun-info)
246 for source = (find-definition-source
247 (sb-c::transform-function xform))
248 for typespec = (sb-kernel:type-specifier
249 (sb-c::transform-type xform))
250 for note = (sb-c::transform-note xform)
251 do (setf (definition-source-description source)
253 (list (second typespec) note)
258 (let ((fun-info (sb-int:info :function :info name)))
260 (let ((otypes '((sb-c::fun-info-derive-type . sb-c:derive-type)
261 (sb-c::fun-info-ltn-annotate . sb-c:ltn-annotate)
262 (sb-c::fun-info-ltn-annotate . sb-c:ltn-annotate)
263 (sb-c::fun-info-optimizer . sb-c:optimizer))))
264 (loop for (reader . name) in otypes
265 for fn = (funcall reader fun-info)
267 (let ((source (find-definition-source fn)))
268 (setf (definition-source-description source)
273 (let ((fun-info (sb-int:info :function :info name)))
275 (loop for vop in (sb-c::fun-info-templates fun-info)
276 for source = (find-definition-source
277 (sb-c::vop-info-generator-function vop))
278 do (setf (definition-source-description source)
279 (list (sb-c::template-name vop)
280 (sb-c::template-note vop)))
284 (let ((transform-fun (sb-int:info :function :source-transform name)))
286 (sb-introspect:find-definition-source transform-fun)))))
290 (defun find-definition-source (object)
292 ((or sb-pcl::condition-class sb-pcl::structure-class)
293 (let ((classoid (sb-impl::find-classoid (class-name object))))
295 (let ((layout (sb-impl::classoid-layout classoid)))
297 (translate-source-location
298 (sb-kernel::layout-source-location layout)))))))
301 (find-definition-sources-by-name
302 (sb-pcl::method-combination-type-name object) :method-combination)))
304 (translate-source-location (sb-impl::package-source-location object)))
306 (translate-source-location (sb-pcl::definition-source object)))
307 ;; Use the PCL definition location information instead of the function
308 ;; debug-info for methods and generic functions. Sometimes the
309 ;; debug-info would point into PCL internals instead of the proper
312 (let ((source (translate-source-location
313 (sb-pcl::definition-source object))))
315 (setf (definition-source-description source)
316 (list (sb-mop:generic-function-lambda-list object))))
319 (let ((source (translate-source-location
320 (sb-pcl::definition-source object))))
322 (setf (definition-source-description source)
323 (append (method-qualifiers object)
324 (if (sb-mop:method-generic-function object)
325 (sb-pcl::unparse-specializers
326 (sb-mop:method-generic-function object)
327 (sb-mop:method-specializers object))
328 (sb-mop:method-specializers object)))))
331 (sb-eval:interpreted-function
332 (let ((source (translate-source-location
333 (sb-eval:interpreted-function-source-location object))))
336 (cond ((struct-accessor-p object)
337 (find-definition-source
338 (struct-accessor-structure-class object)))
339 ((struct-predicate-p object)
340 (find-definition-source
341 (struct-predicate-structure-class object)))
343 (find-function-definition-source object))))
344 ((or condition standard-object structure-object)
345 (find-definition-source (class-of object)))
347 (error "Don't know how to retrieve source location for a ~S~%"
350 (defun find-function-definition-source (function)
351 (let* ((debug-info (function-debug-info function))
352 (debug-source (debug-info-source debug-info))
353 (debug-fun (debug-info-debug-function debug-info))
354 (tlf (if debug-fun (sb-c::compiled-debug-fun-tlf-number debug-fun))))
355 (make-definition-source
357 ;; KLUDGE: at the moment, we don't record the correct toplevel
358 ;; form number for forms processed by EVAL (including EVAL-WHEN
359 ;; :COMPILE-TOPLEVEL). Until that's fixed, don't return a
360 ;; DEFINITION-SOURCE with a pathname. (When that's fixed, take
361 ;; out the (not (debug-source-form ...)) test.
362 (if (and (sb-c::debug-source-namestring debug-source)
363 (not (sb-c::debug-source-form debug-source)))
364 (parse-namestring (sb-c::debug-source-namestring debug-source)))
367 (elt (sb-c::debug-source-start-positions debug-source) tlf))
368 ;; Unfortunately there is no proper source path available in the
369 ;; debug-source. FIXME: We could use sb-di:code-locations to get
370 ;; a full source path. -luke (12/Mar/2005)
371 :form-path (if tlf (list tlf))
372 :file-write-date (sb-c::debug-source-created debug-source)
373 :plist (sb-c::debug-source-plist debug-source))))
375 (defun translate-source-location (location)
377 (make-definition-source
378 :pathname (let ((n (sb-c:definition-source-location-namestring location)))
380 (parse-namestring n)))
382 (let ((number (sb-c:definition-source-location-toplevel-form-number
386 :plist (sb-c:definition-source-location-plist location))
387 (make-definition-source)))
389 ;;; This is kludgey. We expect these functions (the underlying functions,
390 ;;; not the closures) to be in static space and so not move ever.
391 ;;; FIXME It's also possibly wrong: not all structures use these vanilla
392 ;;; accessors, e.g. when the :type option is used
393 (defvar *struct-slotplace-reader*
394 (sb-vm::%simple-fun-self #'definition-source-pathname))
395 (defvar *struct-slotplace-writer*
396 (sb-vm::%simple-fun-self #'(setf definition-source-pathname)))
397 (defvar *struct-predicate*
398 (sb-vm::%simple-fun-self #'definition-source-p))
400 (defun struct-accessor-p (function)
401 (let ((self (sb-vm::%simple-fun-self function)))
402 ;; FIXME there are other kinds of struct accessor. Fill out this list
403 (member self (list *struct-slotplace-reader*
404 *struct-slotplace-writer*))))
406 (defun struct-predicate-p (function)
407 (let ((self (sb-vm::%simple-fun-self function)))
408 ;; FIXME there may be other structure predicate functions
409 (member self (list *struct-predicate*))))
411 ;;; FIXME: maybe this should be renamed as FUNCTION-LAMBDA-LIST?
412 (defun function-arglist (function)
413 "Describe the lambda list for the extended function designator FUNCTION.
414 Works for special-operators, macros, simple functions,
415 interpreted functions, and generic functions. Signals error if
417 (cond ((valid-function-name-p function)
418 (function-arglist (or (and (symbolp function)
419 (macro-function function))
420 (fdefinition function))))
421 ((typep function 'generic-function)
422 (sb-pcl::generic-function-pretty-arglist function))
424 ((typep function 'sb-eval:interpreted-function)
425 (sb-eval:interpreted-function-lambda-list function))
426 (t (sb-kernel:%simple-fun-arglist (sb-kernel:%fun-fun function)))))
428 (defun struct-accessor-structure-class (function)
429 (let ((self (sb-vm::%simple-fun-self function)))
431 ((member self (list *struct-slotplace-reader* *struct-slotplace-writer*))
433 (sb-kernel::classoid-name
434 (sb-kernel::layout-classoid
435 (sb-kernel:%closure-index-ref function 1)))))
438 (defun struct-predicate-structure-class (function)
439 (let ((self (sb-vm::%simple-fun-self function)))
441 ((member self (list *struct-predicate*))
443 (sb-kernel::classoid-name
444 (sb-kernel::layout-classoid
445 (sb-kernel:%closure-index-ref function 0)))))
448 ;;;; find callers/callees, liberated from Helmut Eller's code in SLIME
450 ;;; This interface is trmendously experimental.
452 ;;; For the moment I'm taking the view that FDEFN is an internal
453 ;;; object (one out of one CMUCL developer surveyed didn't know what
454 ;;; they were for), so these routines deal in FUNCTIONs
456 ;;; Find callers and callees by looking at the constant pool of
457 ;;; compiled code objects. We assume every fdefn object in the
458 ;;; constant pool corresponds to a call to that function. A better
459 ;;; strategy would be to use the disassembler to find actual
462 (defun find-function-callees (function)
463 "Return functions called by FUNCTION."
466 (sb-kernel:fun-code-header function)
468 (when (sb-kernel:fdefn-p obj)
469 (push (sb-kernel:fdefn-fun obj)
474 (defun find-function-callers (function &optional (spaces '(:read-only :static
476 "Return functions which call FUNCTION, by searching SPACES for code objects"
477 (let ((referrers '()))
478 (map-caller-code-components
482 (let ((entry (sb-kernel:%code-entry-points code)))
484 (push (princ-to-string code) referrers))
486 (loop for e = entry then (sb-kernel::%simple-fun-next e)
488 do (pushnew e referrers)))))))
491 (declaim (inline map-code-constants))
492 (defun map-code-constants (code fn)
493 "Call FN for each constant in CODE's constant pool."
494 (check-type code sb-kernel:code-component)
495 (loop for i from sb-vm:code-constants-offset below
496 (sb-kernel:get-header-data code)
497 do (funcall fn (sb-kernel:code-header-ref code i))))
499 (declaim (inline map-allocated-code-components))
500 (defun map-allocated-code-components (spaces fn)
501 "Call FN for each allocated code component in one of SPACES. FN
502 receives the object and its size as arguments. SPACES should be a
503 list of the symbols :dynamic, :static, or :read-only."
504 (dolist (space spaces)
505 (sb-vm::map-allocated-objects
506 (lambda (obj header size)
507 (when (= sb-vm:code-header-widetag header)
508 (funcall fn obj size)))
512 (declaim (inline map-caller-code-components))
513 (defun map-caller-code-components (function spaces fn)
514 "Call FN for each code component with a fdefn for FUNCTION in its
516 (let ((function (coerce function 'function)))
517 (map-allocated-code-components
520 (declare (ignore size))
524 (when (and (sb-kernel:fdefn-p constant)
525 (eq (sb-kernel:fdefn-fun constant)
527 (funcall fn obj))))))))
531 (defun get-simple-fun (functoid)
534 (get-simple-fun (sb-vm::fdefn-fun functoid)))
535 ((or null sb-impl::funcallable-instance)
538 (sb-kernel::%closure-fun functoid))))
540 (defun collect-xref (kind-index wanted-name)
542 (dolist (env sb-c::*info-environment* ret)
543 ;; Loop through the infodb ...
544 (sb-c::do-info (env :class class :type type :name info-name
546 ;; ... looking for function or macro definitions
547 (when (and (eql class :function)
548 (or (eql type :macro-function)
549 (eql type :definition)))
550 ;; Get a simple-fun for the definition, and an xref array
551 ;; from the table if available.
552 (let* ((simple-fun (get-simple-fun value))
553 (xrefs (when simple-fun
554 (sb-vm::%simple-fun-xrefs simple-fun)))
556 (aref xrefs kind-index))))
557 ;; Loop through the name/path xref entries in the table
558 (loop for i from 0 below (length array) by 2
559 for xref-name = (aref array i)
560 for xref-path = (aref array (1+ i))
561 do (when (eql xref-name wanted-name)
562 (let ((source-location
563 (find-function-definition-source simple-fun)))
564 ;; Use the more accurate source path from
566 (setf (definition-source-form-path source-location)
568 (push (cons info-name source-location)
571 (defun who-calls (function-name)
572 "Use the xref facility to search for source locations where the
573 global function named FUNCTION-NAME is called. Returns a list of
574 function name, definition-source pairs."
575 (collect-xref #.(position :calls sb-c::*xref-kinds*) function-name))
577 (defun who-binds (symbol)
578 "Use the xref facility to search for source locations where the
579 special variable SYMBOL is rebound. Returns a list of function name,
580 definition-source pairs."
581 (collect-xref #.(position :binds sb-c::*xref-kinds*) symbol))
583 (defun who-references (symbol)
584 "Use the xref facility to search for source locations where the
585 special variable or constant SYMBOL is read. Returns a list of function
586 name, definition-source pairs."
587 (collect-xref #.(position :references sb-c::*xref-kinds*) symbol))
589 (defun who-sets (symbol)
590 "Use the xref facility to search for source locations where the
591 special variable SYMBOL is written to. Returns a list of function name,
592 definition-source pairs."
593 (collect-xref #.(position :sets sb-c::*xref-kinds*) symbol))
595 (defun who-macroexpands (macro-name)
596 "Use the xref facility to search for source locations where the
597 macro MACRO-NAME is expanded. Returns a list of function name,
598 definition-source pairs."
599 (collect-xref #.(position :macroexpands sb-c::*xref-kinds*) macro-name))
601 (provide 'sb-introspect)