MTRACE

NAME
SYNOPSIS
DESCRIPTION
CONFORMING TO
NOTES
BUGS
EXAMPLE
SEE ALSO
COLOPHON

NAME

mtrace, muntrace − malloc tracing

SYNOPSIS

#include <mcheck.h>

void mtrace(void);

void muntrace(void);

DESCRIPTION

The mtrace() function installs hook functions for the memory-allocation functions (malloc(3), realloc(3) memalign(3), free(3)). These hook functions record tracing information about memory allocation and deallocation. The tracing information can be used to discover memory leaks and attempts to free nonallocated memory in a program.

The muntrace() function disables the hook functions installed by mtrace(), so that tracing information is no longer recorded for the memory-allocation functions. If no hook functions were successfully installed by mtrace(), muntrace() does nothing.

When mtrace() is called, it checks the value of the environment variable MALLOC_TRACE, which should contain the pathname of a file in which the tracing information is to be recorded. If the pathname is successfully opened, it is truncated to zero length.

If MALLOC_TRACE is not set, or the pathname it specifies is invalid or not writable, then no hook functions are installed, and mtrace() has no effect. In set-user-ID and set-group-ID programs, MALLOC_TRACE is ignored, and mtrace() has no effect.

CONFORMING TO

These functions are GNU extensions.

NOTES

In normal usage, mtrace() is called once at the start of execution of a program, and muntrace() is never called.

The tracing output produced after a call to mtrace() is textual, but not designed to be human readable. The GNU C library provides a Perl script, mtrace(1), that interprets the trace log and produces human-readable output. For best results, the traced program should be compiled with debugging enabled, so that line-number information is recorded in the executable.

The tracing performed by mtrace() incurs a performance penalty (if MALLOC_TRACE points to a valid, writable pathname).

BUGS

The line-number information produced by mtrace(1) is not always precise: the line number references may refer to the previous or following (nonblank) line of the source code.

EXAMPLE

The shell session below demonstrates the use of the mtrace() function and the mtrace(1) command in a program that has memory leaks at two different locations. The demonstration uses the following program:

$ cat t_mtrace.c
#include <mcheck.h>
#include <stdlib.h>
#include <stdio.h>

int
main(int argc, char *argv[])
{
int j;

mtrace();

for (j = 0; j < 2; j++)
malloc(100); /* Never freed−−a memory leak */

calloc(16, 16); /* Never freed−−a memory leak */
exit(EXIT_SUCCESS);
}

When we run the program as follows, we see that mtrace() diagnosed memory leaks at two different locations in the program:

$ cc −g t_mtrace.c −o t_mtrace
$ export MALLOC_TRACE=/tmp/t
$ ./t_mtrace
$ mtrace ./t_mtrace $MALLOC_TRACE
Memory not freed:
-----------------
Address Size Caller
0x084c9378 0x64 at /home/cecilia/t_mtrace.c:12
0x084c93e0 0x64 at /home/cecilia/t_mtrace.c:12
0x084c9448 0x100 at /home/cecilia/t_mtrace.c:16

The first two messages about unfreed memory correspond to the two malloc(3) calls inside the for loop. The final message corresponds to the call to calloc(3) (which in turn calls malloc(3)).

SEE ALSO

mtrace(1), malloc(3), malloc_hook(3), mcheck(3)

COLOPHON

This page is part of release 3.69 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at http://www.kernel.org/doc/man−pages/.


Open Source


Free Technology Academy - The FTA provides a virtual campus offering course modules on Free Software and Open Standards. Educational materials in the FTA are released under free licenses. FTA is constructed on a Free Software OS and utilizes Free Software and standards to deliver its services.

5 Billion Dollars of Free Software - The Linux Foundation released a report estimating the value of their open source projects at 5 billion. This software is yours for free. Governments and large corporations are using this incredible gift to their advantage every day. Learn how open source can benefit you.

Harvard - MIT - Stanford - The open edX platform delivers free online courses from Harvard, MIT, Columbia, UC Berkeley and other top Universities. Hundreds of courses are available and include almost all major subject areas and course levels. Paid courses and certifications are also available.

Free Office Software - The Libre Office Suite is available for Linux or Windows and incldues, Writer, the word processor, Calc, the spreadsheet application, Impress, the presentation engine, Draw, our drawing and flowcharting application, Base, our database and database frontend, and Math for editing mathematics.

What is Linux - Linux is a computer operating system (OS) that is free and open-source software. The software that runs the computer and the source code used to create it are both available to you at no cost. Linux is used on games, watches, laptops, desktops and super computers. The capability to operate and create on these devices is available to you.

ConnectED and edX - President Obama announced on November 2014, that edX and ConnectED will partner to deliver teacher training and AP courses. ConnectED is the President's initiative to enhance K-12 through the delivery of broadband access and digital content.

Free Technology Academy - The FTA provides a virtual campus offering course modules on Free Software and Open Standards. Educational materials in the FTA are released under free licenses. FTA is constructed on a Free Software OS and utilizes Free Software and standards to deliver its services.

Open Opportunity - Free and Open Source software provide unlimited potential for personal and community development. Use open source to build a career, establish a business or change the world. Learn why free software provides the greatest opportunity on the the planet today.

College Prep - Get ready for college or earn college credit. EdX provides more than 40 High School and AP Exam Preparation Courses. Subjects range from mathematics to science, English and history. EdX courses help students succeed in High School and prepare for college. pre-calc calculus

bash - Bash is both a command interpreter and a programming language. As a command interpreter, bash provides the user interface to the rich set of GNU utilities. The programming language features allow these utilities to be combined. Mastering a shell such as bash is important for anyone learning Linux.

Open Government - NASA, The White House and the Department of Defense are among government organizations with major open source deployments. Through the data.gov website which is part of the open government initiative, agencies deliver 180,000 data sets with open source access.

Free Office Software - The Libre Office Suite is available for Linux or Windows and incldues, Writer, the word processor, Calc, the spreadsheet application, Impress, the presentation engine, Draw, our drawing and flowcharting application, Base, our database and database frontend, and Math for editing mathematics.

President Obama Knows Open Source - On January 20 2009, President Obama's first day in office, the Open Government initiative was issued to provide transparency and access to Government data. Learn how our Government is using open source and the opportunities this provides for you.

Harvard - MIT - Stanford - The open edX platform delivers free online courses from Harvard, MIT, Columbia, UC Berkeley and other top Universities. Hundreds of courses are available and include almost all major subject areas and course levels. Paid courses and certifications are also available.

More Linux Commands


FcDirScan (3)        - scan a font directory without caching it
If cache is not zero or if force is FcFalse, this function currently returns FcFalse. Otherwise, it scans an entire directory and adds all fonts found to set. A...

if_indextoname (3)   - mappings between network interface names and indexes
The if_nametoindex() function returns the index of the network interface corresponding to the name ifname. The if_indextoname() function returns the name of the...

pamarith (1)         - perform arithmetic on two Netpbm images
This program is part of Netpbm(1) pamarith reads two PBM, PGM, PPM, or PAM images as input. It performs the specified binary arithmetic operation on their sampl...

PerlIO::encoding (3pm) - encoding layer
This PerlIO layer opens a filehandle with a transparent encoding filter. On input, it converts the bytes expected to be in the specified character set and encod...

grap2graph (1)       - convert a grap diagram into a cropped bitmap image
Reads a grap program as input; produces an image file (by default in Portable Network Graphics format) suitable for the Web as output. For a description of the...

bounce (8)           - Postfix delivery status reports
The bounce(8) daemon maintains per-message log files with delivery status information. Each log file is named after the queue file that it corresponds to, and i...

ntp_misc (5)         - Miscellaneous Options
The broadcast and multicast modes require a special calibration to determine the network delay between the local and remote servers. Ordinarily, this is done......

mount (2)            - mount file system
mount() attaches the filesystem specified by source (which is often a device name, but can also be a directory name or a dummy) to the directory specified by ta...

gropdf (1)           - PDF driver for groff
gropdf translates the output of GNU troff to PDF. Normally gropdf should be invoked by using the groff command with a -Tpdf option. If no files are given, gropd...

tabs (1)             - set tabs on a terminal
The tabs program clears and sets tab-stops on the terminal. This uses the terminfo clear_all_tabs and set_tab capabilities. If either is absent, tabs is unable...

c++filt (1)          - Demangle C++ and Java symbols.
The C ++ and Java languages provide function overloading, which means that you can write many functions with the same name, providing that each function takes p...

XkbGetKeyboardByName (3) - Build a new keyboard description from a set of nam...
A client may request that the server fetch one or more components from its database and use those components to build a new server keyboard description. The new...

sane-s9036 (5)       - SANE backend for Siemens 9036 flatbed scanners
The sane-s9036 library implements a SANE (Scanner Access Now Easy) backend that provides access to Siemens 9036 flatbed scanners. DEVICE NAMES This backend expe...

unix_chkpwd (8)      - Helper binary that verifies the password of the curren...
unix_chkpwd is a helper program for the pam_unix module that verifies the password of the current user. It also checks password and account expiration dates in...

MB_LEN_MAX (3)       - maximum multibyte length of a character across all loc...
The MB_LEN_MAX macro is the upper bound for the number of bytes needed to represent a single wide character, across all locales. RETURN VALUE A constant integer...