1.0.29.22: smattering of DOCUMENTATION cleanups
[sbcl.git] / contrib / sb-introspect / sb-introspect.lisp
1 ;;; introspection library
2
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
7
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.
11
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.
17
18 ;;; TODO
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?
23 ;;; 4) FIXMEs
24
25 (defpackage :sb-introspect
26   (:use "CL")
27   (:export "ALLOCATION-INFORMATION"
28            "FUNCTION-ARGLIST"
29            "FUNCTION-LAMBDA-LIST"
30            "DEFTYPE-LAMBDA-LIST"
31            "VALID-FUNCTION-NAME-P"
32            "FIND-DEFINITION-SOURCE"
33            "FIND-DEFINITION-SOURCES-BY-NAME"
34            "DEFINITION-SOURCE"
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"
43            "WHO-BINDS"
44            "WHO-CALLS"
45            "WHO-REFERENCES"
46            "WHO-SETS"
47            "WHO-MACROEXPANDS"))
48
49 (in-package :sb-introspect)
50
51 ;;;; Internal interface for SBCL debug info
52
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.
56 ;;;
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)
62
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."
67   'sb-c::debug-source)
68
69 (deftype debug-function ()
70   "Debug function represent static compile-time information about a function."
71   'sb-c::compiled-debug-fun)
72
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)))
78
79 (declaim (ftype (function (function) debug-source) function-debug-source))
80 (defun function-debug-source (function)
81   (debug-info-source (function-debug-info function)))
82
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))
86
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))
90
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))
94
95 ;;;; Finding definitions
96
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
112   (plist nil)
113   ;; Any extra metadata that the caller might be interested in. For
114   ;; example the specializers of the method whose definition-source this
115   ;; is.
116   (description nil :type list))
117
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:
122
123    (Public)
124    :CLASS
125    :COMPILER-MACRO
126    :CONDITION
127    :CONSTANT
128    :FUNCTION
129    :GENERIC-FUNCTION
130    :MACRO
131    :METHOD
132    :METHOD-COMBINATION
133    :PACKAGE
134    :SETF-EXPANDER
135    :STRUCTURE
136    :SYMBOL-MACRO
137    :TYPE
138    :VARIABLE
139
140    (Internal)
141    :OPTIMIZER
142    :SOURCE-TRANSFORM
143    :TRANSFORM
144    :VOP
145
146 If an unsupported TYPE is requested, the function will return NIL.
147 "
148   (flet ((listify (x)
149            (if (listp x)
150                x
151                (list x)))
152          (get-class (name)
153            (and (symbolp name)
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*)))
159              (if profile-info
160                  (sb-profile::profile-info-encapsulated-fun profile-info)
161                  (fdefinition name)))))
162     (listify
163      (case type
164        ((:variable)
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))))
168        ((:constant)
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))))
172        ((:symbol-macro)
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))))
176        ((:macro)
177         (when (and (symbolp name)
178                    (macro-function name))
179           (find-definition-source (macro-function name))))
180        ((:compiler-macro)
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)))))
191        ((:type)
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)))
195           (if loc
196               (translate-source-location loc)
197               (let ((expander-fun (sb-int:info :type :expander name)))
198                 (when expander-fun
199                   (find-definition-source expander-fun))))))
200        ((:method)
201         (when (fboundp name)
202           (let ((fun (real-fdefinition name)))
203            (when (typep fun 'generic-function)
204              (loop for method in (sb-mop::generic-function-methods
205                                   fun)
206                 for source = (find-definition-source method)
207                 when source collect source)))))
208        ((:setf-expander)
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))))
214           (when expander
215             (sb-introspect:find-definition-source (if (symbolp expander)
216                                                       (symbol-function expander)
217                                                       expander)))))
218        ((:structure)
219         (let ((class (get-class name)))
220           (if class
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))))))
226        ((:condition :class)
227         (let ((class (get-class name)))
228           (when (and class
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
236                             nil
237                             (list (find-class 'generic-function)
238                                   (list 'eql name)
239                                   t)
240                             nil)))
241           (when combination-fun
242             (find-definition-source combination-fun))))
243        ((:package)
244         (when (symbolp name)
245           (let ((package (find-package name)))
246             (when package
247               (find-definition-source package)))))
248        ;; TRANSFORM and OPTIMIZER handling from swank-sbcl
249        ((:transform)
250         (when (symbolp name)
251           (let ((fun-info (sb-int:info :function :info name)))
252             (when fun-info
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)
260                              (if (consp typespec)
261                                  (list (second typespec) note)
262                                  (list note)))
263                     collect source)))))
264        ((:optimizer)
265         (when (symbolp name)
266           (let ((fun-info (sb-int:info :function :info name)))
267             (when fun-info
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)
274                       when fn collect
275                       (let ((source (find-definition-source fn)))
276                         (setf (definition-source-description source)
277                               (list name))
278                         source)))))))
279        ((:vop)
280         (when (symbolp name)
281           (let ((fun-info (sb-int:info :function :info name)))
282             (when fun-info
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)))
289                     collect source)))))
290        ((:source-transform)
291         (when (symbolp name)
292           (let ((transform-fun (sb-int:info :function :source-transform name)))
293             (when transform-fun
294               (sb-introspect:find-definition-source transform-fun)))))
295        (t
296         nil)))))
297
298 (defun find-definition-source (object)
299   (typecase object
300     ((or sb-pcl::condition-class sb-pcl::structure-class)
301      (let ((classoid (sb-impl::find-classoid (class-name object))))
302        (when classoid
303          (let ((layout (sb-impl::classoid-layout classoid)))
304            (when layout
305              (translate-source-location
306               (sb-kernel::layout-source-location layout)))))))
307     (method-combination
308      (car
309       (find-definition-sources-by-name
310        (sb-pcl::method-combination-type-name object) :method-combination)))
311     (package
312      (translate-source-location (sb-impl::package-source-location object)))
313     (class
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
318     ;; location.
319     (generic-function
320      (let ((source (translate-source-location
321                     (sb-pcl::definition-source object))))
322        (when source
323          (setf (definition-source-description source)
324                (list (sb-mop:generic-function-lambda-list object))))
325        source))
326     (method
327      (let ((source (translate-source-location
328                     (sb-pcl::definition-source object))))
329        (when source
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)))))
337        source))
338     #+sb-eval
339     (sb-eval:interpreted-function
340      (let ((source (translate-source-location
341                     (sb-eval:interpreted-function-source-location object))))
342        source))
343     (function
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)))
350            (t
351             (find-function-definition-source object))))
352     ((or condition standard-object structure-object)
353      (find-definition-source (class-of object)))
354     (t
355      (error "Don't know how to retrieve source location for a ~S~%"
356             (type-of object)))))
357
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
364      :pathname
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)))
373      :character-offset
374      (if tlf
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))))
382
383 (defun translate-source-location (location)
384   (if location
385       (make-definition-source
386        :pathname (let ((n (sb-c:definition-source-location-namestring location)))
387                    (when n
388                      (parse-namestring n)))
389        :form-path
390        (let ((number (sb-c:definition-source-location-toplevel-form-number
391                          location)))
392          (when number
393            (list number)))
394        :plist (sb-c:definition-source-location-plist location))
395       (make-definition-source)))
396
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))
407
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*))))
413
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*))))
418
419 (defun function-arglist (function)
420   "Deprecated alias for FUNCTION-LAMBDA-LIST."
421   (function-lambda-list function))
422
423 (define-compiler-macro function-arglist (function)
424   (sb-int:deprecation-warning 'function-arglist 'function-lambda-list)
425   `(function-lambda-list ,function))
426
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))
438         #+sb-eval
439         ((typep function 'sb-eval:interpreted-function)
440          (sb-eval:interpreted-function-lambda-list function))
441         (t
442          (sb-kernel:%simple-fun-arglist (sb-kernel:%fun-fun function)))))
443
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
447 value."
448   (sb-int:info :type :lambda-list typespec-operator))
449
450 (defun struct-accessor-structure-class (function)
451   (let ((self (sb-vm::%simple-fun-self function)))
452     (cond
453       ((member self (list *struct-slotplace-reader* *struct-slotplace-writer*))
454        (find-class
455         (sb-kernel::classoid-name
456          (sb-kernel::layout-classoid
457           (sb-kernel:%closure-index-ref function 1)))))
458       )))
459
460 (defun struct-predicate-structure-class (function)
461   (let ((self (sb-vm::%simple-fun-self function)))
462     (cond
463       ((member self (list *struct-predicate*))
464        (find-class
465         (sb-kernel::classoid-name
466          (sb-kernel::layout-classoid
467           (sb-kernel:%closure-index-ref function 0)))))
468       )))
469
470 ;;;; find callers/callees, liberated from Helmut Eller's code in SLIME
471
472 ;;; This interface is trmendously experimental.
473
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
477
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
482 ;;; call-sites.
483
484 (defun find-function-callees (function)
485   "Return functions called by FUNCTION."
486   (let ((callees '()))
487     (map-code-constants
488      (sb-kernel:fun-code-header function)
489      (lambda (obj)
490        (when (sb-kernel:fdefn-p obj)
491          (push (sb-kernel:fdefn-fun obj)
492                callees))))
493     callees))
494
495
496 (defun find-function-callers (function &optional (spaces '(:read-only :static
497                                                            :dynamic)))
498   "Return functions which call FUNCTION, by searching SPACES for code objects"
499   (let ((referrers '()))
500     (map-caller-code-components
501      function
502      spaces
503      (lambda (code)
504        (let ((entry (sb-kernel:%code-entry-points  code)))
505          (cond ((not entry)
506                 (push (princ-to-string code) referrers))
507                (t
508                 (loop for e = entry then (sb-kernel::%simple-fun-next e)
509                       while e
510                       do (pushnew e referrers)))))))
511     referrers))
512
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))))
520
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)))
531      space
532      t)))
533
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
537 constant pool."
538   (let ((function (coerce function 'function)))
539     (map-allocated-code-components
540      spaces
541      (lambda (obj size)
542        (declare (ignore size))
543        (map-code-constants
544         obj
545         (lambda (constant)
546           (when (and (sb-kernel:fdefn-p constant)
547                      (eq (sb-kernel:fdefn-fun constant)
548                          function))
549             (funcall fn obj))))))))
550
551 ;;; XREF facility
552
553 (defun get-simple-fun (functoid)
554   (etypecase functoid
555     (sb-kernel::fdefn
556      (get-simple-fun (sb-vm::fdefn-fun functoid)))
557     ((or null sb-impl::funcallable-instance)
558      nil)
559     (function
560      (sb-kernel::%fun-fun functoid))))
561
562 (defun collect-xref (kind-index wanted-name)
563   (let ((ret nil))
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
567                           :value value)
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-vm::%simple-fun-xrefs simple-fun)))
577                  (array (when xrefs
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
587                          ;; the xref entry.
588                          (setf (definition-source-form-path source-location)
589                                xref-path)
590                          (push (cons info-name source-location)
591                                ret))))))))))
592
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))
598
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))
604
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))
610
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))
616
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))
622
623 ;;;; ALLOCATION INTROSPECTION
624
625 (defun allocation-information (object)
626   #+sb-doc
627   "Returns information about the allocation of OBJECT. Primary return value
628 indicates the general type of allocation: :IMMEDIATE, :HEAP, :STACK,
629 or :FOREIGN.
630
631 Possible secondary return value provides additional information about the
632 allocation.
633
634 For :HEAP objects the secondary value is a plist:
635
636   :SPACE
637     Inficates the heap segment the object is allocated in.
638
639   :GENERATION
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.)
642
643   :LARGE
644     Indicates a \"large\" object subject to non-copying
645     promotion. (GENCGC and :SPACE :DYNAMIC only.)
646
647   :PINNED
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.)
652
653 For :STACK objects secondary value is the thread on whose stack the object is
654 allocated.
655
656 Expected use-cases include introspection to gain insight into allocation and
657 GC behaviour and restricting memoization to heap-allocated arguments.
658
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.
664   ;;
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)
672       (let ((plist
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))
677                       (space
678                        (cond ((< sb-vm:read-only-space-start addr
679                                  (* sb-vm:*read-only-space-free-pointer*
680                                     sb-vm:n-word-bytes))
681                               :read-only)
682                              ((< sb-vm:static-space-start addr
683                                  (* sb-vm:*static-space-free-pointer*
684                                     sb-vm:n-word-bytes))
685                               :static)
686                              ((< (sb-kernel:current-dynamic-space-start) addr
687                                  (sb-sys:sap-int (sb-kernel:dynamic-space-free-pointer)))
688                               :dynamic))))
689                  (when space
690                    #+gencgc
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)))
695                              (list :space space
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)))))
700                        (list :space space))
701                    #-gencgc
702                    (list :space space))))))
703         (cond (plist
704                (values :heap plist))
705               (t
706                (let ((sap (sb-sys:int-sap (sb-kernel:get-lisp-obj-address object))))
707                  ;; FIXME: Check other stacks as well.
708                  #+sb-thread
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*
713                                     thread)))
714                          (c-end (sb-di::descriptor-sap
715                                  (sb-thread::%symbol-value-in-thread
716                                   'sb-vm:*control-stack-end*
717                                   thread))))
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))))))
723                  #-sb-thread
724                  (when (sb-vm:control-stack-pointer-valid-p sap nil)
725                    (return-from allocation-information
726                      (values :stack sb-thread::*current-thread*))))
727                :foreign)))))
728
729 (provide 'sb-introspect)