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