DBGFDisas.cpp revision e11dff3bd45f7b54b4bdd85613e93f3e57a34e65
/* $Id$ */
/** @file
* DBGF - Debugger Facility, Disassembler.
*/
/*
* 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_DBGF
#include "DBGFInternal.h"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Structure used when disassembling and instructions in DBGF.
* This is used so the reader function can get the stuff it needs.
*/
typedef struct
{
/** The core structure. */
/** Pointer to the VM. */
/** Pointer to the VMCPU. */
/** The address space for resolving symbol. */
/** Pointer to the first byte in the segment. */
/** Pointer to the byte after the end of the segment. (might have wrapped!) */
/** The size of the segment minus 1. */
/** The guest paging mode. */
/** Pointer to the current page - R3 Ptr. */
void const *pvPageR3;
/** Pointer to the current page - GC Ptr. */
/** Pointer to the next instruction (relative to GCPtrSegBase). */
/** The lock information that PGMPhysReleasePageMappingLock needs. */
/** Whether the PageMapLock is valid or not. */
bool fLocked;
/** 64 bits mode or not. */
bool f64Bits;
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static FNDISREADBYTES dbgfR3DisasInstrRead;
/**
* Calls the disassembler with the proper reader functions and such for disa
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pVCpu Pointer to the VMCPU.
* @param pSelInfo The selector info.
* @param enmMode The guest paging mode.
* @param fFlags DBGF_DISAS_FLAGS_XXX.
* @param GCPtr The GC pointer (selector offset).
* @param pState The disas CPU state.
*/
{
pState->hAs = pSelInfo->fFlags & DBGFSELINFO_FLAGS_HYPER /** @todo Deal more explicitly with RC in DBGFR3Disas*. */
switch (fFlags & DBGF_DISAS_FLAGS_MODE_MASK)
{
default:
AssertFailed();
break;
break;
break;
break;
}
&cbInstr);
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
/* cleanup */
{
}
return rc;
}
#if 0
/**
* Calls the disassembler for disassembling the next instruction.
*
* @returns VBox status code.
* @param pState The disas CPU state.
*/
{
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
return rc;
}
#endif
/**
* Done with the disassembler state, free associated resources.
*
* @param pState The disas CPU state ++.
*/
{
{
}
}
/**
* @callback_method_impl{FNDISREADBYTES}
*
* @remarks The source is relative to the base address indicated by
* DBGFDISASSTATE::GCPtrSegBase.
*/
static DECLCALLBACK(int) dbgfR3DisasInstrRead(PDISCPUSTATE pDis, uint8_t offInstr, uint8_t cbMinRead, uint8_t cbMaxRead)
{
for (;;)
{
/*
* Need to update the page translation?
*/
{
int rc = VINF_SUCCESS;
/* translate the address */
{
}
else
{
rc = PGMPhysGCPhys2CCPtrReadOnly(pState->pVM, pState->GCPtrPage, &pState->pvPageR3, &pState->PageMapLock);
else
rc = PGMPhysGCPtr2CCPtrReadOnly(pState->pVCpu, pState->GCPtrPage, &pState->pvPageR3, &pState->PageMapLock);
}
if (RT_FAILURE(rc))
{
return rc;
}
}
/*
* Check the segment limit.
*/
return VERR_OUT_OF_SELECTOR_BOUNDS;
/*
* Calc how much we can read, maxing out the read.
*/
{
}
/*
* Read and advance,
*/
{
return VINF_SUCCESS;
}
}
}
/**
* @copydoc FNDISGETSYMBOL
*/
static DECLCALLBACK(int) dbgfR3DisasGetSymbol(PCDISCPUSTATE pCpu, uint32_t u32Sel, RTUINTPTR uAddress, char *pszBuf, size_t cchBuf, RTINTPTR *poff, void *pvUser)
{
int rc;
if ( DIS_FMT_SEL_IS_REG(u32Sel)
{
if (RT_SUCCESS(rc))
}
else
if (RT_SUCCESS(rc))
{
}
return rc;
}
/**
* Disassembles the one instruction according to the specified flags and
* address, internal worker executing on the EMT of the specified virtual CPU.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pVCpu Pointer to the VMCPU.
* @param Sel The code selector. This used to determine the 32/16 bit ness and
* calculation of the actual instruction address.
* @param pGCPtr Pointer to the variable holding the code address
* relative to the base of Sel.
* @param fFlags Flags controlling where to start and how to format.
* A combination of the DBGF_DISAS_FLAGS_* \#defines.
* @param pszOutput Output buffer.
* @param cbOutput Size of the output buffer.
* @param pcbInstr Where to return the size of the instruction.
*/
static DECLCALLBACK(int)
{
int rc;
/*
* Get the Sel and GCPtr if fFlags requests that.
*/
{
}
else if (fFlags & DBGF_DISAS_FLAGS_CURRENT_HYPER)
{
}
/*
* Check if the selector matches the guest CS, use the hidden
* registers from that if they are valid. Saves time and effort.
*/
else
{
else
}
/*
* Read the selector info - assume no stale selectors and nasty stuff like that.
*
* Note! We CANNOT load invalid hidden selector registers since that would
* guest state and make things behave differently.
*/
bool fRealModeAddress = false;
if ( pSRegCS
{
}
else if (Sel == DBGF_SEL_FLAT)
{
: enmMode != PGMMODE_REAL
{
/* Assume the current CS defines the execution mode. */
}
else
{
}
}
else if ( !(fFlags & DBGF_DISAS_FLAGS_CURRENT_HYPER)
|| enmMode == PGMMODE_REAL
)
)
{ /* V86 mode or real mode - real mode addressing */
fRealModeAddress = true;
}
else
{
if (RT_FAILURE(rc))
{
return rc;
}
}
/*
* Disassemble it.
*/
if (RT_FAILURE(rc))
{
return rc;
}
/*
* Format it.
*/
char szBuf[512];
&SelInfo);
/*
* Print it to the user specified buffer.
*/
if (fFlags & DBGF_DISAS_FLAGS_NO_BYTES)
{
else if (fRealModeAddress)
else if (Sel == DBGF_SEL_FLAT)
{
if (enmMode >= PGMMODE_AMD64)
else
}
else
{
if (enmMode >= PGMMODE_AMD64)
else
}
}
else
{
szBuf);
else if (fRealModeAddress)
szBuf);
else if (Sel == DBGF_SEL_FLAT)
{
if (enmMode >= PGMMODE_AMD64)
szBuf);
else
szBuf);
}
else
{
if (enmMode >= PGMMODE_AMD64)
szBuf);
else
szBuf);
}
}
if (pcbInstr)
return VINF_SUCCESS;
}
/**
* Disassembles the one instruction according to the specified flags and address.
*
* @returns VBox status code.
* @param pUVM The user mode VM handle.
* @param idCpu The ID of virtual CPU.
* @param Sel The code selector. This used to determine the 32/16 bit ness and
* calculation of the actual instruction address.
* @param GCPtr The code address relative to the base of Sel.
* @param fFlags Flags controlling where to start and how to format.
* A combination of the DBGF_DISAS_FLAGS_* \#defines.
* @param pszOutput Output buffer. This will always be properly
* terminated if @a cbOutput is greater than zero.
* @param cbOutput Size of the output buffer.
* @param pcbInstr Where to return the size of the instruction.
*
* @remarks May have to switch to the EMT of the virtual CPU in order to do
* address conversion.
*/
VMMR3DECL(int) DBGFR3DisasInstrEx(PUVM pUVM, VMCPUID idCpu, RTSEL Sel, RTGCPTR GCPtr, uint32_t fFlags,
{
*pszOutput = '\0';
AssertReturn((fFlags & DBGF_DISAS_FLAGS_MODE_MASK) <= DBGF_DISAS_FLAGS_64BIT_MODE, VERR_INVALID_PARAMETER);
/*
* Optimize the common case where we're called on the EMT of idCpu since
* we're using this all the time when logging.
*/
int rc;
if ( pVCpu
else
return rc;
}
/**
* Disassembles the current guest context instruction.
* All registers and data will be displayed. Addresses will be attempted resolved to symbols.
*
* @returns VBox status code.
* @param pVCpu Pointer to the VMCPU.
* @param pszOutput Output buffer. This will always be properly
* terminated if @a cbOutput is greater than zero.
* @param cbOutput Size of the output buffer.
* @thread EMT(pVCpu)
*/
{
*pszOutput = '\0';
}
/**
* Disassembles the current guest context instruction and writes it to the log.
* All registers and data will be displayed. Addresses will be attempted resolved to symbols.
*
* @returns VBox status code.
* @param pVCpu Pointer to the VMCPU.
* @param pszPrefix Short prefix string to the disassembly string. (optional)
* @thread EMT(pVCpu)
*/
{
char szBuf[256];
szBuf[0] = '\0';
if (RT_FAILURE(rc))
{
else
}
else
return rc;
}
/**
* Disassembles the specified guest context instruction and writes it to the log.
* Addresses will be attempted resolved to symbols.
*
* @returns VBox status code.
* @param pVCpu Pointer to the VMCPU, defaults to CPU 0 if NULL.
* @param Sel The code selector. This used to determine the 32/16 bit-ness and
* calculation of the actual instruction address.
* @param GCPtr The code address relative to the base of Sel.
* @param pszPrefix Short prefix string to the disassembly string. (optional)
* @thread EMT(pVCpu)
*/
VMMR3DECL(int) DBGFR3DisasInstrLogInternal(PVMCPU pVCpu, RTSEL Sel, RTGCPTR GCPtr, const char *pszPrefix)
{
char szBuf[256];
int rc = dbgfR3DisasInstrExOnVCpu(pVCpu->pVMR3, pVCpu, Sel, &GCPtrTmp, DBGF_DISAS_FLAGS_DEFAULT_MODE,
if (RT_FAILURE(rc))
RTStrPrintf(szBuf, sizeof(szBuf), "DBGFR3DisasInstrLog(, %RTsel, %RGv) failed with rc=%Rrc\n", Sel, GCPtr, rc);
{
else
}
else
return rc;
}