move all error related state to a record

This commit is contained in:
Vitor Santos Costa 2016-09-20 22:52:34 -05:00
parent 6043125221
commit 124b2e9069
9 changed files with 547 additions and 424 deletions

View File

@ -11,7 +11,7 @@
* File: errors.c *
* Last Rev: *
* Mods: *
* Comments: Yap'S error handlers *
* Comments: Yap's error handlers *
* *
*************************************************************************/
@ -32,7 +32,6 @@
#endif
#include "Foreign.h"
bool Yap_Warning(const char *s, ...) {
CACHE_REGS
va_list ap;
@ -62,8 +61,7 @@ bool Yap_Warning(const char *s, ...) {
} else
return false;
va_end(ap);
if (pred->OpcodeOfPred == UNDEF_OPCODE||
pred->OpcodeOfPred == FAIL_OPCODE) {
if (pred->OpcodeOfPred == UNDEF_OPCODE || pred->OpcodeOfPred == FAIL_OPCODE) {
fprintf(stderr, "warning message: %s\n", tmpbuf);
LOCAL_DoingUndefp = false;
LOCAL_within_print_message = false;
@ -75,6 +73,21 @@ bool Yap_Warning(const char *s, ...) {
rc = Yap_execute_pred(pred, ts, true PASS_REGS);
return rc;
}
void Yap_InitError(yap_error_number e, Term t, const char *msg) {
if (LOCAL_ActiveError.status) {
Yap_exit(1);
}
LOCAL_ActiveError.errorNo = e;
LOCAL_ActiveError.errorFile = NULL;
LOCAL_ActiveError.errorFunction = NULL;
LOCAL_ActiveError.errorLine = 0;
if (msg) {
LOCAL_Error_Size = strlen(msg);
strcpy(LOCAL_ActiveError.errorComment, msg);
} else {
LOCAL_Error_Size = 0;
}
}
bool Yap_PrintWarning(Term twarning) {
CACHE_REGS
@ -91,9 +104,7 @@ bool Yap_PrintWarning(Term twarning) {
}
LOCAL_DoingUndefp = true;
LOCAL_within_print_message = true;
if (pred->OpcodeOfPred == UNDEF_OPCODE ||
pred->OpcodeOfPred == FAIL_OPCODE
) {
if (pred->OpcodeOfPred == UNDEF_OPCODE || pred->OpcodeOfPred == FAIL_OPCODE) {
fprintf(stderr, "warning message:\n");
Yap_DebugPlWrite(twarning);
fprintf(stderr, "\n");
@ -112,12 +123,12 @@ bool Yap_PrintWarning(Term twarning) {
return rc;
}
bool Yap_HandleError__(const char *file, const char *function, int lineno, const char *s, ...) {
bool Yap_HandleError__(const char *file, const char *function, int lineno,
const char *s, ...) {
CACHE_REGS
yap_error_number err = LOCAL_Error_TYPE;
const char *serr;
LOCAL_Error_TYPE = YAP_NO_ERROR;
if (LOCAL_ErrorMessage) {
serr = LOCAL_ErrorMessage;
} else {
@ -136,7 +147,8 @@ bool Yap_HandleError__(const char *file, const char *function, int lineno, const
}
if (!Yap_ExpandPreAllocCodeSpace(0, NULL, TRUE)) {
/* crash in flames */
Yap_Error__(file, function, lineno, RESOURCE_ERROR_AUXILIARY_STACK, ARG1, serr);
Yap_Error__(file, function, lineno, RESOURCE_ERROR_AUXILIARY_STACK, ARG1,
serr);
return false;
}
return true;
@ -156,7 +168,6 @@ int Yap_SWIHandleError(const char *s, ...) {
yap_error_number err = LOCAL_Error_TYPE;
char *serr;
LOCAL_Error_TYPE = YAP_NO_ERROR;
if (LOCAL_ErrorMessage) {
serr = LOCAL_ErrorMessage;
} else {
@ -214,7 +225,6 @@ static void error_exit_yap(int value) {
fprintf(stderr, "Execution stack:\n");
for (i = 0; i < frames; ++i) {
fprintf(stderr, " %s\n", strs[i]);
}
free(strs);
#endif
@ -336,14 +346,20 @@ yamop *Yap_Error__(const char *file, const char *function, int lineno,
tmpbuf);
Yap_RestartYap(1);
}
LOCAL_ActiveError.errorNo = type;
LOCAL_ActiveError.errorClass = Yap_errorClass(LOCAL_ActiveError.errorNo);
LOCAL_ActiveError.errorLine = lineno;
LOCAL_ActiveError.errorFunction = function;
LOCAL_ActiveError.errorFile = file;
Yap_find_prolog_culprit(PASS_REGS1);
LOCAL_PrologMode |= InErrorMode;
LOCAL_Error_TYPE = YAP_NO_ERROR;
Yap_ClearExs();
if (where == 0L) {
where = TermNil;
}
// first, obtain current location
// sprintf(LOCAL_FileNameBuf, "%s:%d in C-function %s ", file, lineno, function);
// sprintf(LOCAL_FileNameBuf, "%s:%d in C-function %s ", file, lineno,
// function);
// tf = MkAtomTerm(Yap_LookupAtom(LOCAL_FileNameBuf));
#if DEBUG_STRICT
if (Yap_heap_regs && !(LOCAL_PrologMode & BootMode))
@ -416,14 +432,15 @@ yamop *Yap_Error__(const char *file, const char *function, int lineno,
if (LOCAL_PrologMode & BootMode) {
/* crash in flames! */
fprintf(stderr, "%s:%d:0 YAP Fatal Error %d in function %s:\n %s exiting....\n", file, lineno, type, function, s);
fprintf(stderr,
"%s:%d:0 YAP Fatal Error %d in function %s:\n %s exiting....\n",
file, lineno, type, function, s);
error_exit_yap(1);
}
#ifdef DEBUG
// DumpActiveGoals( USES_REGS1 );
#endif /* DEBUG */
if (!IsVarTerm(where) &&
IsApplTerm(where) &&
if (!IsVarTerm(where) && IsApplTerm(where) &&
FunctorOfTerm(where) == FunctorError) {
error_t = where;
P = (yamop *)FAILCODE;
@ -501,9 +518,6 @@ yamop *Yap_Error__(const char *file, const char *function, int lineno,
ts[2] = where;
nt[0] = mkerrort(type, ts + 2);
}
}
LOCAL_PrologMode &= ~InErrorMode;
if (type != ABORT_EVENT) {
@ -540,12 +554,12 @@ yamop *Yap_Error__(const char *file, const char *function, int lineno,
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]);
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]);
nt[1] = MkPairTerm(
MkPairTerm(MkAtomTerm(Yap_LookupAtom("e")), location), nt[1]);
}
}
}
@ -568,14 +582,11 @@ yamop *Yap_Error__(const char *file, const char *function, int lineno,
Yap_JumpToEnv(error_t);
P = (yamop *)FAILCODE;
LOCAL_PrologMode &= ~InErrorMode;
return P;
}
static Int
is_boolean( USES_REGS1 )
{
static Int is_boolean(USES_REGS1) {
Term t = Deref(ARG1);
// Term Context = Deref(ARG2)Yap_Error(INSTANTIATION_ERROR, t, NULL);;
if (IsVarTerm(t)) {
@ -585,9 +596,7 @@ is_boolean( USES_REGS1 )
return t == TermTrue || t == TermFalse;
}
static Int
is_atom( USES_REGS1 )
{
static Int is_atom(USES_REGS1) {
Term t = Deref(ARG1);
// Term Context = Deref(ARG2)Yap_Error(INSTANTIATION_ERROR, t, NULL);;
if (IsVarTerm(t)) {
@ -597,11 +606,7 @@ is_atom( USES_REGS1 )
return IsAtomTerm(t);
}
static Int
is_callable( USES_REGS1 )
{
static Int is_callable(USES_REGS1) {
Term G = Deref(ARG1);
// Term Context = Deref(ARG2);
while (true) {
@ -638,9 +643,7 @@ is_callable( USES_REGS1 )
return false;
}
static Int
is_predicate_indicator( USES_REGS1 )
{
static Int is_predicate_indicator(USES_REGS1) {
Term G = Deref(ARG1);
// Term Context = Deref(ARG2);
Term mod = CurrentModule;
@ -667,13 +670,85 @@ is_predicate_indicator( USES_REGS1 )
return false;
}
static Int close_error(USES_REGS1) {
LOCAL_Error_TYPE = YAP_NO_ERROR;
return true;
}
void
Yap_InitErrorPreds( void )
{
#undef BEGIN_ERROR_CLASSES
#undef ECLASS
#undef END_ERROR_CLASSES
#undef BEGIN_ERRORS
#undef E0
#undef E
#undef E2
#undef END_ERRORS
#define BEGIN_ERROR_CLASSES() typedef enum aux_class {
#define ECLASS(CL, A, B) CL##__,
#define END_ERROR_CLASSES() \
} \
aux_class_t;
#define BEGIN_ERRORS()
#define E0(X, Y)
#define E(X, Y, Z)
#define E2(X, Y, Z, W)
#define END_ERRORS()
#include <YapErrors.h>
#undef BEGIN_ERROR_CLASSES
#undef ECLASS
#undef END_ERROR_CLASSES
#undef BEGIN_ERRORS
#undef E0
#undef E
#undef E2
#undef END_ERRORS
#define BEGIN_ERROR_CLASSES() static const char *c_error_class_name[] = {
#define ECLASS(CL, A, B) A,
#define END_ERROR_CLASSES() \
NULL \
}
typedef struct c_error_info {
int class;
const char *name;
} c_error_t;
#define BEGIN_ERRORS() static struct c_error_info c_error_list[] = {
#define E0(X, Y) {Y##__, ""},
#define E(X, Y, Z) {Y##__, Z},
#define E2(X, Y, Z, W) {Y##__, Z " " W},
#define END_ERRORS() \
{ YAPC_NO_ERROR, "" } \
} \
;
#include <YapErrors.h>
yap_error_class_number Yap_errorClass(yap_error_number e) {
return c_error_list[e].class;
}
const char *Yap_errorName(yap_error_number e) { return c_error_list[e].name; }
const char *Yap_errorClassName(yap_error_class_number e) {
return c_error_class_name[e];
}
void Yap_InitErrorPreds(void) {
CACHE_REGS
Yap_InitCPred("$close_error", 0, close_error, HiddenPredFlag);
Yap_InitCPred("is_boolean", 2, is_boolean, TestPredFlag);
Yap_InitCPred("is_callable", 2, is_callable, TestPredFlag);
Yap_InitCPred("is_atom", 2, is_atom, TestPredFlag);
Yap_InitCPred("is_predicate_indicator", 2, is_predicate_indicator, TestPredFlag);
Yap_InitCPred("is_predicate_indicator", 2, is_predicate_indicator,
TestPredFlag);
}

View File

@ -86,8 +86,6 @@ char* ScannerStack =NULL
struct scanner_extra_alloc* ScannerExtraBlocks =NULL
/// worker control information
/// pointer to an exception term, from throw
struct DB_TERM* BallTerm =NULL RestoreBallTerm(wid)
/// stack limit after which the stack is managed by C-code.
Int CBorder =0
@ -198,12 +196,12 @@ ADDR TrailBase void
ADDR TrailTop void
char* ErrorMessage void
Term Error_Term void
yap_error_number Error_TYPE void
const char* Error_File void
const char* Error_Function void
int Error_Lineno void
size_t Error_Size void
char ErrorSay[MAX_ERROR_MSG_SIZE] void
/** error handling info, designed to be easy to pass to the foreign world */
struct yap_error_descriptor ActiveError void
/// pointer to an exception term, from throw
struct DB_TERM* BallTerm =NULL
jmp_buf IOBotch void
TokEntry* tokptr void
TokEntry* toktide void
@ -307,7 +305,7 @@ UInt exo_arg =0
struct scan_atoms* search_atoms void
struct pred_entry* SearchPreds void
// Slots
/// Slots Status
yhandle_t CurSlot =0
yhandle_t FrozenHandles =0
yhandle_t NSlots =0

View File

@ -48,7 +48,7 @@
#define HeapLim Yap_heap_regs->HeapLim_
#define FreeBlocks Yap_heap_regs->FreeBlocks_
#if defined(YAPOR) || defined(THREADS)
#define fFreeBlocksLock Yap_heap_regs->FreeBlocksLock_
#define FreeBlocksLock Yap_heap_regs->FreeBlocksLock_
#define HeapUsedLock Yap_heap_regs->HeapUsedLock_
#define HeapTopLock Yap_heap_regs->HeapTopLock_
#define HeapTopOwner Yap_heap_regs->HeapTopOwner_

View File

@ -133,9 +133,6 @@
#define REMOTE_ScannerExtraBlocks(wid) REMOTE(wid)->ScannerExtraBlocks_
#define LOCAL_BallTerm LOCAL->BallTerm_
#define REMOTE_BallTerm(wid) REMOTE(wid)->BallTerm_
#define LOCAL_CBorder LOCAL->CBorder_
#define REMOTE_CBorder(wid) REMOTE(wid)->CBorder_
@ -295,18 +292,12 @@
#define REMOTE_ErrorMessage(wid) REMOTE(wid)->ErrorMessage_
#define LOCAL_Error_Term LOCAL->Error_Term_
#define REMOTE_Error_Term(wid) REMOTE(wid)->Error_Term_
#define LOCAL_Error_TYPE LOCAL->Error_TYPE_
#define REMOTE_Error_TYPE(wid) REMOTE(wid)->Error_TYPE_
#define LOCAL_Error_File LOCAL->Error_File_
#define REMOTE_Error_File(wid) REMOTE(wid)->Error_File_
#define LOCAL_Error_Function LOCAL->Error_Function_
#define REMOTE_Error_Function(wid) REMOTE(wid)->Error_Function_
#define LOCAL_Error_Lineno LOCAL->Error_Lineno_
#define REMOTE_Error_Lineno(wid) REMOTE(wid)->Error_Lineno_
#define LOCAL_Error_Size LOCAL->Error_Size_
#define REMOTE_Error_Size(wid) REMOTE(wid)->Error_Size_
#define LOCAL_ErrorSay LOCAL->ErrorSay_
#define REMOTE_ErrorSay(wid) REMOTE(wid)->ErrorSay_
#define LOCAL_ActiveError LOCAL->ActiveError_
#define REMOTE_ActiveError(wid) REMOTE(wid)->ActiveError_
#define LOCAL_BallTerm LOCAL->BallTerm_
#define REMOTE_BallTerm(wid) REMOTE(wid)->BallTerm_
#define LOCAL_IOBotch LOCAL->IOBotch_
#define REMOTE_IOBotch(wid) REMOTE(wid)->IOBotch_
#define LOCAL_tokptr LOCAL->tokptr_

View File

@ -1,5 +1,6 @@
/* This file, hlocals.h, was generated automatically by "yap -L misc/buildlocalglobal"
/* This file, hlocals.h, was generated automatically by "yap -L
misc/buildlocalglobal"
please do not update, update H/LOCALS instead */
// Stuff that must be considered local to a thread or worker
@ -75,8 +76,6 @@ typedef struct worker_local {
char *ScannerStack_;
struct scanner_extra_alloc *ScannerExtraBlocks_;
/// worker control information
/// pointer to an exception term, from throw
struct DB_TERM* BallTerm_;
/// stack limit after which the stack is managed by C-code.
Int CBorder_;
/// max number of signals (uint64_t)
@ -147,7 +146,8 @@ typedef struct worker_local {
Int *LabelFirstArray_;
UInt LabelFirstArraySz_;
// Thread Local Area for SWI-Prolog emulation routines.
// struct PL_local_data* PL_local_data_p =Yap_InitThreadIO(wid)
// struct PL_local_data* PL_local_data_p
// =Yap_InitThreadIO(wid)
#ifdef THREADS
struct thandle ThreadHandle_;
#endif /* THREADS */
@ -168,12 +168,10 @@ typedef struct worker_local {
ADDR TrailTop_;
char *ErrorMessage_;
Term Error_Term_;
yap_error_number Error_TYPE_;
const char* Error_File_;
const char* Error_Function_;
int Error_Lineno_;
size_t Error_Size_;
char ErrorSay_[MAX_ERROR_MSG_SIZE];
/** error handling info, designed to be easy to pass to the foreign world */
struct yap_error_descriptor ActiveError_;
/// pointer to an exception term, from throw
struct DB_TERM *BallTerm_;
jmp_buf IOBotch_;
TokEntry *tokptr_;
TokEntry *toktide_;
@ -186,8 +184,8 @@ const char* Error_Function_;
size_t CommentsBuffPos_;
size_t CommentsBuffLim_;
sigjmp_buf RestartEnv_;
char FileNameBuf_[YAP_FILENAME_MAX];
char FileNameBuf2_[YAP_FILENAME_MAX];
char FileNameBuf_[YAP_FILENAME_MAX + 1];
char FileNameBuf2_[YAP_FILENAME_MAX + 1];
// Prolog State
UInt BreakLevel_;
Int PrologMode_;
@ -264,7 +262,7 @@ const char* Error_Function_;
// atom completion
struct scan_atoms *search_atoms_;
struct pred_entry *SearchPreds_;
// Slots
/// Slots Status
yhandle_t CurSlot_;
yhandle_t FrozenHandles_;
yhandle_t NSlots_;

View File

@ -76,8 +76,6 @@ static void InitWorker(int wid) {
REMOTE_ScannerExtraBlocks(wid) = NULL;
REMOTE_BallTerm(wid) = NULL;
REMOTE_CBorder(wid) = 0;
REMOTE_MaxActiveSignals(wid) = 64L;
@ -171,9 +169,7 @@ static void InitWorker(int wid) {
REMOTE_BallTerm(wid) = NULL;

View File

@ -76,8 +76,6 @@ static void RestoreWorker(int wid USES_REGS) {
RestoreBallTerm(wid);
@ -192,8 +190,6 @@ static void RestoreWorker(int wid USES_REGS) {

View File

@ -36,22 +36,28 @@
#include "YapErrors.h"
struct yami *Yap_Error__(const char *file, const char *function, int lineno, yap_error_number err,YAP_Term wheret, ...);
#define MAX_ERROR_MSG_SIZE 10
#define Yap_NilError( id, ...) Yap_Error__(__FILE__, __FUNCTION__, __LINE__, id, TermNil, __VA_ARGS__)
struct yami *Yap_Error__(const char *file, const char *function, int lineno,
yap_error_number err, YAP_Term wheret, ...);
#define Yap_Error( id, inp, ...) Yap_Error__(__FILE__, __FUNCTION__, __LINE__, id, inp, __VA_ARGS__)
#define Yap_NilError(id, ...) \
Yap_Error__(__FILE__, __FUNCTION__, __LINE__, id, TermNil, __VA_ARGS__)
#define Yap_Error(id, inp, ...) \
Yap_Error__(__FILE__, __FUNCTION__, __LINE__, id, inp, __VA_ARGS__)
#ifdef YAP_TERM_H
/**
* make sure next argument is a bound instance of type
* atom.
*/
#define Yap_ensure_atom( T0, TF ) { if ( (TF = Yap_ensure_atom__(__FILE__, __FUNCTION__, __LINE__, T0 ) == 0L ) return false; }
#define Yap_ensure_atom(T0, TF) \
{ if ( (TF = Yap_ensure_atom__(__FILE__, __FUNCTION__, __LINE__, T0 ) == 0L ) return false; \
}
INLINE_ONLY extern inline Term
Yap_ensure_atom__( const char *fu, const char *fi, int line, Term in )
{
INLINE_ONLY extern inline Term Yap_ensure_atom__(const char *fu, const char *fi,
int line, Term in) {
Term t = Deref(in);
// Term Context = Deref(ARG2);
if (!IsVarTerm(t) && IsAtomTerm(t))
@ -59,7 +65,8 @@ Yap_ensure_atom__( const char *fu, const char *fi, int line, Term in )
if (IsVarTerm(t)) {
Yap_Error__(fu, fi, line, INSTANTIATION_ERROR, t, NULL);
} else {
if ( IsAtomTerm(t) ) return t ;
if (IsAtomTerm(t))
return t;
Yap_Error__(fu, fi, line, TYPE_ERROR_ATOM, t, NULL);
return 0L;
}
@ -138,4 +145,67 @@ Yap_ensure_atom__( const char *fu, const char *fi, int line, Term in )
goto LAB; \
}
/**
* Error stages since it was initially processed.
*
* Notice that some of the stages may be active simultaneouly.
*/
typedef enum yap_error_status {
/// where we like to be
YAP_NO_ERROR_STATUS = 0x0,
/// Prolog discovered the problem
YAP_ERROR_INITIATED_IN_PROLOG = 0x1,
/// The problem was found before the system can cope
YAP_ERROR_INITIATED_IN_BOOT = 0x2,
/// C-helper like must_ found out the problem
YAP_ERROR_INITIATED_IN_HELPER = 0x4,
/// C-builtin crashed
YAP_ERROR_INITIATED_IN_SYSTEM_C = 0x8,
/// user code crashed
YAP_ERROR_INITIATED_IN_USER_C = 0x10,
/// ok, we put a throw to be dispatched
YAP_THROW_THROWN = 0x20,
/// someone caught it
YAP_THROW_CAUGHT = 0x40,
/// error became an exception (usually SWIG bridge)
YAP_ERROR_EXPORTED_TO_CXX = 0x80,
/// handle error in Prolog
YAP_ERROR_BEING_PROCESSED_IN_PROLOG = 0x100
/// go back t
} yap_error_stage_t;
/// all we need to know about an error/throw
typedef struct yap_error_descriptor {
enum yap_error_status status;
yap_error_class_number errorClass;
yap_error_number errorNo;
YAP_Int errorLine;
const char *errorFunction;
const char *errorFile;
YAP_Int prologPredCl;
YAP_UInt prologPredLine;
YAP_UInt prologPredFirstLine;
YAP_UInt prologPredLastLine;
YAP_Atom prologPredName;
YAP_UInt prologPredArity;
YAP_Term prologPredModule;
YAP_Atom prologPredFile;
struct DB_TERM *errorTerm;
char errorComment[MAX_ERROR_MSG_SIZE];
size_t errorMsgLen;
} yap_error_descriptor_t;
/// compatibility with existing code..
#define LOCAL_Error_TYPE LOCAL_ActiveError.errorNo
#define LOCAL_Error_File LOCAL_ActiveError.errorFile
#define LOCAL_Error_Function LOCAL_ActiveError.errorFunction
#define LOCAL_Error_Lineno LOCAL_ActiveError.errorLine
#define LOCAL_Error_Size LOCAL_ActiveError.errorMsgLen
#define LOCAL_ErrorSay LOCAL_ActiveError.errorComment
extern bool Yap_find_prolog_culprit();
extern yap_error_class_number Yap_errorClass(yap_error_number e);
extern const char *Yap_errorName(yap_error_number e);
extern const char *Yap_errorClassName(yap_error_class_number e);
#endif

View File

@ -1,7 +1,6 @@
///
/// @file YapErrors.h
///
/// @adddtogroup YapError
/// /// @adddtogroup YapError
///
/// The file YapErrors.h contains a list with all the error classes known
/// internally to the YAP system.