authorgravatar for mason@gamesbymason.comMason Remaley <mason@gamesbymason.com> 2026-04-12 14:50:34-07:00
committergravatar for mason@gamesbymason.comMason Remaley <mason@gamesbymason.com> 2026-04-12 23:50:24-07:00
log4ad665d3c8cd6a9d4f6b0e2f065098436142b450
tree26e3f51669fd2a2c44286f2d912e47d4001b3cf9
parente968e6d00473a0fb9de86ae99400503e4e40f0ef

Writes symbols to array list argument


5 files changed, 79 insertions(+), 61 deletions(-)

lib/std/debug.zig+19-10
......@@ -38,8 +38,8 @@ pub const cpu_context = @import("debug/cpu_context.zig");
3838/// pub const init: SelfInfo;
3939/// pub fn deinit(si: *SelfInfo, io: Io) void;
4040///
41/// /// Returns the the symbols and source locations of the instruction at `address`.
42/// pub fn getSymbols(si: *SelfInfo, io: Io, address: usize, include_inline_callers: bool) SelfInfoError![]Symbol;
41/// /// Appends the symbols for the instruction at `address` to `symbols`.
42/// pub fn getSymbols(si: *SelfInfo, io: Io, gpa: Allocator, address: usize, include_inline_callers: bool, symbols: *std.ArrayList(Symbol)) SelfInfoError!void;
4343/// /// Returns a name for the "module" (e.g. shared library or executable image) containing `address`.
4444/// pub fn getModuleName(si: *SelfInfo, io: Io, address: usize) SelfInfoError![]const u8;
4545/// pub fn getModuleSlide(si: *SelfInfo, io: Io, address: usize) SelfInfoError!usize;
......@@ -1190,8 +1190,17 @@ fn printSourceAtAddress(
11901190 t: Io.Terminal,
11911191 options: PrintSourceAddressOptions,
11921192) Writer.Error!void {
1193 const gpa = getDebugInfoAllocator();
1194 const symbols: []Symbol = debug_info.getSymbols(io, options.address, options.resolve_inline_callers) catch |err| {
1193 // In the common case where there's only one symbol, allocate it on the stack. Reserve enough
1194 // space for one item regardless of alignment.
1195 var stack_fallback = std.heap.stackFallback(@sizeOf(Symbol) + @alignOf(Symbol) - 1, getDebugInfoAllocator());
1196 const sfa = stack_fallback.get();
1197 var symbols = std.ArrayList(Symbol).initCapacity(sfa, 1) catch unreachable;
1198 defer {
1199 for (symbols.items) |*symbol| symbol.deinit(sfa);
1200 symbols.deinit(sfa);
1201 }
1202
1203 debug_info.getSymbols(io, sfa, options.address, options.resolve_inline_callers, &symbols) catch |err| {
11951204 t.setColor(.dim) catch {};
11961205 defer t.setColor(.reset) catch {};
11971206 switch (err) {
......@@ -1208,13 +1217,13 @@ fn printSourceAtAddress(
12081217 t.setColor(.reset) catch {};
12091218 },
12101219 }
1211 return printLineInfo(io, t, debug_info, null, options.address, null, null);
12121220 };
1213 defer {
1214 for (symbols) |*symbol| symbol.deinit(gpa);
1215 gpa.free(symbols);
1216 }
1217 for (symbols) |symbol| {
1221
1222 // If we failed to get any symbols, append the unknown symbol. We initialized with a capacity of
1223 // one using a stack fallback allocator so this can't fail.
1224 if (symbols.items.len == 0) symbols.appendAssumeCapacity(.unknown);
1225
1226 for (symbols.items) |symbol| {
12181227 try printLineInfo(
12191228 io,
12201229 t,
lib/std/debug/Dwarf.zig+12-13
......@@ -1545,22 +1545,22 @@ fn getStringGeneric(opt_str: ?[]const u8, offset: u64) ![:0]const u8 {
15451545 return str[casted_offset..last :0];
15461546}
15471547
1548pub fn getSymbols(di: *Dwarf, gpa: Allocator, endian: Endian, address: u64, resolve_inline_callers: bool) std.debug.SelfInfoError![]std.debug.Symbol {
1548pub fn getSymbols(
1549 di: *Dwarf,
1550 gpa: Allocator,
1551 endian: Endian,
1552 address: u64,
1553 resolve_inline_callers: bool,
1554 symbols: *std.ArrayList(std.debug.Symbol),
1555) std.debug.SelfInfoError!void {
15491556 _ = resolve_inline_callers;
15501557
1551 var symbols: std.ArrayList(std.debug.Symbol) = try .initCapacity(gpa, 1);
1552 errdefer {
1553 for (symbols.items) |*symbol| symbol.deinit(gpa);
1554 symbols.deinit(gpa);
1555 }
15561558 const compile_unit = di.findCompileUnit(endian, address) catch |err| switch (err) {
1557 error.EndOfStream, error.Overflow => {
1558 symbols.appendAssumeCapacity(.unknown);
1559 return symbols.toOwnedSlice(gpa);
1560 },
1561 else => |e| return e,
1559 error.EndOfStream => return error.MissingDebugInfo,
1560 error.Overflow => return error.InvalidDebugInfo,
1561 error.ReadFailed, error.InvalidDebugInfo, error.MissingDebugInfo => |e| return e,
15621562 };
1563 symbols.appendAssumeCapacity(.{
1563 try symbols.append(gpa, .{
15641564 .name = di.getSymbolName(address),
15651565 .compile_unit_name = compile_unit.die.getAttrString(di, endian, std.dwarf.AT.name, di.section(.debug_str), compile_unit) catch |err| switch (err) {
15661566 error.MissingDebugInfo, error.InvalidDebugInfo => null,
......@@ -1575,7 +1575,6 @@ pub fn getSymbols(di: *Dwarf, gpa: Allocator, endian: Endian, address: u64, reso
15751575 else => |e| return e,
15761576 },
15771577 });
1578 return symbols.toOwnedSlice(gpa);
15791578}
15801579
15811580/// DWARF5 7.4: "In the 32-bit DWARF format, all values that represent lengths of DWARF sections and
lib/std/debug/SelfInfo/Elf.zig+10-10
......@@ -30,8 +30,14 @@ pub fn deinit(si: *SelfInfo, io: Io) void {
3030 if (si.unwind_cache) |cache| gpa.free(cache);
3131}
3232
33pub fn getSymbols(si: *SelfInfo, io: Io, address: usize, resolve_inline_callers: bool) Error![]std.debug.Symbol {
34 const gpa = std.debug.getDebugInfoAllocator();
33pub fn getSymbols(
34 si: *SelfInfo,
35 io: Io,
36 gpa: Allocator,
37 address: usize,
38 resolve_inline_callers: bool,
39 symbols: *std.ArrayList(std.debug.Symbol),
40) Error!void {
3541 const module = try si.findModule(gpa, io, address, .exclusive);
3642 defer si.rwlock.unlock(io);
3743
......@@ -53,20 +59,14 @@ pub fn getSymbols(si: *SelfInfo, io: Io, address: usize, resolve_inline_callers:
5359 };
5460 loaded_elf.scanned_dwarf = true;
5561 }
56 return dwarf.getSymbols(gpa, native_endian, vaddr, resolve_inline_callers);
62 return dwarf.getSymbols(gpa, native_endian, vaddr, resolve_inline_callers, symbols);
5763 }
5864 // When DWARF is unavailable, fall back to searching the symtab.
59 var symbols: std.ArrayList(std.debug.Symbol) = try .initCapacity(gpa, 1);
60 errdefer {
61 for (symbols.items) |*symbol| symbol.deinit(gpa);
62 symbols.deinit(gpa);
63 }
64 symbols.appendAssumeCapacity(loaded_elf.file.searchSymtab(gpa, vaddr) catch |err| switch (err) {
65 try symbols.append(gpa, loaded_elf.file.searchSymtab(gpa, vaddr) catch |err| switch (err) {
6566 error.NoSymtab, error.NoStrtab => return error.MissingDebugInfo,
6667 error.BadSymtab => return error.InvalidDebugInfo,
6768 error.OutOfMemory => |e| return e,
6869 });
69 return symbols.toOwnedSlice(gpa);
7070}
7171pub fn getModuleName(si: *SelfInfo, io: Io, address: usize) Error![]const u8 {
7272 const gpa = std.debug.getDebugInfoAllocator();
lib/std/debug/SelfInfo/MachO.zig+11-14
......@@ -22,21 +22,21 @@ pub fn deinit(si: *SelfInfo, io: Io) void {
2222 si.modules.deinit(gpa);
2323}
2424
25pub fn getSymbols(si: *SelfInfo, io: Io, address: usize, resolve_inline_callers: bool) Error![]std.debug.Symbol {
25pub fn getSymbols(
26 si: *SelfInfo,
27 io: Io,
28 gpa: Allocator,
29 address: usize,
30 resolve_inline_callers: bool,
31 symbols: *std.ArrayList(std.debug.Symbol),
32) Error!void {
2633 _ = resolve_inline_callers;
2734
28 const gpa = std.debug.getDebugInfoAllocator();
2935 const module = try si.findModule(gpa, io, address);
3036 defer si.mutex.unlock(io);
3137
3238 const file = try module.getFile(gpa, io);
3339
34 var symbols: std.ArrayList(std.debug.Symbol) = try .initCapacity(gpa, 1);
35 errdefer {
36 for (symbols.items) |*symbol| symbol.deinit(gpa);
37 symbols.deinit(gpa);
38 }
39
4040 // This is not necessarily the same as the vmaddr_slide that dyld would report. This is
4141 // because the segments in the file on disk might differ from the ones in memory. Normally
4242 // we wouldn't necessarily expect that to work, but /usr/lib/dyld is incredibly annoying:
......@@ -51,25 +51,23 @@ pub fn getSymbols(si: *SelfInfo, io: Io, address: usize, resolve_inline_callers:
5151
5252 const ofile_dwarf, const ofile_vaddr = file.getDwarfForAddress(gpa, io, vaddr) catch {
5353 // Return at least the symbol name if available.
54 symbols.appendAssumeCapacity(.{
54 return symbols.append(gpa, .{
5555 .name = try file.lookupSymbolName(vaddr),
5656 .compile_unit_name = null,
5757 .source_location = null,
5858 });
59 return symbols.toOwnedSlice(gpa);
6059 };
6160
6261 const compile_unit = ofile_dwarf.findCompileUnit(native_endian, ofile_vaddr) catch {
6362 // Return at least the symbol name if available.
64 symbols.appendAssumeCapacity(.{
63 return symbols.append(gpa, .{
6564 .name = try file.lookupSymbolName(vaddr),
6665 .compile_unit_name = null,
6766 .source_location = null,
6867 });
69 return symbols.toOwnedSlice(gpa);
7068 };
7169
72 symbols.appendAssumeCapacity(.{
70 try symbols.append(gpa, .{
7371 .name = ofile_dwarf.getSymbolName(ofile_vaddr) orelse
7472 try file.lookupSymbolName(vaddr),
7573 .compile_unit_name = compile_unit.die.getAttrString(
......@@ -88,7 +86,6 @@ pub fn getSymbols(si: *SelfInfo, io: Io, address: usize, resolve_inline_callers:
8886 ofile_vaddr,
8987 ) catch null,
9088 });
91 return symbols.toOwnedSlice(gpa);
9289}
9390pub fn getModuleName(si: *SelfInfo, io: Io, address: usize) Error![]const u8 {
9491 _ = si;
lib/std/debug/SelfInfo/Windows.zig+27-14
......@@ -25,13 +25,24 @@ pub fn deinit(si: *SelfInfo, io: Io) void {
2525 si.modules.deinit(gpa);
2626}
2727
28pub fn getSymbols(si: *SelfInfo, io: Io, address: usize, resolve_inline_callers: bool) Error![]std.debug.Symbol {
29 const gpa = std.debug.getDebugInfoAllocator();
28pub fn getSymbols(
29 si: *SelfInfo,
30 io: Io,
31 gpa: Allocator,
32 address: usize,
33 resolve_inline_callers: bool,
34 symbols: *std.ArrayList(std.debug.Symbol),
35) Error!void {
3036 try si.lock.lockShared(io);
3137 defer si.lock.unlockShared(io);
3238 const module = try si.findModule(gpa, address);
3339 const di = try module.getDebugInfo(gpa, io);
34 return di.getSymbols(gpa, address - @intFromPtr(module.entry.DllBase), resolve_inline_callers);
40 return di.getSymbols(
41 gpa,
42 address - @intFromPtr(module.entry.DllBase),
43 resolve_inline_callers,
44 symbols,
45 );
3546}
3647
3748pub fn getModuleName(si: *SelfInfo, io: Io, address: usize) Error![]const u8 {
......@@ -241,7 +252,13 @@ const Module = struct {
241252 arena.deinit();
242253 }
243254
244 fn getSymbols(di: *DebugInfo, gpa: Allocator, vaddr: usize, resolve_inline_callers: bool) Error![]std.debug.Symbol {
255 fn getSymbols(
256 di: *DebugInfo,
257 gpa: Allocator,
258 vaddr: usize,
259 resolve_inline_callers: bool,
260 symbols: *std.ArrayList(std.debug.Symbol),
261 ) Error!void {
245262 pdb: {
246263 const pdb = &(di.pdb orelse break :pdb);
247264 var coff_section: *align(1) const coff.SectionHeader = undefined;
......@@ -275,12 +292,7 @@ const Module = struct {
275292 const addr = vaddr - coff_section.virtual_address;
276293 const maybe_proc = pdb.getProcSym(module, addr);
277294 const compile_unit_name = fs.path.basename(module.obj_file_name);
278 var symbols: std.ArrayList(std.debug.Symbol) = try .initCapacity(gpa, 1);
279 errdefer {
280 for (symbols.items) |*symbol| symbol.deinit(gpa);
281 symbols.deinit(gpa);
282 }
283
295 const symbols_top = symbols.items.len;
284296 if (maybe_proc) |proc| {
285297 const offset_in_func = addr - proc.code_offset;
286298 var last_inlinee: ?u32 = null;
......@@ -308,9 +320,10 @@ const Module = struct {
308320 const loc = maybe_loc orelse continue;
309321
310322 // If we aren't trying to resolve inline callers, and we've matched a
311 // new inline site, we want to overwrite the previous results.
323 // new inline site, we want to overwrite the previously appended
324 // results.
312325 if (!resolve_inline_callers and inline_site.inlinee != last_inlinee) {
313 symbols.items.len = 0;
326 symbols.items.len = symbols_top;
314327 }
315328
316329 // Only resolve the name if we're resolving inline callers, otherwise
......@@ -353,13 +366,13 @@ const Module = struct {
353366 });
354367 }
355368
356 return symbols.toOwnedSlice(gpa);
369 return;
357370 }
358371
359372 dwarf: {
360373 const dwarf = &(di.dwarf orelse break :dwarf);
361374 const addr = vaddr + di.coff_image_base;
362 return dwarf.getSymbols(gpa, native_endian, addr, resolve_inline_callers);
375 return dwarf.getSymbols(gpa, native_endian, addr, resolve_inline_callers, symbols);
363376 }
364377
365378 return error.MissingDebugInfo;