Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix systrack synchronization #199

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -1484,6 +1483,7 @@ write: 1085
openat: 2036
read: 4131
readv: 0
...
```

### filter
Expand Down
33 changes: 12 additions & 21 deletions examples/systrack.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

30 changes: 30 additions & 0 deletions examples/systrack/probes.lua
Original file line number Diff line number Diff line change
@@ -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

2 changes: 1 addition & 1 deletion lib/luaprobe.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down