1.0.0.18:
[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 "FUNCTION-ARGLIST"
28            "VALID-FUNCTION-NAME-P"
29            "FIND-DEFINITION-SOURCE"
30            "FIND-DEFINITION-SOURCES-BY-NAME"
31            "DEFINITION-SOURCE"
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"
40            "WHO-BINDS"
41            "WHO-CALLS"
42            "WHO-REFERENCES"
43            "WHO-SETS"
44            "WHO-MACROEXPANDS"))
45
46 (in-package :sb-introspect)
47
48 ;;;; Internal interface for SBCL debug info
49
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.
53 ;;;
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)
59
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."
64   'sb-c::debug-source)
65
66 (deftype debug-function ()
67   "Debug function represent static compile-time information about a function."
68   'sb-c::compiled-debug-fun)
69
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)))
75
76 (declaim (ftype (function (function) debug-source) function-debug-source))
77 (defun function-debug-source (function)
78   (debug-info-source (function-debug-info function)))
79
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))
83
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))
87
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))
91
92 ;;;; Finding definitions
93
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
109   (plist nil)
110   ;; Any extra metadata that the caller might be interested in. For
111   ;; example the specializers of the method whose definition-source this
112   ;; is.
113   (description nil :type list))
114
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:
119
120    (Public)
121    :CLASS
122    :COMPILER-MACRO
123    :CONDITION
124    :CONSTANT
125    :FUNCTION
126    :GENERIC-FUNCTION
127    :MACRO
128    :METHOD
129    :METHOD-COMBINATION
130    :PACKAGE
131    :SETF-EXPANDER
132    :STRUCTURE
133    :SYMBOL-MACRO
134    :TYPE
135    :VARIABLE
136
137    (Internal)
138    :OPTIMIZER
139    :SOURCE-TRANSFORM
140    :TRANSFORM
141    :VOP
142
143 If an unsupported TYPE is requested, the function will return NIL.
144 "
145   (flet ((listify (x)
146            (if (listp x)
147                x
148                (list x)))
149          (get-class (name)
150            (and (symbolp name)
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*)))
156              (if profile-info
157                  (sb-profile::profile-info-encapsulated-fun profile-info)
158                  (fdefinition name)))))
159     (listify
160      (case type
161        ((:variable)
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))))
165        ((:constant)
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))))
169        ((:symbol-macro)
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))))
173        ((:macro)
174         (when (and (symbolp name)
175                    (macro-function name))
176           (find-definition-source (macro-function name))))
177        ((:compiler-macro)
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)))))
188        ((:type)
189         (let ((expander-fun (sb-int:info :type :expander name)))
190           (when expander-fun
191             (find-definition-source expander-fun))))
192        ((:method)
193         (when (fboundp name)
194           (let ((fun (real-fdefinition name)))
195            (when (typep fun 'generic-function)
196              (loop for method in (sb-mop::generic-function-methods
197                                   fun)
198                 for source = (find-definition-source method)
199                 when source collect source)))))
200        ((:setf-expander)
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))))
206           (when expander
207             (sb-introspect:find-definition-source (if (symbolp expander)
208                                                       (symbol-function expander)
209                                                       expander)))))
210        ((:structure)
211         (let ((class (get-class name)))
212           (if class
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))))))
218        ((:condition :class)
219         (let ((class (get-class name)))
220           (when (and class
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                (ignore-errors (find-method #'sb-mop:find-method-combination
228                                            nil
229                                            (list (find-class 'generic-function)
230                                                  (list 'eql name)
231                                                  t)))))
232           (when combination-fun
233             (find-definition-source combination-fun))))
234        ((:package)
235         (when (symbolp name)
236           (let ((package (find-package name)))
237             (when package
238               (find-definition-source package)))))
239        ;; TRANSFORM and OPTIMIZER handling from swank-sbcl
240        ((:transform)
241         (when (symbolp name)
242           (let ((fun-info (sb-int:info :function :info name)))
243             (when fun-info
244               (loop for xform in (sb-c::fun-info-transforms fun-info)
245                     for source = (find-definition-source
246                                   (sb-c::transform-function xform))
247                     for typespec = (sb-kernel:type-specifier
248                                     (sb-c::transform-type xform))
249                     for note = (sb-c::transform-note xform)
250                     do (setf (definition-source-description source)
251                              (if (consp typespec)
252                                  (list (second typespec) note)
253                                  (list note)))
254                     collect source)))))
255        ((:optimizer)
256         (when (symbolp name)
257           (let ((fun-info (sb-int:info :function :info name)))
258             (when fun-info
259               (let ((otypes '((sb-c::fun-info-derive-type . sb-c:derive-type)
260                               (sb-c::fun-info-ltn-annotate . sb-c:ltn-annotate)
261                               (sb-c::fun-info-ltn-annotate . sb-c:ltn-annotate)
262                               (sb-c::fun-info-optimizer . sb-c:optimizer))))
263                 (loop for (reader . name) in otypes
264                       for fn = (funcall reader fun-info)
265                       when fn collect
266                       (let ((source (find-definition-source fn)))
267                         (setf (definition-source-description source)
268                               (list name))
269                         source)))))))
270        ((:vop)
271         (when (symbolp name)
272           (let ((fun-info (sb-int:info :function :info name)))
273             (when fun-info
274               (loop for vop in (sb-c::fun-info-templates fun-info)
275                     for source = (find-definition-source
276                                   (sb-c::vop-info-generator-function vop))
277                     do (setf (definition-source-description source)
278                              (list (sb-c::template-name vop)
279                                    (sb-c::template-note vop)))
280                     collect source)))))
281        ((:source-transform)
282         (when (symbolp name)
283           (let ((transform-fun (sb-int:info :function :source-transform name)))
284             (when transform-fun
285               (sb-introspect:find-definition-source transform-fun)))))
286        (t
287         nil)))))
288
289 (defun find-definition-source (object)
290   (typecase object
291     ((or sb-pcl::condition-class sb-pcl::structure-class)
292      (let ((classoid (sb-impl::find-classoid (class-name object))))
293        (when classoid
294          (let ((layout (sb-impl::classoid-layout classoid)))
295            (when layout
296              (translate-source-location
297               (sb-kernel::layout-source-location layout)))))))
298     (method-combination
299      (car
300       (find-definition-sources-by-name
301        (sb-pcl::method-combination-type-name object) :method-combination)))
302     (package
303      (translate-source-location (sb-impl::package-source-location object)))
304     (class
305      (translate-source-location (sb-pcl::definition-source object)))
306     ;; Use the PCL definition location information instead of the function
307     ;; debug-info for methods and generic functions. Sometimes the
308     ;; debug-info would point into PCL internals instead of the proper
309     ;; location.
310     (generic-function
311      (let ((source (translate-source-location
312                     (sb-pcl::definition-source object))))
313        (when source
314          (setf (definition-source-description source)
315                (list (sb-mop:generic-function-lambda-list object))))
316        source))
317     (method
318      (let ((source (translate-source-location
319                     (sb-pcl::definition-source object))))
320        (when source
321          (setf (definition-source-description source)
322                (append (method-qualifiers object)
323                        (sb-pcl::unparse-specializers
324                         (sb-mop:method-specializers object)))))
325        source))
326     #+sb-eval
327     (sb-eval:interpreted-function
328      (let ((source (translate-source-location
329                     (sb-eval:interpreted-function-source-location object))))
330        source))
331     (function
332      (cond ((struct-accessor-p object)
333             (find-definition-source
334              (struct-accessor-structure-class object)))
335            ((struct-predicate-p object)
336             (find-definition-source
337              (struct-predicate-structure-class object)))
338            (t
339             (find-function-definition-source object))))
340     (t
341      (error "Don't know how to retrive source location for a ~S~%"
342             (type-of object)))))
343
344 (defun find-function-definition-source (function)
345   (let* ((debug-info (function-debug-info function))
346          (debug-source (debug-info-source debug-info))
347          (debug-fun (debug-info-debug-function debug-info))
348          (tlf (if debug-fun (sb-c::compiled-debug-fun-tlf-number debug-fun))))
349     (make-definition-source
350      :pathname
351      (if (eql (sb-c::debug-source-from debug-source) :file)
352          (parse-namestring (sb-c::debug-source-name debug-source)))
353      :character-offset
354      (if tlf
355          (elt (sb-c::debug-source-start-positions debug-source) tlf))
356      ;; Unfortunately there is no proper source path available in the
357      ;; debug-source. FIXME: We could use sb-di:code-locations to get
358      ;; a full source path. -luke (12/Mar/2005)
359      :form-path (if tlf (list tlf))
360      :file-write-date (sb-c::debug-source-created debug-source)
361      :plist (sb-c::debug-source-plist debug-source))))
362
363 (defun translate-source-location (location)
364   (if location
365       (make-definition-source
366        :pathname (let ((n (sb-c:definition-source-location-namestring location)))
367                    (when n
368                      (parse-namestring n)))
369        :form-path
370        (let ((number (sb-c:definition-source-location-toplevel-form-number
371                          location)))
372          (when number
373            (list number)))
374        :plist (sb-c:definition-source-location-plist location))
375       (make-definition-source)))
376
377 ;;; This is kludgey.  We expect these functions (the underlying functions,
378 ;;; not the closures) to be in static space and so not move ever.
379 ;;; FIXME It's also possibly wrong: not all structures use these vanilla
380 ;;; accessors, e.g. when the :type option is used
381 (defvar *struct-slotplace-reader*
382   (sb-vm::%simple-fun-self #'definition-source-pathname))
383 (defvar *struct-slotplace-writer*
384   (sb-vm::%simple-fun-self #'(setf definition-source-pathname)))
385 (defvar *struct-predicate*
386   (sb-vm::%simple-fun-self #'definition-source-p))
387
388 (defun struct-accessor-p (function)
389   (let ((self (sb-vm::%simple-fun-self function)))
390     ;; FIXME there are other kinds of struct accessor.  Fill out this list
391     (member self (list *struct-slotplace-reader*
392                        *struct-slotplace-writer*))))
393
394 (defun struct-predicate-p (function)
395   (let ((self (sb-vm::%simple-fun-self function)))
396     ;; FIXME there may be other structure predicate functions
397     (member self (list *struct-predicate*))))
398
399 ;;; FIXME: maybe this should be renamed as FUNCTION-LAMBDA-LIST?
400 (defun function-arglist (function)
401   "Describe the lambda list for the extended function designator FUNCTION.
402 Works for special-operators, macros, simple functions,
403 interpreted functions, and generic functions.  Signals error if
404 not found"
405   (cond ((valid-function-name-p function)
406          (function-arglist (or (and (symbolp function)
407                                     (macro-function function))
408                                (fdefinition function))))
409         ((typep function 'generic-function)
410          (sb-pcl::generic-function-pretty-arglist function))
411         #+sb-eval
412         ((typep function 'sb-eval:interpreted-function)
413          (sb-eval:interpreted-function-lambda-list function))
414         (t (sb-impl::%simple-fun-arglist
415             (sb-impl::%closure-fun function)))))
416
417 (defun struct-accessor-structure-class (function)
418   (let ((self (sb-vm::%simple-fun-self function)))
419     (cond
420       ((member self (list *struct-slotplace-reader* *struct-slotplace-writer*))
421        (find-class
422         (sb-kernel::classoid-name
423          (sb-kernel::layout-classoid
424           (sb-kernel:%closure-index-ref function 1)))))
425       )))
426
427 (defun struct-predicate-structure-class (function)
428   (let ((self (sb-vm::%simple-fun-self function)))
429     (cond
430       ((member self (list *struct-predicate*))
431        (find-class
432         (sb-kernel::classoid-name
433          (sb-kernel::layout-classoid
434           (sb-kernel:%closure-index-ref function 0)))))
435       )))
436
437 ;;;; find callers/callees, liberated from Helmut Eller's code in SLIME
438
439 ;;; This interface is trmendously experimental.
440
441 ;;; For the moment I'm taking the view that FDEFN is an internal
442 ;;; object (one out of one CMUCL developer surveyed didn't know what
443 ;;; they were for), so these routines deal in FUNCTIONs
444
445 ;;; Find callers and callees by looking at the constant pool of
446 ;;; compiled code objects.  We assume every fdefn object in the
447 ;;; constant pool corresponds to a call to that function.  A better
448 ;;; strategy would be to use the disassembler to find actual
449 ;;; call-sites.
450
451 (defun find-function-callees (function)
452   "Return functions called by FUNCTION."
453   (let ((callees '()))
454     (map-code-constants
455      (sb-kernel:fun-code-header function)
456      (lambda (obj)
457        (when (sb-kernel:fdefn-p obj)
458          (push (sb-kernel:fdefn-fun obj)
459                callees))))
460     callees))
461
462
463 (defun find-function-callers (function &optional (spaces '(:read-only :static
464                                                            :dynamic)))
465   "Return functions which call FUNCTION, by searching SPACES for code objects"
466   (let ((referrers '()))
467     (map-caller-code-components
468      function
469      spaces
470      (lambda (code)
471        (let ((entry (sb-kernel:%code-entry-points  code)))
472          (cond ((not entry)
473                 (push (princ-to-string code) referrers))
474                (t
475                 (loop for e = entry then (sb-kernel::%simple-fun-next e)
476                       while e
477                       do (pushnew e referrers)))))))
478     referrers))
479
480 (declaim (inline map-code-constants))
481 (defun map-code-constants (code fn)
482   "Call FN for each constant in CODE's constant pool."
483   (check-type code sb-kernel:code-component)
484   (loop for i from sb-vm:code-constants-offset below
485         (sb-kernel:get-header-data code)
486         do (funcall fn (sb-kernel:code-header-ref code i))))
487
488 (declaim (inline map-allocated-code-components))
489 (defun map-allocated-code-components (spaces fn)
490   "Call FN for each allocated code component in one of SPACES.  FN
491 receives the object and its size as arguments.  SPACES should be a
492 list of the symbols :dynamic, :static, or :read-only."
493   (dolist (space spaces)
494     (sb-vm::map-allocated-objects
495      (lambda (obj header size)
496        (when (= sb-vm:code-header-widetag header)
497          (funcall fn obj size)))
498      space)))
499
500 (declaim (inline map-caller-code-components))
501 (defun map-caller-code-components (function spaces fn)
502   "Call FN for each code component with a fdefn for FUNCTION in its
503 constant pool."
504   (let ((function (coerce function 'function)))
505     (map-allocated-code-components
506      spaces
507      (lambda (obj size)
508        (declare (ignore size))
509        (map-code-constants
510         obj
511         (lambda (constant)
512           (when (and (sb-kernel:fdefn-p constant)
513                      (eq (sb-kernel:fdefn-fun constant)
514                          function))
515             (funcall fn obj))))))))
516
517 ;;; XREF facility
518
519 (defun get-simple-fun (functoid)
520   (etypecase functoid
521     (sb-kernel::fdefn
522      (get-simple-fun (sb-vm::fdefn-fun functoid)))
523     ((or null sb-impl::funcallable-instance)
524      nil)
525     (function
526      (sb-kernel::%closure-fun functoid))))
527
528 (defun collect-xref (kind-index wanted-name)
529   (let ((ret nil))
530     (dolist (env sb-c::*info-environment* ret)
531       ;; Loop through the infodb ...
532       (sb-c::do-info (env :class class :type type :name info-name
533                           :value value)
534         ;; ... looking for function or macro definitions
535         (when (and (eql class :function)
536                    (or (eql type :macro-function)
537                        (eql type :definition)))
538           ;; Get a simple-fun for the definition, and an xref array
539           ;; from the table if available.
540           (let* ((simple-fun (get-simple-fun value))
541                  (xrefs (when simple-fun
542                           (sb-vm::%simple-fun-xrefs simple-fun)))
543                  (array (when xrefs
544                           (aref xrefs kind-index))))
545             ;; Loop through the name/path xref entries in the table
546             (loop for i from 0 below (length array) by 2
547                   for xref-name = (aref array i)
548                   for xref-path = (aref array (1+ i))
549                   do (when (eql xref-name wanted-name)
550                        (let ((source-location
551                               (find-function-definition-source simple-fun)))
552                          ;; Use the more accurate source path from
553                          ;; the xref entry.
554                          (setf (definition-source-form-path source-location)
555                                xref-path)
556                          (push (cons info-name source-location)
557                                ret))))))))))
558
559 (defun who-calls (function-name)
560   "Use the xref facility to search for source locations where the
561 global function named FUNCTION-NAME is called. Returns a list of
562 function name, definition-source pairs."
563   (collect-xref #.(position :calls sb-c::*xref-kinds*) function-name))
564
565 (defun who-binds (symbol)
566   "Use the xref facility to search for source locations where the
567 special variable SYMBOL is rebound. Returns a list of function name,
568 definition-source pairs."
569   (collect-xref #.(position :binds sb-c::*xref-kinds*) symbol))
570
571 (defun who-references (symbol)
572   "Use the xref facility to search for source locations where the
573 special variable or constant SYMBOL is read. Returns a list of function
574 name, definition-source pairs."
575   (collect-xref #.(position :references sb-c::*xref-kinds*) symbol))
576
577 (defun who-sets (symbol)
578   "Use the xref facility to search for source locations where the
579 special variable SYMBOL is written to. Returns a list of function name,
580 definition-source pairs."
581   (collect-xref #.(position :sets sb-c::*xref-kinds*) symbol))
582
583 (defun who-macroexpands (macro-name)
584   "Use the xref facility to search for source locations where the
585 macro MACRO-NAME is expanded. Returns a list of function name,
586 definition-source pairs."
587   (collect-xref #.(position :macroexpands sb-c::*xref-kinds*) macro-name))
588
589 (provide 'sb-introspect)