dbgmod.cpp revision d90eec53c9dcaa0f3d1054e8734ed46875b9093a
/* $Id$ */
/** @file
* IPRT - Debug Module Interpreter.
*/
/*
* Copyright (C) 2009-2012 Oracle Corporation
*
* 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP RTLOGGROUP_DBG
#include <iprt/initterm.h>
#include <iprt/semaphore.h>
#include <iprt/strcache.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/** Debug info interpreter registration 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 registration 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 PRTDBGMODREGIMG g_pImgHead;
/** List of registered debug infor interpreters. */
static PRTDBGMODREGDBG 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)
{
g_pDbgHead = NULL;
while (pDbg)
{
}
g_pImgHead = NULL;
while (pImg)
{
}
}
}
/**
* Internal worker for register a debug interpreter.
*
* Called while owning the write lock or when locking isn't required.
*
* @returns IPRT status code.
* @retval VERR_NO_MEMORY
* @retval VERR_ALREADY_EXISTS
*
* @param pVt The virtual function table of the debug
* module interpreter.
*/
{
/*
* Search or duplicate registration.
*/
{
return VERR_ALREADY_EXISTS;
return VERR_ALREADY_EXISTS;
}
/*
* Create a new record and add it to the end of the list.
*/
if (!pReg)
return VERR_NO_MEMORY;
if (pPrev)
else
g_pDbgHead = pReg;
return VINF_SUCCESS;
}
/**
* Internal worker for register a image interpreter.
*
* Called while owning the write lock or when locking isn't required.
*
* @returns IPRT status code.
* @retval VERR_NO_MEMORY
* @retval VERR_ALREADY_EXISTS
*
* @param pVt The virtual function table of the image
* interpreter.
*/
{
/*
* Search or duplicate registration.
*/
{
return VERR_ALREADY_EXISTS;
return VERR_ALREADY_EXISTS;
}
/*
* Create a new record and add it to the end of the list.
*/
if (!pReg)
return VERR_NO_MEMORY;
if (pPrev)
else
g_pImgHead = pReg;
return VINF_SUCCESS;
}
/**
* the built-in interpreters.
*
* @returns IPRT status code.
* @param pvUser NULL.
*/
{
/*
* Create the semaphore and string cache.
*/
if (RT_SUCCESS(rc))
{
/*
* Register the interpreters.
*/
if (RT_SUCCESS(rc))
#ifdef RT_OS_WINDOWS
if (RT_SUCCESS(rc))
#endif
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
{
/*
* Finally, register the IPRT cleanup callback.
*/
if (RT_SUCCESS(rc))
return VINF_SUCCESS;
/* bail out: use the termination callback. */
}
}
else
return rc;
}
/**
* Performs lazy init of our global variables.
* @returns IPRT status code.
*/
DECLINLINE(int) rtDbgModLazyInit(void)
{
}
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))
{
pDbgMod->pszName = RTStrCacheEnterLower(g_hDbgModStrCache, RTPathFilenameEx(pszName, RTPATH_STR_F_STYLE_DOS));
{
if (RT_SUCCESS(rc))
{
return rc;
}
}
}
return rc;
}
RTDECL(int) RTDbgModCreateFromImage(PRTDBGMOD phDbgMod, const char *pszFilename, const char *pszName, RTDBGCFG hDbgCfg)
{
/*
* Input validation and lazy initialization.
*/
*phDbgMod = NIL_RTDBGMOD;
int rc = rtDbgModLazyInit();
if (RT_FAILURE(rc))
return rc;
if (!pszName)
/*
* Allocate a new module instance.
*/
if (!pDbgMod)
return VERR_NO_MEMORY;
if (RT_SUCCESS(rc))
{
{
if (pDbgMod->pszImgFile)
{
/*
* Find an image reader which groks the file.
*/
if (RT_SUCCESS(rc))
{
{
if (RT_SUCCESS(rc))
{
/*
* Find a debug info interpreter.
*/
{
if (RT_SUCCESS(rc))
{
/*
* That's it!
*/
return rc;
}
}
/*
* Image detected, but found no debug info we were
* able to understand.
*/
/** @todo Fall back on exported symbols! */
break;
}
}
/*
* Could it be a file containing raw debug info?
*/
if (!pImg)
{
{
if (RT_SUCCESS(rc))
{
/*
* That's it!
*/
return rc;
}
}
}
/* bail out */
}
}
else
}
else
}
return rc;
}
{
/*
* Input validation and lazy initialization.
*/
*phDbgMod = NIL_RTDBGMOD;
int rc = rtDbgModLazyInit();
if (RT_FAILURE(rc))
return rc;
if (!pszName)
/*
* Allocate a new module instance.
*/
if (!pDbgMod)
return VERR_NO_MEMORY;
if (RT_SUCCESS(rc))
{
{
if (pDbgMod->pszDbgFile)
{
/*
* Try the map file readers.
*/
if (RT_SUCCESS(rc))
{
{
{
if (RT_SUCCESS(rc))
{
return rc;
}
}
}
/* bail out */
}
}
else
}
else
}
return rc;
}
/*
*
* P E I M A G E
* P E I M A G E
* P E I M A G E
*
*/
/**
* Opens debug information for an image.
*
* @returns IPRT status code
* @param pDbgMod The debug module structure.
*
* @note This will generally not look for debug info stored in external
* files. rtDbgModFromPeImageExtDbgInfoCallback can help with that.
*/
{
if (RT_SUCCESS(rc))
{
{
if (RT_SUCCESS(rc))
{
/*
* That's it!
*/
return VINF_SUCCESS;
}
}
}
return VERR_DBG_NO_MATCHING_INTERPRETER;
}
/** @callback_method_impl{FNRTDBGCFGOPEN} */
static DECLCALLBACK(int) rtDbgModExtDbgInfoOpenCallback(RTDBGCFG hDbgCfg, const char *pszFilename, void *pvUser1, void *pvUser2)
{
/*
* Set the debug file name and try possible interpreters.
*/
if (RT_SUCCESS(rc))
{
{
if (RT_SUCCESS(rc))
{
/*
* Got it!
*/
return VINF_CALLBACK_RETURN;
}
}
}
/* No joy. */
return rc;
}
/**
* Argument package used by rtDbgModOpenDebugInfoExternalToImage.
*/
typedef struct RTDBGMODOPENDIETI
{
/** @callback_method_impl{FNRTLDRENUMDBG} */
static DECLCALLBACK(int)
rtDbgModOpenDebugInfoExternalToImageCallback(RTLDRMOD hLdrMod, PCRTLDRDBGINFO pDbgInfo, void *pvUser)
{
if (!pszExtFile)
{
/*
* If a external debug type comes without a file name, calculate a
* likely debug filename for it. (Hack for NT4 drivers.)
*/
pszExt = ".dbg";
pszExt = ".pdb";
{
if (psz)
{
}
}
if (!pszExtFile)
{
return VINF_SUCCESS;
}
}
/*
* Switch on type and call the appropriate search function.
*/
int rc;
{
break;
break;
break;
break;
default:
Log(("rtDbgModOpenDebugInfoExternalToImageCallback: Don't know how to handle enmType=%d and pszFileExt=%s\n",
return VERR_DBG_TODO;
}
if (RT_SUCCESS(rc))
{
LogFlow(("RTDbgMod: Successfully opened external debug info '%s' for '%s'\n",
return VINF_CALLBACK_RETURN;
}
Log(("rtDbgModOpenDebugInfoExternalToImageCallback: '%s' (enmType=%d) for '%s' -> %Rrc\n",
return rc;
}
/**
* Opens debug info listed in the image that is stored in a separate file.
*
* @returns IPRT status code
* @param pDbgMod The debug module.
* @param hDbgCfg The debug config. Can be NIL.
*/
{
int rc = pDbgMod->pImgVt->pfnEnumDbgInfo(pDbgMod, rtDbgModOpenDebugInfoExternalToImageCallback, &Args);
return VINF_SUCCESS;
return VERR_NOT_FOUND;
}
/** @callback_method_impl{FNRTDBGCFGOPEN} */
static DECLCALLBACK(int) rtDbgModFromPeImageOpenCallback(RTDBGCFG hDbgCfg, const char *pszFilename, void *pvUser1, void *pvUser2)
{
/*
* Replace the image file name while probing it.
*/
if (!pszNewImgFile)
return VERR_NO_STR_MEMORY;
/*
* Find an image reader which groks the file.
*/
if (RT_SUCCESS(rc))
{
{
if (RT_SUCCESS(rc))
break;
}
if (RT_SUCCESS(rc))
{
/*
* Check the deferred info.
*/
{
uint32_t uTimestamp = pDeferred->u.PeImage.uTimestamp; /** @todo add method for getting the timestamp. */
{
/*
* We found the executable image we need, now go find any
* debug info associated with it. For PE images, this is
* generally found in an external file, so we do a sweep
* for that first.
*
* Then try open debug inside the module, and finally
* falling back on exports.
*/
if (RT_FAILURE(rc))
if (RT_FAILURE(rc))
if (RT_SUCCESS(rc))
{
return VINF_CALLBACK_RETURN;
}
/* Something bad happened, just give up. */
}
else
{
LogFlow(("rtDbgModFromPeImageOpenCallback: uTimestamp mismatch (found %#x, expected %#x) - %s\n",
}
}
else
{
LogFlow(("rtDbgModFromPeImageOpenCallback: cbImage mismatch (found %#x, expected %#x) - %s\n",
}
}
else
}
/* Restore image name. */
return rc;
}
/** @callback_method_impl{FNRTDBGMODDEFERRED} */
static DECLCALLBACK(int) rtDbgModFromPeImageDeferredCallback(PRTDBGMODINT pDbgMod, PRTDBGMODDEFERRED pDeferred)
{
int rc;
else
{
if (RT_FAILURE(rc))
if (RT_FAILURE(rc))
}
return rc;
}
RTDECL(int) RTDbgModCreateFromPeImage(PRTDBGMOD phDbgMod, const char *pszFilename, const char *pszName, RTLDRMOD hLdrMod,
{
/*
* Input validation and lazy initialization.
*/
*phDbgMod = NIL_RTDBGMOD;
if (!pszName)
int rc = rtDbgModLazyInit();
if (RT_FAILURE(rc))
return rc;
if (hDbgCfg)
{
}
/*
* Allocate a new module instance.
*/
if (!pDbgMod)
return VERR_NO_MEMORY;
if (RT_SUCCESS(rc))
{
{
if (pDbgMod->pszImgFile)
{
/*
* If we have a loader module, we must instantiate the loader
* side of things regardless of the deferred setting.
*/
if (hLdrMod != NIL_RTLDRMOD)
{
if (!cbImage)
}
if (RT_SUCCESS(rc))
{
/*
* Do it now or procrastinate?
*/
{
}
else
{
rc = rtDbgModDeferredCreate(pDbgMod, rtDbgModFromPeImageDeferredCallback, cbImage, hDbgCfg, &pDeferred);
if (RT_SUCCESS(rc))
}
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
/* Failed, bail out. */
if (hLdrMod != NIL_RTLDRMOD)
{
}
}
}
else
}
else
}
return rc;
}
/**
* 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.
*/
}
{
}
{
if (hDbgMod == NIL_RTDBGMOD)
return 0;
if (!cRefs)
return cRefs;
}
{
}
{
return NULL;
return pDbgMod->pszDbgFile;
}
{
return pDbgMod->pszImgFileSpecified;
}
{
}
{
RTDBGMOD_VALID_RETURN_RC(pDbgMod, false);
}
{
RTDBGMOD_VALID_RETURN_RC(pDbgMod, false);
}
{
return iSeg;
}
{
return cbImage;
}
{
}
{
return VINF_SUCCESS;
}
{
/*
* Validate input.
*/
AssertMsgReturn(!piSeg || *piSeg == NIL_RTDBGSEGIDX || *piSeg <= RTDBGSEGIDX_LAST, ("%#x\n", *piSeg), VERR_DBG_SPECIAL_SEGMENT);
/*
* Do the deed.
*/
return rc;
}
{
return cSegs;
}
{
return rc;
}
{
if (iSeg == RTDBGSEGIDX_RVA)
return RTDbgModImageSize(hDbgMod);
}
{
}
RTDECL(int) RTDbgModSymbolAdd(RTDBGMOD hDbgMod, const char *pszSymbol, RTDBGSEGIDX iSeg, RTUINTPTR off,
{
/*
* 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 cSymbols;
}
{
return rc;
}
{
if (!pSymInfo)
return VERR_NO_MEMORY;
if (RT_SUCCESS(rc))
else
return rc;
}
RTDECL(int) RTDbgModSymbolByAddr(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, uint32_t fFlags,
{
/*
* Validate input.
*/
/*
* Convert RVAs.
*/
if (iSeg == RTDBGSEGIDX_RVA)
{
if (iSeg == NIL_RTDBGSEGIDX)
{
return VERR_DBG_INVALID_RVA;
}
}
/*
* Get down to business.
*/
return rc;
}
RTDECL(int) RTDbgModSymbolByAddrA(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, uint32_t fFlags,
{
if (!pSymInfo)
return VERR_NO_MEMORY;
if (RT_SUCCESS(rc))
else
return rc;
}
{
/*
* Validate input.
*/
/*
* Make the query.
*/
return rc;
}
{
if (!pSymInfo)
return VERR_NO_MEMORY;
if (RT_SUCCESS(rc))
else
return rc;
}
{
/*
* 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;
}
{
return cLineNumbers;
}
{
return rc;
}
{
*ppLineInfo = NULL;
if (!pLineInfo)
return VERR_NO_MEMORY;
if (RT_SUCCESS(rc))
*ppLineInfo = pLineInfo;
else
return rc;
}
RTDECL(int) RTDbgModLineByAddr(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTINTPTR poffDisp, PRTDBGLINE pLineInfo)
{
/*
* Validate input.
*/
/*
* Convert RVAs.
*/
if (iSeg == RTDBGSEGIDX_RVA)
{
if (iSeg == NIL_RTDBGSEGIDX)
{
return VERR_DBG_INVALID_RVA;
}
}
return rc;
}
RTDECL(int) RTDbgModLineByAddrA(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTINTPTR poffDisp, PRTDBGLINE *ppLineInfo)
{
*ppLineInfo = NULL;
if (!pLineInfo)
return VERR_NO_MEMORY;
if (RT_SUCCESS(rc))
*ppLineInfo = pLineInfo;
else
return rc;
}