A simple snippet that installs a hook recording all function calls into a table. Could be built to generate call graphs or something.
Last active
August 1, 2019 19:52
-
-
Save DarkWiiPlayer/4571263146af29808236b167429bc3cc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local doubles do | |
local function iter(tab, current) | |
local index = (current+1)*2 | |
local first, second = tab[index-1], tab[index] | |
if first and second then | |
return current+1, first, second | |
else | |
return | |
end | |
end | |
function doubles(tab) | |
return iter, tab, 0 | |
end | |
end | |
local function observed(fn, ...) | |
local calls, size = {}, 0 | |
debug.sethook(function(event) | |
if event == 'call' then | |
local caller = debug.getinfo(3) | |
local callee = debug.getinfo(2) | |
calls[size+1] = caller | |
calls[size+2] = callee | |
size = size + 2 | |
end | |
end, 'c') | |
fn(...) | |
debug.sethook() | |
calls[size], calls[size-1] = nil, nil -- Remove call in above line | |
return calls | |
end | |
local graph = observed(function() | |
print 'Hello World' | |
end) | |
local function name(fn) | |
local n | |
if not fn.name or fn.name == '?' then | |
n = '<function>' | |
else | |
n = fn.name .. ' ('..fn.namewhat..')' | |
end | |
return n .. ' ' .. tostring(fn.func):match("0x%x+") | |
end | |
for index, caller, callee in doubles(graph) do | |
print(index, name(caller).." -> "..name(callee)) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment