| ... | @@ -670,10 +670,11 @@ pub fn openSelfDebugInfo(allocator: *mem.Allocator) anyerror!DebugInfo { | ... | @@ -670,10 +670,11 @@ pub fn openSelfDebugInfo(allocator: *mem.Allocator) anyerror!DebugInfo { |
| 670 | } | 670 | } |
| 671 | } | 671 | } |
| 672 | | 672 | |
| | 673 | /// This takes ownership of coff_file: users of this function should not close |
| | 674 | /// it themselves, even on error. |
| 673 | /// TODO resources https://github.com/ziglang/zig/issues/4353 | 675 | /// TODO resources https://github.com/ziglang/zig/issues/4353 |
| 674 | fn openCoffDebugInfo(allocator: *mem.Allocator, coff_file_path: [:0]const u16) !ModuleDebugInfo { | 676 | fn readCoffDebugInfo(allocator: *mem.Allocator, coff_file: File) !ModuleDebugInfo { |
| 675 | nosuspend { | 677 | nosuspend { |
| 676 | const coff_file = try std.fs.openFileAbsoluteW(coff_file_path, .{ .intended_io_mode = .blocking }); | | |
| 677 | errdefer coff_file.close(); | 678 | errdefer coff_file.close(); |
| 678 | | 679 | |
| 679 | const coff_obj = try allocator.create(coff.Coff); | 680 | const coff_obj = try allocator.create(coff.Coff); |
| ... | @@ -851,10 +852,12 @@ fn chopSlice(ptr: []const u8, offset: u64, size: u64) ![]const u8 { | ... | @@ -851,10 +852,12 @@ fn chopSlice(ptr: []const u8, offset: u64, size: u64) ![]const u8 { |
| 851 | return ptr[start..end]; | 852 | return ptr[start..end]; |
| 852 | } | 853 | } |
| 853 | | 854 | |
| | 855 | /// This takes ownership of elf_file: users of this function should not close |
| | 856 | /// it themselves, even on error. |
| 854 | /// TODO resources https://github.com/ziglang/zig/issues/4353 | 857 | /// TODO resources https://github.com/ziglang/zig/issues/4353 |
| 855 | pub fn openElfDebugInfo(allocator: *mem.Allocator, elf_file_path: []const u8) !ModuleDebugInfo { | 858 | pub fn readElfDebugInfo(allocator: *mem.Allocator, elf_file: File) !ModuleDebugInfo { |
| 856 | nosuspend { | 859 | nosuspend { |
| 857 | const mapped_mem = try mapWholeFile(elf_file_path); | 860 | const mapped_mem = try mapWholeFile(elf_file); |
| 858 | const hdr = @ptrCast(*const elf.Ehdr, &mapped_mem[0]); | 861 | const hdr = @ptrCast(*const elf.Ehdr, &mapped_mem[0]); |
| 859 | if (!mem.eql(u8, hdr.e_ident[0..4], "\x7fELF")) return error.InvalidElfMagic; | 862 | if (!mem.eql(u8, hdr.e_ident[0..4], "\x7fELF")) return error.InvalidElfMagic; |
| 860 | if (hdr.e_ident[elf.EI_VERSION] != 1) return error.InvalidElfVersion; | 863 | if (hdr.e_ident[elf.EI_VERSION] != 1) return error.InvalidElfVersion; |
| ... | @@ -921,8 +924,10 @@ pub fn openElfDebugInfo(allocator: *mem.Allocator, elf_file_path: []const u8) !M | ... | @@ -921,8 +924,10 @@ pub fn openElfDebugInfo(allocator: *mem.Allocator, elf_file_path: []const u8) !M |
| 921 | } | 924 | } |
| 922 | | 925 | |
| 923 | /// TODO resources https://github.com/ziglang/zig/issues/4353 | 926 | /// TODO resources https://github.com/ziglang/zig/issues/4353 |
| 924 | fn openMachODebugInfo(allocator: *mem.Allocator, macho_file_path: []const u8) !ModuleDebugInfo { | 927 | /// This takes ownership of coff_file: users of this function should not close |
| 925 | const mapped_mem = try mapWholeFile(macho_file_path); | 928 | /// it themselves, even on error. |
| | 929 | fn readMachODebugInfo(allocator: *mem.Allocator, macho_file: File) !ModuleDebugInfo { |
| | 930 | const mapped_mem = try mapWholeFile(macho_file); |
| 926 | | 931 | |
| 927 | const hdr = @ptrCast( | 932 | const hdr = @ptrCast( |
| 928 | *const macho.mach_header_64, | 933 | *const macho.mach_header_64, |
| ... | @@ -1055,9 +1060,8 @@ const MachoSymbol = struct { | ... | @@ -1055,9 +1060,8 @@ const MachoSymbol = struct { |
| 1055 | } | 1060 | } |
| 1056 | }; | 1061 | }; |
| 1057 | | 1062 | |
| 1058 | fn mapWholeFile(path: []const u8) ![]align(mem.page_size) const u8 { | 1063 | fn mapWholeFile(file: File) ![]align(mem.page_size) const u8 { |
| 1059 | nosuspend { | 1064 | nosuspend { |
| 1060 | const file = try fs.cwd().openFile(path, .{ .intended_io_mode = .blocking }); | | |
| 1061 | defer file.close(); | 1065 | defer file.close(); |
| 1062 | | 1066 | |
| 1063 | const file_len = try math.cast(usize, try file.getEndPos()); | 1067 | const file_len = try math.cast(usize, try file.getEndPos()); |
| ... | @@ -1140,10 +1144,11 @@ pub const DebugInfo = struct { | ... | @@ -1140,10 +1144,11 @@ pub const DebugInfo = struct { |
| 1140 | errdefer self.allocator.destroy(obj_di); | 1144 | errdefer self.allocator.destroy(obj_di); |
| 1141 | | 1145 | |
| 1142 | const macho_path = mem.spanZ(std.c._dyld_get_image_name(i)); | 1146 | const macho_path = mem.spanZ(std.c._dyld_get_image_name(i)); |
| 1143 | obj_di.* = openMachODebugInfo(self.allocator, macho_path) catch |err| switch (err) { | 1147 | const macho_file = fs.cwd().openFile(macho_path, .{ .always_blocking = true }) catch |err| switch (err) { |
| 1144 | error.FileNotFound => return error.MissingDebugInfo, | 1148 | error.FileNotFound => return error.MissingDebugInfo, |
| 1145 | else => return err, | 1149 | else => return err, |
| 1146 | }; | 1150 | }; |
| | 1151 | obj_di.* = try readMachODebugInfo(self.allocator, macho_file); |
| 1147 | obj_di.base_address = base_address; | 1152 | obj_di.base_address = base_address; |
| 1148 | | 1153 | |
| 1149 | try self.address_map.putNoClobber(base_address, obj_di); | 1154 | try self.address_map.putNoClobber(base_address, obj_di); |
| ... | @@ -1221,10 +1226,11 @@ pub const DebugInfo = struct { | ... | @@ -1221,10 +1226,11 @@ pub const DebugInfo = struct { |
| 1221 | const obj_di = try self.allocator.create(ModuleDebugInfo); | 1226 | const obj_di = try self.allocator.create(ModuleDebugInfo); |
| 1222 | errdefer self.allocator.destroy(obj_di); | 1227 | errdefer self.allocator.destroy(obj_di); |
| 1223 | | 1228 | |
| 1224 | obj_di.* = openCoffDebugInfo(self.allocator, name_buffer[0 .. len + 4 :0]) catch |err| switch (err) { | 1229 | const coff_file = fs.openFileAbsoluteW(name_buffer[0 .. len + 4 :0], .{}) catch |err| switch (err) { |
| 1225 | error.FileNotFound => return error.MissingDebugInfo, | 1230 | error.FileNotFound => return error.MissingDebugInfo, |
| 1226 | else => return err, | 1231 | else => return err, |
| 1227 | }; | 1232 | }; |
| | 1233 | obj_di.* = try readCoffDebugInfo(self.allocator, coff_file); |
| 1228 | obj_di.base_address = seg_start; | 1234 | obj_di.base_address = seg_start; |
| 1229 | | 1235 | |
| 1230 | try self.address_map.putNoClobber(seg_start, obj_di); | 1236 | try self.address_map.putNoClobber(seg_start, obj_di); |
| ... | @@ -1280,23 +1286,18 @@ pub const DebugInfo = struct { | ... | @@ -1280,23 +1286,18 @@ pub const DebugInfo = struct { |
| 1280 | return obj_di; | 1286 | return obj_di; |
| 1281 | } | 1287 | } |
| 1282 | | 1288 | |
| 1283 | const elf_path = if (ctx.name.len > 0) | | |
| 1284 | ctx.name | | |
| 1285 | else blk: { | | |
| 1286 | // Use of MAX_PATH_BYTES here is valid as the resulting path is immediately | | |
| 1287 | // opened with no modification. TODO: Use openSelfExe instead to avoid path | | |
| 1288 | // length limitations | | |
| 1289 | var buf: [fs.MAX_PATH_BYTES]u8 = undefined; | | |
| 1290 | break :blk try fs.selfExePath(&buf); | | |
| 1291 | }; | | |
| 1292 | | | |
| 1293 | const obj_di = try self.allocator.create(ModuleDebugInfo); | 1289 | const obj_di = try self.allocator.create(ModuleDebugInfo); |
| 1294 | errdefer self.allocator.destroy(obj_di); | 1290 | errdefer self.allocator.destroy(obj_di); |
| 1295 | | 1291 | |
| 1296 | obj_di.* = openElfDebugInfo(self.allocator, elf_path) catch |err| switch (err) { | 1292 | const elf_file = (if (ctx.name.len > 0) |
| | 1293 | fs.cwd().openFile(ctx.name, .{ .always_blocking = true }) |
| | 1294 | else |
| | 1295 | fs.openSelfExe(.{ .always_blocking = true })) catch |err| switch (err) { |
| 1297 | error.FileNotFound => return error.MissingDebugInfo, | 1296 | error.FileNotFound => return error.MissingDebugInfo, |
| 1298 | else => return err, | 1297 | else => return err, |
| 1299 | }; | 1298 | }; |
| | 1299 | |
| | 1300 | obj_di.* = try readElfDebugInfo(self.allocator, elf_file); |
| 1300 | obj_di.base_address = ctx.base_address; | 1301 | obj_di.base_address = ctx.base_address; |
| 1301 | | 1302 | |
| 1302 | try self.address_map.putNoClobber(ctx.base_address, obj_di); | 1303 | try self.address_map.putNoClobber(ctx.base_address, obj_di); |
| ... | @@ -1332,7 +1333,8 @@ pub const ModuleDebugInfo = switch (builtin.os.tag) { | ... | @@ -1332,7 +1333,8 @@ pub const ModuleDebugInfo = switch (builtin.os.tag) { |
| 1332 | } | 1333 | } |
| 1333 | | 1334 | |
| 1334 | fn loadOFile(self: *@This(), o_file_path: []const u8) !DW.DwarfInfo { | 1335 | fn loadOFile(self: *@This(), o_file_path: []const u8) !DW.DwarfInfo { |
| 1335 | const mapped_mem = try mapWholeFile(o_file_path); | 1336 | const o_file = try fs.cwd().openFile(o_file_path, .{ .always_blocking = true }); |
| | 1337 | const mapped_mem = try mapWholeFile(o_file); |
| 1336 | | 1338 | |
| 1337 | const hdr = @ptrCast( | 1339 | const hdr = @ptrCast( |
| 1338 | *const macho.mach_header_64, | 1340 | *const macho.mach_header_64, |