Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua & C++ API getting the executing information

Tags:

c++

debugging

lua

In Lua I have a function called utils.debug() and what I would like to do is use it in my Lua code as follows:

function Foo:doSomething
    if (/* something */) then
        print("Success!")
    else
        utils.debug()
    end
end

function Foo:doSomethingElse
    if (/* something else */) then
        print("Awesome!")
    else
        utils.debug()
    end
end

I would like to use it throughout my Lua code to help me debug. As a result, I would like my C++ code to know where in the Lua code the utils.debug() was called from. I looked into lua_Debug and lua_getinfo and they seem pretty close to what I want, but I'm missing a piece:

int MyLua::debug(lua_State* L)
{
    lua_Debug ar;
    lua_getstack(L, 1, &ar);
    lua_getinfo(L, ??????, &ar);

    // print out relevant info from 'ar' 
    // such as in what function it was called, line number, etc
}

Is this what the lua_Debug struct is for or is there another facility or method I should use to do this?

like image 464
Addy Avatar asked Feb 10 '13 16:02

Addy


People also ask

What is Lua used for?

Lua scripting is used to dynamize forms and reports. It's a software workshop that allows you to create real database management applications. NUT allows Applications written in Lua. OpenResty, a web platform based on nginx, supports Lua scripting in different execution phases.

Is Lua same as Python?

Key Difference Between Lua vs PythonThe Lua is based on a multi-paradigm but primarily focus on the scripting language. Python is based on a multi-paradigm but focuses on object-oriented language. The Lua language is using for embedded code in a computer or virtual register-based machine.

Is Lua C or C++?

Lua is embeddable It is also easy to extend programs written in other languages with Lua. Lua has been used to extend programs written not only in C and C++, but also in Java, C#, Smalltalk, Fortran, Ada, Erlang, and even in other scripting languages, such as Perl and Ruby.

Is Lua a real coding language?

Lua (/ˈluːə/ LOO-ə; from Portuguese: lua [ˈlu. (w)ɐ] meaning moon) is a lightweight, high-level, multi-paradigm programming language designed primarily for embedded use in applications.


1 Answers

This is what I use to produce a Lua stack trace:

lua_Debug info;
int level = 0;
while (lua_getstack(l, level, &info)) {
    lua_getinfo(l, "nSl", &info);
    fprintf(stderr, "  [%d] %s:%d -- %s [%s]\n",
        level, info.short_src, info.currentline,
        (info.name ? info.name : "<unknown>"), info.what);
    ++level;
}

See the documentation for lua_getinfo for more info.

like image 120
Rob N Avatar answered Sep 16 '22 20:09

Rob N