diff options
Diffstat (limited to 'tex/context/base/mkiv/util-deb.lua')
-rw-r--r-- | tex/context/base/mkiv/util-deb.lua | 83 |
1 files changed, 61 insertions, 22 deletions
diff --git a/tex/context/base/mkiv/util-deb.lua b/tex/context/base/mkiv/util-deb.lua index b8db0c583..6932e8804 100644 --- a/tex/context/base/mkiv/util-deb.lua +++ b/tex/context/base/mkiv/util-deb.lua @@ -10,9 +10,6 @@ if not modules then modules = { } end modules ['util-deb'] = { -- bound to a variable, like node.new, node.copy etc (contrary to for instance -- node.has_attribute which is bound to a has_attribute local variable in mkiv) -local debug = require "debug" - -local getinfo, sethook = debug.getinfo, debug.sethook local type, next, tostring, tonumber = type, next, tostring, tonumber local format, find, sub, gsub = string.format, string.find, string.sub, string.gsub local insert, remove, sort = table.insert, table.remove, table.sort @@ -98,7 +95,7 @@ end setmetatableindex(names,function(t,name) local v = setmetatableindex(function(t,source) local v = setmetatableindex(function(t,line) - local v = { total = 0, count = 0 } + local v = { total = 0, count = 0, nesting = 0 } t[line] = v return v end) @@ -109,6 +106,9 @@ setmetatableindex(names,function(t,name) return v end) +local getinfo = nil +local sethook = nil + local function hook(where) local f = getinfo(2,"nSl") if f then @@ -128,12 +128,24 @@ local function hook(where) end local data = names[name][source][line] if where == "call" then - data.count = data.count + 1 - insert(data,ticks()) + local nesting = data.nesting + if nesting == 0 then + data.count = data.count + 1 + insert(data,ticks()) + data.nesting = 1 + else + data.nesting = nesting + 1 + end elseif where == "return" then - local t = remove(data) - if t then - data.total = data.total + ticks() - t + local nesting = data.nesting + if nesting == 1 then + local t = remove(data) + if t then + data.total = data.total + ticks() - t + end + data.nesting = 0 + else + data.nesting = nesting - 1 end end end @@ -228,6 +240,27 @@ function debugger.showstats(printer,threshold) -- table.save("luatex-profile.lua",names) end +local function getdebug() + if sethook and getinfo then + return + end + if not debug then + local okay + okay, debug = pcall(require,"debug") + end + if type(debug) ~= "table" then + return + end + getinfo = debug.getinfo + sethook = debug.sethook + if type(getinfo) ~= "function" then + getinfo = nil + end + if type(sethook) ~= "function" then + sethook = nil + end +end + function debugger.savestats(filename,threshold) local f = io.open(filename,'w') if f then @@ -237,7 +270,8 @@ function debugger.savestats(filename,threshold) end function debugger.enable() - if nesting == 0 then + getdebug() + if sethook and getinfo and nesting == 0 then running = true if initialize then initialize() @@ -259,7 +293,7 @@ function debugger.disable() if nesting > 0 then nesting = nesting - 1 end - if nesting == 0 then + if sethook and getinfo and nesting == 0 then sethook() end end @@ -282,20 +316,25 @@ end -- from the lua book: local function showtraceback(rep) -- from lua site / adapted - local level = 2 -- we don't want this function to be reported - local reporter = rep or report - while true do - local info = getinfo(level, "Sl") - if not info then - break - elseif info.what == "C" then - reporter("%2i : %s",level-1,"C function") - else - reporter("%2i : %s : %s",level-1,info.short_src,info.currentline) + getdebug() + if getinfo then + local level = 2 -- we don't want this function to be reported + local reporter = rep or report + while true do + local info = getinfo(level, "Sl") + if not info then + break + elseif info.what == "C" then + reporter("%2i : %s",level-1,"C function") + else + reporter("%2i : %s : %s",level-1,info.short_src,info.currentline) + end + level = level + 1 end - level = level + 1 end end debugger.showtraceback = showtraceback -- debug.showtraceback = showtraceback + +-- showtraceback() |