Mik*_*ffe 27 linux heap malloc gdb x86-64
我正在尝试从Linux amd64上的gdb中调查C/C++堆的状态,有一个很好的方法吗?
我尝试过的一种方法是"调用mallinfo()"但不幸的是我无法提取我想要的值,因为gdb没有正确处理返回值.
我不能轻易地编写一个函数来编译成我所连接的进程的二进制文件,所以我可以通过这种方式在我自己的代码中调用mallinfo()来实现我自己的函数来提取值.是否有一个聪明的技巧可以让我在飞行中做到这一点?
另一种选择可能是找到堆并遍历malloc头文件/空闲列表; 我非常感谢能够找到这些位置和布局的任何指示.
我一直在尝试谷歌并阅读约2个小时的问题,我已经学到了一些有趣的东西,但仍然找不到我需要的东西.
lee*_*777 28
@fd - RedHat bug有你的答案.
该mallinfo函数已弃用,不会更新.真正的查询统计API是TDB.今天,你有malloc_stats和malloc_info.我找不到任何一个文档,但这是他们给你的.
这是否足够接近您的需求?
(gdb) call malloc_stats()
Arena 0:
system bytes = 135168
in use bytes = 96
Total (incl. mmap):
system bytes = 135168
in use bytes = 96
max mmap regions = 0
max mmap bytes = 0
(gdb) call malloc_info(0, stdout)
<malloc version="1">
<heap nr="0">
<sizes>
<unsorted from="1228788" to="1229476" total="3917678" count="3221220448"/>
</sizes>
<total type="fast" count="0" size="0"/>
<total type="rest" count="3221220448" size="3917678"/>
<system type="current" size="135168"/>
<system type="max" size="135168"/>
<aspace type="total" size="135168"/>
<aspace type="mprotect" size="135168"/>
</heap>
<total type="fast" count="0" size="0"/>
<total type="rest" count="3221220448" size="3917678"/>
<system type="current" size="135168
/>
<system type="max" size="135168
/>
<aspace type="total" size="135168"/>
<aspace type="mprotect" size="135168"/>
</malloc>
Run Code Online (Sandbox Code Playgroud)
如果您可以更改代码:
#include <malloc.h>
#include <stdio.h>
void dumpMallinfo(void) {
struct mallinfo m = mallinfo();
printf("uordblks = %d\nfordblks = %d\n", m.uordblks, m.fordblks);
}
Run Code Online (Sandbox Code Playgroud)
在 GDB 中,您可以call dumpMallinfo().