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