From a7504c6bb2ec9e906891e42bc3df28b276e67143 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 12:09:27 -0300 Subject: the backends together: a ring under the debugger, strings that do not collide ztrace record and dbg take --ring, --preload and --alloc: the target gets the ring ztrace run gives it, and the debugger drains it while the target runs (polling for the stop, so a full ring cannot hold both) and before it reports a stop. A program compiled by ztrace cc, with the agent preloaded, stopped at breakpoints, is one recording in order; test-together checks it. Recording such a program showed the debugger's file names and the pass's strings both counted from 1: event.stringId (a hash, top bit set) is now what every producer without a table of its own uses. --- src/trace/host.zig | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'src/trace/host.zig') diff --git a/src/trace/host.zig b/src/trace/host.zig index df2f845..7c43663 100644 --- a/src/trace/host.zig +++ b/src/trace/host.zig @@ -16,6 +16,22 @@ pub const Options = struct { stop_mask: u64 = 0, }; +/// `env` with the ring in it, the agent preloaded when there is one, and +/// `extra` (settings for the agent) after them. +pub fn environment(arena: std.mem.Allocator, env: [:null]const ?[*:0]const u8, ring: *const Ring, preload: ?[]const u8, extra: []const [*:0]const u8) ![:null]const ?[*:0]const u8 { + var envp: std.ArrayList(?[*:0]const u8) = .empty; + for (env) |v| { + const s = std.mem.span(v.?); + if (std.mem.startsWith(u8, s, client.env_fd ++ "=")) continue; + if (preload != null and std.mem.startsWith(u8, s, "LD_PRELOAD=")) continue; + try envp.append(arena, v); + } + try envp.append(arena, try std.fmt.allocPrintSentinel(arena, client.env_fd ++ "={d}", .{ring.fd}, 0)); + if (preload) |lib| try envp.append(arena, try std.fmt.allocPrintSentinel(arena, "LD_PRELOAD={s}", .{lib}, 0)); + for (extra) |v| try envp.append(arena, v); + return envp.toOwnedSliceSentinel(arena, null); +} + pub const Session = struct { ring: Ring, pid: linux.pid_t, @@ -27,16 +43,7 @@ pub const Session = struct { var ring = try Ring.create(options.ring_bytes); errdefer ring.close(); ring.hdr.stop_mask.store(options.stop_mask, .monotonic); - var envp: std.ArrayList(?[*:0]const u8) = .empty; - for (env) |v| { - const s = std.mem.span(v.?); - if (std.mem.startsWith(u8, s, client.env_fd ++ "=")) continue; - if (options.preload != null and std.mem.startsWith(u8, s, "LD_PRELOAD=")) continue; - try envp.append(arena, v); - } - try envp.append(arena, try std.fmt.allocPrintSentinel(arena, client.env_fd ++ "={d}", .{ring.fd}, 0)); - if (options.preload) |lib| try envp.append(arena, try std.fmt.allocPrintSentinel(arena, "LD_PRELOAD={s}", .{lib}, 0)); - const block = try envp.toOwnedSliceSentinel(arena, null); + const block = try environment(arena, env, &ring, options.preload, &.{}); const rc = linux.fork(); if (linux.errno(rc) != .SUCCESS) return error.Fork; -- cgit v1.3