This tutorial aims to cover the basic principles of Valgrind. It was presented by Rodrigo Randel.
Valgrind is a widely used programming tool for debugging and profiling programs. Valgrind's tools are designed to dynamically perform analysis of your program and automatically detect commons and rares bugs, many of which would take hours to be found manually, or would never be found at all.
By using Valgrind tools, such as the memory checker and the cache profiler, you can easily identify when a bug occurs, and get instant feedback about what the bug is, where it occurred, and why. As for Valgrind's profiling tools, use those whenever you want to gather information about how your program is spending its time, or you if want to speed it up. Moreover, Valgrind works with programs written in any language, such as C, C++, Java, Python and many others.
In this tutorial, we are going to cover the following topics:
- Present the main aspects of Valgrind and explain why and when you should use it.
- Give an overview of all the six tools present in the Valgrind's current release, explaining in details two of them: Memcheck and Cachegrind.
- Present how to use Valgrind and how to interpret its output, either via a terminal window or via using an IDE such as Eclipse.
Each folder has an Eclipse project with the code and Makefile for the correspondent example.
To run valgrind with memcheck:
$ valgrind --tool=memcheck ./main
Use --leak-check=full to obtain details for each definitely lost or possibly lost block:
$ valgrind --tool=memcheck --leak-check=full ./main
To check for memory errors run:
$ valgrind --tool=memcheck --leak-check=full ./main
Use --gen-suppressions=yes to ask valgrind to generate suppressions:
$ valgrind --tool=memcheck --gen-suppressions=yes --leak-check=full ./main
Copy the generated suppressions in a file called ex2.supp and run:
$ valgrind --tool=memcheck --suppressions-file=ex2.supp --leak-check=full ./main
Use -v to run Valgrind with verbose to check the suppressed files:
$ valgrind --tool=memcheck --suppressions=ex2.supp -v --leak-check=full ./main
To check for errors run:
$ valgrind ./main
Use valgrind gdbserver to debug with gdb:
$ valgrind --vgdb=yes --vgdb-error=0 ./main
In another shell, run gdb and connect to valgrind:
$ gdb ./main
$ (gdb) target remote | vgdb (--pid=<pid>)
Use monitor commands of presented in slides 22 and 23.
To run memcheck and check for errors run:
$ valgrind --leak-check=full ./main
Use --track-origins=yes easier to track down the root causes of uninitialized value errors
$ valgrind --leak-check=full --track-origins=yes ./main
The demonstrations 5 and 7 were made using the Valgrind support on Eclipse.
The demonstration 6 was made using the KCachegrind tool.