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
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
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
))
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
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)."
33 ;; Give the user a chance to unset *BREAK-ON-SIGNALS* on the
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.)
46 (if bos-actually-breaking
47 "Return from BREAK and assign a new value to ~
49 "Assign a new value to *BREAK-ON-SIGNALS* and ~
50 continue with signal handling.")))
56 "Enter new value for *BREAK-ON-SIGNALS*. ~
57 Current value is ~S.~%~
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
)
66 "~S is not a valid value for *BREAK-ON-SIGNALS* ~
67 (must be a type-specifier).~%"
70 (setf *break-on-signals
* new-value
)))))
72 (defun signal (datum &rest arguments
)
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."
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
)))
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.
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
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 |#
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
))
141 (/show0
"*MAXIMUM-ERROR-DEPTH*=..")
143 (/show0
"in INFINITE-ERROR-PROTECTOR, calling ERROR-ERROR")
144 (sb!impl
::error-error
"Help! "
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
)
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=..")
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")
171 (/show0
"done signalling CONDITION within ERROR")
172 (invoke-debugger condition
))))
174 (defun cerror (continue-string datum
&rest arguments
)
175 (infinite-error-protect
177 (continue "~A" (apply #'format nil continue-string arguments
))
178 (let ((condition (coerce-to-condition datum
182 (with-condition-restarts condition
(list (find-restart 'continue
))
183 (let ((sb!debug
:*stack-top-hint
* (or sb
!debug
:*stack-top-hint
* 'cerror
)))
185 (invoke-debugger condition
))))))
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
)))
198 (coerce-to-condition datum arguments
'simple-condition what
)))))
201 (defun break (&optional
(datum "break") &rest arguments
)
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
)
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
)
223 :report
"Skip warning."
224 (return-from %warn nil
)))
225 (format *error-output
* "~&~@<~S: ~3i~:_~A~:>~%"
226 superclassoid-name condition
)))
229 (defun warn (datum &rest arguments
)
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
))