diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-10 16:03:43 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-10 16:05:20 -0300 |
| commit | 66a2a7cf733f6abc1f79561c52d901ce67019fb1 (patch) | |
| tree | b261d533887b37fb5287ad384d4f845348b35d81 /src/trace/Symbols.zig | |
| parent | e0c4bfcc0395a561804e33942028c519f4e267f7 (diff) | |
| download | computeranim-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/trace/Symbols.zig')
| -rw-r--r-- | src/trace/Symbols.zig | 176 |
1 files changed, 170 insertions, 6 deletions
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; |
