authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-09-08 15:31:09+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-09-30 13:44:52+01:00
logac4d633ed691159ea61130182a1b51635a95e228
tree74e812bb31ea93d5499d63e81a41eb48a1394005
parentf40fbdb3b300233a2fc880bad77c986259c38b0f
signaturelock-open Commit is signed but in an unrecognized format.

std: fix debug.Info and debug.Coverage


3 files changed, 12 insertions(+), 8 deletions(-)

lib/std/debug/Coverage.zig+2-1
......@@ -145,6 +145,7 @@ pub const ResolveAddressesDwarfError = Dwarf.ScanError;
145145pub fn resolveAddressesDwarf(
146146 cov: *Coverage,
147147 gpa: Allocator,
148 endian: std.builtin.Endian,
148149 /// Asserts the addresses are in ascending order.
149150 sorted_pc_addrs: []const u64,
150151 /// Asserts its length equals length of `sorted_pc_addrs`.
......@@ -184,7 +185,7 @@ pub fn resolveAddressesDwarf(
184185 if (cu.src_loc_cache == null) {
185186 cov.mutex.unlock();
186187 defer cov.mutex.lock();
187 d.populateSrcLocCache(gpa, cu) catch |err| switch (err) {
188 d.populateSrcLocCache(gpa, endian, cu) catch |err| switch (err) {
188189 error.MissingDebugInfo, error.InvalidDebugInfo => {
189190 out.* = SourceLocation.invalid;
190191 continue :next_pc;
lib/std/debug/Dwarf.zig+2-2
......@@ -652,7 +652,7 @@ fn scanAllCompileUnits(di: *Dwarf, allocator: Allocator, endian: Endian) ScanErr
652652 }
653653}
654654
655pub fn populateRanges(d: *Dwarf, gpa: Allocator) ScanError!void {
655pub fn populateRanges(d: *Dwarf, gpa: Allocator, endian: Endian) ScanError!void {
656656 assert(d.ranges.items.len == 0);
657657
658658 for (d.compile_unit_list.items, 0..) |*cu, cu_index| {
......@@ -665,7 +665,7 @@ pub fn populateRanges(d: *Dwarf, gpa: Allocator) ScanError!void {
665665 continue;
666666 }
667667 const ranges_value = cu.die.getAttr(AT.ranges) orelse continue;
668 var iter = DebugRangeIterator.init(ranges_value, d, cu) catch continue;
668 var iter = DebugRangeIterator.init(ranges_value, d, endian, cu) catch continue;
669669 while (try iter.next()) |range| {
670670 // Not sure why LLVM thinks it's OK to emit these...
671671 if (range.start == range.end) continue;
lib/std/debug/Info.zig+8-5
......@@ -24,14 +24,15 @@ coverage: *Coverage,
2424pub const LoadError = Dwarf.ElfModule.LoadError;
2525
2626pub fn load(gpa: Allocator, path: Path, coverage: *Coverage) LoadError!Info {
27 var sections: Dwarf.SectionArray = Dwarf.null_section_array;
28 var elf_module = try Dwarf.ElfModule.load(gpa, path, null, null, &sections, null);
29 try elf_module.dwarf.populateRanges(gpa);
27 var elf_module = try Dwarf.ElfModule.load(gpa, path, null, null, null, null);
28 // This is correct because `Dwarf.ElfModule` currently only supports native-endian ELF files.
29 const endian = @import("builtin").target.cpu.arch.endian();
30 try elf_module.dwarf.populateRanges(gpa, endian);
3031 var info: Info = .{
3132 .address_map = .{},
3233 .coverage = coverage,
3334 };
34 try info.address_map.put(gpa, elf_module.base_address, elf_module);
35 try info.address_map.put(gpa, 0, elf_module);
3536 return info;
3637}
3738
......@@ -58,5 +59,7 @@ pub fn resolveAddresses(
5859 assert(sorted_pc_addrs.len == output.len);
5960 if (info.address_map.entries.len != 1) @panic("TODO");
6061 const elf_module = &info.address_map.values()[0];
61 return info.coverage.resolveAddressesDwarf(gpa, sorted_pc_addrs, output, &elf_module.dwarf);
62 // This is correct because `Dwarf.ElfModule` currently only supports native-endian ELF files.
63 const endian = @import("builtin").target.cpu.arch.endian();
64 return info.coverage.resolveAddressesDwarf(gpa, endian, sorted_pc_addrs, output, &elf_module.dwarf);
6265}