[3528] | 1 | /* $Id: kDbgModule.cpp 3528 2007-08-20 02:43:13Z bird $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * kDbg - The Debug Info Reader, Module API.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
| 7 | * Copyright (c) 2006-2007 knut st. osmundsen <bird-src-spam@anduin.net>
|
---|
| 8 | *
|
---|
| 9 | * This file is part of kLIBC.
|
---|
| 10 | *
|
---|
| 11 | * kLIBC is free software; you can redistribute it and/or modify
|
---|
| 12 | * it under the terms of the GNU General Public License as published by
|
---|
| 13 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 14 | * (at your option) any later version.
|
---|
| 15 | *
|
---|
| 16 | * kLIBC is distributed in the hope that it will be useful,
|
---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 19 | * GNU General Public License for more details.
|
---|
| 20 | *
|
---|
| 21 | * You should have received a copy of the GNU General Public License
|
---|
| 22 | * along with kLIBC; if not, write to the Free Software
|
---|
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
| 24 | *
|
---|
| 25 | */
|
---|
| 26 |
|
---|
| 27 |
|
---|
| 28 | /*******************************************************************************
|
---|
| 29 | * Header Files *
|
---|
| 30 | *******************************************************************************/
|
---|
| 31 | #include "kDbg.h"
|
---|
| 32 | #include "kDbgInternal.h"
|
---|
| 33 |
|
---|
| 34 | #include <kLdrModMZ.h>
|
---|
| 35 | #include <kLdrModPE.h>
|
---|
| 36 |
|
---|
| 37 |
|
---|
| 38 |
|
---|
| 39 | /**
|
---|
| 40 | * Opens the debug info for a specified executable module.
|
---|
| 41 | *
|
---|
| 42 | * @returns IPRT status code.
|
---|
| 43 | * @param pszModulePath The path to the executable module.
|
---|
| 44 | * @param ppDbgMod Where to store the debug module handle.
|
---|
| 45 | */
|
---|
| 46 | KDBG_DECL(int) kDbgModuleOpen(const char *pszModulePath, PKDBGMOD *ppDbgMod)
|
---|
| 47 | {
|
---|
| 48 | /*
|
---|
| 49 | * Validate input.
|
---|
| 50 | */
|
---|
| 51 | kDbgAssertPtrReturn(pszModulePath, KDBG_ERR_INVALID_POINTER);
|
---|
| 52 | kDbgAssertMsgReturn(*pszModulePath, ("%p\n", pszModulePath), KDBG_ERR_INVALID_PARAMETER);
|
---|
| 53 | kDbgAssertPtrReturn(ppDbgMod, KDBG_ERR_INVALID_POINTER);
|
---|
| 54 | *ppDbgMod = NULL;
|
---|
| 55 |
|
---|
| 56 | /*
|
---|
| 57 | * The file and try figure out the format.
|
---|
| 58 | */
|
---|
| 59 | PKDBGHLPFILE pFile;
|
---|
| 60 | int rc = kDbgHlpOpenRO(pszModulePath, &pFile);
|
---|
| 61 | if (rc)
|
---|
| 62 | return rc;
|
---|
| 63 |
|
---|
| 64 | int64_t offHdr = 0;
|
---|
| 65 | union
|
---|
| 66 | {
|
---|
| 67 | uint32_t au32[4];
|
---|
| 68 | uint16_t au16[8];
|
---|
| 69 | uint8_t ab[16];
|
---|
| 70 | } Buf;
|
---|
| 71 | rc = kDbgHlpRead(pFile, &Buf, sizeof(Buf));
|
---|
| 72 | if ( !rc
|
---|
| 73 | && Buf.au16[0] == IMAGE_DOS_SIGNATURE)
|
---|
| 74 | {
|
---|
| 75 | /* new header? */
|
---|
| 76 | uint32_t offNewHeader;
|
---|
| 77 | rc = kDbgHlpReadAt(pFile, KDBG_OFFSETOF(IMAGE_DOS_HEADER, e_lfanew), &offNewHeader, sizeof(offNewHeader));
|
---|
| 78 | if (!rc && offNewHeader)
|
---|
| 79 | {
|
---|
| 80 | offHdr = offNewHeader;
|
---|
| 81 | rc = kDbgHlpReadAt(pFile, offNewHeader, &Buf, sizeof(Buf));
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | if (!rc)
|
---|
| 85 | {
|
---|
| 86 | if (Buf.au16[0] == IMAGE_DOS_SIGNATURE)
|
---|
| 87 | rc = KDBG_ERR_FORMAT_NOT_SUPPORTED;
|
---|
| 88 | #ifdef IMAGE_NE_SIGNATURE
|
---|
| 89 | else if (Buf.au16[0] == IMAGE_NE_SIGNATURE)
|
---|
| 90 | rc = KDBG_ERR_FORMAT_NOT_SUPPORTED;
|
---|
| 91 | #endif
|
---|
| 92 | #ifdef IMAGE_LX_SIGNATURE
|
---|
| 93 | else if (Buf.au16[0] == IMAGE_LX_SIGNATURE)
|
---|
| 94 | rc = KDBG_ERR_FORMAT_NOT_SUPPORTED;
|
---|
| 95 | #endif
|
---|
| 96 | #ifdef IMAGE_LE_SIGNATURE
|
---|
| 97 | else if (Buf.au16[0] == IMAGE_LE_SIGNATURE)
|
---|
| 98 | rc = KDBG_ERR_FORMAT_NOT_SUPPORTED;
|
---|
| 99 | #endif
|
---|
| 100 | #ifdef IMAGE_ELF_SIGNATURE
|
---|
| 101 | else if (Buf.au32[0] == IMAGE_ELF_SIGNATURE)
|
---|
| 102 | rc = KDBG_ERR_FORMAT_NOT_SUPPORTED;
|
---|
| 103 | #endif
|
---|
| 104 | #ifdef IMAGE_NT_SIGNATURE
|
---|
| 105 | else if (Buf.au32[0] == IMAGE_NT_SIGNATURE)
|
---|
| 106 | rc = kdbgModPEOpen(pFile, offHdr, pszModulePath, ppDbgMod);
|
---|
| 107 | #endif
|
---|
| 108 | /** @todo there are a number of text file formats too which I want to support. */
|
---|
| 109 | else
|
---|
| 110 | rc = KDBG_ERR_UNKOWN_FORMAT;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | if (rc)
|
---|
| 114 | kDbgHlpClose(pFile);
|
---|
| 115 | return rc;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 |
|
---|
| 119 | /**
|
---|
| 120 | * Validates a debug module handle.
|
---|
| 121 | * All necessary asserting will be taken care of here.
|
---|
| 122 | *
|
---|
| 123 | * @returns True / false.
|
---|
| 124 | * @param pMod The debug module handle.
|
---|
| 125 | */
|
---|
| 126 | KDBG_INLINE(bool) kdbgModIsValid(PKDBGMOD pMod)
|
---|
| 127 | {
|
---|
| 128 | kDbgAssertPtrReturn(pMod, false);
|
---|
| 129 | kDbgAssertMsgReturn(pMod->u32Magic == KDBGMOD_MAGIC, ("%#x", pMod->u32Magic), false);
|
---|
| 130 | kDbgAssertPtrReturn(pMod->pOps, false);
|
---|
| 131 | return true;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 |
|
---|
| 135 | /**
|
---|
| 136 | * Closes the module.
|
---|
| 137 | *
|
---|
| 138 | * @returns IPRT status code.
|
---|
| 139 | * @param pMod The module handle.
|
---|
| 140 | */
|
---|
| 141 | KDBG_DECL(int) kDbgModuleClose(PKDBGMOD pMod)
|
---|
| 142 | {
|
---|
| 143 | if (!kdbgModIsValid(pMod))
|
---|
| 144 | return KDBG_ERR_INVALID_PARAMETER;
|
---|
| 145 |
|
---|
| 146 | int rc = pMod->pOps->pfnClose(pMod);
|
---|
| 147 | if (!rc)
|
---|
| 148 | kDbgHlpFree(pMod);
|
---|
| 149 | return rc;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 |
|
---|
| 153 | /**
|
---|
| 154 | * Gets a symbol by segment:offset.
|
---|
| 155 | * This will be approximated to the nearest symbol if there is no exact match.
|
---|
| 156 | *
|
---|
| 157 | * @returns IPRT status code.
|
---|
| 158 | * @param pMod The module.
|
---|
| 159 | * @param iSegment The segment this offset is relative to.
|
---|
| 160 | * The -1 segment is special, it means that the addres is relative to
|
---|
| 161 | * the image base. The image base is where the first bit of the image
|
---|
| 162 | * is mapped during load.
|
---|
| 163 | * @param off The offset into the segment.
|
---|
| 164 | * @param pSym Where to store the symbol details.
|
---|
| 165 | */
|
---|
| 166 | KDBG_DECL(int) kDbgModuleQuerySymbol(PKDBGMOD pMod, int32_t iSegment, KDBGADDR off, PKDBGSYMBOL pSym)
|
---|
| 167 | {
|
---|
| 168 | if (!kdbgModIsValid(pMod))
|
---|
| 169 | return KDBG_ERR_INVALID_PARAMETER;
|
---|
| 170 | kDbgAssertPtrReturn(pSym, KDBG_ERR_INVALID_POINTER);
|
---|
| 171 |
|
---|
| 172 | return pMod->pOps->pfnQuerySymbol(pMod, iSegment, off, pSym);
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 |
|
---|
| 176 | /**
|
---|
| 177 | * Gets & allocates a symbol by segment:offset.
|
---|
| 178 | * This will be approximated to the nearest symbol if there is no exact match.
|
---|
| 179 | *
|
---|
| 180 | * @returns IPRT status code.
|
---|
| 181 | * @param pMod The module.
|
---|
| 182 | * @param iSegment The segment this offset is relative to.
|
---|
| 183 | * The -1 segment is special, it means that the addres is relative to
|
---|
| 184 | * the image base. The image base is where the first bit of the image
|
---|
| 185 | * is mapped during load.
|
---|
| 186 | * @param off The offset into the segment.
|
---|
| 187 | * @param ppSym Where to store the pointer to the symbol info.
|
---|
| 188 | * Free the returned symbol using kDbgSymbolFree().
|
---|
| 189 | */
|
---|
| 190 | KDBG_DECL(int) kDbgModuleQuerySymbolA(PKDBGMOD pMod, int32_t iSegment, KDBGADDR off, PPKDBGSYMBOL ppSym)
|
---|
| 191 | {
|
---|
| 192 | kDbgAssertPtrReturn(ppSym, KDBG_ERR_INVALID_POINTER);
|
---|
| 193 |
|
---|
| 194 | KDBGSYMBOL Sym;
|
---|
| 195 | int rc = kDbgModuleQuerySymbol(pMod, iSegment, off, &Sym);
|
---|
| 196 | if (!rc)
|
---|
| 197 | {
|
---|
| 198 | *ppSym = kDbgSymbolDup(&Sym);
|
---|
| 199 | if (!*ppSym)
|
---|
| 200 | rc = KDBG_ERR_NO_MEMORY;
|
---|
| 201 | }
|
---|
| 202 | else
|
---|
| 203 | *ppSym = NULL;
|
---|
| 204 | return rc;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 |
|
---|
| 208 | /**
|
---|
| 209 | * Gets a line number entry by segment:offset.
|
---|
| 210 | * This will be approximated to the nearest line number there is no exact match.
|
---|
| 211 | *
|
---|
| 212 | * @returns IPRT status code.
|
---|
| 213 | * @param pMod The module.
|
---|
| 214 | * @param iSegment The segment this offset is relative to.
|
---|
| 215 | * The -1 segment is special, it means that the addres is relative to
|
---|
| 216 | * the image base. The image base is where the first bit of the image
|
---|
| 217 | * is mapped during load.
|
---|
| 218 | * @param off The offset into the segment.
|
---|
| 219 | * @param pLine Where to store the line number details.
|
---|
| 220 | */
|
---|
| 221 | KDBG_DECL(int) kDbgModuleQueryLine(PKDBGMOD pMod, int32_t iSegment, KDBGADDR off, PKDBGLINE pLine)
|
---|
| 222 | {
|
---|
| 223 | if (!kdbgModIsValid(pMod))
|
---|
| 224 | return KDBG_ERR_INVALID_PARAMETER;
|
---|
| 225 | kDbgAssertPtrReturn(pLine, KDBG_ERR_INVALID_POINTER);
|
---|
| 226 |
|
---|
| 227 | return pMod->pOps->pfnQueryLine(pMod, iSegment, off, pLine);
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 |
|
---|
| 231 | /**
|
---|
| 232 | * Gets & allocates a line number entry by segment:offset.
|
---|
| 233 | * This will be approximated to the nearest line number there is no exact match.
|
---|
| 234 | *
|
---|
| 235 | * @returns IPRT status code.
|
---|
| 236 | * @param pMod The module.
|
---|
| 237 | * @param iSegment The segment this offset is relative to.
|
---|
| 238 | * The -1 segment is special, it means that the addres is relative to
|
---|
| 239 | * the image base. The image base is where the first bit of the image
|
---|
| 240 | * is mapped during load.
|
---|
| 241 | * @param off The offset into the segment.
|
---|
| 242 | * @param ppLine Where to store the pointer to the line number info.
|
---|
| 243 | * Free the returned line number using kDbgLineFree().
|
---|
| 244 | */
|
---|
| 245 | KDBG_DECL(int) kDbgModuleQueryLineA(PKDBGMOD pMod, int32_t iSegment, KDBGADDR off, PPKDBGLINE ppLine)
|
---|
| 246 | {
|
---|
| 247 | kDbgAssertPtrReturn(ppLine, KDBG_ERR_INVALID_POINTER);
|
---|
| 248 |
|
---|
| 249 | KDBGLINE Line;
|
---|
| 250 | int rc = kDbgModuleQueryLine(pMod, iSegment, off, &Line);
|
---|
| 251 | if (!rc)
|
---|
| 252 | {
|
---|
| 253 | *ppLine = kDbgLineDup(&Line);
|
---|
| 254 | if (!*ppLine)
|
---|
| 255 | rc = KDBG_ERR_NO_MEMORY;
|
---|
| 256 | }
|
---|
| 257 | else
|
---|
| 258 | *ppLine = NULL;
|
---|
| 259 | return rc;
|
---|
| 260 | }
|
---|
| 261 |
|
---|