debug-info.lisp 13.1 KB
Newer Older
ram's avatar
ram committed
1
2
3
4
5
6
7
8
9
10
11
12
13
;;; -*- Log: code.log; Package: C -*-
;;;
;;; **********************************************************************
;;; This code was written as part of the Spice Lisp project at
;;; Carnegie-Mellon University, and has been placed in the public domain.
;;; If you want to use this code or any part of Spice Lisp, please contact
;;; Scott Fahlman (FAHLMAN@CMUC). 
;;; **********************************************************************
;;;
;;;    This file contains structures used for recording debugger information.
;;;
(in-package "C")

14
15
16
17
18

;;;; SC-Offsets:
;;;
;;;    We represent the place where some value is stored with a SC-OFFSET,
;;; which is the SC number and offset encoded as an integer.
ram's avatar
ram committed
19

20
21
22
23
24
25
26
(defconstant sc-offset-scn-byte (byte 5 0))
(defconstant sc-offset-offset-byte (byte 22 5))
(deftype sc-offset () '(unsigned-byte 27))

(defmacro make-sc-offset (scn offset)
  `(dpb ,scn sc-offset-scn-byte
	(dpb ,offset sc-offset-offset-byte 0)))
ram's avatar
ram committed
27

28
29
(defmacro sc-offset-scn (sco) `(ldb sc-offset-scn-byte ,sco))
(defmacro sc-offset-offset (sco) `(ldb sc-offset-offset-byte ,sco))
ram's avatar
ram committed
30

31
32
33
34
35
36
37
38

;;;; Variable length integers:
;;;
;;;    The debug info representation makes extensive use of integers encoded in
;;; a byte vector using a variable number of bytes:
;;;    0..253 => the integer
;;;    254 => read next two bytes for integer
;;;    255 => read next four bytes for integer
ram's avatar
ram committed
39

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
;;; READ-VAR-INTEGER  --  Interface
;;;
;;;    Given a byte vector Vec and an index variable Index, read a variable
;;; length integer and advance index.
;;;
(defmacro read-var-integer (vec index)
  (once-only ((val `(aref ,vec ,index)))
    `(cond ((<= ,val 253)
	    (incf ,index)
	    ,val)
	   ((= ,val 254)
	    (prog1
		(logior (aref ,vec (+ ,index 1))
			(ash (aref ,vec (+ ,index 2)) 8))
	      (incf ,index 3)))
	   (t
	    (prog1
		(logior (aref ,vec (+ ,index 1))
			(ash (aref ,vec (+ ,index 2)) 8)
	      		(ash (aref ,vec (+ ,index 3)) 16)
	      		(ash (aref ,vec (+ ,index 4)) 24))
	      (incf ,index 5))))))
ram's avatar
ram committed
62
63


64
65
66
67
68
69
70
71
72
73
;;; WRITE-VAR-INTEGER  --  Interface
;;;
;;;    Takes an adjustable vector Vec with a fill pointer and pushes the
;;; variable length representation of Int on the end.
;;;
(defun write-var-integer (int vec)
  (declare (type (unsigned-byte 32) int))
  (cond ((<= int 253)
	 (vector-push-extend int vec))
	(t
74
	 (let ((32-p (> int #xFFFF)))
75
76
77
78
79
80
81
	   (vector-push-extend (if 32-p 255 254) vec)
	   (vector-push-extend (ldb (byte 8 0) int) vec)
	   (vector-push-extend (ldb (byte 8 8) int) vec)
	   (when 32-p
	     (vector-push-extend (ldb (byte 8 16) int) vec)
	     (vector-push-extend (ldb (byte 8 24) int) vec)))))
  (undefined-value))
ram's avatar
ram committed
82
83


84
85
86
87
88

;;;; Packed strings:
;;;
;;;    A packed string is a variable length integer length followed by the
;;; character codes.
ram's avatar
ram committed
89
90


91
;;; READ-VAR-STRING  --  Interface
ram's avatar
ram committed
92
;;;
93
94
;;;    Read a packed string from Vec starting at Index, leaving advancing
;;; Index.
ram's avatar
ram committed
95
;;;
96
(defmacro read-var-string (vec index)
ram's avatar
ram committed
97
98
99
100
101
102
  (once-only ((len `(read-var-integer ,vec ,index)))
    (once-only ((res `(make-string ,len)))
      `(progn
	 (%primitive byte-blt ,vec ,index ,res 0 ,len)
	 (incf ,index ,len)
	 ,res))))
ram's avatar
ram committed
103
104


105
106
107
108
109
110
111
112
113
114
;;; WRITE-VAR-STRING  --  Interface
;;;
;;;    Write String into Vec (adjustable, fill-pointer) represented as the
;;; length (in a var-length integer) followed by the codes of the characters.
;;;
(defun write-var-string (string vec)
  (declare (simple-string string))
  (let ((len (length string)))
    (write-var-integer len vec)
    (dotimes (i len)
ram's avatar
ram committed
115
      (vector-push-extend (char-code (schar string i)) vec)))
ram's avatar
ram committed
116
  (undefined-value))
117
118


119
;;;; Packed bit vectors:
120
;;;
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

;;; READ-PACKED-BIT-VECTOR  --  Interface
;;;
;;;    Read the specified number of Bytes out of Vec at Index and convert them
;;; to a bit-vector.  Index is incremented.
;;;
(defmacro read-packed-bit-vector (bytes vec index)
  (once-only ((n-bytes bytes))
    (once-only ((n-res `(make-array (* ,n-bytes 8) :element-type 'bit)))
      `(progn
	 (%primitive byte-blt ,vec ,index ,n-res 0 ,n-bytes)
	 (incf ,index ,n-bytes)
	 ,n-res))))


;;; WRITE-PACKED-BIT-VECTOR  --  Interface
;;;
;;;    Write Bits out to Vec.  Bits must be an eight-bit multiple.
;;;
140
(defun write-packed-bit-vector (bits vec)
141
  (declare (type simple-bit-vector bits))
142
  (let ((len (length bits))
143
	(start (fill-pointer vec)))
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
    (cond ((eq target-byte-order native-byte-order)
	   (let ((bytes (ash len -3)))
	     (dotimes (i bytes)
	       (vector-push-extend 0 vec))
	     (lisp::with-array-data ((data vec) (ig1) (ig2))
	       (declare (ignore ig1 ig2))
	       (%primitive byte-blt bits 0 data start (+ start bytes)))))
	  (t
	   (macrolet ((frob (initial step done)
			`(let ((shift ,initial)
			       (byte 0))
			   (dotimes (i len)
			     (let ((int (aref bits i)))
			       (setq byte (logior byte (ash int shift)))
			       (,step shift))
			     (when ,done
			       (vector-push-extend byte vec)
			       (setq shift ,initial  byte 0)))
			   (unless (= shift ,initial)
			     (vector-push-extend byte vec)))))
	     (ecase target-byte-order
	       (:little-endian
		(frob 0 incf (= shift 8)))
	       (:big-endian
		(frob 7 decf (minusp shift))))))))
  
170
171
172
173
174
175
  (undefined-value))


;;;; Compiled debug variables:
;;;
;;;    Compiled debug variables are in a packed binary representation in the
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
;;; DEBUG-FUNCTION-VARIABLES:
;;;    single byte of boolean flags:
;;;        uninterned name
;;;	   packaged name
;;;        environment-live
;;;        has distinct save location
;;;        has ID (name not unique in this fun)
;;;    name length in bytes (as var-length integer)
;;;    ...name bytes...
;;;    [if packaged, var-length integer that is package name length]
;;;     ...package name bytes...]
;;;    [If has ID, ID as var-length integer]
;;;    SC-Offset of primary location (as var-length integer)
;;;    [If has save SC, SC-Offset of save location (as var-length integer)]

191
192
193
194
195
196
197
198
199
200
201
202
203
204
(defconstant compiled-debug-variable-uninterned		#b00000001)
(defconstant compiled-debug-variable-packaged		#b00000010)
(defconstant compiled-debug-variable-environment-live	#b00000100)
(defconstant compiled-debug-variable-save-loc-p		#b00001000)
(defconstant compiled-debug-variable-id-p		#b00010000)


;;;; Compiled debug blocks:
;;;
;;;    Compiled debug blocks are in a packed binary representation in the
;;; DEBUG-FUNCTION-BLOCKS:
;;;    number of successors + bit flags (single byte)
;;;        elsewhere-p
;;;    ...ordinal number of each successor in the function's blocks vector...
205
206
;;;    number of locations in this block
;;;    kind of first location (single byte)
207
208
209
210
;;;    delta from previous PC (or from 0 if first location in function.)
;;;    [offset of first top-level form, if no function TLF-NUMBER]
;;;    form number of first source form
;;;    first live mask (length in bytes determined by number of VARIABLES)
211
212
;;;    ...more <kind, delta, top-level form offset, form-number, live-set>
;;;       tuples...
213
214
215
216
217
218
219
220
221
222


(defconstant compiled-debug-block-nsucc-byte (byte 2 0))
(defconstant compiled-debug-block-elsewhere-p #b00000100)

(defconstant compiled-code-location-kind-byte (byte 3 0))
(defconstant compiled-code-location-kinds
  '#(:unknown-return :known-return :internal-error :non-local-exit
		     :block-start))

223
224
225
226


;;;; Debug function:

ram's avatar
ram committed
227
228
229
230
231
232
233
234
235
236
237
(defstruct debug-function)

(defstruct (compiled-debug-function (:include debug-function))
  ;;
  ;; The name of this function.  If from a DEFUN, etc., then this is the
  ;; function name, otherwise it is a descriptive string.
  (name nil :type (or simple-string cons symbol))
  ;;
  ;; The kind of function (same as FUNCTIONAL-KIND):
  (kind nil :type (member nil :optional :external :top-level :cleanup))
  ;;
238
239
240
241
242
243
244
245
246
247
248
249
250
251
  ;; A vector of the packed binary representation of variable locations in this
  ;; function.  These are in alphabetical order by name.  This ordering is used
  ;; in lifetime info to refer to variables: the first entry is 0, the second
  ;; entry is 1, etc.  Variable numbers are *not* the byte index at which the
  ;; representation of the location starts.  The entire vector must be parsed
  ;; before function, alphabetically sorted by the NAME.  This slot may be NIL
  ;; to save space.
  (variables nil :type (or (simple-array (unsigned-byte 8) (*)) null))
  ;;
  ;; A vector of the packed binary representation of the COMPILED-DEBUG-BLOCKS
  ;; in this function, in the order that the blocks were emitted.  The first
  ;; block is the start of the function.  This slot may be NIL to save space.
  (blocks nil :type (or (simple-array (unsigned-byte 8) (*)) null))
  ;;
252
253
254
255
256
  ;; If all code locations in this function are in the same top-level form,
  ;; then this is the number of that form, otherwise NIL.  If NIL, then each
  ;; code location represented in the BLOCKS specifies the TLF number.
  (tlf-number nil :type (or index null))
  ;;
257
258
259
260
261
  ;; A vector describing the variables that the argument values are stored in
  ;; within this function.  The locations are represented by the ordinal number
  ;; of the entry in the VARIABLES.  The locations are in the order that the
  ;; arguments are actually passed in, but special marker symbols can be
  ;; interspersed to indicate the orignal call syntax:
ram's avatar
ram committed
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
  ;;
  ;; DELETED
  ;;    There was an argument to the function in this position, but it was
  ;;    deleted due to lack of references.  The value cannot be recovered.
  ;;
  ;; SUPPLIED-P
  ;;    The following location is the supplied-p value for the preceding
  ;;    keyword or optional.
  ;;
  ;; REST-ARG
  ;;    The following location holds the list of rest args.
  ;;
  ;; MORE-ARG
  ;;    The following two locations are the more arg context and count.
  ;;
  ;; <any other symbol>
  ;;    The following location is the value of the keyword argument with the
  ;;    specified name.
  ;;
281
282
283
  ;; This may be NIL to save space.  If no symbols are present, then this will
  ;; be represented with an I-vector with sufficiently large element type.
  (arguments nil :type (or (simple-array * (*)) null))
ram's avatar
ram committed
284
  ;;
285
286
287
288
289
  ;; There are three alternatives for this slot:
  ;; 
  ;; A vector
  ;;    A vector of SC-OFFSETS describing the return locations.  The
  ;;    vector element type is chosen to hold the largest element.
ram's avatar
ram committed
290
  ;;
291
292
  ;; :Standard 
  ;;    The function returns using the standard unknown-values convention.
ram's avatar
ram committed
293
  ;;
294
295
296
  ;; :Fixed
  ;;    The function returns using the a fixed-values convention, but we
  ;;    elected not to store a vector to save space.
297
  (returns :fixed :type (or (simple-array * (*)) (member :standard :fixed)))
ram's avatar
ram committed
298
  ;;
ram's avatar
ram committed
299
  ;; SC-Offsets describing where the return PC and return FP are kept.
300
  (return-pc nil :type sc-offset)
ram's avatar
ram committed
301
302
303
304
305
  (old-fp nil :type sc-offset)
  ;;
  ;; SC-Offset for the number stack FP in this function, or NIL if no NFP
  ;; allocated.
  (nfp nil :type (or sc-offset null))
306
307
308
  ;;
  ;; The earliest PC in this function at which the environment is properly
  ;; initialized (arguments moved from passing locations, etc.)
309
310
311
312
  (start-pc nil :type index)
  ;;
  ;; The start of elsewhere code for this function (if any.)
  (elsewhere-pc nil :type index))
ram's avatar
ram committed
313
314
315
316
317
318
319
320
321
322
323
324
325
326


(defstruct debug-source
  ;;
  ;; This slot indicates where the definition came from:
  ;;    :File - from a file (Compile-File)
  ;;    :Lisp - from Lisp (Compile)
  ;;  :Stream - from a non-file stream (Compile-From-Stream)
  (from nil :type (member :file :stream :lisp))
  ;;
  ;; If :File, the file name, if :Lisp, the lambda compiled, otherwise some
  ;; descriptive string.
  (name nil :type (or pathname list simple-string))
  ;;
ram's avatar
ram committed
327
328
329
  ;; File comment for this file, if any.
  (comment nil :type (or simple-string null))
  ;;
ram's avatar
ram committed
330
331
332
333
334
335
336
337
338
339
  ;; The universal time that the source was written, or NIL if unavailable.
  (created nil :type (or unsigned-byte null))
  ;;
  ;; The universal time that the source was compiled.
  (compiled nil :type unsigned-byte)
  ;;
  ;; The source path root number of the first form read from this source (i.e.
  ;; the total number of forms converted previously in this compilation.)
  (source-root 0 :type index)
  ;;
340
341
  ;; The file-positions of each truly top-level form read from this file (if
  ;; applicable).  The vector element type will be chosen to hold the largest
342
  ;; element.  May be null to save space.
343
344
  (start-positions nil :type (or (simple-array * (*)) null)))

ram's avatar
ram committed
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359

(defstruct debug-info)

(defstruct (compiled-debug-info (:include debug-info))
  ;;
  ;; Some string describing something about the code in this component.
  (name nil :type simple-string)
  ;;
  ;; A list of DEBUG-SOURCE structures describing where the code for this
  ;; component came from, in the order that they were read.
  ;;
  ;; *** NOTE: the offset of this slot is wired into the fasl dumper so that it
  ;; *** can backpatch the source info when compilation is complete.
  (source nil :type list)
  ;;
360
361
362
363
  ;; The name of the package that DEBUG-FUNCTION-VARIABLES were dumped relative
  ;; to.  Locations that aren't packaged are in this package.
  (package nil :type simple-string)
  ;;
ram's avatar
ram committed
364
365
366
367
368
  ;; A simple-vector of alternating Debug-Function structures and fixnum
  ;; PCs.  This is used to map PCs to functions, so that we can figure out
  ;; what function we were running in.  The function is valid between the PC
  ;; before it (inclusive) and the PC after it (exclusive).  The PCs are in
  ;; sorted order, so we can binary-search.  We omit the first and last PC,
369
370
371
  ;; since their values are 0 and the length of the code vector.  Null only
  ;; temporarily.
  (function-map nil :type (or simple-vector null)))