diff options
Diffstat (limited to 'src/trace')
| -rw-r--r-- | src/trace/Debugger.zig | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/src/trace/Debugger.zig b/src/trace/Debugger.zig index 13f5ce8..bf24f74 100644 --- a/src/trace/Debugger.zig +++ b/src/trace/Debugger.zig @@ -56,6 +56,8 @@ stack_watch: usize = 0, capture: usize = 256, captured: std.ArrayList(u8) = .empty, watches: std.ArrayList(Watch) = .empty, +/// The variables the target declared through its ring, watched like them. +declared: std.ArrayList(Watch) = .empty, breakpoints: std.ArrayList(Breakpoint) = .empty, /// The current thread's registers at its last stop. regs: Regs = .{}, @@ -168,6 +170,7 @@ pub fn deinit(d: *Debugger) void { d.proc.close(); d.syms.deinit(); d.watches.deinit(d.gpa); + d.declared.deinit(d.gpa); d.breakpoints.deinit(d.gpa); for (d.sites.items) |site| d.gpa.destroy(site); d.sites.deinit(d.gpa); @@ -269,18 +272,36 @@ fn waitAny(d: *Debugger) !ptrace.Stop { /// meanwhile: a target waiting for room in a full ring would never stop. fn wait(d: *Debugger, tid: linux.pid_t) !ptrace.Stop { const ring = d.ring orelse return d.proc.wait(tid); + const tapped: event.Sink = .{ .ctx = d, .emitFn = tap }; var idle: u32 = 0; while (true) { if (try d.proc.poll(tid)) |stop| { - _ = try ring.drain(d.sink.?); + _ = try ring.drain(tapped); return stop; } // A step is back in microseconds: look again before sleeping. - idle = if (try ring.drain(d.sink.?) > 0) 0 else idle + 1; + idle = if (try ring.drain(tapped) > 0) 0 else idle + 1; if (idle > 64) ring.wait(200 * std.time.ns_per_us); } } +/// What the target says of itself goes to the sink as it is; and a +/// variable it declares (`ztrace cc --instr vars`, the agent's +/// `ztrace_watch`) is watched from then on, so its bytes are recorded at +/// every stop with no store instrumented: names and types from the +/// compiler, values from the debugger. +fn tap(ctx: *anyopaque, ev: *const Event, payload: []const u8) anyerror!void { + const d: *Debugger = @ptrCast(@alignCast(ctx)); + if (ev.kind == .variable and ev.a[1] > 0) declared: { + const w: Watch = .{ .addr = ev.a[0], .len = @intCast(@min(ev.a[1], 4096)) }; + for (d.declared.items) |have| if (have.addr == w.addr and have.len == w.len) break :declared; + // A stack's addresses come round again: the oldest make room. + if (d.declared.items.len >= 256) _ = d.declared.orderedRemove(0); + try d.declared.append(d.gpa, w); + } + return d.sink.?.emit(ev.*, payload); +} + /// Runs the target with `request` until something stops it for the caller. /// What does not (a recorded syscall, a thread's birth, a hook that says /// go) is sent as an event here; the stop itself is for `report`. @@ -404,6 +425,7 @@ fn report(d: *Debugger, stop: Stop) !Stop { try d.emitLine(); if (d.stack_watch > 0) try d.snapshot(d.regs.rsp, d.stack_watch); for (d.watches.items) |w| try d.snapshot(w.addr, w.len); + for (d.declared.items) |w| try d.snapshot(w.addr, w.len); try d.emitCode(); try d.emitTelescope(); } |
