From: Tobias Ulmer Date: Thu, 24 Oct 2013 10:28:17 +0000 (+0200) Subject: Add trace module, used for logging all function calls X-Git-Tag: e2factory-2.3.15rc1~441 X-Git-Url: https://git.e2factory.org/?a=commitdiff_plain;h=a4b8944e579cf74f553186ca0218e5fe696a3665;p=e2factory.git Add trace module, used for logging all function calls Remove previous implementation from e2lib and move it into a free-standing module. Extend so it can be disabled and enabled when convenient. Signed-off-by: Tobias Ulmer --- diff --git a/generic/Makefile b/generic/Makefile index 37d8ea5..ebde45a 100644 --- a/generic/Makefile +++ b/generic/Makefile @@ -31,7 +31,7 @@ include $(TOPLEVEL)/make.vars LUA_LIBS = strict.lua plugin.lua e2lib.lua LUA_LIBS += e2option.lua hash.lua tools.lua transport.lua cache.lua url.lua -LUA_LIBS += generic_git.lua eio.lua err.lua lock.lua errno.lua +LUA_LIBS += generic_git.lua eio.lua err.lua lock.lua errno.lua trace.lua SO_LIBS = sha1.so leio.so le2lib.so CLEAN_FILES = *~ *.o *.so diff --git a/generic/e2lib.lua b/generic/e2lib.lua index a6d2785..41f1761 100644 --- a/generic/e2lib.lua +++ b/generic/e2lib.lua @@ -56,6 +56,7 @@ local tools = require("tools") local cache = require("cache") local eio = require("eio") local le2lib = require("le2lib") +local trace = require("trace") -- Module-level global variables -- @@ -318,9 +319,8 @@ end -- @return Error object on failure. function e2lib.init() e2lib.log(4, "e2lib.init()") - -- DEBUG: change to "cr" to log return from function - local traceflags = os.getenv("E2_TRACE") or "c" - debug.sethook(e2lib.tracer, traceflags) + + trace.enable() local rc, re = e2lib.signal_reset() if not rc then @@ -439,74 +439,6 @@ function e2lib.init2() return true end ---- function call tracer --- @param event string: type of event --- @param line line number of event (unused) -function e2lib.tracer(event, line) - local ftbl = debug.getinfo(2) - if ftbl == nil or ftbl.name == nil then - return - end - - -- approximate module name, not always accurate but good enough - local module - if ftbl.source == nil or ftbl.source == "=[C]" then - module = "C." - -- DEBUG: comment this to see all C calls. - return - else - module = string.match(ftbl.source, "(%w+%.)lua$") - if module == nil then - module = "." - -- DEBUG: comment this to see all unknown calls. - return - end - end - - if event == "call" then - local out = string.format("%s%s(", module, ftbl.name) - for lo = 1, 10 do - local name, value = debug.getlocal(2, lo) - if name == nil or name == "(*temporary)" then - break - end - if lo > 1 then - out = out .. ", " - end - - if type(value) == "string" then - local isbinary = false - - -- check the first 40 bytes for values common in binary data - for _,v in ipairs({string.byte(value, 1, 41)}) do - if (v >= 0 and v < 9) or (v > 13 and v < 32) then - isbinary = true - break - end - end - - if isbinary then - out = string.format("%s%s=", out, name) - else - local svalue = string.sub(value, 0, 800) - if string.len(value) > string.len(svalue) then - svalue = svalue .. "..." - end - - out = string.format("%s%s=\"%s\"", out, name, svalue) - end - else - out = string.format("%s%s=%s", out, name, tostring(value)) - end - - end - out = out .. ")" - e2lib.log(4, out) - else - e2lib.logf(4, "< %s%s", module, ftbl.name) - end -end - --- Print a warning, composed by concatenating all arguments to a string. -- @param ... any number of strings -- @return nil diff --git a/generic/trace.lua b/generic/trace.lua new file mode 100644 index 0000000..2c9cecf --- /dev/null +++ b/generic/trace.lua @@ -0,0 +1,108 @@ +--- Function call tracing +-- @module generic.trace + +-- Copyright (C) 2013 emlix GmbH, see file AUTHORS +-- +-- This file is part of e2factory, the emlix embedded build system. +-- For more information see http://www.e2factory.org +-- +-- e2factory is a registered trademark of emlix GmbH. +-- +-- e2factory is free software: you can redistribute it and/or modify it under +-- the terms of the GNU General Public License as published by the +-- Free Software Foundation, either version 3 of the License, or (at your +-- option) any later version. +-- +-- This program is distributed in the hope that it will be useful, but WITHOUT +-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +-- FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +-- more details. + +local trace = {} +local e2lib = require("e2lib") +local strict = require("strict") + +--- Function call tracer. Logs all function calls at debug level while active. +-- @param event string: type of event +-- @param line line number of event (unused) +local function tracer(event, line) + local ftbl, module, out, name, value, isbinary, svalue + + ftbl = debug.getinfo(2) + if ftbl == nil or ftbl.name == nil then + return + end + + -- approximate module name, not always accurate but good enough + if ftbl.source == nil or ftbl.source == "=[C]" then + module = "C." + -- DEBUG: comment this to see all C calls. + return + else + module = string.match(ftbl.source, "(%w+%.)lua$") + if module == nil then + module = "." + -- DEBUG: comment this to see all unknown calls. + return + end + end + + if event == "call" then + out = string.format("%s%s(", module, ftbl.name) + for lo = 1, 10 do + name, value = debug.getlocal(2, lo) + if name == nil or name == "(*temporary)" then + break + end + if lo > 1 then + out = out .. ", " + end + + if type(value) == "string" then + isbinary = false + + -- check the first 40 bytes for values common in binary data + for _,v in ipairs({string.byte(value, 1, 41)}) do + if (v >= 0 and v < 9) or (v > 13 and v < 32) then + isbinary = true + break + end + end + + if isbinary then + out = string.format("%s%s=", out, name) + else + svalue = string.sub(value, 0, 800) + if string.len(value) > string.len(svalue) then + svalue = svalue .. "..." + end + + out = string.format("%s%s=\"%s\"", out, name, svalue) + end + else + out = string.format("%s%s=%s", out, name, tostring(value)) + end + + end + out = out .. ")" + e2lib.log(4, out) + else + e2lib.logf(4, "< %s%s", module, ftbl.name) + end +end + +--- Enable function call tracer. +function trace.enable() + e2lib.log(4, "trace.enable()") + local flags = os.getenv("E2_TRACE") or "c" + debug.sethook(tracer, flags) +end + +--- Disable function call tracer. +function trace.disable() + debug.sethook() +end + +return strict.lock(trace) + +-- vim:sw=4:sts=4:et: