Prevents Memory leakage. Valgrind is used for checks in Linux.

Source: Internet
Author: User
Tags valgrind
To prevent memory leakage, use Valgrind in Linux to check general Linux technology-Linux programming and kernel information. The following is a detailed description. One of the most troublesome problems in C/C ++ development is memory management. Sometimes it takes several days to find a memory leak or a memory access out of bounds, if there is a tool that can help us do this, valgrind is such a tool.

Valgrind is a software suite based on a simulated program debugger and analyzer in linux. It can run on x86, amd64, and ppc32 architectures. Valgrind contains a core that provides a virtual CPU running program and a series of tools for debugging, profiling, and similar tasks. Valgrind is highly modular, so developers or users can add new tools to it without damaging their structures.

You can download the latest valgrind from its website, which is open source code and free of charge.

I. Introduction
Valgrind contains several standard tools:

1. memcheck
Memory Management Issues in the memcheck probe. It checks all read/write operations on the memory and intercepts all malloc/new/free/delete calls. Therefore, memcheck can detect the following problems:

1) Use uninitialized memory
2) read/write memory released
3) read/write memory out of bounds
4) Inappropriate read/write memory stack space
5) Memory leakage
6) malloc/new [] and free/delete [] do not match.

2. cachegrind
Cachegrind is a cache parser. It simulates the execution of L1, D1, and L2 cache in the CPU, so it can accurately point out that the cache in the Code is not hit. If you need it, it can print the number of cache hits, memory reference and each line of code in which cache hits are missed, the summary of each function, each module, and the entire program. If you require more detailed information, it can print the number of missed machine codes for each line. On x86 and amd64, cachegrind automatically detects the machine's cache configuration through CPUID, so in most cases it no longer needs more configuration information.

3. helgrind
Helgrind searches for competing data in multi-threaded programs. Helgrind searches for memory addresses, which are accessed by more than one thread. If no consistent lock is used, it will be detected. This indicates that these addresses are not synchronized during multi-threaded access, which may cause time series problems that are difficult to find.

Ii. What does valgrind do to your program?
Valgrind is designed to be non-intrusive and work directly on executable files, so you do not need to recompile, connect, and modify your program before checking. To check a program, you only need to execute the following command.

Valgrind -- tool = tool_name program_name

For example, if we want to perform a memory check on the ls-l command, we only need to execute the following command.

Valgrind -- tool = memcheck ls-l

No matter which tool is used, valgrind always gets control of your program before it starts, and reads debugging information from the executable Link Library. Then, run the program on the virtual CPU provided by the valgrind core. valgrind processes the code based on the selected tool. The tool adds the detection code to the code, and return the Code as the final code to the valgrind core. Finally, the valgrind core runs the code.

To check for Memory leakage, you only need to add -- leak-check = yes. The command is as follows:

Valgrind -- tool = memcheck -- leak-check = yes ls-l

Code added between different tools is greatly changed. At the end of each scope, memcheck adds code to check the access and value calculation of each piece of memory. The code size increases by at least 12 times and the running speed is 25 to 50 times slower than usual.

Every command in the valgrind simulation program is executed. Therefore, the checking tool and profiling tool are not only for your application, but also for shared libraries, gnu c libraries, and X client libraries.

3. Start now
First, enable the debugging mode (-g option of the gcc compiler) when compiling the program ). Without debugging information, even the best valgrind tool will be able to guess which function the specific code belongs. Open the debugging option for compilation and then use valgrind for check. valgrind will give you a detailed report, for example, which line of code has memory leakage.

When you check the C ++ program, you should also consider another option-fno-inline. It makes the function call chain very clear, which can reduce the confusion when you browse large C ++ programs. For example, when using this option, it is easy to use memcheck to check openoffice. Of course, you may not do this, but using this option makes valgrind generate more accurate error reports and reduce confusion.

Some compilation optimization options (such as-O2 or higher Optimization Options) may cause memcheck to submit incorrect uninitialized reports. Therefore, to make valgrind reports more accurate, it is best not to use optimization options during compilation.

If the program is started using a script, you can modify the code of the Startup Program in the script, or run the script using the -- trace-children = yes option.

The following is the output report for checking the ls-l command with memcheck. Execute the following command in the terminal:

Valgrind -- tool = memcheck ls-l

The program prints the result of the ls-l command. The final check report of valgrind is as follows:

= 4187 =

= 4187 = error summary: 0 errors from 0 contexts (suppressed: 19 from 2)

= 4187 = malloc/free: in use at exit: 15,154 bytes in 105 blocks.

= 4187 = malloc/free: 310 allocs, 205 frees, 60,093 bytes allocated.

= 4187 = For counts of detected errors, rerun with:-v

==4187 = searching for pointers to 105 not-freed blocks.

= 4187 = checked 145,292 bytes.

= 4187 =

= 4187 = leak summary:

= 4187 = definitely lost: 0 bytes in 0 blocks.

==4187 = possibly lost: 0 bytes in 0 blocks.

= 4187 = still reachable: 15,154 bytes in 105 blocks.

= 4187 = suppressed: 0 bytes in 0 blocks.

= 4187 = Reachable blocks (those to which a pointer was found) are not shown.

= 4187 = To see them, rerun with: -- show-reachable = yes

Here "4187" refers to the ID of the process that executes ls-l, which is helpful for distinguishing reports of different processes. Memcheck will report how much memory is allocated and released, How much memory is leaked, How much memory is accessible, and how many bytes of memory is checked.

The following two examples Use valgrind for memory check:

Example 1 (test. c ):
# Include

Int main (int argc, char * argv [])
{
Char * ptr;

Ptr = (char *) malloc (10 );
Strcpy (ptr, "01234567890 ");

Return 0;
}

Compile the program
Gcc-g-o test. c

Use valgrind to execute the command
Valgrind -- tool = memcheck -- leak-check = yes./test

The report is as follows:
==4270 = Memcheck, a memory error detector.

= 4270 = Copyright (C) 2002-2006, and gnu gpl 'd, by Julian Seward et al.

= 4270 = Using LibVEX rev 1606, a library for dynamic binary translation.

= 4270 = Copyright (C) 2004-2006, and gnu gpl 'd, by OpenWorks LLP.

= 4270 = Using valgrind-3.2.0, a dynamic binary instrumentation framework.

= 4270 = Copyright (C) 2000-2006, and gnu gpl 'd, by Julian Seward et al.

= 4270 = For more details, rerun with:-v

= 4270 =

==4270 = Invalid write of size 1

= 4270 = at 0x4006190: strcpy (mc_replace_strmem.c: 271)

==4270 = by 0x8048db: main (test. c: 8)

==4270 = Address 0x4023032 is 0 bytes after a block of size 10 alloc 'd

= 4270 = at 0x40044F6: malloc (vg_replace_malloc.c: 149)

==4270 = by 0x80483C5: main (test. c: 7)

= 4270 =

==4270 = Invalid write of size 1

= 4270 = at 0x400619C: strcpy (mc_replace_strmem.c: 271)

==4270 = by 0x8048db: main (test. c: 8)

==4270 = Address 0x4023033 is 1 bytes after a block of size 10 alloc 'd

= 4270 = at 0x40044F6: malloc (vg_replace_malloc.c: 149)

==4270 = by 0x80483C5: main (test. c: 7)

= 4270 =

= 4270 = error summary: 2 errors from 2 contexts (suppressed: 12 from 1)

= 4270 = malloc/free: in use at exit: 10 bytes in 1 blocks.

= 4270 = malloc/free: 1 allocs, 0 frees, 10 bytes allocated.

= 4270 = For counts of detected errors, rerun with:-v

= 4270 = searching for pointers to 1 not-freed blocks.

= 4270 = checked 51,496 bytes.

= 4270 =

= 4270 =

==4270 = 10 bytes in 1 blocks are definitely lost in loss record 1 of 1

= 4270 = at 0x40044F6: malloc (vg_replace_malloc.c: 149)

==4270 = by 0x80483C5: main (test. c: 7)

= 4270 =

= 4270 = leak summary:

= 4270 = definitely lost: 10 bytes in 1 blocks.

==4270 = possibly lost: 0 bytes in 0 blocks.

= 4270 = still reachable: 0 bytes in 0 blocks.

= 4270 = suppressed: 0 bytes in 0 blocks.

= 4270 = Reachable blocks (those to which a pointer was found) are not shown.

= 4270 = To see them, rerun with: -- show-reachable = yes

From this report, we can see that the process number is 4270, And the write memory of row 8th of test. c is out of bounds. The strcpy function causes the write memory to be out of bounds,

The 7th rows leak 10 bytes of memory, causing memory leakage of the malloc function.

Example 2 (test2.c)
# Include

Int foo (int x)
{
If (x <0 ){
Printf ("% d", x );
}

Return 0;
}

Int main (int argc, char * argv [])
{
Int x;

Foo (x );

Return 0;
}

Compile the program
Gcc-g-o test2 test2.c

Use valgrind for memory check
Valgrind -- tool = memcheck./test2

The output report is as follows:
==4285 = Memcheck, a memory error detector.

= 4285 = Copyright (C) 2002-2006, and gnu gpl 'd, by Julian Seward et al.

= 4285 = Using LibVEX rev 1606, a library for dynamic binary translation.

= 4285 = Copyright (C) 2004-2006, and gnu gpl 'd, by OpenWorks LLP.

= 4285 = Using valgrind-3.2.0, a dynamic binary instrumentation framework.

= 4285 = Copyright (C) 2000-2006, and gnu gpl 'd, by Julian Seward et al.

= 4285 = For more details, rerun with:-v

= 4285 =

= 4285 = Conditional jump or move depends on uninitialised value (s)

= 4285 = at 0x8048372: foo (test2.c: 5)

==4285 = by 0x80483B4: main (test2.c: 16)

= 4285 = p

= 4285 = error summary: 1 errors from 1 contexts (suppressed: 12 from 1)

= 4285 = malloc/free: in use at exit: 0 bytes in 0 blocks.

= 4285 = malloc/free: 0 allocs, 0 frees, 0 bytes allocated.

= 4285 = For counts of detected errors, rerun with:-v

= 4285 = All heap blocks were freed -- no leaks are possible.

From this report, we can see that the PID of the process is 4285, And the foo function is called in line 2 of the test2.c file. The foo function in line 3 of the test2.c file uses an uninitialized variable.

Valgrind has many other options to use. For more information, see the man manual page of valgrind and the online documentation of valgrind official website.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.