2

I have a script which executes my unit tests using valgrind. Now the script became big, because I have maybe 10 suppression files (one per library), and it is possible that I will have to add more suppressions files.

Now instead of having a line like this :

MEMCHECK_OPTIONS="--tool=memcheck -q -v --num-callers=24 --leak-check=full --show-below-main=no --undef-value-errors=yes --leak-resolution=high --show-reachable=yes --error-limit=no --xml=yes --suppressions=$SUPPRESSION_FILES_DIR/suppression_stdlib.supp --suppressions=$SUPPRESSION_FILES_DIR/suppression_cg.supp --suppressions=$SUPPRESSION_FILES_DIR/suppression_glut.supp --suppressions=$SUPPRESSION_FILES_DIR/suppression_xlib.supp --suppressions=$SUPPRESSION_FILES_DIR/suppression_glibc.supp --suppressions=$SUPPRESSION_FILES_DIR/suppression_glib.supp --suppressions=$SUPPRESSION_FILES_DIR/suppression_qt.supp --suppressions=$SUPPRESSION_FILES_DIR/suppression_sdl.supp --suppressions=$SUPPRESSION_FILES_DIR/suppression_magick.supp --suppressions=$SUPPRESSION_FILES_DIR/suppression_sqlite.supp --suppressions=$SUPPRESSION_FILES_DIR/suppression_ld.supp --suppressions=$SUPPRESSION_FILES_DIR/suppression_selinux.supp --suppressions=$SUPPRESSION_FILES_DIR/suppression_opengl.supp"

I tried doing like this:

MEMCHECK_OPTIONS="--tool=memcheck -q -v --num-callers=24 --leak-check=full --show-below-main=no --undef-value-errors=yes --leak-resolution=high --show-reachable=yes --error-limit=no --xml=yes --suppressions=$SUPPRESSION_FILES_DIR/*.supp"

but valgrind needs a filename (doesn't accept the asterix).

Since I am doing this in a bash script, can someone tell me what is the easiest way to form that line?

I thought about listing all files in the suppression directory, then iterating over that list, and adding --suppressions= prefix.

EDIT

I forgot to ask. This is what I have so far :

ALL_SUPPRESION_FILES=`ls $SUPPRESSION_FILES_DIR/*.supp`

but I can not find how to transfer that into an array. Can someone help?

BЈовић
  • 62,405
  • 41
  • 173
  • 273

3 Answers3

5

Just do it this way:

# form the list of suppression files to pass to the valgrind
VALGRIND_SUPPRESSION_FILES_LIST=""
for SUPPRESSION_FILE in $SUPPRESSION_FILES_DIR/*.supp; do
  VALGRIND_SUPPRESSION_FILES_LIST+=" --suppressions=$SUPPRESSION_FILE"
done

There's no need for ls.

Here's a way to do it without a loop:

array=($SUPPRESSION_FILES_DIR/*.supp)
VALGRIND_SUPPRESSION_FILES_LIST=${array[@]/#/--suppressions=}

Neither of these work properly if filenames contain spaces, but additional steps can take care of that.

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
1

For those who still facing this problem - have a look at Valgrind Suppression File Howto.

When valgrind runs its default tool, Memcheck, it automatically tries to read a file called $PREFIX/lib/valgrind/default.supp ($PREFIX will normally be /usr). However you can make it use additional suppression files of your choice by adding --suppressions= to your command-line invocation. You can repeat this up to 100 times, which should be sufficient for most situations ;)

Rather than having to type this each time, it's more sensible to write it to an rc file. Each time it runs, valgrind looks for options in files called ~/.valgrindrc and ./.valgrindrc. [...]

Create the files if they don't already exist. So I now have a ~/.valgrindrc containing:

--memcheck:leak-check=full
--show-reachable=yes
--suppressions=/file/path/file1.supp
--suppressions=/file/path/file2.suppth/file2.supp

To check that valgrind is actually using the suppression files, run it with the -v option. The list of suppression files read is near the beginning of the output.

Nathan
  • 136
  • 2
  • 4
0

Well, I managed to solve the issue this way :

# form the list of suppression files to pass to the valgrind
ALL_SUPPRESION_FILES=`ls $SUPPRESSION_FILES_DIR/*.supp`
VALGRIND_SUPPRESSION_FILES_LIST=""
for SUPPRESSION_FILE in ${ALL_SUPPRESION_FILES[@]}; do
  VALGRIND_SUPPRESSION_FILES_LIST="$VALGRIND_SUPPRESSION_FILES_LIST --suppressions=$SUPPRESSION_FILE"
done

I used tokenizing strings and concanating strings to form the list.

Community
  • 1
  • 1
BЈовић
  • 62,405
  • 41
  • 173
  • 273
  • how to acomplish this from command line, without using script. I've tried valgrind --suppressions="file1.supp,file2.supp" ./exec but it doesn't work – Pritesh Acharya Dec 10 '13 at 09:20
  • @PriteshAcharya See the [question](http://stackoverflow.com/q/4326086/476681). You need to put suppressions option per file. In your case `valgrind --suppressions=file1.supp --suppressions=file2.supp ./exec` – BЈовић Dec 10 '13 at 09:24