1 ;;;; disassembler-related stuff not needed in cross-compilation host
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!DISASSEM")
14 ;;;; FIXME: A lot of stupid package prefixes would go away if DISASSEM
15 ;;;; would use the SB!DI package. And some more would go away if it would
16 ;;;; use SB!SYS (in order to get to the SAP-FOO operators).
18 ;;;; combining instructions where one specializes another
20 ;;; Return non-NIL if the instruction SPECIAL is a more specific
21 ;;; version of GENERAL (i.e., the same instruction, but with more
23 (defun inst-specializes-p (special general)
24 (declare (type instruction special general))
25 (let ((smask (inst-mask special))
26 (gmask (inst-mask general)))
27 (and (dchunk= (inst-id general)
28 (dchunk-and (inst-id special) gmask))
29 (dchunk-strict-superset-p smask gmask))))
31 ;;; a bit arbitrary, but should work ok...
33 ;;; Return an integer corresponding to the specificity of the
35 (defun specializer-rank (inst)
36 (declare (type instruction inst))
37 (* (dchunk-count-bits (inst-mask inst)) 4))
39 ;;; Order the list of instructions INSTS with more specific (more
40 ;;; constant bits, or same-as argument constains) ones first. Returns
42 (defun order-specializers (insts)
43 (declare (type list insts))
44 (sort insts #'> :key #'specializer-rank))
46 (defun specialization-error (insts)
47 (error "~@<internal disassembler error: ~2I~_Instructions either aren't related or conflict in some way: ~4I~_~S~:>"
50 ;;; Given a list of instructions INSTS, Sees if one of these instructions is a
51 ;;; more general form of all the others, in which case they are put into its
52 ;;; specializers list, and it is returned. Otherwise an error is signaled.
53 (defun try-specializing (insts)
54 (declare (type list insts))
55 (let ((masters (copy-list insts)))
56 (dolist (possible-master insts)
57 (dolist (possible-specializer insts)
58 (unless (or (eq possible-specializer possible-master)
59 (inst-specializes-p possible-specializer possible-master))
60 (setf masters (delete possible-master masters))
61 (return) ; exit the inner loop
64 (specialization-error insts))
66 (error "multiple specializing masters: ~S" masters))
68 (let ((master (car masters)))
69 (setf (inst-specializers master)
70 (order-specializers (remove master insts)))
73 ;;;; choosing an instruction
75 #!-sb-fluid (declaim (inline inst-matches-p choose-inst-specialization))
77 ;;; Return non-NIL if all constant-bits in INST match CHUNK.
78 (defun inst-matches-p (inst chunk)
79 (declare (type instruction inst)
81 (dchunk= (dchunk-and (inst-mask inst) chunk) (inst-id inst)))
83 ;;; Given an instruction object, INST, and a bit-pattern, CHUNK, pick
84 ;;; the most specific instruction on INST's specializer list whose
85 ;;; constraints are met by CHUNK. If none do, then return INST.
86 (defun choose-inst-specialization (inst chunk)
87 (declare (type instruction inst)
89 (or (dolist (spec (inst-specializers inst) nil)
90 (declare (type instruction spec))
91 (when (inst-matches-p spec chunk)
95 ;;;; searching for an instruction in instruction space
97 ;;; Return the instruction object within INST-SPACE corresponding to the
98 ;;; bit-pattern CHUNK, or NIL if there isn't one.
99 (defun find-inst (chunk inst-space)
100 (declare (type dchunk chunk)
101 (type (or null inst-space instruction) inst-space))
102 (etypecase inst-space
105 (if (inst-matches-p inst-space chunk)
106 (choose-inst-specialization inst-space chunk)
109 (let* ((mask (ispace-valid-mask inst-space))
110 (id (dchunk-and mask chunk)))
111 (declare (type dchunk id mask))
112 (dolist (choice (ispace-choices inst-space))
113 (declare (type inst-space-choice choice))
114 (when (dchunk= id (ischoice-common-id choice))
115 (return (find-inst chunk (ischoice-subspace choice)))))))))
117 ;;;; building the instruction space
119 ;;; Returns an instruction-space object corresponding to the list of
120 ;;; instructions INSTS. If the optional parameter INITIAL-MASK is
121 ;;; supplied, only bits it has set are used.
122 (defun build-inst-space (insts &optional (initial-mask dchunk-one))
123 ;; This is done by finding any set of bits that's common to
124 ;; all instructions, building an instruction-space node that selects on those
125 ;; bits, and recursively handle sets of instructions with a common value for
126 ;; these bits (which, since there should be fewer instructions than in INSTS,
127 ;; should have some additional set of bits to select on, etc). If there
128 ;; are no common bits, or all instructions have the same value within those
129 ;; bits, TRY-SPECIALIZING is called, which handles the cases of many
130 ;; variations on a single instruction.
131 (declare (type list insts)
132 (type dchunk initial-mask))
138 (let ((vmask (dchunk-copy initial-mask)))
140 (dchunk-andf vmask (inst-mask inst)))
141 (if (dchunk-zerop vmask)
142 (try-specializing insts)
145 (let* ((common-id (dchunk-and (inst-id inst) vmask))
146 (bucket (assoc common-id buckets :test #'dchunk=)))
148 (push (list common-id inst) buckets))
150 (push inst (cdr bucket))))))
151 (let ((submask (dchunk-clear initial-mask vmask)))
152 (if (= (length buckets) 1)
153 (try-specializing insts)
156 :choices (mapcar (lambda (bucket)
157 (make-inst-space-choice
158 :subspace (build-inst-space
161 :common-id (car bucket)))
164 ;;;; an inst-space printer for debugging purposes
166 (defun print-masked-binary (num mask word-size &optional (show word-size))
167 (do ((bit (1- word-size) (1- bit)))
169 (write-char (cond ((logbitp bit mask)
170 (if (logbitp bit num) #\1 #\0))
174 (defun print-inst-bits (inst)
175 (print-masked-binary (inst-id inst)
178 (bytes-to-bits (inst-length inst))))
180 ;;; Print a nicely-formatted version of INST-SPACE.
181 (defun print-inst-space (inst-space &optional (indent 0))
182 (etypecase inst-space
185 (format t "~Vt[~A(~A)~40T" indent
186 (inst-name inst-space)
187 (inst-format-name inst-space))
188 (print-inst-bits inst-space)
189 (dolist (inst (inst-specializers inst-space))
190 (format t "~%~Vt:~A~40T" indent (inst-name inst))
191 (print-inst-bits inst))
195 (format t "~Vt---- ~8,'0X ----~%"
197 (ispace-valid-mask inst-space))
200 (format t "~Vt~8,'0X ==>~%"
202 (ischoice-common-id choice))
203 (print-inst-space (ischoice-subspace choice)
205 (ispace-choices inst-space)))))
207 ;;;; (The actual disassembly part follows.)
209 ;;; Code object layout:
211 ;;; code-size (starting from first inst, in words)
212 ;;; entry-points (points to first function header)
214 ;;; trace-table-offset (starting from first inst, in bytes)
218 ;;; <padding to dual-word boundary>
219 ;;; start of instructions
221 ;;; function-headers and lra's buried in here randomly
223 ;;; start of trace-table
224 ;;; <padding to dual-word boundary>
226 ;;; Function header layout (dual word aligned):
229 ;;; next pointer (next function header)
234 ;;; LRA layout (dual word aligned):
237 #!-sb-fluid (declaim (inline words-to-bytes bytes-to-words))
239 (eval-when (:compile-toplevel :load-toplevel :execute)
240 ;;; Convert a word-offset NUM to a byte-offset.
241 (defun words-to-bytes (num)
242 (declare (type offset num))
243 (ash num sb!vm:word-shift))
246 ;;; Convert a byte-offset NUM to a word-offset.
247 (defun bytes-to-words (num)
248 (declare (type offset num))
249 (ash num (- sb!vm:word-shift)))
251 (defconstant lra-size (words-to-bytes 1))
253 (defstruct (offs-hook (:copier nil))
254 (offset 0 :type offset)
255 (function (required-argument) :type function)
256 (before-address nil :type (member t nil)))
258 (defstruct (segment (:conc-name seg-)
259 (:constructor %make-segment)
261 (sap-maker (required-argument)
262 :type (function () sb!sys:system-area-pointer))
263 (length 0 :type length)
264 (virtual-location 0 :type address)
265 (storage-info nil :type (or null storage-info))
266 (code nil :type (or null sb!kernel:code-component))
267 (hooks nil :type list))
268 (def!method print-object ((seg segment) stream)
269 (print-unreadable-object (seg stream :type t)
270 (let ((addr (sb!sys:sap-int (funcall (seg-sap-maker seg)))))
271 (format stream "#X~X[~D]~:[ (#X~X)~;~*~]~@[ in ~S~]"
274 (= (seg-virtual-location seg) addr)
275 (seg-virtual-location seg)
278 ;;; All state during disassembly. We store some seemingly redundant
279 ;;; information so that we can allow garbage collect during disassembly and
280 ;;; not get tripped up by a code block being moved...
281 (defstruct (disassem-state (:conc-name dstate-)
282 (:constructor %make-dstate)
284 (cur-offs 0 :type offset) ; offset of current pos in segment
285 (next-offs 0 :type offset) ; offset of next position
287 (segment-sap (required-argument) :type sb!sys:system-area-pointer)
288 ; a sap pointing to our segment
289 (segment nil :type (or null segment)) ; the current segment
291 (alignment sb!vm:word-bytes :type alignment) ; what to align to in most cases
292 (byte-order :little-endian
293 :type (member :big-endian :little-endian))
295 (properties nil :type list) ; for user code to hang stuff off of
296 (filtered-values (make-array max-filtered-value-index)
297 :type filtered-value-vector)
299 (addr-print-len nil :type ; used for prettifying printing
300 (or null (integer 0 20)))
301 (argument-column 0 :type column)
302 (output-state :beginning ; to make output look nicer
303 :type (member :beginning
307 (labels nil :type list) ; alist of (address . label-number)
308 (label-hash (make-hash-table) ; same thing in a different form
311 (fun-hooks nil :type list) ; list of function
313 ;; these next two are popped as they are used
314 (cur-labels nil :type list) ; alist of (address . label-number)
315 (cur-offs-hooks nil :type list) ; list of offs-hook
317 (notes nil :type list) ; for the current location
319 (current-valid-locations nil ; currently active source variables
320 :type (or null (vector bit))))
321 (def!method print-object ((dstate disassem-state) stream)
322 (print-unreadable-object (dstate stream :type t)
325 (dstate-cur-offs dstate)
326 (dstate-segment dstate))))
328 ;;; Return the absolute address of the current instruction in DSTATE.
329 (defun dstate-cur-addr (dstate)
330 (the address (+ (seg-virtual-location (dstate-segment dstate))
331 (dstate-cur-offs dstate))))
333 ;;; Return the absolute address of the next instruction in DSTATE.
334 (defun dstate-next-addr (dstate)
335 (the address (+ (seg-virtual-location (dstate-segment dstate))
336 (dstate-next-offs dstate))))
340 (defun fun-self (fun)
341 (declare (type compiled-function fun))
342 (sb!kernel:%function-self fun))
344 (defun fun-code (fun)
345 (declare (type compiled-function fun))
346 (sb!kernel:function-code-header (fun-self fun)))
348 (defun fun-next (fun)
349 (declare (type compiled-function fun))
350 (sb!kernel:%function-next fun))
352 (defun fun-address (function)
353 (declare (type compiled-function function))
354 (- (sb!kernel:get-lisp-obj-address function) sb!vm:function-pointer-type))
356 ;;; the offset of FUNCTION from the start of its code-component's
358 (defun fun-insts-offset (function)
359 (declare (type compiled-function function))
360 (- (fun-address function)
361 (sb!sys:sap-int (sb!kernel:code-instructions (fun-code function)))))
363 ;;; the offset of FUNCTION from the start of its code-component
364 (defun fun-offset (function)
365 (declare (type compiled-function function))
366 (words-to-bytes (sb!kernel:get-closure-length function)))
368 ;;;; operations on code-components (which hold the instructions for
369 ;;;; one or more functions)
371 ;;; Return the length of the instruction area in CODE-COMPONENT.
372 (defun code-inst-area-length (code-component)
373 (declare (type sb!kernel:code-component code-component))
374 (sb!kernel:code-header-ref code-component
375 sb!vm:code-trace-table-offset-slot))
377 ;;; Return the address of the instruction area in CODE-COMPONENT.
378 (defun code-inst-area-address (code-component)
379 (declare (type sb!kernel:code-component code-component))
380 (sb!sys:sap-int (sb!kernel:code-instructions code-component)))
382 ;;; Return the first function in CODE-COMPONENT.
383 (defun code-first-function (code-component)
384 (declare (type sb!kernel:code-component code-component))
385 (sb!kernel:code-header-ref code-component
386 sb!vm:code-trace-table-offset-slot))
388 (defun segment-offs-to-code-offs (offset segment)
389 (sb!sys:without-gcing
390 (let* ((seg-base-addr (sb!sys:sap-int (funcall (seg-sap-maker segment))))
392 (logandc1 sb!vm:lowtag-mask
393 (sb!kernel:get-lisp-obj-address (seg-code segment))))
394 (addr (+ offset seg-base-addr)))
395 (declare (type address seg-base-addr code-addr addr))
396 (- addr code-addr))))
398 (defun code-offs-to-segment-offs (offset segment)
399 (sb!sys:without-gcing
400 (let* ((seg-base-addr (sb!sys:sap-int (funcall (seg-sap-maker segment))))
402 (logandc1 sb!vm:lowtag-mask
403 (sb!kernel:get-lisp-obj-address (seg-code segment))))
404 (addr (+ offset code-addr)))
405 (declare (type address seg-base-addr code-addr addr))
406 (- addr seg-base-addr))))
408 (defun code-insts-offs-to-segment-offs (offset segment)
409 (sb!sys:without-gcing
410 (let* ((seg-base-addr (sb!sys:sap-int (funcall (seg-sap-maker segment))))
412 (sb!sys:sap-int (sb!kernel:code-instructions (seg-code segment))))
413 (addr (+ offset code-insts-addr)))
414 (declare (type address seg-base-addr code-insts-addr addr))
415 (- addr seg-base-addr))))
417 (defun lra-hook (chunk stream dstate)
418 (declare (type dchunk chunk)
420 (type (or null stream) stream)
421 (type disassem-state dstate))
422 (when (and (aligned-p (+ (seg-virtual-location (dstate-segment dstate))
423 (dstate-cur-offs dstate))
424 (* 2 sb!vm:word-bytes))
426 (= (sb!sys:sap-ref-8 (dstate-segment-sap dstate)
427 (if (eq (dstate-byte-order dstate)
429 (dstate-cur-offs dstate)
430 (+ (dstate-cur-offs dstate)
432 sb!vm:return-pc-header-type))
433 (unless (null stream)
434 (princ '.lra stream))
435 (incf (dstate-next-offs dstate) lra-size))
438 ;;; Print the function-header (entry-point) pseudo-instruction at the
439 ;;; current location in DSTATE to STREAM.
440 (defun fun-header-hook (stream dstate)
441 (declare (type (or null stream) stream)
442 (type disassem-state dstate))
443 (unless (null stream)
444 (let* ((seg (dstate-segment dstate))
445 (code (seg-code seg))
448 (segment-offs-to-code-offs (dstate-cur-offs dstate) seg)))
450 (sb!kernel:code-header-ref code
451 (+ woffs sb!vm:function-name-slot)))
453 (sb!kernel:code-header-ref code
454 (+ woffs sb!vm:function-arglist-slot)))
456 (sb!kernel:code-header-ref code
457 (+ woffs sb!vm:function-type-slot))))
458 (format stream ".~A ~S~:A" 'entry name args)
459 (note (lambda (stream)
460 (format stream "~:S" type)) ; use format to print NIL as ()
462 (incf (dstate-next-offs dstate)
463 (words-to-bytes sb!vm:function-code-offset)))
465 (defun alignment-hook (chunk stream dstate)
466 (declare (type dchunk chunk)
468 (type (or null stream) stream)
469 (type disassem-state dstate))
471 (+ (seg-virtual-location (dstate-segment dstate))
472 (dstate-cur-offs dstate)))
473 (alignment (dstate-alignment dstate)))
474 (unless (aligned-p location alignment)
476 (format stream "~A~Vt~D~%" '.align
477 (dstate-argument-column dstate)
479 (incf(dstate-next-offs dstate)
480 (- (align location alignment) location)))
483 (defun rewind-current-segment (dstate segment)
484 (declare (type disassem-state dstate)
485 (type segment segment))
486 (setf (dstate-segment dstate) segment)
487 (setf (dstate-cur-offs-hooks dstate)
488 (stable-sort (nreverse (copy-list (seg-hooks segment)))
490 (or (< (offs-hook-offset oh1) (offs-hook-offset oh2))
491 (and (= (offs-hook-offset oh1)
492 (offs-hook-offset oh2))
493 (offs-hook-before-address oh1)
494 (not (offs-hook-before-address oh2)))))))
495 (setf (dstate-cur-offs dstate) 0)
496 (setf (dstate-cur-labels dstate) (dstate-labels dstate)))
498 (defun do-offs-hooks (before-address stream dstate)
499 (declare (type (or null stream) stream)
500 (type disassem-state dstate))
501 (let ((cur-offs (dstate-cur-offs dstate)))
502 (setf (dstate-next-offs dstate) cur-offs)
504 (let ((next-hook (car (dstate-cur-offs-hooks dstate))))
505 (when (null next-hook)
507 (let ((hook-offs (offs-hook-offset next-hook)))
508 (when (or (> hook-offs cur-offs)
509 (and (= hook-offs cur-offs)
511 (not (offs-hook-before-address next-hook))))
513 (unless (< hook-offs cur-offs)
514 (funcall (offs-hook-function next-hook) stream dstate))
515 (pop (dstate-cur-offs-hooks dstate))
516 (unless (= (dstate-next-offs dstate) cur-offs)
519 (defun do-fun-hooks (chunk stream dstate)
520 (let ((hooks (dstate-fun-hooks dstate))
521 (cur-offs (dstate-cur-offs dstate)))
522 (setf (dstate-next-offs dstate) cur-offs)
523 (dolist (hook hooks nil)
524 (let ((prefix-p (funcall hook chunk stream dstate)))
525 (unless (= (dstate-next-offs dstate) cur-offs)
526 (return prefix-p))))))
528 (defun handle-bogus-instruction (stream dstate)
529 (let ((alignment (dstate-alignment dstate)))
530 (unless (null stream)
531 (multiple-value-bind (words bytes)
532 (truncate alignment sb!vm:word-bytes)
534 (print-words words stream dstate))
536 (print-bytes bytes stream dstate))))
537 (incf (dstate-next-offs dstate) alignment)))
539 ;;; Iterate through the instructions in SEGMENT, calling FUNCTION for
540 ;;; each instruction, with arguments of CHUNK, STREAM, and DSTATE.
541 (defun map-segment-instructions (function segment dstate &optional stream)
542 (declare (type function function)
543 (type segment segment)
544 (type disassem-state dstate)
545 (type (or null stream) stream))
547 (let ((ispace (get-inst-space))
548 (prefix-p nil)) ; just processed a prefix inst
550 (rewind-current-segment dstate segment)
553 (when (>= (dstate-cur-offs dstate)
554 (seg-length (dstate-segment dstate)))
558 (setf (dstate-next-offs dstate) (dstate-cur-offs dstate))
560 (do-offs-hooks t stream dstate)
561 (unless (or prefix-p (null stream))
562 (print-current-address stream dstate))
563 (do-offs-hooks nil stream dstate)
565 (unless (> (dstate-next-offs dstate) (dstate-cur-offs dstate))
566 (sb!sys:without-gcing
567 (setf (dstate-segment-sap dstate) (funcall (seg-sap-maker segment)))
570 (sap-ref-dchunk (dstate-segment-sap dstate)
571 (dstate-cur-offs dstate)
572 (dstate-byte-order dstate))))
573 (let ((fun-prefix-p (do-fun-hooks chunk stream dstate)))
574 (if (> (dstate-next-offs dstate) (dstate-cur-offs dstate))
575 (setf prefix-p fun-prefix-p)
576 (let ((inst (find-inst chunk ispace)))
578 (handle-bogus-instruction stream dstate))
580 (setf (dstate-next-offs dstate)
581 (+ (dstate-cur-offs dstate)
584 (let ((prefilter (inst-prefilter inst))
585 (control (inst-control inst)))
587 (funcall prefilter chunk dstate))
589 (funcall function chunk inst)
591 (setf prefix-p (null (inst-printer inst)))
594 (funcall control chunk inst stream dstate))))))
597 (setf (dstate-cur-offs dstate) (dstate-next-offs dstate))
599 (unless (null stream)
601 (print-notes-and-newline stream dstate))
602 (setf (dstate-output-state dstate) nil)))))
604 ;;; Make an initial non-printing disassembly pass through DSTATE,
605 ;;; noting any addresses that are referenced by instructions in this
607 (defun add-segment-labels (segment dstate)
608 ;; add labels at the beginning with a label-number of nil; we'll notice
609 ;; later and fill them in (and sort them)
610 (declare (type disassem-state dstate))
611 (let ((labels (dstate-labels dstate)))
612 (map-segment-instructions
614 (declare (type dchunk chunk) (type instruction inst))
615 (let ((labeller (inst-labeller inst)))
617 (setf labels (funcall labeller chunk labels dstate)))))
620 (setf (dstate-labels dstate) labels)
621 ;; erase any notes that got there by accident
622 (setf (dstate-notes dstate) nil)))
624 ;;; If any labels in DSTATE have been added since the last call to
625 ;;; this function, give them label-numbers, enter them in the
626 ;;; hash-table, and make sure the label list is in sorted order.
627 (defun number-labels (dstate)
628 (let ((labels (dstate-labels dstate)))
629 (when (and labels (null (cdar labels)))
630 ;; at least one label left un-numbered
631 (setf labels (sort labels #'< :key #'car))
633 (label-hash (dstate-label-hash dstate)))
634 (dolist (label labels)
635 (when (not (null (cdr label)))
636 (setf max (max max (cdr label)))))
637 (dolist (label labels)
638 (when (null (cdr label))
640 (setf (cdr label) max)
641 (setf (gethash (car label) label-hash)
642 (format nil "L~D" max)))))
643 (setf (dstate-labels dstate) labels))))
645 ;;; Get the instruction-space, creating it if necessary.
646 (defun get-inst-space ()
647 (let ((ispace *disassem-inst-space*))
650 (maphash (lambda (name inst-flavs)
651 (declare (ignore name))
652 (dolist (flav inst-flavs)
655 (setf ispace (build-inst-space insts)))
656 (setf *disassem-inst-space* ispace))
659 ;;;; Add global hooks.
661 (defun add-offs-hook (segment addr hook)
662 (let ((entry (cons addr hook)))
663 (if (null (seg-hooks segment))
664 (setf (seg-hooks segment) (list entry))
665 (push entry (cdr (last (seg-hooks segment)))))))
667 (defun add-offs-note-hook (segment addr note)
668 (add-offs-hook segment
670 (lambda (stream dstate)
671 (declare (type (or null stream) stream)
672 (type disassem-state dstate))
674 (note note dstate)))))
676 (defun add-offs-comment-hook (segment addr comment)
677 (add-offs-hook segment
679 (lambda (stream dstate)
680 (declare (type (or null stream) stream)
683 (write-string ";;; " stream)
686 (write-string comment stream))
688 (funcall comment stream)))
691 (defun add-fun-hook (dstate function)
692 (push function (dstate-fun-hooks dstate)))
694 (defun set-location-printing-range (dstate from length)
695 (setf (dstate-addr-print-len dstate)
696 ;; 4 bits per hex digit
697 (ceiling (integer-length (logxor from (+ from length))) 4)))
699 ;;; Print the current address in DSTATE to STREAM, plus any labels that
700 ;;; correspond to it, and leave the cursor in the instruction column.
701 (defun print-current-address (stream dstate)
702 (declare (type stream stream)
703 (type disassem-state dstate))
705 (+ (seg-virtual-location (dstate-segment dstate))
706 (dstate-cur-offs dstate)))
707 (location-column-width *disassem-location-column-width*)
708 (plen (dstate-addr-print-len dstate)))
711 (setf plen location-column-width)
712 (let ((seg (dstate-segment dstate)))
713 (set-location-printing-range dstate
714 (seg-virtual-location seg)
716 (when (eq (dstate-output-state dstate) :beginning)
717 (setf plen location-column-width))
721 (setf location-column-width (+ 2 location-column-width))
724 ;; print the location
725 ;; [this is equivalent to (format stream "~V,'0x:" plen printed-value), but
726 ;; usually avoids any consing]
727 (tab0 (- location-column-width plen) stream)
728 (let* ((printed-bits (* 4 plen))
729 (printed-value (ldb (byte printed-bits 0) location))
731 (truncate (- printed-bits (integer-length printed-value)) 4)))
732 (dotimes (i leading-zeros)
733 (write-char #\0 stream))
734 (unless (zerop printed-value)
735 (write printed-value :stream stream :base 16 :radix nil))
736 (write-char #\: stream))
740 (let* ((next-label (car (dstate-cur-labels dstate)))
741 (label-location (car next-label)))
742 (when (or (null label-location) (> label-location location))
744 (unless (< label-location location)
745 (format stream " L~D:" (cdr next-label)))
746 (pop (dstate-cur-labels dstate))))
748 ;; move to the instruction column
749 (tab0 (+ location-column-width 1 label-column-width) stream)
752 (eval-when (:compile-toplevel :execute)
753 (sb!xc:defmacro with-print-restrictions (&rest body)
754 `(let ((*print-pretty* t)
760 ;;; Print a newline to STREAM, inserting any pending notes in DSTATE
761 ;;; as end-of-line comments. If there is more than one note, a
762 ;;; separate line will be used for each one.
763 (defun print-notes-and-newline (stream dstate)
764 (declare (type stream stream)
765 (type disassem-state dstate))
766 (with-print-restrictions
767 (dolist (note (dstate-notes dstate))
768 (format stream "~Vt " *disassem-note-column*)
769 (pprint-logical-block (stream nil :per-line-prefix "; ")
772 (write-string note stream))
774 (funcall note stream))))
777 (setf (dstate-notes dstate) nil)))
779 ;;; Disassemble NUM bytes to STREAM as simple `BYTE' instructions.
780 (defun print-bytes (num stream dstate)
781 (declare (type offset num)
783 (type disassem-state dstate))
784 (format stream "~A~Vt" 'BYTE (dstate-argument-column dstate))
785 (let ((sap (dstate-segment-sap dstate))
786 (start-offs (dstate-cur-offs dstate)))
789 (write-string ", " stream))
790 (format stream "#X~2,'0x" (sb!sys:sap-ref-8 sap (+ offs start-offs))))))
792 ;;; Disassemble NUM machine-words to STREAM as simple `WORD' instructions.
793 (defun print-words (num stream dstate)
794 (declare (type offset num)
796 (type disassem-state dstate))
797 (format stream "~A~Vt" 'WORD (dstate-argument-column dstate))
798 (let ((sap (dstate-segment-sap dstate))
799 (start-offs (dstate-cur-offs dstate))
800 (byte-order (dstate-byte-order dstate)))
801 (dotimes (word-offs num)
802 (unless (zerop word-offs)
803 (write-string ", " stream))
804 (let ((word 0) (bit-shift 0))
805 (dotimes (byte-offs sb!vm:word-bytes)
810 (* word-offs sb!vm:word-bytes)
813 (if (eq byte-order :big-endian)
814 (+ (ash word sb!vm:byte-bits) byte)
815 (+ word (ash byte bit-shift))))
816 (incf bit-shift sb!vm:byte-bits)))
817 (format stream "#X~V,'0X" (ash sb!vm:word-bits -2) word)))))
819 (defvar *default-dstate-hooks* (list #'lra-hook))
821 ;;; Make a disassembler-state object.
822 (defun make-dstate (&optional (fun-hooks *default-dstate-hooks*))
824 (sb!sys:vector-sap (coerce #() '(vector (unsigned-byte 8)))))
825 (alignment *disassem-inst-alignment-bytes*)
827 (+ (or *disassem-opcode-column-width* 0)
828 *disassem-location-column-width*
830 label-column-width)))
832 (when (> alignment 1)
833 (push #'alignment-hook fun-hooks))
835 (%make-dstate :segment-sap sap
837 :argument-column arg-column
839 :byte-order sb!c:*backend-byte-order*)))
841 (defun add-fun-header-hooks (segment)
842 (declare (type segment segment))
843 (do ((fun (sb!kernel:code-header-ref (seg-code segment)
844 sb!vm:code-entry-points-slot)
846 (length (seg-length segment)))
848 (let ((offset (code-offs-to-segment-offs (fun-offset fun) segment)))
849 (when (<= 0 offset length)
850 (push (make-offs-hook :offset offset :function #'fun-header-hook)
851 (seg-hooks segment))))))
853 ;;; A SAP-MAKER is a no-argument function that returns a SAP.
855 #!-sb-fluid (declaim (inline sap-maker))
857 (defun sap-maker (function input offset)
858 (declare (optimize (speed 3))
859 (type (function (t) sb!sys:system-area-pointer) function)
860 (type offset offset))
861 (let ((old-sap (sb!sys:sap+ (funcall function input) offset)))
862 (declare (type sb!sys:system-area-pointer old-sap))
865 (+ (sb!sys:sap-int (funcall function input)) offset)))
866 ;; Saving the sap like this avoids consing except when the sap
867 ;; changes (because the sap-int, arith, etc., get inlined).
868 (declare (type address new-addr))
869 (if (= (sb!sys:sap-int old-sap) new-addr)
871 (setf old-sap (sb!sys:int-sap new-addr)))))))
873 (defun vector-sap-maker (vector offset)
874 (declare (optimize (speed 3))
875 (type offset offset))
876 (sap-maker #'sb!sys:vector-sap vector offset))
878 (defun code-sap-maker (code offset)
879 (declare (optimize (speed 3))
880 (type sb!kernel:code-component code)
881 (type offset offset))
882 (sap-maker #'sb!kernel:code-instructions code offset))
884 (defun memory-sap-maker (address)
885 (declare (optimize (speed 3))
886 (type address address))
887 (let ((sap (sb!sys:int-sap address)))
890 ;;; Return a memory segment located at the system-area-pointer returned by
891 ;;; SAP-MAKER and LENGTH bytes long in the disassem-state object DSTATE.
893 ;;; &KEY arguments include :VIRTUAL-LOCATION (by default the same as
894 ;;; the address), :DEBUG-FUNCTION, :SOURCE-FORM-CACHE (a
895 ;;; SOURCE-FORM-CACHE object), and :HOOKS (a list of OFFS-HOOK
897 (defun make-segment (sap-maker length
899 code virtual-location
900 debug-function source-form-cache
902 (declare (type (function () sb!sys:system-area-pointer) sap-maker)
904 (type (or null address) virtual-location)
905 (type (or null sb!di:debug-function) debug-function)
906 (type (or null source-form-cache) source-form-cache))
911 :virtual-location (or virtual-location
912 (sb!sys:sap-int (funcall sap-maker)))
915 (add-debugging-hooks segment debug-function source-form-cache)
916 (add-fun-header-hooks segment)
919 (defun make-vector-segment (vector offset &rest args)
920 (declare (type vector vector)
922 (inline make-segment))
923 (apply #'make-segment (vector-sap-maker vector offset) args))
925 (defun make-code-segment (code offset length &rest args)
926 (declare (type sb!kernel:code-component code)
928 (inline make-segment))
929 (apply #'make-segment (code-sap-maker code offset) length :code code args))
931 (defun make-memory-segment (address &rest args)
932 (declare (type address address)
933 (inline make-segment))
934 (apply #'make-segment (memory-sap-maker address) args))
937 (defun print-fun-headers (function)
938 (declare (type compiled-function function))
939 (let* ((self (fun-self function))
940 (code (sb!kernel:function-code-header self)))
941 (format t "Code-header ~S: size: ~S, trace-table-offset: ~S~%"
943 (sb!kernel:code-header-ref code
944 sb!vm:code-code-size-slot)
945 (sb!kernel:code-header-ref code
946 sb!vm:code-trace-table-offset-slot))
947 (do ((fun (sb!kernel:code-header-ref code sb!vm:code-entry-points-slot)
950 (let ((fun-offset (sb!kernel:get-closure-length fun)))
951 ;; There is function header fun-offset words from the
953 (format t "Fun-header ~S at offset ~D (words): ~S~A => ~S~%"
956 (sb!kernel:code-header-ref
957 code (+ fun-offset sb!vm:function-name-slot))
958 (sb!kernel:code-header-ref
959 code (+ fun-offset sb!vm:function-arglist-slot))
960 (sb!kernel:code-header-ref
961 code (+ fun-offset sb!vm:function-type-slot)))))))
963 ;;; getting at the source code...
965 (defstruct (source-form-cache (:conc-name sfcache-)
967 (debug-source nil :type (or null sb!di:debug-source))
968 (top-level-form-index -1 :type fixnum)
969 (top-level-form nil :type list)
970 (form-number-mapping-table nil :type (or null (vector list)))
971 (last-location-retrieved nil :type (or null sb!di:code-location))
972 (last-form-retrieved -1 :type fixnum))
974 (defun get-top-level-form (debug-source tlf-index)
975 (let ((name (sb!di:debug-source-name debug-source)))
976 (ecase (sb!di:debug-source-from debug-source)
978 (cond ((not (probe-file name))
979 (warn "The source file ~S no longer seems to exist." name)
982 (let ((start-positions
983 (sb!di:debug-source-start-positions debug-source)))
984 (cond ((null start-positions)
985 (warn "There is no start positions map.")
988 (let* ((local-tlf-index
990 (sb!di:debug-source-root-number
993 (aref start-positions local-tlf-index)))
994 (with-open-file (f name)
995 (cond ((= (sb!di:debug-source-created debug-source)
996 (file-write-date name))
997 (file-position f char-offset))
999 (warn "Source file ~S has been modified; ~@
1000 using form offset instead of file index."
1002 (let ((*read-suppress* t))
1003 (dotimes (i local-tlf-index) (read f)))))
1004 (let ((*readtable* (copy-readtable)))
1005 (set-dispatch-macro-character
1007 (lambda (stream sub-char &rest rest)
1008 (declare (ignore rest sub-char))
1009 (let ((token (read stream t nil t)))
1010 (format nil "#.~S" token))))
1014 (aref name tlf-index)))))
1016 (defun cache-valid (loc cache)
1018 (and (eq (sb!di:code-location-debug-source loc)
1019 (sfcache-debug-source cache))
1020 (eq (sb!di:code-location-top-level-form-offset loc)
1021 (sfcache-top-level-form-index cache)))))
1023 (defun get-source-form (loc context &optional cache)
1024 (let* ((cache-valid (cache-valid loc cache))
1025 (tlf-index (sb!di:code-location-top-level-form-offset loc))
1026 (form-number (sb!di:code-location-form-number loc))
1029 (sfcache-top-level-form cache)
1030 (get-top-level-form (sb!di:code-location-debug-source loc)
1034 (sfcache-form-number-mapping-table cache)
1035 (sb!di:form-number-translations top-level-form tlf-index))))
1036 (when (and (not cache-valid) cache)
1037 (setf (sfcache-debug-source cache) (sb!di:code-location-debug-source loc)
1038 (sfcache-top-level-form-index cache) tlf-index
1039 (sfcache-top-level-form cache) top-level-form
1040 (sfcache-form-number-mapping-table cache) mapping-table))
1041 (cond ((null top-level-form)
1043 ((> form-number (length mapping-table))
1044 (warn "bogus form-number in form! The source file has probably ~@
1045 been changed too much to cope with.")
1047 ;; Disable future warnings.
1048 (setf (sfcache-top-level-form cache) nil))
1052 (setf (sfcache-last-location-retrieved cache) loc)
1053 (setf (sfcache-last-form-retrieved cache) form-number))
1054 (sb!di:source-path-context top-level-form
1055 (aref mapping-table form-number)
1058 (defun get-different-source-form (loc context &optional cache)
1059 (if (and (cache-valid loc cache)
1060 (or (= (sb!di:code-location-form-number loc)
1061 (sfcache-last-form-retrieved cache))
1062 (and (sfcache-last-location-retrieved cache)
1063 (sb!di:code-location=
1065 (sfcache-last-location-retrieved cache)))))
1067 (values (get-source-form loc context cache) t)))
1069 ;;;; stuff to use debugging-info to augment the disassembly
1071 (defun code-function-map (code)
1072 (declare (type sb!kernel:code-component code))
1073 (sb!di::get-debug-info-function-map (sb!kernel:%code-debug-info code)))
1075 (defstruct (location-group (:copier nil))
1076 (locations #() :type (vector (or list fixnum))))
1078 (defstruct (storage-info (:copier nil))
1079 (groups nil :type list) ; alist of (name . location-group)
1080 (debug-vars #() :type vector))
1082 ;;; Return the vector of DEBUG-VARs currently associated with DSTATE.
1083 (defun dstate-debug-vars (dstate)
1084 (declare (type disassem-state dstate))
1085 (storage-info-debug-vars (seg-storage-info (dstate-segment dstate))))
1087 ;;; Given the OFFSET of a location within the location-group called
1088 ;;; LG-NAME, see whether there's a current mapping to a source
1089 ;;; variable in DSTATE, and if so, return the offset of that variable
1090 ;;; in the current debug-var vector.
1091 (defun find-valid-storage-location (offset lg-name dstate)
1092 (declare (type offset offset)
1093 (type symbol lg-name)
1094 (type disassem-state dstate))
1095 (let* ((storage-info
1096 (seg-storage-info (dstate-segment dstate)))
1099 (cdr (assoc lg-name (storage-info-groups storage-info)))))
1101 (dstate-current-valid-locations dstate)))
1103 (not (null currently-valid))
1104 (let ((locations (location-group-locations location-group)))
1105 (and (< offset (length locations))
1106 (let ((used-by (aref locations offset)))
1108 (let ((debug-var-num
1112 (zerop (bit currently-valid used-by)))
1118 (bit currently-valid num)))
1123 ;; Found a valid storage reference!
1124 ;; can't use it again until it's revalidated...
1125 (setf (bit (dstate-current-valid-locations
1132 ;;; Return a new vector which has the same contents as the old one
1133 ;;; VEC, plus new cells (for a total size of NEW-LEN). The additional
1134 ;;; elements are initialized to INITIAL-ELEMENT.
1135 (defun grow-vector (vec new-len &optional initial-element)
1136 (declare (type vector vec)
1137 (type fixnum new-len))
1139 (make-sequence `(vector ,(array-element-type vec) ,new-len)
1141 :initial-element initial-element)))
1142 (dotimes (i (length vec))
1143 (setf (aref new i) (aref vec i)))
1146 ;;; Return a STORAGE-INFO struction describing the object-to-source
1147 ;;; variable mappings from DEBUG-FUNCTION.
1148 (defun storage-info-for-debug-function (debug-function)
1149 (declare (type sb!di:debug-function debug-function))
1150 (let ((sc-vec sb!c::*backend-sc-numbers*)
1152 (debug-vars (sb!di::debug-function-debug-vars
1155 (dotimes (debug-var-offset
1157 (make-storage-info :groups groups
1158 :debug-vars debug-vars))
1159 (let ((debug-var (aref debug-vars debug-var-offset)))
1161 (format t ";;; At offset ~D: ~S~%" debug-var-offset debug-var)
1163 (sb!di::compiled-debug-var-sc-offset debug-var))
1166 (sb!c:sc-sb (aref sc-vec
1167 (sb!c:sc-offset-scn sc-offset))))))
1169 (format t ";;; SET: ~S[~D]~%"
1170 sb-name (sb!c:sc-offset-offset sc-offset))
1171 (unless (null sb-name)
1172 (let ((group (cdr (assoc sb-name groups))))
1174 (setf group (make-location-group))
1175 (push `(,sb-name . ,group) groups))
1176 (let* ((locations (location-group-locations group))
1177 (length (length locations))
1178 (offset (sb!c:sc-offset-offset sc-offset)))
1179 (when (>= offset length)
1181 (grow-vector locations
1185 (location-group-locations group)
1187 (let ((already-there (aref locations offset)))
1188 (cond ((null already-there)
1189 (setf (aref locations offset) debug-var-offset))
1190 ((eql already-there debug-var-offset))
1192 (if (listp already-there)
1193 (pushnew debug-var-offset
1194 (aref locations offset))
1195 (setf (aref locations offset)
1196 (list debug-var-offset
1201 (defun source-available-p (debug-function)
1203 (sb!di:do-debug-function-blocks (block debug-function)
1204 (declare (ignore block))
1206 (sb!di:no-debug-blocks () nil)))
1208 (defun print-block-boundary (stream dstate)
1209 (let ((os (dstate-output-state dstate)))
1210 (when (not (eq os :beginning))
1211 (when (not (eq os :block-boundary))
1213 (setf (dstate-output-state dstate)
1216 ;;; Add hooks to track to track the source code in SEGMENT during
1217 ;;; disassembly. SFCACHE can be either NIL or it can be a
1218 ;;; SOURCE-FORM-CACHE structure, in which case it is used to cache
1219 ;;; forms from files.
1220 (defun add-source-tracking-hooks (segment debug-function &optional sfcache)
1221 (declare (type segment segment)
1222 (type (or null sb!di:debug-function) debug-function)
1223 (type (or null source-form-cache) sfcache))
1224 (let ((last-block-pc -1))
1225 (flet ((add-hook (pc fun &optional before-address)
1226 (push (make-offs-hook
1227 :offset pc ;; ##### FIX to account for non-zero offs in code
1229 :before-address before-address)
1230 (seg-hooks segment))))
1232 (sb!di:do-debug-function-blocks (block debug-function)
1233 (let ((first-location-in-block-p t))
1234 (sb!di:do-debug-block-locations (loc block)
1235 (let ((pc (sb!di::compiled-code-location-pc loc)))
1237 ;; Put blank lines in at block boundaries
1238 (when (and first-location-in-block-p
1239 (/= pc last-block-pc))
1240 (setf first-location-in-block-p nil)
1242 (lambda (stream dstate)
1243 (print-block-boundary stream dstate))
1245 (setf last-block-pc pc))
1247 ;; Print out corresponding source; this information is not
1248 ;; all that accurate, but it's better than nothing
1249 (unless (zerop (sb!di:code-location-form-number loc))
1250 (multiple-value-bind (form new)
1251 (get-different-source-form loc 0 sfcache)
1253 (let ((at-block-begin (= pc last-block-pc)))
1256 (lambda (stream dstate)
1257 (declare (ignore dstate))
1259 (unless at-block-begin
1261 (format stream ";;; [~D] "
1262 (sb!di:code-location-form-number
1264 (prin1-short form stream)
1269 ;; Keep track of variable live-ness as best we can.
1271 (copy-seq (sb!di::compiled-code-location-live-set
1275 (lambda (stream dstate)
1276 (declare (ignore stream))
1277 (setf (dstate-current-valid-locations dstate)
1280 (note (lambda (stream)
1281 (let ((*print-length* nil))
1282 (format stream "live set: ~S"
1286 (sb!di:no-debug-blocks () nil)))))
1288 (defun add-debugging-hooks (segment debug-function &optional sfcache)
1289 (when debug-function
1290 (setf (seg-storage-info segment)
1291 (storage-info-for-debug-function debug-function))
1292 (add-source-tracking-hooks segment debug-function sfcache)
1293 (let ((kind (sb!di:debug-function-kind debug-function)))
1295 (push (make-offs-hook
1297 :function (lambda (stream dstate)
1298 (declare (ignore stream))
1300 (seg-hooks segment))))
1304 (anh "no-arg-parsing entry point"))
1306 (anh (lambda (stream)
1307 (format stream "~S entry point" kind)))))))))
1309 ;;; Return a list of the segments of memory containing machine code
1310 ;;; instructions for FUNCTION.
1311 (defun get-function-segments (function)
1312 (declare (type compiled-function function))
1313 (let* ((code (fun-code function))
1314 (function-map (code-function-map code))
1315 (fname (sb!kernel:%function-name function))
1316 (sfcache (make-source-form-cache)))
1317 (let ((first-block-seen-p nil)
1318 (nil-block-seen-p nil)
1320 (last-debug-function nil)
1322 (flet ((add-seg (offs len df)
1324 (push (make-code-segment code offs len
1326 :source-form-cache sfcache)
1328 (dotimes (fmap-index (length function-map))
1329 (let ((fmap-entry (aref function-map fmap-index)))
1330 (etypecase fmap-entry
1332 (when first-block-seen-p
1333 (add-seg last-offset
1334 (- fmap-entry last-offset)
1335 last-debug-function)
1336 (setf last-debug-function nil))
1337 (setf last-offset fmap-entry))
1338 (sb!c::compiled-debug-function
1339 (let ((name (sb!c::compiled-debug-function-name fmap-entry))
1340 (kind (sb!c::compiled-debug-function-kind fmap-entry)))
1342 (format t ";;; SAW ~S ~S ~S,~S ~D,~D~%"
1343 name kind first-block-seen-p nil-block-seen-p
1345 (sb!c::compiled-debug-function-start-pc fmap-entry))
1346 (cond (#+nil (eq last-offset fun-offset)
1347 (and (equal name fname) (not first-block-seen-p))
1348 (setf first-block-seen-p t))
1349 ((eq kind :external)
1350 (when first-block-seen-p
1353 (when nil-block-seen-p
1355 (when first-block-seen-p
1356 (setf nil-block-seen-p t))))
1357 (setf last-debug-function
1358 (sb!di::make-compiled-debug-function fmap-entry code))
1360 (let ((max-offset (code-inst-area-length code)))
1361 (when (and first-block-seen-p last-debug-function)
1362 (add-seg last-offset
1363 (- max-offset last-offset)
1364 last-debug-function))
1366 (let ((offs (fun-insts-offset function)))
1367 (make-code-segment code offs (- max-offset offs)))
1368 (nreverse segments)))))))
1370 ;;; Return a list of the segments of memory containing machine code
1371 ;;; instructions for the code-component CODE. If START-OFFSET and/or
1372 ;;; LENGTH is supplied, only that part of the code-segment is used
1373 ;;; (but these are constrained to lie within the code-segment).
1374 (defun get-code-segments (code
1377 (length (code-inst-area-length code)))
1378 (declare (type sb!kernel:code-component code)
1379 (type offset start-offset)
1380 (type length length))
1381 (let ((segments nil))
1383 (let ((function-map (code-function-map code))
1384 (sfcache (make-source-form-cache)))
1385 (let ((last-offset 0)
1386 (last-debug-function nil))
1387 (flet ((add-seg (offs len df)
1388 (let* ((restricted-offs
1389 (min (max start-offset offs)
1390 (+ start-offset length)))
1392 (- (min (max start-offset (+ offs len))
1393 (+ start-offset length))
1395 (when (> restricted-len 0)
1396 (push (make-code-segment code
1397 restricted-offs restricted-len
1399 :source-form-cache sfcache)
1401 (dotimes (fmap-index (length function-map))
1402 (let ((fmap-entry (aref function-map fmap-index)))
1403 (etypecase fmap-entry
1405 (add-seg last-offset (- fmap-entry last-offset)
1406 last-debug-function)
1407 (setf last-debug-function nil)
1408 (setf last-offset fmap-entry))
1409 (sb!c::compiled-debug-function
1410 (setf last-debug-function
1411 (sb!di::make-compiled-debug-function fmap-entry
1413 (when last-debug-function
1414 (add-seg last-offset
1415 (- (code-inst-area-length code) last-offset)
1416 last-debug-function))))))
1418 (make-code-segment code start-offset length)
1419 (nreverse segments))))
1421 ;;; Return two values: the amount by which the last instruction in the
1422 ;;; segment goes past the end of the segment, and the offset of the
1423 ;;; end of the segment from the beginning of that instruction. If all
1424 ;;; instructions fit perfectly, return 0 and 0.
1425 (defun segment-overflow (segment dstate)
1426 (declare (type segment segment)
1427 (type disassem-state dstate))
1428 (let ((seglen (seg-length segment))
1430 (map-segment-instructions (lambda (chunk inst)
1431 (declare (ignore chunk inst))
1432 (setf last-start (dstate-cur-offs dstate)))
1435 (values (- (dstate-cur-offs dstate) seglen)
1436 (- seglen last-start))))
1438 ;;; Compute labels for all the memory segments in SEGLIST and adds
1439 ;;; them to DSTATE. It's important to call this function with all the
1440 ;;; segments you're interested in, so that it can find references from
1442 (defun label-segments (seglist dstate)
1443 (declare (type list seglist)
1444 (type disassem-state dstate))
1445 (dolist (seg seglist)
1446 (add-segment-labels seg dstate))
1447 ;; Now remove any labels that don't point anywhere in the segments
1449 (setf (dstate-labels dstate)
1450 (remove-if (lambda (lab)
1453 (let ((start (seg-virtual-location seg)))
1456 (+ start (seg-length seg)))))
1458 (dstate-labels dstate))))
1460 ;;; Disassemble the machine code instructions in SEGMENT to STREAM.
1461 (defun disassemble-segment (segment stream dstate)
1462 (declare (type segment segment)
1463 (type stream stream)
1464 (type disassem-state dstate))
1465 (let ((*print-pretty* nil)) ; otherwise the pp conses hugely
1466 (number-labels dstate)
1467 (map-segment-instructions
1468 (lambda (chunk inst)
1469 (declare (type dchunk chunk) (type instruction inst))
1470 (let ((printer (inst-printer inst)))
1472 (funcall printer chunk inst stream dstate))))
1477 ;;; Disassemble the machine code instructions in each memory segment
1478 ;;; in SEGMENTS in turn to STREAM.
1479 (defun disassemble-segments (segments stream dstate)
1480 (declare (type list segments)
1481 (type stream stream)
1482 (type disassem-state dstate))
1483 (unless (null segments)
1484 (let ((first (car segments))
1485 (last (car (last segments))))
1486 (set-location-printing-range dstate
1487 (seg-virtual-location first)
1488 (- (+ (seg-virtual-location last)
1490 (seg-virtual-location first)))
1491 (setf (dstate-output-state dstate) :beginning)
1492 (dolist (seg segments)
1493 (disassemble-segment seg stream dstate)))))
1495 ;;;; top-level functions
1497 ;;; Disassemble the machine code instructions for FUNCTION.
1498 (defun disassemble-function (function &key
1499 (stream *standard-output*)
1501 (declare (type compiled-function function)
1502 (type stream stream)
1503 (type (member t nil) use-labels))
1504 (let* ((dstate (make-dstate))
1505 (segments (get-function-segments function)))
1507 (label-segments segments dstate))
1508 (disassemble-segments segments stream dstate)))
1510 (defun compile-function-lambda-expr (function)
1511 (declare (type function function))
1512 (multiple-value-bind (lambda closurep name)
1513 (function-lambda-expression function)
1514 (declare (ignore name))
1516 (error "can't compile a lexical closure"))
1517 (compile nil lambda)))
1519 (defun compiled-function-or-lose (thing &optional (name thing))
1520 (cond ((or (symbolp thing)
1522 (eq (car thing) 'setf)))
1523 (compiled-function-or-lose (fdefinition thing) thing))
1524 ((sb!eval:interpreted-function-p thing)
1525 (compile-function-lambda-expr thing))
1529 (eq (car thing) 'sb!impl::lambda))
1530 (compile nil thing))
1532 (error "can't make a compiled function from ~S" name))))
1534 (defun disassemble (object &key
1535 (stream *standard-output*)
1538 "Disassemble the machine code associated with OBJECT, which can be a
1539 function, a lambda expression, or a symbol with a function definition. If
1540 it is not already compiled, the compiler is called to produce something to
1542 (declare (type (or function symbol cons) object)
1543 (type (or (member t) stream) stream)
1544 (type (member t nil) use-labels))
1545 (pprint-logical-block (*standard-output* nil :per-line-prefix "; ")
1546 (let ((fun (compiled-function-or-lose object)))
1547 (if (typep fun 'sb!kernel:byte-function)
1548 (sb!c:disassem-byte-fun fun)
1549 ;; We can't detect closures, so be careful.
1550 (disassemble-function (fun-self fun)
1552 :use-labels use-labels)))
1555 ;;; Disassembles the given area of memory starting at ADDRESS and
1556 ;;; LENGTH long. Note that if CODE-COMPONENT is NIL and this memory
1557 ;;; could move during a GC, you'd better disable it around the call to
1559 (defun disassemble-memory (address
1562 (stream *standard-output*)
1565 (declare (type (or address sb!sys:system-area-pointer) address)
1566 (type length length)
1567 (type stream stream)
1568 (type (or null sb!kernel:code-component) code-component)
1569 (type (member t nil) use-labels))
1571 (if (sb!sys:system-area-pointer-p address)
1572 (sb!sys:sap-int address)
1574 (dstate (make-dstate))
1580 (sb!kernel:code-instructions code-component)))))
1581 (when (or (< code-offs 0)
1582 (> code-offs (code-inst-area-length code-component)))
1583 (error "address ~X not in the code component ~S"
1584 address code-component))
1585 (get-code-segments code-component code-offs length))
1586 (list (make-memory-segment address length)))))
1588 (label-segments segments dstate))
1589 (disassemble-segments segments stream dstate)))
1591 ;;; Disassemble the machine code instructions associated with
1592 ;;; CODE-COMPONENT (this may include multiple entry points).
1593 (defun disassemble-code-component (code-component &key
1594 (stream *standard-output*)
1596 (declare (type (or null sb!kernel:code-component compiled-function)
1598 (type stream stream)
1599 (type (member t nil) use-labels))
1600 (let* ((code-component
1601 (if (functionp code-component)
1602 (fun-code code-component)
1604 (dstate (make-dstate))
1605 (segments (get-code-segments code-component)))
1607 (label-segments segments dstate))
1608 (disassemble-segments segments stream dstate)))
1610 ;;; code for making useful segments from arbitrary lists of code-blocks
1612 ;;; the maximum size of an instruction. Note that this includes
1613 ;;; pseudo-instructions like error traps with their associated
1614 ;;; operands, so it should be big enough to include them, i.e. it's
1615 ;;; not just 4 on a risc machine!
1616 (defconstant max-instruction-size 16)
1618 (defun add-block-segments (seg-code-block
1623 (declare (type list seglist)
1624 (type integer location)
1625 (type (or null (vector (unsigned-byte 8))) connecting-vec)
1626 (type disassem-state dstate))
1627 (flet ((addit (seg overflow)
1628 (let ((length (+ (seg-length seg) overflow)))
1630 (setf (seg-length seg) length)
1631 (incf location length)
1632 (push seg seglist)))))
1633 (let ((connecting-overflow 0)
1634 (amount (length seg-code-block)))
1635 (when connecting-vec
1636 ;; Tack on some of the new block to the old overflow vector.
1637 (let* ((beginning-of-block-amount
1638 (if seg-code-block (min max-instruction-size amount) 0))
1642 '(vector (unsigned-byte 8))
1644 (subseq seg-code-block 0 beginning-of-block-amount))
1646 (when (and (< (length connecting-vec) max-instruction-size)
1647 (not (null seg-code-block)))
1648 (return-from add-block-segments
1649 ;; We want connecting vectors to be large enough to hold
1650 ;; any instruction, and since the current seg-code-block
1651 ;; wasn't large enough to do this (and is now entirely
1652 ;; on the end of the overflow-vector), just save it for
1654 (values seglist location connecting-vec)))
1655 (when (> (length connecting-vec) 0)
1657 (make-vector-segment connecting-vec
1659 (- (length connecting-vec)
1660 beginning-of-block-amount)
1661 :virtual-location location)))
1662 (setf connecting-overflow (segment-overflow seg dstate))
1663 (addit seg connecting-overflow)))))
1664 (cond ((null seg-code-block)
1665 ;; nothing more to add
1666 (values seglist location nil))
1667 ((< (- amount connecting-overflow) max-instruction-size)
1668 ;; We can't create a segment with the minimum size
1669 ;; required for an instruction, so just keep on accumulating
1670 ;; in the overflow vector for the time-being.
1673 (subseq seg-code-block connecting-overflow amount)))
1675 ;; Put as much as we can into a new segment, and the rest
1676 ;; into the overflow-vector.
1677 (let* ((initial-length
1678 (- amount connecting-overflow max-instruction-size))
1680 (make-vector-segment seg-code-block
1683 :virtual-location location))
1685 (segment-overflow seg dstate)))
1686 (addit seg overflow)
1689 (subseq seg-code-block
1690 (+ connecting-overflow (seg-length seg))
1693 ;;;; code to disassemble assembler segments
1695 (defun assem-segment-to-disassem-segments (assem-segment dstate)
1696 (declare (type sb!assem:segment assem-segment)
1697 (type disassem-state dstate))
1699 (disassem-segments nil)
1700 (connecting-vec nil))
1701 (sb!assem:on-segment-contents-vectorly
1703 (lambda (seg-code-block)
1704 (multiple-value-setq (disassem-segments location connecting-vec)
1705 (add-block-segments seg-code-block
1710 (when connecting-vec
1711 (setf disassem-segments
1712 (add-block-segments nil
1717 (sort disassem-segments #'< :key #'seg-virtual-location)))
1719 ;;; Disassemble the machine code instructions associated with
1720 ;;; ASSEM-SEGMENT (of type assem:segment).
1721 (defun disassemble-assem-segment (assem-segment stream)
1722 (declare (type sb!assem:segment assem-segment)
1723 (type stream stream))
1724 (let* ((dstate (make-dstate))
1726 (assem-segment-to-disassem-segments assem-segment dstate)))
1727 (label-segments disassem-segments dstate)
1728 (disassemble-segments disassem-segments stream dstate)))
1730 ;;; routines to find things in the Lisp environment
1732 ;;; an alist of (SYMBOL-SLOT-OFFSET . ACCESS-FUNCTION-NAME) for slots
1733 ;;; in a symbol object that we know about
1734 (defparameter *grokked-symbol-slots*
1735 (sort `((,sb!vm:symbol-value-slot . symbol-value)
1736 (,sb!vm:symbol-plist-slot . symbol-plist)
1737 (,sb!vm:symbol-name-slot . symbol-name)
1738 (,sb!vm:symbol-package-slot . symbol-package))
1742 ;;; Given ADDRESS, try and figure out if which slot of which symbol is
1743 ;;; being referred to. Of course we can just give up, so it's not a
1744 ;;; big deal... Return two values, the symbol and the name of the
1745 ;;; access function of the slot.
1746 (defun grok-symbol-slot-ref (address)
1747 (declare (type address address))
1748 (if (not (aligned-p address sb!vm:word-bytes))
1750 (do ((slots-tail *grokked-symbol-slots* (cdr slots-tail)))
1753 (let* ((field (car slots-tail))
1754 (slot-offset (words-to-bytes (car field)))
1755 (maybe-symbol-addr (- address slot-offset))
1757 (sb!kernel:make-lisp-obj
1758 (+ maybe-symbol-addr sb!vm:other-pointer-type))))
1759 (when (symbolp maybe-symbol)
1760 (return (values maybe-symbol (cdr field))))))))
1762 (defvar *address-of-nil-object* (sb!kernel:get-lisp-obj-address nil))
1764 ;;; Given a BYTE-OFFSET from NIL, try and figure out which slot of
1765 ;;; which symbol is being referred to. Of course we can just give up,
1766 ;;; so it's not a big deal... Return two values, the symbol and the
1767 ;;; access function.
1768 (defun grok-nil-indexed-symbol-slot-ref (byte-offset)
1769 (declare (type offset byte-offset))
1770 (grok-symbol-slot-ref (+ *address-of-nil-object* byte-offset)))
1772 ;;; Return the Lisp object located BYTE-OFFSET from NIL.
1773 (defun get-nil-indexed-object (byte-offset)
1774 (declare (type offset byte-offset))
1775 (sb!kernel:make-lisp-obj (+ *address-of-nil-object* byte-offset)))
1777 ;;; Return two values; the Lisp object located at BYTE-OFFSET in the
1778 ;;; constant area of the code-object in the current segment and T, or
1779 ;;; NIL and NIL if there is no code-object in the current segment.
1780 (defun get-code-constant (byte-offset dstate)
1782 (declare (type offset byte-offset)
1783 (type disassem-state dstate))
1784 (let ((code (seg-code (dstate-segment dstate))))
1787 (sb!kernel:code-header-ref code
1789 sb!vm:other-pointer-type)
1790 (- sb!vm:word-shift)))
1794 (defun get-code-constant-absolute (addr dstate)
1795 (declare (type address addr))
1796 (declare (type disassem-state dstate))
1797 (let ((code (seg-code (dstate-segment dstate))))
1799 (return-from get-code-constant-absolute (values nil nil)))
1800 (let ((code-size (ash (sb!kernel:get-header-data code) sb!vm:word-shift)))
1801 (sb!sys:without-gcing
1802 (let ((code-addr (- (sb!kernel:get-lisp-obj-address code)
1803 sb!vm:other-pointer-type)))
1804 (if (or (< addr code-addr) (>= addr (+ code-addr code-size)))
1806 (values (sb!kernel:code-header-ref
1808 (ash (- addr code-addr) (- sb!vm:word-shift)))
1811 (defvar *assembler-routines-by-addr* nil)
1813 (defvar *foreign-symbols-by-addr* nil)
1815 ;;; Build an address-name hash-table from the name-address hash
1816 (defun invert-address-hash (htable &optional (addr-hash (make-hash-table)))
1817 (maphash (lambda (name address)
1818 (setf (gethash address addr-hash) name))
1822 ;;; Return the name of the primitive Lisp assembler routine or foreign
1823 ;;; symbol located at ADDRESS, or NIL if there isn't one.
1824 (defun find-assembler-routine (address)
1825 (declare (type address address))
1826 (when (null *assembler-routines-by-addr*)
1827 (setf *assembler-routines-by-addr*
1828 (invert-address-hash sb!fasl:*assembler-routines*))
1829 (setf *assembler-routines-by-addr*
1830 (invert-address-hash sb!fasl:*static-foreign-symbols*
1831 *assembler-routines-by-addr*)))
1832 (gethash address *assembler-routines-by-addr*))
1834 ;;;; some handy function for machine-dependent code to use...
1836 #!-sb-fluid (declaim (maybe-inline sap-ref-int read-suffix))
1838 (defun sap-ref-int (sap offset length byte-order)
1839 (declare (type sb!sys:system-area-pointer sap)
1840 (type (unsigned-byte 16) offset)
1841 (type (member 1 2 4) length)
1842 (type (member :little-endian :big-endian) byte-order)
1843 (optimize (speed 3) (safety 0)))
1845 (1 (sb!sys:sap-ref-8 sap offset))
1846 (2 (if (eq byte-order :big-endian)
1847 (+ (ash (sb!sys:sap-ref-8 sap offset) 8)
1848 (sb!sys:sap-ref-8 sap (+ offset 1)))
1849 (+ (ash (sb!sys:sap-ref-8 sap (+ offset 1)) 8)
1850 (sb!sys:sap-ref-8 sap offset))))
1851 (4 (if (eq byte-order :big-endian)
1852 (+ (ash (sb!sys:sap-ref-8 sap offset) 24)
1853 (ash (sb!sys:sap-ref-8 sap (+ 1 offset)) 16)
1854 (ash (sb!sys:sap-ref-8 sap (+ 2 offset)) 8)
1855 (sb!sys:sap-ref-8 sap (+ 3 offset)))
1856 (+ (sb!sys:sap-ref-8 sap offset)
1857 (ash (sb!sys:sap-ref-8 sap (+ 1 offset)) 8)
1858 (ash (sb!sys:sap-ref-8 sap (+ 2 offset)) 16)
1859 (ash (sb!sys:sap-ref-8 sap (+ 3 offset)) 24))))))
1861 (defun read-suffix (length dstate)
1862 (declare (type (member 8 16 32) length)
1863 (type disassem-state dstate)
1864 (optimize (speed 3) (safety 0)))
1865 (let ((length (ecase length (8 1) (16 2) (32 4))))
1866 (declare (type (unsigned-byte 3) length))
1868 (sap-ref-int (dstate-segment-sap dstate)
1869 (dstate-next-offs dstate)
1871 (dstate-byte-order dstate))
1872 (incf (dstate-next-offs dstate) length))))
1874 ;;;; optional routines to make notes about code
1876 ;;; Store NOTE (which can be either a string or a function with a
1877 ;;; single stream argument) to be printed as an end-of-line comment
1878 ;;; after the current instruction is disassembled.
1879 (defun note (note dstate)
1880 (declare (type (or string function) note)
1881 (type disassem-state dstate))
1882 (push note (dstate-notes dstate)))
1884 (defun prin1-short (thing stream)
1885 (with-print-restrictions
1886 (prin1 thing stream)))
1888 (defun prin1-quoted-short (thing stream)
1889 (if (self-evaluating-p thing)
1890 (prin1-short thing stream)
1891 (prin1-short `',thing stream)))
1893 ;;; Store a note about the lisp constant located BYTE-OFFSET bytes
1894 ;;; from the current code-component, to be printed as an end-of-line
1895 ;;; comment after the current instruction is disassembled.
1896 (defun note-code-constant (byte-offset dstate)
1897 (declare (type offset byte-offset)
1898 (type disassem-state dstate))
1899 (multiple-value-bind (const valid)
1900 (get-code-constant byte-offset dstate)
1902 (note (lambda (stream)
1903 (prin1-quoted-short const stream))
1907 ;;; Store a note about the lisp constant located at ADDR in the
1908 ;;; current code-component, to be printed as an end-of-line comment
1909 ;;; after the current instruction is disassembled.
1910 (defun note-code-constant-absolute (addr dstate)
1911 (declare (type address addr)
1912 (type disassem-state dstate))
1913 (multiple-value-bind (const valid)
1914 (get-code-constant-absolute addr dstate)
1916 (note (lambda (stream)
1917 (prin1-quoted-short const stream))
1919 (values const valid)))
1921 ;;; If the memory address located NIL-BYTE-OFFSET bytes from the
1922 ;;; constant NIL is a valid slot in a symbol, store a note describing
1923 ;;; which symbol and slot, to be printed as an end-of-line comment
1924 ;;; after the current instruction is disassembled. Returns non-NIL iff
1925 ;;; a note was recorded.
1926 (defun maybe-note-nil-indexed-symbol-slot-ref (nil-byte-offset dstate)
1927 (declare (type offset nil-byte-offset)
1928 (type disassem-state dstate))
1929 (multiple-value-bind (symbol access-fun)
1930 (grok-nil-indexed-symbol-slot-ref nil-byte-offset)
1932 (note (lambda (stream)
1933 (prin1 (if (eq access-fun 'symbol-value)
1935 `(,access-fun ',symbol))
1940 ;;; If the memory address located NIL-BYTE-OFFSET bytes from the
1941 ;;; constant NIL is a valid lisp object, store a note describing which
1942 ;;; symbol and slot, to be printed as an end-of-line comment after the
1943 ;;; current instruction is disassembled. Returns non-NIL iff a note
1945 (defun maybe-note-nil-indexed-object (nil-byte-offset dstate)
1946 (declare (type offset nil-byte-offset)
1947 (type disassem-state dstate))
1948 (let ((obj (get-nil-indexed-object nil-byte-offset)))
1949 (note (lambda (stream)
1950 (prin1-quoted-short obj stream))
1954 ;;; If ADDRESS is the address of a primitive assembler routine or
1955 ;;; foreign symbol, store a note describing which one, to be printed
1956 ;;; as an end-of-line comment after the current instruction is
1957 ;;; disassembled. Returns non-NIL iff a note was recorded. If
1958 ;;; NOTE-ADDRESS-P is non-NIL, a note of the address is also made.
1959 (defun maybe-note-assembler-routine (address note-address-p dstate)
1960 (declare (type disassem-state dstate))
1961 (unless (typep address 'address)
1962 (return-from maybe-note-assembler-routine nil))
1963 (let ((name (find-assembler-routine address)))
1965 (note (lambda (stream)
1967 (format stream "#x~8,'0x: ~a" address name)
1968 (princ name stream)))
1972 ;;; If there's a valid mapping from OFFSET in the storage class
1973 ;;; SC-NAME to a source variable, make a note of the source-variable
1974 ;;; name, to be printed as an end-of-line comment after the current
1975 ;;; instruction is disassembled. Returns non-NIL iff a note was
1977 (defun maybe-note-single-storage-ref (offset sc-name dstate)
1978 (declare (type offset offset)
1979 (type symbol sc-name)
1980 (type disassem-state dstate))
1981 (let ((storage-location
1982 (find-valid-storage-location offset sc-name dstate)))
1983 (when storage-location
1984 (note (lambda (stream)
1985 (princ (sb!di:debug-var-symbol
1986 (aref (storage-info-debug-vars
1987 (seg-storage-info (dstate-segment dstate)))
1993 ;;; If there's a valid mapping from OFFSET in the storage-base called
1994 ;;; SB-NAME to a source variable, make a note equating ASSOC-WITH with
1995 ;;; the source-variable name, to be printed as an end-of-line comment
1996 ;;; after the current instruction is disassembled. Returns non-NIL iff
1997 ;;; a note was recorded.
1998 (defun maybe-note-associated-storage-ref (offset sb-name assoc-with dstate)
1999 (declare (type offset offset)
2000 (type symbol sb-name)
2001 (type (or symbol string) assoc-with)
2002 (type disassem-state dstate))
2003 (let ((storage-location
2004 (find-valid-storage-location offset sb-name dstate)))
2005 (when storage-location
2006 (note (lambda (stream)
2007 (format stream "~A = ~S"
2009 (sb!di:debug-var-symbol
2010 (aref (dstate-debug-vars dstate)
2016 (defun get-internal-error-name (errnum)
2017 (car (svref sb!c:*backend-internal-errors* errnum)))
2019 (defun get-sc-name (sc-offs)
2020 (sb!c::location-print-name
2021 ;; FIXME: This seems like an awful lot of computation just to get a name.
2022 ;; Couldn't we just use lookup in *BACKEND-SC-NAMES*, without having to cons
2024 (sb!c:make-random-tn :kind :normal
2025 :sc (svref sb!c:*backend-sc-numbers*
2026 (sb!c:sc-offset-scn sc-offs))
2027 :offset (sb!c:sc-offset-offset sc-offs))))
2029 ;;; When called from an error break instruction's :DISASSEM-CONTROL (or
2030 ;;; :DISASSEM-PRINTER) function, will correctly deal with printing the
2031 ;;; arguments to the break.
2033 ;;; ERROR-PARSE-FUN should be a function that accepts:
2034 ;;; 1) a SYSTEM-AREA-POINTER
2035 ;;; 2) a BYTE-OFFSET from the SAP to begin at
2036 ;;; 3) optionally, LENGTH-ONLY, which if non-NIL, means to only return
2037 ;;; the byte length of the arguments (to avoid unnecessary consing)
2038 ;;; It should read information from the SAP starting at BYTE-OFFSET, and
2039 ;;; return four values:
2040 ;;; 1) the error number
2041 ;;; 2) the total length, in bytes, of the information
2042 ;;; 3) a list of SC-OFFSETs of the locations of the error parameters
2043 ;;; 4) a list of the length (as read from the SAP), in bytes, of each
2044 ;;; of the return values.
2045 (defun handle-break-args (error-parse-fun stream dstate)
2046 (declare (type function error-parse-fun)
2047 (type (or null stream) stream)
2048 (type disassem-state dstate))
2049 (multiple-value-bind (errnum adjust sc-offsets lengths)
2050 (funcall error-parse-fun
2051 (dstate-segment-sap dstate)
2052 (dstate-next-offs dstate)
2055 (setf (dstate-cur-offs dstate)
2056 (dstate-next-offs dstate))
2057 (flet ((emit-err-arg (note)
2058 (let ((num (pop lengths)))
2059 (print-notes-and-newline stream dstate)
2060 (print-current-address stream dstate)
2061 (print-bytes num stream dstate)
2062 (incf (dstate-cur-offs dstate) num)
2064 (note note dstate)))))
2066 (emit-err-arg (symbol-name (get-internal-error-name errnum)))
2067 (dolist (sc-offs sc-offsets)
2068 (emit-err-arg (get-sc-name sc-offs)))))
2069 (incf (dstate-next-offs dstate)