authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-02-11 09:40:21+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-02-20 19:41:29+01:00
log2a350cf1745f08c98c5897ce2d27627d28ea00da
tree20c726e243886b054e188b63fd2dfb641482064d
parentb3f728585a06d652393ca6d3feaaac5a42bfa968

osx


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

lib/std/debug.zig+28-10
......@@ -696,8 +696,10 @@ fn machoSearchSymbols(symbols: []const MachoSymbol, address: usize) ?*const Mach
696696 return null;
697697}
698698
699fn printSourceAtAddressMacOs(di: *DebugInfo, out_stream: var, address: usize, tty_config: TTY.Config) !void {
700 const base_addr = process.getBaseAddress();
699fn printSourceAtAddressMacOs(di1: *DebugInfo, out_stream: var, address: usize, tty_config: TTY.Config) !void {
700 const di = try di1.lookupByAddress(address);
701
702 const base_addr = di.base_address;
701703 const adjusted_addr = 0x100000000 + (address - base_addr);
702704
703705 const symbol = machoSearchSymbols(di.symbols, adjusted_addr) orelse {
......@@ -826,10 +828,7 @@ pub fn openSelfDebugInfo(allocator: *mem.Allocator) !DebugInfo {
826828 if (@hasDecl(root, "os") and @hasDecl(root.os, "debug") and @hasDecl(root.os.debug, "openSelfDebugInfo")) {
827829 return noasync root.os.debug.openSelfDebugInfo(allocator);
828830 }
829 if (comptime std.Target.current.isDarwin()) {
830 return noasync openSelfDebugInfoMacOs(allocator);
831 }
832 if (builtin.os == .linux or builtin.os == .windows) {
831 if (builtin.os == .linux or builtin.os == .windows or builtin.os == .macosx) {
833832 _ = try allocator.create(u32);
834833 return DebugInfo.init(allocator);
835834 }
......@@ -1220,13 +1219,34 @@ pub const DebugInfo = struct {
12201219 }
12211220
12221221 pub fn lookupByAddress(self: *DebugInfo, address: usize) !*ObjectDebugInfo {
1223 const obj_di = if (builtin.os == .windows)
1222 const obj_di = if (comptime std.Target.current.isDarwin())
1223 try self.lookupModuleDyld(address)
1224 else if (builtin.os == .windows)
12241225 try self.lookupModuleWin32(address)
12251226 else
12261227 try self.lookupModuleDl(address);
12271228 return obj_di;
12281229 }
12291230
1231 fn lookupModuleDyld(self: *DebugInfo, address: usize) !*ObjectDebugInfo {
1232 const image_count = std.c._dyld_image_count();
1233
1234 var i: u32 = 0;
1235 while (i < image_count) : (i += 1) {
1236 const header = std.c._dyld_get_image_header(i) orelse continue;
1237 // The array of load commands is right after the header
1238 var cmd_ptr = @intToPtr([*]u8, @ptrToInt(header) + @sizeOf(macho.mach_header_64));
1239 const cmd_ptr_end = cmd_ptr + header.sizeofcmds;
1240
1241 var cmd =
1242 for (cmds) |*lc| {
1243 if (lc.cmd != macho.LC_SEGMENT_64) continue;
1244
1245 const segment_cmd = @ptrCast(*macho.segment_command_64, lc);
1246 }
1247 }
1248 }
1249
12301250 fn lookupModuleWin32(self: *DebugInfo, address: usize) !*ObjectDebugInfo {
12311251 const process_handle = windows.kernel32.GetCurrentProcess();
12321252
......@@ -1298,10 +1318,7 @@ pub const DebugInfo = struct {
12981318 if (os.dl_iterate_phdr(DIPContext, dl_iterate_phdr_callback, &ctx) == 0)
12991319 return error.DebugInfoNotFound;
13001320
1301 warn("found in \"{s}\"\n", .{ctx.name});
1302
13031321 if (self.address_map.getValue(ctx.base_address)) |obj_di| {
1304 warn("cache hit!\n", .{});
13051322 return obj_di;
13061323 }
13071324
......@@ -1377,6 +1394,7 @@ fn dl_iterate_phdr_callback(info: *os.dl_phdr_info, size: usize, context: ?*DIPC
13771394
13781395pub const ObjectDebugInfo = switch (builtin.os) {
13791396 .macosx, .ios, .watchos, .tvos => struct {
1397 base_address: usize,
13801398 symbols: []const MachoSymbol,
13811399 strings: []const u8,
13821400 ofiles: OFileTable,