Freelance Projects

Upeksha Wisidagama

Dynamic Program Analysis With Valgrind

Valgrind

Valgrind

The “Val” as in the world “value”. The “grind” is pronounced with a short ‘i’ — ie. “grinned” (rhymes with “tinned”) rather than “grined” (rhymes with “find”).

Don’t feel bad: almost everyone gets it wrong at first.

Valgrind

Where does the name “Valgrind” come from? From Nordic mythology. Originally (before release) the project was named Heimdall, after the watchman of the Nordic gods. He could “see a hundred miles by day or night, hear the grass growing, see the wool growing on a sheep’s back”, etc. This would have been a great name, but it was already taken by a security package “Heimdal”.

Keeping with the Nordic theme, Valgrind was chosen. Valgrind is the name of the main entrance to Valhalla (the Hall of the Chosen Slain in Asgard). Over this entrance there resides a wolf and over it there is the head of a boar and on it perches a huge eagle, whose eyes can see to the far regions of the nine worlds. Only those judged worthy by the guardians are allowed to pass through Valgrind. All others are refused entrance.

Read More: http://en.wikipedia.org/wiki/Norse_mythology

Thor's fight with giants

Valgrind: It’s not short for “value grinder”, although that’s not a bad guess.

- FAQ

Detecting Memory Errors in ls Program!

Install Valgrind using sudo apt-get install valgrind.

Run Valgrind on ls using valgrind ls.

Valgrind ls

ERROR SUMMARY: 0 errors from 0 contexts :–)

How Analyze your C program Dynamically

Create the following C file.

‘uw.c’
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>

int main()
{
  char *p;

  p = (char *) malloc(10);

  p = (char *) malloc(17);
  free(p);

  p = (char *) malloc(13);

  return 0;
}

Compile using gcc -o uw -g uw.c. Run Valgrind with valgrind ./uw.

Valgrind Output

HEAP SUMMARY

in use at exit: 23 bytes in 2 blocks. total heap usage: 3 allocs, 1 frees, 40 bytes allocated.

LEAK SUMMARY

definitely lost: 23 bytes in 2 blocks.

In the above file, we didn’t free the p memory allocation twice. Therefore total 10 + 13 = 23 bytes of memory was leaked in our program.

Learn More: http://valgrind.org/docs/manual/quick-start.html