Search code examples
shellvalgrind

How to use valgrind to check many executable files without staring at the terminal for a long time?


I have a lot of executable files and I want to use valgrind to do memory checking.

I am using the follow command to do the memory check:

valgrind -q ./a1.out
valgrind -q ./a2.out
...
valgrind -q ./a100.out

I must stare at the terminal for a long time to find is there any memory problem exist in my code.

Can valgrind return some value to us? The value stand for there exist problem or not. And shell can operate the value. So we can write some script and automatically get the conclusion that is there any problem in the executable files.

For example, I want somthing like this:

exist_problem = valgrind -q ./a1.out

if [exist_problem == no]
   printf "ALL PASS\n"
fi

Thanks in advance.


Solution

  • Look at valgrind option

    --error-exitcode=<number> exit code to return if errors found [0=disable]
    

    If you use memcheck, you can also define what kinds of leaks are errors:

    --errors-for-leak-kinds=kind1,kind2,..  which leak kinds are errors?
                                            [definite,possible]
    

    Finally, you can also redirect valgrind output to a file, use

    --error-markers=<begin>,<end> add lines with begin/end markers before/after
                              each error output in plain text mode [none]
    

    and grep in your output files.