diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-11 11:26:59 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-11 11:27:09 -0300 |
| commit | dcbe8941df205fa8c8805b022f025e1012dede19 (patch) | |
| tree | 9b4df7f904077c07eab979cb10aba9d6a3becf10 /src/trace/Symbols.zig | |
| parent | 6142fa676731e158622ca7956c150c58bc57f1f2 (diff) | |
| download | computeranim-dcbe8941df205fa8c8805b022f025e1012dede19.tar.gz computeranim-dcbe8941df205fa8c8805b022f025e1012dede19.zip | |
symbols: a library's separate debug info, when it is of the same build
The system glibc stays dynamically linked and is symbolized from the
debug file in the debuginfod client's cache or under /usr/lib/debug,
used only when its own build id equals the mapped library's; nothing is
downloaded. Static functions get names (the loader's resolver is
_dl_runtime_resolve_xsavec; main_arena is found by symbol), libc's
frames their file and line as the build saw them, and locals, blocks and
parameter counts follow. A recording says of each module its build id
and where its names came from; ztrace show prints it. Read only when an
address in the module needs a name: 44 ms and 25 MB at the first one.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNCKgYCGiANnEwcEQxa
Diffstat (limited to 'src/trace/Symbols.zig')
| -rw-r--r-- | src/trace/Symbols.zig | 63 |
1 files changed, 58 insertions, 5 deletions
diff --git a/src/trace/Symbols.zig b/src/trace/Symbols.zig index 1459503..da13047 100644 --- a/src/trace/Symbols.zig +++ b/src/trace/Symbols.zig @@ -42,9 +42,15 @@ pub const Maps = struct { pub const Symbol = struct { name: []const u8, addr: u64, size: u64 }; +/// Where a module's debug info is: none, in the file, or in a separate +/// file whose build id is the module's (or, without one, whose CRC is what +/// its .gnu_debuglink says). +pub const Debug = enum(u8) { none, own, separate }; + /// A row of a line table. `key` is the same for the same file of the same -/// compilation unit, and `dir` and `name` together are its path. -pub const Line = struct { key: usize, dir: []const u8, name: []const u8, line: u32, column: u32 }; +/// compilation unit, and `base` (the compilation's directory), `dir` and +/// `name` together are its path, each relative to the one before it. +pub const Line = struct { key: usize, base: []const u8 = "", dir: []const u8, name: []const u8, line: u32, column: u32 }; pub const Module = struct { path: []const u8, @@ -57,6 +63,9 @@ pub const Module = struct { entry: u64 = 0, /// Null when the file could not be read as ELF. file: ?std.debug.ElfFile = null, + /// Its GNU build id (empty: none), and where its debug info came from. + build_id: []const u8 = "", + debug: Debug = .none, /// Functions and objects by address in the file. symbols: []Symbol = &.{}, dwarf_tried: bool = false, @@ -184,7 +193,7 @@ fn line(table: *const Dwarf.CompileUnit.SrcLocCache, entry: Dwarf.CompileUnit.Sr if (index >= table.files.len) return null; const file = &table.files[index]; if (file.dir_index >= table.directories.len) return null; - return .{ .key = @intFromPtr(file), .dir = table.directories[file.dir_index].path, .name = file.path, .line = entry.line, .column = entry.column }; + return .{ .key = @intFromPtr(file), .base = table.directories[0].path, .dir = table.directories[file.dir_index].path, .name = file.path, .line = entry.line, .column = entry.column }; } pub fn deinit(s: *Symbols) void { @@ -287,10 +296,32 @@ fn load(s: *Symbols, path: []const u8) !void { if (std.Io.Dir.cwd().openFile(s.io, path, .{})) |file| { defer file.close(s.io); m.file = std.debug.ElfFile.load(s.gpa, s.io, file, null, &.none) catch null; + // Stripped (a distribution's libc): its debug info from a separate + // file where one is on disk, by build id in the debuginfod client's + // cache or /usr/lib/debug/.build-id, or by .gnu_debuglink beside it. + // Never fetched. + // Strict: a file found by build id is used only when its own note + // says that id (not the cache's directory name), never half. + if (m.file) |*f| if (f.dwarf == null) { + const paths: std.debug.ElfFile.DebugInfoSearchPaths = .native(path); + const id = buildId(f.mapped_file); + if (std.debug.ElfFile.load(s.gpa, s.io, file, id, &paths)) |richer| { + var found = richer; + if (found.mapped_debug_file) |debug| if (id == null or std.mem.eql(u8, buildId(debug) orelse "", id.?)) std.mem.swap(std.debug.ElfFile, f, &found); + found.deinit(s.gpa); + } else |_| {} + }; } else |_| {} if (m.file) |*f| { errdefer f.deinit(s.gpa); - m.symbols = readSymbols(s.gpa, f.mapped_file) catch |err| switch (err) { + m.build_id = buildId(f.mapped_file) orelse ""; + m.debug = if (f.mapped_debug_file != null) .separate else if (f.dwarf != null) .own else .none; + // The debug file's symbol table, where there is one: every function, static ones too. + const debug_symbols: []Symbol = if (f.mapped_debug_file) |bytes| readSymbols(s.gpa, bytes) catch |err| switch (err) { + error.OutOfMemory => |e| return e, + else => &.{}, + } else &.{}; + m.symbols = if (debug_symbols.len > 0) debug_symbols else readSymbols(s.gpa, f.mapped_file) catch |err| switch (err) { error.OutOfMemory => |e| return e, else => &.{}, }; @@ -325,6 +356,26 @@ fn load(s: *Symbols, path: []const u8) !void { s.modules.appendAssumeCapacity(m); } +/// The GNU build id a file's note gives it: what its separate debug file is found by. +fn buildId(bytes: []const u8) ?[]const u8 { + var r: std.Io.Reader = .fixed(bytes); + const header = elf.Header.read(&r) catch return null; + var it = header.iterateSectionHeadersBuffer(bytes); + while (it.next() catch return null) |sh| { + if (sh.sh_type != elf.SHT_NOTE or sh.sh_offset + sh.sh_size > bytes.len) continue; + var notes: std.Io.Reader = .fixed(bytes[sh.sh_offset..][0..sh.sh_size]); + while (notes.seek + 12 <= notes.buffer.len) { + const name_len = notes.takeInt(u32, .little) catch return null; + const desc_len = notes.takeInt(u32, .little) catch return null; + const kind = notes.takeInt(u32, .little) catch return null; + const name = notes.take(std.mem.alignForward(usize, name_len, 4)) catch return null; + const desc = notes.take(std.mem.alignForward(usize, desc_len, 4)) catch return null; + if (kind == elf.NT_GNU_BUILD_ID and std.mem.eql(u8, name[0..@min(name.len, 4)], "GNU\x00")) return desc[0..desc_len]; + } + } + return null; +} + /// The file's entry point and its first loaded segment. fn firstLoad(bytes: []const u8) ?struct { entry: u64, load: elf.Elf64.Phdr } { var r: std.Io.Reader = .fixed(bytes); @@ -362,7 +413,9 @@ fn readSymbols(gpa: std.mem.Allocator, bytes: []const u8) ![]Symbol { .FUNC, .OBJECT, .GNU_IFUNC, .NOTYPE => {}, else => continue, } - try out.append(gpa, .{ .name = std.mem.sliceTo(names[sym.name..], 0), .addr = sym.value, .size = sym.size }); + // A version is no part of a name: `__libc_start_main@@GLIBC_2.34`. + const name = std.mem.sliceTo(names[sym.name..], 0); + try out.append(gpa, .{ .name = name[0 .. std.mem.indexOfScalar(u8, name, '@') orelse name.len], .addr = sym.value, .size = sym.size }); } std.mem.sortUnstable(Symbol, out.items, {}, struct { fn lessThan(_: void, a: Symbol, b: Symbol) bool { |
