New interactive debugging in version 3.80

Posted by Nick Gammon on Wed 06 Sep 2006 02:52 AM — 2 posts, 11,343 views.

Australia Forum Administrator #0

Version 3.80 of MUSHclient has a cool new feature, in addition to upgrading to Lua 5.1 script engine.

This is an interactive debugging facility, implemented by replacing the debug.debug function in the Lua debug library. The original one wasn't very useful as it required reading from stdin, which isn't really relevant for GUI programs.

The replacement invokes a dialog box, like this:

The first thing the dialog box does is automatically call debug.getinfo to find various things out about the current function (eg. name, line number, type of function).

Then you can use various buttons to request extra information be dumped into the output window, like local variables, upvalues, and a stack traceback.

If you want to find out further things, or change things, you can enter Lua statements into the "Command" window and press <Enter>.

If things look hopeless for your script, you can click on "Abort script".

Or, if you want to proceed, hit <Esc> or click on "Continue execution".

The nice thing about this debugger is that it executes in the context of whoever did the debug.debug () call. Thus, you can interactively debug things like plugins.

Australia Forum Administrator #1
Here is an example of how you could add breakpoints into a script:


breakpoints = { [14] = true, [18] = true }

function hook (why)
  local line = debug.getinfo (2, "l").currentline
  if breakpoints [line] then
    debug.debug ()
  end -- breakpoint wanted
end -- hook

debug.sethook (hook, "l", 0)

print (1)
print (2)
print (3)  -- line 14
print (4)
print (5)
print (6)
print (7)  -- line 18
print (8)



When I run this, the debugger is invoked after printing 2 (that is, when line 14 is reached), and after printing 6 (when line 18 is reached).