d5a38f4ad725fad316e5638a2ec34ec931b26577
[sbcl.git] / src / code / show.lisp
1 ;;;; some stuff for displaying information for debugging/experimenting
2 ;;;; with the system, mostly conditionalized with #!+SB-SHOW
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
12
13 (in-package "SB!INT")
14 \f
15 ;;;; various SB-SHOW-dependent forms
16 ;;;;
17 ;;;; In general, macros named /FOO
18 ;;;;   * are for debugging/tracing
19 ;;;;   * expand into nothing unless :SB-SHOW is in the target
20 ;;;;     features list
21 ;;;; Often, they also do nothing at runtime if */SHOW* is NIL, but
22 ;;;; this is not always true for some very-low-level ones.
23 ;;;;
24 ;;;; (I follow the "/FOO for debugging/tracing expressions" naming
25 ;;;; rule and several other naming conventions in all my Lisp
26 ;;;; programming when possible, and then set Emacs to display comments
27 ;;;; in one shade of blue, tracing expressions in another shade of
28 ;;;; blue, and declarations and assertions in a yellowish shade, so
29 ;;;; that it's easy to separate them from the "real code" which
30 ;;;; actually does the work of the program. -- WHN 2001-05-07)
31
32 ;;; Set this to NIL to suppress output from /SHOW-related forms.
33 #!+sb-show (defvar */show* t)
34
35 (defun cannot-/show (string)
36   #+sb-xc-host (error "can't /SHOW: ~A" string)
37   ;; We end up in this situation when we execute /SHOW too early in
38   ;; cold init. That happens to me often enough that it's really
39   ;; annoying for it to cause a hard failure -- which at that point is
40   ;; hard to recover from -- instead of just diagnostic output.
41   #-sb-xc-host (sb!sys:%primitive
42                 print
43                 (concatenate 'string "/can't /SHOW: " string))
44   (values))
45
46 ;;; Should /SHOW output be suppressed at this point?
47 ;;;
48 ;;; Note that despite the connoting-no-side-effects-pure-predicate
49 ;;; name, we emit some error output if we're called at a point where
50 ;;; /SHOW is inherently invalid.
51 (defun suppress-/show-p ()
52   (cond (;; protection against /SHOW too early in cold init for
53          ;; (FORMAT *TRACE-OUTPUT* ..) to work, part I: Obviously
54          ;; we need *TRACE-OUTPUT* bound.
55          (not (boundp '*trace-output*))
56          (cannot-/show "*TRACE-OUTPUT* isn't bound. (Try /SHOW0.)")
57          t)
58         (;; protection against /SHOW too early in cold init for
59          ;; (FORMAT *TRACE-OUTPUT* ..) to work, part II: In a virtuoso
60          ;; display of name mnemonicity, *READTABLE* is used by the
61          ;; printer to decide which case convention to use when
62          ;; writing symbols, so we need it bound.
63          (not (boundp '*readtable*))
64          (cannot-/show "*READTABLE* isn't bound. (Try /SHOW0.)")
65          t)
66         (;; more protection against /SHOW too early in cold init, part III
67          (not (boundp '*/show*))
68          (cannot-/show "*/SHOW* isn't bound. (Try initializing it earlier.)")
69          t)
70         (;; ordinary, healthy reason to suppress /SHOW, no error
71          ;; output needed
72          (not */show*)
73          t)
74         (t
75          ;; Let the /SHOW go on.
76          nil)))
77
78 ;;; shorthand for a common idiom in output statements used in
79 ;;; debugging: (/SHOW "Case 2:" X Y) becomes a pretty-printed version
80 ;;; of (FORMAT .. "~&/Case 2: X=~S Y=~S~%" X Y), conditional on */SHOW*.
81 (defmacro /show (&rest xlist)
82   #!-sb-show (declare (ignore xlist))
83   #!+sb-show
84   (flet (;; Is X something we want to just show literally by itself?
85          ;; (instead of showing it as NAME=VALUE)
86          (literal-p (x) (or (stringp x) (numberp x))))
87     ;; We build a FORMAT statement out of what we find in XLIST.
88     (let ((format-stream (make-string-output-stream)) ; string arg to FORMAT
89           (format-reverse-rest)  ; reversed &REST argument to FORMAT
90           (first-p t))            ; first pass through loop?
91       (write-string "~&~<~;/" format-stream)
92       (dolist (x xlist)
93         (if first-p
94             (setq first-p nil)
95             (write-string #+ansi-cl " ~_"
96                           #-ansi-cl " " ; for CLISP (CLTL1-ish)
97                           format-stream))
98         (if (literal-p x)
99             (princ x format-stream)
100             (progn (let ((*print-pretty* nil))
101                      (format format-stream "~S=~~S" x))
102                    (push x format-reverse-rest))))
103       (write-string "~;~:>~%" format-stream)
104       (let ((format-string (get-output-stream-string format-stream))
105             (format-rest (reverse format-reverse-rest)))
106         `(locally
107            (declare (optimize (speed 1) (space 2) (safety 3)))
108            (unless (suppress-/show-p)
109              (format *trace-output*
110                      ,format-string
111                      #+ansi-cl (list ,@format-rest)
112                      #-ansi-cl ,@format-rest)) ; for CLISP (CLTL1-ish)
113            (values))))))
114
115 ;;; a disabled-at-compile-time /SHOW, implemented as a macro instead
116 ;;; of a function so that leaving occasionally-useful /SHOWs in place
117 ;;; but disabled incurs no run-time overhead and works even when the
118 ;;; arguments can't be evaluated (e.g. because they're only meaningful
119 ;;; in a debugging version of the system, or just due to bit rot..)
120 (defmacro /noshow (&rest rest)
121   (declare (ignore rest)))
122
123 ;;; like /SHOW, except displaying values in hexadecimal
124 (defmacro /xhow (&rest rest)
125   `(let ((*print-base* 16))
126      (/show ,@rest)))
127 (defmacro /noxhow (&rest rest)
128   (declare (ignore rest)))
129
130 ;;; a trivial version of /SHOW which only prints a constant string,
131 ;;; implemented at a sufficiently low level that it can be used early
132 ;;; in cold init
133 ;;;
134 ;;; Unlike the other /SHOW-related functions, this one doesn't test
135 ;;; */SHOW* at runtime, because messing with special variables early
136 ;;; in cold load is too much trouble to be worth it.
137 (defmacro /show0 (&rest string-designators)
138   ;; We can't use inline MAPCAR here because, at least in 0.6.11.x,
139   ;; this code gets compiled before DO-ANONYMOUS is defined.
140   (declare (notinline mapcar))
141   (let ((s (apply #'concatenate
142                   'simple-string
143                   (mapcar #'string string-designators))))
144     (declare (ignorable s)) ; (for when #!-SB-SHOW)
145     #+sb-xc-host `(/show ,s)
146     #-sb-xc-host `(progn
147                     #!+sb-show
148                     (sb!sys:%primitive print
149                                        ,(concatenate 'simple-string "/" s)))))
150 (defmacro /noshow0 (&rest rest)
151   (declare (ignore rest)))
152
153 ;;; low-level display of a string, works even early in cold init
154 (defmacro /primitive-print (thing)
155   (declare (ignorable thing)) ; (for when #!-SB-SHOW)
156   #!+sb-show
157   (progn
158     #+sb-xc-host `(/show "(/primitive-print)" ,thing)
159     #-sb-xc-host `(sb!sys:%primitive print (the simple-string ,thing))))
160
161 ;;; low-level display of a system word, works even early in cold init
162 (defmacro /hexstr (thing)
163   (declare (ignorable thing)) ; (for when #!-SB-SHOW)
164   #!+sb-show
165   (progn
166     #+sb-xc-host `(/show "(/hexstr)" ,thing)
167     #-sb-xc-host `(sb!sys:%primitive print (hexstr ,thing))))
168
169 (defmacro /nohexstr (thing)
170   (declare (ignore thing)))
171 \f
172 (/show0 "done with show.lisp")