[2] | 1 | /* $Id: kDbgBase.h 40 2010-02-02 16:02:15Z bird $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * kDbg - The Debug Info Reader, Base Definitions and Typedefs.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[29] | 7 | * Copyright (c) 2006-2007 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
|
---|
[2] | 8 | *
|
---|
[29] | 9 | * Permission is hereby granted, free of charge, to any person
|
---|
| 10 | * obtaining a copy of this software and associated documentation
|
---|
| 11 | * files (the "Software"), to deal in the Software without
|
---|
| 12 | * restriction, including without limitation the rights to use,
|
---|
| 13 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
| 14 | * copies of the Software, and to permit persons to whom the
|
---|
| 15 | * Software is furnished to do so, subject to the following
|
---|
| 16 | * conditions:
|
---|
[2] | 17 | *
|
---|
[29] | 18 | * The above copyright notice and this permission notice shall be
|
---|
| 19 | * included in all copies or substantial portions of the Software.
|
---|
[2] | 20 | *
|
---|
[29] | 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
| 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
| 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
| 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
| 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
| 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
| 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
| 28 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
[2] | 29 | */
|
---|
| 30 |
|
---|
| 31 | #ifndef ___kDbgBase_h___
|
---|
| 32 | #define ___kDbgBase_h___
|
---|
| 33 |
|
---|
| 34 | #include <k/kDefs.h>
|
---|
| 35 | #include <k/kTypes.h>
|
---|
| 36 |
|
---|
| 37 |
|
---|
| 38 | /** @defgroup grp_kDbgBase kDbgBase - Base Definitions And Typedefs
|
---|
| 39 | * @{ */
|
---|
| 40 |
|
---|
| 41 | /*
|
---|
| 42 | * kDbg depend on size_t, [u]intNN_t, [u]intptr_t and some related constants.
|
---|
| 43 | * If KDBG_ALREADY_INCLUDED_STD_TYPES or KCOMMON_ALREADY_INCLUDED_STD_TYPES
|
---|
| 44 | * is defined, these has already been defined.
|
---|
| 45 | */
|
---|
| 46 | #if !defined(KDBG_ALREADY_INCLUDED_STD_TYPES) && !defined(KCOMMON_ALREADY_INCLUDED_STD_TYPES)
|
---|
| 47 | # define KCOMMON_ALREADY_INCLUDED_STD_TYPES 1
|
---|
| 48 | # include <sys/types.h>
|
---|
| 49 | # include <stddef.h>
|
---|
| 50 | # ifdef _MSC_VER
|
---|
| 51 | typedef signed char int8_t;
|
---|
| 52 | typedef unsigned char uint8_t;
|
---|
| 53 | typedef signed short int16_t;
|
---|
| 54 | typedef unsigned short uint16_t;
|
---|
| 55 | typedef signed int int32_t;
|
---|
| 56 | typedef unsigned int uint32_t;
|
---|
| 57 | typedef signed __int64 int64_t;
|
---|
| 58 | typedef unsigned __int64 uint64_t;
|
---|
| 59 | typedef int64_t intmax_t;
|
---|
| 60 | typedef uint64_t uintmax_t;
|
---|
| 61 | # define UINT8_C(c) (c)
|
---|
| 62 | # define UINT16_C(c) (c)
|
---|
| 63 | # define UINT32_C(c) (c ## U)
|
---|
| 64 | # define UINT64_C(c) (c ## ULL)
|
---|
| 65 | # define INT8_C(c) (c)
|
---|
| 66 | # define INT16_C(c) (c)
|
---|
| 67 | # define INT32_C(c) (c)
|
---|
| 68 | # define INT64_C(c) (c ## LL)
|
---|
| 69 | # define INT8_MIN (INT8_C(-0x7f) - 1)
|
---|
| 70 | # define INT16_MIN (INT16_C(-0x7fff) - 1)
|
---|
| 71 | # define INT32_MIN (INT32_C(-0x7fffffff) - 1)
|
---|
| 72 | # define INT64_MIN (INT64_C(-0x7fffffffffffffff) - 1)
|
---|
| 73 | # define INT8_MAX INT8_C(0x7f)
|
---|
| 74 | # define INT16_MAX INT16_C(0x7fff)
|
---|
| 75 | # define INT32_MAX INT32_C(0x7fffffff)
|
---|
| 76 | # define INT64_MAX INT64_C(0x7fffffffffffffff)
|
---|
| 77 | # define UINT8_MAX UINT8_C(0xff)
|
---|
| 78 | # define UINT16_MAX UINT16_C(0xffff)
|
---|
| 79 | # define UINT32_MAX UINT32_C(0xffffffff)
|
---|
| 80 | # define UINT64_MAX UINT64_C(0xffffffffffffffff)
|
---|
| 81 | # else
|
---|
| 82 | # include <stdint.h>
|
---|
| 83 | # endif
|
---|
| 84 | #endif /* !KDBG_ALREADY_INCLUDED_STD_TYPES && !KCOMMON_ALREADY_INCLUDED_STD_TYPES */
|
---|
| 85 |
|
---|
| 86 |
|
---|
| 87 | /** @def KDBG_CALL
|
---|
| 88 | * The calling convention used by the kDbg functions. */
|
---|
| 89 | #if defined(_MSC_VER) || defined(__OS2__)
|
---|
| 90 | # define KDBG_CALL __cdecl
|
---|
| 91 | #else
|
---|
| 92 | # define KDBG_CALL
|
---|
| 93 | #endif
|
---|
| 94 |
|
---|
[40] | 95 | #ifdef DOXYGEN_RUNNING
|
---|
[2] | 96 | /** @def KDBG_BUILDING
|
---|
| 97 | * Define KDBG_BUILDING to indicate that kDbg is being built.
|
---|
| 98 | */
|
---|
| 99 | # define KDBG_BUILDING
|
---|
| 100 | /** @def KDBG_RESIDES_IN_DLL
|
---|
| 101 | * Define KDBG_RESIDES_IN_DLL to indicate that kDbg resides in a DLL.
|
---|
| 102 | */
|
---|
| 103 | # define KDBG_RESIDES_IN_DLL
|
---|
| 104 | #endif
|
---|
| 105 |
|
---|
| 106 | /** @def KDBG_DECL
|
---|
| 107 | * Macro for defining public functions. */
|
---|
| 108 | #if defined(KDBG_RESIDES_IN_DLL) \
|
---|
| 109 | && (defined(_MSC_VER) || defined(__OS2__))
|
---|
| 110 | # ifdef KDBG_BUILDING
|
---|
| 111 | # define KDBG_DECL(type) __declspec(dllexport) type
|
---|
| 112 | # else
|
---|
| 113 | # define KDBG_DECL(type) __declspec(dllimport) type
|
---|
| 114 | # endif
|
---|
| 115 | #else
|
---|
| 116 | # define KDBG_DECL(type) type
|
---|
| 117 | #endif
|
---|
| 118 |
|
---|
| 119 | /** @def KDBG_INLINE
|
---|
| 120 | * Macro for defining an inline function. */
|
---|
| 121 | #ifdef __cplusplus
|
---|
| 122 | # if defined(__GNUC__)
|
---|
| 123 | # define KDBG_INLINE(type) static inline type
|
---|
| 124 | # else
|
---|
| 125 | # define KDBG_INLINE(type) inline type
|
---|
| 126 | # endif
|
---|
| 127 | #else
|
---|
| 128 | # if defined(__GNUC__)
|
---|
| 129 | # define KDBG_INLINE(type) static __inline__ type
|
---|
| 130 | # elif defined(_MSC_VER)
|
---|
| 131 | # define KDBG_INLINE(type) _inline type
|
---|
| 132 | # else
|
---|
| 133 | # error "Port me"
|
---|
| 134 | # endif
|
---|
| 135 | #endif
|
---|
| 136 |
|
---|
| 137 |
|
---|
| 138 | /** The kDbg address type. */
|
---|
| 139 | typedef uint64_t KDBGADDR;
|
---|
| 140 | /** Pointer to a kLdr address. */
|
---|
| 141 | typedef KDBGADDR *PKDBGADDR;
|
---|
| 142 | /** Pointer to a const kLdr address. */
|
---|
| 143 | typedef const KDBGADDR *PCKDBGADDR;
|
---|
| 144 |
|
---|
| 145 | /** NIL address. */
|
---|
| 146 | #define NIL_KDBGADDR (~(uint64_t)0)
|
---|
| 147 |
|
---|
| 148 | /** @def PRI_KDBGADDR
|
---|
| 149 | * printf format type. */
|
---|
| 150 | #ifdef _MSC_VER
|
---|
| 151 | # define PRI_KDBGADDR "I64x"
|
---|
| 152 | #else
|
---|
| 153 | # define PRI_KDBGADDR "llx"
|
---|
| 154 | #endif
|
---|
| 155 |
|
---|
| 156 |
|
---|
| 157 | /** Get the minimum of two values. */
|
---|
| 158 | #define KDBG_MIN(a, b) ((a) <= (b) ? (a) : (b))
|
---|
| 159 | /** Get the maximum of two values. */
|
---|
| 160 | #define KDBG_MAX(a, b) ((a) >= (b) ? (a) : (b))
|
---|
| 161 | /** Calculate the offset of a structure member. */
|
---|
| 162 | #define KDBG_OFFSETOF(strct, memb) ( (size_t)( &((strct *)0)->memb ) )
|
---|
| 163 | /** Align a size_t value. */
|
---|
| 164 | #define KDBG_ALIGN_Z(val, align) ( ((val) + ((align) - 1)) & ~(size_t)((align) - 1) )
|
---|
| 165 | /** Align a void * value. */
|
---|
| 166 | #define KDBG_ALIGN_P(pv, align) ( (void *)( ((uintptr_t)(pv) + ((align) - 1)) & ~(uintptr_t)((align) - 1) ) )
|
---|
| 167 | /** Align a size_t value. */
|
---|
| 168 | #define KDBG_ALIGN_ADDR(val, align) ( ((val) + ((align) - 1)) & ~(KDBGADDR)((align) - 1) )
|
---|
| 169 | /** Number of elements in an array. */
|
---|
| 170 | #define KDBG_ELEMENTS(a) ( sizeof(a) / sizeof((a)[0]) )
|
---|
| 171 | /** @def KDBG_VALID_PTR
|
---|
| 172 | * Checks if the specified pointer is a valid address or not. */
|
---|
| 173 | #define KDBG_VALID_PTR(ptr) ( (uintptr_t)(ptr) + 0x1000U >= 0x2000U )
|
---|
| 174 |
|
---|
| 175 |
|
---|
| 176 | /** @def KDBG_LITTLE_ENDIAN
|
---|
| 177 | * The kDbg build is for a little endian target. */
|
---|
| 178 | /** @def KDBG_BIG_ENDIAN
|
---|
| 179 | * The kDbg build is for a big endian target. */
|
---|
| 180 | #if !defined(KDBG_LITTLE_ENDIAN) && !defined(KDBG_BIG_ENDIAN)
|
---|
| 181 | # define KDBG_LITTLE_ENDIAN
|
---|
| 182 | #endif
|
---|
[40] | 183 | #ifdef DOXYGEN_RUNNING
|
---|
[2] | 184 | # define KDBG_BIG_ENDIAN
|
---|
| 185 | #endif
|
---|
| 186 |
|
---|
| 187 |
|
---|
| 188 | /** @name Endian Conversion
|
---|
| 189 | * @{ */
|
---|
| 190 |
|
---|
| 191 | /** @def KDBG_E2E_U16
|
---|
| 192 | * Convert the endian of an unsigned 16-bit value. */
|
---|
| 193 | # define KDBG_E2E_U16(u16) ( (uint16_t) (((u16) >> 8) | ((u16) << 8)) )
|
---|
| 194 | /** @def KDBG_E2E_U32
|
---|
| 195 | * Convert the endian of an unsigned 32-bit value. */
|
---|
| 196 | # define KDBG_E2E_U32(u32) ( ( ((u32) & UINT32_C(0xff000000)) >> 24 ) \
|
---|
| 197 | | ( ((u32) & UINT32_C(0x00ff0000)) >> 8 ) \
|
---|
| 198 | | ( ((u32) & UINT32_C(0x0000ff00)) << 8 ) \
|
---|
| 199 | | ( ((u32) & UINT32_C(0x000000ff)) << 24 ) \
|
---|
| 200 | )
|
---|
| 201 | /** @def KDBG_E2E_U64
|
---|
| 202 | * Convert the endian of an unsigned 64-bit value. */
|
---|
| 203 | # define KDBG_E2E_U64(u64) ( ( ((u64) & UINT64_C(0xff00000000000000)) >> 56 ) \
|
---|
| 204 | | ( ((u64) & UINT64_C(0x00ff000000000000)) >> 40 ) \
|
---|
| 205 | | ( ((u64) & UINT64_C(0x0000ff0000000000)) >> 24 ) \
|
---|
| 206 | | ( ((u64) & UINT64_C(0x000000ff00000000)) >> 8 ) \
|
---|
| 207 | | ( ((u64) & UINT64_C(0x00000000ff000000)) << 8 ) \
|
---|
| 208 | | ( ((u64) & UINT64_C(0x0000000000ff0000)) << 24 ) \
|
---|
| 209 | | ( ((u64) & UINT64_C(0x000000000000ff00)) << 40 ) \
|
---|
| 210 | | ( ((u64) & UINT64_C(0x00000000000000ff)) << 56 ) \
|
---|
| 211 | )
|
---|
| 212 |
|
---|
| 213 | /** @def KDBG_LE2H_U16
|
---|
| 214 | * Unsigned 16-bit little-endian to host endian. */
|
---|
| 215 | /** @def KDBG_LE2H_U32
|
---|
| 216 | * Unsigned 32-bit little-endian to host endian. */
|
---|
| 217 | /** @def KDBG_LE2H_U64
|
---|
| 218 | * Unsigned 64-bit little-endian to host endian. */
|
---|
| 219 | /** @def KDBG_BE2H_U16
|
---|
| 220 | * Unsigned 16-bit big-endian to host endian. */
|
---|
| 221 | /** @def KDBG_BE2H_U32
|
---|
| 222 | * Unsigned 32-bit big-endian to host endian. */
|
---|
| 223 | /** @def KDBG_BE2H_U64
|
---|
| 224 | * Unsigned 64-bit big-endian to host endian. */
|
---|
| 225 | #ifdef KDBG_LITTLE_ENDIAN
|
---|
| 226 | # define KDBG_LE2H_U16(u16) ((uint16_t)(u16))
|
---|
| 227 | # define KDBG_LE2H_U32(u32) ((uint32_t)(u32))
|
---|
| 228 | # define KDBG_LE2H_U64(u64) ((uint32_t)(u32))
|
---|
| 229 | # define KDBG_BE2H_U16(u16) KDBG_E2E_U16(u16)
|
---|
| 230 | # define KDBG_BE2H_U32(u32) KDBG_E2E_U32(u32)
|
---|
| 231 | # define KDBG_BE2H_U64(u64) KDBG_E2E_U64(u64)
|
---|
| 232 | #elif defined(KDBG_BIG_ENDIAN)
|
---|
| 233 | # define KDBG_LE2H_U16(u16) KDBG_E2E_U16(u16)
|
---|
| 234 | # define KDBG_LE2H_U32(u32) KDBG_E2E_U32(u32)
|
---|
| 235 | # define KDBG_LE2H_U32(u64) KDBG_E2E_U64(u64)
|
---|
| 236 | # define KDBG_BE2H_U16(u16) ((uint16_t)(u16))
|
---|
| 237 | # define KDBG_BE2H_U32(u32) ((uint32_t)(u32))
|
---|
| 238 | # define KDBG_BE2H_U64(u64) ((uint32_t)(u32))
|
---|
| 239 | #else
|
---|
| 240 | # error "KDBG_BIG_ENDIAN or KDBG_LITTLE_ENDIAN is supposed to be defined."
|
---|
| 241 | #endif
|
---|
| 242 |
|
---|
| 243 | /** @} */
|
---|
| 244 |
|
---|
| 245 | /** @} */
|
---|
| 246 |
|
---|
| 247 | #endif
|
---|
| 248 |
|
---|