Blame man3/mtrace.3

Packit 7cfc04
.\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.TH MTRACE 3 2017-09-15 "GNU" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
mtrace, muntrace \- malloc tracing
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.B "#include <mcheck.h>"
Packit 7cfc04
.PP
Packit 7cfc04
.B "void mtrace(void);"
Packit 7cfc04
.PP
Packit 7cfc04
.B "void muntrace(void);"
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The
Packit 7cfc04
.BR mtrace ()
Packit 7cfc04
function installs hook functions for the memory-allocation functions
Packit 7cfc04
.RB ( malloc (3),
Packit 7cfc04
.BR realloc (3)
Packit 7cfc04
.BR memalign (3),
Packit 7cfc04
.BR free (3)).
Packit 7cfc04
These hook functions record tracing information about memory allocation
Packit 7cfc04
and deallocation.
Packit 7cfc04
The tracing information can be used to discover memory leaks and
Packit 7cfc04
attempts to free nonallocated memory in a program.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.BR muntrace ()
Packit 7cfc04
function disables the hook functions installed by
Packit 7cfc04
.BR mtrace (),
Packit 7cfc04
so that tracing information is no longer recorded
Packit 7cfc04
for the memory-allocation functions.
Packit 7cfc04
If no hook functions were successfully installed by
Packit 7cfc04
.BR mtrace (),
Packit 7cfc04
.BR muntrace ()
Packit 7cfc04
does nothing.
Packit 7cfc04
.PP
Packit 7cfc04
When
Packit 7cfc04
.BR mtrace ()
Packit 7cfc04
is called, it checks the value of the environment variable
Packit 7cfc04
.BR MALLOC_TRACE ,
Packit 7cfc04
which should contain the pathname of a file in which
Packit 7cfc04
the tracing information is to be recorded.
Packit 7cfc04
If the pathname is successfully opened, it is truncated to zero length.
Packit 7cfc04
.PP
Packit 7cfc04
If
Packit 7cfc04
.BR MALLOC_TRACE
Packit 7cfc04
is not set,
Packit 7cfc04
or the pathname it specifies is invalid or not writable,
Packit 7cfc04
then no hook functions are installed, and
Packit 7cfc04
.BR mtrace ()
Packit 7cfc04
has no effect.
Packit 7cfc04
In set-user-ID and set-group-ID programs,
Packit 7cfc04
.BR MALLOC_TRACE
Packit 7cfc04
is ignored, and
Packit 7cfc04
.BR mtrace ()
Packit 7cfc04
has no effect.
Packit 7cfc04
.SH ATTRIBUTES
Packit 7cfc04
For an explanation of the terms used in this section, see
Packit 7cfc04
.BR attributes (7).
Packit 7cfc04
.TS
Packit 7cfc04
allbox;
Packit 7cfc04
lbw20 lb lb
Packit 7cfc04
l l l.
Packit 7cfc04
Interface	Attribute	Value
Packit 7cfc04
T{
Packit 7cfc04
.BR mtrace (),
Packit 7cfc04
.BR muntrace ()
Packit 7cfc04
T}	Thread safety	MT-Unsafe
Packit 7cfc04
.TE
Packit 7cfc04
.\" FIXME: The marking is different from that in the glibc manual,
Packit 7cfc04
.\" markings in glibc manual are more detailed:
Packit 7cfc04
.\"
Packit 7cfc04
.\"      mtrace: MT-Unsafe env race:mtrace const:malloc_hooks init
Packit 7cfc04
.\"      muntrace: MT-Unsafe race:mtrace const:malloc_hooks locale
Packit 7cfc04
.\"
Packit 7cfc04
.\" But there is something wrong in glibc manual, for example:
Packit 7cfc04
.\" glibc manual says muntrace should have marking locale because it calls
Packit 7cfc04
.\" fprintf(), but muntrace does not execute area which cause locale problem.
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
These functions are GNU extensions.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
In normal usage,
Packit 7cfc04
.BR mtrace ()
Packit 7cfc04
is called once at the start of execution of a program, and
Packit 7cfc04
.BR muntrace ()
Packit 7cfc04
is never called.
Packit 7cfc04
.PP
Packit 7cfc04
The tracing output produced after a call to
Packit 7cfc04
.BR mtrace ()
Packit 7cfc04
is textual, but not designed to be human readable.
Packit 7cfc04
The GNU C library provides a Perl script,
Packit 7cfc04
.BR mtrace (1),
Packit 7cfc04
that interprets the trace log and produces human-readable output.
Packit 7cfc04
For best results,
Packit 7cfc04
the traced program should be compiled with debugging enabled,
Packit 7cfc04
so that line-number information is recorded in the executable.
Packit 7cfc04
.PP
Packit 7cfc04
The tracing performed by
Packit 7cfc04
.BR mtrace ()
Packit 7cfc04
incurs a performance penalty (if
Packit 7cfc04
.B MALLOC_TRACE
Packit 7cfc04
points to a valid, writable pathname).
Packit 7cfc04
.SH BUGS
Packit 7cfc04
The line-number information produced by
Packit 7cfc04
.BR mtrace (1)
Packit 7cfc04
is not always precise:
Packit 7cfc04
the line number references may refer to the previous or following (nonblank)
Packit 7cfc04
line of the source code.
Packit 7cfc04
.SH EXAMPLE
Packit 7cfc04
The shell session below demonstrates the use of the
Packit 7cfc04
.BR mtrace ()
Packit 7cfc04
function and the
Packit 7cfc04
.BR mtrace (1)
Packit 7cfc04
command in a program that has memory leaks at two different locations.
Packit 7cfc04
The demonstration uses the following program:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4
Packit 7cfc04
.EX
Packit 7cfc04
.RB "$ " "cat t_mtrace.c"
Packit 7cfc04
#include <mcheck.h>
Packit 7cfc04
#include <stdlib.h>
Packit 7cfc04
#include <stdio.h>
Packit 7cfc04
Packit 7cfc04
int
Packit 7cfc04
main(int argc, char *argv[])
Packit 7cfc04
{
Packit 7cfc04
    int j;
Packit 7cfc04
Packit 7cfc04
    mtrace();
Packit 7cfc04
Packit 7cfc04
    for (j = 0; j < 2; j++)
Packit 7cfc04
        malloc(100);            /* Never freed\-\-a memory leak */
Packit 7cfc04
Packit 7cfc04
    calloc(16, 16);             /* Never freed\-\-a memory leak */
Packit 7cfc04
    exit(EXIT_SUCCESS);
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
When we run the program as follows, we see that
Packit 7cfc04
.BR mtrace ()
Packit 7cfc04
diagnosed memory leaks at two different locations in the program:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
.RB "$ " "cc \-g t_mtrace.c \-o t_mtrace"
Packit 7cfc04
.RB "$ " "export MALLOC_TRACE=/tmp/t"
Packit 7cfc04
.RB "$ " "./t_mtrace"
Packit 7cfc04
.RB "$ " "mtrace ./t_mtrace $MALLOC_TRACE"
Packit 7cfc04
Memory not freed:
Packit 7cfc04
-----------------
Packit 7cfc04
   Address     Size     Caller
Packit 7cfc04
0x084c9378     0x64  at /home/cecilia/t_mtrace.c:12
Packit 7cfc04
0x084c93e0     0x64  at /home/cecilia/t_mtrace.c:12
Packit 7cfc04
0x084c9448    0x100  at /home/cecilia/t_mtrace.c:16
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
The first two messages about unfreed memory correspond to the two
Packit 7cfc04
.BR malloc (3)
Packit 7cfc04
calls inside the
Packit 7cfc04
.I for
Packit 7cfc04
loop.
Packit 7cfc04
The final message corresponds to the call to
Packit 7cfc04
.BR calloc (3)
Packit 7cfc04
(which in turn calls
Packit 7cfc04
.BR malloc (3)).
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR mtrace (1),
Packit 7cfc04
.BR malloc (3),
Packit 7cfc04
.BR malloc_hook (3),
Packit 7cfc04
.BR mcheck (3)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.