从 C 语言中如何打印 Lua 栈的内容?

我的 C 语言程序可能有一个很傻的 bug。在某个特定的地方,Lua 栈中并没有我认为应该存在的值。

为了调试它,我想在程序的某个特定点打印 Lua 栈的内容。但是在此过程中如何不弄乱栈呢?

点赞
用户1424244
用户1424244

这段代码从栈顶向下遍历,并对每个值调用 tostring,打印其结果(如果没有结果,则打印类型名称)。

assert(lua_checkstack(L, 3));
int top = lua_gettop(L);
int bottom = 1;
lua_getglobal(L, "tostring");
for(int i = top; i >= bottom; i--)
{
    lua_pushvalue(L, -1);
    lua_pushvalue(L, i);
    lua_pcall(L, 1, 1, 0);
    const char *str = lua_tostring(L, -1);
    if (str) {
        printf("%s\n", str);
    }else{
        printf("%s\n", luaL_typename(L, i));
    }
    lua_pop(L, 1);
}
lua_pop(L, 1);
2019-11-28 15:38:49
用户90511
用户90511

这个回答是基于 @lhf 在评论中提供的回答稍作修改。

它的优点是它不修改任何堆栈中的值,并且不需要任何额外的空间。

static void dumpstack (lua_State *L) {
  int top=lua_gettop(L);
  for (int 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;
    }
  }
}

如果需要,还可以在LUA_TNUMBER用lua_isinteger(L,i)来区分整数和浮点数。

2019-11-29 02:00:38