diff options
| author | mlugg <mlugg@mlugg.co.uk> | 2025-09-08 11:38:25 +0100 |
|---|---|---|
| committer | mlugg <mlugg@mlugg.co.uk> | 2025-09-30 13:44:51 +0100 |
| commit | 253fdfce7064a6ebf70dbb62b465d6564eac0948 (patch) | |
| tree | 02bd4ff3c8cc560e94325fdc3ba7383533b78101 /lib/std/debug | |
| parent | 9859440d83e5ef17d353be39f32f2dc0b9ce0e02 (diff) | |
| download | zig-253fdfce7064a6ebf70dbb62b465d6564eac0948.tar.gz zig-253fdfce7064a6ebf70dbb62b465d6564eac0948.zip | |
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.
Diffstat (limited to 'lib/std/debug')
| -rw-r--r-- | lib/std/debug/SelfInfo.zig | 45 | ||||
| -rw-r--r-- | lib/std/debug/SelfInfo/DarwinModule.zig | 15 | ||||
| -rw-r--r-- | lib/std/debug/SelfInfo/ElfModule.zig | 6 |
3 files changed, 34 insertions, 32 deletions
diff --git a/lib/std/debug/SelfInfo.zig b/lib/std/debug/SelfInfo.zig index 93fa7a6045..f1832f4902 100644 --- a/lib/std/debug/SelfInfo.zig +++ b/lib/std/debug/SelfInfo.zig @@ -53,7 +53,7 @@ pub fn deinit(self: *SelfInfo, gpa: Allocator) void { 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); const module: Module = try .lookup(&self.lookup_cache, gpa, context.pc); 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, /// gpa: Allocator, /// di: *DebugInfo, -/// ctx: *SelfInfo.UnwindContext, +/// ctx: *SelfInfo.DwarfUnwindContext, /// ) SelfInfo.Error!usize; /// ``` const Module: type = Module: { @@ -135,8 +135,7 @@ const Module: type = Module: { }; }; -pub const UnwindContext = struct { - gpa: Allocator, // MLUGG TODO: make unmanaged (also maybe rename this type, DwarfUnwindContext or smth idk) +pub const DwarfUnwindContext = struct { cfa: ?usize, pc: usize, thread_context: *std.debug.ThreadContext, @@ -144,7 +143,7 @@ pub const UnwindContext = struct { vm: Dwarf.Unwind.VirtualMachine, 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); const ip_reg_num = Dwarf.abi.ipRegNum(native_arch).?; @@ -154,7 +153,6 @@ pub const UnwindContext = struct { const pc = stripInstructionPtrAuthCode(raw_pc_ptr.*); return .{ - .gpa = gpa, .cfa = null, .pc = pc, .thread_context = thread_context, @@ -164,19 +162,20 @@ pub const UnwindContext = struct { }; } - pub fn deinit(self: *UnwindContext) void { - self.vm.deinit(self.gpa); - self.stack_machine.deinit(self.gpa); + pub fn deinit(self: *DwarfUnwindContext, gpa: Allocator) void { + self.vm.deinit(gpa); + self.stack_machine.deinit(gpa); 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)).*; } /// Resolves the register rule and places the result into `out` (see regBytes) pub fn resolveRegisterRule( - context: *UnwindContext, + context: *DwarfUnwindContext, + gpa: Allocator, col: Dwarf.Unwind.VirtualMachine.Column, expression_context: std.debug.Dwarf.expression.Context, out: []u8, @@ -224,7 +223,7 @@ pub const UnwindContext = struct { }, .expression => |expression| { 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: { if (v != .generic) return error.InvalidExpressionValue; break :blk v.generic; @@ -235,7 +234,7 @@ pub const UnwindContext = struct { }, .val_expression => |expression| { 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 (v != .generic) return error.InvalidExpressionValue; 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. /// /// `explicit_fde_offset` is for cases where the FDE offset is known, such as when __unwind_info - pub fn unwindFrameDwarf( - context: *UnwindContext, + pub fn unwindFrame( + context: *DwarfUnwindContext, + gpa: Allocator, unwind: *const Dwarf.Unwind, load_offset: usize, explicit_fde_offset: ?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.UnimplementedArch, @@ -302,8 +302,9 @@ pub const UnwindContext = struct { => return error.InvalidDebugInfo, }; } - fn unwindFrameDwarfInner( - context: *UnwindContext, + fn unwindFrameInner( + context: *DwarfUnwindContext, + gpa: Allocator, unwind: *const Dwarf.Unwind, load_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.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) { .val_offset => |offset| blk: { const register = row.cfa.register orelse return error.InvalidCFARule; @@ -349,7 +350,7 @@ pub const UnwindContext = struct { context.stack_machine.reset(); const value = try context.stack_machine.run( expr, - context.gpa, + gpa, expression_context, context.cfa, ); @@ -366,7 +367,7 @@ pub const UnwindContext = struct { // Buffering the modifications is done because copying the thread context is not portable, // 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(); 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 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); new_update.* = .{ diff --git a/lib/std/debug/SelfInfo/DarwinModule.zig b/lib/std/debug/SelfInfo/DarwinModule.zig index 4f94798c63..ce15aed95c 100644 --- a/lib/std/debug/SelfInfo/DarwinModule.zig +++ b/lib/std/debug/SelfInfo/DarwinModule.zig @@ -255,7 +255,7 @@ pub const supports_unwinding: bool = true; /// 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 /// 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) { error.InvalidDebugInfo, error.MissingDebugInfo, @@ -274,8 +274,7 @@ pub fn unwindFrame(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo, => return error.InvalidDebugInfo, }; } -fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo, context: *UnwindContext) !usize { - _ = gpa; +fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo, context: *DwarfUnwindContext) !usize { if (di.unwind == null) di.unwind = module.loadUnwindInfo(); const unwind = &di.unwind.?; @@ -505,7 +504,8 @@ fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo, .DWARF => { const eh_frame = unwind.eh_frame orelse return error.MissingDebugInfo; 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), module.load_offset, @intCast(encoding.value.x86_64.dwarf), @@ -524,7 +524,8 @@ fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo, .DWARF => { const eh_frame = unwind.eh_frame orelse return error.MissingDebugInfo; 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), module.load_offset, @intCast(encoding.value.x86_64.dwarf), @@ -574,7 +575,7 @@ fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo, else => comptime unreachable, // unimplemented }; - context.pc = UnwindContext.stripInstructionPtrAuthCode(new_ip); + context.pc = DwarfUnwindContext.stripInstructionPtrAuthCode(new_ip); if (context.pc > 0) context.pc -= 1; return new_ip; } @@ -819,7 +820,7 @@ const macho = std.macho; const mem = std.mem; const posix = std.posix; const testing = std.testing; -const UnwindContext = std.debug.SelfInfo.UnwindContext; +const DwarfUnwindContext = std.debug.SelfInfo.DwarfUnwindContext; const Error = std.debug.SelfInfo.Error; const regBytes = Dwarf.abi.regBytes; const regValueNative = Dwarf.abi.regValueNative; diff --git a/lib/std/debug/SelfInfo/ElfModule.zig b/lib/std/debug/SelfInfo/ElfModule.zig index b58125031e..ff37e283b9 100644 --- a/lib/std/debug/SelfInfo/ElfModule.zig +++ b/lib/std/debug/SelfInfo/ElfModule.zig @@ -193,12 +193,12 @@ fn loadUnwindInfo(module: *const ElfModule, gpa: Allocator, di: *DebugInfo) Erro 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); std.debug.assert(di.unwind[0] != null); for (&di.unwind) |*opt_unwind| { 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 else => |e| return e, }; @@ -233,7 +233,7 @@ const Allocator = std.mem.Allocator; const Dwarf = std.debug.Dwarf; const elf = std.elf; const mem = std.mem; -const UnwindContext = std.debug.SelfInfo.UnwindContext; +const DwarfUnwindContext = std.debug.SelfInfo.DwarfUnwindContext; const Error = std.debug.SelfInfo.Error; const builtin = @import("builtin"); |
