]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/valgrind/src/valgrind-3.6.0-svn/include/pub_tool_basics.h
update
[l4.git] / l4 / pkg / valgrind / src / valgrind-3.6.0-svn / include / pub_tool_basics.h
1
2 /*--------------------------------------------------------------------*/
3 /*--- Header included by every tool C file.      pub_tool_basics.h ---*/
4 /*--------------------------------------------------------------------*/
5
6 /*
7    This file is part of Valgrind, a dynamic binary instrumentation
8    framework.
9
10    Copyright (C) 2000-2010 Julian Seward 
11       jseward@acm.org
12
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License as
15    published by the Free Software Foundation; either version 2 of the
16    License, or (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful, but
19    WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21    General Public License for more details.
22
23    You should have received a copy of the GNU General Public License
24    along with this program; if not, write to the Free Software
25    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26    02111-1307, USA.
27
28    The GNU General Public License is contained in the file COPYING.
29 */
30
31 #ifndef __PUB_TOOL_BASICS_H
32 #define __PUB_TOOL_BASICS_H
33
34 //--------------------------------------------------------------------
35 // PURPOSE: This header should be imported by every single C file in
36 // tools.  It contains the basic types and other things needed everywhere.
37 // There is no corresponding C file because this isn't a module
38 // containing executable code, it's all just declarations.
39 //--------------------------------------------------------------------
40
41 /* ---------------------------------------------------------------------
42    Other headers to include
43    ------------------------------------------------------------------ */
44
45 // VEX defines Char, UChar, Short, UShort, Int, UInt, Long, ULong,
46 // Addr32, Addr64, HWord, HChar, Bool, False and True.
47 #include "libvex_basictypes.h"
48
49 // For varargs types
50 #include <stdarg.h>
51
52 #ifdef VGO_l4re
53 #include <l4/util/util.h>
54 #endif
55
56 /* ---------------------------------------------------------------------
57    symbol prefixing
58    ------------------------------------------------------------------ */
59  
60 // All symbols externally visible from Valgrind are prefixed
61 // as specified here to avoid namespace conflict problems.
62 //
63 // VG_ is for symbols exported from modules.  ML_ (module-local) is
64 // for symbols which are not intended to be visible outside modules,
65 // but which cannot be declared as C 'static's since they need to be
66 // visible across C files within a given module.  It is a mistake for
67 // a ML_ name to appear in a pub_core_*.h or pub_tool_*.h file.
68 // Likewise it is a mistake for a VG_ name to appear in a priv_*.h
69 // file. 
70
71 #define VGAPPEND(str1,str2) str1##str2
72
73 #define VG_(str)    VGAPPEND(vgPlain_,          str)
74 #define ML_(str)    VGAPPEND(vgModuleLocal_,    str)
75
76
77 /* ---------------------------------------------------------------------
78    builtin types
79    ------------------------------------------------------------------ */
80
81 // By choosing the right types, we can get these right for 32-bit and 64-bit
82 // platforms without having to do any conditional compilation or anything.
83 // POSIX references:
84 // - http://www.opengroup.org/onlinepubs/009695399/basedefs/sys/types.h.html
85 // - http://www.opengroup.org/onlinepubs/009695399/basedefs/stddef.h.html
86 // 
87 // Size in bits on:                          32-bit archs   64-bit archs
88 //                                           ------------   ------------
89 typedef unsigned long          UWord;     // 32             64
90 typedef   signed long           Word;     // 32             64
91
92 // Addr is for holding an address.  AddrH was intended to be "Addr on the
93 // host", for the notional case where host word size != guest word size.
94 // But since the assumption that host arch == guest arch has become so
95 // deeply wired in, it's a pretty pointless distinction now.
96 typedef UWord                  Addr;      // 32             64
97 typedef UWord                  AddrH;     // 32             64
98
99 // Our equivalents of POSIX 'size_t' and 'ssize_t':
100 // - size_t is an "unsigned integer type of the result of the sizeof operator".
101 // - ssize_t is "used for a count of bytes or an error indication".
102 typedef UWord                  SizeT;     // 32             64
103 typedef  Word                 SSizeT;     // 32             64
104
105 // Our equivalent of POSIX 'ptrdiff_t':
106 // - ptrdiff_t is a "signed integer type of the result of subtracting two
107 //   pointers".
108 // We use it for memory offsets, eg. the offset into a memory block.
109 typedef  Word                 PtrdiffT;   // 32             64
110
111 // Our equivalent of POSIX 'off_t':
112 // - off_t is "used for file sizes".
113 // At one point we were using it for memory offsets, but PtrdiffT should be
114 // used in those cases.
115 // Nb: on Linux and AIX, off_t is a signed word-sized int.  On Darwin it's
116 // always a signed 64-bit int.  So we defined our own Off64T as well.
117 #if defined(VGO_linux) || defined(VGO_aix5) || defined(VGO_l4re)
118 typedef Word                   OffT;      // 32             64
119 #elif defined(VGO_darwin)
120 typedef Long                   OffT;      // 64             64
121 #else
122 #  error Unknown OS
123 #endif
124 typedef Long                 Off64T;      // 64             64
125
126 #if !defined(NULL)
127 #  define NULL ((void*)0)
128 #endif
129
130 /* This is just too useful to not have around the place somewhere. */
131 typedef  struct { UWord uw1; UWord uw2; }  UWordPair;
132
133
134 /* ---------------------------------------------------------------------
135    non-builtin types
136    ------------------------------------------------------------------ */
137
138 // These probably shouldn't be here, but moving them to their logical
139 // modules results in a lot more #includes...
140
141 /* ThreadIds are simply indices into the VG_(threads)[] array. */
142 typedef UInt ThreadId;
143
144 /* An abstraction of syscall return values.
145    Linux:
146       When _isError == False, 
147          _val holds the return value.
148       When _isError == True,  
149          _err holds the error code.
150
151    AIX:
152       _res is the POSIX result of the syscall.
153       _err is the corresponding errno value.
154       _isError === _err==0
155
156       Unlike on Linux, it is possible for _err to be nonzero (thus an
157       error has occurred), nevertheless _res is also nonzero.  AIX
158       userspace does not appear to consistently inspect _err to
159       determine whether or not an error has occurred.  For example,
160       sys_open() will return -1 for _val if a file cannot be opened,
161       as well as the relevant errno value in _err, but AIX userspace
162       then consults _val to figure out if the syscall failed, rather
163       than looking at _err.  Hence we need to represent them both.
164
165    Darwin:
166       Interpretation depends on _mode:
167       MACH, MDEP:
168          these can never 'fail' (apparently).  The result of the
169          syscall is a single host word, _wLO.
170       UNIX:
171          Can record a double-word error or a double-word result:
172          When _mode is SysRes_UNIX_OK,  _wHI:_wLO holds the result.
173          When _mode is SysRes_UNIX_ERR, _wHI:_wLO holds the error code.
174          Probably the high word of an error is always ignored by
175          userspace, but we have to record it, so that we can correctly
176          update both {R,E}DX and {R,E}AX (in guest state) given a SysRes,
177          if we're required to.
178 */
179 #if defined(VGO_linux)
180 typedef
181    struct {
182       UWord _val;
183       Bool  _isError;
184    }
185    SysRes;
186 #elif defined(VGO_aix5)
187 typedef
188    struct {
189       UWord _res;
190       UWord _err;
191       Bool  _isError;
192    }
193    SysRes;
194 #elif defined(VGO_darwin)
195 typedef
196    enum { 
197       SysRes_MACH=40,  // MACH, result is _wLO
198       SysRes_MDEP,     // MDEP, result is _wLO
199       SysRes_UNIX_OK,  // UNIX, success, result is _wHI:_wLO
200       SysRes_UNIX_ERR  // UNIX, error,   error  is _wHI:_wLO
201    }
202    SysResMode;
203 typedef
204    struct {
205       UWord _wLO;
206       UWord _wHI;
207       SysResMode _mode;
208    }
209    SysRes;
210 #elif defined(VGO_l4re)
211 typedef
212    struct {
213       UWord _res;
214       UWord _err;
215       Bool  _isError;
216    }
217    SysRes;
218 #else
219 #  error "Unknown OS"
220 #endif
221
222
223 /* ---- And now some basic accessor functions for it. ---- */
224
225 #if defined(VGO_linux)
226
227 static inline Bool sr_isError ( SysRes sr ) {
228    return sr._isError;
229 }
230 static inline UWord sr_Res ( SysRes sr ) {
231    return sr._isError ? 0 : sr._val;
232 }
233 static inline UWord sr_ResHI ( SysRes sr ) {
234    return 0;
235 }
236 static inline UWord sr_Err ( SysRes sr ) {
237    return sr._isError ? sr._val : 0;
238 }
239 static inline Bool sr_EQ ( SysRes sr1, SysRes sr2 ) {
240    return sr1._val == sr2._val 
241           && ((sr1._isError && sr2._isError) 
242               || (!sr1._isError && !sr2._isError));
243 }
244
245 #elif defined(VGO_aix5)
246 #  error "need to define SysRes accessors on AIX5 (copy from 3.4.1 sources)"
247
248
249 #elif defined(VGO_darwin)
250
251 static inline Bool sr_isError ( SysRes sr ) {
252    switch (sr._mode) {
253       case SysRes_UNIX_ERR: return True;
254       default:              return False;
255       /* should check tags properly and assert here, but we can't here */
256    }
257 }
258
259 static inline UWord sr_Res ( SysRes sr ) {
260    switch (sr._mode) {
261       case SysRes_MACH:
262       case SysRes_MDEP:
263       case SysRes_UNIX_OK: return sr._wLO;
264       default: return 0; /* should assert, but we can't here */
265    }
266 }
267
268 static inline UWord sr_ResHI ( SysRes sr ) {
269    switch (sr._mode) {
270       case SysRes_UNIX_OK: return sr._wHI;
271       default: return 0; /* should assert, but we can't here */
272    }
273 }
274
275 static inline UWord sr_Err ( SysRes sr ) {
276    switch (sr._mode) {
277       case SysRes_UNIX_ERR: return sr._wLO;
278       default: return 0; /* should assert, but we can't here */
279    }
280 }
281
282 static inline Bool sr_EQ ( SysRes sr1, SysRes sr2 ) {
283    return sr1._mode == sr2._mode
284           && sr1._wLO == sr2._wLO && sr1._wHI == sr2._wHI;
285 }
286
287 #elif defined(VGO_l4re)
288 static inline Bool sr_isError ( SysRes sr ) {
289    return sr._isError;
290 }
291 static inline UWord sr_Res ( SysRes sr ) {
292    return sr._isError ? 0 : sr._res;
293 }
294 static inline UWord sr_ResHI ( SysRes sr ) {
295    return 0;
296 }
297 static inline UWord sr_Err ( SysRes sr ) {
298    return sr._isError ? sr._err : 0;
299 }
300 static inline Bool sr_EQ ( SysRes sr1, SysRes sr2 ) {
301    return sr1._res == sr2._res
302           && ((sr1._isError && sr2._isError)
303               || (!sr1._isError && !sr2._isError));
304 }
305
306
307 #else
308 #  error "Unknown OS"
309 #endif
310
311
312 /* ---------------------------------------------------------------------
313    Miscellaneous (word size, endianness, regparmness, stringification)
314    ------------------------------------------------------------------ */
315
316 /* Word size: this is going to be either 4 or 8. */
317 // It should probably be in m_machine.
318 #define VG_WORDSIZE VEX_HOST_WORDSIZE
319
320 /* Endianness */
321 #undef VG_BIGENDIAN
322 #undef VG_LITTLEENDIAN
323
324 #if defined(VGA_x86) || defined(VGA_amd64) || defined (VGA_arm)
325 #  define VG_LITTLEENDIAN 1
326 #elif defined(VGA_ppc32) || defined(VGA_ppc64) || defined(VGA_s390x)
327 #  define VG_BIGENDIAN 1
328 #else
329 #  error Unknown arch
330 #endif
331
332 /* Regparmness */
333 #if defined(VGA_x86)
334 #  define VG_REGPARM(n)            __attribute__((regparm(n)))
335 #elif defined(VGA_amd64) || defined(VGA_ppc32) \
336       || defined(VGA_ppc64) || defined(VGA_arm) || defined(VGA_s390x)
337 #  define VG_REGPARM(n)            /* */
338 #else
339 #  error Unknown arch
340 #endif
341
342 /* Macro games */
343 #define VG_STRINGIFZ(__str)  #__str
344 #define VG_STRINGIFY(__str)  VG_STRINGIFZ(__str)
345
346 // Where to send bug reports to.
347 #define VG_BUGS_TO "www.valgrind.org"
348
349 /* Branch prediction hints. */
350 #if defined(__GNUC__)
351 #  define LIKELY(x)   __builtin_expect(!!(x), 1)
352 #  define UNLIKELY(x) __builtin_expect(!!(x), 0)
353 #else
354 #  define LIKELY(x)   (x)
355 #  define UNLIKELY(x) (x)
356 #endif
357
358 // printf format string checking for gcc.
359 // This feature has been supported since at least gcc version 2.95.
360 // For more information about the format attribute, see
361 // http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Function-Attributes.html.
362 #if defined(__GNUC__)
363 #define PRINTF_CHECK(x, y) __attribute__((format(__printf__, x, y)))
364 #else
365 #define PRINTF_CHECK(x, y)
366 #endif
367
368
369 #endif /* __PUB_TOOL_BASICS_H */
370
371 /*--------------------------------------------------------------------*/
372 /*--- end                                                          ---*/
373 /*--------------------------------------------------------------------*/