1 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .TH MTRACE 3 2021-03-22 "GNU" "Linux Programmer's Manual"
7 mtrace, muntrace \- malloc tracing
10 .RI ( libc ", " \-lc )
13 .B "#include <mcheck.h>"
15 .B "void mtrace(void);"
16 .B "void muntrace(void);"
21 function installs hook functions for the memory-allocation functions
26 These hook functions record tracing information about memory allocation
28 The tracing information can be used to discover memory leaks and
29 attempts to free nonallocated memory in a program.
33 function disables the hook functions installed by
35 so that tracing information is no longer recorded
36 for the memory-allocation functions.
37 If no hook functions were successfully installed by
44 is called, it checks the value of the environment variable
46 which should contain the pathname of a file in which
47 the tracing information is to be recorded.
48 If the pathname is successfully opened, it is truncated to zero length.
53 or the pathname it specifies is invalid or not writable,
54 then no hook functions are installed, and
57 In set-user-ID and set-group-ID programs,
63 For an explanation of the terms used in this section, see
71 Interface Attribute Value
75 T} Thread safety MT-Unsafe
80 .\" FIXME: The marking is different from that in the glibc manual,
81 .\" markings in glibc manual are more detailed:
83 .\" mtrace: MT-Unsafe env race:mtrace const:malloc_hooks init
84 .\" muntrace: MT-Unsafe race:mtrace const:malloc_hooks locale
86 .\" But there is something wrong in glibc manual, for example:
87 .\" glibc manual says muntrace should have marking locale because it calls
88 .\" fprintf(), but muntrace does not execute area which cause locale problem.
90 These functions are GNU extensions.
94 is called once at the start of execution of a program, and
98 The tracing output produced after a call to
100 is textual, but not designed to be human readable.
101 The GNU C library provides a Perl script,
103 that interprets the trace log and produces human-readable output.
105 the traced program should be compiled with debugging enabled,
106 so that line-number information is recorded in the executable.
108 The tracing performed by
110 incurs a performance penalty (if
112 points to a valid, writable pathname).
114 The line-number information produced by
116 is not always precise:
117 the line number references may refer to the previous or following (nonblank)
118 line of the source code.
120 The shell session below demonstrates the use of the
124 command in a program that has memory leaks at two different locations.
125 The demonstration uses the following program:
129 .RB "$ " "cat t_mtrace.c"
135 main(int argc, char *argv[])
139 for (int j = 0; j < 2; j++)
140 malloc(100); /* Never freed\-\-a memory leak */
142 calloc(16, 16); /* Never freed\-\-a memory leak */
148 When we run the program as follows, we see that
150 diagnosed memory leaks at two different locations in the program:
154 .RB "$ " "cc \-g t_mtrace.c \-o t_mtrace"
155 .RB "$ " "export MALLOC_TRACE=/tmp/t"
156 .RB "$ " "./t_mtrace"
157 .RB "$ " "mtrace ./t_mtrace $MALLOC_TRACE"
159 -\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
161 0x084c9378 0x64 at /home/cecilia/t_mtrace.c:12
162 0x084c93e0 0x64 at /home/cecilia/t_mtrace.c:12
163 0x084c9448 0x100 at /home/cecilia/t_mtrace.c:16
167 The first two messages about unfreed memory correspond to the two
172 The final message corresponds to the call to