Debugging Memory Issues William Cohen NCSU CSC 591W February 20, 2008
Memory Problems: Uninitialized memory Memory leaks Double frees Out of bound array accesses Null pointers Memory corruption FOSS Tools/Solutions: Valgrind Mudflap
Valgrind Introduction Provides a number of tools to analyze programs: Memory error checkers Heap profiler (catch excessive memory use) Cache profiler (also give coverage information) Works code using shared libraries and threads Available for i386 and x86_64 Linux
Valgrind Concepts Instruments executables with binary rewrites Works with existing executables and shared libs Avoids recompiling code Tools instrument particular operations and functions (e.g. Memory reads/writes and malloc) Space and time overheads introduced by instrumentation and binary rewrites Can write custom instrumentation
Valgrind Memory Error Detection (memcheck) Tracks memory allocated and freed Detects reads/writes to uninitialized and freed memory Detects memory leaks
Valgrind Heap Profiler (massif) Provides information on: Number of heap blocks Amount of memory used Stack sizes Tool output includes summaries for each process.
Valgrind Cache Profiler (cachegrind) Simulates processors caches Can provide code coverage information
Valgrind Demonstrations
Mudflap Introduction Compile-time instrumentation Shared libraries are not instrumented Includes array bounds checking Built into recent versions of gcc
Mudflap Demonstrations
Further Reading Steve Best, Linux Debugging and Performance Tuning: Tips and Techniques, Prentice Hall, 2006. http://www.valgrind.org/ http://www.valgrind.org/docs/manual/QuickStart.html http://www.valgrind.org/docs/manual/manual.html http://gcc.fyxm.net/summit/2003/mudflap.pdf