README

debug.lua

Author: Gunnar Zötl , 2014–2017.
Released under MIT/X11 license. See file LICENSE for details.

Introduction

debug.lua is a terminal based standalone frontend for mobdebug. Because it is terminal based, it is navigated only with the keyboard, but it does feature source navigation and a display for expressions that change as you step through the source.

I wrote it because while there are plenty of integrations of mobdebug with lua IDEs, there is no standalone frontend (that I know of), and using mobdebug without one is somewhat painful.

debug.lua has been tested on Linux and Mac OS X, with lua 5.1, 5.2 and 5.3. The debuggee can be anything that is supported by mobdebug.

Installing

debug.lua depends on termfx, mobdebug and by extension luasocket. You can install it using luarocks, which will automatically pull in the dependencies:

luarocks install debug.lua

Alternatively, if you only downloaded the tarball, you can just copy debug.lua whereever you like (eg. /usr/local/bin), and as long as it can find it’s dependencies, it will run.

You will need version 0.63+ of mobdebug, which is available directly from mobdebugs github archive: https://github.com/pkulchenko/MobDebug or from the main luarocks repository.

Using

you start debug.lua by typing

debug.lua

into your shell. You should then see the startup screen, which informs you on which port debug.lua is waiting for connections from a mobdebug client. Normally this is 8172.

Then start the skript to be debugged. As debug.lua uses mobdebug, the debuggee needs to be prepared as described in the mobdebug docs, either by adding

require("mobdebug").start()

at the beginning, or by starting it with

lua -e "require('mobdebug').start()" debuggee.lua

debug.lua will then enter the debugging display. At the top of the screen there is a status line telling you which skript you’re debugging and where the base directory for that skript is. Below that is a source browser in the top two thirds of the screen, plus a command display in the bottom third. The source browser has it’s own status line at it’s bottom, with the current file name and line number, and on the right the number of currently pinned expressions. The pinned expressions display is closed when there are no pinned expressions, but you can also close and reopen it if there are, using the P command.

Command Line Arguments

Configuration Files

On startup debug.lua will look for a configuration file debug.lua.cfg first in ~/.config and then in the current directory. If both files are present, both are loaded and the local file supersedes the one from ~/.config. With this config file you can change the colors used by debug.lua, by either specifying the names of the colors (“BLACK”, “RED”, “GREEN”, “YELLOW”, “BLUE”, “MAGENTA”, “CYAN”, “WHITE”) or a rgb value as a string “#rgb” with values for rgb ranging from 0 to 5. A sample configuration file can be found in the sample-config directory within the debug.lua distribution.

The Source Display

The top (or, if the pinned expressions display is visible, the top left) part of the display is the source display. Here you can see the source code of the program currently beind debugged, and can also navigate through it using the cursor keys or page up/down. The current line, i.e. the line that is the next to be executed, is marked with a blue arrow (->), and lines that have breakpoints attached to them have a red star.

Navigating through the source windows creates a selection. This is only one line, but if you set a breakpoint, the selected line will automatically be inserted into the breakpoint setting command for you. The same is true for deleting breakpoints.

Under the source display there is a small status bar that shows you the source file currently visible and the current line. Also, if you have any pinned expressions, the number of those is shown on the right side of this status bar.

The Pinned Expressions Display

You can evaluate expressions using the = command, and you can pin expressions using the ! command. Pinned expressions are shown in the pinned expressions display, which is only available when there are any pinned expressions.

Pinned expressions are updated whenever the debugger stops, that is, either after n, s, t, r or o commands and when a breakpoint is hit.

There are only as many pinned expressions available as there is space for them on the terminal. If you have for example terminal size of 80x25 chars, there is space for 14 pinned expressions. If you add more, the oldest ones will be removed.

You can toggle the pinned expressions display using the P command. Just turning it off does not remove the pinned expressions.

The Command Display

The command display takes up the lower third of your screen. Here the commands you entered and their results are being displayed. Also, if you specified the -l option when launching debug.lua, the contents will be written to the file specified with -l.

Debugger Commands

Debugger commands come in 2 varieties, those without arguments and those with. Commands without arguments are executed immediately, without the need to press enter. Commands with arguments present a command line, in which you can type the arguments (like the line number for a breakpoint), and are executed as soon as you press return. If you accidentially activated the command line input, you can escape from it using the Escape key.

Arguments are delimited by spaces, and can be numbers or strings. String arguments can be enclosed by quotes, if they need to contain spaces. In the rare event that you actually want to have a quoted string as an argument (probably to =), you can prefix the opening quote with a backtick (`), or use lua’s long string notation.

Debugging Commands

Source Navigation and other Commands

Debugging Coroutines

As mentioned before, debug.lua uses mobdebug. So, in order to debug coroutines, you will have to prepare the debuggee for this, either by adding

require('mobdebug').coro()

at the start, or by starting it with

lua -e "require('mobdebug').start() require('mobdebug').coro()" debuggee.lua

Weirdnesses

You may notice that breakpoints are hit at strange times. That is because of how lua generates line hook calls. For example, the breakpoint that is placed on the line where a function definition starts, is not triggered when that function is invoked, but when it is defined. You need to place your breakpoint on the first line inside a function for it to be triggered when that function is invoked. Additionally, there are a few small differences between how a program is executed in different versions of the interpreter. Also, after executing the last statement in a skript, the variable display is not updated any more. That is because after that, the debuggee terminates and the information is not available any more.

References

Check the mobdebug documentation at https://github.com/pkulchenko/MobDebug