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