Fix grammar in lossage message
[sbcl.git] / src / code / debug-info.lisp
blobc35516b14f69a2b850bdd90ad8d87cc7d99fac7b
1 ;;;; structures used for recording debugger information
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.
12 (in-package "SB!C")
14 ;;;; flags for compiled debug variables
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)]
35 (defconstant compiled-debug-var-more-context-p #b00000001)
36 (defconstant compiled-debug-var-more-count-p #b00000010)
37 (defconstant compiled-debug-var-environment-live #b00000100)
38 (defconstant compiled-debug-var-save-loc-p #b00001000)
39 (defconstant compiled-debug-var-id-p #b00010000)
40 (defconstant compiled-debug-var-minimal-p #b00100000)
41 (defconstant compiled-debug-var-deleted-p #b01000000)
42 (defconstant compiled-debug-var-indirect-p #b10000000)
44 ;;;; compiled debug blocks
45 ;;;;
46 ;;;; Compiled debug blocks are in a packed binary representation in the
47 ;;;; DEBUG-FUN-BLOCKS:
48 ;;;; number of locations in this block
49 ;;;; kind of first location (single byte)
50 ;;;; delta from previous PC (or from 0 if first location in function.)
51 ;;;; [offset of first top level form, if no function TLF-NUMBER]
52 ;;;; form number of first source form
53 ;;;; first live mask (length in bytes determined by number of VARIABLES)
54 ;;;; ...more <kind, delta, top level form offset, form-number, live-set>
55 ;;;; tuples...
57 (defparameter *compiled-code-location-kinds*
58 #(:unknown-return :known-return :internal-error :non-local-exit
59 :block-start :call-site :single-value-return :non-local-entry
60 :step-before-vop))
62 ;;;; DEBUG-FUN objects
64 (def!struct (debug-fun (:constructor nil)))
66 (def!struct (compiled-debug-fun (:include debug-fun)
67 #-sb-xc-host (:pure t))
68 ;; KLUDGE: Courtesy of more than a decade of, ah, organic growth in
69 ;; CMU CL, there are two distinct -- but coupled -- mechanisms to
70 ;; finding the name of a function. The slot here is one mechanism
71 ;; (used in CMU CL to look up names in the debugger, e.g. in
72 ;; BACKTRACE). The other mechanism is the NAME slot in function
73 ;; primitive objects (used in CMU CL to look up names elsewhere,
74 ;; e.g. in CL:FUNCTION-LAMBDA-EXPRESSION and in CL:DESCRIBE).
76 ;; They're coupled by the way that DEBUG-FUN objects are looked up.
77 ;; A list of DEBUG-FUN objects is maintained for each COMPONENT. To
78 ;; figure out which DEBUG-FUN object corresponds to your FUNCTION
79 ;; object, you compare the name values of each. -- WHN 2001-12-20
80 (name (missing-arg) :type (or simple-string cons symbol))
81 ;; The kind of function (same as FUNCTIONAL-KIND):
82 (kind nil :type (member nil :optional :external :toplevel :cleanup))
83 ;; a description of variable locations for this function, in alphabetical
84 ;; order by name; or NIL if no information is available
86 ;; The variable entries are alphabetically ordered. This ordering is
87 ;; used in lifetime info to refer to variables: the first entry is
88 ;; 0, the second entry is 1, etc. Variable numbers are *not* the
89 ;; byte index at which the representation of the location starts.
91 ;; Each entry is:
92 ;; * a FLAGS value, which is a FIXNUM with various
93 ;; COMPILED-DEBUG-FUN-FOO bits set
94 ;; * the symbol which names this variable, unless debug info
95 ;; is minimal
96 ;; * the variable ID, when it has one
97 ;; * SC-offset of primary location, if it has one
98 ;; * SC-offset of save location, if it has one
99 (vars nil :type (or simple-vector null))
100 ;; a vector of the packed binary representation of the
101 ;; COMPILED-DEBUG-BLOCKs in this function, in the order that the
102 ;; blocks were emitted. The first block is the start of the
103 ;; function. This slot may be NIL to save space.
105 ;; FIXME: The "packed binary representation" description in the
106 ;; comment above is the same as the description of the old
107 ;; representation of VARIABLES which doesn't work properly in SBCL
108 ;; (because it doesn't transform correctly under package renaming).
109 ;; Check whether this slot's data might have the same problem that
110 ;; that slot's data did.
111 (blocks nil :type (or (simple-array (unsigned-byte 8) (*)) null))
112 ;; If all code locations in this function are in the same top level
113 ;; form, then this is the number of that form, otherwise NIL. If
114 ;; NIL, then each code location represented in the BLOCKS specifies
115 ;; the TLF number.
116 (tlf-number nil :type (or index null))
117 (form-number nil :type (or index null))
118 ;; a vector describing the variables that the argument values are
119 ;; stored in within this function. The locations are represented by
120 ;; the ordinal number of the entry in the VARIABLES slot value. The
121 ;; locations are in the order that the arguments are actually passed
122 ;; in, but special marker symbols can be interspersed to indicate
123 ;; the original call syntax:
125 ;; DELETED
126 ;; There was an argument to the function in this position, but it was
127 ;; deleted due to lack of references. The value cannot be recovered.
129 ;; SUPPLIED-P
130 ;; The following location is the supplied-p value for the preceding
131 ;; keyword or optional.
133 ;; OPTIONAL-ARGS
134 ;; Indicates that following unqualified args are optionals, not required.
136 ;; REST-ARG
137 ;; The following location holds the list of rest args.
139 ;; MORE-ARG
140 ;; The following two locations are the more arg context and count.
142 ;; <any other symbol>
143 ;; The following location is the value of the &KEY argument with the
144 ;; specified name.
146 ;; This may be NIL to save space. If no symbols are present, then
147 ;; this will be represented with an I-vector with sufficiently large
148 ;; element type. If this is :MINIMAL, then this means that the
149 ;; VARIABLES are all required arguments, and are in the order they
150 ;; appear in the VARIABLES vector. In other words, :MINIMAL stands
151 ;; in for a vector where every element holds its index.
152 (arguments nil :type (or (simple-array * (*)) (member :minimal nil)))
153 ;; There are three alternatives for this slot:
155 ;; a VECTOR
156 ;; A vector of SC-OFFSETS describing the return locations. The
157 ;; vector element type is chosen to hold the largest element.
159 ;; :STANDARD
160 ;; The function returns using the standard unknown-values convention.
162 ;; :FIXED
163 ;; The function returns using the fixed-values convention, but
164 ;; in order to save space, we elected not to store a vector.
165 (returns :fixed :type (or (simple-array * (*)) (member :standard :fixed)))
166 ;; SC-OFFSETs describing where the return PC and return FP are kept.
167 #!-fp-and-pc-standard-save
168 (return-pc (missing-arg) :type sc-offset)
169 #!-fp-and-pc-standard-save
170 (old-fp (missing-arg) :type sc-offset)
171 ;; The earliest PC in this function at which the environment is properly
172 ;; initialized (arguments moved from passing locations, etc.)
173 (start-pc (missing-arg) :type index)
174 ;; The start of elsewhere code for this function (if any.)
175 (elsewhere-pc (missing-arg) :type index)
176 (closure-save nil :type (or sc-offset null))
177 #!+unwind-to-frame-and-call-vop
178 (bsp-save nil :type (or sc-offset null)))
180 ;;;; minimal debug function
182 ;;; The minimal debug info format compactly represents debug-info for some
183 ;;; cases where the other debug info (variables, blocks) is small enough so
184 ;;; that the per-function overhead becomes relatively large. The minimal
185 ;;; debug-info format can represent any function at level 0, and any fixed-arg
186 ;;; function at level 1.
188 ;;; In the minimal format, the debug functions and function map are
189 ;;; packed into a single byte-vector which is placed in the
190 ;;; COMPILED-DEBUG-INFO-FUN-MAP. Because of this, all functions in a
191 ;;; component must be representable in minimal format for any function
192 ;;; to actually be dumped in minimal format. The vector is a sequence
193 ;;; of records in this format:
194 ;;; name representation + kind + return convention (single byte)
195 ;;; bit flags (single byte)
196 ;;; setf, nfp, variables
197 ;;; [package name length (as var-length int), if name is packaged]
198 ;;; [...package name bytes, if name is packaged]
199 ;;; [name length (as var-length int), if there is a name]
200 ;;; [...name bytes, if there is a name]
201 ;;; [variables length (as var-length int), if variables flag]
202 ;;; [...bytes holding variable descriptions]
203 ;;; If variables are dumped (level 1), then the variables are all
204 ;;; arguments (in order) with the minimal-arg bit set.
205 ;;; [If returns is specified, then the number of return values]
206 ;;; [...sequence of var-length ints holding sc-offsets of the return
207 ;;; value locations, if fixed return values are specified.]
208 ;;; return-pc location sc-offset (as var-length int)
209 ;;; old-fp location sc-offset (as var-length int)
210 ;;; [nfp location sc-offset (as var-length int), if nfp flag]
211 ;;; code-start-pc (as a var-length int)
212 ;;; This field implicitly encodes start of this function's code in the
213 ;;; function map, as a delta from the previous function's code start.
214 ;;; If the first function in the component, then this is the delta from
215 ;;; 0 (i.e. the absolute offset.)
216 ;;; start-pc (as a var-length int)
217 ;;; This encodes the environment start PC as an offset from the
218 ;;; code-start PC.
219 ;;; elsewhere-pc
220 ;;; This encodes the elsewhere code start for this function, as a delta
221 ;;; from the previous function's elsewhere code start. (i.e. the
222 ;;; encoding is the same as for code-start-pc.)
224 ;;; ### For functions with XEPs, name could be represented more simply
225 ;;; and compactly as some sort of info about with how to find the
226 ;;; function entry that this is a function for. Actually, you really
227 ;;; hardly need any info. You can just chain through the functions in
228 ;;; the component until you find the right one. Well, I guess you need
229 ;;; to at least know which function is an XEP for the real function
230 ;;; (which would be useful info anyway).
232 ;;;; DEBUG SOURCE
234 ;;; There is one per compiled file and one per function compiled at
235 ;;; toplevel or loaded from source.
236 (def!struct (debug-source #-sb-xc-host (:pure t))
237 ;; (This is one of those structures where IWBNI we had multiple
238 ;; inheritance. The first four slots describe compilation of a
239 ;; file, the fifth and sixth compilation of a form processed by
240 ;; EVAL, and the seventh and eigth all compilation units; and these
241 ;; are orthogonal concerns that can combine independently.)
243 ;; When the DEBUG-SOURCE describes a file, the file's namestring.
244 ;; Otherwise, NIL.
245 (namestring nil :type (or null string))
246 ;; the universal time that the source was written, or NIL if
247 ;; unavailable
248 (created nil :type (or unsigned-byte null))
249 ;; The FILE-POSITIONs of the truly top level forms read from this
250 ;; file (if applicable). The vector element type will be chosen to
251 ;; hold the largest element.
252 (start-positions nil :type (or (simple-array * (*)) null))
254 ;; For functions processed by EVAL (including EVAL-WHEN and LOAD on
255 ;; a source file), the source form.
256 (form nil :type list)
257 ;; This is the function whose source is the form.
258 (function nil)
260 ;; the universal time that the source was compiled
261 (compiled (missing-arg) :type unsigned-byte)
262 ;; Additional information from (WITH-COMPILATION-UNIT (:SOURCE-PLIST ...))
263 (plist *source-plist*))
265 ;;;; DEBUG-INFO structures
267 (def!struct debug-info
268 ;; Some string describing something about the code in this component.
269 (name (missing-arg) :type t)
270 ;; A DEBUG-SOURCE structure describing where the code for this
271 ;; component came from, in the order that forms were read.
272 (source nil))
274 (def!struct (compiled-debug-info
275 (:include debug-info)
276 #-sb-xc-host (:pure t))
277 ;; a SIMPLE-VECTOR of alternating DEBUG-FUN objects and fixnum
278 ;; PCs, used to map PCs to functions, so that we can figure out what
279 ;; function we were running in. Each function is valid between the
280 ;; PC before it (inclusive) and the PC after it (exclusive). The PCs
281 ;; are in sorted order, to allow binary search. We omit the first
282 ;; and last PC, since their values are 0 and the length of the code
283 ;; vector.
285 ;; KLUDGE: PC's can't always be represented by FIXNUMs, unless we're
286 ;; always careful to put our code in low memory. Is that how it
287 ;; works? Would this break if we used a more general memory map? --
288 ;; WHN 20000120
289 (fun-map (missing-arg) :type simple-vector :read-only t))
291 (defvar *!initial-debug-sources*)
293 (defun !debug-info-cold-init ()
294 (let ((now (get-universal-time)))
295 (dolist (debug-source *!initial-debug-sources*)
296 (let* ((namestring (debug-source-namestring debug-source))
297 (timestamp (file-write-date namestring)))
298 (setf (debug-source-created debug-source) timestamp
299 (debug-source-compiled debug-source) now)))))
301 ;;;; file reading
302 ;;;;
303 ;;;; When reading from a file, we have to keep track of some source
304 ;;;; information. We also exploit our ability to back up for printing
305 ;;;; the error context and for recovering from errors.
306 ;;;;
307 ;;;; The interface we provide to this stuff is the stream-oid
308 ;;;; SOURCE-INFO structure. The bookkeeping is done as a side effect
309 ;;;; of getting the next source form.
311 ;;; A FILE-INFO structure holds all the source information for a
312 ;;; given file.
313 (defstruct (file-info
314 (:copier nil)
315 #-no-ansi-print-object
316 (:print-object (lambda (s stream)
317 (print-unreadable-object (s stream :type t)
318 (princ (file-info-name s) stream)))))
319 ;; If a file, the truename of the corresponding source file. If from
320 ;; a Lisp form, :LISP. If from a stream, :STREAM.
321 (name (missing-arg) :type (or pathname (eql :lisp)))
322 ;; the external format that we'll call OPEN with, if NAME is a file.
323 (external-format nil)
324 ;; the defaulted, but not necessarily absolute file name (i.e. prior
325 ;; to TRUENAME call.) Null if not a file. This is used to set
326 ;; *COMPILE-FILE-PATHNAME*, and if absolute, is dumped in the
327 ;; debug-info.
328 (untruename nil :type (or pathname null))
329 ;; the file's write date (if relevant)
330 (write-date nil :type (or unsigned-byte null))
331 ;; parallel vectors containing the forms read out of the file and
332 ;; the file positions that reading of each form started at (i.e. the
333 ;; end of the previous form)
334 (forms (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t))
335 (positions (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t))
336 ;; A vector of character ranges than span each subform in the TLF,
337 ;; reset to empty for each one, updated by form-tracking-stream-observer.
338 (subforms nil :type (or null (vector t)) :read-only t)
339 ;; A list of objects about which the compile may/would/should have signaled
340 ;; a style-warning in the :compile-toplevel situation, so we don't do it
341 ;; again in the :load-toplevel situation.
342 ;; This is a somewhat useless thing to track, but arguably
343 ;; the "&OPTIONAL and &KEY" warning is quite annoying to see repeated.
344 ;; And I doubt it changes anyone's mind about coding style anyway.
345 ;; Typically this matters for DEFTYPE and DEFMACRO.
346 (style-warning-tracker nil :type list))
348 ;;; The SOURCE-INFO structure provides a handle on all the source
349 ;;; information for an entire compilation.
350 (defstruct (source-info
351 #-no-ansi-print-object
352 (:print-object (lambda (s stream)
353 (print-unreadable-object
354 (s stream :type t :identity t))))
355 (:copier nil))
356 ;; the UT that compilation started at
357 (start-time (get-universal-time) :type unsigned-byte)
358 ;; the IRT that compilation started at
359 (start-real-time (get-internal-real-time) :type unsigned-byte)
360 ;; the FILE-INFO structure for this compilation
361 (file-info nil :type (or file-info null))
362 ;; the stream that we are using to read the FILE-INFO, or NIL if
363 ;; no stream has been opened yet
364 (stream nil :type (or stream null))
365 ;; for coalescing DEFINITION-SOURCE-LOCATION of effectively toplevel forms
366 ;; inside one truly toplevel form.
367 (last-defn-source-loc)
368 ;; if the current compilation is recursive (e.g., due to EVAL-WHEN
369 ;; processing at compile-time), the invoking compilation's
370 ;; source-info.
371 (parent nil :type (or source-info null)))