| ... | ... | @@ -11,18 +11,8 @@ const native_arch = builtin.cpu.arch; |
| 11 | 11 | const std = @import("../std.zig"); |
| 12 | 12 | const mem = std.mem; |
| 13 | 13 | const Allocator = std.mem.Allocator; |
| 14 | | const windows = std.os.windows; |
| 15 | | const macho = std.macho; |
| 16 | | const fs = std.fs; |
| 17 | | const coff = std.coff; |
| 18 | 14 | const assert = std.debug.assert; |
| 19 | | const posix = std.posix; |
| 20 | | const elf = std.elf; |
| 21 | 15 | const Dwarf = std.debug.Dwarf; |
| 22 | | const Pdb = std.debug.Pdb; |
| 23 | | const File = std.fs.File; |
| 24 | | const math = std.math; |
| 25 | | const testing = std.testing; |
| 26 | 16 | const regBytes = Dwarf.abi.regBytes; |
| 27 | 17 | const regValueNative = Dwarf.abi.regValueNative; |
| 28 | 18 | |
| ... | ... | @@ -31,6 +21,7 @@ const SelfInfo = @This(); |
| 31 | 21 | modules: std.AutoHashMapUnmanaged(usize, Module.DebugInfo), |
| 32 | 22 | lookup_cache: Module.LookupCache, |
| 33 | 23 | |
| 24 | /// Indicates whether the `SelfInfo` implementation has support for this target. |
| 34 | 25 | pub const target_supported: bool = switch (native_os) { |
| 35 | 26 | .linux, |
| 36 | 27 | .freebsd, |
| ... | ... | @@ -45,9 +36,39 @@ pub const target_supported: bool = switch (native_os) { |
| 45 | 36 | else => false, |
| 46 | 37 | }; |
| 47 | 38 | |
| 39 | /// Indicates whether unwinding for the host is *implemented* here in the Zig |
| 40 | /// standard library. |
| 41 | /// |
| 42 | /// See also `Dwarf.abi.supportsUnwinding` which tells whether Dwarf supports |
| 43 | /// unwinding on a target *in theory*. |
| 44 | pub const supports_unwinding: bool = switch (builtin.target.cpu.arch) { |
| 45 | .x86 => switch (builtin.target.os.tag) { |
| 46 | .linux, .netbsd, .solaris, .illumos => true, |
| 47 | else => false, |
| 48 | }, |
| 49 | .x86_64 => switch (builtin.target.os.tag) { |
| 50 | .linux, .netbsd, .freebsd, .openbsd, .macos, .ios, .solaris, .illumos => true, |
| 51 | else => false, |
| 52 | }, |
| 53 | .arm, .armeb, .thumb, .thumbeb => switch (builtin.target.os.tag) { |
| 54 | .linux => true, |
| 55 | else => false, |
| 56 | }, |
| 57 | .aarch64, .aarch64_be => switch (builtin.target.os.tag) { |
| 58 | .linux, .netbsd, .freebsd, .macos, .ios => true, |
| 59 | else => false, |
| 60 | }, |
| 61 | // Unwinding is possible on other targets but this implementation does |
| 62 | // not support them...yet! |
| 63 | else => false, |
| 64 | }; |
| 65 | comptime { |
| 66 | if (supports_unwinding) assert(Dwarf.abi.supportsUnwinding(&builtin.target)); |
| 67 | } |
| 68 | |
| 48 | 69 | pub const init: SelfInfo = .{ |
| 49 | 70 | .modules = .empty, |
| 50 | | .lookup_cache = if (Module.LookupCache != void) .init, |
| 71 | .lookup_cache = .init, |
| 51 | 72 | }; |
| 52 | 73 | |
| 53 | 74 | pub fn deinit(self: *SelfInfo) void { |
| ... | ... | @@ -59,19 +80,14 @@ pub fn deinit(self: *SelfInfo) void { |
| 59 | 80 | self.allocator.destroy(mdi); |
| 60 | 81 | } |
| 61 | 82 | self.modules.deinit(self.allocator); |
| 62 | | if (native_os == .windows) { |
| 63 | | for (self.modules.items) |module| { |
| 64 | | self.allocator.free(module.name); |
| 65 | | if (module.mapped_file) |mapped_file| mapped_file.deinit(); |
| 66 | | } |
| 67 | | self.modules.deinit(self.allocator); |
| 68 | | } |
| 69 | 83 | } |
| 70 | 84 | |
| 71 | 85 | pub fn unwindFrame(self: *SelfInfo, gpa: Allocator, context: *UnwindContext) !usize { |
| 72 | | comptime assert(target_supported); |
| 86 | comptime assert(supports_unwinding); |
| 73 | 87 | const module: Module = try .lookup(&self.lookup_cache, gpa, context.pc); |
| 74 | 88 | const gop = try self.modules.getOrPut(gpa, module.load_offset); |
| 89 | self.modules.lockPointers(); |
| 90 | defer self.modules.unlockPointers(); |
| 75 | 91 | if (!gop.found_existing) gop.value_ptr.* = .init; |
| 76 | 92 | return module.unwindFrame(gpa, gop.value_ptr, context); |
| 77 | 93 | } |
| ... | ... | @@ -80,417 +96,39 @@ pub fn getSymbolAtAddress(self: *SelfInfo, gpa: Allocator, address: usize) !std. |
| 80 | 96 | comptime assert(target_supported); |
| 81 | 97 | const module: Module = try .lookup(&self.lookup_cache, gpa, address); |
| 82 | 98 | const gop = try self.modules.getOrPut(gpa, module.key()); |
| 99 | self.modules.lockPointers(); |
| 100 | defer self.modules.unlockPointers(); |
| 83 | 101 | if (!gop.found_existing) gop.value_ptr.* = .init; |
| 84 | 102 | return module.getSymbolAtAddress(gpa, gop.value_ptr, address); |
| 85 | 103 | } |
| 86 | 104 | |
| 87 | | /// Returns the module name for a given address. |
| 88 | | /// This can be called when getModuleForAddress fails, so implementations should provide |
| 89 | | /// a path that doesn't rely on any side-effects of a prior successful module lookup. |
| 90 | 105 | pub fn getModuleNameForAddress(self: *SelfInfo, gpa: Allocator, address: usize) error{ Unexpected, OutOfMemory, MissingDebugInfo }![]const u8 { |
| 91 | 106 | comptime assert(target_supported); |
| 92 | 107 | const module: Module = try .lookup(&self.lookup_cache, gpa, address); |
| 93 | 108 | return module.name; |
| 94 | 109 | } |
| 95 | 110 | |
| 111 | /// This type contains the target-specific implementation. It must expose the following declarations: |
| 112 | /// |
| 113 | /// * `LookupCache: type` |
| 114 | /// * `LookupCache.init: LookupCache` |
| 115 | /// * `lookup: fn (*LookupCache, Allocator, address: usize) !Module` |
| 116 | /// * `key: fn (*const Module) usize` |
| 117 | /// * `DebugInfo: type` |
| 118 | /// * `DebugInfo.init: DebugInfo` |
| 119 | /// * `getSymbolAtAddress: fn (*const Module, Allocator, *DebugInfo, address: usize) !std.debug.Symbol` |
| 120 | /// |
| 121 | /// If unwinding is supported on this target, it must additionally expose the following declarations: |
| 122 | /// |
| 123 | /// * `unwindFrame: fn (*const Module, Allocator, *DebugInfo, *UnwindContext) !usize` |
| 96 | 124 | const Module = switch (native_os) { |
| 97 | 125 | else => {}, // Dwarf, // TODO MLUGG: it's this on master but that's definitely broken atm... |
| 98 | | .macos, .ios, .watchos, .tvos, .visionos => struct { |
| 99 | | /// The runtime address where __TEXT is loaded. |
| 100 | | text_base: usize, |
| 101 | | load_offset: usize, |
| 102 | | name: []const u8, |
| 103 | | fn key(m: *const Module) usize { |
| 104 | | return m.text_base; |
| 105 | | } |
| 106 | | fn lookup(cache: *LookupCache, gpa: Allocator, address: usize) !Module { |
| 107 | | _ = cache; |
| 108 | | _ = gpa; |
| 109 | | const image_count = std.c._dyld_image_count(); |
| 110 | | for (0..image_count) |image_idx| { |
| 111 | | const header = std.c._dyld_get_image_header(@intCast(image_idx)) orelse continue; |
| 112 | | const text_base = @intFromPtr(header); |
| 113 | | if (address < text_base) continue; |
| 114 | | const load_offset = std.c._dyld_get_image_vmaddr_slide(@intCast(image_idx)); |
| 115 | | |
| 116 | | // Find the __TEXT segment |
| 117 | | var it: macho.LoadCommandIterator = .{ |
| 118 | | .ncmds = header.ncmds, |
| 119 | | .buffer = @as([*]u8, @ptrCast(header))[@sizeOf(macho.mach_header_64)..][0..header.sizeofcmds], |
| 120 | | }; |
| 121 | | const text_segment_cmd = while (it.next()) |load_cmd| { |
| 122 | | if (load_cmd.cmd() != .SEGMENT_64) continue; |
| 123 | | const segment_cmd = load_cmd.cast(macho.segment_command_64).?; |
| 124 | | if (!mem.eql(u8, segment_cmd.segName(), "__TEXT")) continue; |
| 125 | | break segment_cmd; |
| 126 | | } else continue; |
| 127 | | |
| 128 | | const seg_start = load_offset + text_segment_cmd.vmaddr; |
| 129 | | assert(seg_start == text_base); |
| 130 | | const seg_end = seg_start + text_segment_cmd.vmsize; |
| 131 | | if (address < seg_start or address >= seg_end) continue; |
| 132 | | |
| 133 | | // We've found the matching __TEXT segment. This is the image we need. |
| 134 | | return .{ |
| 135 | | .text_base = text_base, |
| 136 | | .load_offset = load_offset, |
| 137 | | .name = mem.span(std.c._dyld_get_image_name(@intCast(image_idx))), |
| 138 | | }; |
| 139 | | } |
| 140 | | return error.MissingDebugInfo; |
| 141 | | } |
| 142 | | fn loadLocationInfo(module: *const Module, gpa: Allocator, di: *Module.DebugInfo) !void { |
| 143 | | const mapped_mem = try mapDebugInfoFile(module.name); |
| 144 | | errdefer posix.munmap(mapped_mem); |
| 145 | | |
| 146 | | const hdr: *const macho.mach_header_64 = @ptrCast(@alignCast(mapped_mem.ptr)); |
| 147 | | if (hdr.magic != macho.MH_MAGIC_64) |
| 148 | | return error.InvalidDebugInfo; |
| 149 | | |
| 150 | | const symtab: macho.symtab_command = symtab: { |
| 151 | | var it: macho.LoadCommandIterator = .{ |
| 152 | | .ncmds = hdr.ncmds, |
| 153 | | .buffer = mapped_mem[@sizeOf(macho.mach_header_64)..][0..hdr.sizeofcmds], |
| 154 | | }; |
| 155 | | while (it.next()) |cmd| switch (cmd.cmd()) { |
| 156 | | .SYMTAB => break :symtab cmd.cast(macho.symtab_command) orelse return error.InvalidDebugInfo, |
| 157 | | else => {}, |
| 158 | | }; |
| 159 | | return error.MissingDebugInfo; |
| 160 | | }; |
| 161 | | |
| 162 | | const syms_ptr: [*]align(1) const macho.nlist_64 = @ptrCast(mapped_mem[symtab.symoff..]); |
| 163 | | const syms = syms_ptr[0..symtab.nsyms]; |
| 164 | | const strings = mapped_mem[symtab.stroff..][0 .. symtab.strsize - 1 :0]; |
| 165 | | |
| 166 | | var symbols: std.ArrayList(MachoSymbol) = try .initCapacity(gpa, syms.len); |
| 167 | | defer symbols.deinit(gpa); |
| 168 | | |
| 169 | | var ofile: u32 = undefined; |
| 170 | | var last_sym: MachoSymbol = undefined; |
| 171 | | var state: enum { |
| 172 | | init, |
| 173 | | oso_open, |
| 174 | | oso_close, |
| 175 | | bnsym, |
| 176 | | fun_strx, |
| 177 | | fun_size, |
| 178 | | ensym, |
| 179 | | } = .init; |
| 180 | | |
| 181 | | for (syms) |*sym| { |
| 182 | | if (sym.n_type.bits.is_stab == 0) continue; |
| 183 | | |
| 184 | | // TODO handle globals N_GSYM, and statics N_STSYM |
| 185 | | switch (sym.n_type.stab) { |
| 186 | | .oso => switch (state) { |
| 187 | | .init, .oso_close => { |
| 188 | | state = .oso_open; |
| 189 | | ofile = sym.n_strx; |
| 190 | | }, |
| 191 | | else => return error.InvalidDebugInfo, |
| 192 | | }, |
| 193 | | .bnsym => switch (state) { |
| 194 | | .oso_open, .ensym => { |
| 195 | | state = .bnsym; |
| 196 | | last_sym = .{ |
| 197 | | .strx = 0, |
| 198 | | .addr = sym.n_value, |
| 199 | | .size = 0, |
| 200 | | .ofile = ofile, |
| 201 | | }; |
| 202 | | }, |
| 203 | | else => return error.InvalidDebugInfo, |
| 204 | | }, |
| 205 | | .fun => switch (state) { |
| 206 | | .bnsym => { |
| 207 | | state = .fun_strx; |
| 208 | | last_sym.strx = sym.n_strx; |
| 209 | | }, |
| 210 | | .fun_strx => { |
| 211 | | state = .fun_size; |
| 212 | | last_sym.size = @intCast(sym.n_value); |
| 213 | | }, |
| 214 | | else => return error.InvalidDebugInfo, |
| 215 | | }, |
| 216 | | .ensym => switch (state) { |
| 217 | | .fun_size => { |
| 218 | | state = .ensym; |
| 219 | | symbols.appendAssumeCapacity(last_sym); |
| 220 | | }, |
| 221 | | else => return error.InvalidDebugInfo, |
| 222 | | }, |
| 223 | | .so => switch (state) { |
| 224 | | .init, .oso_close => {}, |
| 225 | | .oso_open, .ensym => { |
| 226 | | state = .oso_close; |
| 227 | | }, |
| 228 | | else => return error.InvalidDebugInfo, |
| 229 | | }, |
| 230 | | else => {}, |
| 231 | | } |
| 232 | | } |
| 233 | | |
| 234 | | switch (state) { |
| 235 | | .init => return error.MissingDebugInfo, |
| 236 | | .oso_close => {}, |
| 237 | | else => return error.InvalidDebugInfo, |
| 238 | | } |
| 239 | | |
| 240 | | const symbols_slice = try symbols.toOwnedSlice(gpa); |
| 241 | | errdefer gpa.free(symbols_slice); |
| 242 | | |
| 243 | | // Even though lld emits symbols in ascending order, this debug code |
| 244 | | // should work for programs linked in any valid way. |
| 245 | | // This sort is so that we can binary search later. |
| 246 | | mem.sort(MachoSymbol, symbols_slice, {}, MachoSymbol.addressLessThan); |
| 247 | | |
| 248 | | di.full = .{ |
| 249 | | .mapped_memory = mapped_mem, |
| 250 | | .symbols = symbols_slice, |
| 251 | | .strings = strings, |
| 252 | | .ofiles = .empty, |
| 253 | | }; |
| 254 | | } |
| 255 | | fn loadUnwindInfo(module: *const Module, gpa: Allocator, di: *Module.DebugInfo) !void { |
| 256 | | _ = gpa; |
| 257 | | |
| 258 | | const header: *std.macho.mach_header = @ptrFromInt(module.text_base); |
| 259 | | |
| 260 | | var it: macho.LoadCommandIterator = .{ |
| 261 | | .ncmds = header.ncmds, |
| 262 | | .buffer = @as([*]u8, @ptrCast(header))[@sizeOf(macho.mach_header_64)..][0..header.sizeofcmds], |
| 263 | | }; |
| 264 | | const sections = while (it.next()) |load_cmd| { |
| 265 | | if (load_cmd.cmd() != .SEGMENT_64) continue; |
| 266 | | const segment_cmd = load_cmd.cast(macho.segment_command_64).?; |
| 267 | | if (!mem.eql(u8, segment_cmd.segName(), "__TEXT")) continue; |
| 268 | | break load_cmd.getSections(); |
| 269 | | } else unreachable; |
| 270 | | |
| 271 | | var unwind_info: ?[]const u8 = null; |
| 272 | | var eh_frame: ?[]const u8 = null; |
| 273 | | for (sections) |sect| { |
| 274 | | if (mem.eql(u8, sect.sectName(), "__unwind_info")) { |
| 275 | | const sect_ptr: [*]u8 = @ptrFromInt(@as(usize, @intCast(module.load_offset + sect.addr))); |
| 276 | | unwind_info = sect_ptr[0..@intCast(sect.size)]; |
| 277 | | } else if (mem.eql(u8, sect.sectName(), "__eh_frame")) { |
| 278 | | const sect_ptr: [*]u8 = @ptrFromInt(@as(usize, @intCast(module.load_offset + sect.addr))); |
| 279 | | eh_frame = sect_ptr[0..@intCast(sect.size)]; |
| 280 | | } |
| 281 | | } |
| 282 | | di.unwind = .{ |
| 283 | | .unwind_info = unwind_info, |
| 284 | | .eh_frame = eh_frame, |
| 285 | | }; |
| 286 | | } |
| 287 | | fn getSymbolAtAddress(module: *const Module, gpa: Allocator, di: *DebugInfo, address: usize) !std.debug.Symbol { |
| 288 | | if (di.full == null) try module.loadLocationInfo(gpa, di); |
| 289 | | const vaddr = address - module.load_offset; |
| 290 | | const symbol = MachoSymbol.find(di.full.?.symbols, vaddr) orelse return .{ |
| 291 | | .name = null, |
| 292 | | .compile_unit_name = null, |
| 293 | | .source_location = null, |
| 294 | | }; |
| 295 | | |
| 296 | | // offset of `address` from start of `symbol` |
| 297 | | const address_symbol_offset = vaddr - symbol.addr; |
| 298 | | |
| 299 | | // Take the symbol name from the N_FUN STAB entry, we're going to |
| 300 | | // use it if we fail to find the DWARF infos |
| 301 | | const stab_symbol = mem.sliceTo(di.full.?.strings[symbol.strx..], 0); |
| 302 | | const o_file_path = mem.sliceTo(di.full.?.strings[symbol.ofile..], 0); |
| 303 | | |
| 304 | | // If any information is missing, we can at least return this from now on. |
| 305 | | const sym_only_result: std.debug.Symbol = .{ |
| 306 | | .name = stab_symbol, |
| 307 | | .compile_unit_name = null, |
| 308 | | .source_location = null, |
| 309 | | }; |
| 310 | | |
| 311 | | const o_file: *DebugInfo.OFile = of: { |
| 312 | | const gop = try di.full.?.ofiles.getOrPut(gpa, o_file_path); |
| 313 | | if (!gop.found_existing) { |
| 314 | | gop.value_ptr.* = DebugInfo.loadOFile(gpa, o_file_path) catch |err| { |
| 315 | | defer _ = di.full.?.ofiles.pop().?; |
| 316 | | switch (err) { |
| 317 | | error.MissingDebugInfo, |
| 318 | | error.InvalidDebugInfo, |
| 319 | | => return sym_only_result, |
| 320 | | else => |e| return e, |
| 321 | | } |
| 322 | | }; |
| 323 | | } |
| 324 | | break :of gop.value_ptr; |
| 325 | | }; |
| 326 | | |
| 327 | | const symbol_ofile_vaddr = o_file.addr_table.get(stab_symbol) orelse return sym_only_result; |
| 328 | | |
| 329 | | const compile_unit = o_file.dwarf.findCompileUnit(native_endian, symbol_ofile_vaddr) catch |err| switch (err) { |
| 330 | | error.MissingDebugInfo, error.InvalidDebugInfo => return sym_only_result, |
| 331 | | else => |e| return e, |
| 332 | | }; |
| 333 | | |
| 334 | | return .{ |
| 335 | | .name = o_file.dwarf.getSymbolName(symbol_ofile_vaddr) orelse stab_symbol, |
| 336 | | .compile_unit_name = compile_unit.die.getAttrString( |
| 337 | | &o_file.dwarf, |
| 338 | | native_endian, |
| 339 | | std.dwarf.AT.name, |
| 340 | | o_file.dwarf.section(.debug_str), |
| 341 | | compile_unit, |
| 342 | | ) catch |err| switch (err) { |
| 343 | | error.MissingDebugInfo, error.InvalidDebugInfo => null, |
| 344 | | }, |
| 345 | | .source_location = o_file.dwarf.getLineNumberInfo( |
| 346 | | gpa, |
| 347 | | native_endian, |
| 348 | | compile_unit, |
| 349 | | symbol_ofile_vaddr + address_symbol_offset, |
| 350 | | ) catch |err| switch (err) { |
| 351 | | error.MissingDebugInfo, error.InvalidDebugInfo => null, |
| 352 | | else => return err, |
| 353 | | }, |
| 354 | | }; |
| 355 | | } |
| 356 | | fn unwindFrame(module: *const Module, gpa: Allocator, di: *DebugInfo, context: *UnwindContext) !usize { |
| 357 | | if (di.unwind == null) try module.loadUnwindInfo(gpa, di); |
| 358 | | const unwind_info = di.unwind.?.unwind_info orelse return error.MissingUnwindInfo; |
| 359 | | // MLUGG TODO: inline? |
| 360 | | return unwindFrameMachO( |
| 361 | | module.text_base, |
| 362 | | module.load_offset, |
| 363 | | context, |
| 364 | | unwind_info, |
| 365 | | di.unwind.?.eh_frame, |
| 366 | | ); |
| 367 | | } |
| 368 | | const LookupCache = void; |
| 369 | | const DebugInfo = struct { |
| 370 | | unwind: ?struct { |
| 371 | | // Backed by the in-memory sections mapped by the loader |
| 372 | | unwind_info: ?[]const u8, |
| 373 | | eh_frame: ?[]const u8, |
| 374 | | }, |
| 375 | | // MLUGG TODO: awful field name |
| 376 | | full: ?struct { |
| 377 | | mapped_memory: []align(std.heap.page_size_min) const u8, |
| 378 | | symbols: []const MachoSymbol, |
| 379 | | strings: [:0]const u8, |
| 380 | | // MLUGG TODO: this could use an adapter to just index straight into `strings`! |
| 381 | | ofiles: std.StringArrayHashMapUnmanaged(OFile), |
| 382 | | }, |
| 383 | | |
| 384 | | const init: DebugInfo = .{ |
| 385 | | .unwind = null, |
| 386 | | .full = null, |
| 387 | | }; |
| 388 | | |
| 389 | | const OFile = struct { |
| 390 | | dwarf: Dwarf, |
| 391 | | // MLUGG TODO: this could use an adapter to just index straight into the strtab! |
| 392 | | addr_table: std.StringArrayHashMapUnmanaged(u64), |
| 393 | | }; |
| 394 | | |
| 395 | | fn deinit(di: *DebugInfo, gpa: Allocator) void { |
| 396 | | for (di.full.ofiles.values()) |*ofile| { |
| 397 | | ofile.dwarf.deinit(gpa); |
| 398 | | ofile.addr_table.deinit(gpa); |
| 399 | | } |
| 400 | | di.full.ofiles.deinit(); |
| 401 | | gpa.free(di.full.symbols); |
| 402 | | posix.munmap(di.full.mapped_memory); |
| 403 | | } |
| 404 | | |
| 405 | | fn loadOFile(gpa: Allocator, o_file_path: []const u8) !OFile { |
| 406 | | const mapped_mem = try mapDebugInfoFile(o_file_path); |
| 407 | | errdefer posix.munmap(mapped_mem); |
| 408 | | |
| 409 | | if (mapped_mem.len < @sizeOf(macho.mach_header_64)) return error.InvalidDebugInfo; |
| 410 | | const hdr: *const macho.mach_header_64 = @ptrCast(@alignCast(mapped_mem.ptr)); |
| 411 | | if (hdr.magic != std.macho.MH_MAGIC_64) return error.InvalidDebugInfo; |
| 412 | | |
| 413 | | const seg_cmd: macho.LoadCommandIterator.LoadCommand, const symtab_cmd: macho.symtab_command = cmds: { |
| 414 | | var seg_cmd: ?macho.LoadCommandIterator.LoadCommand = null; |
| 415 | | var symtab_cmd: ?macho.symtab_command = null; |
| 416 | | var it: macho.LoadCommandIterator = .{ |
| 417 | | .ncmds = hdr.ncmds, |
| 418 | | .buffer = mapped_mem[@sizeOf(macho.mach_header_64)..][0..hdr.sizeofcmds], |
| 419 | | }; |
| 420 | | while (it.next()) |cmd| switch (cmd.cmd()) { |
| 421 | | .SEGMENT_64 => seg_cmd = cmd, |
| 422 | | .SYMTAB => symtab_cmd = cmd.cast(macho.symtab_command) orelse return error.InvalidDebugInfo, |
| 423 | | else => {}, |
| 424 | | }; |
| 425 | | break :cmds .{ |
| 426 | | seg_cmd orelse return error.MissingDebugInfo, |
| 427 | | symtab_cmd orelse return error.MissingDebugInfo, |
| 428 | | }; |
| 429 | | }; |
| 430 | | |
| 431 | | if (mapped_mem.len < symtab_cmd.stroff + symtab_cmd.strsize) return error.InvalidDebugInfo; |
| 432 | | if (mapped_mem[symtab_cmd.stroff + symtab_cmd.strsize - 1] != 0) return error.InvalidDebugInfo; |
| 433 | | const strtab = mapped_mem[symtab_cmd.stroff..][0 .. symtab_cmd.strsize - 1]; |
| 434 | | |
| 435 | | const n_sym_bytes = symtab_cmd.nsyms * @sizeOf(macho.nlist_64); |
| 436 | | if (mapped_mem.len < symtab_cmd.symoff + n_sym_bytes) return error.InvalidDebugInfo; |
| 437 | | const symtab: []align(1) const macho.nlist_64 = @ptrCast(mapped_mem[symtab_cmd.symoff..][0..n_sym_bytes]); |
| 438 | | |
| 439 | | // TODO handle tentative (common) symbols |
| 440 | | var addr_table: std.StringArrayHashMapUnmanaged(u64) = .empty; |
| 441 | | defer addr_table.deinit(gpa); |
| 442 | | try addr_table.ensureUnusedCapacity(gpa, @intCast(symtab.len)); |
| 443 | | for (symtab) |sym| { |
| 444 | | if (sym.n_strx == 0) continue; |
| 445 | | switch (sym.n_type.bits.type) { |
| 446 | | .undf => continue, // includes tentative symbols |
| 447 | | .abs => continue, |
| 448 | | else => {}, |
| 449 | | } |
| 450 | | const sym_name = mem.sliceTo(strtab[sym.n_strx..], 0); |
| 451 | | const gop = addr_table.getOrPutAssumeCapacity(sym_name); |
| 452 | | if (gop.found_existing) return error.InvalidDebugInfo; |
| 453 | | gop.value_ptr.* = sym.n_value; |
| 454 | | } |
| 455 | | |
| 456 | | var sections: Dwarf.SectionArray = @splat(null); |
| 457 | | for (seg_cmd.getSections()) |sect| { |
| 458 | | if (!std.mem.eql(u8, "__DWARF", sect.segName())) continue; |
| 459 | | |
| 460 | | const section_index: usize = inline for (@typeInfo(Dwarf.Section.Id).@"enum".fields, 0..) |section, i| { |
| 461 | | if (mem.eql(u8, "__" ++ section.name, sect.sectName())) break i; |
| 462 | | } else continue; |
| 463 | | |
| 464 | | if (mapped_mem.len < sect.offset + sect.size) return error.InvalidDebugInfo; |
| 465 | | const section_bytes = mapped_mem[sect.offset..][0..sect.size]; |
| 466 | | sections[section_index] = .{ |
| 467 | | .data = section_bytes, |
| 468 | | .owned = false, |
| 469 | | }; |
| 470 | | } |
| 471 | | |
| 472 | | const missing_debug_info = |
| 473 | | sections[@intFromEnum(Dwarf.Section.Id.debug_info)] == null or |
| 474 | | sections[@intFromEnum(Dwarf.Section.Id.debug_abbrev)] == null or |
| 475 | | sections[@intFromEnum(Dwarf.Section.Id.debug_str)] == null or |
| 476 | | sections[@intFromEnum(Dwarf.Section.Id.debug_line)] == null; |
| 477 | | if (missing_debug_info) return error.MissingDebugInfo; |
| 478 | | |
| 479 | | var dwarf: Dwarf = .{ .sections = sections }; |
| 480 | | errdefer dwarf.deinit(gpa); |
| 481 | | try dwarf.open(gpa, native_endian); |
| 482 | | |
| 483 | | return .{ |
| 484 | | .dwarf = dwarf, |
| 485 | | .addr_table = addr_table.move(), |
| 486 | | }; |
| 487 | | } |
| 488 | | }; |
| 489 | | }, |
| 126 | .linux, .netbsd, .freebsd, .dragonfly, .openbsd, .haiku, .solaris, .illumos => @import("SelfInfo/ElfModule.zig"), |
| 127 | .macos, .ios, .watchos, .tvos, .visionos => @import("SelfInfo/DarwinModule.zig"), |
| 128 | .uefi, .windows => @import("SelfInfo/WindowsModule.zig"), |
| 490 | 129 | .wasi, .emscripten => struct { |
| 491 | | const LookupCache = void; |
| 492 | | const DebugInfo = struct { |
| 493 | | const init: DebugInfo = .{}; |
| 130 | const LookupCache = struct { |
| 131 | const init: LookupCache = .{}; |
| 494 | 132 | }; |
| 495 | 133 | fn lookup(cache: *LookupCache, gpa: Allocator, address: usize) !Module { |
| 496 | 134 | _ = cache; |
| ... | ... | @@ -498,6 +136,9 @@ const Module = switch (native_os) { |
| 498 | 136 | _ = address; |
| 499 | 137 | @panic("TODO implement lookup module for Wasm"); |
| 500 | 138 | } |
| 139 | const DebugInfo = struct { |
| 140 | const init: DebugInfo = .{}; |
| 141 | }; |
| 501 | 142 | fn getSymbolAtAddress(module: *const Module, gpa: Allocator, di: *DebugInfo, address: usize) !std.debug.Symbol { |
| 502 | 143 | _ = module; |
| 503 | 144 | _ = gpa; |
| ... | ... | @@ -506,430 +147,9 @@ const Module = switch (native_os) { |
| 506 | 147 | unreachable; |
| 507 | 148 | } |
| 508 | 149 | }, |
| 509 | | .linux, .netbsd, .freebsd, .dragonfly, .openbsd, .haiku, .solaris, .illumos => struct { |
| 510 | | load_offset: usize, |
| 511 | | name: []const u8, |
| 512 | | build_id: ?[]const u8, |
| 513 | | gnu_eh_frame: ?[]const u8, |
| 514 | | const LookupCache = void; |
| 515 | | const DebugInfo = struct { |
| 516 | | loaded_elf: ?Dwarf.ElfModule, |
| 517 | | unwind: ?Dwarf.Unwind, |
| 518 | | const init: DebugInfo = .{ |
| 519 | | .loaded_elf = null, |
| 520 | | .unwind = null, |
| 521 | | }; |
| 522 | | }; |
| 523 | | fn key(m: Module) usize { |
| 524 | | return m.load_offset; |
| 525 | | } |
| 526 | | fn lookup(cache: *LookupCache, gpa: Allocator, address: usize) !Module { |
| 527 | | _ = cache; |
| 528 | | _ = gpa; |
| 529 | | if (native_os == .haiku) @panic("TODO implement lookup module for Haiku"); |
| 530 | | const DlIterContext = struct { |
| 531 | | /// input |
| 532 | | address: usize, |
| 533 | | /// output |
| 534 | | module: Module, |
| 535 | | |
| 536 | | fn callback(info: *posix.dl_phdr_info, size: usize, context: *@This()) !void { |
| 537 | | _ = size; |
| 538 | | // The base address is too high |
| 539 | | if (context.address < info.addr) |
| 540 | | return; |
| 541 | | |
| 542 | | const phdrs = info.phdr[0..info.phnum]; |
| 543 | | for (phdrs) |*phdr| { |
| 544 | | if (phdr.p_type != elf.PT_LOAD) continue; |
| 545 | | |
| 546 | | // Overflowing addition is used to handle the case of VSDOs having a p_vaddr = 0xffffffffff700000 |
| 547 | | const seg_start = info.addr +% phdr.p_vaddr; |
| 548 | | const seg_end = seg_start + phdr.p_memsz; |
| 549 | | if (context.address >= seg_start and context.address < seg_end) { |
| 550 | | context.module = .{ |
| 551 | | .load_offset = info.addr, |
| 552 | | // Android libc uses NULL instead of "" to mark the main program |
| 553 | | .name = mem.sliceTo(info.name, 0) orelse "", |
| 554 | | .build_id = null, |
| 555 | | .gnu_eh_frame = null, |
| 556 | | }; |
| 557 | | break; |
| 558 | | } |
| 559 | | } else return; |
| 560 | | |
| 561 | | for (info.phdr[0..info.phnum]) |phdr| { |
| 562 | | switch (phdr.p_type) { |
| 563 | | elf.PT_NOTE => { |
| 564 | | // Look for .note.gnu.build-id |
| 565 | | const segment_ptr: [*]const u8 = @ptrFromInt(info.addr + phdr.p_vaddr); |
| 566 | | var r: std.Io.Reader = .fixed(segment_ptr[0..phdr.p_memsz]); |
| 567 | | const name_size = r.takeInt(u32, native_endian) catch continue; |
| 568 | | const desc_size = r.takeInt(u32, native_endian) catch continue; |
| 569 | | const note_type = r.takeInt(u32, native_endian) catch continue; |
| 570 | | const name = r.take(name_size) catch continue; |
| 571 | | if (note_type != elf.NT_GNU_BUILD_ID) continue; |
| 572 | | if (!mem.eql(u8, name, "GNU\x00")) continue; |
| 573 | | const desc = r.take(desc_size) catch continue; |
| 574 | | context.module.build_id = desc; |
| 575 | | }, |
| 576 | | elf.PT_GNU_EH_FRAME => { |
| 577 | | const segment_ptr: [*]const u8 = @ptrFromInt(info.addr + phdr.p_vaddr); |
| 578 | | context.module.gnu_eh_frame = segment_ptr[0..phdr.p_memsz]; |
| 579 | | }, |
| 580 | | else => {}, |
| 581 | | } |
| 582 | | } |
| 583 | | |
| 584 | | // Stop the iteration |
| 585 | | return error.Found; |
| 586 | | } |
| 587 | | }; |
| 588 | | var ctx: DlIterContext = .{ |
| 589 | | .address = address, |
| 590 | | .module = undefined, |
| 591 | | }; |
| 592 | | posix.dl_iterate_phdr(&ctx, error{Found}, DlIterContext.callback) catch |err| switch (err) { |
| 593 | | error.Found => return ctx.module, |
| 594 | | }; |
| 595 | | return error.MissingDebugInfo; |
| 596 | | } |
| 597 | | fn loadLocationInfo(module: *const Module, gpa: Allocator, di: *Module.DebugInfo) !void { |
| 598 | | if (module.name.len > 0) { |
| 599 | | di.loaded_elf = Dwarf.ElfModule.load(gpa, .{ |
| 600 | | .root_dir = .cwd(), |
| 601 | | .sub_path = module.name, |
| 602 | | }, module.build_id, null, null, null) catch |err| switch (err) { |
| 603 | | error.FileNotFound => return error.MissingDebugInfo, |
| 604 | | error.Overflow => return error.InvalidDebugInfo, |
| 605 | | else => |e| return e, |
| 606 | | }; |
| 607 | | } else { |
| 608 | | const path = try std.fs.selfExePathAlloc(gpa); |
| 609 | | defer gpa.free(path); |
| 610 | | di.loaded_elf = Dwarf.ElfModule.load(gpa, .{ |
| 611 | | .root_dir = .cwd(), |
| 612 | | .sub_path = path, |
| 613 | | }, module.build_id, null, null, null) catch |err| switch (err) { |
| 614 | | error.FileNotFound => return error.MissingDebugInfo, |
| 615 | | error.Overflow => return error.InvalidDebugInfo, |
| 616 | | else => |e| return e, |
| 617 | | }; |
| 618 | | } |
| 619 | | } |
| 620 | | fn getSymbolAtAddress(module: *const Module, gpa: Allocator, di: *DebugInfo, address: usize) !std.debug.Symbol { |
| 621 | | if (di.loaded_elf == null) try module.loadLocationInfo(gpa, di); |
| 622 | | const vaddr = address - module.load_offset; |
| 623 | | return di.loaded_elf.?.dwarf.getSymbol(gpa, native_endian, vaddr); |
| 624 | | } |
| 625 | | fn loadUnwindInfo(module: *const Module, gpa: Allocator, di: *Module.DebugInfo) !void { |
| 626 | | const section_bytes = module.gnu_eh_frame orelse return error.MissingUnwindInfo; // MLUGG TODO: load from file |
| 627 | | const section_vaddr: u64 = @intFromPtr(section_bytes.ptr) - module.load_offset; |
| 628 | | const header: Dwarf.Unwind.EhFrameHeader = try .parse(section_vaddr, section_bytes, @sizeOf(usize), native_endian); |
| 629 | | di.unwind = .initEhFrameHdr(header, section_vaddr, @ptrFromInt(module.load_offset + header.eh_frame_vaddr)); |
| 630 | | try di.unwind.?.prepareLookup(gpa, @sizeOf(usize), native_endian); |
| 631 | | } |
| 632 | | fn unwindFrame(module: *const Module, gpa: Allocator, di: *DebugInfo, context: *UnwindContext) !usize { |
| 633 | | if (di.unwind == null) try module.loadUnwindInfo(gpa, di); |
| 634 | | return unwindFrameDwarf(&di.unwind.?, module.load_offset, context, null); |
| 635 | | } |
| 636 | | }, |
| 637 | | .uefi, .windows => struct { |
| 638 | | base_address: usize, |
| 639 | | size: usize, |
| 640 | | name: []const u8, |
| 641 | | handle: windows.HMODULE, |
| 642 | | fn key(m: Module) usize { |
| 643 | | return m.base_address; |
| 644 | | } |
| 645 | | fn lookup(cache: *LookupCache, gpa: Allocator, address: usize) !Module { |
| 646 | | if (lookupInCache(cache, address)) |m| return m; |
| 647 | | { |
| 648 | | // Check a new module hasn't been loaded |
| 649 | | cache.modules.clearRetainingCapacity(); |
| 650 | | |
| 651 | | const handle = windows.kernel32.CreateToolhelp32Snapshot(windows.TH32CS_SNAPMODULE | windows.TH32CS_SNAPMODULE32, 0); |
| 652 | | if (handle == windows.INVALID_HANDLE_VALUE) { |
| 653 | | return windows.unexpectedError(windows.GetLastError()); |
| 654 | | } |
| 655 | | defer windows.CloseHandle(handle); |
| 656 | | |
| 657 | | var entry: windows.MODULEENTRY32 = undefined; |
| 658 | | entry.dwSize = @sizeOf(windows.MODULEENTRY32); |
| 659 | | if (windows.kernel32.Module32First(handle, &entry) != 0) { |
| 660 | | try cache.modules.append(gpa, entry); |
| 661 | | while (windows.kernel32.Module32Next(handle, &entry) != 0) { |
| 662 | | try cache.modules.append(gpa, entry); |
| 663 | | } |
| 664 | | } |
| 665 | | } |
| 666 | | if (lookupInCache(cache, address)) |m| return m; |
| 667 | | return error.MissingDebugInfo; |
| 668 | | } |
| 669 | | fn lookupInCache(cache: *const LookupCache, address: usize) ?Module { |
| 670 | | for (cache.modules.items) |*entry| { |
| 671 | | const base_address = @intFromPtr(entry.modBaseAddr); |
| 672 | | if (address >= base_address and address < base_address + entry.modBaseSize) { |
| 673 | | return .{ |
| 674 | | .base_address = base_address, |
| 675 | | .size = entry.modBaseSize, |
| 676 | | .name = std.mem.sliceTo(&entry.szModule, 0), |
| 677 | | .handle = entry.hModule, |
| 678 | | }; |
| 679 | | } |
| 680 | | } |
| 681 | | return null; |
| 682 | | } |
| 683 | | fn loadLocationInfo(module: *const Module, gpa: Allocator, di: *DebugInfo) !void { |
| 684 | | const mapped_ptr: [*]const u8 = @ptrFromInt(module.base_address); |
| 685 | | const mapped = mapped_ptr[0..module.size]; |
| 686 | | var coff_obj = coff.Coff.init(mapped, true) catch return error.InvalidDebugInfo; |
| 687 | | // The string table is not mapped into memory by the loader, so if a section name is in the |
| 688 | | // string table then we have to map the full image file from disk. This can happen when |
| 689 | | // a binary is produced with -gdwarf, since the section names are longer than 8 bytes. |
| 690 | | if (coff_obj.strtabRequired()) { |
| 691 | | var name_buffer: [windows.PATH_MAX_WIDE + 4:0]u16 = undefined; |
| 692 | | name_buffer[0..4].* = .{ '\\', '?', '?', '\\' }; // openFileAbsoluteW requires the prefix to be present |
| 693 | | const process_handle = windows.GetCurrentProcess(); |
| 694 | | const len = windows.kernel32.GetModuleFileNameExW( |
| 695 | | process_handle, |
| 696 | | module.handle, |
| 697 | | name_buffer[4..], |
| 698 | | windows.PATH_MAX_WIDE, |
| 699 | | ); |
| 700 | | if (len == 0) return error.MissingDebugInfo; |
| 701 | | const coff_file = fs.openFileAbsoluteW(name_buffer[0 .. len + 4 :0], .{}) catch |err| switch (err) { |
| 702 | | error.FileNotFound => return error.MissingDebugInfo, |
| 703 | | else => |e| return e, |
| 704 | | }; |
| 705 | | errdefer coff_file.close(); |
| 706 | | var section_handle: windows.HANDLE = undefined; |
| 707 | | const create_section_rc = windows.ntdll.NtCreateSection( |
| 708 | | &section_handle, |
| 709 | | windows.STANDARD_RIGHTS_REQUIRED | windows.SECTION_QUERY | windows.SECTION_MAP_READ, |
| 710 | | null, |
| 711 | | null, |
| 712 | | windows.PAGE_READONLY, |
| 713 | | // The documentation states that if no AllocationAttribute is specified, then SEC_COMMIT is the default. |
| 714 | | // In practice, this isn't the case and specifying 0 will result in INVALID_PARAMETER_6. |
| 715 | | windows.SEC_COMMIT, |
| 716 | | coff_file.handle, |
| 717 | | ); |
| 718 | | if (create_section_rc != .SUCCESS) return error.MissingDebugInfo; |
| 719 | | errdefer windows.CloseHandle(section_handle); |
| 720 | | var coff_len: usize = 0; |
| 721 | | var section_view_ptr: [*]const u8 = undefined; |
| 722 | | const map_section_rc = windows.ntdll.NtMapViewOfSection( |
| 723 | | section_handle, |
| 724 | | process_handle, |
| 725 | | @ptrCast(&section_view_ptr), |
| 726 | | null, |
| 727 | | 0, |
| 728 | | null, |
| 729 | | &coff_len, |
| 730 | | .ViewUnmap, |
| 731 | | 0, |
| 732 | | windows.PAGE_READONLY, |
| 733 | | ); |
| 734 | | if (map_section_rc != .SUCCESS) return error.MissingDebugInfo; |
| 735 | | errdefer assert(windows.ntdll.NtUnmapViewOfSection(process_handle, @constCast(section_view_ptr)) == .SUCCESS); |
| 736 | | const section_view = section_view_ptr[0..coff_len]; |
| 737 | | coff_obj = coff.Coff.init(section_view, false) catch return error.InvalidDebugInfo; |
| 738 | | di.mapped_file = .{ |
| 739 | | .file = coff_file, |
| 740 | | .section_handle = section_handle, |
| 741 | | .section_view = section_view, |
| 742 | | }; |
| 743 | | } |
| 744 | | di.coff_image_base = coff_obj.getImageBase(); |
| 745 | | |
| 746 | | if (coff_obj.getSectionByName(".debug_info")) |_| { |
| 747 | | di.dwarf = .{}; |
| 748 | | |
| 749 | | inline for (@typeInfo(Dwarf.Section.Id).@"enum".fields, 0..) |section, i| { |
| 750 | | di.dwarf.?.sections[i] = if (coff_obj.getSectionByName("." ++ section.name)) |section_header| blk: { |
| 751 | | break :blk .{ |
| 752 | | .data = try coff_obj.getSectionDataAlloc(section_header, gpa), |
| 753 | | .owned = true, |
| 754 | | }; |
| 755 | | } else null; |
| 756 | | } |
| 757 | | |
| 758 | | try di.dwarf.?.open(gpa, native_endian); |
| 759 | | } |
| 760 | | |
| 761 | | if (try coff_obj.getPdbPath()) |raw_path| pdb: { |
| 762 | | const path = blk: { |
| 763 | | if (fs.path.isAbsolute(raw_path)) { |
| 764 | | break :blk raw_path; |
| 765 | | } else { |
| 766 | | const self_dir = try fs.selfExeDirPathAlloc(gpa); |
| 767 | | defer gpa.free(self_dir); |
| 768 | | break :blk try fs.path.join(gpa, &.{ self_dir, raw_path }); |
| 769 | | } |
| 770 | | }; |
| 771 | | defer if (path.ptr != raw_path.ptr) gpa.free(path); |
| 772 | | |
| 773 | | di.pdb = Pdb.init(gpa, path) catch |err| switch (err) { |
| 774 | | error.FileNotFound, error.IsDir => break :pdb, |
| 775 | | else => return err, |
| 776 | | }; |
| 777 | | try di.pdb.?.parseInfoStream(); |
| 778 | | try di.pdb.?.parseDbiStream(); |
| 779 | | |
| 780 | | if (!mem.eql(u8, &coff_obj.guid, &di.pdb.?.guid) or coff_obj.age != di.pdb.?.age) |
| 781 | | return error.InvalidDebugInfo; |
| 782 | | |
| 783 | | di.coff_section_headers = try coff_obj.getSectionHeadersAlloc(gpa); |
| 784 | | } |
| 785 | | |
| 786 | | di.loaded = true; |
| 787 | | } |
| 788 | | const LookupCache = struct { |
| 789 | | modules: std.ArrayListUnmanaged(windows.MODULEENTRY32), |
| 790 | | const init: LookupCache = .{ .modules = .empty }; |
| 791 | | }; |
| 792 | | const DebugInfo = struct { |
| 793 | | loaded: bool, |
| 794 | | |
| 795 | | coff_image_base: u64, |
| 796 | | mapped_file: ?struct { |
| 797 | | file: File, |
| 798 | | section_handle: windows.HANDLE, |
| 799 | | section_view: []const u8, |
| 800 | | fn deinit(mapped: @This()) void { |
| 801 | | const process_handle = windows.GetCurrentProcess(); |
| 802 | | assert(windows.ntdll.NtUnmapViewOfSection(process_handle, @constCast(mapped.section_view.ptr)) == .SUCCESS); |
| 803 | | windows.CloseHandle(mapped.section_handle); |
| 804 | | mapped.file.close(); |
| 805 | | } |
| 806 | | }, |
| 807 | | |
| 808 | | dwarf: ?Dwarf, |
| 809 | | |
| 810 | | pdb: ?Pdb, |
| 811 | | /// Populated iff `pdb != null`; otherwise `&.{}`. |
| 812 | | coff_section_headers: []coff.SectionHeader, |
| 813 | | |
| 814 | | const init: DebugInfo = .{ |
| 815 | | .loaded = false, |
| 816 | | .coff_image_base = undefined, |
| 817 | | .mapped_file = null, |
| 818 | | .dwarf = null, |
| 819 | | .pdb = null, |
| 820 | | .coff_section_headers = &.{}, |
| 821 | | }; |
| 822 | | |
| 823 | | fn deinit(di: *DebugInfo, gpa: Allocator) void { |
| 824 | | if (di.dwarf) |*dwarf| dwarf.deinit(gpa); |
| 825 | | if (di.pdb) |*pdb| pdb.deinit(); |
| 826 | | gpa.free(di.coff_section_headers); |
| 827 | | if (di.mapped_file) |mapped| mapped.deinit(); |
| 828 | | } |
| 829 | | |
| 830 | | fn getSymbolFromPdb(di: *DebugInfo, relocated_address: usize) !?std.debug.Symbol { |
| 831 | | var coff_section: *align(1) const coff.SectionHeader = undefined; |
| 832 | | const mod_index = for (di.pdb.?.sect_contribs) |sect_contrib| { |
| 833 | | if (sect_contrib.section > di.coff_section_headers.len) continue; |
| 834 | | // Remember that SectionContribEntry.Section is 1-based. |
| 835 | | coff_section = &di.coff_section_headers[sect_contrib.section - 1]; |
| 836 | | |
| 837 | | const vaddr_start = coff_section.virtual_address + sect_contrib.offset; |
| 838 | | const vaddr_end = vaddr_start + sect_contrib.size; |
| 839 | | if (relocated_address >= vaddr_start and relocated_address < vaddr_end) { |
| 840 | | break sect_contrib.module_index; |
| 841 | | } |
| 842 | | } else { |
| 843 | | // we have no information to add to the address |
| 844 | | return null; |
| 845 | | }; |
| 846 | | |
| 847 | | const module = try di.pdb.?.getModule(mod_index) orelse return error.InvalidDebugInfo; |
| 848 | | |
| 849 | | return .{ |
| 850 | | .name = di.pdb.?.getSymbolName( |
| 851 | | module, |
| 852 | | relocated_address - coff_section.virtual_address, |
| 853 | | ), |
| 854 | | .compile_unit_name = fs.path.basename(module.obj_file_name), |
| 855 | | .source_location = try di.pdb.?.getLineNumberInfo( |
| 856 | | module, |
| 857 | | relocated_address - coff_section.virtual_address, |
| 858 | | ), |
| 859 | | }; |
| 860 | | } |
| 861 | | }; |
| 862 | | |
| 863 | | fn getSymbolAtAddress(module: *const Module, gpa: Allocator, di: *DebugInfo, address: usize) !std.debug.Symbol { |
| 864 | | if (!di.loaded) try module.loadLocationInfo(gpa, di); |
| 865 | | // Translate the runtime address into a virtual address into the module |
| 866 | | const vaddr = address - module.base_address; |
| 867 | | |
| 868 | | if (di.pdb != null) { |
| 869 | | if (try di.getSymbolFromPdb(vaddr)) |symbol| return symbol; |
| 870 | | } |
| 871 | | |
| 872 | | if (di.dwarf) |*dwarf| { |
| 873 | | const dwarf_address = vaddr + di.coff_image_base; |
| 874 | | return dwarf.getSymbol(gpa, native_endian, dwarf_address); |
| 875 | | } |
| 876 | | |
| 877 | | return error.MissingDebugInfo; |
| 878 | | } |
| 879 | | }, |
| 880 | | }; |
| 881 | | |
| 882 | | const MachoSymbol = struct { |
| 883 | | strx: u32, |
| 884 | | addr: u64, |
| 885 | | size: u32, |
| 886 | | ofile: u32, |
| 887 | | fn addressLessThan(context: void, lhs: MachoSymbol, rhs: MachoSymbol) bool { |
| 888 | | _ = context; |
| 889 | | return lhs.addr < rhs.addr; |
| 890 | | } |
| 891 | | /// Assumes that `symbols` is sorted in order of ascending `addr`. |
| 892 | | fn find(symbols: []const MachoSymbol, address: usize) ?*const MachoSymbol { |
| 893 | | if (symbols.len == 0) return null; // no potential match |
| 894 | | if (address < symbols[0].addr) return null; // address is before the lowest-address symbol |
| 895 | | var left: usize = 0; |
| 896 | | var len: usize = symbols.len; |
| 897 | | while (len > 1) { |
| 898 | | const mid = left + len / 2; |
| 899 | | if (address < symbols[mid].addr) { |
| 900 | | len /= 2; |
| 901 | | } else { |
| 902 | | left = mid; |
| 903 | | len -= len / 2; |
| 904 | | } |
| 905 | | } |
| 906 | | return &symbols[left]; |
| 907 | | } |
| 908 | | |
| 909 | | test find { |
| 910 | | const symbols: []const MachoSymbol = &.{ |
| 911 | | .{ .addr = 100, .strx = undefined, .size = undefined, .ofile = undefined }, |
| 912 | | .{ .addr = 200, .strx = undefined, .size = undefined, .ofile = undefined }, |
| 913 | | .{ .addr = 300, .strx = undefined, .size = undefined, .ofile = undefined }, |
| 914 | | }; |
| 915 | | |
| 916 | | try testing.expectEqual(null, find(symbols, 0)); |
| 917 | | try testing.expectEqual(null, find(symbols, 99)); |
| 918 | | try testing.expectEqual(&symbols[0], find(symbols, 100).?); |
| 919 | | try testing.expectEqual(&symbols[0], find(symbols, 150).?); |
| 920 | | try testing.expectEqual(&symbols[0], find(symbols, 199).?); |
| 921 | | |
| 922 | | try testing.expectEqual(&symbols[1], find(symbols, 200).?); |
| 923 | | try testing.expectEqual(&symbols[1], find(symbols, 250).?); |
| 924 | | try testing.expectEqual(&symbols[1], find(symbols, 299).?); |
| 925 | | |
| 926 | | try testing.expectEqual(&symbols[2], find(symbols, 300).?); |
| 927 | | try testing.expectEqual(&symbols[2], find(symbols, 301).?); |
| 928 | | try testing.expectEqual(&symbols[2], find(symbols, 5000).?); |
| 929 | | } |
| 930 | 150 | }; |
| 931 | 151 | test { |
| 932 | | _ = MachoSymbol; |
| 152 | _ = Module; |
| 933 | 153 | } |
| 934 | 154 | |
| 935 | 155 | pub const UnwindContext = struct { |
| ... | ... | @@ -944,6 +164,7 @@ pub const UnwindContext = struct { |
| 944 | 164 | pub fn init(gpa: Allocator, thread_context: *std.debug.ThreadContext) !UnwindContext { |
| 945 | 165 | comptime assert(supports_unwinding); |
| 946 | 166 | |
| 167 | const ip_reg_num = Dwarf.abi.ipRegNum(native_arch).?; |
| 947 | 168 | const pc = stripInstructionPtrAuthCode( |
| 948 | 169 | (try regValueNative(thread_context, ip_reg_num, null)).*, |
| 949 | 170 | ); |
| ... | ... | @@ -970,7 +191,7 @@ pub const UnwindContext = struct { |
| 970 | 191 | } |
| 971 | 192 | |
| 972 | 193 | pub fn getFp(self: *const UnwindContext) !usize { |
| 973 | | return (try regValueNative(self.thread_context, fpRegNum(self.reg_context), self.reg_context)).*; |
| 194 | return (try regValueNative(self.thread_context, Dwarf.abi.fpRegNum(native_arch, self.reg_context), self.reg_context)).*; |
| 974 | 195 | } |
| 975 | 196 | |
| 976 | 197 | /// Resolves the register rule and places the result into `out` (see regBytes) |
| ... | ... | @@ -1019,7 +240,7 @@ pub const UnwindContext = struct { |
| 1019 | 240 | .register => |register| { |
| 1020 | 241 | const src = try regBytes(context.thread_context, register, context.reg_context); |
| 1021 | 242 | if (src.len != out.len) return error.RegisterSizeMismatch; |
| 1022 | | @memcpy(out, try regBytes(context.thread_context, register, context.reg_context)); |
| 243 | @memcpy(out, src); |
| 1023 | 244 | }, |
| 1024 | 245 | .expression => |expression| { |
| 1025 | 246 | context.stack_machine.reset(); |
| ... | ... | @@ -1043,553 +264,171 @@ pub const UnwindContext = struct { |
| 1043 | 264 | .architectural => return error.UnimplementedRegisterRule, |
| 1044 | 265 | } |
| 1045 | 266 | } |
| 1046 | | }; |
| 1047 | | |
| 1048 | | /// Some platforms use pointer authentication - the upper bits of instruction pointers contain a signature. |
| 1049 | | /// This function clears these signature bits to make the pointer usable. |
| 1050 | | pub inline fn stripInstructionPtrAuthCode(ptr: usize) usize { |
| 1051 | | if (native_arch.isAARCH64()) { |
| 1052 | | // `hint 0x07` maps to `xpaclri` (or `nop` if the hardware doesn't support it) |
| 1053 | | // The save / restore is because `xpaclri` operates on x30 (LR) |
| 1054 | | return asm ( |
| 1055 | | \\mov x16, x30 |
| 1056 | | \\mov x30, x15 |
| 1057 | | \\hint 0x07 |
| 1058 | | \\mov x15, x30 |
| 1059 | | \\mov x30, x16 |
| 1060 | | : [ret] "={x15}" (-> usize), |
| 1061 | | : [ptr] "{x15}" (ptr), |
| 1062 | | : .{ .x16 = true }); |
| 1063 | | } |
| 1064 | | |
| 1065 | | return ptr; |
| 1066 | | } |
| 1067 | | |
| 1068 | | /// Unwind a stack frame using DWARF unwinding info, updating the register context. |
| 1069 | | /// |
| 1070 | | /// If `.eh_frame_hdr` is available and complete, it will be used to binary search for the FDE. |
| 1071 | | /// Otherwise, a linear scan of `.eh_frame` and `.debug_frame` is done to find the FDE. The latter |
| 1072 | | /// may require lazily loading the data in those sections. |
| 1073 | | /// |
| 1074 | | /// `explicit_fde_offset` is for cases where the FDE offset is known, such as when __unwind_info |
| 1075 | | /// defers unwinding to DWARF. This is an offset into the `.eh_frame` section. |
| 1076 | | fn unwindFrameDwarf( |
| 1077 | | unwind: *const Dwarf.Unwind, |
| 1078 | | load_offset: usize, |
| 1079 | | context: *UnwindContext, |
| 1080 | | explicit_fde_offset: ?usize, |
| 1081 | | ) !usize { |
| 1082 | | if (!supports_unwinding) return error.UnsupportedCpuArchitecture; |
| 1083 | | if (context.pc == 0) return 0; |
| 1084 | | |
| 1085 | | const pc_vaddr = context.pc - load_offset; |
| 1086 | 267 | |
| 1087 | | const fde_offset = explicit_fde_offset orelse try unwind.lookupPc( |
| 1088 | | pc_vaddr, |
| 1089 | | @sizeOf(usize), |
| 1090 | | native_endian, |
| 1091 | | ) orelse return error.MissingDebugInfo; |
| 1092 | | const format, const cie, const fde = try unwind.getFde(fde_offset, @sizeOf(usize), native_endian); |
| 268 | /// Unwind a stack frame using DWARF unwinding info, updating the register context. |
| 269 | /// |
| 270 | /// If `.eh_frame_hdr` is available and complete, it will be used to binary search for the FDE. |
| 271 | /// Otherwise, a linear scan of `.eh_frame` and `.debug_frame` is done to find the FDE. The latter |
| 272 | /// may require lazily loading the data in those sections. |
| 273 | /// |
| 274 | /// `explicit_fde_offset` is for cases where the FDE offset is known, such as when __unwind_info |
| 275 | /// defers unwinding to DWARF. This is an offset into the `.eh_frame` section. |
| 276 | pub fn unwindFrameDwarf( |
| 277 | context: *UnwindContext, |
| 278 | unwind: *const Dwarf.Unwind, |
| 279 | load_offset: usize, |
| 280 | explicit_fde_offset: ?usize, |
| 281 | ) !usize { |
| 282 | if (!supports_unwinding) return error.UnsupportedCpuArchitecture; |
| 283 | if (context.pc == 0) return 0; |
| 284 | |
| 285 | const pc_vaddr = context.pc - load_offset; |
| 286 | |
| 287 | const fde_offset = explicit_fde_offset orelse try unwind.lookupPc( |
| 288 | pc_vaddr, |
| 289 | @sizeOf(usize), |
| 290 | native_endian, |
| 291 | ) orelse return error.MissingDebugInfo; |
| 292 | const format, const cie, const fde = try unwind.getFde(fde_offset, @sizeOf(usize), native_endian); |
| 293 | |
| 294 | // Check if this FDE *actually* includes the address. |
| 295 | if (pc_vaddr < fde.pc_begin or pc_vaddr >= fde.pc_begin + fde.pc_range) return error.MissingDebugInfo; |
| 296 | |
| 297 | // Do not set `compile_unit` because the spec states that CFIs |
| 298 | // may not reference other debug sections anyway. |
| 299 | var expression_context: Dwarf.expression.Context = .{ |
| 300 | .format = format, |
| 301 | .thread_context = context.thread_context, |
| 302 | .reg_context = context.reg_context, |
| 303 | .cfa = context.cfa, |
| 304 | }; |
| 1093 | 305 | |
| 1094 | | // Check if this FDE *actually* includes the address. |
| 1095 | | if (pc_vaddr < fde.pc_begin or pc_vaddr >= fde.pc_begin + fde.pc_range) return error.MissingDebugInfo; |
| 306 | context.vm.reset(); |
| 307 | context.reg_context.eh_frame = cie.version != 4; |
| 308 | context.reg_context.is_macho = native_os.isDarwin(); |
| 1096 | 309 | |
| 1097 | | // Do not set `compile_unit` because the spec states that CFIs |
| 1098 | | // may not reference other debug sections anyway. |
| 1099 | | var expression_context: Dwarf.expression.Context = .{ |
| 1100 | | .format = format, |
| 1101 | | .thread_context = context.thread_context, |
| 1102 | | .reg_context = context.reg_context, |
| 1103 | | .cfa = context.cfa, |
| 1104 | | }; |
| 310 | const row = try context.vm.runTo(context.gpa, context.pc - load_offset, cie, fde, @sizeOf(usize), native_endian); |
| 311 | context.cfa = switch (row.cfa.rule) { |
| 312 | .val_offset => |offset| blk: { |
| 313 | const register = row.cfa.register orelse return error.InvalidCFARule; |
| 314 | const value = (try regValueNative(context.thread_context, register, context.reg_context)).*; |
| 315 | break :blk try applyOffset(value, offset); |
| 316 | }, |
| 317 | .expression => |expr| blk: { |
| 318 | context.stack_machine.reset(); |
| 319 | const value = try context.stack_machine.run( |
| 320 | expr, |
| 321 | context.gpa, |
| 322 | expression_context, |
| 323 | context.cfa, |
| 324 | ); |
| 1105 | 325 | |
| 1106 | | context.vm.reset(); |
| 1107 | | context.reg_context.eh_frame = cie.version != 4; |
| 1108 | | context.reg_context.is_macho = native_os.isDarwin(); |
| 326 | if (value) |v| { |
| 327 | if (v != .generic) return error.InvalidExpressionValue; |
| 328 | break :blk v.generic; |
| 329 | } else return error.NoExpressionValue; |
| 330 | }, |
| 331 | else => return error.InvalidCFARule, |
| 332 | }; |
| 1109 | 333 | |
| 1110 | | const row = try context.vm.runTo(context.gpa, context.pc - load_offset, cie, fde, @sizeOf(usize), native_endian); |
| 1111 | | context.cfa = switch (row.cfa.rule) { |
| 1112 | | .val_offset => |offset| blk: { |
| 1113 | | const register = row.cfa.register orelse return error.InvalidCFARule; |
| 1114 | | const value = mem.readInt(usize, (try regBytes(context.thread_context, register, context.reg_context))[0..@sizeOf(usize)], native_endian); |
| 1115 | | break :blk try applyOffset(value, offset); |
| 1116 | | }, |
| 1117 | | .expression => |expr| blk: { |
| 1118 | | context.stack_machine.reset(); |
| 1119 | | const value = try context.stack_machine.run( |
| 1120 | | expr, |
| 1121 | | context.gpa, |
| 1122 | | expression_context, |
| 1123 | | context.cfa, |
| 1124 | | ); |
| 334 | expression_context.cfa = context.cfa; |
| 1125 | 335 | |
| 1126 | | if (value) |v| { |
| 1127 | | if (v != .generic) return error.InvalidExpressionValue; |
| 1128 | | break :blk v.generic; |
| 1129 | | } else return error.NoExpressionValue; |
| 1130 | | }, |
| 1131 | | else => return error.InvalidCFARule, |
| 1132 | | }; |
| 336 | // Buffering the modifications is done because copying the thread context is not portable, |
| 337 | // some implementations (ie. darwin) use internal pointers to the mcontext. |
| 338 | var arena: std.heap.ArenaAllocator = .init(context.gpa); |
| 339 | defer arena.deinit(); |
| 340 | const update_arena = arena.allocator(); |
| 1133 | 341 | |
| 1134 | | expression_context.cfa = context.cfa; |
| 342 | const RegisterUpdate = struct { |
| 343 | // Backed by thread_context |
| 344 | dest: []u8, |
| 345 | // Backed by arena |
| 346 | src: []const u8, |
| 347 | prev: ?*@This(), |
| 348 | }; |
| 1135 | 349 | |
| 1136 | | // Buffering the modifications is done because copying the thread context is not portable, |
| 1137 | | // some implementations (ie. darwin) use internal pointers to the mcontext. |
| 1138 | | var arena: std.heap.ArenaAllocator = .init(context.gpa); |
| 1139 | | defer arena.deinit(); |
| 1140 | | const update_arena = arena.allocator(); |
| 350 | var update_tail: ?*RegisterUpdate = null; |
| 351 | var has_return_address = true; |
| 352 | for (context.vm.rowColumns(row)) |column| { |
| 353 | if (column.register) |register| { |
| 354 | if (register == cie.return_address_register) { |
| 355 | has_return_address = column.rule != .undefined; |
| 356 | } |
| 1141 | 357 | |
| 1142 | | const RegisterUpdate = struct { |
| 1143 | | // Backed by thread_context |
| 1144 | | dest: []u8, |
| 1145 | | // Backed by arena |
| 1146 | | src: []const u8, |
| 1147 | | prev: ?*@This(), |
| 1148 | | }; |
| 358 | const dest = try regBytes(context.thread_context, register, context.reg_context); |
| 359 | const src = try update_arena.alloc(u8, dest.len); |
| 360 | try context.resolveRegisterRule(column, expression_context, src); |
| 1149 | 361 | |
| 1150 | | var update_tail: ?*RegisterUpdate = null; |
| 1151 | | var has_return_address = true; |
| 1152 | | for (context.vm.rowColumns(row)) |column| { |
| 1153 | | if (column.register) |register| { |
| 1154 | | if (register == cie.return_address_register) { |
| 1155 | | has_return_address = column.rule != .undefined; |
| 362 | const new_update = try update_arena.create(RegisterUpdate); |
| 363 | new_update.* = .{ |
| 364 | .dest = dest, |
| 365 | .src = src, |
| 366 | .prev = update_tail, |
| 367 | }; |
| 368 | update_tail = new_update; |
| 1156 | 369 | } |
| 370 | } |
| 1157 | 371 | |
| 1158 | | const dest = try regBytes(context.thread_context, register, context.reg_context); |
| 1159 | | const src = try update_arena.alloc(u8, dest.len); |
| 1160 | | try context.resolveRegisterRule(column, expression_context, src); |
| 372 | // On all implemented architectures, the CFA is defined as being the previous frame's SP |
| 373 | (try regValueNative(context.thread_context, Dwarf.abi.spRegNum(native_arch, context.reg_context), context.reg_context)).* = context.cfa.?; |
| 1161 | 374 | |
| 1162 | | const new_update = try update_arena.create(RegisterUpdate); |
| 1163 | | new_update.* = .{ |
| 1164 | | .dest = dest, |
| 1165 | | .src = src, |
| 1166 | | .prev = update_tail, |
| 1167 | | }; |
| 1168 | | update_tail = new_update; |
| 375 | while (update_tail) |tail| { |
| 376 | @memcpy(tail.dest, tail.src); |
| 377 | update_tail = tail.prev; |
| 1169 | 378 | } |
| 1170 | | } |
| 1171 | 379 | |
| 1172 | | // On all implemented architectures, the CFA is defined as being the previous frame's SP |
| 1173 | | (try regValueNative(context.thread_context, spRegNum(context.reg_context), context.reg_context)).* = context.cfa.?; |
| 380 | if (has_return_address) { |
| 381 | context.pc = stripInstructionPtrAuthCode((try regValueNative( |
| 382 | context.thread_context, |
| 383 | cie.return_address_register, |
| 384 | context.reg_context, |
| 385 | )).*); |
| 386 | } else { |
| 387 | context.pc = 0; |
| 388 | } |
| 1174 | 389 | |
| 1175 | | while (update_tail) |tail| { |
| 1176 | | @memcpy(tail.dest, tail.src); |
| 1177 | | update_tail = tail.prev; |
| 390 | const ip_reg_num = Dwarf.abi.ipRegNum(native_arch).?; |
| 391 | (try regValueNative(context.thread_context, ip_reg_num, context.reg_context)).* = context.pc; |
| 392 | |
| 393 | // The call instruction will have pushed the address of the instruction that follows the call as the return address. |
| 394 | // This next instruction may be past the end of the function if the caller was `noreturn` (ie. the last instruction in |
| 395 | // the function was the call). If we were to look up an FDE entry using the return address directly, it could end up |
| 396 | // either not finding an FDE at all, or using the next FDE in the program, producing incorrect results. To prevent this, |
| 397 | // we subtract one so that the next lookup is guaranteed to land inside the |
| 398 | // |
| 399 | // The exception to this rule is signal frames, where we return execution would be returned to the instruction |
| 400 | // that triggered the handler. |
| 401 | const return_address = context.pc; |
| 402 | if (context.pc > 0 and !cie.is_signal_frame) context.pc -= 1; |
| 403 | |
| 404 | return return_address; |
| 1178 | 405 | } |
| 1179 | | |
| 1180 | | if (has_return_address) { |
| 1181 | | context.pc = stripInstructionPtrAuthCode(mem.readInt(usize, (try regBytes( |
| 1182 | | context.thread_context, |
| 1183 | | cie.return_address_register, |
| 1184 | | context.reg_context, |
| 1185 | | ))[0..@sizeOf(usize)], native_endian)); |
| 1186 | | } else { |
| 1187 | | context.pc = 0; |
| 406 | /// Since register rules are applied (usually) during a panic, |
| 407 | /// checked addition / subtraction is used so that we can return |
| 408 | /// an error and fall back to FP-based unwinding. |
| 409 | fn applyOffset(base: usize, offset: i64) !usize { |
| 410 | return if (offset >= 0) |
| 411 | try std.math.add(usize, base, @as(usize, @intCast(offset))) |
| 412 | else |
| 413 | try std.math.sub(usize, base, @as(usize, @intCast(-offset))); |
| 1188 | 414 | } |
| 1189 | | |
| 1190 | | (try regValueNative(context.thread_context, ip_reg_num, context.reg_context)).* = context.pc; |
| 1191 | | |
| 1192 | | // The call instruction will have pushed the address of the instruction that follows the call as the return address. |
| 1193 | | // This next instruction may be past the end of the function if the caller was `noreturn` (ie. the last instruction in |
| 1194 | | // the function was the call). If we were to look up an FDE entry using the return address directly, it could end up |
| 1195 | | // either not finding an FDE at all, or using the next FDE in the program, producing incorrect results. To prevent this, |
| 1196 | | // we subtract one so that the next lookup is guaranteed to land inside the |
| 1197 | | // |
| 1198 | | // The exception to this rule is signal frames, where we return execution would be returned to the instruction |
| 1199 | | // that triggered the handler. |
| 1200 | | const return_address = context.pc; |
| 1201 | | if (context.pc > 0 and !cie.is_signal_frame) context.pc -= 1; |
| 1202 | | |
| 1203 | | return return_address; |
| 1204 | | } |
| 1205 | | |
| 1206 | | fn fpRegNum(reg_context: Dwarf.abi.RegisterContext) u8 { |
| 1207 | | return Dwarf.abi.fpRegNum(native_arch, reg_context); |
| 1208 | | } |
| 1209 | | |
| 1210 | | fn spRegNum(reg_context: Dwarf.abi.RegisterContext) u8 { |
| 1211 | | return Dwarf.abi.spRegNum(native_arch, reg_context); |
| 1212 | | } |
| 1213 | | |
| 1214 | | const ip_reg_num = Dwarf.abi.ipRegNum(native_arch).?; |
| 1215 | | |
| 1216 | | /// Tells whether unwinding for the host is implemented. |
| 1217 | | pub const supports_unwinding = supportsUnwinding(&builtin.target); |
| 1218 | | |
| 1219 | | comptime { |
| 1220 | | if (supports_unwinding) assert(Dwarf.abi.supportsUnwinding(&builtin.target)); |
| 1221 | | } |
| 1222 | | |
| 1223 | | /// Tells whether unwinding for this target is *implemented* here in the Zig |
| 1224 | | /// standard library. |
| 1225 | | /// |
| 1226 | | /// See also `Dwarf.abi.supportsUnwinding` which tells whether Dwarf supports |
| 1227 | | /// unwinding on that target *in theory*. |
| 1228 | | pub fn supportsUnwinding(target: *const std.Target) bool { |
| 1229 | | return switch (target.cpu.arch) { |
| 1230 | | .x86 => switch (target.os.tag) { |
| 1231 | | .linux, .netbsd, .solaris, .illumos => true, |
| 1232 | | else => false, |
| 1233 | | }, |
| 1234 | | .x86_64 => switch (target.os.tag) { |
| 1235 | | .linux, .netbsd, .freebsd, .openbsd, .macos, .ios, .solaris, .illumos => true, |
| 1236 | | else => false, |
| 1237 | | }, |
| 1238 | | .arm, .armeb, .thumb, .thumbeb => switch (target.os.tag) { |
| 1239 | | .linux => true, |
| 1240 | | else => false, |
| 1241 | | }, |
| 1242 | | .aarch64, .aarch64_be => switch (target.os.tag) { |
| 1243 | | .linux, .netbsd, .freebsd, .macos, .ios => true, |
| 1244 | | else => false, |
| 1245 | | }, |
| 1246 | | // Unwinding is possible on other targets but this implementation does |
| 1247 | | // not support them...yet! |
| 1248 | | else => false, |
| 1249 | | }; |
| 1250 | | } |
| 1251 | | |
| 1252 | | /// Since register rules are applied (usually) during a panic, |
| 1253 | | /// checked addition / subtraction is used so that we can return |
| 1254 | | /// an error and fall back to FP-based unwinding. |
| 1255 | | fn applyOffset(base: usize, offset: i64) !usize { |
| 1256 | | return if (offset >= 0) |
| 1257 | | try std.math.add(usize, base, @as(usize, @intCast(offset))) |
| 1258 | | else |
| 1259 | | try std.math.sub(usize, base, @as(usize, @intCast(-offset))); |
| 1260 | | } |
| 1261 | | |
| 1262 | | /// Uses `mmap` to map the file at `opt_path` (or, if `null`, the self executable image) into memory. |
| 1263 | | fn mapDebugInfoFile(opt_path: ?[]const u8) ![]align(std.heap.page_size_min) const u8 { |
| 1264 | | const open_result = if (opt_path) |path| |
| 1265 | | fs.cwd().openFile(path, .{}) |
| 1266 | | else |
| 1267 | | fs.openSelfExe(.{}); |
| 1268 | | const file = open_result catch |err| switch (err) { |
| 1269 | | error.FileNotFound => return error.MissingDebugInfo, |
| 1270 | | else => |e| return e, |
| 1271 | | }; |
| 1272 | | defer file.close(); |
| 1273 | | |
| 1274 | | const file_len = math.cast(usize, try file.getEndPos()) orelse return error.InvalidDebugInfo; |
| 1275 | | |
| 1276 | | return posix.mmap( |
| 1277 | | null, |
| 1278 | | file_len, |
| 1279 | | posix.PROT.READ, |
| 1280 | | .{ .TYPE = .SHARED }, |
| 1281 | | file.handle, |
| 1282 | | 0, |
| 1283 | | ); |
| 1284 | | } |
| 1285 | | |
| 1286 | | /// Unwind a frame using MachO compact unwind info (from __unwind_info). |
| 1287 | | /// If the compact encoding can't encode a way to unwind a frame, it will |
| 1288 | | /// defer unwinding to DWARF, in which case `.eh_frame` will be used if available. |
| 1289 | | fn unwindFrameMachO( |
| 1290 | | text_base: usize, |
| 1291 | | load_offset: usize, |
| 1292 | | context: *UnwindContext, |
| 1293 | | unwind_info: []const u8, |
| 1294 | | opt_eh_frame: ?[]const u8, |
| 1295 | | ) !usize { |
| 1296 | | if (unwind_info.len < @sizeOf(macho.unwind_info_section_header)) return error.InvalidUnwindInfo; |
| 1297 | | const header: *align(1) const macho.unwind_info_section_header = @ptrCast(unwind_info); |
| 1298 | | |
| 1299 | | const index_byte_count = header.indexCount * @sizeOf(macho.unwind_info_section_header_index_entry); |
| 1300 | | if (unwind_info.len < header.indexSectionOffset + index_byte_count) return error.InvalidUnwindInfo; |
| 1301 | | const indices: []align(1) const macho.unwind_info_section_header_index_entry = @ptrCast(unwind_info[header.indexSectionOffset..][0..index_byte_count]); |
| 1302 | | if (indices.len == 0) return error.MissingUnwindInfo; |
| 1303 | | |
| 1304 | | // offset of the PC into the `__TEXT` segment |
| 1305 | | const pc_text_offset = context.pc - text_base; |
| 1306 | | |
| 1307 | | const start_offset: u32, const first_level_offset: u32 = index: { |
| 1308 | | var left: usize = 0; |
| 1309 | | var len: usize = indices.len; |
| 1310 | | while (len > 1) { |
| 1311 | | const mid = left + len / 2; |
| 1312 | | if (pc_text_offset < indices[mid].functionOffset) { |
| 1313 | | len /= 2; |
| 1314 | | } else { |
| 1315 | | left = mid; |
| 1316 | | len -= len / 2; |
| 1317 | | } |
| 415 | /// Some platforms use pointer authentication - the upper bits of instruction pointers contain a signature. |
| 416 | /// This function clears these signature bits to make the pointer usable. |
| 417 | pub inline fn stripInstructionPtrAuthCode(ptr: usize) usize { |
| 418 | if (native_arch.isAARCH64()) { |
| 419 | // `hint 0x07` maps to `xpaclri` (or `nop` if the hardware doesn't support it) |
| 420 | // The save / restore is because `xpaclri` operates on x30 (LR) |
| 421 | return asm ( |
| 422 | \\mov x16, x30 |
| 423 | \\mov x30, x15 |
| 424 | \\hint 0x07 |
| 425 | \\mov x15, x30 |
| 426 | \\mov x30, x16 |
| 427 | : [ret] "={x15}" (-> usize), |
| 428 | : [ptr] "{x15}" (ptr), |
| 429 | : .{ .x16 = true }); |
| 1318 | 430 | } |
| 1319 | | break :index .{ indices[left].secondLevelPagesSectionOffset, indices[left].functionOffset }; |
| 1320 | | }; |
| 1321 | | // An offset of 0 is a sentinel indicating a range does not have unwind info. |
| 1322 | | if (start_offset == 0) return error.MissingUnwindInfo; |
| 1323 | | |
| 1324 | | const common_encodings_byte_count = header.commonEncodingsArrayCount * @sizeOf(macho.compact_unwind_encoding_t); |
| 1325 | | if (unwind_info.len < header.commonEncodingsArraySectionOffset + common_encodings_byte_count) return error.InvalidUnwindInfo; |
| 1326 | | const common_encodings: []align(1) const macho.compact_unwind_encoding_t = @ptrCast( |
| 1327 | | unwind_info[header.commonEncodingsArraySectionOffset..][0..common_encodings_byte_count], |
| 1328 | | ); |
| 1329 | | |
| 1330 | | if (unwind_info.len < start_offset + @sizeOf(macho.UNWIND_SECOND_LEVEL)) return error.InvalidUnwindInfo; |
| 1331 | | const kind: *align(1) const macho.UNWIND_SECOND_LEVEL = @ptrCast(unwind_info[start_offset..]); |
| 1332 | | |
| 1333 | | const entry: struct { |
| 1334 | | function_offset: usize, |
| 1335 | | raw_encoding: u32, |
| 1336 | | } = switch (kind.*) { |
| 1337 | | .REGULAR => entry: { |
| 1338 | | if (unwind_info.len < start_offset + @sizeOf(macho.unwind_info_regular_second_level_page_header)) return error.InvalidUnwindInfo; |
| 1339 | | const page_header: *align(1) const macho.unwind_info_regular_second_level_page_header = @ptrCast(unwind_info[start_offset..]); |
| 1340 | | |
| 1341 | | const entries_byte_count = page_header.entryCount * @sizeOf(macho.unwind_info_regular_second_level_entry); |
| 1342 | | if (unwind_info.len < start_offset + entries_byte_count) return error.InvalidUnwindInfo; |
| 1343 | | const entries: []align(1) const macho.unwind_info_regular_second_level_entry = @ptrCast( |
| 1344 | | unwind_info[start_offset + page_header.entryPageOffset ..][0..entries_byte_count], |
| 1345 | | ); |
| 1346 | | if (entries.len == 0) return error.InvalidUnwindInfo; |
| 1347 | | |
| 1348 | | var left: usize = 0; |
| 1349 | | var len: usize = entries.len; |
| 1350 | | while (len > 1) { |
| 1351 | | const mid = left + len / 2; |
| 1352 | | if (pc_text_offset < entries[mid].functionOffset) { |
| 1353 | | len /= 2; |
| 1354 | | } else { |
| 1355 | | left = mid; |
| 1356 | | len -= len / 2; |
| 1357 | | } |
| 1358 | | } |
| 1359 | | break :entry .{ |
| 1360 | | .function_offset = entries[left].functionOffset, |
| 1361 | | .raw_encoding = entries[left].encoding, |
| 1362 | | }; |
| 1363 | | }, |
| 1364 | | .COMPRESSED => entry: { |
| 1365 | | if (unwind_info.len < start_offset + @sizeOf(macho.unwind_info_compressed_second_level_page_header)) return error.InvalidUnwindInfo; |
| 1366 | | const page_header: *align(1) const macho.unwind_info_compressed_second_level_page_header = @ptrCast(unwind_info[start_offset..]); |
| 1367 | | |
| 1368 | | const entries_byte_count = page_header.entryCount * @sizeOf(macho.UnwindInfoCompressedEntry); |
| 1369 | | if (unwind_info.len < start_offset + entries_byte_count) return error.InvalidUnwindInfo; |
| 1370 | | const entries: []align(1) const macho.UnwindInfoCompressedEntry = @ptrCast( |
| 1371 | | unwind_info[start_offset + page_header.entryPageOffset ..][0..entries_byte_count], |
| 1372 | | ); |
| 1373 | | if (entries.len == 0) return error.InvalidUnwindInfo; |
| 1374 | | |
| 1375 | | var left: usize = 0; |
| 1376 | | var len: usize = entries.len; |
| 1377 | | while (len > 1) { |
| 1378 | | const mid = left + len / 2; |
| 1379 | | if (pc_text_offset < first_level_offset + entries[mid].funcOffset) { |
| 1380 | | len /= 2; |
| 1381 | | } else { |
| 1382 | | left = mid; |
| 1383 | | len -= len / 2; |
| 1384 | | } |
| 1385 | | } |
| 1386 | | const entry = entries[left]; |
| 1387 | | |
| 1388 | | const function_offset = first_level_offset + entry.funcOffset; |
| 1389 | | if (entry.encodingIndex < common_encodings.len) { |
| 1390 | | break :entry .{ |
| 1391 | | .function_offset = function_offset, |
| 1392 | | .raw_encoding = common_encodings[entry.encodingIndex], |
| 1393 | | }; |
| 1394 | | } |
| 1395 | | |
| 1396 | | const local_index = entry.encodingIndex - common_encodings.len; |
| 1397 | | const local_encodings_byte_count = page_header.encodingsCount * @sizeOf(macho.compact_unwind_encoding_t); |
| 1398 | | if (unwind_info.len < start_offset + page_header.encodingsPageOffset + local_encodings_byte_count) return error.InvalidUnwindInfo; |
| 1399 | | const local_encodings: []align(1) const macho.compact_unwind_encoding_t = @ptrCast( |
| 1400 | | unwind_info[start_offset + page_header.encodingsPageOffset ..][0..local_encodings_byte_count], |
| 1401 | | ); |
| 1402 | | if (local_index >= local_encodings.len) return error.InvalidUnwindInfo; |
| 1403 | | break :entry .{ |
| 1404 | | .function_offset = function_offset, |
| 1405 | | .raw_encoding = local_encodings[local_index], |
| 1406 | | }; |
| 1407 | | }, |
| 1408 | | else => return error.InvalidUnwindInfo, |
| 1409 | | }; |
| 1410 | | |
| 1411 | | if (entry.raw_encoding == 0) return error.NoUnwindInfo; |
| 1412 | | const reg_context: Dwarf.abi.RegisterContext = .{ .eh_frame = false, .is_macho = true }; |
| 1413 | 431 | |
| 1414 | | const encoding: macho.CompactUnwindEncoding = @bitCast(entry.raw_encoding); |
| 1415 | | const new_ip = switch (builtin.cpu.arch) { |
| 1416 | | .x86_64 => switch (encoding.mode.x86_64) { |
| 1417 | | .OLD => return error.UnimplementedUnwindEncoding, |
| 1418 | | .RBP_FRAME => ip: { |
| 1419 | | const frame = encoding.value.x86_64.frame; |
| 1420 | | |
| 1421 | | const fp = (try regValueNative(context.thread_context, fpRegNum(reg_context), reg_context)).*; |
| 1422 | | const new_sp = fp + 2 * @sizeOf(usize); |
| 1423 | | |
| 1424 | | const ip_ptr = fp + @sizeOf(usize); |
| 1425 | | const new_ip = @as(*const usize, @ptrFromInt(ip_ptr)).*; |
| 1426 | | const new_fp = @as(*const usize, @ptrFromInt(fp)).*; |
| 1427 | | |
| 1428 | | (try regValueNative(context.thread_context, fpRegNum(reg_context), reg_context)).* = new_fp; |
| 1429 | | (try regValueNative(context.thread_context, spRegNum(reg_context), reg_context)).* = new_sp; |
| 1430 | | (try regValueNative(context.thread_context, ip_reg_num, reg_context)).* = new_ip; |
| 1431 | | |
| 1432 | | const regs: [5]u3 = .{ |
| 1433 | | frame.reg0, |
| 1434 | | frame.reg1, |
| 1435 | | frame.reg2, |
| 1436 | | frame.reg3, |
| 1437 | | frame.reg4, |
| 1438 | | }; |
| 1439 | | for (regs, 0..) |reg, i| { |
| 1440 | | if (reg == 0) continue; |
| 1441 | | const addr = fp - frame.frame_offset * @sizeOf(usize) + i * @sizeOf(usize); |
| 1442 | | const reg_number = try Dwarf.compactUnwindToDwarfRegNumber(reg); |
| 1443 | | (try regValueNative(context.thread_context, reg_number, reg_context)).* = @as(*const usize, @ptrFromInt(addr)).*; |
| 1444 | | } |
| 1445 | | |
| 1446 | | break :ip new_ip; |
| 1447 | | }, |
| 1448 | | .STACK_IMMD, |
| 1449 | | .STACK_IND, |
| 1450 | | => ip: { |
| 1451 | | const frameless = encoding.value.x86_64.frameless; |
| 1452 | | |
| 1453 | | const sp = (try regValueNative(context.thread_context, spRegNum(reg_context), reg_context)).*; |
| 1454 | | const stack_size: usize = stack_size: { |
| 1455 | | if (encoding.mode.x86_64 == .STACK_IMMD) { |
| 1456 | | break :stack_size @as(usize, frameless.stack.direct.stack_size) * @sizeOf(usize); |
| 1457 | | } |
| 1458 | | // In .STACK_IND, the stack size is inferred from the subq instruction at the beginning of the function. |
| 1459 | | const sub_offset_addr = |
| 1460 | | text_base + |
| 1461 | | entry.function_offset + |
| 1462 | | frameless.stack.indirect.sub_offset; |
| 1463 | | // `sub_offset_addr` points to the offset of the literal within the instruction |
| 1464 | | const sub_operand = @as(*align(1) const u32, @ptrFromInt(sub_offset_addr)).*; |
| 1465 | | break :stack_size sub_operand + @sizeOf(usize) * @as(usize, frameless.stack.indirect.stack_adjust); |
| 1466 | | }; |
| 1467 | | |
| 1468 | | // Decode the Lehmer-coded sequence of registers. |
| 1469 | | // For a description of the encoding see lib/libc/include/any-macos.13-any/mach-o/compact_unwind_encoding.h |
| 1470 | | |
| 1471 | | // Decode the variable-based permutation number into its digits. Each digit represents |
| 1472 | | // an index into the list of register numbers that weren't yet used in the sequence at |
| 1473 | | // the time the digit was added. |
| 1474 | | const reg_count = frameless.stack_reg_count; |
| 1475 | | const ip_ptr = ip_ptr: { |
| 1476 | | var digits: [6]u3 = undefined; |
| 1477 | | var accumulator: usize = frameless.stack_reg_permutation; |
| 1478 | | var base: usize = 2; |
| 1479 | | for (0..reg_count) |i| { |
| 1480 | | const div = accumulator / base; |
| 1481 | | digits[digits.len - 1 - i] = @intCast(accumulator - base * div); |
| 1482 | | accumulator = div; |
| 1483 | | base += 1; |
| 1484 | | } |
| 1485 | | |
| 1486 | | var registers: [6]u3 = undefined; |
| 1487 | | var used_indices: [6]bool = @splat(false); |
| 1488 | | for (digits[digits.len - reg_count ..], 0..) |target_unused_index, i| { |
| 1489 | | var unused_count: u8 = 0; |
| 1490 | | const unused_index = for (used_indices, 0..) |used, index| { |
| 1491 | | if (!used) { |
| 1492 | | if (target_unused_index == unused_count) break index; |
| 1493 | | unused_count += 1; |
| 1494 | | } |
| 1495 | | } else unreachable; |
| 1496 | | registers[i] = @intCast(unused_index + 1); |
| 1497 | | used_indices[unused_index] = true; |
| 1498 | | } |
| 1499 | | |
| 1500 | | var reg_addr = sp + stack_size - @sizeOf(usize) * @as(usize, reg_count + 1); |
| 1501 | | for (0..reg_count) |i| { |
| 1502 | | const reg_number = try Dwarf.compactUnwindToDwarfRegNumber(registers[i]); |
| 1503 | | (try regValueNative(context.thread_context, reg_number, reg_context)).* = @as(*const usize, @ptrFromInt(reg_addr)).*; |
| 1504 | | reg_addr += @sizeOf(usize); |
| 1505 | | } |
| 1506 | | |
| 1507 | | break :ip_ptr reg_addr; |
| 1508 | | }; |
| 1509 | | |
| 1510 | | const new_ip = @as(*const usize, @ptrFromInt(ip_ptr)).*; |
| 1511 | | const new_sp = ip_ptr + @sizeOf(usize); |
| 1512 | | |
| 1513 | | (try regValueNative(context.thread_context, spRegNum(reg_context), reg_context)).* = new_sp; |
| 1514 | | (try regValueNative(context.thread_context, ip_reg_num, reg_context)).* = new_ip; |
| 1515 | | |
| 1516 | | break :ip new_ip; |
| 1517 | | }, |
| 1518 | | .DWARF => { |
| 1519 | | const eh_frame = opt_eh_frame orelse return error.MissingEhFrame; |
| 1520 | | const eh_frame_vaddr = @intFromPtr(eh_frame.ptr) - load_offset; |
| 1521 | | return unwindFrameDwarf( |
| 1522 | | &.initSection(.eh_frame, eh_frame_vaddr, eh_frame), |
| 1523 | | load_offset, |
| 1524 | | context, |
| 1525 | | @intCast(encoding.value.x86_64.dwarf), |
| 1526 | | ); |
| 1527 | | }, |
| 1528 | | }, |
| 1529 | | .aarch64, .aarch64_be => switch (encoding.mode.arm64) { |
| 1530 | | .OLD => return error.UnimplementedUnwindEncoding, |
| 1531 | | .FRAMELESS => ip: { |
| 1532 | | const sp = (try regValueNative(context.thread_context, spRegNum(reg_context), reg_context)).*; |
| 1533 | | const new_sp = sp + encoding.value.arm64.frameless.stack_size * 16; |
| 1534 | | const new_ip = (try regValueNative(context.thread_context, 30, reg_context)).*; |
| 1535 | | (try regValueNative(context.thread_context, spRegNum(reg_context), reg_context)).* = new_sp; |
| 1536 | | break :ip new_ip; |
| 1537 | | }, |
| 1538 | | .DWARF => { |
| 1539 | | const eh_frame = opt_eh_frame orelse return error.MissingEhFrame; |
| 1540 | | const eh_frame_vaddr = @intFromPtr(eh_frame.ptr) - load_offset; |
| 1541 | | return unwindFrameDwarf( |
| 1542 | | &.initSection(.eh_frame, eh_frame_vaddr, eh_frame), |
| 1543 | | load_offset, |
| 1544 | | context, |
| 1545 | | @intCast(encoding.value.x86_64.dwarf), |
| 1546 | | ); |
| 1547 | | }, |
| 1548 | | .FRAME => ip: { |
| 1549 | | const frame = encoding.value.arm64.frame; |
| 1550 | | |
| 1551 | | const fp = (try regValueNative(context.thread_context, fpRegNum(reg_context), reg_context)).*; |
| 1552 | | const ip_ptr = fp + @sizeOf(usize); |
| 1553 | | |
| 1554 | | var reg_addr = fp - @sizeOf(usize); |
| 1555 | | inline for (@typeInfo(@TypeOf(frame.x_reg_pairs)).@"struct".fields, 0..) |field, i| { |
| 1556 | | if (@field(frame.x_reg_pairs, field.name) != 0) { |
| 1557 | | (try regValueNative(context.thread_context, 19 + i, reg_context)).* = @as(*const usize, @ptrFromInt(reg_addr)).*; |
| 1558 | | reg_addr += @sizeOf(usize); |
| 1559 | | (try regValueNative(context.thread_context, 20 + i, reg_context)).* = @as(*const usize, @ptrFromInt(reg_addr)).*; |
| 1560 | | reg_addr += @sizeOf(usize); |
| 1561 | | } |
| 1562 | | } |
| 1563 | | |
| 1564 | | inline for (@typeInfo(@TypeOf(frame.d_reg_pairs)).@"struct".fields, 0..) |field, i| { |
| 1565 | | if (@field(frame.d_reg_pairs, field.name) != 0) { |
| 1566 | | // Only the lower half of the 128-bit V registers are restored during unwinding |
| 1567 | | { |
| 1568 | | const dest: *align(1) usize = @ptrCast(try regBytes(context.thread_context, 64 + 8 + i, context.reg_context)); |
| 1569 | | dest.* = @as(*const usize, @ptrFromInt(reg_addr)).*; |
| 1570 | | } |
| 1571 | | reg_addr += @sizeOf(usize); |
| 1572 | | { |
| 1573 | | const dest: *align(1) usize = @ptrCast(try regBytes(context.thread_context, 64 + 9 + i, context.reg_context)); |
| 1574 | | dest.* = @as(*const usize, @ptrFromInt(reg_addr)).*; |
| 1575 | | } |
| 1576 | | reg_addr += @sizeOf(usize); |
| 1577 | | } |
| 1578 | | } |
| 1579 | | |
| 1580 | | const new_ip = @as(*const usize, @ptrFromInt(ip_ptr)).*; |
| 1581 | | const new_fp = @as(*const usize, @ptrFromInt(fp)).*; |
| 1582 | | |
| 1583 | | (try regValueNative(context.thread_context, fpRegNum(reg_context), reg_context)).* = new_fp; |
| 1584 | | (try regValueNative(context.thread_context, ip_reg_num, reg_context)).* = new_ip; |
| 1585 | | |
| 1586 | | break :ip new_ip; |
| 1587 | | }, |
| 1588 | | }, |
| 1589 | | else => comptime unreachable, // unimplemented |
| 1590 | | }; |
| 1591 | | |
| 1592 | | context.pc = stripInstructionPtrAuthCode(new_ip); |
| 1593 | | if (context.pc > 0) context.pc -= 1; |
| 1594 | | return new_ip; |
| 1595 | | } |
| 432 | return ptr; |
| 433 | } |
| 434 | }; |