3969254a95369a098392256aec3cdaf214616e81
[project/luci.git] / libs / core / luasrc / debug.lua
1 local debug = require "debug"
2 local io = require "io"
3 local collectgarbage = collectgarbage
4
5 module "luci.debug"
6 __file__ = debug.getinfo(1, 'S').source:sub(2)
7
8 -- Enables the memory tracer with given flags and returns a function to disable the tracer again
9 function trap_memtrace(flags, dest)
10 flags = flags or "clr"
11 local tracefile = io.open(dest or "/tmp/memtrace", "w")
12 local peak = 0
13
14 local function trap(what, line)
15 local info = debug.getinfo(2, "Sn")
16 if collectgarbage("count") > peak then
17 peak = collectgarbage("count")
18 end
19 if tracefile then
20 tracefile:write(
21 "[", what, "] ", info.source, ":", (line or "?"), "\t",
22 (info.namewhat or ""), "\t",
23 (info.name or ""), "\t",
24 collectgarbage("count"), " (", peak, ")\n"
25 )
26 end
27 end
28
29 debug.sethook(trap, flags)
30
31 return function()
32 debug.sethook()
33 tracefile:close()
34 end
35 end
36