| 1 | /* $Id: kDbgHlp.h 3528 2007-08-20 02:43:13Z 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 | /**
|
|---|
| 121 | * Gets the size of an open file.
|
|---|
| 122 | *
|
|---|
| 123 | * @returns The file size in bytes on success.
|
|---|
| 124 | * On failure -1 is returned.
|
|---|
| 125 | * @param pFile The file handle.
|
|---|
| 126 | */
|
|---|
| 127 | int64_t kDbgHlpFileSize(PKDBGHLPFILE pFile);
|
|---|
| 128 |
|
|---|
| 129 | /**
|
|---|
| 130 | * Reads a number of bytes at a specified file location.
|
|---|
| 131 | *
|
|---|
| 132 | * This will change the current file position to off + cb on success,
|
|---|
| 133 | * while on failure the position will be undefined.
|
|---|
| 134 | *
|
|---|
| 135 | * @returns The file size in bytes on success.
|
|---|
| 136 | * On failure -1 is returned.
|
|---|
| 137 | * @param pFile The file handle.
|
|---|
| 138 | * @param off Where to read.
|
|---|
| 139 | * @param pv Where to store the data.
|
|---|
| 140 | * @param cb How much to read.
|
|---|
| 141 | */
|
|---|
| 142 | int kDbgHlpReadAt(PKDBGHLPFILE pFile, int64_t off, void *pv, size_t cb);
|
|---|
| 143 |
|
|---|
| 144 | /**
|
|---|
| 145 | * Reads a number of bytes at the current file position.
|
|---|
| 146 | *
|
|---|
| 147 | * This will advance the current file position by cb bytes on success
|
|---|
| 148 | * while on failure the position will be undefined.
|
|---|
| 149 | *
|
|---|
| 150 | * @returns The file size in bytes on success.
|
|---|
| 151 | * On failure -1 is returned.
|
|---|
| 152 | * @param pFile The file handle.
|
|---|
| 153 | * @param pv Where to store the data.
|
|---|
| 154 | * @param cb How much to read.
|
|---|
| 155 | * @param off Where to read.
|
|---|
| 156 | */
|
|---|
| 157 | int kDbgHlpRead(PKDBGHLPFILE pFile, void *pv, size_t cb);
|
|---|
| 158 |
|
|---|
| 159 | /**
|
|---|
| 160 | * Sets the current file position.
|
|---|
| 161 | *
|
|---|
| 162 | * @returns 0 on success, and KDBG_ERR_* on failure.
|
|---|
| 163 | * @param pFile The file handle.
|
|---|
| 164 | * @param off The desired file position.
|
|---|
| 165 | */
|
|---|
| 166 | int kDbgHlpSeek(PKDBGHLPFILE pFile, int64_t off);
|
|---|
| 167 |
|
|---|
| 168 | /**
|
|---|
| 169 | * Move the file position relative to the current one.
|
|---|
| 170 | *
|
|---|
| 171 | * @returns 0 on success, and KDBG_ERR_* on failure.
|
|---|
| 172 | * @param pFile The file handle.
|
|---|
| 173 | * @param off How much to move the file position by.
|
|---|
| 174 | */
|
|---|
| 175 | int kDbgHlpSeekByCur(PKDBGHLPFILE pFile, int64_t off);
|
|---|
| 176 |
|
|---|
| 177 | /**
|
|---|
| 178 | * Move the file position relative to the end of the file.
|
|---|
| 179 | *
|
|---|
| 180 | * @returns 0 on success, and KDBG_ERR_* on failure.
|
|---|
| 181 | * @param pFile The file handle.
|
|---|
| 182 | * @param off The offset relative to the end, positive number.
|
|---|
| 183 | */
|
|---|
| 184 | int kDbgHlpSeekByEnd(PKDBGHLPFILE pFile, int64_t off);
|
|---|
| 185 |
|
|---|
| 186 | /**
|
|---|
| 187 | * Gets the current file position.
|
|---|
| 188 | *
|
|---|
| 189 | * @returns The current file position on success.
|
|---|
| 190 | * -1 on failure.
|
|---|
| 191 | * @param pFile The file handle.
|
|---|
| 192 | */
|
|---|
| 193 | int64_t kDbgHlpTell(PKDBGHLPFILE pFile);
|
|---|
| 194 |
|
|---|
| 195 | /** @} */
|
|---|
| 196 |
|
|---|
| 197 | /** @defgroup grp_kDbgHlpAssert kDbg Internal Assertion Macros.
|
|---|
| 198 | * @internal
|
|---|
| 199 | * @{
|
|---|
| 200 | */
|
|---|
| 201 |
|
|---|
| 202 | #ifdef _MSC_VER
|
|---|
| 203 | # define kDbgAssertBreakpoint() do { __debugbreak(); } while (0)
|
|---|
| 204 | #else
|
|---|
| 205 | # define kDbgAssertBreakpoint() do { __asm__ __volatile__ ("int3"); } while (0)
|
|---|
| 206 | #endif
|
|---|
| 207 |
|
|---|
| 208 | #ifdef KDBG_STRICT
|
|---|
| 209 | # define kDbgAssert(expr) \
|
|---|
| 210 | do { \
|
|---|
| 211 | if (!(expr)) \
|
|---|
| 212 | { \
|
|---|
| 213 | kDbgAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
|
|---|
| 214 | kDbgAssertBreakpoint(); \
|
|---|
| 215 | }
|
|---|
| 216 | } while (0)
|
|---|
| 217 |
|
|---|
| 218 | # define kDbgAssertReturn(expr, rcRet) \
|
|---|
| 219 | do { \
|
|---|
| 220 | if (!(expr)) \
|
|---|
| 221 | { \
|
|---|
| 222 | kDbgAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
|
|---|
| 223 | kDbgAssertBreakpoint(); \
|
|---|
| 224 | return (rcRet); \
|
|---|
| 225 | }
|
|---|
| 226 | } while (0)
|
|---|
| 227 |
|
|---|
| 228 | # define kDbgAssertMsg(expr, msg) \
|
|---|
| 229 | do { \
|
|---|
| 230 | if (!(expr)) \
|
|---|
| 231 | { \
|
|---|
| 232 | kDbgAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
|
|---|
| 233 | kDbgAssertMsg2 msg; \
|
|---|
| 234 | kDbgAssertBreakpoint(); \
|
|---|
| 235 | }
|
|---|
| 236 | } while (0)
|
|---|
| 237 |
|
|---|
| 238 | # define kDbgAssertMsgReturn(expr, msg, rcRet) \
|
|---|
| 239 | do { \
|
|---|
| 240 | if (!(expr)) \
|
|---|
| 241 | { \
|
|---|
| 242 | kDbgAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
|
|---|
| 243 | kDbgAssertMsg2 msg; \
|
|---|
| 244 | kDbgAssertBreakpoint(); \
|
|---|
| 245 | return (rcRet); \
|
|---|
| 246 | }
|
|---|
| 247 | } while (0)
|
|---|
| 248 | #else /* !KDBG_STRICT */
|
|---|
| 249 | # define kDbgAssert(expr) do { } while (0)
|
|---|
| 250 | # define kDbgAssertReturn(expr, rcRet) do { } while (0)
|
|---|
| 251 | # define kDbgAssertMsg(expr, msg) do { } while (0)
|
|---|
| 252 | # define kDbgAssertMsgReturn(expr, msg, rcRet) do { } while (0)
|
|---|
| 253 | #endif /* !KDBG_STRICT */
|
|---|
| 254 |
|
|---|
| 255 | #define kDbgAssertPtr(ptr) kDbgAssertMsg(VALID_PTR(expr), ("%s = %p\n", #ptr, (ptr)))
|
|---|
| 256 | #define kDbgAssertPtrReturn(ptr, rcRet) kDbgAssertMsgReturn(VALID_PTR(expr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)), (rcRet))
|
|---|
| 257 | #define kDbgAssertRC(rc) kDbgAssertMsg((rc) == 0, ("%s = %d\n", #rc, (rc)))
|
|---|
| 258 | #define kDbgAssertRCReturn(rc, rcRet) kDbgAssertMsgReturn((rc) == 0, ("%s = %d -> %d\n", #rc, (rc), (rcRet)), (rcRet))
|
|---|
| 259 |
|
|---|
| 260 | /** @} */
|
|---|
| 261 |
|
|---|
| 262 | #ifdef __cplusplus
|
|---|
| 263 | }
|
|---|
| 264 | #endif
|
|---|
| 265 |
|
|---|
| 266 | #endif
|
|---|
| 267 |
|
|---|