From 92516fb4c48e000f2b87674f88a1d37d6b9f8b49 Mon Sep 17 00:00:00 2001 From: Javier Peletier Date: Mon, 7 Apr 2025 19:34:08 +0200 Subject: [PATCH 1/5] debugger hot restart --- extension/script/backend/bootstrap.lua | 14 ++++++++++++-- extension/script/backend/master/mgr.lua | 3 ++- extension/script/backend/worker.lua | 2 +- extension/script/debugger.lua | 4 ++++ src/luadebug/rdebug_hookmgr.cpp | 6 ++++++ 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/extension/script/backend/bootstrap.lua b/extension/script/backend/bootstrap.lua index 6fa66113..c8c6b189 100644 --- a/extension/script/backend/bootstrap.lua +++ b/extension/script/backend/bootstrap.lua @@ -3,20 +3,26 @@ local channel = require "bee.channel" local m = {} +-- Stores the name of the master channel +---@type string | nil +DbgMaster = DbgMaster + local function hasMaster() - return channel.query "DbgMaster" ~= nil + return DbgMaster and channel.query(DbgMaster) ~= nil end local function initMaster(rootpath, address) if hasMaster() then return end - local chan = channel.create "DbgMaster" + DbgMaster = "DbgMaster-" .. address + local chan = channel.create(DbgMaster) local mt = thread.create(([[ local rootpath = %q package.path = rootpath.."/script/?.lua" local log = require "common.log" log.file = rootpath.."/master.log" + DbgMaster = %q local ok, err = xpcall(function() local socket = require "common.socket"(%q) local master = require "backend.master.mgr" @@ -28,11 +34,15 @@ local function initMaster(rootpath, address) end ]]):format( rootpath, + DbgMaster, address )) ExitGuard = setmetatable({}, {__gc=function() chan:push(nil, "EXIT") thread.wait(mt) + local WorkerIdent = tostring(thread.id) + local WorkerChannel = ('DbgWorker(%s)'):format(WorkerIdent) + channel.destroy(WorkerChannel) end}) end diff --git a/extension/script/backend/master/mgr.lua b/extension/script/backend/master/mgr.lua index 9b3383db..1a41a10b 100644 --- a/extension/script/backend/master/mgr.lua +++ b/extension/script/backend/master/mgr.lua @@ -43,7 +43,7 @@ end function mgr.init(io) socket = io - masterThread = assert(channel.query 'DbgMaster') + masterThread = assert(channel.query(DbgMaster)) socket.event_close(event_close) return true end @@ -260,6 +260,7 @@ function mgr.update() local event = require 'backend.master.event' event.terminated() socket.closeall() + channel.destroy(DbgMaster) end function mgr.setClient(c) diff --git a/extension/script/backend/worker.lua b/extension/script/backend/worker.lua index ac149ba5..3dce6413 100644 --- a/extension/script/backend/worker.lua +++ b/extension/script/backend/worker.lua @@ -36,7 +36,7 @@ local CMD = {} local WorkerIdent = tostring(thread.id) local WorkerChannel = ('DbgWorker(%s)'):format(WorkerIdent) -local masterThread = assert(channel.query 'DbgMaster') +local masterThread = assert(channel.query(DbgMaster)) local workerThread = channel.create(WorkerChannel) local function workerThreadUpdate(timeout) diff --git a/extension/script/debugger.lua b/extension/script/debugger.lua index 677241c7..6bbffa69 100644 --- a/extension/script/debugger.lua +++ b/extension/script/debugger.lua @@ -199,6 +199,10 @@ function dbg:attach(cfg) return self end +function dbg:stop() + self.rdebug.clear() +end + function dbg:event(...) self.rdebug.event(...) return self diff --git a/src/luadebug/rdebug_hookmgr.cpp b/src/luadebug/rdebug_hookmgr.cpp index 4c246bdf..7315662a 100644 --- a/src/luadebug/rdebug_hookmgr.cpp +++ b/src/luadebug/rdebug_hookmgr.cpp @@ -568,6 +568,10 @@ struct hookmgr { } luadebug::eventfree::destroy(hL, eventfree); lua_sethook(hL, 0, 0, 0); +#if defined(LUADEBUG_DISABLE_THUNK) + // clear hook manager pointer stored in thunk: + thunk_set(hL, &THUNK_MGR, intptr_t(nullptr)); +#endif #if defined(LUA_HOOKEXCEPTION) exception_open(hL, 0); #endif @@ -594,11 +598,13 @@ struct hookmgr { #else static int full_hook_callback(lua_State* hL, lua_Debug* ar) { hookmgr* mgr = (hookmgr*)thunk_get(hL, &THUNK_MGR); + if (!mgr) return 0; mgr->full_hook(hL, ar); return 0; } static int idle_hook_callback(lua_State* hL, lua_Debug* ar) { hookmgr* mgr = (hookmgr*)thunk_get(hL, &THUNK_MGR); + if (!mgr) return 0; mgr->idle_hook(hL, ar); return 0; } From b1cae0cb33dd3d3fc24e87bd5e5598769b8660ed Mon Sep 17 00:00:00 2001 From: Javier Peletier Date: Tue, 8 Apr 2025 11:57:58 +0200 Subject: [PATCH 2/5] revert unnecessary changes --- extension/script/backend/bootstrap.lua | 17 ++++------------- extension/script/backend/master/mgr.lua | 7 ++++--- extension/script/backend/worker.lua | 4 ++-- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/extension/script/backend/bootstrap.lua b/extension/script/backend/bootstrap.lua index c8c6b189..e49e735e 100644 --- a/extension/script/backend/bootstrap.lua +++ b/extension/script/backend/bootstrap.lua @@ -3,26 +3,20 @@ local channel = require "bee.channel" local m = {} --- Stores the name of the master channel ----@type string | nil -DbgMaster = DbgMaster - local function hasMaster() - return DbgMaster and channel.query(DbgMaster) ~= nil + return channel.query "DbgMaster" ~= nil end local function initMaster(rootpath, address) if hasMaster() then return end - DbgMaster = "DbgMaster-" .. address - local chan = channel.create(DbgMaster) + local chan = channel.create "DbgMaster" local mt = thread.create(([[ local rootpath = %q package.path = rootpath.."/script/?.lua" local log = require "common.log" log.file = rootpath.."/master.log" - DbgMaster = %q local ok, err = xpcall(function() local socket = require "common.socket"(%q) local master = require "backend.master.mgr" @@ -34,15 +28,12 @@ local function initMaster(rootpath, address) end ]]):format( rootpath, - DbgMaster, address )) ExitGuard = setmetatable({}, {__gc=function() chan:push(nil, "EXIT") thread.wait(mt) - local WorkerIdent = tostring(thread.id) - local WorkerChannel = ('DbgWorker(%s)'):format(WorkerIdent) - channel.destroy(WorkerChannel) + channel.destroy("DbgMaster") end}) end @@ -63,4 +54,4 @@ function m.attach(rootpath) end end -return m +return m \ No newline at end of file diff --git a/extension/script/backend/master/mgr.lua b/extension/script/backend/master/mgr.lua index 1a41a10b..181f53e4 100644 --- a/extension/script/backend/master/mgr.lua +++ b/extension/script/backend/master/mgr.lua @@ -43,7 +43,7 @@ end function mgr.init(io) socket = io - masterThread = assert(channel.query(DbgMaster)) + masterThread = assert(channel.query 'DbgMaster') socket.event_close(event_close) return true end @@ -166,6 +166,8 @@ function mgr.exitWorker(w) for WorkerIdent, threadId in pairs(threadCatalog) do if threadId == w then threadCatalog[WorkerIdent] = nil + local workerChannel = ('DbgWorker(%s)'):format(WorkerIdent) + channel.destroy(workerChannel) end end threadStatus[w] = nil @@ -260,7 +262,6 @@ function mgr.update() local event = require 'backend.master.event' event.terminated() socket.closeall() - channel.destroy(DbgMaster) end function mgr.setClient(c) @@ -271,4 +272,4 @@ function mgr.getClient() return client end -return mgr +return mgr \ No newline at end of file diff --git a/extension/script/backend/worker.lua b/extension/script/backend/worker.lua index 3dce6413..be22b68b 100644 --- a/extension/script/backend/worker.lua +++ b/extension/script/backend/worker.lua @@ -36,7 +36,7 @@ local CMD = {} local WorkerIdent = tostring(thread.id) local WorkerChannel = ('DbgWorker(%s)'):format(WorkerIdent) -local masterThread = assert(channel.query(DbgMaster)) +local masterThread = assert(channel.query 'DbgMaster') local workerThread = channel.create(WorkerChannel) local function workerThreadUpdate(timeout) @@ -879,4 +879,4 @@ end) sendToMaster 'initWorker' {} -hookmgr.update_open(true) +hookmgr.update_open(true) \ No newline at end of file From 9160402bcf3c3f313e076f9d327f4ecef9763c75 Mon Sep 17 00:00:00 2001 From: Javier Peletier Date: Tue, 8 Apr 2025 11:59:37 +0200 Subject: [PATCH 3/5] dbg:attach default --- extension/script/debugger.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extension/script/debugger.lua b/extension/script/debugger.lua index 6bbffa69..cf9314e9 100644 --- a/extension/script/debugger.lua +++ b/extension/script/debugger.lua @@ -187,7 +187,7 @@ function dbg:start(cfg) end function dbg:attach(cfg) - initDebugger(self, cfg) + initDebugger(self, cfg or {}) self.rdebug.start(([[ local rootpath = %q From 3d58a21046283cfd3d44c19107e02127f1cdff88 Mon Sep 17 00:00:00 2001 From: Javier Peletier Date: Tue, 8 Apr 2025 12:01:32 +0200 Subject: [PATCH 4/5] fix last cr-lf --- extension/script/backend/bootstrap.lua | 2 +- extension/script/backend/master/mgr.lua | 2 +- extension/script/backend/worker.lua | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/extension/script/backend/bootstrap.lua b/extension/script/backend/bootstrap.lua index e49e735e..45ab5b87 100644 --- a/extension/script/backend/bootstrap.lua +++ b/extension/script/backend/bootstrap.lua @@ -54,4 +54,4 @@ function m.attach(rootpath) end end -return m \ No newline at end of file +return m diff --git a/extension/script/backend/master/mgr.lua b/extension/script/backend/master/mgr.lua index 181f53e4..0aa17daf 100644 --- a/extension/script/backend/master/mgr.lua +++ b/extension/script/backend/master/mgr.lua @@ -272,4 +272,4 @@ function mgr.getClient() return client end -return mgr \ No newline at end of file +return mgr diff --git a/extension/script/backend/worker.lua b/extension/script/backend/worker.lua index be22b68b..ac149ba5 100644 --- a/extension/script/backend/worker.lua +++ b/extension/script/backend/worker.lua @@ -879,4 +879,4 @@ end) sendToMaster 'initWorker' {} -hookmgr.update_open(true) \ No newline at end of file +hookmgr.update_open(true) From 69b7919aad13c5cda67cfe48baff04b52b69316d Mon Sep 17 00:00:00 2001 From: Javier Peletier Date: Fri, 18 Apr 2025 18:37:46 +0200 Subject: [PATCH 5/5] close worker channel in the right place --- extension/script/backend/master/mgr.lua | 2 -- extension/script/backend/worker.lua | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/extension/script/backend/master/mgr.lua b/extension/script/backend/master/mgr.lua index 0aa17daf..9b3383db 100644 --- a/extension/script/backend/master/mgr.lua +++ b/extension/script/backend/master/mgr.lua @@ -166,8 +166,6 @@ function mgr.exitWorker(w) for WorkerIdent, threadId in pairs(threadCatalog) do if threadId == w then threadCatalog[WorkerIdent] = nil - local workerChannel = ('DbgWorker(%s)'):format(WorkerIdent) - channel.destroy(workerChannel) end end threadStatus[w] = nil diff --git a/extension/script/backend/worker.lua b/extension/script/backend/worker.lua index ac149ba5..82129993 100644 --- a/extension/script/backend/worker.lua +++ b/extension/script/backend/worker.lua @@ -829,6 +829,7 @@ end function event.exit() sendToMaster 'exitWorker' {} + channel.destroy(WorkerChannel) end hookmgr.init(function(name, ...)