valgrind.h

Go to the documentation of this file.
00001 /* -*- c -*-
00002    ----------------------------------------------------------------
00003 
00004    Notice that the following BSD-style license applies to this one
00005    file (valgrind.h) only.  The entire rest of Valgrind is licensed
00006    under the terms of the GNU General Public License, version 2.  See
00007    the COPYING file in the source distribution for details.
00008 
00009    ----------------------------------------------------------------
00010 
00011    This file is part of Valgrind, a dynamic binary instrumentation
00012    framework.
00013 
00014    Copyright (C) 2000-2005 Julian Seward.  All rights reserved.
00015 
00016    Redistribution and use in source and binary forms, with or without
00017    modification, are permitted provided that the following conditions
00018    are met:
00019 
00020    1. Redistributions of source code must retain the above copyright
00021       notice, this list of conditions and the following disclaimer.
00022 
00023    2. The origin of this software must not be misrepresented; you must 
00024       not claim that you wrote the original software.  If you use this 
00025       software in a product, an acknowledgment in the product 
00026       documentation would be appreciated but is not required.
00027 
00028    3. Altered source versions must be plainly marked as such, and must
00029       not be misrepresented as being the original software.
00030 
00031    4. The name of the author may not be used to endorse or promote 
00032       products derived from this software without specific prior written 
00033       permission.
00034 
00035    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00036    OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00037    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00038    ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00039    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00040    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00041    GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00042    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00043    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00044    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00045    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00046 
00047    ----------------------------------------------------------------
00048 
00049    Notice that the above BSD-style license applies to this one file
00050    (valgrind.h) only.  The entire rest of Valgrind is licensed under
00051    the terms of the GNU General Public License, version 2.  See the
00052    COPYING file in the source distribution for details.
00053 
00054    ---------------------------------------------------------------- 
00055 */
00056 
00057 
00058 /* This file is for inclusion into client (your!) code.
00059 
00060    You can use these macros to manipulate and query Valgrind's 
00061    execution inside your own programs.
00062 
00063    The resulting executables will still run without Valgrind, just a
00064    little bit more slowly than they otherwise would, but otherwise
00065    unchanged.  When not running on valgrind, each client request
00066    consumes very few (eg. < 10) instructions, so the resulting performance
00067    loss is negligible unless you plan to execute client requests
00068    millions of times per second.  Nevertheless, if that is still a
00069    problem, you can compile with the NVALGRIND symbol defined (gcc
00070    -DNVALGRIND) so that client requests are not even compiled in.  */
00071 
00072 #ifndef __VALGRIND_H
00073 #define __VALGRIND_H
00074 
00075 #include <stdarg.h>
00076 
00077 /* Nb: this file might be included in a file compiled with -ansi.  So
00078    we can't use C++ style "//" comments nor the "asm" keyword (instead
00079    use "__asm__"). */
00080 
00081 /* If we're not compiling for our target architecture, don't generate
00082    any inline asms.  Note that in this file we're using the compiler's
00083    CPP symbols for identifying architectures, which are different to
00084    the ones we use within the rest of Valgrind. */
00085 #if !defined(__i386__) && !defined(__x86_64__) && !defined(__powerpc__)
00086 #  ifndef NVALGRIND
00087 #    define NVALGRIND   1
00088 #  endif  /* NVALGRIND */
00089 #endif
00090 
00091 /* ------------------------------------------------------------------ */
00092 /* The architecture-specific part                                     */
00093 /* ------------------------------------------------------------------ */
00094 
00095 #ifdef NVALGRIND
00096 
00097 /* Define NVALGRIND to completely remove the Valgrind magic sequence
00098    from the compiled code (analogous to NDEBUG's effects on assert()) */
00099 #define VALGRIND_MAGIC_SEQUENCE(                    \
00100         _zzq_rlval, _zzq_default, _zzq_request,                         \
00101         _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4)                     \
00102    {                                    \
00103       (_zzq_rlval) = (_zzq_default);                    \
00104    }
00105 
00106 #else  /* NVALGRIND */
00107 
00108 /* The following defines the magic code sequences which the JITter spots and
00109    handles magically.  Don't look too closely at them; they will rot
00110    your brain.  We must ensure that the default value gets put in the return
00111    slot, so that everything works when this is executed not under Valgrind.
00112    Args are passed in a memory block, and so there's no intrinsic limit to
00113    the number that could be passed, but it's currently four.
00114    
00115    The macro args are: 
00116       _zzq_rlval    result lvalue
00117       _zzq_default  default value (result returned when running on real CPU)
00118       _zzq_request  request code
00119       _zzq_arg1..4  request params
00120 
00121    Nb: we put the assembly code sequences for all architectures in this one
00122    file.  This is because this file must be stand-alone, and we don't want
00123    to have multiple files.
00124 */
00125 
00126 #ifdef __x86_64__
00127 #define VALGRIND_MAGIC_SEQUENCE(                                \
00128         _zzq_rlval, _zzq_default, _zzq_request,                 \
00129         _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4)             \
00130                                                                 \
00131   { volatile unsigned long long _zzq_args[5];                   \
00132     _zzq_args[0] = (volatile unsigned long long)(_zzq_request); \
00133     _zzq_args[1] = (volatile unsigned long long)(_zzq_arg1);    \
00134     _zzq_args[2] = (volatile unsigned long long)(_zzq_arg2);    \
00135     _zzq_args[3] = (volatile unsigned long long)(_zzq_arg3);    \
00136     _zzq_args[4] = (volatile unsigned long long)(_zzq_arg4);    \
00137     __asm__ volatile("roll $29, %%eax ; roll $3, %%eax\n\t" \
00138                      "rorl $27, %%eax ; rorl $5, %%eax\n\t" \
00139                      "roll $13, %%eax ; roll $19, %%eax"        \
00140                      : "=d" (_zzq_rlval)                \
00141                      : "a" (&_zzq_args[0]), "0" (_zzq_default)  \
00142                      : "cc", "memory"               \
00143                     );                      \
00144   }
00145 #endif  /* __x86_64__ */
00146 
00147 #ifdef __i386__
00148 #define VALGRIND_MAGIC_SEQUENCE(                \
00149         _zzq_rlval, _zzq_default, _zzq_request,         \
00150         _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4)     \
00151                                 \
00152   { unsigned int _zzq_args[5];                  \
00153     _zzq_args[0] = (unsigned int)(_zzq_request);        \
00154     _zzq_args[1] = (unsigned int)(_zzq_arg1);           \
00155     _zzq_args[2] = (unsigned int)(_zzq_arg2);           \
00156     _zzq_args[3] = (unsigned int)(_zzq_arg3);           \
00157     _zzq_args[4] = (unsigned int)(_zzq_arg4);           \
00158     __asm__ volatile("roll $29, %%eax ; roll $3, %%eax\n\t" \
00159                      "rorl $27, %%eax ; rorl $5, %%eax\n\t" \
00160                      "roll $13, %%eax ; roll $19, %%eax"    \
00161                      : "=d" (_zzq_rlval)            \
00162                      : "a" (&_zzq_args[0]), "0" (_zzq_default)  \
00163                      : "cc", "memory"               \
00164                     );                      \
00165   }
00166 #endif  /* __i386__ */
00167 
00168 #ifdef __powerpc__
00169 #define VALGRIND_MAGIC_SEQUENCE(                                        \
00170         _zzq_rlval, _zzq_default, _zzq_request,                         \
00171         _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4)                     \
00172                                                                         \
00173   { volatile unsigned int _zzq_args[5];                                 \
00174     register unsigned int _zzq_tmp __asm__("r3");                       \
00175     register volatile unsigned int *_zzq_ptr __asm__("r4");             \
00176     _zzq_args[0] = (volatile unsigned int)(_zzq_request);               \
00177     _zzq_args[1] = (volatile unsigned int)(_zzq_arg1);                  \
00178     _zzq_args[2] = (volatile unsigned int)(_zzq_arg2);                  \
00179     _zzq_args[3] = (volatile unsigned int)(_zzq_arg3);                  \
00180     _zzq_args[4] = (volatile unsigned int)(_zzq_arg4);                  \
00181     _zzq_ptr = _zzq_args;                                               \
00182     __asm__ volatile("tw 0,3,27\n\t"                                    \
00183                      "rlwinm 0,0,29,0,0\n\t"                            \
00184                      "rlwinm 0,0,3,0,0\n\t"                             \
00185                      "rlwinm 0,0,13,0,0\n\t"                            \
00186                      "rlwinm 0,0,19,0,0\n\t"                            \
00187                      "nop\n\t"                                          \
00188                      : "=r" (_zzq_tmp)                                  \
00189                      : "0" (_zzq_default), "r" (_zzq_ptr)               \
00190                      : "memory");                                       \
00191     _zzq_rlval = (__typeof__(_zzq_rlval)) _zzq_tmp;                     \
00192   }
00193 #endif   /* __powerpc__ */
00194 
00195 /* Insert assembly code for other architectures here... */
00196 
00197 #endif /* NVALGRIND */
00198 
00199 
00200 /* ------------------------------------------------------------------ */
00201 /* The architecture-independent part                                  */
00202 /* ------------------------------------------------------------------ */
00203 
00204 /* Some request codes.  There are many more of these, but most are not
00205    exposed to end-user view.  These are the public ones, all of the
00206    form 0x1000 + small_number.
00207 
00208    Core ones are in the range 0x00000000--0x0000ffff.  The non-public ones
00209    start at 0x2000.
00210 */
00211 
00212 /* These macros are used by tools -- they must be public, but don't embed them
00213  * into other programs. */
00214 #define VG_USERREQ_TOOL_BASE(a,b) \
00215    ((unsigned int)(((a)&0xff) << 24 | ((b)&0xff) << 16))
00216 #define VG_IS_TOOL_USERREQ(a, b, v) \
00217    (VG_USERREQ_TOOL_BASE(a,b) == ((v) & 0xffff0000))
00218 
00219 typedef
00220    enum { VG_USERREQ__RUNNING_ON_VALGRIND  = 0x1001,
00221           VG_USERREQ__DISCARD_TRANSLATIONS = 0x1002,
00222 
00223           /* These allow any function to be called from the
00224              simulated CPU but run on the real CPU.
00225              Nb: the first arg passed to the function is always the ThreadId of
00226              the running thread!  So CLIENT_CALL0 actually requires a 1 arg
00227              function, etc. */
00228           VG_USERREQ__CLIENT_CALL0 = 0x1101,
00229           VG_USERREQ__CLIENT_CALL1 = 0x1102,
00230           VG_USERREQ__CLIENT_CALL2 = 0x1103,
00231           VG_USERREQ__CLIENT_CALL3 = 0x1104,
00232 
00233           /* Can be useful in regression testing suites -- eg. can send
00234              Valgrind's output to /dev/null and still count errors. */
00235           VG_USERREQ__COUNT_ERRORS = 0x1201,
00236 
00237           /* These are useful and can be interpreted by any tool that tracks
00238              malloc() et al, by using vg_replace_malloc.c. */
00239           VG_USERREQ__MALLOCLIKE_BLOCK = 0x1301,
00240           VG_USERREQ__FREELIKE_BLOCK   = 0x1302,
00241           /* Memory pool support. */
00242           VG_USERREQ__CREATE_MEMPOOL   = 0x1303,
00243           VG_USERREQ__DESTROY_MEMPOOL  = 0x1304,
00244           VG_USERREQ__MEMPOOL_ALLOC    = 0x1305,
00245           VG_USERREQ__MEMPOOL_FREE     = 0x1306,
00246 
00247           /* Allow printfs to valgrind log. */
00248           VG_USERREQ__PRINTF           = 0x1401,
00249           VG_USERREQ__PRINTF_BACKTRACE = 0x1402,
00250 
00251           /* Stack support. */
00252           VG_USERREQ__STACK_REGISTER   = 0x1501,
00253           VG_USERREQ__STACK_DEREGISTER = 0x1502,
00254           VG_USERREQ__STACK_CHANGE     = 0x1503,
00255    } Vg_ClientRequest;
00256 
00257 #ifndef __GNUC__
00258 #define __extension__
00259 #endif
00260 
00261 /* Returns the number of Valgrinds this code is running under.  That is,
00262    0 if running natively, 1 if running under Valgrind, 2 if running under
00263    Valgrind which is running under another Valgrind, etc. */
00264 #define RUNNING_ON_VALGRIND  __extension__                         \
00265    ({unsigned int _qzz_res;                                        \
00266     VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0 /* returned if not */,     \
00267                             VG_USERREQ__RUNNING_ON_VALGRIND,       \
00268                             0, 0, 0, 0);                           \
00269     _qzz_res;                                                      \
00270    })
00271 
00272 
00273 /* Discard translation of code in the range [_qzz_addr .. _qzz_addr +
00274    _qzz_len - 1].  Useful if you are debugging a JITter or some such,
00275    since it provides a way to make sure valgrind will retranslate the
00276    invalidated area.  Returns no value. */
00277 #define VALGRIND_DISCARD_TRANSLATIONS(_qzz_addr,_qzz_len)          \
00278    {unsigned int _qzz_res;                                         \
00279     VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0,                           \
00280                             VG_USERREQ__DISCARD_TRANSLATIONS,      \
00281                             _qzz_addr, _qzz_len, 0, 0);            \
00282    }
00283 
00284 #ifdef NVALGRIND
00285 
00286 #define VALGRIND_PRINTF(...)
00287 #define VALGRIND_PRINTF_BACKTRACE(...)
00288 
00289 #else /* NVALGRIND */
00290 
00291 int VALGRIND_PRINTF(const char *format, ...)
00292    __attribute__((format(__printf__, 1, 2)));
00293 __attribute__((weak))
00294 int
00295 VALGRIND_PRINTF(const char *format, ...)
00296 {
00297    unsigned long _qzz_res;
00298    va_list vargs;
00299    va_start(vargs, format);
00300    VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, VG_USERREQ__PRINTF,
00301                            (unsigned long)format, (unsigned long)vargs, 0, 0);
00302    va_end(vargs);
00303    return (int)_qzz_res;
00304 }
00305 
00306 int VALGRIND_PRINTF_BACKTRACE(const char *format, ...)
00307    __attribute__((format(__printf__, 1, 2)));
00308 __attribute__((weak))
00309 int
00310 VALGRIND_PRINTF_BACKTRACE(const char *format, ...)
00311 {
00312    unsigned long _qzz_res;
00313    va_list vargs;
00314    va_start(vargs, format);
00315    VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, VG_USERREQ__PRINTF_BACKTRACE,
00316                            (unsigned long)format, (unsigned long)vargs, 0, 0);
00317    va_end(vargs);
00318    return (int)_qzz_res;
00319 }
00320 
00321 #endif /* NVALGRIND */
00322 
00323 /* These requests allow control to move from the simulated CPU to the
00324    real CPU, calling an arbitary function */
00325 #define VALGRIND_NON_SIMD_CALL0(_qyy_fn)                       \
00326    ({unsigned long _qyy_res;                                   \
00327     VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */,  \
00328                             VG_USERREQ__CLIENT_CALL0,          \
00329                             _qyy_fn,                           \
00330                             0, 0, 0);                          \
00331     _qyy_res;                                                  \
00332    })
00333 
00334 #define VALGRIND_NON_SIMD_CALL1(_qyy_fn, _qyy_arg1)            \
00335    ({unsigned long _qyy_res;                                   \
00336     VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */,  \
00337                             VG_USERREQ__CLIENT_CALL1,          \
00338                             _qyy_fn,                           \
00339                             _qyy_arg1, 0, 0);                  \
00340     _qyy_res;                                                  \
00341    })
00342 
00343 #define VALGRIND_NON_SIMD_CALL2(_qyy_fn, _qyy_arg1, _qyy_arg2) \
00344    ({unsigned long _qyy_res;                                   \
00345     VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */,  \
00346                             VG_USERREQ__CLIENT_CALL2,          \
00347                             _qyy_fn,                           \
00348                             _qyy_arg1, _qyy_arg2, 0);          \
00349     _qyy_res;                                                  \
00350    })
00351 
00352 #define VALGRIND_NON_SIMD_CALL3(_qyy_fn, _qyy_arg1, _qyy_arg2, _qyy_arg3)  \
00353    ({unsigned long _qyy_res;                                         \
00354     VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */,        \
00355                             VG_USERREQ__CLIENT_CALL3,                \
00356                             _qyy_fn,                                 \
00357                             _qyy_arg1, _qyy_arg2, _qyy_arg3);        \
00358     _qyy_res;                                                        \
00359    })
00360 
00361 
00362 /* Counts the number of errors that have been recorded by a tool.  Nb:
00363    the tool must record the errors with VG_(maybe_record_error)() or
00364    VG_(unique_error)() for them to be counted. */
00365 #define VALGRIND_COUNT_ERRORS                                           \
00366    ({unsigned int _qyy_res;                                             \
00367     VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */,           \
00368                             VG_USERREQ__COUNT_ERRORS,                   \
00369                             0, 0, 0, 0);                                \
00370     _qyy_res;                                                           \
00371    })
00372 
00373 /* Mark a block of memory as having been allocated by a malloc()-like
00374    function.  `addr' is the start of the usable block (ie. after any
00375    redzone) `rzB' is redzone size if the allocator can apply redzones;
00376    use '0' if not.  Adding redzones makes it more likely Valgrind will spot
00377    block overruns.  `is_zeroed' indicates if the memory is zeroed, as it is
00378    for calloc().  Put it immediately after the point where a block is
00379    allocated. 
00380    
00381    If you're allocating memory via superblocks, and then handing out small
00382    chunks of each superblock, if you don't have redzones on your small
00383    blocks, it's worth marking the superblock with VALGRIND_MAKE_NOACCESS
00384    when it's created, so that block overruns are detected.  But if you can
00385    put redzones on, it's probably better to not do this, so that messages
00386    for small overruns are described in terms of the small block rather than
00387    the superblock (but if you have a big overrun that skips over a redzone,
00388    you could miss an error this way).  See memcheck/tests/custom_alloc.c
00389    for an example.
00390 
00391    Nb: block must be freed via a free()-like function specified
00392    with VALGRIND_FREELIKE_BLOCK or mismatch errors will occur. */
00393 #define VALGRIND_MALLOCLIKE_BLOCK(addr, sizeB, rzB, is_zeroed)     \
00394    {unsigned int _qzz_res;                                         \
00395     VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0,                           \
00396                             VG_USERREQ__MALLOCLIKE_BLOCK,          \
00397                             addr, sizeB, rzB, is_zeroed);          \
00398    }
00399 
00400 /* Mark a block of memory as having been freed by a free()-like function.
00401    `rzB' is redzone size;  it must match that given to
00402    VALGRIND_MALLOCLIKE_BLOCK.  Memory not freed will be detected by the leak
00403    checker.  Put it immediately after the point where the block is freed. */
00404 #define VALGRIND_FREELIKE_BLOCK(addr, rzB)                         \
00405    {unsigned int _qzz_res;                                         \
00406     VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0,                           \
00407                             VG_USERREQ__FREELIKE_BLOCK,            \
00408                             addr, rzB, 0, 0);                      \
00409    }
00410 
00411 /* Create a memory pool. */
00412 #define VALGRIND_CREATE_MEMPOOL(pool, rzB, is_zeroed)              \
00413    {unsigned int _qzz_res;                                         \
00414     VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0,                           \
00415                             VG_USERREQ__CREATE_MEMPOOL,            \
00416                             pool, rzB, is_zeroed, 0);              \
00417    }
00418 
00419 /* Destroy a memory pool. */
00420 #define VALGRIND_DESTROY_MEMPOOL(pool)                             \
00421    {unsigned int _qzz_res;                                         \
00422     VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0,                           \
00423                             VG_USERREQ__DESTROY_MEMPOOL,           \
00424                             pool, 0, 0, 0);                        \
00425    }
00426 
00427 /* Associate a piece of memory with a memory pool. */
00428 #define VALGRIND_MEMPOOL_ALLOC(pool, addr, size)                   \
00429    {unsigned int _qzz_res;                                         \
00430     VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0,                           \
00431                             VG_USERREQ__MEMPOOL_ALLOC,             \
00432                             pool, addr, size, 0);                  \
00433    }
00434 
00435 /* Disassociate a piece of memory from a memory pool. */
00436 #define VALGRIND_MEMPOOL_FREE(pool, addr)                          \
00437    {unsigned int _qzz_res;                                         \
00438     VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0,                           \
00439                             VG_USERREQ__MEMPOOL_FREE,              \
00440                             pool, addr, 0, 0);                     \
00441    }
00442 
00443 /* Mark a piece of memory as being a stack. Returns a stack id. */
00444 #define VALGRIND_STACK_REGISTER(start, end)                        \
00445    ({unsigned int _qzz_res;                                        \
00446     VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0,                           \
00447                             VG_USERREQ__STACK_REGISTER,            \
00448                             start, end, 0, 0);                     \
00449     _qzz_res;                                                      \
00450    })
00451 
00452 /* Unmark the piece of memory associated with a stack id as being a
00453    stack. */
00454 #define VALGRIND_STACK_DEREGISTER(id)                              \
00455    {unsigned int _qzz_res;                                         \
00456     VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0,                           \
00457                             VG_USERREQ__STACK_DEREGISTER,          \
00458                             id, 0, 0, 0);                          \
00459    }
00460 
00461 /* Change the start and end address of the stack id. */
00462 #define VALGRIND_STACK_CHANGE(id, start, end)                      \
00463    {unsigned int _qzz_res;                                         \
00464     VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0,                           \
00465                             VG_USERREQ__STACK_CHANGE,              \
00466                             id, start, end, 0);                    \
00467    }
00468 
00469 #endif   /* __VALGRIND_H */

Generated on Fri Sep 28 00:35:46 2007 for raceintospace by  doxygen 1.5.3