cb22561995-10-11Fredrik Hübinette (Hubbe) /*\
06983f1996-09-22Fredrik Hübinette (Hubbe) ||| This file a part of Pike, and is copyright by Fredrik Hubinette ||| Pike is distributed as GPL (General Public License)
cb22561995-10-11Fredrik Hübinette (Hubbe) ||| See the files COPYING and DISCLAIMER for more information. \*/
2f748a1999-02-20Henrik Grubbström (Grubba) /**/
be9ba12000-07-06Fredrik Hübinette (Hubbe) #define NO_PIKE_SHORTHAND
5267b71995-08-09Fredrik Hübinette (Hubbe) #include "global.h"
bb55f81997-03-16Fredrik Hübinette (Hubbe) #include "pike_macros.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) #include "error.h" #include "interpret.h" #include "stralloc.h"
06983f1996-09-22Fredrik Hübinette (Hubbe) #include "builtin_functions.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) #include "array.h" #include "object.h"
ef7c761998-03-24Fredrik Hübinette (Hubbe) #include "main.h" #include "builtin_functions.h"
c08c8b1998-04-05Fredrik Hübinette (Hubbe) #include "backend.h"
4406cf1998-04-13Henrik Grubbström (Grubba) #include "operators.h"
81299f1999-03-19Fredrik Hübinette (Hubbe) #include "module_support.h"
db40ab1999-04-15Fredrik Hübinette (Hubbe) #include "threads.h"
20513c2000-04-12Fredrik Hübinette (Hubbe) #include "gc.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) 
17fd852000-08-15Henrik Grubbström (Grubba) RCSID("$Id: error.c,v 1.59 2000/08/15 11:42:44 grubba Exp $");
24ddc71998-03-28Henrik Grubbström (Grubba) 
e9f9141996-09-29Fredrik Hübinette (Hubbe) #undef ATTRIBUTE #define ATTRIBUTE(X)
15e5e92000-08-15Henrik Grubbström (Grubba) /* * Backtrace handling. */ struct pike_backtrace { struct pike_frame *trace; struct pike_frame *iterator; int iterator_index; }; static struct program *frame_program = NULL; #define THIS_BT ((struct pike_backtrace *)Pike_fp->current_storage) static void init_backtrace(struct object *o) { MEMSET(THIS_BT, 0, sizeof(struct pike_backtrace)); } static void exit_backtrace(struct object *o) { if (THIS_BT->trace) { free_pike_frame(THIS_BT->trace); THIS_BT->trace = NULL; THIS_BT->iterator = NULL; } } /* void create() */ void f_bt_create(INT32 args) { if (THIS_BT->trace) { free_pike_frame(THIS_BT->trace); } add_ref(THIS_BT->trace = Pike_fp); THIS_BT->iterator = THIS_BT->trace; THIS_BT->iterator_index = 0; pop_n_elems(args); push_int(0); } /* int _sizeof() */ static void f_bt__sizeof(INT32 args) { int size = 0; struct pike_frame *f = THIS_BT->iterator; size = THIS_BT->iterator_index; while (f) { size++; f = f->next; } pop_n_elems(args); push_int(size); } /* array(int) _indices() */ static void f_bt__indices(INT32 args) { /* Not really optimal, but who performs indices() on a backtrace? */ f_bt__sizeof(args); f_allocate(1); f_indices(1); } /* array _values() */ static void f_bt__values(INT32 args) { struct pike_frame *f = THIS_BT->trace; struct array *a; int i; int sz; f_bt__sizeof(args); f_allocate(1); for (i=0, a=Pike_sp[-1].u.array, sz=a->size; (i < sz) && f; i++, f=f->next) { struct object *o = low_clone(frame_program); call_c_initializers(o); add_ref(*((struct pike_frame **)(o->storage)) = f); push_object(o); a->item[i] = Pike_sp[-1]; Pike_sp--; } } /* object(frame) `[](int index) */ static void f_bt__index(INT32 args) { INT_TYPE index; int i; struct pike_frame *f; struct object *o; get_all_args("backtrace->`[]", args, "%i", &index); if (index < 0) { /* Indexing from end not supported. */ index_error("backtrace->`[]", Pike_sp-args, args, NULL, Pike_sp-args, "Indexing with negative value (%d).\n", index); } if (index < THIS_BT->iterator_index) { THIS_BT->iterator = THIS_BT->trace; THIS_BT->iterator_index = 0; } f = THIS_BT->iterator; i = THIS_BT->iterator_index; while (f && (i < index)) { f = f->next; i++; } if (!f) { index_error("backtrace->`[]", Pike_sp-args, args, NULL, Pike_sp-args, "Index out of range [0..%d].\n", i-1); } THIS_BT->iterator = f; THIS_BT->iterator_index = i; o = low_clone(frame_program); call_c_initializers(o); add_ref(*((struct pike_frame **)(o->storage)) = f); pop_n_elems(args); push_object(o); } /* * Frame handling. */ #define THIS_PF ((struct pike_frame **)Pike_fp->current_storage) static void init_pike_frame(struct object *o) { THIS_PF[0] = NULL; } static void exit_pike_frame(struct object *o) { if (THIS_PF[0]) { free_pike_frame(THIS_PF[0]); THIS_PF[0] = NULL; } } /* mixed `[](int index) */ static void f_pf__index(INT32 args) { INT_TYPE index; struct pike_frame *f; get_all_args("pike_frame->`[]", args, "%i", &index); f = THIS_PF[0]; if (!f) { index_error("pike_frame->`[]", Pike_sp-args, args, NULL, Pike_sp-args, "Indexing the empty array with %d.\n", index); } if (index < 0) { index_error("pike_frame->`[]", Pike_sp-args, args, NULL, Pike_sp-args, "Indexing with negative index (%d)\n", index); } if (!(f->current_object && f->context.prog)) { index_error("pike_frame->`[]", Pike_sp-args, args, NULL, Pike_sp-args, "Indexing the NULL value with %d.\n", index); } switch(index) { case 0: /* Filename */ case 1: /* Linenumber */ break; case 2: /* Function */ if (f->current_object->prog) { pop_n_elems(args); ref_push_object(f->current_object); Pike_sp[-1].subtype = f->fun; Pike_sp[-1].type = PIKE_T_FUNCTION; } else { pop_n_elems(args); push_int(0); Pike_sp[-1].subtype = NUMBER_DESTRUCTED; } return; default: /* Arguments */ break; } } /* * Recoveries handling. */ JMP_BUF *recoveries=0;
5267b71995-08-09Fredrik Hübinette (Hubbe) 
c857761999-03-16Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT void check_recovery_context(void)
c857761999-03-16Fredrik Hübinette (Hubbe) { char foo;
fc26f62000-07-06Fredrik Hübinette (Hubbe) #define TESTILITEST ((((char *)Pike_interpreter.recoveries)-((char *)&foo))*STACK_DIRECTION)
15e5e92000-08-15Henrik Grubbström (Grubba)  if(Pike_interpreter.recoveries && TESTILITEST > 0) { fprintf(stderr, "Recoveries is out biking (Pike_interpreter.recoveries=%p, Pike_sp=%p, %d)!\n", Pike_interpreter.recoveries, &foo, TESTILITEST); fprintf(stderr, "Last recovery was added at %s:%d\n", Pike_interpreter.recoveries->file, Pike_interpreter.recoveries->line); fatal("Recoveries is out biking (Pike_interpreter.recoveries=%p, Pike_sp=%p, %d)!\n", Pike_interpreter.recoveries, &foo, TESTILITEST); }
c857761999-03-16Fredrik Hübinette (Hubbe)  /* Add more stuff here when required */ }
20f7a02000-03-20Fredrik Hübinette (Hubbe) 
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT void pike_gdb_breakpoint(void)
20f7a02000-03-20Fredrik Hübinette (Hubbe) { }
c857761999-03-16Fredrik Hübinette (Hubbe) #endif
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT JMP_BUF *init_recovery(JMP_BUF *r DEBUG_LINE_ARGS)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
c857761999-03-16Fredrik Hübinette (Hubbe)  check_recovery_context();
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
5196c51998-07-16Fredrik Hübinette (Hubbe)  r->line=line; r->file=file;
c857761999-03-16Fredrik Hübinette (Hubbe)  OED_FPRINTF((stderr, "init_recovery(%p) %s:%d\n", r, file, line));
5196c51998-07-16Fredrik Hübinette (Hubbe) #endif
fc26f62000-07-06Fredrik Hübinette (Hubbe)  r->frame_pointer=Pike_fp; r->stack_pointer=Pike_sp-Pike_interpreter.evaluator_stack; r->mark_sp=Pike_mark_sp - Pike_interpreter.mark_stack; r->previous=Pike_interpreter.recoveries;
07513e1996-10-04Fredrik Hübinette (Hubbe)  r->onerror=0;
61e9a01998-01-25Fredrik Hübinette (Hubbe)  r->severity=THROW_ERROR;
fc26f62000-07-06Fredrik Hübinette (Hubbe)  Pike_interpreter.recoveries=r;
c857761999-03-16Fredrik Hübinette (Hubbe)  check_recovery_context();
e9f9141996-09-29Fredrik Hübinette (Hubbe)  return r;
5267b71995-08-09Fredrik Hübinette (Hubbe) }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT void pike_throw(void) ATTRIBUTE((noreturn))
5267b71995-08-09Fredrik Hübinette (Hubbe) {
fc26f62000-07-06Fredrik Hübinette (Hubbe)  while(Pike_interpreter.recoveries && throw_severity > Pike_interpreter.recoveries->severity)
61e9a01998-01-25Fredrik Hübinette (Hubbe)  {
fc26f62000-07-06Fredrik Hübinette (Hubbe)  while(Pike_interpreter.recoveries->onerror)
61e9a01998-01-25Fredrik Hübinette (Hubbe)  {
fc26f62000-07-06Fredrik Hübinette (Hubbe)  (*Pike_interpreter.recoveries->onerror->func)(Pike_interpreter.recoveries->onerror->arg); Pike_interpreter.recoveries->onerror=Pike_interpreter.recoveries->onerror->previous;
61e9a01998-01-25Fredrik Hübinette (Hubbe)  }
fc26f62000-07-06Fredrik Hübinette (Hubbe)  Pike_interpreter.recoveries=Pike_interpreter.recoveries->previous;
61e9a01998-01-25Fredrik Hübinette (Hubbe)  }
fc26f62000-07-06Fredrik Hübinette (Hubbe)  if(!Pike_interpreter.recoveries)
5267b71995-08-09Fredrik Hübinette (Hubbe)  fatal("No error recovery context.\n");
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
fc26f62000-07-06Fredrik Hübinette (Hubbe)  if(Pike_sp - Pike_interpreter.evaluator_stack < Pike_interpreter.recoveries->stack_pointer)
294dc51997-08-03Fredrik Hübinette (Hubbe)  fatal("Stack error in error.\n");
5267b71995-08-09Fredrik Hübinette (Hubbe) #endif
fc26f62000-07-06Fredrik Hübinette (Hubbe)  while(Pike_fp != Pike_interpreter.recoveries->frame_pointer)
5267b71995-08-09Fredrik Hübinette (Hubbe)  {
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
fc26f62000-07-06Fredrik Hübinette (Hubbe)  if(!Pike_fp)
5267b71995-08-09Fredrik Hübinette (Hubbe)  fatal("Popped out of stack frames.\n"); #endif
424d9c1999-05-02Fredrik Hübinette (Hubbe)  POP_PIKE_FRAME();
5267b71995-08-09Fredrik Hübinette (Hubbe)  }
fc26f62000-07-06Fredrik Hübinette (Hubbe)  pop_n_elems(Pike_sp - Pike_interpreter.evaluator_stack - Pike_interpreter.recoveries->stack_pointer); Pike_mark_sp = Pike_interpreter.mark_stack + Pike_interpreter.recoveries->mark_sp;
5267b71995-08-09Fredrik Hübinette (Hubbe) 
fc26f62000-07-06Fredrik Hübinette (Hubbe)  while(Pike_interpreter.recoveries->onerror)
cb22561995-10-11Fredrik Hübinette (Hubbe)  {
fc26f62000-07-06Fredrik Hübinette (Hubbe)  (*Pike_interpreter.recoveries->onerror->func)(Pike_interpreter.recoveries->onerror->arg); Pike_interpreter.recoveries->onerror=Pike_interpreter.recoveries->onerror->previous;
cb22561995-10-11Fredrik Hübinette (Hubbe)  }
fc26f62000-07-06Fredrik Hübinette (Hubbe)  longjmp(Pike_interpreter.recoveries->recovery,1);
5267b71995-08-09Fredrik Hübinette (Hubbe) }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT void push_error(char *description)
f52cf61998-04-10Henrik Grubbström (Grubba) { push_text(description); f_backtrace(0); f_aggregate(2); }
cd86322000-07-06Fredrik Hübinette (Hubbe) struct svalue throw_value = { PIKE_T_INT };
61e9a01998-01-25Fredrik Hübinette (Hubbe) int throw_severity;
4406cf1998-04-13Henrik Grubbström (Grubba) static const char *in_error;
db40ab1999-04-15Fredrik Hübinette (Hubbe) 
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT void low_error(char *buf) ATTRIBUTE((noreturn))
db40ab1999-04-15Fredrik Hübinette (Hubbe) { push_error(buf); free_svalue(& throw_value);
fc26f62000-07-06Fredrik Hübinette (Hubbe)  throw_value = *--Pike_sp;
cfa2e51999-12-07Henrik Grubbström (Grubba)  throw_severity = THROW_ERROR;
db40ab1999-04-15Fredrik Hübinette (Hubbe)  in_error=0; pike_throw(); /* Hope someone is catching, or we will be out of balls. */ }
f52cf61998-04-10Henrik Grubbström (Grubba) /* FIXME: NOTE: This function uses a static buffer. * Check sizes of arguments passed! */
4406cf1998-04-13Henrik Grubbström (Grubba) void va_error(const char *fmt, va_list args) ATTRIBUTE((noreturn))
5267b71995-08-09Fredrik Hübinette (Hubbe) {
52cf141998-05-15Henrik Grubbström (Grubba)  char buf[4096];
db40ab1999-04-15Fredrik Hübinette (Hubbe)  SWAP_IN_THREAD_IF_REQUIRED();
5267b71995-08-09Fredrik Hübinette (Hubbe)  if(in_error) {
4406cf1998-04-13Henrik Grubbström (Grubba)  const char *tmp=in_error;
5267b71995-08-09Fredrik Hübinette (Hubbe)  in_error=0;
5fe7a71995-11-06Fredrik Hübinette (Hubbe)  fatal("Recursive error() calls, original error: %s",tmp);
5267b71995-08-09Fredrik Hübinette (Hubbe)  }
5fe7a71995-11-06Fredrik Hübinette (Hubbe)  in_error=buf;
5267b71995-08-09Fredrik Hübinette (Hubbe) 
52cf141998-05-15Henrik Grubbström (Grubba) #ifdef HAVE_VSNPRINTF
f00ecc1998-05-16Fredrik Hübinette (Hubbe)  vsnprintf(buf, 4090, fmt, args);
52cf141998-05-15Henrik Grubbström (Grubba) #else /* !HAVE_VSNPRINTF */
5267b71995-08-09Fredrik Hübinette (Hubbe)  VSPRINTF(buf, fmt, args);
52cf141998-05-15Henrik Grubbström (Grubba) #endif /* HAVE_VSNPRINTF */
5267b71995-08-09Fredrik Hübinette (Hubbe) 
fc26f62000-07-06Fredrik Hübinette (Hubbe)  if(!Pike_interpreter.recoveries)
06983f1996-09-22Fredrik Hübinette (Hubbe)  {
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
06983f1996-09-22Fredrik Hübinette (Hubbe)  dump_backlog(); #endif
dceabb1996-10-09Fredrik Hübinette (Hubbe)  fprintf(stderr,"No error recovery context!\n%s",buf);
06983f1996-09-22Fredrik Hübinette (Hubbe)  exit(99); }
6b92161996-05-16Fredrik Hübinette (Hubbe)  if((long)strlen(buf) >= (long)sizeof(buf))
5267b71995-08-09Fredrik Hübinette (Hubbe)  fatal("Buffer overflow in error()\n");
db40ab1999-04-15Fredrik Hübinette (Hubbe)  low_error(buf);
f52cf61998-04-10Henrik Grubbström (Grubba) }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT void new_error(const char *name, const char *text, struct svalue *oldsp,
4406cf1998-04-13Henrik Grubbström (Grubba)  INT32 args, const char *file, int line) ATTRIBUTE((noreturn))
f52cf61998-04-10Henrik Grubbström (Grubba) { int i;
db40ab1999-04-15Fredrik Hübinette (Hubbe)  ASSERT_THREAD_SWAPPED_IN();
f52cf61998-04-10Henrik Grubbström (Grubba)  if(in_error) {
4406cf1998-04-13Henrik Grubbström (Grubba)  const char *tmp=in_error;
f52cf61998-04-10Henrik Grubbström (Grubba)  in_error=0; fatal("Recursive error() calls, original error: %s",tmp); } in_error=text;
fc26f62000-07-06Fredrik Hübinette (Hubbe)  if(!Pike_interpreter.recoveries)
f52cf61998-04-10Henrik Grubbström (Grubba)  {
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
f52cf61998-04-10Henrik Grubbström (Grubba)  dump_backlog(); #endif fprintf(stderr,"No error recovery context!\n%s():%s",name,text);
68581a2000-01-29Fredrik Hübinette (Hubbe)  if(file) fprintf(stderr,"at %s:%d\n",file,line);
f52cf61998-04-10Henrik Grubbström (Grubba)  exit(99); } push_text(text);
5267b71995-08-09Fredrik Hübinette (Hubbe)  f_backtrace(0);
f52cf61998-04-10Henrik Grubbström (Grubba)  if (file) { push_text(file); push_int(line); } else { push_int(0); push_int(0); } push_text(name); for (i=-args; i; i++) { push_svalue(oldsp + i); } f_aggregate(args + 3); f_aggregate(1); f_add(2);
5267b71995-08-09Fredrik Hübinette (Hubbe)  f_aggregate(2);
f52cf61998-04-10Henrik Grubbström (Grubba) 
5267b71995-08-09Fredrik Hübinette (Hubbe)  free_svalue(& throw_value);
fc26f62000-07-06Fredrik Hübinette (Hubbe)  throw_value = *--Pike_sp;
61e9a01998-01-25Fredrik Hübinette (Hubbe)  throw_severity=THROW_ERROR;
5267b71995-08-09Fredrik Hübinette (Hubbe)  in_error=0;
dc7cc91998-01-14Fredrik Hübinette (Hubbe)  pike_throw(); /* Hope someone is catching, or we will be out of balls. */
5267b71995-08-09Fredrik Hübinette (Hubbe) }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT void exit_on_error(void *msg)
dceabb1996-10-09Fredrik Hübinette (Hubbe) {
56275c2000-04-16Fredrik Hübinette (Hubbe)  ONERROR tmp; SET_ONERROR(tmp,fatal_on_error,"Fatal in exit_on_error!");
111fdd2000-04-17Fredrik Hübinette (Hubbe)  d_flag=0;
56275c2000-04-16Fredrik Hübinette (Hubbe) 
05756c1999-08-24Fredrik Hübinette (Hubbe)  fprintf(stderr,"%s\n",(char *)msg);
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
dceabb1996-10-09Fredrik Hübinette (Hubbe)  dump_backlog(); #endif fprintf(stderr,"%s\n",(char *)msg);
05756c1999-08-24Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG { char *s; fprintf(stderr,"Attempting to dump raw error: (may fail)\n"); init_buf(); describe_svalue(&throw_value,0,0); s=simple_free_buf(); fprintf(stderr,"%s\n",s); free(s); } #endif
dceabb1996-10-09Fredrik Hübinette (Hubbe)  exit(1); }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT void fatal_on_error(void *msg)
dceabb1996-10-09Fredrik Hübinette (Hubbe) {
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
dceabb1996-10-09Fredrik Hübinette (Hubbe)  dump_backlog(); #endif fprintf(stderr,"%s\n",(char *)msg); abort(); }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT void error(const char *fmt,...) ATTRIBUTE((noreturn,format (printf, 1, 2)))
5267b71995-08-09Fredrik Hübinette (Hubbe) { va_list args; va_start(args,fmt); va_error(fmt,args); va_end(args); }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT void debug_fatal(const char *fmt, ...) ATTRIBUTE((noreturn,format (printf, 1, 2)))
5267b71995-08-09Fredrik Hübinette (Hubbe) { va_list args; static int in_fatal = 0; va_start(args,fmt); /* Prevent double fatal. */ if (in_fatal) { (void)VFPRINTF(stderr, fmt, args); abort(); }
a3bcd72000-04-16Fredrik Hübinette (Hubbe) 
5267b71995-08-09Fredrik Hübinette (Hubbe)  in_fatal = 1;
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
5267b71995-08-09Fredrik Hübinette (Hubbe)  dump_backlog(); #endif
a3bcd72000-04-16Fredrik Hübinette (Hubbe)  { extern int Pike_in_gc; if(Pike_in_gc) { fprintf(stderr,"Pike was in GC stage %d when this fatal occured:\n",Pike_in_gc); Pike_in_gc=0; }
9abad92000-04-17Martin Stjernholm  }
a3bcd72000-04-16Fredrik Hübinette (Hubbe) 
5267b71995-08-09Fredrik Hübinette (Hubbe)  (void)VFPRINTF(stderr, fmt, args);
ef7c761998-03-24Fredrik Hübinette (Hubbe)  d_flag=t_flag=0;
fc26f62000-07-06Fredrik Hübinette (Hubbe)  if(Pike_sp && Pike_interpreter.evaluator_stack)
111fdd2000-04-17Fredrik Hübinette (Hubbe)  { fprintf(stderr,"Attempting to dump backlog (may fail)...\n"); push_error("Backtrace at time of fatal:\n"); APPLY_MASTER("describe_backtrace",1);
cd86322000-07-06Fredrik Hübinette (Hubbe)  if(Pike_sp[-1].type==PIKE_T_STRING)
fc26f62000-07-06Fredrik Hübinette (Hubbe)  write_to_stderr(Pike_sp[-1].u.string->str, Pike_sp[-1].u.string->len);
111fdd2000-04-17Fredrik Hübinette (Hubbe)  }else{ fprintf(stderr,"No stack - no backtrace.\n"); }
5267b71995-08-09Fredrik Hübinette (Hubbe)  fflush(stderr); abort(); }
c857761999-03-16Fredrik Hübinette (Hubbe) 
81299f1999-03-19Fredrik Hübinette (Hubbe) #if 1
c857761999-03-16Fredrik Hübinette (Hubbe) 
81299f1999-03-19Fredrik Hübinette (Hubbe) #define ERR_DECLARE #include "errors.h" void f_error_cast(INT32 args) { char *s; get_all_args("error->cast",args,"%s",&s); if(!strncmp(s,"array",5)) { pop_n_elems(args); ref_push_string(GENERIC_ERROR_THIS->desc); ref_push_array(GENERIC_ERROR_THIS->backtrace); f_aggregate(2); }else{
53a2051999-10-06Henrik Grubbström (Grubba)  SIMPLE_BAD_ARG_ERROR("error->cast", 1, "the value \"array\"");
81299f1999-03-19Fredrik Hübinette (Hubbe)  } } void f_error_index(INT32 args) {
65a5492000-08-10Per Hedbor  INT_TYPE ind;
81299f1999-03-19Fredrik Hübinette (Hubbe)  get_all_args("error->`[]",args,"%i",&ind); switch(ind) {
53a2051999-10-06Henrik Grubbström (Grubba)  case 0: pop_n_elems(args); ref_push_string(GENERIC_ERROR_THIS->desc); break; case 1: pop_n_elems(args); ref_push_array(GENERIC_ERROR_THIS->backtrace); break;
81299f1999-03-19Fredrik Hübinette (Hubbe)  default:
fc26f62000-07-06Fredrik Hübinette (Hubbe)  index_error("error->`[]", Pike_sp-args, args, NULL, Pike_sp-args,
53a2051999-10-06Henrik Grubbström (Grubba)  "Index %d is out of range 0 - 1.\n", ind); break;
81299f1999-03-19Fredrik Hübinette (Hubbe)  } } void f_error_describe(INT32 args) { pop_n_elems(args);
fc26f62000-07-06Fredrik Hübinette (Hubbe)  ref_push_object(Pike_fp->current_object);
81299f1999-03-19Fredrik Hübinette (Hubbe)  APPLY_MASTER("describe_backtrace",1); } void f_error_backtrace(INT32 args) { pop_n_elems(args); ref_push_array(GENERIC_ERROR_THIS->backtrace); }
cfa2e51999-12-07Henrik Grubbström (Grubba) #ifdef ERROR_DEBUG #define DWERROR(X) fprintf X #else /* !ERROR_DEBUG */ #define DWERROR(X) #endif /* ERROR_DEBUG */
81299f1999-03-19Fredrik Hübinette (Hubbe) #define INIT_ERROR(FEL)\ va_list foo; \
db40ab1999-04-15Fredrik Hübinette (Hubbe)  struct object *o; \ va_start(foo,desc); \ ASSERT_THREAD_SWAPPED_IN(); \
cfa2e51999-12-07Henrik Grubbström (Grubba)  o=low_clone(PIKE_CONCAT(FEL,_error_program)); \ DWERROR((stderr, "%s(): Throwing a " #FEL " error\n", func))
81299f1999-03-19Fredrik Hübinette (Hubbe)  #define ERROR_DONE(FOO) \ PIKE_CONCAT(FOO,_error_va(o,func, \ base_sp, args, \ desc,foo)); \ va_end(foo) #define ERROR_STRUCT(STRUCT,O) \ ((struct PIKE_CONCAT(STRUCT,_error_struct) *)((O)->storage + PIKE_CONCAT(STRUCT,_error_offset))) #define ERROR_COPY(STRUCT,X) \ ERROR_STRUCT(STRUCT,o)->X=X
b4185e2000-04-04Henrik Grubbström (Grubba) #define ERROR_COPY_SVALUE(STRUCT,X) do { \ if (X) { \ assign_svalue_no_free( & ERROR_STRUCT(STRUCT,o)->X, X); \ } else { \
cd86322000-07-06Fredrik Hübinette (Hubbe)  ERROR_STRUCT(STRUCT, o)->X.type = PIKE_T_INT; \
b4185e2000-04-04Henrik Grubbström (Grubba)  ERROR_STRUCT(STRUCT, o)->X.subtype = 0; \ ERROR_STRUCT(STRUCT, o)->X.u.integer = 0; \ } \ } while (0)
81299f1999-03-19Fredrik Hübinette (Hubbe)  #define ERROR_COPY_REF(STRUCT,X) \ add_ref( ERROR_STRUCT(STRUCT,o)->X=X ) void generic_error_va(struct object *o, char *func, struct svalue *base_sp, int args, char *fmt, va_list foo) ATTRIBUTE((noreturn))
c857761999-03-16Fredrik Hübinette (Hubbe) { char buf[8192];
81299f1999-03-19Fredrik Hübinette (Hubbe)  struct pike_string *desc; struct array *backtrace; int i;
c857761999-03-16Fredrik Hübinette (Hubbe)  #ifdef HAVE_VSNPRINTF
81299f1999-03-19Fredrik Hübinette (Hubbe)  vsnprintf(buf, sizeof(buf)-1, fmt, foo);
c857761999-03-16Fredrik Hübinette (Hubbe) #else /* !HAVE_VSNPRINTF */
53a2051999-10-06Henrik Grubbström (Grubba)  /* Sentinel that will be overwritten on buffer overflow. */ buf[sizeof(buf)-1] = '\0';
81299f1999-03-19Fredrik Hübinette (Hubbe)  VSPRINTF(buf, fmt, foo);
c857761999-03-16Fredrik Hübinette (Hubbe) 
53a2051999-10-06Henrik Grubbström (Grubba)  if(buf[sizeof(buf)-1])
c857761999-03-16Fredrik Hübinette (Hubbe)  fatal("Buffer overflow in error()\n"); #endif /* HAVE_VSNPRINTF */
81299f1999-03-19Fredrik Hübinette (Hubbe)  in_error=buf;
c857761999-03-16Fredrik Hübinette (Hubbe) 
17fd852000-08-15Henrik Grubbström (Grubba)  if (!master_program) {
15e5e92000-08-15Henrik Grubbström (Grubba)  fprintf(stderr, "ERROR: %s\n", buf); }
81299f1999-03-19Fredrik Hübinette (Hubbe)  ERROR_STRUCT(generic,o)->desc=make_shared_string(buf); f_backtrace(0); if(func) { push_int(0); push_int(0); push_text(func);
e88b9e1999-04-02Fredrik Hübinette (Hubbe)  for (i=0;i<args;i++)
81299f1999-03-19Fredrik Hübinette (Hubbe)  push_svalue(base_sp + i); f_aggregate(args + 3); f_aggregate(1); f_add(2); }
c857761999-03-16Fredrik Hübinette (Hubbe) 
cd86322000-07-06Fredrik Hübinette (Hubbe)  if(Pike_sp[-1].type!=PIKE_T_ARRAY)
81299f1999-03-19Fredrik Hübinette (Hubbe)  fatal("Error failed to generate a backtrace!\n");
fc26f62000-07-06Fredrik Hübinette (Hubbe)  ERROR_STRUCT(generic,o)->backtrace=Pike_sp[-1].u.array; Pike_sp--; dmalloc_touch_svalue(Pike_sp);
81299f1999-03-19Fredrik Hübinette (Hubbe)  free_svalue(& throw_value);
cd86322000-07-06Fredrik Hübinette (Hubbe)  throw_value.type=PIKE_T_OBJECT;
81299f1999-03-19Fredrik Hübinette (Hubbe)  throw_value.u.object=o;
a373381999-07-27Henrik Grubbström (Grubba)  throw_severity = THROW_ERROR;
c857761999-03-16Fredrik Hübinette (Hubbe)  in_error=0; pike_throw(); /* Hope someone is catching, or we will be out of balls. */ }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT void generic_error(
81299f1999-03-19Fredrik Hübinette (Hubbe)  char *func, struct svalue *base_sp, int args, char *desc, ...) ATTRIBUTE((noreturn,format (printf, 4, 5)))
c857761999-03-16Fredrik Hübinette (Hubbe) {
81299f1999-03-19Fredrik Hübinette (Hubbe)  INIT_ERROR(generic); ERROR_DONE(generic);
c857761999-03-16Fredrik Hübinette (Hubbe) }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT void index_error(
81299f1999-03-19Fredrik Hübinette (Hubbe)  char *func, struct svalue *base_sp, int args, struct svalue *val, struct svalue *ind, char *desc, ...) ATTRIBUTE((noreturn,format (printf, 6, 7))) { INIT_ERROR(index); ERROR_COPY_SVALUE(index, val); ERROR_COPY_SVALUE(index, ind); ERROR_DONE(generic); }
c857761999-03-16Fredrik Hübinette (Hubbe) 
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT void bad_arg_error(
81299f1999-03-19Fredrik Hübinette (Hubbe)  char *func, struct svalue *base_sp, int args, int which_arg, char *expected_type, struct svalue *got, char *desc, ...) ATTRIBUTE((noreturn,format (printf, 7, 8)))
c857761999-03-16Fredrik Hübinette (Hubbe) {
81299f1999-03-19Fredrik Hübinette (Hubbe)  INIT_ERROR(bad_arg); ERROR_COPY(bad_arg, which_arg); ERROR_STRUCT(bad_arg,o)->expected_type=make_shared_string(expected_type); if(got) { ERROR_COPY_SVALUE(bad_arg, got); }else{
cd86322000-07-06Fredrik Hübinette (Hubbe)  ERROR_STRUCT(bad_arg,o)->got.type=PIKE_T_INT;
81299f1999-03-19Fredrik Hübinette (Hubbe)  ERROR_STRUCT(bad_arg,o)->got.subtype=NUMBER_UNDEFINED; ERROR_STRUCT(bad_arg,o)->got.u.integer=0; }
cfa2e51999-12-07Henrik Grubbström (Grubba)  DWERROR((stderr, "%s():Bad arg %d (expected %s)\n", func, which_arg, expected_type));
81299f1999-03-19Fredrik Hübinette (Hubbe)  ERROR_DONE(generic); }
c857761999-03-16Fredrik Hübinette (Hubbe) 
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT void math_error(
d0ed261999-03-20Fredrik Hübinette (Hubbe)  char *func, struct svalue *base_sp, int args,
28af0c1999-03-23Fredrik Hübinette (Hubbe)  struct svalue *number,
b9f8361999-03-23Fredrik Hübinette (Hubbe)  char *desc, ...) ATTRIBUTE((noreturn,format (printf, 5, 6)))
d0ed261999-03-20Fredrik Hübinette (Hubbe) { INIT_ERROR(math);
28af0c1999-03-23Fredrik Hübinette (Hubbe)  if(number) {
c023d51999-03-23Fredrik Hübinette (Hubbe)  ERROR_COPY_SVALUE(math, number);
28af0c1999-03-23Fredrik Hübinette (Hubbe)  }else{
cd86322000-07-06Fredrik Hübinette (Hubbe)  ERROR_STRUCT(math,o)->number.type=PIKE_T_INT;
c023d51999-03-23Fredrik Hübinette (Hubbe)  ERROR_STRUCT(math,o)->number.subtype=NUMBER_UNDEFINED; ERROR_STRUCT(math,o)->number.u.integer=0;
28af0c1999-03-23Fredrik Hübinette (Hubbe)  }
d0ed261999-03-20Fredrik Hübinette (Hubbe)  ERROR_DONE(generic); }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT void resource_error(
d0ed261999-03-20Fredrik Hübinette (Hubbe)  char *func, struct svalue *base_sp, int args, char *resource_type,
15e5e92000-08-15Henrik Grubbström (Grubba)  size_t howmuch,
d0ed261999-03-20Fredrik Hübinette (Hubbe)  char *desc, ...) ATTRIBUTE((noreturn,format (printf, 6, 7))) { INIT_ERROR(resource); ERROR_COPY(resource,howmuch); ERROR_STRUCT(resource,o)->resource_type=make_shared_string(resource_type); ERROR_DONE(generic); }
c857761999-03-16Fredrik Hübinette (Hubbe) 
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT void permission_error(
64dc4c1999-04-17Henrik Grubbström (Grubba)  char *func, struct svalue *base_sp, int args, char *permission_type,
424d9c1999-05-02Fredrik Hübinette (Hubbe)  char *desc, ...) ATTRIBUTE((noreturn, format(printf, 5, 6)))
64dc4c1999-04-17Henrik Grubbström (Grubba) { INIT_ERROR(permission); ERROR_STRUCT(permission,o)->permission_type= make_shared_string(permission_type); ERROR_DONE(generic); }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT void wrong_number_of_args_error(char *name, int args, int expected)
3a5b1d2000-05-24Fredrik Hübinette (Hubbe) { char *msg; if(expected>args) { msg="Too few arguments"; }else{ msg="Too many arguments"; }
fc26f62000-07-06Fredrik Hübinette (Hubbe)  new_error(name, msg, Pike_sp-args, args, 0,0);
3a5b1d2000-05-24Fredrik Hübinette (Hubbe) }
20513c2000-04-12Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG static void gc_check_throw_value(struct callback *foo, void *bar, void *gazonk) { debug_gc_xmark_svalues(&throw_value,1," in the throw value"); } #endif
81299f1999-03-19Fredrik Hübinette (Hubbe) void init_error(void) { #define ERR_SETUP #include "errors.h"
20513c2000-04-12Fredrik Hübinette (Hubbe)  #ifdef PIKE_DEBUG dmalloc_accept_leak(add_gc_callback(gc_check_throw_value,0,0)); #endif
81299f1999-03-19Fredrik Hübinette (Hubbe) }
c857761999-03-16Fredrik Hübinette (Hubbe) 
81299f1999-03-19Fredrik Hübinette (Hubbe) void cleanup_error(void) { #define ERR_CLEANUP #include "errors.h"
c857761999-03-16Fredrik Hübinette (Hubbe) } #endif