diff options
Diffstat (limited to 'src/agent/agent.zig')
| -rw-r--r-- | src/agent/agent.zig | 58 |
1 files changed, 40 insertions, 18 deletions
diff --git a/src/agent/agent.zig b/src/agent/agent.zig index ecf2768..22b4f8d 100644 --- a/src/agent/agent.zig +++ b/src/agent/agent.zig @@ -28,8 +28,15 @@ var cut: u64 = 0; var state: std.atomic.Value(u8) = .init(0); threadlocal var started = false; -/// The most bytes an event of ours carries; more is cut short, or split. -pub const max_payload = 1024; +/// The most bytes an event of ours carries: a label is cut to it, memory is +/// split at it. A record with it fits the smallest ring. +pub const max_payload = 2048; +/// The most bytes of a syscall's path or buffer that go into its event +/// (ZTRACE_CAPTURE): recordings are meant to stay small. +pub var capture: usize = 256; +/// The most memory one `watch` or `snapshot` takes: more is a mistake (a +/// whole heap, a whole mapping), refused with a line on stderr. +pub const max_region = 1 << 20; /// Every entry point comes through here first. A Zig program that wants its /// hooks and syscalls from the start calls it at the top of main; for the @@ -41,10 +48,11 @@ pub fn init() void { var buf: [1 << 16]u8 = undefined; const env = client.environ(&buf); cut = if (client.getenv(env, "ZTRACE_CUT")) |v| std.fmt.parseInt(u64, v, 0) catch 0 else 0; + if (client.getenv(env, "ZTRACE_CAPTURE")) |v| capture = @min(std.fmt.parseInt(usize, v, 0) catch capture, max_payload); ring = r; var maps = symbols.Maps.open(null) orelse return; - while (maps.next()) |m| emit(.map, 0, .{ m.start, m.end - m.start, m.prot, m.offset, 0, 0 }, text(m.path)); + while (maps.next()) |m| emit(.map, 0, .{ m.start, m.end - m.start, m.prot, m.offset, 0, 0 }, text(m.path, max_payload)); maps.close(); threadStart(); @@ -52,13 +60,13 @@ pub fn init() void { var specs = std.mem.tokenizeScalar(u8, list, ','); while (specs.next()) |spec| { const region = symbols.find(null, spec) orelse symbols.Symbol{ .addr = 0, .size = 0 }; - if (region.size != 0) watch(@as([*]const u8, @ptrFromInt(region.addr))[0..region.size], spec) else unknown(spec); + if (region.size != 0) watch(@as([*]const u8, @ptrFromInt(region.addr))[0..region.size], spec) else complain(&.{ "nothing called ", spec }); } } 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 unknown(spec); + if (symbols.find(null, spec)) |function| hook.add(function.addr) else complain(&.{ "nothing called ", spec }); } } allocations = client.getenv(env, "ZTRACE_ALLOC") != null; @@ -68,9 +76,22 @@ pub fn init() void { } } -/// The user asked for something that is not there: say so where they will see it. -fn unknown(spec: []const u8) void { - for ([_][]const u8{ "ztrace agent: nothing called ", spec, "\n" }) |part| _ = linux.write(2, part.ptr, part.len); +/// What the user asked for cannot be done: said where they will see it. +fn complain(parts: []const []const u8) void { + const was = sud.selector; + sud.selector = sud.allow; + defer sud.selector = was; + _ = linux.write(2, "ztrace agent: ", 14); + for (parts) |part| _ = linux.write(2, part.ptr, part.len); + _ = linux.write(2, "\n", 1); +} + +/// Memory too large to record whole: said, and refused. +fn tooLarge(memory: []const u8, what: []const u8) bool { + if (memory.len <= max_region) return false; + var buf: [32]u8 = undefined; + complain(&.{ what, " of ", std.fmt.bufPrint(&buf, "{d}", .{memory.len}) catch "", " bytes refused: more than 1 MiB. Watch the part that matters." }); + return true; } pub fn threadId() u32 { @@ -110,10 +131,10 @@ pub fn stringId(string: []const u8) u64 { return std.hash.Wyhash.hash(0, string); } -/// `bytes` as a payload: cut to `max_payload`, and never the length of -/// `Regs`, which the recorder would take for registers. -pub fn text(bytes: []const u8) []const u8 { - const len = @min(bytes.len, max_payload); +/// `bytes` as a payload: cut to `max`, and never the length of `Regs`, which +/// the recorder would take for registers. +pub fn text(bytes: []const u8, max: usize) []const u8 { + const len = @min(bytes.len, max); return bytes[0 .. len - @intFromBool(len == @sizeOf(zt.Regs))]; } @@ -132,29 +153,30 @@ pub fn write(addr: u64, bytes: []const u8) void { /// A point in the run worth a moment: a `mark` event with a text and a value. pub fn mark(label: []const u8, value: u64) void { init(); - emit(.mark, 0, .{ value, 0, 0, 0, 0, 0 }, text(label)); + emit(.mark, 0, .{ value, 0, 0, 0, 0, 0 }, text(label, max_payload)); } /// Names `memory` (a `variable` event), records it, and keeps recording what /// changes in it: only the bytes that changed, before each event that ends a -/// moment. It must stay mapped. Watched from inside a hooked call, it is -/// forgotten when that call returns. +/// moment. At most `max_region` bytes, which must stay mapped. Watched from +/// inside a hooked call, it is forgotten when that call returns. pub fn watch(memory: []const u8, name: []const u8) void { init(); - if (ring == null) return; + if (ring == null or tooLarge(memory, "watch")) return; const was = sud.selector; sud.selector = sud.allow; defer sud.selector = was; const id = stringId(name); - emit(.string, 0, .{ id, 0, 0, 0, 0, 0 }, text(name)); + emit(.string, 0, .{ id, 0, 0, 0, 0, 0 }, text(name, max_payload)); emit(.variable, 0, .{ @intFromPtr(memory.ptr), memory.len, id, 0, 0, 0 }, ""); write(@intFromPtr(memory.ptr), memory); regions.add(memory, threadId(), hook.open()); } -/// Records `memory` as it is now, once. +/// Records `memory` as it is now, once. At most `max_region` bytes. pub fn snapshot(memory: []const u8) void { init(); + if (ring == null or tooLarge(memory, "snapshot")) return; write(@intFromPtr(memory.ptr), memory); } |
