The next events debug.sethook can hook into are function calls. These events fire every time a function is called. The event handler only needs to take one argument, a string. The value of this string will always be "call". To subscribe to function call events, provide debug.sethook with two arguments: the handler function, and the "c" string. Function callback hooks become much more powerful when combined with the info obtained from debug.getinfo. The following code demonstrates this:
function PrintV(x, y, z)
local out = "(" .. x .. ","
out = out .. ", " .. y
out = out .. ", " .. z .. ")"
return out
end
function MagnitudeSq(x, y, z)
local magSq = x * x + y * y + z * z
return magSq;
end
function trace(event)
local info = debug.getinfo(2)
if info.what == "Lua" then
print ("event: " .. event)
print (" function: " .. info.name)
print (" defined on: " .. info.linedefined)
end
end
debug.sethook(trace, "c")
local mSq = MagnitudeSq(9, 2, 6)
print ("Sqr mag: " .. mSq)
PrintV(9,2,6)