Skip to content

Commit

Permalink
Convert Windows debug information to a PDB/Dwarf union
Browse files Browse the repository at this point in the history
  • Loading branch information
mchudleigh committed May 9, 2021
1 parent abdf39e commit 2258be5
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions lib/std/debug.zig
Expand Up @@ -50,6 +50,11 @@ pub const LineInfo = struct {
}
};

const PdbOrDwarf = union(enum) {
pdb: pdb.Pdb,
dwarf: DW.DwarfInfo,
};

var stderr_mutex = std.Thread.Mutex{};

/// Deprecated. Use `std.log` functions for logging or `std.debug.print` for
Expand Down Expand Up @@ -536,7 +541,7 @@ fn populateModule(di: *ModuleDebugInfo, mod: *Module) !void {
if (mod.mod_info.C13ByteSize == 0)
return;

const modi = di.pdb.getStreamById(mod.mod_info.ModuleSymStream) orelse return error.MissingDebugInfo;
const modi = di.debug_data.pdb.getStreamById(mod.mod_info.ModuleSymStream) orelse return error.MissingDebugInfo;

const signature = try modi.reader().readIntLittle(u32);
if (signature != 4)
Expand Down Expand Up @@ -710,8 +715,7 @@ fn readCoffDebugInfo(allocator: *mem.Allocator, coff_file: File) !ModuleDebugInf
var di = ModuleDebugInfo{
.base_address = undefined,
.coff = coff_obj,
.pdb = undefined,
.dwarf = null,
.debug_data = undefined,
.sect_contribs = undefined,
.modules = undefined,
};
Expand All @@ -736,7 +740,8 @@ fn readCoffDebugInfo(allocator: *mem.Allocator, coff_file: File) !ModuleDebugInf
.debug_ranges = debug_ranges_data,
};
try DW.openDwarfDebugInfo(&dwarf, allocator);
di.dwarf = dwarf;
di.debug_data = PdbOrDwarf{ .dwarf = dwarf };
return di;
}

var path_buf: [windows.MAX_PATH]u8 = undefined;
Expand All @@ -745,9 +750,11 @@ fn readCoffDebugInfo(allocator: *mem.Allocator, coff_file: File) !ModuleDebugInf

const path = try fs.path.resolve(allocator, &[_][]const u8{raw_path});

try di.pdb.openFile(di.coff, path);
di.debug_data = PdbOrDwarf{ .pdb = undefined }; // PDB will be populated below in openFile()
const pdb_info = &di.debug_data.pdb;
try pdb_info.openFile(di.coff, path);

var pdb_stream = di.pdb.getStream(pdb.StreamType.Pdb) orelse return error.InvalidDebugInfo;
var pdb_stream = pdb_info.getStream(pdb.StreamType.Pdb) orelse return error.InvalidDebugInfo;
const version = try pdb_stream.reader().readIntLittle(u32);
const signature = try pdb_stream.reader().readIntLittle(u32);
const age = try pdb_stream.reader().readIntLittle(u32);
Expand Down Expand Up @@ -800,10 +807,10 @@ fn readCoffDebugInfo(allocator: *mem.Allocator, coff_file: File) !ModuleDebugInf
return error.MissingDebugInfo;
};

di.pdb.string_table = di.pdb.getStreamById(string_table_index) orelse return error.MissingDebugInfo;
di.pdb.dbi = di.pdb.getStream(pdb.StreamType.Dbi) orelse return error.MissingDebugInfo;
pdb_info.string_table = pdb_info.getStreamById(string_table_index) orelse return error.MissingDebugInfo;
pdb_info.dbi = pdb_info.getStream(pdb.StreamType.Dbi) orelse return error.MissingDebugInfo;

const dbi = di.pdb.dbi;
const dbi = pdb_info.dbi;

// Dbi Header
const dbi_stream_header = try dbi.reader().readStruct(pdb.DbiStreamHeader);
Expand Down Expand Up @@ -1546,8 +1553,7 @@ pub const ModuleDebugInfo = switch (builtin.os.tag) {
},
.uefi, .windows => struct {
base_address: usize,
pdb: pdb.Pdb,
dwarf: ?DW.DwarfInfo,
debug_data: PdbOrDwarf,
coff: *coff.Coff,
sect_contribs: []pdb.SectionContribEntry,
modules: []Module,
Expand All @@ -1560,9 +1566,14 @@ pub const ModuleDebugInfo = switch (builtin.os.tag) {
// Translate the VA into an address into this object
const relocated_address = address - self.base_address;

if (self.dwarf) |*dwarf| {
const dwarf_address = relocated_address + self.coff.pe_header.image_base;
return getSymbolFromDwarf(dwarf_address, dwarf);
switch (self.debug_data) {
.dwarf => |*dwarf| {
const dwarf_address = relocated_address + self.coff.pe_header.image_base;
return getSymbolFromDwarf(dwarf_address, dwarf);
},
.pdb => {
// fallthrough to pdb handling
},
}

var coff_section: *coff.Section = undefined;
Expand Down Expand Up @@ -1661,8 +1672,8 @@ pub const ModuleDebugInfo = switch (builtin.os.tag) {
const subsect_index = checksum_offset + block_hdr.NameIndex;
const chksum_hdr = @ptrCast(*pdb.FileChecksumEntryHeader, &mod.subsect_info[subsect_index]);
const strtab_offset = @sizeOf(pdb.PDBStringTableHeader) + chksum_hdr.FileNameOffset;
try self.pdb.string_table.seekTo(strtab_offset);
const source_file_name = try self.pdb.string_table.readNullTermString(self.allocator());
try self.debug_data.pdb.string_table.seekTo(strtab_offset);
const source_file_name = try self.debug_data.pdb.string_table.readNullTermString(self.allocator());

const line_entry_idx = line_i - 1;

Expand Down

0 comments on commit 2258be5

Please sign in to comment.