authorgravatar for gereeter+code@gmail.comJonathan S <gereeter+code@gmail.com> 2020-03-27 15:13:26-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-29 18:27:39-04:00
logab3931fa9581776ef3654b9b55ff447ad91e7949
tree33660c4cbb8a6f8a35dbd3f08f08da0a537359bd
parent631633b25253201968b97cf129d986def37eed4a

Prefer Files to paths in std.debug. Additionally [breaking] add a flags parameter to openSelfExe and stop exporting openElfDebugInfo.

This should save a call to readlink in openSelfDebugInfo and support executables in overlong paths on Linux.

3 files changed, 29 insertions(+), 27 deletions(-)

lib/std/debug.zig+24-22
......@@ -670,10 +670,11 @@ pub fn openSelfDebugInfo(allocator: *mem.Allocator) anyerror!DebugInfo {
670670 }
671671}
672672
673/// This takes ownership of coff_file: users of this function should not close
674/// it themselves, even on error.
673675/// TODO resources https://github.com/ziglang/zig/issues/4353
674fn openCoffDebugInfo(allocator: *mem.Allocator, coff_file_path: [:0]const u16) !ModuleDebugInfo {
676fn readCoffDebugInfo(allocator: *mem.Allocator, coff_file: File) !ModuleDebugInfo {
675677 nosuspend {
676 const coff_file = try std.fs.openFileAbsoluteW(coff_file_path, .{ .intended_io_mode = .blocking });
677678 errdefer coff_file.close();
678679
679680 const coff_obj = try allocator.create(coff.Coff);
......@@ -851,10 +852,12 @@ fn chopSlice(ptr: []const u8, offset: u64, size: u64) ![]const u8 {
851852 return ptr[start..end];
852853}
853854
855/// This takes ownership of elf_file: users of this function should not close
856/// it themselves, even on error.
854857/// TODO resources https://github.com/ziglang/zig/issues/4353
855pub fn openElfDebugInfo(allocator: *mem.Allocator, elf_file_path: []const u8) !ModuleDebugInfo {
858pub fn readElfDebugInfo(allocator: *mem.Allocator, elf_file: File) !ModuleDebugInfo {
856859 nosuspend {
857 const mapped_mem = try mapWholeFile(elf_file_path);
860 const mapped_mem = try mapWholeFile(elf_file);
858861 const hdr = @ptrCast(*const elf.Ehdr, &mapped_mem[0]);
859862 if (!mem.eql(u8, hdr.e_ident[0..4], "\x7fELF")) return error.InvalidElfMagic;
860863 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
921924}
922925
923926/// TODO resources https://github.com/ziglang/zig/issues/4353
924fn openMachODebugInfo(allocator: *mem.Allocator, macho_file_path: []const u8) !ModuleDebugInfo {
925 const mapped_mem = try mapWholeFile(macho_file_path);
927/// This takes ownership of coff_file: users of this function should not close
928/// it themselves, even on error.
929fn readMachODebugInfo(allocator: *mem.Allocator, macho_file: File) !ModuleDebugInfo {
930 const mapped_mem = try mapWholeFile(macho_file);
926931
927932 const hdr = @ptrCast(
928933 *const macho.mach_header_64,
......@@ -1055,9 +1060,8 @@ const MachoSymbol = struct {
10551060 }
10561061};
10571062
1058fn mapWholeFile(path: []const u8) ![]align(mem.page_size) const u8 {
1063fn mapWholeFile(file: File) ![]align(mem.page_size) const u8 {
10591064 nosuspend {
1060 const file = try fs.cwd().openFile(path, .{ .intended_io_mode = .blocking });
10611065 defer file.close();
10621066
10631067 const file_len = try math.cast(usize, try file.getEndPos());
......@@ -1140,10 +1144,11 @@ pub const DebugInfo = struct {
11401144 errdefer self.allocator.destroy(obj_di);
11411145
11421146 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) {
11441148 error.FileNotFound => return error.MissingDebugInfo,
11451149 else => return err,
11461150 };
1151 obj_di.* = try readMachODebugInfo(self.allocator, macho_file);
11471152 obj_di.base_address = base_address;
11481153
11491154 try self.address_map.putNoClobber(base_address, obj_di);
......@@ -1221,10 +1226,11 @@ pub const DebugInfo = struct {
12211226 const obj_di = try self.allocator.create(ModuleDebugInfo);
12221227 errdefer self.allocator.destroy(obj_di);
12231228
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) {
12251230 error.FileNotFound => return error.MissingDebugInfo,
12261231 else => return err,
12271232 };
1233 obj_di.* = try readCoffDebugInfo(self.allocator, coff_file);
12281234 obj_di.base_address = seg_start;
12291235
12301236 try self.address_map.putNoClobber(seg_start, obj_di);
......@@ -1280,23 +1286,18 @@ pub const DebugInfo = struct {
12801286 return obj_di;
12811287 }
12821288
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
12931289 const obj_di = try self.allocator.create(ModuleDebugInfo);
12941290 errdefer self.allocator.destroy(obj_di);
12951291
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) {
12971296 error.FileNotFound => return error.MissingDebugInfo,
12981297 else => return err,
12991298 };
1299
1300 obj_di.* = try readElfDebugInfo(self.allocator, elf_file);
13001301 obj_di.base_address = ctx.base_address;
13011302
13021303 try self.address_map.putNoClobber(ctx.base_address, obj_di);
......@@ -1332,7 +1333,8 @@ pub const ModuleDebugInfo = switch (builtin.os.tag) {
13321333 }
13331334
13341335 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);
13361338
13371339 const hdr = @ptrCast(
13381340 *const macho.mach_header_64,
lib/std/fs.zig+4-4
......@@ -1778,21 +1778,21 @@ pub fn walkPath(allocator: *Allocator, dir_path: []const u8) !Walker {
17781778
17791779pub const OpenSelfExeError = os.OpenError || os.windows.CreateFileError || SelfExePathError || os.FlockError;
17801780
1781pub fn openSelfExe() OpenSelfExeError!File {
1781pub fn openSelfExe(flags: File.OpenFlags) OpenSelfExeError!File {
17821782 if (builtin.os.tag == .linux) {
1783 return openFileAbsoluteZ("/proc/self/exe", .{});
1783 return openFileAbsoluteZ("/proc/self/exe", flags);
17841784 }
17851785 if (builtin.os.tag == .windows) {
17861786 const wide_slice = selfExePathW();
17871787 const prefixed_path_w = try os.windows.wToPrefixedFileW(wide_slice);
1788 return cwd().openFileW(prefixed_path_w.span(), .{});
1788 return cwd().openFileW(prefix_path_w.span(), flags);
17891789 }
17901790 // Use of MAX_PATH_BYTES here is valid as the resulting path is immediately
17911791 // opened with no modification.
17921792 var buf: [MAX_PATH_BYTES]u8 = undefined;
17931793 const self_exe_path = try selfExePath(&buf);
17941794 buf[self_exe_path.len] = 0;
1795 return openFileAbsoluteZ(buf[0..self_exe_path.len :0].ptr, .{});
1795 return openFileAbsoluteZ(self_exe_path[0..self_exe_path.len :0].ptr, flags);
17961796}
17971797
17981798pub const SelfExePathError = os.ReadLinkError || os.SysCtlError;
lib/std/fs/test.zig+1-1
......@@ -6,7 +6,7 @@ const File = std.fs.File;
66test "openSelfExe" {
77 if (builtin.os.tag == .wasi) return error.SkipZigTest;
88
9 const self_exe_file = try std.fs.openSelfExe();
9 const self_exe_file = try std.fs.openSelfExe(.{});
1010 self_exe_file.close();
1111}
1212