| ... | @@ -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/4353 | 822 | /// 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. |
| 825 | pub fn openSelfDebugInfo(allocator: *mem.Allocator) !DebugInfo { | 825 | pub 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 | | | |
| 838 | fn 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 | } |
| 843 | | 840 | |
| 844 | fn openCoffDebugInfo(allocator: *mem.Allocator, self_file: fs.File) !ObjectDebugInfo { | 841 | fn 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); |
| 847 | | 844 | |
| 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 | } |
| 1020 | | 1017 | |
| | 1018 | fn 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/4353 | 1024 | /// TODO resources https://github.com/ziglang/zig/issues/4353 |
| 1022 | pub fn openElfDebugInfo( | 1025 | pub 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( |
| 1043 | | 1046 | |
| 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 | else | 1055 | 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 | } |
| 1059 | | 1062 | |
| 1060 | /// TODO resources https://github.com/ziglang/zig/issues/4353 | | |
| 1061 | fn 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/4353 | 1063 | /// TODO resources https://github.com/ziglang/zig/issues/4353 |
| 1081 | fn openMachODebugInfo(allocator: *mem.Allocator, memmo: []const u8) !ObjectDebugInfo { | 1064 | fn 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 | } |
| 1221 | | 1204 | |
| 1222 | pub fn deinit(self: *DebugInfo) void { | 1205 | pub fn deinit(self: *DebugInfo) void { |
| 1223 | // XXX Free the values | 1206 | // TODO: resources https://github.com/ziglang/zig/issues/4353 |
| 1224 | self.address_map.deinit(); | 1207 | self.address_map.deinit(); |
| 1225 | } | 1208 | } |
| 1226 | | 1209 | |
| ... | @@ -1450,8 +1433,6 @@ pub const DebugInfo = struct { | ... | @@ -1450,8 +1433,6 @@ pub const DebugInfo = struct { |
| 1450 | } | 1433 | } |
| 1451 | }; | 1434 | }; |
| 1452 | | 1435 | |
| 1453 | const DIPContext = struct {}; | | |
| 1454 | | | |
| 1455 | pub const ObjectDebugInfo = switch (builtin.os) { | 1436 | pub 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; |
| 1542 | | 1523 | |
| 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 may | 1529 | // The section name may not exceed 16 chars and a trailing null may |
| 1546 | // not be present | 1530 | // 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 |
| 1573 | | 1557 | |
| 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 | else | 1566 | else |
| 1583 | null, | 1567 | null, |
| 1584 | }; | 1568 | }; |