user3810155
user3810155

Reputation:

How to make Valgrind log all allocations?

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

Upvotes: 11

Views: 7346

Answers (1)

Filipe Gonçalves
Filipe Gonçalves

Reputation: 21213

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

Upvotes: 20

Related Questions