From 297943b01e18bb3d1dd41a8d3e220811de25228b Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 11:49:22 -0300 Subject: ztrace probe: BPF on syscalls, scheduling, kprobes, uprobes and USDT Programs built as instruction arrays for one process (no compiler, no libbpf), writing Events into a BPF ring buffer; a drop counter in a map; task_struct offsets from the kernel's BTF; a USDT probe's arguments read in the kernel at the probe. Without CAP_BPF and CAP_PERFMON it says so; --dry-run builds and lists everything. Tested offline by running the programs in a small BPF machine, and for real by test-kernel-vm, which boots this kernel in QEMU where we are root. ztrace perf: moments are no longer cut per event unless --cut; --max-size, and a full recording stops the target and says so. perf.Sorted merges two backends' events by time. Co-Authored-By: Claude Fable 5.2 --- src/cli/main.zig | 2 + src/cli/perf.zig | 60 ++++++++++++--- src/cli/probe.zig | 215 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 265 insertions(+), 12 deletions(-) create mode 100644 src/cli/probe.zig (limited to 'src/cli') diff --git a/src/cli/main.zig b/src/cli/main.zig index 9e6b0e5..554efde 100644 --- a/src/cli/main.zig +++ b/src/cli/main.zig @@ -8,6 +8,7 @@ const commands = .{ .dbg = @import("dbg.zig"), .run = @import("run.zig"), .perf = @import("perf.zig"), + .probe = @import("probe.zig"), .dump = @import("dump.zig"), .show = @import("show.zig"), .cc = @import("cc.zig"), @@ -20,6 +21,7 @@ const usage = \\ dbg debug a program, recording it: step, break, look, go back \\ run record a program that carries a ring producer (agent, instrumentation) \\ perf record what the kernel sees of a program at full speed (perf events) + \\ probe ... with BPF programs on syscalls, scheduling, kprobes, uprobes, USDT (root) \\ dump print a recording's events \\ show print a recording's summary, or the program's state at a moment \\ cc compile a program with instrumentation that records it diff --git a/src/cli/perf.zig b/src/cli/perf.zig index 57ad11a..58f7d77 100644 --- a/src/cli/perf.zig +++ b/src/cli/perf.zig @@ -30,10 +30,15 @@ pub const usage = \\ size) with the value found there; with :rw every access \\ --break WHAT ... on code: every time it runs, with the registers \\ --threads also the threads and children it creates + \\ --cut end a moment at every fault and every hit of --watch and + \\ --break: the state at each can be gone back to, for more bytes + \\ --max-size BYTES stop when the recording is this large (default 1 GiB) \\ --ring BYTES the kernel's buffer, per CPU (a power of two, default 512 KiB) \\ \\There are four debug registers. Only user space is seen: a page the \\kernel touches first (a read() into fresh memory) faults unseen. + \\Moments end where the recorder ends them anyway (a syscall's exit, a + \\megabyte of events), so that a profile or a storm of faults stays small. \\ ; @@ -44,6 +49,8 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { var out: []const u8 = "out.ztrace"; var options: zt.perf.Options = .{ .counters = &.{ .instructions, .cycles } }; var interval_ms: u32 = 0; + var cut = false; + var max_bytes: ?u64 = null; var specs: std.ArrayList(Spec) = .empty; var i: usize = 0; while (i < args.len) : (i += 1) { @@ -62,6 +69,8 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { options.syscalls = true; } else if (std.mem.eql(u8, arg, "--threads")) { options.follow = true; + } else if (std.mem.eql(u8, arg, "--cut")) { + cut = true; } else if (value == null) { fail(usage, .{}); } else if (std.mem.eql(u8, arg, "-o")) { @@ -79,6 +88,9 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { } else if (std.mem.eql(u8, arg, "--sample")) { options.sample_hz = std.fmt.parseInt(u32, value.?, 0) catch fail("--sample needs a rate\n", .{}); i += 1; + } else if (std.mem.eql(u8, arg, "--max-size")) { + max_bytes = std.fmt.parseInt(u64, value.?, 0) catch fail("--max-size needs bytes\n", .{}); + i += 1; } else if (std.mem.eql(u8, arg, "--ring")) { options.ring_bytes = std.fmt.parseInt(usize, value.?, 0) catch 0; if (!std.math.isPowerOfTwo(options.ring_bytes) or options.ring_bytes < 4096) fail("--ring needs a power of two, 4096 or more\n", .{}); @@ -105,8 +117,9 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { var proc = try zt.ptrace.Process.spawn(path, argv, init.minimal.environ.block.slice.ptr); errdefer proc.kill(); var rec = try zt.Recorder.create(init.gpa, init.io, out); + if (max_bytes) |bytes| rec.max_bytes = bytes; // What changes the state a moment holds (registers, memory) ends one. - rec.cut |= Kind.fault.bit() | Kind.probe.bit() | Kind.mem_write.bit() | Kind.mem_read.bit(); + if (cut) rec.cut |= Kind.fault.bit() | Kind.probe.bit() | Kind.mem_write.bit() | Kind.mem_read.bit(); var session = zt.perf.Session.open(init.gpa, proc.pid, options, rec.sink()) catch |err| switch (err) { error.PerfAccess => fail("perf_event_open was refused: kernel.perf_event_paranoid is above 2, or a security module forbids it\n", .{}), error.PerfNoSuchEvent => fail("perf_event_open: this CPU or kernel lacks one of the events asked for (in a VM without a PMU, try -e task-clock; --sample takes at most kernel.perf_event_max_sample_rate)\n", .{}), @@ -133,7 +146,19 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { zt.perf.release(proc); const start = zt.event.now(); - var next_read = start; + const exit_code: ?u64 = pump(&session, &rec, interval_ms) catch |err| switch (err) { + error.RecordingFull => null, + else => |e| return e, + }; + if (exit_code == null) proc.kill(); + const ns = zt.event.now() - start; + try finish(init, &rec, out, exit_code, session.lost, ns, options.counters, session.counts); +} + +/// Decodes until the target ends; its exit code. `error.RecordingFull` +/// when the recording reaches its limit first. +fn pump(session: *zt.perf.Session, rec: *zt.Recorder, interval_ms: u32) !u64 { + var next_read = zt.event.now(); while (true) { var wait_ms: i32 = 100; if (interval_ms != 0) { @@ -147,22 +172,33 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { if (!try session.poll(wait_ms)) break; } try session.readCounters(); + return exited(session.pid, rec.sink()); +} + +/// Waits for the target's end and records it. The exit code. +pub fn exited(pid: linux.pid_t, sink: zt.Sink) !u64 { var status: u32 = 0; - _ = linux.waitpid(proc.pid, @ptrCast(&status), 0); - const exit_code: u64 = if (linux.W.IFEXITED(status)) linux.W.EXITSTATUS(status) else 128 + @as(u64, @backingInt(linux.W.TERMSIG(status))); - const ns = zt.event.now() - start; - try rec.emit(.{ .ts = zt.event.now(), .tid = @intCast(proc.pid), .kind = .exit, .a = .{ exit_code, 0, 0, 0, 0, 0 } }, ""); + _ = linux.waitpid(pid, @ptrCast(&status), 0); + const code: u64 = if (linux.W.IFEXITED(status)) linux.W.EXITSTATUS(status) else 128 + @as(u64, @backingInt(linux.W.TERMSIG(status))); + try sink.emit(.{ .ts = zt.event.now(), .tid = @intCast(pid), .kind = .exit, .a = .{ code, 0, 0, 0, 0, 0 } }, ""); + return code; +} +/// Closes the recording and says what it holds; `exit_code` is null when +/// it filled up and the target was stopped for it. +pub fn finish(init: std.process.Init, rec: *zt.Recorder, out: []const u8, exit_code: ?u64, lost: u64, ns: u64, counters: []const zt.perf.Counter, counts: []const u64) !void { const events = rec.count; const moments = rec.moments + @intFromBool(rec.delta.items.len > 0); try rec.close(); const size = (try std.Io.Dir.cwd().statFile(init.io, out, .{})).size; - for (options.counters, session.counts) |c, count| std.debug.print("{d:>16} {t}\n", .{ count, c }); - std.debug.print("{s}: {d} moments, {d} events ({d} lost), {d} bytes, {d:.1} ms, {d:.0} events/s, exit {d}\n", .{ - out, moments, - events, session.lost, - size, @as(f64, @floatFromInt(ns)) / 1e6, - @as(f64, @floatFromInt(events)) / (@as(f64, @floatFromInt(ns)) / 1e9), exit_code, + const code = exit_code orelse fail("{s} is full at {d} bytes ({d} events) and the program was stopped: record fewer kinds of event, or allow more with --max-size BYTES\n", .{ out, size, events }); + for (counters, counts) |c, count| std.debug.print("{d:>16} {t}\n", .{ count, c }); + std.debug.print("{s}: {d} moments, {d} events ({d} lost), {d} bytes ({d:.1} an event), {d:.1} ms, {d:.0} events/s, exit {d}\n", .{ + out, moments, + events, lost, + size, @as(f64, @floatFromInt(size)) / @as(f64, @floatFromInt(@max(events, 1))), + @as(f64, @floatFromInt(ns)) / 1e6, @as(f64, @floatFromInt(events)) / (@as(f64, @floatFromInt(ns)) / 1e9), + code, }); } diff --git a/src/cli/probe.zig b/src/cli/probe.zig new file mode 100644 index 0000000..f0e2d12 --- /dev/null +++ b/src/cli/probe.zig @@ -0,0 +1,215 @@ +//! ztrace probe: BPF programs in the kernel watching a program: its +//! syscalls, its scheduling, any kernel or user function, static probes. +//! For root; --dry-run shows what it would do without being it. +const std = @import("std"); +const linux = std.os.linux; +const zt = @import("ztrace"); +const util = @import("util.zig"); +const perf = @import("perf.zig"); +const fail = util.fail; +const bpf = zt.bpf; + +pub const usage = + \\usage: ztrace probe [-o FILE] [OPTIONS] -- PROGRAM [ARGS] + \\ + \\Runs PROGRAM at full speed with BPF programs attached for it in the + \\kernel, into a recording (default out.ztrace). Needs CAP_BPF and + \\CAP_PERFMON, which root has. + \\ + \\ --syscalls every syscall, with the registers at its entry + \\ --sched every switch of a CPU to or from one of its threads + \\ --kprobe FUNCTION + \\ every call of a kernel function it makes: five arguments + \\ --uprobe [FILE:]SYMBOL + \\ every call of a function of its own, or of a library + \\ given by its file: five arguments, the registers + \\ --usdt [FILE:]PROVIDER:NAME + \\ a static probe (), with its own arguments + \\ --dry-run build the programs, find the places, print them; attach nothing + \\ --max-size BYTES stop when the recording is this large (default 1 GiB) + \\ --ring BYTES the ring the programs write to (a power of two, default 4 MiB) + \\ +; + +const Probe = struct { kind: enum { kprobe, uprobe, usdt }, what: [:0]const u8 }; + +pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { + const arena = init.arena.allocator(); + var out: []const u8 = "out.ztrace"; + var syscalls = false; + var sched = false; + var dry = false; + var max_bytes: ?u64 = null; + var ring_bytes: u32 = 1 << 22; + var probes: std.ArrayList(Probe) = .empty; + var i: usize = 0; + while (i < args.len) : (i += 1) { + const arg = args[i]; + const value: ?[:0]const u8 = if (i + 1 < args.len) args[i + 1] else null; + if (std.mem.eql(u8, arg, "--")) { + i += 1; + break; + } else if (std.mem.eql(u8, arg, "--syscalls")) { + syscalls = true; + } else if (std.mem.eql(u8, arg, "--sched")) { + sched = true; + } else if (std.mem.eql(u8, arg, "--dry-run")) { + dry = true; + } else if (value == null) { + fail(usage, .{}); + } else if (std.mem.eql(u8, arg, "-o")) { + out = value.?; + i += 1; + } else if (std.mem.eql(u8, arg, "--max-size")) { + max_bytes = std.fmt.parseInt(u64, value.?, 0) catch fail("--max-size needs bytes\n", .{}); + i += 1; + } else if (std.mem.eql(u8, arg, "--ring")) { + ring_bytes = std.fmt.parseInt(u32, value.?, 0) catch 0; + if (!std.math.isPowerOfTwo(ring_bytes) or ring_bytes < 4096) fail("--ring needs a power of two, 4096 or more\n", .{}); + i += 1; + } else if (std.meta.stringToEnum(@FieldType(Probe, "kind"), arg[@min(arg.len, 2)..])) |kind| { + try probes.append(arena, .{ .kind = kind, .what = value.? }); + i += 1; + } else fail(usage, .{}); + } + if (i >= args.len) fail(usage, .{}); + const path = std.mem.span(try util.resolve(init, args[i])); + + // Everything that can be wrong without the kernel's say is found first. + var places: std.ArrayList(Place) = .empty; + for (probes.items) |p| try places.append(arena, try place(arena, p, path)); + var offsets: [2]u32 = .{ 0, 0 }; + var btf: []const u8 = ""; + if (sched) { + btf = std.Io.Dir.cwd().readFileAlloc(init.io, "/sys/kernel/btf/vmlinux", arena, .unlimited) catch fail("--sched needs the kernel's type information, and /sys/kernel/btf/vmlinux cannot be read\n", .{}); + offsets = .{ + bpf.fieldOffset(btf, "task_struct", "pid") orelse fail("no task_struct.pid in the kernel's BTF\n", .{}), + bpf.fieldOffset(btf, "task_struct", "tgid") orelse fail("no task_struct.tgid in the kernel's BTF\n", .{}), + }; + } + var why: [512]u8 = undefined; + const missing = bpf.missing(&why); + + if (dry) { + // The process id and the maps are not there yet: 0 stands for them. + const maps: bpf.Maps = .{ .ring = 0, .drops = 0 }; + std.debug.print("for the process started from {s}:\n", .{path}); + if (syscalls) { + list("raw tracepoint sys_enter: a syscall_enter and the registers", bpf.sysEnter(maps, 0)); + list("raw tracepoint sys_exit: a syscall_exit", bpf.sysExit(maps, 0)); + } + if (sched) { + std.debug.print("task_struct.pid is at {d}, .tgid at {d} (BTF)\n", .{ offsets[0], offsets[1] }); + list("raw tracepoint sched_switch: a sched", bpf.schedSwitch(maps, 0, offsets[0], offsets[1])); + } + for (places.items) |p| { + std.debug.print("{t} {s}: {s} +{x}", .{ p.kind, p.name, p.target, p.offset }); + if (p.usdt) |u| std.debug.print(", semaphore at +{x}, arguments {s}", .{ if (u.semaphore == 0) 0 else p.elf.?.offset(u.semaphore) orelse 0, u.args }); + std.debug.print("\n", .{}); + const id = zt.perf.stringId(p.name); + list("its program: a probe and the registers", if (p.usdt) |u| bpf.usdtProgram(maps, 0, id, u.args) catch fail("{s}: an argument of this probe ({s}) is in a form not understood yet\n", .{ p.name, u.args }) else bpf.probe(maps, 0, id, null)); + } + std.debug.print("{s}\n", .{if (missing) |m| m else "nothing is missing to attach them"}); + return; + } + if (missing) |m| fail("ztrace probe: {s}\n", .{m}); + + const argv = try util.targetArgv(arena, args[i..]); + var proc = try zt.ptrace.Process.spawn(path, argv, init.minimal.environ.block.slice.ptr); + errdefer proc.kill(); + var rec = try zt.Recorder.create(init.gpa, init.io, out); + if (max_bytes) |bytes| rec.max_bytes = bytes; + // Two sources, the programs' ring and perf's: one sequence in the recording. + var sorted: zt.perf.Sorted = .{ .gpa = init.gpa, .to = rec.sink() }; + defer sorted.deinit(); + var tracer = bpf.Tracer.open(init.gpa, proc.pid, sorted.sink(), ring_bytes) catch |err| refused(err, null); + defer tracer.close(); + if (syscalls) tracer.syscalls() catch |err| refused(err, &tracer); + if (sched) tracer.sched(btf) catch |err| refused(err, &tracer); + for (places.items) |p| { + const id = zt.perf.stringId(p.name); + try sorted.sink().emit(.{ .ts = zt.event.now(), .kind = .string, .a = .{ id, 0, 0, 0, 0, 0 } }, p.name); + (if (p.usdt) |u| tracer.usdtProbe(p.elf.?, p.target, u, id) else switch (p.kind) { + .kprobe => tracer.kprobe(p.target, id), + else => tracer.uprobe(p.target, p.offset, id), + }) catch |err| switch (err) { + error.NoSuchProbe => fail("{s}: the kernel has no such place to probe\n", .{p.name}), + error.BadUsdtArg => fail("{s}: an argument of this probe ({s}) is in a form not understood yet\n", .{ p.name, p.usdt.?.args }), + else => |e| refused(e, &tracer), + }; + } + // The maps and the threads, which no program here reports. + var session = try zt.perf.Session.open(init.gpa, proc.pid, .{}, sorted.sink()); + defer session.close(); + + zt.perf.release(proc); + const start = zt.event.now(); + const exit_code: ?u64 = pump(&tracer, &session, &sorted) catch |err| switch (err) { + error.RecordingFull => null, + else => |e| return e, + }; + if (exit_code == null) proc.kill(); + try perf.finish(init, &rec, out, exit_code, tracer.lost() + session.lost, zt.event.now() - start, &.{}, &.{}); +} + +fn pump(tracer: *bpf.Tracer, session: *zt.perf.Session, sorted: *zt.perf.Sorted) !u64 { + // What both sources have reported past: the start of the round before. + var past: u64 = 0; + while (true) { + const now = zt.event.now(); + const alive = try session.poll(0); + const records = try tracer.poll(if (alive) 20 else 0); + try sorted.flush(past); + past = now; + if (records == 0 and !alive) break; + } + const code = try perf.exited(tracer.pid, sorted.sink()); + try sorted.flush(std.math.maxInt(u64)); + return code; +} + +fn refused(err: anyerror, tracer: ?*bpf.Tracer) noreturn { + const log = if (tracer) |t| std.mem.sliceTo(t.log, 0) else ""; + switch (err) { + error.PermissionDenied => fail("ztrace probe: bpf() refused though the capabilities are there: inside a user namespace or a container they do not count, the kernel wants them in the initial namespace\n", .{}), + error.UnsafeProgram, error.InvalidProgram => fail("ztrace probe: the kernel's verifier refused a program ({t}):\n{s}\n", .{ err, log }), + else => fail("ztrace probe: {t}\n{s}", .{ err, log }), + } +} + +/// Where a probe goes: a kernel function, or an offset into a file. +const Place = struct { + kind: @FieldType(Probe, "kind"), + name: []const u8, + target: [:0]const u8, + offset: u64 = 0, + elf: ?zt.usdt.Elf = null, + usdt: ?zt.usdt.Probe = null, +}; + +fn place(arena: std.mem.Allocator, p: Probe, program: [:0]const u8) !Place { + if (p.kind == .kprobe) return .{ .kind = .kprobe, .name = p.what, .target = p.what }; + // FILE: in front, or the program itself. + const colons = std.mem.count(u8, p.what, ":"); + const named = colons == @as(usize, if (p.kind == .usdt) 2 else 1); + const split = if (named) std.mem.indexOfScalar(u8, p.what, ':').? else 0; + const file = if (named) try arena.dupeSentinel(u8, p.what[0..split], 0) else program; + const name = p.what[if (named) split + 1 else 0..]; + const elf = zt.usdt.Elf.open(file) catch fail("{s}: not an ELF file that can be read\n", .{file}); + if (p.kind == .uprobe) { + const sym = elf.symbol(name) orelse fail("{s}: no symbol {s}\n", .{ file, name }); + return .{ .kind = .uprobe, .name = name, .target = file, .offset = elf.offset(sym.value) orelse fail("{s}: {s} is not in the file\n", .{ file, name }), .elf = elf }; + } + for (try zt.usdt.probes(arena, elf, 0)) |found| { + const colon = std.mem.indexOfScalar(u8, name, ':') orelse fail("--usdt wants [FILE:]PROVIDER:NAME\n", .{}); + if (!std.mem.eql(u8, found.provider, name[0..colon]) or !std.mem.eql(u8, found.name, name[colon + 1 ..])) continue; + return .{ .kind = .usdt, .name = name, .target = file, .offset = elf.offset(found.pc) orelse continue, .elf = elf, .usdt = found }; + } + fail("{s}: no static probe {s} (readelf -n lists them)\n", .{ file, name }); +} + +/// A program as the verifier numbers it: opcode, registers, offset, immediate. +fn list(what: []const u8, program: bpf.Program) void { + std.debug.print("{s}, {d} instructions\n", .{ what, program.len }); + for (program.code(), 0..) |insn, at| std.debug.print(" {d:>3}: {x:0>2} r{d} r{d} {d:>5} {d}\n", .{ at, insn.code, insn.dst, insn.src, insn.off, insn.imm }); +} -- cgit v1.3