[Home] [Downloads] [Search] [Help/forum]



Returns a table with information about a function


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


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:

  • f - returns "func" field
  • l - returns "currentline" field
  • L - returns 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.)
  • n - returns "name" and "namewhat" fields
  • S - returns "source", "short_src", "linedefined" and "what" fields
  • u - returns "nups" field

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:

  • source - where the function was defined. If in a file, it is the file name prefixed by "@".

    If the function was defined in a string (through loadstring) then "source" is this string.

    If the function was defined interactively (through the lua.exe program) then source will be "stdin".

    If the function was defined in a C program, then source will be "[C]".

  • short_src - a shorter version of "source" (up to 60 characters), useful for error messages

  • linedefined - the first line number, in the source, where this function was defined (for Lua functions).

  • lastlinedefined - the last line number, in the source, where this function was defined (for Lua functions).

  • what - what this function is. Can be "Lua" for a regular Lua function, "C" if it is a C function, or "main" if it is part of the main Lua chunk (ie. outside any function).

  • name - an attempt to find the name of the function - may be nil.

    Since functions can have many names (by assigning a function to many variables) or no name, the name cannot always be determined.

    Lua tries to find the name of the function by inspecting the call stack to find how the function was called. This can only work if debug.getinfo was called with a stack level number, not a function itself.

  • namewhat - what the "name" field means. It can be "global", "local", "method", "field", or the empty string (""). The empty string means Lua did not find the name, and thus the name field will not be present.

  • nups - the number of upvalues that this function has.

  • activelines - a table of the active lines of the function, that is ones which have code on them, as opposed to blank lines or comments. Each entry in table contains the line number as a key, and true as the value.

  • currentline - the line that is currently active (only applies to active functions, that is if the call to debug.getinfo is for a stack level).

  • func - the function that is active at that stack level (if called with a stack level).

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


source =[C]
what C
func function: 02061360
short_src [C]
currentline -1
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
t = debug.getinfo (f, "L")
table.foreach (t.activelines, print)

2	true
3	true
4	true

See Also ...

Lua functions

debug.debug - Enters interactive debugging
debug.getfenv - Returns the environment of an object
debug.gethook - Returns the current hook settings
debug.getlocal - Returns name and value of a local variable
debug.getmetatable - Returns the metatable of the given object
debug.getregistry - Returns the registry table
debug.getupvalue - Returns the name and value of an upvalue
debug.setfenv - Sets the environment of an object
debug.sethook - Sets a debug hook function
debug.setlocal - Sets the value of the local variable
debug.setmetatable - Sets the metatable for an object
debug.setupvalue - Sets an upvalue for a function
debug.traceback - Returns a string with a traceback of the stack call


Lua base functions
Lua bc (big number) functions
Lua bit manipulation functions
Lua coroutine functions
Lua debug functions
Lua io functions
Lua math functions
Lua os functions
Lua package functions
Lua PCRE regular expression functions
Lua script extensions
Lua string functions
Lua syntax
Lua table functions
Lua utilities
Scripting callbacks - plugins

(Help topic: lua=debug.getinfo)

Documentation contents page

Search ...

Enter a search string to find matching documentation.

Search for:   

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]