简体   繁体   English

C++。 如何在 linux 中跟踪 memory 分配给.so 模块

[英]C++. How to trace memory allocation to .so module in linux

We are in a situation that have a large application and now there is a situation that would need to know which.so module is allocating how much memory.我们处于一个应用程序很大的情况,现在需要知道 which.so 模块分配了多少 memory。 I have no idea here, I was wondering a custom allocator overriding operator new, but that didn't help because I still cannot trace which module is doing to allocation.我不知道这里,我想知道一个自定义分配器覆盖 operator new,但这没有帮助,因为我仍然无法跟踪哪个模块正在执行分配。 Replacing new by custom allocator would be terrible amount of work.用自定义分配器替换新的将是可怕的工作量。 Does anyone know how can I tell which module is doing how much allocations?有谁知道我怎么知道哪个模块正在分配多少?

It's not easy.这并不容易。

You can hook malloc , free , realloc globally in the application.您可以在应用程序中全局挂钩mallocfreerealloc Corresponding articles on Stackoverflow: How to use __malloc_hook? Stackoverflow上的对应文章: 如何使用__malloc_hook? , An alternative for the deprecated __malloc_hook functionality of glibc . , glibc 已弃用的 __malloc_hook 功能的替代方案

You can retrieve a caller address from that hooks using __builtin_return_address and compare it with addresses of shared libraries.您可以使用__builtin_return_address从该挂钩中检索调用方地址,并将其与共享库的地址进行比较。 Maybe you have to examine a deeper frame address to get a proper address in a library, not an address of libc++.也许您必须检查更深的帧地址才能在库中获得正确的地址,而不是 libc++ 的地址。 Read this Stackoverflow article __builtin_return_address returns null for index >0?阅读这篇 Stackoverflow 文章__builtin_return_address 为索引 >0 返回 null? . .

You could make use of the LD_PRELOAD trick to hook malloc , realloc , free etc. That, combined with the info gleaned from boost.stacktrace would get you most (if not all) of what you need.您可以利用LD_PRELOAD技巧来挂钩mallocreallocfree等。结合从boost.stacktrace收集的信息,您可以得到大部分(如果不是全部)您需要的东西。 Not trivial though.虽然不是微不足道的。

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

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