-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex25.6.lua
More file actions
48 lines (40 loc) · 1.07 KB
/
ex25.6.lua
File metadata and controls
48 lines (40 loc) · 1.07 KB
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
local Counters = {}
local Names = {}
local function hook ()
local f = debug.getinfo(2, "f").func
local count = Counters[f]
if count == nil then
-- first time 'f' is called?
Counters[f] = 1
Names[f] = debug.getinfo(2, "Sn")
else
-- only increment the counter
Counters[f] = count + 1
end
end
local f = assert(loadfile(arg[1]))
debug.sethook(hook, "c") -- turn on the hook for calls
f() -- run the main program
debug.sethook() -- turn off the hook
local function getname (func)
local n = Names[func]
if n.what == "C" then
return n.name
end
local lc = string.format("[%s]:%d", n.short_src, n.linedefined)
if n.what ~= "main" and n.namewhat ~= "" then
return string.format("%s (%s)", lc, n.name)
else
return lc
end
end
local sort_list = {}
for k, v in pairs(Counters) do
table.insert(sort_list, {func = k, count = v})
end
table.sort(sort_list, function (lhs, rhs)
return lhs.count < rhs.count
end)
for _, pair in pairs(sort_list) do
print(getname(pair.func), pair.count)
end