blob: 5a819c78aeb3d6bef5f5799bdbc21458ee359791 [file] [log] [blame]
sewardjde4a1d02002-03-22 01:27:54 +00001
2/*
3 This file is part of Valgrind, an x86 protected-mode emulator
4 designed for debugging and profiling binaries on x86-Unixes.
5
6 Copyright (C) 2000-2002 Julian Seward
7 jseward@acm.org
sewardjde4a1d02002-03-22 01:27:54 +00008
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 02111-1307, USA.
23
24 The GNU General Public License is contained in the file LICENSE.
25*/
26
27
28#ifndef __VALGRIND_H
29#define __VALGRIND_H
30
31
32/* This file is for inclusion into client (your!) code.
33
34 You can use these macros to manipulate and query memory permissions
35 inside your own programs.
36
37 The resulting executables will still run without Valgrind, just a
38 little bit more slowly than they otherwise would, but otherwise
39 unchanged.
40
41 When run on Valgrind with --client-perms=yes, Valgrind observes
42 these macro calls and takes appropriate action. When run on
43 Valgrind with --client-perms=no (the default), Valgrind observes
44 these macro calls but does not take any action as a result. */
45
46
47
48/* This defines the magic code sequence which the JITter spots and
49 handles magically. Don't look too closely at this; it will rot
sewardj2e93c502002-04-12 11:12:52 +000050 your brain. Valgrind dumps the result value in %EDX, so we first
51 copy the default value there, so that it is returned when not
52 running on Valgrind. Since %EAX points to a block of mem
53 containing the args, you can pass as many args as you want like
54 this. Currently this is set up to deal with 4 args since that's
55 the max that we appear to need (pthread_create).
sewardjde4a1d02002-03-22 01:27:54 +000056*/
sewardj2e93c502002-04-12 11:12:52 +000057#define VALGRIND_MAGIC_SEQUENCE( \
58 _zzq_rlval, /* result lvalue */ \
59 _zzq_default, /* result returned when running on real CPU */ \
60 _zzq_request, /* request code */ \
61 _zzq_arg1, /* request first param */ \
62 _zzq_arg2, /* request second param */ \
63 _zzq_arg3, /* request third param */ \
64 _zzq_arg4 /* request fourth param */ ) \
65 \
66 { volatile unsigned int _zzq_args[5]; \
sewardj18d75132002-05-16 11:06:21 +000067 _zzq_args[0] = (volatile unsigned int)(_zzq_request); \
68 _zzq_args[1] = (volatile unsigned int)(_zzq_arg1); \
69 _zzq_args[2] = (volatile unsigned int)(_zzq_arg2); \
70 _zzq_args[3] = (volatile unsigned int)(_zzq_arg3); \
71 _zzq_args[4] = (volatile unsigned int)(_zzq_arg4); \
sewardj2e93c502002-04-12 11:12:52 +000072 asm volatile("movl %1, %%eax\n\t" \
73 "movl %2, %%edx\n\t" \
74 "roll $29, %%eax ; roll $3, %%eax\n\t" \
75 "rorl $27, %%eax ; rorl $5, %%eax\n\t" \
76 "roll $13, %%eax ; roll $19, %%eax\n\t" \
77 "movl %%edx, %0\t" \
78 : "=r" (_zzq_rlval) \
79 : "r" (&_zzq_args[0]), "r" (_zzq_default) \
80 : "eax", "edx", "cc", "memory" \
81 ); \
82 }
83
84
85/* Some request codes. There are many more of these, but most are not
86 exposed to end-user view. These are the public ones, all of the
87 form 0x1000 + small_number.
88*/
89
90#define VG_USERREQ__MAKE_NOACCESS 0x1001
91#define VG_USERREQ__MAKE_WRITABLE 0x1002
92#define VG_USERREQ__MAKE_READABLE 0x1003
93#define VG_USERREQ__DISCARD 0x1004
94#define VG_USERREQ__CHECK_WRITABLE 0x1005
95#define VG_USERREQ__CHECK_READABLE 0x1006
96#define VG_USERREQ__MAKE_NOACCESS_STACK 0x1007
97#define VG_USERREQ__RUNNING_ON_VALGRIND 0x1008
sewardj18d75132002-05-16 11:06:21 +000098#define VG_USERREQ__DO_LEAK_CHECK 0x1009 /* untested */
99#define VG_USERREQ__DISCARD_TRANSLATIONS 0x100A
sewardjde4a1d02002-03-22 01:27:54 +0000100
101
102/* Client-code macros to manipulate the state of memory. */
103
104/* Mark memory at _qzz_addr as unaddressible and undefined for
105 _qzz_len bytes. Returns an int handle pertaining to the block
106 descriptions Valgrind will use in subsequent error messages. */
107#define VALGRIND_MAKE_NOACCESS(_qzz_addr,_qzz_len) \
108 ({unsigned int _qzz_res; \
sewardj2e93c502002-04-12 11:12:52 +0000109 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0 /* default return */, \
110 VG_USERREQ__MAKE_NOACCESS, \
111 _qzz_addr, _qzz_len, 0, 0); \
sewardjde4a1d02002-03-22 01:27:54 +0000112 _qzz_res; \
113 })
114
115/* Similarly, mark memory at _qzz_addr as addressible but undefined
116 for _qzz_len bytes. */
117#define VALGRIND_MAKE_WRITABLE(_qzz_addr,_qzz_len) \
118 ({unsigned int _qzz_res; \
sewardj2e93c502002-04-12 11:12:52 +0000119 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0 /* default return */, \
120 VG_USERREQ__MAKE_WRITABLE, \
sewardj57f12d32002-06-06 08:38:45 +0000121 _qzz_addr, _qzz_len, 0, 0); \
sewardjde4a1d02002-03-22 01:27:54 +0000122 _qzz_res; \
123 })
124
125/* Similarly, mark memory at _qzz_addr as addressible and defined
126 for _qzz_len bytes. */
127#define VALGRIND_MAKE_READABLE(_qzz_addr,_qzz_len) \
128 ({unsigned int _qzz_res; \
sewardj2e93c502002-04-12 11:12:52 +0000129 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0 /* default return */, \
130 VG_USERREQ__MAKE_READABLE, \
131 _qzz_addr, _qzz_len, 0, 0); \
sewardjde4a1d02002-03-22 01:27:54 +0000132 _qzz_res; \
133 })
134
135/* Discard a block-description-handle obtained from the above three
136 macros. After this, Valgrind will no longer be able to relate
137 addressing errors to the user-defined block associated with the
138 handle. The permissions settings associated with the handle remain
139 in place. Returns 1 for an invalid handle, 0 for a valid
140 handle. */
141#define VALGRIND_DISCARD(_qzz_blkindex) \
142 ({unsigned int _qzz_res; \
sewardj2e93c502002-04-12 11:12:52 +0000143 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0 /* default return */, \
144 VG_USERREQ__DISCARD, \
145 0, _qzz_blkindex, 0, 0); \
sewardjde4a1d02002-03-22 01:27:54 +0000146 _qzz_res; \
147 })
148
149
150
151/* Client-code macros to check the state of memory. */
152
153/* Check that memory at _qzz_addr is addressible for _qzz_len bytes.
154 If suitable addressibility is not established, Valgrind prints an
155 error message and returns the address of the first offending byte.
156 Otherwise it returns zero. */
sewardj2e93c502002-04-12 11:12:52 +0000157#define VALGRIND_CHECK_WRITABLE(_qzz_addr,_qzz_len) \
158 ({unsigned int _qzz_res; \
159 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
160 VG_USERREQ__CHECK_WRITABLE, \
161 _qzz_addr, _qzz_len, 0, 0); \
162 _qzz_res; \
sewardjde4a1d02002-03-22 01:27:54 +0000163 })
164
165/* Check that memory at _qzz_addr is addressible and defined for
166 _qzz_len bytes. If suitable addressibility and definedness are not
167 established, Valgrind prints an error message and returns the
168 address of the first offending byte. Otherwise it returns zero. */
sewardj2e93c502002-04-12 11:12:52 +0000169#define VALGRIND_CHECK_READABLE(_qzz_addr,_qzz_len) \
170 ({unsigned int _qzz_res; \
171 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
172 VG_USERREQ__CHECK_READABLE, \
173 _qzz_addr, _qzz_len, 0, 0); \
174 _qzz_res; \
sewardjde4a1d02002-03-22 01:27:54 +0000175 })
176
177
178/* Use this macro to force the definedness and addressibility of a
179 value to be checked. If suitable addressibility and definedness
180 are not established, Valgrind prints an error message and returns
181 the address of the first offending byte. Otherwise it returns
182 zero. */
sewardj2e93c502002-04-12 11:12:52 +0000183#define VALGRIND_CHECK_DEFINED(__lvalue) \
184 (void) \
185 VALGRIND_CHECK_READABLE( \
186 (volatile unsigned char *)&(__lvalue), \
sewardjde4a1d02002-03-22 01:27:54 +0000187 (unsigned int)(sizeof (__lvalue)))
188
189
190
191/* Mark memory, intended to be on the client's stack, at _qzz_addr as
192 unaddressible and undefined for _qzz_len bytes. Does not return a
193 value. The record associated with this setting will be
194 automatically removed by Valgrind when the containing routine
195 exits. */
sewardj2e93c502002-04-12 11:12:52 +0000196#define VALGRIND_MAKE_NOACCESS_STACK(_qzz_addr,_qzz_len) \
197 {unsigned int _qzz_res; \
198 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
199 VG_USERREQ__MAKE_NOACCESS_STACK, \
200 _qzz_addr, _qzz_len, 0, 0); \
201 }
202
203
204/* Returns 1 if running on Valgrind, 0 if running on the real CPU.
205 Currently implemented but untested. */
206#define RUNNING_ON_VALGRIND \
207 ({unsigned int _qzz_res; \
208 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0 /* returned if not */, \
209 VG_USERREQ__RUNNING_ON_VALGRIND, \
210 0, 0, 0, 0); \
211 _qzz_res; \
sewardjde4a1d02002-03-22 01:27:54 +0000212 })
213
214
sewardj2e93c502002-04-12 11:12:52 +0000215/* Mark memory, intended to be on the client's stack, at _qzz_addr as
216 unaddressible and undefined for _qzz_len bytes. Does not return a
217 value. The record associated with this setting will be
218 automatically removed by Valgrind when the containing routine
219 exits.
220
221 Currently implemented but untested.
222*/
223#define VALGRIND_DO_LEAK_CHECK \
224 {unsigned int _qzz_res; \
225 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
226 VG_USERREQ__DO_LEAK_CHECK, \
227 0, 0, 0, 0); \
228 }
229
sewardj18d75132002-05-16 11:06:21 +0000230
231/* Discard translation of code in the range [_qzz_addr .. _qzz_addr +
232 _qzz_len - 1]. Useful if you are debugging a JITter or some such,
233 since it provides a way to make sure valgrind will retranslate the
234 invalidated area. Returns no value. */
235#define VALGRIND_DISCARD_TRANSLATIONS(_qzz_addr,_qzz_len) \
236 {unsigned int _qzz_res; \
237 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
238 VG_USERREQ__DISCARD_TRANSLATIONS, \
239 _qzz_addr, _qzz_len, 0, 0); \
240 }
241
242
sewardjde4a1d02002-03-22 01:27:54 +0000243#endif