| ... | @@ -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 | } |
| 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 | 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 | /// ``` |
| 126 | const Module: type = Module: { | 126 | const Module: type = Module: { |
| ... | @@ -135,8 +135,7 @@ const Module: type = Module: { | ... | @@ -135,8 +135,7 @@ const Module: type = Module: { |
| 135 | }; | 135 | }; |
| 136 | }; | 136 | }; |
| 137 | | 137 | |
| 138 | pub const UnwindContext = struct { | 138 | pub 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 }), |
| 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 | comptime assert(supports_unwinding); | 147 | comptime assert(supports_unwinding); |
| 149 | | 148 | |
| 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.*); |
| 155 | | 154 | |
| 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 | } |
| 166 | | 164 | |
| 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 | } |
| 172 | | 170 | |
| 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 | } |
| 176 | | 174 | |
| 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_info | 253 | /// `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, |
| 263 | | 263 | |
| 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(); |
| 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 | 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 { |
| 366 | | 367 | |
| 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(); |
| 372 | | 373 | |
| ... | @@ -388,7 +389,7 @@ pub const UnwindContext = struct { | ... | @@ -388,7 +389,7 @@ pub const UnwindContext = struct { |
| 388 | | 389 | |
| 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); |
| 392 | | 393 | |
| 393 | const new_update = try update_arena.create(RegisterUpdate); | 394 | const new_update = try update_arena.create(RegisterUpdate); |
| 394 | new_update.* = .{ | 395 | new_update.* = .{ |