2001-04-09 20:54:03 +01:00
|
|
|
/*************************************************************************
|
2015-09-25 10:57:26 +01:00
|
|
|
* *
|
|
|
|
* Yap Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog Was Developed At Nccup - Universidade Do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa And Universidade Do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
2015-10-18 11:39:25 +01:00
|
|
|
* File: errors.c *
|
2015-09-25 10:57:26 +01:00
|
|
|
* Last Rev: *
|
2015-10-18 11:39:25 +01:00
|
|
|
* Mods: *
|
2015-09-25 10:57:26 +01:00
|
|
|
* Comments: Yap'S error handlers *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
#include "absmi.h"
|
|
|
|
#include "yapio.h"
|
|
|
|
#if HAVE_STDARG_H
|
|
|
|
#include <stdarg.h>
|
|
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
2016-02-20 23:58:06 +00:00
|
|
|
#if HAVE_EXECINFO_H
|
|
|
|
#include <execinfo.h>
|
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
#include "Foreign.h"
|
|
|
|
|
2014-09-16 10:00:31 +01:00
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
bool Yap_Warning(const char *s, ...) {
|
2015-10-05 10:29:07 +01:00
|
|
|
CACHE_REGS
|
2016-05-14 11:34:27 +01:00
|
|
|
va_list ap;
|
2015-10-05 10:29:07 +01:00
|
|
|
PredEntry *pred;
|
|
|
|
bool rc;
|
|
|
|
Term ts[2];
|
|
|
|
const char *format;
|
|
|
|
char tmpbuf[MAXPATHLEN];
|
2015-10-18 11:39:25 +01:00
|
|
|
|
2015-10-05 10:29:07 +01:00
|
|
|
if (LOCAL_within_print_message) {
|
|
|
|
/* error within error */
|
|
|
|
fprintf(stderr, "%% WARNING WITHIN WARNING\n");
|
|
|
|
Yap_RestartYap(1);
|
|
|
|
}
|
|
|
|
LOCAL_DoingUndefp = true;
|
|
|
|
LOCAL_within_print_message = true;
|
2015-10-18 11:39:25 +01:00
|
|
|
pred = RepPredProp(PredPropByFunc(FunctorPrintMessage,
|
|
|
|
PROLOG_MODULE)); // PROCEDURE_print_message2
|
2015-10-05 10:29:07 +01:00
|
|
|
va_start(ap, s);
|
|
|
|
format = va_arg(ap, char *);
|
|
|
|
if (format != NULL) {
|
|
|
|
#if HAVE_VSNPRINTF
|
2015-10-18 11:39:25 +01:00
|
|
|
vsnprintf(tmpbuf, MAXPATHLEN - 1, format, ap);
|
2015-10-05 10:29:07 +01:00
|
|
|
#else
|
|
|
|
(void)vsprintf(tmpbuf, format, ap);
|
|
|
|
#endif
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
va_end(ap);
|
2016-01-31 10:16:02 +00:00
|
|
|
if (pred->OpcodeOfPred == UNDEF_OPCODE||
|
|
|
|
pred->OpcodeOfPred == FAIL_OPCODE) {
|
2015-12-15 09:18:36 +00:00
|
|
|
fprintf(stderr, "warning message: %s\n", tmpbuf);
|
|
|
|
LOCAL_DoingUndefp = false;
|
|
|
|
LOCAL_within_print_message = false;
|
2016-01-31 10:16:02 +00:00
|
|
|
return false;
|
2015-12-15 09:18:36 +00:00
|
|
|
}
|
|
|
|
|
2015-11-05 15:21:21 +00:00
|
|
|
ts[1] = MkAtomTerm(AtomWarning);
|
|
|
|
ts[0] = MkAtomTerm(Yap_LookupAtom(tmpbuf));
|
2015-10-05 10:29:07 +01:00
|
|
|
rc = Yap_execute_pred(pred, ts, true PASS_REGS);
|
|
|
|
return rc;
|
2015-06-19 01:30:13 +01:00
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
bool Yap_PrintWarning(Term twarning) {
|
2015-07-06 11:51:24 +01:00
|
|
|
CACHE_REGS
|
2016-05-14 11:34:27 +01:00
|
|
|
PredEntry *pred = RepPredProp(PredPropByFunc(
|
|
|
|
FunctorPrintMessage, PROLOG_MODULE)); // PROCEDURE_print_message2;
|
2016-02-11 14:00:56 +00:00
|
|
|
Term cmod = ( CurrentModule == PROLOG_MODULE ? TermProlog : CurrentModule );
|
2015-07-06 11:51:24 +01:00
|
|
|
bool rc;
|
|
|
|
Term ts[2];
|
2015-10-18 11:39:25 +01:00
|
|
|
|
2015-07-23 01:04:22 +01:00
|
|
|
if (LOCAL_within_print_message) {
|
|
|
|
/* error within error */
|
2015-09-25 10:57:26 +01:00
|
|
|
fprintf(stderr, "%% WARNING WITHIN WARNING\n");
|
|
|
|
Yap_RestartYap(1);
|
2015-07-23 01:04:22 +01:00
|
|
|
}
|
|
|
|
LOCAL_DoingUndefp = true;
|
2015-07-06 11:51:24 +01:00
|
|
|
LOCAL_within_print_message = true;
|
2016-01-31 10:16:02 +00:00
|
|
|
if (pred->OpcodeOfPred == UNDEF_OPCODE ||
|
|
|
|
pred->OpcodeOfPred == FAIL_OPCODE
|
2016-05-14 11:34:27 +01:00
|
|
|
) {
|
|
|
|
fprintf(stderr, "warning message:\n");
|
|
|
|
Yap_DebugPlWrite(twarning);
|
|
|
|
fprintf(stderr, "\n");
|
2015-07-23 01:04:22 +01:00
|
|
|
LOCAL_DoingUndefp = false;
|
2015-07-06 11:51:24 +01:00
|
|
|
LOCAL_within_print_message = false;
|
2016-01-31 10:16:02 +00:00
|
|
|
CurrentModule = cmod;
|
|
|
|
return false;
|
2015-07-06 11:51:24 +01:00
|
|
|
}
|
|
|
|
ts[1] = twarning;
|
2015-11-05 15:21:21 +00:00
|
|
|
ts[0] = MkAtomTerm(AtomWarning);
|
2016-02-11 14:00:56 +00:00
|
|
|
HB = B->cp_h = HR;
|
|
|
|
B->cp_tr = TR;
|
2015-09-25 10:57:26 +01:00
|
|
|
rc = Yap_execute_pred(pred, ts, true PASS_REGS);
|
2015-07-06 11:51:24 +01:00
|
|
|
LOCAL_within_print_message = false;
|
2015-07-23 01:04:22 +01:00
|
|
|
LOCAL_DoingUndefp = false;
|
2015-07-06 11:51:24 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2016-01-31 11:54:45 +00:00
|
|
|
bool Yap_HandleError__(const char *file, const char *function, int lineno, const char *s, ...) {
|
2013-12-02 14:49:41 +00:00
|
|
|
CACHE_REGS
|
2016-05-14 11:34:27 +01:00
|
|
|
yap_error_number err = LOCAL_Error_TYPE;
|
2015-01-26 04:02:46 +00:00
|
|
|
const char *serr;
|
2015-10-18 11:39:25 +01:00
|
|
|
|
2013-12-02 14:49:41 +00:00
|
|
|
LOCAL_Error_TYPE = YAP_NO_ERROR;
|
|
|
|
if (LOCAL_ErrorMessage) {
|
|
|
|
serr = LOCAL_ErrorMessage;
|
|
|
|
} else {
|
2015-01-26 04:02:46 +00:00
|
|
|
serr = s;
|
2013-12-02 14:49:41 +00:00
|
|
|
}
|
|
|
|
switch (err) {
|
2015-10-18 11:39:25 +01:00
|
|
|
case RESOURCE_ERROR_STACK:
|
|
|
|
if (!Yap_gc(2, ENV, gc_P(P, CP))) {
|
2016-01-31 11:54:45 +00:00
|
|
|
Yap_Error__(file, function, lineno, RESOURCE_ERROR_STACK, ARG1, serr);
|
|
|
|
return false;
|
2015-10-18 11:39:25 +01:00
|
|
|
}
|
2016-01-31 11:54:45 +00:00
|
|
|
return true;
|
2015-10-18 11:39:25 +01:00
|
|
|
case RESOURCE_ERROR_AUXILIARY_STACK:
|
|
|
|
if (LOCAL_MAX_SIZE < (char *)AuxSp - AuxBase) {
|
|
|
|
LOCAL_MAX_SIZE += 1024;
|
|
|
|
}
|
|
|
|
if (!Yap_ExpandPreAllocCodeSpace(0, NULL, TRUE)) {
|
|
|
|
/* crash in flames */
|
2016-01-31 11:54:45 +00:00
|
|
|
Yap_Error__(file, function, lineno, RESOURCE_ERROR_AUXILIARY_STACK, ARG1, serr);
|
|
|
|
return false;
|
2015-10-18 11:39:25 +01:00
|
|
|
}
|
2016-01-31 11:54:45 +00:00
|
|
|
return true;
|
2015-10-18 11:39:25 +01:00
|
|
|
case RESOURCE_ERROR_HEAP:
|
|
|
|
if (!Yap_growheap(FALSE, 0, NULL)) {
|
2016-01-31 11:54:45 +00:00
|
|
|
Yap_Error__(file, function, lineno, RESOURCE_ERROR_HEAP, ARG2, serr);
|
|
|
|
return false;
|
2015-10-18 11:39:25 +01:00
|
|
|
}
|
|
|
|
default:
|
2016-01-31 11:54:45 +00:00
|
|
|
Yap_Error__(file, function, lineno, err, LOCAL_Error_Term, serr);
|
|
|
|
return false;
|
2013-12-02 14:49:41 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-25 10:57:26 +01:00
|
|
|
|
|
|
|
int Yap_SWIHandleError(const char *s, ...) {
|
2013-12-02 14:49:41 +00:00
|
|
|
CACHE_REGS
|
2016-05-14 11:34:27 +01:00
|
|
|
yap_error_number err = LOCAL_Error_TYPE;
|
2013-12-02 14:49:41 +00:00
|
|
|
char *serr;
|
2015-10-18 11:39:25 +01:00
|
|
|
|
2013-12-02 14:49:41 +00:00
|
|
|
LOCAL_Error_TYPE = YAP_NO_ERROR;
|
|
|
|
if (LOCAL_ErrorMessage) {
|
|
|
|
serr = LOCAL_ErrorMessage;
|
|
|
|
} else {
|
|
|
|
serr = (char *)s;
|
|
|
|
}
|
|
|
|
switch (err) {
|
2015-10-18 11:39:25 +01:00
|
|
|
case RESOURCE_ERROR_STACK:
|
|
|
|
if (!Yap_gc(2, ENV, gc_P(P, CP))) {
|
|
|
|
Yap_Error(RESOURCE_ERROR_STACK, TermNil, serr);
|
2015-09-25 10:57:26 +01:00
|
|
|
return (FALSE);
|
2015-10-18 11:39:25 +01:00
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
case RESOURCE_ERROR_AUXILIARY_STACK:
|
|
|
|
if (LOCAL_MAX_SIZE < (char *)AuxSp - AuxBase) {
|
|
|
|
LOCAL_MAX_SIZE += 1024;
|
|
|
|
}
|
|
|
|
if (!Yap_ExpandPreAllocCodeSpace(0, NULL, TRUE)) {
|
|
|
|
/* crash in flames */
|
|
|
|
Yap_Error(RESOURCE_ERROR_AUXILIARY_STACK, ARG1, serr);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
case RESOURCE_ERROR_HEAP:
|
|
|
|
if (!Yap_growheap(FALSE, 0, NULL)) {
|
|
|
|
Yap_Error(RESOURCE_ERROR_HEAP, ARG2, serr);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
Yap_Error(err, LOCAL_Error_Term, serr);
|
|
|
|
return (FALSE);
|
2013-12-02 14:49:41 +00:00
|
|
|
}
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
void Yap_RestartYap(int flag) {
|
2012-02-07 15:18:43 +00:00
|
|
|
CACHE_REGS
|
2012-02-05 12:04:29 +00:00
|
|
|
#if PUSH_REGS
|
2016-05-14 11:34:27 +01:00
|
|
|
restore_absmi_regs(&Yap_standard_regs);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2015-09-25 10:57:26 +01:00
|
|
|
siglongjmp(LOCAL_RestartEnv, 1);
|
2009-06-12 20:04:32 +01:00
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
static void error_exit_yap(int value) {
|
2014-09-02 20:19:23 +01:00
|
|
|
CACHE_REGS
|
2016-05-14 11:34:27 +01:00
|
|
|
if (!(LOCAL_PrologMode & BootMode)) {
|
2015-10-18 11:39:25 +01:00
|
|
|
|
2005-02-08 04:05:39 +00:00
|
|
|
#if DEBUG
|
|
|
|
#endif
|
2016-05-14 11:34:27 +01:00
|
|
|
}
|
2005-02-08 18:05:21 +00:00
|
|
|
fprintf(stderr, "\n Exiting ....\n");
|
2016-03-29 01:55:12 +01:00
|
|
|
#if HAVE_BACKTRACE
|
2016-05-14 11:34:27 +01:00
|
|
|
void *callstack[256];
|
|
|
|
int i;
|
|
|
|
int frames = backtrace(callstack, 256);
|
|
|
|
char** strs = backtrace_symbols(callstack, frames);
|
|
|
|
fprintf(stderr, "Execution stack:\n");
|
|
|
|
for (i = 0; i < frames; ++i) {
|
|
|
|
fprintf(stderr, " %s\n", strs[i]);
|
2016-03-29 01:55:12 +01:00
|
|
|
|
2016-05-14 11:34:27 +01:00
|
|
|
}
|
|
|
|
free(strs);
|
2016-03-29 01:55:12 +01:00
|
|
|
#endif
|
2016-05-14 11:34:27 +01:00
|
|
|
Yap_exit(value);
|
2002-09-09 20:52:38 +01:00
|
|
|
}
|
|
|
|
|
2002-02-22 06:12:18 +00:00
|
|
|
/* This needs to be a static because I can't trust the stack (WIN32), and
|
2016-05-14 11:34:27 +01:00
|
|
|
I can't trust the Yap stacks (error) */
|
2002-02-22 06:12:18 +00:00
|
|
|
#define YAP_BUF_SIZE 512
|
|
|
|
|
|
|
|
static char tmpbuf[YAP_BUF_SIZE];
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
// error classes: based on OSI errors.
|
|
|
|
//
|
|
|
|
// - The extra argument says whether there different instances
|
|
|
|
//
|
|
|
|
// - Events are treated within the same pipeline as errors.
|
|
|
|
//
|
|
|
|
|
|
|
|
#undef BEGIN_ERROR_CLASSES
|
|
|
|
#undef ECLASS
|
|
|
|
#undef END_ERROR_CLASSES
|
|
|
|
#undef BEGIN_ERRORS
|
|
|
|
#undef E0
|
|
|
|
#undef E
|
|
|
|
#undef E2
|
|
|
|
#undef END_ERRORS
|
|
|
|
|
2016-05-14 11:34:27 +01:00
|
|
|
#define BEGIN_ERROR_CLASSES() \
|
|
|
|
static Term mkerrorct(yap_error_class_number c, Term *ts) { \
|
2015-10-18 11:39:25 +01:00
|
|
|
switch (c) {
|
2015-09-25 10:57:26 +01:00
|
|
|
|
2016-05-14 11:34:27 +01:00
|
|
|
#define ECLASS(CL, A, B) \
|
|
|
|
case CL: \
|
|
|
|
if (A == 0) \
|
|
|
|
return MkAtomTerm(Yap_LookupAtom(A)); \
|
|
|
|
else { \
|
|
|
|
return Yap_MkApplTerm(Yap_MkFunctor(Yap_LookupAtom(A), B), B, ts); \
|
|
|
|
}
|
2011-10-03 01:01:14 +01:00
|
|
|
|
2016-05-14 11:34:27 +01:00
|
|
|
#define END_ERROR_CLASSES() \
|
|
|
|
} \
|
2015-10-18 11:39:25 +01:00
|
|
|
}
|
2011-10-03 01:01:14 +01:00
|
|
|
|
2016-05-14 11:34:27 +01:00
|
|
|
#define BEGIN_ERRORS() \
|
|
|
|
static Term mkerrort(yap_error_number e, Term *ts) { \
|
2015-10-18 11:39:25 +01:00
|
|
|
switch (e) {
|
|
|
|
|
2016-05-14 11:34:27 +01:00
|
|
|
#define E0(A, B) \
|
|
|
|
case A: \
|
|
|
|
return mkerrorct(B, ts);
|
|
|
|
|
|
|
|
#define E(A, B, C) \
|
|
|
|
case A: \
|
|
|
|
ts -= 1; \
|
|
|
|
ts[0] = MkAtomTerm(Yap_LookupAtom(C)); \
|
|
|
|
return mkerrorct(B, ts);
|
|
|
|
|
|
|
|
#define E2(A, B, C, D) \
|
|
|
|
case A: \
|
|
|
|
ts -= 2; \
|
|
|
|
ts[0] = MkAtomTerm(Yap_LookupAtom(C)); \
|
|
|
|
ts[1] = MkAtomTerm(Yap_LookupAtom(D)); \
|
|
|
|
return mkerrorct(B, ts);
|
|
|
|
|
|
|
|
#define END_ERRORS() \
|
|
|
|
} \
|
|
|
|
}
|
2015-07-06 11:51:24 +01:00
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
#include "YapErrors.h"
|
|
|
|
|
|
|
|
/**
|
2016-05-14 11:34:27 +01:00
|
|
|
* @brief Yap_Error
|
2015-09-25 10:57:26 +01:00
|
|
|
* This function handles errors in the C code. Check errors.yap for the
|
|
|
|
*corresponding Prolog code.
|
|
|
|
*
|
|
|
|
* @param file C source
|
|
|
|
* @param function C function
|
|
|
|
* @param lineno C exact line
|
|
|
|
* @param type the error ID (in YAP this is a single integer)
|
|
|
|
* @param where the culprit
|
|
|
|
* @return usually FAILCODE
|
|
|
|
*
|
|
|
|
* In a good day, the error handler's job is to generate a throw. This includes:
|
|
|
|
* - constructing an ISO style error term;
|
|
|
|
* - constructing a list with all available info on the bug
|
|
|
|
* - generating the throw
|
|
|
|
* - forcing backtracking in order to restart.
|
|
|
|
*
|
|
|
|
* In a bad day, it has to deal with OOM, abort, and errors within errorts.
|
|
|
|
*
|
|
|
|
* The list includes the following options:
|
|
|
|
* + c=c(file, line, function): where the bug was detected;
|
|
|
|
*
|
2015-09-29 23:09:12 +01:00
|
|
|
* + e=p(mod, name, arity, cl, file, lin): where the code was entered;
|
|
|
|
*
|
2015-10-18 11:39:25 +01:00
|
|
|
* + p=p(mod, name, arity, cl, file, line): the prolog procedure that caused
|
|
|
|
*the bug,
|
2015-09-25 10:57:26 +01:00
|
|
|
*and optionally,
|
|
|
|
*
|
|
|
|
* + g=g(Goal): the goal that created this mess
|
|
|
|
*
|
|
|
|
* + i=i(Comment): an user-written comment on this bug.
|
|
|
|
*/
|
|
|
|
yamop *Yap_Error__(const char *file, const char *function, int lineno,
|
|
|
|
yap_error_number type, Term where, ...) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2016-05-14 11:34:27 +01:00
|
|
|
va_list ap;
|
2002-09-09 18:40:12 +01:00
|
|
|
CELL nt[3];
|
2001-04-09 20:54:03 +01:00
|
|
|
Functor fun;
|
2016-08-02 01:15:31 +01:00
|
|
|
Term error_t;
|
|
|
|
Term comment;
|
2015-10-05 10:29:07 +01:00
|
|
|
char *format;
|
|
|
|
char s[MAXPATHLEN];
|
2015-10-18 11:39:25 +01:00
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
/* disallow recursive error handling */
|
|
|
|
if (LOCAL_PrologMode & InErrorMode) {
|
|
|
|
fprintf(stderr, "%% ERROR WITHIN ERROR %d: %s\n", LOCAL_CurrentError,
|
|
|
|
tmpbuf);
|
|
|
|
Yap_RestartYap(1);
|
|
|
|
}
|
|
|
|
LOCAL_PrologMode |= InErrorMode;
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_Error_TYPE = YAP_NO_ERROR;
|
2014-10-16 10:49:11 +01:00
|
|
|
Yap_ClearExs();
|
2015-10-08 02:12:56 +01:00
|
|
|
if (where == 0L) {
|
2008-01-24 00:11:59 +00:00
|
|
|
where = TermNil;
|
2015-10-08 02:12:56 +01:00
|
|
|
}
|
2015-09-25 10:57:26 +01:00
|
|
|
// first, obtain current location
|
2016-08-01 23:50:31 +01:00
|
|
|
// sprintf(LOCAL_FileNameBuf, "%s:%d in C-function %s ", file, lineno, function);
|
|
|
|
// tf = MkAtomTerm(Yap_LookupAtom(LOCAL_FileNameBuf));
|
2005-04-20 21:06:26 +01:00
|
|
|
#if DEBUG_STRICT
|
2015-09-25 10:57:26 +01:00
|
|
|
if (Yap_heap_regs && !(LOCAL_PrologMode & BootMode))
|
|
|
|
fprintf(stderr, "***** Processing Error %d (%lx,%x) %s***\n", type,
|
|
|
|
(unsigned long int)LOCAL_Signals, LOCAL_PrologMode, format);
|
2005-03-02 18:35:49 +00:00
|
|
|
else
|
2015-09-25 10:57:26 +01:00
|
|
|
fprintf(stderr, "***** Processing Error %d (%x) %s***\n", type,
|
|
|
|
LOCAL_PrologMode, format);
|
2005-02-24 21:46:40 +00:00
|
|
|
#endif
|
2015-09-25 10:57:26 +01:00
|
|
|
if (type == INTERRUPT_EVENT) {
|
|
|
|
fprintf(stderr, "%% YAP exiting: cannot handle signal %d\n",
|
|
|
|
(int)IntOfTerm(where));
|
2015-10-08 02:12:56 +01:00
|
|
|
LOCAL_PrologMode &= ~InErrorMode;
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_exit(1);
|
2002-10-23 21:55:37 +01:00
|
|
|
}
|
2015-07-23 01:04:22 +01:00
|
|
|
if (LOCAL_within_print_message) {
|
|
|
|
/* error within error */
|
2015-09-25 10:57:26 +01:00
|
|
|
fprintf(stderr, "%% ERROR WITHIN WARNING %d: %s\n", LOCAL_CurrentError,
|
|
|
|
tmpbuf);
|
2015-10-08 02:12:56 +01:00
|
|
|
LOCAL_PrologMode &= ~InErrorMode;
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_exit(1);
|
2002-02-01 15:48:17 +00:00
|
|
|
}
|
2015-10-18 11:39:25 +01:00
|
|
|
va_start(ap, where);
|
|
|
|
format = va_arg(ap, char *);
|
|
|
|
if (format != NULL) {
|
2015-10-05 10:29:07 +01:00
|
|
|
#if HAVE_VSNPRINTF
|
2015-10-18 11:39:25 +01:00
|
|
|
(void)vsnprintf(s, MAXPATHLEN - 1, format, ap);
|
2015-10-05 10:29:07 +01:00
|
|
|
#else
|
2015-10-18 11:39:25 +01:00
|
|
|
(void)vsprintf(s, format, ap);
|
2015-10-05 10:29:07 +01:00
|
|
|
#endif
|
2015-10-18 11:39:25 +01:00
|
|
|
// fprintf(stderr, "warning: ");
|
|
|
|
comment = MkAtomTerm(Yap_LookupAtom(s));
|
|
|
|
} else if (LOCAL_ErrorSay && LOCAL_ErrorSay[0]) {
|
|
|
|
comment = MkAtomTerm(Yap_LookupAtom(LOCAL_ErrorSay));
|
|
|
|
} else {
|
|
|
|
comment = TermNil;
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
if (P == (yamop *)(FAILCODE)) {
|
|
|
|
LOCAL_PrologMode &= ~InErrorMode;
|
|
|
|
return P;
|
|
|
|
}
|
2015-09-25 10:57:26 +01:00
|
|
|
/* PURE_ABORT may not have set where correctly, BootMode may not have the data
|
|
|
|
* terms ready */
|
|
|
|
if (type == ABORT_EVENT || LOCAL_PrologMode & BootMode) {
|
2002-01-17 22:12:09 +00:00
|
|
|
where = TermNil;
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode &= ~AbortMode;
|
2013-01-29 10:50:04 +00:00
|
|
|
LOCAL_CurrentError = type;
|
2016-07-31 05:19:56 +01:00
|
|
|
LOCAL_PrologMode &= ~InErrorMode;
|
2010-12-04 19:05:13 +00:00
|
|
|
/* make sure failure will be seen at next port */
|
2014-09-02 20:19:23 +01:00
|
|
|
// no need to lock & unlock
|
2011-05-25 16:40:36 +01:00
|
|
|
if (LOCAL_PrologMode & AsyncIntMode)
|
2010-12-04 19:05:13 +00:00
|
|
|
Yap_signal(YAP_FAIL_SIGNAL);
|
2010-04-08 01:44:08 +01:00
|
|
|
P = FAILCODE;
|
2002-02-22 06:12:18 +00:00
|
|
|
} else {
|
2015-09-25 10:57:26 +01:00
|
|
|
if (IsVarTerm(where)) {
|
2002-02-22 06:12:18 +00:00
|
|
|
/* we must be careful someone gave us a copy to a local variable */
|
|
|
|
Term t = MkVarTerm();
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_unify(t, where);
|
2002-02-22 06:12:18 +00:00
|
|
|
where = Deref(where);
|
|
|
|
}
|
|
|
|
/* Exit Abort Mode, if we were there */
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode &= ~AbortMode;
|
2013-01-29 10:50:04 +00:00
|
|
|
LOCAL_CurrentError = type;
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode |= InErrorMode;
|
2005-02-24 21:46:40 +00:00
|
|
|
if (!(where = Yap_CopyTerm(where))) {
|
2015-09-25 10:57:26 +01:00
|
|
|
where = TermNil;
|
2005-02-24 21:46:40 +00:00
|
|
|
}
|
2002-01-10 18:01:14 +00:00
|
|
|
}
|
2015-10-05 10:29:07 +01:00
|
|
|
|
2011-05-25 16:40:36 +01:00
|
|
|
if (LOCAL_PrologMode & BootMode) {
|
2001-04-09 20:54:03 +01:00
|
|
|
/* crash in flames! */
|
2015-11-05 15:21:21 +00:00
|
|
|
fprintf(stderr, "%s:%d:0 YAP Fatal Error %d in function %s:\n %s exiting....\n", file, lineno, type, function, s);
|
2015-09-25 10:57:26 +01:00
|
|
|
error_exit_yap(1);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2015-06-19 01:30:13 +01:00
|
|
|
#ifdef DEBUG
|
|
|
|
// DumpActiveGoals( USES_REGS1 );
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif /* DEBUG */
|
2015-10-18 11:39:25 +01:00
|
|
|
if (!IsVarTerm(where) &&
|
|
|
|
IsApplTerm(where) &&
|
|
|
|
FunctorOfTerm(where) == FunctorError) {
|
|
|
|
error_t = where;
|
|
|
|
P = (yamop *)FAILCODE;
|
2016-03-29 01:55:12 +01:00
|
|
|
Yap_JumpToEnv(error_t);
|
2015-10-18 11:39:25 +01:00
|
|
|
LOCAL_PrologMode &= ~InErrorMode;
|
|
|
|
return P;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
switch (type) {
|
2016-05-14 11:34:27 +01:00
|
|
|
case SYSTEM_ERROR_INTERNAL: {
|
|
|
|
fprintf(stderr, "%% Internal YAP Error: %s exiting....\n", tmpbuf);
|
2016-08-01 23:50:31 +01:00
|
|
|
// serious = true;
|
2016-05-14 11:34:27 +01:00
|
|
|
if (LOCAL_PrologMode & BootMode) {
|
2015-10-18 11:39:25 +01:00
|
|
|
fprintf(stderr, "%% YAP crashed while booting %s\n", tmpbuf);
|
|
|
|
} else {
|
|
|
|
Yap_detect_bug_location(P, FIND_PRED_FROM_ANYWHERE, YAP_BUF_SIZE);
|
2016-02-20 23:58:06 +00:00
|
|
|
if (tmpbuf[0]) {
|
2016-05-14 11:34:27 +01:00
|
|
|
fprintf(stderr, "%% Bug found while executing %s\n", tmpbuf);
|
|
|
|
}
|
2016-02-20 23:58:06 +00:00
|
|
|
#if HAVE_BACKTRACE
|
|
|
|
void *callstack[256];
|
|
|
|
int i;
|
|
|
|
int frames = backtrace(callstack, 256);
|
|
|
|
char** strs = backtrace_symbols(callstack, frames);
|
|
|
|
fprintf(stderr, "Execution stack:\n");
|
|
|
|
for (i = 0; i < frames; ++i) {
|
2016-05-14 11:34:27 +01:00
|
|
|
fprintf(stderr, " %s\n", strs[i]);
|
2016-02-20 23:58:06 +00:00
|
|
|
}
|
|
|
|
free(strs);
|
|
|
|
#endif
|
2002-10-23 21:55:37 +01:00
|
|
|
}
|
2015-10-18 11:39:25 +01:00
|
|
|
error_exit_yap(1);
|
2015-09-25 10:57:26 +01:00
|
|
|
}
|
2015-10-18 11:39:25 +01:00
|
|
|
case SYSTEM_ERROR_FATAL: {
|
|
|
|
fprintf(stderr, "%% Fatal YAP Error: %s exiting....\n", tmpbuf);
|
|
|
|
error_exit_yap(1);
|
|
|
|
}
|
|
|
|
case INTERRUPT_EVENT: {
|
|
|
|
error_exit_yap(1);
|
|
|
|
}
|
|
|
|
case ABORT_EVENT:
|
|
|
|
nt[0] = MkAtomTerm(AtomDAbort);
|
2015-12-15 09:18:36 +00:00
|
|
|
fun = FunctorDollarVar;
|
2016-08-01 23:50:31 +01:00
|
|
|
// serious = true;
|
2015-10-18 11:39:25 +01:00
|
|
|
break;
|
|
|
|
case CALL_COUNTER_UNDERFLOW_EVENT:
|
|
|
|
/* Do a long jump */
|
|
|
|
LOCAL_ReductionsCounterOn = FALSE;
|
|
|
|
LOCAL_PredEntriesCounterOn = FALSE;
|
|
|
|
LOCAL_RetriesCounterOn = FALSE;
|
|
|
|
Yap_JumpToEnv(MkAtomTerm(AtomCallCounter));
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
LOCAL_PrologMode &= ~InErrorMode;
|
|
|
|
return (P);
|
|
|
|
case PRED_ENTRY_COUNTER_UNDERFLOW_EVENT:
|
|
|
|
/* Do a long jump */
|
|
|
|
LOCAL_ReductionsCounterOn = FALSE;
|
|
|
|
LOCAL_PredEntriesCounterOn = FALSE;
|
|
|
|
LOCAL_RetriesCounterOn = FALSE;
|
|
|
|
Yap_JumpToEnv(MkAtomTerm(AtomCallAndRetryCounter));
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
LOCAL_PrologMode &= ~InErrorMode;
|
|
|
|
return (P);
|
|
|
|
case RETRY_COUNTER_UNDERFLOW_EVENT:
|
|
|
|
/* Do a long jump */
|
|
|
|
LOCAL_ReductionsCounterOn = FALSE;
|
|
|
|
LOCAL_PredEntriesCounterOn = FALSE;
|
|
|
|
LOCAL_RetriesCounterOn = FALSE;
|
|
|
|
Yap_JumpToEnv(MkAtomTerm(AtomRetryCounter));
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
LOCAL_PrologMode &= ~InErrorMode;
|
|
|
|
return (P);
|
|
|
|
default: {
|
2016-05-14 11:34:27 +01:00
|
|
|
LOCAL_PrologMode &= ~InErrorMode;
|
2015-10-18 11:39:25 +01:00
|
|
|
Term ts[3];
|
|
|
|
ts[2] = where;
|
|
|
|
nt[0] = mkerrort(type, ts + 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-05-14 11:34:27 +01:00
|
|
|
}
|
|
|
|
LOCAL_PrologMode &= ~InErrorMode;
|
2015-09-25 10:57:26 +01:00
|
|
|
if (type != ABORT_EVENT) {
|
2016-01-20 23:16:04 +00:00
|
|
|
Term location;
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
/* This is used by some complex procedures to detect there was an error */
|
|
|
|
if (IsAtomTerm(nt[0])) {
|
2015-10-18 11:39:25 +01:00
|
|
|
strncpy(LOCAL_ErrorSay, (char *) RepAtom(AtomOfTerm(nt[0]))->StrOfAE,
|
2015-09-25 10:57:26 +01:00
|
|
|
MAX_ERROR_MSG_SIZE);
|
|
|
|
LOCAL_ErrorMessage = LOCAL_ErrorSay;
|
|
|
|
} else {
|
|
|
|
strncpy(LOCAL_ErrorSay,
|
2015-10-18 11:39:25 +01:00
|
|
|
(char *) RepAtom(NameOfFunctor(FunctorOfTerm(nt[0])))->StrOfAE,
|
2015-09-25 10:57:26 +01:00
|
|
|
MAX_ERROR_MSG_SIZE);
|
|
|
|
LOCAL_ErrorMessage = LOCAL_ErrorSay;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2016-01-20 23:16:04 +00:00
|
|
|
nt[1] = TermNil;
|
2015-10-18 11:39:25 +01:00
|
|
|
switch (type) {
|
2016-05-14 11:34:27 +01:00
|
|
|
case RESOURCE_ERROR_HEAP:
|
|
|
|
case RESOURCE_ERROR_STACK:
|
|
|
|
case RESOURCE_ERROR_TRAIL:
|
|
|
|
comment = MkAtomTerm(Yap_LookupAtom(tmpbuf));
|
|
|
|
default:
|
|
|
|
if (comment != TermNil)
|
|
|
|
nt[1] = MkPairTerm(MkPairTerm(MkAtomTerm(Yap_LookupAtom("i")), comment),
|
|
|
|
nt[1]);
|
|
|
|
if (file && function) {
|
|
|
|
Term ts[3], t3;
|
|
|
|
ts[0] = MkAtomTerm(Yap_LookupAtom(file));
|
|
|
|
ts[1] = MkIntegerTerm(lineno);
|
|
|
|
ts[2] = MkAtomTerm(Yap_LookupAtom(function));
|
|
|
|
t3 = Yap_MkApplTerm(Yap_MkFunctor(Yap_LookupAtom("c"), 3), 3, ts);
|
|
|
|
nt[1] =
|
|
|
|
MkPairTerm(MkPairTerm(MkAtomTerm(Yap_LookupAtom("c")), t3), nt[1]);
|
|
|
|
}
|
|
|
|
if ((location = Yap_pc_location(P, B, ENV)) != TermNil) {
|
|
|
|
nt[1] = MkPairTerm(MkPairTerm(MkAtomTerm(Yap_LookupAtom("p")), location),
|
|
|
|
nt[1]);
|
|
|
|
}
|
|
|
|
if ((location = Yap_env_location(CP, B, ENV, 0)) != TermNil) {
|
|
|
|
nt[1] = MkPairTerm(MkPairTerm(MkAtomTerm(Yap_LookupAtom("e")), location),
|
|
|
|
nt[1]);
|
|
|
|
}
|
2015-10-18 11:39:25 +01:00
|
|
|
}
|
2015-09-25 10:57:26 +01:00
|
|
|
}
|
2016-05-14 11:34:27 +01:00
|
|
|
/* disable active signals at this point */
|
|
|
|
LOCAL_Signals = 0;
|
|
|
|
CalculateStackGap(PASS_REGS1);
|
2015-10-18 11:39:25 +01:00
|
|
|
#if DEBUG
|
2016-05-14 11:34:27 +01:00
|
|
|
// DumpActiveGoals( PASS_REGS1 );
|
2015-10-18 11:39:25 +01:00
|
|
|
#endif
|
2016-05-14 11:34:27 +01:00
|
|
|
/* wait if we we are in user code,
|
2015-10-18 11:39:25 +01:00
|
|
|
it's up to her to decide */
|
2016-05-14 11:34:27 +01:00
|
|
|
fun = FunctorError;
|
2016-03-29 01:55:12 +01:00
|
|
|
error_t = Yap_MkApplTerm(fun, 2, nt);
|
|
|
|
|
2016-05-14 11:34:27 +01:00
|
|
|
if (type == ABORT_EVENT) {
|
|
|
|
error_t = MkAtomTerm(AtomDAbort);
|
|
|
|
} else {
|
|
|
|
error_t = Yap_MkApplTerm(fun, 2, nt);
|
|
|
|
}
|
|
|
|
Yap_JumpToEnv(error_t);
|
|
|
|
P = (yamop *)FAILCODE;
|
2016-03-29 01:55:12 +01:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2016-05-14 11:34:27 +01:00
|
|
|
LOCAL_PrologMode &= ~InErrorMode;
|
|
|
|
return P;
|
|
|
|
}
|
2015-12-15 09:28:43 +00:00
|
|
|
|
|
|
|
static Int
|
|
|
|
is_boolean( USES_REGS1 )
|
|
|
|
{
|
|
|
|
Term t = Deref(ARG1);
|
2016-02-11 14:00:56 +00:00
|
|
|
//Term Context = Deref(ARG2)Yap_Error(INSTANTIATION_ERROR, t, NULL);;
|
2015-12-15 09:28:43 +00:00
|
|
|
if (IsVarTerm(t)) {
|
|
|
|
Yap_Error(INSTANTIATION_ERROR, t, NULL);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return t == TermTrue || t == TermFalse;
|
|
|
|
}
|
|
|
|
|
2016-02-11 14:00:56 +00:00
|
|
|
static Int
|
|
|
|
is_atom( USES_REGS1 )
|
|
|
|
{
|
|
|
|
Term t = Deref(ARG1);
|
|
|
|
//Term Context = Deref(ARG2)Yap_Error(INSTANTIATION_ERROR, t, NULL);;
|
|
|
|
if (IsVarTerm(t)) {
|
|
|
|
Yap_Error(INSTANTIATION_ERROR, t, NULL);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return IsAtomTerm( t );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-15 09:28:43 +00:00
|
|
|
|
|
|
|
static Int
|
|
|
|
is_callable( USES_REGS1 )
|
|
|
|
{
|
|
|
|
Term G = Deref(ARG1);
|
|
|
|
//Term Context = Deref(ARG2);
|
|
|
|
while (true) {
|
|
|
|
if (IsVarTerm(G)) {
|
|
|
|
Yap_Error(INSTANTIATION_ERROR, G, NULL);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (IsApplTerm(G)) {
|
|
|
|
Functor f = FunctorOfTerm(G);
|
|
|
|
if (IsExtensionFunctor(f)) {
|
|
|
|
Yap_Error(TYPE_ERROR_CALLABLE, G, NULL);
|
|
|
|
}
|
|
|
|
if (f == FunctorModule) {
|
|
|
|
Term tm = ArgOfTerm( 1, G);
|
|
|
|
if (IsVarTerm(tm)) {
|
|
|
|
Yap_Error(INSTANTIATION_ERROR, G, NULL);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!IsAtomTerm(tm)) {
|
|
|
|
Yap_Error(TYPE_ERROR_CALLABLE, G, NULL);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
G = ArgOfTerm( 2, G );
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (IsPairTerm(G) || IsAtomTerm(G)) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
Yap_Error(TYPE_ERROR_CALLABLE, G, NULL);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-04 14:11:09 +00:00
|
|
|
static Int
|
|
|
|
is_predicate_indicator( USES_REGS1 )
|
|
|
|
{
|
|
|
|
Term G = Deref(ARG1);
|
|
|
|
//Term Context = Deref(ARG2);
|
|
|
|
Term mod = CurrentModule;
|
|
|
|
|
|
|
|
G = Yap_YapStripModule(G, &mod);
|
|
|
|
if (IsVarTerm(G)) {
|
|
|
|
Yap_Error(INSTANTIATION_ERROR, G, NULL);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!IsVarTerm(mod) && !IsAtomTerm(mod)) {
|
|
|
|
Yap_Error(TYPE_ERROR_ATOM, G, NULL);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (IsApplTerm(G)) {
|
|
|
|
Functor f = FunctorOfTerm(G);
|
|
|
|
if (IsExtensionFunctor(f)) {
|
|
|
|
Yap_Error(TYPE_ERROR_PREDICATE_INDICATOR, G, NULL);
|
|
|
|
}
|
|
|
|
if (f == FunctorSlash || f == FunctorDoubleSlash) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Yap_Error(TYPE_ERROR_PREDICATE_INDICATOR, G, NULL);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-15 09:28:43 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
Yap_InitErrorPreds( void )
|
|
|
|
{
|
|
|
|
CACHE_REGS
|
2016-05-14 11:34:27 +01:00
|
|
|
Yap_InitCPred("is_boolean", 2, is_boolean, TestPredFlag);
|
2016-01-03 01:29:27 +00:00
|
|
|
Yap_InitCPred("is_callable", 2, is_callable, TestPredFlag);
|
2016-02-11 14:00:56 +00:00
|
|
|
Yap_InitCPred("is_atom", 2, is_atom, TestPredFlag);
|
2016-01-04 14:11:09 +00:00
|
|
|
Yap_InitCPred("is_predicate_indicator", 2, is_predicate_indicator, TestPredFlag);
|
2015-12-15 09:28:43 +00:00
|
|
|
}
|