summaryrefslogtreecommitdiff
path: root/src/agent/lib.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/agent/lib.zig')
-rw-r--r--src/agent/lib.zig39
1 files changed, 25 insertions, 14 deletions
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);