1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
297
  
298
  
299
  
300
  
301
  
302
  
303
  
304
  
305
  
306
  
307
  
308
  
309
  
310
  
311
  
312
  
313
  
314
  
315
  
316
  
317
  
318
  
319
  
320
  
321
  
322
  
323
  
324
  
325
  
326
  
327
  
328
  
329
  
330
  
331
  
332
  
333
  
334
  
335
  
336
  
337
  
338
  
339
  
340
  
341
  
342
  
343
  
344
  
345
  
346
  
347
  
348
  
349
  
350
  
351
  
352
  
353
  
354
  
355
  
356
  
357
  
358
  
359
  
360
  
361
  
362
  
363
  
364
  
365
  
366
  
367
  
368
  
369
  
370
  
371
  
372
  
373
  
374
  
375
  
376
  
377
  
378
  
379
  
380
  
381
  
382
  
383
  
384
  
385
  
386
  
387
  
388
  
389
  
390
  
391
  
392
  
393
  
394
  
395
  
396
  
397
  
398
  
399
  
400
  
401
  
402
  
403
  
404
  
405
  
406
  
407
  
408
  
409
  
410
  
411
  
412
  
413
  
414
  
415
  
416
  
417
  
418
  
419
  
420
  
421
  
422
  
423
  
424
  
425
  
426
  
427
  
428
  
429
  
430
  
431
  
432
  
433
  
434
  
435
  
436
  
437
  
438
  
439
  
440
  
441
  
442
  
443
  
444
  
/* 
|| This file is part of Pike. For copyright information see COPYRIGHT. 
|| Pike is distributed under GPL, LGPL and MPL. See the file COPYING 
|| for more information. 
*/ 
 
#ifndef PIKE_ERROR_H 
#define PIKE_ERROR_H 
 
#ifdef CONFIGURE_TEST 
 
#include <stdio.h> 
 
static inline void Pike_fatal (const char *fmt, ...) 
{ 
  va_list args; 
  va_start (args, fmt); 
  vfprintf (stderr, fmt, args); 
  abort(); 
} 
 
#define Pike_error Pike_fatal 
 
#else  /* !CONFIGURE_TEST */ 
 
#include "global.h" 
#include <setjmp.h> 
 
#if defined(HAVE_SIGSETJMP) && defined(HAVE_SIGLONGJMP) 
#define HAVE_AND_USE_SIGSETJMP 
#define LOW_JMP_BUF           sigjmp_buf 
#define LOW_SETJMP(X)         sigsetjmp(X, 0) 
#define LOW_LONGJMP(X, Y)     siglongjmp(X, Y) 
#elif defined(HAVE__SETJMP) && defined(HAVE__LONGJMP) 
#define HAVE_AND_USE__SETJMP 
#define LOW_JMP_BUF           jmp_buf 
#define LOW_SETJMP(X)         _setjmp(X) 
#define LOW_LONGJMP(X, Y)     _longjmp(X, Y) 
#else 
/* Assume we have setjmp and longjmp, they are after all defined by ANSI C. */ 
#define HAVE_AND_USE_SETJMP 
#define LOW_JMP_BUF           jmp_buf 
#define LOW_SETJMP(X)         setjmp(X) 
#define LOW_LONGJMP(X, Y)     longjmp(X, Y) 
#endif 
 
#if 1 
PMOD_EXPORT extern const char msg_fatal_error[]; 
#define Pike_fatal \ 
 (fprintf(stderr,msg_fatal_error,__FILE__,(long)__LINE__),debug_fatal) 
#else 
/* This is useful when debugging assembler code sometimes... -Hubbe */ 
#define Pike_fatal \ 
 (fprintf(stderr,"%s: Fatal error:\n",__FILE__ ":" DEFINETOSTR(__LINE__) ),debug_fatal) 
#endif 
 
#define pike_fatal_dloc                                                       \ 
 (fprintf (stderr, msg_fatal_error, DLOC_ARGS), debug_fatal) 
 
PMOD_EXPORT DECLSPEC(noreturn) void debug_va_fatal(const char *fmt, va_list args) ATTRIBUTE((noreturn)); 
PMOD_EXPORT DECLSPEC(noreturn) void debug_fatal(const char *fmt, ...) ATTRIBUTE((noreturn)); 
 
 
#include "svalue.h" 
 
 
typedef void (*error_call)(void *); 
 
#ifndef STRUCT_FRAME_DECLARED 
#define STRUCT_FRAME_DECLARED 
struct pike_frame; 
#endif 
 
#define THROW_N_A 0 
#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(...)      fprintf(stderr, __VA_ARGS__) 
#else /* !ONERROR_DEBUG */ 
#define OED_FPRINTF(...) 
#endif /* ONERROR_DEBUG */ 
 
typedef struct ONERROR 
{ 
  struct ONERROR *previous; 
  struct pike_frame *frame_pointer; 
  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; 
  LOW_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; 
  const char *file; 
  int on_stack; 
#endif 
} JMP_BUF; 
 
PMOD_EXPORT extern struct svalue throw_value; 
PMOD_EXPORT extern int throw_severity; 
 
#ifdef PIKE_DEBUG 
PMOD_EXPORT extern const char msg_unsetjmp_nosync_1[]; 
PMOD_EXPORT extern const char msg_unsetjmp_nosync_2[]; 
#define UNSETJMP(X) do{                                               \ 
    check_recovery_context();                                   \ 
    OED_FPRINTF("unsetjmp(%p) %s:%d\n",                         \ 
                 &(X),  __FILE__, __LINE__);                    \ 
    if(Pike_interpreter.recoveries != &X) {                     \ 
      if(Pike_interpreter.recoveries)                           \ 
        Pike_fatal(msg_unsetjmp_nosync_1,                       \ 
                   Pike_interpreter.recoveries->file);          \ 
      else                                                      \ 
        Pike_fatal("%s", msg_unsetjmp_nosync_2);                \ 
    }                                                           \ 
    Pike_interpreter.recoveries=X.previous;                     \ 
    check_recovery_context();                                   \ 
  }while (0) 
 
#ifdef DMALLOC_LOCATION 
#define PERR_LOCATION() DMALLOC_LOCATION() 
#else 
#define PERR_LOCATION() ( __FILE__ ":" DEFINETOSTR(__LINE__) ) 
#endif 
 
#define DEBUG_INIT_REC_ARGS , int on_stack, const char *location 
#define SETJMP(X) LOW_SETJMP((init_recovery(&X, 0, 1, PERR_LOCATION())->recovery)) 
#define SETJMP_SP(jmp, stack_pop_levels)                              \ 
  LOW_SETJMP((init_recovery(&jmp, stack_pop_levels, 1, PERR_LOCATION())->recovery)) 
#else  /* !PIKE_DEBUG */ 
#define DEBUG_INIT_REC_ARGS 
#define SETJMP(X) LOW_SETJMP((init_recovery(&X, 0)->recovery)) 
#define SETJMP_SP(jmp, stack_pop_levels)                              \ 
  LOW_SETJMP((init_recovery(&jmp, stack_pop_levels)->recovery)) 
#define UNSETJMP(X) Pike_interpreter.recoveries=X.previous 
#endif        /* !PIKE_DEBUG */ 
 
 
#ifdef PIKE_DEBUG 
#define SET_ONERROR(X,Y,Z)                                    \ 
  do{                                                           \ 
    check_recovery_context();                                   \ 
    OED_FPRINTF("SET_ONERROR(%p, %p, %p) %s:%d\n",              \ 
                 &(X), (Y), (void *)(Z), __FILE__, __LINE__);   \ 
    X.frame_pointer = Pike_interpreter.frame_pointer;           \ 
    X.func=(error_call)(Y);                                     \ 
    DO_IF_DMALLOC( if( X.func == free ) X.func=dmalloc_free);   \ 
    X.arg=(void *)(Z);                                          \ 
    if(!Pike_interpreter.recoveries) {                          \ 
      X.previous = NULL;                                        \ 
      break;                                                    \ 
    }                                                           \ 
    X.previous=Pike_interpreter.recoveries->onerror;            \ 
    X.file = __FILE__;                                          \ 
    X.line = __LINE__;                                          \ 
    Pike_interpreter.recoveries->onerror=&X;                    \ 
  }while(0) 
 
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[]; 
#define UNSET_ONERROR(X) do {                                 \ 
    check_recovery_context();                                   \ 
    OED_FPRINTF("UNSET_ONERROR(%p) %s:%d\n",                    \ 
                 &(X), __FILE__, __LINE__);                     \ 
    if(!Pike_interpreter.recoveries) break;                     \ 
    if(Pike_interpreter.recoveries->onerror != &(X)) {          \ 
      fprintf(stderr,msg_last_setjmp,                           \ 
              Pike_interpreter.recoveries->file);               \ 
      if (Pike_interpreter.recoveries->onerror) {               \ 
        Pike_fatal(msg_unset_onerr_nosync_1,                    \ 
                   Pike_interpreter.recoveries->onerror, &(X),  \ 
                   Pike_interpreter.recoveries->onerror->file); \ 
      } else {                                                  \ 
        Pike_fatal("%s", msg_unset_onerr_nosync_2);             \ 
      }                                                         \ 
    }                                                           \ 
    Pike_interpreter.recoveries->onerror=(X).previous;          \ 
  } while(0) 
 
PMOD_EXPORT extern const char msg_assert_onerr[]; 
#define ASSERT_ONERROR(X) \ 
  do{ \ 
    if (!Pike_interpreter.recoveries) break; \ 
    if (Pike_interpreter.recoveries->onerror != &X) { \ 
      Pike_fatal(msg_assert_onerr, \ 
                 __FILE__, __LINE__, &(X)); \ 
    } \ 
  }while(0) 
#else /* !PIKE_DEBUG */ 
#define SET_ONERROR(X,Y,Z) \ 
  do{ \ 
     X.func=(error_call)(Y); \ 
     X.arg=(void *)(Z); \ 
     X.frame_pointer = Pike_interpreter.frame_pointer; \ 
     if(!Pike_interpreter.recoveries) {                \ 
       X.previous = NULL;                              \ 
       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 {                \ 
     UNSET_ONERROR(X);                          \ 
     X.func(X.arg);                             \ 
  }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 */ 
PMOD_EXPORT void check_recovery_context(void); 
PMOD_EXPORT void pike_gdb_breakpoint(INT32 args); 
PMOD_EXPORT JMP_BUF *init_recovery(JMP_BUF *r, size_t stack_pop_levels DEBUG_INIT_REC_ARGS); 
PMOD_EXPORT DECLSPEC(noreturn) void pike_throw(void) ATTRIBUTE((noreturn)); 
PMOD_EXPORT void push_error(const char *description); 
PMOD_EXPORT DECLSPEC(noreturn) void low_error(const char *buf) ATTRIBUTE((noreturn)); 
PMOD_EXPORT void va_make_error (const char *fmt, va_list args); 
PMOD_EXPORT void DECLSPEC(noreturn) va_error(const char *fmt, va_list args) ATTRIBUTE((noreturn)); 
PMOD_EXPORT void make_error (const char *fmt, ...); 
PMOD_EXPORT DECLSPEC(noreturn) void Pike_error(const char *fmt,...) ATTRIBUTE((noreturn)); 
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)); 
PMOD_EXPORT void exit_on_error(const void *msg); 
PMOD_EXPORT void fatal_on_error(const void *msg); 
PMOD_EXPORT DECLSPEC(noreturn) void debug_fatal(const char *fmt, ...) ATTRIBUTE((noreturn)); 
PMOD_EXPORT DECLSPEC(noreturn) void generic_error_va( 
  struct object *o, const char *func, const struct svalue *base_sp, int args, 
  const char *fmt, va_list *fmt_args) 
  ATTRIBUTE((noreturn)); 
PMOD_EXPORT DECLSPEC(noreturn) void throw_error_object( 
  struct object *o, 
  const char *func, 
  struct svalue *base_sp,  int args, 
  const char *desc, ...) ATTRIBUTE((noreturn)); 
PMOD_EXPORT void DECLSPEC(noreturn) generic_error( 
  const char *func, 
  struct svalue *base_sp,  int args, 
  const char *desc, ...) ATTRIBUTE((noreturn)); 
PMOD_EXPORT DECLSPEC(noreturn) void index_error( 
  const char *func, 
  struct svalue *base_sp,  int args, 
  struct svalue *val, 
  struct svalue *ind, 
  const char *desc, ...) ATTRIBUTE((noreturn)); 
PMOD_EXPORT DECLSPEC(noreturn) void bad_arg_error( 
  const char *func, 
  struct svalue *base_sp,  int args, 
  int which_arg, 
  const char *expected_type, 
  struct svalue *got, 
  const char *desc, ...)  ATTRIBUTE((noreturn)); 
PMOD_EXPORT void DECLSPEC(noreturn) math_error( 
  const char *func, 
  struct svalue *base_sp,  int args, 
  struct svalue *number, 
  const char *desc, ...) ATTRIBUTE((noreturn)); 
PMOD_EXPORT void DECLSPEC(noreturn) resource_error( 
  const char *func, 
  struct svalue *base_sp,  int args, 
  const char *resource_type, 
  size_t howmuch, 
  const char *desc, ...) ATTRIBUTE((noreturn)); 
PMOD_EXPORT void DECLSPEC(noreturn) permission_error( 
  const char *func, 
  struct svalue *base_sp, int args, 
  const char *permission_type, 
  const char *desc, ...) ATTRIBUTE((noreturn)); 
PMOD_EXPORT void wrong_number_of_args_error(const char *name, int args, int expected) 
  ATTRIBUTE((noreturn)); 
void init_error(void); 
void cleanup_error(void); 
/* Prototypes end here */ 
 
/* Some useful error macros. */ 
 
PMOD_EXPORT extern const char msg_bad_arg[]; 
 
/** 
 * Throw an exception that the type of an argument is wrong. ARG, FUNC 
 * and EXPECT will be inserted into "Bad argument %d to %s(). Expected 
 * %s.\n" 
 * 
 * @param FUNC The name of the function, e.g. "create". 
 * @param ARG The number of the argument, e.g. 1 for the first. 
 * @param EXPECT The expected type, e.g. "int(0..1)". 
 */ 
#define SIMPLE_ARG_TYPE_ERROR(FUNC, ARG, EXPECT) \ 
   bad_arg_error(FUNC, Pike_sp-args, args, ARG, EXPECT, Pike_sp+ARG-1-args,\ 
                 msg_bad_arg, ARG, FUNC, EXPECT) 
/* Less descriptive macro name kept for compatibility. */ 
#define SIMPLE_BAD_ARG_ERROR(FUNC, ARG, EXPECT) \ 
   bad_arg_error(FUNC, Pike_sp-args, args, ARG, EXPECT, Pike_sp+ARG-1-args,\ 
                 msg_bad_arg, ARG, FUNC, EXPECT) 
 
PMOD_EXPORT extern const char msg_bad_arg_2[]; 
 
/** 
 * Throw an exception that there is some problem with the argument 
 * other than the wrong type. PROBLEM is a full sentence without a 
 * trailing newline. Together with FUNC and ARG they will be inserted 
 * into "Bad argument %d to %s(). %s\n" 
 * 
 * @param FUNC The name of the function, e.g. "create". 
 * @param ARG The number of the argument, e.g. 1 for the first. 
 * @param PROBLEM Describes the problem with the argument, e.g. "The 
 * number of bytes has to be even." 
 */ 
#define SIMPLE_ARG_ERROR(FUNC, ARG, PROBLEM) \ 
  bad_arg_error (FUNC, Pike_sp-args, args, ARG, NULL, Pike_sp+ARG-1-args, \ 
                 msg_bad_arg_2, ARG, FUNC, PROBLEM) 
 
/** 
 * Throw an exception that the number of arguents to the function is 
 * wrong. This macro will use the variable args as the number of given 
 * arguments. 
 * 
 * @param FUNC The name of the function, e.g. "create". 
 * @param ARG The number of expected arguments. 
 */ 
#define SIMPLE_WRONG_NUM_ARGS_ERROR(FUNC, ARG) \ 
  wrong_number_of_args_error (FUNC, args, ARG) 
/* The following is for compatibility. */ 
#define SIMPLE_TOO_FEW_ARGS_ERROR(FUNC, ARG) \ 
  wrong_number_of_args_error (FUNC, args, ARG) 
 
PMOD_EXPORT extern const char msg_out_of_mem[]; 
PMOD_EXPORT extern const char msg_out_of_mem_2[]; 
 
#if 1 
static inline void DECLSPEC(noreturn) out_of_memory_error ( 
  const char *func, 
  struct svalue *base_sp,  int args, 
  size_t amount) ATTRIBUTE((noreturn)); 
static inline void DECLSPEC(noreturn) out_of_memory_error ( 
  const char *func, 
  struct svalue *base_sp,  int args, 
  size_t amount) 
{ 
  resource_error (func, base_sp, args, "memory", amount, 
                  amount ? msg_out_of_mem_2 : msg_out_of_mem, amount); 
} 
#else 
#define out_of_memory_error(FUNC, BASE_SP, ARGS, AMOUNT)      \ 
  do {                                                          \ 
    size_t amount_ = (AMOUNT);                                  \ 
    int args_ = (ARGS);                                         \ 
    resource_error((FUNC), (BASE_SP), args_, "memory", amount_, \ 
                   msg_out_of_mem_2, amount_);                  \ 
  } while(0) 
#endif 
 
#define SIMPLE_OUT_OF_MEMORY_ERROR(FUNC, AMOUNT) \ 
   out_of_memory_error(FUNC, Pike_sp-args, args, AMOUNT) 
 
PMOD_EXPORT extern const char msg_div_by_zero[]; 
#define SIMPLE_DIVISION_BY_ZERO_ERROR(FUNC) \ 
     math_error(FUNC, Pike_sp-args, args, 0, msg_div_by_zero) 
 
#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; \ 
            if(__is_exception) { \ 
                free_svalue(&throw_value); \ 
                mark_free_svalue (&throw_value); \ 
            } \ 
            else \ 
                UNSETJMP(exception); \ 
            if(__exception_rethrow) \ 
                rethrow; \ 
        } \ 
        while(0) 
 
/* Generic error stuff */ 
#define ERR_EXT_DECLARE 
#include "errors.h" 
 
#endif        /* !CONFIGURE_TEST */ 
 
#endif /* PIKE_ERROR_H */