Memory leaks # # # valgrind # valgrind Profile

Valgrind Overview

Architecture

Valgrind is a Linux, open source (GPL V2) simulation debugging tools collection, official address: http://valgrind.org/ . Valgrind by the kernel (core) and other debugging tools based on the composition of the kernel. Kernel is similar to a framework (framework), which simulates a CPU environment, and provide services to other tools; and other tools are similar to plug-ins (plug-in), use of services provided by the kernel to complete a variety of specific memory debugging tasks. Valgrind architecture as shown below:

Valgrind tools that include the following:

  1. Memcheck. This is the most widely used tool valgrind, a heavyweight of memory checker can be found in the vast majority of developing memory usage errors, such as: the use of uninitialized memory, the use of already freed memory, such as memory access violation. This is also part of this article will focus on the.
  2. Callgrind . It is mainly used to check the program in question during the function call appeared.
  3. Cachegrind . It is mainly used to check the procedure cache usage problems.
  4. Helgrind . It is mainly used to check the competition concerns arise in multithreaded programs.
  5. Massif . It is mainly used to check the problems arising from the use of the program stack.
  6. Extension. Can take advantage of core features, you can write your own specific memory debugging tools.

Linux program memory space layout

To find a memory problem under Linux, first of all we must know that in Linux, how memory is being allocated? The figure below shows a typical Linux C program memory space layout:

A typical Linux C program memory consists of several parts the following composition:

  • Code segment (.text). Code segments (code segment / text segment) generally refers to an area of memory used to store program code executed. Here it is stored in CPU instructions to be executed. Code segment is shared, the same code in memory, only one copy, while the section is read-only to prevent errors due to modify the program instruction itself (some architectures also allow code segment can be written as, which allows Modify the program). In the code segment also may contain a constant read-only variables, such as string constants and the like. Thus, the size of this partial region is determined before the program has been run, and usually belongs to a read-only memory area.
  • Initialized data section (.data). Data segment (data segment) generally refers to an area of memory used to store program initialized global variables. Here it is stored in the program variables to definitely assigned an initial value, for example, located outside of any global variables function: int val = "100". It should be emphasized that the above two are located in the program's executable file, the kernel is read from the source file when calling exec function to start the program. A static memory allocation.
  • Uninitialized data section (.bss) . bss section (bss [ Block Started by the Symbol ] segment) generally refers to an area of memory used to store program uninitialized global variables. This section is located in the data, before the kernel executes the program, which is initialized to 0 or null. It occurs outside of any function, for example, global variables: int sum; a static memory allocation. 
  • Heap (Heap). This section is used for dynamic memory allocation in the program, such as frequently used malloc, new series memory function is to apply from this segment. Belonging into dynamic memory allocation, the process operation is dynamically allocated memory segment, its size is not fixed, it can be dynamically expanded or reduced.
  • Stack (Stack). The function of local variables and temporary variables generated during the function call are stored in this segment. We function brackets "{}" variable defined (but not including variables declared static, static means that the variable is stored in the data segment). In addition, when the function is called, its parameters will be pushed to initiate the process stack called, and until the end of the call, the return value will be stored back into the stack. As the stack first in first out (FIFO) features, so the stack is particularly convenient to save / restore call site. In this sense, we can stack as a deposit, exchange data temporary memory area. 

A program are essentially the bss section, data section, text composed of three segments.

In the design of the embedded system, generally it refers to a bss section area of ​​memory used to store program uninitialized global variables, bss segment portion generally will be cleared upon initialization. bss segment is a static memory allocation, ie the program outset cleared up. After the C language program is compiled and the like, initialized global variables stored in the .data section, uninitialized global variables stored in .bss.

both text and data segments in the executable file (in the embedded systems typically cured in the image file), the system is loaded from the executable file; bss section is not in the executable file, the system initializes.

Memory check principle

Memcheck detection principle of memory problems as shown below:

Memcheck can detect memory problems, the key lies in the establishment of two global tables.

  1. Valid-Value 表:

For the entire address space of a process each byte (byte), has a corresponding 8 bits; for each register of the CPU, but also a bit vector corresponding thereto. These bytes or bits that is responsible for recording the value of the register has a valid, initialized value.

  1. Valid-Address 

Process for the entire address space in each byte (byte), as well as the corresponding 1 bit, responsible for recording whether the address can be read.

Detection principle:

  • When a byte in memory to be read, the first check bytes corresponding to A bit. If the display position of the A bit is inactive position, memcheck write error is reported.
  • Core (Core) environment similar to a virtual CPU, memory, so that when a byte is loaded into a real CPU, the corresponding V bit byte also be loaded onto the CPU virtual environment. Once the value of the register, is used to generate a memory address, or the program can affect the output value, corresponding to the checks memcheck V bits, if the value has not been initialized, is reported uninitialized memory error

Published 170 original articles · won praise 207 · Views 4.59 million +

Guess you like

Origin blog.csdn.net/xiaoting451292510/article/details/104943580