LUA_GETINFO(3) Library Functions Manual LUA_GETINFO(3)

lua_getinforeturns information about a specific function or function invocation, function indicator [- (0|1), + Pq 0|1|2 , m]

#include <lua.h>

int
lua_getinfo(lua_State *L, const char *what, lua_Debug *ar);

() returns information about a specific function or function invocation.

To get information about a function invocation, the parameter ar must be a valid activation record that was filled by a previous call to lua_getstack or given as argument to a hook (see lua_Hook(3)).

To get information about a function you push it onto the stack and start the what string with the character '>'. (In that case, lua_getinfo pops the function in the top of the stack.) For instance, to know in which line a function f was defined, you can write the following code:

lua_Debug ar;
lua_getfield(L, LUA_GLOBALSINDEX, "f");  /* get global 'f' */
lua_getinfo(L, ">S", &ar);
printf("%d\n", ar.linedefined);

Each character in the string what selects some fields of the structure ar to be filled or a value to be pushed on the stack:

fills in the field name and namewhat;
fills in the fields source, short_src, linedefined, lastlinedefined, and what;
fills in the field currentline;
fills in the field nups;
pushes onto the stack the function that is running at the given level;
pushes onto the stack a table whose indices are the numbers of the lines that are valid on the function. (A valid line is a line with some associated code, that is, a line where you can put a break point. Non-valid lines include empty lines and comments.) This function returns 0 on error (for instance, an invalid option in what).

lua_Hook(3)

Roberto Ierusalimschy, Luiz Henrique de Figueiredo, and Waldemar Celes, Lua 5.1 Reference Manual.

The lua_getinfo() manual page is based on Lua Reference Manual 5.1 and was created by Sergey Bronnikov.

July 19, 2022 Debian