Sep 09

Debugging Lua scripts using RemDebug

As a follow-up to a previous post: "Accessing the web using Lua", this is a blog post I've been meaning to post for a while but never really finished it until today. Debugging is a powerful tool that every developer uses or should use. While playing around with Lua and some Lua scripts the question came up, how does one debug Lua? Two different ways to debug a Lua script will be shown in blog post.

print statements

Probably the easiest method to implement is the print statement. If one actually calls putting print statements into scripts “debugging” is a different question, but it helps. It can sometimes be hard to figure out what value you were actually looking for and often results in add print statemnt -> run -> adjust -> run -> adjust -> run …. The way it works is simply adding print(yourvalue) to your script:

function bar()
    for i = 1, 10 do
       tab.foo = tab.foo * i
       print(tab.foo)
    end
end

This is a really simple example but getting values to be printed helps when debugging a problem, though in this example might result in a confusing list of numbers.

RemDebug

On the hunt for a debugging solution for the command line you will probably come across the remote debugger for Lua. After downloading and installing it (you can use LuaRocks for it), there a 2 steps to be taken

Start the controller

lua controller.lua

Add this to the script you want to debug:

require("remdebug.engine")
remdebug.engine.start()

Run your lua script in a different console than the controller

lua file2debug.lua

You will notice that the controller stop and gives you an input while your script is being stopped:

Lua Remote Debugger
Run the program you wish to debug
Paused at file home/qs/luarning/debug/file2debug.lua
Type 'help' for commands

The next thing you have to do is tell the controller what its base directory is (where it can find the files you are going to debug):

basedir home/qs/luarning/debug/

Note: On Linux when you enter your basedir, do not put a leading slash in front of the path. I haven’t figured out why that is yet, but using “/home/qs/...” did not work for me on Linux (Ubuntu 9.04).

For a list of the available commands one can use the 'help’ command in the controller console window. The ones I have used most:

setb file2debug.lua 12 --sets a breakpoint in the specified line of the specified file
delb file2debug.lua 12 --removes the breakpoint
delallb --removes all breakpoints
run --run the file until the next breakpoint
eval <exp> --returns the value of the given expression (e.g. eval tab.foo)

RemDebug enables you to set expressions, too, instead of just evaluating them. This makes it a really nice tool to use.

Conclusion

Nothing can stop you when you are good at debugging. And I mean it. As soon as you are able to debug, you start to learn how the program works or how the programming language works / handles things. You get to study the program / script step-by-step. So start debugging your scripts in case there is a bug or you just want to get to know the code you are working with. There are still occasions where you can use print statement, but I think RemDebug is far more powerful.

Further references

Tags: debug, lua, lua remdebug, luarocks, luasocket, remdebug