dbgmod.cpp revision c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3
/* $Id$ */
/** @file
* IPRT - Debug Module Interpreter.
*/
/*
* Copyright (C) 2009 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/initterm.h>
#include <iprt/semaphore.h>
#include <iprt/strcache.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/** Debug info interpreter regisration record. */
typedef struct RTDBGMODREGDBG
{
/** Pointer to the next record. */
struct RTDBGMODREGDBG *pNext;
/** Pointer to the virtual function table for the interpreter. */
/** Usage counter. */
typedef RTDBGMODREGDBG *PRTDBGMODREGDBG;
/** Image interpreter regisration record. */
typedef struct RTDBGMODREGIMG
{
/** Pointer to the next record. */
struct RTDBGMODREGIMG *pNext;
/** Pointer to the virtual function table for the interpreter. */
/** Usage counter. */
typedef RTDBGMODREGIMG *PRTDBGMODREGIMG;
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** Validates a debug module handle and returns rc if not valid. */
do { \
} while (0)
/** Locks the debug module. */
#define RTDBGMOD_LOCK(pDbgMod) \
do { \
} while (0)
/** Unlocks the debug module. */
#define RTDBGMOD_UNLOCK(pDbgMod) \
do { \
} while (0)
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** Init once object for lazy registration of the built-in image and debug
* info interpreters. */
/** List of registered image interpreters. */
static RTDBGMODREGIMG g_pImgHead;
/** List of registered debug infor interpreters. */
static RTDBGMODREGDBG g_pDbgHead;
/** String cache for the debug info interpreters.
* RTSTRCACHE is thread safe. */
/**
* Cleanup debug info interpreter globals.
*
* @param enmReason The cause of the termination.
* @param iStatus The meaning of this depends on enmReason.
* @param pvUser User argument, unused.
*/
static DECLCALLBACK(void) rtDbgModTermCallback(RTTERMREASON enmReason, int32_t iStatus, void *pvUser)
{
if (enmReason == RTTERMREASON_UNLOAD)
{
/** @todo deregister interpreters. */
}
}
/**
* the built-in interpreters.
*
* @returns IPRT status code.
* @param pvUser1 NULL.
* @param pvUser2 NULL.
*/
{
/*
* Create the semaphore and string cache.
*/
if (RT_SUCCESS(rc))
{
/*
* Register the interpreters.
*/
/** @todo */
if (RT_SUCCESS(rc))
{
/*
* Finally, register the IPRT cleanup callback.
*/
if (RT_SUCCESS(rc))
return VINF_SUCCESS;
}
}
return rc;
}
DECLINLINE(int) rtDbgModLazyInit(void)
{
}
/**
* Creates a module based on the default debug info container.
*
* This can be used to manually load a module and its symbol. The primary user
* group is the debug info interpreters, which use this API to create an
* efficient debug info container behind the scenes and forward all queries to
* it once the info has been loaded.
*
* @returns IPRT status code.
*
* @param phDbgMod Where to return the module handle.
* @param pszName The name of the module (mandatory).
* @param cbSeg The size of initial segment. If zero, segments will
* have to be added manually using RTDbgModSegmentAdd.
* @param fFlags Flags reserved for future extensions, MBZ for now.
*/
RTDECL(int) RTDbgModCreate(PRTDBGMOD phDbgMod, const char *pszName, RTUINTPTR cbSeg, uint32_t fFlags)
{
/*
* Input validation and lazy initialization.
*/
*phDbgMod = NIL_RTDBGMOD;
int rc = rtDbgModLazyInit();
if (RT_FAILURE(rc))
return rc;
/*
* Allocate a new module instance.
*/
if (!pDbgMod)
return VERR_NO_MEMORY;
if (RT_SUCCESS(rc))
{
{
if (RT_SUCCESS(rc))
{
return rc;
}
}
}
return rc;
}
RTDECL(int) RTDbgModCreateDeferred(PRTDBGMOD phDbgMod, const char *pszFilename, const char *pszName, RTUINTPTR cb, uint32_t fFlags)
{
return VERR_NOT_IMPLEMENTED;
}
RTDECL(int) RTDbgModCreateFromImage(PRTDBGMOD phDbgMod, const char *pszFilename, const char *pszName, uint32_t fFlags)
{
return VERR_NOT_IMPLEMENTED;
}
RTDECL(int) RTDbgModCreateFromMap(PRTDBGMOD phDbgMod, const char *pszFilename, const char *pszName, RTUINTPTR uSubtrahend, uint32_t fFlags)
{
return VERR_NOT_IMPLEMENTED;
}
/**
* Destroys an module after the reference count has reached zero.
*
* @param pDbgMod The module instance.
*/
{
/*
* Close the debug info interpreter first, then the image interpret.
*/
{
}
{
}
/*
* Free the resources.
*/
}
/**
* Retains another reference to the module.
*
* @returns New reference count, UINT32_MAX on invalid handle (asserted).
*
* @param hDbgMod The module handle.
*
* @remarks Will not take any locks.
*/
{
}
/**
* Release a reference to the module.
*
* When the reference count reaches zero, the module is destroyed.
*
* @returns New reference count, UINT32_MAX on invalid handle (asserted).
*
* @param hDbgMod The module handle. The NIL handle is quietly ignored
* and 0 is returned.
*
* @remarks Will not take any locks.
*/
{
if (hDbgMod == NIL_RTDBGMOD)
return 0;
if (!cRefs)
return cRefs;
}
/**
* Gets the module name.
*
* @returns Pointer to a read only string containing the name.
*
* @param hDbgMod The module handle.
*/
{
}
/**
* Converts an image relative address to a segment:offset address.
*
* @returns Segment index on success.
* NIL_RTDBGSEGIDX is returned if the module handle or the RVA are
* invalid.
*
* @param hDbgMod The module handle.
* @param uRva The image relative address to convert.
* @param poffSeg Where to return the segment offset. Optional.
*/
{
return iSeg;
}
/**
* Image size when mapped if segments are mapped adjecently.
*
* For ELF, PE, and Mach-O images this is (usually) a natural query, for LX and
* NE and such it's a bit odder and the answer may not make much sense for them.
*
* @returns Image mapped size.
* UINTPTR_MAX is returned if the handle is invalid.
*
* @param hDbgMod The module handle.
*/
{
return cbImage;
}
/**
* Adds a segment to the module. Optional feature.
*
* This method is intended used for manually constructing debug info for a
* module. The main usage is from other debug info interpreters that want to
* avoid writing a debug info database and instead uses the standard container
* behind the scenes.
*
* @returns IPRT status code.
* @retval VERR_NOT_SUPPORTED if this feature isn't support by the debug info
* interpreter. This is a common return code.
* @retval VERR_INVALID_HANDLE if hDbgMod is invalid.
* @retval VERR_DBG_ADDRESS_WRAP if uRva+cb wraps around.
* @retval VERR_DBG_SEGMENT_NAME_OUT_OF_RANGE if pszName is too short or long.
* @retval VERR_INVALID_PARAMETER if fFlags contains undefined flags.
* @retval VERR_DBG_SPECIAL_SEGMENT if *piSeg is a special segment.
* @retval VERR_DBG_INVALID_SEGMENT_INDEX if *piSeg doesn't meet expectations.
*
* @param hDbgMod The module handle.
* @param uRva The image relative address of the segment.
* @param cb The size of the segment.
* @param pszName The segment name. Does not normally need to be
* unique, although this is somewhat up to the
* debug interpreter to decide.
* @param fFlags Segment flags. Reserved for future used, MBZ.
* @param piSeg The segment index or NIL_RTDBGSEGIDX on input.
* The assigned segment index on successful return.
* Optional.
*/
{
/*
* Validate input.
*/
AssertMsgReturn(!piSeg || *piSeg == NIL_RTDBGSEGIDX || *piSeg <= RTDBGSEGIDX_LAST, ("%#x\n", *piSeg), VERR_DBG_SPECIAL_SEGMENT);
/*
* Do the deed.
*/
return rc;
}
/**
* Gets the number of segments in the module.
*
* This is can be used to determin the range which can be passed to
* RTDbgModSegmentByIndex and derivates.
*
* @returns The segment relative address.
* NIL_RTDBGSEGIDX if the handle is invalid.
*
* @param hDbgMod The module handle.
*/
{
return cSegs;
}
/**
* Query information about a segment.
*
* This can be used together with RTDbgModSegmentCount to enumerate segments.
* The index starts a 0 and stops one below RTDbgModSegmentCount.
*
* @returns IPRT status code.
* @retval VERR_DBG_INVALID_SEGMENT_INDEX if iSeg is too high.
* @retval VERR_DBG_SPECIAL_SEGMENT if iSeg indicates a special segment.
* @retval VERR_INVALID_HANDLE if hDbgMod is invalid.
*
* @param hDbgMod The module handle.
* @param iSeg The segment index. No special segments.
* @param pSegInfo Where to return the segment info. The
* RTDBGSEGMENT::Address member will be set to
* RTUINTPTR_MAX or the load address used at link time.
*/
{
return RT_SUCCESS(rc);
}
/**
* Gets the size of a segment.
*
* This is a just a wrapper around RTDbgModSegmentByIndex.
*
* @returns The segment size.
* UINTPTR_MAX is returned if either the handle and segment index are
* invalid.
*
* @param hDbgMod The module handle.
* @param iSeg The segment index. RTDBGSEGIDX_ABS is not allowed.
* If RTDBGSEGIDX_RVA is used, the functions returns
* the same value as RTDbgModImageSize.
*/
{
if (iSeg == RTDBGSEGIDX_RVA)
return RTDbgModImageSize(hDbgMod);
}
/**
* Gets the image relative address of a segment.
*
* This is a just a wrapper around RTDbgModSegmentByIndex.
*
* @returns The segment relative address.
* UINTPTR_MAX is returned if either the handle and segment index are
* invalid.
*
* @param hDbgMod The module handle.
* @param iSeg The segment index. No special segment indexes
* allowed (asserted).
*/
{
}
/**
* Adds a line number to the module.
*
* @returns IPRT status code.
* @retval VERR_INVALID_HANDLE if hDbgMod is invalid.
* @retval VERR_NOT_SUPPORTED if the module interpret doesn't support adding
* custom symbols.
* @retval VERR_DBG_SYMBOL_NAME_OUT_OF_RANGE
* @retval VERR_DBG_INVALID_RVA
* @retval VERR_DBG_INVALID_SEGMENT_INDEX
* @retval VERR_DBG_INVALID_SEGMENT_OFFSET
* @retval VERR_INVALID_PARAMETER
*
* @param hDbgMod The module handle.
* @param pszSymbol The symbol name.
* @param iSeg The segment index.
* @param off The segment offset.
* @param cb The size of the symbol.
* @param fFlags Symbol flags.
* @param piOrdinal Where to return the symbol ordinal on success. If
* the interpreter doesn't do ordinals, this will be set to
* UINT32_MAX. Optional
*/
RTDECL(int) RTDbgModSymbolAdd(RTDBGMOD hDbgMod, const char *pszSymbol, RTDBGSEGIDX iSeg, RTUINTPTR off, RTUINTPTR cb, uint32_t fFlags, uint32_t *piOrdinal)
{
/*
* Validate input.
*/
|| ( iSeg >= RTDBGSEGIDX_SPECIAL_FIRST
&& iSeg <= RTDBGSEGIDX_SPECIAL_LAST),
("%#x\n", iSeg),
/*
* Convert RVAs.
*/
if (iSeg == RTDBGSEGIDX_RVA)
{
if (iSeg == NIL_RTDBGSEGIDX)
{
return VERR_DBG_INVALID_RVA;
}
}
/*
* Get down to business.
*/
int rc = pDbgMod->pDbgVt->pfnSymbolAdd(pDbgMod, pszSymbol, cchSymbol, iSeg, off, cb, fFlags, piOrdinal);
return rc;
}
{
return 1;
}
{
return VERR_NOT_IMPLEMENTED;
}
RTDECL(int) RTDbgModSymbolByAddr(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTINTPTR poffDisp, PRTDBGSYMBOL pSymbol)
{
return VERR_NOT_IMPLEMENTED;
}
RTDECL(int) RTDbgModSymbolByAddrA(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTINTPTR poffDisp, PRTDBGSYMBOL *ppSymbol)
{
return VERR_NOT_IMPLEMENTED;
}
{
return VERR_NOT_IMPLEMENTED;
}
{
return VERR_NOT_IMPLEMENTED;
}
/**
* Adds a line number to the module.
*
* @returns IPRT status code.
* @retval VERR_INVALID_HANDLE if hDbgMod is invalid.
* @retval VERR_NOT_SUPPORTED if the module interpret doesn't support adding
* custom symbols.
* @retval VERR_DBG_FILE_NAME_OUT_OF_RANGE
* @retval VERR_DBG_INVALID_RVA
* @retval VERR_DBG_INVALID_SEGMENT_INDEX
* @retval VERR_DBG_INVALID_SEGMENT_OFFSET
* @retval VERR_INVALID_PARAMETER
*
* @param hDbgMod The module handle.
* @param pszFile The file name.
* @param uLineNo The line number.
* @param iSeg The segment index.
* @param off The segment offset.
* @param piOrdinal Where to return the line number ordinal on success.
* If the interpreter doesn't do ordinals, this will be
* set to UINT32_MAX. Optional.
*/
RTDECL(int) RTDbgModLineAdd(RTDBGMOD hDbgMod, const char *pszFile, uint32_t uLineNo, RTDBGSEGIDX iSeg, RTUINTPTR off, uint32_t *piOrdinal)
{
/*
* Validate input.
*/
|| iSeg == RTDBGSEGIDX_RVA,
("%#x\n", iSeg),
/*
* Convert RVAs.
*/
if (iSeg == RTDBGSEGIDX_RVA)
{
if (iSeg == NIL_RTDBGSEGIDX)
{
return VERR_DBG_INVALID_RVA;
}
}
/*
* Get down to business.
*/
return rc;
}
RTDECL(int) RTDbgModLineByAddr(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTINTPTR poffDisp, PRTDBGLINE pLine)
{
return VERR_NOT_IMPLEMENTED;
}
RTDECL(int) RTDbgModLineByAddrA(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTINTPTR poffDisp, PRTDBGLINE *ppLine)
{
return VERR_NOT_IMPLEMENTED;
}