1 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .TH MTRACE 3 2012-04-18 "GNU" "Linux Programmer's Manual"
25 mtrace, muntrace \- malloc tracing
27 .B "#include <mcheck.h>"
29 .B "void mtrace(void);"
31 .B "void muntrace(void);"
35 function installs hook functions for the memory-allocation functions
40 These hook functions record tracing information about memory allocation
42 The tracing information can be used to discover memory leaks and
43 attempts to free nonallocated memory in a program.
47 function disables the hook functions installed by
49 so that tracing information is no longer recorded
50 for the memory-allocation functions.
51 If no hook functions were successfully installed by
58 is called, it checks the value of the environment variable
60 which should contain the pathname of a file in which
61 the tracing information is to be recorded.
62 If the pathname is successfully opened, it is truncated to zero length.
67 or the pathname it specifies is invalid or not writable,
68 then no hook functions are installed, and
71 In set-user-ID and set-group-ID programs,
77 These functions are GNU extensions.
81 is called once at the start of execution of a program, and
85 The tracing output produced after a call to
87 is textual, but not designed to be human readable.
88 The GNU C library provides a Perl script,
90 that interprets the trace log and produces human-readable output.
92 the traced program should be compiled with debugging enabled,
93 so that line-number information is recorded in the executable.
95 The tracing performed by
97 incurs a performance penalty (if
99 points to a valid, writable pathname).
101 The line-number information produced by
103 is not always precise:
104 the line number references may refer to the previous or following (non-blank)
105 line of the source code.
107 The shell session below demonstrates the use of the
111 command in a program that has memory leaks at two different locations.
112 The demonstration uses the following program:
116 .RB "$ " "cat t_mtrace.c"
122 main(int argc, char *argv[])
128 for (j = 0; j < 2; j++)
129 malloc(100); /* Never freed\-\-a memory leak */
131 calloc(16, 16); /* Never freed\-\-a memory leak */
137 When we run the program as follows, we see that
139 diagnosed memory leaks at two different locations in the program:
143 .RB "$ " "cc \-g t_mtrace.c \-o t_mtrace"
144 .RB "$ " "export MALLOC_TRACE=/tmp/t"
145 .RB "$ " "./t_mtrace"
146 .RB "$ " "mtrace ./t_mtrace $MALLOC_TRACE"
150 0x084c9378 0x64 at /home/cecilia/t_mtrace.c:12
151 0x084c93e0 0x64 at /home/cecilia/t_mtrace.c:12
152 0x084c9448 0x100 at /home/cecilia/t_mtrace.c:16
156 The first two messages about unfreed memory correspond to the two
161 The final message corresponds to the call to