Ignore:
Timestamp:
Aug 20, 2007, 4:43:13 AM (18 years ago)
Author:
bird
Message:

Some refactoring and OS abstraction by instroducing kDbgHlp.

File:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/kDbg/kDbgModule.cpp

    • Property svn:eol-style set to native
    • Property svn:keywords set to Id
    r3526 r3528  
    1 /* $Id: $ */
     1/* $Id$ */
    22/** @file
    3  *
    4  * kProfile Mark 2 - Debug Info Reader
    5  *
    6  * Copyright (c) 2006 knut st. osmundsen <bird-src-spam@anduin.net.de>
    7  *
     3 * kDbg - The Debug Info Reader, Module API.
     4 */
     5
     6/*
     7 * Copyright (c) 2006-2007 knut st. osmundsen <bird-src-spam@anduin.net>
    88 *
    99 * This file is part of kLIBC.
     
    2929*   Header Files                                                               *
    3030*******************************************************************************/
    31 #ifdef KDBG_USE_IPRT
    32 # include <iprt/file.h>
    33 # include <iprt/string.h>
    34 # include <iprt/err.h>
    35 # include <iprt/assert.h>
    36 # include <iprt/alloc.h>
    37 #else
    38 #endif
    39 
    40 #include "kDbgBase.h"
    41 #include "DBGInternal.h"
     31#include "kDbg.h"
     32#include "kDbgInternal.h"
     33
    4234#include <kLdrModMZ.h>
    4335#include <kLdrModPE.h>
     
    5244 * @param   ppDbgMod            Where to store the debug module handle.
    5345 */
    54 RTDECL(int) RTDbgModuleOpen(const char *pszModulePath, PRTDBGMOD *ppDbgMod)
     46KDBG_DECL(int) kDbgModuleOpen(const char *pszModulePath, PKDBGMOD *ppDbgMod)
    5547{
    5648    /*
    5749     * Validate input.
    5850     */
    59     AssertMsgReturn(VALID_PTR(pszModulePath), ("%p\n", pszModulePath), VERR_INVALID_POINTER);
    60     AssertMsgReturn(*pszModulePath, ("%p\n", pszModulePath), VERR_INVALID_PARAMETER);
    61     AssertMsgReturn(VALID_PTR(ppDbgMod), ("%p\n", ppDbgMod), VERR_INVALID_POINTER);
     51    kDbgAssertPtrReturn(pszModulePath, KDBG_ERR_INVALID_POINTER);
     52    kDbgAssertMsgReturn(*pszModulePath, ("%p\n", pszModulePath), KDBG_ERR_INVALID_PARAMETER);
     53    kDbgAssertPtrReturn(ppDbgMod, KDBG_ERR_INVALID_POINTER);
    6254    *ppDbgMod = NULL;
    6355
     
    6557     * The file and try figure out the format.
    6658     */
    67     RTFILE File;
    68     int rc = RTFileOpen(&File, pszModulePath, RTFILE_O_READ | RTFILE_O_DENY_WRITE | RTFILE_O_OPEN);
    69     if (RT_FAILURE(rc))
     59    PKDBGHLPFILE pFile;
     60    int rc = kDbgHlpOpenRO(pszModulePath, &pFile);
     61    if (rc)
    7062        return rc;
    7163
    72     RTFOFF offHdr = 0;
     64    int64_t offHdr = 0;
    7365    union
    7466    {
     
    7769        uint8_t     ab[16];
    7870    } Buf;
    79     rc = RTFileRead(File, &Buf, sizeof(Buf), NULL);
    80     if (    RT_SUCCESS(rc)
     71    rc = kDbgHlpRead(pFile, &Buf, sizeof(Buf));
     72    if (    !rc
    8173        &&  Buf.au16[0] == IMAGE_DOS_SIGNATURE)
    8274    {
    8375        /* new header? */
    8476        uint32_t offNewHeader;
    85         rc = RTFileReadAt(File, KDBG_OFFSETOF(IMAGE_DOS_HEADER, e_lfanew), &offNewHeader, sizeof(offNewHeader), NULL);
    86         if (RT_SUCCESS(rc) && offNewHeader)
     77        rc = kDbgHlpReadAt(pFile, KDBG_OFFSETOF(IMAGE_DOS_HEADER, e_lfanew), &offNewHeader, sizeof(offNewHeader));
     78        if (!rc && offNewHeader)
    8779        {
    8880            offHdr = offNewHeader;
    89             rc = RTFileReadAt(File, offNewHeader, &Buf, sizeof(Buf), NULL);
     81            rc = kDbgHlpReadAt(pFile, offNewHeader, &Buf, sizeof(Buf));
    9082        }
    9183    }
    92     if (RT_SUCCESS(rc))
     84    if (!rc)
    9385    {
    9486        if (Buf.au16[0] == IMAGE_DOS_SIGNATURE)
    95             rc = VERR_NOT_SUPPORTED;
     87            rc = KDBG_ERR_FORMAT_NOT_SUPPORTED;
    9688#ifdef IMAGE_NE_SIGNATURE
    9789        else if (Buf.au16[0] == IMAGE_NE_SIGNATURE)
    98             rc = VERR_NOT_SUPPORTED;
     90            rc = KDBG_ERR_FORMAT_NOT_SUPPORTED;
    9991#endif
    10092#ifdef IMAGE_LX_SIGNATURE
    10193        else if (Buf.au16[0] == IMAGE_LX_SIGNATURE)
    102             rc = VERR_NOT_SUPPORTED;
     94            rc = KDBG_ERR_FORMAT_NOT_SUPPORTED;
    10395#endif
    10496#ifdef IMAGE_LE_SIGNATURE
    10597        else if (Buf.au16[0] == IMAGE_LE_SIGNATURE)
    106             rc = VERR_NOT_SUPPORTED;
     98            rc = KDBG_ERR_FORMAT_NOT_SUPPORTED;
    10799#endif
    108100#ifdef IMAGE_ELF_SIGNATURE
    109101        else if (Buf.au32[0] == IMAGE_ELF_SIGNATURE)
    110             rc = VERR_NOT_SUPPORTED;
     102            rc = KDBG_ERR_FORMAT_NOT_SUPPORTED;
    111103#endif
    112104#ifdef IMAGE_NT_SIGNATURE
    113105        else if (Buf.au32[0] == IMAGE_NT_SIGNATURE)
    114             rc = rtDbgModPEOpen(File, offHdr, pszModulePath, ppDbgMod);
     106            rc = kdbgModPEOpen(pFile, offHdr, pszModulePath, ppDbgMod);
    115107#endif
    116108        /** @todo there are a number of text file formats too which I want to support. */
    117109        else
    118             rc = VERR_NOT_SUPPORTED;
    119 
    120     }
    121 
    122     if (RT_FAILURE(rc))
    123         RTFileClose(File);
     110            rc = KDBG_ERR_UNKOWN_FORMAT;
     111    }
     112
     113    if (rc)
     114        kDbgHlpClose(pFile);
    124115    return rc;
    125116}
     
    133124 * @param   pMod        The debug module handle.
    134125 */
    135 DECLINLINE(bool) rtDbgModIsValid(PRTDBGMOD pMod)
    136 {
    137     AssertMsgReturn(VALID_PTR(pMod), ("%p", pMod), false);
    138     AssertMsgReturn(pMod->u32Magic == RTDBGMOD_MAGIC, ("%RX32", pMod->u32Magic), false);
    139     AssertMsgReturn(VALID_PTR(pMod->pOps), ("%p", pMod->pOps), false);
     126KDBG_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);
    140131    return true;
    141132}
     
    148139 * @param   pMod        The module handle.
    149140 */
    150 RTDECL(int) RTDbgModuleClose(PRTDBGMOD pMod)
    151 {
    152     if (!rtDbgModIsValid(pMod))
    153         return VERR_INVALID_PARAMETER;
     141KDBG_DECL(int) kDbgModuleClose(PKDBGMOD pMod)
     142{
     143    if (!kdbgModIsValid(pMod))
     144        return KDBG_ERR_INVALID_PARAMETER;
    154145
    155146    int rc = pMod->pOps->pfnClose(pMod);
    156     if (RT_SUCCESS(rc))
    157         RTMemFree(pMod);
     147    if (!rc)
     148        kDbgHlpFree(pMod);
    158149    return rc;
    159150}
     
    173164 * @param   pSym        Where to store the symbol details.
    174165 */
    175 RTDECL(int) RTDbgModuleQuerySymbol(PRTDBGMOD pMod, int32_t iSegment, RTUINTPTR off, PRTDBGSYMBOL pSym)
    176 {
    177     if (!rtDbgModIsValid(pMod))
    178         return VERR_INVALID_PARAMETER;
    179     AssertMsgReturn(VALID_PTR(pSym), ("%p", pSym), VERR_INVALID_POINTER);
     166KDBG_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);
    180171
    181172    return pMod->pOps->pfnQuerySymbol(pMod, iSegment, off, pSym);
     
    195186 * @param   off         The offset into the segment.
    196187 * @param   ppSym       Where to store the pointer to the symbol info.
    197  *                      Free the returned symbol using RTDbgSymbolFree().
    198  */
    199 RTDECL(int) RTDbgModuleQuerySymbolA(PRTDBGMOD pMod, int32_t iSegment, RTUINTPTR off, PPRTDBGSYMBOL ppSym)
    200 {
    201     AssertMsgReturn(VALID_PTR(ppSym), ("%p\n", ppSym), VERR_INVALID_POINTER);
    202 
    203     RTDBGSYMBOL Sym;
    204     int rc = RTDbgModuleQuerySymbol(pMod, iSegment, off, &Sym);
    205     if (RT_SUCCESS(rc))
    206     {
    207         *ppSym = RTDbgSymbolDup(&Sym);
     188 *                      Free the returned symbol using kDbgSymbolFree().
     189 */
     190KDBG_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);
    208199        if (!*ppSym)
    209             rc = VERR_NO_MEMORY;
     200            rc = KDBG_ERR_NO_MEMORY;
    210201    }
    211202    else
     
    228219 * @param   pLine       Where to store the line number details.
    229220 */
    230 RTDECL(int) RTDbgModuleQueryLine(PRTDBGMOD pMod, int32_t iSegment, RTUINTPTR off, PRTDBGLINE pLine)
    231 {
    232     if (!rtDbgModIsValid(pMod))
    233         return VERR_INVALID_PARAMETER;
    234     AssertMsgReturn(VALID_PTR(pLine), ("%p", pLine), VERR_INVALID_POINTER);
     221KDBG_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);
    235226
    236227    return pMod->pOps->pfnQueryLine(pMod, iSegment, off, pLine);
     
    250241 * @param   off         The offset into the segment.
    251242 * @param   ppLine      Where to store the pointer to the line number info.
    252  *                      Free the returned line number using RTDbgLineFree().
    253  */
    254 RTDECL(int) RTDbgModuleQueryLineA(PRTDBGMOD pMod, int32_t iSegment, RTUINTPTR off, PPRTDBGLINE ppLine)
    255 {
    256     AssertMsgReturn(VALID_PTR(ppLine), ("%p\n", ppLine), VERR_INVALID_POINTER);
    257 
    258     RTDBGLINE Line;
    259     int rc = RTDbgModuleQueryLine(pMod, iSegment, off, &Line);
    260     if (RT_SUCCESS(rc))
    261     {
    262         *ppLine = RTDbgLineDup(&Line);
     243 *                      Free the returned line number using kDbgLineFree().
     244 */
     245KDBG_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);
    263254        if (!*ppLine)
    264             rc = VERR_NO_MEMORY;
     255            rc = KDBG_ERR_NO_MEMORY;
    265256    }
    266257    else
Note: See TracChangeset for help on using the changeset viewer.