在gdb中检查C / C ++堆内存统计信息 [英] Examining C/C++ Heap memory statistics in gdb

查看:773
本文介绍了在gdb中检查C / C ++堆内存统计信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



一种方法是使用gdb在Linux amd64上调查C / C ++堆的状态。我试过的是调用mallinfo(),但不幸的是我不能提取我想要的值,因为gdb没有正确处理返回值。



我不能轻易地编写一个函数来编译到我附加到的进程的二进制文件中,所以我可以简单地实现我自己的函数,通过以这种方式在我自己的代码中调用mallinfo()来提取值。有没有什么聪明的窍门可以让我做到这一点?



另一个选择是找到堆并遍历malloc头文件/空闲列表;我很感激任何指向我可以从中找到这些位置和布局的指针。



我一直在尝试谷歌并且仔细阅读了大约2几小时,我学到了一些有趣的东西,但仍然没有找到我所需要的。

https://bugzilla.redhat.com/show_bug.cgi?id=173813#c4 =noreferrer> RedHat bug 有你的答案。



mallinfo 函数已被弃用,并且不会被更新。一个真正的查询统计API是TDB。今天,您有 malloc_stats malloc_info 。我找不到任何文件,但这是他们给你的。



这足够接近你需要的吗?

 (gdb)调用malloc_stats()
场景0:
系统字节= 135168
使用字节= 96
总数(包括mmap):
系统字节数= 135168
使用字节数= 96
最大值mmap区域= 0
最大值mmap字节数= 0

(gdb)调用malloc_info(0,stdout)
< malloc version =1>
< heap nr =0>
<大小>
< /大小>
< total type =fastcount =0size =0/>
< total type =restcount =3221220448size =3917678/>
< system type =currentsize =135168/>
< system type =maxsize =135168/>
< aspace type =totalsize =135168/>
< aspace type =mprotectsize =135168/>
< / heap>
< total type =fastcount =0size =0/>
< total type =restcount =3221220448size =3917678/>
< system type =currentsize =135168
/>
< system type =maxsize =135168
/>
< aspace type =totalsize =135168/>
< aspace type =mprotectsize =135168/>
< / malloc>


I'm trying to investigate the state of the C/C++ heap from within gdb on Linux amd64, is there a nice way to do this?

One approach I've tried is to "call mallinfo()" but unfortunately I can't then extract the values I want since gdb doesn't deal with the return value properly.

I'm not easily able to write a function to be compiled into the binary for the process I am attached to, so I can simply implement my own function to extract the values by calling mallinfo() in my own code this way. Is there perhaps a clever trick that will allow me to do this on-the-fly?

Another option could be to locate the heap and traverse the malloc headers / free list; I'd appreciate any pointers to where I could start in finding the location and layout of these.

I've been trying to Google and read around the problem for about 2 hours and I've learnt some fascinating stuff but still not found what I need.

解决方案

@fd - the RedHat bug had your answer.

The mallinfo function has been deprecated, and won't be updated. A true query stats API is TDB. Today, you have malloc_stats and malloc_info. I can't find any documentation on either one, but here's what they give you.

Is this close enough to what you need?

(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>

这篇关于在gdb中检查C / C ++堆内存统计信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆