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 | |
nethercote | bb1c991 | 2004-01-04 16:43:23 +0000 | [diff] [blame] | 15 | Copyright (C) 2000-2004 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 | |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 62 | #include <stdarg.h> |
| 63 | |
nethercote | b2decc3 | 2004-10-25 19:33:26 +0000 | [diff] [blame] | 64 | #undef __@VG_ARCH@__ |
| 65 | #define __@VG_ARCH@__ 1 // Architecture we're installed on |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 66 | |
| 67 | /* This file is for inclusion into client (your!) code. |
| 68 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 69 | You can use these macros to manipulate and query Valgrind's |
| 70 | execution inside your own programs. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 71 | |
| 72 | The resulting executables will still run without Valgrind, just a |
| 73 | little bit more slowly than they otherwise would, but otherwise |
sewardj | 285f77f | 2003-03-15 23:39:11 +0000 | [diff] [blame] | 74 | unchanged. When not running on valgrind, each client request |
nethercote | e90c683 | 2004-10-18 18:07:49 +0000 | [diff] [blame] | 75 | consumes very few (eg. < 10) instructions, so the resulting performance |
sewardj | 285f77f | 2003-03-15 23:39:11 +0000 | [diff] [blame] | 76 | loss is negligible unless you plan to execute client requests |
| 77 | millions of times per second. Nevertheless, if that is still a |
| 78 | problem, you can compile with the NVALGRIND symbol defined (gcc |
| 79 | -DNVALGRIND) so that client requests are not even compiled in. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 80 | |
sewardj | 37091fb | 2002-11-16 11:06:50 +0000 | [diff] [blame] | 81 | #ifndef NVALGRIND |
nethercote | e90c683 | 2004-10-18 18:07:49 +0000 | [diff] [blame] | 82 | |
nethercote | 5426544 | 2004-10-26 12:56:58 +0000 | [diff] [blame] | 83 | /* The following defines the magic code sequences which the JITter spots and |
| 84 | handles magically. Don't look too closely at them; they will rot |
nethercote | e90c683 | 2004-10-18 18:07:49 +0000 | [diff] [blame] | 85 | your brain. We must ensure that the default value gets put in the return |
| 86 | slot, so that everything works when this is executed not under Valgrind. |
| 87 | Args are passed in a memory block, and so there's no intrinsic limit to |
| 88 | the number that could be passed, but it's currently four. |
| 89 | |
nethercote | 5426544 | 2004-10-26 12:56:58 +0000 | [diff] [blame] | 90 | The macro args are: |
| 91 | _zzq_rlval result lvalue |
| 92 | _zzq_default default value (result returned when running on real CPU) |
| 93 | _zzq_request request code |
| 94 | _zzq_arg1..4 request params |
| 95 | |
nethercote | e90c683 | 2004-10-18 18:07:49 +0000 | [diff] [blame] | 96 | Nb: we put the assembly code sequences for all architectures in this one |
| 97 | file. This is because this file must be stand-alone, so we can't rely on |
| 98 | eg. x86/ subdirectories like we do within the rest of Valgrind. |
| 99 | */ |
| 100 | |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame^] | 101 | #ifdef __arm__ |
| 102 | // XXX: termporary, until MAGIC_SEQUENCE is written properly |
| 103 | extern int printf (__const char *__restrict __format, ...); |
| 104 | extern void exit (int __status); |
| 105 | #define VALGRIND_MAGIC_SEQUENCE( \ |
| 106 | _zzq_rlval, _zzq_default, _zzq_request, \ |
| 107 | _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4) \ |
| 108 | \ |
| 109 | { volatile unsigned int _zzq_args[5]; \ |
| 110 | _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); \ |
| 115 | (_zzq_rlval) = (_zzq_default);/* temporary only */ \ |
| 116 | printf("argh: MAGIC_SEQUENCE"); exit(1); \ |
| 117 | asm volatile(""); \ |
| 118 | } |
| 119 | // XXX: make sure that the register holding the args and the register taking |
| 120 | // the return value match ARCH_CLREQ_ARGS and ARCH_CLREQ_RET in |
| 121 | // arm/core_arch.h! |
| 122 | #endif // __arm__ |
| 123 | #ifdef __amd64__ |
| 124 | // XXX: termporary, until MAGIC_SEQUENCE is written properly |
| 125 | extern int printf (__const char *__restrict __format, ...); |
| 126 | extern void exit (int __status); |
| 127 | #define VALGRIND_MAGIC_SEQUENCE( \ |
| 128 | _zzq_rlval, _zzq_default, _zzq_request, \ |
| 129 | _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4) \ |
| 130 | \ |
| 131 | { volatile unsigned long _zzq_args[5]; \ |
| 132 | _zzq_args[0] = (volatile unsigned long)(_zzq_request); \ |
| 133 | _zzq_args[1] = (volatile unsigned long)(_zzq_arg1); \ |
| 134 | _zzq_args[2] = (volatile unsigned long)(_zzq_arg2); \ |
| 135 | _zzq_args[3] = (volatile unsigned long)(_zzq_arg3); \ |
| 136 | _zzq_args[4] = (volatile unsigned long)(_zzq_arg4); \ |
| 137 | (_zzq_rlval) = (_zzq_default);/* temporary only */ \ |
| 138 | printf("argh: MAGIC_SEQUENCE"); exit(1); \ |
| 139 | asm volatile(""); \ |
| 140 | } |
| 141 | // XXX: make sure that the register holding the args and the register taking |
| 142 | // the return value match ARCH_CLREQ_ARGS and ARCH_CLREQ_RET in |
| 143 | // amd64/core_arch.h! |
| 144 | #endif // __amd64__ |
nethercote | e90c683 | 2004-10-18 18:07:49 +0000 | [diff] [blame] | 145 | #ifdef __x86__ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 146 | #define VALGRIND_MAGIC_SEQUENCE( \ |
nethercote | 5426544 | 2004-10-26 12:56:58 +0000 | [diff] [blame] | 147 | _zzq_rlval, _zzq_default, _zzq_request, \ |
| 148 | _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4) \ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 149 | \ |
| 150 | { volatile unsigned int _zzq_args[5]; \ |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 151 | _zzq_args[0] = (volatile unsigned int)(_zzq_request); \ |
| 152 | _zzq_args[1] = (volatile unsigned int)(_zzq_arg1); \ |
| 153 | _zzq_args[2] = (volatile unsigned int)(_zzq_arg2); \ |
| 154 | _zzq_args[3] = (volatile unsigned int)(_zzq_arg3); \ |
| 155 | _zzq_args[4] = (volatile unsigned int)(_zzq_arg4); \ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 156 | asm volatile("movl %1, %%eax\n\t" \ |
| 157 | "movl %2, %%edx\n\t" \ |
| 158 | "roll $29, %%eax ; roll $3, %%eax\n\t" \ |
| 159 | "rorl $27, %%eax ; rorl $5, %%eax\n\t" \ |
| 160 | "roll $13, %%eax ; roll $19, %%eax\n\t" \ |
| 161 | "movl %%edx, %0\t" \ |
| 162 | : "=r" (_zzq_rlval) \ |
| 163 | : "r" (&_zzq_args[0]), "r" (_zzq_default) \ |
| 164 | : "eax", "edx", "cc", "memory" \ |
| 165 | ); \ |
| 166 | } |
njn | ca0518d | 2004-11-26 19:34:36 +0000 | [diff] [blame] | 167 | #endif // __x86__ |
nethercote | e90c683 | 2004-10-18 18:07:49 +0000 | [diff] [blame] | 168 | // Insert assembly code for other architectures here... |
| 169 | |
sewardj | 37091fb | 2002-11-16 11:06:50 +0000 | [diff] [blame] | 170 | #else /* NVALGRIND */ |
| 171 | /* Define NVALGRIND to completely remove the Valgrind magic sequence |
| 172 | from the compiled code (analogous to NDEBUG's effects on |
| 173 | assert()) */ |
| 174 | #define VALGRIND_MAGIC_SEQUENCE( \ |
nethercote | 69d9c46 | 2004-10-26 13:00:12 +0000 | [diff] [blame] | 175 | _zzq_rlval, _zzq_default, _zzq_request, \ |
| 176 | _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4) \ |
sewardj | 37091fb | 2002-11-16 11:06:50 +0000 | [diff] [blame] | 177 | { \ |
| 178 | (_zzq_rlval) = (_zzq_default); \ |
| 179 | } |
| 180 | #endif /* NVALGRIND */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 181 | |
nethercote | 69d9c46 | 2004-10-26 13:00:12 +0000 | [diff] [blame] | 182 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 183 | /* Some request codes. There are many more of these, but most are not |
| 184 | exposed to end-user view. These are the public ones, all of the |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 185 | form 0x1000 + small_number. |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 186 | |
| 187 | Core ones are in the range 0x00000000--0x0000ffff. The non-public ones |
| 188 | start at 0x2000. |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 189 | */ |
| 190 | |
njn | fc26ff9 | 2004-11-22 19:12:49 +0000 | [diff] [blame] | 191 | // These macros are used by tools -- they must be public, but don't embed them |
| 192 | // into other programs. |
| 193 | #define VG_USERREQ_TOOL_BASE(a,b) \ |
njn | 4c79121 | 2003-05-02 17:53:54 +0000 | [diff] [blame] | 194 | ((unsigned int)(((a)&0xff) << 24 | ((b)&0xff) << 16)) |
njn | fc26ff9 | 2004-11-22 19:12:49 +0000 | [diff] [blame] | 195 | #define VG_IS_TOOL_USERREQ(a, b, v) \ |
| 196 | (VG_USERREQ_TOOL_BASE(a,b) == ((v) & 0xffff0000)) |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 197 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 198 | typedef |
njn | 4c79121 | 2003-05-02 17:53:54 +0000 | [diff] [blame] | 199 | enum { VG_USERREQ__RUNNING_ON_VALGRIND = 0x1001, |
| 200 | VG_USERREQ__DISCARD_TRANSLATIONS = 0x1002, |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 201 | |
njn | d4795be | 2004-11-24 11:57:51 +0000 | [diff] [blame] | 202 | /* These allow any function to be called from the |
| 203 | simulated CPU but run on the real CPU. |
| 204 | Nb: the first arg passed to the function is always the ThreadId of |
| 205 | the running thread! So CLIENT_CALL0 actually requires a 1 arg |
| 206 | function, etc. */ |
njn | 4c79121 | 2003-05-02 17:53:54 +0000 | [diff] [blame] | 207 | VG_USERREQ__CLIENT_CALL0 = 0x1101, |
| 208 | VG_USERREQ__CLIENT_CALL1 = 0x1102, |
| 209 | VG_USERREQ__CLIENT_CALL2 = 0x1103, |
| 210 | VG_USERREQ__CLIENT_CALL3 = 0x1104, |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 211 | |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 212 | /* Can be useful in regression testing suites -- eg. can send |
| 213 | Valgrind's output to /dev/null and still count errors. */ |
njn | 4c79121 | 2003-05-02 17:53:54 +0000 | [diff] [blame] | 214 | VG_USERREQ__COUNT_ERRORS = 0x1201, |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 215 | |
nethercote | 7cc9c23 | 2004-01-21 15:08:04 +0000 | [diff] [blame] | 216 | /* These are useful and can be interpreted by any tool that tracks |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 217 | malloc() et al, by using vg_replace_malloc.c. */ |
| 218 | VG_USERREQ__MALLOCLIKE_BLOCK = 0x1301, |
| 219 | VG_USERREQ__FREELIKE_BLOCK = 0x1302, |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 220 | /* Memory pool support. */ |
| 221 | VG_USERREQ__CREATE_MEMPOOL = 0x1303, |
| 222 | VG_USERREQ__DESTROY_MEMPOOL = 0x1304, |
| 223 | VG_USERREQ__MEMPOOL_ALLOC = 0x1305, |
| 224 | VG_USERREQ__MEMPOOL_FREE = 0x1306, |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 225 | |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 226 | /* Allow printfs to valgrind log. */ |
| 227 | VG_USERREQ__PRINTF = 0x1401, |
thughes | 85c8a50 | 2004-08-25 13:25:30 +0000 | [diff] [blame] | 228 | VG_USERREQ__PRINTF_BACKTRACE = 0x1402 |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 229 | } Vg_ClientRequest; |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 230 | |
mueller | c9b3655 | 2003-12-31 14:32:23 +0000 | [diff] [blame] | 231 | #ifndef __GNUC__ |
| 232 | #define __extension__ |
| 233 | #endif |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 234 | |
| 235 | /* Returns 1 if running on Valgrind, 0 if running on the real CPU. |
| 236 | Currently implemented but untested. */ |
mueller | c9b3655 | 2003-12-31 14:32:23 +0000 | [diff] [blame] | 237 | #define RUNNING_ON_VALGRIND __extension__ \ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 238 | ({unsigned int _qzz_res; \ |
| 239 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0 /* returned if not */, \ |
| 240 | VG_USERREQ__RUNNING_ON_VALGRIND, \ |
| 241 | 0, 0, 0, 0); \ |
| 242 | _qzz_res; \ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 243 | }) |
| 244 | |
| 245 | |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 246 | /* Discard translation of code in the range [_qzz_addr .. _qzz_addr + |
| 247 | _qzz_len - 1]. Useful if you are debugging a JITter or some such, |
| 248 | since it provides a way to make sure valgrind will retranslate the |
| 249 | invalidated area. Returns no value. */ |
| 250 | #define VALGRIND_DISCARD_TRANSLATIONS(_qzz_addr,_qzz_len) \ |
| 251 | {unsigned int _qzz_res; \ |
| 252 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \ |
| 253 | VG_USERREQ__DISCARD_TRANSLATIONS, \ |
| 254 | _qzz_addr, _qzz_len, 0, 0); \ |
| 255 | } |
| 256 | |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 257 | #ifndef NVALGRIND |
| 258 | |
fitzhardinge | a09a1b5 | 2003-11-07 23:09:48 +0000 | [diff] [blame] | 259 | int VALGRIND_PRINTF(const char *format, ...) |
| 260 | __attribute__((format(__printf__, 1, 2))); |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 261 | __attribute__((weak)) |
| 262 | int |
fitzhardinge | a09a1b5 | 2003-11-07 23:09:48 +0000 | [diff] [blame] | 263 | VALGRIND_PRINTF(const char *format, ...) |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 264 | { |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame^] | 265 | unsigned long _qzz_res; |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 266 | va_list vargs; |
| 267 | va_start(vargs, format); |
| 268 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, VG_USERREQ__PRINTF, |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame^] | 269 | (unsigned long)format, (unsigned long)vargs, 0, 0); |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 270 | va_end(vargs); |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame^] | 271 | return (int)_qzz_res; |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 272 | } |
| 273 | |
fitzhardinge | a09a1b5 | 2003-11-07 23:09:48 +0000 | [diff] [blame] | 274 | int VALGRIND_PRINTF_BACKTRACE(const char *format, ...) |
| 275 | __attribute__((format(__printf__, 1, 2))); |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 276 | __attribute__((weak)) |
| 277 | int |
fitzhardinge | a09a1b5 | 2003-11-07 23:09:48 +0000 | [diff] [blame] | 278 | VALGRIND_PRINTF_BACKTRACE(const char *format, ...) |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 279 | { |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame^] | 280 | unsigned long _qzz_res; |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 281 | va_list vargs; |
| 282 | va_start(vargs, format); |
| 283 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, VG_USERREQ__PRINTF_BACKTRACE, |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame^] | 284 | (unsigned long)format, (unsigned long)vargs, 0, 0); |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 285 | va_end(vargs); |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame^] | 286 | return (int)_qzz_res; |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | #else /* NVALGRIND */ |
| 290 | |
| 291 | #define VALGRIND_PRINTF(...) |
| 292 | #define VALGRIND_PRINTF_BACKTRACE(...) |
| 293 | |
| 294 | #endif /* NVALGRIND */ |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 295 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 296 | /* These requests allow control to move from the simulated CPU to the |
| 297 | real CPU, calling an arbitary function */ |
njn | 057c65f | 2003-04-21 13:30:55 +0000 | [diff] [blame] | 298 | #define VALGRIND_NON_SIMD_CALL0(_qyy_fn) \ |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame^] | 299 | ({unsigned long _qyy_res; \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 300 | VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \ |
| 301 | VG_USERREQ__CLIENT_CALL0, \ |
| 302 | _qyy_fn, \ |
| 303 | 0, 0, 0); \ |
| 304 | _qyy_res; \ |
| 305 | }) |
| 306 | |
njn | 057c65f | 2003-04-21 13:30:55 +0000 | [diff] [blame] | 307 | #define VALGRIND_NON_SIMD_CALL1(_qyy_fn, _qyy_arg1) \ |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame^] | 308 | ({unsigned long _qyy_res; \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 309 | VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \ |
| 310 | VG_USERREQ__CLIENT_CALL1, \ |
| 311 | _qyy_fn, \ |
| 312 | _qyy_arg1, 0, 0); \ |
| 313 | _qyy_res; \ |
| 314 | }) |
| 315 | |
njn | 057c65f | 2003-04-21 13:30:55 +0000 | [diff] [blame] | 316 | #define VALGRIND_NON_SIMD_CALL2(_qyy_fn, _qyy_arg1, _qyy_arg2) \ |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame^] | 317 | ({unsigned long _qyy_res; \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 318 | VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \ |
| 319 | VG_USERREQ__CLIENT_CALL2, \ |
| 320 | _qyy_fn, \ |
| 321 | _qyy_arg1, _qyy_arg2, 0); \ |
| 322 | _qyy_res; \ |
| 323 | }) |
| 324 | |
njn | 057c65f | 2003-04-21 13:30:55 +0000 | [diff] [blame] | 325 | #define VALGRIND_NON_SIMD_CALL3(_qyy_fn, _qyy_arg1, _qyy_arg2, _qyy_arg3) \ |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame^] | 326 | ({unsigned long _qyy_res; \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 327 | VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \ |
| 328 | VG_USERREQ__CLIENT_CALL3, \ |
| 329 | _qyy_fn, \ |
| 330 | _qyy_arg1, _qyy_arg2, _qyy_arg3); \ |
| 331 | _qyy_res; \ |
| 332 | }) |
| 333 | |
| 334 | |
nethercote | 7cc9c23 | 2004-01-21 15:08:04 +0000 | [diff] [blame] | 335 | /* Counts the number of errors that have been recorded by a tool. Nb: |
| 336 | the tool must record the errors with VG_(maybe_record_error)() or |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 337 | VG_(unique_error)() for them to be counted. */ |
| 338 | #define VALGRIND_COUNT_ERRORS \ |
| 339 | ({unsigned int _qyy_res; \ |
| 340 | VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \ |
| 341 | VG_USERREQ__COUNT_ERRORS, \ |
| 342 | 0, 0, 0, 0); \ |
| 343 | _qyy_res; \ |
| 344 | }) |
| 345 | |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 346 | /* Mark a block of memory as having been allocated by a malloc()-like |
| 347 | function. `addr' is the start of the usable block (ie. after any |
| 348 | redzone) `rzB' is redzone size if the allocator can apply redzones; |
| 349 | use '0' if not. Adding redzones makes it more likely Valgrind will spot |
| 350 | block overruns. `is_zeroed' indicates if the memory is zeroed, as it is |
| 351 | for calloc(). Put it immediately after the point where a block is |
| 352 | allocated. |
| 353 | |
| 354 | If you're allocating memory via superblocks, and then handing out small |
| 355 | chunks of each superblock, if you don't have redzones on your small |
| 356 | blocks, it's worth marking the superblock with VALGRIND_MAKE_NOACCESS |
| 357 | when it's created, so that block overruns are detected. But if you can |
| 358 | put redzones on, it's probably better to not do this, so that messages |
| 359 | for small overruns are described in terms of the small block rather than |
| 360 | the superblock (but if you have a big overrun that skips over a redzone, |
| 361 | you could miss an error this way). See memcheck/tests/custom_alloc.c |
| 362 | for an example. |
| 363 | |
| 364 | Nb: block must be freed via a free()-like function specified |
| 365 | with VALGRIND_FREELIKE_BLOCK or mismatch errors will occur. */ |
| 366 | #define VALGRIND_MALLOCLIKE_BLOCK(addr, sizeB, rzB, is_zeroed) \ |
| 367 | {unsigned int _qzz_res; \ |
| 368 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \ |
| 369 | VG_USERREQ__MALLOCLIKE_BLOCK, \ |
| 370 | addr, sizeB, rzB, is_zeroed); \ |
| 371 | } |
| 372 | |
| 373 | /* Mark a block of memory as having been freed by a free()-like function. |
| 374 | `rzB' is redzone size; it must match that given to |
| 375 | VALGRIND_MALLOCLIKE_BLOCK. Memory not freed will be detected by the leak |
| 376 | checker. Put it immediately after the point where the block is freed. */ |
| 377 | #define VALGRIND_FREELIKE_BLOCK(addr, rzB) \ |
| 378 | {unsigned int _qzz_res; \ |
| 379 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \ |
| 380 | VG_USERREQ__FREELIKE_BLOCK, \ |
| 381 | addr, rzB, 0, 0); \ |
| 382 | } |
| 383 | |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 384 | /* Create a memory pool. */ |
| 385 | #define VALGRIND_CREATE_MEMPOOL(pool, rzB, is_zeroed) \ |
| 386 | {unsigned int _qzz_res; \ |
| 387 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \ |
| 388 | VG_USERREQ__CREATE_MEMPOOL, \ |
| 389 | pool, rzB, is_zeroed, 0); \ |
| 390 | } |
| 391 | |
| 392 | /* Destroy a memory pool. */ |
| 393 | #define VALGRIND_DESTROY_MEMPOOL(pool) \ |
| 394 | {unsigned int _qzz_res; \ |
| 395 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \ |
| 396 | VG_USERREQ__DESTROY_MEMPOOL, \ |
| 397 | pool, 0, 0, 0); \ |
| 398 | } |
| 399 | |
| 400 | /* Associate a piece of memory with a memory pool. */ |
| 401 | #define VALGRIND_MEMPOOL_ALLOC(pool, addr, size) \ |
| 402 | {unsigned int _qzz_res; \ |
| 403 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \ |
| 404 | VG_USERREQ__MEMPOOL_ALLOC, \ |
| 405 | pool, addr, size, 0); \ |
| 406 | } |
| 407 | |
| 408 | /* Disassociate a piece of memory from a memory pool. */ |
| 409 | #define VALGRIND_MEMPOOL_FREE(pool, addr) \ |
| 410 | {unsigned int _qzz_res; \ |
| 411 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \ |
| 412 | VG_USERREQ__MEMPOOL_FREE, \ |
| 413 | pool, addr, 0, 0); \ |
| 414 | } |
| 415 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 416 | #endif /* __VALGRIND_H */ |