From 8ac0639bfc0186acedb11759432a2cfa277193f2 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 12:28:33 -0300 Subject: static probes from the debugger: -b usdt:PROVIDER:NAME, no root Debugger.usdt arms the USDT probes of every mapped module as breakpoints whose hook records a probe event with its arguments and lets the program run on, raising a probe's semaphore first; record and dbg take usdt:PROVIDER:NAME (* for any), running the loader through when the probe is a library's. glibc's own 78 probes are there for the asking: memory_sbrk_more shows malloc growing the heap. --- src/trace/Debugger.zig | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'src/trace') diff --git a/src/trace/Debugger.zig b/src/trace/Debugger.zig index d69adcc..6aa64b1 100644 --- a/src/trace/Debugger.zig +++ b/src/trace/Debugger.zig @@ -30,6 +30,7 @@ const Event = event.Event; const Regs = event.Regs; const ptrace = @import("ptrace.zig"); const Ring = @import("Ring.zig"); +const notes = @import("usdt.zig"); const Symbols = @import("Symbols.zig"); const syscalls_table = @import("syscalls.zig"); const P = linux.PTRACE; @@ -64,6 +65,8 @@ next_id: u32 = 1, /// What the sink was sent of the target's memory, by page. shadow: std.AutoHashMapUnmanaged(u64, *Page) = .empty, scratch: std.ArrayList(u8) = .empty, +/// The static probes armed (`usdt`): what each one's hook reads. +sites: std.ArrayList(*Site) = .empty, /// The threads that have stopped before: a first stop is a thread's birth. threads: std.AutoHashMapUnmanaged(linux.pid_t, void) = .empty, /// String ids of source files: by path, and by line-table entry. @@ -135,6 +138,8 @@ pub fn deinit(d: *Debugger) void { d.syms.deinit(); d.watches.deinit(d.gpa); d.breakpoints.deinit(d.gpa); + for (d.sites.items) |site| d.gpa.destroy(site); + d.sites.deinit(d.gpa); var pages = d.shadow.valueIterator(); while (pages.next()) |page| d.gpa.destroy(page.*); d.shadow.deinit(d.gpa); @@ -555,6 +560,68 @@ fn breakpointAt(d: *const Debugger, addr: u64) ?Breakpoint { /// next line's that has any) or a function's name: past `push rbp; mov rbp, /// rsp` when it begins so, where its frame is set up for `backtrace` and /// its arguments are still in their registers. +/// Runs the loader through to the program's entry point, where its +/// libraries are mapped: their symbols and probes can be found from there. +pub fn toEntry(d: *Debugger) !void { + const at = d.syms.entryPoint() orelse return; + if (at == d.regs.rip) return; + const id = try d.breakAt(at, null); + defer d.clear(id); + while (true) switch (try d.cont()) { + .breakpoint => |hit| if (hit == id) return, + .exited, .killed => return error.Exited, + else => {}, + }; +} + +const Site = struct { name: u64, args: [5]?notes.Arg }; + +/// Arms the static probes (USDT, DTrace's) that `spec` names, "provider:name" +/// with "*" for any of either, in every module mapped now. Each hit is a +/// `probe` event with its first five arguments and the registers, and the +/// program runs on: what `ztrace probe` does with a uprobe and root, done +/// with a breakpoint and neither. Returns how many places were armed. +pub fn usdt(d: *Debugger, spec: []const u8) !usize { + const colon = std.mem.indexOfScalar(u8, spec, ':') orelse return error.BadLocation; + var armed: usize = 0; + for (d.syms.modules.items) |m| { + const path = try d.gpa.dupeSentinel(u8, m.path, 0); + defer d.gpa.free(path); + const elf = notes.Elf.open(path) catch continue; + defer elf.close(); + const found = try notes.probes(d.gpa, elf, m.bias); + defer d.gpa.free(found); + for (found) |p| { + if (!probeNamed(spec[0..colon], p.provider) or !probeNamed(spec[colon + 1 ..], p.name)) continue; + var buf: [256]u8 = undefined; + const label = std.fmt.bufPrint(&buf, "{s}:{s}", .{ p.provider, p.name }) catch continue; + const site = try d.gpa.create(Site); + site.* = .{ .name = event.stringId(label), .args = @splat(null) }; + try d.sites.append(d.gpa, site); + var specs = std.mem.tokenizeScalar(u8, p.args, ' '); + for (&site.args) |*arg| arg.* = notes.Arg.parse(specs.next() orelse break) catch null; + try d.emit(.{ .kind = .string, .a = .{ site.name, 0, 0, 0, 0, 0 } }, label); + // A program may test the semaphore before working out a probe's arguments. + if (p.semaphore != 0) try d.write(p.semaphore, &.{ 1, 0 }); + _ = try d.breakAt(p.pc, .{ .ctx = site, .func = atProbe }); + armed += 1; + } + } + return armed; +} + +fn probeNamed(want: []const u8, have: []const u8) bool { + return std.mem.eql(u8, want, "*") or std.mem.eql(u8, want, have); +} + +fn atProbe(ctx: ?*anyopaque, d: *Debugger, _: Stop) anyerror!Action { + const site: *const Site = @ptrCast(@alignCast(ctx.?)); + var ev: Event = .{ .kind = .probe, .a = .{ site.name, 0, 0, 0, 0, 0 } }; + for (site.args, ev.a[1..]) |arg, *slot| slot.* = if (arg) |a| a.fetch(&d.regs, &d.proc) orelse 0 else 0; + try d.emit(ev, std.mem.asBytes(&d.regs)); + return .go; +} + pub fn resolve(d: *Debugger, spec: []const u8) !u64 { if (std.mem.startsWith(u8, spec, "*")) return std.fmt.parseInt(u64, spec[1..], 0) catch error.BadLocation; if (std.mem.lastIndexOfScalar(u8, spec, ':')) |colon| { -- cgit v1.3