PATMR3Dbg.cpp revision c52fc8460f9320bd211137767aa86f7d4352975a
/* $Id$ */
/** @file
* PATM - Dynamic Guest OS Patching Manager, Debugger Related Parts.
*/
/*
* Copyright (C) 2006-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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_PATM
#include "PATMInternal.h"
#include "PATMA.h"
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** Adds a structure member to a debug (pseudo) module as a symbol. */
do { \
} while (0)
/** Adds a structure member to a debug (pseudo) module as a symbol. */
do { \
} while (0)
/**
* Called by PATMR3Init.
*
* @param pVM The cross context VM structure.
*/
{
}
/**
* Called by PATMR3Term.
*
* @param pVM The cross context VM structure.
*/
{
{
}
}
/**
* Called by when the patch memory is reinitialized.
*
* @param pVM The cross context VM structure.
*/
{
{
}
}
{
char * const pszNameStart = pszName;
do { \
{ \
}\
} while (0)
/* Start the name off with the address of the guest code. */
/* Append flags. */
if (fFlags & PATMFL_INTHANDLER)
ADD_SZ("_IntHandler");
if (fFlags & PATMFL_SYSENTER)
ADD_SZ("_SysEnter");
if (fFlags & PATMFL_GUEST_SPECIFIC)
ADD_SZ("_GuestSpecific");
if (fFlags & PATMFL_USER_MODE)
ADD_SZ("_UserMode");
if (fFlags & PATMFL_IDTHANDLER)
ADD_SZ("_IdtHnd");
if (fFlags & PATMFL_TRAPHANDLER)
ADD_SZ("_TrapHnd");
if (fFlags & PATMFL_DUPLICATE_FUNCTION)
ADD_SZ("_DupFunc");
ADD_SZ("_ReplFunc");
ADD_SZ("_TrapHndErrCd");
if (fFlags & PATMFL_MMIO_ACCESS)
ADD_SZ("_MmioAccess");
if (fFlags & PATMFL_SYSENTER_XP)
ADD_SZ("_SysEnterXP");
if (fFlags & PATMFL_INT3_REPLACEMENT)
ADD_SZ("_Int3Repl");
if (fFlags & PATMFL_SUPPORT_CALLS)
ADD_SZ("_SupCalls");
ADD_SZ("_SupIndirCalls");
ADD_SZ("_IdtHandlerWE");
if (fFlags & PATMFL_INHIBIT_IRQS)
ADD_SZ("_InhibitIrqs");
if (fFlags & PATMFL_RECOMPILE_NEXT)
ADD_SZ("_RecompileNext");
ADD_SZ("_Callable");
if (fFlags & PATMFL_TRAMPOLINE)
ADD_SZ("_Trampoline");
if (fFlags & PATMFL_PATCHED_GUEST_CODE)
ADD_SZ("_PatchedGuestCode");
ADD_SZ("_MustInstallPatchJmp");
ADD_SZ("_Int3ReplBlock");
ADD_SZ("_ExtJmp");
if (fFlags & PATMFL_CODE_REFERENCED)
ADD_SZ("_CodeRefed");
return pszName - pszNameStart;
}
/**
* Called when a new patch is added or when first populating the address space.
*
* @param pVM The cross context VM structure.
* @param pPatchRec The patch record.
*/
{
{
/** @todo find a cheap way of checking whether we've already added the patch.
* Using a flag would be nice, except I don't want to consider saved
* state considerations right now (I don't recall if we're still
* depending on structure layout there or not). */
char szName[256];
/* If we have a symbol near the guest address, append that. */
{
if (RT_SUCCESS(rc))
{
}
}
}
}
/**
* Enumeration callback used by patmR3DbgAddPatches
*
* @returns 0 (continue enum)
* @param pNode The patch record node.
* @param pvUser The cross context VM structure.
*/
{
return 0;
}
/**
* Populates an empty "patches" (hDbgModPatchMem) module with patch symbols.
*
* @param pVM The cross context VM structure.
* @param hDbgMod The debug module handle.
*/
{
/*
* Global functions and a start marker.
*/
ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pfnHelperCallGC, PATMLookupAndCallRecord.cbFunction, "PATMLookupAndCall");
ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pfnHelperRetGC, PATMRetFunctionRecord.cbFunction, "PATMRetFunction");
ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pfnHelperJumpGC, PATMLookupAndJumpRecord.cbFunction, "PATMLookupAndJump");
ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pfnHelperIretGC, PATMIretFunctionRecord.cbFunction, "PATMIretFunction");
ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pGCStackGC, PATM_STACK_TOTAL_SIZE, "PATMStack");
/*
* The patches.
*/
RTAvloU32DoWithAll(&pVM->patm.s.PatchLookupTreeHC->PatchTree, true /*fFromLeft*/, patmR3DbgAddPatchCallback, pVM);
}
/**
* Populate DBGF_AS_RC with PATM symbols.
*
* Called by dbgfR3AsLazyPopulate when DBGF_AS_RC or DBGF_AS_RC_AND_GC_GLOBAL is
* accessed for the first time.
*
* @param pVM The cross context VM structure.
* @param hDbgAs The DBGF_AS_RC address space handle.
*/
{
/*
* Add a fake debug module for the PATMGCSTATE structure.
*/
if (RT_SUCCESS(rc))
{
}
/*
* Add something for the stats so we get some kind of symbols for
* references to them while disassembling patches.
*/
if (RT_SUCCESS(rc))
{
ADD_FUNC(hDbgMod, pVM->patm.s.pStatsGC, pVM->patm.s.pStatsGC, PATM_STAT_MEMSIZE, "PATMMemStatsStart");
}
/*
* Add a fake debug module for the patches and stack.
*/
rc = RTDbgModCreate(&hDbgMod, "patches", pVM->patm.s.cbPatchMem + PATM_STACK_TOTAL_SIZE + PAGE_SIZE, 0 /*fFlags*/);
if (RT_SUCCESS(rc))
{
}
}
/**
* Annotates an instruction if patched.
*
* @param pVM The VM handle.
* @param RCPtr The instruction address.
* @param cbInstr The instruction length.
* @param pszBuf The output buffer. This will be an empty string
* if the instruction wasn't patched. If it's
* patched, it will hold a symbol-like string
* describing the patch.
* @param cbBuf The size of the output buffer.
*/
VMMR3_INT_DECL(void) PATMR3DbgAnnotatePatchedInstruction(PVM pVM, RTRCPTR RCPtr, uint8_t cbInstr, char *pszBuf, size_t cbBuf)
{
/*
* Always zero the buffer.
*/
AssertReturnVoid(cbBuf > 0);
*pszBuf = '\0';
/*
* Drop out immediately if it cannot be a patched instruction.
*/
if (!PATMIsEnabled(pVM))
return;
return;
/*
* Look for a patch record covering any part of the instruction.
*
* The first query results in a patched less or equal to RCPtr. While the
* second results in one that's greater than RCPtr.
*/
pPatchRec = (PPATMPATCHREC)RTAvloU32GetBestFit(&pVM->patm.s.PatchLookupTreeHC->PatchTree, RCPtr, false /*fFromAbove*/);
if ( !pPatchRec
{
pPatchRec = (PPATMPATCHREC)RTAvloU32GetBestFit(&pVM->patm.s.PatchLookupTreeHC->PatchTree, RCPtr, true /*fFromAbove*/);
if ( !pPatchRec
return;
}
/*
* Lazy bird uses the symbol name generation code for describing the patch.
*/
{
const char *pszState;
{
}
else
off += RTStrPrintf(&pszBuf[off], cbBuf - off, " - %s (%u b)", pszState, pPatchRec->patch.cbPatchJump);
}
}