authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-09-08 11:38:25+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-09-30 13:44:51+01:00
log253fdfce7064a6ebf70dbb62b465d6564eac0948
tree02bd4ff3c8cc560e94325fdc3ba7383533b78101
parent9859440d83e5ef17d353be39f32f2dc0b9ce0e02
signaturelock-open Commit is signed but in an unrecognized format.

SelfInfo: be honest about how general unwinding is

...in that it isn't: it's currently very specialized to DWARF unwinding. Also, make a type unmanaged.

4 files changed, 41 insertions(+), 36 deletions(-)

lib/std/debug.zig+7-4
......@@ -747,7 +747,7 @@ pub fn dumpStackTrace(st: *const std.builtin.StackTrace) void {
747747
748748const StackIterator = union(enum) {
749749 /// Unwinding using debug info (e.g. DWARF CFI).
750 di: if (SelfInfo.supports_unwinding) SelfInfo.UnwindContext else noreturn,
750 di: if (SelfInfo.supports_unwinding) SelfInfo.DwarfUnwindContext else noreturn,
751751 /// Naive frame-pointer-based unwinding. Very simple, but typically unreliable.
752752 fp: usize,
753753
......@@ -766,17 +766,17 @@ const StackIterator = union(enum) {
766766 if (context_opt) |context| {
767767 context_buf.* = context.*;
768768 relocateContext(context_buf);
769 return .{ .di = .init(getDebugInfoAllocator(), context_buf) };
769 return .{ .di = .init(context_buf) };
770770 }
771771 if (getContext(context_buf)) {
772 return .{ .di = .init(getDebugInfoAllocator(), context_buf) };
772 return .{ .di = .init(context_buf) };
773773 }
774774 return .{ .fp = @frameAddress() };
775775 }
776776 fn deinit(si: *StackIterator) void {
777777 switch (si.*) {
778778 .fp => {},
779 .di => |*unwind_context| unwind_context.deinit(),
779 .di => |*unwind_context| unwind_context.deinit(getDebugInfoAllocator()),
780780 }
781781 }
782782
......@@ -944,6 +944,9 @@ fn printLineInfo(
944944 tty_config.setColor(writer, .reset) catch {};
945945 }
946946 try writer.writeAll("\n");
947 } else |_| {
948 // Ignore all errors; it's a better UX to just print the source location without the
949 // corresponding line number. The user can always open the source file themselves.
947950 }
948951 }
949952 }
lib/std/debug/SelfInfo.zig+23-22
......@@ -53,7 +53,7 @@ pub fn deinit(self: *SelfInfo, gpa: Allocator) void {
5353 if (Module.LookupCache != void) self.lookup_cache.deinit(gpa);
5454}
5555
56pub fn unwindFrame(self: *SelfInfo, gpa: Allocator, context: *UnwindContext) Error!usize {
56pub fn unwindFrame(self: *SelfInfo, gpa: Allocator, context: *DwarfUnwindContext) Error!usize {
5757 comptime assert(supports_unwinding);
5858 const module: Module = try .lookup(&self.lookup_cache, gpa, context.pc);
5959 const gop = try self.modules.getOrPut(gpa, module.key());
......@@ -120,7 +120,7 @@ pub fn getModuleNameForAddress(self: *SelfInfo, gpa: Allocator, address: usize)
120120/// mod: *const Module,
121121/// gpa: Allocator,
122122/// di: *DebugInfo,
123/// ctx: *SelfInfo.UnwindContext,
123/// ctx: *SelfInfo.DwarfUnwindContext,
124124/// ) SelfInfo.Error!usize;
125125/// ```
126126const Module: type = Module: {
......@@ -135,8 +135,7 @@ const Module: type = Module: {
135135 };
136136};
137137
138pub const UnwindContext = struct {
139 gpa: Allocator, // MLUGG TODO: make unmanaged (also maybe rename this type, DwarfUnwindContext or smth idk)
138pub const DwarfUnwindContext = struct {
140139 cfa: ?usize,
141140 pc: usize,
142141 thread_context: *std.debug.ThreadContext,
......@@ -144,7 +143,7 @@ pub const UnwindContext = struct {
144143 vm: Dwarf.Unwind.VirtualMachine,
145144 stack_machine: Dwarf.expression.StackMachine(.{ .call_frame_context = true }),
146145
147 pub fn init(gpa: Allocator, thread_context: *std.debug.ThreadContext) UnwindContext {
146 pub fn init(thread_context: *std.debug.ThreadContext) DwarfUnwindContext {
148147 comptime assert(supports_unwinding);
149148
150149 const ip_reg_num = Dwarf.abi.ipRegNum(native_arch).?;
......@@ -154,7 +153,6 @@ pub const UnwindContext = struct {
154153 const pc = stripInstructionPtrAuthCode(raw_pc_ptr.*);
155154
156155 return .{
157 .gpa = gpa,
158156 .cfa = null,
159157 .pc = pc,
160158 .thread_context = thread_context,
......@@ -164,19 +162,20 @@ pub const UnwindContext = struct {
164162 };
165163 }
166164
167 pub fn deinit(self: *UnwindContext) void {
168 self.vm.deinit(self.gpa);
169 self.stack_machine.deinit(self.gpa);
165 pub fn deinit(self: *DwarfUnwindContext, gpa: Allocator) void {
166 self.vm.deinit(gpa);
167 self.stack_machine.deinit(gpa);
170168 self.* = undefined;
171169 }
172170
173 pub fn getFp(self: *const UnwindContext) !usize {
171 pub fn getFp(self: *const DwarfUnwindContext) !usize {
174172 return (try regValueNative(self.thread_context, Dwarf.abi.fpRegNum(native_arch, self.reg_context), self.reg_context)).*;
175173 }
176174
177175 /// Resolves the register rule and places the result into `out` (see regBytes)
178176 pub fn resolveRegisterRule(
179 context: *UnwindContext,
177 context: *DwarfUnwindContext,
178 gpa: Allocator,
180179 col: Dwarf.Unwind.VirtualMachine.Column,
181180 expression_context: std.debug.Dwarf.expression.Context,
182181 out: []u8,
......@@ -224,7 +223,7 @@ pub const UnwindContext = struct {
224223 },
225224 .expression => |expression| {
226225 context.stack_machine.reset();
227 const value = try context.stack_machine.run(expression, context.gpa, expression_context, context.cfa.?);
226 const value = try context.stack_machine.run(expression, gpa, expression_context, context.cfa.?);
228227 const addr = if (value) |v| blk: {
229228 if (v != .generic) return error.InvalidExpressionValue;
230229 break :blk v.generic;
......@@ -235,7 +234,7 @@ pub const UnwindContext = struct {
235234 },
236235 .val_expression => |expression| {
237236 context.stack_machine.reset();
238 const value = try context.stack_machine.run(expression, context.gpa, expression_context, context.cfa.?);
237 const value = try context.stack_machine.run(expression, gpa, expression_context, context.cfa.?);
239238 if (value) |v| {
240239 if (v != .generic) return error.InvalidExpressionValue;
241240 mem.writeInt(usize, out[0..@sizeOf(usize)], v.generic, native_endian);
......@@ -252,13 +251,14 @@ pub const UnwindContext = struct {
252251 /// may require lazily loading the data in those sections.
253252 ///
254253 /// `explicit_fde_offset` is for cases where the FDE offset is known, such as when __unwind_info
255 pub fn unwindFrameDwarf(
256 context: *UnwindContext,
254 pub fn unwindFrame(
255 context: *DwarfUnwindContext,
256 gpa: Allocator,
257257 unwind: *const Dwarf.Unwind,
258258 load_offset: usize,
259259 explicit_fde_offset: ?usize,
260260 ) Error!usize {
261 return unwindFrameDwarfInner(context, unwind, load_offset, explicit_fde_offset) catch |err| switch (err) {
261 return unwindFrameInner(context, gpa, unwind, load_offset, explicit_fde_offset) catch |err| switch (err) {
262262 error.InvalidDebugInfo, error.MissingDebugInfo, error.OutOfMemory => |e| return e,
263263
264264 error.UnimplementedArch,
......@@ -302,8 +302,9 @@ pub const UnwindContext = struct {
302302 => return error.InvalidDebugInfo,
303303 };
304304 }
305 fn unwindFrameDwarfInner(
306 context: *UnwindContext,
305 fn unwindFrameInner(
306 context: *DwarfUnwindContext,
307 gpa: Allocator,
307308 unwind: *const Dwarf.Unwind,
308309 load_offset: usize,
309310 explicit_fde_offset: ?usize,
......@@ -338,7 +339,7 @@ pub const UnwindContext = struct {
338339 context.reg_context.eh_frame = cie.version != 4;
339340 context.reg_context.is_macho = native_os.isDarwin();
340341
341 const row = try context.vm.runTo(context.gpa, context.pc - load_offset, cie, fde, @sizeOf(usize), native_endian);
342 const row = try context.vm.runTo(gpa, context.pc - load_offset, cie, fde, @sizeOf(usize), native_endian);
342343 context.cfa = switch (row.cfa.rule) {
343344 .val_offset => |offset| blk: {
344345 const register = row.cfa.register orelse return error.InvalidCFARule;
......@@ -349,7 +350,7 @@ pub const UnwindContext = struct {
349350 context.stack_machine.reset();
350351 const value = try context.stack_machine.run(
351352 expr,
352 context.gpa,
353 gpa,
353354 expression_context,
354355 context.cfa,
355356 );
......@@ -366,7 +367,7 @@ pub const UnwindContext = struct {
366367
367368 // Buffering the modifications is done because copying the thread context is not portable,
368369 // some implementations (ie. darwin) use internal pointers to the mcontext.
369 var arena: std.heap.ArenaAllocator = .init(context.gpa);
370 var arena: std.heap.ArenaAllocator = .init(gpa);
370371 defer arena.deinit();
371372 const update_arena = arena.allocator();
372373
......@@ -388,7 +389,7 @@ pub const UnwindContext = struct {
388389
389390 const dest = try regBytes(context.thread_context, register, context.reg_context);
390391 const src = try update_arena.alloc(u8, dest.len);
391 try context.resolveRegisterRule(column, expression_context, src);
392 try context.resolveRegisterRule(gpa, column, expression_context, src);
392393
393394 const new_update = try update_arena.create(RegisterUpdate);
394395 new_update.* = .{
lib/std/debug/SelfInfo/DarwinModule.zig+8-7
......@@ -255,7 +255,7 @@ pub const supports_unwinding: bool = true;
255255/// Unwind a frame using MachO compact unwind info (from __unwind_info).
256256/// If the compact encoding can't encode a way to unwind a frame, it will
257257/// defer unwinding to DWARF, in which case `.eh_frame` will be used if available.
258pub fn unwindFrame(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo, context: *UnwindContext) Error!usize {
258pub fn unwindFrame(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo, context: *DwarfUnwindContext) Error!usize {
259259 return unwindFrameInner(module, gpa, di, context) catch |err| switch (err) {
260260 error.InvalidDebugInfo,
261261 error.MissingDebugInfo,
......@@ -274,8 +274,7 @@ pub fn unwindFrame(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo,
274274 => return error.InvalidDebugInfo,
275275 };
276276}
277fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo, context: *UnwindContext) !usize {
278 _ = gpa;
277fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo, context: *DwarfUnwindContext) !usize {
279278 if (di.unwind == null) di.unwind = module.loadUnwindInfo();
280279 const unwind = &di.unwind.?;
281280
......@@ -505,7 +504,8 @@ fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo,
505504 .DWARF => {
506505 const eh_frame = unwind.eh_frame orelse return error.MissingDebugInfo;
507506 const eh_frame_vaddr = @intFromPtr(eh_frame.ptr) - module.load_offset;
508 return context.unwindFrameDwarf(
507 return context.unwindFrame(
508 gpa,
509509 &.initSection(.eh_frame, eh_frame_vaddr, eh_frame),
510510 module.load_offset,
511511 @intCast(encoding.value.x86_64.dwarf),
......@@ -524,7 +524,8 @@ fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo,
524524 .DWARF => {
525525 const eh_frame = unwind.eh_frame orelse return error.MissingDebugInfo;
526526 const eh_frame_vaddr = @intFromPtr(eh_frame.ptr) - module.load_offset;
527 return context.unwindFrameDwarf(
527 return context.unwindFrame(
528 gpa,
528529 &.initSection(.eh_frame, eh_frame_vaddr, eh_frame),
529530 module.load_offset,
530531 @intCast(encoding.value.x86_64.dwarf),
......@@ -574,7 +575,7 @@ fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo,
574575 else => comptime unreachable, // unimplemented
575576 };
576577
577 context.pc = UnwindContext.stripInstructionPtrAuthCode(new_ip);
578 context.pc = DwarfUnwindContext.stripInstructionPtrAuthCode(new_ip);
578579 if (context.pc > 0) context.pc -= 1;
579580 return new_ip;
580581}
......@@ -819,7 +820,7 @@ const macho = std.macho;
819820const mem = std.mem;
820821const posix = std.posix;
821822const testing = std.testing;
822const UnwindContext = std.debug.SelfInfo.UnwindContext;
823const DwarfUnwindContext = std.debug.SelfInfo.DwarfUnwindContext;
823824const Error = std.debug.SelfInfo.Error;
824825const regBytes = Dwarf.abi.regBytes;
825826const regValueNative = Dwarf.abi.regValueNative;
lib/std/debug/SelfInfo/ElfModule.zig+3-3
......@@ -193,12 +193,12 @@ fn loadUnwindInfo(module: *const ElfModule, gpa: Allocator, di: *DebugInfo) Erro
193193 else => unreachable,
194194 }
195195}
196pub fn unwindFrame(module: *const ElfModule, gpa: Allocator, di: *DebugInfo, context: *UnwindContext) Error!usize {
196pub fn unwindFrame(module: *const ElfModule, gpa: Allocator, di: *DebugInfo, context: *DwarfUnwindContext) Error!usize {
197197 if (di.unwind[0] == null) try module.loadUnwindInfo(gpa, di);
198198 std.debug.assert(di.unwind[0] != null);
199199 for (&di.unwind) |*opt_unwind| {
200200 const unwind = &(opt_unwind.* orelse break);
201 return context.unwindFrameDwarf(unwind, module.load_offset, null) catch |err| switch (err) {
201 return context.unwindFrame(gpa, unwind, module.load_offset, null) catch |err| switch (err) {
202202 error.MissingDebugInfo => continue, // try the next one
203203 else => |e| return e,
204204 };
......@@ -233,7 +233,7 @@ const Allocator = std.mem.Allocator;
233233const Dwarf = std.debug.Dwarf;
234234const elf = std.elf;
235235const mem = std.mem;
236const UnwindContext = std.debug.SelfInfo.UnwindContext;
236const DwarfUnwindContext = std.debug.SelfInfo.DwarfUnwindContext;
237237const Error = std.debug.SelfInfo.Error;
238238
239239const builtin = @import("builtin");