DBGFReg.cpp revision 9e9452ab3fc9a1ca8a23a32adab3b9141b354ed6
/* $Id$ */
/** @file
* DBGF - Debugger Facility, Register Methods.
*/
/*
* Copyright (C) 2010 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"
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** Locks the register database for writing. */
#define DBGF_REG_DB_LOCK_WRITE(pVM) \
do { \
} while (0)
/** Unlocks the register database after writing. */
#define DBGF_REG_DB_UNLOCK_WRITE(pVM) \
do { \
} while (0)
/** Locks the register database for reading. */
#define DBGF_REG_DB_LOCK_READ(pVM) \
do { \
} while (0)
/** Unlocks the register database after reading. */
#define DBGF_REG_DB_UNLOCK_READ(pVM) \
do { \
} while (0)
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Register set registration record type.
*/
typedef enum DBGFREGSETTYPE
{
/** Invalid zero value. */
/** CPU record. */
/** Device record. */
/** End of valid record types. */
/**
* Register set registration record.
*/
typedef struct DBGFREGSET
{
/** String space core. */
/** The registration record type. */
/** The user argument for the callbacks. */
union
{
/** The CPU view. */
/** The device view. */
/** The general view. */
void *pv;
} uUserArg;
/** The register descriptors. */
/** The number of register descriptors. */
/** The register name prefix. */
char szPrefix[32];
} DBGFREGSET;
/** Pointer to a register registration record. */
typedef DBGFREGSET *PDBGFREGSET;
/**
* Validates a register name.
*
* This is used for prefixes, aliases and field names.
*
* @returns true if valid, false if not.
* @param pszName The register name to validate.
*/
static bool dbgfR3RegIsNameValid(const char *pszName)
{
if (!RT_C_IS_ALPHA(*pszName))
return false;
char ch;
if ( !RT_C_IS_LOWER(ch)
&& !RT_C_IS_DIGIT(ch)
&& ch != '_')
return false;
return true;
}
/**
* Common worker for registering a register set.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param paRegisters The register descriptors.
* @param enmType The set type.
* @param pvUserArg The user argument for the callbacks.
* @param pszPrefix The name prefix.
* @param iInstance The instance number to be appended to @a
* pszPrefix when creating the set name.
*/
static int dbgfR3RegRegisterCommon(PVM pVM, PCDBGFREGDESC paRegisters, DBGFREGSETTYPE enmType, void *pvUserArg, const char *pszPrefix, uint32_t iInstance)
{
/*
* Validate input.
*/
/* The name components. */
AssertMsgReturn(cchPrefix < RT_SIZEOFMEMB(DBGFREGSET, szPrefix) - 4 - 1, ("%s\n", pszPrefix), VERR_INVALID_NAME);
/* The descriptors. */
{
AssertMsgReturn(dbgfR3RegIsNameValid(paRegisters[iDesc].pszName), ("%s (#%u)\n", paRegisters[iDesc].pszName, iDesc), VERR_INVALID_NAME);
if (enmType == DBGFREGSETTYPE_CPU)
else
if (paAliases)
{
{
AssertMsgReturn(dbgfR3RegIsNameValid(paAliases[j].pszName), ("%s (%s)\n", paAliases[j].pszName, paRegisters[iDesc].pszName), VERR_INVALID_NAME);
}
}
if (paSubFields)
{
{
AssertMsgReturn(dbgfR3RegIsNameValid(paSubFields[j].pszName), ("%s (%s)\n", paSubFields[j].pszName, paRegisters[iDesc].pszName), VERR_INVALID_NAME);
}
}
}
/*
* Allocate a new record.
*/
PDBGFREGSET pRegRec = (PDBGFREGSET)MMR3HeapAlloc(pVM, MM_TAG_DBGF_REG, RT_OFFSETOF(DBGFREGSET, szPrefix[cchPrefix + 4 + 1]));
if (!pRegRec)
return VERR_NO_MEMORY;
if (fNeedUnderscore)
else
if (fInserted)
return VINF_SUCCESS;
return VERR_DUPLICATE;
}
/**
* Registers a set of registers for a CPU.
*
* @returns VBox status code.
* @param pVCpu The virtual CPU handle.
* @param paRegisters The register descriptors.
*/
{
return dbgfR3RegRegisterCommon(pVCpu->pVMR3, paRegisters, DBGFREGSETTYPE_CPU, pVCpu, "cpu", pVCpu->idCpu);
}
/**
* Registers a set of registers for a device.
*
* @returns VBox status code.
* @param enmReg The register identifier.
* @param enmType The register type. This is for sort out
* aliases. Pass DBGFREGVALTYPE_INVALID to get
* the standard name.
*/
VMMR3DECL(int) DBGFR3RegRegisterDevice(PVM pVM, PCDBGFREGDESC paRegisters, PPDMDEVINS pDevIns, const char *pszPrefix, uint32_t iInstance)
{
return dbgfR3RegRegisterCommon(pVM, paRegisters, DBGFREGSETTYPE_DEVICE, pDevIns, pszPrefix, iInstance);
}
/**
* Clears the register value variable.
*
* @param pValue The variable to clear.
*/
{
}
/**
* Performs a cast between register value types.
*
* @retval VINF_SUCCESS
* @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
* @retval VINF_DBGF_TRUNCATED_REGISTER
* @retval VERR_DBGF_UNSUPPORTED_CAST
*
* @param pValue The value to cast (input + output).
* @param enmFromType The input value.
* @param enmToType The desired output value.
*/
static int dbgfR3RegValCast(PDBGFREGVAL pValue, DBGFREGVALTYPE enmFromType, DBGFREGVALTYPE enmToType)
{
switch (enmFromType)
{
case DBGFREGVALTYPE_U8:
switch (enmToType)
{
case DBGFREGVALTYPE_80: return VERR_DBGF_UNSUPPORTED_CAST;
case DBGFREGVALTYPE_DTR: return VERR_DBGF_UNSUPPORTED_CAST;
case DBGFREGVALTYPE_END:
case DBGFREGVALTYPE_INVALID:
break;
}
break;
case DBGFREGVALTYPE_U16:
switch (enmToType)
{
case DBGFREGVALTYPE_80: return VERR_DBGF_UNSUPPORTED_CAST;
case DBGFREGVALTYPE_DTR: return VERR_DBGF_UNSUPPORTED_CAST;
case DBGFREGVALTYPE_END:
case DBGFREGVALTYPE_INVALID:
break;
}
break;
case DBGFREGVALTYPE_U32:
switch (enmToType)
{
case DBGFREGVALTYPE_80: return VERR_DBGF_UNSUPPORTED_CAST;
case DBGFREGVALTYPE_DTR: return VERR_DBGF_UNSUPPORTED_CAST;
case DBGFREGVALTYPE_END:
case DBGFREGVALTYPE_INVALID:
break;
}
break;
case DBGFREGVALTYPE_U64:
switch (enmToType)
{
case DBGFREGVALTYPE_80: return VERR_DBGF_UNSUPPORTED_CAST;
case DBGFREGVALTYPE_DTR: return VERR_DBGF_UNSUPPORTED_CAST;
case DBGFREGVALTYPE_END:
case DBGFREGVALTYPE_INVALID:
break;
}
break;
case DBGFREGVALTYPE_U128:
switch (enmToType)
{
case DBGFREGVALTYPE_80: return VERR_DBGF_UNSUPPORTED_CAST;
case DBGFREGVALTYPE_DTR: return VERR_DBGF_UNSUPPORTED_CAST;
case DBGFREGVALTYPE_END:
case DBGFREGVALTYPE_INVALID:
break;
}
break;
case DBGFREGVALTYPE_80:
return VERR_DBGF_UNSUPPORTED_CAST;
case DBGFREGVALTYPE_LRD:
switch (enmToType)
{
case DBGFREGVALTYPE_U128:
return VINF_DBGF_TRUNCATED_REGISTER;
case DBGFREGVALTYPE_80: return VERR_DBGF_UNSUPPORTED_CAST;
case DBGFREGVALTYPE_DTR: return VERR_DBGF_UNSUPPORTED_CAST;
case DBGFREGVALTYPE_END:
case DBGFREGVALTYPE_INVALID:
break;
}
break;
case DBGFREGVALTYPE_DTR:
switch (enmToType)
{
case DBGFREGVALTYPE_U128: pValue->u128.s.Lo = InVal.dtr.u64Base; return VINF_DBGF_TRUNCATED_REGISTER;
case DBGFREGVALTYPE_80: return VERR_DBGF_UNSUPPORTED_CAST;
case DBGFREGVALTYPE_END:
case DBGFREGVALTYPE_INVALID:
break;
}
break;
case DBGFREGVALTYPE_INVALID:
case DBGFREGVALTYPE_END:
break;
}
return VERR_DBGF_UNSUPPORTED_CAST;
}
/**
* Worker for the CPU register queries.
*
* @returns VBox status code.
* @retval VINF_SUCCESS
* @retval VERR_INVALID_VM_HANDLE
* @retval VERR_INVALID_CPU_ID
* @retval VERR_DBGF_INVALID_REGISTER
* @retval VERR_DBGF_UNSUPPORTED_CAST
* @retval VINF_DBGF_TRUNCATED_REGISTER
* @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
*
* @param pVM The VM handle.
* @param idCpu The virtual CPU ID.
* @param enmReg The register to query.
* @param enmType The desired return type.
* @param puValue Where to return the register value.
*/
static DECLCALLBACK(int) dbgfR3RegCpuQueryWorker(PVM pVM, VMCPUID idCpu, DBGFREG enmReg, DBGFREGVALTYPE enmType, PDBGFREGVAL puValue)
{
int rc = VINF_SUCCESS;
/*
* Look up the register set of the CPU.
*/
/** @todo optimize this by adding a cpu register set array to DBGF. */
char szSetName[16];
{
/*
* Look up the register and get the register value.
*/
{
if (RT_SUCCESS(rc))
{
/*
* Do the cast if the desired return type doesn't match what
* the getter returned.
*/
rc = VINF_SUCCESS;
else
}
}
else
}
else
return rc;
}
/**
* Queries a 8-bit CPU register value.
*
* @retval VINF_SUCCESS
* @retval VERR_INVALID_VM_HANDLE
* @retval VERR_INVALID_CPU_ID
* @retval VERR_DBGF_INVALID_REGISTER
* @retval VERR_DBGF_UNSUPPORTED_CAST
* @retval VINF_DBGF_TRUNCATED_REGISTER
*
* @param pVM The VM handle.
* @param idCpu The target CPU ID.
* @param enmReg The register that's being queried.
* @param pu8 Where to store the register value.
*/
{
int rc = VMR3ReqCallWait(pVM, idCpu, (PFNRT)dbgfR3RegCpuQueryWorker, 5, pVM, idCpu, enmReg, DBGFREGVALTYPE_U8, &Value);
if (RT_SUCCESS(rc))
else
*pu8 = 0;
return rc;
}
/**
* Queries a 16-bit CPU register value.
*
* @retval VINF_SUCCESS
* @retval VERR_INVALID_VM_HANDLE
* @retval VERR_INVALID_CPU_ID
* @retval VERR_DBGF_INVALID_REGISTER
* @retval VERR_DBGF_UNSUPPORTED_CAST
* @retval VINF_DBGF_TRUNCATED_REGISTER
* @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
*
* @param pVM The VM handle.
* @param idCpu The target CPU ID.
* @param enmReg The register that's being queried.
* @param pu16 Where to store the register value.
*/
{
int rc = VMR3ReqCallWait(pVM, idCpu, (PFNRT)dbgfR3RegCpuQueryWorker, 5, pVM, idCpu, enmReg, DBGFREGVALTYPE_U16, &Value);
if (RT_SUCCESS(rc))
else
*pu16 = 0;
return rc;
}
/**
* Queries a 32-bit CPU register value.
*
* @retval VINF_SUCCESS
* @retval VERR_INVALID_VM_HANDLE
* @retval VERR_INVALID_CPU_ID
* @retval VERR_DBGF_INVALID_REGISTER
* @retval VERR_DBGF_UNSUPPORTED_CAST
* @retval VINF_DBGF_TRUNCATED_REGISTER
* @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
*
* @param pVM The VM handle.
* @param idCpu The target CPU ID.
* @param enmReg The register that's being queried.
* @param pu32 Where to store the register value.
*/
{
int rc = VMR3ReqCallWait(pVM, idCpu, (PFNRT)dbgfR3RegCpuQueryWorker, 5, pVM, idCpu, enmReg, DBGFREGVALTYPE_U32, &Value);
if (RT_SUCCESS(rc))
else
*pu32 = 0;
return rc;
}
/**
* Queries a 64-bit CPU register value.
*
* @retval VINF_SUCCESS
* @retval VERR_INVALID_VM_HANDLE
* @retval VERR_INVALID_CPU_ID
* @retval VERR_DBGF_INVALID_REGISTER
* @retval VERR_DBGF_UNSUPPORTED_CAST
* @retval VINF_DBGF_TRUNCATED_REGISTER
* @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
*
* @param pVM The VM handle.
* @param idCpu The target CPU ID.
* @param enmReg The register that's being queried.
* @param pu64 Where to store the register value.
*/
{
int rc = VMR3ReqCallWait(pVM, idCpu, (PFNRT)dbgfR3RegCpuQueryWorker, 5, pVM, idCpu, enmReg, DBGFREGVALTYPE_U64, &Value);
if (RT_SUCCESS(rc))
else
*pu64 = 0;
return rc;
}
/**
* Wrapper around CPUMQueryGuestMsr for dbgfR3RegCpuQueryBatchWorker.
*
* @retval VINF_SUCCESS
* @retval VERR_DBGF_INVALID_REGISTER
*
* @param pVCpu The current CPU.
* @param pReg The where to store the register value and
* size.
* @param idMsr The MSR to get.
*/
{
if (RT_FAILURE(rc))
{
}
}
static DECLCALLBACK(int) dbgfR3RegCpuQueryBatchWorker(PVM pVM, VMCPUID idCpu, PDBGFREGENTRY paRegs, size_t cRegs)
{
#if 0
while (cRegs-- > 0)
{
pReg++;
AssertMsgReturn(enmReg >= 0 && enmReg <= DBGFREG_END, ("%d (%#x)\n", enmReg, enmReg), VERR_DBGF_INVALID_REGISTER);
if (enmReg != DBGFREG_END)
{
{
{
case DBGFREGVALTYPE_U128:
break;
case DBGFREGVALTYPE_LRD:
break;
default:
}
}
else
{
if (RT_FAILURE(rc))
return rc;
}
}
}
return VINF_SUCCESS;
#else
return VERR_NOT_IMPLEMENTED;
#endif
}
/**
* Query a batch of registers.
*
* @retval VINF_SUCCESS
* @retval VERR_INVALID_VM_HANDLE
* @retval VERR_INVALID_CPU_ID
* @retval VERR_DBGF_INVALID_REGISTER
*
* @param pVM The VM handle.
* @param idCpu The target CPU ID.
* @param paRegs Pointer to an array of @a cRegs elements. On
* input the enmReg members indicates which
* registers to query. On successful return the
* other members are set. DBGFREG_END can be used
* as a filler.
* @param cRegs The number of entries in @a paRegs.
*/
{
if (!cRegs)
return VINF_SUCCESS;
while (iReg-- > 0)
{
AssertMsgReturn(enmReg < DBGFREG_END && enmReg >= DBGFREG_AL, ("%d (%#x)", enmReg, enmReg), VERR_DBGF_INVALID_REGISTER);
}
return VMR3ReqCallWait(pVM, idCpu, (PFNRT)dbgfR3RegCpuQueryBatchWorker, 4, pVM, idCpu, paRegs, cRegs);
}
/**
* Query a all registers for a Virtual CPU.
*
* @retval VINF_SUCCESS
* @retval VERR_INVALID_VM_HANDLE
* @retval VERR_INVALID_CPU_ID
*
* @param pVM The VM handle.
* @param idCpu The target CPU ID.
* @param paRegs Pointer to an array of @a cRegs elements.
* These will be filled with the CPU register
* values. Overflowing entries will be set to
* DBGFREG_END. The returned registers can be
* accessed by using the DBGFREG values as index.
* @param cRegs The number of entries in @a paRegs. The
* recommended value is DBGFREG_ALL_COUNT.
*/
{
/*
* Validate input.
*/
if (!cRegs)
return VINF_SUCCESS;
/*
* Convert it into a batch query (lazy bird).
*/
unsigned iReg = 0;
{
iReg++;
}
return VMR3ReqCallWait(pVM, idCpu, (PFNRT)dbgfR3RegCpuQueryBatchWorker, 4, pVM, idCpu, paRegs, cRegs);
}
/**
* Gets the name of a register.
*
* @returns Pointer to read-only register name (lower case). NULL if the
* parameters are invalid.
* @param enmReg The register identifier.
* @param enmType The register type. This is for sort out
* aliases. Pass DBGFREGVALTYPE_INVALID to get
* the standard name.
*/
{
#if 0 /** @todo need the optimization */
if ( pAlias
&& enmType != DBGFREGVALTYPE_INVALID)
{
{
pAlias++;
}
}
#else
return NULL;
#endif
}
/** @todo Implementing missing APIs. */