1 ;;;; structures used for recording debugger information
3 ;;;; This software is part of the SBCL system. See the README file for
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.
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
19 ;;; single byte of boolean flags:
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-same-name-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
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>
57 (defconstant-eqx +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
63 (eval-when (:compile-toplevel
)
64 (assert (<= (integer-length (1- (length +compiled-code-location-kinds
+))) 4)))
66 ;;; Location flags, encoded in the low 4 bits of loction kind byte
67 (defconstant compiled-code-location-stepping
(ash #b0001
4))
68 (defconstant compiled-code-location-context
(ash #b0010
4))
69 (defconstant compiled-code-location-live
(ash #b0100
4))
70 (defconstant compiled-code-location-zero-form-number
(ash #b1000
4))
72 (defconstant debug-info-var-deleted -
1)
73 (defconstant debug-info-var-rest -
2)
74 (defconstant debug-info-var-more -
3)
75 (defconstant debug-info-var-optional -
4)
76 (defconstant debug-info-var-supplied-p -
5)
79 ;;;; DEBUG-FUN objects
81 (def!struct
(debug-fun (:constructor nil
)
84 (def!struct
(compiled-debug-fun (:include debug-fun
)
86 #-sb-xc-host
(:pure t
))
87 ;; KLUDGE: Courtesy of more than a decade of, ah, organic growth in
88 ;; CMU CL, there are two distinct -- but coupled -- mechanisms to
89 ;; finding the name of a function. The slot here is one mechanism
90 ;; (used in CMU CL to look up names in the debugger, e.g. in
91 ;; BACKTRACE). The other mechanism is the NAME slot in function
92 ;; primitive objects (used in CMU CL to look up names elsewhere,
93 ;; e.g. in CL:FUNCTION-LAMBDA-EXPRESSION and in CL:DESCRIBE).
95 ;; They're coupled by the way that DEBUG-FUN objects are looked up.
96 ;; A list of DEBUG-FUN objects is maintained for each COMPONENT. To
97 ;; figure out which DEBUG-FUN object corresponds to your FUNCTION
98 ;; object, you compare the name values of each. -- WHN 2001-12-20
99 (name (missing-arg) :type
(or simple-string cons symbol
) :read-only t
)
100 ;; a description of variable locations for this function, in alphabetical
101 ;; order by name; or NIL if no information is available
102 ;; If only one variable is encoded then it's stored as is without a vector.
104 ;; The variable entries are alphabetically ordered. This ordering is
105 ;; used in lifetime info to refer to variables: the first entry is
106 ;; 0, the second entry is 1, etc. Variable numbers are *not* the
107 ;; byte index at which the representation of the location starts.
110 ;; * a FLAGS value, which is a FIXNUM with various
111 ;; COMPILED-DEBUG-FUN-FOO bits set
112 ;; * the symbol which names this variable, unless debug info
114 ;; * the variable ID, when it has one
115 ;; * SC-offset of primary location, if it has one
116 ;; * SC-offset of save location, if it has one
117 ;; Can either be a single value or a vector for multiple values.
119 ;; a vector of the packed binary representation of the
120 ;; COMPILED-DEBUG-BLOCKs in this function, in the order that the
121 ;; blocks were emitted. The first block is the start of the
122 ;; function. This slot may be NIL to save space.
124 ;; FIXME: The "packed binary representation" description in the
125 ;; comment above is the same as the description of the old
126 ;; representation of VARIABLES which doesn't work properly in SBCL
127 ;; (because it doesn't transform correctly under package renaming).
128 ;; Check whether this slot's data might have the same problem that
129 ;; that slot's data did.
130 (blocks nil
:type
(or (simple-array (unsigned-byte 8) (*)) null
))
131 (form-number nil
:type
(or index null
))
132 ;; a vector describing the variables that the argument values are
133 ;; stored in within this function. The locations are represented by
134 ;; the ordinal number of the entry in the VARIABLES slot value. The
135 ;; locations are in the order that the arguments are actually passed
136 ;; in, but special negative numbers can be interspersed to indicate
137 ;; the original call syntax:
139 ;; DEBUG-INFO-VAR-DELETED
140 ;; There was an argument to the function in this position, but it was
141 ;; deleted due to lack of references. The value cannot be recovered.
143 ;; DEBUG-INFO-VAR-SUPPLIED-P
144 ;; The following location is the supplied-p value for the preceding
145 ;; keyword or optional.
147 ;; DEBUG-INFO-VAR-OPTIONAL
148 ;; Indicates that following unqualified args are optionals, not required.
150 ;; DEBUG-INFO-VAR-REST
151 ;; The following location holds the list of rest args.
153 ;; DEBUG-INFO-VAR-MORE
154 ;; The following two locations are the more arg context and count.
157 ;; The following location is the value of the &KEY argument with the
160 ;; This may be NIL to save space. If no symbols are present, then
161 ;; this will be represented with an I-vector with sufficiently large
162 ;; element type. If this is :MINIMAL, then this means that the
163 ;; VARIABLES are all required arguments, and are in the order they
164 ;; appear in the VARIABLES vector. In other words, :MINIMAL stands
165 ;; in for a vector where every element holds its index.
167 ;; Can either be a single value or a vector for multiple values.
169 ;; There are three alternatives for this slot:
172 ;; A vector of SC-OFFSETS describing the return locations. The
173 ;; vector element type is chosen to hold the largest element.
176 ;; The function returns using the standard unknown-values convention.
179 ;; The function returns using the fixed-values convention, but
180 ;; in order to save space, we elected not to store a vector.
181 (returns :fixed
:type
(or (simple-array * (*)) (member :standard
:fixed
)))
182 ;; SC-OFFSETs describing where the return PC and return FP are kept.
183 #!-fp-and-pc-standard-save
184 (return-pc (missing-arg) :type sc-offset
)
185 #!-fp-and-pc-standard-save
186 (old-fp (missing-arg) :type sc-offset
)
187 ;; An integer which contains between 2 and 4 varint-encoded fields:
189 ;; The earliest PC in this function at which the environment is properly
190 ;; initialized (arguments moved from passing locations, etc.)
192 ;; The start of elsewhere code for this function (if any.)
193 ;; CLOSURE-SAVE, and BSP-SAVE.
194 (encoded-locs (missing-arg) :type unsigned-byte
:read-only t
))
196 (defun cdf-encode-locs (start-pc elsewhere-pc closure-save
197 #!+unwind-to-frame-and-call-vop bsp-save
)
198 (dx-let ((storage (make-array (* 5 4) :element-type
'(unsigned-byte 8))))
199 (let ((bytes (make-array (* 5 4) :fill-pointer
0 :displaced-to storage
200 :element-type
'(unsigned-byte 8))))
201 ;; ELSEWHERE is encoded first to simplify the C backtrace logic,
202 ;; which does not need access to any of the subsequent fields.
203 (write-var-integer elsewhere-pc bytes
)
204 (write-var-integer start-pc bytes
)
205 #!+unwind-to-frame-and-call-vop
206 (write-var-integer (if bsp-save
(1+ bsp-save
) 0) bytes
)
207 ;; More often the BSP-SAVE is non-null than CLOSURE-SAVE is non-null,
208 ;; so the encoding is potentially smaller with CLOSURE-SAVE being last.
210 (write-var-integer (1+ closure-save
) bytes
))
211 (integer-from-octets bytes
))))
213 (defun cdf-decode-locs (cdf)
214 (let ((encoding (compiled-debug-fun-encoded-locs cdf
))
216 (flet ((decode-varint (&aux
(accumulator 0) (shift 0))
218 (let ((byte (ldb (byte 8 input-pointer
) encoding
)))
219 (incf input-pointer
8)
220 (setf accumulator
(logior accumulator
(ash (logand byte
#x7f
) shift
)))
222 (unless (logtest byte
#x80
) (return accumulator
))))))
223 (let ((elsewhere-pc (decode-varint))
224 (start-pc (decode-varint))
225 #!+unwind-to-frame-and-call-vop
226 ;; 0 -> NULL, 1 -> 0, ...
227 (bsp-save (let ((i (decode-varint))) (unless (zerop i
) (1- i
))))
228 (closure-save (let ((i (decode-varint))) (unless (zerop i
) (1- i
)))))
229 (values start-pc elsewhere-pc closure-save
230 #!+unwind-to-frame-and-call-vop bsp-save
)))))
232 (macrolet ((def (index name
)
234 (nth-value ,index
(cdf-decode-locs cdf
)))))
235 (def 0 compiled-debug-fun-start-pc
)
236 (def 1 compiled-debug-fun-elsewhere-pc
)
237 ;; Most compiled-debug-funs don't need these
238 (def 2 compiled-debug-fun-closure-save
)
239 #!+unwind-to-frame-and-call-vop
(def 3 compiled-debug-fun-bsp-save
))
241 (def!struct
(compiled-debug-fun-optional (:include compiled-debug-fun
)
242 #-sb-xc-host
(:pure t
)
245 (def!struct
(compiled-debug-fun-more (:include compiled-debug-fun
)
246 #-sb-xc-host
(:pure t
)
249 (def!struct
(compiled-debug-fun-external (:include compiled-debug-fun
)
250 #-sb-xc-host
(:pure t
)
253 (def!struct
(compiled-debug-fun-toplevel (:include compiled-debug-fun
)
254 #-sb-xc-host
(:pure t
)
257 (def!struct
(compiled-debug-fun-cleanup (:include compiled-debug-fun
)
258 #-sb-xc-host
(:pure t
)
262 (defun compiled-debug-fun-ctor (kind)
264 (:optional
#'make-compiled-debug-fun-optional
)
265 (:more
#'make-compiled-debug-fun-more
)
266 (:external
#'make-compiled-debug-fun-external
)
267 (:toplevel
#'make-compiled-debug-fun-toplevel
)
268 (:cleanup
#'make-compiled-debug-fun-cleanup
)
269 ((nil) #'make-compiled-debug-fun
)))
271 (defun compiled-debug-fun-kind (debug-fun)
273 (compiled-debug-fun-optional :optional
)
274 (compiled-debug-fun-more :more
)
275 (compiled-debug-fun-external :external
)
276 (compiled-debug-fun-toplevel :toplevel
)
277 (compiled-debug-fun-cleanup :cleanup
)
278 (compiled-debug-fun nil
)))
281 ;;;; minimal debug function
283 ;;; The minimal debug info format compactly represents debug-info for some
284 ;;; cases where the other debug info (variables, blocks) is small enough so
285 ;;; that the per-function overhead becomes relatively large. The minimal
286 ;;; debug-info format can represent any function at level 0, and any fixed-arg
287 ;;; function at level 1.
289 ;;; In the minimal format, the debug functions and function map are
290 ;;; packed into a single byte-vector which is placed in the
291 ;;; COMPILED-DEBUG-INFO-FUN-MAP. Because of this, all functions in a
292 ;;; component must be representable in minimal format for any function
293 ;;; to actually be dumped in minimal format. The vector is a sequence
294 ;;; of records in this format:
295 ;;; name representation + kind + return convention (single byte)
296 ;;; bit flags (single byte)
297 ;;; setf, nfp, variables
298 ;;; [package name length (as var-length int), if name is packaged]
299 ;;; [...package name bytes, if name is packaged]
300 ;;; [name length (as var-length int), if there is a name]
301 ;;; [...name bytes, if there is a name]
302 ;;; [variables length (as var-length int), if variables flag]
303 ;;; [...bytes holding variable descriptions]
304 ;;; If variables are dumped (level 1), then the variables are all
305 ;;; arguments (in order) with the minimal-arg bit set.
306 ;;; [If returns is specified, then the number of return values]
307 ;;; [...sequence of var-length ints holding sc-offsets of the return
308 ;;; value locations, if fixed return values are specified.]
309 ;;; return-pc location sc-offset (as var-length int)
310 ;;; old-fp location sc-offset (as var-length int)
311 ;;; [nfp location sc-offset (as var-length int), if nfp flag]
312 ;;; code-start-pc (as a var-length int)
313 ;;; This field implicitly encodes start of this function's code in the
314 ;;; function map, as a delta from the previous function's code start.
315 ;;; If the first function in the component, then this is the delta from
316 ;;; 0 (i.e. the absolute offset.)
317 ;;; start-pc (as a var-length int)
318 ;;; This encodes the environment start PC as an offset from the
321 ;;; This encodes the elsewhere code start for this function, as a delta
322 ;;; from the previous function's elsewhere code start. (i.e. the
323 ;;; encoding is the same as for code-start-pc.)
325 ;;; ### For functions with XEPs, name could be represented more simply
326 ;;; and compactly as some sort of info about with how to find the
327 ;;; function entry that this is a function for. Actually, you really
328 ;;; hardly need any info. You can just chain through the functions in
329 ;;; the component until you find the right one. Well, I guess you need
330 ;;; to at least know which function is an XEP for the real function
331 ;;; (which would be useful info anyway).
335 ;;; There is one per compiled file and one per function compiled at
336 ;;; toplevel or loaded from source.
337 (def!struct
(debug-source #-sb-xc-host
(:pure t
)
339 ;; (This is one of those structures where IWBNI we had multiple
340 ;; inheritance. The first four slots describe compilation of a
341 ;; file, the fifth and sixth compilation of a form processed by
342 ;; EVAL, and the seventh and eigth all compilation units; and these
343 ;; are orthogonal concerns that can combine independently.)
345 ;; When the DEBUG-SOURCE describes a file, the file's namestring.
347 (namestring nil
:type
(or null string
) :read-only t
)
348 ;; the universal time that the source was written, or NIL if
350 (created nil
:type
(or unsigned-byte null
))
352 ;; For functions processed by EVAL (including EVAL-WHEN and LOAD on
353 ;; a source file), the source form.
354 (form nil
:type list
:read-only t
)
355 ;; This is the function whose source is the form.
356 (function nil
:read-only t
)
358 ;; the universal time that the source was compiled
359 (compiled (missing-arg) :type unsigned-byte
)
360 ;; Additional information from (WITH-COMPILATION-UNIT (:SOURCE-PLIST ...))
361 (plist *source-plist
* :read-only t
))
363 ;;;; DEBUG-INFO structures
365 (def!struct
(debug-info
367 ;; Some string describing something about the code in this component.
368 (name (missing-arg) :type t
:read-only t
)
369 ;; A DEBUG-SOURCE structure describing where the code for this
370 ;; component came from, in the order that forms were read.
373 (def!struct
(compiled-debug-info
374 (:include debug-info
)
376 #-sb-xc-host
(:pure t
))
377 ;; a SIMPLE-VECTOR of alternating DEBUG-FUN objects and fixnum
378 ;; PCs, used to map PCs to functions, so that we can figure out what
379 ;; function we were running in. Each function is valid between the
380 ;; PC before it (inclusive) and the PC after it (exclusive). The PCs
381 ;; are in sorted order, to allow binary search. We omit the first
382 ;; and last PC, since their values are 0 and the length of the code
385 ;; KLUDGE: PC's can't always be represented by FIXNUMs, unless we're
386 ;; always careful to put our code in low memory. Is that how it
387 ;; works? Would this break if we used a more general memory map? --
389 (fun-map (missing-arg) :type simple-vector
:read-only t
)
391 ;; Either a simple-vector or a context if there's only one context.
392 (contexts nil
:type t
:read-only t
)
393 (tlf-number nil
:type
(or index null
))
394 (char-offset nil
:type
(or index null
)))
396 (defvar *!initial-debug-sources
*)
398 (defun !debug-info-cold-init
()
399 (let ((now (get-universal-time)))
400 (dolist (debug-source *!initial-debug-sources
*)
401 (let* ((namestring (debug-source-namestring debug-source
))
402 (timestamp (file-write-date namestring
)))
403 (setf (debug-source-created debug-source
) timestamp
404 (debug-source-compiled debug-source
) now
)))))
408 ;;;; When reading from a file, we have to keep track of some source
409 ;;;; information. We also exploit our ability to back up for printing
410 ;;;; the error context and for recovering from errors.
412 ;;;; The interface we provide to this stuff is the stream-oid
413 ;;;; SOURCE-INFO structure. The bookkeeping is done as a side effect
414 ;;;; of getting the next source form.
416 ;;; A FILE-INFO structure holds all the source information for a
418 (defstruct (file-info
420 #-no-ansi-print-object
421 (:print-object
(lambda (s stream
)
422 (print-unreadable-object (s stream
:type t
)
423 (princ (file-info-name s
) stream
)))))
424 ;; If a file, the truename of the corresponding source file. If from
425 ;; a Lisp form, :LISP. If from a stream, :STREAM.
426 (name (missing-arg) :type
(or pathname
(eql :lisp
)) :read-only t
)
427 ;; the external format that we'll call OPEN with, if NAME is a file.
428 (external-format nil
:read-only t
)
429 ;; the defaulted, but not necessarily absolute file name (i.e. prior
430 ;; to TRUENAME call.) Null if not a file. This is used to set
431 ;; *COMPILE-FILE-PATHNAME*, and if absolute, is dumped in the
433 (untruename nil
:type
(or pathname null
) :read-only t
)
434 ;; the file's write date (if relevant)
435 (write-date nil
:type
(or unsigned-byte null
) :read-only t
)
436 ;; parallel vectors containing the forms read out of the file and
437 ;; the file positions that reading of each form started at (i.e. the
438 ;; end of the previous form)
439 (forms (make-array 10 :fill-pointer
0 :adjustable t
) :type
(vector t
)
441 (positions (make-array 10 :fill-pointer
0 :adjustable t
) :type
(vector t
)
443 ;; A vector of character ranges than span each subform in the TLF,
444 ;; reset to empty for each one, updated by form-tracking-stream-observer.
445 (subforms nil
:type
(or null
(vector t
)) :read-only t
:read-only t
)
446 ;; A list of objects about which the compile may/would/should have signaled
447 ;; a style-warning in the :compile-toplevel situation, so we don't do it
448 ;; again in the :load-toplevel situation.
449 ;; This is a somewhat useless thing to track, but arguably
450 ;; the "&OPTIONAL and &KEY" warning is quite annoying to see repeated.
451 ;; And I doubt it changes anyone's mind about coding style anyway.
452 ;; Typically this matters for DEFTYPE and DEFMACRO.
453 (style-warning-tracker nil
:type list
))
455 ;;; The SOURCE-INFO structure provides a handle on all the source
456 ;;; information for an entire compilation.
457 (defstruct (source-info
458 #-no-ansi-print-object
459 (:print-object
(lambda (s stream
)
460 (print-unreadable-object
461 (s stream
:type t
:identity t
))))
463 ;; the UT that compilation started at
464 (start-time (get-universal-time) :type unsigned-byte
:read-only t
)
465 ;; the IRT that compilation started at
466 (start-real-time (get-internal-real-time) :type unsigned-byte
:read-only t
)
467 ;; the FILE-INFO structure for this compilation
468 (file-info nil
:type
(or file-info null
) :read-only t
)
469 ;; the stream that we are using to read the FILE-INFO, or NIL if
470 ;; no stream has been opened yet
471 (stream nil
:type
(or stream null
))
472 ;; for coalescing DEFINITION-SOURCE-LOCATION of effectively toplevel forms
473 ;; inside one truly toplevel form.
474 (last-defn-source-loc)
475 ;; if the current compilation is recursive (e.g., due to EVAL-WHEN
476 ;; processing at compile-time), the invoking compilation's
478 (parent nil
:type
(or source-info null
) :read-only t
))