Valgrind-a memory error detecting tool, usage and use case.
Size: 987.66 KB
Language: en
Added: Mar 02, 2017
Slides: 13 pages
Slide Content
Valgrind Use-case & Usage
Contents Introduction Preparing Program Running Program Under Valgrind Setting Valgrind Options Error Detection Example Using Memcheck Tool Provisio Useful Links
Introduction Valgrind: m emory mismanagement detector (memory leaks, deallocation etc.) tool suite providing a number of debugging tools (eg: Memcheck, by default) help make your programs faster and more correct can detect many memory-related error Memcheck: memory error detector
Preparing Program c ompile with ‘ -g’ to include debugging information ‘ -o0’ can also be used, but slows down the process u sing ‘-o1’ speeds up the process but line numbers can be inaccurate g cc -g test_prog.c -o test_prog.out
Running Program v algrind [valgrind-options] ./prog [prog-args] v algrind --leak-check=yes --show-reachable=yes --tool=memcheck --error-limit=no --track-origins=yes ./test_prog.out arg1 arg2 … argN --leak-check=yes(yes/no/summary-default) turns on detailed memory leak detector --tool=memcheck(default- Cachegrind/Addrgrind/Callgrind/Massif etc) --error-limit(yes-default/no) 30000 in total, or 300 different ones program will run slower than normal (eg: 20 to 30 times)
Setting Default Options c an be passed through : file ~/.valgrindrc environment variable $VALGRIND_OPTS f ile ./.valgrindrc f or different valgrind command line options read section: 2.6
Error Detection //test_prog.c int main() { char *p; // Allocation #1 of 19 bytes p = (char *) malloc(19); // Allocation #2 of 12 bytes p = (char *) malloc(12); free(p); // Allocation #3 of 16 bytes p = (char *) malloc(16); return 0; }
Using Memcheck Use of illegal address Use of uninitialized values Incorrect freeing
Illegal Address #include <stdio.h> #include <stdlib.h> int main(){ int *arr = malloc(20); int i; for(i = 0; i<5; i++){ arr[i] = i; } free(arr); printf("%d\n", arr[2]); return 0;}
Use of Uninitialised Values #include <stdio.h> int main(){ int x; printf("%d\n", x); return 0; }
Use of Uninitialised Values(Contd.) void func(int y){ if(y==2)printf("OK\n"); else printf("Wrong\n"); } int main(){ int x; func(x); return 0; }
Proviso can’t detect all errors an instance: int main(){ int i; int x =0; int a[10]; for (i = 0; i < 11; i++) a[i] = i; printf("%d\n", a[10]); return 0; }
Useful Links Basic Valgrind Tutorial The Valgrind Quick Start Guide The Valgrind User Manual