[3528] | 1 | /* $Id: kDbgModule.cpp 3530 2007-08-20 03:42:03Z 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)
|
---|
[3530] | 106 | {
|
---|
| 107 | # ifdef KS_OS_WINDOWS
|
---|
| 108 | rc = kdbgModWinDbgHelpOpen(pFile, offHdr, pszModulePath, ppDbgMod);
|
---|
| 109 | if ( rc
|
---|
| 110 | && !kdbgModPEOpen(pFile, offHdr, pszModulePath, ppDbgMod))
|
---|
| 111 | rc = 0;
|
---|
| 112 | # endif
|
---|
[3528] | 113 | rc = kdbgModPEOpen(pFile, offHdr, pszModulePath, ppDbgMod);
|
---|
[3530] | 114 | }
|
---|
[3528] | 115 | #endif
|
---|
| 116 | /** @todo there are a number of text file formats too which I want to support. */
|
---|
| 117 | else
|
---|
| 118 | rc = KDBG_ERR_UNKOWN_FORMAT;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | if (rc)
|
---|
| 122 | kDbgHlpClose(pFile);
|
---|
| 123 | return rc;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 |
|
---|
| 127 | /**
|
---|
| 128 | * Validates a debug module handle.
|
---|
| 129 | * All necessary asserting will be taken care of here.
|
---|
| 130 | *
|
---|
| 131 | * @returns True / false.
|
---|
| 132 | * @param pMod The debug module handle.
|
---|
| 133 | */
|
---|
| 134 | KDBG_INLINE(bool) kdbgModIsValid(PKDBGMOD pMod)
|
---|
| 135 | {
|
---|
| 136 | kDbgAssertPtrReturn(pMod, false);
|
---|
| 137 | kDbgAssertMsgReturn(pMod->u32Magic == KDBGMOD_MAGIC, ("%#x", pMod->u32Magic), false);
|
---|
| 138 | kDbgAssertPtrReturn(pMod->pOps, false);
|
---|
| 139 | return true;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 |
|
---|
| 143 | /**
|
---|
| 144 | * Closes the module.
|
---|
| 145 | *
|
---|
| 146 | * @returns IPRT status code.
|
---|
| 147 | * @param pMod The module handle.
|
---|
| 148 | */
|
---|
| 149 | KDBG_DECL(int) kDbgModuleClose(PKDBGMOD pMod)
|
---|
| 150 | {
|
---|
| 151 | if (!kdbgModIsValid(pMod))
|
---|
| 152 | return KDBG_ERR_INVALID_PARAMETER;
|
---|
| 153 |
|
---|
| 154 | int rc = pMod->pOps->pfnClose(pMod);
|
---|
| 155 | if (!rc)
|
---|
| 156 | kDbgHlpFree(pMod);
|
---|
| 157 | return rc;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 |
|
---|
| 161 | /**
|
---|
| 162 | * Gets a symbol by segment:offset.
|
---|
| 163 | * This will be approximated to the nearest symbol if there is no exact match.
|
---|
| 164 | *
|
---|
| 165 | * @returns IPRT status code.
|
---|
| 166 | * @param pMod The module.
|
---|
| 167 | * @param iSegment The segment this offset is relative to.
|
---|
| 168 | * The -1 segment is special, it means that the addres is relative to
|
---|
| 169 | * the image base. The image base is where the first bit of the image
|
---|
| 170 | * is mapped during load.
|
---|
| 171 | * @param off The offset into the segment.
|
---|
| 172 | * @param pSym Where to store the symbol details.
|
---|
| 173 | */
|
---|
| 174 | KDBG_DECL(int) kDbgModuleQuerySymbol(PKDBGMOD pMod, int32_t iSegment, KDBGADDR off, PKDBGSYMBOL pSym)
|
---|
| 175 | {
|
---|
| 176 | if (!kdbgModIsValid(pMod))
|
---|
| 177 | return KDBG_ERR_INVALID_PARAMETER;
|
---|
| 178 | kDbgAssertPtrReturn(pSym, KDBG_ERR_INVALID_POINTER);
|
---|
| 179 |
|
---|
| 180 | return pMod->pOps->pfnQuerySymbol(pMod, iSegment, off, pSym);
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 |
|
---|
| 184 | /**
|
---|
| 185 | * Gets & allocates a symbol by segment:offset.
|
---|
| 186 | * This will be approximated to the nearest symbol if there is no exact match.
|
---|
| 187 | *
|
---|
| 188 | * @returns IPRT status code.
|
---|
| 189 | * @param pMod The module.
|
---|
| 190 | * @param iSegment The segment this offset is relative to.
|
---|
| 191 | * The -1 segment is special, it means that the addres is relative to
|
---|
| 192 | * the image base. The image base is where the first bit of the image
|
---|
| 193 | * is mapped during load.
|
---|
| 194 | * @param off The offset into the segment.
|
---|
| 195 | * @param ppSym Where to store the pointer to the symbol info.
|
---|
| 196 | * Free the returned symbol using kDbgSymbolFree().
|
---|
| 197 | */
|
---|
| 198 | KDBG_DECL(int) kDbgModuleQuerySymbolA(PKDBGMOD pMod, int32_t iSegment, KDBGADDR off, PPKDBGSYMBOL ppSym)
|
---|
| 199 | {
|
---|
| 200 | kDbgAssertPtrReturn(ppSym, KDBG_ERR_INVALID_POINTER);
|
---|
| 201 |
|
---|
| 202 | KDBGSYMBOL Sym;
|
---|
| 203 | int rc = kDbgModuleQuerySymbol(pMod, iSegment, off, &Sym);
|
---|
| 204 | if (!rc)
|
---|
| 205 | {
|
---|
| 206 | *ppSym = kDbgSymbolDup(&Sym);
|
---|
| 207 | if (!*ppSym)
|
---|
| 208 | rc = KDBG_ERR_NO_MEMORY;
|
---|
| 209 | }
|
---|
| 210 | else
|
---|
| 211 | *ppSym = NULL;
|
---|
| 212 | return rc;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 |
|
---|
| 216 | /**
|
---|
| 217 | * Gets a line number entry by segment:offset.
|
---|
| 218 | * This will be approximated to the nearest line number there is no exact match.
|
---|
| 219 | *
|
---|
| 220 | * @returns IPRT status code.
|
---|
| 221 | * @param pMod The module.
|
---|
| 222 | * @param iSegment The segment this offset is relative to.
|
---|
| 223 | * The -1 segment is special, it means that the addres is relative to
|
---|
| 224 | * the image base. The image base is where the first bit of the image
|
---|
| 225 | * is mapped during load.
|
---|
| 226 | * @param off The offset into the segment.
|
---|
| 227 | * @param pLine Where to store the line number details.
|
---|
| 228 | */
|
---|
| 229 | KDBG_DECL(int) kDbgModuleQueryLine(PKDBGMOD pMod, int32_t iSegment, KDBGADDR off, PKDBGLINE pLine)
|
---|
| 230 | {
|
---|
| 231 | if (!kdbgModIsValid(pMod))
|
---|
| 232 | return KDBG_ERR_INVALID_PARAMETER;
|
---|
| 233 | kDbgAssertPtrReturn(pLine, KDBG_ERR_INVALID_POINTER);
|
---|
| 234 |
|
---|
| 235 | return pMod->pOps->pfnQueryLine(pMod, iSegment, off, pLine);
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 |
|
---|
| 239 | /**
|
---|
| 240 | * Gets & allocates a line number entry by segment:offset.
|
---|
| 241 | * This will be approximated to the nearest line number there is no exact match.
|
---|
| 242 | *
|
---|
| 243 | * @returns IPRT status code.
|
---|
| 244 | * @param pMod The module.
|
---|
| 245 | * @param iSegment The segment this offset is relative to.
|
---|
| 246 | * The -1 segment is special, it means that the addres is relative to
|
---|
| 247 | * the image base. The image base is where the first bit of the image
|
---|
| 248 | * is mapped during load.
|
---|
| 249 | * @param off The offset into the segment.
|
---|
| 250 | * @param ppLine Where to store the pointer to the line number info.
|
---|
| 251 | * Free the returned line number using kDbgLineFree().
|
---|
| 252 | */
|
---|
| 253 | KDBG_DECL(int) kDbgModuleQueryLineA(PKDBGMOD pMod, int32_t iSegment, KDBGADDR off, PPKDBGLINE ppLine)
|
---|
| 254 | {
|
---|
| 255 | kDbgAssertPtrReturn(ppLine, KDBG_ERR_INVALID_POINTER);
|
---|
| 256 |
|
---|
| 257 | KDBGLINE Line;
|
---|
| 258 | int rc = kDbgModuleQueryLine(pMod, iSegment, off, &Line);
|
---|
| 259 | if (!rc)
|
---|
| 260 | {
|
---|
| 261 | *ppLine = kDbgLineDup(&Line);
|
---|
| 262 | if (!*ppLine)
|
---|
| 263 | rc = KDBG_ERR_NO_MEMORY;
|
---|
| 264 | }
|
---|
| 265 | else
|
---|
| 266 | *ppLine = NULL;
|
---|
| 267 | return rc;
|
---|
| 268 | }
|
---|
| 269 |
|
---|