From 329433bc43990c40338bf29be635692c5ff0bc3a Mon Sep 17 00:00:00 2001 From: Lourival Vieira Neto Date: Tue, 24 Sep 2024 14:33:06 +0000 Subject: [PATCH] fix systrack synchronization * change luaprobe to sleep=false * split systrack into two runtimes (driver and probes) --- Makefile | 2 ++ README.md | 4 ++-- examples/systrack.lua | 33 ++++++++++++--------------------- examples/systrack/probes.lua | 30 ++++++++++++++++++++++++++++++ lib/luaprobe.c | 2 +- 5 files changed, 47 insertions(+), 24 deletions(-) create mode 100644 examples/systrack/probes.lua diff --git a/Makefile b/Makefile index ee08eb82..7aeb7911 100644 --- a/Makefile +++ b/Makefile @@ -54,6 +54,8 @@ examples_install: ${INSTALL} -m 0644 examples/dnsblock/*.lua ${SCRIPTS_INSTALL_PATH}/examples/dnsblock ${MKDIR} ${SCRIPTS_INSTALL_PATH}/examples/dnsdoctor ${INSTALL} -m 0644 examples/dnsdoctor/*.lua ${SCRIPTS_INSTALL_PATH}/examples/dnsdoctor + ${MKDIR} ${SCRIPTS_INSTALL_PATH}/examples/systrack + ${INSTALL} -m 0644 examples/systrack/*.lua ${SCRIPTS_INSTALL_PATH}/examples/systrack examples_uninstall: ${RM} -r ${SCRIPTS_INSTALL_PATH}/examples diff --git a/README.md b/README.md index c13eafa1..2651d587 100644 --- a/README.md +++ b/README.md @@ -1469,8 +1469,7 @@ hello kernel! [systrack](examples/systrack.lua) is a kernel script that implements a device driver to monitor system calls. -It prints the amount of times each [system call](examples/systrack.lua#L29) -was called since the driver has been installed. +It prints the amount of times each system call was called since the driver has been installed. #### Usage @@ -1484,6 +1483,7 @@ write: 1085 openat: 2036 read: 4131 readv: 0 +... ``` ### filter diff --git a/examples/systrack.lua b/examples/systrack.lua index 0607b2e0..7bfa900c 100644 --- a/examples/systrack.lua +++ b/examples/systrack.lua @@ -3,41 +3,32 @@ -- SPDX-License-Identifier: MIT OR GPL-2.0-only -- -local linux = require("linux") -local probe = require("probe") -local device = require("device") -local systab = require("syscall.table") - -local syscalls = {"openat", "read", "write", "readv", "writev", "close"} +local lunatik = require("lunatik") +local runner = require("lunatik.runner") +local linux = require("linux") +local device = require("device") +local rcu = require("rcu") local function nop() end -- do nothing local s = linux.stat local driver = {name = "systrack", open = nop, release = nop, mode = s.IRUGO} -local track = {} +local systrack = rcu.table() +lunatik._ENV.systrack = systrack + local toggle = true function driver:read() local log = "" if toggle then - for symbol, counter in pairs(track) do - log = log .. string.format("%s: %d\n", symbol, counter) - end + rcu.map(systrack, function (symbol, counter) + log = log .. string.format("%s: %d\n", symbol, counter:getnumber(0)) + end) end toggle = not toggle return log end -for _, symbol in ipairs(syscalls) do - local address = systab[symbol] - track[symbol] = 0 - - local function handler() - track[symbol] = track[symbol] + 1 - end - - probe.new(address, {pre = handler, post = nop}) -end - +runner.run("examples/systrack/probes", false) device.new(driver) diff --git a/examples/systrack/probes.lua b/examples/systrack/probes.lua new file mode 100644 index 00000000..fa9e3218 --- /dev/null +++ b/examples/systrack/probes.lua @@ -0,0 +1,30 @@ +-- +-- SPDX-FileCopyrightText: (c) 2023-2024 Ring Zero Desenvolvimento de Software LTDA +-- SPDX-License-Identifier: MIT OR GPL-2.0-only +-- + +local lunatik = require("lunatik") +local probe = require("probe") +local syscall = require("syscall.table") +local data = require("data") + +local systrack = lunatik._ENV.systrack + +local function nop() end -- do nothing + +local function inc(counter) + counter:setnumber(0, counter:getnumber(0) + 1) +end + +local sizeofnumber = string.packsize("n") + +for symbol, address in pairs(syscall) do + systrack[symbol] = data.new(sizeofnumber) + + local function handler() + inc(systrack[symbol]) + end + + probe.new(address, {pre = handler, post = nop}) +end + diff --git a/lib/luaprobe.c b/lib/luaprobe.c index 52edc8ce..d5bd0683 100644 --- a/lib/luaprobe.c +++ b/lib/luaprobe.c @@ -167,7 +167,7 @@ static const lunatik_class_t luaprobe_class = { .name = "probe", .methods = luaprobe_mt, .release = luaprobe_release, - .sleep = true, + .sleep = false, }; static int luaprobe_new(lua_State *L)