DBGFReg.cpp revision 78767995a4d318c552cd8b1cdf57547964acf595
/* $Id$ */
/** @file
* DBGF - Debugger Facility, Register Methods.
*/
/*
* Copyright (C) 2010-2011 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* 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 <VBox/vmm/dbgf.h>
#include "DBGFInternal.h"
#include <VBox/vmm/mm.h>
#include <VBox/vmm/vm.h>
#include <VBox/param.h>
#include <VBox/err.h>
#include <VBox/log.h>
#include <iprt/ctype.h>
#include <iprt/string.h>
#include <iprt/uint128.h>
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** Locks the register database for writing. */
#define DBGF_REG_DB_LOCK_WRITE(pVM) \
do { \
int rcSem = RTSemRWRequestWrite((pVM)->dbgf.s.hRegDbLock, RT_INDEFINITE_WAIT); \
AssertRC(rcSem); \
} while (0)
/** Unlocks the register database after writing. */
#define DBGF_REG_DB_UNLOCK_WRITE(pVM) \
do { \
int rcSem = RTSemRWReleaseWrite((pVM)->dbgf.s.hRegDbLock); \
AssertRC(rcSem); \
} while (0)
/** Locks the register database for reading. */
#define DBGF_REG_DB_LOCK_READ(pVM) \
do { \
int rcSem = RTSemRWRequestRead((pVM)->dbgf.s.hRegDbLock, RT_INDEFINITE_WAIT); \
AssertRC(rcSem); \
} while (0)
/** Unlocks the register database after reading. */
#define DBGF_REG_DB_UNLOCK_READ(pVM) \
do { \
int rcSem = RTSemRWReleaseRead((pVM)->dbgf.s.hRegDbLock); \
AssertRC(rcSem); \
} while (0)
/** The max length of a set, register or sub-field name. */
#define DBGF_REG_MAX_NAME 40
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Register set registration record type.
*/
typedef enum DBGFREGSETTYPE
{
/** Invalid zero value. */
DBGFREGSETTYPE_INVALID = 0,
/** CPU record. */
DBGFREGSETTYPE_CPU,
/** Device record. */
DBGFREGSETTYPE_DEVICE,
/** End of valid record types. */
DBGFREGSETTYPE_END
} DBGFREGSETTYPE;
/**
* Register set registration record.
*/
typedef struct DBGFREGSET
{
/** String space core. */
RTSTRSPACECORE Core;
/** The registration record type. */
DBGFREGSETTYPE enmType;
/** The user argument for the callbacks. */
union
{
/** The CPU view. */
PVMCPU pVCpu;
/** The device view. */
PPDMDEVINS pDevIns;
/** The general view. */
void *pv;
} uUserArg;
/** The register descriptors. */
PCDBGFREGDESC paDescs;
/** The number of register descriptors. */
uint32_t cDescs;
/** Array of lookup records.
* The first part of the array runs parallel to paDescs, the rest are
* covering for aliases and bitfield variations. It's done this way to
* simplify the query all operations. */
struct DBGFREGLOOKUP *paLookupRecs;
/** The number of lookup records. */
uint32_t cLookupRecs;
/** The register name prefix. */
char szPrefix[1];
} DBGFREGSET;
/** Pointer to a register registration record. */
typedef DBGFREGSET *PDBGFREGSET;
/** Pointer to a const register registration record. */
typedef DBGFREGSET const *PCDBGFREGSET;
/**
* Register lookup record.
*/
typedef struct DBGFREGLOOKUP
{
/** The string space core. */
RTSTRSPACECORE Core;
/** Pointer to the set. */
PCDBGFREGSET pSet;
/** Pointer to the register descriptor. */
PCDBGFREGDESC pDesc;
/** If an alias this points to the alias descriptor, NULL if not. */
PCDBGFREGALIAS pAlias;
/** If a sub-field this points to the sub-field descriptor, NULL if not. */
PCDBGFREGSUBFIELD pSubField;
} DBGFREGLOOKUP;
/** Pointer to a register lookup record. */
typedef DBGFREGLOOKUP *PDBGFREGLOOKUP;
/** Pointer to a const register lookup record. */
typedef DBGFREGLOOKUP const *PCDBGFREGLOOKUP;
/**
* Argument packet from DBGFR3RegNmQueryAll to dbgfR3RegNmQueryAllWorker.
*/
typedef struct DBGFR3REGNMQUERYALLARGS
{
/** The output register array. */
PDBGFREGENTRYNM paRegs;
/** The number of entries in the output array. */
size_t cRegs;
/** The current register number when enumerating the string space. */
size_t iReg;
} DBGFR3REGNMQUERYALLARGS;
/** Pointer to a dbgfR3RegNmQueryAllWorker argument packet. */
typedef DBGFR3REGNMQUERYALLARGS *PDBGFR3REGNMQUERYALLARGS;
/**
* Argument packet passed by DBGFR3RegNmPrintfV to dbgfR3RegNmPrintfCbOutput
* and dbgfR3RegNmPrintfCbFormat.
*/
typedef struct DBGFR3REGNMPRINTFARGS
{
/** The VM handle. */
PVM pVM;
/** The target CPU. */
VMCPUID idCpu;
/** The output buffer. */
char *pszBuf;
/** The format string. */
const char *pszFormat;
/** The va list with format arguments. */
va_list va;
/** The current buffer offset. */
size_t offBuf;
/** The amount of buffer space left, not counting the terminator char. */
size_t cchLeftBuf;
/** The status code of the whole operation. First error is return,
* subsequent ones are suppressed. */
int rc;
} DBGFR3REGNMPRINTFARGS;
/** Pointer to a DBGFR3RegNmPrintfV argument packet. */
typedef DBGFR3REGNMPRINTFARGS *PDBGFR3REGNMPRINTFARGS;
/**
* Initializes the register database.
*
* @returns VBox status code.
* @param pVM The VM handle.
*/
int dbgfR3RegInit(PVM pVM)
{
int rc = VINF_SUCCESS;
if (!pVM->dbgf.s.fRegDbInitialized)
{
rc = RTSemRWCreate(&pVM->dbgf.s.hRegDbLock);
pVM->dbgf.s.fRegDbInitialized = RT_SUCCESS(rc);
}
return rc;
}
/**
* Terminates the register database.
*
* @param pVM The VM handle.
*/
void dbgfR3RegTerm(PVM pVM)
{
RTSemRWDestroy(pVM->dbgf.s.hRegDbLock);
pVM->dbgf.s.hRegDbLock = NIL_RTSEMRW;
pVM->dbgf.s.fRegDbInitialized = false;
}
/**
* 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.
* @param chDot Set to '.' if accepted, otherwise 0.
*/
static bool dbgfR3RegIsNameValid(const char *pszName, char chDot)
{
const char *psz = pszName;
if (!RT_C_IS_ALPHA(*psz))
return false;
char ch;
while ((ch = *++psz))
if ( !RT_C_IS_LOWER(ch)
&& !RT_C_IS_DIGIT(ch)
&& ch != '_'
&& ch != chDot)
return false;
if (psz - pszName > DBGF_REG_MAX_NAME)
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(dbgfR3RegIsNameValid(pszPrefix, 0), ("%s\n", pszPrefix), VERR_INVALID_NAME);
const char *psz = RTStrEnd(pszPrefix, RTSTR_MAX);
bool const fNeedUnderscore = RT_C_IS_DIGIT(psz[-1]);
size_t const cchPrefix = psz - pszPrefix + fNeedUnderscore;
AssertMsgReturn(cchPrefix < RT_SIZEOFMEMB(DBGFREGSET, szPrefix) - 4 - 1, ("%s\n", pszPrefix), VERR_INVALID_NAME);
AssertMsgReturn(iInstance <= 9999, ("%d\n", iInstance), VERR_INVALID_NAME);
/* The descriptors. */
uint32_t cLookupRecs = 0;
uint32_t iDesc;
for (iDesc = 0; paRegisters[iDesc].pszName != NULL; iDesc++)
{
AssertMsgReturn(dbgfR3RegIsNameValid(paRegisters[iDesc].pszName, 0), ("%s (#%u)\n", paRegisters[iDesc].pszName, iDesc), VERR_INVALID_NAME);
if (enmType == DBGFREGSETTYPE_CPU)
AssertMsgReturn((unsigned)paRegisters[iDesc].enmReg == iDesc && iDesc < (unsigned)DBGFREG_END,
("%d iDesc=%d\n", paRegisters[iDesc].enmReg, iDesc),
VERR_INVALID_PARAMETER);
else
AssertReturn(paRegisters[iDesc].enmReg == DBGFREG_END, VERR_INVALID_PARAMETER);
AssertReturn( paRegisters[iDesc].enmType > DBGFREGVALTYPE_INVALID
&& paRegisters[iDesc].enmType < DBGFREGVALTYPE_END, VERR_INVALID_PARAMETER);
AssertMsgReturn(!(paRegisters[iDesc].fFlags & ~DBGFREG_FLAGS_READ_ONLY),
("%#x (#%u)\n", paRegisters[iDesc].fFlags, iDesc),
VERR_INVALID_PARAMETER);
AssertPtrReturn(paRegisters[iDesc].pfnGet, VERR_INVALID_PARAMETER);
AssertPtrReturn(paRegisters[iDesc].pfnSet, VERR_INVALID_PARAMETER);
uint32_t iAlias = 0;
PCDBGFREGALIAS paAliases = paRegisters[iDesc].paAliases;
if (paAliases)
{
AssertPtrReturn(paAliases, VERR_INVALID_PARAMETER);
for (; paAliases[iAlias].pszName; iAlias++)
{
AssertMsgReturn(dbgfR3RegIsNameValid(paAliases[iAlias].pszName, 0), ("%s (%s)\n", paAliases[iAlias].pszName, paRegisters[iDesc].pszName), VERR_INVALID_NAME);
AssertReturn( paAliases[iAlias].enmType > DBGFREGVALTYPE_INVALID
&& paAliases[iAlias].enmType < DBGFREGVALTYPE_END, VERR_INVALID_PARAMETER);
}
}
uint32_t iSubField = 0;
PCDBGFREGSUBFIELD paSubFields = paRegisters[iDesc].paSubFields;
if (paSubFields)
{
AssertPtrReturn(paSubFields, VERR_INVALID_PARAMETER);
for (; paSubFields[iSubField].pszName; iSubField++)
{
AssertMsgReturn(dbgfR3RegIsNameValid(paSubFields[iSubField].pszName, '.'), ("%s (%s)\n", paSubFields[iSubField].pszName, paRegisters[iDesc].pszName), VERR_INVALID_NAME);
AssertReturn(paSubFields[iSubField].iFirstBit + paSubFields[iSubField].cBits <= 128, VERR_INVALID_PARAMETER);
AssertReturn(paSubFields[iSubField].cBits + paSubFields[iSubField].cShift <= 128, VERR_INVALID_PARAMETER);
AssertPtrNullReturn(paSubFields[iSubField].pfnGet, VERR_INVALID_POINTER);
AssertPtrNullReturn(paSubFields[iSubField].pfnSet, VERR_INVALID_POINTER);
}
}
cLookupRecs += (1 + iAlias) * (1 + iSubField);
}
/* Check the instance number of the CPUs. */
AssertReturn(enmType != DBGFREGSETTYPE_CPU || iInstance < pVM->cCpus, VERR_INVALID_CPU_ID);
/*
* Allocate a new record and all associated lookup records.
*/
size_t cbRegSet = RT_OFFSETOF(DBGFREGSET, szPrefix[cchPrefix + 4 + 1]);
cbRegSet = RT_ALIGN_Z(cbRegSet, 32);
size_t const offLookupRecArray = cbRegSet;
cbRegSet += cLookupRecs * sizeof(DBGFREGLOOKUP);
PDBGFREGSET pRegSet = (PDBGFREGSET)MMR3HeapAllocZ(pVM, MM_TAG_DBGF_REG, cbRegSet);
if (!pRegSet)
return VERR_NO_MEMORY;
/*
* Initialize the new record.
*/
pRegSet->Core.pszString = pRegSet->szPrefix;
pRegSet->enmType = enmType;
pRegSet->uUserArg.pv = pvUserArg;
pRegSet->paDescs = paRegisters;
pRegSet->cDescs = iDesc;
pRegSet->cLookupRecs = cLookupRecs;
pRegSet->paLookupRecs = (PDBGFREGLOOKUP)((uintptr_t)pRegSet + offLookupRecArray);
if (fNeedUnderscore)
RTStrPrintf(pRegSet->szPrefix, cchPrefix + 4 + 1, "%s_%u", pszPrefix, iInstance);
else
RTStrPrintf(pRegSet->szPrefix, cchPrefix + 4 + 1, "%s%u", pszPrefix, iInstance);
/*
* Initialize the lookup records. See DBGFREGSET::paLookupRecs.
*/
char szName[DBGF_REG_MAX_NAME * 3 + 16];
strcpy(szName, pRegSet->szPrefix);
char *pszReg = strchr(szName, '\0');
*pszReg++ = '.';
/* Array parallel to the descriptors. */
int rc = VINF_SUCCESS;
PDBGFREGLOOKUP pLookupRec = &pRegSet->paLookupRecs[0];
for (iDesc = 0; paRegisters[iDesc].pszName != NULL && RT_SUCCESS(rc); iDesc++)
{
strcpy(pszReg, paRegisters[iDesc].pszName);
pLookupRec->Core.pszString = MMR3HeapStrDup(pVM, MM_TAG_DBGF_REG, szName);
if (!pLookupRec->Core.pszString)
rc = VERR_NO_STR_MEMORY;
pLookupRec->pSet = pRegSet;
pLookupRec->pDesc = &paRegisters[iDesc];
pLookupRec->pAlias = NULL;
pLookupRec->pSubField = NULL;
pLookupRec++;
}
/* Aliases and sub-fields. */
for (iDesc = 0; paRegisters[iDesc].pszName != NULL && RT_SUCCESS(rc); iDesc++)
{
PCDBGFREGALIAS pCurAlias = NULL; /* first time we add sub-fields for the real name. */
PCDBGFREGALIAS pNextAlias = paRegisters[iDesc].paAliases;
const char *pszRegName = paRegisters[iDesc].pszName;
while (RT_SUCCESS(rc))
{
/* Add sub-field records. */
PCDBGFREGSUBFIELD paSubFields = paRegisters[iDesc].paSubFields;
if (paSubFields)
{
size_t cchReg = strlen(pszRegName);
memcpy(pszReg, pszRegName, cchReg);
char *pszSub = &pszReg[cchReg];
*pszSub++ = '.';
for (uint32_t iSubField = 0; paSubFields[iSubField].pszName && RT_SUCCESS(rc); iSubField++)
{
strcpy(pszSub, paSubFields[iSubField].pszName);
pLookupRec->Core.pszString = MMR3HeapStrDup(pVM, MM_TAG_DBGF_REG, szName);
if (!pLookupRec->Core.pszString)
rc = VERR_NO_STR_MEMORY;
pLookupRec->pSet = pRegSet;
pLookupRec->pDesc = &paRegisters[iDesc];
pLookupRec->pAlias = pCurAlias;
pLookupRec->pSubField = &paSubFields[iSubField];
pLookupRec++;
}
}
/* Advance to the next alias. */
pCurAlias = pNextAlias++;
if (!pCurAlias)
break;
pszRegName = pCurAlias->pszName;
if (!pszRegName)
break;
/* The alias record. */
strcpy(pszReg, pszRegName);
pLookupRec->Core.pszString = MMR3HeapStrDup(pVM, MM_TAG_DBGF_REG, szName);
if (!pLookupRec->Core.pszString)
rc = VERR_NO_STR_MEMORY;
pLookupRec->pSet = pRegSet;
pLookupRec->pDesc = &paRegisters[iDesc];
pLookupRec->pAlias = pCurAlias;
pLookupRec->pSubField = NULL;
pLookupRec++;
}
}
Assert(pLookupRec == &pRegSet->paLookupRecs[pRegSet->cLookupRecs]);
if (RT_SUCCESS(rc))
{
/*
* Insert the record into the register set string space and optionally into
* the CPU register set cache.
*/
DBGF_REG_DB_LOCK_WRITE(pVM);
bool fInserted = RTStrSpaceInsert(&pVM->dbgf.s.RegSetSpace, &pRegSet->Core);
if (fInserted)
{
pVM->dbgf.s.cRegs += pRegSet->cDescs;
if (enmType == DBGFREGSETTYPE_CPU)
{
if (pRegSet->cDescs > DBGFREG_ALL_COUNT)
pVM->dbgf.s.cRegs -= pRegSet->cDescs - DBGFREG_ALL_COUNT;
pVM->aCpus[iInstance].dbgf.s.pRegSet = pRegSet;
}
PDBGFREGLOOKUP paLookupRecs = pRegSet->paLookupRecs;
uint32_t iLookupRec = pRegSet->cLookupRecs;
while (iLookupRec-- > 0)
{
bool fInserted2 = RTStrSpaceInsert(&pVM->dbgf.s.RegSpace, &paLookupRecs[iLookupRec].Core);
AssertMsg(fInserted2, ("'%s'", paLookupRecs[iLookupRec].Core.pszString));
}
DBGF_REG_DB_UNLOCK_WRITE(pVM);
return VINF_SUCCESS;
}
DBGF_REG_DB_UNLOCK_WRITE(pVM);
rc = VERR_DUPLICATE;
}
/*
* Bail out.
*/
for (uint32_t i = 0; i < pRegSet->cLookupRecs; i++)
MMR3HeapFree((char *)pRegSet->paLookupRecs[i].Core.pszString);
MMR3HeapFree(pRegSet);
return rc;
}
/**
* Registers a set of registers for a CPU.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pVCpu The virtual CPU handle.
* @param paRegisters The register descriptors.
*/
VMMR3_INT_DECL(int) DBGFR3RegRegisterCpu(PVM pVM, PVMCPU pVCpu, PCDBGFREGDESC paRegisters)
{
if (!pVM->dbgf.s.fRegDbInitialized)
{
int rc = dbgfR3RegInit(pVM);
if (RT_FAILURE(rc))
return rc;
}
return dbgfR3RegRegisterCommon(pVM, 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)
{
VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
AssertPtrReturn(paRegisters, VERR_INVALID_POINTER);
AssertPtrReturn(pDevIns, VERR_INVALID_POINTER);
AssertPtrReturn(pszPrefix, VERR_INVALID_POINTER);
return dbgfR3RegRegisterCommon(pVM, paRegisters, DBGFREGSETTYPE_DEVICE, pDevIns, pszPrefix, iInstance);
}
/**
* Clears the register value variable.
*
* @param pValue The variable to clear.
*/
DECLINLINE(void) dbgfR3RegValClear(PDBGFREGVAL pValue)
{
pValue->au64[0] = 0;
pValue->au64[1] = 0;
}
/**
* Sets a 80-bit floating point variable to a 64-bit unsigned interger value.
*
* @param pValue The value.
* @param u64 The integer value.
*/
DECLINLINE(void) dbgfR3RegValR80SetU64(PDBGFREGVAL pValue, uint64_t u64)
{
/** @todo fixme */
pValue->r80.s.fSign = 0;
pValue->r80.s.uExponent = 16383;
pValue->r80.s.u64Mantissa = u64;
}
/**
* Sets a 80-bit floating point variable to a 64-bit unsigned interger value.
*
* @param pValue The value.
* @param u128 The integer value.
*/
DECLINLINE(void) dbgfR3RegValR80SetU128(PDBGFREGVAL pValue, RTUINT128U u128)
{
/** @todo fixme */
pValue->r80.s.fSign = 0;
pValue->r80.s.uExponent = 16383;
pValue->r80.s.u64Mantissa = u128.s.Lo;
}
/**
* Get a 80-bit floating point variable as a 64-bit unsigned integer.
*
* @returns 64-bit unsigned integer.
* @param pValue The value.
*/
DECLINLINE(uint64_t) dbgfR3RegValR80GetU64(PCDBGFREGVAL pValue)
{
/** @todo stupid, stupid MSC. */
return pValue->r80.s.u64Mantissa;
}
/**
* Get a 80-bit floating point variable as a 128-bit unsigned integer.
*
* @returns 128-bit unsigned integer.
* @param pValue The value.
*/
DECLINLINE(RTUINT128U) dbgfR3RegValR80GetU128(PCDBGFREGVAL pValue)
{
/** @todo stupid, stupid MSC. */
RTUINT128U uRet;
#if 0
uRet.s.Lo = (uint64_t)InVal.lrd;
uRet.s.Hi = (uint64_t)InVal.lrd / _4G / _4G;
#else
uRet.s.Lo = pValue->r80.s.u64Mantissa;
uRet.s.Hi = 0;
#endif
return uRet;
}
/**
* 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)
{
DBGFREGVAL const InVal = *pValue;
dbgfR3RegValClear(pValue);
/* Note! No default cases here as gcc warnings about missing enum values
are desired. */
switch (enmFromType)
{
case DBGFREGVALTYPE_U8:
switch (enmToType)
{
case DBGFREGVALTYPE_U8: pValue->u8 = InVal.u8; return VINF_SUCCESS;
case DBGFREGVALTYPE_U16: pValue->u16 = InVal.u8; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
case DBGFREGVALTYPE_U32: pValue->u32 = InVal.u8; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
case DBGFREGVALTYPE_U64: pValue->u64 = InVal.u8; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
case DBGFREGVALTYPE_U128: pValue->u128.s.Lo = InVal.u8; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
case DBGFREGVALTYPE_R80: dbgfR3RegValR80SetU64(pValue, InVal.u8); return VINF_DBGF_ZERO_EXTENDED_REGISTER;
case DBGFREGVALTYPE_DTR: return VERR_DBGF_UNSUPPORTED_CAST;
case DBGFREGVALTYPE_32BIT_HACK:
case DBGFREGVALTYPE_END:
case DBGFREGVALTYPE_INVALID:
break;
}
break;
case DBGFREGVALTYPE_U16:
switch (enmToType)
{
case DBGFREGVALTYPE_U8: pValue->u8 = InVal.u16; return VINF_DBGF_TRUNCATED_REGISTER;
case DBGFREGVALTYPE_U16: pValue->u16 = InVal.u16; return VINF_SUCCESS;
case DBGFREGVALTYPE_U32: pValue->u32 = InVal.u16; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
case DBGFREGVALTYPE_U64: pValue->u64 = InVal.u16; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
case DBGFREGVALTYPE_U128: pValue->u128.s.Lo = InVal.u16; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
case DBGFREGVALTYPE_R80: dbgfR3RegValR80SetU64(pValue, InVal.u16); return VINF_DBGF_ZERO_EXTENDED_REGISTER;
case DBGFREGVALTYPE_DTR: return VERR_DBGF_UNSUPPORTED_CAST;
case DBGFREGVALTYPE_32BIT_HACK:
case DBGFREGVALTYPE_END:
case DBGFREGVALTYPE_INVALID:
break;
}
break;
case DBGFREGVALTYPE_U32:
switch (enmToType)
{
case DBGFREGVALTYPE_U8: pValue->u8 = InVal.u32; return VINF_DBGF_TRUNCATED_REGISTER;
case DBGFREGVALTYPE_U16: pValue->u16 = InVal.u32; return VINF_DBGF_TRUNCATED_REGISTER;
case DBGFREGVALTYPE_U32: pValue->u32 = InVal.u32; return VINF_SUCCESS;
case DBGFREGVALTYPE_U64: pValue->u64 = InVal.u32; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
case DBGFREGVALTYPE_U128: pValue->u128.s.Lo = InVal.u32; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
case DBGFREGVALTYPE_R80: dbgfR3RegValR80SetU64(pValue, InVal.u32); return VINF_DBGF_ZERO_EXTENDED_REGISTER;
case DBGFREGVALTYPE_DTR: return VERR_DBGF_UNSUPPORTED_CAST;
case DBGFREGVALTYPE_32BIT_HACK:
case DBGFREGVALTYPE_END:
case DBGFREGVALTYPE_INVALID:
break;
}
break;
case DBGFREGVALTYPE_U64:
switch (enmToType)
{
case DBGFREGVALTYPE_U8: pValue->u8 = InVal.u64; return VINF_DBGF_TRUNCATED_REGISTER;
case DBGFREGVALTYPE_U16: pValue->u16 = InVal.u64; return VINF_DBGF_TRUNCATED_REGISTER;
case DBGFREGVALTYPE_U32: pValue->u32 = InVal.u64; return VINF_DBGF_TRUNCATED_REGISTER;
case DBGFREGVALTYPE_U64: pValue->u64 = InVal.u64; return VINF_SUCCESS;
case DBGFREGVALTYPE_U128: pValue->u128.s.Lo = InVal.u64; return VINF_DBGF_TRUNCATED_REGISTER;
case DBGFREGVALTYPE_R80: dbgfR3RegValR80SetU64(pValue, InVal.u64); return VINF_DBGF_TRUNCATED_REGISTER;
case DBGFREGVALTYPE_DTR: return VERR_DBGF_UNSUPPORTED_CAST;
case DBGFREGVALTYPE_32BIT_HACK:
case DBGFREGVALTYPE_END:
case DBGFREGVALTYPE_INVALID:
break;
}
break;
case DBGFREGVALTYPE_U128:
switch (enmToType)
{
case DBGFREGVALTYPE_U8: pValue->u8 = InVal.u128.s.Lo; return VINF_DBGF_TRUNCATED_REGISTER;
case DBGFREGVALTYPE_U16: pValue->u16 = InVal.u128.s.Lo; return VINF_DBGF_TRUNCATED_REGISTER;
case DBGFREGVALTYPE_U32: pValue->u32 = InVal.u128.s.Lo; return VINF_DBGF_TRUNCATED_REGISTER;
case DBGFREGVALTYPE_U64: pValue->u64 = InVal.u128.s.Lo; return VINF_DBGF_TRUNCATED_REGISTER;
case DBGFREGVALTYPE_U128: pValue->u128 = InVal.u128; return VINF_SUCCESS;
case DBGFREGVALTYPE_R80: dbgfR3RegValR80SetU128(pValue, InVal.u128); return VINF_DBGF_TRUNCATED_REGISTER;
case DBGFREGVALTYPE_DTR: return VERR_DBGF_UNSUPPORTED_CAST;
case DBGFREGVALTYPE_32BIT_HACK:
case DBGFREGVALTYPE_END:
case DBGFREGVALTYPE_INVALID:
break;
}
break;
case DBGFREGVALTYPE_R80:
switch (enmToType)
{
case DBGFREGVALTYPE_U8: pValue->u8 = (uint8_t )dbgfR3RegValR80GetU64(&InVal); return VINF_DBGF_TRUNCATED_REGISTER;
case DBGFREGVALTYPE_U16: pValue->u16 = (uint16_t)dbgfR3RegValR80GetU64(&InVal); return VINF_DBGF_TRUNCATED_REGISTER;
case DBGFREGVALTYPE_U32: pValue->u32 = (uint32_t)dbgfR3RegValR80GetU64(&InVal); return VINF_DBGF_TRUNCATED_REGISTER;
case DBGFREGVALTYPE_U64: pValue->u64 = (uint64_t)dbgfR3RegValR80GetU64(&InVal); return VINF_DBGF_TRUNCATED_REGISTER;
case DBGFREGVALTYPE_U128: pValue->u128 = dbgfR3RegValR80GetU128(&InVal); return VINF_DBGF_TRUNCATED_REGISTER;
case DBGFREGVALTYPE_R80: pValue->r80 = InVal.r80; return VINF_SUCCESS;
case DBGFREGVALTYPE_DTR: return VERR_DBGF_UNSUPPORTED_CAST;
case DBGFREGVALTYPE_32BIT_HACK:
case DBGFREGVALTYPE_END:
case DBGFREGVALTYPE_INVALID:
break;
}
break;
case DBGFREGVALTYPE_DTR:
switch (enmToType)
{
case DBGFREGVALTYPE_U8: pValue->u8 = InVal.dtr.u64Base; return VINF_DBGF_TRUNCATED_REGISTER;
case DBGFREGVALTYPE_U16: pValue->u16 = InVal.dtr.u64Base; return VINF_DBGF_TRUNCATED_REGISTER;
case DBGFREGVALTYPE_U32: pValue->u32 = InVal.dtr.u64Base; return VINF_DBGF_TRUNCATED_REGISTER;
case DBGFREGVALTYPE_U64: pValue->u64 = InVal.dtr.u64Base; return VINF_DBGF_TRUNCATED_REGISTER;
case DBGFREGVALTYPE_U128: pValue->u128.s.Lo = InVal.dtr.u64Base; return VINF_DBGF_TRUNCATED_REGISTER;
case DBGFREGVALTYPE_R80: dbgfR3RegValR80SetU64(pValue, InVal.dtr.u64Base); return VINF_DBGF_TRUNCATED_REGISTER;
case DBGFREGVALTYPE_DTR: pValue->dtr = InVal.dtr; return VINF_SUCCESS;
case DBGFREGVALTYPE_32BIT_HACK:
case DBGFREGVALTYPE_END:
case DBGFREGVALTYPE_INVALID:
break;
}
break;
case DBGFREGVALTYPE_INVALID:
case DBGFREGVALTYPE_END:
case DBGFREGVALTYPE_32BIT_HACK:
break;
}
AssertMsgFailed(("%d / %d\n", enmFromType, enmToType));
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_REGISTER_NOT_FOUND
* @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 pValue Where to return the register value.
*/
static DECLCALLBACK(int) dbgfR3RegCpuQueryWorker(PVM pVM, VMCPUID idCpu, DBGFREG enmReg, DBGFREGVALTYPE enmType, PDBGFREGVAL pValue)
{
int rc = VINF_SUCCESS;
DBGF_REG_DB_LOCK_READ(pVM);
/*
* Look up the register set of the specified CPU.
*/
PDBGFREGSET pSet = pVM->aCpus[idCpu].dbgf.s.pRegSet;
if (RT_LIKELY(pSet))
{
/*
* Look up the register and get the register value.
*/
if (RT_LIKELY(pSet->cDescs > (size_t)enmReg))
{
PCDBGFREGDESC pDesc = &pSet->paDescs[enmReg];
pValue->au64[0] = pValue->au64[1] = 0;
rc = pDesc->pfnGet(pSet->uUserArg.pv, pDesc, pValue);
if (RT_SUCCESS(rc))
{
/*
* Do the cast if the desired return type doesn't match what
* the getter returned.
*/
if (pDesc->enmType == enmType)
rc = VINF_SUCCESS;
else
rc = dbgfR3RegValCast(pValue, pDesc->enmType, enmType);
}
}
else
rc = VERR_DBGF_REGISTER_NOT_FOUND;
}
else
rc = VERR_INVALID_CPU_ID;
DBGF_REG_DB_UNLOCK_READ(pVM);
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_REGISTER_NOT_FOUND
* @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.
*/
VMMR3DECL(int) DBGFR3RegCpuQueryU8(PVM pVM, VMCPUID idCpu, DBGFREG enmReg, uint8_t *pu8)
{
VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
DBGFREGVAL Value;
int rc = VMR3ReqCallWait(pVM, idCpu, (PFNRT)dbgfR3RegCpuQueryWorker, 5, pVM, idCpu, enmReg, DBGFREGVALTYPE_U8, &Value);
if (RT_SUCCESS(rc))
*pu8 = Value.u8;
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_REGISTER_NOT_FOUND
* @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.
*/
VMMR3DECL(int) DBGFR3RegCpuQueryU16(PVM pVM, VMCPUID idCpu, DBGFREG enmReg, uint16_t *pu16)
{
VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
DBGFREGVAL Value;
int rc = VMR3ReqCallWait(pVM, idCpu, (PFNRT)dbgfR3RegCpuQueryWorker, 5, pVM, idCpu, enmReg, DBGFREGVALTYPE_U16, &Value);
if (RT_SUCCESS(rc))
*pu16 = Value.u16;
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_REGISTER_NOT_FOUND
* @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.
*/
VMMR3DECL(int) DBGFR3RegCpuQueryU32(PVM pVM, VMCPUID idCpu, DBGFREG enmReg, uint32_t *pu32)
{
VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
DBGFREGVAL Value;
int rc = VMR3ReqCallWait(pVM, idCpu, (PFNRT)dbgfR3RegCpuQueryWorker, 5, pVM, idCpu, enmReg, DBGFREGVALTYPE_U32, &Value);
if (RT_SUCCESS(rc))
*pu32 = Value.u32;
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_REGISTER_NOT_FOUND
* @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.
*/
VMMR3DECL(int) DBGFR3RegCpuQueryU64(PVM pVM, VMCPUID idCpu, DBGFREG enmReg, uint64_t *pu64)
{
VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
DBGFREGVAL Value;
int rc = VMR3ReqCallWait(pVM, idCpu, (PFNRT)dbgfR3RegCpuQueryWorker, 5, pVM, idCpu, enmReg, DBGFREGVALTYPE_U64, &Value);
if (RT_SUCCESS(rc))
*pu64 = Value.u64;
else
*pu64 = 0;
return rc;
}
/**
* Wrapper around CPUMQueryGuestMsr for dbgfR3RegCpuQueryBatchWorker.
*
* @retval VINF_SUCCESS
* @retval VERR_DBGF_REGISTER_NOT_FOUND
*
* @param pVCpu The current CPU.
* @param pReg The where to store the register value and
* size.
* @param idMsr The MSR to get.
*/
static void dbgfR3RegGetMsrBatch(PVMCPU pVCpu, PDBGFREGENTRY pReg, uint32_t idMsr)
{
pReg->enmType = DBGFREGVALTYPE_U64;
int rc = CPUMQueryGuestMsr(pVCpu, idMsr, &pReg->Val.u64);
if (RT_FAILURE(rc))
{
AssertMsg(rc == VERR_CPUM_RAISE_GP_0, ("%Rrc\n", rc));
pReg->Val.u64 = 0;
}
}
static DECLCALLBACK(int) dbgfR3RegCpuQueryBatchWorker(PVM pVM, VMCPUID idCpu, PDBGFREGENTRY paRegs, size_t cRegs)
{
#if 0
PVMCPU pVCpu = &pVM->aCpus[idCpu];
PCCPUMCTX pCtx = CPUMQueryGuestCtxPtr(pVCpu);
PDBGFREGENTRY pReg = paRegs - 1;
while (cRegs-- > 0)
{
pReg++;
pReg->Val.au64[0] = 0;
pReg->Val.au64[1] = 0;
DBGFREG const enmReg = pReg->enmReg;
AssertMsgReturn(enmReg >= 0 && enmReg <= DBGFREG_END, ("%d (%#x)\n", enmReg, enmReg), VERR_DBGF_REGISTER_NOT_FOUND);
if (enmReg != DBGFREG_END)
{
PCDBGFREGDESC pDesc = &g_aDbgfRegDescs[enmReg];
if (!pDesc->pfnGet)
{
PCRTUINT128U pu = (PCRTUINT128U)((uintptr_t)pCtx + pDesc->offCtx);
pReg->enmType = pDesc->enmType;
switch (pDesc->enmType)
{
case DBGFREGVALTYPE_U8: pReg->Val.u8 = pu->au8[0]; break;
case DBGFREGVALTYPE_U16: pReg->Val.u16 = pu->au16[0]; break;
case DBGFREGVALTYPE_U32: pReg->Val.u32 = pu->au32[0]; break;
case DBGFREGVALTYPE_U64: pReg->Val.u64 = pu->au64[0]; break;
case DBGFREGVALTYPE_U128:
pReg->Val.au64[0] = pu->au64[0];
pReg->Val.au64[1] = pu->au64[1];
break;
case DBGFREGVALTYPE_R80:
pReg->Val.au64[0] = pu->au64[0];
pReg->Val.au16[5] = pu->au16[5];
break;
default:
AssertMsgFailedReturn(("%s %d\n", pDesc->pszName, pDesc->enmType), VERR_INTERNAL_ERROR_3);
}
}
else
{
int rc = pDesc->pfnGet(pVCpu, pDesc, pCtx, &pReg->Val.u);
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_REGISTER_NOT_FOUND
*
* @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.
*/
VMMR3DECL(int) DBGFR3RegCpuQueryBatch(PVM pVM, VMCPUID idCpu, PDBGFREGENTRY paRegs, size_t cRegs)
{
VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
if (!cRegs)
return VINF_SUCCESS;
AssertReturn(cRegs < _1M, VERR_OUT_OF_RANGE);
AssertPtrReturn(paRegs, VERR_INVALID_POINTER);
size_t iReg = cRegs;
while (iReg-- > 0)
{
DBGFREG enmReg = paRegs[iReg].enmReg;
AssertMsgReturn(enmReg < DBGFREG_END && enmReg >= DBGFREG_AL, ("%d (%#x)", enmReg, enmReg), VERR_DBGF_REGISTER_NOT_FOUND);
}
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.
*/
VMMR3DECL(int) DBGFR3RegCpuQueryAll(PVM pVM, VMCPUID idCpu, PDBGFREGENTRY paRegs, size_t cRegs)
{
/*
* Validate input.
*/
VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
if (!cRegs)
return VINF_SUCCESS;
AssertReturn(cRegs < _1M, VERR_OUT_OF_RANGE);
AssertPtrReturn(paRegs, VERR_INVALID_POINTER);
/*
* Convert it into a batch query (lazy bird).
*/
unsigned iReg = 0;
while (iReg < cRegs && iReg < DBGFREG_ALL_COUNT)
{
paRegs[iReg].enmReg = (DBGFREG)iReg;
iReg++;
}
while (iReg < cRegs)
paRegs[iReg++].enmReg = DBGFREG_END;
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 pVM The VM handle.
* @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(const char *) DBGFR3RegCpuName(PVM pVM, DBGFREG enmReg, DBGFREGVALTYPE enmType)
{
AssertReturn(enmReg >= DBGFREG_AL && enmReg < DBGFREG_END, NULL);
AssertReturn(enmType >= DBGFREGVALTYPE_INVALID && enmType < DBGFREGVALTYPE_END, NULL);
VM_ASSERT_VALID_EXT_RETURN(pVM, NULL);
PCDBGFREGSET pSet = pVM->aCpus[0].dbgf.s.pRegSet;
if (RT_UNLIKELY(!pSet))
return NULL;
PCDBGFREGDESC pDesc = &pSet->paDescs[enmReg];
PCDBGFREGALIAS pAlias = pDesc->paAliases;
if ( pAlias
&& pDesc->enmType != enmType
&& enmType != DBGFREGVALTYPE_INVALID)
{
while (pAlias->pszName)
{
if (pAlias->enmType == enmType)
return pAlias->pszName;
pAlias++;
}
}
return pDesc->pszName;
}
/**
* Fold the string to lower case and copy it into the destination buffer.
*
* @returns Number of folder characters, -1 on overflow.
* @param pszSrc The source string.
* @param cchSrc How much to fold and copy.
* @param pszDst The output buffer.
* @param cbDst The size of the output buffer.
*/
static ssize_t dbgfR3RegCopyToLower(const char *pszSrc, size_t cchSrc, char *pszDst, size_t cbDst)
{
ssize_t cchFolded = 0;
char ch;
while (cchSrc-- > 0 && (ch = *pszSrc++))
{
if (RT_UNLIKELY(cbDst <= 1))
return -1;
cbDst--;
char chLower = RT_C_TO_LOWER(ch);
cchFolded += chLower != ch;
*pszDst++ = chLower;
}
if (RT_UNLIKELY(!cbDst))
return -1;
*pszDst = '\0';
return cchFolded;
}
/**
* Resolves the register name.
*
* @returns Lookup record.
* @param pVM The VM handle.
* @param idDefCpu The default CPU ID set.
* @param pszReg The register name.
*/
static PCDBGFREGLOOKUP dbgfR3RegResolve(PVM pVM, VMCPUID idDefCpu, const char *pszReg)
{
DBGF_REG_DB_LOCK_READ(pVM);
/* Try looking up the name without any case folding or cpu prefixing. */
PCDBGFREGLOOKUP pLookupRec = (PCDBGFREGLOOKUP)RTStrSpaceGet(&pVM->dbgf.s.RegSpace, pszReg);
if (!pLookupRec)
{
char szName[DBGF_REG_MAX_NAME * 4 + 16];
/* Lower case it and try again. */
ssize_t cchFolded = dbgfR3RegCopyToLower(pszReg, RTSTR_MAX, szName, sizeof(szName) - DBGF_REG_MAX_NAME);
if (cchFolded > 0)
pLookupRec = (PCDBGFREGLOOKUP)RTStrSpaceGet(&pVM->dbgf.s.RegSpace, szName);
if ( !pLookupRec
&& cchFolded >= 0
&& idDefCpu != VMCPUID_ANY)
{
/* Prefix it with the specified CPU set. */
size_t cchCpuSet = RTStrPrintf(szName, sizeof(szName), "cpu%u.", idDefCpu);
dbgfR3RegCopyToLower(pszReg, RTSTR_MAX, &szName[cchCpuSet], sizeof(szName) - cchCpuSet);
pLookupRec = (PCDBGFREGLOOKUP)RTStrSpaceGet(&pVM->dbgf.s.RegSpace, szName);
}
}
DBGF_REG_DB_UNLOCK_READ(pVM);
return pLookupRec;
}
/**
* On CPU worker for the register queries, used by dbgfR3RegNmQueryWorker and
* dbgfR3RegNmPrintfCbFormat.
*
* @returns VBox status code.
*
* @param pVM The VM handle.
* @param pLookupRec The register lookup record.
* @param enmType The desired return type.
* @param pValue Where to return the register value.
* @param penmType Where to store the register value type.
* Optional.
*/
static DECLCALLBACK(int) dbgfR3RegNmQueryWorkerOnCpu(PVM pVM, PCDBGFREGLOOKUP pLookupRec, DBGFREGVALTYPE enmType,
PDBGFREGVAL pValue, PDBGFREGVALTYPE penmType)
{
PCDBGFREGDESC pDesc = pLookupRec->pDesc;
PCDBGFREGSET pSet = pLookupRec->pSet;
PCDBGFREGSUBFIELD pSubField = pLookupRec->pSubField;
DBGFREGVALTYPE enmValueType = pDesc->enmType;
int rc;
/*
* Get the register or sub-field value.
*/
dbgfR3RegValClear(pValue);
if (!pSubField)
{
rc = pDesc->pfnGet(pSet->uUserArg.pv, pDesc, pValue);
if ( pLookupRec->pAlias
&& pLookupRec->pAlias->enmType != enmValueType
&& RT_SUCCESS(rc))
{
rc = dbgfR3RegValCast(pValue, enmValueType, pLookupRec->pAlias->enmType);
enmValueType = pLookupRec->pAlias->enmType;
}
}
else
{
if (pSubField->pfnGet)
{
rc = pSubField->pfnGet(pSet->uUserArg.pv, pSubField, &pValue->u128);
enmValueType = DBGFREGVALTYPE_U128;
}
else
{
rc = pDesc->pfnGet(pSet->uUserArg.pv, pDesc, pValue);
if ( pLookupRec->pAlias
&& pLookupRec->pAlias->enmType != enmValueType
&& RT_SUCCESS(rc))
{
rc = dbgfR3RegValCast(pValue, enmValueType, pLookupRec->pAlias->enmType);
enmValueType = pLookupRec->pAlias->enmType;
}
if (RT_SUCCESS(rc))
{
rc = dbgfR3RegValCast(pValue, enmValueType, DBGFREGVALTYPE_U128);
if (RT_SUCCESS(rc))
{
RTUInt128AssignShiftLeft(&pValue->u128, -pSubField->iFirstBit);
RTUInt128AssignAndNFirstBits(&pValue->u128, pSubField->cBits);
if (pSubField->cShift)
RTUInt128AssignShiftLeft(&pValue->u128, pSubField->cShift);
}
}
}
if (RT_SUCCESS(rc))
{
unsigned const cBits = pSubField->cBits + pSubField->cShift;
if (cBits <= 8)
enmValueType = DBGFREGVALTYPE_U8;
else if (cBits <= 16)
enmValueType = DBGFREGVALTYPE_U16;
else if (cBits <= 32)
enmValueType = DBGFREGVALTYPE_U32;
else if (cBits <= 64)
enmValueType = DBGFREGVALTYPE_U64;
else
enmValueType = DBGFREGVALTYPE_U128;
rc = dbgfR3RegValCast(pValue, DBGFREGVALTYPE_U128, enmValueType);
}
}
if (RT_SUCCESS(rc))
{
/*
* Do the cast if the desired return type doesn't match what
* the getter returned.
*/
if ( enmValueType == enmType
|| enmType == DBGFREGVALTYPE_END)
{
rc = VINF_SUCCESS;
if (penmType)
*penmType = enmValueType;
}
else
{
rc = dbgfR3RegValCast(pValue, enmValueType, enmType);
if (penmType)
*penmType = RT_SUCCESS(rc) ? enmType : enmValueType;
}
}
return rc;
}
/**
* Worker for the register queries.
*
* @returns VBox status code.
* @retval VINF_SUCCESS
* @retval VERR_INVALID_VM_HANDLE
* @retval VERR_INVALID_CPU_ID
* @retval VERR_DBGF_REGISTER_NOT_FOUND
* @retval VERR_DBGF_UNSUPPORTED_CAST
* @retval VINF_DBGF_TRUNCATED_REGISTER
* @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
*
* @param pVM The VM handle.
* @param idDefCpu The virtual CPU ID for the default CPU register
* set.
* @param pszReg The register to query.
* @param enmType The desired return type.
* @param pValue Where to return the register value.
* @param penmType Where to store the register value type.
* Optional.
*/
static int dbgfR3RegNmQueryWorker(PVM pVM, VMCPUID idDefCpu, const char *pszReg, DBGFREGVALTYPE enmType,
PDBGFREGVAL pValue, PDBGFREGVALTYPE penmType)
{
/*
* Validate input.
*/
VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
AssertReturn(idDefCpu < pVM->cCpus || idDefCpu == VMCPUID_ANY, VERR_INVALID_CPU_ID);
AssertPtrReturn(pszReg, VERR_INVALID_POINTER);
Assert(enmType > DBGFREGVALTYPE_INVALID && enmType <= DBGFREGVALTYPE_END);
AssertPtr(pValue);
/*
* Resolve the register and call the getter on the relevant CPU.
*/
PCDBGFREGLOOKUP pLookupRec = dbgfR3RegResolve(pVM, idDefCpu, pszReg);
if (pLookupRec)
{
if (pLookupRec->pSet->enmType == DBGFREGSETTYPE_CPU)
idDefCpu = pLookupRec->pSet->uUserArg.pVCpu->idCpu;
return VMR3ReqCallWait(pVM, idDefCpu, (PFNRT)dbgfR3RegNmQueryWorkerOnCpu, 5, pVM, pLookupRec, enmType, pValue, penmType);
}
return VERR_DBGF_REGISTER_NOT_FOUND;
}
/**
* Queries a descriptor table register value.
*
* @retval VINF_SUCCESS
* @retval VERR_INVALID_VM_HANDLE
* @retval VERR_INVALID_CPU_ID
* @retval VERR_DBGF_REGISTER_NOT_FOUND
*
* @param pVM The VM handle.
* @param idDefCpu The default target CPU ID, VMCPUID_ANY if not
* applicable.
* @param pszReg The register that's being queried. Except for
* CPU registers, this must be on the form
* "set.reg[.sub]".
* @param pValue Where to store the register value.
* @param penmType Where to store the register value type.
*/
VMMR3DECL(int) DBGFR3RegNmQuery(PVM pVM, VMCPUID idDefCpu, const char *pszReg, PDBGFREGVAL pValue, PDBGFREGVALTYPE penmType)
{
return dbgfR3RegNmQueryWorker(pVM, idDefCpu, pszReg, DBGFREGVALTYPE_END, pValue, penmType);
}
/**
* Queries a 8-bit register value.
*
* @retval VINF_SUCCESS
* @retval VERR_INVALID_VM_HANDLE
* @retval VERR_INVALID_CPU_ID
* @retval VERR_DBGF_REGISTER_NOT_FOUND
* @retval VERR_DBGF_UNSUPPORTED_CAST
* @retval VINF_DBGF_TRUNCATED_REGISTER
*
* @param pVM The VM handle.
* @param idDefCpu The default target CPU ID, VMCPUID_ANY if not
* applicable.
* @param pszReg The register that's being queried. Except for
* CPU registers, this must be on the form
* "set.reg[.sub]".
* @param pu8 Where to store the register value.
*/
VMMR3DECL(int) DBGFR3RegNmQueryU8(PVM pVM, VMCPUID idDefCpu, const char *pszReg, uint8_t *pu8)
{
DBGFREGVAL Value;
int rc = dbgfR3RegNmQueryWorker(pVM, idDefCpu, pszReg, DBGFREGVALTYPE_U8, &Value, NULL);
if (RT_SUCCESS(rc))
*pu8 = Value.u8;
else
*pu8 = 0;
return rc;
}
/**
* Queries a 16-bit register value.
*
* @retval VINF_SUCCESS
* @retval VERR_INVALID_VM_HANDLE
* @retval VERR_INVALID_CPU_ID
* @retval VERR_DBGF_REGISTER_NOT_FOUND
* @retval VERR_DBGF_UNSUPPORTED_CAST
* @retval VINF_DBGF_TRUNCATED_REGISTER
* @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
*
* @param pVM The VM handle.
* @param idDefCpu The default target CPU ID, VMCPUID_ANY if not
* applicable.
* @param pszReg The register that's being queried. Except for
* CPU registers, this must be on the form
* "set.reg[.sub]".
* @param pu16 Where to store the register value.
*/
VMMR3DECL(int) DBGFR3RegNmQueryU16(PVM pVM, VMCPUID idDefCpu, const char *pszReg, uint16_t *pu16)
{
DBGFREGVAL Value;
int rc = dbgfR3RegNmQueryWorker(pVM, idDefCpu, pszReg, DBGFREGVALTYPE_U16, &Value, NULL);
if (RT_SUCCESS(rc))
*pu16 = Value.u16;
else
*pu16 = 0;
return rc;
}
/**
* Queries a 32-bit register value.
*
* @retval VINF_SUCCESS
* @retval VERR_INVALID_VM_HANDLE
* @retval VERR_INVALID_CPU_ID
* @retval VERR_DBGF_REGISTER_NOT_FOUND
* @retval VERR_DBGF_UNSUPPORTED_CAST
* @retval VINF_DBGF_TRUNCATED_REGISTER
* @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
*
* @param pVM The VM handle.
* @param idDefCpu The default target CPU ID, VMCPUID_ANY if not
* applicable.
* @param pszReg The register that's being queried. Except for
* CPU registers, this must be on the form
* "set.reg[.sub]".
* @param pu32 Where to store the register value.
*/
VMMR3DECL(int) DBGFR3RegNmQueryU32(PVM pVM, VMCPUID idDefCpu, const char *pszReg, uint32_t *pu32)
{
DBGFREGVAL Value;
int rc = dbgfR3RegNmQueryWorker(pVM, idDefCpu, pszReg, DBGFREGVALTYPE_U32, &Value, NULL);
if (RT_SUCCESS(rc))
*pu32 = Value.u32;
else
*pu32 = 0;
return rc;
}
/**
* Queries a 64-bit register value.
*
* @retval VINF_SUCCESS
* @retval VERR_INVALID_VM_HANDLE
* @retval VERR_INVALID_CPU_ID
* @retval VERR_DBGF_REGISTER_NOT_FOUND
* @retval VERR_DBGF_UNSUPPORTED_CAST
* @retval VINF_DBGF_TRUNCATED_REGISTER
* @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
*
* @param pVM The VM handle.
* @param idDefCpu The default target CPU ID, VMCPUID_ANY if not
* applicable.
* @param pszReg The register that's being queried. Except for
* CPU registers, this must be on the form
* "set.reg[.sub]".
* @param pu64 Where to store the register value.
*/
VMMR3DECL(int) DBGFR3RegNmQueryU64(PVM pVM, VMCPUID idDefCpu, const char *pszReg, uint64_t *pu64)
{
DBGFREGVAL Value;
int rc = dbgfR3RegNmQueryWorker(pVM, idDefCpu, pszReg, DBGFREGVALTYPE_U64, &Value, NULL);
if (RT_SUCCESS(rc))
*pu64 = Value.u64;
else
*pu64 = 0;
return rc;
}
/**
* Queries a 128-bit register value.
*
* @retval VINF_SUCCESS
* @retval VERR_INVALID_VM_HANDLE
* @retval VERR_INVALID_CPU_ID
* @retval VERR_DBGF_REGISTER_NOT_FOUND
* @retval VERR_DBGF_UNSUPPORTED_CAST
* @retval VINF_DBGF_TRUNCATED_REGISTER
* @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
*
* @param pVM The VM handle.
* @param idDefCpu The default target CPU ID, VMCPUID_ANY if not
* applicable.
* @param pszReg The register that's being queried. Except for
* CPU registers, this must be on the form
* "set.reg[.sub]".
* @param pu128 Where to store the register value.
*/
VMMR3DECL(int) DBGFR3RegNmQueryU128(PVM pVM, VMCPUID idDefCpu, const char *pszReg, PRTUINT128U pu128)
{
DBGFREGVAL Value;
int rc = dbgfR3RegNmQueryWorker(pVM, idDefCpu, pszReg, DBGFREGVALTYPE_U128, &Value, NULL);
if (RT_SUCCESS(rc))
*pu128 = Value.u128;
else
pu128->s.Hi = pu128->s.Lo = 0;
return rc;
}
#if 0
/**
* Queries a long double register value.
*
* @retval VINF_SUCCESS
* @retval VERR_INVALID_VM_HANDLE
* @retval VERR_INVALID_CPU_ID
* @retval VERR_DBGF_REGISTER_NOT_FOUND
* @retval VERR_DBGF_UNSUPPORTED_CAST
* @retval VINF_DBGF_TRUNCATED_REGISTER
* @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
*
* @param pVM The VM handle.
* @param idDefCpu The default target CPU ID, VMCPUID_ANY if not
* applicable.
* @param pszReg The register that's being queried. Except for
* CPU registers, this must be on the form
* "set.reg[.sub]".
* @param plrd Where to store the register value.
*/
VMMR3DECL(int) DBGFR3RegNmQueryLrd(PVM pVM, VMCPUID idDefCpu, const char *pszReg, long double *plrd)
{
DBGFREGVAL Value;
int rc = dbgfR3RegNmQueryWorker(pVM, idDefCpu, pszReg, DBGFREGVALTYPE_R80, &Value, NULL);
if (RT_SUCCESS(rc))
*plrd = Value.lrd;
else
*plrd = 0;
return rc;
}
#endif
/**
* Queries a descriptor table register value.
*
* @retval VINF_SUCCESS
* @retval VERR_INVALID_VM_HANDLE
* @retval VERR_INVALID_CPU_ID
* @retval VERR_DBGF_REGISTER_NOT_FOUND
* @retval VERR_DBGF_UNSUPPORTED_CAST
* @retval VINF_DBGF_TRUNCATED_REGISTER
* @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
*
* @param pVM The VM handle.
* @param idDefCpu The default target CPU ID, VMCPUID_ANY if not
* applicable.
* @param pszReg The register that's being queried. Except for
* CPU registers, this must be on the form
* "set.reg[.sub]".
* @param pu64Base Where to store the register base value.
* @param pu32Limit Where to store the register limit value.
*/
VMMR3DECL(int) DBGFR3RegNmQueryXdtr(PVM pVM, VMCPUID idDefCpu, const char *pszReg, uint64_t *pu64Base, uint32_t *pu32Limit)
{
DBGFREGVAL Value;
int rc = dbgfR3RegNmQueryWorker(pVM, idDefCpu, pszReg, DBGFREGVALTYPE_DTR, &Value, NULL);
if (RT_SUCCESS(rc))
{
*pu64Base = Value.dtr.u64Base;
*pu32Limit = Value.dtr.u32Limit;
}
else
{
*pu64Base = 0;
*pu32Limit = 0;
}
return rc;
}
/// @todo VMMR3DECL(int) DBGFR3RegNmQueryBatch(PVM pVM,VMCPUID idDefCpu, DBGFREGENTRYNM paRegs, size_t cRegs);
/**
* Gets the number of registers returned by DBGFR3RegNmQueryAll.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pcRegs Where to return the register count.
*/
VMMR3DECL(int) DBGFR3RegNmQueryAllCount(PVM pVM, size_t *pcRegs)
{
VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
*pcRegs = pVM->dbgf.s.cRegs;
return VINF_SUCCESS;
}
/**
* Pad register entries.
*
* @param paRegs The output array.
* @param cRegs The size of the output array.
* @param iReg The first register to pad.
* @param cRegsToPad The number of registers to pad.
*/
static void dbgfR3RegNmQueryAllPadEntries(PDBGFREGENTRYNM paRegs, size_t cRegs, size_t iReg, size_t cRegsToPad)
{
if (iReg < cRegs)
{
size_t iEndReg = iReg + cRegsToPad;
if (iEndReg > cRegs)
iEndReg = cRegs;
while (iReg < iEndReg)
{
paRegs[iReg].pszName = NULL;
paRegs[iReg].enmType = DBGFREGVALTYPE_END;
dbgfR3RegValClear(&paRegs[iReg].Val);
iReg++;
}
}
}
/**
* Query all registers in a set.
*
* @param pSet The set.
* @param cRegsToQuery The number of registers to query.
* @param paRegs The output array.
* @param cRegs The size of the output array.
*/
static void dbgfR3RegNmQueryAllInSet(PCDBGFREGSET pSet, size_t cRegsToQuery, PDBGFREGENTRYNM paRegs, size_t cRegs)
{
int rc = VINF_SUCCESS;
if (cRegsToQuery > pSet->cDescs)
cRegsToQuery = pSet->cDescs;
if (cRegsToQuery > cRegs)
cRegsToQuery = cRegs;
for (size_t iReg = 0; iReg < cRegsToQuery; iReg++)
{
paRegs[iReg].enmType = pSet->paDescs[iReg].enmType;
paRegs[iReg].pszName = pSet->paLookupRecs[iReg].Core.pszString;
dbgfR3RegValClear(&paRegs[iReg].Val);
int rc2 = pSet->paDescs[iReg].pfnGet(pSet->uUserArg.pv, &pSet->paDescs[iReg], &paRegs[iReg].Val);
AssertRCSuccess(rc2);
if (RT_FAILURE(rc2))
dbgfR3RegValClear(&paRegs[iReg].Val);
}
}
/**
* @callback_method_impl{FNRTSTRSPACECALLBACK, Worker used by
* dbgfR3RegNmQueryAllWorker}
*/
static DECLCALLBACK(int) dbgfR3RegNmQueryAllEnum(PRTSTRSPACECORE pStr, void *pvUser)
{
PCDBGFREGSET pSet = (PCDBGFREGSET)pStr;
if (pSet->enmType != DBGFREGSETTYPE_CPU)
{
PDBGFR3REGNMQUERYALLARGS pArgs = (PDBGFR3REGNMQUERYALLARGS)pvUser;
if (pArgs->iReg < pArgs->cRegs)
dbgfR3RegNmQueryAllInSet(pSet, pSet->cDescs, &pArgs->paRegs[pArgs->iReg], pArgs->cRegs - pArgs->iReg);
pArgs->iReg += pSet->cDescs;
}
return 0;
}
/**
* @callback_method_impl{FNVMMEMTRENDEZVOUS, Worker used by DBGFR3RegNmQueryAll}
*/
static DECLCALLBACK(VBOXSTRICTRC) dbgfR3RegNmQueryAllWorker(PVM pVM, PVMCPU pVCpu, void *pvUser)
{
PDBGFR3REGNMQUERYALLARGS pArgs = (PDBGFR3REGNMQUERYALLARGS)pvUser;
PDBGFREGENTRYNM paRegs = pArgs->paRegs;
size_t const cRegs = pArgs->cRegs;
DBGF_REG_DB_LOCK_READ(pVM);
/*
* My CPU registers.
*/
size_t iCpuReg = pVCpu->idCpu * DBGFREG_ALL_COUNT;
if (pVCpu->dbgf.s.pRegSet)
{
if (iCpuReg < cRegs)
dbgfR3RegNmQueryAllInSet(pVCpu->dbgf.s.pRegSet, DBGFREG_ALL_COUNT, &paRegs[iCpuReg], cRegs - iCpuReg);
}
else
dbgfR3RegNmQueryAllPadEntries(paRegs, cRegs, iCpuReg, DBGFREG_ALL_COUNT);
/*
* The primary CPU does all the other registers.
*/
if (pVCpu->idCpu == 0)
{
pArgs->iReg = pVM->cCpus * DBGFREG_ALL_COUNT;
RTStrSpaceEnumerate(&pVM->dbgf.s.RegSetSpace, dbgfR3RegNmQueryAllEnum, pArgs);
dbgfR3RegNmQueryAllPadEntries(paRegs, cRegs, pArgs->iReg, cRegs);
}
DBGF_REG_DB_UNLOCK_READ(pVM);
return VINF_SUCCESS; /* Ignore errors. */
}
/**
* Queries all register.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param paRegs The output register value array. The register
* name string is read only and shall not be freed
* or modified.
* @param cRegs The number of entries in @a paRegs. The
* correct size can be obtained by calling
* DBGFR3RegNmQueryAllCount.
*/
VMMR3DECL(int) DBGFR3RegNmQueryAll(PVM pVM, PDBGFREGENTRYNM paRegs, size_t cRegs)
{
VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
AssertPtrReturn(paRegs, VERR_INVALID_POINTER);
AssertReturn(cRegs > 0, VERR_OUT_OF_RANGE);
DBGFR3REGNMQUERYALLARGS Args;
Args.paRegs = paRegs;
Args.cRegs = cRegs;
return VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ALL_AT_ONCE, dbgfR3RegNmQueryAllWorker, &Args);
}
/**
* Internal worker for DBGFR3RegFormatValue, cbTmp is sufficent.
*
* @copydoc DBGFR3RegFormatValue
*/
DECLINLINE(ssize_t) dbgfR3RegFormatValueInt(char *pszTmp, size_t cbTmp, PCDBGFREGVAL pValue, DBGFREGVALTYPE enmType,
unsigned uBase, signed int cchWidth, signed int cchPrecision, uint32_t fFlags)
{
switch (enmType)
{
case DBGFREGVALTYPE_U8:
return RTStrFormatU8(pszTmp, cbTmp, pValue->u8, uBase, cchWidth, cchPrecision, fFlags);
case DBGFREGVALTYPE_U16:
return RTStrFormatU16(pszTmp, cbTmp, pValue->u16, uBase, cchWidth, cchPrecision, fFlags);
case DBGFREGVALTYPE_U32:
return RTStrFormatU32(pszTmp, cbTmp, pValue->u32, uBase, cchWidth, cchPrecision, fFlags);
case DBGFREGVALTYPE_U64:
return RTStrFormatU64(pszTmp, cbTmp, pValue->u64, uBase, cchWidth, cchPrecision, fFlags);
case DBGFREGVALTYPE_U128:
return RTStrFormatU128(pszTmp, cbTmp, &pValue->u128, uBase, cchWidth, cchPrecision, fFlags);
case DBGFREGVALTYPE_R80:
return RTStrFormatR80u2(pszTmp, cbTmp, &pValue->r80, cchWidth, cchPrecision, fFlags);
case DBGFREGVALTYPE_DTR:
{
ssize_t cch = RTStrFormatU64(pszTmp, cbTmp, pValue->dtr.u64Base,
16, 2+16, 0, RTSTR_F_SPECIAL | RTSTR_F_ZEROPAD);
AssertReturn(cch > 0, VERR_INTERNAL_ERROR_4);
pszTmp[cch++] = ':';
cch += RTStrFormatU64(&pszTmp[cch], cbTmp - cch, pValue->dtr.u32Limit,
16, 4, 0, RTSTR_F_ZEROPAD | RTSTR_F_32BIT);
return cch;
}
case DBGFREGVALTYPE_32BIT_HACK:
case DBGFREGVALTYPE_END:
case DBGFREGVALTYPE_INVALID:
break;
/* no default, want gcc warnings */
}
RTStrPrintf(pszTmp, cbTmp, "!enmType=%d!", enmType);
return VERR_INTERNAL_ERROR_5;
}
/**
* Format a register value, extended version.
*
* @returns The number of bytes returned, VERR_BUFFER_OVERFLOW on failure.
* @param pszBuf The output buffer.
* @param cbBuf The size of the output buffer.
* @param pValue The value to format.
* @param enmType The value type.
* @param uBase The base (ignored if not applicable).
* @param cchWidth The width if RTSTR_F_WIDTH is set, otherwise
* ignored.
* @param cchPrecision The width if RTSTR_F_PRECISION is set, otherwise
* ignored.
* @param fFlags String formatting flags, RTSTR_F_XXX.
*/
VMMDECL(ssize_t) DBGFR3RegFormatValueEx(char *pszBuf, size_t cbBuf, PCDBGFREGVAL pValue, DBGFREGVALTYPE enmType,
unsigned uBase, signed int cchWidth, signed int cchPrecision, uint32_t fFlags)
{
/*
* Format to temporary buffer using worker shared with dbgfR3RegNmPrintfCbFormat.
*/
char szTmp[160];
ssize_t cchOutput = dbgfR3RegFormatValueInt(szTmp, sizeof(szTmp), pValue, enmType, uBase, cchWidth, cchPrecision, fFlags);
if (cchOutput > 0)
{
if ((size_t)cchOutput < cbBuf)
memcpy(pszBuf, szTmp, cchOutput + 1);
else
{
if (cbBuf)
{
memcpy(pszBuf, szTmp, cbBuf - 1);
pszBuf[cbBuf - 1] = '\0';
}
cchOutput = VERR_BUFFER_OVERFLOW;
}
}
return cchOutput;
}
/**
* Format a register value as hexadecimal and with default width according to
* the type.
*
* @returns The number of bytes returned, VERR_BUFFER_OVERFLOW on failure.
* @param pszBuf The output buffer.
* @param cbBuf The size of the output buffer.
* @param pValue The value to format.
* @param enmType The value type.
* @param fSpecial Same as RTSTR_F_SPECIAL.
*/
VMMDECL(ssize_t) DBGFR3RegFormatValue(char *pszBuf, size_t cbBuf, PCDBGFREGVAL pValue, DBGFREGVALTYPE enmType, bool fSpecial)
{
int cchWidth = 0;
switch (enmType)
{
case DBGFREGVALTYPE_U8: cchWidth = 2 + fSpecial*2; break;
case DBGFREGVALTYPE_U16: cchWidth = 4 + fSpecial*2; break;
case DBGFREGVALTYPE_U32: cchWidth = 8 + fSpecial*2; break;
case DBGFREGVALTYPE_U64: cchWidth = 16 + fSpecial*2; break;
case DBGFREGVALTYPE_U128: cchWidth = 32 + fSpecial*2; break;
case DBGFREGVALTYPE_R80: cchWidth = 0; break;
case DBGFREGVALTYPE_DTR: cchWidth = 16+1+4 + fSpecial*2; break;
case DBGFREGVALTYPE_32BIT_HACK:
case DBGFREGVALTYPE_END:
case DBGFREGVALTYPE_INVALID:
break;
/* no default, want gcc warnings */
}
uint32_t fFlags = RTSTR_F_ZEROPAD;
if (fSpecial)
fFlags |= RTSTR_F_SPECIAL;
if (cchWidth != 0)
fFlags |= RTSTR_F_WIDTH;
return DBGFR3RegFormatValueEx(pszBuf, cbBuf, pValue, enmType, 16, cchWidth, 0, fFlags);
}
/**
* @callback_method_impl{FNSTRFORMAT}
*/
static DECLCALLBACK(size_t)
dbgfR3RegNmPrintfCbFormat(void *pvArg, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
const char **ppszFormat, va_list *pArgs, int cchWidth,
int cchPrecision, unsigned fFlags, char chArgSize)
{
PDBGFR3REGNMPRINTFARGS pThis = (PDBGFR3REGNMPRINTFARGS)pvArg;
/*
* Parse out the register bits of the register format type. Noisily reject
* unknown format types.
*/
const char *pszFormat = *ppszFormat;
if ( pszFormat[0] != 'V'
|| pszFormat[1] != 'R')
{
AssertMsgFailed(("'%s'\n", pszFormat));
return 0;
}
unsigned uBase;
if (pszFormat[2] == '{')
uBase = 16;
else if ( pszFormat[2] == 'U'
&& pszFormat[3] == '{')
uBase = 10;
else if ( pszFormat[2] == 'O'
&& pszFormat[3] == '{')
uBase = 8;
else if ( pszFormat[2] == 'B'
&& pszFormat[3] == '{')
uBase = 2;
else
{
AssertMsgFailed(("'%s'\n", pszFormat));
return 0;
}
const char * const pachReg = &pszFormat[3];
const char *pszEnd = strchr(&pachReg[3], '}');
AssertMsgReturn(pszEnd, ("Missing closing curly bracket: '%s'\n", pszFormat), 0);
size_t const cchReg = pachReg - pszEnd;
/*
* Look up the register - same as dbgfR3RegResolve, except for locking and
* input string termination.
*/
char szTmp[DBGF_REG_MAX_NAME * 4 + 64];
/* Try looking up the name without any case folding or cpu prefixing. */
PCDBGFREGLOOKUP pLookupRec = (PCDBGFREGLOOKUP)RTStrSpaceGetN(&pThis->pVM->dbgf.s.RegSpace, pachReg, cchReg);
if (!pLookupRec)
{
/* Lower case it and try again. */
ssize_t cchFolded = dbgfR3RegCopyToLower(pachReg, cchReg, szTmp, sizeof(szTmp) - DBGF_REG_MAX_NAME);
if (cchFolded > 0)
pLookupRec = (PCDBGFREGLOOKUP)RTStrSpaceGet(&pThis->pVM->dbgf.s.RegSpace, szTmp);
if ( !pLookupRec
&& cchFolded >= 0
&& pThis->idCpu != VMCPUID_ANY)
{
/* Prefix it with the specified CPU set. */
size_t cchCpuSet = RTStrPrintf(szTmp, sizeof(szTmp), "cpu%u.", pThis->idCpu);
dbgfR3RegCopyToLower(pachReg, cchReg, &szTmp[cchCpuSet], sizeof(szTmp) - cchCpuSet);
pLookupRec = (PCDBGFREGLOOKUP)RTStrSpaceGet(&pThis->pVM->dbgf.s.RegSpace, szTmp);
}
}
AssertMsgReturn(pLookupRec, ("'%s'\n", pszFormat), 0);
AssertMsgReturn( pLookupRec->pSet->enmType != DBGFREGSETTYPE_CPU
|| pLookupRec->pSet->uUserArg.pVCpu->idCpu == pThis->idCpu,
("'%s' idCpu=%u, pSet/cpu=%u\n", pszFormat, pThis->idCpu, pLookupRec->pSet->uUserArg.pVCpu->idCpu),
0);
/* Commit the format type parsing so we can return more freely below. */
*ppszFormat = pszFormat;
/*
* Get the register value.
*/
DBGFREGVAL Value;
DBGFREGVALTYPE enmType;
int rc = dbgfR3RegNmQueryWorkerOnCpu(pThis->pVM, pLookupRec, DBGFREGVALTYPE_END, &Value, &enmType);
if (RT_FAILURE(rc))
{
PCRTSTATUSMSG pErr = RTErrGet(rc);
if (pErr)
return pfnOutput(pvArgOutput, pErr->pszDefine, strlen(pErr->pszDefine));
return pfnOutput(pvArgOutput, szTmp, RTStrPrintf(szTmp, sizeof(szTmp), "rc=%d", rc));
}
/*
* Format the value.
*/
ssize_t cchOutput = dbgfR3RegFormatValueInt(szTmp, sizeof(szTmp), &Value, enmType, uBase, cchWidth, cchPrecision, fFlags);
if (RT_UNLIKELY(cchOutput <= 0))
{
AssertFailed();
return pfnOutput(pvArgOutput, "internal-error", sizeof("internal-error") - 1);
}
return pfnOutput(pvArgOutput, szTmp, cchOutput);
}
/**
* @callback_method_impl{FNRTSTROUTPUT}
*/
static DECLCALLBACK(size_t)
dbgfR3RegNmPrintfCbOutput(void *pvArg, const char *pachChars, size_t cbChars)
{
PDBGFR3REGNMPRINTFARGS pArgs = (PDBGFR3REGNMPRINTFARGS)pvArg;
size_t cbToCopy = cbChars;
if (cbToCopy >= pArgs->cchLeftBuf)
{
if (RT_SUCCESS(pArgs->rc))
pArgs->rc = VERR_BUFFER_OVERFLOW;
cbToCopy = pArgs->cchLeftBuf;
}
if (cbToCopy > 0)
{
memcpy(&pArgs->pszBuf[pArgs->offBuf], pachChars, cbChars);
pArgs->offBuf += cbChars;
pArgs->cchLeftBuf -= cbChars;
pArgs->pszBuf[pArgs->offBuf] = '\0';
}
return cbToCopy;
}
/**
* On CPU worker for the register formatting, used by DBGFR3RegNmPrintfV.
*
* @returns VBox status code.
*
* @param pArgs The argument package and state.
*/
static DECLCALLBACK(int) dbgfR3RegNmPrintfWorkerOnCpu(PDBGFR3REGNMPRINTFARGS pArgs)
{
DBGF_REG_DB_LOCK_READ(pArgs->pVM);
RTStrFormatV(dbgfR3RegNmPrintfCbOutput, pArgs, dbgfR3RegNmPrintfCbFormat, pArgs, pArgs->pszFormat, pArgs->va);
DBGF_REG_DB_UNLOCK_READ(pArgs->pVM);
return pArgs->rc;
}
/**
* Format a registers.
*
* This is restricted to registers from one CPU, that specified by @a idCpu.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param idCpu The CPU ID of any CPU registers that may be
* printed, pass VMCPUID_ANY if not applicable.
* @param pszBuf The output buffer.
* @param cbBuf The size of the output buffer.
* @param pszFormat The format string. Register names are given by
* %VR{name}, they take no arguments.
* @param va Other format arguments.
*/
VMMR3DECL(int) DBGFR3RegNmPrintfV(PVM pVM, VMCPUID idCpu, char *pszBuf, size_t cbBuf, const char *pszFormat, va_list va)
{
AssertPtrReturn(pszBuf, VERR_INVALID_POINTER);
AssertReturn(cbBuf > 0, VERR_BUFFER_OVERFLOW);
*pszBuf = '\0';
VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
AssertReturn(idCpu < pVM->cCpus || idCpu == VMCPUID_ANY, VERR_INVALID_CPU_ID);
AssertPtrReturn(pszFormat, VERR_INVALID_POINTER);
/*
* Set up an argument package and execute the formatting on the
* specified CPU.
*/
DBGFR3REGNMPRINTFARGS Args;
Args.pVM = pVM;
Args.idCpu = idCpu;
Args.pszBuf = pszBuf;
Args.pszFormat = pszFormat;
va_copy(Args.va, va);
Args.offBuf = 0;
Args.cchLeftBuf = cbBuf - 1;
Args.rc = VINF_SUCCESS;
int rc = VMR3ReqCallWait(pVM, idCpu, (PFNRT)dbgfR3RegNmPrintfWorkerOnCpu, 1, &Args);
va_end(Args.va);
return rc;
}
/**
* Format a registers.
*
* This is restricted to registers from one CPU, that specified by @a idCpu.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param idCpu The CPU ID of any CPU registers that may be
* printed, pass VMCPUID_ANY if not applicable.
* @param pszBuf The output buffer.
* @param cbBuf The size of the output buffer.
* @param pszFormat The format string. Register names are given by
* %VR{name}, %VRU{name}, %VRO{name} and
* %VRB{name}, which are hexadecimal, (unsigned)
* decimal, octal and binary representation. None
* of these types takes any arguments.
* @param ... Other format arguments.
*/
VMMR3DECL(int) DBGFR3RegNmPrintf(PVM pVM, VMCPUID idCpu, char *pszBuf, size_t cbBuf, const char *pszFormat, ...)
{
va_list va;
va_start(va, pszFormat);
int rc = DBGFR3RegNmPrintfV(pVM, idCpu, pszBuf, cbBuf, pszFormat, va);
va_end(va);
return rc;
}