blob: 535e3cc44cb23eadebd7503c58f0be25e03c6f3e [file] [log] [blame]
sewardjde4a1d02002-03-22 01:27:54 +00001
2/*
njn25e49d8e72002-09-23 09:36:25 +00003 ----------------------------------------------------------------
4
5 Notice that the following BSD-style license applies to this one
6 file (valgrind.h) only. The entire rest of Valgrind is licensed
7 under the terms of the GNU General Public License, version 2. See
8 the COPYING file in the source distribution for details.
9
10 ----------------------------------------------------------------
11
njnc9539842002-10-02 13:26:35 +000012 This file is part of Valgrind, an extensible x86 protected-mode
13 emulator for monitoring program execution on x86-Unixes.
sewardjde4a1d02002-03-22 01:27:54 +000014
njn0e1b5142003-04-15 14:58:06 +000015 Copyright (C) 2000-2003 Julian Seward. All rights reserved.
sewardjde4a1d02002-03-22 01:27:54 +000016
njn25e49d8e72002-09-23 09:36:25 +000017 Redistribution and use in source and binary forms, with or without
18 modification, are permitted provided that the following conditions
19 are met:
sewardjde4a1d02002-03-22 01:27:54 +000020
njn25e49d8e72002-09-23 09:36:25 +000021 1. Redistributions of source code must retain the above copyright
22 notice, this list of conditions and the following disclaimer.
sewardjde4a1d02002-03-22 01:27:54 +000023
njn25e49d8e72002-09-23 09:36:25 +000024 2. The origin of this software must not be misrepresented; you must
25 not claim that you wrote the original software. If you use this
26 software in a product, an acknowledgment in the product
27 documentation would be appreciated but is not required.
sewardjde4a1d02002-03-22 01:27:54 +000028
njn25e49d8e72002-09-23 09:36:25 +000029 3. Altered source versions must be plainly marked as such, and must
30 not be misrepresented as being the original software.
31
32 4. The name of the author may not be used to endorse or promote
33 products derived from this software without specific prior written
34 permission.
35
36 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
37 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
38 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
40 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
42 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
43 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
45 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
46 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47
48 ----------------------------------------------------------------
49
50 Notice that the above BSD-style license applies to this one file
51 (valgrind.h) only. The entire rest of Valgrind is licensed under
52 the terms of the GNU General Public License, version 2. See the
53 COPYING file in the source distribution for details.
54
55 ----------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +000056*/
57
58
59#ifndef __VALGRIND_H
60#define __VALGRIND_H
61
fitzhardinge39de4b42003-10-31 07:12:21 +000062#include <stdarg.h>
63
sewardjde4a1d02002-03-22 01:27:54 +000064
65/* This file is for inclusion into client (your!) code.
66
njn25e49d8e72002-09-23 09:36:25 +000067 You can use these macros to manipulate and query Valgrind's
68 execution inside your own programs.
sewardjde4a1d02002-03-22 01:27:54 +000069
70 The resulting executables will still run without Valgrind, just a
71 little bit more slowly than they otherwise would, but otherwise
sewardj285f77f2003-03-15 23:39:11 +000072 unchanged. When not running on valgrind, each client request
73 consumes about 9 x86 instructions, so the resulting performance
74 loss is negligible unless you plan to execute client requests
75 millions of times per second. Nevertheless, if that is still a
76 problem, you can compile with the NVALGRIND symbol defined (gcc
77 -DNVALGRIND) so that client requests are not even compiled in. */
sewardjde4a1d02002-03-22 01:27:54 +000078
79
80
sewardj37091fb2002-11-16 11:06:50 +000081#ifndef NVALGRIND
sewardjde4a1d02002-03-22 01:27:54 +000082/* This defines the magic code sequence which the JITter spots and
83 handles magically. Don't look too closely at this; it will rot
sewardj2e93c502002-04-12 11:12:52 +000084 your brain. Valgrind dumps the result value in %EDX, so we first
85 copy the default value there, so that it is returned when not
86 running on Valgrind. Since %EAX points to a block of mem
87 containing the args, you can pass as many args as you want like
88 this. Currently this is set up to deal with 4 args since that's
89 the max that we appear to need (pthread_create).
sewardjde4a1d02002-03-22 01:27:54 +000090*/
sewardj2e93c502002-04-12 11:12:52 +000091#define VALGRIND_MAGIC_SEQUENCE( \
92 _zzq_rlval, /* result lvalue */ \
93 _zzq_default, /* result returned when running on real CPU */ \
94 _zzq_request, /* request code */ \
95 _zzq_arg1, /* request first param */ \
96 _zzq_arg2, /* request second param */ \
97 _zzq_arg3, /* request third param */ \
98 _zzq_arg4 /* request fourth param */ ) \
99 \
100 { volatile unsigned int _zzq_args[5]; \
sewardj18d75132002-05-16 11:06:21 +0000101 _zzq_args[0] = (volatile unsigned int)(_zzq_request); \
102 _zzq_args[1] = (volatile unsigned int)(_zzq_arg1); \
103 _zzq_args[2] = (volatile unsigned int)(_zzq_arg2); \
104 _zzq_args[3] = (volatile unsigned int)(_zzq_arg3); \
105 _zzq_args[4] = (volatile unsigned int)(_zzq_arg4); \
sewardj2e93c502002-04-12 11:12:52 +0000106 asm volatile("movl %1, %%eax\n\t" \
107 "movl %2, %%edx\n\t" \
108 "roll $29, %%eax ; roll $3, %%eax\n\t" \
109 "rorl $27, %%eax ; rorl $5, %%eax\n\t" \
110 "roll $13, %%eax ; roll $19, %%eax\n\t" \
111 "movl %%edx, %0\t" \
112 : "=r" (_zzq_rlval) \
113 : "r" (&_zzq_args[0]), "r" (_zzq_default) \
114 : "eax", "edx", "cc", "memory" \
115 ); \
116 }
sewardj37091fb2002-11-16 11:06:50 +0000117#else /* NVALGRIND */
118/* Define NVALGRIND to completely remove the Valgrind magic sequence
119 from the compiled code (analogous to NDEBUG's effects on
120 assert()) */
121#define VALGRIND_MAGIC_SEQUENCE( \
122 _zzq_rlval, /* result lvalue */ \
123 _zzq_default, /* result returned when running on real CPU */ \
124 _zzq_request, /* request code */ \
125 _zzq_arg1, /* request first param */ \
126 _zzq_arg2, /* request second param */ \
127 _zzq_arg3, /* request third param */ \
128 _zzq_arg4 /* request fourth param */ ) \
129 { \
130 (_zzq_rlval) = (_zzq_default); \
131 }
132#endif /* NVALGRIND */
sewardj2e93c502002-04-12 11:12:52 +0000133
134/* Some request codes. There are many more of these, but most are not
135 exposed to end-user view. These are the public ones, all of the
njn25e49d8e72002-09-23 09:36:25 +0000136 form 0x1000 + small_number.
njnd7994182003-10-02 13:44:04 +0000137
138 Core ones are in the range 0x00000000--0x0000ffff. The non-public ones
139 start at 0x2000.
sewardj2e93c502002-04-12 11:12:52 +0000140*/
141
njn4c791212003-05-02 17:53:54 +0000142#define VG_USERREQ_SKIN_BASE(a,b) \
143 ((unsigned int)(((a)&0xff) << 24 | ((b)&0xff) << 16))
144#define VG_IS_SKIN_USERREQ(a, b, v) \
145 (VG_USERREQ_SKIN_BASE(a,b) == ((v) & 0xffff0000))
sewardj34042512002-10-22 04:14:35 +0000146
njn25e49d8e72002-09-23 09:36:25 +0000147typedef
njn4c791212003-05-02 17:53:54 +0000148 enum { VG_USERREQ__RUNNING_ON_VALGRIND = 0x1001,
149 VG_USERREQ__DISCARD_TRANSLATIONS = 0x1002,
njn3e884182003-04-15 13:03:23 +0000150
151 /* These allow any function of 0--3 args to be called from the
152 simulated CPU but run on the real CPU */
njn4c791212003-05-02 17:53:54 +0000153 VG_USERREQ__CLIENT_CALL0 = 0x1101,
154 VG_USERREQ__CLIENT_CALL1 = 0x1102,
155 VG_USERREQ__CLIENT_CALL2 = 0x1103,
156 VG_USERREQ__CLIENT_CALL3 = 0x1104,
njn3e884182003-04-15 13:03:23 +0000157
njn47363ab2003-04-21 13:24:40 +0000158 /* Can be useful in regression testing suites -- eg. can send
159 Valgrind's output to /dev/null and still count errors. */
njn4c791212003-05-02 17:53:54 +0000160 VG_USERREQ__COUNT_ERRORS = 0x1201,
njn47363ab2003-04-21 13:24:40 +0000161
njnd7994182003-10-02 13:44:04 +0000162 /* These are useful and can be interpreted by any skin that tracks
163 malloc() et al, by using vg_replace_malloc.c. */
164 VG_USERREQ__MALLOCLIKE_BLOCK = 0x1301,
165 VG_USERREQ__FREELIKE_BLOCK = 0x1302,
166
fitzhardinge39de4b42003-10-31 07:12:21 +0000167 /* Allow printfs to valgrind log. */
168 VG_USERREQ__PRINTF = 0x1401,
169 VG_USERREQ__PRINTF_BACKTRACE = 0x1402,
njn25e49d8e72002-09-23 09:36:25 +0000170 } Vg_ClientRequest;
sewardj2e93c502002-04-12 11:12:52 +0000171
muellerc9b36552003-12-31 14:32:23 +0000172#ifndef __GNUC__
173#define __extension__
174#endif
sewardj2e93c502002-04-12 11:12:52 +0000175
176/* Returns 1 if running on Valgrind, 0 if running on the real CPU.
177 Currently implemented but untested. */
muellerc9b36552003-12-31 14:32:23 +0000178#define RUNNING_ON_VALGRIND __extension__ \
sewardj2e93c502002-04-12 11:12:52 +0000179 ({unsigned int _qzz_res; \
180 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0 /* returned if not */, \
181 VG_USERREQ__RUNNING_ON_VALGRIND, \
182 0, 0, 0, 0); \
183 _qzz_res; \
sewardjde4a1d02002-03-22 01:27:54 +0000184 })
185
186
sewardj18d75132002-05-16 11:06:21 +0000187/* Discard translation of code in the range [_qzz_addr .. _qzz_addr +
188 _qzz_len - 1]. Useful if you are debugging a JITter or some such,
189 since it provides a way to make sure valgrind will retranslate the
190 invalidated area. Returns no value. */
191#define VALGRIND_DISCARD_TRANSLATIONS(_qzz_addr,_qzz_len) \
192 {unsigned int _qzz_res; \
193 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
194 VG_USERREQ__DISCARD_TRANSLATIONS, \
195 _qzz_addr, _qzz_len, 0, 0); \
196 }
197
fitzhardinge39de4b42003-10-31 07:12:21 +0000198#ifndef NVALGRIND
199
fitzhardingea09a1b52003-11-07 23:09:48 +0000200int VALGRIND_PRINTF(const char *format, ...)
201 __attribute__((format(__printf__, 1, 2)));
fitzhardinge39de4b42003-10-31 07:12:21 +0000202__attribute__((weak))
203int
fitzhardingea09a1b52003-11-07 23:09:48 +0000204VALGRIND_PRINTF(const char *format, ...)
fitzhardinge39de4b42003-10-31 07:12:21 +0000205{
206 unsigned int _qzz_res;
207 va_list vargs;
208 va_start(vargs, format);
209 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, VG_USERREQ__PRINTF,
210 (unsigned int)format, (unsigned int)vargs, 0, 0);
211 va_end(vargs);
212 return _qzz_res;
213}
214
fitzhardingea09a1b52003-11-07 23:09:48 +0000215int VALGRIND_PRINTF_BACKTRACE(const char *format, ...)
216 __attribute__((format(__printf__, 1, 2)));
fitzhardinge39de4b42003-10-31 07:12:21 +0000217__attribute__((weak))
218int
fitzhardingea09a1b52003-11-07 23:09:48 +0000219VALGRIND_PRINTF_BACKTRACE(const char *format, ...)
fitzhardinge39de4b42003-10-31 07:12:21 +0000220{
221 unsigned int _qzz_res;
222 va_list vargs;
223 va_start(vargs, format);
224 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, VG_USERREQ__PRINTF_BACKTRACE,
225 (unsigned int)format, (unsigned int)vargs, 0, 0);
226 va_end(vargs);
227 return _qzz_res;
228}
229
230#else /* NVALGRIND */
231
232#define VALGRIND_PRINTF(...)
233#define VALGRIND_PRINTF_BACKTRACE(...)
234
235#endif /* NVALGRIND */
sewardj18d75132002-05-16 11:06:21 +0000236
njn3e884182003-04-15 13:03:23 +0000237/* These requests allow control to move from the simulated CPU to the
238 real CPU, calling an arbitary function */
njn057c65f2003-04-21 13:30:55 +0000239#define VALGRIND_NON_SIMD_CALL0(_qyy_fn) \
njn3e884182003-04-15 13:03:23 +0000240 ({unsigned int _qyy_res; \
241 VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
242 VG_USERREQ__CLIENT_CALL0, \
243 _qyy_fn, \
244 0, 0, 0); \
245 _qyy_res; \
246 })
247
njn057c65f2003-04-21 13:30:55 +0000248#define VALGRIND_NON_SIMD_CALL1(_qyy_fn, _qyy_arg1) \
njn3e884182003-04-15 13:03:23 +0000249 ({unsigned int _qyy_res; \
250 VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
251 VG_USERREQ__CLIENT_CALL1, \
252 _qyy_fn, \
253 _qyy_arg1, 0, 0); \
254 _qyy_res; \
255 })
256
njn057c65f2003-04-21 13:30:55 +0000257#define VALGRIND_NON_SIMD_CALL2(_qyy_fn, _qyy_arg1, _qyy_arg2) \
njn3e884182003-04-15 13:03:23 +0000258 ({unsigned int _qyy_res; \
259 VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
260 VG_USERREQ__CLIENT_CALL2, \
261 _qyy_fn, \
262 _qyy_arg1, _qyy_arg2, 0); \
263 _qyy_res; \
264 })
265
njn057c65f2003-04-21 13:30:55 +0000266#define VALGRIND_NON_SIMD_CALL3(_qyy_fn, _qyy_arg1, _qyy_arg2, _qyy_arg3) \
njn3e884182003-04-15 13:03:23 +0000267 ({unsigned int _qyy_res; \
268 VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
269 VG_USERREQ__CLIENT_CALL3, \
270 _qyy_fn, \
271 _qyy_arg1, _qyy_arg2, _qyy_arg3); \
272 _qyy_res; \
273 })
274
275
njn47363ab2003-04-21 13:24:40 +0000276/* Counts the number of errors that have been recorded by a skin. Nb:
277 the skin must record the errors with VG_(maybe_record_error)() or
278 VG_(unique_error)() for them to be counted. */
279#define VALGRIND_COUNT_ERRORS \
280 ({unsigned int _qyy_res; \
281 VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
282 VG_USERREQ__COUNT_ERRORS, \
283 0, 0, 0, 0); \
284 _qyy_res; \
285 })
286
njnd7994182003-10-02 13:44:04 +0000287/* Mark a block of memory as having been allocated by a malloc()-like
288 function. `addr' is the start of the usable block (ie. after any
289 redzone) `rzB' is redzone size if the allocator can apply redzones;
290 use '0' if not. Adding redzones makes it more likely Valgrind will spot
291 block overruns. `is_zeroed' indicates if the memory is zeroed, as it is
292 for calloc(). Put it immediately after the point where a block is
293 allocated.
294
295 If you're allocating memory via superblocks, and then handing out small
296 chunks of each superblock, if you don't have redzones on your small
297 blocks, it's worth marking the superblock with VALGRIND_MAKE_NOACCESS
298 when it's created, so that block overruns are detected. But if you can
299 put redzones on, it's probably better to not do this, so that messages
300 for small overruns are described in terms of the small block rather than
301 the superblock (but if you have a big overrun that skips over a redzone,
302 you could miss an error this way). See memcheck/tests/custom_alloc.c
303 for an example.
304
305 Nb: block must be freed via a free()-like function specified
306 with VALGRIND_FREELIKE_BLOCK or mismatch errors will occur. */
307#define VALGRIND_MALLOCLIKE_BLOCK(addr, sizeB, rzB, is_zeroed) \
308 {unsigned int _qzz_res; \
309 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
310 VG_USERREQ__MALLOCLIKE_BLOCK, \
311 addr, sizeB, rzB, is_zeroed); \
312 }
313
314/* Mark a block of memory as having been freed by a free()-like function.
315 `rzB' is redzone size; it must match that given to
316 VALGRIND_MALLOCLIKE_BLOCK. Memory not freed will be detected by the leak
317 checker. Put it immediately after the point where the block is freed. */
318#define VALGRIND_FREELIKE_BLOCK(addr, rzB) \
319 {unsigned int _qzz_res; \
320 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
321 VG_USERREQ__FREELIKE_BLOCK, \
322 addr, rzB, 0, 0); \
323 }
324
njn3e884182003-04-15 13:03:23 +0000325#endif /* __VALGRIND_H */