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 {...@@ -747,7 +747,7 @@ pub fn dumpStackTrace(st: *const std.builtin.StackTrace) void {
747747
748const StackIterator = union(enum) {748const StackIterator = union(enum) {
749 /// Unwinding using debug info (e.g. DWARF CFI).749 /// 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,
751 /// Naive frame-pointer-based unwinding. Very simple, but typically unreliable.751 /// Naive frame-pointer-based unwinding. Very simple, but typically unreliable.
752 fp: usize,752 fp: usize,
753753
...@@ -766,17 +766,17 @@ const StackIterator = union(enum) {...@@ -766,17 +766,17 @@ const StackIterator = union(enum) {
766 if (context_opt) |context| {766 if (context_opt) |context| {
767 context_buf.* = context.*;767 context_buf.* = context.*;
768 relocateContext(context_buf);768 relocateContext(context_buf);
769 return .{ .di = .init(getDebugInfoAllocator(), context_buf) };769 return .{ .di = .init(context_buf) };
770 }770 }
771 if (getContext(context_buf)) {771 if (getContext(context_buf)) {
772 return .{ .di = .init(getDebugInfoAllocator(), context_buf) };772 return .{ .di = .init(context_buf) };
773 }773 }
774 return .{ .fp = @frameAddress() };774 return .{ .fp = @frameAddress() };
775 }775 }
776 fn deinit(si: *StackIterator) void {776 fn deinit(si: *StackIterator) void {
777 switch (si.*) {777 switch (si.*) {
778 .fp => {},778 .fp => {},
779 .di => |*unwind_context| unwind_context.deinit(),779 .di => |*unwind_context| unwind_context.deinit(getDebugInfoAllocator()),
780 }780 }
781 }781 }
782782
...@@ -944,6 +944,9 @@ fn printLineInfo(...@@ -944,6 +944,9 @@ fn printLineInfo(
944 tty_config.setColor(writer, .reset) catch {};944 tty_config.setColor(writer, .reset) catch {};
945 }945 }
946 try writer.writeAll("\n");946 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.
947 }950 }
948 }951 }
949 }952 }
lib/std/debug/SelfInfo.zig+23-22
...@@ -53,7 +53,7 @@ pub fn deinit(self: *SelfInfo, gpa: Allocator) void {...@@ -53,7 +53,7 @@ pub fn deinit(self: *SelfInfo, gpa: Allocator) void {
53 if (Module.LookupCache != void) self.lookup_cache.deinit(gpa);53 if (Module.LookupCache != void) self.lookup_cache.deinit(gpa);
54}54}
5555
56pub fn unwindFrame(self: *SelfInfo, gpa: Allocator, context: *UnwindContext) Error!usize {56pub fn unwindFrame(self: *SelfInfo, gpa: Allocator, context: *DwarfUnwindContext) Error!usize {
57 comptime assert(supports_unwinding);57 comptime assert(supports_unwinding);
58 const module: Module = try .lookup(&self.lookup_cache, gpa, context.pc);58 const module: Module = try .lookup(&self.lookup_cache, gpa, context.pc);
59 const gop = try self.modules.getOrPut(gpa, module.key());59 const gop = try self.modules.getOrPut(gpa, module.key());
...@@ -120,7 +120,7 @@ pub fn getModuleNameForAddress(self: *SelfInfo, gpa: Allocator, address: usize)...@@ -120,7 +120,7 @@ pub fn getModuleNameForAddress(self: *SelfInfo, gpa: Allocator, address: usize)
120/// mod: *const Module,120/// mod: *const Module,
121/// gpa: Allocator,121/// gpa: Allocator,
122/// di: *DebugInfo,122/// di: *DebugInfo,
123/// ctx: *SelfInfo.UnwindContext,123/// ctx: *SelfInfo.DwarfUnwindContext,
124/// ) SelfInfo.Error!usize;124/// ) SelfInfo.Error!usize;
125/// ```125/// ```
126const Module: type = Module: {126const Module: type = Module: {
...@@ -135,8 +135,7 @@ const Module: type = Module: {...@@ -135,8 +135,7 @@ const Module: type = Module: {
135 };135 };
136};136};
137137
138pub const UnwindContext = struct {138pub const DwarfUnwindContext = struct {
139 gpa: Allocator, // MLUGG TODO: make unmanaged (also maybe rename this type, DwarfUnwindContext or smth idk)
140 cfa: ?usize,139 cfa: ?usize,
141 pc: usize,140 pc: usize,
142 thread_context: *std.debug.ThreadContext,141 thread_context: *std.debug.ThreadContext,
...@@ -144,7 +143,7 @@ pub const UnwindContext = struct {...@@ -144,7 +143,7 @@ pub const UnwindContext = struct {
144 vm: Dwarf.Unwind.VirtualMachine,143 vm: Dwarf.Unwind.VirtualMachine,
145 stack_machine: Dwarf.expression.StackMachine(.{ .call_frame_context = true }),144 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 {
148 comptime assert(supports_unwinding);147 comptime assert(supports_unwinding);
149148
150 const ip_reg_num = Dwarf.abi.ipRegNum(native_arch).?;149 const ip_reg_num = Dwarf.abi.ipRegNum(native_arch).?;
...@@ -154,7 +153,6 @@ pub const UnwindContext = struct {...@@ -154,7 +153,6 @@ pub const UnwindContext = struct {
154 const pc = stripInstructionPtrAuthCode(raw_pc_ptr.*);153 const pc = stripInstructionPtrAuthCode(raw_pc_ptr.*);
155154
156 return .{155 return .{
157 .gpa = gpa,
158 .cfa = null,156 .cfa = null,
159 .pc = pc,157 .pc = pc,
160 .thread_context = thread_context,158 .thread_context = thread_context,
...@@ -164,19 +162,20 @@ pub const UnwindContext = struct {...@@ -164,19 +162,20 @@ pub const UnwindContext = struct {
164 };162 };
165 }163 }
166164
167 pub fn deinit(self: *UnwindContext) void {165 pub fn deinit(self: *DwarfUnwindContext, gpa: Allocator) void {
168 self.vm.deinit(self.gpa);166 self.vm.deinit(gpa);
169 self.stack_machine.deinit(self.gpa);167 self.stack_machine.deinit(gpa);
170 self.* = undefined;168 self.* = undefined;
171 }169 }
172170
173 pub fn getFp(self: *const UnwindContext) !usize {171 pub fn getFp(self: *const DwarfUnwindContext) !usize {
174 return (try regValueNative(self.thread_context, Dwarf.abi.fpRegNum(native_arch, self.reg_context), self.reg_context)).*;172 return (try regValueNative(self.thread_context, Dwarf.abi.fpRegNum(native_arch, self.reg_context), self.reg_context)).*;
175 }173 }
176174
177 /// Resolves the register rule and places the result into `out` (see regBytes)175 /// Resolves the register rule and places the result into `out` (see regBytes)
178 pub fn resolveRegisterRule(176 pub fn resolveRegisterRule(
179 context: *UnwindContext,177 context: *DwarfUnwindContext,
178 gpa: Allocator,
180 col: Dwarf.Unwind.VirtualMachine.Column,179 col: Dwarf.Unwind.VirtualMachine.Column,
181 expression_context: std.debug.Dwarf.expression.Context,180 expression_context: std.debug.Dwarf.expression.Context,
182 out: []u8,181 out: []u8,
...@@ -224,7 +223,7 @@ pub const UnwindContext = struct {...@@ -224,7 +223,7 @@ pub const UnwindContext = struct {
224 },223 },
225 .expression => |expression| {224 .expression => |expression| {
226 context.stack_machine.reset();225 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.?);
228 const addr = if (value) |v| blk: {227 const addr = if (value) |v| blk: {
229 if (v != .generic) return error.InvalidExpressionValue;228 if (v != .generic) return error.InvalidExpressionValue;
230 break :blk v.generic;229 break :blk v.generic;
...@@ -235,7 +234,7 @@ pub const UnwindContext = struct {...@@ -235,7 +234,7 @@ pub const UnwindContext = struct {
235 },234 },
236 .val_expression => |expression| {235 .val_expression => |expression| {
237 context.stack_machine.reset();236 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.?);
239 if (value) |v| {238 if (value) |v| {
240 if (v != .generic) return error.InvalidExpressionValue;239 if (v != .generic) return error.InvalidExpressionValue;
241 mem.writeInt(usize, out[0..@sizeOf(usize)], v.generic, native_endian);240 mem.writeInt(usize, out[0..@sizeOf(usize)], v.generic, native_endian);
...@@ -252,13 +251,14 @@ pub const UnwindContext = struct {...@@ -252,13 +251,14 @@ pub const UnwindContext = struct {
252 /// may require lazily loading the data in those sections.251 /// may require lazily loading the data in those sections.
253 ///252 ///
254 /// `explicit_fde_offset` is for cases where the FDE offset is known, such as when __unwind_info253 /// `explicit_fde_offset` is for cases where the FDE offset is known, such as when __unwind_info
255 pub fn unwindFrameDwarf(254 pub fn unwindFrame(
256 context: *UnwindContext,255 context: *DwarfUnwindContext,
256 gpa: Allocator,
257 unwind: *const Dwarf.Unwind,257 unwind: *const Dwarf.Unwind,
258 load_offset: usize,258 load_offset: usize,
259 explicit_fde_offset: ?usize,259 explicit_fde_offset: ?usize,
260 ) Error!usize {260 ) 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) {
262 error.InvalidDebugInfo, error.MissingDebugInfo, error.OutOfMemory => |e| return e,262 error.InvalidDebugInfo, error.MissingDebugInfo, error.OutOfMemory => |e| return e,
263263
264 error.UnimplementedArch,264 error.UnimplementedArch,
...@@ -302,8 +302,9 @@ pub const UnwindContext = struct {...@@ -302,8 +302,9 @@ pub const UnwindContext = struct {
302 => return error.InvalidDebugInfo,302 => return error.InvalidDebugInfo,
303 };303 };
304 }304 }
305 fn unwindFrameDwarfInner(305 fn unwindFrameInner(
306 context: *UnwindContext,306 context: *DwarfUnwindContext,
307 gpa: Allocator,
307 unwind: *const Dwarf.Unwind,308 unwind: *const Dwarf.Unwind,
308 load_offset: usize,309 load_offset: usize,
309 explicit_fde_offset: ?usize,310 explicit_fde_offset: ?usize,
...@@ -338,7 +339,7 @@ pub const UnwindContext = struct {...@@ -338,7 +339,7 @@ pub const UnwindContext = struct {
338 context.reg_context.eh_frame = cie.version != 4;339 context.reg_context.eh_frame = cie.version != 4;
339 context.reg_context.is_macho = native_os.isDarwin();340 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);
342 context.cfa = switch (row.cfa.rule) {343 context.cfa = switch (row.cfa.rule) {
343 .val_offset => |offset| blk: {344 .val_offset => |offset| blk: {
344 const register = row.cfa.register orelse return error.InvalidCFARule;345 const register = row.cfa.register orelse return error.InvalidCFARule;
...@@ -349,7 +350,7 @@ pub const UnwindContext = struct {...@@ -349,7 +350,7 @@ pub const UnwindContext = struct {
349 context.stack_machine.reset();350 context.stack_machine.reset();
350 const value = try context.stack_machine.run(351 const value = try context.stack_machine.run(
351 expr,352 expr,
352 context.gpa,353 gpa,
353 expression_context,354 expression_context,
354 context.cfa,355 context.cfa,
355 );356 );
...@@ -366,7 +367,7 @@ pub const UnwindContext = struct {...@@ -366,7 +367,7 @@ pub const UnwindContext = struct {
366367
367 // Buffering the modifications is done because copying the thread context is not portable,368 // Buffering the modifications is done because copying the thread context is not portable,
368 // some implementations (ie. darwin) use internal pointers to the mcontext.369 // 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);
370 defer arena.deinit();371 defer arena.deinit();
371 const update_arena = arena.allocator();372 const update_arena = arena.allocator();
372373
...@@ -388,7 +389,7 @@ pub const UnwindContext = struct {...@@ -388,7 +389,7 @@ pub const UnwindContext = struct {
388389
389 const dest = try regBytes(context.thread_context, register, context.reg_context);390 const dest = try regBytes(context.thread_context, register, context.reg_context);
390 const src = try update_arena.alloc(u8, dest.len);391 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
393 const new_update = try update_arena.create(RegisterUpdate);394 const new_update = try update_arena.create(RegisterUpdate);
394 new_update.* = .{395 new_update.* = .{
lib/std/debug/SelfInfo/DarwinModule.zig+8-7
...@@ -255,7 +255,7 @@ pub const supports_unwinding: bool = true;...@@ -255,7 +255,7 @@ pub const supports_unwinding: bool = true;
255/// Unwind a frame using MachO compact unwind info (from __unwind_info).255/// Unwind a frame using MachO compact unwind info (from __unwind_info).
256/// If the compact encoding can't encode a way to unwind a frame, it will256/// If the compact encoding can't encode a way to unwind a frame, it will
257/// defer unwinding to DWARF, in which case `.eh_frame` will be used if available.257/// 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 {
259 return unwindFrameInner(module, gpa, di, context) catch |err| switch (err) {259 return unwindFrameInner(module, gpa, di, context) catch |err| switch (err) {
260 error.InvalidDebugInfo,260 error.InvalidDebugInfo,
261 error.MissingDebugInfo,261 error.MissingDebugInfo,
...@@ -274,8 +274,7 @@ pub fn unwindFrame(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo,...@@ -274,8 +274,7 @@ pub fn unwindFrame(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo,
274 => return error.InvalidDebugInfo,274 => return error.InvalidDebugInfo,
275 };275 };
276}276}
277fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo, context: *UnwindContext) !usize {277fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo, context: *DwarfUnwindContext) !usize {
278 _ = gpa;
279 if (di.unwind == null) di.unwind = module.loadUnwindInfo();278 if (di.unwind == null) di.unwind = module.loadUnwindInfo();
280 const unwind = &di.unwind.?;279 const unwind = &di.unwind.?;
281280
...@@ -505,7 +504,8 @@ fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo,...@@ -505,7 +504,8 @@ fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo,
505 .DWARF => {504 .DWARF => {
506 const eh_frame = unwind.eh_frame orelse return error.MissingDebugInfo;505 const eh_frame = unwind.eh_frame orelse return error.MissingDebugInfo;
507 const eh_frame_vaddr = @intFromPtr(eh_frame.ptr) - module.load_offset;506 const eh_frame_vaddr = @intFromPtr(eh_frame.ptr) - module.load_offset;
508 return context.unwindFrameDwarf(507 return context.unwindFrame(
508 gpa,
509 &.initSection(.eh_frame, eh_frame_vaddr, eh_frame),509 &.initSection(.eh_frame, eh_frame_vaddr, eh_frame),
510 module.load_offset,510 module.load_offset,
511 @intCast(encoding.value.x86_64.dwarf),511 @intCast(encoding.value.x86_64.dwarf),
...@@ -524,7 +524,8 @@ fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo,...@@ -524,7 +524,8 @@ fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo,
524 .DWARF => {524 .DWARF => {
525 const eh_frame = unwind.eh_frame orelse return error.MissingDebugInfo;525 const eh_frame = unwind.eh_frame orelse return error.MissingDebugInfo;
526 const eh_frame_vaddr = @intFromPtr(eh_frame.ptr) - module.load_offset;526 const eh_frame_vaddr = @intFromPtr(eh_frame.ptr) - module.load_offset;
527 return context.unwindFrameDwarf(527 return context.unwindFrame(
528 gpa,
528 &.initSection(.eh_frame, eh_frame_vaddr, eh_frame),529 &.initSection(.eh_frame, eh_frame_vaddr, eh_frame),
529 module.load_offset,530 module.load_offset,
530 @intCast(encoding.value.x86_64.dwarf),531 @intCast(encoding.value.x86_64.dwarf),
...@@ -574,7 +575,7 @@ fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo,...@@ -574,7 +575,7 @@ fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo,
574 else => comptime unreachable, // unimplemented575 else => comptime unreachable, // unimplemented
575 };576 };
576577
577 context.pc = UnwindContext.stripInstructionPtrAuthCode(new_ip);578 context.pc = DwarfUnwindContext.stripInstructionPtrAuthCode(new_ip);
578 if (context.pc > 0) context.pc -= 1;579 if (context.pc > 0) context.pc -= 1;
579 return new_ip;580 return new_ip;
580}581}
...@@ -819,7 +820,7 @@ const macho = std.macho;...@@ -819,7 +820,7 @@ const macho = std.macho;
819const mem = std.mem;820const mem = std.mem;
820const posix = std.posix;821const posix = std.posix;
821const testing = std.testing;822const testing = std.testing;
822const UnwindContext = std.debug.SelfInfo.UnwindContext;823const DwarfUnwindContext = std.debug.SelfInfo.DwarfUnwindContext;
823const Error = std.debug.SelfInfo.Error;824const Error = std.debug.SelfInfo.Error;
824const regBytes = Dwarf.abi.regBytes;825const regBytes = Dwarf.abi.regBytes;
825const regValueNative = Dwarf.abi.regValueNative;826const 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...@@ -193,12 +193,12 @@ fn loadUnwindInfo(module: *const ElfModule, gpa: Allocator, di: *DebugInfo) Erro
193 else => unreachable,193 else => unreachable,
194 }194 }
195}195}
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 {
197 if (di.unwind[0] == null) try module.loadUnwindInfo(gpa, di);197 if (di.unwind[0] == null) try module.loadUnwindInfo(gpa, di);
198 std.debug.assert(di.unwind[0] != null);198 std.debug.assert(di.unwind[0] != null);
199 for (&di.unwind) |*opt_unwind| {199 for (&di.unwind) |*opt_unwind| {
200 const unwind = &(opt_unwind.* orelse break);200 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) {
202 error.MissingDebugInfo => continue, // try the next one202 error.MissingDebugInfo => continue, // try the next one
203 else => |e| return e,203 else => |e| return e,
204 };204 };
...@@ -233,7 +233,7 @@ const Allocator = std.mem.Allocator;...@@ -233,7 +233,7 @@ const Allocator = std.mem.Allocator;
233const Dwarf = std.debug.Dwarf;233const Dwarf = std.debug.Dwarf;
234const elf = std.elf;234const elf = std.elf;
235const mem = std.mem;235const mem = std.mem;
236const UnwindContext = std.debug.SelfInfo.UnwindContext;236const DwarfUnwindContext = std.debug.SelfInfo.DwarfUnwindContext;
237const Error = std.debug.SelfInfo.Error;237const Error = std.debug.SelfInfo.Error;
238238
239const builtin = @import("builtin");239const builtin = @import("builtin");