The local variables of any function can be inspected with the debug.getlocal function. This function takes two arguments: first a stack level (the same as debug.getinfo), and then a variable index. Variables are indexed in the order they appear. If either argument is out of range, the debug.getlocal function will return nil. Otherwise, it will return the name and value of the current variable.
For example, the following code prints the local variables of its calling function:
function DebugLocals()
local info = debug.getinfo(2, "n")
print ("Local variables of: " .. info.name)
local idx = 1
while true do
local name, val = debug.getlocal(2, idx)
if name == nil then
break
end
print (" " .. name .. " = " .. tostring(val))
idx = idx + 1
end
end
function DoSomething(val1, val2)
local sum = val1 + val2
local difference = val1 - val2
local result = sum * difference
DebugLocals();
return result;
end
DoSomething(3, 4)
DoSomething(9, 3)