Changeset 3550


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

made kDbg compile again (not linking yet though).

Location:
trunk/kStuff
Files:
2 added
19 edited

Legend:

Unmodified
Added
Removed
  • trunk/kStuff/include/k/kDbg.h

    r3541 r3550  
    2323 */
    2424
    25 #ifndef ___kDbg_h___
    26 #define ___kDbg_h___
    27 
    28 #include "kDbgBase.h"
     25#ifndef ___k_kDbg_h___
     26#define ___k_kDbg_h___
     27
     28#include <k/kDefs.h>
     29#include <k/kTypes.h>
     30#include <k/kRdr.h>
    2931
    3032#ifdef __cplusplus
     
    3638 */
    3739
    38 /** The max filename path length used by the debug reader. */
    39 #define KDBG_PATH_MAX       260
    40 
    41 /** The max symbol name length used by the debug reader. */
    42 #define KDBG_SYMBOL_MAX     384
     40/** @def KDBG_DECL
     41 * Declares a kDbg function according to build context.
     42 * @param type          The return type.
     43 */
     44#if defined(KDBG_BUILDING_DYNAMIC)
     45# define KDBG_DECL(type)        K_DECL_EXPORT(type)
     46#elif defined(KDBG_BUILT_DYNAMIC)
     47# define KDBG_DECL(type)        K_DECL_IMPORT(type)
     48#else
     49# define KDBG_DECL(type)        type
     50#endif
     51
     52
     53/** The kDbg address type. */
     54typedef KU64                    KDBGADDR;
     55/** Pointer to a kDbg address. */
     56typedef KDBGADDR               *PKDBGADDR;
     57/** Pointer to a const kDbg address. */
     58typedef const KDBGADDR         *PCKDBGADDR;
     59/** @def KDBGADDR_PRI
     60 * printf format type. */
     61#define KDBGADDR_PRI            KX64_PRI
     62/** @def KDBGADDR_MAX
     63 * Max kDbg address value. */
     64#define KDBGADDR_MAX            KU64_C(0xfffffffffffffffe)
     65/** @def KDBGADDR_C
     66 * kDbg address constant.
     67 * @param c         The constant value. */
     68#define KDBGADDR_C(c)           KU64_C(c)
     69/** NIL address. */
     70#define NIL_KDBGADDR            KU64_MAX
     71
    4372
    4473/** @name   Special Segments
     
    4776 * The specified offset is relative to the image base. The image base is the lowest memory
    4877 * address used by the image when loaded with the address assignments indicated in the image. */
    49 #define KDBGSEG_RVA (-1)
     78#define KDBGSEG_RVA             (-1)
    5079/** Absolute segment. The offset isn't relative to anything. */
    51 #define KDBGSEG_ABS (-2)
     80#define KDBGSEG_ABS             (-2)
    5281/** @} */
    5382
     83
     84/** The max filename path length used by the debug reader. */
     85#define KDBG_PATH_MAX           260
    5486
    5587/**
     
    6395    KDBGADDR    offSegment;
    6496    /** The segment number. */
    65     int32_t     iSegment;
     97    KI32        iSegment;
    6698    /** The Line number. */
    67     uint32_t    iLine;
     99    KU32        iLine;
     100    /** The actual size of this structure. */
     101    KU16        cbSelf;
    68102    /** The length of the filename. */
    69     uint16_t    cchFile;
     103    KU16        cchFile;
    70104    /** The name of the file this line number relates to. */
    71105    char        szFile[KDBG_PATH_MAX];
     
    105139 * @{ */
    106140/** The symbol is weak. */
    107 #define KDBGSYM_FLAGS_WEAK      UINT32_C(0x00000000)
     141#define KDBGSYM_FLAGS_WEAK      KU32_C(0x00000000)
    108142/** The symbol is absolute.
    109143 * (This also indicated by the segment number.) */
    110 #define KDBGSYM_FLAGS_ABS       UINT32_C(0x00000001)
     144#define KDBGSYM_FLAGS_ABS       KU32_C(0x00000001)
    111145/** The symbol is exported. */
    112 #define KDBGSYM_FLAGS_EXPORTED  UINT32_C(0x00000002)
     146#define KDBGSYM_FLAGS_EXPORTED  KU32_C(0x00000002)
    113147/** The symbol is a function/method/procedure/whatever-executable-code. */
    114 #define KDBGSYM_FLAGS_CODE      UINT32_C(0x00000004)
     148#define KDBGSYM_FLAGS_CODE      KU32_C(0x00000004)
    115149/** The symbol is some kind of data. */
    116 #define KDBGSYM_FLAGS_DATA      UINT32_C(0x00000008)
     150#define KDBGSYM_FLAGS_DATA      KU32_C(0x00000008)
    117151/** @} */
     152
     153/** The max symbol name length used by the debug reader. */
     154#define KDBG_SYMBOL_MAX         384
    118155
    119156/**
     
    130167    KDBGADDR    offSegment;
    131168    /** The segment number. */
    132     int32_t     iSegment;
     169    KI32        iSegment;
    133170    /** The symbol flags. */
    134     uint32_t    fFlags;
     171    KU32        fFlags;
    135172/** @todo type info? */
     173    /** The actual size of this structure. */
     174    KU16        cbSelf;
    136175    /** The length of the symbol name. */
    137     uint16_t    cchName;
     176    KU16        cchName;
    138177    /** The symbol name. */
    139178    char        szName[KDBG_SYMBOL_MAX];
     
    170209
    171210
    172 /** Pointer to a kDbgHlp file structure (abstract). */
    173 typedef struct KDBGHLPFILE *PKDBGHLPFILE;
    174 
    175 /** A debug module handle. */
     211/** Pointer to a debug module. */
    176212typedef struct KDBGMOD *PKDBGMOD;
    177 
    178 /**
    179  * The debug module method table.
    180  */
    181 typedef struct KDBGMODOPS
    182 {
    183     /** The name of the reader. */
    184     const char *pszName;
    185 
    186     /** Pointer to the next debug module readers.
    187      * This is only used for dynamically registered readers. */
    188     struct KDBGMODOPS  *pNext;
    189 
    190     /**
    191      * Tries to open the module.
    192      *
    193      * @returns 0 on success, KDBG_ERR on failure.
    194      * @param   pFile           The file
    195      * @param   ppMod           Where to store the module that's been opened.
    196      * @param   off             The file offset of the debug info. This is 0 if there isn't
    197      *                          any specfic debug info section and the reader should start
    198      *                          looking for debug info at the start of the file.
    199      * @param   cb              The size of the debug info in the file. INT64_MAX if we don't
    200      *                          know or there isn't any particular debug info section in the file.
    201      * @param   pLdrMod         The associated loader module. This can be NULL.
    202      *
    203      * @remark  This is NULL for the builtin readers.
    204      */
    205     int (*pfnOpen)(PKDBGMOD *ppMod, PKDBGHLPFILE pFile, KFOFF off, KFOFF cb, struct KLDRMOD *pLdrMod);
    206 
    207     /**
    208      * Closes the module.
    209      *
    210      * This should free all resources associated with the module
    211      * except the pMod which is freed by the caller.
    212      *
    213      * @returns IPRT status code.
    214      * @param   pMod        The module.
    215      */
    216     int (*pfnClose)(PKDBGMOD pMod);
    217 
    218     /**
    219      * Gets a symbol by segment:offset.
    220      * This will be approximated to the nearest symbol if there is no exact match.
    221      *
    222      * @returns 0 on success. KLDR_ERR_* on failure.
    223      * @param   pMod        The module.
    224      * @param   iSegment    The segment this offset is relative to.
    225      *                      The -1 segment is special, it means that the addres is relative to
    226      *                      the image base. The image base is where the first bit of the image
    227      *                      is mapped during load.
    228      * @param   off         The offset into the segment.
    229      * @param   pSym        Where to store the symbol details.
    230      */
    231     int (*pfnQuerySymbol)(PKDBGMOD pMod, KI32 iSegment, KDBGADDR off, PKDBGSYMBOL pSym);
    232 
    233     /**
    234      * Gets a line number entry by segment:offset.
    235      * This will be approximated to the nearest line number there is no exact match.
    236      *
    237      * @returns 0 on success. KLDR_ERR_* on failure.
    238      * @param   pMod        The module.
    239      * @param   iSegment    The segment this offset is relative to.
    240      *                      The -1 segment is special, it means that the addres is relative to
    241      *                      the image base. The image base is where the first bit of the image
    242      *                      is mapped during load.
    243      * @param   off         The offset into the segment.
    244      * @param   pLine       Where to store the line number details.
    245      */
    246     int (*pfnQueryLine)(PKDBGMOD pMod, KI32 iSegment, KDBGADDR uOffset, PKDBGLINE pLine);
    247 
    248     /** This is just to make sure you've initialized all the fields.
    249      * Must be identical to pszName. */
    250     const char *pszName2;
    251 } KDBGMODOPS;
    252 /** Pointer to a module method table. */
    253 typedef KDBGMODOPS *PKDBGMODOPS;
    254 /** Pointer to a const module method table. */
    255 typedef const KDBGMODOPS *PCKDBGMODOPS;
    256 
    257 /**
    258  * Register a debug module reader with the kDbgModule component.
    259  *
    260  * Dynamically registered readers are kept in FIFO order, and external
    261  * readers will be tried after the builtin ones.
    262  *
    263  * @returns 0 on success.
    264  * @returns KERR_INVALID_POINTER if pOps is missing bits.
    265  * @returns KERR_INVALID_PARAMETER if pOps is already in the list.
    266  * @param   pOps        The reader method table, kDbg takes owner ship of
    267  *                      this. This must be writeable as the pNext pointer
    268  *                      will be update. It must also stick around for as
    269  *                      long as kDbg is in use.
    270  */
    271 KDBG_DECL(int) kDbgModuleRegisterReader(PKDBGMODOPS pOps)
    272 ;
    273 
    274 
    275 
    276 /**
    277  * Internal representation of a debug module.
    278  */
    279 typedef struct KDBGMOD
    280 {
    281     /** Magic value (KDBGMOD_MAGIC). */
    282     KI32            u32Magic;
    283     /** The handle to the module. (If closed, this is NIL_RTFILE.) */
    284     PKDBGHLPFILE    pFile;
    285     /** Pointer to the method table. */
    286     PCKDBGMODOPS    pOps;
    287 } KDBGMOD;
    288 
    289 
    290 /** The magic value for the debug module structure. (Some dead english writer) */
    291 #define KDBGMOD_MAGIC      0x19200501
    292 /** The magic value of a dead module structure. */
    293 #define KDBGMOD_MAGIC_DEAD 0x19991231
    294 
    295 
    296 /**
    297  * Opens the debug info for a specified executable module.
    298  *
    299  * @returns IPRT status code.
    300  * @param   pszModulePath       The path to the executable module.
    301  * @param   ppDbgMod            Where to store the debug module handle.
    302  */
    303 KDBG_DECL(int) kDbgModuleOpen(const char *pszModulePath, PKDBGMOD *ppDbgMod);
    304 
    305 /**
    306  * Closes the module.
    307  *
    308  * @returns IPRT status code.
    309  * @param   pMod        The module handle.
    310  */
     213/** Pointer to a debug module pointer. */
     214typedef PKDBGMOD *PPKDBGMOD;
     215
     216
     217KDBG_DECL(int) kDbgModuleOpen(PPKDBGMOD ppDbgMod, const char *pszFilename, struct KLDRMOD *pLdrMod);
     218KDBG_DECL(int) kDbgModuleOpenFile(PPKDBGMOD ppDbgMod, PKRDR pRdr, struct KLDRMOD *pLdrMod);
     219KDBG_DECL(int) kDbgModuleOpenFilePart(PPKDBGMOD ppDbgMod, PKRDR pRdr, KFOFF off, KFOFF cb, struct KLDRMOD *pLdrMod);
    311220KDBG_DECL(int) kDbgModuleClose(PKDBGMOD pMod);
    312 
    313 /**
    314  * Gets a symbol by segment:offset.
    315  * This will be approximated to the nearest symbol if there is no exact match.
    316  *
    317  * @returns IPRT status code.
    318  * @param   pMod        The module.
    319  * @param   iSegment    The segment this offset is relative to.
    320  *                      The -1 segment is special, it means that the addres is relative to
    321  *                      the image base. The image base is where the first bit of the image
    322  *                      is mapped during load.
    323  * @param   off         The offset into the segment.
    324  * @param   pSym        Where to store the symbol details.
    325  */
    326221KDBG_DECL(int) kDbgModuleQuerySymbol(PKDBGMOD pMod, KI32 iSegment, KDBGADDR off, PKDBGSYMBOL pSym);
    327 
    328 /**
    329  * Gets & allocates a symbol by segment:offset.
    330  * This will be approximated to the nearest symbol if there is no exact match.
    331  *
    332  * @returns IPRT status code.
    333  * @param   pMod        The module.
    334  * @param   iSegment    The segment this offset is relative to.
    335  *                      The -1 segment is special, it means that the addres is relative to
    336  *                      the image base. The image base is where the first bit of the image
    337  *                      is mapped during load.
    338  * @param   off         The offset into the segment.
    339  * @param   ppSym       Where to store the pointer to the symbol info.
    340  *                      Free the returned symbol using kDbgSymbolFree().
    341  */
    342222KDBG_DECL(int) kDbgModuleQuerySymbolA(PKDBGMOD pMod, KI32 iSegment, KDBGADDR off, PPKDBGSYMBOL ppSym);
    343 
    344 /**
    345  * Gets a line number entry by segment:offset.
    346  * This will be approximated to the nearest line number there is no exact match.
    347  *
    348  * @returns IPRT status code.
    349  * @param   pMod        The module.
    350  * @param   iSegment    The segment this offset is relative to.
    351  *                      The -1 segment is special, it means that the addres is relative to
    352  *                      the image base. The image base is where the first bit of the image
    353  *                      is mapped during load.
    354  * @param   off         The offset into the segment.
    355  * @param   pLine       Where to store the line number details.
    356  */
    357223KDBG_DECL(int) kDbgModuleQueryLine(PKDBGMOD pMod, KI32 iSegment, KDBGADDR off, PKDBGLINE pLine);
    358 
    359 /**
    360  * Gets & allocates a line number entry by segment:offset.
    361  * This will be approximated to the nearest line number there is no exact match.
    362  *
    363  * @returns IPRT status code.
    364  * @param   pMod        The module.
    365  * @param   iSegment    The segment this offset is relative to.
    366  *                      The -1 segment is special, it means that the addres is relative to
    367  *                      the image base. The image base is where the first bit of the image
    368  *                      is mapped during load.
    369  * @param   off         The offset into the segment.
    370  * @param   ppLine      Where to store the pointer to the line number info.
    371  *                      Free the returned line number using kDbgLineFree().
    372  */
    373224KDBG_DECL(int) kDbgModuleQueryLineA(PKDBGMOD pMod, KI32 iSegment, KDBGADDR off, PPKDBGLINE ppLine);
    374225
  • trunk/kStuff/include/k/kDefs.h

    r3545 r3550  
    450450/** @} */
    451451
    452 /** @} */
    453 
    454 #endif
    455 
     452/** @def NULL
     453 * The nil pointer value. */
     454#ifndef NULL
     455# ifdef __cplusplus
     456#  define NULL          0
     457# else
     458#  define NULL          ((void *)0)
     459# endif
     460#endif
     461
     462/** @} */
     463
     464#endif
     465
  • trunk/kStuff/include/k/kErrors.h

    r3548 r3550  
    8787/** The (module) format isn't supported by this kDbg build. */
    8888#define KDBG_ERR_FORMAT_NOT_SUPPORTED                   (KDBG_ERR_BASE + 1)
     89/** The (module) format isn't supported by this kDbg build. */
     90#define KDBG_ERR_BAD_EXE_FORMAT                         (KDBG_ERR_BASE + 2)
    8991/** A specified address or an address found in the debug info is invalid. */
    90 #define KDBG_ERR_INVALID_ADDRESS                        (KDBG_ERR_BASE + 2)
     92#define KDBG_ERR_INVALID_ADDRESS                        (KDBG_ERR_BASE + 3)
    9193/** The dbghelp.dll is too old or something like that. */
    92 #define KDBG_ERR_DBGHLP_VERSION_MISMATCH                (KDBG_ERR_BASE + 3)
     94#define KDBG_ERR_DBGHLP_VERSION_MISMATCH                (KDBG_ERR_BASE + 4)
    9395/** @} */
    9496
     
    9698 * @{ */
    9799/** the base of the kRdr specific status codes. */
    98 #define KRDR_ERR_BASE                                   (KDBG_ERR_BASE + 4)
     100#define KRDR_ERR_BASE                                   (KDBG_ERR_BASE + 5)
    99101/** The file reader can't take more concurrent mappings. */
    100102#define KRDR_ERR_TOO_MANY_MAPPINGS                      (KRDR_ERR_BASE + 0)
  • trunk/kStuff/include/k/kHlpAlloc.h

    r3545 r3550  
    5252void *  kHlpAlloc(KSIZE cb);
    5353void *  kHlpAllocZ(KSIZE cb);
    54 void *  kHlpDup(void *pv, KSIZE cb);
    55 void *  kHlpStrDup(const char *psz);
     54void *  kHlpDup(const void *pv, KSIZE cb);
     55const char *kHlpStrDup(const char *psz);
    5656void *  kHlpRealloc(void *pv, KSIZE cb);
    5757void    kHlpFree(void *pv);
  • trunk/kStuff/include/k/kHlpAssert.h

    r3543 r3550  
    7373    } while (0)
    7474
     75# define kHlpAssertReturnVoid(expr) \
     76    do { \
     77        if (!(expr)) \
     78        { \
     79            kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
     80            kHlpAssertBreakpoint(); \
     81            return; \
     82        }
     83    } while (0)
     84
    7585# define kHlpAssertMsg(expr, msg) \
    7686    do { \
     
    94104    } while (0)
    95105
     106# define kHlpAssertMsgReturnVoid(expr, msg) \
     107    do { \
     108        if (!(expr)) \
     109        { \
     110            kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
     111            kHlpAssertMsg2 msg; \
     112            kHlpAssertBreakpoint(); \
     113            return; \
     114        }
     115    } while (0)
     116
    96117#else   /* !K_STRICT */
    97118# define kHlpAssert(expr)                       do { } while (0)
    98119# define kHlpAssertReturn(expr, rcRet)          do { if (!(expr)) return (rcRet); } while (0)
     120# define kHlpAssertReturnVoid(expr)             do { if (!(expr)) return; } while (0)
    99121# define kHlpAssertMsg(expr, msg)               do { } while (0)
    100122# define kHlpAssertMsgReturn(expr, msg, rcRet)  do { if (!(expr)) return (rcRet); } while (0)
     123# define kHlpAssertMsgReturnVoid(expr, msg)     do { if (!(expr)) return; } while (0)
    101124#endif  /* !K_STRICT */
    102125
    103126#define kHlpAssertPtr(ptr)                      kHlpAssertMsg(K_VALID_PTR(ptr), ("%s = %p\n", #ptr, (ptr)))
    104127#define kHlpAssertPtrReturn(ptr, rcRet)         kHlpAssertMsgReturn(K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)), (rcRet))
     128#define kHlpAssertPtrReturnVoid(ptr)            kHlpAssertMsgReturnVoid(K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)))
    105129#define kHlpAssertPtrNull(ptr)                  kHlpAssertMsg(!(ptr) || K_VALID_PTR(ptr), ("%s = %p\n", #ptr, (ptr)))
    106130#define kHlpAssertPtrNullReturn(ptr, rcRet)     kHlpAssertMsgReturn(!(ptr) || K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)), (rcRet))
     131#define kHlpAssertPtrNullReturnVoid(ptr)        kHlpAssertMsgReturnVoid(!(ptr) || K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)))
    107132#define kHlpAssertRC(rc)                        kHlpAssertMsg((rc) == 0, ("%s = %d\n", #rc, (rc)))
    108133#define kHlpAssertRCReturn(rc, rcRet)           kHlpAssertMsgReturn((rc) == 0, ("%s = %d -> %d\n", #rc, (rc), (rcRet)), (rcRet))
     134#define kHlpAssertRCReturnVoid(rc)              kHlpAssertMsgReturnVoid((rc) == 0, ("%s = %d -> %d\n", #rc, (rc), (rcRet)))
    109135#define kHlpAssertFailed()                      kHlpAssert(0)
    110136#define kHlpAssertFailedReturn(rcRet)           kHlpAssertReturn(0, (rcRet))
     137#define kHlpAssertFailedReturnVoid()            kHlpAssertReturnVoid(0)
    111138#define kHlpAssertMsgFailed(msg)                kHlpAssertMsg(0, msg)
    112139#define kHlpAssertMsgFailedReturn(msg, rcRet)   kHlpAssertMsgReturn(0, msg, (rcRet))
     140#define kHlpAssertMsgFailedReturnVoid(msg)      kHlpAssertMsgReturnVoid(0, msg))
    113141
    114142/**
  • trunk/kStuff/include/k/kHlpString.h

    r3546 r3550  
    8585int     kHlpMemIComp(const void *pv1, const void *pv2, KSIZE cb);
    8686
     87#ifndef kHlpStrCat
     88char   *kHlpStrCat(char *psz1, const char *psz2);
     89#endif
     90#ifndef kHlpStrNCat
     91char   *kHlpStrNCat(char *psz1, const char *psz2, KSIZE cb);
     92#endif
    8793#ifndef kHlpStrChr
    8894char   *kHlpStrChr(const char *psz, int ch);
     95#endif
     96#ifndef kHlpStrRChr
     97char   *kHlpStrRChr(const char *psz, int ch);
    8998#endif
    9099#ifndef kHlpStrComp
     
    93102#ifndef kHlpStrNComp
    94103int     kHlpStrNComp(const char *psz1, const char *psz2, KSIZE cch);
     104#endif
     105#ifndef kHlpStrCopy
     106char   *kHlpStrCopy(char *psz1, const char *psz2);
     107#endif
     108#ifndef kHlpStrNCopy
     109char   *kHlpStrNCopy(char *psz1, const char *psz2, KSIZE cb);
    95110#endif
    96111#ifndef kHlpStrLen
  • trunk/kStuff/include/k/kMagics.h

    r3543 r3550  
    3030
    3131/** The magic for KRDR::u32Magic. (Katsu Aki (Katsuaki Nakamura))
     32 * @ingroup grp_kRdrAll */
     33#define KRDR_MAGIC          0x19610919
     34/** The magic value for the debug module structure. (Some manga artist)
    3235 * @ingroup grp_kDbgAll */
    33 #define KRDR_MAGIC   0x19610919
    34 
     36#define KDBGMOD_MAGIC       0x19200501
    3537
    3638#endif
  • trunk/kStuff/include/k/kRdr.h

    r3548 r3550  
    6565KRDR_DECL(KIPTR)    kRdrNativeFH( PKRDR pRdr);
    6666KRDR_DECL(KSIZE)    kRdrPageSize( PKRDR pRdr);
    67 #ifndef ___k_kLdr___
     67#ifdef ___k_kLdr___
    6868KRDR_DECL(int)      kRdrMap(      PKRDR pRdr, void **ppvBase, KU32 cSegments, PCKLDRSEG paSegments, KBOOL fFixed);
    6969KRDR_DECL(int)      kRdrRefresh(  PKRDR pRdr, void *pvBase, KU32 cSegments, PCKLDRSEG paSegments);
     
    7171KRDR_DECL(int)      kRdrUnmap(    PKRDR pRdr, void *pvBase, KU32 cSegments, PCKLDRSEG paSegments);
    7272#endif /* !___k_kLdr___ */
    73 KRDR_DECL(void)     kRdrDone(PKRDR pRdr);
     73KRDR_DECL(void)     kRdrDone(     PKRDR pRdr);
    7474
    7575KRDR_DECL(int)      kRdrBufOpen(PPKRDR ppRdr, const char *pszFilename);
    76 KRDR_DECL(int)      kRdrBufWrap(PPKRDR ppRdr, PKRDR pRdr);
    77 KRDR_DECL(int)      kRdrBufLine(  PKRDR pRdr, char *pszLine, KSIZE cbLine);
     76KRDR_DECL(int)      kRdrBufWrap(PPKRDR ppRdr, PKRDR pRdr, KBOOL fCloseIt);
     77KRDR_DECL(KBOOL)    kRdrBufIsBuffered(PKRDR pRdr);
     78KRDR_DECL(int)      kRdrBufLine(PKRDR pRdr, char *pszLine, KSIZE cbLine);
    7879KRDR_DECL(int)      kRdrBufLineEx(PKRDR pRdr, char *pszLine, KSIZE *pcbLine);
    7980KRDR_DECL(const char *) kRdrBufLineQ(PKRDR pRdr);
  • trunk/kStuff/include/k/kRdrAll.h

    r3546 r3550  
    11/* $Id$ */
    22/** @file
    3  * kRdr - The File Provider, All Dependencies Included.
     3 * kRdr - The File Provider, All Details and Dependencies Included.
    44 */
    55
     
    2525 */
    2626
    27 #ifndef ___kRdrAll_h___
    28 #define ___kRdrAll_h___
     27#ifndef ___k_kRdrAll_h___
     28#define ___k_kRdrAll_h___
    2929
    3030#include <k/kDefs.h>
  • trunk/kStuff/include/k/kTypes.h

    r3545 r3550  
    166166typedef signed char             KI8;
    167167typedef unsigned char           KU8;
    168 #define KI64_C(c)               (c ## ULL)
    169 #define KU64_C(c)               (c ## LL)
     168#define KI64_C(c)               (c ## LL)
     169#define KU64_C(c)               (c ## ULL)
    170170#define KI32_C(c)               (c)
    171171#define KU32_C(c)               (c)
     
    224224#   define KX64_PRI              "llx"
    225225#  endif
    226 #  define KI64_C(c)             (c ## ULL)
    227 #  define KU64_C(c)             (c ## LL)
     226#  define KI64_C(c)             (c ## LL)
     227#  define KU64_C(c)             (c ## ULL)
    228228# else
    229229typedef signed long int         KI64;
    230230typedef unsigned long int       KU64;
    231 #  define KI64_C(c)             (c ## UL)
    232 #  define KU64_C(c)             (c ## L)
     231#  define KI64_C(c)             (c ## L)
     232#  define KU64_C(c)             (c ## UL)
    233233#  define KI64_PRI              "ld"
    234234#  define KU64_PRI              "lu"
  • trunk/kStuff/kDbg/Makefile.kmk

    r3537 r3550  
    3131# kDbg - The profiler module.
    3232#
    33 DLLS += kDbg
     33#DLLS += kDbg - disabled for now.
    3434kDbg_TEMPLATE = kStuffDLL
    3535kDbg_DEFS = KDBG_BUILDING KDBG_RESIDES_IN_DLL
     
    3737kDbg_SOURCES := \
    3838        kDbgModule.cpp \
    39         kDbgModPE.cpp \
    40         \
    4139        kDbgLine.cpp \
    42         \
    43         kDbgSymbol.cpp \
    44         \
    45         kDbgHlpCrt.cpp
     40        kDbgSymbol.cpp
    4641
    4742kDbg_SOURCES += \
     
    6661kDbgDump_LIBS = \
    6762        $(TARGET_kDbgStatic) \
    68         $(TARGET_kDbgStatic:kDbgStatic=kLdrStatic)
     63        $(subst kDbg,kLdr,$(TARGET_kDbgStatic)) \
     64        $(subst kDbg,kRdr,$(TARGET_kDbgStatic))
    6965
    7066# Generate the rules
  • trunk/kStuff/kDbg/kDbgDump.cpp

    r3537 r3550  
    2828*   Header Files                                                               *
    2929*******************************************************************************/
    30 #include "kDbg.h"
     30#include <k/kDbg.h>
    3131#include <string.h>
    3232#include <stdio.h>
     
    5353{
    5454    PKDBGMOD pDbgMod;
    55     int rc = kDbgModuleOpen(pszFile, &pDbgMod);
     55    int rc = kDbgModuleOpen(&pDbgMod, pszFile, NULL);
    5656    if (rc)
    5757    {
  • trunk/kStuff/kDbg/kDbgInternal.h

    r3541 r3550  
    2626#define ___kDbgInternal_h___
    2727
    28 #include <k/kDbg.h>
    29 #include "kDbgHlp.h"
     28#include <k/kHlpAssert.h>
     29#include <k/kMagics.h>
     30#include <k/kErrors.h>
     31#include <k/kDbgAll.h>
     32
     33
     34/** @defgroup grp_kDbgInternal  Internal
     35 * @internal
     36 * @addtogroup grp_kDbg
     37 * @{
     38 */
     39
     40/** @def KDBG_STRICT
     41 * If defined the kDbg assertions and other runtime checks will be enabled. */
     42#ifdef K_ALL_STRICT
     43# undef KDBG_STRICT
     44# define KDBG_STRICT
     45#endif
     46
     47/** @name Our Assert macros
     48 * @{ */
     49#ifdef KDBG_STRICT
     50# define kDbgAssert(expr)                       kHlpAssert(expr)
     51# define kDbgAssertReturn(expr, rcRet)          kHlpAssertReturn(expr, rcRet)
     52# define kDbgAssertReturnVoid(expr)             kHlpAssertReturnVoid(expr)
     53# define kDbgAssertMsg(expr, msg)               kHlpAssertMsg(expr, msg)
     54# define kDbgAssertMsgReturn(expr, msg, rcRet)  kHlpAssertMsgReturn(expr, msg, rcRet)
     55# define kDbgAssertMsgReturnVoid(expr, msg)     kHlpAssertMsgReturnVoid(expr, msg)
     56#else   /* !KDBG_STRICT */
     57# define kDbgAssert(expr)                       do { } while (0)
     58# define kDbgAssertReturn(expr, rcRet)          do { if (!(expr)) return (rcRet); } while (0)
     59# define kDbgAssertMsg(expr, msg)               do { } while (0)
     60# define kDbgAssertMsgReturn(expr, msg, rcRet)  do { if (!(expr)) return (rcRet); } while (0)
     61#endif  /* !KDBG_STRICT */
     62
     63#define kDbgAssertPtr(ptr)                      kDbgAssertMsg(K_VALID_PTR(ptr), ("%s = %p\n", #ptr, (ptr)))
     64#define kDbgAssertPtrReturn(ptr, rcRet)         kDbgAssertMsgReturn(K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)), (rcRet))
     65#define kDbgAssertPtrReturnVoid(ptr)            kDbgAssertMsgReturnVoid(K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)))
     66#define kDbgAssertPtrNull(ptr)                  kDbgAssertMsg(!(ptr) || K_VALID_PTR(ptr), ("%s = %p\n", #ptr, (ptr)))
     67#define kDbgAssertPtrNullReturn(ptr, rcRet)     kDbgAssertMsgReturn(!(ptr) || K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)), (rcRet))
     68#define kDbgAssertPtrNullReturnVoid(ptr)        kDbgAssertMsgReturnVoid(!(ptr) || K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)))
     69#define kDbgAssertRC(rc)                        kDbgAssertMsg((rc) == 0, ("%s = %d\n", #rc, (rc)))
     70#define kDbgAssertRCReturn(rc, rcRet)           kDbgAssertMsgReturn((rc) == 0, ("%s = %d -> %d\n", #rc, (rc), (rcRet)), (rcRet))
     71#define kDbgAssertRCReturnVoid(rc)              kDbgAssertMsgReturnVoid((rc) == 0, ("%s = %d -> %d\n", #rc, (rc), (rcRet)))
     72#define kDbgAssertFailed()                      kDbgAssert(0)
     73#define kDbgAssertFailedReturn(rcRet)           kDbgAssertReturn(0, (rcRet))
     74#define kDbgAssertFailedReturnVoid()            kDbgAssertReturnVoid(0)
     75#define kDbgAssertMsgFailed(msg)                kDbgAssertMsg(0, msg)
     76#define kDbgAssertMsgFailedReturn(msg, rcRet)   kDbgAssertMsgReturn(0, msg, (rcRet))
     77#define kDbgAssertMsgFailedReturnVoid(msg)      kDbgAssertMsgReturnVoid(0, msg)
     78/** @} */
     79
     80/** Return / crash validation of a reader argument. */
     81#define KDBGMOD_VALIDATE_EX(pDbgMod, rc) \
     82    do  { \
     83        kDbgAssertPtrReturn((pDbgMod), (rc)); \
     84        kDbgAssertReturn((pDbgMod)->u32Magic == KDBGMOD_MAGIC, (rc)); \
     85        kDbgAssertReturn((pDbgMod)->pOps != NULL, (rc)); \
     86    } while (0)
     87
     88/** Return / crash validation of a reader argument. */
     89#define KDBGMOD_VALIDATE(pDbgMod) \
     90    do  { \
     91        kDbgAssertPtrReturn((pDbgMod), KERR_INVALID_POINTER); \
     92        kDbgAssertReturn((pDbgMod)->u32Magic == KDBGMOD_MAGIC, KERR_INVALID_HANDLE); \
     93        kDbgAssertReturn((pDbgMod)->pOps != NULL, KERR_INVALID_HANDLE); \
     94    } while (0)
     95
     96/** Return / crash validation of a reader argument. */
     97#define KDBGMOD_VALIDATE_VOID(pDbgMod) \
     98    do  { \
     99        kDbgAssertPtrReturnVoid((pDbgMod)); \
     100        kDbgAssertReturnVoid((pDbgMod)->u32Magic == KDBGMOD_MAGIC); \
     101        kDbgAssertReturnVoid((pDbgMod)->pOps != NULL); \
     102    } while (0)
     103
    30104
    31105#ifdef __cplusplus
     
    52126#endif
    53127
     128/** @} */
     129
    54130#endif
    55131
  • trunk/kStuff/kDbg/kDbgLine.cpp

    r3541 r3550  
    77 * Copyright (c) 2006-2007 knut st. osmundsen <bird-src-spam@anduin.net>
    88 *
    9  * This file is part of kLIBC.
     9 * This file is part of kStuff.
    1010 *
    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.
     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 *
    16  * kLIBC is distributed in the hope that it will be useful,
     16 * 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 General Public License for more details.
     18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     19 * Lesser General Public License for more details.
    2020 *
    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
     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
    2424 *
    2525 */
    26 
    2726
    2827/*******************************************************************************
    2928*   Header Files                                                               *
    3029*******************************************************************************/
    31 #include "kDbg.h"
    3230#include "kDbgInternal.h"
    33 
     31#include <k/kHlpAlloc.h>
    3432
    3533
     
    4745KDBG_DECL(PKDBGLINE) kDbgLineDup(PCKDBGLINE pLine)
    4846{
    49     kDbgAssertMsgReturn(KDBG_VALID_PTR(pLine), ("%p\n", pLine), NULL);
    50     size_t cb = KDBG_OFFSETOF(KDBGLINE, szFile[pLine->cchFile + 1]);
    51     return (PKDBGLINE)kDbgHlpAllocDup(pLine, cb);
     47    kDbgAssertPtrReturn(pLine, NULL);
     48    KSIZE cb = K_OFFSETOF(KDBGLINE, szFile[pLine->cchFile + 1]);
     49    PKDBGLINE pNewLine = (PKDBGLINE)kHlpDup(pLine, cb);
     50    if (pNewLine)
     51        pNewLine->cbSelf = cb;
     52    return pNewLine;
    5253}
    5354
    5455
    5556/**
    56  * Frees a line number obtained from the RTDbg API.
     57 * Frees a line number obtained from the kDbg API.
    5758 *
    5859 * @returns 0 on success.
    59  * @returns KERR_INVALID_POINTER if a NULL pointer or an !KDBG_VALID_PTR() is passed in.
     60 * @returns KERR_INVALID_POINTER if pLine isn't a valid pointer.
    6061 *
    61  * @param   pLine       The line number to be freed.
     62 * @param   pLine       The line number to be freed. The null pointer is ignored.
    6263 */
    6364KDBG_DECL(int) kDbgLineFree(PKDBGLINE pLine)
    6465{
    65     if (!pLine)
    66         return KERR_INVALID_POINTER;
    67     kDbgAssertMsgReturn(KDBG_VALID_PTR(pLine), ("%p\n", pLine), KERR_INVALID_POINTER);
    68 
    69     kDbgHlpFree(pLine);
     66    if (pLine)
     67    {
     68        kDbgAssertPtrReturn(pLine, KERR_INVALID_POINTER);
     69        pLine->cbSelf = 0;
     70        kHlpFree(pLine);
     71    }
    7072    return 0;
    7173}
  • trunk/kStuff/kDbg/kDbgModWinDbgHelp.cpp

    r3541 r3550  
    3232#define _IMAGEHLP64
    3333#include <DbgHelp.h>
    34 #include <malloc.h> /* alloca */
    3534
    3635#include "kDbgInternal.h"
     36#include <k/kHlpAlloc.h>
     37#include <k/kHlpString.h>
    3738
    3839
     
    7273    HANDLE      hSymInst;
    7374    /** The image size. */
    74     uint32_t    cbImage;
     75    KU32        cbImage;
    7576    /** The number of sections. (We've added the implicit header section.) */
    76     int32_t     cSections;
     77    KI32        cSections;
    7778    /** The section headers (variable size). The first section is the
    7879     * implicit header section.*/
     
    107108 * @param   puRVA       Where to store the RVA on success.
    108109 */
    109 static int kdbgModDHSegOffToRVA(PKDBGMODDBGHELP pModDH, int32_t iSegment, KDBGADDR off, uint32_t *puRVA)
     110static int kdbgModDHSegOffToRVA(PKDBGMODDBGHELP pModDH, KI32 iSegment, KDBGADDR off, KU32 *puRVA)
    110111{
    111112    if (iSegment >= 0)
     
    116117                            ("off=" PRI_KDBGADDR " VirtualSize=%x\n", off, pModDH->aSections[iSegment].Misc.VirtualSize),
    117118                            KDBG_ERR_INVALID_ADDRESS);
    118         *puRVA = pModDH->aSections[iSegment].VirtualAddress + (uint32_t)off;
     119        *puRVA = pModDH->aSections[iSegment].VirtualAddress + (KU32)off;
    119120        return 0;
    120121    }
     
    124125        kDbgAssertMsgReturn(off < pModDH->cbImage, ("off=" PRI_KDBGADDR ", cbImage=%x\n", off, pModDH->cbImage),
    125126                            KDBG_ERR_INVALID_ADDRESS);
    126         *puRVA = (uint32_t)off;
     127        *puRVA = (KU32)off;
    127128        return 0;
    128129    }
     
    141142 * @param   poff        Where to store the segment offset.
    142143 */
    143 static int kdbgModDHRVAToSegOff(PKDBGMODDBGHELP pModDH, uint32_t uRVA, int32_t *piSegment, KDBGADDR *poff)
     144static int kdbgModDHRVAToSegOff(PKDBGMODDBGHELP pModDH, KU32 uRVA, KI32 *piSegment, KDBGADDR *poff)
    144145{
    145146    kDbgAssertMsgReturn(uRVA < pModDH->cbImage, ("uRVA=%x, cbImage=%x\n", uRVA, pModDH->cbImage),
    146147                        KDBG_ERR_INVALID_ADDRESS);
    147     for (int32_t iSegment = 0; iSegment < pModDH->cSections; iSegment++)
     148    for (KI32 iSegment = 0; iSegment < pModDH->cSections; iSegment++)
    148149    {
    149150        /** @todo should probably be less strict about address in the alignment gaps. */
    150         uint32_t off = uRVA - pModDH->aSections[iSegment].VirtualAddress;
     151        KU32 off = uRVA - pModDH->aSections[iSegment].VirtualAddress;
    151152        if (off < pModDH->aSections[iSegment].Misc.VirtualSize)
    152153        {
     
    163164 * @copydoc KDBGMODOPS::pfnQueryLine
    164165 */
    165 static int kdbgModDHQueryLine(PKDBGMOD pMod, int32_t iSegment, KDBGADDR off, PKDBGLINE pLine)
     166static int kdbgModDHQueryLine(PKDBGMOD pMod, KI32 iSegment, KDBGADDR off, PKDBGLINE pLine)
    166167{
    167168    PKDBGMODDBGHELP pModDH = (PKDBGMODDBGHELP)pMod;
     
    170171     * Translate the address to an RVA.
    171172     */
    172     uint32_t uRVA;
     173    KU32 uRVA;
    173174    int rc = kdbgModDHSegOffToRVA(pModDH, iSegment, off, &uRVA);
    174175    if (!rc)
     
    180181        {
    181182            pLine->RVA = (KDBGADDR)(Line.Address - pModDH->ImageBase);
    182             rc = kdbgModDHRVAToSegOff(pModDH, (uint32_t)pLine->RVA, &pLine->iSegment, &pLine->offSegment);
     183            rc = kdbgModDHRVAToSegOff(pModDH, (KU32)pLine->RVA, &pLine->iSegment, &pLine->offSegment);
    183184            pLine->iLine = Line.LineNumber;
    184             size_t cchFile = strlen(Line.FileName);
     185            KSIZE cchFile = kHlpStrLen(Line.FileName);
    185186            pLine->cchFile = cchFile < sizeof(pLine->szFile)
    186                            ? (uint16_t)cchFile
    187                            : (uint16_t)sizeof(pLine->szFile) - 1;
    188             memcpy(pLine->szFile, Line.FileName, pLine->cchFile);
     187                           ? (KU16)cchFile
     188                           : (KU16)sizeof(pLine->szFile) - 1;
     189            kHlpMemCopy(pLine->szFile, Line.FileName, pLine->cchFile);
    189190            pLine->szFile[pLine->cchFile] = '\0';
    190191        }
     
    202203 * @copydoc KDBGMODOPS::pfnQuerySymbol
    203204 */
    204 static int kdbgModDHQuerySymbol(PKDBGMOD pMod, int32_t iSegment, KDBGADDR off, PKDBGSYMBOL pSym)
     205static int kdbgModDHQuerySymbol(PKDBGMOD pMod, KI32 iSegment, KDBGADDR off, PKDBGSYMBOL pSym)
    205206{
    206207    PKDBGMODDBGHELP pModDH = (PKDBGMODDBGHELP)pMod;
     
    209210     * Translate the address to an RVA.
    210211     */
    211     uint32_t uRVA;
     212    KU32 uRVA;
    212213    int rc = kdbgModDHSegOffToRVA(pModDH, iSegment, off, &uRVA);
    213214    if (!rc)
     
    242243            {
    243244                pSym->RVA        = (KDBGADDR)(Buf.Sym.Address - pModDH->ImageBase);
    244                 rc = kdbgModDHRVAToSegOff(pModDH, (uint32_t)pSym->RVA, &pSym->iSegment, &pSym->offSegment);
     245                rc = kdbgModDHRVAToSegOff(pModDH, (KU32)pSym->RVA, &pSym->iSegment, &pSym->offSegment);
    245246            }
    246             pSym->cchName = (uint16_t)Buf.Sym.NameLen;
     247            pSym->cchName = (KU16)Buf.Sym.NameLen;
    247248            if (pSym->cchName >= sizeof(pSym->szName))
    248249                pSym->cchName = sizeof(pSym->szName) - 1;
    249             memcpy(pSym->szName, Buf.Sym.Name, Buf.Sym.NameLen);
     250            kHlpMemCopy(pSym->szName, Buf.Sym.Name, Buf.Sym.NameLen);
    250251            pSym->szName[Buf.Sym.NameLen] = '\0';
    251252        }
     
    272273    DWORD Err = GetLastError();
    273274    int rc = kdbgModDHConvWinError(Err);
    274     kDbgAssertMsgFailed(("SymInitialize failed: Err=%d rc=%Rrc\n", Err, rc));
     275    kDbgAssertMsgFailed(("SymInitialize failed: Err=%d rc=%d\n", Err, rc));
    275276    return rc;
    276277}
     
    278279
    279280/**
    280  * Methods for a PE module.
    281  */
    282 static const KDBGMODOPS g_kdbgModDHOps =
    283 {
    284     "PE (dbghelp)",
    285     kdbgModDHClose,
    286     kdbgModDHQuerySymbol,
    287     kdbgModDHQueryLine
    288 };
    289 
    290 
    291 /**
    292281 * Checks if the specified dbghelp.dll is usable.
    293282 *
     
    296285 * @param   pszPath     the path to the dbghelp.dll.
    297286 */
    298 static int kdbgModDHTryDbgHelp(const char *pszPath, uint32_t *pu32FileVersionMS, uint32_t *pu32FileVersionLS)
     287static int kdbgModDHTryDbgHelp(const char *pszPath, KU32 *pu32FileVersionMS, KU32 *pu32FileVersionLS)
    299288{
    300289    int rc;
     
    337326 * Find the dbghelp.dll
    338327 */
    339 static int kdbgModDHFindDbgHelp(char *pszPath, size_t cchPath)
     328static int kdbgModDHFindDbgHelp(char *pszPath, KSIZE cchPath)
    340329{
    341330    /*
    342331     * Try the current directory.
    343332     */
    344     uint32_t FileVersionMS = 0;
    345     uint32_t FileVersionLS = 0;
     333    KU32 FileVersionMS = 0;
     334    KU32 FileVersionLS = 0;
    346335    int rc = KERR_GENERAL_FAILURE;
    347336    static char s_szDbgHelp[] = "\\dbghelp.dll";
     
    361350    if (GetModuleFileName(NULL, pszPath, (DWORD)(cchPath - sizeof(s_szDbgHelp) + 1)))
    362351    {
    363         strcat(strrchr(pszPath, '\\'), s_szDbgHelp);
     352        kHlpStrCat(kHlpStrRChr(pszPath, '\\'), s_szDbgHelp);
    364353        int rc2 = kdbgModDHTryDbgHelp(pszPath, &FileVersionMS, &FileVersionLS);
    365354        if (!rc)
     
    374363    if (GetSystemDirectory(pszPath, (DWORD)(cchPath - sizeof(s_szDbgHelp) + 1)))
    375364    {
    376         strcat(pszPath, s_szDbgHelp);
     365        kHlpStrCat(pszPath, s_szDbgHelp);
    377366        int rc2 = kdbgModDHTryDbgHelp(pszPath, &FileVersionMS, &FileVersionLS);
    378367        if (!rc2)
     
    387376    if (GetWindowsDirectory(pszPath, (DWORD)(cchPath - sizeof(s_szDbgHelp) + 1)))
    388377    {
    389         strcat(pszPath, s_szDbgHelp);
     378        kHlpStrCat(pszPath, s_szDbgHelp);
    390379        int rc2 = kdbgModDHTryDbgHelp(pszPath, &FileVersionMS, &FileVersionLS);
    391380        if (!rc2)
     
    407396        {
    408397            /* find the end of the path. */
    409             char *pszEnd = strchr(psz, ';');
     398            char *pszEnd = kHlpStrChr(psz, ';');
    410399            if (!pszEnd)
    411                 pszEnd = strchr(psz, '\0');
     400                pszEnd = kHlpStrChr(psz, '\0');
    412401            if (pszEnd != psz)
    413402            {
    414403                /* construct filename and try it out */
    415                 memcpy(pszPath, psz, pszEnd - psz);
    416                 memcpy(&pszPath[pszEnd - psz], s_szDbgHelp, sizeof(s_szDbgHelp));
     404                kHlpMemCopy(pszPath, psz, pszEnd - psz);
     405                kHlpMemCopy(&pszPath[pszEnd - psz], s_szDbgHelp, sizeof(s_szDbgHelp));
    417406                int rc2 = kdbgModDHTryDbgHelp(pszPath, &FileVersionMS, &FileVersionLS);
    418407                if (!rc2)
     
    453442
    454443    /* primitive locking - make some useful API for this kind of spinning! */
    455     static volatile uint32_t s_u32Lock = 0;
    456     while (!InterlockedCompareExchange((long volatile *)&s_u32Lock, 1, 0))
    457         while (s_u32Lock)
     444    static volatile long s_lLock = 0;
     445    while (!InterlockedCompareExchange(&s_lLock, 1, 0))
     446        while (s_lLock)
    458447            Sleep(1);
    459448    if (g_hDbgHelp)
    460449    {
    461         InterlockedExchange((long volatile *)&s_u32Lock, 0);
     450        InterlockedExchange(&s_lLock, 0);
    462451        return 0;
    463452    }
     
    470459    if (rc)
    471460    {
    472         InterlockedExchange((volatile long *)&s_u32Lock, 0);
     461        InterlockedExchange(&s_lLock, 0);
    473462        return rc;
    474463    }
     
    479468        DWORD Err = GetLastError();
    480469        int rc = kdbgModDHConvWinError(Err);
    481         kDbgAssertMsgFailedReturn(("Failed to load '%s', Err=%d rc=%Rrc\n", szPath, Err, rc), rc);
     470        kDbgAssertMsgFailedReturn(("Failed to load '%s', Err=%d rc=%d\n", szPath, Err, rc), rc);
    482471    }
    483472
     
    515504                { "SymGetLineFromAddr64", (FARPROC *)&g_pfnSymGetLineFromAddr64 },
    516505            };
    517             for (unsigned i = 0; i < KDBG_ELEMENTS(s_aFunctions); i++)
     506            for (unsigned i = 0; i < K_ELEMENTS(s_aFunctions); i++)
    518507            {
    519508                FARPROC pfn = GetProcAddress(hmod, s_aFunctions[i].pszName);
     
    522511                    DWORD Err = GetLastError();
    523512                    rc = kdbgModDHConvWinError(Err);
    524                     kDbgAssertMsgFailed(("Failed to resolve %s in dbghelp, Err=%d rc=%Rrc\n",
    525                                      s_aFunctions[i].pszName, Err, rc));
     513                    kDbgAssertMsgFailed(("Failed to resolve %s in dbghelp, Err=%d rc=%d\n",
     514                                         s_aFunctions[i].pszName, Err, rc));
    526515                    break;
    527516                }
     
    533522                g_hDbgHelp = hmod;
    534523                Sleep(1);
    535                 InterlockedExchange((volatile long *)&s_u32Lock, 0);
     524                InterlockedExchange(&s_lLock, 0);
    536525                return 0;
    537526            }
     
    547536        DWORD Err = GetLastError();
    548537        rc = kdbgModDHConvWinError(Err);
    549         kDbgAssertMsgFailed(("Failed to resolve ImagehlpApiVersionEx in dbghelp, Err=%d rc=%Rrc\n", Err, rc));
     538        kDbgAssertMsgFailed(("Failed to resolve ImagehlpApiVersionEx in dbghelp, Err=%d rc=%d\n", Err, rc));
    550539    }
    551540    FreeLibrary(hmod);
    552     InterlockedExchange((long volatile *)&s_u32Lock, 0);
     541    InterlockedExchange(&s_lLock, 0);
    553542    return rc;
    554543}
     
    556545
    557546/**
    558  * Opens the debug info for a PE image using the windows dbghelp library.
    559  *
    560  * @returns IPRT status code.
    561  *
    562  * @param   pFile               The handle to the module.
    563  * @param   offHdr              The offset of the PE header.
    564  * @param   pszModulePath       The path to the module.
    565  * @param   ppDbgMod            Where to store the module handle.
    566  *
    567  */
    568 int kdbgModWinDbgHelpOpen(PKDBGHLPFILE pFile, PKDBGMOD *ppDbgMod)
    569 {
     547 * @copydoc KDBGMODOPS::pfnOpen
     548 */
     549static int kdbgModDHOpen(PKDBGMOD *ppMod, PKRDR pRdr, KBOOL fCloseRdr, KFOFF off, KFOFF cb, struct KLDRMOD *pLdrMod)
     550{
     551    /*
     552     * This reader doesn't support partial files.
     553     * Also weed out small files early on as they cannot be
     554     * PE images and will only cause read errors
     555     */
     556    if (    off != 0
     557        ||  cb != KFOFF_MAX)
     558        return KDBG_ERR_UNKOWN_FORMAT;
     559    if (kRdrSize(pRdr) < sizeof(IMAGE_NT_HEADERS32) + sizeof(IMAGE_SECTION_HEADER))
     560        return KDBG_ERR_UNKOWN_FORMAT;
     561
    570562    /*
    571563     * We need to read the section headers and get the image size.
    572564     */
     565    /* Find the PE header magic. */
     566    KU32 offHdr = 0;
     567    KU32 u32Magic;
     568    int rc = kRdrRead(pRdr, &u32Magic, sizeof(u32Magic), 0);
     569    kDbgAssertRCReturn(rc, rc);
     570    if ((KU16)u32Magic == IMAGE_DOS_SIGNATURE)
     571    {
     572        rc = kRdrRead(pRdr, &offHdr, sizeof(offHdr), K_OFFSETOF(IMAGE_DOS_HEADER, e_lfanew));
     573        kDbgAssertRCReturn(rc, rc);
     574        if (!offHdr)
     575            return KDBG_ERR_FORMAT_NOT_SUPPORTED;
     576        if (    offHdr < sizeof(IMAGE_DOS_SIGNATURE)
     577            ||  offHdr >= kRdrSize(pRdr) - 4)
     578            return KDBG_ERR_BAD_EXE_FORMAT;
     579
     580        rc = kRdrRead(pRdr, &u32Magic, sizeof(u32Magic), offHdr);
     581        kDbgAssertRCReturn(rc, rc);
     582    }
     583    if (u32Magic != IMAGE_NT_SIGNATURE)
     584        return KDBG_ERR_FORMAT_NOT_SUPPORTED;
     585
     586    /* read the file header and the image size in the optional header.. */
    573587    IMAGE_FILE_HEADER FHdr;
    574     int rc = kDbgHlpReadAt(pFile, offHdr + KDBG_OFFSETOF(IMAGE_NT_HEADERS32, FileHeader), &FHdr, sizeof(FHdr));
     588    rc = kRdrRead(pRdr, &FHdr, sizeof(FHdr), K_OFFSETOF(IMAGE_NT_HEADERS32, FileHeader));
    575589    kDbgAssertRCReturn(rc, rc);
    576590
    577     uint32_t cbImage;
     591    KU32 cbImage;
    578592    if (FHdr.SizeOfOptionalHeader == sizeof(IMAGE_OPTIONAL_HEADER32))
    579         rc = kDbgHlpReadAt(pFile, offHdr + KDBG_OFFSETOF(IMAGE_NT_HEADERS32, OptionalHeader.SizeOfImage),
    580                            &cbImage, sizeof(cbImage));
     593        rc = kRdrRead(pRdr, &cbImage, sizeof(cbImage),
     594                      offHdr + K_OFFSETOF(IMAGE_NT_HEADERS32, OptionalHeader.SizeOfImage));
    581595    else if (FHdr.SizeOfOptionalHeader == sizeof(IMAGE_OPTIONAL_HEADER64))
    582         rc = kDbgHlpReadAt(pFile, offHdr + KDBG_OFFSETOF(IMAGE_NT_HEADERS64, OptionalHeader.SizeOfImage),
    583                            &cbImage, sizeof(cbImage));
     596        rc = kRdrRead(pRdr, &cbImage, sizeof(cbImage),
     597                      offHdr + K_OFFSETOF(IMAGE_NT_HEADERS64, OptionalHeader.SizeOfImage));
    584598    else
    585599        kDbgAssertFailedReturn(KDBG_ERR_BAD_EXE_FORMAT);
     
    596610     * Allocate the module and read/construct the section headers.
    597611     */
    598     PKDBGMODDBGHELP pModDH = (PKDBGMODDBGHELP)kDbgHlpAlloc(KDBG_OFFSETOF(KDBGMODDBGHELP, aSections[FHdr.NumberOfSections + 2]));
     612    PKDBGMODDBGHELP pModDH = (PKDBGMODDBGHELP)kHlpAlloc(K_OFFSETOF(KDBGMODDBGHELP, aSections[FHdr.NumberOfSections + 2]));
    599613    kDbgAssertReturn(pModDH, KERR_NO_MEMORY);
    600614    pModDH->Core.u32Magic   = KDBGMOD_MAGIC;
    601     pModDH->Core.pOps       = &g_kdbgModDHOps;
    602     pModDH->Core.pFile      = pFile;
     615    pModDH->Core.pOps       = &g_kDbgModWinDbgHelpOpen;
     616    pModDH->Core.pRdr       = pRdr;
     617    pModDH->Core.fCloseRdr  = fCloseRdr;
     618    pModDH->Core.pLdrMod    = pLdrMod;
    603619    pModDH->cbImage         = cbImage;
    604620    pModDH->cSections       = 1 + FHdr.NumberOfSections;
    605     rc = kDbgHlpReadAt(pFile, offHdr + KDBG_OFFSETOF(IMAGE_NT_HEADERS32, OptionalHeader) + FHdr.SizeOfOptionalHeader,
    606                        &pModDH->aSections[1], sizeof(pModDH->aSections[0]) * FHdr.NumberOfSections);
     621
     622    rc = kRdrRead(pRdr, &pModDH->aSections[1], sizeof(pModDH->aSections[0]) * FHdr.NumberOfSections,
     623                  offHdr + K_OFFSETOF(IMAGE_NT_HEADERS32, OptionalHeader) + FHdr.SizeOfOptionalHeader);
    607624    if (!rc)
    608625    {
    609626        PIMAGE_SECTION_HEADER pSH = &pModDH->aSections[0];
    610         memcpy(pSH->Name, "headers", sizeof(pSH->Name));
     627        kHlpMemCopy(pSH->Name, "headers", sizeof(pSH->Name));
    611628        pSH->Misc.VirtualSize       = pModDH->aSections[1].VirtualAddress;
    612629        pSH->VirtualAddress         = 0;
     
    619636        pSH->Characteristics        = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_ALIGN_4BYTES | IMAGE_SCN_MEM_READ;
    620637
    621         uint32_t uTheEnd = pModDH->aSections[FHdr.NumberOfSections].VirtualAddress
    622                          + pModDH->aSections[FHdr.NumberOfSections].Misc.VirtualSize;
     638        KU32 uTheEnd = pModDH->aSections[FHdr.NumberOfSections].VirtualAddress
     639                     + pModDH->aSections[FHdr.NumberOfSections].Misc.VirtualSize;
    623640        if (uTheEnd < cbImage)
    624641        {
    625642            pSH = &pModDH->aSections[pModDH->cSections++];
    626             memcpy(pSH->Name, "tail\0\0\0", sizeof(pSH->Name));
     643            kHlpMemCopy(pSH->Name, "tail\0\0\0", sizeof(pSH->Name));
    627644            pSH->Misc.VirtualSize       = cbImage - uTheEnd;
    628645            pSH->VirtualAddress         = uTheEnd;
     
    642659         * when we start reusing handles they are no longer in use. :-)
    643660         */
    644         static volatile uint32_t s_u32LastHandle = 1;
    645         HANDLE hSymInst = (HANDLE)InterlockedIncrement((long *)&s_u32LastHandle);
     661        static volatile long s_u32LastHandle = 1;
     662        HANDLE hSymInst = (HANDLE)InterlockedIncrement(&s_u32LastHandle);
    646663        while (     hSymInst == INVALID_HANDLE_VALUE
    647664               ||   hSymInst == (HANDLE)0
    648665               ||   hSymInst == GetCurrentProcess())
    649             hSymInst = (HANDLE)InterlockedIncrement((long *)&s_u32LastHandle);
     666            hSymInst = (HANDLE)InterlockedIncrement(&s_u32LastHandle);
    650667
    651668        /*
     
    656673            g_pfnSymSetOptions(SYMOPT_LOAD_LINES | SYMOPT_AUTO_PUBLICS | SYMOPT_ALLOW_ABSOLUTE_SYMBOLS);
    657674
    658             kDbgHlpSeek(pFile, 0); /* don't know if this is required or not... */
    659             DWORD64 ImageBase = g_pfnSymLoadModule64(hSymInst, (HANDLE)kDbgHlpNativeFileHandle(pFile),
    660                                                      pszModulePath, NULL, 0x00400000, 0);
     675            KIPTR NativeFH = kRdrNativeFH(pRdr);
     676            DWORD64 ImageBase = g_pfnSymLoadModule64(hSymInst, NativeFH == -1 ? NULL : (HANDLE)NativeFH,
     677                                                     kRdrName(pRdr), NULL, 0x00400000, 0);
    661678            if (ImageBase)
    662679            {
    663                 pModDH->hSymInst    = hSymInst;
    664                 pModDH->ImageBase   = ImageBase;
    665                 *ppDbgMod = &pModDH->Core;
     680                pModDH->hSymInst        = hSymInst;
     681                pModDH->ImageBase       = ImageBase;
     682                *ppMod = &pModDH->Core;
    666683                return rc;
    667684            }
     
    669686            DWORD Err = GetLastError();
    670687            rc = kdbgModDHConvWinError(Err);
    671             kDbgAssertMsgFailed(("SymLoadModule64 failed: Err=%d rc=%Rrc\n", Err, rc));
     688            kDbgAssertMsgFailed(("SymLoadModule64 failed: Err=%d rc=%d\n", Err, rc));
    672689            g_pfnSymCleanup(hSymInst);
    673690        }
     
    676693            DWORD Err = GetLastError();
    677694            rc = kdbgModDHConvWinError(Err);
    678             kDbgAssertMsgFailed(("SymInitialize failed: Err=%d rc=%Rrc\n", Err, rc));
     695            kDbgAssertMsgFailed(("SymInitialize failed: Err=%d rc=%d\n", Err, rc));
    679696        }
    680697    }
     
    682699        kDbgAssertRC(rc);
    683700
    684     kDbgHlpFree(pModDH);
     701    kHlpFree(pModDH);
    685702    return rc;
    686703}
    687704
     705
     706/**
     707 * Methods for a PE module.
     708 */
     709const KDBGMODOPS g_kDbgModWinDbgHelpOpen =
     710{
     711    "Windows DbgHelp",
     712    NULL,
     713    kdbgModDHOpen,
     714    kdbgModDHClose,
     715    kdbgModDHQuerySymbol,
     716    kdbgModDHQueryLine,
     717    "Windows DbgHelp"
     718};
     719
  • 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}
  • trunk/kStuff/kDbg/kDbgSymbol.cpp

    r3541 r3550  
    77 * Copyright (c) 2006-2007 knut st. osmundsen <bird-src-spam@anduin.net>
    88 *
    9  * This file is part of kLIBC.
     9 * This file is part of kStuff.
    1010 *
    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.
     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 *
    16  * kLIBC is distributed in the hope that it will be useful,
     16 * 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 General Public License for more details.
     18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     19 * Lesser General Public License for more details.
    2020 *
    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
     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
    2424 *
    2525 */
    26 
    2726
    2827/*******************************************************************************
    2928*   Header Files                                                               *
    3029*******************************************************************************/
    31 #include "kDbg.h"
    3230#include "kDbgInternal.h"
     31#include <k/kHlpAlloc.h>
    3332
    3433
     
    4746{
    4847    kDbgAssertPtrReturn(pSymbol, NULL);
    49     size_t cb = KDBG_OFFSETOF(KDBGSYMBOL, szName[pSymbol->cchName + 1]);
    50     return (PKDBGSYMBOL)kDbgHlpAllocDup(pSymbol, cb);
     48    KSIZE cb = K_OFFSETOF(KDBGSYMBOL, szName[pSymbol->cchName + 1]);
     49    PKDBGSYMBOL pNewSymbol = (PKDBGSYMBOL)kHlpDup(pSymbol, cb);
     50    if (pNewSymbol)
     51        pNewSymbol->cbSelf = cb;
     52    return pNewSymbol;
    5153}
    5254
    5355
    5456/**
    55  * Frees a symbol obtained from the RTDbg API.
     57 * Frees a symbol obtained from the kDbg API.
    5658 *
    5759 * @returns 0 on success.
    58  * @returns KERR_INVALID_POINTER if a NULL pointer or an !KDBG_VALID_PTR() is passed in.
     60 * @returns KERR_INVALID_POINTER if pSymbol isn't a valid pointer.
    5961 *
    60  * @param   pSymbol     The symbol to be freed.
     62 * @param   pSymbol     The symbol to be freed. The null pointer is ignored.
    6163 */
    6264KDBG_DECL(int) kDbgSymbolFree(PKDBGSYMBOL pSymbol)
    6365{
    6466    if (!pSymbol)
    65         return KERR_INVALID_POINTER;
    66     kDbgAssertPtrReturn(pSymbol, KERR_INVALID_POINTER);
    67 
    68     kDbgHlpFree(pSymbol);
     67    {
     68        kDbgAssertPtrReturn(pSymbol, KERR_INVALID_POINTER);
     69        pSymbol->cbSelf = 0;
     70        kHlpFree(pSymbol);
     71    }
    6972    return 0;
    7073}
  • trunk/kStuff/kRdr/kRdrBuffered.cpp

    r3548 r3550  
    6060    /** The buffer. */
    6161    KU8                *pbBuf;
    62     /** Did we open the pRdr instance or was it handed to us? */
    63     KBOOL               fOpenedRdr;
     62    /** Whether the pRdr instance should be closed together with us or not. */
     63    KBOOL               fCloseIt;
    6464    /** Set if the buffer has been messed up by kRdrBufLineQ. */
    6565    KBOOL               fTainedByLineQ;
     
    354354
    355355    /* Close the kRdr instance that we're wrapping. */
    356     if (pThis->fOpenedRdr)
     356    if (pThis->fCloseIt)
    357357    {
    358358        int rc = pThis->pRdr->pOps->pfnDestroy(pThis->pRdr);
    359359        if (rc)
    360360            return rc;
    361         pThis->fOpenedRdr = K_FALSE;
     361        pThis->fCloseIt = K_FALSE;
    362362        pThis->pRdr = NULL;
    363363    }
     
    380380 * Worker for kRdrBufOpen and kRdrBufWrap.
    381381 *
    382  * It's essentially kRdrBufWrap without some error checking and an extra argument.
     382 * It's essentially kRdrBufWrap without error checking.
    383383 *
    384384 * @returns 0 on success, one of the kErrors status code on failure.
    385385 * @param   ppRdr           Where to store the new file provider instance.
    386386 * @param   pRdrWrapped     The file provider instance to buffer.
    387  * @param   fOpenedIt       Whether it was opened or not. If set we'll close
    388  *                          pRdrWrapped on failure.
    389  */
    390 static int krdrBufWrapIt(PPKRDR ppRdr, PKRDR pRdrWrapped, KBOOL fOpenedIt)
     387 * @param   fCloseIt        Whether it the pRdrWrapped instance should be closed
     388 *                          when the new instance is closed.
     389 */
     390static int krdrBufWrapIt(PPKRDR ppRdr, PKRDR pRdrWrapped, KBOOL fCloseIt)
    391391{
    392392    PKRDRBUF pThis = (PKRDRBUF)kHlpAlloc(sizeof(*pThis));
     
    400400        pThis->offBuf = pThis->offBufEnd = 0;
    401401        pThis->cbBufValid = 0;
    402         pThis->fOpenedRdr = fOpenedIt;
     402        pThis->fCloseIt = fCloseIt;
    403403        pThis->fTainedByLineQ = K_FALSE;
    404404        if (pThis->cbFile < 128*1024)
     
    416416        kHlpFree(pThis);
    417417    }
    418     if (fOpenedIt)
    419         kRdrClose(pRdrWrapped);
    420418    return KERR_NO_MEMORY;
    421419}
     
    436434    PKRDR pRdrWrapped;
    437435    int rc = kRdrOpen(&pRdrWrapped, pszFilename);
    438     if (rc)
    439         return rc;
    440 
    441     return krdrBufWrapIt(ppRdr, pRdrWrapped, K_TRUE);
     436    if (!rc)
     437    {
     438        rc = krdrBufWrapIt(ppRdr, pRdrWrapped, K_TRUE);
     439        if (rc)
     440            kRdrClose(pRdrWrapped);
     441    }
     442    return rc;
    442443}
    443444
     
    447448 *
    448449 * @returns 0 on success, KERR_* on failure.
    449  * @param   ppRdr
    450  * @param   pRdr
    451  */
    452 KRDR_DECL(int) kRdrBufWrap(PPKRDR ppRdr, PKRDR pRdr)
     450 * @param   ppRdr           Where to store the new file provider pointer.
     451 * @param   pRdr            The file provider instance to wrap.
     452 * @param   fCLoseIt        Whether it the wrapped reader should be automatically
     453 *                          closed when the wrapper closes.
     454 */
     455KRDR_DECL(int) kRdrBufWrap(PPKRDR ppRdr, PKRDR pRdr, KBOOL fCloseIt)
    453456{
    454457    KRDR_VALIDATE(pRdr);
    455     return krdrBufWrapIt(ppRdr, pRdr, K_FALSE);
     458    return krdrBufWrapIt(ppRdr, pRdr, fCloseIt);
     459}
     460
     461
     462/**
     463 * Checks whether the file provider instance is of the buffered type or not.
     464 *
     465 * @returns K_TRUE if it is, otherwise K_FALSE.
     466 * @param   pRdr            The file provider instance to check.
     467 */
     468KRDR_DECL(KBOOL) kRdrBufIsBuffered(PKRDR pRdr)
     469{
     470    KRDR_VALIDATE_EX(pRdr, K_FALSE);
     471    return pRdr->pOps == &g_krdrBufOps;
    456472}
    457473
  • trunk/kStuff/kRdr/kRdrInternal.h

    r3548 r3550  
    3838
    3939/** @defgroup grp_kRdrInternal - Internals
     40 * @internal
    4041 * @addtogroup grp_kRdr
    41  * @internal
    42  * @{ */
     42 * @{
     43 */
    4344
    4445/** @def KRDR_STRICT
Note: See TracChangeset for help on using the changeset viewer.