How to Use Valgrind in Linux
Valgrind is a set of simulation-based program debugging and analysis tools running on Linux. It contains a kernel, a software-synthesized CPU, and a series of gadgets, each tool can complete a task-debugging, analysis, or testing. Valgrind can detect memory leaks and memory violations. It can also analyze the usage of cache. It is flexible, lightweight, and powerful. It can be seen as a programmer's Swiss Army knife.
I. Main Functions of Valgrind
The Valgrind toolkit contains multiple tools, such as Memcheck, Cachegrind, Helgrind, Callgrind, and Massif. The functions of the tools are described as follows:
Memcheck mainly checks the following program errors:
1. Use uninitialized memory (Use of uninitialised memory)
2. Use the released memory (Reading/writingmemory after it has been free 'd)
3. Use the memory space that exceeds the malloc allocation (Reading/writing off the end of malloc 'd blocks)
4. unauthorized access to the stack (Reading/writinginappropriate areas on the stack)
5. Whether the applied space is released (Memory leaks-where pointers to malloc 'd blocks are lost forever)
6. Match malloc/free/new/delete application and released memory (Mismatched use of malloc/new [] vs free/delete [])
7. Overlapping src and dst (Overlapping src and dst pointers in memcpy () and related functions)
Callgrind
Callgrind collects some data, function call relationships, and other information during the running of the program. It can also selectively simulate cache. At the end of the operation, it will write the analysis data into a file. Callgrind_annotate can convert the content of this file into a readable form.
Cachegrind
It simulates the level-1 cache I1, D1, and L2 level-2 cache in the CPU, and can accurately point out the cache loss and hit in the program. If needed, it can also provide us with the number of cache loss, memory reference times, and the number of commands generated by each code line, each function, each module, and the entire program. This is of great help to the optimization program.
Helgrind
It is mainly used to check competition problems in multi-threaded programs. Helgrind looks for areas in the memory that are accessed by multiple threads but are not always locked. These areas are often the places where synchronization is lost between threads and lead to undiscovered errors. Helgrind implements the competition detection algorithm named "Eraser" and makes further improvements to reduce the number of errors reported.
Massif
Stack analyzer, which can measure the memory used by the program in the stack, tells us the heap block, heap management block and stack size. Massif can help us reduce memory usage. In modern systems with virtual memory, it can also accelerate the running of our programs and reduce the chance that programs stay in the SWAp zone.
Ii. Use Valgrind
Valgrind is very simple to use. The format of the valgrind command is as follows:
Valgrind [valgrind-options] your-prog [your-prog options]
Some common options are as follows:
-H -- help
Displays help information.
-- Version
Displays the version of the valgrind kernel. Each tool has its own version.
-Q -- quiet
Run quietly and only print the error message.
-V -- verbose
Print more detailed information.
-- Tool = [default: memcheck]
The most common option. Run the tool named toolname in valgrind. If the Tool Name is omitted, memcheck is run by default.
-- Db-attach = [default: no]
Bind to the debugger for debugging errors.
Let's take an example to see how to use it. We construct a C program with Memory leakage, as shown below:
# Include <stdio. h>
# Include <stdlib. h>
Int * Test (void)
{
Int * x = malloc (10 * sizeof (int ));
Delete x; // problem 1: heap block overrun, problem 2: memory leak -- x not free, only first address
Return x;
}
Int main (void)
{
Int count;
Test ();
Printf ("I = % d/n", count); // problem 3: use uninitialised value.
Return 0;
}
$ Gcc-Wall-o Test. c
$ Valgrind -- tool = memcheck./Test
$ Valgrind -- tool = memcheck -- leak-check = yes./Test
Iii. Installation
Sudo apt-get install valgrind
Usage of Valgrind in Linux
Use Valgrind in Linux for Memory leakage detection and Performance Analysis
Install Ubuntu memory leak detection tool Valgrind
Valgrind-memory debugging and code anatomy tools in Linux
Use Valgrind to discover memory problems in Linux programs [graphic]
This article permanently updates the link address: