home ‣ valgrind basics

09 Jan 2007 · Tags: programming, debugging, valgrind ← newer • 149 of 588older →

Memcheck: valgrind myprog arg1 arg2. Use on code compiled with -O0. -O1 should work as well but -O2 might report a lot of false positives.

Memory leaks: valgrind --tool=memcheck --leak-check=full --show-reachable=yes --num-callers=16 myprog arg1 arg2

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:

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.

blog comments powered by Disqus