sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3 | ---------------------------------------------------------------- |
| 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 | |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 12 | This file is part of Valgrind, an extensible x86 protected-mode |
| 13 | emulator for monitoring program execution on x86-Unixes. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 14 | |
njn | 0e1b514 | 2003-04-15 14:58:06 +0000 | [diff] [blame] | 15 | Copyright (C) 2000-2003 Julian Seward. All rights reserved. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 16 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 17 | Redistribution and use in source and binary forms, with or without |
| 18 | modification, are permitted provided that the following conditions |
| 19 | are met: |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 20 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 21 | 1. Redistributions of source code must retain the above copyright |
| 22 | notice, this list of conditions and the following disclaimer. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 23 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 24 | 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. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 28 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 29 | 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 | ---------------------------------------------------------------- |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 56 | */ |
| 57 | |
| 58 | |
| 59 | #ifndef __VALGRIND_H |
| 60 | #define __VALGRIND_H |
| 61 | |
| 62 | |
| 63 | /* This file is for inclusion into client (your!) code. |
| 64 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 65 | You can use these macros to manipulate and query Valgrind's |
| 66 | execution inside your own programs. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 67 | |
| 68 | The resulting executables will still run without Valgrind, just a |
| 69 | little bit more slowly than they otherwise would, but otherwise |
sewardj | 285f77f | 2003-03-15 23:39:11 +0000 | [diff] [blame] | 70 | unchanged. When not running on valgrind, each client request |
| 71 | consumes about 9 x86 instructions, so the resulting performance |
| 72 | loss is negligible unless you plan to execute client requests |
| 73 | millions of times per second. Nevertheless, if that is still a |
| 74 | problem, you can compile with the NVALGRIND symbol defined (gcc |
| 75 | -DNVALGRIND) so that client requests are not even compiled in. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 76 | |
| 77 | |
| 78 | |
sewardj | 37091fb | 2002-11-16 11:06:50 +0000 | [diff] [blame] | 79 | #ifndef NVALGRIND |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 80 | /* This defines the magic code sequence which the JITter spots and |
| 81 | handles magically. Don't look too closely at this; it will rot |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 82 | your brain. Valgrind dumps the result value in %EDX, so we first |
| 83 | copy the default value there, so that it is returned when not |
| 84 | running on Valgrind. Since %EAX points to a block of mem |
| 85 | containing the args, you can pass as many args as you want like |
| 86 | this. Currently this is set up to deal with 4 args since that's |
| 87 | the max that we appear to need (pthread_create). |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 88 | */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 89 | #define VALGRIND_MAGIC_SEQUENCE( \ |
| 90 | _zzq_rlval, /* result lvalue */ \ |
| 91 | _zzq_default, /* result returned when running on real CPU */ \ |
| 92 | _zzq_request, /* request code */ \ |
| 93 | _zzq_arg1, /* request first param */ \ |
| 94 | _zzq_arg2, /* request second param */ \ |
| 95 | _zzq_arg3, /* request third param */ \ |
| 96 | _zzq_arg4 /* request fourth param */ ) \ |
| 97 | \ |
| 98 | { volatile unsigned int _zzq_args[5]; \ |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 99 | _zzq_args[0] = (volatile unsigned int)(_zzq_request); \ |
| 100 | _zzq_args[1] = (volatile unsigned int)(_zzq_arg1); \ |
| 101 | _zzq_args[2] = (volatile unsigned int)(_zzq_arg2); \ |
| 102 | _zzq_args[3] = (volatile unsigned int)(_zzq_arg3); \ |
| 103 | _zzq_args[4] = (volatile unsigned int)(_zzq_arg4); \ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 104 | asm volatile("movl %1, %%eax\n\t" \ |
| 105 | "movl %2, %%edx\n\t" \ |
| 106 | "roll $29, %%eax ; roll $3, %%eax\n\t" \ |
| 107 | "rorl $27, %%eax ; rorl $5, %%eax\n\t" \ |
| 108 | "roll $13, %%eax ; roll $19, %%eax\n\t" \ |
| 109 | "movl %%edx, %0\t" \ |
| 110 | : "=r" (_zzq_rlval) \ |
| 111 | : "r" (&_zzq_args[0]), "r" (_zzq_default) \ |
| 112 | : "eax", "edx", "cc", "memory" \ |
| 113 | ); \ |
| 114 | } |
sewardj | 37091fb | 2002-11-16 11:06:50 +0000 | [diff] [blame] | 115 | #else /* NVALGRIND */ |
| 116 | /* Define NVALGRIND to completely remove the Valgrind magic sequence |
| 117 | from the compiled code (analogous to NDEBUG's effects on |
| 118 | assert()) */ |
| 119 | #define VALGRIND_MAGIC_SEQUENCE( \ |
| 120 | _zzq_rlval, /* result lvalue */ \ |
| 121 | _zzq_default, /* result returned when running on real CPU */ \ |
| 122 | _zzq_request, /* request code */ \ |
| 123 | _zzq_arg1, /* request first param */ \ |
| 124 | _zzq_arg2, /* request second param */ \ |
| 125 | _zzq_arg3, /* request third param */ \ |
| 126 | _zzq_arg4 /* request fourth param */ ) \ |
| 127 | { \ |
| 128 | (_zzq_rlval) = (_zzq_default); \ |
| 129 | } |
| 130 | #endif /* NVALGRIND */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 131 | |
| 132 | /* Some request codes. There are many more of these, but most are not |
| 133 | exposed to end-user view. These are the public ones, all of the |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 134 | form 0x1000 + small_number. |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame^] | 135 | |
| 136 | Core ones are in the range 0x00000000--0x0000ffff. The non-public ones |
| 137 | start at 0x2000. |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 138 | */ |
| 139 | |
njn | 4c79121 | 2003-05-02 17:53:54 +0000 | [diff] [blame] | 140 | #define VG_USERREQ_SKIN_BASE(a,b) \ |
| 141 | ((unsigned int)(((a)&0xff) << 24 | ((b)&0xff) << 16)) |
| 142 | #define VG_IS_SKIN_USERREQ(a, b, v) \ |
| 143 | (VG_USERREQ_SKIN_BASE(a,b) == ((v) & 0xffff0000)) |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 144 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 145 | typedef |
njn | 4c79121 | 2003-05-02 17:53:54 +0000 | [diff] [blame] | 146 | enum { VG_USERREQ__RUNNING_ON_VALGRIND = 0x1001, |
| 147 | VG_USERREQ__DISCARD_TRANSLATIONS = 0x1002, |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 148 | |
| 149 | /* These allow any function of 0--3 args to be called from the |
| 150 | simulated CPU but run on the real CPU */ |
njn | 4c79121 | 2003-05-02 17:53:54 +0000 | [diff] [blame] | 151 | VG_USERREQ__CLIENT_CALL0 = 0x1101, |
| 152 | VG_USERREQ__CLIENT_CALL1 = 0x1102, |
| 153 | VG_USERREQ__CLIENT_CALL2 = 0x1103, |
| 154 | VG_USERREQ__CLIENT_CALL3 = 0x1104, |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 155 | |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 156 | /* Can be useful in regression testing suites -- eg. can send |
| 157 | Valgrind's output to /dev/null and still count errors. */ |
njn | 4c79121 | 2003-05-02 17:53:54 +0000 | [diff] [blame] | 158 | VG_USERREQ__COUNT_ERRORS = 0x1201, |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 159 | |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame^] | 160 | /* These are useful and can be interpreted by any skin that tracks |
| 161 | malloc() et al, by using vg_replace_malloc.c. */ |
| 162 | VG_USERREQ__MALLOCLIKE_BLOCK = 0x1301, |
| 163 | VG_USERREQ__FREELIKE_BLOCK = 0x1302, |
| 164 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 165 | } Vg_ClientRequest; |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 166 | |
| 167 | |
| 168 | /* Returns 1 if running on Valgrind, 0 if running on the real CPU. |
| 169 | Currently implemented but untested. */ |
| 170 | #define RUNNING_ON_VALGRIND \ |
| 171 | ({unsigned int _qzz_res; \ |
| 172 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0 /* returned if not */, \ |
| 173 | VG_USERREQ__RUNNING_ON_VALGRIND, \ |
| 174 | 0, 0, 0, 0); \ |
| 175 | _qzz_res; \ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 176 | }) |
| 177 | |
| 178 | |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 179 | /* Discard translation of code in the range [_qzz_addr .. _qzz_addr + |
| 180 | _qzz_len - 1]. Useful if you are debugging a JITter or some such, |
| 181 | since it provides a way to make sure valgrind will retranslate the |
| 182 | invalidated area. Returns no value. */ |
| 183 | #define VALGRIND_DISCARD_TRANSLATIONS(_qzz_addr,_qzz_len) \ |
| 184 | {unsigned int _qzz_res; \ |
| 185 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \ |
| 186 | VG_USERREQ__DISCARD_TRANSLATIONS, \ |
| 187 | _qzz_addr, _qzz_len, 0, 0); \ |
| 188 | } |
| 189 | |
| 190 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 191 | /* These requests allow control to move from the simulated CPU to the |
| 192 | real CPU, calling an arbitary function */ |
njn | 057c65f | 2003-04-21 13:30:55 +0000 | [diff] [blame] | 193 | #define VALGRIND_NON_SIMD_CALL0(_qyy_fn) \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 194 | ({unsigned int _qyy_res; \ |
| 195 | VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \ |
| 196 | VG_USERREQ__CLIENT_CALL0, \ |
| 197 | _qyy_fn, \ |
| 198 | 0, 0, 0); \ |
| 199 | _qyy_res; \ |
| 200 | }) |
| 201 | |
njn | 057c65f | 2003-04-21 13:30:55 +0000 | [diff] [blame] | 202 | #define VALGRIND_NON_SIMD_CALL1(_qyy_fn, _qyy_arg1) \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 203 | ({unsigned int _qyy_res; \ |
| 204 | VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \ |
| 205 | VG_USERREQ__CLIENT_CALL1, \ |
| 206 | _qyy_fn, \ |
| 207 | _qyy_arg1, 0, 0); \ |
| 208 | _qyy_res; \ |
| 209 | }) |
| 210 | |
njn | 057c65f | 2003-04-21 13:30:55 +0000 | [diff] [blame] | 211 | #define VALGRIND_NON_SIMD_CALL2(_qyy_fn, _qyy_arg1, _qyy_arg2) \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 212 | ({unsigned int _qyy_res; \ |
| 213 | VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \ |
| 214 | VG_USERREQ__CLIENT_CALL2, \ |
| 215 | _qyy_fn, \ |
| 216 | _qyy_arg1, _qyy_arg2, 0); \ |
| 217 | _qyy_res; \ |
| 218 | }) |
| 219 | |
njn | 057c65f | 2003-04-21 13:30:55 +0000 | [diff] [blame] | 220 | #define VALGRIND_NON_SIMD_CALL3(_qyy_fn, _qyy_arg1, _qyy_arg2, _qyy_arg3) \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 221 | ({unsigned int _qyy_res; \ |
| 222 | VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \ |
| 223 | VG_USERREQ__CLIENT_CALL3, \ |
| 224 | _qyy_fn, \ |
| 225 | _qyy_arg1, _qyy_arg2, _qyy_arg3); \ |
| 226 | _qyy_res; \ |
| 227 | }) |
| 228 | |
| 229 | |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 230 | /* Counts the number of errors that have been recorded by a skin. Nb: |
| 231 | the skin must record the errors with VG_(maybe_record_error)() or |
| 232 | VG_(unique_error)() for them to be counted. */ |
| 233 | #define VALGRIND_COUNT_ERRORS \ |
| 234 | ({unsigned int _qyy_res; \ |
| 235 | VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \ |
| 236 | VG_USERREQ__COUNT_ERRORS, \ |
| 237 | 0, 0, 0, 0); \ |
| 238 | _qyy_res; \ |
| 239 | }) |
| 240 | |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame^] | 241 | /* Mark a block of memory as having been allocated by a malloc()-like |
| 242 | function. `addr' is the start of the usable block (ie. after any |
| 243 | redzone) `rzB' is redzone size if the allocator can apply redzones; |
| 244 | use '0' if not. Adding redzones makes it more likely Valgrind will spot |
| 245 | block overruns. `is_zeroed' indicates if the memory is zeroed, as it is |
| 246 | for calloc(). Put it immediately after the point where a block is |
| 247 | allocated. |
| 248 | |
| 249 | If you're allocating memory via superblocks, and then handing out small |
| 250 | chunks of each superblock, if you don't have redzones on your small |
| 251 | blocks, it's worth marking the superblock with VALGRIND_MAKE_NOACCESS |
| 252 | when it's created, so that block overruns are detected. But if you can |
| 253 | put redzones on, it's probably better to not do this, so that messages |
| 254 | for small overruns are described in terms of the small block rather than |
| 255 | the superblock (but if you have a big overrun that skips over a redzone, |
| 256 | you could miss an error this way). See memcheck/tests/custom_alloc.c |
| 257 | for an example. |
| 258 | |
| 259 | Nb: block must be freed via a free()-like function specified |
| 260 | with VALGRIND_FREELIKE_BLOCK or mismatch errors will occur. */ |
| 261 | #define VALGRIND_MALLOCLIKE_BLOCK(addr, sizeB, rzB, is_zeroed) \ |
| 262 | {unsigned int _qzz_res; \ |
| 263 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \ |
| 264 | VG_USERREQ__MALLOCLIKE_BLOCK, \ |
| 265 | addr, sizeB, rzB, is_zeroed); \ |
| 266 | } |
| 267 | |
| 268 | /* Mark a block of memory as having been freed by a free()-like function. |
| 269 | `rzB' is redzone size; it must match that given to |
| 270 | VALGRIND_MALLOCLIKE_BLOCK. Memory not freed will be detected by the leak |
| 271 | checker. Put it immediately after the point where the block is freed. */ |
| 272 | #define VALGRIND_FREELIKE_BLOCK(addr, rzB) \ |
| 273 | {unsigned int _qzz_res; \ |
| 274 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \ |
| 275 | VG_USERREQ__FREELIKE_BLOCK, \ |
| 276 | addr, rzB, 0, 0); \ |
| 277 | } |
| 278 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 279 | #endif /* __VALGRIND_H */ |