blob: 92c06972e53ae1f1293a223a87e47d3e5839582c [file] [log] [blame]
nethercoteebf1d862004-11-01 18:22:05 +00001
2/*--------------------------------------------------------------------*/
njnc7561b92005-06-19 01:24:32 +00003/*--- Header included by every tool C file. pub_tool_basics.h ---*/
nethercoteebf1d862004-11-01 18:22:05 +00004/*--------------------------------------------------------------------*/
5
6/*
njnb9c427c2004-12-01 14:14:42 +00007 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
nethercoteebf1d862004-11-01 18:22:05 +00009
sewardj03f8d3f2012-08-05 15:46:46 +000010 Copyright (C) 2000-2012 Julian Seward
nethercoteebf1d862004-11-01 18:22:05 +000011 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
njnc7561b92005-06-19 01:24:32 +000031#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
njnc7561b92005-06-19 01:24:32 +000049// For varargs types
50#include <stdarg.h>
51
nethercoteebf1d862004-11-01 18:22:05 +000052
53/* ---------------------------------------------------------------------
njn9c533252009-04-24 04:57:07 +000054 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/* ---------------------------------------------------------------------
njnd01fef72005-03-25 23:35:48 +000075 builtin types
nethercoteebf1d862004-11-01 18:22:05 +000076 ------------------------------------------------------------------ */
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.
njnc4431bf2009-01-15 21:29:24 +000080// 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
nethercote8b5f40c2004-11-02 13:29:50 +000083//
nethercoteebf1d862004-11-01 18:22:05 +000084// Size in bits on: 32-bit archs 64-bit archs
85// ------------ ------------
nethercoteebf1d862004-11-01 18:22:05 +000086typedef unsigned long UWord; // 32 64
njnc4431bf2009-01-15 21:29:24 +000087typedef signed long Word; // 32 64
nethercoteebf1d862004-11-01 18:22:05 +000088
njnc4431bf2009-01-15 21:29:24 +000089// 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.
nethercoteebf1d862004-11-01 18:22:05 +000093typedef UWord Addr; // 32 64
sewardjfa8ec112005-01-19 11:55:34 +000094typedef UWord AddrH; // 32 64
nethercoteebf1d862004-11-01 18:22:05 +000095
njnc4431bf2009-01-15 21:29:24 +000096// 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".
nethercote7ac7f7b2004-11-02 12:36:02 +000099typedef UWord SizeT; // 32 64
100typedef Word SSizeT; // 32 64
101
njnc4431bf2009-01-15 21:29:24 +0000102// 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.
106typedef Word PtrdiffT; // 32 64
nethercote5b9fafd2004-11-04 18:39:22 +0000107
njnc4431bf2009-01-15 21:29:24 +0000108// 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.
sewardj6e9de462011-06-28 07:25:29 +0000112// Nb: on Linux, off_t is a signed word-sized int. On Darwin it's
njnf76d27a2009-05-28 01:53:07 +0000113// always a signed 64-bit int. So we defined our own Off64T as well.
sewardj6e9de462011-06-28 07:25:29 +0000114#if defined(VGO_linux)
njnc4431bf2009-01-15 21:29:24 +0000115typedef Word OffT; // 32 64
njnf76d27a2009-05-28 01:53:07 +0000116#elif defined(VGO_darwin)
117typedef Long OffT; // 64 64
118#else
119# error Unknown OS
120#endif
njnc4431bf2009-01-15 21:29:24 +0000121typedef Long Off64T; // 64 64
sewardj274461d2005-10-02 17:01:41 +0000122
njn21edee32005-06-11 04:44:38 +0000123#if !defined(NULL)
124# define NULL ((void*)0)
125#endif
sewardjbc7df202005-05-02 10:25:34 +0000126
sewardj9c606bd2008-09-18 18:12:50 +0000127/* This is just too useful to not have around the place somewhere. */
128typedef struct { UWord uw1; UWord uw2; } UWordPair;
129
sewardje1f33a42006-10-17 01:38:48 +0000130
njnc7561b92005-06-19 01:24:32 +0000131/* ---------------------------------------------------------------------
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. */
139typedef UInt ThreadId;
140
141/* An abstraction of syscall return values.
sewardje1f33a42006-10-17 01:38:48 +0000142 Linux:
njncda2f0f2009-05-18 02:12:08 +0000143 When _isError == False,
144 _val holds the return value.
145 When _isError == True,
146 _err holds the error code.
sewardje1f33a42006-10-17 01:38:48 +0000147
njncda2f0f2009-05-18 02:12:08 +0000148 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
njn38c73d72009-05-19 01:32:52 +0000159 update both {R,E}DX and {R,E}AX (in guest state) given a SysRes,
160 if we're required to.
njnc7561b92005-06-19 01:24:32 +0000161*/
njncda2f0f2009-05-18 02:12:08 +0000162#if defined(VGO_linux)
sewardje1f33a42006-10-17 01:38:48 +0000163typedef
164 struct {
njncda2f0f2009-05-18 02:12:08 +0000165 UWord _val;
sewardj5db15402012-06-07 09:13:21 +0000166 UWord _valEx; // only used on mips-linux
njncda2f0f2009-05-18 02:12:08 +0000167 Bool _isError;
sewardje1f33a42006-10-17 01:38:48 +0000168 }
169 SysRes;
njnf76d27a2009-05-28 01:53:07 +0000170#elif defined(VGO_darwin)
171typedef
bart116458b2009-12-29 14:11:38 +0000172 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;
179typedef
njnf76d27a2009-05-28 01:53:07 +0000180 struct {
181 UWord _wLO;
182 UWord _wHI;
bart116458b2009-12-29 14:11:38 +0000183 SysResMode _mode;
njnf76d27a2009-05-28 01:53:07 +0000184 }
185 SysRes;
njncda2f0f2009-05-18 02:12:08 +0000186#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
195static inline Bool sr_isError ( SysRes sr ) {
196 return sr._isError;
197}
198static inline UWord sr_Res ( SysRes sr ) {
199 return sr._isError ? 0 : sr._val;
200}
sewardj5db15402012-06-07 09:13:21 +0000201static inline UWord sr_ResEx ( SysRes sr ) {
202 return sr._isError ? 0 : sr._valEx;
203}
njncda2f0f2009-05-18 02:12:08 +0000204static inline UWord sr_ResHI ( SysRes sr ) {
205 return 0;
206}
207static inline UWord sr_Err ( SysRes sr ) {
208 return sr._isError ? sr._val : 0;
209}
210static 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
njnf76d27a2009-05-28 01:53:07 +0000216#elif defined(VGO_darwin)
217
218static 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
226static 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
235static 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
242static 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
249static 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
njncda2f0f2009-05-18 02:12:08 +0000254#else
255# error "Unknown OS"
256#endif
sewardje1f33a42006-10-17 01:38:48 +0000257
njnc7561b92005-06-19 01:24:32 +0000258
259/* ---------------------------------------------------------------------
sewardj45f4e7c2005-09-27 19:20:21 +0000260 Miscellaneous (word size, endianness, regparmness, stringification)
njnc7561b92005-06-19 01:24:32 +0000261 ------------------------------------------------------------------ */
262
sewardj6e340c72005-07-10 00:53:42 +0000263/* Word size: this is going to be either 4 or 8. */
njnc7561b92005-06-19 01:24:32 +0000264// It should probably be in m_machine.
265#define VG_WORDSIZE VEX_HOST_WORDSIZE
266
sewardj6e340c72005-07-10 00:53:42 +0000267/* Endianness */
268#undef VG_BIGENDIAN
269#undef VG_LITTLEENDIAN
270
sewardj5db15402012-06-07 09:13:21 +0000271#if defined(VGA_x86) || defined(VGA_amd64) || defined (VGA_arm) \
272 || (defined(VGA_mips32) && defined (_MIPSEL))
sewardj6e340c72005-07-10 00:53:42 +0000273# define VG_LITTLEENDIAN 1
sewardj5db15402012-06-07 09:13:21 +0000274#elif defined(VGA_ppc32) || defined(VGA_ppc64) || defined(VGA_s390x) \
275 || (defined(VGA_mips32) && defined (_MIPSEB))
sewardj6e340c72005-07-10 00:53:42 +0000276# define VG_BIGENDIAN 1
njn38c73d72009-05-19 01:32:52 +0000277#else
278# error Unknown arch
sewardj6e340c72005-07-10 00:53:42 +0000279#endif
280
281/* Regparmness */
njnf536bbb2005-06-13 04:21:38 +0000282#if defined(VGA_x86)
njnaf839f52005-06-23 03:27:57 +0000283# define VG_REGPARM(n) __attribute__((regparm(n)))
sewardj59570ff2010-01-01 11:59:33 +0000284#elif defined(VGA_amd64) || defined(VGA_ppc32) \
sewardj5db15402012-06-07 09:13:21 +0000285 || defined(VGA_ppc64) || defined(VGA_arm) || defined(VGA_s390x) \
286 || defined(VGA_mips32)
njnaf839f52005-06-23 03:27:57 +0000287# define VG_REGPARM(n) /* */
njnf536bbb2005-06-13 04:21:38 +0000288#else
289# error Unknown arch
290#endif
291
sewardj45f4e7c2005-09-27 19:20:21 +0000292/* Macro games */
293#define VG_STRINGIFZ(__str) #__str
294#define VG_STRINGIFY(__str) VG_STRINGIFZ(__str)
295
njn64ea7f82006-10-14 23:26:21 +0000296// Where to send bug reports to.
297#define VG_BUGS_TO "www.valgrind.org"
298
bart5dd8e6a2008-03-22 08:04:29 +0000299/* Branch prediction hints. */
sewardj4ee9c562011-05-09 21:54:44 +0000300#if defined(__GNUC__)
bart5dd8e6a2008-03-22 08:04:29 +0000301# define LIKELY(x) __builtin_expect(!!(x), 1)
sewardj4ee9c562011-05-09 21:54:44 +0000302# define UNLIKELY(x) __builtin_expect(!!(x), 0)
bart5dd8e6a2008-03-22 08:04:29 +0000303#else
304# define LIKELY(x) (x)
305# define UNLIKELY(x) (x)
306#endif
307
sewardj588adef2009-08-15 22:41:51 +0000308// 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
bart5dd8e6a2008-03-22 08:04:29 +0000318
njnc7561b92005-06-19 01:24:32 +0000319#endif /* __PUB_TOOL_BASICS_H */
nethercoteebf1d862004-11-01 18:22:05 +0000320
321/*--------------------------------------------------------------------*/
322/*--- end ---*/
323/*--------------------------------------------------------------------*/