authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-09-17 23:03:45+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-09-30 13:44:54+01:00
logdd8d59686a069fdb72d9f0753e3482ff99cce98c
tree0a90991d41626bb9400a1d6cf956d10b99ed3a6c
parenta18fd41064493e742eacebc88e2afeadd54ff6f0
signaturelock-open Commit is signed but in an unrecognized format.

std.debug: miscellaneous fixes

Mostly on macOS, since Loris showed me a not-great stack trace, and I spent 8 hours trying to make it better. The dyld shared cache is designed in a way which makes this really hard to do right, and documentation is non-existent, but this *seems* to work pretty well. I'll leave the ruling on whether I did a good job to CI and our users.

8 files changed, 276 insertions(+), 132 deletions(-)

lib/std/c.zig+3
......@@ -10994,6 +10994,9 @@ pub extern "c" fn dlclose(handle: *anyopaque) c_int;
1099410994pub extern "c" fn dlsym(handle: ?*anyopaque, symbol: [*:0]const u8) ?*anyopaque;
1099510995pub extern "c" fn dlerror() ?[*:0]u8;
1099610996
10997pub const dladdr = if (native_os.isDarwin()) darwin.dladdr else {};
10998pub const dl_info = if (native_os.isDarwin()) darwin.dl_info else {};
10999
1099711000pub extern "c" fn sync() void;
1099811001pub extern "c" fn syncfs(fd: c_int) c_int;
1099911002pub extern "c" fn fsync(fd: c_int) c_int;
lib/std/c/darwin.zig+8
......@@ -354,6 +354,14 @@ pub extern "c" fn _dyld_image_count() u32;
354354pub extern "c" fn _dyld_get_image_header(image_index: u32) ?*mach_header;
355355pub extern "c" fn _dyld_get_image_vmaddr_slide(image_index: u32) usize;
356356pub extern "c" fn _dyld_get_image_name(image_index: u32) [*:0]const u8;
357pub extern "c" fn dladdr(addr: *const anyopaque, info: *dl_info) c_int;
358
359pub const dl_info = extern struct {
360 fname: [*:0]const u8,
361 fbase: *anyopaque,
362 sname: ?[*:0]const u8,
363 saddr: ?*anyopaque,
364};
357365
358366pub const COPYFILE = packed struct(u32) {
359367 ACL: bool = false,
lib/std/debug.zig+55-20
......@@ -585,12 +585,14 @@ pub fn captureCurrentStackTrace(options: StackUnwindOptions, addr_buf: []usize)
585585 while (true) switch (it.next()) {
586586 .switch_to_fp => if (!it.stratOk(options.allow_unsafe_unwind)) break,
587587 .end => break,
588 .frame => |return_address| {
588 .frame => |pc_addr| {
589589 if (wait_for) |target| {
590 if (return_address != target) continue;
590 // Possible off-by-one error: `pc_addr` might be one less than the return address (so
591 // that it falls *inside* the function call), while `target` *is* a return address.
592 if (pc_addr != target and pc_addr + 1 != target) continue;
591593 wait_for = null;
592594 }
593 if (frame_idx < addr_buf.len) addr_buf[frame_idx] = return_address;
595 if (frame_idx < addr_buf.len) addr_buf[frame_idx] = pc_addr;
594596 frame_idx += 1;
595597 },
596598 };
......@@ -631,6 +633,7 @@ pub fn writeCurrentStackTrace(options: StackUnwindOptions, writer: *Writer, tty_
631633 var printed_any_frame = false;
632634 while (true) switch (it.next()) {
633635 .switch_to_fp => |unwind_error| {
636 if (StackIterator.fp_unwind_is_safe) continue; // no need to even warn
634637 const module_name = di.getModuleNameForAddress(di_gpa, unwind_error.address) catch "???";
635638 const caption: []const u8 = switch (unwind_error.err) {
636639 error.MissingDebugInfo => "unwind info unavailable",
......@@ -658,12 +661,14 @@ pub fn writeCurrentStackTrace(options: StackUnwindOptions, writer: *Writer, tty_
658661 }
659662 },
660663 .end => break,
661 .frame => |return_address| {
664 .frame => |pc_addr| {
662665 if (wait_for) |target| {
663 if (return_address != target) continue;
666 // Possible off-by-one error: `pc_addr` might be one less than the return address (so
667 // that it falls *inside* the function call), while `target` *is* a return address.
668 if (pc_addr != target and pc_addr + 1 != target) continue;
664669 wait_for = null;
665670 }
666 try printSourceAtAddress(di_gpa, di, writer, return_address -| 1, tty_config);
671 try printSourceAtAddress(di_gpa, di, writer, pc_addr, tty_config);
667672 printed_any_frame = true;
668673 },
669674 };
......@@ -703,8 +708,8 @@ pub fn writeStackTrace(st: *const std.builtin.StackTrace, writer: *Writer, tty_c
703708 },
704709 };
705710 const captured_frames = @min(n_frames, st.instruction_addresses.len);
706 for (st.instruction_addresses[0..captured_frames]) |return_address| {
707 try printSourceAtAddress(di_gpa, di, writer, return_address -| 1, tty_config);
711 for (st.instruction_addresses[0..captured_frames]) |pc_addr| {
712 try printSourceAtAddress(di_gpa, di, writer, pc_addr, tty_config);
708713 }
709714 if (n_frames > captured_frames) {
710715 tty_config.setColor(writer, .bold) catch {};
......@@ -725,6 +730,8 @@ pub fn dumpStackTrace(st: *const std.builtin.StackTrace) void {
725730const StackIterator = union(enum) {
726731 /// Unwinding using debug info (e.g. DWARF CFI).
727732 di: if (SelfInfo.supports_unwinding) SelfInfo.UnwindContext else noreturn,
733 /// We will first report the *current* PC of this `UnwindContext`, then we will switch to `di`.
734 di_first: if (SelfInfo.supports_unwinding) SelfInfo.UnwindContext else noreturn,
728735 /// Naive frame-pointer-based unwinding. Very simple, but typically unreliable.
729736 fp: usize,
730737
......@@ -742,9 +749,12 @@ const StackIterator = union(enum) {
742749 }
743750 if (opt_context_ptr) |context_ptr| {
744751 if (!SelfInfo.supports_unwinding) return error.CannotUnwindFromContext;
745 return .{ .di = .init(context_ptr) };
752 // Use `di_first` here so we report the PC in the context before unwinding any further.
753 return .{ .di_first = .init(context_ptr) };
746754 }
747755 if (SelfInfo.supports_unwinding and cpu_context.Native != noreturn) {
756 // We don't need `di_first` here, because our PC is in `std.debug`; we're only interested
757 // in our caller's frame and above.
748758 return .{ .di = .init(&.current()) };
749759 }
750760 return .{ .fp = @frameAddress() };
......@@ -752,7 +762,7 @@ const StackIterator = union(enum) {
752762 fn deinit(si: *StackIterator) void {
753763 switch (si.*) {
754764 .fp => {},
755 .di => |*unwind_context| unwind_context.deinit(getDebugInfoAllocator()),
765 .di, .di_first => |*unwind_context| unwind_context.deinit(getDebugInfoAllocator()),
756766 }
757767 }
758768
......@@ -763,7 +773,7 @@ const StackIterator = union(enum) {
763773 /// Whether the current unwind strategy is allowed given `allow_unsafe`.
764774 fn stratOk(it: *const StackIterator, allow_unsafe: bool) bool {
765775 return switch (it.*) {
766 .di => true,
776 .di, .di_first => true,
767777 // If we omitted frame pointers from *this* compilation, FP unwinding would crash
768778 // immediately regardless of anything. But FPs could also be omitted from a different
769779 // linked object, so it's not guaranteed to be safe, unless the target specifically
......@@ -773,11 +783,11 @@ const StackIterator = union(enum) {
773783 }
774784
775785 const Result = union(enum) {
776 /// A stack frame has been found; this is the corresponding return address.
786 /// A stack frame has been found; this is the corresponding program counter address.
777787 frame: usize,
778788 /// The end of the stack has been reached.
779789 end,
780 /// We were using the `.di` strategy, but are now switching to `.fp` due to this error.
790 /// We were using `SelfInfo.UnwindInfo`, but are now switching to FP unwinding due to this error.
781791 switch_to_fp: struct {
782792 address: usize,
783793 err: SelfInfo.Error,
......@@ -785,20 +795,25 @@ const StackIterator = union(enum) {
785795 };
786796 fn next(it: *StackIterator) Result {
787797 switch (it.*) {
798 .di_first => |unwind_context| {
799 const first_pc = unwind_context.pc;
800 if (first_pc == 0) return .end;
801 it.* = .{ .di = unwind_context };
802 return .{ .frame = first_pc };
803 },
788804 .di => |*unwind_context| {
789805 const di = getSelfDebugInfo() catch unreachable;
790806 const di_gpa = getDebugInfoAllocator();
791 if (di.unwindFrame(di_gpa, unwind_context)) |ra| {
792 if (ra <= 1) return .end;
793 return .{ .frame = ra };
794 } else |err| {
807 di.unwindFrame(di_gpa, unwind_context) catch |err| {
795808 const pc = unwind_context.pc;
796809 it.* = .{ .fp = unwind_context.getFp() };
797810 return .{ .switch_to_fp = .{
798811 .address = pc,
799812 .err = err,
800813 } };
801 }
814 };
815 const pc = unwind_context.pc;
816 return if (pc == 0) .end else .{ .frame = pc };
802817 },
803818 .fp => |fp| {
804819 if (fp == 0) return .end; // we reached the "sentinel" base pointer
......@@ -824,9 +839,9 @@ const StackIterator = union(enum) {
824839 if (bp != 0 and bp <= fp) return .end;
825840
826841 it.fp = bp;
827 const ra = ra_ptr.*;
842 const ra = stripInstructionPtrAuthCode(ra_ptr.*);
828843 if (ra <= 1) return .end;
829 return .{ .frame = ra };
844 return .{ .frame = ra - 1 };
830845 },
831846 }
832847 }
......@@ -860,6 +875,26 @@ const StackIterator = union(enum) {
860875 }
861876};
862877
878/// Some platforms use pointer authentication: the upper bits of instruction pointers contain a
879/// signature. This function clears those signature bits to make the pointer directly usable.
880pub inline fn stripInstructionPtrAuthCode(ptr: usize) usize {
881 if (native_arch.isAARCH64()) {
882 // `hint 0x07` maps to `xpaclri` (or `nop` if the hardware doesn't support it)
883 // The save / restore is because `xpaclri` operates on x30 (LR)
884 return asm (
885 \\mov x16, x30
886 \\mov x30, x15
887 \\hint 0x07
888 \\mov x15, x30
889 \\mov x30, x16
890 : [ret] "={x15}" (-> usize),
891 : [ptr] "{x15}" (ptr),
892 : .{ .x16 = true });
893 }
894
895 return ptr;
896}
897
863898fn printSourceAtAddress(gpa: Allocator, debug_info: *SelfInfo, writer: *Writer, address: usize, tty_config: tty.Config) Writer.Error!void {
864899 const symbol: Symbol = debug_info.getSymbolAtAddress(gpa, address) catch |err| switch (err) {
865900 error.MissingDebugInfo,
lib/std/debug/SelfInfo.zig+18-36
......@@ -2,7 +2,6 @@
22//! goal of minimal code bloat and compilation speed penalty.
33
44const builtin = @import("builtin");
5const native_os = builtin.os.tag;
65const native_endian = native_arch.endian();
76const native_arch = builtin.cpu.arch;
87
......@@ -13,6 +12,8 @@ const assert = std.debug.assert;
1312const Dwarf = std.debug.Dwarf;
1413const CpuContext = std.debug.cpu_context.Native;
1514
15const stripInstructionPtrAuthCode = std.debug.stripInstructionPtrAuthCode;
16
1617const root = @import("root");
1718
1819const SelfInfo = @This();
......@@ -52,7 +53,7 @@ pub fn deinit(self: *SelfInfo, gpa: Allocator) void {
5253 if (Module.LookupCache != void) self.lookup_cache.deinit(gpa);
5354}
5455
55pub fn unwindFrame(self: *SelfInfo, gpa: Allocator, context: *UnwindContext) Error!usize {
56pub fn unwindFrame(self: *SelfInfo, gpa: Allocator, context: *UnwindContext) Error!void {
5657 comptime assert(supports_unwinding);
5758 const module: Module = try .lookup(&self.lookup_cache, gpa, context.pc);
5859 const gop = try self.modules.getOrPut(gpa, module.key());
......@@ -115,7 +116,7 @@ pub fn getModuleNameForAddress(self: *SelfInfo, gpa: Allocator, address: usize)
115116/// pub const supports_unwinding: bool;
116117/// /// Only required if `supports_unwinding == true`.
117118/// pub const UnwindContext = struct {
118/// /// A PC value inside the function of the last unwound frame.
119/// /// A PC value representing the location in the last frame.
119120/// pc: usize,
120121/// pub fn init(ctx: *std.debug.cpu_context.Native, gpa: Allocator) Allocator.Error!UnwindContext;
121122/// pub fn deinit(uc: *UnwindContext, gpa: Allocator) void;
......@@ -123,21 +124,22 @@ pub fn getModuleNameForAddress(self: *SelfInfo, gpa: Allocator, address: usize)
123124/// /// pointer is unknown, 0 may be returned instead.
124125/// pub fn getFp(uc: *UnwindContext) usize;
125126/// };
126/// /// Only required if `supports_unwinding == true`. Unwinds a single stack frame and returns
127/// /// the next return address (which may be 0 indicating end of stack).
127/// /// Only required if `supports_unwinding == true`. Unwinds a single stack frame.
128/// /// The caller will read the new instruction poiter from the `pc` field.
129/// /// `pc = 0` indicates end of stack / no more frames.
128130/// pub fn unwindFrame(
129131/// mod: *const Module,
130132/// gpa: Allocator,
131133/// di: *DebugInfo,
132134/// ctx: *UnwindContext,
133/// ) SelfInfo.Error!usize;
135/// ) SelfInfo.Error!void;
134136/// ```
135137const Module: type = Module: {
136138 // Allow overriding the target-specific `SelfInfo` implementation by exposing `root.debug.Module`.
137139 if (@hasDecl(root, "debug") and @hasDecl(root.debug, "Module")) {
138140 break :Module root.debug.Module;
139141 }
140 break :Module switch (native_os) {
142 break :Module switch (builtin.os.tag) {
141143 .linux,
142144 .netbsd,
143145 .freebsd,
......@@ -222,7 +224,7 @@ pub const DwarfUnwindContext = struct {
222224 const register = col.register orelse return error.InvalidRegister;
223225 // The default type is usually undefined, but can be overriden by ABI authors.
224226 // See the doc comment on `Dwarf.Unwind.VirtualMachine.RegisterRule.default`.
225 if (builtin.cpu.arch.isAARCH64() and register >= 19 and register <= 18) {
227 if (builtin.cpu.arch.isAARCH64() and register >= 19 and register <= 28) {
226228 // Callee-saved registers are initialized as if they had the .same_value rule
227229 const src = try context.cpu_context.dwarfRegisterBytes(register);
228230 if (src.len != out.len) return error.RegisterSizeMismatch;
......@@ -310,7 +312,7 @@ pub const DwarfUnwindContext = struct {
310312 unwind: *const Dwarf.Unwind,
311313 load_offset: usize,
312314 explicit_fde_offset: ?usize,
313 ) Error!usize {
315 ) Error!void {
314316 return unwindFrameInner(context, gpa, unwind, load_offset, explicit_fde_offset) catch |err| switch (err) {
315317 error.InvalidDebugInfo, error.MissingDebugInfo, error.OutOfMemory => |e| return e,
316318
......@@ -358,9 +360,10 @@ pub const DwarfUnwindContext = struct {
358360 unwind: *const Dwarf.Unwind,
359361 load_offset: usize,
360362 explicit_fde_offset: ?usize,
361 ) !usize {
362 if (!supports_unwinding) return error.UnsupportedCpuArchitecture;
363 if (context.pc == 0) return 0;
363 ) !void {
364 comptime assert(supports_unwinding);
365
366 if (context.pc == 0) return;
364367
365368 const pc_vaddr = context.pc - load_offset;
366369
......@@ -430,12 +433,12 @@ pub const DwarfUnwindContext = struct {
430433 }
431434 }
432435
433 const return_address: u64 = if (has_return_address) pc: {
436 const return_address: usize = if (has_return_address) pc: {
434437 const raw_ptr = try regNative(&new_cpu_context, cie.return_address_register);
435438 break :pc stripInstructionPtrAuthCode(raw_ptr.*);
436439 } else 0;
437440
438 (try regNative(new_cpu_context, ip_reg_num)).* = return_address;
441 (try regNative(&new_cpu_context, ip_reg_num)).* = return_address;
439442
440443 // The new CPU context is complete; flush changes.
441444 context.cpu_context = new_cpu_context;
......@@ -444,11 +447,9 @@ pub const DwarfUnwindContext = struct {
444447 // *after* the call, it could (in the case of noreturn functions) actually point outside of
445448 // the caller's address range, meaning an FDE lookup would fail. We can handle this by
446449 // subtracting 1 from `return_address` so that the next lookup is guaranteed to land inside
447 // the `call` instruction`. The exception to this rule is signal frames, where the return
450 // the `call` instruction. The exception to this rule is signal frames, where the return
448451 // address is the same instruction that triggered the handler.
449452 context.pc = if (cie.is_signal_frame) return_address else return_address -| 1;
450
451 return return_address;
452453 }
453454 /// Since register rules are applied (usually) during a panic,
454455 /// checked addition / subtraction is used so that we can return
......@@ -459,25 +460,6 @@ pub const DwarfUnwindContext = struct {
459460 else
460461 try std.math.sub(usize, base, @as(usize, @intCast(-offset)));
461462 }
462 /// Some platforms use pointer authentication - the upper bits of instruction pointers contain a signature.
463 /// This function clears these signature bits to make the pointer usable.
464 pub inline fn stripInstructionPtrAuthCode(ptr: usize) usize {
465 if (native_arch.isAARCH64()) {
466 // `hint 0x07` maps to `xpaclri` (or `nop` if the hardware doesn't support it)
467 // The save / restore is because `xpaclri` operates on x30 (LR)
468 return asm (
469 \\mov x16, x30
470 \\mov x30, x15
471 \\hint 0x07
472 \\mov x15, x30
473 \\mov x30, x16
474 : [ret] "={x15}" (-> usize),
475 : [ptr] "{x15}" (ptr),
476 : .{ .x16 = true });
477 }
478
479 return ptr;
480 }
481463
482464 pub fn regNative(ctx: *CpuContext, num: u16) error{
483465 InvalidRegister,
lib/std/debug/SelfInfo/DarwinModule.zig+153-71
......@@ -1,6 +1,5 @@
11/// The runtime address where __TEXT is loaded.
22text_base: usize,
3load_offset: usize,
43name: []const u8,
54
65pub fn key(m: *const DarwinModule) usize {
......@@ -12,38 +11,14 @@ pub const LookupCache = void;
1211pub fn lookup(cache: *LookupCache, gpa: Allocator, address: usize) Error!DarwinModule {
1312 _ = cache;
1413 _ = gpa;
15 const image_count = std.c._dyld_image_count();
16 for (0..image_count) |image_idx| {
17 const header = std.c._dyld_get_image_header(@intCast(image_idx)) orelse continue;
18 const text_base = @intFromPtr(header);
19 if (address < text_base) continue;
20 const load_offset = std.c._dyld_get_image_vmaddr_slide(@intCast(image_idx));
21
22 // Find the __TEXT segment
23 var it: macho.LoadCommandIterator = .{
24 .ncmds = header.ncmds,
25 .buffer = @as([*]u8, @ptrCast(header))[@sizeOf(macho.mach_header_64)..][0..header.sizeofcmds],
26 };
27 const text_segment_cmd = while (it.next()) |load_cmd| {
28 if (load_cmd.cmd() != .SEGMENT_64) continue;
29 const segment_cmd = load_cmd.cast(macho.segment_command_64).?;
30 if (!mem.eql(u8, segment_cmd.segName(), "__TEXT")) continue;
31 break segment_cmd;
32 } else continue;
33
34 const seg_start = load_offset + text_segment_cmd.vmaddr;
35 assert(seg_start == text_base);
36 const seg_end = seg_start + text_segment_cmd.vmsize;
37 if (address < seg_start or address >= seg_end) continue;
38
39 // We've found the matching __TEXT segment. This is the image we need.
40 return .{
41 .text_base = text_base,
42 .load_offset = load_offset,
43 .name = mem.span(std.c._dyld_get_image_name(@intCast(image_idx))),
44 };
14 var info: std.c.dl_info = undefined;
15 switch (std.c.dladdr(@ptrFromInt(address), &info)) {
16 0 => return error.MissingDebugInfo,
17 else => return .{
18 .name = std.mem.span(info.fname),
19 .text_base = @intFromPtr(info.fbase),
20 },
4521 }
46 return error.MissingDebugInfo;
4722}
4823fn loadUnwindInfo(module: *const DarwinModule) DebugInfo.Unwind {
4924 const header: *std.macho.mach_header = @ptrFromInt(module.text_base);
......@@ -52,56 +27,115 @@ fn loadUnwindInfo(module: *const DarwinModule) DebugInfo.Unwind {
5227 .ncmds = header.ncmds,
5328 .buffer = @as([*]u8, @ptrCast(header))[@sizeOf(macho.mach_header_64)..][0..header.sizeofcmds],
5429 };
55 const sections = while (it.next()) |load_cmd| {
30 const sections, const text_vmaddr = while (it.next()) |load_cmd| {
5631 if (load_cmd.cmd() != .SEGMENT_64) continue;
5732 const segment_cmd = load_cmd.cast(macho.segment_command_64).?;
5833 if (!mem.eql(u8, segment_cmd.segName(), "__TEXT")) continue;
59 break load_cmd.getSections();
34 break .{ load_cmd.getSections(), segment_cmd.vmaddr };
6035 } else unreachable;
6136
37 const vmaddr_slide = module.text_base - text_vmaddr;
38
6239 var unwind_info: ?[]const u8 = null;
6340 var eh_frame: ?[]const u8 = null;
6441 for (sections) |sect| {
6542 if (mem.eql(u8, sect.sectName(), "__unwind_info")) {
66 const sect_ptr: [*]u8 = @ptrFromInt(@as(usize, @intCast(module.load_offset + sect.addr)));
43 const sect_ptr: [*]u8 = @ptrFromInt(@as(usize, @intCast(vmaddr_slide + sect.addr)));
6744 unwind_info = sect_ptr[0..@intCast(sect.size)];
6845 } else if (mem.eql(u8, sect.sectName(), "__eh_frame")) {
69 const sect_ptr: [*]u8 = @ptrFromInt(@as(usize, @intCast(module.load_offset + sect.addr)));
46 const sect_ptr: [*]u8 = @ptrFromInt(@as(usize, @intCast(vmaddr_slide + sect.addr)));
7047 eh_frame = sect_ptr[0..@intCast(sect.size)];
7148 }
7249 }
7350 return .{
51 .vmaddr_slide = vmaddr_slide,
7452 .unwind_info = unwind_info,
7553 .eh_frame = eh_frame,
7654 };
7755}
7856fn loadMachO(module: *const DarwinModule, gpa: Allocator) !DebugInfo.LoadedMachO {
79 const mapped_mem = try mapDebugInfoFile(module.name);
80 errdefer posix.munmap(mapped_mem);
57 const all_mapped_memory = try mapDebugInfoFile(module.name);
58 errdefer posix.munmap(all_mapped_memory);
59
60 // In most cases, the file we just mapped is a Mach-O binary. However, it could be a "universal
61 // binary": a simple file format which contains Mach-O binaries for multiple targets. For
62 // instance, `/usr/lib/dyld` is currently distributed as a universal binary containing images
63 // for both ARM64 Macs and x86_64 Macs.
64 if (all_mapped_memory.len < 4) return error.InvalidDebugInfo;
65 const magic = @as(*const u32, @ptrCast(all_mapped_memory.ptr)).*;
66 // The contents of a Mach-O file, which may or may not be the whole of `all_mapped_memory`.
67 const mapped_macho = switch (magic) {
68 macho.MH_MAGIC_64 => all_mapped_memory,
69
70 macho.FAT_CIGAM => mapped_macho: {
71 // This is the universal binary format (aka a "fat binary"). Annoyingly, the whole thing
72 // is big-endian, so we'll be swapping some bytes.
73 if (all_mapped_memory.len < @sizeOf(macho.fat_header)) return error.InvalidDebugInfo;
74 const hdr: *const macho.fat_header = @ptrCast(all_mapped_memory.ptr);
75 const archs_ptr: [*]const macho.fat_arch = @ptrCast(all_mapped_memory.ptr + @sizeOf(macho.fat_header));
76 const archs: []const macho.fat_arch = archs_ptr[0..@byteSwap(hdr.nfat_arch)];
77 const native_cpu_type = switch (builtin.cpu.arch) {
78 .x86_64 => macho.CPU_TYPE_X86_64,
79 .aarch64 => macho.CPU_TYPE_ARM64,
80 else => comptime unreachable,
81 };
82 for (archs) |*arch| {
83 if (@byteSwap(arch.cputype) != native_cpu_type) continue;
84 const offset = @byteSwap(arch.offset);
85 const size = @byteSwap(arch.size);
86 break :mapped_macho all_mapped_memory[offset..][0..size];
87 }
88 // Our native architecture was not present in the fat binary.
89 return error.MissingDebugInfo;
90 },
91
92 // Even on modern 64-bit targets, this format doesn't seem to be too extensively used. It
93 // will be fairly easy to add support here if necessary; it's very similar to above.
94 macho.FAT_CIGAM_64 => return error.UnsupportedDebugInfo,
8195
82 const hdr: *const macho.mach_header_64 = @ptrCast(@alignCast(mapped_mem.ptr));
96 else => return error.InvalidDebugInfo,
97 };
98
99 const hdr: *const macho.mach_header_64 = @ptrCast(@alignCast(mapped_macho.ptr));
83100 if (hdr.magic != macho.MH_MAGIC_64)
84101 return error.InvalidDebugInfo;
85102
86 const symtab: macho.symtab_command = symtab: {
103 const symtab: macho.symtab_command, const text_vmaddr: u64 = lc_iter: {
87104 var it: macho.LoadCommandIterator = .{
88105 .ncmds = hdr.ncmds,
89 .buffer = mapped_mem[@sizeOf(macho.mach_header_64)..][0..hdr.sizeofcmds],
106 .buffer = mapped_macho[@sizeOf(macho.mach_header_64)..][0..hdr.sizeofcmds],
90107 };
108 var symtab: ?macho.symtab_command = null;
109 var text_vmaddr: ?u64 = null;
91110 while (it.next()) |cmd| switch (cmd.cmd()) {
92 .SYMTAB => break :symtab cmd.cast(macho.symtab_command) orelse return error.InvalidDebugInfo,
111 .SYMTAB => symtab = cmd.cast(macho.symtab_command) orelse return error.InvalidDebugInfo,
112 .SEGMENT_64 => if (cmd.cast(macho.segment_command_64)) |seg_cmd| {
113 if (!mem.eql(u8, seg_cmd.segName(), "__TEXT")) continue;
114 text_vmaddr = seg_cmd.vmaddr;
115 },
93116 else => {},
94117 };
95 return error.MissingDebugInfo;
118 break :lc_iter .{
119 symtab orelse return error.MissingDebugInfo,
120 text_vmaddr orelse return error.MissingDebugInfo,
121 };
96122 };
97123
98 const syms_ptr: [*]align(1) const macho.nlist_64 = @ptrCast(mapped_mem[symtab.symoff..]);
124 const syms_ptr: [*]align(1) const macho.nlist_64 = @ptrCast(mapped_macho[symtab.symoff..]);
99125 const syms = syms_ptr[0..symtab.nsyms];
100 const strings = mapped_mem[symtab.stroff..][0 .. symtab.strsize - 1 :0];
126 const strings = mapped_macho[symtab.stroff..][0 .. symtab.strsize - 1];
101127
102128 var symbols: std.ArrayList(MachoSymbol) = try .initCapacity(gpa, syms.len);
103129 defer symbols.deinit(gpa);
104130
131 // This map is temporary; it is used only to detect duplicates here. This is
132 // necessary because we prefer to use STAB ("symbolic debugging table") symbols,
133 // but they might not be present, so we track normal symbols too.
134 // Indices match 1-1 with those of `symbols`.
135 var symbol_names: std.StringArrayHashMapUnmanaged(void) = .empty;
136 defer symbol_names.deinit(gpa);
137 try symbol_names.ensureUnusedCapacity(gpa, syms.len);
138
105139 var ofile: u32 = undefined;
106140 var last_sym: MachoSymbol = undefined;
107141 var state: enum {
......@@ -115,7 +149,25 @@ fn loadMachO(module: *const DarwinModule, gpa: Allocator) !DebugInfo.LoadedMachO
115149 } = .init;
116150
117151 for (syms) |*sym| {
118 if (sym.n_type.bits.is_stab == 0) continue;
152 if (sym.n_type.bits.is_stab == 0) {
153 if (sym.n_strx == 0) continue;
154 switch (sym.n_type.bits.type) {
155 .undf, .pbud, .indr, .abs, _ => continue,
156 .sect => {
157 const name = std.mem.sliceTo(strings[sym.n_strx..], 0);
158 const gop = symbol_names.getOrPutAssumeCapacity(name);
159 if (!gop.found_existing) {
160 assert(gop.index == symbols.items.len);
161 symbols.appendAssumeCapacity(.{
162 .strx = sym.n_strx,
163 .addr = sym.n_value,
164 .ofile = MachoSymbol.unknown_ofile,
165 });
166 }
167 },
168 }
169 continue;
170 }
119171
120172 // TODO handle globals N_GSYM, and statics N_STSYM
121173 switch (sym.n_type.stab) {
......@@ -132,7 +184,6 @@ fn loadMachO(module: *const DarwinModule, gpa: Allocator) !DebugInfo.LoadedMachO
132184 last_sym = .{
133185 .strx = 0,
134186 .addr = sym.n_value,
135 .size = 0,
136187 .ofile = ofile,
137188 };
138189 },
......@@ -145,14 +196,22 @@ fn loadMachO(module: *const DarwinModule, gpa: Allocator) !DebugInfo.LoadedMachO
145196 },
146197 .fun_strx => {
147198 state = .fun_size;
148 last_sym.size = @intCast(sym.n_value);
149199 },
150200 else => return error.InvalidDebugInfo,
151201 },
152202 .ensym => switch (state) {
153203 .fun_size => {
154204 state = .ensym;
155 symbols.appendAssumeCapacity(last_sym);
205 if (last_sym.strx != 0) {
206 const name = std.mem.sliceTo(strings[sym.n_strx..], 0);
207 const gop = symbol_names.getOrPutAssumeCapacity(name);
208 if (!gop.found_existing) {
209 assert(gop.index == symbols.items.len);
210 symbols.appendAssumeCapacity(last_sym);
211 } else {
212 symbols.items[gop.index] = last_sym;
213 }
214 }
156215 },
157216 else => return error.InvalidDebugInfo,
158217 },
......@@ -168,9 +227,12 @@ fn loadMachO(module: *const DarwinModule, gpa: Allocator) !DebugInfo.LoadedMachO
168227 }
169228
170229 switch (state) {
171 .init => return error.MissingDebugInfo,
230 .init => {
231 // Missing STAB symtab entries is still okay, unless there were also no normal symbols.
232 if (symbols.items.len == 0) return error.MissingDebugInfo;
233 },
172234 .oso_close => {},
173 else => return error.InvalidDebugInfo,
235 else => return error.InvalidDebugInfo, // corrupted STAB entries in symtab
174236 }
175237
176238 const symbols_slice = try symbols.toOwnedSlice(gpa);
......@@ -182,10 +244,11 @@ fn loadMachO(module: *const DarwinModule, gpa: Allocator) !DebugInfo.LoadedMachO
182244 mem.sort(MachoSymbol, symbols_slice, {}, MachoSymbol.addressLessThan);
183245
184246 return .{
185 .mapped_memory = mapped_mem,
247 .mapped_memory = all_mapped_memory,
186248 .symbols = symbols_slice,
187249 .strings = strings,
188250 .ofiles = .empty,
251 .vaddr_offset = module.text_base - text_vmaddr,
189252 };
190253}
191254pub fn getSymbolAtAddress(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo, address: usize) Error!std.debug.Symbol {
......@@ -195,7 +258,7 @@ pub fn getSymbolAtAddress(module: *const DarwinModule, gpa: Allocator, di: *Debu
195258 };
196259 const loaded_macho = &di.loaded_macho.?;
197260
198 const vaddr = address - module.load_offset;
261 const vaddr = address - loaded_macho.vaddr_offset;
199262 const symbol = MachoSymbol.find(loaded_macho.symbols, vaddr) orelse return .unknown;
200263
201264 // offset of `address` from start of `symbol`
......@@ -212,6 +275,11 @@ pub fn getSymbolAtAddress(module: *const DarwinModule, gpa: Allocator, di: *Debu
212275 .source_location = null,
213276 };
214277
278 if (symbol.ofile == MachoSymbol.unknown_ofile) {
279 // We don't have STAB info, so can't track down the object file; all we can do is the symbol name.
280 return sym_only_result;
281 }
282
215283 const o_file: *DebugInfo.OFile = of: {
216284 const gop = try loaded_macho.ofiles.getOrPut(gpa, symbol.ofile);
217285 if (!gop.found_existing) {
......@@ -233,7 +301,7 @@ pub fn getSymbolAtAddress(module: *const DarwinModule, gpa: Allocator, di: *Debu
233301 const compile_unit = o_file.dwarf.findCompileUnit(native_endian, symbol_ofile_vaddr) catch return sym_only_result;
234302
235303 return .{
236 .name = o_file.dwarf.getSymbolName(symbol_ofile_vaddr) orelse stab_symbol,
304 .name = o_file.dwarf.getSymbolName(symbol_ofile_vaddr + address_symbol_offset) orelse stab_symbol,
237305 .compile_unit_name = compile_unit.die.getAttrString(
238306 &o_file.dwarf,
239307 native_endian,
......@@ -256,7 +324,7 @@ pub const UnwindContext = std.debug.SelfInfo.DwarfUnwindContext;
256324/// Unwind a frame using MachO compact unwind info (from __unwind_info).
257325/// If the compact encoding can't encode a way to unwind a frame, it will
258326/// defer unwinding to DWARF, in which case `.eh_frame` will be used if available.
259pub fn unwindFrame(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo, context: *UnwindContext) Error!usize {
327pub fn unwindFrame(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo, context: *UnwindContext) Error!void {
260328 return unwindFrameInner(module, gpa, di, context) catch |err| switch (err) {
261329 error.InvalidDebugInfo,
262330 error.MissingDebugInfo,
......@@ -272,7 +340,7 @@ pub fn unwindFrame(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo,
272340 => return error.InvalidDebugInfo,
273341 };
274342}
275fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo, context: *UnwindContext) !usize {
343fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo, context: *UnwindContext) !void {
276344 if (di.unwind == null) di.unwind = module.loadUnwindInfo();
277345 const unwind = &di.unwind.?;
278346
......@@ -500,11 +568,11 @@ fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo,
500568 },
501569 .DWARF => {
502570 const eh_frame = unwind.eh_frame orelse return error.MissingDebugInfo;
503 const eh_frame_vaddr = @intFromPtr(eh_frame.ptr) - module.load_offset;
571 const eh_frame_vaddr = @intFromPtr(eh_frame.ptr) - unwind.vmaddr_slide;
504572 return context.unwindFrame(
505573 gpa,
506574 &.initSection(.eh_frame, eh_frame_vaddr, eh_frame),
507 module.load_offset,
575 unwind.vmaddr_slide,
508576 @intCast(encoding.value.x86_64.dwarf),
509577 );
510578 },
......@@ -520,11 +588,11 @@ fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo,
520588 },
521589 .DWARF => {
522590 const eh_frame = unwind.eh_frame orelse return error.MissingDebugInfo;
523 const eh_frame_vaddr = @intFromPtr(eh_frame.ptr) - module.load_offset;
591 const eh_frame_vaddr = @intFromPtr(eh_frame.ptr) - unwind.vmaddr_slide;
524592 return context.unwindFrame(
525593 gpa,
526594 &.initSection(.eh_frame, eh_frame_vaddr, eh_frame),
527 module.load_offset,
595 unwind.vmaddr_slide,
528596 @intCast(encoding.value.x86_64.dwarf),
529597 );
530598 },
......@@ -572,9 +640,7 @@ fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo,
572640 else => comptime unreachable, // unimplemented
573641 };
574642
575 context.pc = UnwindContext.stripInstructionPtrAuthCode(new_ip);
576 if (context.pc > 0) context.pc -= 1;
577 return new_ip;
643 context.pc = std.debug.stripInstructionPtrAuthCode(new_ip) -| 1;
578644}
579645pub const DebugInfo = struct {
580646 unwind: ?Unwind,
......@@ -590,6 +656,7 @@ pub const DebugInfo = struct {
590656 for (loaded_macho.ofiles.values()) |*ofile| {
591657 ofile.dwarf.deinit(gpa);
592658 ofile.symbols_by_name.deinit(gpa);
659 posix.munmap(ofile.mapped_memory);
593660 }
594661 loaded_macho.ofiles.deinit(gpa);
595662 gpa.free(loaded_macho.symbols);
......@@ -598,6 +665,9 @@ pub const DebugInfo = struct {
598665 }
599666
600667 const Unwind = struct {
668 /// The slide applied to the following sections. So, `unwind_info.ptr` is this many bytes
669 /// higher than the vmaddr of `__unwind_info`, and likewise for `__eh_frame`.
670 vmaddr_slide: u64,
601671 // Backed by the in-memory sections mapped by the loader
602672 unwind_info: ?[]const u8,
603673 eh_frame: ?[]const u8,
......@@ -606,21 +676,31 @@ pub const DebugInfo = struct {
606676 const LoadedMachO = struct {
607677 mapped_memory: []align(std.heap.page_size_min) const u8,
608678 symbols: []const MachoSymbol,
609 strings: [:0]const u8,
679 strings: []const u8,
610680 /// Key is index into `strings` of the file path.
611681 ofiles: std.AutoArrayHashMapUnmanaged(u32, OFile),
682 /// This is not necessarily the same as the vmaddr_slide that dyld would report. This is
683 /// because the segments in the file on disk might differ from the ones in memory. Normally
684 /// we wouldn't necessarily expect that to work, but /usr/lib/dyld is incredibly annoying:
685 /// it exists on disk (necessarily, because the kernel needs to load it!), but is also in
686 /// the dyld cache (dyld actually restart itself from cache after loading it), and the two
687 /// versions have (very) different segment base addresses. It's sort of like a large slide
688 /// has been applied to all addresses in memory. For an optimal experience, we consider the
689 /// on-disk vmaddr instead of the in-memory one.
690 vaddr_offset: usize,
612691 };
613692
614693 const OFile = struct {
694 mapped_memory: []align(std.heap.page_size_min) const u8,
615695 dwarf: Dwarf,
616 strtab: [:0]const u8,
696 strtab: []const u8,
617697 symtab: []align(1) const macho.nlist_64,
618698 /// All named symbols in `symtab`. Stored `u32` key is the index into `symtab`. Accessed
619699 /// through `SymbolAdapter`, so that the symbol name is used as the logical key.
620700 symbols_by_name: std.ArrayHashMapUnmanaged(u32, void, void, true),
621701
622702 const SymbolAdapter = struct {
623 strtab: [:0]const u8,
703 strtab: []const u8,
624704 symtab: []align(1) const macho.nlist_64,
625705 pub fn hash(ctx: SymbolAdapter, sym_name: []const u8) u32 {
626706 _ = ctx;
......@@ -663,7 +743,7 @@ pub const DebugInfo = struct {
663743
664744 if (mapped_mem.len < symtab_cmd.stroff + symtab_cmd.strsize) return error.InvalidDebugInfo;
665745 if (mapped_mem[symtab_cmd.stroff + symtab_cmd.strsize - 1] != 0) return error.InvalidDebugInfo;
666 const strtab = mapped_mem[symtab_cmd.stroff..][0 .. symtab_cmd.strsize - 1 :0];
746 const strtab = mapped_mem[symtab_cmd.stroff..][0 .. symtab_cmd.strsize - 1];
667747
668748 const n_sym_bytes = symtab_cmd.nsyms * @sizeOf(macho.nlist_64);
669749 if (mapped_mem.len < symtab_cmd.symoff + n_sym_bytes) return error.InvalidDebugInfo;
......@@ -717,6 +797,7 @@ pub const DebugInfo = struct {
717797 try dwarf.open(gpa, native_endian);
718798
719799 return .{
800 .mapped_memory = mapped_mem,
720801 .dwarf = dwarf,
721802 .strtab = strtab,
722803 .symtab = symtab,
......@@ -728,8 +809,9 @@ pub const DebugInfo = struct {
728809const MachoSymbol = struct {
729810 strx: u32,
730811 addr: u64,
731 size: u32,
812 /// Value may be `unknown_ofile`.
732813 ofile: u32,
814 const unknown_ofile = std.math.maxInt(u32);
733815 fn addressLessThan(context: void, lhs: MachoSymbol, rhs: MachoSymbol) bool {
734816 _ = context;
735817 return lhs.addr < rhs.addr;
......@@ -754,9 +836,9 @@ const MachoSymbol = struct {
754836
755837 test find {
756838 const symbols: []const MachoSymbol = &.{
757 .{ .addr = 100, .strx = undefined, .size = undefined, .ofile = undefined },
758 .{ .addr = 200, .strx = undefined, .size = undefined, .ofile = undefined },
759 .{ .addr = 300, .strx = undefined, .size = undefined, .ofile = undefined },
839 .{ .addr = 100, .strx = undefined, .ofile = undefined },
840 .{ .addr = 200, .strx = undefined, .ofile = undefined },
841 .{ .addr = 300, .strx = undefined, .ofile = undefined },
760842 };
761843
762844 try testing.expectEqual(null, find(symbols, 0));
lib/std/debug/SelfInfo/ElfModule.zig+1-1
......@@ -230,7 +230,7 @@ fn loadUnwindInfo(module: *const ElfModule, gpa: Allocator, di: *DebugInfo) Erro
230230 else => unreachable,
231231 }
232232}
233pub fn unwindFrame(module: *const ElfModule, gpa: Allocator, di: *DebugInfo, context: *UnwindContext) Error!usize {
233pub fn unwindFrame(module: *const ElfModule, gpa: Allocator, di: *DebugInfo, context: *UnwindContext) Error!void {
234234 if (di.unwind[0] == null) try module.loadUnwindInfo(gpa, di);
235235 std.debug.assert(di.unwind[0] != null);
236236 for (&di.unwind) |*opt_unwind| {
lib/std/debug/SelfInfo/WindowsModule.zig+32-4
......@@ -332,6 +332,34 @@ pub const UnwindContext = struct {
332332 .Wcr = @splat(0),
333333 .Wvr = @splat(0),
334334 },
335 .thumb => .{
336 .ContextFlags = 0,
337 .R0 = ctx.r[0],
338 .R1 = ctx.r[1],
339 .R2 = ctx.r[2],
340 .R3 = ctx.r[3],
341 .R4 = ctx.r[4],
342 .R5 = ctx.r[5],
343 .R6 = ctx.r[6],
344 .R7 = ctx.r[7],
345 .R8 = ctx.r[8],
346 .R9 = ctx.r[9],
347 .R10 = ctx.r[10],
348 .R11 = ctx.r[11],
349 .R12 = ctx.r[12],
350 .Sp = ctx.r[13],
351 .Lr = ctx.r[14],
352 .Pc = ctx.r[15],
353 .Cpsr = 0,
354 .Fpcsr = 0,
355 .Padding = 0,
356 .DUMMYUNIONNAME = .{ .S = @splat(0) },
357 .Bvr = @splat(0),
358 .Bcr = @splat(0),
359 .Wvr = @splat(0),
360 .Wcr = @splat(0),
361 .Padding2 = @splat(0),
362 },
335363 else => comptime unreachable,
336364 },
337365 .history_table = std.mem.zeroes(windows.UNWIND_HISTORY_TABLE),
......@@ -345,7 +373,7 @@ pub const UnwindContext = struct {
345373 return ctx.cur.getRegs().bp;
346374 }
347375};
348pub fn unwindFrame(module: *const WindowsModule, gpa: Allocator, di: *DebugInfo, context: *UnwindContext) !usize {
376pub fn unwindFrame(module: *const WindowsModule, gpa: Allocator, di: *DebugInfo, context: *UnwindContext) !void {
349377 _ = module;
350378 _ = gpa;
351379 _ = di;
......@@ -374,10 +402,10 @@ pub fn unwindFrame(module: *const WindowsModule, gpa: Allocator, di: *DebugInfo,
374402 const next_regs = context.cur.getRegs();
375403 const tib = &windows.teb().NtTib;
376404 if (next_regs.sp < @intFromPtr(tib.StackLimit) or next_regs.sp > @intFromPtr(tib.StackBase)) {
377 return 0;
405 context.pc = 0;
406 } else {
407 context.pc = next_regs.ip -| 1;
378408 }
379 context.pc = next_regs.ip -| 1;
380 return next_regs.ip;
381409}
382410
383411const WindowsModule = @This();
lib/std/debug/cpu_context.zig+6
......@@ -214,6 +214,12 @@ pub fn fromWindowsContext(ctx: *const std.os.windows.CONTEXT) Native {
214214 .sp = ctx.Sp,
215215 .pc = ctx.Pc,
216216 },
217 .thumb => .{ .r = .{
218 ctx.R0, ctx.R1, ctx.R2, ctx.R3,
219 ctx.R4, ctx.R5, ctx.R6, ctx.R7,
220 ctx.R8, ctx.R9, ctx.R10, ctx.R11,
221 ctx.R12, ctx.Sp, ctx.Lr, ctx.Pc,
222 } },
217223 else => comptime unreachable,
218224 };
219225}