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       void muntrace(void);
13

DESCRIPTION

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

ATTRIBUTES

37       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
38       tributes(7).
39
40       ┌──────────────────────────────────────────┬───────────────┬───────────┐
41Interface                                 Attribute     Value     
42       ├──────────────────────────────────────────┼───────────────┼───────────┤
43mtrace(), muntrace()                      │ Thread safety │ MT-Unsafe │
44       └──────────────────────────────────────────┴───────────────┴───────────┘
45

CONFORMING TO

47       These functions are GNU extensions.
48

NOTES

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

BUGS

64       The line-number information produced by mtrace(1) is  not  always  pre‐
65       cise: the line number references may refer to the previous or following
66       (nonblank) line of the source code.
67

EXAMPLES

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

SEE ALSO

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

COLOPHON

112       This  page  is  part of release 5.12 of the Linux man-pages project.  A
113       description of the project, information about reporting bugs,  and  the
114       latest     version     of     this    page,    can    be    found    at
115       https://www.kernel.org/doc/man-pages/.
116
117
118
119GNU                               2021-03-22                         MTRACE(3)
Impressum