4c0b7be4bdb7afc936f0915cd68ffca2a3d3fb42
[sbcl.git] / src / code / debug-info.lisp
1 ;;;; structures used for recording debugger information
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
11
12 (in-package "SB!C")
13 \f
14 ;;;; SC-OFFSETs
15 ;;;;
16 ;;;; We represent the place where some value is stored with a SC-OFFSET,
17 ;;;; which is the SC number and offset encoded as an integer.
18
19 (defconstant-eqx sc-offset-scn-byte (byte 5 0) #'equalp)
20 (defconstant-eqx sc-offset-offset-byte (byte 22 5) #'equalp)
21 (def!type sc-offset () '(unsigned-byte 27))
22
23 (defmacro make-sc-offset (scn offset)
24   `(dpb ,scn sc-offset-scn-byte
25         (dpb ,offset sc-offset-offset-byte 0)))
26
27 (defmacro sc-offset-scn (sco) `(ldb sc-offset-scn-byte ,sco))
28 (defmacro sc-offset-offset (sco) `(ldb sc-offset-offset-byte ,sco))
29 \f
30 ;;;; flags for compiled debug variables
31
32 ;;; FIXME: old CMU CL representation follows:
33 ;;;    Compiled debug variables are in a packed binary representation in the
34 ;;; DEBUG-FUN-VARIABLES:
35 ;;;    single byte of boolean flags:
36 ;;;     uninterned name
37 ;;;        packaged name
38 ;;;     environment-live
39 ;;;     has distinct save location
40 ;;;     has ID (name not unique in this fun)
41 ;;;     minimal debug-info argument (name generated as ARG-0, ...)
42 ;;;     deleted: placeholder for unused minimal argument
43 ;;;    [name length in bytes (as var-length integer), if not minimal]
44 ;;;    [...name bytes..., if not minimal]
45 ;;;    [if packaged, var-length integer that is package name length]
46 ;;;     ...package name bytes...]
47 ;;;    [If has ID, ID as var-length integer]
48 ;;;    SC-Offset of primary location (as var-length integer)
49 ;;;    [If has save SC, SC-Offset of save location (as var-length integer)]
50
51 ;;; FIXME: The first two are no longer used in SBCL.
52 ;;;(defconstant compiled-debug-var-uninterned           #b00000001)
53 ;;;(defconstant compiled-debug-var-packaged             #b00000010)
54 (defconstant compiled-debug-var-environment-live        #b00000100)
55 (defconstant compiled-debug-var-save-loc-p              #b00001000)
56 (defconstant compiled-debug-var-id-p                    #b00010000)
57 (defconstant compiled-debug-var-minimal-p               #b00100000)
58 (defconstant compiled-debug-var-deleted-p               #b01000000)
59 \f
60 ;;;; compiled debug blocks
61 ;;;;
62 ;;;;    Compiled debug blocks are in a packed binary representation in the
63 ;;;; DEBUG-FUN-BLOCKS:
64 ;;;;    number of successors + bit flags (single byte)
65 ;;;;    elsewhere-p
66 ;;;;    ...ordinal number of each successor in the function's blocks vector...
67 ;;;;    number of locations in this block
68 ;;;;    kind of first location (single byte)
69 ;;;;    delta from previous PC (or from 0 if first location in function.)
70 ;;;;    [offset of first top-level form, if no function TLF-NUMBER]
71 ;;;;    form number of first source form
72 ;;;;    first live mask (length in bytes determined by number of VARIABLES)
73 ;;;;    ...more <kind, delta, top-level form offset, form-number, live-set>
74 ;;;;       tuples...
75
76 (defconstant-eqx compiled-debug-block-nsucc-byte (byte 2 0) #'equalp)
77 (defconstant compiled-debug-block-elsewhere-p #b00000100)
78
79 (defconstant-eqx compiled-code-location-kind-byte (byte 3 0) #'equalp)
80 (defparameter *compiled-code-location-kinds*
81   #(:unknown-return :known-return :internal-error :non-local-exit
82     :block-start :call-site :single-value-return :non-local-entry))
83 \f
84 ;;;; DEBUG-FUN objects
85
86 (def!struct (debug-fun (:constructor nil)))
87
88 (def!struct (compiled-debug-fun (:include debug-fun)
89                                 #-sb-xc-host (:pure t))
90   ;; The name of this function. If from a DEFUN, etc., then this is the
91   ;; function name, otherwise it is a descriptive string.
92   (name (required-argument) :type (or simple-string cons symbol))
93   ;; The kind of function (same as FUNCTIONAL-KIND):
94   (kind nil :type (member nil :optional :external :top-level :cleanup))
95   ;; a description of variable locations for this function, in alphabetical
96   ;; order by name; or NIL if no information is available
97   ;;
98   ;; The variable entries are alphabetically ordered. This ordering is used in
99   ;; lifetime info to refer to variables: the first entry is 0, the second
100   ;; entry is 1, etc. Variable numbers are *not* the byte index at which the
101   ;; representation of the location starts.
102   ;;
103   ;; Each entry is:
104   ;;   * a FLAGS value, which is a FIXNUM with various
105   ;;     COMPILED-DEBUG-FUN-FOO bits set
106   ;;   * the symbol which names this variable, unless debug info is minimal
107   ;;   * the variable ID, when it has one
108   ;;   * SC-offset of primary location, if it has one
109   ;;   * SC-offset of save location, if it has one
110   (variables nil :type (or simple-vector null))
111   ;; a vector of the packed binary representation of the
112   ;; COMPILED-DEBUG-BLOCKs in this function, in the order that the
113   ;; blocks were emitted. The first block is the start of the
114   ;; function. This slot may be NIL to save space.
115   ;;
116   ;; FIXME: The "packed binary representation" description in the comment
117   ;; above is the same as the description of the old representation of
118   ;; VARIABLES which doesn't work properly in SBCL (because it doesn't
119   ;; transform correctly under package renaming). Check whether this slot's
120   ;; data might have the same problem that that slot's data did.
121   (blocks nil :type (or (simple-array (unsigned-byte 8) (*)) null))
122   ;; If all code locations in this function are in the same top-level form,
123   ;; then this is the number of that form, otherwise NIL. If NIL, then each
124   ;; code location represented in the BLOCKS specifies the TLF number.
125   (tlf-number nil :type (or index null))
126   ;; A vector describing the variables that the argument values are stored in
127   ;; within this function. The locations are represented by the ordinal number
128   ;; of the entry in the VARIABLES slot value. The locations are in the order
129   ;; that the arguments are actually passed in, but special marker symbols can
130   ;; be interspersed to indicate the original call syntax:
131   ;;
132   ;; DELETED
133   ;;    There was an argument to the function in this position, but it was
134   ;;    deleted due to lack of references. The value cannot be recovered.
135   ;;
136   ;; SUPPLIED-P
137   ;;    The following location is the supplied-p value for the preceding
138   ;;    keyword or optional.
139   ;;
140   ;; OPTIONAL-ARGS
141   ;;    Indicates that following unqualified args are optionals, not required.
142   ;;
143   ;; REST-ARG
144   ;;    The following location holds the list of rest args.
145   ;;
146   ;; MORE-ARG
147   ;;    The following two locations are the more arg context and count.
148   ;;
149   ;; <any other symbol>
150   ;;    The following location is the value of the &KEY argument with the
151   ;;    specified name.
152   ;;
153   ;; This may be NIL to save space. If no symbols are present, then this will
154   ;; be represented with an I-vector with sufficiently large element type. If
155   ;; this is :MINIMAL, then this means that the VARIABLES are all required
156   ;; arguments, and are in the order they appear in the VARIABLES vector. In
157   ;; other words, :MINIMAL stands in for a vector where every element holds its
158   ;; index.
159   (arguments nil :type (or (simple-array * (*)) (member :minimal nil)))
160   ;; There are three alternatives for this slot:
161   ;;
162   ;; A vector
163   ;;    A vector of SC-OFFSETS describing the return locations. The
164   ;;    vector element type is chosen to hold the largest element.
165   ;;
166   ;; :Standard
167   ;;    The function returns using the standard unknown-values convention.
168   ;;
169   ;; :Fixed
170   ;;    The function returns using the fixed-values convention, but
171   ;;    in order to save space, we elected not to store a vector.
172   (returns :fixed :type (or (simple-array * (*)) (member :standard :fixed)))
173   ;; SC-Offsets describing where the return PC and return FP are kept.
174   (return-pc (required-argument) :type sc-offset)
175   (old-fp (required-argument) :type sc-offset)
176   ;; SC-Offset for the number stack FP in this function, or NIL if no NFP
177   ;; allocated.
178   (nfp nil :type (or sc-offset null))
179   ;; The earliest PC in this function at which the environment is properly
180   ;; initialized (arguments moved from passing locations, etc.)
181   (start-pc (required-argument) :type index)
182   ;; The start of elsewhere code for this function (if any.)
183   (elsewhere-pc (required-argument) :type index))
184 \f
185 ;;;; minimal debug function
186
187 ;;; The minimal debug info format compactly represents debug-info for some
188 ;;; cases where the other debug info (variables, blocks) is small enough so
189 ;;; that the per-function overhead becomes relatively large. The minimal
190 ;;; debug-info format can represent any function at level 0, and any fixed-arg
191 ;;; function at level 1.
192 ;;;
193 ;;; In the minimal format, the debug functions and function map are
194 ;;; packed into a single byte-vector which is placed in the
195 ;;; COMPILED-DEBUG-INFO-FUN-MAP. Because of this, all functions in a
196 ;;; component must be representable in minimal format for any function
197 ;;; to actually be dumped in minimal format. The vector is a sequence
198 ;;; of records in this format:
199 ;;;    name representation + kind + return convention (single byte)
200 ;;;    bit flags (single byte)
201 ;;;     setf, nfp, variables
202 ;;;    [package name length (as var-length int), if name is packaged]
203 ;;;    [...package name bytes, if name is packaged]
204 ;;;    [name length (as var-length int), if there is a name]
205 ;;;    [...name bytes, if there is a name]
206 ;;;    [variables length (as var-length int), if variables flag]
207 ;;;    [...bytes holding variable descriptions]
208 ;;;     If variables are dumped (level 1), then the variables are all
209 ;;;     arguments (in order) with the minimal-arg bit set.
210 ;;;    [If returns is specified, then the number of return values]
211 ;;;    [...sequence of var-length ints holding sc-offsets of the return
212 ;;;     value locations, if fixed return values are specified.]
213 ;;;    return-pc location sc-offset (as var-length int)
214 ;;;    old-fp location sc-offset (as var-length int)
215 ;;;    [nfp location sc-offset (as var-length int), if nfp flag]
216 ;;;    code-start-pc (as a var-length int)
217 ;;;     This field implicitly encodes start of this function's code in the
218 ;;;     function map, as a delta from the previous function's code start.
219 ;;;     If the first function in the component, then this is the delta from
220 ;;;     0 (i.e. the absolute offset.)
221 ;;;    start-pc (as a var-length int)
222 ;;;     This encodes the environment start PC as an offset from the
223 ;;;     code-start PC.
224 ;;;    elsewhere-pc
225 ;;;     This encodes the elsewhere code start for this function, as a delta
226 ;;;     from the previous function's elsewhere code start. (i.e. the
227 ;;;     encoding is the same as for code-start-pc.)
228
229 ;;; ### For functions with XEPs, name could be represented more simply
230 ;;; and compactly as some sort of info about with how to find the
231 ;;; FUNCTION-ENTRY that this is a function for. Actually, you really
232 ;;; hardly need any info. You can just chain through the functions in
233 ;;; the component until you find the right one. Well, I guess you need
234 ;;; to at least know which function is an XEP for the real function
235 ;;; (which would be useful info anyway).
236 \f
237 ;;;; debug source
238
239 (def!struct (debug-source #-sb-xc-host (:pure t))
240   ;; This slot indicates where the definition came from:
241   ;;    :FILE - from a file (i.e. COMPILE-FILE)
242   ;;    :LISP - from Lisp (i.e. COMPILE)
243   (from (required-argument) :type (member :file :lisp))
244   ;; If :FILE, the file name, if :LISP or :STREAM, then a vector of
245   ;; the top-level forms. When from COMPILE, form 0 is #'(LAMBDA ...).
246   (name nil)
247   ;; the universal time that the source was written, or NIL if
248   ;; unavailable
249   (created nil :type (or unsigned-byte null))
250   ;; the universal time that the source was compiled
251   (compiled (required-argument) :type unsigned-byte)
252   ;; the source path root number of the first form read from this
253   ;; source (i.e. the total number of forms converted previously in
254   ;; this compilation)
255   (source-root 0 :type index)
256   ;; The FILE-POSITIONs of the truly top-level forms read from this
257   ;; file (if applicable). The vector element type will be chosen to
258   ;; hold the largest element. May be null to save space, or if
259   ;; :DEBUG-SOURCE-FORM is :LISP.
260   (start-positions nil :type (or (simple-array * (*)) null))
261   ;; If from :LISP, this is the function whose source is form 0.
262   (info nil))
263 \f
264 ;;;; DEBUG-INFO structures
265
266 (def!struct debug-info
267   ;; Some string describing something about the code in this component.
268   (name (required-argument) :type simple-string)
269   ;; A list of DEBUG-SOURCE structures describing where the code for this
270   ;; component came from, in the order that they were read.
271   ;;
272   ;; KLUDGE: comment from CMU CL:
273   ;;   *** NOTE: the offset of this slot is wired into the fasl dumper 
274   ;;   *** so that it can backpatch the source info when compilation
275   ;;   *** is complete.
276   (source nil :type list))
277
278 (def!struct (compiled-debug-info
279              (:include debug-info)
280              #-sb-xc-host (:pure t))
281   ;; a simple-vector of alternating DEBUG-FUN objects and fixnum
282   ;; PCs, used to map PCs to functions, so that we can figure out what
283   ;; function we were running in. Each function is valid between the
284   ;; PC before it (inclusive) and the PC after it (exclusive). The PCs
285   ;; are in sorted order, to allow binary search. We omit the first
286   ;; and last PC, since their values are 0 and the length of the code
287   ;; vector.
288   ;;
289   ;; KLUDGE: PC's can't always be represented by FIXNUMs, unless we're
290   ;; always careful to put our code in low memory. Is that how it
291   ;; works? Would this break if we used a more general memory map? --
292   ;; WHN 20000120
293   (fun-map (required-argument) :type simple-vector :read-only t))