[3528] | 1 | /* $Id: kDbgHlp.h 3529 2007-08-20 03:25:44Z 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 "kDbgBase.h"
|
---|
| 29 |
|
---|
| 30 | #ifdef __cplusplus
|
---|
| 31 | extern "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 | */
|
---|
| 47 | void *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 | */
|
---|
| 56 | void *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 | */
|
---|
| 67 | void *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 | */
|
---|
| 82 | void *kDbgHlpRealloc(void *pv, size_t cb);
|
---|
| 83 |
|
---|
| 84 | /**
|
---|
| 85 | * Frees memory allocated by kDbgHlpAlloc, kDbgHlpAllocZ
|
---|
| 86 | * kDbgHlpAllocDup, or kDbgHlpRealloc.
|
---|
| 87 | *
|
---|
| 88 | * @param pv
|
---|
| 89 | */
|
---|
| 90 | void kDbgHlpFree(void *pv);
|
---|
| 91 |
|
---|
| 92 | /** @} */
|
---|
| 93 |
|
---|
| 94 |
|
---|
| 95 | /** @defgroup grp_kDbgHlpFile kDbg Internal File Access APIs.
|
---|
| 96 | * @internal
|
---|
| 97 | * @{
|
---|
| 98 | */
|
---|
| 99 | /** Pointer to a kDbgHlp file structure (abstract). */
|
---|
| 100 | typedef struct KDBGHLPFILE *PKDBGHLPFILE;
|
---|
| 101 |
|
---|
| 102 | /**
|
---|
| 103 | * Opens the specified file as read-only, buffered if possible.
|
---|
| 104 | *
|
---|
| 105 | * @returns 0 on success, or the appropriate KDBG_ERR_* on failure.
|
---|
| 106 | *
|
---|
| 107 | * @param pszFilename The file to open.
|
---|
| 108 | * @param ppFile Where to store the handle to the open file.
|
---|
| 109 | */
|
---|
| 110 | int kDbgHlpOpenRO(const char *pszFilename, PKDBGHLPFILE *ppFile);
|
---|
| 111 |
|
---|
| 112 |
|
---|
| 113 | /**
|
---|
| 114 | * Closes a file opened by kDbgHlpOpenRO.
|
---|
| 115 | *
|
---|
| 116 | * @param pFile The file handle.
|
---|
| 117 | */
|
---|
| 118 | void kDbgHlpClose(PKDBGHLPFILE pFile);
|
---|
| 119 |
|
---|
| 120 | /**
|
---|
[3529] | 121 | * Gets the native file handle.
|
---|
| 122 | *
|
---|
| 123 | * @return The native file handle.
|
---|
| 124 | * -1 on failure.
|
---|
| 125 | * @param pFile The file handle.
|
---|
| 126 | */
|
---|
| 127 | uintptr_t kDbgHlpNativeFileHandle(PKDBGHLPFILE pFile);
|
---|
| 128 |
|
---|
| 129 | /**
|
---|
[3528] | 130 | * Gets the size of an open file.
|
---|
| 131 | *
|
---|
| 132 | * @returns The file size in bytes on success.
|
---|
| 133 | * On failure -1 is returned.
|
---|
| 134 | * @param pFile The file handle.
|
---|
| 135 | */
|
---|
| 136 | int64_t kDbgHlpFileSize(PKDBGHLPFILE pFile);
|
---|
| 137 |
|
---|
| 138 | /**
|
---|
| 139 | * Reads a number of bytes at a specified file location.
|
---|
| 140 | *
|
---|
| 141 | * This will change the current file position to off + cb on success,
|
---|
| 142 | * while on failure the position will be undefined.
|
---|
| 143 | *
|
---|
| 144 | * @returns The file size in bytes on success.
|
---|
| 145 | * On failure -1 is returned.
|
---|
| 146 | * @param pFile The file handle.
|
---|
| 147 | * @param off Where to read.
|
---|
| 148 | * @param pv Where to store the data.
|
---|
| 149 | * @param cb How much to read.
|
---|
| 150 | */
|
---|
| 151 | int kDbgHlpReadAt(PKDBGHLPFILE pFile, int64_t off, void *pv, size_t cb);
|
---|
| 152 |
|
---|
| 153 | /**
|
---|
| 154 | * Reads a number of bytes at the current file position.
|
---|
| 155 | *
|
---|
| 156 | * This will advance the current file position by cb bytes on success
|
---|
| 157 | * while on failure the position will be undefined.
|
---|
| 158 | *
|
---|
| 159 | * @returns The file size in bytes on success.
|
---|
| 160 | * On failure -1 is returned.
|
---|
| 161 | * @param pFile The file handle.
|
---|
| 162 | * @param pv Where to store the data.
|
---|
| 163 | * @param cb How much to read.
|
---|
| 164 | * @param off Where to read.
|
---|
| 165 | */
|
---|
| 166 | int kDbgHlpRead(PKDBGHLPFILE pFile, void *pv, size_t cb);
|
---|
| 167 |
|
---|
| 168 | /**
|
---|
| 169 | * Sets the current file position.
|
---|
| 170 | *
|
---|
| 171 | * @returns 0 on success, and KDBG_ERR_* on failure.
|
---|
| 172 | * @param pFile The file handle.
|
---|
| 173 | * @param off The desired file position.
|
---|
| 174 | */
|
---|
| 175 | int kDbgHlpSeek(PKDBGHLPFILE pFile, int64_t off);
|
---|
| 176 |
|
---|
| 177 | /**
|
---|
| 178 | * Move the file position relative to the current one.
|
---|
| 179 | *
|
---|
| 180 | * @returns 0 on success, and KDBG_ERR_* on failure.
|
---|
| 181 | * @param pFile The file handle.
|
---|
| 182 | * @param off How much to move the file position by.
|
---|
| 183 | */
|
---|
| 184 | int kDbgHlpSeekByCur(PKDBGHLPFILE pFile, int64_t off);
|
---|
| 185 |
|
---|
| 186 | /**
|
---|
| 187 | * Move the file position relative to the end of the file.
|
---|
| 188 | *
|
---|
| 189 | * @returns 0 on success, and KDBG_ERR_* on failure.
|
---|
| 190 | * @param pFile The file handle.
|
---|
| 191 | * @param off The offset relative to the end, positive number.
|
---|
| 192 | */
|
---|
| 193 | int kDbgHlpSeekByEnd(PKDBGHLPFILE pFile, int64_t off);
|
---|
| 194 |
|
---|
| 195 | /**
|
---|
| 196 | * Gets the current file position.
|
---|
| 197 | *
|
---|
| 198 | * @returns The current file position on success.
|
---|
| 199 | * -1 on failure.
|
---|
| 200 | * @param pFile The file handle.
|
---|
| 201 | */
|
---|
| 202 | int64_t kDbgHlpTell(PKDBGHLPFILE pFile);
|
---|
| 203 |
|
---|
| 204 | /** @} */
|
---|
| 205 |
|
---|
| 206 | /** @defgroup grp_kDbgHlpAssert kDbg Internal Assertion Macros.
|
---|
| 207 | * @internal
|
---|
| 208 | * @{
|
---|
| 209 | */
|
---|
| 210 |
|
---|
| 211 | #ifdef _MSC_VER
|
---|
| 212 | # define kDbgAssertBreakpoint() do { __debugbreak(); } while (0)
|
---|
| 213 | #else
|
---|
| 214 | # define kDbgAssertBreakpoint() do { __asm__ __volatile__ ("int3"); } while (0)
|
---|
| 215 | #endif
|
---|
| 216 |
|
---|
[3529] | 217 | /**
|
---|
| 218 | * Helper function that displays the first part of the assertion message.
|
---|
| 219 | *
|
---|
| 220 | * @param pszExpr The expression.
|
---|
| 221 | * @param pszFile The file name.
|
---|
| 222 | * @param iLine The line number is the file.
|
---|
| 223 | * @param pszFunction The function name.
|
---|
| 224 | */
|
---|
| 225 | void kDbgAssertMsg1(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction);
|
---|
| 226 |
|
---|
| 227 | /**
|
---|
| 228 | * Helper function that displays custom assert message.
|
---|
| 229 | *
|
---|
| 230 | * @param pszFormat Format string that get passed to vprintf.
|
---|
| 231 | * @param ... Format arguments.
|
---|
| 232 | */
|
---|
| 233 | void kDbgAssertMsg2(const char *pszFormat, ...);
|
---|
| 234 |
|
---|
| 235 |
|
---|
[3528] | 236 | #ifdef KDBG_STRICT
|
---|
[3529] | 237 |
|
---|
[3528] | 238 | # define kDbgAssert(expr) \
|
---|
| 239 | do { \
|
---|
| 240 | if (!(expr)) \
|
---|
| 241 | { \
|
---|
| 242 | kDbgAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
|
---|
| 243 | kDbgAssertBreakpoint(); \
|
---|
| 244 | }
|
---|
| 245 | } while (0)
|
---|
| 246 |
|
---|
| 247 | # define kDbgAssertReturn(expr, rcRet) \
|
---|
| 248 | do { \
|
---|
| 249 | if (!(expr)) \
|
---|
| 250 | { \
|
---|
| 251 | kDbgAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
|
---|
| 252 | kDbgAssertBreakpoint(); \
|
---|
| 253 | return (rcRet); \
|
---|
| 254 | }
|
---|
| 255 | } while (0)
|
---|
| 256 |
|
---|
| 257 | # define kDbgAssertMsg(expr, msg) \
|
---|
| 258 | do { \
|
---|
| 259 | if (!(expr)) \
|
---|
| 260 | { \
|
---|
| 261 | kDbgAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
|
---|
| 262 | kDbgAssertMsg2 msg; \
|
---|
| 263 | kDbgAssertBreakpoint(); \
|
---|
| 264 | }
|
---|
| 265 | } while (0)
|
---|
| 266 |
|
---|
| 267 | # define kDbgAssertMsgReturn(expr, msg, rcRet) \
|
---|
| 268 | do { \
|
---|
| 269 | if (!(expr)) \
|
---|
| 270 | { \
|
---|
| 271 | kDbgAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
|
---|
| 272 | kDbgAssertMsg2 msg; \
|
---|
| 273 | kDbgAssertBreakpoint(); \
|
---|
| 274 | return (rcRet); \
|
---|
| 275 | }
|
---|
| 276 | } while (0)
|
---|
[3529] | 277 |
|
---|
[3528] | 278 | #else /* !KDBG_STRICT */
|
---|
| 279 | # define kDbgAssert(expr) do { } while (0)
|
---|
[3529] | 280 | # define kDbgAssertReturn(expr, rcRet) return (rcRet)
|
---|
[3528] | 281 | # define kDbgAssertMsg(expr, msg) do { } while (0)
|
---|
[3529] | 282 | # define kDbgAssertMsgReturn(expr, msg, rcRet) return (rcRet)
|
---|
[3528] | 283 | #endif /* !KDBG_STRICT */
|
---|
| 284 |
|
---|
| 285 | #define kDbgAssertPtr(ptr) kDbgAssertMsg(VALID_PTR(expr), ("%s = %p\n", #ptr, (ptr)))
|
---|
| 286 | #define kDbgAssertPtrReturn(ptr, rcRet) kDbgAssertMsgReturn(VALID_PTR(expr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)), (rcRet))
|
---|
| 287 | #define kDbgAssertRC(rc) kDbgAssertMsg((rc) == 0, ("%s = %d\n", #rc, (rc)))
|
---|
| 288 | #define kDbgAssertRCReturn(rc, rcRet) kDbgAssertMsgReturn((rc) == 0, ("%s = %d -> %d\n", #rc, (rc), (rcRet)), (rcRet))
|
---|
[3529] | 289 | #define kDbgAssertFailed() kDbgAssert(0)
|
---|
| 290 | #define kDbgAssertFailedReturn(rcRet) kDbgAssertReturn(0, (rcRet))
|
---|
| 291 | #define kDbgAssertMsgFailed(msg) kDbgAssertMsg(0, msg)
|
---|
| 292 | #define kDbgAssertMsgFailedReturn(msg, rcRet) kDbgAssertMsgReturn(0, msg, (rcRet))
|
---|
[3528] | 293 |
|
---|
| 294 | /** @} */
|
---|
| 295 |
|
---|
| 296 | #ifdef __cplusplus
|
---|
| 297 | }
|
---|
| 298 | #endif
|
---|
| 299 |
|
---|
| 300 | #endif
|
---|
| 301 |
|
---|