authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-24 11:30:36-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-24 11:30:36-04:00
log95e197667e52d0e9b772b00fb2118414eead05aa
tree7317b4aee9feff3ad8f1b74e8f87b2446fa32e3b
parent3173c90f14c2729e129c409d2191fa2e28d2eba6

macos stack traces have the compilation unit in them


1 files changed, 22 insertions(+), 10 deletions(-)

std/debug/index.zig+22-10
...@@ -253,11 +253,11 @@ fn machoSearchSymbols(symbols: []const MachoSymbol, address: usize) ?*const Mach...@@ -253,11 +253,11 @@ fn machoSearchSymbols(symbols: []const MachoSymbol, address: usize) ?*const Mach
253 return null;253 return null;
254}254}
255255
256fn printSourceAtAddressMacOs(debug_info: *DebugInfo, out_stream: var, address: usize, tty_color: bool) !void {256fn printSourceAtAddressMacOs(di: *DebugInfo, out_stream: var, address: usize, tty_color: bool) !void {
257 const base_addr = @ptrToInt(&std.c._mh_execute_header);257 const base_addr = @ptrToInt(&std.c._mh_execute_header);
258 const adjusted_addr = 0x100000000 + (address - base_addr);258 const adjusted_addr = 0x100000000 + (address - base_addr);
259259
260 const symbol = machoSearchSymbols(debug_info.symbols, adjusted_addr) orelse {260 const symbol = machoSearchSymbols(di.symbols, adjusted_addr) orelse {
261 if (tty_color) {261 if (tty_color) {
262 try out_stream.print("???:?:?: " ++ DIM ++ "0x{x} in ??? (???)" ++ RESET ++ "\n\n\n", address);262 try out_stream.print("???:?:?: " ++ DIM ++ "0x{x} in ??? (???)" ++ RESET ++ "\n\n\n", address);
263 } else {263 } else {
...@@ -266,16 +266,19 @@ fn printSourceAtAddressMacOs(debug_info: *DebugInfo, out_stream: var, address: u...@@ -266,16 +266,19 @@ fn printSourceAtAddressMacOs(debug_info: *DebugInfo, out_stream: var, address: u
266 return;266 return;
267 };267 };
268268
269 const symbol_name = mem.toSliceConst(u8, debug_info.strings.ptr + symbol.nlist.n_strx);269 const symbol_name = mem.toSliceConst(u8, di.strings.ptr + symbol.nlist.n_strx);
270 if (getLineNumberInfoMacOs(debug_info, symbol.*, address)) |line_info| {270 const compile_unit_name = if (symbol.ofile) |ofile| blk: {
271 const compile_unit_name = "???";271 const ofile_path = mem.toSliceConst(u8, di.strings.ptr + ofile.n_strx);
272 try printLineInfo(debug_info, out_stream, line_info, address, symbol_name, compile_unit_name, tty_color);272 break :blk os.path.basename(ofile_path);
273 } else "???";
274 if (getLineNumberInfoMacOs(di, symbol.*, address)) |line_info| {
275 try printLineInfo(di, out_stream, line_info, address, symbol_name, compile_unit_name, tty_color);
273 } else |err| switch (err) {276 } else |err| switch (err) {
274 error.MissingDebugInfo => {277 error.MissingDebugInfo => {
275 if (tty_color) {278 if (tty_color) {
276 try out_stream.print("???:?:?: " ++ DIM ++ "0x{x} in {} (???)" ++ RESET ++ "\n\n\n", address, symbol_name);279 try out_stream.print("???:?:?: " ++ DIM ++ "0x{x} in {} ({})" ++ RESET ++ "\n\n\n", address, symbol_name, compile_unit_name);
277 } else {280 } else {
278 try out_stream.print("???:?:?: 0x{x} in {} (???)\n\n\n", address, symbol_name);281 try out_stream.print("???:?:?: 0x{x} in {} ({})\n\n\n", address, symbol_name, compile_unit_name);
279 }282 }
280 },283 },
281 else => return err,284 else => return err,
...@@ -516,6 +519,8 @@ const MachoSymbol = struct {...@@ -516,6 +519,8 @@ const MachoSymbol = struct {
516519
517const MachOFile = struct {520const MachOFile = struct {
518 bytes: []align(@alignOf(std.c.mach_header_64)) const u8,521 bytes: []align(@alignOf(std.c.mach_header_64)) const u8,
522 sect_debug_info: ?*const std.c.section_64,
523 sect_debug_line: ?*const std.c.section_64,
519};524};
520525
521pub const DebugInfo = switch (builtin.os) {526pub const DebugInfo = switch (builtin.os) {
...@@ -964,6 +969,8 @@ fn getLineNumberInfoMacOs(di: *DebugInfo, symbol: MachoSymbol, target_address: u...@@ -964,6 +969,8 @@ fn getLineNumberInfoMacOs(di: *DebugInfo, symbol: MachoSymbol, target_address: u
964969
965 gop.kv.value = MachOFile{970 gop.kv.value = MachOFile{
966 .bytes = try std.io.readFileAllocAligned(di.ofiles.allocator, ofile_path, @alignOf(std.c.mach_header_64)),971 .bytes = try std.io.readFileAllocAligned(di.ofiles.allocator, ofile_path, @alignOf(std.c.mach_header_64)),
972 .sect_debug_info = null,
973 .sect_debug_line = null,
967 };974 };
968 const hdr = @ptrCast(*const std.c.mach_header_64, gop.kv.value.bytes.ptr);975 const hdr = @ptrCast(*const std.c.mach_header_64, gop.kv.value.bytes.ptr);
969 assert(hdr.magic == std.c.MH_MAGIC_64);976 assert(hdr.magic == std.c.MH_MAGIC_64);
...@@ -981,11 +988,16 @@ fn getLineNumberInfoMacOs(di: *DebugInfo, symbol: MachoSymbol, target_address: u...@@ -981,11 +988,16 @@ fn getLineNumberInfoMacOs(di: *DebugInfo, symbol: MachoSymbol, target_address: u
981 } else {988 } else {
982 return error.MissingDebugInfo;989 return error.MissingDebugInfo;
983 };990 };
984 const sections = @ptrCast([*]const std.c.section_64, ptr + @sizeOf(std.c.segment_command_64))[0..segcmd.nsects];991 const sections = @alignCast(@alignOf(std.c.section_64), @ptrCast([*]const std.c.section_64, ptr + @sizeOf(std.c.segment_command_64)))[0..segcmd.nsects];
985 for (sections) |sect| {992 for (sections) |*sect| {
986 if (sect.flags & std.c.SECTION_TYPE == std.c.S_REGULAR and993 if (sect.flags & std.c.SECTION_TYPE == std.c.S_REGULAR and
987 (sect.flags & std.c.SECTION_ATTRIBUTES) & std.c.S_ATTR_DEBUG == std.c.S_ATTR_DEBUG) {994 (sect.flags & std.c.SECTION_ATTRIBUTES) & std.c.S_ATTR_DEBUG == std.c.S_ATTR_DEBUG) {
988 const sect_name = mem.toSliceConst(u8, &sect.sectname);995 const sect_name = mem.toSliceConst(u8, &sect.sectname);
996 if (mem.eql(u8, sect_name, "__debug_line")) {
997 gop.kv.value.sect_debug_line = sect;
998 } else if (mem.eql(u8, sect_name, "__debug_info")) {
999 gop.kv.value.sect_debug_info = sect;
1000 }
989 std.debug.warn("sect: {}\n", sect_name);1001 std.debug.warn("sect: {}\n", sect_name);
990 }1002 }
991 }1003 }