-
Notifications
You must be signed in to change notification settings - Fork 0
/
callers.lua
67 lines (63 loc) · 2.02 KB
/
callers.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
local OUR_NAME, profiler = ...
profiler.callers = {}
function profilingmonitor(fun, secure)
local cached = profiler.callers[fun]
if not cached then
cached = {
[0] = {name="callers"},
}
profiler.callers[fun] = cached
end
local function errhandler(...)
local s
if secure then
s = debugstack(7,1,0)
else
s = debugstack(6,1,0)
end
local file, lineno, callername = s:match("Interface\\AddOns\\(.-).lua:(%d-):.-function `(.-)'")
if not callername then
file, lineno, callername = s:match("Interface\\(.-).lua:(%d-):.-function `(.-)'")
if not callername then
file, lineno, callername = s:match("Interface\\(.-).lua:(%d):.-function <(.-)>")
end
end
if not callername then --TODO: fixme
callername = "unknown"
--print() print(debugstack(6, 1, 0)) print()
end
local callerinfo = cached[callername]
if not callerinfo then
callerinfo = {name=callername, title=callername, type="caller", ncalls=0, cpu=0}
cached[#cached+1] = callerinfo
cached[callername] = callerinfo
end
callerinfo.ncalls = callerinfo.ncalls + 1
end
if not secure then
return function(...)
xpcall(function() error("finding callsites the ugly way") end, errhandler)
-- do prestuff
local res = fun(...)
-- do poststuff
return res
end
else
return function(...)
xpcall(function() error("finding callsites the ugly way") end, errhandler)
end
end
end
profiler.hooks = {}
function profiler.hook(t, name)
local func = t[name]
local hookfunc
if issecurevariable(t, name) then
hookfunc = profilingmonitor(func, true)
hooksecurefunc(t, name, hookfunc)
else
hookfunc = profilingmonitor(func)
t[name] = hookfunc
end
profiler.hooks[hookfunc] = func
end