debug.getinfo

Returns a table with information about a function

Prototype

t = debug.getinfo (thread, f, what)

Description

Returns a table with information about a function. The optional field "what" is a string indicating which values to return (can be more than one). If omitted, all is returned.

The thread argument is optional and defaults to the current thread.

What fields can be one or more of the following concatenated together: The function name can be an integer representing the stack level, where 0 is debug.getinfo itself, 1 is the function that called debug.getinfo, 2 is the function that called that, and so on. Returns nil if the function number is larger than the number of functions on the stack.

Field meanings of the returned table are:

t = debug.getinfo (table.sort)
table.foreach (t, print)

 -->

source =[C]
what C
func function: 02061360
short_src [C]
currentline -1
namewhat 
linedefined -1
nups 0

-- another example, this time a user-defined function --

function f (a, b, c) print "hi" end
t = debug.getinfo (f, "flnSu")  --> 'what' options explicitly mentioned
table.foreach (t, print)

 -->
 
source  =stdin
what    Lua
func    function: 0x8079a40
name    f
nups    0
currentline     -1
namewhat        global
linedefined     1
short_src       stdin

-- this example prints the name of the currently-running function --

function myfunc () print (debug.getinfo (1, "n").name) end
myfunc ()  --> myfunc

-- this example shows the active lines:

function f (a, b, c) 
print "hi" 
a = b + c
end
t = debug.getinfo (f, "L")
table.foreach (t.activelines, print)

 -->
 
2	true
3	true
4	true

Lua functions

Topics