1 | /* $Id: EMR3Dbg.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * EM - Execution Monitor / Manager, Debugger Related Bits.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 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_EM
|
---|
23 | #include <VBox/vmm/em.h>
|
---|
24 | #include <VBox/vmm/hm.h>
|
---|
25 | #include <VBox/vmm/nem.h>
|
---|
26 | #include <VBox/dbg.h>
|
---|
27 | #include "EMInternal.h"
|
---|
28 | #include <VBox/vmm/vm.h>
|
---|
29 | #include <iprt/string.h>
|
---|
30 | #include <iprt/ctype.h>
|
---|
31 |
|
---|
32 |
|
---|
33 | /** @callback_method_impl{FNDBGCCMD,
|
---|
34 | * Implements the '.alliem' command. }
|
---|
35 | */
|
---|
36 | static DECLCALLBACK(int) enmR3DbgCmdAllIem(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
|
---|
37 | {
|
---|
38 | int rc;
|
---|
39 | bool f;
|
---|
40 |
|
---|
41 | if (cArgs == 0)
|
---|
42 | {
|
---|
43 | rc = EMR3QueryExecutionPolicy(pUVM, EMEXECPOLICY_IEM_ALL, &f);
|
---|
44 | if (RT_FAILURE(rc))
|
---|
45 | return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "EMR3QueryExecutionPolicy(,EMEXECPOLICY_IEM_ALL,");
|
---|
46 | DBGCCmdHlpPrintf(pCmdHlp, f ? "alliem: enabled\n" : "alliem: disabled\n");
|
---|
47 | }
|
---|
48 | else
|
---|
49 | {
|
---|
50 | rc = DBGCCmdHlpVarToBool(pCmdHlp, &paArgs[0], &f);
|
---|
51 | if (RT_FAILURE(rc))
|
---|
52 | return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToBool");
|
---|
53 | rc = EMR3SetExecutionPolicy(pUVM, EMEXECPOLICY_IEM_ALL, f);
|
---|
54 | if (RT_FAILURE(rc))
|
---|
55 | return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "EMR3SetExecutionPolicy(,EMEXECPOLICY_IEM_ALL,%RTbool)", f);
|
---|
56 | }
|
---|
57 | return VINF_SUCCESS;
|
---|
58 | }
|
---|
59 |
|
---|
60 |
|
---|
61 | /** Describes a optional boolean argument. */
|
---|
62 | static DBGCVARDESC const g_BoolArg = { 0, 1, DBGCVAR_CAT_ANY, 0, "boolean", "Boolean value." };
|
---|
63 |
|
---|
64 | /** Commands. */
|
---|
65 | static DBGCCMD const g_aCmds[] =
|
---|
66 | {
|
---|
67 | {
|
---|
68 | "alliem", 0, 1, &g_BoolArg, 1, 0, enmR3DbgCmdAllIem, "[boolean]",
|
---|
69 | "Enables or disabled executing ALL code in IEM, if no arguments are given it displays the current status."
|
---|
70 | },
|
---|
71 | };
|
---|
72 |
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Translates EMEXITTYPE into a name.
|
---|
76 | *
|
---|
77 | * @returns Pointer to read-only name, NULL if unknown type.
|
---|
78 | * @param enmExitType The exit type to name.
|
---|
79 | */
|
---|
80 | VMM_INT_DECL(const char *) EMR3GetExitTypeName(EMEXITTYPE enmExitType)
|
---|
81 | {
|
---|
82 | switch (enmExitType)
|
---|
83 | {
|
---|
84 | case EMEXITTYPE_INVALID: return "invalid";
|
---|
85 | case EMEXITTYPE_IO_PORT_READ: return "I/O port read";
|
---|
86 | case EMEXITTYPE_IO_PORT_WRITE: return "I/O port write";
|
---|
87 | case EMEXITTYPE_IO_PORT_STR_READ: return "I/O port string read";
|
---|
88 | case EMEXITTYPE_IO_PORT_STR_WRITE: return "I/O port string write";
|
---|
89 | case EMEXITTYPE_MMIO: return "MMIO access";
|
---|
90 | case EMEXITTYPE_MMIO_READ: return "MMIO read";
|
---|
91 | case EMEXITTYPE_MMIO_WRITE: return "MMIO write";
|
---|
92 | case EMEXITTYPE_MSR_READ: return "MSR read";
|
---|
93 | case EMEXITTYPE_MSR_WRITE: return "MSR write";
|
---|
94 | case EMEXITTYPE_CPUID: return "CPUID";
|
---|
95 | case EMEXITTYPE_RDTSC: return "RDTSC";
|
---|
96 | case EMEXITTYPE_MOV_CRX: return "MOV CRx";
|
---|
97 | case EMEXITTYPE_MOV_DRX: return "MOV DRx";
|
---|
98 |
|
---|
99 | /* Raw-mode only: */
|
---|
100 | case EMEXITTYPE_INVLPG: return "INVLPG";
|
---|
101 | case EMEXITTYPE_LLDT: return "LLDT";
|
---|
102 | case EMEXITTYPE_RDPMC: return "RDPMC";
|
---|
103 | case EMEXITTYPE_CLTS: return "CLTS";
|
---|
104 | case EMEXITTYPE_STI: return "STI";
|
---|
105 | case EMEXITTYPE_INT: return "INT";
|
---|
106 | case EMEXITTYPE_SYSCALL: return "SYSCALL";
|
---|
107 | case EMEXITTYPE_SYSENTER: return "SYSENTER";
|
---|
108 | case EMEXITTYPE_HLT: return "HLT";
|
---|
109 | }
|
---|
110 | return NULL;
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Translates flags+type into an exit name.
|
---|
116 | *
|
---|
117 | * @returns Exit name.
|
---|
118 | * @param uFlagsAndType The exit to name.
|
---|
119 | * @param pszFallback Buffer for formatting a numeric fallback.
|
---|
120 | * @param cbFallback Size of fallback buffer.
|
---|
121 | */
|
---|
122 | static const char *emR3HistoryGetExitName(uint32_t uFlagsAndType, char *pszFallback, size_t cbFallback)
|
---|
123 | {
|
---|
124 | const char *pszExitName;
|
---|
125 | switch (uFlagsAndType & EMEXIT_F_KIND_MASK)
|
---|
126 | {
|
---|
127 | case EMEXIT_F_KIND_EM:
|
---|
128 | pszExitName = EMR3GetExitTypeName((EMEXITTYPE)(uFlagsAndType & EMEXIT_F_TYPE_MASK));
|
---|
129 | break;
|
---|
130 |
|
---|
131 | case EMEXIT_F_KIND_VMX:
|
---|
132 | pszExitName = HMGetVmxExitName( uFlagsAndType & EMEXIT_F_TYPE_MASK);
|
---|
133 | break;
|
---|
134 |
|
---|
135 | case EMEXIT_F_KIND_SVM:
|
---|
136 | pszExitName = HMGetSvmExitName( uFlagsAndType & EMEXIT_F_TYPE_MASK);
|
---|
137 | break;
|
---|
138 |
|
---|
139 | case EMEXIT_F_KIND_NEM:
|
---|
140 | pszExitName = NEMR3GetExitName( uFlagsAndType & EMEXIT_F_TYPE_MASK);
|
---|
141 | break;
|
---|
142 |
|
---|
143 | case EMEXIT_F_KIND_XCPT:
|
---|
144 | switch (uFlagsAndType & EMEXIT_F_TYPE_MASK)
|
---|
145 | {
|
---|
146 | case X86_XCPT_DE: return "Xcpt #DE";
|
---|
147 | case X86_XCPT_DB: return "Xcpt #DB";
|
---|
148 | case X86_XCPT_NMI: return "Xcpt #NMI";
|
---|
149 | case X86_XCPT_BP: return "Xcpt #BP";
|
---|
150 | case X86_XCPT_OF: return "Xcpt #OF";
|
---|
151 | case X86_XCPT_BR: return "Xcpt #BR";
|
---|
152 | case X86_XCPT_UD: return "Xcpt #UD";
|
---|
153 | case X86_XCPT_NM: return "Xcpt #NM";
|
---|
154 | case X86_XCPT_DF: return "Xcpt #DF";
|
---|
155 | case X86_XCPT_CO_SEG_OVERRUN: return "Xcpt #CO_SEG_OVERRUN";
|
---|
156 | case X86_XCPT_TS: return "Xcpt #TS";
|
---|
157 | case X86_XCPT_NP: return "Xcpt #NP";
|
---|
158 | case X86_XCPT_SS: return "Xcpt #SS";
|
---|
159 | case X86_XCPT_GP: return "Xcpt #GP";
|
---|
160 | case X86_XCPT_PF: return "Xcpt #PF";
|
---|
161 | case X86_XCPT_MF: return "Xcpt #MF";
|
---|
162 | case X86_XCPT_AC: return "Xcpt #AC";
|
---|
163 | case X86_XCPT_MC: return "Xcpt #MC";
|
---|
164 | case X86_XCPT_XF: return "Xcpt #XF";
|
---|
165 | case X86_XCPT_VE: return "Xcpt #VE";
|
---|
166 | case X86_XCPT_SX: return "Xcpt #SX";
|
---|
167 | default:
|
---|
168 | pszExitName = NULL;
|
---|
169 | break;
|
---|
170 | }
|
---|
171 | break;
|
---|
172 |
|
---|
173 | default:
|
---|
174 | AssertFailed();
|
---|
175 | pszExitName = NULL;
|
---|
176 | break;
|
---|
177 | }
|
---|
178 | if (pszExitName)
|
---|
179 | return pszExitName;
|
---|
180 | RTStrPrintf(pszFallback, cbFallback, "%#06x", uFlagsAndType & (EMEXIT_F_KIND_MASK | EMEXIT_F_TYPE_MASK));
|
---|
181 | return pszFallback;
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Displays the VM-exit history.
|
---|
187 | *
|
---|
188 | * @param pVM The cross context VM structure.
|
---|
189 | * @param pHlp The info helper functions.
|
---|
190 | * @param pszArgs Arguments, ignored.
|
---|
191 | */
|
---|
192 | static DECLCALLBACK(void) emR3InfoExitHistory(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
|
---|
193 | {
|
---|
194 | NOREF(pszArgs);
|
---|
195 |
|
---|
196 | /*
|
---|
197 | * Figure out target cpu and parse arguments.
|
---|
198 | */
|
---|
199 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
200 | if (!pVCpu)
|
---|
201 | pVCpu = pVM->apCpusR3[0];
|
---|
202 | bool fReverse = true;
|
---|
203 | uint32_t cLeft = RT_ELEMENTS(pVCpu->em.s.aExitHistory);
|
---|
204 |
|
---|
205 | while (pszArgs && *pszArgs)
|
---|
206 | {
|
---|
207 | pszArgs = RTStrStripL(pszArgs);
|
---|
208 | if (!*pszArgs)
|
---|
209 | break;
|
---|
210 | if (RT_C_IS_DIGIT(*pszArgs))
|
---|
211 | {
|
---|
212 | /* The number to dump. */
|
---|
213 | uint32_t uValue = cLeft;
|
---|
214 | RTStrToUInt32Ex(pszArgs, (char **)&pszArgs, 0, &uValue);
|
---|
215 | if (uValue > 0)
|
---|
216 | cLeft = RT_MIN(uValue, RT_ELEMENTS(pVCpu->em.s.aExitHistory));
|
---|
217 | }
|
---|
218 | else if (RTStrCmp(pszArgs, "reverse") == 0)
|
---|
219 | {
|
---|
220 | pszArgs += 7;
|
---|
221 | fReverse = true;
|
---|
222 | }
|
---|
223 | else if (RTStrCmp(pszArgs, "ascending") == 0)
|
---|
224 | {
|
---|
225 | pszArgs += 9;
|
---|
226 | fReverse = false;
|
---|
227 | }
|
---|
228 | else if (RTStrCmp(pszArgs, "asc") == 0)
|
---|
229 | {
|
---|
230 | pszArgs += 3;
|
---|
231 | fReverse = false;
|
---|
232 | }
|
---|
233 | else
|
---|
234 | {
|
---|
235 | const char *pszStart = pszArgs;
|
---|
236 | while (*pszArgs && !RT_C_IS_SPACE(*pszArgs))
|
---|
237 | pszArgs++;
|
---|
238 | pHlp->pfnPrintf(pHlp, "Unknown option: %.*s\n", pszArgs - pszStart, pszArgs);
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | /*
|
---|
243 | * Do the job.
|
---|
244 | */
|
---|
245 | uint64_t idx = pVCpu->em.s.iNextExit;
|
---|
246 | if (idx == 0)
|
---|
247 | pHlp->pfnPrintf(pHlp, "CPU[%u]: VM-exit history: empty\n", pVCpu->idCpu);
|
---|
248 | else
|
---|
249 | {
|
---|
250 | /*
|
---|
251 | * Print header.
|
---|
252 | */
|
---|
253 | pHlp->pfnPrintf(pHlp,
|
---|
254 | "CPU[%u]: VM-exit history:\n"
|
---|
255 | " Exit No.: TSC timestamp / delta RIP (Flat/*) Exit Name\n"
|
---|
256 | , pVCpu->idCpu);
|
---|
257 |
|
---|
258 | /*
|
---|
259 | * Adjust bounds if ascending order.
|
---|
260 | */
|
---|
261 | if (!fReverse)
|
---|
262 | {
|
---|
263 | if (idx > cLeft)
|
---|
264 | idx -= cLeft;
|
---|
265 | else
|
---|
266 | {
|
---|
267 | cLeft = idx;
|
---|
268 | idx = 0;
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | /*
|
---|
273 | * Print the entries.
|
---|
274 | */
|
---|
275 | uint64_t uPrevTimestamp = 0;
|
---|
276 | do
|
---|
277 | {
|
---|
278 | if (fReverse)
|
---|
279 | idx -= 1;
|
---|
280 | PCEMEXITENTRY const pEntry = &pVCpu->em.s.aExitHistory[(uintptr_t)idx & 0xff];
|
---|
281 |
|
---|
282 | /* Get the exit name. */
|
---|
283 | char szExitName[16];
|
---|
284 | const char *pszExitName = emR3HistoryGetExitName(pEntry->uFlagsAndType, szExitName, sizeof(szExitName));
|
---|
285 |
|
---|
286 | /* Calc delta (negative if reverse order, positive ascending). */
|
---|
287 | int64_t offDelta = uPrevTimestamp != 0 && pEntry->uTimestamp != 0 ? pEntry->uTimestamp - uPrevTimestamp : 0;
|
---|
288 | uPrevTimestamp = pEntry->uTimestamp;
|
---|
289 |
|
---|
290 | char szPC[32];
|
---|
291 | if (!(pEntry->uFlagsAndType & (EMEXIT_F_CS_EIP | EMEXIT_F_UNFLATTENED_PC)))
|
---|
292 | RTStrPrintf(szPC, sizeof(szPC), "%016RX64 ", pEntry->uFlatPC);
|
---|
293 | else if (pEntry->uFlagsAndType & EMEXIT_F_UNFLATTENED_PC)
|
---|
294 | RTStrPrintf(szPC, sizeof(szPC), "%016RX64*", pEntry->uFlatPC);
|
---|
295 | else
|
---|
296 | RTStrPrintf(szPC, sizeof(szPC), "%04x:%08RX32* ", (uint32_t)(pEntry->uFlatPC >> 32), (uint32_t)pEntry->uFlatPC);
|
---|
297 |
|
---|
298 | /* Do the printing. */
|
---|
299 | if (pEntry->idxSlot == UINT32_MAX)
|
---|
300 | pHlp->pfnPrintf(pHlp, " %10RU64: %#018RX64/%+-9RI64 %s %#07x %s\n",
|
---|
301 | idx, pEntry->uTimestamp, offDelta, szPC, pEntry->uFlagsAndType, pszExitName);
|
---|
302 | else
|
---|
303 | {
|
---|
304 | /** @todo more on this later */
|
---|
305 | pHlp->pfnPrintf(pHlp, " %10RU64: %#018RX64/%+-9RI64 %s %#07x %s slot=%#x\n",
|
---|
306 | idx, pEntry->uTimestamp, offDelta, szPC, pEntry->uFlagsAndType, pszExitName, pEntry->idxSlot);
|
---|
307 | }
|
---|
308 |
|
---|
309 | /* Advance if ascending. */
|
---|
310 | if (!fReverse)
|
---|
311 | idx += 1;
|
---|
312 | } while (--cLeft > 0 && idx > 0);
|
---|
313 | }
|
---|
314 | }
|
---|
315 |
|
---|
316 |
|
---|
317 | int emR3InitDbg(PVM pVM)
|
---|
318 | {
|
---|
319 | /*
|
---|
320 | * Register info dumpers.
|
---|
321 | */
|
---|
322 | const char *pszExitsDesc = "Dumps the VM-exit history. Arguments: Number of entries; 'asc', 'ascending' or 'reverse'.";
|
---|
323 | int rc = DBGFR3InfoRegisterInternalEx(pVM, "exits", pszExitsDesc, emR3InfoExitHistory, DBGFINFO_FLAGS_ALL_EMTS);
|
---|
324 | AssertLogRelRCReturn(rc, rc);
|
---|
325 | rc = DBGFR3InfoRegisterInternalEx(pVM, "exithistory", pszExitsDesc, emR3InfoExitHistory, DBGFINFO_FLAGS_ALL_EMTS);
|
---|
326 | AssertLogRelRCReturn(rc, rc);
|
---|
327 |
|
---|
328 | #ifdef VBOX_WITH_DEBUGGER
|
---|
329 | /*
|
---|
330 | * Register debugger commands.
|
---|
331 | */
|
---|
332 | rc = DBGCRegisterCommands(&g_aCmds[0], RT_ELEMENTS(g_aCmds));
|
---|
333 | AssertLogRelRCReturn(rc, rc);
|
---|
334 | #endif
|
---|
335 |
|
---|
336 | return VINF_SUCCESS;
|
---|
337 | }
|
---|
338 |
|
---|