authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-29 16:39:47-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-29 18:30:09-04:00
log89a97a7a278e69a440fd35665d4f1af864d023d9
tree364c995c839ac3521522c2f9ca83368a162d8c66
parentc209da1589f50dee9e961f64cb389d0b4e485aba

cleanups


3 files changed, 15 insertions(+), 9 deletions(-)

lib/std/debug.zig+10-4
...@@ -673,6 +673,7 @@ pub fn openSelfDebugInfo(allocator: *mem.Allocator) anyerror!DebugInfo {...@@ -673,6 +673,7 @@ pub fn openSelfDebugInfo(allocator: *mem.Allocator) anyerror!DebugInfo {
673/// This takes ownership of coff_file: users of this function should not close673/// This takes ownership of coff_file: users of this function should not close
674/// it themselves, even on error.674/// it themselves, even on error.
675/// TODO resources https://github.com/ziglang/zig/issues/4353675/// TODO resources https://github.com/ziglang/zig/issues/4353
676/// TODO it's weird to take ownership even on error, rework this code.
676fn readCoffDebugInfo(allocator: *mem.Allocator, coff_file: File) !ModuleDebugInfo {677fn readCoffDebugInfo(allocator: *mem.Allocator, coff_file: File) !ModuleDebugInfo {
677 nosuspend {678 nosuspend {
678 errdefer coff_file.close();679 errdefer coff_file.close();
...@@ -855,6 +856,7 @@ fn chopSlice(ptr: []const u8, offset: u64, size: u64) ![]const u8 {...@@ -855,6 +856,7 @@ fn chopSlice(ptr: []const u8, offset: u64, size: u64) ![]const u8 {
855/// This takes ownership of elf_file: users of this function should not close856/// This takes ownership of elf_file: users of this function should not close
856/// it themselves, even on error.857/// it themselves, even on error.
857/// TODO resources https://github.com/ziglang/zig/issues/4353858/// TODO resources https://github.com/ziglang/zig/issues/4353
859/// TODO it's weird to take ownership even on error, rework this code.
858pub fn readElfDebugInfo(allocator: *mem.Allocator, elf_file: File) !ModuleDebugInfo {860pub fn readElfDebugInfo(allocator: *mem.Allocator, elf_file: File) !ModuleDebugInfo {
859 nosuspend {861 nosuspend {
860 const mapped_mem = try mapWholeFile(elf_file);862 const mapped_mem = try mapWholeFile(elf_file);
...@@ -926,6 +928,7 @@ pub fn readElfDebugInfo(allocator: *mem.Allocator, elf_file: File) !ModuleDebugI...@@ -926,6 +928,7 @@ pub fn readElfDebugInfo(allocator: *mem.Allocator, elf_file: File) !ModuleDebugI
926/// TODO resources https://github.com/ziglang/zig/issues/4353928/// TODO resources https://github.com/ziglang/zig/issues/4353
927/// This takes ownership of coff_file: users of this function should not close929/// This takes ownership of coff_file: users of this function should not close
928/// it themselves, even on error.930/// it themselves, even on error.
931/// TODO it's weird to take ownership even on error, rework this code.
929fn readMachODebugInfo(allocator: *mem.Allocator, macho_file: File) !ModuleDebugInfo {932fn readMachODebugInfo(allocator: *mem.Allocator, macho_file: File) !ModuleDebugInfo {
930 const mapped_mem = try mapWholeFile(macho_file);933 const mapped_mem = try mapWholeFile(macho_file);
931934
...@@ -1060,6 +1063,9 @@ const MachoSymbol = struct {...@@ -1060,6 +1063,9 @@ const MachoSymbol = struct {
1060 }1063 }
1061};1064};
10621065
1066/// `file` is expected to have been opened with .intended_io_mode == .blocking.
1067/// Takes ownership of file, even on error.
1068/// TODO it's weird to take ownership even on error, rework this code.
1063fn mapWholeFile(file: File) ![]align(mem.page_size) const u8 {1069fn mapWholeFile(file: File) ![]align(mem.page_size) const u8 {
1064 nosuspend {1070 nosuspend {
1065 defer file.close();1071 defer file.close();
...@@ -1144,7 +1150,7 @@ pub const DebugInfo = struct {...@@ -1144,7 +1150,7 @@ pub const DebugInfo = struct {
1144 errdefer self.allocator.destroy(obj_di);1150 errdefer self.allocator.destroy(obj_di);
11451151
1146 const macho_path = mem.spanZ(std.c._dyld_get_image_name(i));1152 const macho_path = mem.spanZ(std.c._dyld_get_image_name(i));
1147 const macho_file = fs.cwd().openFile(macho_path, .{ .always_blocking = true }) catch |err| switch (err) {1153 const macho_file = fs.cwd().openFile(macho_path, .{ .intended_io_mode = .blocking }) catch |err| switch (err) {
1148 error.FileNotFound => return error.MissingDebugInfo,1154 error.FileNotFound => return error.MissingDebugInfo,
1149 else => return err,1155 else => return err,
1150 };1156 };
...@@ -1290,9 +1296,9 @@ pub const DebugInfo = struct {...@@ -1290,9 +1296,9 @@ pub const DebugInfo = struct {
1290 errdefer self.allocator.destroy(obj_di);1296 errdefer self.allocator.destroy(obj_di);
12911297
1292 const elf_file = (if (ctx.name.len > 0)1298 const elf_file = (if (ctx.name.len > 0)
1293 fs.cwd().openFile(ctx.name, .{ .always_blocking = true })1299 fs.cwd().openFile(ctx.name, .{ .intended_io_mode = .blocking })
1294 else1300 else
1295 fs.openSelfExe(.{ .always_blocking = true })) catch |err| switch (err) {1301 fs.openSelfExe(.{ .intended_io_mode = .blocking })) catch |err| switch (err) {
1296 error.FileNotFound => return error.MissingDebugInfo,1302 error.FileNotFound => return error.MissingDebugInfo,
1297 else => return err,1303 else => return err,
1298 };1304 };
...@@ -1333,7 +1339,7 @@ pub const ModuleDebugInfo = switch (builtin.os.tag) {...@@ -1333,7 +1339,7 @@ pub const ModuleDebugInfo = switch (builtin.os.tag) {
1333 }1339 }
13341340
1335 fn loadOFile(self: *@This(), o_file_path: []const u8) !DW.DwarfInfo {1341 fn loadOFile(self: *@This(), o_file_path: []const u8) !DW.DwarfInfo {
1336 const o_file = try fs.cwd().openFile(o_file_path, .{ .always_blocking = true });1342 const o_file = try fs.cwd().openFile(o_file_path, .{ .intended_io_mode = .blocking });
1337 const mapped_mem = try mapWholeFile(o_file);1343 const mapped_mem = try mapWholeFile(o_file);
13381344
1339 const hdr = @ptrCast(1345 const hdr = @ptrCast(
lib/std/fs.zig+3-3
...@@ -1323,7 +1323,7 @@ pub const Dir = struct {...@@ -1323,7 +1323,7 @@ pub const Dir = struct {
1323 var cleanup_dir = true;1323 var cleanup_dir = true;
1324 defer if (cleanup_dir) dir.close();1324 defer if (cleanup_dir) dir.close();
13251325
1326 // Likely valid use of MAX_PATH_BYTES, as dir_name_buf will only1326 // Valid use of MAX_PATH_BYTES because dir_name_buf will only
1327 // ever store a single path component that was returned from the1327 // ever store a single path component that was returned from the
1328 // filesystem.1328 // filesystem.
1329 var dir_name_buf: [MAX_PATH_BYTES]u8 = undefined;1329 var dir_name_buf: [MAX_PATH_BYTES]u8 = undefined;
...@@ -1785,14 +1785,14 @@ pub fn openSelfExe(flags: File.OpenFlags) OpenSelfExeError!File {...@@ -1785,14 +1785,14 @@ pub fn openSelfExe(flags: File.OpenFlags) OpenSelfExeError!File {
1785 if (builtin.os.tag == .windows) {1785 if (builtin.os.tag == .windows) {
1786 const wide_slice = selfExePathW();1786 const wide_slice = selfExePathW();
1787 const prefixed_path_w = try os.windows.wToPrefixedFileW(wide_slice);1787 const prefixed_path_w = try os.windows.wToPrefixedFileW(wide_slice);
1788 return cwd().openFileW(prefix_path_w.span(), flags);1788 return cwd().openFileW(prefixed_path_w.span(), flags);
1789 }1789 }
1790 // Use of MAX_PATH_BYTES here is valid as the resulting path is immediately1790 // Use of MAX_PATH_BYTES here is valid as the resulting path is immediately
1791 // opened with no modification.1791 // opened with no modification.
1792 var buf: [MAX_PATH_BYTES]u8 = undefined;1792 var buf: [MAX_PATH_BYTES]u8 = undefined;
1793 const self_exe_path = try selfExePath(&buf);1793 const self_exe_path = try selfExePath(&buf);
1794 buf[self_exe_path.len] = 0;1794 buf[self_exe_path.len] = 0;
1795 return openFileAbsoluteZ(self_exe_path[0..self_exe_path.len :0].ptr, flags);1795 return openFileAbsoluteZ(buf[0..self_exe_path.len :0].ptr, flags);
1796}1796}
17971797
1798pub const SelfExePathError = os.ReadLinkError || os.SysCtlError;1798pub const SelfExePathError = os.ReadLinkError || os.SysCtlError;
lib/std/process.zig+2-2
...@@ -31,7 +31,7 @@ pub fn getCwdAlloc(allocator: *Allocator) ![]u8 {...@@ -31,7 +31,7 @@ pub fn getCwdAlloc(allocator: *Allocator) ![]u8 {
31 while (true) {31 while (true) {
32 if (os.getcwd(current_buf)) |slice| {32 if (os.getcwd(current_buf)) |slice| {
33 return mem.dupe(allocator, u8, slice);33 return mem.dupe(allocator, u8, slice);
34 } else |err| switch(err) {34 } else |err| switch (err) {
35 error.NameTooLong => {35 error.NameTooLong => {
36 // The path is too long to fit in stack_buf. Allocate geometrically36 // The path is too long to fit in stack_buf. Allocate geometrically
37 // increasing buffers until we find one that works37 // increasing buffers until we find one that works
...@@ -40,7 +40,7 @@ pub fn getCwdAlloc(allocator: *Allocator) ![]u8 {...@@ -40,7 +40,7 @@ pub fn getCwdAlloc(allocator: *Allocator) ![]u8 {
40 current_buf = try allocator.alloc(u8, new_capacity);40 current_buf = try allocator.alloc(u8, new_capacity);
41 heap_buf = current_buf;41 heap_buf = current_buf;
42 },42 },
43 else => return err,43 else => |e| return e,
44 }44 }
45 }45 }
46}46}