Mtrace

Uses your system voice; voice and rate in settings.

Snapshot

≈ 2 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

mtrace is the memory debugger included in the GNU C Library.

mtrace is the memory debugger included in the GNU C Library. void mtrace(void); void muntrace(void); The handlers log all memory allocations and frees to a file defined by the environment variable 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 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. include int main(void) { int* a = (int*)malloc(sizeof(int)); // allocate memory and assign it to the pointer return 0; // Exit the program without freeing memory // we should have released the allocated memory with the statement “free(a)” } $ MALLOC_TRACE=/home/YourUserName/path/to/program/MallocTraceOutputFile.txt $ export MALLOC_TRACE Include mcheck.h in the source code. This is done, for example, by adding the following line to the top of a C or C++ file, as shown below: include Call the function mtrace() before 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(); Compile and run the program as usual. Note that one must compile with the -g flag to get useful output. In GCC on Linux, this can be done using the following commands for a C program: $ gcc yourProgram.c -g $ ./a.out 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. There is a Perl script, of the same name, that can be downloaded to accomplish the same task, in the event that one does not have access to the command mtrace . The mtrace syntax is as follows: $ mtrace For example: $ mtrace a.out MallocTraceOutputFile.txt mtrace can be used with parallel computing but one process at a time, using a condition on the rank like: if (my_rank == 0) { mtrace(); } include include int main(void) { mtrace(); // Starts the recording of memory allocations and releases int* a = (int*)malloc(sizeof(int)); // allocate memory and assign it to the pointer if (!a) { return EXIT_FAILURE; // error } free(a); // free the allocated memory muntrace(); return EXIT_SUCCESS; // exit } Source code for the perl script Linux Journal: Memory Leak Detection in Embedded Systems

Text derived from https://en.wikipedia.org/w/index.php?oldid=1378133385 under CC BY-SA 4.0; modifications have been made.