debug.lua 874 B

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