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 exist for caching data stored in packed binary form in
223 ;;; compiler DEBUG-FUNs. *COMPILED-DEBUG-FUNS* maps a SB!C::DEBUG-FUN
224 ;;; to a DEBUG-FUN. There should only be one DEBUG-FUN in existence
225 ;;; for any function; that is, all CODE-LOCATIONs and other objects
226 ;;; that reference DEBUG-FUNs point to unique objects. This is
227 ;;; due to the overhead in cached information.
229 (defstruct (debug-fun (:constructor nil
)
231 ;; some representation of the function arguments. See
232 ;; DEBUG-FUN-LAMBDA-LIST.
233 ;; NOTE: must parse vars before parsing arg list stuff.
234 (%lambda-list
:unparsed
)
235 ;; cached DEBUG-VARS information (unexported).
236 ;; These are sorted by their name.
237 (%debug-vars
:unparsed
:type
(or simple-vector null
(member :unparsed
)))
238 ;; cached debug-block information. This is NIL when we have tried to
239 ;; parse the packed binary info, but none is available.
240 (blocks :unparsed
:type
(or simple-vector null
(member :unparsed
)))
241 ;; the actual function if available
242 (%function
:unparsed
:type
(or null function
(member :unparsed
))))
243 (def!method print-object
((obj debug-fun
) stream
)
244 (print-unreadable-object (obj stream
:type t
)
245 (prin1 (debug-fun-name obj
) stream
)))
247 (defstruct (bogus-debug-fun
249 (:constructor make-bogus-debug-fun
260 ;;; These exist for caching data stored in packed binary form in compiler
262 (defstruct (debug-block (:constructor nil
)
264 ;; This indicates whether the block is a special glob of code shared
265 ;; by various functions and tucked away elsewhere in a component.
266 ;; This kind of block has no start code-location. This slot is in
267 ;; all debug-blocks since it is an exported interface.
268 (elsewhere-p nil
:type boolean
))
269 (def!method print-object
((obj debug-block
) str
)
270 (print-unreadable-object (obj str
:type t
)
271 (prin1 (debug-block-fun-name obj
) str
)))
274 (setf (fdocumentation 'debug-block-elsewhere-p
'function
)
275 "Return whether debug-block represents elsewhere code.")
277 (defstruct (compiled-debug-block (:include debug-block
)
279 ;; code-location information for the block
280 (code-locations #() :type simple-vector
))
282 (defstruct (code-location (:constructor nil
)
284 ;; the DEBUG-FUN containing this CODE-LOCATION
285 (debug-fun nil
:type debug-fun
)
286 ;; This is initially :UNSURE. Upon first trying to access an
287 ;; :UNPARSED slot, if the data is unavailable, then this becomes T,
288 ;; and the code-location is unknown. If the data is available, this
289 ;; becomes NIL, a known location. We can't use a separate type
290 ;; code-location for this since we must return code-locations before
291 ;; we can tell whether they're known or unknown. For example, when
292 ;; parsing the stack, we don't want to unpack all the variables and
293 ;; blocks just to make frames.
294 (%unknown-p
:unsure
:type
(member t nil
:unsure
))
295 ;; the DEBUG-BLOCK containing CODE-LOCATION. XXX Possibly toss this
296 ;; out and just find it in the blocks cache in DEBUG-FUN.
297 (%debug-block
:unparsed
:type
(or debug-block
(member :unparsed
)))
298 ;; This is the number of forms processed by the compiler or loader
299 ;; before the top level form containing this code-location.
300 (%tlf-offset
:unparsed
:type
(or index
(member :unparsed
)))
301 ;; This is the depth-first number of the node that begins
302 ;; code-location within its top level form.
303 (%form-number
:unparsed
:type
(or index
(member :unparsed
))))
307 ;;; These represent call frames on the stack.
308 (defstruct (frame (:constructor nil
)
310 ;; the next frame up, or NIL when top frame
311 (up nil
:type
(or frame null
))
312 ;; the previous frame down, or NIL when the bottom frame. Before
313 ;; computing the next frame down, this slot holds the frame pointer
314 ;; to the control stack for the given frame. This lets us get the
315 ;; next frame down and the return-pc for that frame.
316 (%down
:unparsed
:type
(or frame
(member nil
:unparsed
)))
317 ;; the DEBUG-FUN for the function whose call this frame represents
318 (debug-fun nil
:type debug-fun
)
319 ;; the CODE-LOCATION where the frame's DEBUG-FUN will continue
320 ;; running when program execution returns to this frame. If someone
321 ;; interrupted this frame, the result could be an unknown
323 (code-location nil
:type code-location
)
324 ;; an a-list of catch-tags to code-locations
325 (%catches
:unparsed
:type
(or list
(member :unparsed
)))
326 ;; pointer to frame on control stack (unexported)
328 ;; This is the frame's number for prompt printing. Top is zero.
329 (number 0 :type index
))
331 (defstruct (compiled-frame
333 (:constructor make-compiled-frame
334 (pointer up debug-fun code-location number
337 ;; This indicates whether someone interrupted the frame.
338 ;; (unexported). If escaped, this is a pointer to the state that was
339 ;; saved when we were interrupted, an os_context_t, i.e. the third
340 ;; argument to an SA_SIGACTION-style signal handler.
342 (def!method print-object
((obj compiled-frame
) str
)
343 (print-unreadable-object (obj str
:type t
)
345 "~S~:[~;, interrupted~]"
346 (debug-fun-name (frame-debug-fun obj
))
347 (compiled-frame-escaped obj
))))
350 ;;; This maps SB!C::COMPILED-DEBUG-FUNs to
351 ;;; COMPILED-DEBUG-FUNs, so we can get at cached stuff and not
352 ;;; duplicate COMPILED-DEBUG-FUN structures.
353 (defvar *compiled-debug-funs
* (make-hash-table :test
'eq
:weakness
:key
))
355 ;;; Make a COMPILED-DEBUG-FUN for a SB!C::COMPILER-DEBUG-FUN and its
356 ;;; component. This maps the latter to the former in
357 ;;; *COMPILED-DEBUG-FUNS*. If there already is a COMPILED-DEBUG-FUN,
358 ;;; then this returns it from *COMPILED-DEBUG-FUNS*.
360 ;;; FIXME: It seems this table can potentially grow without bounds,
361 ;;; and retains roots to functions that might otherwise be collected.
362 (defun make-compiled-debug-fun (compiler-debug-fun component
)
363 (let ((table *compiled-debug-funs
*))
364 (with-locked-system-table (table)
365 (or (gethash compiler-debug-fun table
)
366 (setf (gethash compiler-debug-fun table
)
367 (%make-compiled-debug-fun compiler-debug-fun component
))))))
371 ;;; This is an internal structure that manages information about a
372 ;;; breakpoint locations. See *COMPONENT-BREAKPOINT-OFFSETS*.
373 (defstruct (breakpoint-data (:constructor make-breakpoint-data
376 ;; This is the component in which the breakpoint lies.
378 ;; This is the byte offset into the component.
379 (offset nil
:type index
)
380 ;; The original instruction replaced by the breakpoint.
381 (instruction nil
:type
(or null sb
!vm
::word
))
382 ;; A list of user breakpoints at this location.
383 (breakpoints nil
:type list
))
384 (def!method print-object
((obj breakpoint-data
) str
)
385 (print-unreadable-object (obj str
:type t
)
386 (format str
"~S at ~S"
388 (debug-fun-from-pc (breakpoint-data-component obj
)
389 (breakpoint-data-offset obj
)))
390 (breakpoint-data-offset obj
))))
392 (defstruct (breakpoint (:constructor %make-breakpoint
393 (hook-fun what kind %info
))
395 ;; This is the function invoked when execution encounters the
396 ;; breakpoint. It takes a frame, the breakpoint, and optionally a
397 ;; list of values. Values are supplied for :FUN-END breakpoints as
398 ;; values to return for the function containing the breakpoint.
399 ;; :FUN-END breakpoint hook functions also take a cookie argument.
400 ;; See the COOKIE-FUN slot.
401 (hook-fun (required-arg) :type function
)
402 ;; CODE-LOCATION or DEBUG-FUN
403 (what nil
:type
(or code-location debug-fun
))
404 ;; :CODE-LOCATION, :FUN-START, or :FUN-END for that kind
405 ;; of breakpoint. :UNKNOWN-RETURN-PARTNER if this is the partner of
406 ;; a :code-location breakpoint at an :UNKNOWN-RETURN code-location.
407 (kind nil
:type
(member :code-location
:fun-start
:fun-end
408 :unknown-return-partner
))
409 ;; Status helps the user and the implementation.
410 (status :inactive
:type
(member :active
:inactive
:deleted
))
411 ;; This is a backpointer to a breakpoint-data.
412 (internal-data nil
:type
(or null breakpoint-data
))
413 ;; With code-locations whose type is :UNKNOWN-RETURN, there are
414 ;; really two breakpoints: one at the multiple-value entry point,
415 ;; and one at the single-value entry point. This slot holds the
416 ;; breakpoint for the other one, or NIL if this isn't at an
417 ;; :UNKNOWN-RETURN code location.
418 (unknown-return-partner nil
:type
(or null breakpoint
))
419 ;; :FUN-END breakpoints use a breakpoint at the :FUN-START
420 ;; to establish the end breakpoint upon function entry. We do this
421 ;; by frobbing the LRA to jump to a special piece of code that
422 ;; breaks and provides the return values for the returnee. This slot
423 ;; points to the start breakpoint, so we can activate, deactivate,
425 (start-helper nil
:type
(or null breakpoint
))
426 ;; This is a hook users supply to get a dynamically unique cookie
427 ;; for identifying :FUN-END breakpoint executions. That is, if
428 ;; there is one :FUN-END breakpoint, but there may be multiple
429 ;; pending calls of its function on the stack. This function takes
430 ;; the cookie, and the hook function takes the cookie too.
431 (cookie-fun nil
:type
(or null function
))
432 ;; This slot users can set with whatever information they find useful.
434 (def!method print-object
((obj breakpoint
) str
)
435 (let ((what (breakpoint-what obj
)))
436 (print-unreadable-object (obj str
:type t
)
441 (debug-fun (debug-fun-name what
)))
444 (debug-fun (breakpoint-kind obj
)))))))
446 (defstruct (compiled-debug-fun
448 (:constructor %make-compiled-debug-fun
449 (compiler-debug-fun component
))
451 ;; compiler's dumped DEBUG-FUN information (unexported)
452 (compiler-debug-fun nil
:type sb
!c
::compiled-debug-fun
)
453 ;; code object (unexported).
455 ;; the :FUN-START breakpoint (if any) used to facilitate
456 ;; function end breakpoints
457 (end-starter nil
:type
(or null breakpoint
)))
461 (def!method print-object
((obj code-location
) str
)
462 (print-unreadable-object (obj str
:type t
)
463 (prin1 (debug-fun-name (code-location-debug-fun obj
))
466 (defstruct (compiled-code-location
467 (:include code-location
)
468 (:constructor make-known-code-location
469 (pc debug-fun %debug-block %tlf-offset %form-number
470 %live-set kind step-info
&aux
(%unknown-p nil
)))
471 (:constructor make-compiled-code-location
(pc debug-fun
))
473 ;; an index into DEBUG-FUN's component slot
475 ;; a bit-vector indexed by a variable's position in
476 ;; DEBUG-FUN-DEBUG-VARS indicating whether the variable has a
477 ;; valid value at this code-location. (unexported).
478 (%live-set
:unparsed
:type
(or simple-bit-vector
(member :unparsed
)))
479 ;; (unexported) To see SB!C::LOCATION-KIND, do
480 ;; (SB!KERNEL:TYPEXPAND 'SB!C::LOCATION-KIND).
481 (kind :unparsed
:type
(or (member :unparsed
) sb
!c
::location-kind
))
482 (step-info :unparsed
:type
(or (member :unparsed
:foo
) simple-string
)))
486 ;;; This is used in FIND-ESCAPED-FRAME and with the bogus components
487 ;;; and LRAs used for :FUN-END breakpoints. When a component's
488 ;;; debug-info slot is :BOGUS-LRA, then the REAL-LRA-SLOT contains the
489 ;;; real component to continue executing, as opposed to the bogus
490 ;;; component which appeared in some frame's LRA location.
491 (defconstant real-lra-slot
492 ;; X86 stores a fixup vector at the first constant slot
493 #!-x86 sb
!vm
:code-constants-offset
494 #!+x86
(1+ sb
!vm
:code-constants-offset
))
496 ;;; These are magically converted by the compiler.
497 (defun current-sp () (current-sp))
498 (defun current-fp () (current-fp))
499 (defun stack-ref (s n
) (stack-ref s n
))
500 (defun %set-stack-ref
(s n value
) (%set-stack-ref s n value
))
501 (defun fun-code-header (fun) (fun-code-header fun
))
502 (defun lra-code-header (lra) (lra-code-header lra
))
503 (defun %make-lisp-obj
(value) (%make-lisp-obj value
))
504 (defun get-lisp-obj-address (thing) (get-lisp-obj-address thing
))
505 (defun fun-word-offset (fun) (fun-word-offset fun
))
507 #!-sb-fluid
(declaim (inline control-stack-pointer-valid-p
))
508 (defun control-stack-pointer-valid-p (x &optional
(aligned t
))
509 (declare (type system-area-pointer x
))
510 (let* (#!-stack-grows-downward-not-upward
512 (descriptor-sap *control-stack-start
*))
513 #!+stack-grows-downward-not-upward
515 (descriptor-sap *control-stack-end
*)))
516 #!-stack-grows-downward-not-upward
517 (and (sap< x
(current-sp))
518 (sap<= control-stack-start x
)
519 (or (not aligned
) (zerop (logand (sap-int x
)
520 (1- (ash 1 sb
!vm
:word-shift
))))))
521 #!+stack-grows-downward-not-upward
522 (and (sap>= x
(current-sp))
523 (sap> control-stack-end x
)
524 (or (not aligned
) (zerop (logand (sap-int x
)
525 (1- (ash 1 sb
!vm
:word-shift
))))))))
527 (declaim (inline component-ptr-from-pc
))
528 (sb!alien
:define-alien-routine component-ptr-from-pc
(system-area-pointer)
529 (pc system-area-pointer
))
531 (declaim (inline valid-lisp-pointer-p
))
532 (sb!alien
:define-alien-routine valid-lisp-pointer-p sb
!alien
:int
533 (pointer system-area-pointer
))
535 (declaim (inline component-from-component-ptr
))
536 (defun component-from-component-ptr (component-ptr)
537 (declare (type system-area-pointer component-ptr
))
538 (make-lisp-obj (logior (sap-int component-ptr
)
539 sb
!vm
:other-pointer-lowtag
)))
541 ;;;; (OR X86 X86-64) support
543 (defun compute-lra-data-from-pc (pc)
544 (declare (type system-area-pointer pc
))
545 (let ((component-ptr (component-ptr-from-pc pc
)))
546 (unless (sap= component-ptr
(int-sap #x0
))
547 (let* ((code (component-from-component-ptr component-ptr
))
548 (code-header-len (* (get-header-data code
) sb
!vm
:n-word-bytes
))
549 (pc-offset (- (sap-int pc
)
550 (- (get-lisp-obj-address code
)
551 sb
!vm
:other-pointer-lowtag
)
553 ;;(format t "c-lra-fpc ~A ~A ~A~%" pc code pc-offset)
554 (values pc-offset code
)))))
559 (defconstant sb
!vm
::nargs-offset
#.sb
!vm
::ecx-offset
)
561 ;;; Check for a valid return address - it could be any valid C/Lisp
564 ;;; XXX Could be a little smarter.
565 #!-sb-fluid
(declaim (inline ra-pointer-valid-p
))
566 (defun ra-pointer-valid-p (ra)
567 (declare (type system-area-pointer ra
))
569 ;; not the first page (which is unmapped)
571 ;; FIXME: Where is this documented? Is it really true of every CPU
572 ;; architecture? Is it even necessarily true in current SBCL?
573 (>= (sap-int ra
) 4096)
574 ;; not a Lisp stack pointer
575 (not (control-stack-pointer-valid-p ra
))))
577 ;;; Try to find a valid previous stack. This is complex on the x86 as
578 ;;; it can jump between C and Lisp frames. To help find a valid frame
579 ;;; it searches backwards.
581 ;;; XXX Should probably check whether it has reached the bottom of the
584 ;;; XXX Should handle interrupted frames, both Lisp and C. At present
585 ;;; it manages to find a fp trail, see linux hack below.
586 (declaim (maybe-inline x86-call-context
))
587 (defun x86-call-context (fp)
588 (declare (type system-area-pointer fp
))
589 (let ((ocfp (sap-ref-sap fp
(sb!vm
::frame-byte-offset ocfp-save-offset
)))
590 (ra (sap-ref-sap fp
(sb!vm
::frame-byte-offset return-pc-save-offset
))))
591 (if (and (control-stack-pointer-valid-p fp
)
593 (control-stack-pointer-valid-p ocfp
)
594 (ra-pointer-valid-p ra
))
596 (values nil
(int-sap 0) (int-sap 0)))))
600 ;;; Return the top frame of the control stack as it was before calling
603 (/noshow0
"entering TOP-FRAME")
604 (compute-calling-frame (descriptor-sap (%caller-frame
))
608 ;;; Flush all of the frames above FRAME, and renumber all the frames
610 (defun flush-frames-above (frame)
611 (setf (frame-up frame
) nil
)
612 (do ((number 0 (1+ number
))
613 (frame frame
(frame-%down frame
)))
614 ((not (frame-p frame
)))
615 (setf (frame-number frame
) number
)))
617 (defun find-saved-frame-down (fp up-frame
)
618 (multiple-value-bind (saved-fp saved-pc
)
619 (sb!alien-internals
:find-saved-fp-and-pc fp
)
621 (compute-calling-frame saved-fp saved-pc up-frame t
))))
623 ;;; Return the frame immediately below FRAME on the stack; or when
624 ;;; FRAME is the bottom of the stack, return NIL.
625 (defun frame-down (frame)
626 (/noshow0
"entering FRAME-DOWN")
627 ;; We have to access the old-fp and return-pc out of frame and pass
628 ;; them to COMPUTE-CALLING-FRAME.
629 (let ((down (frame-%down frame
)))
630 (if (eq down
:unparsed
)
631 (let ((debug-fun (frame-debug-fun frame
)))
632 (/noshow0
"in DOWN :UNPARSED case")
633 (setf (frame-%down frame
)
636 (let (#!-fp-and-pc-standard-save
637 (c-d-f (compiled-debug-fun-compiler-debug-fun
639 (compute-calling-frame
642 frame ocfp-save-offset
643 #!-fp-and-pc-standard-save
644 (sb!c
::compiled-debug-fun-old-fp c-d-f
)
645 #!+fp-and-pc-standard-save
646 sb
!c
:old-fp-passing-offset
))
648 frame lra-save-offset
649 #!-fp-and-pc-standard-save
650 (sb!c
::compiled-debug-fun-return-pc c-d-f
)
651 #!+fp-and-pc-standard-save
652 sb
!c
:return-pc-passing-offset
)
655 (let ((fp (frame-pointer frame
)))
656 (when (control-stack-pointer-valid-p fp
)
658 (multiple-value-bind (ok ra ofp
) (x86-call-context fp
)
660 (compute-calling-frame ofp ra frame
)
661 (find-saved-frame-down fp frame
)))
663 (compute-calling-frame
665 (sap-ref-sap fp
(* ocfp-save-offset
669 (sap-ref-32 fp
(* ocfp-save-offset
670 sb
!vm
:n-word-bytes
)))
672 (stack-ref fp lra-save-offset
)
677 ;;; Get the old FP or return PC out of FRAME. STACK-SLOT is the
678 ;;; standard save location offset on the stack. LOC is the saved
679 ;;; SC-OFFSET describing the main location.
680 (defun get-context-value (frame stack-slot loc
)
681 (declare (type compiled-frame frame
) (type unsigned-byte stack-slot
)
682 (type sb
!c
:sc-offset loc
))
683 (let ((pointer (frame-pointer frame
))
684 (escaped (compiled-frame-escaped frame
)))
686 (sub-access-debug-var-slot pointer loc escaped
)
688 (stack-ref pointer stack-slot
)
692 (stack-ref pointer stack-slot
))
694 (sap-ref-sap pointer
(sb!vm
::frame-byte-offset stack-slot
)))))))
696 (defun (setf get-context-value
) (value frame stack-slot loc
)
697 (declare (type compiled-frame frame
) (type unsigned-byte stack-slot
)
698 (type sb
!c
:sc-offset loc
))
699 (let ((pointer (frame-pointer frame
))
700 (escaped (compiled-frame-escaped frame
)))
702 (sub-set-debug-var-slot pointer loc value escaped
)
704 (setf (stack-ref pointer stack-slot
) value
)
708 (setf (stack-ref pointer stack-slot
) value
))
710 (setf (sap-ref-sap pointer
(sb!vm
::frame-byte-offset stack-slot
))
713 (defun foreign-function-backtrace-name (sap)
714 (let ((name (sap-foreign-symbol sap
)))
716 (format nil
"foreign function: ~A" name
)
717 (format nil
"foreign function: #x~X" (sap-int sap
)))))
719 ;;; This returns a frame for the one existing in time immediately
720 ;;; prior to the frame referenced by current-fp. This is current-fp's
721 ;;; caller or the next frame down the control stack. If there is no
722 ;;; down frame, this returns NIL for the bottom of the stack. UP-FRAME
723 ;;; is the up link for the resulting frame object, and it is null when
724 ;;; we call this to get the top of the stack.
726 ;;; The current frame contains the pointer to the temporally previous
727 ;;; frame we want, and the current frame contains the pc at which we
728 ;;; will continue executing upon returning to that previous frame.
730 ;;; Note: Sometimes LRA is actually a fixnum. This happens when lisp
731 ;;; calls into C. In this case, the code object is stored on the stack
732 ;;; after the LRA, and the LRA is the word offset.
734 (defun compute-calling-frame (caller lra up-frame
&optional savedp
)
735 (declare (type system-area-pointer caller
)
737 (/noshow0
"entering COMPUTE-CALLING-FRAME")
738 (when (control-stack-pointer-valid-p caller
)
740 (multiple-value-bind (code pc-offset escaped
)
742 (multiple-value-bind (word-offset code
)
744 (let ((fp (frame-pointer up-frame
)))
746 (stack-ref fp
(1+ lra-save-offset
))))
747 (values (get-header-data lra
)
748 (lra-code-header lra
)))
751 (* (1+ (- word-offset
(get-header-data code
)))
754 (values :foreign-function
757 (find-escaped-frame caller
))
758 (if (and (code-component-p code
)
759 (eq (%code-debug-info code
) :bogus-lra
))
760 (let ((real-lra (code-header-ref code real-lra-slot
)))
761 (compute-calling-frame caller real-lra up-frame
))
762 (let ((d-fun (case code
764 (make-bogus-debug-fun
765 "undefined function"))
767 (make-bogus-debug-fun
768 (foreign-function-backtrace-name
769 (int-sap (get-lisp-obj-address lra
)))))
771 (make-bogus-debug-fun
772 "bogus stack frame"))
774 (debug-fun-from-pc code pc-offset
)))))
775 (/noshow0
"returning MAKE-COMPILED-FRAME from COMPUTE-CALLING-FRAME")
776 (make-compiled-frame caller up-frame d-fun
777 (code-location-from-pc d-fun pc-offset
779 (if up-frame
(1+ (frame-number up-frame
)) 0)
783 (defun compute-calling-frame (caller ra up-frame
&optional savedp
)
784 (declare (type system-area-pointer caller ra
))
785 (/noshow0
"entering COMPUTE-CALLING-FRAME")
786 (when (control-stack-pointer-valid-p caller
)
788 ;; First check for an escaped frame.
789 (multiple-value-bind (code pc-offset escaped off-stack
)
790 (find-escaped-frame caller
)
793 ;; If it's escaped it may be a function end breakpoint trap.
794 (when (and (code-component-p code
)
795 (eq (%code-debug-info code
) :bogus-lra
))
796 ;; If :bogus-lra grab the real lra.
797 (setq pc-offset
(code-header-ref
798 code
(1+ real-lra-slot
)))
799 (setq code
(code-header-ref code real-lra-slot
))
802 (multiple-value-setq (pc-offset code
)
803 (compute-lra-data-from-pc ra
))
805 (setf code
:foreign-function
807 (let ((d-fun (case code
809 (make-bogus-debug-fun
810 "undefined function"))
812 (make-bogus-debug-fun
813 (foreign-function-backtrace-name ra
)))
815 (make-bogus-debug-fun
816 "bogus stack frame"))
818 (debug-fun-from-pc code pc-offset escaped
)))))
819 (/noshow0
"returning MAKE-COMPILED-FRAME from COMPUTE-CALLING-FRAME")
820 (make-compiled-frame caller up-frame d-fun
821 (code-location-from-pc d-fun pc-offset
823 (if up-frame
(1+ (frame-number up-frame
)) 0)
824 ;; If we have an interrupt-context that's not on
825 ;; our stack at all, and we're computing the
826 ;; from from a saved FP, we're probably looking
827 ;; at an interrupted syscall.
828 (or escaped
(and savedp off-stack
)))))))
830 (defun nth-interrupt-context (n)
831 (declare (type (unsigned-byte 32) n
)
832 (optimize (speed 3) (safety 0)))
833 (sb!alien
:sap-alien
(sb!vm
::current-thread-offset-sap
834 (+ sb
!vm
::thread-interrupt-contexts-offset
839 ;;; On SB-DYNAMIC-CORE symbols which come from the runtime go through
840 ;;; an indirection table, but the debugger needs to know the actual
842 (defun static-foreign-symbol-address (name)
844 (find-dynamic-foreign-symbol-address name
)
846 (foreign-symbol-address name
))
849 (defun static-foreign-symbol-sap (name)
850 (int-sap (static-foreign-symbol-address name
)))
853 (defun find-escaped-frame (frame-pointer)
854 (declare (type system-area-pointer frame-pointer
))
855 (/noshow0
"entering FIND-ESCAPED-FRAME")
856 (dotimes (index *free-interrupt-context-index
* (values nil
0 nil
))
857 (let* ((context (nth-interrupt-context index
))
858 (cfp (int-sap (sb!vm
:context-register context sb
!vm
::cfp-offset
))))
859 (/noshow0
"got CONTEXT")
860 (unless (control-stack-pointer-valid-p cfp
)
861 (return (values nil nil nil t
)))
862 (when (sap= frame-pointer cfp
)
864 (/noshow0
"in WITHOUT-GCING")
865 (let* ((component-ptr (component-ptr-from-pc
866 (sb!vm
:context-pc context
)))
867 (code (unless (sap= component-ptr
(int-sap #x0
))
868 (component-from-component-ptr component-ptr
))))
869 (/noshow0
"got CODE")
871 ;; KLUDGE: Detect undefined functions by a range-check
872 ;; against the trampoline address and the following
873 ;; function in the runtime.
874 (if (< (static-foreign-symbol-address "undefined_tramp")
875 (sap-int (sb!vm
:context-pc context
))
876 (static-foreign-symbol-address #!+x86
"closure_tramp"
877 #!+x86-64
"alloc_tramp"))
878 (return (values :undefined-function
0 context
))
879 (return (values code
0 context
))))
880 (let* ((code-header-len (* (get-header-data code
)
883 (- (sap-int (sb!vm
:context-pc context
))
884 (- (get-lisp-obj-address code
)
885 sb
!vm
:other-pointer-lowtag
)
887 (/noshow
"got PC-OFFSET")
888 (unless (<= 0 pc-offset
(%code-code-size code
))
889 ;; We were in an assembly routine. Therefore, use the
892 ;; FIXME: Should this be WARN or ERROR or what?
893 (format t
"** pc-offset ~S not in code obj ~S?~%"
895 (/noshow0
"returning from FIND-ESCAPED-FRAME")
897 (values code pc-offset context
)))))))))
900 (defun find-escaped-frame (frame-pointer)
901 (declare (type system-area-pointer frame-pointer
))
902 (/noshow0
"entering FIND-ESCAPED-FRAME")
903 (dotimes (index *free-interrupt-context-index
* (values nil
0 nil
))
904 (let ((scp (nth-interrupt-context index
)))
906 (when (= (sap-int frame-pointer
)
907 (sb!vm
:context-register scp sb
!vm
::cfp-offset
))
909 (/noshow0
"in WITHOUT-GCING")
910 (let ((code (code-object-from-bits
911 (sb!vm
:context-register scp sb
!vm
::code-offset
))))
912 (/noshow0
"got CODE")
914 (return (values code
0 scp
)))
915 (let* ((code-header-len (* (get-header-data code
)
918 (- (sap-int (sb!vm
:context-pc scp
))
919 (- (get-lisp-obj-address code
)
920 sb
!vm
:other-pointer-lowtag
)
922 (let ((code-size (%code-code-size code
)))
923 (unless (<= 0 pc-offset code-size
)
924 ;; We were in an assembly routine.
925 (multiple-value-bind (new-pc-offset computed-return
)
926 (find-pc-from-assembly-fun code scp
)
927 (setf pc-offset new-pc-offset
)
928 (unless (<= 0 pc-offset code-size
)
930 "Set PC-OFFSET to zero and continue backtrace."
933 "~@<PC-OFFSET (~D) not in code object. Frame details:~
934 ~2I~:@_PC: #X~X~:@_CODE: ~S~:@_CODE FUN: ~S~:@_LRA: ~
935 #X~X~:@_COMPUTED RETURN: #X~X.~:>"
938 (sap-int (sb!vm
:context-pc scp
))
940 (%code-entry-points code
)
942 (sb!vm
:context-register scp sb
!vm
::lra-offset
)
944 (stack-ref frame-pointer lra-save-offset
)
946 ;; We failed to pinpoint where PC is, but set
947 ;; pc-offset to 0 to keep the backtrace from
949 (setf pc-offset
0)))))
950 (/noshow0
"returning from FIND-ESCAPED-FRAME")
952 (if (eq (%code-debug-info code
) :bogus-lra
)
953 (let ((real-lra (code-header-ref code
955 (values (lra-code-header real-lra
)
956 (get-header-data real-lra
)
958 (values code pc-offset scp
))))))))))
961 (defun find-pc-from-assembly-fun (code scp
)
962 "Finds the PC for the return from an assembly routine properly.
963 For some architectures (such as PPC) this will not be the $LRA
965 (let ((return-machine-address (sb!vm
::return-machine-address scp
))
966 (code-header-len (* (get-header-data code
) sb
!vm
:n-word-bytes
)))
967 (values (- return-machine-address
968 (- (get-lisp-obj-address code
)
969 sb
!vm
:other-pointer-lowtag
)
971 return-machine-address
)))
973 ;;; Find the code object corresponding to the object represented by
974 ;;; bits and return it. We assume bogus functions correspond to the
975 ;;; undefined-function.
977 (defun code-object-from-bits (bits)
978 (declare (type word bits
))
979 (let ((object (make-lisp-obj bits nil
)))
980 (if (functionp object
)
981 (or (fun-code-header object
)
983 (let ((lowtag (lowtag-of object
)))
984 (when (= lowtag sb
!vm
:other-pointer-lowtag
)
985 (let ((widetag (widetag-of object
)))
986 (cond ((= widetag sb
!vm
:code-header-widetag
)
988 ((= widetag sb
!vm
:return-pc-header-widetag
)
989 (lra-code-header object
))
995 (defun find-assembly-routine (component pc
)
996 (let* ((start (sap-int (code-instructions component
)))
1000 (loop for name being the hash-key of sb
!fasl
:*assembler-routines
*
1001 using
(hash-value address
)
1002 when
(and (<= start address end
)
1004 (< (- end address
) min-diff
)))
1005 do
(setf min-name name
1006 min-diff
(- end address
)))
1009 ;;; This returns a COMPILED-DEBUG-FUN for COMPONENT and PC. We fetch the
1010 ;;; SB!C::DEBUG-INFO and run down its FUN-MAP to get a
1011 ;;; SB!C::COMPILED-DEBUG-FUN from the PC. The result only needs to
1012 ;;; reference the COMPONENT, for function constants, and the
1013 ;;; SB!C::COMPILED-DEBUG-FUN.
1014 (defun debug-fun-from-pc (component pc
&optional
(escaped t
))
1015 (let ((info (%code-debug-info component
)))
1018 (make-bogus-debug-fun (or (find-assembly-routine component pc
)
1019 "no debug information for frame")))
1020 ((eq info
:bogus-lra
)
1021 (make-bogus-debug-fun "function end breakpoint"))
1023 (let* ((fun-map (sb!c
::compiled-debug-info-fun-map info
))
1024 (len (length fun-map
)))
1025 (declare (type simple-vector fun-map
))
1027 (make-compiled-debug-fun (svref fun-map
0) component
)
1030 (>= pc
(sb!c
::compiled-debug-fun-elsewhere-pc
1031 (svref fun-map
0)))))
1032 (declare (type sb
!int
:index i
))
1035 (let ((next-pc (if elsewhere-p
1036 (sb!c
::compiled-debug-fun-elsewhere-pc
1037 (svref fun-map
(1+ i
)))
1038 (svref fun-map i
))))
1041 ;; Non-escaped frame means that this frame calls something.
1042 ;; And the PC points to where something should return.
1043 ;; The return adress may be in the next
1044 ;; function, e.g. in local tail calls the
1045 ;; function will be entered just after the
1047 ;; See debug.impure.lisp/:local-tail-call for a test-case
1049 (return (make-compiled-debug-fun
1050 (svref fun-map
(1- i
))
1054 ;;; This returns a code-location for the COMPILED-DEBUG-FUN,
1055 ;;; DEBUG-FUN, and the pc into its code vector. If we stopped at a
1056 ;;; breakpoint, find the CODE-LOCATION for that breakpoint. Otherwise,
1057 ;;; make an :UNSURE code location, so it can be filled in when we
1058 ;;; figure out what is going on.
1059 (defun code-location-from-pc (debug-fun pc escaped
)
1060 (or (and (compiled-debug-fun-p debug-fun
)
1062 (let ((data (breakpoint-data
1063 (compiled-debug-fun-component debug-fun
)
1065 (when (and data
(breakpoint-data-breakpoints data
))
1066 (let ((what (breakpoint-what
1067 (first (breakpoint-data-breakpoints data
)))))
1068 (when (compiled-code-location-p what
)
1070 (make-compiled-code-location pc debug-fun
)))
1072 ;;; Return an alist mapping catch tags to CODE-LOCATIONs. These are
1073 ;;; CODE-LOCATIONs at which execution would continue with frame as the
1074 ;;; top frame if someone threw to the corresponding tag.
1075 (defun frame-catches (frame)
1076 (let ((catch (descriptor-sap sb
!vm
:*current-catch-block
*))
1077 (reversed-result nil
)
1078 (fp (frame-pointer frame
)))
1079 (loop until
(zerop (sap-int catch
))
1080 finally
(return (nreverse reversed-result
))
1085 (* sb
!vm
:catch-block-current-cont-slot
1086 sb
!vm
:n-word-bytes
))
1090 (* sb
!vm
:catch-block-current-cont-slot
1091 sb
!vm
:n-word-bytes
))))
1092 (let* (#!-
(or x86 x86-64
)
1093 (lra (stack-ref catch sb
!vm
:catch-block-entry-pc-slot
))
1096 catch
(* sb
!vm
:catch-block-entry-pc-slot
1097 sb
!vm
:n-word-bytes
)))
1100 (stack-ref catch sb
!vm
:catch-block-current-code-slot
))
1102 (component (component-from-component-ptr
1103 (component-ptr-from-pc ra
)))
1106 (* (- (1+ (get-header-data lra
))
1107 (get-header-data component
))
1111 (- (get-lisp-obj-address component
)
1112 sb
!vm
:other-pointer-lowtag
)
1113 (* (get-header-data component
) sb
!vm
:n-word-bytes
))))
1114 (push (cons #!-
(or x86 x86-64
)
1115 (stack-ref catch sb
!vm
:catch-block-tag-slot
)
1118 (sap-ref-word catch
(* sb
!vm
:catch-block-tag-slot
1119 sb
!vm
:n-word-bytes
)))
1120 (make-compiled-code-location
1121 offset
(frame-debug-fun frame
)))
1126 (* sb
!vm
:catch-block-previous-catch-slot
1127 sb
!vm
:n-word-bytes
))
1131 (* sb
!vm
:catch-block-previous-catch-slot
1132 sb
!vm
:n-word-bytes
)))))))
1134 ;;; Modify the value of the OLD-TAG catches in FRAME to NEW-TAG
1135 (defun replace-frame-catch-tag (frame old-tag new-tag
)
1136 (let ((catch (descriptor-sap sb
!vm
:*current-catch-block
*))
1137 (fp (frame-pointer frame
)))
1138 (loop until
(zerop (sap-int catch
))
1142 (* sb
!vm
:catch-block-current-cont-slot
1143 sb
!vm
:n-word-bytes
))
1147 (* sb
!vm
:catch-block-current-cont-slot
1148 sb
!vm
:n-word-bytes
))))
1151 (stack-ref catch sb
!vm
:catch-block-tag-slot
)
1154 (sap-ref-word catch
(* sb
!vm
:catch-block-tag-slot
1155 sb
!vm
:n-word-bytes
)))))
1156 (when (eq current-tag old-tag
)
1158 (setf (stack-ref catch sb
!vm
:catch-block-tag-slot
) new-tag
)
1160 (setf (sap-ref-word catch
(* sb
!vm
:catch-block-tag-slot
1161 sb
!vm
:n-word-bytes
))
1162 (get-lisp-obj-address new-tag
)))))
1166 (* sb
!vm
:catch-block-previous-catch-slot
1167 sb
!vm
:n-word-bytes
))
1171 (* sb
!vm
:catch-block-previous-catch-slot
1172 sb
!vm
:n-word-bytes
)))))))
1176 ;;;; operations on DEBUG-FUNs
1178 ;;; Execute the forms in a context with BLOCK-VAR bound to each
1179 ;;; DEBUG-BLOCK in DEBUG-FUN successively. Result is an optional
1180 ;;; form to execute for return values, and DO-DEBUG-FUN-BLOCKS
1181 ;;; returns nil if there is no result form. This signals a
1182 ;;; NO-DEBUG-BLOCKS condition when the DEBUG-FUN lacks
1183 ;;; DEBUG-BLOCK information.
1184 (defmacro do-debug-fun-blocks
((block-var debug-fun
&optional result
)
1186 (let ((blocks (gensym))
1188 `(let ((,blocks
(debug-fun-debug-blocks ,debug-fun
)))
1189 (declare (simple-vector ,blocks
))
1190 (dotimes (,i
(length ,blocks
) ,result
)
1191 (let ((,block-var
(svref ,blocks
,i
)))
1194 ;;; Execute body in a context with VAR bound to each DEBUG-VAR in
1195 ;;; DEBUG-FUN. This returns the value of executing result (defaults to
1196 ;;; nil). This may iterate over only some of DEBUG-FUN's variables or
1197 ;;; none depending on debug policy; for example, possibly the
1198 ;;; compilation only preserved argument information.
1199 (defmacro do-debug-fun-vars
((var debug-fun
&optional result
) &body body
)
1200 (let ((vars (gensym))
1202 `(let ((,vars
(debug-fun-debug-vars ,debug-fun
)))
1203 (declare (type (or null simple-vector
) ,vars
))
1205 (dotimes (,i
(length ,vars
) ,result
)
1206 (let ((,var
(svref ,vars
,i
)))
1210 ;;; Return the object of type FUNCTION associated with the DEBUG-FUN,
1211 ;;; or NIL if the function is unavailable or is non-existent as a user
1212 ;;; callable function object.
1213 (defun debug-fun-fun (debug-fun)
1214 (let ((cached-value (debug-fun-%function debug-fun
)))
1215 (if (eq cached-value
:unparsed
)
1216 (setf (debug-fun-%function debug-fun
)
1217 (etypecase debug-fun
1220 (compiled-debug-fun-component debug-fun
))
1222 (sb!c
::compiled-debug-fun-start-pc
1223 (compiled-debug-fun-compiler-debug-fun debug-fun
))))
1224 (do ((entry (%code-entry-points component
)
1225 (%simple-fun-next entry
)))
1228 (sb!c
::compiled-debug-fun-start-pc
1229 (compiled-debug-fun-compiler-debug-fun
1230 (fun-debug-fun entry
))))
1232 (bogus-debug-fun nil
)))
1235 ;;; Return the name of the function represented by DEBUG-FUN. This may
1236 ;;; be a string or a cons; do not assume it is a symbol.
1237 (defun debug-fun-name (debug-fun &optional
(pretty t
))
1238 (declare (type debug-fun debug-fun
) (ignorable pretty
))
1239 (etypecase debug-fun
1241 (let ((name (sb!c
::compiled-debug-fun-name
1242 (compiled-debug-fun-compiler-debug-fun debug-fun
))))
1243 ;; Frames named (.EVAL. special-operator) should show the operator name
1244 ;; in backtraces, but if the debugger needs to detect that the frame is
1245 ;; interpreted for other purposes, it can specify PRETTY = NIL.
1246 (cond #!+sb-fasteval
1247 ((and (typep name
'(cons (eql sb
!interpreter
::.eval.
)))
1249 (if (singleton-p (cdr name
)) (cadr name
) (cdr name
)))
1252 (bogus-debug-fun-%name debug-fun
))))
1254 (defun interrupted-frame-error (frame)
1255 (when (and (compiled-frame-p frame
)
1256 (compiled-frame-escaped frame
)
1257 sb
!kernel
::*current-internal-error
*
1258 (array-in-bounds-p sb
!c
:+backend-internal-errors
+
1259 sb
!kernel
::*current-internal-error
*))
1260 (cdr (svref sb
!c
:+backend-internal-errors
+
1261 sb
!kernel
::*current-internal-error
*))))
1263 (defun tl-invalid-arg-count-error-p (frame)
1264 (and (eq (interrupted-frame-error frame
)
1265 'invalid-arg-count-error
)
1266 (eq (debug-fun-kind (frame-debug-fun frame
))
1269 ;; Return the name of the closure, if named, otherwise nil.
1270 (defun debug-fun-closure-name (debug-fun frame
)
1271 (unless (typep debug-fun
'compiled-debug-fun
)
1272 (return-from debug-fun-closure-name nil
))
1273 (let ((compiler-debug-fun (compiled-debug-fun-compiler-debug-fun debug-fun
)))
1275 ;; Frames named (.APPLY. something) are interpreted function applicators.
1276 ;; Show them as the name of the interpreted function being applied.
1278 ((let ((name (sb!c
::compiled-debug-fun-name compiler-debug-fun
)))
1279 (when (typep name
'(cons (eql sb
!interpreter
::.apply.
)))
1280 ;; Find a variable named FUN.
1281 (awhen (car (debug-fun-symbol-vars debug-fun
'sb
!interpreter
::fun
))
1282 (let ((val (debug-var-value it frame
))) ; Ensure it's a function
1283 (when (typep val
'sb
!interpreter
:interpreted-function
)
1284 (sb!interpreter
:fun-name val
))))))) ; Get its name
1285 ((sb!c
::compiled-debug-fun-closure-save compiler-debug-fun
)
1287 (sb!impl
::closure-name
1288 #!+precise-arg-count-error
1289 (if (tl-invalid-arg-count-error-p frame
)
1290 (sub-access-debug-var-slot (frame-pointer frame
)
1292 (compiled-frame-escaped frame
))
1293 (sub-access-debug-var-slot (frame-pointer frame
) it
))
1294 #!-precise-arg-count-error
1295 (sub-access-debug-var-slot (frame-pointer frame
) it
))))
1297 ;; The logic in CLEAN-FRAME-CALL is based on the frame name,
1298 ;; so if the simple-fun is named (XEP mumble) then the closure
1299 ;; needs to pretend to be named similarly.
1300 (let ((simple-fun-name
1301 (sb!di
:debug-fun-name debug-fun
)))
1302 (if (and (listp simple-fun-name
)
1303 (eq (car simple-fun-name
) 'sb
!c
::xep
))
1304 `(sb!c
::xep
,closure-name
)
1305 closure-name
))))))))
1307 ;;; Return a DEBUG-FUN that represents debug information for FUN.
1308 (defun fun-debug-fun (fun)
1309 (declare (type function fun
))
1310 (let ((simple-fun (%fun-fun fun
)))
1311 (let* ((name (%simple-fun-name simple-fun
))
1312 (component (fun-code-header simple-fun
))
1315 (and (sb!c
::compiled-debug-fun-p x
)
1316 (eq (sb!c
::compiled-debug-fun-name x
) name
)
1317 (eq (sb!c
::compiled-debug-fun-kind x
) nil
)))
1318 (sb!c
::compiled-debug-info-fun-map
1319 (%code-debug-info component
)))))
1321 (make-compiled-debug-fun res component
)
1322 ;; KLUDGE: comment from CMU CL:
1323 ;; This used to be the non-interpreted branch, but
1324 ;; William wrote it to return the debug-fun of fun's XEP
1325 ;; instead of fun's debug-fun. The above code does this
1326 ;; more correctly, but it doesn't get or eliminate all
1327 ;; appropriate cases. It mostly works, and probably
1328 ;; works for all named functions anyway.
1330 (debug-fun-from-pc component
1331 (* (- (fun-word-offset simple-fun
)
1332 (get-header-data component
))
1333 sb
!vm
:n-word-bytes
))))))
1335 ;;; Return the kind of the function, which is one of :OPTIONAL,
1336 ;;; :EXTERNAL, :TOPLEVEL, :CLEANUP, or NIL.
1337 (defun debug-fun-kind (debug-fun)
1338 ;; FIXME: This "is one of" information should become part of the function
1339 ;; declamation, not just a doc string
1340 (etypecase debug-fun
1342 (sb!c
::compiled-debug-fun-kind
1343 (compiled-debug-fun-compiler-debug-fun debug-fun
)))
1347 ;;; Is there any variable information for DEBUG-FUN?
1348 (defun debug-var-info-available (debug-fun)
1349 (not (not (debug-fun-debug-vars debug-fun
))))
1351 ;;; Return a list of DEBUG-VARs in DEBUG-FUN having the same name
1352 ;;; and package as SYMBOL. If SYMBOL is uninterned, then this returns
1353 ;;; a list of DEBUG-VARs without package names and with the same name
1354 ;;; as symbol. The result of this function is limited to the
1355 ;;; availability of variable information in DEBUG-FUN; for
1356 ;;; example, possibly DEBUG-FUN only knows about its arguments.
1357 (defun debug-fun-symbol-vars (debug-fun symbol
)
1358 (let ((vars (ambiguous-debug-vars debug-fun
(symbol-name symbol
)))
1359 (package (and (symbol-package symbol
)
1360 (package-name (symbol-package symbol
)))))
1361 (delete-if (if (stringp package
)
1363 (let ((p (debug-var-package-name var
)))
1364 (or (not (stringp p
))
1365 (string/= p package
))))
1367 (stringp (debug-var-package-name var
))))
1370 ;;; Return a list of DEBUG-VARs in DEBUG-FUN whose names contain
1371 ;;; NAME-PREFIX-STRING as an initial substring. The result of this
1372 ;;; function is limited to the availability of variable information in
1373 ;;; debug-fun; for example, possibly debug-fun only knows
1374 ;;; about its arguments.
1375 (defun ambiguous-debug-vars (debug-fun name-prefix-string
)
1376 (declare (simple-string name-prefix-string
))
1377 (let ((variables (debug-fun-debug-vars debug-fun
)))
1378 (declare (type (or null simple-vector
) variables
))
1380 (let* ((len (length variables
))
1381 (prefix-len (length name-prefix-string
))
1382 (pos (find-var name-prefix-string variables len
))
1385 ;; Find names from pos to variable's len that contain prefix.
1386 (do ((i pos
(1+ i
)))
1388 (let* ((var (svref variables i
))
1389 (name (debug-var-symbol-name var
))
1390 (name-len (length name
)))
1391 (declare (simple-string name
))
1392 (when (/= (or (string/= name-prefix-string name
1393 :end1 prefix-len
:end2 name-len
)
1398 (setq res
(nreverse res
)))
1401 ;;; This returns a position in VARIABLES for one containing NAME as an
1402 ;;; initial substring. END is the length of VARIABLES if supplied.
1403 (defun find-var (name variables
&optional end
)
1404 (declare (simple-vector variables
)
1405 (simple-string name
))
1406 (let ((name-len (length name
)))
1407 (position name variables
1409 (let* ((y (debug-var-symbol-name y
))
1411 (declare (simple-string y
))
1412 (and (>= y-len name-len
)
1413 (string= x y
:end1 name-len
:end2 name-len
))))
1414 :end
(or end
(length variables
)))))
1416 ;;; Return a list representing the lambda-list for DEBUG-FUN. The
1417 ;;; list has the following structure:
1418 ;;; (required-var1 required-var2
1420 ;;; (:optional var3 suppliedp-var4)
1421 ;;; (:optional var5)
1423 ;;; (:rest var6) (:rest var7)
1425 ;;; (:keyword keyword-symbol var8 suppliedp-var9)
1426 ;;; (:keyword keyword-symbol var10)
1429 ;;; Each VARi is a DEBUG-VAR; however it may be the symbol :DELETED if
1430 ;;; it is unreferenced in DEBUG-FUN. This signals a
1431 ;;; LAMBDA-LIST-UNAVAILABLE condition when there is no argument list
1433 (defun debug-fun-lambda-list (debug-fun)
1434 (etypecase debug-fun
1435 (compiled-debug-fun (compiled-debug-fun-lambda-list debug-fun
))
1436 (bogus-debug-fun nil
)))
1438 ;;; Note: If this has to compute the lambda list, it caches it in DEBUG-FUN.
1439 (defun compiled-debug-fun-lambda-list (debug-fun)
1440 (let ((lambda-list (debug-fun-%lambda-list debug-fun
)))
1441 (cond ((eq lambda-list
:unparsed
)
1442 (multiple-value-bind (args argsp
)
1443 (parse-compiled-debug-fun-lambda-list debug-fun
)
1444 (setf (debug-fun-%lambda-list debug-fun
) args
)
1447 (debug-signal 'lambda-list-unavailable
1448 :debug-fun debug-fun
))))
1450 ((bogus-debug-fun-p debug-fun
)
1452 ((sb!c
::compiled-debug-fun-arguments
1453 (compiled-debug-fun-compiler-debug-fun debug-fun
))
1454 ;; If the packed information is there (whether empty or not) as
1455 ;; opposed to being nil, then returned our cached value (nil).
1458 ;; Our cached value is nil, and the packed lambda-list information
1459 ;; is nil, so we don't have anything available.
1460 (debug-signal 'lambda-list-unavailable
1461 :debug-fun debug-fun
)))))
1463 ;;; COMPILED-DEBUG-FUN-LAMBDA-LIST calls this when a
1464 ;;; COMPILED-DEBUG-FUN has no lambda list information cached. It
1465 ;;; returns the lambda list as the first value and whether there was
1466 ;;; any argument information as the second value. Therefore,
1467 ;;; (VALUES NIL T) means there were no arguments, but (VALUES NIL NIL)
1468 ;;; means there was no argument information.
1469 (defun parse-compiled-debug-fun-lambda-list (debug-fun)
1470 (let ((args (sb!c
::compiled-debug-fun-arguments
1471 (compiled-debug-fun-compiler-debug-fun debug-fun
))))
1476 (values (coerce (debug-fun-debug-vars debug-fun
) 'list
)
1479 (values (parse-compiled-debug-fun-lambda-list/args-available
1480 (debug-fun-debug-vars debug-fun
) args
)
1483 (defun parse-compiled-debug-fun-lambda-list/args-available
(vars args
)
1484 (declare (type (or null simple-vector
) vars
))
1490 (flet ((push-var (tag-and-info &optional var-count
)
1492 (append tag-and-info
1493 (loop :repeat var-count
:collect
1494 (compiled-debug-fun-lambda-list-var
1495 args
(incf i
) vars
)))
1498 (var-or-deleted (index-or-deleted)
1499 (if (eq index-or-deleted
'sb
!c
::deleted
)
1501 (svref vars index-or-deleted
))))
1504 :for ele
= (aref args i
) :do
1506 ((eq ele
'sb
!c
::optional-args
)
1508 ((eq ele
'sb
!c
::rest-arg
)
1509 (push-var '(:rest
) 1))
1510 ;; The next two args are the &MORE arg context and
1512 ((eq ele
'sb
!c
::more-arg
)
1513 (push-var '(:more
) 2))
1514 ;; SUPPLIED-P var immediately following keyword or
1515 ;; optional. Stick the extra var in the result element
1516 ;; representing the keyword or optional, which is the
1518 ((eq ele
'sb
!c
::supplied-p
)
1519 (push-var (pop result
) 1))
1520 ;; The keyword of a keyword parameter. Store it so the next
1521 ;; element can be used to form a (:keyword KEYWORD VALUE)
1523 ((typep ele
'(and symbol
(not (eql sb
!c
::deleted
))))
1525 ;; The previous element was the keyword of a keyword
1526 ;; parameter and is stored in KEYWORD. The current element
1527 ;; is the index of the value (or a deleted
1528 ;; marker). Construct and push the complete entry.
1530 (push-var (list :keyword keyword
(var-or-deleted ele
))))
1531 ;; We saw an optional marker, so the following non-symbols
1532 ;; are indexes (or deleted markers) indicating optional
1535 (push-var (list :optional
(var-or-deleted ele
))))
1536 ;; Deleted required, optional or keyword argument.
1537 ((eq ele
'sb
!c
::deleted
)
1538 (push-var :deleted
))
1539 ;; Required arg at beginning of args array.
1541 (push-var (svref vars ele
))))
1543 :finally
(return (nreverse result
))))))
1545 ;;; This is used in COMPILED-DEBUG-FUN-LAMBDA-LIST.
1546 (defun compiled-debug-fun-lambda-list-var (args i vars
)
1547 (declare (type (simple-array * (*)) args
)
1548 (simple-vector vars
))
1549 (let ((ele (aref args i
)))
1550 (cond ((not (symbolp ele
)) (svref vars ele
))
1551 ((eq ele
'sb
!c
::deleted
) :deleted
)
1552 (t (error "malformed arguments description")))))
1554 (defun compiled-debug-fun-debug-info (debug-fun)
1555 (%code-debug-info
(compiled-debug-fun-component debug-fun
)))
1557 ;;;; unpacking variable and basic block data
1559 (defvar *parsing-buffer
*
1560 (make-array 20 :adjustable t
:fill-pointer t
))
1561 (defvar *other-parsing-buffer
*
1562 (make-array 20 :adjustable t
:fill-pointer t
))
1563 ;;; PARSE-DEBUG-BLOCKS and PARSE-DEBUG-VARS
1564 ;;; use this to unpack binary encoded information. It returns the
1565 ;;; values returned by the last form in body.
1567 ;;; This binds buffer-var to *parsing-buffer*, makes sure it starts at
1568 ;;; element zero, and makes sure if we unwind, we nil out any set
1569 ;;; elements for GC purposes.
1571 ;;; This also binds other-var to *other-parsing-buffer* when it is
1572 ;;; supplied, making sure it starts at element zero and that we nil
1573 ;;; out any elements if we unwind.
1575 ;;; This defines the local macro RESULT that takes a buffer, copies
1576 ;;; its elements to a resulting simple-vector, nil's out elements, and
1577 ;;; restarts the buffer at element zero. RESULT returns the
1579 (eval-when (:compile-toplevel
:execute
)
1580 (sb!xc
:defmacro with-parsing-buffer
((buffer-var &optional other-var
)
1582 (let ((len (gensym))
1585 (let ((,buffer-var
*parsing-buffer
*)
1586 ,@(if other-var
`((,other-var
*other-parsing-buffer
*))))
1587 (setf (fill-pointer ,buffer-var
) 0)
1588 ,@(if other-var
`((setf (fill-pointer ,other-var
) 0)))
1589 (macrolet ((result (buf)
1590 `(let* ((,',len
(length ,buf
))
1591 (,',res
(make-array ,',len
)))
1592 (replace ,',res
,buf
:end1
,',len
:end2
,',len
)
1593 (fill ,buf nil
:end
,',len
)
1594 (setf (fill-pointer ,buf
) 0)
1597 (fill *parsing-buffer
* nil
)
1598 ,@(if other-var
`((fill *other-parsing-buffer
* nil
))))))
1601 ;;; The argument is a debug internals structure. This returns the
1602 ;;; DEBUG-BLOCKs for DEBUG-FUN, regardless of whether we have unpacked
1603 ;;; them yet. It signals a NO-DEBUG-BLOCKS condition if it can't
1604 ;;; return the blocks.
1605 (defun debug-fun-debug-blocks (debug-fun)
1606 (let ((blocks (debug-fun-blocks debug-fun
)))
1607 (cond ((eq blocks
:unparsed
)
1608 (setf (debug-fun-blocks debug-fun
)
1609 (parse-debug-blocks debug-fun
))
1610 (unless (debug-fun-blocks debug-fun
)
1611 (debug-signal 'no-debug-blocks
1612 :debug-fun debug-fun
))
1613 (debug-fun-blocks debug-fun
))
1616 (debug-signal 'no-debug-blocks
1617 :debug-fun debug-fun
)))))
1619 ;;; Return a SIMPLE-VECTOR of DEBUG-BLOCKs or NIL. NIL indicates there
1620 ;;; was no basic block information.
1621 (defun parse-debug-blocks (debug-fun)
1622 (etypecase debug-fun
1624 (parse-compiled-debug-blocks debug-fun
))
1626 (debug-signal 'no-debug-blocks
:debug-fun debug-fun
))))
1628 ;;; This does some of the work of PARSE-DEBUG-BLOCKS.
1629 (defun parse-compiled-debug-blocks (debug-fun)
1630 (let* ((var-count (length (debug-fun-debug-vars debug-fun
)))
1631 (compiler-debug-fun (compiled-debug-fun-compiler-debug-fun
1633 (blocks (sb!c
::compiled-debug-fun-blocks compiler-debug-fun
))
1634 ;; KLUDGE: 8 is a hard-wired constant in the compiler for the
1635 ;; element size of the packed binary representation of the
1637 (live-set-len (ceiling var-count
8))
1638 (tlf-number (sb!c
::compiled-debug-fun-tlf-number compiler-debug-fun
))
1639 (elsewhere-pc (sb!c
::compiled-debug-fun-elsewhere-pc compiler-debug-fun
)))
1641 (return-from parse-compiled-debug-blocks nil
))
1642 (macrolet ((aref+ (a i
) `(prog1 (aref ,a
,i
) (incf ,i
))))
1643 (with-parsing-buffer (blocks-buffer locations-buffer
)
1645 (len (length blocks
))
1648 (when (>= i len
) (return))
1649 (let ((block (make-compiled-debug-block)))
1650 (dotimes (k (sb!c
:read-var-integer blocks i
))
1651 (let ((kind (svref sb
!c
::*compiled-code-location-kinds
*
1654 (sb!c
:read-var-integer blocks i
)))
1655 (tlf-offset (or tlf-number
1656 (sb!c
:read-var-integer blocks i
)))
1657 (form-number (sb!c
:read-var-integer blocks i
))
1658 (live-set (sb!c
:read-packed-bit-vector
1659 live-set-len blocks i
))
1660 (step-info (sb!c
:read-var-string blocks i
)))
1661 (vector-push-extend (make-known-code-location
1662 pc debug-fun block tlf-offset
1663 form-number live-set kind
1667 (setf (compiled-debug-block-code-locations block
)
1668 (result locations-buffer
)
1669 (compiled-debug-block-elsewhere-p block
)
1670 (> last-pc elsewhere-pc
))
1671 (vector-push-extend block blocks-buffer
))))
1672 (result blocks-buffer
)))))
1674 ;;; The argument is a debug internals structure. This returns NIL if
1675 ;;; there is no variable information. It returns an empty
1676 ;;; simple-vector if there were no locals in the function. Otherwise
1677 ;;; it returns a SIMPLE-VECTOR of DEBUG-VARs.
1678 (defun debug-fun-debug-vars (debug-fun)
1679 (let ((vars (debug-fun-%debug-vars debug-fun
)))
1680 (if (eq vars
:unparsed
)
1681 (setf (debug-fun-%debug-vars debug-fun
)
1682 (etypecase debug-fun
1684 (parse-compiled-debug-vars debug-fun
))
1685 (bogus-debug-fun nil
)))
1688 ;;; VARS is the parsed variables for a minimal debug function. We need
1689 ;;; to assign names of the form ARG-NNN. We must pad with leading
1690 ;;; zeros, since the arguments must be in alphabetical order.
1691 (defun assign-minimal-var-names (vars)
1692 (declare (simple-vector vars
))
1693 (let* ((len (length vars
))
1694 (width (length (format nil
"~W" (1- len
)))))
1696 (without-package-locks
1697 (setf (compiled-debug-var-symbol (svref vars i
))
1698 (intern (format nil
"ARG-~V,'0D" width i
)
1699 ;; The cross-compiler won't dump literal package
1700 ;; references because the target package objects
1701 ;; aren't created until partway through
1702 ;; cold-init. In lieu of adding smarts to the
1703 ;; build framework to handle this, we use an
1704 ;; explicit load-time-value form.
1705 (load-time-value (find-package "SB!DEBUG"))))))))
1707 ;;; Parse the packed representation of DEBUG-VARs from
1708 ;;; DEBUG-FUN's SB!C::COMPILED-DEBUG-FUN, returning a vector
1709 ;;; of DEBUG-VARs, or NIL if there was no information to parse.
1710 (defun parse-compiled-debug-vars (debug-fun)
1711 (let* ((cdebug-fun (compiled-debug-fun-compiler-debug-fun
1713 (packed-vars (sb!c
::compiled-debug-fun-vars cdebug-fun
))
1714 (args-minimal (eq (sb!c
::compiled-debug-fun-arguments cdebug-fun
)
1718 (buffer (make-array 0 :fill-pointer
0 :adjustable t
)))
1719 ((>= i
(length packed-vars
))
1720 (let ((result (coerce buffer
'simple-vector
)))
1722 (assign-minimal-var-names result
))
1724 (flet ((geti () (prog1 (aref packed-vars i
) (incf i
))))
1725 (let* ((flags (geti))
1726 (minimal (logtest sb
!c
::compiled-debug-var-minimal-p flags
))
1727 (deleted (logtest sb
!c
::compiled-debug-var-deleted-p flags
))
1728 (more-context-p (logtest sb
!c
::compiled-debug-var-more-context-p flags
))
1729 (more-count-p (logtest sb
!c
::compiled-debug-var-more-count-p flags
))
1730 (indirect-p (logtest sb
!c
::compiled-debug-var-indirect-p flags
))
1731 (live (logtest sb
!c
::compiled-debug-var-environment-live
1733 (save (logtest sb
!c
::compiled-debug-var-save-loc-p flags
))
1734 (symbol (if minimal nil
(geti)))
1735 (id (if (logtest sb
!c
::compiled-debug-var-id-p flags
)
1738 (sc-offset (if deleted
0
1740 #!+64-bit
(ldb (byte 27 8) flags
)))
1741 (save-sc-offset (and save
1743 #!+64-bit
(ldb (byte 27 35) flags
)))
1744 (indirect-sc-offset (and indirect-p
1746 (aver (not (and args-minimal
(not minimal
))))
1747 (vector-push-extend (make-compiled-debug-var symbol
1753 (cond (more-context-p :more-context
)
1754 (more-count-p :more-count
)))
1759 ;;; If we're sure of whether code-location is known, return T or NIL.
1760 ;;; If we're :UNSURE, then try to fill in the code-location's slots.
1761 ;;; This determines whether there is any debug-block information, and
1762 ;;; if code-location is known.
1764 ;;; ??? IF this conses closures every time it's called, then break off the
1765 ;;; :UNSURE part to get the HANDLER-CASE into another function.
1766 (defun code-location-unknown-p (basic-code-location)
1767 (ecase (code-location-%unknown-p basic-code-location
)
1771 (setf (code-location-%unknown-p basic-code-location
)
1772 (handler-case (not (fill-in-code-location basic-code-location
))
1773 (no-debug-blocks () t
))))))
1775 ;;; Return the DEBUG-BLOCK containing code-location if it is available.
1776 ;;; Some debug policies inhibit debug-block information, and if none
1777 ;;; is available, then this signals a NO-DEBUG-BLOCKS condition.
1778 (defun code-location-debug-block (basic-code-location)
1779 (let ((block (code-location-%debug-block basic-code-location
)))
1780 (if (eq block
:unparsed
)
1781 (etypecase basic-code-location
1782 (compiled-code-location
1783 (compute-compiled-code-location-debug-block basic-code-location
))
1784 ;; (There used to be more cases back before sbcl-0.7.0, when
1785 ;; we did special tricks to debug the IR1 interpreter.)
1789 ;;; Store and return BASIC-CODE-LOCATION's debug-block. We determines
1790 ;;; the correct one using the code-location's pc. We use
1791 ;;; DEBUG-FUN-DEBUG-BLOCKS to return the cached block information
1792 ;;; or signal a NO-DEBUG-BLOCKS condition. The blocks are sorted by
1793 ;;; their first code-location's pc, in ascending order. Therefore, as
1794 ;;; soon as we find a block that starts with a pc greater than
1795 ;;; basic-code-location's pc, we know the previous block contains the
1796 ;;; pc. If we get to the last block, then the code-location is either
1797 ;;; in the second to last block or the last block, and we have to be
1798 ;;; careful in determining this since the last block could be code at
1799 ;;; the end of the function. We have to check for the last block being
1800 ;;; code first in order to see how to compare the code-location's pc.
1801 (defun compute-compiled-code-location-debug-block (basic-code-location)
1802 (let* ((pc (compiled-code-location-pc basic-code-location
))
1803 (debug-fun (code-location-debug-fun
1804 basic-code-location
))
1805 (blocks (debug-fun-debug-blocks debug-fun
))
1806 (len (length blocks
)))
1807 (declare (simple-vector blocks
))
1808 (setf (code-location-%debug-block basic-code-location
)
1814 (let ((last (svref blocks end
)))
1816 ((debug-block-elsewhere-p last
)
1818 (sb!c
::compiled-debug-fun-elsewhere-pc
1819 (compiled-debug-fun-compiler-debug-fun
1821 (svref blocks
(1- end
))
1824 (compiled-code-location-pc
1825 (svref (compiled-debug-block-code-locations last
)
1827 (svref blocks
(1- end
)))
1829 (declare (type index i end
))
1831 (compiled-code-location-pc
1832 (svref (compiled-debug-block-code-locations
1835 (return (svref blocks
(1- i
)))))))))
1837 ;;; Return the CODE-LOCATION's DEBUG-SOURCE.
1838 (defun code-location-debug-source (code-location)
1839 (let ((info (compiled-debug-fun-debug-info
1840 (code-location-debug-fun code-location
))))
1841 (or (sb!c
::debug-info-source info
)
1842 (debug-signal 'no-debug-blocks
:debug-fun
1843 (code-location-debug-fun code-location
)))))
1845 ;;; Returns the number of top level forms before the one containing
1846 ;;; CODE-LOCATION as seen by the compiler in some compilation unit. (A
1847 ;;; compilation unit is not necessarily a single file, see the section
1848 ;;; on debug-sources.)
1849 (defun code-location-toplevel-form-offset (code-location)
1850 (when (code-location-unknown-p code-location
)
1851 (error 'unknown-code-location
:code-location code-location
))
1852 (let ((tlf-offset (code-location-%tlf-offset code-location
)))
1853 (cond ((eq tlf-offset
: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-%tlf-offset 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 number of the form corresponding to CODE-LOCATION. The
1868 ;;; form number is derived by a walking the subforms of a top level
1869 ;;; form in depth-first order.
1870 (defun code-location-form-number (code-location)
1871 (when (code-location-unknown-p code-location
)
1872 (error 'unknown-code-location
:code-location code-location
))
1873 (let ((form-num (code-location-%form-number code-location
)))
1874 (cond ((eq form-num
:unparsed
)
1875 (etypecase code-location
1876 (compiled-code-location
1877 (unless (fill-in-code-location code-location
)
1878 ;; This check should be unnecessary. We're missing
1879 ;; debug info the compiler should have dumped.
1880 (bug "unknown code location"))
1881 (code-location-%form-number code-location
))
1882 ;; (There used to be more cases back before sbcl-0.7.0,,
1883 ;; when we did special tricks to debug the IR1
1888 ;;; Return the kind of CODE-LOCATION, one of:
1889 ;;; :INTERPRETED, :UNKNOWN-RETURN, :KNOWN-RETURN, :INTERNAL-ERROR,
1890 ;;; :NON-LOCAL-EXIT, :BLOCK-START, :CALL-SITE, :SINGLE-VALUE-RETURN,
1891 ;;; :NON-LOCAL-ENTRY
1892 (defun code-location-kind (code-location)
1893 (when (code-location-unknown-p code-location
)
1894 (error 'unknown-code-location
:code-location code-location
))
1895 (etypecase code-location
1896 (compiled-code-location
1897 (let ((kind (compiled-code-location-kind code-location
)))
1898 (cond ((not (eq kind
:unparsed
)) kind
)
1899 ((not (fill-in-code-location code-location
))
1900 ;; This check should be unnecessary. We're missing
1901 ;; debug info the compiler should have dumped.
1902 (bug "unknown code location"))
1904 (compiled-code-location-kind code-location
)))))
1905 ;; (There used to be more cases back before sbcl-0.7.0,,
1906 ;; when we did special tricks to debug the IR1
1910 ;;; This returns CODE-LOCATION's live-set if it is available. If
1911 ;;; there is no debug-block information, this returns NIL.
1912 (defun compiled-code-location-live-set (code-location)
1913 (if (code-location-unknown-p code-location
)
1915 (let ((live-set (compiled-code-location-%live-set code-location
)))
1916 (cond ((eq live-set
:unparsed
)
1917 (unless (fill-in-code-location code-location
)
1918 ;; This check should be unnecessary. We're missing
1919 ;; debug info the compiler should have dumped.
1921 ;; FIXME: This error and comment happen over and over again.
1922 ;; Make them a shared function.
1923 (bug "unknown code location"))
1924 (compiled-code-location-%live-set code-location
))
1927 ;;; true if OBJ1 and OBJ2 are the same place in the code
1928 (defun code-location= (obj1 obj2
)
1930 (compiled-code-location
1932 (compiled-code-location
1933 (and (eq (code-location-debug-fun obj1
)
1934 (code-location-debug-fun obj2
))
1935 (sub-compiled-code-location= obj1 obj2
)))
1936 ;; (There used to be more cases back before sbcl-0.7.0,,
1937 ;; when we did special tricks to debug the IR1
1940 ;; (There used to be more cases back before sbcl-0.7.0,,
1941 ;; when we did special tricks to debug IR1-interpreted code.)
1943 (defun sub-compiled-code-location= (obj1 obj2
)
1944 (= (compiled-code-location-pc obj1
)
1945 (compiled-code-location-pc obj2
)))
1947 ;;; Fill in CODE-LOCATION's :UNPARSED slots, returning T or NIL
1948 ;;; depending on whether the code-location was known in its
1949 ;;; DEBUG-FUN's debug-block information. This may signal a
1950 ;;; NO-DEBUG-BLOCKS condition due to DEBUG-FUN-DEBUG-BLOCKS, and
1951 ;;; it assumes the %UNKNOWN-P slot is already set or going to be set.
1952 (defun fill-in-code-location (code-location)
1953 (declare (type compiled-code-location code-location
))
1954 (let* ((debug-fun (code-location-debug-fun code-location
))
1955 (blocks (debug-fun-debug-blocks debug-fun
)))
1956 (declare (simple-vector blocks
))
1957 (dotimes (i (length blocks
) nil
)
1958 (let* ((block (svref blocks i
))
1959 (locations (compiled-debug-block-code-locations block
)))
1960 (declare (simple-vector locations
))
1961 (dotimes (j (length locations
))
1962 (let ((loc (svref locations j
)))
1963 (when (sub-compiled-code-location= code-location loc
)
1964 (setf (code-location-%debug-block code-location
) block
)
1965 (setf (code-location-%tlf-offset code-location
)
1966 (code-location-%tlf-offset loc
))
1967 (setf (code-location-%form-number code-location
)
1968 (code-location-%form-number loc
))
1969 (setf (compiled-code-location-%live-set code-location
)
1970 (compiled-code-location-%live-set loc
))
1971 (setf (compiled-code-location-kind code-location
)
1972 (compiled-code-location-kind loc
))
1973 (setf (compiled-code-location-step-info code-location
)
1974 (compiled-code-location-step-info loc
))
1975 (return-from fill-in-code-location t
))))))))
1977 ;;;; operations on DEBUG-BLOCKs
1979 ;;; Execute FORMS in a context with CODE-VAR bound to each
1980 ;;; CODE-LOCATION in DEBUG-BLOCK, and return the value of RESULT.
1981 (defmacro do-debug-block-locations
((code-var debug-block
&optional result
)
1983 (let ((code-locations (gensym))
1985 `(let ((,code-locations
(debug-block-code-locations ,debug-block
)))
1986 (declare (simple-vector ,code-locations
))
1987 (dotimes (,i
(length ,code-locations
) ,result
)
1988 (let ((,code-var
(svref ,code-locations
,i
)))
1991 ;;; Return the name of the function represented by DEBUG-FUN.
1992 ;;; This may be a string or a cons; do not assume it is a symbol.
1993 (defun debug-block-fun-name (debug-block)
1994 (etypecase debug-block
1995 (compiled-debug-block
1996 (let ((code-locs (compiled-debug-block-code-locations debug-block
)))
1997 (declare (simple-vector code-locs
))
1998 (if (zerop (length code-locs
))
1999 "??? Can't get name of debug-block's function."
2001 (code-location-debug-fun (svref code-locs
0))))))
2002 ;; (There used to be more cases back before sbcl-0.7.0, when we
2003 ;; did special tricks to debug the IR1 interpreter.)
2006 (defun debug-block-code-locations (debug-block)
2007 (etypecase debug-block
2008 (compiled-debug-block
2009 (compiled-debug-block-code-locations debug-block
))
2010 ;; (There used to be more cases back before sbcl-0.7.0, when we
2011 ;; did special tricks to debug the IR1 interpreter.)
2014 ;;;; operations on debug variables
2016 (defun debug-var-symbol-name (debug-var)
2017 (symbol-name (debug-var-symbol debug-var
)))
2019 ;;; FIXME: Make sure that this isn't called anywhere that it wouldn't
2020 ;;; be acceptable to have NIL returned, or that it's only called on
2021 ;;; DEBUG-VARs whose symbols have non-NIL packages.
2022 (defun debug-var-package-name (debug-var)
2023 (package-name (symbol-package (debug-var-symbol debug-var
))))
2025 ;;; Return the value stored for DEBUG-VAR in frame, or if the value is
2026 ;;; not :VALID, then signal an INVALID-VALUE error.
2027 (defun debug-var-valid-value (debug-var frame
)
2028 (unless (eq (debug-var-validity debug-var
(frame-code-location frame
))
2030 (error 'invalid-value
:debug-var debug-var
:frame frame
))
2031 (debug-var-value debug-var frame
))
2033 ;;; Returns the value stored for DEBUG-VAR in frame. The value may be
2034 ;;; invalid. This is SETFable.
2035 (defun debug-var-value (debug-var frame
)
2036 (aver (typep frame
'compiled-frame
))
2037 (let ((res (access-compiled-debug-var-slot debug-var frame
)))
2038 (if (indirect-value-cell-p res
)
2039 (value-cell-ref res
)
2042 ;;; This returns what is stored for the variable represented by
2043 ;;; DEBUG-VAR relative to the FRAME. This may be an indirect value
2044 ;;; cell if the variable is both closed over and set.
2045 (defun access-compiled-debug-var-slot (debug-var frame
)
2046 (let ((escaped (compiled-frame-escaped frame
)))
2047 (cond ((compiled-debug-var-indirect-sc-offset debug-var
)
2048 (sub-access-debug-var-slot
2049 ;; Indirect are accessed through a frame pointer of the parent.
2051 (sub-access-debug-var-slot
2052 (frame-pointer frame
)
2054 (compiled-debug-var-sc-offset debug-var
)
2056 (compiled-debug-var-save-sc-offset debug-var
)
2057 (compiled-debug-var-sc-offset debug-var
)))
2059 (compiled-debug-var-indirect-sc-offset debug-var
)
2062 (sub-access-debug-var-slot
2063 (frame-pointer frame
)
2064 (compiled-debug-var-sc-offset debug-var
)
2067 (sub-access-debug-var-slot
2068 (frame-pointer frame
)
2069 (or (compiled-debug-var-save-sc-offset debug-var
)
2070 (compiled-debug-var-sc-offset debug-var
)))))))
2072 ;;; a helper function for working with possibly-invalid values:
2073 ;;; Do (%MAKE-LISP-OBJ VAL) only if the value looks valid.
2075 ;;; (Such values can arise in registers on machines with conservative
2076 ;;; GC, and might also arise in debug variable locations when
2077 ;;; those variables are invalid.)
2079 ;;; NOTE: this function is not GC-safe in the slightest when creating
2080 ;;; a pointer to an object in dynamic space. If a GC occurs between
2081 ;;; the start of the call to VALID-LISP-POINTER-P and the end of
2082 ;;; %MAKE-LISP-OBJ then the object could move before the boxed pointer
2083 ;;; is constructed. This can happen on CHENEYGC if an asynchronous
2084 ;;; interrupt occurs within the window. This can happen on GENCGC
2085 ;;; under the same circumstances, but is more likely due to all GENCGC
2086 ;;; platforms supporting threaded operation. This is somewhat
2087 ;;; mitigated on x86oids due to the conservative stack and interrupt
2088 ;;; context "scavenging" on such platforms, but there still may be a
2089 ;;; vulnerable window.
2090 (defun make-lisp-obj (val &optional
(errorp t
))
2091 (macrolet ((maybe-tag-tramp (x)
2094 (* sb
!vm
:n-word-bytes sb
!vm
:simple-fun-code-offset
))
2095 sb
!vm
:fun-pointer-lowtag
)
2100 (zerop (logand val sb
!vm
:fixnum-tag-mask
))
2101 ;; immediate single float, 64-bit only
2103 (= (logand val
#xff
) sb
!vm
:single-float-widetag
)
2105 (and (zerop (logandc2 val
#x1fffffff
)) ; Top bits zero
2106 (= (logand val
#xff
) sb
!vm
:character-widetag
)) ; char tag
2108 (= val sb
!vm
:unbound-marker-widetag
)
2109 ;; undefined_tramp doesn't validate properly as a pointer, and
2110 ;; the actual value can vary by backend (x86oids need not apply)
2112 (= val
(maybe-tag-tramp (foreign-symbol-address "undefined_tramp")))
2114 (= val
(maybe-tag-tramp (foreign-symbol-address "undefined_alien_function")))
2116 (not (zerop (valid-lisp-pointer-p (int-sap val
)))))
2117 (values (%make-lisp-obj val
) t
)
2119 (error "~S is not a valid argument to ~S"
2121 (values (make-unprintable-object (format nil
"invalid object #x~X" val
))
2124 (defun sub-access-debug-var-slot (fp sc-offset
&optional escaped
)
2125 ;; NOTE: The long-float support in here is obviously decayed. When
2126 ;; the x86oid and non-x86oid versions of this function were unified,
2127 ;; the behavior of long-floats was preserved, which only served to
2128 ;; highlight its brokenness.
2129 (macrolet ((with-escaped-value ((var) &body forms
)
2131 (let ((,var
(sb!vm
:context-register
2133 (sb!c
:sc-offset-offset sc-offset
))))
2135 :invalid-value-for-unescaped-register-storage
))
2136 (escaped-float-value (format)
2138 (sb!vm
:context-float-register
2140 (sb!c
:sc-offset-offset sc-offset
) ',format
)
2141 :invalid-value-for-unescaped-register-storage
))
2142 (with-nfp ((var) &body body
)
2143 ;; x86oids have no separate number stack, so dummy it
2145 #!+c-stack-is-control-stack
2148 #!-c-stack-is-control-stack
2149 `(let ((,var
(if escaped
2151 (sb!vm
:context-register escaped
2154 (sap-ref-sap fp
(* nfp-save-offset
2155 sb
!vm
:n-word-bytes
))
2157 (sb!vm
::make-number-stack-pointer
2158 (sap-ref-32 fp
(* nfp-save-offset
2159 sb
!vm
:n-word-bytes
))))))
2161 (number-stack-offset (&optional
(offset 0))
2163 `(+ (sb!vm
::frame-byte-offset
(sb!c
:sc-offset-offset sc-offset
))
2166 `(+ (* (sb!c
:sc-offset-offset sc-offset
) sb
!vm
:n-word-bytes
)
2168 (ecase (sb!c
:sc-offset-scn sc-offset
)
2169 ((#.sb
!vm
:any-reg-sc-number
2170 #.sb
!vm
:descriptor-reg-sc-number
)
2172 (with-escaped-value (val)
2173 (values (make-lisp-obj (mask-field (byte #.sb
!vm
:n-word-bits
0) val
) nil
)))))
2174 (#.sb
!vm
:character-reg-sc-number
2175 (with-escaped-value (val)
2177 (#.sb
!vm
:sap-reg-sc-number
2178 (with-escaped-value (val)
2180 (#.sb
!vm
:signed-reg-sc-number
2181 (with-escaped-value (val)
2182 (if (logbitp (1- sb
!vm
:n-word-bits
) val
)
2183 (logior val
(ash -
1 sb
!vm
:n-word-bits
))
2185 (#.sb
!vm
:unsigned-reg-sc-number
2186 (with-escaped-value (val)
2189 (#.sb
!vm
:non-descriptor-reg-sc-number
2190 (error "Local non-descriptor register access?"))
2192 (#.sb
!vm
:interior-reg-sc-number
2193 (error "Local interior register access?"))
2194 (#.sb
!vm
:single-reg-sc-number
2195 (escaped-float-value single-float
))
2196 (#.sb
!vm
:double-reg-sc-number
2197 (escaped-float-value double-float
))
2199 (#.sb
!vm
:long-reg-sc-number
2200 (escaped-float-value long-float
))
2201 (#.sb
!vm
:complex-single-reg-sc-number
2202 (escaped-float-value complex-single-float
))
2203 (#.sb
!vm
:complex-double-reg-sc-number
2204 (escaped-float-value complex-double-float
))
2206 (#.sb
!vm
:complex-long-reg-sc-number
2207 (escaped-float-value sb
!kernel
::complex-long-float
))
2208 (#.sb
!vm
:single-stack-sc-number
2210 (sap-ref-single nfp
(number-stack-offset))))
2211 (#.sb
!vm
:double-stack-sc-number
2213 (sap-ref-double nfp
(number-stack-offset))))
2215 (#.sb
!vm
:long-stack-sc-number
2217 (sap-ref-long nfp
(number-stack-offset))))
2218 (#.sb
!vm
:complex-single-stack-sc-number
2221 (sap-ref-single nfp
(number-stack-offset))
2222 (sap-ref-single nfp
(number-stack-offset 4)))))
2223 (#.sb
!vm
:complex-double-stack-sc-number
2226 (sap-ref-double nfp
(number-stack-offset))
2227 (sap-ref-double nfp
(number-stack-offset 8)))))
2229 (#.sb
!vm
:complex-long-stack-sc-number
2232 (sap-ref-long nfp
(number-stack-offset))
2234 (number-stack-offset #!+sparc
4
2235 #!+(or x86 x86-64
) 3)))))
2236 (#.sb
!vm
:control-stack-sc-number
2237 (stack-ref fp
(sb!c
:sc-offset-offset sc-offset
)))
2238 (#.sb
!vm
:character-stack-sc-number
2240 (code-char (sap-ref-word nfp
(number-stack-offset)))))
2241 (#.sb
!vm
:unsigned-stack-sc-number
2243 (sap-ref-word nfp
(number-stack-offset))))
2244 (#.sb
!vm
:signed-stack-sc-number
2246 (signed-sap-ref-word nfp
(number-stack-offset))))
2247 (#.sb
!vm
:sap-stack-sc-number
2249 (sap-ref-sap nfp
(number-stack-offset))))
2250 (#.constant-sc-number
2253 (component-from-component-ptr
2254 (component-ptr-from-pc
2255 (sb!vm
:context-pc escaped
)))
2256 (sb!c
:sc-offset-offset sc-offset
))
2257 :invalid-value-for-unescaped-register-storage
)))))
2259 ;;; This stores value as the value of DEBUG-VAR in FRAME. In the
2260 ;;; COMPILED-DEBUG-VAR case, access the current value to determine if
2261 ;;; it is an indirect value cell. This occurs when the variable is
2262 ;;; both closed over and set.
2263 (defun %set-debug-var-value
(debug-var frame new-value
)
2264 (aver (typep frame
'compiled-frame
))
2265 (let ((old-value (access-compiled-debug-var-slot debug-var frame
)))
2266 (if (indirect-value-cell-p old-value
)
2267 (value-cell-set old-value new-value
)
2268 (set-compiled-debug-var-slot debug-var frame new-value
)))
2271 ;;; This stores VALUE for the variable represented by debug-var
2272 ;;; relative to the frame. This assumes the location directly contains
2273 ;;; the variable's value; that is, there is no indirect value cell
2274 ;;; currently there in case the variable is both closed over and set.
2275 (defun set-compiled-debug-var-slot (debug-var frame value
)
2276 (let ((escaped (compiled-frame-escaped frame
)))
2278 (sub-set-debug-var-slot (frame-pointer frame
)
2279 (compiled-debug-var-sc-offset debug-var
)
2281 (sub-set-debug-var-slot
2282 (frame-pointer frame
)
2283 (or (compiled-debug-var-save-sc-offset debug-var
)
2284 (compiled-debug-var-sc-offset debug-var
))
2287 (defun sub-set-debug-var-slot (fp sc-offset value
&optional escaped
)
2288 ;; Like sub-access-debug-var-slot, this is the unification of two
2289 ;; divergent copy-pasted functions. The astute reviewer will notice
2290 ;; that long-floats are messed up here as well, that x86oids
2291 ;; apparently don't support accessing float values that are in
2292 ;; registers, and that non-x86oids store the real part of a float
2293 ;; for both the real and imaginary parts of a complex on the stack
2294 ;; (but not in registers, oddly enough). Some research has
2295 ;; indicated that the different forms of THE used for validating the
2296 ;; type of complex float components between x86oid and non-x86oid
2297 ;; systems are only significant in the case of using a non-complex
2298 ;; number as input (as the non-x86oid case effectively converts
2299 ;; non-complex numbers to complex ones and the x86oid case will
2300 ;; error out). That said, the error message from entering a value
2301 ;; of the wrong type will be slightly easier to understand on x86oid
2303 (macrolet ((set-escaped-value (val)
2305 (setf (sb!vm
:context-register
2307 (sb!c
:sc-offset-offset sc-offset
))
2310 (set-escaped-float-value (format val
)
2312 (setf (sb!vm
:context-float-register
2314 (sb!c
:sc-offset-offset sc-offset
)
2318 (with-nfp ((var) &body body
)
2319 ;; x86oids have no separate number stack, so dummy it
2325 `(let ((,var
(if escaped
2327 (sb!vm
:context-register escaped
2332 sb
!vm
:n-word-bytes
))
2334 (sb!vm
::make-number-stack-pointer
2337 sb
!vm
:n-word-bytes
))))))
2339 (number-stack-offset (&optional
(offset 0))
2341 `(+ (sb!vm
::frame-byte-offset
(sb!c
:sc-offset-offset sc-offset
))
2344 `(+ (* (sb!c
:sc-offset-offset sc-offset
) sb
!vm
:n-word-bytes
)
2346 (ecase (sb!c
:sc-offset-scn sc-offset
)
2347 ((#.sb
!vm
:any-reg-sc-number
2348 #.sb
!vm
:descriptor-reg-sc-number
)
2351 (get-lisp-obj-address value
))))
2352 (#.sb
!vm
:character-reg-sc-number
2353 (set-escaped-value (char-code value
)))
2354 (#.sb
!vm
:sap-reg-sc-number
2355 (set-escaped-value (sap-int value
)))
2356 (#.sb
!vm
:signed-reg-sc-number
2357 (set-escaped-value (logand value
(1- (ash 1 sb
!vm
:n-word-bits
)))))
2358 (#.sb
!vm
:unsigned-reg-sc-number
2359 (set-escaped-value value
))
2361 (#.sb
!vm
:non-descriptor-reg-sc-number
2362 (error "Local non-descriptor register access?"))
2364 (#.sb
!vm
:interior-reg-sc-number
2365 (error "Local interior register access?"))
2366 (#.sb
!vm
:single-reg-sc-number
2367 #!-
(or x86 x86-64
) ;; don't have escaped floats.
2368 (set-escaped-float-value single-float value
))
2369 (#.sb
!vm
:double-reg-sc-number
2370 (set-escaped-float-value double-float value
))
2372 (#.sb
!vm
:long-reg-sc-number
2373 (set-escaped-float-value long-float value
))
2374 (#.sb
!vm
:complex-single-reg-sc-number
2375 (set-escaped-float-value complex-single-float value
))
2376 (#.sb
!vm
:complex-double-reg-sc-number
2377 (set-escaped-float-value complex-double-float value
))
2379 (#.sb
!vm
:complex-long-reg-sc-number
2380 (set-escaped-float-value complex-long-float
))
2381 (#.sb
!vm
:single-stack-sc-number
2383 (setf (sap-ref-single nfp
(number-stack-offset))
2384 (the single-float value
))))
2385 (#.sb
!vm
:double-stack-sc-number
2387 (setf (sap-ref-double nfp
(number-stack-offset))
2388 (the double-float value
))))
2390 (#.sb
!vm
:long-stack-sc-number
2392 (setf (sap-ref-long nfp
(number-stack-offset))
2393 (the long-float value
))))
2394 (#.sb
!vm
:complex-single-stack-sc-number
2396 (setf (sap-ref-single nfp
(number-stack-offset))
2398 (realpart (the (complex single-float
) value
))
2400 (the single-float
(realpart value
)))
2401 (setf (sap-ref-single nfp
(number-stack-offset 4))
2403 (imagpart (the (complex single-float
) value
))
2405 (the single-float
(realpart value
)))))
2406 (#.sb
!vm
:complex-double-stack-sc-number
2408 (setf (sap-ref-double nfp
(number-stack-offset))
2410 (realpart (the (complex double-float
) value
))
2412 (the double-float
(realpart value
)))
2413 (setf (sap-ref-double nfp
(number-stack-offset 8))
2415 (imagpart (the (complex double-float
) value
))
2417 (the double-float
(realpart value
)))))
2419 (#.sb
!vm
:complex-long-stack-sc-number
2422 nfp
(number-stack-offset))
2424 (realpart (the (complex long-float
) value
))
2426 (the long-float
(realpart value
)))
2428 nfp
(number-stack-offset #!+sparc
4
2429 #!+(or x86 x86-64
) 3))
2431 (imagpart (the (complex long-float
) value
))
2433 (the long-float
(realpart value
)))))
2434 (#.sb
!vm
:control-stack-sc-number
2435 (setf (stack-ref fp
(sb!c
:sc-offset-offset sc-offset
)) value
))
2436 (#.sb
!vm
:character-stack-sc-number
2438 (setf (sap-ref-word nfp
(number-stack-offset 0))
2439 (char-code (the character value
)))))
2440 (#.sb
!vm
:unsigned-stack-sc-number
2442 (setf (sap-ref-word nfp
(number-stack-offset 0)) (the word value
))))
2443 (#.sb
!vm
:signed-stack-sc-number
2445 (setf (signed-sap-ref-word nfp
(number-stack-offset))
2446 (the signed-word value
))))
2447 (#.sb
!vm
:sap-stack-sc-number
2449 (setf (sap-ref-sap nfp
(number-stack-offset))
2450 (the system-area-pointer value
)))))))
2452 ;;; The method for setting and accessing COMPILED-DEBUG-VAR values use
2453 ;;; this to determine if the value stored is the actual value or an
2454 ;;; indirection cell.
2455 (defun indirect-value-cell-p (x)
2456 (and (= (lowtag-of x
) sb
!vm
:other-pointer-lowtag
)
2457 (= (widetag-of x
) sb
!vm
:value-cell-header-widetag
)))
2459 ;;; Return three values reflecting the validity of DEBUG-VAR's value
2460 ;;; at BASIC-CODE-LOCATION:
2461 ;;; :VALID The value is known to be available.
2462 ;;; :INVALID The value is known to be unavailable.
2463 ;;; :UNKNOWN The value's availability is unknown.
2465 ;;; If the variable is always alive, then it is valid. If the
2466 ;;; code-location is unknown, then the variable's validity is
2467 ;;; :unknown. Once we've called CODE-LOCATION-UNKNOWN-P, we know the
2468 ;;; live-set information has been cached in the code-location.
2469 (defun debug-var-validity (debug-var basic-code-location
)
2470 (compiled-debug-var-validity debug-var basic-code-location
))
2472 (defun debug-var-info (debug-var)
2473 (compiled-debug-var-info debug-var
))
2475 ;;; This is the method for DEBUG-VAR-VALIDITY for COMPILED-DEBUG-VARs.
2476 ;;; For safety, make sure basic-code-location is what we think.
2477 (defun compiled-debug-var-validity (debug-var basic-code-location
)
2478 (declare (type compiled-code-location basic-code-location
))
2479 (cond ((debug-var-alive-p debug-var
)
2480 (let ((debug-fun (code-location-debug-fun basic-code-location
)))
2481 (if (>= (compiled-code-location-pc basic-code-location
)
2482 (sb!c
::compiled-debug-fun-start-pc
2483 (compiled-debug-fun-compiler-debug-fun debug-fun
)))
2486 ((code-location-unknown-p basic-code-location
) :unknown
)
2488 (let ((pos (position debug-var
2489 (debug-fun-debug-vars
2490 (code-location-debug-fun
2491 basic-code-location
)))))
2493 (error 'unknown-debug-var
2494 :debug-var debug-var
2496 (code-location-debug-fun basic-code-location
)))
2497 ;; There must be live-set info since basic-code-location is known.
2498 (if (zerop (sbit (compiled-code-location-live-set
2499 basic-code-location
)
2506 ;;; This code produces and uses what we call source-paths. A
2507 ;;; source-path is a list whose first element is a form number as
2508 ;;; returned by CODE-LOCATION-FORM-NUMBER and whose last element is a
2509 ;;; top level form number as returned by
2510 ;;; CODE-LOCATION-TOPLEVEL-FORM-NUMBER. The elements from the last to
2511 ;;; the first, exclusively, are the numbered subforms into which to
2512 ;;; descend. For example:
2514 ;;; (let ((a (aref x 3)))
2516 ;;; The call to AREF in this example is form number 5. Assuming this
2517 ;;; DEFUN is the 11'th top level form, the source-path for the AREF
2518 ;;; call is as follows:
2520 ;;; Given the DEFUN, 3 gets you the LET, 1 gets you the bindings, 0
2521 ;;; gets the first binding, and 1 gets the AREF form.
2523 ;;; This returns a table mapping form numbers to source-paths. A
2524 ;;; source-path indicates a descent into the TOPLEVEL-FORM form,
2525 ;;; going directly to the subform corressponding to the form number.
2527 ;;; The vector elements are in the same format as the compiler's
2528 ;;; NODE-SOURCE-PATH; that is, the first element is the form number and
2529 ;;; the last is the TOPLEVEL-FORM number.
2531 ;;; This should be synchronized with SB-C::SUB-FIND-SOURCE-PATHS
2532 (defun form-number-translations (form tlf-number
)
2534 (translations (make-array 12 :fill-pointer
0 :adjustable t
)))
2535 (labels ((translate1 (form path
)
2536 (unless (member form seen
)
2538 (vector-push-extend (cons (fill-pointer translations
) path
)
2543 (declare (fixnum pos
))
2546 (when (atom subform
) (return))
2547 (let ((fm (car subform
)))
2548 (when (sb!int
:comma-p fm
)
2549 (setf fm
(sb!int
:comma-expr fm
)))
2551 (translate1 fm
(cons pos path
)))
2553 ;; Don't look into quoted constants.
2556 (setq subform
(cdr subform
))
2557 (when (eq subform trail
) (return)))))
2561 (setq trail
(cdr trail
))))))))
2562 (translate1 form
(list tlf-number
)))
2563 (coerce translations
'simple-vector
)))
2565 ;;; FORM is a top level form, and path is a source-path into it. This
2566 ;;; returns the form indicated by the source-path. Context is the
2567 ;;; number of enclosing forms to return instead of directly returning
2568 ;;; the source-path form. When context is non-zero, the form returned
2569 ;;; contains a marker, #:****HERE****, immediately before the form
2570 ;;; indicated by path.
2571 (defun source-path-context (form path context
)
2572 (declare (type unsigned-byte context
))
2573 ;; Get to the form indicated by path or the enclosing form indicated
2574 ;; by context and path.
2575 (let ((path (reverse (butlast (cdr path
)))))
2576 (dotimes (i (- (length path
) context
))
2577 (let ((index (first path
)))
2578 (unless (and (listp form
) (< index
(length form
)))
2579 (error "Source path no longer exists."))
2580 (setq form
(elt form index
))
2581 (setq path
(rest path
))))
2582 ;; Recursively rebuild the source form resulting from the above
2583 ;; descent, copying the beginning of each subform up to the next
2584 ;; subform we descend into according to path. At the bottom of the
2585 ;; recursion, we return the form indicated by path preceded by our
2586 ;; marker, and this gets spliced into the resulting list structure
2587 ;; on the way back up.
2588 (labels ((frob (form path level
)
2589 (if (or (zerop level
) (null path
))
2592 `(#:***here
*** ,form
))
2593 (let ((n (first path
)))
2594 (unless (and (listp form
) (< n
(length form
)))
2595 (error "Source path no longer exists."))
2596 (let ((res (frob (elt form n
) (rest path
) (1- level
))))
2597 (nconc (subseq form
0 n
)
2598 (cons res
(nthcdr (1+ n
) form
))))))))
2599 (frob form path context
))))
2601 ;;; Given a code location, return the associated form-number
2602 ;;; translations and the actual top level form.
2603 (defun get-toplevel-form (location)
2604 (let ((d-source (code-location-debug-source location
)))
2605 (let* ((offset (code-location-toplevel-form-offset location
))
2607 (cond ((debug-source-form d-source
)
2608 (debug-source-form d-source
))
2609 ((debug-source-namestring d-source
)
2610 (get-file-toplevel-form location
))
2611 (t (bug "Don't know how to use a DEBUG-SOURCE without ~
2612 a namestring or a form.")))))
2613 (values (form-number-translations res offset
) res
))))
2615 ;;; To suppress the read-time evaluation #. macro during source read,
2616 ;;; *READTABLE* is modified.
2618 ;;; FIXME: This breaks #+#.(cl:if ...) Maybe we need a SAFE-READ-EVAL, which
2619 ;;; this code can use for side- effect free #. calls?
2621 ;;; FIXME: This also knows nothing of custom readtables. The assumption
2622 ;;; is that the current readtable is a decent approximation for what
2623 ;;; we want, but that's lossy.
2624 (defun safe-readtable ()
2625 (let ((rt (copy-readtable)))
2626 (set-dispatch-macro-character
2627 #\
# #\.
(lambda (stream sub-char
&rest rest
)
2628 (declare (ignore rest sub-char
))
2629 (let ((token (read stream t nil t
)))
2630 (format nil
"#.~S" token
)))
2634 ;;; Locate the source file (if it still exists) and grab the top level
2635 ;;; form. If the file is modified, we use the top level form offset
2636 ;;; instead of the recorded character offset.
2637 (defun get-file-toplevel-form (location)
2638 (let* ((d-source (code-location-debug-source location
))
2639 (tlf-offset (code-location-toplevel-form-offset location
))
2641 (aref (or (sb!di
:debug-source-start-positions d-source
)
2642 (error "no start positions map"))
2644 (namestring (debug-source-namestring d-source
)))
2645 ;; FIXME: External format?
2646 (with-open-file (f namestring
:if-does-not-exist nil
)
2648 (let ((*readtable
* (safe-readtable)))
2649 (cond ((eql (debug-source-created d-source
) (file-write-date f
))
2650 (file-position f char-offset
))
2653 "~%; File has been modified since compilation:~%; ~A~@
2654 ; Using form offset instead of character position.~%"
2656 (let ((*read-suppress
* t
))
2657 (loop repeat tlf-offset
2661 ;;;; PREPROCESS-FOR-EVAL
2663 ;;; Return a function of one argument that evaluates form in the
2664 ;;; lexical context of the BASIC-CODE-LOCATION LOC, or signal a
2665 ;;; NO-DEBUG-VARS condition when the LOC's DEBUG-FUN has no
2666 ;;; DEBUG-VAR information available.
2668 ;;; The returned function takes the frame to get values from as its
2669 ;;; argument, and it returns the values of FORM. The returned function
2670 ;;; can signal the following conditions: INVALID-VALUE,
2671 ;;; AMBIGUOUS-VAR-NAME, and FRAME-FUN-MISMATCH.
2672 (defun preprocess-for-eval (form loc
)
2673 (declare (type code-location loc
))
2674 (let ((n-frame (gensym))
2675 (fun (code-location-debug-fun loc
))
2678 (unless (debug-var-info-available fun
)
2679 (debug-signal 'no-debug-vars
:debug-fun fun
))
2680 (sb!int
:collect
((binds)
2682 (do-debug-fun-vars (var fun
)
2683 (let ((validity (debug-var-validity var loc
)))
2684 (unless (eq validity
:invalid
)
2685 (case (debug-var-info var
)
2687 (setf more-context var
))
2689 (setf more-count var
)))
2690 (let* ((sym (debug-var-symbol var
))
2691 (found (assoc sym
(binds))))
2693 (setf (second found
) :ambiguous
)
2694 (binds (list sym validity var
)))))))
2695 (when (and more-context more-count
)
2696 (let ((more (assoc 'sb
!debug
::more
(binds))))
2698 (setf (second more
) :ambiguous
)
2699 (binds (list 'sb
!debug
::more
:more more-context more-count
)))))
2700 (dolist (bind (binds))
2701 (let ((name (first bind
))
2703 (ecase (second bind
)
2705 (specs `(,name
(debug-var-value ',var
,n-frame
))))
2707 (let ((count-var (fourth bind
)))
2708 (specs `(,name
(multiple-value-list
2709 (sb!c
:%more-arg-values
(debug-var-value ',var
,n-frame
)
2711 (debug-var-value ',count-var
,n-frame
)))))))
2713 (specs `(,name
(debug-signal 'invalid-value
2717 (specs `(,name
(debug-signal 'ambiguous-var-name
2719 :frame
,n-frame
)))))))
2720 (let ((res (coerce `(lambda (,n-frame
)
2721 (declare (ignorable ,n-frame
))
2722 (symbol-macrolet ,(specs) ,form
))
2725 ;; This prevents these functions from being used in any
2726 ;; location other than a function return location, so maybe
2727 ;; this should only check whether FRAME's DEBUG-FUN is the
2729 (unless (code-location= (frame-code-location frame
) loc
)
2730 (debug-signal 'frame-fun-mismatch
2731 :code-location loc
:form form
:frame frame
))
2732 (funcall res frame
))))))
2736 (defun eval-in-frame (frame form
)
2737 (declare (type frame frame
))
2739 "Evaluate FORM in the lexical context of FRAME's current code location,
2740 returning the results of the evaluation."
2741 (funcall (preprocess-for-eval form
(frame-code-location frame
)) frame
))
2745 ;;;; user-visible interface
2747 ;;; Create and return a breakpoint. When program execution encounters
2748 ;;; the breakpoint, the system calls HOOK-FUN. HOOK-FUN takes the
2749 ;;; current frame for the function in which the program is running and
2750 ;;; the breakpoint object.
2752 ;;; WHAT and KIND determine where in a function the system invokes
2753 ;;; HOOK-FUN. WHAT is either a code-location or a DEBUG-FUN. KIND is
2754 ;;; one of :CODE-LOCATION, :FUN-START, or :FUN-END. Since the starts
2755 ;;; and ends of functions may not have code-locations representing
2756 ;;; them, designate these places by supplying WHAT as a DEBUG-FUN and
2757 ;;; KIND indicating the :FUN-START or :FUN-END. When WHAT is a
2758 ;;; DEBUG-FUN and kind is :FUN-END, then HOOK-FUN must take two
2759 ;;; additional arguments, a list of values returned by the function
2760 ;;; and a FUN-END-COOKIE.
2762 ;;; INFO is information supplied by and used by the user.
2764 ;;; FUN-END-COOKIE is a function. To implement :FUN-END
2765 ;;; breakpoints, the system uses starter breakpoints to establish the
2766 ;;; :FUN-END breakpoint for each invocation of the function. Upon
2767 ;;; each entry, the system creates a unique cookie to identify the
2768 ;;; invocation, and when the user supplies a function for this
2769 ;;; argument, the system invokes it on the frame and the cookie. The
2770 ;;; system later invokes the :FUN-END breakpoint hook on the same
2771 ;;; cookie. The user may save the cookie for comparison in the hook
2774 ;;; Signal an error if WHAT is an unknown code-location.
2775 (defun make-breakpoint (hook-fun what
2776 &key
(kind :code-location
) info fun-end-cookie
)
2779 (when (code-location-unknown-p what
)
2780 (error "cannot make a breakpoint at an unknown code location: ~S"
2782 (aver (eq kind
:code-location
))
2783 (let ((bpt (%make-breakpoint hook-fun what kind info
)))
2785 (compiled-code-location
2786 ;; This slot is filled in due to calling CODE-LOCATION-UNKNOWN-P.
2787 (when (eq (compiled-code-location-kind what
) :unknown-return
)
2788 (let ((other-bpt (%make-breakpoint hook-fun what
2789 :unknown-return-partner
2791 (setf (breakpoint-unknown-return-partner bpt
) other-bpt
)
2792 (setf (breakpoint-unknown-return-partner other-bpt
) bpt
))))
2793 ;; (There used to be more cases back before sbcl-0.7.0,,
2794 ;; when we did special tricks to debug the IR1
2801 (%make-breakpoint hook-fun what kind info
))
2803 (unless (eq (sb!c
::compiled-debug-fun-returns
2804 (compiled-debug-fun-compiler-debug-fun what
))
2806 (error ":FUN-END breakpoints are currently unsupported ~
2807 for the known return convention."))
2809 (let* ((bpt (%make-breakpoint hook-fun what kind info
))
2810 (starter (compiled-debug-fun-end-starter what
)))
2812 (setf starter
(%make-breakpoint
#'list what
:fun-start nil
))
2813 (setf (breakpoint-hook-fun starter
)
2814 (fun-end-starter-hook starter what
))
2815 (setf (compiled-debug-fun-end-starter what
) starter
))
2816 (setf (breakpoint-start-helper bpt
) starter
)
2817 (push bpt
(breakpoint-%info starter
))
2818 (setf (breakpoint-cookie-fun bpt
) fun-end-cookie
)
2821 ;;; These are unique objects created upon entry into a function by a
2822 ;;; :FUN-END breakpoint's starter hook. These are only created
2823 ;;; when users supply :FUN-END-COOKIE to MAKE-BREAKPOINT. Also,
2824 ;;; the :FUN-END breakpoint's hook is called on the same cookie
2825 ;;; when it is created.
2826 (defstruct (fun-end-cookie
2827 (:print-object
(lambda (obj str
)
2828 (print-unreadable-object (obj str
:type t
))))
2829 (:constructor make-fun-end-cookie
(bogus-lra debug-fun
))
2831 ;; a pointer to the bogus-lra created for :FUN-END breakpoints
2833 ;; the DEBUG-FUN associated with this cookie
2836 ;;; This maps bogus-lra-components to cookies, so that
2837 ;;; HANDLE-FUN-END-BREAKPOINT can find the appropriate cookie for the
2838 ;;; breakpoint hook.
2839 (defvar *fun-end-cookies
* (make-hash-table :test
'eq
:synchronized t
))
2841 ;;; This returns a hook function for the start helper breakpoint
2842 ;;; associated with a :FUN-END breakpoint. The returned function
2843 ;;; makes a fake LRA that all returns go through, and this piece of
2844 ;;; fake code actually breaks. Upon return from the break, the code
2845 ;;; provides the returnee with any values. Since the returned function
2846 ;;; effectively activates FUN-END-BPT on each entry to DEBUG-FUN's
2847 ;;; function, we must establish breakpoint-data about FUN-END-BPT.
2848 (defun fun-end-starter-hook (starter-bpt debug-fun
)
2849 (declare (type breakpoint starter-bpt
)
2850 (type compiled-debug-fun debug-fun
))
2851 (lambda (frame breakpoint
)
2852 (declare (ignore breakpoint
)
2854 (let ((lra-sc-offset
2855 #!-fp-and-pc-standard-save
2856 (sb!c
::compiled-debug-fun-return-pc
2857 (compiled-debug-fun-compiler-debug-fun debug-fun
))
2858 #!+fp-and-pc-standard-save
2859 sb
!c
:return-pc-passing-offset
))
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
))
2893 #!-fp-and-pc-standard-save
2894 (sb!c
::compiled-debug-fun-return-pc
2895 (compiled-debug-fun-compiler-debug-fun
2896 (fun-end-cookie-debug-fun cookie
)))
2897 #!+fp-and-pc-standard-save
2898 sb
!c
:return-pc-passing-offset
))
2899 (do ((frame frame
(frame-down frame
)))
2901 (when (and (compiled-frame-p frame
)
2902 (#!-
(or x86 x86-64
) eq
#!+(or x86 x86-64
) sap
=
2904 (get-context-value frame lra-save-offset lra-sc-offset
)))
2907 ;;;; ACTIVATE-BREAKPOINT
2909 ;;; Cause the system to invoke the breakpoint's hook function until
2910 ;;; the next call to DEACTIVATE-BREAKPOINT or DELETE-BREAKPOINT. The
2911 ;;; system invokes breakpoint hook functions in the opposite order
2912 ;;; that you activate them.
2913 (defun activate-breakpoint (breakpoint)
2914 (when (eq (breakpoint-status breakpoint
) :deleted
)
2915 (error "cannot activate a deleted breakpoint: ~S" breakpoint
))
2916 (unless (eq (breakpoint-status breakpoint
) :active
)
2917 (ecase (breakpoint-kind breakpoint
)
2919 (let ((loc (breakpoint-what breakpoint
)))
2921 (compiled-code-location
2922 (activate-compiled-code-location-breakpoint breakpoint
)
2923 (let ((other (breakpoint-unknown-return-partner breakpoint
)))
2925 (activate-compiled-code-location-breakpoint other
))))
2926 ;; (There used to be more cases back before sbcl-0.7.0, when
2927 ;; we did special tricks to debug the IR1 interpreter.)
2930 (etypecase (breakpoint-what breakpoint
)
2932 (activate-compiled-fun-start-breakpoint breakpoint
))
2933 ;; (There used to be more cases back before sbcl-0.7.0, when
2934 ;; we did special tricks to debug the IR1 interpreter.)
2937 (etypecase (breakpoint-what breakpoint
)
2939 (let ((starter (breakpoint-start-helper breakpoint
)))
2940 (unless (eq (breakpoint-status starter
) :active
)
2941 ;; may already be active by some other :FUN-END breakpoint
2942 (activate-compiled-fun-start-breakpoint starter
)))
2943 (setf (breakpoint-status breakpoint
) :active
))
2944 ;; (There used to be more cases back before sbcl-0.7.0, when
2945 ;; we did special tricks to debug the IR1 interpreter.)
2949 (defun activate-compiled-code-location-breakpoint (breakpoint)
2950 (declare (type breakpoint breakpoint
))
2951 (let ((loc (breakpoint-what breakpoint
)))
2952 (declare (type compiled-code-location loc
))
2953 (sub-activate-breakpoint
2955 (breakpoint-data (compiled-debug-fun-component
2956 (code-location-debug-fun loc
))
2957 (+ (compiled-code-location-pc loc
)
2958 (if (or (eq (breakpoint-kind breakpoint
)
2959 :unknown-return-partner
)
2960 (eq (compiled-code-location-kind loc
)
2961 :single-value-return
))
2962 sb
!vm
:single-value-return-byte-offset
2965 (defun activate-compiled-fun-start-breakpoint (breakpoint)
2966 (declare (type breakpoint breakpoint
))
2967 (let ((debug-fun (breakpoint-what breakpoint
)))
2968 (sub-activate-breakpoint
2970 (breakpoint-data (compiled-debug-fun-component debug-fun
)
2971 (sb!c
::compiled-debug-fun-start-pc
2972 (compiled-debug-fun-compiler-debug-fun
2975 (defun sub-activate-breakpoint (breakpoint data
)
2976 (declare (type breakpoint breakpoint
)
2977 (type breakpoint-data data
))
2978 (setf (breakpoint-status breakpoint
) :active
)
2980 (unless (breakpoint-data-breakpoints data
)
2981 (setf (breakpoint-data-instruction data
)
2983 (breakpoint-install (get-lisp-obj-address
2984 (breakpoint-data-component data
))
2985 (breakpoint-data-offset data
)))))
2986 (setf (breakpoint-data-breakpoints data
)
2987 (append (breakpoint-data-breakpoints data
) (list breakpoint
)))
2988 (setf (breakpoint-internal-data breakpoint
) data
)))
2990 ;;;; DEACTIVATE-BREAKPOINT
2992 ;;; Stop the system from invoking the breakpoint's hook function.
2993 (defun deactivate-breakpoint (breakpoint)
2994 (when (eq (breakpoint-status breakpoint
) :active
)
2996 (let ((loc (breakpoint-what breakpoint
)))
2998 ((or compiled-code-location compiled-debug-fun
)
2999 (deactivate-compiled-breakpoint breakpoint
)
3000 (let ((other (breakpoint-unknown-return-partner breakpoint
)))
3002 (deactivate-compiled-breakpoint other
))))
3003 ;; (There used to be more cases back before sbcl-0.7.0, when
3004 ;; we did special tricks to debug the IR1 interpreter.)
3008 (defun deactivate-compiled-breakpoint (breakpoint)
3009 (if (eq (breakpoint-kind breakpoint
) :fun-end
)
3010 (let ((starter (breakpoint-start-helper breakpoint
)))
3011 (unless (find-if (lambda (bpt)
3012 (and (not (eq bpt breakpoint
))
3013 (eq (breakpoint-status bpt
) :active
)))
3014 (breakpoint-%info starter
))
3015 (deactivate-compiled-breakpoint starter
)))
3016 (let* ((data (breakpoint-internal-data breakpoint
))
3017 (bpts (delete breakpoint
(breakpoint-data-breakpoints data
))))
3018 (setf (breakpoint-internal-data breakpoint
) nil
)
3019 (setf (breakpoint-data-breakpoints data
) bpts
)
3022 (breakpoint-remove (get-lisp-obj-address
3023 (breakpoint-data-component data
))
3024 (breakpoint-data-offset data
)
3025 (breakpoint-data-instruction data
)))
3026 (delete-breakpoint-data data
))))
3027 (setf (breakpoint-status breakpoint
) :inactive
)
3030 ;;;; BREAKPOINT-INFO
3032 ;;; Return the user-maintained info associated with breakpoint. This
3034 (defun breakpoint-info (breakpoint)
3035 (breakpoint-%info breakpoint
))
3036 (defun %set-breakpoint-info
(breakpoint value
)
3037 (setf (breakpoint-%info breakpoint
) value
)
3038 (let ((other (breakpoint-unknown-return-partner breakpoint
)))
3040 (setf (breakpoint-%info other
) value
))))
3042 ;;;; BREAKPOINT-ACTIVE-P and DELETE-BREAKPOINT
3044 (defun breakpoint-active-p (breakpoint)
3045 (ecase (breakpoint-status breakpoint
)
3047 ((:inactive
:deleted
) nil
)))
3049 ;;; Free system storage and remove computational overhead associated
3050 ;;; with breakpoint. After calling this, breakpoint is completely
3051 ;;; impotent and can never become active again.
3052 (defun delete-breakpoint (breakpoint)
3053 (let ((status (breakpoint-status breakpoint
)))
3054 (unless (eq status
:deleted
)
3055 (when (eq status
:active
)
3056 (deactivate-breakpoint breakpoint
))
3057 (setf (breakpoint-status breakpoint
) :deleted
)
3058 (let ((other (breakpoint-unknown-return-partner breakpoint
)))
3060 (setf (breakpoint-status other
) :deleted
)))
3061 (when (eq (breakpoint-kind breakpoint
) :fun-end
)
3062 (let* ((starter (breakpoint-start-helper breakpoint
))
3063 (breakpoints (delete breakpoint
3064 (the list
(breakpoint-info starter
)))))
3065 (setf (breakpoint-info starter
) breakpoints
)
3067 (delete-breakpoint starter
)
3068 (setf (compiled-debug-fun-end-starter
3069 (breakpoint-what breakpoint
))
3073 ;;;; C call out stubs
3075 ;;; This actually installs the break instruction in the component. It
3076 ;;; returns the overwritten bits. You must call this in a context in
3077 ;;; which GC is disabled, so that Lisp doesn't move objects around
3078 ;;; that C is pointing to.
3079 (sb!alien
:define-alien-routine
"breakpoint_install" sb
!alien
:unsigned-int
3080 (code-obj sb
!alien
:unsigned
)
3081 (pc-offset sb
!alien
:int
))
3083 ;;; This removes the break instruction and replaces the original
3084 ;;; instruction. You must call this in a context in which GC is disabled
3085 ;;; so Lisp doesn't move objects around that C is pointing to.
3086 (sb!alien
:define-alien-routine
"breakpoint_remove" sb
!alien
:void
3087 (code-obj sb
!alien
:unsigned
)
3088 (pc-offset sb
!alien
:int
)
3089 (old-inst sb
!alien
:unsigned-int
))
3091 (sb!alien
:define-alien-routine
"breakpoint_do_displaced_inst" sb
!alien
:void
3092 (scp (* os-context-t
))
3093 (orig-inst sb
!alien
:unsigned-int
))
3095 ;;;; breakpoint handlers (layer between C and exported interface)
3097 ;;; This maps components to a mapping of offsets to BREAKPOINT-DATAs.
3098 (defvar *component-breakpoint-offsets
* (make-hash-table :test
'eq
:synchronized t
))
3100 ;;; This returns the BREAKPOINT-DATA object associated with component cross
3101 ;;; offset. If none exists, this makes one, installs it, and returns it.
3102 (defun breakpoint-data (component offset
&optional
(create t
))
3103 (flet ((install-breakpoint-data ()
3105 (let ((data (make-breakpoint-data component offset
)))
3106 (push (cons offset data
)
3107 (gethash component
*component-breakpoint-offsets
*))
3109 (let ((offsets (gethash component
*component-breakpoint-offsets
*)))
3111 (let ((data (assoc offset offsets
)))
3114 (install-breakpoint-data)))
3115 (install-breakpoint-data)))))
3117 ;;; We use this when there are no longer any active breakpoints
3118 ;;; corresponding to DATA.
3119 (defun delete-breakpoint-data (data)
3120 ;; Again, this looks brittle. Is there no danger of being interrupted
3122 (let* ((component (breakpoint-data-component data
))
3123 (offsets (delete (breakpoint-data-offset data
)
3124 (gethash component
*component-breakpoint-offsets
*)
3127 (setf (gethash component
*component-breakpoint-offsets
*) offsets
)
3128 (remhash component
*component-breakpoint-offsets
*)))
3131 ;;; The C handler for interrupts calls this when it has a
3132 ;;; debugging-tool break instruction. This does *not* handle all
3133 ;;; breaks; for example, it does not handle breaks for internal
3135 (defun handle-breakpoint (offset component signal-context
)
3136 (let ((data (breakpoint-data component offset nil
)))
3138 (error "unknown breakpoint in ~S at offset ~S"
3139 (debug-fun-name (debug-fun-from-pc component offset
))
3141 (let ((breakpoints (breakpoint-data-breakpoints data
)))
3142 (if (or (null breakpoints
)
3143 (eq (breakpoint-kind (car breakpoints
)) :fun-end
))
3144 (handle-fun-end-breakpoint-aux breakpoints data signal-context
)
3145 (handle-breakpoint-aux breakpoints data
3146 offset component signal-context
)))))
3148 ;;; This holds breakpoint-datas while invoking the breakpoint hooks
3149 ;;; associated with that particular component and location. While they
3150 ;;; are executing, if we hit the location again, we ignore the
3151 ;;; breakpoint to avoid infinite recursion. fun-end breakpoints
3152 ;;; must work differently since the breakpoint-data is unique for each
3154 (defvar *executing-breakpoint-hooks
* nil
)
3156 ;;; This handles code-location and DEBUG-FUN :FUN-START
3158 (defun handle-breakpoint-aux (breakpoints data offset component signal-context
)
3160 (bug "breakpoint that nobody wants"))
3161 (unless (member data
*executing-breakpoint-hooks
*)
3162 (let ((*executing-breakpoint-hooks
* (cons data
3163 *executing-breakpoint-hooks
*)))
3164 (invoke-breakpoint-hooks breakpoints signal-context
)))
3165 ;; At this point breakpoints may not hold the same list as
3166 ;; BREAKPOINT-DATA-BREAKPOINTS since invoking hooks may have allowed
3167 ;; a breakpoint deactivation. In fact, if all breakpoints were
3168 ;; deactivated then data is invalid since it was deleted and so the
3169 ;; correct one must be looked up if it is to be used. If there are
3170 ;; no more breakpoints active at this location, then the normal
3171 ;; instruction has been put back, and we do not need to
3172 ;; DO-DISPLACED-INST.
3173 (setf data
(breakpoint-data component offset nil
))
3174 (when (and data
(breakpoint-data-breakpoints data
))
3175 ;; The breakpoint is still active, so we need to execute the
3176 ;; displaced instruction and leave the breakpoint instruction
3177 ;; behind. The best way to do this is different on each machine,
3178 ;; so we just leave it up to the C code.
3179 (breakpoint-do-displaced-inst signal-context
3180 (breakpoint-data-instruction data
))
3181 ;; Some platforms have no usable sigreturn() call. If your
3182 ;; implementation of arch_do_displaced_inst() _does_ sigreturn(),
3183 ;; it's polite to warn here
3184 #!+(and sparc solaris
)
3185 (error "BREAKPOINT-DO-DISPLACED-INST returned?")))
3187 (defun invoke-breakpoint-hooks (breakpoints signal-context
)
3188 (let* ((frame (signal-context-frame signal-context
)))
3189 (dolist (bpt breakpoints
)
3190 (funcall (breakpoint-hook-fun bpt
)
3192 ;; If this is an :UNKNOWN-RETURN-PARTNER, then pass the
3193 ;; hook function the original breakpoint, so that users
3194 ;; aren't forced to confront the fact that some
3195 ;; breakpoints really are two.
3196 (if (eq (breakpoint-kind bpt
) :unknown-return-partner
)
3197 (breakpoint-unknown-return-partner bpt
)
3200 (defun signal-context-frame (signal-context)
3203 (declare (optimize (inhibit-warnings 3)))
3204 (sb!alien
:sap-alien signal-context
(* os-context-t
))))
3205 (cfp (int-sap (sb!vm
:context-register scp sb
!vm
::cfp-offset
))))
3206 (compute-calling-frame cfp
3207 ;; KLUDGE: This argument is ignored on
3208 ;; x86oids in this scenario, but is
3209 ;; declared to be a SAP.
3210 #!+(or x86 x86-64
) (sb!vm
:context-pc scp
)
3211 #!-
(or x86 x86-64
) nil
3214 (defun handle-fun-end-breakpoint (offset component context
)
3215 (let ((data (breakpoint-data component offset nil
)))
3217 (error "unknown breakpoint in ~S at offset ~S"
3218 (debug-fun-name (debug-fun-from-pc component offset
))
3220 (let ((breakpoints (breakpoint-data-breakpoints data
)))
3222 (aver (eq (breakpoint-kind (car breakpoints
)) :fun-end
))
3223 (handle-fun-end-breakpoint-aux breakpoints data context
)))))
3225 ;;; Either HANDLE-BREAKPOINT calls this for :FUN-END breakpoints
3226 ;;; [old C code] or HANDLE-FUN-END-BREAKPOINT calls this directly
3228 (defun handle-fun-end-breakpoint-aux (breakpoints data signal-context
)
3229 ;; FIXME: This looks brittle: what if we are interrupted somewhere
3230 ;; here? ...or do we have interrupts disabled here?
3231 (delete-breakpoint-data data
)
3234 (declare (optimize (inhibit-warnings 3)))
3235 (sb!alien
:sap-alien signal-context
(* os-context-t
))))
3236 (frame (signal-context-frame signal-context
))
3237 (component (breakpoint-data-component data
))
3238 (cookie (gethash component
*fun-end-cookies
*)))
3239 (remhash component
*fun-end-cookies
*)
3240 (dolist (bpt breakpoints
)
3241 (funcall (breakpoint-hook-fun bpt
)
3243 (get-fun-end-breakpoint-values scp
)
3246 (defun get-fun-end-breakpoint-values (scp)
3247 (let ((ocfp (int-sap (sb!vm
:context-register
3249 #!-
(or x86 x86-64
) sb
!vm
::ocfp-offset
3250 #!+(or x86 x86-64
) sb
!vm
::ebx-offset
)))
3251 (nargs (make-lisp-obj
3252 (sb!vm
:context-register scp sb
!vm
::nargs-offset
)))
3253 (reg-arg-offsets '#.sb
!vm
::*register-arg-offsets
*)
3256 (dotimes (arg-num nargs
)
3257 (push (if reg-arg-offsets
3259 (sb!vm
:context-register scp
(pop reg-arg-offsets
)))
3260 (stack-ref ocfp
(+ arg-num
3261 #!+(or x86 x86-64
) sb
!vm
::sp-
>fp-offset
)))
3263 (nreverse results
)))
3265 ;;;; MAKE-BOGUS-LRA (used for :FUN-END breakpoints)
3267 (defconstant bogus-lra-constants
3268 #!-
(or x86-64 x86
) 1
3270 ;; One more for a fixup vector
3273 ;;; Make a bogus LRA object that signals a breakpoint trap when
3274 ;;; returned to. If the breakpoint trap handler returns, REAL-LRA is
3275 ;;; returned to. Three values are returned: the bogus LRA object, the
3276 ;;; code component it is part of, and the PC offset for the trap
3278 (defun make-bogus-lra (real-lra)
3280 ;; These are really code labels, not variables: but this way we get
3282 (let* ((src-start (static-foreign-symbol-sap "fun_end_breakpoint_guts"))
3283 (src-end (static-foreign-symbol-sap "fun_end_breakpoint_end"))
3284 (trap-loc (static-foreign-symbol-sap "fun_end_breakpoint_trap"))
3285 (length (sap- src-end src-start
))
3287 (sb!c
:allocate-code-object bogus-lra-constants length
))
3288 (dst-start (code-instructions code-object
)))
3289 (declare (type system-area-pointer
3290 src-start src-end dst-start trap-loc
)
3291 (type index length
))
3292 (setf (%code-debug-info code-object
) :bogus-lra
)
3294 (setf (code-header-ref code-object real-lra-slot
) real-lra
3295 ;; Set up the widetag and header of LRA
3296 ;; The header contains the same thing as the code object header,
3297 ;; the number of boxed words, which include slots and
3298 ;; constants and it has to be double word aligned.
3300 ;; It used to be a part of the fun_end_breakpoint_guts
3301 ;; but its position and value depend on the offsets
3302 ;; and alignment of code object slots.
3303 (sap-ref-word dst-start
(- sb
!vm
:n-word-bits
))
3304 (+ sb
!vm
:return-pc-header-widetag
3305 (logandc2 (+ code-constants-offset
3310 (multiple-value-bind (offset code
) (compute-lra-data-from-pc real-lra
)
3311 (setf (code-header-ref code-object real-lra-slot
) code
)
3312 (setf (code-header-ref code-object
(1+ real-lra-slot
)) offset
))
3313 (system-area-ub8-copy src-start
0 dst-start
0 length
)
3315 (sb!vm
:sanctify-for-execution code-object
)
3317 (values dst-start code-object
(sap- trap-loc src-start
))
3319 (let ((new-lra (make-lisp-obj (+ (sap-int dst-start
)
3320 sb
!vm
:other-pointer-lowtag
))))
3321 ;; We used to set the header value of the LRA here to the
3322 ;; offset from the enclosing component to the LRA header, but
3323 ;; MAKE-LISP-OBJ actually checks the value before we get a
3324 ;; chance to set it, so it's now done in arch-assem.S.
3325 (values new-lra code-object
(sap- trap-loc src-start
))))))
3329 ;;; This appears here because it cannot go with the DEBUG-FUN
3330 ;;; interface since DO-DEBUG-BLOCK-LOCATIONS isn't defined until after
3331 ;;; the DEBUG-FUN routines.
3333 ;;; Return a code-location before the body of a function and after all
3334 ;;; the arguments are in place; or if that location can't be
3335 ;;; determined due to a lack of debug information, return NIL.
3336 (defun debug-fun-start-location (debug-fun)
3337 (etypecase debug-fun
3339 (code-location-from-pc debug-fun
3340 (sb!c
::compiled-debug-fun-start-pc
3341 (compiled-debug-fun-compiler-debug-fun
3344 ;; (There used to be more cases back before sbcl-0.7.0, when
3345 ;; we did special tricks to debug the IR1 interpreter.)
3349 ;;;; Single-stepping
3351 ;;; The single-stepper works by inserting conditional trap instructions
3352 ;;; into the generated code (see src/compiler/*/call.lisp), currently:
3354 ;;; 1) Before the code generated for a function call that was
3355 ;;; translated to a VOP
3356 ;;; 2) Just before the call instruction for a full call
3358 ;;; In both cases, the trap will only be executed if stepping has been
3359 ;;; enabled, in which case it'll ultimately be handled by
3360 ;;; HANDLE-SINGLE-STEP-TRAP, which will either signal a stepping condition,
3361 ;;; or replace the function that's about to be called with a wrapper
3362 ;;; which will signal the condition.
3364 (defun handle-single-step-trap (kind callee-register-offset
)
3365 (let ((context (nth-interrupt-context (1- *free-interrupt-context-index
*))))
3366 ;; The following calls must get tail-call eliminated for
3367 ;; *STEP-FRAME* to get set correctly on non-x86.
3368 (if (= kind single-step-before-trap
)
3369 (handle-single-step-before-trap context
)
3370 (handle-single-step-around-trap context callee-register-offset
))))
3372 (defvar *step-frame
* nil
)
3374 (defun handle-single-step-before-trap (context)
3375 (let ((step-info (single-step-info-from-context context
)))
3376 ;; If there was not enough debug information available, there's no
3377 ;; sense in signaling the condition.
3381 (signal-context-frame (sb!alien
::alien-sap context
))
3383 ;; KLUDGE: Use the first non-foreign frame as the
3384 ;; *STACK-TOP-HINT*. Getting the frame from the signal
3385 ;; context as on x86 would be cleaner, but
3386 ;; SIGNAL-CONTEXT-FRAME doesn't seem seem to work at all
3388 (loop with frame
= (frame-down (top-frame))
3390 for dfun
= (frame-debug-fun frame
)
3391 do
(when (typep dfun
'compiled-debug-fun
)
3393 do
(setf frame
(frame-down frame
)))))
3394 (sb!impl
::step-form step-info
3395 ;; We could theoretically store information in
3396 ;; the debug-info about to determine the
3397 ;; arguments here, but for now let's just pass
3401 ;;; This function will replace the fdefn / function that was in the
3402 ;;; register at CALLEE-REGISTER-OFFSET with a wrapper function. To
3403 ;;; ensure that the full call will use the wrapper instead of the
3404 ;;; original, conditional trap must be emitted before the fdefn /
3405 ;;; function is converted into a raw address.
3406 (defun handle-single-step-around-trap (context callee-register-offset
)
3407 ;; Fetch the function / fdefn we're about to call from the
3408 ;; appropriate register.
3409 (let* ((callee (make-lisp-obj
3410 (context-register context callee-register-offset
)))
3411 (step-info (single-step-info-from-context context
)))
3412 ;; If there was not enough debug information available, there's no
3413 ;; sense in signaling the condition.
3415 (return-from handle-single-step-around-trap
))
3416 (let* ((fun (lambda (&rest args
)
3418 (apply (typecase callee
3419 (fdefn (fdefn-fun callee
))
3422 ;; Signal a step condition
3424 (let ((*step-frame
* (frame-down (top-frame))))
3425 (sb!impl
::step-form step-info args
))))
3426 ;; And proceed based on its return value.
3428 ;; STEP-INTO was selected. Use *STEP-OUT* to
3429 ;; let the stepper know that selecting the
3430 ;; STEP-OUT restart is valid inside this
3431 (let ((sb!impl
::*step-out
* :maybe
))
3432 ;; Pass the return values of the call to
3433 ;; STEP-VALUES, which will signal a
3434 ;; condition with them in the VALUES slot.
3436 (multiple-value-call #'sb
!impl
::step-values
3439 ;; If the user selected the STEP-OUT
3440 ;; restart during the call, resume
3442 (when (eq sb
!impl
::*step-out
* t
)
3443 (sb!impl
::enable-stepping
))))
3444 ;; STEP-NEXT / CONTINUE / OUT selected:
3445 ;; Disable the stepper for the duration of
3447 (sb!impl
::with-stepping-disabled
3449 (new-callee (etypecase callee
3451 (let ((fdefn (make-fdefn (gensym))))
3452 (setf (fdefn-fun fdefn
) fun
)
3455 ;; And then store the wrapper in the same place.
3456 (with-pinned-objects (new-callee)
3457 ;; %SET-CONTEXT-REGISTER is a function, so the address of
3458 ;; NEW-CALLEE gets converted to a fixnum before passing, which
3459 ;; won't keep NEW-CALLEE pinned down. Once it's inside
3460 ;; CONTEXT, which is registered in thread->interrupt_contexts,
3461 ;; it will properly point to NEW-CALLEE.
3462 (setf (context-register context callee-register-offset
)
3463 (get-lisp-obj-address new-callee
))))))
3465 ;;; Given a signal context, fetch the step-info that's been stored in
3466 ;;; the debug info at the trap point.
3467 (defun single-step-info-from-context (context)
3468 (multiple-value-bind (pc-offset code
)
3469 (compute-lra-data-from-pc (context-pc context
))
3470 (let* ((debug-fun (debug-fun-from-pc code pc-offset
))
3471 (location (code-location-from-pc debug-fun
3476 (fill-in-code-location location
)
3477 (code-location-debug-source location
)
3478 (compiled-code-location-step-info location
))
3482 ;;; Return the frame that triggered a single-step condition. Used to
3483 ;;; provide a *STACK-TOP-HINT*.
3484 (defun find-stepped-frame ()
3488 ;;;; fetching errorful function name
3490 ;;; This flag is used to prevent infinite recursive lossage when
3491 ;;; we can't find the caller for some reason.
3492 (defvar *finding-frame
* nil
)
3494 (defun find-caller-frame ()
3495 (unless *finding-frame
*
3497 (let* ((*finding-frame
* t
)
3498 (frame (frame-down (frame-down (top-frame)))))
3499 (flush-frames-above frame
)
3501 ((or error debug-condition
) ()))))
3503 (defun find-interrupted-frame ()
3504 (when (plusp *free-interrupt-context-index
*)
3506 (signal-context-frame
3508 (nth-interrupt-context (1- *free-interrupt-context-index
*))))
3509 ((or error debug-condition
) ()))))
3511 (defun find-caller-of-named-frame (name)
3512 (unless *finding-frame
*
3514 (let ((*finding-frame
* t
))
3515 (do ((frame (top-frame) (frame-down frame
)))
3517 (when (and (compiled-frame-p frame
)
3518 (eq name
(debug-fun-name
3519 (frame-debug-fun frame
))))
3520 (let ((caller (frame-down frame
)))
3521 (flush-frames-above caller
)
3523 ((or error debug-condition
) ()))))