3e7aa82000-11-30Fredrik Hübinette (Hubbe) /*\ ||| This file a part of Pike, and is copyright by Fredrik Hubinette ||| Pike is distributed as GPL (General Public License) ||| See the files COPYING and DISCLAIMER for more information. \*/ /*
c299402001-11-10Martin Stjernholm  * $Id: pike_error.h,v 1.16 2001/11/10 19:43:51 mast Exp $
3e7aa82000-11-30Fredrik Hübinette (Hubbe)  */
aad99b2001-03-28Fredrik Hübinette (Hubbe) #ifndef PIKE_ERROR_H #define PIKE_ERROR_H
3e7aa82000-11-30Fredrik Hübinette (Hubbe)  #include "machine.h" #ifdef HAVE_SETJMP_H #include <setjmp.h> #undef HAVE_SETJMP_H #endif #include <stdarg.h>
aad99b2001-03-28Fredrik Hübinette (Hubbe) #if 1
c299402001-11-10Martin Stjernholm PMOD_EXPORT extern const char msg_fatal_error[];
aad99b2001-03-28Fredrik Hübinette (Hubbe) #define fatal \
c299402001-11-10Martin Stjernholm  (fprintf(stderr,msg_fatal_error,__FILE__,__LINE__),debug_fatal)
aad99b2001-03-28Fredrik Hübinette (Hubbe) #else /* This is useful when debugging assembler code sometimes... -Hubbe */ #define fatal \ (fprintf(stderr,"%s: Fatal error:\n",__FILE__ ":" DEFINETOSTR(__LINE__) ),debug_fatal) #endif PMOD_EXPORT DECLSPEC(noreturn) void debug_fatal(const char *fmt, ...) ATTRIBUTE((noreturn,format (printf, 1, 2)));
3e7aa82000-11-30Fredrik Hübinette (Hubbe) #include "svalue.h" typedef void (*error_call)(void *); #ifndef STRUCT_FRAME_DECLARED #define STRUCT_FRAME_DECLARED struct pike_frame; #endif #define THROW_ERROR 10 #define THROW_THREAD_EXIT 20 #define THROW_THREAD_KILLED 30 #define THROW_EXIT 40 #define THROW_MAX_SEVERITY 100 /* #define ONERROR_DEBUG */ #ifdef ONERROR_DEBUG #define OED_FPRINTF(X) fprintf X #else /* !ONERROR_DEBUG */ #define OED_FPRINTF(X) #endif /* ONERROR_DEBUG */ typedef struct ONERROR { struct ONERROR *previous; error_call func; void *arg; #ifdef PIKE_DEBUG const char *file; int line; #endif /* PIKE_DEBUG */ } ONERROR; typedef struct JMP_BUF { struct JMP_BUF *previous; jmp_buf recovery; struct pike_frame *frame_pointer; ptrdiff_t stack_pointer; ptrdiff_t mark_sp; INT32 severity; ONERROR *onerror; #ifdef PIKE_DEBUG int line; char *file; #endif } JMP_BUF;
f31ced2000-12-16Marcus Comstedt PMOD_EXPORT extern struct svalue throw_value;
3e7aa82000-11-30Fredrik Hübinette (Hubbe) extern int throw_severity; #ifdef PIKE_DEBUG
c299402001-11-10Martin Stjernholm PMOD_EXPORT extern const char msg_unsetjmp_nosync_1[]; PMOD_EXPORT extern const char msg_unsetjmp_nosync_2[];
3e7aa82000-11-30Fredrik Hübinette (Hubbe) #define UNSETJMP(X) do{ \ check_recovery_context(); \ OED_FPRINTF((stderr, "unsetjmp(%p) %s:%d\n", \ &(X), __FILE__, __LINE__)); \ if(Pike_interpreter.recoveries != &X) { \ if(Pike_interpreter.recoveries) \
c299402001-11-10Martin Stjernholm  fatal(msg_unsetjmp_nosync_1,Pike_interpreter.recoveries->file); \
3e7aa82000-11-30Fredrik Hübinette (Hubbe)  else \
c299402001-11-10Martin Stjernholm  fatal(msg_unsetjmp_nosync_2); \
3e7aa82000-11-30Fredrik Hübinette (Hubbe)  } \ Pike_interpreter.recoveries=X.previous; \ check_recovery_context(); \ }while (0)
38e1e82001-11-08Fredrik Hübinette (Hubbe)  #ifdef DMALLOC_LOCATION #define PERR_LOCATION() DMALLOC_LOCATION() #else #define PERR_LOCATION() ( __FILE__ ":" DEFINETOSTR(__LINE__) ) #endif #define DEBUG_LINE_ARGS ,char *location #define SETJMP(X) setjmp((init_recovery(&X, PERR_LOCATION())->recovery))
3e7aa82000-11-30Fredrik Hübinette (Hubbe) #else #define DEBUG_LINE_ARGS #define SETJMP(X) setjmp((init_recovery(&X)->recovery)) #define UNSETJMP(X) Pike_interpreter.recoveries=X.previous #endif #ifdef PIKE_DEBUG #define SET_ONERROR(X,Y,Z) \ do{ \ check_recovery_context(); \ OED_FPRINTF((stderr, "SET_ONERROR(%p, %p, %p) %s:%d\n", \ &(X), (Y), (void *)(Z), __FILE__, __LINE__)); \ X.func=(error_call)(Y); \ DO_IF_DMALLOC( if( X.func == free ) X.func=dmalloc_free;) \ X.arg=(void *)(Z); \ if(!Pike_interpreter.recoveries) break; \ X.previous=Pike_interpreter.recoveries->onerror; \ X.file = __FILE__; \ X.line = __LINE__; \ Pike_interpreter.recoveries->onerror=&X; \ }while(0)
c299402001-11-10Martin Stjernholm PMOD_EXPORT extern const char msg_last_setjmp[]; PMOD_EXPORT extern const char msg_unset_onerr_nosync_1[]; PMOD_EXPORT extern const char msg_unset_onerr_nosync_2[];
3e7aa82000-11-30Fredrik Hübinette (Hubbe) #define UNSET_ONERROR(X) do {\ check_recovery_context(); \ OED_FPRINTF((stderr, "UNSET_ONERROR(%p) %s:%d\n", \ &(X), __FILE__, __LINE__)); \ if(!Pike_interpreter.recoveries) break; \ if(Pike_interpreter.recoveries->onerror != &(X)) { \
c299402001-11-10Martin Stjernholm  fprintf(stderr,msg_last_setjmp,Pike_interpreter.recoveries->file); \
3e7aa82000-11-30Fredrik Hübinette (Hubbe)  if (Pike_interpreter.recoveries->onerror) { \
c299402001-11-10Martin Stjernholm  fatal(msg_unset_onerr_nosync_1,\ Pike_interpreter.recoveries->onerror, &(X), \
38e1e82001-11-08Fredrik Hübinette (Hubbe)  Pike_interpreter.recoveries->onerror->file); \
3e7aa82000-11-30Fredrik Hübinette (Hubbe)  } else { \
c299402001-11-10Martin Stjernholm  fatal(msg_unset_onerr_nosync_2); \
3e7aa82000-11-30Fredrik Hübinette (Hubbe)  } \ } \ Pike_interpreter.recoveries->onerror=(X).previous; \ } while(0)
c299402001-11-10Martin Stjernholm PMOD_EXPORT extern const char msg_assert_onerr[];
3e7aa82000-11-30Fredrik Hübinette (Hubbe) #define ASSERT_ONERROR(X) \ do{ \ if (!Pike_interpreter.recoveries) break; \ if (Pike_interpreter.recoveries->onerror != &X) { \
c299402001-11-10Martin Stjernholm  fatal(msg_assert_onerr, \
3e7aa82000-11-30Fredrik Hübinette (Hubbe)  __FILE__, __LINE__, &(X)); \ } \ }while(0) #else /* !PIKE_DEBUG */ #define SET_ONERROR(X,Y,Z) \ do{ \ X.func=(error_call)(Y); \ X.arg=(void *)(Z); \ if(!Pike_interpreter.recoveries) break; \ X.previous=Pike_interpreter.recoveries->onerror; \ Pike_interpreter.recoveries->onerror=&X; \ }while(0) #define UNSET_ONERROR(X) Pike_interpreter.recoveries && (Pike_interpreter.recoveries->onerror=X.previous) #define ASSERT_ONERROR(X) #endif /* PIKE_DEBUG */ #define CALL_AND_UNSET_ONERROR(X) do { \ X.func(X.arg); \ UNSET_ONERROR(X); \ }while(0) #if defined(PIKE_DEBUG) && 0 /* Works, but probably not interresting for most people * /grubba 1998-04-11 */ #define PIKE_ERROR(NAME, TEXT, SP, ARGS) new_error(NAME, TEXT, SP, ARGS, __FILE__, __LINE__); #else #define PIKE_ERROR(NAME, TEXT, SP, ARGS) new_error(NAME, TEXT, SP, ARGS, NULL, 0); #endif /* PIKE_DEBUG */ /* Prototypes begin here */
f31ced2000-12-16Marcus Comstedt PMOD_EXPORT void check_recovery_context(void);
3e7aa82000-11-30Fredrik Hübinette (Hubbe) PMOD_EXPORT void pike_gdb_breakpoint(void); PMOD_EXPORT JMP_BUF *init_recovery(JMP_BUF *r DEBUG_LINE_ARGS); PMOD_EXPORT DECLSPEC(noreturn) void pike_throw(void) ATTRIBUTE((noreturn));
66a1572001-01-12Martin Stjernholm PMOD_EXPORT void push_error(const char *description);
fcec352000-12-04Martin Stjernholm PMOD_EXPORT DECLSPEC(noreturn) void low_error(const char *buf) ATTRIBUTE((noreturn));
db57b12001-03-07Henrik Grubbström (Grubba) void DECLSPEC(noreturn) va_error(const char *fmt, va_list args) ATTRIBUTE((noreturn));
3e7aa82000-11-30Fredrik Hübinette (Hubbe) PMOD_EXPORT DECLSPEC(noreturn) void new_error(const char *name, const char *text, struct svalue *oldsp, INT32 args, const char *file, int line) ATTRIBUTE((noreturn));
c299402001-11-10Martin Stjernholm PMOD_EXPORT void exit_on_error(const void *msg); PMOD_EXPORT void fatal_on_error(const void *msg);
3e7aa82000-11-30Fredrik Hübinette (Hubbe) PMOD_EXPORT DECLSPEC(noreturn) void Pike_error(const char *fmt,...) ATTRIBUTE((noreturn,format (printf, 1, 2))); PMOD_EXPORT DECLSPEC(noreturn) void debug_fatal(const char *fmt, ...) ATTRIBUTE((noreturn,format (printf, 1, 2))); void DECLSPEC(noreturn) generic_error_va(struct object *o,
c299402001-11-10Martin Stjernholm  const char *func,
3e7aa82000-11-30Fredrik Hübinette (Hubbe)  struct svalue *base_sp, int args,
c299402001-11-10Martin Stjernholm  const char *fmt,
3e7aa82000-11-30Fredrik Hübinette (Hubbe)  va_list foo) ATTRIBUTE((noreturn));
78283b2001-08-16Martin Stjernholm PMOD_EXPORT DECLSPEC(noreturn) void throw_error_object( struct object *o,
c299402001-11-10Martin Stjernholm  const char *func,
78283b2001-08-16Martin Stjernholm  struct svalue *base_sp, int args,
c299402001-11-10Martin Stjernholm  const char *desc, ...) ATTRIBUTE((noreturn,format (printf, 5, 6)));
f31ced2000-12-16Marcus Comstedt PMOD_EXPORT void DECLSPEC(noreturn) generic_error(
c299402001-11-10Martin Stjernholm  const char *func,
3e7aa82000-11-30Fredrik Hübinette (Hubbe)  struct svalue *base_sp, int args,
c299402001-11-10Martin Stjernholm  const char *desc, ...) ATTRIBUTE((noreturn,format (printf, 4, 5)));
a7bd072000-12-18Fredrik Hübinette (Hubbe) PMOD_EXPORT DECLSPEC(noreturn) void index_error(
c299402001-11-10Martin Stjernholm  const char *func,
3e7aa82000-11-30Fredrik Hübinette (Hubbe)  struct svalue *base_sp, int args, struct svalue *val, struct svalue *ind,
c299402001-11-10Martin Stjernholm  const char *desc, ...) ATTRIBUTE((noreturn,format (printf, 6, 7)));
a7bd072000-12-18Fredrik Hübinette (Hubbe) PMOD_EXPORT DECLSPEC(noreturn) void bad_arg_error(
c299402001-11-10Martin Stjernholm  const char *func,
3e7aa82000-11-30Fredrik Hübinette (Hubbe)  struct svalue *base_sp, int args, int which_arg,
c299402001-11-10Martin Stjernholm  const char *expected_type,
3e7aa82000-11-30Fredrik Hübinette (Hubbe)  struct svalue *got,
c299402001-11-10Martin Stjernholm  const char *desc, ...) ATTRIBUTE((noreturn,format (printf, 7, 8)));
f31ced2000-12-16Marcus Comstedt PMOD_EXPORT void DECLSPEC(noreturn) math_error(
c299402001-11-10Martin Stjernholm  const char *func,
3e7aa82000-11-30Fredrik Hübinette (Hubbe)  struct svalue *base_sp, int args, struct svalue *number,
c299402001-11-10Martin Stjernholm  const char *desc, ...) ATTRIBUTE((noreturn,format (printf, 5, 6)));
f31ced2000-12-16Marcus Comstedt PMOD_EXPORT void DECLSPEC(noreturn) resource_error(
c299402001-11-10Martin Stjernholm  const char *func,
3e7aa82000-11-30Fredrik Hübinette (Hubbe)  struct svalue *base_sp, int args,
c299402001-11-10Martin Stjernholm  const char *resource_type,
3e7aa82000-11-30Fredrik Hübinette (Hubbe)  size_t howmuch,
c299402001-11-10Martin Stjernholm  const char *desc, ...) ATTRIBUTE((noreturn,format (printf, 6, 7)));
f31ced2000-12-16Marcus Comstedt PMOD_EXPORT void DECLSPEC(noreturn) permission_error(
c299402001-11-10Martin Stjernholm  const char *func,
3e7aa82000-11-30Fredrik Hübinette (Hubbe)  struct svalue *base_sp, int args,
c299402001-11-10Martin Stjernholm  const char *permission_type, const char *desc, ...) ATTRIBUTE((noreturn, format(printf, 5, 6))); PMOD_EXPORT void wrong_number_of_args_error(const char *name, int args, int expected);
3e7aa82000-11-30Fredrik Hübinette (Hubbe) void init_error(void); void cleanup_error(void); /* Prototypes end here */
a4a1722000-12-05Per Hedbor /* Some useful error macros. */
3e7aa82000-11-30Fredrik Hübinette (Hubbe) 
c299402001-11-10Martin Stjernholm PMOD_EXPORT extern const char msg_bad_arg[];
3e7aa82000-11-30Fredrik Hübinette (Hubbe) #define SIMPLE_BAD_ARG_ERROR(FUNC, ARG, EXPECT) \ bad_arg_error(FUNC, Pike_sp-args, args, ARG, EXPECT, Pike_sp+ARG-1-args,\
c299402001-11-10Martin Stjernholm  msg_bad_arg, ARG, FUNC, EXPECT)
3e7aa82000-11-30Fredrik Hübinette (Hubbe) 
c299402001-11-10Martin Stjernholm PMOD_EXPORT extern const char msg_too_few_args[];
3e7aa82000-11-30Fredrik Hübinette (Hubbe) #define SIMPLE_TOO_FEW_ARGS_ERROR(FUNC, ARG) \ bad_arg_error(FUNC, Pike_sp-args, args, ARG, "void", 0,\
c299402001-11-10Martin Stjernholm  msg_too_few_args,FUNC)
3e7aa82000-11-30Fredrik Hübinette (Hubbe) 
c299402001-11-10Martin Stjernholm PMOD_EXPORT extern const char msg_out_of_mem[];
3e7aa82000-11-30Fredrik Hübinette (Hubbe) #define SIMPLE_OUT_OF_MEMORY_ERROR(FUNC, AMOUNT) \
c299402001-11-10Martin Stjernholm  resource_error(FUNC, Pike_sp-args, args, "memory", AMOUNT, msg_out_of_mem)
3e7aa82000-11-30Fredrik Hübinette (Hubbe) 
c299402001-11-10Martin Stjernholm PMOD_EXPORT extern const char msg_div_by_zero[];
3e7aa82000-11-30Fredrik Hübinette (Hubbe) #define SIMPLE_DIVISION_BY_ZERO_ERROR(FUNC) \
c299402001-11-10Martin Stjernholm  math_error(FUNC, Pike_sp-args, args, 0, msg_div_by_zero)
3e7aa82000-11-30Fredrik Hübinette (Hubbe)  #ifndef PIKE_DEBUG #define check_recovery_context() ((void)0) #endif /* Experimental convenience exception macros. */ #define exception_try \ do \ { \ int __exception_rethrow, __is_exception; \ JMP_BUF exception; \ __is_exception = SETJMP(exception); \ __exception_rethrow = 0; \ if(__is_exception) /* rethrow needs this */ \ UNSETJMP(exception); \ if(!__is_exception) #define exception_catch_if \ else if #define exception_catch(e) \ exception_catch_if(exception->severity = (e)) #define exception_catch_all \ exception_catch_if(1) #define exception_semicatch_all \ exception_catch_if((__exception_rethrow = 1)) #define rethrow \ pike_throw() #define exception_endtry \ else \ __exception_rethrow = 1; \
c1b8f02001-07-02Martin Stjernholm  if(__is_exception) { \ free_svalue(&throw_value); \ throw_value.type = T_INT; \ } \ else \
3e7aa82000-11-30Fredrik Hübinette (Hubbe)  UNSETJMP(exception); \ if(__exception_rethrow) \ rethrow; \ } \ while(0)
a4a1722000-12-05Per Hedbor /* Generic error stuff */
3e7aa82000-11-30Fredrik Hübinette (Hubbe) #define ERR_EXT_DECLARE #include "errors.h"
aad99b2001-03-28Fredrik Hübinette (Hubbe) #endif /* PIKE_ERROR_H */