1.0.25.50: detect binding and alien stack exhaustion
[sbcl.git] / src / code / error.lisp
1 ;;;; SBCL-specific parts of the condition system, i.e. parts which
2 ;;;; don't duplicate/clobber functionality already provided by the
3 ;;;; cross-compilation host Common Lisp
4
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
13
14 (in-package "SB!KERNEL")
15
16 ;;; not sure this is the right place, but where else?
17 (defun style-warn (datum &rest arguments)
18   (/show0 "entering STYLE-WARN")
19   (/show datum arguments)
20   (if (stringp datum)
21       (with-sane-io-syntax
22         (warn 'simple-style-warning
23               :format-control datum
24               :format-arguments arguments))
25       ;; Maybe FIXME: check that the DATUM is a STYLE-WARNING or a
26       ;; specifier for a subtype of STYLE-WARNING?  (I had trouble
27       ;; getting through cold-init with that check enabled, though.)
28       ;; -- RMK, 20080701.
29       (apply #'warn datum arguments)))
30
31 ;;; a utility for SIGNAL, ERROR, CERROR, WARN, COMPILER-NOTIFY and
32 ;;; INVOKE-DEBUGGER: Parse the hairy argument conventions into a
33 ;;; single argument that's directly usable by all the other routines.
34 (defun coerce-to-condition (datum arguments default-type fun-name)
35   (cond ((typep datum 'condition)
36          (when (and arguments (not (eq fun-name 'cerror)))
37            (cerror "Ignore the additional arguments."
38                    'simple-type-error
39                    :datum arguments
40                    :expected-type 'null
41                    :format-control "You may not supply additional arguments ~
42                                     when giving ~S to ~S."
43                    :format-arguments (list datum fun-name)))
44          datum)
45         ((symbolp datum) ; roughly, (SUBTYPEP DATUM 'CONDITION)
46          (apply #'make-condition datum arguments))
47         ((or (stringp datum) (functionp datum))
48          (make-condition default-type
49                          :format-control datum
50                          :format-arguments arguments))
51         (t
52          (error 'simple-type-error
53                 :datum datum
54                 :expected-type '(or symbol string)
55                 :format-control "bad argument to ~S: ~S"
56                 :format-arguments (list fun-name datum)))))
57
58 (define-condition layout-invalid (type-error)
59   ()
60   (:report
61    (lambda (condition stream)
62      (format stream
63              "~@<invalid structure layout: ~
64               ~2I~_A test for class ~4I~_~S ~
65               ~2I~_was passed the obsolete instance ~4I~_~S~:>"
66              (classoid-proper-name (type-error-expected-type condition))
67              (type-error-datum condition)))))
68
69 (define-condition case-failure (type-error)
70   ((name :reader case-failure-name :initarg :name)
71    (possibilities :reader case-failure-possibilities :initarg :possibilities))
72   (:report
73     (lambda (condition stream)
74       (format stream "~@<~S fell through ~S expression. ~
75                       ~:_Wanted one of ~:S.~:>"
76               (type-error-datum condition)
77               (case-failure-name condition)
78               (case-failure-possibilities condition)))))
79
80 (define-condition compiled-program-error (program-error)
81   ((message :initarg :message :reader program-error-message)
82    (source :initarg :source :reader program-error-source))
83   (:report (lambda (condition stream)
84              (format stream "Execution of a form compiled with errors.~%~
85                              Form:~%  ~A~%~
86                              Compile-time error:~%  ~A"
87                        (program-error-source condition)
88                        (program-error-message condition)))))
89
90 (define-condition interpreted-program-error
91     (program-error encapsulated-condition)
92   ;; Unlike COMPILED-PROGRAM-ERROR, we don't need to dump these, so
93   ;; storing the original condition and form is OK.
94   ((form :initarg :form :reader program-error-form))
95   (:report (lambda (condition stream)
96              (format stream "~&Evaluation of~%  ~S~%~
97                              caused error:~%  ~A~%"
98                      (program-error-form condition)
99                      (encapsulated-condition condition)))))
100
101 (define-condition simple-control-error (simple-condition control-error) ())
102 (define-condition simple-file-error    (simple-condition file-error)    ())
103 (define-condition simple-program-error (simple-condition program-error) ())
104 (define-condition simple-stream-error  (simple-condition stream-error)  ())
105 (define-condition simple-parse-error   (simple-condition parse-error)   ())
106
107 (define-condition character-coding-error (error)
108   ((external-format :initarg :external-format :reader character-coding-error-external-format)))
109 (define-condition character-encoding-error (character-coding-error)
110   ((code :initarg :code :reader character-encoding-error-code)))
111 (define-condition character-decoding-error (character-coding-error)
112   ((octets :initarg :octets :reader character-decoding-error-octets)))
113 (define-condition stream-encoding-error (stream-error character-encoding-error)
114   ()
115   (:report
116    (lambda (c s)
117      (let ((stream (stream-error-stream c))
118            (code (character-encoding-error-code c)))
119        (format s "~@<encoding error on stream ~S (~S ~S): ~2I~_~
120                   the character with code ~D cannot be encoded.~@:>"
121                stream ':external-format
122                (character-coding-error-external-format c)
123                code)))))
124 (define-condition stream-decoding-error (stream-error character-decoding-error)
125   ()
126   (:report
127    (lambda (c s)
128      (let ((stream (stream-error-stream c))
129            (octets (character-decoding-error-octets c)))
130        (format s "~@<decoding error on stream ~S (~S ~S): ~2I~_~
131                   the octet sequence ~S cannot be decoded.~@:>"
132                stream ':external-format
133                (character-coding-error-external-format c)
134                octets)))))
135
136 (define-condition c-string-encoding-error (character-encoding-error)
137   ()
138   (:report
139    (lambda (c s)
140      (format s "~@<c-string encoding error (:external-format ~S): ~2I~_~
141                   the character with code ~D cannot be encoded.~@:>"
142                (character-coding-error-external-format c)
143                (character-encoding-error-code c)))))
144
145 (define-condition c-string-decoding-error (character-decoding-error)
146   ()
147   (:report
148    (lambda (c s)
149      (format s "~@<c-string decoding error (:external-format ~S): ~2I~_~
150                   the octet sequence ~S cannot be decoded.~@:>"
151              (character-coding-error-external-format c)
152              (character-decoding-error-octets c)))))
153
154 (define-condition control-stack-exhausted (storage-condition)
155   ()
156   (:report
157     (lambda (condition stream)
158       (declare (ignore condition))
159       (format stream
160               ;; no pretty-printing, because that would use a lot of stack.
161               "Control stack exhausted (no more space for function call frames).
162 This is probably due to heavily nested or infinitely recursive function
163 calls, or a tail call that SBCL cannot or has not optimized away.
164
165 PROCEED WITH CAUTION."))))
166
167 (define-condition binding-stack-exhausted (storage-condition)
168   ()
169   (:report
170     (lambda (condition stream)
171       (declare (ignore condition))
172       (format stream
173               ;; no pretty-printing, because that would use a lot of stack.
174               "Binding stack exhausted.
175
176 PROCEED WITH CAUTION."))))
177
178 (define-condition alien-stack-exhausted (storage-condition)
179   ()
180   (:report
181     (lambda (condition stream)
182       (declare (ignore condition))
183       (format stream
184               ;; no pretty-printing, because that would use a lot of stack.
185               "Alien stack exhausted.
186
187 PROCEED WITH CAUTION."))))
188
189 (define-condition heap-exhausted-error (storage-condition)
190   ()
191   (:report
192    (lambda (condition stream)
193      (declare (ignore condition))
194      (declare (special *heap-exhausted-error-available-bytes*
195                        *heap-exhausted-error-requested-bytes*))
196      ;; See comments in interr.lisp -- there is a method to this madness.
197      (if (and (boundp '*heap-exhausted-error-available-bytes*)
198               (boundp '*heap-exhausted-error-requested-bytes*))
199          (format stream
200                  ;; no pretty-printing, because that will use a lot of heap.
201                  "Heap exhausted (no more space for allocation).
202 There are still ~D bytes available; the request was for ~D bytes.
203
204 PROCEED WITH CAUTION."
205                  *heap-exhausted-error-available-bytes*
206                  *heap-exhausted-error-requested-bytes*)
207          (format stream
208                  "A ~S condition without bindings for heap statistics.  (If
209 you did not expect to see this message, please report it."
210                  'heap-exhausted-error)))))
211
212 (define-condition system-condition (condition)
213   ((address :initarg :address :reader system-condition-address :initform nil)
214    (context :initarg :context :reader system-condition-context :initform nil)))
215
216 (define-condition memory-fault-error (system-condition error) ()
217   (:report
218    (lambda (condition stream)
219      (format stream "Unhandled memory fault at #x~X."
220              (system-condition-address condition)))))
221
222 (define-condition breakpoint-error (system-condition error) ()
223   (:report
224    (lambda (condition stream)
225      (format stream "Unhandled breakpoint/trap at #x~X."
226              (system-condition-address condition)))))
227
228 (define-condition interactive-interrupt (system-condition serious-condition) ()
229   (:report
230    (lambda (condition stream)
231      (format stream "Interactive interrupt at #x~X."
232              (system-condition-address condition)))))