e89294d304673e49b7dfcde9552704fb64c5e7f4
[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-FUNCTION-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-FUNCTION-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-FUNCTION objects
85
86 (def!struct (debug-function (:constructor nil)))
87
88 (def!struct (compiled-debug-function (:include debug-function)
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-FUNCTION-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 COMPILED-DEBUG-BLOCKs
112   ;; in this function, in the order that the blocks were emitted. The first
113   ;; block is the start of the function. This slot may be NIL to save space.
114   ;;
115   ;; FIXME: The "packed binary representation" description in the comment
116   ;; above is the same as the description of the old representation of
117   ;; VARIABLES which doesn't work properly in SBCL (because it doesn't
118   ;; transform correctly under package renaming). Check whether this slot's
119   ;; data might have the same problem that that slot's data did.
120   (blocks nil :type (or (simple-array (unsigned-byte 8) (*)) null))
121   ;; If all code locations in this function are in the same top-level form,
122   ;; then this is the number of that form, otherwise NIL. If NIL, then each
123   ;; code location represented in the BLOCKS specifies the TLF number.
124   (tlf-number nil :type (or index null))
125   ;; A vector describing the variables that the argument values are stored in
126   ;; within this function. The locations are represented by the ordinal number
127   ;; of the entry in the VARIABLES slot value. The locations are in the order
128   ;; that the arguments are actually passed in, but special marker symbols can
129   ;; be interspersed to indicate the original call syntax:
130   ;;
131   ;; DELETED
132   ;;    There was an argument to the function in this position, but it was
133   ;;    deleted due to lack of references. The value cannot be recovered.
134   ;;
135   ;; SUPPLIED-P
136   ;;    The following location is the supplied-p value for the preceding
137   ;;    keyword or optional.
138   ;;
139   ;; OPTIONAL-ARGS
140   ;;    Indicates that following unqualified args are optionals, not required.
141   ;;
142   ;; REST-ARG
143   ;;    The following location holds the list of rest args.
144   ;;
145   ;; MORE-ARG
146   ;;    The following two locations are the more arg context and count.
147   ;;
148   ;; <any other symbol>
149   ;;    The following location is the value of the keyword argument with the
150   ;;    specified name.
151   ;;
152   ;; This may be NIL to save space. If no symbols are present, then this will
153   ;; be represented with an I-vector with sufficiently large element type. If
154   ;; this is :MINIMAL, then this means that the VARIABLES are all required
155   ;; arguments, and are in the order they appear in the VARIABLES vector. In
156   ;; other words, :MINIMAL stands in for a vector where every element holds its
157   ;; index.
158   (arguments nil :type (or (simple-array * (*)) (member :minimal nil)))
159   ;; There are three alternatives for this slot:
160   ;;
161   ;; A vector
162   ;;    A vector of SC-OFFSETS describing the return locations. The
163   ;;    vector element type is chosen to hold the largest element.
164   ;;
165   ;; :Standard
166   ;;    The function returns using the standard unknown-values convention.
167   ;;
168   ;; :Fixed
169   ;;    The function returns using the fixed-values convention, but
170   ;;    in order to save space, we elected not to store a vector.
171   (returns :fixed :type (or (simple-array * (*)) (member :standard :fixed)))
172   ;; SC-Offsets describing where the return PC and return FP are kept.
173   (return-pc (required-argument) :type sc-offset)
174   (old-fp (required-argument) :type sc-offset)
175   ;; SC-Offset for the number stack FP in this function, or NIL if no NFP
176   ;; allocated.
177   (nfp nil :type (or sc-offset null))
178   ;; The earliest PC in this function at which the environment is properly
179   ;; initialized (arguments moved from passing locations, etc.)
180   (start-pc (required-argument) :type index)
181   ;; The start of elsewhere code for this function (if any.)
182   (elsewhere-pc (required-argument) :type index))
183 \f
184 ;;;; minimal debug function
185
186 ;;; The minimal debug info format compactly represents debug-info for some
187 ;;; cases where the other debug info (variables, blocks) is small enough so
188 ;;; that the per-function overhead becomes relatively large. The minimal
189 ;;; debug-info format can represent any function at level 0, and any fixed-arg
190 ;;; function at level 1.
191 ;;;
192 ;;; In the minimal format, the debug functions and function map are packed into
193 ;;; a single byte-vector which is placed in the
194 ;;; COMPILED-DEBUG-INFO-FUNCTION-MAP. Because of this, all functions in a
195 ;;; component must be representable in minimal format for any function to
196 ;;; actually be dumped in minimal format. The vector is a sequence of records
197 ;;; in this format:
198 ;;;    name representation + kind + return convention (single byte)
199 ;;;    bit flags (single byte)
200 ;;;     setf, nfp, variables
201 ;;;    [package name length (as var-length int), if name is packaged]
202 ;;;    [...package name bytes, if name is packaged]
203 ;;;    [name length (as var-length int), if there is a name]
204 ;;;    [...name bytes, if there is a name]
205 ;;;    [variables length (as var-length int), if variables flag]
206 ;;;    [...bytes holding variable descriptions]
207 ;;;     If variables are dumped (level 1), then the variables are all
208 ;;;     arguments (in order) with the minimal-arg bit set.
209 ;;;    [If returns is specified, then the number of return values]
210 ;;;    [...sequence of var-length ints holding sc-offsets of the return
211 ;;;     value locations, if fixed return values are specified.]
212 ;;;    return-pc location sc-offset (as var-length int)
213 ;;;    old-fp location sc-offset (as var-length int)
214 ;;;    [nfp location sc-offset (as var-length int), if nfp flag]
215 ;;;    code-start-pc (as a var-length int)
216 ;;;     This field implicitly encodes start of this function's code in the
217 ;;;     function map, as a delta from the previous function's code start.
218 ;;;     If the first function in the component, then this is the delta from
219 ;;;     0 (i.e. the absolute offset.)
220 ;;;    start-pc (as a var-length int)
221 ;;;     This encodes the environment start PC as an offset from the
222 ;;;     code-start PC.
223 ;;;    elsewhere-pc
224 ;;;     This encodes the elsewhere code start for this function, as a delta
225 ;;;     from the previous function's elsewhere code start. (i.e. the
226 ;;;     encoding is the same as for code-start-pc.)
227
228 #|
229 ### For functions with XEPs, name could be represented more simply and
230 compactly as some sort of info about with how to find the function-entry that
231 this is a function for. Actually, you really hardly need any info. You can
232 just chain through the functions in the component until you find the right one.
233 Well, I guess you need to at least know which function is an XEP for the real
234 function (which would be useful info anyway).
235 |#
236
237 ;;; The following are definitions of bit-fields in the first byte of
238 ;;; the minimal debug function:
239 (defconstant minimal-debug-function-name-symbol 0)
240 (defconstant minimal-debug-function-name-packaged 1)
241 (defconstant minimal-debug-function-name-uninterned 2)
242 (defconstant minimal-debug-function-name-component 3)
243 (defconstant-eqx minimal-debug-function-name-style-byte (byte 2 0) #'equalp)
244 (defconstant-eqx minimal-debug-function-kind-byte (byte 3 2) #'equalp)
245 (defparameter *minimal-debug-function-kinds*
246   #(nil :optional :external :top-level :cleanup))
247 (defconstant minimal-debug-function-returns-standard 0)
248 (defconstant minimal-debug-function-returns-specified 1)
249 (defconstant minimal-debug-function-returns-fixed 2)
250 (defconstant-eqx minimal-debug-function-returns-byte (byte 2 5) #'equalp)
251
252 ;;; The following are bit-flags in the second byte of the minimal debug
253 ;;; function:
254 ;;;   * If true, wrap (SETF ...) around the name.
255 (defconstant minimal-debug-function-setf-bit (ash 1 0))
256 ;;;   * If true, there is a NFP.
257 (defconstant minimal-debug-function-nfp-bit (ash 1 1))
258 ;;;   * If true, variables (hence arguments) have been dumped.
259 (defconstant minimal-debug-function-variables-bit (ash 1 2))
260 \f
261 ;;;; debug source
262
263 (def!struct (debug-source #-sb-xc-host (:pure t))
264   ;; This slot indicates where the definition came from:
265   ;;    :FILE - from a file (COMPILE-FILE)
266   ;;    :LISP - from Lisp (COMPILE)
267   (from (required-argument) :type (member :file :lisp))
268   ;; If :FILE, the file name, if :LISP or :STREAM, then a vector of
269   ;; the top-level forms. When from COMPILE, form 0 is #'(LAMBDA ...).
270   (name nil)
271   ;; the universal time that the source was written, or NIL if
272   ;; unavailable
273   (created nil :type (or unsigned-byte null))
274   ;; the universal time that the source was compiled
275   (compiled (required-argument) :type unsigned-byte)
276   ;; the source path root number of the first form read from this
277   ;; source (i.e. the total number of forms converted previously in
278   ;; this compilation)
279   (source-root 0 :type index)
280   ;; The FILE-POSITIONs of the truly top-level forms read from this
281   ;; file (if applicable). The vector element type will be chosen to
282   ;; hold the largest element. May be null to save space.
283   (start-positions nil :type (or (simple-array * (*)) null))
284   ;; If from :LISP, this is the function whose source is form 0.
285   (info nil))
286 \f
287 ;;;; DEBUG-INFO structures
288
289 (def!struct debug-info
290   ;; Some string describing something about the code in this component.
291   (name (required-argument) :type simple-string)
292   ;; A list of DEBUG-SOURCE structures describing where the code for this
293   ;; component came from, in the order that they were read.
294   ;;
295   ;; KLUDGE: comment from CMU CL:
296   ;;   *** NOTE: the offset of this slot is wired into the fasl dumper 
297   ;;   *** so that it can backpatch the source info when compilation
298   ;;   *** is complete.
299   (source nil :type list))
300
301 (def!struct (compiled-debug-info
302              (:include debug-info)
303              #-sb-xc-host (:pure t))
304   ;; a simple-vector of alternating DEBUG-FUNCTION objects and fixnum
305   ;; PCs, used to map PCs to functions, so that we can figure out what
306   ;; function we were running in. Each function is valid between the
307   ;; PC before it (inclusive) and the PC after it (exclusive). The PCs
308   ;; are in sorted order, to allow binary search. We omit the first
309   ;; and last PC, since their values are 0 and the length of the code
310   ;; vector.
311   ;;
312   ;; KLUDGE: PC's can't always be represented by FIXNUMs, unless we're
313   ;; always careful to put our code in low memory. Is that how it
314   ;; works? Would this break if we used a more general memory map? --
315   ;; WHN 20000120
316   (function-map (required-argument) :type simple-vector :read-only t))