lua-users home
lua-l archive

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


2013/12/17 Karol Drożak <karoldro@gmail.com>:
> I have some code written in Lua:
> local function f1 ()
>   f0 () -- error
> end
>
> local function f2 ()
>   f1 ()
> end
>
> local function f3 ()
>   f2 ()
> end
>
> function program01 ()
>   f3 ()
> end
>
> And I have call function "program01" from program written in C:
> ...
> lua_getglobal (L, "program01");
> int rv = lua_pcall (L, 0, 0, 0);
> if (rv) {
>   printf ("ERR %s\n", lua_tostring (L, -1));
> }
> ...
>
> How can I trace back execution of this code?

You need to tell lua_pcall to use an error handler. This is done
through the last argument to lua_pcall. See the manual for details.

> Please give me an example.

Instead of:

    lua_getglobal (L, "program01");
    int rv = lua_pcall(L, 0, 0, 0);
    if (rv) { /* ... */; lua_pop(L, 1); /* remove error from the stack */ }

You need to write:

    lua_getglobal(L, "debug");
    lua_getfield(L, -1, "traceback");
    lua_replace(L, -2);
    lua_getglobal (L, "program01");
    int rv = lua_pcall(L, 0, 0, -2);
    if (rv) { /* ... */; lua_pop(L, 1); /* remove error from the stack */ }
    lua_pop(L, 1); /* remove debug.traceback from the stack */