nethercote | ebf1d86 | 2004-11-01 18:22:05 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 3 | /*--- Header included by every tool C file. pub_tool_basics.h ---*/ |
nethercote | ebf1d86 | 2004-11-01 18:22:05 +0000 | [diff] [blame] | 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
njn | b9c427c | 2004-12-01 14:14:42 +0000 | [diff] [blame] | 7 | This file is part of Valgrind, a dynamic binary instrumentation |
| 8 | framework. |
nethercote | ebf1d86 | 2004-11-01 18:22:05 +0000 | [diff] [blame] | 9 | |
sewardj | 03f8d3f | 2012-08-05 15:46:46 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2012 Julian Seward |
nethercote | ebf1d86 | 2004-11-01 18:22:05 +0000 | [diff] [blame] | 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 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 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 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 49 | // For varargs types |
| 50 | #include <stdarg.h> |
| 51 | |
nethercote | ebf1d86 | 2004-11-01 18:22:05 +0000 | [diff] [blame] | 52 | |
| 53 | /* --------------------------------------------------------------------- |
njn | 9c53325 | 2009-04-24 04:57:07 +0000 | [diff] [blame] | 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 | /* --------------------------------------------------------------------- |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 75 | builtin types |
nethercote | ebf1d86 | 2004-11-01 18:22:05 +0000 | [diff] [blame] | 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. |
njn | c4431bf | 2009-01-15 21:29:24 +0000 | [diff] [blame] | 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 |
nethercote | 8b5f40c | 2004-11-02 13:29:50 +0000 | [diff] [blame] | 83 | // |
nethercote | ebf1d86 | 2004-11-01 18:22:05 +0000 | [diff] [blame] | 84 | // Size in bits on: 32-bit archs 64-bit archs |
| 85 | // ------------ ------------ |
nethercote | ebf1d86 | 2004-11-01 18:22:05 +0000 | [diff] [blame] | 86 | typedef unsigned long UWord; // 32 64 |
njn | c4431bf | 2009-01-15 21:29:24 +0000 | [diff] [blame] | 87 | typedef signed long Word; // 32 64 |
nethercote | ebf1d86 | 2004-11-01 18:22:05 +0000 | [diff] [blame] | 88 | |
njn | c4431bf | 2009-01-15 21:29:24 +0000 | [diff] [blame] | 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. |
nethercote | ebf1d86 | 2004-11-01 18:22:05 +0000 | [diff] [blame] | 93 | typedef UWord Addr; // 32 64 |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 94 | typedef UWord AddrH; // 32 64 |
nethercote | ebf1d86 | 2004-11-01 18:22:05 +0000 | [diff] [blame] | 95 | |
njn | c4431bf | 2009-01-15 21:29:24 +0000 | [diff] [blame] | 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". |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 99 | typedef UWord SizeT; // 32 64 |
| 100 | typedef Word SSizeT; // 32 64 |
| 101 | |
njn | c4431bf | 2009-01-15 21:29:24 +0000 | [diff] [blame] | 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 |
nethercote | 5b9fafd | 2004-11-04 18:39:22 +0000 | [diff] [blame] | 107 | |
njn | c4431bf | 2009-01-15 21:29:24 +0000 | [diff] [blame] | 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. |
sewardj | 6e9de46 | 2011-06-28 07:25:29 +0000 | [diff] [blame] | 112 | // Nb: on Linux, off_t is a signed word-sized int. On Darwin it's |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 113 | // always a signed 64-bit int. So we defined our own Off64T as well. |
sewardj | 6e9de46 | 2011-06-28 07:25:29 +0000 | [diff] [blame] | 114 | #if defined(VGO_linux) |
njn | c4431bf | 2009-01-15 21:29:24 +0000 | [diff] [blame] | 115 | typedef Word OffT; // 32 64 |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 116 | #elif defined(VGO_darwin) |
| 117 | typedef Long OffT; // 64 64 |
| 118 | #else |
| 119 | # error Unknown OS |
| 120 | #endif |
njn | c4431bf | 2009-01-15 21:29:24 +0000 | [diff] [blame] | 121 | typedef Long Off64T; // 64 64 |
sewardj | 274461d | 2005-10-02 17:01:41 +0000 | [diff] [blame] | 122 | |
njn | 21edee3 | 2005-06-11 04:44:38 +0000 | [diff] [blame] | 123 | #if !defined(NULL) |
| 124 | # define NULL ((void*)0) |
| 125 | #endif |
sewardj | bc7df20 | 2005-05-02 10:25:34 +0000 | [diff] [blame] | 126 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 127 | /* This is just too useful to not have around the place somewhere. */ |
| 128 | typedef struct { UWord uw1; UWord uw2; } UWordPair; |
| 129 | |
sewardj | e1f33a4 | 2006-10-17 01:38:48 +0000 | [diff] [blame] | 130 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 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. |
sewardj | e1f33a4 | 2006-10-17 01:38:48 +0000 | [diff] [blame] | 142 | Linux: |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 143 | When _isError == False, |
| 144 | _val holds the return value. |
| 145 | When _isError == True, |
| 146 | _err holds the error code. |
sewardj | e1f33a4 | 2006-10-17 01:38:48 +0000 | [diff] [blame] | 147 | |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 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 |
njn | 38c73d7 | 2009-05-19 01:32:52 +0000 | [diff] [blame] | 159 | update both {R,E}DX and {R,E}AX (in guest state) given a SysRes, |
| 160 | if we're required to. |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 161 | */ |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 162 | #if defined(VGO_linux) |
sewardj | e1f33a4 | 2006-10-17 01:38:48 +0000 | [diff] [blame] | 163 | typedef |
| 164 | struct { |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 165 | UWord _val; |
sewardj | 5db1540 | 2012-06-07 09:13:21 +0000 | [diff] [blame] | 166 | UWord _valEx; // only used on mips-linux |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 167 | Bool _isError; |
sewardj | e1f33a4 | 2006-10-17 01:38:48 +0000 | [diff] [blame] | 168 | } |
| 169 | SysRes; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 170 | #elif defined(VGO_darwin) |
| 171 | typedef |
bart | 116458b | 2009-12-29 14:11:38 +0000 | [diff] [blame] | 172 | enum { |
| 173 | SysRes_MACH=40, // MACH, result is _wLO |
| 174 | SysRes_MDEP, // MDEP, result is _wLO |
| 175 | SysRes_UNIX_OK, // UNIX, success, result is _wHI:_wLO |
| 176 | SysRes_UNIX_ERR // UNIX, error, error is _wHI:_wLO |
| 177 | } |
| 178 | SysResMode; |
| 179 | typedef |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 180 | struct { |
| 181 | UWord _wLO; |
| 182 | UWord _wHI; |
bart | 116458b | 2009-12-29 14:11:38 +0000 | [diff] [blame] | 183 | SysResMode _mode; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 184 | } |
| 185 | SysRes; |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 186 | #else |
| 187 | # error "Unknown OS" |
| 188 | #endif |
| 189 | |
| 190 | |
| 191 | /* ---- And now some basic accessor functions for it. ---- */ |
| 192 | |
| 193 | #if defined(VGO_linux) |
| 194 | |
| 195 | static inline Bool sr_isError ( SysRes sr ) { |
| 196 | return sr._isError; |
| 197 | } |
| 198 | static inline UWord sr_Res ( SysRes sr ) { |
| 199 | return sr._isError ? 0 : sr._val; |
| 200 | } |
sewardj | 5db1540 | 2012-06-07 09:13:21 +0000 | [diff] [blame] | 201 | static inline UWord sr_ResEx ( SysRes sr ) { |
| 202 | return sr._isError ? 0 : sr._valEx; |
| 203 | } |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 204 | static inline UWord sr_ResHI ( SysRes sr ) { |
| 205 | return 0; |
| 206 | } |
| 207 | static inline UWord sr_Err ( SysRes sr ) { |
| 208 | return sr._isError ? sr._val : 0; |
| 209 | } |
| 210 | static inline Bool sr_EQ ( SysRes sr1, SysRes sr2 ) { |
| 211 | return sr1._val == sr2._val |
| 212 | && ((sr1._isError && sr2._isError) |
| 213 | || (!sr1._isError && !sr2._isError)); |
| 214 | } |
| 215 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 216 | #elif defined(VGO_darwin) |
| 217 | |
| 218 | static inline Bool sr_isError ( SysRes sr ) { |
| 219 | switch (sr._mode) { |
| 220 | case SysRes_UNIX_ERR: return True; |
| 221 | default: return False; |
| 222 | /* should check tags properly and assert here, but we can't here */ |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | static inline UWord sr_Res ( SysRes sr ) { |
| 227 | switch (sr._mode) { |
| 228 | case SysRes_MACH: |
| 229 | case SysRes_MDEP: |
| 230 | case SysRes_UNIX_OK: return sr._wLO; |
| 231 | default: return 0; /* should assert, but we can't here */ |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | static inline UWord sr_ResHI ( SysRes sr ) { |
| 236 | switch (sr._mode) { |
| 237 | case SysRes_UNIX_OK: return sr._wHI; |
| 238 | default: return 0; /* should assert, but we can't here */ |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | static inline UWord sr_Err ( SysRes sr ) { |
| 243 | switch (sr._mode) { |
| 244 | case SysRes_UNIX_ERR: return sr._wLO; |
| 245 | default: return 0; /* should assert, but we can't here */ |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | static inline Bool sr_EQ ( SysRes sr1, SysRes sr2 ) { |
| 250 | return sr1._mode == sr2._mode |
| 251 | && sr1._wLO == sr2._wLO && sr1._wHI == sr2._wHI; |
| 252 | } |
| 253 | |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 254 | #else |
| 255 | # error "Unknown OS" |
| 256 | #endif |
sewardj | e1f33a4 | 2006-10-17 01:38:48 +0000 | [diff] [blame] | 257 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 258 | |
| 259 | /* --------------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 260 | Miscellaneous (word size, endianness, regparmness, stringification) |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 261 | ------------------------------------------------------------------ */ |
| 262 | |
sewardj | 6e340c7 | 2005-07-10 00:53:42 +0000 | [diff] [blame] | 263 | /* Word size: this is going to be either 4 or 8. */ |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 264 | // It should probably be in m_machine. |
| 265 | #define VG_WORDSIZE VEX_HOST_WORDSIZE |
| 266 | |
sewardj | 6e340c7 | 2005-07-10 00:53:42 +0000 | [diff] [blame] | 267 | /* Endianness */ |
| 268 | #undef VG_BIGENDIAN |
| 269 | #undef VG_LITTLEENDIAN |
| 270 | |
sewardj | 5db1540 | 2012-06-07 09:13:21 +0000 | [diff] [blame] | 271 | #if defined(VGA_x86) || defined(VGA_amd64) || defined (VGA_arm) \ |
| 272 | || (defined(VGA_mips32) && defined (_MIPSEL)) |
sewardj | 6e340c7 | 2005-07-10 00:53:42 +0000 | [diff] [blame] | 273 | # define VG_LITTLEENDIAN 1 |
sewardj | 5db1540 | 2012-06-07 09:13:21 +0000 | [diff] [blame] | 274 | #elif defined(VGA_ppc32) || defined(VGA_ppc64) || defined(VGA_s390x) \ |
| 275 | || (defined(VGA_mips32) && defined (_MIPSEB)) |
sewardj | 6e340c7 | 2005-07-10 00:53:42 +0000 | [diff] [blame] | 276 | # define VG_BIGENDIAN 1 |
njn | 38c73d7 | 2009-05-19 01:32:52 +0000 | [diff] [blame] | 277 | #else |
| 278 | # error Unknown arch |
sewardj | 6e340c7 | 2005-07-10 00:53:42 +0000 | [diff] [blame] | 279 | #endif |
| 280 | |
| 281 | /* Regparmness */ |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 282 | #if defined(VGA_x86) |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 283 | # define VG_REGPARM(n) __attribute__((regparm(n))) |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 284 | #elif defined(VGA_amd64) || defined(VGA_ppc32) \ |
sewardj | 5db1540 | 2012-06-07 09:13:21 +0000 | [diff] [blame] | 285 | || defined(VGA_ppc64) || defined(VGA_arm) || defined(VGA_s390x) \ |
| 286 | || defined(VGA_mips32) |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 287 | # define VG_REGPARM(n) /* */ |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 288 | #else |
| 289 | # error Unknown arch |
| 290 | #endif |
| 291 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 292 | /* Macro games */ |
| 293 | #define VG_STRINGIFZ(__str) #__str |
| 294 | #define VG_STRINGIFY(__str) VG_STRINGIFZ(__str) |
| 295 | |
njn | 64ea7f8 | 2006-10-14 23:26:21 +0000 | [diff] [blame] | 296 | // Where to send bug reports to. |
| 297 | #define VG_BUGS_TO "www.valgrind.org" |
| 298 | |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 299 | /* Branch prediction hints. */ |
sewardj | 4ee9c56 | 2011-05-09 21:54:44 +0000 | [diff] [blame] | 300 | #if defined(__GNUC__) |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 301 | # define LIKELY(x) __builtin_expect(!!(x), 1) |
sewardj | 4ee9c56 | 2011-05-09 21:54:44 +0000 | [diff] [blame] | 302 | # define UNLIKELY(x) __builtin_expect(!!(x), 0) |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 303 | #else |
| 304 | # define LIKELY(x) (x) |
| 305 | # define UNLIKELY(x) (x) |
| 306 | #endif |
| 307 | |
sewardj | 588adef | 2009-08-15 22:41:51 +0000 | [diff] [blame] | 308 | // printf format string checking for gcc. |
| 309 | // This feature has been supported since at least gcc version 2.95. |
| 310 | // For more information about the format attribute, see |
| 311 | // http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Function-Attributes.html. |
| 312 | #if defined(__GNUC__) |
| 313 | #define PRINTF_CHECK(x, y) __attribute__((format(__printf__, x, y))) |
| 314 | #else |
| 315 | #define PRINTF_CHECK(x, y) |
| 316 | #endif |
| 317 | |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 318 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 319 | #endif /* __PUB_TOOL_BASICS_H */ |
nethercote | ebf1d86 | 2004-11-01 18:22:05 +0000 | [diff] [blame] | 320 | |
| 321 | /*--------------------------------------------------------------------*/ |
| 322 | /*--- end ---*/ |
| 323 | /*--------------------------------------------------------------------*/ |