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