What Does Valgrind Do?
- Runs a program on a virtual CPU
- Is able to check every memory access
- Can check for memory leaks, errors
- Detects use of uninitialized memory
- Runs 2550 slower
- Can handle optimized binary (with some footnotes)
- No need to generate special binary
Running Valgrind in different modes:
valgrind –tool= program args
Checker types:
- memcheck – A through memory analysis
* massif – A Heap profiler
* helgrind – A datarace (multithreaded access) detector
- addrcheck – A lighter weight memory checker (fast)
- cachegrind – A processor cache hit/miss analyzer
- callgrind – A heavyweight profiler
- lackey simple profiler and memory tracer
Other Experimental Variants:
- crocus – A signal call tracing tool
- interactive – Valgrind with a gdb like interface
- redux – A data flow tracer
Memcheck tool Errors Detected:
- Use of uninitialised memory
- Reading/writing memory after it has been free’d
- Reading/writing off the end of malloc’d blocks
- Reading/writing inappropriate areas on the stack
- Memory leaks where pointers to malloc’d blocks are lost forever
- Mismatched use of malloc/new/new [] vs free/delete/delete []
- Overlapping src and dst pointers in memcpy() and related functions
Not Detected:
- Bounds exceeded in stack or static arrays
Memcheck tool Common Command Line Args For memcheck:
- leakcheck=yes # Report on leak statisics
- showreachable=yes # Report pointers still active
Global Command Line Args (all tools)
- v # be verbose
- logfile=file # messages separated by pid e.g: file.pid1234
- logfileexactly=file # all messages into ‘file’
- logfd=2 # merges output onto program’s stderr
Making Options a Bit Easier: Options can come from 4 sources:
- command line
- Dot file in home directory .valgrindrc
- Environment variable $VALGRIND_OPTS
- Dot file in current directory .valgrindrc
The syntax is: toolname: option Example:
- memcheck: leakcheck=yes
- memcheck: showreachable=yes
General Limitations:
- ? PThreads are emulated, possibly causing execution differences
- Scheduler is roundrobin only, but reschedules more often the default
- Threads run oneat a time, regardless of # of CPU’s
- Priority based scheduling calls ignored (with message)
- Shared memory synchronization can fail (atomic operations not atomic)
- Some instructions (like 3Dnow) not supported
- Floating point emulated by 64bit, not 80 bit FP
Important Tips for Using Valgrind