]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/mcheck.3
getent.1, iconv.1, ldd.1, locale.1, localedef.1, memusage.1, memusagestat.1, pldd...
[thirdparty/man-pages.git] / man3 / mcheck.3
CommitLineData
fd298193
MK
1.\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
2.\"
93015253 3.\" %%%LICENSE_START(VERBATIM)
fd298193
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
fd298193 24.\"
9ba01802 25.TH MCHECK 3 2019-03-06 "GNU" "Linux Programmer's Manual"
fd298193 26.SH NAME
90d4c3de 27mcheck, mcheck_check_all, mcheck_pedantic, mprobe \- heap consistency checking
fd298193
MK
28.SH SYNOPSIS
29.nf
30.B #include <mcheck.h>
68e4db0a 31.PP
fd298193 32.BI "int mcheck(void (*" abortfunc ")(enum mcheck_status " mstatus ));
f90f031e 33.PP
fd298193 34.BI "int mcheck_pedantic(void (*" abortfunc ")(enum mcheck_status " mstatus ));
f90f031e 35.PP
fd298193 36.B void mcheck_check_all(void);
f90f031e 37.PP
fd298193 38.BI "enum mcheck_status mprobe(void *" ptr );
fd298193
MK
39.fi
40.SH DESCRIPTION
41The
42.BR mcheck ()
43function installs a set of debugging hooks for the
44.BR malloc (3)
45family of memory-allocation functions.
46These hooks cause certain consistency checks to be performed
47on the state of the heap.
48The checks can detect application errors such as freeing a block of memory
49more than once or corrupting the bookkeeping data structures
50that immediately precede a block of allocated memory.
847e0d88 51.PP
fd298193
MK
52To be effective, the
53.BR mcheck ()
54function must be called before the first call to
55.BR malloc (3)
56or a related function.
57In cases where this is difficult to ensure, linking the program with
8f7f19ad 58.IR \-lmcheck
fd298193
MK
59inserts an implicit call to
60.BR mcheck ()
61(with a NULL argument)
62before the first call to a memory-allocation function.
847e0d88 63.PP
fd298193
MK
64The
65.BR mcheck_pedantic ()
66function is similar to
67.BR mcheck (),
68but performs checks on all allocated blocks whenever
69one of the memory-allocation functions is called.
70This can be very slow!
847e0d88 71.PP
fd298193
MK
72The
73.BR mcheck_check_all ()
74function causes an immediate check on all allocated blocks.
33a0ccb2 75This call is effective only if
fd298193
MK
76.BR mcheck ()
77is called beforehand.
847e0d88 78.PP
fd298193
MK
79If the system detects an inconsistency in the heap,
80the caller-supplied function pointed to by
81.I abortfunc
99bf4345 82is invoked with a single argument,
fd298193
MK
83.IR mstatus ,
84that indicates what type of inconsistency was detected.
85If
86.I abortfunc
87is NULL, a default function prints an error message on
88.IR stderr
89and calls
90.BR abort (3).
847e0d88 91.PP
fd298193
MK
92The
93.BR mprobe ()
94function performs a consistency check on
95the block of allocated memory pointed to by
96.IR ptr .
97The
98.BR mcheck ()
99function should be called beforehand (otherwise
100.BR mprobe ()
101returns
102.BR MCHECK_DISABLED ).
847e0d88 103.PP
fd298193
MK
104The following list describes the values returned by
105.BR mprobe ()
106or passed as the
107.I mstatus
3960d7a2 108argument when
fd298193
MK
109.I abortfunc
110is invoked:
111.TP
112.BR MCHECK_DISABLED " (" mprobe "() only)"
113.BR mcheck ()
114was not called before the first memory allocation function was called.
115Consistency checking is not possible.
116.TP
117.BR MCHECK_OK " (" mprobe "() only)"
118No inconsistency detected.
119.TP
120.B MCHECK_HEAD
121Memory preceding an allocated block was clobbered.
122.TP
123.B MCHECK_TAIL
124Memory following an allocated block was clobbered.
125.TP
126.B
127MCHECK_FREE
128A block of memory was freed twice.
129.SH RETURN VALUE
130.BR mcheck ()
131and
132.BR mcheck_pedantic ()
133return 0 on success, or \-1 on error.
134.SH VERSIONS
135The
136.BR mcheck_pedantic ()
137and
138.BR mcheck_check_all ()
139functions are available since glibc 2.2.
140The
6fdbc779 141.BR mcheck ()
fd298193 142and
6fdbc779 143.BR mprobe ()
fd298193 144functions are present since at least glibc 2.0
834fde73
ZL
145.SH ATTRIBUTES
146For an explanation of the terms used in this section, see
147.BR attributes (7).
148.TS
149allbox;
150lbw28 lb lbw21
151l l l.
152Interface Attribute Value
153T{
154.BR mcheck (),
155.BR mcheck_pedantic (),
156.br
157.BR mcheck_check_all (),
158.BR mprobe ()
159T} Thread safety T{
160MT-Unsafe race:mcheck
161.br
162const:malloc_hooks
163T}
164.TE
847e0d88 165.sp 1
fd298193
MK
166.SH CONFORMING TO
167These functions are GNU extensions.
168.SH NOTES
169Linking a program with
170.I \-lmcheck
171and using the
172.B MALLOC_CHECK_
173environment variable (described in
174.BR mallopt (3))
175cause the same kinds of errors to be detected.
176But, using
177.B MALLOC_CHECK_
178does not require the application to be relinked.
179.\" But is MALLOC_CHECK_ slower?
fd298193
MK
180.SH EXAMPLE
181The program below calls
182.BR mcheck ()
183with a NULL argument and then frees the same block of memory twice.
184The following shell session demonstrates what happens
185when running the program:
e646a1ba 186.PP
fd298193 187.in +4n
e646a1ba 188.EX
fd298193
MK
189.RB "$" " ./a.out"
190About to free
191
192About to free a second time
193block freed twice
194Aborted (core dumped)
b8302363 195.EE
fd298193
MK
196.in
197.SS Program source
198\&
e7d0bb47 199.EX
fd298193
MK
200#include <stdlib.h>
201#include <stdio.h>
202#include <mcheck.h>
203
204int
205main(int argc, char *argv[])
206{
207 char *p;
208
209 if (mcheck(NULL) != 0) {
d1a71985 210 fprintf(stderr, "mcheck() failed\en");
fd298193
MK
211
212 exit(EXIT_FAILURE);
213 }
214
215 p = malloc(1000);
216
d1a71985 217 fprintf(stderr, "About to free\en");
fd298193 218 free(p);
d1a71985 219 fprintf(stderr, "\enAbout to free a second time\en");
fd298193
MK
220 free(p);
221
222 exit(EXIT_SUCCESS);
223}
e7d0bb47 224.EE
fd298193
MK
225.SH SEE ALSO
226.BR malloc (3),
fd298193
MK
227.BR mallopt (3),
228.BR mtrace (3)