Xavier Lamorlette
Notes sur Valgrind, issuees d'une formation de Julian Seward, et de mon expérience.
Le site de Valgrind est valgrind.org.
Sommaire :
crtdbg.h
on MSVCMemcheck (+ Valkyrie UI): memory error detector:
Recommended compiling options:
-g
(to see line numbers)-O
(avoid -O2
and above)-fno-inline-functions
(maybe)Run Memcheck:
valgrind --tool=memcheck --xml=yes --leak-check=full --show-reachable=yes --xml-file=results.xml program [with its arguments]
Options:
--num-callers=[x]
: to see more frames in errors stack traces--track-origins=yes
: to find the sources of undefinedness (slower)--suppressions=[filename]
(--gen-suppressions=yes/all
to start)--trace-children=yes
, --child-silent-after-fork=yes
,
--log-file=logfile%p.txt
, --trace-children-skip=patt1,patt2
Make requests from code:
#include <memcheck/memcheck.h>
Callgrind (+ KCachegrind UI): low level time profiler and function-call profiler, simulating CPU caches.
Recommended compiling options:
-g
(to see line numbers)-march=native
-fno-inline-functions
: disable inlining to get clearer resultsRun Callgrind:
valgrind --tool=callgrind --simulate-cache=yes --branch-sim=yes --cacheuse=yes \
--callgrind-out-file=callgrind.out program [with its arguments]
callgrind_annotate callgrind.out [source files to annotate] | grep -v callgrind_annotate
Manual selective profiling:
--instr-atstart=no
callgrind_control -i on [pid]
callgrind_control -i off [pid]
callgrind_control -d [pid]
callgrind_control -z [pid]
Automatic selective profiling:
valgrind --tool=callgrind --simulate-cache=yes \
--toggle-collect="*MyClass::MyMethod*" \
--dump-after="MyClass::MyMethod(arguments)" \
program [with its arguments]
To get the exact signature of a function:
nm -l -C libdll.so | grep MyMethod
One file per thread: --separate-threads=yes
.
Cachegrind: simpler than Callgrind. Does branch predictions.
Run Cachegrind:
valgrind --tool=cachegrind --branch-sim=yes --cache-sim=yes program [with its arguments]
cg_annotate cachegrind.out.[pid] [source files to annotate] # or --auto=yes
Massif: heap memory space profiler. Takes periodic snapshots of heap.
Run Massif:
valgrind --tool=massif program [with its arguments]
Display Massif data:
ms_print massif.out.valgrind.[pid]
ms_print
options:
--x=[x]
: graph number of columns--y=[y]
: graph number of rowsTool similar to Massif: DHAT.
Helgrind: thread error detector.
Similar tool: DRD.
crtdbg.h
on MSVC#define MEMORY_LEAK_TRACKER 1
#if MEMORY_LEAK_TRACKER
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#define DEBUG_NEW new (_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif /* MEMORY_LEAK_TRACKER */
[…]
#if MEMORY_LEAK_TRACKER
int dbgFlags = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
dbgFlags |= _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF;
_CrtSetDbgFlag(dbgFlags);
// Only track one allocation per run!
// e.g. allocation number that causes leaks:
//_CrtSetBreakAlloc(162);
#endif /* MEMORY_LEAK_TRACKER */
La dernière mise à jour de cette page date de juin 2025.
Le contenu de ce site est, en tant qu'œuvre originale de l'esprit, protégé par le droit d'auteur.
Pour tout commentaire, vous pouvez m'écrire à xavier.lamorlette@gmail.com.