1 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .TH MTRACE 3 2017-09-15 "GNU" "Linux Programmer's Manual"
27 mtrace, muntrace \- malloc tracing
29 .B "#include <mcheck.h>"
31 .B "void mtrace(void);"
33 .B "void muntrace(void);"
37 function installs hook functions for the memory-allocation functions
42 These hook functions record tracing information about memory allocation
44 The tracing information can be used to discover memory leaks and
45 attempts to free nonallocated memory in a program.
49 function disables the hook functions installed by
51 so that tracing information is no longer recorded
52 for the memory-allocation functions.
53 If no hook functions were successfully installed by
60 is called, it checks the value of the environment variable
62 which should contain the pathname of a file in which
63 the tracing information is to be recorded.
64 If the pathname is successfully opened, it is truncated to zero length.
69 or the pathname it specifies is invalid or not writable,
70 then no hook functions are installed, and
73 In set-user-ID and set-group-ID programs,
79 For an explanation of the terms used in this section, see
85 Interface Attribute Value
89 T} Thread safety MT-Unsafe
91 .\" FIXME: The marking is different from that in the glibc manual,
92 .\" markings in glibc manual are more detailed:
94 .\" mtrace: MT-Unsafe env race:mtrace const:malloc_hooks init
95 .\" muntrace: MT-Unsafe race:mtrace const:malloc_hooks locale
97 .\" But there is something wrong in glibc manual, for example:
98 .\" glibc manual says muntrace should have marking locale because it calls
99 .\" fprintf(), but muntrace does not execute area which cause locale problem.
101 These functions are GNU extensions.
105 is called once at the start of execution of a program, and
109 The tracing output produced after a call to
111 is textual, but not designed to be human readable.
112 The GNU C library provides a Perl script,
114 that interprets the trace log and produces human-readable output.
116 the traced program should be compiled with debugging enabled,
117 so that line-number information is recorded in the executable.
119 The tracing performed by
121 incurs a performance penalty (if
123 points to a valid, writable pathname).
125 The line-number information produced by
127 is not always precise:
128 the line number references may refer to the previous or following (nonblank)
129 line of the source code.
131 The shell session below demonstrates the use of the
135 command in a program that has memory leaks at two different locations.
136 The demonstration uses the following program:
140 .RB "$ " "cat t_mtrace.c"
146 main(int argc, char *argv[])
152 for (j = 0; j < 2; j++)
153 malloc(100); /* Never freed\-\-a memory leak */
155 calloc(16, 16); /* Never freed\-\-a memory leak */
161 When we run the program as follows, we see that
163 diagnosed memory leaks at two different locations in the program:
167 .RB "$ " "cc \-g t_mtrace.c \-o t_mtrace"
168 .RB "$ " "export MALLOC_TRACE=/tmp/t"
169 .RB "$ " "./t_mtrace"
170 .RB "$ " "mtrace ./t_mtrace $MALLOC_TRACE"
174 0x084c9378 0x64 at /home/cecilia/t_mtrace.c:12
175 0x084c93e0 0x64 at /home/cecilia/t_mtrace.c:12
176 0x084c9448 0x100 at /home/cecilia/t_mtrace.c:16
180 The first two messages about unfreed memory correspond to the two
185 The final message corresponds to the call to