blob: a2707702498d566d4dfa5006bbc8f6eccede3cd5 [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
62
sewardj6591dc22003-03-15 19:12:43 +000063#ifndef __VALGRIND_SOMESKIN_H
sewardj34a257c2003-03-15 19:20:52 +000064 #warning For valgrind versions 1.9.0 and after,
65 #warning you should not include valgrind.h directly.
66 #warning Instead include the .h relevant to the skin
67 #warning you want to use. For most people this means
68 #warning you need to include memcheck.h instead of
69 #warning valgrind.h.
70 #error Compilation of your source will now abort.
sewardj6591dc22003-03-15 19:12:43 +000071#endif
72
73
sewardjde4a1d02002-03-22 01:27:54 +000074/* This file is for inclusion into client (your!) code.
75
njn25e49d8e72002-09-23 09:36:25 +000076 You can use these macros to manipulate and query Valgrind's
77 execution inside your own programs.
sewardjde4a1d02002-03-22 01:27:54 +000078
79 The resulting executables will still run without Valgrind, just a
80 little bit more slowly than they otherwise would, but otherwise
sewardj285f77f2003-03-15 23:39:11 +000081 unchanged. When not running on valgrind, each client request
82 consumes about 9 x86 instructions, so the resulting performance
83 loss is negligible unless you plan to execute client requests
84 millions of times per second. Nevertheless, if that is still a
85 problem, you can compile with the NVALGRIND symbol defined (gcc
86 -DNVALGRIND) so that client requests are not even compiled in. */
sewardjde4a1d02002-03-22 01:27:54 +000087
88
89
sewardj37091fb2002-11-16 11:06:50 +000090#ifndef NVALGRIND
sewardjde4a1d02002-03-22 01:27:54 +000091/* This defines the magic code sequence which the JITter spots and
92 handles magically. Don't look too closely at this; it will rot
sewardj2e93c502002-04-12 11:12:52 +000093 your brain. Valgrind dumps the result value in %EDX, so we first
94 copy the default value there, so that it is returned when not
95 running on Valgrind. Since %EAX points to a block of mem
96 containing the args, you can pass as many args as you want like
97 this. Currently this is set up to deal with 4 args since that's
98 the max that we appear to need (pthread_create).
sewardjde4a1d02002-03-22 01:27:54 +000099*/
sewardj2e93c502002-04-12 11:12:52 +0000100#define VALGRIND_MAGIC_SEQUENCE( \
101 _zzq_rlval, /* result lvalue */ \
102 _zzq_default, /* result returned when running on real CPU */ \
103 _zzq_request, /* request code */ \
104 _zzq_arg1, /* request first param */ \
105 _zzq_arg2, /* request second param */ \
106 _zzq_arg3, /* request third param */ \
107 _zzq_arg4 /* request fourth param */ ) \
108 \
109 { volatile unsigned int _zzq_args[5]; \
sewardj18d75132002-05-16 11:06:21 +0000110 _zzq_args[0] = (volatile unsigned int)(_zzq_request); \
111 _zzq_args[1] = (volatile unsigned int)(_zzq_arg1); \
112 _zzq_args[2] = (volatile unsigned int)(_zzq_arg2); \
113 _zzq_args[3] = (volatile unsigned int)(_zzq_arg3); \
114 _zzq_args[4] = (volatile unsigned int)(_zzq_arg4); \
sewardj2e93c502002-04-12 11:12:52 +0000115 asm volatile("movl %1, %%eax\n\t" \
116 "movl %2, %%edx\n\t" \
117 "roll $29, %%eax ; roll $3, %%eax\n\t" \
118 "rorl $27, %%eax ; rorl $5, %%eax\n\t" \
119 "roll $13, %%eax ; roll $19, %%eax\n\t" \
120 "movl %%edx, %0\t" \
121 : "=r" (_zzq_rlval) \
122 : "r" (&_zzq_args[0]), "r" (_zzq_default) \
123 : "eax", "edx", "cc", "memory" \
124 ); \
125 }
sewardj37091fb2002-11-16 11:06:50 +0000126#else /* NVALGRIND */
127/* Define NVALGRIND to completely remove the Valgrind magic sequence
128 from the compiled code (analogous to NDEBUG's effects on
129 assert()) */
130#define VALGRIND_MAGIC_SEQUENCE( \
131 _zzq_rlval, /* result lvalue */ \
132 _zzq_default, /* result returned when running on real CPU */ \
133 _zzq_request, /* request code */ \
134 _zzq_arg1, /* request first param */ \
135 _zzq_arg2, /* request second param */ \
136 _zzq_arg3, /* request third param */ \
137 _zzq_arg4 /* request fourth param */ ) \
138 { \
139 (_zzq_rlval) = (_zzq_default); \
140 }
141#endif /* NVALGRIND */
sewardj2e93c502002-04-12 11:12:52 +0000142
143/* Some request codes. There are many more of these, but most are not
144 exposed to end-user view. These are the public ones, all of the
njn25e49d8e72002-09-23 09:36:25 +0000145 form 0x1000 + small_number.
sewardj2e93c502002-04-12 11:12:52 +0000146*/
147
njn4c791212003-05-02 17:53:54 +0000148#define VG_USERREQ_SKIN_BASE(a,b) \
149 ((unsigned int)(((a)&0xff) << 24 | ((b)&0xff) << 16))
150#define VG_IS_SKIN_USERREQ(a, b, v) \
151 (VG_USERREQ_SKIN_BASE(a,b) == ((v) & 0xffff0000))
sewardj34042512002-10-22 04:14:35 +0000152
njn25e49d8e72002-09-23 09:36:25 +0000153typedef
njn4c791212003-05-02 17:53:54 +0000154 enum { VG_USERREQ__RUNNING_ON_VALGRIND = 0x1001,
155 VG_USERREQ__DISCARD_TRANSLATIONS = 0x1002,
njn3e884182003-04-15 13:03:23 +0000156
157 /* These allow any function of 0--3 args to be called from the
158 simulated CPU but run on the real CPU */
njn4c791212003-05-02 17:53:54 +0000159 VG_USERREQ__CLIENT_CALL0 = 0x1101,
160 VG_USERREQ__CLIENT_CALL1 = 0x1102,
161 VG_USERREQ__CLIENT_CALL2 = 0x1103,
162 VG_USERREQ__CLIENT_CALL3 = 0x1104,
njn3e884182003-04-15 13:03:23 +0000163
njn47363ab2003-04-21 13:24:40 +0000164 /* Can be useful in regression testing suites -- eg. can send
165 Valgrind's output to /dev/null and still count errors. */
njn4c791212003-05-02 17:53:54 +0000166 VG_USERREQ__COUNT_ERRORS = 0x1201,
njn47363ab2003-04-21 13:24:40 +0000167
sewardj34a257c2003-03-15 19:20:52 +0000168 VG_USERREQ__FINAL_DUMMY_CLIENT_REQUEST
njn25e49d8e72002-09-23 09:36:25 +0000169 } Vg_ClientRequest;
sewardj2e93c502002-04-12 11:12:52 +0000170
171
172/* Returns 1 if running on Valgrind, 0 if running on the real CPU.
173 Currently implemented but untested. */
174#define RUNNING_ON_VALGRIND \
175 ({unsigned int _qzz_res; \
176 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0 /* returned if not */, \
177 VG_USERREQ__RUNNING_ON_VALGRIND, \
178 0, 0, 0, 0); \
179 _qzz_res; \
sewardjde4a1d02002-03-22 01:27:54 +0000180 })
181
182
sewardj18d75132002-05-16 11:06:21 +0000183/* Discard translation of code in the range [_qzz_addr .. _qzz_addr +
184 _qzz_len - 1]. Useful if you are debugging a JITter or some such,
185 since it provides a way to make sure valgrind will retranslate the
186 invalidated area. Returns no value. */
187#define VALGRIND_DISCARD_TRANSLATIONS(_qzz_addr,_qzz_len) \
188 {unsigned int _qzz_res; \
189 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
190 VG_USERREQ__DISCARD_TRANSLATIONS, \
191 _qzz_addr, _qzz_len, 0, 0); \
192 }
193
194
njn3e884182003-04-15 13:03:23 +0000195/* These requests allow control to move from the simulated CPU to the
196 real CPU, calling an arbitary function */
njn057c65f2003-04-21 13:30:55 +0000197#define VALGRIND_NON_SIMD_CALL0(_qyy_fn) \
njn3e884182003-04-15 13:03:23 +0000198 ({unsigned int _qyy_res; \
199 VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
200 VG_USERREQ__CLIENT_CALL0, \
201 _qyy_fn, \
202 0, 0, 0); \
203 _qyy_res; \
204 })
205
njn057c65f2003-04-21 13:30:55 +0000206#define VALGRIND_NON_SIMD_CALL1(_qyy_fn, _qyy_arg1) \
njn3e884182003-04-15 13:03:23 +0000207 ({unsigned int _qyy_res; \
208 VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
209 VG_USERREQ__CLIENT_CALL1, \
210 _qyy_fn, \
211 _qyy_arg1, 0, 0); \
212 _qyy_res; \
213 })
214
njn057c65f2003-04-21 13:30:55 +0000215#define VALGRIND_NON_SIMD_CALL2(_qyy_fn, _qyy_arg1, _qyy_arg2) \
njn3e884182003-04-15 13:03:23 +0000216 ({unsigned int _qyy_res; \
217 VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
218 VG_USERREQ__CLIENT_CALL2, \
219 _qyy_fn, \
220 _qyy_arg1, _qyy_arg2, 0); \
221 _qyy_res; \
222 })
223
njn057c65f2003-04-21 13:30:55 +0000224#define VALGRIND_NON_SIMD_CALL3(_qyy_fn, _qyy_arg1, _qyy_arg2, _qyy_arg3) \
njn3e884182003-04-15 13:03:23 +0000225 ({unsigned int _qyy_res; \
226 VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
227 VG_USERREQ__CLIENT_CALL3, \
228 _qyy_fn, \
229 _qyy_arg1, _qyy_arg2, _qyy_arg3); \
230 _qyy_res; \
231 })
232
233
234/* These requests are similar to those above; they insert the current
235 ThreadState as the first argument to the called function. */
njn057c65f2003-04-21 13:30:55 +0000236#define VALGRIND_NON_SIMD_tstCALL0(_qyy_fn) \
njn3e884182003-04-15 13:03:23 +0000237 ({unsigned int _qyy_res; \
238 VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
239 VG_USERREQ__CLIENT_tstCALL0, \
240 _qyy_fn, \
241 0, 0, 0); \
242 _qyy_res; \
243 })
244
njn057c65f2003-04-21 13:30:55 +0000245#define VALGRIND_NON_SIMD_tstCALL1(_qyy_fn, _qyy_arg1) \
njn3e884182003-04-15 13:03:23 +0000246 ({unsigned int _qyy_res; \
247 VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
248 VG_USERREQ__CLIENT_tstCALL1, \
249 _qyy_fn, \
250 _qyy_arg1, 0, 0); \
251 _qyy_res; \
252 })
253
njn057c65f2003-04-21 13:30:55 +0000254#define VALGRIND_NON_SIMD_tstCALL2(_qyy_fn, _qyy_arg1, _qyy_arg2) \
njn3e884182003-04-15 13:03:23 +0000255 ({unsigned int _qyy_res; \
256 VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
257 VG_USERREQ__CLIENT_tstCALL2, \
258 _qyy_fn, \
259 _qyy_arg1, _qyy_arg2, 0); \
260 _qyy_res; \
261 })
262
njn057c65f2003-04-21 13:30:55 +0000263#define VALGRIND_NON_SIMD_tstCALL3(_qyy_fn, _qyy_arg1, _qyy_arg2, _qyy_arg3) \
njn3e884182003-04-15 13:03:23 +0000264 ({unsigned int _qyy_res; \
265 VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
266 VG_USERREQ__CLIENT_tstCALL3, \
267 _qyy_fn, \
268 _qyy_arg1, _qyy_arg2, _qyy_arg3); \
269 _qyy_res; \
270 })
271
272
njn47363ab2003-04-21 13:24:40 +0000273/* Counts the number of errors that have been recorded by a skin. Nb:
274 the skin must record the errors with VG_(maybe_record_error)() or
275 VG_(unique_error)() for them to be counted. */
276#define VALGRIND_COUNT_ERRORS \
277 ({unsigned int _qyy_res; \
278 VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
279 VG_USERREQ__COUNT_ERRORS, \
280 0, 0, 0, 0); \
281 _qyy_res; \
282 })
283
njn3e884182003-04-15 13:03:23 +0000284#endif /* __VALGRIND_H */