]>
Commit | Line | Data |
---|---|---|
c196e485 MK |
1 | .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com> |
2 | .\" | |
93015253 | 3 | .\" %%%LICENSE_START(VERBATIM) |
c196e485 MK |
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. | |
4b72fb64 | 23 | .\" %%%LICENSE_END |
c196e485 | 24 | .\" |
4b8c67d9 | 25 | .TH MTRACE 3 2017-09-15 "GNU" "Linux Programmer's Manual" |
fea681da | 26 | .SH NAME |
c196e485 | 27 | mtrace, muntrace \- malloc tracing |
fea681da | 28 | .SH SYNOPSIS |
fea681da | 29 | .B "#include <mcheck.h>" |
68e4db0a | 30 | .PP |
fea681da | 31 | .B "void mtrace(void);" |
68e4db0a | 32 | .PP |
fea681da MK |
33 | .B "void muntrace(void);" |
34 | .SH DESCRIPTION | |
c196e485 | 35 | The |
fea681da | 36 | .BR mtrace () |
c196e485 MK |
37 | function installs hook functions for the memory-allocation functions |
38 | .RB ( malloc (3), | |
fb186734 | 39 | .BR realloc (3) |
c196e485 MK |
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. | |
847e0d88 | 46 | .PP |
c196e485 | 47 | The |
fea681da | 48 | .BR muntrace () |
c196e485 MK |
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. | |
847e0d88 | 57 | .PP |
c196e485 | 58 | When |
eace9488 | 59 | .BR mtrace () |
c196e485 MK |
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. | |
847e0d88 | 65 | .PP |
c196e485 MK |
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 | |
fea681da | 71 | .BR mtrace () |
c196e485 MK |
72 | has no effect. |
73 | In set-user-ID and set-group-ID programs, | |
74 | .BR MALLOC_TRACE | |
75 | is ignored, and | |
fea681da | 76 | .BR mtrace () |
c196e485 | 77 | has no effect. |
871faea4 PH |
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 () | |
03360f97 MK |
89 | T} Thread safety MT-Unsafe |
90 | .TE | |
da353ffc | 91 | .\" FIXME: The marking is different from that in the glibc manual, |
03360f97 MK |
92 | .\" markings in glibc manual are more detailed: |
93 | .\" | |
94 | .\" mtrace: MT-Unsafe env race:mtrace const:malloc_hooks init | |
95 | .\" muntrace: MT-Unsafe race:mtrace const:malloc_hooks locale | |
96 | .\" | |
19c7049d | 97 | .\" But there is something wrong in glibc manual, for example: |
60570269 | 98 | .\" glibc manual says muntrace should have marking locale because it calls |
19c7049d | 99 | .\" fprintf(), but muntrace does not execute area which cause locale problem. |
47297adb | 100 | .SH CONFORMING TO |
c196e485 | 101 | These functions are GNU extensions. |
19c98696 | 102 | .SH NOTES |
c196e485 MK |
103 | In normal usage, |
104 | .BR mtrace () | |
105 | is called once at the start of execution of a program, and | |
106 | .BR muntrace () | |
107 | is never called. | |
847e0d88 | 108 | .PP |
c196e485 MK |
109 | The tracing output produced after a call to |
110 | .BR mtrace () | |
111 | is textual, but not designed to be human readable. | |
112 | The GNU C library provides a Perl script, | |
113 | .BR mtrace (1), | |
114 | that interprets the trace log and produces human-readable output. | |
115 | For best results, | |
116 | the traced program should be compiled with debugging enabled, | |
117 | so that line-number information is recorded in the executable. | |
847e0d88 | 118 | .PP |
c196e485 MK |
119 | The tracing performed by |
120 | .BR mtrace () | |
121 | incurs a performance penalty (if | |
122 | .B MALLOC_TRACE | |
123 | points to a valid, writable pathname). | |
124 | .SH BUGS | |
125 | The line-number information produced by | |
126 | .BR mtrace (1) | |
127 | is not always precise: | |
246414a6 | 128 | the line number references may refer to the previous or following (nonblank) |
c196e485 MK |
129 | line of the source code. |
130 | .SH EXAMPLE | |
131 | The shell session below demonstrates the use of the | |
132 | .BR mtrace () | |
133 | function and the | |
134 | .BR mtrace (1) | |
135 | command in a program that has memory leaks at two different locations. | |
136 | The demonstration uses the following program: | |
207050fa | 137 | .PP |
c196e485 | 138 | .in +4 |
207050fa | 139 | .EX |
c196e485 MK |
140 | .RB "$ " "cat t_mtrace.c" |
141 | #include <mcheck.h> | |
142 | #include <stdlib.h> | |
143 | #include <stdio.h> | |
144 | ||
145 | int | |
146 | main(int argc, char *argv[]) | |
147 | { | |
148 | int j; | |
149 | ||
150 | mtrace(); | |
151 | ||
152 | for (j = 0; j < 2; j++) | |
153 | malloc(100); /* Never freed\-\-a memory leak */ | |
154 | ||
155 | calloc(16, 16); /* Never freed\-\-a memory leak */ | |
156 | exit(EXIT_SUCCESS); | |
157 | } | |
e646a1ba | 158 | .EE |
c196e485 | 159 | .in |
e646a1ba | 160 | .PP |
c196e485 | 161 | When we run the program as follows, we see that |
fea681da | 162 | .BR mtrace () |
c196e485 | 163 | diagnosed memory leaks at two different locations in the program: |
e646a1ba | 164 | .PP |
c196e485 | 165 | .in +4n |
e646a1ba | 166 | .EX |
c196e485 MK |
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" | |
171 | Memory not freed: | |
172 | ----------------- | |
173 | Address Size Caller | |
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 | |
b8302363 | 177 | .EE |
c196e485 | 178 | .in |
847e0d88 | 179 | .PP |
c196e485 MK |
180 | The first two messages about unfreed memory correspond to the two |
181 | .BR malloc (3) | |
182 | calls inside the | |
183 | .I for | |
184 | loop. | |
185 | The final message corresponds to the call to | |
186 | .BR calloc (3) | |
187 | (which in turn calls | |
188 | .BR malloc (3)). | |
47297adb | 189 | .SH SEE ALSO |
c196e485 | 190 | .BR mtrace (1), |
fea681da | 191 | .BR malloc (3), |
361c3c7f MK |
192 | .BR malloc_hook (3), |
193 | .BR mcheck (3) |