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-2011 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, 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) 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 Darwin: 149 Interpretation depends on _mode: 150 MACH, MDEP: 151 these can never 'fail' (apparently). The result of the 152 syscall is a single host word, _wLO. 153 UNIX: 154 Can record a double-word error or a double-word result: 155 When _mode is SysRes_UNIX_OK, _wHI:_wLO holds the result. 156 When _mode is SysRes_UNIX_ERR, _wHI:_wLO holds the error code. 157 Probably the high word of an error is always ignored by 158 userspace, but we have to record it, so that we can correctly 159 update both {R,E}DX and {R,E}AX (in guest state) given a SysRes, 160 if we're required to. 161 */ 162 #if defined(VGO_linux) 163 typedef 164 struct { 165 UWord _val; 166 Bool _isError; 167 } 168 SysRes; 169 #elif defined(VGO_darwin) 170 typedef 171 enum { 172 SysRes_MACH=40, // MACH, result is _wLO 173 SysRes_MDEP, // MDEP, result is _wLO 174 SysRes_UNIX_OK, // UNIX, success, result is _wHI:_wLO 175 SysRes_UNIX_ERR // UNIX, error, error is _wHI:_wLO 176 } 177 SysResMode; 178 typedef 179 struct { 180 UWord _wLO; 181 UWord _wHI; 182 SysResMode _mode; 183 } 184 SysRes; 185 #else 186 # error "Unknown OS" 187 #endif 188 189 190 /* ---- And now some basic accessor functions for it. ---- */ 191 192 #if defined(VGO_linux) 193 194 static inline Bool sr_isError ( SysRes sr ) { 195 return sr._isError; 196 } 197 static inline UWord sr_Res ( SysRes sr ) { 198 return sr._isError ? 0 : sr._val; 199 } 200 static inline UWord sr_ResHI ( SysRes sr ) { 201 return 0; 202 } 203 static inline UWord sr_Err ( SysRes sr ) { 204 return sr._isError ? sr._val : 0; 205 } 206 static inline Bool sr_EQ ( SysRes sr1, SysRes sr2 ) { 207 return sr1._val == sr2._val 208 && ((sr1._isError && sr2._isError) 209 || (!sr1._isError && !sr2._isError)); 210 } 211 212 #elif defined(VGO_darwin) 213 214 static inline Bool sr_isError ( SysRes sr ) { 215 switch (sr._mode) { 216 case SysRes_UNIX_ERR: return True; 217 default: return False; 218 /* should check tags properly and assert here, but we can't here */ 219 } 220 } 221 222 static inline UWord sr_Res ( SysRes sr ) { 223 switch (sr._mode) { 224 case SysRes_MACH: 225 case SysRes_MDEP: 226 case SysRes_UNIX_OK: return sr._wLO; 227 default: return 0; /* should assert, but we can't here */ 228 } 229 } 230 231 static inline UWord sr_ResHI ( SysRes sr ) { 232 switch (sr._mode) { 233 case SysRes_UNIX_OK: return sr._wHI; 234 default: return 0; /* should assert, but we can't here */ 235 } 236 } 237 238 static inline UWord sr_Err ( SysRes sr ) { 239 switch (sr._mode) { 240 case SysRes_UNIX_ERR: return sr._wLO; 241 default: return 0; /* should assert, but we can't here */ 242 } 243 } 244 245 static inline Bool sr_EQ ( SysRes sr1, SysRes sr2 ) { 246 return sr1._mode == sr2._mode 247 && sr1._wLO == sr2._wLO && sr1._wHI == sr2._wHI; 248 } 249 250 #else 251 # error "Unknown OS" 252 #endif 253 254 255 /* --------------------------------------------------------------------- 256 Miscellaneous (word size, endianness, regparmness, stringification) 257 ------------------------------------------------------------------ */ 258 259 /* Word size: this is going to be either 4 or 8. */ 260 // It should probably be in m_machine. 261 #define VG_WORDSIZE VEX_HOST_WORDSIZE 262 263 /* Endianness */ 264 #undef VG_BIGENDIAN 265 #undef VG_LITTLEENDIAN 266 267 #if defined(VGA_x86) || defined(VGA_amd64) || defined (VGA_arm) 268 # define VG_LITTLEENDIAN 1 269 #elif defined(VGA_ppc32) || defined(VGA_ppc64) || defined(VGA_s390x) 270 # define VG_BIGENDIAN 1 271 #else 272 # error Unknown arch 273 #endif 274 275 /* Regparmness */ 276 #if defined(VGA_x86) 277 # define VG_REGPARM(n) __attribute__((regparm(n))) 278 #elif defined(VGA_amd64) || defined(VGA_ppc32) \ 279 || defined(VGA_ppc64) || defined(VGA_arm) || defined(VGA_s390x) 280 # define VG_REGPARM(n) /* */ 281 #else 282 # error Unknown arch 283 #endif 284 285 /* Macro games */ 286 #define VG_STRINGIFZ(__str) #__str 287 #define VG_STRINGIFY(__str) VG_STRINGIFZ(__str) 288 289 // Where to send bug reports to. 290 #define VG_BUGS_TO "www.valgrind.org" 291 292 /* Branch prediction hints. */ 293 #if defined(__GNUC__) 294 # define LIKELY(x) __builtin_expect(!!(x), 1) 295 # define UNLIKELY(x) __builtin_expect(!!(x), 0) 296 #else 297 # define LIKELY(x) (x) 298 # define UNLIKELY(x) (x) 299 #endif 300 301 // printf format string checking for gcc. 302 // This feature has been supported since at least gcc version 2.95. 303 // For more information about the format attribute, see 304 // http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Function-Attributes.html. 305 #if defined(__GNUC__) 306 #define PRINTF_CHECK(x, y) __attribute__((format(__printf__, x, y))) 307 #else 308 #define PRINTF_CHECK(x, y) 309 #endif 310 311 312 #endif /* __PUB_TOOL_BASICS_H */ 313 314 /*--------------------------------------------------------------------*/ 315 /*--- end ---*/ 316 /*--------------------------------------------------------------------*/ 317