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