summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/trace/client.zig43
1 files changed, 32 insertions, 11 deletions
diff --git a/src/trace/client.zig b/src/trace/client.zig
index c8f2eea..157edc6 100644
--- a/src/trace/client.zig
+++ b/src/trace/client.zig
@@ -13,6 +13,15 @@ var ring: Ring = undefined;
/// 0 untried, 1 attaching, 2 attached, 3 no ring.
var state: std.atomic.Value(u8) = .init(0);
+/// The calling thread's id, asked for once. 0 is "not asked yet": a forked
+/// child sets it back, having inherited its parent's.
+pub threadlocal var tid: u32 = 0;
+
+pub fn threadId() u32 {
+ if (tid == 0) tid = @intCast(linux.gettid());
+ return tid;
+}
+
/// The process's ring, attached on first use. Null when it is not traced.
pub fn get() ?*Ring {
while (true) switch (state.load(.acquire)) {
@@ -20,7 +29,9 @@ pub fn get() ?*Ring {
3 => return null,
1 => _ = linux.sched_yield(),
else => if (state.cmpxchgStrong(0, 1, .acquire, .monotonic) == null) {
- const attached = if (envFd()) |fd| Ring.attach(fd) catch null else null;
+ var buf: [1 << 16]u8 = undefined;
+ const fd = if (getenv(environ(&buf), env_fd)) |v| std.fmt.parseInt(linux.fd_t, v, 10) catch null else null;
+ const attached = if (fd) |f| Ring.attach(f) catch null else null;
if (attached) |r| ring = r;
state.store(if (attached != null) 2 else 3, .release);
},
@@ -29,28 +40,38 @@ pub fn get() ?*Ring {
pub fn emit(kind: event.Kind, aux: u16, a: [6]u64, payload: []const u8) void {
const r = get() orelse return;
- // shortcut: a gettid syscall per event; cache it per thread once the agent has TLS.
- r.emit(.{ .ts = event.now(), .tid = @intCast(linux.gettid()), .kind = kind, .aux = aux, .a = a }, payload);
+ r.emit(.{ .ts = event.now(), .tid = threadId(), .kind = kind, .aux = aux, .a = a }, payload);
}
-/// `ZTRACE_RING_FD` from /proc/self/environ: a preloaded library cannot
-/// count on libc's environ being set up yet.
-fn envFd() ?linux.fd_t {
+/// The environment the process started with, read from /proc/self/environ
+/// into `buf`: a preloaded library cannot count on libc's environ being set
+/// up yet.
+pub fn environ(buf: []u8) []const u8 {
const rc = linux.open("/proc/self/environ", .{}, 0);
- if (linux.errno(rc) != .SUCCESS) return null;
+ if (linux.errno(rc) != .SUCCESS) return buf[0..0];
const fd: linux.fd_t = @intCast(rc);
defer _ = linux.close(fd);
- var buf: [1 << 16]u8 = undefined;
var len: usize = 0;
while (len < buf.len) {
const n = linux.read(fd, buf[len..].ptr, buf.len - len);
if (linux.errno(n) != .SUCCESS or n == 0) break;
len += n;
}
- var vars = std.mem.splitScalar(u8, buf[0..len], 0);
+ return buf[0..len];
+}
+
+/// The value of `name` in a block `environ` returned.
+pub fn getenv(block: []const u8, name: []const u8) ?[]const u8 {
+ var vars = std.mem.splitScalar(u8, block, 0);
while (vars.next()) |v| {
- if (!std.mem.startsWith(u8, v, env_fd ++ "=")) continue;
- return std.fmt.parseInt(linux.fd_t, v[env_fd.len + 1 ..], 10) catch null;
+ if (v.len > name.len and v[name.len] == '=' and std.mem.startsWith(u8, v, name)) return v[name.len + 1 ..];
}
return null;
}
+
+test "a variable is found by its whole name" {
+ const block = "ZTRACE_RING_FD_X=9\x00ZTRACE_RING_FD=5\x00EMPTY=\x00";
+ try std.testing.expectEqualStrings("5", getenv(block, env_fd).?);
+ try std.testing.expectEqualStrings("", getenv(block, "EMPTY").?);
+ try std.testing.expect(getenv(block, "ZTRACE") == null);
+}