OSDN Git Service

(split) LDP: Update original to LDP v3.40.
[linuxjm/LDP_man-pages.git] / original / man3 / mcheck.3
1 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
2 .\"
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.
6 .\"
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.
11 .\"
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
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\"
23 .TH MCHECK 3  2012-04-18 "GNU" "Linux Programmer's Manual"
24 .SH NAME
25 mcheck, mcheck_check_all, mcheck_pedantic, mprobe - heap consistency checking
26 .SH SYNOPSIS
27 .nf
28 .B #include <mcheck.h>
29 .sp
30 .BI "int mcheck(void (*" abortfunc ")(enum mcheck_status " mstatus ));
31
32 .BI "int mcheck_pedantic(void (*" abortfunc ")(enum mcheck_status " mstatus ));
33
34 .B void mcheck_check_all(void);
35
36 .BI "enum mcheck_status mprobe(void *" ptr );
37
38 .fi
39 .SH DESCRIPTION
40 The
41 .BR mcheck ()
42 function installs a set of debugging hooks for the
43 .BR malloc (3)
44 family of memory-allocation functions.
45 These hooks cause certain consistency checks to be performed
46 on the state of the heap.
47 The checks can detect application errors such as freeing a block of memory
48 more than once or corrupting the bookkeeping data structures
49 that immediately precede a block of allocated memory.
50
51 To be effective, the
52 .BR mcheck ()
53 function must be called before the first call to
54 .BR malloc (3)
55 or a related function.
56 In cases where this is difficult to ensure, linking the program with
57 .IR \-mcheck
58 inserts an implicit call to
59 .BR mcheck ()
60 (with a NULL argument)
61 before the first call to a memory-allocation function.
62
63 The
64 .BR mcheck_pedantic ()
65 function is similar to
66 .BR mcheck (),
67 but performs checks on all allocated blocks whenever
68 one of the memory-allocation functions is called.
69 This can be very slow!
70
71 The
72 .BR mcheck_check_all ()
73 function causes an immediate check on all allocated blocks.
74 This call is only effective if
75 .BR mcheck ()
76 is called beforehand.
77
78 If the system detects an inconsistency in the heap,
79 the caller-supplied function pointed to by
80 .I abortfunc
81 is invoked with a single argument argument,
82 .IR mstatus ,
83 that indicates what type of inconsistency was detected.
84 If
85 .I abortfunc
86 is NULL, a default function prints an error message on
87 .IR stderr
88 and calls
89 .BR abort (3).
90
91 The
92 .BR mprobe ()
93 function performs a consistency check on
94 the block of allocated memory pointed to by
95 .IR ptr .
96 The
97 .BR mcheck ()
98 function should be called beforehand (otherwise
99 .BR mprobe ()
100 returns
101 .BR MCHECK_DISABLED ).
102
103 The following list describes the values returned by
104 .BR mprobe ()
105 or passed as the
106 .I mstatus
107 argument when
108 .I abortfunc
109 is invoked:
110 .TP
111 .BR MCHECK_DISABLED " (" mprobe "() only)"
112 .BR mcheck ()
113 was not called before the first memory allocation function was called.
114 Consistency checking is not possible.
115 .TP
116 .BR MCHECK_OK " (" mprobe "() only)"
117 No inconsistency detected.
118 .TP
119 .B MCHECK_HEAD
120 Memory preceding an allocated block was clobbered.
121 .TP
122 .B MCHECK_TAIL
123 Memory following an allocated block was clobbered.
124 .TP
125 .B
126 MCHECK_FREE
127 A block of memory was freed twice.
128 .SH RETURN VALUE
129 .BR mcheck ()
130 and
131 .BR mcheck_pedantic ()
132 return 0 on success, or \-1 on error.
133 .SH VERSIONS
134 The
135 .BR mcheck_pedantic ()
136 and
137 .BR mcheck_check_all ()
138 functions are available since glibc 2.2.
139 The
140 .BR mcheck()
141 and
142 .BR mprobe()
143 functions are present since at least glibc 2.0
144 .SH CONFORMING TO
145 These functions are GNU extensions.
146 .SH NOTES
147 Linking a program with
148 .I \-lmcheck
149 and using the
150 .B MALLOC_CHECK_
151 environment variable (described in
152 .BR mallopt (3))
153 cause the same kinds of errors to be detected.
154 But, using
155 .B MALLOC_CHECK_
156 does not require the application to be relinked.
157 .\" But is MALLOC_CHECK_ slower?
158
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 .\" FIXME add SEE ALSO pointer from malloc(3) to here
207 .\" FIXME add SEE ALSO pointer from mallopt(3) to here
208 .\" FIXME add SEE ALSO pointer from mtrace(3) to here
209 .BR mallopt (3),
210 .BR mtrace (3)