0.6.7.26: fixed breakpoints on OpenBSD
[sbcl/smoofra.git] / src / code / debug-int.lisp
bloba4d95df3a7f9c1400108cfe373beefb2ba8d4cef
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-info (debug-condition)
46 #!+sb-doc
47 (:documentation "There is absolutely no debugging information available.")
48 (:report (lambda (condition stream)
49 (declare (ignore condition))
50 (fresh-line stream)
51 (write-line "No debugging information available." stream))))
53 (define-condition no-debug-function-returns (debug-condition)
54 ((debug-function :reader no-debug-function-returns-debug-function
55 :initarg :debug-function))
56 #!+sb-doc
57 (:documentation
58 "The system could not return values from a frame with debug-function since
59 it lacked information about returning values.")
60 (:report (lambda (condition stream)
61 (let ((fun (debug-function-function
62 (no-debug-function-returns-debug-function condition))))
63 (format stream
64 "~&Cannot return values from ~:[frame~;~:*~S~] since ~
65 the debug information lacks details about returning ~
66 values here."
67 fun)))))
69 (define-condition no-debug-blocks (debug-condition)
70 ((debug-function :reader no-debug-blocks-debug-function
71 :initarg :debug-function))
72 #!+sb-doc
73 (:documentation "The debug-function has no debug-block information.")
74 (:report (lambda (condition stream)
75 (format stream "~&~S has no debug-block information."
76 (no-debug-blocks-debug-function condition)))))
78 (define-condition no-debug-vars (debug-condition)
79 ((debug-function :reader no-debug-vars-debug-function
80 :initarg :debug-function))
81 #!+sb-doc
82 (:documentation "The debug-function has no DEBUG-VAR information.")
83 (:report (lambda (condition stream)
84 (format stream "~&~S has no debug variable information."
85 (no-debug-vars-debug-function condition)))))
87 (define-condition lambda-list-unavailable (debug-condition)
88 ((debug-function :reader lambda-list-unavailable-debug-function
89 :initarg :debug-function))
90 #!+sb-doc
91 (:documentation
92 "The debug-function has no lambda-list since argument DEBUG-VARs are
93 unavailable.")
94 (:report (lambda (condition stream)
95 (format stream "~&~S has no lambda-list information available."
96 (lambda-list-unavailable-debug-function condition)))))
98 (define-condition invalid-value (debug-condition)
99 ((debug-var :reader invalid-value-debug-var :initarg :debug-var)
100 (frame :reader invalid-value-frame :initarg :frame))
101 (:report (lambda (condition stream)
102 (format stream "~&~S has :invalid or :unknown value in ~S."
103 (invalid-value-debug-var condition)
104 (invalid-value-frame condition)))))
106 (define-condition ambiguous-variable-name (debug-condition)
107 ((name :reader ambiguous-variable-name-name :initarg :name)
108 (frame :reader ambiguous-variable-name-frame :initarg :frame))
109 (:report (lambda (condition stream)
110 (format stream "~&~S names more than one valid variable in ~S."
111 (ambiguous-variable-name-name condition)
112 (ambiguous-variable-name-frame condition)))))
114 ;;;; errors and DEBUG-SIGNAL
116 ;;; The debug-internals code tries to signal all programmer errors as
117 ;;; subtypes of DEBUG-ERROR. There are calls to ERROR signalling
118 ;;; SIMPLE-ERRORs, but these dummy checks in the code and shouldn't
119 ;;; come up.
121 ;;; While under development, this code also signals errors in code
122 ;;; branches that remain unimplemented.
124 (define-condition debug-error (error) ()
125 #!+sb-doc
126 (:documentation
127 "All programmer errors from using the interface for building debugging
128 tools inherit from this type."))
130 (define-condition unhandled-condition (debug-error)
131 ((condition :reader unhandled-condition-condition :initarg :condition))
132 (:report (lambda (condition stream)
133 (format stream "~&unhandled DEBUG-CONDITION:~%~A"
134 (unhandled-condition-condition condition)))))
136 (define-condition unknown-code-location (debug-error)
137 ((code-location :reader unknown-code-location-code-location
138 :initarg :code-location))
139 (:report (lambda (condition stream)
140 (format stream "~&invalid use of an unknown code-location: ~S"
141 (unknown-code-location-code-location condition)))))
143 (define-condition unknown-debug-var (debug-error)
144 ((debug-var :reader unknown-debug-var-debug-var :initarg :debug-var)
145 (debug-function :reader unknown-debug-var-debug-function
146 :initarg :debug-function))
147 (:report (lambda (condition stream)
148 (format stream "~&~S is not in ~S."
149 (unknown-debug-var-debug-var condition)
150 (unknown-debug-var-debug-function condition)))))
152 (define-condition invalid-control-stack-pointer (debug-error)
154 (:report (lambda (condition stream)
155 (declare (ignore condition))
156 (fresh-line stream)
157 (write-string "invalid control stack pointer" stream))))
159 (define-condition frame-function-mismatch (debug-error)
160 ((code-location :reader frame-function-mismatch-code-location
161 :initarg :code-location)
162 (frame :reader frame-function-mismatch-frame :initarg :frame)
163 (form :reader frame-function-mismatch-form :initarg :form))
164 (:report (lambda (condition stream)
165 (format stream
166 "~&Form was preprocessed for ~S,~% but called on ~S:~% ~S"
167 (frame-function-mismatch-code-location condition)
168 (frame-function-mismatch-frame condition)
169 (frame-function-mismatch-form condition)))))
171 ;;; This signals debug-conditions. If they go unhandled, then signal an
172 ;;; unhandled-condition error.
174 ;;; ??? Get SIGNAL in the right package!
175 (defmacro debug-signal (datum &rest arguments)
176 `(let ((condition (make-condition ,datum ,@arguments)))
177 (signal condition)
178 (error 'unhandled-condition :condition condition)))
180 ;;;; structures
181 ;;;;
182 ;;;; Most of these structures model information stored in internal
183 ;;;; data structures created by the compiler. Whenever comments
184 ;;;; preface an object or type with "compiler", they refer to the
185 ;;;; internal compiler thing, not to the object or type with the same
186 ;;;; name in the "DI" package.
188 ;;;; DEBUG-VARs
190 ;;; These exist for caching data stored in packed binary form in
191 ;;; compiler debug-functions. Debug-functions store these.
192 (defstruct (debug-var (:constructor nil))
193 ;; the name of the variable
194 (symbol (required-argument) :type symbol)
195 ;; a unique integer identification relative to other variables with the same
196 ;; symbol
197 (id 0 :type sb!c::index)
198 ;; Does the variable always have a valid value?
199 (alive-p nil :type boolean))
200 (def!method print-object ((debug-var debug-var) stream)
201 (print-unreadable-object (debug-var stream :type t :identity t)
202 (format stream
203 "~S ~D"
204 (debug-var-symbol debug-var)
205 (debug-var-id debug-var))))
207 #!+sb-doc
208 (setf (fdocumentation 'debug-var-id 'function)
209 "Returns the integer that makes DEBUG-VAR's name and package unique
210 with respect to other DEBUG-VARs in the same function.")
212 (defstruct (compiled-debug-var
213 (:include debug-var)
214 (:constructor make-compiled-debug-var
215 (symbol id alive-p sc-offset save-sc-offset)))
216 ;; Storage class and offset. (unexported).
217 (sc-offset nil :type sb!c::sc-offset)
218 ;; Storage class and offset when saved somewhere.
219 (save-sc-offset nil :type (or sb!c::sc-offset null)))
221 (defstruct (interpreted-debug-var
222 (:include debug-var (alive-p t))
223 (:constructor make-interpreted-debug-var (symbol ir1-var)))
224 ;; This is the IR1 structure that holds information about interpreted vars.
225 (ir1-var nil :type sb!c::lambda-var))
227 ;;;; frames
229 ;;; These represent call-frames on the stack.
230 (defstruct (frame (:constructor nil))
231 ;; the next frame up, or NIL when top frame
232 (up nil :type (or frame null))
233 ;; the previous frame down, or NIL when the bottom frame. Before
234 ;; computing the next frame down, this slot holds the frame pointer
235 ;; to the control stack for the given frame. This lets us get the
236 ;; next frame down and the return-pc for that frame.
237 (%down :unparsed :type (or frame (member nil :unparsed)))
238 ;; the debug-function for the function whose call this frame
239 ;; represents
240 (debug-function nil :type debug-function)
241 ;; the code-location to continue upon return to frame
242 (code-location nil :type code-location)
243 ;; an a-list of catch-tags to code-locations
244 (%catches :unparsed :type (or list (member :unparsed)))
245 ;; pointer to frame on control stack. (unexported) When this frame
246 ;; is an interpreted-frame, this pointer is an index into the
247 ;; interpreter's stack.
248 pointer
249 ;; This is the frame's number for prompt printing. Top is zero.
250 (number 0 :type index))
252 #!+sb-doc
253 (setf (fdocumentation 'frame-up 'function)
254 "Returns the frame immediately above frame on the stack. When frame is
255 the top of the stack, this returns nil.")
257 #!+sb-doc
258 (setf (fdocumentation 'frame-debug-function 'function)
259 "Returns the debug-function for the function whose call frame represents.")
261 #!+sb-doc
262 (setf (fdocumentation 'frame-code-location 'function)
263 "Returns the code-location where the frame's debug-function will continue
264 running when program execution returns to this frame. If someone
265 interrupted this frame, the result could be an unknown code-location.")
267 (defstruct (compiled-frame
268 (:include frame)
269 (:constructor make-compiled-frame
270 (pointer up debug-function code-location number
271 #!+gengc saved-state-chain
272 &optional escaped)))
273 ;; This indicates whether someone interrupted the frame.
274 ;; (unexported). If escaped, this is a pointer to the state that was
275 ;; saved when we were interrupted. On the non-gengc system, this is
276 ;; a pointer to an os_context_t, i.e. the third argument to an
277 ;; SA_SIGACTION-style signal handler. On the gengc system, this is a
278 ;; state pointer from SAVED-STATE-CHAIN.
279 escaped
280 ;; a list of SAPs to saved states. Each time we unwind past an
281 ;; exception, we pop the next entry off this list. When we get to
282 ;; the end of the list, there is nothing else on the stack.
283 #!+gengc (saved-state-chain nil :type list))
284 (def!method print-object ((obj compiled-frame) str)
285 (print-unreadable-object (obj str :type t)
286 (format str
287 "~S~:[~;, interrupted~]"
288 (debug-function-name (frame-debug-function obj))
289 (compiled-frame-escaped obj))))
291 (defstruct (interpreted-frame
292 (:include frame)
293 (:constructor make-interpreted-frame
294 (pointer up debug-function code-location number
295 real-frame closure)))
296 ;; This points to the compiled-frame for SB!EVAL:INTERNAL-APPLY-LOOP.
297 (real-frame nil :type compiled-frame)
298 ;; This is the closed over data used by the interpreter.
299 (closure nil :type simple-vector))
300 (def!method print-object ((obj interpreted-frame) str)
301 (print-unreadable-object (obj str :type t)
302 (prin1 (debug-function-name (frame-debug-function obj)) str)))
304 ;;;; DEBUG-FUNCTIONs
306 ;;; These exist for caching data stored in packed binary form in
307 ;;; compiler debug-functions. *COMPILED-DEBUG-FUNCTIONS* maps a
308 ;;; SB!C::DEBUG-FUNCTION to a DEBUG-FUNCTION. There should only be one
309 ;;; DEBUG-FUNCTION in existence for any function; that is, all
310 ;;; code-locations and other objects that reference DEBUG-FUNCTIONs
311 ;;; point to unique objects. This is due to the overhead in cached
312 ;;; information.
313 (defstruct debug-function
314 ;; Some representation of the function arguments. See
315 ;; DEBUG-FUNCTION-LAMBDA-LIST.
316 ;; NOTE: must parse vars before parsing arg list stuff.
317 (%lambda-list :unparsed)
318 ;; Cached DEBUG-VARS information. (unexported).
319 ;; These are sorted by their name.
320 (%debug-vars :unparsed :type (or simple-vector null (member :unparsed)))
321 ;; Cached debug-block information. This is NIL when we have tried to
322 ;; parse the packed binary info, but none is available.
323 (blocks :unparsed :type (or simple-vector null (member :unparsed)))
324 ;; The actual function if available.
325 (%function :unparsed :type (or null function (member :unparsed))))
326 (def!method print-object ((obj debug-function) stream)
327 (print-unreadable-object (obj stream :type t)
328 (prin1 (debug-function-name obj) stream)))
330 (defstruct (compiled-debug-function
331 (:include debug-function)
332 (:constructor %make-compiled-debug-function
333 (compiler-debug-fun component)))
334 ;; Compiler's dumped debug-function information. (unexported).
335 (compiler-debug-fun nil :type sb!c::compiled-debug-function)
336 ;; Code object. (unexported).
337 component
338 ;; The :FUNCTION-START breakpoint (if any) used to facilitate
339 ;; function end breakpoints.
340 (end-starter nil :type (or null breakpoint)))
342 ;;; This maps SB!C::COMPILED-DEBUG-FUNCTIONs to
343 ;;; COMPILED-DEBUG-FUNCTIONs, so we can get at cached stuff and not
344 ;;; duplicate COMPILED-DEBUG-FUNCTION structures.
345 (defvar *compiled-debug-functions* (make-hash-table :test 'eq))
347 ;;; Make a COMPILED-DEBUG-FUNCTION for a SB!C::COMPILER-DEBUG-FUNCTION
348 ;;; and its component. This maps the latter to the former in
349 ;;; *COMPILED-DEBUG-FUNCTIONS*. If there already is a
350 ;;; COMPILED-DEBUG-FUNCTION, then this returns it from
351 ;;; *COMPILED-DEBUG-FUNCTIONS*.
352 (defun make-compiled-debug-function (compiler-debug-fun component)
353 (or (gethash compiler-debug-fun *compiled-debug-functions*)
354 (setf (gethash compiler-debug-fun *compiled-debug-functions*)
355 (%make-compiled-debug-function compiler-debug-fun component))))
357 (defstruct (interpreted-debug-function
358 (:include debug-function)
359 (:constructor %make-interpreted-debug-function (ir1-lambda)))
360 ;; This is the IR1 lambda that this debug-function represents.
361 (ir1-lambda nil :type sb!c::clambda))
363 (defstruct (bogus-debug-function
364 (:include debug-function)
365 (:constructor make-bogus-debug-function
366 (%name &aux (%lambda-list nil) (%debug-vars nil)
367 (blocks nil) (%function nil))))
368 %name)
370 (defvar *ir1-lambda-debug-function* (make-hash-table :test 'eq))
372 (defun make-interpreted-debug-function (ir1-lambda)
373 (let ((home-lambda (sb!c::lambda-home ir1-lambda)))
374 (or (gethash home-lambda *ir1-lambda-debug-function*)
375 (setf (gethash home-lambda *ir1-lambda-debug-function*)
376 (%make-interpreted-debug-function home-lambda)))))
378 ;;;; DEBUG-BLOCKs
380 ;;; These exist for caching data stored in packed binary form in compiler
381 ;;; debug-blocks.
382 (defstruct (debug-block (:constructor nil))
383 ;; Code-locations where execution continues after this block.
384 (successors nil :type list)
385 ;; This indicates whether the block is a special glob of code shared by
386 ;; various functions and tucked away elsewhere in a component. This kind of
387 ;; block has no start code-location. In an interpreted-debug-block, this is
388 ;; always nil. This slot is in all debug-blocks since it is an exported
389 ;; interface.
390 (elsewhere-p nil :type boolean))
391 (def!method print-object ((obj debug-block) str)
392 (print-unreadable-object (obj str :type t)
393 (prin1 (debug-block-function-name obj) str)))
395 #!+sb-doc
396 (setf (fdocumentation 'debug-block-successors 'function)
397 "Returns the list of possible code-locations where execution may continue
398 when the basic-block represented by debug-block completes its execution.")
400 #!+sb-doc
401 (setf (fdocumentation 'debug-block-elsewhere-p 'function)
402 "Returns whether debug-block represents elsewhere code.")
404 (defstruct (compiled-debug-block (:include debug-block)
405 (:constructor
406 make-compiled-debug-block
407 (code-locations successors elsewhere-p)))
408 ;; Code-location information for the block.
409 (code-locations nil :type simple-vector))
411 (defstruct (interpreted-debug-block (:include debug-block
412 (elsewhere-p nil))
413 (:constructor %make-interpreted-debug-block
414 (ir1-block)))
415 ;; This is the IR1 block this debug-block represents.
416 (ir1-block nil :type sb!c::cblock)
417 ;; Code-location information for the block.
418 (locations :unparsed :type (or (member :unparsed) simple-vector)))
420 (defvar *ir1-block-debug-block* (make-hash-table :test 'eq))
422 ;;; Make a DEBUG-BLOCK for the interpreter's IR1-BLOCK. If we have it
423 ;;; in the cache, return it. If we need to make it, then first make
424 ;;; DEBUG-BLOCKs for all the IR1-BLOCKs in IR1-BLOCK's home lambda;
425 ;;; this makes sure all the successors of IR1-BLOCK have DEBUG-BLOCKs.
426 ;;; We need this to fill in the resulting DEBUG-BLOCK's successors
427 ;;; list with DEBUG-BLOCKs, not IR1-BLOCKs. After making all the
428 ;;; possible DEBUG-BLOCKs we'll need to reference, go back over the
429 ;;; list of new DEBUG-BLOCKs and fill in their successor slots with
430 ;;; lists of DEBUG-BLOCKs. Then look up our argument IR1-BLOCK to find
431 ;;; its DEBUG-BLOCK since we know we have it now.
432 (defun make-interpreted-debug-block (ir1-block)
433 (check-type ir1-block sb!c::cblock)
434 (let ((res (gethash ir1-block *ir1-block-debug-block*)))
435 (or res
436 (let ((lambda (sb!c::block-home-lambda ir1-block)))
437 (sb!c::do-blocks (block (sb!c::block-component ir1-block))
438 (when (eq lambda (sb!c::block-home-lambda block))
439 (push (setf (gethash block *ir1-block-debug-block*)
440 (%make-interpreted-debug-block block))
441 res)))
442 (dolist (block res)
443 (let* ((successors nil)
444 (cblock (interpreted-debug-block-ir1-block block))
445 (succ (sb!c::block-succ cblock))
446 (valid-succ
447 (if (and succ
448 (eq (car succ)
449 (sb!c::component-tail
450 (sb!c::block-component cblock))))
452 succ)))
453 (dolist (sblock valid-succ)
454 (let ((dblock (gethash sblock *ir1-block-debug-block*)))
455 (when dblock
456 (push dblock successors))))
457 (setf (debug-block-successors block) (nreverse successors))))
458 (gethash ir1-block *ir1-block-debug-block*)))))
460 ;;;; breakpoints
462 ;;; This is an internal structure that manages information about a
463 ;;; breakpoint locations. See *COMPONENT-BREAKPOINT-OFFSETS*.
464 (defstruct (breakpoint-data (:constructor make-breakpoint-data
465 (component offset)))
466 ;; This is the component in which the breakpoint lies.
467 component
468 ;; This is the byte offset into the component.
469 (offset nil :type sb!c::index)
470 ;; The original instruction replaced by the breakpoint.
471 (instruction nil :type (or null (unsigned-byte 32)))
472 ;; A list of user breakpoints at this location.
473 (breakpoints nil :type list))
474 (def!method print-object ((obj breakpoint-data) str)
475 (print-unreadable-object (obj str :type t)
476 (format str "~S at ~S"
477 (debug-function-name
478 (debug-function-from-pc (breakpoint-data-component obj)
479 (breakpoint-data-offset obj)))
480 (breakpoint-data-offset obj))))
482 (defstruct (breakpoint (:constructor %make-breakpoint
483 (hook-function what kind %info)))
484 ;; This is the function invoked when execution encounters the
485 ;; breakpoint. It takes a frame, the breakpoint, and optionally a
486 ;; list of values. Values are supplied for :FUNCTION-END breakpoints
487 ;; as values to return for the function containing the breakpoint.
488 ;; :FUNCTION-END breakpoint hook-functions also take a cookie
489 ;; argument. See COOKIE-FUN slot.
490 (hook-function nil :type function)
491 ;; CODE-LOCATION or DEBUG-FUNCTION
492 (what nil :type (or code-location debug-function))
493 ;; :CODE-LOCATION, :FUNCTION-START, or :FUNCTION-END for that kind
494 ;; of breakpoint. :UNKNOWN-RETURN-PARTNER if this is the partner of
495 ;; a :code-location breakpoint at an :UNKNOWN-RETURN code-location.
496 (kind nil :type (member :code-location :function-start :function-end
497 :unknown-return-partner))
498 ;; Status helps the user and the implementation.
499 (status :inactive :type (member :active :inactive :deleted))
500 ;; This is a backpointer to a breakpoint-data.
501 (internal-data nil :type (or null breakpoint-data))
502 ;; With code-locations whose type is :UNKNOWN-RETURN, there are
503 ;; really two breakpoints: one at the multiple-value entry point,
504 ;; and one at the single-value entry point. This slot holds the
505 ;; breakpoint for the other one, or NIL if this isn't at an
506 ;; :UNKNOWN-RETURN code location.
507 (unknown-return-partner nil :type (or null breakpoint))
508 ;; :FUNCTION-END breakpoints use a breakpoint at the :FUNCTION-START
509 ;; to establish the end breakpoint upon function entry. We do this
510 ;; by frobbing the LRA to jump to a special piece of code that
511 ;; breaks and provides the return values for the returnee. This slot
512 ;; points to the start breakpoint, so we can activate, deactivate,
513 ;; and delete it.
514 (start-helper nil :type (or null breakpoint))
515 ;; This is a hook users supply to get a dynamically unique cookie
516 ;; for identifying :FUNCTION-END breakpoint executions. That is, if
517 ;; there is one :FUNCTION-END breakpoint, but there may be multiple
518 ;; pending calls of its function on the stack. This function takes
519 ;; the cookie, and the hook-function takes the cookie too.
520 (cookie-fun nil :type (or null function))
521 ;; This slot users can set with whatever information they find useful.
522 %info)
523 (def!method print-object ((obj breakpoint) str)
524 (let ((what (breakpoint-what obj)))
525 (print-unreadable-object (obj str :type t)
526 (format str
527 "~S~:[~;~:*~S~]"
528 (etypecase what
529 (code-location what)
530 (debug-function (debug-function-name what)))
531 (etypecase what
532 (code-location nil)
533 (debug-function (breakpoint-kind obj)))))))
535 #!+sb-doc
536 (setf (fdocumentation 'breakpoint-hook-function 'function)
537 "Returns the breakpoint's function the system calls when execution encounters
538 the breakpoint, and it is active. This is SETF'able.")
540 #!+sb-doc
541 (setf (fdocumentation 'breakpoint-what 'function)
542 "Returns the breakpoint's what specification.")
544 #!+sb-doc
545 (setf (fdocumentation 'breakpoint-kind 'function)
546 "Returns the breakpoint's kind specification.")
548 ;;;; CODE-LOCATIONs
550 (defstruct (code-location (:constructor nil))
551 ;; This is the debug-function containing code-location.
552 (debug-function nil :type debug-function)
553 ;; This is initially :UNSURE. Upon first trying to access an
554 ;; :unparsed slot, if the data is unavailable, then this becomes t,
555 ;; and the code-location is unknown. If the data is available, this
556 ;; becomes nil, a known location. We can't use a separate type
557 ;; code-location for this since we must return code-locations before
558 ;; we can tell whether they're known or unknown. For example, when
559 ;; parsing the stack, we don't want to unpack all the variables and
560 ;; blocks just to make frames.
561 (%unknown-p :unsure :type (member t nil :unsure))
562 ;; This is the debug-block containing code-location. Possibly toss
563 ;; this out and just find it in the blocks cache in debug-function.
564 (%debug-block :unparsed :type (or debug-block (member :unparsed)))
565 ;; This is the number of forms processed by the compiler or loader
566 ;; before the top-level form containing this code-location.
567 (%tlf-offset :unparsed :type (or sb!c::index (member :unparsed)))
568 ;; This is the depth-first number of the node that begins
569 ;; code-location within its top-level form.
570 (%form-number :unparsed :type (or sb!c::index (member :unparsed))))
571 (def!method print-object ((obj code-location) str)
572 (print-unreadable-object (obj str :type t)
573 (prin1 (debug-function-name (code-location-debug-function obj))
574 str)))
576 #!+sb-doc
577 (setf (fdocumentation 'code-location-debug-function 'function)
578 "Returns the debug-function representing information about the function
579 corresponding to the code-location.")
581 (defstruct (compiled-code-location
582 (:include code-location)
583 (:constructor make-known-code-location
584 (pc debug-function %tlf-offset %form-number
585 %live-set kind &aux (%unknown-p nil)))
586 (:constructor make-compiled-code-location (pc debug-function)))
587 ;; This is an index into debug-function's component slot.
588 (pc nil :type sb!c::index)
589 ;; This is a bit-vector indexed by a variable's position in
590 ;; DEBUG-FUNCTION-DEBUG-VARS indicating whether the variable has a
591 ;; valid value at this code-location. (unexported).
592 (%live-set :unparsed :type (or simple-bit-vector (member :unparsed)))
593 ;; (unexported) To see SB!C::LOCATION-KIND, do
594 ;; (SB!KERNEL:TYPE-EXPAND 'SB!C::LOCATION-KIND).
595 (kind :unparsed :type (or (member :unparsed) sb!c::location-kind)))
597 (defstruct (interpreted-code-location
598 (:include code-location
599 (%unknown-p nil))
600 (:constructor make-interpreted-code-location
601 (ir1-node debug-function)))
602 ;; This is an index into debug-function's component slot.
603 (ir1-node nil :type sb!c::node))
605 ;;; DEBUG-SOURCEs
607 #!-sb-fluid (declaim (inline debug-source-root-number))
608 (defun debug-source-root-number (debug-source)
609 #!+sb-doc
610 "Returns the number of top-level forms processed by the compiler before
611 compiling this source. If this source is uncompiled, this is zero. This
612 may be zero even if the source is compiled since the first form in the first
613 file compiled in one compilation, for example, must have a root number of
614 zero -- the compiler saw no other top-level forms before it."
615 (sb!c::debug-source-source-root debug-source))
617 #!+sb-doc
618 (setf (fdocumentation 'sb!c::debug-source-from 'function)
619 "Returns an indication of the type of source. The following are the possible
620 values:
621 :file from a file (obtained by COMPILE-FILE if compiled).
622 :lisp from Lisp (obtained by COMPILE if compiled).")
624 #!+sb-doc
625 (setf (fdocumentation 'sb!c::debug-source-name 'function)
626 "Returns the actual source in some sense represented by debug-source, which
627 is related to DEBUG-SOURCE-FROM:
628 :file the pathname of the file.
629 :lisp a lambda-expression.")
631 #!+sb-doc
632 (setf (fdocumentation 'sb!c::debug-source-created 'function)
633 "Returns the universal time someone created the source. This may be nil if
634 it is unavailable.")
636 #!+sb-doc
637 (setf (fdocumentation 'sb!c::debug-source-compiled 'function)
638 "Returns the time someone compiled the source. This is nil if the source
639 is uncompiled.")
641 #!+sb-doc
642 (setf (fdocumentation 'sb!c::debug-source-start-positions 'function)
643 "This function returns the file position of each top-level form as an array
644 if debug-source is from a :file. If DEBUG-SOURCE-FROM is :lisp,
645 this returns nil.")
647 #!+sb-doc
648 (setf (fdocumentation 'sb!c::debug-source-p 'function)
649 "Returns whether object is a debug-source.")
651 ;;;; frames
653 ;;; This is used in FIND-ESCAPE-FRAME and with the bogus components
654 ;;; and LRAs used for :function-end breakpoints. When a components
655 ;;; debug-info slot is :bogus-lra, then the real-lra-slot contains the
656 ;;; real component to continue executing, as opposed to the bogus
657 ;;; component which appeared in some frame's LRA location.
658 (defconstant real-lra-slot sb!vm:code-constants-offset)
660 ;;; These are magically converted by the compiler.
661 (defun current-sp () (current-sp))
662 (defun current-fp () (current-fp))
663 (defun stack-ref (s n) (stack-ref s n))
664 (defun %set-stack-ref (s n value) (%set-stack-ref s n value))
665 (defun function-code-header (fun) (function-code-header fun))
666 #!-gengc (defun lra-code-header (lra) (lra-code-header lra))
667 (defun make-lisp-obj (value) (make-lisp-obj value))
668 (defun get-lisp-obj-address (thing) (get-lisp-obj-address thing))
669 (defun function-word-offset (fun) (function-word-offset fun))
671 #!-sb-fluid (declaim (inline cstack-pointer-valid-p))
672 (defun cstack-pointer-valid-p (x)
673 (declare (type system-area-pointer x))
674 #!-x86 ; stack grows toward high address values
675 (and (sap< x (current-sp))
676 (sap<= #!-gengc (int-sap control-stack-start)
677 #!+gengc (mutator-control-stack-base)
679 (zerop (logand (sap-int x) #b11)))
680 #!+x86 ; stack grows toward low address values
681 (and (sap>= x (current-sp))
682 (sap> (int-sap control-stack-end) x)
683 (zerop (logand (sap-int x) #b11))))
685 #!+(or gengc x86)
686 (sb!alien:def-alien-routine component-ptr-from-pc (system-area-pointer)
687 (pc system-area-pointer))
689 #!+(or gengc x86)
690 (defun component-from-component-ptr (component-ptr)
691 (declare (type system-area-pointer component-ptr))
692 (make-lisp-obj (logior (sap-int component-ptr)
693 sb!vm:other-pointer-type)))
695 ;;;; X86 support
697 #!+x86
698 (progn
700 (defun compute-lra-data-from-pc (pc)
701 (declare (type system-area-pointer pc))
702 (let ((component-ptr (component-ptr-from-pc pc)))
703 (unless (sap= component-ptr (int-sap #x0))
704 (let* ((code (component-from-component-ptr component-ptr))
705 (code-header-len (* (get-header-data code) sb!vm:word-bytes))
706 (pc-offset (- (sap-int pc)
707 (- (get-lisp-obj-address code)
708 sb!vm:other-pointer-type)
709 code-header-len)))
710 ; (format t "c-lra-fpc ~A ~A ~A~%" pc code pc-offset)
711 (values pc-offset code)))))
713 (defconstant sb!vm::nargs-offset #.sb!vm::ecx-offset)
715 ;;; Check for a valid return address - it could be any valid C/Lisp
716 ;;; address.
718 ;;; XXX Could be a little smarter.
719 #!-sb-fluid (declaim (inline ra-pointer-valid-p))
720 (defun ra-pointer-valid-p (ra)
721 (declare (type system-area-pointer ra))
722 (and
723 ;; Not the first page which is unmapped.
724 (>= (sap-int ra) 4096)
725 ;; Not a Lisp stack pointer.
726 (not (cstack-pointer-valid-p ra))))
728 ;;; Try to find a valid previous stack. This is complex on the x86 as
729 ;;; it can jump between C and Lisp frames. To help find a valid frame
730 ;;; it searches backwards.
732 ;;; XXX Should probably check whether it has reached the bottom of the
733 ;;; stack.
735 ;;; XXX Should handle interrupted frames, both Lisp and C. At present it
736 ;;; manages to find a fp trail, see linux hack below.
737 (defun x86-call-context (fp &key (depth 8))
738 (declare (type system-area-pointer fp)
739 (fixnum depth))
740 ;;(format t "*CC ~S ~S~%" fp depth)
741 (cond
742 ((not (cstack-pointer-valid-p fp))
743 #+nil (format t "debug invalid fp ~S~%" fp)
744 nil)
746 ;; Check the two possible frame pointers.
747 (let ((lisp-ocfp (sap-ref-sap fp (- (* (1+ sb!vm::ocfp-save-offset) 4))))
748 (lisp-ra (sap-ref-sap fp (- (* (1+ sb!vm::return-pc-save-offset)
749 4))))
750 (c-ocfp (sap-ref-sap fp (* 0 sb!vm:word-bytes)))
751 (c-ra (sap-ref-sap fp (* 1 sb!vm:word-bytes))))
752 (cond ((and (sap> lisp-ocfp fp) (cstack-pointer-valid-p lisp-ocfp)
753 (ra-pointer-valid-p lisp-ra)
754 (sap> c-ocfp fp) (cstack-pointer-valid-p c-ocfp)
755 (ra-pointer-valid-p c-ra))
756 #+nil (format t
757 "*C Both valid ~S ~S ~S ~S~%"
758 lisp-ocfp lisp-ra c-ocfp c-ra)
759 ;; Look forward another step to check their validity.
760 (let ((lisp-path-fp (x86-call-context lisp-ocfp
761 :depth (- depth 1)))
762 (c-path-fp (x86-call-context c-ocfp :depth (- depth 1))))
763 (cond ((and lisp-path-fp c-path-fp)
764 ;; Both still seem valid - choose the smallest.
765 #+nil (format t "debug: both still valid ~S ~S ~S ~S~%"
766 lisp-ocfp lisp-ra c-ocfp c-ra)
767 (if (sap< lisp-ocfp c-ocfp)
768 (values lisp-ra lisp-ocfp)
769 (values c-ra c-ocfp)))
770 (lisp-path-fp
771 ;; The lisp convention is looking good.
772 #+nil (format t "*C lisp-ocfp ~S ~S~%" lisp-ocfp lisp-ra)
773 (values lisp-ra lisp-ocfp))
774 (c-path-fp
775 ;; The C convention is looking good.
776 #+nil (format t "*C c-ocfp ~S ~S~%" c-ocfp c-ra)
777 (values c-ra c-ocfp))
779 ;; Neither seems right?
780 #+nil (format t "debug: no valid2 fp found ~S ~S~%"
781 lisp-ocfp c-ocfp)
782 nil))))
783 ((and (sap> lisp-ocfp fp) (cstack-pointer-valid-p lisp-ocfp)
784 (ra-pointer-valid-p lisp-ra))
785 ;; The lisp convention is looking good.
786 #+nil (format t "*C lisp-ocfp ~S ~S~%" lisp-ocfp lisp-ra)
787 (values lisp-ra lisp-ocfp))
788 ((and (sap> c-ocfp fp) (cstack-pointer-valid-p c-ocfp)
789 #!-linux (ra-pointer-valid-p c-ra))
790 ;; The C convention is looking good.
791 #+nil (format t "*C c-ocfp ~S ~S~%" c-ocfp c-ra)
792 (values c-ra c-ocfp))
794 #+nil (format t "debug: no valid fp found ~S ~S~%"
795 lisp-ocfp c-ocfp)
796 nil))))))
798 ) ; #+x86 PROGN
800 ;;; Convert the descriptor into a SAP. The bits all stay the same, we just
801 ;;; change our notion of what we think they are.
802 #!-sb-fluid (declaim (inline descriptor-sap))
803 (defun descriptor-sap (x)
804 (int-sap (get-lisp-obj-address x)))
806 (defun top-frame ()
807 #!+sb-doc
808 "Returns the top frame of the control stack as it was before calling this
809 function."
810 (multiple-value-bind (fp pc) (%caller-frame-and-pc)
811 (possibly-an-interpreted-frame
812 (compute-calling-frame (descriptor-sap fp)
813 #!-gengc pc #!+gengc (descriptor-sap pc)
814 nil)
815 nil)))
817 (defun flush-frames-above (frame)
818 #!+sb-doc
819 "Flush all of the frames above FRAME, and renumber all the frames below
820 FRAME."
821 (setf (frame-up frame) nil)
822 (do ((number 0 (1+ number))
823 (frame frame (frame-%down frame)))
824 ((not (frame-p frame)))
825 (setf (frame-number frame) number)))
827 ;;; We have to access the old-fp and return-pc out of frame and pass them to
828 ;;; COMPUTE-CALLING-FRAME.
829 (defun frame-down (frame)
830 #!+sb-doc
831 "Returns the frame immediately below frame on the stack. When frame is
832 the bottom of the stack, this returns nil."
833 (let ((down (frame-%down frame)))
834 (if (eq down :unparsed)
835 (let* ((real (frame-real-frame frame))
836 (debug-fun (frame-debug-function real)))
837 (setf (frame-%down frame)
838 (etypecase debug-fun
839 (compiled-debug-function
840 (let ((c-d-f (compiled-debug-function-compiler-debug-fun
841 debug-fun)))
842 (possibly-an-interpreted-frame
843 (compute-calling-frame
844 (descriptor-sap
845 (get-context-value
846 real sb!vm::ocfp-save-offset
847 (sb!c::compiled-debug-function-old-fp c-d-f)))
848 #!-gengc
849 (get-context-value
850 real sb!vm::lra-save-offset
851 (sb!c::compiled-debug-function-return-pc c-d-f))
852 #!+gengc
853 (descriptor-sap
854 (get-context-value
855 real sb!vm::ra-save-offset
856 (sb!c::compiled-debug-function-return-pc c-d-f)))
857 frame)
858 frame)))
859 (bogus-debug-function
860 (let ((fp (frame-pointer real)))
861 (when (cstack-pointer-valid-p fp)
862 #!+x86
863 (multiple-value-bind (ra ofp) (x86-call-context fp)
864 (compute-calling-frame ofp ra frame))
865 #!-x86
866 (compute-calling-frame
867 #!-alpha
868 (sap-ref-sap fp (* sb!vm::ocfp-save-offset
869 sb!vm:word-bytes))
870 #!+alpha
871 (int-sap
872 (sap-ref-32 fp (* sb!vm::ocfp-save-offset
873 sb!vm:word-bytes)))
875 #!-gengc
876 (stack-ref fp sb!vm::lra-save-offset)
877 #!+gengc
878 (sap-ref-sap fp (* sb!vm::ra-save-offset
879 sb!vm:word-bytes))
880 frame)))))))
881 down)))
883 ;;; Get the old FP or return PC out of FRAME. STACK-SLOT is the
884 ;;; standard save location offset on the stack. LOC is the saved
885 ;;; SC-OFFSET describing the main location.
886 #!-x86
887 (defun get-context-value (frame stack-slot loc)
888 (declare (type compiled-frame frame) (type unsigned-byte stack-slot)
889 (type sb!c::sc-offset loc))
890 (let ((pointer (frame-pointer frame))
891 (escaped (compiled-frame-escaped frame)))
892 (if escaped
893 (sub-access-debug-var-slot pointer loc escaped)
894 (stack-ref pointer stack-slot))))
895 #!+x86
896 (defun get-context-value (frame stack-slot loc)
897 (declare (type compiled-frame frame) (type unsigned-byte stack-slot)
898 (type sb!c::sc-offset loc))
899 (let ((pointer (frame-pointer frame))
900 (escaped (compiled-frame-escaped frame)))
901 (if escaped
902 (sub-access-debug-var-slot pointer loc escaped)
903 (ecase stack-slot
904 (#.sb!vm::ocfp-save-offset
905 (stack-ref pointer stack-slot))
906 (#.sb!vm::lra-save-offset
907 (sap-ref-sap pointer (- (* (1+ stack-slot) 4))))))))
909 #!-x86
910 (defun (setf get-context-value) (value frame stack-slot loc)
911 (declare (type compiled-frame frame) (type unsigned-byte stack-slot)
912 (type sb!c::sc-offset loc))
913 (let ((pointer (frame-pointer frame))
914 (escaped (compiled-frame-escaped frame)))
915 (if escaped
916 (sub-set-debug-var-slot pointer loc value escaped)
917 (setf (stack-ref pointer stack-slot) value))))
919 #!+x86
920 (defun (setf get-context-value) (value frame stack-slot loc)
921 (declare (type compiled-frame frame) (type unsigned-byte stack-slot)
922 (type sb!c::sc-offset loc))
923 (let ((pointer (frame-pointer frame))
924 (escaped (compiled-frame-escaped frame)))
925 (if escaped
926 (sub-set-debug-var-slot pointer loc value escaped)
927 (ecase stack-slot
928 (#.sb!vm::ocfp-save-offset
929 (setf (stack-ref pointer stack-slot) value))
930 (#.sb!vm::lra-save-offset
931 (setf (sap-ref-sap pointer (- (* (1+ stack-slot) 4))) value))))))
933 (defvar *debugging-interpreter* nil
934 #!+sb-doc
935 "When set, the debugger foregoes making interpreted-frames, so you can
936 debug the functions that manifest the interpreter.")
938 ;;; This takes a newly computed frame, FRAME, and the frame above it
939 ;;; on the stack, UP-FRAME, which is possibly NIL. FRAME is NIL when
940 ;;; we hit the bottom of the control stack. When FRAME represents a
941 ;;; call to SB!EVAL::INTERNAL-APPLY-LOOP, we make an interpreted frame
942 ;;; to replace FRAME. The interpreted frame points to FRAME.
943 (defun possibly-an-interpreted-frame (frame up-frame)
944 (if (or (not frame)
945 (not (eq (debug-function-name (frame-debug-function frame))
946 'sb!eval::internal-apply-loop))
947 *debugging-interpreter*
948 (compiled-frame-escaped frame))
949 frame
950 (flet ((get-var (name location)
951 (let ((vars (sb!di:ambiguous-debug-vars
952 (sb!di:frame-debug-function frame) name)))
953 (when (or (null vars) (> (length vars) 1))
954 (error "zero or more than one ~A variable in ~
955 SB!EVAL::INTERNAL-APPLY-LOOP"
956 (string-downcase name)))
957 (if (eq (debug-var-validity (car vars) location)
958 :valid)
959 (car vars)))))
960 (let* ((code-loc (frame-code-location frame))
961 (ptr-var (get-var "FRAME-PTR" code-loc))
962 (node-var (get-var "NODE" code-loc))
963 (closure-var (get-var "CLOSURE" code-loc)))
964 (if (and ptr-var node-var closure-var)
965 (let* ((node (debug-var-value node-var frame))
966 (d-fun (make-interpreted-debug-function
967 (sb!c::block-home-lambda (sb!c::node-block
968 node)))))
969 (make-interpreted-frame
970 (debug-var-value ptr-var frame)
971 up-frame
972 d-fun
973 (make-interpreted-code-location node d-fun)
974 (frame-number frame)
975 frame
976 (debug-var-value closure-var frame)))
977 frame)))))
979 ;;; This returns a frame for the one existing in time immediately
980 ;;; prior to the frame referenced by current-fp. This is current-fp's
981 ;;; caller or the next frame down the control stack. If there is no
982 ;;; down frame, this returns nil for the bottom of the stack. Up-frame
983 ;;; is the up link for the resulting frame object, and it is nil when
984 ;;; we call this to get the top of the stack.
986 ;;; The current frame contains the pointer to the temporally previous
987 ;;; frame we want, and the current frame contains the pc at which we
988 ;;; will continue executing upon returning to that previous frame.
990 ;;; Note: Sometimes LRA is actually a fixnum. This happens when lisp
991 ;;; calls into C. In this case, the code object is stored on the stack
992 ;;; after the LRA, and the LRA is the word offset.
993 #!-(or gengc x86)
994 (defun compute-calling-frame (caller lra up-frame)
995 (declare (type system-area-pointer caller))
996 (when (cstack-pointer-valid-p caller)
997 (multiple-value-bind (code pc-offset escaped)
998 (if lra
999 (multiple-value-bind (word-offset code)
1000 (if (fixnump lra)
1001 (let ((fp (frame-pointer up-frame)))
1002 (values lra
1003 (stack-ref fp (1+ sb!vm::lra-save-offset))))
1004 (values (get-header-data lra)
1005 (lra-code-header lra)))
1006 (if code
1007 (values code
1008 (* (1+ (- word-offset (get-header-data code)))
1009 sb!vm:word-bytes)
1010 nil)
1011 (values :foreign-function
1013 nil)))
1014 (find-escaped-frame caller))
1015 (if (and (code-component-p code)
1016 (eq (%code-debug-info code) :bogus-lra))
1017 (let ((real-lra (code-header-ref code real-lra-slot)))
1018 (compute-calling-frame caller real-lra up-frame))
1019 (let ((d-fun (case code
1020 (:undefined-function
1021 (make-bogus-debug-function
1022 "undefined function"))
1023 (:foreign-function
1024 (make-bogus-debug-function
1025 "foreign function call land"))
1026 ((nil)
1027 (make-bogus-debug-function
1028 "bogus stack frame"))
1030 (debug-function-from-pc code pc-offset)))))
1031 (make-compiled-frame caller up-frame d-fun
1032 (code-location-from-pc d-fun pc-offset
1033 escaped)
1034 (if up-frame (1+ (frame-number up-frame)) 0)
1035 escaped))))))
1037 #!+x86
1038 (defun compute-calling-frame (caller ra up-frame)
1039 (declare (type system-area-pointer caller ra))
1040 ; (format t "ccf: ~A ~A ~A~%" caller ra up-frame)
1041 (when (cstack-pointer-valid-p caller)
1042 ; (format t "ccf2~%")
1043 ;; First check for an escaped frame.
1044 (multiple-value-bind (code pc-offset escaped) (find-escaped-frame caller)
1045 (cond (code
1046 ;; If it's escaped it may be a function end breakpoint trap.
1047 ; (format t "ccf2: escaped ~S ~S~%" code pc-offset)
1048 (when (and (code-component-p code)
1049 (eq (%code-debug-info code) :bogus-lra))
1050 ;; If :bogus-lra grab the real lra.
1051 (setq pc-offset (code-header-ref
1052 code (1+ real-lra-slot)))
1053 (setq code (code-header-ref code real-lra-slot))
1054 ; (format t "ccf3 :bogus-lra ~S ~S~%" code pc-offset)
1055 (assert code)))
1057 ;; Not escaped
1058 (multiple-value-setq (pc-offset code)
1059 (compute-lra-data-from-pc ra))
1060 ; (format t "ccf4 ~S ~S~%" code pc-offset)
1061 (unless code
1062 (setf code :foreign-function
1063 pc-offset 0
1064 escaped nil))))
1066 (let ((d-fun (case code
1067 (:undefined-function
1068 (make-bogus-debug-function
1069 "undefined function"))
1070 (:foreign-function
1071 (make-bogus-debug-function
1072 "foreign function call land"))
1073 ((nil)
1074 (make-bogus-debug-function
1075 "bogus stack frame"))
1077 (debug-function-from-pc code pc-offset)))))
1078 (make-compiled-frame caller up-frame d-fun
1079 (code-location-from-pc d-fun pc-offset
1080 escaped)
1081 (if up-frame (1+ (frame-number up-frame)) 0)
1082 escaped)))))
1084 #!-(or gengc x86)
1085 ;;; FIXME: The original CMU CL code had support for this case, but it
1086 ;;; must have been fairly stale even in CMU CL, since it had
1087 ;;; references to the MIPS package, and there have been enough
1088 ;;; relevant changes in SBCL (particularly using
1089 ;;; POSIX/SIGACTION0-style signal context instead of BSD-style
1090 ;;; sigcontext) that this code is unmaintainable (since as of
1091 ;;; sbcl-0.6.7, and for the foreseeable future, we can't test it,
1092 ;;; since we only support X86 and its gencgc).
1094 ;;; If we restore this case, the best approach would be to go back to
1095 ;;; the original CMU CL code and start from there.
1096 (eval-when (:compile-toplevel :load-toplevel :execute)
1097 (error "hopelessly stale"))
1098 #!+x86
1099 (defun find-escaped-frame (frame-pointer)
1100 (declare (type system-area-pointer frame-pointer))
1101 (dotimes (index sb!impl::*free-interrupt-context-index* (values nil 0 nil))
1102 (sb!alien:with-alien
1103 ((lisp-interrupt-contexts (array (* os-context-t) nil)
1104 :extern))
1105 (let ((context (sb!alien:deref lisp-interrupt-contexts index)))
1106 (when (= (sap-int frame-pointer)
1107 (sb!vm:context-register context sb!vm::cfp-offset))
1108 (without-gcing
1109 (let* ((component-ptr (component-ptr-from-pc
1110 (sb!vm:context-pc context)))
1111 (code (if (sap= component-ptr (int-sap #x0))
1112 nil ; FIXME: UNLESS might be clearer than IF.
1113 (component-from-component-ptr component-ptr))))
1114 (when (null code)
1115 (return (values code 0 context)))
1116 (let* ((code-header-len (* (get-header-data code)
1117 sb!vm:word-bytes))
1118 (pc-offset
1119 (- (sap-int (sb!vm:context-pc context))
1120 (- (get-lisp-obj-address code)
1121 sb!vm:other-pointer-type)
1122 code-header-len)))
1123 (unless (<= 0 pc-offset
1124 (* (code-header-ref code sb!vm:code-code-size-slot)
1125 sb!vm:word-bytes))
1126 ;; We were in an assembly routine. Therefore, use the LRA as
1127 ;; the pc.
1128 (format t "** pc-offset ~S not in code obj ~S?~%"
1129 pc-offset code))
1130 (return
1131 (values code pc-offset context))))))))))
1133 ;;; Find the code object corresponding to the object represented by
1134 ;;; bits and return it. We assume bogus functions correspond to the
1135 ;;; undefined-function.
1136 #!-gengc
1137 (defun code-object-from-bits (bits)
1138 (declare (type (unsigned-byte 32) bits))
1139 (let ((object (make-lisp-obj bits)))
1140 (if (functionp object)
1141 (or (function-code-header object)
1142 :undefined-function)
1143 (let ((lowtag (get-lowtag object)))
1144 (if (= lowtag sb!vm:other-pointer-type)
1145 (let ((type (get-type object)))
1146 (cond ((= type sb!vm:code-header-type)
1147 object)
1148 ((= type sb!vm:return-pc-header-type)
1149 (lra-code-header object))
1151 nil))))))))
1153 ;;; SB!KERNEL:*SAVED-STATE-CHAIN* -- maintained by the C code as a
1154 ;;; list of SAPs, each SAP pointing to a saved exception state.
1155 #!+gengc
1156 (declaim (special *saved-state-chain*))
1158 ;;; CMU CL had
1159 ;;; (DEFUN LOOKUP-TRACE-TABLE-ENTRY (COMPONENT PC) ..)
1160 ;;; for this case, but it hasn't been maintained in SBCL.
1161 #!+gengc
1162 (eval-when (:compile-toplevel :load-toplevel :execute)
1163 (error "hopelessly stale"))
1165 ;;; CMU CL had
1166 ;;; (DEFUN EXTRACT-INFO-FROM-STATE (STATE) ..)
1167 ;;; for this case, but it hasn't been maintained in SBCL.
1168 #!+gengc
1169 (eval-when (:compile-toplevel :load-toplevel :execute)
1170 (error "hopelessly stale"))
1172 ;;; CMU CL had
1173 ;;; (DEFUN COMPUTE-CALLING-FRAME (OCFP RA UP-FRAME) ..)
1174 ;;; for this case, but it hasn't been maintained in SBCL.
1175 #!+gengc
1176 (eval-when (:compile-toplevel :load-toplevel :execute)
1177 (error "hopelessly stale"))
1179 ;;;; frame utilities
1181 ;;; This returns a COMPILED-DEBUG-FUNCTION for code and pc. We fetch
1182 ;;; the SB!C::DEBUG-INFO and run down its function-map to get a
1183 ;;; SB!C::COMPILED-DEBUG-FUNCTION from the pc. The result only needs
1184 ;;; to reference the component, for function constants, and the
1185 ;;; SB!C::COMPILED-DEBUG-FUNCTION.
1186 (defun debug-function-from-pc (component pc)
1187 (let ((info (%code-debug-info component)))
1188 (cond
1189 ((not info)
1190 (debug-signal 'no-debug-info))
1191 ((eq info :bogus-lra)
1192 (make-bogus-debug-function "function end breakpoint"))
1194 (let* ((function-map (get-debug-info-function-map info))
1195 (len (length function-map)))
1196 (declare (simple-vector function-map))
1197 (if (= len 1)
1198 (make-compiled-debug-function (svref function-map 0) component)
1199 (let ((i 1)
1200 (elsewhere-p
1201 (>= pc (sb!c::compiled-debug-function-elsewhere-pc
1202 (svref function-map 0)))))
1203 ;; FIXME: I don't think SB!C is the home package of INDEX.
1204 (declare (type sb!c::index i))
1205 (loop
1206 (when (or (= i len)
1207 (< pc (if elsewhere-p
1208 (sb!c::compiled-debug-function-elsewhere-pc
1209 (svref function-map (1+ i)))
1210 (svref function-map i))))
1211 (return (make-compiled-debug-function
1212 (svref function-map (1- i))
1213 component)))
1214 (incf i 2)))))))))
1216 ;;; This returns a code-location for the COMPILED-DEBUG-FUNCTION,
1217 ;;; DEBUG-FUN, and the pc into its code vector. If we stopped at a
1218 ;;; breakpoint, find the CODE-LOCATION for that breakpoint. Otherwise,
1219 ;;; make an :UNSURE code location, so it can be filled in when we
1220 ;;; figure out what is going on.
1221 (defun code-location-from-pc (debug-fun pc escaped)
1222 (or (and (compiled-debug-function-p debug-fun)
1223 escaped
1224 (let ((data (breakpoint-data
1225 (compiled-debug-function-component debug-fun)
1226 pc nil)))
1227 (when (and data (breakpoint-data-breakpoints data))
1228 (let ((what (breakpoint-what
1229 (first (breakpoint-data-breakpoints data)))))
1230 (when (compiled-code-location-p what)
1231 what)))))
1232 (make-compiled-code-location pc debug-fun)))
1234 (defun frame-catches (frame)
1235 #!+sb-doc
1236 "Returns an a-list mapping catch tags to code-locations. These are
1237 code-locations at which execution would continue with frame as the top
1238 frame if someone threw to the corresponding tag."
1239 (let ((catch
1240 #!-gengc (descriptor-sap sb!impl::*current-catch-block*)
1241 #!+gengc (mutator-current-catch-block))
1242 (res nil)
1243 (fp (frame-pointer (frame-real-frame frame))))
1244 (loop
1245 (when (zerop (sap-int catch)) (return (nreverse res)))
1246 (when (sap= fp
1247 #!-alpha
1248 (sap-ref-sap catch
1249 (* sb!vm:catch-block-current-cont-slot
1250 sb!vm:word-bytes))
1251 #!+alpha
1252 (:int-sap
1253 (sap-ref-32 catch
1254 (* sb!vm:catch-block-current-cont-slot
1255 sb!vm:word-bytes))))
1256 (let* (#!-(or gengc x86)
1257 (lra (stack-ref catch sb!vm:catch-block-entry-pc-slot))
1258 #!+(or gengc x86)
1259 (ra (sap-ref-sap
1260 catch (* sb!vm:catch-block-entry-pc-slot
1261 sb!vm:word-bytes)))
1262 #!-x86
1263 (component
1264 (stack-ref catch sb!vm:catch-block-current-code-slot))
1265 #!+x86
1266 (component (component-from-component-ptr
1267 (component-ptr-from-pc ra)))
1268 (offset
1269 #!-(or gengc x86)
1270 (* (- (1+ (get-header-data lra))
1271 (get-header-data component))
1272 sb!vm:word-bytes)
1273 #!+gengc
1274 (+ (- (sap-int ra)
1275 (get-lisp-obj-address component)
1276 (get-header-data component))
1277 sb!vm:other-pointer-type)
1278 #!+x86
1279 (- (sap-int ra)
1280 (- (get-lisp-obj-address component)
1281 sb!vm:other-pointer-type)
1282 (* (get-header-data component) sb!vm:word-bytes))))
1283 (push (cons #!-x86
1284 (stack-ref catch sb!vm:catch-block-tag-slot)
1285 #!+x86
1286 (make-lisp-obj
1287 (sap-ref-32 catch (* sb!vm:catch-block-tag-slot
1288 sb!vm:word-bytes)))
1289 (make-compiled-code-location
1290 offset (frame-debug-function frame)))
1291 res)))
1292 (setf catch
1293 #!-alpha
1294 (sap-ref-sap catch
1295 (* sb!vm:catch-block-previous-catch-slot
1296 sb!vm:word-bytes))
1297 #!+alpha
1298 (:int-sap
1299 (sap-ref-32 catch
1300 (* sb!vm:catch-block-previous-catch-slot
1301 sb!vm:word-bytes)))))))
1303 ;;; If an interpreted frame, return the real frame, otherwise frame.
1304 (defun frame-real-frame (frame)
1305 (etypecase frame
1306 (compiled-frame frame)
1307 (interpreted-frame (interpreted-frame-real-frame frame))))
1309 ;;;; operations on DEBUG-FUNCTIONs
1311 (defmacro do-debug-function-blocks ((block-var debug-function &optional result)
1312 &body body)
1313 #!+sb-doc
1314 "Executes the forms in a context with block-var bound to each debug-block in
1315 debug-function successively. Result is an optional form to execute for
1316 return values, and DO-DEBUG-FUNCTION-BLOCKS returns nil if there is no
1317 result form. This signals a no-debug-blocks condition when the
1318 debug-function lacks debug-block information."
1319 (let ((blocks (gensym))
1320 (i (gensym)))
1321 `(let ((,blocks (debug-function-debug-blocks ,debug-function)))
1322 (declare (simple-vector ,blocks))
1323 (dotimes (,i (length ,blocks) ,result)
1324 (let ((,block-var (svref ,blocks ,i)))
1325 ,@body)))))
1327 (defmacro do-debug-function-variables ((var debug-function &optional result)
1328 &body body)
1329 #!+sb-doc
1330 "Executes body in a context with var bound to each debug-var in
1331 debug-function. This returns the value of executing result (defaults to
1332 nil). This may iterate over only some of debug-function's variables or none
1333 depending on debug policy; for example, possibly the compilation only
1334 preserved argument information."
1335 (let ((vars (gensym))
1336 (i (gensym)))
1337 `(let ((,vars (debug-function-debug-vars ,debug-function)))
1338 (declare (type (or null simple-vector) ,vars))
1339 (if ,vars
1340 (dotimes (,i (length ,vars) ,result)
1341 (let ((,var (svref ,vars ,i)))
1342 ,@body))
1343 ,result))))
1345 (defun debug-function-function (debug-function)
1346 #!+sb-doc
1347 "Returns the Common Lisp function associated with the debug-function. This
1348 returns nil if the function is unavailable or is non-existent as a user
1349 callable function object."
1350 (let ((cached-value (debug-function-%function debug-function)))
1351 (if (eq cached-value :unparsed)
1352 (setf (debug-function-%function debug-function)
1353 (etypecase debug-function
1354 (compiled-debug-function
1355 (let ((component
1356 (compiled-debug-function-component debug-function))
1357 (start-pc
1358 (sb!c::compiled-debug-function-start-pc
1359 (compiled-debug-function-compiler-debug-fun
1360 debug-function))))
1361 (do ((entry (%code-entry-points component)
1362 (%function-next entry)))
1363 ((null entry) nil)
1364 (when (= start-pc
1365 (sb!c::compiled-debug-function-start-pc
1366 (compiled-debug-function-compiler-debug-fun
1367 (function-debug-function entry))))
1368 (return entry)))))
1369 (interpreted-debug-function
1370 (sb!c::lambda-eval-info-function
1371 (sb!c::leaf-info
1372 (interpreted-debug-function-ir1-lambda debug-function))))
1373 (bogus-debug-function nil)))
1374 cached-value)))
1376 (defun debug-function-name (debug-function)
1377 #!+sb-doc
1378 "Returns the name of the function represented by debug-function. This may
1379 be a string or a cons; do not assume it is a symbol."
1380 (etypecase debug-function
1381 (compiled-debug-function
1382 (sb!c::compiled-debug-function-name
1383 (compiled-debug-function-compiler-debug-fun debug-function)))
1384 (interpreted-debug-function
1385 (sb!c::lambda-name (interpreted-debug-function-ir1-lambda
1386 debug-function)))
1387 (bogus-debug-function
1388 (bogus-debug-function-%name debug-function))))
1390 (defun function-debug-function (fun)
1391 #!+sb-doc
1392 "Returns a debug-function that represents debug information for function."
1393 (case (get-type fun)
1394 (#.sb!vm:closure-header-type
1395 (function-debug-function (%closure-function fun)))
1396 (#.sb!vm:funcallable-instance-header-type
1397 (cond ((sb!eval:interpreted-function-p fun)
1398 (make-interpreted-debug-function
1399 (or (sb!eval::interpreted-function-definition fun)
1400 (sb!eval::convert-interpreted-fun fun))))
1402 (function-debug-function (funcallable-instance-function fun)))))
1403 ((#.sb!vm:function-header-type #.sb!vm:closure-function-header-type)
1404 (let* ((name (%function-name fun))
1405 (component (function-code-header fun))
1406 (res (find-if
1407 #'(lambda (x)
1408 (and (sb!c::compiled-debug-function-p x)
1409 (eq (sb!c::compiled-debug-function-name x) name)
1410 (eq (sb!c::compiled-debug-function-kind x) nil)))
1411 (get-debug-info-function-map
1412 (%code-debug-info component)))))
1413 (if res
1414 (make-compiled-debug-function res component)
1415 ;; KLUDGE: comment from CMU CL:
1416 ;; This used to be the non-interpreted branch, but
1417 ;; William wrote it to return the debug-fun of fun's XEP
1418 ;; instead of fun's debug-fun. The above code does this
1419 ;; more correctly, but it doesn't get or eliminate all
1420 ;; appropriate cases. It mostly works, and probably
1421 ;; works for all named functions anyway.
1422 ;; -- WHN 20000120
1423 (debug-function-from-pc component
1424 (* (- (function-word-offset fun)
1425 (get-header-data component))
1426 sb!vm:word-bytes)))))))
1428 (defun debug-function-kind (debug-function)
1429 #!+sb-doc
1430 "Returns the kind of the function which is one of :OPTIONAL, :EXTERNAL,
1431 :TOP-level, :CLEANUP, or NIL."
1432 ;; FIXME: This "is one of" information should become part of the function
1433 ;; declamation, not just a doc string
1434 (etypecase debug-function
1435 (compiled-debug-function
1436 (sb!c::compiled-debug-function-kind
1437 (compiled-debug-function-compiler-debug-fun debug-function)))
1438 (interpreted-debug-function
1439 (sb!c::lambda-kind (interpreted-debug-function-ir1-lambda
1440 debug-function)))
1441 (bogus-debug-function
1442 nil)))
1444 (defun debug-var-info-available (debug-function)
1445 #!+sb-doc
1446 "Is there any variable information for DEBUG-FUNCTION?"
1447 (not (not (debug-function-debug-vars debug-function))))
1449 (defun debug-function-symbol-variables (debug-function symbol)
1450 #!+sb-doc
1451 "Returns a list of debug-vars in debug-function having the same name
1452 and package as symbol. If symbol is uninterned, then this returns a list of
1453 debug-vars without package names and with the same name as symbol. The
1454 result of this function is limited to the availability of variable
1455 information in debug-function; for example, possibly debug-function only
1456 knows about its arguments."
1457 (let ((vars (ambiguous-debug-vars debug-function (symbol-name symbol)))
1458 (package (and (symbol-package symbol)
1459 (package-name (symbol-package symbol)))))
1460 (delete-if (if (stringp package)
1461 (lambda (var)
1462 (let ((p (debug-var-package-name var)))
1463 (or (not (stringp p))
1464 (string/= p package))))
1465 (lambda (var)
1466 (stringp (debug-var-package-name var))))
1467 vars)))
1469 (defun ambiguous-debug-vars (debug-function name-prefix-string)
1470 "Returns a list of debug-vars in debug-function whose names contain
1471 name-prefix-string as an intial substring. The result of this function is
1472 limited to the availability of variable information in debug-function; for
1473 example, possibly debug-function only knows about its arguments."
1474 (declare (simple-string name-prefix-string))
1475 (let ((variables (debug-function-debug-vars debug-function)))
1476 (declare (type (or null simple-vector) variables))
1477 (if variables
1478 (let* ((len (length variables))
1479 (prefix-len (length name-prefix-string))
1480 (pos (find-variable name-prefix-string variables len))
1481 (res nil))
1482 (when pos
1483 ;; Find names from pos to variable's len that contain prefix.
1484 (do ((i pos (1+ i)))
1485 ((= i len))
1486 (let* ((var (svref variables i))
1487 (name (debug-var-symbol-name var))
1488 (name-len (length name)))
1489 (declare (simple-string name))
1490 (when (/= (or (string/= name-prefix-string name
1491 :end1 prefix-len :end2 name-len)
1492 prefix-len)
1493 prefix-len)
1494 (return))
1495 (push var res)))
1496 (setq res (nreverse res)))
1497 res))))
1499 ;;; This returns a position in variables for one containing name as an
1500 ;;; initial substring. End is the length of variables if supplied.
1501 (defun find-variable (name variables &optional end)
1502 (declare (simple-vector variables)
1503 (simple-string name))
1504 (let ((name-len (length name)))
1505 (position name variables
1506 :test #'(lambda (x y)
1507 (let* ((y (debug-var-symbol-name y))
1508 (y-len (length y)))
1509 (declare (simple-string y))
1510 (and (>= y-len name-len)
1511 (string= x y :end1 name-len :end2 name-len))))
1512 :end (or end (length variables)))))
1514 (defun debug-function-lambda-list (debug-function)
1515 #!+sb-doc
1516 "Returns a list representing the lambda-list for debug-function. The list
1517 has the following structure:
1518 (required-var1 required-var2
1520 (:optional var3 suppliedp-var4)
1521 (:optional var5)
1523 (:rest var6) (:rest var7)
1525 (:keyword keyword-symbol var8 suppliedp-var9)
1526 (:keyword keyword-symbol var10)
1529 Each VARi is a DEBUG-VAR; however it may be the symbol :deleted it
1530 is unreferenced in debug-function. This signals a lambda-list-unavailable
1531 condition when there is no argument list information."
1532 (etypecase debug-function
1533 (compiled-debug-function
1534 (compiled-debug-function-lambda-list debug-function))
1535 (interpreted-debug-function
1536 (interpreted-debug-function-lambda-list debug-function))
1537 (bogus-debug-function
1538 nil)))
1540 ;;; The hard part is when the lambda-list is unparsed. If it is
1541 ;;; unparsed, and all the arguments are required, this is still pretty
1542 ;;; easy; just whip the appropriate DEBUG-VARs into a list. Otherwise,
1543 ;;; we have to pick out the funny arguments including any suppliedp
1544 ;;; variables. In this situation, the ir1-lambda is an external entry
1545 ;;; point that takes arguments users really pass in. It looks at those
1546 ;;; and computes defaults and suppliedp variables, ultimately passing
1547 ;;; everything defined as a a parameter to the real function as final
1548 ;;; arguments. If this has to compute the lambda list, it caches it in
1549 ;;; debug-function.
1550 (defun interpreted-debug-function-lambda-list (debug-function)
1551 (let ((lambda-list (debug-function-%lambda-list debug-function))
1552 (debug-vars (debug-function-debug-vars debug-function))
1553 (ir1-lambda (interpreted-debug-function-ir1-lambda debug-function))
1554 (res nil))
1555 (if (eq lambda-list :unparsed)
1556 (flet ((frob (v debug-vars)
1557 (if (sb!c::lambda-var-refs v)
1558 (find v debug-vars
1559 :key #'interpreted-debug-var-ir1-var)
1560 :deleted)))
1561 (let ((xep-args (sb!c::lambda-optional-dispatch ir1-lambda)))
1562 (if (and xep-args
1563 (eq (sb!c::optional-dispatch-main-entry xep-args)
1564 ir1-lambda))
1565 ;; There are rest, optional, keyword, and suppliedp vars.
1566 (let ((final-args (sb!c::lambda-vars ir1-lambda)))
1567 (dolist (xep-arg (sb!c::optional-dispatch-arglist xep-args))
1568 (let ((info (sb!c::lambda-var-arg-info xep-arg))
1569 (final-arg (pop final-args)))
1570 (cond (info
1571 (case (sb!c::arg-info-kind info)
1572 (:required
1573 (push (frob final-arg debug-vars) res))
1574 (:keyword
1575 (push (list :keyword
1576 (sb!c::arg-info-keyword info)
1577 (frob final-arg debug-vars))
1578 res))
1579 (:rest
1580 (push (list :rest (frob final-arg debug-vars))
1581 res))
1582 (:optional
1583 (push (list :optional
1584 (frob final-arg debug-vars))
1585 res)))
1586 (when (sb!c::arg-info-supplied-p info)
1587 (nconc
1588 (car res)
1589 (list (frob (pop final-args) debug-vars)))))
1591 (push (frob final-arg debug-vars) res)))))
1592 (setf (debug-function-%lambda-list debug-function)
1593 (nreverse res)))
1594 ;; All required args, so return them in a list.
1595 (dolist (v (sb!c::lambda-vars ir1-lambda)
1596 (setf (debug-function-%lambda-list debug-function)
1597 (nreverse res)))
1598 (push (frob v debug-vars) res)))))
1599 ;; Everything's unparsed and cached, so return it.
1600 lambda-list)))
1602 ;;; If this has to compute the lambda list, it caches it in debug-function.
1603 (defun compiled-debug-function-lambda-list (debug-function)
1604 (let ((lambda-list (debug-function-%lambda-list debug-function)))
1605 (cond ((eq lambda-list :unparsed)
1606 (multiple-value-bind (args argsp)
1607 (parse-compiled-debug-function-lambda-list debug-function)
1608 (setf (debug-function-%lambda-list debug-function) args)
1609 (if argsp
1610 args
1611 (debug-signal 'lambda-list-unavailable
1612 :debug-function debug-function))))
1613 (lambda-list)
1614 ((bogus-debug-function-p debug-function)
1615 nil)
1616 ((sb!c::compiled-debug-function-arguments
1617 (compiled-debug-function-compiler-debug-fun
1618 debug-function))
1619 ;; If the packed information is there (whether empty or not) as
1620 ;; opposed to being nil, then returned our cached value (nil).
1621 nil)
1623 ;; Our cached value is nil, and the packed lambda-list information
1624 ;; is nil, so we don't have anything available.
1625 (debug-signal 'lambda-list-unavailable
1626 :debug-function debug-function)))))
1628 ;;; COMPILED-DEBUG-FUNCTION-LAMBDA-LIST calls this when a
1629 ;;; compiled-debug-function has no lambda-list information cached. It
1630 ;;; returns the lambda-list as the first value and whether there was
1631 ;;; any argument information as the second value. Therefore, nil and t
1632 ;;; means there were no arguments, but nil and nil means there was no
1633 ;;; argument information.
1634 (defun parse-compiled-debug-function-lambda-list (debug-function)
1635 (let ((args (sb!c::compiled-debug-function-arguments
1636 (compiled-debug-function-compiler-debug-fun
1637 debug-function))))
1638 (cond
1639 ((not args)
1640 (values nil nil))
1641 ((eq args :minimal)
1642 (values (coerce (debug-function-debug-vars debug-function) 'list)
1645 (let ((vars (debug-function-debug-vars debug-function))
1646 (i 0)
1647 (len (length args))
1648 (res nil)
1649 (optionalp nil))
1650 (declare (type (or null simple-vector) vars))
1651 (loop
1652 (when (>= i len) (return))
1653 (let ((ele (aref args i)))
1654 (cond
1655 ((symbolp ele)
1656 (case ele
1657 (sb!c::deleted
1658 ;; Deleted required arg at beginning of args array.
1659 (push :deleted res))
1660 (sb!c::optional-args
1661 (setf optionalp t))
1662 (sb!c::supplied-p
1663 ;; SUPPLIED-P var immediately following keyword or
1664 ;; optional. Stick the extra var in the result
1665 ;; element representing the keyword or optional,
1666 ;; which is the previous one.
1667 (nconc (car res)
1668 (list (compiled-debug-function-lambda-list-var
1669 args (incf i) vars))))
1670 (sb!c::rest-arg
1671 (push (list :rest
1672 (compiled-debug-function-lambda-list-var
1673 args (incf i) vars))
1674 res))
1675 (sb!c::more-arg
1676 ;; Just ignore the fact that the next two args are
1677 ;; the more arg context and count, and act like they
1678 ;; are regular arguments.
1679 nil)
1681 ;; keyword arg
1682 (push (list :keyword
1684 (compiled-debug-function-lambda-list-var
1685 args (incf i) vars))
1686 res))))
1687 (optionalp
1688 ;; We saw an optional marker, so the following
1689 ;; non-symbols are indexes indicating optional
1690 ;; variables.
1691 (push (list :optional (svref vars ele)) res))
1693 ;; Required arg at beginning of args array.
1694 (push (svref vars ele) res))))
1695 (incf i))
1696 (values (nreverse res) t))))))
1698 ;;; This is used in COMPILED-DEBUG-FUNCTION-LAMBDA-LIST.
1699 (defun compiled-debug-function-lambda-list-var (args i vars)
1700 (declare (type (simple-array * (*)) args)
1701 (simple-vector vars))
1702 (let ((ele (aref args i)))
1703 (cond ((not (symbolp ele)) (svref vars ele))
1704 ((eq ele 'sb!c::deleted) :deleted)
1705 (t (error "malformed arguments description")))))
1707 (defun compiled-debug-function-debug-info (debug-fun)
1708 (%code-debug-info (compiled-debug-function-component debug-fun)))
1710 ;;;; unpacking variable and basic block data
1712 (defvar *parsing-buffer*
1713 (make-array 20 :adjustable t :fill-pointer t))
1714 (defvar *other-parsing-buffer*
1715 (make-array 20 :adjustable t :fill-pointer t))
1716 ;;; PARSE-DEBUG-BLOCKS, PARSE-DEBUG-VARS and UNCOMPACT-FUNCTION-MAP
1717 ;;; use this to unpack binary encoded information. It returns the
1718 ;;; values returned by the last form in body.
1720 ;;; This binds buffer-var to *parsing-buffer*, makes sure it starts at
1721 ;;; element zero, and makes sure if we unwind, we nil out any set
1722 ;;; elements for GC purposes.
1724 ;;; This also binds other-var to *other-parsing-buffer* when it is
1725 ;;; supplied, making sure it starts at element zero and that we nil
1726 ;;; out any elements if we unwind.
1728 ;;; This defines the local macro RESULT that takes a buffer, copies
1729 ;;; its elements to a resulting simple-vector, nil's out elements, and
1730 ;;; restarts the buffer at element zero. RESULT returns the
1731 ;;; simple-vector.
1732 (eval-when (:compile-toplevel :execute)
1733 (sb!xc:defmacro with-parsing-buffer ((buffer-var &optional other-var)
1734 &body body)
1735 (let ((len (gensym))
1736 (res (gensym)))
1737 `(unwind-protect
1738 (let ((,buffer-var *parsing-buffer*)
1739 ,@(if other-var `((,other-var *other-parsing-buffer*))))
1740 (setf (fill-pointer ,buffer-var) 0)
1741 ,@(if other-var `((setf (fill-pointer ,other-var) 0)))
1742 (macrolet ((result (buf)
1743 `(let* ((,',len (length ,buf))
1744 (,',res (make-array ,',len)))
1745 (replace ,',res ,buf :end1 ,',len :end2 ,',len)
1746 (fill ,buf nil :end ,',len)
1747 (setf (fill-pointer ,buf) 0)
1748 ,',res)))
1749 ,@body))
1750 (fill *parsing-buffer* nil)
1751 ,@(if other-var `((fill *other-parsing-buffer* nil))))))
1752 ) ; EVAL-WHEN
1754 ;;; The argument is a debug internals structure. This returns the
1755 ;;; debug-blocks for debug-function, regardless of whether we have
1756 ;;; unpacked them yet. It signals a no-debug-blocks condition if it
1757 ;;; can't return the blocks.
1758 (defun debug-function-debug-blocks (debug-function)
1759 (let ((blocks (debug-function-blocks debug-function)))
1760 (cond ((eq blocks :unparsed)
1761 (setf (debug-function-blocks debug-function)
1762 (parse-debug-blocks debug-function))
1763 (unless (debug-function-blocks debug-function)
1764 (debug-signal 'no-debug-blocks
1765 :debug-function debug-function))
1766 (debug-function-blocks debug-function))
1767 (blocks)
1769 (debug-signal 'no-debug-blocks
1770 :debug-function debug-function)))))
1772 ;;; This returns a simple-vector of debug-blocks or nil. NIL indicates
1773 ;;; there was no basic block information.
1774 (defun parse-debug-blocks (debug-function)
1775 (etypecase debug-function
1776 (compiled-debug-function
1777 (parse-compiled-debug-blocks debug-function))
1778 (bogus-debug-function
1779 (debug-signal 'no-debug-blocks :debug-function debug-function))
1780 (interpreted-debug-function
1781 (parse-interpreted-debug-blocks debug-function))))
1783 ;;; This does some of the work of PARSE-DEBUG-BLOCKS.
1784 (defun parse-compiled-debug-blocks (debug-function)
1785 (let* ((debug-fun (compiled-debug-function-compiler-debug-fun
1786 debug-function))
1787 (var-count (length (debug-function-debug-vars debug-function)))
1788 (blocks (sb!c::compiled-debug-function-blocks debug-fun))
1789 ;; KLUDGE: 8 is a hard-wired constant in the compiler for the
1790 ;; element size of the packed binary representation of the
1791 ;; blocks data.
1792 (live-set-len (ceiling var-count 8))
1793 (tlf-number (sb!c::compiled-debug-function-tlf-number debug-fun)))
1794 (unless blocks (return-from parse-compiled-debug-blocks nil))
1795 (macrolet ((aref+ (a i) `(prog1 (aref ,a ,i) (incf ,i))))
1796 (with-parsing-buffer (blocks-buffer locations-buffer)
1797 (let ((i 0)
1798 (len (length blocks))
1799 (last-pc 0))
1800 (loop
1801 (when (>= i len) (return))
1802 (let ((succ-and-flags (aref+ blocks i))
1803 (successors nil))
1804 (declare (type (unsigned-byte 8) succ-and-flags)
1805 (list successors))
1806 (dotimes (k (ldb sb!c::compiled-debug-block-nsucc-byte
1807 succ-and-flags))
1808 (push (sb!c::read-var-integer blocks i) successors))
1809 (let* ((locations
1810 (dotimes (k (sb!c::read-var-integer blocks i)
1811 (result locations-buffer))
1812 (let ((kind (svref sb!c::compiled-code-location-kinds
1813 (aref+ blocks i)))
1814 (pc (+ last-pc
1815 (sb!c::read-var-integer blocks i)))
1816 (tlf-offset (or tlf-number
1817 (sb!c::read-var-integer blocks
1818 i)))
1819 (form-number (sb!c::read-var-integer blocks i))
1820 (live-set (sb!c::read-packed-bit-vector
1821 live-set-len blocks i)))
1822 (vector-push-extend (make-known-code-location
1823 pc debug-function tlf-offset
1824 form-number live-set kind)
1825 locations-buffer)
1826 (setf last-pc pc))))
1827 (block (make-compiled-debug-block
1828 locations successors
1829 (not (zerop (logand
1830 sb!c::compiled-debug-block-elsewhere-p
1831 succ-and-flags))))))
1832 (vector-push-extend block blocks-buffer)
1833 (dotimes (k (length locations))
1834 (setf (code-location-%debug-block (svref locations k))
1835 block))))))
1836 (let ((res (result blocks-buffer)))
1837 (declare (simple-vector res))
1838 (dotimes (i (length res))
1839 (let* ((block (svref res i))
1840 (succs nil))
1841 (dolist (ele (debug-block-successors block))
1842 (push (svref res ele) succs))
1843 (setf (debug-block-successors block) succs)))
1844 res)))))
1846 ;;; This does some of the work of PARSE-DEBUG-BLOCKS.
1847 (defun parse-interpreted-debug-blocks (debug-function)
1848 (let ((ir1-lambda (interpreted-debug-function-ir1-lambda debug-function)))
1849 (with-parsing-buffer (buffer)
1850 (sb!c::do-blocks (block (sb!c::block-component
1851 (sb!c::node-block (sb!c::lambda-bind
1852 ir1-lambda))))
1853 (when (eq ir1-lambda (sb!c::block-home-lambda block))
1854 (vector-push-extend (make-interpreted-debug-block block) buffer)))
1855 (result buffer))))
1857 ;;; The argument is a debug internals structure. This returns nil if
1858 ;;; there is no variable information. It returns an empty
1859 ;;; simple-vector if there were no locals in the function. Otherwise
1860 ;;; it returns a simple-vector of DEBUG-VARs.
1861 (defun debug-function-debug-vars (debug-function)
1862 (let ((vars (debug-function-%debug-vars debug-function)))
1863 (if (eq vars :unparsed)
1864 (setf (debug-function-%debug-vars debug-function)
1865 (etypecase debug-function
1866 (compiled-debug-function
1867 (parse-compiled-debug-vars debug-function))
1868 (bogus-debug-function nil)
1869 (interpreted-debug-function
1870 (parse-interpreted-debug-vars debug-function))))
1871 vars)))
1873 ;;; This grabs all the variables from DEBUG-FUN's ir1-lambda, from the
1874 ;;; IR1 lambda vars, and all of its LET's. Each LET is an IR1 lambda.
1875 ;;; For each variable, we make an INTERPRETED-DEBUG-VAR. We then SORT
1876 ;;; all the variables by name. Then we go through, and for any
1877 ;;; duplicated names we distinguish the INTERPRETED-DEBUG-VARs by
1878 ;;; setting their id slots to a distinct number.
1879 (defun parse-interpreted-debug-vars (debug-fun)
1880 (let* ((ir1-lambda (interpreted-debug-function-ir1-lambda debug-fun))
1881 (vars (flet ((frob (ir1-lambda buf)
1882 (dolist (v (sb!c::lambda-vars ir1-lambda))
1883 (vector-push-extend
1884 (let* ((id (sb!c::leaf-name v)))
1885 (make-interpreted-debug-var id v))
1886 buf))))
1887 (with-parsing-buffer (buf)
1888 (frob ir1-lambda buf)
1889 (dolist (let-lambda (sb!c::lambda-lets ir1-lambda))
1890 (frob let-lambda buf))
1891 (result buf)))))
1892 (declare (simple-vector vars))
1893 (sort vars #'string< :key #'debug-var-symbol-name)
1894 (let ((len (length vars)))
1895 (when (> len 1)
1896 (let ((i 0)
1897 (j 1))
1898 (block PUNT
1899 (loop
1900 (let* ((var-i (svref vars i))
1901 (var-j (svref vars j))
1902 (name (debug-var-symbol-name var-i)))
1903 (when (string= name (debug-var-symbol-name var-j))
1904 (let ((count 1))
1905 (loop
1906 (setf (debug-var-id var-j) count)
1907 (when (= (incf j) len) (return-from PUNT))
1908 (setf var-j (svref vars j))
1909 (when (string/= name (debug-var-symbol-name var-j))
1910 (return))
1911 (incf count))))
1912 (setf i j)
1913 (incf j)
1914 (when (= j len) (return))))))))
1915 vars))
1917 ;;; Vars is the parsed variables for a minimal debug function. We need to
1918 ;;; assign names of the form ARG-NNN. We must pad with leading zeros, since
1919 ;;; the arguments must be in alphabetical order.
1920 (defun assign-minimal-var-names (vars)
1921 (declare (simple-vector vars))
1922 (let* ((len (length vars))
1923 (width (length (format nil "~D" (1- len)))))
1924 (dotimes (i len)
1925 (setf (compiled-debug-var-symbol (svref vars i))
1926 (intern (format nil "ARG-~V,'0D" width i)
1927 ;; KLUDGE: It's somewhat nasty to have a bare
1928 ;; package name string here. It would probably be
1929 ;; better to have #.(FIND-PACKAGE "SB!DEBUG")
1930 ;; instead, since then at least it would transform
1931 ;; correctly under package renaming and stuff.
1932 ;; However, genesis can't handle dumped packages..
1933 ;; -- WHN 20000129
1935 ;; FIXME: Maybe this could be fixed by moving the
1936 ;; whole debug-int.lisp file to warm init? (after
1937 ;; which dumping a #.(FIND-PACKAGE ..) expression
1938 ;; would work fine) If this is possible, it would
1939 ;; probably be a good thing, since minimizing the
1940 ;; amount of stuff in cold init is basically good.
1941 "SB-DEBUG")))))
1943 ;;; Parse the packed representation of DEBUG-VARs from
1944 ;;; DEBUG-FUNCTION's SB!C::COMPILED-DEBUG-FUNCTION, returning a vector
1945 ;;; of DEBUG-VARs, or NIL if there was no information to parse.
1946 (defun parse-compiled-debug-vars (debug-function)
1947 (let* ((cdebug-fun (compiled-debug-function-compiler-debug-fun debug-function))
1948 (packed-vars (sb!c::compiled-debug-function-variables cdebug-fun))
1949 (args-minimal (eq (sb!c::compiled-debug-function-arguments cdebug-fun)
1950 :minimal)))
1951 (when packed-vars
1952 (do ((i 0)
1953 (buffer (make-array 0 :fill-pointer 0 :adjustable t)))
1954 ((>= i (length packed-vars))
1955 (let ((result (coerce buffer 'simple-vector)))
1956 (when args-minimal
1957 (assign-minimal-var-names result))
1958 result))
1959 (flet ((geti () (prog1 (aref packed-vars i) (incf i))))
1960 (let* ((flags (geti))
1961 (minimal (logtest sb!c::compiled-debug-var-minimal-p flags))
1962 (deleted (logtest sb!c::compiled-debug-var-deleted-p flags))
1963 (live (logtest sb!c::compiled-debug-var-environment-live flags))
1964 (save (logtest sb!c::compiled-debug-var-save-loc-p flags))
1965 (symbol (if minimal nil (geti)))
1966 (id (if (logtest sb!c::compiled-debug-var-id-p flags)
1967 (geti)
1969 (sc-offset (if deleted 0 (geti)))
1970 (save-sc-offset (if save (geti) nil)))
1971 (assert (not (and args-minimal (not minimal))))
1972 (vector-push-extend (make-compiled-debug-var symbol
1974 live
1975 sc-offset
1976 save-sc-offset)
1977 buffer)))))))
1979 ;;;; unpacking minimal debug functions
1981 (eval-when (:compile-toplevel :execute)
1983 ;;; sleazoid "macro" to keep our indentation sane in UNCOMPACT-FUNCTION-MAP
1984 (sb!xc:defmacro make-uncompacted-debug-fun ()
1985 '(sb!c::make-compiled-debug-function
1986 :name
1987 (let ((base (ecase (ldb sb!c::minimal-debug-function-name-style-byte
1988 options)
1989 (#.sb!c::minimal-debug-function-name-symbol
1990 (intern (sb!c::read-var-string map i)
1991 (sb!c::compiled-debug-info-package info)))
1992 (#.sb!c::minimal-debug-function-name-packaged
1993 (let ((pkg (sb!c::read-var-string map i)))
1994 (intern (sb!c::read-var-string map i) pkg)))
1995 (#.sb!c::minimal-debug-function-name-uninterned
1996 (make-symbol (sb!c::read-var-string map i)))
1997 (#.sb!c::minimal-debug-function-name-component
1998 (sb!c::compiled-debug-info-name info)))))
1999 (if (logtest flags sb!c::minimal-debug-function-setf-bit)
2000 `(setf ,base)
2001 base))
2002 :kind (svref sb!c::minimal-debug-function-kinds
2003 (ldb sb!c::minimal-debug-function-kind-byte options))
2004 :variables
2005 (when vars-p
2006 (let ((len (sb!c::read-var-integer map i)))
2007 (prog1 (subseq map i (+ i len))
2008 (incf i len))))
2009 :arguments (when vars-p :minimal)
2010 :returns
2011 (ecase (ldb sb!c::minimal-debug-function-returns-byte options)
2012 (#.sb!c::minimal-debug-function-returns-standard
2013 :standard)
2014 (#.sb!c::minimal-debug-function-returns-fixed
2015 :fixed)
2016 (#.sb!c::minimal-debug-function-returns-specified
2017 (with-parsing-buffer (buf)
2018 (dotimes (idx (sb!c::read-var-integer map i))
2019 (vector-push-extend (sb!c::read-var-integer map i) buf))
2020 (result buf))))
2021 :return-pc (sb!c::read-var-integer map i)
2022 :old-fp (sb!c::read-var-integer map i)
2023 :nfp (when (logtest flags sb!c::minimal-debug-function-nfp-bit)
2024 (sb!c::read-var-integer map i))
2025 :start-pc
2026 (progn
2027 (setq code-start-pc (+ code-start-pc (sb!c::read-var-integer map i)))
2028 (+ code-start-pc (sb!c::read-var-integer map i)))
2029 :elsewhere-pc
2030 (setq elsewhere-pc (+ elsewhere-pc (sb!c::read-var-integer map i)))))
2032 ) ; EVAL-WHEN
2034 ;;; Return a normal function map derived from a minimal debug info
2035 ;;; function map. This involves looping parsing
2036 ;;; minimal-debug-functions and then building a vector out of them.
2038 ;;; FIXME: This and its helper macro just above become dead code now
2039 ;;; that we no longer use compacted function maps.
2040 (defun uncompact-function-map (info)
2041 (declare (type sb!c::compiled-debug-info info))
2043 ;; (This is stubified until we solve the problem of representing
2044 ;; debug information in a way which plays nicely with package renaming.)
2045 (error "FIXME: dead code UNCOMPACT-FUNCTION-MAP (was stub)")
2047 (let* ((map (sb!c::compiled-debug-info-function-map info))
2048 (i 0)
2049 (len (length map))
2050 (code-start-pc 0)
2051 (elsewhere-pc 0))
2052 (declare (type (simple-array (unsigned-byte 8) (*)) map))
2053 (sb!int:collect ((res))
2054 (loop
2055 (when (= i len) (return))
2056 (let* ((options (prog1 (aref map i) (incf i)))
2057 (flags (prog1 (aref map i) (incf i)))
2058 (vars-p (logtest flags
2059 sb!c::minimal-debug-function-variables-bit))
2060 (dfun (make-uncompacted-debug-fun)))
2061 (res code-start-pc)
2062 (res dfun)))
2064 (coerce (cdr (res)) 'simple-vector))))
2066 ;;; This variable maps minimal debug-info function maps to an unpacked
2067 ;;; version thereof.
2068 (defvar *uncompacted-function-maps* (make-hash-table :test 'eq))
2070 ;;; Return a function-map for a given compiled-debug-info object. If
2071 ;;; the info is minimal, and has not been parsed, then parse it.
2073 ;;; FIXME: Now that we no longer use the minimal-debug-function
2074 ;;; representation, calls to this function can be replaced by calls to
2075 ;;; the bare COMPILED-DEBUG-INFO-FUNCTION-MAP slot accessor function,
2076 ;;; and this function and everything it calls become dead code which
2077 ;;; can be deleted.
2078 (defun get-debug-info-function-map (info)
2079 (declare (type sb!c::compiled-debug-info info))
2080 (let ((map (sb!c::compiled-debug-info-function-map info)))
2081 (if (simple-vector-p map)
2083 (or (gethash map *uncompacted-function-maps*)
2084 (setf (gethash map *uncompacted-function-maps*)
2085 (uncompact-function-map info))))))
2087 ;;;; CODE-LOCATIONs
2089 ;;; If we're sure of whether code-location is known, return t or nil.
2090 ;;; If we're :unsure, then try to fill in the code-location's slots.
2091 ;;; This determines whether there is any debug-block information, and
2092 ;;; if code-location is known.
2094 ;;; ??? IF this conses closures every time it's called, then break off the
2095 ;;; :unsure part to get the HANDLER-CASE into another function.
2096 (defun code-location-unknown-p (basic-code-location)
2097 #!+sb-doc
2098 "Returns whether basic-code-location is unknown. It returns nil when the
2099 code-location is known."
2100 (ecase (code-location-%unknown-p basic-code-location)
2101 ((t) t)
2102 ((nil) nil)
2103 (:unsure
2104 (setf (code-location-%unknown-p basic-code-location)
2105 (handler-case (not (fill-in-code-location basic-code-location))
2106 (no-debug-blocks () t))))))
2108 (defun code-location-debug-block (basic-code-location)
2109 #!+sb-doc
2110 "Returns the debug-block containing code-location if it is available. Some
2111 debug policies inhibit debug-block information, and if none is available,
2112 then this signals a no-debug-blocks condition."
2113 (let ((block (code-location-%debug-block basic-code-location)))
2114 (if (eq block :unparsed)
2115 (etypecase basic-code-location
2116 (compiled-code-location
2117 (compute-compiled-code-location-debug-block basic-code-location))
2118 (interpreted-code-location
2119 (setf (code-location-%debug-block basic-code-location)
2120 (make-interpreted-debug-block
2121 (sb!c::node-block
2122 (interpreted-code-location-ir1-node basic-code-location))))))
2123 block)))
2125 ;;; This stores and returns BASIC-CODE-LOCATION's debug-block. It
2126 ;;; determines the correct one using the code-location's pc. This uses
2127 ;;; DEBUG-FUNCTION-DEBUG-BLOCKS to return the cached block information
2128 ;;; or signal a 'no-debug-blocks condition. The blocks are sorted by
2129 ;;; their first code-location's pc, in ascending order. Therefore, as
2130 ;;; soon as we find a block that starts with a pc greater than
2131 ;;; basic-code-location's pc, we know the previous block contains the
2132 ;;; pc. If we get to the last block, then the code-location is either
2133 ;;; in the second to last block or the last block, and we have to be
2134 ;;; careful in determining this since the last block could be code at
2135 ;;; the end of the function. We have to check for the last block being
2136 ;;; code first in order to see how to compare the code-location's pc.
2137 (defun compute-compiled-code-location-debug-block (basic-code-location)
2138 (let* ((pc (compiled-code-location-pc basic-code-location))
2139 (debug-function (code-location-debug-function
2140 basic-code-location))
2141 (blocks (debug-function-debug-blocks debug-function))
2142 (len (length blocks)))
2143 (declare (simple-vector blocks))
2144 (setf (code-location-%debug-block basic-code-location)
2145 (if (= len 1)
2146 (svref blocks 0)
2147 (do ((i 1 (1+ i))
2148 (end (1- len)))
2149 ((= i end)
2150 (let ((last (svref blocks end)))
2151 (cond
2152 ((debug-block-elsewhere-p last)
2153 (if (< pc
2154 (sb!c::compiled-debug-function-elsewhere-pc
2155 (compiled-debug-function-compiler-debug-fun
2156 debug-function)))
2157 (svref blocks (1- end))
2158 last))
2159 ((< pc
2160 (compiled-code-location-pc
2161 (svref (compiled-debug-block-code-locations last)
2162 0)))
2163 (svref blocks (1- end)))
2164 (t last))))
2165 (declare (type sb!c::index i end))
2166 (when (< pc
2167 (compiled-code-location-pc
2168 (svref (compiled-debug-block-code-locations
2169 (svref blocks i))
2170 0)))
2171 (return (svref blocks (1- i)))))))))
2173 (defun code-location-debug-source (code-location)
2174 #!+sb-doc
2175 "Returns the code-location's debug-source."
2176 (etypecase code-location
2177 (compiled-code-location
2178 (let* ((info (compiled-debug-function-debug-info
2179 (code-location-debug-function code-location)))
2180 (sources (sb!c::compiled-debug-info-source info))
2181 (len (length sources)))
2182 (declare (list sources))
2183 (when (zerop len)
2184 (debug-signal 'no-debug-blocks :debug-function
2185 (code-location-debug-function code-location)))
2186 (if (= len 1)
2187 (car sources)
2188 (do ((prev sources src)
2189 (src (cdr sources) (cdr src))
2190 (offset (code-location-top-level-form-offset code-location)))
2191 ((null src) (car prev))
2192 (when (< offset (sb!c::debug-source-source-root (car src)))
2193 (return (car prev)))))))
2194 (interpreted-code-location
2195 (first
2196 (let ((sb!c::*lexenv* (make-null-lexenv)))
2197 (sb!c::debug-source-for-info
2198 (sb!c::component-source-info
2199 (sb!c::block-component
2200 (sb!c::node-block
2201 (interpreted-code-location-ir1-node code-location))))))))))
2203 (defun code-location-top-level-form-offset (code-location)
2204 #!+sb-doc
2205 "Returns the number of top-level forms before the one containing
2206 code-location as seen by the compiler in some compilation unit. A
2207 compilation unit is not necessarily a single file, see the section on
2208 debug-sources."
2209 (when (code-location-unknown-p code-location)
2210 (error 'unknown-code-location :code-location code-location))
2211 (let ((tlf-offset (code-location-%tlf-offset code-location)))
2212 (cond ((eq tlf-offset :unparsed)
2213 (etypecase code-location
2214 (compiled-code-location
2215 (unless (fill-in-code-location code-location)
2216 ;; This check should be unnecessary. We're missing
2217 ;; debug info the compiler should have dumped.
2218 (error "internal error: unknown code location"))
2219 (code-location-%tlf-offset code-location))
2220 (interpreted-code-location
2221 (setf (code-location-%tlf-offset code-location)
2222 (sb!c::source-path-tlf-number
2223 (sb!c::node-source-path
2224 (interpreted-code-location-ir1-node code-location)))))))
2225 (t tlf-offset))))
2227 (defun code-location-form-number (code-location)
2228 #!+sb-doc
2229 "Returns the number of the form corresponding to code-location. The form
2230 number is derived by a walking the subforms of a top-level form in
2231 depth-first order."
2232 (when (code-location-unknown-p code-location)
2233 (error 'unknown-code-location :code-location code-location))
2234 (let ((form-num (code-location-%form-number code-location)))
2235 (cond ((eq form-num :unparsed)
2236 (etypecase code-location
2237 (compiled-code-location
2238 (unless (fill-in-code-location code-location)
2239 ;; This check should be unnecessary. We're missing
2240 ;; debug info the compiler should have dumped.
2241 (error "internal error: unknown code location"))
2242 (code-location-%form-number code-location))
2243 (interpreted-code-location
2244 (setf (code-location-%form-number code-location)
2245 (sb!c::source-path-form-number
2246 (sb!c::node-source-path
2247 (interpreted-code-location-ir1-node code-location)))))))
2248 (t form-num))))
2250 (defun code-location-kind (code-location)
2251 #!+sb-doc
2252 "Return the kind of CODE-LOCATION, one of:
2253 :interpreted, :unknown-return, :known-return, :internal-error,
2254 :non-local-exit, :block-start, :call-site, :single-value-return,
2255 :non-local-entry"
2256 (when (code-location-unknown-p code-location)
2257 (error 'unknown-code-location :code-location code-location))
2258 (etypecase code-location
2259 (compiled-code-location
2260 (let ((kind (compiled-code-location-kind code-location)))
2261 (cond ((not (eq kind :unparsed)) kind)
2262 ((not (fill-in-code-location code-location))
2263 ;; This check should be unnecessary. We're missing
2264 ;; debug info the compiler should have dumped.
2265 (error "internal error: unknown code location"))
2267 (compiled-code-location-kind code-location)))))
2268 (interpreted-code-location
2269 :interpreted)))
2271 ;;; This returns CODE-LOCATION's live-set if it is available. If
2272 ;;; there is no debug-block information, this returns NIL.
2273 (defun compiled-code-location-live-set (code-location)
2274 (if (code-location-unknown-p code-location)
2276 (let ((live-set (compiled-code-location-%live-set code-location)))
2277 (cond ((eq live-set :unparsed)
2278 (unless (fill-in-code-location code-location)
2279 ;; This check should be unnecessary. We're missing debug info
2280 ;; the compiler should have dumped.
2282 ;; FIXME: This error and comment happen over and over again.
2283 ;; Make them a shared function.
2284 (error "internal error: unknown code location"))
2285 (compiled-code-location-%live-set code-location))
2286 (t live-set)))))
2288 (defun code-location= (obj1 obj2)
2289 #!+sb-doc
2290 "Returns whether obj1 and obj2 are the same place in the code."
2291 (etypecase obj1
2292 (compiled-code-location
2293 (etypecase obj2
2294 (compiled-code-location
2295 (and (eq (code-location-debug-function obj1)
2296 (code-location-debug-function obj2))
2297 (sub-compiled-code-location= obj1 obj2)))
2298 (interpreted-code-location
2299 nil)))
2300 (interpreted-code-location
2301 (etypecase obj2
2302 (compiled-code-location
2303 nil)
2304 (interpreted-code-location
2305 (eq (interpreted-code-location-ir1-node obj1)
2306 (interpreted-code-location-ir1-node obj2)))))))
2307 (defun sub-compiled-code-location= (obj1 obj2)
2308 (= (compiled-code-location-pc obj1)
2309 (compiled-code-location-pc obj2)))
2311 ;;; This fills in CODE-LOCATION's :unparsed slots. It returns t or nil
2312 ;;; depending on whether the code-location was known in its
2313 ;;; debug-function's debug-block information. This may signal a
2314 ;;; NO-DEBUG-BLOCKS condition due to DEBUG-FUNCTION-DEBUG-BLOCKS, and
2315 ;;; it assumes the %UNKNOWN-P slot is already set or going to be set.
2316 (defun fill-in-code-location (code-location)
2317 (declare (type compiled-code-location code-location))
2318 (let* ((debug-function (code-location-debug-function code-location))
2319 (blocks (debug-function-debug-blocks debug-function)))
2320 (declare (simple-vector blocks))
2321 (dotimes (i (length blocks) nil)
2322 (let* ((block (svref blocks i))
2323 (locations (compiled-debug-block-code-locations block)))
2324 (declare (simple-vector locations))
2325 (dotimes (j (length locations))
2326 (let ((loc (svref locations j)))
2327 (when (sub-compiled-code-location= code-location loc)
2328 (setf (code-location-%debug-block code-location) block)
2329 (setf (code-location-%tlf-offset code-location)
2330 (code-location-%tlf-offset loc))
2331 (setf (code-location-%form-number code-location)
2332 (code-location-%form-number loc))
2333 (setf (compiled-code-location-%live-set code-location)
2334 (compiled-code-location-%live-set loc))
2335 (setf (compiled-code-location-kind code-location)
2336 (compiled-code-location-kind loc))
2337 (return-from fill-in-code-location t))))))))
2339 ;;;; operations on DEBUG-BLOCKs
2341 (defmacro do-debug-block-locations ((code-var debug-block &optional return)
2342 &body body)
2343 #!+sb-doc
2344 "Executes forms in a context with code-var bound to each code-location in
2345 debug-block. This returns the value of executing result (defaults to nil)."
2346 (let ((code-locations (gensym))
2347 (i (gensym)))
2348 `(let ((,code-locations (debug-block-code-locations ,debug-block)))
2349 (declare (simple-vector ,code-locations))
2350 (dotimes (,i (length ,code-locations) ,return)
2351 (let ((,code-var (svref ,code-locations ,i)))
2352 ,@body)))))
2354 (defun debug-block-function-name (debug-block)
2355 #!+sb-doc
2356 "Returns the name of the function represented by debug-function. This may
2357 be a string or a cons; do not assume it is a symbol."
2358 (etypecase debug-block
2359 (compiled-debug-block
2360 (let ((code-locs (compiled-debug-block-code-locations debug-block)))
2361 (declare (simple-vector code-locs))
2362 (if (zerop (length code-locs))
2363 "??? Can't get name of debug-block's function."
2364 (debug-function-name
2365 (code-location-debug-function (svref code-locs 0))))))
2366 (interpreted-debug-block
2367 (sb!c::lambda-name (sb!c::block-home-lambda
2368 (interpreted-debug-block-ir1-block debug-block))))))
2370 (defun debug-block-code-locations (debug-block)
2371 (etypecase debug-block
2372 (compiled-debug-block
2373 (compiled-debug-block-code-locations debug-block))
2374 (interpreted-debug-block
2375 (interpreted-debug-block-code-locations debug-block))))
2377 (defun interpreted-debug-block-code-locations (debug-block)
2378 (let ((code-locs (interpreted-debug-block-locations debug-block)))
2379 (if (eq code-locs :unparsed)
2380 (with-parsing-buffer (buf)
2381 (sb!c::do-nodes (node cont (interpreted-debug-block-ir1-block
2382 debug-block))
2383 (vector-push-extend (make-interpreted-code-location
2384 node
2385 (make-interpreted-debug-function
2386 (sb!c::block-home-lambda (sb!c::node-block
2387 node))))
2388 buf))
2389 (setf (interpreted-debug-block-locations debug-block)
2390 (result buf)))
2391 code-locs)))
2393 ;;;; operations on debug variables
2395 (defun debug-var-symbol-name (debug-var)
2396 (symbol-name (debug-var-symbol debug-var)))
2398 ;;; FIXME: Make sure that this isn't called anywhere that it wouldn't
2399 ;;; be acceptable to have NIL returned, or that it's only called on
2400 ;;; DEBUG-VARs whose symbols have non-NIL packages.
2401 (defun debug-var-package-name (debug-var)
2402 (package-name (symbol-package (debug-var-symbol debug-var))))
2404 (defun debug-var-valid-value (debug-var frame)
2405 #!+sb-doc
2406 "Returns the value stored for DEBUG-VAR in frame. If the value is not
2407 :valid, then this signals an invalid-value error."
2408 (unless (eq (debug-var-validity debug-var (frame-code-location frame))
2409 :valid)
2410 (error 'invalid-value :debug-var debug-var :frame frame))
2411 (debug-var-value debug-var frame))
2413 (defun debug-var-value (debug-var frame)
2414 #!+sb-doc
2415 "Returns the value stored for DEBUG-VAR in frame. The value may be
2416 invalid. This is SETF'able."
2417 (etypecase debug-var
2418 (compiled-debug-var
2419 (check-type frame compiled-frame)
2420 (let ((res (access-compiled-debug-var-slot debug-var frame)))
2421 (if (indirect-value-cell-p res)
2422 (sb!c:value-cell-ref res)
2423 res)))
2424 (interpreted-debug-var
2425 (check-type frame interpreted-frame)
2426 (sb!eval::leaf-value-lambda-var
2427 (interpreted-code-location-ir1-node (frame-code-location frame))
2428 (interpreted-debug-var-ir1-var debug-var)
2429 (frame-pointer frame)
2430 (interpreted-frame-closure frame)))))
2432 ;;; This returns what is stored for the variable represented by
2433 ;;; DEBUG-VAR relative to the FRAME. This may be an indirect value
2434 ;;; cell if the variable is both closed over and set.
2435 (defun access-compiled-debug-var-slot (debug-var frame)
2436 (let ((escaped (compiled-frame-escaped frame)))
2437 (if escaped
2438 (sub-access-debug-var-slot
2439 (frame-pointer frame)
2440 (compiled-debug-var-sc-offset debug-var)
2441 escaped)
2442 (sub-access-debug-var-slot
2443 (frame-pointer frame)
2444 (or (compiled-debug-var-save-sc-offset debug-var)
2445 (compiled-debug-var-sc-offset debug-var))))))
2447 ;;; a helper function for working with possibly-invalid values:
2448 ;;; Do (MAKE-LISP-OBJ VAL) only if the value looks valid.
2450 ;;; (Such values can arise in registers on machines with conservative
2451 ;;; GC, and might also arise in debug variable locations when
2452 ;;; those variables are invalid.)
2453 (defun make-valid-lisp-obj (val)
2454 (/show0 "entering MAKE-VALID-LISP-OBJ, VAL=..")
2455 #!+sb-show (%primitive print (sb!impl::hexstr val))
2456 (if (or
2457 ;; fixnum
2458 (zerop (logand val 3))
2459 ;; character
2460 (and (zerop (logand val #xffff0000)) ; Top bits zero
2461 (= (logand val #xff) sb!vm:base-char-type)) ; Char tag
2462 ;; unbound marker
2463 (= val sb!vm:unbound-marker-type)
2464 ;; pointer
2465 (and (logand val 1)
2466 ;; Check that the pointer is valid. XXX Could do a better
2467 ;; job. FIXME: e.g. by calling out to an is_valid_pointer
2468 ;; routine in the C runtime support code
2469 (or (< sb!vm:read-only-space-start val
2470 (* sb!vm:*read-only-space-free-pointer*
2471 sb!vm:word-bytes))
2472 (< sb!vm:static-space-start val
2473 (* sb!vm:*static-space-free-pointer*
2474 sb!vm:word-bytes))
2475 (< sb!vm:dynamic-space-start val
2476 (sap-int (dynamic-space-free-pointer))))))
2477 (make-lisp-obj val)
2478 :invalid-object))
2480 ;;; CMU CL had
2481 ;;; (DEFUN SUB-ACCESS-DEBUG-VAR-SLOT (FP SC-OFFSET &OPTIONAL ESCAPED) ..)
2482 ;;; code for this case.
2483 #!-x86
2484 (eval-when (:compile-toplevel :load-toplevel :execute)
2485 (error "hopelessly stale"))
2487 #!+x86
2488 (defun sub-access-debug-var-slot (fp sc-offset &optional escaped)
2489 (declare (type system-area-pointer fp))
2490 (/show0 "entering SUB-ACCESS-DEBUG-VAR-SLOT, FP,SC-OFFSET,ESCAPED=..")
2491 #!+sb-show (%primitive print (sb!impl::hexstr fp))
2492 #!+sb-show (%primitive print (sb!impl::hexstr sc-offset))
2493 #!+sb-show (%primitive print (sb!impl::hexstr escaped))
2494 (macrolet ((with-escaped-value ((var) &body forms)
2495 `(if escaped
2496 (let ((,var (sb!vm:context-register
2497 escaped
2498 (sb!c:sc-offset-offset sc-offset))))
2499 (/show0 "in escaped case, ,VAR value=..")
2500 #!+sb-show (%primitive print (sb!impl::hexstr ,var))
2501 ,@forms)
2502 :invalid-value-for-unescaped-register-storage))
2503 (escaped-float-value (format)
2504 `(if escaped
2505 (sb!vm:context-float-register
2506 escaped (sb!c:sc-offset-offset sc-offset) ',format)
2507 :invalid-value-for-unescaped-register-storage))
2508 (escaped-complex-float-value (format)
2509 `(if escaped
2510 (complex
2511 (sb!vm:context-float-register
2512 escaped (sb!c:sc-offset-offset sc-offset) ',format)
2513 (sb!vm:context-float-register
2514 escaped (1+ (sb!c:sc-offset-offset sc-offset)) ',format))
2515 :invalid-value-for-unescaped-register-storage)))
2516 (ecase (sb!c:sc-offset-scn sc-offset)
2517 ((#.sb!vm:any-reg-sc-number #.sb!vm:descriptor-reg-sc-number)
2518 (/show0 "case of ANY-REG-SC-NUMBER or DESCRIPTOR-REG-SC-NUMBER")
2519 (without-gcing
2520 (with-escaped-value (val)
2521 (/show0 "VAL=..")
2522 #!+sb-show (%primitive print (sb!impl::hexstr val))
2523 (make-valid-lisp-obj val))))
2524 (#.sb!vm:base-char-reg-sc-number
2525 (/show0 "case of BASE-CHAR-REG-SC-NUMBER")
2526 (with-escaped-value (val)
2527 (code-char val)))
2528 (#.sb!vm:sap-reg-sc-number
2529 (/show0 "case of SAP-REG-SC-NUMBER")
2530 (with-escaped-value (val)
2531 (int-sap val)))
2532 (#.sb!vm:signed-reg-sc-number
2533 (/show0 "case of SIGNED-REG-SC-NUMBER")
2534 (with-escaped-value (val)
2535 (if (logbitp (1- sb!vm:word-bits) val)
2536 (logior val (ash -1 sb!vm:word-bits))
2537 val)))
2538 (#.sb!vm:unsigned-reg-sc-number
2539 (/show0 "case of UNSIGNED-REG-SC-NUMBER")
2540 (with-escaped-value (val)
2541 val))
2542 (#.sb!vm:single-reg-sc-number
2543 (/show0 "case of SINGLE-REG-SC-NUMBER")
2544 (escaped-float-value single-float))
2545 (#.sb!vm:double-reg-sc-number
2546 (/show0 "case of DOUBLE-REG-SC-NUMBER")
2547 (escaped-float-value double-float))
2548 #!+long-float
2549 (#.sb!vm:long-reg-sc-number
2550 (/show0 "case of LONG-REG-SC-NUMBER")
2551 (escaped-float-value long-float))
2552 (#.sb!vm:complex-single-reg-sc-number
2553 (/show0 "case of COMPLEX-SINGLE-REG-SC-NUMBER")
2554 (escaped-complex-float-value single-float))
2555 (#.sb!vm:complex-double-reg-sc-number
2556 (/show0 "case of COMPLEX-DOUBLE-REG-SC-NUMBER")
2557 (escaped-complex-float-value double-float))
2558 #!+long-float
2559 (#.sb!vm:complex-long-reg-sc-number
2560 (/show0 "case of COMPLEX-LONG-REG-SC-NUMBER")
2561 (escaped-complex-float-value long-float))
2562 (#.sb!vm:single-stack-sc-number
2563 (/show0 "case of SINGLE-STACK-SC-NUMBER")
2564 (sap-ref-single fp (- (* (1+ (sb!c:sc-offset-offset sc-offset))
2565 sb!vm:word-bytes))))
2566 (#.sb!vm:double-stack-sc-number
2567 (/show0 "case of DOUBLE-STACK-SC-NUMBER")
2568 (sap-ref-double fp (- (* (+ (sb!c:sc-offset-offset sc-offset) 2)
2569 sb!vm:word-bytes))))
2570 #!+long-float
2571 (#.sb!vm:long-stack-sc-number
2572 (/show0 "case of LONG-STACK-SC-NUMBER")
2573 (sap-ref-long fp (- (* (+ (sb!c:sc-offset-offset sc-offset) 3)
2574 sb!vm:word-bytes))))
2575 (#.sb!vm:complex-single-stack-sc-number
2576 (/show0 "case of COMPLEX-STACK-SC-NUMBER")
2577 (complex
2578 (sap-ref-single fp (- (* (1+ (sb!c:sc-offset-offset sc-offset))
2579 sb!vm:word-bytes)))
2580 (sap-ref-single fp (- (* (+ (sb!c:sc-offset-offset sc-offset) 2)
2581 sb!vm:word-bytes)))))
2582 (#.sb!vm:complex-double-stack-sc-number
2583 (/show0 "case of COMPLEX-DOUBLE-STACK-SC-NUMBER")
2584 (complex
2585 (sap-ref-double fp (- (* (+ (sb!c:sc-offset-offset sc-offset) 2)
2586 sb!vm:word-bytes)))
2587 (sap-ref-double fp (- (* (+ (sb!c:sc-offset-offset sc-offset) 4)
2588 sb!vm:word-bytes)))))
2589 #!+long-float
2590 (#.sb!vm:complex-long-stack-sc-number
2591 (/show0 "case of COMPLEX-LONG-STACK-SC-NUMBER")
2592 (complex
2593 (sap-ref-long fp (- (* (+ (sb!c:sc-offset-offset sc-offset) 3)
2594 sb!vm:word-bytes)))
2595 (sap-ref-long fp (- (* (+ (sb!c:sc-offset-offset sc-offset) 6)
2596 sb!vm:word-bytes)))))
2597 (#.sb!vm:control-stack-sc-number
2598 (/show0 "case of CONTROL-STACK-SC-NUMBER")
2599 (stack-ref fp (sb!c:sc-offset-offset sc-offset)))
2600 (#.sb!vm:base-char-stack-sc-number
2601 (/show0 "case of BASE-CHAR-STACK-SC-NUMBER")
2602 (code-char
2603 (sap-ref-32 fp (- (* (1+ (sb!c:sc-offset-offset sc-offset))
2604 sb!vm:word-bytes)))))
2605 (#.sb!vm:unsigned-stack-sc-number
2606 (/show0 "case of UNSIGNED-STACK-SC-NUMBER")
2607 (sap-ref-32 fp (- (* (1+ (sb!c:sc-offset-offset sc-offset))
2608 sb!vm:word-bytes))))
2609 (#.sb!vm:signed-stack-sc-number
2610 (/show0 "case of SIGNED-STACK-SC-NUMBER")
2611 (signed-sap-ref-32 fp (- (* (1+ (sb!c:sc-offset-offset sc-offset))
2612 sb!vm:word-bytes))))
2613 (#.sb!vm:sap-stack-sc-number
2614 (/show0 "case of SAP-STACK-SC-NUMBER")
2615 (sap-ref-sap fp (- (* (1+ (sb!c:sc-offset-offset sc-offset))
2616 sb!vm:word-bytes)))))))
2618 ;;; This stores value as the value of DEBUG-VAR in FRAME. In the
2619 ;;; COMPILED-DEBUG-VAR case, access the current value to determine if
2620 ;;; it is an indirect value cell. This occurs when the variable is
2621 ;;; both closed over and set. For INTERPRETED-DEBUG-VARs just call
2622 ;;; SB!EVAL::SET-LEAF-VALUE-LAMBDA-VAR with the right interpreter
2623 ;;; objects.
2624 (defun %set-debug-var-value (debug-var frame value)
2625 (etypecase debug-var
2626 (compiled-debug-var
2627 (check-type frame compiled-frame)
2628 (let ((current-value (access-compiled-debug-var-slot debug-var frame)))
2629 (if (indirect-value-cell-p current-value)
2630 (sb!c:value-cell-set current-value value)
2631 (set-compiled-debug-var-slot debug-var frame value))))
2632 (interpreted-debug-var
2633 (check-type frame interpreted-frame)
2634 (sb!eval::set-leaf-value-lambda-var
2635 (interpreted-code-location-ir1-node (frame-code-location frame))
2636 (interpreted-debug-var-ir1-var debug-var)
2637 (frame-pointer frame)
2638 (interpreted-frame-closure frame)
2639 value)))
2640 value)
2642 ;;; This stores value for the variable represented by debug-var
2643 ;;; relative to the frame. This assumes the location directly contains
2644 ;;; the variable's value; that is, there is no indirect value cell
2645 ;;; currently there in case the variable is both closed over and set.
2646 (defun set-compiled-debug-var-slot (debug-var frame value)
2647 (let ((escaped (compiled-frame-escaped frame)))
2648 (if escaped
2649 (sub-set-debug-var-slot (frame-pointer frame)
2650 (compiled-debug-var-sc-offset debug-var)
2651 value escaped)
2652 (sub-set-debug-var-slot
2653 (frame-pointer frame)
2654 (or (compiled-debug-var-save-sc-offset debug-var)
2655 (compiled-debug-var-sc-offset debug-var))
2656 value))))
2658 #!-x86
2659 (defun sub-set-debug-var-slot (fp sc-offset value &optional escaped)
2660 (macrolet ((set-escaped-value (val)
2661 `(if escaped
2662 (setf (sb!vm:context-register
2663 escaped
2664 (sb!c:sc-offset-offset sc-offset))
2665 ,val)
2666 value))
2667 (set-escaped-float-value (format val)
2668 `(if escaped
2669 (setf (sb!vm:context-float-register
2670 escaped
2671 (sb!c:sc-offset-offset sc-offset)
2672 ',format)
2673 ,val)
2674 value))
2675 (with-nfp ((var) &body body)
2676 `(let ((,var (if escaped
2677 (int-sap
2678 (sb!vm:context-register escaped
2679 sb!vm::nfp-offset))
2680 #!-alpha
2681 (sap-ref-sap fp
2682 (* sb!vm::nfp-save-offset
2683 sb!vm:word-bytes))
2684 #!+alpha
2685 (%alpha::make-number-stack-pointer
2686 (sap-ref-32 fp
2687 (* sb!vm::nfp-save-offset
2688 sb!vm:word-bytes))))))
2689 ,@body)))
2690 (ecase (sb!c:sc-offset-scn sc-offset)
2691 ((#.sb!vm:any-reg-sc-number
2692 #.sb!vm:descriptor-reg-sc-number
2693 #!+rt #.sb!vm:word-pointer-reg-sc-number)
2694 (without-gcing
2695 (set-escaped-value
2696 (get-lisp-obj-address value))))
2697 (#.sb!vm:base-char-reg-sc-number
2698 (set-escaped-value (char-code value)))
2699 (#.sb!vm:sap-reg-sc-number
2700 (set-escaped-value (sap-int value)))
2701 (#.sb!vm:signed-reg-sc-number
2702 (set-escaped-value (logand value (1- (ash 1 sb!vm:word-bits)))))
2703 (#.sb!vm:unsigned-reg-sc-number
2704 (set-escaped-value value))
2705 (#.sb!vm:non-descriptor-reg-sc-number
2706 (error "Local non-descriptor register access?"))
2707 (#.sb!vm:interior-reg-sc-number
2708 (error "Local interior register access?"))
2709 (#.sb!vm:single-reg-sc-number
2710 (set-escaped-float-value single-float value))
2711 (#.sb!vm:double-reg-sc-number
2712 (set-escaped-float-value double-float value))
2713 #!+long-float
2714 (#.sb!vm:long-reg-sc-number
2715 (set-escaped-float-value long-float value))
2716 (#.sb!vm:complex-single-reg-sc-number
2717 (when escaped
2718 (setf (sb!vm:context-float-register escaped
2719 (sb!c:sc-offset-offset sc-offset)
2720 'single-float)
2721 (realpart value))
2722 (setf (sb!vm:context-float-register
2723 escaped (1+ (sb!c:sc-offset-offset sc-offset))
2724 'single-float)
2725 (imagpart value)))
2726 value)
2727 (#.sb!vm:complex-double-reg-sc-number
2728 (when escaped
2729 (setf (sb!vm:context-float-register
2730 escaped (sb!c:sc-offset-offset sc-offset) 'double-float)
2731 (realpart value))
2732 (setf (sb!vm:context-float-register
2733 escaped
2734 (+ (sb!c:sc-offset-offset sc-offset) #!+sparc 2 #!-sparc 1)
2735 'double-float)
2736 (imagpart value)))
2737 value)
2738 #!+long-float
2739 (#.sb!vm:complex-long-reg-sc-number
2740 (when escaped
2741 (setf (sb!vm:context-float-register
2742 escaped (sb!c:sc-offset-offset sc-offset) 'long-float)
2743 (realpart value))
2744 (setf (sb!vm:context-float-register
2745 escaped
2746 (+ (sb!c:sc-offset-offset sc-offset) #!+sparc 4)
2747 'long-float)
2748 (imagpart value)))
2749 value)
2750 (#.sb!vm:single-stack-sc-number
2751 (with-nfp (nfp)
2752 (setf (sap-ref-single nfp (* (sb!c:sc-offset-offset sc-offset)
2753 sb!vm:word-bytes))
2754 (the single-float value))))
2755 (#.sb!vm:double-stack-sc-number
2756 (with-nfp (nfp)
2757 (setf (sap-ref-double nfp (* (sb!c:sc-offset-offset sc-offset)
2758 sb!vm:word-bytes))
2759 (the double-float value))))
2760 #!+long-float
2761 (#.sb!vm:long-stack-sc-number
2762 (with-nfp (nfp)
2763 (setf (sap-ref-long nfp (* (sb!c:sc-offset-offset sc-offset)
2764 sb!vm:word-bytes))
2765 (the long-float value))))
2766 (#.sb!vm:complex-single-stack-sc-number
2767 (with-nfp (nfp)
2768 (setf (sap-ref-single
2769 nfp (* (sb!c:sc-offset-offset sc-offset) sb!vm:word-bytes))
2770 (the single-float (realpart value)))
2771 (setf (sap-ref-single
2772 nfp (* (1+ (sb!c:sc-offset-offset sc-offset))
2773 sb!vm:word-bytes))
2774 (the single-float (realpart value)))))
2775 (#.sb!vm:complex-double-stack-sc-number
2776 (with-nfp (nfp)
2777 (setf (sap-ref-double
2778 nfp (* (sb!c:sc-offset-offset sc-offset) sb!vm:word-bytes))
2779 (the double-float (realpart value)))
2780 (setf (sap-ref-double
2781 nfp (* (+ (sb!c:sc-offset-offset sc-offset) 2)
2782 sb!vm:word-bytes))
2783 (the double-float (realpart value)))))
2784 #!+long-float
2785 (#.sb!vm:complex-long-stack-sc-number
2786 (with-nfp (nfp)
2787 (setf (sap-ref-long
2788 nfp (* (sb!c:sc-offset-offset sc-offset) sb!vm:word-bytes))
2789 (the long-float (realpart value)))
2790 (setf (sap-ref-long
2791 nfp (* (+ (sb!c:sc-offset-offset sc-offset) #!+sparc 4)
2792 sb!vm:word-bytes))
2793 (the long-float (realpart value)))))
2794 (#.sb!vm:control-stack-sc-number
2795 (setf (stack-ref fp (sb!c:sc-offset-offset sc-offset)) value))
2796 (#.sb!vm:base-char-stack-sc-number
2797 (with-nfp (nfp)
2798 (setf (sap-ref-32 nfp (* (sb!c:sc-offset-offset sc-offset)
2799 sb!vm:word-bytes))
2800 (char-code (the character value)))))
2801 (#.sb!vm:unsigned-stack-sc-number
2802 (with-nfp (nfp)
2803 (setf (sap-ref-32 nfp (* (sb!c:sc-offset-offset sc-offset)
2804 sb!vm:word-bytes))
2805 (the (unsigned-byte 32) value))))
2806 (#.sb!vm:signed-stack-sc-number
2807 (with-nfp (nfp)
2808 (setf (signed-sap-ref-32 nfp (* (sb!c:sc-offset-offset sc-offset)
2809 sb!vm:word-bytes))
2810 (the (signed-byte 32) value))))
2811 (#.sb!vm:sap-stack-sc-number
2812 (with-nfp (nfp)
2813 (setf (sap-ref-sap nfp (* (sb!c:sc-offset-offset sc-offset)
2814 sb!vm:word-bytes))
2815 (the system-area-pointer value)))))))
2817 #!+x86
2818 (defun sub-set-debug-var-slot (fp sc-offset value &optional escaped)
2819 (macrolet ((set-escaped-value (val)
2820 `(if escaped
2821 (setf (sb!vm:context-register
2822 escaped
2823 (sb!c:sc-offset-offset sc-offset))
2824 ,val)
2825 value)))
2826 (ecase (sb!c:sc-offset-scn sc-offset)
2827 ((#.sb!vm:any-reg-sc-number #.sb!vm:descriptor-reg-sc-number)
2828 (without-gcing
2829 (set-escaped-value
2830 (get-lisp-obj-address value))))
2831 (#.sb!vm:base-char-reg-sc-number
2832 (set-escaped-value (char-code value)))
2833 (#.sb!vm:sap-reg-sc-number
2834 (set-escaped-value (sap-int value)))
2835 (#.sb!vm:signed-reg-sc-number
2836 (set-escaped-value (logand value (1- (ash 1 sb!vm:word-bits)))))
2837 (#.sb!vm:unsigned-reg-sc-number
2838 (set-escaped-value value))
2839 (#.sb!vm:single-reg-sc-number
2840 #+nil ;; don't have escaped floats.
2841 (set-escaped-float-value single-float value))
2842 (#.sb!vm:double-reg-sc-number
2843 #+nil ;; don't have escaped floats -- still in npx?
2844 (set-escaped-float-value double-float value))
2845 #!+long-float
2846 (#.sb!vm:long-reg-sc-number
2847 #+nil ;; don't have escaped floats -- still in npx?
2848 (set-escaped-float-value long-float value))
2849 (#.sb!vm:single-stack-sc-number
2850 (setf (sap-ref-single
2851 fp (- (* (1+ (sb!c:sc-offset-offset sc-offset))
2852 sb!vm:word-bytes)))
2853 (the single-float value)))
2854 (#.sb!vm:double-stack-sc-number
2855 (setf (sap-ref-double
2856 fp (- (* (+ (sb!c:sc-offset-offset sc-offset) 2)
2857 sb!vm:word-bytes)))
2858 (the double-float value)))
2859 #!+long-float
2860 (#.sb!vm:long-stack-sc-number
2861 (setf (sap-ref-long
2862 fp (- (* (+ (sb!c:sc-offset-offset sc-offset) 3)
2863 sb!vm:word-bytes)))
2864 (the long-float value)))
2865 (#.sb!vm:complex-single-stack-sc-number
2866 (setf (sap-ref-single
2867 fp (- (* (1+ (sb!c:sc-offset-offset sc-offset))
2868 sb!vm:word-bytes)))
2869 (realpart (the (complex single-float) value)))
2870 (setf (sap-ref-single
2871 fp (- (* (+ (sb!c:sc-offset-offset sc-offset) 2)
2872 sb!vm:word-bytes)))
2873 (imagpart (the (complex single-float) value))))
2874 (#.sb!vm:complex-double-stack-sc-number
2875 (setf (sap-ref-double
2876 fp (- (* (+ (sb!c:sc-offset-offset sc-offset) 2)
2877 sb!vm:word-bytes)))
2878 (realpart (the (complex double-float) value)))
2879 (setf (sap-ref-double
2880 fp (- (* (+ (sb!c:sc-offset-offset sc-offset) 4)
2881 sb!vm:word-bytes)))
2882 (imagpart (the (complex double-float) value))))
2883 #!+long-float
2884 (#.sb!vm:complex-long-stack-sc-number
2885 (setf (sap-ref-long
2886 fp (- (* (+ (sb!c:sc-offset-offset sc-offset) 3)
2887 sb!vm:word-bytes)))
2888 (realpart (the (complex long-float) value)))
2889 (setf (sap-ref-long
2890 fp (- (* (+ (sb!c:sc-offset-offset sc-offset) 6)
2891 sb!vm:word-bytes)))
2892 (imagpart (the (complex long-float) value))))
2893 (#.sb!vm:control-stack-sc-number
2894 (setf (stack-ref fp (sb!c:sc-offset-offset sc-offset)) value))
2895 (#.sb!vm:base-char-stack-sc-number
2896 (setf (sap-ref-32 fp (- (* (1+ (sb!c:sc-offset-offset sc-offset))
2897 sb!vm:word-bytes)))
2898 (char-code (the character value))))
2899 (#.sb!vm:unsigned-stack-sc-number
2900 (setf (sap-ref-32 fp (- (* (1+ (sb!c:sc-offset-offset sc-offset))
2901 sb!vm:word-bytes)))
2902 (the (unsigned-byte 32) value)))
2903 (#.sb!vm:signed-stack-sc-number
2904 (setf (signed-sap-ref-32
2905 fp (- (* (1+ (sb!c:sc-offset-offset sc-offset)) sb!vm:word-bytes)))
2906 (the (signed-byte 32) value)))
2907 (#.sb!vm:sap-stack-sc-number
2908 (setf (sap-ref-sap fp (- (* (1+ (sb!c:sc-offset-offset sc-offset))
2909 sb!vm:word-bytes)))
2910 (the system-area-pointer value))))))
2912 ;;; The method for setting and accessing COMPILED-DEBUG-VAR values use
2913 ;;; this to determine if the value stored is the actual value or an
2914 ;;; indirection cell.
2915 (defun indirect-value-cell-p (x)
2916 (and (= (get-lowtag x) sb!vm:other-pointer-type)
2917 (= (get-type x) sb!vm:value-cell-header-type)))
2919 ;;; If the variable is always alive, then it is valid. If the
2920 ;;; code-location is unknown, then the variable's validity is
2921 ;;; :unknown. Once we've called CODE-LOCATION-UNKNOWN-P, we know the
2922 ;;; live-set information has been cached in the code-location.
2923 (defun debug-var-validity (debug-var basic-code-location)
2924 #!+sb-doc
2925 "Returns three values reflecting the validity of DEBUG-VAR's value
2926 at BASIC-CODE-LOCATION:
2927 :VALID The value is known to be available.
2928 :INVALID The value is known to be unavailable.
2929 :UNKNOWN The value's availability is unknown."
2930 (etypecase debug-var
2931 (compiled-debug-var
2932 (compiled-debug-var-validity debug-var basic-code-location))
2933 (interpreted-debug-var
2934 (check-type basic-code-location interpreted-code-location)
2935 (let ((validp (rassoc (interpreted-debug-var-ir1-var debug-var)
2936 (sb!c::lexenv-variables
2937 (sb!c::node-lexenv
2938 (interpreted-code-location-ir1-node
2939 basic-code-location))))))
2940 (if validp :valid :invalid)))))
2942 ;;; This is the method for DEBUG-VAR-VALIDITY for COMPILED-DEBUG-VARs.
2943 ;;; For safety, make sure basic-code-location is what we think.
2944 (defun compiled-debug-var-validity (debug-var basic-code-location)
2945 (check-type basic-code-location compiled-code-location)
2946 (cond ((debug-var-alive-p debug-var)
2947 (let ((debug-fun (code-location-debug-function basic-code-location)))
2948 (if (>= (compiled-code-location-pc basic-code-location)
2949 (sb!c::compiled-debug-function-start-pc
2950 (compiled-debug-function-compiler-debug-fun debug-fun)))
2951 :valid
2952 :invalid)))
2953 ((code-location-unknown-p basic-code-location) :unknown)
2955 (let ((pos (position debug-var
2956 (debug-function-debug-vars
2957 (code-location-debug-function basic-code-location)))))
2958 (unless pos
2959 (error 'unknown-debug-var
2960 :debug-var debug-var
2961 :debug-function
2962 (code-location-debug-function basic-code-location)))
2963 ;; There must be live-set info since basic-code-location is known.
2964 (if (zerop (sbit (compiled-code-location-live-set basic-code-location)
2965 pos))
2966 :invalid
2967 :valid)))))
2969 ;;;; sources
2971 ;;; This code produces and uses what we call source-paths. A
2972 ;;; source-path is a list whose first element is a form number as
2973 ;;; returned by CODE-LOCATION-FORM-NUMBER and whose last element is a
2974 ;;; top-level-form number as returned by
2975 ;;; CODE-LOCATION-TOP-LEVEL-FORM-NUMBER. The elements from the last to
2976 ;;; the first, exclusively, are the numbered subforms into which to
2977 ;;; descend. For example:
2978 ;;; (defun foo (x)
2979 ;;; (let ((a (aref x 3)))
2980 ;;; (cons a 3)))
2981 ;;; The call to AREF in this example is form number 5. Assuming this
2982 ;;; DEFUN is the 11'th top-level-form, the source-path for the AREF
2983 ;;; call is as follows:
2984 ;;; (5 1 0 1 3 11)
2985 ;;; Given the DEFUN, 3 gets you the LET, 1 gets you the bindings, 0
2986 ;;; gets the first binding, and 1 gets the AREF form.
2988 ;;; Temporary buffer used to build form-number => source-path translation in
2989 ;;; FORM-NUMBER-TRANSLATIONS.
2990 (defvar *form-number-temp* (make-array 10 :fill-pointer 0 :adjustable t))
2992 ;;; Table used to detect CAR circularities in FORM-NUMBER-TRANSLATIONS.
2993 (defvar *form-number-circularity-table* (make-hash-table :test 'eq))
2995 ;;; The vector elements are in the same format as the compiler's
2996 ;;; NODE-SOUCE-PATH; that is, the first element is the form number and the last
2997 ;;; is the top-level-form number.
2998 (defun form-number-translations (form tlf-number)
2999 #!+sb-doc
3000 "This returns a table mapping form numbers to source-paths. A source-path
3001 indicates a descent into the top-level-form form, going directly to the
3002 subform corressponding to the form number."
3003 (clrhash *form-number-circularity-table*)
3004 (setf (fill-pointer *form-number-temp*) 0)
3005 (sub-translate-form-numbers form (list tlf-number))
3006 (coerce *form-number-temp* 'simple-vector))
3007 (defun sub-translate-form-numbers (form path)
3008 (unless (gethash form *form-number-circularity-table*)
3009 (setf (gethash form *form-number-circularity-table*) t)
3010 (vector-push-extend (cons (fill-pointer *form-number-temp*) path)
3011 *form-number-temp*)
3012 (let ((pos 0)
3013 (subform form)
3014 (trail form))
3015 (declare (fixnum pos))
3016 (macrolet ((frob ()
3017 '(progn
3018 (when (atom subform) (return))
3019 (let ((fm (car subform)))
3020 (when (consp fm)
3021 (sub-translate-form-numbers fm (cons pos path)))
3022 (incf pos))
3023 (setq subform (cdr subform))
3024 (when (eq subform trail) (return)))))
3025 (loop
3026 (frob)
3027 (frob)
3028 (setq trail (cdr trail)))))))
3030 (defun source-path-context (form path context)
3031 #!+sb-doc
3032 "Form is a top-level form, and path is a source-path into it. This returns
3033 the form indicated by the source-path. Context is the number of enclosing
3034 forms to return instead of directly returning the source-path form. When
3035 context is non-zero, the form returned contains a marker, #:****HERE****,
3036 immediately before the form indicated by path."
3037 (declare (type unsigned-byte context))
3038 ;; Get to the form indicated by path or the enclosing form indicated
3039 ;; by context and path.
3040 (let ((path (reverse (butlast (cdr path)))))
3041 (dotimes (i (- (length path) context))
3042 (let ((index (first path)))
3043 (unless (and (listp form) (< index (length form)))
3044 (error "Source path no longer exists."))
3045 (setq form (elt form index))
3046 (setq path (rest path))))
3047 ;; Recursively rebuild the source form resulting from the above
3048 ;; descent, copying the beginning of each subform up to the next
3049 ;; subform we descend into according to path. At the bottom of the
3050 ;; recursion, we return the form indicated by path preceded by our
3051 ;; marker, and this gets spliced into the resulting list structure
3052 ;; on the way back up.
3053 (labels ((frob (form path level)
3054 (if (or (zerop level) (null path))
3055 (if (zerop context)
3056 form
3057 `(#:***here*** ,form))
3058 (let ((n (first path)))
3059 (unless (and (listp form) (< n (length form)))
3060 (error "Source path no longer exists."))
3061 (let ((res (frob (elt form n) (rest path) (1- level))))
3062 (nconc (subseq form 0 n)
3063 (cons res (nthcdr (1+ n) form))))))))
3064 (frob form path context))))
3066 ;;;; PREPROCESS-FOR-EVAL and EVAL-IN-FRAME
3068 ;;; Create a SYMBOL-MACROLET for each variable valid at the location which
3069 ;;; accesses that variable from the frame argument.
3070 (defun preprocess-for-eval (form loc)
3071 #!+sb-doc
3072 "Return a function of one argument that evaluates form in the lexical
3073 context of the basic-code-location loc. PREPROCESS-FOR-EVAL signals a
3074 no-debug-vars condition when the loc's debug-function has no
3075 debug-var information available. The returned function takes the frame
3076 to get values from as its argument, and it returns the values of form.
3077 The returned function signals the following conditions: invalid-value,
3078 ambiguous-variable-name, and frame-function-mismatch"
3079 (declare (type code-location loc))
3080 (let ((n-frame (gensym))
3081 (fun (code-location-debug-function loc)))
3082 (unless (debug-var-info-available fun)
3083 (debug-signal 'no-debug-vars :debug-function fun))
3084 (sb!int:collect ((binds)
3085 (specs))
3086 (do-debug-function-variables (var fun)
3087 (let ((validity (debug-var-validity var loc)))
3088 (unless (eq validity :invalid)
3089 (let* ((sym (debug-var-symbol var))
3090 (found (assoc sym (binds))))
3091 (if found
3092 (setf (second found) :ambiguous)
3093 (binds (list sym validity var)))))))
3094 (dolist (bind (binds))
3095 (let ((name (first bind))
3096 (var (third bind)))
3097 (ecase (second bind)
3098 (:valid
3099 (specs `(,name (debug-var-value ',var ,n-frame))))
3100 (:unknown
3101 (specs `(,name (debug-signal 'invalid-value :debug-var ',var
3102 :frame ,n-frame))))
3103 (:ambiguous
3104 (specs `(,name (debug-signal 'ambiguous-variable-name :name ',name
3105 :frame ,n-frame)))))))
3106 (let ((res (coerce `(lambda (,n-frame)
3107 (declare (ignorable ,n-frame))
3108 (symbol-macrolet ,(specs) ,form))
3109 'function)))
3110 #'(lambda (frame)
3111 ;; This prevents these functions from being used in any
3112 ;; location other than a function return location, so
3113 ;; maybe this should only check whether frame's
3114 ;; debug-function is the same as loc's.
3115 (unless (code-location= (frame-code-location frame) loc)
3116 (debug-signal 'frame-function-mismatch
3117 :code-location loc :form form :frame frame))
3118 (funcall res frame))))))
3120 (defun eval-in-frame (frame form)
3121 (declare (type frame frame))
3122 #!+sb-doc
3123 "Evaluate Form in the lexical context of Frame's current code location,
3124 returning the results of the evaluation."
3125 (funcall (preprocess-for-eval form (frame-code-location frame)) frame))
3127 ;;;; breakpoints
3129 ;;;; user-visible interface
3131 (defun make-breakpoint (hook-function what
3132 &key (kind :code-location) info function-end-cookie)
3133 #!+sb-doc
3134 "This creates and returns a breakpoint. When program execution encounters
3135 the breakpoint, the system calls hook-function. Hook-function takes the
3136 current frame for the function in which the program is running and the
3137 breakpoint object.
3138 What and kind determine where in a function the system invokes
3139 hook-function. What is either a code-location or a debug-function. Kind is
3140 one of :code-location, :function-start, or :function-end. Since the starts
3141 and ends of functions may not have code-locations representing them,
3142 designate these places by supplying what as a debug-function and kind
3143 indicating the :function-start or :function-end. When what is a
3144 debug-function and kind is :function-end, then hook-function must take two
3145 additional arguments, a list of values returned by the function and a
3146 function-end-cookie.
3147 Info is information supplied by and used by the user.
3148 Function-end-cookie is a function. To implement :function-end breakpoints,
3149 the system uses starter breakpoints to establish the :function-end breakpoint
3150 for each invocation of the function. Upon each entry, the system creates a
3151 unique cookie to identify the invocation, and when the user supplies a
3152 function for this argument, the system invokes it on the frame and the
3153 cookie. The system later invokes the :function-end breakpoint hook on the
3154 same cookie. The user may save the cookie for comparison in the hook
3155 function.
3156 This signals an error if what is an unknown code-location."
3157 (etypecase what
3158 (code-location
3159 (when (code-location-unknown-p what)
3160 (error "cannot make a breakpoint at an unknown code location: ~S"
3161 what))
3162 (assert (eq kind :code-location))
3163 (let ((bpt (%make-breakpoint hook-function what kind info)))
3164 (etypecase what
3165 (interpreted-code-location
3166 (error "Breakpoints in interpreted code are currently unsupported."))
3167 (compiled-code-location
3168 ;; This slot is filled in due to calling CODE-LOCATION-UNKNOWN-P.
3169 (when (eq (compiled-code-location-kind what) :unknown-return)
3170 (let ((other-bpt (%make-breakpoint hook-function what
3171 :unknown-return-partner
3172 info)))
3173 (setf (breakpoint-unknown-return-partner bpt) other-bpt)
3174 (setf (breakpoint-unknown-return-partner other-bpt) bpt)))))
3175 bpt))
3176 (compiled-debug-function
3177 (ecase kind
3178 (:function-start
3179 (%make-breakpoint hook-function what kind info))
3180 (:function-end
3181 (unless (eq (sb!c::compiled-debug-function-returns
3182 (compiled-debug-function-compiler-debug-fun what))
3183 :standard)
3184 (error ":FUNCTION-END breakpoints are currently unsupported ~
3185 for the known return convention."))
3187 (let* ((bpt (%make-breakpoint hook-function what kind info))
3188 (starter (compiled-debug-function-end-starter what)))
3189 (unless starter
3190 (setf starter (%make-breakpoint #'list what :function-start nil))
3191 (setf (breakpoint-hook-function starter)
3192 (function-end-starter-hook starter what))
3193 (setf (compiled-debug-function-end-starter what) starter))
3194 (setf (breakpoint-start-helper bpt) starter)
3195 (push bpt (breakpoint-%info starter))
3196 (setf (breakpoint-cookie-fun bpt) function-end-cookie)
3197 bpt))))
3198 (interpreted-debug-function
3199 (error ":function-end breakpoints are currently unsupported ~
3200 for interpreted-debug-functions."))))
3202 ;;; These are unique objects created upon entry into a function by a
3203 ;;; :FUNCTION-END breakpoint's starter hook. These are only created
3204 ;;; when users supply :FUNCTION-END-COOKIE to MAKE-BREAKPOINT. Also,
3205 ;;; the :FUNCTION-END breakpoint's hook is called on the same cookie
3206 ;;; when it is created.
3207 (defstruct (function-end-cookie
3208 (:print-object (lambda (obj str)
3209 (print-unreadable-object (obj str :type t))))
3210 (:constructor make-function-end-cookie (bogus-lra debug-fun)))
3211 ;; This is a pointer to the bogus-lra created for :function-end bpts.
3212 bogus-lra
3213 ;; This is the debug-function associated with the cookie.
3214 debug-fun)
3216 ;;; This maps bogus-lra-components to cookies, so
3217 ;;; HANDLE-FUNCTION-END-BREAKPOINT can find the appropriate cookie for the
3218 ;;; breakpoint hook.
3219 (defvar *function-end-cookies* (make-hash-table :test 'eq))
3221 ;;; This returns a hook function for the start helper breakpoint
3222 ;;; associated with a :FUNCTION-END breakpoint. The returned function
3223 ;;; makes a fake LRA that all returns go through, and this piece of
3224 ;;; fake code actually breaks. Upon return from the break, the code
3225 ;;; provides the returnee with any values. Since the returned function
3226 ;;; effectively activates FUN-END-BPT on each entry to DEBUG-FUN's
3227 ;;; function, we must establish breakpoint-data about FUN-END-BPT.
3228 (defun function-end-starter-hook (starter-bpt debug-fun)
3229 (declare (type breakpoint starter-bpt)
3230 (type compiled-debug-function debug-fun))
3231 #'(lambda (frame breakpoint)
3232 (declare (ignore breakpoint)
3233 (type frame frame))
3234 (let ((lra-sc-offset
3235 (sb!c::compiled-debug-function-return-pc
3236 (compiled-debug-function-compiler-debug-fun debug-fun))))
3237 (multiple-value-bind (lra component offset)
3238 (make-bogus-lra
3239 (get-context-value frame
3240 #!-gengc sb!vm::lra-save-offset
3241 #!+gengc sb!vm::ra-save-offset
3242 lra-sc-offset))
3243 (setf (get-context-value frame
3244 #!-gengc sb!vm::lra-save-offset
3245 #!+gengc sb!vm::ra-save-offset
3246 lra-sc-offset)
3247 lra)
3248 (let ((end-bpts (breakpoint-%info starter-bpt)))
3249 (let ((data (breakpoint-data component offset)))
3250 (setf (breakpoint-data-breakpoints data) end-bpts)
3251 (dolist (bpt end-bpts)
3252 (setf (breakpoint-internal-data bpt) data)))
3253 (let ((cookie (make-function-end-cookie lra debug-fun)))
3254 (setf (gethash component *function-end-cookies*) cookie)
3255 (dolist (bpt end-bpts)
3256 (let ((fun (breakpoint-cookie-fun bpt)))
3257 (when fun (funcall fun frame cookie))))))))))
3259 (defun function-end-cookie-valid-p (frame cookie)
3260 #!+sb-doc
3261 "This takes a function-end-cookie and a frame, and it returns whether the
3262 cookie is still valid. A cookie becomes invalid when the frame that
3263 established the cookie has exited. Sometimes cookie holders are unaware
3264 of cookie invalidation because their :function-end breakpoint hooks didn't
3265 run due to THROW'ing. This takes a frame as an efficiency hack since the
3266 user probably has a frame object in hand when using this routine, and it
3267 saves repeated parsing of the stack and consing when asking whether a
3268 series of cookies is valid."
3269 (let ((lra (function-end-cookie-bogus-lra cookie))
3270 (lra-sc-offset (sb!c::compiled-debug-function-return-pc
3271 (compiled-debug-function-compiler-debug-fun
3272 (function-end-cookie-debug-fun cookie)))))
3273 (do ((frame frame (frame-down frame)))
3274 ((not frame) nil)
3275 (when (and (compiled-frame-p frame)
3276 (eq lra
3277 (get-context-value frame
3278 #!-gengc sb!vm::lra-save-offset
3279 #!+gengc sb!vm::ra-save-offset
3280 lra-sc-offset)))
3281 (return t)))))
3283 ;;;; ACTIVATE-BREAKPOINT
3285 (defun activate-breakpoint (breakpoint)
3286 #!+sb-doc
3287 "This causes the system to invoke the breakpoint's hook-function until the
3288 next call to DEACTIVATE-BREAKPOINT or DELETE-BREAKPOINT. The system invokes
3289 breakpoint hook functions in the opposite order that you activate them."
3290 (when (eq (breakpoint-status breakpoint) :deleted)
3291 (error "cannot activate a deleted breakpoint: ~S" breakpoint))
3292 (unless (eq (breakpoint-status breakpoint) :active)
3293 (ecase (breakpoint-kind breakpoint)
3294 (:code-location
3295 (let ((loc (breakpoint-what breakpoint)))
3296 (etypecase loc
3297 (interpreted-code-location
3298 (error "Breakpoints in interpreted code are currently unsupported."))
3299 (compiled-code-location
3300 (activate-compiled-code-location-breakpoint breakpoint)
3301 (let ((other (breakpoint-unknown-return-partner breakpoint)))
3302 (when other
3303 (activate-compiled-code-location-breakpoint other)))))))
3304 (:function-start
3305 (etypecase (breakpoint-what breakpoint)
3306 (compiled-debug-function
3307 (activate-compiled-function-start-breakpoint breakpoint))
3308 (interpreted-debug-function
3309 (error "I don't know how you made this, but they're unsupported: ~S"
3310 (breakpoint-what breakpoint)))))
3311 (:function-end
3312 (etypecase (breakpoint-what breakpoint)
3313 (compiled-debug-function
3314 (let ((starter (breakpoint-start-helper breakpoint)))
3315 (unless (eq (breakpoint-status starter) :active)
3316 ;; May already be active by some other :function-end breakpoint.
3317 (activate-compiled-function-start-breakpoint starter)))
3318 (setf (breakpoint-status breakpoint) :active))
3319 (interpreted-debug-function
3320 (error "I don't know how you made this, but they're unsupported: ~S"
3321 (breakpoint-what breakpoint)))))))
3322 breakpoint)
3324 (defun activate-compiled-code-location-breakpoint (breakpoint)
3325 (declare (type breakpoint breakpoint))
3326 (let ((loc (breakpoint-what breakpoint)))
3327 (declare (type compiled-code-location loc))
3328 (sub-activate-breakpoint
3329 breakpoint
3330 (breakpoint-data (compiled-debug-function-component
3331 (code-location-debug-function loc))
3332 (+ (compiled-code-location-pc loc)
3333 (if (or (eq (breakpoint-kind breakpoint)
3334 :unknown-return-partner)
3335 (eq (compiled-code-location-kind loc)
3336 :single-value-return))
3337 sb!vm:single-value-return-byte-offset
3338 0))))))
3340 (defun activate-compiled-function-start-breakpoint (breakpoint)
3341 (declare (type breakpoint breakpoint))
3342 (let ((debug-fun (breakpoint-what breakpoint)))
3343 (sub-activate-breakpoint
3344 breakpoint
3345 (breakpoint-data (compiled-debug-function-component debug-fun)
3346 (sb!c::compiled-debug-function-start-pc
3347 (compiled-debug-function-compiler-debug-fun
3348 debug-fun))))))
3350 (defun sub-activate-breakpoint (breakpoint data)
3351 (declare (type breakpoint breakpoint)
3352 (type breakpoint-data data))
3353 (setf (breakpoint-status breakpoint) :active)
3354 (without-interrupts
3355 (unless (breakpoint-data-breakpoints data)
3356 (setf (breakpoint-data-instruction data)
3357 (without-gcing
3358 (breakpoint-install (get-lisp-obj-address
3359 (breakpoint-data-component data))
3360 (breakpoint-data-offset data)))))
3361 (setf (breakpoint-data-breakpoints data)
3362 (append (breakpoint-data-breakpoints data) (list breakpoint)))
3363 (setf (breakpoint-internal-data breakpoint) data)))
3365 ;;;; DEACTIVATE-BREAKPOINT
3367 (defun deactivate-breakpoint (breakpoint)
3368 #!+sb-doc
3369 "This stops the system from invoking the breakpoint's hook-function."
3370 (when (eq (breakpoint-status breakpoint) :active)
3371 (without-interrupts
3372 (let ((loc (breakpoint-what breakpoint)))
3373 (etypecase loc
3374 ((or interpreted-code-location interpreted-debug-function)
3375 (error
3376 "Breakpoints in interpreted code are currently unsupported."))
3377 ((or compiled-code-location compiled-debug-function)
3378 (deactivate-compiled-breakpoint breakpoint)
3379 (let ((other (breakpoint-unknown-return-partner breakpoint)))
3380 (when other
3381 (deactivate-compiled-breakpoint other))))))))
3382 breakpoint)
3384 (defun deactivate-compiled-breakpoint (breakpoint)
3385 (if (eq (breakpoint-kind breakpoint) :function-end)
3386 (let ((starter (breakpoint-start-helper breakpoint)))
3387 (unless (find-if #'(lambda (bpt)
3388 (and (not (eq bpt breakpoint))
3389 (eq (breakpoint-status bpt) :active)))
3390 (breakpoint-%info starter))
3391 (deactivate-compiled-breakpoint starter)))
3392 (let* ((data (breakpoint-internal-data breakpoint))
3393 (bpts (delete breakpoint (breakpoint-data-breakpoints data))))
3394 (setf (breakpoint-internal-data breakpoint) nil)
3395 (setf (breakpoint-data-breakpoints data) bpts)
3396 (unless bpts
3397 (without-gcing
3398 (breakpoint-remove (get-lisp-obj-address
3399 (breakpoint-data-component data))
3400 (breakpoint-data-offset data)
3401 (breakpoint-data-instruction data)))
3402 (delete-breakpoint-data data))))
3403 (setf (breakpoint-status breakpoint) :inactive)
3404 breakpoint)
3406 ;;;; BREAKPOINT-INFO
3408 (defun breakpoint-info (breakpoint)
3409 #!+sb-doc
3410 "This returns the user-maintained info associated with breakpoint. This
3411 is SETF'able."
3412 (breakpoint-%info breakpoint))
3413 (defun %set-breakpoint-info (breakpoint value)
3414 (setf (breakpoint-%info breakpoint) value)
3415 (let ((other (breakpoint-unknown-return-partner breakpoint)))
3416 (when other
3417 (setf (breakpoint-%info other) value))))
3419 ;;;; BREAKPOINT-ACTIVE-P and DELETE-BREAKPOINT
3421 (defun breakpoint-active-p (breakpoint)
3422 #!+sb-doc
3423 "This returns whether breakpoint is currently active."
3424 (ecase (breakpoint-status breakpoint)
3425 (:active t)
3426 ((:inactive :deleted) nil)))
3428 (defun delete-breakpoint (breakpoint)
3429 #!+sb-doc
3430 "This frees system storage and removes computational overhead associated with
3431 breakpoint. After calling this, breakpoint is completely impotent and can
3432 never become active again."
3433 (let ((status (breakpoint-status breakpoint)))
3434 (unless (eq status :deleted)
3435 (when (eq status :active)
3436 (deactivate-breakpoint breakpoint))
3437 (setf (breakpoint-status breakpoint) :deleted)
3438 (let ((other (breakpoint-unknown-return-partner breakpoint)))
3439 (when other
3440 (setf (breakpoint-status other) :deleted)))
3441 (when (eq (breakpoint-kind breakpoint) :function-end)
3442 (let* ((starter (breakpoint-start-helper breakpoint))
3443 (breakpoints (delete breakpoint
3444 (the list (breakpoint-info starter)))))
3445 (setf (breakpoint-info starter) breakpoints)
3446 (unless breakpoints
3447 (delete-breakpoint starter)
3448 (setf (compiled-debug-function-end-starter
3449 (breakpoint-what breakpoint))
3450 nil))))))
3451 breakpoint)
3453 ;;;; C call out stubs
3455 ;;; This actually installs the break instruction in the component. It
3456 ;;; returns the overwritten bits. You must call this in a context in
3457 ;;; which GC is disabled, so that Lisp doesn't move objects around
3458 ;;; that C is pointing to.
3459 (sb!alien:def-alien-routine "breakpoint_install" sb!c-call:unsigned-long
3460 (code-obj sb!c-call:unsigned-long)
3461 (pc-offset sb!c-call:int))
3463 ;;; This removes the break instruction and replaces the original
3464 ;;; instruction. You must call this in a context in which GC is disabled
3465 ;;; so Lisp doesn't move objects around that C is pointing to.
3466 (sb!alien:def-alien-routine "breakpoint_remove" sb!c-call:void
3467 (code-obj sb!c-call:unsigned-long)
3468 (pc-offset sb!c-call:int)
3469 (old-inst sb!c-call:unsigned-long))
3471 (sb!alien:def-alien-routine "breakpoint_do_displaced_inst" sb!c-call:void
3472 (scp (* os-context-t))
3473 (orig-inst sb!c-call:unsigned-long))
3475 ;;;; breakpoint handlers (layer between C and exported interface)
3477 ;;; This maps components to a mapping of offsets to breakpoint-datas.
3478 (defvar *component-breakpoint-offsets* (make-hash-table :test 'eq))
3480 ;;; This returns the breakpoint-data associated with component cross
3481 ;;; offset. If none exists, this makes one, installs it, and returns it.
3482 (defun breakpoint-data (component offset &optional (create t))
3483 (flet ((install-breakpoint-data ()
3484 (when create
3485 (let ((data (make-breakpoint-data component offset)))
3486 (push (cons offset data)
3487 (gethash component *component-breakpoint-offsets*))
3488 data))))
3489 (let ((offsets (gethash component *component-breakpoint-offsets*)))
3490 (if offsets
3491 (let ((data (assoc offset offsets)))
3492 (if data
3493 (cdr data)
3494 (install-breakpoint-data)))
3495 (install-breakpoint-data)))))
3497 ;;; We use this when there are no longer any active breakpoints
3498 ;;; corresponding to data.
3499 (defun delete-breakpoint-data (data)
3500 (let* ((component (breakpoint-data-component data))
3501 (offsets (delete (breakpoint-data-offset data)
3502 (gethash component *component-breakpoint-offsets*)
3503 :key #'car)))
3504 (if offsets
3505 (setf (gethash component *component-breakpoint-offsets*) offsets)
3506 (remhash component *component-breakpoint-offsets*)))
3507 (values))
3509 ;;; The C handler for interrupts calls this when it has a
3510 ;;; debugging-tool break instruction. This does NOT handle all breaks;
3511 ;;; for example, it does not handle breaks for internal errors.
3512 (defun handle-breakpoint (offset component signal-context)
3513 (/show0 "entering HANDLE-BREAKPOINT")
3514 (let ((data (breakpoint-data component offset nil)))
3515 (unless data
3516 (error "unknown breakpoint in ~S at offset ~S"
3517 (debug-function-name (debug-function-from-pc component offset))
3518 offset))
3519 (let ((breakpoints (breakpoint-data-breakpoints data)))
3520 (if (or (null breakpoints)
3521 (eq (breakpoint-kind (car breakpoints)) :function-end))
3522 (handle-function-end-breakpoint-aux breakpoints data signal-context)
3523 (handle-breakpoint-aux breakpoints data
3524 offset component signal-context)))))
3526 ;;; This holds breakpoint-datas while invoking the breakpoint hooks
3527 ;;; associated with that particular component and location. While they
3528 ;;; are executing, if we hit the location again, we ignore the
3529 ;;; breakpoint to avoid infinite recursion. Function-end breakpoints
3530 ;;; must work differently since the breakpoint-data is unique for each
3531 ;;; invocation.
3532 (defvar *executing-breakpoint-hooks* nil)
3534 ;;; This handles code-location and debug-function :FUNCTION-START
3535 ;;; breakpoints.
3536 (defun handle-breakpoint-aux (breakpoints data offset component signal-context)
3537 (/show0 "entering HANDLE-BREAKPOINT-AUX")
3538 (unless breakpoints
3539 (error "internal error: breakpoint that nobody wants"))
3540 (unless (member data *executing-breakpoint-hooks*)
3541 (let ((*executing-breakpoint-hooks* (cons data
3542 *executing-breakpoint-hooks*)))
3543 (invoke-breakpoint-hooks breakpoints component offset)))
3544 ;; At this point breakpoints may not hold the same list as
3545 ;; BREAKPOINT-DATA-BREAKPOINTS since invoking hooks may have allowed
3546 ;; a breakpoint deactivation. In fact, if all breakpoints were
3547 ;; deactivated then data is invalid since it was deleted and so the
3548 ;; correct one must be looked up if it is to be used. If there are
3549 ;; no more breakpoints active at this location, then the normal
3550 ;; instruction has been put back, and we do not need to
3551 ;; DO-DISPLACED-INST.
3552 (let ((data (breakpoint-data component offset nil)))
3553 (when (and data (breakpoint-data-breakpoints data))
3554 ;; The breakpoint is still active, so we need to execute the
3555 ;; displaced instruction and leave the breakpoint instruction
3556 ;; behind. The best way to do this is different on each machine,
3557 ;; so we just leave it up to the C code.
3558 (breakpoint-do-displaced-inst signal-context
3559 (breakpoint-data-instruction data))
3560 ; Under HPUX we can't sigreturn so bp-do-disp-i has to return.
3561 #!-(or hpux irix x86)
3562 (error "BREAKPOINT-DO-DISPLACED-INST returned?"))))
3564 (defun invoke-breakpoint-hooks (breakpoints component offset)
3565 (let* ((debug-fun (debug-function-from-pc component offset))
3566 (frame (do ((f (top-frame) (frame-down f)))
3567 ((eq debug-fun (frame-debug-function f)) f))))
3568 (dolist (bpt breakpoints)
3569 (funcall (breakpoint-hook-function bpt)
3570 frame
3571 ;; If this is an :UNKNOWN-RETURN-PARTNER, then pass the
3572 ;; hook function the original breakpoint, so that users
3573 ;; aren't forced to confront the fact that some
3574 ;; breakpoints really are two.
3575 (if (eq (breakpoint-kind bpt) :unknown-return-partner)
3576 (breakpoint-unknown-return-partner bpt)
3577 bpt)))))
3579 (defun handle-function-end-breakpoint (offset component context)
3580 (/show0 "entering HANDLE-FUNCTION-END-BREAKPOINT")
3581 (let ((data (breakpoint-data component offset nil)))
3582 (unless data
3583 (error "unknown breakpoint in ~S at offset ~S"
3584 (debug-function-name (debug-function-from-pc component offset))
3585 offset))
3586 (let ((breakpoints (breakpoint-data-breakpoints data)))
3587 (when breakpoints
3588 (assert (eq (breakpoint-kind (car breakpoints)) :function-end))
3589 (handle-function-end-breakpoint-aux breakpoints data context)))))
3591 ;;; Either HANDLE-BREAKPOINT calls this for :FUNCTION-END breakpoints
3592 ;;; [old C code] or HANDLE-FUNCTION-END-BREAKPOINT calls this directly
3593 ;;; [new C code].
3594 (defun handle-function-end-breakpoint-aux (breakpoints data signal-context)
3595 (/show0 "entering HANDLE-FUNCTION-END-BREAKPOINT-AUX")
3596 (delete-breakpoint-data data)
3597 (let* ((scp
3598 (locally
3599 (declare (optimize (inhibit-warnings 3)))
3600 (sb!alien:sap-alien signal-context (* os-context-t))))
3601 (frame (do ((cfp (sb!vm:context-register scp sb!vm::cfp-offset))
3602 (f (top-frame) (frame-down f)))
3603 ((= cfp (sap-int (frame-pointer f))) f)
3604 (declare (type (unsigned-byte #.sb!vm:word-bits) cfp))))
3605 (component (breakpoint-data-component data))
3606 (cookie (gethash component *function-end-cookies*)))
3607 (remhash component *function-end-cookies*)
3608 (dolist (bpt breakpoints)
3609 (funcall (breakpoint-hook-function bpt)
3610 frame bpt
3611 (get-function-end-breakpoint-values scp)
3612 cookie))))
3614 (defun get-function-end-breakpoint-values (scp)
3615 (let ((ocfp (int-sap (sb!vm:context-register
3617 #!-x86 sb!vm::ocfp-offset
3618 #!+x86 sb!vm::ebx-offset)))
3619 (nargs (make-lisp-obj
3620 (sb!vm:context-register scp sb!vm::nargs-offset)))
3621 (reg-arg-offsets '#.sb!vm::register-arg-offsets)
3622 (results nil))
3623 (without-gcing
3624 (dotimes (arg-num nargs)
3625 (push (if reg-arg-offsets
3626 (make-lisp-obj
3627 (sb!vm:context-register scp (pop reg-arg-offsets)))
3628 (stack-ref ocfp arg-num))
3629 results)))
3630 (nreverse results)))
3632 ;;;; MAKE-BOGUS-LRA (used for :function-end breakpoints)
3634 (defconstant
3635 bogus-lra-constants
3636 #!-x86 2 #!+x86 3)
3637 (defconstant
3638 known-return-p-slot
3639 (+ sb!vm:code-constants-offset #!-x86 1 #!+x86 2))
3641 ;;; FIXME: This is also defined in debug-vm.lisp. Which definition
3642 ;;; takes precedence? (One definition uses ALLOCATE-CODE-OBJECT, and
3643 ;;; the other has been hacked for X86 GENCGC to use
3644 ;;; ALLOCATE-DYNAMIC-CODE-OBJECT..)
3645 (defun make-bogus-lra (real-lra &optional known-return-p)
3646 #!+sb-doc
3647 "Make a bogus LRA object that signals a breakpoint trap when returned to. If
3648 the breakpoint trap handler returns, REAL-LRA is returned to. Three values
3649 are returned: the bogus LRA object, the code component it is part of, and
3650 the PC offset for the trap instruction."
3651 (without-gcing
3652 (let* ((src-start (foreign-symbol-address "function_end_breakpoint_guts"))
3653 (src-end (foreign-symbol-address "function_end_breakpoint_end"))
3654 (trap-loc (foreign-symbol-address "function_end_breakpoint_trap"))
3655 (length (sap- src-end src-start))
3656 (code-object
3657 (%primitive
3658 #!-(and x86 gencgc) sb!c:allocate-code-object
3659 #!+(and x86 gencgc) sb!c::allocate-dynamic-code-object
3660 (1+ bogus-lra-constants)
3661 length))
3662 (dst-start (code-instructions code-object)))
3663 (declare (type system-area-pointer
3664 src-start src-end dst-start trap-loc)
3665 (type index length))
3666 (setf (%code-debug-info code-object) :bogus-lra)
3667 (setf (code-header-ref code-object sb!vm:code-trace-table-offset-slot)
3668 length)
3669 #!-x86
3670 (setf (code-header-ref code-object real-lra-slot) real-lra)
3671 #!+x86
3672 (multiple-value-bind (offset code) (compute-lra-data-from-pc real-lra)
3673 (setf (code-header-ref code-object real-lra-slot) code)
3674 (setf (code-header-ref code-object (1+ real-lra-slot)) offset))
3675 (setf (code-header-ref code-object known-return-p-slot)
3676 known-return-p)
3677 (system-area-copy src-start 0 dst-start 0 (* length sb!vm:byte-bits))
3678 (sb!vm:sanctify-for-execution code-object)
3679 #!+x86
3680 (values dst-start code-object (sap- trap-loc src-start))
3681 #!-x86
3682 (let ((new-lra (make-lisp-obj (+ (sap-int dst-start)
3683 sb!vm:other-pointer-type))))
3684 (set-header-data
3685 new-lra
3686 (logandc2 (+ sb!vm:code-constants-offset bogus-lra-constants 1)
3688 (sb!vm:sanctify-for-execution code-object)
3689 (values new-lra code-object (sap- trap-loc src-start))))))
3691 ;;;; miscellaneous
3693 ;;; This appears here because it cannot go with the debug-function
3694 ;;; interface since DO-DEBUG-BLOCK-LOCATIONS isn't defined until after
3695 ;;; the debug-function routines.
3697 (defun debug-function-start-location (debug-fun)
3698 #!+sb-doc
3699 "This returns a code-location before the body of a function and after all
3700 the arguments are in place. If this cannot determine that location due to
3701 a lack of debug information, it returns nil."
3702 (etypecase debug-fun
3703 (compiled-debug-function
3704 (code-location-from-pc debug-fun
3705 (sb!c::compiled-debug-function-start-pc
3706 (compiled-debug-function-compiler-debug-fun
3707 debug-fun))
3708 nil))
3709 (interpreted-debug-function
3710 ;; Return the first location if there are any, otherwise NIL.
3711 (handler-case (do-debug-function-blocks (block debug-fun nil)
3712 (do-debug-block-locations (loc block nil)
3713 (return-from debug-function-start-location loc)))
3714 (no-debug-blocks (condx)
3715 (declare (ignore condx))
3716 nil)))))
3718 (defun print-code-locations (function)
3719 (let ((debug-fun (function-debug-function function)))
3720 (do-debug-function-blocks (block debug-fun)
3721 (do-debug-block-locations (loc block)
3722 (fill-in-code-location loc)
3723 (format t "~S code location at ~D"
3724 (compiled-code-location-kind loc)
3725 (compiled-code-location-pc loc))
3726 (sb!debug::print-code-location-source-form loc 0)
3727 (terpri)))))