Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I log the stack during an allocation on Linux?

On OS X, setting the MallocStackLogging environment variable before running a program will cause malloc(3) and free(3) to record the stack each time they are called for each memory region. This is particularly useful for debugging memory corruption issues, as you can go back through the history of the region that has been corrupted and figure out what part of the code was/should have been responsible for the memory.

I understand that I could use __malloc_hook on Linux to write my own implementation of this debugging facility, but I wonder if there isn't some easier way to accomplish the same thing? What's the recommended way to track allocations on Linux?

like image 711
Josh Avatar asked Nov 19 '25 13:11

Josh


1 Answers

I have never heard of that feature. If it weren't possible, though, you could still wrap them around some function which would accomplish that.

Or, consider a memory analyzer (heap-user-after-free, memory leak, buffer overflow and so on) such as valgrind or AddressSanitizer.

like image 145
edmz Avatar answered Nov 22 '25 02:11

edmz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!