2 /*--------------------------------------------------------------------*/
3 /*--- Header included by every tool C file. pub_tool_basics.h ---*/
4 /*--------------------------------------------------------------------*/
7 This file is part of Valgrind, a dynamic binary instrumentation
10 Copyright (C) 2000-2010 Julian Seward
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.
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.
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
28 The GNU General Public License is contained in the file COPYING.
31 #ifndef __PUB_TOOL_BASICS_H
32 #define __PUB_TOOL_BASICS_H
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 //--------------------------------------------------------------------
41 /* ---------------------------------------------------------------------
42 Other headers to include
43 ------------------------------------------------------------------ */
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"
53 #include <l4/util/util.h>
56 /* ---------------------------------------------------------------------
58 ------------------------------------------------------------------ */
60 // All symbols externally visible from Valgrind are prefixed
61 // as specified here to avoid namespace conflict problems.
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
71 #define VGAPPEND(str1,str2) str1##str2
73 #define VG_(str) VGAPPEND(vgPlain_, str)
74 #define ML_(str) VGAPPEND(vgModuleLocal_, str)
77 /* ---------------------------------------------------------------------
79 ------------------------------------------------------------------ */
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.
84 // - http://www.opengroup.org/onlinepubs/009695399/basedefs/sys/types.h.html
85 // - http://www.opengroup.org/onlinepubs/009695399/basedefs/stddef.h.html
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
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
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
105 // Our equivalent of POSIX 'ptrdiff_t':
106 // - ptrdiff_t is a "signed integer type of the result of subtracting two
108 // We use it for memory offsets, eg. the offset into a memory block.
109 typedef Word PtrdiffT; // 32 64
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
124 typedef Long Off64T; // 64 64
127 # define NULL ((void*)0)
130 /* This is just too useful to not have around the place somewhere. */
131 typedef struct { UWord uw1; UWord uw2; } UWordPair;
134 /* ---------------------------------------------------------------------
136 ------------------------------------------------------------------ */
138 // These probably shouldn't be here, but moving them to their logical
139 // modules results in a lot more #includes...
141 /* ThreadIds are simply indices into the VG_(threads)[] array. */
142 typedef UInt ThreadId;
144 /* An abstraction of syscall return values.
146 When _isError == False,
147 _val holds the return value.
148 When _isError == True,
149 _err holds the error code.
152 _res is the POSIX result of the syscall.
153 _err is the corresponding errno value.
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.
166 Interpretation depends on _mode:
168 these can never 'fail' (apparently). The result of the
169 syscall is a single host word, _wLO.
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.
179 #if defined(VGO_linux)
186 #elif defined(VGO_aix5)
194 #elif defined(VGO_darwin)
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
210 #elif defined(VGO_l4re)
223 /* ---- And now some basic accessor functions for it. ---- */
225 #if defined(VGO_linux)
227 static inline Bool sr_isError ( SysRes sr ) {
230 static inline UWord sr_Res ( SysRes sr ) {
231 return sr._isError ? 0 : sr._val;
233 static inline UWord sr_ResHI ( SysRes sr ) {
236 static inline UWord sr_Err ( SysRes sr ) {
237 return sr._isError ? sr._val : 0;
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));
245 #elif defined(VGO_aix5)
246 # error "need to define SysRes accessors on AIX5 (copy from 3.4.1 sources)"
249 #elif defined(VGO_darwin)
251 static inline Bool sr_isError ( SysRes sr ) {
253 case SysRes_UNIX_ERR: return True;
254 default: return False;
255 /* should check tags properly and assert here, but we can't here */
259 static inline UWord sr_Res ( SysRes sr ) {
263 case SysRes_UNIX_OK: return sr._wLO;
264 default: return 0; /* should assert, but we can't here */
268 static inline UWord sr_ResHI ( SysRes sr ) {
270 case SysRes_UNIX_OK: return sr._wHI;
271 default: return 0; /* should assert, but we can't here */
275 static inline UWord sr_Err ( SysRes sr ) {
277 case SysRes_UNIX_ERR: return sr._wLO;
278 default: return 0; /* should assert, but we can't here */
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;
287 #elif defined(VGO_l4re)
288 static inline Bool sr_isError ( SysRes sr ) {
291 static inline UWord sr_Res ( SysRes sr ) {
292 return sr._isError ? 0 : sr._res;
294 static inline UWord sr_ResHI ( SysRes sr ) {
297 static inline UWord sr_Err ( SysRes sr ) {
298 return sr._isError ? sr._err : 0;
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));
312 /* ---------------------------------------------------------------------
313 Miscellaneous (word size, endianness, regparmness, stringification)
314 ------------------------------------------------------------------ */
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
322 #undef VG_LITTLEENDIAN
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
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) /* */
343 #define VG_STRINGIFZ(__str) #__str
344 #define VG_STRINGIFY(__str) VG_STRINGIFZ(__str)
346 // Where to send bug reports to.
347 #define VG_BUGS_TO "www.valgrind.org"
349 /* Branch prediction hints. */
350 #if defined(__GNUC__)
351 # define LIKELY(x) __builtin_expect(!!(x), 1)
352 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
354 # define LIKELY(x) (x)
355 # define UNLIKELY(x) (x)
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)))
365 #define PRINTF_CHECK(x, y)
369 #endif /* __PUB_TOOL_BASICS_H */
371 /*--------------------------------------------------------------------*/
373 /*--------------------------------------------------------------------*/