283 lines
6.8 KiB
HTML
283 lines
6.8 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of MTRACE</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>MTRACE</H1>
|
|
Section: Linux Programmer's Manual (3)<BR>Updated: 2017-09-15<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
mtrace, muntrace - malloc tracing
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<B>#include <<A HREF="file:///usr/include/mcheck.h">mcheck.h</A>></B>
|
|
|
|
<P>
|
|
|
|
<B>void mtrace(void);</B>
|
|
|
|
<P>
|
|
|
|
<B>void muntrace(void);</B>
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
The
|
|
<B>mtrace</B>()
|
|
|
|
function installs hook functions for the memory-allocation functions
|
|
(<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+realloc">realloc</A></B>(3)
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+memalign">memalign</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+free">free</A></B>(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.
|
|
<P>
|
|
|
|
The
|
|
<B>muntrace</B>()
|
|
|
|
function disables the hook functions installed by
|
|
<B>mtrace</B>(),
|
|
|
|
so that tracing information is no longer recorded
|
|
for the memory-allocation functions.
|
|
If no hook functions were successfully installed by
|
|
<B>mtrace</B>(),
|
|
|
|
<B>muntrace</B>()
|
|
|
|
does nothing.
|
|
<P>
|
|
|
|
When
|
|
<B>mtrace</B>()
|
|
|
|
is called, it checks the value of the environment variable
|
|
<B>MALLOC_TRACE</B>,
|
|
|
|
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.
|
|
<P>
|
|
|
|
If
|
|
<B>MALLOC_TRACE</B>
|
|
|
|
is not set,
|
|
or the pathname it specifies is invalid or not writable,
|
|
then no hook functions are installed, and
|
|
<B>mtrace</B>()
|
|
|
|
has no effect.
|
|
In set-user-ID and set-group-ID programs,
|
|
<B>MALLOC_TRACE</B>
|
|
|
|
is ignored, and
|
|
<B>mtrace</B>()
|
|
|
|
has no effect.
|
|
<A NAME="lbAE"> </A>
|
|
<H2>ATTRIBUTES</H2>
|
|
|
|
For an explanation of the terms used in this section, see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
|
|
|
|
<TABLE BORDER>
|
|
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
|
|
<TR VALIGN=top><TD>
|
|
<B>mtrace</B>(),
|
|
|
|
<B>muntrace</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Unsafe<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<A NAME="lbAF"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
These functions are GNU extensions.
|
|
<A NAME="lbAG"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
In normal usage,
|
|
<B>mtrace</B>()
|
|
|
|
is called once at the start of execution of a program, and
|
|
<B>muntrace</B>()
|
|
|
|
is never called.
|
|
<P>
|
|
|
|
The tracing output produced after a call to
|
|
<B>mtrace</B>()
|
|
|
|
is textual, but not designed to be human readable.
|
|
The GNU C library provides a Perl script,
|
|
<B><A HREF="/cgi-bin/man/man2html?1+mtrace">mtrace</A></B>(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.
|
|
<P>
|
|
|
|
The tracing performed by
|
|
<B>mtrace</B>()
|
|
|
|
incurs a performance penalty (if
|
|
<B>MALLOC_TRACE</B>
|
|
|
|
points to a valid, writable pathname).
|
|
<A NAME="lbAH"> </A>
|
|
<H2>BUGS</H2>
|
|
|
|
The line-number information produced by
|
|
<B><A HREF="/cgi-bin/man/man2html?1+mtrace">mtrace</A></B>(1)
|
|
|
|
is not always precise:
|
|
the line number references may refer to the previous or following (nonblank)
|
|
line of the source code.
|
|
<A NAME="lbAI"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
The shell session below demonstrates the use of the
|
|
<B>mtrace</B>()
|
|
|
|
function and the
|
|
<B><A HREF="/cgi-bin/man/man2html?1+mtrace">mtrace</A></B>(1)
|
|
|
|
command in a program that has memory leaks at two different locations.
|
|
The demonstration uses the following program:
|
|
<P>
|
|
|
|
|
|
|
|
$ <B>cat t_mtrace.c</B>
|
|
|
|
#include <<A HREF="file:///usr/include/mcheck.h">mcheck.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> int j;
|
|
<P>
|
|
<BR> mtrace();
|
|
<P>
|
|
<BR> for (j = 0; j < 2; j++)
|
|
<BR> malloc(100); /* Never freed--a memory leak */
|
|
<P>
|
|
<BR> calloc(16, 16); /* Never freed--a memory leak */
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
|
|
<P>
|
|
|
|
When we run the program as follows, we see that
|
|
<B>mtrace</B>()
|
|
|
|
diagnosed memory leaks at two different locations in the program:
|
|
<P>
|
|
|
|
|
|
|
|
$ <B>cc -g t_mtrace.c -o t_mtrace</B>
|
|
|
|
$ <B>export MALLOC_TRACE=/tmp/t</B>
|
|
|
|
$ <B>./t_mtrace</B>
|
|
|
|
$ <B>mtrace ./t_mtrace $MALLOC_TRACE</B>
|
|
|
|
Memory not freed:
|
|
-----------------
|
|
<BR> 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
|
|
|
|
|
|
<P>
|
|
|
|
The first two messages about unfreed memory correspond to the two
|
|
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3)
|
|
|
|
calls inside the
|
|
<I>for</I>
|
|
|
|
loop.
|
|
The final message corresponds to the call to
|
|
<B><A HREF="/cgi-bin/man/man2html?3+calloc">calloc</A></B>(3)
|
|
|
|
(which in turn calls
|
|
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3)).
|
|
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?1+mtrace">mtrace</A></B>(1),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+malloc_hook">malloc_hook</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+mcheck">mcheck</A></B>(3)
|
|
|
|
<A NAME="lbAK"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="1"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="2"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="3"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="4"><A HREF="#lbAE">ATTRIBUTES</A><DD>
|
|
<DT id="5"><A HREF="#lbAF">CONFORMING TO</A><DD>
|
|
<DT id="6"><A HREF="#lbAG">NOTES</A><DD>
|
|
<DT id="7"><A HREF="#lbAH">BUGS</A><DD>
|
|
<DT id="8"><A HREF="#lbAI">EXAMPLE</A><DD>
|
|
<DT id="9"><A HREF="#lbAJ">SEE ALSO</A><DD>
|
|
<DT id="10"><A HREF="#lbAK">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:48 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|