2 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .TH mtrace 3 (date) "Linux man-pages (unreleased)"
8 mtrace, muntrace \- malloc tracing
11 .RI ( libc ", " \-lc )
14 .B "#include <mcheck.h>"
16 .B "void mtrace(void);"
17 .B "void muntrace(void);"
22 function installs hook functions for the memory-allocation functions
27 These hook functions record tracing information about memory allocation
29 The tracing information can be used to discover memory leaks and
30 attempts to free nonallocated memory in a program.
34 function disables the hook functions installed by
36 so that tracing information is no longer recorded
37 for the memory-allocation functions.
38 If no hook functions were successfully installed by
45 is called, it checks the value of the environment variable
47 which should contain the pathname of a file in which
48 the tracing information is to be recorded.
49 If the pathname is successfully opened, it is truncated to zero length.
54 or the pathname it specifies is invalid or not writable,
55 then no hook functions are installed, and
58 In set-user-ID and set-group-ID programs,
64 For an explanation of the terms used in this section, see
70 Interface Attribute Value
76 T} Thread safety MT-Unsafe
78 .\" FIXME: The marking is different from that in the glibc manual,
79 .\" markings in glibc manual are more detailed:
81 .\" mtrace: MT-Unsafe env race:mtrace const:malloc_hooks init
82 .\" muntrace: MT-Unsafe race:mtrace const:malloc_hooks locale
84 .\" But there is something wrong in glibc manual, for example:
85 .\" glibc manual says muntrace should have marking locale because it calls
86 .\" fprintf(), but muntrace does not execute area which cause locale problem.
92 is called once at the start of execution of a program, and
96 The tracing output produced after a call to
98 is textual, but not designed to be human readable.
99 The GNU C library provides a Perl script,
101 that interprets the trace log and produces human-readable output.
103 the traced program should be compiled with debugging enabled,
104 so that line-number information is recorded in the executable.
106 The tracing performed by
108 incurs a performance penalty (if
110 points to a valid, writable pathname).
112 The line-number information produced by
114 is not always precise:
115 the line number references may refer to the previous or following (nonblank)
116 line of the source code.
118 The shell session below demonstrates the use of the
122 command in a program that has memory leaks at two different locations.
123 The demonstration uses the following program:
126 .RB "$ " "cat t_mtrace.c"
127 .\" [[memory leak]] SRC BEGIN (t_mtrace.c)
138 for (unsigned int j = 0; j < 2; j++)
139 malloc(100); /* Never freed\-\-a memory leak */
141 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