1 | /* $Id: kDbgHlp.h 29 2009-07-01 20:30:29Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * kDbg - The Debug Info Reader, Internal Header.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2006-2007 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
|
---|
8 | *
|
---|
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:
|
---|
17 | *
|
---|
18 | * The above copyright notice and this permission notice shall be
|
---|
19 | * included in all copies or substantial portions of the Software.
|
---|
20 | *
|
---|
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.
|
---|
29 | */
|
---|
30 |
|
---|
31 | #ifndef ___kDbgHlp_h___
|
---|
32 | #define ___kDbgHlp_h___
|
---|
33 |
|
---|
34 | #include <k/kDbgBase.h>
|
---|
35 |
|
---|
36 | #ifdef __cplusplus
|
---|
37 | extern "C" {
|
---|
38 | #endif
|
---|
39 |
|
---|
40 |
|
---|
41 | /** @defgroup grp_kDbgHlpHeap kDbg Internal Heap APIs.
|
---|
42 | * @internal
|
---|
43 | * @{
|
---|
44 | */
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Allocates memory.
|
---|
48 | *
|
---|
49 | * @returns Pointer to the allocated memory.
|
---|
50 | * NULL on failure.
|
---|
51 | * @param cb The number of bytes to allocate.
|
---|
52 | */
|
---|
53 | void *kDbgHlpAlloc(size_t cb);
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Allocates memory like kDbgHlpAlloc, except that it's zeroed.
|
---|
57 | *
|
---|
58 | * @returns Pointer to the allocated memory.
|
---|
59 | * NULL on failure.
|
---|
60 | * @param cb The number of bytes to allocate.
|
---|
61 | */
|
---|
62 | void *kDbgHlpAllocZ(size_t cb);
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Combination of kDbgHlpAlloc and memcpy.
|
---|
66 | *
|
---|
67 | * @returns Pointer to the duplicate.
|
---|
68 | * NULL on failure.
|
---|
69 | *
|
---|
70 | * @param pv The memory to be duplicate.
|
---|
71 | * @param cb The size of the block.
|
---|
72 | */
|
---|
73 | void *kDbgHlpAllocDup(const void *pv, size_t cb);
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Reallocates a memory block returned by kDbgHlpAlloc, kDbgHlpAllocZ
|
---|
77 | * kDbgHlpAllocDup or this function.
|
---|
78 | *
|
---|
79 | * The content of new memory added to the memory block is undefined.
|
---|
80 | *
|
---|
81 | * @returns Pointer to the allocated memory.
|
---|
82 | * NULL on failure, the old block remains intact.
|
---|
83 | * @param pv The memory block to reallocate.
|
---|
84 | * If NULL this function will work like kDbgHlpAlloc.
|
---|
85 | * @param cb The number of bytes to allocate.
|
---|
86 | * If 0 this function will work like kDbgHlpFree.
|
---|
87 | */
|
---|
88 | void *kDbgHlpRealloc(void *pv, size_t cb);
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Frees memory allocated by kDbgHlpAlloc, kDbgHlpAllocZ
|
---|
92 | * kDbgHlpAllocDup, or kDbgHlpRealloc.
|
---|
93 | *
|
---|
94 | * @param pv
|
---|
95 | */
|
---|
96 | void kDbgHlpFree(void *pv);
|
---|
97 |
|
---|
98 | /** @} */
|
---|
99 |
|
---|
100 |
|
---|
101 | /** @defgroup grp_kDbgHlpFile kDbg Internal File Access APIs.
|
---|
102 | * @internal
|
---|
103 | * @{
|
---|
104 | */
|
---|
105 | /**
|
---|
106 | * Opens the specified file as read-only, buffered if possible.
|
---|
107 | *
|
---|
108 | * @returns 0 on success, or the appropriate KDBG_ERR_* on failure.
|
---|
109 | *
|
---|
110 | * @param pszFilename The file to open.
|
---|
111 | * @param ppFile Where to store the handle to the open file.
|
---|
112 | */
|
---|
113 | int kDbgHlpOpenRO(const char *pszFilename, PKDBGHLPFILE *ppFile);
|
---|
114 |
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Closes a file opened by kDbgHlpOpenRO.
|
---|
118 | *
|
---|
119 | * @param pFile The file handle.
|
---|
120 | */
|
---|
121 | void kDbgHlpClose(PKDBGHLPFILE pFile);
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Gets the native file handle.
|
---|
125 | *
|
---|
126 | * @return The native file handle.
|
---|
127 | * -1 on failure.
|
---|
128 | * @param pFile The file handle.
|
---|
129 | */
|
---|
130 | uintptr_t kDbgHlpNativeFileHandle(PKDBGHLPFILE pFile);
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Gets the size of an open file.
|
---|
134 | *
|
---|
135 | * @returns The file size in bytes on success.
|
---|
136 | * On failure -1 is returned.
|
---|
137 | * @param pFile The file handle.
|
---|
138 | */
|
---|
139 | int64_t kDbgHlpFileSize(PKDBGHLPFILE pFile);
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Reads a number of bytes at a specified file location.
|
---|
143 | *
|
---|
144 | * This will change the current file position to off + cb on success,
|
---|
145 | * while on failure the position will be undefined.
|
---|
146 | *
|
---|
147 | * @returns The file size in bytes on success.
|
---|
148 | * On failure -1 is returned.
|
---|
149 | * @param pFile The file handle.
|
---|
150 | * @param off Where to read.
|
---|
151 | * @param pv Where to store the data.
|
---|
152 | * @param cb How much to read.
|
---|
153 | */
|
---|
154 | int kDbgHlpReadAt(PKDBGHLPFILE pFile, int64_t off, void *pv, size_t cb);
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Reads a number of bytes at the current file position.
|
---|
158 | *
|
---|
159 | * This will advance the current file position by cb bytes on success
|
---|
160 | * while on failure the position will be undefined.
|
---|
161 | *
|
---|
162 | * @returns The file size in bytes on success.
|
---|
163 | * On failure -1 is returned.
|
---|
164 | * @param pFile The file handle.
|
---|
165 | * @param pv Where to store the data.
|
---|
166 | * @param cb How much to read.
|
---|
167 | * @param off Where to read.
|
---|
168 | */
|
---|
169 | int kDbgHlpRead(PKDBGHLPFILE pFile, void *pv, size_t cb);
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Sets the current file position.
|
---|
173 | *
|
---|
174 | * @returns 0 on success, and KDBG_ERR_* on failure.
|
---|
175 | * @param pFile The file handle.
|
---|
176 | * @param off The desired file position.
|
---|
177 | */
|
---|
178 | int kDbgHlpSeek(PKDBGHLPFILE pFile, int64_t off);
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Move the file position relative to the current one.
|
---|
182 | *
|
---|
183 | * @returns 0 on success, and KDBG_ERR_* on failure.
|
---|
184 | * @param pFile The file handle.
|
---|
185 | * @param off How much to move the file position by.
|
---|
186 | */
|
---|
187 | int kDbgHlpSeekByCur(PKDBGHLPFILE pFile, int64_t off);
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Move the file position relative to the end of the file.
|
---|
191 | *
|
---|
192 | * @returns 0 on success, and KDBG_ERR_* on failure.
|
---|
193 | * @param pFile The file handle.
|
---|
194 | * @param off The offset relative to the end, positive number.
|
---|
195 | */
|
---|
196 | int kDbgHlpSeekByEnd(PKDBGHLPFILE pFile, int64_t off);
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Gets the current file position.
|
---|
200 | *
|
---|
201 | * @returns The current file position on success.
|
---|
202 | * -1 on failure.
|
---|
203 | * @param pFile The file handle.
|
---|
204 | */
|
---|
205 | int64_t kDbgHlpTell(PKDBGHLPFILE pFile);
|
---|
206 |
|
---|
207 | /** @} */
|
---|
208 |
|
---|
209 | /** @defgroup grp_kDbgHlpAssert kDbg Internal Assertion Macros.
|
---|
210 | * @internal
|
---|
211 | * @{
|
---|
212 | */
|
---|
213 |
|
---|
214 | #ifdef _MSC_VER
|
---|
215 | # define kDbgAssertBreakpoint() do { __debugbreak(); } while (0)
|
---|
216 | #else
|
---|
217 | # define kDbgAssertBreakpoint() do { __asm__ __volatile__ ("int3"); } while (0)
|
---|
218 | #endif
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Helper function that displays the first part of the assertion message.
|
---|
222 | *
|
---|
223 | * @param pszExpr The expression.
|
---|
224 | * @param pszFile The file name.
|
---|
225 | * @param iLine The line number is the file.
|
---|
226 | * @param pszFunction The function name.
|
---|
227 | */
|
---|
228 | void kDbgAssertMsg1(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction);
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * Helper function that displays custom assert message.
|
---|
232 | *
|
---|
233 | * @param pszFormat Format string that get passed to vprintf.
|
---|
234 | * @param ... Format arguments.
|
---|
235 | */
|
---|
236 | void kDbgAssertMsg2(const char *pszFormat, ...);
|
---|
237 |
|
---|
238 |
|
---|
239 | #ifdef KDBG_STRICT
|
---|
240 |
|
---|
241 | # define kDbgAssert(expr) \
|
---|
242 | do { \
|
---|
243 | if (!(expr)) \
|
---|
244 | { \
|
---|
245 | kDbgAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
|
---|
246 | kDbgAssertBreakpoint(); \
|
---|
247 | }
|
---|
248 | } while (0)
|
---|
249 |
|
---|
250 | # define kDbgAssertReturn(expr, rcRet) \
|
---|
251 | do { \
|
---|
252 | if (!(expr)) \
|
---|
253 | { \
|
---|
254 | kDbgAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
|
---|
255 | kDbgAssertBreakpoint(); \
|
---|
256 | return (rcRet); \
|
---|
257 | }
|
---|
258 | } while (0)
|
---|
259 |
|
---|
260 | # define kDbgAssertMsg(expr, msg) \
|
---|
261 | do { \
|
---|
262 | if (!(expr)) \
|
---|
263 | { \
|
---|
264 | kDbgAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
|
---|
265 | kDbgAssertMsg2 msg; \
|
---|
266 | kDbgAssertBreakpoint(); \
|
---|
267 | }
|
---|
268 | } while (0)
|
---|
269 |
|
---|
270 | # define kDbgAssertMsgReturn(expr, msg, rcRet) \
|
---|
271 | do { \
|
---|
272 | if (!(expr)) \
|
---|
273 | { \
|
---|
274 | kDbgAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
|
---|
275 | kDbgAssertMsg2 msg; \
|
---|
276 | kDbgAssertBreakpoint(); \
|
---|
277 | return (rcRet); \
|
---|
278 | }
|
---|
279 | } while (0)
|
---|
280 |
|
---|
281 | #else /* !KDBG_STRICT */
|
---|
282 | # define kDbgAssert(expr) do { } while (0)
|
---|
283 | # define kDbgAssertReturn(expr, rcRet) do { if (!(expr)) return (rcRet); } while (0)
|
---|
284 | # define kDbgAssertMsg(expr, msg) do { } while (0)
|
---|
285 | # define kDbgAssertMsgReturn(expr, msg, rcRet) do { if (!(expr)) return (rcRet); } while (0)
|
---|
286 | #endif /* !KDBG_STRICT */
|
---|
287 |
|
---|
288 | #define kDbgAssertPtr(ptr) kDbgAssertMsg(KDBG_VALID_PTR(ptr), ("%s = %p\n", #ptr, (ptr)))
|
---|
289 | #define kDbgAssertPtrReturn(ptr, rcRet) kDbgAssertMsgReturn(KDBG_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)), (rcRet))
|
---|
290 | #define kDbgAssertPtrNull(ptr) kDbgAssertMsg(!(ptr) || KDBG_VALID_PTR(ptr), ("%s = %p\n", #ptr, (ptr)))
|
---|
291 | #define kDbgAssertPtrNullReturn(ptr, rcRet) kDbgAssertMsgReturn(!(ptr) || KDBG_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)), (rcRet))
|
---|
292 | #define kDbgAssertRC(rc) kDbgAssertMsg((rc) == 0, ("%s = %d\n", #rc, (rc)))
|
---|
293 | #define kDbgAssertRCReturn(rc, rcRet) kDbgAssertMsgReturn((rc) == 0, ("%s = %d -> %d\n", #rc, (rc), (rcRet)), (rcRet))
|
---|
294 | #define kDbgAssertFailed() kDbgAssert(0)
|
---|
295 | #define kDbgAssertFailedReturn(rcRet) kDbgAssertReturn(0, (rcRet))
|
---|
296 | #define kDbgAssertMsgFailed(msg) kDbgAssertMsg(0, msg)
|
---|
297 | #define kDbgAssertMsgFailedReturn(msg, rcRet) kDbgAssertMsgReturn(0, msg, (rcRet))
|
---|
298 |
|
---|
299 | /** @} */
|
---|
300 |
|
---|
301 | #ifdef __cplusplus
|
---|
302 | }
|
---|
303 | #endif
|
---|
304 |
|
---|
305 | #endif
|
---|
306 |
|
---|