lua-users home
lua-l archive

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

On Mar 4, 2015, at 11:42 PM, Sean Conner <> wrote:

It was thus said that the Great Dirk Laurie once stated:
2015-03-05 3:11 GMT+02:00 Dave Hayden <>:
On Mar 4, 2015, at 5:02 PM, Dave Hayden <> wrote:

Is there some new way of getting a stack trace when lua fails an assert?

 If you mean something like this:

 stack= [@/tmp/x.lua(7):]      [=[C](-1):assert]       [@/tmp/x.lua(25):gamma]
 [@/tmp/x.lua(30):beta] [@/tmp/x.lua(34):alpha] [@/tmp/x.lua(37):]
 [=[C](-1):xpcall]      [@/tmp/x.lua(37):]      [=[C](-1):]

Ah, yep, I meant the call stack, not the data stack. And I meant an api_check() fail, not assert. Sorry for the confusion!

If I'm running in the debugger and hit an api_check fail I can dump the Lua call stack, no problem. But if it's someone else is running the app, they can only give me a C stack trace. Having the Lua stack as well has been extremely useful. This helped me debug a lot of my misuse of the C API (had to make lua_State* L a global in 5.3):

    void luaAssert(int x, const char* file, int line, const char* expr)
        if ( !x )
            if ( L != NULL )
                lua_checkstack(L, 4); // XXX - is that enough space?
                luaL_traceback(L, L, NULL, 0);
                printf("%s\n\n", lua_tostring(L, -1));

             printf("%s:%u: failed assertion `%s'\n", file, line, expr);

Then in luaconf.h (might should be in lua_user.h instead, I suppose):

    extern void pc_luaAssert(int x, const char* file, int line, const char* expr);
    #define luai_apicheck(e) pc_luaAssert(e, __FILE__, __LINE__, #e)