如何使Valgrind记录所有分配? [英] How to make Valgrind log all allocations?
问题描述
即使没有发现内存错误,我也想让Valgrind记录分配。如何才能做到这一点?
I'd like to make Valgrind log the allocations even when no memory errors were found. How can this be done?
推荐答案
您将使用 Massif 为此(一种valgrind工具)。手册链接很容易理解,但为将来参考,以下是如何使用它的方法,直接来自手册:
You would use Massif for that (a valgrind tool). The manual link is easy enough to follow, but for future reference, here's how to use it, straight out of the manual:
valgrind --tool=massif prog
这将生成一个文件,您可以使用<$ c $进行分析c> ms_print 。文件名将为 massif.out。< numbers>
。只需使用 ms_print
即可获得不错的输出:
This will produce a file that you can analyze with ms_print
. The filename will be massif.out.<numbers>
. Just use ms_print
to get a nice output:
ms_print massif.out.12345
您要查找的内容可以在<$ c的输出末尾找到$ c> ms_print 。对于此示例程序(它们在手册中显示的程序):
What you're looking for can be found in the end of the output of ms_print
. For this example program (the program they show in the manual):
#include <stdlib.h>
void g(void)
{
malloc(4000);
}
void f(void)
{
malloc(2000);
g();
}
int main(void)
{
int i;
int* a[10];
for (i = 0; i < 10; i++) {
a[i] = malloc(1000);
}
f();
g();
for (i = 0; i < 10; i++) {
free(a[i]);
}
return 0;
}
我们可以看到谁分配了什么:
We can see who allocated what:
->79.81% (8,000B) 0x400589: g (in /home/filipe/dev/a.out)
| ->39.90% (4,000B) 0x40059E: f (in /home/filipe/dev/a.out)
| | ->39.90% (4,000B) 0x4005D7: main (in /home/filipe/dev/a.out)
| |
| ->39.90% (4,000B) 0x4005DC: main (in /home/filipe/dev/a.out)
|
->19.95% (2,000B) 0x400599: f (in /home/filipe/dev/a.out)
| ->19.95% (2,000B) 0x4005D7: main (in /home/filipe/dev/a.out)
|
->00.00% (0B) in 1+ places, all below ms_print's threshold (01.00%)
这篇关于如何使Valgrind记录所有分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!