Debian Codegraph @

src/c/error.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * error.d - error handling
6 *
7 * Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya
8 * Copyright (c) 1990 Giuseppe Attardi
9 * Copyright (c) 2001 Juan Jose Garcia Ripoll
10 *
11 * See file 'LICENSE' for the copyright details.
12 *
13 */
14
15 #include <ecl/ecl.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <signal.h>
20 #include <stdlib.h>
21 #if defined(ECL_MS_WINDOWS_HOST) || defined(cygwin)
22 # include <windows.h>
23 #endif
24 #include <ecl/internal.h>
25 #include <ecl/ecl-inl.h>
26
27 static cl_object
28 cl_symbol_or_object(cl_object x)
29 {
30 if (ECL_FIXNUMP(x))
31 return (cl_object)(cl_symbols + ecl_fixnum(x));
32 return x;
33 }
34
35 void
36 _ecl_unexpected_return()
37 {
38 ecl_internal_error(
39 "*** \n"
40 "*** A call to ERROR returned without handling the error.\n"
41 "*** This should have never happened and is usually a signal\n"
42 "*** that the debugger or the universal error handler were\n"
43 "*** improperly coded or altered. Please contact the maintainers\n"
44 "***\n");
45 }
46
47 void
48 ecl_internal_error(const char *s)
49 {
50 int saved_errno = errno;
51 fprintf(stderr, "\nInternal or unrecoverable error in:\n%s\n", s);
52 if (saved_errno) {
53 fprintf(stderr, " [%d: %s]\n", saved_errno, strerror(saved_errno));
54 }
55 fflush(stderr);
56 _ecl_dump_c_backtrace();
57 #ifdef SIGIOT
58 signal(SIGIOT, SIG_DFL); /* avoid getting into a loop with abort */
59 #endif
60 abort();
61 }
62
63 #ifdef ECL_THREADS
64 void
65 ecl_thread_internal_error(const char *s)
66 {
67 int saved_errno = errno;
68 fprintf(stderr, "\nInternal thread error in:\n%s\n", s);
69 if (saved_errno) {
70 fprintf(stderr, " [%d: %s]\n", saved_errno, strerror(saved_errno));
71 }
72 _ecl_dump_c_backtrace();
73 fprintf(stderr,
74 "\nDid you forget to call `ecl_import_current_thread'?\n"
75 "Exitting thread.\n");
76 fflush(stderr);
77 ecl_thread_exit();
78 }
79 #endif
80
81 void
82 ecl_unrecoverable_error(cl_env_ptr the_env, const char *message)
83 {
84 /*
85 * Right now we have no means of specifying a jump point
86 * for really bad events. We just jump to the outermost
87 * frame, which is equivalent to quitting, and wait for
88 * someone to intercept this jump.
89 */
90 ecl_frame_ptr destination;
91 cl_object tag;
92
93 /*
94 * We output the error message with very low level routines
95 * because we can not risk another stack overflow.
96 */
97 writestr_stream(message, cl_core.error_output);
98
99 tag = ECL_SYM_VAL(the_env, @'si::*quit-tag*');
100 the_env->nvalues = 0;
101 if (tag) {
102 destination = frs_sch(tag);
103 if (destination) {
104 ecl_unwind(the_env, destination);
105 }
106 }
107 if (the_env->frs_stack.org <= the_env->frs_stack.top) {
108 destination = ecl_process_env()->frs_stack.org;
109 ecl_unwind(the_env, destination);
110 } else {
111 ecl_internal_error("\n;;;\n;;; No frame to jump to\n;;; Aborting ECL\n;;;");
112 }
113 }
114
115 void
116 ecl_miscompilation_error()
117 {
118 ecl_internal_error(
119 "***\n"
120 "*** Encountered a code path that should have never been taken.\n"
121 "*** This likely indicates a bug in the ECL compiler. Please contact\n"
122 "*** the maintainers.\n"
123 "***\n");
124 }
125
126 /*****************************************************************************/
127 /* Support for Lisp Error Handler */
128 /*****************************************************************************/
129
130 void
131 FEerror(const char *s, int narg, ...)
132 {
133 ecl_va_list args;
134 cl_object rest;
135 ecl_va_start(args, narg, narg, 0);
136 ecl_enable_interrupts();
137 rest = cl_grab_rest_args(args);
138 ecl_va_end(args);
139 funcall(4, @'si::universal-error-handler',
140 ECL_NIL, /* not correctable */
141 ecl_make_constant_base_string(s,-1), /* condition text */
142 rest);
143 _ecl_unexpected_return();
144 }
145
146 cl_object
147 CEerror(cl_object c, const char *err, int narg, ...)
148 {
149 ecl_va_list args;
150 cl_object rest;
151 ecl_va_start(args, narg, narg, 0);
152 ecl_enable_interrupts();
153 rest = cl_grab_rest_args(args);
154 ecl_va_end(args);
155 return funcall(4, @'si::universal-error-handler',
156 c, /* correctable */
157 ecl_make_constant_base_string(err,-1), /* continue-format-string */
158 rest);
159 }
160
161 /***********************
162 * Conditions signaler *
163 ***********************/
164
165 void
166 CEstack_overflow(cl_object type, cl_object limit, cl_object resume)
167 {
168 cl_env_ptr the_env = ecl_process_env();
169 cl_index the_size;
170 if (!Null(resume)) resume = @"Extend stack size";
171 ECL_UNWIND_PROTECT_BEGIN(the_env) {
172 cl_cerror(6, resume, @'ext::stack-overflow', @':type', type, @':size', limit);
173 } ECL_UNWIND_PROTECT_EXIT {
174 /* reset the margin */
175 si_set_limit(type, limit);
176 } ECL_UNWIND_PROTECT_END;
177 /* resize the stack */
178 the_size = ecl_to_size(limit);
179 the_size = the_size + the_size/2;
180 si_set_limit(type, ecl_make_fixnum(the_size));
181 }
182
183 void
184 FEprogram_error(const char *s, int narg, ...)
185 {
186 cl_object real_args, text;
187 ecl_va_list args;
188 ecl_va_start(args, narg, narg, 0);
189 text = ecl_make_constant_base_string(s,-1);
190 real_args = cl_grab_rest_args(args);
191 if (cl_boundp(@'si::*current-form*') != ECL_NIL) {
192 /* When FEprogram_error is invoked from the compiler, we can
193 * provide information about the offending form.
194 */
195 cl_object stmt = ecl_symbol_value(@'si::*current-form*');
196 if (stmt != ECL_NIL) {
197 real_args = @list(3, stmt, text, real_args);
198 text = @"In form~%~S~%~?";
199 }
200 }
201 si_signal_simple_error(4,
202 @'program-error', /* condition name */
203 ECL_NIL, /* not correctable */
204 text,
205 real_args);
206 _ecl_unexpected_return();
207 }
208
209 void
210 FEcontrol_error(const char *s, int narg, ...)
211 {
212 ecl_va_list args;
213 cl_object rest;
214 ecl_va_start(args, narg, narg, 0);
215 rest = cl_grab_rest_args(args);
216 ecl_va_end(args);
217 si_signal_simple_error(4,
218 @'control-error', /* condition name */
219 ECL_NIL, /* not correctable */
220 ecl_make_constant_base_string(s,-1), /* format control */
221 rest); /* format args */
222 _ecl_unexpected_return();
223 }
224
225 void
226 FEreader_error(const char *s, cl_object stream, int narg, ...)
227 {
228 cl_object message = ecl_make_constant_base_string(s,-1);
229 cl_object args_list;
230 ecl_va_list args;
231 ecl_va_start(args, narg, narg, 0);
232 args_list = cl_grab_rest_args(args);
233 ecl_va_end(args);
234 if (Null(stream)) {
235 /* Parser error */
236 si_signal_simple_error(4,
237 @'parse-error', /* condition name */
238 ECL_NIL, /* not correctable */
239 message, /* format control */
240 args_list);
241 } else {
242 /* Actual reader error */
243 cl_object prefix = @"Reader error in file ~S, position ~D:~%";
244 cl_object position = cl_file_position(1, stream);
245 message = si_base_string_concatenate(2, prefix, message);
246 args_list = cl_listX(3, stream, position, args_list);
247 si_signal_simple_error(6,
248 @'reader-error', /* condition name */
249 ECL_NIL, /* not correctable */
250 message, /* format control */
251 args_list, /* format args */
252 @':stream', stream);
253 }
254 _ecl_unexpected_return();
255 }
256
257
258 void
259 FEcannot_open(cl_object file)
260 {
261 cl_object c_error = _ecl_strerror(errno);
262 si_signal_simple_error
263 (6, @'file-error', /* condition */
264 ECL_NIL, /* continuable */
265 @"Cannot open ~S.~%C library error: ~A", /* format */
266 cl_list(2, file, c_error), /* format args */
267 @':pathname', /* file-error options */
268 file);
269 _ecl_unexpected_return();
270 }
271
272 void
273 FEend_of_file(cl_object strm)
274 {
275 cl_error(3, @'end-of-file', @':stream', strm);
276 }
277
278 void
279 FEclosed_stream(cl_object strm)
280 {
281 cl_error(3, @'stream-error', @':stream', strm);
282 }
283
284 cl_object
285 si_signal_type_error(cl_object value, cl_object type)
286 {
287 cl_error(5, @'type-error', @':expected-type', type,
288 @':datum', value);
289 }
290
291 void
292 FEwrong_type_argument(cl_object type, cl_object value)
293 {
294 si_signal_type_error(value, cl_symbol_or_object(type));
295 }
296
297 void
298 FEwrong_type_only_arg(cl_object function, cl_object value, cl_object type)
299 {
300 const char *message =
301 "In ~:[an anonymous function~;~:*function ~A~], "
302 "the value of the only argument is~& ~S~&which is "
303 "not of the expected type ~A";
304 cl_env_ptr env = ecl_process_env();
305 struct ecl_ihs_frame tmp_ihs;
306 function = cl_symbol_or_object(function);
307 type = cl_symbol_or_object(type);
308 if (!Null(function) && env->ihs_stack.top && env->ihs_stack.top->function != function) {
309 ecl_ihs_push(env,&tmp_ihs,function,ECL_NIL);
310 }
311 si_signal_simple_error(8,
312 @'type-error', /* condition name */
313 ECL_NIL, /* not correctable */
314 ecl_make_constant_base_string(message,-1), /* format control */
315 cl_list(3, function, value, type),
316 @':expected-type', type,
317 @':datum', value);
318 _ecl_unexpected_return();
319 }
320
321 void
322 FEwrong_type_nth_arg(cl_object function, cl_narg narg, cl_object value, cl_object type)
323 {
324 const char *message =
325 "In ~:[an anonymous function~;~:*function ~A~], "
326 "the value of the ~:R argument is~& ~S~&which is "
327 "not of the expected type ~A";
328 cl_env_ptr env = ecl_process_env();
329 struct ecl_ihs_frame tmp_ihs;
330 function = cl_symbol_or_object(function);
331 type = cl_symbol_or_object(type);
332 if (!Null(function) && env->ihs_stack.top && env->ihs_stack.top->function != function) {
333 ecl_ihs_push(env,&tmp_ihs,function,ECL_NIL);
334 }
335 si_signal_simple_error(8,
336 @'type-error', /* condition name */
337 ECL_NIL, /* not correctable */
338 ecl_make_constant_base_string(message,-1), /* format control */
339 cl_list(4, function, ecl_make_fixnum(narg),
340 value, type),
341 @':expected-type', type,
342 @':datum', value);
343 _ecl_unexpected_return();
344 }
345
346 void
347 FEwrong_type_key_arg(cl_object function, cl_object key, cl_object value, cl_object type)
348 {
349 const char *message =
350 "In ~:[an anonymous function~;~:*function ~A~], "
351 "the value of the argument ~S is~& ~S~&which is "
352 "not of the expected type ~A";
353 cl_env_ptr env = ecl_process_env();
354 struct ecl_ihs_frame tmp_ihs;
355 function = cl_symbol_or_object(function);
356 type = cl_symbol_or_object(type);
357 key = cl_symbol_or_object(key);
358 if (!Null(function) && env->ihs_stack.top && env->ihs_stack.top->function != function) {
359 ecl_ihs_push(env,&tmp_ihs,function,ECL_NIL);
360 }
361 si_signal_simple_error(8,
362 @'type-error', /* condition name */
363 ECL_NIL, /* not correctable */
364 ecl_make_constant_base_string(message,-1), /* format control */
365 cl_list(4, function, key, value, type),
366 @':expected-type', type,
367 @':datum', value);
368 _ecl_unexpected_return();
369 }
370
371 void
372 FEwrong_index(cl_object function, cl_object a, int which, cl_object ndx,
373 cl_index nonincl_limit)
374 {
375 const char *message1 =
376 "In ~:[an anonymous function~;~:*function ~A~], "
377 "the ~*index into the object~% ~S~%"
378 "takes a value ~D out of the range ~A.";
379 const char *message2 =
380 "In ~:[an anonymous function~;~:*function ~A~], "
381 "the ~:R index into the object~% ~S~%"
382 "takes a value ~D out of the range ~A.";
383 cl_object limit = ecl_make_integer(nonincl_limit-1);
384 cl_object type = ecl_make_integer_type(ecl_make_fixnum(0), limit);
385 cl_object message = ecl_make_constant_base_string((which<0) ? message1 : message2,-1);
386 cl_env_ptr env = ecl_process_env();
387 struct ecl_ihs_frame tmp_ihs;
388 function = cl_symbol_or_object(function);
389 if (!Null(function) && env->ihs_stack.top && env->ihs_stack.top->function != function) {
390 ecl_ihs_push(env,&tmp_ihs,function,ECL_NIL);
391 }
392 cl_error(9,
393 @'simple-type-error', /* condition name */
394 @':format-control', message,
395 @':format-arguments',
396 cl_list(5, function, ecl_make_fixnum(which+1), a, ndx, type),
397 @':expected-type', type,
398 @':datum', ndx);
399 }
400
401 void
402 FEunbound_variable(cl_object sym)
403 {
404 cl_error(3, @'unbound-variable', @':name', sym);
405 }
406
407 void
408 FEundefined_function(cl_object fname)
409 {
410 cl_error(3, @'undefined-function', @':name', fname);
411 }
412
413 void
414 FEprint_not_readable(cl_object x)
415 {
416 cl_error(3, @'print-not-readable', @':object', x);
417 }
418
419 void
420 FEtimeout()
421 {
422 cl_error(1, @'ext::timeout');
423 }
424
425 /*************
426 * Shortcuts *
427 *************/
428
429 void
430 FEwrong_num_arguments(cl_object fun)
431 {
432 fun = cl_symbol_or_object(fun);
433 FEprogram_error("Wrong number of arguments passed to function ~S.",
434 1, fun);
435 }
436
437 void
438 FEwrong_num_arguments_anonym(void)
439 {
440 FEprogram_error("Wrong number of arguments passed to an anonymous function", 0);
441 }
442
443 void
444 FEinvalid_macro_call(cl_object name)
445 {
446 FEerror("Invalid macro call to ~S.", 1, name);
447 }
448
449 void
450 FEinvalid_variable(const char *s, cl_object obj)
451 {
452 FEerror(s, 1, obj);
453 }
454
455 void
456 FEillegal_variable_name(cl_object v)
457 {
458 FEprogram_error("Not a valid variable name ~S.", 1, v);
459 }
460
461 void
462 FEassignment_to_constant(cl_object v)
463 {
464 FEprogram_error("SETQ: Tried to assign a value to the constant ~S.", 1, v);
465 }
466
467 void
468 FEbinding_a_constant(cl_object v)
469 {
470 FEprogram_error("The constant ~S is being bound.", 1, v);
471 }
472
473 void
474 FEinvalid_function(cl_object obj)
475 {
476 FEwrong_type_argument(@'function', obj);
477 }
478
479 void
480 FEinvalid_function_name(cl_object fname)
481 {
482 cl_error(9, @'simple-type-error', @':format-control',
483 @"Not a valid function name ~D.",
484 @':format-arguments', cl_list(1, fname),
485 @':expected-type', cl_list(2, @'satisfies', @'si::valid-function-name-p'),
486 @':datum', fname);
487 }
488
489 #ifdef ECL_THREADS
490 void
491 FEerror_not_owned(cl_object lock)
492 {
493 FEerror("Attempted to give up lock ~S that is not owned by process ~S",
494 2, lock, mp_current_process());
495 }
496
497 void
498 FEunknown_lock_error(cl_object lock)
499 {
500 #ifdef ECL_WINDOWS_THREADS
501 FEwin32_error("When acting on lock ~A, got an unexpected error.", 1, lock);
502 #else
503 FEerror("When acting on lock ~A, got an unexpected error.", 1, lock);
504 #endif
505 }
506 #endif
507
508 /* bootstrap version */
509 static int recursive_error = 0;
510
511 static cl_object
512 universal_error_handler(cl_object continue_string, cl_object datum,
513 cl_object args)
514 {
515 const cl_env_ptr the_env = ecl_process_env();
516 cl_object stream;
517 if (recursive_error)
518 goto ABORT;
519 recursive_error = 1;
520 stream = cl_core.error_output;
521 if (!Null(stream)) {
522 ecl_bds_bind(the_env, @'*print-readably*', ECL_NIL);
523 ecl_bds_bind(the_env, @'*print-level*', ecl_make_fixnum(4));
524 ecl_bds_bind(the_env, @'*print-length*', ecl_make_fixnum(8));
525 ecl_bds_bind(the_env, @'*print-circle*', ECL_NIL);
526 ecl_bds_bind(the_env, @'*print-base*', ecl_make_fixnum(10));
527 writestr_stream("\n;;; Unhandled lisp initialization error", stream);
528 writestr_stream("\n;;; Message:\n", stream);
529 si_write_ugly_object(datum, stream);
530 writestr_stream("\n;;; Arguments:\n", stream);
531 si_write_ugly_object(args, stream);
532 ecl_bds_unwind_n(the_env, 5);
533 }
534 ABORT:
535 ecl_internal_error("\nLisp initialization error.\n");
536 }
537
538 void
539 FEdivision_by_zero(cl_object x, cl_object y)
540 {
541 cl_error(5, @'division-by-zero', @':operation', @'/',
542 @':operands', cl_list(2, x, y));
543 }
544
545 cl_object
546 _ecl_strerror(int code)
547 {
548 const char *error = strerror(code);
549 return ecl_make_simple_base_string(error,-1);
550 }
551
552 /*************************************
553 * Errors generated by the C library *
554 *************************************/
555 /*
556 * Interprets an error code from the C library according to the POSIX
557 * standard, and produces a suitable error message by combining the user
558 * supplied format with an explanation of the cause of the error.
559 */
560 void
561 FElibc_error(const char *msg, int narg, ...)
562 {
563 ecl_va_list args;
564 cl_object rest, error = _ecl_strerror(errno);
565
566 ecl_va_start(args, narg, narg, 0);
567 rest = cl_grab_rest_args(args);
568 ecl_va_end(args);
569
570 FEerror("~?~%C library explanation: ~A.", 3,
571 ecl_make_constant_base_string(msg,-1), rest,
572 error);
573 }
574
575 #if defined(ECL_MS_WINDOWS_HOST) || defined(cygwin)
576 ecl_def_ct_base_string(unknown_error,"[Unable to get error message]",28,static,const);
577
578 void
579 FEwin32_error(const char *msg, int narg, ...)
580 {
581 ecl_va_list args;
582 cl_object rest, win_msg_obj;
583 char *win_msg;
584
585 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_ALLOCATE_BUFFER,
586 0, GetLastError(), 0, (void*)&win_msg, 0, NULL) == 0)
587 win_msg_obj = unknown_error;
588 else {
589 win_msg_obj = ecl_make_simple_base_string(win_msg,-1);
590 LocalFree(win_msg);
591 }
592
593 ecl_va_start(args, narg, narg, 0);
594 rest = cl_grab_rest_args(args);
595 ecl_va_end(args);
596 FEerror("~?~%Windows library explanation: ~A.", 3,
597 ecl_make_constant_base_string(msg,-1), rest,
598 win_msg_obj);
599 }
600 #endif
601
602 /************************************
603 * Higher level interface to errors *
604 ************************************/
605
606 @(defun error (eformat &rest args)
607 @ {
608 ecl_enable_interrupts();
609 funcall(4, @'si::universal-error-handler', ECL_NIL, eformat,
610 cl_grab_rest_args(args));
611 _ecl_unexpected_return();
612 } @)
613
614 @(defun cerror (cformat eformat &rest args)
615 @ {
616 ecl_enable_interrupts();
617 @(return funcall(4, @'si::universal-error-handler', cformat, eformat,
618 cl_grab_rest_args(args)));
619 } @)
620
621 void
622 init_error(void)
623 {
624 ecl_def_c_function(@'si::universal-error-handler',
625 (cl_objectfn_fixed)universal_error_handler,
626 3);
627 }