Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua - find out calling function

Tags:

function

lua

In Lua, is it possible to know which function has called the current function.

For instance

function a()
    get_calling_function()    --Should print function b
end 


function b()
    a() 
end 

Is something like this possible?
Does the debug library have such functionality?

like image 617
SatheeshJM Avatar asked May 31 '12 18:05

SatheeshJM


People also ask

How do I call a function in Lua?

The API protocol to call a function is simple: First, you push the function to be called; second, you push the arguments to the call; then you use lua_pcall to do the actual call; finally, you pop the results from the stack.

What is CB in Lua?

Functions in Lua are first class citizens that can be treated like any other values; they can be assigned to variables or passed as function arguments. local function cb() ... end local button = Button:new(..., cb) Or without a local variable. local button = Button:new(..., function() ...

Can you have a function inside a function in Lua?

Lua does allow you to define nested functions, but I can only think of two reasons to use them: to return a function that encapsulates a task, usually with some of the wrapper function's args and/or locals as upvalues.

Is Lua pass by reference?

Lua's function , table , userdata and thread (coroutine) types are passed by reference. The other types are passed by value.


2 Answers

You could use debug.traceback():

function a()
    print(debug.traceback())
end 


function b()
    a() 
end 

b()

which would print:

stack traceback:
    ./test.lua:45: in function 'a'
    ./test.lua:50: in function 'b'
    ./test.lua:53: in main chunk
    [C]: in ?
like image 87
Bart Kiers Avatar answered Oct 14 '22 21:10

Bart Kiers


you can use debug.sethook() to set up a hook that gets called each time certain special events happen in lua. it can be useful for things like this.

local debugInfo = { caller = nil, callee = nil }
function hook()
    local info = debug.getinfo(2)
    if info == nil then
        debugInfo.callee = nil
        return
    end

    -- we only want to watch lua function calls (not C functions)
    if info.what ~= "Lua" then
        debugInfo.callee = "C function"
        return
    end

    debugInfo.caller = debugInfo.callee
    debugInfo.callee = info.name
end


debug.sethook(hook, "c")

function caller1()
    if debugInfo.caller ~= nil and debugInfo.callee ~= nil then
        msg = debugInfo.callee.. " was called by ".. debugInfo.caller.. "!"
        print(msg)
    end
end

function caller2()
    caller1()
end


caller2()

this prints 'caller1 was called from caller2!'

debug.sethook can handle 3 different characters in the second parameter so you can let it know when to notify you. 'c' means call your hook function any time a function is called in lua, 'r' means call your hook function every time a function returns in lua, and 'l' means call your hook function whenever lua processes a new line of code.

you could set this up to build your own custom stack trace if you really wanted to, and you could also use debug.getlocal() within your hook to even try to work out what arguments were passed to your called function.

edit for lhf. this is actually a much simpler way of doing what you're asking, if you don't need to track this and just need to know the context of how the function was called.

function caller1()
    local current_func = debug.getinfo(1)
    local calling_func = debug.getinfo(2)
    print(current_func.name.. " was called by ".. calling_func.name.. "!")
end

function caller2()
    caller1()
end
like image 37
Mike Corcoran Avatar answered Oct 14 '22 19:10

Mike Corcoran