master
1const std = @import("std");
2const fatal = std.process.fatal;
3
4extern fn add(a: u32, b: u32, addr: *usize) u32;
5
6pub fn main() void {
7 var debug_alloc_inst: std.heap.DebugAllocator(.{}) = .init;
8 defer std.debug.assert(debug_alloc_inst.deinit() == .ok);
9 const gpa = debug_alloc_inst.allocator();
10
11 var di: std.debug.SelfInfo = .init;
12 defer di.deinit(gpa);
13
14 var threaded: std.Io.Threaded = .init(gpa);
15 defer threaded.deinit();
16 const io = threaded.io();
17
18 var add_addr: usize = undefined;
19 _ = add(1, 2, &add_addr);
20
21 const symbol = di.getSymbol(gpa, io, add_addr) catch |err| fatal("failed to get symbol: {t}", .{err});
22 defer if (symbol.source_location) |sl| gpa.free(sl.file_name);
23
24 if (symbol.name == null) fatal("failed to resolve symbol name", .{});
25 if (symbol.compile_unit_name == null) fatal("failed to resolve compile unit", .{});
26 if (symbol.source_location == null) fatal("failed to resolve source location", .{});
27
28 if (!std.mem.eql(u8, symbol.name.?, "add")) {
29 fatal("incorrect symbol name '{s}'", .{symbol.name.?});
30 }
31 const sl = &symbol.source_location.?;
32 if (!std.mem.eql(u8, std.fs.path.basename(sl.file_name), "shared_lib.c")) {
33 fatal("incorrect file name '{s}'", .{sl.file_name});
34 }
35 if (sl.line != 3 or sl.column != 0) {
36 fatal("incorrect line/column :{d}:{d}", .{ sl.line, sl.column });
37 }
38}