1 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
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.
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.
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
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .TH MCHECK 3 2012-04-18 "GNU" "Linux Programmer's Manual"
25 mcheck, mcheck_check_all, mcheck_pedantic, mprobe - heap consistency checking
28 .B #include <mcheck.h>
30 .BI "int mcheck(void (*" abortfunc ")(enum mcheck_status " mstatus ));
32 .BI "int mcheck_pedantic(void (*" abortfunc ")(enum mcheck_status " mstatus ));
34 .B void mcheck_check_all(void);
36 .BI "enum mcheck_status mprobe(void *" ptr );
42 function installs a set of debugging hooks for the
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.
53 function must be called before the first call to
55 or a related function.
56 In cases where this is difficult to ensure, linking the program with
58 inserts an implicit call to
60 (with a NULL argument)
61 before the first call to a memory-allocation function.
64 .BR mcheck_pedantic ()
65 function is similar to
67 but performs checks on all allocated blocks whenever
68 one of the memory-allocation functions is called.
69 This can be very slow!
72 .BR mcheck_check_all ()
73 function causes an immediate check on all allocated blocks.
74 This call is only effective if
78 If the system detects an inconsistency in the heap,
79 the caller-supplied function pointed to by
81 is invoked with a single argument argument,
83 that indicates what type of inconsistency was detected.
86 is NULL, a default function prints an error message on
93 function performs a consistency check on
94 the block of allocated memory pointed to by
98 function should be called beforehand (otherwise
101 .BR MCHECK_DISABLED ).
103 The following list describes the values returned by
111 .BR MCHECK_DISABLED " (" mprobe "() only)"
113 was not called before the first memory allocation function was called.
114 Consistency checking is not possible.
116 .BR MCHECK_OK " (" mprobe "() only)"
117 No inconsistency detected.
120 Memory preceding an allocated block was clobbered.
123 Memory following an allocated block was clobbered.
127 A block of memory was freed twice.
131 .BR mcheck_pedantic ()
132 return 0 on success, or \-1 on error.
135 .BR mcheck_pedantic ()
137 .BR mcheck_check_all ()
138 functions are available since glibc 2.2.
143 functions are present since at least glibc 2.0
145 These functions are GNU extensions.
147 Linking a program with
151 environment variable (described in
153 cause the same kinds of errors to be detected.
156 does not require the application to be relinked.
157 .\" But is MALLOC_CHECK_ slower?
160 The program below calls
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:
171 About to free a second time
173 Aborted (core dumped)
184 main(int argc, char *argv[])
188 if (mcheck(NULL) != 0) {
189 fprintf(stderr, "mcheck() failed\\n");
196 fprintf(stderr, "About to free\\n");
198 fprintf(stderr, "\\nAbout to free a second time\\n");
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