How to make Valgrind log all allocations?

二次信任 提交于 2019-12-01 16:33:29

问题


I'd like to make Valgrind log the allocations even when no memory errors were found. How can this be done?


回答1:


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

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

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%)


来源:https://stackoverflow.com/questions/30512000/how-to-make-valgrind-log-all-allocations

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!