http://www.pcdebug.com/linux-debugging/a-primer-on-valgrind-valgrind-tutorial.html

A Primer on Valgrind – Valgrind Tutorial

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 25­50 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 data­race (multi­threaded 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:

  • ­­leak­check=yes    # Report on leak statisics
  • ­­show­reachable=yes  # Report pointers still active

Global Command Line Args (all tools)

  •  ­v            # be verbose
  • ­­log­file=file  # messages separated by pid e.g: file.pid1234
  • ­­log­file­exactly=file  # all messages into ‘file’
  • ­­log­fd=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: leak­check=yes
  • ­­memcheck: show­reachable=yes

General Limitations:

  • ? PThreads are emulated, possibly causing execution differences
  • Scheduler is round­robin only, but reschedules more often the default
  • Threads run one­at 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




Reply via email to