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