Not sure how to dealloc after accessing the symbol from Module using Address

Hey! First time poster here, loving zig so far.

Question?
I’m building a tracker for allocations and would like to log the src of the allocations, now of course I could just create a wrapper around the std.mem.Allocator, and pass @src(), but I was curious if I could adjust the std.mem.Allocator, vtable and then do some magic on the inside to log the src of the alloc call. That way I can expose a simple allocator, that can be passed around by the user.

When attempting to get the info I did the following:

  const debug_info = try std.debug.getSelfDebugInfo() ;
  const module = try debug_info.getModuleForAddress(addr) ;
  const symbol = try module.getSymbolAtAddress(allocator, addr) ;
  const src = try symbol.source_location;

The problem I’m having at the moment is how do I dealloc the module, running module.deinit() does not dealloc the dwarf and func_list, ect… There seems to be an enormous amount of allocations and creations of slices ect, when I call const symbol = try module.getSymbolAtAddress(allocator, addr) ;.

At the end of the day I just want to log something like this
Alloc […] 960 bytes | src/main.zig:50

Can anyone help me with this, the goal is to just slightly adjust the vtable of the std.mem.Allocator, so that I can expose the allocator interface to a user with this custom functionality.

Appreciate all the help and insights, thanks again!

Hello @vic-Rokx

You don’t have to change the Allocator vtable for that.
Zig calls @returnAddress() to get the caller’s address and pass it for each call to the Allocator interface.
You can use printSourceAtAddress to print the filename, line number and the source code line: How to print caller's file&line number at runtime? - #2 by dimdin

Welcome to ziggit :slight_smile:

5 Likes

Thanks so much for the fast reply! and solution, definitely way simpler than what i was doing haha