This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/C/stack.c

2022 lines
57 KiB
C
Raw Normal View History

2015-09-21 23:05:36 +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 *
* *
**************************************************************************
* *
* File: stack.c *
* comments: Stack Introspection *
2015-09-21 23:05:36 +01:00
* *
* Last rev: $Date: 2008-07-22 23:34:44 $,$Author: vsc $ *
* $Log: not supported by cvs2svn $ *
* Revision 1.230 2008/06/02 17:20:28 vsc *
* *
* *
*************************************************************************/
/**
* @file stack.c
* @author VITOR SANTOS COSTA <vsc@VITORs-MacBook-Pro.local>
* @date Tue Sep 8 23:33:02 2015
*
2015-09-21 23:05:36 +01:00
* @brief Get to know what is in your stack.
*
*
2015-09-21 23:05:36 +01:00
*/
#include "Yap.h"
#include "clause.h"
#include "yapio.h"
2015-11-05 15:32:57 +00:00
#include "iopreds.h"
2015-09-21 23:05:36 +01:00
#include "eval.h"
#include "tracer.h"
#ifdef YAPOR
#include "or.macros.h"
#endif /* YAPOR */
2015-09-21 23:05:36 +01:00
#ifdef TABLING
#include "tab.macros.h"
#endif /* TABLING */
#if HAVE_STRING_H
#include <string.h>
#endif
#include <heapgc.h>
#if !defined(YAPOR) && !defined(THREADS)
static void mark_pred(int, PredEntry *);
static void do_toggle_static_predicates_in_use(int);
#endif
static Int in_use(USES_REGS1);
static Int PredForCode(yamop *, Atom *, arity_t *, Term *, PredEntry **);
2015-09-21 23:05:36 +01:00
static LogUpdIndex *find_owner_log_index(LogUpdIndex *, yamop *);
static StaticIndex *find_owner_static_index(StaticIndex *, yamop *);
#define IN_BLOCK(P, B, SZ) \
((CODEADDR)(P) >= (CODEADDR)(B) && (CODEADDR)(P) < (CODEADDR)(B) + (SZ))
2015-09-21 23:05:36 +01:00
static PredEntry *get_pred(Term t, Term tmod, char *pname) {
2015-09-21 23:05:36 +01:00
Term t0 = t;
restart:
2015-09-21 23:05:36 +01:00
if (IsVarTerm(t)) {
Yap_Error(INSTANTIATION_ERROR, t0, pname);
return NULL;
} else if (IsAtomTerm(t)) {
return RepPredProp(Yap_GetPredPropByAtom(AtomOfTerm(t), tmod));
} else if (IsIntegerTerm(t) && tmod == IDB_MODULE) {
return Yap_FindLUIntKey(IntegerOfTerm(t));
} else if (IsApplTerm(t)) {
Functor fun = FunctorOfTerm(t);
2015-09-21 23:05:36 +01:00
if (IsExtensionFunctor(fun)) {
Yap_Error(TYPE_ERROR_CALLABLE, Yap_PredicateIndicator(t, tmod), pname);
return NULL;
}
if (fun == FunctorModule) {
Term tmod = ArgOfTerm(1, t);
if (IsVarTerm(tmod)) {
Yap_Error(INSTANTIATION_ERROR, t0, pname);
return NULL;
2015-09-21 23:05:36 +01:00
}
if (!IsAtomTerm(tmod)) {
Yap_Error(TYPE_ERROR_ATOM, t0, pname);
return NULL;
2015-09-21 23:05:36 +01:00
}
t = ArgOfTerm(2, t);
goto restart;
}
return RepPredProp(Yap_GetPredPropByFunc(fun, tmod));
} else
return NULL;
}
static PredEntry *PredForChoicePt(yamop *p_code, op_numbers *opn) {
2015-09-21 23:05:36 +01:00
while (TRUE) {
op_numbers opnum;
if (!p_code)
return NULL;
opnum = Yap_op_from_opcode(p_code->opc);
if (opn)
*opn = opnum;
switch (opnum) {
2015-09-21 23:05:36 +01:00
case _Nstop:
return NULL;
case _jump:
p_code = p_code->y_u.l.l;
break;
case _retry_me:
case _trust_me:
return p_code->y_u.Otapl.p;
case _retry_exo:
case _retry_all_exo:
return p_code->y_u.lp.p;
case _try_logical:
case _retry_logical:
case _trust_logical:
case _count_retry_logical:
case _count_trust_logical:
case _profiled_retry_logical:
case _profiled_trust_logical:
return p_code->y_u.OtaLl.d->ClPred;
#ifdef TABLING
case _trie_trust_var:
case _trie_retry_var:
case _trie_trust_var_in_pair:
case _trie_retry_var_in_pair:
case _trie_trust_val:
case _trie_retry_val:
case _trie_trust_val_in_pair:
case _trie_retry_val_in_pair:
case _trie_trust_atom:
case _trie_retry_atom:
case _trie_trust_atom_in_pair:
case _trie_retry_atom_in_pair:
case _trie_trust_null:
case _trie_retry_null:
case _trie_trust_null_in_pair:
case _trie_retry_null_in_pair:
case _trie_trust_pair:
case _trie_retry_pair:
case _trie_trust_appl:
case _trie_retry_appl:
case _trie_trust_appl_in_pair:
case _trie_retry_appl_in_pair:
case _trie_trust_extension:
case _trie_retry_extension:
case _trie_trust_double:
case _trie_retry_double:
case _trie_trust_longint:
case _trie_retry_longint:
case _trie_trust_gterm:
case _trie_retry_gterm:
return NULL;
case _table_load_answer:
case _table_try_answer:
case _table_answer_resolution:
case _table_completion:
#ifdef THREADS_CONSUMER_SHARING
case _table_answer_resolution_completion:
#endif /* THREADS_CONSUMER_SHARING */
2015-09-21 23:05:36 +01:00
return NULL; /* ricroc: is this OK? */
/* compile error --> return ENV_ToP(gc_B->cp_cp); */
#endif /* TABLING */
2015-09-21 23:05:36 +01:00
case _or_else:
if (p_code == p_code->y_u.Osblp.l) {
/* repeat */
Atom at = AtomRepeatSpace;
return RepPredProp(PredPropByAtom(at, PROLOG_MODULE));
2015-09-21 23:05:36 +01:00
} else {
return p_code->y_u.Osblp.p0;
2015-09-21 23:05:36 +01:00
}
break;
case _or_last:
#ifdef YAPOR
return p_code->y_u.Osblp.p0;
#else
return p_code->y_u.p.p;
#endif /* YAPOR */
break;
case _count_retry_me:
case _retry_profiled:
case _retry2:
case _retry3:
case _retry4:
p_code = NEXTOP(p_code, l);
2015-09-21 23:05:36 +01:00
break;
default:
return p_code->y_u.Otapl.p;
}
}
return NULL;
}
/**
2015-09-21 23:05:36 +01:00
* Yap_PredForChoicePt(): find out the predicate who generated a CP.
*
2015-09-21 23:05:36 +01:00
* @param cp the choice point
* @param op the YAAM instruction to process next
*
2015-09-21 23:05:36 +01:00
* @return A predixate structure or NULL
*
* usually pretty straightforward, it can fall in trouble with
8 OR-P or tabling.
*/
PredEntry *Yap_PredForChoicePt(choiceptr cp, op_numbers *op) {
2015-09-21 23:05:36 +01:00
if (cp == NULL)
return NULL;
return PredForChoicePt(cp->cp_ap, op);
}
#if !defined(YAPOR) && !defined(THREADS)
static yamop *cur_clause(PredEntry *pe, yamop *codeptr) {
2015-09-21 23:05:36 +01:00
StaticClause *cl;
cl = ClauseCodeToStaticClause(pe->cs.p_code.FirstClause);
do {
if (IN_BLOCK(codeptr, cl, cl->ClSize)) {
2015-09-21 23:05:36 +01:00
return cl->ClCode;
}
if (cl->ClCode == pe->cs.p_code.LastClause)
break;
cl = cl->ClNext;
} while (TRUE);
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
"could not find clause for indexing code");
return (NULL);
2015-09-21 23:05:36 +01:00
}
static yamop *cur_log_upd_clause(PredEntry *pe, yamop *codeptr) {
2015-09-21 23:05:36 +01:00
LogUpdClause *cl;
cl = ClauseCodeToLogUpdClause(pe->cs.p_code.FirstClause);
do {
if (IN_BLOCK(codeptr, cl->ClCode, cl->ClSize)) {
return ((yamop *)cl->ClCode);
2015-09-21 23:05:36 +01:00
}
cl = cl->ClNext;
} while (cl != NULL);
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
"could not find clause for indexing code");
return (NULL);
2015-09-21 23:05:36 +01:00
}
bool Yap_search_for_static_predicate_in_use(PredEntry *p,
bool check_everything) {
2015-09-21 23:05:36 +01:00
choiceptr b_ptr = B;
CELL *env_ptr = ENV;
if (check_everything && P) {
PredEntry *pe = EnvPreg(P);
if (p == pe)
return true;
2015-09-21 23:05:36 +01:00
pe = EnvPreg(CP);
if (p == pe)
return true;
2015-09-21 23:05:36 +01:00
}
do {
PredEntry *pe;
/* check first environments that are younger than our latest choicepoint */
if (check_everything && env_ptr) {
/*
I do not need to check environments for asserts,
only for retracts
2015-09-21 23:05:36 +01:00
*/
while (env_ptr && b_ptr > (choiceptr)env_ptr) {
yamop *cp = (yamop *)env_ptr[E_CP];
PredEntry *pe;
pe = EnvPreg(cp);
if (p == pe)
return true;
if (env_ptr != NULL)
env_ptr = (CELL *)(env_ptr[E_E]);
2015-09-21 23:05:36 +01:00
}
}
/* now mark the choicepoint */
if (b_ptr)
pe = PredForChoicePt(b_ptr->cp_ap, NULL);
else
return false;
if (pe == p) {
if (check_everything)
return true;
PELOCK(38, p);
2015-09-21 23:05:36 +01:00
if (p->PredFlags & IndexedPredFlag) {
yamop *code_p = b_ptr->cp_ap;
yamop *code_beg = p->cs.p_code.TrueCodeOfPred;
/* FIX ME */
if (p->PredFlags & LogUpdatePredFlag) {
LogUpdIndex *cl = ClauseCodeToLogUpdIndex(code_beg);
if (find_owner_log_index(cl, code_p))
b_ptr->cp_ap = cur_log_upd_clause(pe, b_ptr->cp_ap->y_u.Otapl.d);
} else if (p->PredFlags & MegaClausePredFlag) {
StaticIndex *cl = ClauseCodeToStaticIndex(code_beg);
if (find_owner_static_index(cl, code_p))
b_ptr->cp_ap = cur_clause(pe, b_ptr->cp_ap->y_u.Otapl.d);
} else {
/* static clause */
StaticIndex *cl = ClauseCodeToStaticIndex(code_beg);
if (find_owner_static_index(cl, code_p)) {
b_ptr->cp_ap = cur_clause(pe, b_ptr->cp_ap->y_u.Otapl.d);
}
}
2015-09-21 23:05:36 +01:00
}
UNLOCKPE(63, pe);
2015-09-21 23:05:36 +01:00
}
env_ptr = b_ptr->cp_env;
b_ptr = b_ptr->cp_b;
} while (b_ptr != NULL);
return (FALSE);
2015-09-21 23:05:36 +01:00
}
static void mark_pred(int mark, PredEntry *pe) {
2015-09-21 23:05:36 +01:00
/* if the predicate is static mark it */
if (pe->ModuleOfPred) {
PELOCK(39, p);
2015-09-21 23:05:36 +01:00
if (mark) {
pe->PredFlags |= InUsePredFlag;
} else {
pe->PredFlags &= ~InUsePredFlag;
}
UNLOCK(pe->PELock);
}
}
/* go up the chain of choice_points and environments,
marking all static predicates that current execution is depending
upon */
static void do_toggle_static_predicates_in_use(int mask) {
2015-09-21 23:05:36 +01:00
choiceptr b_ptr = B;
CELL *env_ptr = ENV;
if (b_ptr == NULL)
return;
do {
PredEntry *pe;
/* check first environments that are younger than our latest choicepoint */
while (b_ptr > (choiceptr)env_ptr) {
PredEntry *pe = EnvPreg((yamop *)env_ptr[E_CP]);
mark_pred(mask, pe);
env_ptr = (CELL *)(env_ptr[E_E]);
}
/* now mark the choicepoint */
if ((b_ptr)) {
if ((pe = PredForChoicePt(b_ptr->cp_ap, NULL))) {
mark_pred(mask, pe);
2015-09-21 23:05:36 +01:00
}
}
env_ptr = b_ptr->cp_env;
b_ptr = b_ptr->cp_b;
} while (b_ptr != NULL);
/* mark or unmark all predicates */
STATIC_PREDICATES_MARKED = mask;
}
static Int toggle_static_predicates_in_use(USES_REGS1) {
2015-09-21 23:05:36 +01:00
#if !defined(YAPOR) && !defined(THREADS)
Term t = Deref(ARG1);
Int mask;
/* find out whether we need to mark or unmark */
if (IsVarTerm(t)) {
Yap_Error(INSTANTIATION_ERROR, t, "toggle_static_predicates_in_use/1");
return (FALSE);
2015-09-21 23:05:36 +01:00
}
if (!IsIntTerm(t)) {
Yap_Error(TYPE_ERROR_INTEGER, t, "toggle_static_predicates_in_use/1");
return (FALSE);
} else {
2015-09-21 23:05:36 +01:00
mask = IntOfTerm(t);
}
do_toggle_static_predicates_in_use(mask);
#endif
return TRUE;
}
#endif /* !defined(YAPOR) && !defined(THREADS) */
static void clause_was_found(PredEntry *pp, Atom *pat, UInt *parity) {
2015-09-21 23:05:36 +01:00
if (pp->ModuleOfPred == IDB_MODULE) {
if (pp->PredFlags & NumberDBPredFlag) {
*parity = 0;
*pat = AtomInteger;
} else if (pp->PredFlags & AtomDBPredFlag) {
2015-09-21 23:05:36 +01:00
*parity = 0;
*pat = (Atom)pp->FunctorOfPred;
} else {
*pat = NameOfFunctor(pp->FunctorOfPred);
*parity = ArityOfFunctor(pp->FunctorOfPred);
}
} else {
2015-09-29 23:08:10 +01:00
if (parity) {
*parity = pp->ArityOfPE;
}
if (pat) {
if (pp->ArityOfPE) {
*pat = NameOfFunctor(pp->FunctorOfPred);
} else {
*pat = (Atom)(pp->FunctorOfPred);
}
}
2015-09-21 23:05:36 +01:00
}
}
static void code_in_pred_info(PredEntry *pp, Atom *pat, UInt *parity) {
2015-09-21 23:05:36 +01:00
clause_was_found(pp, pat, parity);
}
static int code_in_pred_lu_index(LogUpdIndex *icl, yamop *codeptr,
void **startp, void **endp) {
2015-09-21 23:05:36 +01:00
LogUpdIndex *cicl;
if (IN_BLOCK(codeptr, icl, icl->ClSize)) {
if (startp)
*startp = (CODEADDR)icl;
if (endp)
*endp = (CODEADDR)icl + icl->ClSize;
2015-09-21 23:05:36 +01:00
return TRUE;
}
cicl = icl->ChildIndex;
while (cicl != NULL) {
if (code_in_pred_lu_index(cicl, codeptr, startp, endp))
return TRUE;
cicl = cicl->SiblingIndex;
}
return FALSE;
}
static int code_in_pred_s_index(StaticIndex *icl, yamop *codeptr, void **startp,
void **endp) {
2015-09-21 23:05:36 +01:00
StaticIndex *cicl;
if (IN_BLOCK(codeptr, icl, icl->ClSize)) {
if (startp)
*startp = (CODEADDR)icl;
if (endp)
*endp = (CODEADDR)icl + icl->ClSize;
2015-09-21 23:05:36 +01:00
return TRUE;
}
cicl = icl->ChildIndex;
while (cicl != NULL) {
if (code_in_pred_s_index(cicl, codeptr, startp, endp))
return TRUE;
cicl = cicl->SiblingIndex;
}
return FALSE;
}
static Int find_code_in_clause(PredEntry *pp, yamop *codeptr, void **startp,
void **endp) {
2015-09-21 23:05:36 +01:00
Int i = 1;
yamop *clcode;
clcode = pp->cs.p_code.FirstClause;
if (clcode != NULL) {
if (pp->PredFlags & LogUpdatePredFlag) {
LogUpdClause *cl = ClauseCodeToLogUpdClause(clcode);
do {
if (IN_BLOCK(codeptr, (CODEADDR)cl, cl->ClSize)) {
if (startp)
*startp = (CODEADDR)cl;
if (endp)
*endp = (CODEADDR)cl + cl->ClSize;
return i;
}
i++;
cl = cl->ClNext;
2015-09-21 23:05:36 +01:00
} while (cl != NULL);
} else if (pp->PredFlags & DynamicPredFlag) {
do {
DynamicClause *cl;
cl = ClauseCodeToDynamicClause(clcode);
if (IN_BLOCK(codeptr, cl, cl->ClSize)) {
if (startp)
*startp = (CODEADDR)cl;
if (endp)
*endp = (CODEADDR)cl + cl->ClSize;
return i;
}
if (clcode == pp->cs.p_code.LastClause)
break;
i++;
clcode = NextDynamicClause(clcode);
2015-09-21 23:05:36 +01:00
} while (TRUE);
} else if (pp->PredFlags & MegaClausePredFlag) {
MegaClause *cl;
2015-09-21 23:05:36 +01:00
cl = ClauseCodeToMegaClause(clcode);
if (IN_BLOCK(codeptr, cl, cl->ClSize)) {
if (startp)
*startp = (CODEADDR)cl;
if (endp)
*endp = (CODEADDR)cl + cl->ClSize;
return 1 + ((char *)codeptr - (char *)cl->ClCode) / cl->ClItemSize;
2015-09-21 23:05:36 +01:00
}
} else {
StaticClause *cl;
2015-09-21 23:05:36 +01:00
cl = ClauseCodeToStaticClause(clcode);
do {
if (IN_BLOCK(codeptr, cl, cl->ClSize)) {
if (startp)
*startp = (CODEADDR)cl;
if (endp)
*endp = (CODEADDR)cl + cl->ClSize;
return i;
}
if (cl->ClCode == pp->cs.p_code.LastClause)
break;
i++;
cl = cl->ClNext;
2015-09-21 23:05:36 +01:00
} while (TRUE);
}
}
return (0);
2015-09-21 23:05:36 +01:00
}
static Term clause_loc(void *clcode, PredEntry *pp) {
2015-09-21 23:05:36 +01:00
2015-09-29 23:08:10 +01:00
CACHE_REGS
2015-09-21 23:05:36 +01:00
if (pp->PredFlags & LogUpdatePredFlag) {
LogUpdClause *cl = ClauseCodeToLogUpdClause(clcode);
cl = ClauseCodeToLogUpdClause(clcode);
if (cl->ClFlags & FactMask) {
return MkIntegerTerm(cl->lusl.ClLine);
} else {
return MkIntegerTerm(cl->lusl.ClSource->ag.line_number);
}
2015-09-21 23:05:36 +01:00
} else if (pp->PredFlags & DynamicPredFlag) {
DynamicClause *cl;
cl = ClauseCodeToDynamicClause(clcode);
return TermNil;
2015-09-21 23:05:36 +01:00
} else if (pp->PredFlags & MegaClausePredFlag) {
MegaClause *mcl = ClauseCodeToMegaClause(pp->cs.p_code.FirstClause);
return MkIntTerm(mcl->ClLine);
} else {
StaticClause *cl;
cl = ClauseCodeToStaticClause(clcode);
if (cl->ClFlags & FactMask) {
return MkIntTerm(cl->usc.ClLine);
} else if (cl->ClFlags & SrcMask) {
return MkIntTerm(cl->usc.ClSource->ag.line_number);
} else
return TermNil;
2015-09-21 23:05:36 +01:00
}
return TermNil;
}
static int cl_code_in_pred(PredEntry *pp, yamop *codeptr, void **startp,
void **endp) {
2015-09-21 23:05:36 +01:00
Int out;
PELOCK(39, pp);
2015-09-21 23:05:36 +01:00
/* check if the codeptr comes from the indexing code */
if (pp->PredFlags & IndexedPredFlag) {
if (pp->PredFlags & LogUpdatePredFlag) {
if (code_in_pred_lu_index(
ClauseCodeToLogUpdIndex(pp->cs.p_code.TrueCodeOfPred), codeptr,
startp, endp)) {
UNLOCK(pp->PELock);
return TRUE;
2015-09-21 23:05:36 +01:00
}
} else {
if (code_in_pred_s_index(
ClauseCodeToStaticIndex(pp->cs.p_code.TrueCodeOfPred), codeptr,
startp, endp)) {
UNLOCK(pp->PELock);
return TRUE;
2015-09-21 23:05:36 +01:00
}
}
}
if (pp->PredFlags & (CPredFlag | AsmPredFlag | UserCPredFlag)) {
2015-09-21 23:05:36 +01:00
StaticClause *cl = ClauseCodeToStaticClause(pp->CodeOfPred);
if (IN_BLOCK(codeptr, (CODEADDR)cl, cl->ClSize)) {
2015-09-21 23:05:36 +01:00
if (startp)
*startp = (CODEADDR)cl;
2015-09-21 23:05:36 +01:00
if (endp)
*endp = (CODEADDR)cl + cl->ClSize;
2015-09-21 23:05:36 +01:00
UNLOCK(pp->PELock);
return TRUE;
} else {
UNLOCK(pp->PELock);
return FALSE;
}
} else {
out = find_code_in_clause(pp, codeptr, startp, endp);
}
UNLOCK(pp->PELock);
if (out)
return TRUE;
2015-09-21 23:05:36 +01:00
return FALSE;
}
static Int code_in_pred(PredEntry *pp, Atom *pat, UInt *parity,
yamop *codeptr) {
2015-09-21 23:05:36 +01:00
Int out;
PELOCK(40, pp);
2015-09-21 23:05:36 +01:00
/* check if the codeptr comes from the indexing code */
if (pp->PredFlags & IndexedPredFlag) {
if (pp->PredFlags & LogUpdatePredFlag) {
if (code_in_pred_lu_index(
ClauseCodeToLogUpdIndex(pp->cs.p_code.TrueCodeOfPred), codeptr,
NULL, NULL)) {
code_in_pred_info(pp, pat, parity);
UNLOCK(pp->PELock);
return -1;
2015-09-21 23:05:36 +01:00
}
} else {
if (code_in_pred_s_index(
ClauseCodeToStaticIndex(pp->cs.p_code.TrueCodeOfPred), codeptr,
NULL, NULL)) {
code_in_pred_info(pp, pat, parity);
UNLOCK(pp->PELock);
return -1;
2015-09-21 23:05:36 +01:00
}
}
}
if ((out = find_code_in_clause(pp, codeptr, NULL, NULL))) {
clause_was_found(pp, pat, parity);
}
UNLOCK(pp->PELock);
2015-09-21 23:05:36 +01:00
return out;
}
static Int PredForCode(yamop *codeptr, Atom *pat, UInt *parity, Term *pmodule,
PredEntry **pep) {
2015-09-21 23:05:36 +01:00
Int found = 0;
ModEntry *me = CurrentModules;
/* should we allow the user to see hidden predicates? */
while (me) {
PredEntry *pp;
pp = me->PredForME;
while (pp != NULL) {
if ((found = code_in_pred(pp, pat, parity, codeptr)) != 0) {
2015-09-21 23:05:36 +01:00
if (pmodule)
*pmodule = MkAtomTerm(me->AtomOfME);
2015-09-29 23:08:10 +01:00
if (pep)
*pep = pp;
2015-09-21 23:05:36 +01:00
return found;
}
pp = pp->NextPredOfModule;
}
me = me->NextME;
}
return (0);
2015-09-21 23:05:36 +01:00
}
Int Yap_PredForCode(yamop *codeptr, find_pred_type where_from, Atom *pat,
UInt *parity, Term *pmodule) {
2015-09-21 23:05:36 +01:00
PredEntry *p;
if (where_from == FIND_PRED_FROM_CP) {
p = PredForChoicePt(codeptr, NULL);
} else if (where_from == FIND_PRED_FROM_ENV) {
p = EnvPreg(codeptr);
if (p) {
Int out;
if (p->ModuleOfPred == PROLOG_MODULE)
*pmodule = TermProlog;
2015-09-21 23:05:36 +01:00
else
*pmodule = p->ModuleOfPred;
out = find_code_in_clause(p, codeptr, NULL, NULL);
2015-09-21 23:05:36 +01:00
clause_was_found(p, pat, parity);
return out;
}
} else {
2015-09-29 23:08:10 +01:00
return PredForCode(codeptr, pat, parity, pmodule, NULL);
2015-09-21 23:05:36 +01:00
}
if (p == NULL) {
return 0;
}
clause_was_found(p, pat, parity);
if (p->ModuleOfPred == PROLOG_MODULE)
*pmodule = TermProlog;
else
*pmodule = p->ModuleOfPred;
return -1;
}
/* intruction blocks we found ourselves at */
static PredEntry *walk_got_lu_block(LogUpdIndex *cl, void **startp,
void **endp) {
2015-09-21 23:05:36 +01:00
PredEntry *pp = cl->ClPred;
*startp = (CODEADDR)cl;
*endp = (CODEADDR)cl + cl->ClSize;
2015-09-21 23:05:36 +01:00
return pp;
}
/* intruction blocks we found ourselves at */
static PredEntry *walk_got_lu_clause(LogUpdClause *cl, void **startp,
void **endp) {
2015-09-21 23:05:36 +01:00
*startp = (CODEADDR)cl;
*endp = (CODEADDR)cl + cl->ClSize;
2015-09-21 23:05:36 +01:00
return cl->ClPred;
}
/* we hit a meta-call, so we don't know what is happening */
static PredEntry *found_meta_call(void **startp, void **endp) {
2015-09-21 23:05:36 +01:00
PredEntry *pp = PredMetaCall;
*startp = (CODEADDR) & (pp->OpcodeOfPred);
*endp = (CODEADDR)NEXTOP((yamop *)&(pp->OpcodeOfPred), e);
2015-09-21 23:05:36 +01:00
return pp;
}
/* intruction blocks we found ourselves at */
static PredEntry *walk_found_c_pred(PredEntry *pp, void **startp, void **endp) {
StaticClause *cl = ClauseCodeToStaticClause(pp->CodeOfPred);
*startp = (CODEADDR) & (cl->ClCode);
*endp = (CODEADDR) & (cl->ClCode) + cl->ClSize;
2015-09-21 23:05:36 +01:00
return pp;
}
/* we hit a mega-clause, no point in going on */
static PredEntry *found_mega_clause(PredEntry *pp, void **startp, void **endp) {
2015-09-21 23:05:36 +01:00
MegaClause *mcl = ClauseCodeToMegaClause(pp->cs.p_code.FirstClause);
*startp = (CODEADDR)mcl;
*endp = (CODEADDR)mcl + mcl->ClSize;
2015-09-21 23:05:36 +01:00
return pp;
}
/* we hit a mega-clause, no point in going on */
static PredEntry *found_idb_clause(yamop *pc, void **startp, void **endp) {
2015-09-21 23:05:36 +01:00
LogUpdClause *cl = ClauseCodeToLogUpdClause(pc);
*startp = (CODEADDR)cl;
*endp = (CODEADDR)cl + cl->ClSize;
2015-09-21 23:05:36 +01:00
return cl->ClPred;
}
/* we hit a expand_index, no point in going on */
static PredEntry *found_expand_index(yamop *pc, void **startp, void **endp,
yamop *codeptr USES_REGS) {
2015-09-21 23:05:36 +01:00
PredEntry *pp = codeptr->y_u.sssllp.p;
if (pc == codeptr) {
*startp = (CODEADDR)codeptr;
*endp = (CODEADDR)NEXTOP(codeptr, sssllp);
2015-09-21 23:05:36 +01:00
}
return pp;
}
/* we hit a expand_index, no point in going on */
static PredEntry *found_fail(yamop *pc, void **startp, void **endp USES_REGS) {
PredEntry *pp = RepPredProp(Yap_GetPredPropByAtom(AtomFail, CurrentModule));
2015-09-21 23:05:36 +01:00
*startp = *endp = (CODEADDR)FAILCODE;
return pp;
}
/* we hit a expand_index, no point in going on */
static PredEntry *found_owner_op(yamop *pc, void **startp,
void **endp USES_REGS) {
PredEntry *pp = ((PredEntry *)(Unsigned(pc) -
(CELL)(&(((PredEntry *)NULL)->OpcodeOfPred))));
*startp = (CODEADDR) & (pp->OpcodeOfPred);
*endp = (CODEADDR)NEXTOP((yamop *)&(pp->OpcodeOfPred), e);
2015-09-21 23:05:36 +01:00
return pp;
}
/* we hit a expand_index, no point in going on */
static PredEntry *found_expand(yamop *pc, void **startp,
void **endp USES_REGS) {
PredEntry *pp =
((PredEntry *)(Unsigned(pc) -
(CELL)(&(((PredEntry *)NULL)->cs.p_code.ExpandCode))));
*startp = (CODEADDR) & (pp->cs.p_code.ExpandCode);
*endp = (CODEADDR)NEXTOP((yamop *)&(pp->cs.p_code.ExpandCode), e);
2015-09-21 23:05:36 +01:00
return pp;
}
static PredEntry *found_ystop(yamop *pc, int clause_code, void **startp,
void **endp, PredEntry *pp USES_REGS) {
2015-09-21 23:05:36 +01:00
if (pc == YESCODE) {
pp = RepPredProp(Yap_GetPredPropByAtom(AtomTrue, CurrentModule));
2015-09-21 23:05:36 +01:00
*startp = (CODEADDR)YESCODE;
*endp = (CODEADDR)YESCODE + (CELL)(NEXTOP((yamop *)NULL, e));
2015-09-21 23:05:36 +01:00
return pp;
}
if (!pp) {
/* must be an index */
PredEntry **pep = (PredEntry **)pc->y_u.l.l;
pp = pep[-1];
}
if (pp->PredFlags & LogUpdatePredFlag) {
if (clause_code) {
LogUpdClause *cl = ClauseCodeToLogUpdClause(pc->y_u.l.l);
*startp = (CODEADDR)cl;
*endp = (CODEADDR)cl + cl->ClSize;
2015-09-21 23:05:36 +01:00
} else {
LogUpdIndex *cl = ClauseCodeToLogUpdIndex(pc->y_u.l.l);
*startp = (CODEADDR)cl;
*endp = (CODEADDR)cl + cl->ClSize;
2015-09-21 23:05:36 +01:00
}
} else if (pp->PredFlags & DynamicPredFlag) {
DynamicClause *cl = ClauseCodeToDynamicClause(pc->y_u.l.l);
*startp = (CODEADDR)cl;
*endp = (CODEADDR)cl + cl->ClSize;
2015-09-21 23:05:36 +01:00
} else {
if (clause_code) {
StaticClause *cl = ClauseCodeToStaticClause(pc->y_u.l.l);
*startp = (CODEADDR)cl;
*endp = (CODEADDR)cl + cl->ClSize;
2015-09-21 23:05:36 +01:00
} else {
StaticIndex *cl = ClauseCodeToStaticIndex(pc->y_u.l.l);
*startp = (CODEADDR)cl;
*endp = (CODEADDR)cl + cl->ClSize;
2015-09-21 23:05:36 +01:00
}
}
return pp;
}
static PredEntry *ClauseInfoForCode(yamop *codeptr, void **startp,
void **endp USES_REGS) {
2015-09-21 23:05:36 +01:00
yamop *pc;
PredEntry *pp = NULL;
int clause_code = FALSE;
if (codeptr >= COMMA_CODE && codeptr < FAILCODE) {
pp = RepPredProp(Yap_GetPredPropByFunc(FunctorComma, CurrentModule));
2015-09-21 23:05:36 +01:00
*startp = (CODEADDR)COMMA_CODE;
*endp = (CODEADDR)(FAILCODE - 1);
2015-09-21 23:05:36 +01:00
return pp;
}
pc = codeptr;
#include "walkclause.h"
return NULL;
}
PredEntry *Yap_PredEntryForCode(yamop *codeptr, find_pred_type where_from,
void **startp, void **endp) {
2015-09-21 23:05:36 +01:00
CACHE_REGS
if (where_from == FIND_PRED_FROM_CP) {
PredEntry *pp = PredForChoicePt(codeptr, NULL);
if (cl_code_in_pred(pp, codeptr, startp, endp)) {
return pp;
}
} else if (where_from == FIND_PRED_FROM_ENV) {
PredEntry *pp = EnvPreg(codeptr);
if (cl_code_in_pred(pp, codeptr, startp, endp)) {
return pp;
}
} else {
return ClauseInfoForCode(codeptr, startp, endp PASS_REGS);
}
return NULL;
}
/**
2015-09-21 23:05:36 +01:00
* Detect whether the predicate describing the goal in A1,
* module A2 is currently live in the stack.
*
* @param USES_REGS1
*
2015-09-21 23:05:36 +01:00
* @return liveness
*/
static Int in_use(USES_REGS1) { /* '$in_use'(+P,+Mod) */
PredEntry *pe;
Int out;
2015-09-21 23:05:36 +01:00
pe = get_pred(Deref(ARG1), Deref(ARG2), "$in_use");
2015-09-21 23:05:36 +01:00
if (EndOfPAEntr(pe))
return FALSE;
PELOCK(25, pe);
out = Yap_static_in_use(pe, TRUE);
UNLOCKPE(42, pe);
return (out);
2015-09-21 23:05:36 +01:00
}
static Int pred_for_code(USES_REGS1) {
2015-09-21 23:05:36 +01:00
yamop *codeptr;
Atom at;
arity_t arity;
Term tmodule = TermProlog;
Int cl;
Term t = Deref(ARG1);
if (IsVarTerm(t)) {
return FALSE;
} else if (IsApplTerm(t) && FunctorOfTerm(t) == FunctorStaticClause) {
codeptr = Yap_ClauseFromTerm(t)->ClCode;
2015-09-21 23:05:36 +01:00
} else if (IsIntegerTerm(t)) {
codeptr = (yamop *)IntegerOfTerm(t);
2015-09-21 23:05:36 +01:00
} else if (IsDBRefTerm(t)) {
codeptr = (yamop *)DBRefOfTerm(t);
2015-09-21 23:05:36 +01:00
} else {
return FALSE;
}
2015-09-29 23:08:10 +01:00
cl = PredForCode(codeptr, &at, &arity, &tmodule, NULL);
if (!tmodule)
tmodule = TermProlog;
2015-09-21 23:05:36 +01:00
if (cl == 0) {
return Yap_unify(ARG5, MkIntTerm(0));
2015-09-21 23:05:36 +01:00
} else {
return (Yap_unify(ARG2, MkAtomTerm(at)) &&
Yap_unify(ARG3, MkIntegerTerm(arity)) && Yap_unify(ARG4, tmodule) &&
Yap_unify(ARG5, MkIntegerTerm(cl)));
2015-09-21 23:05:36 +01:00
}
}
static LogUpdIndex *find_owner_log_index(LogUpdIndex *cl, yamop *code_p) {
2015-09-21 23:05:36 +01:00
yamop *code_beg = cl->ClCode;
yamop *code_end = (yamop *)((char *)cl + cl->ClSize);
if (code_p >= code_beg && code_p <= code_end) {
return cl;
}
cl = cl->ChildIndex;
while (cl != NULL) {
LogUpdIndex *out;
if ((out = find_owner_log_index(cl, code_p)) != NULL) {
return out;
}
cl = cl->SiblingIndex;
}
return NULL;
}
static StaticIndex *find_owner_static_index(StaticIndex *cl, yamop *code_p) {
2015-09-21 23:05:36 +01:00
yamop *code_beg = cl->ClCode;
yamop *code_end = (yamop *)((char *)cl + cl->ClSize);
if (code_p >= code_beg && code_p <= code_end) {
return cl;
}
cl = cl->ChildIndex;
while (cl != NULL) {
StaticIndex *out;
if ((out = find_owner_static_index(cl, code_p)) != NULL) {
return out;
}
cl = cl->SiblingIndex;
}
return NULL;
}
ClauseUnion *Yap_find_owner_index(yamop *ipc, PredEntry *ap) {
2015-09-21 23:05:36 +01:00
/* we assume we have an owner index */
if (ap->PredFlags & LogUpdatePredFlag) {
LogUpdIndex *cl = ClauseCodeToLogUpdIndex(ap->cs.p_code.TrueCodeOfPred);
return (ClauseUnion *)find_owner_log_index(cl, ipc);
2015-09-21 23:05:36 +01:00
} else {
StaticIndex *cl = ClauseCodeToStaticIndex(ap->cs.p_code.TrueCodeOfPred);
return (ClauseUnion *)find_owner_static_index(cl, ipc);
2015-09-21 23:05:36 +01:00
}
}
static Term all_envs(CELL *env_ptr USES_REGS) {
2015-09-21 23:05:36 +01:00
Term tf = AbsPair(HR);
CELL *start = HR;
CELL *bp = NULL;
2015-09-21 23:05:36 +01:00
/* walk the environment chain */
while (env_ptr) {
bp = HR;
HR += 2;
/* notice that MkIntegerTerm may increase the HReap */
bp[0] = MkIntegerTerm(LCL0 - env_ptr);
if (HR >= ASP - 1024) {
2015-09-21 23:05:36 +01:00
HR = start;
LOCAL_Error_Size = (ASP - 1024) - HR;
2015-09-21 23:05:36 +01:00
while (env_ptr) {
LOCAL_Error_Size += 2;
env_ptr = (CELL *)(env_ptr[E_E]);
2015-09-21 23:05:36 +01:00
}
return 0L;
} else {
bp[1] = AbsPair(HR);
}
env_ptr = (CELL *)(env_ptr[E_E]);
2015-09-21 23:05:36 +01:00
}
bp[1] = TermNil;
return tf;
}
static Term all_cps(choiceptr b_ptr USES_REGS) {
2015-09-21 23:05:36 +01:00
CELL *bp = NULL;
CELL *start = HR;
Term tf = AbsPair(HR);
while (b_ptr) {
bp = HR;
HR += 2;
/* notice that MkIntegerTerm may increase the HReap */
bp[0] = MkIntegerTerm((Int)(LCL0 - (CELL *)b_ptr));
if (HR >= ASP - 1024) {
2015-09-21 23:05:36 +01:00
HR = start;
LOCAL_Error_Size = (ASP - 1024) - HR;
2015-09-21 23:05:36 +01:00
while (b_ptr) {
LOCAL_Error_Size += 2;
b_ptr = b_ptr->cp_b;
2015-09-21 23:05:36 +01:00
}
return 0L;
} else {
bp[1] = AbsPair(HR);
}
b_ptr = b_ptr->cp_b;
}
bp[1] = TermNil;
return tf;
}
static Int p_all_choicepoints(USES_REGS1) {
2015-09-21 23:05:36 +01:00
Term t;
while ((t = all_cps(B PASS_REGS)) == 0L) {
if (!Yap_gcl(LOCAL_Error_Size, 1, ENV, gc_P(P, CP))) {
2015-09-25 10:57:26 +01:00
Yap_Error(RESOURCE_ERROR_STACK, TermNil, "while dumping choicepoints");
2015-09-21 23:05:36 +01:00
return FALSE;
}
}
return Yap_unify(ARG1, t);
2015-09-21 23:05:36 +01:00
}
static Int p_all_envs(USES_REGS1) {
2015-09-21 23:05:36 +01:00
Term t;
while ((t = all_envs(ENV PASS_REGS)) == 0L) {
if (!Yap_gcl(LOCAL_Error_Size, 1, ENV, gc_P(P, CP))) {
2015-09-25 10:57:26 +01:00
Yap_Error(RESOURCE_ERROR_STACK, TermNil, "while dumping environments");
2015-09-21 23:05:36 +01:00
return FALSE;
}
}
return Yap_unify(ARG1, t);
2015-09-21 23:05:36 +01:00
}
Term clause_info(yamop *codeptr, PredEntry *pp) {
CACHE_REGS
2015-09-21 23:05:36 +01:00
Term ts[2];
2015-09-29 23:08:10 +01:00
void *begin;
2015-09-21 23:05:36 +01:00
if (pp->ArityOfPE == 0) {
ts[0] = MkAtomTerm((Atom)pp->FunctorOfPred);
ts[1] = MkIntTerm(0);
} else {
ts[0] = MkAtomTerm(NameOfFunctor(pp->FunctorOfPred));
2015-09-21 23:05:36 +01:00
ts[1] = MkIntegerTerm(pp->ArityOfPE);
}
2015-09-21 23:05:36 +01:00
ts[0] = MkAtomTerm(pp->src.OwnerFile);
Term t1 = Yap_MkApplTerm(FunctorModule, 2, ts);
if (find_code_in_clause(pp, codeptr, &begin, NULL) <= 0 ||
((ts[0] = clause_loc(begin, pp) == TermNil))) {
2015-09-21 23:05:36 +01:00
ts[0] = clause_loc(pp->cs.p_code.FirstClause, pp);
ts[1] = clause_loc(pp->cs.p_code.LastClause, pp);
if (ts[0] == ts[1] && ts[1] != TermNil) {
} else if (ts[1] == TermNil && ts[0] != TermNil)
ts[0] = Yap_MkApplTerm(FunctorMinus, 2, ts);
2015-09-21 23:05:36 +01:00
}
ts[1] = t1;
return Yap_MkApplTerm(FunctorModule, 2, ts);
2015-09-21 23:05:36 +01:00
}
static Term error_culprit(bool internal USES_REGS) {
2015-09-21 23:05:36 +01:00
PredEntry *pe;
// case number 1: Yap_Error called from built-in.
if (internal) {
pe = EnvPreg(CP);
if (pe->PredFlags & HiddenPredFlag)
return clause_info(P, pe);
2015-09-21 23:05:36 +01:00
} else {
CELL *curENV = ENV;
2015-09-21 23:05:36 +01:00
yamop *curCP = CP;
PredEntry *pe = EnvPreg(curCP);
while (curCP != YESCODE) {
if (pe->ModuleOfPred)
return clause_info(P, pe);
2015-09-21 23:05:36 +01:00
curENV = (CELL *)(curENV[E_E]);
curCP = (yamop *)(curENV[E_CP]);
pe = EnvPreg(curCP);
}
}
return TermNil;
}
static Term all_calls(bool internal USES_REGS) {
2015-09-21 23:05:36 +01:00
Term ts[6];
Functor f = Yap_MkFunctor(AtomLocalSp, 6);
2015-09-21 23:05:36 +01:00
// The first argument is key: it tries to
// catch the culprit at the user level,
ts[0] = error_culprit(internal PASS_REGS);
ts[1] = MkAddressTerm(P);
ts[2] = MkAddressTerm(CP);
ts[3] = MkAddressTerm(PP);
if (trueLocalPrologFlag(STACK_DUMP_ON_ERROR_FLAG)) {
ts[4] = all_envs(ENV PASS_REGS);
ts[5] = all_cps(B PASS_REGS);
if (ts[4] == 0L || ts[5] == 0L)
2015-09-21 23:05:36 +01:00
return 0L;
} else {
ts[4] = ts[5] = TermNil;
}
return Yap_MkApplTerm(f, 6, ts);
2015-09-21 23:05:36 +01:00
}
/**
2015-09-21 23:05:36 +01:00
* report the current status of the stacks up to level $N$
*
* @param depth
*
* @return data on the current program counter
2015-09-21 23:05:36 +01:00
*/
Term Yap_all_calls(void) {
2015-09-21 23:05:36 +01:00
CACHE_REGS
return all_calls(true PASS_REGS);
2015-09-21 23:05:36 +01:00
}
static Int current_stack(USES_REGS1) {
2015-09-21 23:05:36 +01:00
Term t;
while ((t = all_calls(false PASS_REGS)) == 0L) {
if (!Yap_gcl(LOCAL_Error_Size, 1, ENV, gc_P(P, CP))) {
2015-09-25 10:57:26 +01:00
Yap_Error(RESOURCE_ERROR_STACK, TermNil, "while dumping stack");
2015-09-21 23:05:36 +01:00
return FALSE;
}
}
return Yap_unify(ARG1, t);
2015-09-21 23:05:36 +01:00
}
#if LOW_PROF
static void add_code_in_lu_index(LogUpdIndex *cl, PredEntry *pp) {
2015-09-21 23:05:36 +01:00
char *code_end = (char *)cl + cl->ClSize;
Yap_inform_profiler_of_clause(cl, code_end, pp, GPROF_LU_INDEX);
cl = cl->ChildIndex;
while (cl != NULL) {
add_code_in_lu_index(cl, pp);
cl = cl->SiblingIndex;
}
}
static void add_code_in_static_index(StaticIndex *cl, PredEntry *pp) {
2015-09-21 23:05:36 +01:00
char *code_end = (char *)cl + cl->ClSize;
Yap_inform_profiler_of_clause(cl, code_end, pp, GPROF_STATIC_INDEX);
cl = cl->ChildIndex;
while (cl != NULL) {
add_code_in_static_index(cl, pp);
cl = cl->SiblingIndex;
}
}
static void add_code_in_pred(PredEntry *pp) {
2015-09-21 23:05:36 +01:00
yamop *clcode;
PELOCK(49, pp);
2015-09-21 23:05:36 +01:00
/* check if the codeptr comes from the indexing code */
/* highly likely this is used for indexing */
Yap_inform_profiler_of_clause(&(pp->OpcodeOfPred), &(pp->OpcodeOfPred) + 1,
pp, GPROF_INIT_OPCODE);
if (pp->PredFlags & (CPredFlag | AsmPredFlag)) {
2015-09-21 23:05:36 +01:00
char *code_end;
StaticClause *cl;
clcode = pp->CodeOfPred;
cl = ClauseCodeToStaticClause(clcode);
code_end = (char *)cl + cl->ClSize;
Yap_inform_profiler_of_clause(cl, code_end, pp, GPROF_INIT_SYSTEM_CODE);
UNLOCK(pp->PELock);
return;
}
Yap_inform_profiler_of_clause(&(pp->cs.p_code.ExpandCode),
&(pp->cs.p_code.ExpandCode) + 1, pp,
GPROF_INIT_EXPAND);
2015-09-21 23:05:36 +01:00
clcode = pp->cs.p_code.TrueCodeOfPred;
if (pp->PredFlags & IndexedPredFlag) {
if (pp->PredFlags & LogUpdatePredFlag) {
LogUpdIndex *cl = ClauseCodeToLogUpdIndex(clcode);
add_code_in_lu_index(cl, pp);
} else {
StaticIndex *cl = ClauseCodeToStaticIndex(clcode);
add_code_in_static_index(cl, pp);
}
}
clcode = pp->cs.p_code.FirstClause;
if (clcode != NULL) {
if (pp->PredFlags & LogUpdatePredFlag) {
LogUpdClause *cl = ClauseCodeToLogUpdClause(clcode);
do {
char *code_end;
2015-09-21 23:05:36 +01:00
code_end = (char *)cl + cl->ClSize;
Yap_inform_profiler_of_clause(cl, code_end, pp,
GPROF_INIT_LOG_UPD_CLAUSE);
cl = cl->ClNext;
2015-09-21 23:05:36 +01:00
} while (cl != NULL);
} else if (pp->PredFlags & DynamicPredFlag) {
do {
DynamicClause *cl;
CODEADDR code_end;
cl = ClauseCodeToDynamicClause(clcode);
code_end = (CODEADDR)cl + cl->ClSize;
Yap_inform_profiler_of_clause(cl, code_end, pp,
GPROF_INIT_DYNAMIC_CLAUSE);
if (clcode == pp->cs.p_code.LastClause)
break;
clcode = NextDynamicClause(clcode);
2015-09-21 23:05:36 +01:00
} while (TRUE);
} else {
StaticClause *cl = ClauseCodeToStaticClause(clcode);
do {
char *code_end;
code_end = (char *)cl + cl->ClSize;
Yap_inform_profiler_of_clause(cl, code_end, pp,
GPROF_INIT_STATIC_CLAUSE);
if (cl->ClCode == pp->cs.p_code.LastClause)
break;
cl = cl->ClNext;
2015-09-21 23:05:36 +01:00
} while (TRUE);
}
}
UNLOCK(pp->PELock);
}
void Yap_dump_code_area_for_profiler(void) {
2015-09-21 23:05:36 +01:00
ModEntry *me = CurrentModules;
while (me) {
PredEntry *pp = me->PredForME;
while (pp != NULL) {
/* if (pp->ArityOfPE) {
fprintf(stderr,"%s/%d %p\n",
RepAtom(NameOfFunctor(pp->FunctorOfPred))->StrOfAE,
pp->ArityOfPE,
pp);
2015-09-21 23:05:36 +01:00
} else {
fprintf(stderr,"%s %p\n",
RepAtom((Atom)(pp->FunctorOfPred))->StrOfAE,
pp);
}*/
2015-09-21 23:05:36 +01:00
add_code_in_pred(pp);
pp = pp->NextPredOfModule;
}
me = me->NextME;
}
Yap_inform_profiler_of_clause(
COMMA_CODE, FAILCODE, RepPredProp(Yap_GetPredPropByFunc(FunctorComma, 0)),
GPROF_INIT_COMMA);
Yap_inform_profiler_of_clause(FAILCODE, FAILCODE + 1,
RepPredProp(Yap_GetPredPropByAtom(AtomFail, 0)),
GPROF_INIT_FAIL);
2015-09-21 23:05:36 +01:00
}
#endif /* LOW_PROF */
static Int program_continuation(USES_REGS1) {
2015-09-21 23:05:36 +01:00
PredEntry *pe = EnvPreg((yamop *)((ENV_Parent(ENV))[E_CP]));
if (pe->ModuleOfPred) {
if (!Yap_unify(ARG1, pe->ModuleOfPred))
2015-09-21 23:05:36 +01:00
return FALSE;
} else {
if (!Yap_unify(ARG1, TermProlog))
2015-09-21 23:05:36 +01:00
return FALSE;
}
if (pe->ArityOfPE) {
if (!Yap_unify(ARG2, MkAtomTerm(NameOfFunctor(pe->FunctorOfPred))))
2015-09-21 23:05:36 +01:00
return FALSE;
if (!Yap_unify(ARG3, MkIntegerTerm(ArityOfFunctor(pe->FunctorOfPred))))
2015-09-21 23:05:36 +01:00
return FALSE;
} else {
if (!Yap_unify(ARG2, MkAtomTerm((Atom)pe->FunctorOfPred)))
2015-09-21 23:05:36 +01:00
return FALSE;
if (!Yap_unify(ARG3, MkIntTerm(0)))
2015-09-21 23:05:36 +01:00
return FALSE;
}
return TRUE;
}
static Term BuildActivePred(PredEntry *ap, CELL *vect) {
2015-09-21 23:05:36 +01:00
CACHE_REGS
arity_t i;
if (!ap->ArityOfPE) {
return MkVarTerm();
}
for (i = 0; i < ap->ArityOfPE; i++) {
Term t = Deref(vect[i]);
if (IsVarTerm(t)) {
CELL *pt = VarOfTerm(t);
/* one stack */
if (pt > HR) {
Term nt = MkVarTerm();
Yap_unify(t, nt);
2015-09-21 23:05:36 +01:00
}
}
}
return Yap_MkApplTerm(ap->FunctorOfPred, ap->ArityOfPE, vect);
}
static int UnifyPredInfo(PredEntry *pe, int start_arg USES_REGS) {
2015-09-21 23:05:36 +01:00
arity_t arity = pe->ArityOfPE;
Term tmod, tname;
if (pe->ModuleOfPred != IDB_MODULE) {
if (pe->ModuleOfPred == PROLOG_MODULE) {
tmod = TermProlog;
} else {
tmod = pe->ModuleOfPred;
}
if (pe->ArityOfPE == 0) {
tname = MkAtomTerm((Atom)pe->FunctorOfPred);
} else {
Functor f = pe->FunctorOfPred;
tname = MkAtomTerm(NameOfFunctor(f));
}
} else {
tmod = pe->ModuleOfPred;
if (pe->PredFlags & NumberDBPredFlag) {
tname = MkIntegerTerm(pe->src.IndxId);
} else if (pe->PredFlags & AtomDBPredFlag) {
tname = MkAtomTerm((Atom)pe->FunctorOfPred);
} else {
Functor f = pe->FunctorOfPred;
tname = MkAtomTerm(NameOfFunctor(f));
}
}
return Yap_unify(XREGS[start_arg], tmod) &&
Yap_unify(XREGS[start_arg + 1], tname) &&
Yap_unify(XREGS[start_arg + 2], MkIntegerTerm(arity));
2015-09-21 23:05:36 +01:00
}
static Int ClauseId(yamop *ipc, PredEntry *pe) {
2015-09-21 23:05:36 +01:00
if (!ipc)
return 0;
return find_code_in_clause(pe, ipc, NULL, NULL);
}
static Int env_info(USES_REGS1) {
CELL *env = LCL0 - IntegerOfTerm(Deref(ARG1));
2015-09-21 23:05:36 +01:00
yamop *env_cp;
Term env_b, taddr;
if (!env)
return FALSE;
env_b = MkIntegerTerm((Int)(LCL0 - (CELL *)env[E_CB]));
2015-09-21 23:05:36 +01:00
env_cp = (yamop *)env[E_CP];
/* pe = PREVOP(env_cp,Osbpp)->y_u.Osbpp.p0; */
taddr = MkIntegerTerm((Int)env);
return Yap_unify(ARG3, MkIntegerTerm((Int)env_cp)) &&
Yap_unify(ARG2, taddr) && Yap_unify(ARG4, env_b);
2015-09-21 23:05:36 +01:00
}
static Int p_cpc_info(USES_REGS1) {
2015-09-21 23:05:36 +01:00
PredEntry *pe;
yamop *ipc = (yamop *)IntegerOfTerm(Deref(ARG1));
pe = PREVOP(ipc, Osbpp)->y_u.Osbpp.p0;
2015-09-21 23:05:36 +01:00
return UnifyPredInfo(pe, 2 PASS_REGS) &&
Yap_unify(ARG5, MkIntegerTerm(ClauseId(ipc, pe)));
2015-09-21 23:05:36 +01:00
}
static Int p_choicepoint_info(USES_REGS1) {
choiceptr cptr = (choiceptr)(LCL0 - IntegerOfTerm(Deref(ARG1)));
2015-09-21 23:05:36 +01:00
PredEntry *pe = NULL;
int go_on = TRUE;
yamop *ipc = cptr->cp_ap;
yamop *ncl = NULL;
Term t = TermNil, taddr;
taddr = MkIntegerTerm((Int)cptr);
while (go_on) {
op_numbers opnum = Yap_op_from_opcode(ipc->opc);
go_on = FALSE;
switch (opnum) {
#ifdef TABLING
case _table_load_answer:
#ifdef LOW_LEVEL_TRACER
pe = LOAD_CP(cptr)->cp_pred_entry;
#else
pe = UndefCode;
#endif
t = MkVarTerm();
break;
case _table_try_answer:
case _table_retry_me:
case _table_trust_me:
case _table_retry:
case _table_trust:
case _table_completion:
#ifdef THREADS_CONSUMER_SHARING
case _table_answer_resolution_completion:
#endif /* THREADS_CONSUMER_SHARING */
#ifdef LOW_LEVEL_TRACER
#ifdef DETERMINISTIC_TABLING
if (IS_DET_GEN_CP(cptr)) {
pe = DET_GEN_CP(cptr)->cp_pred_entry;
t = MkVarTerm();
2015-09-21 23:05:36 +01:00
} else
#endif /* DETERMINISTIC_TABLING */
{
pe = GEN_CP(cptr)->cp_pred_entry;
t = BuildActivePred(pe, (CELL *)(GEN_CP(B) + 1));
2015-09-21 23:05:36 +01:00
}
#else
pe = UndefCode;
t = MkVarTerm();
#endif
break;
case _table_answer_resolution:
#ifdef LOW_LEVEL_TRACER
pe = CONS_CP(cptr)->cp_pred_entry;
#else
pe = UndefCode;
#endif
t = MkVarTerm();
break;
case _trie_trust_var:
case _trie_retry_var:
case _trie_trust_var_in_pair:
case _trie_retry_var_in_pair:
case _trie_trust_val:
case _trie_retry_val:
case _trie_trust_val_in_pair:
case _trie_retry_val_in_pair:
case _trie_trust_atom:
case _trie_retry_atom:
case _trie_trust_atom_in_pair:
case _trie_retry_atom_in_pair:
case _trie_trust_null:
case _trie_retry_null:
case _trie_trust_null_in_pair:
case _trie_retry_null_in_pair:
case _trie_trust_pair:
case _trie_retry_pair:
case _trie_trust_appl:
case _trie_retry_appl:
case _trie_trust_appl_in_pair:
case _trie_retry_appl_in_pair:
case _trie_trust_extension:
case _trie_retry_extension:
case _trie_trust_double:
case _trie_retry_double:
case _trie_trust_longint:
case _trie_retry_longint:
case _trie_trust_gterm:
case _trie_retry_gterm:
pe = UndefCode;
t = MkVarTerm();
break;
#endif /* TABLING */
case _try_logical:
case _retry_logical:
case _trust_logical:
case _count_retry_logical:
case _count_trust_logical:
case _profiled_retry_logical:
case _profiled_trust_logical:
ncl = ipc->y_u.OtaLl.d->ClCode;
pe = ipc->y_u.OtaLl.d->ClPred;
t = BuildActivePred(pe, cptr->cp_args);
break;
case _or_else:
pe = ipc->y_u.Osblp.p0;
ncl = ipc;
t = Yap_MkNewApplTerm(FunctorOr, 2);
break;
case _or_last:
#ifdef YAPOR
pe = ipc->y_u.Osblp.p0;
#else
pe = ipc->y_u.p.p;
#endif
ncl = ipc;
t = Yap_MkNewApplTerm(FunctorOr, 2);
break;
case _retry2:
case _retry3:
case _retry4:
pe = NULL;
t = TermNil;
ipc = NEXTOP(ipc, l);
2015-09-21 23:05:36 +01:00
if (!ncl)
ncl = ipc->y_u.Otapl.d;
2015-09-21 23:05:36 +01:00
go_on = TRUE;
break;
case _jump:
pe = NULL;
t = TermNil;
ipc = ipc->y_u.l.l;
go_on = TRUE;
break;
case _retry_c:
case _retry_userc:
ncl = NEXTOP(ipc, OtapFs);
2015-09-21 23:05:36 +01:00
pe = ipc->y_u.OtapFs.p;
t = BuildActivePred(pe, cptr->cp_args);
break;
case _retry_profiled:
case _count_retry:
pe = NULL;
t = TermNil;
ncl = ipc->y_u.Otapl.d;
ipc = NEXTOP(ipc, p);
2015-09-21 23:05:36 +01:00
go_on = TRUE;
break;
case _retry_me:
case _trust_me:
case _count_retry_me:
case _count_trust_me:
case _profiled_retry_me:
case _profiled_trust_me:
case _retry_and_mark:
case _profiled_retry_and_mark:
case _retry:
case _trust:
if (!ncl)
ncl = ipc->y_u.Otapl.d;
2015-09-21 23:05:36 +01:00
pe = ipc->y_u.Otapl.p;
t = BuildActivePred(pe, cptr->cp_args);
break;
case _retry_exo:
case _retry_all_exo:
ncl = NULL;
pe = ipc->y_u.lp.p;
t = BuildActivePred(pe, cptr->cp_args);
break;
case _Nstop: {
Atom at = AtomLive;
t = MkAtomTerm(at);
pe = RepPredProp(PredPropByAtom(at, CurrentModule));
} break;
2015-09-21 23:05:36 +01:00
case _Ystop:
default:
return FALSE;
}
}
return UnifyPredInfo(pe, 3 PASS_REGS) && Yap_unify(ARG2, taddr) &&
Yap_unify(ARG6, t) &&
Yap_unify(ARG7, MkIntegerTerm(ClauseId(ncl, pe)));
2015-09-21 23:05:36 +01:00
}
static Int /* $parent_pred(Module, Name, Arity) */
parent_pred(USES_REGS1) {
2015-09-21 23:05:36 +01:00
/* This predicate is called from the debugger.
We assume a sequence of the form a -> b */
Atom at;
arity_t arity;
Term module;
2015-09-29 23:08:10 +01:00
if (!PredForCode(P_before_spy, &at, &arity, &module, NULL)) {
2015-09-21 23:05:36 +01:00
return Yap_unify(ARG1, MkIntTerm(0)) &&
Yap_unify(ARG2, MkAtomTerm(AtomMetaCall)) &&
Yap_unify(ARG3, MkIntTerm(0));
}
2015-09-21 23:05:36 +01:00
return Yap_unify(ARG1, MkIntTerm(module)) &&
Yap_unify(ARG2, MkAtomTerm(at)) && Yap_unify(ARG3, MkIntTerm(arity));
2015-09-21 23:05:36 +01:00
}
void Yap_dump_stack(void);
void DumpActiveGoals(CACHE_TYPE1);
2015-09-25 10:57:26 +01:00
static int hidden(Atom);
static int legal_env(CELL *CACHE_TYPE);
2015-09-25 10:57:26 +01:00
#define ONLOCAL(ptr) \
(CellPtr(ptr) > CellPtr(HR) && CellPtr(ptr) < CellPtr(LOCAL_LocalBase))
2015-09-25 10:57:26 +01:00
static int hidden(Atom at) {
2015-09-25 10:57:26 +01:00
AtomEntry *chain;
READ_LOCK(INVISIBLECHAIN.AERWLock);
chain = RepAtom(INVISIBLECHAIN.Entry);
while (!EndOfPAEntr(chain) && AbsAtom(chain) != at)
2015-09-25 10:57:26 +01:00
chain = RepAtom(chain->NextOfAE);
READ_UNLOCK(INVISIBLECHAIN.AERWLock);
if (EndOfPAEntr(chain))
2015-09-25 10:57:26 +01:00
return (FALSE);
return (TRUE);
}
static int legal_env(CELL *ep USES_REGS) {
2015-09-25 10:57:26 +01:00
CELL cp, ps;
PredEntry *pe;
if (!ONLOCAL(ep) || Unsigned(ep) & 3)
2015-09-25 10:57:26 +01:00
return (FALSE);
cp = ep[E_CP];
if (!ONHEAP(cp))
2015-09-25 10:57:26 +01:00
return (FALSE);
ps = *((CELL *)(Addr(cp) - CellSize));
pe = (PredEntry *)(ps - sizeof(OPREG) - sizeof(Prop));
PELOCK(70, pe);
if (!ONHEAP(pe) || Unsigned(pe) & 3 || pe->KindOfPE & 0xff00) {
2015-09-25 10:57:26 +01:00
UNLOCK(pe->PELock);
return (FALSE);
}
UNLOCK(pe->PELock);
return (TRUE);
}
static bool handled_exception(USES_REGS1) {
yamop *pos = NEXTOP(PredDollarCatch->cs.p_code.TrueCodeOfPred, l);
bool found_handler = false;
choiceptr gc_b;
2015-09-25 10:57:26 +01:00
gc_b = B;
while (gc_b) {
yamop *ap = gc_b->cp_ap;
if (ap == NOCODE) {
/* C-code: let they deal with that */
return false;
} else if (ap == pos) {
if (found_handler)
return TRUE; /* we have two handlers */
found_handler = true;
}
gc_b = gc_b->cp_b;
}
/* handled by Top c-code? */
return !found_handler;
2015-09-25 10:57:26 +01:00
}
void Yap_dump_stack(void) {
2015-09-25 10:57:26 +01:00
CACHE_REGS
choiceptr b_ptr = B;
CELL *env_ptr = ENV;
char tp[256];
yamop *ipc = CP;
int max_count = 200;
/* check if handled */
if (handled_exception(PASS_REGS1))
2015-09-25 10:57:26 +01:00
return;
#if DEBUG
fprintf(stderr, "%% YAP regs: P=%p, CP=%p, ASP=%p, H=%p, TR=%p, HeapTop=%p\n",
P, CP, ASP, HR, TR, HeapTop);
fprintf(stderr, "%% YAP mode: %ux\n", (unsigned int)LOCAL_PrologMode);
2015-09-25 10:57:26 +01:00
if (LOCAL_ErrorMessage)
fprintf(stderr, "%% LOCAL_ErrorMessage: %s\n", LOCAL_ErrorMessage);
2015-09-25 10:57:26 +01:00
#endif
if (HR > ASP || HR > LCL0) {
fprintf(stderr, "%% YAP ERROR: Global Collided against Local (%p--%p)\n",
HR, ASP);
} else if (HeapTop > (ADDR)LOCAL_GlobalBase) {
fprintf(stderr,
"%% YAP ERROR: Code Space Collided against Global (%p--%p)\n",
HeapTop, LOCAL_GlobalBase);
2015-09-25 10:57:26 +01:00
} else {
#if !USE_SYSTEM_MALLOC
fprintf(stderr, "%ldKB of Code Space (%p--%p)\n",
(long int)((CELL)HeapTop - (CELL)Yap_HeapBase) / 1024, Yap_HeapBase,
HeapTop);
2015-09-25 10:57:26 +01:00
#if USE_DL_MALLOC
if (Yap_NOfMemoryHoles) {
UInt i;
for (i = 0; i < Yap_NOfMemoryHoles; i++)
fprintf(stderr, " Current hole: %p--%p\n", Yap_MemoryHoles[i].start,
2015-09-25 10:57:26 +01:00
Yap_MemoryHoles[i].end);
}
#endif
#endif
2015-10-09 19:05:36 +01:00
Yap_detect_bug_location(P, FIND_PRED_FROM_ANYWHERE
, 256);
fprintf(stderr, "%%\n%% PC: %s\n", (char *)HR);
2015-10-09 19:05:36 +01:00
Yap_detect_bug_location(CP, FIND_PRED_FROM_ANYWHERE, 256);
fprintf(stderr, "%% Continuation: %s\n", (char *)HR);
fprintf(stderr, "%% %luKB of Global Stack (%p--%p)\n",
(unsigned long int)(sizeof(CELL) * (HR - H0)) / 1024, H0, HR);
fprintf(stderr, "%% %luKB of Local Stack (%p--%p)\n",
(unsigned long int)(sizeof(CELL) * (LCL0 - ASP)) / 1024, ASP, LCL0);
fprintf(stderr, "%% %luKB of Trail (%p--%p)\n",
(unsigned long int)((ADDR)TR - LOCAL_TrailBase) / 1024,
LOCAL_TrailBase, TR);
fprintf(stderr, "%% Performed %ld garbage collections\n",
(unsigned long int)LOCAL_GcCalls);
2015-09-25 10:57:26 +01:00
#if LOW_LEVEL_TRACER
{
extern long long vsc_count;
if (vsc_count) {
#if _WIN32
fprintf(stderr, "Trace Counter at %I64d\n", vsc_count);
2015-09-25 10:57:26 +01:00
#else
fprintf(stderr, "Trace Counter at %lld\n", vsc_count);
2015-09-25 10:57:26 +01:00
#endif
}
}
#endif
fprintf(stderr, "%% All Active Calls and\n");
fprintf(stderr, "%% Goals With Alternatives Open (Global In "
"Use--Local In Use)\n%%\n");
2015-09-25 10:57:26 +01:00
while (b_ptr != NULL) {
while (env_ptr && env_ptr <= (CELL *)b_ptr) {
2015-10-09 19:05:36 +01:00
Yap_detect_bug_location(ipc, FIND_PRED_FROM_ENV, 256);
if (env_ptr == (CELL *)b_ptr && (choiceptr)env_ptr[E_CB] > b_ptr) {
2015-09-25 10:57:26 +01:00
b_ptr = b_ptr->cp_b;
fprintf(stderr, "%% %s\n", tp);
2015-09-25 10:57:26 +01:00
} else {
fprintf(stderr, "%% %s\n", tp);
2015-09-25 10:57:26 +01:00
}
if (!max_count--) {
fprintf(stderr, "%% .....\n");
2015-09-25 10:57:26 +01:00
return;
}
ipc = (yamop *)(env_ptr[E_CP]);
env_ptr = (CELL *)(env_ptr[E_E]);
}
if (b_ptr) {
if (!max_count--) {
fprintf(stderr, "%% .....\n");
2015-09-25 10:57:26 +01:00
return;
}
if (b_ptr->cp_ap && /* tabling */
b_ptr->cp_ap->opc != Yap_opcode(_or_else) &&
b_ptr->cp_ap->opc != Yap_opcode(_or_last) &&
b_ptr->cp_ap->opc != Yap_opcode(_Nstop)) {
/* we can safely ignore ; because there is always an upper env */
2015-10-09 19:05:36 +01:00
Yap_detect_bug_location(b_ptr->cp_ap, FIND_PRED_FROM_CP, 256);
fprintf(stderr, "%% %s (%luKB--%luKB)\n", tp,
(unsigned long int)((b_ptr->cp_h - H0) * sizeof(CELL) / 1024),
(unsigned long int)((ADDR)LCL0 - (ADDR)b_ptr) / 1024);
2015-09-25 10:57:26 +01:00
}
b_ptr = b_ptr->cp_b;
}
}
}
}
void DumpActiveGoals(USES_REGS1) {
2015-09-25 10:57:26 +01:00
/* try to dump active goals */
CELL *ep = YENV; /* and current environment */
2015-09-25 10:57:26 +01:00
choiceptr b_ptr = B;
CELL cp;
PredEntry *pe;
int first = 1;
if (legal_env(YENV PASS_REGS) && YENV < ENV)
2015-09-25 10:57:26 +01:00
ep = YENV;
else if (legal_env(ENV PASS_REGS))
2015-09-25 10:57:26 +01:00
ep = ENV;
while (TRUE) {
if (!ONLOCAL(ep) || (Unsigned(ep) & (sizeof(CELL) - 1)))
break;
cp = ep[E_CP];
if (!ONHEAP(cp) || (Unsigned(cp) & (sizeof(CELL) - 1)))
break;
pe = EnvPreg((yamop *)cp);
if (!ONHEAP(pe) || Unsigned(pe) & (sizeof(CELL) - 1))
break;
PELOCK(71, pe);
if (pe->KindOfPE & 0xff00) {
UNLOCK(pe->PELock);
break;
2015-09-25 10:57:26 +01:00
}
if (pe->PredFlags & (CompiledPredFlag | DynamicPredFlag)) {
Functor f;
UNLOCK(pe->PELock);
f = pe->FunctorOfPred;
if (pe->KindOfPE && hidden(NameOfFunctor(f)))
goto next;
if (first++ == 1)
fprintf(stderr, "Active ancestors:\n");
2015-11-05 15:32:57 +00:00
Term mod = pe->ModuleOfPred;
if (mod == PROLOG_MODULE)
mod = TermProlog;
Term t = Yap_MkNewApplTerm(f, pe->ArityOfPE);
Yap_plwrite(Yap_PredicateIndicator(t,mod),GLOBAL_Stream+2, 0, 0, GLOBAL_MaxPriority);
2015-11-05 15:32:57 +00:00
fputc( '\n', stderr );
} else {
UNLOCK(pe->PELock);
}
next:
ep = (CELL *)ep[E_E];
}
2015-09-25 10:57:26 +01:00
first = 1;
fprintf(stderr, "Active Choice-Points:\n");
while (TRUE) {
PredEntry *pe;
op_numbers opnum;
if (!ONLOCAL(b_ptr) || b_ptr->cp_b == NULL)
break;
fprintf(stderr, "%p ", b_ptr);
pe = Yap_PredForChoicePt(b_ptr, &opnum);
if (opnum == _Nstop) {
fprintf(stderr, " ********** C-Code Interface Boundary ***********\n");
} else {
Functor f;
Term mod = PROLOG_MODULE;
f = pe->FunctorOfPred;
if (pe->ModuleOfPred)
mod = pe->ModuleOfPred;
else
mod = TermProlog;
if (mod != TermProlog && mod != MkAtomTerm(AtomUser)) {
Yap_plwrite(mod,GLOBAL_Stream+2, 0, 0, GLOBAL_MaxPriority);
2015-11-05 15:32:57 +00:00
fputc( ':', stderr );
}
if (mod == IDB_MODULE) {
if (pe->PredFlags & NumberDBPredFlag) {
Int id = pe->src.IndxId;
Yap_plwrite(MkIntegerTerm(id),GLOBAL_Stream+2, 0, 0, GLOBAL_MaxPriority);
} else if (pe->PredFlags & AtomDBPredFlag) {
Atom At = (Atom)pe->FunctorOfPred;
Yap_plwrite(MkAtomTerm(At),GLOBAL_Stream+2, 0, 0, GLOBAL_MaxPriority);
} else {
Functor f = pe->FunctorOfPred;
Atom At = NameOfFunctor(f);
arity_t arity = ArityOfFunctor(f);
int i;
Yap_plwrite(MkAtomTerm(At),GLOBAL_Stream+2, 0, 0, GLOBAL_MaxPriority);
2015-11-05 15:32:57 +00:00
fputc( '(', stderr );
for (i = 0; i < arity; i++) {
if (i > 0)
2015-11-05 15:32:57 +00:00
fputc( ',', stderr );
fputc( '_', stderr );
}
2015-11-05 15:32:57 +00:00
fputc( ')', stderr );
}
2015-11-05 15:32:57 +00:00
fputc( '(', stderr );
Yap_plwrite(b_ptr->cp_a2,GLOBAL_Stream+2, 0, 0, GLOBAL_MaxPriority);
2015-11-05 15:32:57 +00:00
fputc( ')', stderr );
} else if (pe->ArityOfPE == 0) {
Yap_plwrite(MkAtomTerm((Atom)f),GLOBAL_Stream+2, 0, 0, GLOBAL_MaxPriority);
2015-09-25 10:57:26 +01:00
} else {
Int i = 0, arity = pe->ArityOfPE;
if (opnum == _or_last || opnum == _or_else) {
Yap_plwrite(MkAtomTerm(NameOfFunctor(f)),GLOBAL_Stream+2, 0, 0, GLOBAL_MaxPriority);
2015-11-05 15:32:57 +00:00
fputc( '(', stderr );
for (i = 0; i < arity; i++) {
if (i > 0)
2015-11-05 15:32:57 +00:00
fputc( ',', stderr );
fputc( '_', stderr );
}
2015-11-05 15:32:57 +00:00
fputs(") :- ... ( _ ; _ ", stderr);
} else {
Term *args = &(b_ptr->cp_a1);
Yap_plwrite(MkAtomTerm(NameOfFunctor(f)),GLOBAL_Stream+2, 0, 0, GLOBAL_MaxPriority);
2015-11-05 15:32:57 +00:00
fputc( '(', stderr );
for (i = 0; i < arity; i++) {
if (i > 0)
2015-11-05 15:32:57 +00:00
fputc( ',', stderr );
Yap_plwrite(args[i],GLOBAL_Stream+2, 0, 0, GLOBAL_MaxPriority);
2015-11-05 15:32:57 +00:00
}
}
2015-11-05 15:32:57 +00:00
fputc( ')', stderr );
2015-09-25 10:57:26 +01:00
}
2015-11-05 15:32:57 +00:00
fputc( '\n', stderr );
2015-09-25 10:57:26 +01:00
}
b_ptr = b_ptr->cp_b;
}
2015-09-25 10:57:26 +01:00
}
2015-10-09 19:05:36 +01:00
void Yap_detect_bug_location(yamop *yap_pc, int where_from,
int psize) {
Atom pred_name;
2015-09-25 10:57:26 +01:00
UInt pred_arity;
Term pred_module;
Int cl;
if ((cl = Yap_PredForCode(yap_pc, where_from, &pred_name, &pred_arity,
&pred_module)) == 0) {
/* system predicate */
2015-10-09 19:05:36 +01:00
fprintf(stderr, "%s", "meta-call");
2015-09-25 10:57:26 +01:00
} else if (pred_module == 0) {
2015-10-09 19:05:36 +01:00
fprintf(stderr, "in prolog:%s/%lu", RepAtom(pred_name)->StrOfAE,
(unsigned long int)pred_arity);
2015-09-25 10:57:26 +01:00
} else if (cl < 0) {
2015-10-09 19:05:36 +01:00
fprintf(stderr, "%s:%s/%lu", RepAtom(AtomOfTerm(pred_module))->StrOfAE,
2015-09-25 10:57:26 +01:00
RepAtom(pred_name)->StrOfAE, (unsigned long int)pred_arity);
} else {
2015-10-09 19:05:36 +01:00
fprintf(stderr, "%s:%s/%lu at clause %lu",
2015-09-25 10:57:26 +01:00
RepAtom(AtomOfTerm(pred_module))->StrOfAE,
RepAtom(pred_name)->StrOfAE, (unsigned long int)pred_arity,
(unsigned long int)cl);
2015-09-25 10:57:26 +01:00
}
}
static Term build_bug_location(yamop *codeptr, PredEntry *pe) {
2015-09-25 10:57:26 +01:00
CACHE_REGS
2015-09-29 23:08:10 +01:00
Term p[5];
if (pe->ModuleOfPred == PROLOG_MODULE)
p[0] = TermProlog;
else
p[0] = pe->ModuleOfPred;
if (pe->ArityOfPE)
p[1] = MkAtomTerm(NameOfFunctor(pe->FunctorOfPred));
else
p[1] = MkAtomTerm((Atom)pe->FunctorOfPred);
p[2] = MkIntegerTerm(pe->ArityOfPE);
if (pe->src.OwnerFile) {
p[3] = MkAtomTerm(pe->src.OwnerFile);
if (pe->PredFlags & MegaClausePredFlag) {
MegaClause *mcl;
mcl = ClauseCodeToMegaClause(pe->cs.p_code.FirstClause);
p[4] = MkIntegerTerm(mcl->ClLine);
} else {
void *clcode;
if (find_code_in_clause(pe, codeptr, &clcode, NULL) > 0) {
if (pe->PredFlags & LogUpdatePredFlag) {
LogUpdClause *cl = clcode;
if (cl->ClFlags & FactMask) {
p[4] = MkIntegerTerm(cl->lusl.ClLine);
} else {
p[4] = MkIntegerTerm(cl->lusl.ClSource->ag.line_number);
}
} else if (pe->PredFlags & DynamicPredFlag) {
p[4] = MkIntTerm(0);
} else {
StaticClause *cl;
cl = clcode;
if (cl->ClFlags & FactMask) {
p[4] = MkIntTerm(cl->usc.ClLine);
} else if (cl->ClFlags & SrcMask) {
p[4] = MkIntTerm(cl->usc.ClSource->ag.line_number);
} else
p[4] = MkIntTerm(0);
}
2015-09-25 10:57:26 +01:00
}
}
} else if (pe->OpcodeOfPred == UNDEF_OPCODE) {
RESET_VARIABLE(p + 3);
RESET_VARIABLE(p + 4);
} else {
// by default, user_input
p[3] = MkAtomTerm(AtomUserIn);
p[4] = MkIntTerm(0);
}
return Yap_MkApplTerm(Yap_MkFunctor(Yap_LookupAtom("p"), 5), 5, p);
2015-09-29 23:08:10 +01:00
}
Term Yap_pc_location(yamop *pc, choiceptr b_ptr, CELL *env) {
2015-09-29 23:08:10 +01:00
CACHE_REGS
yamop *codeptr = pc;
PredEntry *pe;
if (PP == NULL) {
if (PredForCode(pc, NULL, NULL, NULL, &pe) <= 0)
2015-09-29 23:08:10 +01:00
return TermNil;
} else
pe = PP;
if (pe != NULL
// pe->ModuleOfPred != PROLOG_MODULE &&
2015-09-29 23:08:10 +01:00
// &&!(pe->PredFlags & HiddenPredFlag)
) {
return build_bug_location(codeptr, pe);
2015-09-29 23:08:10 +01:00
}
return TermNil;
}
Term Yap_env_location(yamop *cp, choiceptr b_ptr, CELL *env, Int ignore_first) {
while (true) {
2015-09-29 23:08:10 +01:00
if (b_ptr == NULL || env == NULL)
return TermNil;
PredEntry *pe = EnvPreg(cp);
if (pe == PredTrue)
return TermNil;
if (ignore_first <= 0 && pe
// pe->ModuleOfPred != PROLOG_MODULE &&s
&&
!(pe->PredFlags & HiddenPredFlag)) {
return build_bug_location(cp, pe);
} else {
if (NULL && b_ptr && b_ptr->cp_env < env) {
cp = b_ptr->cp_cp;
env = b_ptr->cp_env;
b_ptr = b_ptr->cp_b;
2015-09-25 10:57:26 +01:00
} else {
cp = (yamop *)env[E_CP];
env = ENV_Parent(env);
}
ignore_first--;
2015-09-25 10:57:26 +01:00
}
2015-09-29 23:08:10 +01:00
}
2015-09-25 10:57:26 +01:00
}
static Int clause_location(USES_REGS1) {
2015-09-29 23:08:10 +01:00
return Yap_unify(Yap_pc_location(P, B, ENV), ARG1) &&
Yap_unify(Yap_env_location(CP, B, ENV, 1), ARG2);
2015-09-29 23:08:10 +01:00
}
2015-09-25 10:57:26 +01:00
static Int ancestor_location(USES_REGS1) {
return Yap_unify(Yap_env_location(CP, B, ENV, 2), ARG1) &&
Yap_unify(Yap_env_location(CP, B, ENV, 3), ARG2);
2015-09-29 23:08:10 +01:00
}
2015-09-25 10:57:26 +01:00
void Yap_InitStInfo(void) {
2015-09-21 23:05:36 +01:00
CACHE_REGS
Term cm = CurrentModule;
Yap_InitCPred("in_use", 2, in_use,
HiddenPredFlag | TestPredFlag | SafePredFlag | SyncPredFlag);
2015-09-29 23:08:10 +01:00
#ifndef THREADS
Yap_InitCPred("toggle_static_predicates_in_use", 0,
toggle_static_predicates_in_use,
HiddenPredFlag | SafePredFlag | SyncPredFlag);
2015-09-29 23:08:10 +01:00
#endif
2015-09-21 23:05:36 +01:00
CurrentModule = HACKS_MODULE;
Yap_InitCPred("current_choicepoints", 1, p_all_choicepoints, 0);
Yap_InitCPred("current_continuations", 1, p_all_envs, 0);
Yap_InitCPred("choicepoint", 7, p_choicepoint_info, 0);
Yap_InitCPred("continuation", 4, env_info, 0);
2015-09-21 23:05:36 +01:00
Yap_InitCPred("cp_to_predicate", 5, p_cpc_info, 0);
CurrentModule = cm;
2015-09-29 23:08:10 +01:00
Yap_InitCPred("current_stack", 1, current_stack, HiddenPredFlag);
Yap_InitCPred("pred_for_code", 5, pred_for_code, HiddenPredFlag);
Yap_InitCPred("parent_pred", 3, parent_pred, HiddenPredFlag | SafePredFlag);
Yap_InitCPred("program_continuation", 3, program_continuation,
HiddenPredFlag | SafePredFlag);
Yap_InitCPred("clause_location", 2, clause_location,
HiddenPredFlag | SafePredFlag);
Yap_InitCPred("ancestor_location", 2, ancestor_location,
HiddenPredFlag | SafePredFlag);
2015-09-21 23:05:36 +01:00
}