Ignore:
Timestamp:
Aug 26, 2007, 3:13:35 AM (18 years ago)
Author:
bird
Message:

made kDbg compile again (not linking yet though).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kStuff/kDbg/kDbgModule.cpp

    r3543 r3550  
    99 * This file is part of kStuff.
    1010 *
    11  * kStuff is free software; you can redistribute it and/or modify
    12  * it under the terms of the GNU Lesser General Public License as published
    13  * by the Free Software Foundation; either version 2 of the License, or
    14  * (at your option) any later version.
     11 * kStuff is free software; you can redistribute it and/or
     12 * modify it under the terms of the GNU Lesser General Public
     13 * License as published by the Free Software Foundation; either
     14 * version 2.1 of the License, or (at your option) any later version.
    1515 *
    1616 * kStuff is distributed in the hope that it will be useful,
    1717 * but WITHOUT ANY WARRANTY; without even the implied warranty of
    18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    19  * GNU Lesser General Public License for more details.
    20  *
    21  * You should have received a copy of the GNU Lesser General Public License
    22  * along with kStuff; if not, write to the Free Software
    23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    24  *
    25  */
    26 
    27 
     18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     19 * Lesser General Public License for more details.
     20 *
     21 * You should have received a copy of the GNU Lesser General Public
     22 * License along with kStuff; if not, write to the Free Software
     23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
     24 *
     25 */
    2826
    2927/*******************************************************************************
    3028*   Header Files                                                               *
    3129*******************************************************************************/
    32 #include <k/kDbgAll.h>
    33 #include <k/kErrors.h>
    34 #include <string.h>
    3530#include "kDbgInternal.h"
     31#include <k/kHlpString.h>
     32#include <k/kHlpAlloc.h>
    3633
    3734
     
    4744    &g_kDbgModWinDbgHelpOpen,
    4845#endif
    49     &g_kDbgModLdr,
    50     &g_kDbgModCv8,
    51     &g_kDbgModDwarf,
    52     &g_kDbgModHll,
    53     &g_kDbgModStabs,
    54     &g_kDbgModSym,
    55     &g_kDbgModMapILink,
    56     &g_kDbgModMapMSLink,
    57     &g_kDbgModMapNm,
    58     &g_kDbgModMapWLink
     46//    &g_kDbgModLdr,
     47//    &g_kDbgModCv8,
     48//    &g_kDbgModDwarf,
     49//    &g_kDbgModHll,
     50//    &g_kDbgModStabs,
     51//    &g_kDbgModSym,
     52//    &g_kDbgModMapILink,
     53//    &g_kDbgModMapMSLink,
     54//    &g_kDbgModMapNm,
     55//    &g_kDbgModMapWLink
    5956};
    6057
     
    9390    kDbgAssertPtrReturn(pOps->pfnQueryLine, KERR_INVALID_POINTER);
    9491    kDbgAssertPtrReturn(pOps->pszName2, KERR_INVALID_POINTER);
    95     if (strcmp(pOps->pszName, pOps->pszName2))
     92    if (kHlpStrComp(pOps->pszName, pOps->pszName2))
    9693        return KERR_INVALID_PARAMETER;
    9794    kDbgAssertReturn(pOps->pNext == NULL, KERR_INVALID_PARAMETER);
     
    153150
    154151
     152
     153/**
     154 * Worker for the kDbgModuleOpen* APIs.
     155 *
     156 * This will make sure the reader is buffered. I will also take care of
     157 * closing the reader opened by kDbgModuleOpen on failure.
     158 *
     159 * @returns 0 on success. An appropriate kErrors status code on failure.
     160 * @param   ppDbgMod        Where to store the new debug module reader instance.
     161 * @param   pRdr            The file provider.
     162 * @param   fCloseRdr       Whether pRdr should be close or not. This applies both
     163 *                          to the failure path and to the success path, where it'll
     164 *                          be close when the module is closed by kDbgModuleClose().
     165 * @param   off             The offset into the file where the debug info is supposed
     166 *                          to be found.
     167 *                          This is 0 if the entire file is the subject.
     168 * @param   cb              The size of the debug info part of the file.
     169 *                          This is KFOFF_MAX if the entire file is the subject.
     170 * @param   pLdrMod         An optional kLdrMod association.
     171 */
     172static int kdbgModuleOpenWorker(PPKDBGMOD ppDbgMod, PKRDR pRdr, KBOOL fCloseRdr, KFOFF off, KFOFF cb, struct KLDRMOD *pLdrMod)
     173{
     174    /*
     175     * If the reader isn't buffered create a buffered wrapper for it.
     176     */
     177    int rc;
     178    PKRDR pRdrWrapped = NULL;
     179    if (!kRdrBufIsBuffered(pRdr))
     180    {
     181        rc = kRdrBufWrap(&pRdrWrapped, pRdr, fCloseRdr);
     182        if (rc)
     183        {
     184            if (fCloseRdr)
     185                kRdrClose(pRdr);
     186            return rc;
     187        }
     188        pRdr = pRdrWrapped;
     189    }
     190
     191    /*
     192     * Walk the built-in table and the list of registered readers
     193     * and let each of them have a go at the file. Stop and return
     194     * on the first one returning successfully.
     195     */
     196    rc = KDBG_ERR_UNKOWN_FORMAT;
     197    for (KSIZE i = 0; i < K_ELEMENTS(g_aBuiltIns); i++)
     198        if (g_aBuiltIns[i]->pfnOpen)
     199        {
     200            int rc2 = g_aBuiltIns[i]->pfnOpen(ppDbgMod, pRdr, fCloseRdr, off, cb, pLdrMod);
     201            if (!rc)
     202                return 0;
     203            if (rc2 != KDBG_ERR_UNKOWN_FORMAT && rc == KDBG_ERR_UNKOWN_FORMAT)
     204                rc = rc2;
     205        }
     206
     207    for (PKDBGMODOPS pCur = g_pHead; pCur; pCur = pCur->pNext)
     208        if (g_aBuiltIns[i]->pfnOpen)
     209        {
     210            int rc2 = g_aBuiltIns[i]->pfnOpen(ppDbgMod, pRdr, fCloseRdr, off, cb, pLdrMod);
     211            if (!rc)
     212                return 0;
     213            if (rc2 != KDBG_ERR_UNKOWN_FORMAT && rc == KDBG_ERR_UNKOWN_FORMAT)
     214                rc = rc2;
     215        }
     216
     217    if (pRdrWrapped)
     218        kRdrClose(pRdrWrapped);
     219    else if (fCloseRdr)
     220        kRdrClose(pRdr);
     221    return rc;
     222}
     223
     224
    155225/**
    156226 * Opens a debug module reader for the specified file or file section
     
    158228 * @returns kStuff status code.
    159229 * @param   ppDbgMod            Where to store the debug module reader handle.
    160  * @param   pFile               The file reader.
     230 * @param   pRdr                The file reader.
    161231 * @param   off                 The offset of the file section. If the entire file, pass 0.
    162232 * @param   cb                  The size of the file section. If the entire file, pass KFOFF_MAX.
     
    167237 *                              This is an optional parameter, pass NULL if no kLdr module at hand.
    168238 */
    169 KDBG_DECL(int) kDbgModuleOpenFilePart(PKDBGMOD *ppDbgMod, PKDBGHLPFILE pFile, KFOFF off, KFOFF cb, struct KLDRMOD *pLdrMod)
     239KDBG_DECL(int) kDbgModuleOpenFilePart(PPKDBGMOD ppDbgMod, PKRDR pRdr, KFOFF off, KFOFF cb, struct KLDRMOD *pLdrMod)
    170240{
    171241    /*
     
    173243     */
    174244    kDbgAssertPtrReturn(ppDbgMod, KERR_INVALID_POINTER);
    175     kDbgAssertPtrReturn(pFile, KERR_INVALID_POINTER);
     245    kDbgAssertPtrReturn(pRdr, KERR_INVALID_POINTER);
    176246    kDbgAssertPtrNullReturn(pLdrMod, KERR_INVALID_POINTER);
    177247    kDbgAssertMsgReturn(off >= 0 && off < KFOFF_MAX, (KFOFF_PRI "\n", off), KERR_INVALID_OFFSET);
     
    181251
    182252    /*
    183      * Walk the built-in table and the list of registered readers
    184      * and let each of them have a go at the file. Stop and return
    185      * on the first one returning successfully.
    186      */
    187     int rc = KDBG_ERR_UNKOWN_FORMAT;
    188     for (KSIZE i = 0; i < K_ELEMENTS(g_aBuiltIns); i++)
    189         if (g_aBuiltIns[i]->pfnOpen)
    190         {
    191             int rc2 = g_aBuiltIns[i]->pfnOpen(ppDbgMod, pFile, off, cb, pLdrMod);
    192             if (!rc)
    193                 return 0;
    194             if (rc2 != KDBG_ERR_UNKOWN_FORMAT && rc == KDBG_ERR_UNKOWN_FORMAT)
    195                 rc = rc2;
    196         }
    197 
    198     for (PKDBGMODOPS pCur = g_pHead; pCur; pCur = pCur->pNext)
    199         if (g_aBuiltIns[i]->pfnOpen)
    200         {
    201             int rc2 = g_aBuiltIns[i]->pfnOpen(ppDbgMod, pFile, off, cb, pLdrMod);
    202             if (!rc)
    203                 return 0;
    204             if (rc2 != KDBG_ERR_UNKOWN_FORMAT && rc == KDBG_ERR_UNKOWN_FORMAT)
    205                 rc = rc2;
    206         }
    207     return rc;
     253     * Hand it over to the internal worker.
     254     */
     255    return kdbgModuleOpenWorker(ppDbgMod, pRdr, K_FALSE /* fCloseRdr */, off, cb, pLdrMod);
    208256}
    209257
     
    214262 * @returns kStuff status code.
    215263 * @param   ppDbgMod            Where to store the debug module reader handle.
    216  * @param   pFile               The file reader.
     264 * @param   pRdr                The file reader.
    217265 * @param   pLdrMod             Associated kLdr module that the kDbg component can use to
    218266 *                              verify and suplement the debug info found in the file specified
     
    221269 *                              This is an optional parameter, pass NULL if no kLdr module at hand.
    222270 */
    223 KDBG_DECL(int) kDbgModuleOpenFile(PKDBGMOD *ppDbgMod, PKDBGHLPFILE pFile, struct KLDRMOD *pLdrMod)
    224 {
    225     return kDbgModuleOpenFilePart(ppDbgMod, pFile, 0, KFOFF_MAX, pLdrMod);
     271KDBG_DECL(int) kDbgModuleOpenFile(PPKDBGMOD ppDbgMod, PKRDR pRdr, struct KLDRMOD *pLdrMod)
     272{
     273    return kDbgModuleOpenFilePart(ppDbgMod, pRdr, 0, KFOFF_MAX, pLdrMod);
    226274}
    227275
     
    240288 *                              This is an optional parameter, pass NULL if no kLdr module at hand.
    241289 */
    242 KDBG_DECL(int) kDbgModuleOpen(PKDBGMOD *ppDbgMod, const char *pszFilename, struct KLDRMOD *pLdrMod)
     290KDBG_DECL(int) kDbgModuleOpen(PPKDBGMOD ppDbgMod, const char *pszFilename, struct KLDRMOD *pLdrMod)
    243291{
    244292    /*
     
    254302     * Open the file and see if we can read it.
    255303     */
    256     PKDBGHLPFILE pFile;
    257     int rc = kDbgHlpOpenRO(pszFilename, &pFile);
     304    PKRDR pRdr;
     305    int rc = kRdrBufOpen(&pRdr, pszFilename);
    258306    if (rc)
    259307        return rc;
    260     rc = kDbgModuleOpenFilePart(ppDbgMod, pFile, 0, KFOFF_MAX, pLdrMod);
    261     if (rc)
    262         kDbgHlpClose(pFile);
     308    rc = kdbgModuleOpenWorker(ppDbgMod, pRdr, K_TRUE /* fCloseRdr */, 0, KFOFF_MAX, pLdrMod);
    263309    return rc;
    264 }
    265 
    266 
    267 /**
    268  * Validates a debug module handle.
    269  * All necessary asserting will be taken care of here.
    270  *
    271  * @returns True / false.
    272  * @param   pMod        The debug module handle.
    273  */
    274 KDBG_INLINE(bool) kdbgModIsValid(PKDBGMOD pMod)
    275 {
    276     kDbgAssertPtrReturn(pMod, false);
    277     kDbgAssertMsgReturn(pMod->u32Magic == KDBGMOD_MAGIC, ("%#x", pMod->u32Magic), false);
    278     kDbgAssertPtrReturn(pMod->pOps, false);
    279     return true;
    280310}
    281311
     
    289319KDBG_DECL(int) kDbgModuleClose(PKDBGMOD pMod)
    290320{
    291     if (!kdbgModIsValid(pMod))
    292         return KERR_INVALID_PARAMETER;
    293 
     321    KDBGMOD_VALIDATE(pMod);
    294322    int rc = pMod->pOps->pfnClose(pMod);
    295323    if (!rc)
    296         kDbgHlpFree(pMod);
     324    {
     325        pMod->u32Magic++;
     326        kHlpFree(pMod);
     327    }
    297328    return rc;
    298329}
     
    314345KDBG_DECL(int) kDbgModuleQuerySymbol(PKDBGMOD pMod, int32_t iSegment, KDBGADDR off, PKDBGSYMBOL pSym)
    315346{
    316     if (!kdbgModIsValid(pMod))
    317         return KERR_INVALID_PARAMETER;
     347    KDBGMOD_VALIDATE(pMod);
    318348    kDbgAssertPtrReturn(pSym, KERR_INVALID_POINTER);
    319 
    320349    return pMod->pOps->pfnQuerySymbol(pMod, iSegment, off, pSym);
    321350}
     
    369398KDBG_DECL(int) kDbgModuleQueryLine(PKDBGMOD pMod, int32_t iSegment, KDBGADDR off, PKDBGLINE pLine)
    370399{
    371     if (!kdbgModIsValid(pMod))
    372         return KERR_INVALID_PARAMETER;
     400    KDBGMOD_VALIDATE(pMod);
    373401    kDbgAssertPtrReturn(pLine, KERR_INVALID_POINTER);
    374 
    375402    return pMod->pOps->pfnQueryLine(pMod, iSegment, off, pLine);
    376403}
Note: See TracChangeset for help on using the changeset viewer.