summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-10 16:03:43 -0300
committerGabriel Schneider <[email protected]>2026-11-10 16:05:20 -0300
commit66a2a7cf733f6abc1f79561c52d901ce67019fb1 (patch)
treeb261d533887b37fb5287ad384d4f845348b35d81 /src
parente0c4bfcc0395a561804e33942028c519f4e267f7 (diff)
downloadcomputeranim-66a2a7cf733f6abc1f79561c52d901ce67019fb1.tar.gz
computeranim-66a2a7cf733f6abc1f79561c52d901ce67019fb1.zip
trace: what the context draws is in the recording; SQLite on the ladder
An instruction's string carries its source line and, for a direct call, how many parameters the callee takes (DWARF); telescope.frames gives each frame's extent on the stack; with --alloc the debugger watches each live allocation and the allocator's header before it, changes only, 256 KiB a stop at most. C++ names are shown and found as the source has them (the command links the C++ runtime for its demangler). SQLite 3.50.4 through every backend, measured (README, bench/sqlite.sh). It found two faults: a file compiled by its full path was recorded under a path joined onto the compile directory, and ztrace run noticed a target's exit up to 10 ms late. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Diffstat (limited to 'src')
-rw-r--r--src/cli/dbg.zig1
-rw-r--r--src/cli/disasm.c9
-rw-r--r--src/cli/perf.zig2
-rw-r--r--src/cli/probe.zig2
-rw-r--r--src/cli/record.zig1
-rw-r--r--src/cli/run.zig3
-rw-r--r--src/cli/util.zig16
-rw-r--r--src/trace/Debugger.zig130
-rw-r--r--src/trace/Symbols.zig176
-rw-r--r--src/trace/debugger_test.zig35
-rw-r--r--src/trace/telescope.zig100
-rw-r--r--src/trace/together_test.zig36
12 files changed, 460 insertions, 51 deletions
diff --git a/src/cli/dbg.zig b/src/cli/dbg.zig
index 22923be..5fcf88e 100644
--- a/src/cli/dbg.zig
+++ b/src/cli/dbg.zig
@@ -87,6 +87,7 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void {
defer s.d.deinit();
if (produced) |p| s.d.ring = p.ring;
s.d.disasm = disasm.one;
+ s.d.syms.demangle = util.demangle;
s.d.telescope = 3;
defer s.stop_payload.deinit(init.gpa);
s.d.stack_watch = stack_bytes;
diff --git a/src/cli/disasm.c b/src/cli/disasm.c
index 82fd0bb..9b15e0c 100644
--- a/src/cli/disasm.c
+++ b/src/cli/disasm.c
@@ -76,7 +76,8 @@ static int slot(x86_reg reg) {
// The instruction at `code`, which is at `addr`: its text into `text`, its
// length returned; 0 where there is no instruction. With `regs` (the
// thread's, about to run it), the memory it touches into `touches`, at
-// most `*count` of them, and how many into `*count`.
+// most `*count` of them, and how many into `*count`. A branch is told
+// without them too, its target where the instruction alone says it.
size_t zt_disasm(const uint8_t *code, size_t len, uint64_t addr, char *text, size_t cap, const uint64_t *regs, struct zt_touch *touches, size_t *count, struct zt_branch *branch) {
if (!handle) {
if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) != CS_ERR_OK) return 0;
@@ -89,17 +90,17 @@ size_t zt_disasm(const uint8_t *code, size_t len, uint64_t addr, char *text, siz
if (cs_disasm(handle, code, len, addr, 1, &insn) != 1) return 0;
size_t size = insn->size;
snprintf(text, cap, insn->op_str[0] ? "%s %s" : "%s", insn->mnemonic, insn->op_str);
- if (regs && branch && insn->detail) {
+ if (branch && insn->detail) {
const cs_x86 *x = &insn->detail->x86;
branch->kind = cs_insn_group(handle, insn, CS_GRP_RET) ? 3 : cs_insn_group(handle, insn, CS_GRP_CALL) ? 1 : cs_insn_group(handle, insn, CS_GRP_JUMP) ? 2 : 0;
- branch->taken = holds(insn->id, regs[18], regs[11]);
+ branch->taken = regs ? holds(insn->id, regs[18], regs[11]) : 1;
branch->indirect = 0;
branch->target = 0;
if (branch->kind == 1 || branch->kind == 2) {
const cs_x86_op *op = &x->operands[0];
if (x->op_count < 1) branch->kind = 0;
else if (op->type == X86_OP_IMM) branch->target = (uint64_t)op->imm;
- else if (op->type == X86_OP_REG && slot(op->reg) >= 0) branch->target = regs[slot(op->reg)];
+ else if (op->type == X86_OP_REG && slot(op->reg) >= 0) branch->target = regs ? regs[slot(op->reg)] : 0;
else if (op->type == X86_OP_MEM) {
// Its address is the first touch below: the target is what is there.
branch->indirect = 1;
diff --git a/src/cli/perf.zig b/src/cli/perf.zig
index 48523ce..f43c51f 100644
--- a/src/cli/perf.zig
+++ b/src/cli/perf.zig
@@ -142,7 +142,7 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void {
// Symbols: the program's own from the start, a library's once the
// loader has mapped it, which is at the program's entry point.
- var symbols: zt.Symbols = .{ .gpa = init.gpa, .io = init.io };
+ var symbols: zt.Symbols = .{ .gpa = init.gpa, .io = init.io, .demangle = util.demangle };
defer symbols.deinit();
symbols.free(try symbols.refresh(proc.pid));
var at_entry = false;
diff --git a/src/cli/probe.zig b/src/cli/probe.zig
index 311f2c0..abe67c1 100644
--- a/src/cli/probe.zig
+++ b/src/cli/probe.zig
@@ -126,7 +126,7 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void {
defer tracer.close();
if (syscalls) tracer.syscalls() catch |err| refused(err, &tracer);
if (sched) tracer.sched(btf) catch |err| refused(err, &tracer);
- var symbols: zt.Symbols = .{ .gpa = init.gpa, .io = init.io };
+ var symbols: zt.Symbols = .{ .gpa = init.gpa, .io = init.io, .demangle = util.demangle };
defer symbols.deinit();
symbols.free(try symbols.refresh(proc.pid));
for (places.items) |p| {
diff --git a/src/cli/record.zig b/src/cli/record.zig
index f5281ca..a82fa9e 100644
--- a/src/cli/record.zig
+++ b/src/cli/record.zig
@@ -108,6 +108,7 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void {
defer d.deinit();
if (produced) |p| d.ring = p.ring;
d.disasm = disasm.one;
+ d.syms.demangle = util.demangle;
d.telescope = telescope;
d.syscalls = if (by == .syscall) .stop else .record;
d.stack_watch = stack_bytes;
diff --git a/src/cli/run.zig b/src/cli/run.zig
index 34455dd..00c18d1 100644
--- a/src/cli/run.zig
+++ b/src/cli/run.zig
@@ -119,7 +119,8 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void {
const start = zt.event.now();
var full = false;
- while (session.pump(stops.sink(), 10 * std.time.ns_per_ms) catch |err| switch (err) {
+ // An idle wait is how late the target's exit is noticed: a millisecond.
+ while (session.pump(stops.sink(), std.time.ns_per_ms) catch |err| switch (err) {
error.RecordingFull => full: {
full = true;
break :full false;
diff --git a/src/cli/util.zig b/src/cli/util.zig
index 1d7ee4c..652956d 100644
--- a/src/cli/util.zig
+++ b/src/cli/util.zig
@@ -62,6 +62,22 @@ pub const Producer = struct {
}
};
+extern fn __cxa_demangle(name: [*:0]const u8, out: ?[*]u8, len: ?*usize, status: *c_int) ?[*:0]u8;
+
+/// What `Symbols.demangle` is set to: the C++ runtime's demangler.
+pub fn demangle(name: []const u8, buf: []u8) ?[]const u8 {
+ if (!std.mem.startsWith(u8, name, "_Z") or name.len >= buf.len) return null;
+ @memcpy(buf[0..name.len], name);
+ buf[name.len] = 0;
+ var status: c_int = 0;
+ const out = __cxa_demangle(buf[0..name.len :0], null, null, &status) orelse return null;
+ defer std.c.free(out);
+ const text = std.mem.span(out);
+ if (text.len > buf.len) return null;
+ @memcpy(buf[0..text.len], text);
+ return buf[0..text.len];
+}
+
/// The arguments after a command's own, as the target's argv.
pub fn targetArgv(arena: std.mem.Allocator, args: []const [:0]const u8) ![:null]?[*:0]const u8 {
const argv = try arena.allocSentinel(?[*:0]const u8, args.len, null);
diff --git a/src/trace/Debugger.zig b/src/trace/Debugger.zig
index bf24f74..bbdaee6 100644
--- a/src/trace/Debugger.zig
+++ b/src/trace/Debugger.zig
@@ -19,8 +19,9 @@
//! no file ("[stack]").
//!
//! Memory is never recorded wholesale: only the watched regions, the stack
-//! window and what syscalls point at, cut at `capture`, and of those only
-//! the words that changed since they were last sent.
+//! window, the live allocations a ring tells of and what syscalls point
+//! at, cut at `capture`, and of those only the words that changed since
+//! they were last sent.
const Debugger = @This();
const std = @import("std");
@@ -33,6 +34,7 @@ const Ring = @import("Ring.zig");
const notes = @import("usdt.zig");
const Symbols = @import("Symbols.zig");
const syscalls_table = @import("syscalls.zig");
+const Line = @import("store.zig").Line;
const P = linux.PTRACE;
gpa: std.mem.Allocator,
@@ -58,6 +60,12 @@ 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,
+/// The live allocations the target tells of through its ring (`--alloc`),
+/// by address: watched too, each with the allocator's header before it.
+allocations: std.ArrayList(Watch) = .empty,
+/// The most read of them at a stop, in bytes: a stop's cost must not grow
+/// with the target's heap.
+allocation_watch: usize = 256 << 10,
breakpoints: std.ArrayList(Breakpoint) = .empty,
/// The current thread's registers at its last stop.
regs: Regs = .{},
@@ -171,6 +179,7 @@ pub fn deinit(d: *Debugger) void {
d.syms.deinit();
d.watches.deinit(d.gpa);
d.declared.deinit(d.gpa);
+ d.allocations.deinit(d.gpa);
d.breakpoints.deinit(d.gpa);
for (d.sites.items) |site| d.gpa.destroy(site);
d.sites.deinit(d.gpa);
@@ -299,9 +308,47 @@ fn tap(ctx: *anyopaque, ev: *const Event, payload: []const u8) anyerror!void {
if (d.declared.items.len >= 256) _ = d.declared.orderedRemove(0);
try d.declared.append(d.gpa, w);
}
+ // An allocation is watched from its birth to its free, kept in address order.
+ if (ev.kind == .alloc or ev.kind == .free) {
+ const items = d.allocations.items;
+ const at = std.sort.lowerBound(Watch, items, ev.a[0], struct {
+ fn order(want: u64, have: Watch) std.math.Order {
+ return std.math.order(want, have.addr);
+ }
+ }.order);
+ const found = at < items.len and items[at].addr == ev.a[0];
+ if (ev.kind == .free) {
+ if (found) _ = d.allocations.orderedRemove(at);
+ } else if (found) items[at].len = @intCast(ev.a[1]) else try d.allocations.insert(d.gpa, at, .{ .addr = ev.a[0], .len = @intCast(ev.a[1]) });
+ }
return d.sink.?.emit(ev.*, payload);
}
+/// The live allocations as they are now, each with the 16 bytes before it
+/// (glibc's chunk header, musl's), and of a large one its first 4 KiB, as
+/// a declared variable: neighbours are read as one region, and what is
+/// sent of them is what changed.
+fn snapshotAllocations(d: *Debugger) !void {
+ var from: u64 = 0;
+ var to: u64 = 0;
+ var room = d.allocation_watch;
+ for (d.allocations.items) |a| {
+ const start = a.addr -| 16;
+ const end = a.addr + @min(a.len, 4096);
+ // shortcut: past `allocation_watch` the allocations highest in memory go unread, upgrade by reading only what the instructions' `touch`es wrote.
+ if (end - start > room) break;
+ room -= end - start;
+ if (start <= to) {
+ to = @max(to, end);
+ continue;
+ }
+ if (to > from) try d.snapshot(from, to - from);
+ from = start;
+ to = end;
+ }
+ if (to > from) try d.snapshot(from, to - from);
+}
+
/// 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`.
@@ -426,6 +473,7 @@ fn report(d: *Debugger, stop: Stop) !Stop {
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.snapshotAllocations();
try d.emitCode();
try d.emitTelescope();
}
@@ -496,16 +544,29 @@ fn emitCode(d: *Debugger) !void {
/// Records the instruction at `addr` as a string (`event.codeId`), once: a
/// byte for its length, a byte for the length of the one before it (0:
-/// not known), then its text. Returns its length; null for no instruction.
+/// not known), then its text; and when either is known, a zero, its source
+/// line (`store.Line`) and a byte for how many parameters a direct call's
+/// callee takes, plus one (0: not known). Returns its length; null for no
+/// instruction.
fn emitInstruction(d: *Debugger, insnAt: @typeInfo(@TypeOf(d.disasm)).optional.child, addr: u64, back: u8) !?u8 {
if (d.code_len.get(addr)) |len| return len;
var code: [15]u8 = undefined;
- var text: [192]u8 = undefined;
+ var text: [2 + 192 + 2 + @sizeOf(Line)]u8 = undefined;
const n = d.read(addr, &code);
- const insn = insnAt(code[0..n], addr, text[2..], null, &.{}) orelse return null;
+ const insn = insnAt(code[0..n], addr, text[2..][0..192], null, &.{}) orelse return null;
text[0] = insn.len;
text[1] = back;
- try d.emit(.{ .kind = .string, .a = .{ event.codeId(addr), 0, 0, 0, 0, 0 } }, text[0 .. 2 + insn.text.len]);
+ var len = 2 + insn.text.len;
+ const b = insn.branch;
+ const params: u8 = if (b.kind == 1 and b.indirect == 0 and b.target != 0) (if (d.syms.parameters(b.target)) |p| p +| 1 else 0) else 0;
+ const line: Line = if (d.lineAt(addr)) |l| .{ .file = try d.fileId(l), .line = l.line, .column = l.column } else .{};
+ if (line.line != 0 or params != 0) {
+ text[len] = 0;
+ @memcpy(text[len + 1 ..][0..@sizeOf(Line)], std.mem.asBytes(&line));
+ text[len + 1 + @sizeOf(Line)] = params;
+ len += 2 + @sizeOf(Line);
+ }
+ try d.emit(.{ .kind = .string, .a = .{ event.codeId(addr), 0, 0, 0, 0, 0 } }, text[0..len]);
try d.code_len.put(d.gpa, addr, insn.len);
return insn.len;
}
@@ -627,21 +688,26 @@ pub fn decode(d: *Debugger, nr: u16) ![]const u8 {
return text[0 .. text.len - @intFromBool(text.len == @sizeOf(Regs))];
}
+/// The string id of a line's file: its path, recorded the first time.
+fn fileId(d: *Debugger, l: Symbols.Line) !u64 {
+ if (d.file_ids.get(l.key)) |id| return id;
+ // A file compiled by its full path names itself whole.
+ const path = if (std.fs.path.isAbsolute(l.name)) try d.gpa.dupe(u8, l.name) else try std.fs.path.join(d.gpa, &.{ l.dir, l.name });
+ const named = d.files.getOrPut(d.gpa, path) catch |err| {
+ d.gpa.free(path);
+ return err;
+ };
+ if (named.found_existing) d.gpa.free(path) else {
+ named.value_ptr.* = event.stringId(path);
+ try d.emit(.{ .kind = .string, .a = .{ named.value_ptr.*, 0, 0, 0, 0, 0 } }, path);
+ }
+ try d.file_ids.put(d.gpa, l.key, named.value_ptr.*);
+ return named.value_ptr.*;
+}
+
fn emitLine(d: *Debugger) !void {
const l = d.lineAt(d.regs.rip) orelse return;
- const file = d.file_ids.get(l.key) orelse id: {
- const path = try std.fs.path.join(d.gpa, &.{ l.dir, l.name });
- const named = d.files.getOrPut(d.gpa, path) catch |err| {
- d.gpa.free(path);
- return err;
- };
- if (named.found_existing) d.gpa.free(path) else {
- named.value_ptr.* = event.stringId(path);
- try d.emit(.{ .kind = .string, .a = .{ named.value_ptr.*, 0, 0, 0, 0, 0 } }, path);
- }
- try d.file_ids.put(d.gpa, l.key, named.value_ptr.*);
- break :id named.value_ptr.*;
- };
+ const file = try d.fileId(l);
if (file == d.last_line.file and l.line == d.last_line.line) return;
d.last_line = .{ .file = file, .line = l.line };
try d.emit(.{ .kind = .line, .a = .{ file, l.line, l.column, d.regs.rip, 0, 0 } }, "");
@@ -904,19 +970,39 @@ fn know(d: *Debugger, addr: u64) void {
/// caller's address is still on top of the stack.
// shortcut: a function that keeps no frame hides its caller (a leaf, or one stopped inside its prologue), upgrade with the unwind tables (`std.debug.Dwarf.Unwind`).
pub fn backtrace(mem: anytype, regs: *const Regs, at_entry: bool, out: []u64) []u64 {
+ return walk(u64, mem, regs, at_entry, out);
+}
+
+/// A frame of the call chain: its pc, and the stack it holds, from `low`
+/// (the stack pointer, for the newest) up to `high`, just past its return
+/// address: where the caller's stack pointer was before the call (the
+/// CFA). `high` is `low` where the frame's end is not known.
+pub const Frame = struct { pc: u64, low: u64, high: u64 };
+
+/// `backtrace`, each frame with its extent on the stack.
+pub fn frames(mem: anytype, regs: *const Regs, at_entry: bool, out: []Frame) []Frame {
+ return walk(Frame, mem, regs, at_entry, out);
+}
+
+fn walk(comptime T: type, mem: anytype, regs: *const Regs, at_entry: bool, out: []T) []T {
var n: usize = 0;
var pc = regs.rip;
var fp = regs.rbp;
+ var low = regs.rsp;
var frame: [16]u8 = undefined;
if (at_entry and out.len > 1 and mem.read(regs.rsp, frame[0..8]) == 8) {
- out[0] = pc;
+ // Its frame is the return address alone.
+ out[0] = if (T == u64) pc else .{ .pc = pc, .low = low, .high = low + 8 };
n = 1;
+ low += 8;
pc = std.mem.readInt(u64, frame[0..8], .little);
}
while (n < out.len) {
- out[n] = pc;
+ const known = fp != 0 and mem.read(fp, &frame) == 16;
+ out[n] = if (T == u64) pc else .{ .pc = pc, .low = low, .high = if (known and fp >= low) fp + 16 else low };
n += 1;
- if (fp == 0 or mem.read(fp, &frame) != 16) break;
+ if (!known) break;
+ low = @max(low, fp + 16);
const next = std.mem.readInt(u64, frame[0..8], .little);
pc = std.mem.readInt(u64, frame[8..16], .little);
// Frames lie up the stack: one that does not is not a frame.
diff --git a/src/trace/Symbols.zig b/src/trace/Symbols.zig
index ace4c41..3c37040 100644
--- a/src/trace/Symbols.zig
+++ b/src/trace/Symbols.zig
@@ -14,6 +14,11 @@ maps: Maps = .{},
modules: std.ArrayList(Module) = .empty,
/// The line-table row `lineAt` answered from last, as target addresses.
row: struct { start: u64 = 0, len: u64 = 0, line: ?Line = null } = .{},
+/// A C++ name as its source has it, into `buf`; null for no C++ name.
+/// Whoever has a demangler sets it (the ztrace command: the C++ runtime's).
+/// Names are then shown and found so, without their parameters:
+/// "v8::internal::Heap::CollectGarbage" for "_ZN2v88internal4Heap14Coll...".
+demangle: ?*const fn (name: []const u8, buf: []u8) ?[]const u8 = null,
pub const Mapping = struct {
start: u64,
@@ -52,6 +57,9 @@ pub const Module = struct {
/// Functions and objects by address in the file.
symbols: []Symbol = &.{},
dwarf_tried: bool = false,
+ /// How many parameters each function takes, by its first address in
+ /// the file: read from DWARF on first use.
+ params: ?std.AutoHashMapUnmanaged(u64, u8) = null,
/// The line tables, opened on first use. Null without debug info.
fn dwarf(m: *Module, gpa: std.mem.Allocator) ?*Dwarf {
@@ -97,6 +105,7 @@ fn line(table: *const Dwarf.CompileUnit.SrcLocCache, entry: Dwarf.CompileUnit.Sr
pub fn deinit(s: *Symbols) void {
for (s.modules.items) |*m| {
if (m.file) |*f| f.deinit(s.gpa);
+ if (m.params) |*p| p.deinit(s.gpa);
s.gpa.free(m.symbols);
s.gpa.free(m.path);
}
@@ -292,12 +301,18 @@ pub fn symbolAt(s: *Symbols, addr: u64) ?Symbol {
return null;
}
-/// A symbol by name: the executable's first, then the libraries'.
+/// A symbol by name: the executable's first, then the libraries'. As the
+/// linker has it, else (with `demangle`) as the source does.
pub fn find(s: *Symbols, name: []const u8) ?Symbol {
- for (s.modules.items) |*m| {
- if (m.end == 0) continue;
- for (m.symbols) |sym| {
- if (std.mem.eql(u8, sym.name, name)) return .{ .name = sym.name, .addr = sym.addr + m.bias, .size = sym.size };
+ var buf: [1024]u8 = undefined;
+ for ([_]bool{ false, true }) |source| {
+ if (source and s.demangle == null) break;
+ for (s.modules.items) |*m| {
+ if (m.end == 0) continue;
+ for (m.symbols) |sym| {
+ const have = if (source) s.sourceName(sym.name, &buf) orelse continue else sym.name;
+ if (std.mem.eql(u8, have, name)) return .{ .name = sym.name, .addr = sym.addr + m.bias, .size = sym.size };
+ }
}
}
return null;
@@ -348,6 +363,120 @@ pub fn lineAddress(s: *Symbols, file: []const u8, wanted: u32) ?u64 {
return if (best) |b| b.addr else null;
}
+/// How many parameters the function that begins at `addr` takes, as its
+/// DWARF says (a variadic tail not counted). Null without debug info, or
+/// where no function it describes begins.
+pub fn parameters(s: *Symbols, addr: u64) ?u8 {
+ const m = s.moduleAt(addr) orelse return null;
+ const d = m.dwarf(s.gpa) orelse return null;
+ if (m.params == null) {
+ m.params = .empty;
+ countParameters(s.gpa, d, &m.params.?) catch {};
+ }
+ return m.params.?.get(addr - m.bias);
+}
+
+/// Each function's parameters, by its first address: the formal parameters
+/// among the children of each subprogram with code, in .debug_info. The
+/// units and their abbreviations are the ones `std.debug.Dwarf` read.
+fn countParameters(gpa: std.mem.Allocator, d: *const Dwarf, out: *std.AutoHashMapUnmanaged(u64, u8)) !void {
+ const info = d.section(.debug_info) orelse return;
+ const DW = std.dwarf;
+ for (d.compile_unit_list.items) |*cu| {
+ const table = for (d.abbrev_table_list.items) |*t| {
+ if (t.offset == cu.abbrev_offset) break t;
+ } else continue;
+ const offset: u64 = if (cu.format == .@"64") 8 else 4;
+ var r: std.Io.Reader = .fixed(info[0..@min(info.len, cu.offset + cu.size)]);
+ // Past the unit's header: its length, version, (DWARF 5) unit type,
+ // the abbreviations' offset and the address size.
+ r.seek = @intCast(cu.offset + (if (offset == 8) @as(u64, 12) else 4) + 2 + offset + 1 + @intFromBool(cu.version >= 5));
+ var depth: usize = 0;
+ // The function whose parameters are being counted, and how deep it is.
+ var function: ?struct { pc: u64, depth: usize } = null;
+ while (r.seek < r.buffer.len) {
+ const code = try r.takeLeb128(u64);
+ if (code == 0) {
+ depth -|= 1;
+ continue;
+ }
+ // Codes are mostly numbered from 1, in order.
+ const abbrev = if (code <= table.abbrevs.len and table.abbrevs[code - 1].code == code) &table.abbrevs[code - 1] else for (table.abbrevs) |*a| {
+ if (a.code == code) break a;
+ } else return error.InvalidDebugInfo;
+ var low_pc: ?u64 = null;
+ for (abbrev.attrs) |attr| {
+ const v = try formValue(&r, attr.form_id, cu);
+ if (attr.id == DW.AT.low_pc) low_pc = switch (attr.form_id) {
+ DW.FORM.addr => v,
+ DW.FORM.addrx, DW.FORM.addrx1, DW.FORM.addrx2, DW.FORM.addrx3, DW.FORM.addrx4, DW.FORM.GNU_addr_index => debugAddr(d, cu, v),
+ else => null,
+ };
+ }
+ const here = depth;
+ if (abbrev.has_children) depth += 1;
+ if (function) |f| if (here <= f.depth) {
+ function = null;
+ };
+ switch (abbrev.tag_id) {
+ DW.TAG.subprogram => if (low_pc) |pc| {
+ try out.put(gpa, pc, 0);
+ if (abbrev.has_children) function = .{ .pc = pc, .depth = here };
+ },
+ DW.TAG.formal_parameter => if (function) |f| if (here == f.depth + 1) {
+ out.getPtr(f.pc).?.* +|= 1;
+ },
+ else => {},
+ }
+ }
+ }
+}
+
+/// The address at `index` of a unit's part of .debug_addr.
+fn debugAddr(d: *const Dwarf, cu: *const Dwarf.CompileUnit, index: u64) ?u64 {
+ const table = d.section(.debug_addr) orelse return null;
+ const at = cu.addr_base + index * cu.addr_size_bytes;
+ if (cu.addr_size_bytes != 8 or at + 8 > table.len) return null;
+ return std.mem.readInt(u64, table[@intCast(at)..][0..8], .little);
+}
+
+/// An attribute's value, read past: the number it is for the forms that
+/// are one, 0 for the rest (strings, blocks).
+fn formValue(r: *std.Io.Reader, form_id: u64, cu: *const Dwarf.CompileUnit) !u64 {
+ const F = std.dwarf.FORM;
+ const offset: usize = if (cu.format == .@"64") 8 else 4;
+ var form = form_id;
+ while (form == F.indirect) form = try r.takeLeb128(u64);
+ return switch (form) {
+ F.addr => try r.takeVarInt(u64, .little, cu.addr_size_bytes),
+ F.data1, F.ref1, F.flag, F.strx1, F.addrx1 => try r.takeByte(),
+ F.data2, F.ref2, F.strx2, F.addrx2 => try r.takeInt(u16, .little),
+ F.strx3, F.addrx3 => try r.takeInt(u24, .little),
+ F.data4, F.ref4, F.ref_sup4, F.strx4, F.addrx4 => try r.takeInt(u32, .little),
+ F.data8, F.ref8, F.ref_sig8, F.ref_sup8 => try r.takeInt(u64, .little),
+ F.udata, F.ref_udata, F.strx, F.addrx, F.loclistx, F.rnglistx, F.GNU_addr_index, F.GNU_str_index => try r.takeLeb128(u64),
+ F.sdata => @bitCast(try r.takeLeb128(i64)),
+ F.strp, F.line_strp, F.sec_offset, F.strp_sup, F.GNU_ref_alt, F.GNU_strp_alt => try r.takeVarInt(u64, .little, offset),
+ F.ref_addr => try r.takeVarInt(u64, .little, if (cu.version == 2) cu.addr_size_bytes else offset),
+ F.string => string: {
+ _ = try r.takeSentinel(0);
+ break :string 0;
+ },
+ F.data16 => skip(r, 16),
+ F.block1 => skip(r, try r.takeByte()),
+ F.block2 => skip(r, try r.takeInt(u16, .little)),
+ F.block4 => skip(r, try r.takeInt(u32, .little)),
+ F.block, F.exprloc => skip(r, try r.takeLeb128(usize)),
+ F.flag_present, F.implicit_const => 0,
+ else => error.InvalidDebugInfo,
+ };
+}
+
+fn skip(r: *std.Io.Reader, n: usize) !u64 {
+ try r.discardAll(n);
+ return 0;
+}
+
/// `addr` for a reader: "fib+12 (fib.c:5)", "libc.so.6!write+18",
/// "libc.so.6!27781" where no symbol covers it, "[stack]+1f40".
pub fn describe(s: *Symbols, w: *std.Io.Writer, addr: u64) !void {
@@ -356,6 +485,40 @@ pub fn describe(s: *Symbols, w: *std.Io.Writer, addr: u64) !void {
if (s.lineAt(addr)) |l| try w.print(" ({s}:{d})", .{ std.fs.path.basename(l.name), l.line });
}
+/// A symbol's name as `demangle` makes it, its parameters left out.
+fn sourceName(s: *const Symbols, name: []const u8, buf: []u8) ?[]const u8 {
+ const full = (s.demangle orelse return null)(name, buf) orelse return null;
+ // They are the last parenthesis: "(anonymous namespace)::f(int) const".
+ const close = std.mem.lastIndexOfScalar(u8, full, ')') orelse return full;
+ var depth: usize = 0;
+ var i = close + 1;
+ while (i > 0) {
+ i -= 1;
+ if (full[i] == ')') depth += 1;
+ if (full[i] == '(') {
+ depth -= 1;
+ if (depth == 0) return full[0..i];
+ }
+ }
+ return full;
+}
+
+test "a C++ name is shown without its parameters" {
+ const s: Symbols = .{ .gpa = std.testing.allocator, .io = std.testing.io, .demangle = struct {
+ fn as(name: []const u8, buf: []u8) ?[]const u8 {
+ @memcpy(buf[0..name.len], name);
+ return buf[0..name.len];
+ }
+ }.as };
+ var buf: [128]u8 = undefined;
+ for ([_][2][]const u8{
+ .{ "v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace)", "v8::internal::Heap::CollectGarbage" },
+ .{ "(anonymous namespace)::f(int) const", "(anonymous namespace)::f" },
+ .{ "S::operator()(int)", "S::operator()" },
+ .{ "v8::internal::FLAG_x", "v8::internal::FLAG_x" },
+ }) |case| try std.testing.expectEqualStrings(case[1], s.sourceName(case[0], &buf).?);
+}
+
/// The name of `addr` alone: "fib+12", "libc.so.6!write+18", "[stack]+1f40".
/// False, and nothing written, for an address with none.
pub fn label(s: *Symbols, w: *std.Io.Writer, addr: u64) !bool {
@@ -363,7 +526,8 @@ pub fn label(s: *Symbols, w: *std.Io.Writer, addr: u64) !bool {
const sym = s.symbolAt(addr);
// The executable is the first module mapped: its symbols need no prefix.
if (sym == null or m != &s.modules.items[0]) try w.print("{s}!", .{std.fs.path.basename(m.path)});
- if (sym) |found| try w.print("{s}+{d}", .{ found.name, addr - found.addr }) else try w.print("{x}", .{addr - m.bias});
+ var buf: [1024]u8 = undefined;
+ if (sym) |found| try w.print("{s}+{d}", .{ s.sourceName(found.name, &buf) orelse found.name, addr - found.addr }) else try w.print("{x}", .{addr - m.bias});
return true;
}
const m = s.mappingAt(addr) orelse return false;
diff --git a/src/trace/debugger_test.zig b/src/trace/debugger_test.zig
index e1191a9..bfee1d6 100644
--- a/src/trace/debugger_test.zig
+++ b/src/trace/debugger_test.zig
@@ -97,6 +97,41 @@ test "a breakpoint on fib is hit once a call, in a position-independent executab
try std.testing.expectEqual(@as(u8, 0), code);
}
+test "DWARF says how many parameters a function takes" {
+ var d = try spawn(targets.list, "2", null);
+ defer d.deinit();
+ for ([_][]const u8{ "push", "reverse", "list_done", "main" }, [_]u8{ 2, 1, 1, 2 }) |name, n| {
+ try std.testing.expectEqual(@as(?u8, n), d.syms.parameters(d.symbol(name).?.addr));
+ }
+ // Only where a function begins; nor in a library without debug info.
+ try std.testing.expectEqual(@as(?u8, null), d.syms.parameters(d.symbol("push").?.addr + 1));
+ try std.testing.expectEqual(@as(?u8, null), d.syms.parameters(d.regs.rip));
+}
+
+test "a recording by `ztrace record` has the line of each instruction round the pc, and a call's parameters" {
+ var trace = try zt.Trace.open(gpa, io, targets.fib_recording);
+ defer trace.close();
+ // The first stop at the breakpoint on fib, and the instructions from there.
+ const m, const pc = find: for (0..trace.len) |i| {
+ const m = try trace.moment(i);
+ var events = try m.events(gpa);
+ defer events.deinit(gpa);
+ while (events.next()) |item| if (item.ev.kind == .breakpoint) break :find .{ m, item.ev.a[0] };
+ } else return error.NoBreakpoint;
+ var text: [256]u8 = undefined;
+ const here = (try zt.telescope.instruction(m, pc, &text)).?;
+ try std.testing.expectEqual(@as(u32, 4), here.line.?.line);
+ var name: [std.Io.Dir.max_path_bytes]u8 = undefined;
+ try std.testing.expect(std.mem.endsWith(u8, (try m.string(here.line.?.file, &name)).?, "fib.c"));
+ // fib calls itself once (-O1 made the other call a loop): with its one parameter.
+ var at = pc;
+ const call = while (try zt.telescope.instruction(m, at, &text)) |insn| : (at += insn.len) {
+ if (std.mem.startsWith(u8, insn.text, "call")) break insn;
+ } else return error.NoCall;
+ try std.testing.expectEqual(@as(?u8, 1), call.params);
+ try std.testing.expectEqual(@as(u32, 5), call.line.?.line);
+}
+
test "step walks the source lines, in and out of calls, as gdb does" {
var d = try spawn(targets.list, "2", null);
defer d.deinit();
diff --git a/src/trace/telescope.zig b/src/trace/telescope.zig
index ba7caa3..7e3e3e0 100644
--- a/src/trace/telescope.zig
+++ b/src/trace/telescope.zig
@@ -6,6 +6,7 @@ const std = @import("std");
const event = @import("event.zig");
const store = @import("store.zig");
const Moment = store.Moment;
+const Debugger = @import("Debugger.zig");
/// The kind of memory an address is in, from its mapping.
pub const Region = enum {
@@ -91,31 +92,50 @@ pub fn chase(m: Moment, value: u64, links: []Link, text: []u8) ![]Link {
/// The instruction at `addr` as the recording has it (`event.codeId`): its
/// length, the length of the one before it (0 when the recording does not
-/// know: the one before is then not at `addr - back`), and its text. Null
-/// where none was recorded.
-pub fn instruction(m: Moment, addr: u64, buf: []u8) !?struct { len: u8, back: u8, text: []const u8 } {
+/// know: the one before is then not at `addr - back`), its text, the
+/// source line it is of, and for a direct call how many parameters the
+/// callee takes (its DWARF's). Null where none was recorded; `line` and
+/// `params` null where they are not known, or `buf` cut them off.
+pub fn instruction(m: Moment, addr: u64, buf: []u8) !?struct { len: u8, back: u8, text: []const u8, line: ?store.Line = null, params: ?u8 = null } {
const s = (try m.string(event.codeId(addr), buf)) orelse return null;
if (s.len < 2) return null;
- return .{ .len = s[0], .back = s[1], .text = s[2..] };
+ const text = std.mem.sliceTo(s[2..], 0);
+ // After the text, when there is one: a zero, the line, the parameters + 1.
+ const more = s[2 + text.len ..];
+ if (more.len < 2 + @sizeOf(store.Line)) return .{ .len = s[0], .back = s[1], .text = text };
+ const line = std.mem.bytesToValue(store.Line, more[1..][0..@sizeOf(store.Line)]);
+ const params = more[1 + @sizeOf(store.Line)];
+ return .{ .len = s[0], .back = s[1], .text = text, .line = if (line.line != 0) line else null, .params = if (params != 0) params - 1 else null };
}
/// The call chain of a thread at a moment, by frame pointer, from what the
/// recording holds of its stack: the pc, then each return address outwards.
/// It ends where the frames were not recorded.
pub fn backtrace(m: Moment, regs: *const event.Regs, out: []u64) ![]u64 {
- const Memory = struct {
- m: Moment,
- pub fn read(mem: @This(), addr: u64, buf: []u8) usize {
- var known: [16]bool = undefined;
- mem.m.read(addr, buf, known[0..buf.len]) catch return 0;
- return if (std.mem.indexOfScalar(bool, known[0..buf.len], false) == null) buf.len else 0;
- }
- };
- // At a function's first instruction its frame is not made yet: the
- // return address is on top of the stack.
+ return Debugger.backtrace(Memory{ .m = m }, regs, try atEntry(m, regs), out);
+}
+
+/// `backtrace`, each frame with the stack it holds (`Debugger.Frame`): a
+/// call's span over the stack words.
+pub fn frames(m: Moment, regs: *const event.Regs, out: []Debugger.Frame) ![]Debugger.Frame {
+ return Debugger.frames(Memory{ .m = m }, regs, try atEntry(m, regs), out);
+}
+
+/// A moment's memory as the frame walk reads it: all of it recorded, or none.
+const Memory = struct {
+ m: Moment,
+ pub fn read(mem: @This(), addr: u64, buf: []u8) usize {
+ var known: [16]bool = undefined;
+ mem.m.read(addr, buf, known[0..buf.len]) catch return 0;
+ return if (std.mem.indexOfScalar(bool, known[0..buf.len], false) == null) buf.len else 0;
+ }
+};
+
+/// At a function's first instruction its frame is not made yet: the
+/// return address is on top of the stack.
+fn atEntry(m: Moment, regs: *const event.Regs) !bool {
var name: [256]u8 = undefined;
- const at_entry = if (try m.string(regs.rip, &name)) |n| std.mem.endsWith(u8, n, "+0") else false;
- return @import("Debugger.zig").backtrace(Memory{ .m = m }, regs, at_entry, out);
+ return if (try m.string(regs.rip, &name)) |n| std.mem.endsWith(u8, n, "+0") else false;
}
test "a chain is followed to a string, to code, to a number, and no further than is known" {
@@ -187,5 +207,53 @@ test "a chain is followed to a string, to code, to a number, and no further than
try std.testing.expectEqual(@as(u8, 3), insn.len);
try std.testing.expectEqual(@as(u8, 1), insn.back);
try std.testing.expectEqualStrings("mov rbp, rsp", insn.text);
+ try std.testing.expect(insn.line == null and insn.params == null);
try std.testing.expect(try instruction(m, 0x400126, &text) == null);
}
+
+test "an instruction's line and its callee's parameters; each frame's extent on the stack" {
+ const gpa = std.testing.allocator;
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var dir_buf: [std.Io.Dir.max_path_bytes]u8 = undefined;
+ const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)];
+ const path = try std.fs.path.join(gpa, &.{ dir, "t.ztrace" });
+ defer gpa.free(path);
+
+ var rec = try store.Recorder.create(gpa, std.testing.io, path);
+ // A call on line 12 of file 9 to a function of two parameters, as the debugger records it.
+ const line: store.Line = .{ .file = 9, .line = 12, .column = 3 };
+ try rec.emit(.{ .kind = .string, .a = .{ event.codeId(0x400200), 0, 0, 0, 0, 0 } }, "\x05\x00call 0x400300\x00" ++ std.mem.asBytes(&line) ++ "\x03");
+ try rec.emit(.{ .kind = .string, .a = .{ 0x400123, 0, 0, 0, 0, 0 } }, "main+4");
+ try rec.emit(.{ .kind = .string, .a = .{ 0x400300, 0, 0, 0, 0, 0 } }, "f+0");
+ // rsp 7ff100, rbp 7ff120 -> 7ff140 -> 0: three frames, the last of no known end.
+ const stack = [_]u64{ 0x7ff140, 0x400205, 0, 0, 0x7ff160, 0x400400 };
+ try rec.emit(.{ .kind = .mem_write, .tid = 1, .a = .{ 0x7ff120, 48, 0, 0, 0, 0 } }, std.mem.sliceAsBytes(&stack));
+ try rec.emit(.{ .kind = .mem_write, .tid = 1, .a = .{ 0x7ff100, 8, 0, 0, 0, 0 } }, std.mem.asBytes(&@as(u64, 0x400205)));
+ try rec.emit(.{ .kind = .mark, .tid = 1 }, "");
+ try rec.close();
+
+ var trace = try store.Trace.open(gpa, std.testing.io, path);
+ defer trace.close();
+ const m = try trace.moment(0);
+ var text: [64]u8 = undefined;
+ const call = (try instruction(m, 0x400200, &text)).?;
+ try std.testing.expectEqualStrings("call 0x400300", call.text);
+ try std.testing.expectEqual(line, call.line.?);
+ try std.testing.expectEqual(@as(?u8, 2), call.params);
+
+ const Frame = Debugger.Frame;
+ var out: [4]Frame = undefined;
+ var regs: event.Regs = .{ .rip = 0x400123, .rsp = 0x7ff100, .rbp = 0x7ff120 };
+ try std.testing.expectEqualSlices(Frame, &.{
+ .{ .pc = 0x400123, .low = 0x7ff100, .high = 0x7ff130 },
+ .{ .pc = 0x400205, .low = 0x7ff130, .high = 0x7ff150 },
+ .{ .pc = 0x400400, .low = 0x7ff150, .high = 0x7ff150 },
+ }, try frames(m, &regs, &out));
+ // At a function's first instruction its frame is its return address.
+ regs.rip = 0x400300;
+ try std.testing.expectEqualSlices(Frame, &.{
+ .{ .pc = 0x400300, .low = 0x7ff100, .high = 0x7ff108 },
+ .{ .pc = 0x400205, .low = 0x7ff108, .high = 0x7ff130 },
+ }, (try frames(m, &regs, &out))[0..2]);
+}
diff --git a/src/trace/together_test.zig b/src/trace/together_test.zig
new file mode 100644
index 0000000..09abb7b
--- /dev/null
+++ b/src/trace/together_test.zig
@@ -0,0 +1,36 @@
+//! The recording `test-together` makes (a list built by `ztrace cc`, its
+//! allocations told by the agent, stopped at breakpoints by the debugger),
+//! read back: found through the `recording` options.
+const std = @import("std");
+const zt = @import("ztrace");
+const recording = @import("recording");
+
+test "at every stop each live allocation is there to read, with glibc's header before it" {
+ const gpa = std.testing.allocator;
+ var trace = try zt.Trace.open(gpa, std.testing.io, recording.together);
+ defer trace.close();
+ var checked: usize = 0;
+ for (0..trace.len) |i| {
+ const m = try trace.moment(i);
+ var events = try m.events(gpa);
+ defer events.deinit(gpa);
+ const stop = while (events.next()) |item| {
+ if (item.ev.kind == .breakpoint) break true;
+ } else false;
+ if (!stop) continue;
+ const heap = try m.heap(gpa);
+ defer gpa.free(heap);
+ for (heap) |a| {
+ var bytes: [16 + 16]u8 = undefined;
+ var known: [16 + 16]bool = undefined;
+ try std.testing.expectEqual(@as(u64, 16), a.size);
+ try m.read(a.ptr - 16, &bytes, &known);
+ try std.testing.expect(std.mem.allEqual(bool, &known, true));
+ // A chunk of 32 bytes: the 16 asked for, 8 of header, rounded to 16.
+ try std.testing.expectEqual(@as(u64, 32), std.mem.readInt(u64, bytes[8..16], .little) & ~@as(u64, 7));
+ checked += 1;
+ }
+ }
+ // Five nodes, at the stops after each was made.
+ try std.testing.expect(checked >= 5);
+}