]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/mcheck.3
dlinfo.3: ATTRIBUTES: Note function that is thread-safe
[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.\"
67d2c687 25.TH MCHECK 3 2015-05-07 "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>
31.sp
32.BI "int mcheck(void (*" abortfunc ")(enum mcheck_status " mstatus ));
33
34.BI "int mcheck_pedantic(void (*" abortfunc ")(enum mcheck_status " mstatus ));
3960d7a2 35
fd298193
MK
36.B void mcheck_check_all(void);
37
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.
51
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.
63
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!
71
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.
78
79If the system detects an inconsistency in the heap,
80the caller-supplied function pointed to by
81.I abortfunc
82is invoked with a single argument argument,
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).
91
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 ).
103
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
165
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:
186.in +4n
187.nf
188
189.RB "$" " ./a.out"
190About to free
191
192About to free a second time
193block freed twice
194Aborted (core dumped)
195.fi
196.in
197.SS Program source
198\&
199.nf
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) {
210 fprintf(stderr, "mcheck() failed\\n");
211
212 exit(EXIT_FAILURE);
213 }
214
215 p = malloc(1000);
216
217 fprintf(stderr, "About to free\\n");
218 free(p);
219 fprintf(stderr, "\\nAbout to free a second time\\n");
220 free(p);
221
222 exit(EXIT_SUCCESS);
223}
224.fi
225.SH SEE ALSO
226.BR malloc (3),
fd298193
MK
227.BR mallopt (3),
228.BR mtrace (3)