Tutorial 3: Valgrind: By: Vajih Montaghami

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

SE465/ECE453/CS447/CS647

Tutorial 3: Valgrind
By: Vajih Montaghami
2012-02-17
Saturday, 18 February, 12

vmontagh@uwaterloo.ca

Introduction
framework for building dynamic analysis tools Memcheck is a memory error detector, Cachegrind is a cache and branch- prediction profiler, Callgrind is a call-graph generating cache profiler, Helgrind, DRD are thread error detectors, Massif is a heap profiler, DHAT, SGcheck, BBV, ...
2012-02-17
Saturday, 18 February, 12

vmontagh@uwaterloo.ca

Memory checker?!
Use of un-initialized memory, R/W memory after it has been free'd, R/W off the end of malloc'd blocks, R/W inappropriate areas on the stack, Memory leak: Somewhere, you call malloc but never call free, Mismatched use of malloc/new/new[] vs free/delete/delete[], Overlapping src and dst pointers in memcpy() and related functions.

2012-02-17
Saturday, 18 February, 12

vmontagh@uwaterloo.ca

1. http://developer.kde.org/~sewardj/ 2. http://freshmeat.net/projects/valgrind/

3.2. Installing Run Install and


Uncompress, compile and install it:
#tar xvfz valgrind1.0.0.tar.gz #cd valgrind1.0.0 #./configure #make #make install

Mac, Linux:

Add the path to your path variable. Now valgrind is ready to catch the bugs.

Ubuntu:

#apt-get install valgrind

2012-02-17
Saturday, 18 February, 12

vmontagh@uwaterloo.ca

Prepare to Check
Compile by gcc or g++: -g: Debug Mode. -O0: Turn of the Optimization. Run by Valgrind:
$ valgrind --leak-check=full -v --show-reachable=yes --track-origins=yes ./example
vmontagh@uwaterloo.ca

2012-02-17
Saturday, 18 February, 12

Example 1:Outbound Access

2012-02-17
Saturday, 18 February, 12

vmontagh@uwaterloo.ca

Example 2: Uninitialized Variables

2012-02-17
Saturday, 18 February, 12

vmontagh@uwaterloo.ca

Example 3: Memory leak

2012-02-17
Saturday, 18 February, 12

vmontagh@uwaterloo.ca

Limitation
bounds checking on stack/static arrays checks programs dynamically

2012-02-17
Saturday, 18 February, 12

vmontagh@uwaterloo.ca

Reference
valgrind.org Manual: valgrind.org/docs/manual/valgrind_manual.pdf HOWTO Manual: www.ibiblio.org/pub/Linux/docs/HOWTO/
other-formats/pdf/Valgrind-HOWTO.pdf

2012-02-17
Saturday, 18 February, 12

vmontagh@uwaterloo.ca

10

You might also like