VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/DBGFReg.cpp@ 45457

Last change on this file since 45457 was 44691, checked in by vboxsync, 12 years ago

Added a device helper for registering device registers with DBGF (breaks extpacks). Added IOREDTBLn subfields.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 84.7 KB
Line 
1/* $Id: DBGFReg.cpp 44691 2013-02-14 15:33:24Z vboxsync $ */
2/** @file
3 * DBGF - Debugger Facility, Register Methods.
4 */
5
6/*
7 * Copyright (C) 2010-2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#define LOG_GROUP LOG_GROUP_DBGF
23#include <VBox/vmm/dbgf.h>
24#include "DBGFInternal.h"
25#include <VBox/vmm/mm.h>
26#include <VBox/vmm/vm.h>
27#include <VBox/vmm/uvm.h>
28#include <VBox/param.h>
29#include <VBox/err.h>
30#include <VBox/log.h>
31#include <iprt/ctype.h>
32#include <iprt/string.h>
33#include <iprt/uint128.h>
34
35
36/*******************************************************************************
37* Defined Constants And Macros *
38*******************************************************************************/
39/** Locks the register database for writing. */
40#define DBGF_REG_DB_LOCK_WRITE(pUVM) \
41 do { \
42 int rcSem = RTSemRWRequestWrite((pUVM)->dbgf.s.hRegDbLock, RT_INDEFINITE_WAIT); \
43 AssertRC(rcSem); \
44 } while (0)
45
46/** Unlocks the register database after writing. */
47#define DBGF_REG_DB_UNLOCK_WRITE(pUVM) \
48 do { \
49 int rcSem = RTSemRWReleaseWrite((pUVM)->dbgf.s.hRegDbLock); \
50 AssertRC(rcSem); \
51 } while (0)
52
53/** Locks the register database for reading. */
54#define DBGF_REG_DB_LOCK_READ(pUVM) \
55 do { \
56 int rcSem = RTSemRWRequestRead((pUVM)->dbgf.s.hRegDbLock, RT_INDEFINITE_WAIT); \
57 AssertRC(rcSem); \
58 } while (0)
59
60/** Unlocks the register database after reading. */
61#define DBGF_REG_DB_UNLOCK_READ(pUVM) \
62 do { \
63 int rcSem = RTSemRWReleaseRead((pUVM)->dbgf.s.hRegDbLock); \
64 AssertRC(rcSem); \
65 } while (0)
66
67
68/** The max length of a set, register or sub-field name. */
69#define DBGF_REG_MAX_NAME 40
70
71
72/*******************************************************************************
73* Structures and Typedefs *
74*******************************************************************************/
75/**
76 * Register set registration record type.
77 */
78typedef enum DBGFREGSETTYPE
79{
80 /** Invalid zero value. */
81 DBGFREGSETTYPE_INVALID = 0,
82 /** CPU record. */
83 DBGFREGSETTYPE_CPU,
84 /** Device record. */
85 DBGFREGSETTYPE_DEVICE,
86 /** End of valid record types. */
87 DBGFREGSETTYPE_END
88} DBGFREGSETTYPE;
89
90
91/**
92 * Register set registration record.
93 */
94typedef struct DBGFREGSET
95{
96 /** String space core. */
97 RTSTRSPACECORE Core;
98 /** The registration record type. */
99 DBGFREGSETTYPE enmType;
100 /** The user argument for the callbacks. */
101 union
102 {
103 /** The CPU view. */
104 PVMCPU pVCpu;
105 /** The device view. */
106 PPDMDEVINS pDevIns;
107 /** The general view. */
108 void *pv;
109 } uUserArg;
110
111 /** The register descriptors. */
112 PCDBGFREGDESC paDescs;
113 /** The number of register descriptors. */
114 uint32_t cDescs;
115
116 /** Array of lookup records.
117 * The first part of the array runs parallel to paDescs, the rest are
118 * covering for aliases and bitfield variations. It's done this way to
119 * simplify the query all operations. */
120 struct DBGFREGLOOKUP *paLookupRecs;
121 /** The number of lookup records. */
122 uint32_t cLookupRecs;
123
124 /** The register name prefix. */
125 char szPrefix[1];
126} DBGFREGSET;
127/** Pointer to a register registration record. */
128typedef DBGFREGSET *PDBGFREGSET;
129/** Pointer to a const register registration record. */
130typedef DBGFREGSET const *PCDBGFREGSET;
131
132
133/**
134 * Register lookup record.
135 */
136typedef struct DBGFREGLOOKUP
137{
138 /** The string space core. */
139 RTSTRSPACECORE Core;
140 /** Pointer to the set. */
141 PCDBGFREGSET pSet;
142 /** Pointer to the register descriptor. */
143 PCDBGFREGDESC pDesc;
144 /** If an alias this points to the alias descriptor, NULL if not. */
145 PCDBGFREGALIAS pAlias;
146 /** If a sub-field this points to the sub-field descriptor, NULL if not. */
147 PCDBGFREGSUBFIELD pSubField;
148} DBGFREGLOOKUP;
149/** Pointer to a register lookup record. */
150typedef DBGFREGLOOKUP *PDBGFREGLOOKUP;
151/** Pointer to a const register lookup record. */
152typedef DBGFREGLOOKUP const *PCDBGFREGLOOKUP;
153
154
155/**
156 * Argument packet from DBGFR3RegNmQueryAll to dbgfR3RegNmQueryAllWorker.
157 */
158typedef struct DBGFR3REGNMQUERYALLARGS
159{
160 /** The output register array. */
161 PDBGFREGENTRYNM paRegs;
162 /** The number of entries in the output array. */
163 size_t cRegs;
164 /** The current register number when enumerating the string space.
165 * @remarks Only used by EMT(0). */
166 size_t iReg;
167} DBGFR3REGNMQUERYALLARGS;
168/** Pointer to a dbgfR3RegNmQueryAllWorker argument packet. */
169typedef DBGFR3REGNMQUERYALLARGS *PDBGFR3REGNMQUERYALLARGS;
170
171
172/**
173 * Argument packet passed by DBGFR3RegPrintfV to dbgfR3RegPrintfCbOutput and
174 * dbgfR3RegPrintfCbFormat.
175 */
176typedef struct DBGFR3REGPRINTFARGS
177{
178 /** The user mode VM handle. */
179 PUVM pUVM;
180 /** The target CPU. */
181 VMCPUID idCpu;
182 /** Set if we're looking at guest registers. */
183 bool fGuestRegs;
184 /** The output buffer. */
185 char *pszBuf;
186 /** The format string. */
187 const char *pszFormat;
188 /** The va list with format arguments. */
189 va_list va;
190
191 /** The current buffer offset. */
192 size_t offBuf;
193 /** The amount of buffer space left, not counting the terminator char. */
194 size_t cchLeftBuf;
195 /** The status code of the whole operation. First error is return,
196 * subsequent ones are suppressed. */
197 int rc;
198} DBGFR3REGPRINTFARGS;
199/** Pointer to a DBGFR3RegPrintfV argument packet. */
200typedef DBGFR3REGPRINTFARGS *PDBGFR3REGPRINTFARGS;
201
202
203
204/**
205 * Initializes the register database.
206 *
207 * @returns VBox status code.
208 * @param pUVM The user mode VM handle.
209 */
210int dbgfR3RegInit(PUVM pUVM)
211{
212 int rc = VINF_SUCCESS;
213 if (!pUVM->dbgf.s.fRegDbInitialized)
214 {
215 rc = RTSemRWCreate(&pUVM->dbgf.s.hRegDbLock);
216 pUVM->dbgf.s.fRegDbInitialized = RT_SUCCESS(rc);
217 }
218 return rc;
219}
220
221
222/**
223 * Terminates the register database.
224 *
225 * @param pUVM The user mode VM handle.
226 */
227void dbgfR3RegTerm(PUVM pUVM)
228{
229 RTSemRWDestroy(pUVM->dbgf.s.hRegDbLock);
230 pUVM->dbgf.s.hRegDbLock = NIL_RTSEMRW;
231 pUVM->dbgf.s.fRegDbInitialized = false;
232}
233
234
235/**
236 * Validates a register name.
237 *
238 * This is used for prefixes, aliases and field names.
239 *
240 * @returns true if valid, false if not.
241 * @param pszName The register name to validate.
242 * @param chDot Set to '.' if accepted, otherwise 0.
243 */
244static bool dbgfR3RegIsNameValid(const char *pszName, char chDot)
245{
246 const char *psz = pszName;
247 if (!RT_C_IS_ALPHA(*psz))
248 return false;
249 char ch;
250 while ((ch = *++psz))
251 if ( !RT_C_IS_LOWER(ch)
252 && !RT_C_IS_DIGIT(ch)
253 && ch != '_'
254 && ch != chDot)
255 return false;
256 if (psz - pszName > DBGF_REG_MAX_NAME)
257 return false;
258 return true;
259}
260
261
262/**
263 * Common worker for registering a register set.
264 *
265 * @returns VBox status code.
266 * @param pUVM The user mode VM handle.
267 * @param paRegisters The register descriptors.
268 * @param enmType The set type.
269 * @param pvUserArg The user argument for the callbacks.
270 * @param pszPrefix The name prefix.
271 * @param iInstance The instance number to be appended to @a
272 * pszPrefix when creating the set name.
273 */
274static int dbgfR3RegRegisterCommon(PUVM pUVM, PCDBGFREGDESC paRegisters, DBGFREGSETTYPE enmType, void *pvUserArg,
275 const char *pszPrefix, uint32_t iInstance)
276{
277 /*
278 * Validate input.
279 */
280 /* The name components. */
281 AssertMsgReturn(dbgfR3RegIsNameValid(pszPrefix, 0), ("%s\n", pszPrefix), VERR_INVALID_NAME);
282 const char *psz = RTStrEnd(pszPrefix, RTSTR_MAX);
283 bool const fNeedUnderscore = RT_C_IS_DIGIT(psz[-1]);
284 size_t const cchPrefix = psz - pszPrefix + fNeedUnderscore;
285 AssertMsgReturn(cchPrefix < RT_SIZEOFMEMB(DBGFREGSET, szPrefix) - 4 - 1, ("%s\n", pszPrefix), VERR_INVALID_NAME);
286
287 AssertMsgReturn(iInstance <= 9999, ("%d\n", iInstance), VERR_INVALID_NAME);
288
289 /* The descriptors. */
290 uint32_t cLookupRecs = 0;
291 uint32_t iDesc;
292 for (iDesc = 0; paRegisters[iDesc].pszName != NULL; iDesc++)
293 {
294 AssertMsgReturn(dbgfR3RegIsNameValid(paRegisters[iDesc].pszName, 0), ("%s (#%u)\n", paRegisters[iDesc].pszName, iDesc), VERR_INVALID_NAME);
295
296 if (enmType == DBGFREGSETTYPE_CPU)
297 AssertMsgReturn((unsigned)paRegisters[iDesc].enmReg == iDesc && iDesc < (unsigned)DBGFREG_END,
298 ("%d iDesc=%d\n", paRegisters[iDesc].enmReg, iDesc),
299 VERR_INVALID_PARAMETER);
300 else
301 AssertReturn(paRegisters[iDesc].enmReg == DBGFREG_END, VERR_INVALID_PARAMETER);
302 AssertReturn( paRegisters[iDesc].enmType > DBGFREGVALTYPE_INVALID
303 && paRegisters[iDesc].enmType < DBGFREGVALTYPE_END, VERR_INVALID_PARAMETER);
304 AssertMsgReturn(!(paRegisters[iDesc].fFlags & ~DBGFREG_FLAGS_READ_ONLY),
305 ("%#x (#%u)\n", paRegisters[iDesc].fFlags, iDesc),
306 VERR_INVALID_PARAMETER);
307 AssertPtrReturn(paRegisters[iDesc].pfnGet, VERR_INVALID_PARAMETER);
308 AssertReturn(RT_VALID_PTR(paRegisters[iDesc].pfnSet) || (paRegisters[iDesc].fFlags & DBGFREG_FLAGS_READ_ONLY),
309 VERR_INVALID_PARAMETER);
310
311 uint32_t iAlias = 0;
312 PCDBGFREGALIAS paAliases = paRegisters[iDesc].paAliases;
313 if (paAliases)
314 {
315 AssertPtrReturn(paAliases, VERR_INVALID_PARAMETER);
316 for (; paAliases[iAlias].pszName; iAlias++)
317 {
318 AssertMsgReturn(dbgfR3RegIsNameValid(paAliases[iAlias].pszName, 0), ("%s (%s)\n", paAliases[iAlias].pszName, paRegisters[iDesc].pszName), VERR_INVALID_NAME);
319 AssertReturn( paAliases[iAlias].enmType > DBGFREGVALTYPE_INVALID
320 && paAliases[iAlias].enmType < DBGFREGVALTYPE_END, VERR_INVALID_PARAMETER);
321 }
322 }
323
324 uint32_t iSubField = 0;
325 PCDBGFREGSUBFIELD paSubFields = paRegisters[iDesc].paSubFields;
326 if (paSubFields)
327 {
328 AssertPtrReturn(paSubFields, VERR_INVALID_PARAMETER);
329 for (; paSubFields[iSubField].pszName; iSubField++)
330 {
331 AssertMsgReturn(dbgfR3RegIsNameValid(paSubFields[iSubField].pszName, '.'), ("%s (%s)\n", paSubFields[iSubField].pszName, paRegisters[iDesc].pszName), VERR_INVALID_NAME);
332 AssertReturn(paSubFields[iSubField].iFirstBit + paSubFields[iSubField].cBits <= 128, VERR_INVALID_PARAMETER);
333 AssertReturn(paSubFields[iSubField].cBits + paSubFields[iSubField].cShift <= 128, VERR_INVALID_PARAMETER);
334 AssertPtrNullReturn(paSubFields[iSubField].pfnGet, VERR_INVALID_POINTER);
335 AssertPtrNullReturn(paSubFields[iSubField].pfnSet, VERR_INVALID_POINTER);
336 }
337 }
338
339 cLookupRecs += (1 + iAlias) * (1 + iSubField);
340 }
341
342 /* Check the instance number of the CPUs. */
343 AssertReturn(enmType != DBGFREGSETTYPE_CPU || iInstance < pUVM->cCpus, VERR_INVALID_CPU_ID);
344
345 /*
346 * Allocate a new record and all associated lookup records.
347 */
348 size_t cbRegSet = RT_OFFSETOF(DBGFREGSET, szPrefix[cchPrefix + 4 + 1]);
349 cbRegSet = RT_ALIGN_Z(cbRegSet, 32);
350 size_t const offLookupRecArray = cbRegSet;
351 cbRegSet += cLookupRecs * sizeof(DBGFREGLOOKUP);
352
353 PDBGFREGSET pRegSet = (PDBGFREGSET)MMR3HeapAllocZU(pUVM, MM_TAG_DBGF_REG, cbRegSet);
354 if (!pRegSet)
355 return VERR_NO_MEMORY;
356
357 /*
358 * Initialize the new record.
359 */
360 pRegSet->Core.pszString = pRegSet->szPrefix;
361 pRegSet->enmType = enmType;
362 pRegSet->uUserArg.pv = pvUserArg;
363 pRegSet->paDescs = paRegisters;
364 pRegSet->cDescs = iDesc;
365 pRegSet->cLookupRecs = cLookupRecs;
366 pRegSet->paLookupRecs = (PDBGFREGLOOKUP)((uintptr_t)pRegSet + offLookupRecArray);
367 if (fNeedUnderscore)
368 RTStrPrintf(pRegSet->szPrefix, cchPrefix + 4 + 1, "%s_%u", pszPrefix, iInstance);
369 else
370 RTStrPrintf(pRegSet->szPrefix, cchPrefix + 4 + 1, "%s%u", pszPrefix, iInstance);
371
372
373 /*
374 * Initialize the lookup records. See DBGFREGSET::paLookupRecs.
375 */
376 char szName[DBGF_REG_MAX_NAME * 3 + 16];
377 strcpy(szName, pRegSet->szPrefix);
378 char *pszReg = strchr(szName, '\0');
379 *pszReg++ = '.';
380
381 /* Array parallel to the descriptors. */
382 int rc = VINF_SUCCESS;
383 PDBGFREGLOOKUP pLookupRec = &pRegSet->paLookupRecs[0];
384 for (iDesc = 0; paRegisters[iDesc].pszName != NULL && RT_SUCCESS(rc); iDesc++)
385 {
386 strcpy(pszReg, paRegisters[iDesc].pszName);
387 pLookupRec->Core.pszString = MMR3HeapStrDupU(pUVM, MM_TAG_DBGF_REG, szName);
388 if (!pLookupRec->Core.pszString)
389 rc = VERR_NO_STR_MEMORY;
390 pLookupRec->pSet = pRegSet;
391 pLookupRec->pDesc = &paRegisters[iDesc];
392 pLookupRec->pAlias = NULL;
393 pLookupRec->pSubField = NULL;
394 pLookupRec++;
395 }
396
397 /* Aliases and sub-fields. */
398 for (iDesc = 0; paRegisters[iDesc].pszName != NULL && RT_SUCCESS(rc); iDesc++)
399 {
400 PCDBGFREGALIAS pCurAlias = NULL; /* first time we add sub-fields for the real name. */
401 PCDBGFREGALIAS pNextAlias = paRegisters[iDesc].paAliases;
402 const char *pszRegName = paRegisters[iDesc].pszName;
403 while (RT_SUCCESS(rc))
404 {
405 /* Add sub-field records. */
406 PCDBGFREGSUBFIELD paSubFields = paRegisters[iDesc].paSubFields;
407 if (paSubFields)
408 {
409 size_t cchReg = strlen(pszRegName);
410 memcpy(pszReg, pszRegName, cchReg);
411 char *pszSub = &pszReg[cchReg];
412 *pszSub++ = '.';
413 for (uint32_t iSubField = 0; paSubFields[iSubField].pszName && RT_SUCCESS(rc); iSubField++)
414 {
415 strcpy(pszSub, paSubFields[iSubField].pszName);
416 pLookupRec->Core.pszString = MMR3HeapStrDupU(pUVM, MM_TAG_DBGF_REG, szName);
417 if (!pLookupRec->Core.pszString)
418 rc = VERR_NO_STR_MEMORY;
419 pLookupRec->pSet = pRegSet;
420 pLookupRec->pDesc = &paRegisters[iDesc];
421 pLookupRec->pAlias = pCurAlias;
422 pLookupRec->pSubField = &paSubFields[iSubField];
423 pLookupRec++;
424 }
425 }
426
427 /* Advance to the next alias. */
428 pCurAlias = pNextAlias++;
429 if (!pCurAlias)
430 break;
431 pszRegName = pCurAlias->pszName;
432 if (!pszRegName)
433 break;
434
435 /* The alias record. */
436 strcpy(pszReg, pszRegName);
437 pLookupRec->Core.pszString = MMR3HeapStrDupU(pUVM, MM_TAG_DBGF_REG, szName);
438 if (!pLookupRec->Core.pszString)
439 rc = VERR_NO_STR_MEMORY;
440 pLookupRec->pSet = pRegSet;
441 pLookupRec->pDesc = &paRegisters[iDesc];
442 pLookupRec->pAlias = pCurAlias;
443 pLookupRec->pSubField = NULL;
444 pLookupRec++;
445 }
446 }
447 Assert(pLookupRec == &pRegSet->paLookupRecs[pRegSet->cLookupRecs]);
448
449 if (RT_SUCCESS(rc))
450 {
451 /*
452 * Insert the record into the register set string space and optionally into
453 * the CPU register set cache.
454 */
455 DBGF_REG_DB_LOCK_WRITE(pUVM);
456
457 bool fInserted = RTStrSpaceInsert(&pUVM->dbgf.s.RegSetSpace, &pRegSet->Core);
458 if (fInserted)
459 {
460 pUVM->dbgf.s.cRegs += pRegSet->cDescs;
461 if (enmType == DBGFREGSETTYPE_CPU)
462 {
463 if (pRegSet->cDescs > DBGFREG_ALL_COUNT)
464 pUVM->dbgf.s.cRegs -= pRegSet->cDescs - DBGFREG_ALL_COUNT;
465 if (!strcmp(pszPrefix, "cpu"))
466 pUVM->aCpus[iInstance].dbgf.s.pGuestRegSet = pRegSet;
467 else
468 pUVM->aCpus[iInstance].dbgf.s.pHyperRegSet = pRegSet;
469 }
470
471 PDBGFREGLOOKUP paLookupRecs = pRegSet->paLookupRecs;
472 uint32_t iLookupRec = pRegSet->cLookupRecs;
473 while (iLookupRec-- > 0)
474 {
475 bool fInserted2 = RTStrSpaceInsert(&pUVM->dbgf.s.RegSpace, &paLookupRecs[iLookupRec].Core);
476 AssertMsg(fInserted2, ("'%s'", paLookupRecs[iLookupRec].Core.pszString)); NOREF(fInserted2);
477 }
478
479 DBGF_REG_DB_UNLOCK_WRITE(pUVM);
480 return VINF_SUCCESS;
481 }
482
483 DBGF_REG_DB_UNLOCK_WRITE(pUVM);
484 rc = VERR_DUPLICATE;
485 }
486
487 /*
488 * Bail out.
489 */
490 for (uint32_t i = 0; i < pRegSet->cLookupRecs; i++)
491 MMR3HeapFree((char *)pRegSet->paLookupRecs[i].Core.pszString);
492 MMR3HeapFree(pRegSet);
493
494 return rc;
495}
496
497
498/**
499 * Registers a set of registers for a CPU.
500 *
501 * @returns VBox status code.
502 * @param pVM Pointer to the VM.
503 * @param pVCpu Pointer to the VMCPU.
504 * @param paRegisters The register descriptors.
505 * @param fGuestRegs Set if it's the guest registers, clear if
506 * hypervisor registers.
507 */
508VMMR3_INT_DECL(int) DBGFR3RegRegisterCpu(PVM pVM, PVMCPU pVCpu, PCDBGFREGDESC paRegisters, bool fGuestRegs)
509{
510 PUVM pUVM = pVM->pUVM;
511 if (!pUVM->dbgf.s.fRegDbInitialized)
512 {
513 int rc = dbgfR3RegInit(pUVM);
514 if (RT_FAILURE(rc))
515 return rc;
516 }
517
518 return dbgfR3RegRegisterCommon(pUVM, paRegisters, DBGFREGSETTYPE_CPU, pVCpu,
519 fGuestRegs ? "cpu" : "hypercpu", pVCpu->idCpu);
520}
521
522
523/**
524 * Registers a set of registers for a device.
525 *
526 * @returns VBox status code.
527 * @param pVM Pointer to the VM.
528 * @param paRegisters The register descriptors.
529 * @param pDevIns The device instance. This will be the callback user
530 * argument.
531 * @param pszPrefix The device name.
532 * @param iInstance The device instance.
533 */
534VMMR3_INT_DECL(int) DBGFR3RegRegisterDevice(PVM pVM, PCDBGFREGDESC paRegisters, PPDMDEVINS pDevIns,
535 const char *pszPrefix, uint32_t iInstance)
536{
537 AssertPtrReturn(paRegisters, VERR_INVALID_POINTER);
538 AssertPtrReturn(pDevIns, VERR_INVALID_POINTER);
539 AssertPtrReturn(pszPrefix, VERR_INVALID_POINTER);
540
541 return dbgfR3RegRegisterCommon(pVM->pUVM, paRegisters, DBGFREGSETTYPE_DEVICE, pDevIns, pszPrefix, iInstance);
542}
543
544
545/**
546 * Clears the register value variable.
547 *
548 * @param pValue The variable to clear.
549 */
550DECLINLINE(void) dbgfR3RegValClear(PDBGFREGVAL pValue)
551{
552 pValue->au64[0] = 0;
553 pValue->au64[1] = 0;
554}
555
556
557/**
558 * Sets a 80-bit floating point variable to a 64-bit unsigned interger value.
559 *
560 * @param pValue The value.
561 * @param u64 The integer value.
562 */
563DECLINLINE(void) dbgfR3RegValR80SetU64(PDBGFREGVAL pValue, uint64_t u64)
564{
565 /** @todo fixme */
566 pValue->r80.s.fSign = 0;
567 pValue->r80.s.uExponent = 16383;
568 pValue->r80.s.u64Mantissa = u64;
569}
570
571
572/**
573 * Sets a 80-bit floating point variable to a 64-bit unsigned interger value.
574 *
575 * @param pValue The value.
576 * @param u128 The integer value.
577 */
578DECLINLINE(void) dbgfR3RegValR80SetU128(PDBGFREGVAL pValue, RTUINT128U u128)
579{
580 /** @todo fixme */
581 pValue->r80.s.fSign = 0;
582 pValue->r80.s.uExponent = 16383;
583 pValue->r80.s.u64Mantissa = u128.s.Lo;
584}
585
586
587/**
588 * Get a 80-bit floating point variable as a 64-bit unsigned integer.
589 *
590 * @returns 64-bit unsigned integer.
591 * @param pValue The value.
592 */
593DECLINLINE(uint64_t) dbgfR3RegValR80GetU64(PCDBGFREGVAL pValue)
594{
595 /** @todo stupid, stupid MSC. */
596 return pValue->r80.s.u64Mantissa;
597}
598
599
600/**
601 * Get a 80-bit floating point variable as a 128-bit unsigned integer.
602 *
603 * @returns 128-bit unsigned integer.
604 * @param pValue The value.
605 */
606DECLINLINE(RTUINT128U) dbgfR3RegValR80GetU128(PCDBGFREGVAL pValue)
607{
608 /** @todo stupid, stupid MSC. */
609 RTUINT128U uRet;
610#if 0
611 uRet.s.Lo = (uint64_t)InVal.lrd;
612 uRet.s.Hi = (uint64_t)InVal.lrd / _4G / _4G;
613#else
614 uRet.s.Lo = pValue->r80.s.u64Mantissa;
615 uRet.s.Hi = 0;
616#endif
617 return uRet;
618}
619
620
621/**
622 * Performs a cast between register value types.
623 *
624 * @retval VINF_SUCCESS
625 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
626 * @retval VINF_DBGF_TRUNCATED_REGISTER
627 * @retval VERR_DBGF_UNSUPPORTED_CAST
628 *
629 * @param pValue The value to cast (input + output).
630 * @param enmFromType The input value.
631 * @param enmToType The desired output value.
632 */
633static int dbgfR3RegValCast(PDBGFREGVAL pValue, DBGFREGVALTYPE enmFromType, DBGFREGVALTYPE enmToType)
634{
635 DBGFREGVAL const InVal = *pValue;
636 dbgfR3RegValClear(pValue);
637
638 /* Note! No default cases here as gcc warnings about missing enum values
639 are desired. */
640 switch (enmFromType)
641 {
642 case DBGFREGVALTYPE_U8:
643 switch (enmToType)
644 {
645 case DBGFREGVALTYPE_U8: pValue->u8 = InVal.u8; return VINF_SUCCESS;
646 case DBGFREGVALTYPE_U16: pValue->u16 = InVal.u8; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
647 case DBGFREGVALTYPE_U32: pValue->u32 = InVal.u8; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
648 case DBGFREGVALTYPE_U64: pValue->u64 = InVal.u8; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
649 case DBGFREGVALTYPE_U128: pValue->u128.s.Lo = InVal.u8; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
650 case DBGFREGVALTYPE_R80: dbgfR3RegValR80SetU64(pValue, InVal.u8); return VINF_DBGF_ZERO_EXTENDED_REGISTER;
651 case DBGFREGVALTYPE_DTR: return VERR_DBGF_UNSUPPORTED_CAST;
652
653 case DBGFREGVALTYPE_32BIT_HACK:
654 case DBGFREGVALTYPE_END:
655 case DBGFREGVALTYPE_INVALID:
656 break;
657 }
658 break;
659
660 case DBGFREGVALTYPE_U16:
661 switch (enmToType)
662 {
663 case DBGFREGVALTYPE_U8: pValue->u8 = InVal.u16; return VINF_DBGF_TRUNCATED_REGISTER;
664 case DBGFREGVALTYPE_U16: pValue->u16 = InVal.u16; return VINF_SUCCESS;
665 case DBGFREGVALTYPE_U32: pValue->u32 = InVal.u16; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
666 case DBGFREGVALTYPE_U64: pValue->u64 = InVal.u16; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
667 case DBGFREGVALTYPE_U128: pValue->u128.s.Lo = InVal.u16; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
668 case DBGFREGVALTYPE_R80: dbgfR3RegValR80SetU64(pValue, InVal.u16); return VINF_DBGF_ZERO_EXTENDED_REGISTER;
669 case DBGFREGVALTYPE_DTR: return VERR_DBGF_UNSUPPORTED_CAST;
670
671 case DBGFREGVALTYPE_32BIT_HACK:
672 case DBGFREGVALTYPE_END:
673 case DBGFREGVALTYPE_INVALID:
674 break;
675 }
676 break;
677
678 case DBGFREGVALTYPE_U32:
679 switch (enmToType)
680 {
681 case DBGFREGVALTYPE_U8: pValue->u8 = InVal.u32; return VINF_DBGF_TRUNCATED_REGISTER;
682 case DBGFREGVALTYPE_U16: pValue->u16 = InVal.u32; return VINF_DBGF_TRUNCATED_REGISTER;
683 case DBGFREGVALTYPE_U32: pValue->u32 = InVal.u32; return VINF_SUCCESS;
684 case DBGFREGVALTYPE_U64: pValue->u64 = InVal.u32; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
685 case DBGFREGVALTYPE_U128: pValue->u128.s.Lo = InVal.u32; return VINF_DBGF_ZERO_EXTENDED_REGISTER;
686 case DBGFREGVALTYPE_R80: dbgfR3RegValR80SetU64(pValue, InVal.u32); return VINF_DBGF_ZERO_EXTENDED_REGISTER;
687 case DBGFREGVALTYPE_DTR: return VERR_DBGF_UNSUPPORTED_CAST;
688
689 case DBGFREGVALTYPE_32BIT_HACK:
690 case DBGFREGVALTYPE_END:
691 case DBGFREGVALTYPE_INVALID:
692 break;
693 }
694 break;
695
696 case DBGFREGVALTYPE_U64:
697 switch (enmToType)
698 {
699 case DBGFREGVALTYPE_U8: pValue->u8 = InVal.u64; return VINF_DBGF_TRUNCATED_REGISTER;
700 case DBGFREGVALTYPE_U16: pValue->u16 = InVal.u64; return VINF_DBGF_TRUNCATED_REGISTER;
701 case DBGFREGVALTYPE_U32: pValue->u32 = InVal.u64; return VINF_DBGF_TRUNCATED_REGISTER;
702 case DBGFREGVALTYPE_U64: pValue->u64 = InVal.u64; return VINF_SUCCESS;
703 case DBGFREGVALTYPE_U128: pValue->u128.s.Lo = InVal.u64; return VINF_DBGF_TRUNCATED_REGISTER;
704 case DBGFREGVALTYPE_R80: dbgfR3RegValR80SetU64(pValue, InVal.u64); return VINF_DBGF_TRUNCATED_REGISTER;
705 case DBGFREGVALTYPE_DTR: return VERR_DBGF_UNSUPPORTED_CAST;
706
707 case DBGFREGVALTYPE_32BIT_HACK:
708 case DBGFREGVALTYPE_END:
709 case DBGFREGVALTYPE_INVALID:
710 break;
711 }
712 break;
713
714 case DBGFREGVALTYPE_U128:
715 switch (enmToType)
716 {
717 case DBGFREGVALTYPE_U8: pValue->u8 = InVal.u128.s.Lo; return VINF_DBGF_TRUNCATED_REGISTER;
718 case DBGFREGVALTYPE_U16: pValue->u16 = InVal.u128.s.Lo; return VINF_DBGF_TRUNCATED_REGISTER;
719 case DBGFREGVALTYPE_U32: pValue->u32 = InVal.u128.s.Lo; return VINF_DBGF_TRUNCATED_REGISTER;
720 case DBGFREGVALTYPE_U64: pValue->u64 = InVal.u128.s.Lo; return VINF_DBGF_TRUNCATED_REGISTER;
721 case DBGFREGVALTYPE_U128: pValue->u128 = InVal.u128; return VINF_SUCCESS;
722 case DBGFREGVALTYPE_R80: dbgfR3RegValR80SetU128(pValue, InVal.u128); return VINF_DBGF_TRUNCATED_REGISTER;
723 case DBGFREGVALTYPE_DTR: return VERR_DBGF_UNSUPPORTED_CAST;
724
725 case DBGFREGVALTYPE_32BIT_HACK:
726 case DBGFREGVALTYPE_END:
727 case DBGFREGVALTYPE_INVALID:
728 break;
729 }
730 break;
731
732 case DBGFREGVALTYPE_R80:
733 switch (enmToType)
734 {
735 case DBGFREGVALTYPE_U8: pValue->u8 = (uint8_t )dbgfR3RegValR80GetU64(&InVal); return VINF_DBGF_TRUNCATED_REGISTER;
736 case DBGFREGVALTYPE_U16: pValue->u16 = (uint16_t)dbgfR3RegValR80GetU64(&InVal); return VINF_DBGF_TRUNCATED_REGISTER;
737 case DBGFREGVALTYPE_U32: pValue->u32 = (uint32_t)dbgfR3RegValR80GetU64(&InVal); return VINF_DBGF_TRUNCATED_REGISTER;
738 case DBGFREGVALTYPE_U64: pValue->u64 = (uint64_t)dbgfR3RegValR80GetU64(&InVal); return VINF_DBGF_TRUNCATED_REGISTER;
739 case DBGFREGVALTYPE_U128: pValue->u128 = dbgfR3RegValR80GetU128(&InVal); return VINF_DBGF_TRUNCATED_REGISTER;
740 case DBGFREGVALTYPE_R80: pValue->r80 = InVal.r80; return VINF_SUCCESS;
741 case DBGFREGVALTYPE_DTR: return VERR_DBGF_UNSUPPORTED_CAST;
742
743 case DBGFREGVALTYPE_32BIT_HACK:
744 case DBGFREGVALTYPE_END:
745 case DBGFREGVALTYPE_INVALID:
746 break;
747 }
748 break;
749
750 case DBGFREGVALTYPE_DTR:
751 switch (enmToType)
752 {
753 case DBGFREGVALTYPE_U8: pValue->u8 = InVal.dtr.u64Base; return VINF_DBGF_TRUNCATED_REGISTER;
754 case DBGFREGVALTYPE_U16: pValue->u16 = InVal.dtr.u64Base; return VINF_DBGF_TRUNCATED_REGISTER;
755 case DBGFREGVALTYPE_U32: pValue->u32 = InVal.dtr.u64Base; return VINF_DBGF_TRUNCATED_REGISTER;
756 case DBGFREGVALTYPE_U64: pValue->u64 = InVal.dtr.u64Base; return VINF_DBGF_TRUNCATED_REGISTER;
757 case DBGFREGVALTYPE_U128: pValue->u128.s.Lo = InVal.dtr.u64Base; return VINF_DBGF_TRUNCATED_REGISTER;
758 case DBGFREGVALTYPE_R80: dbgfR3RegValR80SetU64(pValue, InVal.dtr.u64Base); return VINF_DBGF_TRUNCATED_REGISTER;
759 case DBGFREGVALTYPE_DTR: pValue->dtr = InVal.dtr; return VINF_SUCCESS;
760
761 case DBGFREGVALTYPE_32BIT_HACK:
762 case DBGFREGVALTYPE_END:
763 case DBGFREGVALTYPE_INVALID:
764 break;
765 }
766 break;
767
768 case DBGFREGVALTYPE_INVALID:
769 case DBGFREGVALTYPE_END:
770 case DBGFREGVALTYPE_32BIT_HACK:
771 break;
772 }
773
774 AssertMsgFailed(("%d / %d\n", enmFromType, enmToType));
775 return VERR_DBGF_UNSUPPORTED_CAST;
776}
777
778
779/**
780 * Worker for the CPU register queries.
781 *
782 * @returns VBox status code.
783 * @retval VINF_SUCCESS
784 * @retval VERR_INVALID_VM_HANDLE
785 * @retval VERR_INVALID_CPU_ID
786 * @retval VERR_DBGF_REGISTER_NOT_FOUND
787 * @retval VERR_DBGF_UNSUPPORTED_CAST
788 * @retval VINF_DBGF_TRUNCATED_REGISTER
789 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
790 *
791 * @param pUVM The user mode VM handle.
792 * @param idCpu The virtual CPU ID.
793 * @param enmReg The register to query.
794 * @param enmType The desired return type.
795 * @param fGuestRegs Query guest CPU registers if set (true),
796 * hypervisor CPU registers if clear (false).
797 * @param pValue Where to return the register value.
798 */
799static DECLCALLBACK(int) dbgfR3RegCpuQueryWorkerOnCpu(PUVM pUVM, VMCPUID idCpu, DBGFREG enmReg, DBGFREGVALTYPE enmType,
800 bool fGuestRegs, PDBGFREGVAL pValue)
801{
802 int rc = VINF_SUCCESS;
803 DBGF_REG_DB_LOCK_READ(pUVM);
804
805 /*
806 * Look up the register set of the specified CPU.
807 */
808 PDBGFREGSET pSet = fGuestRegs
809 ? pUVM->aCpus[idCpu].dbgf.s.pGuestRegSet
810 : pUVM->aCpus[idCpu].dbgf.s.pHyperRegSet;
811 if (RT_LIKELY(pSet))
812 {
813 /*
814 * Look up the register and get the register value.
815 */
816 if (RT_LIKELY(pSet->cDescs > (size_t)enmReg))
817 {
818 PCDBGFREGDESC pDesc = &pSet->paDescs[enmReg];
819
820 pValue->au64[0] = pValue->au64[1] = 0;
821 rc = pDesc->pfnGet(pSet->uUserArg.pv, pDesc, pValue);
822 if (RT_SUCCESS(rc))
823 {
824 /*
825 * Do the cast if the desired return type doesn't match what
826 * the getter returned.
827 */
828 if (pDesc->enmType == enmType)
829 rc = VINF_SUCCESS;
830 else
831 rc = dbgfR3RegValCast(pValue, pDesc->enmType, enmType);
832 }
833 }
834 else
835 rc = VERR_DBGF_REGISTER_NOT_FOUND;
836 }
837 else
838 rc = VERR_INVALID_CPU_ID;
839
840 DBGF_REG_DB_UNLOCK_READ(pUVM);
841 return rc;
842}
843
844
845/**
846 * Internal worker for the CPU register query functions.
847 *
848 * @returns VBox status code.
849 * @retval VINF_SUCCESS
850 * @retval VERR_INVALID_VM_HANDLE
851 * @retval VERR_INVALID_CPU_ID
852 * @retval VERR_DBGF_REGISTER_NOT_FOUND
853 * @retval VERR_DBGF_UNSUPPORTED_CAST
854 * @retval VINF_DBGF_TRUNCATED_REGISTER
855 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
856 *
857 * @param pUVM The user mode VM handle.
858 * @param idCpu The virtual CPU ID. Can be OR'ed with
859 * DBGFREG_HYPER_VMCPUID.
860 * @param enmReg The register to query.
861 * @param enmType The desired return type.
862 * @param pValue Where to return the register value.
863 */
864static int dbgfR3RegCpuQueryWorker(PUVM pUVM, VMCPUID idCpu, DBGFREG enmReg, DBGFREGVALTYPE enmType, PDBGFREGVAL pValue)
865{
866 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
867 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
868 AssertMsgReturn(enmReg >= DBGFREG_AL && enmReg <= DBGFREG_END, ("%d\n", enmReg), VERR_INVALID_PARAMETER);
869
870 bool const fGuestRegs = !(idCpu & DBGFREG_HYPER_VMCPUID);
871 idCpu &= ~DBGFREG_HYPER_VMCPUID;
872 AssertReturn(idCpu < pUVM->cCpus, VERR_INVALID_CPU_ID);
873
874 return VMR3ReqPriorityCallWaitU(pUVM, idCpu, (PFNRT)dbgfR3RegCpuQueryWorkerOnCpu, 6,
875 pUVM, idCpu, enmReg, enmType, fGuestRegs, pValue);
876}
877
878
879/**
880 * Queries a 8-bit CPU register value.
881 *
882 * @retval VINF_SUCCESS
883 * @retval VERR_INVALID_VM_HANDLE
884 * @retval VERR_INVALID_CPU_ID
885 * @retval VERR_DBGF_REGISTER_NOT_FOUND
886 * @retval VERR_DBGF_UNSUPPORTED_CAST
887 * @retval VINF_DBGF_TRUNCATED_REGISTER
888 *
889 * @param pUVM The user mode VM handle.
890 * @param idCpu The target CPU ID. Can be OR'ed with
891 * DBGFREG_HYPER_VMCPUID.
892 * @param enmReg The register that's being queried.
893 * @param pu8 Where to store the register value.
894 */
895VMMR3DECL(int) DBGFR3RegCpuQueryU8(PUVM pUVM, VMCPUID idCpu, DBGFREG enmReg, uint8_t *pu8)
896{
897 DBGFREGVAL Value;
898 int rc = dbgfR3RegCpuQueryWorker(pUVM, idCpu, enmReg, DBGFREGVALTYPE_U8, &Value);
899 if (RT_SUCCESS(rc))
900 *pu8 = Value.u8;
901 else
902 *pu8 = 0;
903 return rc;
904}
905
906
907/**
908 * Queries a 16-bit CPU register value.
909 *
910 * @retval VINF_SUCCESS
911 * @retval VERR_INVALID_VM_HANDLE
912 * @retval VERR_INVALID_CPU_ID
913 * @retval VERR_DBGF_REGISTER_NOT_FOUND
914 * @retval VERR_DBGF_UNSUPPORTED_CAST
915 * @retval VINF_DBGF_TRUNCATED_REGISTER
916 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
917 *
918 * @param pUVM The user mode VM handle.
919 * @param idCpu The target CPU ID. Can be OR'ed with
920 * DBGFREG_HYPER_VMCPUID.
921 * @param enmReg The register that's being queried.
922 * @param pu16 Where to store the register value.
923 */
924VMMR3DECL(int) DBGFR3RegCpuQueryU16(PUVM pUVM, VMCPUID idCpu, DBGFREG enmReg, uint16_t *pu16)
925{
926 DBGFREGVAL Value;
927 int rc = dbgfR3RegCpuQueryWorker(pUVM, idCpu, enmReg, DBGFREGVALTYPE_U16, &Value);
928 if (RT_SUCCESS(rc))
929 *pu16 = Value.u16;
930 else
931 *pu16 = 0;
932 return rc;
933}
934
935
936/**
937 * Queries a 32-bit CPU register value.
938 *
939 * @retval VINF_SUCCESS
940 * @retval VERR_INVALID_VM_HANDLE
941 * @retval VERR_INVALID_CPU_ID
942 * @retval VERR_DBGF_REGISTER_NOT_FOUND
943 * @retval VERR_DBGF_UNSUPPORTED_CAST
944 * @retval VINF_DBGF_TRUNCATED_REGISTER
945 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
946 *
947 * @param pUVM The user mode VM handle.
948 * @param idCpu The target CPU ID. Can be OR'ed with
949 * DBGFREG_HYPER_VMCPUID.
950 * @param enmReg The register that's being queried.
951 * @param pu32 Where to store the register value.
952 */
953VMMR3DECL(int) DBGFR3RegCpuQueryU32(PUVM pUVM, VMCPUID idCpu, DBGFREG enmReg, uint32_t *pu32)
954{
955 DBGFREGVAL Value;
956 int rc = dbgfR3RegCpuQueryWorker(pUVM, idCpu, enmReg, DBGFREGVALTYPE_U32, &Value);
957 if (RT_SUCCESS(rc))
958 *pu32 = Value.u32;
959 else
960 *pu32 = 0;
961 return rc;
962}
963
964
965/**
966 * Queries a 64-bit CPU register value.
967 *
968 * @retval VINF_SUCCESS
969 * @retval VERR_INVALID_VM_HANDLE
970 * @retval VERR_INVALID_CPU_ID
971 * @retval VERR_DBGF_REGISTER_NOT_FOUND
972 * @retval VERR_DBGF_UNSUPPORTED_CAST
973 * @retval VINF_DBGF_TRUNCATED_REGISTER
974 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
975 *
976 * @param pUVM The user mode VM handle.
977 * @param idCpu The target CPU ID. Can be OR'ed with
978 * DBGFREG_HYPER_VMCPUID.
979 * @param enmReg The register that's being queried.
980 * @param pu64 Where to store the register value.
981 */
982VMMR3DECL(int) DBGFR3RegCpuQueryU64(PUVM pUVM, VMCPUID idCpu, DBGFREG enmReg, uint64_t *pu64)
983{
984 DBGFREGVAL Value;
985 int rc = dbgfR3RegCpuQueryWorker(pUVM, idCpu, enmReg, DBGFREGVALTYPE_U64, &Value);
986 if (RT_SUCCESS(rc))
987 *pu64 = Value.u64;
988 else
989 *pu64 = 0;
990 return rc;
991}
992
993#if 0 /* rewrite / remove */
994
995/**
996 * Wrapper around CPUMQueryGuestMsr for dbgfR3RegCpuQueryBatchWorker.
997 *
998 * @retval VINF_SUCCESS
999 * @retval VERR_DBGF_REGISTER_NOT_FOUND
1000 *
1001 * @param pVCpu The current CPU.
1002 * @param pReg The where to store the register value and
1003 * size.
1004 * @param idMsr The MSR to get.
1005 */
1006static void dbgfR3RegGetMsrBatch(PVMCPU pVCpu, PDBGFREGENTRY pReg, uint32_t idMsr)
1007{
1008 pReg->enmType = DBGFREGVALTYPE_U64;
1009 int rc = CPUMQueryGuestMsr(pVCpu, idMsr, &pReg->Val.u64);
1010 if (RT_FAILURE(rc))
1011 {
1012 AssertMsg(rc == VERR_CPUM_RAISE_GP_0, ("%Rrc\n", rc));
1013 pReg->Val.u64 = 0;
1014 }
1015}
1016
1017
1018static DECLCALLBACK(int) dbgfR3RegCpuQueryBatchWorker(PUVM pUVM, VMCPUID idCpu, PDBGFREGENTRY paRegs, size_t cRegs)
1019{
1020#if 0
1021 PVMCPU pVCpu = &pUVM->pVM->aCpus[idCpu];
1022 PCCPUMCTX pCtx = CPUMQueryGuestCtxPtr(pVCpu);
1023
1024 PDBGFREGENTRY pReg = paRegs - 1;
1025 while (cRegs-- > 0)
1026 {
1027 pReg++;
1028 pReg->Val.au64[0] = 0;
1029 pReg->Val.au64[1] = 0;
1030
1031 DBGFREG const enmReg = pReg->enmReg;
1032 AssertMsgReturn(enmReg >= 0 && enmReg <= DBGFREG_END, ("%d (%#x)\n", enmReg, enmReg), VERR_DBGF_REGISTER_NOT_FOUND);
1033 if (enmReg != DBGFREG_END)
1034 {
1035 PCDBGFREGDESC pDesc = &g_aDbgfRegDescs[enmReg];
1036 if (!pDesc->pfnGet)
1037 {
1038 PCRTUINT128U pu = (PCRTUINT128U)((uintptr_t)pCtx + pDesc->offCtx);
1039 pReg->enmType = pDesc->enmType;
1040 switch (pDesc->enmType)
1041 {
1042 case DBGFREGVALTYPE_U8: pReg->Val.u8 = pu->au8[0]; break;
1043 case DBGFREGVALTYPE_U16: pReg->Val.u16 = pu->au16[0]; break;
1044 case DBGFREGVALTYPE_U32: pReg->Val.u32 = pu->au32[0]; break;
1045 case DBGFREGVALTYPE_U64: pReg->Val.u64 = pu->au64[0]; break;
1046 case DBGFREGVALTYPE_U128:
1047 pReg->Val.au64[0] = pu->au64[0];
1048 pReg->Val.au64[1] = pu->au64[1];
1049 break;
1050 case DBGFREGVALTYPE_R80:
1051 pReg->Val.au64[0] = pu->au64[0];
1052 pReg->Val.au16[5] = pu->au16[5];
1053 break;
1054 default:
1055 AssertMsgFailedReturn(("%s %d\n", pDesc->pszName, pDesc->enmType), VERR_IPE_NOT_REACHED_DEFAULT_CASE);
1056 }
1057 }
1058 else
1059 {
1060 int rc = pDesc->pfnGet(pVCpu, pDesc, pCtx, &pReg->Val.u);
1061 if (RT_FAILURE(rc))
1062 return rc;
1063 }
1064 }
1065 }
1066 return VINF_SUCCESS;
1067#else
1068 return VERR_NOT_IMPLEMENTED;
1069#endif
1070}
1071
1072
1073/**
1074 * Query a batch of registers.
1075 *
1076 * @retval VINF_SUCCESS
1077 * @retval VERR_INVALID_VM_HANDLE
1078 * @retval VERR_INVALID_CPU_ID
1079 * @retval VERR_DBGF_REGISTER_NOT_FOUND
1080 *
1081 * @param pUVM The user mode VM handle.
1082 * @param idCpu The target CPU ID. Can be OR'ed with
1083 * DBGFREG_HYPER_VMCPUID.
1084 * @param paRegs Pointer to an array of @a cRegs elements. On
1085 * input the enmReg members indicates which
1086 * registers to query. On successful return the
1087 * other members are set. DBGFREG_END can be used
1088 * as a filler.
1089 * @param cRegs The number of entries in @a paRegs.
1090 */
1091VMMR3DECL(int) DBGFR3RegCpuQueryBatch(PUVM pUVM, VMCPUID idCpu, PDBGFREGENTRY paRegs, size_t cRegs)
1092{
1093 UVM_ASSERT_VALID_EXT_RETURN(pUVM, NULL);
1094 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, NULL);
1095 AssertReturn(idCpu < pUVM->cCpus, VERR_INVALID_CPU_ID);
1096 if (!cRegs)
1097 return VINF_SUCCESS;
1098 AssertReturn(cRegs < _1M, VERR_OUT_OF_RANGE);
1099 AssertPtrReturn(paRegs, VERR_INVALID_POINTER);
1100 size_t iReg = cRegs;
1101 while (iReg-- > 0)
1102 {
1103 DBGFREG enmReg = paRegs[iReg].enmReg;
1104 AssertMsgReturn(enmReg < DBGFREG_END && enmReg >= DBGFREG_AL, ("%d (%#x)", enmReg, enmReg), VERR_DBGF_REGISTER_NOT_FOUND);
1105 }
1106
1107 return VMR3ReqCallWaitU(pUVM, idCpu, (PFNRT)dbgfR3RegCpuQueryBatchWorker, 4, pUVM, idCpu, paRegs, cRegs);
1108}
1109
1110
1111/**
1112 * Query all registers for a Virtual CPU.
1113 *
1114 * @retval VINF_SUCCESS
1115 * @retval VERR_INVALID_VM_HANDLE
1116 * @retval VERR_INVALID_CPU_ID
1117 *
1118 * @param pUVM The user mode VM handle.
1119 * @param idCpu The target CPU ID. Can be OR'ed with
1120 * DBGFREG_HYPER_VMCPUID.
1121 * @param paRegs Pointer to an array of @a cRegs elements.
1122 * These will be filled with the CPU register
1123 * values. Overflowing entries will be set to
1124 * DBGFREG_END. The returned registers can be
1125 * accessed by using the DBGFREG values as index.
1126 * @param cRegs The number of entries in @a paRegs. The
1127 * recommended value is DBGFREG_ALL_COUNT.
1128 */
1129VMMR3DECL(int) DBGFR3RegCpuQueryAll(PUVM pUVM, VMCPUID idCpu, PDBGFREGENTRY paRegs, size_t cRegs)
1130{
1131 /*
1132 * Validate input.
1133 */
1134 UVM_ASSERT_VALID_EXT_RETURN(pUVM, NULL);
1135 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, NULL);
1136 AssertReturn(idCpu < pUVM->cCpus, VERR_INVALID_CPU_ID);
1137 if (!cRegs)
1138 return VINF_SUCCESS;
1139 AssertReturn(cRegs < _1M, VERR_OUT_OF_RANGE);
1140 AssertPtrReturn(paRegs, VERR_INVALID_POINTER);
1141
1142 /*
1143 * Convert it into a batch query (lazy bird).
1144 */
1145 unsigned iReg = 0;
1146 while (iReg < cRegs && iReg < DBGFREG_ALL_COUNT)
1147 {
1148 paRegs[iReg].enmReg = (DBGFREG)iReg;
1149 iReg++;
1150 }
1151 while (iReg < cRegs)
1152 paRegs[iReg++].enmReg = DBGFREG_END;
1153
1154 return VMR3ReqCallWaitU(pUVM, idCpu, (PFNRT)dbgfR3RegCpuQueryBatchWorker, 4, pUVM, idCpu, paRegs, cRegs);
1155}
1156
1157#endif /* rewrite or remove? */
1158
1159/**
1160 * Gets the name of a register.
1161 *
1162 * @returns Pointer to read-only register name (lower case). NULL if the
1163 * parameters are invalid.
1164 *
1165 * @param pUVM The user mode VM handle.
1166 * @param enmReg The register identifier.
1167 * @param enmType The register type. This is for sort out
1168 * aliases. Pass DBGFREGVALTYPE_INVALID to get
1169 * the standard name.
1170 */
1171VMMR3DECL(const char *) DBGFR3RegCpuName(PUVM pUVM, DBGFREG enmReg, DBGFREGVALTYPE enmType)
1172{
1173 AssertReturn(enmReg >= DBGFREG_AL && enmReg < DBGFREG_END, NULL);
1174 AssertReturn(enmType >= DBGFREGVALTYPE_INVALID && enmType < DBGFREGVALTYPE_END, NULL);
1175 UVM_ASSERT_VALID_EXT_RETURN(pUVM, NULL);
1176 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, NULL);
1177
1178 PCDBGFREGSET pSet = pUVM->aCpus[0].dbgf.s.pGuestRegSet;
1179 if (RT_UNLIKELY(!pSet))
1180 return NULL;
1181
1182 PCDBGFREGDESC pDesc = &pSet->paDescs[enmReg];
1183 PCDBGFREGALIAS pAlias = pDesc->paAliases;
1184 if ( pAlias
1185 && pDesc->enmType != enmType
1186 && enmType != DBGFREGVALTYPE_INVALID)
1187 {
1188 while (pAlias->pszName)
1189 {
1190 if (pAlias->enmType == enmType)
1191 return pAlias->pszName;
1192 pAlias++;
1193 }
1194 }
1195
1196 return pDesc->pszName;
1197}
1198
1199
1200/**
1201 * Fold the string to lower case and copy it into the destination buffer.
1202 *
1203 * @returns Number of folder characters, -1 on overflow.
1204 * @param pszSrc The source string.
1205 * @param cchSrc How much to fold and copy.
1206 * @param pszDst The output buffer.
1207 * @param cbDst The size of the output buffer.
1208 */
1209static ssize_t dbgfR3RegCopyToLower(const char *pszSrc, size_t cchSrc, char *pszDst, size_t cbDst)
1210{
1211 ssize_t cchFolded = 0;
1212 char ch;
1213 while (cchSrc-- > 0 && (ch = *pszSrc++))
1214 {
1215 if (RT_UNLIKELY(cbDst <= 1))
1216 return -1;
1217 cbDst--;
1218
1219 char chLower = RT_C_TO_LOWER(ch);
1220 cchFolded += chLower != ch;
1221 *pszDst++ = chLower;
1222 }
1223 if (RT_UNLIKELY(!cbDst))
1224 return -1;
1225 *pszDst = '\0';
1226 return cchFolded;
1227}
1228
1229
1230/**
1231 * Resolves the register name.
1232 *
1233 * @returns Lookup record.
1234 * @param pUVM The user mode VM handle.
1235 * @param idDefCpu The default CPU ID set.
1236 * @param pszReg The register name.
1237 * @param fGuestRegs Default to guest CPU registers if set, the
1238 * hypervisor CPU registers if clear.
1239 */
1240static PCDBGFREGLOOKUP dbgfR3RegResolve(PUVM pUVM, VMCPUID idDefCpu, const char *pszReg, bool fGuestRegs)
1241{
1242 DBGF_REG_DB_LOCK_READ(pUVM);
1243
1244 /* Try looking up the name without any case folding or cpu prefixing. */
1245 PRTSTRSPACE pRegSpace = &pUVM->dbgf.s.RegSpace;
1246 PCDBGFREGLOOKUP pLookupRec = (PCDBGFREGLOOKUP)RTStrSpaceGet(pRegSpace, pszReg);
1247 if (!pLookupRec)
1248 {
1249 char szName[DBGF_REG_MAX_NAME * 4 + 16];
1250
1251 /* Lower case it and try again. */
1252 ssize_t cchFolded = dbgfR3RegCopyToLower(pszReg, RTSTR_MAX, szName, sizeof(szName) - DBGF_REG_MAX_NAME);
1253 if (cchFolded > 0)
1254 pLookupRec = (PCDBGFREGLOOKUP)RTStrSpaceGet(pRegSpace, szName);
1255 if ( !pLookupRec
1256 && cchFolded >= 0
1257 && idDefCpu != VMCPUID_ANY)
1258 {
1259 /* Prefix it with the specified CPU set. */
1260 size_t cchCpuSet = RTStrPrintf(szName, sizeof(szName), fGuestRegs ? "cpu%u." : "hypercpu%u.", idDefCpu);
1261 dbgfR3RegCopyToLower(pszReg, RTSTR_MAX, &szName[cchCpuSet], sizeof(szName) - cchCpuSet);
1262 pLookupRec = (PCDBGFREGLOOKUP)RTStrSpaceGet(pRegSpace, szName);
1263 }
1264 }
1265
1266 DBGF_REG_DB_UNLOCK_READ(pUVM);
1267 return pLookupRec;
1268}
1269
1270
1271/**
1272 * Validates the register name.
1273 *
1274 * @returns VBox status code.
1275 * @retval VINF_SUCCESS if the register was found.
1276 * @retval VERR_DBGF_REGISTER_NOT_FOUND if not found.
1277 *
1278 * @param pUVM The user mode VM handle.
1279 * @param idDefCpu The default CPU.
1280 * @param pszReg The registe name.
1281 */
1282VMMR3DECL(int) DBGFR3RegNmValidate(PUVM pUVM, VMCPUID idDefCpu, const char *pszReg)
1283{
1284 /*
1285 * Validate input.
1286 */
1287 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1288 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
1289 AssertReturn((idDefCpu & ~DBGFREG_HYPER_VMCPUID) < pUVM->cCpus || idDefCpu == VMCPUID_ANY, VERR_INVALID_CPU_ID);
1290 AssertPtrReturn(pszReg, VERR_INVALID_POINTER);
1291
1292 /*
1293 * Resolve the register.
1294 */
1295 bool const fGuestRegs = !(idDefCpu & DBGFREG_HYPER_VMCPUID) && idDefCpu != VMCPUID_ANY;
1296 PCDBGFREGLOOKUP pLookupRec = dbgfR3RegResolve(pUVM, idDefCpu, pszReg, fGuestRegs);
1297 if (!pLookupRec)
1298 return VERR_DBGF_REGISTER_NOT_FOUND;
1299 return VINF_SUCCESS;
1300}
1301
1302
1303/**
1304 * On CPU worker for the register queries, used by dbgfR3RegNmQueryWorker and
1305 * dbgfR3RegPrintfCbFormatNormal.
1306 *
1307 * @returns VBox status code.
1308 *
1309 * @param pUVM The user mode VM handle.
1310 * @param pLookupRec The register lookup record.
1311 * @param enmType The desired return type.
1312 * @param pValue Where to return the register value.
1313 * @param penmType Where to store the register value type.
1314 * Optional.
1315 */
1316static DECLCALLBACK(int) dbgfR3RegNmQueryWorkerOnCpu(PUVM pUVM, PCDBGFREGLOOKUP pLookupRec, DBGFREGVALTYPE enmType,
1317 PDBGFREGVAL pValue, PDBGFREGVALTYPE penmType)
1318{
1319 PCDBGFREGDESC pDesc = pLookupRec->pDesc;
1320 PCDBGFREGSET pSet = pLookupRec->pSet;
1321 PCDBGFREGSUBFIELD pSubField = pLookupRec->pSubField;
1322 DBGFREGVALTYPE enmValueType = pDesc->enmType;
1323 int rc;
1324
1325 NOREF(pUVM);
1326
1327 /*
1328 * Get the register or sub-field value.
1329 */
1330 dbgfR3RegValClear(pValue);
1331 if (!pSubField)
1332 {
1333 rc = pDesc->pfnGet(pSet->uUserArg.pv, pDesc, pValue);
1334 if ( pLookupRec->pAlias
1335 && pLookupRec->pAlias->enmType != enmValueType
1336 && RT_SUCCESS(rc))
1337 {
1338 rc = dbgfR3RegValCast(pValue, enmValueType, pLookupRec->pAlias->enmType);
1339 enmValueType = pLookupRec->pAlias->enmType;
1340 }
1341 }
1342 else
1343 {
1344 if (pSubField->pfnGet)
1345 {
1346 rc = pSubField->pfnGet(pSet->uUserArg.pv, pSubField, &pValue->u128);
1347 enmValueType = DBGFREGVALTYPE_U128;
1348 }
1349 else
1350 {
1351 rc = pDesc->pfnGet(pSet->uUserArg.pv, pDesc, pValue);
1352 if ( pLookupRec->pAlias
1353 && pLookupRec->pAlias->enmType != enmValueType
1354 && RT_SUCCESS(rc))
1355 {
1356 rc = dbgfR3RegValCast(pValue, enmValueType, pLookupRec->pAlias->enmType);
1357 enmValueType = pLookupRec->pAlias->enmType;
1358 }
1359 if (RT_SUCCESS(rc))
1360 {
1361 rc = dbgfR3RegValCast(pValue, enmValueType, DBGFREGVALTYPE_U128);
1362 if (RT_SUCCESS(rc))
1363 {
1364 RTUInt128AssignShiftLeft(&pValue->u128, -pSubField->iFirstBit);
1365 RTUInt128AssignAndNFirstBits(&pValue->u128, pSubField->cBits);
1366 if (pSubField->cShift)
1367 RTUInt128AssignShiftLeft(&pValue->u128, pSubField->cShift);
1368 }
1369 }
1370 }
1371 if (RT_SUCCESS(rc))
1372 {
1373 unsigned const cBits = pSubField->cBits + pSubField->cShift;
1374 if (cBits <= 8)
1375 enmValueType = DBGFREGVALTYPE_U8;
1376 else if (cBits <= 16)
1377 enmValueType = DBGFREGVALTYPE_U16;
1378 else if (cBits <= 32)
1379 enmValueType = DBGFREGVALTYPE_U32;
1380 else if (cBits <= 64)
1381 enmValueType = DBGFREGVALTYPE_U64;
1382 else
1383 enmValueType = DBGFREGVALTYPE_U128;
1384 rc = dbgfR3RegValCast(pValue, DBGFREGVALTYPE_U128, enmValueType);
1385 }
1386 }
1387 if (RT_SUCCESS(rc))
1388 {
1389 /*
1390 * Do the cast if the desired return type doesn't match what
1391 * the getter returned.
1392 */
1393 if ( enmValueType == enmType
1394 || enmType == DBGFREGVALTYPE_END)
1395 {
1396 rc = VINF_SUCCESS;
1397 if (penmType)
1398 *penmType = enmValueType;
1399 }
1400 else
1401 {
1402 rc = dbgfR3RegValCast(pValue, enmValueType, enmType);
1403 if (penmType)
1404 *penmType = RT_SUCCESS(rc) ? enmType : enmValueType;
1405 }
1406 }
1407
1408 return rc;
1409}
1410
1411
1412/**
1413 * Worker for the register queries.
1414 *
1415 * @returns VBox status code.
1416 * @retval VINF_SUCCESS
1417 * @retval VERR_INVALID_VM_HANDLE
1418 * @retval VERR_INVALID_CPU_ID
1419 * @retval VERR_DBGF_REGISTER_NOT_FOUND
1420 * @retval VERR_DBGF_UNSUPPORTED_CAST
1421 * @retval VINF_DBGF_TRUNCATED_REGISTER
1422 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
1423 *
1424 * @param pUVM The user mode VM handle.
1425 * @param idDefCpu The virtual CPU ID for the default CPU register
1426 * set. Can be OR'ed with DBGFREG_HYPER_VMCPUID.
1427 * @param pszReg The register to query.
1428 * @param enmType The desired return type.
1429 * @param pValue Where to return the register value.
1430 * @param penmType Where to store the register value type.
1431 * Optional.
1432 */
1433static int dbgfR3RegNmQueryWorker(PUVM pUVM, VMCPUID idDefCpu, const char *pszReg, DBGFREGVALTYPE enmType,
1434 PDBGFREGVAL pValue, PDBGFREGVALTYPE penmType)
1435{
1436 /*
1437 * Validate input.
1438 */
1439 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1440 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
1441 AssertReturn((idDefCpu & ~DBGFREG_HYPER_VMCPUID) < pUVM->cCpus || idDefCpu == VMCPUID_ANY, VERR_INVALID_CPU_ID);
1442 AssertPtrReturn(pszReg, VERR_INVALID_POINTER);
1443
1444 Assert(enmType > DBGFREGVALTYPE_INVALID && enmType <= DBGFREGVALTYPE_END);
1445 AssertPtr(pValue);
1446
1447 /*
1448 * Resolve the register and call the getter on the relevant CPU.
1449 */
1450 bool const fGuestRegs = !(idDefCpu & DBGFREG_HYPER_VMCPUID) && idDefCpu != VMCPUID_ANY;
1451 PCDBGFREGLOOKUP pLookupRec = dbgfR3RegResolve(pUVM, idDefCpu, pszReg, fGuestRegs);
1452 if (pLookupRec)
1453 {
1454 if (pLookupRec->pSet->enmType == DBGFREGSETTYPE_CPU)
1455 idDefCpu = pLookupRec->pSet->uUserArg.pVCpu->idCpu;
1456 else if (idDefCpu != VMCPUID_ANY)
1457 idDefCpu &= ~DBGFREG_HYPER_VMCPUID;
1458 return VMR3ReqPriorityCallWaitU(pUVM, idDefCpu, (PFNRT)dbgfR3RegNmQueryWorkerOnCpu, 5,
1459 pUVM, pLookupRec, enmType, pValue, penmType);
1460 }
1461 return VERR_DBGF_REGISTER_NOT_FOUND;
1462}
1463
1464
1465/**
1466 * Queries a descriptor table register value.
1467 *
1468 * @retval VINF_SUCCESS
1469 * @retval VERR_INVALID_VM_HANDLE
1470 * @retval VERR_INVALID_CPU_ID
1471 * @retval VERR_DBGF_REGISTER_NOT_FOUND
1472 *
1473 * @param pUVM The user mode VM handle.
1474 * @param idDefCpu The default target CPU ID, VMCPUID_ANY if not
1475 * applicable. Can be OR'ed with
1476 * DBGFREG_HYPER_VMCPUID.
1477 * @param pszReg The register that's being queried. Except for
1478 * CPU registers, this must be on the form
1479 * "set.reg[.sub]".
1480 * @param pValue Where to store the register value.
1481 * @param penmType Where to store the register value type.
1482 */
1483VMMR3DECL(int) DBGFR3RegNmQuery(PUVM pUVM, VMCPUID idDefCpu, const char *pszReg, PDBGFREGVAL pValue, PDBGFREGVALTYPE penmType)
1484{
1485 return dbgfR3RegNmQueryWorker(pUVM, idDefCpu, pszReg, DBGFREGVALTYPE_END, pValue, penmType);
1486}
1487
1488
1489/**
1490 * Queries a 8-bit register value.
1491 *
1492 * @retval VINF_SUCCESS
1493 * @retval VERR_INVALID_VM_HANDLE
1494 * @retval VERR_INVALID_CPU_ID
1495 * @retval VERR_DBGF_REGISTER_NOT_FOUND
1496 * @retval VERR_DBGF_UNSUPPORTED_CAST
1497 * @retval VINF_DBGF_TRUNCATED_REGISTER
1498 *
1499 * @param pUVM The user mode VM handle.
1500 * @param idDefCpu The default target CPU ID, VMCPUID_ANY if not
1501 * applicable. Can be OR'ed with
1502 * DBGFREG_HYPER_VMCPUID.
1503 * @param pszReg The register that's being queried. Except for
1504 * CPU registers, this must be on the form
1505 * "set.reg[.sub]".
1506 * @param pu8 Where to store the register value.
1507 */
1508VMMR3DECL(int) DBGFR3RegNmQueryU8(PUVM pUVM, VMCPUID idDefCpu, const char *pszReg, uint8_t *pu8)
1509{
1510 DBGFREGVAL Value;
1511 int rc = dbgfR3RegNmQueryWorker(pUVM, idDefCpu, pszReg, DBGFREGVALTYPE_U8, &Value, NULL);
1512 if (RT_SUCCESS(rc))
1513 *pu8 = Value.u8;
1514 else
1515 *pu8 = 0;
1516 return rc;
1517}
1518
1519
1520/**
1521 * Queries a 16-bit register value.
1522 *
1523 * @retval VINF_SUCCESS
1524 * @retval VERR_INVALID_VM_HANDLE
1525 * @retval VERR_INVALID_CPU_ID
1526 * @retval VERR_DBGF_REGISTER_NOT_FOUND
1527 * @retval VERR_DBGF_UNSUPPORTED_CAST
1528 * @retval VINF_DBGF_TRUNCATED_REGISTER
1529 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
1530 *
1531 * @param pUVM The user mode VM handle.
1532 * @param idDefCpu The default target CPU ID, VMCPUID_ANY if not
1533 * applicable. Can be OR'ed with
1534 * DBGFREG_HYPER_VMCPUID.
1535 * @param pszReg The register that's being queried. Except for
1536 * CPU registers, this must be on the form
1537 * "set.reg[.sub]".
1538 * @param pu16 Where to store the register value.
1539 */
1540VMMR3DECL(int) DBGFR3RegNmQueryU16(PUVM pUVM, VMCPUID idDefCpu, const char *pszReg, uint16_t *pu16)
1541{
1542 DBGFREGVAL Value;
1543 int rc = dbgfR3RegNmQueryWorker(pUVM, idDefCpu, pszReg, DBGFREGVALTYPE_U16, &Value, NULL);
1544 if (RT_SUCCESS(rc))
1545 *pu16 = Value.u16;
1546 else
1547 *pu16 = 0;
1548 return rc;
1549}
1550
1551
1552/**
1553 * Queries a 32-bit register value.
1554 *
1555 * @retval VINF_SUCCESS
1556 * @retval VERR_INVALID_VM_HANDLE
1557 * @retval VERR_INVALID_CPU_ID
1558 * @retval VERR_DBGF_REGISTER_NOT_FOUND
1559 * @retval VERR_DBGF_UNSUPPORTED_CAST
1560 * @retval VINF_DBGF_TRUNCATED_REGISTER
1561 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
1562 *
1563 * @param pUVM The user mode VM handle.
1564 * @param idDefCpu The default target CPU ID, VMCPUID_ANY if not
1565 * applicable. Can be OR'ed with
1566 * DBGFREG_HYPER_VMCPUID.
1567 * @param pszReg The register that's being queried. Except for
1568 * CPU registers, this must be on the form
1569 * "set.reg[.sub]".
1570 * @param pu32 Where to store the register value.
1571 */
1572VMMR3DECL(int) DBGFR3RegNmQueryU32(PUVM pUVM, VMCPUID idDefCpu, const char *pszReg, uint32_t *pu32)
1573{
1574 DBGFREGVAL Value;
1575 int rc = dbgfR3RegNmQueryWorker(pUVM, idDefCpu, pszReg, DBGFREGVALTYPE_U32, &Value, NULL);
1576 if (RT_SUCCESS(rc))
1577 *pu32 = Value.u32;
1578 else
1579 *pu32 = 0;
1580 return rc;
1581}
1582
1583
1584/**
1585 * Queries a 64-bit register value.
1586 *
1587 * @retval VINF_SUCCESS
1588 * @retval VERR_INVALID_VM_HANDLE
1589 * @retval VERR_INVALID_CPU_ID
1590 * @retval VERR_DBGF_REGISTER_NOT_FOUND
1591 * @retval VERR_DBGF_UNSUPPORTED_CAST
1592 * @retval VINF_DBGF_TRUNCATED_REGISTER
1593 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
1594 *
1595 * @param pUVM The user mode VM handle.
1596 * @param idDefCpu The default target CPU ID, VMCPUID_ANY if not
1597 * applicable. Can be OR'ed with
1598 * DBGFREG_HYPER_VMCPUID.
1599 * @param pszReg The register that's being queried. Except for
1600 * CPU registers, this must be on the form
1601 * "set.reg[.sub]".
1602 * @param pu64 Where to store the register value.
1603 */
1604VMMR3DECL(int) DBGFR3RegNmQueryU64(PUVM pUVM, VMCPUID idDefCpu, const char *pszReg, uint64_t *pu64)
1605{
1606 DBGFREGVAL Value;
1607 int rc = dbgfR3RegNmQueryWorker(pUVM, idDefCpu, pszReg, DBGFREGVALTYPE_U64, &Value, NULL);
1608 if (RT_SUCCESS(rc))
1609 *pu64 = Value.u64;
1610 else
1611 *pu64 = 0;
1612 return rc;
1613}
1614
1615
1616/**
1617 * Queries a 128-bit register value.
1618 *
1619 * @retval VINF_SUCCESS
1620 * @retval VERR_INVALID_VM_HANDLE
1621 * @retval VERR_INVALID_CPU_ID
1622 * @retval VERR_DBGF_REGISTER_NOT_FOUND
1623 * @retval VERR_DBGF_UNSUPPORTED_CAST
1624 * @retval VINF_DBGF_TRUNCATED_REGISTER
1625 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
1626 *
1627 * @param pUVM The user mode VM handle.
1628 * @param idDefCpu The default target CPU ID, VMCPUID_ANY if not
1629 * applicable. Can be OR'ed with
1630 * DBGFREG_HYPER_VMCPUID.
1631 * @param pszReg The register that's being queried. Except for
1632 * CPU registers, this must be on the form
1633 * "set.reg[.sub]".
1634 * @param pu128 Where to store the register value.
1635 */
1636VMMR3DECL(int) DBGFR3RegNmQueryU128(PUVM pUVM, VMCPUID idDefCpu, const char *pszReg, PRTUINT128U pu128)
1637{
1638 DBGFREGVAL Value;
1639 int rc = dbgfR3RegNmQueryWorker(pUVM, idDefCpu, pszReg, DBGFREGVALTYPE_U128, &Value, NULL);
1640 if (RT_SUCCESS(rc))
1641 *pu128 = Value.u128;
1642 else
1643 pu128->s.Hi = pu128->s.Lo = 0;
1644 return rc;
1645}
1646
1647
1648#if 0
1649/**
1650 * Queries a long double register value.
1651 *
1652 * @retval VINF_SUCCESS
1653 * @retval VERR_INVALID_VM_HANDLE
1654 * @retval VERR_INVALID_CPU_ID
1655 * @retval VERR_DBGF_REGISTER_NOT_FOUND
1656 * @retval VERR_DBGF_UNSUPPORTED_CAST
1657 * @retval VINF_DBGF_TRUNCATED_REGISTER
1658 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
1659 *
1660 * @param pUVM The user mode VM handle.
1661 * @param idDefCpu The default target CPU ID, VMCPUID_ANY if not
1662 * applicable. Can be OR'ed with
1663 * DBGFREG_HYPER_VMCPUID.
1664 * @param pszReg The register that's being queried. Except for
1665 * CPU registers, this must be on the form
1666 * "set.reg[.sub]".
1667 * @param plrd Where to store the register value.
1668 */
1669VMMR3DECL(int) DBGFR3RegNmQueryLrd(PUVM pUVM, VMCPUID idDefCpu, const char *pszReg, long double *plrd)
1670{
1671 DBGFREGVAL Value;
1672 int rc = dbgfR3RegNmQueryWorker(pUVM, idDefCpu, pszReg, DBGFREGVALTYPE_R80, &Value, NULL);
1673 if (RT_SUCCESS(rc))
1674 *plrd = Value.lrd;
1675 else
1676 *plrd = 0;
1677 return rc;
1678}
1679#endif
1680
1681
1682/**
1683 * Queries a descriptor table register value.
1684 *
1685 * @retval VINF_SUCCESS
1686 * @retval VERR_INVALID_VM_HANDLE
1687 * @retval VERR_INVALID_CPU_ID
1688 * @retval VERR_DBGF_REGISTER_NOT_FOUND
1689 * @retval VERR_DBGF_UNSUPPORTED_CAST
1690 * @retval VINF_DBGF_TRUNCATED_REGISTER
1691 * @retval VINF_DBGF_ZERO_EXTENDED_REGISTER
1692 *
1693 * @param pUVM The user mode VM handle.
1694 * @param idDefCpu The default target CPU ID, VMCPUID_ANY if not
1695 * applicable. Can be OR'ed with
1696 * DBGFREG_HYPER_VMCPUID.
1697 * @param pszReg The register that's being queried. Except for
1698 * CPU registers, this must be on the form
1699 * "set.reg[.sub]".
1700 * @param pu64Base Where to store the register base value.
1701 * @param pu32Limit Where to store the register limit value.
1702 */
1703VMMR3DECL(int) DBGFR3RegNmQueryXdtr(PUVM pUVM, VMCPUID idDefCpu, const char *pszReg, uint64_t *pu64Base, uint32_t *pu32Limit)
1704{
1705 DBGFREGVAL Value;
1706 int rc = dbgfR3RegNmQueryWorker(pUVM, idDefCpu, pszReg, DBGFREGVALTYPE_DTR, &Value, NULL);
1707 if (RT_SUCCESS(rc))
1708 {
1709 *pu64Base = Value.dtr.u64Base;
1710 *pu32Limit = Value.dtr.u32Limit;
1711 }
1712 else
1713 {
1714 *pu64Base = 0;
1715 *pu32Limit = 0;
1716 }
1717 return rc;
1718}
1719
1720
1721/// @todo VMMR3DECL(int) DBGFR3RegNmQueryBatch(PUVM pUVM,VMCPUID idDefCpu, DBGFREGENTRYNM paRegs, size_t cRegs);
1722
1723
1724/**
1725 * Gets the number of registers returned by DBGFR3RegNmQueryAll.
1726 *
1727 * @returns VBox status code.
1728 * @param pUVM The user mode VM handle.
1729 * @param pcRegs Where to return the register count.
1730 */
1731VMMR3DECL(int) DBGFR3RegNmQueryAllCount(PUVM pUVM, size_t *pcRegs)
1732{
1733 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1734 *pcRegs = pUVM->dbgf.s.cRegs;
1735 return VINF_SUCCESS;
1736}
1737
1738
1739/**
1740 * Pad register entries.
1741 *
1742 * @param paRegs The output array.
1743 * @param cRegs The size of the output array.
1744 * @param iReg The first register to pad.
1745 * @param cRegsToPad The number of registers to pad.
1746 */
1747static void dbgfR3RegNmQueryAllPadEntries(PDBGFREGENTRYNM paRegs, size_t cRegs, size_t iReg, size_t cRegsToPad)
1748{
1749 if (iReg < cRegs)
1750 {
1751 size_t iEndReg = iReg + cRegsToPad;
1752 if (iEndReg > cRegs)
1753 iEndReg = cRegs;
1754 while (iReg < iEndReg)
1755 {
1756 paRegs[iReg].pszName = NULL;
1757 paRegs[iReg].enmType = DBGFREGVALTYPE_END;
1758 dbgfR3RegValClear(&paRegs[iReg].Val);
1759 iReg++;
1760 }
1761 }
1762}
1763
1764
1765/**
1766 * Query all registers in a set.
1767 *
1768 * @param pSet The set.
1769 * @param cRegsToQuery The number of registers to query.
1770 * @param paRegs The output array.
1771 * @param cRegs The size of the output array.
1772 */
1773static void dbgfR3RegNmQueryAllInSet(PCDBGFREGSET pSet, size_t cRegsToQuery, PDBGFREGENTRYNM paRegs, size_t cRegs)
1774{
1775 if (cRegsToQuery > pSet->cDescs)
1776 cRegsToQuery = pSet->cDescs;
1777 if (cRegsToQuery > cRegs)
1778 cRegsToQuery = cRegs;
1779
1780 for (size_t iReg = 0; iReg < cRegsToQuery; iReg++)
1781 {
1782 paRegs[iReg].enmType = pSet->paDescs[iReg].enmType;
1783 paRegs[iReg].pszName = pSet->paLookupRecs[iReg].Core.pszString;
1784 dbgfR3RegValClear(&paRegs[iReg].Val);
1785 int rc2 = pSet->paDescs[iReg].pfnGet(pSet->uUserArg.pv, &pSet->paDescs[iReg], &paRegs[iReg].Val);
1786 AssertRCSuccess(rc2);
1787 if (RT_FAILURE(rc2))
1788 dbgfR3RegValClear(&paRegs[iReg].Val);
1789 }
1790}
1791
1792
1793/**
1794 * @callback_method_impl{FNRTSTRSPACECALLBACK, Worker used by
1795 * dbgfR3RegNmQueryAllWorker}
1796 */
1797static DECLCALLBACK(int) dbgfR3RegNmQueryAllEnum(PRTSTRSPACECORE pStr, void *pvUser)
1798{
1799 PCDBGFREGSET pSet = (PCDBGFREGSET)pStr;
1800 if (pSet->enmType != DBGFREGSETTYPE_CPU)
1801 {
1802 PDBGFR3REGNMQUERYALLARGS pArgs = (PDBGFR3REGNMQUERYALLARGS)pvUser;
1803 if (pArgs->iReg < pArgs->cRegs)
1804 dbgfR3RegNmQueryAllInSet(pSet, pSet->cDescs, &pArgs->paRegs[pArgs->iReg], pArgs->cRegs - pArgs->iReg);
1805 pArgs->iReg += pSet->cDescs;
1806 }
1807
1808 return 0;
1809}
1810
1811
1812/**
1813 * @callback_method_impl{FNVMMEMTRENDEZVOUS, Worker used by DBGFR3RegNmQueryAll}
1814 */
1815static DECLCALLBACK(VBOXSTRICTRC) dbgfR3RegNmQueryAllWorker(PVM pVM, PVMCPU pVCpu, void *pvUser)
1816{
1817 PDBGFR3REGNMQUERYALLARGS pArgs = (PDBGFR3REGNMQUERYALLARGS)pvUser;
1818 PDBGFREGENTRYNM paRegs = pArgs->paRegs;
1819 size_t const cRegs = pArgs->cRegs;
1820 PUVM pUVM = pVM->pUVM;
1821 PUVMCPU pUVCpu = pVCpu->pUVCpu;
1822
1823 DBGF_REG_DB_LOCK_READ(pUVM);
1824
1825 /*
1826 * My guest CPU registers.
1827 */
1828 size_t iCpuReg = pVCpu->idCpu * DBGFREG_ALL_COUNT;
1829 if (pUVCpu->dbgf.s.pGuestRegSet)
1830 {
1831 if (iCpuReg < cRegs)
1832 dbgfR3RegNmQueryAllInSet(pUVCpu->dbgf.s.pGuestRegSet, DBGFREG_ALL_COUNT, &paRegs[iCpuReg], cRegs - iCpuReg);
1833 }
1834 else
1835 dbgfR3RegNmQueryAllPadEntries(paRegs, cRegs, iCpuReg, DBGFREG_ALL_COUNT);
1836
1837 /*
1838 * My hypervisor CPU registers.
1839 */
1840 iCpuReg = pUVM->cCpus * DBGFREG_ALL_COUNT + pUVCpu->idCpu * DBGFREG_ALL_COUNT;
1841 if (pUVCpu->dbgf.s.pHyperRegSet)
1842 {
1843 if (iCpuReg < cRegs)
1844 dbgfR3RegNmQueryAllInSet(pUVCpu->dbgf.s.pHyperRegSet, DBGFREG_ALL_COUNT, &paRegs[iCpuReg], cRegs - iCpuReg);
1845 }
1846 else
1847 dbgfR3RegNmQueryAllPadEntries(paRegs, cRegs, iCpuReg, DBGFREG_ALL_COUNT);
1848
1849 /*
1850 * The primary CPU does all the other registers.
1851 */
1852 if (pUVCpu->idCpu == 0)
1853 {
1854 pArgs->iReg = pUVM->cCpus * DBGFREG_ALL_COUNT * 2;
1855 RTStrSpaceEnumerate(&pUVM->dbgf.s.RegSetSpace, dbgfR3RegNmQueryAllEnum, pArgs);
1856 dbgfR3RegNmQueryAllPadEntries(paRegs, cRegs, pArgs->iReg, cRegs);
1857 }
1858
1859 DBGF_REG_DB_UNLOCK_READ(pUVM);
1860 return VINF_SUCCESS; /* Ignore errors. */
1861}
1862
1863
1864/**
1865 * Queries all register.
1866 *
1867 * @returns VBox status code.
1868 * @param pUVM The user mode VM handle.
1869 * @param paRegs The output register value array. The register
1870 * name string is read only and shall not be freed
1871 * or modified.
1872 * @param cRegs The number of entries in @a paRegs. The
1873 * correct size can be obtained by calling
1874 * DBGFR3RegNmQueryAllCount.
1875 */
1876VMMR3DECL(int) DBGFR3RegNmQueryAll(PUVM pUVM, PDBGFREGENTRYNM paRegs, size_t cRegs)
1877{
1878 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1879 PVM pVM = pUVM->pVM;
1880 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1881 AssertPtrReturn(paRegs, VERR_INVALID_POINTER);
1882 AssertReturn(cRegs > 0, VERR_OUT_OF_RANGE);
1883
1884 DBGFR3REGNMQUERYALLARGS Args;
1885 Args.paRegs = paRegs;
1886 Args.cRegs = cRegs;
1887
1888 return VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ALL_AT_ONCE, dbgfR3RegNmQueryAllWorker, &Args);
1889}
1890
1891
1892VMMR3DECL(int) DBGFR3RegNmSet(PUVM pUVM, VMCPUID idDefCpu, const char *pszReg, PCDBGFREGVAL pValue, DBGFREGVALTYPE enmType)
1893{
1894 NOREF(pUVM); NOREF(idDefCpu); NOREF(pszReg); NOREF(pValue); NOREF(enmType);
1895 return VERR_NOT_IMPLEMENTED;
1896}
1897
1898
1899/**
1900 * Internal worker for DBGFR3RegFormatValue, cbTmp is sufficent.
1901 *
1902 * @copydoc DBGFR3RegFormatValue
1903 */
1904DECLINLINE(ssize_t) dbgfR3RegFormatValueInt(char *pszTmp, size_t cbTmp, PCDBGFREGVAL pValue, DBGFREGVALTYPE enmType,
1905 unsigned uBase, signed int cchWidth, signed int cchPrecision, uint32_t fFlags)
1906{
1907 switch (enmType)
1908 {
1909 case DBGFREGVALTYPE_U8:
1910 return RTStrFormatU8(pszTmp, cbTmp, pValue->u8, uBase, cchWidth, cchPrecision, fFlags);
1911 case DBGFREGVALTYPE_U16:
1912 return RTStrFormatU16(pszTmp, cbTmp, pValue->u16, uBase, cchWidth, cchPrecision, fFlags);
1913 case DBGFREGVALTYPE_U32:
1914 return RTStrFormatU32(pszTmp, cbTmp, pValue->u32, uBase, cchWidth, cchPrecision, fFlags);
1915 case DBGFREGVALTYPE_U64:
1916 return RTStrFormatU64(pszTmp, cbTmp, pValue->u64, uBase, cchWidth, cchPrecision, fFlags);
1917 case DBGFREGVALTYPE_U128:
1918 return RTStrFormatU128(pszTmp, cbTmp, &pValue->u128, uBase, cchWidth, cchPrecision, fFlags);
1919 case DBGFREGVALTYPE_R80:
1920 return RTStrFormatR80u2(pszTmp, cbTmp, &pValue->r80Ex, cchWidth, cchPrecision, fFlags);
1921 case DBGFREGVALTYPE_DTR:
1922 {
1923 ssize_t cch = RTStrFormatU64(pszTmp, cbTmp, pValue->dtr.u64Base,
1924 16, 2+16, 0, RTSTR_F_SPECIAL | RTSTR_F_ZEROPAD);
1925 AssertReturn(cch > 0, VERR_DBGF_REG_IPE_1);
1926 pszTmp[cch++] = ':';
1927 cch += RTStrFormatU64(&pszTmp[cch], cbTmp - cch, pValue->dtr.u32Limit,
1928 16, 4, 0, RTSTR_F_ZEROPAD | RTSTR_F_32BIT);
1929 return cch;
1930 }
1931
1932 case DBGFREGVALTYPE_32BIT_HACK:
1933 case DBGFREGVALTYPE_END:
1934 case DBGFREGVALTYPE_INVALID:
1935 break;
1936 /* no default, want gcc warnings */
1937 }
1938
1939 RTStrPrintf(pszTmp, cbTmp, "!enmType=%d!", enmType);
1940 return VERR_DBGF_REG_IPE_2;
1941}
1942
1943
1944/**
1945 * Format a register value, extended version.
1946 *
1947 * @returns The number of bytes returned, VERR_BUFFER_OVERFLOW on failure.
1948 * @param pszBuf The output buffer.
1949 * @param cbBuf The size of the output buffer.
1950 * @param pValue The value to format.
1951 * @param enmType The value type.
1952 * @param uBase The base (ignored if not applicable).
1953 * @param cchWidth The width if RTSTR_F_WIDTH is set, otherwise
1954 * ignored.
1955 * @param cchPrecision The width if RTSTR_F_PRECISION is set, otherwise
1956 * ignored.
1957 * @param fFlags String formatting flags, RTSTR_F_XXX.
1958 */
1959VMMR3DECL(ssize_t) DBGFR3RegFormatValueEx(char *pszBuf, size_t cbBuf, PCDBGFREGVAL pValue, DBGFREGVALTYPE enmType,
1960 unsigned uBase, signed int cchWidth, signed int cchPrecision, uint32_t fFlags)
1961{
1962 /*
1963 * Format to temporary buffer using worker shared with dbgfR3RegPrintfCbFormatNormal.
1964 */
1965 char szTmp[160];
1966 ssize_t cchOutput = dbgfR3RegFormatValueInt(szTmp, sizeof(szTmp), pValue, enmType, uBase, cchWidth, cchPrecision, fFlags);
1967 if (cchOutput > 0)
1968 {
1969 if ((size_t)cchOutput < cbBuf)
1970 memcpy(pszBuf, szTmp, cchOutput + 1);
1971 else
1972 {
1973 if (cbBuf)
1974 {
1975 memcpy(pszBuf, szTmp, cbBuf - 1);
1976 pszBuf[cbBuf - 1] = '\0';
1977 }
1978 cchOutput = VERR_BUFFER_OVERFLOW;
1979 }
1980 }
1981 return cchOutput;
1982}
1983
1984
1985/**
1986 * Format a register value as hexadecimal and with default width according to
1987 * the type.
1988 *
1989 * @returns The number of bytes returned, VERR_BUFFER_OVERFLOW on failure.
1990 * @param pszBuf The output buffer.
1991 * @param cbBuf The size of the output buffer.
1992 * @param pValue The value to format.
1993 * @param enmType The value type.
1994 * @param fSpecial Same as RTSTR_F_SPECIAL.
1995 */
1996VMMR3DECL(ssize_t) DBGFR3RegFormatValue(char *pszBuf, size_t cbBuf, PCDBGFREGVAL pValue, DBGFREGVALTYPE enmType, bool fSpecial)
1997{
1998 int cchWidth = 0;
1999 switch (enmType)
2000 {
2001 case DBGFREGVALTYPE_U8: cchWidth = 2 + fSpecial*2; break;
2002 case DBGFREGVALTYPE_U16: cchWidth = 4 + fSpecial*2; break;
2003 case DBGFREGVALTYPE_U32: cchWidth = 8 + fSpecial*2; break;
2004 case DBGFREGVALTYPE_U64: cchWidth = 16 + fSpecial*2; break;
2005 case DBGFREGVALTYPE_U128: cchWidth = 32 + fSpecial*2; break;
2006 case DBGFREGVALTYPE_R80: cchWidth = 0; break;
2007 case DBGFREGVALTYPE_DTR: cchWidth = 16+1+4 + fSpecial*2; break;
2008
2009 case DBGFREGVALTYPE_32BIT_HACK:
2010 case DBGFREGVALTYPE_END:
2011 case DBGFREGVALTYPE_INVALID:
2012 break;
2013 /* no default, want gcc warnings */
2014 }
2015 uint32_t fFlags = RTSTR_F_ZEROPAD;
2016 if (fSpecial)
2017 fFlags |= RTSTR_F_SPECIAL;
2018 if (cchWidth != 0)
2019 fFlags |= RTSTR_F_WIDTH;
2020 return DBGFR3RegFormatValueEx(pszBuf, cbBuf, pValue, enmType, 16, cchWidth, 0, fFlags);
2021}
2022
2023
2024/**
2025 * Format a register using special hacks as well as sub-field specifications
2026 * (the latter isn't implemented yet).
2027 */
2028static size_t
2029dbgfR3RegPrintfCbFormatField(PDBGFR3REGPRINTFARGS pThis, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
2030 PCDBGFREGLOOKUP pLookupRec, int cchWidth, int cchPrecision, unsigned fFlags)
2031{
2032 char szTmp[160];
2033
2034 NOREF(cchWidth); NOREF(cchPrecision); NOREF(fFlags);
2035
2036 /*
2037 * Retrieve the register value.
2038 */
2039 DBGFREGVAL Value;
2040 DBGFREGVALTYPE enmType;
2041 int rc = dbgfR3RegNmQueryWorkerOnCpu(pThis->pUVM, pLookupRec, DBGFREGVALTYPE_END, &Value, &enmType);
2042 if (RT_FAILURE(rc))
2043 {
2044 PCRTSTATUSMSG pErr = RTErrGet(rc);
2045 if (pErr)
2046 return pfnOutput(pvArgOutput, pErr->pszDefine, strlen(pErr->pszDefine));
2047 return pfnOutput(pvArgOutput, szTmp, RTStrPrintf(szTmp, sizeof(szTmp), "rc=%d", rc));
2048 }
2049
2050 char *psz = szTmp;
2051
2052 /*
2053 * Special case: Format eflags.
2054 */
2055 if ( pLookupRec->pSet->enmType == DBGFREGSETTYPE_CPU
2056 && pLookupRec->pDesc->enmReg == DBGFREG_RFLAGS
2057 && pLookupRec->pSubField == NULL)
2058 {
2059 rc = dbgfR3RegValCast(&Value, enmType, DBGFREGVALTYPE_U32);
2060 AssertRC(rc);
2061 uint32_t const efl = Value.u32;
2062
2063 /* the iopl */
2064 psz += RTStrPrintf(psz, sizeof(szTmp) / 2, "iopl=%u ", X86_EFL_GET_IOPL(efl));
2065
2066 /* add flags */
2067 static const struct
2068 {
2069 const char *pszSet;
2070 const char *pszClear;
2071 uint32_t fFlag;
2072 } aFlags[] =
2073 {
2074 { "vip",NULL, X86_EFL_VIP },
2075 { "vif",NULL, X86_EFL_VIF },
2076 { "ac", NULL, X86_EFL_AC },
2077 { "vm", NULL, X86_EFL_VM },
2078 { "rf", NULL, X86_EFL_RF },
2079 { "nt", NULL, X86_EFL_NT },
2080 { "ov", "nv", X86_EFL_OF },
2081 { "dn", "up", X86_EFL_DF },
2082 { "ei", "di", X86_EFL_IF },
2083 { "tf", NULL, X86_EFL_TF },
2084 { "ng", "pl", X86_EFL_SF },
2085 { "zr", "nz", X86_EFL_ZF },
2086 { "ac", "na", X86_EFL_AF },
2087 { "po", "pe", X86_EFL_PF },
2088 { "cy", "nc", X86_EFL_CF },
2089 };
2090 for (unsigned i = 0; i < RT_ELEMENTS(aFlags); i++)
2091 {
2092 const char *pszAdd = aFlags[i].fFlag & efl ? aFlags[i].pszSet : aFlags[i].pszClear;
2093 if (pszAdd)
2094 {
2095 *psz++ = *pszAdd++;
2096 *psz++ = *pszAdd++;
2097 if (*pszAdd)
2098 *psz++ = *pszAdd++;
2099 *psz++ = ' ';
2100 }
2101 }
2102
2103 /* drop trailing space */
2104 psz--;
2105 }
2106 else
2107 {
2108 /*
2109 * General case.
2110 */
2111 AssertMsgFailed(("Not implemented: %s\n", pLookupRec->Core.pszString));
2112 return pfnOutput(pvArgOutput, pLookupRec->Core.pszString, pLookupRec->Core.cchString);
2113 }
2114
2115 /* Output the string. */
2116 return pfnOutput(pvArgOutput, szTmp, psz - &szTmp[0]);
2117}
2118
2119
2120/**
2121 * Formats a register having parsed up to the register name.
2122 */
2123static size_t
2124dbgfR3RegPrintfCbFormatNormal(PDBGFR3REGPRINTFARGS pThis, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
2125 PCDBGFREGLOOKUP pLookupRec, unsigned uBase, int cchWidth, int cchPrecision, unsigned fFlags)
2126{
2127 char szTmp[160];
2128
2129 /*
2130 * Get the register value.
2131 */
2132 DBGFREGVAL Value;
2133 DBGFREGVALTYPE enmType;
2134 int rc = dbgfR3RegNmQueryWorkerOnCpu(pThis->pUVM, pLookupRec, DBGFREGVALTYPE_END, &Value, &enmType);
2135 if (RT_FAILURE(rc))
2136 {
2137 PCRTSTATUSMSG pErr = RTErrGet(rc);
2138 if (pErr)
2139 return pfnOutput(pvArgOutput, pErr->pszDefine, strlen(pErr->pszDefine));
2140 return pfnOutput(pvArgOutput, szTmp, RTStrPrintf(szTmp, sizeof(szTmp), "rc=%d", rc));
2141 }
2142
2143 /*
2144 * Format the value.
2145 */
2146 ssize_t cchOutput = dbgfR3RegFormatValueInt(szTmp, sizeof(szTmp), &Value, enmType, uBase, cchWidth, cchPrecision, fFlags);
2147 if (RT_UNLIKELY(cchOutput <= 0))
2148 {
2149 AssertFailed();
2150 return pfnOutput(pvArgOutput, "internal-error", sizeof("internal-error") - 1);
2151 }
2152 return pfnOutput(pvArgOutput, szTmp, cchOutput);
2153}
2154
2155
2156/**
2157 * @callback_method_impl{FNSTRFORMAT}
2158 */
2159static DECLCALLBACK(size_t)
2160dbgfR3RegPrintfCbFormat(void *pvArg, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
2161 const char **ppszFormat, va_list *pArgs, int cchWidth,
2162 int cchPrecision, unsigned fFlags, char chArgSize)
2163{
2164 NOREF(pArgs); NOREF(chArgSize);
2165
2166 /*
2167 * Parse the format type and hand the job to the appropriate worker.
2168 */
2169 PDBGFR3REGPRINTFARGS pThis = (PDBGFR3REGPRINTFARGS)pvArg;
2170 const char *pszFormat = *ppszFormat;
2171 if ( pszFormat[0] != 'V'
2172 || pszFormat[1] != 'R')
2173 {
2174 AssertMsgFailed(("'%s'\n", pszFormat));
2175 return 0;
2176 }
2177 unsigned offCurly = 2;
2178 if (pszFormat[offCurly] != '{')
2179 {
2180 AssertMsgReturn(pszFormat[offCurly], ("'%s'\n", pszFormat), 0);
2181 offCurly++;
2182 AssertMsgReturn(pszFormat[offCurly] == '{', ("'%s'\n", pszFormat), 0);
2183 }
2184 const char *pachReg = &pszFormat[offCurly + 1];
2185
2186 /*
2187 * The end and length of the register.
2188 */
2189 const char *pszEnd = strchr(pachReg, '}');
2190 AssertMsgReturn(pszEnd, ("Missing closing curly bracket: '%s'\n", pszFormat), 0);
2191 size_t const cchReg = pszEnd - pachReg;
2192
2193 /*
2194 * Look up the register - same as dbgfR3RegResolve, except for locking and
2195 * input string termination.
2196 */
2197 PRTSTRSPACE pRegSpace = &pThis->pUVM->dbgf.s.RegSpace;
2198 /* Try looking up the name without any case folding or cpu prefixing. */
2199 PCDBGFREGLOOKUP pLookupRec = (PCDBGFREGLOOKUP)RTStrSpaceGetN(pRegSpace, pachReg, cchReg);
2200 if (!pLookupRec)
2201 {
2202 /* Lower case it and try again. */
2203 char szName[DBGF_REG_MAX_NAME * 4 + 16];
2204 ssize_t cchFolded = dbgfR3RegCopyToLower(pachReg, cchReg, szName, sizeof(szName) - DBGF_REG_MAX_NAME);
2205 if (cchFolded > 0)
2206 pLookupRec = (PCDBGFREGLOOKUP)RTStrSpaceGet(pRegSpace, szName);
2207 if ( !pLookupRec
2208 && cchFolded >= 0
2209 && pThis->idCpu != VMCPUID_ANY)
2210 {
2211 /* Prefix it with the specified CPU set. */
2212 size_t cchCpuSet = RTStrPrintf(szName, sizeof(szName), pThis->fGuestRegs ? "cpu%u." : "hypercpu%u.", pThis->idCpu);
2213 dbgfR3RegCopyToLower(pachReg, cchReg, &szName[cchCpuSet], sizeof(szName) - cchCpuSet);
2214 pLookupRec = (PCDBGFREGLOOKUP)RTStrSpaceGet(pRegSpace, szName);
2215 }
2216 }
2217 AssertMsgReturn(pLookupRec, ("'%s'\n", pszFormat), 0);
2218 AssertMsgReturn( pLookupRec->pSet->enmType != DBGFREGSETTYPE_CPU
2219 || pLookupRec->pSet->uUserArg.pVCpu->idCpu == pThis->idCpu,
2220 ("'%s' idCpu=%u, pSet/cpu=%u\n", pszFormat, pThis->idCpu, pLookupRec->pSet->uUserArg.pVCpu->idCpu),
2221 0);
2222
2223 /*
2224 * Commit the parsed format string. Up to this point it is nice to know
2225 * what register lookup failed and such, so we've delayed comitting.
2226 */
2227 *ppszFormat = pszEnd + 1;
2228
2229 /*
2230 * Call the responsible worker.
2231 */
2232 switch (pszFormat[offCurly - 1])
2233 {
2234 case 'R': /* %VR{} */
2235 case 'X': /* %VRX{} */
2236 return dbgfR3RegPrintfCbFormatNormal(pThis, pfnOutput, pvArgOutput, pLookupRec,
2237 16, cchWidth, cchPrecision, fFlags);
2238 case 'U':
2239 return dbgfR3RegPrintfCbFormatNormal(pThis, pfnOutput, pvArgOutput, pLookupRec,
2240 10, cchWidth, cchPrecision, fFlags);
2241 case 'O':
2242 return dbgfR3RegPrintfCbFormatNormal(pThis, pfnOutput, pvArgOutput, pLookupRec,
2243 8, cchWidth, cchPrecision, fFlags);
2244 case 'B':
2245 return dbgfR3RegPrintfCbFormatNormal(pThis, pfnOutput, pvArgOutput, pLookupRec,
2246 2, cchWidth, cchPrecision, fFlags);
2247 case 'F':
2248 return dbgfR3RegPrintfCbFormatField(pThis, pfnOutput, pvArgOutput, pLookupRec, cchWidth, cchPrecision, fFlags);
2249 default:
2250 AssertFailed();
2251 return 0;
2252 }
2253}
2254
2255
2256
2257/**
2258 * @callback_method_impl{FNRTSTROUTPUT}
2259 */
2260static DECLCALLBACK(size_t)
2261dbgfR3RegPrintfCbOutput(void *pvArg, const char *pachChars, size_t cbChars)
2262{
2263 PDBGFR3REGPRINTFARGS pArgs = (PDBGFR3REGPRINTFARGS)pvArg;
2264 size_t cbToCopy = cbChars;
2265 if (cbToCopy >= pArgs->cchLeftBuf)
2266 {
2267 if (RT_SUCCESS(pArgs->rc))
2268 pArgs->rc = VERR_BUFFER_OVERFLOW;
2269 cbToCopy = pArgs->cchLeftBuf;
2270 }
2271 if (cbToCopy > 0)
2272 {
2273 memcpy(&pArgs->pszBuf[pArgs->offBuf], pachChars, cbToCopy);
2274 pArgs->offBuf += cbToCopy;
2275 pArgs->cchLeftBuf -= cbToCopy;
2276 pArgs->pszBuf[pArgs->offBuf] = '\0';
2277 }
2278 return cbToCopy;
2279}
2280
2281
2282/**
2283 * On CPU worker for the register formatting, used by DBGFR3RegPrintfV.
2284 *
2285 * @returns VBox status code.
2286 *
2287 * @param pArgs The argument package and state.
2288 */
2289static DECLCALLBACK(int) dbgfR3RegPrintfWorkerOnCpu(PDBGFR3REGPRINTFARGS pArgs)
2290{
2291 DBGF_REG_DB_LOCK_READ(pArgs->pUVM);
2292 RTStrFormatV(dbgfR3RegPrintfCbOutput, pArgs, dbgfR3RegPrintfCbFormat, pArgs, pArgs->pszFormat, pArgs->va);
2293 DBGF_REG_DB_UNLOCK_READ(pArgs->pUVM);
2294 return pArgs->rc;
2295}
2296
2297
2298/**
2299 * Format a registers.
2300 *
2301 * This is restricted to registers from one CPU, that specified by @a idCpu.
2302 *
2303 * @returns VBox status code.
2304 * @param pUVM The user mode VM handle.
2305 * @param idCpu The CPU ID of any CPU registers that may be
2306 * printed, pass VMCPUID_ANY if not applicable.
2307 * @param pszBuf The output buffer.
2308 * @param cbBuf The size of the output buffer.
2309 * @param pszFormat The format string. Register names are given by
2310 * %VR{name}, they take no arguments.
2311 * @param va Other format arguments.
2312 */
2313VMMR3DECL(int) DBGFR3RegPrintfV(PUVM pUVM, VMCPUID idCpu, char *pszBuf, size_t cbBuf, const char *pszFormat, va_list va)
2314{
2315 AssertPtrReturn(pszBuf, VERR_INVALID_POINTER);
2316 AssertReturn(cbBuf > 0, VERR_BUFFER_OVERFLOW);
2317 *pszBuf = '\0';
2318
2319 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2320 AssertReturn((idCpu & ~DBGFREG_HYPER_VMCPUID) < pUVM->cCpus || idCpu == VMCPUID_ANY, VERR_INVALID_CPU_ID);
2321 AssertPtrReturn(pszFormat, VERR_INVALID_POINTER);
2322
2323 /*
2324 * Set up an argument package and execute the formatting on the
2325 * specified CPU.
2326 */
2327 DBGFR3REGPRINTFARGS Args;
2328 Args.pUVM = pUVM;
2329 Args.idCpu = idCpu != VMCPUID_ANY ? idCpu & ~DBGFREG_HYPER_VMCPUID : idCpu;
2330 Args.fGuestRegs = idCpu != VMCPUID_ANY && !(idCpu & DBGFREG_HYPER_VMCPUID);
2331 Args.pszBuf = pszBuf;
2332 Args.pszFormat = pszFormat;
2333 va_copy(Args.va, va);
2334 Args.offBuf = 0;
2335 Args.cchLeftBuf = cbBuf - 1;
2336 Args.rc = VINF_SUCCESS;
2337 int rc = VMR3ReqPriorityCallWaitU(pUVM, Args.idCpu, (PFNRT)dbgfR3RegPrintfWorkerOnCpu, 1, &Args);
2338 va_end(Args.va);
2339 return rc;
2340}
2341
2342
2343/**
2344 * Format a registers.
2345 *
2346 * This is restricted to registers from one CPU, that specified by @a idCpu.
2347 *
2348 * @returns VBox status code.
2349 * @param pUVM The user mode VM handle.
2350 * @param idCpu The CPU ID of any CPU registers that may be
2351 * printed, pass VMCPUID_ANY if not applicable.
2352 * @param pszBuf The output buffer.
2353 * @param cbBuf The size of the output buffer.
2354 * @param pszFormat The format string. Register names are given by
2355 * %VR{name}, %VRU{name}, %VRO{name} and
2356 * %VRB{name}, which are hexadecimal, (unsigned)
2357 * decimal, octal and binary representation. None
2358 * of these types takes any arguments.
2359 * @param ... Other format arguments.
2360 */
2361VMMR3DECL(int) DBGFR3RegPrintf(PUVM pUVM, VMCPUID idCpu, char *pszBuf, size_t cbBuf, const char *pszFormat, ...)
2362{
2363 va_list va;
2364 va_start(va, pszFormat);
2365 int rc = DBGFR3RegPrintfV(pUVM, idCpu, pszBuf, cbBuf, pszFormat, va);
2366 va_end(va);
2367 return rc;
2368}
2369
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette