summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md13
-rw-r--r--build/llvm.zig5
-rw-r--r--src/trace/Debugger.zig26
3 files changed, 41 insertions, 3 deletions
diff --git a/README.md b/README.md
index 3e181ca..b9beb28 100644
--- a/README.md
+++ b/README.md
@@ -159,6 +159,19 @@ ztrace perf --faults --syscalls --alloc --preload zig-out/lib/libztrace-agent.so
zig build test-together
```
+The cleanest machine-level recording with names in it is `ztrace cc
+--instr vars` under the debugger: the pass declares each variable (name,
+type, address) and instruments nothing else, the debugger watches every
+variable declared through the ring and records its bytes at each stop, and
+the lines, registers, disassembly and memory touches are the debugger's
+own, of code that is otherwise the program's.
+
+```sh
+ztrace cc --instr vars -- clang -O0 targets/instr_sort.c -o sort
+ztrace record --by line --ring -- ./sort && ztrace show out.ztrace 140
+ unsorted: [8]i32 = 05 00 00 00 02 00 00 00 ... i: i32 = 02 00 00 00
+```
+
Static probes (USDT, DTrace's) need neither BPF nor root this way: `ztrace
record --by break -b 'usdt:libc:*' -- ./prog` arms each probe as a
breakpoint that records a `probe` event with its arguments and runs on.
diff --git a/build/llvm.zig b/build/llvm.zig
index 94b7501..7794ecd 100644
--- a/build/llvm.zig
+++ b/build/llvm.zig
@@ -10,12 +10,15 @@ pub fn add(ctx: Ctx) std.Build.LazyPath {
// The runtime runs inside the target at every event: optimized whatever
// mode the rest is built in.
- const fast = b.createModule(.{ .root_source_file = b.path("src/trace/root.zig"), .target = ctx.target, .optimize = .ReleaseFast, .pic = true });
+ // No debug info either: a debugger stepping the program by line then
+ // runs through the runtime's calls instead of stopping in them.
+ const fast = b.createModule(.{ .root_source_file = b.path("src/trace/root.zig"), .target = ctx.target, .optimize = .ReleaseFast, .pic = true, .strip = true });
const rt = b.addLibrary(.{ .name = "ztrace-rt", .root_module = b.createModule(.{
.root_source_file = b.path("src/instr/rt.zig"),
.target = ctx.target,
.optimize = .ReleaseFast,
.pic = true,
+ .strip = true,
.imports = &.{.{ .name = "ztrace", .module = fast }},
}) });
b.installArtifact(rt);
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();
}