1 ;;;; the implementation of the programmer's interface to writing
4 ;;;; This software is part of the SBCL system. See the README file for
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.
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?
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.
35 ;;;; Use DEBUG-SIGNAL to signal these conditions.
37 (define-condition debug-condition
(serious-condition)
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
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
))))
55 "~&Cannot return values from ~:[frame~;~:*~S~] since ~
56 the debug information lacks details about returning ~
60 (define-condition no-debug-blocks
(debug-condition)
61 ((debug-fun :reader no-debug-blocks-debug-fun
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
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
83 "The DEBUG-FUN has no lambda list since argument DEBUG-VARs are
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
112 ;;; While under development, this code also signals errors in code
113 ;;; branches that remain unimplemented.
115 (define-condition debug-error
(error) ()
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
))
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
)
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
)))
170 (error 'unhandled-debug-condition
:condition condition
)))
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.
182 ;;; These exist for caching data stored in packed binary form in
183 ;;; compiler DEBUG-FUNs.
184 (defstruct (debug-var (:constructor 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
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
)
197 (debug-var-symbol debug-var
)
198 (debug-var-id debug-var
))))
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
207 (:constructor make-compiled-debug-var
209 sc-offset save-sc-offset indirect-sc-offset info
))
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
))
222 ;;; These represent call frames on the stack.
223 (defstruct (frame (:constructor 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
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)
243 ;; This is the frame's number for prompt printing. Top is zero.
244 (number 0 :type index
))
246 (defstruct (compiled-frame
248 (:constructor make-compiled-frame
249 (pointer up debug-fun code-location number
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.
257 (def!method print-object
((obj compiled-frame
) str
)
258 (print-unreadable-object (obj str
:type t
)
260 "~S~:[~;, interrupted~]"
261 (debug-fun-name (frame-debug-fun obj
))
262 (compiled-frame-escaped obj
))))
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
)
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
292 (:constructor %make-compiled-debug-fun
293 (compiler-debug-fun component
))
295 ;; compiler's dumped DEBUG-FUN information (unexported)
296 (compiler-debug-fun nil
:type sb
!c
::compiled-debug-fun
)
297 ;; code object (unexported).
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
324 (:constructor make-bogus-debug-fun
335 ;;; These exist for caching data stored in packed binary form in compiler
337 (defstruct (debug-block (:constructor 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
)))
349 (setf (fdocumentation 'debug-block-elsewhere-p
'function
)
350 "Return whether debug-block represents elsewhere code.")
352 (defstruct (compiled-debug-block (:include debug-block
)
354 ;; code-location information for the block
355 (code-locations #() :type simple-vector
))
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
364 ;; This is the component in which the breakpoint lies.
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"
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
))
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,
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.
422 (def!method print-object
((obj breakpoint
) str
)
423 (let ((what (breakpoint-what obj
)))
424 (print-unreadable-object (obj str
:type t
)
429 (debug-fun (debug-fun-name what
)))
432 (debug-fun (breakpoint-kind obj
)))))))
436 (defstruct (code-location (:constructor 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
))
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
))
470 ;; an index into DEBUG-FUN's component slot
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
)))
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
))
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
520 (descriptor-sap *control-stack-start
*))
521 #!+stack-grows-downward-not-upward
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
)
561 ;;(format t "c-lra-fpc ~A ~A ~A~%" pc code pc-offset)
562 (values pc-offset code
)))))
567 (defconstant sb
!vm
::nargs-offset
#.sb
!vm
::ecx-offset
)
569 ;;; Check for a valid return address - it could be any valid C/Lisp
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
))
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
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
)
601 (control-stack-pointer-valid-p ocfp
)
602 (ra-pointer-valid-p ra
))
604 (values nil
(int-sap 0) (int-sap 0)))))
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
617 (/noshow0
"entering TOP-FRAME")
618 (compute-calling-frame (descriptor-sap (%caller-frame
))
622 ;;; Flush all of the frames above FRAME, and renumber all the frames
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
)
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
)
650 (let ((c-d-f (compiled-debug-fun-compiler-debug-fun
652 (compute-calling-frame
655 frame ocfp-save-offset
656 (sb!c
::compiled-debug-fun-old-fp c-d-f
)))
658 frame lra-save-offset
659 (sb!c
::compiled-debug-fun-return-pc c-d-f
))
662 (let ((fp (frame-pointer frame
)))
663 (when (control-stack-pointer-valid-p fp
)
665 (multiple-value-bind (ok ra ofp
) (x86-call-context fp
)
667 (compute-calling-frame ofp ra frame
)
668 (find-saved-frame-down fp frame
)))
670 (compute-calling-frame
672 (sap-ref-sap fp
(* ocfp-save-offset
676 (sap-ref-32 fp
(* ocfp-save-offset
677 sb
!vm
:n-word-bytes
)))
679 (stack-ref fp lra-save-offset
)
684 ;;; Get the old FP or return PC out of FRAME. STACK-SLOT is the
685 ;;; standard save location offset on the stack. LOC is the saved
686 ;;; SC-OFFSET describing the main location.
687 (defun get-context-value (frame stack-slot loc
)
688 (declare (type compiled-frame frame
) (type unsigned-byte stack-slot
)
689 (type sb
!c
:sc-offset loc
))
690 (let ((pointer (frame-pointer frame
))
691 (escaped (compiled-frame-escaped frame
)))
693 (sub-access-debug-var-slot pointer loc escaped
)
695 (stack-ref pointer stack-slot
)
699 (stack-ref pointer stack-slot
))
701 (sap-ref-sap pointer
(sb!vm
::frame-byte-offset stack-slot
)))))))
703 (defun (setf get-context-value
) (value frame stack-slot loc
)
704 (declare (type compiled-frame frame
) (type unsigned-byte stack-slot
)
705 (type sb
!c
:sc-offset loc
))
706 (let ((pointer (frame-pointer frame
))
707 (escaped (compiled-frame-escaped frame
)))
709 (sub-set-debug-var-slot pointer loc value escaped
)
711 (setf (stack-ref pointer stack-slot
) value
)
715 (setf (stack-ref pointer stack-slot
) value
))
717 (setf (sap-ref-sap pointer
(sb!vm
::frame-byte-offset stack-slot
))
720 (defun foreign-function-backtrace-name (sap)
721 (let ((name (sap-foreign-symbol sap
)))
723 (format nil
"foreign function: ~A" name
)
724 (format nil
"foreign function: #x~X" (sap-int sap
)))))
726 ;;; This returns a frame for the one existing in time immediately
727 ;;; prior to the frame referenced by current-fp. This is current-fp's
728 ;;; caller or the next frame down the control stack. If there is no
729 ;;; down frame, this returns NIL for the bottom of the stack. UP-FRAME
730 ;;; is the up link for the resulting frame object, and it is null when
731 ;;; we call this to get the top of the stack.
733 ;;; The current frame contains the pointer to the temporally previous
734 ;;; frame we want, and the current frame contains the pc at which we
735 ;;; will continue executing upon returning to that previous frame.
737 ;;; Note: Sometimes LRA is actually a fixnum. This happens when lisp
738 ;;; calls into C. In this case, the code object is stored on the stack
739 ;;; after the LRA, and the LRA is the word offset.
741 (defun compute-calling-frame (caller lra up-frame
)
742 (declare (type system-area-pointer caller
))
743 (/noshow0
"entering COMPUTE-CALLING-FRAME")
744 (when (control-stack-pointer-valid-p caller
)
746 (multiple-value-bind (code pc-offset escaped
)
748 (multiple-value-bind (word-offset code
)
750 (let ((fp (frame-pointer up-frame
)))
752 (stack-ref fp
(1+ lra-save-offset
))))
753 (values (get-header-data lra
)
754 (lra-code-header lra
)))
757 (* (1+ (- word-offset
(get-header-data code
)))
760 (values :foreign-function
763 (find-escaped-frame caller
))
764 (if (and (code-component-p code
)
765 (eq (%code-debug-info code
) :bogus-lra
))
766 (let ((real-lra (code-header-ref code real-lra-slot
)))
767 (compute-calling-frame caller real-lra up-frame
))
768 (let ((d-fun (case code
770 (make-bogus-debug-fun
771 "undefined function"))
773 (make-bogus-debug-fun
774 (foreign-function-backtrace-name
775 (int-sap (get-lisp-obj-address lra
)))))
777 (make-bogus-debug-fun
778 "bogus stack frame"))
780 (debug-fun-from-pc code pc-offset
)))))
781 (/noshow0
"returning MAKE-COMPILED-FRAME from COMPUTE-CALLING-FRAME")
782 (make-compiled-frame caller up-frame d-fun
783 (code-location-from-pc d-fun pc-offset
785 (if up-frame
(1+ (frame-number up-frame
)) 0)
789 (defun compute-calling-frame (caller ra up-frame
&optional savedp
)
790 (declare (type system-area-pointer caller ra
))
791 (/noshow0
"entering COMPUTE-CALLING-FRAME")
792 (when (control-stack-pointer-valid-p caller
)
794 ;; First check for an escaped frame.
795 (multiple-value-bind (code pc-offset escaped off-stack
)
796 (find-escaped-frame caller
)
799 ;; If it's escaped it may be a function end breakpoint trap.
800 (when (and (code-component-p code
)
801 (eq (%code-debug-info code
) :bogus-lra
))
802 ;; If :bogus-lra grab the real lra.
803 (setq pc-offset
(code-header-ref
804 code
(1+ real-lra-slot
)))
805 (setq code
(code-header-ref code real-lra-slot
))
808 (multiple-value-setq (pc-offset code
)
809 (compute-lra-data-from-pc ra
))
811 (setf code
:foreign-function
813 (let ((d-fun (case code
815 (make-bogus-debug-fun
816 "undefined function"))
818 (make-bogus-debug-fun
819 (foreign-function-backtrace-name ra
)))
821 (make-bogus-debug-fun
822 "bogus stack frame"))
824 (debug-fun-from-pc code pc-offset escaped
)))))
825 (/noshow0
"returning MAKE-COMPILED-FRAME from COMPUTE-CALLING-FRAME")
826 (make-compiled-frame caller up-frame d-fun
827 (code-location-from-pc d-fun pc-offset
829 (if up-frame
(1+ (frame-number up-frame
)) 0)
830 ;; If we have an interrupt-context that's not on
831 ;; our stack at all, and we're computing the
832 ;; from from a saved FP, we're probably looking
833 ;; at an interrupted syscall.
834 (or escaped
(and savedp off-stack
)))))))
836 (defun nth-interrupt-context (n)
837 (declare (type (unsigned-byte 32) n
)
838 (optimize (speed 3) (safety 0)))
839 (sb!alien
:sap-alien
(sb!vm
::current-thread-offset-sap
840 (+ sb
!vm
::thread-interrupt-contexts-offset
845 ;;; On SB-DYNAMIC-CORE symbols which come from the runtime go through
846 ;;; an indirection table, but the debugger needs to know the actual
848 (defun static-foreign-symbol-address (name)
850 (find-dynamic-foreign-symbol-address name
)
852 (foreign-symbol-address name
))
855 (defun static-foreign-symbol-sap (name)
856 (int-sap (static-foreign-symbol-address name
)))
859 (defun find-escaped-frame (frame-pointer)
860 (declare (type system-area-pointer frame-pointer
))
861 (/noshow0
"entering FIND-ESCAPED-FRAME")
862 (dotimes (index *free-interrupt-context-index
* (values nil
0 nil
))
863 (let* ((context (nth-interrupt-context index
))
864 (cfp (int-sap (sb!vm
:context-register context sb
!vm
::cfp-offset
))))
865 (/noshow0
"got CONTEXT")
866 (unless (control-stack-pointer-valid-p cfp
)
867 (return (values nil nil nil t
)))
868 (when (sap= frame-pointer cfp
)
870 (/noshow0
"in WITHOUT-GCING")
871 (let* ((component-ptr (component-ptr-from-pc
872 (sb!vm
:context-pc context
)))
873 (code (unless (sap= component-ptr
(int-sap #x0
))
874 (component-from-component-ptr component-ptr
))))
875 (/noshow0
"got CODE")
877 ;; KLUDGE: Detect undefined functions by a range-check
878 ;; against the trampoline address and the following
879 ;; function in the runtime.
880 (if (< (static-foreign-symbol-address "undefined_tramp")
881 (sap-int (sb!vm
:context-pc context
))
882 (static-foreign-symbol-address #!+x86
"closure_tramp"
883 #!+x86-64
"alloc_tramp"))
884 (return (values :undefined-function
0 context
))
885 (return (values code
0 context
))))
886 (let* ((code-header-len (* (get-header-data code
)
889 (- (sap-int (sb!vm
:context-pc context
))
890 (- (get-lisp-obj-address code
)
891 sb
!vm
:other-pointer-lowtag
)
893 (/noshow
"got PC-OFFSET")
894 (unless (<= 0 pc-offset
(%code-code-size code
))
895 ;; We were in an assembly routine. Therefore, use the
898 ;; FIXME: Should this be WARN or ERROR or what?
899 (format t
"** pc-offset ~S not in code obj ~S?~%"
901 (/noshow0
"returning from FIND-ESCAPED-FRAME")
903 (values code pc-offset context
)))))))))
906 (defun find-escaped-frame (frame-pointer)
907 (declare (type system-area-pointer frame-pointer
))
908 (/noshow0
"entering FIND-ESCAPED-FRAME")
909 (dotimes (index *free-interrupt-context-index
* (values nil
0 nil
))
910 (let ((scp (nth-interrupt-context index
)))
912 (when (= (sap-int frame-pointer
)
913 (sb!vm
:context-register scp sb
!vm
::cfp-offset
))
915 (/noshow0
"in WITHOUT-GCING")
916 (let ((code (code-object-from-bits
917 (sb!vm
:context-register scp sb
!vm
::code-offset
))))
918 (/noshow0
"got CODE")
920 (return (values code
0 scp
)))
921 (let* ((code-header-len (* (get-header-data code
)
924 (- (sap-int (sb!vm
:context-pc scp
))
925 (- (get-lisp-obj-address code
)
926 sb
!vm
:other-pointer-lowtag
)
928 (let ((code-size (%code-code-size code
)))
929 (unless (<= 0 pc-offset code-size
)
930 ;; We were in an assembly routine.
931 (multiple-value-bind (new-pc-offset computed-return
)
932 (find-pc-from-assembly-fun code scp
)
933 (setf pc-offset new-pc-offset
)
934 (unless (<= 0 pc-offset code-size
)
936 "Set PC-OFFSET to zero and continue backtrace."
939 "~@<PC-OFFSET (~D) not in code object. Frame details:~
940 ~2I~:@_PC: #X~X~:@_CODE: ~S~:@_CODE FUN: ~S~:@_LRA: ~
941 #X~X~:@_COMPUTED RETURN: #X~X.~:>"
944 (sap-int (sb!vm
:context-pc scp
))
946 (%code-entry-points code
)
948 (sb!vm
:context-register scp sb
!vm
::lra-offset
)
950 (stack-ref frame-pointer lra-save-offset
)
952 ;; We failed to pinpoint where PC is, but set
953 ;; pc-offset to 0 to keep the backtrace from
955 (setf pc-offset
0)))))
956 (/noshow0
"returning from FIND-ESCAPED-FRAME")
958 (if (eq (%code-debug-info code
) :bogus-lra
)
959 (let ((real-lra (code-header-ref code
961 (values (lra-code-header real-lra
)
962 (get-header-data real-lra
)
964 (values code pc-offset scp
))))))))))
967 (defun find-pc-from-assembly-fun (code scp
)
968 "Finds the PC for the return from an assembly routine properly.
969 For some architectures (such as PPC) this will not be the $LRA
971 (let ((return-machine-address (sb!vm
::return-machine-address scp
))
972 (code-header-len (* (get-header-data code
) sb
!vm
:n-word-bytes
)))
973 (values (- return-machine-address
974 (- (get-lisp-obj-address code
)
975 sb
!vm
:other-pointer-lowtag
)
977 return-machine-address
)))
979 ;;; Find the code object corresponding to the object represented by
980 ;;; bits and return it. We assume bogus functions correspond to the
981 ;;; undefined-function.
983 (defun code-object-from-bits (bits)
984 (declare (type (unsigned-byte 32) bits
))
985 (let ((object (make-lisp-obj bits nil
)))
986 (if (functionp object
)
987 (or (fun-code-header object
)
989 (let ((lowtag (lowtag-of object
)))
990 (when (= lowtag sb
!vm
:other-pointer-lowtag
)
991 (let ((widetag (widetag-of object
)))
992 (cond ((= widetag sb
!vm
:code-header-widetag
)
994 ((= widetag sb
!vm
:return-pc-header-widetag
)
995 (lra-code-header object
))
1001 (defun find-assembly-routine (component pc
)
1002 (let* ((start (sap-int (code-instructions component
)))
1006 (loop for name being the hash-key of sb
!fasl
:*assembler-routines
*
1007 using
(hash-value address
)
1008 when
(and (<= start address end
)
1010 (< (- end address
) min-diff
)))
1011 do
(setf min-name name
1012 min-diff
(- end address
)))
1015 ;;; This returns a COMPILED-DEBUG-FUN for COMPONENT and PC. We fetch the
1016 ;;; SB!C::DEBUG-INFO and run down its FUN-MAP to get a
1017 ;;; SB!C::COMPILED-DEBUG-FUN from the PC. The result only needs to
1018 ;;; reference the COMPONENT, for function constants, and the
1019 ;;; SB!C::COMPILED-DEBUG-FUN.
1020 (defun debug-fun-from-pc (component pc
&optional
(escaped t
))
1021 (let ((info (%code-debug-info component
)))
1024 (make-bogus-debug-fun (or (find-assembly-routine component pc
)
1025 "no debug information for frame")))
1026 ((eq info
:bogus-lra
)
1027 (make-bogus-debug-fun "function end breakpoint"))
1029 (let* ((fun-map (sb!c
::compiled-debug-info-fun-map info
))
1030 (len (length fun-map
)))
1031 (declare (type simple-vector fun-map
))
1033 (make-compiled-debug-fun (svref fun-map
0) component
)
1036 (>= pc
(sb!c
::compiled-debug-fun-elsewhere-pc
1037 (svref fun-map
0)))))
1038 (declare (type sb
!int
:index i
))
1041 (let ((next-pc (if elsewhere-p
1042 (sb!c
::compiled-debug-fun-elsewhere-pc
1043 (svref fun-map
(1+ i
)))
1044 (svref fun-map i
))))
1047 ;; Non-escaped frame means that this frame calls something.
1048 ;; And the PC points to where something should return.
1049 ;; The return adress may be in the next
1050 ;; function, e.g. in local tail calls the
1051 ;; function will be entered just after the
1053 ;; See debug.impure.lisp/:local-tail-call for a test-case
1055 (return (make-compiled-debug-fun
1056 (svref fun-map
(1- i
))
1060 ;;; This returns a code-location for the COMPILED-DEBUG-FUN,
1061 ;;; DEBUG-FUN, and the pc into its code vector. If we stopped at a
1062 ;;; breakpoint, find the CODE-LOCATION for that breakpoint. Otherwise,
1063 ;;; make an :UNSURE code location, so it can be filled in when we
1064 ;;; figure out what is going on.
1065 (defun code-location-from-pc (debug-fun pc escaped
)
1066 (or (and (compiled-debug-fun-p debug-fun
)
1068 (let ((data (breakpoint-data
1069 (compiled-debug-fun-component debug-fun
)
1071 (when (and data
(breakpoint-data-breakpoints data
))
1072 (let ((what (breakpoint-what
1073 (first (breakpoint-data-breakpoints data
)))))
1074 (when (compiled-code-location-p what
)
1076 (make-compiled-code-location pc debug-fun
)))
1078 ;;; Return an alist mapping catch tags to CODE-LOCATIONs. These are
1079 ;;; CODE-LOCATIONs at which execution would continue with frame as the
1080 ;;; top frame if someone threw to the corresponding tag.
1081 (defun frame-catches (frame)
1082 (let ((catch (descriptor-sap sb
!vm
:*current-catch-block
*))
1083 (reversed-result nil
)
1084 (fp (frame-pointer frame
)))
1085 (loop until
(zerop (sap-int catch
))
1086 finally
(return (nreverse reversed-result
))
1091 (* sb
!vm
:catch-block-current-cont-slot
1092 sb
!vm
:n-word-bytes
))
1096 (* sb
!vm
:catch-block-current-cont-slot
1097 sb
!vm
:n-word-bytes
))))
1098 (let* (#!-
(or x86 x86-64
)
1099 (lra (stack-ref catch sb
!vm
:catch-block-entry-pc-slot
))
1102 catch
(* sb
!vm
:catch-block-entry-pc-slot
1103 sb
!vm
:n-word-bytes
)))
1106 (stack-ref catch sb
!vm
:catch-block-current-code-slot
))
1108 (component (component-from-component-ptr
1109 (component-ptr-from-pc ra
)))
1112 (* (- (1+ (get-header-data lra
))
1113 (get-header-data component
))
1117 (- (get-lisp-obj-address component
)
1118 sb
!vm
:other-pointer-lowtag
)
1119 (* (get-header-data component
) sb
!vm
:n-word-bytes
))))
1120 (push (cons #!-
(or x86 x86-64
)
1121 (stack-ref catch sb
!vm
:catch-block-tag-slot
)
1124 (sap-ref-word catch
(* sb
!vm
:catch-block-tag-slot
1125 sb
!vm
:n-word-bytes
)))
1126 (make-compiled-code-location
1127 offset
(frame-debug-fun frame
)))
1132 (* sb
!vm
:catch-block-previous-catch-slot
1133 sb
!vm
:n-word-bytes
))
1137 (* sb
!vm
:catch-block-previous-catch-slot
1138 sb
!vm
:n-word-bytes
)))))))
1140 ;;; Modify the value of the OLD-TAG catches in FRAME to NEW-TAG
1141 (defun replace-frame-catch-tag (frame old-tag new-tag
)
1142 (let ((catch (descriptor-sap sb
!vm
:*current-catch-block
*))
1143 (fp (frame-pointer frame
)))
1144 (loop until
(zerop (sap-int catch
))
1148 (* sb
!vm
:catch-block-current-cont-slot
1149 sb
!vm
:n-word-bytes
))
1153 (* sb
!vm
:catch-block-current-cont-slot
1154 sb
!vm
:n-word-bytes
))))
1157 (stack-ref catch sb
!vm
:catch-block-tag-slot
)
1160 (sap-ref-word catch
(* sb
!vm
:catch-block-tag-slot
1161 sb
!vm
:n-word-bytes
)))))
1162 (when (eq current-tag old-tag
)
1164 (setf (stack-ref catch sb
!vm
:catch-block-tag-slot
) new-tag
)
1166 (setf (sap-ref-word catch
(* sb
!vm
:catch-block-tag-slot
1167 sb
!vm
:n-word-bytes
))
1168 (get-lisp-obj-address new-tag
)))))
1172 (* sb
!vm
:catch-block-previous-catch-slot
1173 sb
!vm
:n-word-bytes
))
1177 (* sb
!vm
:catch-block-previous-catch-slot
1178 sb
!vm
:n-word-bytes
)))))))
1182 ;;;; operations on DEBUG-FUNs
1184 ;;; Execute the forms in a context with BLOCK-VAR bound to each
1185 ;;; DEBUG-BLOCK in DEBUG-FUN successively. Result is an optional
1186 ;;; form to execute for return values, and DO-DEBUG-FUN-BLOCKS
1187 ;;; returns nil if there is no result form. This signals a
1188 ;;; NO-DEBUG-BLOCKS condition when the DEBUG-FUN lacks
1189 ;;; DEBUG-BLOCK information.
1190 (defmacro do-debug-fun-blocks
((block-var debug-fun
&optional result
)
1192 (let ((blocks (gensym))
1194 `(let ((,blocks
(debug-fun-debug-blocks ,debug-fun
)))
1195 (declare (simple-vector ,blocks
))
1196 (dotimes (,i
(length ,blocks
) ,result
)
1197 (let ((,block-var
(svref ,blocks
,i
)))
1200 ;;; Execute body in a context with VAR bound to each DEBUG-VAR in
1201 ;;; DEBUG-FUN. This returns the value of executing result (defaults to
1202 ;;; nil). This may iterate over only some of DEBUG-FUN's variables or
1203 ;;; none depending on debug policy; for example, possibly the
1204 ;;; compilation only preserved argument information.
1205 (defmacro do-debug-fun-vars
((var debug-fun
&optional result
) &body body
)
1206 (let ((vars (gensym))
1208 `(let ((,vars
(debug-fun-debug-vars ,debug-fun
)))
1209 (declare (type (or null simple-vector
) ,vars
))
1211 (dotimes (,i
(length ,vars
) ,result
)
1212 (let ((,var
(svref ,vars
,i
)))
1216 ;;; Return the object of type FUNCTION associated with the DEBUG-FUN,
1217 ;;; or NIL if the function is unavailable or is non-existent as a user
1218 ;;; callable function object.
1219 (defun debug-fun-fun (debug-fun)
1220 (let ((cached-value (debug-fun-%function debug-fun
)))
1221 (if (eq cached-value
:unparsed
)
1222 (setf (debug-fun-%function debug-fun
)
1223 (etypecase debug-fun
1226 (compiled-debug-fun-component debug-fun
))
1228 (sb!c
::compiled-debug-fun-start-pc
1229 (compiled-debug-fun-compiler-debug-fun debug-fun
))))
1230 (do ((entry (%code-entry-points component
)
1231 (%simple-fun-next entry
)))
1234 (sb!c
::compiled-debug-fun-start-pc
1235 (compiled-debug-fun-compiler-debug-fun
1236 (fun-debug-fun entry
))))
1238 (bogus-debug-fun nil
)))
1241 ;;; Return the name of the function represented by DEBUG-FUN. This may
1242 ;;; be a string or a cons; do not assume it is a symbol.
1243 (defun debug-fun-name (debug-fun)
1244 (declare (type debug-fun debug-fun
))
1245 (etypecase debug-fun
1247 (sb!c
::compiled-debug-fun-name
1248 (compiled-debug-fun-compiler-debug-fun debug-fun
)))
1250 (bogus-debug-fun-%name debug-fun
))))
1252 (defun interrupted-frame-error (frame)
1253 (when (and (compiled-frame-p frame
)
1254 (compiled-frame-escaped frame
)
1255 sb
!kernel
::*current-internal-error
*
1256 (array-in-bounds-p sb
!c
:*backend-internal-errors
*
1257 sb
!kernel
::*current-internal-error
*))
1258 (cdr (svref sb
!c
:*backend-internal-errors
* sb
!kernel
::*current-internal-error
*))))
1260 (defun tl-invalid-arg-count-error-p (frame)
1261 (and (eq (interrupted-frame-error frame
)
1262 'invalid-arg-count-error
)
1263 (eq (debug-fun-kind (frame-debug-fun frame
))
1266 ;; Return the name of the closure, if named, otherwise nil.
1267 (defun debug-fun-closure-name (debug-fun frame
)
1268 (when (typep debug-fun
'compiled-debug-fun
)
1269 (let* ((compiler-debug-fun
1270 (compiled-debug-fun-compiler-debug-fun debug-fun
))
1272 (sb!c
::compiled-debug-fun-closure-save compiler-debug-fun
)))
1275 (sb!impl
::closure-name
1276 #!+precise-arg-count-error
1277 (if (tl-invalid-arg-count-error-p frame
)
1278 (sub-access-debug-var-slot (frame-pointer frame
)
1280 (compiled-frame-escaped frame
))
1281 (sub-access-debug-var-slot (frame-pointer frame
)
1283 #!-precise-arg-count-error
1284 (sub-access-debug-var-slot (frame-pointer frame
)
1287 ;; The logic in CLEAN-FRAME-CALL is based on the frame name,
1288 ;; so if the simple-fun is named (XEP mumble) then the closure
1289 ;; needs to pretend to be named similarly.
1290 (let ((simple-fun-name
1291 (sb!di
:debug-fun-name debug-fun
)))
1292 (if (and (listp simple-fun-name
)
1293 (eq (car simple-fun-name
) 'sb
!c
::xep
))
1294 `(sb!c
::xep
,closure-name
)
1295 closure-name
))))))))
1297 ;;; Return a DEBUG-FUN that represents debug information for FUN.
1298 (defun fun-debug-fun (fun)
1299 (declare (type function fun
))
1300 (let ((simple-fun (%fun-fun fun
)))
1301 (let* ((name (%simple-fun-name simple-fun
))
1302 (component (fun-code-header simple-fun
))
1305 (and (sb!c
::compiled-debug-fun-p x
)
1306 (eq (sb!c
::compiled-debug-fun-name x
) name
)
1307 (eq (sb!c
::compiled-debug-fun-kind x
) nil
)))
1308 (sb!c
::compiled-debug-info-fun-map
1309 (%code-debug-info component
)))))
1311 (make-compiled-debug-fun res component
)
1312 ;; KLUDGE: comment from CMU CL:
1313 ;; This used to be the non-interpreted branch, but
1314 ;; William wrote it to return the debug-fun of fun's XEP
1315 ;; instead of fun's debug-fun. The above code does this
1316 ;; more correctly, but it doesn't get or eliminate all
1317 ;; appropriate cases. It mostly works, and probably
1318 ;; works for all named functions anyway.
1320 (debug-fun-from-pc component
1321 (* (- (fun-word-offset simple-fun
)
1322 (get-header-data component
))
1323 sb
!vm
:n-word-bytes
))))))
1325 ;;; Return the kind of the function, which is one of :OPTIONAL,
1326 ;;; :EXTERNAL, :TOPLEVEL, :CLEANUP, or NIL.
1327 (defun debug-fun-kind (debug-fun)
1328 ;; FIXME: This "is one of" information should become part of the function
1329 ;; declamation, not just a doc string
1330 (etypecase debug-fun
1332 (sb!c
::compiled-debug-fun-kind
1333 (compiled-debug-fun-compiler-debug-fun debug-fun
)))
1337 ;;; Is there any variable information for DEBUG-FUN?
1338 (defun debug-var-info-available (debug-fun)
1339 (not (not (debug-fun-debug-vars debug-fun
))))
1341 ;;; Return a list of DEBUG-VARs in DEBUG-FUN having the same name
1342 ;;; and package as SYMBOL. If SYMBOL is uninterned, then this returns
1343 ;;; a list of DEBUG-VARs without package names and with the same name
1344 ;;; as symbol. The result of this function is limited to the
1345 ;;; availability of variable information in DEBUG-FUN; for
1346 ;;; example, possibly DEBUG-FUN only knows about its arguments.
1347 (defun debug-fun-symbol-vars (debug-fun symbol
)
1348 (let ((vars (ambiguous-debug-vars debug-fun
(symbol-name symbol
)))
1349 (package (and (symbol-package symbol
)
1350 (package-name (symbol-package symbol
)))))
1351 (delete-if (if (stringp package
)
1353 (let ((p (debug-var-package-name var
)))
1354 (or (not (stringp p
))
1355 (string/= p package
))))
1357 (stringp (debug-var-package-name var
))))
1360 ;;; Return a list of DEBUG-VARs in DEBUG-FUN whose names contain
1361 ;;; NAME-PREFIX-STRING as an initial substring. The result of this
1362 ;;; function is limited to the availability of variable information in
1363 ;;; debug-fun; for example, possibly debug-fun only knows
1364 ;;; about its arguments.
1365 (defun ambiguous-debug-vars (debug-fun name-prefix-string
)
1366 (declare (simple-string name-prefix-string
))
1367 (let ((variables (debug-fun-debug-vars debug-fun
)))
1368 (declare (type (or null simple-vector
) variables
))
1370 (let* ((len (length variables
))
1371 (prefix-len (length name-prefix-string
))
1372 (pos (find-var name-prefix-string variables len
))
1375 ;; Find names from pos to variable's len that contain prefix.
1376 (do ((i pos
(1+ i
)))
1378 (let* ((var (svref variables i
))
1379 (name (debug-var-symbol-name var
))
1380 (name-len (length name
)))
1381 (declare (simple-string name
))
1382 (when (/= (or (string/= name-prefix-string name
1383 :end1 prefix-len
:end2 name-len
)
1388 (setq res
(nreverse res
)))
1391 ;;; This returns a position in VARIABLES for one containing NAME as an
1392 ;;; initial substring. END is the length of VARIABLES if supplied.
1393 (defun find-var (name variables
&optional end
)
1394 (declare (simple-vector variables
)
1395 (simple-string name
))
1396 (let ((name-len (length name
)))
1397 (position name variables
1399 (let* ((y (debug-var-symbol-name y
))
1401 (declare (simple-string y
))
1402 (and (>= y-len name-len
)
1403 (string= x y
:end1 name-len
:end2 name-len
))))
1404 :end
(or end
(length variables
)))))
1406 ;;; Return a list representing the lambda-list for DEBUG-FUN. The
1407 ;;; list has the following structure:
1408 ;;; (required-var1 required-var2
1410 ;;; (:optional var3 suppliedp-var4)
1411 ;;; (:optional var5)
1413 ;;; (:rest var6) (:rest var7)
1415 ;;; (:keyword keyword-symbol var8 suppliedp-var9)
1416 ;;; (:keyword keyword-symbol var10)
1419 ;;; Each VARi is a DEBUG-VAR; however it may be the symbol :DELETED if
1420 ;;; it is unreferenced in DEBUG-FUN. This signals a
1421 ;;; LAMBDA-LIST-UNAVAILABLE condition when there is no argument list
1423 (defun debug-fun-lambda-list (debug-fun)
1424 (etypecase debug-fun
1425 (compiled-debug-fun (compiled-debug-fun-lambda-list debug-fun
))
1426 (bogus-debug-fun nil
)))
1428 ;;; Note: If this has to compute the lambda list, it caches it in DEBUG-FUN.
1429 (defun compiled-debug-fun-lambda-list (debug-fun)
1430 (let ((lambda-list (debug-fun-%lambda-list debug-fun
)))
1431 (cond ((eq lambda-list
:unparsed
)
1432 (multiple-value-bind (args argsp
)
1433 (parse-compiled-debug-fun-lambda-list debug-fun
)
1434 (setf (debug-fun-%lambda-list debug-fun
) args
)
1437 (debug-signal 'lambda-list-unavailable
1438 :debug-fun debug-fun
))))
1440 ((bogus-debug-fun-p debug-fun
)
1442 ((sb!c
::compiled-debug-fun-arguments
1443 (compiled-debug-fun-compiler-debug-fun debug-fun
))
1444 ;; If the packed information is there (whether empty or not) as
1445 ;; opposed to being nil, then returned our cached value (nil).
1448 ;; Our cached value is nil, and the packed lambda-list information
1449 ;; is nil, so we don't have anything available.
1450 (debug-signal 'lambda-list-unavailable
1451 :debug-fun debug-fun
)))))
1453 ;;; COMPILED-DEBUG-FUN-LAMBDA-LIST calls this when a
1454 ;;; COMPILED-DEBUG-FUN has no lambda list information cached. It
1455 ;;; returns the lambda list as the first value and whether there was
1456 ;;; any argument information as the second value. Therefore,
1457 ;;; (VALUES NIL T) means there were no arguments, but (VALUES NIL NIL)
1458 ;;; means there was no argument information.
1459 (defun parse-compiled-debug-fun-lambda-list (debug-fun)
1460 (let ((args (sb!c
::compiled-debug-fun-arguments
1461 (compiled-debug-fun-compiler-debug-fun debug-fun
))))
1466 (values (coerce (debug-fun-debug-vars debug-fun
) 'list
)
1469 (let ((vars (debug-fun-debug-vars debug-fun
))
1474 (declare (type (or null simple-vector
) vars
))
1476 (when (>= i len
) (return))
1477 (let ((ele (aref args i
)))
1482 ;; Deleted required arg at beginning of args array.
1483 (push :deleted res
))
1484 (sb!c
::optional-args
1487 ;; SUPPLIED-P var immediately following keyword or
1488 ;; optional. Stick the extra var in the result
1489 ;; element representing the keyword or optional,
1490 ;; which is the previous one.
1492 ;; FIXME: NCONC used for side-effect: the effect is defined,
1493 ;; but this is bad style no matter what.
1495 (list (compiled-debug-fun-lambda-list-var
1496 args
(incf i
) vars
))))
1499 (compiled-debug-fun-lambda-list-var
1500 args
(incf i
) vars
))
1503 ;; The next two args are the &MORE arg context and count.
1505 (compiled-debug-fun-lambda-list-var
1507 (compiled-debug-fun-lambda-list-var
1508 args
(incf i
) vars
))
1512 (push (list :keyword
1514 (compiled-debug-fun-lambda-list-var
1515 args
(incf i
) vars
))
1518 ;; We saw an optional marker, so the following
1519 ;; non-symbols are indexes indicating optional
1521 (push (list :optional
(svref vars ele
)) res
))
1523 ;; Required arg at beginning of args array.
1524 (push (svref vars ele
) res
))))
1526 (values (nreverse res
) t
))))))
1528 ;;; This is used in COMPILED-DEBUG-FUN-LAMBDA-LIST.
1529 (defun compiled-debug-fun-lambda-list-var (args i vars
)
1530 (declare (type (simple-array * (*)) args
)
1531 (simple-vector vars
))
1532 (let ((ele (aref args i
)))
1533 (cond ((not (symbolp ele
)) (svref vars ele
))
1534 ((eq ele
'sb
!c
::deleted
) :deleted
)
1535 (t (error "malformed arguments description")))))
1537 (defun compiled-debug-fun-debug-info (debug-fun)
1538 (%code-debug-info
(compiled-debug-fun-component debug-fun
)))
1540 ;;;; unpacking variable and basic block data
1542 (defvar *parsing-buffer
*
1543 (make-array 20 :adjustable t
:fill-pointer t
))
1544 (defvar *other-parsing-buffer
*
1545 (make-array 20 :adjustable t
:fill-pointer t
))
1546 ;;; PARSE-DEBUG-BLOCKS and PARSE-DEBUG-VARS
1547 ;;; use this to unpack binary encoded information. It returns the
1548 ;;; values returned by the last form in body.
1550 ;;; This binds buffer-var to *parsing-buffer*, makes sure it starts at
1551 ;;; element zero, and makes sure if we unwind, we nil out any set
1552 ;;; elements for GC purposes.
1554 ;;; This also binds other-var to *other-parsing-buffer* when it is
1555 ;;; supplied, making sure it starts at element zero and that we nil
1556 ;;; out any elements if we unwind.
1558 ;;; This defines the local macro RESULT that takes a buffer, copies
1559 ;;; its elements to a resulting simple-vector, nil's out elements, and
1560 ;;; restarts the buffer at element zero. RESULT returns the
1562 (eval-when (:compile-toplevel
:execute
)
1563 (sb!xc
:defmacro with-parsing-buffer
((buffer-var &optional other-var
)
1565 (let ((len (gensym))
1568 (let ((,buffer-var
*parsing-buffer
*)
1569 ,@(if other-var
`((,other-var
*other-parsing-buffer
*))))
1570 (setf (fill-pointer ,buffer-var
) 0)
1571 ,@(if other-var
`((setf (fill-pointer ,other-var
) 0)))
1572 (macrolet ((result (buf)
1573 `(let* ((,',len
(length ,buf
))
1574 (,',res
(make-array ,',len
)))
1575 (replace ,',res
,buf
:end1
,',len
:end2
,',len
)
1576 (fill ,buf nil
:end
,',len
)
1577 (setf (fill-pointer ,buf
) 0)
1580 (fill *parsing-buffer
* nil
)
1581 ,@(if other-var
`((fill *other-parsing-buffer
* nil
))))))
1584 ;;; The argument is a debug internals structure. This returns the
1585 ;;; DEBUG-BLOCKs for DEBUG-FUN, regardless of whether we have unpacked
1586 ;;; them yet. It signals a NO-DEBUG-BLOCKS condition if it can't
1587 ;;; return the blocks.
1588 (defun debug-fun-debug-blocks (debug-fun)
1589 (let ((blocks (debug-fun-blocks debug-fun
)))
1590 (cond ((eq blocks
:unparsed
)
1591 (setf (debug-fun-blocks debug-fun
)
1592 (parse-debug-blocks debug-fun
))
1593 (unless (debug-fun-blocks debug-fun
)
1594 (debug-signal 'no-debug-blocks
1595 :debug-fun debug-fun
))
1596 (debug-fun-blocks debug-fun
))
1599 (debug-signal 'no-debug-blocks
1600 :debug-fun debug-fun
)))))
1602 ;;; Return a SIMPLE-VECTOR of DEBUG-BLOCKs or NIL. NIL indicates there
1603 ;;; was no basic block information.
1604 (defun parse-debug-blocks (debug-fun)
1605 (etypecase debug-fun
1607 (parse-compiled-debug-blocks debug-fun
))
1609 (debug-signal 'no-debug-blocks
:debug-fun debug-fun
))))
1611 ;;; This does some of the work of PARSE-DEBUG-BLOCKS.
1612 (defun parse-compiled-debug-blocks (debug-fun)
1613 (let* ((var-count (length (debug-fun-debug-vars debug-fun
)))
1614 (compiler-debug-fun (compiled-debug-fun-compiler-debug-fun
1616 (blocks (sb!c
::compiled-debug-fun-blocks compiler-debug-fun
))
1617 ;; KLUDGE: 8 is a hard-wired constant in the compiler for the
1618 ;; element size of the packed binary representation of the
1620 (live-set-len (ceiling var-count
8))
1621 (tlf-number (sb!c
::compiled-debug-fun-tlf-number compiler-debug-fun
))
1622 (elsewhere-pc (sb!c
::compiled-debug-fun-elsewhere-pc compiler-debug-fun
)))
1624 (return-from parse-compiled-debug-blocks nil
))
1625 (macrolet ((aref+ (a i
) `(prog1 (aref ,a
,i
) (incf ,i
))))
1626 (with-parsing-buffer (blocks-buffer locations-buffer
)
1628 (len (length blocks
))
1631 (when (>= i len
) (return))
1632 (let ((block (make-compiled-debug-block)))
1633 (dotimes (k (sb!c
:read-var-integer blocks i
))
1634 (let ((kind (svref sb
!c
::*compiled-code-location-kinds
*
1637 (sb!c
:read-var-integer blocks i
)))
1638 (tlf-offset (or tlf-number
1639 (sb!c
:read-var-integer blocks i
)))
1640 (form-number (sb!c
:read-var-integer blocks i
))
1641 (live-set (sb!c
:read-packed-bit-vector
1642 live-set-len blocks i
))
1643 (step-info (sb!c
:read-var-string blocks i
)))
1644 (vector-push-extend (make-known-code-location
1645 pc debug-fun block tlf-offset
1646 form-number live-set kind
1650 (setf (compiled-debug-block-code-locations block
)
1651 (result locations-buffer
)
1652 (compiled-debug-block-elsewhere-p block
)
1653 (> last-pc elsewhere-pc
))
1654 (vector-push-extend block blocks-buffer
))))
1655 (result blocks-buffer
)))))
1657 ;;; The argument is a debug internals structure. This returns NIL if
1658 ;;; there is no variable information. It returns an empty
1659 ;;; simple-vector if there were no locals in the function. Otherwise
1660 ;;; it returns a SIMPLE-VECTOR of DEBUG-VARs.
1661 (defun debug-fun-debug-vars (debug-fun)
1662 (let ((vars (debug-fun-%debug-vars debug-fun
)))
1663 (if (eq vars
:unparsed
)
1664 (setf (debug-fun-%debug-vars debug-fun
)
1665 (etypecase debug-fun
1667 (parse-compiled-debug-vars debug-fun
))
1668 (bogus-debug-fun nil
)))
1671 ;;; VARS is the parsed variables for a minimal debug function. We need
1672 ;;; to assign names of the form ARG-NNN. We must pad with leading
1673 ;;; zeros, since the arguments must be in alphabetical order.
1674 (defun assign-minimal-var-names (vars)
1675 (declare (simple-vector vars
))
1676 (let* ((len (length vars
))
1677 (width (length (format nil
"~W" (1- len
)))))
1679 (without-package-locks
1680 (setf (compiled-debug-var-symbol (svref vars i
))
1681 (intern (format nil
"ARG-~V,'0D" width i
)
1682 ;; The cross-compiler won't dump literal package
1683 ;; references because the target package objects
1684 ;; aren't created until partway through
1685 ;; cold-init. In lieu of adding smarts to the
1686 ;; build framework to handle this, we use an
1687 ;; explicit load-time-value form.
1688 (load-time-value (find-package "SB!DEBUG"))))))))
1690 ;;; Parse the packed representation of DEBUG-VARs from
1691 ;;; DEBUG-FUN's SB!C::COMPILED-DEBUG-FUN, returning a vector
1692 ;;; of DEBUG-VARs, or NIL if there was no information to parse.
1693 (defun parse-compiled-debug-vars (debug-fun)
1694 (let* ((cdebug-fun (compiled-debug-fun-compiler-debug-fun
1696 (packed-vars (sb!c
::compiled-debug-fun-vars cdebug-fun
))
1697 (args-minimal (eq (sb!c
::compiled-debug-fun-arguments cdebug-fun
)
1701 (buffer (make-array 0 :fill-pointer
0 :adjustable t
)))
1702 ((>= i
(length packed-vars
))
1703 (let ((result (coerce buffer
'simple-vector
)))
1705 (assign-minimal-var-names result
))
1707 (flet ((geti () (prog1 (aref packed-vars i
) (incf i
))))
1708 (let* ((flags (geti))
1709 (minimal (logtest sb
!c
::compiled-debug-var-minimal-p flags
))
1710 (deleted (logtest sb
!c
::compiled-debug-var-deleted-p flags
))
1711 (more-context-p (logtest sb
!c
::compiled-debug-var-more-context-p flags
))
1712 (more-count-p (logtest sb
!c
::compiled-debug-var-more-count-p flags
))
1713 (indirect-p (logtest sb
!c
::compiled-debug-var-indirect-p flags
))
1714 (live (logtest sb
!c
::compiled-debug-var-environment-live
1716 (save (logtest sb
!c
::compiled-debug-var-save-loc-p flags
))
1717 (symbol (if minimal nil
(geti)))
1718 (id (if (logtest sb
!c
::compiled-debug-var-id-p flags
)
1721 (sc-offset (if deleted
0 (geti)))
1722 (save-sc-offset (and save
(geti)))
1723 (indirect-sc-offset (and indirect-p
1725 (aver (not (and args-minimal
(not minimal
))))
1726 (vector-push-extend (make-compiled-debug-var symbol
1732 (cond (more-context-p :more-context
)
1733 (more-count-p :more-count
)))
1738 ;;; If we're sure of whether code-location is known, return T or NIL.
1739 ;;; If we're :UNSURE, then try to fill in the code-location's slots.
1740 ;;; This determines whether there is any debug-block information, and
1741 ;;; if code-location is known.
1743 ;;; ??? IF this conses closures every time it's called, then break off the
1744 ;;; :UNSURE part to get the HANDLER-CASE into another function.
1745 (defun code-location-unknown-p (basic-code-location)
1746 (ecase (code-location-%unknown-p basic-code-location
)
1750 (setf (code-location-%unknown-p basic-code-location
)
1751 (handler-case (not (fill-in-code-location basic-code-location
))
1752 (no-debug-blocks () t
))))))
1754 ;;; Return the DEBUG-BLOCK containing code-location if it is available.
1755 ;;; Some debug policies inhibit debug-block information, and if none
1756 ;;; is available, then this signals a NO-DEBUG-BLOCKS condition.
1757 (defun code-location-debug-block (basic-code-location)
1758 (let ((block (code-location-%debug-block basic-code-location
)))
1759 (if (eq block
:unparsed
)
1760 (etypecase basic-code-location
1761 (compiled-code-location
1762 (compute-compiled-code-location-debug-block basic-code-location
))
1763 ;; (There used to be more cases back before sbcl-0.7.0, when
1764 ;; we did special tricks to debug the IR1 interpreter.)
1768 ;;; Store and return BASIC-CODE-LOCATION's debug-block. We determines
1769 ;;; the correct one using the code-location's pc. We use
1770 ;;; DEBUG-FUN-DEBUG-BLOCKS to return the cached block information
1771 ;;; or signal a NO-DEBUG-BLOCKS condition. The blocks are sorted by
1772 ;;; their first code-location's pc, in ascending order. Therefore, as
1773 ;;; soon as we find a block that starts with a pc greater than
1774 ;;; basic-code-location's pc, we know the previous block contains the
1775 ;;; pc. If we get to the last block, then the code-location is either
1776 ;;; in the second to last block or the last block, and we have to be
1777 ;;; careful in determining this since the last block could be code at
1778 ;;; the end of the function. We have to check for the last block being
1779 ;;; code first in order to see how to compare the code-location's pc.
1780 (defun compute-compiled-code-location-debug-block (basic-code-location)
1781 (let* ((pc (compiled-code-location-pc basic-code-location
))
1782 (debug-fun (code-location-debug-fun
1783 basic-code-location
))
1784 (blocks (debug-fun-debug-blocks debug-fun
))
1785 (len (length blocks
)))
1786 (declare (simple-vector blocks
))
1787 (setf (code-location-%debug-block basic-code-location
)
1793 (let ((last (svref blocks end
)))
1795 ((debug-block-elsewhere-p last
)
1797 (sb!c
::compiled-debug-fun-elsewhere-pc
1798 (compiled-debug-fun-compiler-debug-fun
1800 (svref blocks
(1- end
))
1803 (compiled-code-location-pc
1804 (svref (compiled-debug-block-code-locations last
)
1806 (svref blocks
(1- end
)))
1808 (declare (type index i end
))
1810 (compiled-code-location-pc
1811 (svref (compiled-debug-block-code-locations
1814 (return (svref blocks
(1- i
)))))))))
1816 ;;; Return the CODE-LOCATION's DEBUG-SOURCE.
1817 (defun code-location-debug-source (code-location)
1818 (let ((info (compiled-debug-fun-debug-info
1819 (code-location-debug-fun code-location
))))
1820 (or (sb!c
::debug-info-source info
)
1821 (debug-signal 'no-debug-blocks
:debug-fun
1822 (code-location-debug-fun code-location
)))))
1824 ;;; Returns the number of top level forms before the one containing
1825 ;;; CODE-LOCATION as seen by the compiler in some compilation unit. (A
1826 ;;; compilation unit is not necessarily a single file, see the section
1827 ;;; on debug-sources.)
1828 (defun code-location-toplevel-form-offset (code-location)
1829 (when (code-location-unknown-p code-location
)
1830 (error 'unknown-code-location
:code-location code-location
))
1831 (let ((tlf-offset (code-location-%tlf-offset code-location
)))
1832 (cond ((eq tlf-offset
:unparsed
)
1833 (etypecase code-location
1834 (compiled-code-location
1835 (unless (fill-in-code-location code-location
)
1836 ;; This check should be unnecessary. We're missing
1837 ;; debug info the compiler should have dumped.
1838 (bug "unknown code location"))
1839 (code-location-%tlf-offset code-location
))
1840 ;; (There used to be more cases back before sbcl-0.7.0,,
1841 ;; when we did special tricks to debug the IR1
1846 ;;; Return the number of the form corresponding to CODE-LOCATION. The
1847 ;;; form number is derived by a walking the subforms of a top level
1848 ;;; form in depth-first order.
1849 (defun code-location-form-number (code-location)
1850 (when (code-location-unknown-p code-location
)
1851 (error 'unknown-code-location
:code-location code-location
))
1852 (let ((form-num (code-location-%form-number code-location
)))
1853 (cond ((eq form-num
:unparsed
)
1854 (etypecase code-location
1855 (compiled-code-location
1856 (unless (fill-in-code-location code-location
)
1857 ;; This check should be unnecessary. We're missing
1858 ;; debug info the compiler should have dumped.
1859 (bug "unknown code location"))
1860 (code-location-%form-number code-location
))
1861 ;; (There used to be more cases back before sbcl-0.7.0,,
1862 ;; when we did special tricks to debug the IR1
1867 ;;; Return the kind of CODE-LOCATION, one of:
1868 ;;; :INTERPRETED, :UNKNOWN-RETURN, :KNOWN-RETURN, :INTERNAL-ERROR,
1869 ;;; :NON-LOCAL-EXIT, :BLOCK-START, :CALL-SITE, :SINGLE-VALUE-RETURN,
1870 ;;; :NON-LOCAL-ENTRY
1871 (defun code-location-kind (code-location)
1872 (when (code-location-unknown-p code-location
)
1873 (error 'unknown-code-location
:code-location code-location
))
1874 (etypecase code-location
1875 (compiled-code-location
1876 (let ((kind (compiled-code-location-kind code-location
)))
1877 (cond ((not (eq kind
:unparsed
)) kind
)
1878 ((not (fill-in-code-location code-location
))
1879 ;; This check should be unnecessary. We're missing
1880 ;; debug info the compiler should have dumped.
1881 (bug "unknown code location"))
1883 (compiled-code-location-kind code-location
)))))
1884 ;; (There used to be more cases back before sbcl-0.7.0,,
1885 ;; when we did special tricks to debug the IR1
1889 ;;; This returns CODE-LOCATION's live-set if it is available. If
1890 ;;; there is no debug-block information, this returns NIL.
1891 (defun compiled-code-location-live-set (code-location)
1892 (if (code-location-unknown-p code-location
)
1894 (let ((live-set (compiled-code-location-%live-set code-location
)))
1895 (cond ((eq live-set
:unparsed
)
1896 (unless (fill-in-code-location code-location
)
1897 ;; This check should be unnecessary. We're missing
1898 ;; debug info the compiler should have dumped.
1900 ;; FIXME: This error and comment happen over and over again.
1901 ;; Make them a shared function.
1902 (bug "unknown code location"))
1903 (compiled-code-location-%live-set code-location
))
1906 ;;; true if OBJ1 and OBJ2 are the same place in the code
1907 (defun code-location= (obj1 obj2
)
1909 (compiled-code-location
1911 (compiled-code-location
1912 (and (eq (code-location-debug-fun obj1
)
1913 (code-location-debug-fun obj2
))
1914 (sub-compiled-code-location= obj1 obj2
)))
1915 ;; (There used to be more cases back before sbcl-0.7.0,,
1916 ;; when we did special tricks to debug the IR1
1919 ;; (There used to be more cases back before sbcl-0.7.0,,
1920 ;; when we did special tricks to debug IR1-interpreted code.)
1922 (defun sub-compiled-code-location= (obj1 obj2
)
1923 (= (compiled-code-location-pc obj1
)
1924 (compiled-code-location-pc obj2
)))
1926 ;;; Fill in CODE-LOCATION's :UNPARSED slots, returning T or NIL
1927 ;;; depending on whether the code-location was known in its
1928 ;;; DEBUG-FUN's debug-block information. This may signal a
1929 ;;; NO-DEBUG-BLOCKS condition due to DEBUG-FUN-DEBUG-BLOCKS, and
1930 ;;; it assumes the %UNKNOWN-P slot is already set or going to be set.
1931 (defun fill-in-code-location (code-location)
1932 (declare (type compiled-code-location code-location
))
1933 (let* ((debug-fun (code-location-debug-fun code-location
))
1934 (blocks (debug-fun-debug-blocks debug-fun
)))
1935 (declare (simple-vector blocks
))
1936 (dotimes (i (length blocks
) nil
)
1937 (let* ((block (svref blocks i
))
1938 (locations (compiled-debug-block-code-locations block
)))
1939 (declare (simple-vector locations
))
1940 (dotimes (j (length locations
))
1941 (let ((loc (svref locations j
)))
1942 (when (sub-compiled-code-location= code-location loc
)
1943 (setf (code-location-%debug-block code-location
) block
)
1944 (setf (code-location-%tlf-offset code-location
)
1945 (code-location-%tlf-offset loc
))
1946 (setf (code-location-%form-number code-location
)
1947 (code-location-%form-number loc
))
1948 (setf (compiled-code-location-%live-set code-location
)
1949 (compiled-code-location-%live-set loc
))
1950 (setf (compiled-code-location-kind code-location
)
1951 (compiled-code-location-kind loc
))
1952 (setf (compiled-code-location-step-info code-location
)
1953 (compiled-code-location-step-info loc
))
1954 (return-from fill-in-code-location t
))))))))
1956 ;;;; operations on DEBUG-BLOCKs
1958 ;;; Execute FORMS in a context with CODE-VAR bound to each
1959 ;;; CODE-LOCATION in DEBUG-BLOCK, and return the value of RESULT.
1960 (defmacro do-debug-block-locations
((code-var debug-block
&optional result
)
1962 (let ((code-locations (gensym))
1964 `(let ((,code-locations
(debug-block-code-locations ,debug-block
)))
1965 (declare (simple-vector ,code-locations
))
1966 (dotimes (,i
(length ,code-locations
) ,result
)
1967 (let ((,code-var
(svref ,code-locations
,i
)))
1970 ;;; Return the name of the function represented by DEBUG-FUN.
1971 ;;; This may be a string or a cons; do not assume it is a symbol.
1972 (defun debug-block-fun-name (debug-block)
1973 (etypecase debug-block
1974 (compiled-debug-block
1975 (let ((code-locs (compiled-debug-block-code-locations debug-block
)))
1976 (declare (simple-vector code-locs
))
1977 (if (zerop (length code-locs
))
1978 "??? Can't get name of debug-block's function."
1980 (code-location-debug-fun (svref code-locs
0))))))
1981 ;; (There used to be more cases back before sbcl-0.7.0, when we
1982 ;; did special tricks to debug the IR1 interpreter.)
1985 (defun debug-block-code-locations (debug-block)
1986 (etypecase debug-block
1987 (compiled-debug-block
1988 (compiled-debug-block-code-locations debug-block
))
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 ;;;; operations on debug variables
1995 (defun debug-var-symbol-name (debug-var)
1996 (symbol-name (debug-var-symbol debug-var
)))
1998 ;;; FIXME: Make sure that this isn't called anywhere that it wouldn't
1999 ;;; be acceptable to have NIL returned, or that it's only called on
2000 ;;; DEBUG-VARs whose symbols have non-NIL packages.
2001 (defun debug-var-package-name (debug-var)
2002 (package-name (symbol-package (debug-var-symbol debug-var
))))
2004 ;;; Return the value stored for DEBUG-VAR in frame, or if the value is
2005 ;;; not :VALID, then signal an INVALID-VALUE error.
2006 (defun debug-var-valid-value (debug-var frame
)
2007 (unless (eq (debug-var-validity debug-var
(frame-code-location frame
))
2009 (error 'invalid-value
:debug-var debug-var
:frame frame
))
2010 (debug-var-value debug-var frame
))
2012 ;;; Returns the value stored for DEBUG-VAR in frame. The value may be
2013 ;;; invalid. This is SETFable.
2014 (defun debug-var-value (debug-var frame
)
2015 (aver (typep frame
'compiled-frame
))
2016 (let ((res (access-compiled-debug-var-slot debug-var frame
)))
2017 (if (indirect-value-cell-p res
)
2018 (value-cell-ref res
)
2021 ;;; This returns what is stored for the variable represented by
2022 ;;; DEBUG-VAR relative to the FRAME. This may be an indirect value
2023 ;;; cell if the variable is both closed over and set.
2024 (defun access-compiled-debug-var-slot (debug-var frame
)
2025 (let ((escaped (compiled-frame-escaped frame
)))
2026 (cond ((compiled-debug-var-indirect-sc-offset debug-var
)
2027 (sub-access-debug-var-slot
2028 ;; Indirect are accessed through a frame pointer of the parent.
2030 (sub-access-debug-var-slot
2031 (frame-pointer frame
)
2033 (compiled-debug-var-sc-offset debug-var
)
2035 (compiled-debug-var-save-sc-offset debug-var
)
2036 (compiled-debug-var-sc-offset debug-var
)))
2038 (compiled-debug-var-indirect-sc-offset debug-var
)
2041 (sub-access-debug-var-slot
2042 (frame-pointer frame
)
2043 (compiled-debug-var-sc-offset debug-var
)
2046 (sub-access-debug-var-slot
2047 (frame-pointer frame
)
2048 (or (compiled-debug-var-save-sc-offset debug-var
)
2049 (compiled-debug-var-sc-offset debug-var
)))))))
2051 ;;; a helper function for working with possibly-invalid values:
2052 ;;; Do (%MAKE-LISP-OBJ VAL) only if the value looks valid.
2054 ;;; (Such values can arise in registers on machines with conservative
2055 ;;; GC, and might also arise in debug variable locations when
2056 ;;; those variables are invalid.)
2058 ;;; NOTE: this function is not GC-safe in the slightest when creating
2059 ;;; a pointer to an object in dynamic space. If a GC occurs between
2060 ;;; the start of the call to VALID-LISP-POINTER-P and the end of
2061 ;;; %MAKE-LISP-OBJ then the object could move before the boxed pointer
2062 ;;; is constructed. This can happen on CHENEYGC if an asynchronous
2063 ;;; interrupt occurs within the window. This can happen on GENCGC
2064 ;;; under the same circumstances, but is more likely due to all GENCGC
2065 ;;; platforms supporting threaded operation. This is somewhat
2066 ;;; mitigated on x86oids due to the conservative stack and interrupt
2067 ;;; context "scavenging" on such platforms, but there still may be a
2068 ;;; vulnerable window.
2069 (defun make-lisp-obj (val &optional
(errorp t
))
2072 (zerop (logand val sb
!vm
:fixnum-tag-mask
))
2073 ;; immediate single float, 64-bit only
2074 #!+#.
(cl:if
(cl:= sb
!vm
::n-machine-word-bits
64) '(and) '(or))
2075 (= (logand val
#xff
) sb
!vm
:single-float-widetag
)
2077 (and (zerop (logandc2 val
#x1fffffff
)) ; Top bits zero
2078 (= (logand val
#xff
) sb
!vm
:character-widetag
)) ; char tag
2080 (= val sb
!vm
:unbound-marker-widetag
)
2081 ;; undefined_tramp doesn't validate properly as a pointer, and
2082 ;; the actual value can vary by backend (x86oids need not
2084 #!+(or alpha hppa mips ppc
)
2085 (= val
(+ (- (foreign-symbol-address "undefined_tramp")
2086 (* sb
!vm
:n-word-bytes sb
!vm
:simple-fun-code-offset
))
2087 sb
!vm
:fun-pointer-lowtag
))
2089 (= val
(foreign-symbol-address "undefined_tramp"))
2091 (not (zerop (valid-lisp-pointer-p (int-sap val
)))))
2092 (values (%make-lisp-obj val
) t
)
2094 (error "~S is not a valid argument to ~S"
2096 (values (make-unprintable-object (format nil
"invalid object #x~X" val
))
2099 (defun sub-access-debug-var-slot (fp sc-offset
&optional escaped
)
2100 ;; NOTE: The long-float support in here is obviously decayed. When
2101 ;; the x86oid and non-x86oid versions of this function were unified,
2102 ;; the behavior of long-floats was preserved, which only served to
2103 ;; highlight its brokenness.
2104 (macrolet ((with-escaped-value ((var) &body forms
)
2106 (let ((,var
(sb!vm
:context-register
2108 (sb!c
:sc-offset-offset sc-offset
))))
2110 :invalid-value-for-unescaped-register-storage
))
2111 (escaped-float-value (format)
2113 (sb!vm
:context-float-register
2115 (sb!c
:sc-offset-offset sc-offset
)
2117 :invalid-value-for-unescaped-register-storage
))
2118 (escaped-complex-float-value (format offset
)
2121 (sb!vm
:context-float-register
2122 escaped
(sb!c
:sc-offset-offset sc-offset
) ',format
)
2123 (sb!vm
:context-float-register
2124 escaped
(+ (sb!c
:sc-offset-offset sc-offset
) ,offset
) ',format
))
2125 :invalid-value-for-unescaped-register-storage
))
2126 (with-nfp ((var) &body body
)
2127 ;; x86oids have no separate number stack, so dummy it
2133 `(let ((,var
(if escaped
2135 (sb!vm
:context-register escaped
2138 (sap-ref-sap fp
(* nfp-save-offset
2139 sb
!vm
:n-word-bytes
))
2141 (sb!vm
::make-number-stack-pointer
2142 (sap-ref-32 fp
(* nfp-save-offset
2143 sb
!vm
:n-word-bytes
))))))
2145 (stack-frame-offset (data-width offset
)
2147 `(sb!vm
::frame-byte-offset
(+ (sb!c
:sc-offset-offset sc-offset
)
2151 (declare (ignore data-width
))
2153 `(* (+ (sb!c
:sc-offset-offset sc-offset
) ,offset
)
2154 sb
!vm
:n-word-bytes
)))
2155 (ecase (sb!c
:sc-offset-scn sc-offset
)
2156 ((#.sb
!vm
:any-reg-sc-number
2157 #.sb
!vm
:descriptor-reg-sc-number
2158 #!+rt
#.sb
!vm
:word-pointer-reg-sc-number
)
2160 (with-escaped-value (val)
2161 (values (make-lisp-obj val nil
)))))
2162 (#.sb
!vm
:character-reg-sc-number
2163 (with-escaped-value (val)
2165 (#.sb
!vm
:sap-reg-sc-number
2166 (with-escaped-value (val)
2168 (#.sb
!vm
:signed-reg-sc-number
2169 (with-escaped-value (val)
2170 (if (logbitp (1- sb
!vm
:n-word-bits
) val
)
2171 (logior val
(ash -
1 sb
!vm
:n-word-bits
))
2173 (#.sb
!vm
:unsigned-reg-sc-number
2174 (with-escaped-value (val)
2177 (#.sb
!vm
:non-descriptor-reg-sc-number
2178 (error "Local non-descriptor register access?"))
2180 (#.sb
!vm
:interior-reg-sc-number
2181 (error "Local interior register access?"))
2182 (#.sb
!vm
:single-reg-sc-number
2183 (escaped-float-value single-float
))
2184 (#.sb
!vm
:double-reg-sc-number
2185 (escaped-float-value double-float
))
2187 (#.sb
!vm
:long-reg-sc-number
2188 (escaped-float-value long-float
))
2189 (#.sb
!vm
:complex-single-reg-sc-number
2190 (escaped-complex-float-value single-float
1))
2191 (#.sb
!vm
:complex-double-reg-sc-number
2192 (escaped-complex-float-value double-float
#!+sparc
2 #!-sparc
1))
2194 (#.sb
!vm
:complex-long-reg-sc-number
2195 (escaped-complex-float-value long-float
2196 #!+sparc
4 #!+(or x86 x86-64
) 1
2197 #!-
(or sparc x86 x86-64
) 0))
2198 (#.sb
!vm
:single-stack-sc-number
2200 (sap-ref-single nfp
(stack-frame-offset 1 0))))
2201 (#.sb
!vm
:double-stack-sc-number
2203 (sap-ref-double nfp
(stack-frame-offset 2 0))))
2205 (#.sb
!vm
:long-stack-sc-number
2207 (sap-ref-long nfp
(stack-frame-offset 3 0))))
2208 (#.sb
!vm
:complex-single-stack-sc-number
2211 (sap-ref-single nfp
(stack-frame-offset 1 0))
2212 (sap-ref-single nfp
(stack-frame-offset 1 1)))))
2213 (#.sb
!vm
:complex-double-stack-sc-number
2216 (sap-ref-double nfp
(stack-frame-offset 2 0))
2217 (sap-ref-double nfp
(stack-frame-offset 2 2)))))
2219 (#.sb
!vm
:complex-long-stack-sc-number
2222 (sap-ref-long nfp
(stack-frame-offset 3 0))
2224 (stack-frame-offset 3 #!+sparc
4
2225 #!+(or x86 x86-64
) 3
2226 #!-
(or sparc x86 x86-64
) 0)))))
2227 (#.sb
!vm
:control-stack-sc-number
2228 (stack-ref fp
(sb!c
:sc-offset-offset sc-offset
)))
2229 (#.sb
!vm
:character-stack-sc-number
2231 (code-char (sap-ref-word nfp
(stack-frame-offset 1 0)))))
2232 (#.sb
!vm
:unsigned-stack-sc-number
2234 (sap-ref-word nfp
(stack-frame-offset 1 0))))
2235 (#.sb
!vm
:signed-stack-sc-number
2237 (signed-sap-ref-word nfp
(stack-frame-offset 1 0))))
2238 (#.sb
!vm
:sap-stack-sc-number
2240 (sap-ref-sap nfp
(stack-frame-offset 1 0)))))))
2242 ;;; This stores value as the value of DEBUG-VAR in FRAME. In the
2243 ;;; COMPILED-DEBUG-VAR case, access the current value to determine if
2244 ;;; it is an indirect value cell. This occurs when the variable is
2245 ;;; both closed over and set.
2246 (defun %set-debug-var-value
(debug-var frame new-value
)
2247 (aver (typep frame
'compiled-frame
))
2248 (let ((old-value (access-compiled-debug-var-slot debug-var frame
)))
2249 (if (indirect-value-cell-p old-value
)
2250 (value-cell-set old-value new-value
)
2251 (set-compiled-debug-var-slot debug-var frame new-value
)))
2254 ;;; This stores VALUE for the variable represented by debug-var
2255 ;;; relative to the frame. This assumes the location directly contains
2256 ;;; the variable's value; that is, there is no indirect value cell
2257 ;;; currently there in case the variable is both closed over and set.
2258 (defun set-compiled-debug-var-slot (debug-var frame value
)
2259 (let ((escaped (compiled-frame-escaped frame
)))
2261 (sub-set-debug-var-slot (frame-pointer frame
)
2262 (compiled-debug-var-sc-offset debug-var
)
2264 (sub-set-debug-var-slot
2265 (frame-pointer frame
)
2266 (or (compiled-debug-var-save-sc-offset debug-var
)
2267 (compiled-debug-var-sc-offset debug-var
))
2270 (defun sub-set-debug-var-slot (fp sc-offset value
&optional escaped
)
2271 ;; Like sub-access-debug-var-slot, this is the unification of two
2272 ;; divergent copy-pasted functions. The astute reviewer will notice
2273 ;; that long-floats are messed up here as well, that x86oids
2274 ;; apparently don't support accessing float values that are in
2275 ;; registers, and that non-x86oids store the real part of a float
2276 ;; for both the real and imaginary parts of a complex on the stack
2277 ;; (but not in registers, oddly enough). Some research has
2278 ;; indicated that the different forms of THE used for validating the
2279 ;; type of complex float components between x86oid and non-x86oid
2280 ;; systems are only significant in the case of using a non-complex
2281 ;; number as input (as the non-x86oid case effectively converts
2282 ;; non-complex numbers to complex ones and the x86oid case will
2283 ;; error out). That said, the error message from entering a value
2284 ;; of the wrong type will be slightly easier to understand on x86oid
2286 (macrolet ((set-escaped-value (val)
2288 (setf (sb!vm
:context-register
2290 (sb!c
:sc-offset-offset sc-offset
))
2293 (set-escaped-float-value (format val
)
2295 (setf (sb!vm
:context-float-register
2297 (sb!c
:sc-offset-offset sc-offset
)
2301 (set-escaped-complex-float-value (format offset val
)
2304 (setf (sb!vm
:context-float-register
2305 escaped
(sb!c
:sc-offset-offset sc-offset
) ',format
)
2307 (setf (sb!vm
:context-float-register
2308 escaped
(+ (sb!c
:sc-offset-offset sc-offset
) ,offset
)
2312 (with-nfp ((var) &body body
)
2313 ;; x86oids have no separate number stack, so dummy it
2319 `(let ((,var
(if escaped
2321 (sb!vm
:context-register escaped
2326 sb
!vm
:n-word-bytes
))
2328 (sb!vm
::make-number-stack-pointer
2331 sb
!vm
:n-word-bytes
))))))
2333 (stack-frame-offset (data-width offset
)
2335 `(sb!vm
::frame-byte-offset
(+ (sb!c
:sc-offset-offset sc-offset
)
2339 (declare (ignore data-width
))
2341 `(* (+ (sb!c
:sc-offset-offset sc-offset
) ,offset
)
2342 sb
!vm
:n-word-bytes
)))
2343 (ecase (sb!c
:sc-offset-scn sc-offset
)
2344 ((#.sb
!vm
:any-reg-sc-number
2345 #.sb
!vm
:descriptor-reg-sc-number
2346 #!+rt
#.sb
!vm
:word-pointer-reg-sc-number
)
2349 (get-lisp-obj-address value
))))
2350 (#.sb
!vm
:character-reg-sc-number
2351 (set-escaped-value (char-code value
)))
2352 (#.sb
!vm
:sap-reg-sc-number
2353 (set-escaped-value (sap-int value
)))
2354 (#.sb
!vm
:signed-reg-sc-number
2355 (set-escaped-value (logand value
(1- (ash 1 sb
!vm
:n-word-bits
)))))
2356 (#.sb
!vm
:unsigned-reg-sc-number
2357 (set-escaped-value value
))
2359 (#.sb
!vm
:non-descriptor-reg-sc-number
2360 (error "Local non-descriptor register access?"))
2362 (#.sb
!vm
:interior-reg-sc-number
2363 (error "Local interior register access?"))
2364 (#.sb
!vm
:single-reg-sc-number
2365 #!-
(or x86 x86-64
) ;; don't have escaped floats.
2366 (set-escaped-float-value single-float value
))
2367 (#.sb
!vm
:double-reg-sc-number
2368 #!-
(or x86 x86-64
) ;; don't have escaped floats -- still in npx?
2369 (set-escaped-float-value double-float value
))
2371 (#.sb
!vm
:long-reg-sc-number
2372 #!-
(or x86 x86-64
) ;; don't have escaped floats -- still in npx?
2373 (set-escaped-float-value long-float value
))
2375 (#.sb
!vm
:complex-single-reg-sc-number
2376 (set-escaped-complex-float-value single-float
1 value
))
2378 (#.sb
!vm
:complex-double-reg-sc-number
2379 (set-escaped-complex-float-value double-float
#!+sparc
2 #!-sparc
1 value
))
2380 #!+(and long-float
(not (or x86 x86-64
)))
2381 (#.sb
!vm
:complex-long-reg-sc-number
2382 (set-escaped-complex-float-value long-float
#!+sparc
4 #!-sparc
0 value
))
2383 (#.sb
!vm
:single-stack-sc-number
2385 (setf (sap-ref-single nfp
(stack-frame-offset 1 0))
2386 (the single-float value
))))
2387 (#.sb
!vm
:double-stack-sc-number
2389 (setf (sap-ref-double nfp
(stack-frame-offset 2 0))
2390 (the double-float value
))))
2392 (#.sb
!vm
:long-stack-sc-number
2394 (setf (sap-ref-long nfp
(stack-frame-offset 3 0))
2395 (the long-float value
))))
2396 (#.sb
!vm
:complex-single-stack-sc-number
2398 (setf (sap-ref-single
2399 nfp
(stack-frame-offset 1 0))
2401 (realpart (the (complex single-float
) value
))
2403 (the single-float
(realpart value
)))
2404 (setf (sap-ref-single
2405 nfp
(stack-frame-offset 1 1))
2407 (imagpart (the (complex single-float
) value
))
2409 (the single-float
(realpart value
)))))
2410 (#.sb
!vm
:complex-double-stack-sc-number
2412 (setf (sap-ref-double
2413 nfp
(stack-frame-offset 2 0))
2415 (realpart (the (complex double-float
) value
))
2417 (the double-float
(realpart value
)))
2418 (setf (sap-ref-double
2419 nfp
(stack-frame-offset 2 2))
2421 (imagpart (the (complex double-float
) value
))
2423 (the double-float
(realpart value
)))))
2425 (#.sb
!vm
:complex-long-stack-sc-number
2428 nfp
(stack-frame-offset 3 0))
2430 (realpart (the (complex long-float
) value
))
2432 (the long-float
(realpart value
)))
2434 nfp
(stack-frame-offset 3 #!+sparc
4
2435 #!+(or x86 x86-64
) 3
2436 #!-
(or sparc x86 x86-64
) 0))
2438 (imagpart (the (complex long-float
) value
))
2440 (the long-float
(realpart value
)))))
2441 (#.sb
!vm
:control-stack-sc-number
2442 (setf (stack-ref fp
(sb!c
:sc-offset-offset sc-offset
)) value
))
2443 (#.sb
!vm
:character-stack-sc-number
2445 (setf (sap-ref-word nfp
(stack-frame-offset 1 0))
2446 (char-code (the character value
)))))
2447 (#.sb
!vm
:unsigned-stack-sc-number
2449 (setf (sap-ref-word nfp
(stack-frame-offset 1 0))
2450 (the (unsigned-byte 32) value
))))
2451 (#.sb
!vm
:signed-stack-sc-number
2453 (setf (signed-sap-ref-word nfp
(stack-frame-offset 1 0))
2454 (the (signed-byte 32) value
))))
2455 (#.sb
!vm
:sap-stack-sc-number
2457 (setf (sap-ref-sap nfp
(stack-frame-offset 1 0))
2458 (the system-area-pointer value
)))))))
2460 ;;; The method for setting and accessing COMPILED-DEBUG-VAR values use
2461 ;;; this to determine if the value stored is the actual value or an
2462 ;;; indirection cell.
2463 (defun indirect-value-cell-p (x)
2464 (and (= (lowtag-of x
) sb
!vm
:other-pointer-lowtag
)
2465 (= (widetag-of x
) sb
!vm
:value-cell-header-widetag
)))
2467 ;;; Return three values reflecting the validity of DEBUG-VAR's value
2468 ;;; at BASIC-CODE-LOCATION:
2469 ;;; :VALID The value is known to be available.
2470 ;;; :INVALID The value is known to be unavailable.
2471 ;;; :UNKNOWN The value's availability is unknown.
2473 ;;; If the variable is always alive, then it is valid. If the
2474 ;;; code-location is unknown, then the variable's validity is
2475 ;;; :unknown. Once we've called CODE-LOCATION-UNKNOWN-P, we know the
2476 ;;; live-set information has been cached in the code-location.
2477 (defun debug-var-validity (debug-var basic-code-location
)
2478 (compiled-debug-var-validity debug-var basic-code-location
))
2480 (defun debug-var-info (debug-var)
2481 (compiled-debug-var-info debug-var
))
2483 ;;; This is the method for DEBUG-VAR-VALIDITY for COMPILED-DEBUG-VARs.
2484 ;;; For safety, make sure basic-code-location is what we think.
2485 (defun compiled-debug-var-validity (debug-var basic-code-location
)
2486 (declare (type compiled-code-location basic-code-location
))
2487 (cond ((debug-var-alive-p debug-var
)
2488 (let ((debug-fun (code-location-debug-fun basic-code-location
)))
2489 (if (>= (compiled-code-location-pc basic-code-location
)
2490 (sb!c
::compiled-debug-fun-start-pc
2491 (compiled-debug-fun-compiler-debug-fun debug-fun
)))
2494 ((code-location-unknown-p basic-code-location
) :unknown
)
2496 (let ((pos (position debug-var
2497 (debug-fun-debug-vars
2498 (code-location-debug-fun
2499 basic-code-location
)))))
2501 (error 'unknown-debug-var
2502 :debug-var debug-var
2504 (code-location-debug-fun basic-code-location
)))
2505 ;; There must be live-set info since basic-code-location is known.
2506 (if (zerop (sbit (compiled-code-location-live-set
2507 basic-code-location
)
2514 ;;; This code produces and uses what we call source-paths. A
2515 ;;; source-path is a list whose first element is a form number as
2516 ;;; returned by CODE-LOCATION-FORM-NUMBER and whose last element is a
2517 ;;; top level form number as returned by
2518 ;;; CODE-LOCATION-TOPLEVEL-FORM-NUMBER. The elements from the last to
2519 ;;; the first, exclusively, are the numbered subforms into which to
2520 ;;; descend. For example:
2522 ;;; (let ((a (aref x 3)))
2524 ;;; The call to AREF in this example is form number 5. Assuming this
2525 ;;; DEFUN is the 11'th top level form, the source-path for the AREF
2526 ;;; call is as follows:
2528 ;;; Given the DEFUN, 3 gets you the LET, 1 gets you the bindings, 0
2529 ;;; gets the first binding, and 1 gets the AREF form.
2531 ;;; This returns a table mapping form numbers to source-paths. A
2532 ;;; source-path indicates a descent into the TOPLEVEL-FORM form,
2533 ;;; going directly to the subform corressponding to the form number.
2535 ;;; The vector elements are in the same format as the compiler's
2536 ;;; NODE-SOURCE-PATH; that is, the first element is the form number and
2537 ;;; the last is the TOPLEVEL-FORM number.
2538 (defun form-number-translations (form tlf-number
)
2540 (translations (make-array 12 :fill-pointer
0 :adjustable t
)))
2541 (labels ((translate1 (form path
)
2542 (unless (member form seen
)
2544 (vector-push-extend (cons (fill-pointer translations
) path
)
2549 (declare (fixnum pos
))
2552 (when (atom subform
) (return))
2553 (let ((fm (car subform
)))
2555 (translate1 fm
(cons pos path
)))
2557 (setq subform
(cdr subform
))
2558 (when (eq subform trail
) (return)))))
2562 (setq trail
(cdr trail
))))))))
2563 (translate1 form
(list tlf-number
)))
2564 (coerce translations
'simple-vector
)))
2566 ;;; FORM is a top level form, and path is a source-path into it. This
2567 ;;; returns the form indicated by the source-path. Context is the
2568 ;;; number of enclosing forms to return instead of directly returning
2569 ;;; the source-path form. When context is non-zero, the form returned
2570 ;;; contains a marker, #:****HERE****, immediately before the form
2571 ;;; indicated by path.
2572 (defun source-path-context (form path context
)
2573 (declare (type unsigned-byte context
))
2574 ;; Get to the form indicated by path or the enclosing form indicated
2575 ;; by context and path.
2576 (let ((path (reverse (butlast (cdr path
)))))
2577 (dotimes (i (- (length path
) context
))
2578 (let ((index (first path
)))
2579 (unless (and (listp form
) (< index
(length form
)))
2580 (error "Source path no longer exists."))
2581 (setq form
(elt form index
))
2582 (setq path
(rest path
))))
2583 ;; Recursively rebuild the source form resulting from the above
2584 ;; descent, copying the beginning of each subform up to the next
2585 ;; subform we descend into according to path. At the bottom of the
2586 ;; recursion, we return the form indicated by path preceded by our
2587 ;; marker, and this gets spliced into the resulting list structure
2588 ;; on the way back up.
2589 (labels ((frob (form path level
)
2590 (if (or (zerop level
) (null path
))
2593 `(#:***here
*** ,form
))
2594 (let ((n (first path
)))
2595 (unless (and (listp form
) (< n
(length form
)))
2596 (error "Source path no longer exists."))
2597 (let ((res (frob (elt form n
) (rest path
) (1- level
))))
2598 (nconc (subseq form
0 n
)
2599 (cons res
(nthcdr (1+ n
) form
))))))))
2600 (frob form path context
))))
2602 ;;; Given a code location, return the associated form-number
2603 ;;; translations and the actual top level form.
2604 (defun get-toplevel-form (location)
2605 (let ((d-source (code-location-debug-source location
)))
2606 (let* ((offset (code-location-toplevel-form-offset location
))
2608 (cond ((debug-source-form d-source
)
2609 (debug-source-form d-source
))
2610 ((debug-source-namestring d-source
)
2611 (get-file-toplevel-form location
))
2612 (t (bug "Don't know how to use a DEBUG-SOURCE without ~
2613 a namestring or a form.")))))
2614 (values (form-number-translations res offset
) res
))))
2616 ;;; To suppress the read-time evaluation #. macro during source read,
2617 ;;; *READTABLE* is modified.
2619 ;;; FIXME: This breaks #+#.(cl:if ...) Maybe we need a SAFE-READ-EVAL, which
2620 ;;; this code can use for side- effect free #. calls?
2622 ;;; FIXME: This also knows nothing of custom readtables. The assumption
2623 ;;; is that the current readtable is a decent approximation for what
2624 ;;; we want, but that's lossy.
2625 (defun safe-readtable ()
2626 (let ((rt (copy-readtable)))
2627 (set-dispatch-macro-character
2628 #\
# #\.
(lambda (stream sub-char
&rest rest
)
2629 (declare (ignore rest sub-char
))
2630 (let ((token (read stream t nil t
)))
2631 (format nil
"#.~S" token
)))
2635 ;;; Locate the source file (if it still exists) and grab the top level
2636 ;;; form. If the file is modified, we use the top level form offset
2637 ;;; instead of the recorded character offset.
2638 (defun get-file-toplevel-form (location)
2639 (let* ((d-source (code-location-debug-source location
))
2640 (tlf-offset (code-location-toplevel-form-offset location
))
2641 (local-tlf-offset (- tlf-offset
2642 (debug-source-root-number d-source
)))
2644 (aref (or (sb!di
:debug-source-start-positions d-source
)
2645 (error "no start positions map"))
2647 (namestring (debug-source-namestring d-source
)))
2648 ;; FIXME: External format?
2649 (with-open-file (f namestring
:if-does-not-exist nil
)
2651 (let ((*readtable
* (safe-readtable)))
2652 (cond ((eql (debug-source-created d-source
) (file-write-date f
))
2653 (file-position f char-offset
))
2656 "~%; File has been modified since compilation:~%; ~A~@
2657 ; Using form offset instead of character position.~%"
2659 (let ((*read-suppress
* t
))
2660 (loop repeat local-tlf-offset
2664 ;;;; PREPROCESS-FOR-EVAL
2666 ;;; Return a function of one argument that evaluates form in the
2667 ;;; lexical context of the BASIC-CODE-LOCATION LOC, or signal a
2668 ;;; NO-DEBUG-VARS condition when the LOC's DEBUG-FUN has no
2669 ;;; DEBUG-VAR information available.
2671 ;;; The returned function takes the frame to get values from as its
2672 ;;; argument, and it returns the values of FORM. The returned function
2673 ;;; can signal the following conditions: INVALID-VALUE,
2674 ;;; AMBIGUOUS-VAR-NAME, and FRAME-FUN-MISMATCH.
2675 (defun preprocess-for-eval (form loc
)
2676 (declare (type code-location loc
))
2677 (let ((n-frame (gensym))
2678 (fun (code-location-debug-fun loc
))
2681 (unless (debug-var-info-available fun
)
2682 (debug-signal 'no-debug-vars
:debug-fun fun
))
2683 (sb!int
:collect
((binds)
2685 (do-debug-fun-vars (var fun
)
2686 (let ((validity (debug-var-validity var loc
)))
2687 (unless (eq validity
:invalid
)
2688 (case (debug-var-info var
)
2690 (setf more-context var
))
2692 (setf more-count var
)))
2693 (let* ((sym (debug-var-symbol var
))
2694 (found (assoc sym
(binds))))
2696 (setf (second found
) :ambiguous
)
2697 (binds (list sym validity var
)))))))
2698 (when (and more-context more-count
)
2699 (let ((more (assoc 'sb
!debug
::more
(binds))))
2701 (setf (second more
) :ambiguous
)
2702 (binds (list 'sb
!debug
::more
:more more-context more-count
)))))
2703 (dolist (bind (binds))
2704 (let ((name (first bind
))
2706 (ecase (second bind
)
2708 (specs `(,name
(debug-var-value ',var
,n-frame
))))
2710 (let ((count-var (fourth bind
)))
2711 (specs `(,name
(multiple-value-list
2712 (sb!c
:%more-arg-values
(debug-var-value ',var
,n-frame
)
2714 (debug-var-value ',count-var
,n-frame
)))))))
2716 (specs `(,name
(debug-signal 'invalid-value
2720 (specs `(,name
(debug-signal 'ambiguous-var-name
2722 :frame
,n-frame
)))))))
2723 (let ((res (coerce `(lambda (,n-frame
)
2724 (declare (ignorable ,n-frame
))
2725 (symbol-macrolet ,(specs) ,form
))
2728 ;; This prevents these functions from being used in any
2729 ;; location other than a function return location, so maybe
2730 ;; this should only check whether FRAME's DEBUG-FUN is the
2732 (unless (code-location= (frame-code-location frame
) loc
)
2733 (debug-signal 'frame-fun-mismatch
2734 :code-location loc
:form form
:frame frame
))
2735 (funcall res frame
))))))
2739 (defun eval-in-frame (frame form
)
2740 (declare (type frame frame
))
2742 "Evaluate FORM in the lexical context of FRAME's current code location,
2743 returning the results of the evaluation."
2744 (funcall (preprocess-for-eval form
(frame-code-location frame
)) frame
))
2748 ;;;; user-visible interface
2750 ;;; Create and return a breakpoint. When program execution encounters
2751 ;;; the breakpoint, the system calls HOOK-FUN. HOOK-FUN takes the
2752 ;;; current frame for the function in which the program is running and
2753 ;;; the breakpoint object.
2755 ;;; WHAT and KIND determine where in a function the system invokes
2756 ;;; HOOK-FUN. WHAT is either a code-location or a DEBUG-FUN. KIND is
2757 ;;; one of :CODE-LOCATION, :FUN-START, or :FUN-END. Since the starts
2758 ;;; and ends of functions may not have code-locations representing
2759 ;;; them, designate these places by supplying WHAT as a DEBUG-FUN and
2760 ;;; KIND indicating the :FUN-START or :FUN-END. When WHAT is a
2761 ;;; DEBUG-FUN and kind is :FUN-END, then HOOK-FUN must take two
2762 ;;; additional arguments, a list of values returned by the function
2763 ;;; and a FUN-END-COOKIE.
2765 ;;; INFO is information supplied by and used by the user.
2767 ;;; FUN-END-COOKIE is a function. To implement :FUN-END
2768 ;;; breakpoints, the system uses starter breakpoints to establish the
2769 ;;; :FUN-END breakpoint for each invocation of the function. Upon
2770 ;;; each entry, the system creates a unique cookie to identify the
2771 ;;; invocation, and when the user supplies a function for this
2772 ;;; argument, the system invokes it on the frame and the cookie. The
2773 ;;; system later invokes the :FUN-END breakpoint hook on the same
2774 ;;; cookie. The user may save the cookie for comparison in the hook
2777 ;;; Signal an error if WHAT is an unknown code-location.
2778 (defun make-breakpoint (hook-fun what
2779 &key
(kind :code-location
) info fun-end-cookie
)
2782 (when (code-location-unknown-p what
)
2783 (error "cannot make a breakpoint at an unknown code location: ~S"
2785 (aver (eq kind
:code-location
))
2786 (let ((bpt (%make-breakpoint hook-fun what kind info
)))
2788 (compiled-code-location
2789 ;; This slot is filled in due to calling CODE-LOCATION-UNKNOWN-P.
2790 (when (eq (compiled-code-location-kind what
) :unknown-return
)
2791 (let ((other-bpt (%make-breakpoint hook-fun what
2792 :unknown-return-partner
2794 (setf (breakpoint-unknown-return-partner bpt
) other-bpt
)
2795 (setf (breakpoint-unknown-return-partner other-bpt
) bpt
))))
2796 ;; (There used to be more cases back before sbcl-0.7.0,,
2797 ;; when we did special tricks to debug the IR1
2804 (%make-breakpoint hook-fun what kind info
))
2806 (unless (eq (sb!c
::compiled-debug-fun-returns
2807 (compiled-debug-fun-compiler-debug-fun what
))
2809 (error ":FUN-END breakpoints are currently unsupported ~
2810 for the known return convention."))
2812 (let* ((bpt (%make-breakpoint hook-fun what kind info
))
2813 (starter (compiled-debug-fun-end-starter what
)))
2815 (setf starter
(%make-breakpoint
#'list what
:fun-start nil
))
2816 (setf (breakpoint-hook-fun starter
)
2817 (fun-end-starter-hook starter what
))
2818 (setf (compiled-debug-fun-end-starter what
) starter
))
2819 (setf (breakpoint-start-helper bpt
) starter
)
2820 (push bpt
(breakpoint-%info starter
))
2821 (setf (breakpoint-cookie-fun bpt
) fun-end-cookie
)
2824 ;;; These are unique objects created upon entry into a function by a
2825 ;;; :FUN-END breakpoint's starter hook. These are only created
2826 ;;; when users supply :FUN-END-COOKIE to MAKE-BREAKPOINT. Also,
2827 ;;; the :FUN-END breakpoint's hook is called on the same cookie
2828 ;;; when it is created.
2829 (defstruct (fun-end-cookie
2830 (:print-object
(lambda (obj str
)
2831 (print-unreadable-object (obj str
:type t
))))
2832 (:constructor make-fun-end-cookie
(bogus-lra debug-fun
))
2834 ;; a pointer to the bogus-lra created for :FUN-END breakpoints
2836 ;; the DEBUG-FUN associated with this cookie
2839 ;;; This maps bogus-lra-components to cookies, so that
2840 ;;; HANDLE-FUN-END-BREAKPOINT can find the appropriate cookie for the
2841 ;;; breakpoint hook.
2842 (defvar *fun-end-cookies
* (make-hash-table :test
'eq
:synchronized t
))
2844 ;;; This returns a hook function for the start helper breakpoint
2845 ;;; associated with a :FUN-END breakpoint. The returned function
2846 ;;; makes a fake LRA that all returns go through, and this piece of
2847 ;;; fake code actually breaks. Upon return from the break, the code
2848 ;;; provides the returnee with any values. Since the returned function
2849 ;;; effectively activates FUN-END-BPT on each entry to DEBUG-FUN's
2850 ;;; function, we must establish breakpoint-data about FUN-END-BPT.
2851 (defun fun-end-starter-hook (starter-bpt debug-fun
)
2852 (declare (type breakpoint starter-bpt
)
2853 (type compiled-debug-fun debug-fun
))
2854 (lambda (frame breakpoint
)
2855 (declare (ignore breakpoint
)
2857 (let ((lra-sc-offset
2858 (sb!c
::compiled-debug-fun-return-pc
2859 (compiled-debug-fun-compiler-debug-fun debug-fun
))))
2860 (multiple-value-bind (lra component offset
)
2862 (get-context-value frame
2865 (setf (get-context-value frame
2869 (let ((end-bpts (breakpoint-%info starter-bpt
)))
2870 (let ((data (breakpoint-data component offset
)))
2871 (setf (breakpoint-data-breakpoints data
) end-bpts
)
2872 (dolist (bpt end-bpts
)
2873 (setf (breakpoint-internal-data bpt
) data
)))
2874 (let ((cookie (make-fun-end-cookie lra debug-fun
)))
2875 (setf (gethash component
*fun-end-cookies
*) cookie
)
2876 (dolist (bpt end-bpts
)
2877 (let ((fun (breakpoint-cookie-fun bpt
)))
2878 (when fun
(funcall fun frame cookie
))))))))))
2880 ;;; This takes a FUN-END-COOKIE and a frame, and it returns
2881 ;;; whether the cookie is still valid. A cookie becomes invalid when
2882 ;;; the frame that established the cookie has exited. Sometimes cookie
2883 ;;; holders are unaware of cookie invalidation because their
2884 ;;; :FUN-END breakpoint hooks didn't run due to THROW'ing.
2886 ;;; This takes a frame as an efficiency hack since the user probably
2887 ;;; has a frame object in hand when using this routine, and it saves
2888 ;;; repeated parsing of the stack and consing when asking whether a
2889 ;;; series of cookies is valid.
2890 (defun fun-end-cookie-valid-p (frame cookie
)
2891 (let ((lra (fun-end-cookie-bogus-lra cookie
))
2892 (lra-sc-offset (sb!c
::compiled-debug-fun-return-pc
2893 (compiled-debug-fun-compiler-debug-fun
2894 (fun-end-cookie-debug-fun cookie
)))))
2895 (do ((frame frame
(frame-down frame
)))
2897 (when (and (compiled-frame-p frame
)
2898 (#!-
(or x86 x86-64
) eq
#!+(or x86 x86-64
) sap
=
2900 (get-context-value frame lra-save-offset lra-sc-offset
)))
2903 ;;;; ACTIVATE-BREAKPOINT
2905 ;;; Cause the system to invoke the breakpoint's hook function until
2906 ;;; the next call to DEACTIVATE-BREAKPOINT or DELETE-BREAKPOINT. The
2907 ;;; system invokes breakpoint hook functions in the opposite order
2908 ;;; that you activate them.
2909 (defun activate-breakpoint (breakpoint)
2910 (when (eq (breakpoint-status breakpoint
) :deleted
)
2911 (error "cannot activate a deleted breakpoint: ~S" breakpoint
))
2912 (unless (eq (breakpoint-status breakpoint
) :active
)
2913 (ecase (breakpoint-kind breakpoint
)
2915 (let ((loc (breakpoint-what breakpoint
)))
2917 (compiled-code-location
2918 (activate-compiled-code-location-breakpoint breakpoint
)
2919 (let ((other (breakpoint-unknown-return-partner breakpoint
)))
2921 (activate-compiled-code-location-breakpoint other
))))
2922 ;; (There used to be more cases back before sbcl-0.7.0, when
2923 ;; we did special tricks to debug the IR1 interpreter.)
2926 (etypecase (breakpoint-what breakpoint
)
2928 (activate-compiled-fun-start-breakpoint breakpoint
))
2929 ;; (There used to be more cases back before sbcl-0.7.0, when
2930 ;; we did special tricks to debug the IR1 interpreter.)
2933 (etypecase (breakpoint-what breakpoint
)
2935 (let ((starter (breakpoint-start-helper breakpoint
)))
2936 (unless (eq (breakpoint-status starter
) :active
)
2937 ;; may already be active by some other :FUN-END breakpoint
2938 (activate-compiled-fun-start-breakpoint starter
)))
2939 (setf (breakpoint-status breakpoint
) :active
))
2940 ;; (There used to be more cases back before sbcl-0.7.0, when
2941 ;; we did special tricks to debug the IR1 interpreter.)
2945 (defun activate-compiled-code-location-breakpoint (breakpoint)
2946 (declare (type breakpoint breakpoint
))
2947 (let ((loc (breakpoint-what breakpoint
)))
2948 (declare (type compiled-code-location loc
))
2949 (sub-activate-breakpoint
2951 (breakpoint-data (compiled-debug-fun-component
2952 (code-location-debug-fun loc
))
2953 (+ (compiled-code-location-pc loc
)
2954 (if (or (eq (breakpoint-kind breakpoint
)
2955 :unknown-return-partner
)
2956 (eq (compiled-code-location-kind loc
)
2957 :single-value-return
))
2958 sb
!vm
:single-value-return-byte-offset
2961 (defun activate-compiled-fun-start-breakpoint (breakpoint)
2962 (declare (type breakpoint breakpoint
))
2963 (let ((debug-fun (breakpoint-what breakpoint
)))
2964 (sub-activate-breakpoint
2966 (breakpoint-data (compiled-debug-fun-component debug-fun
)
2967 (sb!c
::compiled-debug-fun-start-pc
2968 (compiled-debug-fun-compiler-debug-fun
2971 (defun sub-activate-breakpoint (breakpoint data
)
2972 (declare (type breakpoint breakpoint
)
2973 (type breakpoint-data data
))
2974 (setf (breakpoint-status breakpoint
) :active
)
2976 (unless (breakpoint-data-breakpoints data
)
2977 (setf (breakpoint-data-instruction data
)
2979 (breakpoint-install (get-lisp-obj-address
2980 (breakpoint-data-component data
))
2981 (breakpoint-data-offset data
)))))
2982 (setf (breakpoint-data-breakpoints data
)
2983 (append (breakpoint-data-breakpoints data
) (list breakpoint
)))
2984 (setf (breakpoint-internal-data breakpoint
) data
)))
2986 ;;;; DEACTIVATE-BREAKPOINT
2988 ;;; Stop the system from invoking the breakpoint's hook function.
2989 (defun deactivate-breakpoint (breakpoint)
2990 (when (eq (breakpoint-status breakpoint
) :active
)
2992 (let ((loc (breakpoint-what breakpoint
)))
2994 ((or compiled-code-location compiled-debug-fun
)
2995 (deactivate-compiled-breakpoint breakpoint
)
2996 (let ((other (breakpoint-unknown-return-partner breakpoint
)))
2998 (deactivate-compiled-breakpoint other
))))
2999 ;; (There used to be more cases back before sbcl-0.7.0, when
3000 ;; we did special tricks to debug the IR1 interpreter.)
3004 (defun deactivate-compiled-breakpoint (breakpoint)
3005 (if (eq (breakpoint-kind breakpoint
) :fun-end
)
3006 (let ((starter (breakpoint-start-helper breakpoint
)))
3007 (unless (find-if (lambda (bpt)
3008 (and (not (eq bpt breakpoint
))
3009 (eq (breakpoint-status bpt
) :active
)))
3010 (breakpoint-%info starter
))
3011 (deactivate-compiled-breakpoint starter
)))
3012 (let* ((data (breakpoint-internal-data breakpoint
))
3013 (bpts (delete breakpoint
(breakpoint-data-breakpoints data
))))
3014 (setf (breakpoint-internal-data breakpoint
) nil
)
3015 (setf (breakpoint-data-breakpoints data
) bpts
)
3018 (breakpoint-remove (get-lisp-obj-address
3019 (breakpoint-data-component data
))
3020 (breakpoint-data-offset data
)
3021 (breakpoint-data-instruction data
)))
3022 (delete-breakpoint-data data
))))
3023 (setf (breakpoint-status breakpoint
) :inactive
)
3026 ;;;; BREAKPOINT-INFO
3028 ;;; Return the user-maintained info associated with breakpoint. This
3030 (defun breakpoint-info (breakpoint)
3031 (breakpoint-%info breakpoint
))
3032 (defun %set-breakpoint-info
(breakpoint value
)
3033 (setf (breakpoint-%info breakpoint
) value
)
3034 (let ((other (breakpoint-unknown-return-partner breakpoint
)))
3036 (setf (breakpoint-%info other
) value
))))
3038 ;;;; BREAKPOINT-ACTIVE-P and DELETE-BREAKPOINT
3040 (defun breakpoint-active-p (breakpoint)
3041 (ecase (breakpoint-status breakpoint
)
3043 ((:inactive
:deleted
) nil
)))
3045 ;;; Free system storage and remove computational overhead associated
3046 ;;; with breakpoint. After calling this, breakpoint is completely
3047 ;;; impotent and can never become active again.
3048 (defun delete-breakpoint (breakpoint)
3049 (let ((status (breakpoint-status breakpoint
)))
3050 (unless (eq status
:deleted
)
3051 (when (eq status
:active
)
3052 (deactivate-breakpoint breakpoint
))
3053 (setf (breakpoint-status breakpoint
) :deleted
)
3054 (let ((other (breakpoint-unknown-return-partner breakpoint
)))
3056 (setf (breakpoint-status other
) :deleted
)))
3057 (when (eq (breakpoint-kind breakpoint
) :fun-end
)
3058 (let* ((starter (breakpoint-start-helper breakpoint
))
3059 (breakpoints (delete breakpoint
3060 (the list
(breakpoint-info starter
)))))
3061 (setf (breakpoint-info starter
) breakpoints
)
3063 (delete-breakpoint starter
)
3064 (setf (compiled-debug-fun-end-starter
3065 (breakpoint-what breakpoint
))
3069 ;;;; C call out stubs
3071 ;;; This actually installs the break instruction in the component. It
3072 ;;; returns the overwritten bits. You must call this in a context in
3073 ;;; which GC is disabled, so that Lisp doesn't move objects around
3074 ;;; that C is pointing to.
3075 (sb!alien
:define-alien-routine
"breakpoint_install" sb
!alien
:unsigned-int
3076 (code-obj sb
!alien
:unsigned
)
3077 (pc-offset sb
!alien
:int
))
3079 ;;; This removes the break instruction and replaces the original
3080 ;;; instruction. You must call this in a context in which GC is disabled
3081 ;;; so Lisp doesn't move objects around that C is pointing to.
3082 (sb!alien
:define-alien-routine
"breakpoint_remove" sb
!alien
:void
3083 (code-obj sb
!alien
:unsigned
)
3084 (pc-offset sb
!alien
:int
)
3085 (old-inst sb
!alien
:unsigned-int
))
3087 (sb!alien
:define-alien-routine
"breakpoint_do_displaced_inst" sb
!alien
:void
3088 (scp (* os-context-t
))
3089 (orig-inst sb
!alien
:unsigned-int
))
3091 ;;;; breakpoint handlers (layer between C and exported interface)
3093 ;;; This maps components to a mapping of offsets to BREAKPOINT-DATAs.
3094 (defvar *component-breakpoint-offsets
* (make-hash-table :test
'eq
:synchronized t
))
3096 ;;; This returns the BREAKPOINT-DATA object associated with component cross
3097 ;;; offset. If none exists, this makes one, installs it, and returns it.
3098 (defun breakpoint-data (component offset
&optional
(create t
))
3099 (flet ((install-breakpoint-data ()
3101 (let ((data (make-breakpoint-data component offset
)))
3102 (push (cons offset data
)
3103 (gethash component
*component-breakpoint-offsets
*))
3105 (let ((offsets (gethash component
*component-breakpoint-offsets
*)))
3107 (let ((data (assoc offset offsets
)))
3110 (install-breakpoint-data)))
3111 (install-breakpoint-data)))))
3113 ;;; We use this when there are no longer any active breakpoints
3114 ;;; corresponding to DATA.
3115 (defun delete-breakpoint-data (data)
3116 ;; Again, this looks brittle. Is there no danger of being interrupted
3118 (let* ((component (breakpoint-data-component data
))
3119 (offsets (delete (breakpoint-data-offset data
)
3120 (gethash component
*component-breakpoint-offsets
*)
3123 (setf (gethash component
*component-breakpoint-offsets
*) offsets
)
3124 (remhash component
*component-breakpoint-offsets
*)))
3127 ;;; The C handler for interrupts calls this when it has a
3128 ;;; debugging-tool break instruction. This does *not* handle all
3129 ;;; breaks; for example, it does not handle breaks for internal
3131 (defun handle-breakpoint (offset component signal-context
)
3132 (let ((data (breakpoint-data component offset nil
)))
3134 (error "unknown breakpoint in ~S at offset ~S"
3135 (debug-fun-name (debug-fun-from-pc component offset
))
3137 (let ((breakpoints (breakpoint-data-breakpoints data
)))
3138 (if (or (null breakpoints
)
3139 (eq (breakpoint-kind (car breakpoints
)) :fun-end
))
3140 (handle-fun-end-breakpoint-aux breakpoints data signal-context
)
3141 (handle-breakpoint-aux breakpoints data
3142 offset component signal-context
)))))
3144 ;;; This holds breakpoint-datas while invoking the breakpoint hooks
3145 ;;; associated with that particular component and location. While they
3146 ;;; are executing, if we hit the location again, we ignore the
3147 ;;; breakpoint to avoid infinite recursion. fun-end breakpoints
3148 ;;; must work differently since the breakpoint-data is unique for each
3150 (defvar *executing-breakpoint-hooks
* nil
)
3152 ;;; This handles code-location and DEBUG-FUN :FUN-START
3154 (defun handle-breakpoint-aux (breakpoints data offset component signal-context
)
3156 (bug "breakpoint that nobody wants"))
3157 (unless (member data
*executing-breakpoint-hooks
*)
3158 (let ((*executing-breakpoint-hooks
* (cons data
3159 *executing-breakpoint-hooks
*)))
3160 (invoke-breakpoint-hooks breakpoints signal-context
)))
3161 ;; At this point breakpoints may not hold the same list as
3162 ;; BREAKPOINT-DATA-BREAKPOINTS since invoking hooks may have allowed
3163 ;; a breakpoint deactivation. In fact, if all breakpoints were
3164 ;; deactivated then data is invalid since it was deleted and so the
3165 ;; correct one must be looked up if it is to be used. If there are
3166 ;; no more breakpoints active at this location, then the normal
3167 ;; instruction has been put back, and we do not need to
3168 ;; DO-DISPLACED-INST.
3169 (setf data
(breakpoint-data component offset nil
))
3170 (when (and data
(breakpoint-data-breakpoints data
))
3171 ;; The breakpoint is still active, so we need to execute the
3172 ;; displaced instruction and leave the breakpoint instruction
3173 ;; behind. The best way to do this is different on each machine,
3174 ;; so we just leave it up to the C code.
3175 (breakpoint-do-displaced-inst signal-context
3176 (breakpoint-data-instruction data
))
3177 ;; Some platforms have no usable sigreturn() call. If your
3178 ;; implementation of arch_do_displaced_inst() _does_ sigreturn(),
3179 ;; it's polite to warn here
3180 #!+(and sparc solaris
)
3181 (error "BREAKPOINT-DO-DISPLACED-INST returned?")))
3183 (defun invoke-breakpoint-hooks (breakpoints signal-context
)
3184 (let* ((frame (signal-context-frame signal-context
)))
3185 (dolist (bpt breakpoints
)
3186 (funcall (breakpoint-hook-fun bpt
)
3188 ;; If this is an :UNKNOWN-RETURN-PARTNER, then pass the
3189 ;; hook function the original breakpoint, so that users
3190 ;; aren't forced to confront the fact that some
3191 ;; breakpoints really are two.
3192 (if (eq (breakpoint-kind bpt
) :unknown-return-partner
)
3193 (breakpoint-unknown-return-partner bpt
)
3196 (defun signal-context-frame (signal-context)
3199 (declare (optimize (inhibit-warnings 3)))
3200 (sb!alien
:sap-alien signal-context
(* os-context-t
))))
3201 (cfp (int-sap (sb!vm
:context-register scp sb
!vm
::cfp-offset
))))
3202 (compute-calling-frame cfp
3203 ;; KLUDGE: This argument is ignored on
3204 ;; x86oids in this scenario, but is
3205 ;; declared to be a SAP.
3206 #!+(or x86 x86-64
) (sb!vm
:context-pc scp
)
3207 #!-
(or x86 x86-64
) nil
3210 (defun handle-fun-end-breakpoint (offset component context
)
3211 (let ((data (breakpoint-data component offset nil
)))
3213 (error "unknown breakpoint in ~S at offset ~S"
3214 (debug-fun-name (debug-fun-from-pc component offset
))
3216 (let ((breakpoints (breakpoint-data-breakpoints data
)))
3218 (aver (eq (breakpoint-kind (car breakpoints
)) :fun-end
))
3219 (handle-fun-end-breakpoint-aux breakpoints data context
)))))
3221 ;;; Either HANDLE-BREAKPOINT calls this for :FUN-END breakpoints
3222 ;;; [old C code] or HANDLE-FUN-END-BREAKPOINT calls this directly
3224 (defun handle-fun-end-breakpoint-aux (breakpoints data signal-context
)
3225 ;; FIXME: This looks brittle: what if we are interrupted somewhere
3226 ;; here? ...or do we have interrupts disabled here?
3227 (delete-breakpoint-data data
)
3230 (declare (optimize (inhibit-warnings 3)))
3231 (sb!alien
:sap-alien signal-context
(* os-context-t
))))
3232 (frame (signal-context-frame signal-context
))
3233 (component (breakpoint-data-component data
))
3234 (cookie (gethash component
*fun-end-cookies
*)))
3235 (remhash component
*fun-end-cookies
*)
3236 (dolist (bpt breakpoints
)
3237 (funcall (breakpoint-hook-fun bpt
)
3239 (get-fun-end-breakpoint-values scp
)
3242 (defun get-fun-end-breakpoint-values (scp)
3243 (let ((ocfp (int-sap (sb!vm
:context-register
3245 #!-
(or x86 x86-64
) sb
!vm
::ocfp-offset
3246 #!+(or x86 x86-64
) sb
!vm
::ebx-offset
)))
3247 (nargs (make-lisp-obj
3248 (sb!vm
:context-register scp sb
!vm
::nargs-offset
)))
3249 (reg-arg-offsets '#.sb
!vm
::*register-arg-offsets
*)
3252 (dotimes (arg-num nargs
)
3253 (push (if reg-arg-offsets
3255 (sb!vm
:context-register scp
(pop reg-arg-offsets
)))
3256 (stack-ref ocfp
(+ arg-num
3257 #!+(or x86 x86-64
) sb
!vm
::sp-
>fp-offset
)))
3259 (nreverse results
)))
3261 ;;;; MAKE-BOGUS-LRA (used for :FUN-END breakpoints)
3263 (defconstant bogus-lra-constants
3264 #!-
(or x86-64 x86
) 1
3266 ;; One more for a fixup vector
3269 ;;; Make a bogus LRA object that signals a breakpoint trap when
3270 ;;; returned to. If the breakpoint trap handler returns, REAL-LRA is
3271 ;;; returned to. Three values are returned: the bogus LRA object, the
3272 ;;; code component it is part of, and the PC offset for the trap
3274 (defun make-bogus-lra (real-lra)
3276 ;; These are really code labels, not variables: but this way we get
3278 (let* ((src-start (static-foreign-symbol-sap "fun_end_breakpoint_guts"))
3279 (src-end (static-foreign-symbol-sap "fun_end_breakpoint_end"))
3280 (trap-loc (static-foreign-symbol-sap "fun_end_breakpoint_trap"))
3281 (length (sap- src-end src-start
))
3283 (sb!c
:allocate-code-object bogus-lra-constants length
))
3284 (dst-start (code-instructions code-object
)))
3285 (declare (type system-area-pointer
3286 src-start src-end dst-start trap-loc
)
3287 (type index length
))
3288 (setf (%code-debug-info code-object
) :bogus-lra
)
3290 (setf (code-header-ref code-object real-lra-slot
) real-lra
3291 ;; Set up the widetag and header of LRA
3292 ;; The header contains the same thing as the code object header,
3293 ;; the number of boxed words, which include slots and
3294 ;; constants and it has to be double word aligned.
3296 ;; It used to be a part of the fun_end_breakpoint_guts
3297 ;; but its position and value depend on the offsets
3298 ;; and alignment of code object slots.
3299 (sap-ref-word dst-start
(- sb
!vm
:n-word-bits
))
3300 (+ sb
!vm
:return-pc-header-widetag
3301 (logandc2 (+ code-constants-offset
3306 (multiple-value-bind (offset code
) (compute-lra-data-from-pc real-lra
)
3307 (setf (code-header-ref code-object real-lra-slot
) code
)
3308 (setf (code-header-ref code-object
(1+ real-lra-slot
)) offset
))
3309 (system-area-ub8-copy src-start
0 dst-start
0 length
)
3311 (sb!vm
:sanctify-for-execution code-object
)
3313 (values dst-start code-object
(sap- trap-loc src-start
))
3315 (let ((new-lra (make-lisp-obj (+ (sap-int dst-start
)
3316 sb
!vm
:other-pointer-lowtag
))))
3317 ;; We used to set the header value of the LRA here to the
3318 ;; offset from the enclosing component to the LRA header, but
3319 ;; MAKE-LISP-OBJ actually checks the value before we get a
3320 ;; chance to set it, so it's now done in arch-assem.S.
3321 (values new-lra code-object
(sap- trap-loc src-start
))))))
3325 ;;; This appears here because it cannot go with the DEBUG-FUN
3326 ;;; interface since DO-DEBUG-BLOCK-LOCATIONS isn't defined until after
3327 ;;; the DEBUG-FUN routines.
3329 ;;; Return a code-location before the body of a function and after all
3330 ;;; the arguments are in place; or if that location can't be
3331 ;;; determined due to a lack of debug information, return NIL.
3332 (defun debug-fun-start-location (debug-fun)
3333 (etypecase debug-fun
3335 (code-location-from-pc debug-fun
3336 (sb!c
::compiled-debug-fun-start-pc
3337 (compiled-debug-fun-compiler-debug-fun
3340 ;; (There used to be more cases back before sbcl-0.7.0, when
3341 ;; we did special tricks to debug the IR1 interpreter.)
3345 ;;;; Single-stepping
3347 ;;; The single-stepper works by inserting conditional trap instructions
3348 ;;; into the generated code (see src/compiler/*/call.lisp), currently:
3350 ;;; 1) Before the code generated for a function call that was
3351 ;;; translated to a VOP
3352 ;;; 2) Just before the call instruction for a full call
3354 ;;; In both cases, the trap will only be executed if stepping has been
3355 ;;; enabled, in which case it'll ultimately be handled by
3356 ;;; HANDLE-SINGLE-STEP-TRAP, which will either signal a stepping condition,
3357 ;;; or replace the function that's about to be called with a wrapper
3358 ;;; which will signal the condition.
3360 (defun handle-single-step-trap (kind callee-register-offset
)
3361 (let ((context (nth-interrupt-context (1- *free-interrupt-context-index
*))))
3362 ;; The following calls must get tail-call eliminated for
3363 ;; *STEP-FRAME* to get set correctly on non-x86.
3364 (if (= kind single-step-before-trap
)
3365 (handle-single-step-before-trap context
)
3366 (handle-single-step-around-trap context callee-register-offset
))))
3368 (defvar *step-frame
* nil
)
3370 (defun handle-single-step-before-trap (context)
3371 (let ((step-info (single-step-info-from-context context
)))
3372 ;; If there was not enough debug information available, there's no
3373 ;; sense in signaling the condition.
3377 (signal-context-frame (sb!alien
::alien-sap context
))
3379 ;; KLUDGE: Use the first non-foreign frame as the
3380 ;; *STACK-TOP-HINT*. Getting the frame from the signal
3381 ;; context as on x86 would be cleaner, but
3382 ;; SIGNAL-CONTEXT-FRAME doesn't seem seem to work at all
3384 (loop with frame
= (frame-down (top-frame))
3386 for dfun
= (frame-debug-fun frame
)
3387 do
(when (typep dfun
'compiled-debug-fun
)
3389 do
(setf frame
(frame-down frame
)))))
3390 (sb!impl
::step-form step-info
3391 ;; We could theoretically store information in
3392 ;; the debug-info about to determine the
3393 ;; arguments here, but for now let's just pass
3397 ;;; This function will replace the fdefn / function that was in the
3398 ;;; register at CALLEE-REGISTER-OFFSET with a wrapper function. To
3399 ;;; ensure that the full call will use the wrapper instead of the
3400 ;;; original, conditional trap must be emitted before the fdefn /
3401 ;;; function is converted into a raw address.
3402 (defun handle-single-step-around-trap (context callee-register-offset
)
3403 ;; Fetch the function / fdefn we're about to call from the
3404 ;; appropriate register.
3405 (let* ((callee (make-lisp-obj
3406 (context-register context callee-register-offset
)))
3407 (step-info (single-step-info-from-context context
)))
3408 ;; If there was not enough debug information available, there's no
3409 ;; sense in signaling the condition.
3411 (return-from handle-single-step-around-trap
))
3412 (let* ((fun (lambda (&rest args
)
3414 (apply (typecase callee
3415 (fdefn (fdefn-fun callee
))
3418 ;; Signal a step condition
3420 (let ((*step-frame
* (frame-down (top-frame))))
3421 (sb!impl
::step-form step-info args
))))
3422 ;; And proceed based on its return value.
3424 ;; STEP-INTO was selected. Use *STEP-OUT* to
3425 ;; let the stepper know that selecting the
3426 ;; STEP-OUT restart is valid inside this
3427 (let ((sb!impl
::*step-out
* :maybe
))
3428 ;; Pass the return values of the call to
3429 ;; STEP-VALUES, which will signal a
3430 ;; condition with them in the VALUES slot.
3432 (multiple-value-call #'sb
!impl
::step-values
3435 ;; If the user selected the STEP-OUT
3436 ;; restart during the call, resume
3438 (when (eq sb
!impl
::*step-out
* t
)
3439 (sb!impl
::enable-stepping
))))
3440 ;; STEP-NEXT / CONTINUE / OUT selected:
3441 ;; Disable the stepper for the duration of
3443 (sb!impl
::with-stepping-disabled
3445 (new-callee (etypecase callee
3447 (let ((fdefn (make-fdefn (gensym))))
3448 (setf (fdefn-fun fdefn
) fun
)
3451 ;; And then store the wrapper in the same place.
3452 (with-pinned-objects (new-callee)
3453 ;; %SET-CONTEXT-REGISTER is a function, so the address of
3454 ;; NEW-CALLEE gets converted to a fixnum before passing, which
3455 ;; won't keep NEW-CALLEE pinned down. Once it's inside
3456 ;; CONTEXT, which is registered in thread->interrupt_contexts,
3457 ;; it will properly point to NEW-CALLEE.
3458 (setf (context-register context callee-register-offset
)
3459 (get-lisp-obj-address new-callee
))))))
3461 ;;; Given a signal context, fetch the step-info that's been stored in
3462 ;;; the debug info at the trap point.
3463 (defun single-step-info-from-context (context)
3464 (multiple-value-bind (pc-offset code
)
3465 (compute-lra-data-from-pc (context-pc context
))
3466 (let* ((debug-fun (debug-fun-from-pc code pc-offset
))
3467 (location (code-location-from-pc debug-fun
3472 (fill-in-code-location location
)
3473 (code-location-debug-source location
)
3474 (compiled-code-location-step-info location
))
3478 ;;; Return the frame that triggered a single-step condition. Used to
3479 ;;; provide a *STACK-TOP-HINT*.
3480 (defun find-stepped-frame ()