[3528] | 1 | /* $Id: kDbgBase.h 3528 2007-08-20 02:43:13Z bird $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * kDbg - The Debug Info Reader, Base Definitions and Typedefs.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
| 7 | * Copyright (c) 2006-2007 knut st. osmundsen <bird@anduin.net>
|
---|
| 8 | *
|
---|
| 9 | *
|
---|
| 10 | * This file is part of kDbg.
|
---|
| 11 | *
|
---|
| 12 | * kDbg is free software; you can redistribute it and/or modify
|
---|
| 13 | * it under the terms of the GNU General Public License as published by
|
---|
| 14 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 15 | * (at your option) any later version.
|
---|
| 16 | *
|
---|
| 17 | * kDbg is distributed in the hope that it will be useful,
|
---|
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 20 | * GNU General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with kDbg; if not, write to the Free Software
|
---|
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
| 25 | *
|
---|
| 26 | */
|
---|
| 27 |
|
---|
| 28 | #ifndef ___kDbgBase_h___
|
---|
| 29 | #define ___kDbgBase_h___
|
---|
| 30 |
|
---|
| 31 |
|
---|
| 32 | /** @defgroup grp_kDbgBase kDbgBase - Base Definitions And Typedefs
|
---|
| 33 | * @{ */
|
---|
| 34 |
|
---|
| 35 | /*
|
---|
| 36 | * kDbg depend on size_t, [u]intNN_t, [u]intptr_t and some related constants.
|
---|
| 37 | * If KDBG_ALREADY_INCLUDED_STD_TYPES or KCOMMON_ALREADY_INCLUDED_STD_TYPES
|
---|
| 38 | * is defined, these has already been defined.
|
---|
| 39 | */
|
---|
| 40 | #if !defined(KDBG_ALREADY_INCLUDED_STD_TYPES) && !defined(KCOMMON_ALREADY_INCLUDED_STD_TYPES)
|
---|
| 41 | # define KCOMMON_ALREADY_INCLUDED_STD_TYPES 1
|
---|
| 42 | # include <sys/types.h>
|
---|
| 43 | # include <stddef.h>
|
---|
| 44 | # ifdef _MSC_VER
|
---|
| 45 | typedef signed char int8_t;
|
---|
| 46 | typedef unsigned char uint8_t;
|
---|
| 47 | typedef signed short int16_t;
|
---|
| 48 | typedef unsigned short uint16_t;
|
---|
| 49 | typedef signed int int32_t;
|
---|
| 50 | typedef unsigned int uint32_t;
|
---|
| 51 | typedef signed __int64 int64_t;
|
---|
| 52 | typedef unsigned __int64 uint64_t;
|
---|
| 53 | typedef int64_t intmax_t;
|
---|
| 54 | typedef uint64_t uintmax_t;
|
---|
| 55 | # define UINT8_C(c) (c)
|
---|
| 56 | # define UINT16_C(c) (c)
|
---|
| 57 | # define UINT32_C(c) (c ## U)
|
---|
| 58 | # define UINT64_C(c) (c ## ULL)
|
---|
| 59 | # define INT8_C(c) (c)
|
---|
| 60 | # define INT16_C(c) (c)
|
---|
| 61 | # define INT32_C(c) (c)
|
---|
| 62 | # define INT64_C(c) (c ## LL)
|
---|
| 63 | # define INT8_MIN (INT8_C(-0x7f) - 1)
|
---|
| 64 | # define INT16_MIN (INT16_C(-0x7fff) - 1)
|
---|
| 65 | # define INT32_MIN (INT32_C(-0x7fffffff) - 1)
|
---|
| 66 | # define INT64_MIN (INT64_C(-0x7fffffffffffffff) - 1)
|
---|
| 67 | # define INT8_MAX INT8_C(0x7f)
|
---|
| 68 | # define INT16_MAX INT16_C(0x7fff)
|
---|
| 69 | # define INT32_MAX INT32_C(0x7fffffff)
|
---|
| 70 | # define INT64_MAX INT64_C(0x7fffffffffffffff)
|
---|
| 71 | # define UINT8_MAX UINT8_C(0xff)
|
---|
| 72 | # define UINT16_MAX UINT16_C(0xffff)
|
---|
| 73 | # define UINT32_MAX UINT32_C(0xffffffff)
|
---|
| 74 | # define UINT64_MAX UINT64_C(0xffffffffffffffff)
|
---|
| 75 | # else
|
---|
| 76 | # include <stdint.h>
|
---|
| 77 | # endif
|
---|
| 78 | #endif /* !KDBG_ALREADY_INCLUDED_STD_TYPES && !KCOMMON_ALREADY_INCLUDED_STD_TYPES */
|
---|
| 79 |
|
---|
| 80 |
|
---|
| 81 | /** @def KDBG_CALL
|
---|
| 82 | * The calling convention used by the kDbg functions. */
|
---|
| 83 | #if defined(_MSC_VER) || defined(__OS2__)
|
---|
| 84 | # define KDBG_CALL __cdecl
|
---|
| 85 | #else
|
---|
| 86 | # define KDBG_CALL
|
---|
| 87 | #endif
|
---|
| 88 |
|
---|
| 89 | #ifdef __DOXYGEN__
|
---|
| 90 | /** @def KDBG_BUILDING
|
---|
| 91 | * Define KDBG_BUILDING to indicate that kDbg is being built.
|
---|
| 92 | */
|
---|
| 93 | # define KDBG_BUILDING
|
---|
| 94 | /** @def KDBG_RESIDES_IN_DLL
|
---|
| 95 | * Define KDBG_RESIDES_IN_DLL to indicate that kDbg resides in a DLL.
|
---|
| 96 | */
|
---|
| 97 | # define KDBG_RESIDES_IN_DLL
|
---|
| 98 | #endif
|
---|
| 99 |
|
---|
| 100 | /** @def KDBG_DECL
|
---|
| 101 | * Macro for defining public functions. */
|
---|
| 102 | #if defined(KDBG_RESIDES_IN_DLL) \
|
---|
| 103 | && (defined(_MSC_VER) || defined(__OS2__))
|
---|
| 104 | # ifdef KDBG_BUILDING
|
---|
| 105 | # define KDBG_DECL(type) __declspec(dllexport) type
|
---|
| 106 | # else
|
---|
| 107 | # define KDBG_DECL(type) __declspec(dllimport) type
|
---|
| 108 | # endif
|
---|
| 109 | #else
|
---|
| 110 | # define KDBG_DECL(type) type
|
---|
| 111 | #endif
|
---|
| 112 |
|
---|
| 113 | /** @def KDBG_INLINE
|
---|
| 114 | * Macro for defining an inline function. */
|
---|
| 115 | #ifdef __cplusplus
|
---|
| 116 | # if defined(__GNUC__)
|
---|
| 117 | # define KDBG_INLINE(type) static inline type
|
---|
| 118 | # else
|
---|
| 119 | # define KDBG_INLINE(type) inline type
|
---|
| 120 | # endif
|
---|
| 121 | #else
|
---|
| 122 | # if defined(__GNUC__)
|
---|
| 123 | # define KDBG_INLINE(type) static __inline__ type
|
---|
| 124 | # elif defined(_MSC_VER)
|
---|
| 125 | # define KDBG_INLINE(type) _inline type
|
---|
| 126 | # else
|
---|
| 127 | # error "Port me"
|
---|
| 128 | # endif
|
---|
| 129 | #endif
|
---|
| 130 |
|
---|
| 131 |
|
---|
| 132 | /** The kDbg address type. */
|
---|
| 133 | typedef uint64_t KDBGADDR;
|
---|
| 134 | /** Pointer to a kLdr address. */
|
---|
| 135 | typedef KDBGADDR *PKDBGADDR;
|
---|
| 136 | /** Pointer to a const kLdr address. */
|
---|
| 137 | typedef const KDBGADDR *PCKDBGADDR;
|
---|
| 138 |
|
---|
| 139 | /** NIL address. */
|
---|
| 140 | #define NIL_KDBGADDR (~(uint64_t)0)
|
---|
| 141 |
|
---|
| 142 | /** @def PRI_KDBGADDR
|
---|
| 143 | * printf format type. */
|
---|
| 144 | #ifdef _MSC_VER
|
---|
| 145 | # define PRI_KDBGADDR "I64x"
|
---|
| 146 | #else
|
---|
| 147 | # define PRI_KDBGADDR "llx"
|
---|
| 148 | #endif
|
---|
| 149 |
|
---|
| 150 |
|
---|
| 151 | /** Get the minimum of two values. */
|
---|
| 152 | #define KDBG_MIN(a, b) ((a) <= (b) ? (a) : (b))
|
---|
| 153 | /** Get the maximum of two values. */
|
---|
| 154 | #define KDBG_MAX(a, b) ((a) >= (b) ? (a) : (b))
|
---|
| 155 | /** Calculate the offset of a structure member. */
|
---|
| 156 | #define KDBG_OFFSETOF(strct, memb) ( (size_t)( &((strct *)0)->memb ) )
|
---|
| 157 | /** Align a size_t value. */
|
---|
| 158 | #define KDBG_ALIGN_Z(val, align) ( ((val) + ((align) - 1)) & ~(size_t)((align) - 1) )
|
---|
| 159 | /** Align a void * value. */
|
---|
| 160 | #define KDBG_ALIGN_P(pv, align) ( (void *)( ((uintptr_t)(pv) + ((align) - 1)) & ~(uintptr_t)((align) - 1) ) )
|
---|
| 161 | /** Align a size_t value. */
|
---|
| 162 | #define KDBG_ALIGN_ADDR(val, align) ( ((val) + ((align) - 1)) & ~(KDBGADDR)((align) - 1) )
|
---|
| 163 | /** Number of elements in an array. */
|
---|
| 164 | #define KDBG_ELEMENTS(a) ( sizeof(a) / sizeof((a)[0]) )
|
---|
| 165 | /** @def KDBG_VALID_PTR
|
---|
| 166 | * Checks if the specified pointer is a valid address or not. */
|
---|
| 167 | #define KDBG_VALID_PTR(ptr) ( (uintptr_t)(ptr) + 0x1000U >= 0x2000U )
|
---|
| 168 |
|
---|
| 169 |
|
---|
| 170 | /** @def KDBG_LITTLE_ENDIAN
|
---|
| 171 | * The kDbg build is for a little endian target. */
|
---|
| 172 | /** @def KDBG_BIG_ENDIAN
|
---|
| 173 | * The kDbg build is for a big endian target. */
|
---|
| 174 | #if !defined(KDBG_LITTLE_ENDIAN) && !defined(KDBG_BIG_ENDIAN)
|
---|
| 175 | # define KDBG_LITTLE_ENDIAN
|
---|
| 176 | #endif
|
---|
| 177 | #ifdef __DOXYGEN__
|
---|
| 178 | # define KDBG_BIG_ENDIAN
|
---|
| 179 | #endif
|
---|
| 180 |
|
---|
| 181 |
|
---|
| 182 | /** @name Endian Conversion
|
---|
| 183 | * @{ */
|
---|
| 184 |
|
---|
| 185 | /** @def KDBG_E2E_U16
|
---|
| 186 | * Convert the endian of an unsigned 16-bit value. */
|
---|
| 187 | # define KDBG_E2E_U16(u16) ( (uint16_t) (((u16) >> 8) | ((u16) << 8)) )
|
---|
| 188 | /** @def KDBG_E2E_U32
|
---|
| 189 | * Convert the endian of an unsigned 32-bit value. */
|
---|
| 190 | # define KDBG_E2E_U32(u32) ( ( ((u32) & UINT32_C(0xff000000)) >> 24 ) \
|
---|
| 191 | | ( ((u32) & UINT32_C(0x00ff0000)) >> 8 ) \
|
---|
| 192 | | ( ((u32) & UINT32_C(0x0000ff00)) << 8 ) \
|
---|
| 193 | | ( ((u32) & UINT32_C(0x000000ff)) << 24 ) \
|
---|
| 194 | )
|
---|
| 195 | /** @def KDBG_E2E_U64
|
---|
| 196 | * Convert the endian of an unsigned 64-bit value. */
|
---|
| 197 | # define KDBG_E2E_U64(u64) ( ( ((u64) & UINT64_C(0xff00000000000000)) >> 56 ) \
|
---|
| 198 | | ( ((u64) & UINT64_C(0x00ff000000000000)) >> 40 ) \
|
---|
| 199 | | ( ((u64) & UINT64_C(0x0000ff0000000000)) >> 24 ) \
|
---|
| 200 | | ( ((u64) & UINT64_C(0x000000ff00000000)) >> 8 ) \
|
---|
| 201 | | ( ((u64) & UINT64_C(0x00000000ff000000)) << 8 ) \
|
---|
| 202 | | ( ((u64) & UINT64_C(0x0000000000ff0000)) << 24 ) \
|
---|
| 203 | | ( ((u64) & UINT64_C(0x000000000000ff00)) << 40 ) \
|
---|
| 204 | | ( ((u64) & UINT64_C(0x00000000000000ff)) << 56 ) \
|
---|
| 205 | )
|
---|
| 206 |
|
---|
| 207 | /** @def KDBG_LE2H_U16
|
---|
| 208 | * Unsigned 16-bit little-endian to host endian. */
|
---|
| 209 | /** @def KDBG_LE2H_U32
|
---|
| 210 | * Unsigned 32-bit little-endian to host endian. */
|
---|
| 211 | /** @def KDBG_LE2H_U64
|
---|
| 212 | * Unsigned 64-bit little-endian to host endian. */
|
---|
| 213 | /** @def KDBG_BE2H_U16
|
---|
| 214 | * Unsigned 16-bit big-endian to host endian. */
|
---|
| 215 | /** @def KDBG_BE2H_U32
|
---|
| 216 | * Unsigned 32-bit big-endian to host endian. */
|
---|
| 217 | /** @def KDBG_BE2H_U64
|
---|
| 218 | * Unsigned 64-bit big-endian to host endian. */
|
---|
| 219 | #ifdef KDBG_LITTLE_ENDIAN
|
---|
| 220 | # define KDBG_LE2H_U16(u16) ((uint16_t)(u16))
|
---|
| 221 | # define KDBG_LE2H_U32(u32) ((uint32_t)(u32))
|
---|
| 222 | # define KDBG_LE2H_U64(u64) ((uint32_t)(u32))
|
---|
| 223 | # define KDBG_BE2H_U16(u16) KDBG_E2E_U16(u16)
|
---|
| 224 | # define KDBG_BE2H_U32(u32) KDBG_E2E_U32(u32)
|
---|
| 225 | # define KDBG_BE2H_U64(u64) KDBG_E2E_U64(u64)
|
---|
| 226 | #elif defined(KDBG_BIG_ENDIAN)
|
---|
| 227 | # define KDBG_LE2H_U16(u16) KDBG_E2E_U16(u16)
|
---|
| 228 | # define KDBG_LE2H_U32(u32) KDBG_E2E_U32(u32)
|
---|
| 229 | # define KDBG_LE2H_U32(u64) KDBG_E2E_U64(u64)
|
---|
| 230 | # define KDBG_BE2H_U16(u16) ((uint16_t)(u16))
|
---|
| 231 | # define KDBG_BE2H_U32(u32) ((uint32_t)(u32))
|
---|
| 232 | # define KDBG_BE2H_U64(u64) ((uint32_t)(u32))
|
---|
| 233 | #else
|
---|
| 234 | # error "KDBG_BIG_ENDIAN or KDBG_LITTLE_ENDIAN is supposed to be defined."
|
---|
| 235 | #endif
|
---|
| 236 |
|
---|
| 237 | /** @} */
|
---|
| 238 |
|
---|
| 239 | /** @} */
|
---|
| 240 |
|
---|
| 241 | #endif
|
---|
| 242 |
|
---|