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 (at) 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 53 /* --------------------------------------------------------------------- 54 symbol prefixing 55 ------------------------------------------------------------------ */ 56 57 // All symbols externally visible from Valgrind are prefixed 58 // as specified here to avoid namespace conflict problems. 59 // 60 // VG_ is for symbols exported from modules. ML_ (module-local) is 61 // for symbols which are not intended to be visible outside modules, 62 // but which cannot be declared as C 'static's since they need to be 63 // visible across C files within a given module. It is a mistake for 64 // a ML_ name to appear in a pub_core_*.h or pub_tool_*.h file. 65 // Likewise it is a mistake for a VG_ name to appear in a priv_*.h 66 // file. 67 68 #define VGAPPEND(str1,str2) str1##str2 69 70 #define VG_(str) VGAPPEND(vgPlain_, str) 71 #define ML_(str) VGAPPEND(vgModuleLocal_, str) 72 73 74 /* --------------------------------------------------------------------- 75 builtin types 76 ------------------------------------------------------------------ */ 77 78 // By choosing the right types, we can get these right for 32-bit and 64-bit 79 // platforms without having to do any conditional compilation or anything. 80 // POSIX references: 81 // - http://www.opengroup.org/onlinepubs/009695399/basedefs/sys/types.h.html 82 // - http://www.opengroup.org/onlinepubs/009695399/basedefs/stddef.h.html 83 // 84 // Size in bits on: 32-bit archs 64-bit archs 85 // ------------ ------------ 86 typedef unsigned long UWord; // 32 64 87 typedef signed long Word; // 32 64 88 89 // Addr is for holding an address. AddrH was intended to be "Addr on the 90 // host", for the notional case where host word size != guest word size. 91 // But since the assumption that host arch == guest arch has become so 92 // deeply wired in, it's a pretty pointless distinction now. 93 typedef UWord Addr; // 32 64 94 typedef UWord AddrH; // 32 64 95 96 // Our equivalents of POSIX 'size_t' and 'ssize_t': 97 // - size_t is an "unsigned integer type of the result of the sizeof operator". 98 // - ssize_t is "used for a count of bytes or an error indication". 99 typedef UWord SizeT; // 32 64 100 typedef Word SSizeT; // 32 64 101 102 // Our equivalent of POSIX 'ptrdiff_t': 103 // - ptrdiff_t is a "signed integer type of the result of subtracting two 104 // pointers". 105 // We use it for memory offsets, eg. the offset into a memory block. 106 typedef Word PtrdiffT; // 32 64 107 108 // Our equivalent of POSIX 'off_t': 109 // - off_t is "used for file sizes". 110 // At one point we were using it for memory offsets, but PtrdiffT should be 111 // used in those cases. 112 // Nb: on Linux and AIX, off_t is a signed word-sized int. On Darwin it's 113 // always a signed 64-bit int. So we defined our own Off64T as well. 114 #if defined(VGO_linux) || defined(VGO_aix5) 115 typedef Word OffT; // 32 64 116 #elif defined(VGO_darwin) 117 typedef Long OffT; // 64 64 118 #else 119 # error Unknown OS 120 #endif 121 typedef Long Off64T; // 64 64 122 123 #if !defined(NULL) 124 # define NULL ((void*)0) 125 #endif 126 127 /* This is just too useful to not have around the place somewhere. */ 128 typedef struct { UWord uw1; UWord uw2; } UWordPair; 129 130 131 /* --------------------------------------------------------------------- 132 non-builtin types 133 ------------------------------------------------------------------ */ 134 135 // These probably shouldn't be here, but moving them to their logical 136 // modules results in a lot more #includes... 137 138 /* ThreadIds are simply indices into the VG_(threads)[] array. */ 139 typedef UInt ThreadId; 140 141 /* An abstraction of syscall return values. 142 Linux: 143 When _isError == False, 144 _val holds the return value. 145 When _isError == True, 146 _err holds the error code. 147 148 AIX: 149 _res is the POSIX result of the syscall. 150 _err is the corresponding errno value. 151 _isError === _err==0 152 153 Unlike on Linux, it is possible for _err to be nonzero (thus an 154 error has occurred), nevertheless _res is also nonzero. AIX 155 userspace does not appear to consistently inspect _err to 156 determine whether or not an error has occurred. For example, 157 sys_open() will return -1 for _val if a file cannot be opened, 158 as well as the relevant errno value in _err, but AIX userspace 159 then consults _val to figure out if the syscall failed, rather 160 than looking at _err. Hence we need to represent them both. 161 162 Darwin: 163 Interpretation depends on _mode: 164 MACH, MDEP: 165 these can never 'fail' (apparently). The result of the 166 syscall is a single host word, _wLO. 167 UNIX: 168 Can record a double-word error or a double-word result: 169 When _mode is SysRes_UNIX_OK, _wHI:_wLO holds the result. 170 When _mode is SysRes_UNIX_ERR, _wHI:_wLO holds the error code. 171 Probably the high word of an error is always ignored by 172 userspace, but we have to record it, so that we can correctly 173 update both {R,E}DX and {R,E}AX (in guest state) given a SysRes, 174 if we're required to. 175 */ 176 #if defined(VGO_linux) 177 typedef 178 struct { 179 UWord _val; 180 Bool _isError; 181 } 182 SysRes; 183 #elif defined(VGO_aix5) 184 typedef 185 struct { 186 UWord _res; 187 UWord _err; 188 Bool _isError; 189 } 190 SysRes; 191 #elif defined(VGO_darwin) 192 typedef 193 enum { 194 SysRes_MACH=40, // MACH, result is _wLO 195 SysRes_MDEP, // MDEP, result is _wLO 196 SysRes_UNIX_OK, // UNIX, success, result is _wHI:_wLO 197 SysRes_UNIX_ERR // UNIX, error, error is _wHI:_wLO 198 } 199 SysResMode; 200 typedef 201 struct { 202 UWord _wLO; 203 UWord _wHI; 204 SysResMode _mode; 205 } 206 SysRes; 207 #else 208 # error "Unknown OS" 209 #endif 210 211 212 /* ---- And now some basic accessor functions for it. ---- */ 213 214 #if defined(VGO_linux) 215 216 static inline Bool sr_isError ( SysRes sr ) { 217 return sr._isError; 218 } 219 static inline UWord sr_Res ( SysRes sr ) { 220 return sr._isError ? 0 : sr._val; 221 } 222 static inline UWord sr_ResHI ( SysRes sr ) { 223 return 0; 224 } 225 static inline UWord sr_Err ( SysRes sr ) { 226 return sr._isError ? sr._val : 0; 227 } 228 static inline Bool sr_EQ ( SysRes sr1, SysRes sr2 ) { 229 return sr1._val == sr2._val 230 && ((sr1._isError && sr2._isError) 231 || (!sr1._isError && !sr2._isError)); 232 } 233 234 #elif defined(VGO_aix5) 235 # error "need to define SysRes accessors on AIX5 (copy from 3.4.1 sources)" 236 237 238 #elif defined(VGO_darwin) 239 240 static inline Bool sr_isError ( SysRes sr ) { 241 switch (sr._mode) { 242 case SysRes_UNIX_ERR: return True; 243 default: return False; 244 /* should check tags properly and assert here, but we can't here */ 245 } 246 } 247 248 static inline UWord sr_Res ( SysRes sr ) { 249 switch (sr._mode) { 250 case SysRes_MACH: 251 case SysRes_MDEP: 252 case SysRes_UNIX_OK: return sr._wLO; 253 default: return 0; /* should assert, but we can't here */ 254 } 255 } 256 257 static inline UWord sr_ResHI ( SysRes sr ) { 258 switch (sr._mode) { 259 case SysRes_UNIX_OK: return sr._wHI; 260 default: return 0; /* should assert, but we can't here */ 261 } 262 } 263 264 static inline UWord sr_Err ( SysRes sr ) { 265 switch (sr._mode) { 266 case SysRes_UNIX_ERR: return sr._wLO; 267 default: return 0; /* should assert, but we can't here */ 268 } 269 } 270 271 static inline Bool sr_EQ ( SysRes sr1, SysRes sr2 ) { 272 return sr1._mode == sr2._mode 273 && sr1._wLO == sr2._wLO && sr1._wHI == sr2._wHI; 274 } 275 276 #else 277 # error "Unknown OS" 278 #endif 279 280 281 /* --------------------------------------------------------------------- 282 Miscellaneous (word size, endianness, regparmness, stringification) 283 ------------------------------------------------------------------ */ 284 285 /* Word size: this is going to be either 4 or 8. */ 286 // It should probably be in m_machine. 287 #define VG_WORDSIZE VEX_HOST_WORDSIZE 288 289 /* Endianness */ 290 #undef VG_BIGENDIAN 291 #undef VG_LITTLEENDIAN 292 293 #if defined(VGA_x86) || defined(VGA_amd64) || defined (VGA_arm) 294 # define VG_LITTLEENDIAN 1 295 #elif defined(VGA_ppc32) || defined(VGA_ppc64) 296 # define VG_BIGENDIAN 1 297 #else 298 # error Unknown arch 299 #endif 300 301 /* Regparmness */ 302 #if defined(VGA_x86) 303 # define VG_REGPARM(n) __attribute__((regparm(n))) 304 #elif defined(VGA_amd64) || defined(VGA_ppc32) \ 305 || defined(VGA_ppc64) || defined(VGA_arm) 306 # define VG_REGPARM(n) /* */ 307 #else 308 # error Unknown arch 309 #endif 310 311 /* Macro games */ 312 #define VG_STRINGIFZ(__str) #__str 313 #define VG_STRINGIFY(__str) VG_STRINGIFZ(__str) 314 315 // Where to send bug reports to. 316 #define VG_BUGS_TO "www.valgrind.org" 317 318 /* Branch prediction hints. */ 319 #if 1 /*HAVE_BUILTIN_EXPECT*/ 320 # define LIKELY(x) __builtin_expect(!!(x), 1) 321 # define UNLIKELY(x) __builtin_expect((x), 0) 322 #else 323 # define LIKELY(x) (x) 324 # define UNLIKELY(x) (x) 325 #endif 326 327 // printf format string checking for gcc. 328 // This feature has been supported since at least gcc version 2.95. 329 // For more information about the format attribute, see 330 // http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Function-Attributes.html. 331 #if defined(__GNUC__) 332 #define PRINTF_CHECK(x, y) __attribute__((format(__printf__, x, y))) 333 #else 334 #define PRINTF_CHECK(x, y) 335 #endif 336 337 338 #endif /* __PUB_TOOL_BASICS_H */ 339 340 /*--------------------------------------------------------------------*/ 341 /*--- end ---*/ 342 /*--------------------------------------------------------------------*/ 343