VirtualBox

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

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

Debugger/EmulateCodeview: Start dumping branch tables contained in graphs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 274.2 KB
Line 
1/* $Id: DBGCEmulateCodeView.cpp 64591 2016-11-06 19:15:57Z 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 || enmType == DBGFFLOWBBENDTYPE_UNCOND_INDIRECT_JMP)
1440 DBGFR3FlowBbGetBranchAddress(hFlowBb, &pDumpBb->AddrTarget);
1441
1442 if (fFlags & DBGF_FLOW_BB_F_INCOMPLETE_ERR)
1443 {
1444 const char *pszErr = NULL;
1445 DBGFR3FlowBbQueryError(hFlowBb, &pszErr);
1446 if (pszErr)
1447 {
1448 pDumpBb->cchHeight++;
1449 pDumpBb->cchWidth = RT_MAX(pDumpBb->cchWidth, (uint32_t)strlen(pszErr));
1450 }
1451 }
1452 for (unsigned i = 0; i < cInstr; i++)
1453 {
1454 const char *pszInstr = NULL;
1455 int rc = DBGFR3FlowBbQueryInstr(hFlowBb, i, NULL, NULL, &pszInstr);
1456 AssertRC(rc);
1457 pDumpBb->cchWidth = RT_MAX(pDumpBb->cchWidth, (uint32_t)strlen(pszInstr));
1458 }
1459 pDumpBb->cchWidth += 4; /* Include spacing and border left and right. */
1460}
1461
1462
1463/**
1464 * Dumps a top or bottom boundary line.
1465 *
1466 * @returns nothing.
1467 * @param hScreen The screen to draw to.
1468 * @param uStartX Where to start drawing the boundary.
1469 * @param uStartY Y coordinate.
1470 * @param cchWidth Width of the boundary.
1471 * @param enmColor The color to use for drawing.
1472 */
1473static void dbgcCmdUnassembleCfgDumpBbBoundary(DBGCSCREEN hScreen, uint32_t uStartX, uint32_t uStartY, uint32_t cchWidth,
1474 DBGCSCREENCOLOR enmColor)
1475{
1476 dbgcScreenAsciiDrawCharacter(hScreen, uStartX, uStartY, '+', enmColor);
1477 dbgcScreenAsciiDrawLineHorizontal(hScreen, uStartX + 1, uStartX + 1 + cchWidth - 2,
1478 uStartY, '-', enmColor);
1479 dbgcScreenAsciiDrawCharacter(hScreen, uStartX + cchWidth - 1, uStartY, '+', enmColor);
1480}
1481
1482
1483/**
1484 * Dumps a spacing line between the top or bottom boundary and the actual disassembly.
1485 *
1486 * @returns nothing.
1487 * @param hScreen The screen to draw to.
1488 * @param uStartX Where to start drawing the spacing.
1489 * @param uStartY Y coordinate.
1490 * @param cchWidth Width of the spacing.
1491 * @param enmColor The color to use for drawing.
1492 */
1493static void dbgcCmdUnassembleCfgDumpBbSpacing(DBGCSCREEN hScreen, uint32_t uStartX, uint32_t uStartY, uint32_t cchWidth,
1494 DBGCSCREENCOLOR enmColor)
1495{
1496 dbgcScreenAsciiDrawCharacter(hScreen, uStartX, uStartY, '|', enmColor);
1497 dbgcScreenAsciiDrawLineHorizontal(hScreen, uStartX + 1, uStartX + 1 + cchWidth - 2,
1498 uStartY, ' ', enmColor);
1499 dbgcScreenAsciiDrawCharacter(hScreen, uStartX + cchWidth - 1, uStartY, '|', enmColor);
1500}
1501
1502
1503/**
1504 * Writes a given text to the screen.
1505 *
1506 * @returns nothing.
1507 * @param hScreen The screen to draw to.
1508 * @param uStartX Where to start drawing the line.
1509 * @param uStartY Y coordinate.
1510 * @param cchWidth Maximum width of the text.
1511 * @param pszText The text to write.
1512 * @param enmTextColor The color to use for drawing the text.
1513 * @param enmBorderColor The color to use for drawing the border.
1514 */
1515static void dbgcCmdUnassembleCfgDumpBbText(DBGCSCREEN hScreen, uint32_t uStartX, uint32_t uStartY,
1516 uint32_t cchWidth, const char *pszText,
1517 DBGCSCREENCOLOR enmTextColor, DBGCSCREENCOLOR enmBorderColor)
1518{
1519 dbgcScreenAsciiDrawCharacter(hScreen, uStartX, uStartY, '|', enmBorderColor);
1520 dbgcScreenAsciiDrawCharacter(hScreen, uStartX + 1, uStartY, ' ', enmTextColor);
1521 dbgcScreenAsciiDrawString(hScreen, uStartX + 2, uStartY, pszText, enmTextColor);
1522 dbgcScreenAsciiDrawCharacter(hScreen, uStartX + cchWidth - 1, uStartY, '|', enmBorderColor);
1523}
1524
1525
1526/**
1527 * Dumps one basic block using the dumper callback.
1528 *
1529 * @returns nothing.
1530 * @param pDumpBb The basic block dump state to dump.
1531 * @param hScreen The screen to draw to.
1532 */
1533static void dbgcCmdUnassembleCfgDumpBb(PDBGCFLOWBBDUMP pDumpBb, DBGCSCREEN hScreen)
1534{
1535 uint32_t uStartY = pDumpBb->uStartY;
1536 bool fError = RT_BOOL(DBGFR3FlowBbGetFlags(pDumpBb->hFlowBb) & DBGF_FLOW_BB_F_INCOMPLETE_ERR);
1537 DBGCSCREENCOLOR enmColor = fError ? DBGCSCREENCOLOR_RED_BRIGHT : DBGCSCREENCOLOR_DEFAULT;
1538
1539 dbgcCmdUnassembleCfgDumpBbBoundary(hScreen, pDumpBb->uStartX, uStartY, pDumpBb->cchWidth, enmColor);
1540 uStartY++;
1541 dbgcCmdUnassembleCfgDumpBbSpacing(hScreen, pDumpBb->uStartX, uStartY, pDumpBb->cchWidth, enmColor);
1542 uStartY++;
1543
1544 uint32_t cInstr = DBGFR3FlowBbGetInstrCount(pDumpBb->hFlowBb);
1545 for (unsigned i = 0; i < cInstr; i++)
1546 {
1547 const char *pszInstr = NULL;
1548 DBGFR3FlowBbQueryInstr(pDumpBb->hFlowBb, i, NULL, NULL, &pszInstr);
1549 dbgcCmdUnassembleCfgDumpBbText(hScreen, pDumpBb->uStartX, uStartY + i,
1550 pDumpBb->cchWidth, pszInstr, DBGCSCREENCOLOR_DEFAULT,
1551 enmColor);
1552 }
1553 uStartY += cInstr;
1554
1555 if (fError)
1556 {
1557 const char *pszErr = NULL;
1558 DBGFR3FlowBbQueryError(pDumpBb->hFlowBb, &pszErr);
1559 if (pszErr)
1560 dbgcCmdUnassembleCfgDumpBbText(hScreen, pDumpBb->uStartX, uStartY,
1561 pDumpBb->cchWidth, pszErr, enmColor,
1562 enmColor);
1563 uStartY++;
1564 }
1565
1566 dbgcCmdUnassembleCfgDumpBbSpacing(hScreen, pDumpBb->uStartX, uStartY, pDumpBb->cchWidth, enmColor);
1567 uStartY++;
1568 dbgcCmdUnassembleCfgDumpBbBoundary(hScreen, pDumpBb->uStartX, uStartY, pDumpBb->cchWidth, enmColor);
1569 uStartY++;
1570}
1571
1572
1573/**
1574 * Dumps one branch table using the dumper callback.
1575 *
1576 * @returns nothing.
1577 * @param pDumpBb The basic block dump state to dump.
1578 * @param hScreen The screen to draw to.
1579 */
1580static void dbgcCmdUnassembleCfgDumpBranchTbl(PDBGCFLOWBRANCHTBLDUMP pDumpBranchTbl, DBGCSCREEN hScreen)
1581{
1582 uint32_t uStartY = pDumpBranchTbl->uStartY;
1583 DBGCSCREENCOLOR enmColor = DBGCSCREENCOLOR_CYAN_BRIGHT;
1584
1585 dbgcCmdUnassembleCfgDumpBbBoundary(hScreen, pDumpBranchTbl->uStartX, uStartY, pDumpBranchTbl->cchWidth, enmColor);
1586 uStartY++;
1587 dbgcCmdUnassembleCfgDumpBbSpacing(hScreen, pDumpBranchTbl->uStartX, uStartY, pDumpBranchTbl->cchWidth, enmColor);
1588 uStartY++;
1589
1590 uint32_t cSlots = DBGFR3FlowBranchTblGetSlots(pDumpBranchTbl->hFlowBranchTbl);
1591 for (unsigned i = 0; i < cSlots; i++)
1592 {
1593 DBGFADDRESS Addr;
1594 char szAddr[128];
1595
1596 RT_ZERO(szAddr);
1597 DBGFR3FlowBranchTblGetAddrAtSlot(pDumpBranchTbl->hFlowBranchTbl, i, &Addr);
1598
1599 if (Addr.Sel == DBGF_SEL_FLAT)
1600 RTStrPrintf(&szAddr[0], sizeof(szAddr), "%RGv", Addr.FlatPtr);
1601 else
1602 RTStrPrintf(&szAddr[0], sizeof(szAddr), "%04x:%RGv", Addr.Sel, Addr.off);
1603
1604 dbgcCmdUnassembleCfgDumpBbText(hScreen, pDumpBranchTbl->uStartX, uStartY + i,
1605 pDumpBranchTbl->cchWidth, &szAddr[0], DBGCSCREENCOLOR_DEFAULT,
1606 enmColor);
1607 }
1608 uStartY += cSlots;
1609
1610 dbgcCmdUnassembleCfgDumpBbSpacing(hScreen, pDumpBranchTbl->uStartX, uStartY, pDumpBranchTbl->cchWidth, enmColor);
1611 uStartY++;
1612 dbgcCmdUnassembleCfgDumpBbBoundary(hScreen, pDumpBranchTbl->uStartX, uStartY, pDumpBranchTbl->cchWidth, enmColor);
1613 uStartY++;
1614}
1615
1616
1617/**
1618 * Fills in the dump states for the basic blocks and branch tables.
1619 *
1620 * @returns VBox status code.
1621 * @param hFlowIt The control flow graph iterator handle.
1622 * @param hFlowBranchTblIt The control flow graph branch table iterator handle.
1623 * @param paDumpBb The array of basic block dump states.
1624 * @param paDumpBranchTbl The array of branch table dump states.
1625 * @param cBbs Number of basic blocks.
1626 * @param cBranchTbls Number of branch tables.
1627 */
1628static int dbgcCmdUnassembleCfgDumpCalcDimensions(DBGFFLOWIT hFlowIt, DBGFFLOWBRANCHTBLIT hFlowBranchTblIt,
1629 PDBGCFLOWBBDUMP paDumpBb, PDBGCFLOWBRANCHTBLDUMP paDumpBranchTbl,
1630 uint32_t cBbs, uint32_t cBranchTbls)
1631{
1632 RT_NOREF2(cBbs, cBranchTbls);
1633
1634 /* Calculate the sizes of each basic block first. */
1635 DBGFFLOWBB hFlowBb = DBGFR3FlowItNext(hFlowIt);
1636 uint32_t idx = 0;
1637 while (hFlowBb)
1638 {
1639 dbgcCmdUnassembleCfgDumpCalcBbSize(hFlowBb, &paDumpBb[idx]);
1640 idx++;
1641 hFlowBb = DBGFR3FlowItNext(hFlowIt);
1642 }
1643
1644 idx = 0;
1645 DBGFFLOWBRANCHTBL hFlowBranchTbl = DBGFR3FlowBranchTblItNext(hFlowBranchTblIt);
1646 while (hFlowBranchTbl)
1647 {
1648 paDumpBranchTbl[idx].hFlowBranchTbl = hFlowBranchTbl;
1649 paDumpBranchTbl[idx].cchHeight = DBGFR3FlowBranchTblGetSlots(hFlowBranchTbl) + 4; /* Spacing and border. */
1650 paDumpBranchTbl[idx].cchWidth = 25 + 4; /* Spacing and border. */
1651 idx++;
1652 hFlowBranchTbl = DBGFR3FlowBranchTblItNext(hFlowBranchTblIt);
1653 }
1654
1655 return VINF_SUCCESS;
1656}
1657
1658/**
1659 * Dumps the given control flow graph to the output.
1660 *
1661 * @returns VBox status code.
1662 * @param hCfg The control flow graph handle.
1663 * @param fUseColor Flag whether the output should be colorized.
1664 * @param pCmdHlp The command helper callback table.
1665 */
1666static int dbgcCmdUnassembleCfgDump(DBGFFLOW hCfg, bool fUseColor, PDBGCCMDHLP pCmdHlp)
1667{
1668 int rc = VINF_SUCCESS;
1669 DBGFFLOWIT hCfgIt = NULL;
1670 DBGFFLOWBRANCHTBLIT hFlowBranchTblIt = NULL;
1671 uint32_t cBbs = DBGFR3FlowGetBbCount(hCfg);
1672 uint32_t cBranchTbls = DBGFR3FlowGetBranchTblCount(hCfg);
1673 PDBGCFLOWBBDUMP paDumpBb = (PDBGCFLOWBBDUMP)RTMemTmpAllocZ(cBbs * sizeof(DBGCFLOWBBDUMP));
1674 PDBGCFLOWBRANCHTBLDUMP paDumpBranchTbl = NULL;
1675
1676 if (cBranchTbls)
1677 paDumpBranchTbl = (PDBGCFLOWBRANCHTBLDUMP)RTMemAllocZ(cBranchTbls * sizeof(DBGCFLOWBRANCHTBLDUMP));
1678
1679 if (RT_UNLIKELY(!paDumpBb || (!paDumpBranchTbl && cBranchTbls > 0)))
1680 rc = VERR_NO_MEMORY;
1681 if (RT_SUCCESS(rc))
1682 rc = DBGFR3FlowItCreate(hCfg, DBGFFLOWITORDER_BY_ADDR_LOWEST_FIRST, &hCfgIt);
1683 if (RT_SUCCESS(rc) && cBranchTbls > 0)
1684 rc = DBGFR3FlowBranchTblItCreate(hCfg, DBGFFLOWITORDER_BY_ADDR_LOWEST_FIRST, &hFlowBranchTblIt);
1685
1686 if (RT_SUCCESS(rc))
1687 {
1688 rc = dbgcCmdUnassembleCfgDumpCalcDimensions(hCfgIt, hFlowBranchTblIt, paDumpBb, paDumpBranchTbl,
1689 cBbs, cBranchTbls);
1690
1691 /* Calculate the ASCII screen dimensions and create one. */
1692 uint32_t cchWidth = 0;
1693 uint32_t cchLeftExtra = 5;
1694 uint32_t cchRightExtra = 5;
1695 uint32_t cchHeight = 0;
1696 for (unsigned i = 0; i < cBbs; i++)
1697 {
1698 PDBGCFLOWBBDUMP pDumpBb = &paDumpBb[i];
1699 cchWidth = RT_MAX(cchWidth, pDumpBb->cchWidth);
1700 cchHeight += pDumpBb->cchHeight;
1701
1702 /* Incomplete blocks don't have a successor. */
1703 if (DBGFR3FlowBbGetFlags(pDumpBb->hFlowBb) & DBGF_FLOW_BB_F_INCOMPLETE_ERR)
1704 continue;
1705
1706 switch (DBGFR3FlowBbGetType(pDumpBb->hFlowBb))
1707 {
1708 case DBGFFLOWBBENDTYPE_EXIT:
1709 case DBGFFLOWBBENDTYPE_LAST_DISASSEMBLED:
1710 break;
1711 case DBGFFLOWBBENDTYPE_UNCOND_JMP:
1712 if ( dbgcCmdUnassembleCfgAddrLower(&pDumpBb->AddrTarget, &pDumpBb->AddrStart)
1713 || dbgcCmdUnassembleCfgAddrEqual(&pDumpBb->AddrTarget, &pDumpBb->AddrStart))
1714 cchLeftExtra++;
1715 else
1716 cchRightExtra++;
1717 break;
1718 case DBGFFLOWBBENDTYPE_UNCOND:
1719 cchHeight += 2; /* For the arrow down to the next basic block. */
1720 break;
1721 case DBGFFLOWBBENDTYPE_COND:
1722 cchHeight += 2; /* For the arrow down to the next basic block. */
1723 if ( dbgcCmdUnassembleCfgAddrLower(&pDumpBb->AddrTarget, &pDumpBb->AddrStart)
1724 || dbgcCmdUnassembleCfgAddrEqual(&pDumpBb->AddrTarget, &pDumpBb->AddrStart))
1725 cchLeftExtra++;
1726 else
1727 cchRightExtra++;
1728 break;
1729 case DBGFFLOWBBENDTYPE_UNCOND_INDIRECT_JMP:
1730 default:
1731 AssertFailed();
1732 }
1733 }
1734
1735 for (unsigned i = 0; i < cBranchTbls; i++)
1736 {
1737 PDBGCFLOWBRANCHTBLDUMP pDumpBranchTbl = &paDumpBranchTbl[i];
1738 cchWidth = RT_MAX(cchWidth, pDumpBranchTbl->cchWidth);
1739 cchHeight += pDumpBranchTbl->cchHeight;
1740 }
1741
1742 cchWidth += 2;
1743
1744 DBGCSCREEN hScreen = NULL;
1745 rc = dbgcScreenAsciiCreate(&hScreen, cchWidth + cchLeftExtra + cchRightExtra, cchHeight);
1746 if (RT_SUCCESS(rc))
1747 {
1748 uint32_t uY = 0;
1749
1750 /* Dump the branch tables first. */
1751 for (unsigned i = 0; i < cBranchTbls; i++)
1752 {
1753 paDumpBranchTbl[i].uStartX = cchLeftExtra + (cchWidth - paDumpBranchTbl[i].cchWidth) / 2;
1754 paDumpBranchTbl[i].uStartY = uY;
1755 dbgcCmdUnassembleCfgDumpBranchTbl(&paDumpBranchTbl[i], hScreen);
1756 uY += paDumpBranchTbl[i].cchHeight;
1757 }
1758
1759 /* Dump the basic blocks and connections to the immediate successor. */
1760 for (unsigned i = 0; i < cBbs; i++)
1761 {
1762 paDumpBb[i].uStartX = cchLeftExtra + (cchWidth - paDumpBb[i].cchWidth) / 2;
1763 paDumpBb[i].uStartY = uY;
1764 dbgcCmdUnassembleCfgDumpBb(&paDumpBb[i], hScreen);
1765 uY += paDumpBb[i].cchHeight;
1766
1767 /* Incomplete blocks don't have a successor. */
1768 if (DBGFR3FlowBbGetFlags(paDumpBb[i].hFlowBb) & DBGF_FLOW_BB_F_INCOMPLETE_ERR)
1769 continue;
1770
1771 switch (DBGFR3FlowBbGetType(paDumpBb[i].hFlowBb))
1772 {
1773 case DBGFFLOWBBENDTYPE_EXIT:
1774 case DBGFFLOWBBENDTYPE_LAST_DISASSEMBLED:
1775 case DBGFFLOWBBENDTYPE_UNCOND_JMP:
1776 case DBGFFLOWBBENDTYPE_UNCOND_INDIRECT_JMP:
1777 break;
1778 case DBGFFLOWBBENDTYPE_UNCOND:
1779 /* Draw the arrow down to the next block. */
1780 dbgcScreenAsciiDrawCharacter(hScreen, cchLeftExtra + cchWidth / 2, uY,
1781 '|', DBGCSCREENCOLOR_BLUE_BRIGHT);
1782 uY++;
1783 dbgcScreenAsciiDrawCharacter(hScreen, cchLeftExtra + cchWidth / 2, uY,
1784 'V', DBGCSCREENCOLOR_BLUE_BRIGHT);
1785 uY++;
1786 break;
1787 case DBGFFLOWBBENDTYPE_COND:
1788 /* Draw the arrow down to the next block. */
1789 dbgcScreenAsciiDrawCharacter(hScreen, cchLeftExtra + cchWidth / 2, uY,
1790 '|', DBGCSCREENCOLOR_RED_BRIGHT);
1791 uY++;
1792 dbgcScreenAsciiDrawCharacter(hScreen, cchLeftExtra + cchWidth / 2, uY,
1793 'V', DBGCSCREENCOLOR_RED_BRIGHT);
1794 uY++;
1795 break;
1796 default:
1797 AssertFailed();
1798 }
1799 }
1800
1801 /* Last pass, connect all remaining branches. */
1802 uint32_t uBackConns = 0;
1803 uint32_t uFwdConns = 0;
1804 for (unsigned i = 0; i < cBbs; i++)
1805 {
1806 PDBGCFLOWBBDUMP pDumpBb = &paDumpBb[i];
1807 DBGFFLOWBBENDTYPE enmEndType = DBGFR3FlowBbGetType(pDumpBb->hFlowBb);
1808
1809 /* Incomplete blocks don't have a successor. */
1810 if (DBGFR3FlowBbGetFlags(pDumpBb->hFlowBb) & DBGF_FLOW_BB_F_INCOMPLETE_ERR)
1811 continue;
1812
1813 switch (enmEndType)
1814 {
1815 case DBGFFLOWBBENDTYPE_EXIT:
1816 case DBGFFLOWBBENDTYPE_LAST_DISASSEMBLED:
1817 case DBGFFLOWBBENDTYPE_UNCOND:
1818 break;
1819 case DBGFFLOWBBENDTYPE_COND:
1820 case DBGFFLOWBBENDTYPE_UNCOND_JMP:
1821 {
1822 /* Find the target first to get the coordinates. */
1823 PDBGCFLOWBBDUMP pDumpBbTgt = NULL;
1824 for (unsigned idxDumpBb = 0; idxDumpBb < cBbs; idxDumpBb++)
1825 {
1826 pDumpBbTgt = &paDumpBb[idxDumpBb];
1827 if (dbgcCmdUnassembleCfgAddrEqual(&pDumpBb->AddrTarget, &pDumpBbTgt->AddrStart))
1828 break;
1829 }
1830
1831 DBGCSCREENCOLOR enmColor = enmEndType == DBGFFLOWBBENDTYPE_UNCOND_JMP
1832 ? DBGCSCREENCOLOR_YELLOW_BRIGHT
1833 : DBGCSCREENCOLOR_GREEN_BRIGHT;
1834
1835 /*
1836 * Use the right side for targets with higher addresses,
1837 * left when jumping backwards.
1838 */
1839 if ( dbgcCmdUnassembleCfgAddrLower(&pDumpBb->AddrTarget, &pDumpBb->AddrStart)
1840 || dbgcCmdUnassembleCfgAddrEqual(&pDumpBb->AddrTarget, &pDumpBb->AddrStart))
1841 {
1842 /* Going backwards. */
1843 uint32_t uXVerLine = /*cchLeftExtra - 1 -*/ uBackConns + 1;
1844 uint32_t uYHorLine = pDumpBb->uStartY + pDumpBb->cchHeight - 1 - 2;
1845 uBackConns++;
1846
1847 /* Draw the arrow pointing to the target block. */
1848 dbgcScreenAsciiDrawCharacter(hScreen, pDumpBbTgt->uStartX - 1, pDumpBbTgt->uStartY,
1849 '>', enmColor);
1850 /* Draw the horizontal line. */
1851 dbgcScreenAsciiDrawLineHorizontal(hScreen, uXVerLine + 1, pDumpBbTgt->uStartX - 2,
1852 pDumpBbTgt->uStartY, '-', enmColor);
1853 dbgcScreenAsciiDrawCharacter(hScreen, uXVerLine, pDumpBbTgt->uStartY, '+',
1854 enmColor);
1855 /* Draw the vertical line down to the source block. */
1856 dbgcScreenAsciiDrawLineVertical(hScreen, uXVerLine, pDumpBbTgt->uStartY + 1, uYHorLine - 1,
1857 '|', enmColor);
1858 dbgcScreenAsciiDrawCharacter(hScreen, uXVerLine, uYHorLine, '+', enmColor);
1859 /* Draw the horizontal connection between the source block and vertical part. */
1860 dbgcScreenAsciiDrawLineHorizontal(hScreen, uXVerLine + 1, pDumpBb->uStartX - 1,
1861 uYHorLine, '-', enmColor);
1862
1863 }
1864 else
1865 {
1866 /* Going forward. */
1867 uint32_t uXVerLine = cchWidth + cchLeftExtra + (cchRightExtra - uFwdConns) - 1;
1868 uint32_t uYHorLine = pDumpBb->uStartY + pDumpBb->cchHeight - 1 - 2;
1869 uFwdConns++;
1870
1871 /* Draw the horizontal line. */
1872 dbgcScreenAsciiDrawLineHorizontal(hScreen, pDumpBb->uStartX + pDumpBb->cchWidth,
1873 uXVerLine - 1, uYHorLine, '-', enmColor);
1874 dbgcScreenAsciiDrawCharacter(hScreen, uXVerLine, uYHorLine, '+', enmColor);
1875 /* Draw the vertical line down to the target block. */
1876 dbgcScreenAsciiDrawLineVertical(hScreen, uXVerLine, uYHorLine + 1, pDumpBbTgt->uStartY - 1,
1877 '|', enmColor);
1878 /* Draw the horizontal connection between the target block and vertical part. */
1879 dbgcScreenAsciiDrawLineHorizontal(hScreen, pDumpBbTgt->uStartX + pDumpBbTgt->cchWidth,
1880 uXVerLine, pDumpBbTgt->uStartY, '-', enmColor);
1881 dbgcScreenAsciiDrawCharacter(hScreen, uXVerLine, pDumpBbTgt->uStartY, '+',
1882 enmColor);
1883 /* Draw the arrow pointing to the target block. */
1884 dbgcScreenAsciiDrawCharacter(hScreen, pDumpBbTgt->uStartX + pDumpBbTgt->cchWidth,
1885 pDumpBbTgt->uStartY, '<', enmColor);
1886 }
1887 break;
1888 }
1889 case DBGFFLOWBBENDTYPE_UNCOND_INDIRECT_JMP:
1890 default:
1891 AssertFailed();
1892 }
1893 }
1894
1895 rc = dbgcScreenAsciiBlit(hScreen, dbgcCmdUnassembleCfgBlit, pCmdHlp, fUseColor);
1896 dbgcScreenAsciiDestroy(hScreen);
1897 }
1898 }
1899
1900 if (paDumpBb)
1901 {
1902 for (unsigned i = 0; i < cBbs; i++)
1903 DBGFR3FlowBbRelease(paDumpBb[i].hFlowBb);
1904 RTMemTmpFree(paDumpBb);
1905 }
1906
1907 if (paDumpBranchTbl)
1908 {
1909 for (unsigned i = 0; i < cBranchTbls; i++)
1910 DBGFR3FlowBranchTblRelease(paDumpBranchTbl[i].hFlowBranchTbl);
1911 RTMemTmpFree(paDumpBranchTbl);
1912 }
1913
1914 if (hCfgIt)
1915 DBGFR3FlowItDestroy(hCfgIt);
1916 if (hFlowBranchTblIt)
1917 DBGFR3FlowBranchTblItDestroy(hFlowBranchTblIt);
1918
1919 return rc;
1920}
1921
1922
1923/**
1924 * @callback_method_impl{FNDBGCCMD, The 'ucfg' command.}
1925 */
1926static DECLCALLBACK(int) dbgcCmdUnassembleCfg(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
1927{
1928 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1929
1930 /*
1931 * Validate input.
1932 */
1933 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
1934 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, -1, cArgs <= 1);
1935 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs == 0 || DBGCVAR_ISPOINTER(paArgs[0].enmType));
1936
1937 if (!cArgs && !DBGCVAR_ISPOINTER(pDbgc->DisasmPos.enmType))
1938 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Don't know where to start disassembling");
1939
1940 /*
1941 * Check the desired mode.
1942 */
1943 unsigned fFlags = DBGF_DISAS_FLAGS_UNPATCHED_BYTES | DBGF_DISAS_FLAGS_ANNOTATE_PATCHED;
1944 bool fUseColor = false;
1945 switch (pCmd->pszCmd[4])
1946 {
1947 default: AssertFailed();
1948 case '\0': fFlags |= DBGF_DISAS_FLAGS_DEFAULT_MODE; break;
1949 case '6': fFlags |= DBGF_DISAS_FLAGS_64BIT_MODE; break;
1950 case '3': fFlags |= DBGF_DISAS_FLAGS_32BIT_MODE; break;
1951 case '1': fFlags |= DBGF_DISAS_FLAGS_16BIT_MODE; break;
1952 case 'v': fFlags |= DBGF_DISAS_FLAGS_16BIT_REAL_MODE; break;
1953 case 'c': fUseColor = true; break;
1954 }
1955
1956 /** @todo should use DBGFADDRESS for everything */
1957
1958 /*
1959 * Find address.
1960 */
1961 if (!cArgs)
1962 {
1963 if (!DBGCVAR_ISPOINTER(pDbgc->DisasmPos.enmType))
1964 {
1965 /** @todo Batch query CS, RIP, CPU mode and flags. */
1966 PVMCPU pVCpu = VMMR3GetCpuByIdU(pUVM, pDbgc->idCpu);
1967 if ( pDbgc->fRegCtxGuest
1968 && CPUMIsGuestIn64BitCode(pVCpu))
1969 {
1970 pDbgc->DisasmPos.enmType = DBGCVAR_TYPE_GC_FLAT;
1971 pDbgc->SourcePos.u.GCFlat = CPUMGetGuestRIP(pVCpu);
1972 }
1973 else
1974 {
1975 pDbgc->DisasmPos.enmType = DBGCVAR_TYPE_GC_FAR;
1976 pDbgc->SourcePos.u.GCFar.off = pDbgc->fRegCtxGuest ? CPUMGetGuestEIP(pVCpu) : CPUMGetHyperEIP(pVCpu);
1977 pDbgc->SourcePos.u.GCFar.sel = pDbgc->fRegCtxGuest ? CPUMGetGuestCS(pVCpu) : CPUMGetHyperCS(pVCpu);
1978 if ( (fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_DEFAULT_MODE
1979 && pDbgc->fRegCtxGuest
1980 && (CPUMGetGuestEFlags(pVCpu) & X86_EFL_VM))
1981 {
1982 fFlags &= ~DBGF_DISAS_FLAGS_MODE_MASK;
1983 fFlags |= DBGF_DISAS_FLAGS_16BIT_REAL_MODE;
1984 }
1985 }
1986
1987 if (pDbgc->fRegCtxGuest)
1988 fFlags |= DBGF_DISAS_FLAGS_CURRENT_GUEST;
1989 else
1990 fFlags |= DBGF_DISAS_FLAGS_CURRENT_HYPER | DBGF_DISAS_FLAGS_HYPER;
1991 }
1992 else if ((fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_DEFAULT_MODE && pDbgc->fDisasm)
1993 {
1994 fFlags &= ~DBGF_DISAS_FLAGS_MODE_MASK;
1995 fFlags |= pDbgc->fDisasm & (DBGF_DISAS_FLAGS_MODE_MASK | DBGF_DISAS_FLAGS_HYPER);
1996 }
1997 pDbgc->DisasmPos.enmRangeType = DBGCVAR_RANGE_NONE;
1998 }
1999 else
2000 pDbgc->DisasmPos = paArgs[0];
2001 pDbgc->pLastPos = &pDbgc->DisasmPos;
2002
2003 /*
2004 * Range.
2005 */
2006 switch (pDbgc->DisasmPos.enmRangeType)
2007 {
2008 case DBGCVAR_RANGE_NONE:
2009 pDbgc->DisasmPos.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
2010 pDbgc->DisasmPos.u64Range = 10;
2011 break;
2012
2013 case DBGCVAR_RANGE_ELEMENTS:
2014 if (pDbgc->DisasmPos.u64Range > 2048)
2015 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Too many lines requested. Max is 2048 lines");
2016 break;
2017
2018 case DBGCVAR_RANGE_BYTES:
2019 if (pDbgc->DisasmPos.u64Range > 65536)
2020 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The requested range is too big. Max is 64KB");
2021 break;
2022
2023 default:
2024 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Unknown range type %d", pDbgc->DisasmPos.enmRangeType);
2025 }
2026
2027 /*
2028 * Convert physical and host addresses to guest addresses.
2029 */
2030 RTDBGAS hDbgAs = pDbgc->hDbgAs;
2031 int rc;
2032 switch (pDbgc->DisasmPos.enmType)
2033 {
2034 case DBGCVAR_TYPE_GC_FLAT:
2035 case DBGCVAR_TYPE_GC_FAR:
2036 break;
2037 case DBGCVAR_TYPE_GC_PHYS:
2038 hDbgAs = DBGF_AS_PHYS;
2039 case DBGCVAR_TYPE_HC_FLAT:
2040 case DBGCVAR_TYPE_HC_PHYS:
2041 {
2042 DBGCVAR VarTmp;
2043 rc = DBGCCmdHlpEval(pCmdHlp, &VarTmp, "%%(%Dv)", &pDbgc->DisasmPos);
2044 if (RT_FAILURE(rc))
2045 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "failed to evaluate '%%(%Dv)'", &pDbgc->DisasmPos);
2046 pDbgc->DisasmPos = VarTmp;
2047 break;
2048 }
2049 default: AssertFailed(); break;
2050 }
2051
2052 DBGFADDRESS CurAddr;
2053 if ( (fFlags & DBGF_DISAS_FLAGS_MODE_MASK) == DBGF_DISAS_FLAGS_16BIT_REAL_MODE
2054 && pDbgc->DisasmPos.enmType == DBGCVAR_TYPE_GC_FAR)
2055 DBGFR3AddrFromFlat(pUVM, &CurAddr, ((uint32_t)pDbgc->DisasmPos.u.GCFar.sel << 4) + pDbgc->DisasmPos.u.GCFar.off);
2056 else
2057 {
2058 rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &pDbgc->DisasmPos, &CurAddr);
2059 if (RT_FAILURE(rc))
2060 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr failed on '%Dv'", &pDbgc->DisasmPos);
2061 }
2062
2063 DBGFFLOW hCfg;
2064 rc = DBGFR3FlowCreate(pUVM, pDbgc->idCpu, &CurAddr, 0 /*cbDisasmMax*/,
2065 DBGF_FLOW_CREATE_F_TRY_RESOLVE_INDIRECT_BRANCHES, fFlags, &hCfg);
2066 if (RT_SUCCESS(rc))
2067 {
2068 /* Dump the graph. */
2069 rc = dbgcCmdUnassembleCfgDump(hCfg, fUseColor, pCmdHlp);
2070 DBGFR3FlowRelease(hCfg);
2071 }
2072 else
2073 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGFR3FlowCreate failed on '%Dv'", &pDbgc->DisasmPos);
2074
2075 NOREF(pCmd);
2076 return rc;
2077}
2078
2079
2080/**
2081 * @callback_method_impl{FNDBGCCMD, The 'ls' command.}
2082 */
2083static DECLCALLBACK(int) dbgcCmdListSource(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2084{
2085 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2086
2087 /*
2088 * Validate input.
2089 */
2090 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs <= 1);
2091 if (cArgs == 1)
2092 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, DBGCVAR_ISPOINTER(paArgs[0].enmType));
2093 if (!pUVM && !cArgs && !DBGCVAR_ISPOINTER(pDbgc->SourcePos.enmType))
2094 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Don't know where to start listing...");
2095 if (!pUVM && cArgs && DBGCVAR_ISGCPOINTER(paArgs[0].enmType))
2096 return DBGCCmdHlpFail(pCmdHlp, pCmd, "GC address but no VM");
2097
2098 /*
2099 * Find address.
2100 */
2101 if (!cArgs)
2102 {
2103 if (!DBGCVAR_ISPOINTER(pDbgc->SourcePos.enmType))
2104 {
2105 PVMCPU pVCpu = VMMR3GetCpuByIdU(pUVM, pDbgc->idCpu);
2106 pDbgc->SourcePos.enmType = DBGCVAR_TYPE_GC_FAR;
2107 pDbgc->SourcePos.u.GCFar.off = pDbgc->fRegCtxGuest ? CPUMGetGuestEIP(pVCpu) : CPUMGetHyperEIP(pVCpu);
2108 pDbgc->SourcePos.u.GCFar.sel = pDbgc->fRegCtxGuest ? CPUMGetGuestCS(pVCpu) : CPUMGetHyperCS(pVCpu);
2109 }
2110 pDbgc->SourcePos.enmRangeType = DBGCVAR_RANGE_NONE;
2111 }
2112 else
2113 pDbgc->SourcePos = paArgs[0];
2114 pDbgc->pLastPos = &pDbgc->SourcePos;
2115
2116 /*
2117 * Ensure the source address is flat GC.
2118 */
2119 switch (pDbgc->SourcePos.enmType)
2120 {
2121 case DBGCVAR_TYPE_GC_FLAT:
2122 break;
2123 case DBGCVAR_TYPE_GC_PHYS:
2124 case DBGCVAR_TYPE_GC_FAR:
2125 case DBGCVAR_TYPE_HC_FLAT:
2126 case DBGCVAR_TYPE_HC_PHYS:
2127 {
2128 int rc = DBGCCmdHlpEval(pCmdHlp, &pDbgc->SourcePos, "%%(%Dv)", &pDbgc->SourcePos);
2129 if (RT_FAILURE(rc))
2130 return DBGCCmdHlpPrintf(pCmdHlp, "error: Invalid address or address type. (rc=%d)\n", rc);
2131 break;
2132 }
2133 default: AssertFailed(); break;
2134 }
2135
2136 /*
2137 * Range.
2138 */
2139 switch (pDbgc->SourcePos.enmRangeType)
2140 {
2141 case DBGCVAR_RANGE_NONE:
2142 pDbgc->SourcePos.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
2143 pDbgc->SourcePos.u64Range = 10;
2144 break;
2145
2146 case DBGCVAR_RANGE_ELEMENTS:
2147 if (pDbgc->SourcePos.u64Range > 2048)
2148 return DBGCCmdHlpPrintf(pCmdHlp, "error: Too many lines requested. Max is 2048 lines.\n");
2149 break;
2150
2151 case DBGCVAR_RANGE_BYTES:
2152 if (pDbgc->SourcePos.u64Range > 65536)
2153 return DBGCCmdHlpPrintf(pCmdHlp, "error: The requested range is too big. Max is 64KB.\n");
2154 break;
2155
2156 default:
2157 return DBGCCmdHlpPrintf(pCmdHlp, "internal error: Unknown range type %d.\n", pDbgc->SourcePos.enmRangeType);
2158 }
2159
2160 /*
2161 * Do the disassembling.
2162 */
2163 bool fFirst = 1;
2164 RTDBGLINE LinePrev = { 0, 0, 0, 0, 0, "" };
2165 int iRangeLeft = (int)pDbgc->SourcePos.u64Range;
2166 if (iRangeLeft == 0) /* kludge for 'r'. */
2167 iRangeLeft = -1;
2168 for (;;)
2169 {
2170 /*
2171 * Get line info.
2172 */
2173 RTDBGLINE Line;
2174 RTGCINTPTR off;
2175 DBGFADDRESS SourcePosAddr;
2176 int rc = DBGCCmdHlpVarToDbgfAddr(pCmdHlp, &pDbgc->SourcePos, &SourcePosAddr);
2177 if (RT_FAILURE(rc))
2178 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToDbgfAddr(,%Dv)", &pDbgc->SourcePos);
2179 rc = DBGFR3AsLineByAddr(pUVM, pDbgc->hDbgAs, &SourcePosAddr, &off, &Line, NULL);
2180 if (RT_FAILURE(rc))
2181 return VINF_SUCCESS;
2182
2183 unsigned cLines = 0;
2184 if (memcmp(&Line, &LinePrev, sizeof(Line)))
2185 {
2186 /*
2187 * Print filenamename
2188 */
2189 if (!fFirst && strcmp(Line.szFilename, LinePrev.szFilename))
2190 fFirst = true;
2191 if (fFirst)
2192 {
2193 rc = DBGCCmdHlpPrintf(pCmdHlp, "[%s @ %d]\n", Line.szFilename, Line.uLineNo);
2194 if (RT_FAILURE(rc))
2195 return rc;
2196 }
2197
2198 /*
2199 * Try open the file and read the line.
2200 */
2201 FILE *phFile = fopen(Line.szFilename, "r");
2202 if (phFile)
2203 {
2204 /* Skip ahead to the desired line. */
2205 char szLine[4096];
2206 unsigned cBefore = fFirst ? RT_MIN(2, Line.uLineNo - 1) : Line.uLineNo - LinePrev.uLineNo - 1;
2207 if (cBefore > 7)
2208 cBefore = 0;
2209 unsigned cLeft = Line.uLineNo - cBefore;
2210 while (cLeft > 0)
2211 {
2212 szLine[0] = '\0';
2213 if (!fgets(szLine, sizeof(szLine), phFile))
2214 break;
2215 cLeft--;
2216 }
2217 if (!cLeft)
2218 {
2219 /* print the before lines */
2220 for (;;)
2221 {
2222 size_t cch = strlen(szLine);
2223 while (cch > 0 && (szLine[cch - 1] == '\r' || szLine[cch - 1] == '\n' || RT_C_IS_SPACE(szLine[cch - 1])) )
2224 szLine[--cch] = '\0';
2225 if (cBefore-- <= 0)
2226 break;
2227
2228 rc = DBGCCmdHlpPrintf(pCmdHlp, " %4d: %s\n", Line.uLineNo - cBefore - 1, szLine);
2229 szLine[0] = '\0';
2230 const char *pszShutUpGcc = fgets(szLine, sizeof(szLine), phFile); NOREF(pszShutUpGcc);
2231 cLines++;
2232 }
2233 /* print the actual line */
2234 rc = DBGCCmdHlpPrintf(pCmdHlp, "%08llx %4d: %s\n", Line.Address, Line.uLineNo, szLine);
2235 }
2236 fclose(phFile);
2237 if (RT_FAILURE(rc))
2238 return rc;
2239 fFirst = false;
2240 }
2241 else
2242 return DBGCCmdHlpPrintf(pCmdHlp, "Warning: couldn't open source file '%s'\n", Line.szFilename);
2243
2244 LinePrev = Line;
2245 }
2246
2247
2248 /*
2249 * Advance
2250 */
2251 if (iRangeLeft < 0) /* 'r' */
2252 break;
2253 if (pDbgc->SourcePos.enmRangeType == DBGCVAR_RANGE_ELEMENTS)
2254 iRangeLeft -= cLines;
2255 else
2256 iRangeLeft -= 1;
2257 rc = DBGCCmdHlpEval(pCmdHlp, &pDbgc->SourcePos, "(%Dv) + %x", &pDbgc->SourcePos, 1);
2258 if (RT_FAILURE(rc))
2259 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Expression: (%Dv) + %x\n", &pDbgc->SourcePos, 1);
2260 if (iRangeLeft <= 0)
2261 break;
2262 }
2263
2264 NOREF(pCmd);
2265 return 0;
2266}
2267
2268
2269/**
2270 * @callback_method_impl{FNDBGCCMD, The 'r' command.}
2271 */
2272static DECLCALLBACK(int) dbgcCmdReg(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2273{
2274 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2275 if (!pDbgc->fRegCtxGuest)
2276 return dbgcCmdRegHyper(pCmd, pCmdHlp, pUVM, paArgs, cArgs);
2277 return dbgcCmdRegGuest(pCmd, pCmdHlp, pUVM, paArgs, cArgs);
2278}
2279
2280
2281/**
2282 * @callback_method_impl{FNDBGCCMD, Common worker for the dbgcCmdReg*()
2283 * commands.}
2284 */
2285static DECLCALLBACK(int) dbgcCmdRegCommon(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs,
2286 const char *pszPrefix)
2287{
2288 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2289 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs == 1 || cArgs == 2 || cArgs == 3);
2290 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, paArgs[0].enmType == DBGCVAR_TYPE_STRING
2291 || paArgs[0].enmType == DBGCVAR_TYPE_SYMBOL);
2292
2293 /*
2294 * Parse the register name and kind.
2295 */
2296 const char *pszReg = paArgs[0].u.pszString;
2297 if (*pszReg == '@')
2298 pszReg++;
2299 VMCPUID idCpu = pDbgc->idCpu;
2300 if (*pszPrefix)
2301 idCpu |= DBGFREG_HYPER_VMCPUID;
2302 if (*pszReg == '.')
2303 {
2304 pszReg++;
2305 idCpu |= DBGFREG_HYPER_VMCPUID;
2306 }
2307 const char * const pszActualPrefix = idCpu & DBGFREG_HYPER_VMCPUID ? "." : "";
2308
2309 /*
2310 * Query the register type & value (the setter needs the type).
2311 */
2312 DBGFREGVALTYPE enmType;
2313 DBGFREGVAL Value;
2314 int rc = DBGFR3RegNmQuery(pUVM, idCpu, pszReg, &Value, &enmType);
2315 if (RT_FAILURE(rc))
2316 {
2317 if (rc == VERR_DBGF_REGISTER_NOT_FOUND)
2318 return DBGCCmdHlpVBoxError(pCmdHlp, VERR_INVALID_PARAMETER, "Unknown register: '%s%s'.\n",
2319 pszActualPrefix, pszReg);
2320 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegNmQuery failed querying '%s%s': %Rrc.\n",
2321 pszActualPrefix, pszReg, rc);
2322 }
2323 if (cArgs == 1)
2324 {
2325 /*
2326 * Show the register.
2327 */
2328 char szValue[160];
2329 rc = DBGFR3RegFormatValue(szValue, sizeof(szValue), &Value, enmType, true /*fSpecial*/);
2330 if (RT_SUCCESS(rc))
2331 rc = DBGCCmdHlpPrintf(pCmdHlp, "%s%s=%s\n", pszActualPrefix, pszReg, szValue);
2332 else
2333 rc = DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegFormatValue failed: %Rrc.\n", rc);
2334 }
2335 else
2336 {
2337 DBGCVAR NewValueTmp;
2338 PCDBGCVAR pNewValue;
2339 if (cArgs == 3)
2340 {
2341 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 1, paArgs[1].enmType == DBGCVAR_TYPE_STRING);
2342 if (strcmp(paArgs[1].u.pszString, "="))
2343 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Second argument must be '='.");
2344 pNewValue = &paArgs[2];
2345 }
2346 else
2347 {
2348 /* Not possible to convince the parser to support both codeview and
2349 windbg syntax and make the equal sign optional. Try help it. */
2350 /** @todo make DBGCCmdHlpConvert do more with strings. */
2351 rc = DBGCCmdHlpConvert(pCmdHlp, &paArgs[1], DBGCVAR_TYPE_NUMBER, true /*fConvSyms*/, &NewValueTmp);
2352 if (RT_FAILURE(rc))
2353 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "The last argument must be a value or valid symbol.");
2354 pNewValue = &NewValueTmp;
2355 }
2356
2357 /*
2358 * Modify the register.
2359 */
2360 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 1, pNewValue->enmType == DBGCVAR_TYPE_NUMBER);
2361 if (enmType != DBGFREGVALTYPE_DTR)
2362 {
2363 enmType = DBGFREGVALTYPE_U64;
2364 rc = DBGCCmdHlpVarToNumber(pCmdHlp, pNewValue, &Value.u64);
2365 }
2366 else
2367 {
2368 enmType = DBGFREGVALTYPE_DTR;
2369 rc = DBGCCmdHlpVarToNumber(pCmdHlp, pNewValue, &Value.dtr.u64Base);
2370 if (RT_SUCCESS(rc) && pNewValue->enmRangeType != DBGCVAR_RANGE_NONE)
2371 Value.dtr.u32Limit = (uint32_t)pNewValue->u64Range;
2372 }
2373 if (RT_SUCCESS(rc))
2374 {
2375 rc = DBGFR3RegNmSet(pUVM, idCpu, pszReg, &Value, enmType);
2376 if (RT_FAILURE(rc))
2377 rc = DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegNmSet failed settings '%s%s': %Rrc\n",
2378 pszActualPrefix, pszReg, rc);
2379 if (rc != VINF_SUCCESS)
2380 DBGCCmdHlpPrintf(pCmdHlp, "%s: warning: %Rrc\n", pCmd->pszCmd, rc);
2381 }
2382 else
2383 rc = DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegFormatValue failed: %Rrc.\n", rc);
2384 }
2385 return rc;
2386}
2387
2388
2389/**
2390 * @callback_method_impl{FNDBGCCMD,
2391 * The 'rg'\, 'rg64' and 'rg32' commands\, worker for 'r'.}
2392 */
2393static DECLCALLBACK(int) dbgcCmdRegGuest(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2394{
2395 /*
2396 * Show all registers our selves.
2397 */
2398 if (cArgs == 0)
2399 {
2400 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2401 bool const f64BitMode = !strcmp(pCmd->pszCmd, "rg64")
2402 || ( strcmp(pCmd->pszCmd, "rg32") != 0
2403 && DBGFR3CpuIsIn64BitCode(pUVM, pDbgc->idCpu));
2404 char szDisAndRegs[8192];
2405 int rc;
2406
2407 if (pDbgc->fRegTerse)
2408 {
2409 if (f64BitMode)
2410 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu, &szDisAndRegs[0], sizeof(szDisAndRegs),
2411 "u %016VR{rip} L 0\n"
2412 "rax=%016VR{rax} rbx=%016VR{rbx} rcx=%016VR{rcx} rdx=%016VR{rdx}\n"
2413 "rsi=%016VR{rsi} rdi=%016VR{rdi} r8 =%016VR{r8} r9 =%016VR{r9}\n"
2414 "r10=%016VR{r10} r11=%016VR{r11} r12=%016VR{r12} r13=%016VR{r13}\n"
2415 "r14=%016VR{r14} r15=%016VR{r15} %VRF{rflags}\n"
2416 "rip=%016VR{rip} rsp=%016VR{rsp} rbp=%016VR{rbp}\n"
2417 "cs=%04VR{cs} ds=%04VR{ds} es=%04VR{es} fs=%04VR{fs} gs=%04VR{gs} ss=%04VR{ss} rflags=%08VR{rflags}\n");
2418 else
2419 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu, szDisAndRegs, sizeof(szDisAndRegs),
2420 "u %04VR{cs}:%08VR{eip} L 0\n"
2421 "eax=%08VR{eax} ebx=%08VR{ebx} ecx=%08VR{ecx} edx=%08VR{edx} esi=%08VR{esi} edi=%08VR{edi}\n"
2422 "eip=%08VR{eip} esp=%08VR{esp} ebp=%08VR{ebp} %VRF{eflags}\n"
2423 "cs=%04VR{cs} ds=%04VR{ds} es=%04VR{es} fs=%04VR{fs} gs=%04VR{gs} ss=%04VR{ss} eflags=%08VR{eflags}\n");
2424 }
2425 else
2426 {
2427 if (f64BitMode)
2428 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu, &szDisAndRegs[0], sizeof(szDisAndRegs),
2429 "u %016VR{rip} L 0\n"
2430 "rax=%016VR{rax} rbx=%016VR{rbx} rcx=%016VR{rcx} rdx=%016VR{rdx}\n"
2431 "rsi=%016VR{rsi} rdi=%016VR{rdi} r8 =%016VR{r8} r9 =%016VR{r9}\n"
2432 "r10=%016VR{r10} r11=%016VR{r11} r12=%016VR{r12} r13=%016VR{r13}\n"
2433 "r14=%016VR{r14} r15=%016VR{r15} %VRF{rflags}\n"
2434 "rip=%016VR{rip} rsp=%016VR{rsp} rbp=%016VR{rbp}\n"
2435 "cs={%04VR{cs} base=%016VR{cs_base} limit=%08VR{cs_lim} flags=%04VR{cs_attr}} cr0=%016VR{cr0}\n"
2436 "ds={%04VR{ds} base=%016VR{ds_base} limit=%08VR{ds_lim} flags=%04VR{ds_attr}} cr2=%016VR{cr2}\n"
2437 "es={%04VR{es} base=%016VR{es_base} limit=%08VR{es_lim} flags=%04VR{es_attr}} cr3=%016VR{cr3}\n"
2438 "fs={%04VR{fs} base=%016VR{fs_base} limit=%08VR{fs_lim} flags=%04VR{fs_attr}} cr4=%016VR{cr4}\n"
2439 "gs={%04VR{gs} base=%016VR{gs_base} limit=%08VR{gs_lim} flags=%04VR{gs_attr}} cr8=%016VR{cr8}\n"
2440 "ss={%04VR{ss} base=%016VR{ss_base} limit=%08VR{ss_lim} flags=%04VR{ss_attr}}\n"
2441 "dr0=%016VR{dr0} dr1=%016VR{dr1} dr2=%016VR{dr2} dr3=%016VR{dr3}\n"
2442 "dr6=%016VR{dr6} dr7=%016VR{dr7}\n"
2443 "gdtr=%016VR{gdtr_base}:%04VR{gdtr_lim} idtr=%016VR{idtr_base}:%04VR{idtr_lim} rflags=%08VR{rflags}\n"
2444 "ldtr={%04VR{ldtr} base=%016VR{ldtr_base} limit=%08VR{ldtr_lim} flags=%08VR{ldtr_attr}}\n"
2445 "tr ={%04VR{tr} base=%016VR{tr_base} limit=%08VR{tr_lim} flags=%08VR{tr_attr}}\n"
2446 " sysenter={cs=%04VR{sysenter_cs} eip=%08VR{sysenter_eip} esp=%08VR{sysenter_esp}}\n"
2447 " efer=%016VR{efer}\n"
2448 " pat=%016VR{pat}\n"
2449 " sf_mask=%016VR{sf_mask}\n"
2450 "krnl_gs_base=%016VR{krnl_gs_base}\n"
2451 " lstar=%016VR{lstar}\n"
2452 " star=%016VR{star} cstar=%016VR{cstar}\n"
2453 "fcw=%04VR{fcw} fsw=%04VR{fsw} ftw=%04VR{ftw} mxcsr=%04VR{mxcsr} mxcsr_mask=%04VR{mxcsr_mask}\n"
2454 );
2455 else
2456 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu, szDisAndRegs, sizeof(szDisAndRegs),
2457 "u %04VR{cs}:%08VR{eip} L 0\n"
2458 "eax=%08VR{eax} ebx=%08VR{ebx} ecx=%08VR{ecx} edx=%08VR{edx} esi=%08VR{esi} edi=%08VR{edi}\n"
2459 "eip=%08VR{eip} esp=%08VR{esp} ebp=%08VR{ebp} %VRF{eflags}\n"
2460 "cs={%04VR{cs} base=%08VR{cs_base} limit=%08VR{cs_lim} flags=%04VR{cs_attr}} dr0=%08VR{dr0} dr1=%08VR{dr1}\n"
2461 "ds={%04VR{ds} base=%08VR{ds_base} limit=%08VR{ds_lim} flags=%04VR{ds_attr}} dr2=%08VR{dr2} dr3=%08VR{dr3}\n"
2462 "es={%04VR{es} base=%08VR{es_base} limit=%08VR{es_lim} flags=%04VR{es_attr}} dr6=%08VR{dr6} dr7=%08VR{dr7}\n"
2463 "fs={%04VR{fs} base=%08VR{fs_base} limit=%08VR{fs_lim} flags=%04VR{fs_attr}} cr0=%08VR{cr0} cr2=%08VR{cr2}\n"
2464 "gs={%04VR{gs} base=%08VR{gs_base} limit=%08VR{gs_lim} flags=%04VR{gs_attr}} cr3=%08VR{cr3} cr4=%08VR{cr4}\n"
2465 "ss={%04VR{ss} base=%08VR{ss_base} limit=%08VR{ss_lim} flags=%04VR{ss_attr}} cr8=%08VR{cr8}\n"
2466 "gdtr=%08VR{gdtr_base}:%04VR{gdtr_lim} idtr=%08VR{idtr_base}:%04VR{idtr_lim} eflags=%08VR{eflags}\n"
2467 "ldtr={%04VR{ldtr} base=%08VR{ldtr_base} limit=%08VR{ldtr_lim} flags=%04VR{ldtr_attr}}\n"
2468 "tr ={%04VR{tr} base=%08VR{tr_base} limit=%08VR{tr_lim} flags=%04VR{tr_attr}}\n"
2469 "sysenter={cs=%04VR{sysenter_cs} eip=%08VR{sysenter_eip} esp=%08VR{sysenter_esp}}\n"
2470 "fcw=%04VR{fcw} fsw=%04VR{fsw} ftw=%04VR{ftw} mxcsr=%04VR{mxcsr} mxcsr_mask=%04VR{mxcsr_mask}\n"
2471 );
2472 }
2473 if (RT_FAILURE(rc))
2474 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegPrintf failed");
2475 char *pszRegs = strchr(szDisAndRegs, '\n');
2476 *pszRegs++ = '\0';
2477 rc = DBGCCmdHlpPrintf(pCmdHlp, "%s", pszRegs);
2478
2479 /*
2480 * Disassemble one instruction at cs:[r|e]ip.
2481 */
2482 if (!f64BitMode && strstr(pszRegs, " vm ")) /* a big ugly... */
2483 return pCmdHlp->pfnExec(pCmdHlp, "uv86 %s", szDisAndRegs + 2);
2484 return pCmdHlp->pfnExec(pCmdHlp, "%s", szDisAndRegs);
2485 }
2486 return dbgcCmdRegCommon(pCmd, pCmdHlp, pUVM, paArgs, cArgs, "");
2487}
2488
2489
2490/**
2491 * @callback_method_impl{FNDBGCCMD, The 'rh' command.}
2492 */
2493static DECLCALLBACK(int) dbgcCmdRegHyper(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2494{
2495 /*
2496 * Show all registers our selves.
2497 */
2498 if (cArgs == 0)
2499 {
2500 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2501 char szDisAndRegs[8192];
2502 int rc;
2503
2504 if (pDbgc->fRegTerse)
2505 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu | DBGFREG_HYPER_VMCPUID, szDisAndRegs, sizeof(szDisAndRegs),
2506 "u %VR{cs}:%VR{eip} L 0\n"
2507 ".eax=%08VR{eax} .ebx=%08VR{ebx} .ecx=%08VR{ecx} .edx=%08VR{edx} .esi=%08VR{esi} .edi=%08VR{edi}\n"
2508 ".eip=%08VR{eip} .esp=%08VR{esp} .ebp=%08VR{ebp} .%VRF{eflags}\n"
2509 ".cs=%04VR{cs} .ds=%04VR{ds} .es=%04VR{es} .fs=%04VR{fs} .gs=%04VR{gs} .ss=%04VR{ss} .eflags=%08VR{eflags}\n");
2510 else
2511 rc = DBGFR3RegPrintf(pUVM, pDbgc->idCpu | DBGFREG_HYPER_VMCPUID, szDisAndRegs, sizeof(szDisAndRegs),
2512 "u %04VR{cs}:%08VR{eip} L 0\n"
2513 ".eax=%08VR{eax} .ebx=%08VR{ebx} .ecx=%08VR{ecx} .edx=%08VR{edx} .esi=%08VR{esi} .edi=%08VR{edi}\n"
2514 ".eip=%08VR{eip} .esp=%08VR{esp} .ebp=%08VR{ebp} .%VRF{eflags}\n"
2515 ".cs={%04VR{cs} base=%08VR{cs_base} limit=%08VR{cs_lim} flags=%04VR{cs_attr}} .dr0=%08VR{dr0} .dr1=%08VR{dr1}\n"
2516 ".ds={%04VR{ds} base=%08VR{ds_base} limit=%08VR{ds_lim} flags=%04VR{ds_attr}} .dr2=%08VR{dr2} .dr3=%08VR{dr3}\n"
2517 ".es={%04VR{es} base=%08VR{es_base} limit=%08VR{es_lim} flags=%04VR{es_attr}} .dr6=%08VR{dr6} .dr6=%08VR{dr6}\n"
2518 ".fs={%04VR{fs} base=%08VR{fs_base} limit=%08VR{fs_lim} flags=%04VR{fs_attr}} .cr3=%016VR{cr3}\n"
2519 ".gs={%04VR{gs} base=%08VR{gs_base} limit=%08VR{gs_lim} flags=%04VR{gs_attr}}\n"
2520 ".ss={%04VR{ss} base=%08VR{ss_base} limit=%08VR{ss_lim} flags=%04VR{ss_attr}}\n"
2521 ".gdtr=%08VR{gdtr_base}:%04VR{gdtr_lim} .idtr=%08VR{idtr_base}:%04VR{idtr_lim} .eflags=%08VR{eflags}\n"
2522 ".ldtr={%04VR{ldtr} base=%08VR{ldtr_base} limit=%08VR{ldtr_lim} flags=%04VR{ldtr_attr}}\n"
2523 ".tr ={%04VR{tr} base=%08VR{tr_base} limit=%08VR{tr_lim} flags=%04VR{tr_attr}}\n"
2524 );
2525 if (RT_FAILURE(rc))
2526 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "DBGFR3RegPrintf failed");
2527 char *pszRegs = strchr(szDisAndRegs, '\n');
2528 *pszRegs++ = '\0';
2529 rc = DBGCCmdHlpPrintf(pCmdHlp, "%s", pszRegs);
2530
2531 /*
2532 * Disassemble one instruction at cs:[r|e]ip.
2533 */
2534 return pCmdHlp->pfnExec(pCmdHlp, "%s", szDisAndRegs);
2535 }
2536 return dbgcCmdRegCommon(pCmd, pCmdHlp, pUVM, paArgs, cArgs, ".");
2537}
2538
2539
2540/**
2541 * @callback_method_impl{FNDBGCCMD, The 'rt' command.}
2542 */
2543static DECLCALLBACK(int) dbgcCmdRegTerse(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2544{
2545 NOREF(pCmd); NOREF(pUVM); NOREF(paArgs); NOREF(cArgs);
2546
2547 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2548 pDbgc->fRegTerse = !pDbgc->fRegTerse;
2549 return DBGCCmdHlpPrintf(pCmdHlp, pDbgc->fRegTerse ? "info: Terse register info.\n" : "info: Verbose register info.\n");
2550}
2551
2552
2553/**
2554 * @callback_method_impl{FNDBGCCMD, The 't' command.}
2555 */
2556static DECLCALLBACK(int) dbgcCmdTrace(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2557{
2558 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2559
2560 int rc = DBGFR3Step(pUVM, pDbgc->idCpu);
2561 if (RT_SUCCESS(rc))
2562 pDbgc->fReady = false;
2563 else
2564 rc = pDbgc->CmdHlp.pfnVBoxError(&pDbgc->CmdHlp, rc, "When trying to single step VM %p\n", pDbgc->pVM);
2565
2566 NOREF(pCmd); NOREF(paArgs); NOREF(cArgs);
2567 return rc;
2568}
2569
2570
2571/**
2572 * @callback_method_impl{FNDBGCCMD, The 'k'\, 'kg' and 'kh' commands.}
2573 */
2574static DECLCALLBACK(int) dbgcCmdStack(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2575{
2576 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2577
2578 /*
2579 * Figure which context we're called for and start walking that stack.
2580 */
2581 int rc;
2582 PCDBGFSTACKFRAME pFirstFrame;
2583 bool const fGuest = pCmd->pszCmd[1] == 'g'
2584 || (!pCmd->pszCmd[1] && pDbgc->fRegCtxGuest);
2585 rc = DBGFR3StackWalkBegin(pUVM, pDbgc->idCpu, fGuest ? DBGFCODETYPE_GUEST : DBGFCODETYPE_HYPER, &pFirstFrame);
2586 if (RT_FAILURE(rc))
2587 return DBGCCmdHlpPrintf(pCmdHlp, "Failed to begin stack walk, rc=%Rrc\n", rc);
2588
2589 /*
2590 * Print header.
2591 * 12345678 12345678 0023:87654321 12345678 87654321 12345678 87654321 symbol
2592 */
2593 uint32_t fBitFlags = 0;
2594 for (PCDBGFSTACKFRAME pFrame = pFirstFrame;
2595 pFrame;
2596 pFrame = DBGFR3StackWalkNext(pFrame))
2597 {
2598 uint32_t const fCurBitFlags = pFrame->fFlags & (DBGFSTACKFRAME_FLAGS_16BIT | DBGFSTACKFRAME_FLAGS_32BIT | DBGFSTACKFRAME_FLAGS_64BIT);
2599 if (fCurBitFlags & DBGFSTACKFRAME_FLAGS_16BIT)
2600 {
2601 if (fCurBitFlags != fBitFlags)
2602 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "SS:BP Ret SS:BP Ret CS:EIP Arg0 Arg1 Arg2 Arg3 CS:EIP / Symbol [line]\n");
2603 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04RX16:%04RX16 %04RX16:%04RX16 %04RX32:%08RX32 %08RX32 %08RX32 %08RX32 %08RX32",
2604 pFrame->AddrFrame.Sel,
2605 (uint16_t)pFrame->AddrFrame.off,
2606 pFrame->AddrReturnFrame.Sel,
2607 (uint16_t)pFrame->AddrReturnFrame.off,
2608 (uint32_t)pFrame->AddrReturnPC.Sel,
2609 (uint32_t)pFrame->AddrReturnPC.off,
2610 pFrame->Args.au32[0],
2611 pFrame->Args.au32[1],
2612 pFrame->Args.au32[2],
2613 pFrame->Args.au32[3]);
2614 }
2615 else if (fCurBitFlags & DBGFSTACKFRAME_FLAGS_32BIT)
2616 {
2617 if (fCurBitFlags != fBitFlags)
2618 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "EBP Ret EBP Ret CS:EIP Arg0 Arg1 Arg2 Arg3 CS:EIP / Symbol [line]\n");
2619 rc = DBGCCmdHlpPrintf(pCmdHlp, "%08RX32 %08RX32 %04RX32:%08RX32 %08RX32 %08RX32 %08RX32 %08RX32",
2620 (uint32_t)pFrame->AddrFrame.off,
2621 (uint32_t)pFrame->AddrReturnFrame.off,
2622 (uint32_t)pFrame->AddrReturnPC.Sel,
2623 (uint32_t)pFrame->AddrReturnPC.off,
2624 pFrame->Args.au32[0],
2625 pFrame->Args.au32[1],
2626 pFrame->Args.au32[2],
2627 pFrame->Args.au32[3]);
2628 }
2629 else if (fCurBitFlags & DBGFSTACKFRAME_FLAGS_64BIT)
2630 {
2631 if (fCurBitFlags != fBitFlags)
2632 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "RBP Ret SS:RBP Ret RIP CS:RIP / Symbol [line]\n");
2633 rc = DBGCCmdHlpPrintf(pCmdHlp, "%016RX64 %04RX16:%016RX64 %016RX64",
2634 (uint64_t)pFrame->AddrFrame.off,
2635 pFrame->AddrReturnFrame.Sel,
2636 (uint64_t)pFrame->AddrReturnFrame.off,
2637 (uint64_t)pFrame->AddrReturnPC.off);
2638 }
2639 if (RT_FAILURE(rc))
2640 break;
2641 if (!pFrame->pSymPC)
2642 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
2643 fCurBitFlags & DBGFSTACKFRAME_FLAGS_64BIT
2644 ? " %RTsel:%016RGv"
2645 : fCurBitFlags & DBGFSTACKFRAME_FLAGS_32BIT
2646 ? " %RTsel:%08RGv"
2647 : " %RTsel:%04RGv"
2648 , pFrame->AddrPC.Sel, pFrame->AddrPC.off);
2649 else
2650 {
2651 RTGCINTPTR offDisp = pFrame->AddrPC.FlatPtr - pFrame->pSymPC->Value; /** @todo this isn't 100% correct for segmented stuff. */
2652 if (offDisp > 0)
2653 rc = DBGCCmdHlpPrintf(pCmdHlp, " %s+%llx", pFrame->pSymPC->szName, (int64_t)offDisp);
2654 else if (offDisp < 0)
2655 rc = DBGCCmdHlpPrintf(pCmdHlp, " %s-%llx", pFrame->pSymPC->szName, -(int64_t)offDisp);
2656 else
2657 rc = DBGCCmdHlpPrintf(pCmdHlp, " %s", pFrame->pSymPC->szName);
2658 }
2659 if (RT_SUCCESS(rc) && pFrame->pLinePC)
2660 rc = DBGCCmdHlpPrintf(pCmdHlp, " [%s @ 0i%d]", pFrame->pLinePC->szFilename, pFrame->pLinePC->uLineNo);
2661 if (RT_SUCCESS(rc))
2662 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
2663 if (RT_FAILURE(rc))
2664 break;
2665
2666 fBitFlags = fCurBitFlags;
2667 }
2668
2669 DBGFR3StackWalkEnd(pFirstFrame);
2670
2671 NOREF(paArgs); NOREF(cArgs);
2672 return rc;
2673}
2674
2675
2676static int dbgcCmdDumpDTWorker64(PDBGCCMDHLP pCmdHlp, PCX86DESC64 pDesc, unsigned iEntry, bool fHyper, bool *pfDblEntry)
2677{
2678 /* GUEST64 */
2679 int rc;
2680
2681 const char *pszHyper = fHyper ? " HYPER" : "";
2682 const char *pszPresent = pDesc->Gen.u1Present ? "P " : "NP";
2683 if (pDesc->Gen.u1DescType)
2684 {
2685 static const char * const s_apszTypes[] =
2686 {
2687 "DataRO", /* 0 Read-Only */
2688 "DataRO", /* 1 Read-Only - Accessed */
2689 "DataRW", /* 2 Read/Write */
2690 "DataRW", /* 3 Read/Write - Accessed */
2691 "DownRO", /* 4 Expand-down, Read-Only */
2692 "DownRO", /* 5 Expand-down, Read-Only - Accessed */
2693 "DownRW", /* 6 Expand-down, Read/Write */
2694 "DownRW", /* 7 Expand-down, Read/Write - Accessed */
2695 "CodeEO", /* 8 Execute-Only */
2696 "CodeEO", /* 9 Execute-Only - Accessed */
2697 "CodeER", /* A Execute/Readable */
2698 "CodeER", /* B Execute/Readable - Accessed */
2699 "ConfE0", /* C Conforming, Execute-Only */
2700 "ConfE0", /* D Conforming, Execute-Only - Accessed */
2701 "ConfER", /* E Conforming, Execute/Readable */
2702 "ConfER" /* F Conforming, Execute/Readable - Accessed */
2703 };
2704 const char *pszAccessed = pDesc->Gen.u4Type & RT_BIT(0) ? "A " : "NA";
2705 const char *pszGranularity = pDesc->Gen.u1Granularity ? "G" : " ";
2706 const char *pszBig = pDesc->Gen.u1DefBig ? "BIG" : " ";
2707 uint32_t u32Base = X86DESC_BASE(pDesc);
2708 uint32_t cbLimit = X86DESC_LIMIT_G(pDesc);
2709
2710 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Bas=%08x Lim=%08x DPL=%d %s %s %s %s AVL=%d L=%d%s\n",
2711 iEntry, s_apszTypes[pDesc->Gen.u4Type], u32Base, cbLimit,
2712 pDesc->Gen.u2Dpl, pszPresent, pszAccessed, pszGranularity, pszBig,
2713 pDesc->Gen.u1Available, pDesc->Gen.u1Long, pszHyper);
2714 }
2715 else
2716 {
2717 static const char * const s_apszTypes[] =
2718 {
2719 "Ill-0 ", /* 0 0000 Reserved (Illegal) */
2720 "Ill-1 ", /* 1 0001 Available 16-bit TSS */
2721 "LDT ", /* 2 0010 LDT */
2722 "Ill-3 ", /* 3 0011 Busy 16-bit TSS */
2723 "Ill-4 ", /* 4 0100 16-bit Call Gate */
2724 "Ill-5 ", /* 5 0101 Task Gate */
2725 "Ill-6 ", /* 6 0110 16-bit Interrupt Gate */
2726 "Ill-7 ", /* 7 0111 16-bit Trap Gate */
2727 "Ill-8 ", /* 8 1000 Reserved (Illegal) */
2728 "Tss64A", /* 9 1001 Available 32-bit TSS */
2729 "Ill-A ", /* A 1010 Reserved (Illegal) */
2730 "Tss64B", /* B 1011 Busy 32-bit TSS */
2731 "Call64", /* C 1100 32-bit Call Gate */
2732 "Ill-D ", /* D 1101 Reserved (Illegal) */
2733 "Int64 ", /* E 1110 32-bit Interrupt Gate */
2734 "Trap64" /* F 1111 32-bit Trap Gate */
2735 };
2736 switch (pDesc->Gen.u4Type)
2737 {
2738 /* raw */
2739 case X86_SEL_TYPE_SYS_UNDEFINED:
2740 case X86_SEL_TYPE_SYS_UNDEFINED2:
2741 case X86_SEL_TYPE_SYS_UNDEFINED4:
2742 case X86_SEL_TYPE_SYS_UNDEFINED3:
2743 case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
2744 case X86_SEL_TYPE_SYS_286_TSS_BUSY:
2745 case X86_SEL_TYPE_SYS_286_CALL_GATE:
2746 case X86_SEL_TYPE_SYS_286_INT_GATE:
2747 case X86_SEL_TYPE_SYS_286_TRAP_GATE:
2748 case X86_SEL_TYPE_SYS_TASK_GATE:
2749 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s %.8Rhxs DPL=%d %s%s\n",
2750 iEntry, s_apszTypes[pDesc->Gen.u4Type], pDesc,
2751 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
2752 break;
2753
2754 case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
2755 case X86_SEL_TYPE_SYS_386_TSS_BUSY:
2756 case X86_SEL_TYPE_SYS_LDT:
2757 {
2758 const char *pszBusy = pDesc->Gen.u4Type & RT_BIT(1) ? "B " : "NB";
2759 const char *pszBig = pDesc->Gen.u1DefBig ? "BIG" : " ";
2760 const char *pszLong = pDesc->Gen.u1Long ? "LONG" : " ";
2761
2762 uint64_t u64Base = X86DESC64_BASE(pDesc);
2763 uint32_t cbLimit = X86DESC_LIMIT_G(pDesc);
2764
2765 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Bas=%016RX64 Lim=%08x DPL=%d %s %s %s %sAVL=%d R=%d%s\n",
2766 iEntry, s_apszTypes[pDesc->Gen.u4Type], u64Base, cbLimit,
2767 pDesc->Gen.u2Dpl, pszPresent, pszBusy, pszLong, pszBig,
2768 pDesc->Gen.u1Available, pDesc->Gen.u1Long | (pDesc->Gen.u1DefBig << 1),
2769 pszHyper);
2770 if (pfDblEntry)
2771 *pfDblEntry = true;
2772 break;
2773 }
2774
2775 case X86_SEL_TYPE_SYS_386_CALL_GATE:
2776 {
2777 unsigned cParams = pDesc->au8[4] & 0x1f;
2778 const char *pszCountOf = pDesc->Gen.u4Type & RT_BIT(3) ? "DC" : "WC";
2779 RTSEL sel = pDesc->au16[1];
2780 uint64_t off = pDesc->au16[0]
2781 | ((uint64_t)pDesc->au16[3] << 16)
2782 | ((uint64_t)pDesc->Gen.u32BaseHigh3 << 32);
2783 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Sel:Off=%04x:%016RX64 DPL=%d %s %s=%d%s\n",
2784 iEntry, s_apszTypes[pDesc->Gen.u4Type], sel, off,
2785 pDesc->Gen.u2Dpl, pszPresent, pszCountOf, cParams, pszHyper);
2786 if (pfDblEntry)
2787 *pfDblEntry = true;
2788 break;
2789 }
2790
2791 case X86_SEL_TYPE_SYS_386_INT_GATE:
2792 case X86_SEL_TYPE_SYS_386_TRAP_GATE:
2793 {
2794 RTSEL sel = pDesc->Gate.u16Sel;
2795 uint64_t off = pDesc->Gate.u16OffsetLow
2796 | ((uint64_t)pDesc->Gate.u16OffsetHigh << 16)
2797 | ((uint64_t)pDesc->Gate.u32OffsetTop << 32);
2798 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Sel:Off=%04x:%016RX64 DPL=%u %s IST=%u%s\n",
2799 iEntry, s_apszTypes[pDesc->Gate.u4Type], sel, off,
2800 pDesc->Gate.u2Dpl, pszPresent, pDesc->Gate.u3IST, pszHyper);
2801 if (pfDblEntry)
2802 *pfDblEntry = true;
2803 break;
2804 }
2805
2806 /* impossible, just it's necessary to keep gcc happy. */
2807 default:
2808 return VINF_SUCCESS;
2809 }
2810 }
2811 return VINF_SUCCESS;
2812}
2813
2814
2815/**
2816 * Worker function that displays one descriptor entry (GDT, LDT, IDT).
2817 *
2818 * @returns pfnPrintf status code.
2819 * @param pCmdHlp The DBGC command helpers.
2820 * @param pDesc The descriptor to display.
2821 * @param iEntry The descriptor entry number.
2822 * @param fHyper Whether the selector belongs to the hypervisor or not.
2823 */
2824static int dbgcCmdDumpDTWorker32(PDBGCCMDHLP pCmdHlp, PCX86DESC pDesc, unsigned iEntry, bool fHyper)
2825{
2826 int rc;
2827
2828 const char *pszHyper = fHyper ? " HYPER" : "";
2829 const char *pszPresent = pDesc->Gen.u1Present ? "P " : "NP";
2830 if (pDesc->Gen.u1DescType)
2831 {
2832 static const char * const s_apszTypes[] =
2833 {
2834 "DataRO", /* 0 Read-Only */
2835 "DataRO", /* 1 Read-Only - Accessed */
2836 "DataRW", /* 2 Read/Write */
2837 "DataRW", /* 3 Read/Write - Accessed */
2838 "DownRO", /* 4 Expand-down, Read-Only */
2839 "DownRO", /* 5 Expand-down, Read-Only - Accessed */
2840 "DownRW", /* 6 Expand-down, Read/Write */
2841 "DownRW", /* 7 Expand-down, Read/Write - Accessed */
2842 "CodeEO", /* 8 Execute-Only */
2843 "CodeEO", /* 9 Execute-Only - Accessed */
2844 "CodeER", /* A Execute/Readable */
2845 "CodeER", /* B Execute/Readable - Accessed */
2846 "ConfE0", /* C Conforming, Execute-Only */
2847 "ConfE0", /* D Conforming, Execute-Only - Accessed */
2848 "ConfER", /* E Conforming, Execute/Readable */
2849 "ConfER" /* F Conforming, Execute/Readable - Accessed */
2850 };
2851 const char *pszAccessed = pDesc->Gen.u4Type & RT_BIT(0) ? "A " : "NA";
2852 const char *pszGranularity = pDesc->Gen.u1Granularity ? "G" : " ";
2853 const char *pszBig = pDesc->Gen.u1DefBig ? "BIG" : " ";
2854 uint32_t u32Base = pDesc->Gen.u16BaseLow
2855 | ((uint32_t)pDesc->Gen.u8BaseHigh1 << 16)
2856 | ((uint32_t)pDesc->Gen.u8BaseHigh2 << 24);
2857 uint32_t cbLimit = pDesc->Gen.u16LimitLow | (pDesc->Gen.u4LimitHigh << 16);
2858 if (pDesc->Gen.u1Granularity)
2859 cbLimit <<= PAGE_SHIFT;
2860
2861 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Bas=%08x Lim=%08x DPL=%d %s %s %s %s AVL=%d L=%d%s\n",
2862 iEntry, s_apszTypes[pDesc->Gen.u4Type], u32Base, cbLimit,
2863 pDesc->Gen.u2Dpl, pszPresent, pszAccessed, pszGranularity, pszBig,
2864 pDesc->Gen.u1Available, pDesc->Gen.u1Long, pszHyper);
2865 }
2866 else
2867 {
2868 static const char * const s_apszTypes[] =
2869 {
2870 "Ill-0 ", /* 0 0000 Reserved (Illegal) */
2871 "Tss16A", /* 1 0001 Available 16-bit TSS */
2872 "LDT ", /* 2 0010 LDT */
2873 "Tss16B", /* 3 0011 Busy 16-bit TSS */
2874 "Call16", /* 4 0100 16-bit Call Gate */
2875 "TaskG ", /* 5 0101 Task Gate */
2876 "Int16 ", /* 6 0110 16-bit Interrupt Gate */
2877 "Trap16", /* 7 0111 16-bit Trap Gate */
2878 "Ill-8 ", /* 8 1000 Reserved (Illegal) */
2879 "Tss32A", /* 9 1001 Available 32-bit TSS */
2880 "Ill-A ", /* A 1010 Reserved (Illegal) */
2881 "Tss32B", /* B 1011 Busy 32-bit TSS */
2882 "Call32", /* C 1100 32-bit Call Gate */
2883 "Ill-D ", /* D 1101 Reserved (Illegal) */
2884 "Int32 ", /* E 1110 32-bit Interrupt Gate */
2885 "Trap32" /* F 1111 32-bit Trap Gate */
2886 };
2887 switch (pDesc->Gen.u4Type)
2888 {
2889 /* raw */
2890 case X86_SEL_TYPE_SYS_UNDEFINED:
2891 case X86_SEL_TYPE_SYS_UNDEFINED2:
2892 case X86_SEL_TYPE_SYS_UNDEFINED4:
2893 case X86_SEL_TYPE_SYS_UNDEFINED3:
2894 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s %.8Rhxs DPL=%d %s%s\n",
2895 iEntry, s_apszTypes[pDesc->Gen.u4Type], pDesc,
2896 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
2897 break;
2898
2899 case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
2900 case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
2901 case X86_SEL_TYPE_SYS_286_TSS_BUSY:
2902 case X86_SEL_TYPE_SYS_386_TSS_BUSY:
2903 case X86_SEL_TYPE_SYS_LDT:
2904 {
2905 const char *pszGranularity = pDesc->Gen.u1Granularity ? "G" : " ";
2906 const char *pszBusy = pDesc->Gen.u4Type & RT_BIT(1) ? "B " : "NB";
2907 const char *pszBig = pDesc->Gen.u1DefBig ? "BIG" : " ";
2908 uint32_t u32Base = pDesc->Gen.u16BaseLow
2909 | ((uint32_t)pDesc->Gen.u8BaseHigh1 << 16)
2910 | ((uint32_t)pDesc->Gen.u8BaseHigh2 << 24);
2911 uint32_t cbLimit = pDesc->Gen.u16LimitLow | (pDesc->Gen.u4LimitHigh << 16);
2912 if (pDesc->Gen.u1Granularity)
2913 cbLimit <<= PAGE_SHIFT;
2914
2915 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Bas=%08x Lim=%08x DPL=%d %s %s %s %s AVL=%d R=%d%s\n",
2916 iEntry, s_apszTypes[pDesc->Gen.u4Type], u32Base, cbLimit,
2917 pDesc->Gen.u2Dpl, pszPresent, pszBusy, pszGranularity, pszBig,
2918 pDesc->Gen.u1Available, pDesc->Gen.u1Long | (pDesc->Gen.u1DefBig << 1),
2919 pszHyper);
2920 break;
2921 }
2922
2923 case X86_SEL_TYPE_SYS_TASK_GATE:
2924 {
2925 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s TSS=%04x DPL=%d %s%s\n",
2926 iEntry, s_apszTypes[pDesc->Gen.u4Type], pDesc->au16[1],
2927 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
2928 break;
2929 }
2930
2931 case X86_SEL_TYPE_SYS_286_CALL_GATE:
2932 case X86_SEL_TYPE_SYS_386_CALL_GATE:
2933 {
2934 unsigned cParams = pDesc->au8[4] & 0x1f;
2935 const char *pszCountOf = pDesc->Gen.u4Type & RT_BIT(3) ? "DC" : "WC";
2936 RTSEL sel = pDesc->au16[1];
2937 uint32_t off = pDesc->au16[0] | ((uint32_t)pDesc->au16[3] << 16);
2938 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Sel:Off=%04x:%08x DPL=%d %s %s=%d%s\n",
2939 iEntry, s_apszTypes[pDesc->Gen.u4Type], sel, off,
2940 pDesc->Gen.u2Dpl, pszPresent, pszCountOf, cParams, pszHyper);
2941 break;
2942 }
2943
2944 case X86_SEL_TYPE_SYS_286_INT_GATE:
2945 case X86_SEL_TYPE_SYS_386_INT_GATE:
2946 case X86_SEL_TYPE_SYS_286_TRAP_GATE:
2947 case X86_SEL_TYPE_SYS_386_TRAP_GATE:
2948 {
2949 RTSEL sel = pDesc->au16[1];
2950 uint32_t off = pDesc->au16[0] | ((uint32_t)pDesc->au16[3] << 16);
2951 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %s Sel:Off=%04x:%08x DPL=%d %s%s\n",
2952 iEntry, s_apszTypes[pDesc->Gen.u4Type], sel, off,
2953 pDesc->Gen.u2Dpl, pszPresent, pszHyper);
2954 break;
2955 }
2956
2957 /* impossible, just it's necessary to keep gcc happy. */
2958 default:
2959 return VINF_SUCCESS;
2960 }
2961 }
2962 return rc;
2963}
2964
2965
2966/**
2967 * @callback_method_impl{FNDBGCCMD, The 'dg'\, 'dga'\, 'dl' and 'dla' commands.}
2968 */
2969static DECLCALLBACK(int) dbgcCmdDumpDT(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2970{
2971 /*
2972 * Validate input.
2973 */
2974 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
2975
2976 /*
2977 * Get the CPU mode, check which command variation this is
2978 * and fix a default parameter if needed.
2979 */
2980 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2981 PVMCPU pVCpu = VMMR3GetCpuByIdU(pUVM, pDbgc->idCpu);
2982 CPUMMODE enmMode = CPUMGetGuestMode(pVCpu);
2983 bool fGdt = pCmd->pszCmd[1] == 'g';
2984 bool fAll = pCmd->pszCmd[2] == 'a';
2985 RTSEL SelTable = fGdt ? 0 : X86_SEL_LDT;
2986
2987 DBGCVAR Var;
2988 if (!cArgs)
2989 {
2990 cArgs = 1;
2991 paArgs = &Var;
2992 Var.enmType = DBGCVAR_TYPE_NUMBER;
2993 Var.u.u64Number = 0;
2994 Var.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
2995 Var.u64Range = 1024;
2996 }
2997
2998 /*
2999 * Process the arguments.
3000 */
3001 for (unsigned i = 0; i < cArgs; i++)
3002 {
3003 /*
3004 * Retrieve the selector value from the argument.
3005 * The parser may confuse pointers and numbers if more than one
3006 * argument is given, that that into account.
3007 */
3008 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, i, paArgs[i].enmType == DBGCVAR_TYPE_NUMBER || DBGCVAR_ISPOINTER(paArgs[i].enmType));
3009 uint64_t u64;
3010 unsigned cSels = 1;
3011 switch (paArgs[i].enmType)
3012 {
3013 case DBGCVAR_TYPE_NUMBER:
3014 u64 = paArgs[i].u.u64Number;
3015 if (paArgs[i].enmRangeType != DBGCVAR_RANGE_NONE)
3016 cSels = RT_MIN(paArgs[i].u64Range, 1024);
3017 break;
3018 case DBGCVAR_TYPE_GC_FAR: u64 = paArgs[i].u.GCFar.sel; break;
3019 case DBGCVAR_TYPE_GC_FLAT: u64 = paArgs[i].u.GCFlat; break;
3020 case DBGCVAR_TYPE_GC_PHYS: u64 = paArgs[i].u.GCPhys; break;
3021 case DBGCVAR_TYPE_HC_FLAT: u64 = (uintptr_t)paArgs[i].u.pvHCFlat; break;
3022 case DBGCVAR_TYPE_HC_PHYS: u64 = paArgs[i].u.HCPhys; break;
3023 default: u64 = _64K; break;
3024 }
3025 if (u64 < _64K)
3026 {
3027 unsigned Sel = (RTSEL)u64;
3028
3029 /*
3030 * Dump the specified range.
3031 */
3032 bool fSingle = cSels == 1;
3033 while ( cSels-- > 0
3034 && Sel < _64K)
3035 {
3036 DBGFSELINFO SelInfo;
3037 int rc = DBGFR3SelQueryInfo(pUVM, pDbgc->idCpu, Sel | SelTable, DBGFSELQI_FLAGS_DT_GUEST, &SelInfo);
3038 if (RT_SUCCESS(rc))
3039 {
3040 if (SelInfo.fFlags & DBGFSELINFO_FLAGS_REAL_MODE)
3041 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x RealM Bas=%04x Lim=%04x\n",
3042 Sel, (unsigned)SelInfo.GCPtrBase, (unsigned)SelInfo.cbLimit);
3043 else if ( fAll
3044 || fSingle
3045 || SelInfo.u.Raw.Gen.u1Present)
3046 {
3047 if (enmMode == CPUMMODE_PROTECTED)
3048 rc = dbgcCmdDumpDTWorker32(pCmdHlp, &SelInfo.u.Raw, Sel, !!(SelInfo.fFlags & DBGFSELINFO_FLAGS_HYPER));
3049 else
3050 {
3051 bool fDblSkip = false;
3052 rc = dbgcCmdDumpDTWorker64(pCmdHlp, &SelInfo.u.Raw64, Sel, !!(SelInfo.fFlags & DBGFSELINFO_FLAGS_HYPER), &fDblSkip);
3053 if (fDblSkip)
3054 Sel += 4;
3055 }
3056 }
3057 }
3058 else
3059 {
3060 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %Rrc\n", Sel, rc);
3061 if (!fAll)
3062 return rc;
3063 }
3064 if (RT_FAILURE(rc))
3065 return rc;
3066
3067 /* next */
3068 Sel += 8;
3069 }
3070 }
3071 else
3072 DBGCCmdHlpPrintf(pCmdHlp, "error: %llx is out of bounds\n", u64);
3073 }
3074
3075 return VINF_SUCCESS;
3076}
3077
3078
3079/**
3080 * @callback_method_impl{FNDBGCCMD, The 'di' and 'dia' commands.}
3081 */
3082static DECLCALLBACK(int) dbgcCmdDumpIDT(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3083{
3084 /*
3085 * Validate input.
3086 */
3087 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
3088
3089 /*
3090 * Establish some stuff like the current IDTR and CPU mode,
3091 * and fix a default parameter.
3092 */
3093 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3094 PVMCPU pVCpu = VMMR3GetCpuByIdU(pUVM, pDbgc->idCpu);
3095 uint16_t cbLimit;
3096 RTGCUINTPTR GCPtrBase = CPUMGetGuestIDTR(pVCpu, &cbLimit);
3097 CPUMMODE enmMode = CPUMGetGuestMode(pVCpu);
3098 unsigned cbEntry;
3099 switch (enmMode)
3100 {
3101 case CPUMMODE_REAL: cbEntry = sizeof(RTFAR16); break;
3102 case CPUMMODE_PROTECTED: cbEntry = sizeof(X86DESC); break;
3103 case CPUMMODE_LONG: cbEntry = sizeof(X86DESC64); break;
3104 default:
3105 return DBGCCmdHlpPrintf(pCmdHlp, "error: Invalid CPU mode %d.\n", enmMode);
3106 }
3107
3108 bool fAll = pCmd->pszCmd[2] == 'a';
3109 DBGCVAR Var;
3110 if (!cArgs)
3111 {
3112 cArgs = 1;
3113 paArgs = &Var;
3114 Var.enmType = DBGCVAR_TYPE_NUMBER;
3115 Var.u.u64Number = 0;
3116 Var.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
3117 Var.u64Range = 256;
3118 }
3119
3120 /*
3121 * Process the arguments.
3122 */
3123 for (unsigned i = 0; i < cArgs; i++)
3124 {
3125 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, i, paArgs[i].enmType == DBGCVAR_TYPE_NUMBER);
3126 if (paArgs[i].u.u64Number < 256)
3127 {
3128 RTGCUINTPTR iInt = (RTGCUINTPTR)paArgs[i].u.u64Number;
3129 unsigned cInts = paArgs[i].enmRangeType != DBGCVAR_RANGE_NONE
3130 ? paArgs[i].u64Range
3131 : 1;
3132 bool fSingle = cInts == 1;
3133 while ( cInts-- > 0
3134 && iInt < 256)
3135 {
3136 /*
3137 * Try read it.
3138 */
3139 union
3140 {
3141 RTFAR16 Real;
3142 X86DESC Prot;
3143 X86DESC64 Long;
3144 } u;
3145 if (iInt * cbEntry + (cbEntry - 1) > cbLimit)
3146 {
3147 DBGCCmdHlpPrintf(pCmdHlp, "%04x not within the IDT\n", (unsigned)iInt);
3148 if (!fAll && !fSingle)
3149 return VINF_SUCCESS;
3150 }
3151 DBGCVAR AddrVar;
3152 AddrVar.enmType = DBGCVAR_TYPE_GC_FLAT;
3153 AddrVar.u.GCFlat = GCPtrBase + iInt * cbEntry;
3154 AddrVar.enmRangeType = DBGCVAR_RANGE_NONE;
3155 int rc = pCmdHlp->pfnMemRead(pCmdHlp, &u, cbEntry, &AddrVar, NULL);
3156 if (RT_FAILURE(rc))
3157 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading IDT entry %#04x.\n", (unsigned)iInt);
3158
3159 /*
3160 * Display it.
3161 */
3162 switch (enmMode)
3163 {
3164 case CPUMMODE_REAL:
3165 rc = DBGCCmdHlpPrintf(pCmdHlp, "%04x %RTfp16\n", (unsigned)iInt, u.Real);
3166 /** @todo resolve 16:16 IDTE to a symbol */
3167 break;
3168 case CPUMMODE_PROTECTED:
3169 if (fAll || fSingle || u.Prot.Gen.u1Present)
3170 rc = dbgcCmdDumpDTWorker32(pCmdHlp, &u.Prot, iInt, false);
3171 break;
3172 case CPUMMODE_LONG:
3173 if (fAll || fSingle || u.Long.Gen.u1Present)
3174 rc = dbgcCmdDumpDTWorker64(pCmdHlp, &u.Long, iInt, false, NULL);
3175 break;
3176 default: break; /* to shut up gcc */
3177 }
3178 if (RT_FAILURE(rc))
3179 return rc;
3180
3181 /* next */
3182 iInt++;
3183 }
3184 }
3185 else
3186 DBGCCmdHlpPrintf(pCmdHlp, "error: %llx is out of bounds (max 256)\n", paArgs[i].u.u64Number);
3187 }
3188
3189 return VINF_SUCCESS;
3190}
3191
3192
3193/**
3194 * @callback_method_impl{FNDBGCCMD,
3195 * The 'da'\, 'dq'\, 'dd'\, 'dw' and 'db' commands.}
3196 */
3197static DECLCALLBACK(int) dbgcCmdDumpMem(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3198{
3199 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3200
3201 /*
3202 * Validate input.
3203 */
3204 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs <= 1);
3205 if (cArgs == 1)
3206 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, DBGCVAR_ISPOINTER(paArgs[0].enmType));
3207 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
3208
3209 /*
3210 * Figure out the element size.
3211 */
3212 unsigned cbElement;
3213 bool fAscii = false;
3214 switch (pCmd->pszCmd[1])
3215 {
3216 default:
3217 case 'b': cbElement = 1; break;
3218 case 'w': cbElement = 2; break;
3219 case 'd': cbElement = 4; break;
3220 case 'q': cbElement = 8; break;
3221 case 'a':
3222 cbElement = 1;
3223 fAscii = true;
3224 break;
3225 case '\0':
3226 fAscii = !!(pDbgc->cbDumpElement & 0x80000000);
3227 cbElement = pDbgc->cbDumpElement & 0x7fffffff;
3228 if (!cbElement)
3229 cbElement = 1;
3230 break;
3231 }
3232
3233 /*
3234 * Find address.
3235 */
3236 if (!cArgs)
3237 pDbgc->DumpPos.enmRangeType = DBGCVAR_RANGE_NONE;
3238 else
3239 pDbgc->DumpPos = paArgs[0];
3240
3241 /*
3242 * Range.
3243 */
3244 switch (pDbgc->DumpPos.enmRangeType)
3245 {
3246 case DBGCVAR_RANGE_NONE:
3247 pDbgc->DumpPos.enmRangeType = DBGCVAR_RANGE_BYTES;
3248 pDbgc->DumpPos.u64Range = 0x60;
3249 break;
3250
3251 case DBGCVAR_RANGE_ELEMENTS:
3252 if (pDbgc->DumpPos.u64Range > 2048)
3253 return DBGCCmdHlpPrintf(pCmdHlp, "error: Too many elements requested. Max is 2048 elements.\n");
3254 pDbgc->DumpPos.enmRangeType = DBGCVAR_RANGE_BYTES;
3255 pDbgc->DumpPos.u64Range = (cbElement ? cbElement : 1) * pDbgc->DumpPos.u64Range;
3256 break;
3257
3258 case DBGCVAR_RANGE_BYTES:
3259 if (pDbgc->DumpPos.u64Range > 65536)
3260 return DBGCCmdHlpPrintf(pCmdHlp, "error: The requested range is too big. Max is 64KB.\n");
3261 break;
3262
3263 default:
3264 return DBGCCmdHlpPrintf(pCmdHlp, "internal error: Unknown range type %d.\n", pDbgc->DumpPos.enmRangeType);
3265 }
3266
3267 pDbgc->pLastPos = &pDbgc->DumpPos;
3268
3269 /*
3270 * Do the dumping.
3271 */
3272 pDbgc->cbDumpElement = cbElement | (fAscii << 31);
3273 int cbLeft = (int)pDbgc->DumpPos.u64Range;
3274 uint8_t u8Prev = '\0';
3275 for (;;)
3276 {
3277 /*
3278 * Read memory.
3279 */
3280 char achBuffer[16];
3281 size_t cbReq = RT_MIN((int)sizeof(achBuffer), cbLeft);
3282 size_t cb = RT_MIN((int)sizeof(achBuffer), cbLeft);
3283 int rc = pCmdHlp->pfnMemRead(pCmdHlp, &achBuffer, cbReq, &pDbgc->DumpPos, &cb);
3284 if (RT_FAILURE(rc))
3285 {
3286 if (u8Prev && u8Prev != '\n')
3287 DBGCCmdHlpPrintf(pCmdHlp, "\n");
3288 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading memory at %DV.\n", &pDbgc->DumpPos);
3289 }
3290
3291 /*
3292 * Display it.
3293 */
3294 memset(&achBuffer[cb], 0, sizeof(achBuffer) - cb);
3295 if (!fAscii)
3296 {
3297 DBGCCmdHlpPrintf(pCmdHlp, "%DV:", &pDbgc->DumpPos);
3298 unsigned i;
3299 for (i = 0; i < cb; i += cbElement)
3300 {
3301 const char *pszSpace = " ";
3302 if (cbElement <= 2 && i == 8 && !fAscii)
3303 pszSpace = "-";
3304 switch (cbElement)
3305 {
3306 case 1: DBGCCmdHlpPrintf(pCmdHlp, "%s%02x", pszSpace, *(uint8_t *)&achBuffer[i]); break;
3307 case 2: DBGCCmdHlpPrintf(pCmdHlp, "%s%04x", pszSpace, *(uint16_t *)&achBuffer[i]); break;
3308 case 4: DBGCCmdHlpPrintf(pCmdHlp, "%s%08x", pszSpace, *(uint32_t *)&achBuffer[i]); break;
3309 case 8: DBGCCmdHlpPrintf(pCmdHlp, "%s%016llx", pszSpace, *(uint64_t *)&achBuffer[i]); break;
3310 }
3311 }
3312
3313 /* chars column */
3314 if (pDbgc->cbDumpElement == 1)
3315 {
3316 while (i++ < sizeof(achBuffer))
3317 DBGCCmdHlpPrintf(pCmdHlp, " ");
3318 DBGCCmdHlpPrintf(pCmdHlp, " ");
3319 for (i = 0; i < cb; i += cbElement)
3320 {
3321 uint8_t u8 = *(uint8_t *)&achBuffer[i];
3322 if (RT_C_IS_PRINT(u8) && u8 < 127 && u8 >= 32)
3323 DBGCCmdHlpPrintf(pCmdHlp, "%c", u8);
3324 else
3325 DBGCCmdHlpPrintf(pCmdHlp, ".");
3326 }
3327 }
3328 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
3329 }
3330 else
3331 {
3332 /*
3333 * We print up to the first zero and stop there.
3334 * Only printables + '\t' and '\n' are printed.
3335 */
3336 if (!u8Prev)
3337 DBGCCmdHlpPrintf(pCmdHlp, "%DV:\n", &pDbgc->DumpPos);
3338 uint8_t u8 = '\0';
3339 unsigned i;
3340 for (i = 0; i < cb; i++)
3341 {
3342 u8Prev = u8;
3343 u8 = *(uint8_t *)&achBuffer[i];
3344 if ( u8 < 127
3345 && ( (RT_C_IS_PRINT(u8) && u8 >= 32)
3346 || u8 == '\t'
3347 || u8 == '\n'))
3348 DBGCCmdHlpPrintf(pCmdHlp, "%c", u8);
3349 else if (!u8)
3350 break;
3351 else
3352 DBGCCmdHlpPrintf(pCmdHlp, "\\x%x", u8);
3353 }
3354 if (u8 == '\0')
3355 cb = cbLeft = i + 1;
3356 if (cbLeft - cb <= 0 && u8Prev != '\n')
3357 DBGCCmdHlpPrintf(pCmdHlp, "\n");
3358 }
3359
3360 /*
3361 * Advance
3362 */
3363 cbLeft -= (int)cb;
3364 rc = DBGCCmdHlpEval(pCmdHlp, &pDbgc->DumpPos, "(%Dv) + %x", &pDbgc->DumpPos, cb);
3365 if (RT_FAILURE(rc))
3366 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Expression: (%Dv) + %x\n", &pDbgc->DumpPos, cb);
3367 if (cbLeft <= 0)
3368 break;
3369 }
3370
3371 NOREF(pCmd);
3372 return VINF_SUCCESS;
3373}
3374
3375
3376/**
3377 * Best guess at which paging mode currently applies to the guest
3378 * paging structures.
3379 *
3380 * This have to come up with a decent answer even when the guest
3381 * is in non-paged protected mode or real mode.
3382 *
3383 * @returns cr3.
3384 * @param pDbgc The DBGC instance.
3385 * @param pfPAE Where to store the page address extension indicator.
3386 * @param pfLME Where to store the long mode enabled indicator.
3387 * @param pfPSE Where to store the page size extension indicator.
3388 * @param pfPGE Where to store the page global enabled indicator.
3389 * @param pfNXE Where to store the no-execution enabled indicator.
3390 */
3391static RTGCPHYS dbgcGetGuestPageMode(PDBGC pDbgc, bool *pfPAE, bool *pfLME, bool *pfPSE, bool *pfPGE, bool *pfNXE)
3392{
3393 PVMCPU pVCpu = VMMR3GetCpuByIdU(pDbgc->pUVM, pDbgc->idCpu);
3394 RTGCUINTREG cr4 = CPUMGetGuestCR4(pVCpu);
3395 *pfPSE = !!(cr4 & X86_CR4_PSE);
3396 *pfPGE = !!(cr4 & X86_CR4_PGE);
3397 if (cr4 & X86_CR4_PAE)
3398 {
3399 *pfPSE = true;
3400 *pfPAE = true;
3401 }
3402 else
3403 *pfPAE = false;
3404
3405 *pfLME = CPUMGetGuestMode(pVCpu) == CPUMMODE_LONG;
3406 *pfNXE = false; /* GUEST64 GUESTNX */
3407 return CPUMGetGuestCR3(pVCpu);
3408}
3409
3410
3411/**
3412 * Determine the shadow paging mode.
3413 *
3414 * @returns cr3.
3415 * @param pDbgc The DBGC instance.
3416 * @param pfPAE Where to store the page address extension indicator.
3417 * @param pfLME Where to store the long mode enabled indicator.
3418 * @param pfPSE Where to store the page size extension indicator.
3419 * @param pfPGE Where to store the page global enabled indicator.
3420 * @param pfNXE Where to store the no-execution enabled indicator.
3421 */
3422static RTHCPHYS dbgcGetShadowPageMode(PDBGC pDbgc, bool *pfPAE, bool *pfLME, bool *pfPSE, bool *pfPGE, bool *pfNXE)
3423{
3424 PVMCPU pVCpu = VMMR3GetCpuByIdU(pDbgc->pUVM, pDbgc->idCpu);
3425
3426 *pfPSE = true;
3427 *pfPGE = false;
3428 switch (PGMGetShadowMode(pVCpu))
3429 {
3430 default:
3431 case PGMMODE_32_BIT:
3432 *pfPAE = *pfLME = *pfNXE = false;
3433 break;
3434 case PGMMODE_PAE:
3435 *pfLME = *pfNXE = false;
3436 *pfPAE = true;
3437 break;
3438 case PGMMODE_PAE_NX:
3439 *pfLME = false;
3440 *pfPAE = *pfNXE = true;
3441 break;
3442 case PGMMODE_AMD64:
3443 *pfNXE = false;
3444 *pfPAE = *pfLME = true;
3445 break;
3446 case PGMMODE_AMD64_NX:
3447 *pfPAE = *pfLME = *pfNXE = true;
3448 break;
3449 }
3450 return PGMGetHyperCR3(pVCpu);
3451}
3452
3453
3454/**
3455 * @callback_method_impl{FNDBGCCMD,
3456 * The 'dpd'\, 'dpda'\, 'dpdb'\, 'dpdg' and 'dpdh' commands.}
3457 */
3458static DECLCALLBACK(int) dbgcCmdDumpPageDir(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3459{
3460 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3461
3462 /*
3463 * Validate input.
3464 */
3465 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs <= 1);
3466 if (cArgs == 1 && pCmd->pszCmd[3] == 'a')
3467 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, DBGCVAR_ISPOINTER(paArgs[0].enmType));
3468 if (cArgs == 1 && pCmd->pszCmd[3] != 'a')
3469 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, paArgs[0].enmType == DBGCVAR_TYPE_NUMBER
3470 || DBGCVAR_ISPOINTER(paArgs[0].enmType));
3471 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
3472
3473 /*
3474 * Guest or shadow page directories? Get the paging parameters.
3475 */
3476 bool fGuest = pCmd->pszCmd[3] != 'h';
3477 if (!pCmd->pszCmd[3] || pCmd->pszCmd[3] == 'a')
3478 fGuest = paArgs[0].enmType == DBGCVAR_TYPE_NUMBER
3479 ? pDbgc->fRegCtxGuest
3480 : DBGCVAR_ISGCPOINTER(paArgs[0].enmType);
3481
3482 bool fPAE, fLME, fPSE, fPGE, fNXE;
3483 uint64_t cr3 = fGuest
3484 ? dbgcGetGuestPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE)
3485 : dbgcGetShadowPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE);
3486 const unsigned cbEntry = fPAE ? sizeof(X86PTEPAE) : sizeof(X86PTE);
3487
3488 /*
3489 * Setup default argument if none was specified.
3490 * Fix address / index confusion.
3491 */
3492 DBGCVAR VarDefault;
3493 if (!cArgs)
3494 {
3495 if (pCmd->pszCmd[3] == 'a')
3496 {
3497 if (fLME || fPAE)
3498 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");
3499 if (fGuest)
3500 DBGCVAR_INIT_GC_PHYS(&VarDefault, cr3);
3501 else
3502 DBGCVAR_INIT_HC_PHYS(&VarDefault, cr3);
3503 }
3504 else
3505 DBGCVAR_INIT_GC_FLAT(&VarDefault, 0);
3506 paArgs = &VarDefault;
3507 cArgs = 1;
3508 }
3509 else if (paArgs[0].enmType == DBGCVAR_TYPE_NUMBER)
3510 {
3511 /* If it's a number (not an address), it's an index, so convert it to an address. */
3512 Assert(pCmd->pszCmd[3] != 'a');
3513 VarDefault = paArgs[0];
3514 if (fPAE)
3515 return DBGCCmdHlpPrintf(pCmdHlp, "PDE indexing is only implemented for 32-bit paging.\n");
3516 if (VarDefault.u.u64Number >= PAGE_SIZE / cbEntry)
3517 return DBGCCmdHlpPrintf(pCmdHlp, "PDE index is out of range [0..%d].\n", PAGE_SIZE / cbEntry - 1);
3518 VarDefault.u.u64Number <<= X86_PD_SHIFT;
3519 VarDefault.enmType = DBGCVAR_TYPE_GC_FLAT;
3520 paArgs = &VarDefault;
3521 }
3522
3523 /*
3524 * Locate the PDE to start displaying at.
3525 *
3526 * The 'dpda' command takes the address of a PDE, while the others are guest
3527 * virtual address which PDEs should be displayed. So, 'dpda' is rather simple
3528 * while the others require us to do all the tedious walking thru the paging
3529 * hierarchy to find the intended PDE.
3530 */
3531 unsigned iEntry = ~0U; /* The page directory index. ~0U for 'dpta'. */
3532 DBGCVAR VarGCPtr = { NULL, }; /* The GC address corresponding to the current PDE (iEntry != ~0U). */
3533 DBGCVAR VarPDEAddr; /* The address of the current PDE. */
3534 unsigned cEntries; /* The number of entries to display. */
3535 unsigned cEntriesMax; /* The max number of entries to display. */
3536 int rc;
3537 if (pCmd->pszCmd[3] == 'a')
3538 {
3539 VarPDEAddr = paArgs[0];
3540 switch (VarPDEAddr.enmRangeType)
3541 {
3542 case DBGCVAR_RANGE_BYTES: cEntries = VarPDEAddr.u64Range / cbEntry; break;
3543 case DBGCVAR_RANGE_ELEMENTS: cEntries = VarPDEAddr.u64Range; break;
3544 default: cEntries = 10; break;
3545 }
3546 cEntriesMax = PAGE_SIZE / cbEntry;
3547 }
3548 else
3549 {
3550 /*
3551 * Determine the range.
3552 */
3553 switch (paArgs[0].enmRangeType)
3554 {
3555 case DBGCVAR_RANGE_BYTES: cEntries = paArgs[0].u64Range / PAGE_SIZE; break;
3556 case DBGCVAR_RANGE_ELEMENTS: cEntries = paArgs[0].u64Range; break;
3557 default: cEntries = 10; break;
3558 }
3559
3560 /*
3561 * Normalize the input address, it must be a flat GC address.
3562 */
3563 rc = DBGCCmdHlpEval(pCmdHlp, &VarGCPtr, "%%(%Dv)", &paArgs[0]);
3564 if (RT_FAILURE(rc))
3565 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "%%(%Dv)", &paArgs[0]);
3566 if (VarGCPtr.enmType == DBGCVAR_TYPE_HC_FLAT)
3567 {
3568 VarGCPtr.u.GCFlat = (uintptr_t)VarGCPtr.u.pvHCFlat;
3569 VarGCPtr.enmType = DBGCVAR_TYPE_GC_FLAT;
3570 }
3571 if (fPAE)
3572 VarGCPtr.u.GCFlat &= ~(((RTGCPTR)1 << X86_PD_PAE_SHIFT) - 1);
3573 else
3574 VarGCPtr.u.GCFlat &= ~(((RTGCPTR)1 << X86_PD_SHIFT) - 1);
3575
3576 /*
3577 * Do the paging walk until we get to the page directory.
3578 */
3579 DBGCVAR VarCur;
3580 if (fGuest)
3581 DBGCVAR_INIT_GC_PHYS(&VarCur, cr3);
3582 else
3583 DBGCVAR_INIT_HC_PHYS(&VarCur, cr3);
3584 if (fLME)
3585 {
3586 /* Page Map Level 4 Lookup. */
3587 /* Check if it's a valid address first? */
3588 VarCur.u.u64Number &= X86_PTE_PAE_PG_MASK;
3589 VarCur.u.u64Number += (((uint64_t)VarGCPtr.u.GCFlat >> X86_PML4_SHIFT) & X86_PML4_MASK) * sizeof(X86PML4E);
3590 X86PML4E Pml4e;
3591 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pml4e, sizeof(Pml4e), &VarCur, NULL);
3592 if (RT_FAILURE(rc))
3593 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PML4E memory at %DV.\n", &VarCur);
3594 if (!Pml4e.n.u1Present)
3595 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory pointer table is not present for %Dv.\n", &VarGCPtr);
3596
3597 VarCur.u.u64Number = Pml4e.u & X86_PML4E_PG_MASK;
3598 Assert(fPAE);
3599 }
3600 if (fPAE)
3601 {
3602 /* Page directory pointer table. */
3603 X86PDPE Pdpe;
3604 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE) * sizeof(Pdpe);
3605 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pdpe, sizeof(Pdpe), &VarCur, NULL);
3606 if (RT_FAILURE(rc))
3607 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDPE memory at %DV.\n", &VarCur);
3608 if (!Pdpe.n.u1Present)
3609 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory is not present for %Dv.\n", &VarGCPtr);
3610
3611 iEntry = (VarGCPtr.u.GCFlat >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
3612 VarPDEAddr = VarCur;
3613 VarPDEAddr.u.u64Number = Pdpe.u & X86_PDPE_PG_MASK;
3614 VarPDEAddr.u.u64Number += iEntry * sizeof(X86PDEPAE);
3615 }
3616 else
3617 {
3618 /* 32-bit legacy - CR3 == page directory. */
3619 iEntry = (VarGCPtr.u.GCFlat >> X86_PD_SHIFT) & X86_PD_MASK;
3620 VarPDEAddr = VarCur;
3621 VarPDEAddr.u.u64Number += iEntry * sizeof(X86PDE);
3622 }
3623 cEntriesMax = (PAGE_SIZE - iEntry) / cbEntry;
3624 }
3625
3626 /* adjust cEntries */
3627 cEntries = RT_MAX(1, cEntries);
3628 cEntries = RT_MIN(cEntries, cEntriesMax);
3629
3630 /*
3631 * The display loop.
3632 */
3633 DBGCCmdHlpPrintf(pCmdHlp, iEntry != ~0U ? "%DV (index %#x):\n" : "%DV:\n",
3634 &VarPDEAddr, iEntry);
3635 do
3636 {
3637 /*
3638 * Read.
3639 */
3640 X86PDEPAE Pde;
3641 Pde.u = 0;
3642 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pde, cbEntry, &VarPDEAddr, NULL);
3643 if (RT_FAILURE(rc))
3644 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading PDE memory at %DV.\n", &VarPDEAddr);
3645
3646 /*
3647 * Display.
3648 */
3649 if (iEntry != ~0U)
3650 {
3651 DBGCCmdHlpPrintf(pCmdHlp, "%03x %DV: ", iEntry, &VarGCPtr);
3652 iEntry++;
3653 }
3654 if (fPSE && Pde.b.u1Size)
3655 DBGCCmdHlpPrintf(pCmdHlp,
3656 fPAE
3657 ? "%016llx big phys=%016llx %s %s %s %s %s avl=%02x %s %s %s %s %s"
3658 : "%08llx big phys=%08llx %s %s %s %s %s avl=%02x %s %s %s %s %s",
3659 Pde.u,
3660 Pde.u & X86_PDE_PAE_PG_MASK,
3661 Pde.b.u1Present ? "p " : "np",
3662 Pde.b.u1Write ? "w" : "r",
3663 Pde.b.u1User ? "u" : "s",
3664 Pde.b.u1Accessed ? "a " : "na",
3665 Pde.b.u1Dirty ? "d " : "nd",
3666 Pde.b.u3Available,
3667 Pde.b.u1Global ? (fPGE ? "g" : "G") : " ",
3668 Pde.b.u1WriteThru ? "pwt" : " ",
3669 Pde.b.u1CacheDisable ? "pcd" : " ",
3670 Pde.b.u1PAT ? "pat" : "",
3671 Pde.b.u1NoExecute ? (fNXE ? "nx" : "NX") : " ");
3672 else
3673 DBGCCmdHlpPrintf(pCmdHlp,
3674 fPAE
3675 ? "%016llx 4kb phys=%016llx %s %s %s %s %s avl=%02x %s %s %s %s"
3676 : "%08llx 4kb phys=%08llx %s %s %s %s %s avl=%02x %s %s %s %s",
3677 Pde.u,
3678 Pde.u & X86_PDE_PAE_PG_MASK,
3679 Pde.n.u1Present ? "p " : "np",
3680 Pde.n.u1Write ? "w" : "r",
3681 Pde.n.u1User ? "u" : "s",
3682 Pde.n.u1Accessed ? "a " : "na",
3683 Pde.u & RT_BIT(6) ? "6 " : " ",
3684 Pde.n.u3Available,
3685 Pde.u & RT_BIT(8) ? "8" : " ",
3686 Pde.n.u1WriteThru ? "pwt" : " ",
3687 Pde.n.u1CacheDisable ? "pcd" : " ",
3688 Pde.u & RT_BIT(7) ? "7" : "",
3689 Pde.n.u1NoExecute ? (fNXE ? "nx" : "NX") : " ");
3690 if (Pde.u & UINT64_C(0x7fff000000000000))
3691 DBGCCmdHlpPrintf(pCmdHlp, " weird=%RX64", (Pde.u & UINT64_C(0x7fff000000000000)));
3692 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
3693 if (RT_FAILURE(rc))
3694 return rc;
3695
3696 /*
3697 * Advance.
3698 */
3699 VarPDEAddr.u.u64Number += cbEntry;
3700 if (iEntry != ~0U)
3701 VarGCPtr.u.GCFlat += fPAE ? RT_BIT_32(X86_PD_PAE_SHIFT) : RT_BIT_32(X86_PD_SHIFT);
3702 } while (cEntries-- > 0);
3703
3704 return VINF_SUCCESS;
3705}
3706
3707
3708/**
3709 * @callback_method_impl{FNDBGCCMD, The 'dpdb' command.}
3710 */
3711static DECLCALLBACK(int) dbgcCmdDumpPageDirBoth(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3712{
3713 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
3714 int rc1 = pCmdHlp->pfnExec(pCmdHlp, "dpdg %DV", &paArgs[0]);
3715 int rc2 = pCmdHlp->pfnExec(pCmdHlp, "dpdh %DV", &paArgs[0]);
3716 if (RT_FAILURE(rc1))
3717 return rc1;
3718 NOREF(pCmd); NOREF(paArgs); NOREF(cArgs);
3719 return rc2;
3720}
3721
3722
3723/**
3724 * @callback_method_impl{FNDBGCCMD, The 'dph*' commands and main part of 'm'.}
3725 */
3726static DECLCALLBACK(int) dbgcCmdDumpPageHierarchy(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3727{
3728 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3729 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
3730
3731 /*
3732 * Figure the context and base flags.
3733 */
3734 uint32_t fFlags = DBGFPGDMP_FLAGS_PAGE_INFO | DBGFPGDMP_FLAGS_PRINT_CR3;
3735 if (pCmd->pszCmd[0] == 'm')
3736 fFlags |= DBGFPGDMP_FLAGS_GUEST | DBGFPGDMP_FLAGS_SHADOW;
3737 else if (pCmd->pszCmd[3] == '\0')
3738 fFlags |= pDbgc->fRegCtxGuest ? DBGFPGDMP_FLAGS_GUEST : DBGFPGDMP_FLAGS_SHADOW;
3739 else if (pCmd->pszCmd[3] == 'g')
3740 fFlags |= DBGFPGDMP_FLAGS_GUEST;
3741 else if (pCmd->pszCmd[3] == 'h')
3742 fFlags |= DBGFPGDMP_FLAGS_SHADOW;
3743 else
3744 AssertFailed();
3745
3746 if (pDbgc->cPagingHierarchyDumps == 0)
3747 fFlags |= DBGFPGDMP_FLAGS_HEADER;
3748 pDbgc->cPagingHierarchyDumps = (pDbgc->cPagingHierarchyDumps + 1) % 42;
3749
3750 /*
3751 * Get the range.
3752 */
3753 PCDBGCVAR pRange = cArgs > 0 ? &paArgs[0] : pDbgc->pLastPos;
3754 RTGCPTR GCPtrFirst = NIL_RTGCPTR;
3755 int rc = DBGCCmdHlpVarToFlatAddr(pCmdHlp, pRange, &GCPtrFirst);
3756 if (RT_FAILURE(rc))
3757 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Failed to convert %DV to a flat address: %Rrc", pRange, rc);
3758
3759 uint64_t cbRange;
3760 rc = DBGCCmdHlpVarGetRange(pCmdHlp, pRange, PAGE_SIZE, PAGE_SIZE * 8, &cbRange);
3761 if (RT_FAILURE(rc))
3762 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Failed to obtain the range of %DV: %Rrc", pRange, rc);
3763
3764 RTGCPTR GCPtrLast = RTGCPTR_MAX - GCPtrFirst;
3765 if (cbRange >= GCPtrLast)
3766 GCPtrLast = RTGCPTR_MAX;
3767 else if (!cbRange)
3768 GCPtrLast = GCPtrFirst;
3769 else
3770 GCPtrLast = GCPtrFirst + cbRange - 1;
3771
3772 /*
3773 * Do we have a CR3?
3774 */
3775 uint64_t cr3 = 0;
3776 if (cArgs > 1)
3777 {
3778 if ((fFlags & (DBGFPGDMP_FLAGS_GUEST | DBGFPGDMP_FLAGS_SHADOW)) == (DBGFPGDMP_FLAGS_GUEST | DBGFPGDMP_FLAGS_SHADOW))
3779 return DBGCCmdHlpFail(pCmdHlp, pCmd, "No CR3 or mode arguments when dumping both context, please.");
3780 if (paArgs[1].enmType != DBGCVAR_TYPE_NUMBER)
3781 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The CR3 argument is not a number: %DV", &paArgs[1]);
3782 cr3 = paArgs[1].u.u64Number;
3783 }
3784 else
3785 fFlags |= DBGFPGDMP_FLAGS_CURRENT_CR3;
3786
3787 /*
3788 * Do we have a mode?
3789 */
3790 if (cArgs > 2)
3791 {
3792 if (paArgs[2].enmType != DBGCVAR_TYPE_STRING)
3793 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The mode argument is not a string: %DV", &paArgs[2]);
3794 static const struct MODETOFLAGS
3795 {
3796 const char *pszName;
3797 uint32_t fFlags;
3798 } s_aModeToFlags[] =
3799 {
3800 { "ept", DBGFPGDMP_FLAGS_EPT },
3801 { "legacy", 0 },
3802 { "legacy-np", DBGFPGDMP_FLAGS_NP },
3803 { "pse", DBGFPGDMP_FLAGS_PSE },
3804 { "pse-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_NP },
3805 { "pae", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE },
3806 { "pae-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_NP },
3807 { "pae-nx", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_NXE },
3808 { "pae-nx-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_NXE | DBGFPGDMP_FLAGS_NP },
3809 { "long", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_LME },
3810 { "long-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_LME | DBGFPGDMP_FLAGS_NP },
3811 { "long-nx", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_LME | DBGFPGDMP_FLAGS_NXE },
3812 { "long-nx-np", DBGFPGDMP_FLAGS_PSE | DBGFPGDMP_FLAGS_PAE | DBGFPGDMP_FLAGS_LME | DBGFPGDMP_FLAGS_NXE | DBGFPGDMP_FLAGS_NP }
3813 };
3814 int i = RT_ELEMENTS(s_aModeToFlags);
3815 while (i-- > 0)
3816 if (!strcmp(s_aModeToFlags[i].pszName, paArgs[2].u.pszString))
3817 {
3818 fFlags |= s_aModeToFlags[i].fFlags;
3819 break;
3820 }
3821 if (i < 0)
3822 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Unknown mode: \"%s\"", paArgs[2].u.pszString);
3823 }
3824 else
3825 fFlags |= DBGFPGDMP_FLAGS_CURRENT_MODE;
3826
3827 /*
3828 * Call the worker.
3829 */
3830 rc = DBGFR3PagingDumpEx(pUVM, pDbgc->idCpu, fFlags, cr3, GCPtrFirst, GCPtrLast, 99 /*cMaxDepth*/,
3831 DBGCCmdHlpGetDbgfOutputHlp(pCmdHlp));
3832 if (RT_FAILURE(rc))
3833 return DBGCCmdHlpFail(pCmdHlp, pCmd, "DBGFR3PagingDumpEx: %Rrc\n", rc);
3834 return VINF_SUCCESS;
3835}
3836
3837
3838
3839/**
3840 * @callback_method_impl{FNDBGCCMD, The 'dpg*' commands.}
3841 */
3842static DECLCALLBACK(int) dbgcCmdDumpPageTable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
3843{
3844 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3845
3846 /*
3847 * Validate input.
3848 */
3849 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs == 1);
3850 if (pCmd->pszCmd[3] == 'a')
3851 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, DBGCVAR_ISPOINTER(paArgs[0].enmType));
3852 else
3853 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, paArgs[0].enmType == DBGCVAR_TYPE_NUMBER
3854 || DBGCVAR_ISPOINTER(paArgs[0].enmType));
3855 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
3856
3857 /*
3858 * Guest or shadow page tables? Get the paging parameters.
3859 */
3860 bool fGuest = pCmd->pszCmd[3] != 'h';
3861 if (!pCmd->pszCmd[3] || pCmd->pszCmd[3] == 'a')
3862 fGuest = paArgs[0].enmType == DBGCVAR_TYPE_NUMBER
3863 ? pDbgc->fRegCtxGuest
3864 : DBGCVAR_ISGCPOINTER(paArgs[0].enmType);
3865
3866 bool fPAE, fLME, fPSE, fPGE, fNXE;
3867 uint64_t cr3 = fGuest
3868 ? dbgcGetGuestPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE)
3869 : dbgcGetShadowPageMode(pDbgc, &fPAE, &fLME, &fPSE, &fPGE, &fNXE);
3870 const unsigned cbEntry = fPAE ? sizeof(X86PTEPAE) : sizeof(X86PTE);
3871
3872 /*
3873 * Locate the PTE to start displaying at.
3874 *
3875 * The 'dpta' command takes the address of a PTE, while the others are guest
3876 * virtual address which PTEs should be displayed. So, 'pdta' is rather simple
3877 * while the others require us to do all the tedious walking thru the paging
3878 * hierarchy to find the intended PTE.
3879 */
3880 unsigned iEntry = ~0U; /* The page table index. ~0U for 'dpta'. */
3881 DBGCVAR VarGCPtr; /* The GC address corresponding to the current PTE (iEntry != ~0U). */
3882 DBGCVAR VarPTEAddr; /* The address of the current PTE. */
3883 unsigned cEntries; /* The number of entries to display. */
3884 unsigned cEntriesMax; /* The max number of entries to display. */
3885 int rc;
3886 if (pCmd->pszCmd[3] == 'a')
3887 {
3888 VarPTEAddr = paArgs[0];
3889 switch (VarPTEAddr.enmRangeType)
3890 {
3891 case DBGCVAR_RANGE_BYTES: cEntries = VarPTEAddr.u64Range / cbEntry; break;
3892 case DBGCVAR_RANGE_ELEMENTS: cEntries = VarPTEAddr.u64Range; break;
3893 default: cEntries = 10; break;
3894 }
3895 cEntriesMax = PAGE_SIZE / cbEntry;
3896 }
3897 else
3898 {
3899 /*
3900 * Determine the range.
3901 */
3902 switch (paArgs[0].enmRangeType)
3903 {
3904 case DBGCVAR_RANGE_BYTES: cEntries = paArgs[0].u64Range / PAGE_SIZE; break;
3905 case DBGCVAR_RANGE_ELEMENTS: cEntries = paArgs[0].u64Range; break;
3906 default: cEntries = 10; break;
3907 }
3908
3909 /*
3910 * Normalize the input address, it must be a flat GC address.
3911 */
3912 rc = DBGCCmdHlpEval(pCmdHlp, &VarGCPtr, "%%(%Dv)", &paArgs[0]);
3913 if (RT_FAILURE(rc))
3914 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "%%(%Dv)", &paArgs[0]);
3915 if (VarGCPtr.enmType == DBGCVAR_TYPE_HC_FLAT)
3916 {
3917 VarGCPtr.u.GCFlat = (uintptr_t)VarGCPtr.u.pvHCFlat;
3918 VarGCPtr.enmType = DBGCVAR_TYPE_GC_FLAT;
3919 }
3920 VarGCPtr.u.GCFlat &= ~(RTGCPTR)PAGE_OFFSET_MASK;
3921
3922 /*
3923 * Do the paging walk until we get to the page table.
3924 */
3925 DBGCVAR VarCur;
3926 if (fGuest)
3927 DBGCVAR_INIT_GC_PHYS(&VarCur, cr3);
3928 else
3929 DBGCVAR_INIT_HC_PHYS(&VarCur, cr3);
3930 if (fLME)
3931 {
3932 /* Page Map Level 4 Lookup. */
3933 /* Check if it's a valid address first? */
3934 VarCur.u.u64Number &= X86_PTE_PAE_PG_MASK;
3935 VarCur.u.u64Number += (((uint64_t)VarGCPtr.u.GCFlat >> X86_PML4_SHIFT) & X86_PML4_MASK) * sizeof(X86PML4E);
3936 X86PML4E Pml4e;
3937 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pml4e, sizeof(Pml4e), &VarCur, NULL);
3938 if (RT_FAILURE(rc))
3939 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PML4E memory at %DV.\n", &VarCur);
3940 if (!Pml4e.n.u1Present)
3941 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory pointer table is not present for %Dv.\n", &VarGCPtr);
3942
3943 VarCur.u.u64Number = Pml4e.u & X86_PML4E_PG_MASK;
3944 Assert(fPAE);
3945 }
3946 if (fPAE)
3947 {
3948 /* Page directory pointer table. */
3949 X86PDPE Pdpe;
3950 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE) * sizeof(Pdpe);
3951 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pdpe, sizeof(Pdpe), &VarCur, NULL);
3952 if (RT_FAILURE(rc))
3953 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDPE memory at %DV.\n", &VarCur);
3954 if (!Pdpe.n.u1Present)
3955 return DBGCCmdHlpPrintf(pCmdHlp, "Page directory is not present for %Dv.\n", &VarGCPtr);
3956
3957 VarCur.u.u64Number = Pdpe.u & X86_PDPE_PG_MASK;
3958
3959 /* Page directory (PAE). */
3960 X86PDEPAE Pde;
3961 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK) * sizeof(Pde);
3962 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pde, sizeof(Pde), &VarCur, NULL);
3963 if (RT_FAILURE(rc))
3964 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDE memory at %DV.\n", &VarCur);
3965 if (!Pde.n.u1Present)
3966 return DBGCCmdHlpPrintf(pCmdHlp, "Page table is not present for %Dv.\n", &VarGCPtr);
3967 if (fPSE && Pde.n.u1Size)
3968 return pCmdHlp->pfnExec(pCmdHlp, "dpd%s %Dv L3", &pCmd->pszCmd[3], &VarGCPtr);
3969
3970 iEntry = (VarGCPtr.u.GCFlat >> X86_PT_PAE_SHIFT) & X86_PT_PAE_MASK;
3971 VarPTEAddr = VarCur;
3972 VarPTEAddr.u.u64Number = Pde.u & X86_PDE_PAE_PG_MASK;
3973 VarPTEAddr.u.u64Number += iEntry * sizeof(X86PTEPAE);
3974 }
3975 else
3976 {
3977 /* Page directory (legacy). */
3978 X86PDE Pde;
3979 VarCur.u.u64Number += ((VarGCPtr.u.GCFlat >> X86_PD_SHIFT) & X86_PD_MASK) * sizeof(Pde);
3980 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pde, sizeof(Pde), &VarCur, NULL);
3981 if (RT_FAILURE(rc))
3982 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PDE memory at %DV.\n", &VarCur);
3983 if (!Pde.n.u1Present)
3984 return DBGCCmdHlpPrintf(pCmdHlp, "Page table is not present for %Dv.\n", &VarGCPtr);
3985 if (fPSE && Pde.n.u1Size)
3986 return pCmdHlp->pfnExec(pCmdHlp, "dpd%s %Dv L3", &pCmd->pszCmd[3], &VarGCPtr);
3987
3988 iEntry = (VarGCPtr.u.GCFlat >> X86_PT_SHIFT) & X86_PT_MASK;
3989 VarPTEAddr = VarCur;
3990 VarPTEAddr.u.u64Number = Pde.u & X86_PDE_PG_MASK;
3991 VarPTEAddr.u.u64Number += iEntry * sizeof(X86PTE);
3992 }
3993 cEntriesMax = (PAGE_SIZE - iEntry) / cbEntry;
3994 }
3995
3996 /* adjust cEntries */
3997 cEntries = RT_MAX(1, cEntries);
3998 cEntries = RT_MIN(cEntries, cEntriesMax);
3999
4000 /*
4001 * The display loop.
4002 */
4003 DBGCCmdHlpPrintf(pCmdHlp, iEntry != ~0U ? "%DV (base %DV / index %#x):\n" : "%DV:\n",
4004 &VarPTEAddr, &VarGCPtr, iEntry);
4005 do
4006 {
4007 /*
4008 * Read.
4009 */
4010 X86PTEPAE Pte;
4011 Pte.u = 0;
4012 rc = pCmdHlp->pfnMemRead(pCmdHlp, &Pte, cbEntry, &VarPTEAddr, NULL);
4013 if (RT_FAILURE(rc))
4014 return DBGCCmdHlpVBoxError(pCmdHlp, rc, "Reading PTE memory at %DV.\n", &VarPTEAddr);
4015
4016 /*
4017 * Display.
4018 */
4019 if (iEntry != ~0U)
4020 {
4021 DBGCCmdHlpPrintf(pCmdHlp, "%03x %DV: ", iEntry, &VarGCPtr);
4022 iEntry++;
4023 }
4024 DBGCCmdHlpPrintf(pCmdHlp,
4025 fPAE
4026 ? "%016llx 4kb phys=%016llx %s %s %s %s %s avl=%02x %s %s %s %s %s"
4027 : "%08llx 4kb phys=%08llx %s %s %s %s %s avl=%02x %s %s %s %s %s",
4028 Pte.u,
4029 Pte.u & X86_PTE_PAE_PG_MASK,
4030 Pte.n.u1Present ? "p " : "np",
4031 Pte.n.u1Write ? "w" : "r",
4032 Pte.n.u1User ? "u" : "s",
4033 Pte.n.u1Accessed ? "a " : "na",
4034 Pte.n.u1Dirty ? "d " : "nd",
4035 Pte.n.u3Available,
4036 Pte.n.u1Global ? (fPGE ? "g" : "G") : " ",
4037 Pte.n.u1WriteThru ? "pwt" : " ",
4038 Pte.n.u1CacheDisable ? "pcd" : " ",
4039 Pte.n.u1PAT ? "pat" : " ",
4040 Pte.n.u1NoExecute ? (fNXE ? "nx" : "NX") : " "
4041 );
4042 if (Pte.u & UINT64_C(0x7fff000000000000))
4043 DBGCCmdHlpPrintf(pCmdHlp, " weird=%RX64", (Pte.u & UINT64_C(0x7fff000000000000)));
4044 rc = DBGCCmdHlpPrintf(pCmdHlp, "\n");
4045 if (RT_FAILURE(rc))
4046 return rc;
4047
4048 /*
4049 * Advance.
4050 */
4051 VarPTEAddr.u.u64Number += cbEntry;
4052 if (iEntry != ~0U)
4053 VarGCPtr.u.GCFlat += PAGE_SIZE;
4054 } while (cEntries-- > 0);
4055
4056 return VINF_SUCCESS;
4057}
4058
4059
4060/**
4061 * @callback_method_impl{FNDBGCCMD, The 'dptb' command.}
4062 */
4063static DECLCALLBACK(int) dbgcCmdDumpPageTableBoth(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
4064{
4065 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
4066 int rc1 = pCmdHlp->pfnExec(pCmdHlp, "dptg %DV", &paArgs[0]);
4067 int rc2 = pCmdHlp->pfnExec(pCmdHlp, "dpth %DV", &paArgs[0]);
4068 if (RT_FAILURE(rc1))
4069 return rc1;
4070 NOREF(pCmd); NOREF(cArgs);
4071 return rc2;
4072}
4073
4074
4075/**
4076 * @callback_method_impl{FNDBGCCMD, The 'dt' command.}
4077 */
4078static DECLCALLBACK(int) dbgcCmdDumpTSS(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
4079{
4080 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
4081 int rc;
4082
4083 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
4084 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, cArgs <= 1);
4085 if (cArgs == 1)
4086 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, paArgs[0].enmType != DBGCVAR_TYPE_STRING
4087 && paArgs[0].enmType != DBGCVAR_TYPE_SYMBOL);
4088
4089 /*
4090 * Check if the command indicates the type.
4091 */
4092 enum { kTss16, kTss32, kTss64, kTssToBeDetermined } enmTssType = kTssToBeDetermined;
4093 if (!strcmp(pCmd->pszCmd, "dt16"))
4094 enmTssType = kTss16;
4095 else if (!strcmp(pCmd->pszCmd, "dt32"))
4096 enmTssType = kTss32;
4097 else if (!strcmp(pCmd->pszCmd, "dt64"))
4098 enmTssType = kTss64;
4099
4100 /*
4101 * We can get a TSS selector (number), a far pointer using a TSS selector, or some kind of TSS pointer.
4102 */
4103 uint32_t SelTss = UINT32_MAX;
4104 DBGCVAR VarTssAddr;
4105 if (cArgs == 0)
4106 {
4107 /** @todo consider querying the hidden bits instead (missing API). */
4108 uint16_t SelTR;
4109 rc = DBGFR3RegCpuQueryU16(pUVM, pDbgc->idCpu, DBGFREG_TR, &SelTR);
4110 if (RT_FAILURE(rc))
4111 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Failed to query TR, rc=%Rrc\n", rc);
4112 DBGCVAR_INIT_GC_FAR(&VarTssAddr, SelTR, 0);
4113 SelTss = SelTR;
4114 }
4115 else if (paArgs[0].enmType == DBGCVAR_TYPE_NUMBER)
4116 {
4117 if (paArgs[0].u.u64Number < 0xffff)
4118 DBGCVAR_INIT_GC_FAR(&VarTssAddr, (RTSEL)paArgs[0].u.u64Number, 0);
4119 else
4120 {
4121 if (paArgs[0].enmRangeType == DBGCVAR_RANGE_ELEMENTS)
4122 return DBGCCmdHlpFail(pCmdHlp, pCmd, "Element count doesn't combine with a TSS address.\n");
4123 DBGCVAR_INIT_GC_FLAT(&VarTssAddr, paArgs[0].u.u64Number);
4124 if (paArgs[0].enmRangeType == DBGCVAR_RANGE_BYTES)
4125 {
4126 VarTssAddr.enmRangeType = paArgs[0].enmRangeType;
4127 VarTssAddr.u64Range = paArgs[0].u64Range;
4128 }
4129 }
4130 }
4131 else
4132 VarTssAddr = paArgs[0];
4133
4134 /*
4135 * Deal with TSS:ign by means of the GDT.
4136 */
4137 if (VarTssAddr.enmType == DBGCVAR_TYPE_GC_FAR)
4138 {
4139 SelTss = VarTssAddr.u.GCFar.sel;
4140 DBGFSELINFO SelInfo;
4141 rc = DBGFR3SelQueryInfo(pUVM, pDbgc->idCpu, VarTssAddr.u.GCFar.sel, DBGFSELQI_FLAGS_DT_GUEST, &SelInfo);
4142 if (RT_FAILURE(rc))
4143 return DBGCCmdHlpFail(pCmdHlp, pCmd, "DBGFR3SelQueryInfo(,%u,%d,,) -> %Rrc.\n",
4144 pDbgc->idCpu, VarTssAddr.u.GCFar.sel, rc);