Lua - find out calling function
Solution 1
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 ?
Solution 2
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
Comments
-
SatheeshJM almost 4 years
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? -
lhf almost 12 yearsYou don't need to call
debug.getinfo
inside a hook. You can calldebug.getinfo
inside your function directly. -
Mike Corcoran almost 12 yearsha, you're right. i over-engineered it. i'll add a snippet showing the easier way.
-
Lucien Greathouse about 9 yearsWith tail-call optimization, the simpler version might give incorrect results if
caller2
's call tocaller1
ends up tail-call optimized, since it prevents an addition to the call stack.