1 ;;;; functions and macros to define and deal with internal errors
2 ;;;; (i.e. problems that can be signaled from assembler code)
4 ;;;; This software is part of the SBCL system. See the README file for
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.
13 (in-package "SB!KERNEL")
17 (defvar *internal-errors*
18 #.(map 'vector #'cdr sb!c:*backend-internal-errors*))
20 (eval-when (:compile-toplevel :execute)
22 (sb!xc:defmacro deferr (name args &rest body)
23 (let* ((rest-pos (position '&rest args))
24 (required (if rest-pos (subseq args 0 rest-pos) args))
28 (fn-name (symbolicate name "-HANDLER")))
30 ;; FIXME: Having a separate full DEFUN for each error doesn't
31 ;; seem to add much value, and it takes a lot of space. Perhaps
32 ;; we could do this dispatch with a big CASE statement instead?
33 (defun ,fn-name (name ,fp ,context ,sc-offsets)
34 ;; FIXME: It would probably be good to do *STACK-TOP-HINT*
35 ;; tricks to hide this internal error-handling logic from the
36 ;; poor high level user, so his debugger tells him about
37 ;; where his error was detected instead of telling him where
38 ;; he ended up inside the system error-handling logic.
39 (declare (ignorable name ,fp ,context ,sc-offsets))
40 (let (,@(let ((offset -1))
42 `(,var (sb!di::sub-access-debug-var-slot
49 `((,(nth (1+ rest-pos) args)
50 (mapcar (lambda (sc-offset)
51 (sb!di::sub-access-debug-var-slot
55 (nthcdr ,rest-pos ,sc-offsets))))))
57 (setf (svref *internal-errors* ,(error-number-or-lose name))
62 (deferr unknown-error (&rest args)
63 (error "unknown error:~{ ~S~})" args))
65 (deferr object-not-fun-error (object)
68 :expected-type 'function))
70 (deferr object-not-list-error (object)
73 :expected-type 'list))
75 (deferr object-not-bignum-error (object)
78 :expected-type 'bignum))
80 (deferr object-not-ratio-error (object)
83 :expected-type 'ratio))
85 (deferr object-not-single-float-error (object)
88 :expected-type 'single-float))
90 (deferr object-not-double-float-error (object)
93 :expected-type 'double-float))
96 (deferr object-not-long-float-error (object)
99 :expected-type 'long-float))
101 (deferr object-not-simple-string-error (object)
104 :expected-type 'simple-string))
106 (deferr object-not-fixnum-error (object)
109 :expected-type 'fixnum))
111 (deferr object-not-vector-error (object)
114 :expected-type 'vector))
116 (deferr object-not-string-error (object)
119 :expected-type 'string))
121 (deferr object-not-base-string-error (object)
124 :expected-type 'base-string))
126 (deferr object-not-bit-vector-error (object)
129 :expected-type 'bit-vector))
131 (deferr object-not-array-error (object)
134 :expected-type 'array))
136 (deferr object-not-number-error (object)
139 :expected-type 'number))
141 (deferr object-not-rational-error (object)
144 :expected-type 'rational))
146 (deferr object-not-float-error (object)
149 :expected-type 'float))
151 (deferr object-not-real-error (object)
154 :expected-type 'real))
156 (deferr object-not-integer-error (object)
159 :expected-type 'integer))
161 (deferr object-not-cons-error (object)
164 :expected-type 'cons))
166 (deferr object-not-symbol-error (object)
169 :expected-type 'symbol))
171 (deferr undefined-fun-error (fdefn-or-symbol)
172 (error 'undefined-function
173 :name (etypecase fdefn-or-symbol
174 (symbol fdefn-or-symbol)
175 (fdefn (fdefn-name fdefn-or-symbol)))))
177 (deferr invalid-arg-count-error (nargs)
178 (error 'simple-program-error
179 :format-control "invalid number of arguments: ~S"
180 :format-arguments (list nargs)))
182 (deferr bogus-arg-to-values-list-error (list)
183 (error 'simple-type-error
187 "~@<attempt to use VALUES-LIST on a dotted list: ~2I~_~S~:>"
188 :format-arguments (list list)))
190 (deferr unbound-symbol-error (symbol)
191 (error 'unbound-variable :name symbol))
193 (deferr object-not-base-char-error (object)
196 :expected-type 'base-char))
198 (deferr object-not-sap-error (object)
201 :expected-type 'system-area-pointer))
203 (deferr invalid-unwind-error ()
204 (error 'simple-control-error
206 "attempt to RETURN-FROM a block or GO to a tag that no longer exists"
209 (deferr unseen-throw-tag-error (tag)
210 (error 'simple-control-error
211 :format-control "attempt to THROW to a tag that does not exist: ~S"
212 :format-arguments (list tag)))
214 (deferr nil-fun-returned-error (function)
215 (error 'simple-control-error
217 "A function with declared result type NIL returned:~% ~S"
218 :format-arguments (list function)))
220 (deferr nil-array-accessed-error (array)
221 (error 'nil-array-accessed-error
222 :datum array :expected-type '(not (array nil))))
224 (deferr division-by-zero-error (this that)
225 (error 'division-by-zero
227 :operands (list this that)))
229 (deferr object-not-type-error (object type)
230 (error (if (and (typep object 'instance)
231 (layout-invalid (%instance-layout object)))
235 :expected-type type))
237 (deferr layout-invalid-error (object layout)
238 (error 'layout-invalid
240 :expected-type (layout-classoid layout)))
242 (deferr odd-key-args-error ()
243 (error 'simple-program-error
244 :format-control "odd number of &KEY arguments"))
246 (deferr unknown-key-arg-error (key-name)
247 (error 'simple-program-error
248 :format-control "unknown &KEY argument: ~S"
249 :format-arguments (list key-name)))
251 (deferr invalid-array-index-error (array bound index)
252 (error 'simple-type-error
254 "invalid array index ~W for ~S (should be nonnegative and <~W)"
255 :format-arguments (list index array bound)
257 :expected-type `(integer 0 (,bound))))
259 (deferr object-not-simple-array-error (object)
262 :expected-type 'simple-array))
264 (deferr object-not-signed-byte-32-error (object)
267 :expected-type '(signed-byte 32)))
269 (deferr object-not-unsigned-byte-32-error (object)
272 :expected-type '(unsigned-byte 32)))
275 ((define-simple-array-internal-errors ()
279 `(deferr ,(symbolicate
281 (sb!vm:saetp-primitive-type-name saetp)
286 :expected-type '(simple-array
287 ,(sb!vm:saetp-specifier saetp)
289 sb!vm:*specialized-array-element-type-properties*))))
290 (define-simple-array-internal-errors))
292 (deferr object-not-complex-error (object)
295 :expected-type 'complex))
297 (deferr object-not-complex-rational-error (object)
300 :expected-type '(complex rational)))
302 (deferr object-not-complex-single-float-error (object)
305 :expected-type '(complex single-float)))
307 (deferr object-not-complex-double-float-error (object)
310 :expected-type '(complex double-float)))
313 (deferr object-not-complex-long-float-error (object)
316 :expected-type '(complex long-float)))
318 (deferr object-not-weak-pointer-error (object)
321 :expected-type 'weak-pointer))
323 (deferr object-not-instance-error (object)
326 :expected-type 'instance))
328 (deferr object-not-complex-vector-error (object)
331 :expected-type '(and vector (not simple-array))))
333 ;;;; fetching errorful function name
335 ;;; This flag is used to prevent infinite recursive lossage when
336 ;;; we can't find the caller for some reason.
337 (defvar *finding-name* nil)
339 (defun find-caller-name-and-frame ()
341 (values "<error finding caller name -- already finding name>" nil)
343 (let* ((*finding-name* t)
344 (frame (sb!di:frame-down (sb!di:frame-down (sb!di:top-frame))))
345 (name (sb!di:debug-fun-name
346 (sb!di:frame-debug-fun frame))))
347 (sb!di:flush-frames-above frame)
350 (values "<error finding caller name -- trapped error>" nil))
351 (sb!di:debug-condition ()
352 (values "<error finding caller name -- trapped debug-condition>"
355 (defun find-interrupted-name ()
356 (/show0 "entering FIND-INTERRUPTED-NAME")
358 (values "<error finding interrupted name -- already finding name>" nil)
360 (let ((*finding-name* t))
361 (/show0 "in ordinary case")
362 (do ((frame (sb!di:top-frame) (sb!di:frame-down frame)))
364 (/show0 "null frame")
365 (values "<error finding interrupted name -- null frame>" nil))
366 (/show0 "at head of DO loop")
367 (when (and (sb!di::compiled-frame-p frame)
368 (sb!di::compiled-frame-escaped frame))
369 (sb!di:flush-frames-above frame)
370 (/show0 "returning from within DO loop")
371 (return (values (sb!di:debug-fun-name
372 (sb!di:frame-debug-fun frame))
375 (/show0 "trapped ERROR")
376 (values "<error finding interrupted name -- trapped error>" nil))
377 (sb!di:debug-condition ()
378 (/show0 "trapped DEBUG-CONDITION")
379 (values "<error finding interrupted name -- trapped debug-condition>"
382 ;;;; INTERNAL-ERROR signal handler
384 (defvar *internal-error-args*)
386 (defun internal-error (context continuable)
387 (declare (type system-area-pointer context))
388 (declare (ignore continuable))
389 (/show0 "entering INTERNAL-ERROR, CONTEXT=..")
391 (infinite-error-protect
392 (/show0 "about to bind ALIEN-CONTEXT")
393 (let ((alien-context (locally
394 (declare (optimize (inhibit-warnings 3)))
395 (sb!alien:sap-alien context (* os-context-t)))))
396 (/show0 "about to bind ERROR-NUMBER and ARGUMENTS")
397 (multiple-value-bind (error-number arguments)
398 (sb!vm:internal-error-args alien-context)
400 ;; There's a limit to how much error reporting we can usefully
401 ;; do before initialization is complete, but try to be a little
402 ;; bit helpful before we die.
403 (/show0 "back from INTERNAL-ERROR-ARGS, ERROR-NUMBER=..")
404 (/hexstr error-number)
405 (/show0 "cold/low ARGUMENTS=..")
407 (unless *cold-init-complete-p*
408 (%primitive print "can't recover from error in cold init, halting")
409 (%primitive sb!c:halt))
411 (multiple-value-bind (name sb!debug:*stack-top-hint*)
412 (find-interrupted-name)
413 (/show0 "back from FIND-INTERRUPTED-NAME")
414 (let ((fp (int-sap (sb!vm:context-register alien-context
416 (handler (and (< -1 error-number (length *internal-errors*))
417 (svref *internal-errors* error-number))))
418 (cond ((null handler)
421 "unknown internal error, ~D, args=~S"
424 (mapcar (lambda (sc-offset)
425 (sb!di::sub-access-debug-var-slot
426 fp sc-offset alien-context))
428 ((not (functionp handler))
430 :format-control "internal error ~D: ~A; args=~S"
434 (mapcar (lambda (sc-offset)
435 (sb!di::sub-access-debug-var-slot
436 fp sc-offset alien-context))
439 (funcall handler name fp alien-context arguments)))))))))
441 (defun control-stack-exhausted-error ()
442 (let ((sb!debug:*stack-top-hint* nil))
443 (infinite-error-protect
444 (format *error-output*
445 "Control stack guard page temporarily disabled: proceed with caution~%")
446 (error 'control-stack-exhausted))))