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{
822822/// TODO resources https://github.com/ziglang/zig/issues/4353
823823/// TODO once https://github.com/ziglang/zig/issues/3157 is fully implemented,
824824/// 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 {
826826 if (builtin.strip_debug_info)
827827 return error.MissingDebugInfo;
828828 if (@hasDecl(root, "os") and @hasDecl(root.os, "debug") and @hasDecl(root.os.debug, "openSelfDebugInfo")) {
829829 return noasync root.os.debug.openSelfDebugInfo(allocator);
830830 }
831 if (builtin.os == .linux or builtin.os == .windows or builtin.os == .macosx) {
832 _ = try allocator.create(u32);
833 return DebugInfo.init(allocator);
831 switch (builtin.os) {
832 .linux,
833 .freebsd,
834 .macosx,
835 .windows,
836 => return DebugInfo.init(allocator),
837 else => @compileError("openSelfDebugInfo unsupported for this platform"),
834838 }
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);
842839}
843840
844841fn openCoffDebugInfo(allocator: *mem.Allocator, self_file: fs.File) !ObjectDebugInfo {
......@@ -846,7 +843,7 @@ fn openCoffDebugInfo(allocator: *mem.Allocator, self_file: fs.File) !ObjectDebug
846843 coff_obj.* = coff.Coff.init(allocator, self_file);
847844
848845 var di = ObjectDebugInfo{
849 .base_address = 0,
846 .base_address = undefined,
850847 .coff = coff_obj,
851848 .pdb = undefined,
852849 .sect_contribs = undefined,
......@@ -1018,10 +1015,16 @@ fn findDwarfSectionFromElf(elf_file: *elf.Elf, name: []const u8) !?DW.DwarfInfo.
10181015 };
10191016}
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
10211024/// TODO resources https://github.com/ziglang/zig/issues/4353
10221025pub fn openElfDebugInfo(
10231026 allocator: *mem.Allocator,
1024 data: []u8,
1027 data: []const u8,
10251028) !DW.DwarfInfo {
10261029 var seekable_stream = io.SliceSeekableInStream.init(data);
10271030 var efile = try elf.Elf.openStream(
......@@ -1043,12 +1046,12 @@ pub fn openElfDebugInfo(
10431046
10441047 var di = DW.DwarfInfo{
10451048 .endian = efile.endian,
1046 .debug_info = (data[@intCast(usize, debug_info.sh_offset)..@intCast(usize, 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)]),
1048 .debug_str = (data[@intCast(usize, debug_str.sh_offset)..@intCast(usize, 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)]),
1049 .debug_info = try chopSlice(data, debug_info.sh_offset, debug_info.sh_size),
1050 .debug_abbrev = try chopSlice(data, debug_abbrev.sh_offset, debug_abbrev.sh_size),
1051 .debug_str = try chopSlice(data, debug_str.sh_offset, debug_str.sh_size),
1052 .debug_line = try chopSlice(data, debug_line.sh_offset, debug_line.sh_size),
10501053 .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)
10521055 else
10531056 null,
10541057 };
......@@ -1057,26 +1060,6 @@ pub fn openElfDebugInfo(
10571060 return di;
10581061}
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
10801063/// TODO resources https://github.com/ziglang/zig/issues/4353
10811064fn openMachODebugInfo(allocator: *mem.Allocator, memmo: []const u8) !ObjectDebugInfo {
10821065 // const hdr = &std.c._mh_execute_header;
......@@ -1220,7 +1203,7 @@ pub const DebugInfo = struct {
12201203 }
12211204
12221205 pub fn deinit(self: *DebugInfo) void {
1223 // XXX Free the values
1206 // TODO: resources https://github.com/ziglang/zig/issues/4353
12241207 self.address_map.deinit();
12251208 }
12261209
......@@ -1450,8 +1433,6 @@ pub const DebugInfo = struct {
14501433 }
14511434};
14521435
1453const DIPContext = struct {};
1454
14551436pub const ObjectDebugInfo = switch (builtin.os) {
14561437 .macosx, .ios, .watchos, .tvos => struct {
14571438 base_address: usize,
......@@ -1540,7 +1521,10 @@ fn getLineNumberInfoMacOs(di: *ObjectDebugInfo, symbol: MachoSymbol, address: us
15401521 var opt_debug_str: ?*const macho.section_64 = null;
15411522 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];
15441528 for (sections) |*sect| {
15451529 // The section name may not exceed 16 chars and a trailing null may
15461530 // not be present
......@@ -1573,12 +1557,12 @@ fn getLineNumberInfoMacOs(di: *ObjectDebugInfo, symbol: MachoSymbol, address: us
15731557
15741558 gop.kv.value = DW.DwarfInfo{
15751559 .endian = .Little,
1576 .debug_info = exe_mmap[@intCast(usize, debug_info.offset)..@intCast(usize, debug_info.offset + debug_info.size)],
1577 .debug_abbrev = exe_mmap[@intCast(usize, debug_abbrev.offset)..@intCast(usize, debug_abbrev.offset + debug_abbrev.size)],
1578 .debug_str = exe_mmap[@intCast(usize, debug_str.offset)..@intCast(usize, debug_str.offset + debug_str.size)],
1579 .debug_line = exe_mmap[@intCast(usize, debug_line.offset)..@intCast(usize, debug_line.offset + debug_line.size)],
1560 .debug_info = try chopSlice(exe_mmap, debug_info.offset, debug_info.size),
1561 .debug_abbrev = try chopSlice(exe_mmap, debug_abbrev.offset, debug_abbrev.size),
1562 .debug_str = try chopSlice(exe_mmap, debug_str.offset, debug_str.size),
1563 .debug_line = try chopSlice(exe_mmap, debug_line.offset, debug_line.size),
15801564 .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)
15821566 else
15831567 null,
15841568 };
lib/std/dwarf.zig+5-5
......@@ -387,11 +387,11 @@ fn getAbbrevTableEntry(abbrev_table: *const AbbrevTable, abbrev_code: u64) ?*con
387387pub const DwarfInfo = struct {
388388 endian: builtin.Endian,
389389 // No memory is owned by the DwarfInfo
390 debug_info: []u8,
391 debug_abbrev: []u8,
392 debug_str: []u8,
393 debug_line: []u8,
394 debug_ranges: ?[]u8,
390 debug_info: []const u8,
391 debug_abbrev: []const u8,
392 debug_str: []const u8,
393 debug_line: []const u8,
394 debug_ranges: ?[]const u8,
395395 // Filled later by the initializer
396396 abbrev_table_list: ArrayList(AbbrevTableHeader) = undefined,
397397 compile_unit_list: ArrayList(CompileUnit) = undefined,