//! 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 \\ --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", 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.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"}); 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.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. 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); 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.event.stringId(p.name); try sorted.sink().emit(.{ .ts = zt.event.now(), .kind = .string, .a = .{ id, 0, 0, 0, 0, 0 } }, p.name); 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) { 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.close(); 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.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 }), } } /// 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, /// In the program itself, not a library of it. own: bool = false, 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, .own = !named, .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 }); }