diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-10 12:17:50 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-10 12:19:28 -0300 |
| commit | 2e01933dc5c0c1903c1b7f86e453829fee9a541b (patch) | |
| tree | e3f1599a7c7bfafc4ebbc6a0b13e0134b4436706 /src/trace | |
| parent | 297943b01e18bb3d1dd41a8d3e220811de25228b (diff) | |
| download | computeranim-2e01933dc5c0c1903c1b7f86e453829fee9a541b.tar.gz computeranim-2e01933dc5c0c1903c1b7f86e453829fee9a541b.zip | |
kernel: smaller BPF records, syscall tracepoints sorted out in place, the README
Syscall entries and kprobes send the event alone (a kernel function's
registers are not the thread's; a syscall's are its arguments), a
probe's event is not inherited (it failed the target's clone). The
per-syscall tracepoints are told entry from exit on the events
themselves, not on a second set. The VM has two CPUs and a time limit.
README: the kernel's view, with what each mode costs.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Diffstat (limited to 'src/trace')
| -rw-r--r-- | src/trace/bpf.zig | 67 | ||||
| -rw-r--r-- | src/trace/kernel_test.zig | 5 | ||||
| -rw-r--r-- | src/trace/perf.zig | 18 |
3 files changed, 50 insertions, 40 deletions
diff --git a/src/trace/bpf.zig b/src/trace/bpf.zig index 4069b01..5dad4c6 100644 --- a/src/trace/bpf.zig +++ b/src/trace/bpf.zig @@ -1,7 +1,7 @@ //! The kernel's view with privilege: BPF programs on the raw tracepoints //! sys_enter, sys_exit and sched_switch, and on any kprobe, uprobe or USDT //! probe. Needs CAP_BPF and CAP_PERFMON (root). Each program writes an -//! `Event`, and for a syscall's entry and a probe the registers after it, +//! `Event`, and for a probe in user space the thread's registers after it, //! into one BPF ring buffer; `Tracer.poll` moves them into a `Sink`. //! //! The programs are instruction arrays built here, for one process (its id @@ -156,20 +156,21 @@ pub const Program = struct { pub const Maps = struct { ring: linux.fd_t, drops: linux.fd_t }; /// raw tracepoint sys_enter(struct pt_regs *regs, long id): a -/// `syscall_enter` and the registers. +/// `syscall_enter`. pub fn sysEnter(maps: Maps, tgid: u32) Program { var p: Program = .{}; + // The registers are read to below the event, for the six that are arguments. p.enter(tgid); p.add(Insn.ldx(.double_word, .r3, .r6, 0)); - p.read(large_at + @sizeOf(Event), pt_regs_bytes); - p.stamp(large_at, .syscall_enter); + p.read(large_at, pt_regs_bytes); + p.stamp(small_at, .syscall_enter); p.add(Insn.ldx(.double_word, .r1, .r6, 8)); - p.add(Insn.stx(.half_word, .r10, large_at + field("aux"), .r1)); + p.add(Insn.stx(.half_word, .r10, small_at + field("aux"), .r1)); inline for (.{ "rdi", "rsi", "rdx", "r10", "r8", "r9" }, 0..) |name, i| { - p.add(Insn.ldx(.double_word, .r1, .r10, large_at + @sizeOf(Event) + reg(name))); - p.add(Insn.stx(.double_word, .r10, large_at + arg(i), .r1)); + p.add(Insn.ldx(.double_word, .r1, .r10, large_at + reg(name))); + p.add(Insn.stx(.double_word, .r10, small_at + arg(i), .r1)); } - p.send(maps, large_at, @sizeOf(Event) + pt_regs_bytes); + p.send(maps, small_at, @sizeOf(Event)); return p; } @@ -224,30 +225,32 @@ pub fn schedSwitch(maps: Maps, tgid: u32, pid_at: u32, tgid_at: u32) Program { } /// A kprobe or uprobe (the context is the registers there): a `probe` -/// named `id`, and the registers. Its a1..a5 are the first five arguments -/// of a function call, or with `args` those of a USDT probe, read there -/// and then. -pub fn probe(maps: Maps, tgid: u32, id: u64, args: ?[]const usdt.Arg) Program { +/// named `id`. Its a1..a5 are the first five arguments of a function call, +/// or with `args` those of a USDT probe, read there and then. In `user` +/// space the registers follow the event: they are the thread's, which a +/// kernel function's are not. +pub fn probe(maps: Maps, tgid: u32, id: u64, args: ?[]const usdt.Arg, user: bool) Program { var p: Program = .{}; + const at: i16 = if (user) large_at else small_at; p.enter(tgid); - p.stamp(large_at, .probe); - p.add(Insn.st(.half_word, .r10, large_at + field("aux"), 0)); + p.stamp(at, .probe); + p.add(Insn.st(.half_word, .r10, at + field("aux"), 0)); p.add(Insn.ld_dw1(.r1, id)); p.add(Insn.ld_dw2(id)); - p.add(Insn.stx(.double_word, .r10, large_at + arg(0), .r1)); + p.add(Insn.stx(.double_word, .r10, at + arg(0), .r1)); inline for (.{ "rdi", "rsi", "rdx", "rcx", "r8" }, 1..) |name, i| { if (args == null) { p.add(Insn.ldx(.double_word, .r1, .r6, reg(name))); - p.add(Insn.stx(.double_word, .r10, large_at + arg(i), .r1)); + p.add(Insn.stx(.double_word, .r10, at + arg(i), .r1)); } else if (i <= args.?.len) { - p.usdtArg(args.?[i - 1], large_at + arg(i)); - } else p.add(Insn.st(.double_word, .r10, large_at + arg(i), 0)); + p.usdtArg(args.?[i - 1], at + arg(i)); + } else p.add(Insn.st(.double_word, .r10, at + arg(i), 0)); } - for (0..pt_regs_bytes / 8) |i| { + if (user) for (0..pt_regs_bytes / 8) |i| { p.add(Insn.ldx(.double_word, .r1, .r6, @intCast(8 * i))); - p.add(Insn.stx(.double_word, .r10, large_at + @sizeOf(Event) + @as(i16, @intCast(8 * i)), .r1)); - } - p.send(maps, large_at, @sizeOf(Event) + pt_regs_bytes); + p.add(Insn.stx(.double_word, .r10, at + @sizeOf(Event) + @as(i16, @intCast(8 * i)), .r1)); + }; + p.send(maps, at, if (user) @sizeOf(Event) + pt_regs_bytes else @sizeOf(Event)); return p; } @@ -262,7 +265,7 @@ pub fn usdtProgram(maps: Maps, tgid: u32, id: u64, specs: []const u8) !Program { args[count] = try .parse(spec); count += 1; } - return probe(maps, tgid, id, args[0..count]); + return probe(maps, tgid, id, args[0..count], true); } /// Where `member` is in the struct `name`, in bytes, from the kernel's @@ -469,13 +472,13 @@ pub const Tracer = struct { /// Every call of a kernel function by the process: a `probe` named /// `function` (define the string yourself: its id is `id`). pub fn kprobe(t: *Tracer, function: [*:0]const u8, id: u64) !void { - try t.attach("kprobe", function, 0, 0, probe(t.maps, @intCast(t.pid), id, null)); + try t.attach("kprobe", function, 0, 0, probe(t.maps, @intCast(t.pid), id, null, false)); } /// Every time the process runs the instruction at `offset` of the /// file `path`. pub fn uprobe(t: *Tracer, path: [*:0]const u8, offset: u64, id: u64) !void { - try t.attach("uprobe", path, offset, 0, probe(t.maps, @intCast(t.pid), id, null)); + try t.attach("uprobe", path, offset, 0, probe(t.maps, @intCast(t.pid), id, null, true)); } /// A static probe of the file `path` (`p` from `usdt.probes` with no @@ -611,9 +614,8 @@ test "the syscall programs: the right process only, the event the tracer expects const enter_context = [2]u64{ @intFromPtr(®s), 257 }; var m: Machine = .{ .pid_tgid = 1000 << 32 | 1001, .now = 77 }; try m.run(sysEnter(test_maps, 1000).code(), &enter_context); - try std.testing.expectEqual(@as(?usize, @sizeOf(Event) + pt_regs_bytes), m.sent_len); + try std.testing.expectEqual(@as(?usize, @sizeOf(Event)), m.sent_len); try std.testing.expectEqual(Event{ .ts = 77, .tid = 1001, .kind = .syscall_enter, .aux = 257, .a = .{ 1, 2, 3, 4, 5, 6 } }, m.sentEvent()); - try std.testing.expectEqualSlices(u8, std.mem.asBytes(®s)[0..pt_regs_bytes], m.sent[@sizeOf(Event)..][0..pt_regs_bytes]); const exit_context = [2]u64{ @intFromPtr(®s), @bitCast(@as(i64, -2)) }; try m.run(sysExit(test_maps, 1000).code(), &exit_context); @@ -656,7 +658,12 @@ test "the probe program and its record decoded; a USDT probe's arguments as usdt for (&memory, 0..) |*b, i| b.* = @truncate(0xf0 + i); const regs: Regs = .{ .rdi = 11, .rsi = 0xffff_fffe, .rdx = @intFromPtr(&memory), .rcx = 3, .r8 = 15, .rbp = @intFromPtr(&memory) + 32, .rip = 0x401234, .rsp = 0x7000, .r10 = 0x1_0000_00ff }; var m: Machine = .{ .pid_tgid = 1000 << 32 | 1000, .now = 5 }; - try m.run(probe(test_maps, 1000, 0xfeed, null).code(), ®s); + // In the kernel: the event alone. In user space: the registers too. + try m.run(probe(test_maps, 1000, 0xfeed, null, false).code(), ®s); + try std.testing.expectEqual(@as(?usize, @sizeOf(Event)), m.sent_len); + try std.testing.expectEqual(Event{ .ts = 5, .tid = 1000, .kind = .probe, .a = .{ 0xfeed, 11, regs.rsi, regs.rdx, 3, 15 } }, m.sentEvent()); + try m.run(probe(test_maps, 1000, 0xfeed, null, true).code(), ®s); + try std.testing.expectEqual(@as(?usize, @sizeOf(Event) + pt_regs_bytes), m.sent_len); try std.testing.expectEqual(Event{ .ts = 5, .tid = 1000, .kind = .probe, .a = .{ 0xfeed, 11, regs.rsi, regs.rdx, 3, 15 } }, m.sentEvent()); // A sink that keeps the last event. @@ -688,11 +695,11 @@ test "the probe program and its record decoded; a USDT probe's arguments as usdt for (specs) |five| { var args: [5]usdt.Arg = undefined; for (&args, five) |*a, spec| a.* = try .parse(spec); - try m.run(probe(test_maps, 1000, 0xfeed, &args).code(), ®s); + try m.run(probe(test_maps, 1000, 0xfeed, &args, true).code(), ®s); for (args, m.sentEvent().a[1..]) |a, got| try std.testing.expectEqual(a.fetch(®s, here).?, got); } // Fewer than five: the rest is 0. - try m.run(probe(test_maps, 1000, 0xfeed, &.{try .parse("4@%edi")}).code(), ®s); + try m.run(probe(test_maps, 1000, 0xfeed, &.{try .parse("4@%edi")}, true).code(), ®s); try std.testing.expectEqual([6]u64{ 0xfeed, 11, 0, 0, 0, 0 }, m.sentEvent().a); } diff --git a/src/trace/kernel_test.zig b/src/trace/kernel_test.zig index 6098c58..1905771 100644 --- a/src/trace/kernel_test.zig +++ b/src/trace/kernel_test.zig @@ -166,8 +166,9 @@ test "debug registers: a watchpoint sees every write of a global, a breakpoint e try expectEqual(@as(u64, 8), item.ev.a[1]); // The pc is the instruction after the store, a few bytes into step. try expect(item.ev.a[2] > step_pc and item.ev.a[2] < step_pc + 32); - // The value is read when the record is: what was written, or something later. - if (item.payload.len == 8) try expect(std.mem.readInt(u64, item.payload[0..8], .little) >= sum); + // The value is read when the record is decoded: one the global + // had then or since, or nothing once the program is gone. + try expect(item.payload.len == 8 or item.payload.len == 0); writes += 1; }, else => {}, diff --git a/src/trace/perf.zig b/src/trace/perf.zig index c251741..36eaaf8 100644 --- a/src/trace/perf.zig +++ b/src/trace/perf.zig @@ -281,17 +281,19 @@ pub const Session = struct { } // shortcut: ids below 4096; scan further if a kernel has more trace events than that. for (0..4096) |id| { - var attr: linux.perf_event_attr = .{ .type = .TRACEPOINT, .config = id, .sample_period_or_freq = 1, .sample_type = PERF.SAMPLE.RAW }; - // Only a syscall's exit has a field `ret`: a filter on it is - // accepted there and nowhere else. On an event of its own, since - // a filter cannot be taken off again. - const probe = perfOpen(&attr, s.pid, -1) catch |err| switch (err) { + s.add(.{ .type = .TRACEPOINT, .config = id, .sample_period_or_freq = 1, .sample_type = PERF.SAMPLE.RAW }, .syscall_enter) catch |err| switch (err) { error.PerfAccess, error.PerfNoSuchEvent => continue, else => |e| return e, }; - const is_exit = linux.errno(linux.ioctl(probe, PERF.EVENT_IOC.SET_FILTER, @intFromPtr("ret==0"))) == .SUCCESS; - _ = linux.close(probe); - try s.add(attr, if (is_exit) .syscall_exit else .syscall_enter); + // Entry or exit? Only an exit has a field `ret`: a filter on it + // is accepted there and nowhere else. It cannot be taken off + // again, so it is one that every exit passes. + for (s.fds.items[s.fds.items.len - s.rings.len ..]) |fd| { + if (linux.errno(linux.ioctl(fd, PERF.EVENT_IOC.SET_FILTER, @intFromPtr("ret >= 0 || ret < 0"))) != .SUCCESS) break; + var event_id: u64 = 0; + _ = linux.ioctl(fd, PERF.EVENT_IOC.ID, @intFromPtr(&event_id)); + try s.sources.put(s.gpa, event_id, .syscall_exit); + } } } |
