authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-02-18 13:06:17+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-02-20 19:41:29+01:00
logfabab58528f4b55378418d838c8d56b7c7da5015
treeb4e0cbe84eccd7ef1c000cc86e9f0e48f48bd691
parentb9c02e4076204c49500fd1d0dcb91420aed17f03

less hideous


2 files changed, 36 insertions(+), 52 deletions(-)

lib/std/debug.zig+31-47
...@@ -822,23 +822,20 @@ pub const OpenSelfDebugInfoError = error{...@@ -822,23 +822,20 @@ pub const OpenSelfDebugInfoError = error{
822/// TODO resources https://github.com/ziglang/zig/issues/4353822/// TODO resources https://github.com/ziglang/zig/issues/4353
823/// TODO once https://github.com/ziglang/zig/issues/3157 is fully implemented,823/// TODO once https://github.com/ziglang/zig/issues/3157 is fully implemented,
824/// make this `noasync fn` and remove the individual noasync calls.824/// make this `noasync fn` and remove the individual noasync calls.
825pub fn openSelfDebugInfo(allocator: *mem.Allocator) !DebugInfo {825pub fn openSelfDebugInfo(allocator: *mem.Allocator) anyerror!DebugInfo {
826 if (builtin.strip_debug_info)826 if (builtin.strip_debug_info)
827 return error.MissingDebugInfo;827 return error.MissingDebugInfo;
828 if (@hasDecl(root, "os") and @hasDecl(root.os, "debug") and @hasDecl(root.os.debug, "openSelfDebugInfo")) {828 if (@hasDecl(root, "os") and @hasDecl(root.os, "debug") and @hasDecl(root.os.debug, "openSelfDebugInfo")) {
829 return noasync root.os.debug.openSelfDebugInfo(allocator);829 return noasync root.os.debug.openSelfDebugInfo(allocator);
830 }830 }
831 if (builtin.os == .linux or builtin.os == .windows or builtin.os == .macosx) {831 switch (builtin.os) {
832 _ = try allocator.create(u32);832 .linux,
833 return DebugInfo.init(allocator);833 .freebsd,
834 .macosx,
835 .windows,
836 => return DebugInfo.init(allocator),
837 else => @compileError("openSelfDebugInfo unsupported for this platform"),
834 }838 }
835 return noasync openSelfDebugInfoPosix(allocator);
836}
837
838fn openSelfDebugInfoWindows(allocator: *mem.Allocator) !ObjectDebugInfo {
839 const self_file = try fs.openSelfExe();
840 defer self_file.close();
841 return openCoffDebugInfo(allocator, self_file);
842}839}
843840
844fn openCoffDebugInfo(allocator: *mem.Allocator, self_file: fs.File) !ObjectDebugInfo {841fn openCoffDebugInfo(allocator: *mem.Allocator, self_file: fs.File) !ObjectDebugInfo {
...@@ -846,7 +843,7 @@ fn openCoffDebugInfo(allocator: *mem.Allocator, self_file: fs.File) !ObjectDebug...@@ -846,7 +843,7 @@ fn openCoffDebugInfo(allocator: *mem.Allocator, self_file: fs.File) !ObjectDebug
846 coff_obj.* = coff.Coff.init(allocator, self_file);843 coff_obj.* = coff.Coff.init(allocator, self_file);
847844
848 var di = ObjectDebugInfo{845 var di = ObjectDebugInfo{
849 .base_address = 0,846 .base_address = undefined,
850 .coff = coff_obj,847 .coff = coff_obj,
851 .pdb = undefined,848 .pdb = undefined,
852 .sect_contribs = undefined,849 .sect_contribs = undefined,
...@@ -1018,10 +1015,16 @@ fn findDwarfSectionFromElf(elf_file: *elf.Elf, name: []const u8) !?DW.DwarfInfo....@@ -1018,10 +1015,16 @@ fn findDwarfSectionFromElf(elf_file: *elf.Elf, name: []const u8) !?DW.DwarfInfo.
1018 };1015 };
1019}1016}
10201017
1018fn chopSlice(ptr: []const u8, offset: u64, size: u64) ![]const u8 {
1019 const start = try math.cast(usize, offset);
1020 const end = start + try math.cast(usize, size);
1021 return ptr[start..end];
1022}
1023
1021/// TODO resources https://github.com/ziglang/zig/issues/43531024/// TODO resources https://github.com/ziglang/zig/issues/4353
1022pub fn openElfDebugInfo(1025pub fn openElfDebugInfo(
1023 allocator: *mem.Allocator,1026 allocator: *mem.Allocator,
1024 data: []u8,1027 data: []const u8,
1025) !DW.DwarfInfo {1028) !DW.DwarfInfo {
1026 var seekable_stream = io.SliceSeekableInStream.init(data);1029 var seekable_stream = io.SliceSeekableInStream.init(data);
1027 var efile = try elf.Elf.openStream(1030 var efile = try elf.Elf.openStream(
...@@ -1043,12 +1046,12 @@ pub fn openElfDebugInfo(...@@ -1043,12 +1046,12 @@ pub fn openElfDebugInfo(
10431046
1044 var di = DW.DwarfInfo{1047 var di = DW.DwarfInfo{
1045 .endian = efile.endian,1048 .endian = efile.endian,
1046 .debug_info = (data[@intCast(usize, debug_info.sh_offset)..@intCast(usize, debug_info.sh_offset + debug_info.sh_size)]),1049 .debug_info = try chopSlice(data, debug_info.sh_offset, debug_info.sh_size),
1047 .debug_abbrev = (data[@intCast(usize, debug_abbrev.sh_offset)..@intCast(usize, debug_abbrev.sh_offset + debug_abbrev.sh_size)]),1050 .debug_abbrev = try chopSlice(data, debug_abbrev.sh_offset, debug_abbrev.sh_size),
1048 .debug_str = (data[@intCast(usize, debug_str.sh_offset)..@intCast(usize, debug_str.sh_offset + debug_str.sh_size)]),1051 .debug_str = try chopSlice(data, debug_str.sh_offset, debug_str.sh_size),
1049 .debug_line = (data[@intCast(usize, debug_line.sh_offset)..@intCast(usize, debug_line.sh_offset + debug_line.sh_size)]),1052 .debug_line = try chopSlice(data, debug_line.sh_offset, debug_line.sh_size),
1050 .debug_ranges = if (opt_debug_ranges) |debug_ranges|1053 .debug_ranges = if (opt_debug_ranges) |debug_ranges|
1051 data[@intCast(usize, debug_ranges.sh_offset)..@intCast(usize, debug_ranges.sh_offset + debug_ranges.sh_size)]1054 try chopSlice(data, debug_ranges.sh_offset, debug_ranges.sh_size)
1052 else1055 else
1053 null,1056 null,
1054 };1057 };
...@@ -1057,26 +1060,6 @@ pub fn openElfDebugInfo(...@@ -1057,26 +1060,6 @@ pub fn openElfDebugInfo(
1057 return di;1060 return di;
1058}1061}
10591062
1060/// TODO resources https://github.com/ziglang/zig/issues/4353
1061fn openSelfDebugInfoPosix(allocator: *mem.Allocator) !DW.DwarfInfo {
1062 var exe_file = try fs.openSelfExe();
1063 errdefer exe_file.close();
1064
1065 const exe_len = math.cast(usize, try exe_file.getEndPos()) catch
1066 return error.DebugInfoTooLarge;
1067 const exe_mmap = try os.mmap(
1068 null,
1069 exe_len,
1070 os.PROT_READ,
1071 os.MAP_SHARED,
1072 exe_file.handle,
1073 0,
1074 );
1075 errdefer os.munmap(exe_mmap);
1076
1077 return openElfDebugInfo(allocator, exe_mmap);
1078}
1079
1080/// TODO resources https://github.com/ziglang/zig/issues/43531063/// TODO resources https://github.com/ziglang/zig/issues/4353
1081fn openMachODebugInfo(allocator: *mem.Allocator, memmo: []const u8) !ObjectDebugInfo {1064fn openMachODebugInfo(allocator: *mem.Allocator, memmo: []const u8) !ObjectDebugInfo {
1082 // const hdr = &std.c._mh_execute_header;1065 // const hdr = &std.c._mh_execute_header;
...@@ -1220,7 +1203,7 @@ pub const DebugInfo = struct {...@@ -1220,7 +1203,7 @@ pub const DebugInfo = struct {
1220 }1203 }
12211204
1222 pub fn deinit(self: *DebugInfo) void {1205 pub fn deinit(self: *DebugInfo) void {
1223 // XXX Free the values1206 // TODO: resources https://github.com/ziglang/zig/issues/4353
1224 self.address_map.deinit();1207 self.address_map.deinit();
1225 }1208 }
12261209
...@@ -1450,8 +1433,6 @@ pub const DebugInfo = struct {...@@ -1450,8 +1433,6 @@ pub const DebugInfo = struct {
1450 }1433 }
1451};1434};
14521435
1453const DIPContext = struct {};
1454
1455pub const ObjectDebugInfo = switch (builtin.os) {1436pub const ObjectDebugInfo = switch (builtin.os) {
1456 .macosx, .ios, .watchos, .tvos => struct {1437 .macosx, .ios, .watchos, .tvos => struct {
1457 base_address: usize,1438 base_address: usize,
...@@ -1540,7 +1521,10 @@ fn getLineNumberInfoMacOs(di: *ObjectDebugInfo, symbol: MachoSymbol, address: us...@@ -1540,7 +1521,10 @@ fn getLineNumberInfoMacOs(di: *ObjectDebugInfo, symbol: MachoSymbol, address: us
1540 var opt_debug_str: ?*const macho.section_64 = null;1521 var opt_debug_str: ?*const macho.section_64 = null;
1541 var opt_debug_ranges: ?*const macho.section_64 = null;1522 var opt_debug_ranges: ?*const macho.section_64 = null;
15421523
1543 const sections = @ptrCast([*]const macho.section_64, @alignCast(@alignOf(macho.section_64), ptr + @sizeOf(std.macho.segment_command_64)))[0..segcmd.nsects];1524 const sections = @ptrCast(
1525 [*]const macho.section_64,
1526 @alignCast(@alignOf(macho.section_64), ptr + @sizeOf(std.macho.segment_command_64)),
1527 )[0..segcmd.nsects];
1544 for (sections) |*sect| {1528 for (sections) |*sect| {
1545 // The section name may not exceed 16 chars and a trailing null may1529 // The section name may not exceed 16 chars and a trailing null may
1546 // not be present1530 // not be present
...@@ -1573,12 +1557,12 @@ fn getLineNumberInfoMacOs(di: *ObjectDebugInfo, symbol: MachoSymbol, address: us...@@ -1573,12 +1557,12 @@ fn getLineNumberInfoMacOs(di: *ObjectDebugInfo, symbol: MachoSymbol, address: us
15731557
1574 gop.kv.value = DW.DwarfInfo{1558 gop.kv.value = DW.DwarfInfo{
1575 .endian = .Little,1559 .endian = .Little,
1576 .debug_info = exe_mmap[@intCast(usize, debug_info.offset)..@intCast(usize, debug_info.offset + debug_info.size)],1560 .debug_info = try chopSlice(exe_mmap, debug_info.offset, debug_info.size),
1577 .debug_abbrev = exe_mmap[@intCast(usize, debug_abbrev.offset)..@intCast(usize, debug_abbrev.offset + debug_abbrev.size)],1561 .debug_abbrev = try chopSlice(exe_mmap, debug_abbrev.offset, debug_abbrev.size),
1578 .debug_str = exe_mmap[@intCast(usize, debug_str.offset)..@intCast(usize, debug_str.offset + debug_str.size)],1562 .debug_str = try chopSlice(exe_mmap, debug_str.offset, debug_str.size),
1579 .debug_line = exe_mmap[@intCast(usize, debug_line.offset)..@intCast(usize, debug_line.offset + debug_line.size)],1563 .debug_line = try chopSlice(exe_mmap, debug_line.offset, debug_line.size),
1580 .debug_ranges = if (opt_debug_ranges) |debug_ranges|1564 .debug_ranges = if (opt_debug_ranges) |debug_ranges|
1581 exe_mmap[@intCast(usize, debug_ranges.offset)..@intCast(usize, debug_ranges.offset + debug_ranges.size)]1565 try chopSlice(exe_mmap, debug_ranges.offset, debug_ranges.size)
1582 else1566 else
1583 null,1567 null,
1584 };1568 };
lib/std/dwarf.zig+5-5
...@@ -387,11 +387,11 @@ fn getAbbrevTableEntry(abbrev_table: *const AbbrevTable, abbrev_code: u64) ?*con...@@ -387,11 +387,11 @@ fn getAbbrevTableEntry(abbrev_table: *const AbbrevTable, abbrev_code: u64) ?*con
387pub const DwarfInfo = struct {387pub const DwarfInfo = struct {
388 endian: builtin.Endian,388 endian: builtin.Endian,
389 // No memory is owned by the DwarfInfo389 // No memory is owned by the DwarfInfo
390 debug_info: []u8,390 debug_info: []const u8,
391 debug_abbrev: []u8,391 debug_abbrev: []const u8,
392 debug_str: []u8,392 debug_str: []const u8,
393 debug_line: []u8,393 debug_line: []const u8,
394 debug_ranges: ?[]u8,394 debug_ranges: ?[]const u8,
395 // Filled later by the initializer395 // Filled later by the initializer
396 abbrev_table_list: ArrayList(AbbrevTableHeader) = undefined,396 abbrev_table_list: ArrayList(AbbrevTableHeader) = undefined,
397 compile_unit_list: ArrayList(CompileUnit) = undefined,397 compile_unit_list: ArrayList(CompileUnit) = undefined,