How to debug in Lua

Lua is a lightweight, high-level scripting language renowned for its versatility and simplicity. It was developed in the early 1990s.

As a scripting language, Lua is designed to work with existing applications. Due to its compact implementation, Lua can easily be linked to other programs, allowing programmers to improve the functionality of complex systems.

Debugging in Lua

Debugging Lua code also includes utilizing different strategies and apparatuses to recognize and fix blunders in the program. In Lua, some common debugging methods include the following:

Printing and logging

We use the print function to output variable values and intermediate results to the console. This can help us understand the program’s flow and identify issues.

local a = 42
local b= 89
local sum = a+b
local difference= sum-b
print(sum) -- Printing the value of the sum
print(difference) -- Printing the value of difference

We can also use a logging library like log4lua or lua-log to log messages at different levels of severity.

Error messages

Lua provides an error message that includes the line number and description when an error occurs. Pay attention to these messages as they can help pinpoint the issue.

Error message with description
Error message with description

Using assert

The assert function is useful for checking conditions that must be true during execution. If the condition is false, it raises an error and displays an error message.

local value = 5
assert(value > 0, "Value must be greater than 0")

Otherwise, it will show the error message as follows:

Error message for assert statement
Error message for assert statement

Insert assert statements at critical points in the code to ensure that the values meet our expectations.

Debug library

Lua offers a debug library that furnishes a range of fundamental functions enabling the creation of custom debuggers. Although Lua lacks a built-in debugger, numerous Lua debuggers are crafted by diverse developers, with many available as open-source solutions.

Some of the commonly used functions are as follows:

  • debug.debug(): This starts a debugging session at the current point in our code, allowing us to execute Lua code interactively.

  • debug.traceback(): This generates a stack trace, displaying the function call hierarchy leading to the error.

  • debug.gethook(): This retrieves the current hook settings.

  • debug.getinfo([thread,] function | stack_level [, option]): It returns a table with information about a function or stack level.

  • debug.getlocal([thread,] level, local_index): This returns the name and value of a local variable in a given stack level.

  • debug.getmetatable(value): This returns the metatable of a given value.

  • debug.getregistry(): This returns the Lua registry table.

  • debug.getupvalue(function, upvalue_index): This returns the name and value of an upvalue in a given function.

  • debug.getuservalue([thread,] object): This returns the user value associated with a given Lua object.

function myFunction()
local value1 = 42
local value2 = value1 + 10
error("An error occurred")
end
local success, err = pcall(myFunction)
if not success then
print("Error:", err)
print("Stack trace:")
print(debug.traceback())
end
  • debug.gethook([thread]): This returns the current hook settings.
  • debug.getlocal([thread,] level, local_index): This returns the name and value of a local variable in a given stack level.
  • debug.getmetatable(value): This returns the metatable of a given value.

IDEs and debuggers

Integrated Development Environments (IDEs) with Lua support often come with debugging features. These tools allow us to set breakpoints, step through our code line by line, inspect variables, and view the call stack.

Some popular Lua IDEs and debuggers include ZeroBrane Studio, IntelliJ IDEA with the Lua plugin, and Visual Studio Code with the Lua extension.

Code review

Sometimes, errors can be challenging, and another pair of eyes can be helpful. Ask the Lua community for a code review to identify any logical or syntactical issues that may not be obvious.

By combining these techniques, we can effectively debug our Lua code and identify and fix any errors or issues that arise during execution.

Copyright ©2024 Educative, Inc. All rights reserved