localfunctionsetbreakpoint(func, line) local s = status iftype(func) ~= "function"or ( line andtype(line) ~= "number") then io.write("invalid parameter\n") returnnil end
-- get func info local info = getfuncinfo(func) ifnot info then io.write("unable to get func info\n") returnnil end
-- verify the line line = verifyfuncline(info, line) ifnot line then io.write("invalid line\n") returnnil end -- 省略 end
localfunctiongetfuncinfo(func, level) local s = status local info = s.funcinfos[func] ifnot info then if level then s.funcinfos[func] = debug.getinfo(level + 1, "nSL") else s.funcinfos[func] = debug.getinfo(func, "SL") end info = s.funcinfos[func] info.sortedlines = {} for k, _ inpairs(info.activelines) do table.insert(info.sortedlines, k) end table.sort(info.sortedlines) elseif level then-- name和namewhat需要实时获取 local nameinfo = debug.getinfo(level + 1, "n") info.name = nameinfo.name info.namewhat = nameinfo.namewhat end return info end
localfunctionverifyfuncline(info, line) ifnot line then return info.sortedlines[1] end if line < info.linedefined or line > info.lastlinedefined then returnnil end for _, v inipairs(info.sortedlines) do if v >= line then return v end end assert(false) -- impossible to reach here end
localfunctionhook(event, line) -- 省略 elseif event == "line"then local curfunc = s.stackinfos[s.stackdepth].func local funcbp = s.funcbpt[curfunc] assert(funcbp) if funcbp[line] then local info = getfuncinfo(curfunc, 2) 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
localfunctionsortlines(func) local info = debug.getinfo(func, "nSL") info.sortedlines = {} for k, v inpairs(info.activelines) do print(k, v) table.insert(info.sortedlines, k) end
table.sort(info.sortedlines)
for k, v inipairs(info.sortedlines) do print(k, v) end end