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