get when a function is called

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
RonanZero
Citizen
Posts: 90
Joined: Mon Oct 20, 2014 3:33 am

get when a function is called

Post by RonanZero »

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;
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: get when a function is called

Post by zorg »

yes, but it will be slow:

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
(I only copied this from one of my libs that worked, dunno if it will like this; you can always check PIL)
Me and my stuff :3True 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.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: get when a function is called

Post by ivan »

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:

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
When you're done profiling:

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()
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.
i want to see when any function at all is called and what it is
For C functions it's not very meaningful unless you find the Lua point where the C function is being called.
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.
Post Reply

Who is online

Users browsing this forum: No registered users and 200 guests