How to make Valgrind log all allocations?

前端 未结 1 1845
轻奢々
轻奢々 2020-12-20 16:39

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

相关标签:
1条回答
  • 2020-12-20 16:57

    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%)
    
    0 讨论(0)
提交回复
热议问题