浏览代码

more debug information (still with bug for tag methods...)

Roberto Ierusalimschy 25 年之前
父节点
当前提交
bcdbdaccc3
共有 6 个文件被更改,包括 41 次插入30 次删除
  1. 21 17
      ldebug.c
  2. 7 7
      ldo.c
  3. 3 1
      ldo.h
  4. 5 2
      liolib.c
  5. 2 2
      lobject.c
  6. 3 1
      lvm.c

+ 21 - 17
ldebug.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: ldebug.c,v 1.2 1999/12/23 18:19:57 roberto Exp roberto $
+** $Id: ldebug.c,v 1.3 1999/12/29 16:31:15 roberto Exp roberto $
 ** Debug Interface
 ** Debug Interface
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -45,9 +45,9 @@ int lua_setdebug (lua_State *L, int debug) {
 }
 }
 
 
 
 
-lua_Function lua_stackedfunction (lua_State *L, int level) {
+static lua_Function aux_stackedfunction (lua_State *L, int level, StkId top) {
   int i;
   int i;
-  for (i = (L->top-1)-L->stack; i>=0; i--) {
+  for (i = (top-1)-L->stack; i>=0; i--) {
     if (is_T_MARK(L->stack[i].ttype)) {
     if (is_T_MARK(L->stack[i].ttype)) {
       if (level == 0)
       if (level == 0)
         return L->stack+i;
         return L->stack+i;
@@ -58,10 +58,15 @@ lua_Function lua_stackedfunction (lua_State *L, int level) {
 }
 }
 
 
 
 
-const char *luaG_getname (lua_State *L, const char **name) {
-  lua_Function f = lua_stackedfunction(L, 0);
+lua_Function lua_stackedfunction (lua_State *L, int level) {
+  return aux_stackedfunction(L, level, L->top);
+}
+
+
+static const char *luaG_getname (lua_State *L, const char **name, StkId top) {
+  lua_Function f = aux_stackedfunction(L, 0, top);
   if (f == LUA_NOOBJECT || !hasdebuginfo(L, f) || ttype(f+2) == LUA_T_NIL)
   if (f == LUA_NOOBJECT || !hasdebuginfo(L, f) || ttype(f+2) == LUA_T_NIL)
-    return NULL;  /* no name available */
+    return "";  /* no name available */
   else {
   else {
     int i = (f+2)->value.i;
     int i = (f+2)->value.i;
     if (ttype(f) == LUA_T_LCLMARK)
     if (ttype(f) == LUA_T_LCLMARK)
@@ -69,14 +74,7 @@ const char *luaG_getname (lua_State *L, const char **name) {
     LUA_ASSERT(L, ttype(f) == LUA_T_LMARK, "must be a Lua function");
     LUA_ASSERT(L, ttype(f) == LUA_T_LMARK, "must be a Lua function");
     LUA_ASSERT(L, ttype(&tfvalue(f)->consts[i]) == LUA_T_STRING, "");
     LUA_ASSERT(L, ttype(&tfvalue(f)->consts[i]) == LUA_T_STRING, "");
     *name = tsvalue(&tfvalue(f)->consts[i])->str;
     *name = tsvalue(&tfvalue(f)->consts[i])->str;
-    switch (ttype(f+2)) {
-      case LUA_T_NGLOBAL: return "global";
-      case LUA_T_NLOCAL: return "local";
-      case LUA_T_NDOT: return "field";
-      default:
-        LUA_INTERNALERROR(L, "invalid tag for NAME");
-        return NULL;  /* unreacheable; to avoid warnings */
-    }
+    return luaO_typename(f+2);
   }
   }
 }
 }
 
 
@@ -162,8 +160,14 @@ static int checkfunc (lua_State *L, TObject *o) {
 
 
 
 
 const char *lua_getobjname (lua_State *L, lua_Object o, const char **name) {
 const char *lua_getobjname (lua_State *L, lua_Object o, const char **name) {
-  /* try to find a name for given function */
   GlobalVar *g;
   GlobalVar *g;
+  if (is_T_MARK(ttype(o))) {  /* `o' is an active function? */
+    /* look for caller debug information */
+    const char *kind = luaG_getname(L, name, o);
+    if (*kind) return kind;
+    /* else go through */
+  }
+  /* try to find a name for given function */
   luaA_setnormalized(L->top, o); /* to be used by `checkfunc' */
   luaA_setnormalized(L->top, o); /* to be used by `checkfunc' */
   for (g=L->rootglobal; g; g=g->next) {
   for (g=L->rootglobal; g; g=g->next) {
     if (checkfunc(L, &g->value)) {
     if (checkfunc(L, &g->value)) {
@@ -180,8 +184,8 @@ const char *lua_getobjname (lua_State *L, lua_Object o, const char **name) {
 static void call_index_error (lua_State *L, TObject *o, const char *tp,
 static void call_index_error (lua_State *L, TObject *o, const char *tp,
                               const char *v) {
                               const char *v) {
   const char *name;
   const char *name;
-  const char *kind = luaG_getname(L, &name);
-  if (kind) {  /* is there a name? */
+  const char *kind = luaG_getname(L, &name, L->top);
+  if (*kind) {  /* is there a name? */
     luaL_verror(L, "%.10s `%.30s' is not a %.10s", kind, name, tp);
     luaL_verror(L, "%.10s `%.30s' is not a %.10s", kind, name, tp);
   }
   }
   else {
   else {

+ 7 - 7
ldo.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: ldo.c,v 1.61 1999/12/27 17:33:22 roberto Exp roberto $
+** $Id: ldo.c,v 1.62 1999/12/29 16:31:15 roberto Exp roberto $
 ** Stack and Call structure of Lua
 ** Stack and Call structure of Lua
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -114,8 +114,8 @@ void luaD_lineHook (lua_State *L, int line) {
 }
 }
 
 
 
 
-static void luaD_callHook (lua_State *L, StkId func, lua_CHFunction callhook,
-                           int isreturn) {
+void luaD_callHook (lua_State *L, StkId func, lua_CHFunction callhook,
+                    int isreturn) {
   if (L->allowhooks) {
   if (L->allowhooks) {
     struct C_Lua_Stack oldCLS = L->Cstack;
     struct C_Lua_Stack oldCLS = L->Cstack;
     StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->top;
     StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->top;
@@ -156,6 +156,8 @@ static StkId callC (lua_State *L, lua_CFunction f, StkId base) {
   L->Cstack.num = numarg;
   L->Cstack.num = numarg;
   L->Cstack.lua2C = base;
   L->Cstack.lua2C = base;
   L->Cstack.base = L->top;
   L->Cstack.base = L->top;
+  if (L->callhook)
+    luaD_callHook(L, base-1, L->callhook, 0);
   (*f)(L);  /* do the actual call */
   (*f)(L);  /* do the actual call */
   firstResult = L->Cstack.base;
   firstResult = L->Cstack.base;
   L->Cstack = oldCLS;
   L->Cstack = oldCLS;
@@ -194,8 +196,6 @@ void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults) {
 void luaD_call (lua_State *L, StkId func, int nResults) {
 void luaD_call (lua_State *L, StkId func, int nResults) {
   StkId firstResult;
   StkId firstResult;
   lua_CHFunction callhook = L->callhook;
   lua_CHFunction callhook = L->callhook;
-  if (callhook)
-    luaD_callHook(L, func, callhook, 0);
   retry:  /* for `function' tag method */
   retry:  /* for `function' tag method */
   switch (ttype(func)) {
   switch (ttype(func)) {
     case LUA_T_CPROTO:
     case LUA_T_CPROTO:
@@ -224,10 +224,10 @@ void luaD_call (lua_State *L, StkId func, int nResults) {
         luaG_callerror(L, func);
         luaG_callerror(L, func);
       luaD_openstack(L, func);
       luaD_openstack(L, func);
       *func = *im;  /* tag method is the new function to be called */
       *func = *im;  /* tag method is the new function to be called */
-      goto retry;  /* retry the call (without calling callhook again) */
+      goto retry;   /* retry the call */
     }
     }
   }
   }
-  if (callhook)  /* same hook that was used at entry */
+  if (callhook)  /* same hook that was active at entry */
     luaD_callHook(L, NULL, callhook, 1);  /* `return' hook */
     luaD_callHook(L, NULL, callhook, 1);  /* `return' hook */
   /* adjust the number of results */
   /* adjust the number of results */
   if (nResults == MULT_RET)
   if (nResults == MULT_RET)

+ 3 - 1
ldo.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: ldo.h,v 1.14 1999/12/06 11:41:28 roberto Exp roberto $
+** $Id: ldo.h,v 1.15 1999/12/21 18:04:41 roberto Exp roberto $
 ** Stack and Call structure of Lua
 ** Stack and Call structure of Lua
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -26,6 +26,8 @@
 void luaD_init (lua_State *L, int stacksize);
 void luaD_init (lua_State *L, int stacksize);
 void luaD_adjusttop (lua_State *L, StkId base, int extra);
 void luaD_adjusttop (lua_State *L, StkId base, int extra);
 void luaD_openstack (lua_State *L, StkId pos);
 void luaD_openstack (lua_State *L, StkId pos);
+void luaD_callHook (lua_State *L, StkId func, lua_CHFunction callhook,
+                    int isreturn);
 void luaD_lineHook (lua_State *L, int line);
 void luaD_lineHook (lua_State *L, int line);
 void luaD_call (lua_State *L, StkId func, int nResults);
 void luaD_call (lua_State *L, StkId func, int nResults);
 void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults);
 void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults);

+ 5 - 2
liolib.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: liolib.c,v 1.53 1999/12/27 13:04:53 roberto Exp roberto $
+** $Id: liolib.c,v 1.54 1999/12/28 11:52:49 roberto Exp roberto $
 ** Standard I/O (and system) library
 ** Standard I/O (and system) library
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -546,9 +546,12 @@ static void errorfb (lua_State *L) {
       break;  /* buffer is full */
       break;  /* buffer is full */
     }
     }
     switch (*lua_getobjname(L, func, &name)) {
     switch (*lua_getobjname(L, func, &name)) {
-      case 'g':
+      case 'g':  case 'l':
         sprintf(buff+strlen(buff), "function `%.50s'", name);
         sprintf(buff+strlen(buff), "function `%.50s'", name);
         break;
         break;
+      case 'f':
+        sprintf(buff+strlen(buff), "method `%.50s'", name);
+        break;
       case 't':
       case 't':
         sprintf(buff+strlen(buff), "`%.50s' tag method", name);
         sprintf(buff+strlen(buff), "`%.50s' tag method", name);
         break;
         break;

+ 2 - 2
lobject.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lobject.c,v 1.27 1999/12/14 18:31:20 roberto Exp roberto $
+** $Id: lobject.c,v 1.28 1999/12/23 18:19:57 roberto Exp roberto $
 ** Some generic functions over Lua objects
 ** Some generic functions over Lua objects
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -16,7 +16,7 @@
 const char *const luaO_typenames[] = { /* ORDER LUA_T */
 const char *const luaO_typenames[] = { /* ORDER LUA_T */
     "userdata", "number", "string", "table", "function", "function", "nil",
     "userdata", "number", "string", "table", "function", "function", "nil",
     "function", "function", "function", "function", "function", "function",
     "function", "function", "function", "function", "function", "function",
-    "line", NULL
+    "line", "global", "local", "field", NULL
 };
 };
 
 
 
 

+ 3 - 1
lvm.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lvm.c,v 1.76 1999/12/27 17:33:22 roberto Exp roberto $
+** $Id: lvm.c,v 1.77 1999/12/29 16:31:15 roberto Exp roberto $
 ** Lua virtual machine
 ** Lua virtual machine
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -297,6 +297,8 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf,
   register StkId top;  /* keep top local, for performance */
   register StkId top;  /* keep top local, for performance */
   register const Byte *pc = tf->code;
   register const Byte *pc = tf->code;
   const TObject *consts = tf->consts;
   const TObject *consts = tf->consts;
+  if (L->callhook)
+    luaD_callHook(L, base-1, L->callhook, 0);
   luaD_checkstack(L, (*pc++)+EXTRA_STACK);
   luaD_checkstack(L, (*pc++)+EXTRA_STACK);
   if (*pc < ZEROVARARG)
   if (*pc < ZEROVARARG)
     luaD_adjusttop(L, base, *(pc++));
     luaD_adjusttop(L, base, *(pc++));