diff --git a/lib/std/debug.zig b/lib/std/debug.zig index c998006c8c33..95f0c42de14d 100644 --- a/lib/std/debug.zig +++ b/lib/std/debug.zig @@ -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 @@ -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) @@ -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, }; @@ -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; @@ -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); @@ -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); @@ -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, @@ -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; @@ -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;