summaryrefslogtreecommitdiffstats
path: root/src/lib/kStuff/kDbg/kDbgHlp.h
blob: cd5116dd6737d4d0b22b76e6023d65292a99b9bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* $Id: kDbgHlp.h 78 2016-07-13 15:52:04Z bird $ */
/** @file
 * kDbg - The Debug Info Reader, Internal Header.
 */

/*
 * Copyright (c) 2006-2007 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef ___kDbgHlp_h___
#define ___kDbgHlp_h___

#include <k/kDbgBase.h>

#ifdef __cplusplus
extern "C" {
#endif


/** @defgroup grp_kDbgHlpHeap   kDbg Internal Heap APIs.
 * @internal
 * @{
 */

/**
 * Allocates memory.
 *
 * @returns Pointer to the allocated memory.
 *          NULL on failure.
 * @param   cb      The number of bytes to allocate.
 */
void *kDbgHlpAlloc(size_t cb);

/**
 * Allocates memory like kDbgHlpAlloc, except that it's zeroed.
 *
 * @returns Pointer to the allocated memory.
 *          NULL on failure.
 * @param   cb      The number of bytes to allocate.
 */
void *kDbgHlpAllocZ(size_t cb);

/**
 * Combination of kDbgHlpAlloc and memcpy.
 *
 * @returns Pointer to the duplicate.
 *          NULL on failure.
 *
 * @param   pv      The memory to be duplicate.
 * @param   cb      The size of the block.
 */
void *kDbgHlpAllocDup(const void *pv, size_t cb);

/**
 * Reallocates a memory block returned by kDbgHlpAlloc, kDbgHlpAllocZ
 * kDbgHlpAllocDup or this function.
 *
 * The content of new memory added to the memory block is undefined.
 *
 * @returns Pointer to the allocated memory.
 *          NULL on failure, the old block remains intact.
 * @param   pv      The memory block to reallocate.
 *                  If NULL this function will work like kDbgHlpAlloc.
 * @param   cb      The number of bytes to allocate.
 *                  If 0 this function will work like kDbgHlpFree.
 */
void *kDbgHlpRealloc(void *pv, size_t cb);

/**
 * Frees memory allocated by kDbgHlpAlloc, kDbgHlpAllocZ
 * kDbgHlpAllocDup, or kDbgHlpRealloc.
 *
 * @param pv
 */
void kDbgHlpFree(void *pv);

/** @} */


/** @defgroup grp_kDbgHlpFile   kDbg Internal File Access APIs.
 * @internal
 * @{
 */
/**
 * Opens the specified file as read-only, buffered if possible.
 *
 * @returns 0 on success, or the appropriate KDBG_ERR_* on failure.
 *
 * @param   pszFilename     The file to open.
 * @param   ppFile          Where to store the handle to the open file.
 */
int kDbgHlpOpenRO(const char *pszFilename, PKDBGHLPFILE *ppFile);


/**
 * Closes a file opened by kDbgHlpOpenRO.
 *
 * @param   pFile           The file handle.
 */
void kDbgHlpClose(PKDBGHLPFILE pFile);

/**
 * Gets the native file handle.
 *
 * @return  The native file handle.
 *          -1 on failure.
 * @param   pFile           The file handle.
 */
uintptr_t kDbgHlpNativeFileHandle(PKDBGHLPFILE pFile);

/**
 * Gets the size of an open file.
 *
 * @returns The file size in bytes on success.
 *          On failure -1 is returned.
 * @param   pFile           The file handle.
 */
int64_t kDbgHlpFileSize(PKDBGHLPFILE pFile);

/**
 * Reads a number of bytes at a specified file location.
 *
 * This will change the current file position to off + cb on success,
 * while on failure the position will be undefined.
 *
 * @returns The file size in bytes on success.
 *          On failure -1 is returned.
 * @param   pFile           The file handle.
 * @param   off             Where to read.
 * @param   pv              Where to store the data.
 * @param   cb              How much to read.
 */
int kDbgHlpReadAt(PKDBGHLPFILE pFile, int64_t off, void *pv, size_t cb);

/**
 * Reads a number of bytes at the current file position.
 *
 * This will advance the current file position by cb bytes on success
 * while on failure the position will be undefined.
 *
 * @returns The file size in bytes on success.
 *          On failure -1 is returned.
 * @param   pFile           The file handle.
 * @param   pv              Where to store the data.
 * @param   cb              How much to read.
 * @param   off             Where to read.
 */
int kDbgHlpRead(PKDBGHLPFILE pFile, void *pv, size_t cb);

/**
 * Sets the current file position.
 *
 * @returns 0 on success, and KDBG_ERR_* on failure.
 * @param   pFile           The file handle.
 * @param   off             The desired file position.
 */
int kDbgHlpSeek(PKDBGHLPFILE pFile, int64_t off);

/**
 * Move the file position relative to the current one.
 *
 * @returns 0 on success, and KDBG_ERR_* on failure.
 * @param   pFile           The file handle.
 * @param   off             How much to move the file position by.
 */
int kDbgHlpSeekByCur(PKDBGHLPFILE pFile, int64_t off);

/**
 * Move the file position relative to the end of the file.
 *
 * @returns 0 on success, and KDBG_ERR_* on failure.
 * @param   pFile           The file handle.
 * @param   off             The offset relative to the end, positive number.
 */
int kDbgHlpSeekByEnd(PKDBGHLPFILE pFile, int64_t off);

/**
 * Gets the current file position.
 *
 * @returns The current file position on success.
 *          -1 on failure.
 * @param   pFile           The file handle.
 */
int64_t kDbgHlpTell(PKDBGHLPFILE pFile);

/** @} */

/** @defgroup grp_kDbgHlpAssert     kDbg Internal Assertion Macros.
 * @internal
 * @{
 */

#ifdef _MSC_VER
# define kDbgAssertBreakpoint() do { __debugbreak(); } while (0)
#else
# define kDbgAssertBreakpoint() do { __asm__ __volatile__ ("int3"); } while (0)
#endif

/**
 * Helper function that displays the first part of the assertion message.
 *
 * @param   pszExpr         The expression.
 * @param   pszFile         The file name.
 * @param   iLine           The line number is the file.
 * @param   pszFunction     The function name.
 */
void kDbgAssertMsg1(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction);

/**
 * Helper function that displays custom assert message.
 *
 * @param   pszFormat       Format string that get passed to vprintf.
 * @param   ...             Format arguments.
 */
void kDbgAssertMsg2(const char *pszFormat, ...);


#ifdef KDBG_STRICT

# define kDbgAssert(expr) \
    do { \
        if (!(expr)) \
        { \
            kDbgAssertMsg1(#expr, __FILE__, __LINE__, K_FUNCTION); \
            kDbgAssertBreakpoint(); \
        } \
    } while (0)

# define kDbgAssertReturn(expr, rcRet) \
    do { \
        if (!(expr)) \
        { \
            kDbgAssertMsg1(#expr, __FILE__, __LINE__, K_FUNCTION); \
            kDbgAssertBreakpoint(); \
            return (rcRet); \
        } \
    } while (0)

# define kDbgAssertMsg(expr, msg) \
    do { \
        if (!(expr)) \
        { \
            kDbgAssertMsg1(#expr, __FILE__, __LINE__, K_FUNCTION); \
            kDbgAssertMsg2 msg; \
            kDbgAssertBreakpoint(); \
        } \
    } while (0)

# define kDbgAssertMsgReturn(expr, msg, rcRet) \
    do { \
        if (!(expr)) \
        { \
            kDbgAssertMsg1(#expr, __FILE__, __LINE__, K_FUNCTION); \
            kDbgAssertMsg2 msg; \
            kDbgAssertBreakpoint(); \
            return (rcRet); \
        } \
    } while (0)

#else   /* !KDBG_STRICT */
# define kDbgAssert(expr)                       do { } while (0)
# define kDbgAssertReturn(expr, rcRet)          do { if (!(expr)) return (rcRet); } while (0)
# define kDbgAssertMsg(expr, msg)               do { } while (0)
# define kDbgAssertMsgReturn(expr, msg, rcRet)  do { if (!(expr)) return (rcRet); } while (0)
#endif  /* !KDBG_STRICT */

#define kDbgAssertPtr(ptr)                      kDbgAssertMsg(KDBG_VALID_PTR(ptr), ("%s = %p\n", #ptr, (ptr)))
#define kDbgAssertPtrReturn(ptr, rcRet)         kDbgAssertMsgReturn(KDBG_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)), (rcRet))
#define kDbgAssertPtrNull(ptr)                  kDbgAssertMsg(!(ptr) || KDBG_VALID_PTR(ptr), ("%s = %p\n", #ptr, (ptr)))
#define kDbgAssertPtrNullReturn(ptr, rcRet)     kDbgAssertMsgReturn(!(ptr) || KDBG_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)), (rcRet))
#define kDbgAssertRC(rc)                        kDbgAssertMsg((rc) == 0, ("%s = %d\n", #rc, (rc)))
#define kDbgAssertRCReturn(rc, rcRet)           kDbgAssertMsgReturn((rc) == 0, ("%s = %d -> %d\n", #rc, (rc), (rcRet)), (rcRet))
#define kDbgAssertFailed()                      kDbgAssert(0)
#define kDbgAssertFailedReturn(rcRet)           kDbgAssertReturn(0, (rcRet))
#define kDbgAssertMsgFailed(msg)                kDbgAssertMsg(0, msg)
#define kDbgAssertMsgFailedReturn(msg, rcRet)   kDbgAssertMsgReturn(0, msg, (rcRet))

/** @} */

#ifdef __cplusplus
}
#endif

#endif