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