Monday, January 14, 2013

Valgrind - debugging and profiling tool


Valgrind is a set of tools to do code profiling and memory debugging so that memory leaks can be detected in Linux. In addition it can also serve as a framework for building new debugging tools.

Valgrind runs on the following architectures

  • i386
  • x86_64 (AMD-64)
  • ppc
  • ppc64System z

Let us see how to use valgrind


The main advantage of Valgrind is that it works with existing compiled executables. We do not need to recompile or modify programs to make use of valgrind.

Valgrind usage is as follows

valgrind <valgrind_options> <your-prog> <your-program-options>

valgrind consists of many tools and one of the important configuration option is --tool. This option tells valgrind which tool to run. If the --tool option is omitted, then the tool "memcheck" is chosen by default.

Eg: 

valgrind --tool=memcheck find . -mtime +10 -type f

If there us a memory leak, then the number of allocs and the number of frees will differ. So for a detailed analysis, we can rerun the program with "--leak-check=yes" option

valgrind --tool=memcheck --leak-check=yes <program>

Here is a list of standard valgrind tools

  • memcheck - Detects memory errors. It helps you tune your programs to behave correctly.
  • cachegrind - Profiles cache prediction. It helps you tune your programs to run faster.
  • callgrind - Works in a similar way to cachegrind but also gathers additional cache-profiling information.
  • exp-drd - Detects thread errors. It helps you tune your multi-threaded programs to behave correctly.
  • helgrind - Another thread error detector. Similar to exp-drd but uses different techniques for problem analysis.
  • massif - A heap profiler. Heap is an area of memory used for dynamic memory allocation. This tool helps you tune your program to use less memory.
  • lackey - An example tool showing instrumentation basics

Using valgrind, we can

  • Find invalid Pointer Use
  • Detect the use Of Uninitialized Variables

Specifying options for valgrind tool in a file

The options for valgrind can be specified in the file .valgrindrc, placed in the home directory of the user who runs valgrind command.

For example, if we want memcheck to always write profile data to the /tmp/memcheck_PID.log, add the following line to the .valgrindrc file in our home directory: 

--memcheck:memcheck-out-file=/tmp/memcheck_%p.log

How valgrind works?

valgrind needs a real executable (machine code) as an argument. valgrind takes control of the executable before it starts. The executable's code is redirected to the selected valgrind tool, and the tool adds its own code to handle its debugging. Then the code is handed back to the valgrind core and the execution continues.

For example, memcheck adds its code, which checks every memory access. As a consequence, the program runs much slower than in the native execution environment.

valgrind not only checks the code of the program, but also all libraries related to the code.


No comments:

Post a Comment