const std = @import("std"); const builtin = @import("builtin"); const Allocator = std.mem.Allocator; pub const CallerInfo = struct { file_name: []const u8, line_number: u64, pub fn deinit(self: CallerInfo) void { std.heap.page_allocator.free(self.file_name); } }; pub fn getCallerInfo(address: usize) !?CallerInfo { if (comptime builtin.mode != .debug) return null; const debug_info = try std.debug.getSelfDebugInfo(); const io = std.Options.debug_io; var text_arena: std.heap.ArenaAllocator = .init(std.heap.page_allocator); defer text_arena.deinit(); var symbols: std.ArrayList(std.debug.Symbol) = .empty; defer symbols.deinit(std.heap.page_allocator); debug_info.getSymbols( io, std.heap.page_allocator, text_arena.allocator(), address, false, &symbols, ) catch |err| switch (err) { error.MissingDebugInfo, error.InvalidDebugInfo => return null, else => return err, }; if (symbols.items.len == 0) return null; const source_location = symbols.items[0].source_location orelse return null; return .{ .file_name = try std.heap.page_allocator.dupe(u8, source_location.file_name), .line_number = source_location.line, }; }