sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1 | /* -*- c -*- |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2 | ---------------------------------------------------------------- |
| 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 | |
njn | b9c427c | 2004-12-01 14:14:42 +0000 | [diff] [blame] | 11 | This file is part of Valgrind, a dynamic binary instrumentation |
| 12 | framework. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 13 | |
njn | 5361242 | 2005-03-12 16:22:54 +0000 | [diff] [blame] | 14 | Copyright (C) 2000-2005 Julian Seward. All rights reserved. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 15 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 16 | Redistribution and use in source and binary forms, with or without |
| 17 | modification, are permitted provided that the following conditions |
| 18 | are met: |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 19 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 20 | 1. Redistributions of source code must retain the above copyright |
| 21 | notice, this list of conditions and the following disclaimer. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 22 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 23 | 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. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 27 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 28 | 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 | ---------------------------------------------------------------- |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 55 | */ |
| 56 | |
| 57 | |
njn | 30d76c6 | 2005-06-18 15:07:39 +0000 | [diff] [blame] | 58 | /* 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 |
| 66 | consumes very few (eg. < 10) instructions, so the resulting performance |
| 67 | 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 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 72 | #ifndef __VALGRIND_H |
| 73 | #define __VALGRIND_H |
| 74 | |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 75 | #include <stdarg.h> |
| 76 | |
njn | 3dd0a91 | 2005-06-28 19:44:10 +0000 | [diff] [blame] | 77 | /* 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 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 81 | /* If we're not compiling for our target architecture, don't generate |
njn | 26aba4d | 2005-05-16 13:31:23 +0000 | [diff] [blame] | 82 | any inline asms. Note that in this file we're using the compiler's |
| 83 | CPP symbols for identifying architectures, which are different to |
| 84 | the ones we use within the rest of Valgrind. */ |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 85 | #if !defined(__i386__) && !defined(__x86_64__) && !defined(__powerpc__) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 86 | # ifndef NVALGRIND |
| 87 | # define NVALGRIND 1 |
| 88 | # endif /* NVALGRIND */ |
| 89 | #endif |
| 90 | |
njn | 30d76c6 | 2005-06-18 15:07:39 +0000 | [diff] [blame] | 91 | /* ------------------------------------------------------------------ */ |
| 92 | /* The architecture-specific part */ |
| 93 | /* ------------------------------------------------------------------ */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 94 | |
njn | 26aba4d | 2005-05-16 13:31:23 +0000 | [diff] [blame] | 95 | #ifdef NVALGRIND |
| 96 | |
| 97 | /* Define NVALGRIND to completely remove the Valgrind magic sequence |
| 98 | from the compiled code (analogous to NDEBUG's effects on assert()) */ |
| 99 | #define VALGRIND_MAGIC_SEQUENCE( \ |
| 100 | _zzq_rlval, _zzq_default, _zzq_request, \ |
| 101 | _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4) \ |
| 102 | { \ |
| 103 | (_zzq_rlval) = (_zzq_default); \ |
| 104 | } |
| 105 | |
| 106 | #else /* NVALGRIND */ |
nethercote | e90c683 | 2004-10-18 18:07:49 +0000 | [diff] [blame] | 107 | |
nethercote | 5426544 | 2004-10-26 12:56:58 +0000 | [diff] [blame] | 108 | /* The following defines the magic code sequences which the JITter spots and |
| 109 | handles magically. Don't look too closely at them; they will rot |
nethercote | e90c683 | 2004-10-18 18:07:49 +0000 | [diff] [blame] | 110 | your brain. We must ensure that the default value gets put in the return |
| 111 | slot, so that everything works when this is executed not under Valgrind. |
| 112 | Args are passed in a memory block, and so there's no intrinsic limit to |
| 113 | the number that could be passed, but it's currently four. |
| 114 | |
nethercote | 5426544 | 2004-10-26 12:56:58 +0000 | [diff] [blame] | 115 | The macro args are: |
| 116 | _zzq_rlval result lvalue |
| 117 | _zzq_default default value (result returned when running on real CPU) |
| 118 | _zzq_request request code |
| 119 | _zzq_arg1..4 request params |
| 120 | |
nethercote | e90c683 | 2004-10-18 18:07:49 +0000 | [diff] [blame] | 121 | Nb: we put the assembly code sequences for all architectures in this one |
njn | 26aba4d | 2005-05-16 13:31:23 +0000 | [diff] [blame] | 122 | file. This is because this file must be stand-alone, and we don't want |
| 123 | to have multiple files. |
nethercote | e90c683 | 2004-10-18 18:07:49 +0000 | [diff] [blame] | 124 | */ |
| 125 | |
njn | 26aba4d | 2005-05-16 13:31:23 +0000 | [diff] [blame] | 126 | #ifdef __x86_64__ |
sewardj | de4a4ab | 2005-03-23 13:10:32 +0000 | [diff] [blame] | 127 | extern int printf (__const char *__restrict __format, ...); |
| 128 | extern void exit (int __status); |
| 129 | #define VALGRIND_MAGIC_SEQUENCE( \ |
| 130 | _zzq_rlval, _zzq_default, _zzq_request, \ |
| 131 | _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4) \ |
| 132 | \ |
| 133 | { volatile unsigned long long _zzq_args[5]; \ |
| 134 | _zzq_args[0] = (volatile unsigned long long)(_zzq_request); \ |
| 135 | _zzq_args[1] = (volatile unsigned long long)(_zzq_arg1); \ |
| 136 | _zzq_args[2] = (volatile unsigned long long)(_zzq_arg2); \ |
| 137 | _zzq_args[3] = (volatile unsigned long long)(_zzq_arg3); \ |
| 138 | _zzq_args[4] = (volatile unsigned long long)(_zzq_arg4); \ |
njn | 3dd0a91 | 2005-06-28 19:44:10 +0000 | [diff] [blame] | 139 | __asm__ volatile("roll $29, %%eax ; roll $3, %%eax\n\t" \ |
| 140 | "rorl $27, %%eax ; rorl $5, %%eax\n\t" \ |
| 141 | "roll $13, %%eax ; roll $19, %%eax" \ |
| 142 | : "=d" (_zzq_rlval) \ |
| 143 | : "a" (&_zzq_args[0]), "0" (_zzq_default) \ |
| 144 | : "cc", "memory" \ |
| 145 | ); \ |
sewardj | de4a4ab | 2005-03-23 13:10:32 +0000 | [diff] [blame] | 146 | } |
njn | 3dd0a91 | 2005-06-28 19:44:10 +0000 | [diff] [blame] | 147 | #endif /* __x86_64__ */ |
sewardj | de4a4ab | 2005-03-23 13:10:32 +0000 | [diff] [blame] | 148 | |
| 149 | #ifdef __i386__ |
| 150 | #define VALGRIND_MAGIC_SEQUENCE( \ |
| 151 | _zzq_rlval, _zzq_default, _zzq_request, \ |
| 152 | _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4) \ |
| 153 | \ |
| 154 | { unsigned int _zzq_args[5]; \ |
| 155 | _zzq_args[0] = (unsigned int)(_zzq_request); \ |
| 156 | _zzq_args[1] = (unsigned int)(_zzq_arg1); \ |
| 157 | _zzq_args[2] = (unsigned int)(_zzq_arg2); \ |
| 158 | _zzq_args[3] = (unsigned int)(_zzq_arg3); \ |
| 159 | _zzq_args[4] = (unsigned int)(_zzq_arg4); \ |
njn | 3dd0a91 | 2005-06-28 19:44:10 +0000 | [diff] [blame] | 160 | __asm__ volatile("roll $29, %%eax ; roll $3, %%eax\n\t" \ |
| 161 | "rorl $27, %%eax ; rorl $5, %%eax\n\t" \ |
| 162 | "roll $13, %%eax ; roll $19, %%eax" \ |
| 163 | : "=d" (_zzq_rlval) \ |
| 164 | : "a" (&_zzq_args[0]), "0" (_zzq_default) \ |
| 165 | : "cc", "memory" \ |
| 166 | ); \ |
sewardj | de4a4ab | 2005-03-23 13:10:32 +0000 | [diff] [blame] | 167 | } |
njn | 3dd0a91 | 2005-06-28 19:44:10 +0000 | [diff] [blame] | 168 | #endif /* __i386__ */ |
sewardj | de4a4ab | 2005-03-23 13:10:32 +0000 | [diff] [blame] | 169 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 170 | #ifdef __powerpc__ |
| 171 | #define VALGRIND_MAGIC_SEQUENCE( \ |
| 172 | _zzq_rlval, _zzq_default, _zzq_request, \ |
| 173 | _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4) \ |
| 174 | \ |
| 175 | { volatile unsigned int _zzq_args[5]; \ |
| 176 | register unsigned int _zzq_tmp __asm__("r3"); \ |
| 177 | register volatile unsigned int *_zzq_ptr __asm__("r4"); \ |
| 178 | _zzq_args[0] = (volatile unsigned int)(_zzq_request); \ |
| 179 | _zzq_args[1] = (volatile unsigned int)(_zzq_arg1); \ |
| 180 | _zzq_args[2] = (volatile unsigned int)(_zzq_arg2); \ |
| 181 | _zzq_args[3] = (volatile unsigned int)(_zzq_arg3); \ |
| 182 | _zzq_args[4] = (volatile unsigned int)(_zzq_arg4); \ |
| 183 | _zzq_ptr = _zzq_args; \ |
njn | 3dd0a91 | 2005-06-28 19:44:10 +0000 | [diff] [blame] | 184 | __asm__ volatile("tw 0,3,27\n\t" \ |
| 185 | "rlwinm 0,0,29,0,0\n\t" \ |
| 186 | "rlwinm 0,0,3,0,0\n\t" \ |
| 187 | "rlwinm 0,0,13,0,0\n\t" \ |
| 188 | "rlwinm 0,0,19,0,0\n\t" \ |
| 189 | "nop\n\t" \ |
| 190 | : "=r" (_zzq_tmp) \ |
| 191 | : "0" (_zzq_default), "r" (_zzq_ptr) \ |
| 192 | : "memory"); \ |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 193 | _zzq_rlval = (__typeof__(_zzq_rlval)) _zzq_tmp; \ |
| 194 | } |
njn | 3dd0a91 | 2005-06-28 19:44:10 +0000 | [diff] [blame] | 195 | #endif /* __powerpc__ */ |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 196 | |
njn | 3dd0a91 | 2005-06-28 19:44:10 +0000 | [diff] [blame] | 197 | /* Insert assembly code for other architectures here... */ |
njn | 26aba4d | 2005-05-16 13:31:23 +0000 | [diff] [blame] | 198 | |
sewardj | 37091fb | 2002-11-16 11:06:50 +0000 | [diff] [blame] | 199 | #endif /* NVALGRIND */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 200 | |
nethercote | 69d9c46 | 2004-10-26 13:00:12 +0000 | [diff] [blame] | 201 | |
njn | 30d76c6 | 2005-06-18 15:07:39 +0000 | [diff] [blame] | 202 | /* ------------------------------------------------------------------ */ |
| 203 | /* The architecture-independent part */ |
| 204 | /* ------------------------------------------------------------------ */ |
| 205 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 206 | /* Some request codes. There are many more of these, but most are not |
| 207 | exposed to end-user view. These are the public ones, all of the |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 208 | form 0x1000 + small_number. |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 209 | |
| 210 | Core ones are in the range 0x00000000--0x0000ffff. The non-public ones |
| 211 | start at 0x2000. |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 212 | */ |
| 213 | |
njn | 3dd0a91 | 2005-06-28 19:44:10 +0000 | [diff] [blame] | 214 | /* These macros are used by tools -- they must be public, but don't embed them |
| 215 | * into other programs. */ |
njn | fc26ff9 | 2004-11-22 19:12:49 +0000 | [diff] [blame] | 216 | #define VG_USERREQ_TOOL_BASE(a,b) \ |
njn | 4c79121 | 2003-05-02 17:53:54 +0000 | [diff] [blame] | 217 | ((unsigned int)(((a)&0xff) << 24 | ((b)&0xff) << 16)) |
njn | fc26ff9 | 2004-11-22 19:12:49 +0000 | [diff] [blame] | 218 | #define VG_IS_TOOL_USERREQ(a, b, v) \ |
| 219 | (VG_USERREQ_TOOL_BASE(a,b) == ((v) & 0xffff0000)) |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 220 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 221 | typedef |
njn | 4c79121 | 2003-05-02 17:53:54 +0000 | [diff] [blame] | 222 | enum { VG_USERREQ__RUNNING_ON_VALGRIND = 0x1001, |
| 223 | VG_USERREQ__DISCARD_TRANSLATIONS = 0x1002, |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 224 | |
njn | d4795be | 2004-11-24 11:57:51 +0000 | [diff] [blame] | 225 | /* These allow any function to be called from the |
| 226 | simulated CPU but run on the real CPU. |
| 227 | Nb: the first arg passed to the function is always the ThreadId of |
| 228 | the running thread! So CLIENT_CALL0 actually requires a 1 arg |
| 229 | function, etc. */ |
njn | 4c79121 | 2003-05-02 17:53:54 +0000 | [diff] [blame] | 230 | VG_USERREQ__CLIENT_CALL0 = 0x1101, |
| 231 | VG_USERREQ__CLIENT_CALL1 = 0x1102, |
| 232 | VG_USERREQ__CLIENT_CALL2 = 0x1103, |
| 233 | VG_USERREQ__CLIENT_CALL3 = 0x1104, |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 234 | |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 235 | /* Can be useful in regression testing suites -- eg. can send |
| 236 | Valgrind's output to /dev/null and still count errors. */ |
njn | 4c79121 | 2003-05-02 17:53:54 +0000 | [diff] [blame] | 237 | VG_USERREQ__COUNT_ERRORS = 0x1201, |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 238 | |
nethercote | 7cc9c23 | 2004-01-21 15:08:04 +0000 | [diff] [blame] | 239 | /* These are useful and can be interpreted by any tool that tracks |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 240 | malloc() et al, by using vg_replace_malloc.c. */ |
| 241 | VG_USERREQ__MALLOCLIKE_BLOCK = 0x1301, |
| 242 | VG_USERREQ__FREELIKE_BLOCK = 0x1302, |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 243 | /* Memory pool support. */ |
| 244 | VG_USERREQ__CREATE_MEMPOOL = 0x1303, |
| 245 | VG_USERREQ__DESTROY_MEMPOOL = 0x1304, |
| 246 | VG_USERREQ__MEMPOOL_ALLOC = 0x1305, |
| 247 | VG_USERREQ__MEMPOOL_FREE = 0x1306, |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 248 | |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 249 | /* Allow printfs to valgrind log. */ |
njn | 30d76c6 | 2005-06-18 15:07:39 +0000 | [diff] [blame] | 250 | VG_USERREQ__PRINTF = 0x1401, |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 251 | VG_USERREQ__PRINTF_BACKTRACE = 0x1402, |
| 252 | |
| 253 | /* Stack support. */ |
| 254 | VG_USERREQ__STACK_REGISTER = 0x1501, |
| 255 | VG_USERREQ__STACK_DEREGISTER = 0x1502, |
| 256 | VG_USERREQ__STACK_CHANGE = 0x1503, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 257 | } Vg_ClientRequest; |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 258 | |
mueller | c9b3655 | 2003-12-31 14:32:23 +0000 | [diff] [blame] | 259 | #ifndef __GNUC__ |
| 260 | #define __extension__ |
| 261 | #endif |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 262 | |
njn | f09745a | 2005-05-10 03:01:23 +0000 | [diff] [blame] | 263 | /* Returns the number of Valgrinds this code is running under. That is, |
| 264 | 0 if running natively, 1 if running under Valgrind, 2 if running under |
| 265 | Valgrind which is running under another Valgrind, etc. */ |
mueller | c9b3655 | 2003-12-31 14:32:23 +0000 | [diff] [blame] | 266 | #define RUNNING_ON_VALGRIND __extension__ \ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 267 | ({unsigned int _qzz_res; \ |
| 268 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0 /* returned if not */, \ |
| 269 | VG_USERREQ__RUNNING_ON_VALGRIND, \ |
| 270 | 0, 0, 0, 0); \ |
| 271 | _qzz_res; \ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 272 | }) |
| 273 | |
| 274 | |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 275 | /* Discard translation of code in the range [_qzz_addr .. _qzz_addr + |
| 276 | _qzz_len - 1]. Useful if you are debugging a JITter or some such, |
| 277 | since it provides a way to make sure valgrind will retranslate the |
| 278 | invalidated area. Returns no value. */ |
| 279 | #define VALGRIND_DISCARD_TRANSLATIONS(_qzz_addr,_qzz_len) \ |
| 280 | {unsigned int _qzz_res; \ |
| 281 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \ |
| 282 | VG_USERREQ__DISCARD_TRANSLATIONS, \ |
| 283 | _qzz_addr, _qzz_len, 0, 0); \ |
| 284 | } |
| 285 | |
njn | 26aba4d | 2005-05-16 13:31:23 +0000 | [diff] [blame] | 286 | #ifdef NVALGRIND |
| 287 | |
| 288 | #define VALGRIND_PRINTF(...) |
| 289 | #define VALGRIND_PRINTF_BACKTRACE(...) |
| 290 | |
| 291 | #else /* NVALGRIND */ |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 292 | |
fitzhardinge | a09a1b5 | 2003-11-07 23:09:48 +0000 | [diff] [blame] | 293 | int VALGRIND_PRINTF(const char *format, ...) |
| 294 | __attribute__((format(__printf__, 1, 2))); |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 295 | __attribute__((weak)) |
| 296 | int |
fitzhardinge | a09a1b5 | 2003-11-07 23:09:48 +0000 | [diff] [blame] | 297 | VALGRIND_PRINTF(const char *format, ...) |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 298 | { |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 299 | unsigned long _qzz_res; |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 300 | va_list vargs; |
| 301 | va_start(vargs, format); |
| 302 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, VG_USERREQ__PRINTF, |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 303 | (unsigned long)format, (unsigned long)vargs, 0, 0); |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 304 | va_end(vargs); |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 305 | return (int)_qzz_res; |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 306 | } |
| 307 | |
fitzhardinge | a09a1b5 | 2003-11-07 23:09:48 +0000 | [diff] [blame] | 308 | int VALGRIND_PRINTF_BACKTRACE(const char *format, ...) |
| 309 | __attribute__((format(__printf__, 1, 2))); |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 310 | __attribute__((weak)) |
| 311 | int |
fitzhardinge | a09a1b5 | 2003-11-07 23:09:48 +0000 | [diff] [blame] | 312 | VALGRIND_PRINTF_BACKTRACE(const char *format, ...) |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 313 | { |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 314 | unsigned long _qzz_res; |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 315 | va_list vargs; |
| 316 | va_start(vargs, format); |
| 317 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, VG_USERREQ__PRINTF_BACKTRACE, |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 318 | (unsigned long)format, (unsigned long)vargs, 0, 0); |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 319 | va_end(vargs); |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 320 | return (int)_qzz_res; |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 321 | } |
| 322 | |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 323 | #endif /* NVALGRIND */ |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 324 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 325 | /* These requests allow control to move from the simulated CPU to the |
| 326 | real CPU, calling an arbitary function */ |
njn | 057c65f | 2003-04-21 13:30:55 +0000 | [diff] [blame] | 327 | #define VALGRIND_NON_SIMD_CALL0(_qyy_fn) \ |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 328 | ({unsigned long _qyy_res; \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 329 | VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \ |
| 330 | VG_USERREQ__CLIENT_CALL0, \ |
| 331 | _qyy_fn, \ |
| 332 | 0, 0, 0); \ |
| 333 | _qyy_res; \ |
| 334 | }) |
| 335 | |
njn | 057c65f | 2003-04-21 13:30:55 +0000 | [diff] [blame] | 336 | #define VALGRIND_NON_SIMD_CALL1(_qyy_fn, _qyy_arg1) \ |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 337 | ({unsigned long _qyy_res; \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 338 | VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \ |
| 339 | VG_USERREQ__CLIENT_CALL1, \ |
| 340 | _qyy_fn, \ |
| 341 | _qyy_arg1, 0, 0); \ |
| 342 | _qyy_res; \ |
| 343 | }) |
| 344 | |
njn | 057c65f | 2003-04-21 13:30:55 +0000 | [diff] [blame] | 345 | #define VALGRIND_NON_SIMD_CALL2(_qyy_fn, _qyy_arg1, _qyy_arg2) \ |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 346 | ({unsigned long _qyy_res; \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 347 | VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \ |
| 348 | VG_USERREQ__CLIENT_CALL2, \ |
| 349 | _qyy_fn, \ |
| 350 | _qyy_arg1, _qyy_arg2, 0); \ |
| 351 | _qyy_res; \ |
| 352 | }) |
| 353 | |
njn | 057c65f | 2003-04-21 13:30:55 +0000 | [diff] [blame] | 354 | #define VALGRIND_NON_SIMD_CALL3(_qyy_fn, _qyy_arg1, _qyy_arg2, _qyy_arg3) \ |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 355 | ({unsigned long _qyy_res; \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 356 | VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \ |
| 357 | VG_USERREQ__CLIENT_CALL3, \ |
| 358 | _qyy_fn, \ |
| 359 | _qyy_arg1, _qyy_arg2, _qyy_arg3); \ |
| 360 | _qyy_res; \ |
| 361 | }) |
| 362 | |
| 363 | |
nethercote | 7cc9c23 | 2004-01-21 15:08:04 +0000 | [diff] [blame] | 364 | /* Counts the number of errors that have been recorded by a tool. Nb: |
| 365 | the tool must record the errors with VG_(maybe_record_error)() or |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 366 | VG_(unique_error)() for them to be counted. */ |
| 367 | #define VALGRIND_COUNT_ERRORS \ |
| 368 | ({unsigned int _qyy_res; \ |
| 369 | VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \ |
| 370 | VG_USERREQ__COUNT_ERRORS, \ |
| 371 | 0, 0, 0, 0); \ |
| 372 | _qyy_res; \ |
| 373 | }) |
| 374 | |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 375 | /* Mark a block of memory as having been allocated by a malloc()-like |
| 376 | function. `addr' is the start of the usable block (ie. after any |
| 377 | redzone) `rzB' is redzone size if the allocator can apply redzones; |
| 378 | use '0' if not. Adding redzones makes it more likely Valgrind will spot |
| 379 | block overruns. `is_zeroed' indicates if the memory is zeroed, as it is |
| 380 | for calloc(). Put it immediately after the point where a block is |
| 381 | allocated. |
| 382 | |
| 383 | If you're allocating memory via superblocks, and then handing out small |
| 384 | chunks of each superblock, if you don't have redzones on your small |
| 385 | blocks, it's worth marking the superblock with VALGRIND_MAKE_NOACCESS |
| 386 | when it's created, so that block overruns are detected. But if you can |
| 387 | put redzones on, it's probably better to not do this, so that messages |
| 388 | for small overruns are described in terms of the small block rather than |
| 389 | the superblock (but if you have a big overrun that skips over a redzone, |
| 390 | you could miss an error this way). See memcheck/tests/custom_alloc.c |
| 391 | for an example. |
| 392 | |
| 393 | Nb: block must be freed via a free()-like function specified |
| 394 | with VALGRIND_FREELIKE_BLOCK or mismatch errors will occur. */ |
| 395 | #define VALGRIND_MALLOCLIKE_BLOCK(addr, sizeB, rzB, is_zeroed) \ |
| 396 | {unsigned int _qzz_res; \ |
| 397 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \ |
| 398 | VG_USERREQ__MALLOCLIKE_BLOCK, \ |
| 399 | addr, sizeB, rzB, is_zeroed); \ |
| 400 | } |
| 401 | |
| 402 | /* Mark a block of memory as having been freed by a free()-like function. |
| 403 | `rzB' is redzone size; it must match that given to |
| 404 | VALGRIND_MALLOCLIKE_BLOCK. Memory not freed will be detected by the leak |
| 405 | checker. Put it immediately after the point where the block is freed. */ |
| 406 | #define VALGRIND_FREELIKE_BLOCK(addr, rzB) \ |
| 407 | {unsigned int _qzz_res; \ |
| 408 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \ |
| 409 | VG_USERREQ__FREELIKE_BLOCK, \ |
| 410 | addr, rzB, 0, 0); \ |
| 411 | } |
| 412 | |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 413 | /* Create a memory pool. */ |
| 414 | #define VALGRIND_CREATE_MEMPOOL(pool, rzB, is_zeroed) \ |
| 415 | {unsigned int _qzz_res; \ |
| 416 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \ |
| 417 | VG_USERREQ__CREATE_MEMPOOL, \ |
| 418 | pool, rzB, is_zeroed, 0); \ |
| 419 | } |
| 420 | |
| 421 | /* Destroy a memory pool. */ |
| 422 | #define VALGRIND_DESTROY_MEMPOOL(pool) \ |
| 423 | {unsigned int _qzz_res; \ |
| 424 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \ |
| 425 | VG_USERREQ__DESTROY_MEMPOOL, \ |
| 426 | pool, 0, 0, 0); \ |
| 427 | } |
| 428 | |
| 429 | /* Associate a piece of memory with a memory pool. */ |
| 430 | #define VALGRIND_MEMPOOL_ALLOC(pool, addr, size) \ |
| 431 | {unsigned int _qzz_res; \ |
| 432 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \ |
| 433 | VG_USERREQ__MEMPOOL_ALLOC, \ |
| 434 | pool, addr, size, 0); \ |
| 435 | } |
| 436 | |
| 437 | /* Disassociate a piece of memory from a memory pool. */ |
| 438 | #define VALGRIND_MEMPOOL_FREE(pool, addr) \ |
| 439 | {unsigned int _qzz_res; \ |
| 440 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \ |
| 441 | VG_USERREQ__MEMPOOL_FREE, \ |
| 442 | pool, addr, 0, 0); \ |
| 443 | } |
| 444 | |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 445 | /* Mark a piece of memory as being a stack. Returns a stack id. */ |
| 446 | #define VALGRIND_STACK_REGISTER(start, end) \ |
| 447 | ({unsigned int _qzz_res; \ |
| 448 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \ |
| 449 | VG_USERREQ__STACK_REGISTER, \ |
| 450 | start, end, 0, 0); \ |
| 451 | _qzz_res; \ |
| 452 | }) |
| 453 | |
| 454 | /* Unmark the piece of memory associated with a stack id as being a |
| 455 | stack. */ |
| 456 | #define VALGRIND_STACK_DEREGISTER(id) \ |
| 457 | {unsigned int _qzz_res; \ |
| 458 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \ |
| 459 | VG_USERREQ__STACK_DEREGISTER, \ |
| 460 | id, 0, 0, 0); \ |
| 461 | } |
| 462 | |
| 463 | /* Change the start and end address of the stack id. */ |
| 464 | #define VALGRIND_STACK_CHANGE(id, start, end) \ |
| 465 | {unsigned int _qzz_res; \ |
| 466 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \ |
| 467 | VG_USERREQ__STACK_CHANGE, \ |
| 468 | id, start, end, 0); \ |
| 469 | } |
| 470 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 471 | #endif /* __VALGRIND_H */ |