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 "ALLOCATION-INFORMATION"
29 "FUNCTION-LAMBDA-LIST"
31 "VALID-FUNCTION-NAME-P"
32 "FIND-DEFINITION-SOURCE"
33 "FIND-DEFINITION-SOURCES-BY-NAME"
35 "DEFINITION-SOURCE-PATHNAME"
36 "DEFINITION-SOURCE-FORM-PATH"
37 "DEFINITION-SOURCE-CHARACTER-OFFSET"
38 "DEFINITION-SOURCE-FILE-WRITE-DATE"
39 "DEFINITION-SOURCE-PLIST"
40 "DEFINITION-NOT-FOUND" "DEFINITION-NAME"
41 "FIND-FUNCTION-CALLEES"
42 "FIND-FUNCTION-CALLERS"
49 (in-package :sb-introspect)
51 ;;;; Internal interface for SBCL debug info
53 ;;; Here are some tutorial-style type definitions to help understand
54 ;;; the internal SBCL debugging data structures we're using. The
55 ;;; commentary is based on CMUCL's debug internals manual.
57 (deftype debug-info ()
58 "Structure containing all the debug information related to a function.
59 Function objects reference debug-infos which in turn reference
60 debug-sources and so on."
61 'sb-c::compiled-debug-info)
63 (deftype debug-source ()
64 "Debug sources describe where to find source code.
65 For example, the debug source for a function compiled from a file will
66 include the pathname of the file and the position of the definition."
69 (deftype debug-function ()
70 "Debug function represent static compile-time information about a function."
71 'sb-c::compiled-debug-fun)
73 (declaim (ftype (function (function) debug-info) function-debug-info))
74 (defun function-debug-info (function)
75 (let* ((function-object (sb-kernel::%fun-fun function))
76 (function-header (sb-kernel:fun-code-header function-object)))
77 (sb-kernel:%code-debug-info function-header)))
79 (declaim (ftype (function (function) debug-source) function-debug-source))
80 (defun function-debug-source (function)
81 (debug-info-source (function-debug-info function)))
83 (declaim (ftype (function (debug-info) debug-source) debug-info-source))
84 (defun debug-info-source (debug-info)
85 (sb-c::debug-info-source debug-info))
87 (declaim (ftype (function (debug-info) debug-function) debug-info-debug-function))
88 (defun debug-info-debug-function (debug-info)
89 (elt (sb-c::compiled-debug-info-fun-map debug-info) 0))
91 (defun valid-function-name-p (name)
92 "True if NAME denotes a function name that can be passed to MACRO-FUNCTION or FDEFINITION "
93 (and (sb-int:valid-function-name-p name) t))
95 ;;;; Finding definitions
97 (defstruct definition-source
98 ;; Pathname of the source file that the definition was compiled from.
99 ;; This is null if the definition was not compiled from a file.
100 (pathname nil :type (or null pathname))
101 ;; Source-path of the definition within the file.
102 ;; This may be incomplete depending on the debug level at which the
103 ;; source was compiled.
104 (form-path '() :type list)
105 ;; Character offset of the top-level-form containing the definition.
106 ;; This corresponds to the first element of form-path.
107 (character-offset nil :type (or null integer))
108 ;; File-write-date of the source file when compiled.
109 ;; Null if not compiled from a file.
110 (file-write-date nil :type (or null integer))
111 ;; plist from WITH-COMPILATION-UNIT
113 ;; Any extra metadata that the caller might be interested in. For
114 ;; example the specializers of the method whose definition-source this
116 (description nil :type list))
118 (defun find-definition-sources-by-name (name type)
119 "Returns a list of DEFINITION-SOURCEs for the objects of type TYPE
120 defined with name NAME. NAME may be a symbol or a extended function
121 name. Type can currently be one of the following:
146 If an unsupported TYPE is requested, the function will return NIL.
154 (find-class name nil)))
155 (real-fdefinition (name)
156 ;; for getting the real function object, even if the
157 ;; function is being profiled
158 (let ((profile-info (gethash name sb-profile::*profiled-fun-name->info*)))
160 (sb-profile::profile-info-encapsulated-fun profile-info)
161 (fdefinition name)))))
165 (when (and (symbolp name)
166 (eq (sb-int:info :variable :kind name) :special))
167 (translate-source-location (sb-int:info :source-location type name))))
169 (when (and (symbolp name)
170 (eq (sb-int:info :variable :kind name) :constant))
171 (translate-source-location (sb-int:info :source-location type name))))
173 (when (and (symbolp name)
174 (eq (sb-int:info :variable :kind name) :macro))
175 (translate-source-location (sb-int:info :source-location type name))))
177 (when (and (symbolp name)
178 (macro-function name))
179 (find-definition-source (macro-function name))))
181 (when (compiler-macro-function name)
182 (find-definition-source (compiler-macro-function name))))
183 ((:function :generic-function)
184 (when (and (fboundp name)
185 (or (not (symbolp name))
186 (not (macro-function name))))
187 (let ((fun (real-fdefinition name)))
188 (when (eq (not (typep fun 'generic-function))
189 (not (eq type :generic-function)))
190 (find-definition-source fun)))))
192 ;; Source locations for types are saved separately when the expander
193 ;; is a closure without a good source-location.
194 (let ((loc (sb-int:info :type :source-location name)))
196 (translate-source-location loc)
197 (let ((expander-fun (sb-int:info :type :expander name)))
199 (find-definition-source expander-fun))))))
202 (let ((fun (real-fdefinition name)))
203 (when (typep fun 'generic-function)
204 (loop for method in (sb-mop::generic-function-methods
206 for source = (find-definition-source method)
207 when source collect source)))))
209 (when (and (consp name)
210 (eq (car name) 'setf))
211 (setf name (cadr name)))
212 (let ((expander (or (sb-int:info :setf :inverse name)
213 (sb-int:info :setf :expander name))))
215 (sb-introspect:find-definition-source (if (symbolp expander)
216 (symbol-function expander)
219 (let ((class (get-class name)))
221 (when (typep class 'sb-pcl::structure-class)
222 (find-definition-source class))
223 (when (sb-int:info :typed-structure :info name)
224 (translate-source-location
225 (sb-int:info :source-location :typed-structure name))))))
227 (let ((class (get-class name)))
229 (not (typep class 'sb-pcl::structure-class)))
230 (when (eq (not (typep class 'sb-pcl::condition-class))
231 (not (eq type :condition)))
232 (find-definition-source class)))))
233 ((:method-combination)
234 (let ((combination-fun
235 (find-method #'sb-mop:find-method-combination
237 (list (find-class 'generic-function)
241 (when combination-fun
242 (find-definition-source combination-fun))))
245 (let ((package (find-package name)))
247 (find-definition-source package)))))
248 ;; TRANSFORM and OPTIMIZER handling from swank-sbcl
251 (let ((fun-info (sb-int:info :function :info name)))
253 (loop for xform in (sb-c::fun-info-transforms fun-info)
254 for source = (find-definition-source
255 (sb-c::transform-function xform))
256 for typespec = (sb-kernel:type-specifier
257 (sb-c::transform-type xform))
258 for note = (sb-c::transform-note xform)
259 do (setf (definition-source-description source)
261 (list (second typespec) note)
266 (let ((fun-info (sb-int:info :function :info name)))
268 (let ((otypes '((sb-c::fun-info-derive-type . sb-c:derive-type)
269 (sb-c::fun-info-ltn-annotate . sb-c:ltn-annotate)
270 (sb-c::fun-info-ltn-annotate . sb-c:ltn-annotate)
271 (sb-c::fun-info-optimizer . sb-c:optimizer))))
272 (loop for (reader . name) in otypes
273 for fn = (funcall reader fun-info)
275 (let ((source (find-definition-source fn)))
276 (setf (definition-source-description source)
281 (let ((fun-info (sb-int:info :function :info name)))
283 (loop for vop in (sb-c::fun-info-templates fun-info)
284 for source = (find-definition-source
285 (sb-c::vop-info-generator-function vop))
286 do (setf (definition-source-description source)
287 (list (sb-c::template-name vop)
288 (sb-c::template-note vop)))
292 (let ((transform-fun (sb-int:info :function :source-transform name)))
294 (sb-introspect:find-definition-source transform-fun)))))
298 (defun find-definition-source (object)
300 ((or sb-pcl::condition-class sb-pcl::structure-class)
301 (let ((classoid (sb-impl::find-classoid (class-name object))))
303 (let ((layout (sb-impl::classoid-layout classoid)))
305 (translate-source-location
306 (sb-kernel::layout-source-location layout)))))))
309 (find-definition-sources-by-name
310 (sb-pcl::method-combination-type-name object) :method-combination)))
312 (translate-source-location (sb-impl::package-source-location object)))
314 (translate-source-location (sb-pcl::definition-source object)))
315 ;; Use the PCL definition location information instead of the function
316 ;; debug-info for methods and generic functions. Sometimes the
317 ;; debug-info would point into PCL internals instead of the proper
320 (let ((source (translate-source-location
321 (sb-pcl::definition-source object))))
323 (setf (definition-source-description source)
324 (list (sb-mop:generic-function-lambda-list object))))
327 (let ((source (translate-source-location
328 (sb-pcl::definition-source object))))
330 (setf (definition-source-description source)
331 (append (method-qualifiers object)
332 (if (sb-mop:method-generic-function object)
333 (sb-pcl::unparse-specializers
334 (sb-mop:method-generic-function object)
335 (sb-mop:method-specializers object))
336 (sb-mop:method-specializers object)))))
339 (sb-eval:interpreted-function
340 (let ((source (translate-source-location
341 (sb-eval:interpreted-function-source-location object))))
344 (cond ((struct-accessor-p object)
345 (find-definition-source
346 (struct-accessor-structure-class object)))
347 ((struct-predicate-p object)
348 (find-definition-source
349 (struct-predicate-structure-class object)))
351 (find-function-definition-source object))))
352 ((or condition standard-object structure-object)
353 (find-definition-source (class-of object)))
355 (error "Don't know how to retrieve source location for a ~S~%"
358 (defun find-function-definition-source (function)
359 (let* ((debug-info (function-debug-info function))
360 (debug-source (debug-info-source debug-info))
361 (debug-fun (debug-info-debug-function debug-info))
362 (tlf (if debug-fun (sb-c::compiled-debug-fun-tlf-number debug-fun))))
363 (make-definition-source
365 ;; KLUDGE: at the moment, we don't record the correct toplevel
366 ;; form number for forms processed by EVAL (including EVAL-WHEN
367 ;; :COMPILE-TOPLEVEL). Until that's fixed, don't return a
368 ;; DEFINITION-SOURCE with a pathname. (When that's fixed, take
369 ;; out the (not (debug-source-form ...)) test.
370 (if (and (sb-c::debug-source-namestring debug-source)
371 (not (sb-c::debug-source-form debug-source)))
372 (parse-namestring (sb-c::debug-source-namestring debug-source)))
375 (elt (sb-c::debug-source-start-positions debug-source) tlf))
376 ;; Unfortunately there is no proper source path available in the
377 ;; debug-source. FIXME: We could use sb-di:code-locations to get
378 ;; a full source path. -luke (12/Mar/2005)
379 :form-path (if tlf (list tlf))
380 :file-write-date (sb-c::debug-source-created debug-source)
381 :plist (sb-c::debug-source-plist debug-source))))
383 (defun translate-source-location (location)
385 (make-definition-source
386 :pathname (let ((n (sb-c:definition-source-location-namestring location)))
388 (parse-namestring n)))
390 (let ((number (sb-c:definition-source-location-toplevel-form-number
394 :plist (sb-c:definition-source-location-plist location))
395 (make-definition-source)))
397 ;;; This is kludgey. We expect these functions (the underlying functions,
398 ;;; not the closures) to be in static space and so not move ever.
399 ;;; FIXME It's also possibly wrong: not all structures use these vanilla
400 ;;; accessors, e.g. when the :type option is used
401 (defvar *struct-slotplace-reader*
402 (sb-vm::%simple-fun-self #'definition-source-pathname))
403 (defvar *struct-slotplace-writer*
404 (sb-vm::%simple-fun-self #'(setf definition-source-pathname)))
405 (defvar *struct-predicate*
406 (sb-vm::%simple-fun-self #'definition-source-p))
408 (defun struct-accessor-p (function)
409 (let ((self (sb-vm::%simple-fun-self function)))
410 ;; FIXME there are other kinds of struct accessor. Fill out this list
411 (member self (list *struct-slotplace-reader*
412 *struct-slotplace-writer*))))
414 (defun struct-predicate-p (function)
415 (let ((self (sb-vm::%simple-fun-self function)))
416 ;; FIXME there may be other structure predicate functions
417 (member self (list *struct-predicate*))))
419 (defun function-arglist (function)
420 "Deprecated alias for FUNCTION-LAMBDA-LIST."
421 (function-lambda-list function))
423 (define-compiler-macro function-arglist (function)
424 (sb-int:deprecation-warning 'function-arglist 'function-lambda-list)
425 `(function-lambda-list ,function))
427 (defun function-lambda-list (function)
428 "Describe the lambda list for the extended function designator FUNCTION.
429 Works for special-operators, macros, simple functions, interpreted functions,
430 and generic functions. Signals an error if FUNCTION is not a valid extended
431 function designator."
432 (cond ((valid-function-name-p function)
433 (function-lambda-list (or (and (symbolp function)
434 (macro-function function))
435 (fdefinition function))))
436 ((typep function 'generic-function)
437 (sb-pcl::generic-function-pretty-arglist function))
439 ((typep function 'sb-eval:interpreted-function)
440 (sb-eval:interpreted-function-lambda-list function))
442 (sb-kernel:%simple-fun-arglist (sb-kernel:%fun-fun function)))))
444 (defun deftype-lambda-list (typespec-operator)
445 "Returns the lambda list of TYPESPEC-OPERATOR as first return
446 value, and a flag whether the arglist could be found as second
448 (sb-int:info :type :lambda-list typespec-operator))
450 (defun struct-accessor-structure-class (function)
451 (let ((self (sb-vm::%simple-fun-self function)))
453 ((member self (list *struct-slotplace-reader* *struct-slotplace-writer*))
455 (sb-kernel::classoid-name
456 (sb-kernel::layout-classoid
457 (sb-kernel:%closure-index-ref function 1)))))
460 (defun struct-predicate-structure-class (function)
461 (let ((self (sb-vm::%simple-fun-self function)))
463 ((member self (list *struct-predicate*))
465 (sb-kernel::classoid-name
466 (sb-kernel::layout-classoid
467 (sb-kernel:%closure-index-ref function 0)))))
470 ;;;; find callers/callees, liberated from Helmut Eller's code in SLIME
472 ;;; This interface is trmendously experimental.
474 ;;; For the moment I'm taking the view that FDEFN is an internal
475 ;;; object (one out of one CMUCL developer surveyed didn't know what
476 ;;; they were for), so these routines deal in FUNCTIONs
478 ;;; Find callers and callees by looking at the constant pool of
479 ;;; compiled code objects. We assume every fdefn object in the
480 ;;; constant pool corresponds to a call to that function. A better
481 ;;; strategy would be to use the disassembler to find actual
484 (defun find-function-callees (function)
485 "Return functions called by FUNCTION."
488 (sb-kernel:fun-code-header function)
490 (when (sb-kernel:fdefn-p obj)
491 (push (sb-kernel:fdefn-fun obj)
496 (defun find-function-callers (function &optional (spaces '(:read-only :static
498 "Return functions which call FUNCTION, by searching SPACES for code objects"
499 (let ((referrers '()))
500 (map-caller-code-components
504 (let ((entry (sb-kernel:%code-entry-points code)))
506 (push (princ-to-string code) referrers))
508 (loop for e = entry then (sb-kernel::%simple-fun-next e)
510 do (pushnew e referrers)))))))
513 (declaim (inline map-code-constants))
514 (defun map-code-constants (code fn)
515 "Call FN for each constant in CODE's constant pool."
516 (check-type code sb-kernel:code-component)
517 (loop for i from sb-vm:code-constants-offset below
518 (sb-kernel:get-header-data code)
519 do (funcall fn (sb-kernel:code-header-ref code i))))
521 (declaim (inline map-allocated-code-components))
522 (defun map-allocated-code-components (spaces fn)
523 "Call FN for each allocated code component in one of SPACES. FN
524 receives the object and its size as arguments. SPACES should be a
525 list of the symbols :dynamic, :static, or :read-only."
526 (dolist (space spaces)
527 (sb-vm::map-allocated-objects
528 (lambda (obj header size)
529 (when (= sb-vm:code-header-widetag header)
530 (funcall fn obj size)))
534 (declaim (inline map-caller-code-components))
535 (defun map-caller-code-components (function spaces fn)
536 "Call FN for each code component with a fdefn for FUNCTION in its
538 (let ((function (coerce function 'function)))
539 (map-allocated-code-components
542 (declare (ignore size))
546 (when (and (sb-kernel:fdefn-p constant)
547 (eq (sb-kernel:fdefn-fun constant)
549 (funcall fn obj))))))))
553 (defun get-simple-fun (functoid)
556 (get-simple-fun (sb-vm::fdefn-fun functoid)))
557 ((or null sb-impl::funcallable-instance)
560 (sb-kernel::%fun-fun functoid))))
562 (defun collect-xref (kind-index wanted-name)
564 (dolist (env sb-c::*info-environment* ret)
565 ;; Loop through the infodb ...
566 (sb-c::do-info (env :class class :type type :name info-name
568 ;; ... looking for function or macro definitions
569 (when (and (eql class :function)
570 (or (eql type :macro-function)
571 (eql type :definition)))
572 ;; Get a simple-fun for the definition, and an xref array
573 ;; from the table if available.
574 (let* ((simple-fun (get-simple-fun value))
575 (xrefs (when simple-fun
576 (sb-kernel:%simple-fun-xrefs simple-fun)))
578 (aref xrefs kind-index))))
579 ;; Loop through the name/path xref entries in the table
580 (loop for i from 0 below (length array) by 2
581 for xref-name = (aref array i)
582 for xref-path = (aref array (1+ i))
583 do (when (eql xref-name wanted-name)
584 (let ((source-location
585 (find-function-definition-source simple-fun)))
586 ;; Use the more accurate source path from
588 (setf (definition-source-form-path source-location)
590 (push (cons info-name source-location)
593 (defun who-calls (function-name)
594 "Use the xref facility to search for source locations where the
595 global function named FUNCTION-NAME is called. Returns a list of
596 function name, definition-source pairs."
597 (collect-xref #.(position :calls sb-c::*xref-kinds*) function-name))
599 (defun who-binds (symbol)
600 "Use the xref facility to search for source locations where the
601 special variable SYMBOL is rebound. Returns a list of function name,
602 definition-source pairs."
603 (collect-xref #.(position :binds sb-c::*xref-kinds*) symbol))
605 (defun who-references (symbol)
606 "Use the xref facility to search for source locations where the
607 special variable or constant SYMBOL is read. Returns a list of function
608 name, definition-source pairs."
609 (collect-xref #.(position :references sb-c::*xref-kinds*) symbol))
611 (defun who-sets (symbol)
612 "Use the xref facility to search for source locations where the
613 special variable SYMBOL is written to. Returns a list of function name,
614 definition-source pairs."
615 (collect-xref #.(position :sets sb-c::*xref-kinds*) symbol))
617 (defun who-macroexpands (macro-name)
618 "Use the xref facility to search for source locations where the
619 macro MACRO-NAME is expanded. Returns a list of function name,
620 definition-source pairs."
621 (collect-xref #.(position :macroexpands sb-c::*xref-kinds*) macro-name))
623 ;;;; ALLOCATION INTROSPECTION
625 (defun allocation-information (object)
627 "Returns information about the allocation of OBJECT. Primary return value
628 indicates the general type of allocation: :IMMEDIATE, :HEAP, :STACK,
631 Possible secondary return value provides additional information about the
634 For :HEAP objects the secondary value is a plist:
637 Inficates the heap segment the object is allocated in.
640 Is the current generation of the object: 0 for nursery, 6 for pseudo-static
641 generation loaded from core. (GENCGC and :SPACE :DYNAMIC only.)
644 Indicates a \"large\" object subject to non-copying
645 promotion. (GENCGC and :SPACE :DYNAMIC only.)
648 Indicates that the page(s) on which the object resides are kept live due
649 to conservative references. Note that object may reside on a pinned page
650 even if :PINNED in NIL if the GC has not had the need to mark the the page
651 as pinned. (GENCGC and :SPACE :DYNAMIC only.)
653 For :STACK objects secondary value is the thread on whose stack the object is
656 Expected use-cases include introspection to gain insight into allocation and
657 GC behaviour and restricting memoization to heap-allocated arguments.
659 Experimental: interface subject to change."
660 ;; FIXME: Would be nice to provide the size of the object as well, though
661 ;; maybe that should be a separate function, and something like MAP-PARTS
662 ;; for mapping over parts of arbitrary objects so users can get "deep sizes"
663 ;; as well if they want to.
665 ;; FIXME: For the memoization use-case possibly we should also provide a
666 ;; simpler HEAP-ALLOCATED-P, since that doesn't require disabling the GC
667 ;; scanning threads for negative answers? Similarly, STACK-ALLOCATED-P for
668 ;; checking if an object has been stack-allocated by a given thread for
669 ;; testing purposes might not come amiss.
670 (if (typep object '(or fixnum character))
671 (values :immediate nil)
673 (sb-sys:without-gcing
674 ;; Disable GC so the object cannot move to another page while
675 ;; we have the address.
676 (let* ((addr (sb-kernel:get-lisp-obj-address object))
678 (cond ((< sb-vm:read-only-space-start addr
679 (* sb-vm:*read-only-space-free-pointer*
682 ((< sb-vm:static-space-start addr
683 (* sb-vm:*static-space-free-pointer*
686 ((< (sb-kernel:current-dynamic-space-start) addr
687 (sb-sys:sap-int (sb-kernel:dynamic-space-free-pointer)))
691 (if (eq :dynamic space)
692 (let ((index (sb-vm::find-page-index addr)))
693 (symbol-macrolet ((page (sb-alien:deref sb-vm::page-table index)))
694 (let ((flags (sb-alien:slot page 'sb-vm::flags)))
696 :generation (sb-alien:slot page 'sb-vm::gen)
697 :write-protected (logbitp 0 flags)
698 :pinned (logbitp 5 flags)
699 :large (logbitp 6 flags)))))
702 (list :space space))))))
704 (values :heap plist))
706 (let ((sap (sb-sys:int-sap (sb-kernel:get-lisp-obj-address object))))
707 ;; FIXME: Check other stacks as well.
709 (dolist (thread (sb-thread:list-all-threads))
710 (let ((c-start (sb-di::descriptor-sap
711 (sb-thread::%symbol-value-in-thread
712 'sb-vm:*control-stack-start*
714 (c-end (sb-di::descriptor-sap
715 (sb-thread::%symbol-value-in-thread
716 'sb-vm:*control-stack-end*
718 (when (and c-start c-end)
719 (when (and (sb-sys:sap<= c-start sap)
720 (sb-sys:sap< sap c-end))
721 (return-from allocation-information
722 (values :stack thread))))))
724 (when (sb-vm:control-stack-pointer-valid-p sap nil)
725 (return-from allocation-information
726 (values :stack sb-thread::*current-thread*))))
729 (provide 'sb-introspect)