]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/mtrace.3
mmap.2, a64l.3, abort.3, abs.3, acos.3, acosh.3, addseverity.3, adjtime.3, aio_cancel...
[thirdparty/man-pages.git] / man3 / mtrace.3
1 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
2 .\"
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.
7 .\"
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.
12 .\"
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
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .TH MTRACE 3 2015-03-02 "GNU" "Linux Programmer's Manual"
26 .SH NAME
27 mtrace, muntrace \- malloc tracing
28 .SH SYNOPSIS
29 .B "#include <mcheck.h>"
30 .sp
31 .B "void mtrace(void);"
32 .sp
33 .B "void muntrace(void);"
34 .SH DESCRIPTION
35 The
36 .BR mtrace ()
37 function installs hook functions for the memory-allocation functions
38 .RB ( malloc (3),
39 .BR realloc (3)
40 .BR memalign (3),
41 .BR free (3)).
42 These hook functions record tracing information about memory allocation
43 and deallocation.
44 The tracing information can be used to discover memory leaks and
45 attempts to free nonallocated memory in a program.
46
47 The
48 .BR muntrace ()
49 function disables the hook functions installed by
50 .BR mtrace (),
51 so that tracing information is no longer recorded
52 for the memory-allocation functions.
53 If no hook functions were successfully installed by
54 .BR mtrace (),
55 .BR muntrace ()
56 does nothing.
57
58 When
59 .BR mtrace ()
60 is called, it checks the value of the environment variable
61 .BR MALLOC_TRACE ,
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.
65
66 If
67 .BR MALLOC_TRACE
68 is not set,
69 or the pathname it specifies is invalid or not writable,
70 then no hook functions are installed, and
71 .BR mtrace ()
72 has no effect.
73 In set-user-ID and set-group-ID programs,
74 .BR MALLOC_TRACE
75 is ignored, and
76 .BR mtrace ()
77 has no effect.
78 .SH ATTRIBUTES
79 For an explanation of the terms used in this section, see
80 .BR attributes (7).
81 .TS
82 allbox;
83 lbw20 lb lb
84 l l l.
85 Interface Attribute Value
86 T{
87 .BR mtrace (),
88 .BR muntrace ()
89 .\" FIXME: the following marking is different from which in glibc manual,
90 .\" markings in glibc manual are more detailed.
91 .\" mtrace: MT-Unsafe env race:mtrace const:malloc_hooks init
92 .\" muntrace: MT-Unsafe race:mtrace const:malloc_hooks locale
93 .\" But there is something wrong in glibc manual, for example:
94 .\" glibc manual says muntrace should have marking locale because it calls
95 .\" fprintf(), but muntrace does not execute area which cause locale problem.
96 T} Thread safety MT-Unsafe
97 .TE
98 .SH CONFORMING TO
99 These functions are GNU extensions.
100 .SH NOTES
101 In normal usage,
102 .BR mtrace ()
103 is called once at the start of execution of a program, and
104 .BR muntrace ()
105 is never called.
106
107 The tracing output produced after a call to
108 .BR mtrace ()
109 is textual, but not designed to be human readable.
110 The GNU C library provides a Perl script,
111 .BR mtrace (1),
112 that interprets the trace log and produces human-readable output.
113 For best results,
114 the traced program should be compiled with debugging enabled,
115 so that line-number information is recorded in the executable.
116
117 The tracing performed by
118 .BR mtrace ()
119 incurs a performance penalty (if
120 .B MALLOC_TRACE
121 points to a valid, writable pathname).
122 .SH BUGS
123 The line-number information produced by
124 .BR mtrace (1)
125 is not always precise:
126 the line number references may refer to the previous or following (nonblank)
127 line of the source code.
128 .SH EXAMPLE
129 The shell session below demonstrates the use of the
130 .BR mtrace ()
131 function and the
132 .BR mtrace (1)
133 command in a program that has memory leaks at two different locations.
134 The demonstration uses the following program:
135 .in +4
136 .nf
137
138 .RB "$ " "cat t_mtrace.c"
139 #include <mcheck.h>
140 #include <stdlib.h>
141 #include <stdio.h>
142
143 int
144 main(int argc, char *argv[])
145 {
146 int j;
147
148 mtrace();
149
150 for (j = 0; j < 2; j++)
151 malloc(100); /* Never freed\-\-a memory leak */
152
153 calloc(16, 16); /* Never freed\-\-a memory leak */
154 exit(EXIT_SUCCESS);
155 }
156
157 .fi
158 .in
159 When we run the program as follows, we see that
160 .BR mtrace ()
161 diagnosed memory leaks at two different locations in the program:
162 .in +4n
163 .nf
164
165 .RB "$ " "cc \-g t_mtrace.c \-o t_mtrace"
166 .RB "$ " "export MALLOC_TRACE=/tmp/t"
167 .RB "$ " "./t_mtrace"
168 .RB "$ " "mtrace ./t_mtrace $MALLOC_TRACE"
169 Memory not freed:
170 -----------------
171 Address Size Caller
172 0x084c9378 0x64 at /home/cecilia/t_mtrace.c:12
173 0x084c93e0 0x64 at /home/cecilia/t_mtrace.c:12
174 0x084c9448 0x100 at /home/cecilia/t_mtrace.c:16
175 .fi
176 .in
177
178 The first two messages about unfreed memory correspond to the two
179 .BR malloc (3)
180 calls inside the
181 .I for
182 loop.
183 The final message corresponds to the call to
184 .BR calloc (3)
185 (which in turn calls
186 .BR malloc (3)).
187 .SH SEE ALSO
188 .BR mtrace (1),
189 .BR malloc (3),
190 .BR malloc_hook (3),
191 .BR mcheck (3)