Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you tell Valgrind to completely suppress a particular .so file?

I'm trying to use Valgrind on a program that I'm working on, but Valgrind generates a bunch of errors for one of the libraries that I'm using. I'd like to be able to tell it to suppress all errors which involve that library. The closest rule that I can come up with for the suppression file is

{    rule name    Memcheck:Cond    ...    obj:/path/to/library/thelibrary.so } 

This doesn't entirely do the job, however. I have to create one of these for every suppression type that comes up (Cond, Value4, Param, etc), and it seems to still miss some errors which have the library in the stack trace.

Is there a way to give Valgrind a single suppression rule to make it completely ignore a particular library? And even if there is no way to make such a rule which covers all suppression types, is there at least a way to create a rule which ignores all errors of a particular suppression type from a particular library?

like image 265
Jonathan M Davis Avatar asked Mar 03 '10 23:03

Jonathan M Davis


People also ask

How do I write a Valgrind suppression file?

To make it easier to write suppressions, you can use the --gen-suppressions=yes option. This tells Valgrind to print out a suppression for each reported error, which you can then copy into a suppressions file. Different error-checking tools report different kinds of errors.

How do you use Valgrind options?

To run Valgrind, pass the executable as an argument (along with any parameters to the program). The flags are, in short: --leak-check=full : "each individual leak will be shown in detail" --show-leak-kinds=all : Show all of "definite, indirect, possible, reachable" leak kinds in the "full" report.

How does Valgrind work internally?

Valgrind works by doing a just-in-time (JIT) translation of the input program into an equivalent version that has additional checking. For the memcheck tool, this means it literally looks at the x86 code in the executable, and detects what instructions represent memory accesses.

How does Valgrind help in debugging?

Valgrind is a multipurpose code profiling and memory debugging tool for Linux when on the x86 and, as of version 3, AMD64, architectures. It allows you to run your program in Valgrind's own environment that monitors memory usage such as calls to malloc and free (or new and delete in C++).


2 Answers

For most of the suppression types, you omit the wildcard, like so:

{    name    Memcheck:Cond    obj:/path/to/lib/lib.so.10.1 }  {    name    Memcheck:Free    obj:/path/to/lib/lib.so.10.1 }  {    name    Memcheck:Value8    obj:/path/to/lib/lib.so.10.1 } 

Note that you must list each type of error separately, you can't wildcard them. You must also list the entire pathname of the library (as shown by valgrind, with any "decorations" like version numbers).

Also, leaks are handled differently -- for those you need something that looks like this:

{    name    Memcheck:Leak    fun:*alloc    ...    obj:/path/to/lib/lib.so.10.1    ... } 
like image 176
BillTorpey Avatar answered Sep 17 '22 17:09

BillTorpey


It appears that it is necessary to include a separate suppression record for each type of error (Cond, Value4, Param, etc). But based on my testing with valgrind-3.6.0.SVN-Debian, I believe you can use the following simplified form for each type of error...

{    <insert_a_suppression_name_here>    Memcheck:Cond    ...    obj:/path/to/library/thelibrary.so.*    ... }  {    <insert_a_suppression_name_here>    Memcheck:Leak    ...    obj:/path/to/library/thelibrary.so.*    ... } 

The three dots are called frame-level wildcards in the Valgrind docs. These match zero or more frames in the call stack. In other words, you use these when it doesn't matter who called into the library, or what functions the library subsequently calls.

Sometimes errors include "obj:" frames and sometimes they only use "fun:" frames. This is based, in general, on whether or not that function is included in the library's symbol table. If the goal is to exclude the entire library, it may work best if the library does not include symbols so that you can exclude based on the library filename instead of having to create separate suppressions for each function call within the library. Hopefully, Valgrind is clever enough to suppress errors based on library filename even when it does know the function name, but I haven't verified this.

If you do need to add suppressions based on individual functions within the library, you should be able to use the same form...

{    <insert_a_suppression_name_here>    Memcheck:Leak    ...    fun:the_name_of_the_function    ... } 

Note: You can include --gen-suppressions=all on the valgrind command-line in order to see the exact form and names (including any C++ mangling) required to suppress each error. You can use that output as a template for your suppression records -- in which you would usually want to replace most lines with ... in order to simplify the process of suppressing all errors that might occur in association with a specific library or function call.

Note: <insert_a_suppression_name_here> is a placeholder in which you can type whatever descriptive text that you want. It is required to not be blank.

like image 39
Brent Bradburn Avatar answered Sep 20 '22 17:09

Brent Bradburn