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 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 fun))
293 (defun fun-address (fun)
294 (declare (type compiled-function fun))
295 (ecase (sb!kernel:widetag-of fun)
296 (#.sb!vm:simple-fun-header-widetag
297 (- (sb!kernel:get-lisp-obj-address fun) sb!vm:fun-pointer-lowtag))
298 (#.sb!vm:closure-header-widetag
299 (fun-address (sb!kernel:%closure-fun fun)))
300 (#.sb!vm:funcallable-instance-header-widetag
301 (fun-address (sb!kernel:funcallable-instance-fun fun)))))
303 ;;; the offset of FUNCTION from the start of its code-component's
305 (defun fun-insts-offset (function)
306 (declare (type compiled-function function))
307 (- (fun-address function)
308 (sb!sys:sap-int (sb!kernel:code-instructions (fun-code function)))))
310 ;;; the offset of FUNCTION from the start of its code-component
311 (defun fun-offset (function)
312 (declare (type compiled-function function))
313 (words-to-bytes (sb!kernel:get-closure-length function)))
315 ;;;; operations on code-components (which hold the instructions for
316 ;;;; one or more functions)
318 ;;; Return the length of the instruction area in CODE-COMPONENT.
319 (defun code-inst-area-length (code-component)
320 (declare (type sb!kernel:code-component code-component))
321 (sb!kernel:code-header-ref code-component
322 sb!vm:code-trace-table-offset-slot))
324 ;;; Return the address of the instruction area in CODE-COMPONENT.
325 (defun code-inst-area-address (code-component)
326 (declare (type sb!kernel:code-component code-component))
327 (sb!sys:sap-int (sb!kernel:code-instructions code-component)))
329 ;;; unused as of sbcl-0.pre7.129
331 ;;; Return the first function in CODE-COMPONENT.
332 (defun code-first-function (code-component)
333 (declare (type sb!kernel:code-component code-component))
334 (sb!kernel:code-header-ref code-component
335 sb!vm:code-trace-table-offset-slot))
338 (defun segment-offs-to-code-offs (offset segment)
339 (sb!sys:without-gcing
340 (let* ((seg-base-addr (sb!sys:sap-int (funcall (seg-sap-maker segment))))
342 (logandc1 sb!vm:lowtag-mask
343 (sb!kernel:get-lisp-obj-address (seg-code segment))))
344 (addr (+ offset seg-base-addr)))
345 (declare (type address seg-base-addr code-addr addr))
346 (- addr code-addr))))
348 (defun code-offs-to-segment-offs (offset segment)
349 (sb!sys:without-gcing
350 (let* ((seg-base-addr (sb!sys:sap-int (funcall (seg-sap-maker segment))))
352 (logandc1 sb!vm:lowtag-mask
353 (sb!kernel:get-lisp-obj-address (seg-code segment))))
354 (addr (+ offset code-addr)))
355 (declare (type address seg-base-addr code-addr addr))
356 (- addr seg-base-addr))))
358 (defun code-insts-offs-to-segment-offs (offset segment)
359 (sb!sys:without-gcing
360 (let* ((seg-base-addr (sb!sys:sap-int (funcall (seg-sap-maker segment))))
362 (sb!sys:sap-int (sb!kernel:code-instructions (seg-code segment))))
363 (addr (+ offset code-insts-addr)))
364 (declare (type address seg-base-addr code-insts-addr addr))
365 (- addr seg-base-addr))))
367 (defun lra-hook (chunk stream dstate)
368 (declare (type dchunk chunk)
370 (type (or null stream) stream)
371 (type disassem-state dstate))
372 (when (and (aligned-p (+ (seg-virtual-location (dstate-segment dstate))
373 (dstate-cur-offs dstate))
374 (* 2 sb!vm:n-word-bytes))
376 (= (sb!sys:sap-ref-8 (dstate-segment-sap dstate)
377 (if (eq (dstate-byte-order dstate)
379 (dstate-cur-offs dstate)
380 (+ (dstate-cur-offs dstate)
382 sb!vm:return-pc-header-widetag))
383 (unless (null stream)
384 (princ '.lra stream))
385 (incf (dstate-next-offs dstate) lra-size))
388 ;;; Print the fun-header (entry-point) pseudo-instruction at the
389 ;;; current location in DSTATE to STREAM.
390 (defun fun-header-hook (stream dstate)
391 (declare (type (or null stream) stream)
392 (type disassem-state dstate))
393 (unless (null stream)
394 (let* ((seg (dstate-segment dstate))
395 (code (seg-code seg))
398 (segment-offs-to-code-offs (dstate-cur-offs dstate) seg)))
400 (sb!kernel:code-header-ref code
402 sb!vm:simple-fun-name-slot)))
404 (sb!kernel:code-header-ref code
406 sb!vm:simple-fun-arglist-slot)))
408 (sb!kernel:code-header-ref code
410 sb!vm:simple-fun-type-slot))))
411 (format stream ".~A ~S~:A" 'entry name args)
412 (note (lambda (stream)
413 (format stream "~:S" type)) ; use format to print NIL as ()
415 (incf (dstate-next-offs dstate)
416 (words-to-bytes sb!vm:simple-fun-code-offset)))
418 (defun alignment-hook (chunk stream dstate)
419 (declare (type dchunk chunk)
421 (type (or null stream) stream)
422 (type disassem-state dstate))
424 (+ (seg-virtual-location (dstate-segment dstate))
425 (dstate-cur-offs dstate)))
426 (alignment (dstate-alignment dstate)))
427 (unless (aligned-p location alignment)
429 (format stream "~A~Vt~W~%" '.align
430 (dstate-argument-column dstate)
432 (incf(dstate-next-offs dstate)
433 (- (align location alignment) location)))
436 (defun rewind-current-segment (dstate segment)
437 (declare (type disassem-state dstate)
438 (type segment segment))
439 (setf (dstate-segment dstate) segment)
440 (setf (dstate-cur-offs-hooks dstate)
441 (stable-sort (nreverse (copy-list (seg-hooks segment)))
443 (or (< (offs-hook-offset oh1) (offs-hook-offset oh2))
444 (and (= (offs-hook-offset oh1)
445 (offs-hook-offset oh2))
446 (offs-hook-before-address oh1)
447 (not (offs-hook-before-address oh2)))))))
448 (setf (dstate-cur-offs dstate) 0)
449 (setf (dstate-cur-labels dstate) (dstate-labels dstate)))
451 (defun call-offs-hooks (before-address stream dstate)
452 (declare (type (or null stream) stream)
453 (type disassem-state dstate))
454 (let ((cur-offs (dstate-cur-offs dstate)))
455 (setf (dstate-next-offs dstate) cur-offs)
457 (let ((next-hook (car (dstate-cur-offs-hooks dstate))))
458 (when (null next-hook)
460 (let ((hook-offs (offs-hook-offset next-hook)))
461 (when (or (> hook-offs cur-offs)
462 (and (= hook-offs cur-offs)
464 (not (offs-hook-before-address next-hook))))
466 (unless (< hook-offs cur-offs)
467 (funcall (offs-hook-fun next-hook) stream dstate))
468 (pop (dstate-cur-offs-hooks dstate))
469 (unless (= (dstate-next-offs dstate) cur-offs)
472 (defun call-fun-hooks (chunk stream dstate)
473 (let ((hooks (dstate-fun-hooks dstate))
474 (cur-offs (dstate-cur-offs dstate)))
475 (setf (dstate-next-offs dstate) cur-offs)
476 (dolist (hook hooks nil)
477 (let ((prefix-p (funcall hook chunk stream dstate)))
478 (unless (= (dstate-next-offs dstate) cur-offs)
479 (return prefix-p))))))
481 (defun handle-bogus-instruction (stream dstate)
482 (let ((alignment (dstate-alignment dstate)))
483 (unless (null stream)
484 (multiple-value-bind (words bytes)
485 (truncate alignment sb!vm:n-word-bytes)
487 (print-words words stream dstate))
489 (print-bytes bytes stream dstate))))
490 (incf (dstate-next-offs dstate) alignment)))
492 ;;; Iterate through the instructions in SEGMENT, calling FUNCTION for
493 ;;; each instruction, with arguments of CHUNK, STREAM, and DSTATE.
494 (defun map-segment-instructions (function segment dstate &optional stream)
495 (declare (type function function)
496 (type segment segment)
497 (type disassem-state dstate)
498 (type (or null stream) stream))
500 (let ((ispace (get-inst-space))
501 (prefix-p nil)) ; just processed a prefix inst
503 (rewind-current-segment dstate segment)
506 (when (>= (dstate-cur-offs dstate)
507 (seg-length (dstate-segment dstate)))
511 (setf (dstate-next-offs dstate) (dstate-cur-offs dstate))
513 (call-offs-hooks t stream dstate)
514 (unless (or prefix-p (null stream))
515 (print-current-address stream dstate))
516 (call-offs-hooks nil stream dstate)
518 (unless (> (dstate-next-offs dstate) (dstate-cur-offs dstate))
519 (sb!sys:without-gcing
520 (setf (dstate-segment-sap dstate) (funcall (seg-sap-maker segment)))
523 (sap-ref-dchunk (dstate-segment-sap dstate)
524 (dstate-cur-offs dstate)
525 (dstate-byte-order dstate))))
526 (let ((fun-prefix-p (call-fun-hooks chunk stream dstate)))
527 (if (> (dstate-next-offs dstate) (dstate-cur-offs dstate))
528 (setf prefix-p fun-prefix-p)
529 (let ((inst (find-inst chunk ispace)))
531 (handle-bogus-instruction stream dstate))
533 (setf (dstate-next-offs dstate)
534 (+ (dstate-cur-offs dstate)
537 (let ((prefilter (inst-prefilter inst))
538 (control (inst-control inst)))
540 (funcall prefilter chunk dstate))
542 (funcall function chunk inst)
544 (setf prefix-p (null (inst-printer inst)))
547 (funcall control chunk inst stream dstate))))))
550 (setf (dstate-cur-offs dstate) (dstate-next-offs dstate))
552 (unless (null stream)
554 (print-notes-and-newline stream dstate))
555 (setf (dstate-output-state dstate) nil)))))
557 ;;; Make an initial non-printing disassembly pass through DSTATE,
558 ;;; noting any addresses that are referenced by instructions in this
560 (defun add-segment-labels (segment dstate)
561 ;; add labels at the beginning with a label-number of nil; we'll notice
562 ;; later and fill them in (and sort them)
563 (declare (type disassem-state dstate))
564 (let ((labels (dstate-labels dstate)))
565 (map-segment-instructions
567 (declare (type dchunk chunk) (type instruction inst))
568 (let ((labeller (inst-labeller inst)))
570 (setf labels (funcall labeller chunk labels dstate)))))
573 (setf (dstate-labels dstate) labels)
574 ;; erase any notes that got there by accident
575 (setf (dstate-notes dstate) nil)))
577 ;;; If any labels in DSTATE have been added since the last call to
578 ;;; this function, give them label-numbers, enter them in the
579 ;;; hash-table, and make sure the label list is in sorted order.
580 (defun number-labels (dstate)
581 (let ((labels (dstate-labels dstate)))
582 (when (and labels (null (cdar labels)))
583 ;; at least one label left un-numbered
584 (setf labels (sort labels #'< :key #'car))
586 (label-hash (dstate-label-hash dstate)))
587 (dolist (label labels)
588 (when (not (null (cdr label)))
589 (setf max (max max (cdr label)))))
590 (dolist (label labels)
591 (when (null (cdr label))
593 (setf (cdr label) max)
594 (setf (gethash (car label) label-hash)
595 (format nil "L~W" max)))))
596 (setf (dstate-labels dstate) labels))))
598 ;;; Get the instruction-space, creating it if necessary.
599 (defun get-inst-space ()
600 (let ((ispace *disassem-inst-space*))
603 (maphash (lambda (name inst-flavs)
604 (declare (ignore name))
605 (dolist (flav inst-flavs)
608 (setf ispace (build-inst-space insts)))
609 (setf *disassem-inst-space* ispace))
612 ;;;; Add global hooks.
614 (defun add-offs-hook (segment addr hook)
615 (let ((entry (cons addr hook)))
616 (if (null (seg-hooks segment))
617 (setf (seg-hooks segment) (list entry))
618 (push entry (cdr (last (seg-hooks segment)))))))
620 (defun add-offs-note-hook (segment addr note)
621 (add-offs-hook segment
623 (lambda (stream dstate)
624 (declare (type (or null stream) stream)
625 (type disassem-state dstate))
627 (note note dstate)))))
629 (defun add-offs-comment-hook (segment addr comment)
630 (add-offs-hook segment
632 (lambda (stream dstate)
633 (declare (type (or null stream) stream)
636 (write-string ";;; " stream)
639 (write-string comment stream))
641 (funcall comment stream)))
644 (defun add-fun-hook (dstate function)
645 (push function (dstate-fun-hooks dstate)))
647 (defun set-location-printing-range (dstate from length)
648 (setf (dstate-addr-print-len dstate)
649 ;; 4 bits per hex digit
650 (ceiling (integer-length (logxor from (+ from length))) 4)))
652 ;;; Print the current address in DSTATE to STREAM, plus any labels that
653 ;;; correspond to it, and leave the cursor in the instruction column.
654 (defun print-current-address (stream dstate)
655 (declare (type stream stream)
656 (type disassem-state dstate))
658 (+ (seg-virtual-location (dstate-segment dstate))
659 (dstate-cur-offs dstate)))
660 (location-column-width *disassem-location-column-width*)
661 (plen (dstate-addr-print-len dstate)))
664 (setf plen location-column-width)
665 (let ((seg (dstate-segment dstate)))
666 (set-location-printing-range dstate
667 (seg-virtual-location seg)
669 (when (eq (dstate-output-state dstate) :beginning)
670 (setf plen location-column-width))
674 (setf location-column-width (+ 2 location-column-width))
677 ;; print the location
678 ;; [this is equivalent to (format stream "~V,'0x:" plen printed-value), but
679 ;; usually avoids any consing]
680 (tab0 (- location-column-width plen) stream)
681 (let* ((printed-bits (* 4 plen))
682 (printed-value (ldb (byte printed-bits 0) location))
684 (truncate (- printed-bits (integer-length printed-value)) 4)))
685 (dotimes (i leading-zeros)
686 (write-char #\0 stream))
687 (unless (zerop printed-value)
688 (write printed-value :stream stream :base 16 :radix nil))
689 (write-char #\: stream))
693 (let* ((next-label (car (dstate-cur-labels dstate)))
694 (label-location (car next-label)))
695 (when (or (null label-location) (> label-location location))
697 (unless (< label-location location)
698 (format stream " L~W:" (cdr next-label)))
699 (pop (dstate-cur-labels dstate))))
701 ;; move to the instruction column
702 (tab0 (+ location-column-width 1 label-column-width) stream)
705 (eval-when (:compile-toplevel :execute)
706 (sb!xc:defmacro with-print-restrictions (&rest body)
707 `(let ((*print-pretty* t)
713 ;;; Print a newline to STREAM, inserting any pending notes in DSTATE
714 ;;; as end-of-line comments. If there is more than one note, a
715 ;;; separate line will be used for each one.
716 (defun print-notes-and-newline (stream dstate)
717 (declare (type stream stream)
718 (type disassem-state dstate))
719 (with-print-restrictions
720 (dolist (note (dstate-notes dstate))
721 (format stream "~Vt " *disassem-note-column*)
722 (pprint-logical-block (stream nil :per-line-prefix "; ")
725 (write-string note stream))
727 (funcall note stream))))
730 (setf (dstate-notes dstate) nil)))
732 ;;; Disassemble NUM bytes to STREAM as simple `BYTE' instructions.
733 (defun print-bytes (num stream dstate)
734 (declare (type offset num)
736 (type disassem-state dstate))
737 (format stream "~A~Vt" 'BYTE (dstate-argument-column dstate))
738 (let ((sap (dstate-segment-sap dstate))
739 (start-offs (dstate-cur-offs dstate)))
742 (write-string ", " stream))
743 (format stream "#X~2,'0x" (sb!sys:sap-ref-8 sap (+ offs start-offs))))))
745 ;;; Disassemble NUM machine-words to STREAM as simple `WORD' instructions.
746 (defun print-words (num stream dstate)
747 (declare (type offset num)
749 (type disassem-state dstate))
750 (format stream "~A~Vt" 'WORD (dstate-argument-column dstate))
751 (let ((sap (dstate-segment-sap dstate))
752 (start-offs (dstate-cur-offs dstate))
753 (byte-order (dstate-byte-order dstate)))
754 (dotimes (word-offs num)
755 (unless (zerop word-offs)
756 (write-string ", " stream))
757 (let ((word 0) (bit-shift 0))
758 (dotimes (byte-offs sb!vm:n-word-bytes)
763 (* word-offs sb!vm:n-word-bytes)
766 (if (eq byte-order :big-endian)
767 (+ (ash word sb!vm:n-byte-bits) byte)
768 (+ word (ash byte bit-shift))))
769 (incf bit-shift sb!vm:n-byte-bits)))
770 (format stream "#X~V,'0X" (ash sb!vm:n-word-bits -2) word)))))
772 (defvar *default-dstate-hooks* (list #'lra-hook))
774 ;;; Make a disassembler-state object.
775 (defun make-dstate (&optional (fun-hooks *default-dstate-hooks*))
777 (sb!sys:vector-sap (coerce #() '(vector (unsigned-byte 8)))))
778 (alignment *disassem-inst-alignment-bytes*)
780 (+ (or *disassem-opcode-column-width* 0)
781 *disassem-location-column-width*
783 label-column-width)))
785 (when (> alignment 1)
786 (push #'alignment-hook fun-hooks))
788 (%make-dstate :segment-sap sap
790 :argument-column arg-column
792 :byte-order sb!c:*backend-byte-order*)))
794 (defun add-fun-header-hooks (segment)
795 (declare (type segment segment))
796 (do ((fun (sb!kernel:code-header-ref (seg-code segment)
797 sb!vm:code-entry-points-slot)
799 (length (seg-length segment)))
801 (let ((offset (code-offs-to-segment-offs (fun-offset fun) segment)))
802 (when (<= 0 offset length)
803 (push (make-offs-hook :offset offset :fun #'fun-header-hook)
804 (seg-hooks segment))))))
806 ;;; A SAP-MAKER is a no-argument function that returns a SAP.
808 #!-sb-fluid (declaim (inline sap-maker))
810 (defun sap-maker (function input offset)
811 (declare (optimize (speed 3))
812 (type (function (t) sb!sys:system-area-pointer) function)
813 (type offset offset))
814 (let ((old-sap (sb!sys:sap+ (funcall function input) offset)))
815 (declare (type sb!sys:system-area-pointer old-sap))
818 (+ (sb!sys:sap-int (funcall function input)) offset)))
819 ;; Saving the sap like this avoids consing except when the sap
820 ;; changes (because the sap-int, arith, etc., get inlined).
821 (declare (type address new-addr))
822 (if (= (sb!sys:sap-int old-sap) new-addr)
824 (setf old-sap (sb!sys:int-sap new-addr)))))))
826 (defun vector-sap-maker (vector offset)
827 (declare (optimize (speed 3))
828 (type offset offset))
829 (sap-maker #'sb!sys:vector-sap vector offset))
831 (defun code-sap-maker (code offset)
832 (declare (optimize (speed 3))
833 (type sb!kernel:code-component code)
834 (type offset offset))
835 (sap-maker #'sb!kernel:code-instructions code offset))
837 (defun memory-sap-maker (address)
838 (declare (optimize (speed 3))
839 (type address address))
840 (let ((sap (sb!sys:int-sap address)))
843 ;;; Return a memory segment located at the system-area-pointer returned by
844 ;;; SAP-MAKER and LENGTH bytes long in the disassem-state object DSTATE.
846 ;;; &KEY arguments include :VIRTUAL-LOCATION (by default the same as
847 ;;; the address), :DEBUG-FUN, :SOURCE-FORM-CACHE (a
848 ;;; SOURCE-FORM-CACHE object), and :HOOKS (a list of OFFS-HOOK
850 (defun make-segment (sap-maker length
852 code virtual-location
853 debug-fun source-form-cache
855 (declare (type (function () sb!sys:system-area-pointer) sap-maker)
856 (type disassem-length length)
857 (type (or null address) virtual-location)
858 (type (or null sb!di:debug-fun) debug-fun)
859 (type (or null source-form-cache) source-form-cache))
864 :virtual-location (or virtual-location
865 (sb!sys:sap-int (funcall sap-maker)))
868 (add-debugging-hooks segment debug-fun source-form-cache)
869 (add-fun-header-hooks segment)
872 (defun make-vector-segment (vector offset &rest args)
873 (declare (type vector vector)
875 (inline make-segment))
876 (apply #'make-segment (vector-sap-maker vector offset) args))
878 (defun make-code-segment (code offset length &rest args)
879 (declare (type sb!kernel:code-component code)
881 (inline make-segment))
882 (apply #'make-segment (code-sap-maker code offset) length :code code args))
884 (defun make-memory-segment (address &rest args)
885 (declare (type address address)
886 (inline make-segment))
887 (apply #'make-segment (memory-sap-maker address) args))
890 (defun print-fun-headers (function)
891 (declare (type compiled-function function))
892 (let* ((self (fun-self function))
893 (code (sb!kernel:fun-code-header self)))
894 (format t "Code-header ~S: size: ~S, trace-table-offset: ~S~%"
896 (sb!kernel:code-header-ref code
897 sb!vm:code-code-size-slot)
898 (sb!kernel:code-header-ref code
899 sb!vm:code-trace-table-offset-slot))
900 (do ((fun (sb!kernel:code-header-ref code sb!vm:code-entry-points-slot)
903 (let ((fun-offset (sb!kernel:get-closure-length fun)))
904 ;; There is function header fun-offset words from the
906 (format t "Fun-header ~S at offset ~W (words): ~S~A => ~S~%"
909 (sb!kernel:code-header-ref
910 code (+ fun-offset sb!vm:simple-fun-name-slot))
911 (sb!kernel:code-header-ref
912 code (+ fun-offset sb!vm:simple-fun-arglist-slot))
913 (sb!kernel:code-header-ref
914 code (+ fun-offset sb!vm:simple-fun-type-slot)))))))
916 ;;; getting at the source code...
918 (defstruct (source-form-cache (:conc-name sfcache-)
920 (debug-source nil :type (or null sb!di:debug-source))
921 (toplevel-form-index -1 :type fixnum)
922 (toplevel-form nil :type list)
923 (form-number-mapping-table nil :type (or null (vector list)))
924 (last-location-retrieved nil :type (or null sb!di:code-location))
925 (last-form-retrieved -1 :type fixnum))
927 (defun get-toplevel-form (debug-source tlf-index)
928 (let ((name (sb!di:debug-source-name debug-source)))
929 (ecase (sb!di:debug-source-from debug-source)
931 (cond ((not (probe-file name))
932 (warn "The source file ~S no longer seems to exist." name)
935 (let ((start-positions
936 (sb!di:debug-source-start-positions debug-source)))
937 (cond ((null start-positions)
938 (warn "There is no start positions map.")
941 (let* ((local-tlf-index
943 (sb!di:debug-source-root-number
946 (aref start-positions local-tlf-index)))
947 (with-open-file (f name)
948 (cond ((= (sb!di:debug-source-created debug-source)
949 (file-write-date name))
950 (file-position f char-offset))
952 (warn "Source file ~S has been modified; ~@
953 using form offset instead of ~
956 (let ((*read-suppress* t))
957 (dotimes (i local-tlf-index) (read f)))))
958 (let ((*readtable* (copy-readtable)))
959 (set-dispatch-macro-character
961 (lambda (stream sub-char &rest rest)
962 (declare (ignore rest sub-char))
963 (let ((token (read stream t nil t)))
964 (format nil "#.~S" token))))
968 (aref name tlf-index)))))
970 (defun cache-valid (loc cache)
972 (and (eq (sb!di:code-location-debug-source loc)
973 (sfcache-debug-source cache))
974 (eq (sb!di:code-location-toplevel-form-offset loc)
975 (sfcache-toplevel-form-index cache)))))
977 (defun get-source-form (loc context &optional cache)
978 (let* ((cache-valid (cache-valid loc cache))
979 (tlf-index (sb!di:code-location-toplevel-form-offset loc))
980 (form-number (sb!di:code-location-form-number loc))
983 (sfcache-toplevel-form cache)
984 (get-toplevel-form (sb!di:code-location-debug-source loc)
988 (sfcache-form-number-mapping-table cache)
989 (sb!di:form-number-translations toplevel-form tlf-index))))
990 (when (and (not cache-valid) cache)
991 (setf (sfcache-debug-source cache) (sb!di:code-location-debug-source loc)
992 (sfcache-toplevel-form-index cache) tlf-index
993 (sfcache-toplevel-form cache) toplevel-form
994 (sfcache-form-number-mapping-table cache) mapping-table))
995 (cond ((null toplevel-form)
997 ((> form-number (length mapping-table))
998 (warn "bogus form-number in form! The source file has probably ~@
999 been changed too much to cope with.")
1001 ;; Disable future warnings.
1002 (setf (sfcache-toplevel-form cache) nil))
1006 (setf (sfcache-last-location-retrieved cache) loc)
1007 (setf (sfcache-last-form-retrieved cache) form-number))
1008 (sb!di:source-path-context toplevel-form
1009 (aref mapping-table form-number)
1012 (defun get-different-source-form (loc context &optional cache)
1013 (if (and (cache-valid loc cache)
1014 (or (= (sb!di:code-location-form-number loc)
1015 (sfcache-last-form-retrieved cache))
1016 (and (sfcache-last-location-retrieved cache)
1017 (sb!di:code-location=
1019 (sfcache-last-location-retrieved cache)))))
1021 (values (get-source-form loc context cache) t)))
1023 ;;;; stuff to use debugging info to augment the disassembly
1025 (defun code-fun-map (code)
1026 (declare (type sb!kernel:code-component code))
1027 (sb!c::compiled-debug-info-fun-map (sb!kernel:%code-debug-info code)))
1029 (defstruct (location-group (:copier nil))
1030 (locations #() :type (vector (or list fixnum))))
1032 (defstruct (storage-info (:copier nil))
1033 (groups nil :type list) ; alist of (name . location-group)
1034 (debug-vars #() :type vector))
1036 ;;; Return the vector of DEBUG-VARs currently associated with DSTATE.
1037 (defun dstate-debug-vars (dstate)
1038 (declare (type disassem-state dstate))
1039 (storage-info-debug-vars (seg-storage-info (dstate-segment dstate))))
1041 ;;; Given the OFFSET of a location within the location-group called
1042 ;;; LG-NAME, see whether there's a current mapping to a source
1043 ;;; variable in DSTATE, and if so, return the offset of that variable
1044 ;;; in the current debug-var vector.
1045 (defun find-valid-storage-location (offset lg-name dstate)
1046 (declare (type offset offset)
1047 (type symbol lg-name)
1048 (type disassem-state dstate))
1049 (let* ((storage-info
1050 (seg-storage-info (dstate-segment dstate)))
1053 (cdr (assoc lg-name (storage-info-groups storage-info)))))
1055 (dstate-current-valid-locations dstate)))
1057 (not (null currently-valid))
1058 (let ((locations (location-group-locations location-group)))
1059 (and (< offset (length locations))
1060 (let ((used-by (aref locations offset)))
1062 (let ((debug-var-num
1066 (zerop (bit currently-valid used-by)))
1072 (bit currently-valid num)))
1077 ;; Found a valid storage reference!
1078 ;; can't use it again until it's revalidated...
1079 (setf (bit (dstate-current-valid-locations
1086 ;;; Return a new vector which has the same contents as the old one
1087 ;;; VEC, plus new cells (for a total size of NEW-LEN). The additional
1088 ;;; elements are initialized to INITIAL-ELEMENT.
1089 (defun grow-vector (vec new-len &optional initial-element)
1090 (declare (type vector vec)
1091 (type fixnum new-len))
1093 (make-sequence `(vector ,(array-element-type vec) ,new-len)
1095 :initial-element initial-element)))
1096 (dotimes (i (length vec))
1097 (setf (aref new i) (aref vec i)))
1100 ;;; Return a STORAGE-INFO struction describing the object-to-source
1101 ;;; variable mappings from DEBUG-FUN.
1102 (defun storage-info-for-debug-fun (debug-fun)
1103 (declare (type sb!di:debug-fun debug-fun))
1104 (let ((sc-vec sb!c::*backend-sc-numbers*)
1106 (debug-vars (sb!di::debug-fun-debug-vars
1109 (dotimes (debug-var-offset
1111 (make-storage-info :groups groups
1112 :debug-vars debug-vars))
1113 (let ((debug-var (aref debug-vars debug-var-offset)))
1115 (format t ";;; At offset ~W: ~S~%" debug-var-offset debug-var)
1117 (sb!di::compiled-debug-var-sc-offset debug-var))
1120 (sb!c:sc-sb (aref sc-vec
1121 (sb!c:sc-offset-scn sc-offset))))))
1123 (format t ";;; SET: ~S[~W]~%"
1124 sb-name (sb!c:sc-offset-offset sc-offset))
1125 (unless (null sb-name)
1126 (let ((group (cdr (assoc sb-name groups))))
1128 (setf group (make-location-group))
1129 (push `(,sb-name . ,group) groups))
1130 (let* ((locations (location-group-locations group))
1131 (length (length locations))
1132 (offset (sb!c:sc-offset-offset sc-offset)))
1133 (when (>= offset length)
1135 (grow-vector locations
1139 (location-group-locations group)
1141 (let ((already-there (aref locations offset)))
1142 (cond ((null already-there)
1143 (setf (aref locations offset) debug-var-offset))
1144 ((eql already-there debug-var-offset))
1146 (if (listp already-there)
1147 (pushnew debug-var-offset
1148 (aref locations offset))
1149 (setf (aref locations offset)
1150 (list debug-var-offset
1155 (defun source-available-p (debug-fun)
1157 (sb!di:do-debug-fun-blocks (block debug-fun)
1158 (declare (ignore block))
1160 (sb!di:no-debug-blocks () nil)))
1162 (defun print-block-boundary (stream dstate)
1163 (let ((os (dstate-output-state dstate)))
1164 (when (not (eq os :beginning))
1165 (when (not (eq os :block-boundary))
1167 (setf (dstate-output-state dstate)
1170 ;;; Add hooks to track to track the source code in SEGMENT during
1171 ;;; disassembly. SFCACHE can be either NIL or it can be a
1172 ;;; SOURCE-FORM-CACHE structure, in which case it is used to cache
1173 ;;; forms from files.
1174 (defun add-source-tracking-hooks (segment debug-fun &optional sfcache)
1175 (declare (type segment segment)
1176 (type (or null sb!di:debug-fun) debug-fun)
1177 (type (or null source-form-cache) sfcache))
1178 (let ((last-block-pc -1))
1179 (flet ((add-hook (pc fun &optional before-address)
1180 (push (make-offs-hook
1181 :offset pc ;; ### FIX to account for non-zero offs in code
1183 :before-address before-address)
1184 (seg-hooks segment))))
1186 (sb!di:do-debug-fun-blocks (block debug-fun)
1187 (let ((first-location-in-block-p t))
1188 (sb!di:do-debug-block-locations (loc block)
1189 (let ((pc (sb!di::compiled-code-location-pc loc)))
1191 ;; Put blank lines in at block boundaries
1192 (when (and first-location-in-block-p
1193 (/= pc last-block-pc))
1194 (setf first-location-in-block-p nil)
1196 (lambda (stream dstate)
1197 (print-block-boundary stream dstate))
1199 (setf last-block-pc pc))
1201 ;; Print out corresponding source; this information is not
1202 ;; all that accurate, but it's better than nothing
1203 (unless (zerop (sb!di:code-location-form-number loc))
1204 (multiple-value-bind (form new)
1205 (get-different-source-form loc 0 sfcache)
1207 (let ((at-block-begin (= pc last-block-pc)))
1210 (lambda (stream dstate)
1211 (declare (ignore dstate))
1213 (unless at-block-begin
1215 (format stream ";;; [~W] "
1216 (sb!di:code-location-form-number
1218 (prin1-short form stream)
1223 ;; Keep track of variable live-ness as best we can.
1225 (copy-seq (sb!di::compiled-code-location-live-set
1229 (lambda (stream dstate)
1230 (declare (ignore stream))
1231 (setf (dstate-current-valid-locations dstate)
1234 (note (lambda (stream)
1235 (let ((*print-length* nil))
1236 (format stream "live set: ~S"
1240 (sb!di:no-debug-blocks () nil)))))
1242 (defun add-debugging-hooks (segment debug-fun &optional sfcache)
1244 (setf (seg-storage-info segment)
1245 (storage-info-for-debug-fun debug-fun))
1246 (add-source-tracking-hooks segment debug-fun sfcache)
1247 (let ((kind (sb!di:debug-fun-kind debug-fun)))
1248 (flet ((add-new-hook (n)
1249 (push (make-offs-hook
1251 :fun (lambda (stream dstate)
1252 (declare (ignore stream))
1254 (seg-hooks segment))))
1258 (add-new-hook "no-arg-parsing entry point"))
1260 (add-new-hook (lambda (stream)
1261 (format stream "~S entry point" kind)))))))))
1263 ;;; Return a list of the segments of memory containing machine code
1264 ;;; instructions for FUNCTION.
1265 (defun get-fun-segments (function)
1266 (declare (type compiled-function function))
1267 (let* ((code (fun-code function))
1268 (fun-map (code-fun-map code))
1269 (fname (sb!kernel:%simple-fun-name function))
1270 (sfcache (make-source-form-cache)))
1271 (let ((first-block-seen-p nil)
1272 (nil-block-seen-p nil)
1274 (last-debug-fun nil)
1276 (flet ((add-seg (offs len df)
1278 (push (make-code-segment code offs len
1280 :source-form-cache sfcache)
1282 (dotimes (fmap-index (length fun-map))
1283 (let ((fmap-entry (aref fun-map fmap-index)))
1284 (etypecase fmap-entry
1286 (when first-block-seen-p
1287 (add-seg last-offset
1288 (- fmap-entry last-offset)
1290 (setf last-debug-fun nil))
1291 (setf last-offset fmap-entry))
1292 (sb!c::compiled-debug-fun
1293 (let ((name (sb!c::compiled-debug-fun-name fmap-entry))
1294 (kind (sb!c::compiled-debug-fun-kind fmap-entry)))
1296 (format t ";;; SAW ~S ~S ~S,~S ~W,~W~%"
1297 name kind first-block-seen-p nil-block-seen-p
1299 (sb!c::compiled-debug-fun-start-pc fmap-entry))
1300 (cond (#+nil (eq last-offset fun-offset)
1301 (and (equal name fname) (not first-block-seen-p))
1302 (setf first-block-seen-p t))
1303 ((eq kind :external)
1304 (when first-block-seen-p
1307 (when nil-block-seen-p
1309 (when first-block-seen-p
1310 (setf nil-block-seen-p t))))
1311 (setf last-debug-fun
1312 (sb!di::make-compiled-debug-fun fmap-entry code)))))))
1313 (let ((max-offset (code-inst-area-length code)))
1314 (when (and first-block-seen-p last-debug-fun)
1315 (add-seg last-offset
1316 (- max-offset last-offset)
1319 (let ((offs (fun-insts-offset function)))
1321 (make-code-segment code offs (- max-offset offs))))
1322 (nreverse segments)))))))
1324 ;;; Return a list of the segments of memory containing machine code
1325 ;;; instructions for the code-component CODE. If START-OFFSET and/or
1326 ;;; LENGTH is supplied, only that part of the code-segment is used
1327 ;;; (but these are constrained to lie within the code-segment).
1328 (defun get-code-segments (code
1331 (length (code-inst-area-length code)))
1332 (declare (type sb!kernel:code-component code)
1333 (type offset start-offset)
1334 (type disassem-length length))
1335 (let ((segments nil))
1337 (let ((fun-map (code-fun-map code))
1338 (sfcache (make-source-form-cache)))
1339 (let ((last-offset 0)
1340 (last-debug-fun nil))
1341 (flet ((add-seg (offs len df)
1342 (let* ((restricted-offs
1343 (min (max start-offset offs)
1344 (+ start-offset length)))
1346 (- (min (max start-offset (+ offs len))
1347 (+ start-offset length))
1349 (when (> restricted-len 0)
1350 (push (make-code-segment code
1351 restricted-offs restricted-len
1353 :source-form-cache sfcache)
1355 (dotimes (fun-map-index (length fun-map))
1356 (let ((fun-map-entry (aref fun-map fun-map-index)))
1357 (etypecase fun-map-entry
1359 (add-seg last-offset (- fun-map-entry last-offset)
1361 (setf last-debug-fun nil)
1362 (setf last-offset fun-map-entry))
1363 (sb!c::compiled-debug-fun
1364 (setf last-debug-fun
1365 (sb!di::make-compiled-debug-fun fun-map-entry
1367 (when last-debug-fun
1368 (add-seg last-offset
1369 (- (code-inst-area-length code) last-offset)
1370 last-debug-fun))))))
1372 (make-code-segment code start-offset length)
1373 (nreverse segments))))
1375 ;;; Return two values: the amount by which the last instruction in the
1376 ;;; segment goes past the end of the segment, and the offset of the
1377 ;;; end of the segment from the beginning of that instruction. If all
1378 ;;; instructions fit perfectly, return 0 and 0.
1379 (defun segment-overflow (segment dstate)
1380 (declare (type segment segment)
1381 (type disassem-state dstate))
1382 (let ((seglen (seg-length segment))
1384 (map-segment-instructions (lambda (chunk inst)
1385 (declare (ignore chunk inst))
1386 (setf last-start (dstate-cur-offs dstate)))
1389 (values (- (dstate-cur-offs dstate) seglen)
1390 (- seglen last-start))))
1392 ;;; Compute labels for all the memory segments in SEGLIST and adds
1393 ;;; them to DSTATE. It's important to call this function with all the
1394 ;;; segments you're interested in, so that it can find references from
1396 (defun label-segments (seglist dstate)
1397 (declare (type list seglist)
1398 (type disassem-state dstate))
1399 (dolist (seg seglist)
1400 (add-segment-labels seg dstate))
1401 ;; Now remove any labels that don't point anywhere in the segments
1403 (setf (dstate-labels dstate)
1404 (remove-if (lambda (lab)
1407 (let ((start (seg-virtual-location seg)))
1410 (+ start (seg-length seg)))))
1412 (dstate-labels dstate))))
1414 ;;; Disassemble the machine code instructions in SEGMENT to STREAM.
1415 (defun disassemble-segment (segment stream dstate)
1416 (declare (type segment segment)
1417 (type stream stream)
1418 (type disassem-state dstate))
1419 (let ((*print-pretty* nil)) ; otherwise the pp conses hugely
1420 (number-labels dstate)
1421 (map-segment-instructions
1422 (lambda (chunk inst)
1423 (declare (type dchunk chunk) (type instruction inst))
1424 (let ((printer (inst-printer inst)))
1426 (funcall printer chunk inst stream dstate))))
1431 ;;; Disassemble the machine code instructions in each memory segment
1432 ;;; in SEGMENTS in turn to STREAM.
1433 (defun disassemble-segments (segments stream dstate)
1434 (declare (type list segments)
1435 (type stream stream)
1436 (type disassem-state dstate))
1437 (unless (null segments)
1438 (let ((first (car segments))
1439 (last (car (last segments))))
1440 (set-location-printing-range dstate
1441 (seg-virtual-location first)
1442 (- (+ (seg-virtual-location last)
1444 (seg-virtual-location first)))
1445 (setf (dstate-output-state dstate) :beginning)
1446 (dolist (seg segments)
1447 (disassemble-segment seg stream dstate)))))
1449 ;;;; top level functions
1451 ;;; Disassemble the machine code instructions for FUNCTION.
1452 (defun disassemble-fun (fun &key
1453 (stream *standard-output*)
1455 (declare (type compiled-function fun)
1456 (type stream stream)
1457 (type (member t nil) use-labels))
1458 (let* ((dstate (make-dstate))
1459 (segments (get-fun-segments fun)))
1461 (label-segments segments dstate))
1462 (disassemble-segments segments stream dstate)))
1464 ;;; FIXME: We probably don't need this any more now that there are
1465 ;;; no interpreted functions, only compiled ones.
1466 (defun compile-function-lambda-expr (function)
1467 (declare (type function function))
1468 (multiple-value-bind (lambda closurep name)
1469 (function-lambda-expression function)
1470 (declare (ignore name))
1472 (error "can't compile a lexical closure"))
1473 (compile nil lambda)))
1475 (defun compiled-fun-or-lose (thing &optional (name thing))
1476 (cond ((legal-fun-name-p thing)
1477 (compiled-fun-or-lose (fdefinition thing) thing))
1481 (eq (car thing) 'lambda))
1482 (compile nil thing))
1484 (error "can't make a compiled function from ~S" name))))
1486 (defun disassemble (object &key
1487 (stream *standard-output*)
1490 "Disassemble the compiled code associated with OBJECT, which can be a
1491 function, a lambda expression, or a symbol with a function definition. If
1492 it is not already compiled, the compiler is called to produce something to
1494 (declare (type (or function symbol cons) object)
1495 (type (or (member t) stream) stream)
1496 (type (member t nil) use-labels))
1497 (pprint-logical-block (*standard-output* nil :per-line-prefix "; ")
1498 (disassemble-fun (compiled-fun-or-lose object)
1500 :use-labels use-labels)
1503 ;;; Disassembles the given area of memory starting at ADDRESS and
1504 ;;; LENGTH long. Note that if CODE-COMPONENT is NIL and this memory
1505 ;;; could move during a GC, you'd better disable it around the call to
1507 (defun disassemble-memory (address
1510 (stream *standard-output*)
1513 (declare (type (or address sb!sys:system-area-pointer) address)
1514 (type disassem-length length)
1515 (type stream stream)
1516 (type (or null sb!kernel:code-component) code-component)
1517 (type (member t nil) use-labels))
1519 (if (sb!sys:system-area-pointer-p address)
1520 (sb!sys:sap-int address)
1522 (dstate (make-dstate))
1528 (sb!kernel:code-instructions code-component)))))
1529 (when (or (< code-offs 0)
1530 (> code-offs (code-inst-area-length code-component)))
1531 (error "address ~X not in the code component ~S"
1532 address code-component))
1533 (get-code-segments code-component code-offs length))
1534 (list (make-memory-segment address length)))))
1536 (label-segments segments dstate))
1537 (disassemble-segments segments stream dstate)))
1539 ;;; Disassemble the machine code instructions associated with
1540 ;;; CODE-COMPONENT (this may include multiple entry points).
1541 (defun disassemble-code-component (code-component &key
1542 (stream *standard-output*)
1544 (declare (type (or null sb!kernel:code-component compiled-function)
1546 (type stream stream)
1547 (type (member t nil) use-labels))
1548 (let* ((code-component
1549 (if (functionp code-component)
1550 (fun-code code-component)
1552 (dstate (make-dstate))
1553 (segments (get-code-segments code-component)))
1555 (label-segments segments dstate))
1556 (disassemble-segments segments stream dstate)))
1558 ;;; code for making useful segments from arbitrary lists of code-blocks
1560 ;;; the maximum size of an instruction. Note that this includes
1561 ;;; pseudo-instructions like error traps with their associated
1562 ;;; operands, so it should be big enough to include them, i.e. it's
1563 ;;; not just 4 on a risc machine!
1564 (defconstant max-instruction-size 16)
1566 (defun add-block-segments (seg-code-block
1571 (declare (type list seglist)
1572 (type integer location)
1573 (type (or null (vector (unsigned-byte 8))) connecting-vec)
1574 (type disassem-state dstate))
1575 (flet ((addit (seg overflow)
1576 (let ((length (+ (seg-length seg) overflow)))
1578 (setf (seg-length seg) length)
1579 (incf location length)
1580 (push seg seglist)))))
1581 (let ((connecting-overflow 0)
1582 (amount (length seg-code-block)))
1583 (when connecting-vec
1584 ;; Tack on some of the new block to the old overflow vector.
1585 (let* ((beginning-of-block-amount
1586 (if seg-code-block (min max-instruction-size amount) 0))
1590 '(vector (unsigned-byte 8))
1592 (subseq seg-code-block 0 beginning-of-block-amount))
1594 (when (and (< (length connecting-vec) max-instruction-size)
1595 (not (null seg-code-block)))
1596 (return-from add-block-segments
1597 ;; We want connecting vectors to be large enough to hold
1598 ;; any instruction, and since the current seg-code-block
1599 ;; wasn't large enough to do this (and is now entirely
1600 ;; on the end of the overflow-vector), just save it for
1602 (values seglist location connecting-vec)))
1603 (when (> (length connecting-vec) 0)
1605 (make-vector-segment connecting-vec
1607 (- (length connecting-vec)
1608 beginning-of-block-amount)
1609 :virtual-location location)))
1610 (setf connecting-overflow (segment-overflow seg dstate))
1611 (addit seg connecting-overflow)))))
1612 (cond ((null seg-code-block)
1613 ;; nothing more to add
1614 (values seglist location nil))
1615 ((< (- amount connecting-overflow) max-instruction-size)
1616 ;; We can't create a segment with the minimum size
1617 ;; required for an instruction, so just keep on accumulating
1618 ;; in the overflow vector for the time-being.
1621 (subseq seg-code-block connecting-overflow amount)))
1623 ;; Put as much as we can into a new segment, and the rest
1624 ;; into the overflow-vector.
1625 (let* ((initial-length
1626 (- amount connecting-overflow max-instruction-size))
1628 (make-vector-segment seg-code-block
1631 :virtual-location location))
1633 (segment-overflow seg dstate)))
1634 (addit seg overflow)
1637 (subseq seg-code-block
1638 (+ connecting-overflow (seg-length seg))
1641 ;;;; code to disassemble assembler segments
1643 (defun assem-segment-to-disassem-segments (assem-segment dstate)
1644 (declare (type sb!assem:segment assem-segment)
1645 (type disassem-state dstate))
1647 (disassem-segments nil)
1648 (connecting-vec nil))
1649 (sb!assem:on-segment-contents-vectorly
1651 (lambda (seg-code-block)
1652 (multiple-value-setq (disassem-segments location connecting-vec)
1653 (add-block-segments seg-code-block
1658 (when connecting-vec
1659 (setf disassem-segments
1660 (add-block-segments nil
1665 (sort disassem-segments #'< :key #'seg-virtual-location)))
1667 ;;; Disassemble the machine code instructions associated with
1668 ;;; ASSEM-SEGMENT (of type assem:segment).
1669 (defun disassemble-assem-segment (assem-segment stream)
1670 (declare (type sb!assem:segment assem-segment)
1671 (type stream stream))
1672 (let* ((dstate (make-dstate))
1674 (assem-segment-to-disassem-segments assem-segment dstate)))
1675 (label-segments disassem-segments dstate)
1676 (disassemble-segments disassem-segments stream dstate)))
1678 ;;; routines to find things in the Lisp environment
1680 ;;; an alist of (SYMBOL-SLOT-OFFSET . ACCESS-FUN-NAME) for slots
1681 ;;; in a symbol object that we know about
1682 (defparameter *grokked-symbol-slots*
1683 (sort `((,sb!vm:symbol-value-slot . symbol-value)
1684 (,sb!vm:symbol-plist-slot . symbol-plist)
1685 (,sb!vm:symbol-name-slot . symbol-name)
1686 (,sb!vm:symbol-package-slot . symbol-package))
1690 ;;; Given ADDRESS, try and figure out if which slot of which symbol is
1691 ;;; being referred to. Of course we can just give up, so it's not a
1692 ;;; big deal... Return two values, the symbol and the name of the
1693 ;;; access function of the slot.
1694 (defun grok-symbol-slot-ref (address)
1695 (declare (type address address))
1696 (if (not (aligned-p address sb!vm:n-word-bytes))
1698 (do ((slots-tail *grokked-symbol-slots* (cdr slots-tail)))
1701 (let* ((field (car slots-tail))
1702 (slot-offset (words-to-bytes (car field)))
1703 (maybe-symbol-addr (- address slot-offset))
1705 (sb!kernel:make-lisp-obj
1706 (+ maybe-symbol-addr sb!vm:other-pointer-lowtag))))
1707 (when (symbolp maybe-symbol)
1708 (return (values maybe-symbol (cdr field))))))))
1710 (defvar *address-of-nil-object* (sb!kernel:get-lisp-obj-address nil))
1712 ;;; Given a BYTE-OFFSET from NIL, try and figure out which slot of
1713 ;;; which symbol is being referred to. Of course we can just give up,
1714 ;;; so it's not a big deal... Return two values, the symbol and the
1715 ;;; access function.
1716 (defun grok-nil-indexed-symbol-slot-ref (byte-offset)
1717 (declare (type offset byte-offset))
1718 (grok-symbol-slot-ref (+ *address-of-nil-object* byte-offset)))
1720 ;;; Return the Lisp object located BYTE-OFFSET from NIL.
1721 (defun get-nil-indexed-object (byte-offset)
1722 (declare (type offset byte-offset))
1723 (sb!kernel:make-lisp-obj (+ *address-of-nil-object* byte-offset)))
1725 ;;; Return two values; the Lisp object located at BYTE-OFFSET in the
1726 ;;; constant area of the code-object in the current segment and T, or
1727 ;;; NIL and NIL if there is no code-object in the current segment.
1728 (defun get-code-constant (byte-offset dstate)
1730 (declare (type offset byte-offset)
1731 (type disassem-state dstate))
1732 (let ((code (seg-code (dstate-segment dstate))))
1735 (sb!kernel:code-header-ref code
1737 sb!vm:other-pointer-lowtag)
1738 (- sb!vm:word-shift)))
1742 (defun get-code-constant-absolute (addr dstate)
1743 (declare (type address addr))
1744 (declare (type disassem-state dstate))
1745 (let ((code (seg-code (dstate-segment dstate))))
1747 (return-from get-code-constant-absolute (values nil nil)))
1748 (let ((code-size (ash (sb!kernel:get-header-data code) sb!vm:word-shift)))
1749 (sb!sys:without-gcing
1750 (let ((code-addr (- (sb!kernel:get-lisp-obj-address code)
1751 sb!vm:other-pointer-lowtag)))
1752 (if (or (< addr code-addr) (>= addr (+ code-addr code-size)))
1754 (values (sb!kernel:code-header-ref
1756 (ash (- addr code-addr) (- sb!vm:word-shift)))
1759 (defvar *assembler-routines-by-addr* nil)
1761 (defvar *foreign-symbols-by-addr* nil)
1763 ;;; Build an address-name hash-table from the name-address hash
1764 (defun invert-address-hash (htable &optional (addr-hash (make-hash-table)))
1765 (maphash (lambda (name address)
1766 (setf (gethash address addr-hash) name))
1770 ;;; Return the name of the primitive Lisp assembler routine or foreign
1771 ;;; symbol located at ADDRESS, or NIL if there isn't one.
1772 (defun find-assembler-routine (address)
1773 (declare (type address address))
1774 (when (null *assembler-routines-by-addr*)
1775 (setf *assembler-routines-by-addr*
1776 (invert-address-hash sb!fasl:*assembler-routines*))
1777 (setf *assembler-routines-by-addr*
1778 (invert-address-hash sb!fasl:*static-foreign-symbols*
1779 *assembler-routines-by-addr*)))
1780 (gethash address *assembler-routines-by-addr*))
1782 ;;;; some handy function for machine-dependent code to use...
1784 #!-sb-fluid (declaim (maybe-inline sap-ref-int read-suffix))
1786 (defun sap-ref-int (sap offset length byte-order)
1787 (declare (type sb!sys:system-area-pointer sap)
1788 (type (unsigned-byte 16) offset)
1789 (type (member 1 2 4) length)
1790 (type (member :little-endian :big-endian) byte-order)
1791 (optimize (speed 3) (safety 0)))
1793 (1 (sb!sys:sap-ref-8 sap offset))
1794 (2 (if (eq byte-order :big-endian)
1795 (+ (ash (sb!sys:sap-ref-8 sap offset) 8)
1796 (sb!sys:sap-ref-8 sap (+ offset 1)))
1797 (+ (ash (sb!sys:sap-ref-8 sap (+ offset 1)) 8)
1798 (sb!sys:sap-ref-8 sap offset))))
1799 (4 (if (eq byte-order :big-endian)
1800 (+ (ash (sb!sys:sap-ref-8 sap offset) 24)
1801 (ash (sb!sys:sap-ref-8 sap (+ 1 offset)) 16)
1802 (ash (sb!sys:sap-ref-8 sap (+ 2 offset)) 8)
1803 (sb!sys:sap-ref-8 sap (+ 3 offset)))
1804 (+ (sb!sys:sap-ref-8 sap offset)
1805 (ash (sb!sys:sap-ref-8 sap (+ 1 offset)) 8)
1806 (ash (sb!sys:sap-ref-8 sap (+ 2 offset)) 16)
1807 (ash (sb!sys:sap-ref-8 sap (+ 3 offset)) 24))))))
1809 (defun read-suffix (length dstate)
1810 (declare (type (member 8 16 32) length)
1811 (type disassem-state dstate)
1812 (optimize (speed 3) (safety 0)))
1813 (let ((length (ecase length (8 1) (16 2) (32 4))))
1814 (declare (type (unsigned-byte 3) length))
1816 (sap-ref-int (dstate-segment-sap dstate)
1817 (dstate-next-offs dstate)
1819 (dstate-byte-order dstate))
1820 (incf (dstate-next-offs dstate) length))))
1822 ;;;; optional routines to make notes about code
1824 ;;; Store NOTE (which can be either a string or a function with a
1825 ;;; single stream argument) to be printed as an end-of-line comment
1826 ;;; after the current instruction is disassembled.
1827 (defun note (note dstate)
1828 (declare (type (or string function) note)
1829 (type disassem-state dstate))
1830 (push note (dstate-notes dstate)))
1832 (defun prin1-short (thing stream)
1833 (with-print-restrictions
1834 (prin1 thing stream)))
1836 (defun prin1-quoted-short (thing stream)
1837 (if (self-evaluating-p thing)
1838 (prin1-short thing stream)
1839 (prin1-short `',thing stream)))
1841 ;;; Store a note about the lisp constant located BYTE-OFFSET bytes
1842 ;;; from the current code-component, to be printed as an end-of-line
1843 ;;; comment after the current instruction is disassembled.
1844 (defun note-code-constant (byte-offset dstate)
1845 (declare (type offset byte-offset)
1846 (type disassem-state dstate))
1847 (multiple-value-bind (const valid)
1848 (get-code-constant byte-offset dstate)
1850 (note (lambda (stream)
1851 (prin1-quoted-short const stream))
1855 ;;; Store a note about the lisp constant located at ADDR in the
1856 ;;; current code-component, to be printed as an end-of-line comment
1857 ;;; after the current instruction is disassembled.
1858 (defun note-code-constant-absolute (addr dstate)
1859 (declare (type address addr)
1860 (type disassem-state dstate))
1861 (multiple-value-bind (const valid)
1862 (get-code-constant-absolute addr dstate)
1864 (note (lambda (stream)
1865 (prin1-quoted-short const stream))
1867 (values const valid)))
1869 ;;; If the memory address located NIL-BYTE-OFFSET bytes from the
1870 ;;; constant NIL is a valid slot in a symbol, store a note describing
1871 ;;; which symbol and slot, to be printed as an end-of-line comment
1872 ;;; after the current instruction is disassembled. Returns non-NIL iff
1873 ;;; a note was recorded.
1874 (defun maybe-note-nil-indexed-symbol-slot-ref (nil-byte-offset dstate)
1875 (declare (type offset nil-byte-offset)
1876 (type disassem-state dstate))
1877 (multiple-value-bind (symbol access-fun)
1878 (grok-nil-indexed-symbol-slot-ref nil-byte-offset)
1880 (note (lambda (stream)
1881 (prin1 (if (eq access-fun 'symbol-value)
1883 `(,access-fun ',symbol))
1888 ;;; If the memory address located NIL-BYTE-OFFSET bytes from the
1889 ;;; constant NIL is a valid lisp object, store a note describing which
1890 ;;; symbol and slot, to be printed as an end-of-line comment after the
1891 ;;; current instruction is disassembled. Returns non-NIL iff a note
1893 (defun maybe-note-nil-indexed-object (nil-byte-offset dstate)
1894 (declare (type offset nil-byte-offset)
1895 (type disassem-state dstate))
1896 (let ((obj (get-nil-indexed-object nil-byte-offset)))
1897 (note (lambda (stream)
1898 (prin1-quoted-short obj stream))
1902 ;;; If ADDRESS is the address of a primitive assembler routine or
1903 ;;; foreign symbol, store a note describing which one, to be printed
1904 ;;; as an end-of-line comment after the current instruction is
1905 ;;; disassembled. Returns non-NIL iff a note was recorded. If
1906 ;;; NOTE-ADDRESS-P is non-NIL, a note of the address is also made.
1907 (defun maybe-note-assembler-routine (address note-address-p dstate)
1908 (declare (type disassem-state dstate))
1909 (unless (typep address 'address)
1910 (return-from maybe-note-assembler-routine nil))
1911 (let ((name (find-assembler-routine address)))
1913 (note (lambda (stream)
1915 (format stream "#x~8,'0x: ~a" address name)
1916 (princ name stream)))
1920 ;;; If there's a valid mapping from OFFSET in the storage class
1921 ;;; SC-NAME to a source variable, make a note of the source-variable
1922 ;;; name, to be printed as an end-of-line comment after the current
1923 ;;; instruction is disassembled. Returns non-NIL iff a note was
1925 (defun maybe-note-single-storage-ref (offset sc-name dstate)
1926 (declare (type offset offset)
1927 (type symbol sc-name)
1928 (type disassem-state dstate))
1929 (let ((storage-location
1930 (find-valid-storage-location offset sc-name dstate)))
1931 (when storage-location
1932 (note (lambda (stream)
1933 (princ (sb!di:debug-var-symbol
1934 (aref (storage-info-debug-vars
1935 (seg-storage-info (dstate-segment dstate)))
1941 ;;; If there's a valid mapping from OFFSET in the storage-base called
1942 ;;; SB-NAME to a source variable, make a note equating ASSOC-WITH with
1943 ;;; the source-variable name, to be printed as an end-of-line comment
1944 ;;; after the current instruction is disassembled. Returns non-NIL iff
1945 ;;; a note was recorded.
1946 (defun maybe-note-associated-storage-ref (offset sb-name assoc-with dstate)
1947 (declare (type offset offset)
1948 (type symbol sb-name)
1949 (type (or symbol string) assoc-with)
1950 (type disassem-state dstate))
1951 (let ((storage-location
1952 (find-valid-storage-location offset sb-name dstate)))
1953 (when storage-location
1954 (note (lambda (stream)
1955 (format stream "~A = ~S"
1957 (sb!di:debug-var-symbol
1958 (aref (dstate-debug-vars dstate)
1959 storage-location))))
1963 (defun get-internal-error-name (errnum)
1964 (car (svref sb!c:*backend-internal-errors* errnum)))
1966 (defun get-sc-name (sc-offs)
1967 (sb!c::location-print-name
1968 ;; FIXME: This seems like an awful lot of computation just to get a name.
1969 ;; Couldn't we just use lookup in *BACKEND-SC-NAMES*, without having to cons
1971 (sb!c:make-random-tn :kind :normal
1972 :sc (svref sb!c:*backend-sc-numbers*
1973 (sb!c:sc-offset-scn sc-offs))
1974 :offset (sb!c:sc-offset-offset sc-offs))))
1976 ;;; When called from an error break instruction's :DISASSEM-CONTROL (or
1977 ;;; :DISASSEM-PRINTER) function, will correctly deal with printing the
1978 ;;; arguments to the break.
1980 ;;; ERROR-PARSE-FUN should be a function that accepts:
1981 ;;; 1) a SYSTEM-AREA-POINTER
1982 ;;; 2) a BYTE-OFFSET from the SAP to begin at
1983 ;;; 3) optionally, LENGTH-ONLY, which if non-NIL, means to only return
1984 ;;; the byte length of the arguments (to avoid unnecessary consing)
1985 ;;; It should read information from the SAP starting at BYTE-OFFSET, and
1986 ;;; return four values:
1987 ;;; 1) the error number
1988 ;;; 2) the total length, in bytes, of the information
1989 ;;; 3) a list of SC-OFFSETs of the locations of the error parameters
1990 ;;; 4) a list of the length (as read from the SAP), in bytes, of each
1991 ;;; of the return values.
1992 (defun handle-break-args (error-parse-fun stream dstate)
1993 (declare (type function error-parse-fun)
1994 (type (or null stream) stream)
1995 (type disassem-state dstate))
1996 (multiple-value-bind (errnum adjust sc-offsets lengths)
1997 (funcall error-parse-fun
1998 (dstate-segment-sap dstate)
1999 (dstate-next-offs dstate)
2002 (setf (dstate-cur-offs dstate)
2003 (dstate-next-offs dstate))
2004 (flet ((emit-err-arg (note)
2005 (let ((num (pop lengths)))
2006 (print-notes-and-newline stream dstate)
2007 (print-current-address stream dstate)
2008 (print-bytes num stream dstate)
2009 (incf (dstate-cur-offs dstate) num)
2011 (note note dstate)))))
2013 (emit-err-arg (symbol-name (get-internal-error-name errnum)))
2014 (dolist (sc-offs sc-offsets)
2015 (emit-err-arg (get-sc-name sc-offs)))))
2016 (incf (dstate-next-offs dstate)