authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-02-20 20:18:51+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-02-20 20:18:51+01:00
log342a2749487d9952cf2afd6f8fb3e9534d4a210a
tree84126141f2bcfe8acf7ad0545243f1d94dfc95ab
parentce2efbdca6f1bf8a1ac01c379cb90990a2ea4e78

tidy interface, const correctness


2 files changed, 65 insertions(+), 84 deletions(-)

lib/std/debug.zig+60-79
......@@ -838,9 +838,13 @@ pub fn openSelfDebugInfo(allocator: *mem.Allocator) anyerror!DebugInfo {
838838 }
839839}
840840
841fn openCoffDebugInfo(allocator: *mem.Allocator, self_file: fs.File) !ObjectDebugInfo {
841/// TODO resources https://github.com/ziglang/zig/issues/4353
842fn openCoffDebugInfo(allocator: *mem.Allocator, coff_file_path: [:0]const u16) !ObjectDebugInfo {
843 const coff_file = try std.fs.openFileAbsoluteW(coff_file_path.ptr, .{});
844 errdefer coff_file.close();
845
842846 const coff_obj = try allocator.create(coff.Coff);
843 coff_obj.* = coff.Coff.init(allocator, self_file);
847 coff_obj.* = coff.Coff.init(allocator, coff_file);
844848
845849 var di = ObjectDebugInfo{
846850 .base_address = undefined,
......@@ -1007,14 +1011,6 @@ fn readSparseBitVector(stream: var, allocator: *mem.Allocator) ![]usize {
10071011 return list.toOwnedSlice();
10081012}
10091013
1010fn findDwarfSectionFromElf(elf_file: *elf.Elf, name: []const u8) !?DW.DwarfInfo.Section {
1011 const elf_header = (try elf_file.findSection(name)) orelse return null;
1012 return DW.DwarfInfo.Section{
1013 .offset = elf_header.sh_offset,
1014 .size = elf_header.sh_size,
1015 };
1016}
1017
10181014fn chopSlice(ptr: []const u8, offset: u64, size: u64) ![]const u8 {
10191015 const start = try math.cast(usize, offset);
10201016 const end = start + try math.cast(usize, size);
......@@ -1022,11 +1018,10 @@ fn chopSlice(ptr: []const u8, offset: u64, size: u64) ![]const u8 {
10221018}
10231019
10241020/// TODO resources https://github.com/ziglang/zig/issues/4353
1025pub fn openElfDebugInfo(
1026 allocator: *mem.Allocator,
1027 data: []const u8,
1028) !DW.DwarfInfo {
1029 var seekable_stream = io.SliceSeekableInStream.init(data);
1021pub fn openElfDebugInfo(allocator: *mem.Allocator, elf_file_path: []const u8) !ObjectDebugInfo {
1022 const mapped_mem = mapWholeFile(elf_file_path) catch |_| return error.InvalidDebugInfo;
1023
1024 var seekable_stream = io.SliceSeekableInStream.init(mapped_mem);
10301025 var efile = try elf.Elf.openStream(
10311026 allocator,
10321027 @ptrCast(*DW.DwarfSeekableStream, &seekable_stream.seekable_stream),
......@@ -1046,26 +1041,32 @@ pub fn openElfDebugInfo(
10461041
10471042 var di = DW.DwarfInfo{
10481043 .endian = efile.endian,
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),
1044 .debug_info = try chopSlice(mapped_mem, debug_info.sh_offset, debug_info.sh_size),
1045 .debug_abbrev = try chopSlice(mapped_mem, debug_abbrev.sh_offset, debug_abbrev.sh_size),
1046 .debug_str = try chopSlice(mapped_mem, debug_str.sh_offset, debug_str.sh_size),
1047 .debug_line = try chopSlice(mapped_mem, debug_line.sh_offset, debug_line.sh_size),
10531048 .debug_ranges = if (opt_debug_ranges) |debug_ranges|
1054 try chopSlice(data, debug_ranges.sh_offset, debug_ranges.sh_size)
1049 try chopSlice(mapped_mem, debug_ranges.sh_offset, debug_ranges.sh_size)
10551050 else
10561051 null,
10571052 };
10581053
10591054 try DW.openDwarfDebugInfo(&di, allocator);
1060 return di;
1055
1056 return ObjectDebugInfo{
1057 .base_address = undefined,
1058 .dwarf = di,
1059 .mapped_memory = mapped_mem,
1060 };
10611061}
10621062
10631063/// TODO resources https://github.com/ziglang/zig/issues/4353
1064fn openMachODebugInfo(allocator: *mem.Allocator, memmo: []const u8) !ObjectDebugInfo {
1065 // const hdr = &std.c._mh_execute_header;
1064fn openMachODebugInfo(allocator: *mem.Allocator, macho_file_path: []const u8) !ObjectDebugInfo {
1065 const mapped_mem = mapWholeFile(macho_file_path) catch |_| return error.InvalidDebugInfo;
1066
10661067 const hdr = @ptrCast(
10671068 *const macho.mach_header_64,
1068 @alignCast(@alignOf(macho.mach_header_64), memmo.ptr),
1069 @alignCast(@alignOf(macho.mach_header_64), mapped_mem.ptr),
10691070 );
10701071 assert(hdr.magic == std.macho.MH_MAGIC_64);
10711072
......@@ -1137,7 +1138,7 @@ fn openMachODebugInfo(allocator: *mem.Allocator, memmo: []const u8) !ObjectDebug
11371138
11381139 return ObjectDebugInfo{
11391140 .base_address = undefined,
1140 .mapped_memory = undefined,
1141 .mapped_memory = mapped_mem,
11411142 .ofiles = ObjectDebugInfo.OFileTable.init(allocator),
11421143 .symbols = symbols,
11431144 .strings = strings,
......@@ -1191,6 +1192,24 @@ const MachoSymbol = struct {
11911192 }
11921193};
11931194
1195fn mapWholeFile(path: []const u8) ![]const u8 {
1196 const file = try fs.openFileAbsolute(path, .{});
1197 defer file.close();
1198
1199 const file_len = try math.cast(usize, try file.getEndPos());
1200 const mapped_mem = try os.mmap(
1201 null,
1202 file_len,
1203 os.PROT_READ,
1204 os.MAP_SHARED,
1205 file.handle,
1206 0,
1207 );
1208 errdefer os.munmap(mapped_mem);
1209
1210 return mapped_mem;
1211}
1212
11941213pub const DebugInfo = struct {
11951214 allocator: *mem.Allocator,
11961215 address_map: std.AutoHashMap(usize, *ObjectDebugInfo),
......@@ -1253,30 +1272,14 @@ pub const DebugInfo = struct {
12531272 return obj_di;
12541273 }
12551274
1256 const image_name = std.c._dyld_get_image_name(i);
1257 const exe_file = try fs.openFileAbsoluteC(image_name, .{});
1258 errdefer exe_file.close();
1259
1260 const exe_len = math.cast(usize, try exe_file.getEndPos()) catch
1261 return error.DebugInfoTooLarge;
1262 const exe_mmap = try os.mmap(
1263 null,
1264 exe_len,
1265 os.PROT_READ,
1266 os.MAP_SHARED,
1267 exe_file.handle,
1268 0,
1269 );
1270 errdefer os.munmap(exe_mmap);
1271
12721275 const obj_di = try self.allocator.create(ObjectDebugInfo);
12731276 errdefer self.allocator.destroy(obj_di);
12741277
12751278 try self.address_map.putNoClobber(base_address, obj_di);
12761279
1277 obj_di.* = try openMachODebugInfo(self.allocator, exe_mmap);
1280 const macho_path = mem.toSliceConst(u8, std.c._dyld_get_image_name(i));
1281 obj_di.* = try openMachODebugInfo(self.allocator, macho_path);
12781282 obj_di.base_address = base_address;
1279 obj_di.mapped_memory = exe_mmap;
12801283
12811284 return obj_di;
12821285 }
......@@ -1348,18 +1351,12 @@ pub const DebugInfo = struct {
13481351 );
13491352 assert(len > 0);
13501353
1351 // The compiler segfaults if the slicing is done as a parameter
1352 // (#4423)
1353 const tmp = name_buffer[0..:0];
1354 const file_obj = try fs.openFileAbsoluteW(tmp, .{});
1355 errdefer file_obj.close();
1356
13571354 const obj_di = try self.allocator.create(ObjectDebugInfo);
13581355 errdefer self.allocator.destroy(obj_di);
13591356
13601357 try self.address_map.putNoClobber(seg_start, obj_di);
13611358
1362 obj_di.* = try openCoffDebugInfo(self.allocator, file_obj);
1359 obj_di.* = try openCoffDebugInfo(self.allocator, name_buffer[0..:0]);
13631360 obj_di.base_address = seg_start;
13641361
13651362 return obj_di;
......@@ -1407,45 +1404,29 @@ pub const DebugInfo = struct {
14071404 }
14081405 }.callback)) {
14091406 return error.DebugInfoNotFound;
1410 } else |err| {
1411 switch (err) {
1412 error.Found => {},
1413 else => return error.DebugInfoNotFound,
1414 }
1407 } else |err| switch (err) {
1408 error.Found => {},
1409 else => return error.DebugInfoNotFound,
14151410 }
14161411
14171412 if (self.address_map.getValue(ctx.base_address)) |obj_di| {
14181413 return obj_di;
14191414 }
14201415
1421 const exe_file = if (ctx.name.len > 0)
1422 try fs.openFileAbsolute(ctx.name, .{})
1423 else
1424 try fs.openSelfExe();
1425 defer exe_file.close();
1426
1427 const exe_len = math.cast(usize, try exe_file.getEndPos()) catch
1428 return error.DebugInfoTooLarge;
1429 const exe_mmap = try os.mmap(
1430 null,
1431 exe_len,
1432 os.PROT_READ,
1433 os.MAP_SHARED,
1434 exe_file.handle,
1435 0,
1436 );
1437 errdefer os.munmap(exe_mmap);
1416 const elf_path = if (ctx.name.len > 0)
1417 ctx.name
1418 else blk: {
1419 var buf: [fs.MAX_PATH_BYTES]u8 = undefined;
1420 break :blk try fs.selfExePath(&buf);
1421 };
14381422
14391423 const obj_di = try self.allocator.create(ObjectDebugInfo);
14401424 errdefer self.allocator.destroy(obj_di);
14411425
14421426 try self.address_map.putNoClobber(ctx.base_address, obj_di);
14431427
1444 obj_di.* = .{
1445 .dwarf = try openElfDebugInfo(self.allocator, exe_mmap),
1446 .mapped_memory = exe_mmap,
1447 .base_address = ctx.base_address,
1448 };
1428 obj_di.* = try openElfDebugInfo(self.allocator, elf_path);
1429 obj_di.base_address = ctx.base_address;
14491430
14501431 return obj_di;
14511432 }
......@@ -1454,7 +1435,7 @@ pub const DebugInfo = struct {
14541435pub const ObjectDebugInfo = switch (builtin.os) {
14551436 .macosx, .ios, .watchos, .tvos => struct {
14561437 base_address: usize,
1457 mapped_memory: []u8,
1438 mapped_memory: []const u8,
14581439 symbols: []const MachoSymbol,
14591440 strings: []const u8,
14601441 ofiles: OFileTable,
......@@ -1480,7 +1461,7 @@ pub const ObjectDebugInfo = switch (builtin.os) {
14801461 .linux, .freebsd => struct {
14811462 base_address: usize,
14821463 dwarf: DW.DwarfInfo,
1483 mapped_memory: []u8,
1464 mapped_memory: []const u8,
14841465 },
14851466 else => DW.DwarfInfo,
14861467};
lib/std/dwarf.zig+5-5
......@@ -21,7 +21,7 @@ const PcRange = struct {
2121
2222const Func = struct {
2323 pc_range: ?PcRange,
24 name: ?[]u8,
24 name: ?[]const u8,
2525};
2626
2727const CompileUnit = struct {
......@@ -60,7 +60,7 @@ const FormValue = union(enum) {
6060 SecOffset: u64,
6161 Ref: u64,
6262 RefAddr: u64,
63 String: []u8,
63 String: []const u8,
6464 StrPtr: u64,
6565};
6666
......@@ -124,7 +124,7 @@ const Die = struct {
124124 };
125125 }
126126
127 fn getAttrString(self: *const Die, di: *DwarfInfo, id: u64) ![]u8 {
127 fn getAttrString(self: *const Die, di: *DwarfInfo, id: u64) ![]const u8 {
128128 const form_value = self.getAttr(id) orelse return error.MissingDebugInfo;
129129 return switch (form_value.*) {
130130 FormValue.String => |value| value,
......@@ -740,7 +740,7 @@ pub const DwarfInfo = struct {
740740 }
741741 }
742742
743 var include_directories = ArrayList([]u8).init(di.allocator());
743 var include_directories = ArrayList([]const u8).init(di.allocator());
744744 try include_directories.append(compile_unit_cwd);
745745 while (true) {
746746 const dir = try s.stream.readUntilDelimiterAlloc(di.allocator(), 0, math.maxInt(usize));
......@@ -861,7 +861,7 @@ pub const DwarfInfo = struct {
861861 return error.MissingDebugInfo;
862862 }
863863
864 fn getString(di: *DwarfInfo, offset: u64) ![]u8 {
864 fn getString(di: *DwarfInfo, offset: u64) ![]const u8 {
865865 if (offset > di.debug_str.len)
866866 return error.InvalidDebugInfo;
867867 const casted_offset = math.cast(usize, offset) catch