lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Luiz Henrique de Figueiredo wrote:
Here's another version, based on code from PiL:
	http://www.lua.org/pil/24.2.3.html

static void dumpstack (lua_State *L, const char *message) {
  int i;
  int top=lua_gettop(L);
  printf("dumpstack -- %s\n",message);
  for (i=1; i<=top; i++) {
    printf("%d\t%s\t",i,luaL_typename(L,i));
    switch (lua_type(L, i)) {
      case LUA_TNUMBER:
        printf("%g\n",lua_tonumber(L,i));
        break;
      case LUA_TSTRING:
        printf("%s\n",lua_tostring(L,i));
        break;
      case LUA_TBOOLEAN:
        printf("%s\n", (lua_toboolean(L, i) ? "true" : "false"));
        break;
      case LUA_TNIL:
        printf("%s\n", "nil");
        break;
      default:
        printf("%p\n",lua_topointer(L,i));
        break;
    }
  }
  printf("dumpstack -- END\n");
}

Sometimes seeing coroutine status is helpful, too:

// For Lua 5.1.1 & 5.0.2
case LUA_TTHREAD:
{
   lua_State *pThread = lua_tothread(L, i);
   if (pThread)
   {
      if (pThread == L)
      {
         printf("running\n");
      }
      else
      {
#if LUA_VER >= 511
         switch (lua_status(pThread))
         {
            case LUA_YIELD: printf("suspended\n"); break;
            case 0:
            {
               lua_Debug ar;
               if (lua_getstack(pThread, 0, &ar) > 0)
                  printf("normal\n");
               else if (lua_gettop(pThread) == 0)
                  printf("dead\n");
               else
                  printf("suspended\n");
            }
            break;
            default: printf("dead\n"); break;
         }
#else
         lua_debug ar;
if ((lua_getstack(pThread, 0, &ar) == 0) && (lua_gettop(pThread) == 0))
            printf("dead\n");
         else
            printf("suspended\n");
#endif
      }
   }
}
break;