简体   繁体   English

如何在Linux上分配期间记录堆栈?

[英]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. 在OS X上,在运行程序之前设置MallocStackLogging环境变量将导致malloc(3)free(3)在每次为每个内存区域调用时记录堆栈。 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? 我知道我可以在Linux上使用__malloc_hook来编写我自己的这个调试工具的实现,但我想知道是否有更简单的方法来完成同样的事情? What's the recommended way to track allocations on Linux? 在Linux上跟踪分配的推荐方法是什么?

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. 或者,考虑一个内存分析器(堆后用户释放,内存泄漏,缓冲区溢出等),例如valgrind或AddressSanitizer。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM