Skip to content

Instantly share code, notes, and snippets.

@nektro
Created October 20, 2022 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nektro/f363b7e6d72885e6d7b9a57e76c47c12 to your computer and use it in GitHub Desktop.
Save nektro/f363b7e6d72885e6d7b9a57e76c47c12 to your computer and use it in GitHub Desktop.
const std = @import("std");
const builtin = @import("builtin");
pub fn main() !void {
try dbg("{d}", 24);
}
fn dbg(comptime fmt: []const u8, x: anytype) !void {
comptime std.debug.assert(builtin.mode == .Debug);
const stderr = std.io.getStdErr().writer();
const debug_info = try std.debug.getSelfDebugInfo();
var it = std.debug.StackIterator.init(@returnAddress(), null);
const address = it.next().?;
const module = try debug_info.getModuleForAddress(address);
const symbol_info = try module.getSymbolAtAddress(debug_info.allocator, address);
defer symbol_info.deinit(debug_info.allocator);
if (symbol_info.line_info) |li| {
try stderr.print("[{s}:{d}:{d}]: ", .{ li.file_name, li.line, li.column });
} else {
try stderr.writeAll("[???:?:?]: ");
}
try stderr.print(fmt, .{x});
try stderr.writeAll("\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment