DBGFAddrSpace.cpp revision 1c984c0cd0dd72c793c20d4878ebea9f6b5bb3fa
/* $Id$ */
/** @file
* DBGF - Debugger Facility, Address Space Management.
*/
/*
* Copyright (C) 2008-2013 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.
*/
/** @page pg_dbgf_addr_space DBGFAddrSpace - Address Space Management
*
* What's an address space? It's mainly a convenient way of stuffing
* module segments and ad-hoc symbols together. It will also help out
* when the debugger gets extended to deal with user processes later.
*
* There are two standard address spaces that will always be present:
* - The physical address space.
* - The global virtual address space.
*
* Additional address spaces will be added and removed at runtime for
* guest processes. The global virtual address space will be used to
* track the kernel parts of the OS, or at least the bits of the kernel
* that is part of all address spaces (mac os x and 4G/4G patched linux).
*
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DBGF
#ifdef VBOX_WITH_RAW_MODE
#endif
#include "DBGFInternal.h"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Address space database node.
*/
typedef struct DBGFASDBNODE
{
/** The node core for DBGF::AsHandleTree, the key is the address space handle. */
/** The node core for DBGF::AsPidTree, the key is the process id. */
/** The node core for DBGF::AsNameSpace, the string is the address space name. */
} DBGFASDBNODE;
/** Pointer to an address space database node. */
typedef DBGFASDBNODE *PDBGFASDBNODE;
/**
* For dbgfR3AsLoadImageOpenData and dbgfR3AsLoadMapOpenData.
*/
typedef struct DBGFR3ASLOADOPENDATA
{
const char *pszModName;
/**
* Callback for dbgfR3AsSearchPath and dbgfR3AsSearchEnvPath.
*
* @returns VBox status code. If success, then the search is completed.
* @param pszFilename The file name under evaluation.
* @param pvUser The user argument.
*/
/** Pointer to a FNDBGFR3ASSEARCHOPEN. */
typedef FNDBGFR3ASSEARCHOPEN *PFNDBGFR3ASSEARCHOPEN;
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** Locks the address space database for writing. */
#define DBGF_AS_DB_LOCK_WRITE(pUVM) \
do { \
} while (0)
/** Unlocks the address space database after writing. */
#define DBGF_AS_DB_UNLOCK_WRITE(pUVM) \
do { \
} while (0)
/** Locks the address space database for reading. */
#define DBGF_AS_DB_LOCK_READ(pUVM) \
do { \
} while (0)
/** Unlocks the address space database after reading. */
#define DBGF_AS_DB_UNLOCK_READ(pUVM) \
do { \
} while (0)
/**
* Initializes the address space parts of DBGF.
*
* @returns VBox status code.
* @param pUVM The user mode VM handle.
*/
{
/*
* Create the semaphore.
*/
/*
* Create the debugging config instance and set it up, defaulting to
* deferred loading in order to keep things fast.
*/
static struct
{
const char *pszEnvName;
const char *pszCfgName;
} const s_aProps[] =
{
};
for (unsigned i = 0; i < RT_ELEMENTS(s_aProps); i++)
{
if (pszEnvValue)
{
rc = RTDbgCfgChangeString(pUVM->dbgf.s.hDbgCfg, s_aProps[i].enmProp, RTDBGCFGOP_PREPEND, pszEnvValue);
if (RT_FAILURE(rc))
}
char *pszCfgValue;
if (RT_FAILURE(rc))
if (pszCfgValue)
{
rc = RTDbgCfgChangeString(pUVM->dbgf.s.hDbgCfg, s_aProps[i].enmProp, RTDBGCFGOP_PREPEND, pszCfgValue);
if (RT_FAILURE(rc))
}
}
/*
* Prepend the NoArch and VBoxDbgSyms directories to the path.
*/
char szPath[RTPATH_MAX];
#ifdef RT_OS_DARWIN
#else
#endif
/*
* Create the standard address spaces.
*/
return VINF_SUCCESS;
}
/**
* Callback used by dbgfR3AsTerm / RTAvlPVDestroy to release an address space.
*
* @returns 0.
* @param pNode The address space database node.
* @param pvIgnore NULL.
*/
{
/* Don't bother freeing it here as MM will free it soon and MM is much at
it when doing it wholesale instead of piecemeal. */
return 0;
}
/**
* Terminates the address space parts of DBGF.
*
* @param pUVM The user mode VM handle.
*/
{
/*
* Create the semaphore.
*/
/*
* Release all the address spaces.
*/
{
}
}
/**
* Relocates the RC address space.
*
* @param pUVM The user mode VM handle.
* @param offDelta The relocation delta.
*/
{
/*
* We will relocate the raw-mode context modules by offDelta if they have
* been injected into the the DBGF_AS_RC map.
*/
&& offDelta != 0)
{
/* Take a snapshot of the modules as we might have overlapping
addresses between the previous and new mapping. */
{
struct DBGFASRELOCENTRY
{
if (paEntries)
{
/* Snapshot. */
{
else
}
/* Unlink them. */
{
}
/* Link them at the new locations. */
{
}
}
else
}
else
}
}
/**
* Gets the IPRT debugging configuration handle (no refs retained).
*
* @returns Config handle or NIL_RTDBGCFG.
* @param pUVM The user mode VM handle.
*/
{
}
/**
* Adds the address space to the database.
*
* @returns VBox status code.
* @param pUVM The user mode VM handle.
* @param hDbgAs The address space handle. The reference of the caller
* will NOT be consumed.
* @param ProcId The process id or NIL_RTPROCESS.
*/
{
/*
* Input validation.
*/
if (!pszName)
return VERR_INVALID_HANDLE;
if (cRefs == UINT32_MAX)
return VERR_INVALID_HANDLE;
/*
* Allocate a tracking node.
*/
int rc = VERR_NO_MEMORY;
if (pDbNode)
{
{
{
return VINF_SUCCESS;
}
/* bail out */
}
}
return rc;
}
/**
* Delete an address space from the database.
*
* The address space must not be engaged as any of the standard aliases.
*
* @returns VBox status code.
* @retval VERR_SHARING_VIOLATION if in use as an alias.
* @retval VERR_NOT_FOUND if not found in the address space database.
*
* @param pUVM The user mode VM handle.
* @param hDbgAs The address space handle. Aliases are not allowed.
*/
{
/*
* Input validation. Retain the address space so it can be released outside
* the lock as well as validated.
*/
if (hDbgAs == NIL_RTDBGAS)
return VINF_SUCCESS;
if (cRefs == UINT32_MAX)
return VERR_INVALID_HANDLE;
/*
* You cannot delete any of the aliases.
*/
{
return VERR_SHARING_VIOLATION;
}
/*
* Ok, try remove it from the database.
*/
if (!pDbNode)
{
return VERR_NOT_FOUND;
}
/*
* Free the resources.
*/
return VINF_SUCCESS;
}
/**
* Changes an alias to point to a new address space.
*
* Not all the aliases can be changed, currently it's only DBGF_AS_GLOBAL
* and DBGF_AS_KERNEL.
*
* @returns VBox status code.
* @param pUVM The user mode VM handle.
* @param hAlias The alias to change.
* @param hAliasFor The address space hAlias should be an alias for. This
* can be an alias. The caller's reference to this address
* space will NOT be consumed.
*/
{
/*
* Input validation.
*/
if (hRealAliasFor == NIL_RTDBGAS)
return VERR_INVALID_HANDLE;
/*
* Make sure the handle has is already in the database.
*/
int rc = VERR_NOT_FOUND;
{
/*
* Update the alias table and release the current address space.
*/
ASMAtomicXchgHandle(&pUVM->dbgf.s.ahAsAliases[DBGF_AS_ALIAS_2_INDEX(hAlias)], hRealAliasFor, &hAsOld);
rc = VINF_SUCCESS;
}
return rc;
}
/**
* @callback_method_impl{FNPDMR3ENUM}
*/
static DECLCALLBACK(int) dbgfR3AsLazyPopulateR0Callback(PVM pVM, const char *pszFilename, const char *pszName,
{
/* Only ring-0 modules. */
if (enmCtx == PDMLDRCTX_RING_0)
{
int rc = RTDbgModCreateFromImage(&hDbgMod, pszFilename, pszName, RTLDRARCH_HOST, pVM->pUVM->dbgf.s.hDbgCfg);
if (RT_SUCCESS(rc))
{
if (RT_FAILURE(rc))
LogRel(("DBGF: Failed to link module \"%s\" into DBGF_AS_R0 at %RTptr: %Rrc\n",
}
else
LogRel(("DBGF: RTDbgModCreateFromImage failed with rc=%Rrc for module \"%s\" (%s)\n",
}
return VINF_SUCCESS;
}
/**
* @callback_method_impl{FNPDMR3ENUM}
*/
static DECLCALLBACK(int) dbgfR3AsLazyPopulateRCCallback(PVM pVM, const char *pszFilename, const char *pszName,
{
/* Only raw-mode modules. */
if (enmCtx == PDMLDRCTX_RAW_MODE)
{
int rc = RTDbgModCreateFromImage(&hDbgMod, pszFilename, pszName, RTLDRARCH_X86_32, pVM->pUVM->dbgf.s.hDbgCfg);
if (RT_SUCCESS(rc))
{
if (RT_FAILURE(rc))
LogRel(("DBGF: Failed to link module \"%s\" into DBGF_AS_RC at %RTptr: %Rrc\n",
}
else
LogRel(("DBGF: RTDbgModCreateFromImage failed with rc=%Rrc for module \"%s\" (%s)\n",
}
return VINF_SUCCESS;
}
/**
* Lazily populates the specified address space.
*
* @param pUVM The user mode VM handle.
* @param hAlias The alias.
*/
{
{
{
LogRel(("DBGF: Lazy init of RC address space\n"));
#ifdef VBOX_WITH_RAW_MODE
#endif
}
{
/** @todo Lazy load pc and vga bios symbols or the EFI stuff. */
}
}
}
/**
* Resolves the address space handle into a real handle if it's an alias.
*
* @returns Real address space handle. NIL_RTDBGAS if invalid handle.
*
* @param pUVM The user mode VM handle.
* @param hAlias The possibly address space alias.
*
* @remarks Doesn't take any locks.
*/
{
if (iAlias < DBGF_AS_COUNT)
return hAlias;
}
/**
* Resolves the address space handle into a real handle if it's an alias,
* and retains whatever it is.
*
* @returns Real address space handle. NIL_RTDBGAS if invalid handle.
*
* @param pUVM The user mode VM handle.
* @param hAlias The possibly address space alias.
*/
{
if (iAlias < DBGF_AS_COUNT)
{
if (DBGF_AS_IS_FIXED_ALIAS(hAlias))
{
/* Perform lazy address space population. */
/* Won't ever change, no need to grab the lock. */
}
else
{
/* May change, grab the lock so we can read it safely. */
}
}
else
/* Not an alias, just retain it. */
}
/**
* Query an address space by name.
*
* @returns Retained address space handle if found, NIL_RTDBGAS if not.
*
* @param pUVM The user mode VM handle.
* @param pszName The name.
*/
{
/*
* Validate the input.
*/
/*
* Look it up in the string space and retain the result.
*/
if (pNode)
{
}
return hDbgAs;
}
/**
* Query an address space by process ID.
*
* @returns Retained address space handle if found, NIL_RTDBGAS if not.
*
* @param pUVM The user mode VM handle.
* @param ProcId The process ID.
*/
{
/*
* Validate the input.
*/
/*
* Look it up in the PID tree and retain the result.
*/
if (pNode)
{
}
return hDbgAs;
}
/**
* Searches for the file in the path.
*
* The file is first tested without any path modification, then we walk the path
* looking in each directory.
*
* @returns VBox status code.
* @param pszFilename The file to search for.
* @param pszPath The search path.
* @param pfnOpen The open callback function.
* @param pvUser User argument for the callback.
*/
static int dbgfR3AsSearchPath(const char *pszFilename, const char *pszPath, PFNDBGFR3ASSEARCHOPEN pfnOpen, void *pvUser)
{
char szFound[RTPATH_MAX];
/* Check the filename length. */
if (cchFilename >= sizeof(szFound))
return VERR_FILENAME_TOO_LONG;
if (!pszName)
return VERR_IS_A_DIRECTORY;
/*
* Try default location first.
*/
if (RT_SUCCESS(rc))
return rc;
/*
* Walk the search path.
*/
while (*psz)
{
/* Skip leading blanks - no directories with leading spaces, thank you. */
while (RT_C_IS_BLANK(*psz))
psz++;
/* Find the end of this element. */
const char *pszNext;
if (!pszEnd)
else
{
{
/** @todo RTPathCompose, RTPathComposeN(). This code isn't right
* for 'E:' on DOS systems. It may also create unwanted double slashes. */
if (RT_SUCCESS(rc2))
return rc2;
&& ( rc == VERR_FILE_NOT_FOUND
|| rc == VERR_OPEN_FAILED))
}
}
/* advance */
}
/*
* Walk the path once again, this time do a depth search.
*/
/** @todo do a depth search using the specified path. */
/* failed */
return rc;
}
/**
* Same as dbgfR3AsSearchEnv, except that the path is taken from the environment.
*
* If the environment variable doesn't exist, the current directory is searched
* instead.
*
* @returns VBox status code.
* @param pszFilename The filename.
* @param pszEnvVar The environment variable name.
* @param pfnOpen The open callback function.
* @param pvUser User argument for the callback.
*/
static int dbgfR3AsSearchEnvPath(const char *pszFilename, const char *pszEnvVar, PFNDBGFR3ASSEARCHOPEN pfnOpen, void *pvUser)
{
int rc;
if (pszPath)
{
}
else
return rc;
}
/**
* Same as dbgfR3AsSearchEnv, except that the path is taken from the DBGF config
* (CFGM).
*
* Nothing is done if the CFGM variable isn't set.
*
* @returns VBox status code.
* @param pUVM The user mode VM handle.
* @param pszFilename The filename.
* @param pszCfgValue The name of the config variable (under /DBGF/).
* @param pfnOpen The open callback function.
* @param pvUser User argument for the callback.
*/
{
char *pszPath;
int rc = CFGMR3QueryStringAllocDef(CFGMR3GetChild(CFGMR3GetRootU(pUVM), "/DBGF"), pszCfgValue, &pszPath, NULL);
if (RT_FAILURE(rc))
return rc;
if (!pszPath)
return VERR_FILE_NOT_FOUND;
return rc;
}
/**
* Load symbols from an executable module into the specified address space.
*
* If an module exist at the specified address it will be replaced by this
* call, otherwise a new module is created.
*
* @returns VBox status code.
*
* @param pUVM The user mode VM handle.
* @param hDbgAs The address space.
* @param pszFilename The filename of the executable module.
* @param pszModName The module name. If NULL, then then the file name
* base is used (no extension or nothing).
* @param enmArch The desired architecture, use RTLDRARCH_WHATEVER if
* it's not relevant or known.
* @param pModAddress The load address of the module.
* @param iModSeg The segment to load, pass NIL_RTDBGSEGIDX to load
* the whole image.
* @param fFlags Flags reserved for future extensions, must be 0.
*/
VMMR3DECL(int) DBGFR3AsLoadImage(PUVM pUVM, RTDBGAS hDbgAs, const char *pszFilename, const char *pszModName, RTLDRARCH enmArch,
{
/*
* Validate input
*/
if (hRealAS == NIL_RTDBGAS)
return VERR_INVALID_HANDLE;
if (RT_SUCCESS(rc))
{
if (RT_FAILURE(rc))
}
return rc;
}
/**
* Load symbols from a map file into a module at the specified address space.
*
* If an module exist at the specified address it will be replaced by this
* call, otherwise a new module is created.
*
* @returns VBox status code.
*
* @param pUVM The user mode VM handle.
* @param hDbgAs The address space.
* @param pszFilename The map file.
* @param pszModName The module name. If NULL, then then the file name
* base is used (no extension or nothing).
* @param pModAddress The load address of the module.
* @param iModSeg The segment to load, pass NIL_RTDBGSEGIDX to load
* the whole image.
* @param uSubtrahend Value to to subtract from the symbols in the map
* file. This is useful for the linux System.map and
* @param fFlags Flags reserved for future extensions, must be 0.
*/
VMMR3DECL(int) DBGFR3AsLoadMap(PUVM pUVM, RTDBGAS hDbgAs, const char *pszFilename, const char *pszModName,
{
/*
* Validate input
*/
if (hRealAS == NIL_RTDBGAS)
return VERR_INVALID_HANDLE;
int rc = RTDbgModCreateFromMap(&hDbgMod, pszFilename, pszModName, uSubtrahend, pUVM->dbgf.s.hDbgCfg);
if (RT_SUCCESS(rc))
{
if (RT_FAILURE(rc))
}
return rc;
}
/**
* Wrapper around RTDbgAsModuleLink, RTDbgAsModuleLinkSeg and DBGFR3AsResolve.
*
* @returns VBox status code.
* @param pUVM The user mode VM handle.
* @param hDbgAs The address space handle.
* @param hMod The module handle.
* @param pModAddress The link address.
* @param iModSeg The segment to link, NIL_RTDBGSEGIDX for the entire image.
* @param fFlags Flags to pass to the link functions, see RTDBGASLINK_FLAGS_*.
*/
VMMR3DECL(int) DBGFR3AsLinkModule(PUVM pUVM, RTDBGAS hDbgAs, RTDBGMOD hMod, PCDBGFADDRESS pModAddress,
{
/*
* Input validation.
*/
if (hRealAS == NIL_RTDBGAS)
return VERR_INVALID_HANDLE;
/*
* Do the job.
*/
int rc;
if (iModSeg == NIL_RTDBGSEGIDX)
else
return rc;
}
/**
* Wrapper around RTDbgAsModuleByName and RTDbgAsModuleUnlink.
*
* Unlinks all mappings matching the given module name.
*
* @returns VBox status code.
* @param pUVM The user mode VM handle.
* @param hDbgAs The address space handle.
* @param pszModName The name of the module to unlink.
*/
{
/*
* Input validation.
*/
if (hRealAS == NIL_RTDBGAS)
return VERR_INVALID_HANDLE;
/*
* Do the job.
*/
if (RT_SUCCESS(rc))
{
for (;;)
{
if (RT_FAILURE(rc))
break;
if (RT_FAILURE_NP(rc))
{
if (rc == VERR_NOT_FOUND)
rc = VINF_SUCCESS;
break;
}
}
}
return rc;
}
/**
* Adds the module name to the symbol name.
*
* @param hMod The module handle.
*/
{
/* Figure the lengths, adjust them if the result is too long. */
{
}
/* Do the moving and copying. */
}
/** Temporary symbol conversion function. */
{
}
/**
* Query a symbol by address.
*
* The returned symbol is the one we consider closes to the specified address.
*
* @returns VBox status code. See RTDbgAsSymbolByAddr.
*
* @param pUVM The user mode VM handle.
* @param hDbgAs The address space handle.
* @param pAddress The address to lookup.
* @param fFlags One of the RTDBGSYMADDR_FLAGS_XXX flags.
* @param poffDisp Where to return the distance between the returned
* symbol and pAddress. Optional.
* @param pSymbol Where to return the symbol information. The returned
* symbol name will be prefixed by the module name as
* far as space allows.
* @param phMod Where to return the module handle. Optional.
*/
VMMR3DECL(int) DBGFR3AsSymbolByAddr(PUVM pUVM, RTDBGAS hDbgAs, PCDBGFADDRESS pAddress, uint32_t fFlags,
{
/*
* Implement the special address space aliases the lazy way.
*/
if (hDbgAs == DBGF_AS_RC_AND_GC_GLOBAL)
{
if (RT_FAILURE(rc))
return rc;
}
/*
* Input validation.
*/
if (poffDisp)
*poffDisp = 0;
if (phMod)
*phMod = NIL_RTDBGMOD;
if (hRealAS == NIL_RTDBGAS)
return VERR_INVALID_HANDLE;
/*
* Do the lookup.
*/
if (RT_SUCCESS(rc))
{
if (!phMod)
}
return rc;
}
/**
* Convenience function that combines RTDbgSymbolDup and DBGFR3AsSymbolByAddr.
*
* @returns Pointer to the symbol on success. This must be free using
* RTDbgSymbolFree(). NULL is returned if not found or any error
* occurs.
*
* @param pUVM The user mode VM handle.
* @param hDbgAs See DBGFR3AsSymbolByAddr.
* @param pAddress See DBGFR3AsSymbolByAddr.
* @param fFlags See DBGFR3AsSymbolByAddr.
* @param poffDisp See DBGFR3AsSymbolByAddr.
* @param phMod See DBGFR3AsSymbolByAddr.
*/
VMMR3DECL(PRTDBGSYMBOL) DBGFR3AsSymbolByAddrA(PUVM pUVM, RTDBGAS hDbgAs, PCDBGFADDRESS pAddress, uint32_t fFlags,
{
if (RT_SUCCESS(rc))
return RTDbgSymbolDup(&SymInfo);
return NULL;
}
/**
* Query a symbol by name.
*
* The symbol can be prefixed by a module name pattern to scope the search. The
* pattern is a simple string pattern with '*' and '?' as wild chars. See
* RTStrSimplePatternMatch().
*
* @returns VBox status code. See RTDbgAsSymbolByAddr.
*
* @param pUVM The user mode VM handle.
* @param hDbgAs The address space handle.
* @param pszSymbol The symbol to search for, maybe prefixed by a
* module pattern.
* @param pSymbol Where to return the symbol information.
* The returned symbol name will be prefixed by
* the module name as far as space allows.
* @param phMod Where to return the module handle. Optional.
*/
{
/*
* Implement the special address space aliases the lazy way.
*/
if (hDbgAs == DBGF_AS_RC_AND_GC_GLOBAL)
{
if (RT_FAILURE(rc))
return rc;
}
/*
* Input validation.
*/
if (phMod)
*phMod = NIL_RTDBGMOD;
if (hRealAS == NIL_RTDBGAS)
return VERR_INVALID_HANDLE;
/*
* Do the lookup.
*/
if (RT_SUCCESS(rc))
{
if (!phMod)
}
return rc;
}
{
/*
* Implement the special address space aliases the lazy way.
*/
if (hDbgAs == DBGF_AS_RC_AND_GC_GLOBAL)
{
if (RT_FAILURE(rc))
return rc;
}
/*
* Input validation.
*/
if (poffDisp)
*poffDisp = 0;
if (phMod)
*phMod = NIL_RTDBGMOD;
if (hRealAS == NIL_RTDBGAS)
return VERR_INVALID_HANDLE;
/*
* Do the lookup.
*/
}
{
if (RT_SUCCESS(rc))
return RTDbgLineDup(&Line);
return NULL;
}