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.
This commit is contained in:
mlugg 2025-09-08 11:38:25 +01:00
parent 9859440d83
commit 253fdfce70
No known key found for this signature in database
GPG key ID: 3F5B7DCCBF4AF02E
4 changed files with 41 additions and 36 deletions

View file

@ -747,7 +747,7 @@ pub fn dumpStackTrace(st: *const std.builtin.StackTrace) void {
const StackIterator = union(enum) { const StackIterator = union(enum) {
/// Unwinding using debug info (e.g. DWARF CFI). /// Unwinding using debug info (e.g. DWARF CFI).
di: if (SelfInfo.supports_unwinding) SelfInfo.UnwindContext else noreturn, di: if (SelfInfo.supports_unwinding) SelfInfo.DwarfUnwindContext else noreturn,
/// Naive frame-pointer-based unwinding. Very simple, but typically unreliable. /// Naive frame-pointer-based unwinding. Very simple, but typically unreliable.
fp: usize, fp: usize,
@ -766,17 +766,17 @@ const StackIterator = union(enum) {
if (context_opt) |context| { if (context_opt) |context| {
context_buf.* = context.*; context_buf.* = context.*;
relocateContext(context_buf); relocateContext(context_buf);
return .{ .di = .init(getDebugInfoAllocator(), context_buf) }; return .{ .di = .init(context_buf) };
} }
if (getContext(context_buf)) { if (getContext(context_buf)) {
return .{ .di = .init(getDebugInfoAllocator(), context_buf) }; return .{ .di = .init(context_buf) };
} }
return .{ .fp = @frameAddress() }; return .{ .fp = @frameAddress() };
} }
fn deinit(si: *StackIterator) void { fn deinit(si: *StackIterator) void {
switch (si.*) { switch (si.*) {
.fp => {}, .fp => {},
.di => |*unwind_context| unwind_context.deinit(), .di => |*unwind_context| unwind_context.deinit(getDebugInfoAllocator()),
} }
} }
@ -944,6 +944,9 @@ fn printLineInfo(
tty_config.setColor(writer, .reset) catch {}; tty_config.setColor(writer, .reset) catch {};
} }
try writer.writeAll("\n"); try writer.writeAll("\n");
} else |_| {
// Ignore all errors; it's a better UX to just print the source location without the
// corresponding line number. The user can always open the source file themselves.
} }
} }
} }

View file

@ -53,7 +53,7 @@ pub fn deinit(self: *SelfInfo, gpa: Allocator) void {
if (Module.LookupCache != void) self.lookup_cache.deinit(gpa); if (Module.LookupCache != void) self.lookup_cache.deinit(gpa);
} }
pub fn unwindFrame(self: *SelfInfo, gpa: Allocator, context: *UnwindContext) Error!usize { pub fn unwindFrame(self: *SelfInfo, gpa: Allocator, context: *DwarfUnwindContext) Error!usize {
comptime assert(supports_unwinding); comptime assert(supports_unwinding);
const module: Module = try .lookup(&self.lookup_cache, gpa, context.pc); const module: Module = try .lookup(&self.lookup_cache, gpa, context.pc);
const gop = try self.modules.getOrPut(gpa, module.key()); const gop = try self.modules.getOrPut(gpa, module.key());
@ -120,7 +120,7 @@ pub fn getModuleNameForAddress(self: *SelfInfo, gpa: Allocator, address: usize)
/// mod: *const Module, /// mod: *const Module,
/// gpa: Allocator, /// gpa: Allocator,
/// di: *DebugInfo, /// di: *DebugInfo,
/// ctx: *SelfInfo.UnwindContext, /// ctx: *SelfInfo.DwarfUnwindContext,
/// ) SelfInfo.Error!usize; /// ) SelfInfo.Error!usize;
/// ``` /// ```
const Module: type = Module: { const Module: type = Module: {
@ -135,8 +135,7 @@ const Module: type = Module: {
}; };
}; };
pub const UnwindContext = struct { pub const DwarfUnwindContext = struct {
gpa: Allocator, // MLUGG TODO: make unmanaged (also maybe rename this type, DwarfUnwindContext or smth idk)
cfa: ?usize, cfa: ?usize,
pc: usize, pc: usize,
thread_context: *std.debug.ThreadContext, thread_context: *std.debug.ThreadContext,
@ -144,7 +143,7 @@ pub const UnwindContext = struct {
vm: Dwarf.Unwind.VirtualMachine, vm: Dwarf.Unwind.VirtualMachine,
stack_machine: Dwarf.expression.StackMachine(.{ .call_frame_context = true }), stack_machine: Dwarf.expression.StackMachine(.{ .call_frame_context = true }),
pub fn init(gpa: Allocator, thread_context: *std.debug.ThreadContext) UnwindContext { pub fn init(thread_context: *std.debug.ThreadContext) DwarfUnwindContext {
comptime assert(supports_unwinding); comptime assert(supports_unwinding);
const ip_reg_num = Dwarf.abi.ipRegNum(native_arch).?; const ip_reg_num = Dwarf.abi.ipRegNum(native_arch).?;
@ -154,7 +153,6 @@ pub const UnwindContext = struct {
const pc = stripInstructionPtrAuthCode(raw_pc_ptr.*); const pc = stripInstructionPtrAuthCode(raw_pc_ptr.*);
return .{ return .{
.gpa = gpa,
.cfa = null, .cfa = null,
.pc = pc, .pc = pc,
.thread_context = thread_context, .thread_context = thread_context,
@ -164,19 +162,20 @@ pub const UnwindContext = struct {
}; };
} }
pub fn deinit(self: *UnwindContext) void { pub fn deinit(self: *DwarfUnwindContext, gpa: Allocator) void {
self.vm.deinit(self.gpa); self.vm.deinit(gpa);
self.stack_machine.deinit(self.gpa); self.stack_machine.deinit(gpa);
self.* = undefined; self.* = undefined;
} }
pub fn getFp(self: *const UnwindContext) !usize { pub fn getFp(self: *const DwarfUnwindContext) !usize {
return (try regValueNative(self.thread_context, Dwarf.abi.fpRegNum(native_arch, self.reg_context), self.reg_context)).*; return (try regValueNative(self.thread_context, Dwarf.abi.fpRegNum(native_arch, self.reg_context), self.reg_context)).*;
} }
/// Resolves the register rule and places the result into `out` (see regBytes) /// Resolves the register rule and places the result into `out` (see regBytes)
pub fn resolveRegisterRule( pub fn resolveRegisterRule(
context: *UnwindContext, context: *DwarfUnwindContext,
gpa: Allocator,
col: Dwarf.Unwind.VirtualMachine.Column, col: Dwarf.Unwind.VirtualMachine.Column,
expression_context: std.debug.Dwarf.expression.Context, expression_context: std.debug.Dwarf.expression.Context,
out: []u8, out: []u8,
@ -224,7 +223,7 @@ pub const UnwindContext = struct {
}, },
.expression => |expression| { .expression => |expression| {
context.stack_machine.reset(); context.stack_machine.reset();
const value = try context.stack_machine.run(expression, context.gpa, expression_context, context.cfa.?); const value = try context.stack_machine.run(expression, gpa, expression_context, context.cfa.?);
const addr = if (value) |v| blk: { const addr = if (value) |v| blk: {
if (v != .generic) return error.InvalidExpressionValue; if (v != .generic) return error.InvalidExpressionValue;
break :blk v.generic; break :blk v.generic;
@ -235,7 +234,7 @@ pub const UnwindContext = struct {
}, },
.val_expression => |expression| { .val_expression => |expression| {
context.stack_machine.reset(); context.stack_machine.reset();
const value = try context.stack_machine.run(expression, context.gpa, expression_context, context.cfa.?); const value = try context.stack_machine.run(expression, gpa, expression_context, context.cfa.?);
if (value) |v| { if (value) |v| {
if (v != .generic) return error.InvalidExpressionValue; if (v != .generic) return error.InvalidExpressionValue;
mem.writeInt(usize, out[0..@sizeOf(usize)], v.generic, native_endian); mem.writeInt(usize, out[0..@sizeOf(usize)], v.generic, native_endian);
@ -252,13 +251,14 @@ pub const UnwindContext = struct {
/// may require lazily loading the data in those sections. /// may require lazily loading the data in those sections.
/// ///
/// `explicit_fde_offset` is for cases where the FDE offset is known, such as when __unwind_info /// `explicit_fde_offset` is for cases where the FDE offset is known, such as when __unwind_info
pub fn unwindFrameDwarf( pub fn unwindFrame(
context: *UnwindContext, context: *DwarfUnwindContext,
gpa: Allocator,
unwind: *const Dwarf.Unwind, unwind: *const Dwarf.Unwind,
load_offset: usize, load_offset: usize,
explicit_fde_offset: ?usize, explicit_fde_offset: ?usize,
) Error!usize { ) Error!usize {
return unwindFrameDwarfInner(context, unwind, load_offset, explicit_fde_offset) catch |err| switch (err) { return unwindFrameInner(context, gpa, unwind, load_offset, explicit_fde_offset) catch |err| switch (err) {
error.InvalidDebugInfo, error.MissingDebugInfo, error.OutOfMemory => |e| return e, error.InvalidDebugInfo, error.MissingDebugInfo, error.OutOfMemory => |e| return e,
error.UnimplementedArch, error.UnimplementedArch,
@ -302,8 +302,9 @@ pub const UnwindContext = struct {
=> return error.InvalidDebugInfo, => return error.InvalidDebugInfo,
}; };
} }
fn unwindFrameDwarfInner( fn unwindFrameInner(
context: *UnwindContext, context: *DwarfUnwindContext,
gpa: Allocator,
unwind: *const Dwarf.Unwind, unwind: *const Dwarf.Unwind,
load_offset: usize, load_offset: usize,
explicit_fde_offset: ?usize, explicit_fde_offset: ?usize,
@ -338,7 +339,7 @@ pub const UnwindContext = struct {
context.reg_context.eh_frame = cie.version != 4; context.reg_context.eh_frame = cie.version != 4;
context.reg_context.is_macho = native_os.isDarwin(); context.reg_context.is_macho = native_os.isDarwin();
const row = try context.vm.runTo(context.gpa, context.pc - load_offset, cie, fde, @sizeOf(usize), native_endian); const row = try context.vm.runTo(gpa, context.pc - load_offset, cie, fde, @sizeOf(usize), native_endian);
context.cfa = switch (row.cfa.rule) { context.cfa = switch (row.cfa.rule) {
.val_offset => |offset| blk: { .val_offset => |offset| blk: {
const register = row.cfa.register orelse return error.InvalidCFARule; const register = row.cfa.register orelse return error.InvalidCFARule;
@ -349,7 +350,7 @@ pub const UnwindContext = struct {
context.stack_machine.reset(); context.stack_machine.reset();
const value = try context.stack_machine.run( const value = try context.stack_machine.run(
expr, expr,
context.gpa, gpa,
expression_context, expression_context,
context.cfa, context.cfa,
); );
@ -366,7 +367,7 @@ pub const UnwindContext = struct {
// Buffering the modifications is done because copying the thread context is not portable, // Buffering the modifications is done because copying the thread context is not portable,
// some implementations (ie. darwin) use internal pointers to the mcontext. // some implementations (ie. darwin) use internal pointers to the mcontext.
var arena: std.heap.ArenaAllocator = .init(context.gpa); var arena: std.heap.ArenaAllocator = .init(gpa);
defer arena.deinit(); defer arena.deinit();
const update_arena = arena.allocator(); const update_arena = arena.allocator();
@ -388,7 +389,7 @@ pub const UnwindContext = struct {
const dest = try regBytes(context.thread_context, register, context.reg_context); const dest = try regBytes(context.thread_context, register, context.reg_context);
const src = try update_arena.alloc(u8, dest.len); const src = try update_arena.alloc(u8, dest.len);
try context.resolveRegisterRule(column, expression_context, src); try context.resolveRegisterRule(gpa, column, expression_context, src);
const new_update = try update_arena.create(RegisterUpdate); const new_update = try update_arena.create(RegisterUpdate);
new_update.* = .{ new_update.* = .{

View file

@ -255,7 +255,7 @@ pub const supports_unwinding: bool = true;
/// Unwind a frame using MachO compact unwind info (from __unwind_info). /// Unwind a frame using MachO compact unwind info (from __unwind_info).
/// If the compact encoding can't encode a way to unwind a frame, it will /// If the compact encoding can't encode a way to unwind a frame, it will
/// defer unwinding to DWARF, in which case `.eh_frame` will be used if available. /// defer unwinding to DWARF, in which case `.eh_frame` will be used if available.
pub fn unwindFrame(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo, context: *UnwindContext) Error!usize { pub fn unwindFrame(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo, context: *DwarfUnwindContext) Error!usize {
return unwindFrameInner(module, gpa, di, context) catch |err| switch (err) { return unwindFrameInner(module, gpa, di, context) catch |err| switch (err) {
error.InvalidDebugInfo, error.InvalidDebugInfo,
error.MissingDebugInfo, error.MissingDebugInfo,
@ -274,8 +274,7 @@ pub fn unwindFrame(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo,
=> return error.InvalidDebugInfo, => return error.InvalidDebugInfo,
}; };
} }
fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo, context: *UnwindContext) !usize { fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo, context: *DwarfUnwindContext) !usize {
_ = gpa;
if (di.unwind == null) di.unwind = module.loadUnwindInfo(); if (di.unwind == null) di.unwind = module.loadUnwindInfo();
const unwind = &di.unwind.?; const unwind = &di.unwind.?;
@ -505,7 +504,8 @@ fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo,
.DWARF => { .DWARF => {
const eh_frame = unwind.eh_frame orelse return error.MissingDebugInfo; const eh_frame = unwind.eh_frame orelse return error.MissingDebugInfo;
const eh_frame_vaddr = @intFromPtr(eh_frame.ptr) - module.load_offset; const eh_frame_vaddr = @intFromPtr(eh_frame.ptr) - module.load_offset;
return context.unwindFrameDwarf( return context.unwindFrame(
gpa,
&.initSection(.eh_frame, eh_frame_vaddr, eh_frame), &.initSection(.eh_frame, eh_frame_vaddr, eh_frame),
module.load_offset, module.load_offset,
@intCast(encoding.value.x86_64.dwarf), @intCast(encoding.value.x86_64.dwarf),
@ -524,7 +524,8 @@ fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo,
.DWARF => { .DWARF => {
const eh_frame = unwind.eh_frame orelse return error.MissingDebugInfo; const eh_frame = unwind.eh_frame orelse return error.MissingDebugInfo;
const eh_frame_vaddr = @intFromPtr(eh_frame.ptr) - module.load_offset; const eh_frame_vaddr = @intFromPtr(eh_frame.ptr) - module.load_offset;
return context.unwindFrameDwarf( return context.unwindFrame(
gpa,
&.initSection(.eh_frame, eh_frame_vaddr, eh_frame), &.initSection(.eh_frame, eh_frame_vaddr, eh_frame),
module.load_offset, module.load_offset,
@intCast(encoding.value.x86_64.dwarf), @intCast(encoding.value.x86_64.dwarf),
@ -574,7 +575,7 @@ fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo,
else => comptime unreachable, // unimplemented else => comptime unreachable, // unimplemented
}; };
context.pc = UnwindContext.stripInstructionPtrAuthCode(new_ip); context.pc = DwarfUnwindContext.stripInstructionPtrAuthCode(new_ip);
if (context.pc > 0) context.pc -= 1; if (context.pc > 0) context.pc -= 1;
return new_ip; return new_ip;
} }
@ -819,7 +820,7 @@ const macho = std.macho;
const mem = std.mem; const mem = std.mem;
const posix = std.posix; const posix = std.posix;
const testing = std.testing; const testing = std.testing;
const UnwindContext = std.debug.SelfInfo.UnwindContext; const DwarfUnwindContext = std.debug.SelfInfo.DwarfUnwindContext;
const Error = std.debug.SelfInfo.Error; const Error = std.debug.SelfInfo.Error;
const regBytes = Dwarf.abi.regBytes; const regBytes = Dwarf.abi.regBytes;
const regValueNative = Dwarf.abi.regValueNative; const regValueNative = Dwarf.abi.regValueNative;

View file

@ -193,12 +193,12 @@ fn loadUnwindInfo(module: *const ElfModule, gpa: Allocator, di: *DebugInfo) Erro
else => unreachable, else => unreachable,
} }
} }
pub fn unwindFrame(module: *const ElfModule, gpa: Allocator, di: *DebugInfo, context: *UnwindContext) Error!usize { pub fn unwindFrame(module: *const ElfModule, gpa: Allocator, di: *DebugInfo, context: *DwarfUnwindContext) Error!usize {
if (di.unwind[0] == null) try module.loadUnwindInfo(gpa, di); if (di.unwind[0] == null) try module.loadUnwindInfo(gpa, di);
std.debug.assert(di.unwind[0] != null); std.debug.assert(di.unwind[0] != null);
for (&di.unwind) |*opt_unwind| { for (&di.unwind) |*opt_unwind| {
const unwind = &(opt_unwind.* orelse break); const unwind = &(opt_unwind.* orelse break);
return context.unwindFrameDwarf(unwind, module.load_offset, null) catch |err| switch (err) { return context.unwindFrame(gpa, unwind, module.load_offset, null) catch |err| switch (err) {
error.MissingDebugInfo => continue, // try the next one error.MissingDebugInfo => continue, // try the next one
else => |e| return e, else => |e| return e,
}; };
@ -233,7 +233,7 @@ const Allocator = std.mem.Allocator;
const Dwarf = std.debug.Dwarf; const Dwarf = std.debug.Dwarf;
const elf = std.elf; const elf = std.elf;
const mem = std.mem; const mem = std.mem;
const UnwindContext = std.debug.SelfInfo.UnwindContext; const DwarfUnwindContext = std.debug.SelfInfo.DwarfUnwindContext;
const Error = std.debug.SelfInfo.Error; const Error = std.debug.SelfInfo.Error;
const builtin = @import("builtin"); const builtin = @import("builtin");