trace-calls.lua 749 B

1234567891011121314151617181920212223242526272829303132
  1. -- trace calls
  2. -- example: lua -ltrace-calls bisect.lua
  3. local level=0
  4. local function hook(event)
  5. local t=debug.getinfo(3)
  6. io.write(level," >>> ",string.rep(" ",level))
  7. if t~=nil and t.currentline>=0 then io.write(t.short_src,":",t.currentline," ") end
  8. t=debug.getinfo(2)
  9. if event=="call" then
  10. level=level+1
  11. else
  12. level=level-1 if level<0 then level=0 end
  13. end
  14. if t.what=="main" then
  15. if event=="call" then
  16. io.write("begin ",t.short_src)
  17. else
  18. io.write("end ",t.short_src)
  19. end
  20. elseif t.what=="Lua" then
  21. -- table.foreach(t,print)
  22. io.write(event," ",t.name or "(Lua)"," <",t.linedefined,":",t.short_src,">")
  23. else
  24. io.write(event," ",t.name or "(C)"," [",t.what,"] ")
  25. end
  26. io.write("\n")
  27. end
  28. debug.sethook(hook,"cr")
  29. level=0