| ... | ... | @@ -696,8 +696,10 @@ fn machoSearchSymbols(symbols: []const MachoSymbol, address: usize) ?*const Mach |
| 696 | 696 | return null; |
| 697 | 697 | } |
| 698 | 698 | |
| 699 | | fn printSourceAtAddressMacOs(di: *DebugInfo, out_stream: var, address: usize, tty_config: TTY.Config) !void { |
| 700 | | const base_addr = process.getBaseAddress(); |
| 699 | fn 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; |
| 701 | 703 | const adjusted_addr = 0x100000000 + (address - base_addr); |
| 702 | 704 | |
| 703 | 705 | const symbol = machoSearchSymbols(di.symbols, adjusted_addr) orelse { |
| ... | ... | @@ -826,10 +828,7 @@ pub fn openSelfDebugInfo(allocator: *mem.Allocator) !DebugInfo { |
| 826 | 828 | if (@hasDecl(root, "os") and @hasDecl(root.os, "debug") and @hasDecl(root.os.debug, "openSelfDebugInfo")) { |
| 827 | 829 | return noasync root.os.debug.openSelfDebugInfo(allocator); |
| 828 | 830 | } |
| 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) { |
| 833 | 832 | _ = try allocator.create(u32); |
| 834 | 833 | return DebugInfo.init(allocator); |
| 835 | 834 | } |
| ... | ... | @@ -1220,13 +1219,34 @@ pub const DebugInfo = struct { |
| 1220 | 1219 | } |
| 1221 | 1220 | |
| 1222 | 1221 | 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) |
| 1224 | 1225 | try self.lookupModuleWin32(address) |
| 1225 | 1226 | else |
| 1226 | 1227 | try self.lookupModuleDl(address); |
| 1227 | 1228 | return obj_di; |
| 1228 | 1229 | } |
| 1229 | 1230 | |
| 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 | |
| 1230 | 1250 | fn lookupModuleWin32(self: *DebugInfo, address: usize) !*ObjectDebugInfo { |
| 1231 | 1251 | const process_handle = windows.kernel32.GetCurrentProcess(); |
| 1232 | 1252 | |
| ... | ... | @@ -1298,10 +1318,7 @@ pub const DebugInfo = struct { |
| 1298 | 1318 | if (os.dl_iterate_phdr(DIPContext, dl_iterate_phdr_callback, &ctx) == 0) |
| 1299 | 1319 | return error.DebugInfoNotFound; |
| 1300 | 1320 | |
| 1301 | | warn("found in \"{s}\"\n", .{ctx.name}); |
| 1302 | | |
| 1303 | 1321 | if (self.address_map.getValue(ctx.base_address)) |obj_di| { |
| 1304 | | warn("cache hit!\n", .{}); |
| 1305 | 1322 | return obj_di; |
| 1306 | 1323 | } |
| 1307 | 1324 | |
| ... | ... | @@ -1377,6 +1394,7 @@ fn dl_iterate_phdr_callback(info: *os.dl_phdr_info, size: usize, context: ?*DIPC |
| 1377 | 1394 | |
| 1378 | 1395 | pub const ObjectDebugInfo = switch (builtin.os) { |
| 1379 | 1396 | .macosx, .ios, .watchos, .tvos => struct { |
| 1397 | base_address: usize, |
| 1380 | 1398 | symbols: []const MachoSymbol, |
| 1381 | 1399 | strings: []const u8, |
| 1382 | 1400 | ofiles: OFileTable, |