source: trunk/kDbg/kDbgHlp.h@ 8

Last change on this file since 8 was 2, checked in by bird, 18 years ago

Imported http://svn.netlabs.org/repos/libc/trunk/kStuff, revision 3612.

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