std.debug: improve the APIs and stuff

This commit is contained in:
mlugg 2025-09-05 19:43:08 +01:00
parent d4f710791f
commit 5709369d05
No known key found for this signature in database
GPG Key ID: 3F5B7DCCBF4AF02E
4 changed files with 457 additions and 676 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1449,7 +1449,7 @@ fn getStringGeneric(opt_str: ?[]const u8, offset: u64) ![:0]const u8 {
pub fn getSymbol(di: *Dwarf, allocator: Allocator, endian: Endian, address: u64) !std.debug.Symbol { pub fn getSymbol(di: *Dwarf, allocator: Allocator, endian: Endian, address: u64) !std.debug.Symbol {
const compile_unit = di.findCompileUnit(endian, address) catch |err| switch (err) { const compile_unit = di.findCompileUnit(endian, address) catch |err| switch (err) {
error.MissingDebugInfo, error.InvalidDebugInfo => return .{ .name = null, .compile_unit_name = null, .source_location = null }, error.MissingDebugInfo, error.InvalidDebugInfo => return .unknown,
else => return err, else => return err,
}; };
return .{ return .{

View File

@ -158,7 +158,7 @@ test {
} }
pub const UnwindContext = struct { pub const UnwindContext = struct {
gpa: Allocator, 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,
@ -166,22 +166,20 @@ 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(gpa: Allocator, thread_context: *std.debug.ThreadContext) UnwindContext {
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).?;
const pc = stripInstructionPtrAuthCode( const raw_pc_ptr = regValueNative(thread_context, ip_reg_num, null) catch {
(try regValueNative(thread_context, ip_reg_num, null)).*, unreachable; // error means unsupported, in which case `supports_unwinding` should have been `false`
); };
const pc = stripInstructionPtrAuthCode(raw_pc_ptr.*);
const context_copy = try gpa.create(std.debug.ThreadContext);
std.debug.copyContext(thread_context, context_copy);
return .{ return .{
.gpa = gpa, .gpa = gpa,
.cfa = null, .cfa = null,
.pc = pc, .pc = pc,
.thread_context = context_copy, .thread_context = thread_context,
.reg_context = undefined, .reg_context = undefined,
.vm = .{}, .vm = .{},
.stack_machine = .{}, .stack_machine = .{},
@ -191,7 +189,6 @@ pub const UnwindContext = struct {
pub fn deinit(self: *UnwindContext) void { pub fn deinit(self: *UnwindContext) void {
self.vm.deinit(self.gpa); self.vm.deinit(self.gpa);
self.stack_machine.deinit(self.gpa); self.stack_machine.deinit(self.gpa);
self.gpa.destroy(self.thread_context);
self.* = undefined; self.* = undefined;
} }

View File

@ -196,11 +196,7 @@ pub fn getSymbolAtAddress(module: *const DarwinModule, gpa: Allocator, di: *Debu
const full = &di.full.?; const full = &di.full.?;
const vaddr = address - module.load_offset; const vaddr = address - module.load_offset;
const symbol = MachoSymbol.find(full.symbols, vaddr) orelse return .{ const symbol = MachoSymbol.find(full.symbols, vaddr) orelse return .unknown;
.name = null,
.compile_unit_name = null,
.source_location = null,
};
// offset of `address` from start of `symbol` // offset of `address` from start of `symbol`
const address_symbol_offset = vaddr - symbol.addr; const address_symbol_offset = vaddr - symbol.addr;