|
@@ -1,9 +1,6 @@
|
|
|
/*
|
|
|
-** Error handling and debugging API.
|
|
|
+** Error handling.
|
|
|
** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h
|
|
|
-**
|
|
|
-** Portions taken verbatim or adapted from the Lua interpreter.
|
|
|
-** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
|
|
|
*/
|
|
|
|
|
|
#define lj_err_c
|
|
@@ -11,12 +8,11 @@
|
|
|
|
|
|
#include "lj_obj.h"
|
|
|
#include "lj_err.h"
|
|
|
+#include "lj_debug.h"
|
|
|
#include "lj_str.h"
|
|
|
-#include "lj_tab.h"
|
|
|
#include "lj_func.h"
|
|
|
#include "lj_state.h"
|
|
|
#include "lj_frame.h"
|
|
|
-#include "lj_bc.h"
|
|
|
#include "lj_ff.h"
|
|
|
#include "lj_trace.h"
|
|
|
#include "lj_vm.h"
|
|
@@ -78,355 +74,6 @@ LJ_DATADEF const char *lj_err_allmsg =
|
|
|
#include "lj_errmsg.h"
|
|
|
;
|
|
|
|
|
|
-/* -- Frame and function introspection ------------------------------------ */
|
|
|
-
|
|
|
-static BCPos currentpc(lua_State *L, GCfunc *fn, cTValue *nextframe)
|
|
|
-{
|
|
|
- const BCIns *ins;
|
|
|
- lua_assert(fn->c.gct == ~LJ_TFUNC || fn->c.gct == ~LJ_TTHREAD);
|
|
|
- if (!isluafunc(fn)) { /* Cannot derive a PC for non-Lua functions. */
|
|
|
- return ~(BCPos)0;
|
|
|
- } else if (nextframe == NULL) { /* Lua function on top. */
|
|
|
- void *cf = cframe_raw(L->cframe);
|
|
|
- if (cf == NULL || (char *)cframe_pc(cf) == (char *)cframe_L(cf))
|
|
|
- return ~(BCPos)0;
|
|
|
- ins = cframe_pc(cf); /* Only happens during error/hook handling. */
|
|
|
- } else {
|
|
|
- if (frame_islua(nextframe)) {
|
|
|
- ins = frame_pc(nextframe);
|
|
|
- } else if (frame_iscont(nextframe)) {
|
|
|
- ins = frame_contpc(nextframe);
|
|
|
- } else {
|
|
|
- /* Lua function below errfunc/gc/hook: find cframe to get the PC. */
|
|
|
- void *cf = cframe_raw(L->cframe);
|
|
|
- TValue *f = L->base-1;
|
|
|
- if (cf == NULL)
|
|
|
- return ~(BCPos)0;
|
|
|
- while (f > nextframe) {
|
|
|
- if (frame_islua(f)) {
|
|
|
- f = frame_prevl(f);
|
|
|
- } else {
|
|
|
- if (frame_isc(f))
|
|
|
- cf = cframe_raw(cframe_prev(cf));
|
|
|
- f = frame_prevd(f);
|
|
|
- }
|
|
|
- }
|
|
|
- if (cframe_prev(cf))
|
|
|
- cf = cframe_raw(cframe_prev(cf));
|
|
|
- ins = cframe_pc(cf);
|
|
|
- }
|
|
|
- }
|
|
|
- return proto_bcpos(funcproto(fn), ins) - 1;
|
|
|
-}
|
|
|
-
|
|
|
-static BCLine currentline(lua_State *L, GCfunc *fn, cTValue *nextframe)
|
|
|
-{
|
|
|
- BCPos pc = currentpc(L, fn, nextframe);
|
|
|
- if (pc != ~(BCPos)0) {
|
|
|
- GCproto *pt = funcproto(fn);
|
|
|
- lua_assert(pc < pt->sizebc);
|
|
|
- return proto_line(pt, pc);
|
|
|
- } else {
|
|
|
- return -1;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-static const char *getvarname(const GCproto *pt, BCPos pc, BCReg slot)
|
|
|
-{
|
|
|
- MSize i;
|
|
|
- VarInfo *vi = proto_varinfo(pt);
|
|
|
- for (i = 0; i < pt->sizevarinfo && vi[i].startpc <= pc; i++)
|
|
|
- if (pc < vi[i].endpc && slot-- == 0)
|
|
|
- return strdata(gco2str(gcref(vi[i].name)));
|
|
|
- return NULL;
|
|
|
-}
|
|
|
-
|
|
|
-static const char *getobjname(GCproto *pt, const BCIns *ip, BCReg slot,
|
|
|
- const char **name)
|
|
|
-{
|
|
|
- const char *lname;
|
|
|
-restart:
|
|
|
- lname = getvarname(pt, proto_bcpos(pt, ip), slot);
|
|
|
- if (lname != NULL) { *name = lname; return "local"; }
|
|
|
- while (--ip > proto_bc(pt)) {
|
|
|
- BCIns ins = *ip;
|
|
|
- BCOp op = bc_op(ins);
|
|
|
- BCReg ra = bc_a(ins);
|
|
|
- if (bcmode_a(op) == BCMbase) {
|
|
|
- if (slot >= ra && (op != BC_KNIL || slot <= bc_d(ins)))
|
|
|
- return NULL;
|
|
|
- } else if (bcmode_a(op) == BCMdst && ra == slot) {
|
|
|
- switch (bc_op(ins)) {
|
|
|
- case BC_MOV:
|
|
|
- if (ra == slot) { slot = bc_d(ins); goto restart; }
|
|
|
- break;
|
|
|
- case BC_GGET:
|
|
|
- *name = strdata(gco2str(proto_kgc(pt, ~(ptrdiff_t)bc_d(ins))));
|
|
|
- return "global";
|
|
|
- case BC_TGETS:
|
|
|
- *name = strdata(gco2str(proto_kgc(pt, ~(ptrdiff_t)bc_c(ins))));
|
|
|
- if (ip > proto_bc(pt)) {
|
|
|
- BCIns insp = ip[-1];
|
|
|
- if (bc_op(insp) == BC_MOV && bc_a(insp) == ra+1 &&
|
|
|
- bc_d(insp) == bc_b(ins))
|
|
|
- return "method";
|
|
|
- }
|
|
|
- return "field";
|
|
|
- case BC_UGET:
|
|
|
- *name = strdata(proto_uvname(pt, bc_d(ins)));
|
|
|
- return "upvalue";
|
|
|
- default:
|
|
|
- return NULL;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return NULL;
|
|
|
-}
|
|
|
-
|
|
|
-static const char *getfuncname(lua_State *L, TValue *frame, const char **name)
|
|
|
-{
|
|
|
- MMS mm;
|
|
|
- const BCIns *ip;
|
|
|
- TValue *pframe;
|
|
|
- GCfunc *fn;
|
|
|
- BCPos pc;
|
|
|
- if (frame_isvarg(frame))
|
|
|
- frame = frame_prevd(frame);
|
|
|
- pframe = frame_prev(frame);
|
|
|
- fn = frame_func(pframe);
|
|
|
- pc = currentpc(L, fn, frame);
|
|
|
- if (pc == ~(BCPos)0)
|
|
|
- return NULL;
|
|
|
- lua_assert(pc < funcproto(fn)->sizebc);
|
|
|
- ip = &proto_bc(funcproto(fn))[pc];
|
|
|
- mm = bcmode_mm(bc_op(*ip));
|
|
|
- if (mm == MM_call) {
|
|
|
- BCReg slot = bc_a(*ip);
|
|
|
- if (bc_op(*ip) == BC_ITERC) slot -= 3;
|
|
|
- return getobjname(funcproto(fn), ip, slot, name);
|
|
|
- } else if (mm != MM__MAX) {
|
|
|
- *name = strdata(mmname_str(G(L), mm));
|
|
|
- return "metamethod";
|
|
|
- } else {
|
|
|
- return NULL;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void lj_err_pushloc(lua_State *L, GCproto *pt, BCPos pc)
|
|
|
-{
|
|
|
- GCstr *name = proto_chunkname(pt);
|
|
|
- if (name) {
|
|
|
- const char *s = strdata(name);
|
|
|
- MSize i, len = name->len;
|
|
|
- BCLine line = pc < pt->sizebc ? proto_line(pt, pc) : 0;
|
|
|
- if (*s == '@') {
|
|
|
- s++; len--;
|
|
|
- for (i = len; i > 0; i--)
|
|
|
- if (s[i] == '/' || s[i] == '\\') {
|
|
|
- s += i+1;
|
|
|
- break;
|
|
|
- }
|
|
|
- lj_str_pushf(L, "%s:%d", s, line);
|
|
|
- } else if (len > 40) {
|
|
|
- lj_str_pushf(L, "%p:%d", pt, line);
|
|
|
- } else if (*s == '=') {
|
|
|
- lj_str_pushf(L, "%s:%d", s+1, line);
|
|
|
- } else {
|
|
|
- lj_str_pushf(L, "\"%s\":%d", s, line);
|
|
|
- }
|
|
|
- } else {
|
|
|
- lj_str_pushf(L, "%p:%u", pt, pc);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-static void err_chunkid(char *out, const char *src)
|
|
|
-{
|
|
|
- if (*src == '=') {
|
|
|
- strncpy(out, src+1, LUA_IDSIZE); /* remove first char */
|
|
|
- out[LUA_IDSIZE-1] = '\0'; /* ensures null termination */
|
|
|
- } else if (*src == '@') { /* out = "source", or "...source" */
|
|
|
- size_t l = strlen(++src); /* skip the `@' */
|
|
|
- if (l >= LUA_IDSIZE) {
|
|
|
- src += l-(LUA_IDSIZE-4); /* get last part of file name */
|
|
|
- strcpy(out, "...");
|
|
|
- out += 3;
|
|
|
- }
|
|
|
- strcpy(out, src);
|
|
|
- } else { /* out = [string "string"] */
|
|
|
- size_t len; /* Length, up to first control char. */
|
|
|
- for (len = 0; len < LUA_IDSIZE-12; len++)
|
|
|
- if (((const unsigned char *)src)[len] < ' ') break;
|
|
|
- strcpy(out, "[string \""); out += 9;
|
|
|
- if (src[len] != '\0') { /* must truncate? */
|
|
|
- if (len > LUA_IDSIZE-15) len = LUA_IDSIZE-15;
|
|
|
- strncpy(out, src, len); out += len;
|
|
|
- strcpy(out, "..."); out += 3;
|
|
|
- } else {
|
|
|
- strcpy(out, src); out += len;
|
|
|
- }
|
|
|
- strcpy(out, "\"]");
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-/* -- Public debug API ---------------------------------------------------- */
|
|
|
-
|
|
|
-static TValue *findlocal(lua_State *L, const lua_Debug *ar,
|
|
|
- const char **name, BCReg slot)
|
|
|
-{
|
|
|
- uint32_t offset = (uint32_t)ar->i_ci & 0xffff;
|
|
|
- uint32_t size = (uint32_t)ar->i_ci >> 16;
|
|
|
- TValue *frame = tvref(L->stack) + offset;
|
|
|
- TValue *nextframe = size ? frame + size : NULL;
|
|
|
- GCfunc *fn = frame_func(frame);
|
|
|
- BCPos pc = currentpc(L, fn, nextframe);
|
|
|
- if (pc != ~(BCPos)0 &&
|
|
|
- (*name = getvarname(funcproto(fn), pc, slot-1)) != NULL)
|
|
|
- ;
|
|
|
- else if (slot > 0 && frame + slot < (nextframe ? nextframe : L->top))
|
|
|
- *name = "(*temporary)";
|
|
|
- else
|
|
|
- *name = NULL;
|
|
|
- return frame+slot;
|
|
|
-}
|
|
|
-
|
|
|
-LUA_API const char *lua_getlocal(lua_State *L, const lua_Debug *ar, int n)
|
|
|
-{
|
|
|
- const char *name;
|
|
|
- TValue *o = findlocal(L, ar, &name, (BCReg)n);
|
|
|
- if (name) {
|
|
|
- copyTV(L, L->top, o);
|
|
|
- incr_top(L);
|
|
|
- }
|
|
|
- return name;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-LUA_API const char *lua_setlocal(lua_State *L, const lua_Debug *ar, int n)
|
|
|
-{
|
|
|
- const char *name;
|
|
|
- TValue *o = findlocal(L, ar, &name, (BCReg)n);
|
|
|
- if (name)
|
|
|
- copyTV(L, o, L->top-1);
|
|
|
- L->top--;
|
|
|
- return name;
|
|
|
-}
|
|
|
-
|
|
|
-LUA_API int lua_getinfo(lua_State *L, const char *what, lua_Debug *ar)
|
|
|
-{
|
|
|
- int status = 1;
|
|
|
- TValue *frame = NULL;
|
|
|
- TValue *nextframe = NULL;
|
|
|
- GCfunc *fn;
|
|
|
- if (*what == '>') {
|
|
|
- TValue *func = L->top - 1;
|
|
|
- api_check(L, tvisfunc(func));
|
|
|
- fn = funcV(func);
|
|
|
- L->top--;
|
|
|
- what++;
|
|
|
- } else {
|
|
|
- uint32_t offset = (uint32_t)ar->i_ci & 0xffff;
|
|
|
- uint32_t size = (uint32_t)ar->i_ci >> 16;
|
|
|
- lua_assert(offset != 0);
|
|
|
- frame = tvref(L->stack) + offset;
|
|
|
- if (size) nextframe = frame + size;
|
|
|
- lua_assert(frame <= tvref(L->maxstack) &&
|
|
|
- (!nextframe || nextframe <= tvref(L->maxstack)));
|
|
|
- fn = frame_func(frame);
|
|
|
- lua_assert(fn->c.gct == ~LJ_TFUNC);
|
|
|
- }
|
|
|
- for (; *what; what++) {
|
|
|
- switch (*what) {
|
|
|
- case 'S':
|
|
|
- if (isluafunc(fn)) {
|
|
|
- GCproto *pt = funcproto(fn);
|
|
|
- ar->source = strdata(proto_chunkname(pt));
|
|
|
- ar->linedefined = (int)proto_line(pt, 0);
|
|
|
- ar->lastlinedefined = (int)pt->lastlinedefined;
|
|
|
- ar->what = (ar->linedefined == 0) ? "main" : "Lua";
|
|
|
- } else {
|
|
|
- ar->source = "=[C]";
|
|
|
- ar->linedefined = -1;
|
|
|
- ar->lastlinedefined = -1;
|
|
|
- ar->what = "C";
|
|
|
- }
|
|
|
- err_chunkid(ar->short_src, ar->source);
|
|
|
- break;
|
|
|
- case 'l':
|
|
|
- ar->currentline = frame ? currentline(L, fn, nextframe) : -1;
|
|
|
- break;
|
|
|
- case 'u':
|
|
|
- ar->nups = fn->c.nupvalues;
|
|
|
- break;
|
|
|
- case 'n':
|
|
|
- ar->namewhat = frame ? getfuncname(L, frame, &ar->name) : NULL;
|
|
|
- if (ar->namewhat == NULL) {
|
|
|
- ar->namewhat = "";
|
|
|
- ar->name = NULL;
|
|
|
- }
|
|
|
- break;
|
|
|
- case 'f':
|
|
|
- setfuncV(L, L->top, fn);
|
|
|
- incr_top(L);
|
|
|
- break;
|
|
|
- case 'L':
|
|
|
- if (isluafunc(fn)) {
|
|
|
- GCtab *t = lj_tab_new(L, 0, 0);
|
|
|
- GCproto *pt = funcproto(fn);
|
|
|
- BCLine *lineinfo = proto_lineinfo(pt);
|
|
|
- MSize i, szl = pt->sizebc;
|
|
|
- for (i = 1; i < szl; i++)
|
|
|
- setboolV(lj_tab_setint(L, t, lineinfo[i]), 1);
|
|
|
- settabV(L, L->top, t);
|
|
|
- } else {
|
|
|
- setnilV(L->top);
|
|
|
- }
|
|
|
- incr_top(L);
|
|
|
- break;
|
|
|
- default:
|
|
|
- status = 0; /* Bad option. */
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- return status;
|
|
|
-}
|
|
|
-
|
|
|
-cTValue *lj_err_getframe(lua_State *L, int level, int *size)
|
|
|
-{
|
|
|
- cTValue *frame, *nextframe, *bot = tvref(L->stack);
|
|
|
- /* Traverse frames backwards. */
|
|
|
- for (nextframe = frame = L->base-1; frame > bot; ) {
|
|
|
- if (frame_gc(frame) == obj2gco(L))
|
|
|
- level++; /* Skip dummy frames. See lj_meta_call(). */
|
|
|
- if (level-- == 0) {
|
|
|
- *size = (int)(nextframe - frame);
|
|
|
- return frame; /* Level found. */
|
|
|
- }
|
|
|
- nextframe = frame;
|
|
|
- if (frame_islua(frame)) {
|
|
|
- frame = frame_prevl(frame);
|
|
|
- } else {
|
|
|
- if (frame_isvarg(frame))
|
|
|
- level++; /* Skip vararg pseudo-frame. */
|
|
|
- frame = frame_prevd(frame);
|
|
|
- }
|
|
|
- }
|
|
|
- *size = level;
|
|
|
- return NULL; /* Level not found. */
|
|
|
-}
|
|
|
-
|
|
|
-LUA_API int lua_getstack(lua_State *L, int level, lua_Debug *ar)
|
|
|
-{
|
|
|
- int size;
|
|
|
- cTValue *frame = lj_err_getframe(L, level, &size);
|
|
|
- if (frame) {
|
|
|
- ar->i_ci = (size << 16) + (int)(frame - tvref(L->stack));
|
|
|
- return 1;
|
|
|
- } else {
|
|
|
- ar->i_ci = level - size;
|
|
|
- return 0;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
/* -- Internal frame unwinding -------------------------------------------- */
|
|
|
|
|
|
/* Unwind Lua stack and move error message to new top. */
|
|
@@ -854,25 +501,6 @@ LJ_NOINLINE void lj_err_run(lua_State *L)
|
|
|
lj_err_throw(L, LUA_ERRRUN);
|
|
|
}
|
|
|
|
|
|
-/* Add location to error message. */
|
|
|
-LJ_NOINLINE static void err_loc(lua_State *L, const char *msg,
|
|
|
- cTValue *frame, cTValue *nextframe)
|
|
|
-{
|
|
|
- if (frame) {
|
|
|
- GCfunc *fn = frame_func(frame);
|
|
|
- if (isluafunc(fn)) {
|
|
|
- char buff[LUA_IDSIZE];
|
|
|
- BCLine line = currentline(L, fn, nextframe);
|
|
|
- if (line >= 0) {
|
|
|
- err_chunkid(buff, strdata(proto_chunkname(funcproto(fn))));
|
|
|
- lj_str_pushf(L, "%s:%d: %s", buff, line, msg);
|
|
|
- return;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- lj_str_pushf(L, "%s", msg);
|
|
|
-}
|
|
|
-
|
|
|
/* Formatted runtime error message. */
|
|
|
LJ_NORET LJ_NOINLINE static void err_msgv(lua_State *L, ErrMsg em, ...)
|
|
|
{
|
|
@@ -882,7 +510,7 @@ LJ_NORET LJ_NOINLINE static void err_msgv(lua_State *L, ErrMsg em, ...)
|
|
|
if (curr_funcisL(L)) L->top = curr_topL(L);
|
|
|
msg = lj_str_pushvf(L, err2msg(em), argp);
|
|
|
va_end(argp);
|
|
|
- err_loc(L, msg, L->base-1, NULL);
|
|
|
+ lj_debug_addloc(L, msg, L->base-1, NULL);
|
|
|
lj_err_run(L);
|
|
|
}
|
|
|
|
|
@@ -898,7 +526,7 @@ LJ_NOINLINE void lj_err_lex(lua_State *L, const char *src, const char *tok,
|
|
|
{
|
|
|
char buff[LUA_IDSIZE];
|
|
|
const char *msg;
|
|
|
- err_chunkid(buff, src);
|
|
|
+ lj_debug_shortname(buff, src);
|
|
|
msg = lj_str_pushvf(L, err2msg(em), argp);
|
|
|
msg = lj_str_pushf(L, "%s:%d: %s", buff, line, msg);
|
|
|
if (tok)
|
|
@@ -910,12 +538,12 @@ LJ_NOINLINE void lj_err_lex(lua_State *L, const char *src, const char *tok,
|
|
|
LJ_NOINLINE void lj_err_optype(lua_State *L, cTValue *o, ErrMsg opm)
|
|
|
{
|
|
|
const char *tname = typename(o);
|
|
|
- const char *oname = NULL;
|
|
|
const char *opname = err2msg(opm);
|
|
|
if (curr_funcisL(L)) {
|
|
|
GCproto *pt = curr_proto(L);
|
|
|
const BCIns *pc = cframe_Lpc(L) - 1;
|
|
|
- const char *kind = getobjname(pt, pc, (BCReg)(o - L->base), &oname);
|
|
|
+ const char *oname = NULL;
|
|
|
+ const char *kind = lj_debug_slotname(pt, pc, (BCReg)(o-L->base), &oname);
|
|
|
if (kind)
|
|
|
err_msgv(L, LJ_ERR_BADOPRT, opname, kind, oname, tname);
|
|
|
}
|
|
@@ -967,7 +595,7 @@ LJ_NOINLINE void lj_err_callermsg(lua_State *L, const char *msg)
|
|
|
}
|
|
|
#endif
|
|
|
}
|
|
|
- err_loc(L, msg, pframe, frame);
|
|
|
+ lj_debug_addloc(L, msg, pframe, frame);
|
|
|
lj_err_run(L);
|
|
|
}
|
|
|
|
|
@@ -993,7 +621,7 @@ LJ_NORET LJ_NOINLINE static void err_argmsg(lua_State *L, int narg,
|
|
|
const char *msg)
|
|
|
{
|
|
|
const char *fname = "?";
|
|
|
- const char *ftype = getfuncname(L, L->base - 1, &fname);
|
|
|
+ const char *ftype = lj_debug_funcname(L, L->base - 1, &fname);
|
|
|
if (narg < 0 && narg > LUA_REGISTRYINDEX)
|
|
|
narg = (int)(L->top - L->base) + narg + 1;
|
|
|
if (ftype && ftype[3] == 'h' && --narg == 0) /* Check for "method". */
|
|
@@ -1066,8 +694,8 @@ LUALIB_API int luaL_typerror(lua_State *L, int narg, const char *xname)
|
|
|
LUALIB_API void luaL_where(lua_State *L, int level)
|
|
|
{
|
|
|
int size;
|
|
|
- cTValue *frame = lj_err_getframe(L, level, &size);
|
|
|
- err_loc(L, "", frame, size ? frame+size : NULL);
|
|
|
+ cTValue *frame = lj_debug_frame(L, level, &size);
|
|
|
+ lj_debug_addloc(L, "", frame, size ? frame+size : NULL);
|
|
|
}
|
|
|
|
|
|
LUALIB_API int luaL_error(lua_State *L, const char *fmt, ...)
|