OSDN Git Service

LDP: Update original to LDP v3.79
[linuxjm/LDP_man-pages.git] / original / man3 / mcheck.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 MCHECK 3  2014-01-11 "GNU" "Linux Programmer's Manual"
26 .SH NAME
27 mcheck, mcheck_check_all, mcheck_pedantic, mprobe \- heap consistency checking
28 .SH SYNOPSIS
29 .nf
30 .B #include <mcheck.h>
31 .sp
32 .BI "int mcheck(void (*" abortfunc ")(enum mcheck_status " mstatus ));
33
34 .BI "int mcheck_pedantic(void (*" abortfunc ")(enum mcheck_status " mstatus ));
35
36 .B void mcheck_check_all(void);
37
38 .BI "enum mcheck_status mprobe(void *" ptr );
39 .fi
40 .SH DESCRIPTION
41 The
42 .BR mcheck ()
43 function installs a set of debugging hooks for the
44 .BR malloc (3)
45 family of memory-allocation functions.
46 These hooks cause certain consistency checks to be performed
47 on the state of the heap.
48 The checks can detect application errors such as freeing a block of memory
49 more than once or corrupting the bookkeeping data structures
50 that immediately precede a block of allocated memory.
51
52 To be effective, the
53 .BR mcheck ()
54 function must be called before the first call to
55 .BR malloc (3)
56 or a related function.
57 In cases where this is difficult to ensure, linking the program with
58 .IR \-lmcheck
59 inserts an implicit call to
60 .BR mcheck ()
61 (with a NULL argument)
62 before the first call to a memory-allocation function.
63
64 The
65 .BR mcheck_pedantic ()
66 function is similar to
67 .BR mcheck (),
68 but performs checks on all allocated blocks whenever
69 one of the memory-allocation functions is called.
70 This can be very slow!
71
72 The
73 .BR mcheck_check_all ()
74 function causes an immediate check on all allocated blocks.
75 This call is effective only if
76 .BR mcheck ()
77 is called beforehand.
78
79 If the system detects an inconsistency in the heap,
80 the caller-supplied function pointed to by
81 .I abortfunc
82 is invoked with a single argument argument,
83 .IR mstatus ,
84 that indicates what type of inconsistency was detected.
85 If
86 .I abortfunc
87 is NULL, a default function prints an error message on
88 .IR stderr
89 and calls
90 .BR abort (3).
91
92 The
93 .BR mprobe ()
94 function performs a consistency check on
95 the block of allocated memory pointed to by
96 .IR ptr .
97 The
98 .BR mcheck ()
99 function should be called beforehand (otherwise
100 .BR mprobe ()
101 returns
102 .BR MCHECK_DISABLED ).
103
104 The following list describes the values returned by
105 .BR mprobe ()
106 or passed as the
107 .I mstatus
108 argument when
109 .I abortfunc
110 is invoked:
111 .TP
112 .BR MCHECK_DISABLED " (" mprobe "() only)"
113 .BR mcheck ()
114 was not called before the first memory allocation function was called.
115 Consistency checking is not possible.
116 .TP
117 .BR MCHECK_OK " (" mprobe "() only)"
118 No inconsistency detected.
119 .TP
120 .B MCHECK_HEAD
121 Memory preceding an allocated block was clobbered.
122 .TP
123 .B MCHECK_TAIL
124 Memory following an allocated block was clobbered.
125 .TP
126 .B
127 MCHECK_FREE
128 A block of memory was freed twice.
129 .SH RETURN VALUE
130 .BR mcheck ()
131 and
132 .BR mcheck_pedantic ()
133 return 0 on success, or \-1 on error.
134 .SH VERSIONS
135 The
136 .BR mcheck_pedantic ()
137 and
138 .BR mcheck_check_all ()
139 functions are available since glibc 2.2.
140 The
141 .BR mcheck ()
142 and
143 .BR mprobe ()
144 functions are present since at least glibc 2.0
145 .SH CONFORMING TO
146 These functions are GNU extensions.
147 .SH NOTES
148 Linking a program with
149 .I \-lmcheck
150 and using the
151 .B MALLOC_CHECK_
152 environment variable (described in
153 .BR mallopt (3))
154 cause the same kinds of errors to be detected.
155 But, using
156 .B MALLOC_CHECK_
157 does not require the application to be relinked.
158 .\" But is MALLOC_CHECK_ slower?
159 .SH EXAMPLE
160 The program below calls
161 .BR mcheck ()
162 with a NULL argument and then frees the same block of memory twice.
163 The following shell session demonstrates what happens
164 when running the program:
165 .in +4n
166 .nf
167
168 .RB "$" " ./a.out"
169 About to free
170
171 About to free a second time
172 block freed twice
173 Aborted (core dumped)
174 .fi
175 .in
176 .SS Program source
177 \&
178 .nf
179 #include <stdlib.h>
180 #include <stdio.h>
181 #include <mcheck.h>
182
183 int
184 main(int argc, char *argv[])
185 {
186     char *p;
187
188     if (mcheck(NULL) != 0) {
189         fprintf(stderr, "mcheck() failed\\n");
190
191         exit(EXIT_FAILURE);
192     }
193
194     p = malloc(1000);
195
196     fprintf(stderr, "About to free\\n");
197     free(p);
198     fprintf(stderr, "\\nAbout to free a second time\\n");
199     free(p);
200
201     exit(EXIT_SUCCESS);
202 }
203 .fi
204 .SH SEE ALSO
205 .BR malloc (3),
206 .BR mallopt (3),
207 .BR mtrace (3)
208 .SH COLOPHON
209 This page is part of release 3.79 of the Linux
210 .I man-pages
211 project.
212 A description of the project,
213 information about reporting bugs,
214 and the latest version of this page,
215 can be found at
216 \%http://www.kernel.org/doc/man\-pages/.