2e024560ab485b29a0ad685074ff567e3b54e4b5
[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 ;;;; flags for compiled debug variables
15
16 ;;; FIXME: old CMU CL representation follows:
17 ;;;    Compiled debug variables are in a packed binary representation in the
18 ;;; DEBUG-FUN-VARS:
19 ;;;    single byte of boolean flags:
20 ;;;     uninterned name
21 ;;;        packaged name
22 ;;;     environment-live
23 ;;;     has distinct save location
24 ;;;     has ID (name not unique in this fun)
25 ;;;     minimal debug-info argument (name generated as ARG-0, ...)
26 ;;;     deleted: placeholder for unused minimal argument
27 ;;;    [name length in bytes (as var-length integer), if not minimal]
28 ;;;    [...name bytes..., if not minimal]
29 ;;;    [if packaged, var-length integer that is package name length]
30 ;;;     ...package name bytes...]
31 ;;;    [If has ID, ID as var-length integer]
32 ;;;    SC-Offset of primary location (as var-length integer)
33 ;;;    [If has save SC, SC-OFFSET of save location (as var-length integer)]
34
35 ;;; FIXME: The first two are no longer used in SBCL.
36 ;;;(defconstant compiled-debug-var-uninterned           #b00000001)
37 ;;;(defconstant compiled-debug-var-packaged             #b00000010)
38 (def!constant compiled-debug-var-environment-live       #b00000100)
39 (def!constant compiled-debug-var-save-loc-p             #b00001000)
40 (def!constant compiled-debug-var-id-p                   #b00010000)
41 (def!constant compiled-debug-var-minimal-p              #b00100000)
42 (def!constant compiled-debug-var-deleted-p              #b01000000)
43 \f
44 ;;;; compiled debug blocks
45 ;;;;
46 ;;;;    Compiled debug blocks are in a packed binary representation in the
47 ;;;; DEBUG-FUN-BLOCKS:
48 ;;;;    number of successors + bit flags (single byte)
49 ;;;;    elsewhere-p
50 ;;;;    ...ordinal number of each successor in the function's blocks vector...
51 ;;;;    number of locations in this block
52 ;;;;    kind of first location (single byte)
53 ;;;;    delta from previous PC (or from 0 if first location in function.)
54 ;;;;    [offset of first top level form, if no function TLF-NUMBER]
55 ;;;;    form number of first source form
56 ;;;;    first live mask (length in bytes determined by number of VARIABLES)
57 ;;;;    ...more <kind, delta, top level form offset, form-number, live-set>
58 ;;;;       tuples...
59
60 (defconstant-eqx compiled-debug-block-nsucc-byte (byte 2 0) #'equalp)
61 (def!constant compiled-debug-block-elsewhere-p #b00000100)
62
63 (defconstant-eqx compiled-code-location-kind-byte (byte 4 0) #'equalp)
64 (defparameter *compiled-code-location-kinds*
65   #(:unknown-return :known-return :internal-error :non-local-exit
66     :block-start :call-site :single-value-return :non-local-entry
67     :step-before-vop))
68 \f
69 ;;;; DEBUG-FUN objects
70
71 (def!struct (debug-fun (:constructor nil)))
72
73 (def!struct (compiled-debug-fun (:include debug-fun)
74                                 #-sb-xc-host (:pure t))
75   ;; KLUDGE: Courtesy of more than a decade of, ah, organic growth in
76   ;; CMU CL, there are two distinct -- but coupled -- mechanisms to
77   ;; finding the name of a function. The slot here is one mechanism
78   ;; (used in CMU CL to look up names in the debugger, e.g. in
79   ;; BACKTRACE). The other mechanism is the NAME slot in function
80   ;; primitive objects (used in CMU CL to look up names elsewhere,
81   ;; e.g. in CL:FUNCTION-LAMBDA-EXPRESSION and in CL:DESCRIBE).
82   ;;
83   ;; They're coupled by the way that DEBUG-FUN objects are looked up.
84   ;; A list of DEBUG-FUN objects is maintained for each COMPONENT. To
85   ;; figure out which DEBUG-FUN object corresponds to your FUNCTION
86   ;; object, you compare the name values of each. -- WHN 2001-12-20
87   (name (missing-arg) :type (or simple-string cons symbol))
88   ;; The kind of function (same as FUNCTIONAL-KIND):
89   (kind nil :type (member nil :optional :external :toplevel :cleanup))
90   ;; a description of variable locations for this function, in alphabetical
91   ;; order by name; or NIL if no information is available
92   ;;
93   ;; The variable entries are alphabetically ordered. This ordering is
94   ;; used in lifetime info to refer to variables: the first entry is
95   ;; 0, the second entry is 1, etc. Variable numbers are *not* the
96   ;; byte index at which the representation of the location starts.
97   ;;
98   ;; Each entry is:
99   ;;   * a FLAGS value, which is a FIXNUM with various
100   ;;     COMPILED-DEBUG-FUN-FOO bits set
101   ;;   * the symbol which names this variable, unless debug info
102   ;;     is minimal
103   ;;   * the variable ID, when it has one
104   ;;   * SC-offset of primary location, if it has one
105   ;;   * SC-offset of save location, if it has one
106   (vars nil :type (or simple-vector null))
107   ;; a vector of the packed binary representation of the
108   ;; COMPILED-DEBUG-BLOCKs in this function, in the order that the
109   ;; blocks were emitted. The first block is the start of the
110   ;; function. This slot may be NIL to save space.
111   ;;
112   ;; FIXME: The "packed binary representation" description in the
113   ;; comment above is the same as the description of the old
114   ;; representation of VARIABLES which doesn't work properly in SBCL
115   ;; (because it doesn't transform correctly under package renaming).
116   ;; Check whether this slot's data might have the same problem that
117   ;; that slot's data did.
118   (blocks nil :type (or (simple-array (unsigned-byte 8) (*)) null))
119   ;; If all code locations in this function are in the same top level
120   ;; form, then this is the number of that form, otherwise NIL. If
121   ;; NIL, then each code location represented in the BLOCKS specifies
122   ;; the TLF number.
123   (tlf-number nil :type (or index null))
124   ;; a vector describing the variables that the argument values are
125   ;; stored in within this function. The locations are represented by
126   ;; the ordinal number of the entry in the VARIABLES slot value. The
127   ;; locations are in the order that the arguments are actually passed
128   ;; in, but special marker symbols can be interspersed to indicate
129   ;; 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 &KEY argument with the
150   ;;    specified name.
151   ;;
152   ;; This may be NIL to save space. If no symbols are present, then
153   ;; this will be represented with an I-vector with sufficiently large
154   ;; element type. If this is :MINIMAL, then this means that the
155   ;; VARIABLES are all required arguments, and are in the order they
156   ;; appear in the VARIABLES vector. In other words, :MINIMAL stands
157   ;; in for a vector where every element holds its 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 (missing-arg) :type sc-offset)
174   (old-fp (missing-arg) :type sc-offset)
175   ;; SC-OFFSET for the number stack FP in this function, or NIL if no
176   ;; NFP 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 (missing-arg) :type index)
181   ;; The start of elsewhere code for this function (if any.)
182   (elsewhere-pc (missing-arg) :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
193 ;;; packed into a single byte-vector which is placed in the
194 ;;; COMPILED-DEBUG-INFO-FUN-MAP. Because of this, all functions in a
195 ;;; component must be representable in minimal format for any function
196 ;;; to actually be dumped in minimal format. The vector is a sequence
197 ;;; of records 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 ;;; ### For functions with XEPs, name could be represented more simply
229 ;;; and compactly as some sort of info about with how to find the
230 ;;; function entry that this is a function for. Actually, you really
231 ;;; hardly need any info. You can just chain through the functions in
232 ;;; the component until you find the right one. Well, I guess you need
233 ;;; to at least know which function is an XEP for the real function
234 ;;; (which would be useful info anyway).
235 \f
236 ;;;; DEBUG SOURCE
237
238 ;;; There is one per compiled file and one per function compiled at
239 ;;; toplevel or loaded from source.
240 (def!struct (debug-source #-sb-xc-host (:pure t))
241   ;; (This is one of those structures where IWBNI we had multiple
242   ;; inheritance.  The first four slots describe compilation of a
243   ;; file, the fifth and sixth compilation of a form processed by
244   ;; EVAL, and the seventh and eigth all compilation units; and these
245   ;; are orthogonal concerns that can combine independently.)
246
247   ;; When the DEBUG-SOURCE describes a file, the file's namestring.
248   ;; Otherwise, NIL.
249   (namestring nil :type (or null string))
250   ;; the universal time that the source was written, or NIL if
251   ;; unavailable
252   (created nil :type (or unsigned-byte null))
253   ;; the source path root number of the first form read from this
254   ;; source (i.e. the total number of forms converted previously in
255   ;; this compilation).  (Note: this will always be 0 so long as the
256   ;; SOURCE-INFO structure has exactly one FILE-INFO.)
257   (source-root 0 :type index)
258   ;; The FILE-POSITIONs of the truly top level forms read from this
259   ;; file (if applicable). The vector element type will be chosen to
260   ;; hold the largest element.
261   (start-positions nil :type (or (simple-array * (*)) null))
262
263   ;; For functions processed by EVAL (including EVAL-WHEN and LOAD on
264   ;; a source file), the source form.
265   (form nil :type list)
266   ;; This is the function whose source is the form.
267   (function nil)
268
269   ;; the universal time that the source was compiled
270   (compiled (missing-arg) :type unsigned-byte)
271   ;; Additional information from (WITH-COMPILATION-UNIT (:SOURCE-PLIST ...))
272   (plist *source-plist*))
273 \f
274 ;;;; DEBUG-INFO structures
275
276 (def!struct debug-info
277   ;; Some string describing something about the code in this component.
278   (name (missing-arg) :type t)
279   ;; A list of DEBUG-SOURCE structures describing where the code for this
280   ;; component came from, in the order that they were read.
281   ;;
282   ;; KLUDGE: comment from CMU CL:
283   ;;   *** NOTE: the offset of this slot is wired into the fasl dumper
284   ;;   *** so that it can backpatch the source info when compilation
285   ;;   *** is complete.
286   (source nil))
287
288 (def!struct (compiled-debug-info
289              (:include debug-info)
290              #-sb-xc-host (:pure t))
291   ;; a SIMPLE-VECTOR of alternating DEBUG-FUN objects and fixnum
292   ;; PCs, used to map PCs to functions, so that we can figure out what
293   ;; function we were running in. Each function is valid between the
294   ;; PC before it (inclusive) and the PC after it (exclusive). The PCs
295   ;; are in sorted order, to allow binary search. We omit the first
296   ;; and last PC, since their values are 0 and the length of the code
297   ;; vector.
298   ;;
299   ;; KLUDGE: PC's can't always be represented by FIXNUMs, unless we're
300   ;; always careful to put our code in low memory. Is that how it
301   ;; works? Would this break if we used a more general memory map? --
302   ;; WHN 20000120
303   (fun-map (missing-arg) :type simple-vector :read-only t))