source: trunk/kStuff/kDbg/kDbgHlp.h@ 3602

Last change on this file since 3602 was 3541, checked in by bird, 18 years ago

errors and stuff.

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