DBGFSym.cpp revision 46306988a9f29e10ff031451c1fba3fd429ec6f6
/* $Id$ */
/** @file
* DBGF - Debugger Facility, Symbol Management.
*/
/*
* Copyright (C) 2006-2007 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.
*
* 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 *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DBGF
#if defined(RT_OS_WINDOWS) && 1 //defined(DEBUG_bird) // enabled this is you want to debug win32 guests, the hypervisor of EFI.
# include <Windows.h>
# define _IMAGEHLP64
# include <DbgHelp.h>
# define HAVE_DBGHELP /* if doing guest stuff, this can be nice. */
#endif
/** @todo Only use DBGHELP for reading modules since it doesn't do all we want (relocations), or is way to slow in some cases (add symbol)! */
#include "DBGFInternal.h"
#ifndef HAVE_DBGHELP
#endif
#include <stdlib.h>
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
#ifdef HAVE_DBGHELP
#endif
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
#ifndef HAVE_DBGHELP
/* later */
/**
* Internal represenation of a symbol.
*/
typedef struct DBGFSYM
{
/** Node core with the symbol address range. */
/** Pointer to the module this symbol is associated with. */
/** Pointer to the next symbol in with this name. */
/** Symbol name. */
char szName[1];
/**
* Symbol name space node.
*/
typedef struct DBGFSYMSPACE
{
/** Node core with the symbol name.
* (it's allocated in the same block as this struct) */
/** Pointer to the first symbol with this name (LIFO). */
#endif
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
#ifndef HAVE_DBGHELP
/**
* Initializes the symbol tree.
*/
{
if (pSym)
{
return VINF_SUCCESS;
return VERR_INTERNAL_ERROR;
}
return VERR_NO_MEMORY;
}
/**
* Insert a record into the symbol tree.
*/
static int dbgfR3SymbolInsert(PVM pVM, const char *pszName, RTGCPTR Address, size_t cb, PDBGFMOD pModule)
{
/*
* Make the address space node.
*/
PDBGFSYM pSym = (PDBGFSYM)MMR3HeapAlloc(pVM, MM_TAG_DBGF_SYMBOL, RT_OFFSETOF(DBGFSYM, szName[cchName]));
if (pSym)
{
if (pOld)
{
{
}
else
{
/*
* Make the name space node.
*/
if (!pName)
{
/* make new symbol space node. */
PDBGFSYMSPACE pName = (PDBGFSYMSPACE)MMR3HeapAlloc(pVM, MM_TAG_DBGF_SYMBOL, sizeof(*pName) + cchName);
if (pName)
{
return VINF_SUCCESS;
}
else
return VINF_SUCCESS;
}
else
{
/* Add to existing symbol name. */
return VINF_SUCCESS;
}
}
}
else
return VERR_INTERNAL_ERROR;
}
return VERR_NO_MEMORY;
}
/**
* Get nearest symbol.
* @returns NULL if no symbol was the for that address.
*/
{
return pSym;
return NULL;
}
/**
* Get first symbol.
* @returns NULL if no symbol by that name.
*/
{
if (pName)
return NULL;
}
#endif
/**
* Strips all kind of spaces from head and tail of a string.
*/
static char *dbgfR3Strip(char *psz)
{
psz++;
*psz2-- = '\0';
return psz;
}
/**
* Initialize the debug info for a VM.
*
* This will check the CFGM for any symbols or symbol files
* which needs loading.
*
* @returns VBox status code.
* @param pVM The VM handle.
*/
{
int rc;
/*
* Initialize the symbol table.
*/
pVM->dbgf.s.pSymbolSpace = (PRTSTRSPACE)MMR3HeapAllocZ(pVM, MM_TAG_DBGF_SYMBOL, sizeof(*pVM->dbgf.s.pSymbolSpace));
#ifndef HAVE_DBGHELP
/* modules & lines later */
if (RT_FAILURE(rc))
return rc;
#endif
/*
* Check if there are 'loadsyms' commands in the configuration.
*/
if (pNode)
{
/*
* Enumerate the commands.
*/
{
char szCmdName[128];
/* File */
char *pszFilename;
AssertMsgRCReturn(rc, ("rc=%Rrc querying the 'File' attribute of '/DBGF/loadsyms/%s'!\n", rc, szCmdName), rc);
/* Delta (optional) */
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
offDelta = 0;
else
AssertMsgRCReturn(rc, ("rc=%Rrc querying the 'Delta' attribute of '/DBGF/loadsyms/%s'!\n", rc, szCmdName), rc);
/* Module (optional) */
char *pszModule;
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
else
AssertMsgRCReturn(rc, ("rc=%Rrc querying the 'Module' attribute of '/DBGF/loadsyms/%s'!\n", rc, szCmdName), rc);
/* Module (optional) */
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
ModuleAddress = 0;
else
AssertMsgRCReturn(rc, ("rc=%Rrc querying the 'ModuleAddress' attribute of '/DBGF/loadsyms/%s'!\n", rc, szCmdName), rc);
/* Image size (optional) */
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
cbModule = 0;
else
AssertMsgRCReturn(rc, ("rc=%Rrc querying the 'ModuleAddress' attribute of '/DBGF/loadsyms/%s'!\n", rc, szCmdName), rc);
/*
* Execute the command.
*/
AssertMsgRCReturn(rc, ("pszFilename=%s offDelta=%RGv pszModule=%s ModuleAddress=%RGv cbModule=%RGv\n",
}
}
/*
* Check if there are any 'symadd' commands in the configuration.
*/
return VINF_SUCCESS;
}
/**
* We delay certain
* Initialize the debug info for a VM.
*/
{
return VINF_SUCCESS;
#ifdef HAVE_DBGHELP
{
/*
* Enumerate all modules loaded by PDM and add them to the symbol database.
*/
return VINF_SUCCESS;
}
return win32Error(pVM);
#else
return VINF_SUCCESS;
#endif
}
#ifdef HAVE_DBGHELP
/**
* Module enumeration callback function.
*
* @returns VBox status.
* Failure will stop the search and return the return code.
* Warnings will be ignored and not returned.
* @param pVM VM Handle.
* @param pszFilename Module filename.
* @param pszName Module name. (short and unique)
* @param ImageBase Address where to executable image is loaded.
* @param cbImage Size of the executable image.
* @param fRC Set if guest context, clear if host context.
* @param pvArg User argument.
*/
{
if (!LoadedImageBase)
else
return VINF_SUCCESS;
}
#endif
/**
* Terminate the debug info repository for the specified VM.
*
* @returns VBox status.
* @param pVM VM Handle.
*/
{
#ifdef HAVE_DBGHELP
return VINF_SUCCESS;
#else
return VINF_SUCCESS;
#endif
}
/** Symbol file type.. */
typedef enum SYMFILETYPE
{
} SYMFILETYPE, *PSYMFILETYPE;
/**
* Probe the type of a symbol information file.
*
* @returns The file type.
* @param pFile File handle.
*/
{
char szHead[4096];
if (cchHead > 0)
{
return SYMFILETYPE_MS_MAP;
return SYMFILETYPE_LD_MAP;
)
return SYMFILETYPE_LINUX_SYSTEM_MAP;
)
return SYMFILETYPE_LINUX_SYSTEM_MAP;
return SYMFILETYPE_PDB;
return SYMFILETYPE_ELF;
return SYMFILETYPE_MZ;
return SYMFILETYPE_OBJDUMP;
}
return SYMFILETYPE_UNKNOWN;
}
static int dbgfR3LoadLinuxSystemMap(PVM pVM, FILE *pFile, RTGCUINTPTR ModuleAddress, RTGCUINTPTR AddressDelta)
{
char szLine[4096];
{
/* parse the line: <address> <type> <name> */
if ( RT_SUCCESS(rc)
&& Address == u64Address
&& u64Address != 0
&& u64Address != (RTGCUINTPTR)~0)
{
pszEnd++;
{
if (*psz)
{
if (RT_FAILURE(rc2))
}
}
}
}
return VINF_SUCCESS;
}
/**
* Tries to open the file using the image search paths.
*
* This is currently a quick hack and the only way to specifying the path is by setting
* VBOXDBG_IMAGE_PATH in the environment. It uses semicolon as separator everywhere.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pszFilename The name of the file to locate and open.
* @param pszFound Where to return the actual filename.
* @param cchFound The buffer size.
* @param ppFile Where to return the opened file.
*/
int dbgfR3ModuleLocateAndOpen(PVM pVM, const char *pszFilename, char *pszFound, size_t cchFound, FILE **ppFile)
{
/* Check the filename length. */
if (cchFilename >= cchFound)
return VERR_FILENAME_TOO_LONG;
if (!pszName)
return VERR_IS_A_DIRECTORY;
/*
* Try default location first.
*/
if (pFile)
return VINF_SUCCESS;
/*
* Walk the search path.
*/
if (!psz)
while (*psz)
{
/* Skip leading blanks - no directories with leading spaces, thank you. */
while (RT_C_IS_BLANK(*psz))
psz++;
/* Fine 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 (pFile)
return VINF_SUCCESS;
}
/** @todo do a depth search using the specified path. */
}
/* advance */
}
/* not found */
return VERR_OPEN_FAILED;
}
/**
* Load debug info, optionally related to a specific module.
*
* @returns VBox status.
* @param pVM VM Handle.
* @param pszFilename Path to the file containing the symbol information.
* This can be the executable image, a flat symbol file of some kind or stripped debug info.
* @param AddressDelta The value to add to the loaded symbols.
* @param pszName Short hand name for the module. If not related to a module specify NULL.
* @param ModuleAddress Address which the image is loaded at. This will be used to reference the module other places in the api.
* Ignored when pszName is NULL.
* @param cbImage Size of the image.
* Ignored when pszName is NULL.
*/
VMMR3DECL(int) DBGFR3ModuleLoad(PVM pVM, const char *pszFilename, RTGCUINTPTR AddressDelta, const char *pszName, RTGCUINTPTR ModuleAddress, unsigned cbImage)
{
/*
* Lazy init.
*/
{
if (RT_FAILURE(rc))
return rc;
}
/*
* Open the load file.
*/
char szFoundFile[RTPATH_MAX];
if (pFile)
{
/*
* Probe the file type.
*/
if (enmType != SYMFILETYPE_UNKNOWN)
{
/*
* Add the module.
*/
if (pszName)
{
#ifdef HAVE_DBGHELP
/** @todo arg! checkout the inserting of modules and then loading them again.... Or just the module representation.... */
DWORD64 ImageBase = SymLoadModule64(pVM, NULL, (char *)(void *)szFoundFile, (char *)(void *)pszName, ModuleAddress, cbImage);
if (!ImageBase)
ImageBase = SymLoadModule64(pVM, NULL, (char *)(void *)pszName, (char *)(void *)pszName, ModuleAddress, cbImage);
if (ImageBase)
{
AssertMsg(ModuleAddress == 0 || ModuleAddress == ImageBase, ("ModuleAddres=%RGv ImageBase=%llx\n", ModuleAddress, ImageBase));
}
else
#else
#endif
}
if (RT_SUCCESS(rc))
{
/*
* Seek to the start of the file.
*/
/*
* Process the specific.
*/
switch (enmType)
{
break;
case SYMFILETYPE_PDB:
case SYMFILETYPE_DBG:
case SYMFILETYPE_MZ:
#ifdef HAVE_DBGHELP
/* done it all above! */
break;
#endif
case SYMFILETYPE_LD_MAP:
case SYMFILETYPE_MS_MAP:
case SYMFILETYPE_OBJDUMP:
case SYMFILETYPE_ELF:
break;
default:
AssertFailed();
break;
} /* file switch. */
} /* module added successfully. */
} /* format identified */
else
/** @todo check for read errors */
}
return rc;
}
/**
* Interface used by PDMR3LdrRelocate for telling us that a GC module has been relocated.
*
* @param pVM The VM handle.
* @param OldImageBase The old image base.
* @param NewImageBase The new image base.
* @param cbImage The image size.
* @param pszFilename The image filename.
* @param pszName The module name.
*/
VMMR3DECL(void) DBGFR3ModuleRelocate(PVM pVM, RTGCUINTPTR OldImageBase, RTGCUINTPTR NewImageBase, RTGCUINTPTR cbImage,
const char *pszFilename, const char *pszName)
{
#ifdef HAVE_DBGHELP
{
DWORD64 LoadedImageBase = SymLoadModule64(pVM, NULL, (char *)(void *)pszFilename, (char *)(void *)pszName, NewImageBase, ImageSize);
if (!LoadedImageBase)
else
}
#else
#endif
}
/**
* Adds a symbol to the debug info manager.
*
* @returns VBox status.
* @param pVM VM Handle.
* @param ModuleAddress Module address. Use 0 if no module.
* @param SymbolAddress Symbol address
* @param cbSymbol Size of the symbol. Use 0 if info not available.
* @param pszSymbol Symbol name.
*/
VMMR3DECL(int) DBGFR3SymbolAdd(PVM pVM, RTGCUINTPTR ModuleAddress, RTGCUINTPTR SymbolAddress, RTUINT cbSymbol, const char *pszSymbol)
{
/*
* Validate.
*/
{
AssertMsgFailed(("No symbol name!\n"));
return VERR_INVALID_PARAMETER;
}
/*
* Lazy init.
*/
{
if (RT_FAILURE(rc))
return rc;
}
#ifdef HAVE_DBGHELP
return VINF_SUCCESS;
return win32Error(pVM);
#else
/** @todo module lookup. */
#endif
}
/**
* Find symbol by address (nearest).
*
* @returns VBox status.
* @param pVM VM handle.
* @param Address Address.
* @param poffDisplacement Where to store the symbol displacement from Address.
* @param pSymbol Where to store the symbol info.
*/
VMMR3DECL(int) DBGFR3SymbolByAddr(PVM pVM, RTGCUINTPTR Address, PRTGCINTPTR poffDisplacement, PDBGFSYMBOL pSymbol)
{
/*
* Lazy init.
*/
{
if (RT_FAILURE(rc))
return rc;
}
/*
* Look it up.
*/
#ifdef HAVE_DBGHELP
char achBuffer[sizeof(IMAGEHLP_SYMBOL64) + DBGF_SYMBOL_NAME_LENGTH * sizeof(TCHAR) + sizeof(ULONG64)];
{
return VINF_SUCCESS;
}
//return win32Error(pVM);
#else
if (pSym)
{
if (poffDisplacement)
return VINF_SUCCESS;
}
#endif
/*
* Try PDM.
*/
{
char szModName[64];
char szNearSym1[260];
char szNearSym2[260];
{
if (poffDisplacement)
return VINF_SUCCESS;
}
}
return VERR_SYMBOL_NOT_FOUND;
}
/**
* Find symbol by name (first).
*
* @returns VBox status.
* @param pVM VM handle.
* @param pszSymbol Symbol name.
* @param pSymbol Where to store the symbol info.
*/
{
/*
* Lazy init.
*/
{
if (RT_FAILURE(rc))
return rc;
}
/*
* Look it up.
*/
#ifdef HAVE_DBGHELP
char achBuffer[sizeof(IMAGEHLP_SYMBOL64) + DBGF_SYMBOL_NAME_LENGTH * sizeof(TCHAR) + sizeof(ULONG64)];
{
return VINF_SUCCESS;
}
return win32Error(pVM);
#else
if (pSym)
{
return VINF_SUCCESS;
}
return VERR_SYMBOL_NOT_FOUND;
#endif
}
/**
* Duplicates a symbol.
*
* @returns Pointer to the duplicated symbol.
* @param pVM The VM handle.
* @param pSymbol The symbol to duplicate.
*/
{
if (pDup)
return pDup;
}
/**
* Find symbol by address (nearest), allocate return buffer.
*
* @returns Pointer to the symbol. Must be freed using DBGFR3SymbolFree().
* @returns NULL if the symbol was not found or if we're out of memory.
* @param pVM VM handle.
* @param Address Address.
* @param poffDisplacement Where to store the symbol displacement from Address.
*/
VMMR3DECL(PDBGFSYMBOL) DBGFR3SymbolByAddrAlloc(PVM pVM, RTGCUINTPTR Address, PRTGCINTPTR poffDisplacement)
{
if (RT_FAILURE(rc))
return NULL;
}
/**
* Find symbol by name (first), allocate return buffer.
*
* @returns Pointer to the symbol. Must be freed using DBGFR3SymbolFree().
* @returns NULL if the symbol was not found or if we're out of memory.
* @param pVM VM handle.
* @param pszSymbol Symbol name.
*/
{
if (RT_FAILURE(rc))
return NULL;
}
/**
* Frees a symbol returned by DBGFR3SymbolbyNameAlloc() or DBGFR3SymbolByAddressAlloc().
*
* @param pSymbol Pointer to the symbol.
*/
{
if (pSymbol)
}
/**
* Find line by address (nearest).
*
* @returns VBox status.
* @param pVM VM handle.
* @param Address Address.
* @param poffDisplacement Where to store the line displacement from Address.
* @param pLine Where to store the line info.
*/
VMMR3DECL(int) DBGFR3LineByAddr(PVM pVM, RTGCUINTPTR Address, PRTGCINTPTR poffDisplacement, PDBGFLINE pLine)
{
/*
* Lazy init.
*/
{
if (RT_FAILURE(rc))
return rc;
}
/*
* Look it up.
*/
#ifdef HAVE_DBGHELP
IMAGEHLP_LINE64 Line = {0};
{
if (poffDisplacement)
*poffDisplacement = (long)off;
return VINF_SUCCESS;
}
return win32Error(pVM);
#else
return VERR_NOT_IMPLEMENTED;
#endif
}
/**
* Duplicates a line.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pLine The line to duplicate.
*/
{
if (pDup)
return pDup;
}
/**
* Find line by address (nearest), allocate return buffer.
*
* @returns Pointer to the line. Must be freed using DBGFR3LineFree().
* @returns NULL if the line was not found or if we're out of memory.
* @param pVM VM handle.
* @param Address Address.
* @param poffDisplacement Where to store the line displacement from Address.
*/
VMMR3DECL(PDBGFLINE) DBGFR3LineByAddrAlloc(PVM pVM, RTGCUINTPTR Address, PRTGCINTPTR poffDisplacement)
{
if (RT_FAILURE(rc))
return NULL;
}
/**
* Frees a line returned by DBGFR3LineByAddressAlloc().
*
* @param pLine Pointer to the line.
*/
{
if (pLine)
}
#ifdef HAVE_DBGHELP
//static BOOL CALLBACK win32EnumModulesCallback(PSTR ModuleName, DWORD64 BaseOfDll, PVOID UserContext)
//{
// Log(("dbg: module: %08llx %s\n", ModuleName, BaseOfDll));
// return TRUE;
//}
{
int rc = GetLastError();
//SymEnumerateModules64(pVM, win32EnumModulesCallback, NULL);
return VERR_GENERAL_FAILURE;
}
#endif