Hi friends,
Are you getting a trouble to find memory leak/corruption in C/C++?
Are you getting a trouble to integrate Valgrind Tool with Linux?
Are you getting a trouble to start Valgring with C/C++ binary process?
---- This blog will give you complete installation of Valgrind tool in Linux, How to use and how to analyze memory corruption/leak in you code/Binary.
Valgrind Tool : The Valgrind tool suite provides a number of debugging and profiling tools that help you get memory leaks and memory corruption in code. The most popular of these tools is called Memcheck. It can detect many memory-related errors that are common in C and C++ programs and that can lead to crashes and unpredictable behavior. so keep enable flag --tool=memcheck.
Install Valgrind :
Direct install into Linux Machine: in terminal "sudo apt-get install valgrind"
Manually Install Valgrind :
Let's Download Valgrind Tool:
Download : http://valgrind.org/downloads/valgrind-3.8.1.tar.bz2
You can download latest release from here: http://valgrind.org/downloads/
It is time to install :
1. Copy .tar file to your Linux folder.
2. Untar it. : tar xzf valgrind-x86_64.tar.gz
3. ./configure
4. make
5. make install
6. ln -s valgrind-x86_64/bin/valgrind <destination>/bin/valgrind64
Get Valgrind Help in Linux:
<destination>/bin/valgrind64 --help
Example:
in Workspace write a code :
workspace#vi c.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *p = NULL;
printf("Allocate..1\n");
p = malloc(4000);
printf("Allocate..2\n");
p = malloc (8000);
printf("free\n");
free(p);
return 0;
}
workspace# gcc -g c.c -o c.o
workspace# ./bin/valgrind ./c.o
==2479==
Allocate..1
Allocate..2
free
==2479==
==2479== HEAP SUMMARY:
==2479== in use at exit: 4,000 bytes in 1 blocks
==2479== total heap usage: 2 allocs, 1 frees, 12,000 bytes allocated
==2479==
==2479== LEAK SUMMARY:
==2479== definitely lost: 4,000 bytes in 1 blocks
==2479== indirectly lost: 0 bytes in 0 blocks
==2479== possibly lost: 0 bytes in 0 blocks
==2479== still reachable: 0 bytes in 0 blocks
==2479== suppressed: 0 bytes in 0 blocks
Core Dump Analysis using Valgrind :
C Program:
test.c
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *p;
*p = malloc (100);
p[101] = 12;
return 0;
}
# gcc -g test.c -o test.o
#valgrind ./test.o
Result:
==16856== HEAP SUMMARY:
==16856== in use at exit: 100 bytes in 1 blocks
==16856== total heap usage: 1 allocs, 0 frees, 100 bytes allocated
==16856==
==16856== LEAK SUMMARY:
==16856== definitely lost: 100 bytes in 1 blocks
==16856== indirectly lost: 0 bytes in 0 blocks
==16856== possibly lost: 0 bytes in 0 blocks
==16856== still reachable: 0 bytes in 0 blocks
==16856== suppressed: 0 bytes in 0 blocks
==16856== Rerun with --leak-check=full to see details of leaked memory
==16856==
==16856== For counts of detected and suppressed errors, rerun with: -v
==16856== Use --track-origins=yes to see where uninitialised values come from
==16856== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 4 from 4)
Segmentation fault
----- Valgrind generates vgcore.* file. i.e vgcore.16856
Analysis Core Dump file using gdb
Syntax: gdb <binary> <coredump file>
# gdb test.o vgcore.16856
Reading symbols from /usr/lib64/valgrind/vgpreload_core-amd64-linux.so...done.
Loaded symbols for /usr/lib64/valgrind/vgpreload_core-amd64-linux.so
Reading symbols from /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so...done.
Loaded symbols for /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so
Reading symbols from /lib64/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib64/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
Core was generated by `'.
Program terminated with signal 11, Segmentation fault.
#0 0x00000000004004b0 in main () at test.c:8
8 *p = malloc (100);
(gdb)
(gdb) b main
Breakpoint 1 at 0x4004a0: file test.c, line 8.
(gdb) r
Starting program: /nobackup/pkatudia/Data/test.o
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x2aaaaaaab000
Breakpoint 1, main () at test.c:8
8 *p = malloc (100);
(gdb) n
Program received signal SIGSEGV, Segmentation fault.
0x00000000004004b0 in main () at test.c:8
8 *p = malloc (100);
(gdb) n
Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.
(gdb) quit
Valgrind process along with Binary:
If you are running a C/C++ binary then how to start valgrind process along with your binary.
1. Make Valgrind process to parent process
2. Make your binary as a child process of Valgrind process.
***Stop your binary first.***
Linux# cp my_binary my_binary_origin
Linux# echo "VALGRIND_LIB=<Valgrind_Lib_path>/valgrind-x86_64/lib/valgrind <Valgrind_Bin_Path>/bin/valgrind64 -v --trace-children=yes --vgdb=yes --fullpath-after=string --track-origins=yes --num-callers=30 --tool=memcheck --leak-check=full --log-file=<log_destination_path>/my_binary_valgrindlog /my_binary_origin" > /my_binary
***Execute your Binary***
***Stop your Binary.***
Valgrind Log will be in /my_binary_valgrindlog
Error and Log analysis :
- Valgrind generates a log after finish the process.
- In every log you will get LEAK SUMMARY and HEAP SUMMARY
- Also it shows how many bytes allocation and free after process
- You can get SIGSEGV, SIGFPE…..
- It generates vgcore.*** files if code generates core dump.
- It also shows error like Invalid Read, Invalid Write..
Reference Links:
http://valgrind.org/
http://www.cprogramming.com/debugging/valgrind.html
**********************END************************
Thanks for Reading..
Prakash Katudia
prakash.katudia@gmail.com
Good post Prakash
ReplyDelete