localfunctionremovebreakpoint(id) local s = status if s.bptable[id] == nilthen return end [[ 新增代码开始 ]] local func = s.bptable[id].func local line = s.bptable[id].line s.funcbpt[func].num = s.funcbpt[func].num - 1 s.funcbpt[func][line] = nil if s.funcbpt[func].num == 0then s.funcbpt[func] = nil end [[ 新增代码结束 ]] s.bptable[id] = nil s.bpnum = s.bpnum - 1 if s.bpnum == 0then debug.sethook() -- 移除钩子 end end
local function hook (event, line) local s = status if event == "call" or event == "tail call" then local func = debug.getinfo(2, "f").func - for _, v in pairs(s.bptable) do - -- found breakpoint in current function - if v.func == func then - if event == "call" then - s.stackdepth = s.stackdepth + 1 - end - s.stackinfos[s.stackdepth] = - {func = func, hasbreak = true} - debug.sethook(hook, "crl") -- 添加"line"事件 - return - end - end - -- 没有断点 - if event == "call" then + if event == "call" then -- 对于尾调用,直接覆盖 s.stackdepth = s.stackdepth + 1 end - s.stackinfos[s.stackdepth] = {func = func, hasbreak = false} - debug.sethook(hook, "cr") -- 去掉"line"事件 + -- found breakpoint in current function + if s.funcbpt[func] then + s.stackinfos[s.stackdepth] = {func = func, hasbreak = true} + debug.sethook(hook, "crl") -- 添加"line"事件 + else -- no breakpoints found + s.stackinfos[s.stackdepth] = {func = func, hasbreak = false} + debug.sethook(hook, "cr") -- 去掉"line"事件 + end elseif event == "return" or event == "tail return" then
-- 省略 elseif event == "line"then for _, v inpairs(s.bptable) do if v.func == s.stackinfos[s.stackdepth].func and v.line == line then ifnot s.funcinfos[v.func] then s.funcinfos[v.func] = debug.getinfo(2, "nS") end local info = s.funcinfos[v.func] local prompt = string.format("%s (%s)%s %s:%d\n", info.what, info.namewhat, info.name, info.short_src, line) io.write(prompt) debug.debug() end end end -- 省略
elseif event == "line"then local curfunc = s.stackinfos[s.stackdepth].func local funcbp = s.funcbpt[curfunc] assert(funcbp) if funcbp[line] then ifnot s.funcinfos[curfunc] then s.funcinfos[curfunc] = debug.getinfo(2, "nS") end local info = s.funcinfos[curfunc] local prompt = string.format("%s (%s)%s %s:%d\n", info.what, info.namewhat, info.name, info.short_src, line) io.write(prompt) debug.debug() end end