blob: 7a735a967f531d4c45af5ead68085af867f67f28 [file] [log] [blame]
sewardjb5f6f512005-03-10 23:59:00 +00001/* -*- c -*-
njn25e49d8e72002-09-23 09:36:25 +00002 ----------------------------------------------------------------
3
4 Notice that the following BSD-style license applies to this one
5 file (valgrind.h) only. The entire rest of Valgrind is licensed
6 under the terms of the GNU General Public License, version 2. See
7 the COPYING file in the source distribution for details.
8
9 ----------------------------------------------------------------
10
njnb9c427c2004-12-01 14:14:42 +000011 This file is part of Valgrind, a dynamic binary instrumentation
12 framework.
sewardjde4a1d02002-03-22 01:27:54 +000013
njn53612422005-03-12 16:22:54 +000014 Copyright (C) 2000-2005 Julian Seward. All rights reserved.
sewardjde4a1d02002-03-22 01:27:54 +000015
njn25e49d8e72002-09-23 09:36:25 +000016 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions
18 are met:
sewardjde4a1d02002-03-22 01:27:54 +000019
njn25e49d8e72002-09-23 09:36:25 +000020 1. Redistributions of source code must retain the above copyright
21 notice, this list of conditions and the following disclaimer.
sewardjde4a1d02002-03-22 01:27:54 +000022
njn25e49d8e72002-09-23 09:36:25 +000023 2. The origin of this software must not be misrepresented; you must
24 not claim that you wrote the original software. If you use this
25 software in a product, an acknowledgment in the product
26 documentation would be appreciated but is not required.
sewardjde4a1d02002-03-22 01:27:54 +000027
njn25e49d8e72002-09-23 09:36:25 +000028 3. Altered source versions must be plainly marked as such, and must
29 not be misrepresented as being the original software.
30
31 4. The name of the author may not be used to endorse or promote
32 products derived from this software without specific prior written
33 permission.
34
35 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
36 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
37 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
39 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
41 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
42 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
43 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
44 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
45 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46
47 ----------------------------------------------------------------
48
49 Notice that the above BSD-style license applies to this one file
50 (valgrind.h) only. The entire rest of Valgrind is licensed under
51 the terms of the GNU General Public License, version 2. See the
52 COPYING file in the source distribution for details.
53
54 ----------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +000055*/
56
57
njn30d76c62005-06-18 15:07:39 +000058/* This file is for inclusion into client (your!) code.
59
60 You can use these macros to manipulate and query Valgrind's
61 execution inside your own programs.
62
63 The resulting executables will still run without Valgrind, just a
64 little bit more slowly than they otherwise would, but otherwise
65 unchanged. When not running on valgrind, each client request
sewardj0ec07f32006-01-12 12:32:32 +000066 consumes very few (eg. 7) instructions, so the resulting performance
njn30d76c62005-06-18 15:07:39 +000067 loss is negligible unless you plan to execute client requests
68 millions of times per second. Nevertheless, if that is still a
69 problem, you can compile with the NVALGRIND symbol defined (gcc
70 -DNVALGRIND) so that client requests are not even compiled in. */
71
sewardjde4a1d02002-03-22 01:27:54 +000072#ifndef __VALGRIND_H
73#define __VALGRIND_H
74
fitzhardinge39de4b42003-10-31 07:12:21 +000075#include <stdarg.h>
76
njn3dd0a912005-06-28 19:44:10 +000077/* Nb: this file might be included in a file compiled with -ansi. So
78 we can't use C++ style "//" comments nor the "asm" keyword (instead
79 use "__asm__"). */
80
sewardj0ec07f32006-01-12 12:32:32 +000081/* Derive some tags indicating what the target architecture is. Note
82 that in this file we're using the compiler's CPP symbols for
83 identifying architectures, which are different to the ones we use
84 within the rest of Valgrind. Note, __powerpc__ is active for both
85 32 and 64-bit PPC, whereas __powerpc64__ is only active for the
86 latter. */
87#undef ARCH_x86
88#undef ARCH_amd64
89#undef ARCH_ppc32
90#undef ARCH_ppc64
91
92#if defined(__i386__)
93# define ARCH_x86 1
94#elif defined(__x86_64__)
95# define ARCH_amd64 1
96#elif defined(__powerpc__) && !defined(__powerpc64__)
97# define ARCH_ppc32 1
98#elif defined(__powerpc__) && defined(__powerpc64__)
99# define ARCH_ppc64 1
sewardjb5f6f512005-03-10 23:59:00 +0000100#endif
101
sewardj0ec07f32006-01-12 12:32:32 +0000102/* If we're not compiling for our target architecture, don't generate
103 any inline asms. */
104#if !defined(ARCH_x86) && !defined(ARCH_amd64) \
105 && !defined(ARCH_ppc32) && !defined(ARCH_ppc64)
106# if !defined(NVALGRIND)
107# define NVALGRIND 1
108# endif
109#endif
110
111
njn30d76c62005-06-18 15:07:39 +0000112/* ------------------------------------------------------------------ */
sewardj0ec07f32006-01-12 12:32:32 +0000113/* ARCHITECTURE SPECIFICS for SPECIAL INSTRUCTIONS. There is nothing */
114/* in here of use to end-users -- skip to the next section. */
njn30d76c62005-06-18 15:07:39 +0000115/* ------------------------------------------------------------------ */
sewardjde4a1d02002-03-22 01:27:54 +0000116
sewardj0ec07f32006-01-12 12:32:32 +0000117#if defined(NVALGRIND)
njn26aba4d2005-05-16 13:31:23 +0000118
119/* Define NVALGRIND to completely remove the Valgrind magic sequence
sewardj0ec07f32006-01-12 12:32:32 +0000120 from the compiled code (analogous to NDEBUG's effects on
121 assert()) */
122#define VALGRIND_DO_CLIENT_REQUEST( \
123 _zzq_rlval, _zzq_default, _zzq_request, \
sewardj9af10a12006-02-01 14:59:42 +0000124 _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4, _zzq_arg5) \
sewardj0ec07f32006-01-12 12:32:32 +0000125 { \
126 (_zzq_rlval) = (_zzq_default); \
njn26aba4d2005-05-16 13:31:23 +0000127 }
128
sewardj0ec07f32006-01-12 12:32:32 +0000129#else /* ! NVALGRIND */
nethercotee90c6832004-10-18 18:07:49 +0000130
sewardj0ec07f32006-01-12 12:32:32 +0000131/* The following defines the magic code sequences which the JITter
132 spots and handles magically. Don't look too closely at them as
133 they will rot your brain.
134
135 The assembly code sequences for all architectures is in this one
136 file. This is because this file must be stand-alone, and we don't
137 want to have multiple files.
138
139 For VALGRIND_DO_CLIENT_REQUEST, we must ensure that the default
140 value gets put in the return slot, so that everything works when
141 this is executed not under Valgrind. Args are passed in a memory
142 block, and so there's no intrinsic limit to the number that could
sewardj9af10a12006-02-01 14:59:42 +0000143 be passed, but it's currently five.
nethercotee90c6832004-10-18 18:07:49 +0000144
nethercote54265442004-10-26 12:56:58 +0000145 The macro args are:
146 _zzq_rlval result lvalue
147 _zzq_default default value (result returned when running on real CPU)
148 _zzq_request request code
sewardj9af10a12006-02-01 14:59:42 +0000149 _zzq_arg1..5 request params
nethercote54265442004-10-26 12:56:58 +0000150
sewardj0ec07f32006-01-12 12:32:32 +0000151 The other two macros are used to support function wrapping, and are
sewardjd68ac3e2006-01-20 14:31:57 +0000152 a lot simpler. VALGRIND_GET_NR_CONTEXT returns the value of the
153 guest's NRADDR pseudo-register and whatever other information is
154 needed to safely run the call original from the wrapper: on
155 ppc64-linux, the R2 value at the divert point is also needed. This
156 information is abstracted into a user-visible type, OrigFn.
157
158 VALGRIND_CALL_NOREDIR_* behaves the same as the following on the
159 guest, but guarantees that the branch instruction will not be
160 redirected: x86: call *%eax, amd64: call *%rax, ppc32/ppc64:
161 branch-and-link-to-r11. VALGRIND_CALL_NOREDIR is just text, not a
162 complete inline asm, since it needs to be combined with more magic
163 inline asm stuff to be useful.
nethercotee90c6832004-10-18 18:07:49 +0000164*/
165
sewardj0ec07f32006-01-12 12:32:32 +0000166/* ---------------------------- x86 ---------------------------- */
sewardjde4a4ab2005-03-23 13:10:32 +0000167
sewardj0ec07f32006-01-12 12:32:32 +0000168#if defined(ARCH_x86)
sewardjc8858442006-01-20 15:17:20 +0000169
170typedef
171 struct {
172 unsigned int nraddr; /* where's the code? */
173 }
174 OrigFn;
175
sewardj0ec07f32006-01-12 12:32:32 +0000176#define __SPECIAL_INSTRUCTION_PREAMBLE \
177 "roll $3, %%edi ; roll $13, %%edi\n\t" \
sewardj1a85f4f2006-01-12 21:15:35 +0000178 "roll $29, %%edi ; roll $19, %%edi\n\t"
sewardjde4a4ab2005-03-23 13:10:32 +0000179
sewardj0ec07f32006-01-12 12:32:32 +0000180#define VALGRIND_DO_CLIENT_REQUEST( \
181 _zzq_rlval, _zzq_default, _zzq_request, \
sewardj9af10a12006-02-01 14:59:42 +0000182 _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4, _zzq_arg5) \
183 { volatile unsigned int _zzq_args[6]; \
sewardj0ec07f32006-01-12 12:32:32 +0000184 volatile unsigned int _zzq_result; \
185 _zzq_args[0] = (unsigned int)(_zzq_request); \
186 _zzq_args[1] = (unsigned int)(_zzq_arg1); \
187 _zzq_args[2] = (unsigned int)(_zzq_arg2); \
188 _zzq_args[3] = (unsigned int)(_zzq_arg3); \
189 _zzq_args[4] = (unsigned int)(_zzq_arg4); \
sewardj9af10a12006-02-01 14:59:42 +0000190 _zzq_args[5] = (unsigned int)(_zzq_arg5); \
sewardj0ec07f32006-01-12 12:32:32 +0000191 __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE \
192 /* %EDX = client_request ( %EAX ) */ \
193 "xchgl %%ebx,%%ebx" \
194 : "=d" (_zzq_result) \
195 : "a" (&_zzq_args[0]), "0" (_zzq_default) \
196 : "cc", "memory" \
197 ); \
198 _zzq_rlval = _zzq_result; \
cerion85665ca2005-06-20 15:51:07 +0000199 }
sewardj2c48c7b2005-11-29 13:05:56 +0000200
sewardjc8858442006-01-20 15:17:20 +0000201#define VALGRIND_GET_NR_CONTEXT(_zzq_rlval) \
202 { volatile OrigFn* _zzq_orig = &(_zzq_rlval); \
203 volatile unsigned int __addr; \
sewardj0ec07f32006-01-12 12:32:32 +0000204 __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE \
205 /* %EAX = guest_NRADDR */ \
206 "xchgl %%ecx,%%ecx" \
207 : "=a" (__addr) \
208 : \
209 : "cc", "memory" \
210 ); \
sewardjc8858442006-01-20 15:17:20 +0000211 _zzq_orig->nraddr = __addr; \
sewardj2c48c7b2005-11-29 13:05:56 +0000212 }
sewardj0ec07f32006-01-12 12:32:32 +0000213
214#define VALGRIND_CALL_NOREDIR_EAX \
215 __SPECIAL_INSTRUCTION_PREAMBLE \
216 /* call-noredir *%EAX */ \
217 "xchgl %%edx,%%edx\n\t"
218#endif /* ARCH_x86 */
219
220/* --------------------------- amd64 --------------------------- */
221
222#if defined(ARCH_amd64)
sewardjc8858442006-01-20 15:17:20 +0000223
224typedef
225 struct {
226 unsigned long long int nraddr; /* where's the code? */
227 }
228 OrigFn;
229
sewardj0ec07f32006-01-12 12:32:32 +0000230#define __SPECIAL_INSTRUCTION_PREAMBLE \
231 "rolq $3, %%rdi ; rolq $13, %%rdi\n\t" \
sewardj1a85f4f2006-01-12 21:15:35 +0000232 "rolq $61, %%rdi ; rolq $51, %%rdi\n\t"
sewardj0ec07f32006-01-12 12:32:32 +0000233
234#define VALGRIND_DO_CLIENT_REQUEST( \
235 _zzq_rlval, _zzq_default, _zzq_request, \
sewardj9af10a12006-02-01 14:59:42 +0000236 _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4, _zzq_arg5) \
237 { volatile unsigned long long int _zzq_args[6]; \
sewardj0ec07f32006-01-12 12:32:32 +0000238 volatile unsigned long long int _zzq_result; \
239 _zzq_args[0] = (unsigned long long int)(_zzq_request); \
240 _zzq_args[1] = (unsigned long long int)(_zzq_arg1); \
241 _zzq_args[2] = (unsigned long long int)(_zzq_arg2); \
242 _zzq_args[3] = (unsigned long long int)(_zzq_arg3); \
243 _zzq_args[4] = (unsigned long long int)(_zzq_arg4); \
sewardj9af10a12006-02-01 14:59:42 +0000244 _zzq_args[5] = (unsigned long long int)(_zzq_arg5); \
sewardj0ec07f32006-01-12 12:32:32 +0000245 __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE \
246 /* %RDX = client_request ( %RAX ) */ \
247 "xchgq %%rbx,%%rbx" \
248 : "=d" (_zzq_result) \
249 : "a" (&_zzq_args[0]), "0" (_zzq_default) \
250 : "cc", "memory" \
251 ); \
252 _zzq_rlval = _zzq_result; \
253 }
254
sewardjc8858442006-01-20 15:17:20 +0000255#define VALGRIND_GET_NR_CONTEXT(_zzq_rlval) \
256 { volatile OrigFn* _zzq_orig = &(_zzq_rlval); \
257 volatile unsigned long long int __addr; \
sewardj0ec07f32006-01-12 12:32:32 +0000258 __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE \
259 /* %RAX = guest_NRADDR */ \
260 "xchgq %%rcx,%%rcx" \
261 : "=a" (__addr) \
262 : \
263 : "cc", "memory" \
264 ); \
sewardjc8858442006-01-20 15:17:20 +0000265 _zzq_orig->nraddr = __addr; \
sewardj0ec07f32006-01-12 12:32:32 +0000266 }
267
268#define VALGRIND_CALL_NOREDIR_RAX \
269 __SPECIAL_INSTRUCTION_PREAMBLE \
270 /* call-noredir *%RAX */ \
271 "xchgq %%rdx,%%rdx\n\t"
272#endif /* ARCH_amd64 */
273
274/* --------------------------- ppc32 --------------------------- */
275
276#if defined(ARCH_ppc32)
sewardjd68ac3e2006-01-20 14:31:57 +0000277
278typedef
279 struct {
sewardjc8858442006-01-20 15:17:20 +0000280 unsigned int nraddr; /* where's the code? */
sewardjd68ac3e2006-01-20 14:31:57 +0000281 }
282 OrigFn;
283
sewardj0ec07f32006-01-12 12:32:32 +0000284#define __SPECIAL_INSTRUCTION_PREAMBLE \
285 "rlwinm 0,0,3,0,0 ; rlwinm 0,0,13,0,0\n\t" \
sewardj1a85f4f2006-01-12 21:15:35 +0000286 "rlwinm 0,0,29,0,0 ; rlwinm 0,0,19,0,0\n\t"
sewardj0ec07f32006-01-12 12:32:32 +0000287
288#define VALGRIND_DO_CLIENT_REQUEST( \
289 _zzq_rlval, _zzq_default, _zzq_request, \
sewardj9af10a12006-02-01 14:59:42 +0000290 _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4, _zzq_arg5) \
sewardj0ec07f32006-01-12 12:32:32 +0000291 \
sewardj9af10a12006-02-01 14:59:42 +0000292 { unsigned int _zzq_args[6]; \
sewardj1a85f4f2006-01-12 21:15:35 +0000293 register unsigned int _zzq_result __asm__("r3"); \
294 register unsigned int* _zzq_ptr __asm__("r4"); \
sewardj0ec07f32006-01-12 12:32:32 +0000295 _zzq_args[0] = (unsigned int)(_zzq_request); \
296 _zzq_args[1] = (unsigned int)(_zzq_arg1); \
297 _zzq_args[2] = (unsigned int)(_zzq_arg2); \
298 _zzq_args[3] = (unsigned int)(_zzq_arg3); \
299 _zzq_args[4] = (unsigned int)(_zzq_arg4); \
sewardj9af10a12006-02-01 14:59:42 +0000300 _zzq_args[5] = (unsigned int)(_zzq_arg5); \
sewardj0ec07f32006-01-12 12:32:32 +0000301 _zzq_ptr = _zzq_args; \
302 __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE \
303 /* %R3 = client_request ( %R4 ) */ \
304 "or 1,1,1" \
305 : "=r" (_zzq_result) \
306 : "0" (_zzq_default), "r" (_zzq_ptr) \
307 : "cc", "memory"); \
308 _zzq_rlval = _zzq_result; \
309 }
310
sewardjd68ac3e2006-01-20 14:31:57 +0000311#define VALGRIND_GET_NR_CONTEXT(_zzq_rlval) \
312 { volatile OrigFn* _zzq_orig = &(_zzq_rlval); \
313 register unsigned int __addr __asm__("r3"); \
sewardj0ec07f32006-01-12 12:32:32 +0000314 __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE \
315 /* %R3 = guest_NRADDR */ \
316 "or 2,2,2" \
317 : "=r" (__addr) \
318 : \
319 : "cc", "memory" \
320 ); \
sewardjd68ac3e2006-01-20 14:31:57 +0000321 _zzq_orig->nraddr = __addr; \
sewardj0ec07f32006-01-12 12:32:32 +0000322 }
323
324#define VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11 \
325 __SPECIAL_INSTRUCTION_PREAMBLE \
326 /* branch-and-link-to-noredir *%R11 */ \
327 "or 3,3,3\n\t"
328#endif /* ARCH_ppc32 */
329
330/* --------------------------- ppc64 --------------------------- */
331
332#if defined(ARCH_ppc64)
sewardjd68ac3e2006-01-20 14:31:57 +0000333
334typedef
335 struct {
336 unsigned long long int nraddr; /* where's the code? */
337 unsigned long long int r2; /* what tocptr do we need? */
338 }
339 OrigFn;
340
sewardj1a85f4f2006-01-12 21:15:35 +0000341#define __SPECIAL_INSTRUCTION_PREAMBLE \
342 "rotldi 0,0,3 ; rotldi 0,0,13\n\t" \
343 "rotldi 0,0,61 ; rotldi 0,0,51\n\t"
344
sewardj0ec07f32006-01-12 12:32:32 +0000345#define VALGRIND_DO_CLIENT_REQUEST( \
346 _zzq_rlval, _zzq_default, _zzq_request, \
sewardj9af10a12006-02-01 14:59:42 +0000347 _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4, _zzq_arg5) \
sewardj0ec07f32006-01-12 12:32:32 +0000348 \
sewardj9af10a12006-02-01 14:59:42 +0000349 { unsigned long long int _zzq_args[6]; \
sewardj1a85f4f2006-01-12 21:15:35 +0000350 register unsigned long long int _zzq_result __asm__("r3"); \
351 register unsigned long long int* _zzq_ptr __asm__("r4"); \
352 _zzq_args[0] = (unsigned long long int)(_zzq_request); \
353 _zzq_args[1] = (unsigned long long int)(_zzq_arg1); \
354 _zzq_args[2] = (unsigned long long int)(_zzq_arg2); \
355 _zzq_args[3] = (unsigned long long int)(_zzq_arg3); \
356 _zzq_args[4] = (unsigned long long int)(_zzq_arg4); \
sewardj9af10a12006-02-01 14:59:42 +0000357 _zzq_args[5] = (unsigned long long int)(_zzq_arg5); \
sewardj0ec07f32006-01-12 12:32:32 +0000358 _zzq_ptr = _zzq_args; \
sewardj1a85f4f2006-01-12 21:15:35 +0000359 __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE \
360 /* %R3 = client_request ( %R4 ) */ \
361 "or 1,1,1" \
362 : "=r" (_zzq_result) \
sewardj0ec07f32006-01-12 12:32:32 +0000363 : "0" (_zzq_default), "r" (_zzq_ptr) \
sewardj1a85f4f2006-01-12 21:15:35 +0000364 : "cc", "memory"); \
365 _zzq_rlval = _zzq_result; \
sewardj0ec07f32006-01-12 12:32:32 +0000366 }
sewardj1a85f4f2006-01-12 21:15:35 +0000367
sewardjd68ac3e2006-01-20 14:31:57 +0000368#define VALGRIND_GET_NR_CONTEXT(_zzq_rlval) \
369 { volatile OrigFn* _zzq_orig = &(_zzq_rlval); \
370 register unsigned long long int __addr __asm__("r3"); \
sewardj1a85f4f2006-01-12 21:15:35 +0000371 __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE \
372 /* %R3 = guest_NRADDR */ \
373 "or 2,2,2" \
374 : "=r" (__addr) \
375 : \
376 : "cc", "memory" \
377 ); \
sewardjd68ac3e2006-01-20 14:31:57 +0000378 _zzq_orig->nraddr = __addr; \
379 __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE \
380 /* %R3 = guest_NRADDR_GPR2 */ \
381 "or 4,4,4" \
382 : "=r" (__addr) \
383 : \
384 : "cc", "memory" \
385 ); \
386 _zzq_orig->r2 = __addr; \
sewardj1a85f4f2006-01-12 21:15:35 +0000387 }
388
389#define VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11 \
390 __SPECIAL_INSTRUCTION_PREAMBLE \
391 /* branch-and-link-to-noredir *%R11 */ \
392 "or 3,3,3\n\t"
393
sewardj0ec07f32006-01-12 12:32:32 +0000394#endif /* ARCH_ppc64 */
cerion85665ca2005-06-20 15:51:07 +0000395
njn3dd0a912005-06-28 19:44:10 +0000396/* Insert assembly code for other architectures here... */
njn26aba4d2005-05-16 13:31:23 +0000397
sewardj37091fb2002-11-16 11:06:50 +0000398#endif /* NVALGRIND */
sewardj2e93c502002-04-12 11:12:52 +0000399
nethercote69d9c462004-10-26 13:00:12 +0000400
njn30d76c62005-06-18 15:07:39 +0000401/* ------------------------------------------------------------------ */
sewardj0ec07f32006-01-12 12:32:32 +0000402/* ARCHITECTURE SPECIFICS for FUNCTION WRAPPING. This is all very */
403/* ugly. It's the least-worst tradeoff I can think of. */
404/* ------------------------------------------------------------------ */
405
406/* This section defines magic (a.k.a appalling-hack) macros for doing
407 guaranteed-no-redirection macros, so as to get from function
408 wrappers to the functions they are wrapping. The whole point is to
409 construct standard call sequences, but to do the call itself with a
410 special no-redirect call pseudo-instruction that the JIT
411 understands and handles specially. This section is long and
412 repetitious, and I can't see a way to make it shorter.
413
414 The naming scheme is as follows:
415
416 CALL_FN_{W,v}_{v,W,WW,WWW,WWWW,5W,6W,7W,etc}
417
418 'W' stands for "word" and 'v' for "void". Hence there are
419 different macros for calling arity 0, 1, 2, 3, 4, etc, functions,
420 and for each, the possibility of returning a word-typed result, or
421 no result.
422*/
423
424/* Use these to write the name of your wrapper. NOTE: duplicates
425 VG_WRAP_FUNCTION_Z{U,Z} in pub_tool_redir.h. */
426
427#define I_WRAP_SONAME_FNNAME_ZU(soname,fnname) \
428 _vgwZU_##soname##_##fnname
429
430#define I_WRAP_SONAME_FNNAME_ZZ(soname,fnname) \
431 _vgwZZ_##soname##_##fnname
432
sewardjd68ac3e2006-01-20 14:31:57 +0000433/* Use this macro from within a wrapper function to collect the
434 context (address and possibly other info) of the original function.
435 Once you have that you can then use it in one of the CALL_FN_
436 macros. The type of the argument _lval is OrigFn. */
437#define VALGRIND_GET_ORIG_FN(_lval) VALGRIND_GET_NR_CONTEXT(_lval)
sewardj0ec07f32006-01-12 12:32:32 +0000438
439/* Derivatives of the main macros below, for calling functions
440 returning void. */
441
442#define CALL_FN_v_v(fnptr) \
443 do { volatile unsigned long _junk; \
444 CALL_FN_W_v(_junk,fnptr); } while (0)
445
446#define CALL_FN_v_W(fnptr, arg1) \
447 do { volatile unsigned long _junk; \
448 CALL_FN_W_W(_junk,fnptr,arg1); } while (0)
449
450#define CALL_FN_v_WW(fnptr, arg1,arg2) \
451 do { volatile unsigned long _junk; \
452 CALL_FN_W_WW(_junk,fnptr,arg1,arg2); } while (0)
453
454/* ---------------------------- x86 ---------------------------- */
455
456#if defined(ARCH_x86)
457
458/* These regs are trashed by the hidden call. No need to mention eax
459 as gcc can already see that, plus causes gcc to bomb. */
460#define __CALLER_SAVED_REGS /*"eax"*/ "ecx", "edx"
461
462/* These CALL_FN_ macros assume that on x86-linux, sizeof(unsigned
463 long) == 4. */
464
sewardj66226cc2006-01-20 15:46:46 +0000465#define CALL_FN_W_v(lval, orig) \
sewardj0ec07f32006-01-12 12:32:32 +0000466 do { \
sewardj66226cc2006-01-20 15:46:46 +0000467 volatile OrigFn _orig = (orig); \
sewardj0ec07f32006-01-12 12:32:32 +0000468 volatile unsigned long _argvec[1]; \
469 volatile unsigned long _res; \
sewardj66226cc2006-01-20 15:46:46 +0000470 _argvec[0] = (unsigned long)_orig.nraddr; \
sewardj0ec07f32006-01-12 12:32:32 +0000471 __asm__ volatile( \
472 "movl (%%eax), %%eax\n\t" /* target->%eax */ \
473 VALGRIND_CALL_NOREDIR_EAX \
474 : /*out*/ "=a" (_res) \
475 : /*in*/ "a" (&_argvec[0]) \
476 : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \
477 ); \
478 lval = (__typeof__(lval)) _res; \
479 } while (0)
480
sewardj66226cc2006-01-20 15:46:46 +0000481#define CALL_FN_W_W(lval, orig, arg1) \
sewardj0ec07f32006-01-12 12:32:32 +0000482 do { \
sewardj66226cc2006-01-20 15:46:46 +0000483 volatile OrigFn _orig = (orig); \
sewardj0ec07f32006-01-12 12:32:32 +0000484 volatile unsigned long _argvec[2]; \
485 volatile unsigned long _res; \
sewardj66226cc2006-01-20 15:46:46 +0000486 _argvec[0] = (unsigned long)_orig.nraddr; \
sewardj0ec07f32006-01-12 12:32:32 +0000487 _argvec[1] = (unsigned long)(arg1); \
488 __asm__ volatile( \
489 "pushl 4(%%eax)\n\t" \
490 "movl (%%eax), %%eax\n\t" /* target->%eax */ \
491 VALGRIND_CALL_NOREDIR_EAX \
492 "addl $4, %%esp\n" \
493 : /*out*/ "=a" (_res) \
494 : /*in*/ "a" (&_argvec[0]) \
495 : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \
496 ); \
497 lval = (__typeof__(lval)) _res; \
498 } while (0)
499
sewardj66226cc2006-01-20 15:46:46 +0000500#define CALL_FN_W_WW(lval, orig, arg1,arg2) \
sewardj0ec07f32006-01-12 12:32:32 +0000501 do { \
sewardj66226cc2006-01-20 15:46:46 +0000502 volatile OrigFn _orig = (orig); \
sewardj0ec07f32006-01-12 12:32:32 +0000503 volatile unsigned long _argvec[3]; \
504 volatile unsigned long _res; \
sewardj66226cc2006-01-20 15:46:46 +0000505 _argvec[0] = (unsigned long)_orig.nraddr; \
sewardj0ec07f32006-01-12 12:32:32 +0000506 _argvec[1] = (unsigned long)(arg1); \
507 _argvec[2] = (unsigned long)(arg2); \
508 __asm__ volatile( \
509 "pushl 8(%%eax)\n\t" \
510 "pushl 4(%%eax)\n\t" \
511 "movl (%%eax), %%eax\n\t" /* target->%eax */ \
512 VALGRIND_CALL_NOREDIR_EAX \
513 "addl $8, %%esp\n" \
514 : /*out*/ "=a" (_res) \
515 : /*in*/ "a" (&_argvec[0]) \
516 : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \
517 ); \
518 lval = (__typeof__(lval)) _res; \
519 } while (0)
520
sewardj66226cc2006-01-20 15:46:46 +0000521#define CALL_FN_W_WWWW(lval, orig, arg1,arg2,arg3,arg4) \
sewardj0ec07f32006-01-12 12:32:32 +0000522 do { \
sewardj66226cc2006-01-20 15:46:46 +0000523 volatile OrigFn _orig = (orig); \
sewardj0ec07f32006-01-12 12:32:32 +0000524 volatile unsigned long _argvec[5]; \
525 volatile unsigned long _res; \
sewardj66226cc2006-01-20 15:46:46 +0000526 _argvec[0] = (unsigned long)_orig.nraddr; \
sewardj0ec07f32006-01-12 12:32:32 +0000527 _argvec[1] = (unsigned long)(arg1); \
528 _argvec[2] = (unsigned long)(arg2); \
529 _argvec[3] = (unsigned long)(arg3); \
530 _argvec[4] = (unsigned long)(arg4); \
531 __asm__ volatile( \
532 "pushl 16(%%eax)\n\t" \
533 "pushl 12(%%eax)\n\t" \
534 "pushl 8(%%eax)\n\t" \
535 "pushl 4(%%eax)\n\t" \
536 "movl (%%eax), %%eax\n\t" /* target->%eax */ \
537 VALGRIND_CALL_NOREDIR_EAX \
538 "addl $16, %%esp\n" \
539 : /*out*/ "=a" (_res) \
540 : /*in*/ "a" (&_argvec[0]) \
541 : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \
542 ); \
543 lval = (__typeof__(lval)) _res; \
544 } while (0)
545
sewardj66226cc2006-01-20 15:46:46 +0000546#define CALL_FN_W_5W(lval, orig, arg1,arg2,arg3,arg4,arg5) \
sewardj0ec07f32006-01-12 12:32:32 +0000547 do { \
sewardj66226cc2006-01-20 15:46:46 +0000548 volatile OrigFn _orig = (orig); \
sewardj0ec07f32006-01-12 12:32:32 +0000549 volatile unsigned long _argvec[6]; \
550 volatile unsigned long _res; \
sewardj66226cc2006-01-20 15:46:46 +0000551 _argvec[0] = (unsigned long)_orig.nraddr; \
sewardj0ec07f32006-01-12 12:32:32 +0000552 _argvec[1] = (unsigned long)(arg1); \
553 _argvec[2] = (unsigned long)(arg2); \
554 _argvec[3] = (unsigned long)(arg3); \
555 _argvec[4] = (unsigned long)(arg4); \
556 _argvec[5] = (unsigned long)(arg5); \
557 __asm__ volatile( \
558 "pushl 20(%%eax)\n\t" \
559 "pushl 16(%%eax)\n\t" \
560 "pushl 12(%%eax)\n\t" \
561 "pushl 8(%%eax)\n\t" \
562 "pushl 4(%%eax)\n\t" \
563 "movl (%%eax), %%eax\n\t" /* target->%eax */ \
564 VALGRIND_CALL_NOREDIR_EAX \
565 "addl $20, %%esp\n" \
566 : /*out*/ "=a" (_res) \
567 : /*in*/ "a" (&_argvec[0]) \
568 : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \
569 ); \
570 lval = (__typeof__(lval)) _res; \
571 } while (0)
572
sewardj66226cc2006-01-20 15:46:46 +0000573#define CALL_FN_W_6W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6) \
sewardj0ec07f32006-01-12 12:32:32 +0000574 do { \
sewardj66226cc2006-01-20 15:46:46 +0000575 volatile OrigFn _orig = (orig); \
sewardj0ec07f32006-01-12 12:32:32 +0000576 volatile unsigned long _argvec[7]; \
577 volatile unsigned long _res; \
sewardj66226cc2006-01-20 15:46:46 +0000578 _argvec[0] = (unsigned long)_orig.nraddr; \
sewardj0ec07f32006-01-12 12:32:32 +0000579 _argvec[1] = (unsigned long)(arg1); \
580 _argvec[2] = (unsigned long)(arg2); \
581 _argvec[3] = (unsigned long)(arg3); \
582 _argvec[4] = (unsigned long)(arg4); \
583 _argvec[5] = (unsigned long)(arg5); \
584 _argvec[6] = (unsigned long)(arg6); \
585 __asm__ volatile( \
586 "pushl 24(%%eax)\n\t" \
587 "pushl 20(%%eax)\n\t" \
588 "pushl 16(%%eax)\n\t" \
589 "pushl 12(%%eax)\n\t" \
590 "pushl 8(%%eax)\n\t" \
591 "pushl 4(%%eax)\n\t" \
592 "movl (%%eax), %%eax\n\t" /* target->%eax */ \
593 VALGRIND_CALL_NOREDIR_EAX \
594 "addl $24, %%esp\n" \
595 : /*out*/ "=a" (_res) \
596 : /*in*/ "a" (&_argvec[0]) \
597 : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \
598 ); \
599 lval = (__typeof__(lval)) _res; \
600 } while (0)
601
sewardj66226cc2006-01-20 15:46:46 +0000602#define CALL_FN_W_7W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6, \
603 arg7) \
sewardj0ec07f32006-01-12 12:32:32 +0000604 do { \
sewardj66226cc2006-01-20 15:46:46 +0000605 volatile OrigFn _orig = (orig); \
sewardj0ec07f32006-01-12 12:32:32 +0000606 volatile unsigned long _argvec[8]; \
607 volatile unsigned long _res; \
sewardj66226cc2006-01-20 15:46:46 +0000608 _argvec[0] = (unsigned long)_orig.nraddr; \
sewardj0ec07f32006-01-12 12:32:32 +0000609 _argvec[1] = (unsigned long)(arg1); \
610 _argvec[2] = (unsigned long)(arg2); \
611 _argvec[3] = (unsigned long)(arg3); \
612 _argvec[4] = (unsigned long)(arg4); \
613 _argvec[5] = (unsigned long)(arg5); \
614 _argvec[6] = (unsigned long)(arg6); \
615 _argvec[7] = (unsigned long)(arg7); \
616 __asm__ volatile( \
617 "pushl 28(%%eax)\n\t" \
618 "pushl 24(%%eax)\n\t" \
619 "pushl 20(%%eax)\n\t" \
620 "pushl 16(%%eax)\n\t" \
621 "pushl 12(%%eax)\n\t" \
622 "pushl 8(%%eax)\n\t" \
623 "pushl 4(%%eax)\n\t" \
624 "movl (%%eax), %%eax\n\t" /* target->%eax */ \
625 VALGRIND_CALL_NOREDIR_EAX \
626 "addl $28, %%esp\n" \
627 : /*out*/ "=a" (_res) \
628 : /*in*/ "a" (&_argvec[0]) \
629 : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \
630 ); \
631 lval = (__typeof__(lval)) _res; \
632 } while (0)
633
sewardj66226cc2006-01-20 15:46:46 +0000634#define CALL_FN_W_8W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6, \
635 arg7,arg8) \
sewardj0ec07f32006-01-12 12:32:32 +0000636 do { \
sewardj66226cc2006-01-20 15:46:46 +0000637 volatile OrigFn _orig = (orig); \
sewardj0ec07f32006-01-12 12:32:32 +0000638 volatile unsigned long _argvec[9]; \
639 volatile unsigned long _res; \
sewardj66226cc2006-01-20 15:46:46 +0000640 _argvec[0] = (unsigned long)_orig.nraddr; \
sewardj0ec07f32006-01-12 12:32:32 +0000641 _argvec[1] = (unsigned long)(arg1); \
642 _argvec[2] = (unsigned long)(arg2); \
643 _argvec[3] = (unsigned long)(arg3); \
644 _argvec[4] = (unsigned long)(arg4); \
645 _argvec[5] = (unsigned long)(arg5); \
646 _argvec[6] = (unsigned long)(arg6); \
647 _argvec[7] = (unsigned long)(arg7); \
648 _argvec[8] = (unsigned long)(arg8); \
649 __asm__ volatile( \
650 "pushl 32(%%eax)\n\t" \
651 "pushl 28(%%eax)\n\t" \
652 "pushl 24(%%eax)\n\t" \
653 "pushl 20(%%eax)\n\t" \
654 "pushl 16(%%eax)\n\t" \
655 "pushl 12(%%eax)\n\t" \
656 "pushl 8(%%eax)\n\t" \
657 "pushl 4(%%eax)\n\t" \
658 "movl (%%eax), %%eax\n\t" /* target->%eax */ \
659 VALGRIND_CALL_NOREDIR_EAX \
660 "addl $32, %%esp\n" \
661 : /*out*/ "=a" (_res) \
662 : /*in*/ "a" (&_argvec[0]) \
663 : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \
664 ); \
665 lval = (__typeof__(lval)) _res; \
666 } while (0)
667
sewardj66226cc2006-01-20 15:46:46 +0000668#define CALL_FN_W_12W(lval, orig, arg1,arg2,arg3,arg4,arg5, \
669 arg6,arg7,arg8,arg9,arg10, \
670 arg11,arg12) \
sewardj0ec07f32006-01-12 12:32:32 +0000671 do { \
sewardj66226cc2006-01-20 15:46:46 +0000672 volatile OrigFn _orig = (orig); \
sewardj0ec07f32006-01-12 12:32:32 +0000673 volatile unsigned long _argvec[13]; \
674 volatile unsigned long _res; \
sewardj66226cc2006-01-20 15:46:46 +0000675 _argvec[0] = (unsigned long)_orig.nraddr; \
sewardj0ec07f32006-01-12 12:32:32 +0000676 _argvec[1] = (unsigned long)(arg1); \
677 _argvec[2] = (unsigned long)(arg2); \
678 _argvec[3] = (unsigned long)(arg3); \
679 _argvec[4] = (unsigned long)(arg4); \
680 _argvec[5] = (unsigned long)(arg5); \
681 _argvec[6] = (unsigned long)(arg6); \
682 _argvec[7] = (unsigned long)(arg7); \
683 _argvec[8] = (unsigned long)(arg8); \
684 _argvec[9] = (unsigned long)(arg9); \
685 _argvec[10] = (unsigned long)(arg10); \
686 _argvec[11] = (unsigned long)(arg11); \
687 _argvec[12] = (unsigned long)(arg12); \
688 __asm__ volatile( \
689 "pushl 48(%%eax)\n\t" \
690 "pushl 44(%%eax)\n\t" \
691 "pushl 40(%%eax)\n\t" \
692 "pushl 36(%%eax)\n\t" \
693 "pushl 32(%%eax)\n\t" \
694 "pushl 28(%%eax)\n\t" \
695 "pushl 24(%%eax)\n\t" \
696 "pushl 20(%%eax)\n\t" \
697 "pushl 16(%%eax)\n\t" \
698 "pushl 12(%%eax)\n\t" \
699 "pushl 8(%%eax)\n\t" \
700 "pushl 4(%%eax)\n\t" \
701 "movl (%%eax), %%eax\n\t" /* target->%eax */ \
702 VALGRIND_CALL_NOREDIR_EAX \
703 "addl $48, %%esp\n" \
704 : /*out*/ "=a" (_res) \
705 : /*in*/ "a" (&_argvec[0]) \
706 : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \
707 ); \
708 lval = (__typeof__(lval)) _res; \
709 } while (0)
710
711#endif /* ARCH_x86 */
712
713/* --------------------------- amd64 --------------------------- */
714
715#if defined(ARCH_amd64)
716
717/* ARGREGS: rdi rsi rdx rcx r8 r9 (the rest on stack in R-to-L order) */
718
719/* These regs are trashed by the hidden call. */
720#define __CALLER_SAVED_REGS /*"rax",*/ "rcx", "rdx", "rsi", \
721 "rdi", "r8", "r9", "r10", "r11"
722
723/* These CALL_FN_ macros assume that on amd64-linux, sizeof(unsigned
724 long) == 8. */
725
sewardjc8858442006-01-20 15:17:20 +0000726#define CALL_FN_W_v(lval, orig) \
sewardj0ec07f32006-01-12 12:32:32 +0000727 do { \
sewardjc8858442006-01-20 15:17:20 +0000728 volatile OrigFn _orig = (orig); \
sewardj0ec07f32006-01-12 12:32:32 +0000729 volatile unsigned long _argvec[1]; \
730 volatile unsigned long _res; \
sewardjc8858442006-01-20 15:17:20 +0000731 _argvec[0] = (unsigned long)_orig.nraddr; \
sewardj0ec07f32006-01-12 12:32:32 +0000732 __asm__ volatile( \
733 "movq (%%rax), %%rax\n\t" /* target->%rax */ \
734 VALGRIND_CALL_NOREDIR_RAX \
735 : /*out*/ "=a" (_res) \
736 : /*in*/ "a" (&_argvec[0]) \
737 : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \
738 ); \
739 lval = (__typeof__(lval)) _res; \
740 } while (0)
741
sewardjc8858442006-01-20 15:17:20 +0000742#define CALL_FN_W_W(lval, orig, arg1) \
sewardj0ec07f32006-01-12 12:32:32 +0000743 do { \
sewardjc8858442006-01-20 15:17:20 +0000744 volatile OrigFn _orig = (orig); \
sewardj0ec07f32006-01-12 12:32:32 +0000745 volatile unsigned long _argvec[2]; \
746 volatile unsigned long _res; \
sewardjc8858442006-01-20 15:17:20 +0000747 _argvec[0] = (unsigned long)_orig.nraddr; \
sewardj0ec07f32006-01-12 12:32:32 +0000748 _argvec[1] = (unsigned long)(arg1); \
749 __asm__ volatile( \
750 "movq 8(%%rax), %%rdi\n\t" \
751 "movq (%%rax), %%rax\n\t" /* target->%rax */ \
752 VALGRIND_CALL_NOREDIR_RAX \
753 : /*out*/ "=a" (_res) \
754 : /*in*/ "a" (&_argvec[0]) \
755 : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \
756 ); \
757 lval = (__typeof__(lval)) _res; \
758 } while (0)
759
sewardjc8858442006-01-20 15:17:20 +0000760#define CALL_FN_W_WW(lval, orig, arg1,arg2) \
sewardj0ec07f32006-01-12 12:32:32 +0000761 do { \
sewardjc8858442006-01-20 15:17:20 +0000762 volatile OrigFn _orig = (orig); \
sewardj0ec07f32006-01-12 12:32:32 +0000763 volatile unsigned long _argvec[3]; \
764 volatile unsigned long _res; \
sewardjc8858442006-01-20 15:17:20 +0000765 _argvec[0] = (unsigned long)_orig.nraddr; \
sewardj0ec07f32006-01-12 12:32:32 +0000766 _argvec[1] = (unsigned long)(arg1); \
767 _argvec[2] = (unsigned long)(arg2); \
768 __asm__ volatile( \
769 "movq 16(%%rax), %%rsi\n\t" \
770 "movq 8(%%rax), %%rdi\n\t" \
771 "movq (%%rax), %%rax\n\t" /* target->%rax */ \
772 VALGRIND_CALL_NOREDIR_RAX \
773 : /*out*/ "=a" (_res) \
774 : /*in*/ "a" (&_argvec[0]) \
775 : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \
776 ); \
777 lval = (__typeof__(lval)) _res; \
778 } while (0)
779
780#endif /* ARCH_amd64 */
781
782/* --------------------------- ppc32 --------------------------- */
783
784#if defined(ARCH_ppc32)
785
786/* ARGREGS: r3 r4 r5 r6 r7 r8 r9 r10 (the rest on stack somewhere) */
787
788/* These regs are trashed by the hidden call. */
789#define __CALLER_SAVED_REGS "lr", \
790 "r0", "r2", "r3", "r4", "r5", "r6", \
791 "r7", "r8", "r9", "r10", "r11", "r12"
792
793/* These CALL_FN_ macros assume that on ppc32-linux, sizeof(unsigned
794 long) == 4. */
795
sewardj38de0992006-01-20 16:46:34 +0000796#define CALL_FN_W_v(lval, orig) \
sewardj0ec07f32006-01-12 12:32:32 +0000797 do { \
sewardjd68ac3e2006-01-20 14:31:57 +0000798 volatile OrigFn _orig = (orig); \
sewardj0ec07f32006-01-12 12:32:32 +0000799 volatile unsigned long _argvec[1]; \
800 volatile unsigned long _res; \
sewardjd68ac3e2006-01-20 14:31:57 +0000801 _argvec[0] = (unsigned long)_orig.nraddr; \
sewardj0ec07f32006-01-12 12:32:32 +0000802 __asm__ volatile( \
803 "mr 11,%1\n\t" \
804 "lwz 11,0(11)\n\t" /* target->r11 */ \
805 VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11 \
806 "mr %0,3" \
807 : /*out*/ "=r" (_res) \
808 : /*in*/ "r" (&_argvec[0]) \
809 : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \
810 ); \
811 lval = (__typeof__(lval)) _res; \
812 } while (0)
813
sewardj38de0992006-01-20 16:46:34 +0000814#define CALL_FN_W_W(lval, orig, arg1) \
sewardj0ec07f32006-01-12 12:32:32 +0000815 do { \
sewardj38de0992006-01-20 16:46:34 +0000816 volatile OrigFn _orig = (orig); \
sewardj0ec07f32006-01-12 12:32:32 +0000817 volatile unsigned long _argvec[2]; \
818 volatile unsigned long _res; \
sewardj38de0992006-01-20 16:46:34 +0000819 _argvec[0] = (unsigned long)_orig.nraddr; \
sewardj0ec07f32006-01-12 12:32:32 +0000820 _argvec[1] = (unsigned long)arg1; \
821 __asm__ volatile( \
822 "mr 11,%1\n\t" \
823 "lwz 3,4(11)\n\t" /* arg1->r3 */ \
824 "lwz 11,0(11)\n\t" /* target->r11 */ \
825 VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11 \
826 "mr %0,3" \
827 : /*out*/ "=r" (_res) \
828 : /*in*/ "r" (&_argvec[0]) \
829 : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \
830 ); \
831 lval = (__typeof__(lval)) _res; \
832 } while (0)
833
sewardj38de0992006-01-20 16:46:34 +0000834#define CALL_FN_W_WW(lval, orig, arg1,arg2) \
sewardj0ec07f32006-01-12 12:32:32 +0000835 do { \
sewardj38de0992006-01-20 16:46:34 +0000836 volatile OrigFn _orig = (orig); \
sewardj0ec07f32006-01-12 12:32:32 +0000837 volatile unsigned long _argvec[3]; \
838 volatile unsigned long _res; \
sewardj38de0992006-01-20 16:46:34 +0000839 _argvec[0] = (unsigned long)_orig.nraddr; \
sewardj0ec07f32006-01-12 12:32:32 +0000840 _argvec[1] = (unsigned long)arg1; \
841 _argvec[2] = (unsigned long)arg2; \
842 __asm__ volatile( \
843 "mr 11,%1\n\t" \
844 "lwz 3,4(11)\n\t" /* arg1->r3 */ \
845 "lwz 4,8(11)\n\t" \
846 "lwz 11,0(11)\n\t" /* target->r11 */ \
847 VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11 \
848 "mr %0,3" \
849 : /*out*/ "=r" (_res) \
850 : /*in*/ "r" (&_argvec[0]) \
851 : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \
852 ); \
853 lval = (__typeof__(lval)) _res; \
854 } while (0)
855
856#endif /* ARCH_ppc32 */
857
858/* --------------------------- ppc64 --------------------------- */
859
sewardj9734b202006-01-17 01:49:37 +0000860#if defined(ARCH_ppc64)
861
862/* ARGREGS: r3 r4 r5 r6 r7 r8 r9 r10 (the rest on stack somewhere) */
863
864/* These regs are trashed by the hidden call. */
865#define __CALLER_SAVED_REGS "lr", \
866 "r0", "r2", "r3", "r4", "r5", "r6", \
867 "r7", "r8", "r9", "r10", "r11", "r12"
868
869/* These CALL_FN_ macros assume that on ppc64-linux, sizeof(unsigned
870 long) == 8. */
871
sewardjd68ac3e2006-01-20 14:31:57 +0000872#define CALL_FN_W_v(lval, orig) \
sewardj9734b202006-01-17 01:49:37 +0000873 do { \
sewardjd68ac3e2006-01-20 14:31:57 +0000874 volatile OrigFn _orig = (orig); \
875 volatile unsigned long _argvec[3+0]; \
sewardj9734b202006-01-17 01:49:37 +0000876 volatile unsigned long _res; \
sewardjd68ac3e2006-01-20 14:31:57 +0000877 /* _argvec[0] holds current r2 across the call */ \
878 _argvec[1] = (unsigned long)_orig.r2; \
879 _argvec[2] = (unsigned long)_orig.nraddr; \
sewardj9734b202006-01-17 01:49:37 +0000880 __asm__ volatile( \
881 "mr 11,%1\n\t" \
sewardjd68ac3e2006-01-20 14:31:57 +0000882 "std 2,-16(11)\n\t" /* save tocptr */ \
883 "ld 2,-8(11)\n\t" /* use nraddr's tocptr */ \
884 "ld 11, 0(11)\n\t" /* target->r11 */ \
sewardj9734b202006-01-17 01:49:37 +0000885 VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11 \
886 "mr 11,%1\n\t" \
887 "mr %0,3\n\t" \
sewardjd68ac3e2006-01-20 14:31:57 +0000888 "ld 2,-16(11)" /* restore tocptr */ \
sewardj9734b202006-01-17 01:49:37 +0000889 : /*out*/ "=r" (_res) \
sewardjd68ac3e2006-01-20 14:31:57 +0000890 : /*in*/ "r" (&_argvec[2]) \
sewardj9734b202006-01-17 01:49:37 +0000891 : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \
892 ); \
893 lval = (__typeof__(lval)) _res; \
894 } while (0)
895
sewardjd68ac3e2006-01-20 14:31:57 +0000896#define CALL_FN_W_W(lval, orig, arg1) \
sewardj9734b202006-01-17 01:49:37 +0000897 do { \
sewardjd68ac3e2006-01-20 14:31:57 +0000898 volatile OrigFn _orig = (orig); \
899 volatile unsigned long _argvec[3+1]; \
sewardj9734b202006-01-17 01:49:37 +0000900 volatile unsigned long _res; \
sewardjd68ac3e2006-01-20 14:31:57 +0000901 /* _argvec[0] holds current r2 across the call */ \
902 _argvec[1] = (unsigned long)_orig.r2; \
903 _argvec[2] = (unsigned long)_orig.nraddr; \
904 _argvec[2+1] = (unsigned long)arg1; \
sewardj9734b202006-01-17 01:49:37 +0000905 __asm__ volatile( \
906 "mr 11,%1\n\t" \
sewardjd68ac3e2006-01-20 14:31:57 +0000907 "std 2,-16(11)\n\t" /* save tocptr */ \
908 "ld 2,-8(11)\n\t" /* use nraddr's tocptr */ \
909 "ld 3, 8(11)\n\t" /* arg1->r3 */ \
910 "ld 11, 0(11)\n\t" /* target->r11 */ \
sewardj9734b202006-01-17 01:49:37 +0000911 VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11 \
912 "mr 11,%1\n\t" \
913 "mr %0,3\n\t" \
sewardjd68ac3e2006-01-20 14:31:57 +0000914 "ld 2,-16(11)" /* restore tocptr */ \
sewardj9734b202006-01-17 01:49:37 +0000915 : /*out*/ "=r" (_res) \
sewardjd68ac3e2006-01-20 14:31:57 +0000916 : /*in*/ "r" (&_argvec[2]) \
sewardj9734b202006-01-17 01:49:37 +0000917 : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \
918 ); \
919 lval = (__typeof__(lval)) _res; \
920 } while (0)
921
sewardjd68ac3e2006-01-20 14:31:57 +0000922#define CALL_FN_W_WW(lval, orig, arg1,arg2) \
sewardj9734b202006-01-17 01:49:37 +0000923 do { \
sewardjd68ac3e2006-01-20 14:31:57 +0000924 volatile OrigFn _orig = (orig); \
925 volatile unsigned long _argvec[3+2]; \
sewardj9734b202006-01-17 01:49:37 +0000926 volatile unsigned long _res; \
sewardjd68ac3e2006-01-20 14:31:57 +0000927 /* _argvec[0] holds current r2 across the call */ \
928 _argvec[1] = (unsigned long)_orig.r2; \
929 _argvec[2] = (unsigned long)_orig.nraddr; \
930 _argvec[2+1] = (unsigned long)arg1; \
931 _argvec[2+2] = (unsigned long)arg2; \
sewardj9734b202006-01-17 01:49:37 +0000932 __asm__ volatile( \
933 "mr 11,%1\n\t" \
sewardjd68ac3e2006-01-20 14:31:57 +0000934 "std 2,-16(11)\n\t" /* save tocptr */ \
935 "ld 2,-8(11)\n\t" /* use nraddr's tocptr */ \
936 "ld 3, 8(11)\n\t" /* arg1->r3 */ \
937 "ld 4, 16(11)\n\t" /* arg1->r4 */ \
938 "ld 11, 0(11)\n\t" /* target->r11 */ \
sewardj9734b202006-01-17 01:49:37 +0000939 VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11 \
940 "mr 11,%1\n\t" \
941 "mr %0,3\n\t" \
sewardjd68ac3e2006-01-20 14:31:57 +0000942 "ld 2,-16(11)" /* restore tocptr */ \
sewardj9734b202006-01-17 01:49:37 +0000943 : /*out*/ "=r" (_res) \
sewardjd68ac3e2006-01-20 14:31:57 +0000944 : /*in*/ "r" (&_argvec[2]) \
sewardj9734b202006-01-17 01:49:37 +0000945 : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \
946 ); \
947 lval = (__typeof__(lval)) _res; \
948 } while (0)
949
950#endif /* ARCH_ppc64 */
951
sewardj0ec07f32006-01-12 12:32:32 +0000952
953/* ------------------------------------------------------------------ */
954/* ARCHITECTURE INDEPENDENT MACROS for CLIENT REQUESTS. */
955/* */
njn30d76c62005-06-18 15:07:39 +0000956/* ------------------------------------------------------------------ */
957
sewardj2e93c502002-04-12 11:12:52 +0000958/* Some request codes. There are many more of these, but most are not
959 exposed to end-user view. These are the public ones, all of the
njn25e49d8e72002-09-23 09:36:25 +0000960 form 0x1000 + small_number.
njnd7994182003-10-02 13:44:04 +0000961
sewardj0ec07f32006-01-12 12:32:32 +0000962 Core ones are in the range 0x00000000--0x0000ffff. The non-public
963 ones start at 0x2000.
sewardj2e93c502002-04-12 11:12:52 +0000964*/
965
sewardj0ec07f32006-01-12 12:32:32 +0000966/* These macros are used by tools -- they must be public, but don't
967 embed them into other programs. */
njnfc26ff92004-11-22 19:12:49 +0000968#define VG_USERREQ_TOOL_BASE(a,b) \
njn4c791212003-05-02 17:53:54 +0000969 ((unsigned int)(((a)&0xff) << 24 | ((b)&0xff) << 16))
njnfc26ff92004-11-22 19:12:49 +0000970#define VG_IS_TOOL_USERREQ(a, b, v) \
971 (VG_USERREQ_TOOL_BASE(a,b) == ((v) & 0xffff0000))
sewardj34042512002-10-22 04:14:35 +0000972
njn25e49d8e72002-09-23 09:36:25 +0000973typedef
njn4c791212003-05-02 17:53:54 +0000974 enum { VG_USERREQ__RUNNING_ON_VALGRIND = 0x1001,
975 VG_USERREQ__DISCARD_TRANSLATIONS = 0x1002,
njn3e884182003-04-15 13:03:23 +0000976
sewardj0ec07f32006-01-12 12:32:32 +0000977 /* These allow any function to be called from the simulated
978 CPU but run on the real CPU. Nb: the first arg passed to
979 the function is always the ThreadId of the running
980 thread! So CLIENT_CALL0 actually requires a 1 arg
njnd4795be2004-11-24 11:57:51 +0000981 function, etc. */
njn4c791212003-05-02 17:53:54 +0000982 VG_USERREQ__CLIENT_CALL0 = 0x1101,
983 VG_USERREQ__CLIENT_CALL1 = 0x1102,
984 VG_USERREQ__CLIENT_CALL2 = 0x1103,
985 VG_USERREQ__CLIENT_CALL3 = 0x1104,
njn3e884182003-04-15 13:03:23 +0000986
sewardj0ec07f32006-01-12 12:32:32 +0000987 /* Can be useful in regression testing suites -- eg. can
988 send Valgrind's output to /dev/null and still count
989 errors. */
njn4c791212003-05-02 17:53:54 +0000990 VG_USERREQ__COUNT_ERRORS = 0x1201,
njn47363ab2003-04-21 13:24:40 +0000991
sewardj0ec07f32006-01-12 12:32:32 +0000992 /* These are useful and can be interpreted by any tool that
993 tracks malloc() et al, by using vg_replace_malloc.c. */
njnd7994182003-10-02 13:44:04 +0000994 VG_USERREQ__MALLOCLIKE_BLOCK = 0x1301,
995 VG_USERREQ__FREELIKE_BLOCK = 0x1302,
rjwalshbc0bb832004-06-19 18:12:36 +0000996 /* Memory pool support. */
997 VG_USERREQ__CREATE_MEMPOOL = 0x1303,
998 VG_USERREQ__DESTROY_MEMPOOL = 0x1304,
999 VG_USERREQ__MEMPOOL_ALLOC = 0x1305,
1000 VG_USERREQ__MEMPOOL_FREE = 0x1306,
njnd7994182003-10-02 13:44:04 +00001001
fitzhardinge39de4b42003-10-31 07:12:21 +00001002 /* Allow printfs to valgrind log. */
njn30d76c62005-06-18 15:07:39 +00001003 VG_USERREQ__PRINTF = 0x1401,
rjwalsh0140af52005-06-04 20:42:33 +00001004 VG_USERREQ__PRINTF_BACKTRACE = 0x1402,
1005
1006 /* Stack support. */
1007 VG_USERREQ__STACK_REGISTER = 0x1501,
1008 VG_USERREQ__STACK_DEREGISTER = 0x1502,
1009 VG_USERREQ__STACK_CHANGE = 0x1503,
njn25e49d8e72002-09-23 09:36:25 +00001010 } Vg_ClientRequest;
sewardj2e93c502002-04-12 11:12:52 +00001011
sewardj0ec07f32006-01-12 12:32:32 +00001012#if !defined(__GNUC__)
1013# define __extension__ /* */
muellerc9b36552003-12-31 14:32:23 +00001014#endif
sewardj2e93c502002-04-12 11:12:52 +00001015
sewardj0ec07f32006-01-12 12:32:32 +00001016/* Returns the number of Valgrinds this code is running under. That
1017 is, 0 if running natively, 1 if running under Valgrind, 2 if
1018 running under Valgrind which is running under another Valgrind,
1019 etc. */
1020#define RUNNING_ON_VALGRIND __extension__ \
1021 ({unsigned int _qzz_res; \
1022 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0 /* if not */, \
1023 VG_USERREQ__RUNNING_ON_VALGRIND, \
sewardj9af10a12006-02-01 14:59:42 +00001024 0, 0, 0, 0, 0); \
sewardj0ec07f32006-01-12 12:32:32 +00001025 _qzz_res; \
sewardjde4a1d02002-03-22 01:27:54 +00001026 })
1027
1028
sewardj18d75132002-05-16 11:06:21 +00001029/* Discard translation of code in the range [_qzz_addr .. _qzz_addr +
1030 _qzz_len - 1]. Useful if you are debugging a JITter or some such,
1031 since it provides a way to make sure valgrind will retranslate the
1032 invalidated area. Returns no value. */
sewardj0ec07f32006-01-12 12:32:32 +00001033#define VALGRIND_DISCARD_TRANSLATIONS(_qzz_addr,_qzz_len) \
1034 {unsigned int _qzz_res; \
1035 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
1036 VG_USERREQ__DISCARD_TRANSLATIONS, \
sewardj9af10a12006-02-01 14:59:42 +00001037 _qzz_addr, _qzz_len, 0, 0, 0); \
sewardj18d75132002-05-16 11:06:21 +00001038 }
1039
njn26aba4d2005-05-16 13:31:23 +00001040
sewardj0ec07f32006-01-12 12:32:32 +00001041/* These requests are for getting Valgrind itself to print something.
1042 Possibly with a backtrace. This is a really ugly hack. */
1043
1044#if defined(NVALGRIND)
1045
1046# define VALGRIND_PRINTF(...)
1047# define VALGRIND_PRINTF_BACKTRACE(...)
njn26aba4d2005-05-16 13:31:23 +00001048
1049#else /* NVALGRIND */
fitzhardinge39de4b42003-10-31 07:12:21 +00001050
fitzhardingea09a1b52003-11-07 23:09:48 +00001051int VALGRIND_PRINTF(const char *format, ...)
1052 __attribute__((format(__printf__, 1, 2)));
fitzhardinge39de4b42003-10-31 07:12:21 +00001053__attribute__((weak))
1054int
fitzhardingea09a1b52003-11-07 23:09:48 +00001055VALGRIND_PRINTF(const char *format, ...)
fitzhardinge39de4b42003-10-31 07:12:21 +00001056{
njnc6168192004-11-29 13:54:10 +00001057 unsigned long _qzz_res;
fitzhardinge39de4b42003-10-31 07:12:21 +00001058 va_list vargs;
1059 va_start(vargs, format);
sewardj0ec07f32006-01-12 12:32:32 +00001060 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, VG_USERREQ__PRINTF,
sewardj9af10a12006-02-01 14:59:42 +00001061 (unsigned long)format, (unsigned long)vargs,
1062 0, 0, 0);
fitzhardinge39de4b42003-10-31 07:12:21 +00001063 va_end(vargs);
njnc6168192004-11-29 13:54:10 +00001064 return (int)_qzz_res;
fitzhardinge39de4b42003-10-31 07:12:21 +00001065}
1066
fitzhardingea09a1b52003-11-07 23:09:48 +00001067int VALGRIND_PRINTF_BACKTRACE(const char *format, ...)
1068 __attribute__((format(__printf__, 1, 2)));
fitzhardinge39de4b42003-10-31 07:12:21 +00001069__attribute__((weak))
1070int
fitzhardingea09a1b52003-11-07 23:09:48 +00001071VALGRIND_PRINTF_BACKTRACE(const char *format, ...)
fitzhardinge39de4b42003-10-31 07:12:21 +00001072{
njnc6168192004-11-29 13:54:10 +00001073 unsigned long _qzz_res;
fitzhardinge39de4b42003-10-31 07:12:21 +00001074 va_list vargs;
1075 va_start(vargs, format);
sewardj0ec07f32006-01-12 12:32:32 +00001076 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, VG_USERREQ__PRINTF_BACKTRACE,
sewardj9af10a12006-02-01 14:59:42 +00001077 (unsigned long)format, (unsigned long)vargs,
1078 0, 0, 0);
fitzhardinge39de4b42003-10-31 07:12:21 +00001079 va_end(vargs);
njnc6168192004-11-29 13:54:10 +00001080 return (int)_qzz_res;
fitzhardinge39de4b42003-10-31 07:12:21 +00001081}
1082
fitzhardinge39de4b42003-10-31 07:12:21 +00001083#endif /* NVALGRIND */
sewardj18d75132002-05-16 11:06:21 +00001084
sewardj0ec07f32006-01-12 12:32:32 +00001085
njn3e884182003-04-15 13:03:23 +00001086/* These requests allow control to move from the simulated CPU to the
1087 real CPU, calling an arbitary function */
sewardj0ec07f32006-01-12 12:32:32 +00001088#define VALGRIND_NON_SIMD_CALL0(_qyy_fn) \
1089 ({unsigned long _qyy_res; \
1090 VALGRIND_DO_CLIENT_REQUEST(_qyy_res, 0 /* default return */, \
1091 VG_USERREQ__CLIENT_CALL0, \
1092 _qyy_fn, \
sewardj9af10a12006-02-01 14:59:42 +00001093 0, 0, 0, 0); \
sewardj0ec07f32006-01-12 12:32:32 +00001094 _qyy_res; \
njn3e884182003-04-15 13:03:23 +00001095 })
1096
sewardj0ec07f32006-01-12 12:32:32 +00001097#define VALGRIND_NON_SIMD_CALL1(_qyy_fn, _qyy_arg1) \
1098 ({unsigned long _qyy_res; \
1099 VALGRIND_DO_CLIENT_REQUEST(_qyy_res, 0 /* default return */, \
1100 VG_USERREQ__CLIENT_CALL1, \
1101 _qyy_fn, \
sewardj9af10a12006-02-01 14:59:42 +00001102 _qyy_arg1, 0, 0, 0); \
sewardj0ec07f32006-01-12 12:32:32 +00001103 _qyy_res; \
njn3e884182003-04-15 13:03:23 +00001104 })
1105
sewardj0ec07f32006-01-12 12:32:32 +00001106#define VALGRIND_NON_SIMD_CALL2(_qyy_fn, _qyy_arg1, _qyy_arg2) \
1107 ({unsigned long _qyy_res; \
1108 VALGRIND_DO_CLIENT_REQUEST(_qyy_res, 0 /* default return */, \
1109 VG_USERREQ__CLIENT_CALL2, \
1110 _qyy_fn, \
sewardj9af10a12006-02-01 14:59:42 +00001111 _qyy_arg1, _qyy_arg2, 0, 0); \
sewardj0ec07f32006-01-12 12:32:32 +00001112 _qyy_res; \
njn3e884182003-04-15 13:03:23 +00001113 })
1114
sewardj0ec07f32006-01-12 12:32:32 +00001115#define VALGRIND_NON_SIMD_CALL3(_qyy_fn, _qyy_arg1, _qyy_arg2, _qyy_arg3) \
1116 ({unsigned long _qyy_res; \
1117 VALGRIND_DO_CLIENT_REQUEST(_qyy_res, 0 /* default return */, \
1118 VG_USERREQ__CLIENT_CALL3, \
1119 _qyy_fn, \
sewardj9af10a12006-02-01 14:59:42 +00001120 _qyy_arg1, _qyy_arg2, \
1121 _qyy_arg3, 0); \
sewardj0ec07f32006-01-12 12:32:32 +00001122 _qyy_res; \
njn3e884182003-04-15 13:03:23 +00001123 })
1124
1125
nethercote7cc9c232004-01-21 15:08:04 +00001126/* Counts the number of errors that have been recorded by a tool. Nb:
1127 the tool must record the errors with VG_(maybe_record_error)() or
njn47363ab2003-04-21 13:24:40 +00001128 VG_(unique_error)() for them to be counted. */
sewardj0ec07f32006-01-12 12:32:32 +00001129#define VALGRIND_COUNT_ERRORS \
1130 ({unsigned int _qyy_res; \
1131 VALGRIND_DO_CLIENT_REQUEST(_qyy_res, 0 /* default return */, \
1132 VG_USERREQ__COUNT_ERRORS, \
sewardj9af10a12006-02-01 14:59:42 +00001133 0, 0, 0, 0, 0); \
sewardj0ec07f32006-01-12 12:32:32 +00001134 _qyy_res; \
njn47363ab2003-04-21 13:24:40 +00001135 })
1136
njnd7994182003-10-02 13:44:04 +00001137/* Mark a block of memory as having been allocated by a malloc()-like
1138 function. `addr' is the start of the usable block (ie. after any
1139 redzone) `rzB' is redzone size if the allocator can apply redzones;
1140 use '0' if not. Adding redzones makes it more likely Valgrind will spot
1141 block overruns. `is_zeroed' indicates if the memory is zeroed, as it is
1142 for calloc(). Put it immediately after the point where a block is
1143 allocated.
1144
1145 If you're allocating memory via superblocks, and then handing out small
1146 chunks of each superblock, if you don't have redzones on your small
1147 blocks, it's worth marking the superblock with VALGRIND_MAKE_NOACCESS
1148 when it's created, so that block overruns are detected. But if you can
1149 put redzones on, it's probably better to not do this, so that messages
1150 for small overruns are described in terms of the small block rather than
1151 the superblock (but if you have a big overrun that skips over a redzone,
1152 you could miss an error this way). See memcheck/tests/custom_alloc.c
1153 for an example.
1154
1155 Nb: block must be freed via a free()-like function specified
1156 with VALGRIND_FREELIKE_BLOCK or mismatch errors will occur. */
sewardj0ec07f32006-01-12 12:32:32 +00001157#define VALGRIND_MALLOCLIKE_BLOCK(addr, sizeB, rzB, is_zeroed) \
1158 {unsigned int _qzz_res; \
1159 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
1160 VG_USERREQ__MALLOCLIKE_BLOCK, \
sewardj9af10a12006-02-01 14:59:42 +00001161 addr, sizeB, rzB, is_zeroed, 0); \
njnd7994182003-10-02 13:44:04 +00001162 }
1163
1164/* Mark a block of memory as having been freed by a free()-like function.
1165 `rzB' is redzone size; it must match that given to
1166 VALGRIND_MALLOCLIKE_BLOCK. Memory not freed will be detected by the leak
1167 checker. Put it immediately after the point where the block is freed. */
sewardj0ec07f32006-01-12 12:32:32 +00001168#define VALGRIND_FREELIKE_BLOCK(addr, rzB) \
1169 {unsigned int _qzz_res; \
1170 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
1171 VG_USERREQ__FREELIKE_BLOCK, \
sewardj9af10a12006-02-01 14:59:42 +00001172 addr, rzB, 0, 0, 0); \
njnd7994182003-10-02 13:44:04 +00001173 }
1174
rjwalshbc0bb832004-06-19 18:12:36 +00001175/* Create a memory pool. */
sewardj0ec07f32006-01-12 12:32:32 +00001176#define VALGRIND_CREATE_MEMPOOL(pool, rzB, is_zeroed) \
1177 {unsigned int _qzz_res; \
1178 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
1179 VG_USERREQ__CREATE_MEMPOOL, \
sewardj9af10a12006-02-01 14:59:42 +00001180 pool, rzB, is_zeroed, 0, 0); \
rjwalshbc0bb832004-06-19 18:12:36 +00001181 }
1182
1183/* Destroy a memory pool. */
sewardj0ec07f32006-01-12 12:32:32 +00001184#define VALGRIND_DESTROY_MEMPOOL(pool) \
1185 {unsigned int _qzz_res; \
1186 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
1187 VG_USERREQ__DESTROY_MEMPOOL, \
sewardj9af10a12006-02-01 14:59:42 +00001188 pool, 0, 0, 0, 0); \
rjwalshbc0bb832004-06-19 18:12:36 +00001189 }
1190
1191/* Associate a piece of memory with a memory pool. */
sewardj0ec07f32006-01-12 12:32:32 +00001192#define VALGRIND_MEMPOOL_ALLOC(pool, addr, size) \
1193 {unsigned int _qzz_res; \
1194 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
1195 VG_USERREQ__MEMPOOL_ALLOC, \
sewardj9af10a12006-02-01 14:59:42 +00001196 pool, addr, size, 0, 0); \
rjwalshbc0bb832004-06-19 18:12:36 +00001197 }
1198
1199/* Disassociate a piece of memory from a memory pool. */
sewardj0ec07f32006-01-12 12:32:32 +00001200#define VALGRIND_MEMPOOL_FREE(pool, addr) \
1201 {unsigned int _qzz_res; \
1202 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
1203 VG_USERREQ__MEMPOOL_FREE, \
sewardj9af10a12006-02-01 14:59:42 +00001204 pool, addr, 0, 0, 0); \
rjwalshbc0bb832004-06-19 18:12:36 +00001205 }
1206
rjwalsh0140af52005-06-04 20:42:33 +00001207/* Mark a piece of memory as being a stack. Returns a stack id. */
sewardj0ec07f32006-01-12 12:32:32 +00001208#define VALGRIND_STACK_REGISTER(start, end) \
1209 ({unsigned int _qzz_res; \
1210 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
1211 VG_USERREQ__STACK_REGISTER, \
sewardj9af10a12006-02-01 14:59:42 +00001212 start, end, 0, 0, 0); \
sewardj0ec07f32006-01-12 12:32:32 +00001213 _qzz_res; \
rjwalsh0140af52005-06-04 20:42:33 +00001214 })
1215
1216/* Unmark the piece of memory associated with a stack id as being a
1217 stack. */
sewardj0ec07f32006-01-12 12:32:32 +00001218#define VALGRIND_STACK_DEREGISTER(id) \
1219 {unsigned int _qzz_res; \
1220 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
1221 VG_USERREQ__STACK_DEREGISTER, \
sewardj9af10a12006-02-01 14:59:42 +00001222 id, 0, 0, 0, 0); \
rjwalsh0140af52005-06-04 20:42:33 +00001223 }
1224
1225/* Change the start and end address of the stack id. */
sewardj0ec07f32006-01-12 12:32:32 +00001226#define VALGRIND_STACK_CHANGE(id, start, end) \
1227 {unsigned int _qzz_res; \
1228 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
1229 VG_USERREQ__STACK_CHANGE, \
sewardj9af10a12006-02-01 14:59:42 +00001230 id, start, end, 0, 0); \
rjwalsh0140af52005-06-04 20:42:33 +00001231 }
1232
sewardj0ec07f32006-01-12 12:32:32 +00001233
1234#undef ARCH_x86
1235#undef ARCH_amd64
1236#undef ARCH_ppc32
1237#undef ARCH_ppc64
1238
njn3e884182003-04-15 13:03:23 +00001239#endif /* __VALGRIND_H */