从嵌入的lua的C代码打印stacktrace
发布时间:2020-12-15 00:22:57 所属栏目:大数据 来源:网络整理
导读:如果我明白这一点,Lua默认会在发生错误时调用调试库“debug.traceback”. 然而,当将Lua嵌入到C代码中时,如下例所示: Simple Lua API Example 我们只能在堆栈顶部提供错误消息. 即 if (status) { /* If something went wrong,error message is at the top of
如果我明白这一点,Lua默认会在发生错误时调用调试库“debug.traceback”.
然而,当将Lua嵌入到C代码中时,如下例所示: 我们只能在堆栈顶部提供错误消息. 即 if (status) { /* If something went wrong,error message is at the top of */ /* the stack */ fprintf(stderr,"Couldn't load file: %sn",lua_tostring(L,-1)); /* I want to print a stacktrace here. How do I do that? */ exit(1); } 在初始错误后,如何从C打印堆栈跟踪? 解决方法
不,不会. Lua运行时(lua.exe)将会这样做,但Lua库不会自己做.如果您想要使用Lua错误的调用堆栈,则需要生成一个. Lua运行时通过使用 static int traceback (lua_State *L) { if (!lua_isstring(L,1)) /* 'message' not a string? */ return 1; /* keep it intact */ lua_getfield(L,LUA_GLOBALSINDEX,"debug"); if (!lua_istable(L,-1)) { lua_pop(L,1); return 1; } lua_getfield(L,-1,"traceback"); if (!lua_isfunction(L,2); return 1; } lua_pushvalue(L,1); /* pass error message */ lua_pushinteger(L,2); /* skip this function and traceback */ lua_call(L,2,1); /* call debug.traceback */ return 1; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |