23ad564e3d4ee221e378730f6f9ad6124a6b03a2
[sbcl.git] / src / code / debug-int.lisp
1 ;;;; the implementation of the programmer's interface to writing
2 ;;;; debugging tools
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
12
13 (in-package "SB!DI")
14
15 ;;; FIXME: There are an awful lot of package prefixes in this code.
16 ;;; Couldn't we have SB-DI use the SB-C and SB-VM packages?
17 \f
18 ;;;; conditions
19
20 ;;;; The interface to building debugging tools signals conditions that
21 ;;;; prevent it from adhering to its contract. These are
22 ;;;; serious-conditions because the program using the interface must
23 ;;;; handle them before it can correctly continue execution. These
24 ;;;; debugging conditions are not errors since it is no fault of the
25 ;;;; programmers that the conditions occur. The interface does not
26 ;;;; provide for programs to detect these situations other than
27 ;;;; calling a routine that detects them and signals a condition. For
28 ;;;; example, programmers call A which may fail to return successfully
29 ;;;; due to a lack of debug information, and there is no B the they
30 ;;;; could have called to realize A would fail. It is not an error to
31 ;;;; have called A, but it is an error for the program to then ignore
32 ;;;; the signal generated by A since it cannot continue without A's
33 ;;;; correctly returning a value or performing some operation.
34 ;;;;
35 ;;;; Use DEBUG-SIGNAL to signal these conditions.
36
37 (define-condition debug-condition (serious-condition)
38   ()
39   #!+sb-doc
40   (:documentation
41    "All DEBUG-CONDITIONs inherit from this type. These are serious conditions
42     that must be handled, but they are not programmer errors."))
43
44 (define-condition no-debug-info (debug-condition)
45   ((code-component :reader no-debug-info-code-component
46                    :initarg :code-component))
47   #!+sb-doc
48   (:documentation "There is no usable debugging information available.")
49   (:report (lambda (condition stream)
50              (fresh-line stream)
51              (format stream
52                      "no debug information available for ~S~%"
53                      (no-debug-info-code-component condition)))))
54
55 (define-condition no-debug-function-returns (debug-condition)
56   ((debug-function :reader no-debug-function-returns-debug-function
57                    :initarg :debug-function))
58   #!+sb-doc
59   (:documentation
60    "The system could not return values from a frame with DEBUG-FUNCTION since
61     it lacked information about returning values.")
62   (:report (lambda (condition stream)
63              (let ((fun (debug-function-function
64                          (no-debug-function-returns-debug-function condition))))
65                (format stream
66                        "~&Cannot return values from ~:[frame~;~:*~S~] since ~
67                         the debug information lacks details about returning ~
68                         values here."
69                        fun)))))
70
71 (define-condition no-debug-blocks (debug-condition)
72   ((debug-function :reader no-debug-blocks-debug-function
73                    :initarg :debug-function))
74   #!+sb-doc
75   (:documentation "The debug-function has no debug-block information.")
76   (:report (lambda (condition stream)
77              (format stream "~&~S has no debug-block information."
78                      (no-debug-blocks-debug-function condition)))))
79
80 (define-condition no-debug-vars (debug-condition)
81   ((debug-function :reader no-debug-vars-debug-function
82                    :initarg :debug-function))
83   #!+sb-doc
84   (:documentation "The debug-function has no DEBUG-VAR information.")
85   (:report (lambda (condition stream)
86              (format stream "~&~S has no debug variable information."
87                      (no-debug-vars-debug-function condition)))))
88
89 (define-condition lambda-list-unavailable (debug-condition)
90   ((debug-function :reader lambda-list-unavailable-debug-function
91                    :initarg :debug-function))
92   #!+sb-doc
93   (:documentation
94    "The debug-function has no lambda-list since argument DEBUG-VARs are
95     unavailable.")
96   (:report (lambda (condition stream)
97              (format stream "~&~S has no lambda-list information available."
98                      (lambda-list-unavailable-debug-function condition)))))
99
100 (define-condition invalid-value (debug-condition)
101   ((debug-var :reader invalid-value-debug-var :initarg :debug-var)
102    (frame :reader invalid-value-frame :initarg :frame))
103   (:report (lambda (condition stream)
104              (format stream "~&~S has :invalid or :unknown value in ~S."
105                      (invalid-value-debug-var condition)
106                      (invalid-value-frame condition)))))
107
108 (define-condition ambiguous-variable-name (debug-condition)
109   ((name :reader ambiguous-variable-name-name :initarg :name)
110    (frame :reader ambiguous-variable-name-frame :initarg :frame))
111   (:report (lambda (condition stream)
112              (format stream "~&~S names more than one valid variable in ~S."
113                      (ambiguous-variable-name-name condition)
114                      (ambiguous-variable-name-frame condition)))))
115 \f
116 ;;;; errors and DEBUG-SIGNAL
117
118 ;;; The debug-internals code tries to signal all programmer errors as
119 ;;; subtypes of DEBUG-ERROR. There are calls to ERROR signalling
120 ;;; SIMPLE-ERRORs, but these dummy checks in the code and shouldn't
121 ;;; come up.
122 ;;;
123 ;;; While under development, this code also signals errors in code
124 ;;; branches that remain unimplemented.
125
126 (define-condition debug-error (error) ()
127   #!+sb-doc
128   (:documentation
129    "All programmer errors from using the interface for building debugging
130     tools inherit from this type."))
131
132 (define-condition unhandled-debug-condition (debug-error)
133   ((condition :reader unhandled-debug-condition-condition :initarg :condition))
134   (:report (lambda (condition stream)
135              (format stream "~&unhandled DEBUG-CONDITION:~%~A"
136                      (unhandled-debug-condition-condition condition)))))
137
138 (define-condition unknown-code-location (debug-error)
139   ((code-location :reader unknown-code-location-code-location
140                   :initarg :code-location))
141   (:report (lambda (condition stream)
142              (format stream "~&invalid use of an unknown code-location: ~S"
143                      (unknown-code-location-code-location condition)))))
144
145 (define-condition unknown-debug-var (debug-error)
146   ((debug-var :reader unknown-debug-var-debug-var :initarg :debug-var)
147    (debug-function :reader unknown-debug-var-debug-function
148                    :initarg :debug-function))
149   (:report (lambda (condition stream)
150              (format stream "~&~S is not in ~S."
151                      (unknown-debug-var-debug-var condition)
152                      (unknown-debug-var-debug-function condition)))))
153
154 (define-condition invalid-control-stack-pointer (debug-error)
155   ()
156   (:report (lambda (condition stream)
157              (declare (ignore condition))
158              (fresh-line stream)
159              (write-string "invalid control stack pointer" stream))))
160
161 (define-condition frame-function-mismatch (debug-error)
162   ((code-location :reader frame-function-mismatch-code-location
163                   :initarg :code-location)
164    (frame :reader frame-function-mismatch-frame :initarg :frame)
165    (form :reader frame-function-mismatch-form :initarg :form))
166   (:report (lambda (condition stream)
167              (format
168               stream
169               "~&Form was preprocessed for ~S,~% but called on ~S:~%  ~S"
170               (frame-function-mismatch-code-location condition)
171               (frame-function-mismatch-frame condition)
172               (frame-function-mismatch-form condition)))))
173
174 ;;; This signals debug-conditions. If they go unhandled, then signal
175 ;;; an UNHANDLED-DEBUG-CONDITION error.
176 ;;;
177 ;;; ??? Get SIGNAL in the right package!
178 (defmacro debug-signal (datum &rest arguments)
179   `(let ((condition (make-condition ,datum ,@arguments)))
180      (signal condition)
181      (error 'unhandled-debug-condition :condition condition)))
182 \f
183 ;;;; structures
184 ;;;;
185 ;;;; Most of these structures model information stored in internal
186 ;;;; data structures created by the compiler. Whenever comments
187 ;;;; preface an object or type with "compiler", they refer to the
188 ;;;; internal compiler thing, not to the object or type with the same
189 ;;;; name in the "SB-DI" package.
190
191 ;;;; DEBUG-VARs
192
193 ;;; These exist for caching data stored in packed binary form in
194 ;;; compiler debug-functions. Debug-functions store these.
195 (defstruct (debug-var (:constructor nil)
196                       (:copier nil))
197   ;; the name of the variable
198   (symbol (required-argument) :type symbol)
199   ;; a unique integer identification relative to other variables with the same
200   ;; symbol
201   (id 0 :type sb!c::index)
202   ;; Does the variable always have a valid value?
203   (alive-p nil :type boolean))
204 (def!method print-object ((debug-var debug-var) stream)
205   (print-unreadable-object (debug-var stream :type t :identity t)
206     (format stream
207             "~S ~D"
208             (debug-var-symbol debug-var)
209             (debug-var-id debug-var))))
210
211 #!+sb-doc
212 (setf (fdocumentation 'debug-var-id 'function)
213   "Return the integer that makes DEBUG-VAR's name and package unique
214    with respect to other DEBUG-VARs in the same function.")
215
216 (defstruct (compiled-debug-var
217             (:include debug-var)
218             (:constructor make-compiled-debug-var
219                           (symbol id alive-p sc-offset save-sc-offset))
220             (:copier nil))
221   ;; Storage class and offset. (unexported).
222   (sc-offset nil :type sb!c::sc-offset)
223   ;; Storage class and offset when saved somewhere.
224   (save-sc-offset nil :type (or sb!c::sc-offset null)))
225
226 ;;;; frames
227
228 ;;; These represent call-frames on the stack.
229 (defstruct (frame (:constructor nil)
230                   (:copier nil))
231   ;; the next frame up, or NIL when top frame
232   (up nil :type (or frame null))
233   ;; the previous frame down, or NIL when the bottom frame. Before
234   ;; computing the next frame down, this slot holds the frame pointer
235   ;; to the control stack for the given frame. This lets us get the
236   ;; next frame down and the return-pc for that frame.
237   (%down :unparsed :type (or frame (member nil :unparsed)))
238   ;; the debug-function for the function whose call this frame
239   ;; represents
240   (debug-function nil :type debug-function)
241   ;; the code-location to continue upon return to frame
242   (code-location nil :type code-location)
243   ;; an a-list of catch-tags to code-locations
244   (%catches :unparsed :type (or list (member :unparsed)))
245   ;; pointer to frame on control stack. (unexported) When this frame
246   ;; is an interpreted-frame, this pointer is an index into the
247   ;; interpreter's stack.
248   pointer
249   ;; This is the frame's number for prompt printing. Top is zero.
250   (number 0 :type index))
251
252 #!+sb-doc
253 (setf (fdocumentation 'frame-up 'function)
254   "Return the frame immediately above frame on the stack. When frame is
255    the top of the stack, this returns nil.")
256
257 #!+sb-doc
258 (setf (fdocumentation 'frame-debug-function 'function)
259   "Return the debug-function for the function whose call frame represents.")
260
261 #!+sb-doc
262 (setf (fdocumentation 'frame-code-location 'function)
263   "Return the code-location where the frame's debug-function will continue
264    running when program execution returns to this frame. If someone
265    interrupted this frame, the result could be an unknown code-location.")
266
267 (defstruct (compiled-frame
268             (:include frame)
269             (:constructor make-compiled-frame
270                           (pointer up debug-function code-location number
271                                    #!+gengc saved-state-chain
272                                    &optional escaped))
273             (:copier nil))
274   ;; This indicates whether someone interrupted the frame.
275   ;; (unexported). If escaped, this is a pointer to the state that was
276   ;; saved when we were interrupted. On the non-gengc system, this is
277   ;; a pointer to an os_context_t, i.e. the third argument to an
278   ;; SA_SIGACTION-style signal handler. On the gengc system, this is a
279   ;; state pointer from SAVED-STATE-CHAIN.
280   escaped
281   ;; a list of SAPs to saved states. Each time we unwind past an
282   ;; exception, we pop the next entry off this list. When we get to
283   ;; the end of the list, there is nothing else on the stack.
284   #!+gengc (saved-state-chain nil :type list))
285 (def!method print-object ((obj compiled-frame) str)
286   (print-unreadable-object (obj str :type t)
287     (format str
288             "~S~:[~;, interrupted~]"
289             (debug-function-name (frame-debug-function obj))
290             (compiled-frame-escaped obj))))
291
292 (defstruct (interpreted-frame
293             (:include frame)
294             (:constructor make-interpreted-frame
295                           (pointer up debug-function code-location number
296                            real-frame closure))
297             (:copier nil))
298   ;; This points to the compiled-frame for SB!BYTECODE:INTERNAL-APPLY-LOOP.
299   (real-frame nil :type compiled-frame)
300   ;; This is the closed over data used by the interpreter.
301   (closure nil :type simple-vector))
302 (def!method print-object ((obj interpreted-frame) str)
303   (print-unreadable-object (obj str :type t)
304     (prin1 (debug-function-name (frame-debug-function obj)) str)))
305 \f
306 ;;;; DEBUG-FUNCTIONs
307
308 ;;; These exist for caching data stored in packed binary form in
309 ;;; compiler debug-functions. *COMPILED-DEBUG-FUNCTIONS* maps a
310 ;;; SB!C::DEBUG-FUNCTION to a DEBUG-FUNCTION. There should only be one
311 ;;; DEBUG-FUNCTION in existence for any function; that is, all
312 ;;; code-locations and other objects that reference DEBUG-FUNCTIONs
313 ;;; point to unique objects. This is due to the overhead in cached
314 ;;; information.
315 (defstruct (debug-function (:constructor nil)
316                            (:copier nil))
317   ;; some representation of the function arguments. See
318   ;; DEBUG-FUNCTION-LAMBDA-LIST.
319   ;; NOTE: must parse vars before parsing arg list stuff.
320   (%lambda-list :unparsed)
321   ;; cached DEBUG-VARS information (unexported).
322   ;; These are sorted by their name.
323   (%debug-vars :unparsed :type (or simple-vector null (member :unparsed)))
324   ;; cached debug-block information. This is NIL when we have tried to
325   ;; parse the packed binary info, but none is available.
326   (blocks :unparsed :type (or simple-vector null (member :unparsed)))
327   ;; the actual function if available
328   (%function :unparsed :type (or null function (member :unparsed))))
329 (def!method print-object ((obj debug-function) stream)
330   (print-unreadable-object (obj stream :type t)
331     (prin1 (debug-function-name obj) stream)))
332
333 (defstruct (compiled-debug-function
334             (:include debug-function)
335             (:constructor %make-compiled-debug-function
336                           (compiler-debug-fun component))
337             (:copier nil))
338   ;; compiler's dumped debug-function information (unexported)
339   (compiler-debug-fun nil :type sb!c::compiled-debug-function)
340   ;; code object (unexported).
341   component
342   ;; the :FUNCTION-START breakpoint (if any) used to facilitate
343   ;; function end breakpoints
344   (end-starter nil :type (or null breakpoint)))
345
346 ;;; This maps SB!C::COMPILED-DEBUG-FUNCTIONs to
347 ;;; COMPILED-DEBUG-FUNCTIONs, so we can get at cached stuff and not
348 ;;; duplicate COMPILED-DEBUG-FUNCTION structures.
349 (defvar *compiled-debug-functions* (make-hash-table :test 'eq))
350
351 ;;; Make a COMPILED-DEBUG-FUNCTION for a SB!C::COMPILER-DEBUG-FUNCTION
352 ;;; and its component. This maps the latter to the former in
353 ;;; *COMPILED-DEBUG-FUNCTIONS*. If there already is a
354 ;;; COMPILED-DEBUG-FUNCTION, then this returns it from
355 ;;; *COMPILED-DEBUG-FUNCTIONS*.
356 (defun make-compiled-debug-function (compiler-debug-fun component)
357   (or (gethash compiler-debug-fun *compiled-debug-functions*)
358       (setf (gethash compiler-debug-fun *compiled-debug-functions*)
359             (%make-compiled-debug-function compiler-debug-fun component))))
360
361 (defstruct (bogus-debug-function
362             (:include debug-function)
363             (:constructor make-bogus-debug-function
364                           (%name &aux (%lambda-list nil) (%debug-vars nil)
365                                  (blocks nil) (%function nil)))
366             (:copier nil))
367   %name)
368
369 (defvar *ir1-lambda-debug-function* (make-hash-table :test 'eq))
370 \f
371 ;;;; DEBUG-BLOCKs
372
373 ;;; These exist for caching data stored in packed binary form in compiler
374 ;;; DEBUG-BLOCKs.
375 (defstruct (debug-block (:constructor nil)
376                         (:copier nil))
377   ;; Code-locations where execution continues after this block.
378   (successors nil :type list)
379   ;; This indicates whether the block is a special glob of code shared
380   ;; by various functions and tucked away elsewhere in a component.
381   ;; This kind of block has no start code-location. This slot is in
382   ;; all debug-blocks since it is an exported interface.
383   (elsewhere-p nil :type boolean))
384 (def!method print-object ((obj debug-block) str)
385   (print-unreadable-object (obj str :type t)
386     (prin1 (debug-block-function-name obj) str)))
387
388 #!+sb-doc
389 (setf (fdocumentation 'debug-block-successors 'function)
390   "Returns the list of possible code-locations where execution may continue
391    when the basic-block represented by debug-block completes its execution.")
392
393 #!+sb-doc
394 (setf (fdocumentation 'debug-block-elsewhere-p 'function)
395   "Returns whether debug-block represents elsewhere code.")
396
397 (defstruct (compiled-debug-block (:include debug-block)
398                                  (:constructor
399                                   make-compiled-debug-block
400                                   (code-locations successors elsewhere-p))
401                                  (:copier nil))
402   ;; code-location information for the block
403   (code-locations nil :type simple-vector))
404
405 (defvar *ir1-block-debug-block* (make-hash-table :test 'eq))
406 \f
407 ;;;; breakpoints
408
409 ;;; This is an internal structure that manages information about a
410 ;;; breakpoint locations. See *COMPONENT-BREAKPOINT-OFFSETS*.
411 (defstruct (breakpoint-data (:constructor make-breakpoint-data
412                                           (component offset))
413                             (:copier nil))
414   ;; This is the component in which the breakpoint lies.
415   component
416   ;; This is the byte offset into the component.
417   (offset nil :type sb!c::index)
418   ;; The original instruction replaced by the breakpoint.
419   (instruction nil :type (or null (unsigned-byte 32)))
420   ;; A list of user breakpoints at this location.
421   (breakpoints nil :type list))
422 (def!method print-object ((obj breakpoint-data) str)
423   (print-unreadable-object (obj str :type t)
424     (format str "~S at ~S"
425             (debug-function-name
426              (debug-function-from-pc (breakpoint-data-component obj)
427                                      (breakpoint-data-offset obj)))
428             (breakpoint-data-offset obj))))
429
430 (defstruct (breakpoint (:constructor %make-breakpoint
431                                      (hook-function what kind %info))
432                        (:copier nil))
433   ;; This is the function invoked when execution encounters the
434   ;; breakpoint. It takes a frame, the breakpoint, and optionally a
435   ;; list of values. Values are supplied for :FUNCTION-END breakpoints
436   ;; as values to return for the function containing the breakpoint.
437   ;; :FUNCTION-END breakpoint hook-functions also take a cookie
438   ;; argument. See COOKIE-FUN slot.
439   (hook-function nil :type function)
440   ;; CODE-LOCATION or DEBUG-FUNCTION
441   (what nil :type (or code-location debug-function))
442   ;; :CODE-LOCATION, :FUNCTION-START, or :FUNCTION-END for that kind
443   ;; of breakpoint. :UNKNOWN-RETURN-PARTNER if this is the partner of
444   ;; a :code-location breakpoint at an :UNKNOWN-RETURN code-location.
445   (kind nil :type (member :code-location :function-start :function-end
446                           :unknown-return-partner))
447   ;; Status helps the user and the implementation.
448   (status :inactive :type (member :active :inactive :deleted))
449   ;; This is a backpointer to a breakpoint-data.
450   (internal-data nil :type (or null breakpoint-data))
451   ;; With code-locations whose type is :UNKNOWN-RETURN, there are
452   ;; really two breakpoints: one at the multiple-value entry point,
453   ;; and one at the single-value entry point. This slot holds the
454   ;; breakpoint for the other one, or NIL if this isn't at an
455   ;; :UNKNOWN-RETURN code location.
456   (unknown-return-partner nil :type (or null breakpoint))
457   ;; :FUNCTION-END breakpoints use a breakpoint at the :FUNCTION-START
458   ;; to establish the end breakpoint upon function entry. We do this
459   ;; by frobbing the LRA to jump to a special piece of code that
460   ;; breaks and provides the return values for the returnee. This slot
461   ;; points to the start breakpoint, so we can activate, deactivate,
462   ;; and delete it.
463   (start-helper nil :type (or null breakpoint))
464   ;; This is a hook users supply to get a dynamically unique cookie
465   ;; for identifying :FUNCTION-END breakpoint executions. That is, if
466   ;; there is one :FUNCTION-END breakpoint, but there may be multiple
467   ;; pending calls of its function on the stack. This function takes
468   ;; the cookie, and the hook-function takes the cookie too.
469   (cookie-fun nil :type (or null function))
470   ;; This slot users can set with whatever information they find useful.
471   %info)
472 (def!method print-object ((obj breakpoint) str)
473   (let ((what (breakpoint-what obj)))
474     (print-unreadable-object (obj str :type t)
475       (format str
476               "~S~:[~;~:*~S~]"
477               (etypecase what
478                 (code-location what)
479                 (debug-function (debug-function-name what)))
480               (etypecase what
481                 (code-location nil)
482                 (debug-function (breakpoint-kind obj)))))))
483
484 #!+sb-doc
485 (setf (fdocumentation 'breakpoint-hook-function 'function)
486   "Returns the breakpoint's function the system calls when execution encounters
487    the breakpoint, and it is active. This is SETF'able.")
488
489 #!+sb-doc
490 (setf (fdocumentation 'breakpoint-what 'function)
491   "Returns the breakpoint's what specification.")
492
493 #!+sb-doc
494 (setf (fdocumentation 'breakpoint-kind 'function)
495   "Returns the breakpoint's kind specification.")
496
497 ;;;; CODE-LOCATIONs
498
499 (defstruct (code-location (:constructor nil)
500                           (:copier nil))
501   ;; This is the debug-function containing code-location.
502   (debug-function nil :type debug-function)
503   ;; This is initially :UNSURE. Upon first trying to access an
504   ;; :unparsed slot, if the data is unavailable, then this becomes t,
505   ;; and the code-location is unknown. If the data is available, this
506   ;; becomes nil, a known location. We can't use a separate type
507   ;; code-location for this since we must return code-locations before
508   ;; we can tell whether they're known or unknown. For example, when
509   ;; parsing the stack, we don't want to unpack all the variables and
510   ;; blocks just to make frames.
511   (%unknown-p :unsure :type (member t nil :unsure))
512   ;; This is the debug-block containing code-location. Possibly toss
513   ;; this out and just find it in the blocks cache in debug-function.
514   (%debug-block :unparsed :type (or debug-block (member :unparsed)))
515   ;; This is the number of forms processed by the compiler or loader
516   ;; before the top-level form containing this code-location.
517   (%tlf-offset :unparsed :type (or sb!c::index (member :unparsed)))
518   ;; This is the depth-first number of the node that begins
519   ;; code-location within its top-level form.
520   (%form-number :unparsed :type (or sb!c::index (member :unparsed))))
521 (def!method print-object ((obj code-location) str)
522   (print-unreadable-object (obj str :type t)
523     (prin1 (debug-function-name (code-location-debug-function obj))
524            str)))
525
526 #!+sb-doc
527 (setf (fdocumentation 'code-location-debug-function 'function)
528   "Returns the debug-function representing information about the function
529    corresponding to the code-location.")
530
531 (defstruct (compiled-code-location
532             (:include code-location)
533             (:constructor make-known-code-location
534                           (pc debug-function %tlf-offset %form-number
535                               %live-set kind &aux (%unknown-p nil)))
536             (:constructor make-compiled-code-location (pc debug-function))
537             (:copier nil))
538   ;; This is an index into debug-function's component slot.
539   (pc nil :type sb!c::index)
540   ;; This is a bit-vector indexed by a variable's position in
541   ;; DEBUG-FUNCTION-DEBUG-VARS indicating whether the variable has a
542   ;; valid value at this code-location. (unexported).
543   (%live-set :unparsed :type (or simple-bit-vector (member :unparsed)))
544   ;; (unexported) To see SB!C::LOCATION-KIND, do
545   ;; (SB!KERNEL:TYPE-EXPAND 'SB!C::LOCATION-KIND).
546   (kind :unparsed :type (or (member :unparsed) sb!c::location-kind)))
547 \f
548 ;;;; DEBUG-SOURCEs
549
550 ;;; Return the number of top-level forms processed by the compiler
551 ;;; before compiling this source. If this source is uncompiled, this
552 ;;; is zero. This may be zero even if the source is compiled since the
553 ;;; first form in the first file compiled in one compilation, for
554 ;;; example, must have a root number of zero -- the compiler saw no
555 ;;; other top-level forms before it.
556 (defun debug-source-root-number (debug-source)
557   (sb!c::debug-source-source-root debug-source))
558 \f
559 ;;;; frames
560
561 ;;; This is used in FIND-ESCAPED-FRAME and with the bogus components
562 ;;; and LRAs used for :function-end breakpoints. When a components
563 ;;; debug-info slot is :bogus-lra, then the real-lra-slot contains the
564 ;;; real component to continue executing, as opposed to the bogus
565 ;;; component which appeared in some frame's LRA location.
566 (defconstant real-lra-slot sb!vm:code-constants-offset)
567
568 ;;; These are magically converted by the compiler.
569 (defun current-sp () (current-sp))
570 (defun current-fp () (current-fp))
571 (defun stack-ref (s n) (stack-ref s n))
572 (defun %set-stack-ref (s n value) (%set-stack-ref s n value))
573 (defun function-code-header (fun) (function-code-header fun))
574 #!-gengc (defun lra-code-header (lra) (lra-code-header lra))
575 (defun make-lisp-obj (value) (make-lisp-obj value))
576 (defun get-lisp-obj-address (thing) (get-lisp-obj-address thing))
577 (defun function-word-offset (fun) (function-word-offset fun))
578
579 #!-sb-fluid (declaim (inline cstack-pointer-valid-p))
580 (defun cstack-pointer-valid-p (x)
581   (declare (type system-area-pointer x))
582   #!-x86 ; stack grows toward high address values
583   (and (sap< x (current-sp))
584        (sap<= #!-gengc (int-sap control-stack-start)
585               #!+gengc (mutator-control-stack-base)
586               x)
587        (zerop (logand (sap-int x) #b11)))
588   #!+x86 ; stack grows toward low address values
589   (and (sap>= x (current-sp))
590        (sap> (int-sap control-stack-end) x)
591        (zerop (logand (sap-int x) #b11))))
592
593 #!+(or gengc x86)
594 (sb!alien:def-alien-routine component-ptr-from-pc (system-area-pointer)
595   (pc system-area-pointer))
596
597 #!+(or gengc x86)
598 (defun component-from-component-ptr (component-ptr)
599   (declare (type system-area-pointer component-ptr))
600   (make-lisp-obj (logior (sap-int component-ptr)
601                          sb!vm:other-pointer-type)))
602
603 ;;;; X86 support
604
605 #!+x86
606 (progn
607
608 (defun compute-lra-data-from-pc (pc)
609   (declare (type system-area-pointer pc))
610   (let ((component-ptr (component-ptr-from-pc pc)))
611     (unless (sap= component-ptr (int-sap #x0))
612        (let* ((code (component-from-component-ptr component-ptr))
613               (code-header-len (* (get-header-data code) sb!vm:word-bytes))
614               (pc-offset (- (sap-int pc)
615                             (- (get-lisp-obj-address code)
616                                sb!vm:other-pointer-type)
617                             code-header-len)))
618 ;        (format t "c-lra-fpc ~A ~A ~A~%" pc code pc-offset)
619          (values pc-offset code)))))
620
621 (defconstant sb!vm::nargs-offset #.sb!vm::ecx-offset)
622
623 ;;; Check for a valid return address - it could be any valid C/Lisp
624 ;;; address.
625 ;;;
626 ;;; XXX Could be a little smarter.
627 #!-sb-fluid (declaim (inline ra-pointer-valid-p))
628 (defun ra-pointer-valid-p (ra)
629   (declare (type system-area-pointer ra))
630   (and
631    ;; Not the first page which is unmapped.
632    (>= (sap-int ra) 4096)
633    ;; Not a Lisp stack pointer.
634    (not (cstack-pointer-valid-p ra))))
635
636 ;;; Try to find a valid previous stack. This is complex on the x86 as
637 ;;; it can jump between C and Lisp frames. To help find a valid frame
638 ;;; it searches backwards.
639 ;;;
640 ;;; XXX Should probably check whether it has reached the bottom of the
641 ;;; stack.
642 ;;;
643 ;;; XXX Should handle interrupted frames, both Lisp and C. At present
644 ;;; it manages to find a fp trail, see linux hack below.
645 (defun x86-call-context (fp &key (depth 0))
646   (declare (type system-area-pointer fp)
647            (fixnum depth))
648   ;;(format t "*CC ~S ~S~%" fp depth)
649   (cond
650    ((not (cstack-pointer-valid-p fp))
651     #+nil (format t "debug invalid fp ~S~%" fp)
652     nil)
653    (t
654     ;; Check the two possible frame pointers.
655     (let ((lisp-ocfp (sap-ref-sap fp (- (* (1+ sb!vm::ocfp-save-offset) 4))))
656           (lisp-ra (sap-ref-sap fp (- (* (1+ sb!vm::return-pc-save-offset)
657                                          4))))
658           (c-ocfp (sap-ref-sap fp (* 0 sb!vm:word-bytes)))
659           (c-ra (sap-ref-sap fp (* 1 sb!vm:word-bytes))))
660       (cond ((and (sap> lisp-ocfp fp) (cstack-pointer-valid-p lisp-ocfp)
661                   (ra-pointer-valid-p lisp-ra)
662                   (sap> c-ocfp fp) (cstack-pointer-valid-p c-ocfp)
663                   (ra-pointer-valid-p c-ra))
664              #+nil (format t
665                            "*C Both valid ~S ~S ~S ~S~%"
666                            lisp-ocfp lisp-ra c-ocfp c-ra)
667              ;; Look forward another step to check their validity.
668              (let ((lisp-path-fp (x86-call-context lisp-ocfp
669                                                    :depth (1+ depth)))
670                    (c-path-fp (x86-call-context c-ocfp :depth (1+ depth))))
671                (cond ((and lisp-path-fp c-path-fp)
672                        ;; Both still seem valid - choose the lisp frame.
673                        #+nil (when (zerop depth)
674                                (format t
675                                        "debug: both still valid ~S ~S ~S ~S~%"
676                                        lisp-ocfp lisp-ra c-ocfp c-ra))
677                       #+freebsd
678                       (if (sap> lisp-ocfp c-ocfp)
679                         (values lisp-ra lisp-ocfp)
680                         (values c-ra c-ocfp))
681                        #-freebsd
682                        (values lisp-ra lisp-ocfp))
683                      (lisp-path-fp
684                       ;; The lisp convention is looking good.
685                       #+nil (format t "*C lisp-ocfp ~S ~S~%" lisp-ocfp lisp-ra)
686                       (values lisp-ra lisp-ocfp))
687                      (c-path-fp
688                       ;; The C convention is looking good.
689                       #+nil (format t "*C c-ocfp ~S ~S~%" c-ocfp c-ra)
690                       (values c-ra c-ocfp))
691                      (t
692                       ;; Neither seems right?
693                       #+nil (format t "debug: no valid2 fp found ~S ~S~%"
694                                     lisp-ocfp c-ocfp)
695                       nil))))
696             ((and (sap> lisp-ocfp fp) (cstack-pointer-valid-p lisp-ocfp)
697                   (ra-pointer-valid-p lisp-ra))
698              ;; The lisp convention is looking good.
699              #+nil (format t "*C lisp-ocfp ~S ~S~%" lisp-ocfp lisp-ra)
700              (values lisp-ra lisp-ocfp))
701             ((and (sap> c-ocfp fp) (cstack-pointer-valid-p c-ocfp)
702                   #!-linux (ra-pointer-valid-p c-ra))
703              ;; The C convention is looking good.
704              #+nil (format t "*C c-ocfp ~S ~S~%" c-ocfp c-ra)
705              (values c-ra c-ocfp))
706             (t
707              #+nil (format t "debug: no valid fp found ~S ~S~%"
708                            lisp-ocfp c-ocfp)
709              nil))))))
710
711 ) ; #+x86 PROGN
712 \f
713 ;;; Convert the descriptor into a SAP. The bits all stay the same, we just
714 ;;; change our notion of what we think they are.
715 #!-sb-fluid (declaim (inline descriptor-sap))
716 (defun descriptor-sap (x)
717   (int-sap (get-lisp-obj-address x)))
718
719 ;;; Return the top frame of the control stack as it was before calling
720 ;;; this function.
721 (defun top-frame ()
722   (multiple-value-bind (fp pc) (%caller-frame-and-pc)
723     (possibly-an-interpreted-frame
724      (compute-calling-frame (descriptor-sap fp)
725                             #!-gengc pc #!+gengc (descriptor-sap pc)
726                             nil)
727      nil)))
728
729 ;;; Flush all of the frames above FRAME, and renumber all the frames
730 ;;; below FRAME.
731 (defun flush-frames-above (frame)
732   (setf (frame-up frame) nil)
733   (do ((number 0 (1+ number))
734        (frame frame (frame-%down frame)))
735       ((not (frame-p frame)))
736     (setf (frame-number frame) number)))
737
738 ;;; Return the frame immediately below FRAME on the stack; or when
739 ;;; FRAME is the bottom of the stack, return NIL.
740 (defun frame-down (frame)
741   ;; We have to access the old-fp and return-pc out of frame and pass
742   ;; them to COMPUTE-CALLING-FRAME.
743   (let ((down (frame-%down frame)))
744     (if (eq down :unparsed)
745         (let* ((real (frame-real-frame frame))
746                (debug-fun (frame-debug-function real)))
747           (setf (frame-%down frame)
748                 (etypecase debug-fun
749                   (compiled-debug-function
750                    (let ((c-d-f (compiled-debug-function-compiler-debug-fun
751                                  debug-fun)))
752                      (possibly-an-interpreted-frame
753                       (compute-calling-frame
754                        (descriptor-sap
755                         (get-context-value
756                          real sb!vm::ocfp-save-offset
757                          (sb!c::compiled-debug-function-old-fp c-d-f)))
758                        #!-gengc
759                        (get-context-value
760                         real sb!vm::lra-save-offset
761                         (sb!c::compiled-debug-function-return-pc c-d-f))
762                        #!+gengc
763                        (descriptor-sap
764                         (get-context-value
765                          real sb!vm::ra-save-offset
766                          (sb!c::compiled-debug-function-return-pc c-d-f)))
767                        frame)
768                       frame)))
769                   (bogus-debug-function
770                    (let ((fp (frame-pointer real)))
771                      (when (cstack-pointer-valid-p fp)
772                        #!+x86
773                         (multiple-value-bind (ra ofp) (x86-call-context fp)
774                           (compute-calling-frame ofp ra frame))
775                         #!-x86
776                        (compute-calling-frame
777                         #!-alpha
778                         (sap-ref-sap fp (* sb!vm::ocfp-save-offset
779                                            sb!vm:word-bytes))
780                         #!+alpha
781                         (int-sap
782                          (sap-ref-32 fp (* sb!vm::ocfp-save-offset
783                                            sb!vm:word-bytes)))
784
785                         #!-gengc
786                         (stack-ref fp sb!vm::lra-save-offset)
787                         #!+gengc
788                         (sap-ref-sap fp (* sb!vm::ra-save-offset
789                                            sb!vm:word-bytes))
790                         frame)))))))
791         down)))
792
793 ;;; Get the old FP or return PC out of FRAME. STACK-SLOT is the
794 ;;; standard save location offset on the stack. LOC is the saved
795 ;;; SC-OFFSET describing the main location.
796 #!-x86
797 (defun get-context-value (frame stack-slot loc)
798   (declare (type compiled-frame frame) (type unsigned-byte stack-slot)
799            (type sb!c::sc-offset loc))
800   (let ((pointer (frame-pointer frame))
801         (escaped (compiled-frame-escaped frame)))
802     (if escaped
803         (sub-access-debug-var-slot pointer loc escaped)
804         (stack-ref pointer stack-slot))))
805 #!+x86
806 (defun get-context-value (frame stack-slot loc)
807   (declare (type compiled-frame frame) (type unsigned-byte stack-slot)
808            (type sb!c::sc-offset loc))
809   (let ((pointer (frame-pointer frame))
810         (escaped (compiled-frame-escaped frame)))
811     (if escaped
812         (sub-access-debug-var-slot pointer loc escaped)
813         (ecase stack-slot
814           (#.sb!vm::ocfp-save-offset
815            (stack-ref pointer stack-slot))
816           (#.sb!vm::lra-save-offset
817            (sap-ref-sap pointer (- (* (1+ stack-slot) 4))))))))
818
819 #!-x86
820 (defun (setf get-context-value) (value frame stack-slot loc)
821   (declare (type compiled-frame frame) (type unsigned-byte stack-slot)
822            (type sb!c::sc-offset loc))
823   (let ((pointer (frame-pointer frame))
824         (escaped (compiled-frame-escaped frame)))
825     (if escaped
826         (sub-set-debug-var-slot pointer loc value escaped)
827         (setf (stack-ref pointer stack-slot) value))))
828
829 #!+x86
830 (defun (setf get-context-value) (value frame stack-slot loc)
831   (declare (type compiled-frame frame) (type unsigned-byte stack-slot)
832            (type sb!c::sc-offset loc))
833   (let ((pointer (frame-pointer frame))
834         (escaped (compiled-frame-escaped frame)))
835     (if escaped
836         (sub-set-debug-var-slot pointer loc value escaped)
837         (ecase stack-slot
838           (#.sb!vm::ocfp-save-offset
839            (setf (stack-ref pointer stack-slot) value))
840           (#.sb!vm::lra-save-offset
841            (setf (sap-ref-sap pointer (- (* (1+ stack-slot) 4))) value))))))
842
843 ;;; This doesn't do anything in sbcl-0.7.0, since the functionality
844 ;;; was lost in the switch from IR1 interpreter to bytecode interpreter.
845 ;;; However, it might be revived someday. (See the FIXME for
846 ;;; POSSIBLY-AN-INTERPRETED-FRAME.)
847 ;;;
848 ;;; (defvar *debugging-interpreter* nil
849 ;;;   #!+sb-doc
850 ;;;   "When set, the debugger foregoes making interpreted frames, so you can
851 ;;;    debug the functions that manifest the interpreter.")
852
853 ;;; Note: In CMU CL with the IR1 interpreter, this did
854 ;;;    This takes a newly computed frame, FRAME, and the frame above it
855 ;;;    on the stack, UP-FRAME, which is possibly NIL. FRAME is NIL when
856 ;;;    we hit the bottom of the control stack. When FRAME represents a
857 ;;;    call to SB!BYTECODE::INTERNAL-APPLY-LOOP, we make an interpreted frame
858 ;;;    to replace FRAME. The interpreted frame points to FRAME.
859 ;;; But with SBCL's switch to byte-interpreter-only, this is functionality
860 ;;; wasn't maintained, so this is just a placeholder, and when you
861 ;;; try to "debug byte code" you end up debugging the byte interpreter
862 ;;; instead.
863 ;;; 
864 ;;; (It might be good to update the old CMU CL functionality so that
865 ;;; you can really debug byte code instead of seeing a bunch of
866 ;;; confusing byte interpreter implementation stuff, so I've left the
867 ;;; placeholder in place. But be aware that doing so is a big messy
868 ;;; job: grep for 'interpreted-debug-' in the sbcl-0.6.13 sources to
869 ;;; see what you're getting into. -- WHN)
870 (defun possibly-an-interpreted-frame (frame up-frame)
871
872   ;; new SBCL code, not ambitious enough to do anything tricky like
873   ;; hiding the byte interpreter when debugging
874   (declare (ignore up-frame))
875   frame
876
877   ;; old CMU CL code to hide IR1 interpreter when debugging 
878   ;;
879   ;;(if (or (not frame)
880   ;;        (not (eq (debug-function-name (frame-debug-function
881   ;;                                       frame))
882   ;;                 'sb!bytecode::internal-apply-loop))
883   ;;        *debugging-interpreter*
884   ;;        (compiled-frame-escaped frame))
885   ;;    frame
886   ;;    (flet ((get-var (name location)
887   ;;             (let ((vars (sb!di:ambiguous-debug-vars
888   ;;                          (sb!di:frame-debug-function frame) name)))
889   ;;               (when (or (null vars) (> (length vars) 1))
890   ;;                 (error "zero or more than one ~A variable in ~
891   ;;                         SB!BYTECODE::INTERNAL-APPLY-LOOP"
892   ;;                        (string-downcase name)))
893   ;;               (if (eq (debug-var-validity (car vars) location)
894   ;;                       :valid)
895   ;;                   (car vars)))))
896   ;;      (let* ((code-loc (frame-code-location frame))
897   ;;             (ptr-var (get-var "FRAME-PTR" code-loc))
898   ;;             (node-var (get-var "NODE" code-loc))
899   ;;             (closure-var (get-var "CLOSURE" code-loc)))
900   ;;        (if (and ptr-var node-var closure-var)
901   ;;            (let* ((node (debug-var-value node-var frame))
902   ;;                   (d-fun (make-interpreted-debug-function
903   ;;                           (sb!c::block-home-lambda (sb!c::node-block
904   ;;                                                     node)))))
905   ;;              (make-interpreted-frame
906   ;;               (debug-var-value ptr-var frame)
907   ;;               up-frame
908   ;;               d-fun
909   ;;               (make-interpreted-code-location node d-fun)
910   ;;               (frame-number frame)
911   ;;               frame
912   ;;               (debug-var-value closure-var frame)))
913   ;;            frame))))
914   )
915
916 ;;; This returns a frame for the one existing in time immediately
917 ;;; prior to the frame referenced by current-fp. This is current-fp's
918 ;;; caller or the next frame down the control stack. If there is no
919 ;;; down frame, this returns nil for the bottom of the stack. Up-frame
920 ;;; is the up link for the resulting frame object, and it is nil when
921 ;;; we call this to get the top of the stack.
922 ;;;
923 ;;; The current frame contains the pointer to the temporally previous
924 ;;; frame we want, and the current frame contains the pc at which we
925 ;;; will continue executing upon returning to that previous frame.
926 ;;;
927 ;;; Note: Sometimes LRA is actually a fixnum. This happens when lisp
928 ;;; calls into C. In this case, the code object is stored on the stack
929 ;;; after the LRA, and the LRA is the word offset.
930 #!-(or gengc x86)
931 (defun compute-calling-frame (caller lra up-frame)
932   (declare (type system-area-pointer caller))
933   (when (cstack-pointer-valid-p caller)
934     (multiple-value-bind (code pc-offset escaped)
935         (if lra
936             (multiple-value-bind (word-offset code)
937                 (if (fixnump lra)
938                     (let ((fp (frame-pointer up-frame)))
939                       (values lra
940                               (stack-ref fp (1+ sb!vm::lra-save-offset))))
941                     (values (get-header-data lra)
942                             (lra-code-header lra)))
943               (if code
944                   (values code
945                           (* (1+ (- word-offset (get-header-data code)))
946                              sb!vm:word-bytes)
947                           nil)
948                   (values :foreign-function
949                           0
950                           nil)))
951             (find-escaped-frame caller))
952       (if (and (code-component-p code)
953                (eq (%code-debug-info code) :bogus-lra))
954           (let ((real-lra (code-header-ref code real-lra-slot)))
955             (compute-calling-frame caller real-lra up-frame))
956           (let ((d-fun (case code
957                          (:undefined-function
958                           (make-bogus-debug-function
959                            "undefined function"))
960                          (:foreign-function
961                           (make-bogus-debug-function
962                            "foreign function call land"))
963                          ((nil)
964                           (make-bogus-debug-function
965                            "bogus stack frame"))
966                          (t
967                           (debug-function-from-pc code pc-offset)))))
968             (make-compiled-frame caller up-frame d-fun
969                                  (code-location-from-pc d-fun pc-offset
970                                                         escaped)
971                                  (if up-frame (1+ (frame-number up-frame)) 0)
972                                  escaped))))))
973
974 #!+x86
975 (defun compute-calling-frame (caller ra up-frame)
976   (declare (type system-area-pointer caller ra))
977   (when (cstack-pointer-valid-p caller)
978     ;; First check for an escaped frame.
979     (multiple-value-bind (code pc-offset escaped) (find-escaped-frame caller)
980         (cond (code
981                ;; If it's escaped it may be a function end breakpoint trap.
982                (when (and (code-component-p code)
983                           (eq (%code-debug-info code) :bogus-lra))
984                  ;; If :bogus-lra grab the real lra.
985                  (setq pc-offset (code-header-ref
986                                   code (1+ real-lra-slot)))
987                  (setq code (code-header-ref code real-lra-slot))
988                  (aver code)))
989               (t
990                ;; not escaped
991                (multiple-value-setq (pc-offset code)
992                  (compute-lra-data-from-pc ra))
993                (unless code
994                  (setf code :foreign-function
995                        pc-offset 0
996                        escaped nil))))
997
998         (let ((d-fun (case code
999                            (:undefined-function
1000                             (make-bogus-debug-function
1001                              "undefined function"))
1002                            (:foreign-function
1003                             (make-bogus-debug-function
1004                              "foreign function call land"))
1005                            ((nil)
1006                             (make-bogus-debug-function
1007                              "bogus stack frame"))
1008                            (t
1009                             (debug-function-from-pc code pc-offset)))))
1010           (make-compiled-frame caller up-frame d-fun
1011                                (code-location-from-pc d-fun pc-offset
1012                                                       escaped)
1013                                (if up-frame (1+ (frame-number up-frame)) 0)
1014                                escaped)))))
1015
1016 #!+x86
1017 (defun find-escaped-frame (frame-pointer)
1018   (declare (type system-area-pointer frame-pointer))
1019   (dotimes (index *free-interrupt-context-index* (values nil 0 nil))
1020     (sb!alien:with-alien
1021         ((lisp-interrupt-contexts (array (* os-context-t) nil)
1022                                   :extern))
1023       (let ((context (sb!alien:deref lisp-interrupt-contexts index)))
1024         (when (= (sap-int frame-pointer)
1025                  (sb!vm:context-register context sb!vm::cfp-offset))
1026           (without-gcing
1027            (let* ((component-ptr (component-ptr-from-pc
1028                                   (sb!vm:context-pc context)))
1029                   (code (unless (sap= component-ptr (int-sap #x0))
1030                           (component-from-component-ptr component-ptr))))
1031              (when (null code)
1032                (return (values code 0 context)))
1033              (let* ((code-header-len (* (get-header-data code)
1034                                         sb!vm:word-bytes))
1035                     (pc-offset
1036                      (- (sap-int (sb!vm:context-pc context))
1037                         (- (get-lisp-obj-address code)
1038                            sb!vm:other-pointer-type)
1039                         code-header-len)))
1040                (unless (<= 0 pc-offset
1041                            (* (code-header-ref code sb!vm:code-code-size-slot)
1042                               sb!vm:word-bytes))
1043                  ;; We were in an assembly routine. Therefore, use the
1044                  ;; LRA as the pc.
1045                  ;;
1046                  ;; FIXME: Should this be WARN or ERROR or what?
1047                  (format t "** pc-offset ~S not in code obj ~S?~%"
1048                          pc-offset code))
1049                (return
1050                 (values code pc-offset context))))))))))
1051
1052 #!-x86
1053 (defun find-escaped-frame (frame-pointer)
1054   (declare (type system-area-pointer frame-pointer))
1055   (dotimes (index *free-interrupt-context-index* (values nil 0 nil))
1056     (sb!alien:with-alien
1057      ((lisp-interrupt-contexts (array (* os-context-t) nil) :extern))
1058      (let ((scp (sb!alien:deref lisp-interrupt-contexts index)))
1059        (when (= (sap-int frame-pointer)
1060                 (sb!vm:context-register scp sb!vm::cfp-offset))
1061          (without-gcing
1062           (let ((code (code-object-from-bits
1063                        (sb!vm:context-register scp sb!vm::code-offset))))
1064             (when (symbolp code)
1065               (return (values code 0 scp)))
1066             (let* ((code-header-len (* (get-header-data code)
1067                                        sb!vm:word-bytes))
1068                    (pc-offset
1069                     (- (sap-int (sb!vm:context-pc scp))
1070                        (- (get-lisp-obj-address code)
1071                           sb!vm:other-pointer-type)
1072                        code-header-len)))
1073               ;; Check to see whether we were executing in a branch
1074               ;; delay slot.
1075               #!+(or pmax sgi) ; pmax only (and broken anyway)
1076               (when (logbitp 31 (sb!alien:slot scp '%mips::sc-cause))
1077                 (incf pc-offset sb!vm:word-bytes))
1078               (unless (<= 0 pc-offset
1079                           (* (code-header-ref code sb!vm:code-code-size-slot)
1080                              sb!vm:word-bytes))
1081                 ;; We were in an assembly routine. Therefore, use the
1082                 ;; LRA as the pc.
1083                 (setf pc-offset
1084                       (- (sb!vm:context-register scp sb!vm::lra-offset)
1085                          (get-lisp-obj-address code)
1086                          code-header-len)))
1087                (return
1088                 (if (eq (%code-debug-info code) :bogus-lra)
1089                     (let ((real-lra (code-header-ref code
1090                                                      real-lra-slot)))
1091                       (values (lra-code-header real-lra)
1092                               (get-header-data real-lra)
1093                               nil))
1094                   (values code pc-offset scp)))))))))))
1095
1096 ;;; Find the code object corresponding to the object represented by
1097 ;;; bits and return it. We assume bogus functions correspond to the
1098 ;;; undefined-function.
1099 #!-gengc
1100 (defun code-object-from-bits (bits)
1101   (declare (type (unsigned-byte 32) bits))
1102   (let ((object (make-lisp-obj bits)))
1103     (if (functionp object)
1104         (or (function-code-header object)
1105             :undefined-function)
1106         (let ((lowtag (get-lowtag object)))
1107           (if (= lowtag sb!vm:other-pointer-type)
1108               (let ((type (get-type object)))
1109                 (cond ((= type sb!vm:code-header-type)
1110                        object)
1111                       ((= type sb!vm:return-pc-header-type)
1112                        (lra-code-header object))
1113                       (t
1114                        nil))))))))
1115
1116 ;;; SB!KERNEL:*SAVED-STATE-CHAIN* -- maintained by the C code as a
1117 ;;; list of SAPs, each SAP pointing to a saved exception state.
1118 #!+gengc
1119 (declaim (special *saved-state-chain*))
1120
1121 ;;; CMU CL had
1122 ;;;   (DEFUN LOOKUP-TRACE-TABLE-ENTRY (COMPONENT PC) ..)
1123 ;;; for this case, but it hasn't been maintained in SBCL.
1124 #!+gengc
1125 (eval-when (:compile-toplevel :load-toplevel :execute)
1126   (error "hopelessly stale"))
1127
1128 ;;; CMU CL had
1129 ;;;   (DEFUN EXTRACT-INFO-FROM-STATE (STATE) ..)
1130 ;;; for this case, but it hasn't been maintained in SBCL.
1131 #!+gengc
1132 (eval-when (:compile-toplevel :load-toplevel :execute)
1133   (error "hopelessly stale"))
1134
1135 ;;; CMU CL had
1136 ;;;   (DEFUN COMPUTE-CALLING-FRAME (OCFP RA UP-FRAME) ..)
1137 ;;; for this case, but it hasn't been maintained in SBCL.
1138 #!+gengc
1139 (eval-when (:compile-toplevel :load-toplevel :execute)
1140   (error "hopelessly stale"))
1141 \f
1142 ;;;; frame utilities
1143
1144 ;;; This returns a COMPILED-DEBUG-FUNCTION for code and pc. We fetch
1145 ;;; the SB!C::DEBUG-INFO and run down its function-map to get a
1146 ;;; SB!C::COMPILED-DEBUG-FUNCTION from the pc. The result only needs
1147 ;;; to reference the component, for function constants, and the
1148 ;;; SB!C::COMPILED-DEBUG-FUNCTION.
1149 (defun debug-function-from-pc (component pc)
1150   (let ((info (%code-debug-info component)))
1151     (cond
1152      ((not info)
1153       (debug-signal 'no-debug-info :code-component component))
1154      ((eq info :bogus-lra)
1155       (make-bogus-debug-function "function end breakpoint"))
1156      (t
1157       (let* ((function-map (get-debug-info-function-map info))
1158              (len (length function-map)))
1159         (declare (simple-vector function-map))
1160         (if (= len 1)
1161             (make-compiled-debug-function (svref function-map 0) component)
1162             (let ((i 1)
1163                   (elsewhere-p
1164                    (>= pc (sb!c::compiled-debug-function-elsewhere-pc
1165                            (svref function-map 0)))))
1166               (declare (type sb!int:index i))
1167               (loop
1168                 (when (or (= i len)
1169                           (< pc (if elsewhere-p
1170                                     (sb!c::compiled-debug-function-elsewhere-pc
1171                                      (svref function-map (1+ i)))
1172                                     (svref function-map i))))
1173                   (return (make-compiled-debug-function
1174                            (svref function-map (1- i))
1175                            component)))
1176                 (incf i 2)))))))))
1177
1178 ;;; This returns a code-location for the COMPILED-DEBUG-FUNCTION,
1179 ;;; DEBUG-FUN, and the pc into its code vector. If we stopped at a
1180 ;;; breakpoint, find the CODE-LOCATION for that breakpoint. Otherwise,
1181 ;;; make an :UNSURE code location, so it can be filled in when we
1182 ;;; figure out what is going on.
1183 (defun code-location-from-pc (debug-fun pc escaped)
1184   (or (and (compiled-debug-function-p debug-fun)
1185            escaped
1186            (let ((data (breakpoint-data
1187                         (compiled-debug-function-component debug-fun)
1188                         pc nil)))
1189              (when (and data (breakpoint-data-breakpoints data))
1190                (let ((what (breakpoint-what
1191                             (first (breakpoint-data-breakpoints data)))))
1192                  (when (compiled-code-location-p what)
1193                    what)))))
1194       (make-compiled-code-location pc debug-fun)))
1195
1196 ;;; Return an alist mapping catch tags to CODE-LOCATIONs. These are
1197 ;;; CODE-LOCATIONs at which execution would continue with frame as the
1198 ;;; top frame if someone threw to the corresponding tag.
1199 (defun frame-catches (frame)
1200   (let ((catch
1201          #!-gengc (descriptor-sap *current-catch-block*)
1202          #!+gengc (mutator-current-catch-block))
1203         (res nil)
1204         (fp (frame-pointer (frame-real-frame frame))))
1205     (loop
1206       (when (zerop (sap-int catch)) (return (nreverse res)))
1207       (when (sap= fp
1208                   #!-alpha
1209                   (sap-ref-sap catch
1210                                       (* sb!vm:catch-block-current-cont-slot
1211                                          sb!vm:word-bytes))
1212                   #!+alpha
1213                   (:int-sap
1214                    (sap-ref-32 catch
1215                                       (* sb!vm:catch-block-current-cont-slot
1216                                          sb!vm:word-bytes))))
1217         (let* (#!-(or gengc x86)
1218                (lra (stack-ref catch sb!vm:catch-block-entry-pc-slot))
1219                #!+(or gengc x86)
1220                (ra (sap-ref-sap
1221                     catch (* sb!vm:catch-block-entry-pc-slot
1222                              sb!vm:word-bytes)))
1223                #!-x86
1224                (component
1225                 (stack-ref catch sb!vm:catch-block-current-code-slot))
1226                #!+x86
1227                (component (component-from-component-ptr
1228                            (component-ptr-from-pc ra)))
1229                (offset
1230                 #!-(or gengc x86)
1231                 (* (- (1+ (get-header-data lra))
1232                       (get-header-data component))
1233                    sb!vm:word-bytes)
1234                 #!+gengc
1235                 (+ (- (sap-int ra)
1236                       (get-lisp-obj-address component)
1237                       (get-header-data component))
1238                    sb!vm:other-pointer-type)
1239                 #!+x86
1240                 (- (sap-int ra)
1241                    (- (get-lisp-obj-address component)
1242                       sb!vm:other-pointer-type)
1243                    (* (get-header-data component) sb!vm:word-bytes))))
1244           (push (cons #!-x86
1245                       (stack-ref catch sb!vm:catch-block-tag-slot)
1246                       #!+x86
1247                       (make-lisp-obj
1248                        (sap-ref-32 catch (* sb!vm:catch-block-tag-slot
1249                                                    sb!vm:word-bytes)))
1250                       (make-compiled-code-location
1251                        offset (frame-debug-function frame)))
1252                 res)))
1253       (setf catch
1254             #!-alpha
1255             (sap-ref-sap catch
1256                                 (* sb!vm:catch-block-previous-catch-slot
1257                                    sb!vm:word-bytes))
1258             #!+alpha
1259             (:int-sap
1260              (sap-ref-32 catch
1261                                 (* sb!vm:catch-block-previous-catch-slot
1262                                    sb!vm:word-bytes)))))))
1263
1264 ;;; If an interpreted frame, return the real frame, otherwise frame.
1265 (defun frame-real-frame (frame)
1266   (etypecase frame
1267     (compiled-frame frame)
1268     (interpreted-frame (interpreted-frame-real-frame frame))))
1269 \f
1270 ;;;; operations on DEBUG-FUNCTIONs
1271
1272 ;;; Execute the forms in a context with block-var bound to each
1273 ;;; debug-block in debug-function successively. Result is an optional
1274 ;;; form to execute for return values, and DO-DEBUG-FUNCTION-BLOCKS
1275 ;;; returns nil if there is no result form. This signals a
1276 ;;; no-debug-blocks condition when the debug-function lacks
1277 ;;; debug-block information.
1278 (defmacro do-debug-function-blocks ((block-var debug-function &optional result)
1279                                     &body body)
1280   (let ((blocks (gensym))
1281         (i (gensym)))
1282     `(let ((,blocks (debug-function-debug-blocks ,debug-function)))
1283        (declare (simple-vector ,blocks))
1284        (dotimes (,i (length ,blocks) ,result)
1285          (let ((,block-var (svref ,blocks ,i)))
1286            ,@body)))))
1287
1288 ;;; Execute body in a context with var bound to each debug-var in
1289 ;;; debug-function. This returns the value of executing result (defaults to
1290 ;;; nil). This may iterate over only some of debug-function's variables or none
1291 ;;; depending on debug policy; for example, possibly the compilation only
1292 ;;; preserved argument information.
1293 (defmacro do-debug-function-variables ((var debug-function &optional result)
1294                                        &body body)
1295   (let ((vars (gensym))
1296         (i (gensym)))
1297     `(let ((,vars (debug-function-debug-vars ,debug-function)))
1298        (declare (type (or null simple-vector) ,vars))
1299        (if ,vars
1300            (dotimes (,i (length ,vars) ,result)
1301              (let ((,var (svref ,vars ,i)))
1302                ,@body))
1303            ,result))))
1304
1305 ;;; Return the Common Lisp function associated with the debug-function. This
1306 ;;; returns nil if the function is unavailable or is non-existent as a user
1307 ;;; callable function object.
1308 (defun debug-function-function (debug-function)
1309   (let ((cached-value (debug-function-%function debug-function)))
1310     (if (eq cached-value :unparsed)
1311         (setf (debug-function-%function debug-function)
1312               (etypecase debug-function
1313                 (compiled-debug-function
1314                  (let ((component
1315                         (compiled-debug-function-component debug-function))
1316                        (start-pc
1317                         (sb!c::compiled-debug-function-start-pc
1318                          (compiled-debug-function-compiler-debug-fun
1319                           debug-function))))
1320                    (do ((entry (%code-entry-points component)
1321                                (%function-next entry)))
1322                        ((null entry) nil)
1323                      (when (= start-pc
1324                               (sb!c::compiled-debug-function-start-pc
1325                                (compiled-debug-function-compiler-debug-fun
1326                                 (function-debug-function entry))))
1327                        (return entry)))))
1328                 (bogus-debug-function nil)))
1329         cached-value)))
1330
1331 ;;; Return the name of the function represented by debug-function. This may
1332 ;;; be a string or a cons; do not assume it is a symbol.
1333 (defun debug-function-name (debug-function)
1334   (etypecase debug-function
1335     (compiled-debug-function
1336      (sb!c::compiled-debug-function-name
1337       (compiled-debug-function-compiler-debug-fun debug-function)))
1338     (bogus-debug-function
1339      (bogus-debug-function-%name debug-function))))
1340
1341 ;;; Return a debug-function that represents debug information for function.
1342 (defun function-debug-function (fun)
1343   (ecase (get-type fun)
1344     (#.sb!vm:closure-header-type
1345      (function-debug-function (%closure-function fun)))
1346     (#.sb!vm:funcallable-instance-header-type
1347      (function-debug-function (funcallable-instance-function fun)))
1348     ((#.sb!vm:function-header-type #.sb!vm:closure-function-header-type)
1349       (let* ((name (%function-name fun))
1350              (component (function-code-header fun))
1351              (res (find-if
1352                    (lambda (x)
1353                      (and (sb!c::compiled-debug-function-p x)
1354                           (eq (sb!c::compiled-debug-function-name x) name)
1355                           (eq (sb!c::compiled-debug-function-kind x) nil)))
1356                    (get-debug-info-function-map
1357                     (%code-debug-info component)))))
1358         (if res
1359             (make-compiled-debug-function res component)
1360             ;; KLUDGE: comment from CMU CL:
1361             ;;   This used to be the non-interpreted branch, but
1362             ;;   William wrote it to return the debug-fun of fun's XEP
1363             ;;   instead of fun's debug-fun. The above code does this
1364             ;;   more correctly, but it doesn't get or eliminate all
1365             ;;   appropriate cases. It mostly works, and probably
1366             ;;   works for all named functions anyway.
1367             ;; -- WHN 20000120
1368             (debug-function-from-pc component
1369                                     (* (- (function-word-offset fun)
1370                                           (get-header-data component))
1371                                        sb!vm:word-bytes)))))))
1372
1373 ;;; Return the kind of the function, which is one of :OPTIONAL,
1374 ;;; :EXTERNAL, TOP-level, :CLEANUP, or NIL.
1375 (defun debug-function-kind (debug-function)
1376   ;; FIXME: This "is one of" information should become part of the function
1377   ;; declamation, not just a doc string
1378   (etypecase debug-function
1379     (compiled-debug-function
1380      (sb!c::compiled-debug-function-kind
1381       (compiled-debug-function-compiler-debug-fun debug-function)))
1382     (bogus-debug-function
1383      nil)))
1384
1385 ;;; Is there any variable information for DEBUG-FUNCTION?
1386 (defun debug-var-info-available (debug-function)
1387   (not (not (debug-function-debug-vars debug-function))))
1388
1389 ;;; Return a list of debug-vars in debug-function having the same name
1390 ;;; and package as symbol. If symbol is uninterned, then this returns
1391 ;;; a list of debug-vars without package names and with the same name
1392 ;;; as symbol. The result of this function is limited to the
1393 ;;; availability of variable information in debug-function; for
1394 ;;; example, possibly DEBUG-FUNCTION only knows about its arguments.
1395 (defun debug-function-symbol-variables (debug-function symbol)
1396   (let ((vars (ambiguous-debug-vars debug-function (symbol-name symbol)))
1397         (package (and (symbol-package symbol)
1398                       (package-name (symbol-package symbol)))))
1399     (delete-if (if (stringp package)
1400                    (lambda (var)
1401                      (let ((p (debug-var-package-name var)))
1402                        (or (not (stringp p))
1403                            (string/= p package))))
1404                    (lambda (var)
1405                      (stringp (debug-var-package-name var))))
1406                vars)))
1407
1408 ;;; Return a list of debug-vars in debug-function whose names contain
1409 ;;; name-prefix-string as an intial substring. The result of this
1410 ;;; function is limited to the availability of variable information in
1411 ;;; debug-function; for example, possibly debug-function only knows
1412 ;;; about its arguments.
1413 (defun ambiguous-debug-vars (debug-function name-prefix-string)
1414   (declare (simple-string name-prefix-string))
1415   (let ((variables (debug-function-debug-vars debug-function)))
1416     (declare (type (or null simple-vector) variables))
1417     (if variables
1418         (let* ((len (length variables))
1419                (prefix-len (length name-prefix-string))
1420                (pos (find-variable name-prefix-string variables len))
1421                (res nil))
1422           (when pos
1423             ;; Find names from pos to variable's len that contain prefix.
1424             (do ((i pos (1+ i)))
1425                 ((= i len))
1426               (let* ((var (svref variables i))
1427                      (name (debug-var-symbol-name var))
1428                      (name-len (length name)))
1429                 (declare (simple-string name))
1430                 (when (/= (or (string/= name-prefix-string name
1431                                         :end1 prefix-len :end2 name-len)
1432                               prefix-len)
1433                           prefix-len)
1434                   (return))
1435                 (push var res)))
1436             (setq res (nreverse res)))
1437           res))))
1438
1439 ;;; This returns a position in variables for one containing name as an
1440 ;;; initial substring. End is the length of variables if supplied.
1441 (defun find-variable (name variables &optional end)
1442   (declare (simple-vector variables)
1443            (simple-string name))
1444   (let ((name-len (length name)))
1445     (position name variables
1446               :test #'(lambda (x y)
1447                         (let* ((y (debug-var-symbol-name y))
1448                                (y-len (length y)))
1449                           (declare (simple-string y))
1450                           (and (>= y-len name-len)
1451                                (string= x y :end1 name-len :end2 name-len))))
1452               :end (or end (length variables)))))
1453
1454 ;;; Return a list representing the lambda-list for DEBUG-FUNCTION. The
1455 ;;; list has the following structure:
1456 ;;;   (required-var1 required-var2
1457 ;;;    ...
1458 ;;;    (:optional var3 suppliedp-var4)
1459 ;;;    (:optional var5)
1460 ;;;    ...
1461 ;;;    (:rest var6) (:rest var7)
1462 ;;;    ...
1463 ;;;    (:keyword keyword-symbol var8 suppliedp-var9)
1464 ;;;    (:keyword keyword-symbol var10)
1465 ;;;    ...
1466 ;;;   )
1467 ;;; Each VARi is a DEBUG-VAR; however it may be the symbol :DELETED if
1468 ;;; it is unreferenced in DEBUG-FUNCTION. This signals a
1469 ;;; LAMBDA-LIST-UNAVAILABLE condition when there is no argument list
1470 ;;; information.
1471 (defun debug-function-lambda-list (debug-function)
1472   (etypecase debug-function
1473     (compiled-debug-function
1474      (compiled-debug-function-lambda-list debug-function))
1475     (bogus-debug-function
1476      nil)))
1477
1478 ;;; Note: If this has to compute the lambda list, it caches it in
1479 ;;; DEBUG-FUNCTION.
1480 (defun compiled-debug-function-lambda-list (debug-function)
1481   (let ((lambda-list (debug-function-%lambda-list debug-function)))
1482     (cond ((eq lambda-list :unparsed)
1483            (multiple-value-bind (args argsp)
1484                (parse-compiled-debug-function-lambda-list debug-function)
1485              (setf (debug-function-%lambda-list debug-function) args)
1486              (if argsp
1487                  args
1488                  (debug-signal 'lambda-list-unavailable
1489                                :debug-function debug-function))))
1490           (lambda-list)
1491           ((bogus-debug-function-p debug-function)
1492            nil)
1493           ((sb!c::compiled-debug-function-arguments
1494             (compiled-debug-function-compiler-debug-fun
1495              debug-function))
1496            ;; If the packed information is there (whether empty or not) as
1497            ;; opposed to being nil, then returned our cached value (nil).
1498            nil)
1499           (t
1500            ;; Our cached value is nil, and the packed lambda-list information
1501            ;; is nil, so we don't have anything available.
1502            (debug-signal 'lambda-list-unavailable
1503                          :debug-function debug-function)))))
1504
1505 ;;; COMPILED-DEBUG-FUNCTION-LAMBDA-LIST calls this when a
1506 ;;; compiled-debug-function has no lambda-list information cached. It
1507 ;;; returns the lambda-list as the first value and whether there was
1508 ;;; any argument information as the second value. Therefore, nil and t
1509 ;;; means there were no arguments, but nil and nil means there was no
1510 ;;; argument information.
1511 (defun parse-compiled-debug-function-lambda-list (debug-function)
1512   (let ((args (sb!c::compiled-debug-function-arguments
1513                (compiled-debug-function-compiler-debug-fun
1514                 debug-function))))
1515     (cond
1516      ((not args)
1517       (values nil nil))
1518      ((eq args :minimal)
1519       (values (coerce (debug-function-debug-vars debug-function) 'list)
1520               t))
1521      (t
1522       (let ((vars (debug-function-debug-vars debug-function))
1523             (i 0)
1524             (len (length args))
1525             (res nil)
1526             (optionalp nil))
1527         (declare (type (or null simple-vector) vars))
1528         (loop
1529           (when (>= i len) (return))
1530           (let ((ele (aref args i)))
1531             (cond
1532              ((symbolp ele)
1533               (case ele
1534                 (sb!c::deleted
1535                  ;; Deleted required arg at beginning of args array.
1536                  (push :deleted res))
1537                 (sb!c::optional-args
1538                  (setf optionalp t))
1539                 (sb!c::supplied-p
1540                  ;; SUPPLIED-P var immediately following keyword or
1541                  ;; optional. Stick the extra var in the result
1542                  ;; element representing the keyword or optional,
1543                  ;; which is the previous one.
1544                  (nconc (car res)
1545                         (list (compiled-debug-function-lambda-list-var
1546                                args (incf i) vars))))
1547                 (sb!c::rest-arg
1548                  (push (list :rest
1549                              (compiled-debug-function-lambda-list-var
1550                               args (incf i) vars))
1551                        res))
1552                 (sb!c::more-arg
1553                  ;; Just ignore the fact that the next two args are
1554                  ;; the &MORE arg context and count, and act like they
1555                  ;; are regular arguments.
1556                  nil)
1557                 (t
1558                  ;; &KEY arg
1559                  (push (list :keyword
1560                              ele
1561                              (compiled-debug-function-lambda-list-var
1562                               args (incf i) vars))
1563                        res))))
1564              (optionalp
1565               ;; We saw an optional marker, so the following
1566               ;; non-symbols are indexes indicating optional
1567               ;; variables.
1568               (push (list :optional (svref vars ele)) res))
1569              (t
1570               ;; Required arg at beginning of args array.
1571               (push (svref vars ele) res))))
1572           (incf i))
1573         (values (nreverse res) t))))))
1574
1575 ;;; This is used in COMPILED-DEBUG-FUNCTION-LAMBDA-LIST.
1576 (defun compiled-debug-function-lambda-list-var (args i vars)
1577   (declare (type (simple-array * (*)) args)
1578            (simple-vector vars))
1579   (let ((ele (aref args i)))
1580     (cond ((not (symbolp ele)) (svref vars ele))
1581           ((eq ele 'sb!c::deleted) :deleted)
1582           (t (error "malformed arguments description")))))
1583
1584 (defun compiled-debug-function-debug-info (debug-fun)
1585   (%code-debug-info (compiled-debug-function-component debug-fun)))
1586 \f
1587 ;;;; unpacking variable and basic block data
1588
1589 (defvar *parsing-buffer*
1590   (make-array 20 :adjustable t :fill-pointer t))
1591 (defvar *other-parsing-buffer*
1592   (make-array 20 :adjustable t :fill-pointer t))
1593 ;;; PARSE-DEBUG-BLOCKS, PARSE-DEBUG-VARS and UNCOMPACT-FUNCTION-MAP
1594 ;;; use this to unpack binary encoded information. It returns the
1595 ;;; values returned by the last form in body.
1596 ;;;
1597 ;;; This binds buffer-var to *parsing-buffer*, makes sure it starts at
1598 ;;; element zero, and makes sure if we unwind, we nil out any set
1599 ;;; elements for GC purposes.
1600 ;;;
1601 ;;; This also binds other-var to *other-parsing-buffer* when it is
1602 ;;; supplied, making sure it starts at element zero and that we nil
1603 ;;; out any elements if we unwind.
1604 ;;;
1605 ;;; This defines the local macro RESULT that takes a buffer, copies
1606 ;;; its elements to a resulting simple-vector, nil's out elements, and
1607 ;;; restarts the buffer at element zero. RESULT returns the
1608 ;;; simple-vector.
1609 (eval-when (:compile-toplevel :execute)
1610 (sb!xc:defmacro with-parsing-buffer ((buffer-var &optional other-var)
1611                                      &body body)
1612   (let ((len (gensym))
1613         (res (gensym)))
1614     `(unwind-protect
1615          (let ((,buffer-var *parsing-buffer*)
1616                ,@(if other-var `((,other-var *other-parsing-buffer*))))
1617            (setf (fill-pointer ,buffer-var) 0)
1618            ,@(if other-var `((setf (fill-pointer ,other-var) 0)))
1619            (macrolet ((result (buf)
1620                         `(let* ((,',len (length ,buf))
1621                                 (,',res (make-array ,',len)))
1622                            (replace ,',res ,buf :end1 ,',len :end2 ,',len)
1623                            (fill ,buf nil :end ,',len)
1624                            (setf (fill-pointer ,buf) 0)
1625                            ,',res)))
1626              ,@body))
1627      (fill *parsing-buffer* nil)
1628      ,@(if other-var `((fill *other-parsing-buffer* nil))))))
1629 ) ; EVAL-WHEN
1630
1631 ;;; The argument is a debug internals structure. This returns the
1632 ;;; debug-blocks for debug-function, regardless of whether we have
1633 ;;; unpacked them yet. It signals a no-debug-blocks condition if it
1634 ;;; can't return the blocks.
1635 (defun debug-function-debug-blocks (debug-function)
1636   (let ((blocks (debug-function-blocks debug-function)))
1637     (cond ((eq blocks :unparsed)
1638            (setf (debug-function-blocks debug-function)
1639                  (parse-debug-blocks debug-function))
1640            (unless (debug-function-blocks debug-function)
1641              (debug-signal 'no-debug-blocks
1642                            :debug-function debug-function))
1643            (debug-function-blocks debug-function))
1644           (blocks)
1645           (t
1646            (debug-signal 'no-debug-blocks
1647                          :debug-function debug-function)))))
1648
1649 ;;; This returns a SIMPLE-VECTOR of DEBUG-BLOCKs or NIL. NIL indicates
1650 ;;; there was no basic block information.
1651 (defun parse-debug-blocks (debug-function)
1652   (etypecase debug-function
1653     (compiled-debug-function
1654      (parse-compiled-debug-blocks debug-function))
1655     (bogus-debug-function
1656      (debug-signal 'no-debug-blocks :debug-function debug-function))))
1657
1658 ;;; This does some of the work of PARSE-DEBUG-BLOCKS.
1659 (defun parse-compiled-debug-blocks (debug-function)
1660   (let* ((debug-fun (compiled-debug-function-compiler-debug-fun
1661                      debug-function))
1662          (var-count (length (debug-function-debug-vars debug-function)))
1663          (blocks (sb!c::compiled-debug-function-blocks debug-fun))
1664          ;; KLUDGE: 8 is a hard-wired constant in the compiler for the
1665          ;; element size of the packed binary representation of the
1666          ;; blocks data.
1667          (live-set-len (ceiling var-count 8))
1668          (tlf-number (sb!c::compiled-debug-function-tlf-number debug-fun)))
1669     (unless blocks (return-from parse-compiled-debug-blocks nil))
1670     (macrolet ((aref+ (a i) `(prog1 (aref ,a ,i) (incf ,i))))
1671       (with-parsing-buffer (blocks-buffer locations-buffer)
1672         (let ((i 0)
1673               (len (length blocks))
1674               (last-pc 0))
1675           (loop
1676             (when (>= i len) (return))
1677             (let ((succ-and-flags (aref+ blocks i))
1678                   (successors nil))
1679               (declare (type (unsigned-byte 8) succ-and-flags)
1680                        (list successors))
1681               (dotimes (k (ldb sb!c::compiled-debug-block-nsucc-byte
1682                                succ-and-flags))
1683                 (push (sb!c::read-var-integer blocks i) successors))
1684               (let* ((locations
1685                       (dotimes (k (sb!c::read-var-integer blocks i)
1686                                   (result locations-buffer))
1687                         (let ((kind (svref sb!c::*compiled-code-location-kinds*
1688                                            (aref+ blocks i)))
1689                               (pc (+ last-pc
1690                                      (sb!c::read-var-integer blocks i)))
1691                               (tlf-offset (or tlf-number
1692                                               (sb!c::read-var-integer blocks
1693                                                                       i)))
1694                               (form-number (sb!c::read-var-integer blocks i))
1695                               (live-set (sb!c::read-packed-bit-vector
1696                                          live-set-len blocks i)))
1697                           (vector-push-extend (make-known-code-location
1698                                                pc debug-function tlf-offset
1699                                                form-number live-set kind)
1700                                               locations-buffer)
1701                           (setf last-pc pc))))
1702                      (block (make-compiled-debug-block
1703                              locations successors
1704                              (not (zerop (logand
1705                                           sb!c::compiled-debug-block-elsewhere-p
1706                                           succ-and-flags))))))
1707                 (vector-push-extend block blocks-buffer)
1708                 (dotimes (k (length locations))
1709                   (setf (code-location-%debug-block (svref locations k))
1710                         block))))))
1711         (let ((res (result blocks-buffer)))
1712           (declare (simple-vector res))
1713           (dotimes (i (length res))
1714             (let* ((block (svref res i))
1715                    (succs nil))
1716               (dolist (ele (debug-block-successors block))
1717                 (push (svref res ele) succs))
1718               (setf (debug-block-successors block) succs)))
1719           res)))))
1720
1721 ;;; The argument is a debug internals structure. This returns NIL if
1722 ;;; there is no variable information. It returns an empty
1723 ;;; simple-vector if there were no locals in the function. Otherwise
1724 ;;; it returns a SIMPLE-VECTOR of DEBUG-VARs.
1725 (defun debug-function-debug-vars (debug-function)
1726   (let ((vars (debug-function-%debug-vars debug-function)))
1727     (if (eq vars :unparsed)
1728         (setf (debug-function-%debug-vars debug-function)
1729               (etypecase debug-function
1730                 (compiled-debug-function
1731                  (parse-compiled-debug-vars debug-function))
1732                 (bogus-debug-function nil)))
1733         vars)))
1734
1735 ;;; VARS is the parsed variables for a minimal debug function. We need
1736 ;;; to assign names of the form ARG-NNN. We must pad with leading
1737 ;;; zeros, since the arguments must be in alphabetical order.
1738 (defun assign-minimal-var-names (vars)
1739   (declare (simple-vector vars))
1740   (let* ((len (length vars))
1741          (width (length (format nil "~D" (1- len)))))
1742     (dotimes (i len)
1743       (setf (compiled-debug-var-symbol (svref vars i))
1744             (intern (format nil "ARG-~V,'0D" width i)
1745                     ;; KLUDGE: It's somewhat nasty to have a bare
1746                     ;; package name string here. It would probably be
1747                     ;; better to have #.(FIND-PACKAGE "SB!DEBUG")
1748                     ;; instead, since then at least it would transform
1749                     ;; correctly under package renaming and stuff.
1750                     ;; However, genesis can't handle dumped packages..
1751                     ;; -- WHN 20000129
1752                     ;;
1753                     ;; FIXME: Maybe this could be fixed by moving the
1754                     ;; whole debug-int.lisp file to warm init? (after
1755                     ;; which dumping a #.(FIND-PACKAGE ..) expression
1756                     ;; would work fine) If this is possible, it would
1757                     ;; probably be a good thing, since minimizing the
1758                     ;; amount of stuff in cold init is basically good.
1759                     "SB-DEBUG")))))
1760
1761 ;;; Parse the packed representation of DEBUG-VARs from
1762 ;;; DEBUG-FUNCTION's SB!C::COMPILED-DEBUG-FUNCTION, returning a vector
1763 ;;; of DEBUG-VARs, or NIL if there was no information to parse.
1764 (defun parse-compiled-debug-vars (debug-function)
1765   (let* ((cdebug-fun (compiled-debug-function-compiler-debug-fun debug-function))
1766          (packed-vars (sb!c::compiled-debug-function-variables cdebug-fun))
1767          (args-minimal (eq (sb!c::compiled-debug-function-arguments cdebug-fun)
1768                            :minimal)))
1769     (when packed-vars
1770       (do ((i 0)
1771            (buffer (make-array 0 :fill-pointer 0 :adjustable t)))
1772           ((>= i (length packed-vars))
1773            (let ((result (coerce buffer 'simple-vector)))
1774              (when args-minimal
1775                (assign-minimal-var-names result))
1776              result))
1777         (flet ((geti () (prog1 (aref packed-vars i) (incf i))))
1778           (let* ((flags (geti))
1779                  (minimal (logtest sb!c::compiled-debug-var-minimal-p flags))
1780                  (deleted (logtest sb!c::compiled-debug-var-deleted-p flags))
1781                  (live (logtest sb!c::compiled-debug-var-environment-live flags))
1782                  (save (logtest sb!c::compiled-debug-var-save-loc-p flags))
1783                  (symbol (if minimal nil (geti)))
1784                  (id (if (logtest sb!c::compiled-debug-var-id-p flags)
1785                          (geti)
1786                          0))
1787                  (sc-offset (if deleted 0 (geti)))
1788                  (save-sc-offset (if save (geti) nil)))
1789             (aver (not (and args-minimal (not minimal))))
1790             (vector-push-extend (make-compiled-debug-var symbol
1791                                                          id
1792                                                          live
1793                                                          sc-offset
1794                                                          save-sc-offset)
1795                                 buffer)))))))
1796 \f
1797 ;;;; unpacking minimal debug functions
1798
1799 (eval-when (:compile-toplevel :execute)
1800
1801 ;;; sleazoid "macro" to keep our indentation sane in UNCOMPACT-FUNCTION-MAP
1802 (sb!xc:defmacro make-uncompacted-debug-fun ()
1803   '(sb!c::make-compiled-debug-function
1804     :name
1805     (let ((base (ecase (ldb sb!c::minimal-debug-function-name-style-byte
1806                             options)
1807                   (#.sb!c::minimal-debug-function-name-symbol
1808                    (intern (sb!c::read-var-string map i)
1809                            (sb!c::compiled-debug-info-package info)))
1810                   (#.sb!c::minimal-debug-function-name-packaged
1811                    (let ((pkg (sb!c::read-var-string map i)))
1812                      (intern (sb!c::read-var-string map i) pkg)))
1813                   (#.sb!c::minimal-debug-function-name-uninterned
1814                    (make-symbol (sb!c::read-var-string map i)))
1815                   (#.sb!c::minimal-debug-function-name-component
1816                    (sb!c::compiled-debug-info-name info)))))
1817       (if (logtest flags sb!c::minimal-debug-function-setf-bit)
1818           `(setf ,base)
1819           base))
1820     :kind (svref sb!c::*minimal-debug-function-kinds*
1821                  (ldb sb!c::minimal-debug-function-kind-byte options))
1822     :variables
1823     (when vars-p
1824       (let ((len (sb!c::read-var-integer map i)))
1825         (prog1 (subseq map i (+ i len))
1826           (incf i len))))
1827     :arguments (when vars-p :minimal)
1828     :returns
1829     (ecase (ldb sb!c::minimal-debug-function-returns-byte options)
1830       (#.sb!c::minimal-debug-function-returns-standard
1831        :standard)
1832       (#.sb!c::minimal-debug-function-returns-fixed
1833        :fixed)
1834       (#.sb!c::minimal-debug-function-returns-specified
1835        (with-parsing-buffer (buf)
1836          (dotimes (idx (sb!c::read-var-integer map i))
1837            (vector-push-extend (sb!c::read-var-integer map i) buf))
1838          (result buf))))
1839     :return-pc (sb!c::read-var-integer map i)
1840     :old-fp (sb!c::read-var-integer map i)
1841     :nfp (when (logtest flags sb!c::minimal-debug-function-nfp-bit)
1842            (sb!c::read-var-integer map i))
1843     :start-pc
1844     (progn
1845       (setq code-start-pc (+ code-start-pc (sb!c::read-var-integer map i)))
1846       (+ code-start-pc (sb!c::read-var-integer map i)))
1847     :elsewhere-pc
1848     (setq elsewhere-pc (+ elsewhere-pc (sb!c::read-var-integer map i)))))
1849
1850 ) ; EVAL-WHEN
1851
1852 ;;; Return a normal function map derived from a minimal debug info
1853 ;;; function map. This involves looping parsing
1854 ;;; minimal-debug-functions and then building a vector out of them.
1855 ;;;
1856 ;;; FIXME: This and its helper macro just above become dead code now
1857 ;;; that we no longer use compacted function maps.
1858 (defun uncompact-function-map (info)
1859   (declare (type sb!c::compiled-debug-info info))
1860
1861   ;; (This is stubified until we solve the problem of representing
1862   ;; debug information in a way which plays nicely with package renaming.)
1863   (error "FIXME: dead code UNCOMPACT-FUNCTION-MAP (was stub)")
1864
1865   (let* ((map (sb!c::compiled-debug-info-function-map info))
1866          (i 0)
1867          (len (length map))
1868          (code-start-pc 0)
1869          (elsewhere-pc 0))
1870     (declare (type (simple-array (unsigned-byte 8) (*)) map))
1871     (sb!int:collect ((res))
1872       (loop
1873         (when (= i len) (return))
1874         (let* ((options (prog1 (aref map i) (incf i)))
1875                (flags (prog1 (aref map i) (incf i)))
1876                (vars-p (logtest flags
1877                                 sb!c::minimal-debug-function-variables-bit))
1878                (dfun (make-uncompacted-debug-fun)))
1879           (res code-start-pc)
1880           (res dfun)))
1881
1882       (coerce (cdr (res)) 'simple-vector))))
1883
1884 ;;; a map from minimal DEBUG-INFO function maps to unpacked
1885 ;;; versions thereof
1886 (defvar *uncompacted-function-maps* (make-hash-table :test 'eq))
1887
1888 ;;; Return a FUNCTION-MAP for a given COMPILED-DEBUG-info object. If
1889 ;;; the info is minimal, and has not been parsed, then parse it.
1890 ;;;
1891 ;;; FIXME: Now that we no longer use the MINIMAL-DEBUG-FUNCTION
1892 ;;; representation, calls to this function can be replaced by calls to
1893 ;;; the bare COMPILED-DEBUG-INFO-FUNCTION-MAP slot accessor function,
1894 ;;; and this function and everything it calls become dead code which
1895 ;;; can be deleted.
1896 (defun get-debug-info-function-map (info)
1897   (declare (type sb!c::compiled-debug-info info))
1898   (let ((map (sb!c::compiled-debug-info-function-map info)))
1899     (if (simple-vector-p map)
1900         map
1901         (or (gethash map *uncompacted-function-maps*)
1902             (setf (gethash map *uncompacted-function-maps*)
1903                   (uncompact-function-map info))))))
1904 \f
1905 ;;;; CODE-LOCATIONs
1906
1907 ;;; If we're sure of whether code-location is known, return T or NIL.
1908 ;;; If we're :UNSURE, then try to fill in the code-location's slots.
1909 ;;; This determines whether there is any debug-block information, and
1910 ;;; if code-location is known.
1911 ;;;
1912 ;;; ??? IF this conses closures every time it's called, then break off the
1913 ;;; :UNSURE part to get the HANDLER-CASE into another function.
1914 (defun code-location-unknown-p (basic-code-location)
1915   (ecase (code-location-%unknown-p basic-code-location)
1916     ((t) t)
1917     ((nil) nil)
1918     (:unsure
1919      (setf (code-location-%unknown-p basic-code-location)
1920            (handler-case (not (fill-in-code-location basic-code-location))
1921              (no-debug-blocks () t))))))
1922
1923 ;;; Return the DEBUG-BLOCK containing code-location if it is available.
1924 ;;; Some debug policies inhibit debug-block information, and if none
1925 ;;; is available, then this signals a NO-DEBUG-BLOCKS condition.
1926 (defun code-location-debug-block (basic-code-location)
1927   (let ((block (code-location-%debug-block basic-code-location)))
1928     (if (eq block :unparsed)
1929         (etypecase basic-code-location
1930           (compiled-code-location
1931            (compute-compiled-code-location-debug-block basic-code-location))
1932           ;; (There used to be more cases back before sbcl-0.7.0, when
1933           ;; we did special tricks to debug the IR1 interpreter.)
1934           )
1935         block)))
1936
1937 ;;; Store and return BASIC-CODE-LOCATION's debug-block. We determines
1938 ;;; the correct one using the code-location's pc. We use
1939 ;;; DEBUG-FUNCTION-DEBUG-BLOCKS to return the cached block information
1940 ;;; or signal a NO-DEBUG-BLOCKS condition. The blocks are sorted by
1941 ;;; their first code-location's pc, in ascending order. Therefore, as
1942 ;;; soon as we find a block that starts with a pc greater than
1943 ;;; basic-code-location's pc, we know the previous block contains the
1944 ;;; pc. If we get to the last block, then the code-location is either
1945 ;;; in the second to last block or the last block, and we have to be
1946 ;;; careful in determining this since the last block could be code at
1947 ;;; the end of the function. We have to check for the last block being
1948 ;;; code first in order to see how to compare the code-location's pc.
1949 (defun compute-compiled-code-location-debug-block (basic-code-location)
1950   (let* ((pc (compiled-code-location-pc basic-code-location))
1951          (debug-function (code-location-debug-function
1952                           basic-code-location))
1953          (blocks (debug-function-debug-blocks debug-function))
1954          (len (length blocks)))
1955     (declare (simple-vector blocks))
1956     (setf (code-location-%debug-block basic-code-location)
1957           (if (= len 1)
1958               (svref blocks 0)
1959               (do ((i 1 (1+ i))
1960                    (end (1- len)))
1961                   ((= i end)
1962                    (let ((last (svref blocks end)))
1963                      (cond
1964                       ((debug-block-elsewhere-p last)
1965                        (if (< pc
1966                               (sb!c::compiled-debug-function-elsewhere-pc
1967                                (compiled-debug-function-compiler-debug-fun
1968                                 debug-function)))
1969                            (svref blocks (1- end))
1970                            last))
1971                       ((< pc
1972                           (compiled-code-location-pc
1973                            (svref (compiled-debug-block-code-locations last)
1974                                   0)))
1975                        (svref blocks (1- end)))
1976                       (t last))))
1977                 (declare (type sb!c::index i end))
1978                 (when (< pc
1979                          (compiled-code-location-pc
1980                           (svref (compiled-debug-block-code-locations
1981                                   (svref blocks i))
1982                                  0)))
1983                   (return (svref blocks (1- i)))))))))
1984
1985 ;;; Return the CODE-LOCATION's DEBUG-SOURCE.
1986 (defun code-location-debug-source (code-location)
1987   (etypecase code-location
1988     (compiled-code-location
1989      (let* ((info (compiled-debug-function-debug-info
1990                    (code-location-debug-function code-location)))
1991             (sources (sb!c::compiled-debug-info-source info))
1992             (len (length sources)))
1993        (declare (list sources))
1994        (when (zerop len)
1995          (debug-signal 'no-debug-blocks :debug-function
1996                        (code-location-debug-function code-location)))
1997        (if (= len 1)
1998            (car sources)
1999            (do ((prev sources src)
2000                 (src (cdr sources) (cdr src))
2001                 (offset (code-location-top-level-form-offset code-location)))
2002                ((null src) (car prev))
2003              (when (< offset (sb!c::debug-source-source-root (car src)))
2004                (return (car prev)))))))
2005     ;; (There used to be more cases back before sbcl-0.7.0, when we
2006     ;; did special tricks to debug the IR1 interpreter.)
2007     ))
2008
2009 ;;; Returns the number of top-level forms before the one containing
2010 ;;; CODE-LOCATION as seen by the compiler in some compilation unit. (A
2011 ;;; compilation unit is not necessarily a single file, see the section
2012 ;;; on debug-sources.)
2013 (defun code-location-top-level-form-offset (code-location)
2014   (when (code-location-unknown-p code-location)
2015     (error 'unknown-code-location :code-location code-location))
2016   (let ((tlf-offset (code-location-%tlf-offset code-location)))
2017     (cond ((eq tlf-offset :unparsed)
2018            (etypecase code-location
2019              (compiled-code-location
2020               (unless (fill-in-code-location code-location)
2021                 ;; This check should be unnecessary. We're missing
2022                 ;; debug info the compiler should have dumped.
2023                 (error "internal error: unknown code location"))
2024               (code-location-%tlf-offset code-location))
2025              ;; (There used to be more cases back before sbcl-0.7.0,,
2026              ;; when we did special tricks to debug the IR1
2027              ;; interpreter.)
2028              ))
2029           (t tlf-offset))))
2030
2031 ;;; Return the number of the form corresponding to CODE-LOCATION. The
2032 ;;; form number is derived by a walking the subforms of a top-level
2033 ;;; form in depth-first order.
2034 (defun code-location-form-number (code-location)
2035   (when (code-location-unknown-p code-location)
2036     (error 'unknown-code-location :code-location code-location))
2037   (let ((form-num (code-location-%form-number code-location)))
2038     (cond ((eq form-num :unparsed)
2039            (etypecase code-location
2040              (compiled-code-location
2041               (unless (fill-in-code-location code-location)
2042                 ;; This check should be unnecessary. We're missing
2043                 ;; debug info the compiler should have dumped.
2044                 (error "internal error: unknown code location"))
2045               (code-location-%form-number code-location))
2046              ;; (There used to be more cases back before sbcl-0.7.0,,
2047              ;; when we did special tricks to debug the IR1
2048              ;; interpreter.)
2049              ))
2050           (t form-num))))
2051
2052 ;;; Return the kind of CODE-LOCATION, one of:
2053 ;;;  :INTERPRETED, :UNKNOWN-RETURN, :KNOWN-RETURN, :INTERNAL-ERROR,
2054 ;;;  :NON-LOCAL-EXIT, :BLOCK-START, :CALL-SITE, :SINGLE-VALUE-RETURN,
2055 ;;;  :NON-LOCAL-ENTRY
2056 (defun code-location-kind (code-location)
2057   (when (code-location-unknown-p code-location)
2058     (error 'unknown-code-location :code-location code-location))
2059   (etypecase code-location
2060     (compiled-code-location
2061      (let ((kind (compiled-code-location-kind code-location)))
2062        (cond ((not (eq kind :unparsed)) kind)
2063              ((not (fill-in-code-location code-location))
2064               ;; This check should be unnecessary. We're missing
2065               ;; debug info the compiler should have dumped.
2066               (error "internal error: unknown code location"))
2067              (t
2068               (compiled-code-location-kind code-location)))))
2069     ;; (There used to be more cases back before sbcl-0.7.0,,
2070     ;; when we did special tricks to debug the IR1
2071     ;; interpreter.)
2072     ))
2073
2074 ;;; This returns CODE-LOCATION's live-set if it is available. If
2075 ;;; there is no debug-block information, this returns NIL.
2076 (defun compiled-code-location-live-set (code-location)
2077   (if (code-location-unknown-p code-location)
2078       nil
2079       (let ((live-set (compiled-code-location-%live-set code-location)))
2080         (cond ((eq live-set :unparsed)
2081                (unless (fill-in-code-location code-location)
2082                  ;; This check should be unnecessary. We're missing
2083                  ;; debug info the compiler should have dumped.
2084                  ;;
2085                  ;; FIXME: This error and comment happen over and over again.
2086                  ;; Make them a shared function.
2087                  (error "internal error: unknown code location"))
2088                (compiled-code-location-%live-set code-location))
2089               (t live-set)))))
2090
2091 ;;; true if OBJ1 and OBJ2 are the same place in the code
2092 (defun code-location= (obj1 obj2)
2093   (etypecase obj1
2094     (compiled-code-location
2095      (etypecase obj2
2096        (compiled-code-location
2097         (and (eq (code-location-debug-function obj1)
2098                  (code-location-debug-function obj2))
2099              (sub-compiled-code-location= obj1 obj2)))
2100        ;; (There used to be more cases back before sbcl-0.7.0,,
2101        ;; when we did special tricks to debug the IR1
2102        ;; interpreter.)
2103        ))
2104     ;; (There used to be more cases back before sbcl-0.7.0,,
2105     ;; when we did special tricks to debug the IR1
2106     ;; interpreter.)
2107     ))
2108 (defun sub-compiled-code-location= (obj1 obj2)
2109   (= (compiled-code-location-pc obj1)
2110      (compiled-code-location-pc obj2)))
2111
2112 ;;; Fill in CODE-LOCATION's :UNPARSED slots, returning T or NIL
2113 ;;; depending on whether the code-location was known in its
2114 ;;; debug-function's debug-block information. This may signal a
2115 ;;; NO-DEBUG-BLOCKS condition due to DEBUG-FUNCTION-DEBUG-BLOCKS, and
2116 ;;; it assumes the %UNKNOWN-P slot is already set or going to be set.
2117 (defun fill-in-code-location (code-location)
2118   (declare (type compiled-code-location code-location))
2119   (let* ((debug-function (code-location-debug-function code-location))
2120          (blocks (debug-function-debug-blocks debug-function)))
2121     (declare (simple-vector blocks))
2122     (dotimes (i (length blocks) nil)
2123       (let* ((block (svref blocks i))
2124              (locations (compiled-debug-block-code-locations block)))
2125         (declare (simple-vector locations))
2126         (dotimes (j (length locations))
2127           (let ((loc (svref locations j)))
2128             (when (sub-compiled-code-location= code-location loc)
2129               (setf (code-location-%debug-block code-location) block)
2130               (setf (code-location-%tlf-offset code-location)
2131                     (code-location-%tlf-offset loc))
2132               (setf (code-location-%form-number code-location)
2133                     (code-location-%form-number loc))
2134               (setf (compiled-code-location-%live-set code-location)
2135                     (compiled-code-location-%live-set loc))
2136               (setf (compiled-code-location-kind code-location)
2137                     (compiled-code-location-kind loc))
2138               (return-from fill-in-code-location t))))))))
2139 \f
2140 ;;;; operations on DEBUG-BLOCKs
2141
2142 ;;; Execute FORMS in a context with CODE-VAR bound to each
2143 ;;; CODE-LOCATION in DEBUG-BLOCK, and return the value of RESULT.
2144 (defmacro do-debug-block-locations ((code-var debug-block &optional result)
2145                                     &body body)
2146   (let ((code-locations (gensym))
2147         (i (gensym)))
2148     `(let ((,code-locations (debug-block-code-locations ,debug-block)))
2149        (declare (simple-vector ,code-locations))
2150        (dotimes (,i (length ,code-locations) ,result)
2151          (let ((,code-var (svref ,code-locations ,i)))
2152            ,@body)))))
2153
2154 ;;; Return the name of the function represented by DEBUG-FUNCTION.
2155 ;;; This may be a string or a cons; do not assume it is a symbol.
2156 (defun debug-block-function-name (debug-block)
2157   (etypecase debug-block
2158     (compiled-debug-block
2159      (let ((code-locs (compiled-debug-block-code-locations debug-block)))
2160        (declare (simple-vector code-locs))
2161        (if (zerop (length code-locs))
2162            "??? Can't get name of debug-block's function."
2163            (debug-function-name
2164             (code-location-debug-function (svref code-locs 0))))))
2165     ;; (There used to be more cases back before sbcl-0.7.0, when we
2166     ;; did special tricks to debug the IR1 interpreter.)
2167     ))
2168
2169 (defun debug-block-code-locations (debug-block)
2170   (etypecase debug-block
2171     (compiled-debug-block
2172      (compiled-debug-block-code-locations debug-block))
2173     ;; (There used to be more cases back before sbcl-0.7.0, when we
2174     ;; did special tricks to debug the IR1 interpreter.)
2175     ))
2176 \f
2177 ;;;; operations on debug variables
2178
2179 (defun debug-var-symbol-name (debug-var)
2180   (symbol-name (debug-var-symbol debug-var)))
2181
2182 ;;; FIXME: Make sure that this isn't called anywhere that it wouldn't
2183 ;;; be acceptable to have NIL returned, or that it's only called on
2184 ;;; DEBUG-VARs whose symbols have non-NIL packages.
2185 (defun debug-var-package-name (debug-var)
2186   (package-name (symbol-package (debug-var-symbol debug-var))))
2187
2188 ;;; Return the value stored for DEBUG-VAR in frame, or if the value is
2189 ;;; not :VALID, then signal an INVALID-VALUE error.
2190 (defun debug-var-valid-value (debug-var frame)
2191   (unless (eq (debug-var-validity debug-var (frame-code-location frame))
2192               :valid)
2193     (error 'invalid-value :debug-var debug-var :frame frame))
2194   (debug-var-value debug-var frame))
2195
2196 ;;; Returns the value stored for DEBUG-VAR in frame. The value may be
2197 ;;; invalid. This is SETFable.
2198 (defun debug-var-value (debug-var frame)
2199   (etypecase debug-var
2200     (compiled-debug-var
2201      (aver (typep frame 'compiled-frame))
2202      (let ((res (access-compiled-debug-var-slot debug-var frame)))
2203        (if (indirect-value-cell-p res)
2204            (value-cell-ref res)
2205            res)))
2206     ;; (This function used to be more interesting, with more type
2207     ;; cases here, before the IR1 interpreter went away. It might
2208     ;; become more interesting again if we ever try to generalize the
2209     ;; CMU CL POSSIBLY-AN-INTERPRETED-FRAME thing to elide
2210     ;; internal-to-the-byte-interpreter debug frames the way that CMU
2211     ;; CL elided internal-to-the-IR1-interpreter debug frames.)
2212     ))
2213
2214 ;;; This returns what is stored for the variable represented by
2215 ;;; DEBUG-VAR relative to the FRAME. This may be an indirect value
2216 ;;; cell if the variable is both closed over and set.
2217 (defun access-compiled-debug-var-slot (debug-var frame)
2218   (declare (optimize (speed 1)))
2219   (let ((escaped (compiled-frame-escaped frame)))
2220     (if escaped
2221         (sub-access-debug-var-slot
2222          (frame-pointer frame)
2223          (compiled-debug-var-sc-offset debug-var)
2224          escaped)
2225       (sub-access-debug-var-slot
2226        (frame-pointer frame)
2227        (or (compiled-debug-var-save-sc-offset debug-var)
2228            (compiled-debug-var-sc-offset debug-var))))))
2229
2230 ;;; a helper function for working with possibly-invalid values:
2231 ;;; Do (MAKE-LISP-OBJ VAL) only if the value looks valid.
2232 ;;;
2233 ;;; (Such values can arise in registers on machines with conservative
2234 ;;; GC, and might also arise in debug variable locations when
2235 ;;; those variables are invalid.)
2236 (defun make-valid-lisp-obj (val)
2237   (/show0 "entering MAKE-VALID-LISP-OBJ, VAL=..")
2238   #!+sb-show (/hexstr val)
2239   (if (or
2240        ;; fixnum
2241        (zerop (logand val 3))
2242        ;; character
2243        (and (zerop (logand val #xffff0000)) ; Top bits zero
2244             (= (logand val #xff) sb!vm:base-char-type)) ; Char tag
2245        ;; unbound marker
2246        (= val sb!vm:unbound-marker-type)
2247        ;; pointer
2248        (and (logand val 1)
2249             ;; Check that the pointer is valid. XXX Could do a better
2250             ;; job. FIXME: e.g. by calling out to an is_valid_pointer
2251             ;; routine in the C runtime support code
2252             (or (< sb!vm:read-only-space-start val
2253                    (* sb!vm:*read-only-space-free-pointer*
2254                       sb!vm:word-bytes))
2255                 (< sb!vm:static-space-start val
2256                    (* sb!vm:*static-space-free-pointer*
2257                       sb!vm:word-bytes))
2258                 (< sb!vm:dynamic-space-start val
2259                    (sap-int (dynamic-space-free-pointer))))))
2260       (make-lisp-obj val)
2261       :invalid-object))
2262
2263 #!-x86
2264 (defun sub-access-debug-var-slot (fp sc-offset &optional escaped)
2265   (macrolet ((with-escaped-value ((var) &body forms)
2266                `(if escaped
2267                     (let ((,var (sb!vm:context-register
2268                                  escaped
2269                                  (sb!c:sc-offset-offset sc-offset))))
2270                       ,@forms)
2271                     :invalid-value-for-unescaped-register-storage))
2272              (escaped-float-value (format)
2273                `(if escaped
2274                     (sb!vm:context-float-register
2275                      escaped
2276                      (sb!c:sc-offset-offset sc-offset)
2277                      ',format)
2278                     :invalid-value-for-unescaped-register-storage))
2279              (with-nfp ((var) &body body)
2280                `(let ((,var (if escaped
2281                                 (sb!sys:int-sap
2282                                  (sb!vm:context-register escaped
2283                                                          sb!vm::nfp-offset))
2284                                 #!-alpha
2285                                 (sb!sys:sap-ref-sap fp (* sb!vm::nfp-save-offset
2286                                                           sb!vm:word-bytes))
2287                                 #!+alpha
2288                                 (sb!vm::make-number-stack-pointer
2289                                  (sb!sys:sap-ref-32 fp (* sb!vm::nfp-save-offset
2290                                                           sb!vm:word-bytes))))))
2291                   ,@body)))
2292     (ecase (sb!c:sc-offset-scn sc-offset)
2293       ((#.sb!vm:any-reg-sc-number
2294         #.sb!vm:descriptor-reg-sc-number
2295         #!+rt #.sb!vm:word-pointer-reg-sc-number)
2296        (sb!sys:without-gcing
2297         (with-escaped-value (val) (sb!kernel:make-lisp-obj val))))
2298                             
2299       (#.sb!vm:base-char-reg-sc-number
2300        (with-escaped-value (val)
2301          (code-char val)))
2302       (#.sb!vm:sap-reg-sc-number
2303        (with-escaped-value (val)
2304          (sb!sys:int-sap val)))
2305       (#.sb!vm:signed-reg-sc-number
2306        (with-escaped-value (val)
2307          (if (logbitp (1- sb!vm:word-bits) val)
2308              (logior val (ash -1 sb!vm:word-bits))
2309              val)))
2310       (#.sb!vm:unsigned-reg-sc-number
2311        (with-escaped-value (val)
2312          val))
2313       (#.sb!vm:non-descriptor-reg-sc-number
2314        (error "Local non-descriptor register access?"))
2315       (#.sb!vm:interior-reg-sc-number
2316        (error "Local interior register access?"))
2317       (#.sb!vm:single-reg-sc-number
2318        (escaped-float-value single-float))
2319       (#.sb!vm:double-reg-sc-number
2320        (escaped-float-value double-float))
2321       #!+long-float
2322       (#.sb!vm:long-reg-sc-number
2323        (escaped-float-value long-float))
2324       (#.sb!vm:complex-single-reg-sc-number
2325        (if escaped
2326            (complex
2327             (sb!vm:context-float-register
2328              escaped (sb!c:sc-offset-offset sc-offset) 'single-float)
2329             (sb!vm:context-float-register
2330              escaped (1+ (sb!c:sc-offset-offset sc-offset)) 'single-float))
2331            :invalid-value-for-unescaped-register-storage))
2332       (#.sb!vm:complex-double-reg-sc-number
2333        (if escaped
2334            (complex
2335             (sb!vm:context-float-register
2336              escaped (sb!c:sc-offset-offset sc-offset) 'double-float)
2337             (sb!vm:context-float-register
2338              escaped (+ (sb!c:sc-offset-offset sc-offset) #!+sparc 2 #-sparc 1)
2339              'double-float))
2340            :invalid-value-for-unescaped-register-storage))
2341       #!+long-float
2342       (#.sb!vm:complex-long-reg-sc-number
2343        (if escaped
2344            (complex
2345             (sb!vm:context-float-register
2346              escaped (sb!c:sc-offset-offset sc-offset) 'long-float)
2347             (sb!vm:context-float-register
2348              escaped (+ (sb!c:sc-offset-offset sc-offset) #!+sparc 4)
2349              'long-float))
2350            :invalid-value-for-unescaped-register-storage))
2351       (#.sb!vm:single-stack-sc-number
2352        (with-nfp (nfp)
2353          (sb!sys:sap-ref-single nfp (* (sb!c:sc-offset-offset sc-offset)
2354                                        sb!vm:word-bytes))))
2355       (#.sb!vm:double-stack-sc-number
2356        (with-nfp (nfp)
2357          (sb!sys:sap-ref-double nfp (* (sb!c:sc-offset-offset sc-offset)
2358                                        sb!vm:word-bytes))))
2359       #!+long-float
2360       (#.sb!vm:long-stack-sc-number
2361        (with-nfp (nfp)
2362          (sb!sys:sap-ref-long nfp (* (sb!c:sc-offset-offset sc-offset)
2363                                      sb!vm:word-bytes))))
2364       (#.sb!vm:complex-single-stack-sc-number
2365        (with-nfp (nfp)
2366          (complex
2367           (sb!sys:sap-ref-single nfp (* (sb!c:sc-offset-offset sc-offset)
2368                                         sb!vm:word-bytes))
2369           (sb!sys:sap-ref-single nfp (* (1+ (sb!c:sc-offset-offset sc-offset))
2370                                         sb!vm:word-bytes)))))
2371       (#.sb!vm:complex-double-stack-sc-number
2372        (with-nfp (nfp)
2373          (complex
2374           (sb!sys:sap-ref-double nfp (* (sb!c:sc-offset-offset sc-offset)
2375                                         sb!vm:word-bytes))
2376           (sb!sys:sap-ref-double nfp (* (+ (sb!c:sc-offset-offset sc-offset) 2)
2377                                         sb!vm:word-bytes)))))
2378       #!+long-float
2379       (#.sb!vm:complex-long-stack-sc-number
2380        (with-nfp (nfp)
2381          (complex
2382           (sb!sys:sap-ref-long nfp (* (sb!c:sc-offset-offset sc-offset)
2383                                       sb!vm:word-bytes))
2384           (sb!sys:sap-ref-long nfp (* (+ (sb!c:sc-offset-offset sc-offset)
2385                                          #!+sparc 4)
2386                                       sb!vm:word-bytes)))))
2387       (#.sb!vm:control-stack-sc-number
2388        (sb!kernel:stack-ref fp (sb!c:sc-offset-offset sc-offset)))
2389       (#.sb!vm:base-char-stack-sc-number
2390        (with-nfp (nfp)
2391          (code-char (sb!sys:sap-ref-32 nfp (* (sb!c:sc-offset-offset sc-offset)
2392                                               sb!vm:word-bytes)))))
2393       (#.sb!vm:unsigned-stack-sc-number
2394        (with-nfp (nfp)
2395          (sb!sys:sap-ref-32 nfp (* (sb!c:sc-offset-offset sc-offset)
2396                                    sb!vm:word-bytes))))
2397       (#.sb!vm:signed-stack-sc-number
2398        (with-nfp (nfp)
2399          (sb!sys:signed-sap-ref-32 nfp (* (sb!c:sc-offset-offset sc-offset)
2400                                           sb!vm:word-bytes))))
2401       (#.sb!vm:sap-stack-sc-number
2402        (with-nfp (nfp)
2403          (sb!sys:sap-ref-sap nfp (* (sb!c:sc-offset-offset sc-offset)
2404                                     sb!vm:word-bytes)))))))
2405
2406 #!+x86
2407 (defun sub-access-debug-var-slot (fp sc-offset &optional escaped)
2408   (declare (type system-area-pointer fp))
2409   (/show0 "entering SUB-ACCESS-DEBUG-VAR-SLOT, FP,SC-OFFSET,ESCAPED=..")
2410   (/hexstr fp) (/hexstr sc-offset) (/hexstr escaped)
2411   (macrolet ((with-escaped-value ((var) &body forms)
2412                `(if escaped
2413                     (let ((,var (sb!vm:context-register
2414                                  escaped
2415                                  (sb!c:sc-offset-offset sc-offset))))
2416                       (/show0 "in escaped case, ,VAR value=..")
2417                       (/hexstr ,var)
2418                       ,@forms)
2419                     :invalid-value-for-unescaped-register-storage))
2420              (escaped-float-value (format)
2421                `(if escaped
2422                     (sb!vm:context-float-register
2423                      escaped (sb!c:sc-offset-offset sc-offset) ',format)
2424                     :invalid-value-for-unescaped-register-storage))
2425              (escaped-complex-float-value (format)
2426                `(if escaped
2427                     (complex
2428                      (sb!vm:context-float-register
2429                       escaped (sb!c:sc-offset-offset sc-offset) ',format)
2430                      (sb!vm:context-float-register
2431                       escaped (1+ (sb!c:sc-offset-offset sc-offset)) ',format))
2432                     :invalid-value-for-unescaped-register-storage)))
2433     (ecase (sb!c:sc-offset-scn sc-offset)
2434       ((#.sb!vm:any-reg-sc-number #.sb!vm:descriptor-reg-sc-number)
2435        (/show0 "case of ANY-REG-SC-NUMBER or DESCRIPTOR-REG-SC-NUMBER")
2436        (without-gcing
2437         (with-escaped-value (val)
2438           (/show0 "VAL=..")
2439           (/hexstr val)
2440           (make-valid-lisp-obj val))))
2441       (#.sb!vm:base-char-reg-sc-number
2442        (/show0 "case of BASE-CHAR-REG-SC-NUMBER")
2443        (with-escaped-value (val)
2444          (code-char val)))
2445       (#.sb!vm:sap-reg-sc-number
2446        (/show0 "case of SAP-REG-SC-NUMBER")
2447        (with-escaped-value (val)
2448          (int-sap val)))
2449       (#.sb!vm:signed-reg-sc-number
2450        (/show0 "case of SIGNED-REG-SC-NUMBER")
2451        (with-escaped-value (val)
2452          (if (logbitp (1- sb!vm:word-bits) val)
2453              (logior val (ash -1 sb!vm:word-bits))
2454              val)))
2455       (#.sb!vm:unsigned-reg-sc-number
2456        (/show0 "case of UNSIGNED-REG-SC-NUMBER")
2457        (with-escaped-value (val)
2458          val))
2459       (#.sb!vm:single-reg-sc-number
2460        (/show0 "case of SINGLE-REG-SC-NUMBER")
2461        (escaped-float-value single-float))
2462       (#.sb!vm:double-reg-sc-number
2463        (/show0 "case of DOUBLE-REG-SC-NUMBER")
2464        (escaped-float-value double-float))
2465       #!+long-float
2466       (#.sb!vm:long-reg-sc-number
2467        (/show0 "case of LONG-REG-SC-NUMBER")
2468        (escaped-float-value long-float))
2469       (#.sb!vm:complex-single-reg-sc-number
2470        (/show0 "case of COMPLEX-SINGLE-REG-SC-NUMBER")
2471        (escaped-complex-float-value single-float))
2472       (#.sb!vm:complex-double-reg-sc-number
2473        (/show0 "case of COMPLEX-DOUBLE-REG-SC-NUMBER")
2474        (escaped-complex-float-value double-float))
2475       #!+long-float
2476       (#.sb!vm:complex-long-reg-sc-number
2477        (/show0 "case of COMPLEX-LONG-REG-SC-NUMBER")
2478        (escaped-complex-float-value long-float))
2479       (#.sb!vm:single-stack-sc-number
2480        (/show0 "case of SINGLE-STACK-SC-NUMBER")
2481        (sap-ref-single fp (- (* (1+ (sb!c:sc-offset-offset sc-offset))
2482                                 sb!vm:word-bytes))))
2483       (#.sb!vm:double-stack-sc-number
2484        (/show0 "case of DOUBLE-STACK-SC-NUMBER")
2485        (sap-ref-double fp (- (* (+ (sb!c:sc-offset-offset sc-offset) 2)
2486                                 sb!vm:word-bytes))))
2487       #!+long-float
2488       (#.sb!vm:long-stack-sc-number
2489        (/show0 "case of LONG-STACK-SC-NUMBER")
2490        (sap-ref-long fp (- (* (+ (sb!c:sc-offset-offset sc-offset) 3)
2491                               sb!vm:word-bytes))))
2492       (#.sb!vm:complex-single-stack-sc-number
2493        (/show0 "case of COMPLEX-STACK-SC-NUMBER")
2494        (complex
2495         (sap-ref-single fp (- (* (1+ (sb!c:sc-offset-offset sc-offset))
2496                                  sb!vm:word-bytes)))
2497         (sap-ref-single fp (- (* (+ (sb!c:sc-offset-offset sc-offset) 2)
2498                                  sb!vm:word-bytes)))))
2499       (#.sb!vm:complex-double-stack-sc-number
2500        (/show0 "case of COMPLEX-DOUBLE-STACK-SC-NUMBER")
2501        (complex
2502         (sap-ref-double fp (- (* (+ (sb!c:sc-offset-offset sc-offset) 2)
2503                                  sb!vm:word-bytes)))
2504         (sap-ref-double fp (- (* (+ (sb!c:sc-offset-offset sc-offset) 4)
2505                                  sb!vm:word-bytes)))))
2506       #!+long-float
2507       (#.sb!vm:complex-long-stack-sc-number
2508        (/show0 "case of COMPLEX-LONG-STACK-SC-NUMBER")
2509        (complex
2510         (sap-ref-long fp (- (* (+ (sb!c:sc-offset-offset sc-offset) 3)
2511                                sb!vm:word-bytes)))
2512         (sap-ref-long fp (- (* (+ (sb!c:sc-offset-offset sc-offset) 6)
2513                                sb!vm:word-bytes)))))
2514       (#.sb!vm:control-stack-sc-number
2515        (/show0 "case of CONTROL-STACK-SC-NUMBER")
2516        (stack-ref fp (sb!c:sc-offset-offset sc-offset)))
2517       (#.sb!vm:base-char-stack-sc-number
2518        (/show0 "case of BASE-CHAR-STACK-SC-NUMBER")
2519        (code-char
2520         (sap-ref-32 fp (- (* (1+ (sb!c:sc-offset-offset sc-offset))
2521                              sb!vm:word-bytes)))))
2522       (#.sb!vm:unsigned-stack-sc-number
2523        (/show0 "case of UNSIGNED-STACK-SC-NUMBER")
2524        (sap-ref-32 fp (- (* (1+ (sb!c:sc-offset-offset sc-offset))
2525                             sb!vm:word-bytes))))
2526       (#.sb!vm:signed-stack-sc-number
2527        (/show0 "case of SIGNED-STACK-SC-NUMBER")
2528        (signed-sap-ref-32 fp (- (* (1+ (sb!c:sc-offset-offset sc-offset))
2529                                    sb!vm:word-bytes))))
2530       (#.sb!vm:sap-stack-sc-number
2531        (/show0 "case of SAP-STACK-SC-NUMBER")
2532        (sap-ref-sap fp (- (* (1+ (sb!c:sc-offset-offset sc-offset))
2533                              sb!vm:word-bytes)))))))
2534
2535 ;;; This stores value as the value of DEBUG-VAR in FRAME. In the
2536 ;;; COMPILED-DEBUG-VAR case, access the current value to determine if
2537 ;;; it is an indirect value cell. This occurs when the variable is
2538 ;;; both closed over and set.
2539 (defun %set-debug-var-value (debug-var frame value)
2540   (etypecase debug-var
2541     (compiled-debug-var
2542      (aver (typep frame 'compiled-frame))
2543      (let ((current-value (access-compiled-debug-var-slot debug-var frame)))
2544        (if (indirect-value-cell-p current-value)
2545            (value-cell-set current-value value)
2546            (set-compiled-debug-var-slot debug-var frame value))))
2547     ;; (This function used to be more interesting, with more type
2548     ;; cases here, before the IR1 interpreter went away. It might
2549     ;; become more interesting again if we ever try to generalize the
2550     ;; CMU CL POSSIBLY-AN-INTERPRETED-FRAME thing to elide
2551     ;; internal-to-the-byte-interpreter debug frames the way that CMU
2552     ;; CL elided internal-to-the-IR1-interpreter debug frames.)
2553     )
2554   value)
2555
2556 ;;; This stores value for the variable represented by debug-var
2557 ;;; relative to the frame. This assumes the location directly contains
2558 ;;; the variable's value; that is, there is no indirect value cell
2559 ;;; currently there in case the variable is both closed over and set.
2560 (defun set-compiled-debug-var-slot (debug-var frame value)
2561   (let ((escaped (compiled-frame-escaped frame)))
2562     (if escaped
2563         (sub-set-debug-var-slot (frame-pointer frame)
2564                                 (compiled-debug-var-sc-offset debug-var)
2565                                 value escaped)
2566         (sub-set-debug-var-slot
2567          (frame-pointer frame)
2568          (or (compiled-debug-var-save-sc-offset debug-var)
2569              (compiled-debug-var-sc-offset debug-var))
2570          value))))
2571
2572 #!-x86
2573 (defun sub-set-debug-var-slot (fp sc-offset value &optional escaped)
2574   (macrolet ((set-escaped-value (val)
2575                `(if escaped
2576                     (setf (sb!vm:context-register
2577                            escaped
2578                            (sb!c:sc-offset-offset sc-offset))
2579                           ,val)
2580                     value))
2581              (set-escaped-float-value (format val)
2582                `(if escaped
2583                     (setf (sb!vm:context-float-register
2584                            escaped
2585                            (sb!c:sc-offset-offset sc-offset)
2586                            ',format)
2587                           ,val)
2588                     value))
2589              (with-nfp ((var) &body body)
2590                `(let ((,var (if escaped
2591                                 (int-sap
2592                                  (sb!vm:context-register escaped
2593                                                          sb!vm::nfp-offset))
2594                                 #!-alpha
2595                                 (sap-ref-sap fp
2596                                              (* sb!vm::nfp-save-offset
2597                                                 sb!vm:word-bytes))
2598                                 #!+alpha
2599                                 (sb!vm::make-number-stack-pointer
2600                                  (sap-ref-32 fp
2601                                              (* sb!vm::nfp-save-offset
2602                                                 sb!vm:word-bytes))))))
2603                   ,@body)))
2604     (ecase (sb!c:sc-offset-scn sc-offset)
2605       ((#.sb!vm:any-reg-sc-number
2606         #.sb!vm:descriptor-reg-sc-number
2607         #!+rt #.sb!vm:word-pointer-reg-sc-number)
2608        (without-gcing
2609         (set-escaped-value
2610           (get-lisp-obj-address value))))
2611       (#.sb!vm:base-char-reg-sc-number
2612        (set-escaped-value (char-code value)))
2613       (#.sb!vm:sap-reg-sc-number
2614        (set-escaped-value (sap-int value)))
2615       (#.sb!vm:signed-reg-sc-number
2616        (set-escaped-value (logand value (1- (ash 1 sb!vm:word-bits)))))
2617       (#.sb!vm:unsigned-reg-sc-number
2618        (set-escaped-value value))
2619       (#.sb!vm:non-descriptor-reg-sc-number
2620        (error "Local non-descriptor register access?"))
2621       (#.sb!vm:interior-reg-sc-number
2622        (error "Local interior register access?"))
2623       (#.sb!vm:single-reg-sc-number
2624        (set-escaped-float-value single-float value))
2625       (#.sb!vm:double-reg-sc-number
2626        (set-escaped-float-value double-float value))
2627       #!+long-float
2628       (#.sb!vm:long-reg-sc-number
2629        (set-escaped-float-value long-float value))
2630       (#.sb!vm:complex-single-reg-sc-number
2631        (when escaped
2632          (setf (sb!vm:context-float-register escaped
2633                                              (sb!c:sc-offset-offset sc-offset)
2634                                              'single-float)
2635                (realpart value))
2636          (setf (sb!vm:context-float-register
2637                 escaped (1+ (sb!c:sc-offset-offset sc-offset))
2638                 'single-float)
2639                (imagpart value)))
2640        value)
2641       (#.sb!vm:complex-double-reg-sc-number
2642        (when escaped
2643          (setf (sb!vm:context-float-register
2644                 escaped (sb!c:sc-offset-offset sc-offset) 'double-float)
2645                (realpart value))
2646          (setf (sb!vm:context-float-register
2647                 escaped
2648                 (+ (sb!c:sc-offset-offset sc-offset) #!+sparc 2 #!-sparc 1)
2649                 'double-float)
2650                (imagpart value)))
2651        value)
2652       #!+long-float
2653       (#.sb!vm:complex-long-reg-sc-number
2654        (when escaped
2655          (setf (sb!vm:context-float-register
2656                 escaped (sb!c:sc-offset-offset sc-offset) 'long-float)
2657                (realpart value))
2658          (setf (sb!vm:context-float-register
2659                 escaped
2660                 (+ (sb!c:sc-offset-offset sc-offset) #!+sparc 4)
2661                 'long-float)
2662                (imagpart value)))
2663        value)
2664       (#.sb!vm:single-stack-sc-number
2665        (with-nfp (nfp)
2666          (setf (sap-ref-single nfp (* (sb!c:sc-offset-offset sc-offset)
2667                                       sb!vm:word-bytes))
2668                (the single-float value))))
2669       (#.sb!vm:double-stack-sc-number
2670        (with-nfp (nfp)
2671          (setf (sap-ref-double nfp (* (sb!c:sc-offset-offset sc-offset)
2672                                       sb!vm:word-bytes))
2673                (the double-float value))))
2674       #!+long-float
2675       (#.sb!vm:long-stack-sc-number
2676        (with-nfp (nfp)
2677          (setf (sap-ref-long nfp (* (sb!c:sc-offset-offset sc-offset)
2678                                     sb!vm:word-bytes))
2679                (the long-float value))))
2680       (#.sb!vm:complex-single-stack-sc-number
2681        (with-nfp (nfp)
2682          (setf (sap-ref-single
2683                 nfp (* (sb!c:sc-offset-offset sc-offset) sb!vm:word-bytes))
2684                (the single-float (realpart value)))
2685          (setf (sap-ref-single
2686                 nfp (* (1+ (sb!c:sc-offset-offset sc-offset))
2687                        sb!vm:word-bytes))
2688                (the single-float (realpart value)))))
2689       (#.sb!vm:complex-double-stack-sc-number
2690        (with-nfp (nfp)
2691          (setf (sap-ref-double
2692                 nfp (* (sb!c:sc-offset-offset sc-offset) sb!vm:word-bytes))
2693                (the double-float (realpart value)))
2694          (setf (sap-ref-double
2695                 nfp (* (+ (sb!c:sc-offset-offset sc-offset) 2)
2696                        sb!vm:word-bytes))
2697                (the double-float (realpart value)))))
2698       #!+long-float
2699       (#.sb!vm:complex-long-stack-sc-number
2700        (with-nfp (nfp)
2701          (setf (sap-ref-long
2702                 nfp (* (sb!c:sc-offset-offset sc-offset) sb!vm:word-bytes))
2703                (the long-float (realpart value)))
2704          (setf (sap-ref-long
2705                 nfp (* (+ (sb!c:sc-offset-offset sc-offset) #!+sparc 4)
2706                        sb!vm:word-bytes))
2707                (the long-float (realpart value)))))
2708       (#.sb!vm:control-stack-sc-number
2709        (setf (stack-ref fp (sb!c:sc-offset-offset sc-offset)) value))
2710       (#.sb!vm:base-char-stack-sc-number
2711        (with-nfp (nfp)
2712          (setf (sap-ref-32 nfp (* (sb!c:sc-offset-offset sc-offset)
2713                                          sb!vm:word-bytes))
2714                (char-code (the character value)))))
2715       (#.sb!vm:unsigned-stack-sc-number
2716        (with-nfp (nfp)
2717          (setf (sap-ref-32 nfp (* (sb!c:sc-offset-offset sc-offset)
2718                                   sb!vm:word-bytes))
2719                (the (unsigned-byte 32) value))))
2720       (#.sb!vm:signed-stack-sc-number
2721        (with-nfp (nfp)
2722          (setf (signed-sap-ref-32 nfp (* (sb!c:sc-offset-offset sc-offset)
2723                                          sb!vm:word-bytes))
2724                (the (signed-byte 32) value))))
2725       (#.sb!vm:sap-stack-sc-number
2726        (with-nfp (nfp)
2727          (setf (sap-ref-sap nfp (* (sb!c:sc-offset-offset sc-offset)
2728                                    sb!vm:word-bytes))
2729                (the system-area-pointer value)))))))
2730
2731 #!+x86
2732 (defun sub-set-debug-var-slot (fp sc-offset value &optional escaped)
2733   (macrolet ((set-escaped-value (val)
2734                `(if escaped
2735                     (setf (sb!vm:context-register
2736                            escaped
2737                            (sb!c:sc-offset-offset sc-offset))
2738                           ,val)
2739                     value)))
2740     (ecase (sb!c:sc-offset-scn sc-offset)
2741       ((#.sb!vm:any-reg-sc-number #.sb!vm:descriptor-reg-sc-number)
2742        (without-gcing
2743         (set-escaped-value
2744           (get-lisp-obj-address value))))
2745       (#.sb!vm:base-char-reg-sc-number
2746        (set-escaped-value (char-code value)))
2747       (#.sb!vm:sap-reg-sc-number
2748        (set-escaped-value (sap-int value)))
2749       (#.sb!vm:signed-reg-sc-number
2750        (set-escaped-value (logand value (1- (ash 1 sb!vm:word-bits)))))
2751       (#.sb!vm:unsigned-reg-sc-number
2752        (set-escaped-value value))
2753       (#.sb!vm:single-reg-sc-number
2754         #+nil ;; don't have escaped floats.
2755        (set-escaped-float-value single-float value))
2756       (#.sb!vm:double-reg-sc-number
2757         #+nil ;;  don't have escaped floats -- still in npx?
2758        (set-escaped-float-value double-float value))
2759       #!+long-float
2760       (#.sb!vm:long-reg-sc-number
2761         #+nil ;;  don't have escaped floats -- still in npx?
2762        (set-escaped-float-value long-float value))
2763       (#.sb!vm:single-stack-sc-number
2764        (setf (sap-ref-single
2765               fp (- (* (1+ (sb!c:sc-offset-offset sc-offset))
2766                        sb!vm:word-bytes)))
2767              (the single-float value)))
2768       (#.sb!vm:double-stack-sc-number
2769        (setf (sap-ref-double
2770               fp (- (* (+ (sb!c:sc-offset-offset sc-offset) 2)
2771                        sb!vm:word-bytes)))
2772              (the double-float value)))
2773       #!+long-float
2774       (#.sb!vm:long-stack-sc-number
2775        (setf (sap-ref-long
2776               fp (- (* (+ (sb!c:sc-offset-offset sc-offset) 3)
2777                        sb!vm:word-bytes)))
2778              (the long-float value)))
2779       (#.sb!vm:complex-single-stack-sc-number
2780        (setf (sap-ref-single
2781               fp (- (* (1+ (sb!c:sc-offset-offset sc-offset))
2782                        sb!vm:word-bytes)))
2783              (realpart (the (complex single-float) value)))
2784        (setf (sap-ref-single
2785               fp (- (* (+ (sb!c:sc-offset-offset sc-offset) 2)
2786                        sb!vm:word-bytes)))
2787              (imagpart (the (complex single-float) value))))
2788       (#.sb!vm:complex-double-stack-sc-number
2789        (setf (sap-ref-double
2790               fp (- (* (+ (sb!c:sc-offset-offset sc-offset) 2)
2791                        sb!vm:word-bytes)))
2792              (realpart (the (complex double-float) value)))
2793        (setf (sap-ref-double
2794               fp (- (* (+ (sb!c:sc-offset-offset sc-offset) 4)
2795                        sb!vm:word-bytes)))
2796              (imagpart (the (complex double-float) value))))
2797       #!+long-float
2798       (#.sb!vm:complex-long-stack-sc-number
2799        (setf (sap-ref-long
2800               fp (- (* (+ (sb!c:sc-offset-offset sc-offset) 3)
2801                        sb!vm:word-bytes)))
2802              (realpart (the (complex long-float) value)))
2803        (setf (sap-ref-long
2804               fp (- (* (+ (sb!c:sc-offset-offset sc-offset) 6)
2805                        sb!vm:word-bytes)))
2806              (imagpart (the (complex long-float) value))))
2807       (#.sb!vm:control-stack-sc-number
2808        (setf (stack-ref fp (sb!c:sc-offset-offset sc-offset)) value))
2809       (#.sb!vm:base-char-stack-sc-number
2810        (setf (sap-ref-32 fp (- (* (1+ (sb!c:sc-offset-offset sc-offset))
2811                                          sb!vm:word-bytes)))
2812              (char-code (the character value))))
2813       (#.sb!vm:unsigned-stack-sc-number
2814        (setf (sap-ref-32 fp (- (* (1+ (sb!c:sc-offset-offset sc-offset))
2815                                          sb!vm:word-bytes)))
2816              (the (unsigned-byte 32) value)))
2817       (#.sb!vm:signed-stack-sc-number
2818        (setf (signed-sap-ref-32
2819               fp (- (* (1+ (sb!c:sc-offset-offset sc-offset)) sb!vm:word-bytes)))
2820              (the (signed-byte 32) value)))
2821       (#.sb!vm:sap-stack-sc-number
2822        (setf (sap-ref-sap fp (- (* (1+ (sb!c:sc-offset-offset sc-offset))
2823                                           sb!vm:word-bytes)))
2824              (the system-area-pointer value))))))
2825
2826 ;;; The method for setting and accessing COMPILED-DEBUG-VAR values use
2827 ;;; this to determine if the value stored is the actual value or an
2828 ;;; indirection cell.
2829 (defun indirect-value-cell-p (x)
2830   (and (= (get-lowtag x) sb!vm:other-pointer-type)
2831        (= (get-type x) sb!vm:value-cell-header-type)))
2832
2833 ;;; Return three values reflecting the validity of DEBUG-VAR's value
2834 ;;; at BASIC-CODE-LOCATION:
2835 ;;;   :VALID    The value is known to be available.
2836 ;;;   :INVALID  The value is known to be unavailable.
2837 ;;;   :UNKNOWN  The value's availability is unknown.
2838 ;;;
2839 ;;; If the variable is always alive, then it is valid. If the
2840 ;;; code-location is unknown, then the variable's validity is
2841 ;;; :unknown. Once we've called CODE-LOCATION-UNKNOWN-P, we know the
2842 ;;; live-set information has been cached in the code-location.
2843 (defun debug-var-validity (debug-var basic-code-location)
2844   (etypecase debug-var
2845     (compiled-debug-var
2846      (compiled-debug-var-validity debug-var basic-code-location))
2847     ;; (There used to be more cases back before sbcl-0.7.0, when
2848     ;; we did special tricks to debug the IR1 interpreter.)
2849     ))
2850
2851 ;;; This is the method for DEBUG-VAR-VALIDITY for COMPILED-DEBUG-VARs.
2852 ;;; For safety, make sure basic-code-location is what we think.
2853 (defun compiled-debug-var-validity (debug-var basic-code-location)
2854   (declare (type compiled-code-location basic-code-location))
2855   (cond ((debug-var-alive-p debug-var)
2856          (let ((debug-fun (code-location-debug-function basic-code-location)))
2857            (if (>= (compiled-code-location-pc basic-code-location)
2858                    (sb!c::compiled-debug-function-start-pc
2859                     (compiled-debug-function-compiler-debug-fun debug-fun)))
2860                :valid
2861                :invalid)))
2862         ((code-location-unknown-p basic-code-location) :unknown)
2863         (t
2864          (let ((pos (position debug-var
2865                               (debug-function-debug-vars
2866                                (code-location-debug-function
2867                                 basic-code-location)))))
2868            (unless pos
2869              (error 'unknown-debug-var
2870                     :debug-var debug-var
2871                     :debug-function
2872                     (code-location-debug-function basic-code-location)))
2873            ;; There must be live-set info since basic-code-location is known.
2874            (if (zerop (sbit (compiled-code-location-live-set
2875                              basic-code-location)
2876                             pos))
2877                :invalid
2878                :valid)))))
2879 \f
2880 ;;;; sources
2881
2882 ;;; This code produces and uses what we call source-paths. A
2883 ;;; source-path is a list whose first element is a form number as
2884 ;;; returned by CODE-LOCATION-FORM-NUMBER and whose last element is a
2885 ;;; top-level-form number as returned by
2886 ;;; CODE-LOCATION-TOP-LEVEL-FORM-NUMBER. The elements from the last to
2887 ;;; the first, exclusively, are the numbered subforms into which to
2888 ;;; descend. For example:
2889 ;;;    (defun foo (x)
2890 ;;;      (let ((a (aref x 3)))
2891 ;;;     (cons a 3)))
2892 ;;; The call to AREF in this example is form number 5. Assuming this
2893 ;;; DEFUN is the 11'th top-level-form, the source-path for the AREF
2894 ;;; call is as follows:
2895 ;;;    (5 1 0 1 3 11)
2896 ;;; Given the DEFUN, 3 gets you the LET, 1 gets you the bindings, 0
2897 ;;; gets the first binding, and 1 gets the AREF form.
2898
2899 ;;; temporary buffer used to build form-number => source-path translation in
2900 ;;; FORM-NUMBER-TRANSLATIONS
2901 (defvar *form-number-temp* (make-array 10 :fill-pointer 0 :adjustable t))
2902
2903 ;;; table used to detect CAR circularities in FORM-NUMBER-TRANSLATIONS
2904 (defvar *form-number-circularity-table* (make-hash-table :test 'eq))
2905
2906 ;;; This returns a table mapping form numbers to source-paths. A source-path
2907 ;;; indicates a descent into the top-level-form form, going directly to the
2908 ;;; subform corressponding to the form number.
2909 ;;;
2910 ;;; The vector elements are in the same format as the compiler's
2911 ;;; NODE-SOURCE-PATH; that is, the first element is the form number and
2912 ;;; the last is the top-level-form number.
2913 (defun form-number-translations (form tlf-number)
2914   (clrhash *form-number-circularity-table*)
2915   (setf (fill-pointer *form-number-temp*) 0)
2916   (sub-translate-form-numbers form (list tlf-number))
2917   (coerce *form-number-temp* 'simple-vector))
2918 (defun sub-translate-form-numbers (form path)
2919   (unless (gethash form *form-number-circularity-table*)
2920     (setf (gethash form *form-number-circularity-table*) t)
2921     (vector-push-extend (cons (fill-pointer *form-number-temp*) path)
2922                         *form-number-temp*)
2923     (let ((pos 0)
2924           (subform form)
2925           (trail form))
2926       (declare (fixnum pos))
2927       (macrolet ((frob ()
2928                    '(progn
2929                       (when (atom subform) (return))
2930                       (let ((fm (car subform)))
2931                         (when (consp fm)
2932                           (sub-translate-form-numbers fm (cons pos path)))
2933                         (incf pos))
2934                       (setq subform (cdr subform))
2935                       (when (eq subform trail) (return)))))
2936         (loop
2937           (frob)
2938           (frob)
2939           (setq trail (cdr trail)))))))
2940
2941 ;;; FORM is a top-level form, and path is a source-path into it. This
2942 ;;; returns the form indicated by the source-path. Context is the
2943 ;;; number of enclosing forms to return instead of directly returning
2944 ;;; the source-path form. When context is non-zero, the form returned
2945 ;;; contains a marker, #:****HERE****, immediately before the form
2946 ;;; indicated by path.
2947 (defun source-path-context (form path context)
2948   (declare (type unsigned-byte context))
2949   ;; Get to the form indicated by path or the enclosing form indicated
2950   ;; by context and path.
2951   (let ((path (reverse (butlast (cdr path)))))
2952     (dotimes (i (- (length path) context))
2953       (let ((index (first path)))
2954         (unless (and (listp form) (< index (length form)))
2955           (error "Source path no longer exists."))
2956         (setq form (elt form index))
2957         (setq path (rest path))))
2958     ;; Recursively rebuild the source form resulting from the above
2959     ;; descent, copying the beginning of each subform up to the next
2960     ;; subform we descend into according to path. At the bottom of the
2961     ;; recursion, we return the form indicated by path preceded by our
2962     ;; marker, and this gets spliced into the resulting list structure
2963     ;; on the way back up.
2964     (labels ((frob (form path level)
2965                (if (or (zerop level) (null path))
2966                    (if (zerop context)
2967                        form
2968                        `(#:***here*** ,form))
2969                    (let ((n (first path)))
2970                      (unless (and (listp form) (< n (length form)))
2971                        (error "Source path no longer exists."))
2972                      (let ((res (frob (elt form n) (rest path) (1- level))))
2973                        (nconc (subseq form 0 n)
2974                               (cons res (nthcdr (1+ n) form))))))))
2975       (frob form path context))))
2976 \f
2977 ;;;; PREPROCESS-FOR-EVAL
2978
2979 ;;; Return a function of one argument that evaluates form in the
2980 ;;; lexical context of the BASIC-CODE-LOCATION LOC, or signal a
2981 ;;; NO-DEBUG-VARS condition when the LOC's DEBUG-FUNCTION has no
2982 ;;; DEBUG-VAR information available.
2983 ;;;
2984 ;;; The returned function takes the frame to get values from as its
2985 ;;; argument, and it returns the values of FORM. The returned function
2986 ;;; can signal the following conditions: INVALID-VALUE,
2987 ;;; AMBIGUOUS-VARIABLE-NAME, and FRAME-FUNCTION-MISMATCH.
2988 (defun preprocess-for-eval (form loc)
2989   (declare (type code-location loc))
2990   (let ((n-frame (gensym))
2991         (fun (code-location-debug-function loc)))
2992     (unless (debug-var-info-available fun)
2993       (debug-signal 'no-debug-vars :debug-function fun))
2994     (sb!int:collect ((binds)
2995                      (specs))
2996       (do-debug-function-variables (var fun)
2997         (let ((validity (debug-var-validity var loc)))
2998           (unless (eq validity :invalid)
2999             (let* ((sym (debug-var-symbol var))
3000                    (found (assoc sym (binds))))
3001               (if found
3002                   (setf (second found) :ambiguous)
3003                   (binds (list sym validity var)))))))
3004       (dolist (bind (binds))
3005         (let ((name (first bind))
3006               (var (third bind)))
3007           (ecase (second bind)
3008             (:valid
3009              (specs `(,name (debug-var-value ',var ,n-frame))))
3010             (:unknown
3011              (specs `(,name (debug-signal 'invalid-value :debug-var ',var
3012                                           :frame ,n-frame))))
3013             (:ambiguous
3014              (specs `(,name (debug-signal 'ambiguous-variable-name :name ',name
3015                                           :frame ,n-frame)))))))
3016       (let ((res (coerce `(lambda (,n-frame)
3017                             (declare (ignorable ,n-frame))
3018                             (symbol-macrolet ,(specs) ,form))
3019                          'function)))
3020         #'(lambda (frame)
3021             ;; This prevents these functions from being used in any
3022             ;; location other than a function return location, so
3023             ;; maybe this should only check whether frame's
3024             ;; debug-function is the same as loc's.
3025             (unless (code-location= (frame-code-location frame) loc)
3026               (debug-signal 'frame-function-mismatch
3027                             :code-location loc :form form :frame frame))
3028             (funcall res frame))))))
3029 \f
3030 ;;;; breakpoints
3031
3032 ;;;; user-visible interface
3033
3034 ;;; Create and return a breakpoint. When program execution encounters
3035 ;;; the breakpoint, the system calls HOOK-FUNCTION. HOOK-FUNCTION takes the
3036 ;;; current frame for the function in which the program is running and the
3037 ;;; breakpoint object.
3038 ;;;
3039 ;;; WHAT and KIND determine where in a function the system invokes
3040 ;;; HOOK-FUNCTION. WHAT is either a code-location or a debug-function.
3041 ;;; KIND is one of :CODE-LOCATION, :FUNCTION-START, or :FUNCTION-END.
3042 ;;; Since the starts and ends of functions may not have code-locations
3043 ;;; representing them, designate these places by supplying WHAT as a
3044 ;;; debug-function and KIND indicating the :FUNCTION-START or
3045 ;;; :FUNCTION-END. When WHAT is a debug-function and kind is
3046 ;;; :FUNCTION-END, then hook-function must take two additional
3047 ;;; arguments, a list of values returned by the function and a
3048 ;;; FUNCTION-END-COOKIE.
3049 ;;;
3050 ;;; INFO is information supplied by and used by the user.
3051 ;;;
3052 ;;; FUNCTION-END-COOKIE is a function. To implement :FUNCTION-END
3053 ;;; breakpoints, the system uses starter breakpoints to establish the
3054 ;;; :FUNCTION-END breakpoint for each invocation of the function. Upon
3055 ;;; each entry, the system creates a unique cookie to identify the
3056 ;;; invocation, and when the user supplies a function for this
3057 ;;; argument, the system invokes it on the frame and the cookie. The
3058 ;;; system later invokes the :FUNCTION-END breakpoint hook on the same
3059 ;;; cookie. The user may save the cookie for comparison in the hook
3060 ;;; function.
3061 ;;;
3062 ;;; Signal an error if WHAT is an unknown code-location.
3063 (defun make-breakpoint (hook-function what
3064                         &key (kind :code-location) info function-end-cookie)
3065   (etypecase what
3066     (code-location
3067      (when (code-location-unknown-p what)
3068        (error "cannot make a breakpoint at an unknown code location: ~S"
3069               what))
3070      (aver (eq kind :code-location))
3071      (let ((bpt (%make-breakpoint hook-function what kind info)))
3072        (etypecase what
3073          (compiled-code-location
3074           ;; This slot is filled in due to calling CODE-LOCATION-UNKNOWN-P.
3075           (when (eq (compiled-code-location-kind what) :unknown-return)
3076             (let ((other-bpt (%make-breakpoint hook-function what
3077                                                :unknown-return-partner
3078                                                info)))
3079               (setf (breakpoint-unknown-return-partner bpt) other-bpt)
3080               (setf (breakpoint-unknown-return-partner other-bpt) bpt))))
3081          ;; (There used to be more cases back before sbcl-0.7.0,,
3082          ;; when we did special tricks to debug the IR1
3083          ;; interpreter.)
3084          )
3085        bpt))
3086     (compiled-debug-function
3087      (ecase kind
3088        (:function-start
3089         (%make-breakpoint hook-function what kind info))
3090        (:function-end
3091         (unless (eq (sb!c::compiled-debug-function-returns
3092                      (compiled-debug-function-compiler-debug-fun what))
3093                     :standard)
3094           (error ":FUNCTION-END breakpoints are currently unsupported ~
3095                   for the known return convention."))
3096
3097         (let* ((bpt (%make-breakpoint hook-function what kind info))
3098                (starter (compiled-debug-function-end-starter what)))
3099           (unless starter
3100             (setf starter (%make-breakpoint #'list what :function-start nil))
3101             (setf (breakpoint-hook-function starter)
3102                   (function-end-starter-hook starter what))
3103             (setf (compiled-debug-function-end-starter what) starter))
3104           (setf (breakpoint-start-helper bpt) starter)
3105           (push bpt (breakpoint-%info starter))
3106           (setf (breakpoint-cookie-fun bpt) function-end-cookie)
3107           bpt))))))
3108
3109 ;;; These are unique objects created upon entry into a function by a
3110 ;;; :FUNCTION-END breakpoint's starter hook. These are only created
3111 ;;; when users supply :FUNCTION-END-COOKIE to MAKE-BREAKPOINT. Also,
3112 ;;; the :FUNCTION-END breakpoint's hook is called on the same cookie
3113 ;;; when it is created.
3114 (defstruct (function-end-cookie
3115             (:print-object (lambda (obj str)
3116                              (print-unreadable-object (obj str :type t))))
3117             (:constructor make-function-end-cookie (bogus-lra debug-fun))
3118             (:copier nil))
3119   ;; a pointer to the bogus-lra created for :FUNCTION-END breakpoints
3120   bogus-lra
3121   ;; the debug-function associated with the cookie
3122   debug-fun)
3123
3124 ;;; This maps bogus-lra-components to cookies, so that
3125 ;;; HANDLE-FUNCTION-END-BREAKPOINT can find the appropriate cookie for the
3126 ;;; breakpoint hook.
3127 (defvar *function-end-cookies* (make-hash-table :test 'eq))
3128
3129 ;;; This returns a hook function for the start helper breakpoint
3130 ;;; associated with a :FUNCTION-END breakpoint. The returned function
3131 ;;; makes a fake LRA that all returns go through, and this piece of
3132 ;;; fake code actually breaks. Upon return from the break, the code
3133 ;;; provides the returnee with any values. Since the returned function
3134 ;;; effectively activates FUN-END-BPT on each entry to DEBUG-FUN's
3135 ;;; function, we must establish breakpoint-data about FUN-END-BPT.
3136 (defun function-end-starter-hook (starter-bpt debug-fun)
3137   (declare (type breakpoint starter-bpt)
3138            (type compiled-debug-function debug-fun))
3139   #'(lambda (frame breakpoint)
3140       (declare (ignore breakpoint)
3141                (type frame frame))
3142       (let ((lra-sc-offset
3143              (sb!c::compiled-debug-function-return-pc
3144               (compiled-debug-function-compiler-debug-fun debug-fun))))
3145         (multiple-value-bind (lra component offset)
3146             (make-bogus-lra
3147              (get-context-value frame
3148                                 #!-gengc sb!vm::lra-save-offset
3149                                 #!+gengc sb!vm::ra-save-offset
3150                                 lra-sc-offset))
3151           (setf (get-context-value frame
3152                                    #!-gengc sb!vm::lra-save-offset
3153                                    #!+gengc sb!vm::ra-save-offset
3154                                    lra-sc-offset)
3155                 lra)
3156           (let ((end-bpts (breakpoint-%info starter-bpt)))
3157             (let ((data (breakpoint-data component offset)))
3158               (setf (breakpoint-data-breakpoints data) end-bpts)
3159               (dolist (bpt end-bpts)
3160                 (setf (breakpoint-internal-data bpt) data)))
3161             (let ((cookie (make-function-end-cookie lra debug-fun)))
3162               (setf (gethash component *function-end-cookies*) cookie)
3163               (dolist (bpt end-bpts)
3164                 (let ((fun (breakpoint-cookie-fun bpt)))
3165                   (when fun (funcall fun frame cookie))))))))))
3166
3167 ;;; This takes a FUNCTION-END-COOKIE and a frame, and it returns
3168 ;;; whether the cookie is still valid. A cookie becomes invalid when
3169 ;;; the frame that established the cookie has exited. Sometimes cookie
3170 ;;; holders are unaware of cookie invalidation because their
3171 ;;; :FUNCTION-END breakpoint hooks didn't run due to THROW'ing.
3172 ;;;
3173 ;;; This takes a frame as an efficiency hack since the user probably
3174 ;;; has a frame object in hand when using this routine, and it saves
3175 ;;; repeated parsing of the stack and consing when asking whether a
3176 ;;; series of cookies is valid.
3177 (defun function-end-cookie-valid-p (frame cookie)
3178   (let ((lra (function-end-cookie-bogus-lra cookie))
3179         (lra-sc-offset (sb!c::compiled-debug-function-return-pc
3180                         (compiled-debug-function-compiler-debug-fun
3181                          (function-end-cookie-debug-fun cookie)))))
3182     (do ((frame frame (frame-down frame)))
3183         ((not frame) nil)
3184       (when (and (compiled-frame-p frame)
3185                  (eq lra
3186                      (get-context-value frame
3187                                         #!-gengc sb!vm::lra-save-offset
3188                                         #!+gengc sb!vm::ra-save-offset
3189                                         lra-sc-offset)))
3190         (return t)))))
3191 \f
3192 ;;;; ACTIVATE-BREAKPOINT
3193
3194 ;;; Cause the system to invoke the breakpoint's hook-function until
3195 ;;; the next call to DEACTIVATE-BREAKPOINT or DELETE-BREAKPOINT. The
3196 ;;; system invokes breakpoint hook functions in the opposite order
3197 ;;; that you activate them.
3198 (defun activate-breakpoint (breakpoint)
3199   (when (eq (breakpoint-status breakpoint) :deleted)
3200     (error "cannot activate a deleted breakpoint: ~S" breakpoint))
3201   (unless (eq (breakpoint-status breakpoint) :active)
3202     (ecase (breakpoint-kind breakpoint)
3203       (:code-location
3204        (let ((loc (breakpoint-what breakpoint)))
3205          (etypecase loc
3206            (compiled-code-location
3207             (activate-compiled-code-location-breakpoint breakpoint)
3208             (let ((other (breakpoint-unknown-return-partner breakpoint)))
3209               (when other
3210                 (activate-compiled-code-location-breakpoint other))))
3211            ;; (There used to be more cases back before sbcl-0.7.0, when
3212            ;; we did special tricks to debug the IR1 interpreter.)
3213            )))
3214       (:function-start
3215        (etypecase (breakpoint-what breakpoint)
3216          (compiled-debug-function
3217           (activate-compiled-function-start-breakpoint breakpoint))
3218          ;; (There used to be more cases back before sbcl-0.7.0, when
3219          ;; we did special tricks to debug the IR1 interpreter.)
3220          ))
3221       (:function-end
3222        (etypecase (breakpoint-what breakpoint)
3223          (compiled-debug-function
3224           (let ((starter (breakpoint-start-helper breakpoint)))
3225             (unless (eq (breakpoint-status starter) :active)
3226               ;; may already be active by some other :FUNCTION-END breakpoint
3227               (activate-compiled-function-start-breakpoint starter)))
3228           (setf (breakpoint-status breakpoint) :active))
3229          ;; (There used to be more cases back before sbcl-0.7.0, when
3230          ;; we did special tricks to debug the IR1 interpreter.)
3231          ))))
3232   breakpoint)
3233
3234 (defun activate-compiled-code-location-breakpoint (breakpoint)
3235   (declare (type breakpoint breakpoint))
3236   (let ((loc (breakpoint-what breakpoint)))
3237     (declare (type compiled-code-location loc))
3238     (sub-activate-breakpoint
3239      breakpoint
3240      (breakpoint-data (compiled-debug-function-component
3241                        (code-location-debug-function loc))
3242                       (+ (compiled-code-location-pc loc)
3243                          (if (or (eq (breakpoint-kind breakpoint)
3244                                      :unknown-return-partner)
3245                                  (eq (compiled-code-location-kind loc)
3246                                      :single-value-return))
3247                              sb!vm:single-value-return-byte-offset
3248                              0))))))
3249
3250 (defun activate-compiled-function-start-breakpoint (breakpoint)
3251   (declare (type breakpoint breakpoint))
3252   (let ((debug-fun (breakpoint-what breakpoint)))
3253     (sub-activate-breakpoint
3254      breakpoint
3255      (breakpoint-data (compiled-debug-function-component debug-fun)
3256                       (sb!c::compiled-debug-function-start-pc
3257                        (compiled-debug-function-compiler-debug-fun
3258                         debug-fun))))))
3259
3260 (defun sub-activate-breakpoint (breakpoint data)
3261   (declare (type breakpoint breakpoint)
3262            (type breakpoint-data data))
3263   (setf (breakpoint-status breakpoint) :active)
3264   (without-interrupts
3265    (unless (breakpoint-data-breakpoints data)
3266      (setf (breakpoint-data-instruction data)
3267            (without-gcing
3268             (breakpoint-install (get-lisp-obj-address
3269                                  (breakpoint-data-component data))
3270                                 (breakpoint-data-offset data)))))
3271    (setf (breakpoint-data-breakpoints data)
3272          (append (breakpoint-data-breakpoints data) (list breakpoint)))
3273    (setf (breakpoint-internal-data breakpoint) data)))
3274 \f
3275 ;;;; DEACTIVATE-BREAKPOINT
3276
3277 ;;; Stop the system from invoking the breakpoint's hook-function.
3278 (defun deactivate-breakpoint (breakpoint)
3279   (when (eq (breakpoint-status breakpoint) :active)
3280     (without-interrupts
3281      (let ((loc (breakpoint-what breakpoint)))
3282        (etypecase loc
3283          ((or compiled-code-location compiled-debug-function)
3284           (deactivate-compiled-breakpoint breakpoint)
3285           (let ((other (breakpoint-unknown-return-partner breakpoint)))
3286             (when other
3287               (deactivate-compiled-breakpoint other))))
3288          ;; (There used to be more cases back before sbcl-0.7.0, when
3289          ;; we did special tricks to debug the IR1 interpreter.)
3290          ))))
3291   breakpoint)
3292
3293 (defun deactivate-compiled-breakpoint (breakpoint)
3294   (if (eq (breakpoint-kind breakpoint) :function-end)
3295       (let ((starter (breakpoint-start-helper breakpoint)))
3296         (unless (find-if #'(lambda (bpt)
3297                              (and (not (eq bpt breakpoint))
3298                                   (eq (breakpoint-status bpt) :active)))
3299                          (breakpoint-%info starter))
3300           (deactivate-compiled-breakpoint starter)))
3301       (let* ((data (breakpoint-internal-data breakpoint))
3302              (bpts (delete breakpoint (breakpoint-data-breakpoints data))))
3303         (setf (breakpoint-internal-data breakpoint) nil)
3304         (setf (breakpoint-data-breakpoints data) bpts)
3305         (unless bpts
3306           (without-gcing
3307            (breakpoint-remove (get-lisp-obj-address
3308                                (breakpoint-data-component data))
3309                               (breakpoint-data-offset data)
3310                               (breakpoint-data-instruction data)))
3311           (delete-breakpoint-data data))))
3312   (setf (breakpoint-status breakpoint) :inactive)
3313   breakpoint)
3314 \f
3315 ;;;; BREAKPOINT-INFO
3316
3317 ;;; Return the user-maintained info associated with breakpoint. This
3318 ;;; is SETF'able.
3319 (defun breakpoint-info (breakpoint)
3320   (breakpoint-%info breakpoint))
3321 (defun %set-breakpoint-info (breakpoint value)
3322   (setf (breakpoint-%info breakpoint) value)
3323   (let ((other (breakpoint-unknown-return-partner breakpoint)))
3324     (when other
3325       (setf (breakpoint-%info other) value))))
3326 \f
3327 ;;;; BREAKPOINT-ACTIVE-P and DELETE-BREAKPOINT
3328
3329 (defun breakpoint-active-p (breakpoint)
3330   (ecase (breakpoint-status breakpoint)
3331     (:active t)
3332     ((:inactive :deleted) nil)))
3333
3334 ;;; Free system storage and remove computational overhead associated
3335 ;;; with breakpoint. After calling this, breakpoint is completely
3336 ;;; impotent and can never become active again.
3337 (defun delete-breakpoint (breakpoint)
3338   (let ((status (breakpoint-status breakpoint)))
3339     (unless (eq status :deleted)
3340       (when (eq status :active)
3341         (deactivate-breakpoint breakpoint))
3342       (setf (breakpoint-status breakpoint) :deleted)
3343       (let ((other (breakpoint-unknown-return-partner breakpoint)))
3344         (when other
3345           (setf (breakpoint-status other) :deleted)))
3346       (when (eq (breakpoint-kind breakpoint) :function-end)
3347         (let* ((starter (breakpoint-start-helper breakpoint))
3348                (breakpoints (delete breakpoint
3349                                     (the list (breakpoint-info starter)))))
3350           (setf (breakpoint-info starter) breakpoints)
3351           (unless breakpoints
3352             (delete-breakpoint starter)
3353             (setf (compiled-debug-function-end-starter
3354                    (breakpoint-what breakpoint))
3355                   nil))))))
3356   breakpoint)
3357 \f
3358 ;;;; C call out stubs
3359
3360 ;;; This actually installs the break instruction in the component. It
3361 ;;; returns the overwritten bits. You must call this in a context in
3362 ;;; which GC is disabled, so that Lisp doesn't move objects around
3363 ;;; that C is pointing to.
3364 (sb!alien:def-alien-routine "breakpoint_install" sb!c-call:unsigned-long
3365   (code-obj sb!c-call:unsigned-long)
3366   (pc-offset sb!c-call:int))
3367
3368 ;;; This removes the break instruction and replaces the original
3369 ;;; instruction. You must call this in a context in which GC is disabled
3370 ;;; so Lisp doesn't move objects around that C is pointing to.
3371 (sb!alien:def-alien-routine "breakpoint_remove" sb!c-call:void
3372   (code-obj sb!c-call:unsigned-long)
3373   (pc-offset sb!c-call:int)
3374   (old-inst sb!c-call:unsigned-long))
3375
3376 (sb!alien:def-alien-routine "breakpoint_do_displaced_inst" sb!c-call:void
3377   (scp (* os-context-t))
3378   (orig-inst sb!c-call:unsigned-long))
3379
3380 ;;;; breakpoint handlers (layer between C and exported interface)
3381
3382 ;;; This maps components to a mapping of offsets to breakpoint-datas.
3383 (defvar *component-breakpoint-offsets* (make-hash-table :test 'eq))
3384
3385 ;;; This returns the breakpoint-data associated with component cross
3386 ;;; offset. If none exists, this makes one, installs it, and returns it.
3387 (defun breakpoint-data (component offset &optional (create t))
3388   (flet ((install-breakpoint-data ()
3389            (when create
3390              (let ((data (make-breakpoint-data component offset)))
3391                (push (cons offset data)
3392                      (gethash component *component-breakpoint-offsets*))
3393                data))))
3394     (let ((offsets (gethash component *component-breakpoint-offsets*)))
3395       (if offsets
3396           (let ((data (assoc offset offsets)))
3397             (if data
3398                 (cdr data)
3399                 (install-breakpoint-data)))
3400           (install-breakpoint-data)))))
3401
3402 ;;; We use this when there are no longer any active breakpoints
3403 ;;; corresponding to data.
3404 (defun delete-breakpoint-data (data)
3405   (let* ((component (breakpoint-data-component data))
3406          (offsets (delete (breakpoint-data-offset data)
3407                           (gethash component *component-breakpoint-offsets*)
3408                           :key #'car)))
3409     (if offsets
3410         (setf (gethash component *component-breakpoint-offsets*) offsets)
3411         (remhash component *component-breakpoint-offsets*)))
3412   (values))
3413
3414 ;;; The C handler for interrupts calls this when it has a
3415 ;;; debugging-tool break instruction. This does NOT handle all breaks;
3416 ;;; for example, it does not handle breaks for internal errors.
3417 (defun handle-breakpoint (offset component signal-context)
3418   (/show0 "entering HANDLE-BREAKPOINT")
3419   (let ((data (breakpoint-data component offset nil)))
3420     (unless data
3421       (error "unknown breakpoint in ~S at offset ~S"
3422               (debug-function-name (debug-function-from-pc component offset))
3423               offset))
3424     (let ((breakpoints (breakpoint-data-breakpoints data)))
3425       (if (or (null breakpoints)
3426               (eq (breakpoint-kind (car breakpoints)) :function-end))
3427           (handle-function-end-breakpoint-aux breakpoints data signal-context)
3428           (handle-breakpoint-aux breakpoints data
3429                                  offset component signal-context)))))
3430
3431 ;;; This holds breakpoint-datas while invoking the breakpoint hooks
3432 ;;; associated with that particular component and location. While they
3433 ;;; are executing, if we hit the location again, we ignore the
3434 ;;; breakpoint to avoid infinite recursion. Function-end breakpoints
3435 ;;; must work differently since the breakpoint-data is unique for each
3436 ;;; invocation.
3437 (defvar *executing-breakpoint-hooks* nil)
3438
3439 ;;; This handles code-location and debug-function :FUNCTION-START
3440 ;;; breakpoints.
3441 (defun handle-breakpoint-aux (breakpoints data offset component signal-context)
3442   (/show0 "entering HANDLE-BREAKPOINT-AUX")
3443   (unless breakpoints
3444     (error "internal error: breakpoint that nobody wants"))
3445   (unless (member data *executing-breakpoint-hooks*)
3446     (let ((*executing-breakpoint-hooks* (cons data
3447                                               *executing-breakpoint-hooks*)))
3448       (invoke-breakpoint-hooks breakpoints component offset)))
3449   ;; At this point breakpoints may not hold the same list as
3450   ;; BREAKPOINT-DATA-BREAKPOINTS since invoking hooks may have allowed
3451   ;; a breakpoint deactivation. In fact, if all breakpoints were
3452   ;; deactivated then data is invalid since it was deleted and so the
3453   ;; correct one must be looked up if it is to be used. If there are
3454   ;; no more breakpoints active at this location, then the normal
3455   ;; instruction has been put back, and we do not need to
3456   ;; DO-DISPLACED-INST.
3457   (let ((data (breakpoint-data component offset nil)))
3458     (when (and data (breakpoint-data-breakpoints data))
3459       ;; The breakpoint is still active, so we need to execute the
3460       ;; displaced instruction and leave the breakpoint instruction
3461       ;; behind. The best way to do this is different on each machine,
3462       ;; so we just leave it up to the C code.
3463       (breakpoint-do-displaced-inst signal-context
3464                                     (breakpoint-data-instruction data))
3465       ;; Some platforms have no usable sigreturn() call.  If your
3466       ;; implementation of arch_do_displaced_inst() doesn't sigreturn(),
3467       ;; add it to this list.
3468       #!-(or hpux irix x86 alpha)
3469       (error "BREAKPOINT-DO-DISPLACED-INST returned?"))))
3470
3471 (defun invoke-breakpoint-hooks (breakpoints component offset)
3472   (let* ((debug-fun (debug-function-from-pc component offset))
3473          (frame (do ((f (top-frame) (frame-down f)))
3474                     ((eq debug-fun (frame-debug-function f)) f))))
3475     (dolist (bpt breakpoints)
3476       (funcall (breakpoint-hook-function bpt)
3477                frame
3478                ;; If this is an :UNKNOWN-RETURN-PARTNER, then pass the
3479                ;; hook function the original breakpoint, so that users
3480                ;; aren't forced to confront the fact that some
3481                ;; breakpoints really are two.
3482                (if (eq (breakpoint-kind bpt) :unknown-return-partner)
3483                    (breakpoint-unknown-return-partner bpt)
3484                    bpt)))))
3485
3486 (defun handle-function-end-breakpoint (offset component context)
3487   (/show0 "entering HANDLE-FUNCTION-END-BREAKPOINT")
3488   (let ((data (breakpoint-data component offset nil)))
3489     (unless data
3490       (error "unknown breakpoint in ~S at offset ~S"
3491               (debug-function-name (debug-function-from-pc component offset))
3492               offset))
3493     (let ((breakpoints (breakpoint-data-breakpoints data)))
3494       (when breakpoints
3495         (aver (eq (breakpoint-kind (car breakpoints)) :function-end))
3496         (handle-function-end-breakpoint-aux breakpoints data context)))))
3497
3498 ;;; Either HANDLE-BREAKPOINT calls this for :FUNCTION-END breakpoints
3499 ;;; [old C code] or HANDLE-FUNCTION-END-BREAKPOINT calls this directly
3500 ;;; [new C code].
3501 (defun handle-function-end-breakpoint-aux (breakpoints data signal-context)
3502   (/show0 "entering HANDLE-FUNCTION-END-BREAKPOINT-AUX")
3503   (delete-breakpoint-data data)
3504   (let* ((scp
3505           (locally
3506             (declare (optimize (inhibit-warnings 3)))
3507             (sb!alien:sap-alien signal-context (* os-context-t))))
3508          (frame (do ((cfp (sb!vm:context-register scp sb!vm::cfp-offset))
3509                      (f (top-frame) (frame-down f)))
3510                     ((= cfp (sap-int (frame-pointer f))) f)
3511                   (declare (type (unsigned-byte #.sb!vm:word-bits) cfp))))
3512          (component (breakpoint-data-component data))
3513          (cookie (gethash component *function-end-cookies*)))
3514     (remhash component *function-end-cookies*)
3515     (dolist (bpt breakpoints)
3516       (funcall (breakpoint-hook-function bpt)
3517                frame bpt
3518                (get-function-end-breakpoint-values scp)
3519                cookie))))
3520
3521 (defun get-function-end-breakpoint-values (scp)
3522   (let ((ocfp (int-sap (sb!vm:context-register
3523                         scp
3524                         #!-x86 sb!vm::ocfp-offset
3525                         #!+x86 sb!vm::ebx-offset)))
3526         (nargs (make-lisp-obj
3527                 (sb!vm:context-register scp sb!vm::nargs-offset)))
3528         (reg-arg-offsets '#.sb!vm::*register-arg-offsets*)
3529         (results nil))
3530     (without-gcing
3531      (dotimes (arg-num nargs)
3532        (push (if reg-arg-offsets
3533                  (make-lisp-obj
3534                   (sb!vm:context-register scp (pop reg-arg-offsets)))
3535                (stack-ref ocfp arg-num))
3536              results)))
3537     (nreverse results)))
3538 \f
3539 ;;;; MAKE-BOGUS-LRA (used for :FUNCTION-END breakpoints)
3540
3541 (defconstant bogus-lra-constants
3542   #!-x86 2 #!+x86 3)
3543 (defconstant known-return-p-slot
3544   (+ sb!vm:code-constants-offset #!-x86 1 #!+x86 2))
3545
3546 ;;; Make a bogus LRA object that signals a breakpoint trap when
3547 ;;; returned to. If the breakpoint trap handler returns, REAL-LRA is
3548 ;;; returned to. Three values are returned: the bogus LRA object, the
3549 ;;; code component it is part of, and the PC offset for the trap
3550 ;;; instruction.
3551 (defun make-bogus-lra (real-lra &optional known-return-p)
3552   (without-gcing
3553    (let* ((src-start (foreign-symbol-address "function_end_breakpoint_guts"))
3554           (src-end (foreign-symbol-address "function_end_breakpoint_end"))
3555           (trap-loc (foreign-symbol-address "function_end_breakpoint_trap"))
3556           (length (sap- src-end src-start))
3557           (code-object
3558            (%primitive
3559             #!-(and x86 gencgc) sb!c:allocate-code-object
3560             #!+(and x86 gencgc) sb!c::allocate-dynamic-code-object
3561             (1+ bogus-lra-constants)
3562             length))
3563           (dst-start (code-instructions code-object)))
3564      (declare (type system-area-pointer
3565                     src-start src-end dst-start trap-loc)
3566               (type index length))
3567      (setf (%code-debug-info code-object) :bogus-lra)
3568      (setf (code-header-ref code-object sb!vm:code-trace-table-offset-slot)
3569            length)
3570      #!-x86
3571      (setf (code-header-ref code-object real-lra-slot) real-lra)
3572      #!+x86
3573      (multiple-value-bind (offset code) (compute-lra-data-from-pc real-lra)
3574        (setf (code-header-ref code-object real-lra-slot) code)
3575        (setf (code-header-ref code-object (1+ real-lra-slot)) offset))
3576      (setf (code-header-ref code-object known-return-p-slot)
3577            known-return-p)
3578      (system-area-copy src-start 0 dst-start 0 (* length sb!vm:byte-bits))
3579      (sb!vm:sanctify-for-execution code-object)
3580      #!+x86
3581      (values dst-start code-object (sap- trap-loc src-start))
3582      #!-x86
3583      (let ((new-lra (make-lisp-obj (+ (sap-int dst-start)
3584                                       sb!vm:other-pointer-type))))
3585        (set-header-data
3586         new-lra
3587         (logandc2 (+ sb!vm:code-constants-offset bogus-lra-constants 1)
3588                   1))
3589        (sb!vm:sanctify-for-execution code-object)
3590        (values new-lra code-object (sap- trap-loc src-start))))))
3591 \f
3592 ;;;; miscellaneous
3593
3594 ;;; This appears here because it cannot go with the DEBUG-FUNCTION
3595 ;;; interface since DO-DEBUG-BLOCK-LOCATIONS isn't defined until after
3596 ;;; the DEBUG-FUNCTION routines.
3597
3598 ;;; Return a code-location before the body of a function and after all
3599 ;;; the arguments are in place; or if that location can't be
3600 ;;; determined due to a lack of debug information, return NIL.
3601 (defun debug-function-start-location (debug-fun)
3602   (etypecase debug-fun
3603     (compiled-debug-function
3604      (code-location-from-pc debug-fun
3605                             (sb!c::compiled-debug-function-start-pc
3606                              (compiled-debug-function-compiler-debug-fun
3607                               debug-fun))
3608                             nil))
3609     ;; (There used to be more cases back before sbcl-0.7.0, when
3610     ;; we did special tricks to debug the IR1 interpreter.)
3611     ))
3612
3613 (defun print-code-locations (function)
3614   (let ((debug-fun (function-debug-function function)))
3615     (do-debug-function-blocks (block debug-fun)
3616       (do-debug-block-locations (loc block)
3617         (fill-in-code-location loc)
3618         (format t "~S code location at ~D"
3619                 (compiled-code-location-kind loc)
3620                 (compiled-code-location-pc loc))
3621         (sb!debug::print-code-location-source-form loc 0)
3622         (terpri)))))