lua-users home
lua-l archive

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


> I'm interested in ldb because it has "break" and "stop/continue", something 
> which I want to explore for my application both for debugging and to pause 
> lua scripts during normal execution.  Lua's debug API doesn't seem to 
> have a break or stop/continue.  Is there a simple way to implement this?
	You can set a line hook that checks for the specific line you want
to break.  Something like:

setlinehook (function (line) if line == <line> then <do something> end end)

	But this will not let you make more than one break.  I made a small
test of reimplementing the 'doat' function of Ldb in Lua.  It uses only
the line hook (it's just an example) and I didn't tested it using many
files.  The idea of 'doat' is to define something to do at a given point
of execution, like showing local variables values or checking some condition
to stop the execution.  The call doat(show_locals, fib, 1) can be read as
"do 'show_locals' at line 1 of 'fib'".  The original 'doat' (as all Ldb's
functions) provides a copy of the local environment of the running function
at the global environment, so the 'action' doesn't have to search for
locals.  I didn't implemented it here, but it isn't difficult.

	Good luck!
		Tomas

----------------------------------------------------------------------
$debug

----------------------------------------------------------------------
-- debug stuff

actions_list = {}

function doat (action, func, line)
   local f = funcinfo (func)
   local file_line = f.def_line
   if line then
      file_line = file_line + line
   end
   local file_name = f.source
   if not actions_list[file_name] then
      actions_list[file_name] = {}
   end
   actions_list[file_name][file_line] = action
end   
   
function line_hook (line)
   local f = getstack (2)               -- the running function
   if actions_list[f.source] then
      local action = actions_list[f.source][line]
      if action then
         action ()
      end
   end
end      
      
function show_locals ()
   local l = getlocal (3)               -- the running function
   foreach (l, print)
end
   
----------------------------------------------------------------------
-- application

function fib (n)
   if n <= 1 then
      return 1
   else
      return fib(n-1) + fib(n-2)
   end
end

----------------------------------------------------------------------
-- debugging

setlinehook (line_hook)

doat (show_locals, fib, 1)
print (fib (5))

setlinehook ()