Page 1 of 2

[SOLVED] Debugging using ZeroBrane Studio

Posted: Fri Jun 16, 2017 2:53 pm
by randomnovice
Hi folks,

I'm running into an occasional spinning wheel of death bug (infinite loop somewhere!?) and my programme is long enough and (probably unnecessarily) complicated enough that it's not easy to spot exactly where/how.

I wrote it using TextEdit before discovering the wonders of ZeroBrane (which is fab btw).

I think ZeroBrane will allow me to evaluate variables etc. in real (run)time, but I haven't yet understood how. I've looked at some blogs & posts on this and have managed to activate the print command to print live (rather than after the programme finishes) which is helpful but I believe it should be possible to do more i.e. ask to evaluate variables during operation.

What would I love to do is when the spinning wheel bug comes up, ask some questions of the programme and see if I can discover how it ended up like that. Or it would also be helpful to know what line of code it is attempting to execute.

I think there is a way to run the programme step-by-step, waiting each time before continuing, but this isn't much good to me as it runs thousands of iterations before any bug ever crops up (and the bug appears to be sporadic).

Thanks in advance!

Re: Debugging using ZeroBrane Studio

Posted: Fri Jun 16, 2017 9:57 pm
by artofwork
I don't debug a program by using the debugger I usually test a program step by step, testing each value as I go and if i run into an issue i'll comment out all the code until i find what is causing the issue.

The only time data is printed the console or written to file is when you tell it to do so. There is no magical method in existence which will execute print unless you program a procedure or method that does just that.

Based on your question I think you need to take a moment of your time and learn lua from the ground up because knowing the behavior of print and even io.write is something most of us who do know the language having been using since day one of learning it.

Re: Debugging using ZeroBrane Studio

Posted: Sat Jun 17, 2017 12:30 am
by alloyed
So when I'm writing something that could be an infinite loop I use something the following function

Code: Select all

local function safe_iter(max_iters)
    max_iters = max_iters or 10000
    local i = 0
    return function()
        i = i + 1
        if i > max_iters then error("iteration limit reached") end
    end
end
use like so

Code: Select all

local incr = safe_iter()
while true do
    incr()
end
and this catches infinite loops before they kill my computer, with a nice stack trace
To know when you might be writing an infinite loop, look for while/repeat loops, or functions that call themselves (aka recursive functions). sometimes, a function can call itself in a roundabout way, for example a() -> b() -> c() -> a(). This is harder to debug, and I don't have really great answers for it that aren't complex, so my suggestion would be to try to avoid the situation entirely. This is not amazing advice but hopefully this gives you something to look for?

Re: Debugging using ZeroBrane Studio

Posted: Sat Jun 17, 2017 7:19 am
by randomnovice
Thanks both,
The problem with testing step-by-step is in this case it's an AI and the issue is sporadic. Sometimes it runs absolutely fine multiple times in a row. Other times the AI just gets stuck somewhere.
I'll just have to re-check all the loops and try to find one that could somehow get stuck.
Like I said - even being able to know what line was attempting to be executed when you stop the programme would be super helpful if such a thing existed.

Re: Debugging using ZeroBrane Studio

Posted: Sat Jun 17, 2017 4:52 pm
by brucejs777
I'd like to get the LOVE debugging to work as well. Debug facility exist for good reason, and I've found it indispensable over the years for efficient and effective debuging.
ZeroBrane can debug Lua programs when the interpreter is set to Lua. But it seems to lose it's debug facility when the interpreter is set to LOVE. Perhaps there is a different way to set up the build so as to run in Lua interpreter mode, yet use LOVE as a library ?

Re: Debugging using ZeroBrane Studio

Posted: Sat Jun 17, 2017 9:21 pm
by easy82
Hello guys. I think you are missing 1 line of code:
http://notebook.kulchenko.com/zerobrane ... -debugging

Re: Debugging using ZeroBrane Studio

Posted: Sat Jun 17, 2017 9:35 pm
by zorg
easy82 wrote: Sat Jun 17, 2017 9:21 pm Hello guys. I think you are missing 1 line of code:
http://notebook.kulchenko.com/zerobrane ... -debugging
Don't be afraid to be more specific! (Hadd érezzék a törődést) :3
Paul on the zerobrane site wrote:Add if arg[#arg] == "-debug" then require("mobdebug").start() end line to your script somewhere inside love.load(arg) function. This will allow you to use Project | Run command to run the application and Project | Start Debugging to debug your application.

Re: Debugging using ZeroBrane Studio

Posted: Sun Jun 18, 2017 3:47 pm
by randomnovice
Thanks both. I did try that line before but I actually get an error:

Error
main.ua:64: attempt to get length of local 'arg' (a nil value)

Traceback

main.lua:64: in function 'load'
main.lua:8: in function <main.lua:3>
[C]: in funtion 'xpcall'

Re: Debugging using ZeroBrane Studio

Posted: Sun Jun 18, 2017 3:49 pm
by randomnovice
Shall I change the call of
love.load()

in love.run() to

love.load({"-debug"}) ??

Re: Debugging using ZeroBrane Studio

Posted: Sun Jun 18, 2017 3:54 pm
by zorg
You don't call love.load yourself, you define it, it's a callback.

In love.run, it should look like this:

Code: Select all

if love.load then love.load(arg) end
In your main.lua or whatever, this should work, though i never used zerobrane:

Code: Select all

function love.load(arg)
    if arg[#arg] == "-debug" then require("mobdebug").start() end
end