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
253253 return null;
254254}
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 {
257257 const base_addr = @ptrToInt(&std.c._mh_execute_header);
258258 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 {
261261 if (tty_color) {
262262 try out_stream.print("???:?:?: " ++ DIM ++ "0x{x} in ??? (???)" ++ RESET ++ "\n\n\n", address);
263263 } else {
......@@ -266,16 +266,19 @@ fn printSourceAtAddressMacOs(debug_info: *DebugInfo, out_stream: var, address: u
266266 return;
267267 };
268268
269 const symbol_name = mem.toSliceConst(u8, debug_info.strings.ptr + symbol.nlist.n_strx);
270 if (getLineNumberInfoMacOs(debug_info, symbol.*, address)) |line_info| {
271 const compile_unit_name = "???";
272 try printLineInfo(debug_info, out_stream, line_info, address, symbol_name, compile_unit_name, tty_color);
269 const symbol_name = mem.toSliceConst(u8, di.strings.ptr + symbol.nlist.n_strx);
270 const compile_unit_name = if (symbol.ofile) |ofile| blk: {
271 const ofile_path = mem.toSliceConst(u8, di.strings.ptr + ofile.n_strx);
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);
273276 } else |err| switch (err) {
274277 error.MissingDebugInfo => {
275278 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);
277280 } 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);
279282 }
280283 },
281284 else => return err,
......@@ -516,6 +519,8 @@ const MachoSymbol = struct {
516519
517520const MachOFile = struct {
518521 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,
519524};
520525
521526pub const DebugInfo = switch (builtin.os) {
......@@ -964,6 +969,8 @@ fn getLineNumberInfoMacOs(di: *DebugInfo, symbol: MachoSymbol, target_address: u
964969
965970 gop.kv.value = MachOFile{
966971 .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,
967974 };
968975 const hdr = @ptrCast(*const std.c.mach_header_64, gop.kv.value.bytes.ptr);
969976 assert(hdr.magic == std.c.MH_MAGIC_64);
......@@ -981,11 +988,16 @@ fn getLineNumberInfoMacOs(di: *DebugInfo, symbol: MachoSymbol, target_address: u
981988 } else {
982989 return error.MissingDebugInfo;
983990 };
984 const sections = @ptrCast([*]const std.c.section_64, ptr + @sizeOf(std.c.segment_command_64))[0..segcmd.nsects];
985 for (sections) |sect| {
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];
992 for (sections) |*sect| {
986993 if (sect.flags & std.c.SECTION_TYPE == std.c.S_REGULAR and
987994 (sect.flags & std.c.SECTION_ATTRIBUTES) & std.c.S_ATTR_DEBUG == std.c.S_ATTR_DEBUG) {
988995 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 }
9891001 std.debug.warn("sect: {}\n", sect_name);
9901002 }
9911003 }