Mtrace
Encyclopedia
mtrace is the memory debugger
Memory debugger
A memory debugger is a programming tool for finding memory leaks and buffer overflows. These are due to bugs related to the allocation and deallocation of dynamic memory. Programs written in languages that have garbage collection, such as managed code, might also need memory debuggers, e.g...

 included in the GNU C Library
GNU C Library
The GNU C Library, commonly known as glibc, is the C standard library released by the GNU Project. Originally written by the Free Software Foundation for the GNU operating system, the library's development has been overseen by a committee since 2001, with Ulrich Drepper from Red Hat as the lead...

.

Use

The function mtrace installs handlers for malloc, realloc and free; the function muntrace disables these handlers. Their prototypes
Function prototype
A function prototype in C, Perl or C++ is a declaration of a function that omits the function body but does specify the function's return type, name, arity and argument types...

, defined in the header file mcheck.h, are

void mtrace(void);

void muntrace(void);

The handlers log all memory allocations and frees to a file defined by the environment variable
Environment variable
Environment variables are a set of dynamic named values that can affect the way running processes will behave on a computer.They can be said in some sense to create the operating environment in which a process runs...

 MALLOC_TRACE (if the variable is unset, describes an invalid filename, or describes a filename the user does not have permissions to, the handlers are not installed).

A perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

 script called mtrace, not to be confused with the function of the same name, is also distributed with the GNU C Library; the script parses through the output file and reports all allocations that were not freed.

Bad Source Code

The following is an example of bad source code. The problem with the program is that it allocates memory, but doesn’t free the memory before exiting.
  1. include


int main(void) {

int * a;

a = malloc(sizeof(int)); /* allocate memory and assign it to the pointer */

return 0; /* we exited the program without freeing memory */

/* we should have released the allocated memory with the statement “free(a)” */

}

MTrace Usage

1. Set the environment variable MALLOC_TRACE to the pathname of the desired output file. Setting environment variables is slightly different in each shell. In Bourne Shell
Bourne shell
The Bourne shell, or sh, was the default Unix shell of Unix Version 7 and most Unix-like systems continue to have /bin/sh - which will be the Bourne shell, or a symbolic link or hard link to a compatible shell - even when more modern shells are used by most users.Developed by Stephen Bourne at AT&T...

-compatible shells, like Bash, the command is as follows:

MALLOC_TRACE=/home/YourUserName/path/to/program/MallocTraceOutputFile.txt
export MALLOC_TRACE;

2. Include mcheck.h in the source code. This is done, for example, by adding the following line to the top of a C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

 or C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

 file, as shown below:
  1. include


3. Call the function mtrace before you start allocating memory. It is usually easiest to call mtrace at the very beginning of the main function:

mtrace;


To delineate the end of the code that should be traced, call the function muntrace. This is usually done at the end of the main function:

muntrace;


4. Compile and run the program as usual. Note that you need to compile with the -g option to get useful output. In GCC on Linux, this can be done using the following commands for a C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

 program:

gcc yourProgram.c -g
./a.out

5. Memory leak information will be reported in the file specified by the MALLOC_TRACE environment variable. The difficulty is, this file will be in a computer-readable format. Most Linux machines come with a console command called mtrace, that converts the computer readable format into human-readable text as shown below. If you do not have access to this console command, there is a Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

script, of the same name, that can be downloaded to accomplish the same task. The mtrace syntax is as follows:


mtrace

For example:

mtrace a.out MallocTraceOutputFile.txt

6. mtrace can be used with parallel computing but one process at a time, using a condition on the rank like:

if (my_rank0) mtrace;

MTrace Output

If the mtrace command reports “No Memory Leaks”, then all memory that was allocated in the last execution of that program was also released, which is the way it should be. If, on the other hand, mtrace gives output such as that below, it means the programmer still has some work to do.

Memory not freed:
-----------------
Address Size Caller
0x08049910 0x4 at /home/sureshsathiah/tips/leak.c:9

Good Source Code

The following is an example of good source code. It releases memory after it is allocated, and it uses mtrace to notify the programmer if there are memory leaks.
  1. include
  2. include


int main(void) {

mtrace; /* Starts the recording of memory allocations and releases */

int* a = NULL;

a = malloc(sizeof(int)); /* allocate memory and assign it to the pointer */
if (a NULL) {
return 1; /* error */
}

free(a); /* we free the memory we allocated so we don't have leaks */
muntrace;

return 0; /* exit */

}

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK