VirtualBox

source: vbox/trunk/src/VBox/Debugger/DBGCEmulateCodeView.cpp@ 64577

Last change on this file since 64577 was 64577, checked in by vboxsync, 8 years ago

Debugger/CodeView: Some coloring fixes for the ucfgc command

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 270.0 KB
Line 
1/* $Id: DBGCEmulateCodeView.cpp 64577 2016-11-04 14:12:36Z vboxsync $ */
2/** @file
3 * DBGC - Debugger Console, CodeView / WinDbg Emulation.
4 */
5
6/*
7 * Copyright (C) 2006-2016 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_DBGC
23#include <VBox/dbg.h>
24#include <VBox/vmm/dbgf.h>
25#include <VBox/vmm/pgm.h>
26#include <VBox/vmm/cpum.h>
27#include <VBox/dis.h>
28#include <VBox/param.h>
29#include <VBox/err.h>
30#include <VBox/log.h>
31
32#include <iprt/asm.h>
33#include <iprt/mem.h>
34#include <iprt/string.h>
35#include <iprt/assert.h>
36#include <iprt/ctype.h>
37
38#include <stdlib.h>
39#include <stdio.h>
40
41#include "DBGCInternal.h"
42
43
44/*********************************************************************************************************************************
45* Internal Functions *
46*********************************************************************************************************************************/
47static FNDBGCCMD dbgcCmdBrkAccess;
48static FNDBGCCMD dbgcCmdBrkClear;
49static FNDBGCCMD dbgcCmdBrkDisable;
50static FNDBGCCMD dbgcCmdBrkEnable;
51static FNDBGCCMD dbgcCmdBrkList;
52static FNDBGCCMD dbgcCmdBrkSet;
53static FNDBGCCMD dbgcCmdBrkREM;
54static FNDBGCCMD dbgcCmdDumpMem;
55static FNDBGCCMD dbgcCmdDumpDT;
56static FNDBGCCMD dbgcCmdDumpIDT;
57static FNDBGCCMD dbgcCmdDumpPageDir;
58static FNDBGCCMD dbgcCmdDumpPageDirBoth;
59static FNDBGCCMD dbgcCmdDumpPageHierarchy;
60static FNDBGCCMD dbgcCmdDumpPageTable;
61static FNDBGCCMD dbgcCmdDumpPageTableBoth;
62static FNDBGCCMD dbgcCmdDumpTSS;
63static FNDBGCCMD dbgcCmdDumpTypeInfo;
64static FNDBGCCMD dbgcCmdDumpTypedVal;
65static FNDBGCCMD dbgcCmdEditMem;
66static FNDBGCCMD dbgcCmdGo;
67static FNDBGCCMD dbgcCmdListModules;
68static FNDBGCCMD dbgcCmdListNear;
69static FNDBGCCMD dbgcCmdListSource;
70static FNDBGCCMD dbgcCmdMemoryInfo;
71static FNDBGCCMD dbgcCmdReg;
72static FNDBGCCMD dbgcCmdRegGuest;
73static FNDBGCCMD dbgcCmdRegHyper;
74static FNDBGCCMD dbgcCmdRegTerse;
75static FNDBGCCMD dbgcCmdSearchMem;
76static FNDBGCCMD dbgcCmdSearchMemType;
77static FNDBGCCMD dbgcCmdEventCtrl;
78static FNDBGCCMD dbgcCmdEventCtrlList;
79static FNDBGCCMD dbgcCmdEventCtrlReset;
80static FNDBGCCMD dbgcCmdStack;
81static FNDBGCCMD dbgcCmdTrace;
82static FNDBGCCMD dbgcCmdUnassemble;
83static FNDBGCCMD dbgcCmdUnassembleCfg;
84
85
86/*********************************************************************************************************************************
87* Global Variables *
88*********************************************************************************************************************************/
89/** 'ba' arguments. */
90static const DBGCVARDESC g_aArgBrkAcc[] =
91{
92 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
93 { 1, 1, DBGCVAR_CAT_STRING, 0, "access", "The access type: x=execute, rw=read/write (alias r), w=write, i=not implemented." },
94 { 1, 1, DBGCVAR_CAT_NUMBER, 0, "size", "The access size: 1, 2, 4, or 8. 'x' access requires 1, and 8 requires amd64 long mode." },
95 { 1, 1, DBGCVAR_CAT_GC_POINTER, 0, "address", "The address." },
96 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "passes", "The number of passes before we trigger the breakpoint. (0 is default)" },
97 { 0, 1, DBGCVAR_CAT_NUMBER, DBGCVD_FLAGS_DEP_PREV, "max passes", "The number of passes after which we stop triggering the breakpoint. (~0 is default)" },
98 { 0, 1, DBGCVAR_CAT_STRING, 0, "cmds", "String of commands to be executed when the breakpoint is hit. Quote it!" },
99};
100
101
102/** 'bc', 'bd', 'be' arguments. */
103static const DBGCVARDESC g_aArgBrks[] =
104{
105 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
106 { 0, ~0U, DBGCVAR_CAT_NUMBER, 0, "#bp", "Breakpoint number." },
107 { 0, 1, DBGCVAR_CAT_STRING, 0, "all", "All breakpoints." },
108};
109
110
111/** 'bp' arguments. */
112static const DBGCVARDESC g_aArgBrkSet[] =
113{
114 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
115 { 1, 1, DBGCVAR_CAT_GC_POINTER, 0, "address", "The address." },
116 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "passes", "The number of passes before we trigger the breakpoint. (0 is default)" },
117 { 0, 1, DBGCVAR_CAT_NUMBER, DBGCVD_FLAGS_DEP_PREV, "max passes", "The number of passes after which we stop triggering the breakpoint. (~0 is default)" },
118 { 0, 1, DBGCVAR_CAT_STRING, 0, "cmds", "String of commands to be executed when the breakpoint is hit. Quote it!" },
119};
120
121
122/** 'br' arguments. */
123static const DBGCVARDESC g_aArgBrkREM[] =
124{
125 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
126 { 1, 1, DBGCVAR_CAT_GC_POINTER, 0, "address", "The address." },
127 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "passes", "The number of passes before we trigger the breakpoint. (0 is default)" },
128 { 0, 1, DBGCVAR_CAT_NUMBER, DBGCVD_FLAGS_DEP_PREV, "max passes", "The number of passes after which we stop triggering the breakpoint. (~0 is default)" },
129 { 0, 1, DBGCVAR_CAT_STRING, 0, "cmds", "String of commands to be executed when the breakpoint is hit. Quote it!" },
130};
131
132
133/** 'd?' arguments. */
134static const DBGCVARDESC g_aArgDumpMem[] =
135{
136 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
137 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address where to start dumping memory." },
138};
139
140
141/** 'dg', 'dga', 'dl', 'dla' arguments. */
142static const DBGCVARDESC g_aArgDumpDT[] =
143{
144 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
145 { 0, ~0U, DBGCVAR_CAT_NUMBER, 0, "sel", "Selector or selector range." },
146 { 0, ~0U, DBGCVAR_CAT_POINTER, 0, "address", "Far address which selector should be dumped." },
147};
148
149
150/** 'di', 'dia' arguments. */
151static const DBGCVARDESC g_aArgDumpIDT[] =
152{
153 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
154 { 0, ~0U, DBGCVAR_CAT_NUMBER, 0, "int", "The interrupt vector or interrupt vector range." },
155};
156
157
158/** 'dpd*' arguments. */
159static const DBGCVARDESC g_aArgDumpPD[] =
160{
161 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
162 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "index", "Index into the page directory." },
163 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address which page directory entry to start dumping from. Range is applied to the page directory." },
164};
165
166
167/** 'dpda' arguments. */
168static const DBGCVARDESC g_aArgDumpPDAddr[] =
169{
170 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
171 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address of the page directory entry to start dumping from." },
172};
173
174
175/** 'dph*' arguments. */
176static const DBGCVARDESC g_aArgDumpPH[] =
177{
178 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
179 { 0, 1, DBGCVAR_CAT_GC_POINTER, 0, "address", "Where in the address space to start dumping and for how long (range). The default address/range will be used if omitted." },
180 { 0, 1, DBGCVAR_CAT_NUMBER, DBGCVD_FLAGS_DEP_PREV, "cr3", "The CR3 value to use. The current CR3 of the context will be used if omitted." },
181 { 0, 1, DBGCVAR_CAT_STRING, DBGCVD_FLAGS_DEP_PREV, "mode", "The paging mode: legacy, pse, pae, long, ept. Append '-np' for nested paging and '-nx' for no-execute. The current mode will be used if omitted." },
182};
183
184
185/** 'dpt?' arguments. */
186static const DBGCVARDESC g_aArgDumpPT[] =
187{
188 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
189 { 1, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address which page directory entry to start dumping from." },
190};
191
192
193/** 'dpta' arguments. */
194static const DBGCVARDESC g_aArgDumpPTAddr[] =
195{
196 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
197 { 1, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address of the page table entry to start dumping from." },
198};
199
200
201/** 'dt' arguments. */
202static const DBGCVARDESC g_aArgDumpTSS[] =
203{
204 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
205 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "tss", "TSS selector number." },
206 { 0, 1, DBGCVAR_CAT_POINTER, 0, "tss:ign|addr", "TSS address. If the selector is a TSS selector, the offset will be ignored." }
207};
208
209
210/** 'dti' arguments. */
211static const DBGCVARDESC g_aArgDumpTypeInfo[] =
212{
213 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
214 { 1, 1, DBGCVAR_CAT_STRING, 0, "type", "The type to dump" },
215 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "levels", "How many levels to dump the type information" }
216};
217
218
219/** 'dtv' arguments. */
220static const DBGCVARDESC g_aArgDumpTypedVal[] =
221{
222 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
223 { 1, 1, DBGCVAR_CAT_STRING, 0, "type", "The type to use" },
224 { 1, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address to start dumping from." },
225 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "levels", "How many levels to dump" }
226};
227
228
229/** 'e?' arguments. */
230static const DBGCVARDESC g_aArgEditMem[] =
231{
232 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
233 { 1, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address where to write." },
234 { 1, ~0U, DBGCVAR_CAT_NUMBER, 0, "value", "Value to write." },
235};
236
237
238/** 'lm' arguments. */
239static const DBGCVARDESC g_aArgListMods[] =
240{
241 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
242 { 0, ~0U, DBGCVAR_CAT_STRING, 0, "module", "Module name." },
243};
244
245
246/** 'ln' arguments. */
247static const DBGCVARDESC g_aArgListNear[] =
248{
249 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
250 { 0, ~0U, DBGCVAR_CAT_POINTER, 0, "address", "Address of the symbol to look up." },
251 { 0, ~0U, DBGCVAR_CAT_SYMBOL, 0, "symbol", "Symbol to lookup." },
252};
253
254
255/** 'ls' arguments. */
256static const DBGCVARDESC g_aArgListSource[] =
257{
258 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
259 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address where to start looking for source lines." },
260};
261
262
263/** 'm' argument. */
264static const DBGCVARDESC g_aArgMemoryInfo[] =
265{
266 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
267 { 1, 1, DBGCVAR_CAT_POINTER, 0, "address", "Pointer to obtain info about." },
268};
269
270
271/** 'r' arguments. */
272static const DBGCVARDESC g_aArgReg[] =
273{
274 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
275 { 0, 1, DBGCVAR_CAT_SYMBOL, 0, "register", "Register to show or set." },
276 { 0, 1, DBGCVAR_CAT_STRING, DBGCVD_FLAGS_DEP_PREV, "=", "Equal sign." },
277 { 0, 1, DBGCVAR_CAT_NUMBER, DBGCVD_FLAGS_DEP_PREV, "value", "New register value." },
278};
279
280
281/** 's' arguments. */
282static const DBGCVARDESC g_aArgSearchMem[] =
283{
284 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
285 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-b", "Byte string." },
286 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-w", "Word string." },
287 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-d", "DWord string." },
288 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-q", "QWord string." },
289 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-a", "ASCII string." },
290 { 0, 1, DBGCVAR_CAT_OPTION, 0, "-u", "Unicode string." },
291 { 0, 1, DBGCVAR_CAT_OPTION_NUMBER, 0, "-n <Hits>", "Maximum number of hits." },
292 { 0, 1, DBGCVAR_CAT_GC_POINTER, 0, "range", "Register to show or set." },
293 { 0, ~0U, DBGCVAR_CAT_ANY, 0, "pattern", "Pattern to search for." },
294};
295
296
297/** 's?' arguments. */
298static const DBGCVARDESC g_aArgSearchMemType[] =
299{
300 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
301 { 1, 1, DBGCVAR_CAT_GC_POINTER, 0, "range", "Register to show or set." },
302 { 1, ~0U, DBGCVAR_CAT_ANY, 0, "pattern", "Pattern to search for." },
303};
304
305
306/** 'sxe', 'sxn', 'sxi', 'sx-' arguments. */
307static const DBGCVARDESC g_aArgEventCtrl[] =
308{
309 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
310 { 0, 1, DBGCVAR_CAT_STRING, 0, "-c", "The -c option, requires <cmds>." },
311 { 0, 1, DBGCVAR_CAT_STRING, DBGCVD_FLAGS_DEP_PREV, "cmds", "Command to execute on this event." },
312 { 0 /*weird*/, ~0U, DBGCVAR_CAT_STRING, 0, "event", "One or more events, 'all' refering to all events." },
313};
314
315/** 'sx' and 'sr' arguments. */
316static const DBGCVARDESC g_aArgEventCtrlOpt[] =
317{
318 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
319 { 0, ~0U, DBGCVAR_CAT_STRING, 0, "event", "Zero or more events, 'all' refering to all events and being the default." },
320};
321
322/** 'u' arguments. */
323static const DBGCVARDESC g_aArgUnassemble[] =
324{
325 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
326 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address where to start disassembling." },
327};
328
329/** 'ucfg' arguments. */
330static const DBGCVARDESC g_aArgUnassembleCfg[] =
331{
332 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
333 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address where to start disassembling." },
334};
335
336
337/** Command descriptors for the CodeView / WinDbg emulation.
338 * The emulation isn't attempting to be identical, only somewhat similar.
339 */
340const DBGCCMD g_aCmdsCodeView[] =
341{
342 /* pszCmd, cArgsMin, cArgsMax, paArgDescs, cArgDescs, fFlags, pfnHandler pszSyntax, ....pszDescription */
343 { "ba", 3, 6, &g_aArgBrkAcc[0], RT_ELEMENTS(g_aArgBrkAcc), 0, dbgcCmdBrkAccess, "<access> <size> <address> [passes [max passes]] [cmds]",
344 "Sets a data access breakpoint." },
345 { "bc", 1, ~0U, &g_aArgBrks[0], RT_ELEMENTS(g_aArgBrks), 0, dbgcCmdBrkClear, "all | <bp#> [bp# []]", "Deletes a set of breakpoints." },
346 { "bd", 1, ~0U, &g_aArgBrks[0], RT_ELEMENTS(g_aArgBrks), 0, dbgcCmdBrkDisable, "all | <bp#> [bp# []]", "Disables a set of breakpoints." },
347 { "be", 1, ~0U, &g_aArgBrks[0], RT_ELEMENTS(g_aArgBrks), 0, dbgcCmdBrkEnable, "all | <bp#> [bp# []]", "Enables a set of breakpoints." },
348 { "bl", 0, 0, NULL, 0, 0, dbgcCmdBrkList, "", "Lists all the breakpoints." },
349 { "bp", 1, 4, &g_aArgBrkSet[0], RT_ELEMENTS(g_aArgBrkSet), 0, dbgcCmdBrkSet, "<address> [passes [max passes]] [cmds]",
350 "Sets a breakpoint (int 3)." },
351 { "br", 1, 4, &g_aArgBrkREM[0], RT_ELEMENTS(g_aArgBrkREM), 0, dbgcCmdBrkREM, "<address> [passes [max passes]] [cmds]",
352 "Sets a recompiler specific breakpoint." },
353 { "d", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory using last element size." },
354 { "da", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory as ascii string." },
355 { "db", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory in bytes." },
356 { "dd", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory in double words." },
357 { "da", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory as ascii string." },
358 { "dg", 0, ~0U, &g_aArgDumpDT[0], RT_ELEMENTS(g_aArgDumpDT), 0, dbgcCmdDumpDT, "[sel [..]]", "Dump the global descriptor table (GDT)." },
359 { "dga", 0, ~0U, &g_aArgDumpDT[0], RT_ELEMENTS(g_aArgDumpDT), 0, dbgcCmdDumpDT, "[sel [..]]", "Dump the global descriptor table (GDT) including not-present entries." },
360 { "di", 0, ~0U, &g_aArgDumpIDT[0], RT_ELEMENTS(g_aArgDumpIDT), 0, dbgcCmdDumpIDT, "[int [..]]", "Dump the interrupt descriptor table (IDT)." },
361 { "dia", 0, ~0U, &g_aArgDumpIDT[0], RT_ELEMENTS(g_aArgDumpIDT), 0, dbgcCmdDumpIDT, "[int [..]]", "Dump the interrupt descriptor table (IDT) including not-present entries." },
362 { "dl", 0, ~0U, &g_aArgDumpDT[0], RT_ELEMENTS(g_aArgDumpDT), 0, dbgcCmdDumpDT, "[sel [..]]", "Dump the local descriptor table (LDT)." },
363 { "dla", 0, ~0U, &g_aArgDumpDT[0], RT_ELEMENTS(g_aArgDumpDT), 0, dbgcCmdDumpDT, "[sel [..]]", "Dump the local descriptor table (LDT) including not-present entries." },
364 { "dpd", 0, 1, &g_aArgDumpPD[0], RT_ELEMENTS(g_aArgDumpPD), 0, dbgcCmdDumpPageDir, "[addr|index]", "Dumps page directory entries of the default context." },
365 { "dpda", 0, 1, &g_aArgDumpPDAddr[0],RT_ELEMENTS(g_aArgDumpPDAddr), 0, dbgcCmdDumpPageDir, "[addr]", "Dumps memory at given address as a page directory." },
366 { "dpdb", 0, 1, &g_aArgDumpPD[0], RT_ELEMENTS(g_aArgDumpPD), 0, dbgcCmdDumpPageDirBoth, "[addr|index]", "Dumps page directory entries of the guest and the hypervisor. " },
367 { "dpdg", 0, 1, &g_aArgDumpPD[0], RT_ELEMENTS(g_aArgDumpPD), 0, dbgcCmdDumpPageDir, "[addr|index]", "Dumps page directory entries of the guest." },
368 { "dpdh", 0, 1, &g_aArgDumpPD[0], RT_ELEMENTS(g_aArgDumpPD), 0, dbgcCmdDumpPageDir, "[addr|index]", "Dumps page directory entries of the hypervisor. " },
369 { "dph", 0, 3, &g_aArgDumpPH[0], RT_ELEMENTS(g_aArgDumpPH), 0, dbgcCmdDumpPageHierarchy, "[addr [cr3 [mode]]", "Dumps the paging hierarchy at for specfied address range. Default context." },
370 { "dphg", 0, 3, &g_aArgDumpPH[0], RT_ELEMENTS(g_aArgDumpPH), 0, dbgcCmdDumpPageHierarchy, "[addr [cr3 [mode]]", "Dumps the paging hierarchy at for specfied address range. Guest context." },
371 { "dphh", 0, 3, &g_aArgDumpPH[0], RT_ELEMENTS(g_aArgDumpPH), 0, dbgcCmdDumpPageHierarchy, "[addr [cr3 [mode]]", "Dumps the paging hierarchy at for specfied address range. Hypervisor context." },
372 { "dpt", 1, 1, &g_aArgDumpPT[0], RT_ELEMENTS(g_aArgDumpPT), 0, dbgcCmdDumpPageTable,"<addr>", "Dumps page table entries of the default context." },
373 { "dpta", 1, 1, &g_aArgDumpPTAddr[0],RT_ELEMENTS(g_aArgDumpPTAddr), 0, dbgcCmdDumpPageTable,"<addr>", "Dumps memory at given address as a page table." },
374 { "dptb", 1, 1, &g_aArgDumpPT[0], RT_ELEMENTS(g_aArgDumpPT), 0, dbgcCmdDumpPageTableBoth,"<addr>", "Dumps page table entries of the guest and the hypervisor." },
375 { "dptg", 1, 1, &g_aArgDumpPT[0], RT_ELEMENTS(g_aArgDumpPT), 0, dbgcCmdDumpPageTable,"<addr>", "Dumps page table entries of the guest." },
376 { "dpth", 1, 1, &g_aArgDumpPT[0], RT_ELEMENTS(g_aArgDumpPT), 0, dbgcCmdDumpPageTable,"<addr>", "Dumps page table entries of the hypervisor." },
377 { "dq", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory in quad words." },
378 { "dt", 0, 1, &g_aArgDumpTSS[0], RT_ELEMENTS(g_aArgDumpTSS), 0, dbgcCmdDumpTSS, "[tss|tss:ign|addr]", "Dump the task state segment (TSS)." },
379 { "dt16", 0, 1, &g_aArgDumpTSS[0], RT_ELEMENTS(g_aArgDumpTSS), 0, dbgcCmdDumpTSS, "[tss|tss:ign|addr]", "Dump the 16-bit task state segment (TSS)." },
380 { "dt32", 0, 1, &g_aArgDumpTSS[0], RT_ELEMENTS(g_aArgDumpTSS), 0, dbgcCmdDumpTSS, "[tss|tss:ign|addr]", "Dump the 32-bit task state segment (TSS)." },
381 { "dt64", 0, 1, &g_aArgDumpTSS[0], RT_ELEMENTS(g_aArgDumpTSS), 0, dbgcCmdDumpTSS, "[tss|tss:ign|addr]", "Dump the 64-bit task state segment (TSS)." },
382 { "dti", 1, 2, &g_aArgDumpTypeInfo[0],RT_ELEMENTS(g_aArgDumpTypeInfo), 0, dbgcCmdDumpTypeInfo,"<type> [levels]", "Dump type information." },
383 { "dtv", 2, 3, &g_aArgDumpTypedVal[0],RT_ELEMENTS(g_aArgDumpTypedVal), 0, dbgcCmdDumpTypedVal,"<type> <addr> [levels]", "Dump a memory buffer using the information in the given type." },
384 { "dw", 0, 1, &g_aArgDumpMem[0], RT_ELEMENTS(g_aArgDumpMem), 0, dbgcCmdDumpMem, "[addr]", "Dump memory in words." },
385 /** @todo add 'e', 'ea str', 'eza str', 'eu str' and 'ezu str'. See also
386 * dbgcCmdSearchMem and its dbgcVarsToBytes usage. */
387 { "eb", 2, 2, &g_aArgEditMem[0], RT_ELEMENTS(g_aArgEditMem), 0, dbgcCmdEditMem, "<addr> <value>", "Write a 1-byte value to memory." },
388 { "ew", 2, 2, &g_aArgEditMem[0], RT_ELEMENTS(g_aArgEditMem), 0, dbgcCmdEditMem, "<addr> <value>", "Write a 2-byte value to memory." },
389 { "ed", 2, 2, &g_aArgEditMem[0], RT_ELEMENTS(g_aArgEditMem), 0, dbgcCmdEditMem, "<addr> <value>", "Write a 4-byte value to memory." },
390 { "eq", 2, 2, &g_aArgEditMem[0], RT_ELEMENTS(g_aArgEditMem), 0, dbgcCmdEditMem, "<addr> <value>", "Write a 8-byte value to memory." },
391 { "g", 0, 0, NULL, 0, 0, dbgcCmdGo, "", "Continue execution." },
392 { "k", 0, 0, NULL, 0, 0, dbgcCmdStack, "", "Callstack." },
393 { "kg", 0, 0, NULL, 0, 0, dbgcCmdStack, "", "Callstack - guest." },
394 { "kh", 0, 0, NULL, 0, 0, dbgcCmdStack, "", "Callstack - hypervisor." },
395 { "lm", 0, ~0U, &g_aArgListMods[0], RT_ELEMENTS(g_aArgListMods), 0, dbgcCmdListModules, "[module [..]]", "List modules." },
396 { "lmv", 0, ~0U, &g_aArgListMods[0], RT_ELEMENTS(g_aArgListMods), 0, dbgcCmdListModules, "[module [..]]", "List modules, verbose." },
397 { "lmo", 0, ~0U, &g_aArgListMods[0], RT_ELEMENTS(g_aArgListMods), 0, dbgcCmdListModules, "[module [..]]", "List modules and their segments." },
398 { "lmov", 0, ~0U, &g_aArgListMods[0], RT_ELEMENTS(g_aArgListMods), 0, dbgcCmdListModules, "[module [..]]", "List modules and their segments, verbose." },
399 { "ln", 0, ~0U, &g_aArgListNear[0], RT_ELEMENTS(g_aArgListNear), 0, dbgcCmdListNear, "[addr/sym [..]]", "List symbols near to the address. Default address is CS:EIP." },
400 { "ls", 0, 1, &g_aArgListSource[0],RT_ELEMENTS(g_aArgListSource), 0, dbgcCmdListSource, "[addr]", "Source." },
401 { "m", 1, 1, &g_aArgMemoryInfo[0],RT_ELEMENTS(g_aArgMemoryInfo), 0, dbgcCmdMemoryInfo, "<addr>", "Display information about that piece of memory." },
402 { "r", 0, 3, &g_aArgReg[0], RT_ELEMENTS(g_aArgReg), 0, dbgcCmdReg, "[reg [[=] newval]]", "Show or set register(s) - active reg set." },
403 { "rg", 0, 3, &g_aArgReg[0], RT_ELEMENTS(g_aArgReg), 0, dbgcCmdRegGuest, "[reg [[=] newval]]", "Show or set register(s) - guest reg set." },
404 { "rg32", 0, 0, NULL, 0, 0, dbgcCmdRegGuest, "", "Show 32-bit guest registers." },
405 { "rg64", 0, 0, NULL, 0, 0, dbgcCmdRegGuest, "", "Show 64-bit guest registers." },
406 { "rh", 0, 3, &g_aArgReg[0], RT_ELEMENTS(g_aArgReg), 0, dbgcCmdRegHyper, "[reg [[=] newval]]", "Show or set register(s) - hypervisor reg set." },
407 { "rt", 0, 0, NULL, 0, 0, dbgcCmdRegTerse, "", "Toggles terse / verbose register info." },
408 { "s", 0, ~0U, &g_aArgSearchMem[0], RT_ELEMENTS(g_aArgSearchMem), 0, dbgcCmdSearchMem, "[options] <range> <pattern>", "Continue last search." },
409 { "sa", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for an ascii string." },
410 { "sb", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for one or more bytes." },
411 { "sd", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for one or more double words." },
412 { "sq", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for one or more quad words." },
413 { "su", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for an unicode string." },
414 { "sw", 2, ~0U, &g_aArgSearchMemType[0], RT_ELEMENTS(g_aArgSearchMemType),0, dbgcCmdSearchMemType, "<range> <pattern>", "Search memory for one or more words." },
415 { "sx", 0, ~0U, &g_aArgEventCtrlOpt[0], RT_ELEMENTS(g_aArgEventCtrlOpt), 0, dbgcCmdEventCtrlList, "[<event> [..]]", "Lists settings for exceptions, exits and other events. All if no filter is specified." },
416 { "sx-", 3, ~0U, &g_aArgEventCtrl[0], RT_ELEMENTS(g_aArgEventCtrl), 0, dbgcCmdEventCtrl, "-c <cmd> <event> [..]", "Modifies the command for one or more exceptions, exits or other event. 'all' addresses all." },
417 { "sxe", 1, ~0U, &g_aArgEventCtrl[0], RT_ELEMENTS(g_aArgEventCtrl), 0, dbgcCmdEventCtrl, "[-c <cmd>] <event> [..]", "Enable: Break into the debugger on the specified exceptions, exits and other events. 'all' addresses all." },
418 { "sxn", 1, ~0U, &g_aArgEventCtrl[0], RT_ELEMENTS(g_aArgEventCtrl), 0, dbgcCmdEventCtrl, "[-c <cmd>] <event> [..]", "Notify: Display info in the debugger and continue on the specified exceptions, exits and other events. 'all' addresses all." },
419 { "sxi", 1, ~0U, &g_aArgEventCtrl[0], RT_ELEMENTS(g_aArgEventCtrl), 0, dbgcCmdEventCtrl, "[-c <cmd>] <event> [..]", "Ignore: Ignore the specified exceptions, exits and other events ('all' = all of them). Without the -c option, the guest runs like normal." },
420 { "sxr", 0, 0, &g_aArgEventCtrlOpt[0], RT_ELEMENTS(g_aArgEventCtrlOpt), 0, dbgcCmdEventCtrlReset, "", "Reset the settings to default for exceptions, exits and other events. All if no filter is specified." },
421 { "t", 0, 0, NULL, 0, 0, dbgcCmdTrace, "", "Instruction trace (step into)." },
422 { "u", 0, 1, &g_aArgUnassemble[0],RT_ELEMENTS(g_aArgUnassemble), 0, dbgcCmdUnassemble, "[addr]", "Unassemble." },
423 { "u64", 0, 1, &g_aArgUnassemble[0],RT_ELEMENTS(g_aArgUnassemble), 0, dbgcCmdUnassemble, "[addr]", "Unassemble 64-bit code." },
424 { "u32", 0, 1, &g_aArgUnassemble[0],RT_ELEMENTS(g_aArgUnassemble), 0, dbgcCmdUnassemble, "[addr]", "Unassemble 32-bit code." },
425 { "u16", 0, 1, &g_aArgUnassemble[0],RT_ELEMENTS(g_aArgUnassemble), 0, dbgcCmdUnassemble, "[addr]", "Unassemble 16-bit code." },
426 { "uv86", 0, 1, &g_aArgUnassemble[0],RT_ELEMENTS(g_aArgUnassemble), 0, dbgcCmdUnassemble, "[addr]", "Unassemble 16-bit code with v8086/real mode addressing." },
427 { "ucfg", 0, 1, &g_aArgUnassembleCfg[0], RT_ELEMENTS(g_aArgUnassembleCfg), 0, dbgcCmdUnassembleCfg, "[addr]", "Unassemble creating a control flow graph." },
428 { "ucfgc", 0, 1, &g_aArgUnassembleCfg[0], RT_ELEMENTS(g_aArgUnassembleCfg), 0, dbgcCmdUnassembleCfg, "[addr]", "Unassemble creating a control flow graph with colors." },
429};
430
431/** The number of commands in the CodeView/WinDbg emulation. */
432const uint32_t g_cCmdsCodeView = RT_ELEMENTS(g_aCmdsCodeView);
433
434
435/**
436 * Selectable debug event descriptors.
437 *
438 * @remarks Sorted by DBGCSXEVT::enmType value.
439 */
440const DBGCSXEVT g_aDbgcSxEvents[] =
441{
442 { DBGFEVENT_INTERRUPT_HARDWARE, "hwint", NULL, kDbgcSxEventKind_Interrupt, kDbgcEvtState_Disabled, 0, "Hardware interrupt" },
443 { DBGFEVENT_INTERRUPT_SOFTWARE, "swint", NULL, kDbgcSxEventKind_Interrupt, kDbgcEvtState_Disabled, 0, "Software interrupt" },
444 { DBGFEVENT_TRIPLE_FAULT, "triplefault", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Enabled, 0, "Triple fault "},
445 { DBGFEVENT_XCPT_DE, "xcpt_de", "de", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#DE (integer divide error)" },
446 { DBGFEVENT_XCPT_DB, "xcpt_db", "db", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#DB (debug)" },
447 { DBGFEVENT_XCPT_02, "xcpt_02", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
448 { DBGFEVENT_XCPT_BP, "xcpt_bp", "bp", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#BP (breakpoint)" },
449 { DBGFEVENT_XCPT_OF, "xcpt_of", "of", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#OF (overflow (INTO))" },
450 { DBGFEVENT_XCPT_BR, "xcpt_br", "br", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#BR (bound range exceeded)" },
451 { DBGFEVENT_XCPT_UD, "xcpt_ud", "ud", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#UD (undefined opcode)" },
452 { DBGFEVENT_XCPT_NM, "xcpt_nm", "nm", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#NM (FPU not available)" },
453 { DBGFEVENT_XCPT_DF, "xcpt_df", "df", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#DF (double fault)" },
454 { DBGFEVENT_XCPT_09, "xcpt_09", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "Coprocessor segment overrun" },
455 { DBGFEVENT_XCPT_TS, "xcpt_ts", "ts", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, "#TS (task switch)" },
456 { DBGFEVENT_XCPT_NP, "xcpt_np", "np", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, "#NP (segment not present)" },
457 { DBGFEVENT_XCPT_SS, "xcpt_ss", "ss", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, "#SS (stack segment fault)" },
458 { DBGFEVENT_XCPT_GP, "xcpt_gp", "gp", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, "#GP (general protection fault)" },
459 { DBGFEVENT_XCPT_PF, "xcpt_pf", "pf", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, "#PF (page fault)" },
460 { DBGFEVENT_XCPT_0f, "xcpt_0f", "xcpt0f", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
461 { DBGFEVENT_XCPT_MF, "xcpt_mf", "mf", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#MF (math fault)" },
462 { DBGFEVENT_XCPT_AC, "xcpt_ac", "ac", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#AC (alignment check)" },
463 { DBGFEVENT_XCPT_MC, "xcpt_mc", "mc", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#MC (machine check)" },
464 { DBGFEVENT_XCPT_XF, "xcpt_xf", "xf", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#XF (SIMD floating-point exception)" },
465 { DBGFEVENT_XCPT_VE, "xcpt_vd", "ve", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, "#VE (virtualization exception)" },
466 { DBGFEVENT_XCPT_15, "xcpt_15", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
467 { DBGFEVENT_XCPT_16, "xcpt_16", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
468 { DBGFEVENT_XCPT_17, "xcpt_17", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
469 { DBGFEVENT_XCPT_18, "xcpt_18", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
470 { DBGFEVENT_XCPT_19, "xcpt_19", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
471 { DBGFEVENT_XCPT_1a, "xcpt_1a", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
472 { DBGFEVENT_XCPT_1b, "xcpt_1b", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
473 { DBGFEVENT_XCPT_1c, "xcpt_1c", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
474 { DBGFEVENT_XCPT_1d, "xcpt_1d", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
475 { DBGFEVENT_XCPT_SX, "xcpt_sx", "sx", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, "#SX (security exception)" },
476 { DBGFEVENT_XCPT_1f, "xcpt_1f", "xcpt1f", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
477 { DBGFEVENT_INSTR_HALT, "instr_halt", "hlt", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
478 { DBGFEVENT_INSTR_MWAIT, "instr_mwait", "mwait", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
479 { DBGFEVENT_INSTR_MONITOR, "instr_monitor", "monitor", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
480 { DBGFEVENT_INSTR_CPUID, "instr_cpuid", "cpuid", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
481 { DBGFEVENT_INSTR_INVD, "instr_invd", "invd", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
482 { DBGFEVENT_INSTR_WBINVD, "instr_wbinvd", "wbinvd", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
483 { DBGFEVENT_INSTR_INVLPG, "instr_invlpg", "invlpg", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
484 { DBGFEVENT_INSTR_RDTSC, "instr_rdtsc", "rdtsc", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
485 { DBGFEVENT_INSTR_RDTSCP, "instr_rdtscp", "rdtscp", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
486 { DBGFEVENT_INSTR_RDPMC, "instr_rdpmc", "rdpmc", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
487 { DBGFEVENT_INSTR_RDMSR, "instr_rdmsr", "rdmsr", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
488 { DBGFEVENT_INSTR_WRMSR, "instr_wrmsr", "wrmsr", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
489 { DBGFEVENT_INSTR_CRX_READ, "instr_crx_read", "crx_read", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, NULL },
490 { DBGFEVENT_INSTR_CRX_WRITE, "instr_crx_write", "crx_write",kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, NULL },
491 { DBGFEVENT_INSTR_DRX_READ, "instr_drx_read", "drx_read", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, NULL },
492 { DBGFEVENT_INSTR_DRX_WRITE, "instr_drx_write", "drx_write",kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, DBGCSXEVT_F_TAKE_ARG, NULL },
493 { DBGFEVENT_INSTR_PAUSE, "instr_pause", "pause", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
494 { DBGFEVENT_INSTR_XSETBV, "instr_xsetbv", "xsetbv", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
495 { DBGFEVENT_INSTR_SIDT, "instr_sidt", "sidt", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
496 { DBGFEVENT_INSTR_LIDT, "instr_lidt", "lidt", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
497 { DBGFEVENT_INSTR_SGDT, "instr_sgdt", "sgdt", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
498 { DBGFEVENT_INSTR_LGDT, "instr_lgdt", "lgdt", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
499 { DBGFEVENT_INSTR_SLDT, "instr_sldt", "sldt", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
500 { DBGFEVENT_INSTR_LLDT, "instr_lldt", "lldt", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
501 { DBGFEVENT_INSTR_STR, "instr_str", "str", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
502 { DBGFEVENT_INSTR_LTR, "instr_ltr", "ltr", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
503 { DBGFEVENT_INSTR_GETSEC, "instr_getsec", "getsec", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
504 { DBGFEVENT_INSTR_RSM, "instr_rsm", "rsm", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
505 { DBGFEVENT_INSTR_RDRAND, "instr_rdrand", "rdrand", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
506 { DBGFEVENT_INSTR_RDSEED, "instr_rdseed", "rdseed", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
507 { DBGFEVENT_INSTR_XSAVES, "instr_xsaves", "xsaves", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
508 { DBGFEVENT_INSTR_XRSTORS, "instr_xrstors", "xrstors", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
509 { DBGFEVENT_INSTR_VMM_CALL, "instr_vmm_call", "vmm_call", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
510 { DBGFEVENT_INSTR_VMX_VMCLEAR, "instr_vmx_vmclear", "vmclear", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
511 { DBGFEVENT_INSTR_VMX_VMLAUNCH, "instr_vmx_vmlaunch", "vmlaunch", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
512 { DBGFEVENT_INSTR_VMX_VMPTRLD, "instr_vmx_vmptrld", "vmptrld", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
513 { DBGFEVENT_INSTR_VMX_VMPTRST, "instr_vmx_vmptrst", "vmptrst", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
514 { DBGFEVENT_INSTR_VMX_VMREAD, "instr_vmx_vmread", "vmread", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
515 { DBGFEVENT_INSTR_VMX_VMRESUME, "instr_vmx_vmresume", "vmresume", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
516 { DBGFEVENT_INSTR_VMX_VMWRITE, "instr_vmx_vmwrite", "vmwrite", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
517 { DBGFEVENT_INSTR_VMX_VMXOFF, "instr_vmx_vmxoff", "vmxoff", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
518 { DBGFEVENT_INSTR_VMX_VMXON, "instr_vmx_vmxon", "vmxon", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
519 { DBGFEVENT_INSTR_VMX_VMFUNC, "instr_vmx_vmfunc", "vmfunc", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
520 { DBGFEVENT_INSTR_VMX_INVEPT, "instr_vmx_invept", "invept", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
521 { DBGFEVENT_INSTR_VMX_INVVPID, "instr_vmx_invvpid", "invvpid", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
522 { DBGFEVENT_INSTR_VMX_INVPCID, "instr_vmx_invpcid", "invpcid", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
523 { DBGFEVENT_INSTR_SVM_VMRUN, "instr_svm_vmrun", "vmrun", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
524 { DBGFEVENT_INSTR_SVM_VMLOAD, "instr_svm_vmload", "vmload", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
525 { DBGFEVENT_INSTR_SVM_VMSAVE, "instr_svm_vmsave", "vmsave", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
526 { DBGFEVENT_INSTR_SVM_STGI, "instr_svm_stgi", "stgi", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
527 { DBGFEVENT_INSTR_SVM_CLGI, "instr_svm_clgi", "clgi", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
528 { DBGFEVENT_EXIT_TASK_SWITCH, "exit_task_switch", "task_switch", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
529 { DBGFEVENT_EXIT_HALT, "exit_halt", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
530 { DBGFEVENT_EXIT_MWAIT, "exit_mwait", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
531 { DBGFEVENT_EXIT_MONITOR, "exit_monitor", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
532 { DBGFEVENT_EXIT_CPUID, "exit_cpuid", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
533 { DBGFEVENT_EXIT_INVD, "exit_invd", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
534 { DBGFEVENT_EXIT_WBINVD, "exit_wbinvd", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
535 { DBGFEVENT_EXIT_INVLPG, "exit_invlpg", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
536 { DBGFEVENT_EXIT_RDTSC, "exit_rdtsc", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
537 { DBGFEVENT_EXIT_RDTSCP, "exit_rdtscp", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
538 { DBGFEVENT_EXIT_RDPMC, "exit_rdpmc", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
539 { DBGFEVENT_EXIT_RDMSR, "exit_rdmsr", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
540 { DBGFEVENT_EXIT_WRMSR, "exit_wrmsr", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
541 { DBGFEVENT_EXIT_CRX_READ, "exit_crx_read", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
542 { DBGFEVENT_EXIT_CRX_WRITE, "exit_crx_write", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
543 { DBGFEVENT_EXIT_DRX_READ, "exit_drx_read", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
544 { DBGFEVENT_EXIT_DRX_WRITE, "exit_drx_write", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
545 { DBGFEVENT_EXIT_PAUSE, "exit_pause", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
546 { DBGFEVENT_EXIT_XSETBV, "exit_xsetbv", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
547 { DBGFEVENT_EXIT_SIDT, "exit_sidt", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
548 { DBGFEVENT_EXIT_LIDT, "exit_lidt", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
549 { DBGFEVENT_EXIT_SGDT, "exit_sgdt", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
550 { DBGFEVENT_EXIT_LGDT, "exit_lgdt", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
551 { DBGFEVENT_EXIT_SLDT, "exit_sldt", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
552 { DBGFEVENT_EXIT_LLDT, "exit_lldt", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
553 { DBGFEVENT_EXIT_STR, "exit_str", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
554 { DBGFEVENT_EXIT_LTR, "exit_ltr", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
555 { DBGFEVENT_EXIT_GETSEC, "exit_getsec", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
556 { DBGFEVENT_EXIT_RSM, "exit_rsm", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
557 { DBGFEVENT_EXIT_RDRAND, "exit_rdrand", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
558 { DBGFEVENT_EXIT_RDSEED, "exit_rdseed", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
559 { DBGFEVENT_EXIT_XSAVES, "exit_xsaves", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
560 { DBGFEVENT_EXIT_XRSTORS, "exit_xrstors", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
561 { DBGFEVENT_EXIT_VMM_CALL, "exit_vmm_call", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
562 { DBGFEVENT_EXIT_VMX_VMCLEAR, "exit_vmx_vmclear", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
563 { DBGFEVENT_EXIT_VMX_VMLAUNCH, "exit_vmx_vmlaunch", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
564 { DBGFEVENT_EXIT_VMX_VMPTRLD, "exit_vmx_vmptrld", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
565 { DBGFEVENT_EXIT_VMX_VMPTRST, "exit_vmx_vmptrst", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
566 { DBGFEVENT_EXIT_VMX_VMREAD, "exit_vmx_vmread", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
567 { DBGFEVENT_EXIT_VMX_VMRESUME, "exit_vmx_vmresume", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
568 { DBGFEVENT_EXIT_VMX_VMWRITE, "exit_vmx_vmwrite", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
569 { DBGFEVENT_EXIT_VMX_VMXOFF, "exit_vmx_vmxoff", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
570 { DBGFEVENT_EXIT_VMX_VMXON, "exit_vmx_vmxon", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
571 { DBGFEVENT_EXIT_VMX_VMFUNC, "exit_vmx_vmfunc", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
572 { DBGFEVENT_EXIT_VMX_INVEPT, "exit_vmx_invept", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
573 { DBGFEVENT_EXIT_VMX_INVVPID, "exit_vmx_invvpid", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
574 { DBGFEVENT_EXIT_VMX_INVPCID, "exit_vmx_invpcid", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
575 { DBGFEVENT_EXIT_VMX_EPT_VIOLATION, "exit_vmx_ept_violation", "eptvio", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
576 { DBGFEVENT_EXIT_VMX_EPT_MISCONFIG, "exit_vmx_ept_misconfig", "eptmis", kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
577 { DBGFEVENT_EXIT_VMX_VAPIC_ACCESS, "exit_vmx_vapic_access", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
578 { DBGFEVENT_EXIT_VMX_VAPIC_WRITE, "exit_vmx_vapic_write", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
579 { DBGFEVENT_EXIT_SVM_VMRUN, "exit_svm_vmrun", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
580 { DBGFEVENT_EXIT_SVM_VMLOAD, "exit_svm_vmload", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
581 { DBGFEVENT_EXIT_SVM_VMSAVE, "exit_svm_vmsave", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
582 { DBGFEVENT_EXIT_SVM_STGI, "exit_svm_stgi", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
583 { DBGFEVENT_EXIT_SVM_CLGI, "exit_svm_clgi", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
584 { DBGFEVENT_IOPORT_UNASSIGNED, "pio_unassigned", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
585 { DBGFEVENT_IOPORT_UNUSED, "pio_unused", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
586 { DBGFEVENT_MEMORY_UNASSIGNED, "mmio_unassigned", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
587 { DBGFEVENT_MEMORY_ROM_WRITE, "rom_write", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
588 { DBGFEVENT_BSOD_MSR, "bsod_msr", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
589 { DBGFEVENT_BSOD_EFI, "bsod_efi", NULL, kDbgcSxEventKind_Plain, kDbgcEvtState_Disabled, 0, NULL },
590};
591/** Number of entries in g_aDbgcSxEvents. */
592const uint32_t g_cDbgcSxEvents = RT_ELEMENTS(g_aDbgcSxEvents);
593
594
595
596/**
597 * @callback_method_impl{FNDBGCCMD, The 'go' command.}
598 */
599static DECLCALLBACK(int) dbgcCmdGo(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
600{
601 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
602
603 /*
604 * Check if the VM is halted or not before trying to resume it.
605 */
606 if (!DBGFR3IsHalted(pUVM))
607 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The VM is already running");
608
609 int rc = DBGFR3Resume(pUVM);
610 if (RT_FAILURE(rc))
611 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3Resume");
612
613 NOREF(paArgs); NOREF(cArgs);
614 return VINF_SUCCESS;
615}
616
617
618/**
619 * @callback_method_impl{FNDBGCCMD, The 'ba' command.}
620 */
621static DECLCALLBACK(int) dbgcCmdBrkAccess(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
622{
623 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
624
625 /*
626 * Interpret access type.
627 */
628 if ( !strchr("xrwi", paArgs[0].u.pszString[0])
629 || paArgs[0].u.pszString[1])
630 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid access type '%s' for '%s'. Valid types are 'e', 'r', 'w' and 'i'",
631 paArgs[0].u.pszString, pCmd->pszCmd);
632 uint8_t fType = 0;
633 switch (paArgs[0].u.pszString[0])
634 {
635 case 'x': fType = X86_DR7_RW_EO; break;
636 case 'r': fType = X86_DR7_RW_RW; break;
637 case 'w': fType = X86_DR7_RW_WO; break;
638 case 'i': fType = X86_DR7_RW_IO; break;
639 }
640
641 /*
642 * Validate size.
643 */
644 if (fType == X86_DR7_RW_EO && paArgs[1].u.u64Number != 1)
645 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid access size %RX64 for '%s'. 'x' access type requires size 1!",
646 paArgs[1].u.u64Number, pCmd->pszCmd);
647 switch (paArgs[1].u.u64Number)
648 {
649 case 1:
650 case 2:
651 case 4:
652 break;
653 /*case 8: - later*/
654 default:
655 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid access size %RX64 for '%s'. 1, 2 or 4!",
656 paArgs[1].u.u64Number, pCmd->pszCmd);
657 }
658 uint8_t cb = (uint8_t)paArgs[1].u.u64Number;
659
660 /*
661 * Convert the pointer to a DBGF address.
662 */
663 DBGFADDRESS Address;
664 int rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &paArgs[2], &Address);
665 if (RT_FAILURE(rc))
666 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr(,%DV,)", &paArgs[2]);
667
668 /*
669 * Pick out the optional arguments.
670 */
671 uint64_t iHitTrigger = 0;
672 uint64_t iHitDisable = UINT64_MAX;
673 const char *pszCmds = NULL;
674 unsigned iArg = 3;
675 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
676 {
677 iHitTrigger = paArgs[iArg].u.u64Number;
678 iArg++;
679 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
680 {
681 iHitDisable = paArgs[iArg].u.u64Number;
682 iArg++;
683 }
684 }
685 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_STRING)
686 {
687 pszCmds = paArgs[iArg].u.pszString;
688 iArg++;
689 }
690
691 /*
692 * Try set the breakpoint.
693 */
694 uint32_t iBp;
695 rc = DBGFR3BpSetReg(pUVM, &Address, iHitTrigger, iHitDisable, fType, cb, &iBp);
696 if (RT_SUCCESS(rc))
697 {
698 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
699 rc = dbgcBpAdd(pDbgc, iBp, pszCmds);
700 if (RT_SUCCESS(rc))
701 return DBGCCmdHlpPrintf(pCmdHlp, "Set access breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
702 if (rc == VERR_DBGC_BP_EXISTS)
703 {
704 rc = dbgcBpUpdate(pDbgc, iBp, pszCmds);
705 if (RT_SUCCESS(rc))
706 return DBGCCmdHlpPrintf(pCmdHlp, "Updated access breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
707 }
708 int rc2 = DBGFR3BpClear(pDbgc->pUVM, iBp);
709 AssertRC(rc2);
710 }
711 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "Failed to set access breakpoint at %RGv", Address.FlatPtr);
712}
713
714
715/**
716 * @callback_method_impl{FNDBGCCMD, The 'bc' command.}
717 */
718static DECLCALLBACK(int) dbgcCmdBrkClear(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
719{
720 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
721
722 /*
723 * Enumerate the arguments.
724 */
725 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
726 int rc = VINF_SUCCESS;
727 for (unsigned iArg = 0; iArg < cArgs && RT_SUCCESS(rc); iArg++)
728 {
729 if (paArgs[iArg].enmType != DBGCVAR_TYPE_STRING)
730 {
731 /* one */
732 uint32_t iBp = (uint32_t)paArgs[iArg].u.u64Number;
733 if (iBp == paArgs[iArg].u.u64Number)
734 {
735 int rc2 = DBGFR3BpClear(pUVM, iBp);
736 if (RT_FAILURE(rc2))
737 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc2, "DBGFR3BpClear(,%#x)", iBp);
738 if (RT_SUCCESS(rc2) || rc2 == VERR_DBGF_BP_NOT_FOUND)
739 dbgcBpDelete(pDbgc, iBp);
740 }
741 else
742 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Breakpoint id %RX64 is too large", paArgs[iArg].u.u64Number);
743 }
744 else if (!strcmp(paArgs[iArg].u.pszString, "all"))
745 {
746 /* all */
747 PDBGCBP pBp = pDbgc->pFirstBp;
748 while (pBp)
749 {
750 uint32_t iBp = pBp->iBp;
751 pBp = pBp->pNext;
752
753 int rc2 = DBGFR3BpClear(pUVM, iBp);
754 if (RT_FAILURE(rc2))
755 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc2, "DBGFR3BpClear(,%#x)", iBp);
756 if (RT_SUCCESS(rc2) || rc2 == VERR_DBGF_BP_NOT_FOUND)
757 dbgcBpDelete(pDbgc, iBp);
758 }
759 }
760 else
761 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid argument '%s'", paArgs[iArg].u.pszString);
762 }
763 return rc;
764}
765
766
767/**
768 * @callback_method_impl{FNDBGCCMD, The 'bd' command.}
769 */
770static DECLCALLBACK(int) dbgcCmdBrkDisable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
771{
772 /*
773 * Enumerate the arguments.
774 */
775 int rc = VINF_SUCCESS;
776 for (unsigned iArg = 0; iArg < cArgs && RT_SUCCESS(rc); iArg++)
777 {
778 if (paArgs[iArg].enmType != DBGCVAR_TYPE_STRING)
779 {
780 /* one */
781 uint32_t iBp = (uint32_t)paArgs[iArg].u.u64Number;
782 if (iBp == paArgs[iArg].u.u64Number)
783 {
784 rc = DBGFR3BpDisable(pUVM, iBp);
785 if (RT_FAILURE(rc))
786 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3BpDisable failed for breakpoint %#x", iBp);
787 }
788 else
789 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Breakpoint id %RX64 is too large", paArgs[iArg].u.u64Number);
790 }
791 else if (!strcmp(paArgs[iArg].u.pszString, "all"))
792 {
793 /* all */
794 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
795 for (PDBGCBP pBp = pDbgc->pFirstBp; pBp; pBp = pBp->pNext)
796 {
797 int rc2 = DBGFR3BpDisable(pUVM, pBp->iBp);
798 if (RT_FAILURE(rc2))
799 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc2, "DBGFR3BpDisable failed for breakpoint %#x", pBp->iBp);
800 }
801 }
802 else
803 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid argument '%s'", paArgs[iArg].u.pszString);
804 }
805 return rc;
806}
807
808
809/**
810 * @callback_method_impl{FNDBGCCMD, The 'be' command.}
811 */
812static DECLCALLBACK(int) dbgcCmdBrkEnable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
813{
814 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
815
816 /*
817 * Enumerate the arguments.
818 */
819 int rc = VINF_SUCCESS;
820 for (unsigned iArg = 0; iArg < cArgs && RT_SUCCESS(rc); iArg++)
821 {
822 if (paArgs[iArg].enmType != DBGCVAR_TYPE_STRING)
823 {
824 /* one */
825 uint32_t iBp = (uint32_t)paArgs[iArg].u.u64Number;
826 if (iBp == paArgs[iArg].u.u64Number)
827 {
828 rc = DBGFR3BpEnable(pUVM, iBp);
829 if (RT_FAILURE(rc))
830 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3BpEnable failed for breakpoint %#x", iBp);
831 }
832 else
833 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Breakpoint id %RX64 is too large", paArgs[iArg].u.u64Number);
834 }
835 else if (!strcmp(paArgs[iArg].u.pszString, "all"))
836 {
837 /* all */
838 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
839 for (PDBGCBP pBp = pDbgc->pFirstBp; pBp; pBp = pBp->pNext)
840 {
841 int rc2 = DBGFR3BpEnable(pUVM, pBp->iBp);
842 if (RT_FAILURE(rc2))
843 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc2, "DBGFR3BpEnable failed for breakpoint %#x", pBp->iBp);
844 }
845 }
846 else
847 rc = DBGCCmdHlpFail(pCmdHlp, pCmd, "Invalid argument '%s'", paArgs[iArg].u.pszString);
848 }
849 return rc;
850}
851
852
853/**
854 * Breakpoint enumeration callback function.
855 *
856 * @returns VBox status code. Any failure will stop the enumeration.
857 * @param pUVM The user mode VM handle.
858 * @param pvUser The user argument.
859 * @param pBp Pointer to the breakpoint information. (readonly)
860 */
861static DECLCALLBACK(int) dbgcEnumBreakpointsCallback(PUVM pUVM, void *pvUser, PCDBGFBP pBp)
862{
863 PDBGC pDbgc = (PDBGC)pvUser;
864 PDBGCBP pDbgcBp = dbgcBpGet(pDbgc, pBp->iBp);
865
866 /*
867 * BP type and size.
868 */
869 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%#4x %c ", pBp->iBp, pBp->fEnabled ? 'e' : 'd');
870 bool fHasAddress = false;
871 switch (pBp->enmType)
872 {
873 case DBGFBPTYPE_INT3:
874 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " p %RGv", pBp->u.Int3.GCPtr);
875 fHasAddress = true;
876 break;
877 case DBGFBPTYPE_REG:
878 {
879 char chType;
880 switch (pBp->u.Reg.fType)
881 {
882 case X86_DR7_RW_EO: chType = 'x'; break;
883 case X86_DR7_RW_WO: chType = 'w'; break;
884 case X86_DR7_RW_IO: chType = 'i'; break;
885 case X86_DR7_RW_RW: chType = 'r'; break;
886 default: chType = '?'; break;
887
888 }
889 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%d %c %RGv", pBp->u.Reg.cb, chType, pBp->u.Reg.GCPtr);
890 fHasAddress = true;
891 break;
892 }
893
894 case DBGFBPTYPE_REM:
895 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " r %RGv", pBp->u.Rem.GCPtr);
896 fHasAddress = true;
897 break;
898
899/** @todo realign the list when I/O and MMIO breakpoint command have been added and it's possible to test this code. */
900 case DBGFBPTYPE_PORT_IO:
901 case DBGFBPTYPE_MMIO:
902 {
903 uint32_t fAccess = pBp->enmType == DBGFBPTYPE_PORT_IO ? pBp->u.PortIo.fAccess : pBp->u.Mmio.fAccess;
904 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, pBp->enmType == DBGFBPTYPE_PORT_IO ? " i" : " m");
905 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " %c%c%c%c%c%c",
906 fAccess & DBGFBPIOACCESS_READ_MASK ? 'r' : '-',
907 fAccess & DBGFBPIOACCESS_READ_BYTE ? '1' : '-',
908 fAccess & DBGFBPIOACCESS_READ_WORD ? '2' : '-',
909 fAccess & DBGFBPIOACCESS_READ_DWORD ? '4' : '-',
910 fAccess & DBGFBPIOACCESS_READ_QWORD ? '8' : '-',
911 fAccess & DBGFBPIOACCESS_READ_OTHER ? '+' : '-');
912 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " %c%c%c%c%c%c",
913 fAccess & DBGFBPIOACCESS_WRITE_MASK ? 'w' : '-',
914 fAccess & DBGFBPIOACCESS_WRITE_BYTE ? '1' : '-',
915 fAccess & DBGFBPIOACCESS_WRITE_WORD ? '2' : '-',
916 fAccess & DBGFBPIOACCESS_WRITE_DWORD ? '4' : '-',
917 fAccess & DBGFBPIOACCESS_WRITE_QWORD ? '8' : '-',
918 fAccess & DBGFBPIOACCESS_WRITE_OTHER ? '+' : '-');
919 if (pBp->enmType == DBGFBPTYPE_PORT_IO)
920 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " %04x-%04x",
921 pBp->u.PortIo.uPort, pBp->u.PortIo.uPort + pBp->u.PortIo.cPorts - 1);
922 else
923 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%RGp LB %03x", pBp->u.Mmio.PhysAddr, pBp->u.Mmio.cb);
924 break;
925 }
926
927 default:
928 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " unknown type %d!!", pBp->enmType);
929 AssertFailed();
930 break;
931
932 }
933 if (pBp->iHitDisable == ~(uint64_t)0)
934 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " %04RX64 (%04RX64 to ~0) ", pBp->cHits, pBp->iHitTrigger);
935 else
936 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " %04RX64 (%04RX64 to %04RX64)", pBp->cHits, pBp->iHitTrigger, pBp->iHitDisable);
937
938 /*
939 * Try resolve the address if it has one.
940 */
941 if (fHasAddress)
942 {
943 RTDBGSYMBOL Sym;
944 RTINTPTR off;
945 DBGFADDRESS Addr;
946 int rc = DBGFR3AsSymbolByAddr(pUVM, pDbgc->hDbgAs, DBGFR3AddrFromFlat(pDbgc->pUVM, &Addr, pBp->u.GCPtr),
947 RTDBGSYMADDR_FLAGS_LESS_OR_EQUAL, &off, &Sym, NULL);
948 if (RT_SUCCESS(rc))
949 {
950 if (!off)
951 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%s", Sym.szName);
952 else if (off > 0)
953 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%s+%RGv", Sym.szName, off);
954 else
955 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "%s-%RGv", Sym.szName, -off);
956 }
957 }
958
959 /*
960 * The commands.
961 */
962 if (pDbgcBp)
963 {
964 if (pDbgcBp->cchCmd)
965 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "\n cmds: '%s'\n", pDbgcBp->szCmd);
966 else
967 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "\n");
968 }
969 else
970 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, " [unknown bp]\n");
971
972 return VINF_SUCCESS;
973}
974
975
976/**
977 * @callback_method_impl{FNDBGCCMD, The 'bl' command.}
978 */
979static DECLCALLBACK(int) dbgcCmdBrkList(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
980{
981 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
982 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, -1, cArgs == 0);
983 NOREF(paArgs);
984
985 /*
986 * Enumerate the breakpoints.
987 */
988 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
989 int rc = DBGFR3BpEnum(pUVM, dbgcEnumBreakpointsCallback, pDbgc);
990 if (RT_FAILURE(rc))
991 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3BpEnum");
992 return rc;
993}
994
995
996/**
997 * @callback_method_impl{FNDBGCCMD, The 'bp' command.}
998 */
999static DECLCALLBACK(int) dbgcCmdBrkSet(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
1000{
1001 /*
1002 * Convert the pointer to a DBGF address.
1003 */
1004 DBGFADDRESS Address;
1005 int rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &paArgs[0], &Address);
1006 if (RT_FAILURE(rc))
1007 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr(,'%DV',)", &paArgs[0]);
1008
1009 /*
1010 * Pick out the optional arguments.
1011 */
1012 uint64_t iHitTrigger = 0;
1013 uint64_t iHitDisable = UINT64_MAX;
1014 const char *pszCmds = NULL;
1015 unsigned iArg = 1;
1016 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
1017 {
1018 iHitTrigger = paArgs[iArg].u.u64Number;
1019 iArg++;
1020 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
1021 {
1022 iHitDisable = paArgs[iArg].u.u64Number;
1023 iArg++;
1024 }
1025 }
1026 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_STRING)
1027 {
1028 pszCmds = paArgs[iArg].u.pszString;
1029 iArg++;
1030 }
1031
1032 /*
1033 * Try set the breakpoint.
1034 */
1035 uint32_t iBp;
1036 rc = DBGFR3BpSet(pUVM, &Address, iHitTrigger, iHitDisable, &iBp);
1037 if (RT_SUCCESS(rc))
1038 {
1039 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1040 rc = dbgcBpAdd(pDbgc, iBp, pszCmds);
1041 if (RT_SUCCESS(rc))
1042 return DBGCCmdHlpPrintf(pCmdHlp, "Set breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
1043 if (rc == VERR_DBGC_BP_EXISTS)
1044 {
1045 rc = dbgcBpUpdate(pDbgc, iBp, pszCmds);
1046 if (RT_SUCCESS(rc))
1047 return DBGCCmdHlpPrintf(pCmdHlp, "Updated breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
1048 }
1049 int rc2 = DBGFR3BpClear(pDbgc->pUVM, iBp);
1050 AssertRC(rc2);
1051 }
1052 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "Failed to set breakpoint at %RGv", Address.FlatPtr);
1053}
1054
1055
1056/**
1057 * @callback_method_impl{FNDBGCCMD, The 'br' command.}
1058 */
1059static DECLCALLBACK(int) dbgcCmdBrkREM(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
1060{
1061 /*
1062 * Convert the pointer to a DBGF address.
1063 */
1064 DBGFADDRESS Address;
1065 int rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &paArgs[0], &Address);
1066 if (RT_FAILURE(rc))
1067 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr(,'%DV',)", &paArgs[0]);
1068
1069 /*
1070 * Pick out the optional arguments.
1071 */
1072 uint64_t iHitTrigger = 0;
1073 uint64_t iHitDisable = UINT64_MAX;
1074 const char *pszCmds = NULL;
1075 unsigned iArg = 1;
1076 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
1077 {
1078 iHitTrigger = paArgs[iArg].u.u64Number;
1079 iArg++;
1080 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
1081 {
1082 iHitDisable = paArgs[iArg].u.u64Number;
1083 iArg++;
1084 }
1085 }
1086 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_STRING)
1087 {
1088 pszCmds = paArgs[iArg].u.pszString;
1089 iArg++;
1090 }
1091
1092 /*
1093 * Try set the breakpoint.
1094 */
1095 uint32_t iBp;
1096 rc = DBGFR3BpSetREM(pUVM, &Address, iHitTrigger, iHitDisable, &iBp);
1097 if (RT_SUCCESS(rc))
1098 {
1099 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1100 rc = dbgcBpAdd(pDbgc, iBp, pszCmds);
1101 if (RT_SUCCESS(rc))
1102 return DBGCCmdHlpPrintf(pCmdHlp, "Set REM breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
1103 if (rc == VERR_DBGC_BP_EXISTS)
1104 {
1105 rc = dbgcBpUpdate(pDbgc, iBp, pszCmds);
1106 if (RT_SUCCESS(rc))
1107 return DBGCCmdHlpPrintf(pCmdHlp, "Updated REM breakpoint %u at %RGv\n", iBp, Address.FlatPtr);
1108 }
1109 int rc2 = DBGFR3BpClear(pDbgc->pUVM, iBp);
1110 AssertRC(rc2);
1111 }
1112 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "Failed to set REM breakpoint at %RGv", Address.FlatPtr);
1113}
1114
1115
1116/**
1117 * Helps the unassmble ('u') command display symbols it starts at and passes.
1118 *
1119 * @param pUVM The user mode VM handle.
1120 * @param pCmdHlp The command helpers for printing via.
1121 * @param hDbgAs The address space to look up addresses in.
1122 * @param pAddress The current address.
1123 * @param pcbCallAgain Where to return the distance to the next check (in
1124 * instruction bytes).
1125 */
1126static void dbgcCmdUnassambleHelpListNear(PUVM pUVM, PDBGCCMDHLP pCmdHlp, RTDBGAS hDbgAs, PCDBGFADDRESS pAddress,
1127 PRTUINTPTR pcbCallAgain)
1128{
1129 RTDBGSYMBOL Symbol;
1130 RTGCINTPTR offDispSym;
1131 int rc = DBGFR3AsSymbolByAddr(pUVM, hDbgAs, pAddress, RTDBGSYMADDR_FLAGS_LESS_OR_EQUAL, &offDispSym, &Symbol, NULL);
1132 if (RT_FAILURE(rc) || offDispSym > _1G)
1133 rc = DBGFR3AsSymbolByAddr(pUVM, hDbgAs, pAddress, RTDBGSYMADDR_FLAGS_GREATER_OR_EQUAL, &offDispSym, &Symbol, NULL);
1134 if (RT_SUCCESS(rc) && offDispSym < _1G)
1135 {
1136 if (!offDispSym)
1137 {
1138 DBGCCmdHlpPrintf(pCmdHlp, "%s:\n", Symbol.szName);
1139 *pcbCallAgain = !Symbol.cb ? 64 : Symbol.cb;
1140 }
1141 else if (offDispSym > 0)
1142 {
1143 DBGCCmdHlpPrintf(pCmdHlp, "%s+%#llx:\n", Symbol.szName, (uint64_t)offDispSym);
1144 *pcbCallAgain = !Symbol.cb ? 64 : Symbol.cb > (RTGCUINTPTR)offDispSym ? Symbol.cb - (RTGCUINTPTR)offDispSym : 1;
1145 }
1146 else
1147 {
1148 DBGCCmdHlpPrintf(pCmdHlp, "%s-%#llx:\n", Symbol.szName, (uint64_t)-offDispSym);
1149 *pcbCallAgain = !Symbol.cb ? 64 : (RTGCUINTPTR)-offDispSym + Symbol.cb;
1150 }
1151 }
1152 else
1153 *pcbCallAgain = UINT32_MAX;
1154}
1155
1156
1157/**
1158 * @callback_method_impl{FNDBGCCMD, The 'u' command.}
1159 */
1160static DECLCALLBACK(int) dbgcCmdUnassemble(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
1161{
1162 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1163
1164 /*
1165 * Validate input.
1166 */
1167 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
1168 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, -1, cArgs <= 1);
1169 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs == 0 || DBGCVAR_ISPOINTER(paArgs[0].enmType));
1170
1171 if (!cArgs && !DBGCVAR_ISPOINTER(pDbgc->DisasmPos.enmType))
1172 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Don't know where to start disassembling");
1173
1174 /*
1175 * Check the desired mode.
1176 */
1177 unsigned fFlags = DBGF_DISAS_FLAGS_NO_ADDRESS | DBGF_DISAS_FLAGS_UNPATCHED_BYTES | DBGF_DISAS_FLAGS_ANNOTATE_PATCHED;
1178 switch (pCmd->pszCmd[1])
1179 {
1180 default: AssertFailed();
1181 case '\0': fFlags |= DBGF_DISAS_FLAGS_DEFAULT_MODE; break;
1182 case '6': fFlags |= DBGF_DISAS_FLAGS_64BIT_MODE; break;
1183 case '3': fFlags |= DBGF_DISAS_FLAGS_32BIT_MODE; break;
1184 case '1': fFlags |= DBGF_DISAS_FLAGS_16BIT_MODE; break;
1185 case 'v': fFlags |= DBGF_DISAS_FLAGS_16BIT_REAL_MODE; break;
1186 }
1187
1188 /** @todo should use DBGFADDRESS for everything */
1189
1190 /*
1191 * Find address.
1192 */
1193 if (!cArgs)
1194 {
1195 if (!DBGCVAR_ISPOINTER(pDbgc->DisasmPos.enmType))
1196 {
1197 /** @todo Batch query CS, RIP, CPU mode and flags. */
1198 PVMCPU pVCpu = VMMR3GetCpuByIdU(pUVM, pDbgc->idCpu);
1199 if ( pDbgc->fRegCtxGuest
1200 && CPUMIsGuestIn64BitCode(pVCpu))
1201 {
1202 pDbgc->DisasmPos.enmType = DBGCVAR_TYPE_GC_FLAT;
1203 pDbgc->SourcePos.u.GCFlat = CPUMGetGuestRIP(pVCpu);
1204 }
1205 else
1206 {
1207 pDbgc->DisasmPos.enmType = DBGCVAR_TYPE_GC_FAR;
1208 pDbgc->SourcePos.u.GCFar.off = pDbgc->fRegCtxGuest ? CPUMGetGuestEIP(pVCpu) : CPUMGetHyperEIP(pVCpu);
1209 pDbgc->SourcePos.u.GCFar.sel = pDbgc->fRegCtxGuest ? CPUMGetGuestCS(pVCpu) : CPUMGetHyperCS(pVCpu);
1210 if ( (fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_DEFAULT_MODE
1211 && pDbgc->fRegCtxGuest
1212 && (CPUMGetGuestEFlags(pVCpu) & X86_EFL_VM))
1213 {
1214 fFlags &= ~DBGF_DISAS_FLAGS_MODE_MASK;
1215 fFlags |= DBGF_DISAS_FLAGS_16BIT_REAL_MODE;
1216 }
1217 }
1218
1219 if (pDbgc->fRegCtxGuest)
1220 fFlags |= DBGF_DISAS_FLAGS_CURRENT_GUEST;
1221 else
1222 fFlags |= DBGF_DISAS_FLAGS_CURRENT_HYPER | DBGF_DISAS_FLAGS_HYPER;
1223 }
1224 else if ((fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_DEFAULT_MODE && pDbgc->fDisasm)
1225 {
1226 fFlags &= ~DBGF_DISAS_FLAGS_MODE_MASK;
1227 fFlags |= pDbgc->fDisasm & (DBGF_DISAS_FLAGS_MODE_MASK | DBGF_DISAS_FLAGS_HYPER);
1228 }
1229 pDbgc->DisasmPos.enmRangeType = DBGCVAR_RANGE_NONE;
1230 }
1231 else
1232 pDbgc->DisasmPos = paArgs[0];
1233 pDbgc->pLastPos = &pDbgc->DisasmPos;
1234
1235 /*
1236 * Range.
1237 */
1238 switch (pDbgc->DisasmPos.enmRangeType)
1239 {
1240 case DBGCVAR_RANGE_NONE:
1241 pDbgc->DisasmPos.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
1242 pDbgc->DisasmPos.u64Range = 10;
1243 break;
1244
1245 case DBGCVAR_RANGE_ELEMENTS:
1246 if (pDbgc->DisasmPos.u64Range > 2048)
1247 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Too many lines requested. Max is 2048 lines");
1248 break;
1249
1250 case DBGCVAR_RANGE_BYTES:
1251 if (pDbgc->DisasmPos.u64Range > 65536)
1252 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The requested range is too big. Max is 64KB");
1253 break;
1254
1255 default:
1256 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Unknown range type %d", pDbgc->DisasmPos.enmRangeType);
1257 }
1258
1259 /*
1260 * Convert physical and host addresses to guest addresses.
1261 */
1262 RTDBGAS hDbgAs = pDbgc->hDbgAs;
1263 int rc;
1264 switch (pDbgc->DisasmPos.enmType)
1265 {
1266 case DBGCVAR_TYPE_GC_FLAT:
1267 case DBGCVAR_TYPE_GC_FAR:
1268 break;
1269 case DBGCVAR_TYPE_GC_PHYS:
1270 hDbgAs = DBGF_AS_PHYS;
1271 case DBGCVAR_TYPE_HC_FLAT:
1272 case DBGCVAR_TYPE_HC_PHYS:
1273 {
1274 DBGCVAR VarTmp;
1275 rc = DBGCCmdHlpEval(pCmdHlp, &VarTmp, "%%(%Dv)", &pDbgc->DisasmPos);
1276 if (RT_FAILURE(rc))
1277 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "failed to evaluate '%%(%Dv)'", &pDbgc->DisasmPos);
1278 pDbgc->DisasmPos = VarTmp;
1279 break;
1280 }
1281 default: AssertFailed(); break;
1282 }
1283
1284 DBGFADDRESS CurAddr;
1285 if ( (fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_16BIT_REAL_MODE
1286 && pDbgc->DisasmPos.enmType == DBGCVAR_TYPE_GC_FAR)
1287 DBGFR3AddrFromFlat(pUVM, &CurAddr, ((uint32_t)pDbgc->DisasmPos.u.GCFar.sel << 4) + pDbgc->DisasmPos.u.GCFar.off);
1288 else
1289 {
1290 rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &pDbgc->DisasmPos, &CurAddr);
1291 if (RT_FAILURE(rc))
1292 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr failed on '%Dv'", &pDbgc->DisasmPos);
1293 }
1294
1295 if (CurAddr.fFlags & DBGFADDRESS_FLAGS_HMA)
1296 fFlags |= DBGF_DISAS_FLAGS_HYPER; /* This crap is due to not using DBGFADDRESS as DBGFR3Disas* input. */
1297 pDbgc->fDisasm = fFlags;
1298
1299 /*
1300 * Figure out where we are and display it. Also calculate when we need to
1301 * check for a new symbol if possible.
1302 */
1303 RTGCUINTPTR cbCheckSymbol;
1304 dbgcCmdUnassambleHelpListNear(pUVM, pCmdHlp, hDbgAs, &CurAddr, &cbCheckSymbol);
1305
1306 /*
1307 * Do the disassembling.
1308 */
1309 unsigned cTries = 32;
1310 int iRangeLeft = (int)pDbgc->DisasmPos.u64Range;
1311 if (iRangeLeft == 0) /* kludge for 'r'. */
1312 iRangeLeft = -1;
1313 for (;;)
1314 {
1315 /*
1316 * Disassemble the instruction.
1317 */
1318 char szDis[256];
1319 uint32_t cbInstr = 1;
1320 if (pDbgc->DisasmPos.enmType == DBGCVAR_TYPE_GC_FLAT)
1321 rc = DBGFR3DisasInstrEx(pUVM, pDbgc->idCpu, DBGF_SEL_FLAT, pDbgc->DisasmPos.u.GCFlat, fFlags,
1322 &szDis[0], sizeof(szDis), &cbInstr);
1323 else
1324 rc = DBGFR3DisasInstrEx(pUVM, pDbgc->idCpu, pDbgc->DisasmPos.u.GCFar.sel, pDbgc->DisasmPos.u.GCFar.off, fFlags,
1325 &szDis[0], sizeof(szDis), &cbInstr);
1326 if (RT_SUCCESS(rc))
1327 {
1328 /* print it */
1329 rc = DBGCCmdHlpPrintf(pCmdHlp, "%-16DV %s\n", &pDbgc->DisasmPos, &szDis[0]);
1330 if (RT_FAILURE(rc))
1331 return rc;
1332 }
1333 else
1334 {
1335 /* bitch. */
1336 int rc2 = DBGCCmdHlpPrintf(pCmdHlp, "Failed to disassemble instruction, skipping one byte.\n");
1337 if (RT_FAILURE(rc2))
1338 return rc2;
1339 if (cTries-- > 0)
1340 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "Too many disassembly failures. Giving up");
1341 cbInstr = 1;
1342 }
1343
1344 /* advance */
1345 if (iRangeLeft < 0) /* 'r' */
1346 break;
1347 if (pDbgc->DisasmPos.enmRangeType == DBGCVAR_RANGE_ELEMENTS)
1348 iRangeLeft--;
1349 else
1350 iRangeLeft -= cbInstr;
1351 rc = DBGCCmdHlpEval(pCmdHlp, &pDbgc->DisasmPos, "(%Dv) + %x", &pDbgc->DisasmPos, cbInstr);
1352 if (RT_FAILURE(rc))
1353 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpEval(,,'(%Dv) + %x')", &pDbgc->DisasmPos, cbInstr);
1354 if (iRangeLeft <= 0)
1355 break;
1356 fFlags &= ~(DBGF_DISAS_FLAGS_CURRENT_GUEST | DBGF_DISAS_FLAGS_CURRENT_HYPER);
1357
1358 /* Print next symbol? */
1359 if (cbCheckSymbol <= cbInstr)
1360 {
1361 if ( (fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_16BIT_REAL_MODE
1362 && pDbgc->DisasmPos.enmType == DBGCVAR_TYPE_GC_FAR)
1363 DBGFR3AddrFromFlat(pUVM, &CurAddr, ((uint32_t)pDbgc->DisasmPos.u.GCFar.sel << 4) + pDbgc->DisasmPos.u.GCFar.off);
1364 else
1365 rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &pDbgc->DisasmPos, &CurAddr);
1366 if (RT_SUCCESS(rc))
1367 dbgcCmdUnassambleHelpListNear(pUVM, pCmdHlp, hDbgAs, &CurAddr, &cbCheckSymbol);
1368 else
1369 cbCheckSymbol = UINT32_MAX;
1370 }
1371 else
1372 cbCheckSymbol -= cbInstr;
1373 }
1374
1375 NOREF(pCmd);
1376 return VINF_SUCCESS;
1377}
1378
1379
1380/**
1381 * @callback_method_impl{FNDGCSCREENBLIT}
1382 */
1383static DECLCALLBACK(int) dbgcCmdUnassembleCfgBlit(const char *psz, void *pvUser)
1384{
1385 PDBGCCMDHLP pCmdHlp = (PDBGCCMDHLP)pvUser;
1386 return DBGCCmdHlpPrintf(pCmdHlp, "%s", psz);
1387}
1388
1389
1390/**
1391 * Checks whether both addresses are equal.
1392 *
1393 * @returns true if both addresses point to the same location, false otherwise.
1394 * @param pAddr1 First address.
1395 * @param pAddr2 Second address.
1396 */
1397static bool dbgcCmdUnassembleCfgAddrEqual(PDBGFADDRESS pAddr1, PDBGFADDRESS pAddr2)
1398{
1399 return pAddr1->Sel == pAddr2->Sel
1400 && pAddr1->off == pAddr2->off;
1401}
1402
1403
1404/**
1405 * Checks whether the first given address is lower than the second one.
1406 *
1407 * @returns true if both addresses point to the same location, false otherwise.
1408 * @param pAddr1 First address.
1409 * @param pAddr2 Second address.
1410 */
1411static bool dbgcCmdUnassembleCfgAddrLower(PDBGFADDRESS pAddr1, PDBGFADDRESS pAddr2)
1412{
1413 return pAddr1->Sel == pAddr2->Sel
1414 && pAddr1->off < pAddr2->off;
1415}
1416
1417
1418/**
1419 * Calculates the size required for the given basic block including the
1420 * border and spacing on the edges.
1421 *
1422 * @returns nothing.
1423 * @param hFlowBb The basic block handle.
1424 * @param pDumpBb The dumper state to fill in for the basic block.
1425 */
1426static void dbgcCmdUnassembleCfgDumpCalcBbSize(DBGFFLOWBB hFlowBb, PDBGCFLOWBBDUMP pDumpBb)
1427{
1428 uint32_t fFlags = DBGFR3FlowBbGetFlags(hFlowBb);
1429 uint32_t cInstr = DBGFR3FlowBbGetInstrCount(hFlowBb);
1430
1431 pDumpBb->hFlowBb = hFlowBb;
1432 pDumpBb->cchHeight = cInstr + 4; /* Include spacing and border top and bottom. */
1433 pDumpBb->cchWidth = 0;
1434 DBGFR3FlowBbGetStartAddress(hFlowBb, &pDumpBb->AddrStart);
1435
1436 DBGFFLOWBBENDTYPE enmType = DBGFR3FlowBbGetType(hFlowBb);
1437 if ( enmType == DBGFFLOWBBENDTYPE_COND
1438 || enmType == DBGFFLOWBBENDTYPE_UNCOND_JMP)
1439 DBGFR3FlowBbGetBranchAddress(hFlowBb, &pDumpBb->AddrTarget);
1440
1441 if (fFlags & DBGF_FLOW_BB_F_INCOMPLETE_ERR)
1442 {
1443 const char *pszErr = NULL;
1444 DBGFR3FlowBbQueryError(hFlowBb, &pszErr);
1445 if (pszErr)
1446 {
1447 pDumpBb->cchHeight++;
1448 pDumpBb->cchWidth = RT_MAX(pDumpBb->cchWidth, (uint32_t)strlen(pszErr));
1449 }
1450 }
1451 for (unsigned i = 0; i < cInstr; i++)
1452 {
1453 const char *pszInstr = NULL;
1454 int rc = DBGFR3FlowBbQueryInstr(hFlowBb, i, NULL, NULL, &pszInstr);
1455 AssertRC(rc);
1456 pDumpBb->cchWidth = RT_MAX(pDumpBb->cchWidth, (uint32_t)strlen(pszInstr));
1457 }
1458 pDumpBb->cchWidth += 4; /* Include spacing and border left and right. */
1459}
1460
1461
1462/**
1463 * Dumps a top or bottom boundary line.
1464 *
1465 * @returns nothing.
1466 * @param hScreen The screen to draw to.
1467 * @param uStartX Where to start drawing the boundary.
1468 * @param uStartY Y coordinate.
1469 * @param cchWidth Width of the boundary.
1470 * @param enmColor The color to use for drawing.
1471 */
1472static void dbgcCmdUnassembleCfgDumpBbBoundary(DBGCSCREEN hScreen, uint32_t uStartX, uint32_t uStartY, uint32_t cchWidth,
1473 DBGCSCREENCOLOR enmColor)
1474{
1475 dbgcScreenAsciiDrawCharacter(hScreen, uStartX, uStartY, '+', enmColor);
1476 dbgcScreenAsciiDrawLineHorizontal(hScreen, uStartX + 1, uStartX + 1 + cchWidth - 2,
1477 uStartY, '-', enmColor);
1478 dbgcScreenAsciiDrawCharacter(hScreen, uStartX + cchWidth - 1, uStartY, '+', enmColor);
1479}
1480
1481
1482/**
1483 * Dumps a spacing line between the top or bottom boundary and the actual disassembly.
1484 *
1485 * @returns nothing.
1486 * @param hScreen The screen to draw to.
1487 * @param uStartX Where to start drawing the spacing.
1488 * @param uStartY Y coordinate.
1489 * @param cchWidth Width of the spacing.
1490 * @param enmColor The color to use for drawing.
1491 */
1492static void dbgcCmdUnassembleCfgDumpBbSpacing(DBGCSCREEN hScreen, uint32_t uStartX, uint32_t uStartY, uint32_t cchWidth,
1493 DBGCSCREENCOLOR enmColor)
1494{
1495 dbgcScreenAsciiDrawCharacter(hScreen, uStartX, uStartY, '|', enmColor);
1496 dbgcScreenAsciiDrawLineHorizontal(hScreen, uStartX + 1, uStartX + 1 + cchWidth - 2,
1497 uStartY, ' ', enmColor);
1498 dbgcScreenAsciiDrawCharacter(hScreen, uStartX + cchWidth - 1, uStartY, '|', enmColor);
1499}
1500
1501
1502/**
1503 * Writes a given text to the screen.
1504 *
1505 * @returns nothing.
1506 * @param hScreen The screen to draw to.
1507 * @param uStartX Where to start drawing the line.
1508 * @param uStartY Y coordinate.
1509 * @param cchWidth Maximum width of the text.
1510 * @param pszText The text to write.
1511 * @param enmTextColor The color to use for drawing the text.
1512 * @param enmBorderColor The color to use for drawing the border.
1513 */
1514static void dbgcCmdUnassembleCfgDumpBbText(DBGCSCREEN hScreen, uint32_t uStartX, uint32_t uStartY,
1515 uint32_t cchWidth, const char *pszText,
1516 DBGCSCREENCOLOR enmTextColor, DBGCSCREENCOLOR enmBorderColor)
1517{
1518 dbgcScreenAsciiDrawCharacter(hScreen, uStartX, uStartY, '|', enmBorderColor);
1519 dbgcScreenAsciiDrawCharacter(hScreen, uStartX + 1, uStartY, ' ', enmTextColor);
1520 dbgcScreenAsciiDrawString(hScreen, uStartX + 2, uStartY, pszText, enmTextColor);
1521 dbgcScreenAsciiDrawCharacter(hScreen, uStartX + cchWidth - 1, uStartY, '|', enmBorderColor);
1522}
1523
1524
1525/**
1526 * Dumps one basic block using the dumper callback.
1527 *
1528 * @returns nothing.
1529 * @param pDumpBb The basic block dump state to dump.
1530 * @param hScreen The screen to draw to.
1531 */
1532static void dbgcCmdUnassembleCfgDumpBb(PDBGCFLOWBBDUMP pDumpBb, DBGCSCREEN hScreen)
1533{
1534 uint32_t uStartY = pDumpBb->uStartY;
1535 bool fError = RT_BOOL(DBGFR3FlowBbGetFlags(pDumpBb->hFlowBb) & DBGF_FLOW_BB_F_INCOMPLETE_ERR);
1536 DBGCSCREENCOLOR enmColor = fError ? DBGCSCREENCOLOR_RED_BRIGHT : DBGCSCREENCOLOR_DEFAULT;
1537
1538 dbgcCmdUnassembleCfgDumpBbBoundary(hScreen, pDumpBb->uStartX, uStartY, pDumpBb->cchWidth, enmColor);
1539 uStartY++;
1540 dbgcCmdUnassembleCfgDumpBbSpacing(hScreen, pDumpBb->uStartX, uStartY, pDumpBb->cchWidth, enmColor);
1541 uStartY++;
1542
1543 uint32_t cInstr = DBGFR3FlowBbGetInstrCount(pDumpBb->hFlowBb);
1544 for (unsigned i = 0; i < cInstr; i++)
1545 {
1546 const char *pszInstr = NULL;
1547 DBGFR3FlowBbQueryInstr(pDumpBb->hFlowBb, i, NULL, NULL, &pszInstr);
1548 dbgcCmdUnassembleCfgDumpBbText(hScreen, pDumpBb->uStartX, uStartY + i,
1549 pDumpBb->cchWidth, pszInstr, DBGCSCREENCOLOR_DEFAULT,
1550 enmColor);
1551 }
1552 uStartY += cInstr;
1553
1554 if (fError)
1555 {
1556 const char *pszErr = NULL;
1557 DBGFR3FlowBbQueryError(pDumpBb->hFlowBb, &pszErr);
1558 if (pszErr)
1559 dbgcCmdUnassembleCfgDumpBbText(hScreen, pDumpBb->uStartX, uStartY,
1560 pDumpBb->cchWidth, pszErr, enmColor,
1561 enmColor);
1562 uStartY++;
1563 }
1564
1565 dbgcCmdUnassembleCfgDumpBbSpacing(hScreen, pDumpBb->uStartX, uStartY, pDumpBb->cchWidth, enmColor);
1566 uStartY++;
1567 dbgcCmdUnassembleCfgDumpBbBoundary(hScreen, pDumpBb->uStartX, uStartY, pDumpBb->cchWidth, enmColor);
1568 uStartY++;
1569}
1570
1571
1572/**
1573 * Dumps the given control flow graph to the output.
1574 *
1575 * @returns VBox status code.
1576 * @param hCfg The control flow graph handle.
1577 * @param fUseColor Flag whether the output should be colorized.
1578 * @param pCmdHlp The command helper callback table.
1579 */
1580static int dbgcCmdUnassembleCfgDump(DBGFFLOW hCfg, bool fUseColor, PDBGCCMDHLP pCmdHlp)
1581{
1582 DBGFFLOWIT hCfgIt;
1583 int rc = DBGFR3FlowItCreate(hCfg, DBGFFLOWITORDER_BY_ADDR_LOWEST_FIRST, &hCfgIt);
1584 if (RT_SUCCESS(rc))
1585 {
1586 uint32_t cBbs = DBGFR3FlowGetBbCount(hCfg);
1587 PDBGCFLOWBBDUMP paDumpBb = (PDBGCFLOWBBDUMP)RTMemTmpAllocZ(cBbs * sizeof(DBGCFLOWBBDUMP));
1588 if (paDumpBb)
1589 {
1590 /* Calculate the sizes of each basic block first. */
1591 DBGFFLOWBB hFlowBb = DBGFR3FlowItNext(hCfgIt);
1592 uint32_t idxDumpBb = 0;
1593 while (hFlowBb)
1594 {
1595 dbgcCmdUnassembleCfgDumpCalcBbSize(hFlowBb, &paDumpBb[idxDumpBb]);
1596 idxDumpBb++;
1597 hFlowBb = DBGFR3FlowItNext(hCfgIt);
1598 }
1599
1600 /* Calculate the ASCII screen dimensions and create one. */
1601 uint32_t cchWidth = 0;
1602 uint32_t cchLeftExtra = 5;
1603 uint32_t cchRightExtra = 5;
1604 uint32_t cchHeight = 0;
1605 for (unsigned i = 0; i < cBbs; i++)
1606 {
1607 PDBGCFLOWBBDUMP pDumpBb = &paDumpBb[i];
1608 cchWidth = RT_MAX(cchWidth, pDumpBb->cchWidth);
1609 cchHeight += pDumpBb->cchHeight;
1610
1611 /* Incomplete blocks don't have a successor. */
1612 if (DBGFR3FlowBbGetFlags(pDumpBb->hFlowBb) & DBGF_FLOW_BB_F_INCOMPLETE_ERR)
1613 continue;
1614
1615 switch (DBGFR3FlowBbGetType(pDumpBb->hFlowBb))
1616 {
1617 case DBGFFLOWBBENDTYPE_EXIT:
1618 case DBGFFLOWBBENDTYPE_LAST_DISASSEMBLED:
1619 break;
1620 case DBGFFLOWBBENDTYPE_UNCOND_JMP:
1621 if ( dbgcCmdUnassembleCfgAddrLower(&pDumpBb->AddrTarget, &pDumpBb->AddrStart)
1622 || dbgcCmdUnassembleCfgAddrEqual(&pDumpBb->AddrTarget, &pDumpBb->AddrStart))
1623 cchLeftExtra++;
1624 else
1625 cchRightExtra++;
1626 break;
1627 case DBGFFLOWBBENDTYPE_UNCOND:
1628 cchHeight += 2; /* For the arrow down to the next basic block. */
1629 break;
1630 case DBGFFLOWBBENDTYPE_COND:
1631 cchHeight += 2; /* For the arrow down to the next basic block. */
1632 if ( dbgcCmdUnassembleCfgAddrLower(&pDumpBb->AddrTarget, &pDumpBb->AddrStart)
1633 || dbgcCmdUnassembleCfgAddrEqual(&pDumpBb->AddrTarget, &pDumpBb->AddrStart))
1634 cchLeftExtra++;
1635 else
1636 cchRightExtra++;
1637 break;
1638 default:
1639 AssertFailed();
1640 }
1641 }
1642
1643 cchWidth += 2;
1644
1645 DBGCSCREEN hScreen = NULL;
1646 rc = dbgcScreenAsciiCreate(&hScreen, cchWidth + cchLeftExtra + cchRightExtra, cchHeight);
1647 if (RT_SUCCESS(rc))
1648 {
1649 uint32_t uY = 0;
1650
1651 /* Dump the basic blocks and connections to the immediate successor. */
1652 for (unsigned i = 0; i < cBbs; i++)
1653 {
1654 paDumpBb[i].uStartX = cchLeftExtra + (cchWidth - paDumpBb[i].cchWidth) / 2;
1655 paDumpBb[i].uStartY = uY;
1656 dbgcCmdUnassembleCfgDumpBb(&paDumpBb[i], hScreen);
1657 uY += paDumpBb[i].cchHeight;
1658
1659 /* Incomplete blocks don't have a successor. */
1660 if (DBGFR3FlowBbGetFlags(paDumpBb[i].hFlowBb) & DBGF_FLOW_BB_F_INCOMPLETE_ERR)
1661 continue;
1662
1663 switch (DBGFR3FlowBbGetType(paDumpBb[i].hFlowBb))
1664 {
1665 case DBGFFLOWBBENDTYPE_EXIT:
1666 case DBGFFLOWBBENDTYPE_LAST_DISASSEMBLED:
1667 case DBGFFLOWBBENDTYPE_UNCOND_JMP:
1668 break;
1669 case DBGFFLOWBBENDTYPE_UNCOND:
1670 /* Draw the arrow down to the next block. */
1671 dbgcScreenAsciiDrawCharacter(hScreen, cchLeftExtra + cchWidth / 2, uY,
1672 '|', DBGCSCREENCOLOR_BLUE_BRIGHT);
1673 uY++;
1674 dbgcScreenAsciiDrawCharacter(hScreen, cchLeftExtra + cchWidth / 2, uY,
1675 'V', DBGCSCREENCOLOR_BLUE_BRIGHT);
1676 uY++;
1677 break;
1678 case DBGFFLOWBBENDTYPE_COND:
1679 /* Draw the arrow down to the next block. */
1680 dbgcScreenAsciiDrawCharacter(hScreen, cchLeftExtra + cchWidth / 2, uY,
1681 '|', DBGCSCREENCOLOR_RED_BRIGHT);
1682 uY++;
1683 dbgcScreenAsciiDrawCharacter(hScreen, cchLeftExtra + cchWidth / 2, uY,
1684 'V', DBGCSCREENCOLOR_RED_BRIGHT);
1685 uY++;
1686 break;
1687 default:
1688 AssertFailed();
1689 }
1690 }
1691
1692 /* Last pass, connect all remaining branches. */
1693 uint32_t uBackConns = 0;
1694 uint32_t uFwdConns = 0;
1695 for (unsigned i = 0; i < cBbs; i++)
1696 {
1697 PDBGCFLOWBBDUMP pDumpBb = &paDumpBb[i];
1698 DBGFFLOWBBENDTYPE enmEndType = DBGFR3FlowBbGetType(pDumpBb->hFlowBb);
1699
1700 /* Incomplete blocks don't have a successor. */
1701 if (DBGFR3FlowBbGetFlags(pDumpBb->hFlowBb) & DBGF_FLOW_BB_F_INCOMPLETE_ERR)
1702 continue;
1703
1704 switch (enmEndType)
1705 {
1706 case DBGFFLOWBBENDTYPE_EXIT:
1707 case DBGFFLOWBBENDTYPE_LAST_DISASSEMBLED:
1708 case DBGFFLOWBBENDTYPE_UNCOND:
1709 break;
1710 case DBGFFLOWBBENDTYPE_COND:
1711 case DBGFFLOWBBENDTYPE_UNCOND_JMP:
1712 {
1713 /* Find the target first to get the coordinates. */
1714 PDBGCFLOWBBDUMP pDumpBbTgt = NULL;
1715 for (idxDumpBb = 0; idxDumpBb < cBbs; idxDumpBb++)
1716 {
1717 pDumpBbTgt = &paDumpBb[idxDumpBb];
1718 if (dbgcCmdUnassembleCfgAddrEqual(&pDumpBb->AddrTarget, &pDumpBbTgt->AddrStart))
1719 break;
1720 }
1721
1722 DBGCSCREENCOLOR enmColor = enmEndType == DBGFFLOWBBENDTYPE_UNCOND_JMP
1723 ? DBGCSCREENCOLOR_YELLOW_BRIGHT
1724 : DBGCSCREENCOLOR_GREEN_BRIGHT;
1725
1726 /*
1727 * Use the right side for targets with higher addresses,
1728 * left when jumping backwards.
1729 */
1730 if ( dbgcCmdUnassembleCfgAddrLower(&pDumpBb->AddrTarget, &pDumpBb->AddrStart)
1731 || dbgcCmdUnassembleCfgAddrEqual(&pDumpBb->AddrTarget, &pDumpBb->AddrStart))
1732 {
1733 /* Going backwards. */
1734 uint32_t uXVerLine = /*cchLeftExtra - 1 -*/ uBackConns + 1;
1735 uint32_t uYHorLine = pDumpBb->uStartY + pDumpBb->cchHeight - 1 - 2;
1736 uBackConns++;
1737
1738 /* Draw the arrow pointing to the target block. */
1739 dbgcScreenAsciiDrawCharacter(hScreen, pDumpBbTgt->uStartX - 1, pDumpBbTgt->uStartY,
1740 '>', enmColor);
1741 /* Draw the horizontal line. */
1742 dbgcScreenAsciiDrawLineHorizontal(hScreen, uXVerLine + 1, pDumpBbTgt->uStartX - 2,
1743 pDumpBbTgt->uStartY, '-', enmColor);
1744 dbgcScreenAsciiDrawCharacter(hScreen, uXVerLine, pDumpBbTgt->uStartY, '+',
1745 enmColor);
1746 /* Draw the vertical line down to the source block. */
1747 dbgcScreenAsciiDrawLineVertical(hScreen, uXVerLine, pDumpBbTgt->uStartY + 1, uYHorLine - 1,
1748 '|', enmColor);
1749 dbgcScreenAsciiDrawCharacter(hScreen, uXVerLine, uYHorLine, '+', enmColor);
1750 /* Draw the horizontal connection between the source block and vertical part. */
1751 dbgcScreenAsciiDrawLineHorizontal(hScreen, uXVerLine + 1, pDumpBb->uStartX - 1,
1752 uYHorLine, '-', enmColor);
1753
1754 }
1755 else
1756 {
1757 /* Going forward. */
1758 uint32_t uXVerLine = cchWidth + cchLeftExtra + (cchRightExtra - uFwdConns) - 1;
1759 uint32_t uYHorLine = pDumpBb->uStartY + pDumpBb->cchHeight - 1 - 2;
1760 uFwdConns++;
1761
1762 /* Draw the horizontal line. */
1763 dbgcScreenAsciiDrawLineHorizontal(hScreen, pDumpBb->uStartX + pDumpBb->cchWidth,
1764 uXVerLine - 1, uYHorLine, '-', enmColor);
1765 dbgcScreenAsciiDrawCharacter(hScreen, uXVerLine, uYHorLine, '+', enmColor);
1766 /* Draw the vertical line down to the target block. */
1767 dbgcScreenAsciiDrawLineVertical(hScreen, uXVerLine, uYHorLine + 1, pDumpBbTgt->uStartY - 1,
1768 '|', enmColor);
1769 /* Draw the horizontal connection between the target block and vertical part. */
1770 dbgcScreenAsciiDrawLineHorizontal(hScreen, pDumpBbTgt->uStartX + pDumpBbTgt->cchWidth,
1771 uXVerLine, pDumpBbTgt->uStartY, '-', enmColor);
1772 dbgcScreenAsciiDrawCharacter(hScreen, uXVerLine, pDumpBbTgt->uStartY, '+',
1773 enmColor);
1774 /* Draw the arrow pointing to the target block. */
1775 dbgcScreenAsciiDrawCharacter(hScreen, pDumpBbTgt->uStartX + pDumpBbTgt->cchWidth,
1776 pDumpBbTgt->uStartY, '<', enmColor);
1777 }
1778 break;
1779 }
1780 default:
1781 AssertFailed();
1782 }
1783 }
1784
1785 rc = dbgcScreenAsciiBlit(hScreen, dbgcCmdUnassembleCfgBlit, pCmdHlp, fUseColor);
1786 dbgcScreenAsciiDestroy(hScreen);
1787 }
1788
1789 for (unsigned i = 0; i < cBbs; i++)
1790 DBGFR3FlowBbRelease(paDumpBb[i].hFlowBb);
1791 RTMemTmpFree(paDumpBb);
1792 }
1793 else
1794 rc = VERR_NO_MEMORY;
1795
1796 DBGFR3FlowItDestroy(hCfgIt);
1797 }
1798
1799 return rc;
1800}
1801
1802
1803/**
1804 * @callback_method_impl{FNDBGCCMD, The 'ucfg' command.}
1805 */
1806static DECLCALLBACK(int) dbgcCmdUnassembleCfg(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
1807{
1808 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1809
1810 /*
1811 * Validate input.
1812 */
1813 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
1814 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, -1, cArgs <= 1);
1815 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs == 0 || DBGCVAR_ISPOINTER(paArgs[0].enmType));
1816
1817 if (!cArgs && !DBGCVAR_ISPOINTER(pDbgc->DisasmPos.enmType))
1818 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Don't know where to start disassembling");
1819
1820 /*
1821 * Check the desired mode.
1822 */
1823 unsigned fFlags = DBGF_DISAS_FLAGS_UNPATCHED_BYTES | DBGF_DISAS_FLAGS_ANNOTATE_PATCHED;
1824 bool fUseColor = false;
1825 switch (pCmd->pszCmd[4])
1826 {
1827 default: AssertFailed();
1828 case '\0': fFlags |= DBGF_DISAS_FLAGS_DEFAULT_MODE; break;
1829 case '6': fFlags |= DBGF_DISAS_FLAGS_64BIT_MODE; break;
1830 case '3': fFlags |= DBGF_DISAS_FLAGS_32BIT_MODE; break;
1831 case '1': fFlags |= DBGF_DISAS_FLAGS_16BIT_MODE; break;
1832 case 'v': fFlags |= DBGF_DISAS_FLAGS_16BIT_REAL_MODE; break;
1833 case 'c': fUseColor = true; break;
1834 }
1835
1836 /** @todo should use DBGFADDRESS for everything */
1837
1838 /*
1839 * Find address.
1840 */
1841 if (!cArgs)
1842 {
1843 if (!DBGCVAR_ISPOINTER(pDbgc->DisasmPos.enmType))
1844 {
1845 /** @todo Batch query CS, RIP, CPU mode and flags. */
1846 PVMCPU pVCpu = VMMR3GetCpuByIdU(pUVM, pDbgc->idCpu);
1847 if ( pDbgc->fRegCtxGuest
1848 && CPUMIsGuestIn64BitCode(pVCpu))
1849 {
1850 pDbgc->DisasmPos.enmType = DBGCVAR_TYPE_GC_FLAT;
1851 pDbgc->SourcePos.u.GCFlat = CPUMGetGuestRIP(pVCpu);
1852 }
1853 else
1854 {
1855 pDbgc->DisasmPos.enmType = DBGCVAR_TYPE_GC_FAR;
1856 pDbgc->SourcePos.u.GCFar.off = pDbgc->fRegCtxGuest ? CPUMGetGuestEIP(pVCpu) : CPUMGetHyperEIP(pVCpu);
1857 pDbgc->SourcePos.u.GCFar.sel = pDbgc->fRegCtxGuest ? CPUMGetGuestCS(pVCpu) : CPUMGetHyperCS(pVCpu);
1858 if ( (fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_DEFAULT_MODE
1859 && pDbgc->fRegCtxGuest
1860 && (CPUMGetGuestEFlags(pVCpu) & X86_EFL_VM))
1861 {
1862 fFlags &= ~DBGF_DISAS_FLAGS_MODE_MASK;
1863 fFlags |= DBGF_DISAS_FLAGS_16BIT_REAL_MODE;
1864 }
1865 }
1866
1867 if (pDbgc->fRegCtxGuest)
1868 fFlags |= DBGF_DISAS_FLAGS_CURRENT_GUEST;
1869 else
1870 fFlags |= DBGF_DISAS_FLAGS_CURRENT_HYPER | DBGF_DISAS_FLAGS_HYPER;
1871 }
1872 else if ((fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_DEFAULT_MODE && pDbgc->fDisasm)
1873 {
1874 fFlags &= ~DBGF_DISAS_FLAGS_MODE_MASK;
1875 fFlags |= pDbgc->fDisasm & (DBGF_DISAS_FLAGS_MODE_MASK | DBGF_DISAS_FLAGS_HYPER);
1876 }
1877 pDbgc->DisasmPos.enmRangeType = DBGCVAR_RANGE_NONE;
1878 }
1879 else
1880 pDbgc->DisasmPos = paArgs[0];
1881 pDbgc->pLastPos = &pDbgc->DisasmPos;
1882
1883 /*
1884 * Range.
1885 */
1886 switch (pDbgc->DisasmPos.enmRangeType)
1887 {
1888 case DBGCVAR_RANGE_NONE:
1889 pDbgc->DisasmPos.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
1890 pDbgc->DisasmPos.u64Range = 10;
1891 break;
1892
1893 case DBGCVAR_RANGE_ELEMENTS:
1894 if (pDbgc->DisasmPos.u64Range > 2048)
1895 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Too many lines requested. Max is 2048 lines");
1896 break;
1897
1898 case DBGCVAR_RANGE_BYTES:
1899 if (pDbgc->DisasmPos.u64Range > 65536)
1900 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The requested range is too big. Max is 64KB");
1901 break;
1902
1903 default:
1904 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Unknown range type %d", pDbgc->DisasmPos.enmRangeType);
1905 }
1906
1907 /*
1908 * Convert physical and host addresses to guest addresses.
1909 */
1910 RTDBGAS hDbgAs = pDbgc->hDbgAs;
1911 int rc;
1912 switch (pDbgc->DisasmPos.enmType)
1913 {
1914 case DBGCVAR_TYPE_GC_FLAT:
1915 case DBGCVAR_TYPE_GC_FAR:
1916 break;
1917 case DBGCVAR_TYPE_GC_PHYS:
1918 hDbgAs = DBGF_AS_PHYS;
1919 case DBGCVAR_TYPE_HC_FLAT:
1920 case DBGCVAR_TYPE_HC_PHYS:
1921 {
1922 DBGCVAR VarTmp;
1923 rc = DBGCCmdHlpEval(pCmdHlp, &VarTmp, "%%(%Dv)", &pDbgc->DisasmPos);
1924 if (RT_FAILURE(rc))
1925 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "failed to evaluate '%%(%Dv)'", &pDbgc->DisasmPos);
1926 pDbgc->DisasmPos = VarTmp;
1927 break;
1928 }
1929 default: AssertFailed(); break;
1930 }
1931
1932 DBGFADDRESS CurAddr;
1933 if ( (fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_16BIT_REAL_MODE
1934 && pDbgc->DisasmPos.enmType == DBGCVAR_TYPE_GC_FAR)
1935 DBGFR3AddrFromFlat(pUVM, &CurAddr, ((uint32_t)pDbgc->DisasmPos.u.GCFar.sel << 4) + pDbgc->DisasmPos.u.GCFar.off);
1936 else
1937 {
1938 rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &pDbgc->DisasmPos, &CurAddr);
1939 if (RT_FAILURE(rc))
1940 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr failed on '%Dv'", &pDbgc->DisasmPos);
1941 }
1942
1943 DBGFFLOW hCfg;
1944 rc = DBGFR3FlowCreate(pUVM, pDbgc->idCpu, &CurAddr, 0 /*cbDisasmMax*/, fFlags, &hCfg);
1945 if (RT_SUCCESS(rc))
1946 {
1947 /* Dump the graph. */
1948 rc = dbgcCmdUnassembleCfgDump(hCfg, fUseColor, pCmdHlp);
1949 DBGFR3FlowRelease(hCfg);
1950 }
1951 else
1952 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3FlowCreate failed on '%Dv'", &pDbgc->DisasmPos);
1953
1954 NOREF(pCmd);
1955 return rc;
1956}
1957
1958
1959/**
1960 * @callback_method_impl{FNDBGCCMD, The 'ls' command.}
1961 */
1962static DECLCALLBACK(int) dbgcCmdListSource(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
1963{
1964 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1965
1966 /*
1967 * Validate input.
1968 */
1969 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs <= 1);
1970 if (cArgs == 1)
1971 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, DBGCVAR_ISPOINTER(paArgs[0].enmType));
1972 if (!pUVM && !cArgs && !DBGCVAR_ISPOINTER(pDbgc->SourcePos.enmType))
1973 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Don't know where to start listing...");
1974 if (!pUVM && cArgs && DBGCVAR_ISGCPOINTER(paArgs[0].enmType))
1975 return DBGCCmdHlpFail(pCmdHlp, pCmd, "GC address but no VM");
1976
1977 /*
1978 * Find address.
1979 */
1980 if (!cArgs)
1981 {
1982 if (!DBGCVAR_ISPOINTER(pDbgc->SourcePos.enmType))
1983 {
1984 PVMCPU pVCpu = VMMR3GetCpuByIdU(pUVM, pDbgc->idCpu);
1985 pDbgc->SourcePos.enmType = DBGCVAR_TYPE_GC_FAR;
1986 pDbgc->SourcePos.u.GCFar.off = pDbgc->fRegCtxGuest ? CPUMGetGuestEIP(pVCpu) : CPUMGetHyperEIP(pVCpu);
1987 pDbgc->SourcePos.u.GCFar.sel = pDbgc->fRegCtxGuest ? CPUMGetGuestCS(pVCpu) : CPUMGetHyperCS(pVCpu);
1988 }
1989 pDbgc->SourcePos.enmRangeType = DBGCVAR_RANGE_NONE;
1990 }
1991 else
1992 pDbgc->SourcePos = paArgs[0];
1993 pDbgc->pLastPos = &pDbgc->SourcePos;
1994
1995 /*
1996 * Ensure the source address is flat GC.
1997 */
1998 switch (pDbgc->SourcePos.enmType)
1999 {
2000 case DBGCVAR_TYPE_GC_FLAT:
2001 break;
2002 case DBGCVAR_TYPE_GC_PHYS:
2003 case DBGCVAR_TYPE_GC_FAR:
2004 case DBGCVAR_TYPE_HC_FLAT:
2005 case DBGCVAR_TYPE_HC_PHYS:
2006 {
2007 int rc = DBGCCmdHlpEval(pCmdHlp, &pDbgc->SourcePos, "%%(%Dv)", &pDbgc->SourcePos);
2008 if (RT_FAILURE(rc))
2009 return DBGCCmdHlpPrintf(pCmdHlp, "error: Invalid address or address type. (rc=%d)\n", rc);
2010 break;
2011 }
2012 default: AssertFailed(); break;
2013 }
2014
2015 /*
2016 * Range.
2017 */
2018 switch (pDbgc->SourcePos.enmRangeType)
2019 {
2020 case DBGCVAR_RANGE_NONE:
2021 pDbgc->SourcePos.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
2022 pDbgc->SourcePos.u64Range = 10;
2023 break;
2024
2025 case DBGCVAR_RANGE_ELEMENTS:
2026 if (pDbgc->SourcePos.u64Range > 2048)
2027 return DBGCCmdHlpPrintf(pCmdHlp, "error: Too many lines requested. Max is 2048 lines.\n");
2028 break;
2029
2030 case DBGCVAR_RANGE_BYTES:
2031 if (pDbgc->SourcePos.u64Range > 65536)
2032 return DBGCCmdHlpPrintf(pCmdHlp, "error: The requested range is too big. Max is 64KB.\n");
2033 break;
2034
2035 default:
2036 return DBGCCmdHlpPrintf(pCmdHlp, "internal error: Unknown range type %d.\n", pDbgc->SourcePos.enmRangeType);
2037 }
2038
2039 /*
2040 * Do the disassembling.
2041 */
2042 bool fFirst = 1;
2043 RTDBGLINE LinePrev = { 0, 0, 0, 0, 0, "" };
2044 int iRangeLeft = (int)pDbgc->SourcePos.u64Range;
2045 if (iRangeLeft == 0) /* kludge for 'r'. */
2046 iRangeLeft = -1;
2047 for (;;)
2048 {
2049 /*
2050 * Get line info.
2051 */
2052 RTDBGLINE Line;
2053 RTGCINTPTR off;
2054 DBGFADDRESS SourcePosAddr;
2055 int rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &pDbgc->SourcePos, &SourcePosAddr);
2056 if (RT_FAILURE(rc))
2057 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr(,%Dv)", &pDbgc->SourcePos);
2058 rc = DBGFR3AsLineByAddr(pUVM, pDbgc->hDbgAs, &SourcePosAddr, &off, &Line, NULL);
2059 if (RT_FAILURE(rc))
2060 return VINF_SUCCESS;
2061
2062 unsigned cLines = 0;
2063 if (memcmp(&Line, &LinePrev, sizeof(Line)))
2064 {
2065 /*
2066 * Print filenamename
2067 */
2068 if (!fFirst && strcmp(Line.szFilename, LinePrev.szFilename))
2069 fFirst = true;
2070 if (fFirst)
2071 {
2072 rc = DBGCCmdHlpPrintf(pCmdHlp, "[%s @ %d]\n", Line.szFilename, Line.uLineNo);
2073 if (RT_FAILURE(rc))
2074 return rc;
2075 }
2076
2077 /*
2078 * Try open the file and read the line.
2079 */
2080 FILE *phFile = fopen(Line.szFilename, "r");
2081 if (phFile)
2082 {
2083 /* Skip ahead to the desired line. */
2084 char szLine[4096];
2085 unsigned cBefore = fFirst ? RT_MIN(2, Line.uLineNo - 1) : Line.uLineNo - LinePrev.uLineNo - 1;
2086 if (cBefore > 7)
2087 cBefore = 0;
2088 unsigned cLeft = Line.uLineNo - cBefore;
2089 while (cLeft > 0)
2090 {
2091 szLine[0] = '\0';
2092 if (!fgets(szLine, sizeof(szLine), phFile))
2093 break;
2094 cLeft--;
2095 }
2096 if (!cLeft)
2097 {
2098 /* print the before lines */
2099 for (;;)
2100 {
2101 size_t cch = strlen(szLine);
2102 while (cch > 0 && (szLine[cch - 1] == '\r' || szLine[cch - 1] == '\n' || RT_C_IS_SPACE(szLine[cch - 1])) )
2103 szLine[--cch] = '\0';
2104 if (cBefore-- <= 0)
2105 break;
2106
2107 rc = DBGCCmdHlpPrintf(pCmdHlp, " %4d: %s\n", Line.uLineNo - cBefore - 1, szLine);
2108 szLine[0] = '\0';
2109 const char *pszShutUpGcc = fgets(szLine, sizeof(szLine), phFile); NOREF(pszShutUpGcc);
2110 cLines++;
2111 }
2112 /* print the actual line */
2113 rc = DBGCCmdHlpPrintf(pCmdHlp, "%08llx %4d: %s\n", Line.Address, Line.uLineNo, szLine);
2114 }
2115 fclose(phFile);
2116 if (RT_FAILURE(rc))
2117 return rc;
2118 fFirst = false;
2119 }
2120 else
2121 return DBGCCmdHlpPrintf(pCmdHlp, "Warning: couldn't open source file '%s'\n", Line.szFilename);
2122
2123 LinePrev = Line;
2124 }
2125
2126
2127 /*
2128 * Advance
2129 */
2130 if (iRangeLeft < 0) /* 'r' */
2131 break;
2132 if (pDbgc->SourcePos.enmRangeType == DBGCVAR_RANGE_ELEMENTS)
2133 iRangeLeft -= cLines;
2134 else
2135 iRangeLeft -= 1;
2136 rc = DBGCCmdHlpEval(pCmdHlp, &pDbgc->SourcePos, "(%Dv) + %x", &pDbgc->SourcePos, 1);
2137 if (RT_FAILURE(rc))
2138 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Expression: (%Dv) + %x\n", &pDbgc->SourcePos, 1);
2139 if (iRangeLeft <= 0)
2140 break;
2141 }
2142
2143 NOREF(pCmd);
2144 return 0;
2145}
2146
2147
2148/**
2149 * @callback_method_impl{FNDBGCCMD, The 'r' command.}
2150 */
2151static DECLCALLBACK(int) dbgcCmdReg(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2152{
2153 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2154 if (!pDbgc->fRegCtxGuest)
2155 return dbgcCmdRegHyper(pCmd, pCmdHlp, pUVM, paArgs, cArgs);
2156 return dbgcCmdRegGuest(pCmd, pCmdHlp, pUVM, paArgs, cArgs);
2157}
2158
2159
2160/**
2161 * @callback_method_impl{FNDBGCCMD, Common worker for the dbgcCmdReg*()
2162 * commands.}
2163 */
2164static DECLCALLBACK(int) dbgcCmdRegCommon(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs,
2165 const char *pszPrefix)
2166{
2167 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2168 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs == 1 || cArgs == 2 || cArgs == 3);
2169 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, paArgs[0].enmType == DBGCVAR_TYPE_STRING
2170 || paArgs[0].enmType == DBGCVAR_TYPE_SYMBOL);
2171
2172 /*
2173 * Parse the register name and kind.
2174 */
2175 const char *pszReg = paArgs[0].u.pszString;
2176 if (*pszReg == '@')
2177 pszReg++;
2178 VMCPUID idCpu = pDbgc->idCpu;
2179 if (*pszPrefix)
2180 idCpu |= DBGFREG_HYPER_VMCPUID;
2181 if (*pszReg == '.')
2182 {
2183 pszReg++;
2184 idCpu |= DBGFREG_HYPER_VMCPUID;
2185 }
2186 const char * const pszActualPrefix = idCpu & DBGFREG_HYPER_VMCPUID ? "." : "";
2187
2188 /*
2189 * Query the register type & value (the setter needs the type).
2190 */
2191 DBGFREGVALTYPE enmType;
2192 DBGFREGVAL Value;
2193 int rc = DBGFR3RegNmQuery(pUVM, idCpu, pszReg, &Value, &enmType);
2194 if (RT_FAILURE(rc))
2195 {
2196 if (rc == VERR_DBGF_REGISTER_NOT_FOUND)
2197 return DBGCCmdHlpVBoxError(pCmdHlp, VERR_INVALID_PARAMETER, "Unknown register: '%s%s'.\n",
2198 pszActualPrefix, pszReg);
2199 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegNmQuery failed querying '%s%s': %Rrc.\n",
2200 pszActualPrefix, pszReg, rc);
2201 }
2202 if (cArgs == 1)
2203 {
2204 /*
2205 * Show the register.
2206 */
2207 char szValue[160];
2208 rc = DBGFR3RegFormatValue(szValue, sizeof(szValue), &Value, enmType, true /*fSpecial*/);
2209 if (RT_SUCCESS(rc))
2210 rc = DBGCCmdHlpPrintf(pCmdHlp, "%s%s=%s\n", pszActualPrefix, pszReg, szValue);
2211 else
2212 rc = DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegFormatValue failed: %Rrc.\n", rc);
2213 }
2214 else
2215 {
2216 DBGCVAR NewValueTmp;
2217 PCDBGCVAR pNewValue;
2218 if (cArgs == 3)
2219 {
2220 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 1, paArgs[1].enmType == DBGCVAR_TYPE_STRING);
2221 if (strcmp(paArgs[1].u.pszString, "="))
2222 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Second argument must be '='.");
2223 pNewValue = &paArgs[2];
2224 }
2225 else
2226 {
2227 /* Not possible to convince the parser to support both codeview and
2228 windbg syntax and make the equal sign optional. Try help it. */
2229 /** @todo make DBGCCmdHlpConvert do more with strings. */
2230 rc = DBGCCmdHlpConvert(pCmdHlp, &paArgs[1], DBGCVAR_TYPE_NUMBER, true /*fConvSyms*/, &NewValueTmp);
2231 if (RT_FAILURE(rc))
2232 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "The last argument must be a value or valid symbol.");
2233 pNewValue = &NewValueTmp;
2234 }
2235
2236 /*
2237 * Modify the register.
2238 */
2239 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 1, pNewValue->enmType == DBGCVAR_TYPE_NUMBER);
2240 if (enmType != DBGFREGVALTYPE_DTR)
2241 {
2242 enmType = DBGFREGVALTYPE_U64;
2243 rc = DBGCCmdHlpVarToNumber(pCmdHlp, pNewValue, &Value.u64);
2244 }
2245 else
2246 {
2247 enmType = DBGFREGVALTYPE_DTR;
2248 rc = DBGCCmdHlpVarToNumber(pCmdHlp, pNewValue, &Value.dtr.u64Base);
2249 if (RT_SUCCESS(rc) && pNewValue->enmRangeType != DBGCVAR_RANGE_NONE)
2250 Value.dtr.u32Limit = (uint32_t)pNewValue->u64Range;
2251 }
2252 if (RT_SUCCESS(rc))
2253 {
2254 rc = DBGFR3RegNmSet(pUVM, idCpu, pszReg, &Value, enmType);
2255 if (RT_FAILURE(rc))
2256 rc = DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegNmSet failed settings '%s%s': %Rrc\n",
2257 pszActualPrefix, pszReg, rc);
2258 if (rc != VINF_SUCCESS)
2259 DBGCCmdHlpPrintf(pCmdHlp, "%s: warning: %Rrc\n", pCmd->pszCmd, rc);
2260 }
2261 else
2262 rc = DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegFormatValue failed: %Rrc.\n", rc);
2263 }
2264 return rc;
2265}
2266
2267
2268/**
2269 * @callback_method_impl{FNDBGCCMD,
2270 * The 'rg'\, 'rg64' and 'rg32' commands\, worker for 'r'.}
2271 */
2272static DECLCALLBACK(int) dbgcCmdRegGuest(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2273{
2274 /*
2275 * Show all registers our selves.
2276 */
2277 if (cArgs == 0)
2278 {
2279 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2280 bool const f64BitMode = !strcmp(pCmd->pszCmd, "rg64")
2281 || ( strcmp(pCmd->pszCmd, "rg32") != 0
2282 && DBGFR3CpuIsIn64BitCode(pUVM, pDbgc->idCpu));
2283 char szDisAndRegs[8192];
2284 int rc;
2285
2286 if (pDbgc->fRegTerse)
2287 {
2288 if (f64BitMode)
2289 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu, &szDisAndRegs[0], sizeof(szDisAndRegs),
2290 "u %016VR{rip} L 0\n"
2291 "rax=%016VR{rax} rbx=%016VR{rbx} rcx=%016VR{rcx} rdx=%016VR{rdx}\n"
2292 "rsi=%016VR{rsi} rdi=%016VR{rdi} r8 =%016VR{r8} r9 =%016VR{r9}\n"
2293 "r10=%016VR{r10} r11=%016VR{r11} r12=%016VR{r12} r13=%016VR{r13}\n"
2294 "r14=%016VR{r14} r15=%016VR{r15} %VRF{rflags}\n"
2295 "rip=%016VR{rip} rsp=%016VR{rsp} rbp=%016VR{rbp}\n"
2296 "cs=%04VR{cs} ds=%04VR{ds} es=%04VR{es} fs=%04VR{fs} gs=%04VR{gs} ss=%04VR{ss} rflags=%08VR{rflags}\n");
2297 else
2298 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu, szDisAndRegs, sizeof(szDisAndRegs),
2299 "u %04VR{cs}:%08VR{eip} L 0\n"
2300 "eax=%08VR{eax} ebx=%08VR{ebx} ecx=%08VR{ecx} edx=%08VR{edx} esi=%08VR{esi} edi=%08VR{edi}\n"
2301 "eip=%08VR{eip} esp=%08VR{esp} ebp=%08VR{ebp} %VRF{eflags}\n"
2302 "cs=%04VR{cs} ds=%04VR{ds} es=%04VR{es} fs=%04VR{fs} gs=%04VR{gs} ss=%04VR{ss} eflags=%08VR{eflags}\n");
2303 }
2304 else
2305 {
2306 if (f64BitMode)
2307 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu, &szDisAndRegs[0], sizeof(szDisAndRegs),
2308 "u %016VR{rip} L 0\n"
2309 "rax=%016VR{rax} rbx=%016VR{rbx} rcx=%016VR{rcx} rdx=%016VR{rdx}\n"
2310 "rsi=%016VR{rsi} rdi=%016VR{rdi} r8 =%016VR{r8} r9 =%016VR{r9}\n"
2311 "r10=%016VR{r10} r11=%016VR{r11} r12=%016VR{r12} r13=%016VR{r13}\n"
2312 "r14=%016VR{r14} r15=%016VR{r15} %VRF{rflags}\n"
2313 "rip=%016VR{rip} rsp=%016VR{rsp} rbp=%016VR{rbp}\n"
2314 "cs={%04VR{cs} base=%016VR{cs_base} limit=%08VR{cs_lim} flags=%04VR{cs_attr}} cr0=%016VR{cr0}\n"
2315 "ds={%04VR{ds} base=%016VR{ds_base} limit=%08VR{ds_lim} flags=%04VR{ds_attr}} cr2=%016VR{cr2}\n"
2316 "es={%04VR{es} base=%016VR{es_base} limit=%08VR{es_lim} flags=%04VR{es_attr}} cr3=%016VR{cr3}\n"
2317 "fs={%04VR{fs} base=%016VR{fs_base} limit=%08VR{fs_lim} flags=%04VR{fs_attr}} cr4=%016VR{cr4}\n"
2318 "gs={%04VR{gs} base=%016VR{gs_base} limit=%08VR{gs_lim} flags=%04VR{gs_attr}} cr8=%016VR{cr8}\n"
2319 "ss={%04VR{ss} base=%016VR{ss_base} limit=%08VR{ss_lim} flags=%04VR{ss_attr}}\n"
2320 "dr0=%016VR{dr0} dr1=%016VR{dr1} dr2=%016VR{dr2} dr3=%016VR{dr3}\n"
2321 "dr6=%016VR{dr6} dr7=%016VR{dr7}\n"
2322 "gdtr=%016VR{gdtr_base}:%04VR{gdtr_lim} idtr=%016VR{idtr_base}:%04VR{idtr_lim} rflags=%08VR{rflags}\n"
2323 "ldtr={%04VR{ldtr} base=%016VR{ldtr_base} limit=%08VR{ldtr_lim} flags=%08VR{ldtr_attr}}\n"
2324 "tr ={%04VR{tr} base=%016VR{tr_base} limit=%08VR{tr_lim} flags=%08VR{tr_attr}}\n"
2325 " sysenter={cs=%04VR{sysenter_cs} eip=%08VR{sysenter_eip} esp=%08VR{sysenter_esp}}\n"
2326 " efer=%016VR{efer}\n"
2327 " pat=%016VR{pat}\n"
2328 " sf_mask=%016VR{sf_mask}\n"
2329 "krnl_gs_base=%016VR{krnl_gs_base}\n"
2330 " lstar=%016VR{lstar}\n"
2331 " star=%016VR{star} cstar=%016VR{cstar}\n"
2332 "fcw=%04VR{fcw} fsw=%04VR{fsw} ftw=%04VR{ftw} mxcsr=%04VR{mxcsr} mxcsr_mask=%04VR{mxcsr_mask}\n"
2333 );
2334 else
2335 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu, szDisAndRegs, sizeof(szDisAndRegs),
2336 "u %04VR{cs}:%08VR{eip} L 0\n"
2337 "eax=%08VR{eax} ebx=%08VR{ebx} ecx=%08VR{ecx} edx=%08VR{edx} esi=%08VR{esi} edi=%08VR{edi}\n"
2338 "eip=%08VR{eip} esp=%08VR{esp} ebp=%08VR{ebp} %VRF{eflags}\n"
2339 "cs={%04VR{cs} base=%08VR{cs_base} limit=%08VR{cs_lim} flags=%04VR{cs_attr}} dr0=%08VR{dr0} dr1=%08VR{dr1}\n"
2340 "ds={%04VR{ds} base=%08VR{ds_base} limit=%08VR{ds_lim} flags=%04VR{ds_attr}} dr2=%08VR{dr2} dr3=%08VR{dr3}\n"
2341 "es={%04VR{es} base=%08VR{es_base} limit=%08VR{es_lim} flags=%04VR{es_attr}} dr6=%08VR{dr6} dr7=%08VR{dr7}\n"
2342 "fs={%04VR{fs} base=%08VR{fs_base} limit=%08VR{fs_lim} flags=%04VR{fs_attr}} cr0=%08VR{cr0} cr2=%08VR{cr2}\n"
2343 "gs={%04VR{gs} base=%08VR{gs_base} limit=%08VR{gs_lim} flags=%04VR{gs_attr}} cr3=%08VR{cr3} cr4=%08VR{cr4}\n"
2344 "ss={%04VR{ss} base=%08VR{ss_base} limit=%08VR{ss_lim} flags=%04VR{ss_attr}} cr8=%08VR{cr8}\n"
2345 "gdtr=%08VR{gdtr_base}:%04VR{gdtr_lim} idtr=%08VR{idtr_base}:%04VR{idtr_lim} eflags=%08VR{eflags}\n"
2346 "ldtr={%04VR{ldtr} base=%08VR{ldtr_base} limit=%08VR{ldtr_lim} flags=%04VR{ldtr_attr}}\n"
2347 "tr ={%04VR{tr} base=%08VR{tr_base} limit=%08VR{tr_lim} flags=%04VR{tr_attr}}\n"
2348 "sysenter={cs=%04VR{sysenter_cs} eip=%08VR{sysenter_eip} esp=%08VR{sysenter_esp}}\n"
2349 "fcw=%04VR{fcw} fsw=%04VR{fsw} ftw=%04VR{ftw} mxcsr=%04VR{mxcsr} mxcsr_mask=%04VR{mxcsr_mask}\n"
2350 );
2351 }
2352 if (RT_FAILURE(rc))
2353 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegPrintf failed");
2354 char *pszRegs = strchr(szDisAndRegs, '\n');
2355 *pszRegs++ = '\0';
2356 rc = DBGCCmdHlpPrintf(pCmdHlp, "%s", pszRegs);
2357
2358 /*
2359 * Disassemble one instruction at cs:[r|e]ip.
2360 */
2361 if (!f64BitMode && strstr(pszRegs, " vm ")) /* a big ugly... */
2362 return pCmdHlp->pfnExec(pCmdHlp, "uv86 %s", szDisAndRegs + 2);
2363 return pCmdHlp->pfnExec(pCmdHlp, "%s", szDisAndRegs);
2364 }
2365 return dbgcCmdRegCommon(pCmd, pCmdHlp, pUVM, paArgs, cArgs, "");
2366}
2367
2368
2369/**
2370 * @callback_method_impl{FNDBGCCMD, The 'rh' command.}
2371 */
2372static DECLCALLBACK(int) dbgcCmdRegHyper(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2373{
2374 /*
2375 * Show all registers our selves.
2376 */
2377 if (cArgs == 0)
2378 {
2379 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2380 char szDisAndRegs[8192];
2381 int rc;
2382
2383 if (pDbgc->fRegTerse)
2384 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu | DBGFREG_HYPER_VMCPUID, szDisAndRegs, sizeof(szDisAndRegs),
2385 "u %VR{cs}:%VR{eip} L 0\n"
2386 ".eax=%08VR{eax} .ebx=%08VR{ebx} .ecx=%08VR{ecx} .edx=%08VR{edx} .esi=%08VR{esi} .edi=%08VR{edi}\n"
2387 ".eip=%08VR{eip} .esp=%08VR{esp} .ebp=%08VR{ebp} .%VRF{eflags}\n"
2388 ".cs=%04VR{cs} .ds=%04VR{ds} .es=%04VR{es} .fs=%04VR{fs} .gs=%04VR{gs} .ss=%04VR{ss} .eflags=%08VR{eflags}\n");
2389 else
2390 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu | DBGFREG_HYPER_VMCPUID, szDisAndRegs, sizeof(szDisAndRegs),
2391 "u %04VR{cs}:%08VR{eip} L 0\n"
2392 ".eax=%08VR{eax} .ebx=%08VR{ebx} .ecx=%08VR{ecx} .edx=%08VR{edx} .esi=%08VR{esi} .edi=%08VR{edi}\n"
2393 ".eip=%08VR{eip} .esp=%08VR{esp} .ebp=%08VR{ebp} .%VRF{eflags}\n"
2394 ".cs={%04VR{cs} base=%08VR{cs_base} limit=%08VR{cs_lim} flags=%04VR{cs_attr}} .dr0=%08VR{dr0} .dr1=%08VR{dr1}\n"
2395 ".ds={%04VR{ds} base=%08VR{ds_base} limit=%08VR{ds_lim} flags=%04VR{ds_attr}} .dr2=%08VR{dr2} .dr3=%08VR{dr3}\n"
2396 ".es={%04VR{es} base=%08VR{es_base} limit=%08VR{es_lim} flags=%04VR{es_attr}} .dr6=%08VR{dr6} .dr6=%08VR{dr6}\n"
2397 ".fs={%04VR{fs} base=%08VR{fs_base} limit=%08VR{fs_lim} flags=%04VR{fs_attr}} .cr3=%016VR{cr3}\n"
2398 ".gs={%04VR{gs} base=%08VR{gs_base} limit=%08VR{gs_lim} flags=%04VR{gs_attr}}\n"
2399 ".ss={%04VR{ss} base=%08VR{ss_base} limit=%08VR{ss_lim} flags=%04VR{ss_attr}}\n"
2400 ".gdtr=%08VR{gdtr_base}:%04VR{gdtr_lim} .idtr=%08VR{idtr_base}:%04VR{idtr_lim} .eflags=%08VR{eflags}\n"
2401 ".ldtr={%04VR{ldtr} base=%08VR{ldtr_base} limit=%08VR{ldtr_lim} flags=%04VR{ldtr_attr}}\n"
2402 ".tr ={%04VR{tr} base=%08VR{tr_base} limit=%08VR{tr_lim} flags=%04VR{tr_attr}}\n"
2403 );
2404 if (RT_FAILURE(rc))
2405 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegPrintf failed");
2406 char *pszRegs = strchr(szDisAndRegs, '\n');
2407 *pszRegs++ = '\0';
2408 rc = DBGCCmdHlpPrintf(pCmdHlp, "%s", pszRegs);
2409
2410 /*
2411 * Disassemble one instruction at cs:[r|e]ip.
2412 */
2413 return pCmdHlp->pfnExec(pCmdHlp, "%s", szDisAndRegs);
2414 }
2415 return dbgcCmdRegCommon(pCmd, pCmdHlp, pUVM, paArgs, cArgs, ".");
2416}
2417
2418
2419/**
2420 * @callback_method_impl{FNDBGCCMD, The 'rt' command.}
2421 */
2422static DECLCALLBACK(int) dbgcCmdRegTerse(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2423{
2424 NOREF(pCmd); NOREF(pUVM); NOREF(paArgs); NOREF(cArgs);
2425
2426 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2427 pDbgc->fRegTerse = !pDbgc->fRegTerse;
2428 return DBGCCmdHlpPrintf(pCmdHlp, pDbgc->fRegTerse ? "info: Terse register info.\n" : "info: Verbose register info.\n");
2429}
2430
2431
2432/**
2433 * @callback_method_impl{FNDBGCCMD, The 't' command.}
2434 */
2435static DECLCALLBACK(int) dbgcCmdTrace(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2436{
2437 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2438
2439 int rc = DBGFR3Step(pUVM, pDbgc->idCpu);
2440 if (RT_SUCCESS(rc))
2441 pDbgc->fReady = false;
2442 else
2443 rc = pDbgc->CmdHlp.pfnVBoxError(&pDbgc->CmdHlp, rc, "When trying to single step VM %p\n", pDbgc->pVM);
2444
2445 NOREF(pCmd); NOREF(paArgs); NOREF(cArgs);
2446 return rc;
2447}
2448
2449
2450/**
2451 * @callback_method_impl{FNDBGCCMD, The 'k'\, 'kg' and 'kh' commands.}
2452 */
2453static DECLCALLBACK(int) dbgcCmdStack(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2454{
2455 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2456
2457 /*
2458 * Figure which context we're called for and start walking that stack.
2459 */
2460 int rc;
2461 PCDBGFSTACKFRAME pFirstFrame;
2462 bool const fGuest = pCmd->pszCmd[1] == 'g'
2463 || (!pCmd->pszCmd[1] && pDbgc->fRegCtxGuest);
2464 rc = DBGFR3StackWalkBegin(pUVM, pDbgc->idCpu, fGuest ? DBGFCODETYPE_GUEST : DBGFCODETYPE_HYPER, &pFirstFrame);
2465 if (RT_FAILURE(rc))
2466 return DBGCCmdHlpPrintf(pCmdHlp, "Failed to begin stack walk, rc=%Rrc\n", rc);
2467
2468 /*
2469 * Print header.
2470 * 12345678 12345678 0023:87654321 12345678 87654321 12345678 87654321 symbol
2471 */
2472 uint32_t fBitFlags = 0;
2473 for (PCDBGFSTACKFRAME pFrame = pFirstFrame;
2474 pFrame;
2475 pFrame = DBGFR3StackWalkNext(pFrame))
2476 {
2477 uint32_t const fCurBitFlags = pFrame->fFlags & (DBGFSTACKFRAME_FLAGS_16BIT | DBGFSTACKFRAME_FLAGS_32BIT | DBGFSTACKFRAME_FLAGS_64BIT);
2478 if (fCurBitFlags & DBGFSTACKFRAME_FLAGS_16BIT)
2479 {
2480 if (fCurBitFlags != fBitFlags)
2481 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "SS:BP Ret SS:BP Ret CS:EIP Arg0 Arg1 Arg2 Arg3 CS:EIP / Symbol [line]\n");
2482 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04RX16:%04RX16 %04RX16:%04RX16 %04RX32:%08RX32 %08RX32 %08RX32 %08RX32 %08RX32",
2483 pFrame->AddrFrame.Sel,
2484 (uint16_t)pFrame->AddrFrame.off,
2485 pFrame->AddrReturnFrame.Sel,
2486 (uint16_t)pFrame->AddrReturnFrame.off,
2487 (uint32_t)pFrame->AddrReturnPC.Sel,
2488 (uint32_t)pFrame->AddrReturnPC.off,
2489 pFrame->Args.au32[0],
2490 pFrame->Args.au32[1],
2491 pFrame->Args.au32[2],
2492 pFrame->Args.au32[3]);
2493 }
2494 else if (fCurBitFlags & DBGFSTACKFRAME_FLAGS_32BIT)
2495 {
2496 if (fCurBitFlags != fBitFlags)
2497 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "EBP Ret EBP Ret CS:EIP Arg0 Arg1 Arg2 Arg3 CS:EIP / Symbol [line]\n");
2498 rc = DBGCCmdHlpPrintf(pCmdHlp, "%08RX32 %08RX32 %04RX32:%08RX32 %08RX32 %08RX32 %08RX32 %08RX32",
2499 (uint32_t)pFrame->AddrFrame.off,
2500 (uint32_t)pFrame->AddrReturnFrame.off,
2501 (uint32_t)pFrame->AddrReturnPC.Sel,
2502 (uint32_t)pFrame->AddrReturnPC.off,
2503 pFrame->Args.au32[0],
2504 pFrame->Args.au32[1],
2505 pFrame->Args.au32[2],
2506 pFrame->Args.au32[3]);
2507 }
2508 else if (fCurBitFlags & DBGFSTACKFRAME_FLAGS_64BIT)
2509 {
2510 if (fCurBitFlags != fBitFlags)
2511 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "RBP Ret SS:RBP Ret RIP CS:RIP / Symbol [line]\n");
2512 rc = DBGCCmdHlpPrintf(pCmdHlp, "%016RX64 %04RX16:%016RX64 %016RX64",
2513 (uint64_t)pFrame->AddrFrame.off,
2514 pFrame->AddrReturnFrame.Sel,
2515 (uint64_t)pFrame->AddrReturnFrame.off,
2516 (uint64_t)pFrame->AddrReturnPC.off);
2517 }
2518 if (RT_FAILURE(rc))
2519 break;
2520 if (!pFrame->pSymPC)
2521 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
2522 fCurBitFlags & DBGFSTACKFRAME_FLAGS_64BIT
2523 ? " %RTsel:%016RGv"
2524 : fCurBitFlags & DBGFSTACKFRAME_FLAGS_32BIT
2525 ? " %RTsel:%08RGv"
2526 : " %RTsel:%04RGv"
2527 , pFrame->AddrPC.Sel, pFrame->AddrPC.off);
2528 else
2529 {
2530 RTGCINTPTR offDisp = pFrame->AddrPC.FlatPtr - pFrame->pSymPC->Value; /** @todo this isn't 100% correct for segmented stuff. */
2531 if (offDisp > 0)
2532 rc = DBGCCmdHlpPrintf(pCmdHlp, " %s+%llx", pFrame->pSymPC->szName, (int64_t)offDisp);
2533 else if (offDisp < 0)
2534 rc = DBGCCmdHlpPrintf(pCmdHlp, " %s-%llx", pFrame->pSymPC->szName, -(int64_t)offDisp);
2535 else
2536 rc = DBGCCmdHlpPrintf(pCmdHlp, " %s", pFrame->pSymPC->szName);
2537 }
2538 if (RT_SUCCESS(rc) && pFrame->pLinePC)
2539 rc = DBGCCmdHlpPrintf(pCmdHlp, " [%s @ 0i%d]", pFrame->pLinePC->szFilename, pFrame->pLinePC->uLineNo);
2540 if (RT_SUCCESS(rc))
2541 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
2542 if (RT_FAILURE(rc))
2543 break;
2544
2545 fBitFlags = fCurBitFlags;
2546 }
2547
2548 DBGFR3StackWalkEnd(pFirstFrame);
2549
2550 NOREF(paArgs); NOREF(cArgs);
2551 return rc;
2552}
2553
2554
2555static int dbgcCmdDumpDTWorker64(PDBGCCMDHLP pCmdHlp, PCX86DESC64 pDesc, unsigned iEntry, bool fHyper, bool *pfDblEntry)
2556{
2557 /* GUEST64 */
2558 int rc;
2559
2560 const char *pszHyper = fHyper ? " HYPER" : "";
2561 const char *pszPresent = pDesc->Gen.u1Present ? "P " : "NP";
2562 if (pDesc->Gen.u1DescType)
2563 {
2564 static const char * const s_apszTypes[] =
2565 {
2566 "DataRO", /* 0 Read-Only */
2567 "DataRO", /* 1 Read-Only - Accessed */
2568 "DataRW", /* 2 Read/Write */
2569 "DataRW", /* 3 Read/Write - Accessed */
2570 "DownRO", /* 4 Expand-down, Read-Only */
2571 "DownRO", /* 5 Expand-down, Read-Only - Accessed */
2572 "DownRW", /* 6 Expand-down, Read/Write */
2573 "DownRW", /* 7 Expand-down, Read/Write - Accessed */
2574 "CodeEO", /* 8 Execute-Only */
2575 "CodeEO", /* 9 Execute-Only - Accessed */
2576 "CodeER", /* A Execute/Readable */
2577 "CodeER", /* B Execute/Readable - Accessed */
2578 "ConfE0", /* C Conforming, Execute-Only */
2579 "ConfE0", /* D Conforming, Execute-Only - Accessed */
2580 "ConfER", /* E Conforming, Execute/Readable */
2581 "ConfER" /* F Conforming, Execute/Readable - Accessed */
2582 };
2583 const char *pszAccessed = pDesc->Gen.u4Type & RT_BIT(0) ? "A " : "NA";
2584 const char *pszGranularity = pDesc->Gen.u1Granularity ? "G" : " ";
2585 const char *pszBig = pDesc->Gen.u1DefBig ? "BIG" : " ";
2586 uint32_t u32Base = X86DESC_BASE(pDesc);
2587 uint32_t cbLimit = X86DESC_LIMIT_G(pDesc);
2588
2589 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Bas=%08x Lim=%08x DPL=%d %s %s %s %s AVL=%d L=%d%s\n",
2590 iEntry, s_apszTypes[pDesc->Gen.u4Type], u32Base, cbLimit,
2591 pDesc->Gen.u2Dpl, pszPresent, pszAccessed, pszGranularity, pszBig,
2592 pDesc->Gen.u1Available, pDesc->Gen.u1Long, pszHyper);
2593 }
2594 else
2595 {
2596 static const char * const s_apszTypes[] =
2597 {
2598 "Ill-0 ", /* 0 0000 Reserved (Illegal) */
2599 "Ill-1 ", /* 1 0001 Available 16-bit TSS */
2600 "LDT ", /* 2 0010 LDT */
2601 "Ill-3 ", /* 3 0011 Busy 16-bit TSS */
2602 "Ill-4 ", /* 4 0100 16-bit Call Gate */
2603 "Ill-5 ", /* 5 0101 Task Gate */
2604 "Ill-6 ", /* 6 0110 16-bit Interrupt Gate */
2605 "Ill-7 ", /* 7 0111 16-bit Trap Gate */
2606 "Ill-8 ", /* 8 1000 Reserved (Illegal) */
2607 "Tss64A", /* 9 1001 Available 32-bit TSS */
2608 "Ill-A ", /* A 1010 Reserved (Illegal) */
2609 "Tss64B", /* B 1011 Busy 32-bit TSS */
2610 "Call64", /* C 1100 32-bit Call Gate */
2611 "Ill-D ", /* D 1101 Reserved (Illegal) */
2612 "Int64 ", /* E 1110 32-bit Interrupt Gate */
2613 "Trap64" /* F 1111 32-bit Trap Gate */
2614 };
2615 switch (pDesc->Gen.u4Type)
2616 {
2617 /* raw */
2618 case X86_SEL_TYPE_SYS_UNDEFINED:
2619 case X86_SEL_TYPE_SYS_UNDEFINED2:
2620 case X86_SEL_TYPE_SYS_UNDEFINED4:
2621 case X86_SEL_TYPE_SYS_UNDEFINED3:
2622 case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
2623 case X86_SEL_TYPE_SYS_286_TSS_BUSY:
2624 case X86_SEL_TYPE_SYS_286_CALL_GATE:
2625 case X86_SEL_TYPE_SYS_286_INT_GATE:
2626 case X86_SEL_TYPE_SYS_286_TRAP_GATE:
2627 case X86_SEL_TYPE_SYS_TASK_GATE:
2628 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s %.8Rhxs DPL=%d %s%s\n",
2629 iEntry, s_apszTypes[pDesc->Gen.u4Type], pDesc,
2630 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
2631 break;
2632
2633 case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
2634 case X86_SEL_TYPE_SYS_386_TSS_BUSY:
2635 case X86_SEL_TYPE_SYS_LDT:
2636 {
2637 const char *pszBusy = pDesc->Gen.u4Type & RT_BIT(1) ? "B " : "NB";
2638 const char *pszBig = pDesc->Gen.u1DefBig ? "BIG" : " ";
2639 const char *pszLong = pDesc->Gen.u1Long ? "LONG" : " ";
2640
2641 uint64_t u64Base = X86DESC64_BASE(pDesc);
2642 uint32_t cbLimit = X86DESC_LIMIT_G(pDesc);
2643
2644 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Bas=%016RX64 Lim=%08x DPL=%d %s %s %s %sAVL=%d R=%d%s\n",
2645 iEntry, s_apszTypes[pDesc->Gen.u4Type], u64Base, cbLimit,
2646 pDesc->Gen.u2Dpl, pszPresent, pszBusy, pszLong, pszBig,
2647 pDesc->Gen.u1Available, pDesc->Gen.u1Long | (pDesc->Gen.u1DefBig << 1),
2648 pszHyper);
2649 if (pfDblEntry)
2650 *pfDblEntry = true;
2651 break;
2652 }
2653
2654 case X86_SEL_TYPE_SYS_386_CALL_GATE:
2655 {
2656 unsigned cParams = pDesc->au8[4] & 0x1f;
2657 const char *pszCountOf = pDesc->Gen.u4Type & RT_BIT(3) ? "DC" : "WC";
2658 RTSEL sel = pDesc->au16[1];
2659 uint64_t off = pDesc->au16[0]
2660 | ((uint64_t)pDesc->au16[3] << 16)
2661 | ((uint64_t)pDesc->Gen.u32BaseHigh3 << 32);
2662 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Sel:Off=%04x:%016RX64 DPL=%d %s %s=%d%s\n",
2663 iEntry, s_apszTypes[pDesc->Gen.u4Type], sel, off,
2664 pDesc->Gen.u2Dpl, pszPresent, pszCountOf, cParams, pszHyper);
2665 if (pfDblEntry)
2666 *pfDblEntry = true;
2667 break;
2668 }
2669
2670 case X86_SEL_TYPE_SYS_386_INT_GATE:
2671 case X86_SEL_TYPE_SYS_386_TRAP_GATE:
2672 {
2673 RTSEL sel = pDesc->Gate.u16Sel;
2674 uint64_t off = pDesc->Gate.u16OffsetLow
2675 | ((uint64_t)pDesc->Gate.u16OffsetHigh << 16)
2676 | ((uint64_t)pDesc->Gate.u32OffsetTop << 32);
2677 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Sel:Off=%04x:%016RX64 DPL=%u %s IST=%u%s\n",
2678 iEntry, s_apszTypes[pDesc->Gate.u4Type], sel, off,
2679 pDesc->Gate.u2Dpl, pszPresent, pDesc->Gate.u3IST, pszHyper);
2680 if (pfDblEntry)
2681 *pfDblEntry = true;
2682 break;
2683 }
2684
2685 /* impossible, just it's necessary to keep gcc happy. */
2686 default:
2687 return VINF_SUCCESS;
2688 }
2689 }
2690 return VINF_SUCCESS;
2691}
2692
2693
2694/**
2695 * Worker function that displays one descriptor entry (GDT, LDT, IDT).
2696 *
2697 * @returns pfnPrintf status code.
2698 * @param pCmdHlp The DBGC command helpers.
2699 * @param pDesc The descriptor to display.
2700 * @param iEntry The descriptor entry number.
2701 * @param fHyper Whether the selector belongs to the hypervisor or not.
2702 */
2703static int dbgcCmdDumpDTWorker32(PDBGCCMDHLP pCmdHlp, PCX86DESC pDesc, unsigned iEntry, bool fHyper)
2704{
2705 int rc;
2706
2707 const char *pszHyper = fHyper ? " HYPER" : "";
2708 const char *pszPresent = pDesc->Gen.u1Present ? "P " : "NP";
2709 if (pDesc->Gen.u1DescType)
2710 {
2711 static const char * const s_apszTypes[] =
2712 {
2713 "DataRO", /* 0 Read-Only */
2714 "DataRO", /* 1 Read-Only - Accessed */
2715 "DataRW", /* 2 Read/Write */
2716 "DataRW", /* 3 Read/Write - Accessed */
2717 "DownRO", /* 4 Expand-down, Read-Only */
2718 "DownRO", /* 5 Expand-down, Read-Only - Accessed */
2719 "DownRW", /* 6 Expand-down, Read/Write */
2720 "DownRW", /* 7 Expand-down, Read/Write - Accessed */
2721 "CodeEO", /* 8 Execute-Only */
2722 "CodeEO", /* 9 Execute-Only - Accessed */
2723 "CodeER", /* A Execute/Readable */
2724 "CodeER", /* B Execute/Readable - Accessed */
2725 "ConfE0", /* C Conforming, Execute-Only */
2726 "ConfE0", /* D Conforming, Execute-Only - Accessed */
2727 "ConfER", /* E Conforming, Execute/Readable */
2728 "ConfER" /* F Conforming, Execute/Readable - Accessed */
2729 };
2730 const char *pszAccessed = pDesc->Gen.u4Type & RT_BIT(0) ? "A " : "NA";
2731 const char *pszGranularity = pDesc->Gen.u1Granularity ? "G" : " ";
2732 const char *pszBig = pDesc->Gen.u1DefBig ? "BIG" : " ";
2733 uint32_t u32Base = pDesc->Gen.u16BaseLow
2734 | ((uint32_t)pDesc->Gen.u8BaseHigh1 << 16)
2735 | ((uint32_t)pDesc->Gen.u8BaseHigh2 << 24);
2736 uint32_t cbLimit = pDesc->Gen.u16LimitLow | (pDesc->Gen.u4LimitHigh << 16);
2737 if (pDesc->Gen.u1Granularity)
2738 cbLimit <<= PAGE_SHIFT;
2739
2740 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Bas=%08x Lim=%08x DPL=%d %s %s %s %s AVL=%d L=%d%s\n",
2741 iEntry, s_apszTypes[pDesc->Gen.u4Type], u32Base, cbLimit,
2742 pDesc->Gen.u2Dpl, pszPresent, pszAccessed, pszGranularity, pszBig,
2743 pDesc->Gen.u1Available, pDesc->Gen.u1Long, pszHyper);
2744 }
2745 else
2746 {
2747 static const char * const s_apszTypes[] =
2748 {
2749 "Ill-0 ", /* 0 0000 Reserved (Illegal) */
2750 "Tss16A", /* 1 0001 Available 16-bit TSS */
2751 "LDT ", /* 2 0010 LDT */
2752 "Tss16B", /* 3 0011 Busy 16-bit TSS */
2753 "Call16", /* 4 0100 16-bit Call Gate */
2754 "TaskG ", /* 5 0101 Task Gate */
2755 "Int16 ", /* 6 0110 16-bit Interrupt Gate */
2756 "Trap16", /* 7 0111 16-bit Trap Gate */
2757 "Ill-8 ", /* 8 1000 Reserved (Illegal) */
2758 "Tss32A", /* 9 1001 Available 32-bit TSS */
2759 "Ill-A ", /* A 1010 Reserved (Illegal) */
2760 "Tss32B", /* B 1011 Busy 32-bit TSS */
2761 "Call32", /* C 1100 32-bit Call Gate */
2762 "Ill-D ", /* D 1101 Reserved (Illegal) */
2763 "Int32 ", /* E 1110 32-bit Interrupt Gate */
2764 "Trap32" /* F 1111 32-bit Trap Gate */
2765 };
2766 switch (pDesc->Gen.u4Type)
2767 {
2768 /* raw */
2769 case X86_SEL_TYPE_SYS_UNDEFINED:
2770 case X86_SEL_TYPE_SYS_UNDEFINED2:
2771 case X86_SEL_TYPE_SYS_UNDEFINED4:
2772 case X86_SEL_TYPE_SYS_UNDEFINED3:
2773 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s %.8Rhxs DPL=%d %s%s\n",
2774 iEntry, s_apszTypes[pDesc->Gen.u4Type], pDesc,
2775 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
2776 break;
2777
2778 case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
2779 case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
2780 case X86_SEL_TYPE_SYS_286_TSS_BUSY:
2781 case X86_SEL_TYPE_SYS_386_TSS_BUSY:
2782 case X86_SEL_TYPE_SYS_LDT:
2783 {
2784 const char *pszGranularity = pDesc->Gen.u1Granularity ? "G" : " ";
2785 const char *pszBusy = pDesc->Gen.u4Type & RT_BIT(1) ? "B " : "NB";
2786 const char *pszBig = pDesc->Gen.u1DefBig ? "BIG" : " ";
2787 uint32_t u32Base = pDesc->Gen.u16BaseLow
2788 | ((uint32_t)pDesc->Gen.u8BaseHigh1 << 16)
2789 | ((uint32_t)pDesc->Gen.u8BaseHigh2 << 24);
2790 uint32_t cbLimit = pDesc->Gen.u16LimitLow | (pDesc->Gen.u4LimitHigh << 16);
2791 if (pDesc->Gen.u1Granularity)
2792 cbLimit <<= PAGE_SHIFT;
2793
2794 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Bas=%08x Lim=%08x DPL=%d %s %s %s %s AVL=%d R=%d%s\n",
2795 iEntry, s_apszTypes[pDesc->Gen.u4Type], u32Base, cbLimit,
2796 pDesc->Gen.u2Dpl, pszPresent, pszBusy, pszGranularity, pszBig,
2797 pDesc->Gen.u1Available, pDesc->Gen.u1Long | (pDesc->Gen.u1DefBig << 1),
2798 pszHyper);
2799 break;
2800 }
2801
2802 case X86_SEL_TYPE_SYS_TASK_GATE:
2803 {
2804 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s TSS=%04x DPL=%d %s%s\n",
2805 iEntry, s_apszTypes[pDesc->Gen.u4Type], pDesc->au16[1],
2806 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
2807 break;
2808 }
2809
2810 case X86_SEL_TYPE_SYS_286_CALL_GATE:
2811 case X86_SEL_TYPE_SYS_386_CALL_GATE:
2812 {
2813 unsigned cParams = pDesc->au8[4] & 0x1f;
2814 const char *pszCountOf = pDesc->Gen.u4Type & RT_BIT(3) ? "DC" : "WC";
2815 RTSEL sel = pDesc->au16[1];
2816 uint32_t off = pDesc->au16[0] | ((uint32_t)pDesc->au16[3] << 16);
2817 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Sel:Off=%04x:%08x DPL=%d %s %s=%d%s\n",
2818 iEntry, s_apszTypes[pDesc->Gen.u4Type], sel, off,
2819 pDesc->Gen.u2Dpl, pszPresent, pszCountOf, cParams, pszHyper);
2820 break;
2821 }
2822
2823 case X86_SEL_TYPE_SYS_286_INT_GATE:
2824 case X86_SEL_TYPE_SYS_386_INT_GATE:
2825 case X86_SEL_TYPE_SYS_286_TRAP_GATE:
2826 case X86_SEL_TYPE_SYS_386_TRAP_GATE:
2827 {
2828 RTSEL sel = pDesc->au16[1];
2829 uint32_t off = pDesc->au16[0] | ((uint32_t)pDesc->au16[3] << 16);
2830 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Sel:Off=%04x:%08x DPL=%d %s%s\n",
2831 iEntry, s_apszTypes[pDesc->Gen.u4Type], sel, off,
2832 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
2833 break;
2834 }
2835
2836 /* impossible, just it's necessary to keep gcc happy. */
2837 default:
2838 return VINF_SUCCESS;
2839 }
2840 }
2841 return rc;
2842}
2843
2844
2845/**
2846 * @callback_method_impl{FNDBGCCMD, The 'dg'\, 'dga'\, 'dl' and 'dla' commands.}
2847 */
2848static DECLCALLBACK(int) dbgcCmdDumpDT(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2849{
2850 /*
2851 * Validate input.
2852 */
2853 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
2854
2855 /*
2856 * Get the CPU mode, check which command variation this is
2857 * and fix a default parameter if needed.
2858 */
2859 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2860 PVMCPU pVCpu = VMMR3GetCpuByIdU(pUVM, pDbgc->idCpu);
2861 CPUMMODE enmMode = CPUMGetGuestMode(pVCpu);
2862 bool fGdt = pCmd->pszCmd[1] == 'g';
2863 bool fAll = pCmd->pszCmd[2] == 'a';
2864 RTSEL SelTable = fGdt ? 0 : X86_SEL_LDT;
2865
2866 DBGCVAR Var;
2867 if (!cArgs)
2868 {
2869 cArgs = 1;
2870 paArgs = &Var;
2871 Var.enmType = DBGCVAR_TYPE_NUMBER;
2872 Var.u.u64Number = 0;
2873 Var.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
2874 Var.u64Range = 1024;
2875 }
2876
2877 /*
2878 * Process the arguments.
2879 */
2880 for (unsigned i = 0; i < cArgs; i++)
2881 {
2882 /*
2883 * Retrieve the selector value from the argument.
2884 * The parser may confuse pointers and numbers if more than one
2885 * argument is given, that that into account.
2886 */
2887 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, i, paArgs[i].enmType == DBGCVAR_TYPE_NUMBER || DBGCVAR_ISPOINTER(paArgs[i].enmType));
2888 uint64_t u64;
2889 unsigned cSels = 1;
2890 switch (paArgs[i].enmType)
2891 {
2892 case DBGCVAR_TYPE_NUMBER:
2893 u64 = paArgs[i].u.u64Number;
2894 if (paArgs[i].enmRangeType != DBGCVAR_RANGE_NONE)
2895 cSels = RT_MIN(paArgs[i].u64Range, 1024);
2896 break;
2897 case DBGCVAR_TYPE_GC_FAR: u64 = paArgs[i].u.GCFar.sel; break;
2898 case DBGCVAR_TYPE_GC_FLAT: u64 = paArgs[i].u.GCFlat; break;
2899 case DBGCVAR_TYPE_GC_PHYS: u64 = paArgs[i].u.GCPhys; break;
2900 case DBGCVAR_TYPE_HC_FLAT: u64 = (uintptr_t)paArgs[i].u.pvHCFlat; break;
2901 case DBGCVAR_TYPE_HC_PHYS: u64 = paArgs[i].u.HCPhys; break;
2902 default: u64 = _64K; break;
2903 }
2904 if (u64 < _64K)
2905 {
2906 unsigned Sel = (RTSEL)u64;
2907
2908 /*
2909 * Dump the specified range.
2910 */
2911 bool fSingle = cSels == 1;
2912 while ( cSels-- > 0
2913 && Sel < _64K)
2914 {
2915 DBGFSELINFO SelInfo;
2916 int rc = DBGFR3SelQueryInfo(pUVM, pDbgc->idCpu, Sel | SelTable, DBGFSELQI_FLAGS_DT_GUEST, &SelInfo);
2917 if (RT_SUCCESS(rc))
2918 {
2919 if (SelInfo.fFlags & DBGFSELINFO_FLAGS_REAL_MODE)
2920 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x RealM Bas=%04x Lim=%04x\n",
2921 Sel, (unsigned)SelInfo.GCPtrBase, (unsigned)SelInfo.cbLimit);
2922 else if ( fAll
2923 || fSingle
2924 || SelInfo.u.Raw.Gen.u1Present)
2925 {
2926 if (enmMode == CPUMMODE_PROTECTED)
2927 rc = dbgcCmdDumpDTWorker32(pCmdHlp, &SelInfo.u.Raw, Sel, !!(SelInfo.fFlags & DBGFSELINFO_FLAGS_HYPER));
2928 else
2929 {
2930 bool fDblSkip = false;
2931 rc = dbgcCmdDumpDTWorker64(pCmdHlp, &SelInfo.u.Raw64, Sel, !!(SelInfo.fFlags & DBGFSELINFO_FLAGS_HYPER), &fDblSkip);
2932 if (fDblSkip)
2933 Sel += 4;
2934 }
2935 }
2936 }
2937 else
2938 {
2939 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %Rrc\n", Sel, rc);
2940 if (!fAll)
2941 return rc;
2942 }
2943 if (RT_FAILURE(rc))
2944 return rc;
2945
2946 /* next */
2947 Sel += 8;
2948 }
2949 }
2950 else
2951 DBGCCmdHlpPrintf(pCmdHlp, "error: %llx is out of bounds\n", u64);
2952 }
2953
2954 return VINF_SUCCESS;
2955}
2956
2957
2958/**
2959 * @callback_method_impl{FNDBGCCMD, The 'di' and 'dia' commands.}
2960 */
2961static DECLCALLBACK(int) dbgcCmdDumpIDT(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2962{
2963 /*
2964 * Validate input.
2965 */
2966 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
2967
2968 /*
2969 * Establish some stuff like the current IDTR and CPU mode,
2970 * and fix a default parameter.
2971 */
2972 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2973 PVMCPU pVCpu = VMMR3GetCpuByIdU(pUVM, pDbgc->idCpu);
2974 uint16_t cbLimit;
2975 RTGCUINTPTR GCPtrBase = CPUMGetGuestIDTR(pVCpu, &cbLimit);
2976 CPUMMODE enmMode = CPUMGetGuestMode(pVCpu);
2977 unsigned cbEntry;
2978 switch (enmMode)
2979 {
2980 case CPUMMODE_REAL: cbEntry = sizeof(RTFAR16); break;
2981 case CPUMMODE_PROTECTED: cbEntry = sizeof(X86DESC); break;
2982 case CPUMMODE_LONG: cbEntry = sizeof(X86DESC64); break;
2983 default:
2984 return DBGCCmdHlpPrintf(pCmdHlp, "error: Invalid CPU mode %d.\n", enmMode);
2985 }
2986
2987 bool fAll = pCmd->pszCmd[2] == 'a';
2988 DBGCVAR Var;
2989 if (!cArgs)
2990 {
2991 cArgs = 1;
2992 paArgs = &Var;
2993 Var.enmType = DBGCVAR_TYPE_NUMBER;
2994 Var.u.u64Number = 0;
2995 Var.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
2996 Var.u64Range = 256;
2997 }
2998
2999 /*
3000 * Process the arguments.
3001 */
3002 for (unsigned i = 0; i < cArgs; i++)
3003 {
3004 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, i, paArgs[i].enmType == DBGCVAR_TYPE_NUMBER);
3005 if (paArgs[i].u.u64Number < 256)
3006 {
3007 RTGCUINTPTR iInt = (RTGCUINTPTR)paArgs[i].u.u64Number;
3008 unsigned cInts = paArgs[i].enmRangeType != DBGCVAR_RANGE_NONE
3009 ? paArgs[i].u64Range
3010 : 1;
3011 bool fSingle = cInts == 1;
3012 while ( cInts-- > 0
3013 && iInt < 256)
3014 {
3015 /*
3016 * Try read it.
3017 */
3018 union
3019 {
3020 RTFAR16 Real;
3021 X86DESC Prot;
3022 X86DESC64 Long;
3023 } u;
3024 if (iInt * cbEntry + (cbEntry - 1) > cbLimit)
3025 {
3026 DBGCCmdHlpPrintf(pCmdHlp, "%04x not within the IDT\n", (unsigned)iInt);
3027 if (!fAll && !fSingle)
3028 return VINF_SUCCESS;
3029 }
3030 DBGCVAR AddrVar;
3031 AddrVar.enmType = DBGCVAR_TYPE_GC_FLAT;
3032 AddrVar.u.GCFlat = GCPtrBase + iInt * cbEntry;
3033 AddrVar.enmRangeType = DBGCVAR_RANGE_NONE;
3034 int rc = pCmdHlp->pfnMemRead(pCmdHlp, &u, cbEntry, &AddrVar, NULL);
3035 if (RT_FAILURE(rc))
3036 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading IDT entry %#04x.\n", (unsigned)iInt);
3037
3038 /*
3039 * Display it.
3040 */
3041 switch (enmMode)
3042 {
3043 case CPUMMODE_REAL:
3044 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %RTfp16\n", (unsigned)iInt, u.Real);
3045 /** @todo resolve 16:16 IDTE to a symbol */
3046 break;
3047 case CPUMMODE_PROTECTED:
3048 if (fAll || fSingle || u.Prot.Gen.u1Present)
3049 rc = dbgcCmdDumpDTWorker32(pCmdHlp, &u.Prot, iInt, false);
3050 break;
3051 case CPUMMODE_LONG:
3052 if (fAll || fSingle || u.Long.Gen.u1Present)
3053 rc = dbgcCmdDumpDTWorker64(pCmdHlp, &u.Long, iInt, false, NULL);
3054 break;
3055 default: break; /* to shut up gcc */
3056 }
3057 if (RT_FAILURE(rc))
3058 return rc;
3059
3060 /* next */
3061 iInt++;
3062 }
3063 }
3064 else
3065 DBGCCmdHlpPrintf(pCmdHlp, "error: %llx is out of bounds (max 256)\n", paArgs[i].u.u64Number);
3066 }
3067
3068 return VINF_SUCCESS;
3069}
3070
3071
3072/**
3073 * @callback_method_impl{FNDBGCCMD,
3074 * The 'da'\, 'dq'\, 'dd'\, 'dw' and 'db' commands.}
3075 */
3076static DECLCALLBACK(int) dbgcCmdDumpMem(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3077{
3078 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3079
3080 /*
3081 * Validate input.
3082 */
3083 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs <= 1);
3084 if (cArgs == 1)
3085 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, DBGCVAR_ISPOINTER(paArgs[0].enmType));
3086 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
3087
3088 /*
3089 * Figure out the element size.
3090 */
3091 unsigned cbElement;
3092 bool fAscii = false;
3093 switch (pCmd->pszCmd[1])
3094 {
3095 default:
3096 case 'b': cbElement = 1; break;
3097 case 'w': cbElement = 2; break;
3098 case 'd': cbElement = 4; break;
3099 case 'q': cbElement = 8; break;
3100 case 'a':
3101 cbElement = 1;
3102 fAscii = true;
3103 break;
3104 case '\0':
3105 fAscii = !!(pDbgc->cbDumpElement & 0x80000000);
3106 cbElement = pDbgc->cbDumpElement & 0x7fffffff;
3107 if (!cbElement)
3108 cbElement = 1;
3109 break;
3110 }
3111
3112 /*
3113 * Find address.
3114 */
3115 if (!cArgs)
3116 pDbgc->DumpPos.enmRangeType = DBGCVAR_RANGE_NONE;
3117 else
3118 pDbgc->DumpPos = paArgs[0];
3119
3120 /*
3121 * Range.
3122 */
3123 switch (pDbgc->DumpPos.enmRangeType)
3124 {
3125 case DBGCVAR_RANGE_NONE:
3126 pDbgc->DumpPos.enmRangeType = DBGCVAR_RANGE_BYTES;
3127 pDbgc->DumpPos.u64Range = 0x60;
3128 break;
3129
3130 case DBGCVAR_RANGE_ELEMENTS:
3131 if (pDbgc->DumpPos.u64Range > 2048)
3132 return DBGCCmdHlpPrintf(pCmdHlp, "error: Too many elements requested. Max is 2048 elements.\n");
3133 pDbgc->DumpPos.enmRangeType = DBGCVAR_RANGE_BYTES;
3134 pDbgc->DumpPos.u64Range = (cbElement ? cbElement : 1) * pDbgc->DumpPos.u64Range;
3135 break;
3136
3137 case DBGCVAR_RANGE_BYTES:
3138 if (pDbgc->DumpPos.u64Range > 65536)
3139 return DBGCCmdHlpPrintf(pCmdHlp, "error: The requested range is too big. Max is 64KB.\n");
3140 break;
3141
3142 default:
3143 return DBGCCmdHlpPrintf(pCmdHlp, "internal error: Unknown range type %d.\n", pDbgc->DumpPos.enmRangeType);
3144 }
3145
3146 pDbgc->pLastPos = &pDbgc->DumpPos;
3147
3148 /*
3149 * Do the dumping.
3150 */
3151 pDbgc->cbDumpElement = cbElement | (fAscii << 31);
3152 int cbLeft = (int)pDbgc->DumpPos.u64Range;
3153 uint8_t u8Prev = '\0';
3154 for (;;)
3155 {
3156 /*
3157 * Read memory.
3158 */
3159 char achBuffer[16];
3160 size_t cbReq = RT_MIN((int)sizeof(achBuffer), cbLeft);
3161 size_t cb = RT_MIN((int)sizeof(achBuffer), cbLeft);
3162 int rc = pCmdHlp->pfnMemRead(pCmdHlp, &achBuffer, cbReq, &pDbgc->DumpPos, &cb);
3163 if (RT_FAILURE(rc))
3164 {
3165 if (u8Prev && u8Prev != '\n')
3166 DBGCCmdHlpPrintf(pCmdHlp, "\n");
3167 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading memory at %DV.\n", &pDbgc->DumpPos);
3168 }
3169
3170 /*
3171 * Display it.
3172 */
3173 memset(&achBuffer[cb], 0, sizeof(achBuffer) - cb);
3174 if (!fAscii)
3175 {
3176 DBGCCmdHlpPrintf(pCmdHlp, "%DV:", &pDbgc->DumpPos);
3177 unsigned i;
3178 for (i = 0; i < cb; i += cbElement)
3179 {
3180 const char *pszSpace = " ";
3181 if (cbElement <= 2 && i == 8 && !fAscii)
3182 pszSpace = "-";
3183 switch (cbElement)
3184 {
3185 case 1: DBGCCmdHlpPrintf(pCmdHlp, "%s%02x", pszSpace, *(uint8_t *)&achBuffer[i]); break;
3186 case 2: DBGCCmdHlpPrintf(pCmdHlp, "%s%04x", pszSpace, *(uint16_t *)&achBuffer[i]); break;
3187 case 4: DBGCCmdHlpPrintf(pCmdHlp, "%s%08x", pszSpace, *(uint32_t *)&achBuffer[i]); break;
3188 case 8: DBGCCmdHlpPrintf(pCmdHlp, "%s%016llx", pszSpace, *(uint64_t *)&achBuffer[i]); break;
3189 }
3190 }
3191
3192 /* chars column */
3193 if (pDbgc->cbDumpElement == 1)
3194 {
3195 while (i++ < sizeof(achBuffer))
3196 DBGCCmdHlpPrintf(pCmdHlp, " ");
3197 DBGCCmdHlpPrintf(pCmdHlp, " ");
3198 for (i = 0; i < cb; i += cbElement)
3199 {
3200 uint8_t u8 = *(uint8_t *)&achBuffer[i];
3201 if (RT_C_IS_PRINT(u8) && u8 < 127 && u8 >= 32)
3202 DBGCCmdHlpPrintf(pCmdHlp, "%c", u8);
3203 else
3204 DBGCCmdHlpPrintf(pCmdHlp, ".");
3205 }
3206 }
3207 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
3208 }
3209 else
3210 {
3211 /*
3212 * We print up to the first zero and stop there.
3213 * Only printables + '\t' and '\n' are printed.
3214 */
3215 if (!u8Prev)
3216 DBGCCmdHlpPrintf(pCmdHlp, "%DV:\n", &pDbgc->DumpPos);
3217 uint8_t u8 = '\0';
3218 unsigned i;
3219 for (i = 0; i < cb; i++)
3220 {
3221 u8Prev = u8;
3222 u8 = *(uint8_t *)&achBuffer[i];
3223 if ( u8 < 127
3224 && ( (RT_C_IS_PRINT(u8) && u8 >= 32)
3225 || u8 == '\t'
3226 || u8 == '\n'))
3227 DBGCCmdHlpPrintf(pCmdHlp, "%c", u8);
3228 else if (!u8)
3229 break;
3230 else
3231 DBGCCmdHlpPrintf(pCmdHlp, "\\x%x", u8);
3232 }
3233 if (u8 == '\0')
3234 cb = cbLeft = i + 1;
3235 if (cbLeft - cb <= 0 && u8Prev != '\n')
3236 DBGCCmdHlpPrintf(pCmdHlp, "\n");
3237 }
3238
3239 /*
3240 * Advance
3241 */
3242 cbLeft -= (int)cb;
3243 rc = DBGCCmdHlpEval(pCmdHlp, &pDbgc->DumpPos, "(%Dv) + %x", &pDbgc->DumpPos, cb);
3244 if (RT_FAILURE(rc))
3245 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Expression: (%Dv) + %x\n", &pDbgc->DumpPos, cb);
3246 if (cbLeft <= 0)
3247 break;
3248 }
3249
3250 NOREF(pCmd);
3251 return VINF_SUCCESS;
3252}
3253
3254
3255/**
3256 * Best guess at which paging mode currently applies to the guest
3257 * paging structures.
3258 *
3259 * This have to come up with a decent answer even when the guest
3260 * is in non-paged protected mode or real mode.
3261 *
3262 * @returns cr3.
3263 * @param pDbgc The DBGC instance.
3264 * @param pfPAE Where to store the page address extension indicator.
3265 * @param pfLME Where to store the long mode enabled indicator.
3266 * @param pfPSE Where to store the page size extension indicator.
3267 * @param pfPGE Where to store the page global enabled indicator.
3268 * @param pfNXE Where to store the no-execution enabled indicator.
3269 */
3270static RTGCPHYS dbgcGetGuestPageMode(PDBGC pDbgc, bool *pfPAE, bool *pfLME, bool *pfPSE, bool *pfPGE, bool *pfNXE)
3271{
3272 PVMCPU pVCpu = VMMR3GetCpuByIdU(pDbgc->pUVM, pDbgc->idCpu);
3273 RTGCUINTREG cr4 = CPUMGetGuestCR4(pVCpu);
3274 *pfPSE = !!(cr4 & X86_CR4_PSE);
3275 *pfPGE = !!(cr4 & X86_CR4_PGE);
3276 if (cr4 & X86_CR4_PAE)
3277 {
3278 *pfPSE = true;
3279 *pfPAE = true;
3280 }
3281 else
3282 *pfPAE = false;
3283
3284 *pfLME = CPUMGetGuestMode(pVCpu) == CPUMMODE_LONG;
3285 *pfNXE = false; /* GUEST64 GUESTNX */
3286 return CPUMGetGuestCR3(pVCpu);
3287}
3288
3289
3290/**
3291 * Determine the shadow paging mode.
3292 *
3293 * @returns cr3.
3294 * @param pDbgc The DBGC instance.
3295 * @param pfPAE Where to store the page address extension indicator.
3296 * @param pfLME Where to store the long mode enabled indicator.
3297 * @param pfPSE Where to store the page size extension indicator.
3298 * @param pfPGE Where to store the page global enabled indicator.
3299 * @param pfNXE Where to store the no-execution enabled indicator.
3300 */
3301static RTHCPHYS dbgcGetShadowPageMode(PDBGC pDbgc, bool *pfPAE, bool *pfLME, bool *pfPSE, bool *pfPGE, bool *pfNXE)
3302{
3303 PVMCPU pVCpu = VMMR3GetCpuByIdU(pDbgc->pUVM, pDbgc->idCpu);
3304
3305 *pfPSE = true;
3306 *pfPGE = false;
3307 switch (PGMGetShadowMode(pVCpu))
3308 {
3309 default:
3310 case PGMMODE_32_BIT:
3311 *pfPAE = *pfLME = *pfNXE = false;
3312 break;
3313 case PGMMODE_PAE:
3314 *pfLME = *pfNXE = false;
3315 *pfPAE = true;
3316 break;
3317 case PGMMODE_PAE_NX:
3318 *pfLME = false;
3319 *pfPAE = *pfNXE = true;
3320 break;
3321 case PGMMODE_AMD64:
3322 *pfNXE = false;
3323 *pfPAE = *pfLME = true;
3324 break;
3325 case PGMMODE_AMD64_NX:
3326 *pfPAE = *pfLME = *pfNXE = true;
3327 break;
3328 }
3329 return PGMGetHyperCR3(pVCpu);
3330}
3331
3332
3333/**
3334 * @callback_method_impl{FNDBGCCMD,
3335 * The 'dpd'\, 'dpda'\, 'dpdb'\, 'dpdg' and 'dpdh' commands.}
3336 */
3337static DECLCALLBACK(int) dbgcCmdDumpPageDir(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3338{
3339 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3340
3341 /*
3342 * Validate input.
3343 */
3344 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs <= 1);
3345 if (cArgs == 1 && pCmd->pszCmd[3] == 'a')
3346 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, DBGCVAR_ISPOINTER(paArgs[0].enmType));
3347 if (cArgs == 1 && pCmd->pszCmd[3] != 'a')
3348 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, paArgs[0].enmType == DBGCVAR_TYPE_NUMBER
3349 || DBGCVAR_ISPOINTER(paArgs[0].enmType));
3350 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
3351
3352 /*
3353 * Guest or shadow page directories? Get the paging parameters.
3354 */
3355 bool fGuest = pCmd->pszCmd[3] != 'h';
3356 if (!pCmd->pszCmd[3] || pCmd->pszCmd[3] == 'a')
3357 fGuest = paArgs[0].enmType == DBGCVAR_TYPE_NUMBER
3358 ? pDbgc->fRegCtxGuest
3359 : DBGCVAR_ISGCPOINTER(paArgs[0].enmType);
3360
3361 bool fPAE, fLME, fPSE, fPGE, fNXE;
3362 uint64_t cr3 = fGuest
3363 ? dbgcGetGuestPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE)
3364 : dbgcGetShadowPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE);
3365 const unsigned cbEntry = fPAE ? sizeof(X86PTEPAE) : sizeof(X86PTE);
3366
3367 /*
3368 * Setup default argument if none was specified.
3369 * Fix address / index confusion.
3370 */
3371 DBGCVAR VarDefault;
3372 if (!cArgs)
3373 {
3374 if (pCmd->pszCmd[3] == 'a')
3375 {
3376 if (fLME || fPAE)
3377 return DBGCCmdHlpPrintf(pCmdHlp, "Default argument for 'dpda' hasn't been fully implemented yet. Try with an address or use one of the other commands.\n");
3378 if (fGuest)
3379 DBGCVAR_INIT_GC_PHYS(&VarDefault, cr3);
3380 else
3381 DBGCVAR_INIT_HC_PHYS(&VarDefault, cr3);
3382 }
3383 else
3384 DBGCVAR_INIT_GC_FLAT(&VarDefault, 0);
3385 paArgs = &VarDefault;
3386 cArgs = 1;
3387 }
3388 else if (paArgs[0].enmType == DBGCVAR_TYPE_NUMBER)
3389 {
3390 /* If it's a number (not an address), it's an index, so convert it to an address. */
3391 Assert(pCmd->pszCmd[3] != 'a');
3392 VarDefault = paArgs[0];
3393 if (fPAE)
3394 return DBGCCmdHlpPrintf(pCmdHlp, "PDE indexing is only implemented for 32-bit paging.\n");
3395 if (VarDefault.u.u64Number >= PAGE_SIZE / cbEntry)
3396 return DBGCCmdHlpPrintf(pCmdHlp, "PDE index is out of range [0..%d].\n", PAGE_SIZE / cbEntry - 1);
3397 VarDefault.u.u64Number <<= X86_PD_SHIFT;
3398 VarDefault.enmType = DBGCVAR_TYPE_GC_FLAT;
3399 paArgs = &VarDefault;
3400 }
3401
3402 /*
3403 * Locate the PDE to start displaying at.
3404 *
3405 * The 'dpda' command takes the address of a PDE, while the others are guest
3406 * virtual address which PDEs should be displayed. So, 'dpda' is rather simple
3407 * while the others require us to do all the tedious walking thru the paging
3408 * hierarchy to find the intended PDE.
3409 */
3410 unsigned iEntry = ~0U; /* The page directory index. ~0U for 'dpta'. */
3411 DBGCVAR VarGCPtr = { NULL, }; /* The GC address corresponding to the current PDE (iEntry != ~0U). */
3412 DBGCVAR VarPDEAddr; /* The address of the current PDE. */
3413 unsigned cEntries; /* The number of entries to display. */
3414 unsigned cEntriesMax; /* The max number of entries to display. */
3415 int rc;
3416 if (pCmd->pszCmd[3] == 'a')
3417 {
3418 VarPDEAddr = paArgs[0];
3419 switch (VarPDEAddr.enmRangeType)
3420 {
3421 case DBGCVAR_RANGE_BYTES: cEntries = VarPDEAddr.u64Range / cbEntry; break;
3422 case DBGCVAR_RANGE_ELEMENTS: cEntries = VarPDEAddr.u64Range; break;
3423 default: cEntries = 10; break;
3424 }
3425 cEntriesMax = PAGE_SIZE / cbEntry;
3426 }
3427 else
3428 {
3429 /*
3430 * Determine the range.
3431 */
3432 switch (paArgs[0].enmRangeType)
3433 {
3434 case DBGCVAR_RANGE_BYTES: cEntries = paArgs[0].u64Range / PAGE_SIZE; break;
3435 case DBGCVAR_RANGE_ELEMENTS: cEntries = paArgs[0].u64Range; break;
3436 default: cEntries = 10; break;
3437 }
3438
3439 /*
3440 * Normalize the input address, it must be a flat GC address.
3441 */
3442 rc = DBGCCmdHlpEval(pCmdHlp, &VarGCPtr, "%%(%Dv)", &paArgs[0]);
3443 if (RT_FAILURE(rc))
3444 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "%%(%Dv)", &paArgs[0]);
3445 if (VarGCPtr.enmType == DBGCVAR_TYPE_HC_FLAT)
3446 {
3447 VarGCPtr.u.GCFlat = (uintptr_t)VarGCPtr.u.pvHCFlat;
3448 VarGCPtr.enmType = DBGCVAR_TYPE_GC_FLAT;
3449 }
3450 if (fPAE)
3451 VarGCPtr.u.GCFlat &= ~(((RTGCPTR)1 << X86_PD_PAE_SHIFT) - 1);
3452 else
3453 VarGCPtr.u.GCFlat &= ~(((RTGCPTR)1 << X86_PD_SHIFT) - 1);
3454
3455 /*
3456 * Do the paging walk until we get to the page directory.
3457 */
3458 DBGCVAR VarCur;
3459 if (fGuest)
3460 DBGCVAR_INIT_GC_PHYS(&VarCur, cr3);
3461 else
3462 DBGCVAR_INIT_HC_PHYS(&VarCur, cr3);
3463 if (fLME)
3464 {
3465 /* Page Map Level 4 Lookup. */
3466 /* Check if it's a valid address first? */
3467 VarCur.u.u64Number &= X86_PTE_PAE_PG_MASK;
3468 VarCur.u.u64Number += (((uint64_t)VarGCPtr.u.GCFlat >> X86_PML4_SHIFT) & X86_PML4_MASK) * sizeof(X86PML4E);
3469 X86PML4E Pml4e;
3470 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pml4e, sizeof(Pml4e), &VarCur, NULL);
3471 if (RT_FAILURE(rc))
3472 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PML4E memory at %DV.\n", &VarCur);
3473 if (!Pml4e.n.u1Present)
3474 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory pointer table is not present for %Dv.\n", &VarGCPtr);
3475
3476 VarCur.u.u64Number = Pml4e.u & X86_PML4E_PG_MASK;
3477 Assert(fPAE);
3478 }
3479 if (fPAE)
3480 {
3481 /* Page directory pointer table. */
3482 X86PDPE Pdpe;
3483 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE) * sizeof(Pdpe);
3484 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pdpe, sizeof(Pdpe), &VarCur, NULL);
3485 if (RT_FAILURE(rc))
3486 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDPE memory at %DV.\n", &VarCur);
3487 if (!Pdpe.n.u1Present)
3488 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory is not present for %Dv.\n", &VarGCPtr);
3489
3490 iEntry = (VarGCPtr.u.GCFlat >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
3491 VarPDEAddr = VarCur;
3492 VarPDEAddr.u.u64Number = Pdpe.u & X86_PDPE_PG_MASK;
3493 VarPDEAddr.u.u64Number += iEntry * sizeof(X86PDEPAE);
3494 }
3495 else
3496 {
3497 /* 32-bit legacy - CR3 == page directory. */
3498 iEntry = (VarGCPtr.u.GCFlat >> X86_PD_SHIFT) & X86_PD_MASK;
3499 VarPDEAddr = VarCur;
3500 VarPDEAddr.u.u64Number += iEntry * sizeof(X86PDE);
3501 }
3502 cEntriesMax = (PAGE_SIZE - iEntry) / cbEntry;
3503 }
3504
3505 /* adjust cEntries */
3506 cEntries = RT_MAX(1, cEntries);
3507 cEntries = RT_MIN(cEntries, cEntriesMax);
3508
3509 /*
3510 * The display loop.
3511 */
3512 DBGCCmdHlpPrintf(pCmdHlp, iEntry != ~0U ? "%DV (index %#x):\n" : "%DV:\n",
3513 &VarPDEAddr, iEntry);
3514 do
3515 {
3516 /*
3517 * Read.
3518 */
3519 X86PDEPAE Pde;
3520 Pde.u = 0;
3521 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pde, cbEntry, &VarPDEAddr, NULL);
3522 if (RT_FAILURE(rc))
3523 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading PDE memory at %DV.\n", &VarPDEAddr);
3524
3525 /*
3526 * Display.
3527 */
3528 if (iEntry != ~0U)
3529 {
3530 DBGCCmdHlpPrintf(pCmdHlp, "%03x %DV: ", iEntry, &VarGCPtr);
3531 iEntry++;
3532 }
3533 if (fPSE && Pde.b.u1Size)
3534 DBGCCmdHlpPrintf(pCmdHlp,
3535 fPAE
3536 ? "%016llx big phys=%016llx %s %s %s %s %s avl=%02x %s %s %s %s %s"
3537 : "%08llx big phys=%08llx %s %s %s %s %s avl=%02x %s %s %s %s %s",
3538 Pde.u,
3539 Pde.u & X86_PDE_PAE_PG_MASK,
3540 Pde.b.u1Present ? "p " : "np",
3541 Pde.b.u1Write ? "w" : "r",
3542 Pde.b.u1User ? "u" : "s",
3543 Pde.b.u1Accessed ? "a " : "na",
3544 Pde.b.u1Dirty ? "d " : "nd",
3545 Pde.b.u3Available,
3546 Pde.b.u1Global ? (fPGE ? "g" : "G") : " ",
3547 Pde.b.u1WriteThru ? "pwt" : " ",
3548 Pde.b.u1CacheDisable ? "pcd" : " ",
3549 Pde.b.u1PAT ? "pat" : "",
3550 Pde.b.u1NoExecute ? (fNXE ? "nx" : "NX") : " ");
3551 else
3552 DBGCCmdHlpPrintf(pCmdHlp,
3553 fPAE
3554 ? "%016llx 4kb phys=%016llx %s %s %s %s %s avl=%02x %s %s %s %s"
3555 : "%08llx 4kb phys=%08llx %s %s %s %s %s avl=%02x %s %s %s %s",
3556 Pde.u,
3557 Pde.u & X86_PDE_PAE_PG_MASK,
3558 Pde.n.u1Present ? "p " : "np",
3559 Pde.n.u1Write ? "w" : "r",
3560 Pde.n.u1User ? "u" : "s",
3561 Pde.n.u1Accessed ? "a " : "na",
3562 Pde.u & RT_BIT(6) ? "6 " : " ",
3563 Pde.n.u3Available,
3564 Pde.u & RT_BIT(8) ? "8" : " ",
3565 Pde.n.u1WriteThru ? "pwt" : " ",
3566 Pde.n.u1CacheDisable ? "pcd" : " ",
3567 Pde.u & RT_BIT(7) ? "7" : "",
3568 Pde.n.u1NoExecute ? (fNXE ? "nx" : "NX") : " ");
3569 if (Pde.u & UINT64_C(0x7fff000000000000))
3570 DBGCCmdHlpPrintf(pCmdHlp, " weird=%RX64", (Pde.u & UINT64_C(0x7fff000000000000)));
3571 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
3572 if (RT_FAILURE(rc))
3573 return rc;
3574
3575 /*
3576 * Advance.
3577 */
3578 VarPDEAddr.u.u64Number += cbEntry;
3579 if (iEntry != ~0U)
3580 VarGCPtr.u.GCFlat += fPAE ? RT_BIT_32(X86_PD_PAE_SHIFT) : RT_BIT_32(X86_PD_SHIFT);
3581 } while (cEntries-- > 0);
3582
3583 return VINF_SUCCESS;
3584}
3585
3586
3587/**
3588 * @callback_method_impl{FNDBGCCMD, The 'dpdb' command.}
3589 */
3590static DECLCALLBACK(int) dbgcCmdDumpPageDirBoth(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3591{
3592 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
3593 int rc1 = pCmdHlp->pfnExec(pCmdHlp, "dpdg %DV", &paArgs[0]);
3594 int rc2 = pCmdHlp->pfnExec(pCmdHlp, "dpdh %DV", &paArgs[0]);
3595 if (RT_FAILURE(rc1))
3596 return rc1;
3597 NOREF(pCmd); NOREF(paArgs); NOREF(cArgs);
3598 return rc2;
3599}
3600
3601
3602/**
3603 * @callback_method_impl{FNDBGCCMD, The 'dph*' commands and main part of 'm'.}
3604 */
3605static DECLCALLBACK(int) dbgcCmdDumpPageHierarchy(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3606{
3607 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3608 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
3609
3610 /*
3611 * Figure the context and base flags.
3612 */
3613 uint32_t fFlags = DBGFPGDMP_FLAGS_PAGE_INFO | DBGFPGDMP_FLAGS_PRINT_CR3;
3614 if (pCmd->pszCmd[0] == 'm')
3615 fFlags |= DBGFPGDMP_FLAGS_GUEST | DBGFPGDMP_FLAGS_SHADOW;
3616 else if (pCmd->pszCmd[3] == '\0')
3617 fFlags |= pDbgc->fRegCtxGuest ? DBGFPGDMP_FLAGS_GUEST : DBGFPGDMP_FLAGS_SHADOW;
3618 else if (pCmd->pszCmd[3] == 'g')
3619 fFlags |= DBGFPGDMP_FLAGS_GUEST;
3620 else if (pCmd->pszCmd[3] == 'h')
3621 fFlags |= DBGFPGDMP_FLAGS_SHADOW;
3622 else
3623 AssertFailed();
3624
3625 if (pDbgc->cPagingHierarchyDumps == 0)
3626 fFlags |= DBGFPGDMP_FLAGS_HEADER;
3627 pDbgc->cPagingHierarchyDumps = (pDbgc->cPagingHierarchyDumps + 1) % 42;
3628
3629 /*
3630 * Get the range.
3631 */
3632 PCDBGCVAR pRange = cArgs > 0 ? &paArgs[0] : pDbgc->pLastPos;
3633 RTGCPTR GCPtrFirst = NIL_RTGCPTR;
3634 int rc = DBGCCmdHlpVarToFlatAddr(pCmdHlp, pRange, &GCPtrFirst);
3635 if (RT_FAILURE(rc))
3636 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Failed to convert %DV to a flat address: %Rrc", pRange, rc);
3637
3638 uint64_t cbRange;
3639 rc = DBGCCmdHlpVarGetRange(pCmdHlp, pRange, PAGE_SIZE, PAGE_SIZE * 8, &cbRange);
3640 if (RT_FAILURE(rc))
3641 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Failed to obtain the range of %DV: %Rrc", pRange, rc);
3642
3643 RTGCPTR GCPtrLast = RTGCPTR_MAX - GCPtrFirst;
3644 if (cbRange >= GCPtrLast)
3645 GCPtrLast = RTGCPTR_MAX;
3646 else if (!cbRange)
3647 GCPtrLast = GCPtrFirst;
3648 else
3649 GCPtrLast = GCPtrFirst + cbRange - 1;
3650
3651 /*
3652 * Do we have a CR3?
3653 */
3654 uint64_t cr3 = 0;
3655 if (cArgs > 1)
3656 {
3657 if ((fFlags & (DBGFPGDMP_FLAGS_GUEST | DBGFPGDMP_FLAGS_SHADOW)) == (DBGFPGDMP_FLAGS_GUEST | DBGFPGDMP_FLAGS_SHADOW))
3658 return DBGCCmdHlpFail(pCmdHlp, pCmd, "No CR3 or mode arguments when dumping both context, please.");
3659 if (paArgs[1].enmType != DBGCVAR_TYPE_NUMBER)
3660 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The CR3 argument is not a number: %DV", &paArgs[1]);
3661 cr3 = paArgs[1].u.u64Number;
3662 }
3663 else
3664 fFlags |= DBGFPGDMP_FLAGS_CURRENT_CR3;
3665
3666 /*
3667 * Do we have a mode?
3668 */
3669 if (cArgs > 2)
3670 {
3671 if (paArgs[2].enmType != DBGCVAR_TYPE_STRING)
3672 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The mode argument is not a string: %DV", &paArgs[2]);
3673 static const struct MODETOFLAGS
3674 {
3675 const char *pszName;
3676 uint32_t fFlags;
3677 } s_aModeToFlags[] =
3678 {
3679 { "ept", DBGFPGDMP_FLAGS_EPT },
3680 { "legacy", 0 },
3681 { "legacy-np", DBGFPGDMP_FLAGS_NP },
3682 { "pse", DBGFPGDMP_FLAGS_PSE },
3683 { "pse-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_NP },
3684 { "pae", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE },
3685 { "pae-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_NP },
3686 { "pae-nx", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_NXE },
3687 { "pae-nx-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_NXE | DBGFPGDMP_FLAGS_NP },
3688 { "long", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_LME },
3689 { "long-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_LME | DBGFPGDMP_FLAGS_NP },
3690 { "long-nx", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_LME | DBGFPGDMP_FLAGS_NXE },
3691 { "long-nx-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_LME | DBGFPGDMP_FLAGS_NXE | DBGFPGDMP_FLAGS_NP }
3692 };
3693 int i = RT_ELEMENTS(s_aModeToFlags);
3694 while (i-- > 0)
3695 if (!strcmp(s_aModeToFlags[i].pszName, paArgs[2].u.pszString))
3696 {
3697 fFlags |= s_aModeToFlags[i].fFlags;
3698 break;
3699 }
3700 if (i < 0)
3701 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Unknown mode: \"%s\"", paArgs[2].u.pszString);
3702 }
3703 else
3704 fFlags |= DBGFPGDMP_FLAGS_CURRENT_MODE;
3705
3706 /*
3707 * Call the worker.
3708 */
3709 rc = DBGFR3PagingDumpEx(pUVM, pDbgc->idCpu, fFlags, cr3, GCPtrFirst, GCPtrLast, 99 /*cMaxDepth*/,
3710 DBGCCmdHlpGetDbgfOutputHlp(pCmdHlp));
3711 if (RT_FAILURE(rc))
3712 return DBGCCmdHlpFail(pCmdHlp, pCmd, "DBGFR3PagingDumpEx: %Rrc\n", rc);
3713 return VINF_SUCCESS;
3714}
3715
3716
3717
3718/**
3719 * @callback_method_impl{FNDBGCCMD, The 'dpg*' commands.}
3720 */
3721static DECLCALLBACK(int) dbgcCmdDumpPageTable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3722{
3723 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3724
3725 /*
3726 * Validate input.
3727 */
3728 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs == 1);
3729 if (pCmd->pszCmd[3] == 'a')
3730 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, DBGCVAR_ISPOINTER(paArgs[0].enmType));
3731 else
3732 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, paArgs[0].enmType == DBGCVAR_TYPE_NUMBER
3733 || DBGCVAR_ISPOINTER(paArgs[0].enmType));
3734 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
3735
3736 /*
3737 * Guest or shadow page tables? Get the paging parameters.
3738 */
3739 bool fGuest = pCmd->pszCmd[3] != 'h';
3740 if (!pCmd->pszCmd[3] || pCmd->pszCmd[3] == 'a')
3741 fGuest = paArgs[0].enmType == DBGCVAR_TYPE_NUMBER
3742 ? pDbgc->fRegCtxGuest
3743 : DBGCVAR_ISGCPOINTER(paArgs[0].enmType);
3744
3745 bool fPAE, fLME, fPSE, fPGE, fNXE;
3746 uint64_t cr3 = fGuest
3747 ? dbgcGetGuestPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE)
3748 : dbgcGetShadowPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE);
3749 const unsigned cbEntry = fPAE ? sizeof(X86PTEPAE) : sizeof(X86PTE);
3750
3751 /*
3752 * Locate the PTE to start displaying at.
3753 *
3754 * The 'dpta' command takes the address of a PTE, while the others are guest
3755 * virtual address which PTEs should be displayed. So, 'pdta' is rather simple
3756 * while the others require us to do all the tedious walking thru the paging
3757 * hierarchy to find the intended PTE.
3758 */
3759 unsigned iEntry = ~0U; /* The page table index. ~0U for 'dpta'. */
3760 DBGCVAR VarGCPtr; /* The GC address corresponding to the current PTE (iEntry != ~0U). */
3761 DBGCVAR VarPTEAddr; /* The address of the current PTE. */
3762 unsigned cEntries; /* The number of entries to display. */
3763 unsigned cEntriesMax; /* The max number of entries to display. */
3764 int rc;
3765 if (pCmd->pszCmd[3] == 'a')
3766 {
3767 VarPTEAddr = paArgs[0];
3768 switch (VarPTEAddr.enmRangeType)
3769 {
3770 case DBGCVAR_RANGE_BYTES: cEntries = VarPTEAddr.u64Range / cbEntry; break;
3771 case DBGCVAR_RANGE_ELEMENTS: cEntries = VarPTEAddr.u64Range; break;
3772 default: cEntries = 10; break;
3773 }
3774 cEntriesMax = PAGE_SIZE / cbEntry;
3775 }
3776 else
3777 {
3778 /*
3779 * Determine the range.
3780 */
3781 switch (paArgs[0].enmRangeType)
3782 {
3783 case DBGCVAR_RANGE_BYTES: cEntries = paArgs[0].u64Range / PAGE_SIZE; break;
3784 case DBGCVAR_RANGE_ELEMENTS: cEntries = paArgs[0].u64Range; break;
3785 default: cEntries = 10; break;
3786 }
3787
3788 /*
3789 * Normalize the input address, it must be a flat GC address.
3790 */
3791 rc = DBGCCmdHlpEval(pCmdHlp, &VarGCPtr, "%%(%Dv)", &paArgs[0]);
3792 if (RT_FAILURE(rc))
3793 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "%%(%Dv)", &paArgs[0]);
3794 if (VarGCPtr.enmType == DBGCVAR_TYPE_HC_FLAT)
3795 {
3796 VarGCPtr.u.GCFlat = (uintptr_t)VarGCPtr.u.pvHCFlat;
3797 VarGCPtr.enmType = DBGCVAR_TYPE_GC_FLAT;
3798 }
3799 VarGCPtr.u.GCFlat &= ~(RTGCPTR)PAGE_OFFSET_MASK;
3800
3801 /*
3802 * Do the paging walk until we get to the page table.
3803 */
3804 DBGCVAR VarCur;
3805 if (fGuest)
3806 DBGCVAR_INIT_GC_PHYS(&VarCur, cr3);
3807 else
3808 DBGCVAR_INIT_HC_PHYS(&VarCur, cr3);
3809 if (fLME)
3810 {
3811 /* Page Map Level 4 Lookup. */
3812 /* Check if it's a valid address first? */
3813 VarCur.u.u64Number &= X86_PTE_PAE_PG_MASK;
3814 VarCur.u.u64Number += (((uint64_t)VarGCPtr.u.GCFlat >> X86_PML4_SHIFT) & X86_PML4_MASK) * sizeof(X86PML4E);
3815 X86PML4E Pml4e;
3816 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pml4e, sizeof(Pml4e), &VarCur, NULL);
3817 if (RT_FAILURE(rc))
3818 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PML4E memory at %DV.\n", &VarCur);
3819 if (!Pml4e.n.u1Present)
3820 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory pointer table is not present for %Dv.\n", &VarGCPtr);
3821
3822 VarCur.u.u64Number = Pml4e.u & X86_PML4E_PG_MASK;
3823 Assert(fPAE);
3824 }
3825 if (fPAE)
3826 {
3827 /* Page directory pointer table. */
3828 X86PDPE Pdpe;
3829 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE) * sizeof(Pdpe);
3830 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pdpe, sizeof(Pdpe), &VarCur, NULL);
3831 if (RT_FAILURE(rc))
3832 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDPE memory at %DV.\n", &VarCur);
3833 if (!Pdpe.n.u1Present)
3834 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory is not present for %Dv.\n", &VarGCPtr);
3835
3836 VarCur.u.u64Number = Pdpe.u & X86_PDPE_PG_MASK;
3837
3838 /* Page directory (PAE). */
3839 X86PDEPAE Pde;
3840 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK) * sizeof(Pde);
3841 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pde, sizeof(Pde), &VarCur, NULL);
3842 if (RT_FAILURE(rc))
3843 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDE memory at %DV.\n", &VarCur);
3844 if (!Pde.n.u1Present)
3845 return DBGCCmdHlpPrintf(pCmdHlp, "Page table is not present for %Dv.\n", &VarGCPtr);
3846 if (fPSE && Pde.n.u1Size)
3847 return pCmdHlp->pfnExec(pCmdHlp, "dpd%s %Dv L3", &pCmd->pszCmd[3], &VarGCPtr);
3848
3849 iEntry = (VarGCPtr.u.GCFlat >> X86_PT_PAE_SHIFT) & X86_PT_PAE_MASK;
3850 VarPTEAddr = VarCur;
3851 VarPTEAddr.u.u64Number = Pde.u & X86_PDE_PAE_PG_MASK;
3852 VarPTEAddr.u.u64Number += iEntry * sizeof(X86PTEPAE);
3853 }
3854 else
3855 {
3856 /* Page directory (legacy). */
3857 X86PDE Pde;
3858 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PD_SHIFT) & X86_PD_MASK) * sizeof(Pde);
3859 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pde, sizeof(Pde), &VarCur, NULL);
3860 if (RT_FAILURE(rc))
3861 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDE memory at %DV.\n", &VarCur);
3862 if (!Pde.n.u1Present)
3863 return DBGCCmdHlpPrintf(pCmdHlp, "Page table is not present for %Dv.\n", &VarGCPtr);
3864 if (fPSE && Pde.n.u1Size)
3865 return pCmdHlp->pfnExec(pCmdHlp, "dpd%s %Dv L3", &pCmd->pszCmd[3], &VarGCPtr);
3866
3867 iEntry = (VarGCPtr.u.GCFlat >> X86_PT_SHIFT) & X86_PT_MASK;
3868 VarPTEAddr = VarCur;
3869 VarPTEAddr.u.u64Number = Pde.u & X86_PDE_PG_MASK;
3870 VarPTEAddr.u.u64Number += iEntry * sizeof(X86PTE);
3871 }
3872 cEntriesMax = (PAGE_SIZE - iEntry) / cbEntry;
3873 }
3874
3875 /* adjust cEntries */
3876 cEntries = RT_MAX(1, cEntries);
3877 cEntries = RT_MIN(cEntries, cEntriesMax);
3878
3879 /*
3880 * The display loop.
3881 */
3882 DBGCCmdHlpPrintf(pCmdHlp, iEntry != ~0U ? "%DV (base %DV / index %#x):\n" : "%DV:\n",
3883 &VarPTEAddr, &VarGCPtr, iEntry);
3884 do
3885 {
3886 /*
3887 * Read.
3888 */
3889 X86PTEPAE Pte;
3890 Pte.u = 0;
3891 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pte, cbEntry, &VarPTEAddr, NULL);
3892 if (RT_FAILURE(rc))
3893 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PTE memory at %DV.\n", &VarPTEAddr);
3894
3895 /*
3896 * Display.
3897 */
3898 if (iEntry != ~0U)
3899 {
3900 DBGCCmdHlpPrintf(pCmdHlp, "%03x %DV: ", iEntry, &VarGCPtr);
3901 iEntry++;
3902 }
3903 DBGCCmdHlpPrintf(pCmdHlp,
3904 fPAE
3905 ? "%016llx 4kb phys=%016llx %s %s %s %s %s avl=%02x %s %s %s %s %s"
3906 : "%08llx 4kb phys=%08llx %s %s %s %s %s avl=%02x %s %s %s %s %s",
3907 Pte.u,
3908 Pte.u & X86_PTE_PAE_PG_MASK,
3909 Pte.n.u1Present ? "p " : "np",
3910 Pte.n.u1Write ? "w" : "r",
3911 Pte.n.u1User ? "u" : "s",
3912 Pte.n.u1Accessed ? "a " : "na",
3913 Pte.n.u1Dirty ? "d " : "nd",
3914 Pte.n.u3Available,
3915 Pte.n.u1Global ? (fPGE ? "g" : "G") : " ",
3916 Pte.n.u1WriteThru ? "pwt" : " ",
3917 Pte.n.u1CacheDisable ? "pcd" : " ",
3918 Pte.n.u1PAT ? "pat" : " ",
3919 Pte.n.u1NoExecute ? (fNXE ? "nx" : "NX") : " "
3920 );
3921 if (Pte.u & UINT64_C(0x7fff000000000000))
3922 DBGCCmdHlpPrintf(pCmdHlp, " weird=%RX64", (Pte.u & UINT64_C(0x7fff000000000000)));
3923 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
3924 if (RT_FAILURE(rc))
3925 return rc;
3926
3927 /*
3928 * Advance.
3929 */
3930 VarPTEAddr.u.u64Number += cbEntry;
3931 if (iEntry != ~0U)
3932 VarGCPtr.u.GCFlat += PAGE_SIZE;
3933 } while (cEntries-- > 0);
3934
3935 return VINF_SUCCESS;
3936}
3937
3938
3939/**
3940 * @callback_method_impl{FNDBGCCMD, The 'dptb' command.}
3941 */
3942static DECLCALLBACK(int) dbgcCmdDumpPageTableBoth(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3943{
3944 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
3945 int rc1 = pCmdHlp->pfnExec(pCmdHlp, "dptg %DV", &paArgs[0]);
3946 int rc2 = pCmdHlp->pfnExec(pCmdHlp, "dpth %DV", &paArgs[0]);
3947 if (RT_FAILURE(rc1))
3948 return rc1;
3949 NOREF(pCmd); NOREF(cArgs);
3950 return rc2;
3951}
3952
3953
3954/**
3955 * @callback_method_impl{FNDBGCCMD, The 'dt' command.}
3956 */
3957static DECLCALLBACK(int) dbgcCmdDumpTSS(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3958{
3959 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3960 int rc;
3961
3962 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
3963 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs <= 1);
3964 if (cArgs == 1)
3965 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, paArgs[0].enmType != DBGCVAR_TYPE_STRING
3966 && paArgs[0].enmType != DBGCVAR_TYPE_SYMBOL);
3967
3968 /*
3969 * Check if the command indicates the type.
3970 */
3971 enum { kTss16, kTss32, kTss64, kTssToBeDetermined } enmTssType = kTssToBeDetermined;
3972 if (!strcmp(pCmd->pszCmd, "dt16"))
3973 enmTssType = kTss16;
3974 else if (!strcmp(pCmd->pszCmd, "dt32"))
3975 enmTssType = kTss32;
3976 else if (!strcmp(pCmd->pszCmd, "dt64"))
3977 enmTssType = kTss64;
3978
3979 /*
3980 * We can get a TSS selector (number), a far pointer using a TSS selector, or some kind of TSS pointer.
3981 */
3982 uint32_t SelTss = UINT32_MAX;
3983 DBGCVAR VarTssAddr;
3984 if (cArgs == 0)
3985 {
3986 /** @todo consider querying the hidden bits instead (missing API). */
3987 uint16_t SelTR;
3988 rc = DBGFR3RegCpuQueryU16(pUVM, pDbgc->idCpu, DBGFREG_TR, &SelTR);
3989 if (RT_FAILURE(rc))
3990 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Failed to query TR, rc=%Rrc\n", rc);
3991 DBGCVAR_INIT_GC_FAR(&VarTssAddr, SelTR, 0);
3992 SelTss = SelTR;
3993 }
3994 else if (paArgs[0].enmType == DBGCVAR_TYPE_NUMBER)
3995 {
3996 if (paArgs[0].u.u64Number < 0xffff)
3997 DBGCVAR_INIT_GC_FAR(&VarTssAddr, (RTSEL)paArgs[0].u.u64Number, 0);
3998 else
3999 {
4000 if (paArgs[0].enmRangeType == DBGCVAR_RANGE_ELEMENTS)
4001 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Element count doesn't combine with a TSS address.\n");
4002 DBGCVAR_INIT_GC_FLAT(&VarTssAddr, paArgs[0].u.u64Number);
4003 if (paArgs[0].enmRangeType == DBGCVAR_RANGE_BYTES)
4004 {
4005 VarTssAddr.enmRangeType = paArgs[0].enmRangeType;
4006 VarTssAddr.u64Range = paArgs[0].u64Range;
4007 }
4008 }
4009 }
4010 else
4011 VarTssAddr = paArgs[0];
4012
4013 /*
4014 * Deal with TSS:ign by means of the GDT.
4015 */
4016 if (VarTssAddr.enmType == DBGCVAR_TYPE_GC_FAR)
4017 {
4018 SelTss = VarTssAddr.u.GCFar.sel;
4019 DBGFSELINFO SelInfo;
4020 rc = DBGFR3SelQueryInfo(pUVM, pDbgc->idCpu, VarTssAddr.u.GCFar.sel, DBGFSELQI_FLAGS_DT_GUEST, &SelInfo);
4021 if (RT_FAILURE(rc))
4022 return DBGCCmdHlpFail(pCmdHlp, pCmd, "DBGFR3SelQueryInfo(,%u,%d,,) -> %Rrc.\n",
4023 pDbgc->idCpu, VarTssAddr.u.GCFar.sel, rc);
4024
4025 if (SelInfo.u.Raw.Gen.u1DescType)
4026 return DBGCCmdHlpFail(pCmdHlp, pCmd, "%04x is not a TSS selector. (!sys)\n", VarTssAddr.u.GCFar.sel);
4027
4028 switch (SelInfo.u.Raw.Gen.u4Type)
4029 {
4030 case X86_SEL_TYPE_SYS_286_TSS_BUSY:
4031 case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
4032 if (enmTssType == kTssToBeDetermined)
4033 enmTssType = kTss16;
4034 break;
4035
4036 case X86_SEL_TYPE_SYS_386_TSS_BUSY: /* AMD64 too */
4037 case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
4038 if (enmTssType == kTssToBeDetermined)
4039 enmTssType = SelInfo.fFlags & DBGFSELINFO_FLAGS_LONG_MODE ? kTss64 : kTss32;
4040 break;
4041
4042 default:
4043 return DBGCCmdHlpFail(pCmdHlp, pCmd, "%04x is not a TSS selector. (type=%x)\n",
4044 VarTssAddr.u.GCFar.sel, SelInfo.u.Raw.Gen.u4Type);
4045 }
4046
4047 DBGCVAR_INIT_GC_FLAT(&VarTssAddr, SelInfo.GCPtrBase);
4048 DBGCVAR_SET_RANGE(&VarTssAddr, DBGCVAR_RANGE_BYTES, RT_MAX(SelInfo.cbLimit + 1, SelInfo.cbLimit));
4049 }
4050
4051 /*
4052 * Determine the TSS type if none is currently given.
4053 */
4054 if (enmTssType == kTssToBeDetermined)
4055 {
4056 if ( VarTssAddr.u64Range > 0
4057 && VarTssAddr.u64Range < sizeof(X86TSS32) - 4)
4058 enmTssType = kTss16;
4059 else
4060 {
4061 uint64_t uEfer;
4062 rc = DBGFR3RegCpuQueryU64(pUVM, pDbgc->idCpu, DBGFREG_MSR_K6_EFER, &uEfer);
4063 if ( RT_FAILURE(rc)
4064 || !(uEfer & MSR_K6_EFER_LMA) )
4065 enmTssType = kTss32;
4066 else
4067 enmTssType = kTss64;
4068 }
4069 }
4070
4071 /*
4072 * Figure the min/max sizes.
4073 * ASSUMES max TSS size is 64 KB.
4074 */
4075 uint32_t cbTssMin;
4076 uint32_t cbTssMax;
4077 switch (enmTssType)
4078 {
4079 case kTss16:
4080 cbTssMin = cbTssMax = X86_SEL_TYPE_SYS_286_TSS_LIMIT_MIN + 1;
4081 break;
4082 case kTss32:
4083 cbTssMin = X86_SEL_TYPE_SYS_386_TSS_LIMIT_MIN + 1;
4084 cbTssMax = _64K;
4085 break;
4086 case kTss64:
4087 cbTssMin = X86_SEL_TYPE_SYS_386_TSS_LIMIT_MIN + 1;
4088 cbTssMax = _64K;
4089 break;
4090 default:
4091 AssertFailedReturn(VERR_INTERNAL_ERROR);
4092 }
4093 uint32_t cbTss = VarTssAddr.enmRangeType == DBGCVAR_RANGE_BYTES ? (uint32_t)VarTssAddr.u64Range : 0;
4094 if (cbTss == 0)
4095 cbTss = cbTssMin;
4096 else if (cbTss < cbTssMin)
4097 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Minimum TSS size is %u bytes, you specified %llu (%llx) bytes.\n",
4098 cbTssMin, VarTssAddr.u64Range, VarTssAddr.u64Range);
4099 else if (cbTss > cbTssMax)
4100 cbTss = cbTssMax;
4101 DBGCVAR_SET_RANGE(&VarTssAddr, DBGCVAR_RANGE_BYTES, cbTss);
4102
4103 /*
4104 * Read the TSS into a temporary buffer.
4105 */
4106 uint8_t abBuf[_64K];
4107 size_t cbTssRead;
4108 rc = DBGCCmdHlpMemRead(pCmdHlp, abBuf, cbTss, &VarTssAddr, &cbTssRead);
4109 if (RT_FAILURE(rc))
4110 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Failed to read TSS at %Dv: %Rrc\n", &VarTssAddr, rc);
4111 if (cbTssRead < cbTssMin)
4112 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Failed to read essential parts of the TSS (read %zu, min %zu).\n",
4113 cbTssRead, cbTssMin);
4114 if (cbTssRead < cbTss)
4115 memset(&abBuf[cbTssRead], 0xff, cbTss - cbTssRead);
4116
4117
4118 /*
4119 * Format the TSS.
4120 */
4121 uint16_t offIoBitmap;
4122 switch (enmTssType)
4123 {
4124 case kTss16:
4125 {
4126 PCX86TSS16 pTss = (PCX86TSS16)&abBuf[0];
4127 if (SelTss != UINT32_MAX)
4128 DBGCCmdHlpPrintf(pCmdHlp, "%04x TSS16 at %Dv\n", SelTss, &VarTssAddr);
4129 else
4130 DBGCCmdHlpPrintf(pCmdHlp, "TSS16 at %Dv\n", &VarTssAddr);
4131 DBGCCmdHlpPrintf(pCmdHlp,
4132 "ax=%04x bx=%04x cx=%04x dx=%04x si=%04x di=%04x\n"
4133 "ip=%04x sp=%04x bp=%04x\n"
4134 "cs=%04x ss=%04x ds=%04x es=%04x flags=%04x\n"
4135 "ss:sp0=%04x:%04x ss:sp1=%04x:%04x ss:sp2=%04x:%04x\n"
4136 "prev=%04x ldtr=%04x\n"
4137 ,
4138 pTss->ax, pTss->bx, pTss->cx, pTss->dx, pTss->si, pTss->di,
4139 pTss->ip, pTss->sp, pTss->bp,
4140 pTss->cs, pTss->ss, pTss->ds, pTss->es, pTss->flags,
4141 pTss->ss0, pTss->sp0