mlugg 1120546f72
std.debug.SelfInfo: remove shared logic
There were only a few dozen lines of common logic, and they frankly
introduced more complexity than they eliminated. Instead, let's accept
that the implementations of `SelfInfo` are all pretty different and want
to track different state. This probably fixes some synchronization and
memory bugs by simplifying a bunch of stuff. It also improves the DWARF
unwind cache, making it around twice as fast in a debug build with the
self-hosted x86_64 backend, because we no longer have to redundantly go
through the hashmap lookup logic to find the module. Unwinding on
Windows will also see a slight performance boost from this change,
because `RtlVirtualUnwind` does not need to know the module whatsoever,
so the old `SelfInfo` implementation was doing redundant work. Lastly,
this makes it even easier to implement `SelfInfo` on freestanding
targets; there is no longer a need to emulate a real module system,
since the user controls the whole implementation!

There are various other small refactors here in the `SelfInfo`
implementations as well as in the DWARF unwinding logic. This change
turned out to make a lot of stuff simpler!
2025-09-30 14:18:26 +01:00

35 lines
1.3 KiB
Zig

const std = @import("std");
const fatal = std.process.fatal;
extern fn add(a: u32, b: u32, addr: *usize) u32;
pub fn main() void {
var debug_alloc_inst: std.heap.DebugAllocator(.{}) = .init;
defer std.debug.assert(debug_alloc_inst.deinit() == .ok);
const gpa = debug_alloc_inst.allocator();
var di: std.debug.SelfInfo = .init;
defer di.deinit(gpa);
var add_addr: usize = undefined;
_ = add(1, 2, &add_addr);
const symbol = di.getSymbol(gpa, add_addr) catch |err| fatal("failed to get symbol: {t}", .{err});
defer if (symbol.source_location) |sl| gpa.free(sl.file_name);
if (symbol.name == null) fatal("failed to resolve symbol name", .{});
if (symbol.compile_unit_name == null) fatal("failed to resolve compile unit", .{});
if (symbol.source_location == null) fatal("failed to resolve source location", .{});
if (!std.mem.eql(u8, symbol.name.?, "add")) {
fatal("incorrect symbol name '{s}'", .{symbol.name.?});
}
const sl = &symbol.source_location.?;
if (!std.mem.eql(u8, std.fs.path.basename(sl.file_name), "shared_lib.c")) {
fatal("incorrect file name '{s}'", .{sl.file_name});
}
if (sl.line != 3 or sl.column != 0) {
fatal("incorrect line/column :{d}:{d}", .{ sl.line, sl.column });
}
}