diff --git a/docs/luadebug/visitor.lua b/docs/luadebug/visitor.lua index 9472d700..a64bfe77 100644 --- a/docs/luadebug/visitor.lua +++ b/docs/luadebug/visitor.lua @@ -299,6 +299,14 @@ end function visitor.costatus(co) end +--- +---@param co refvalue +---@return lightuserdata? +---co不是thread返回nil,否则返回co在调试目标中的地址。该地址能作为协程的标识跨VM传递。 +--- +function visitor.threadptr(co) +end + --- ---@return integer ---等价于`collectgarbage "count"`。 diff --git a/extension/script/backend/worker.lua b/extension/script/backend/worker.lua index ff7f45c2..83e1e468 100644 --- a/extension/script/backend/worker.lua +++ b/extension/script/backend/worker.lua @@ -945,12 +945,19 @@ end function event.thread(co, type) if not debuggeeReady() then return end + -- L是触发事件的协程,即co的调用方(父协程) local L = hookmgr.gethost() if co then - if type == 0 then - coroutineTree[L] = co - elseif type == 1 then - coroutineTree[co] = nil + -- co是调试目标里的协程,转成地址才能在调试器侧标识它 + co = rdebug.threadptr and rdebug.threadptr(co) + if co then + if type == 0 then + coroutineTree[co] = L + hookmgr.updatehookmask(co) + return + elseif type == 1 then + coroutineTree[co] = nil + end end end hookmgr.updatehookmask(L) diff --git a/src/luadebug/rdebug_visitor.cpp b/src/luadebug/rdebug_visitor.cpp index 5168f7d7..96f96bfe 100644 --- a/src/luadebug/rdebug_visitor.cpp +++ b/src/luadebug/rdebug_visitor.cpp @@ -1247,6 +1247,15 @@ namespace luadebug::visitor { return 1; } + static int visitor_threadptr(luadbg_State* L, lua_State* hL, protected_area& area) { + if (!copy_from_dbg(L, hL, area, 1, LUADBG_TTHREAD)) { + return 0; + } + luadbg_pushlightuserdata(L, lua_tothread(hL, -1)); + lua_pop(hL, 1); + return 1; + } + static int visitor_gccount(luadbg_State* L, lua_State* hL, protected_area& area) { int k = lua_gc(hL, LUA_GCCOUNT, 0); int b = lua_gc(hL, LUA_GCCOUNTB, 0); @@ -1319,6 +1328,7 @@ namespace luadebug::visitor { { "watch", protected_call }, { "cleanwatch", protected_call }, { "costatus", protected_call }, + { "threadptr", protected_call }, { "gccount", protected_call }, { "cfunctioninfo", protected_call }, #if LUA_VERSION_NUM >= 503