diff options
| -rw-r--r-- | README.md | 4 | ||||
| -rw-r--r-- | build/kernel.zig | 4 | ||||
| -rw-r--r-- | src/cli/perf.zig | 53 | ||||
| -rw-r--r-- | src/cli/probe.zig | 31 | ||||
| -rw-r--r-- | src/trace/bpf.zig | 5 | ||||
| -rw-r--r-- | src/trace/kernel_test.zig | 11 | ||||
| -rw-r--r-- | src/trace/perf.zig | 39 |
7 files changed, 71 insertions, 76 deletions
@@ -361,7 +361,9 @@ otherwise; `--dry-run` builds everything and lists it. BPF programs, built here as instruction arrays for one process, on the raw tracepoints `sys_enter`, `sys_exit` and `sched_switch` (both threads of a switch, through the kernel's BTF), on kprobes, on uprobes, and on USDT probes, -whose arguments are read in the kernel at the probe. They write `Event`s +whose arguments are read in the kernel at the probe (the program's own +semaphores are raised by writing them: the kernel's counter misses when +lld has mapped that page of the file twice). They write `Event`s into one BPF ring buffer and count what did not fit. A syscall costs 0.15 us, a probe 0.5 to 0.8 (in a VM), 22 to 73 bytes an event. diff --git a/build/kernel.zig b/build/kernel.zig index bbed126..aae593a 100644 --- a/build/kernel.zig +++ b/build/kernel.zig @@ -27,6 +27,8 @@ pub fn add(ctx: Ctx) void { .imports = &.{ .{ .name = "ztrace", .module = ctx.ztrace }, .{ .name = "targets", .module = paths.createModule() } }, }) }); step.dependOn(&b.addRunArtifact(tests).step); + // ... and the library's own tests of this area: what needs no target. + step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = ctx.ztrace, .filters = &.{ "perf.test", "usdt.test", "bpf.test" } })).step); // The BPF side for real: this machine's kernel booted in QEMU, where we // are root. Not part of `test`: it wants qemu, /dev/kvm, cpio and a @@ -47,7 +49,7 @@ pub fn add(ctx: Ctx) void { // in libc; step(1002, 2001) in the thread; tick(3, 14) with its semaphore. for ([_][]const u8{ "(0 lost)", " syscall_enter ", " aux=230 ", " sched ", - " a=7166cdb822920ddd 3eb ", " a=a02712eb14a9b985 3ea 7d1 ", " a=7f4909df02928d94 3 e ", + " a=f166cdb822920ddd 3eb ", " a=a02712eb14a9b985 3ea 7d1 ", " a=ff4909df02928d94 3 e ", }) |expected| vm.expectStdOutMatch(expected); b.step("test-kernel-vm", "Run the BPF programs as root in a QEMU VM of this kernel").dependOn(&vm.step); } diff --git a/src/cli/perf.zig b/src/cli/perf.zig index 58f7d77..ea211fe 100644 --- a/src/cli/perf.zig +++ b/src/cli/perf.zig @@ -115,7 +115,7 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { const argv = try util.targetArgv(arena, args[i..]); // Held at its first instruction until the events are on it. var proc = try zt.ptrace.Process.spawn(path, argv, init.minimal.environ.block.slice.ptr); - errdefer proc.kill(); + errdefer proc.close(); 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. @@ -127,19 +127,26 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { }; defer session.close(); + // Symbols: the program's own from the start, a library's once the + // loader has mapped it, which is at the program's entry point. + var symbols: zt.Symbols = .{ .gpa = init.gpa, .io = init.io }; + defer symbols.deinit(); + symbols.free(try symbols.refresh(proc.pid)); var at_entry = false; for (specs.items) |spec| { - const sym: zt.perf.Symbol = if (std.fmt.parseInt(u64, spec.what, 0)) |addr| .{ .addr = addr, .size = 8 } else |_| found: { - if (try zt.perf.lookup(init.gpa, proc.pid, spec.what)) |sym| break :found sym; - // A library's symbol: there once the loader has mapped it. - if (!at_entry) try runToEntry(&proc); - at_entry = true; - break :found try zt.perf.lookup(init.gpa, proc.pid, spec.what) orelse fail("{s}: no such symbol in what the program has mapped\n", .{spec.what}); + const addr, const size = if (std.fmt.parseInt(u64, spec.what, 0)) |addr| .{ addr, 8 } else |_| found: { + if (symbols.find(spec.what) == null and !at_entry) { + try runTo(&proc, symbols.entryPoint() orelse fail("{s}: no symbols to be had from the program\n", .{spec.what})); + at_entry = true; + symbols.free(try symbols.refresh(proc.pid)); + } + const sym = symbols.find(spec.what) orelse fail("{s}: no such symbol in what the program has mapped\n", .{spec.what}); + break :found .{ sym.addr, sym.size }; }; - const len = spec.len orelse if (std.mem.indexOfScalar(u64, &.{ 1, 2, 4 }, sym.size)) |_| @as(u8, @intCast(sym.size)) else 8; - session.watch(.{ .addr = sym.addr, .len = len, .mode = spec.mode, .name = spec.what }) catch |err| switch (err) { + const len = spec.len orelse if (std.mem.indexOfScalar(u64, &.{ 1, 2, 4 }, size)) |_| @as(u8, @intCast(size)) else 8; + session.watch(.{ .addr = addr, .len = len, .mode = spec.mode, .name = spec.what }) catch |err| switch (err) { error.NoFreeBreakpoint => fail("{s}: no debug register left, there are four\n", .{spec.what}), - error.PerfNoSuchEvent => fail("{s}: a watchpoint covers 1, 2, 4 or 8 bytes at an address aligned to that, not {d} at {x}\n", .{ spec.what, len, sym.addr }), + error.PerfNoSuchEvent => fail("{s}: a watchpoint covers 1, 2, 4 or 8 bytes at an address aligned to that, not {d} at {x}\n", .{ spec.what, len, addr }), else => |e| return e, }; } @@ -150,7 +157,7 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { error.RecordingFull => null, else => |e| return e, }; - if (exit_code == null) proc.kill(); + if (exit_code == null) proc.close(); const ns = zt.event.now() - start; try finish(init, &rec, out, exit_code, session.lost, ns, options.counters, session.counts); } @@ -202,30 +209,18 @@ pub fn finish(init: std.process.Init, rec: *zt.Recorder, out: []const u8, exit_c }); } -/// Runs the held target up to its own entry point, where the dynamic -/// loader is done and its libraries are mapped. -fn runToEntry(proc: *zt.ptrace.Process) !void { - var path_buf: [64]u8 = undefined; - const fd = linux.open(try std.fmt.bufPrintSentinel(&path_buf, "/proc/{d}/auxv", .{proc.pid}, 0), .{}, 0); - if (linux.errno(fd) != .SUCCESS) return error.NoSuchProcess; - defer _ = linux.close(@intCast(fd)); - var auxv: [64][2]u64 = undefined; - const n = linux.read(@intCast(fd), @ptrCast(&auxv), @sizeOf(@TypeOf(auxv))); - if (linux.errno(n) != .SUCCESS) return error.NoSuchProcess; - const entry = for (auxv[0 .. n / 16]) |pair| { - if (pair[0] == std.elf.AT.ENTRY) break pair[1]; - } else return error.NoEntryPoint; - +/// Runs the held target up to `addr`, through a breakpoint there. +fn runTo(proc: *zt.ptrace.Process, addr: u64) !void { var saved: [1]u8 = undefined; - if (proc.read(entry, &saved) != 1) return error.BadAddress; - try proc.write(entry, &.{0xcc}); + if (proc.read(addr, &saved) != 1) return error.BadAddress; + try proc.write(addr, &.{0xcc}); while (true) switch (try proc.cont()) { .trap => break, .exited, .killed => fail("the program ended before its entry point\n", .{}), else => {}, }; - try proc.write(entry, &saved); + try proc.write(addr, &saved); var regs = try proc.regs(); - regs.rip = entry; + regs.rip = addr; try proc.setRegs(®s); } diff --git a/src/cli/probe.zig b/src/cli/probe.zig index 539af5c..311f2c0 100644 --- a/src/cli/probe.zig +++ b/src/cli/probe.zig @@ -106,7 +106,7 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { 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); + const id = zt.event.stringId(p.name); 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"}); @@ -116,7 +116,7 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { 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(); + errdefer proc.close(); 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. @@ -126,10 +126,27 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { defer tracer.close(); if (syscalls) tracer.syscalls() catch |err| refused(err, &tracer); if (sched) tracer.sched(btf) catch |err| refused(err, &tracer); + var symbols: zt.Symbols = .{ .gpa = init.gpa, .io = init.io }; + defer symbols.deinit(); + symbols.free(try symbols.refresh(proc.pid)); for (places.items) |p| { - const id = zt.perf.stringId(p.name); + const id = zt.event.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) { + var static = p.usdt; + // A semaphore of the program itself is ours to raise: it is mapped, + // and held. The kernel's way, a counter at an offset of the file, + // lands on the wrong address when the linker has that page of the + // file mapped writable twice, as lld does; it is left what is not + // mapped yet, a library's. + if (static) |*u| if (u.semaphore != 0 and p.own and symbols.modules.items.len > 0) { + const at = u.semaphore +% symbols.modules.items[0].bias; + var count: [2]u8 = undefined; + if (proc.read(at, &count) != 2) fail("{s}: its semaphore is not in the program's memory\n", .{p.name}); + std.mem.writeInt(u16, &count, std.mem.readInt(u16, &count, .little) +% 1, .little); + try proc.write(at, &count); + u.semaphore = 0; + }; + (if (static) |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) { @@ -148,7 +165,7 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { error.RecordingFull => null, else => |e| return e, }; - if (exit_code == null) proc.kill(); + if (exit_code == null) proc.close(); try perf.finish(init, &rec, out, exit_code, tracer.lost() + session.lost, zt.event.now() - start, &.{}, &.{}); } @@ -184,6 +201,8 @@ const Place = struct { name: []const u8, target: [:0]const u8, offset: u64 = 0, + /// In the program itself, not a library of it. + own: bool = false, elf: ?zt.usdt.Elf = null, usdt: ?zt.usdt.Probe = null, }; @@ -204,7 +223,7 @@ fn place(arena: std.mem.Allocator, p: Probe, program: [:0]const u8) !Place { 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 }; + return .{ .kind = .usdt, .name = name, .target = file, .offset = elf.offset(found.pc) orelse continue, .own = !named, .elf = elf, .usdt = found }; } fail("{s}: no static probe {s} (readelf -n lists them)\n", .{ file, name }); } diff --git a/src/trace/bpf.zig b/src/trace/bpf.zig index 5dad4c6..a550419 100644 --- a/src/trace/bpf.zig +++ b/src/trace/bpf.zig @@ -482,7 +482,10 @@ pub const Tracer = struct { } /// A static probe of the file `path` (`p` from `usdt.probes` with no - /// bias). The kernel raises its semaphore while we are attached. + /// bias). The kernel raises its semaphore in every process that maps + /// the file, while we are attached; where the same page of the file is + /// mapped writable twice it raises the wrong word, so for a target at + /// hand write the semaphore yourself and pass the probe without one. pub fn usdtProbe(t: *Tracer, elf: usdt.Elf, path: [*:0]const u8, p: usdt.Probe, id: u64) !void { const semaphore = if (p.semaphore == 0) 0 else elf.offset(p.semaphore) orelse return error.NoSuchProbe; try t.attach("uprobe", path, elf.offset(p.pc) orelse return error.NoSuchProbe, semaphore, try usdtProgram(t.maps, @intCast(t.pid), id, p.args)); diff --git a/src/trace/kernel_test.zig b/src/trace/kernel_test.zig index 1905771..8277e34 100644 --- a/src/trace/kernel_test.zig +++ b/src/trace/kernel_test.zig @@ -60,17 +60,20 @@ fn record(log: *Log, path: []const u8, args: []const []const u8, options: perf.O const envp = [_:null]?[*:0]const u8{}; var proc = try zt.ptrace.Process.spawn(argv[0].?, argv, &envp); - errdefer proc.kill(); + errdefer proc.close(); var session = perf.Session.open(gpa, proc.pid, options, log.sink()) catch |err| switch (err) { error.PerfAccess, error.PerfNoSuchEvent => { - proc.kill(); + proc.close(); return error.SkipZigTest; }, else => |e| return e, }; defer session.close(); + var symbols: zt.Symbols = .{ .gpa = gpa, .io = std.testing.io }; + defer symbols.deinit(); + symbols.free(try symbols.refresh(proc.pid)); for (armed) |a| { - const sym = (try perf.lookup(gpa, proc.pid, a.symbol)).?; + const sym = symbols.find(a.symbol).?; try session.watch(.{ .addr = sym.addr, .len = @intCast(@min(sym.size, 8)), .mode = a.mode, .name = a.symbol }); } perf.release(proc); @@ -297,7 +300,7 @@ test "usdt: the notes say what readelf says; arguments are fetched at a probe" { const argv = [_:null]?[*:0]const u8{ path, "4", null }; const envp = [_:null]?[*:0]const u8{}; var proc = try zt.ptrace.Process.spawn(path, &argv, &envp); - errdefer proc.kill(); + errdefer proc.close(); const maps = try perf.readMaps(gpa, proc.pid); defer gpa.free(maps); // The program itself is the first thing mapped. diff --git a/src/trace/perf.zig b/src/trace/perf.zig index 36eaaf8..962996a 100644 --- a/src/trace/perf.zig +++ b/src/trace/perf.zig @@ -21,7 +21,6 @@ const std = @import("std"); const linux = std.os.linux; const event = @import("event.zig"); const ptrace = @import("ptrace.zig"); -const usdt = @import("usdt.zig"); const Event = event.Event; const Regs = event.Regs; const PERF = linux.PERF; @@ -31,12 +30,6 @@ pub const reading: u16 = 1; /// `aux` of a `fault` that needed I/O. pub const fault_major: u16 = 1; -/// The id a `string` event gives `text`: its hash, so that backends agree -/// without a registry. -pub fn stringId(text: []const u8) u64 { - return std.hash.Wyhash.hash(0, text); -} - /// What can be counted. Context switches and migrations are missing: their /// software events fire in kernel mode, which paranoid 2 excludes, so they /// always read 0 (`Options.switches` gives the switches as events instead). @@ -236,7 +229,7 @@ pub const Session = struct { var attr: linux.perf_event_attr = .{ .type = .BREAKPOINT, .sample_period_or_freq = 1, .bp_type = @backingInt(w.mode), .config1 = w.addr, .config2 = if (w.mode == .exec) 8 else w.len }; // Where it executes is known; what it was called with is not. if (w.mode == .exec) attr.sample_type = PERF.SAMPLE.REGS_USER; - try s.add(attr, .{ .watch = .{ .len = w.len, .mode = w.mode, .name = stringId(w.name) } }); + try s.add(attr, .{ .watch = .{ .len = w.len, .mode = w.mode, .name = event.stringId(w.name) } }); if (w.mode == .exec) try s.name(w.name); } @@ -298,7 +291,7 @@ pub const Session = struct { } fn name(s: *Session, text: []const u8) !void { - try s.sink.emit(.{ .ts = event.now(), .tid = @intCast(s.pid), .kind = .string, .a = .{ stringId(text), 0, 0, 0, 0, 0 } }, text); + try s.sink.emit(.{ .ts = event.now(), .tid = @intCast(s.pid), .kind = .string, .a = .{ event.stringId(text), 0, 0, 0, 0, 0 } }, text); } /// The mappings the target has now, from /proc: the kernel only reports @@ -323,7 +316,7 @@ pub const Session = struct { if (linux.read(fd, @ptrCast(&v), 24) != 24) continue; // A counter that had to share its hardware counted part of the time: scale it. count.* = if (v[2] != 0 and v[2] < v[1]) @intCast(@as(u128, v[0]) * v[1] / v[2]) else v[0]; - try s.sink.emit(.{ .ts = ts, .tid = @intCast(s.pid), .kind = .sample, .aux = reading, .a = .{ 0, stringId(@tagName(c)), count.*, 0, 0, 0 } }, ""); + try s.sink.emit(.{ .ts = ts, .tid = @intCast(s.pid), .kind = .sample, .aux = reading, .a = .{ 0, event.stringId(@tagName(c)), count.*, 0, 0, 0 } }, ""); } } @@ -417,7 +410,7 @@ pub const Session = struct { // The counter overflowed on the way into the kernel: not a place in the program. if (pc >> 63 != 0) return; ev.kind = .sample; - ev.a = .{ pc, stringId("cycles"), word(body, 48), addr, 0, 0 }; + ev.a = .{ pc, event.stringId("cycles"), word(body, 48), addr, 0, 0 }; var chain: []const u8 = ""; if (s.options.callchain) { // The first entry says "user space from here", not a pc. @@ -439,7 +432,7 @@ pub const Session = struct { .write => { // The value now: the target has run on since it wrote. var bytes: [8]u8 = undefined; - const n = (ptrace.Process{ .pid = s.pid }).read(addr, bytes[0..w.len]); + const n = (ptrace.Process{ .pid = s.pid, .tid = s.pid }).read(addr, bytes[0..w.len]); ev.kind = .mem_write; ev.a = .{ addr, w.len, pc, 0, 0, 0 }; try s.sink.emit(ev, bytes[0..n]); @@ -573,28 +566,6 @@ pub fn release(p: ptrace.Process) void { _ = linux.ptrace(linux.PTRACE.DETACH, p.pid, 0, 0, 0); } -pub const Symbol = struct { addr: u64, size: u64 }; - -/// Where `pid` has the symbol `name` now, looking through every ELF file -/// it has mapped, in address order. Shared libraries are there once the -/// dynamic loader has run, not at the first instruction. -pub fn lookup(gpa: std.mem.Allocator, pid: linux.pid_t, name: []const u8) !?Symbol { - const text = try readMaps(gpa, pid); - defer gpa.free(text); - var lines = std.mem.tokenizeScalar(u8, text, '\n'); - while (lines.next()) |line| { - const m = parseMap(line) orelse continue; - if (m.offset != 0 or m.path.len == 0 or m.path[0] != '/') continue; - const path = try gpa.dupeSentinel(u8, m.path, 0); - defer gpa.free(path); - const elf = usdt.Elf.open(path) catch continue; - defer elf.close(); - const sym = elf.symbol(name) orelse continue; - return .{ .addr = sym.value +% elf.bias(m.start), .size = sym.size }; - } - return null; -} - /// /proc/PID/maps, whole. Free with `gpa`. pub fn readMaps(gpa: std.mem.Allocator, pid: linux.pid_t) ![]u8 { var path_buf: [64]u8 = undefined; |
