summaryrefslogtreecommitdiff
path: root/bench/debugger.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-10 11:58:00 -0300
committerGabriel Schneider <[email protected]>2026-11-10 11:58:53 -0300
commit8beab959b633e6641d07aba053a55e6e8e61eb00 (patch)
tree0e5a91c0a00e121fe4094a333438ee90ccc5fdda /bench/debugger.zig
parent3fdb67c479dcc1ad0f1e51832bc086622692249c (diff)
downloadcomputeranim-8beab959b633e6641d07aba053a55e6e8e61eb00.tar.gz
computeranim-8beab959b633e6641d07aba053a55e6e8e61eb00.zip
targets, test-debugger, bench-debugger
A linked list, a graph search in Zig, the same C under glibc and musl, two threads. The tests drive the debugger over them: breakpoint counts, the lines a step walks (as gdb), a list snapshotted by a hook, a watch's words, the syscalls that tell glibc from musl, threads, attach, and scripted dbg and record sessions. The bench measures steps, stops and memory access from bare ptrace up to a recording. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Diffstat (limited to 'bench/debugger.zig')
-rw-r--r--bench/debugger.zig217
1 files changed, 217 insertions, 0 deletions
diff --git a/bench/debugger.zig b/bench/debugger.zig
new file mode 100644
index 0000000..f682dff
--- /dev/null
+++ b/bench/debugger.zig
@@ -0,0 +1,217 @@
+//! What the debugger costs, measured: `zig build bench-debugger`. Steps,
+//! syscall stops and memory access, each from the bare ptrace calls up to a
+//! recording, so the differences say where the time goes.
+const std = @import("std");
+const linux = std.os.linux;
+const zt = @import("ztrace");
+const Debugger = zt.Debugger;
+const now = zt.event.now;
+
+var out: *std.Io.Writer = undefined;
+
+fn row(what: []const u8, n: u64, ns: u64, unit: []const u8) !void {
+ const per = @as(f64, @floatFromInt(ns)) / @as(f64, @floatFromInt(@max(n, 1)));
+ try out.print(" {s:<46} {d:>9} {s:<6} {d:>9.0}/s {d:>8.2} us each\n", .{ what, n, unit, 1e9 / per, per / 1e3 });
+}
+
+/// A sink that only counts.
+const Count = struct {
+ events: u64 = 0,
+ bytes: u64 = 0,
+ fn sink(c: *Count) zt.Sink {
+ return .{ .ctx = c, .emitFn = emit };
+ }
+ fn emit(ctx: *anyopaque, _: *const zt.Event, payload: []const u8) anyerror!void {
+ const c: *Count = @ptrCast(@alignCast(ctx));
+ c.events += 1;
+ c.bytes += payload.len;
+ }
+};
+
+const Mode = enum { stepi, step, cont };
+
+/// Runs a target to its exit under the debugger, one way of stopping, and
+/// returns the stops and the time.
+fn drive(init: std.process.Init, argv: [:null]const ?[*:0]const u8, sink: ?zt.Sink, mode: Mode, stack: usize, syscalls: @FieldType(Debugger, "syscalls"), breakpoint: ?[]const u8) !struct { u64, u64 } {
+ var d = try Debugger.spawn(init.gpa, init.io, argv[0].?, argv.ptr, init.minimal.environ.block.slice.ptr, sink);
+ defer d.deinit();
+ d.stack_watch = stack;
+ d.syscalls = syscalls;
+ if (breakpoint) |where| _ = try d.breakAt(try d.resolve(where), null);
+ var stops: u64 = 0;
+ const start = now();
+ while (true) : (stops += 1) switch (try switch (mode) {
+ .stepi => d.stepi(),
+ .step => d.step(),
+ .cont => d.cont(),
+ }) {
+ .exited, .killed => break,
+ else => {},
+ };
+ return .{ stops, now() - start };
+}
+
+/// The same into a recording: its moments, how fast, and what each costs on disk.
+fn recorded(init: std.process.Init, what: []const u8, argv: [:null]const ?[*:0]const u8, mode: Mode, syscalls: @FieldType(Debugger, "syscalls"), breakpoint: ?[]const u8) !void {
+ const path = "bench.ztrace";
+ var rec = try zt.Recorder.create(init.gpa, init.io, path);
+ rec.cut &= ~zt.Kind.line.bit();
+ _, const ns = try drive(init, argv, rec.sink(), mode, 128, syscalls, breakpoint);
+ const start = now();
+ const moments = rec.moments;
+ try rec.close();
+ const size = (try std.Io.Dir.cwd().statFile(init.io, path, .{})).size;
+ try std.Io.Dir.cwd().deleteFile(init.io, path);
+ try row(what, moments, ns + (now() - start), "moments");
+ try out.print(" {s:<46} {d:>9} bytes {d:>9} a moment\n", .{ "", size, size / @max(moments, 1) });
+}
+
+pub fn main(init: std.process.Init) !void {
+ const args = try init.minimal.args.toSlice(init.arena.allocator());
+ if (args.len != 4) return error.Usage; // fib, graph, dd
+ // The numbers go to stderr, and stdout nowhere: it is the targets' too.
+ var buf: [4096]u8 = undefined;
+ var stderr = std.Io.File.stderr().writer(init.io, &buf);
+ out = &stderr.interface;
+ _ = linux.dup2(@intCast(linux.open("/dev/null", .{ .ACCMODE = .WRONLY }, 0)), 1);
+ const envp = init.minimal.environ.block.slice.ptr;
+ const fib = [_:null]?[*:0]const u8{ args[1].ptr, "12", null };
+ const graph = [_:null]?[*:0]const u8{ args[2].ptr, null };
+
+ try out.print("instruction steps ({s} 12)\n", .{std.fs.path.basename(args[1])});
+ {
+ var p = try zt.ptrace.Process.spawn(fib[0].?, &fib, envp);
+ defer p.close();
+ var n: u64 = 0;
+ const start = now();
+ while (try p.stepi() == .trap) n += 1;
+ try row("ptrace singlestep + wait", n, now() - start, "steps");
+ }
+ {
+ var p = try zt.ptrace.Process.spawn(fib[0].?, &fib, envp);
+ defer p.close();
+ var n: u64 = 0;
+ var sum: u64 = 0;
+ const start = now();
+ while (try p.stepi() == .trap) : (n += 1) sum +%= (try p.regs()).rip;
+ try row("... + registers", n, now() - start, "steps");
+ }
+ {
+ const n, const ns = try drive(init, &fib, null, .stepi, 0, .ignore, null);
+ try row("Debugger.stepi, no sink", n, ns, "steps");
+ }
+ var count: Count = .{};
+ {
+ const n, const ns = try drive(init, &fib, count.sink(), .stepi, 0, .ignore, null);
+ try row("... + events (the source line looked up)", n, ns, "steps");
+ }
+ {
+ const n, const ns = try drive(init, &fib, count.sink(), .stepi, 0, .record, null);
+ try row("... + syscalls told from the registers", n, ns, "steps");
+ }
+ {
+ const n, const ns = try drive(init, &fib, count.sink(), .stepi, 128, .record, null);
+ try row("... + 128 bytes of stack watched", n, ns, "steps");
+ }
+ {
+ const n, const ns = try drive(init, &fib, count.sink(), .stepi, 4096, .record, null);
+ try row("... or 4096 bytes of stack watched", n, ns, "steps");
+ }
+ try recorded(init, "recorded (`ztrace record`)", &fib, .stepi, .record, null);
+
+ try out.print("\nsource-line steps ({s})\n", .{std.fs.path.basename(args[2])});
+ {
+ const insns, _ = try drive(init, &graph, null, .stepi, 0, .ignore, null);
+ const n, const ns = try drive(init, &graph, null, .step, 0, .ignore, null);
+ try row("Debugger.step, no sink", n, ns, "lines");
+ try out.print(" {s:<46} {d:>9} instructions under them, {d:.2} us each\n", .{ "", insns, @as(f64, @floatFromInt(ns)) / @as(f64, @floatFromInt(insns)) / 1e3 });
+ }
+ try recorded(init, "recorded (`ztrace record --by line`)", &graph, .step, .record, null);
+ try out.print("\nbreakpoints ({s} 22: one at fib, 28657 calls)\n", .{std.fs.path.basename(args[1])});
+ const deep = [_:null]?[*:0]const u8{ args[1].ptr, "22", null };
+ {
+ const n, const ns = try drive(init, &deep, null, .cont, 0, .ignore, "fib");
+ try row("Debugger.cont to a breakpoint, no sink", n, ns, "hits");
+ }
+ try recorded(init, "recorded (`ztrace record --by break -b fib`)", &deep, .cont, .record, "fib");
+
+ const calls = 200_000;
+ const dd = [_:null]?[*:0]const u8{ args[3].ptr, "if=/dev/zero", "of=/dev/null", "bs=1", std.fmt.comptimePrint("count={d}", .{calls / 2}), "status=none", null };
+ try out.print("\nsyscall stops ({s} bs=1 count={d}: a read and a write each)\n", .{ std.fs.path.basename(args[3]), calls / 2 });
+ const native = native: {
+ const start = now();
+ const pid = linux.fork();
+ if (pid == 0) {
+ _ = linux.execve(dd[0].?, &dd, envp);
+ linux.exit_group(127);
+ }
+ var status: u32 = 0;
+ _ = linux.waitpid(@intCast(pid), @ptrCast(&status), 0);
+ break :native now() - start;
+ };
+ try row("native", calls, native, "calls");
+ {
+ var p = try zt.ptrace.Process.spawn(dd[0].?, &dd, envp);
+ defer p.close();
+ const start = now();
+ while (try p.syscall() == .syscall) {}
+ try row("ptrace syscall + wait, entry and exit", calls, now() - start - native, "calls");
+ }
+ {
+ _, const ns = try drive(init, &dd, null, .cont, 0, .stop, null);
+ try row("Debugger.cont stopping at both, no sink", calls, ns - native, "calls");
+ }
+ {
+ _, const ns = try drive(init, &dd, count.sink(), .cont, 0, .record, null);
+ try row("... as events with the bytes written", calls, ns - native, "calls");
+ }
+ try recorded(init, "recorded (`ztrace record --by syscall`)", &dd, .cont, .stop, null);
+
+ try out.print("\nmemory (1 MiB of {s}'s code; 64 bytes of its stack)\n", .{std.fs.path.basename(args[2])});
+ {
+ var d = try Debugger.spawn(init.gpa, init.io, graph[0].?, &graph, envp, null);
+ defer d.deinit();
+ const p = &d.proc;
+ const code = d.symbol("graph.main").?.addr & ~@as(u64, 0xfffff);
+ const block = try init.gpa.alloc(u8, 1 << 20);
+ defer init.gpa.free(block);
+ const rounds = 200;
+ var start = now();
+ for (0..rounds) |_| if (p.read(code + 0x1000, block) != block.len) return error.ShortRead;
+ var ns = now() - start;
+ try out.print(" {s:<46} {d:>9.0} MiB/s\n", .{ "process_vm_readv, one call", @as(f64, rounds) * 1e9 / @as(f64, @floatFromInt(ns)) });
+ start = now();
+ var at: usize = 0;
+ while (at < block.len) : (at += 8) {
+ const word = linux.ptrace(linux.PTRACE.PEEKDATA, p.pid, code + 0x1000 + at, @intFromPtr(block[at..].ptr), 0);
+ if (linux.errno(word) != .SUCCESS) return error.Peek;
+ }
+ ns = now() - start;
+ try out.print(" {s:<46} {d:>9.0} MiB/s\n", .{ "PTRACE_PEEKDATA, a word a call", 1e9 / @as(f64, @floatFromInt(ns)) });
+
+ var small: [64]u8 = undefined;
+ const n = 200_000;
+ start = now();
+ for (0..n) |_| _ = p.read(d.regs.rsp, &small);
+ try row("64 bytes: process_vm_readv", n, now() - start, "reads");
+ start = now();
+ for (0..n) |_| {
+ for (0..8) |i| _ = linux.ptrace(linux.PTRACE.PEEKDATA, p.pid, d.regs.rsp + i * 8, @intFromPtr(&small[i * 8]), 0);
+ }
+ try row("64 bytes: PTRACE_PEEKDATA x 8", n, now() - start, "reads");
+ start = now();
+ for (0..n) |_| try p.write(d.regs.rsp - 256, &small);
+ try row("64 bytes: process_vm_writev", n, now() - start, "writes");
+ // Code is read-only: the same call goes through /proc/pid/mem.
+ _ = p.read(code + 0x1000, &small);
+ start = now();
+ for (0..n) |_| try p.write(code + 0x1000, &small);
+ try row("64 bytes of code: writev fails, /proc/pid/mem", n, now() - start, "writes");
+ start = now();
+ for (0..n) |_| {
+ for (0..8) |i| _ = linux.ptrace(linux.PTRACE.POKEDATA, p.pid, code + 0x1000 + i * 8, std.mem.readInt(u64, small[i * 8 ..][0..8], .little), 0);
+ }
+ try row("64 bytes of code: PTRACE_POKEDATA x 8", n, now() - start, "writes");
+ }
+ try out.flush();
+}