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