lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I put together a cross-platform lua debugger with an HTML GUI powered
by node.js just last week.  I'm using it to debug Lua on embedded
devices (android, iphone).  At some point, I'd love to open-source it,
but it's pretty tightly coupled to our current codebase.

Some lessons learned:

Lua's built-in debugging support is pretty atrocious - the debug hook
has unacceptable performance implications, and the logic for stepping
over / out gets more complicated once you realize that matching up
debughook calls and returns isn't really reliable if you're using
LuaJIT (I have code that captures and compares the call stack to
figure this out instead).

You can't really have the debug hook installed most of the time
(because of performance), so my strategy for breakpoints is different:
transform the lua files as they are loaded and insert calls to
DEBUG_BREAK() wherever there's a breakpoint.  You can insert a loader
using into package.loaders, so your code will get a crack at lua files
as they come in. My app has been designed to be able to re-load almost
any lua file transparently, so I can reload files when new breakpoints
are added without reloading the app. And the DEBUG_BREAK call gets
passed the file/line information, so even if the code hasn't been
reloaded yet (if you're currently broken into the debugger and it's in
the call stack), you can at least disable breakpoints.  About the only
case that doesn't work is adding new breakpoints in the currently
broken function (or directly above in the call stack).

Once you've broken using DEBUG_BREAK, I poll for remote debugger
commands (over the wire). I only set the debug hook in response to a
STEP command, and immediately unset it once the command is finished.

I would kill for a proper lua debugging API.  My dream API would look
something like this:

  debugger.break() -- calls the break hook registered with setbreakhook
  debugger.setbreakhook(cb) -- called after break(), step, etc,
executes.  format is:
    function breakhook(getinfo)
      passed in a getinfo function [like debug.getinfo(level,
format)].  level of 2 = where break happened.  optionally, make sure
debug.getinfo works consistently w.r.t stack levels
    end
    call with nil to clear it out.

  debugger.setbreakpoint(file, line)
  debugger.clearbreakpoint(file, line)
  debugger.clearallbreakpoints()
  debugger.step_over([N]) -- steps to the next logical line (repeat
[N] times), calls breakhook
  debugger.step_out([N]) -- steps out of the current function (repeat
[N] times), calls breakhook
  debugger.step_into([N]) -- steps into (as soon as the source/line
changes it triggers, or on "call/return")  (repeat [N] times), calls
breakhook.  Not sure about TCO.
  debugger.continue()
  debugger.setsamplehook(cb, interval) -- called every interval lines,
should use a high number.  useful for sample profilers, doing
background work while debug.step_over() is happening, etc.
  -- could use existing debug.sethook for this instead.


On Tue, Feb 14, 2012 at 3:31 PM, Joshua Jensen <josh.jjensen@gmail.com> wrote:
> ----- Original Message -----
> From: Patrick Rapin
> Date: 2/14/2012 10:13 AM
>>
>>  - Lua 5.2 support would be a plus
>>
>> The remote debugger integrated in LuaPlus [2] seems to be my best bet
>> up to now, but I still couldn't make it work.
>
> I have pushed my changes [1] that make Tilde work (partially) under Lua 5.2;
> the updates can be found in the add-tilde-support branch.  I am uncertain
> how to convert certain lua_getfenv() and lua_setfenv() calls.  Those are the
> primary problem spots right now, and the code will assert when it reaches
> them.
>
> A new -debug command-line option has been added to lua.exe.  Use this to
> make the Lua executable wait for a Tilde connection.
>
> A JamPlus-capable Jamfile.jam is at src/Jamfile.jam.  If you have JamPlus,
> you can build a Tilde-capable Lua 5.2 by just calling 'jam' at the
> command-line.  The Tilde source code is at src/tilde.  src/lua.c was copied
> to src/lua.cpp, and some Tilde updates were added.
>
> Some time soon, I'll merge the Lua 5.2 Tilde with the Lua 5.1 code.
>
> Let me know if you have questions.
>
> -Josh
>
> [1] https://github.com/jjensen/lua52/tree/add-tilde-support
>