home » knowledge base » valgrind basics (2007-01-09)
Memcheck: valgrind --leak-check=yes myprog arg1 arg2. Use on code compiled with -O0.
-O1 should work as well but -O2 might report a lot of false positives.
Call profiler: valgrind --tool=callgrind myprog arg1 arg2. Generates file callgrind.out.<pid>
where <pid> is process id of this profiler run. Use kcachegrind on this file to inspect the results.
If you want to profile only a section of the code:
#include <valgrind/callgrind.h>
CALLGRIND_TOGGLE_COLLECT macros
--collect-atstart=no so that valgrind doesn't profile from the beginning but only the
sections of the code within CALLGRIND_TOGGLE_COLLECT
In other words, if your code looks like:
#include <valgrind/callgrind.h> CALLGRIND_TOGGLE_COLLECT foo(); CALLGRIND_TOGGLE_COLLECT
your program is profiled only when foo() function is running.
There's also an option --instr-atstart=no and CALLGRIND_START_INSTRUMENTATION and
CALLGRIND_STOP_INSTRUMENTATION macros. In theory you can use this option and insert those 2
macros into the code to speedup profiling (instrumatation slows down valgrind) but in
practice valgrind 3.2 crashed when I was using this option.