From 36d7f642e71207d04f5da80530da552e06447337 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 12:37:33 -0300 Subject: syscalls: what writev's buffers hold is captured, by the debugger and the agent musl's printf and Zig programs write through writev: pardes drawing an image showed nothing of the kitty graphics commands it sends until this. The README says what Lua and pardes were run with and what they showed. --- README.md | 16 ++++++++++++++++ src/agent/sud.zig | 19 +++++++++++++++++++ src/trace/Debugger.zig | 13 +++++++++++++ src/trace/syscalls.zig | 9 ++++++--- 4 files changed, 54 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ce44bf4..8e2766b 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,22 @@ everything instrumented (lines, calls, variables, stores, in all of its A moment a C line of an interpreter is a great many moments: choose what is instrumented (`--only`, `--mem`, `--instr`) before choosing to wait. +The other backends on the same Lua, uninstrumented: the agent's +allocations and syscalls (368 allocations, no ptrace), breakpoints on +`luaH_resize` (48 hits), source lines from DWARF (53 000 a second), +counters and page faults from perf (10.8 million instructions, 89 faults). + +And pardes drawing an image, through its own harness (`zig build +image-harness` there, the command it runs given to `ztrace run --preload +... --syscalls --capture 2048`): the harness and the pardes it starts are +one recording, 9 threads, and the kitty graphics protocol is there to read +in the captured writes: pardes asks (`_Gi=1,a=q`), the terminal answers +(`_Gi=1;OK`), pardes sends the pixels (`_Gf=32,s=2,v=2,i=1;DU3n/w...`). Two +limits it shows: a recording's state is one address space, so a child's +memory and mappings share its parent's maps (its events are told apart by +thread); and pardes allocates through its own heaps, so `--alloc` sees only +what its C libraries ask of libc. + ## Together The backends are not modes to choose between: they feed one sink. A diff --git a/src/agent/sud.zig b/src/agent/sud.zig index a32735d..5d2c6ad 100644 --- a/src/agent/sud.zig +++ b/src/agent/sud.zig @@ -168,10 +168,29 @@ fn argument(number: u64, a: [6]u64) []const u8 { nr(.open), nr(.stat), nr(.lstat), nr(.access), nr(.execve), nr(.chdir), nr(.unlink), nr(.readlink), nr(.mkdir), nr(.rmdir), nr(.truncate), nr(.chmod) => string(a[0]), nr(.openat), nr(.newfstatat), nr(.unlinkat), nr(.readlinkat), nr(.faccessat), nr(.faccessat2), nr(.statx), nr(.mkdirat), nr(.execveat) => string(a[1]), nr(.write), nr(.pwrite64), nr(.sendto) => if (a[1] == 0) "" else agent.text(@as([*]const u8, @ptrFromInt(a[1]))[0..a[2]], agent.capture), + nr(.writev), nr(.pwritev), nr(.pwritev2) => gathered(a[1], a[2]), else => "", }; } +/// A thread's vector of buffers copied end to end, until its event is emitted. +threadlocal var gather: [2048]u8 = undefined; + +/// What writev's buffers hold, as far as `capture` goes. +fn gathered(vec: u64, count: u64) []const u8 { + if (vec == 0) return ""; + const iov: [*]const std.posix.iovec_const = @ptrFromInt(vec); + const max = @min(agent.capture, gather.len); + var len: usize = 0; + for (iov[0..@min(count, 1024)]) |v| { + const n = @min(v.len, max - len); + @memcpy(gather[len..][0..n], v.base[0..n]); + len += n; + if (len == max) break; + } + return agent.text(gather[0..len], max); +} + fn string(addr: u64) []const u8 { if (addr == 0) return ""; const s: [*]const u8 = @ptrFromInt(addr); diff --git a/src/trace/Debugger.zig b/src/trace/Debugger.zig index 6aa64b1..5e679dd 100644 --- a/src/trace/Debugger.zig +++ b/src/trace/Debugger.zig @@ -394,6 +394,18 @@ fn emitStop(d: *Debugger, stop: Stop) !void { /// point at, cut at `capture`. Empty when it takes none. Good until the /// next call. // shortcut: the first such argument only (rename has two), upgrade when a consumer wants the second. +/// What a vector of buffers holds (writev's), end to end, as far as `buf` goes. +fn gather(d: *Debugger, vec: u64, count: u64, buf: []u8) []u8 { + var len: usize = 0; + for (0..@min(count, 64)) |i| { + var iov: [2]u64 = undefined; + if (d.proc.read(vec + i * 16, std.mem.asBytes(&iov)) != 16) break; + len += d.proc.read(iov[0], buf[len..][0..@min(buf.len - len, iov[1])]); + if (len == buf.len) break; + } + return buf[0..len]; +} + pub fn decode(d: *Debugger, nr: u16) ![]const u8 { const args = [_]u64{ d.regs.rdi, d.regs.rsi, d.regs.rdx, d.regs.r10, d.regs.r8, d.regs.r9 }; try d.captured.resize(d.gpa, d.capture); @@ -401,6 +413,7 @@ pub fn decode(d: *Debugger, nr: u16) ![]const u8 { const text = for (syscalls_table.signature(nr), 0..) |kind, i| switch (kind) { 's' => break d.proc.readString(args[i], buf), 'b' => break buf[0..d.proc.read(args[i], buf[0..@min(buf.len, args[i + 1])])], + 'v' => break d.gather(args[i], args[i + 1], buf), else => {}, } else return ""; // A payload as long as the registers is taken for them: a byte less is not. diff --git a/src/trace/syscalls.zig b/src/trace/syscalls.zig index 2bf32b5..7e276bb 100644 --- a/src/trace/syscalls.zig +++ b/src/trace/syscalls.zig @@ -12,14 +12,17 @@ pub fn name(nr: u64) ?[]const u8 { /// A letter an argument: i an int (a descriptor, a pid, a signal), d a /// size, x a pointer or flags, s a NUL-terminated string, b a buffer as long -/// as the next argument says. Six x for a syscall not listed. +/// as the next argument says, v a vector of that many buffers (an iovec). +/// Six x for a syscall not listed. pub fn signature(nr: u64) []const u8 { return switch (std.enums.fromInt(linux.SYS, nr) orelse return "xxxxxx") { .getpid, .gettid, .getppid, .getuid, .geteuid, .getgid, .getegid, .sched_yield, .pause, .fork, .vfork => "", .close, .exit, .exit_group, .dup, .fsync => "i", .brk, .set_tid_address, .uname, .pipe, .time => "x", .chdir, .rmdir, .unlink => "s", - .read, .readv, .writev, .getdents64 => "ixd", + .read, .readv, .getdents64 => "ixd", + .writev => "ivd", + .pwritev, .pwritev2 => "ivdd", .write => "ibd", .pread64 => "ixdd", .pwrite64 => "ibdd", @@ -70,7 +73,7 @@ pub fn formatEnter(w: *std.Io.Writer, ev: *const Event, payload: []const u8) !vo switch (kind) { 'i' => try w.print("{d}", .{@as(i32, @bitCast(@as(u32, @truncate(ev.a[i]))))}), 'd' => try w.print("{d}", .{ev.a[i]}), - 's', 'b' => if (decoded) |text| { + 's', 'b', 'v' => if (decoded) |text| { // Dots, as strace has them, when the buffer was longer than what was captured. try w.print("\"{f}\"{s}", .{ std.zig.fmtString(text), if (kind == 'b' and text.len < ev.a[i + 1]) "..." else "" }); decoded = null; -- cgit v1.3