1MTRACE(3)                  Linux Programmer's Manual                 MTRACE(3)
2
3
4

NAME

6       mtrace, muntrace - malloc tracing
7

SYNOPSIS

9       #include <mcheck.h>
10
11       void mtrace(void);
12
13       void muntrace(void);
14

DESCRIPTION

16       The mtrace() function installs hook functions for the memory-allocation
17       functions (malloc(3), realloc(3)  memalign(3),  free(3)).   These  hook
18       functions  record tracing information about memory allocation and deal‐
19       location.  The tracing information can be used to discover memory leaks
20       and attempts to free nonallocated memory in a program.
21
22       The  muntrace()  function  disables  the  hook  functions  installed by
23       mtrace(), so that tracing information is no  longer  recorded  for  the
24       memory-allocation  functions.   If  no hook functions were successfully
25       installed by mtrace(), muntrace() does nothing.
26
27       When mtrace() is called, it checks the value of the  environment  vari‐
28       able MALLOC_TRACE, which should contain the pathname of a file in which
29       the tracing information is to be recorded.  If the pathname is success‐
30       fully opened, it is truncated to zero length.
31
32       If  MALLOC_TRACE is not set, or the pathname it specifies is invalid or
33       not writable, then no hook functions are installed, and mtrace() has no
34       effect.   In  set-user-ID  and  set-group-ID  programs, MALLOC_TRACE is
35       ignored, and mtrace() has no effect.
36

CONFORMING TO

38       These functions are GNU extensions.
39

NOTES

41       In normal usage, mtrace() is called once at the start of execution of a
42       program, and muntrace() is never called.
43
44       The  tracing  output  produced after a call to mtrace() is textual, but
45       not designed to be human readable.  The GNU C library provides  a  Perl
46       script,  mtrace(1),  that  interprets the trace log and produces human-
47       readable output.  For best results, the traced program should  be  com‐
48       piled  with  debugging  enabled,  so  that  line-number  information is
49       recorded in the executable.
50
51       The tracing performed by mtrace() incurs a performance penalty (if MAL‐
52       LOC_TRACE points to a valid, writable pathname).
53

BUGS

55       The  line-number  information  produced by mtrace(1) is not always pre‐
56       cise: the line number references may refer to the previous or following
57       (non-blank) line of the source code.
58

EXAMPLE

60       The  shell  session below demonstrates the use of the mtrace() function
61       and the mtrace(1) command in a program that has  memory  leaks  at  two
62       different locations.  The demonstration uses the following program:
63
64           $ cat t_mtrace.c
65           #include <mcheck.h>
66           #include <stdlib.h>
67           #include <stdio.h>
68
69           int
70           main(int argc, char *argv[])
71           {
72               int j;
73
74               mtrace();
75
76               for (j = 0; j < 2; j++)
77                   malloc(100);            /* Never freed--a memory leak */
78
79               calloc(16, 16);             /* Never freed--a memory leak */
80               exit(EXIT_SUCCESS);
81           }
82
83       When we run the program as follows, we see that mtrace() diagnosed mem‐
84       ory leaks at two different locations in the program:
85
86           $ cc -g t_mtrace.c -o t_mtrace
87           $ export MALLOC_TRACE=/tmp/t
88           $ ./t_mtrace
89           $ mtrace ./t_mtrace $MALLOC_TRACE
90           Memory not freed:
91           -----------------
92              Address     Size     Caller
93           0x084c9378     0x64  at /home/cecilia/t_mtrace.c:12
94           0x084c93e0     0x64  at /home/cecilia/t_mtrace.c:12
95           0x084c9448    0x100  at /home/cecilia/t_mtrace.c:16
96
97       The first two messages about unfreed memory correspond to the two  mal‐
98       loc(3) calls inside the for loop.  The final message corresponds to the
99       call to calloc(3) (which in turn calls malloc(3)).
100

SEE ALSO

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

COLOPHON

105       This page is part of release 3.53 of the Linux  man-pages  project.   A
106       description  of  the project, and information about reporting bugs, can
107       be found at http://www.kernel.org/doc/man-pages/.
108
109
110
111GNU                               2012-04-18                         MTRACE(3)
Impressum