summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-10 12:07:53 -0300
committerGabriel Schneider <[email protected]>2026-11-10 12:09:56 -0300
commita10e1691dbe5b944812dc8761a450636bc247e3d (patch)
tree68b4ae8b519496dd35a94bb799f3fd564ec42569 /src
parent08d36ff6295a5f04e503ffe0aaa8a4e712af865f (diff)
downloadcomputeranim-a10e1691dbe5b944812dc8761a450636bc247e3d.tar.gz
computeranim-a10e1691dbe5b944812dc8761a450636bc247e3d.zip
agent: bench-agent and its numbers, hooks with names, an archive cc can link
zig build bench-agent runs each capability against its native loop and prints what it costs and writes; README has the table. ztrace run looks at the ring a little longer before sleeping (a target that finds the tracer asleep pays a futex wake an event). A hooked function's name is defined as the string of its address, so the recorder's call stack reads fib. A static link without libc's malloc says which -u to add. The archive carries no stack probes, so cc links it in debug builds too. Tests for --wrap (musl, zig cc) and for a recording that fills. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Diffstat (limited to 'src')
-rw-r--r--src/agent/agent.zig8
-rw-r--r--src/agent/hook.zig4
-rw-r--r--src/agent/lib.zig39
-rw-r--r--src/cli/run.zig4
4 files changed, 40 insertions, 15 deletions
diff --git a/src/agent/agent.zig b/src/agent/agent.zig
index 22b4f8d..bc53336 100644
--- a/src/agent/agent.zig
+++ b/src/agent/agent.zig
@@ -66,7 +66,13 @@ pub fn init() void {
if (client.getenv(env, "ZTRACE_HOOKS")) |list| {
var specs = std.mem.tokenizeScalar(u8, list, ',');
while (specs.next()) |spec| {
- if (symbols.find(null, spec)) |function| hook.add(function.addr) else complain(&.{ "nothing called ", spec });
+ const function = symbols.find(null, spec) orelse {
+ complain(&.{ "nothing called ", spec });
+ continue;
+ };
+ // A call names its callee by address: the string of that id is its name.
+ emit(.string, 0, .{ function.addr, 0, 0, 0, 0, 0 }, text(spec, max_payload));
+ hook.add(function.addr);
}
}
allocations = client.getenv(env, "ZTRACE_ALLOC") != null;
diff --git a/src/agent/hook.zig b/src/agent/hook.zig
index 57a374c..fa44cc4 100644
--- a/src/agent/hook.zig
+++ b/src/agent/hook.zig
@@ -11,6 +11,10 @@
//! handler swaps the return address on the stack for `returned`, which emits
//! the event and jumps to the real one.
//!
+//! shortcut: SIGTRAP is forced; a target that calls a hooked function with
+//! SIGTRAP blocked is killed by the kernel. Under --syscalls the agent keeps
+//! the target's masks clear of it; without, nothing does.
+//!
//! shortcut: a signal per call, about a microsecond. For hooks on hot
//! functions, copy the prologue to a trampoline and patch a jump in (this
//! needs an x86-64 instruction-length decoder, to move whole instructions and
diff --git a/src/agent/lib.zig b/src/agent/lib.zig
index c497cf4..fb59ae0 100644
--- a/src/agent/lib.zig
+++ b/src/agent/lib.zig
@@ -82,6 +82,17 @@ fn ready() bool {
return true;
}
+/// libc's own `name`. A fully static link leaves out what nothing refers
+/// to, and with --wrap nothing refers to libc's malloc any more: said, with
+/// the way out, instead of a jump to nowhere.
+fn libc(comptime name: []const u8) @typeInfo(@FieldType(Real, name)).optional.child {
+ return @field(real, name) orelse {
+ const message = "ztrace agent: this link has no " ++ name ++ " of libc's; add -Wl,-u," ++ name ++ "\n";
+ _ = std.os.linux.write(2, message, message.len);
+ std.os.linux.exit_group(134);
+ };
+}
+
/// Memory for the allocations made while the lookup runs. Never reused, so
/// always zero; each block is preceded by its size.
var early: [1 << 16]u8 align(16) = @splat(0);
@@ -112,14 +123,14 @@ fn freed(p: ?*anyopaque, pc: usize) void {
const interposed = struct {
fn malloc(size: usize) callconv(.c) ?*anyopaque {
if (!ready()) return earlyAlloc(size, 16);
- const p = real.malloc.?(size);
+ const p = libc("malloc")(size);
allocated(.malloc, p, size, @returnAddress());
return p;
}
fn calloc(n: usize, size: usize) callconv(.c) ?*anyopaque {
if (!ready()) return earlyAlloc(n *| size, 16);
- const p = real.calloc.?(n, size);
+ const p = libc("calloc")(n, size);
allocated(.calloc, p, n *| size, @returnAddress());
return p;
}
@@ -127,14 +138,14 @@ const interposed = struct {
fn realloc(old: ?*anyopaque, size: usize) callconv(.c) ?*anyopaque {
if (!ready() or isEarly(old)) {
// An early block cannot grow in place: a new one, with its bytes.
- const p: [*]u8 = @ptrCast((if (ready()) real.malloc.?(size) else earlyAlloc(size, 16)) orelse return null);
+ const p: [*]u8 = @ptrCast((if (ready()) libc("malloc")(size) else earlyAlloc(size, 16)) orelse return null);
if (old) |o| {
const had = @as(*const usize, @ptrFromInt(@intFromPtr(o) - 16)).*;
@memcpy(p[0..@min(had, size)], @as([*]const u8, @ptrCast(o)));
}
return p;
}
- const p = real.realloc.?(old, size);
+ const p = libc("realloc")(old, size);
// One that moved is a free and an alloc; one in place, an alloc at the same pointer.
if (old != null and p != old and (p != null or size == 0)) freed(old, @returnAddress());
allocated(.realloc, p, size, @returnAddress());
@@ -145,7 +156,7 @@ const interposed = struct {
if (p == null or isEarly(p) or !ready()) return;
// Before it is free: once it is, another thread's alloc may be given the same pointer.
freed(p, @returnAddress());
- real.free.?(p);
+ libc("free")(p);
}
fn posix_memalign(out: *?*anyopaque, alignment: usize, size: usize) callconv(.c) c_int {
@@ -153,39 +164,39 @@ const interposed = struct {
out.* = earlyAlloc(size, alignment) orelse return @intFromEnum(std.os.linux.E.NOMEM);
return 0;
}
- const rc = real.posix_memalign.?(out, alignment, size);
+ const rc = libc("posix_memalign")(out, alignment, size);
if (rc == 0) allocated(.memalign, out.*, size, @returnAddress());
return rc;
}
fn aligned_alloc(alignment: usize, size: usize) callconv(.c) ?*anyopaque {
if (!ready()) return earlyAlloc(size, alignment);
- const p = real.aligned_alloc.?(alignment, size);
+ const p = libc("aligned_alloc")(alignment, size);
allocated(.memalign, p, size, @returnAddress());
return p;
}
fn memalign(alignment: usize, size: usize) callconv(.c) ?*anyopaque {
if (!ready()) return earlyAlloc(size, alignment);
- const p = real.memalign.?(alignment, size);
+ const p = libc("memalign")(alignment, size);
allocated(.memalign, p, size, @returnAddress());
return p;
}
fn pthread_create(thread: *anyopaque, attr: ?*const anyopaque, start: Start, arg: ?*anyopaque) callconv(.c) c_int {
_ = ready();
- if (agent.ring == null) return real.pthread_create.?(thread, attr, start, arg);
+ if (agent.ring == null) return libc("pthread_create")(thread, attr, start, arg);
// The new thread runs `threadMain`, which needs to know what it was meant to run.
- const box: *Box = @ptrCast(@alignCast(real.malloc.?(@sizeOf(Box)) orelse return @intFromEnum(std.os.linux.E.AGAIN)));
+ const box: *Box = @ptrCast(@alignCast(libc("malloc")(@sizeOf(Box)) orelse return @intFromEnum(std.os.linux.E.AGAIN)));
box.* = .{ .start = start, .arg = arg };
- const rc = real.pthread_create.?(thread, attr, threadMain, box);
- if (rc != 0) real.free.?(box);
+ const rc = libc("pthread_create")(thread, attr, threadMain, box);
+ if (rc != 0) libc("free")(box);
return rc;
}
fn fork() callconv(.c) c_int {
_ = ready();
- const pid = real.fork.?();
+ const pid = libc("fork")();
if (pid == 0 and agent.ring != null) agent.forked();
return pid;
}
@@ -196,7 +207,7 @@ const Box = struct { start: Start, arg: ?*anyopaque };
fn threadMain(arg: ?*anyopaque) callconv(.c) ?*anyopaque {
const box: *Box = @ptrCast(@alignCast(arg));
const run = box.*;
- real.free.?(box);
+ libc("free")(box);
agent.threadStart();
agent.sud.selector = agent.sud.resting;
const result = run.start(run.arg);
diff --git a/src/cli/run.zig b/src/cli/run.zig
index db3529e..34455dd 100644
--- a/src/cli/run.zig
+++ b/src/cli/run.zig
@@ -129,6 +129,10 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void {
// Everything pumped is recorded and read: whoever waits at it may go on.
if (stops.held) session.ring.resumeAll();
stops.held = false;
+ // Look again for a while before sleeping: a target that finds the
+ // tracer asleep pays a futex wake for the event, more than the event.
+ var spins: usize = 0;
+ while (session.ring.peek() == null and spins < 2000) : (spins += 1) std.atomic.spinLoopHint();
}
const ns = zt.event.now() - start;
if (full) {