blob: 7f3c183b5602abec7f6c7e670189f6dad5e21c29 [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
nethercotebb1c9912004-01-04 16:43:23 +000015 Copyright (C) 2000-2004 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
nethercoteb2decc32004-10-25 19:33:26 +000064#undef __@VG_ARCH@__
65#define __@VG_ARCH@__ 1 // Architecture we're installed on
sewardjde4a1d02002-03-22 01:27:54 +000066
67/* This file is for inclusion into client (your!) code.
68
njn25e49d8e72002-09-23 09:36:25 +000069 You can use these macros to manipulate and query Valgrind's
70 execution inside your own programs.
sewardjde4a1d02002-03-22 01:27:54 +000071
72 The resulting executables will still run without Valgrind, just a
73 little bit more slowly than they otherwise would, but otherwise
sewardj285f77f2003-03-15 23:39:11 +000074 unchanged. When not running on valgrind, each client request
nethercotee90c6832004-10-18 18:07:49 +000075 consumes very few (eg. < 10) instructions, so the resulting performance
sewardj285f77f2003-03-15 23:39:11 +000076 loss is negligible unless you plan to execute client requests
77 millions of times per second. Nevertheless, if that is still a
78 problem, you can compile with the NVALGRIND symbol defined (gcc
79 -DNVALGRIND) so that client requests are not even compiled in. */
sewardjde4a1d02002-03-22 01:27:54 +000080
sewardj37091fb2002-11-16 11:06:50 +000081#ifndef NVALGRIND
nethercotee90c6832004-10-18 18:07:49 +000082
nethercote54265442004-10-26 12:56:58 +000083/* The following defines the magic code sequences which the JITter spots and
84 handles magically. Don't look too closely at them; they will rot
nethercotee90c6832004-10-18 18:07:49 +000085 your brain. We must ensure that the default value gets put in the return
86 slot, so that everything works when this is executed not under Valgrind.
87 Args are passed in a memory block, and so there's no intrinsic limit to
88 the number that could be passed, but it's currently four.
89
nethercote54265442004-10-26 12:56:58 +000090 The macro args are:
91 _zzq_rlval result lvalue
92 _zzq_default default value (result returned when running on real CPU)
93 _zzq_request request code
94 _zzq_arg1..4 request params
95
nethercotee90c6832004-10-18 18:07:49 +000096 Nb: we put the assembly code sequences for all architectures in this one
97 file. This is because this file must be stand-alone, so we can't rely on
98 eg. x86/ subdirectories like we do within the rest of Valgrind.
99*/
100
101#ifdef __x86__
sewardj2e93c502002-04-12 11:12:52 +0000102#define VALGRIND_MAGIC_SEQUENCE( \
nethercote54265442004-10-26 12:56:58 +0000103 _zzq_rlval, _zzq_default, _zzq_request, \
104 _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4) \
sewardj2e93c502002-04-12 11:12:52 +0000105 \
106 { volatile unsigned int _zzq_args[5]; \
sewardj18d75132002-05-16 11:06:21 +0000107 _zzq_args[0] = (volatile unsigned int)(_zzq_request); \
108 _zzq_args[1] = (volatile unsigned int)(_zzq_arg1); \
109 _zzq_args[2] = (volatile unsigned int)(_zzq_arg2); \
110 _zzq_args[3] = (volatile unsigned int)(_zzq_arg3); \
111 _zzq_args[4] = (volatile unsigned int)(_zzq_arg4); \
sewardj2e93c502002-04-12 11:12:52 +0000112 asm volatile("movl %1, %%eax\n\t" \
113 "movl %2, %%edx\n\t" \
114 "roll $29, %%eax ; roll $3, %%eax\n\t" \
115 "rorl $27, %%eax ; rorl $5, %%eax\n\t" \
116 "roll $13, %%eax ; roll $19, %%eax\n\t" \
117 "movl %%edx, %0\t" \
118 : "=r" (_zzq_rlval) \
119 : "r" (&_zzq_args[0]), "r" (_zzq_default) \
120 : "eax", "edx", "cc", "memory" \
121 ); \
122 }
nethercotee90c6832004-10-18 18:07:49 +0000123#endif // __x86__
124
125// Insert assembly code for other architectures here...
126
sewardj37091fb2002-11-16 11:06:50 +0000127#else /* NVALGRIND */
128/* Define NVALGRIND to completely remove the Valgrind magic sequence
129 from the compiled code (analogous to NDEBUG's effects on
130 assert()) */
131#define VALGRIND_MAGIC_SEQUENCE( \
nethercote69d9c462004-10-26 13:00:12 +0000132 _zzq_rlval, _zzq_default, _zzq_request, \
133 _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4) \
sewardj37091fb2002-11-16 11:06:50 +0000134 { \
135 (_zzq_rlval) = (_zzq_default); \
136 }
137#endif /* NVALGRIND */
sewardj2e93c502002-04-12 11:12:52 +0000138
nethercote69d9c462004-10-26 13:00:12 +0000139
sewardj2e93c502002-04-12 11:12:52 +0000140/* Some request codes. There are many more of these, but most are not
141 exposed to end-user view. These are the public ones, all of the
njn25e49d8e72002-09-23 09:36:25 +0000142 form 0x1000 + small_number.
njnd7994182003-10-02 13:44:04 +0000143
144 Core ones are in the range 0x00000000--0x0000ffff. The non-public ones
145 start at 0x2000.
sewardj2e93c502002-04-12 11:12:52 +0000146*/
147
njnfc26ff92004-11-22 19:12:49 +0000148// These macros are used by tools -- they must be public, but don't embed them
149// into other programs.
150#define VG_USERREQ_TOOL_BASE(a,b) \
njn4c791212003-05-02 17:53:54 +0000151 ((unsigned int)(((a)&0xff) << 24 | ((b)&0xff) << 16))
njnfc26ff92004-11-22 19:12:49 +0000152#define VG_IS_TOOL_USERREQ(a, b, v) \
153 (VG_USERREQ_TOOL_BASE(a,b) == ((v) & 0xffff0000))
sewardj34042512002-10-22 04:14:35 +0000154
njn25e49d8e72002-09-23 09:36:25 +0000155typedef
njn4c791212003-05-02 17:53:54 +0000156 enum { VG_USERREQ__RUNNING_ON_VALGRIND = 0x1001,
157 VG_USERREQ__DISCARD_TRANSLATIONS = 0x1002,
njn3e884182003-04-15 13:03:23 +0000158
njnd4795be2004-11-24 11:57:51 +0000159 /* These allow any function to be called from the
160 simulated CPU but run on the real CPU.
161 Nb: the first arg passed to the function is always the ThreadId of
162 the running thread! So CLIENT_CALL0 actually requires a 1 arg
163 function, etc. */
njn4c791212003-05-02 17:53:54 +0000164 VG_USERREQ__CLIENT_CALL0 = 0x1101,
165 VG_USERREQ__CLIENT_CALL1 = 0x1102,
166 VG_USERREQ__CLIENT_CALL2 = 0x1103,
167 VG_USERREQ__CLIENT_CALL3 = 0x1104,
njn3e884182003-04-15 13:03:23 +0000168
njn47363ab2003-04-21 13:24:40 +0000169 /* Can be useful in regression testing suites -- eg. can send
170 Valgrind's output to /dev/null and still count errors. */
njn4c791212003-05-02 17:53:54 +0000171 VG_USERREQ__COUNT_ERRORS = 0x1201,
njn47363ab2003-04-21 13:24:40 +0000172
nethercote7cc9c232004-01-21 15:08:04 +0000173 /* These are useful and can be interpreted by any tool that tracks
njnd7994182003-10-02 13:44:04 +0000174 malloc() et al, by using vg_replace_malloc.c. */
175 VG_USERREQ__MALLOCLIKE_BLOCK = 0x1301,
176 VG_USERREQ__FREELIKE_BLOCK = 0x1302,
rjwalshbc0bb832004-06-19 18:12:36 +0000177 /* Memory pool support. */
178 VG_USERREQ__CREATE_MEMPOOL = 0x1303,
179 VG_USERREQ__DESTROY_MEMPOOL = 0x1304,
180 VG_USERREQ__MEMPOOL_ALLOC = 0x1305,
181 VG_USERREQ__MEMPOOL_FREE = 0x1306,
njnd7994182003-10-02 13:44:04 +0000182
fitzhardinge39de4b42003-10-31 07:12:21 +0000183 /* Allow printfs to valgrind log. */
184 VG_USERREQ__PRINTF = 0x1401,
thughes85c8a502004-08-25 13:25:30 +0000185 VG_USERREQ__PRINTF_BACKTRACE = 0x1402
njn25e49d8e72002-09-23 09:36:25 +0000186 } Vg_ClientRequest;
sewardj2e93c502002-04-12 11:12:52 +0000187
muellerc9b36552003-12-31 14:32:23 +0000188#ifndef __GNUC__
189#define __extension__
190#endif
sewardj2e93c502002-04-12 11:12:52 +0000191
192/* Returns 1 if running on Valgrind, 0 if running on the real CPU.
193 Currently implemented but untested. */
muellerc9b36552003-12-31 14:32:23 +0000194#define RUNNING_ON_VALGRIND __extension__ \
sewardj2e93c502002-04-12 11:12:52 +0000195 ({unsigned int _qzz_res; \
196 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0 /* returned if not */, \
197 VG_USERREQ__RUNNING_ON_VALGRIND, \
198 0, 0, 0, 0); \
199 _qzz_res; \
sewardjde4a1d02002-03-22 01:27:54 +0000200 })
201
202
sewardj18d75132002-05-16 11:06:21 +0000203/* Discard translation of code in the range [_qzz_addr .. _qzz_addr +
204 _qzz_len - 1]. Useful if you are debugging a JITter or some such,
205 since it provides a way to make sure valgrind will retranslate the
206 invalidated area. Returns no value. */
207#define VALGRIND_DISCARD_TRANSLATIONS(_qzz_addr,_qzz_len) \
208 {unsigned int _qzz_res; \
209 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
210 VG_USERREQ__DISCARD_TRANSLATIONS, \
211 _qzz_addr, _qzz_len, 0, 0); \
212 }
213
fitzhardinge39de4b42003-10-31 07:12:21 +0000214#ifndef NVALGRIND
215
fitzhardingea09a1b52003-11-07 23:09:48 +0000216int VALGRIND_PRINTF(const char *format, ...)
217 __attribute__((format(__printf__, 1, 2)));
fitzhardinge39de4b42003-10-31 07:12:21 +0000218__attribute__((weak))
219int
fitzhardingea09a1b52003-11-07 23:09:48 +0000220VALGRIND_PRINTF(const char *format, ...)
fitzhardinge39de4b42003-10-31 07:12:21 +0000221{
222 unsigned int _qzz_res;
223 va_list vargs;
224 va_start(vargs, format);
225 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, VG_USERREQ__PRINTF,
226 (unsigned int)format, (unsigned int)vargs, 0, 0);
227 va_end(vargs);
228 return _qzz_res;
229}
230
fitzhardingea09a1b52003-11-07 23:09:48 +0000231int VALGRIND_PRINTF_BACKTRACE(const char *format, ...)
232 __attribute__((format(__printf__, 1, 2)));
fitzhardinge39de4b42003-10-31 07:12:21 +0000233__attribute__((weak))
234int
fitzhardingea09a1b52003-11-07 23:09:48 +0000235VALGRIND_PRINTF_BACKTRACE(const char *format, ...)
fitzhardinge39de4b42003-10-31 07:12:21 +0000236{
237 unsigned int _qzz_res;
238 va_list vargs;
239 va_start(vargs, format);
240 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, VG_USERREQ__PRINTF_BACKTRACE,
241 (unsigned int)format, (unsigned int)vargs, 0, 0);
242 va_end(vargs);
243 return _qzz_res;
244}
245
246#else /* NVALGRIND */
247
248#define VALGRIND_PRINTF(...)
249#define VALGRIND_PRINTF_BACKTRACE(...)
250
251#endif /* NVALGRIND */
sewardj18d75132002-05-16 11:06:21 +0000252
njn3e884182003-04-15 13:03:23 +0000253/* These requests allow control to move from the simulated CPU to the
254 real CPU, calling an arbitary function */
njn057c65f2003-04-21 13:30:55 +0000255#define VALGRIND_NON_SIMD_CALL0(_qyy_fn) \
njn3e884182003-04-15 13:03:23 +0000256 ({unsigned int _qyy_res; \
257 VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
258 VG_USERREQ__CLIENT_CALL0, \
259 _qyy_fn, \
260 0, 0, 0); \
261 _qyy_res; \
262 })
263
njn057c65f2003-04-21 13:30:55 +0000264#define VALGRIND_NON_SIMD_CALL1(_qyy_fn, _qyy_arg1) \
njn3e884182003-04-15 13:03:23 +0000265 ({unsigned int _qyy_res; \
266 VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
267 VG_USERREQ__CLIENT_CALL1, \
268 _qyy_fn, \
269 _qyy_arg1, 0, 0); \
270 _qyy_res; \
271 })
272
njn057c65f2003-04-21 13:30:55 +0000273#define VALGRIND_NON_SIMD_CALL2(_qyy_fn, _qyy_arg1, _qyy_arg2) \
njn3e884182003-04-15 13:03:23 +0000274 ({unsigned int _qyy_res; \
275 VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
276 VG_USERREQ__CLIENT_CALL2, \
277 _qyy_fn, \
278 _qyy_arg1, _qyy_arg2, 0); \
279 _qyy_res; \
280 })
281
njn057c65f2003-04-21 13:30:55 +0000282#define VALGRIND_NON_SIMD_CALL3(_qyy_fn, _qyy_arg1, _qyy_arg2, _qyy_arg3) \
njn3e884182003-04-15 13:03:23 +0000283 ({unsigned int _qyy_res; \
284 VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
285 VG_USERREQ__CLIENT_CALL3, \
286 _qyy_fn, \
287 _qyy_arg1, _qyy_arg2, _qyy_arg3); \
288 _qyy_res; \
289 })
290
291
nethercote7cc9c232004-01-21 15:08:04 +0000292/* Counts the number of errors that have been recorded by a tool. Nb:
293 the tool must record the errors with VG_(maybe_record_error)() or
njn47363ab2003-04-21 13:24:40 +0000294 VG_(unique_error)() for them to be counted. */
295#define VALGRIND_COUNT_ERRORS \
296 ({unsigned int _qyy_res; \
297 VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
298 VG_USERREQ__COUNT_ERRORS, \
299 0, 0, 0, 0); \
300 _qyy_res; \
301 })
302
njnd7994182003-10-02 13:44:04 +0000303/* Mark a block of memory as having been allocated by a malloc()-like
304 function. `addr' is the start of the usable block (ie. after any
305 redzone) `rzB' is redzone size if the allocator can apply redzones;
306 use '0' if not. Adding redzones makes it more likely Valgrind will spot
307 block overruns. `is_zeroed' indicates if the memory is zeroed, as it is
308 for calloc(). Put it immediately after the point where a block is
309 allocated.
310
311 If you're allocating memory via superblocks, and then handing out small
312 chunks of each superblock, if you don't have redzones on your small
313 blocks, it's worth marking the superblock with VALGRIND_MAKE_NOACCESS
314 when it's created, so that block overruns are detected. But if you can
315 put redzones on, it's probably better to not do this, so that messages
316 for small overruns are described in terms of the small block rather than
317 the superblock (but if you have a big overrun that skips over a redzone,
318 you could miss an error this way). See memcheck/tests/custom_alloc.c
319 for an example.
320
321 Nb: block must be freed via a free()-like function specified
322 with VALGRIND_FREELIKE_BLOCK or mismatch errors will occur. */
323#define VALGRIND_MALLOCLIKE_BLOCK(addr, sizeB, rzB, is_zeroed) \
324 {unsigned int _qzz_res; \
325 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
326 VG_USERREQ__MALLOCLIKE_BLOCK, \
327 addr, sizeB, rzB, is_zeroed); \
328 }
329
330/* Mark a block of memory as having been freed by a free()-like function.
331 `rzB' is redzone size; it must match that given to
332 VALGRIND_MALLOCLIKE_BLOCK. Memory not freed will be detected by the leak
333 checker. Put it immediately after the point where the block is freed. */
334#define VALGRIND_FREELIKE_BLOCK(addr, rzB) \
335 {unsigned int _qzz_res; \
336 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
337 VG_USERREQ__FREELIKE_BLOCK, \
338 addr, rzB, 0, 0); \
339 }
340
rjwalshbc0bb832004-06-19 18:12:36 +0000341/* Create a memory pool. */
342#define VALGRIND_CREATE_MEMPOOL(pool, rzB, is_zeroed) \
343 {unsigned int _qzz_res; \
344 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
345 VG_USERREQ__CREATE_MEMPOOL, \
346 pool, rzB, is_zeroed, 0); \
347 }
348
349/* Destroy a memory pool. */
350#define VALGRIND_DESTROY_MEMPOOL(pool) \
351 {unsigned int _qzz_res; \
352 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
353 VG_USERREQ__DESTROY_MEMPOOL, \
354 pool, 0, 0, 0); \
355 }
356
357/* Associate a piece of memory with a memory pool. */
358#define VALGRIND_MEMPOOL_ALLOC(pool, addr, size) \
359 {unsigned int _qzz_res; \
360 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
361 VG_USERREQ__MEMPOOL_ALLOC, \
362 pool, addr, size, 0); \
363 }
364
365/* Disassociate a piece of memory from a memory pool. */
366#define VALGRIND_MEMPOOL_FREE(pool, addr) \
367 {unsigned int _qzz_res; \
368 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
369 VG_USERREQ__MEMPOOL_FREE, \
370 pool, addr, 0, 0); \
371 }
372
njn3e884182003-04-15 13:03:23 +0000373#endif /* __VALGRIND_H */