summaryrefslogtreecommitdiff
path: root/src/lj_debug.c
diff options
context:
space:
mode:
authorMike Pall <mike>2012-10-01 20:45:30 +0200
committerMike Pall <mike>2012-10-01 20:45:30 +0200
commitfcddd5a3a0269f30df0c0da6e9ff89d3e70d0e2c (patch)
tree4bce654a35af804e16eb55324aa92f9ab550a984 /src/lj_debug.c
parent3ad61689cf11ae73fef5dfe12f7ccb5fe539622e (diff)
downloadluajit-fcddd5a3a0269f30df0c0da6e9ff89d3e70d0e2c.tar.gz
luajit-fcddd5a3a0269f30df0c0da6e9ff89d3e70d0e2c.tar.bz2
luajit-fcddd5a3a0269f30df0c0da6e9ff89d3e70d0e2c.zip
From Lua 5.2: Add luaL_traceback().
Diffstat (limited to 'src/lj_debug.c')
-rw-r--r--src/lj_debug.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/lj_debug.c b/src/lj_debug.c
index 0e26cd7c..4f4c4bb1 100644
--- a/src/lj_debug.c
+++ b/src/lj_debug.c
@@ -543,3 +543,54 @@ LUA_API int lua_getstack(lua_State *L, int level, lua_Debug *ar)
543 } 543 }
544} 544}
545 545
546/* Number of frames for the leading and trailing part of a traceback. */
547#define TRACEBACK_LEVELS1 12
548#define TRACEBACK_LEVELS2 10
549
550LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg,
551 int level)
552{
553 int top = (int)(L->top - L->base);
554 int lim = TRACEBACK_LEVELS1;
555 lua_Debug ar;
556 if (msg) lua_pushfstring(L, "%s\n", msg);
557 lua_pushliteral(L, "stack traceback:");
558 while (lua_getstack(L1, level++, &ar)) {
559 GCfunc *fn;
560 if (level > lim) {
561 if (!lua_getstack(L1, level + TRACEBACK_LEVELS2, &ar)) {
562 level--;
563 } else {
564 lua_pushliteral(L, "\n\t...");
565 lua_getstack(L1, -10, &ar);
566 level = ar.i_ci - TRACEBACK_LEVELS2;
567 }
568 lim = 2147483647;
569 continue;
570 }
571 lua_getinfo(L1, "Snlf", &ar);
572 fn = funcV(L1->top-1); L1->top--;
573 if (isffunc(fn) && !*ar.namewhat)
574 lua_pushfstring(L, "\n\t[builtin#%d]:", fn->c.ffid);
575 else
576 lua_pushfstring(L, "\n\t%s:", ar.short_src);
577 if (ar.currentline > 0)
578 lua_pushfstring(L, "%d:", ar.currentline);
579 if (*ar.namewhat) {
580 lua_pushfstring(L, " in function " LUA_QS, ar.name);
581 } else {
582 if (*ar.what == 'm') {
583 lua_pushliteral(L, " in main chunk");
584 } else if (*ar.what == 'C') {
585 lua_pushfstring(L, " at %p", fn->c.f);
586 } else {
587 lua_pushfstring(L, " in function <%s:%d>",
588 ar.short_src, ar.linedefined);
589 }
590 }
591 if ((int)(L->top - L->base) - top >= 15)
592 lua_concat(L, (int)(L->top - L->base) - top);
593 }
594 lua_concat(L, (int)(L->top - L->base) - top);
595}
596