std: fix debug.Info and debug.Coverage

This commit is contained in:
mlugg 2025-09-08 15:31:09 +01:00
parent f40fbdb3b3
commit ac4d633ed6
No known key found for this signature in database
GPG key ID: 3F5B7DCCBF4AF02E
3 changed files with 12 additions and 8 deletions

View file

@ -145,6 +145,7 @@ pub const ResolveAddressesDwarfError = Dwarf.ScanError;
pub fn resolveAddressesDwarf( pub fn resolveAddressesDwarf(
cov: *Coverage, cov: *Coverage,
gpa: Allocator, gpa: Allocator,
endian: std.builtin.Endian,
/// Asserts the addresses are in ascending order. /// Asserts the addresses are in ascending order.
sorted_pc_addrs: []const u64, sorted_pc_addrs: []const u64,
/// Asserts its length equals length of `sorted_pc_addrs`. /// Asserts its length equals length of `sorted_pc_addrs`.
@ -184,7 +185,7 @@ pub fn resolveAddressesDwarf(
if (cu.src_loc_cache == null) { if (cu.src_loc_cache == null) {
cov.mutex.unlock(); cov.mutex.unlock();
defer cov.mutex.lock(); defer cov.mutex.lock();
d.populateSrcLocCache(gpa, cu) catch |err| switch (err) { d.populateSrcLocCache(gpa, endian, cu) catch |err| switch (err) {
error.MissingDebugInfo, error.InvalidDebugInfo => { error.MissingDebugInfo, error.InvalidDebugInfo => {
out.* = SourceLocation.invalid; out.* = SourceLocation.invalid;
continue :next_pc; continue :next_pc;

View file

@ -652,7 +652,7 @@ fn scanAllCompileUnits(di: *Dwarf, allocator: Allocator, endian: Endian) ScanErr
} }
} }
pub fn populateRanges(d: *Dwarf, gpa: Allocator) ScanError!void { pub fn populateRanges(d: *Dwarf, gpa: Allocator, endian: Endian) ScanError!void {
assert(d.ranges.items.len == 0); assert(d.ranges.items.len == 0);
for (d.compile_unit_list.items, 0..) |*cu, cu_index| { for (d.compile_unit_list.items, 0..) |*cu, cu_index| {
@ -665,7 +665,7 @@ pub fn populateRanges(d: *Dwarf, gpa: Allocator) ScanError!void {
continue; continue;
} }
const ranges_value = cu.die.getAttr(AT.ranges) orelse continue; const ranges_value = cu.die.getAttr(AT.ranges) orelse continue;
var iter = DebugRangeIterator.init(ranges_value, d, cu) catch continue; var iter = DebugRangeIterator.init(ranges_value, d, endian, cu) catch continue;
while (try iter.next()) |range| { while (try iter.next()) |range| {
// Not sure why LLVM thinks it's OK to emit these... // Not sure why LLVM thinks it's OK to emit these...
if (range.start == range.end) continue; if (range.start == range.end) continue;

View file

@ -24,14 +24,15 @@ coverage: *Coverage,
pub const LoadError = Dwarf.ElfModule.LoadError; pub const LoadError = Dwarf.ElfModule.LoadError;
pub fn load(gpa: Allocator, path: Path, coverage: *Coverage) LoadError!Info { pub fn load(gpa: Allocator, path: Path, coverage: *Coverage) LoadError!Info {
var sections: Dwarf.SectionArray = Dwarf.null_section_array; var elf_module = try Dwarf.ElfModule.load(gpa, path, null, null, null, null);
var elf_module = try Dwarf.ElfModule.load(gpa, path, null, null, &sections, null); // This is correct because `Dwarf.ElfModule` currently only supports native-endian ELF files.
try elf_module.dwarf.populateRanges(gpa); const endian = @import("builtin").target.cpu.arch.endian();
try elf_module.dwarf.populateRanges(gpa, endian);
var info: Info = .{ var info: Info = .{
.address_map = .{}, .address_map = .{},
.coverage = coverage, .coverage = coverage,
}; };
try info.address_map.put(gpa, elf_module.base_address, elf_module); try info.address_map.put(gpa, 0, elf_module);
return info; return info;
} }
@ -58,5 +59,7 @@ pub fn resolveAddresses(
assert(sorted_pc_addrs.len == output.len); assert(sorted_pc_addrs.len == output.len);
if (info.address_map.entries.len != 1) @panic("TODO"); if (info.address_map.entries.len != 1) @panic("TODO");
const elf_module = &info.address_map.values()[0]; const elf_module = &info.address_map.values()[0];
return info.coverage.resolveAddressesDwarf(gpa, sorted_pc_addrs, output, &elf_module.dwarf); // This is correct because `Dwarf.ElfModule` currently only supports native-endian ELF files.
const endian = @import("builtin").target.cpu.arch.endian();
return info.coverage.resolveAddressesDwarf(gpa, endian, sorted_pc_addrs, output, &elf_module.dwarf);
} }