get when a function is called
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
get when a function is called
this would REALLY help with debugging, i want to see when any function at all is called and what it is, is there a way to do this? i can't add a print to every single function in my entire project then go through removing them all
while true do end;
- zorg
- Party member
- Posts: 3444
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: get when a function is called
yes, but it will be slow:
(I only copied this from one of my libs that worked, dunno if it will like this; you can always check PIL)
Code: Select all
debugFunction = function(event,line)
local s = debug.getinfo(2).short_src
print(s,line) -- or better yet, show it buffered on screen with l.g.print/f, and log it into a file as well...
end
debug.sethook(debugFunction,'l') -- l means to debug per-line, a neat trace function
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Re: get when a function is called
I used the same approach in my profiler:
https://bitbucket.org/itraykov/utils/src -> log -> profile.lua
Basically, it tracks time, number of calls and function names.
Just drop the profile.lua file in your project and try:
When you're done profiling:
There are some issues with 'debug.getinfo' though.
First it uses a lot of memory so make sure the GC is turned on.
Second, there is extra overhead for function calls so the 'execTime' will be slower when you have a lot of function calls.
Other than that it's pretty useful.
It's possible but it's very slow and not realistic (so I disabled it in the profiler).
Per-line debugging as described by Zorg could work but for small projects.
I suspect that it would be slower than function hooks.
https://bitbucket.org/itraykov/utils/src -> log -> profile.lua
Basically, it tracks time, number of calls and function names.
Just drop the profile.lua file in your project and try:
Code: Select all
profiler = require('utils.log.profile')
profiler.hookall("Lua") -- could be "Lua", "C", "internal" or leave blank to hook everything
-- profiler.setclock(superAccurateTicksCounterFunction) -- optional, default is os.clock
profiler.start() -- start the profiler
-- run your lua code
Code: Select all
profiler.stop()
-- print results
local n = 1
-- print top 10 functions, sorted by longest execution time
for funcName, nCalls, execTime, codeLine in profiler.query("time", 10) do
printf("%d.'%s'x%d time:%s (%s)", n, funcName, nCalls, execTime, codeLine)
n = n + 1
end
-- reset stats
profiler.reset()
First it uses a lot of memory so make sure the GC is turned on.
Second, there is extra overhead for function calls so the 'execTime' will be slower when you have a lot of function calls.
Other than that it's pretty useful.
For C functions it's not very meaningful unless you find the Lua point where the C function is being called.i want to see when any function at all is called and what it is
It's possible but it's very slow and not realistic (so I disabled it in the profiler).
Per-line debugging as described by Zorg could work but for small projects.
I suspect that it would be slower than function hooks.
Who is online
Users browsing this forum: No registered users and 200 guests