summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cli/probe.zig7
-rw-r--r--src/trace/bpf.zig67
-rw-r--r--src/trace/kernel_test.zig5
-rw-r--r--src/trace/perf.zig18
4 files changed, 54 insertions, 43 deletions
diff --git a/src/cli/probe.zig b/src/cli/probe.zig
index f0e2d12..539af5c 100644
--- a/src/cli/probe.zig
+++ b/src/cli/probe.zig
@@ -16,7 +16,7 @@ pub const usage =
\\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
+ \\ --syscalls every syscall
\\ --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
@@ -95,7 +95,7 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void {
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_enter: a syscall_enter", bpf.sysEnter(maps, 0));
list("raw tracepoint sys_exit: a syscall_exit", bpf.sysExit(maps, 0));
}
if (sched) {
@@ -107,7 +107,7 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void {
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));
+ list("its program: a probe", 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, p.kind != .kprobe));
}
std.debug.print("{s}\n", .{if (missing) |m| m else "nothing is missing to attach them"});
return;
@@ -172,6 +172,7 @@ 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.SystemResources => fail("ztrace probe: the kernel has no memory to spare for a ring that large (--ring)\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 }),
}
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(&regs), 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(&regs)[0..pt_regs_bytes], m.sent[@sizeOf(Event)..][0..pt_regs_bytes]);
const exit_context = [2]u64{ @intFromPtr(&regs), @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(), &regs);
+ // In the kernel: the event alone. In user space: the registers too.
+ try m.run(probe(test_maps, 1000, 0xfeed, null, false).code(), &regs);
+ 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(), &regs);
+ 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(), &regs);
+ try m.run(probe(test_maps, 1000, 0xfeed, &args, true).code(), &regs);
for (args, m.sentEvent().a[1..]) |a, got| try std.testing.expectEqual(a.fetch(&regs, here).?, got);
}
// Fewer than five: the rest is 0.
- try m.run(probe(test_maps, 1000, 0xfeed, &.{try .parse("4@%edi")}).code(), &regs);
+ try m.run(probe(test_maps, 1000, 0xfeed, &.{try .parse("4@%edi")}, true).code(), &regs);
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);
+ }
}
}