Suppressions in Valgrind

Recently I was trying to find all the memory leak in my product. Now my product is very complex and uses couple of third party libraries. When I ran valgrind it’s log file was full of errors from those third party libraries for which I do not have code.

If you want to find the memory leaks in your product only, one can use the Suppression facility in the valgrind

Following are the steps to create suppression file.

  • Create a file for example product.suppression
  • If you want to completely ignore certain shared library then add following lines to that file
  • {
       SUPPRESSION_TEST_OBJECT_COND
       Memcheck:Cond
       ...
       obj:<path to the shared library>
       ...
    }
    {
       SUPPRESSION_TEST_OBJECT_LEAK
       Memcheck:Leak
       ...
       obj:<path to the shared library>                                                                                                 
       ...
    }
    
  • Sometime I found that valgrind give false positive information. For example you may have code which is reusing certain buffer
  • In such case run the valgrind using –gen-suppressions=all option
  • Open the file and you will see output in some following format
  • Locate the memory leak which you think is false positive
  • Copy the suppression code and add it to your main suppression file
  • {
       <insert_a_suppression_name_here>
       Memcheck:Leak
       match-leak-kinds: possible
       fun:malloc
       fun:_ZN8stlp_std14__malloc_alloc8allocateEj
       fun:_ZN8stlp_std12basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE9_M_appendEPKcS7_
       fun:_ZN5dunit4Task4initEi
       fun:_Z12runPutGetAllbb
       fun:_GLOBAL__I_main
       obj:/export/pnq-gst-dev01a/users/adongre/valgrind_702/nc/ThinClient702X_maint/build-artifacts/linux/tests/cppcache/testThinClientPutGetAll
       obj:/export/pnq-gst-dev01a/users/adongre/valgrind_702/nc/ThinClient702X_maint/build-artifacts/linux/tests/cppcache/testThinClientPutGetAll
       fun:__libc_csu_init
       fun:(below main)
    }
    
  • You can also add it like below so that any code path calling that function will be ignored
  • {
       <insert_a_suppression_name_here>
       Memcheck:Leak
       match-leak-kinds: possible
       ...
       fun:_ZN8stlp_std14__malloc_alloc8allocateEj
       ...
    }
    
  • Remember to replace the line with some identifier
  • Next time when you run valgrind use the option –suppressions=
Advertisements

One thought on “Suppressions in Valgrind

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s