More compact type testing of classes.
[sbcl.git] / src / code / cold-error.lisp
blob2b6bf5563d45dbfc627d60fc428ec3891d84cd1f
1 ;;;; miscellaneous error stuff that needs to be in the cold load
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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!KERNEL")
14 (!defvar *break-on-signals* nil
15 #!+sb-doc
16 "When (TYPEP condition *BREAK-ON-SIGNALS*) is true, then calls to SIGNAL will
17 enter the debugger prior to signalling that condition.")
19 (defun maybe-break-on-signal (condition)
20 (let ((old-bos *break-on-signals*)
21 (bos-actually-breaking nil))
22 (restart-case
23 (let ((break-on-signals *break-on-signals*)
24 (*break-on-signals* nil))
25 ;; The rebinding encloses the TYPEP so that a bogus
26 ;; type specifier will not lead to infinite recursion when
27 ;; TYPEP fails.
28 (when (typep condition break-on-signals)
29 (setf bos-actually-breaking t)
30 (break "~A~%BREAK was entered because of *BREAK-ON-SIGNALS* ~
31 (now rebound to NIL)."
32 condition)))
33 ;; Give the user a chance to unset *BREAK-ON-SIGNALS* on the
34 ;; way out.
36 ;; (e.g.: Consider a long compilation. After a failed compile
37 ;; the user sets *BREAK-ON-SIGNALS* to T, and select the
38 ;; RECOMPILE restart. Once the user diagnoses and fixes the
39 ;; problem, he selects RECOMPILE again... and discovers that
40 ;; he's entered the *BREAK-ON-SIGNALS* hell with no escape,
41 ;; unless we provide this restart.)
42 (reassign (new-value)
43 :report
44 (lambda (stream)
45 (format stream
46 (if bos-actually-breaking
47 "Return from BREAK and assign a new value to ~
48 *BREAK-ON-SIGNALS*."
49 "Assign a new value to *BREAK-ON-SIGNALS* and ~
50 continue with signal handling.")))
51 :interactive
52 (lambda ()
53 (let (new-value)
54 (loop
55 (format *query-io*
56 "Enter new value for *BREAK-ON-SIGNALS*. ~
57 Current value is ~S.~%~
58 > "
59 old-bos)
60 (force-output *query-io*)
61 (let ((*break-on-signals* nil))
62 (setf new-value (eval (read *query-io*)))
63 (if (typep new-value 'type-specifier)
64 (return)
65 (format *query-io*
66 "~S is not a valid value for *BREAK-ON-SIGNALS* ~
67 (must be a type-specifier).~%"
68 new-value))))
69 (list new-value)))
70 (setf *break-on-signals* new-value)))))
72 (defun signal (datum &rest arguments)
73 #!+sb-doc
74 "Invokes the signal facility on a condition formed from DATUM and
75 ARGUMENTS. If the condition is not handled, NIL is returned. If
76 (TYPEP condition *BREAK-ON-SIGNALS*) is true, the debugger is invoked
77 before any signalling is done."
78 (let* ((condition
79 (coerce-to-condition datum arguments 'simple-condition 'signal))
80 (handler-clusters *handler-clusters*)
81 (sb!debug:*stack-top-hint* (or sb!debug:*stack-top-hint* 'signal)))
82 (when *break-on-signals*
83 (maybe-break-on-signal condition))
84 (do ((cluster (pop handler-clusters) (pop handler-clusters)))
85 ((null cluster)
86 nil)
87 ;; Remove CLUSTER from *HANDLER-CLUSTERS*: if a condition is
88 ;; signaled in either the type test, i.e. (the function (car
89 ;; handler)), or the handler, (the function (cdr handler)), the
90 ;; recursive SIGNAL call should not consider CLUSTER as doing
91 ;; would lead to infinite recursive SIGNAL calls.
92 (let ((*handler-clusters* handler-clusters))
93 (dolist (handler cluster)
94 (macrolet ((cast-to-fun (f possibly-symbolp)
95 ;; For efficiency the cases are tested in this order:
96 ;; - FUNCTIONP is just a lowtag test
97 ;; - FDEFN-P is a lowtag + widetag.
98 ;; Avoiding a SYMBOLP test is fine because
99 ;; SYMBOL-FUNCTION rejects bogosity anyway.
100 `(let ((f ,f))
101 (cond ((functionp f) f)
102 (,(if possibly-symbolp `(fdefn-p f) 't)
103 (sb!c:safe-fdefn-fun f))
104 ,@(if possibly-symbolp
105 `((t (symbol-function f))))))))
106 (let ((test (car (truly-the cons handler))))
107 (when (if (%instancep test) ; a condition classoid cell
108 (classoid-cell-typep test condition)
109 (funcall (cast-to-fun test nil) condition))
110 (funcall (cast-to-fun (cdr handler) t) condition)))))))))
112 ;;;; working with *CURRENT-ERROR-DEPTH* and *MAXIMUM-ERROR-DEPTH*
114 ;;; counts of nested errors (with internal errors double-counted)
115 (defvar *maximum-error-depth*) ; this gets set to 10 in !COLD-INIT
116 (!defvar *current-error-depth* 0)
118 ;;; INFINITE-ERROR-PROTECT is used by ERROR and friends to keep us out
119 ;;; of hyperspace.
120 (defmacro infinite-error-protect (&rest forms)
121 `(let ((*current-error-depth* (infinite-error-protector)))
122 (/show0 "in INFINITE-ERROR-PROTECT, incremented error depth")
123 ;; This is almost totally unhelpful. Running with #!+sb-show does not mean
124 ;; that you care to see an additional 16K characters of output
125 ;; each time this macro is used when no error is actually happening.
126 #| #!+sb-show (sb!debug:print-backtrace :count 8) ; arbitrary truncation |#
127 ,@forms))
129 ;;; a helper function for INFINITE-ERROR-PROTECT
130 (defun infinite-error-protector ()
131 (/show0 "entering INFINITE-ERROR-PROTECTOR, *CURRENT-ERROR-DEPTH*=..")
132 (/hexstr *current-error-depth*)
133 ;; *MAXIMUM-ERROR-DEPTH* is not bound during cold-init, and testing BOUNDP
134 ;; is superfluous since REALP will return false either way.
135 (let ((cur (locally (declare (optimize (safety 0))) *current-error-depth*))
136 (max (locally (declare (optimize (safety 0))) *maximum-error-depth*)))
137 (cond ((or (not (fixnump cur)) (not (fixnump max)))
138 (%primitive print "Argh! corrupted error depth, halting")
139 (%primitive sb!c:halt))
140 ((> cur max)
141 (/show0 "*MAXIMUM-ERROR-DEPTH*=..")
142 (/hexstr max)
143 (/show0 "in INFINITE-ERROR-PROTECTOR, calling ERROR-ERROR")
144 (sb!impl::error-error "Help! "
146 " nested errors. "
147 "SB-KERNEL:*MAXIMUM-ERROR-DEPTH* exceeded."))
149 (/show0 "returning normally from INFINITE-ERROR-PROTECTOR")
150 (1+ *current-error-depth*)))))
152 (defun error (datum &rest arguments)
153 #!+sb-doc
154 "Invoke the signal facility on a condition formed from DATUM and ARGUMENTS.
155 If the condition is not handled, the debugger is invoked."
156 (/show0 "entering ERROR, argument list=..")
157 (/hexstr arguments)
159 (/show0 "cold-printing ERROR arguments one by one..")
160 #!+sb-show (dolist (argument arguments)
161 (sb!impl::cold-print argument))
162 (/show0 "done cold-printing ERROR arguments")
164 (infinite-error-protect
165 (let ((condition (coerce-to-condition datum arguments
166 'simple-error 'error))
167 (sb!debug:*stack-top-hint* (or sb!debug:*stack-top-hint* 'error)))
168 (/show0 "done coercing DATUM to CONDITION")
169 (/show0 "signalling CONDITION from within ERROR")
170 (signal condition)
171 (/show0 "done signalling CONDITION within ERROR")
172 (invoke-debugger condition))))
174 (defun cerror (continue-string datum &rest arguments)
175 (infinite-error-protect
176 (with-simple-restart
177 (continue "~A" (apply #'format nil continue-string arguments))
178 (let ((condition (coerce-to-condition datum
179 arguments
180 'simple-error
181 'cerror)))
182 (with-condition-restarts condition (list (find-restart 'continue))
183 (let ((sb!debug:*stack-top-hint* (or sb!debug:*stack-top-hint* 'cerror)))
184 (signal condition)
185 (invoke-debugger condition))))))
186 nil)
188 ;;; like BREAK, but without rebinding *DEBUGGER-HOOK* to NIL, so that
189 ;;; we can use it in system code (e.g. in SIGINT handling) without
190 ;;; messing up --disable-debugger mode (which works by setting
191 ;;; *DEBUGGER-HOOK*); or for that matter, without messing up ordinary
192 ;;; applications which try to do similar things with *DEBUGGER-HOOK*
193 (defun %break (what &optional (datum "break") &rest arguments)
194 (infinite-error-protect
195 (with-simple-restart (continue "Return from ~S." what)
196 (let ((sb!debug:*stack-top-hint* (or sb!debug:*stack-top-hint* '%break)))
197 (invoke-debugger
198 (coerce-to-condition datum arguments 'simple-condition what)))))
199 nil)
201 (defun break (&optional (datum "break") &rest arguments)
202 #!+sb-doc
203 "Print a message and invoke the debugger without allowing any possibility
204 of condition handling occurring."
205 (let ((*debugger-hook* nil) ; as specifically required by ANSI
206 (sb!debug:*stack-top-hint* (or sb!debug:*stack-top-hint* 'break)))
207 (apply #'%break 'break datum arguments)))
209 (defun %warn (datum arguments super default-type)
210 (infinite-error-protect
211 (let ((condition (coerce-to-condition datum arguments default-type 'warn))
212 (superclassoid-name (classoid-name super)))
213 ;: CONDITION is necessarily an INSTANCE,
214 ;; but pedantry requires it be the right subtype of instance.
215 (unless (classoid-typep (%instance-layout condition)
216 super condition)
217 (error 'simple-type-error
218 :datum datum :expected-type superclassoid-name
219 :format-control "~S does not designate a ~A class"
220 :format-arguments (list datum superclassoid-name)))
221 (restart-case (signal condition)
222 (muffle-warning ()
223 :report "Skip warning."
224 (return-from %warn nil)))
225 (format *error-output* "~&~@<~S: ~3i~:_~A~:>~%"
226 superclassoid-name condition)))
227 nil)
229 (defun warn (datum &rest arguments)
230 #!+sb-doc
231 "Warn about a situation by signalling a condition formed by DATUM and
232 ARGUMENTS. While the condition is being signaled, a MUFFLE-WARNING restart
233 exists that causes WARN to immediately return NIL."
234 (%warn datum arguments (find-classoid 'warning) 'simple-warning))
236 (defun style-warn (datum &rest arguments)
237 (%warn datum arguments (find-classoid 'style-warning) 'simple-style-warning))