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)
48 "~@<Instructions either aren't related or conflict in some way: ~4I~_~S~:>"
51 ;;; Given a list of instructions INSTS, Sees if one of these instructions is a
52 ;;; more general form of all the others, in which case they are put into its
53 ;;; specializers list, and it is returned. Otherwise an error is signaled.
54 (defun try-specializing (insts)
55 (declare (type list insts))
56 (let ((masters (copy-list insts)))
57 (dolist (possible-master insts)
58 (dolist (possible-specializer insts)
59 (unless (or (eq possible-specializer possible-master)
60 (inst-specializes-p possible-specializer possible-master))
61 (setf masters (delete possible-master masters))
62 (return) ; exit the inner loop
65 (specialization-error insts))
67 (error "multiple specializing masters: ~S" masters))
69 (let ((master (car masters)))
70 (setf (inst-specializers master)
71 (order-specializers (remove master insts)))
74 ;;;; choosing an instruction
76 #!-sb-fluid (declaim (inline inst-matches-p choose-inst-specialization))
78 ;;; Return non-NIL if all constant-bits in INST match CHUNK.
79 (defun inst-matches-p (inst chunk)
80 (declare (type instruction inst)
82 (dchunk= (dchunk-and (inst-mask inst) chunk) (inst-id inst)))
84 ;;; Given an instruction object, INST, and a bit-pattern, CHUNK, pick
85 ;;; the most specific instruction on INST's specializer list whose
86 ;;; constraints are met by CHUNK. If none do, then return INST.
87 (defun choose-inst-specialization (inst chunk)
88 (declare (type instruction inst)
90 (or (dolist (spec (inst-specializers inst) nil)
91 (declare (type instruction spec))
92 (when (inst-matches-p spec chunk)
96 ;;;; searching for an instruction in instruction space
98 ;;; Return the instruction object within INST-SPACE corresponding to the
99 ;;; bit-pattern CHUNK, or NIL if there isn't one.
100 (defun find-inst (chunk inst-space)
101 (declare (type dchunk chunk)
102 (type (or null inst-space instruction) inst-space))
103 (etypecase inst-space
106 (if (inst-matches-p inst-space chunk)
107 (choose-inst-specialization inst-space chunk)
110 (let* ((mask (ispace-valid-mask inst-space))
111 (id (dchunk-and mask chunk)))
112 (declare (type dchunk id mask))
113 (dolist (choice (ispace-choices inst-space))
114 (declare (type inst-space-choice choice))
115 (when (dchunk= id (ischoice-common-id choice))
116 (return (find-inst chunk (ischoice-subspace choice)))))))))
118 ;;;; building the instruction space
120 ;;; Returns an instruction-space object corresponding to the list of
121 ;;; instructions INSTS. If the optional parameter INITIAL-MASK is
122 ;;; supplied, only bits it has set are used.
123 (defun build-inst-space (insts &optional (initial-mask dchunk-one))
124 ;; This is done by finding any set of bits that's common to
125 ;; all instructions, building an instruction-space node that selects on those
126 ;; bits, and recursively handle sets of instructions with a common value for
127 ;; these bits (which, since there should be fewer instructions than in INSTS,
128 ;; should have some additional set of bits to select on, etc). If there
129 ;; are no common bits, or all instructions have the same value within those
130 ;; bits, TRY-SPECIALIZING is called, which handles the cases of many
131 ;; variations on a single instruction.
132 (declare (type list insts)
133 (type dchunk initial-mask))
139 (let ((vmask (dchunk-copy initial-mask)))
141 (dchunk-andf vmask (inst-mask inst)))
142 (if (dchunk-zerop vmask)
143 (try-specializing insts)
146 (let* ((common-id (dchunk-and (inst-id inst) vmask))
147 (bucket (assoc common-id buckets :test #'dchunk=)))
149 (push (list common-id inst) buckets))
151 (push inst (cdr bucket))))))
152 (let ((submask (dchunk-clear initial-mask vmask)))
153 (if (= (length buckets) 1)
154 (try-specializing insts)
157 :choices (mapcar (lambda (bucket)
158 (make-inst-space-choice
159 :subspace (build-inst-space
162 :common-id (car bucket)))
165 ;;;; an inst-space printer for debugging purposes
167 (defun print-masked-binary (num mask word-size &optional (show word-size))
168 (do ((bit (1- word-size) (1- bit)))
170 (write-char (cond ((logbitp bit mask)
171 (if (logbitp bit num) #\1 #\0))
175 (defun print-inst-bits (inst)
176 (print-masked-binary (inst-id inst)
179 (bytes-to-bits (inst-length inst))))
181 ;;; Print a nicely-formatted version of INST-SPACE.
182 (defun print-inst-space (inst-space &optional (indent 0))
183 (etypecase inst-space
186 (format t "~Vt[~A(~A)~40T" indent
187 (inst-name inst-space)
188 (inst-format-name inst-space))
189 (print-inst-bits inst-space)
190 (dolist (inst (inst-specializers inst-space))
191 (format t "~%~Vt:~A~40T" indent (inst-name inst))
192 (print-inst-bits inst))
196 (format t "~Vt---- ~8,'0X ----~%"
198 (ispace-valid-mask inst-space))
201 (format t "~Vt~8,'0X ==>~%"
203 (ischoice-common-id choice))
204 (print-inst-space (ischoice-subspace choice)
206 (ispace-choices inst-space)))))
208 ;;;; (The actual disassembly part follows.)
210 ;;; Code object layout:
212 ;;; code-size (starting from first inst, in words)
213 ;;; entry-points (points to first function header)
215 ;;; trace-table-offset (starting from first inst, in bytes)
219 ;;; <padding to dual-word boundary>
220 ;;; start of instructions
222 ;;; fun-headers and lra's buried in here randomly
224 ;;; start of trace-table
225 ;;; <padding to dual-word boundary>
227 ;;; Function header layout (dual word aligned):
230 ;;; next pointer (next function header)
235 ;;; LRA layout (dual word aligned):
238 #!-sb-fluid (declaim (inline words-to-bytes bytes-to-words))
240 (eval-when (:compile-toplevel :load-toplevel :execute)
241 ;;; Convert a word-offset NUM to a byte-offset.
242 (defun words-to-bytes (num)
243 (declare (type offset num))
244 (ash num sb!vm:word-shift))
247 ;;; Convert a byte-offset NUM to a word-offset.
248 (defun bytes-to-words (num)
249 (declare (type offset num))
250 (ash num (- sb!vm:word-shift)))
252 (defconstant lra-size (words-to-bytes 1))
254 (defstruct (offs-hook (:copier nil))
255 (offset 0 :type offset)
256 (fun (missing-arg) :type function)
257 (before-address nil :type (member t nil)))
259 (defstruct (segment (:conc-name seg-)
260 (:constructor %make-segment)
262 (sap-maker (missing-arg)
263 :type (function () sb!sys:system-area-pointer))
264 (length 0 :type disassem-length)
265 (virtual-location 0 :type address)
266 (storage-info nil :type (or null storage-info))
267 (code nil :type (or null sb!kernel:code-component))
268 (hooks nil :type list))
269 (def!method print-object ((seg segment) stream)
270 (print-unreadable-object (seg stream :type t)
271 (let ((addr (sb!sys:sap-int (funcall (seg-sap-maker seg)))))
272 (format stream "#X~X[~W]~:[ (#X~X)~;~*~]~@[ in ~S~]"
275 (= (seg-virtual-location seg) addr)
276 (seg-virtual-location seg)
281 (defun fun-self (fun)
282 (declare (type compiled-function fun))
283 (sb!kernel:%simple-fun-self (sb!kernel:%fun-fun fun)))
285 (defun fun-code (fun)
286 (declare (type compiled-function fun))
287 (sb!kernel:fun-code-header (fun-self fun)))
289 (defun fun-next (fun)
290 (declare (type compiled-function fun))
291 (sb!kernel:%simple-fun-next (sb!kernel:%fun-fun fun)))
293 (defun fun-address (fun)
294 (declare (type compiled-function fun))
295 (- (sb!kernel:get-lisp-obj-address (sb!kernel:%fun-fun fun)) sb!vm:fun-pointer-lowtag))
297 ;;; the offset of FUNCTION from the start of its code-component's
299 (defun fun-insts-offset (function)
300 (declare (type compiled-function function))
301 (- (fun-address function)
302 (sb!sys:sap-int (sb!kernel:code-instructions (fun-code function)))))
304 ;;; the offset of FUNCTION from the start of its code-component
305 (defun fun-offset (function)
306 (declare (type compiled-function function))
307 (words-to-bytes (sb!kernel:get-closure-length function)))
309 ;;;; operations on code-components (which hold the instructions for
310 ;;;; one or more functions)
312 ;;; Return the length of the instruction area in CODE-COMPONENT.
313 (defun code-inst-area-length (code-component)
314 (declare (type sb!kernel:code-component code-component))
315 (sb!kernel:code-header-ref code-component
316 sb!vm:code-trace-table-offset-slot))
318 ;;; Return the address of the instruction area in CODE-COMPONENT.
319 (defun code-inst-area-address (code-component)
320 (declare (type sb!kernel:code-component code-component))
321 (sb!sys:sap-int (sb!kernel:code-instructions code-component)))
323 ;;; unused as of sbcl-0.pre7.129
325 ;;; Return the first function in CODE-COMPONENT.
326 (defun code-first-function (code-component)
327 (declare (type sb!kernel:code-component code-component))
328 (sb!kernel:code-header-ref code-component
329 sb!vm:code-trace-table-offset-slot))
332 (defun segment-offs-to-code-offs (offset segment)
333 (sb!sys:without-gcing
334 (let* ((seg-base-addr (sb!sys:sap-int (funcall (seg-sap-maker segment))))
336 (logandc1 sb!vm:lowtag-mask
337 (sb!kernel:get-lisp-obj-address (seg-code segment))))
338 (addr (+ offset seg-base-addr)))
339 (declare (type address seg-base-addr code-addr addr))
340 (- addr code-addr))))
342 (defun code-offs-to-segment-offs (offset segment)
343 (sb!sys:without-gcing
344 (let* ((seg-base-addr (sb!sys:sap-int (funcall (seg-sap-maker segment))))
346 (logandc1 sb!vm:lowtag-mask
347 (sb!kernel:get-lisp-obj-address (seg-code segment))))
348 (addr (+ offset code-addr)))
349 (declare (type address seg-base-addr code-addr addr))
350 (- addr seg-base-addr))))
352 (defun code-insts-offs-to-segment-offs (offset segment)
353 (sb!sys:without-gcing
354 (let* ((seg-base-addr (sb!sys:sap-int (funcall (seg-sap-maker segment))))
356 (sb!sys:sap-int (sb!kernel:code-instructions (seg-code segment))))
357 (addr (+ offset code-insts-addr)))
358 (declare (type address seg-base-addr code-insts-addr addr))
359 (- addr seg-base-addr))))
361 (defun lra-hook (chunk stream dstate)
362 (declare (type dchunk chunk)
364 (type (or null stream) stream)
365 (type disassem-state dstate))
366 (when (and (aligned-p (+ (seg-virtual-location (dstate-segment dstate))
367 (dstate-cur-offs dstate))
368 (* 2 sb!vm:n-word-bytes))
370 (= (sb!sys:sap-ref-8 (dstate-segment-sap dstate)
371 (if (eq (dstate-byte-order dstate)
373 (dstate-cur-offs dstate)
374 (+ (dstate-cur-offs dstate)
376 sb!vm:return-pc-header-widetag))
377 (unless (null stream)
378 (note "possible LRA header" dstate)))
381 ;;; Print the fun-header (entry-point) pseudo-instruction at the
382 ;;; current location in DSTATE to STREAM.
383 (defun fun-header-hook (stream dstate)
384 (declare (type (or null stream) stream)
385 (type disassem-state dstate))
386 (unless (null stream)
387 (let* ((seg (dstate-segment dstate))
388 (code (seg-code seg))
391 (segment-offs-to-code-offs (dstate-cur-offs dstate) seg)))
393 (sb!kernel:code-header-ref code
395 sb!vm:simple-fun-name-slot)))
397 (sb!kernel:code-header-ref code
399 sb!vm:simple-fun-arglist-slot)))
401 (sb!kernel:code-header-ref code
403 sb!vm:simple-fun-type-slot))))
404 (format stream ".~A ~S~:A" 'entry name args)
405 (note (lambda (stream)
406 (format stream "~:S" type)) ; use format to print NIL as ()
408 (incf (dstate-next-offs dstate)
409 (words-to-bytes sb!vm:simple-fun-code-offset)))
411 (defun alignment-hook (chunk stream dstate)
412 (declare (type dchunk chunk)
414 (type (or null stream) stream)
415 (type disassem-state dstate))
417 (+ (seg-virtual-location (dstate-segment dstate))
418 (dstate-cur-offs dstate)))
419 (alignment (dstate-alignment dstate)))
420 (unless (aligned-p location alignment)
422 (format stream "~A~Vt~W~%" '.align
423 (dstate-argument-column dstate)
425 (incf(dstate-next-offs dstate)
426 (- (align location alignment) location)))
429 (defun rewind-current-segment (dstate segment)
430 (declare (type disassem-state dstate)
431 (type segment segment))
432 (setf (dstate-segment dstate) segment)
433 (setf (dstate-cur-offs-hooks dstate)
434 (stable-sort (nreverse (copy-list (seg-hooks segment)))
436 (or (< (offs-hook-offset oh1) (offs-hook-offset oh2))
437 (and (= (offs-hook-offset oh1)
438 (offs-hook-offset oh2))
439 (offs-hook-before-address oh1)
440 (not (offs-hook-before-address oh2)))))))
441 (setf (dstate-cur-offs dstate) 0)
442 (setf (dstate-cur-labels dstate) (dstate-labels dstate)))
444 (defun call-offs-hooks (before-address stream dstate)
445 (declare (type (or null stream) stream)
446 (type disassem-state dstate))
447 (let ((cur-offs (dstate-cur-offs dstate)))
448 (setf (dstate-next-offs dstate) cur-offs)
450 (let ((next-hook (car (dstate-cur-offs-hooks dstate))))
451 (when (null next-hook)
453 (let ((hook-offs (offs-hook-offset next-hook)))
454 (when (or (> hook-offs cur-offs)
455 (and (= hook-offs cur-offs)
457 (not (offs-hook-before-address next-hook))))
459 (unless (< hook-offs cur-offs)
460 (funcall (offs-hook-fun next-hook) stream dstate))
461 (pop (dstate-cur-offs-hooks dstate))
462 (unless (= (dstate-next-offs dstate) cur-offs)
465 (defun call-fun-hooks (chunk stream dstate)
466 (let ((hooks (dstate-fun-hooks dstate))
467 (cur-offs (dstate-cur-offs dstate)))
468 (setf (dstate-next-offs dstate) cur-offs)
469 (dolist (hook hooks nil)
470 (let ((prefix-p (funcall hook chunk stream dstate)))
471 (unless (= (dstate-next-offs dstate) cur-offs)
472 (return prefix-p))))))
474 (defun handle-bogus-instruction (stream dstate)
475 (let ((alignment (dstate-alignment dstate)))
476 (unless (null stream)
477 (multiple-value-bind (words bytes)
478 (truncate alignment sb!vm:n-word-bytes)
480 (print-inst (* words sb!vm:n-word-bytes) stream dstate))
482 (print-inst bytes stream dstate)))
483 (print-bytes alignment stream dstate))
484 (incf (dstate-next-offs dstate) alignment)))
486 ;;; Iterate through the instructions in SEGMENT, calling FUNCTION for
487 ;;; each instruction, with arguments of CHUNK, STREAM, and DSTATE.
488 (defun map-segment-instructions (function segment dstate &optional stream)
489 (declare (type function function)
490 (type segment segment)
491 (type disassem-state dstate)
492 (type (or null stream) stream))
494 (let ((ispace (get-inst-space))
495 (prefix-p nil) ; just processed a prefix inst
496 (prefix-len 0)) ; length of any prefix instruction(s)
498 (rewind-current-segment dstate segment)
501 (when (>= (dstate-cur-offs dstate)
502 (seg-length (dstate-segment dstate)))
506 (setf (dstate-next-offs dstate) (dstate-cur-offs dstate))
508 (call-offs-hooks t stream dstate)
509 (unless (or prefix-p (null stream))
510 (print-current-address stream dstate))
511 (call-offs-hooks nil stream dstate)
513 (unless (> (dstate-next-offs dstate) (dstate-cur-offs dstate))
514 (sb!sys:without-gcing
515 (setf (dstate-segment-sap dstate) (funcall (seg-sap-maker segment)))
518 (sap-ref-dchunk (dstate-segment-sap dstate)
519 (dstate-cur-offs dstate)
520 (dstate-byte-order dstate))))
521 (let ((fun-prefix-p (call-fun-hooks chunk stream dstate)))
522 (if (> (dstate-next-offs dstate) (dstate-cur-offs dstate))
523 (setf prefix-p fun-prefix-p)
524 (let ((inst (find-inst chunk ispace)))
526 (handle-bogus-instruction stream dstate))
528 (setf (dstate-inst-properties dstate) nil)
529 (setf (dstate-next-offs dstate)
530 (+ (dstate-cur-offs dstate)
532 (let ((orig-next (dstate-next-offs dstate)))
533 (print-inst (inst-length inst) stream dstate :trailing-space nil)
534 (let ((prefilter (inst-prefilter inst))
535 (control (inst-control inst)))
537 (funcall prefilter chunk dstate))
539 (setf prefix-p (null (inst-printer inst)))
541 ;; print any instruction bytes recognized by the prefilter which calls read-suffix
542 ;; and updates next-offs
544 (let ((suffix-len (- (dstate-next-offs dstate) orig-next)))
545 (when (plusp suffix-len)
546 (print-inst suffix-len stream dstate :offset (inst-length inst) :trailing-space nil))
548 (dotimes (i (- *disassem-inst-column-width* (* 2 (+ (inst-length inst) suffix-len prefix-len))))
549 (write-char #\space stream))
550 (write-char #\space stream))
552 (setf prefix-len (+ (inst-length inst) suffix-len))))
554 (funcall function chunk inst)
557 (funcall control chunk inst stream dstate))
560 (setf (dstate-cur-offs dstate) (dstate-next-offs dstate))
562 (unless (null stream)
565 (print-notes-and-newline stream dstate))
566 (setf (dstate-output-state dstate) nil)))))
568 ;;; Make an initial non-printing disassembly pass through DSTATE,
569 ;;; noting any addresses that are referenced by instructions in this
571 (defun add-segment-labels (segment dstate)
572 ;; add labels at the beginning with a label-number of nil; we'll notice
573 ;; later and fill them in (and sort them)
574 (declare (type disassem-state dstate))
575 (let ((labels (dstate-labels dstate)))
576 (map-segment-instructions
578 (declare (type dchunk chunk) (type instruction inst))
579 (let ((labeller (inst-labeller inst)))
581 (setf labels (funcall labeller chunk labels dstate)))))
584 (setf (dstate-labels dstate) labels)
585 ;; erase any notes that got there by accident
586 (setf (dstate-notes dstate) nil)))
588 ;;; If any labels in DSTATE have been added since the last call to
589 ;;; this function, give them label-numbers, enter them in the
590 ;;; hash-table, and make sure the label list is in sorted order.
591 (defun number-labels (dstate)
592 (let ((labels (dstate-labels dstate)))
593 (when (and labels (null (cdar labels)))
594 ;; at least one label left un-numbered
595 (setf labels (sort labels #'< :key #'car))
597 (label-hash (dstate-label-hash dstate)))
598 (dolist (label labels)
599 (when (not (null (cdr label)))
600 (setf max (max max (cdr label)))))
601 (dolist (label labels)
602 (when (null (cdr label))
604 (setf (cdr label) max)
605 (setf (gethash (car label) label-hash)
606 (format nil "L~W" max)))))
607 (setf (dstate-labels dstate) labels))))
609 ;;; Get the instruction-space, creating it if necessary.
610 (defun get-inst-space ()
611 (let ((ispace *disassem-inst-space*))
614 (maphash (lambda (name inst-flavs)
615 (declare (ignore name))
616 (dolist (flav inst-flavs)
619 (setf ispace (build-inst-space insts)))
620 (setf *disassem-inst-space* ispace))
623 ;;;; Add global hooks.
625 (defun add-offs-hook (segment addr hook)
626 (let ((entry (cons addr hook)))
627 (if (null (seg-hooks segment))
628 (setf (seg-hooks segment) (list entry))
629 (push entry (cdr (last (seg-hooks segment)))))))
631 (defun add-offs-note-hook (segment addr note)
632 (add-offs-hook segment
634 (lambda (stream dstate)
635 (declare (type (or null stream) stream)
636 (type disassem-state dstate))
638 (note note dstate)))))
640 (defun add-offs-comment-hook (segment addr comment)
641 (add-offs-hook segment
643 (lambda (stream dstate)
644 (declare (type (or null stream) stream)
647 (write-string ";;; " stream)
650 (write-string comment stream))
652 (funcall comment stream)))
655 (defun add-fun-hook (dstate function)
656 (push function (dstate-fun-hooks dstate)))
658 (defun set-location-printing-range (dstate from length)
659 (setf (dstate-addr-print-len dstate)
660 ;; 4 bits per hex digit
661 (ceiling (integer-length (logxor from (+ from length))) 4)))
663 ;;; Print the current address in DSTATE to STREAM, plus any labels that
664 ;;; correspond to it, and leave the cursor in the instruction column.
665 (defun print-current-address (stream dstate)
666 (declare (type stream stream)
667 (type disassem-state dstate))
669 (+ (seg-virtual-location (dstate-segment dstate))
670 (dstate-cur-offs dstate)))
671 (location-column-width *disassem-location-column-width*)
672 (plen (dstate-addr-print-len dstate)))
675 (setf plen location-column-width)
676 (let ((seg (dstate-segment dstate)))
677 (set-location-printing-range dstate
678 (seg-virtual-location seg)
680 (when (eq (dstate-output-state dstate) :beginning)
681 (setf plen location-column-width))
685 (setf location-column-width (+ 2 location-column-width))
688 ;; print the location
689 ;; [this is equivalent to (format stream "~V,'0x:" plen printed-value), but
690 ;; usually avoids any consing]
691 (tab0 (- location-column-width plen) stream)
692 (let* ((printed-bits (* 4 plen))
693 (printed-value (ldb (byte printed-bits 0) location))
695 (truncate (- printed-bits (integer-length printed-value)) 4)))
696 (dotimes (i leading-zeros)
697 (write-char #\0 stream))
698 (unless (zerop printed-value)
699 (write printed-value :stream stream :base 16 :radix nil))
700 (write-char #\: stream))
704 (let* ((next-label (car (dstate-cur-labels dstate)))
705 (label-location (car next-label)))
706 (when (or (null label-location) (> label-location location))
708 (unless (< label-location location)
709 (format stream " L~W:" (cdr next-label)))
710 (pop (dstate-cur-labels dstate))))
712 ;; move to the instruction column
713 (tab0 (+ location-column-width 1 label-column-width) stream)
716 (eval-when (:compile-toplevel :execute)
717 (sb!xc:defmacro with-print-restrictions (&rest body)
718 `(let ((*print-pretty* t)
724 ;;; Print a newline to STREAM, inserting any pending notes in DSTATE
725 ;;; as end-of-line comments. If there is more than one note, a
726 ;;; separate line will be used for each one.
727 (defun print-notes-and-newline (stream dstate)
728 (declare (type stream stream)
729 (type disassem-state dstate))
730 (with-print-restrictions
731 (dolist (note (dstate-notes dstate))
732 (format stream "~Vt " *disassem-note-column*)
733 (pprint-logical-block (stream nil :per-line-prefix "; ")
736 (write-string note stream))
738 (funcall note stream))))
741 (setf (dstate-notes dstate) nil)))
743 ;;; Print NUM instruction bytes to STREAM as hex values.
744 (defun print-inst (num stream dstate &key (offset 0) (trailing-space t))
745 (let ((sap (dstate-segment-sap dstate))
746 (start-offs (+ offset (dstate-cur-offs dstate))))
748 (format stream "~2,'0x" (sb!sys:sap-ref-8 sap (+ offs start-offs))))
750 (dotimes (i (- *disassem-inst-column-width* (* 2 num)))
751 (write-char #\space stream))
752 (write-char #\space stream))))
754 ;;; Disassemble NUM bytes to STREAM as simple `BYTE' instructions.
755 (defun print-bytes (num stream dstate)
756 (declare (type offset num)
758 (type disassem-state dstate))
759 (format stream "~A~Vt" 'BYTE (dstate-argument-column dstate))
760 (let ((sap (dstate-segment-sap dstate))
761 (start-offs (dstate-cur-offs dstate)))
764 (write-string ", " stream))
765 (format stream "#X~2,'0x" (sb!sys:sap-ref-8 sap (+ offs start-offs))))))
767 ;;; Disassemble NUM machine-words to STREAM as simple `WORD' instructions.
768 (defun print-words (num stream dstate)
769 (declare (type offset num)
771 (type disassem-state dstate))
772 (format stream "~A~Vt" 'WORD (dstate-argument-column dstate))
773 (let ((sap (dstate-segment-sap dstate))
774 (start-offs (dstate-cur-offs dstate))
775 (byte-order (dstate-byte-order dstate)))
776 (dotimes (word-offs num)
777 (unless (zerop word-offs)
778 (write-string ", " stream))
779 (let ((word 0) (bit-shift 0))
780 (dotimes (byte-offs sb!vm:n-word-bytes)
785 (* word-offs sb!vm:n-word-bytes)
788 (if (eq byte-order :big-endian)
789 (+ (ash word sb!vm:n-byte-bits) byte)
790 (+ word (ash byte bit-shift))))
791 (incf bit-shift sb!vm:n-byte-bits)))
792 (format stream "#X~V,'0X" (ash sb!vm:n-word-bits -2) word)))))
794 (defvar *default-dstate-hooks* (list #'lra-hook))
796 ;;; Make a disassembler-state object.
797 (defun make-dstate (&optional (fun-hooks *default-dstate-hooks*))
799 (sb!sys:vector-sap (coerce #() '(vector (unsigned-byte 8)))))
800 (alignment *disassem-inst-alignment-bytes*)
802 (+ (or *disassem-opcode-column-width* 0)
803 *disassem-location-column-width*
805 label-column-width)))
807 (when (> alignment 1)
808 (push #'alignment-hook fun-hooks))
810 (%make-dstate :segment-sap sap
812 :argument-column arg-column
814 :byte-order sb!c:*backend-byte-order*)))
816 (defun add-fun-header-hooks (segment)
817 (declare (type segment segment))
818 (do ((fun (sb!kernel:code-header-ref (seg-code segment)
819 sb!vm:code-entry-points-slot)
821 (length (seg-length segment)))
823 (let ((offset (code-offs-to-segment-offs (fun-offset fun) segment)))
824 (when (<= 0 offset length)
825 (push (make-offs-hook :offset offset :fun #'fun-header-hook)
826 (seg-hooks segment))))))
828 ;;; A SAP-MAKER is a no-argument function that returns a SAP.
830 #!-sb-fluid (declaim (inline sap-maker))
832 (defun sap-maker (function input offset)
833 (declare (optimize (speed 3))
834 (type (function (t) sb!sys:system-area-pointer) function)
835 (type offset offset))
836 (let ((old-sap (sb!sys:sap+ (funcall function input) offset)))
837 (declare (type sb!sys:system-area-pointer old-sap))
840 (+ (sb!sys:sap-int (funcall function input)) offset)))
841 ;; Saving the sap like this avoids consing except when the sap
842 ;; changes (because the sap-int, arith, etc., get inlined).
843 (declare (type address new-addr))
844 (if (= (sb!sys:sap-int old-sap) new-addr)
846 (setf old-sap (sb!sys:int-sap new-addr)))))))
848 (defun vector-sap-maker (vector offset)
849 (declare (optimize (speed 3))
850 (type offset offset))
851 (sap-maker #'sb!sys:vector-sap vector offset))
853 (defun code-sap-maker (code offset)
854 (declare (optimize (speed 3))
855 (type sb!kernel:code-component code)
856 (type offset offset))
857 (sap-maker #'sb!kernel:code-instructions code offset))
859 (defun memory-sap-maker (address)
860 (declare (optimize (speed 3))
861 (type address address))
862 (let ((sap (sb!sys:int-sap address)))
865 ;;; Return a memory segment located at the system-area-pointer returned by
866 ;;; SAP-MAKER and LENGTH bytes long in the disassem-state object DSTATE.
868 ;;; &KEY arguments include :VIRTUAL-LOCATION (by default the same as
869 ;;; the address), :DEBUG-FUN, :SOURCE-FORM-CACHE (a
870 ;;; SOURCE-FORM-CACHE object), and :HOOKS (a list of OFFS-HOOK
872 (defun make-segment (sap-maker length
874 code virtual-location
875 debug-fun source-form-cache
877 (declare (type (function () sb!sys:system-area-pointer) sap-maker)
878 (type disassem-length length)
879 (type (or null address) virtual-location)
880 (type (or null sb!di:debug-fun) debug-fun)
881 (type (or null source-form-cache) source-form-cache))
886 :virtual-location (or virtual-location
887 (sb!sys:sap-int (funcall sap-maker)))
890 (add-debugging-hooks segment debug-fun source-form-cache)
891 (add-fun-header-hooks segment)
894 (defun make-vector-segment (vector offset &rest args)
895 (declare (type vector vector)
897 (inline make-segment))
898 (apply #'make-segment (vector-sap-maker vector offset) args))
900 (defun make-code-segment (code offset length &rest args)
901 (declare (type sb!kernel:code-component code)
903 (inline make-segment))
904 (apply #'make-segment (code-sap-maker code offset) length :code code args))
906 (defun make-memory-segment (address &rest args)
907 (declare (type address address)
908 (inline make-segment))
909 (apply #'make-segment (memory-sap-maker address) args))
912 (defun print-fun-headers (function)
913 (declare (type compiled-function function))
914 (let* ((self (fun-self function))
915 (code (sb!kernel:fun-code-header self)))
916 (format t "Code-header ~S: size: ~S, trace-table-offset: ~S~%"
918 (sb!kernel:code-header-ref code
919 sb!vm:code-code-size-slot)
920 (sb!kernel:code-header-ref code
921 sb!vm:code-trace-table-offset-slot))
922 (do ((fun (sb!kernel:code-header-ref code sb!vm:code-entry-points-slot)
925 (let ((fun-offset (sb!kernel:get-closure-length fun)))
926 ;; There is function header fun-offset words from the
928 (format t "Fun-header ~S at offset ~W (words): ~S~A => ~S~%"
931 (sb!kernel:code-header-ref
932 code (+ fun-offset sb!vm:simple-fun-name-slot))
933 (sb!kernel:code-header-ref
934 code (+ fun-offset sb!vm:simple-fun-arglist-slot))
935 (sb!kernel:code-header-ref
936 code (+ fun-offset sb!vm:simple-fun-type-slot)))))))
938 ;;; getting at the source code...
940 (defstruct (source-form-cache (:conc-name sfcache-)
942 (debug-source nil :type (or null sb!di:debug-source))
943 (toplevel-form-index -1 :type fixnum)
944 (toplevel-form nil :type list)
945 (form-number-mapping-table nil :type (or null (vector list)))
946 (last-location-retrieved nil :type (or null sb!di:code-location))
947 (last-form-retrieved -1 :type fixnum))
949 (defun get-toplevel-form (debug-source tlf-index)
950 (let ((name (sb!di:debug-source-name debug-source)))
951 (ecase (sb!di:debug-source-from debug-source)
953 (cond ((not (probe-file name))
954 (warn "The source file ~S no longer seems to exist." name)
957 (let ((start-positions
958 (sb!di:debug-source-start-positions debug-source)))
959 (cond ((null start-positions)
960 (warn "There is no start positions map.")
963 (let* ((local-tlf-index
965 (sb!di:debug-source-root-number
968 (aref start-positions local-tlf-index)))
969 (with-open-file (f name)
970 (cond ((= (sb!di:debug-source-created debug-source)
971 (file-write-date name))
972 (file-position f char-offset))
974 (warn "Source file ~S has been modified; ~@
975 using form offset instead of ~
978 (let ((*read-suppress* t))
979 (dotimes (i local-tlf-index) (read f)))))
980 (let ((*readtable* (copy-readtable)))
981 (set-dispatch-macro-character
983 (lambda (stream sub-char &rest rest)
984 (declare (ignore rest sub-char))
985 (let ((token (read stream t nil t)))
986 (format nil "#.~S" token))))
990 (aref name tlf-index)))))
992 (defun cache-valid (loc cache)
994 (and (eq (sb!di:code-location-debug-source loc)
995 (sfcache-debug-source cache))
996 (eq (sb!di:code-location-toplevel-form-offset loc)
997 (sfcache-toplevel-form-index cache)))))
999 (defun get-source-form (loc context &optional cache)
1000 (let* ((cache-valid (cache-valid loc cache))
1001 (tlf-index (sb!di:code-location-toplevel-form-offset loc))
1002 (form-number (sb!di:code-location-form-number loc))
1005 (sfcache-toplevel-form cache)
1006 (get-toplevel-form (sb!di:code-location-debug-source loc)
1010 (sfcache-form-number-mapping-table cache)
1011 (sb!di:form-number-translations toplevel-form tlf-index))))
1012 (when (and (not cache-valid) cache)
1013 (setf (sfcache-debug-source cache) (sb!di:code-location-debug-source loc)
1014 (sfcache-toplevel-form-index cache) tlf-index
1015 (sfcache-toplevel-form cache) toplevel-form
1016 (sfcache-form-number-mapping-table cache) mapping-table))
1017 (cond ((null toplevel-form)
1019 ((>= form-number (length mapping-table))
1020 (warn "bogus form-number in form! The source file has probably ~@
1021 been changed too much to cope with.")
1023 ;; Disable future warnings.
1024 (setf (sfcache-toplevel-form cache) nil))
1028 (setf (sfcache-last-location-retrieved cache) loc)
1029 (setf (sfcache-last-form-retrieved cache) form-number))
1030 (sb!di:source-path-context toplevel-form
1031 (aref mapping-table form-number)
1034 (defun get-different-source-form (loc context &optional cache)
1035 (if (and (cache-valid loc cache)
1036 (or (= (sb!di:code-location-form-number loc)
1037 (sfcache-last-form-retrieved cache))
1038 (and (sfcache-last-location-retrieved cache)
1039 (sb!di:code-location=
1041 (sfcache-last-location-retrieved cache)))))
1043 (values (get-source-form loc context cache) t)))
1045 ;;;; stuff to use debugging info to augment the disassembly
1047 (defun code-fun-map (code)
1048 (declare (type sb!kernel:code-component code))
1049 (sb!c::compiled-debug-info-fun-map (sb!kernel:%code-debug-info code)))
1051 (defstruct (location-group (:copier nil))
1052 (locations #() :type (vector (or list fixnum))))
1054 (defstruct (storage-info (:copier nil))
1055 (groups nil :type list) ; alist of (name . location-group)
1056 (debug-vars #() :type vector))
1058 ;;; Return the vector of DEBUG-VARs currently associated with DSTATE.
1059 (defun dstate-debug-vars (dstate)
1060 (declare (type disassem-state dstate))
1061 (storage-info-debug-vars (seg-storage-info (dstate-segment dstate))))
1063 ;;; Given the OFFSET of a location within the location-group called
1064 ;;; LG-NAME, see whether there's a current mapping to a source
1065 ;;; variable in DSTATE, and if so, return the offset of that variable
1066 ;;; in the current debug-var vector.
1067 (defun find-valid-storage-location (offset lg-name dstate)
1068 (declare (type offset offset)
1069 (type symbol lg-name)
1070 (type disassem-state dstate))
1071 (let* ((storage-info
1072 (seg-storage-info (dstate-segment dstate)))
1075 (cdr (assoc lg-name (storage-info-groups storage-info)))))
1077 (dstate-current-valid-locations dstate)))
1079 (not (null currently-valid))
1080 (let ((locations (location-group-locations location-group)))
1081 (and (< offset (length locations))
1082 (let ((used-by (aref locations offset)))
1084 (let ((debug-var-num
1088 (zerop (bit currently-valid used-by)))
1094 (bit currently-valid num)))
1099 ;; Found a valid storage reference!
1100 ;; can't use it again until it's revalidated...
1101 (setf (bit (dstate-current-valid-locations
1108 ;;; Return a new vector which has the same contents as the old one
1109 ;;; VEC, plus new cells (for a total size of NEW-LEN). The additional
1110 ;;; elements are initialized to INITIAL-ELEMENT.
1111 (defun grow-vector (vec new-len &optional initial-element)
1112 (declare (type vector vec)
1113 (type fixnum new-len))
1115 (make-sequence `(vector ,(array-element-type vec) ,new-len)
1117 :initial-element initial-element)))
1118 (dotimes (i (length vec))
1119 (setf (aref new i) (aref vec i)))
1122 ;;; Return a STORAGE-INFO struction describing the object-to-source
1123 ;;; variable mappings from DEBUG-FUN.
1124 (defun storage-info-for-debug-fun (debug-fun)
1125 (declare (type sb!di:debug-fun debug-fun))
1126 (let ((sc-vec sb!c::*backend-sc-numbers*)
1128 (debug-vars (sb!di::debug-fun-debug-vars
1131 (dotimes (debug-var-offset
1133 (make-storage-info :groups groups
1134 :debug-vars debug-vars))
1135 (let ((debug-var (aref debug-vars debug-var-offset)))
1137 (format t ";;; At offset ~W: ~S~%" debug-var-offset debug-var)
1139 (sb!di::compiled-debug-var-sc-offset debug-var))
1142 (sb!c:sc-sb (aref sc-vec
1143 (sb!c:sc-offset-scn sc-offset))))))
1145 (format t ";;; SET: ~S[~W]~%"
1146 sb-name (sb!c:sc-offset-offset sc-offset))
1147 (unless (null sb-name)
1148 (let ((group (cdr (assoc sb-name groups))))
1150 (setf group (make-location-group))
1151 (push `(,sb-name . ,group) groups))
1152 (let* ((locations (location-group-locations group))
1153 (length (length locations))
1154 (offset (sb!c:sc-offset-offset sc-offset)))
1155 (when (>= offset length)
1157 (grow-vector locations
1161 (location-group-locations group)
1163 (let ((already-there (aref locations offset)))
1164 (cond ((null already-there)
1165 (setf (aref locations offset) debug-var-offset))
1166 ((eql already-there debug-var-offset))
1168 (if (listp already-there)
1169 (pushnew debug-var-offset
1170 (aref locations offset))
1171 (setf (aref locations offset)
1172 (list debug-var-offset
1177 (defun source-available-p (debug-fun)
1179 (sb!di:do-debug-fun-blocks (block debug-fun)
1180 (declare (ignore block))
1182 (sb!di:no-debug-blocks () nil)))
1184 (defun print-block-boundary (stream dstate)
1185 (let ((os (dstate-output-state dstate)))
1186 (when (not (eq os :beginning))
1187 (when (not (eq os :block-boundary))
1189 (setf (dstate-output-state dstate)
1192 ;;; Add hooks to track the source code in SEGMENT during disassembly.
1193 ;;; SFCACHE can be either NIL or it can be a SOURCE-FORM-CACHE
1194 ;;; structure, in which case it is used to cache forms from files.
1195 (defun add-source-tracking-hooks (segment debug-fun &optional sfcache)
1196 (declare (type segment segment)
1197 (type (or null sb!di:debug-fun) debug-fun)
1198 (type (or null source-form-cache) sfcache))
1199 (let ((last-block-pc -1))
1200 (flet ((add-hook (pc fun &optional before-address)
1201 (push (make-offs-hook
1202 :offset pc ;; ### FIX to account for non-zero offs in code
1204 :before-address before-address)
1205 (seg-hooks segment))))
1207 (sb!di:do-debug-fun-blocks (block debug-fun)
1208 (let ((first-location-in-block-p t))
1209 (sb!di:do-debug-block-locations (loc block)
1210 (let ((pc (sb!di::compiled-code-location-pc loc)))
1212 ;; Put blank lines in at block boundaries
1213 (when (and first-location-in-block-p
1214 (/= pc last-block-pc))
1215 (setf first-location-in-block-p nil)
1217 (lambda (stream dstate)
1218 (print-block-boundary stream dstate))
1220 (setf last-block-pc pc))
1222 ;; Print out corresponding source; this information is not
1223 ;; all that accurate, but it's better than nothing
1224 (unless (zerop (sb!di:code-location-form-number loc))
1225 (multiple-value-bind (form new)
1226 (get-different-source-form loc 0 sfcache)
1228 (let ((at-block-begin (= pc last-block-pc)))
1231 (lambda (stream dstate)
1232 (declare (ignore dstate))
1234 (unless at-block-begin
1236 (format stream ";;; [~W] "
1237 (sb!di:code-location-form-number
1239 (prin1-short form stream)
1244 ;; Keep track of variable live-ness as best we can.
1246 (copy-seq (sb!di::compiled-code-location-live-set
1250 (lambda (stream dstate)
1251 (declare (ignore stream))
1252 (setf (dstate-current-valid-locations dstate)
1255 (note (lambda (stream)
1256 (let ((*print-length* nil))
1257 (format stream "live set: ~S"
1261 (sb!di:no-debug-blocks () nil)))))
1263 (defun add-debugging-hooks (segment debug-fun &optional sfcache)
1265 (setf (seg-storage-info segment)
1266 (storage-info-for-debug-fun debug-fun))
1267 (add-source-tracking-hooks segment debug-fun sfcache)
1268 (let ((kind (sb!di:debug-fun-kind debug-fun)))
1269 (flet ((add-new-hook (n)
1270 (push (make-offs-hook
1272 :fun (lambda (stream dstate)
1273 (declare (ignore stream))
1275 (seg-hooks segment))))
1279 (add-new-hook "no-arg-parsing entry point"))
1281 (add-new-hook (lambda (stream)
1282 (format stream "~S entry point" kind)))))))))
1284 ;;; Return a list of the segments of memory containing machine code
1285 ;;; instructions for FUNCTION.
1286 (defun get-fun-segments (function)
1287 (declare (type compiled-function function))
1288 (let* ((code (fun-code function))
1289 (fun-map (code-fun-map code))
1290 (fname (sb!kernel:%simple-fun-name function))
1291 (sfcache (make-source-form-cache)))
1292 (let ((first-block-seen-p nil)
1293 (nil-block-seen-p nil)
1295 (last-debug-fun nil)
1297 (flet ((add-seg (offs len df)
1299 (push (make-code-segment code offs len
1301 :source-form-cache sfcache)
1303 (dotimes (fmap-index (length fun-map))
1304 (let ((fmap-entry (aref fun-map fmap-index)))
1305 (etypecase fmap-entry
1307 (when first-block-seen-p
1308 (add-seg last-offset
1309 (- fmap-entry last-offset)
1311 (setf last-debug-fun nil))
1312 (setf last-offset fmap-entry))
1313 (sb!c::compiled-debug-fun
1314 (let ((name (sb!c::compiled-debug-fun-name fmap-entry))
1315 (kind (sb!c::compiled-debug-fun-kind fmap-entry)))
1317 (format t ";;; SAW ~S ~S ~S,~S ~W,~W~%"
1318 name kind first-block-seen-p nil-block-seen-p
1320 (sb!c::compiled-debug-fun-start-pc fmap-entry))
1321 (cond (#+nil (eq last-offset fun-offset)
1322 (and (equal name fname) (not first-block-seen-p))
1323 (setf first-block-seen-p t))
1324 ((eq kind :external)
1325 (when first-block-seen-p
1328 (when nil-block-seen-p
1330 (when first-block-seen-p
1331 (setf nil-block-seen-p t))))
1332 (setf last-debug-fun
1333 (sb!di::make-compiled-debug-fun fmap-entry code)))))))
1334 (let ((max-offset (code-inst-area-length code)))
1335 (when (and first-block-seen-p last-debug-fun)
1336 (add-seg last-offset
1337 (- max-offset last-offset)
1340 (let ((offs (fun-insts-offset function)))
1342 (make-code-segment code offs (- max-offset offs))))
1343 (nreverse segments)))))))
1345 ;;; Return a list of the segments of memory containing machine code
1346 ;;; instructions for the code-component CODE. If START-OFFSET and/or
1347 ;;; LENGTH is supplied, only that part of the code-segment is used
1348 ;;; (but these are constrained to lie within the code-segment).
1349 (defun get-code-segments (code
1352 (length (code-inst-area-length code)))
1353 (declare (type sb!kernel:code-component code)
1354 (type offset start-offset)
1355 (type disassem-length length))
1356 (let ((segments nil))
1358 (let ((fun-map (code-fun-map code))
1359 (sfcache (make-source-form-cache)))
1360 (let ((last-offset 0)
1361 (last-debug-fun nil))
1362 (flet ((add-seg (offs len df)
1363 (let* ((restricted-offs
1364 (min (max start-offset offs)
1365 (+ start-offset length)))
1367 (- (min (max start-offset (+ offs len))
1368 (+ start-offset length))
1370 (when (> restricted-len 0)
1371 (push (make-code-segment code
1372 restricted-offs restricted-len
1374 :source-form-cache sfcache)
1376 (dotimes (fun-map-index (length fun-map))
1377 (let ((fun-map-entry (aref fun-map fun-map-index)))
1378 (etypecase fun-map-entry
1380 (add-seg last-offset (- fun-map-entry last-offset)
1382 (setf last-debug-fun nil)
1383 (setf last-offset fun-map-entry))
1384 (sb!c::compiled-debug-fun
1385 (setf last-debug-fun
1386 (sb!di::make-compiled-debug-fun fun-map-entry
1388 (when last-debug-fun
1389 (add-seg last-offset
1390 (- (code-inst-area-length code) last-offset)
1391 last-debug-fun))))))
1393 (make-code-segment code start-offset length)
1394 (nreverse segments))))
1396 ;;; Return two values: the amount by which the last instruction in the
1397 ;;; segment goes past the end of the segment, and the offset of the
1398 ;;; end of the segment from the beginning of that instruction. If all
1399 ;;; instructions fit perfectly, return 0 and 0.
1400 (defun segment-overflow (segment dstate)
1401 (declare (type segment segment)
1402 (type disassem-state dstate))
1403 (let ((seglen (seg-length segment))
1405 (map-segment-instructions (lambda (chunk inst)
1406 (declare (ignore chunk inst))
1407 (setf last-start (dstate-cur-offs dstate)))
1410 (values (- (dstate-cur-offs dstate) seglen)
1411 (- seglen last-start))))
1413 ;;; Compute labels for all the memory segments in SEGLIST and adds
1414 ;;; them to DSTATE. It's important to call this function with all the
1415 ;;; segments you're interested in, so that it can find references from
1417 (defun label-segments (seglist dstate)
1418 (declare (type list seglist)
1419 (type disassem-state dstate))
1420 (dolist (seg seglist)
1421 (add-segment-labels seg dstate))
1422 ;; Now remove any labels that don't point anywhere in the segments
1424 (setf (dstate-labels dstate)
1425 (remove-if (lambda (lab)
1428 (let ((start (seg-virtual-location seg)))
1431 (+ start (seg-length seg)))))
1433 (dstate-labels dstate))))
1435 ;;; Disassemble the machine code instructions in SEGMENT to STREAM.
1436 (defun disassemble-segment (segment stream dstate)
1437 (declare (type segment segment)
1438 (type stream stream)
1439 (type disassem-state dstate))
1440 (let ((*print-pretty* nil)) ; otherwise the pp conses hugely
1441 (number-labels dstate)
1442 (map-segment-instructions
1443 (lambda (chunk inst)
1444 (declare (type dchunk chunk) (type instruction inst))
1445 (let ((printer (inst-printer inst)))
1447 (funcall printer chunk inst stream dstate))))
1452 ;;; Disassemble the machine code instructions in each memory segment
1453 ;;; in SEGMENTS in turn to STREAM.
1454 (defun disassemble-segments (segments stream dstate)
1455 (declare (type list segments)
1456 (type stream stream)
1457 (type disassem-state dstate))
1458 (unless (null segments)
1459 (let ((first (car segments))
1460 (last (car (last segments))))
1461 (set-location-printing-range dstate
1462 (seg-virtual-location first)
1463 (- (+ (seg-virtual-location last)
1465 (seg-virtual-location first)))
1466 (setf (dstate-output-state dstate) :beginning)
1467 (dolist (seg segments)
1468 (disassemble-segment seg stream dstate)))))
1470 ;;;; top level functions
1472 ;;; Disassemble the machine code instructions for FUNCTION.
1473 (defun disassemble-fun (fun &key
1474 (stream *standard-output*)
1476 (declare (type compiled-function fun)
1477 (type stream stream)
1478 (type (member t nil) use-labels))
1479 (let* ((dstate (make-dstate))
1480 (segments (get-fun-segments fun)))
1482 (label-segments segments dstate))
1483 (disassemble-segments segments stream dstate)))
1485 ;;; FIXME: We probably don't need this any more now that there are
1486 ;;; no interpreted functions, only compiled ones.
1487 (defun compile-function-lambda-expr (function)
1488 (declare (type function function))
1489 (multiple-value-bind (lambda closurep name)
1490 (function-lambda-expression function)
1491 (declare (ignore name))
1493 (error "can't compile a lexical closure"))
1494 (compile nil lambda)))
1496 (defun valid-extended-function-designator-for-disassemble-p (thing)
1497 (cond ((legal-fun-name-p thing)
1498 (compiled-fun-or-lose (fdefinition thing) thing))
1500 ((sb!eval:interpreted-function-p thing)
1501 (compile nil thing))
1505 (eq (car thing) 'lambda))
1506 (compile nil thing))
1509 (defun compiled-fun-or-lose (thing &optional (name thing))
1510 (let ((fun (valid-extended-function-designator-for-disassemble-p thing)))
1513 (error 'simple-type-error
1515 :expected-type '(satisfies valid-extended-function-designator-for-disassemble-p)
1516 :format-control "can't make a compiled function from ~S"
1517 :format-arguments (list name)))))
1519 (defun disassemble (object &key
1520 (stream *standard-output*)
1523 "Disassemble the compiled code associated with OBJECT, which can be a
1524 function, a lambda expression, or a symbol with a function definition. If
1525 it is not already compiled, the compiler is called to produce something to
1527 (declare (type (or function symbol cons) object)
1528 (type (or (member t) stream) stream)
1529 (type (member t nil) use-labels))
1530 (pprint-logical-block (*standard-output* nil :per-line-prefix "; ")
1531 (disassemble-fun (compiled-fun-or-lose object)
1533 :use-labels use-labels)
1536 ;;; Disassembles the given area of memory starting at ADDRESS and
1537 ;;; LENGTH long. Note that if CODE-COMPONENT is NIL and this memory
1538 ;;; could move during a GC, you'd better disable it around the call to
1540 (defun disassemble-memory (address
1543 (stream *standard-output*)
1546 (declare (type (or address sb!sys:system-area-pointer) address)
1547 (type disassem-length length)
1548 (type stream stream)
1549 (type (or null sb!kernel:code-component) code-component)
1550 (type (member t nil) use-labels))
1552 (if (sb!sys:system-area-pointer-p address)
1553 (sb!sys:sap-int address)
1555 (dstate (make-dstate))
1561 (sb!kernel:code-instructions code-component)))))
1562 (when (or (< code-offs 0)
1563 (> code-offs (code-inst-area-length code-component)))
1564 (error "address ~X not in the code component ~S"
1565 address code-component))
1566 (get-code-segments code-component code-offs length))
1567 (list (make-memory-segment address length)))))
1569 (label-segments segments dstate))
1570 (disassemble-segments segments stream dstate)))
1572 ;;; Disassemble the machine code instructions associated with
1573 ;;; CODE-COMPONENT (this may include multiple entry points).
1574 (defun disassemble-code-component (code-component &key
1575 (stream *standard-output*)
1577 (declare (type (or null sb!kernel:code-component compiled-function)
1579 (type stream stream)
1580 (type (member t nil) use-labels))
1581 (let* ((code-component
1582 (if (functionp code-component)
1583 (fun-code code-component)
1585 (dstate (make-dstate))
1586 (segments (get-code-segments code-component)))
1588 (label-segments segments dstate))
1589 (disassemble-segments segments stream dstate)))
1591 ;;; code for making useful segments from arbitrary lists of code-blocks
1593 ;;; the maximum size of an instruction. Note that this includes
1594 ;;; pseudo-instructions like error traps with their associated
1595 ;;; operands, so it should be big enough to include them, i.e. it's
1596 ;;; not just 4 on a risc machine!
1597 (defconstant max-instruction-size 16)
1599 (defun add-block-segments (seg-code-block
1604 (declare (type list seglist)
1605 (type integer location)
1606 (type (or null (vector (unsigned-byte 8))) connecting-vec)
1607 (type disassem-state dstate))
1608 (flet ((addit (seg overflow)
1609 (let ((length (+ (seg-length seg) overflow)))
1611 (setf (seg-length seg) length)
1612 (incf location length)
1613 (push seg seglist)))))
1614 (let ((connecting-overflow 0)
1615 (amount (length seg-code-block)))
1616 (when connecting-vec
1617 ;; Tack on some of the new block to the old overflow vector.
1618 (let* ((beginning-of-block-amount
1619 (if seg-code-block (min max-instruction-size amount) 0))
1623 '(vector (unsigned-byte 8))
1625 (subseq seg-code-block 0 beginning-of-block-amount))
1627 (when (and (< (length connecting-vec) max-instruction-size)
1628 (not (null seg-code-block)))
1629 (return-from add-block-segments
1630 ;; We want connecting vectors to be large enough to hold
1631 ;; any instruction, and since the current seg-code-block
1632 ;; wasn't large enough to do this (and is now entirely
1633 ;; on the end of the overflow-vector), just save it for
1635 (values seglist location connecting-vec)))
1636 (when (> (length connecting-vec) 0)
1638 (make-vector-segment connecting-vec
1640 (- (length connecting-vec)
1641 beginning-of-block-amount)
1642 :virtual-location location)))
1643 (setf connecting-overflow (segment-overflow seg dstate))
1644 (addit seg connecting-overflow)))))
1645 (cond ((null seg-code-block)
1646 ;; nothing more to add
1647 (values seglist location nil))
1648 ((< (- amount connecting-overflow) max-instruction-size)
1649 ;; We can't create a segment with the minimum size
1650 ;; required for an instruction, so just keep on accumulating
1651 ;; in the overflow vector for the time-being.
1654 (subseq seg-code-block connecting-overflow amount)))
1656 ;; Put as much as we can into a new segment, and the rest
1657 ;; into the overflow-vector.
1658 (let* ((initial-length
1659 (- amount connecting-overflow max-instruction-size))
1661 (make-vector-segment seg-code-block
1664 :virtual-location location))
1666 (segment-overflow seg dstate)))
1667 (addit seg overflow)
1670 (subseq seg-code-block
1671 (+ connecting-overflow (seg-length seg))
1674 ;;;; code to disassemble assembler segments
1676 (defun assem-segment-to-disassem-segments (assem-segment dstate)
1677 (declare (type sb!assem:segment assem-segment)
1678 (type disassem-state dstate))
1680 (disassem-segments nil)
1681 (connecting-vec nil))
1682 (sb!assem:on-segment-contents-vectorly
1684 (lambda (seg-code-block)
1685 (multiple-value-setq (disassem-segments location connecting-vec)
1686 (add-block-segments seg-code-block
1691 (when connecting-vec
1692 (setf disassem-segments
1693 (add-block-segments nil
1698 (sort disassem-segments #'< :key #'seg-virtual-location)))
1700 ;;; Disassemble the machine code instructions associated with
1701 ;;; ASSEM-SEGMENT (of type assem:segment).
1702 (defun disassemble-assem-segment (assem-segment stream)
1703 (declare (type sb!assem:segment assem-segment)
1704 (type stream stream))
1705 (let* ((dstate (make-dstate))
1707 (assem-segment-to-disassem-segments assem-segment dstate)))
1708 (label-segments disassem-segments dstate)
1709 (disassemble-segments disassem-segments stream dstate)))
1711 ;;; routines to find things in the Lisp environment
1713 ;;; an alist of (SYMBOL-SLOT-OFFSET . ACCESS-FUN-NAME) for slots
1714 ;;; in a symbol object that we know about
1715 (defparameter *grokked-symbol-slots*
1716 (sort `((,sb!vm:symbol-value-slot . symbol-value)
1717 (,sb!vm:symbol-plist-slot . symbol-plist)
1718 (,sb!vm:symbol-name-slot . symbol-name)
1719 (,sb!vm:symbol-package-slot . symbol-package))
1723 ;;; Given ADDRESS, try and figure out if which slot of which symbol is
1724 ;;; being referred to. Of course we can just give up, so it's not a
1725 ;;; big deal... Return two values, the symbol and the name of the
1726 ;;; access function of the slot.
1727 (defun grok-symbol-slot-ref (address)
1728 (declare (type address address))
1729 (if (not (aligned-p address sb!vm:n-word-bytes))
1731 (do ((slots-tail *grokked-symbol-slots* (cdr slots-tail)))
1734 (let* ((field (car slots-tail))
1735 (slot-offset (words-to-bytes (car field)))
1736 (maybe-symbol-addr (- address slot-offset))
1738 (sb!kernel:make-lisp-obj
1739 (+ maybe-symbol-addr sb!vm:other-pointer-lowtag))))
1740 (when (symbolp maybe-symbol)
1741 (return (values maybe-symbol (cdr field))))))))
1743 (defvar *address-of-nil-object* (sb!kernel:get-lisp-obj-address nil))
1745 ;;; Given a BYTE-OFFSET from NIL, try and figure out which slot of
1746 ;;; which symbol is being referred to. Of course we can just give up,
1747 ;;; so it's not a big deal... Return two values, the symbol and the
1748 ;;; access function.
1749 (defun grok-nil-indexed-symbol-slot-ref (byte-offset)
1750 (declare (type offset byte-offset))
1751 (grok-symbol-slot-ref (+ *address-of-nil-object* byte-offset)))
1753 ;;; Return the Lisp object located BYTE-OFFSET from NIL.
1754 (defun get-nil-indexed-object (byte-offset)
1755 (declare (type offset byte-offset))
1756 (sb!kernel:make-lisp-obj (+ *address-of-nil-object* byte-offset)))
1758 ;;; Return two values; the Lisp object located at BYTE-OFFSET in the
1759 ;;; constant area of the code-object in the current segment and T, or
1760 ;;; NIL and NIL if there is no code-object in the current segment.
1761 (defun get-code-constant (byte-offset dstate)
1763 (declare (type offset byte-offset)
1764 (type disassem-state dstate))
1765 (let ((code (seg-code (dstate-segment dstate))))
1768 (sb!kernel:code-header-ref code
1770 sb!vm:other-pointer-lowtag)
1771 (- sb!vm:word-shift)))
1775 (defun get-code-constant-absolute (addr dstate)
1776 (declare (type address addr))
1777 (declare (type disassem-state dstate))
1778 (let ((code (seg-code (dstate-segment dstate))))
1780 (return-from get-code-constant-absolute (values nil nil)))
1781 (let ((code-size (ash (sb!kernel:get-header-data code) sb!vm:word-shift)))
1782 (sb!sys:without-gcing
1783 (let ((code-addr (- (sb!kernel:get-lisp-obj-address code)
1784 sb!vm:other-pointer-lowtag)))
1785 (if (or (< addr code-addr) (>= addr (+ code-addr code-size)))
1787 (values (sb!kernel:code-header-ref
1789 (ash (- addr code-addr) (- sb!vm:word-shift)))
1792 (defvar *assembler-routines-by-addr* nil)
1794 (defvar *foreign-symbols-by-addr* nil)
1796 ;;; Build an address-name hash-table from the name-address hash
1797 (defun invert-address-hash (htable &optional (addr-hash (make-hash-table)))
1798 (maphash (lambda (name address)
1799 (setf (gethash address addr-hash) name))
1803 ;;; Return the name of the primitive Lisp assembler routine or foreign
1804 ;;; symbol located at ADDRESS, or NIL if there isn't one.
1805 (defun find-assembler-routine (address)
1806 (declare (type address address))
1807 (when (null *assembler-routines-by-addr*)
1808 (setf *assembler-routines-by-addr*
1809 (invert-address-hash sb!fasl:*assembler-routines*))
1810 (setf *assembler-routines-by-addr*
1811 (invert-address-hash sb!sys:*static-foreign-symbols*
1812 *assembler-routines-by-addr*)))
1813 (gethash address *assembler-routines-by-addr*))
1815 ;;;; some handy function for machine-dependent code to use...
1817 #!-sb-fluid (declaim (maybe-inline sap-ref-int read-suffix))
1819 (defun sap-ref-int (sap offset length byte-order)
1820 (declare (type sb!sys:system-area-pointer sap)
1821 (type (unsigned-byte 16) offset)
1822 (type (member 1 2 4 8) length)
1823 (type (member :little-endian :big-endian) byte-order)
1824 (optimize (speed 3) (safety 0)))
1826 (1 (sb!sys:sap-ref-8 sap offset))
1827 (2 (if (eq byte-order :big-endian)
1828 (+ (ash (sb!sys:sap-ref-8 sap offset) 8)
1829 (sb!sys:sap-ref-8 sap (+ offset 1)))
1830 (+ (ash (sb!sys:sap-ref-8 sap (+ offset 1)) 8)
1831 (sb!sys:sap-ref-8 sap offset))))
1832 (4 (if (eq byte-order :big-endian)
1833 (+ (ash (sb!sys:sap-ref-8 sap offset) 24)
1834 (ash (sb!sys:sap-ref-8 sap (+ 1 offset)) 16)
1835 (ash (sb!sys:sap-ref-8 sap (+ 2 offset)) 8)
1836 (sb!sys:sap-ref-8 sap (+ 3 offset)))
1837 (+ (sb!sys:sap-ref-8 sap offset)
1838 (ash (sb!sys:sap-ref-8 sap (+ 1 offset)) 8)
1839 (ash (sb!sys:sap-ref-8 sap (+ 2 offset)) 16)
1840 (ash (sb!sys:sap-ref-8 sap (+ 3 offset)) 24))))
1841 (8 (if (eq byte-order :big-endian)
1842 (+ (ash (sb!sys:sap-ref-8 sap offset) 56)
1843 (ash (sb!sys:sap-ref-8 sap (+ 1 offset)) 48)
1844 (ash (sb!sys:sap-ref-8 sap (+ 2 offset)) 40)
1845 (ash (sb!sys:sap-ref-8 sap (+ 3 offset)) 32)
1846 (ash (sb!sys:sap-ref-8 sap (+ 4 offset)) 24)
1847 (ash (sb!sys:sap-ref-8 sap (+ 5 offset)) 16)
1848 (ash (sb!sys:sap-ref-8 sap (+ 6 offset)) 8)
1849 (sb!sys:sap-ref-8 sap (+ 7 offset)))
1850 (+ (sb!sys:sap-ref-8 sap offset)
1851 (ash (sb!sys:sap-ref-8 sap (+ 1 offset)) 8)
1852 (ash (sb!sys:sap-ref-8 sap (+ 2 offset)) 16)
1853 (ash (sb!sys:sap-ref-8 sap (+ 3 offset)) 24)
1854 (ash (sb!sys:sap-ref-8 sap (+ 4 offset)) 32)
1855 (ash (sb!sys:sap-ref-8 sap (+ 5 offset)) 40)
1856 (ash (sb!sys:sap-ref-8 sap (+ 6 offset)) 48)
1857 (ash (sb!sys:sap-ref-8 sap (+ 7 offset)) 56))))))
1859 (defun read-suffix (length dstate)
1860 (declare (type (member 8 16 32 64) length)
1861 (type disassem-state dstate)
1862 (optimize (speed 3) (safety 0)))
1863 (let ((length (ecase length (8 1) (16 2) (32 4) (64 8))))
1864 (declare (type (unsigned-byte 4) length))
1866 (sap-ref-int (dstate-segment-sap dstate)
1867 (dstate-next-offs dstate)
1869 (dstate-byte-order dstate))
1870 (incf (dstate-next-offs dstate) length))))
1872 ;;;; optional routines to make notes about code
1874 ;;; Store NOTE (which can be either a string or a function with a
1875 ;;; single stream argument) to be printed as an end-of-line comment
1876 ;;; after the current instruction is disassembled.
1877 (defun note (note dstate)
1878 (declare (type (or string function) note)
1879 (type disassem-state dstate))
1880 (push note (dstate-notes dstate)))
1882 (defun prin1-short (thing stream)
1883 (with-print-restrictions
1884 (prin1 thing stream)))
1886 (defun prin1-quoted-short (thing stream)
1887 (if (self-evaluating-p thing)
1888 (prin1-short thing stream)
1889 (prin1-short `',thing stream)))
1891 ;;; Store a note about the lisp constant located BYTE-OFFSET bytes
1892 ;;; from the current code-component, to be printed as an end-of-line
1893 ;;; comment after the current instruction is disassembled.
1894 (defun note-code-constant (byte-offset dstate)
1895 (declare (type offset byte-offset)
1896 (type disassem-state dstate))
1897 (multiple-value-bind (const valid)
1898 (get-code-constant byte-offset dstate)
1900 (note (lambda (stream)
1901 (prin1-quoted-short const stream))
1905 ;;; Store a note about the lisp constant located at ADDR in the
1906 ;;; current code-component, to be printed as an end-of-line comment
1907 ;;; after the current instruction is disassembled.
1908 (defun note-code-constant-absolute (addr dstate)
1909 (declare (type address addr)
1910 (type disassem-state dstate))
1911 (multiple-value-bind (const valid)
1912 (get-code-constant-absolute addr dstate)
1914 (note (lambda (stream)
1915 (prin1-quoted-short const stream))
1917 (values const valid)))
1919 ;;; If the memory address located NIL-BYTE-OFFSET bytes from the
1920 ;;; constant NIL is a valid slot in a symbol, store a note describing
1921 ;;; which symbol and slot, to be printed as an end-of-line comment
1922 ;;; after the current instruction is disassembled. Returns non-NIL iff
1923 ;;; a note was recorded.
1924 (defun maybe-note-nil-indexed-symbol-slot-ref (nil-byte-offset dstate)
1925 (declare (type offset nil-byte-offset)
1926 (type disassem-state dstate))
1927 (multiple-value-bind (symbol access-fun)
1928 (grok-nil-indexed-symbol-slot-ref nil-byte-offset)
1930 (note (lambda (stream)
1931 (prin1 (if (eq access-fun 'symbol-value)
1933 `(,access-fun ',symbol))
1938 ;;; If the memory address located NIL-BYTE-OFFSET bytes from the
1939 ;;; constant NIL is a valid lisp object, store a note describing which
1940 ;;; symbol and slot, to be printed as an end-of-line comment after the
1941 ;;; current instruction is disassembled. Returns non-NIL iff a note
1943 (defun maybe-note-nil-indexed-object (nil-byte-offset dstate)
1944 (declare (type offset nil-byte-offset)
1945 (type disassem-state dstate))
1946 (let ((obj (get-nil-indexed-object nil-byte-offset)))
1947 (note (lambda (stream)
1948 (prin1-quoted-short obj stream))
1952 ;;; If ADDRESS is the address of a primitive assembler routine or
1953 ;;; foreign symbol, store a note describing which one, to be printed
1954 ;;; as an end-of-line comment after the current instruction is
1955 ;;; disassembled. Returns non-NIL iff a note was recorded. If
1956 ;;; NOTE-ADDRESS-P is non-NIL, a note of the address is also made.
1957 (defun maybe-note-assembler-routine (address note-address-p dstate)
1958 (declare (type disassem-state dstate))
1959 (unless (typep address 'address)
1960 (return-from maybe-note-assembler-routine nil))
1962 (find-assembler-routine address)
1964 (sb!sys:sap-foreign-symbol (sb!sys:int-sap address)))))
1966 (note (lambda (stream)
1968 (format stream "#x~8,'0x: ~a" address name)
1969 (princ name stream)))
1973 ;;; If there's a valid mapping from OFFSET in the storage class
1974 ;;; SC-NAME to a source variable, make a note of the source-variable
1975 ;;; name, to be printed as an end-of-line comment after the current
1976 ;;; instruction is disassembled. Returns non-NIL iff a note was
1978 (defun maybe-note-single-storage-ref (offset sc-name dstate)
1979 (declare (type offset offset)
1980 (type symbol sc-name)
1981 (type disassem-state dstate))
1982 (let ((storage-location
1983 (find-valid-storage-location offset sc-name dstate)))
1984 (when storage-location
1985 (note (lambda (stream)
1986 (princ (sb!di:debug-var-symbol
1987 (aref (storage-info-debug-vars
1988 (seg-storage-info (dstate-segment dstate)))
1994 ;;; If there's a valid mapping from OFFSET in the storage-base called
1995 ;;; SB-NAME to a source variable, make a note equating ASSOC-WITH with
1996 ;;; the source-variable name, to be printed as an end-of-line comment
1997 ;;; after the current instruction is disassembled. Returns non-NIL iff
1998 ;;; a note was recorded.
1999 (defun maybe-note-associated-storage-ref (offset sb-name assoc-with dstate)
2000 (declare (type offset offset)
2001 (type symbol sb-name)
2002 (type (or symbol string) assoc-with)
2003 (type disassem-state dstate))
2004 (let ((storage-location
2005 (find-valid-storage-location offset sb-name dstate)))
2006 (when storage-location
2007 (note (lambda (stream)
2008 (format stream "~A = ~S"
2010 (sb!di:debug-var-symbol
2011 (aref (dstate-debug-vars dstate)
2012 storage-location))))
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-inst num stream dstate)
2062 (print-bytes num stream dstate)
2063 (incf (dstate-cur-offs dstate) num)
2065 (note note dstate)))))
2067 (emit-err-arg (symbol-name (get-internal-error-name errnum)))
2068 (dolist (sc-offs sc-offsets)
2069 (emit-err-arg (get-sc-name sc-offs)))))
2070 (incf (dstate-next-offs dstate)