VirtualBox

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

Last change on this file since 83082 was 83082, checked in by vboxsync, 5 years ago

DBGC/lm: Dump the UUID and timestamp of images in verbose mode.

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