lua-users home
lua-l archive

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


I've done exactly that. Its a generic pure Lua solution. Its attached. Do
with it what you will.


Regards,

Dave Nichols
Match-IT Limited
Tel: 0845 1300 510
Fax: 0845 1300 610
mailto:dave.nichols@make247.com
http://www.make247.com

Email Disclaimer: The contents of this electronic mail message and any
attachments (collectively "this message") are confidential, possibly
privileged and intended only for its addressee ("the addressee"). If
received in error, please delete immediately without disclosing its contents
to anyone. Neither the sender nor its management or employees will in any
way be responsible for any advice, opinion, conclusion or other information
contained in this message or arising from it's disclosure.


-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br]On Behalf Of Stefan
Brantschen
Sent: 07 September 2006 14:18
To: Lua list
Subject: Intercepting coroutine.create


I am writing a debugger that is coroutine-savvy, ie. I also want to
able to inspect any coroutines created by the debugged program from
the debugger. For this, I need to hook a corresponding debug routine
into each coroutine (via debug.sethook()), and I am doing this by
intercepting coroutine.create().

I am fairly new to Lua and would like to ask if my approach is OK
from the expert's POV. Note that I only want to use Lua, not C.

This is the relevant code snippet of the debugger that I came up with:

local new_coroutine = {}
setmetatable(new_coroutine, new_coroutine)
new_coroutine.__index = coroutine

function new_coroutine.create (...)
   local cr = new_coroutine.__index.create(...)
   -- 'probe.hook' is the debugger's "hook" routine
   debug.sethook(cr, probe.hook, "clr")
   return cr
end

local new_G = {}
setmetatable(new_G, {__index = _G})
new_G.coroutine = new_coroutine

-- 'file' contains the debugged program
local mf, err = loadfile(file)
-- error handling omitted here
setfenv(mf, new_G)
local mfc = new_coroutine.create(mf)
local result = {coroutine.resume(mfc)}


Is this a suitable approach? Anything that could be improved?

Thanks and regards
- Stefan

Attachment: debugger.lua
Description: Binary data