valgrind.h revision 7cc9c239f785f2903b597cdb34418bed42d25331
1de4a1d01951937632098a6cda45859afa587a06fsewardj
2de4a1d01951937632098a6cda45859afa587a06fsewardj/*
3e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   ----------------------------------------------------------------
4e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn
5e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   Notice that the following BSD-style license applies to this one
6e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   file (valgrind.h) only.  The entire rest of Valgrind is licensed
7e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   under the terms of the GNU General Public License, version 2.  See
8e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   the COPYING file in the source distribution for details.
9e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn
10e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   ----------------------------------------------------------------
11e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn
12c953984b8ee34ac55830b9871138b409d9d9476cnjn   This file is part of Valgrind, an extensible x86 protected-mode
13c953984b8ee34ac55830b9871138b409d9d9476cnjn   emulator for monitoring program execution on x86-Unixes.
14de4a1d01951937632098a6cda45859afa587a06fsewardj
15bb1c99123c95fb9a4a2617d6e1d09559ac68db80nethercote   Copyright (C) 2000-2004 Julian Seward.  All rights reserved.
16e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn
17e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   Redistribution and use in source and binary forms, with or without
18e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   modification, are permitted provided that the following conditions
19e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   are met:
20e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn
21e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   1. Redistributions of source code must retain the above copyright
22e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn      notice, this list of conditions and the following disclaimer.
23e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn
24e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   2. The origin of this software must not be misrepresented; you must
25e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn      not claim that you wrote the original software.  If you use this
26e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn      software in a product, an acknowledgment in the product
27e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn      documentation would be appreciated but is not required.
28de4a1d01951937632098a6cda45859afa587a06fsewardj
29e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   3. Altered source versions must be plainly marked as such, and must
30e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn      not be misrepresented as being the original software.
31de4a1d01951937632098a6cda45859afa587a06fsewardj
32e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   4. The name of the author may not be used to endorse or promote
33e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn      products derived from this software without specific prior written
34e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn      permission.
35de4a1d01951937632098a6cda45859afa587a06fsewardj
36e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
37e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
38e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
40e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
42e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
43e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
45e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
46e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47de4a1d01951937632098a6cda45859afa587a06fsewardj
48e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   ----------------------------------------------------------------
49e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn
50e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   Notice that the above BSD-style license applies to this one file
51e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   (valgrind.h) only.  The entire rest of Valgrind is licensed under
52e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   the terms of the GNU General Public License, version 2.  See the
53e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   COPYING file in the source distribution for details.
54e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn
55e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   ----------------------------------------------------------------
56de4a1d01951937632098a6cda45859afa587a06fsewardj*/
57de4a1d01951937632098a6cda45859afa587a06fsewardj
58de4a1d01951937632098a6cda45859afa587a06fsewardj
59de4a1d01951937632098a6cda45859afa587a06fsewardj#ifndef __VALGRIND_H
60de4a1d01951937632098a6cda45859afa587a06fsewardj#define __VALGRIND_H
61de4a1d01951937632098a6cda45859afa587a06fsewardj
6239de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge#include <stdarg.h>
6339de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge
64de4a1d01951937632098a6cda45859afa587a06fsewardj
65de4a1d01951937632098a6cda45859afa587a06fsewardj/* This file is for inclusion into client (your!) code.
66de4a1d01951937632098a6cda45859afa587a06fsewardj
67e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   You can use these macros to manipulate and query Valgrind's
68e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   execution inside your own programs.
69de4a1d01951937632098a6cda45859afa587a06fsewardj
70de4a1d01951937632098a6cda45859afa587a06fsewardj   The resulting executables will still run without Valgrind, just a
71de4a1d01951937632098a6cda45859afa587a06fsewardj   little bit more slowly than they otherwise would, but otherwise
72285f77f8204e23274b43068aa91ad04741fda043sewardj   unchanged.  When not running on valgrind, each client request
73285f77f8204e23274b43068aa91ad04741fda043sewardj   consumes about 9 x86 instructions, so the resulting performance
74285f77f8204e23274b43068aa91ad04741fda043sewardj   loss is negligible unless you plan to execute client requests
75285f77f8204e23274b43068aa91ad04741fda043sewardj   millions of times per second.  Nevertheless, if that is still a
76285f77f8204e23274b43068aa91ad04741fda043sewardj   problem, you can compile with the NVALGRIND symbol defined (gcc
77285f77f8204e23274b43068aa91ad04741fda043sewardj   -DNVALGRIND) so that client requests are not even compiled in.  */
78de4a1d01951937632098a6cda45859afa587a06fsewardj
79de4a1d01951937632098a6cda45859afa587a06fsewardj
80de4a1d01951937632098a6cda45859afa587a06fsewardj
8137091fb739760631f436043c47de612cf9fd2dd1sewardj#ifndef NVALGRIND
82de4a1d01951937632098a6cda45859afa587a06fsewardj/* This defines the magic code sequence which the JITter spots and
83de4a1d01951937632098a6cda45859afa587a06fsewardj   handles magically.  Don't look too closely at this; it will rot
842e93c50dc50235189661b70e3f27a4098d5cccccsewardj   your brain.  Valgrind dumps the result value in %EDX, so we first
852e93c50dc50235189661b70e3f27a4098d5cccccsewardj   copy the default value there, so that it is returned when not
862e93c50dc50235189661b70e3f27a4098d5cccccsewardj   running on Valgrind.  Since %EAX points to a block of mem
872e93c50dc50235189661b70e3f27a4098d5cccccsewardj   containing the args, you can pass as many args as you want like
882e93c50dc50235189661b70e3f27a4098d5cccccsewardj   this.  Currently this is set up to deal with 4 args since that's
892e93c50dc50235189661b70e3f27a4098d5cccccsewardj   the max that we appear to need (pthread_create).
90de4a1d01951937632098a6cda45859afa587a06fsewardj*/
912e93c50dc50235189661b70e3f27a4098d5cccccsewardj#define VALGRIND_MAGIC_SEQUENCE(                                        \
922e93c50dc50235189661b70e3f27a4098d5cccccsewardj        _zzq_rlval,   /* result lvalue */                               \
932e93c50dc50235189661b70e3f27a4098d5cccccsewardj        _zzq_default, /* result returned when running on real CPU */    \
942e93c50dc50235189661b70e3f27a4098d5cccccsewardj        _zzq_request, /* request code */                                \
952e93c50dc50235189661b70e3f27a4098d5cccccsewardj        _zzq_arg1,    /* request first param */                         \
962e93c50dc50235189661b70e3f27a4098d5cccccsewardj        _zzq_arg2,    /* request second param */                        \
972e93c50dc50235189661b70e3f27a4098d5cccccsewardj        _zzq_arg3,    /* request third param */                         \
982e93c50dc50235189661b70e3f27a4098d5cccccsewardj        _zzq_arg4     /* request fourth param */ )                      \
992e93c50dc50235189661b70e3f27a4098d5cccccsewardj                                                                        \
1002e93c50dc50235189661b70e3f27a4098d5cccccsewardj  { volatile unsigned int _zzq_args[5];                                 \
10118d7513cc08bf982711c8a22b70d56af6aa87b33sewardj    _zzq_args[0] = (volatile unsigned int)(_zzq_request);               \
10218d7513cc08bf982711c8a22b70d56af6aa87b33sewardj    _zzq_args[1] = (volatile unsigned int)(_zzq_arg1);                  \
10318d7513cc08bf982711c8a22b70d56af6aa87b33sewardj    _zzq_args[2] = (volatile unsigned int)(_zzq_arg2);                  \
10418d7513cc08bf982711c8a22b70d56af6aa87b33sewardj    _zzq_args[3] = (volatile unsigned int)(_zzq_arg3);                  \
10518d7513cc08bf982711c8a22b70d56af6aa87b33sewardj    _zzq_args[4] = (volatile unsigned int)(_zzq_arg4);                  \
1062e93c50dc50235189661b70e3f27a4098d5cccccsewardj    asm volatile("movl %1, %%eax\n\t"                                   \
1072e93c50dc50235189661b70e3f27a4098d5cccccsewardj                 "movl %2, %%edx\n\t"                                   \
1082e93c50dc50235189661b70e3f27a4098d5cccccsewardj                 "roll $29, %%eax ; roll $3, %%eax\n\t"                 \
1092e93c50dc50235189661b70e3f27a4098d5cccccsewardj                 "rorl $27, %%eax ; rorl $5, %%eax\n\t"                 \
1102e93c50dc50235189661b70e3f27a4098d5cccccsewardj                 "roll $13, %%eax ; roll $19, %%eax\n\t"                \
1112e93c50dc50235189661b70e3f27a4098d5cccccsewardj                 "movl %%edx, %0\t"                                     \
1122e93c50dc50235189661b70e3f27a4098d5cccccsewardj                 : "=r" (_zzq_rlval)                                    \
1132e93c50dc50235189661b70e3f27a4098d5cccccsewardj                 : "r" (&_zzq_args[0]), "r" (_zzq_default)              \
1142e93c50dc50235189661b70e3f27a4098d5cccccsewardj                 : "eax", "edx", "cc", "memory"                         \
1152e93c50dc50235189661b70e3f27a4098d5cccccsewardj                );                                                      \
1162e93c50dc50235189661b70e3f27a4098d5cccccsewardj  }
11737091fb739760631f436043c47de612cf9fd2dd1sewardj#else  /* NVALGRIND */
11837091fb739760631f436043c47de612cf9fd2dd1sewardj/* Define NVALGRIND to completely remove the Valgrind magic sequence
11937091fb739760631f436043c47de612cf9fd2dd1sewardj   from the compiled code (analogous to NDEBUG's effects on
12037091fb739760631f436043c47de612cf9fd2dd1sewardj   assert())  */
12137091fb739760631f436043c47de612cf9fd2dd1sewardj#define VALGRIND_MAGIC_SEQUENCE(					\
12237091fb739760631f436043c47de612cf9fd2dd1sewardj        _zzq_rlval,   /* result lvalue */				\
12337091fb739760631f436043c47de612cf9fd2dd1sewardj        _zzq_default, /* result returned when running on real CPU */	\
12437091fb739760631f436043c47de612cf9fd2dd1sewardj        _zzq_request, /* request code */				\
12537091fb739760631f436043c47de612cf9fd2dd1sewardj        _zzq_arg1,    /* request first param */				\
12637091fb739760631f436043c47de612cf9fd2dd1sewardj        _zzq_arg2,    /* request second param */			\
12737091fb739760631f436043c47de612cf9fd2dd1sewardj        _zzq_arg3,    /* request third param */				\
12837091fb739760631f436043c47de612cf9fd2dd1sewardj        _zzq_arg4     /* request fourth param */ )			\
12937091fb739760631f436043c47de612cf9fd2dd1sewardj   {									\
13037091fb739760631f436043c47de612cf9fd2dd1sewardj      (_zzq_rlval) = (_zzq_default);					\
13137091fb739760631f436043c47de612cf9fd2dd1sewardj   }
13237091fb739760631f436043c47de612cf9fd2dd1sewardj#endif /* NVALGRIND */
1332e93c50dc50235189661b70e3f27a4098d5cccccsewardj
1342e93c50dc50235189661b70e3f27a4098d5cccccsewardj/* Some request codes.  There are many more of these, but most are not
1352e93c50dc50235189661b70e3f27a4098d5cccccsewardj   exposed to end-user view.  These are the public ones, all of the
136e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   form 0x1000 + small_number.
137d799418996812817596beaa8b59563e3f3cb2ddanjn
138d799418996812817596beaa8b59563e3f3cb2ddanjn   Core ones are in the range 0x00000000--0x0000ffff.  The non-public ones
139d799418996812817596beaa8b59563e3f3cb2ddanjn   start at 0x2000.
1402e93c50dc50235189661b70e3f27a4098d5cccccsewardj*/
1412e93c50dc50235189661b70e3f27a4098d5cccccsewardj
1424c791211835f0e90cbde578187c06e563de3b023njn#define VG_USERREQ_SKIN_BASE(a,b) \
1434c791211835f0e90cbde578187c06e563de3b023njn   ((unsigned int)(((a)&0xff) << 24 | ((b)&0xff) << 16))
1444c791211835f0e90cbde578187c06e563de3b023njn#define VG_IS_SKIN_USERREQ(a, b, v) \
1454c791211835f0e90cbde578187c06e563de3b023njn   (VG_USERREQ_SKIN_BASE(a,b) == ((v) & 0xffff0000))
14634042515c1715b3e0c5c0a5e0bd033e9d4858f01sewardj
147e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjntypedef
1484c791211835f0e90cbde578187c06e563de3b023njn   enum { VG_USERREQ__RUNNING_ON_VALGRIND  = 0x1001,
1494c791211835f0e90cbde578187c06e563de3b023njn          VG_USERREQ__DISCARD_TRANSLATIONS = 0x1002,
1503e88418f808bf2840646504481d6a5be1df16541njn
1513e88418f808bf2840646504481d6a5be1df16541njn          /* These allow any function of 0--3 args to be called from the
1523e88418f808bf2840646504481d6a5be1df16541njn             simulated CPU but run on the real CPU */
1534c791211835f0e90cbde578187c06e563de3b023njn          VG_USERREQ__CLIENT_CALL0 = 0x1101,
1544c791211835f0e90cbde578187c06e563de3b023njn          VG_USERREQ__CLIENT_CALL1 = 0x1102,
1554c791211835f0e90cbde578187c06e563de3b023njn          VG_USERREQ__CLIENT_CALL2 = 0x1103,
1564c791211835f0e90cbde578187c06e563de3b023njn          VG_USERREQ__CLIENT_CALL3 = 0x1104,
1573e88418f808bf2840646504481d6a5be1df16541njn
15847363aba8fa03b094195bca99fc232ce5f85605dnjn          /* Can be useful in regression testing suites -- eg. can send
15947363aba8fa03b094195bca99fc232ce5f85605dnjn             Valgrind's output to /dev/null and still count errors. */
1604c791211835f0e90cbde578187c06e563de3b023njn          VG_USERREQ__COUNT_ERRORS = 0x1201,
16147363aba8fa03b094195bca99fc232ce5f85605dnjn
1627cc9c239f785f2903b597cdb34418bed42d25331nethercote          /* These are useful and can be interpreted by any tool that tracks
163d799418996812817596beaa8b59563e3f3cb2ddanjn             malloc() et al, by using vg_replace_malloc.c. */
164d799418996812817596beaa8b59563e3f3cb2ddanjn          VG_USERREQ__MALLOCLIKE_BLOCK = 0x1301,
165d799418996812817596beaa8b59563e3f3cb2ddanjn          VG_USERREQ__FREELIKE_BLOCK   = 0x1302,
166d799418996812817596beaa8b59563e3f3cb2ddanjn
16739de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge          /* Allow printfs to valgrind log. */
16839de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge          VG_USERREQ__PRINTF = 0x1401,
16939de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge          VG_USERREQ__PRINTF_BACKTRACE = 0x1402,
170e49d8e7dfd3a9c96feb9935b5920973dfc0b170anjn   } Vg_ClientRequest;
1712e93c50dc50235189661b70e3f27a4098d5cccccsewardj
172c9b365507e9bd5d500476e3e83f4d30f9c68a351mueller#ifndef __GNUC__
173c9b365507e9bd5d500476e3e83f4d30f9c68a351mueller#define __extension__
174c9b365507e9bd5d500476e3e83f4d30f9c68a351mueller#endif
1752e93c50dc50235189661b70e3f27a4098d5cccccsewardj
1762e93c50dc50235189661b70e3f27a4098d5cccccsewardj/* Returns 1 if running on Valgrind, 0 if running on the real CPU.
1772e93c50dc50235189661b70e3f27a4098d5cccccsewardj   Currently implemented but untested. */
178c9b365507e9bd5d500476e3e83f4d30f9c68a351mueller#define RUNNING_ON_VALGRIND  __extension__                         \
1792e93c50dc50235189661b70e3f27a4098d5cccccsewardj   ({unsigned int _qzz_res;                                        \
1802e93c50dc50235189661b70e3f27a4098d5cccccsewardj    VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0 /* returned if not */,     \
1812e93c50dc50235189661b70e3f27a4098d5cccccsewardj                            VG_USERREQ__RUNNING_ON_VALGRIND,       \
1822e93c50dc50235189661b70e3f27a4098d5cccccsewardj                            0, 0, 0, 0);                           \
1832e93c50dc50235189661b70e3f27a4098d5cccccsewardj    _qzz_res;                                                      \
184de4a1d01951937632098a6cda45859afa587a06fsewardj   })
185de4a1d01951937632098a6cda45859afa587a06fsewardj
186de4a1d01951937632098a6cda45859afa587a06fsewardj
18718d7513cc08bf982711c8a22b70d56af6aa87b33sewardj/* Discard translation of code in the range [_qzz_addr .. _qzz_addr +
18818d7513cc08bf982711c8a22b70d56af6aa87b33sewardj   _qzz_len - 1].  Useful if you are debugging a JITter or some such,
18918d7513cc08bf982711c8a22b70d56af6aa87b33sewardj   since it provides a way to make sure valgrind will retranslate the
19018d7513cc08bf982711c8a22b70d56af6aa87b33sewardj   invalidated area.  Returns no value. */
19118d7513cc08bf982711c8a22b70d56af6aa87b33sewardj#define VALGRIND_DISCARD_TRANSLATIONS(_qzz_addr,_qzz_len)          \
19218d7513cc08bf982711c8a22b70d56af6aa87b33sewardj   {unsigned int _qzz_res;                                         \
19318d7513cc08bf982711c8a22b70d56af6aa87b33sewardj    VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0,                           \
19418d7513cc08bf982711c8a22b70d56af6aa87b33sewardj                            VG_USERREQ__DISCARD_TRANSLATIONS,      \
19518d7513cc08bf982711c8a22b70d56af6aa87b33sewardj                            _qzz_addr, _qzz_len, 0, 0);            \
19618d7513cc08bf982711c8a22b70d56af6aa87b33sewardj   }
19718d7513cc08bf982711c8a22b70d56af6aa87b33sewardj
19839de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge#ifndef NVALGRIND
19939de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge
200a09a1b5d4e02b7451345dac00f2d321d1b4b2ccefitzhardingeint VALGRIND_PRINTF(const char *format, ...)
201a09a1b5d4e02b7451345dac00f2d321d1b4b2ccefitzhardinge   __attribute__((format(__printf__, 1, 2)));
20239de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge__attribute__((weak))
20339de4b473801ab10a48e356cddc863212dd28cd1fitzhardingeint
204a09a1b5d4e02b7451345dac00f2d321d1b4b2ccefitzhardingeVALGRIND_PRINTF(const char *format, ...)
20539de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge{
20639de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge   unsigned int _qzz_res;
20739de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge   va_list vargs;
20839de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge   va_start(vargs, format);
20939de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge   VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, VG_USERREQ__PRINTF,
21039de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge                           (unsigned int)format, (unsigned int)vargs, 0, 0);
21139de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge   va_end(vargs);
21239de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge   return _qzz_res;
21339de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge}
21439de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge
215a09a1b5d4e02b7451345dac00f2d321d1b4b2ccefitzhardingeint VALGRIND_PRINTF_BACKTRACE(const char *format, ...)
216a09a1b5d4e02b7451345dac00f2d321d1b4b2ccefitzhardinge   __attribute__((format(__printf__, 1, 2)));
21739de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge__attribute__((weak))
21839de4b473801ab10a48e356cddc863212dd28cd1fitzhardingeint
219a09a1b5d4e02b7451345dac00f2d321d1b4b2ccefitzhardingeVALGRIND_PRINTF_BACKTRACE(const char *format, ...)
22039de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge{
22139de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge   unsigned int _qzz_res;
22239de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge   va_list vargs;
22339de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge   va_start(vargs, format);
22439de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge   VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, VG_USERREQ__PRINTF_BACKTRACE,
22539de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge                           (unsigned int)format, (unsigned int)vargs, 0, 0);
22639de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge   va_end(vargs);
22739de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge   return _qzz_res;
22839de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge}
22939de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge
23039de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge#else /* NVALGRIND */
23139de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge
23239de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge#define VALGRIND_PRINTF(...)
23339de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge#define VALGRIND_PRINTF_BACKTRACE(...)
23439de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge
23539de4b473801ab10a48e356cddc863212dd28cd1fitzhardinge#endif /* NVALGRIND */
23618d7513cc08bf982711c8a22b70d56af6aa87b33sewardj
2373e88418f808bf2840646504481d6a5be1df16541njn/* These requests allow control to move from the simulated CPU to the
2383e88418f808bf2840646504481d6a5be1df16541njn   real CPU, calling an arbitary function */
239057c65f4c980e1e46da862df6a84ed097249bbecnjn#define VALGRIND_NON_SIMD_CALL0(_qyy_fn)                       \
2403e88418f808bf2840646504481d6a5be1df16541njn   ({unsigned int _qyy_res;                                    \
2413e88418f808bf2840646504481d6a5be1df16541njn    VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */,  \
2423e88418f808bf2840646504481d6a5be1df16541njn                            VG_USERREQ__CLIENT_CALL0,          \
2433e88418f808bf2840646504481d6a5be1df16541njn                            _qyy_fn,                           \
2443e88418f808bf2840646504481d6a5be1df16541njn                            0, 0, 0);                          \
2453e88418f808bf2840646504481d6a5be1df16541njn    _qyy_res;                                                  \
2463e88418f808bf2840646504481d6a5be1df16541njn   })
2473e88418f808bf2840646504481d6a5be1df16541njn
248057c65f4c980e1e46da862df6a84ed097249bbecnjn#define VALGRIND_NON_SIMD_CALL1(_qyy_fn, _qyy_arg1)            \
2493e88418f808bf2840646504481d6a5be1df16541njn   ({unsigned int _qyy_res;                                    \
2503e88418f808bf2840646504481d6a5be1df16541njn    VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */,  \
2513e88418f808bf2840646504481d6a5be1df16541njn                            VG_USERREQ__CLIENT_CALL1,          \
2523e88418f808bf2840646504481d6a5be1df16541njn                            _qyy_fn,                           \
2533e88418f808bf2840646504481d6a5be1df16541njn                            _qyy_arg1, 0, 0);                  \
2543e88418f808bf2840646504481d6a5be1df16541njn    _qyy_res;                                                  \
2553e88418f808bf2840646504481d6a5be1df16541njn   })
2563e88418f808bf2840646504481d6a5be1df16541njn
257057c65f4c980e1e46da862df6a84ed097249bbecnjn#define VALGRIND_NON_SIMD_CALL2(_qyy_fn, _qyy_arg1, _qyy_arg2) \
2583e88418f808bf2840646504481d6a5be1df16541njn   ({unsigned int _qyy_res;                                    \
2593e88418f808bf2840646504481d6a5be1df16541njn    VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */,  \
2603e88418f808bf2840646504481d6a5be1df16541njn                            VG_USERREQ__CLIENT_CALL2,          \
2613e88418f808bf2840646504481d6a5be1df16541njn                            _qyy_fn,                           \
2623e88418f808bf2840646504481d6a5be1df16541njn                            _qyy_arg1, _qyy_arg2, 0);          \
2633e88418f808bf2840646504481d6a5be1df16541njn    _qyy_res;                                                  \
2643e88418f808bf2840646504481d6a5be1df16541njn   })
2653e88418f808bf2840646504481d6a5be1df16541njn
266057c65f4c980e1e46da862df6a84ed097249bbecnjn#define VALGRIND_NON_SIMD_CALL3(_qyy_fn, _qyy_arg1, _qyy_arg2, _qyy_arg3)  \
2673e88418f808bf2840646504481d6a5be1df16541njn   ({unsigned int _qyy_res;                                          \
2683e88418f808bf2840646504481d6a5be1df16541njn    VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */,        \
2693e88418f808bf2840646504481d6a5be1df16541njn                            VG_USERREQ__CLIENT_CALL3,                \
2703e88418f808bf2840646504481d6a5be1df16541njn                            _qyy_fn,                                 \
2713e88418f808bf2840646504481d6a5be1df16541njn                            _qyy_arg1, _qyy_arg2, _qyy_arg3);        \
2723e88418f808bf2840646504481d6a5be1df16541njn    _qyy_res;                                                        \
2733e88418f808bf2840646504481d6a5be1df16541njn   })
2743e88418f808bf2840646504481d6a5be1df16541njn
2753e88418f808bf2840646504481d6a5be1df16541njn
2767cc9c239f785f2903b597cdb34418bed42d25331nethercote/* Counts the number of errors that have been recorded by a tool.  Nb:
2777cc9c239f785f2903b597cdb34418bed42d25331nethercote   the tool must record the errors with VG_(maybe_record_error)() or
27847363aba8fa03b094195bca99fc232ce5f85605dnjn   VG_(unique_error)() for them to be counted. */
27947363aba8fa03b094195bca99fc232ce5f85605dnjn#define VALGRIND_COUNT_ERRORS                                           \
28047363aba8fa03b094195bca99fc232ce5f85605dnjn   ({unsigned int _qyy_res;                                             \
28147363aba8fa03b094195bca99fc232ce5f85605dnjn    VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */,           \
28247363aba8fa03b094195bca99fc232ce5f85605dnjn                            VG_USERREQ__COUNT_ERRORS,                   \
28347363aba8fa03b094195bca99fc232ce5f85605dnjn                            0, 0, 0, 0);                                \
28447363aba8fa03b094195bca99fc232ce5f85605dnjn    _qyy_res;                                                           \
28547363aba8fa03b094195bca99fc232ce5f85605dnjn   })
28647363aba8fa03b094195bca99fc232ce5f85605dnjn
287d799418996812817596beaa8b59563e3f3cb2ddanjn/* Mark a block of memory as having been allocated by a malloc()-like
288d799418996812817596beaa8b59563e3f3cb2ddanjn   function.  `addr' is the start of the usable block (ie. after any
289d799418996812817596beaa8b59563e3f3cb2ddanjn   redzone) `rzB' is redzone size if the allocator can apply redzones;
290d799418996812817596beaa8b59563e3f3cb2ddanjn   use '0' if not.  Adding redzones makes it more likely Valgrind will spot
291d799418996812817596beaa8b59563e3f3cb2ddanjn   block overruns.  `is_zeroed' indicates if the memory is zeroed, as it is
292d799418996812817596beaa8b59563e3f3cb2ddanjn   for calloc().  Put it immediately after the point where a block is
293d799418996812817596beaa8b59563e3f3cb2ddanjn   allocated.
294d799418996812817596beaa8b59563e3f3cb2ddanjn
295d799418996812817596beaa8b59563e3f3cb2ddanjn   If you're allocating memory via superblocks, and then handing out small
296d799418996812817596beaa8b59563e3f3cb2ddanjn   chunks of each superblock, if you don't have redzones on your small
297d799418996812817596beaa8b59563e3f3cb2ddanjn   blocks, it's worth marking the superblock with VALGRIND_MAKE_NOACCESS
298d799418996812817596beaa8b59563e3f3cb2ddanjn   when it's created, so that block overruns are detected.  But if you can
299d799418996812817596beaa8b59563e3f3cb2ddanjn   put redzones on, it's probably better to not do this, so that messages
300d799418996812817596beaa8b59563e3f3cb2ddanjn   for small overruns are described in terms of the small block rather than
301d799418996812817596beaa8b59563e3f3cb2ddanjn   the superblock (but if you have a big overrun that skips over a redzone,
302d799418996812817596beaa8b59563e3f3cb2ddanjn   you could miss an error this way).  See memcheck/tests/custom_alloc.c
303d799418996812817596beaa8b59563e3f3cb2ddanjn   for an example.
304d799418996812817596beaa8b59563e3f3cb2ddanjn
305d799418996812817596beaa8b59563e3f3cb2ddanjn   Nb: block must be freed via a free()-like function specified
306d799418996812817596beaa8b59563e3f3cb2ddanjn   with VALGRIND_FREELIKE_BLOCK or mismatch errors will occur. */
307d799418996812817596beaa8b59563e3f3cb2ddanjn#define VALGRIND_MALLOCLIKE_BLOCK(addr, sizeB, rzB, is_zeroed)     \
308d799418996812817596beaa8b59563e3f3cb2ddanjn   {unsigned int _qzz_res;                                         \
309d799418996812817596beaa8b59563e3f3cb2ddanjn    VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0,                           \
310d799418996812817596beaa8b59563e3f3cb2ddanjn                            VG_USERREQ__MALLOCLIKE_BLOCK,          \
311d799418996812817596beaa8b59563e3f3cb2ddanjn                            addr, sizeB, rzB, is_zeroed);          \
312d799418996812817596beaa8b59563e3f3cb2ddanjn   }
313d799418996812817596beaa8b59563e3f3cb2ddanjn
314d799418996812817596beaa8b59563e3f3cb2ddanjn/* Mark a block of memory as having been freed by a free()-like function.
315d799418996812817596beaa8b59563e3f3cb2ddanjn   `rzB' is redzone size;  it must match that given to
316d799418996812817596beaa8b59563e3f3cb2ddanjn   VALGRIND_MALLOCLIKE_BLOCK.  Memory not freed will be detected by the leak
317d799418996812817596beaa8b59563e3f3cb2ddanjn   checker.  Put it immediately after the point where the block is freed. */
318d799418996812817596beaa8b59563e3f3cb2ddanjn#define VALGRIND_FREELIKE_BLOCK(addr, rzB)                         \
319d799418996812817596beaa8b59563e3f3cb2ddanjn   {unsigned int _qzz_res;                                         \
320d799418996812817596beaa8b59563e3f3cb2ddanjn    VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0,                           \
321d799418996812817596beaa8b59563e3f3cb2ddanjn                            VG_USERREQ__FREELIKE_BLOCK,            \
322d799418996812817596beaa8b59563e3f3cb2ddanjn                            addr, rzB, 0, 0);                      \
323d799418996812817596beaa8b59563e3f3cb2ddanjn   }
324d799418996812817596beaa8b59563e3f3cb2ddanjn
3253e88418f808bf2840646504481d6a5be1df16541njn#endif   /* __VALGRIND_H */
326