Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
423 views
in Technique[技术] by (71.8m points)

c++ - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...