Valgrind
Tutorial valgrind
Lets start talking about the What happen to the RAM when you execute your program.
Measure the memory
the command free
will show you the free memory in the system
vagrant@vagrant-ubuntu-trusty-64:~$ whatis free
free (1) - Display amount of free and used memory in the system
and with the flag --si
and -h
you can get a better representation, --si
will show you GigaBytes
instead Gigibytes
and the flag -h
include the symbols
vagrant@vagrant-ubuntu-trusty-64:~$ free --si -h
total used free shared buffers cached
Mem: 501M 205M 295M 372K 18M 72M
-/+ buffers/cache: 114M 386M
Swap: 0B 0B 0B
In this example I has 295 MegaBytes as free memory.
And we are going to include the command watch
which refresh the value every 2 secs by defaults
vagrant@vagrant-ubuntu-trusty-64:~$ watch free --si -h
I recommend to use a multiplexor terminal. like tmux.
If I payed for 512MB I’m going to use 512MB
The program is an infinity memory allocation (malloc) loop
#include <stdlib.h>
#include <stdio.h>int main(void)
{
int *p;
while(1)
{
p = malloc(128);
printf("%ld\n", (long)p);
}
return (0);
}
What happened? the memory increase its position. The OS killed the program? and it restore the memory free after kill it.
Well Let me explain what happened to the memory.
The HEAP
the Heap is a space in the memory RAM where a programmer allocate memory manually
What is Valgrind?
How to install Valgrind?
for ubuntu 14.04, the easiest way is with the apt
program
vagrant@vagrant-ubuntu-trusty-64:~$ sudo apt install valgrind -y
.
.
.
vagrant@vagrant-ubuntu-trusty-64:~$ valgrind --version
valgrind-3.10.1
Let’s try this code
mypid
This is the process id, every program executed is a process, and every process has it’s id.
#include <stdio.h>
#include <unistd.h>int main(void)
{
printf("%ld, %s\n", (long)getpid());
return (0);
}
Malloc
Let’s Borrow some memory
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>int main(void)
{
char *msg = malloc(10); printf("%ld, %s\n", (long)getpid(), msg);
return (0);
}
if you use the flag --leak-check=full
let’s include this flag as a default in the configuration file that can be found in the ~/.valgrindrc
this is an example
vagrant@vagrant-ubuntu-trusty-64:~$ cat .valgrindrc
--leak-check=full
let’s fix it
If i include the free command at end it would be the ouput
let’s create and writing error and copy a literal string into msg
As you can see the program is trying to write 10 bytes into a 9 bytes allocated memory and the error set us in the line 11
Let’s create an read error
As you can see we are trying to access to the position 11
the next step is joing the valgrind with the debugger. This is easy, you only need to include the flag --vgdb-error=0
let’s join the three errors, write, read and memory lost
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>int main(void)
{
char *msg = malloc(8);
strcpy(msg, "Holberton");
msg[0] = 'R';
printf("%ld, %s\n", (long)getpid(), msg);
free(msg);return (0);
}
Configuration Files
Differnt types of error
Memcheck: Still Reachable