How to Debug your Lua Scripts with ZeroBrane Studio


Debugging like a PRO


Downloads


ZeroBrane Studio is a lightweight Lua IDE with code completion, syntax highlighting, live coding, code analyzer, and, important thing here, it has the debugging support for Lua 5.1, Lua 5.2, Lua 5.3 and LuaJIT.

GeeXLab uses Lua 5.3.4 and the Lua engine is embedded into GeeXLab.

Question: how can we debug an Lua script that runs inside GeeXLab with an external debugger that has its own version of Lua ?

Answer: thanks to mobdebug. In a word, mobdebug is Lua lib that is loaded in any GeeXLab demo using the require command. Once loaded and started, mobdebug does a TCP connection to ZeroBrane (on port 8172). Once connected, ZeroBrane controls the target Lua script and suddenly we have the power to step into our Lua code. That’s all!

Now, let’s look at how to debug a GeeXLab demo in Lua with ZeroBrane.


Remark: GeeXLab 0.29.15+ is required to debug Lua scripts ZeroBrane (more info HERE)


 
1 – Put mobdebug.lua in your INIT script. mobdebug.lua is shipped with the GeeXLab demo and is also shipped with ZeroBrane Studio (in the lualibs/ folder). Here is a way to add mobdebug.lua in an INIT script (see THIS ARTICLE for more details on require()):

local demo_dir = gh_utils.get_demo_dir()
package.path = package.path .. ";" .. demo_dir .. "mobdebug.lua;" .. demo_dir .. "socket.lua;"
require('mobdebug').start()
...

 
2 – Launch ZeroBrane (zbstudio.exe) and start the debug server. This is fundamental because mobdebug in the GeeXLab INIT script will try to establish a connexion with that server.

You can download ZeroBrane from THIS PAGE (windows) or from THIS ONE (windows + linux).

ZeroBrane - start the debugger server

 
3 – Open the INIT script with ZeroBrane. I put a breakpoint (CTRL+F9) at line 26:

ZeroBrane - GeeXLab demo INIT script

 
4 – Everything is ready. We can now start the demo in GeeXLab and you showld be able now to step into the Lua code with ZeroBrane:

ZeroBrane - GeeXLab - debugging the INIT script

 
To add a breakpoint on a line, select the line and add the breakpoint with the mouse or, better with CTRL+F9. To run the code until next breakpoint, press on F5. To step into the code, use F10. All controls can be found in the Project menu.

 
5 – To watch the content of any Lua variable, just use the menu Add Watch Expression. In the following screenshot I added winW and winH variables to the watch window:

ZeroBrane - GeeXLab - debugging the INIT script - watching variables

ZeroBrane - GeeXLab - debugging the INIT script - watching variables

 
More links:





Leave a Comment

Your email address will not be published. Required fields are marked *